Repository navigation
fix: absorb same-frame tool_call deltas before the finish_reason settle (#1306) - #1311
Conversation
…le (#1306) When the last tool_call arguments fragment and finish_reason arrive in the SAME SSE chunk, the openai adapter settled BEFORE absorbing that frame's fragment: the proxy call flushed with truncated/empty arguments, pending was cleared, and the post-finish fallthrough re-buffered the fragment as a nameless orphan that the [DONE] settle silently dropped. compress then executed with {} -> kind=missing-content -> canonicalArgs('') is stable -> #156 repeated-identical-failure early break. - move pure fragment accumulation ahead of the finish_reason settle so it always sees the complete pending state (wire output for real tools is unchanged: the same-frame chunk still replays exactly once via rawBuf) - flushPendingAsStructured now warns when dropping an arguments-carrying orphan buffer instead of losing data silently - core.ts logs the failing arguments shape at the JSON.parse fallback so wire-level loss is separable from model-authored junk in logs
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-1311Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr1311.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 Review complete — mergeable. All five gates walked; evidence below. ① Original issue (direction)#1306's failure chain (same-frame ② Regression review — existing behaviors on touched paths, item by item
Behavior change disclosure (old → new + why): for the degenerate shape entire tool call (id+name+args in one chunk) arriving with ③ Code review
④ Test validity — teeth confirmed by A/BRan the new tests against master's ⑤ Diff cleanlinessSingle linear commit on current master (parent = Verification evidence
中文摘要:修复了 OpenAI 流式适配中 |
Fixes #1306
Root cause
src/loop/adapter-openai.tshandledfinish_reasonbefore absorbing the current chunk'sdelta.tool_callsfragments:settleToolCalls()flushed the proxy call with the arguments accumulated so far — missing this frame's fragment — thenpending.clear();continue) fell through to the accumulation block, which re-buffered the fragment as a nameless orphan (id="",name="");[DONE]settle dropped the orphan silently (guard requires name or id) → arguments permanently lost;""→JSON.parsefallback{}→kind=missing-content;canonicalArgs("")is stable across rounds →failedSignaturesmatch →breaking early (#156)terminated the turn.Real tools were unaffected on the wire (verbatim raw replay); only proxy tools (compress/decompress/…) depend on the accumulated arguments.
Fix
absorbToolCallDeltas) now runs at the top of the frame loop, before thefinish_reasonsettle. The settle always sees the complete pending state; the orphan-producing mechanism is gone. Wire output for real tools is unchanged — the same-frame chunk still replays exactly once (via the verbatim finish-chunk replay), verified by test 9.flushPendingAsStructurednow warns when dropping an arguments-carrying nameless buffer instead of losing data silently;core.tslogs the failing arguments shape (len + head) at theJSON.parsefallback so wire-level loss is separable from model-authored junk.Tests (
tests/openai-toolcall-finish-reason.test.ts)finish_reason→ structured compress call carries the complete arguments. Fails on master (args""), passes with the fix.runCompressLoopround with the same-frame upstream → compress actually executes (Compressed m00001–…), nomissing-content, exactly one re-request.The existing 7 tests in the file (name-splitting, mixed rounds, verbatim replay) all still pass.
Pre-flight
npm run typecheck✅node --import tsx --test tests/openai-toolcall-finish-reason.test.ts→ 11/11 ✅src/loopstashed, the 3 new tests fail exactly as the issue describes; restored → 11/11 greennpm run build✅npm testlocally stalls intests/dsh-native.test.ts(5s-timeout failures) — reproduced on pristine master in this sandbox too (environmental, not from this diff); deferring the full suite to CI as agreed.No wire/message-shape change for existing paths; comments reference the invariant docs already in the file. Kernel untouched (the
diag.keys-in-error-text suggestion from the issue is kernel-side and stays out of scope).