Skip to content

fix: deliver configured v2 model windows to the proxy; anchor nudge sizing on real usage (#1569) - #1589

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-28_v2-window-stamp-and-usage-anchor
Sep 29, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-28_v2-window-stamp-and-usage-anchor

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #1569

What & why

#1569 reported that on opencode v2 (stable) the context limit configured in opencode.json never reaches the bili proxy: x-bili-plugin-context-window is never stamped, so the proxy silently falls back to registry-peek windows — (a) suppressing proactive nudges for months and (b) misfiring emergency shrink. Triage found two independent defects.

A — window never stamped (plugin side). The V2 plugin only read windows from ctx.catalog.model.list() — but stable v2.0.x builds expose NO ctx.catalog at all (probed @opencode/cli 2.0.18: Context exposes model/provider domains instead; ModelApi.list() → { location, data: ModelInfo[] } with limit = { context, output }). So on every stable release the header went unstamped and the proxy degraded into registry/configured-window sizing silently.

Fix: refreshWindows now harvests from three seams — ctx.model.list() (v2.0.x stable), ctx.catalog.model.list() (kept as a hedge for builds/tests exposing it), and catalog.provider.list() records (newer dev builds; Map or plain-object model maps, sync or thenable). When all seams yield zero usable limits, the plugin emits a one-shot console.warn telling the user that opencode.json's limit.context is NOT reaching the proxy. On the proxy side, a plugin-marked request that still carries no window from any source now logs one warn per model (previously silent).

Timing note: each seam commits independently with one .then hop — the same microtask profile as the original single-await implementation. A shared Promise.all settlement point costs an extra hop that the client's next request outruns under Node's microtask FIFO; that deterministically reproduced the round-2 stamp miss in tests/opencode-v2.test.ts#6 during development.

B — estimate-grade sizing misfires (proxy side). While the latest baseline is estimate-grade (the transient window right after a failed turn), nudge/truncation sizing used min(char-count upper bound, localInputEstimate) — ~3.5× high on code/JSON-heavy tool results. That inflation lit spurious nudge bands whose kernel growth-reference pin then blocked GENUINE nudges until context regrew past the artifact (false T1 at 66%, false EMERGENCY at 120%, ~20-min dead zone in the #1569 log).

Fix: new SessionStats.lastUsageGradeTokens anchor, written ONLY by real upstream usage reports (streaming applyUsageSample, JSON non-stream path, loop recordUsage; the arming paths never touch it). While anchored, effectiveTokenCount sizes on the billing-calibrated CJK-aware estimate of the current view (estimateCoreMessages) instead of the char-count upper bound. Never-reporting upstreams keep the fail-closed upper-bound behavior (#553/#728) — their anchor stays absent. The anchor is dropped at native-compaction boundaries (pre-compaction billing evidence describes a payload lineage that no longer exists).

Behavior changes (disclosure)

  1. Nudge/truncation sizing during estimate-grade turns, for sessions that have received ≥1 real usage report: old = min(char-count upper bound, localInputEstimate) → new = calibrated estimateCoreMessages(current view). Why: the old value over-counts code/JSON-heavy tool results ~3.5×, producing spurious nudge bands plus a dead zone that suppressed genuine nudges ([OpenCode v2] Configured model context limit never reaches the proxy (x-bili-plugin-context-window not stamped) - silent registry-peek fallback suppresses proactive nudge and misfires emergency shrink #1569 log). Never-reporting upstreams are unchanged.
  2. Post-failure truncation depth uses the same calibrated view while anchored — overflow rescue may shift one turn LATER than before (to the 400-triggered armOverflowShrink path); safety unchanged (the 400 arm remains the backstop).
  3. V2 plugin surface: also reads ctx.model.list + catalog.provider seams now; warns once per plugin instance when all seams are empty (was silent).
  4. Proxy log surface: one warn per model when a plugin-marked request carries no window from any source (new diagnostic line; no behavior change beyond logging).
  5. New optional session stat lastUsageGradeTokens (persisted like other stats; absent on legacy session files → legacy path). No config-schema change.

Pre-flight

  • npm run typecheck ✅
  • npm test: 3054 tests, 3034 pass, 8 fail — all 8 reproduce identically on clean master baseline (1c60633) in a temp worktree (e2e bug(ccr): retrieval-drop correction notes are consumed before upstream delivery is confirmed #1457 drop-note lanes ×2, resolveCompressSurface ×2, wire-contract goldens ×4) → pre-existing here, not caused by this change
  • npm run build ✅
  • New regression suite tests/issue1569-window-stamp-and-anchor.test.ts: 8/8 (A1–A6 seam matrix incl. malformed-entry inertness + one-shot warn; B1/B2 integration A/B: poisoned estimate view, reporting vs never-reporting upstream)
  • tests/opencode-v2.test.ts: 28/28 (round-2 stamp timing preserved)

@ranxianglei
ranxianglei force-pushed the 2026-09-28_v2-window-stamp-and-usage-anchor branch from ec4848b to 210cddc Compare September 28, 2026 12:20
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-28_v2-window-stamp-and-usage-anchor (210cddc)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1528-pr.1589.1d436ba

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-pr1589.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 review — fix for #1569 (rebased onto current master)

① 原 issue / 方向
#1569 的两个独立缺陷都在根因层修复,不是打补丁:

  • A(窗口不送达):修在发送侧 src/agent/opencode-v2.ts — refreshWindows 多缝收割(ctx.model.list() / catalog.model.list() / catalog.provider.list(),Map 或 plain-object、sync 或 thenable),每条缝独立 .then 提交。针对的是稳定 v2.0.x 没有 ctx.catalog 这一真实原因。
  • B(estimate-grade 误触发):修在决策点 effectiveTokenCount(src/server.ts)——有真实 usage 锚点时改用校准后的 estimateCoreMessages(当前视图),不再用字符上界。对照 issue 里的生产日志验证成立:估算 609968 vs 实际 170319(≈3.6×)、假 T1@66%、负增长 pin 掉真 nudge ~20 分钟、假 EMERGENCY@120%(1110034 vs 307093)。

Stale base → 已直接 rebase
PR 基线是 1c60633c,期间 master 走了很远(cache-attribution stack #1558 把三处 usage-settle 重构进共享的 settleUsageReport @ src/cache-ledger.ts;其后还有 #1544/#1571/#1573/release v0.1.167)。PR 的三处内联锚点写入与该重构正面冲突。我已把分支 rebase 到当前 master af7d7407:三处写入收敛为 settleUsageReport 内一行(session.stats.lastUsageGradeTokens = session.stats.lastInputTokens,位于 s.total > 0 块内)——语义逐字节等价(同样的 total>0 门、同样扣过 compressCredit 的净值;三种响应形态 recordUsage core.ts:290 / applyUsageSample plugin.ts:1141 / server.ts forward non-stream 全部经过这里)。最终 head 210cddc8 已 force-push 到分支,保持单线性 commit。

② 回归审查(既有行为清点)

  1. handle() 窗口解析链:顺序与取值不变;仅在「合作 agent 在但窗口缺失」时新增 once-per-model warn(纯日志)。→ 保持
  2. effectiveTokenCount:usage-grade early-return(第一行)不变;锚点缺失时 legacy min(est, raw) 路径不变——永不报 usage 的上游(fork 会话 lastInputTokens=0 时 preflight 退化为字符估算,大载荷被低估后不压缩直接裸转上游 #553/[Bug] 上游从不报告 usage 时 nudge 永不触发——上下文无界增长直到超窗(ChatGPT 登录态后端) #728)保持 fail-closed 上界。新分支只在 estimate-grade 轮次且已有 ≥1 次真实报告时可达(否则第一行就早退)→ 披露项 1 范围精确成立
  3. session.stats:新增可选字段 lastUsageGradeTokens,随 stats 持久化,原生压缩边界随 localInputEstimate 一起在 resetSessionCompression 删除(压缩前的计费证据描述的是已死的 payload 谱系)→ 有意变更,无持久化版本 bump
  4. opencode-v2 refreshWindows:原 catalog 缝保留为对冲;微任务时序理由(每缝独立 .then;Promise.all 结算点多一跳会被客户端下一请求抢先)在代码与测试中都有记录 → 有意变更
  5. wire 协议 / config schema / update 机制 / kernel pin:未触碰 → 保持

③ 代码审查

④ 测试有效性
新套件 tests/issue1569-window-stamp-and-anchor.test.ts(8 个)断言有牙:B1/B2 在同一 poisoned-estimate 视图上做 A/B——anchored <25k(校准)vs unanchored ≥40k(字符上界);anchored 轮次 NUDGE_MARKER 缺席;永不报告上游保持 legacy 上界;A1–A6 覆盖缝矩阵含畸形条目惰性 + one-shot warn。两个缺陷回归时都能抓住。

⑤ diff 整洁度
单线性 commit,5 files +601/−17 vs master,每一行都与 PR 目的相关;无 lockfile/version churn。

Behavior changes(flagged,与 PR 披露一致)

  1. estimate-grade 轮次(已有 ≥1 次真实 usage 报告的会话):nudge/truncation sizing 由 min(字符上界, localInputEstimate) → 校准的 estimateCoreMessages(当前视图)。永不报告上游不变。
  2. 失败后 truncation depth 用同一校准视图——overflow rescue 可能晚一轮落到 400 触发的 armOverflowShrink 路径;安全语义不变。
  3. V2 插件:新增收割缝 + one-shot console.warn(原先静默)。
  4. Proxy:每模型一条 warn,仅日志。
  5. 新增可选持久化 stat;无 config schema 变化。

验证证据(final head 210cddc8,base af7d7407)

  • typecheck ✅ · build ✅(dist-import-annotations OK)
  • npm test:3121 tests, 3105 pass, 0 fail, 16 skipped。PR 声称旧基线上的 8 个 pre-existing 失败(bug(ccr): retrieval-drop correction notes are consumed before upstream delivery is confirmed #1457 lanes ×2、resolveCompressSurface ×2、wire-contract goldens ×4)在当前 master 上逐一确认通过,非本次改动引入或掩盖
  • 定向:issue1569 套件 8/8 · opencode-v2 套件 28/28
  • CI:旧 head ec4848ba 尚无任何 check-run 上报;新 head 会重跑

Out-of-scope 观察(未动,不另立项):[acp-drift] fold coverage mismatch: 18/29 属 #1195/#1328 已有跟踪范围;[tag-echo] stripped model-emitted render tag ×10 是过滤器按设计工作。

结论:可以合并(待新 head CI 转绿)。

中文摘要:修复了 v2 宿主配置的 limit.context 无法送达代理、以及 estimate-grade 轮次按字符上界(~3.5× 高估)误触发压缩的问题(改为以真实 usage 报告为锚点使用校准估算);原分支基线过旧,已直接 rebase 到当前 master 并把三处锚点写入收敛进共享 settleUsageReport(语义完全等价),typecheck/build/全量测试(0 失败)全部通过,可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 补充:新 head 210cddc8 的 CI 已全部跑完——13/13 check-run 全部 success(含 ubuntu/windows × node 22/24 矩阵、e2e、hermetic registry e2e、各真实 client lane、version-guard)。本地验证 + CI 双绿,PR 处于可合并状态,等人工合并即可。

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

Labels

None yet

Projects

None yet

1 participant