Repository navigation
fix(acp): keep a shared-runtime agent turn through a request wait - #38853
Conversation
On a claude chat served by the shared runtime, a request wait (a live model or effort change) that starts while a turn the agent began waits to be read takes that turn's frames into its own buffer until its answer arrives. The dashboard turn opened for the agent then found an empty queue and ended at once, which freed the chat: a message sent before the wait returned started a prompt, the wait put the frames back while that prompt read, and the agent's reply showed at the front of the follow-up's answer. The wait now marks when it holds the agent turn's first frame, and stream_unsolicited() waits until the wait has put it back before it reads, as the per-chat client does since #38313. The turn then reads the reply as its own message, and a message sent meanwhile queues behind it. Closes #37904
UX Review (Opus 5.5) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks. The fix is a port of the per-chat client's existing hold rule to the shared-runtime handle, and the issue is linked (#37904). What this change shipsInventory (4 items) — 4 justifiedIntent: on a claude chat that uses the shared runtime, the agent's own reply should stay its own message when a request wait (such as a live model change) is running as it arrives, and a follow-up sent meanwhile should get only its own answer. This is a FIX.
[FIRST-PRINCIPLES-REVIEWED] 9cbd56b Description read[DESCRIPTION-READ] fb6f3b7a36852f80b28f0fec586c13362dbc3c48b0e2438b09a1fe9edbcdf49b That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: this ports the per-chat client's hold-until-requeued rule to the session handle, and the request wait's Notes
[DESIGN-REVIEWED] 9cbd56b Description read[DESCRIPTION-READ] 60c1a8d08ea2cf7fd6108181122d8dcadd089b979746fd704f149d05a919ab4e That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
Code change approver recommendation: A person should decideWhy: the decider estimates a 12% chance that this change introduces a defect, within the 12% limit for approving on its own, but it is held at tier pending: being reviewed. Copies the per-chat client's hold rule to the shared-runtime session handle. _wait_for_response sets an event on the handle when it takes the pending agent turn's first frame, and sets it in its finally after the frames are back in the queue. The finally runs on every exit (answer, timeout, cancel, runtime death), so the agent's turn can't wait forever. stream_unsolicited resolves its timeout first, then waits while the event is unset. If a second wait takes the frame, the loop re-reads the field and waits on that one too. Putting the frames back starts no second hand-off, because _agent_turn_start is still set. The rewritten regression test drives the issue's steps through the dashboard slot, and by reading the code I agree it would fail on main. The bindings test is updated, and CI and all AI review lanes are green. Tier: pending: being reviewed. An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit |
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
I reviewed head 9cbd56b and ran the changed shared-runtime agent turn tests and the session handle binding tests on it. All pass. The rewritten scenario test fails when the two source files are reverted to main, so it covers the reported bug. The new event is created only when a request wait holds the agent turn's first frame. The wait's finally block sets it on every exit path (answer, timeout, cancel, runtime death), so the agent turn cannot wait longer than the request does. The change follows the per-chat client's existing rule and finds no correctness, security or data loss problem, so nothing blocks.
Problem / Motivation
Goal: On a claude chat served by the shared runtime, a reply that Claude Code sends on its own still shows up as its own message when a request wait (such as a live model change) is running as it arrives, and a message the user sends meanwhile gets only its own answer.
With
chat_runtime_sharingon, the session handle hands a turn the agent starts on its own (for example the reply after arun_in_backgroundcommand) to the chat, and the chat opens a turn that reads it. A request wait on the same session (_wait_for_response: a live model or effort change, a slash command) takes every queued frame into its own buffer until its answer arrives. When it takes the agent turn's first frame before that turn reads, the turn finds an empty queue and ends at once, which frees the chat. A follow-up sent before the wait returns then starts a prompt, the wait puts the frames back while that prompt reads, and the agent's reply shows at the front of the follow-up's answer: #17566's symptom, on the shared-runtime path.Why it matters
Anyone on the claude backend with the
chat_runtime_sharingpreview on can hit it: a model or effort change while a background command finishes, then a message, and the agent's reply is shown as the start of the wrong answer. The per-chat client no longer does this since #38313, so the same chat behaves differently depending on a preview switch.Not a goal
AcpClientpath: fix(acp): keep an agent-started turn readable through a request wait #38313 fixed it there.test_a_prompt_that_starts_first_reads_the_agents_frames_as_its_own).What changed (motivation → approach → change)
Symptom: a follow-up sent while a request wait runs gets the agent's own reply glued to its answer. Cause: the turn opened for the agent's reply does not know the wait holds that reply's first frame, so it reads an empty queue, ends, and frees the chat for the follow-up. Change, the per-chat client's rule ported to the session handle:
session_handle_lifecycle._wait_for_responseasyncio.Eventon the handle (_agent_turn_hold_end) and sets it in itsfinally, after the frames are back in the queue.session_handle_agent_turns.stream_unsolicitedsession_handle_class_agent_turn_hold_endis a class default (None) beside the other agent-turn fields, so a handle that never holds a frame reads the default.The wait always ends, by its answer, its timeout, a cancel or the runtime's death, and its
finallysets the event on each of those paths, so the turn never waits longer than the request does.Backwards compatibility
Compatible: the only new behaviour is that an agent-started turn opened while a request wait holds its first frame waits for that wait to return. Every other turn and every other backend reads as on main.
Removes nothing: the change adds one wait and one field; no path, key, record or public function is removed.
Tests
test_a_reply_held_by_a_request_wait_reaches_the_dashboard_as_its_own_turn(rewritten): the issue's steps through the dashboard's slot claim (chat_runner._on_agent_turn) over a real shared-runtime session handle. The agent's reply is handed off, a request wait holds its frames, and a follow-up is sent before the wait's answer arrives. The turn opened for the reply reads it whole, and the follow-up's answer carries only its own text. On main the first turn read nothing and the follow-up's answer started with the agent's reply.test_session_handle_bindings.py: pins_agent_turn_hold_endas a data member of the handle.Proof: PROVEN test/test_claude_shared_agent_turns.py::test_a_reply_held_by_a_request_wait_reaches_the_dashboard_as_its_own_turn, test/test_session_handle_bindings.py::test_the_member_set_is_the_frozen_inventory, test/test_session_handle_bindings.py::test_each_member_keeps_its_kind_shape_and_owner[_agent_turn_hold_end] (head 9cbd56b)
Determinism: the rewritten test passed 10 of 10 runs at
-n0, and its whole file passed in reversed order and in three shuffled orders (pytest-randomly seeds 12345, 424242, 987654321).Manual verification
Run on Linux (Ubuntu 24.04.5 LTS) with the claude backend live (claude-agent-acp from npm, Claude Code over Bedrock) and
chat_runtime_sharingon, throughchat._run_chatwith a realSessionManager, so the chat was served by anAcpSessionHandleon the shared runtime. Prompt: the issue'srun_in_backgroundrequest. When Claude Code started its own turn for the result, a request wait on that session was held open for 20 s, and the follow-up "In one short sentence: is anything still running in the background?" was sent 3 s later through the slot's own queue-or-run path.mainHappy path on this PR, with no request wait: the agent's reply "It finished and printed probe-done." was its own row with
origin: {"kind": "agent"}, and the follow-up's row held only its own answer.The same host ran the project's full local gate list on this commit (66 entries,
scripts/local-gate.py, semgrep, the flake-pattern ratchet): all exited 0.Real-machine verification
Machine: EC2 m7i.2xlarge, Ubuntu 24.04.5 LTS x86_64, 8 vCPU / 32 GiB
Before (main 433d99b):
python -m pytest -n0 -q -p no:randomly "test/test_claude_shared_agent_turns.py::test_a_reply_held_by_a_request_wait_reaches_the_dashboard_as_its_own_turn" test/test_session_handle_bindings.py(this PR's tests on main's code) ->3 failed, 541 passed:assert [[], ['printed probe-done', "the follow-up's answer"]] == [['printed probe-done'], ["the follow-up's answer"]]. The live claude run in the table above ended the agent's turn at once during the request wait.After (head 9cbd56b):
python -m pytest -n0 -q -p no:randomly "test/test_claude_shared_agent_turns.py::test_a_reply_held_by_a_request_wait_reaches_the_dashboard_as_its_own_turn" test/test_session_handle_bindings.py->544 passed. The same live claude run queued the follow-up behind the agent's turn, and the agent's reply was its own row.Cleanup: instance terminated, no key pair, no volumes left (describe output empty for the instance and for volumes attached to it); the security group, which has no inbound rules, is shared by the project's verification machines and kept
Related Issues
Closes #37904
Related: #17566, #38313 (the per-chat client's version of this fix), #37932 (the issue's second item).
Sibling sweep
Swept: git grep -n "self._queue.get\b|self._queue.get(|_queue.get_nowait()" -- src/kiro_crew/acp/session_handle*.py src/kiro_crew/acp/session_provider*.py
Sibling: src/kiro_crew/acp/session_handle_lifecycle.py -- changed here
Sibling: src/kiro_crew/acp/session_handle_compaction.py -- not affected -- on claude its only chat caller is the
/compactturn, which holds the chat while it waits, so the agent's turn and a follow-up both queue behind itSibling: src/kiro_crew/acp/session_handle_mcp.py -- not affected -- its drains run while a session starts, loads or switches agent, before the chat watches for agent turns
Sibling: src/kiro_crew/acp/session_handle_turn.py -- not affected -- the pre-turn drain is the reader that takes an agent turn's frames as its own (
_take_agent_turn_frames)Sibling: src/kiro_crew/acp/session_handle_dispatch.py -- not affected -- the turn's own dispatch loop, which reads the frames rather than holding them
Pattern harvest
Not generalizable: the gap is specific to a session queue that two readers share while one of them hands a turn to the chat; the per-chat client and now the session handle each carry the one rule.
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits