* fix(core): persist partial assistant turn when stream errors mid tool_use
Weak-network failures during an Anthropic-compatible stream (DeepSeek,
api.anthropic.com, etc.) can drop the SSE between a tool_use
`content_block_stop` and the terminal `message_stop`. The functionCall
chunk is already yielded at content_block_stop, so:
- Turn.run records a ToolCallRequest event.
- useGeminiStream's for-await exits and schedules the tool.
- handleCompletedTools eventually fires submitQuery(..., ToolResult)
and pushes a user[functionResponse] into history.
- But processStreamResponse's history.push for the model turn never
ran (the for-await threw first), so the matching tool_use is gone.
The next request body has `user → user[tool_result]` with no tool_use
in between, and the server rejects with HTTP 400:
"tool_use_id ... must have a corresponding tool_use block in the
previous message". Ctrl+Y can't recover because
stripOrphanedUserEntriesFromHistory only strips trailing user
entries — the lost tool_use is unrecoverable, and the session is
wedged.
Wrap the for-await loop in processStreamResponse with try/catch.
When the stream throws AND any functionCall chunk was already
yielded (hasToolCall=true), persist the partial assistant turn to
history before re-throwing. The eventual tool_result submission
then has a matching tool_use and the session can continue.
Plain-text partial turns (no functionCall yielded) are intentionally
NOT persisted: the Retry path pops the trailing user prompt and
re-issues it, so a stale partial-text model turn between them would
either bias the retry or surface as duplicate output.
* test(core): cover thinking+tool_use mid-stream throw in partial-history fix
Adds a third case to the partial-history persistence test suite for
reasoning-mode providers (DeepSeek thinking, Claude 4.6+ adaptive): when
the assistant turn streams a thinking block AND a tool_use before the
SSE drops, the partial push must keep the thinking part before the
functionCall so DeepSeek's `injectThinkingOnToolUseTurns` converter
pass sees an existing block on the replayed turn and does not pre-pend
a synthetic empty one (which would discard the model's original
reasoning text).
* fix(core,cli): close tool_use↔tool_result invariant at failure points
Extends the partial-history fix in fe35e3778 to cover the residual race
paths surfaced in PR #4176 review:
- Race A: Ctrl+Y while in-flight tool hasn't finished. History is
[user, model(tool_use)] — `stripOrphanedUserEntriesFromHistory` only
pops trailing user entries, so the retry payload lands as a fresh
user turn after the orphan tool_use and API rejects. Meanwhile the
scheduler's `onAllToolCallsComplete` is single-shot and gated on
`isResponding`, so the eventual tool_result is silently swallowed.
- Race B: process crash / OOM / SIGKILL between the partial-tool_use
push and the React scheduler's tool_result submission. On `--resume`
the dangling model(tool_use) wedges the first API call.
- Race C: external tooling / manual JSONL edits leaving the same
dangling shape.
The fix has three pieces working together:
1. `repairOrphanedToolUseTurns(history)` in geminiChat.ts walks history
left-to-right and synthesizes an `error`-typed functionResponse for
every functionCall whose id is not echoed back in the next user
turn. Appends to an existing user turn when present, otherwise
inserts a new one. Returns the injected (callId, name) list.
2. `GeminiClient.repairOrphanedToolUseTurnsInHistory()` wraps the helper
and is called from three points:
- `startChat()` after loading the transcript (Race B/C, --resume).
- `sendMessageStream` Retry branch after stripOrphans (Race A).
- `sendMessageStream` UserQuery/Cron branch (defensive belt-and-
suspenders for anything that slipped past 1 and 2).
3. `handleCompletedTools` in useGeminiStream.ts dedupes against
chat.history before submitting tool_results — if a synthetic
functionResponse for the same callId is already present (planted by
the repair pass), the in-flight scheduler's late result is dropped
and the call is `markToolsAsSubmitted` so the UI advances. Same
trade-off upstream Claude Code's `StreamingToolExecutor.discard()`
makes — late real results are dropped on the wire after synthesis,
the model sees the synthetic error and can retry the tool if it
still wants the result.
Together with the partial-history push from fe35e3778, every tool_use
that ever streamed to the consumer is guaranteed to have a matching
tool_result on the wire — regardless of whether the stream errored,
the user retried mid-flight, the process crashed, or the session is
later resumed. This is the qwen-code analogue of upstream Claude Code's
`yieldMissingToolResultBlocks` (query.ts:123-149), but split across
the core/cli boundary because the React tool scheduler runs out-of-band
from the stream loop (so the synthesis path can't atomically discard
in-flight tools the way upstream's StreamingToolExecutor can; the
history-dedup at handleCompletedTools fills that gap instead).
Tests:
- 8 new repair-helper tests in geminiChat.test.ts cover Race A,
Race B, partial coverage of parallel tool_use, idempotence on
already-paired history, no-op on tool-free history, caller-
supplied reason text, multiple non-adjacent dangling rounds, and
routes through the GeminiChat instance-method wrapper.
- client.test.ts mocks updated for the new GeminiChat method.
- All 88 geminiChat tests pass; 131 client tests pass; 91
useGeminiStream tests pass.
* fix(core,cli): close tool_use↔tool_result invariant at failure points
Post-review audit of 3de3241a2 surfaced two real races the earlier fix
did not actually close:
- Race A (the one yiliang114 originally flagged): handleCompletedTools
is gated on `isResponding` (useGeminiStream:1971) BEFORE the dedup
branch ran, so when the user Ctrl+Y'd mid-tool, the dedup never
fired and the in-flight tool stayed permanently stuck in
`completed-but-not-submitted` (the scheduler's
`allToolCallsCompleteHandler` is single-shot). The dedup branch was
structurally unreachable on the very race it was meant to defend
against.
- Race in the Retry path: the repair pass in
`client.sendMessageStream` Retry branch ran BEFORE the chat-internal
`push(userContent)`, so a Retry of a previous ToolResult submission
(lastPrompt is a functionResponse part array) raced its own real
`functionResponse` against the synthesized error one. The synthesis
was winning and pre-empting the real result, producing two
`functionResponse` entries with the same callId on the wire.
This commit relocates both pieces to where the contract actually holds:
1. Move the dedup branch in `handleCompletedTools` to BEFORE the
`isResponding` early-return so `markToolsAsSubmitted` always runs
for callIds already paired in history, unblocking the UI/scheduler
even when a new stream is in flight. The submission-side `geminiTools
= completedAndReadyToSubmitTools.filter(... && !inHistory ...)` then
covers the no-double-submit case in one expression.
2. Move the repair call from `client.sendMessageStream` Retry branch
into `chat.sendMessageStream` immediately AFTER the user-supplied
turn is pushed. The user's own tool_result (when the Retry payload
carries one) gets the first chance to close the pair before the
synthesizer sees it as dangling. `client.startChat()` keeps a
belt-and-suspenders pass at session-load time so any pre-send code
reading `chat.history` sees a well-formed shape.
Adds three integration tests covering the new wiring:
- geminiChat.test.ts: chat.sendMessageStream synthesizes a
functionResponse when history carries a dangling tool_use AND the
user-supplied content doesn't already close it (Race B/C plus
Race A from the chat side).
- geminiChat.test.ts: chat.sendMessageStream does NOT synthesize
when the user-supplied content IS a matching functionResponse
(the Retry-of-ToolResult race the previous wiring tripped on).
- useGeminiStream.test.tsx: handleCompletedTools dedups a late real
result whose callId already has a functionResponse in chat.history
(Race A end-to-end: markToolsAsSubmitted fires but no
submitQuery is dispatched).
Test summary: 90/90 geminiChat, 131/131 client, 92/92 useGeminiStream;
all 8186 core tests pass; tsc clean.
Known limitation logged for follow-up: a Retry of ToolResult whose
intervening stream itself partial-pushed a SECOND tool_use produces a
trailing user turn with both the stale re-pushed `fr_A` and the
synthesized `fr_B` for the second call — `fr_A` retry lands as an
orphan because `fc_A` was already paired earlier in history. Triggered
only when partial-push fires on the second stream AND user retries the
tool_result rather than the user prompt; extremely low frequency in
practice. Cleanest fix is in the retryLastPrompt layer (don't re-push
an already-paired tool_result), which is out of scope for this PR.
* fix(core,cli): close tool_use↔tool_result invariant at failure points
Review surfaced four issues on top of b2fed61cd. All addressed here:
- [P0] Synthetic functionResponse was appended to the trailing user
turn's parts; on a Ctrl+Y race the shape became `user[text, fr]` and
Anthropic-compatible backends still 400 because tool_result blocks
must come FIRST in a user message (Claude Code does the same hoist
in utils/messages.ts:hoistToolResults). Repair now slots synthetic
fr in before the first non-functionResponse part, after any
pre-existing real fr parts (which preserves real-tool-result order
on a parallel-partial-submit). Tests asserting the part order added.
- [P0] The dedup test fixture in useGeminiStream.test.tsx forced an
incompatible object through a direct cast to TrackedCompletedToolCall;
`tsc` rejected it with TS2352. Fixture restructured to match the
type's actual shape (matches the existing `Mocked` patterns at
L597-606 / L2396-2401) so the changed-file typecheck stays green.
- [P1] On stream error, `recordAssistantTurn` ran unconditionally
(gated only on parts being non-empty), so partial text-only turns
we intentionally do NOT push into in-memory history were still
written to the chat-recording JSONL — leaving --resume's
transcript-load path to re-inject a model turn the in-session run
discarded. Gate now matches the `history.push` decision: record
iff we will persist (success path OR stream-error-with-tool_use).
- [P2] Added client-level tests for the startChat repair wiring:
extraHistory ending in `model[functionCall]` triggers synthetic fr
injection on init; happy-path resume is a no-op. Defends against a
future reorder/removal of the call in startChat() regressing the
--resume recovery path.
All 91 geminiChat, 133 client, 92 useGeminiStream tests pass. Full
core suite (8186 tests) green. Changed-file tsc clean except for the
pre-existing `recordCompletedToolCall` typing gap that exists on main.
* fix(core): roll back partial assistant push on retryable mid-stream errors
Regression surfaced by @yiliang114 on PR #4176: a stream attempt that
yields a `functionCall` and then throws a retryable error (e.g.
`StreamContentError` with a 429 payload, or `InvalidStreamError`)
triggers the partial-assistant-turn push in `processStreamResponse`,
but the outer `sendMessageStream` retry loop catches the error and
issues a fresh attempt. The failed-attempt `model[functionCall]` was
left in history, and the successful retry's response landed as a
SECOND consecutive `model` entry — invalid user/model alternation
AND the failed-attempt `tool_use` is orphan on the wire (no matching
tool_result). The very wedge this PR is meant to escape.
Track the index of the pushed partial in a new
`pendingPartialAssistantTurnIndex` field, reset it on every
`sendMessageStream` entry, and pop it before each retry-and-continue
path in the catch block (rate-limit, reactive-compression, transient
stream anomaly, content-validation retry). Paths that `break`
(unretryable) keep the partial — the caller will see it as part of
the error surface and the existing repair / dedup machinery in
`useGeminiStream.handleCompletedTools` handles it correctly.
The pop is defensive (index-bounds + role check) so a hypothetical
intervening `setHistory` / `truncateHistory` can't cause an out-of-
bounds splice.
Adds a regression test in `geminiChat.test.ts` that reproduces
@yiliang114's exact shape: yield `functionCall`, throw
`StreamContentError(429)`, second attempt yields `Success after retry`
plain text + STOP. Asserts the final history is exactly
`[user, model(success text)]` — no leading failed-attempt model turn,
no orphan `functionCall` anywhere.
All 92 geminiChat, 133 client, 92 useGeminiStream tests pass; full
core suite green; tsc clean.
* fix(core): clear pendingPartialAssistantTurnIndex on history replacement
Audit follow-up to db344a403. The retry-rollback marker
`pendingPartialAssistantTurnIndex` captures an absolute index into
`this.history`, but `setHistory` / `clearHistory` / `truncateHistory`
wipe or shift the underlying array without touching the marker. Two
paths where this can bite:
- Reactive compression on contextOverflow calls `this.setHistory(
newHistory)` inside `tryCompress`. If a partial push has happened
earlier (extreme edge case — would require a partial push followed
by a context-overflow on retry), the marker's stale index could
coincide with a model entry in the compressed history, and
`popPartialIfPushed` would splice the wrong turn.
- `/clear` / `--resume` / programmatic `setHistory` / external
`chat.truncateHistory()` (Session.ts:244) wipe the basis the marker
was captured against, leaving the next send's
`popPartialIfPushed` with a meaningless index.
The fix is mechanical: each history-replacement method clears the
marker. The marker is per-send and ephemeral, so losing it across a
history replacement is safe (the next send either has no partial yet,
or will push a fresh one with a fresh index).
`popPartialIfPushed` already bounds-checks + role-checks before
splicing, so this is defense-in-depth — the bug above requires the
defensive check to ALSO line up. But cheap to harden and the comment
documents the invariant for future readers. 235 core/geminiChat +
core/client tests still pass.
* fix(core,cli): close mimo-v2.5-pro review gaps on partial-tool_use repair
Four review items from the mimo-v2.5-pro pass on #4176:
S1 — `addHistory` and `stripThoughtsFromHistory` did not reset
`pendingPartialAssistantTurnIndex`. `setHistory`/`truncateHistory`/
`clearHistory` already do; consistency with the defensive pattern
matters because `stripThoughtsFromHistory` filter+map produces a new
array that makes the marker stale, and any future addHistory variant
that splices into the middle could leave `popPartialIfPushed` looking
at a wrong index. Comments updated to record the actual semantics
(plain push() is safe; a future splicing variant would not be).
S2 — When the history-based dedup in `useGeminiStream.handleCompletedTools`
filters a tool out of `geminiTools` because its callId is already paired
in chat.history, `recordCompletedToolCall` was never called for it.
That silently skips the `toolCallCount` increment and the
`skillsModifiedInSession` flip — the latter gates the skills-reload
prompt at end-of-turn, so a deduped `write_file` under a project
SKILLS path would never trigger a reload. Now records inline with the
dedup mark, filtered to non-client-initiated to match the original
geminiTools loop's shape.
S3 — Existing Race A dedup test never set `isResponding=true`, so a
future refactor that moves the dedup block below the `if (isResponding)
return` guard would pass every test while silently leaving deduped
tools stuck in `completed-but-not-submitted` (the scheduler's
`onAllToolCallsComplete` is single-shot per batch). New test holds a
stream open via `submitQuery` to pin `isResponding=true`, then triggers
`onComplete` and asserts `markToolsAsSubmitted` still fires. Verified
the test fails when the dedup is moved below the guard. Existing
Race A test also gained a `recordCompletedToolCall` assertion locking
in the S2 fix.
N1 — Hoisting comment in `repairOrphanedToolUseTurns` documented the
why (mirror upstream `hoistToolResults`) but not the consequence of
removal. Added "CONSEQUENCE OF REMOVAL" paragraph spelling out the
exact 400 a naive `next.parts = [...existing, ...syntheticParts]`
re-introduces, so the constraint is unmissable for future maintainers.
Verified: 94/94 useGeminiStream, 102/102 geminiChat, 133/133 client
tests pass. tsc clean both packages, eslint + prettier clean on all
three changed files. S3 regression-injection check confirmed the new
test catches the dedup-below-guard regression.
* fix(core): defer chat-recording flush until partial-turn rollback decision
Yiliang114's PR #4176 follow-up: `popPartialIfPushed()` rolls a
failed-attempt's `model[functionCall]` out of in-memory `this.history`
before retrying, but the same partial turn was already appended to the
chat-recording JSONL by `recordAssistantTurn()`. After a successful
retry, live history was clean while the durable transcript still
carried two assistant entries — the failed attempt and the success.
`--resume` then rehydrated the failed `model[functionCall]` and the
resumed model context picked up a tool_use the live session correctly
discarded.
Fix: the recording call on the stream-error + hasToolCall path is now
deferred. processStreamResponse stashes the would-be record on a new
`pendingPartialAssistantRecord` field instead of immediately appending.
The retry loop's `popPartialIfPushed` clears the stash alongside the
in-memory splice — no flush, no leak. Once the retry loop has settled
(success-break: stash already null; unretryable-break: partial
survived), a flush hook right after the for-loop appends the stashed
record to JSONL so the transcript matches whatever lives in
`this.history`. The success path (streamError === null) still records
immediately as before — only the at-risk-of-rollback path is deferred.
Lifecycle is paired one-to-one with `pendingPartialAssistantTurnIndex`:
set together, popped together, flushed together, and reset in every
history-replacement method (sendMessageStream entry, setHistory,
clearHistory, truncateHistory, addHistory, stripThoughtsFromHistory)
so an exotic path can't leak a stale stash into a future send's flush.
Two regression tests in geminiChat.test.ts:
1. "rolls back the chat-recording entry too when the retry succeeds"
— failing-then-succeeding stream sequence asserts
`recordAssistantTurn` is called exactly once with the success
text, no functionCall part anywhere in the recorded message.
Verified the test fails (mock called twice) when the deferral
branch is bypassed.
2. "flushes the chat-recording entry on the unretryable break path"
— synthetic non-retryable error after a tool_use chunk asserts
the partial IS recorded so the JSONL stays aligned with the
in-memory partial that survives. Verified the test fails
(mock called zero times) when the post-loop flush is removed.
Tests: 104/104 geminiChat (+2 new), 133/133 client, 94/94
useGeminiStream. tsc + eslint + prettier clean.
* fix(core,cli): close 4 deepseek-v4-pro review threads on PR #4176
Four follow-ups from the deepseek-v4-pro pass on commit 2dbfc4e3b:
[A] useGeminiStream.ts — dedup recordCompletedToolCall now skips
cancelled tools. `dedupedTools` includes anything in a terminal state
(success | error | cancelled), but cancelled means the tool never
actually produced model-visible output. Counting it via
`recordCompletedToolCall` would inflate `toolCallCount` and could flip
`skillsModifiedInSession` for a never-executed skill-write. The
markToolsAsSubmitted call still fires so the scheduler unblocks.
Mirrors the rationale of the existing `allToolsCancelled` branch which
surfaces non-deduped cancellations via addHistory + reportCancelled
rather than the completed-call metric.
[B] client.ts — JSDoc on `repairOrphanedToolUseTurnsInHistory` claimed
three call points (startChat, Retry submit path, defensive
UserQuery/Cron pass) but in practice this method is only called once
from `startChat()`. The other two coverage points live one layer down
inside `GeminiChat.sendMessageStream` and call the standalone
`repairOrphanedToolUseTurns(history)` function directly without
routing through this wrapper. Updated to reflect the actual coupling.
[C] geminiChat.ts — `addHistory` now warns via debugLogger if it
clears a non-null partial-push marker. Today's callers
(useGeminiStream cancelled-tool synthesis, ACP session injects,
shellCommandProcessor) only run between sends, so the marker is null
on entry. If a future code path calls addHistory between the partial
push and the retry attempt, the silent clear would strand the
partial: popPartialIfPushed would no-op, the failed model[functionCall]
would survive into the retry, and a successful retry's response would
land as a SECOND consecutive model turn — the wedge this whole
subsystem exists to prevent. The warn surfaces the offending caller in
the log instead of forcing a blind trace through the marker lifecycle.
[D] geminiChat.ts — extracted `clearPendingPartialState()` helper
covering all 7 sites that need to reset both
`pendingPartialAssistantTurnIndex` and `pendingPartialAssistantRecord`
in lockstep (sendMessageStream entry, popPartialIfPushed, the
post-loop flush hook, clearHistory, addHistory, setHistory,
truncateHistory, stripThoughtsFromHistory). The two fields ARE always
paired by lifecycle (set together on stream-error stash, popped
together on retry, flushed together at the rethrow site), so any
single-field reset would be a bug. The helper makes the lockstep
invariant explicit and means a future history-mutating method can't
forget to clear one field.
Regression test: useGeminiStream.test.tsx adds
"skips recordCompletedToolCall for deduped CANCELLED tools" — sets up
a deduped cancelled tool with callId paired in history, asserts
markToolsAsSubmitted IS called (scheduler unblocks) but
recordCompletedToolCall is NOT called. Verified the test fails when
the cancelled-filter line is removed (regression-injection check).
Tests: 237/237 core, 95/95 useGeminiStream (+1 new). tsc + eslint +
prettier clean. Existing CI failure on Test (ubuntu-latest, Node 22.x)
is an unrelated timing flake in promptHookRunner.test.ts
("expected 49 to be greater than or equal to 50") — macOS and Windows
both pass.
* fix(core): close 3 review threads on partial-tool_use repair (PR #4176)
Three follow-ups from the qwen-latest-series-invite-beta-v28 pass on
commit fd12639c9:
[E] CORRECTNESS — `repairOrphanedToolUseTurns` now scans EVERY
consecutive `user` turn after a `model[functionCall]` (not just
`history[i+1]`) when building the matched-id set. The shape
`model[fc], user[text], user[fr_real]` arises when a user aborts a
long-running tool, types a follow-up text turn, and the React
scheduler's late `submitQuery` appends the real `tool_result` as a
SEPARATE user entry. Previously the repair saw only the immediate
follow-up text turn, found no matching fr, and synthesized a duplicate
`error` `functionResponse` for a callId that already had a real result
two turns down. The downstream dedup in
`useGeminiStream.handleCompletedTools` then dropped the REAL result on
the next submission (its callId was now "already in history" thanks to
the synthetic), and the model only ever saw the error placeholder —
silently swapping a real tool success for an error.
Two regression tests in `geminiChat.test.ts`:
- "does NOT synthesize when the real functionResponse lives in a
non-adjacent later user turn" — proves the forward-scan finds the
real fr at history[i+2]+ and skips synthesis.
- "still synthesizes for a partial mismatch when forward scan misses
one callId" — counterpart that confirms parallel tool_use shapes
where only some callIds have real fr's still get synthetic ones
hoisted onto the immediate next user turn.
Verified via regression-injection (revert to scanning only
`history[i+1]`): the non-adjacent test fails with `injected =
[{callId:'call_nonadjacent_real',...}]` — proves the test catches the
bug, not just verifies a no-op.
[F] OBSERVABILITY — added a `[PARTIAL_PUSH]` `debugLogger.warn` at the
partial-turn push site in `processStreamResponse`. The repair lifecycle
already had push/pop/repair logging at the dedup and synthesis ends
(`[REPAIR] Dropping ...`, `[REPAIR] Synthesized ...`), but the original
PUSH event — root cause of every downstream recovery — was unlogged.
At 3 AM investigating a stale-partial wedge, the trace now anchors at
the exact moment the partial was created with pendingIndex, callIds,
and the originating error message.
[G] INVARIANT — `stripOrphanedUserEntriesFromHistory` now calls
`clearPendingPartialState()` after popping. Today this is safe even
without the reset (only trailing `user` entries are popped, which
can't shift the index of an earlier `model` partial), but every other
history-mutation method in the class — `clearHistory`, `addHistory`,
`setHistory`, `truncateHistory`, `stripThoughtsFromHistory` — now
clears the partial-push state in lockstep. Omitting it here would be
a silent exception to the uniform invariant the helper extraction
established. A future caller invoking this between the deferred JSONL
flush and the next `sendMessageStream` would otherwise leave a stale
marker that happens to line up with whatever model entry is at that
index in the meanwhile.
Tests: 239/239 core (+2 new), 95/95 useGeminiStream. tsc + eslint +
prettier clean. Latest CI run: all three platforms (Linux/macOS/
Windows) green.
* fix(core): close 6 review threads on partial-tool_use repair (PR #4176)
Six follow-ups from the qwen-latest-series-invite-beta-v28 + gpt-5.5
passes on commit 2880de577:
[A] CORRECTNESS — `repairOrphanedToolUseTurns` now HOISTS a real
`functionResponse` from a non-adjacent later user turn into the
IMMEDIATE next user turn (placed before non-fr parts), in addition to
the forward-scan dedup added in 2880de577. The shape `model[fc],
user[text], user[fr_real]` arises when a user aborts a long-running
tool, types a follow-up text turn, and the React scheduler's late
`submitQuery` appends the real `tool_result` as a SEPARATE user entry.
Forward-scan alone correctly skipped the synthesis duplicate, but the
wire layout still serialized `model[tool_use] → user[text] →
user[tool_result]`, which Anthropic-compatible backends reject with
"tool_use_id ... must have a corresponding tool_use block in the
previous message". The hoist physically moves the real fr part out of
its original turn into history[i+1], drops the source turn if it
becomes empty, and reuses the existing front-of-user-turn
insertion logic so caller-supplied fr ordering stays preserved.
Hoisted ids are NOT added to `injected` — the real fr is already in
history (just relocated), so the React scheduler's history-based
dedup handles them naturally without an extra entry that would
double-trigger the dedup-drop log.
Three new tests in `geminiChat.test.ts`:
- "hoists the real functionResponse from a non-adjacent later user
turn into the adjacent one" — pure relocate case (source turn had
only the fr → removed).
- "synthesizes missing fr AND hoists real fr in a parallel tool_use
mismatch" — parallel `model[fc_a, fc_b]` with real fr_a in a
non-adjacent turn: cid_b synthesized, cid_a hoisted, source turn
removed.
- "hoists real fr but preserves the source user turn when it
carries other content" — pins the empty-turn cleanup so it only
drops turns whose parts list goes to zero (mixed text + fr source
keeps its text after the fr is extracted).
[B] CRITICAL — moved the deferred chat-recording flush from BEFORE the
max-tokens escalation block into the outer `finally`. The escalated
`makeApiCallAndProcessStream → processStreamResponse` can set a NEW
`pendingPartialAssistantRecord` if it errors mid-tool_use, and that
throw escapes through the for-await without touching the (now-passed)
retry-loop catch. Before this fix the new record was never appended
to JSONL: live history retained the partial `model[functionCall]`
that the escalated processStreamResponse pushed, but the durable
transcript silently dropped it; on `--resume` the transcript-load
path didn't see the partial, `repairOrphanedToolUseTurnsInHistory`
found nothing to repair, and the React scheduler's late real result
became a permanent orphan — reproducing the exact wedge this PR
prevents. Putting the flush in `finally` covers all throw paths
(escalation, post-retry-loop `throw lastError`, consumer `.return()`)
and the normal completion path with one statement, while keeping the
"marker and stash cleared in lockstep" invariant.
One new regression test: "flushes the JSONL record when escalated
stream throws mid-tool_use" — pins the flush behavior under the
specific shape (initial MAX_TOKENS success → escalation cuts mid-
tool_use → throw). Asserts both that the partial survives in
`this.history` and that `recordAssistantTurn` got called with the
partial functionCall id.
[C] DOC — updated `repairOrphanedToolUseTurns` doc comment to spell
out both fix-ups (synthesize + hoist), the wire-format consequence of
removal ("tool_use_id ... must have a corresponding tool_use block"),
and the rule that hoisted ids are not in the returned `injected` list.
The previous "immediately following user turn" wording contradicted
the forward-scan implementation; the new wording matches the actual
behavior across both fix-up paths.
[D] OBSERVABILITY — inline `repairOrphanedToolUseTurns` call in
`sendMessageStream` now logs `[REPAIR] sendMessageStream inline pass
synthesized N functionResponse(s) ...` when synthesis fires. The
startChat() path already logs via `repairOrphanedToolUseTurnsInHistory`
(`[REPAIR] Synthesized ...`), and `useGeminiStream.handleCompletedTools`
logs `[REPAIR] Dropping ...` on dedup. Without a tagged log at the
inline call site, an investigator looking at a dedup-drop had no way
to tell whether the synthetic was planted at session-load or at the
per-send pass.
[E] OBSERVABILITY — `popPartialIfPushed` now logs `[PARTIAL_POP]
Splice skipped` when the marker is set but `history.length <= idx` or
`history[idx]?.role !== 'model'`. This can't happen today (every
history-mutation method clears the marker in lockstep), but the warn
makes any future regression observable rather than silent: a
mutation path that forgets to clear would otherwise leave a stale
partial in history with no diagnostic trace.
[F] CLARITY — added a comment at the post-`tryCompress`
`popPartialIfPushed()` call explaining the intentional defense-in-
depth no-op. `tryCompress()` succeeds → `setHistory()` →
`clearPendingPartialState()`, so the marker is null by the time the
pop runs. The call is kept (not removed) so a future refactor that
switches `tryCompress` to in-place mutation would still drop the
stale partial before `requestContents` is rebuilt.
Tests: 336/336 (108 in geminiChat.test.ts + 4 new = +4; 95
useGeminiStream + 133 client unchanged). tsc + eslint + prettier
clean on changed files.
* fix(core,cli): close 10 review threads on partial-tool_use repair (PR #4176)
Five Critical + four Suggestion threads from the gpt-5.5 +
qwen-latest-series-invite-beta-v34 passes on commit ce68749b5, plus
one N/A documented in code/test comments where the requested test
target is provably unreachable.
[A] CORRECTNESS — `repairOrphanedToolUseTurns` now collects EVERY
location for each `functionResponse` id, not just the first.
Previously the `matched` Map stored only the first occurrence per
callId, so when the same id was echoed back more than once across
the consecutive user turns (shape `model[fc id=cid], user[text],
user[fr cid], user[fr cid]` — possible when the React scheduler
retries the late submitQuery after the orphan repair already planted
one, or two parallel late-submit paths land), hoisting only the
first left a duplicate behind. The wire payload then serialized
`model[tool_use] -> user[tool_result] -> user[tool_result]`, the
backend rejected the trailing block as an orphan, and the session
stayed wedged for the same 400 class this whole repair pass exists
to escape. Fix: track all locations per id; pick the first as the
canonical survivor (hoisted into history[i+1] when non-adjacent, or
left in place when adjacent), and drop EVERY duplicate. New tests:
"drops duplicate functionResponse entries for the same callId across
user turns" (canonical non-adjacent + duplicate further down →
3-entry result), "drops duplicate fr even when the canonical copy is
already in the adjacent turn" (no hoist needed but duplicate cleanup
must still fire). gpt-5.5 review thread on PR #4176.
[B] CORRECTNESS — Max-tokens recovery catch (`recoveryError` block)
now pops the partial `model` turn FIRST, then the
OUTPUT_RECOVERY_MESSAGE user turn. Before the fix, when a recovery
stream errored AFTER yielding a `functionCall` chunk,
`processStreamResponse` pushed a partial `model` turn into history
before re-throwing — so by the time the catch ran, the trailing
entries were `[..., user(OUTPUT_RECOVERY_MESSAGE), model(partial fc)]`
and the naive `if last is user, pop` check no-op'd, stranding the
control prompt as a real user turn. Two consequences: the recovery
prompt's instructions polluted later turns, and the inline repair
on the next sendMessageStream synthesized an `error` `functionResponse`
for the dangling `functionCall` — which the dedup then dropped when
the React scheduler's REAL tool result arrived, so the model saw an
"execution result was not recorded" error for a tool that actually
succeeded. Fix: pop the partial model turn, clear the partial-push
markers, then pop the recovery user turn. New regression test:
"should pop both the partial model turn AND the recovery user
message when recovery throws after a functionCall".
qwen-latest-series-invite-beta-v34 review thread.
[C] PERFORMANCE — added `getHistoryFunctionResponseIds(): Set<string>`
to `GeminiChat` and a wrapper on `GeminiClient`. The dedup pass in
`useGeminiStream.handleCompletedTools` previously called
`geminiClient.getHistory()`, which returns `structuredClone(this.history)`
— a recursive deep clone of every part including large tool outputs.
On long sessions (200+ entries) running on every tool-completion
batch, this caused multi-millisecond stalls on the React UI thread
during streaming. The new accessor walks `this.history` in place
and only collects the id strings the dedup actually needs.
`useGeminiStream` prefers it; falls back to the cloning getHistory()
for older mocks that don't expose it (tests stay green).
qwen-latest-series-invite-beta-v34 review thread.
[D] ROBUSTNESS — wrapped the `finally`-block JSONL flush in
try/catch. Recording-service errors (disk full, write permission,
serialization failure) MUST NOT propagate out of the generator's
`finally` — that would mask the real send outcome (success or
original throw) with a JSONL-write error the caller can't usefully
act on. On failure the partial is dropped from JSONL but stays
durable in `this.history`, so live behavior is unaffected; eventual
consistency is restored on the next successful flush.
qwen-latest-series-invite-beta-v34 review thread.
[E] TEST COVERAGE — added an InvalidStreamError transient-retry
rollback test exercising `popPartialIfPushed()` on the
NO_FINISH_REASON / NO_RESPONSE_TEXT path (separate budget from the
already-tested rate-limit branch). Stream yields a functionCall
then throws InvalidStreamError; retry succeeds; assert no orphan
functionCall remains in history. qwen-latest-series-invite-beta-v34
review thread.
[F] N/A WITH RATIONALE — verified that the InvalidStreamError
content-retry branch (geminiChat.ts ~line 1399) is unreachable for
that error class: `isTransientStreamError` and `isContentError` are
the same predicate (`error instanceof InvalidStreamError`), so the
transient branch above always either continues or breaks before
control reaches the content branch. The `popPartialIfPushed()` call
is preserved as defense-in-depth for a future error class that
should diverge the predicates. Documented at the call site (code
comment) and at the would-be test site (test-block comment) so
future readers see the unreachability analysis instead of repeating
the review-comment ask.
[G] DRIFT GUARD — `recordAssistantTurn` and `this.history.push`
gates now share the single `willPersistToHistory` binding instead of
re-deriving the same `hasToolCall && (thoughtContentPart || ...)`
expression. Drift risk: tightening one without the other would
silently desync the JSONL transcript from in-memory history.
qwen-latest-series-invite-beta-v34 review thread.
[H] DOUBLE-DISPATCH — `clientTools` filter in
`useGeminiStream.handleCompletedTools` now excludes callIds whose
fr is already in history. Previously a deduped client-initiated
tool would have `markToolsAsSubmitted` called twice (once in the
dedup block, once in the clientTools block), triggering an extra
React render cycle. qwen-latest-series-invite-beta-v34 review thread.
[I] TEST COVERAGE — added six tests under "partial-push marker
invariants on history mutation" pinning that each of the six
mutation methods (clearHistory, addHistory, setHistory,
truncateHistory, stripThoughtsFromHistory,
stripOrphanedUserEntriesFromHistory) calls clearPendingPartialState()
in lockstep. Markers planted directly via private-field assignment
(the lifecycle would otherwise clear them in finally before the
test could observe them). Future refactors that drop a
clearPendingPartialState() call from one site are now caught at
test time rather than as a silent stale-partial wedge in production.
qwen-latest-series-invite-beta-v34 review thread.
[J] TEST COVERAGE — added a mixed-batch dedup test in
useGeminiStream.test.tsx: scheduler completes two tools in the same
batch, one whose callId already has an fr in history (deduped), one
fresh. Asserts (a) markToolsAsSubmitted called with both, (b)
recordCompletedToolCall fires exactly once per tool (deduped via
the dedup-loop, fresh via the geminiTools loop — no double-record
on the deduped id), (c) sendMessageStream IS called for the fresh
tool's real result. The `!historyCallIdsWithResponse.has(callId)`
filter on `geminiTools` is the only guard against double-counting
telemetry; existing tests supplied only deduped tools.
qwen-latest-series-invite-beta-v34 review thread.
Tests: 347/347 (118 in geminiChat.test.ts, 96 in
useGeminiStream.test.tsx, 133 in client.test.ts; net +8 new tests).
tsc + eslint + prettier clean on changed files.
* fix(core,cli): close 3 review threads on partial-tool_use repair (PR #4176)
Three Suggestion threads from the qwen-latest-series-invite-beta-v34
pass on commit c8fa3143f.
[A] DIAGNOSTICS — `repairOrphanedToolUseTurns` return type now
carries a `droppedDuplicates: Array<{ callId; name }>` field
alongside `injected`. Previously a duplicate-only repair (no
synthesis, no hoist — the main scenario the prior commit added)
returned `injected.length === 0` and both call sites
(`repairOrphanedToolUseTurnsInHistory` in client.ts,
`sendMessageStream` inline in geminiChat.ts) only logged on
synthesis, leaving zero diagnostic trail. If a future callId
collision bug caused the wrong `functionResponse` to be dropped,
there was no breadcrumb pointing back to the repair function. Both
call sites now emit a `[REPAIR] Dropped N duplicate functionResponse(s)`
warn line tagged with their origin (session-load vs inline per-send)
so investigators can anchor to the exact pass.
[B] DEFENSIVE INVARIANT — max-tokens recovery catch now uses an
index-checked pop instead of a positional `history.pop()`. The
semantically equivalent `popPartialIfPushed` uses
`splice(idx, 1)` with an explicit bounds/role check and a
`debugLogger.warn` on mismatch; the recovery catch was the only
rollback site that pop'd whatever model entry happened to be last,
with zero diagnostic output. Today the invariant holds (nothing
mutates `this.history` between `processStreamResponse`'s push and
the for-await catch), but a future change that inserts a mutation
in that window — compression side-effect, abort-signal handler,
telemetry hook — would silently pop the wrong entry while
`clearPendingPartialState()` cleared markers for the actual
partial, leaving it permanently stranded. The new
`[RECOVERY_POP] Marker/last-index mismatch` warn surfaces any
future violation immediately.
[C] TEST COVERAGE — added 6 unit tests for
`GeminiChat.getHistoryFunctionResponseIds()` (empty, mixed
user/model, model[fc] ignored, duplicate collapse to Set,
malformed parts, no aliasing of internal state). Also wired the
mixed-batch dedup test in `useGeminiStream.test.tsx` through the
fast-path accessor (mock returns the dedup Set directly) and
asserted that `getHistoryFunctionResponseIds` IS called and the
legacy cloning `getHistory()` is NOT. The earlier version of that
test only mocked `getHistory()`, so the optimization the previous
commit added was never actually exercised — a regression that
dropped the fast-path branch from the dispatcher would re-route
every batch onto the slow `structuredClone` path with no test
failure. The new assertion makes that regression visible.
Tests: 353/353 (124 in geminiChat.test.ts +6, 96 in
useGeminiStream.test.tsx unchanged but mock surface expanded,
133 in client.test.ts unchanged). tsc + eslint + prettier clean
on changed files.
* test(cli): route existing dedup tests through fast-path accessor (PR #4176)
One Suggestion thread from the deepseek-v4-pro pass on commit
c30bba6e7.
`MockedGeminiClientClass` did not expose `getHistoryFunctionResponseIds`,
so 3 of the 4 dedup tests in `useGeminiStream.test.tsx` fell through
to the `else if (typeof geminiClient.getHistory === 'function')`
branch — the `structuredClone(this.history)` slow path. Only the
mixed-batch test added in c30bba6e7 wired the fast path explicitly.
Net effect: a regression in `getHistoryFunctionResponseIds` (wrong
ids, missing ids, exception) would silently re-route every dedup
batch onto the slow clone path with all 4 tests still green, while
production paid the multi-millisecond UI-thread stall this PR
specifically added the accessor to avoid.
Fix in two parts:
1. Add `getHistoryFunctionResponseIds = vi.fn().mockReturnValue(new
Set<string>())` to the `MockedGeminiClientClass` default. Every
test now exposes the fast-path accessor, so the dispatcher takes
the `getHistoryFunctionResponseIds` branch by default — matching
production. Tests that need a non-empty dedup set override the
mock explicitly.
2. Override the mock in each of the three previously-affected dedup
tests with a `Set` containing the callId(s) their `getHistory()`
fixture had paired. The dedup assertions stay identical — but
the path the production code takes to reach them now matches
the path under test.
Tests: 96/96 useGeminiStream (no new tests; existing dedup tests
now exercise the fast path the previous commit added). 353/353
across core + cli. tsc + eslint + prettier clean.
* refactor(core,cli): address yiliang114 review observations (PR #4176)
Five items from the yiliang114 review on commit 06a695156. Two P1
(observability + structural maintainability) and three editorial.
[A] P1 — repairOrphanedToolUseTurns refactored into three pure
phases: scanModelTurn / planRepair / applyRepair. Each runs in
isolation against narrow `ScanResult` / `RepairPlan` types, and only
the last mutates `history`. The outer forward-walk loop is now a
~25-line orchestrator: scan → bail on empty expected → plan → bail
on empty plan → apply → record `injected` + `droppedDuplicates` →
advance cursor past any freshly-inserted user turn. Index drift can
only happen in applyRepair, so an audit narrows to one function.
Behavior identical to the pre-refactor monolith — all 13 existing
repair tests pass unchanged.
[B] P1 — finally-block JSONL flush error log promoted from
`debugLogger.warn` to `debugLogger.error`. A persistent write
failure (disk full, permission denied, serialization broken)
silently loses every deferred partial after the first occurrence;
that's the class of failure that warrants monitoring attention, not
the warn category that fades into log noise. Single-occurrence
transient failures still surface as one error per occurrence.
[C] P2 — addHistory partial-marker log promoted to `debugLogger.error`
with `[INVARIANT_VIOLATION]` tag. The existing call graph cannot
legitimately hit this branch (it only fires when addHistory runs
mid-sendMessageStream, which no current caller does); any future
hit is a true bug in a new caller, not noise.
[D] P2 — removed the `getHistory()` fallback branch in
`useGeminiStream.handleCompletedTools`. Verified via grep: every
GeminiClient consumer (real production code + MockedGeminiClientClass
+ every individual dedup test) now exposes
`getHistoryFunctionResponseIds`, so the three-branch duck-type
dispatch was dead-code at production level and only existed to
support an interim cycle when the fast-path accessor was being
introduced. The dispatcher is now a one-liner; production path is
strictly the fast accessor, with an empty Set returned if `geminiClient`
itself is missing (only happens in hook-level unit tests with no
client at all).
[E] P2 — cleanup pass dropping reviewer/thread citations from code
comments (e.g. "qwen-latest-series-invite-beta-v34 thread on PR
#4176", "gpt-5.5 review thread", "deepseek-v4-pro thread",
"yiliang114 repro"). The "why" explanations stay — those are the
load-bearing context. Reviewer/thread attribution lives in the
PR history; embedding it in long-term comments was noise that
ages poorly. Touched 9 sites in geminiChat.ts, 2 in client.ts,
1 in useGeminiStream.ts, 4 in useGeminiStream.test.tsx, and 8 in
geminiChat.test.ts (test names + comment blocks).
Also: P2.4 (yiliang114 flagged a potential test-coverage regression
on TPM throttling + Retry-After delay) verified as a false alarm
— `should retry on TPM throttling StreamContentError with initial
delay` (geminiChat.test.ts:2959) and `should use Retry-After delay
for streamed rate-limit errors` (line 3035) both still exist.
Tests: 353/353 (no new tests; all existing pass through the
refactored repair function and the simplified dispatcher). tsc +
eslint + prettier clean.
* refactor(core): consolidate partial-tool_use repair docs into one design block (PR #4176)
Addresses the pomelo-nwu review observation on c30bba6e7:
~60% of the +918 lines added to geminiChat.ts were comments, with
several 20–40 line prose blocks repeating the same race-class /
tool_use_id wedge analysis at every call site. Net –139 / +117
(net delete ~22 lines but mostly redistributing weight from per-site
blocks to a single canonical note).
Changes:
* Add one canonical design block above ORPHAN_TOOL_USE_REPAIR_REASON
covering: the tool_use_id 400 wedge, the three race classes (A
Ctrl+Y mid-flight, B process crash, C SSE drop), the two-layer fix
(partial-push + repair), and the partial-push marker lifecycle.
Every per-site comment that used to repeat this now points back.
* Trim popPartialIfPushed COMPRESSED-branch comment from 14 lines to
4 (no-op-today + reason for keeping).
* Trim recovery-catch comment block from ~38 lines to ~7 (pop-order
matters + index-checked, pointer to canonical note).
* Trim addHistory invariant comment from ~22 lines to ~5 (when this
fires + why error-level, pointer to canonical note).
* Trim processStreamResponse partial-push comment from ~22 lines to
~8 (Race C name + signal + plain-text-not-persisted rule).
* Replace tool_use_id wedge re-explanations at 5 sites (repair doc,
scanModelTurn doc, applyRepair doc, popPartialIfPushed comment,
processStreamResponse comment) with single-line pointers. Canonical
block at line 411 is now the only authoritative copy.
Reviewer marked this as non-blocking ("shouldn't gate the merge");
done as cleanup polish while LGTM is in.
Tests: 375/375 (no test changes). tsc + eslint + prettier clean.
* refactor(core): further trim partial-tool_use repair comments (PR #4176)
Second cleanup pass on pomelo-nwu's feedback. Previous commit
(298588190) cut net –22 lines but several blocks were still
20–25 lines of prose; this pass takes the rest down to the
one-or-two-line pointer pattern the reviewer suggested.
geminiChat.ts: 2384 → 2207 lines (-177). Comment density 37% → 32%.
Trimmed:
* pendingPartialAssistantTurnIndex + pendingPartialAssistantRecord
field doc blocks (16 + 22 lines → one combined 5-line block
pointing at the canonical note).
* clearPendingPartialState method doc (10 → 4 lines).
* sendMessageStream inline-repair comment block (29 → 7 lines).
* `finally` JSONL flush block (32 → 8 lines).
* repairOrphanedToolUseTurns function doc (39 → 14 lines).
* scanModelTurn / planRepair / applyRepair phase docs (11–22 → 4–9
lines each).
* RepairPlan interface doc (14 → 1 line).
* GeminiChat.repairOrphanedToolUseTurns wrapper doc (9 → 3 lines).
* GeminiChat.getHistoryFunctionResponseIds doc (13 → 6 lines).
What stayed (intentional load-bearing context):
* The canonical block above ORPHAN_TOOL_USE_REPAIR_REASON — that's
the consolidation point every per-site one-liner points back to.
* Pre-existing upstream JSDoc (ctor, sendMessageStream, getHistory,
redactStructuredOutputArgsForRecording).
* The Reactive compression no-op note (already at the 4-line cap).
* The recovery-catch 7-line block (already at the cap).
* The addHistory invariant 5-line block (already at the cap).
Tests: 375/375 pass on the affected suites. tsc + eslint + prettier
clean on changed files. Behavior unchanged.