Repository navigation
session: one terminal event per run, also on Ctrl+C and subscriber errors - #7
Merged
Merged
Conversation
…rors Two cases broke the 'exactly one terminal event' rule in AgentSession.prompt: - run.completed was emitted inside the try, so a subscriber that raised on it sent the run into 'except Exception', which emitted run.failed too. The run log recorded success while the stream ended with a failure. - KeyboardInterrupt is not an Exception, so a Ctrl+C that arrived as an exception produced no terminal event at all. The loop had already written the cancelled-turn reply; the run now ends with run.cancelled. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
AgentSession.promptis meant to end every run with exactly one terminal event (run.completed,run.cancelledorrun.failed). Two paths broke that. Both were found while writing the events chapter of the handbook, and both were reproduced with the real classes.run.completedwas emitted inside thetry. A subscriber that raised on it (for example a terminal that went away) sent control intoexcept Exception, which emittedrun.failedas well.runs.jsonlrecordedsuccesswhile the stream ended with a failure.KeyboardInterruptis not anException. When Ctrl+C reachedpromptas an exception, neither handler ran, so subscribers sawrun.startedand nothing after it.run_turnalready writes the cancelled-turn reply for this case, so the session was correct but the event stream was not.Change
run.completedmoves to theelsebranch, outside thetry. A subscriber error there still reaches the caller, but it no longer adds a second terminal event.KeyboardInterruptis handled withRunCancelledand ends the run withrun.cancelled.Tests
Two new tests in
tests/test_runtime_events.py. Both fail onmainand pass here. Full suite: 439 tests OK,ruff check .clean.Not changed
run.completedstill makespromptraise, although the turn itself succeeded and the reply is saved. Swallowing subscriber errors would be a larger decision.fanoutstill miss that event.🤖 Generated with Claude Code