fix(turns): dispatch on_user_turn_stop_timeout synchronously so handlers see turn state - #5280
Closed
scttbnsn wants to merge 2 commits into
Closed
fix(turns): dispatch on_user_turn_stop_timeout synchronously so handlers see turn state#5280scttbnsn wants to merge 2 commits into
scttbnsn wants to merge 2 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
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.
Fixes #4848.
The bug
LLMUserAggregatorregisterson_user_turn_stop_timeoutas a regular (async) event, so the user's handler is scheduled as a fire-and-forget task. The watchdog path inUserTurnController._user_turn_stop_timeout_task_handlerthen goes straight on to_trigger_user_turn_stop(None, ...), which drains the aggregation before that task ever runs. So a handler doing the documented thing, readingaggregation_string()to decide if the turn was empty, sees""for a turn that produced a full transcript (which then arrives intact inon_user_turn_stopped). The issue's "no transcript" apology double-fire comes straight from that.The fix
Register the event with
sync=True, matching whatUserTurnControlleralready does for its ownon_user_turn_stop_timeoutregistration. The handler now runs, and finishes, before the force-stop drains the buffer.UserTurnProcessorhad the identical race: its stop-timeout event was also async-dispatched while_trigger_user_turn_stopsynchronously callshandle_user_turn_stopped()on every stop strategy right after, resetting whatever per-turn state a strategy buffers (the same patternTurnAnalyzerUserTurnStopStrategyuses forself._text). Same one-line fix there.A tradeoff to point out: the handler now runs inline before the force-stop, so a slow handler delays the stop. Sync events are supposed to be fast anyway, and both docstrings now say so on this event. Seemed better than the alternative, where the event can't do the thing its own example shows.
Tests
Two new regression tests, both red without the fix:
test_user_turn_stop_timeout_handler_sees_aggregation: watchdog force-stops a turn with a real transcript; the timeout handler'saggregation_string()must equal it.test_user_turn_stop_timeout_handler_sees_strategy_state(processor): a buffering stop strategy's per-turn state must still be readable in the timeout handler.Full turn-related suites pass (123 tests across the aggregator, controller, and processor files), ruff clean.