Skip to content

Fix Claude Code follow-up/compact 400s and duplicated assistant text - #1

Open
zhaohui-yang wants to merge 1 commit into
dwgx:mainfrom
zhaohui-yang:fix/claude-code-tool-resume-and-dedupe
Open

Fix Claude Code follow-up/compact 400s and duplicated assistant text#1
zhaohui-yang wants to merge 1 commit into
dwgx:mainfrom
zhaohui-yang:fix/claude-code-tool-resume-and-dedupe

Conversation

@zhaohui-yang

Copy link
Copy Markdown

Claude Code (and other Anthropic clients) resend the full message list every turn, including tool_result blocks from already-finished rounds. Three bugs in the protocol adapter/tool relay show up on that path:

  1. Duplicated assistant text. Streaming already forwarded assistant deltas, then run.wait().result.result was appended again because #hasText looked at sink.parts. The Anthropic SSE writer stores text on accum, so the check was always false after a streamed turn.

  2. Follow-up 400 no matching pending tool call. digToolResults scanned the entire messages array. Historical tool_result ids no longer map to a live RelayTurn.

  3. Autocompact then fails with the same 400. Compact requests often end with finished tool_result blocks after the last assistant message. Returning 400 aborts compact and Claude Code surfaces Prompt is too long · automatic compaction failed. Stale ids now log a warning and start a new run (folded prompt) instead of 400.

Tradeoff on (3): a client that retries tool results for a turn that already expired will bill a new Cloud Agents run rather than get 400. That is the interoperable behavior for Claude Code; tests updated accordingly.

Reproduction:

  • Point Claude Code at /v1/messages, run a turn that calls a client tool, then send a second user prompt.
  • Same setup: after a tool-heavy turn, trigger automatic compact.

Ignore historical tool_result when deciding resume, treat stale ids as a
new run instead of 400, and do not append result.result after streamed
assistant text.
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.

1 participant