Repository navigation
feat: detect + retry fake completions (tool call written as text, #371) - #378
Conversation
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-378Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr378.tgz
npm install -g packageThis comment is automatically updated on each push. |
[bot] Picked up #378. Reviewing the |
[bot] Review complete — LGTM, no blocking issues. Reviewed the diff on Pre-flight (re-run on the branch, all match your claims):
What I verified in the code:
Minor findings (report-only, non-blocking):
E2E regression: per AGENTS.md, changes to the request pipeline ( |
A small model (e.g. MiniMax-M3 via a gateway) can write a tool call as plain text - echoing the tool-call XML template from the context - instead of emitting a real tool block. With no tool block the agent ends the turn early and shows success, but the tool never ran (the "fake completion" from #371, root-cause follow-up to #361). Add a proxy-side detect + retry fallback: - Detect: a response carrying a complete tool-call structure (an opening invoke-style tag, or 2+ distinct closing tags) AND no real tool block for the wire protocol (Anthropic tool_use / OpenAI tool_calls / Responses function_call). Reuses PR #365's containsToolCallXmlFragment detector; the structural guard avoids false-positives on legit prose that merely mentions a single closing tag. - Retry: re-request upstream with a corrective hint appended as a trailing user message. Bounded per turn (max retries) and per session (session.metadata.fakeCompletionStreak, resets on a clean turn). Opt-in (disabled by default): the fallback must buffer the whole response before the client sees it (a fake completion is only knowable at end-of-stream), which defeats incremental streaming. That cost is only worth paying for the low-frequency case, so it is opt-in via BILI_FAKE_COMPLETION_RETRIES (default 0 = pre-#371 passthrough). Tests: tests/fake-completion.test.ts (21) - detection per protocol, the structure guard, hint injection per protocol, and full-proxy integration (fake -> retry -> real tool_use; retry cap via streak; legit prose no-retry).
5cccaa7 to
0dde055
Compare
…letion resolution Merging #378 with post-#466 master left the #460 strip branch reading upstream.body, which resolveFakeCompletion has already consumed when BILI_FAKE_COMPLETION_RETRIES > 0 — the client would hang on an empty response. Read responseBody instead (identical to upstream.body when retries are disabled).
|
Pushed to this branch (merge master + one fix), same drill as #464:
Verified on the merged tree:
One design gap noted for follow-up (not blocking this PR): the fake-completion gate sits after the plugin-mode early return in |
[bot] Verifying the new push (master merge + 892ffa2): checking out the branch, reviewing the strip-branch fix, and re-running the pre-flight (typecheck / targeted tests / full suite) plus confirming the 3 suite failures are pre-existing on master HEAD. Will report here. |
…tream.body With BILI_FAKE_COMPLETION_RETRIES>0 and a streaming request, the #378 buffering block consumes upstream.body before the ladder. The new G3 application/json branch piped upstream.body — a drained stream — so the client received an empty body. Read responseBody instead, matching the SSE strip branch and the bare pipeThrough fallback. Includes a red-then-green regression test driving the real proxy harness (fake-completion enabled + stream:true + JSON answer).
What
Fixes the "fake completion" from #371 (root-cause follow-up to #361): a small model (e.g. MiniMax-M3 via a gateway) can WRITE a tool call as plain text — echoing the tool-call XML template from the context — instead of emitting a real tool block. With no tool block the agent ends the turn early and shows success, but the tool never ran.
This adds a proxy-side detect + retry fallback:
<invoke>-style tag, or 2+ distinct closing tags like</invoke>+</tool_calls>) AND no real tool block for the wire protocol (Anthropictool_use/ OpenAItool_calls/ Responsesfunction_call). Reuses PR fix: warn on tool-call XML fragments + close-side truncated acp echo (#361) #365'scontainsToolCallXmlFragmentdetector; the structural guard (opening tag OR 2+ distinct closers) avoids false-positives on legit prose that merely mentions a single closing tag.session.metadata.fakeCompletionStreak— consecutive fake-completion turns; resets on a clean turn).Opt-in (disabled by default)
The fallback must buffer the whole response before the client sees it (a fake completion is only knowable at end-of-stream), which defeats incremental streaming. That cost is only worth paying for the low-frequency fake-completion case, so it is opt-in:
BILI_FAKE_COMPLETION_RETRIES=2— enable (2 retries/turn + 2 consecutive-turn cap).0— pre-[bug] 假完成:模型把工具调用写成文字(无 tool_use 块)→ 轮次提前终止,agent 显示正常完成(#361 根因 follow-up) #371 passthrough (streaming untouched).This matches the issue's "先收集数据…确认频率后再决定是否上重试(避免为低频问题加复杂逻辑)" guidance: detection is already observable (PR #365's warn), and the retry can be turned on for the models/scenarios where it is confirmed to help.
Trade-off (documented)
When enabled, streaming responses are buffered before being sent to the client (the client waits for the complete response before seeing tokens). This is the price of being able to retry before the client sees a fake completion.
Tests
tests/fake-completion.test.ts(21 tests):hasToolCallStructure: opening tag / 2+ distinct closers → structure; single closer (legit prose) → not.injectFakeCompletionHintper protocol (merge into last user msg / append).tool_usereaches the client (fake text discarded).Pre-flight
npm run typecheck— greennpm test— 883 passing (all green)npm run build— greenRebased onto latest master (v0.1.66).