feat(run): lifecycle interrupt + live input injection; cancel-aware tools; safer tracing #1557
+823
−72
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Included in this PR
Resolves #798
active_run
handle onRunner.run
andRunner.run_streamed
cancel(reason)
for a clean stopinject(items)
included on the next turnstate()
for a tiny debug snapshotStreamed runs emit a cancelled status event and close the queue; non-streamed return a terminal result without final output.
Public APIs stay the same; tracing and hook order are preserved for normal completions.
Tests
New tests cover cancel, inject, and status emission in both streamed and non-streamed paths.
RunState + run_single_step (next step)
What I'm thinking is that this PR lays the groundwork for a proper step API while solving the immediate problem of cancellation and live input injection. The follow-up I’m proposing could perhaps follow this path (all open to discussion!):
Introduce a light
RunState
that captures current agent, turn, items, and bookkeeping.Expose
run_single_step(state)
->new_state
that does “call model → run tools/handoffs → decide next”.Keep Runner.run* as a thin loop over run_single_step for backward compatibility.
Add step-level tests and examples so folks can drive agents interactively.
If this direction sounds good, I can open a focused PR right after this one that sketches
RunState
and a minimalrun_single_step
while maintaining today’s surface.Happy to make changes to better align with roadmap.