applyChunkToParts/isReplayChunk can persist a needsApproval tool part without its input (tool-input-delta field mismatch + tool-approval-request ordering)
Summary
Two unit-level defects in applyChunkToParts / isReplayChunk (src/chat/message-builder.ts) can leave a needsApproval tool part persisted without its input under specific chunk orderings. If that happens, the post-approval continuation reconstructs the tool call from the persisted part and runs execute with empty input — while the client UI (which accumulates input deltas correctly) shows the full arguments in the approval card. For a human-in-the-loop feature that's the bad case: the action the user approved is not the action that executes, and for tolerant tools it's a silent no-op/wrong mutation.
Scoping note (honesty about impact): we found this while chasing a production failure that ultimately turned out to be an unrelated application-side bug on our end, so we have not confirmed a live stream ordering that triggers these paths. Filing anyway because the code paths are provably wrong (pure-function repro below, no model or DO needed) and the failure mode, if an ordering ever produces it, is severe and very hard to attribute.
Versions
agents0.16.2 (AIChatAgent, WS transport, server-held history)@cloudflare/ai-chat0.8.6,ai6.0.208
The defects
applyChunkToParts / isReplayChunk are what the DO uses (via _streamSSEReply) to rebuild the assistant message from stream chunks for persistence:
-
tool-input-deltanever accumulates input text. The handler does:if (toolPart && toolPart.state === "input-streaming") toolPart.input = chunk.input;but the v6 UI stream protocol carries the delta in
inputTextDelta;chunk.inputis alwaysundefined. Deltas contribute nothing, so the part'sinputstaysundefineduntiltool-input-availablearrives — and there is no recovery if it doesn't, or if it's discarded (below). -
A
tool-input-availablethat arrives aftertool-approval-requestis discarded. Inai@6streamText, the parsed tool-call part (which becomestool-input-available) is enqueued on the main transform stream while thetool-approval-requestis enqueued on the tool-results stream; the two are merged, and we did not find a documented ordering guarantee across the merge. If the approval request lands first, the part flips toapproval-requestedand the latetool-input-availableis dropped twice over:isReplayChunkclassifies it as a replay (existing.state !== "input-streaming") so_streamSSEReplyskips it entirely, and- even if it reached
applyChunkToParts, thetool-input-availablehandler only assigns inputif (p.state === "input-streaming").
If either path leaves the persisted part with input: undefined, the continuation after approval converts it via convertToModelMessages (input: part.input) and executeToolCall runs execute(undefined).
Repro (pure, no model needed)
Feed the chunk sequences through the same isReplayChunk → applyChunkToParts pipeline _streamSSEReply uses:
import { applyChunkToParts, isReplayChunk } from "agents/chat";
const INPUT = { id: "res-1", data: { approval_flow: "..." } };
const INPUT_JSON = JSON.stringify(INPUT);
function applyStream(parts, chunks) {
for (const chunk of chunks) {
if (isReplayChunk(parts, chunk)) continue;
applyChunkToParts(parts, chunk);
}
return parts;
}
const start = { type: "tool-input-start", toolCallId: "tc1", toolName: "update_thing" };
const deltas = [
{ type: "tool-input-delta", toolCallId: "tc1", inputTextDelta: INPUT_JSON.slice(0, 20) },
{ type: "tool-input-delta", toolCallId: "tc1", inputTextDelta: INPUT_JSON.slice(20) },
];
const inputAvailable = { type: "tool-input-available", toolCallId: "tc1", toolName: "update_thing", input: INPUT };
const approvalRequest = { type: "tool-approval-request", toolCallId: "tc1", approvalId: "ap1" };
// approval request beats tool-input-available in the merged stream:
let [part] = applyStream([], [start, approvalRequest, inputAvailable]);
console.log(part.input); // undefined — expected INPUT
// tool-input-available missing entirely (deltas only):
[part] = applyStream([], [start, ...deltas, approvalRequest]);
console.log(part.input); // undefined — expected INPUT (deltas were never accumulated)
Suggested fix
We run this as a pnpm patch (defensively); all changes are in applyChunkToParts / isReplayChunk:
tool-input-delta: accumulateinputTextDeltaonto the part (e.g.rawInputstring) instead of assigning the nonexistentchunk.input.tool-approval-request: if the part has noinputyet, recover it from the accumulated raw text (parse vianormalizeToolInput) before the approval-time persistence snapshot is taken.- Late
tool-input-available: when the existing part'sinputis stillundefined, adopt the chunk's input even though the state already advanced — and exempt that case inisReplayChunkso the chunk isn't skipped as a replay.
case "tool-input-delta": {
const toolPart = findToolPartByCallId(parts, chunk.toolCallId);
- if (toolPart && toolPart.state === "input-streaming") toolPart.input = chunk.input;
+ if (toolPart && toolPart.state === "input-streaming") {
+ if (typeof chunk.inputTextDelta === "string") toolPart.rawInput = (typeof toolPart.rawInput === "string" ? toolPart.rawInput : "") + chunk.inputTextDelta;
+ if (chunk.input !== void 0) toolPart.input = chunk.input;
+ }
return true;
}
if (p.state === "input-streaming") {
p.state = "input-available";
p.input = normalizeToolInput(chunk.input).input;
...
- }
+ } else if (p.input === void 0) p.input = normalizeToolInput(chunk.input).input;
return true;
if (toolPart) {
const p = toolPart;
p.state = "approval-requested";
p.approval = { id: chunk.approvalId };
+ if (p.input === void 0 && typeof p.rawInput === "string") p.input = normalizeToolInput(p.rawInput).input;
}
if (chunk.type === "tool-input-start") return true;
+ if (chunk.type === "tool-input-available" && existing.input === void 0) return false;
return existing.state !== "input-streaming";
Happy to turn this into a PR if useful. Possibly related history: #918, #1627 (other needsApproval persistence-state issues).
Adding independent confirmation for @astanciu's report, and deferring to their offered PR here.
We independently confirmed both defects at HEAD 83e093b by reading the code path.
One extra nuance: the existing tests in packages/ai-chat/src/tests/message-builder.test.ts, including the delta cases around lines 649 and 1218, pass input on tool-input-delta chunks. That shape does not occur on the wire, so the suite green-lights the dead code. Separately, inputTextDelta is declared in StreamChunkData but is never read anywhere in non-test code, which makes the field mismatch easy to verify.
The persistence path raises the severity: _streamSSEReply snapshots the message at tool-approval-request time, and repairInterruptedToolParts preserves approval-responded parts verbatim. So the missing input survives through to convertToModelMessages and then execute.
I support @astanciu's proposed 4-hunk direction and their offer to PR it.