Repository navigation
fix(acp): end a claude turn whose prompt was never sent - #37922
Conversation
UX Review (Opus 5.5) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS This is a fix at the mechanism level, with a test that fails on base. All three prompt entries that clear What this change shipsInventory (4 items) — 4 justifiedIntent: on the claude backend, a reply Claude Code sends on its own after a failed or stopped prompt shows as its own message, instead of being held until the user's next prompt. FIX.
[FIRST-PRINCIPLES-REVIEWED] 9184a0f Description read[DESCRIPTION-READ] 2cfd5dbd3af282d9bda58b3f113d37e57776edc986c4b591e798e68b3db3647d That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Proportionate fix: the three prompt entries now set the turn-done flag on the exit path that skipped it, using the same pattern Notes
[DESIGN-REVIEWED] 9184a0f Description read[DESCRIPTION-READ] b7dc421520ddae179dfe595e2a461c14471b52cc04b7e6236bb2bbef14abdbc5 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: |
f517a8f to
073b392
Compare
073b392 to
85087c1
Compare
85087c1 to
f38e0d7
Compare
A prompt entry (send_message, send_message_stream, stream_events) clears _turn_done before it sends, and only _prompt_loop sets it again. When the entry ends before its prompt goes out (ensure_ready or _send_prompt raises, or the turn is cancelled there), _turn_done stayed clear. On claude that keeps the idle reader off until the next prompt, so a reply Claude Code sends on its own meanwhile (a background command finishing) waits in the pipe and shows under the user's next message. A later Stop's wait_turn_done also waited out its whole budget. Each entry now sets _turn_done on that exit and offers the idle reader a restart. On every other backend the restart is a no-op. Refs #17566
f38e0d7 to
9184a0f
Compare
Code change approver recommendation: ApproveWhy: the decider estimates an 8% chance that this change introduces a defect, within the 12% limit for approving on its own, and every check passes. On the claude backend, a prompt entry (send_message, send_message_stream, stream_events) that fails or is cancelled before its prompt goes out now sets _turn_done again and offers the idle reader a restart. This matches what _send_prompt's own refusal sites already do. The idle reader starts only if no one has claimed stdout, the turn is done and the process is alive. A retried prompt claims stdout through the wrapped ensure_ready, which stops the reader first, and the agent-turn gate orders prompts behind an open agent-started stream. stream_command never clears _turn_done, and stream_unsolicited and the shared handle already set it again on every exit. launch_tuning's "turn in flight" refusal is also corrected as a side effect. A cancel after the frame was written makes the prompt's tail show as an agent-started turn, which the existing design already handles; on main it would have shown under the next prompt, so this is not a regression. The new parametrized test covers all three entries with both a refusal and a cancel, and the spec is updated in the same commit. 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.
Approving. The review checked the whole diff on 9184a0f. All three prompt entries (send_message, send_message_stream, stream_events) now set _turn_done again when ensure_ready or _send_prompt raises or is cancelled, and the original exception still propagates. On non-claude backends _maybe_start_idle_reader returns at once. The try block holds no yield. The new parametrized test fails on main because the idle reader never starts there. The spec paragraph in acp-client.md ships in the same commit.
Done well: the test pushes a real agent_message_chunk through the real idle reader and asserts that on_agent_turn fires. So it pins the symptom from #17566 end to end, not just the flag.
Optional:
- The
exceptalso runs when a write was attempted and then stalled or was cancelled after the frame was buffered. The spec paragraph and the_release_unsent_turndocstring say "before its prompt is sent", and "Not a goal" reads as if that case is unchanged. One sentence would make the prose match the code. The late tail is still read as its own agent-started turn. - The new test injects its failure only at
_send_prompt. A third case whereensure_readyraises would pin the whole try boundary. - The Proof: line names the earlier head 073b392. Its patch is identical, but re-running the revert proof on this head would make the evidence match.
Problem / Motivation
Goal: On the claude backend, a reply Claude Code sends on its own after a turn (for example when a background command finishes) still shows as its own message after a prompt that failed or was stopped before it was sent, instead of waiting for the user's next message.
send_message,send_message_streamandstream_eventsclear_turn_donebefore they send, and only the turn's read (_prompt_loop) sets it again. When the entry ends before its prompt goes out (ensure_readyraises,_send_promptraises while it builds or writes the prompt, or the task is cancelled there, for example by a Stop during preparation),_turn_donestays clear. The claude idle reader starts only once the turn is done, so it stays off until the next prompt that reaches_prompt_loop. A reply Claude Code then writes on its own waits in the pipe, and the user's next prompt reads it first: it shows under that message, all at once. A later Stop'swait_turn_donealso waits out its whole budget.Why it matters
The claude backend starts its own turns whenever a
run_in_backgroundcommand or a background subagent finishes. After one failed or stopped prompt, every such reply in that session is held back until the user types again and is then glued onto an unrelated answer, which is #17566's symptom.Not a goal
_mcp_notifications; recorded for a follow-up._prompt_looparmed itsfinally.What changed (motivation → approach → change)
_turn_done, and nothing set it again because_prompt_loopnever ran, so_maybe_start_idle_readerrefused to start.ensure_readyand_send_promptintry/except BaseException. On that exit, a new module helper_release_unsent_turn(acp/client_turn.py) sets_turn_doneand calls_maybe_start_idle_reader(), then the exception propagates unchanged._send_prompt's own refusal sites already set_turn_donethe same way.acp-client.md("Idle reader and agent-started turns") records the rule.Backwards compatibility
Compatible: the success path is unchanged, and on failure the same exception still propagates. On a backend without the idle reader,
_maybe_start_idle_readerreturns at once becauseon_agent_turnis None, so the only effect there is that_turn_doneis set, whichwait_turn_doneexpects.Removes nothing: the change adds a failure-path release and a private helper; nothing main accepts is rejected, renamed or removed.
Tests
test/test_acp_client_agent_started_turns.py::test_a_prompt_that_never_went_out_leaves_the_reader_free_to_start, overstream_events,send_message_streamandsend_message, each with_send_promptrefused (AcpError) or cancelled (CancelledError): the entry still raises,_turn_doneis set, and anagent_message_chunkfed afterwards is handed off toon_agent_turn.Proof: PROVEN test/test_acp_client_agent_started_turns.py::test_a_prompt_that_never_went_out_leaves_the_reader_free_to_start (all 6 cases fail with the
client_turn.pychange reverted; head 073b392)Related suites run locally:
test/test_acp_client*.py,test/test_client_bindings.py,test/test_dashboard_agent_started_turn.py,test/test_claude_raw_sdk_tasks.py,test/test_reply_format_once.py,test/test_session_provider_agent_turns.py: all pass.Manual verification
N/A — unit coverage sufficient: the defect is a state flag on a failure path, driven here through a real
asyncio.StreamReaderand the real idle reader. Live claude-agent-acp 0.94.0 captures confirmed the frame order this relies on: the user turn's response comes before the autonomous cycle's chunks and itsusage_updatewith origintask-notification.Related Issues
Refs #17566
Sibling sweep
Swept:
git grep -n "_turn_done.clear()" -- src/kiro_crew/acp/Sibling: src/kiro_crew/acp/client_turn.py -- changed here -- send_message, send_message_stream and stream_events
Sibling: src/kiro_crew/acp/session_handle_turn.py -- not affected -- the shared handle clears it inside _run_turn, whose own except arms set it again
Sibling: src/kiro_crew/acp/client_turn.py:stream_unsolicited -- not affected -- its finally already sets _turn_done when the read ended before _prompt_loop armed
Pattern harvest
Not generalizable: the one-shot flag pairing is specific to
AcpClient's prompt entries, and the three sites are fixed together here.Checklist
Contribution License Agreement
Contributed under the project's Apache-2.0 license.