Skip to content

fix: count the image term in effectiveTokenCount's anonymous fork branch - #1139

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-09-22_anon-fork-image-term
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-09-22_anon-fork-image-term

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

Residual from #1119 / PR #1120 review, per @ranxianglei's scope correction: PR #1120 made the #728 min-cap branch of effectiveTokenCount image-aware (const raw = estimateCoreMessagesUpper(msgs) + inboundImageTokens, all four prepare sites passing imageTokensInParsedBody(...)), but the anonymous prefix-affinity early return still computed its bound text-only:

if (session.metadata.anonymousPrefixAffinity) return estimateCoreMessagesUpper(msgs); // images dropped

Wire codecs move images out of CoreMessage.text into sidecars, so an image-heavy fork replaying its full raw history (client reload / #553 forks on usage-silent upstreams) read a text-only char count: usage % far below reality → decideNudge structurally unfireable → over-window payload forwarded raw (the #1110 shape, minus the crash).

Fix

Hoist raw above the early return — exactly as scoped in the issue comment: since a fork replays the FULL raw history in one request, this request's imageTokensInParsedBody is the whole history's image mass, so one hoist is the complete fix. No call-site changes needed (they already pass the image term with per-protocol billing).

Verification

  • New regression pin in tests/fork-nudge-trigger.test.ts: single anonymous request, window 80k, text-only bound ≈ 58% (below every band — 45% first-sight / 70% host emergency / 75% pressure) + 2 × 10k-token inline images → image-aware bound ≈ 83% ≥ 75% pressure band. Fails pre-fix (no nudge — bug reproduced), passes post-fix (trailing nudge injected, images ride un-folded, preflight silent, all messages survive un-folded). Numbers mirror the proven effectiveTokenCount's text-only raw bound caps away the image term from the nudge numerator (silent-backend sessions) #1119 pin in silent-backend-nudge.test.ts.
  • Existing A/B pins in the same file still pass (text-only anonymous fork nudges at 90%; explicit-identity control stays silent).
  • Pre-flight on rebased head (3c8cc3b + this commit): typecheck clean, full suite 2216 tests / 2214 pass / 0 fail / 2 skipped (e2e-gated), build green.

Fixes #1137

The anonymous prefix-affinity early return in effectiveTokenCount still returned estimateCoreMessagesUpper(msgs) — text only — even though #1120 made the explicit-identity min-cap branch image-aware and all four prepare sites pass inboundImageTokens. Wire codecs move images out of CoreMessage.text into sidecars, so an image-heavy fork replaying its full raw history read a text-only char count: usage % far below reality, decideNudge structurally unfireable, over-window payload forwarded raw.

Hoist raw above the early return: a fork replays the FULL raw history in one request, so this request's image term IS the whole history's image mass. Conservative direction unchanged (upper bound → compress earlier, never later); the #728 stale-high invariant is preserved untouched.

Regression pin in tests/fork-nudge-trigger.test.ts: an anonymous image-heavy fork whose text-only bound (~58% of an 80k window) sits below every nudge band but whose image-aware bound (~83%) crosses the 75% pressure band — fails pre-fix (no nudge), passes post-fix; preflight stays silent and nothing folds.

Fixes #1137
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-22_anon-fork-image-term (a0c1cc7)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1139

Each push to this PR publishes a new version under the pr-1139 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1139.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] 🏷 Reviewing this PR now: fetching branch 2026-09-22_anon-fork-image-term (a0c1cc7), verifying the diff against #1137's scope, re-running typecheck + full suite + build on the head, and checking CI. Will report the conclusion shortly.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR review complete — verified independently, ready to merge.

Branch/base: single commit a0c1cc7 on 2026-09-22_anon-fork-image-term, parent = current master 3c8cc3b (freshly rebased, no drift). Diff is tight: src/server.ts (+8/−7 net comment reflow) and tests/fork-nudge-trigger.test.ts only. No unrelated churn.

Code review (against #1137's scope):

  • The fix is exactly the scoped hoist: const raw = estimateCoreMessagesUpper(msgs) + inboundImageTokens; now sits above the anonymous prefix-affinity early return, which returns raw. The min-cap path stays Math.min(est, raw) — untouched.
  • Verified all four call sites already pass the image term with per-protocol billing (src/server.ts:2225 anthropic, :2387 openai, :2579 google, :2794 responses) — no call-site changes needed, as claimed.
  • Invariants hold: conservative direction (upper bound → compress earlier) unchanged; [Bug] 上游从不报告 usage 时 nudge 永不触发——上下文无界增长直到超窗(ChatGPT 登录态后端) #728 stale-high invariant preserved (client-side shrink shrinks both terms); lastInputTokens > 0 fast path still precedes the computation; fresh explicit session first turn stays 0-and-0 (unchanged path). The reflowed comments keep the rationale documented at the new location.

Independent A/B reproduction (not just trusting the claims):

Pre-flight on head a0c1cc7: typecheck clean · build green · full suite 2216 tests / 2214 pass / 0 fail / 2 skipped (e2e-gated) — matches the issue body exactly.

CI: PR is open with mergeable_state: clean; all check runs green — tests on ubuntu+windows × node 22/24, real-codex vs deterministic fake upstream (zero tokens), version-guard, build-artifact.

E2E note: the full ACP_TEST_E2E codex run needs a reachable Responses-compatible upstream (default 127.0.0.1:8199), which this sandbox cannot reach — so I relied on CI's real-codex zero-token phase (green on this commit). That's adequate here regardless: the changed branch fires only for anonymous prefix-affinity sessions, while codex e2e runs under explicit identity, so the targeted regression pin is the appropriate coverage for this change.

Verdict: single-module pure fix, regression test reproduces the original bug, green on the rebased head, diff clean, references its issue via Fixes #1137. Nothing to fix from my side — merge is yours to do.

修复了 effectiveTokenCount 匿名 fork 分支漏计图片 token 的问题(图像被 wire codec 移到 sidecar,text-only 上界导致 usage% 虚低、nudge 结构性无法触发),改动仅为把 image-aware 的 raw 上界提升到 early return 之上,已独立复现"修前失败/修后通过"并全量验证(typecheck/build/2216 测试/CI 全绿),可以合并。

@ranxianglei ranxianglei added the duplicate This issue or pull request already exists label Sep 22, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

Closing as duplicate of #1140 — both PRs implement the identical fix for #1137 (hoist const raw = estimateCoreMessagesUpper(msgs) + inboundImageTokens above the anonymous prefix-affinity early return in effectiveTokenCount) plus a regression pin in tests/fork-nudge-trigger.test.ts. The thread designates #1140 as the canonical PR; this one was opened earlier by a parallel agent run. #1140 verified green on a rebased head (typecheck + full suite 2214/2216 pass + build). Reopen if you'd rather keep this one.

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

Labels

duplicate This issue or pull request already exists

Projects

None yet

Development

Successfully merging this pull request may close these issues.

effectiveTokenCount is image-blind for zero-baseline sessions (anonymous prefix-affinity forks and the #728 local-estimate cap)

1 participant