Skip to content

fix(acp): keep a shared-runtime agent turn through a request wait - #38853

Merged
KiroCrewCaptain merged 1 commit into
mainfrom
fix/37904-agent-turn-request-wait
Nov 19, 2026
Merged

KiroCrewCaptain merged 1 commit into
mainfrom
fix/37904-agent-turn-request-wait

Conversation

@bolichen97

@bolichen97 bolichen97 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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_sharing on, the session handle hands a turn the agent starts on its own (for example the reply after a run_in_background command) 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_sharing preview 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

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:

Piece What it does now
session_handle_lifecycle._wait_for_response When it buffers the pending agent turn's first frame, it records an asyncio.Event on the handle (_agent_turn_hold_end) and sets it in its finally, after the frames are back in the queue.
session_handle_agent_turns.stream_unsolicited It resolves the prompt timeout first (a config read on a thread), then waits while that event is unset, so the config read leaves no gap between that check and the turn's drain. The turn then reads the reply as its own message, and a message sent meanwhile queues behind it on the chat, as for any running turn.
session_handle_class _agent_turn_hold_end is 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 finally sets 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_end as 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_sharing on, through chat._run_chat with a real SessionManager, so the chat was served by an AcpSessionHandle on the shared runtime. Prompt: the issue's run_in_background request. 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.

main this PR
The turn opened for the agent's reply ended at once, with the wait still running waited for the wait, then read the reply
The follow-up started at once on the free chat queued behind the agent's turn
What the chat showed follow-up row: "The command finished with"; a later agent row: "exit code 0 and printed probe-done." agent row: "The background command finished with exit code 0 and printed probe-done."; follow-up row: "No, the only background command I started has finished, and nothing else is running."

Happy 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 /compact turn, which holds the chat while it waits, so the agent's turn and a follow-up both queue behind it
Sibling: 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

  • A Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

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
@github-actions

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 9cbd56b139593cd777def64806f0ca01b95c133f touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 9cbd56b139593cd777def64806f0ca01b95c133f — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

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 ships

Inventory (4 items) — 4 justified

Intent: 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.

  1. While a request wait holds the first frame of a turn the agent started, that turn now waits for the wait to end, then reads the reply as its own row — justified
  2. A follow-up sent during such a wait now queues behind the agent's turn instead of starting at once — justified
  3. The prompt timeout is now resolved before the hold check, not inside the turn — justified
  4. The pinned test outcome changes from [[], ["printed probe-done"]] to the agent's reply plus a separate follow-up answer. The description says it was rewritten and states what main did — justified

[FIRST-PRINCIPLES-REVIEWED] 9cbd56b

Description read

[DESCRIPTION-READ] fb6f3b7a36852f80b28f0fec586c13362dbc3c48b0e2438b09a1fe9edbcdf49b

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of 9cbd56b139593cd777def64806f0ca01b95c133f — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

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 finally always sets the event, so the agent's turn can never wait forever.

Notes

  • The old assertion [[], ["printed probe-done"]] pinned the empty first turn, which is the bug itself, not a behaviour anyone chose. Rewriting it is correct.

[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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 9cbd56b139593cd777def64806f0ca01b95c133f — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 9cbd56b

Verdict parsed from the review's SHA-scoped output markers for commit 9cbd56b139593cd777def64806f0ca01b95c133f.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 9cbd56b139593cd777def64806f0ca01b95c133f false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 9cbd56b139593cd777def64806f0ca01b95c133f and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 9cbd56b

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 9cbd56b139593cd777def64806f0ca01b95c133f false-positive: <reason, 20 to 500 characters>

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Nov 18, 2026
@KiroCrewCaptain

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: A person should decide

Why: 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 9cbd56b139 · 87 changed lines. If this recommendation is wrong, reply here: replies are how the approver is evaluated.

@KiroCrewCaptain KiroCrewCaptain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@KiroCrewCaptain
KiroCrewCaptain merged commit b6b9b3e into main Nov 19, 2026
105 of 108 checks passed
@KiroCrewCaptain
KiroCrewCaptain deleted the fix/37904-agent-turn-request-wait branch November 19, 2026 11:46
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Nov 19, 2026
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.

Claude shared runtime: an agent-started turn is lost through a request wait, and a silent agent cycle holds the chat

2 participants