Repository navigation
[PR] fix #4488: harvest usage from an OpenAI trailing frame whose choices stay non-empty with an empty delta - #4494
ranxianglei wants to merge 1 commit into
Conversation
…on-empty with an empty delta (#4488)
📦 Built Package ArtifactBranch: Latest build — install this exact versionnpm install -g billion-context@0.0.5408-pr.4494.4b4ec42This exact version is immutable and reproducible. The rolling Recent builds (commit → version)
Download artifact
tar xzf billion-context-pr4494.tgz
npm install -g packageInstall instructions are refreshed on each push. |
(ai: vllm-qwen/qwen3.8-27b) 查重:不重复。 检索 open+closed PR 无其他 parser 侧 usage-harvest 修复在途;与已合入的 #593/#589(修的是 标签: bug / p1 / scope/wiring(对齐原 issue #4488;p1 因受影响会话内部记账完全失明直至 502;未打客户端标签——修复是通用 OpenAI relay 解析路径,不绑定特定客户端)。 结构化八问
独立验证(非仅信 PR 描述)
判定代码质量达标,§7.4 auto-merge 条件除「full-suite green」外全部满足——而该条被 #4498 红风暴卡住(根因 bisect 到 f013414/#4390,修复 PR #4502 在途),与本 PR 无关。建议:#4498 修复、master 转绿后 rerun 本 PR CI 即可合并,无需改代码;可选在 PR 描述补一句第 5 问提到的合成 completion 行为变化(旧值→新值:零填充→真实 usage)。合并操作留给你(agent 不合 PR)。 |
Root cause
src/loop/adapter-openai.tsparseStreamharvestsusagefrom exactly two frame shapes: a frame with nochoicesat all (choices:[], the #593 fix) and a frame carryingfinish_reason. An include_usage relay that delivers usage on a trailing frame whosechoicesarray stays non-empty — one choice with an emptydeltaand nofinish_reason(OpenAI's owninclude_usagefinal-chunk shape, or a gateway variant of it) — matches neither, so its usage was silently dropped.The drop was internal-only: that same frame still fell through to the empty-delta meta forward (
adapter-openai.tsL672-673), so the client kept receiving the raw usage frame. What broke was bili's own ledger — nokind:"usage"event was ever emitted, sorecordUsage -> settleUsageReportnever ran for the real sample and nudge stayed pinned on the last stale anchor (growth=0) until the context grew past the upstream's limit (502). This is why[acp-usage]never appeared across the reporter's 154-round freeze window.Fix
One guarded branch in
parseStream: when a frame carriesusagebut has nofinish_reasonand an emptydeltaobject, yield akind:"usage"event (withopenaiCachedTokensnormalization). The guards keep every content / finish / tool-call frame out.Deliberately no
continue: the frame falls through to the empty-delta meta forward unchanged, so the client-visible stream stays byte-identical to pre-fix and only the internal ledger gains the sample. (Acontinuehere would sever that forward and change what the client sees — a regression test pins this.)Because core.ts consumes
kind:"usage"last-wins into one value that feeds bothsettleUsageReport(internal ledger) andemitCompletion(client completion), harvesting on the trailing frame corrects both with no double-count.Both compression modes (AGENTS.md §6)
settleUsageReport+emitCompletion. Fixed. No wire-shape change (the forwarded bytes are unchanged).Verification
npm run typecheck: clean. Full-suite verdict = remote CI on the pushed head.Behavior change disclosure
None to the client-visible stream. By design (the no-
continuefall-through) the forwarded bytes are byte-identical to pre-fix; the only observable change is that the internal ledger now receives the real usage sample it was previously missing — which is the defect being fixed. No threshold / default / timing / output-format drift.Relation to #4383 / #4413 / #4443
Independent defects, one symptom (the freeze). Those are the read-side fix (a stale small anchor degraded to the local estimate as a safety net); this is the parser-side fix (make the real sample land every round). Complementary — both wanted. Not a duplicate of #593 either, which fixed the different
choices:[]shape.Closes #4488