Skip to content

fix(chat): preserve assistant text blocks across tool iterations#50

Merged
ABB65 merged 2 commits into
mainfrom
fix/chat-preserve-assistant-text-blocks
May 15, 2026
Merged

fix(chat): preserve assistant text blocks across tool iterations#50
ABB65 merged 2 commits into
mainfrom
fix/chat-preserve-assistant-text-blocks

Conversation

@ABB65
Copy link
Copy Markdown
Member

@ABB65 ABB65 commented May 15, 2026

Summary

Conversation loop streams assistant text deltas to SSE, but per-iteration assistantBlocks was rebuilt only from currentToolCalls when pushing into config.messages for 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.lastContent on text-only end_turn. The streaming branch never set lastAssistantContent, so the most common chat shape (plain text reply, no tool use) reached saveChatResult with [] — 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. assistantBlocks now allocated at the top of each iteration and populated as content blocks come in:

  • Streaming branch: single text buffer accumulates text deltas, flushed on tool_use_start and message_end. tool_use_end pushes block to both currentToolCalls (for tool execution) and assistantBlocks (for protocol/persistence).
  • Non-streaming branch: each block from response.content pushed directly.
  • Old reconstruction at the tool-execution boundary deleted.
  • lastAssistantContent = assistantBlocks assigned 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 empty lastContent regression that was silently producing [tool calls] placeholders
  • preserves streamed assistant text before tool use in the next model message — main protocol-amputation fix
  • preserves streamed interleaved text and tool_use block order — text→tool→text→tool ordering
  • preserves non-streaming assistant text before tool use in later iterations — iter 2+ branch correctness
  • pnpm test:unit — 408 passed (was 404)
  • pnpm typecheck clean
  • pnpm lint — 0 errors on changed files

Out of scope

DB persistence of intermediate tool iterations and tool_result blocks — belongs to the upcoming tool-trace-persistence PR (will introduce messages.content_blocks jsonb + iteration index).

Contentrain 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.
@ABB65 ABB65 merged commit 9c0f158 into main May 15, 2026
1 check passed
@ABB65 ABB65 deleted the fix/chat-preserve-assistant-text-blocks branch May 15, 2026 14:26
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