Skip to content

[PR] fix: clamp learned image cost to the payload's own prior (#4153) - #4156

Merged
ranxianglei merged 1 commit into
masterfrom
2026-10-21_learned-image-cost-ceiling
Oct 24, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-10-21_learned-image-cost-ceiling

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #4153

What was wrong

settleImageLearning attributes the entire usage residual (upstream billed input minus bili's text-side estimate of the same forwarded payload) to images. On a route whose bill systematically outruns the text estimate — reasoning/system/tool mass, relay accounting, a larger client preamble — the learned per-image cost converges via EMA to a nonsense fixed point. Observed: 448,724 tok/image on a 1920×1080 frame whose pixels prior is 2,125. With imageTokenCap defaulting to 0, nothing clamped the value, so a session only ~69K over its window fail-fasted on a phantom 855K deficit.

The three existing guards all missed it:

  • LEARNED_PER_IMAGE_MAX sat at 1e6 — three orders of magnitude above any plausible single-image bill.
  • The cap > 0 ? Math.min(cost, cap) branch was dead at the default cap = 0.
  • The entry TTL refreshed on every settle, so a busy session never expired.

Fix

  • Consume side (load-bearing): the learned reserve for a payload may never exceed LEARNED_VARIANCE_BAND × that payload's own prior (learnedImageReserve in src/cache-ledger.ts). The band (15×) is anchored to the Bill images and text separately — the image estimate's ±15× error bar poisons every window decision (dual-channel accounting) #1843 documented encoder-variance range. It is nImages-independent in total and neutralizes an already-poisoned persisted value immediately — regardless of how it accumulated or whether a cap is configured.
  • Backstop: LEARNED_PER_IMAGE_MAX reduced 1e6 → 100k for the no-prior path only (still admits every realistic single-image bill, including large byte-billing payloads).
  • Write side: settleImageLearning resets instead of EMA-ing off a stale base when the route fingerprint (billing/cap) changed, so setting a cap to mitigate now actually clears the poisoned value rather than only changing read behavior.

Downward corrections (learned < prior — the #1843 rescue for over-counting bytes priors) are untouched: the ceiling bounds only the high side.

Behavior change (old → new)

Surface Old New Why
Learned reserve vs. payload prior unbounded when imageTokenCap=0 ≤ 15× payload prior a residual-attribution blow-up can no longer brick a session
No-prior backstop (LEARNED_PER_IMAGE_MAX) 1,000,000 / image 100,000 / image reject degenerate samples that poison the store
Learning across a billing/cap reconfig EMA continues off the old base reset to the fresh sample a mitigating cap change now clears the stored value

Incident numbers: pre-fix context 1,852,920 (deficit 855,406 → fail-fast); post-fix the per-image cost clamps to 31,875, the reserve drops to 127,500, and context falls to the usage baseline 1,069,263 (deficit 69,263 → compressible).

Config surface

No change. imageTokenCap keeps its name, type, and default (0); its existing per-route semantics are unchanged. The new bound is derived from the payload's own prior, not from a new knob.

Tests

tests/issue4153-learned-image-cost-ceiling.test.ts pins the incident end-to-end (the exact 448,724.59893398033 / seen=791 fixture), the nImages-independence of the total cap, the reduced backstop, rescue preservation, cap∩band composition, the settle admit-vs-reject semantics, and the write-side fingerprint reset. Targeted suites green locally: #4153 + #1843 (14 tests), e2e-image-billing + #2078 outbound-estimate (8 tests); tsc --noEmit clean. Full-suite verdict deferred to CI.

Single-module scope (cache-ledger + its one call site in server.ts); no kernel/ change.

@ranxianglei ranxianglei changed the title fix: clamp learned image cost to the payload's own prior (#4153) [PR] fix: clamp learned image cost to the payload's own prior (#4153) Oct 21, 2026
@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-21_learned-image-cost-ceiling (64fcd2b)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.4952-pr.4156.e149339

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr4156.tgz
npm install -g package

Install instructions are refreshed on each push.

settleImageLearning attributes the entire usage residual (billed input minus
the text-side estimate) to images, so a route whose bill outruns bili's text
estimate by reasoning/system/tool mass or relay accounting converges its
learned per-image cost to a nonsense fixed point (448k observed on a 2k-prior
1080p screenshot). With imageTokenCap defaulting to 0 nothing clamped it, so a
session only ~69K over the window fail-fasted on a phantom 855K deficit. Three
guards all missed it: the backstop sat at 1e6, the cap clamp branch was dead at
cap=0, and the TTL refreshed on every settle.

- Consume side: the learned reserve for a payload may never exceed
  LEARNED_VARIANCE_BAND x that payload's own prior (nImages-independent), so an
  already-poisoned persisted value is neutralized immediately no matter how it
  got there. This is the load-bearing cure.
- Backstop reduced 1e6 -> 100k for the no-prior path only.
- Write side resets on fingerprint change so a reconfigured route stops EMA-ing
  off a poisoned base (previously setting a cap changed only read behavior and
  never diluted the stored value).

Downward corrections (learned < prior, the #1843 rescue for over-counting bytes
priors) are untouched: the ceiling bounds only the high side.

Fixes #4153

@zjbc2000 zjbc2000 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

审过 PR #4156 的实现,逐行确认三点,都不需要返工。

一、承重墙确认

if (typeof prior === "number" && prior > 0) {
    per = Math.min(per, (LEARNED_VARIANCE_BAND * prior) / nImages);
}
return per * nImages;

per ≤ 15 × prior / nImages ⟹ per × nImages ≤ 15 × prior。total 上界与 nImages 无关这一点实现对了——这正是 consume ceiling 比 settle backstop 强的地方,你把两边的关系处理得比我建议的更完整(我原本只说「backstop 不能当解药」,你的做法是保留它的作用 + 另加真正的解药)。

prior 从 imageReserveFor 单点穿入,else 分支保留 LEARNED_PER_IMAGE_MAX 兜无 prior 路径。调用链没有第二处消费 learnedImageCosts,所以没有绕过点。

二、细节一(fp 重置)—— 是我的替代方案不成立,你选得对

我原话给的是「或 settle 侧把 prior 上界作用到 prev.cost」。审下来这个方案做不到:noteForwardedImageFacts 记录的是 {nImages, textSide, host, fp},settleImageLearning 拿不到 prior,要穿就得改两个发送点的签名。一行重置不仅更小,而且语义更准——它实现的是「重配即视学习库失效」这条显式规则,而不是靠一个数值上界去猜旧值该不该信。 撤回我的替代方案。

三、测试覆盖超出我的要求

nImages-independence 用 n ∈ [1,4,40] 钉住;settle-many 专门验证「40 图 → per 71,500 过 backstop,但消费侧仍钳回」;reset 验证 seen 归一后 EMA 从干净基底继续。这三个正好覆盖了我提的两个细节,没有漏。

rescue 和 band 两条也说明你没有把上界做成单向压制——learned < prior 与带内上修都保留。


一条观测建议(不阻塞合并)

审 estimateWireOverhead 时确认 textSide 不含 base64 字节:

return defaultCountTokens(sysText)
     + defaultCountTokens(JSON.stringify(parsed.tools ?? []))
     + (protocol === "responses" ? countLoadedToolTokens(parsed) : 0);

所以 billedTotal − textSide 在构造上就只剩「图片 + 其它非文本质量」,残差归因的思路本身成立,缺的只是上界——你的修法对准了真问题。

但这也意味着 reasoning tokens 与 relay 记账会照样落进图片分母。15× 的带能兜住本 issue 的 211× 偏差,但若某路由的 reasoning 占比本身就超过 15×prior,仍会贴到带边缘。

建议(可选):在 settleImageLearning 里加一条 debug 级日志,把 observed / per / appliedClamp 三者一起打出。理由不是防御性,是可诊断性:这次我能把根因钉死,全靠 learnedImageCosts 恰好持久化在 metadata_json 里,可以直接读出 448,724.59893398033 并与 context − local-est 除尽对账。如果这条学习值只存在内存里,本 issue 根本无从下手——同理,下次若出现新形态的偏差,一行日志能让下一个人少花两小时。

不加也完全可以合。CI 我这侧看到 26 pass / 0 fail / 3 running(ubuntu 24 与 windows 22×2 仍在跑),kernel-guard / config-surface-guard / docs-* / lock-sync 已绿。批准合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重:不重复。 按失败签名检索 open+closed issue/PR(learnedImageCosts / imageTokenCap / learned image cost):命中 #4153(本 PR 的目标 issue)、#1843/#1857(L1 学习机制的起源,已关闭)、#3947(已关闭)。唯一同家族在途项是 #4016(图片 sidecar 漏计——同套 L1 机制的反方向失效:低估导致 shrink 不武装;本条是高估导致 fail-fast),症状与机制均不同,各自独立处理,不构成重复。

标签: 判定为 bug / scope/kernel / p2 —— 缺陷类;机制在核心 cache-ledger 压缩决策面(跨车道,非 dsh 专属,故不打客户端标);有 imageTokenCap 绕行且会话可恢复 → p2。⚠️ 实际落标失败:ework web 的 labels API 在本环境未实现(GET/POST /api/v1/repos/…/issues/4156/labels 均返回 404「endpoint not implemented in ework shim」),无法通过平台 API 打标,请运维知悉。

Review 进度: PR 分支落后 master 64 个提交,已本地 rebase 到当前 master(干净、零冲突);rebase 后 head 上定向测试 36/36 绿(#4153 新套件 + #1843 rescue + e2e-image-billing + #2078 outbound-estimate + 相邻 image 估算面)、tsc --noEmit clean、build OK。master 侧同期合入的三个相邻提交(egress window fuse / overflow 校准环 / cooldown 移除,均属 #4079 族)已逐一核对:走字节级 doom 数学与 k̂ 校准环,不消费 imageReserve 表面,与本 PR 无语义交互。rebase 后 CI 跑完给出结构化八问终审结论。

@ranxianglei
ranxianglei force-pushed the 2026-10-21_learned-image-cost-ceiling branch from d5cdbc7 to 64fcd2b Compare October 21, 2026 11:43
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

Review 完成(独立复审,未复读 issue 侧结论):判定 = 可合入(§7.4 第 8 条涉 fallback/默认值变更 → 人工合并)。分支已 rebase 到当前 master(干净、零冲突)并推送,新 head 64fcd2b7a;该 head 上:定向测试 36/36 绿、tsc --noEmit clean、build OK、远程 CI 30/30 全绿(21 success + 9 个门控 e2e lane skip,零失败)。

结构化八问:

  1. 是什么问题:L1 图片成本学习把整段 usage 残差(上游计费 input − bili 文本侧估算)全记到图上;路由计费系统性高于文本估算时(reasoning/system/tool 质量、relay 记账),学出的单图成本收敛到荒谬定点(1080p 截屏学成 448,724 tok,pixels prior 仅 2,125),默认 imageTokenCap=0 无人拦截 → preflight 判「图片单独就超窗」,把一个实际只超窗 ~69K 的会话 fail-fast 掉。
  2. 什么原因导致的:归因模型无界 + 护栏三道全漏:settle 整段残差 ÷ nImages 做 EMA(α=0.5 × seen=791 收敛到错误定点);backstop 1e6 松三个数量级;cap=0 时钳制分支是死代码;TTL 每次 settle 刷新、忙会话永不失效。症状在管道层(fail-fast),病灶在学习层(残差归因无界 + 消费侧无上界)——修的是后者,不是前者。
  3. 影响范围与严重程度:任何「带图 + 上游计费口径 > bili 文本估算 + 学习已收敛」的路由都中招(事故路由之外,同会话 ark… 条目学成 3,823 也超 pixels 上界,偏差非单路由)。中毒期间该会话每轮 fail-fast 不可用,但有 imageTokenCap 绕行可恢复 → p2。跨车道核心面:四协议全部经 imageReserveFor 单一咽喉消费(已 grep 确认无其他 learnedImageCosts 读取方)。
  4. 怎么修复的:三层防御,承重墙是第一层——消费侧 prior 相对上界:payload 的学出 reserve ≤ LEARNED_VARIANCE_BAND(15×) × 该 payload 自身 prior,总额与 nImages 无关,对已中毒的持久化值即时生效(不等 TTL/重配)。15× 锚定 Bill images and text separately — the image estimate's ±15× error bar poisons every window decision (dual-channel accounting) #1843 文档化的编码器方差带。写侧 backstop 1e6→100k 只守无 prior 路径,拒绝退化样本入库。写侧 fp reset:billing/cap 指纹变化即视为全新样本(cost=per, seen=1),不再拿旧基底 EMA——设 cap 止血从此真正清掉脏值。向下修正(Bill images and text separately — the image estimate's ±15× error bar poisons every window decision (dual-channel accounting) #1843 rescue,learned<prior)不动:上界只约束高侧。
  5. 是否有回归问题:逐项审过触碰路径的既有行为:
  6. 是否破坏缓存:否。wire 字节与前缀缓存不受影响(reserve 只参与尺寸判定,不进转发字节);learned store 失效仅发生在 billing/cap 重配(有意语义);学习值在带内的健康会话零行为变化。
  7. 是否改了设计方向:否。L1 架构(per-route EMA + prior 回落 + fp/TTL 失效)不变;消费点加信任上界、写点加显式重置语义——「学到的修正不得超过独立测量值的合理倍数」是 sanity filter 的正确位置,不是新方向。
  8. 是否引入新配置:否。imageTokenCap 名称/类型/默认值不变(config-surface-guard 绿);新上界由 payload 自身 prior 派生,不是新旋钮。

测试有效性(质量关):真测试、断言有牙——事故 fixture 精确钉死(448,724.59893398033 / seen=791 → 钳回 127,500)、nImages 无关性循环(n=1/4/40 总额恒 15×prior_total)、backstop admit/reject 双向、rescue 保留、cap∩band 紧者胜双向、fp reset + EMA 连续性。任一层回归(删钳/抬 backstop/删 reset)对应测试即红,不是恒真断言。

代码质量:命名达意(LEARNED_VARIANCE_BAND/fresh/prior 参数文档化),注释讲为什么并给锚点(#1843/#4153),无死代码/魔法数字,风格与周边一致;diff 三文件、每行服务于目的,无 lockfile/生成物 churn。

验证证据链:rebase 干净 → 本地定向 36/36 + tsc + build → push → 远程 CI 在推送 head 上 30/30 绿(全量裁决以 CI 为准,本地未跑全量)。

结论:可合入(人工合并即可;合并后 #4153 经 Fixes #4153 自动关闭)。合并顺序两个 PR 皆可,第二落地的一方注意上面第 5 问末条的移植点。

@ranxianglei
ranxianglei merged commit bd9f5fe into master Oct 24, 2026
50 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.807-master.bd9f5fe (rolling master tag).

npm install -g billion-context@0.0.807-master.bd9f5fe  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants