8000 session: one terminal event per run, also on Ctrl+C and subscriber errors by Jiminyang1 · Pull Request #7 · Jiminyang1/minibot · GitHub
[go: up one dir, main page]

Skip to content

session: one terminal event per run, also on Ctrl+C and subscriber errors - #7

Merged
Jiminyang1 merged 1 commit into
mainfrom
fix/run-terminal-events
Nov 17, 2026
Merged

Jiminyang1 merged 1 commit into
mainfrom
fix/run-terminal-events

Conversation

@Jiminyang1
Copy link
Copy Markdown
Owner

Problem

AgentSession.prompt is meant to end every run with exactly one terminal event (run.completed, run.cancelled or run.failed). Two paths broke that. Both were found while writing the events chapter of the handbook, and both were reproduced with the real classes.

  1. Two terminal events. run.completed was emitted inside the try. A subscriber that raised on it (for example a terminal that went away) sent control into except Exception, which emitted run.failed as well. runs.jsonl recorded success while the stream ended with a failure.
  2. No terminal event. KeyboardInterrupt is not an Exception. When Ctrl+C reached prompt as an exception, neither handler ran, so subscribers saw run.started and nothing after it. run_turn already writes the cancelled-turn reply for this case, so the session was correct but the event stream was not.

Change

  • run.completed moves to the else branch, outside the try. A subscriber error there still reaches the caller, but it no longer adds a second terminal event.
  • KeyboardInterrupt is handled with RunCancelled and ends the run with run.cancelled.

Tests

Two new tests in tests/test_runtime_events.py. Both fail on main and pass here. Full suite: 439 tests OK, ruff check . clean.

Not changed

  • A subscriber that raises on run.completed still makes prompt raise, although the turn itself succeeded and the reply is saved. Swallowing subscriber errors would be a larger decision.
  • Subscribers that come after a failing one in fanout still miss that event.

🤖 Generated with Claude Code

…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>
@Jiminyang1
Jiminyang1 merged commit d576659 into main Nov 17, 2026
4 checks passed
Jiminyang1 added a commit that referenced this pull request Nov 17, 2026
Brings in #7–#11. #11 already contains this branch's base commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0