fix(run): persist the max-turns handler output to the session - #4160
Merged
Conversation
seratch
approved these changes
Aug 4, 2026
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.
Summary
When a
max_turnsrun error handler returns a final output withinclude_in_history=True, the non-streamed runner never wrote the synthesized assistant message to the session.save_result_to_sessionusesrun_state._current_turn_persisted_item_countas a slice offset into thenew_itemslist it is given. That is only valid when the caller passes the cumulative turn item list. The max-turns block passes a fresh one-item list[synthesized_item], and it runs before the per-turn counter reset, so any previous turn that persisted at least one item makesalready_persisted >= len(new_items)and nothing is saved at all. The next run on that session has no record of the assistant's answer.The streamed path resets the counter before the max-turns check and persists the item correctly, so the two paths silently disagreed on what ends up in history.
Fix: pass
run_state=Nonewith an explicitreasoning_item_id_policyat that call site, matching whatsave_resumed_turn_itemsalready does for the same "this is not a cumulative turn list" situation. Per-turn counter semantics are untouched everywhere else.Test plan
Added a shared helper in
tests/test_max_turns.pythat runs one tool turn, tripsmax_turns=1with a handler returninginclude_in_history=True, and asserts the resultingSQLiteSessioncontents.main(session is missing the finalmessage), passes with the fixtests/test_max_turns.py→ 22 passed.tests/test_agent_runner.py tests/test_run_state.py tests/memory tests/test_hitl_session_scenario.py→ 579 passed..agents/skills/code-change-verification/scripts/run.shpassed.Issue number
N/A — found while auditing session persistence parity between the streamed and non-streamed runners.
Checks
.agents/skills/code-change-verification/scripts/run.sh/reviewbefore submitting this PR