Skip to content

fix: prefer usage-grade anchor over anonymous raw-history fallback (#2033) - #2036

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-04_anonymous-anchor-fallback
Oct 4, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-10-04_anonymous-anchor-fallback

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #2033

What

effectiveTokenCount (src/server.ts) returned the full raw-history upper bound for every anonymous-prefix-affinity session, before the lastUsageGradeTokens anchor check could run. An anonymous session that had already received a real usage report therefore lost its measured anchor after a single failed turn (503 / no usage): armFailureShrink arms an estimate-grade baseline, and the next sizing decision took the anonymous early return with raw = estimateCoreMessagesUpper(msgs) + inboundImageTokens — the client's full resubmitted history, which carries unfolded content server-side folding never shrank. Explicit-identity sessions were protected by the anchor check (#1839/#1846); anonymous sessions were not.

Repro numbers from the regression test (window 120K, real input 5000): pre-fix the next nudge sized at 58701 (raw bound, ~12× the real input, usage=49% of window); post-fix it sizes at 5000 (source: "usage").

Fix

One reordering inside effectiveTokenCount: the positive lastUsageGradeTokens check now runs before the anonymous-prefix-affinity raw-history return. The usage/overflow-arm fast path stays first; the raw-history fallback remains immediately after, still serving genuinely anchor-less anonymous sessions (#553) and never-reporting upstreams (#728 — their anchor is absent by construction, so this change cannot affect them).

Behavior change disclosure (old → new)

Session state Old sizing New sizing
Anonymous, anchored, latest baseline not usage-grade (post-failure window) raw-history bound (estimate) last real usage anchor (usage) — the fix
Anonymous, anchor-less (fresh pfa-* mint/fork, never-reporting upstream) raw-history bound unchanged
Explicit identity, any state unchanged unchanged (the explicit est <= 0 && grade > 0 path is unreachable: the anchor is only written by settleUsageReport, which requires a prior prepare having written localInputEstimate > 0; neither stat is persisted, so restart resets both together)

No caps added, no failure rescue removed, no percentage display logic touched. Image accounting (#1137): anchored sessions return the anchor without adding inboundImageTokens — identical to today's explicit-session behavior; anchor-less anonymous sessions keep images in raw. Fork isolation verified: fork adoption copies compression blocks only, not stats, so pfa-* forks start anchor-less and keep the conservative bound.

Tests

New tests/issue2033-anonymous-anchor.test.ts (genuine anonymous OpenAI client, no identity headers):

Related suites green locally (targeted, per local-test discipline): issue1839-ghost-denominator (G1–G3), issue1569 (A1–A6, B1/B2), silent-backend-nudge (#728A/B/C, #1119), fork-adoption; typecheck clean. Full suite + codex e2e run in CI (request-pipeline change).

Config surface

None touched.

@github-actions

github-actions Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-04_anonymous-anchor-fallback (90ea8ad)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2071-pr.2036.d4fe0bb

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-pr2036.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR #2036 review 完成。分支基于当前 master(merge-base == e6b2857,无需 rebase),单提交 f37882d + 我补的一个注释修正提交 90ea8ad。查重:open/closed 全检索无重复项,#2033 是唯一来源 issue。

审核过程(不是只读 diff):

  • 对 effectiveTokenCount 做了全状态组合枚举:改动前后仅一个组合行为变化(匿名 + 有锚 + 最新基线非 usage-grade):raw/estimate → anchor/usage,即修复本身;其余组合逐条比对一致。
  • 逐条核实 PR 声明:①「explicit est <= 0 && grade > 0 不可达」成立——锚只由 settleUsageReport(src/cache-ledger.ts:660,仅 total > 0 时写)写入,而 settle 前必有成功 prepare,prepare 的 localInputEstimate 含 countSystemAndToolsTokens 开销,真实请求恒 > 0;两 stat 均不持久化(persist.ts 只存 lastInputTokens/source),native-compaction rebase 时一起清零(src/session.ts:733/:736)。② fork 隔离成立——applyForkAdoption(src/fork-adoption.ts:251)只复制 blocks/blockContents/messageRefs/tokenSnapshot/计数器,从不碰 stats,pfa-* fork 天然无锚、保留保守上界。③ 图像记账与现有 explicit 会话行为完全一致(锚值本就不含 inboundImageTokens)。
  • 测试有牙:我把 master 的 server.ts 换回去实测——A1 精确失败于 expected 5000, actual 58701,与 PR 正文数字一致;A2 前后都过(fork 会话 lastInputTokens=0 时 preflight 退化为字符估算,大载荷被低估后不压缩直接裸转上游 #553/[Bug] 上游从不报告 usage 时 nudge 永不触发——上下文无界增长直到超窗(ChatGPT 登录态后端) #728 guardrail 未被误伤)。hermetic relay 按最后一条消息内容路由判定,规避了历史重发重复匹配的坑,设计干净。
  • 本地定向验证(按本地测试纪律,全量交 CI):新测试 2/2、issue1839 G1–G3、issue1569 A1–A6+B1/B2、silent-backend-nudge #728A/B/C+effectiveTokenCount's text-only raw bound caps away the image term from the nudge numerator (silent-backend sessions) #1119、fork-adoption 两文件共 19/19 全绿;typecheck clean。
  • 顺手小修(独立标注提交):测试注释里 pre-fix 数值写的 (~15k here) 是过期草稿值,实测为 58701,已在 PR 分支补 commit 90ea8ade docs: correct stale pre-fix raw-bound figure in #2033 test comment(注释-only,零行为影响)。

结构化七问:

  1. 是什么问题:匿名(pfa-*)会话在一次无 usage 报告的失败轮(如 503)之后,nudge 定容从真实输入 5000 漂到原始历史上界 58701(~12×,窗口占用虚报 49%)——任何一轮没有拿到 usage 报告的请求,都会把上下文分母放大 12–20×(nudge 读到 >1000% usage、假 EMERGENCY、基线冻结数分钟) #1839 给显式会话关掉的幽灵路径在匿名分支上漏着。
  2. 什么原因导致的:控制流顺序不对称——匿名 early-return 排在 usage-grade 锚检查之前,有锚的匿名会话永远走不到锚检查;根因是 任何一轮没有拿到 usage 报告的请求,都会把上下文分母放大 12–20×(nudge 读到 >1000% usage、假 EMERGENCY、基线冻结数分钟) #1839 修复只覆盖了显式分支,没有把「usage-grade 锚优先」不变量统一抬到所有分支之前。
  3. 怎么修复的:纯重排、零新逻辑——把正值 lastUsageGradeTokens 检查移到匿名 raw-history fallback 之前;usage/overflow-arm 快速路径保持首位;raw fallback 继续服务真正无锚的匿名会话(fork 会话 lastInputTokens=0 时 preflight 退化为字符估算,大载荷被低估后不压缩直接裸转上游 #553)和永不报告的上游([Bug] 上游从不报告 usage 时 nudge 永不触发——上下文无界增长直到超窗(ChatGPT 登录态后端) #728,其锚构造性缺席,不受影响)。
  4. 是否有回归问题:无。状态枚举确认唯一变化即修复本身;高危模式逐项过——无吞错变化、无阈值/时序漂移、日志与输出格式未动(显示 grade 跟随锚正是修复意图的语义,非漂移)。唯一理论组合(explicit est<=0 && grade>0)从喂 0 变为喂锚,代码审计确认进程内不可达;即便出现也是更安全的方向(不盲掉 nudge,同 [Bug] 上游从不报告 usage 时 nudge 永不触发——上下文无界增长直到超窗(ChatGPT 登录态后端) #728 哲学)。定向回归套件 19/19 绿。
  5. 是否破坏缓存:否。只影响 nudge 定容的本地 token 计数输入,不碰消息内容、wire 形状、cache ledger、压缩块 refs;fork 隔离已验证(fork 不带 stats)。
  6. 是否改了设计方向:否。单函数内重排,把既有「usage-grade 锚优先」不变量补齐到匿名分支,无架构/边界/数据流转向。
  7. 是否引入新配置:无。

CI:新 head 90ea8ad 上 16 个 check(test 矩阵 ubuntu/windows × node 22/24、codex/dsh/opencode/pi 真客户端 e2e、registry e2e、real-image billing、build-artifact)正在跑,mergeable=true、无冲突(当前 blocked 仅为 CI pending)。结果出来我跟进。结论:方向正确、实现最小、测试钉死原 bug、回归面干净——CI 绿即可合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 CI 跟进:head 90ea8ade 上全部 16/16 checks 绿(test 矩阵 ubuntu/windows × node 22/24、codex/dsh/opencode/pi 真客户端 e2e、hermetic registry e2e、real-image billing、build-artifact),PR 当前 mergeable=true、mergeable_state=clean。审查结论维持上一条:可合并。

@ranxianglei
ranxianglei merged commit 4fd4cfe into master Oct 4, 2026
16 checks passed
@ranxianglei
ranxianglei deleted the 2026-10-04_anonymous-anchor-fallback branch October 15, 2026 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Gap] Anonymous failure fallback can bypass an existing usage-grade anchor

1 participant