fix(chat): preserve assistant text blocks across tool iterations#50
Merged
Conversation
added 2 commits
May 15, 2026 17:02
The conversation loop streams assistant text deltas to SSE, but the
per-iteration `assistantBlocks` was rebuilt only from `currentToolCalls`
when pushing back into `config.messages` for the next Anthropic call.
Two concrete regressions fell out of this:
1. Multi-turn protocol amputation. The next iteration saw the prior
assistant turn as `[tool_use]` only — Claude's own narration
("I'll check the schema first, then update X") was missing from
its view of the conversation. Anthropic accepts the payload but
downstream tool sequencing degrades.
2. Empty `done.lastContent` on text-only end_turn. The streaming
branch never set `lastAssistantContent`, so the most common chat
shape (a plain text reply with no tool use) reached `saveChatResult`
with `[]` — DB ended up storing the `'[tool calls]'` placeholder
from the fallback at db.ts:250 instead of the actual assistant text.
Fix is purely in-memory: `assistantBlocks` is now allocated at the top
of each iteration and populated as content blocks come in (streamed
text accumulates into a single block flushed at tool_use_start /
message_end; tool_use is pushed alongside; non-streaming branch pushes
every block from `response.content`). The old reconstruction at the
tool-execution boundary is gone, and `lastAssistantContent` is assigned
once outside the streaming/non-streaming split so end_turn and
tool_use both produce the correct final shape.
Four regression tests cover: text-only end_turn, streamed text → tool
→ next iteration, interleaved text/tool ordering, and non-streaming
iter 2+ text + tool_use. No schema or persistence changes — DB
storage of intermediate tool iterations and tool_result blocks stays
out of scope for the upcoming tool-trace-persistence PR.
…tText
The integration test was asserting that `saveChatResult` is called
with `('', [])` for assistantText and assistantContent — which is the
exact buggy behavior the previous commit fixed. With streamed text now
correctly preserved end-to-end, the chat-route test must expect the
populated values it receives from the mock provider.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Conversation loop streams assistant text deltas to SSE, but per-iteration
assistantBlockswas rebuilt only fromcurrentToolCallswhen pushing intoconfig.messagesfor the next Anthropic call. Two concrete regressions fell out of this:1. Multi-turn protocol amputation. Next iteration saw prior assistant turn as
[tool_use]only — Claude's own narration ("I'll check the schema first, then update X") was missing from its view. Anthropic accepts the payload but downstream tool sequencing degrades.2. Empty
done.lastContenton text-only end_turn. The streaming branch never setlastAssistantContent, so the most common chat shape (plain text reply, no tool use) reachedsaveChatResultwith[]— DB stored the'[tool calls]'placeholder fallback (db.ts:250) instead of the actual assistant text. This silently broke chat history persistence.Fix
Purely in-memory.
assistantBlocksnow allocated at the top of each iteration and populated as content blocks come in:textdeltas, flushed ontool_use_startandmessage_end.tool_use_endpushes block to bothcurrentToolCalls(for tool execution) andassistantBlocks(for protocol/persistence).response.contentpushed directly.lastAssistantContent = assistantBlocksassigned once outside the split, before the stopReason check — so end_turn and tool_use both yield the correct final shape.Test plan
Four regression tests in
conversation-engine-regression.test.ts:returns streamed text-only responses in done.lastContent— guards the emptylastContentregression that was silently producing[tool calls]placeholderspreserves streamed assistant text before tool use in the next model message— main protocol-amputation fixpreserves streamed interleaved text and tool_use block order— text→tool→text→tool orderingpreserves non-streaming assistant text before tool use in later iterations— iter 2+ branch correctnesspnpm test:unit— 408 passed (was 404)pnpm typecheckcleanpnpm lint— 0 errors on changed filesOut of scope
DB persistence of intermediate tool iterations and
tool_resultblocks — belongs to the upcoming tool-trace-persistence PR (will introducemessages.content_blocksjsonb + iteration index).