Repository navigation
fix(#2011): floor output-budget caps so max_tokens is always an integer - #2012
Conversation
Since 0.1.181 the input estimate can carry a fraction (image pixel-prior billing, #1843, adds ctx.imageTokens to the estimate), so clampOutputBudget produced a fractional cap that clampOutgoingOutput wrote verbatim into the wire's max_tokens / max_completion_tokens / max_output_tokens. Those fields are integer-typed on every protocol; strict upstreams rejected the whole turn with `max_tokens: Input should be a valid integer` (400) or `Endpoint is unavailable.` (422). Guarantee an integer at both layers so no caller can emit a fractional count: clampOutputBudget floors the cap (keeps log == wire), and writeOutputBudget coerces at the sole writer (also guards the restore path + future callers). No-op for whole-number estimates, so v<=0.1.180 behavior is byte-identical. Regression tests anchor to the exact 0.1.181 production values. Fixes #2011
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.2044-pr.2012.a74bea9This exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr2012.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 收到,正在审核本 PR(拉分支核对代码 + CI 状态 + 回归审查)。结论稍后在此更新。 |
…et comment The fraction since v0.1.181 comes from #1843 L1's learned per-route image cost (learnedImageReserve, cache-ledger.ts — EMA over observed/nImages usage samples), which takes precedence over the integer pixel/byte priors (#488). The old wording credited 'image pixel-prior billing' for adding ctx.imageTokens, but those priors are integers and ctx.imageTokens landed much earlier.
[bot] 🏷 PR #2012 审核结论:修法正确、覆盖完整、无回归引入;CI 红为 master 继承(#2003 已跟踪),非本 PR 引入。可合并。 结构化七问
测试有效性:4 个新测试有牙——我把两个 src 文件回退到 master 版本复跑,2/4 失败(小数 cap + 小数 wire write 两个用例精确抓到原 bug),恢复后 4/4 通过。定向跑 issue2011 + issue453-backstop + side-request-isolation + fix-1075-side-request:41 pass / 0 fail。 CI 状态:base 为当前 master(33d58e5e,#1989 合入后),无需 rebase。e2e 各泳道全绿(真实 opencode/dsh/pi/codex native、hermetic registry ubuntu+windows、build-artifact、version-guard)。红的 4 个 顺手小修(已推 PR 分支,commit aa21543):budget.ts 注释把小数来源误归为 "image pixel-prior billing, #1843 adds ctx.imageTokens"——pixel prior 是整数、 diff 整洁度:3 文件 +79/-3(含注释修正后 +81/-5),每行均与本 issue 相关,无跑题内容;version 未动 ✓。 判定:可合并。 合并受继承自 master 的 typecheck 红阻塞(非本 PR 所致)——待 #2006/#2007 落地或凭上述证据提前合并均可。我无法代合并,请自行操作:#2012 |
Summary
Fixes #2011 — since v0.1.181 the proxy can write a fractional
max_tokensinto the outgoing request, which strict OpenAI-compatible upstreams reject and which kills the whole agent turn:Root cause
clampOutputBudget()(src/server/budget.ts) computescap = window - inputEstimate - margin. Since 0.1.181,inputEstimatecan carry a fraction: the dual-channel image billing work (#1843) addsctx.imageTokens(a pixel-prior value) onto the estimate inclampOutgoingOutput, so the cap came out fractional (e.g.110715.5,95666.75). That value was written verbatim bywriteOutputBudget()into the wire'smax_tokens/max_completion_tokens/max_output_tokens— fields that are integer-typed on every protocol.The issue's control group confirms it: every clamp line on v ≤ 0.1.180 is a whole number; the fractions appear only from 0.1.181 onward. The acp-kernel pin is unchanged at 0.0.100 across both versions, so this is a host-side estimate source, not a kernel change.
Fix
Guarantee an integer at both layers so no caller can ever emit a fractional count:
clampOutputBudgetfloors the cap — keeps the log line (output budget clamped … -> N) consistent with what is actually written.writeOutputBudgetcoerces at the sole writer — also guards the restore path ([bug] bili dsh 长会话中 max_output_tokens 被压到 ≤200,主请求被误判为 side request 并绕过压缩 #546/#546 输出预算高水位可被客户端衰减值污染,饿死会话永久钉在小预算(restore 缺下限) #1665) and any future caller at the type boundary.Math.floor(not round/ceil) so the emitted budget never exceeds the exact headroom (input + output <= windowstill holds).Behavior change (old -> new)
max_tokensgoes from a rejected fraction to its floor — e.g.110715.5 -> 110715,95666.75 -> 95666. Strictly more conservative by < 1 token; previously these requests were hard-rejected.floorof an integer is itself, and the writer'sNumber.isIntegershort-circuit leaves integers untouched. Pinned by the two "no drift" tests.No threshold, default, timing, or output-format drift beyond making an already-invalid field conform to its declared integer type.
Config surface
None. No new env/config/CLI flag; no schema or persistence change.
Scope note (preflight)
src/preflight.ts'swindowClampedOutputalso writes an integer-typedmax_output_tokensfor the summary side-call. Its inputs are all integer-typed today (defaultCountTokensover text + an integer model window; summary calls carry no images), so it has no fractional source in the current codebase and was not part of the reported incident — left untouched to keep this diff scoped to the actual broken path. Noted here as the one adjacent site, in case a future change makes its inputs fractional.Tests
New
tests/issue2011-integer-output-budget.test.tsanchors to the exact 0.1.181 production values:clampOutputBudget(384000, 85032.5, 200000)-> integer110715;(..., 99364.25, ...)->95666.writeOutputBudgetfloors a fraction for every field shape (max_tokens,max_completion_tokens,max_output_tokens,generationConfig.maxOutputTokens) and passes integers through unchanged.Verified teeth: reverting the fix makes the two fractional tests fail; restoring it makes them pass. Targeted run green:
issue2011+issue453-backstop+side-request-isolation= 35 pass / 0 fail. My files add zero type errors (the 12 pre-existing typecheck errors in unrelated test files are identical with/without this change).Regression review (existing behavior touched)
clampOutputBudget: only the return value gains aMath.floor. Integer inputs unchanged; thecap < FLOOR || cap >= requestedguard now compares against the floored value (sub-token edge only).writeOutputBudget: adds an integer coercion before writing. All existing callers feed integers (verified acrosstests/side-request-isolation.test.ts), so their behavior is unchanged.Fixes #2011