Skip to content

fix(acp): end a claude turn whose prompt was never sent - #37922

Merged
KiroCrewCaptain merged 1 commit into
mainfrom
fix/claude-stream-tail
Nov 16, 2026
Merged

KiroCrewCaptain merged 1 commit into
mainfrom
fix/claude-stream-tail

Conversation

@iamwhatever

@iamwhatever iamwhatever commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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_stream and stream_events clear _turn_done before they send, and only the turn's read (_prompt_loop) sets it again. When the entry ends before its prompt goes out (ensure_ready raises, _send_prompt raises while it builds or writes the prompt, or the task is cancelled there, for example by a Stop during preparation), _turn_done stays 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's wait_turn_done also waits out its whole budget.

Why it matters

The claude backend starts its own turns whenever a run_in_background command 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

What changed (motivation → approach → change)

  • Symptom: after a prompt entry raised or was cancelled before sending, the next agent-started reply waited for the next user prompt.
  • Root cause: the entry cleared _turn_done, and nothing set it again because _prompt_loop never ran, so _maybe_start_idle_reader refused to start.
  • Change: each of the three entries wraps ensure_ready and _send_prompt in try/except BaseException. On that exit, a new module helper _release_unsent_turn (acp/client_turn.py) sets _turn_done and calls _maybe_start_idle_reader(), then the exception propagates unchanged. _send_prompt's own refusal sites already set _turn_done the 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_reader returns at once because on_agent_turn is None, so the only effect there is that _turn_done is set, which wait_turn_done expects.

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, over stream_events, send_message_stream and send_message, each with _send_prompt refused (AcpError) or cancelled (CancelledError): the entry still raises, _turn_done is set, and an agent_message_chunk fed afterwards is handed off to on_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.py change 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.StreamReader and 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 its usage_update with origin task-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

  • At most two commits (one is the norm), with a Conventional Commits title
  • 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

Contribution License Agreement

Contributed under the project's Apache-2.0 license.

@iamwhatever
iamwhatever requested review from a team and KiroCrewCaptain as code owners November 16, 2026 09:47
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 16, 2026
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

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

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 9184a0f3409e3a139dd18575b4b4eba404b302df — 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

This is a fix at the mechanism level, with a test that fails on base. All three prompt entries that clear _turn_done get the change together, and session_handle_turn.py and stream_unsolicited already set it again on their exit paths.

What this change ships

Inventory (4 items) — 4 justified

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

  1. On the claude backend, a reply Claude Code sends on its own after a prompt that failed or was cancelled before sending now shows as its own message, instead of under the next prompt — justified
  2. After such a prompt, a later Stop's wait_turn_done no longer waits out its whole budget — justified
  3. New private helper _release_unsent_turn, called from three entries (send_message, send_message_stream, stream_events) — justified
  4. A paragraph in acp-client.md "Idle reader" stating the release rule (same-commit spec update required by AGENTS.md) — justified

[FIRST-PRINCIPLES-REVIEWED] 9184a0f

Description read

[DESCRIPTION-READ] 2cfd5dbd3af282d9bda58b3f113d37e57776edc986c4b591e798e68b3db3647d

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

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

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

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 _send_prompt's own refusal sites already use.

Notes

  • The except BaseException also catches a raise or cancel inside _send_request after the prompt frame is written. That starts the idle reader while the turn's response is still in the pipe. "Not a goal" scopes this out, and the reader puts the frames back into _buffer in order, so the risk is low and a revert undoes it. Consider pinning it with a test.

[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 .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

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 9184a0f3409e3a139dd18575b4b4eba404b302df — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 9184a0f

Verdict parsed from the review's SHA-scoped output markers for commit 9184a0f3409e3a139dd18575b4b4eba404b302df.

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

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 9184a0f3409e3a139dd18575b4b4eba404b302df and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 9184a0f

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

@iamwhatever
iamwhatever force-pushed the fix/claude-stream-tail branch from f517a8f to 073b392 Compare November 16, 2026 10:05
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Nov 16, 2026
@iamwhatever
iamwhatever force-pushed the fix/claude-stream-tail branch from 073b392 to 85087c1 Compare November 16, 2026 11:20
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Nov 16, 2026
@iamwhatever
iamwhatever force-pushed the fix/claude-stream-tail branch from 85087c1 to f38e0d7 Compare November 16, 2026 12:21
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Nov 16, 2026
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
@iamwhatever
iamwhatever force-pushed the fix/claude-stream-tail branch from f38e0d7 to 9184a0f Compare November 16, 2026 13:53
@github-actions github-actions Bot added 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 16, 2026
@KiroCrewCaptain

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: Approve

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

@KiroCrewCaptain
KiroCrewCaptain enabled auto-merge (squash) November 16, 2026 15:38

@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.

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 except also runs when a write was attempted and then stalled or was cancelled after the frame was buffered. The spec paragraph and the _release_unsent_turn docstring 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 where ensure_ready raises 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.

@KiroCrewCaptain
KiroCrewCaptain merged commit 5d92564 into main Nov 16, 2026
99 checks passed
@KiroCrewCaptain
KiroCrewCaptain deleted the fix/claude-stream-tail branch November 16, 2026 15:39
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.

2 participants