Repository navigation
[PR] fix(wiring): charge the image reserve on the outbound wire, not the inbound body - #4061
DadayaSuwen wants to merge 7 commits into
Conversation
(ai: vllm-qwen/qwen3.8-27b) 查重:不重复。 与 #3947/#4016 同属「图片质量计费」子系统,但症状与机制都不同:#3947 是出站少算(usage 快路径漏掉图片通道 → payload 估小 ~19× → 重试风暴);本 PR 是入站多算(预留量给已被裁下 wire 的图持续计费 → 窗口表读 863% → nudge 反向压住折叠)。同族的还有已关闭的 #2562(output clamp 里图片双重计数,位置不同)。机制、症状均不同 → 不重复。 标签: 独立验证了什么(不是读 PR 描述)
发现的问题与处置
行为变更披露(按规报告,旧值→新值+原因)① learned 封顶:旧 = learned 替换 prior;新 = learned 绝不超过 prior。 旧 ② 无字节媒体形状失去保守猜测:远程 URL 图(flat 4096/张)、OpenAI file 引用、redacted_thinking payload、#3398 自定义形状 part——入站 walker 给它们计价,出站视图里没有可索引的像素,于是即使没有折叠 nudge meter 的预留也会缩小(例:N 张 URL 图:N×4096 → 0)。方向安全(outbound ≤ inbound 恒成立;URL 字符串本身计入 text 估计;preflight 在实际 forward body 上仍给这些形状计价)。PR 描述里「完全相等」应限定为 inline base64/data-URL 图——已补测试钉住这个 delta(f9bcbdab5)。 ③ projectThinkingMass gap 位移(作者已披露,我核实过公式):anthropic 车道 thinking 归因 gap = provider 总量 − local − overhead − 图片质量;图片质量变小 → 更多残差归到只有签名没有正文的 thinking 块。该残差本来就是被计费但本地看不见的 thinking 质量(#1320),归因方向正确,数字确实会动。 回归清点(diff 触碰路径承担的既有行为)
测试有效性7 例(现 8 例)有牙:用例 1 双向断言「被折叠的图不预留 + 入站口径仍在收费」(把 bug 本身钉住);用例 5 钉 learned 语义(含 min() 选择);用例 7 钉无折叠相等;我加的用例 8 钉无字节形状的 delta。缺口:全部走 bytes 计费、无 pixels 行(低风险,同一 base64ImageCost);unit 级、无穿透 prepare 路径的 e2e(全量 CI 覆盖)。 交互提示#4016(修 #3947,open)触碰同一计费面(cache-ledger 学习侧 + server.ts)——两个 PR 谁后合,谁需要再跑一遍图片计费相关套件确认无语义冲突。 八问
判定:可合(待 3cbc21d 上主 CI 转绿;合并动作留给人)。三个小修已推到 PR 分支(f9bcbdab5 / 089c7ba / 3cbc21d,各自独立标注);唯一待 owner 拍板项:learned 封顶是否接受 min() 语义——我建议接受(理由见①),披露记录以本回复为准。 |
(ai: vllm-qwen/qwen3.8-27b) CI 更新:最终 head |
|
(ai: deepseek/deepseek-v4) 补丁验证与 CI 说明(中文)改了哪些文件(5 commits / 7 files / +254 −16)
本地验证
关键断言(测试里钉住的)
首轮 CI 红的原因(已澄清,非本补丁) 第一轮
上游 master 自己( 另有一条 当前状态 最终 head 一个已知边界(已在审核回复里列过,这里再确认一次) 非 inline-base64 载体(远程 URL 图、openai |
(ai: vllm-qwen/qwen3.8-27b) 收到。逐条核对结果: ① 首轮 CI 红的「既有 flake」说法——已独立核实,成立。 我查了你引用的 master run ② 对你摘要的两处更正:
当前状态:head = 八问(本轮增量结论,未变项同上轮全量回复)
判定维持:可合——待 |
(ai: vllm-qwen/qwen3.8-27b) CI 状态更新(
我已对该 run( |
(ai: vllm-qwen/qwen3.8-27b) CI 追查收尾——重跑结果出来了,且根因已定位:这不是偶发 flake,是 master 侧的既有缺陷。 重跑: 定性:连续多轮、跨平台、同签名,加上本地复现——master 树( 即 #3769 修复引入的两条回归测试目前在 master 自身反复挂掉(其 scheduled run 处置:已按政策立 一个 issue 跟踪:#4063「master CI 持续红:#3769 的两条回归测试在多 shard 反复失败,阻塞所有 PR 转绿」(带来源标记 + 全部证据 + 建议方向:先定性真漏洞 vs 断言坏,kernel 域走 Kernel Boundary)。注意一个严肃的可能:若失败反映的是 #3769 修复的真实漏洞(升级加载后隐藏的失败压缩记录重新浮现 = 原缓存前缀断裂问题在生产仍可能发生),那它只是被当成 flake 掩盖了——#4063 里已写明这条分支。 对本 PR 的含义与你的选择:
learned 封顶 |
The window meter counted images on the INBOUND request body while the egress prune had already dropped them. Clients resend their whole history every turn, folded images included, so a fold that removed hundreds of screenshots from the wire left the inbound body untouched: the reserve kept charging for pixels nobody would be billed for, the meter read several hundred percent of the window, and the nudge emergency-suppressed the very folds that would have shrunk the payload. Production trace (a screenshot-heavy session): usage=863% with a 196KiB outbound body, and three consecutive upstream 400s at ~1.75M tokens against a 1.5M ceiling. Learning already measures the OUTBOUND body — noteForwardedImageFacts is fed countImagesInRawBody(wireBody) in relay.ts and loop/core.ts — so consuming the inbound body on the read side was an asymmetry, not a design. outboundImageReserve() walks prune(msgs, session.state) and charges each surviving image through the same base64ImageCost() the wire-body estimators use (now exported), then applies the same learned-truth cap imageReserveFor does. It returns 0 when no image survives — the honest reserve for a text-only wire. All four prepare paths (anthropic, openai, responses, google) now feed it to effectiveTokenCount instead of imageReserveFor(parsed). The two calibers agree exactly when nothing is folded, so the change is inert on unfurled sessions; a fold can only shrink the reserve, never grow it. Side effect worth noting: prepare-anthropic feeds the same number to projectThinkingMass, whose gap is providerInputTokens - local - overhead - imageTokens. A smaller image mass widens that gap, so more of the residual is attributed to signature-only thinking blocks. That residual is genuinely the billed-but-invisible thinking mass, so the attribution moves in the correct direction.
…ranxianglei#3952 was an unrelated closed PR) Review-side fix on this branch: the suite pins ranxianglei#4060's outbound image-reserve caliber, so it is named issue4060-outbound-image-reserve.test.ts (repo convention: tests/<issueNNNN>-*.test.ts). No behavior change.
…edia shapes Remote-URL images and redacted_thinking payloads are priced by the inbound body walker but carry no indexable image bytes in the pruned outbound view, so the metering reserve shrinks for them even without a fold. Pins the documented scope of the no-fold equality claim (inline base64/data-URL images); overflow stays backstopped by preflight, which prices these shapes through the body walker on the actual forward body.
…erve comment ranxianglei#3952 is an unrelated closed PR (split-brain log observability); the regression this code implements is named after its own issue.
…e parameter The third argument now receives the OUTBOUND image reserve (outboundImageReserve) from all four prepare paths, not an inbound-body measurement — the old inboundImageTokens name describes the value this PR stops passing.
…eserve reader and the raw-body walkers
e6fa022 to
2406d42
Compare
(ai: vllm-qwen/qwen3.8-27b) CI 红了不是这个 PR 的代码问题,是分支基线太旧(stale-base)。我已把 PR 干净地变基到当前 master 并推回,本地定向验证全绿,等你这边 CI 跑完确认即可。 发生了什么:三个 test 分片全挂在同一对 我做的:把 PR 变基到当前 master tip 结构化结论(八问):
判定:可以等 CI 转绿后合并。若你希望我顺带做点别的(补说明、拆 PR 等),告诉我即可。 |
(ai: deepseek/deepseek-v4)
Fixes #4060
改了什么
图片预留量原来是按入站请求体算的,但出站前
prune早就把那些图裁掉了。客户端每轮重发整段历史,折叠之后入站 body 毫发无损,预留量就一直为永远发不出去的像素计费。新增的
outboundImageReserve()把同一份图片质量算在真正会上 wire 的消息上:prune(msgs, session.state)——出站自己用的那份折叠视图;base64ImageCost()(从image-tokens.tsexport 出来),保证 pixels/bytes 计费和配置上限跟 wire-body 估算器同一个尺度;imageReserveFor()同样的 learned 封顶(learned === undefined ? prior : Math.min(learned, prior));0——纯文本 wire 的诚实预留。四条 prepare 路径(
anthropic/openai/responses/google)现在都把它喂给effectiveTokenCount,不再用imageReserveFor(..., parsed, ...)。为什么这是根因,不是堵症状
学习侧量的本来就是出站 wire body(
src/server/relay.ts:598和src/loop/core.ts:512的noteForwardedImageFacts(countImagesInRawBody(wireBody))),所以settleImageLearning学到的是「实际发出去的东西」的单价——而读侧吃的却是入站 body。学出站,却按入站收费,这是不对称,不是设计。AGENTS.md 里那个 litmus test——「同一个根因明天换个形状出现,这修复还成立吗?」——是过的:以后任何新加的、吃入站 body 的预留消费者都会撞同一堵墙,而这次改动让出站视图成为图片质量的唯一来源。
促使我修它的线上轨迹(一个截图很多的会话,历史约 360 张图,出站视图
withB64=0,图已全被折叠):8630034.972029012 = lastInputTokens(23531) + inboundImageTokens(8606503.972029012),而8606503.972029012 / 23743.59(学到的每图成本)= 362.48——预留量按 362 张图收费,实际出站是 196 KiB 纯文本、零张图。紧急分支随后压掉了本该缩小 payload 的折叠,会话一路涨到上游拒收(约 1748713 tokens,超过单次上限 1500000 tokens)。性质
outbound <= inbound,有断言)。kernel/**。副作用,明说
prepare-anthropic把同一个数字喂给了projectThinkingMass,它的 gap 是providerInputTokens - local - overhead - imageTokens。图片质量变小 → gap 变大 → 更多「上游减本地」的残差被归到只有签名没有正文的 thinking 块上。这个残差本来就是被计费但本地看不见的 thinking 质量(#1320),所以归因方向是对的——但数字确实会动,这是除预留量之外唯一一处行为变化。测试
新增
tests/issue3952-outbound-image-reserve.test.ts——7 个用例,按 #3617 的 gate 纪律双向断言:tool_result.content[]里的图也算(stripImages 滑窗剥离的图片无恢复通道,且滑窗每轮击穿 prompt cache(billion-context-pi#593 同族缺口) #1995 parity,线上正是这个形状);回归(本地只跑目标文件):
tsc --noEmit干净,只剩这个 checkout 本来就有的src/storage/mysql.ts(30,78): Cannot find module 'mysql2/promise'(optional peer dep,npm ci没装)。故意不做的
显示字段(
localInputEstimate、contextTokens)继续用 fail-closed 的upperBound。改成 billing 口径会破坏 #2117 契约——tests/issue2117-context-estimate-provenance.test.ts明确断言localInputEstimate > contextEstimateTokens,且没有 usage 锚点时displayContextBest必须返回kind:"upper"。我试过,e2e 报bound (8600) must exceed the billing caliber (8600),已经撤回。那是另一个 issue、另一处测试改动,这个 PR 只做一件事。验证环境
dsh
0.2.0-rc.2+ billion-context0.1.202,anthropic 协议过 OpenAI 兼容网关。提 PR 前先在线上dist/index.js上做过 monkey patch 实测:受影响会话的usage从 863% 回到 5~9%。