Skip to content

fix: anchor post-rebuild meter to rebuilt payload size (#1820) - #1824

Closed
ranxianglei wants to merge 2 commits into
masterfrom
wt-1820-ework-daemon
Closed

ranxianglei wants to merge 2 commits into
masterfrom
wt-1820-ework-daemon

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Oct 1, 2026 •

Copy link
Copy Markdown
Owner

What

After a big preflight rebuild the usage-grade baseline is momentarily absent (the rebuild request's own report hasn't landed yet — or the upstream never reports one), so effectiveTokenCount fell through to branches sizing on the INCOMING RAW history: the very mass the rebuild just folded away. On code/JSON payloads the char-count upper bound inflates ~3.4×, so the first post-rebuild turn decided against a phantom meter (repro: 597900 vs ~178079 real, usage=358%), lit the kernel's uncadenced EMERGENCY branch, pinned the growth reference phantom-high, and injected a 7K-char nudge into an already-at-window context. #1595's re-anchor only corrected it one more round-trip later.

Fix (per the issue's suggested direction): the preflight fit gate already measures the rebuilt payload — the view that actually goes out. Stamp that size as a bounded meter anchor:

  • preflightCompressIfNeeded success path stamps session.metadata.postRebuildAnchor = { tokens: rebuiltSize, remainingPrepares: 3 } (same measurement as the fit gate, with the raw-body fallback mirroring outboundPayloadBreakdown).
  • effectiveTokenCount consults the anchor AFTER the usage-grade fast path, BEFORE the raw/estimate fallback branches.
  • Lifetime is bounded three ways: each prepare ticks it down (runPrepare top) and the last tick deletes it → never-reporting upstreams fall back to legacy per-turn sizing instead of a frozen meter; a real usage-grade sample supersedes it immediately (settleUsageReport clears it); a native-compaction boundary drops it (resetSessionCompression, same lineage argument as [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's calibration reset).
  • The stamp lives in user-persisted metadata, so reads re-validate; corrupt stamps degrade to "no anchor".

Behavior change (old → new)

  • First ~2 client turns after any successful preflight rebuild: meter decides against the rebuilt payload's measured size (~real size) instead of the incoming-raw char-scale estimate (~3.4× on code/JSON). Consequence: no phantom EMERGENCY nudge / enlarged turn after big folds; on genuinely over-window sessions the nudge still fires once legacy sizing resumes (or earlier, if a usage report lands).
  • Edge refinement in the fits expression: when the kernel transform failed (processedMessages empty, raw body rides), fits now measures originalMessages instead of an empty array — previously such a request could read "fits" purely from overhead+images and skip the refold path.
  • No config surface change. No wire change (the nudge text/injection mechanism is untouched; only when it fires changed). No persistence-format version bump (metadata key, validated on read).

Tests

Part of #1812 (defect B — merged back from #1820). Umbrella stays open until #1818/#1828 land too.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR #1824 review — 代码正确、方向对;但 base 已过期需 rebase,且不能自动合并

先给结论:我把 PR head(c7a174c0)拉下来逐行核对了 diff,并在临时 git 树上把它 merge 到当前 origin/master(92da6acd)验证落点。三处逻辑 hunk 全部干净落地、位置正确,唯一冲突是 src/server.ts 的两行 import(可平凡 union-of-intent 解决)。但 base 过期是硬前置,CI 目前在 head 上还没有任何 check-run。

结构化七问

  1. 是什么问题:大 preflight rebuild 之后 usage-grade 基线瞬时缺失(meter 取不到真实样本),effectiveTokenCount 落回按「入站原始历史」估大小的分支——而那份质量正是刚被折叠掉的。code/JSON 上 char 上界膨胀 ~3.4×(issue 报的 repro:597900 vs ~178079 真实,usage=358%),首个 post-rebuild turn 对着幽灵 meter 判 EMERGENCY,把 ~7K nudge 打进已在窗口的上下文。(数值来自 issue;我独立核对的是代码机制,未复现具体数字。)

  2. 什么原因导致的:根因是 meter 的「取数来源」与实际出站 payload 脱节——rebuild 已把历史折小,meter fallback 却仍按入站 raw 估大小。nudge: no-usage recovery leaves stale reference after network or 5xx failure #1595 的 re-anchor 只是事后一轮才纠正(治标)。本 PR 把取数点移到 preflight fit gate 已经算好的 rebuilt payload 尺寸上,打在源头(疏,不堵)。层次判断:用户描述的是幽灵 nudge(症状),根因是 meter 取数错位;PR 修的是根因 ✓

  3. 怎么修复的:策略 = bounded meter anchor。preflightCompressIfNeeded 成功分支(compressedRanges>0)用 fit gate 同一测量值 stamp session.metadata.postRebuildAnchor={tokens:rebuiltSize, remainingPrepares:3};effectiveTokenCount 在 usage-grade fast-path 之后、raw/estimate fallback 之前读它;生命周期三处兜底(runPrepare 顶 tick 递减、末次删除→永不报告的上游退回 legacy 逐轮估;settleUsageReport 收到真 usage 立即清;resetSessionCompression 在 native-compaction 边界清,同 [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 calibration reset 的血缘论证)。stamp 放 user-persisted metadata,读时再校验,坏 stamp 降级为「无 anchor」。我在 rebased 树上逐一核过三处落点:runPrepare tick @2625、effectiveTokenCount 夹在 fast-path 与 raw fallback 之间 @3198-3199、preflight 成功分支内 @5249(no-progress 分支不 stamp)——都正确。

  4. 是否有回归问题:逐项过既有行为——

    • 稳态(无近期 rebuild):postRebuildAnchorTokens 无 anchor 返回 0 → 完全走原分支,零影响 ✓
    • fits 边缘精化(processedMessages 空→改测 originalMessages):旧代码 size≈overhead 可能误判 fits 让大 raw body 直接转发成 guaranteed-400;新代码测 raw body → 正确 fail/refold。这是修 bug 不是回归,符合 preflight fail-fast × soft-protection livelock: over-window payload with nothing foldable outside the protected zone 502s forever #330「按实际出站 payload 判定 forward/fail」原意,PR 已披露 ✓
    • resetSessionCompression/settleUsageReport 清 anchor:只影响 post-rebuild 短暂窗口或 compaction 边界,语义正确,无稳态漂移 ✓
    • 高危模式排查:fail-fast 未变吞错;nudge cadence 仍是 kernel-owned flat 50K 未漂(变的是「何时因 meter 触发」);输出格式无 wire 改动(nudge 注入机制未动,只改变其触发时机);默认值未变 ✓
    • 两压缩模式:proxy 模式避免幽灵 nudge 是主收益;plugin 模式 proxy 本就抑制 nudge 注入,meter 值变化喂给 agent 自身决策——两种模式都不破 wire ✓
  5. 是否破坏缓存:不破坏。anchor 只影响运行时 meter 决策,不改任何持久化消息字节;head system message 仍 byte-stable(prefix cache 头不动);nudge 追加在会话后段,其出现/缺席不影响 prefix-cache 头。少一个幽灵 nudge 反而让上下文更省 cache。outboundPayloadBreakdown/fit-gate 测量路径此处只读 ✓

  6. 是否改了设计方向:不涉及架构转向。anchor 是现有 meter(effectiveTokenCount)+ preflight fit-gate 测量的局部、有界扩展,复用 fit gate 已算好的同一测量值,与 nudge: no-usage recovery leaves stale reference after network or 5xx failure #1595/[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 同一条 lineage。数据流/边界未变 ✓

  7. 是否引入了新的配置:不涉及新 config surface。唯一新增是运行时 session metadata key postRebuildAnchor(读时校验、向后兼容:旧文件无此 key→视为无 anchor;新 key 被旧代码忽略)。PR 声明 no config surface change,我核实一致 ✓

合并判定:代码可合,但须人工处理前置,不能自动合并:

建议下一步:作者把 wt-1820-ework-daemon rebase 到 92da6acd(只需解上面那两行 import),推上去等 CI 绿。我本地没有跑 typecheck/test/build(工作区无 node_modules,需 npm ci),rebase + CI 会覆盖。CI 绿后我复核 rebase 后的 head 并给最终放行。按 [PR] 规则,CI 未失败、你未明确要求修改,我只审报、不代做 rebase。

@ranxianglei

Copy link
Copy Markdown
Owner Author

[bot] 🏷 Superseded by the consolidated PR #1818 per owner instruction (2026-10-01: 「把刚刚拆分的合并到一个 pr 其他关闭」). This fix (defect B — post-rebuild meter anchored to rebuilt payload size) ships inside #1818 as commit 5f444f53. Closing; tracking continues under umbrella #1812.

@ranxianglei ranxianglei closed this Oct 1, 2026
@ranxianglei ranxianglei reopened this Oct 1, 2026
… conflict: keep master's mergeAdjacentConfigurationUpdates import + drop ensureCanonicalId, add postRebuildAnchor* to session.js imports)
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: wt-1820-ework-daemon (18ef0cd)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1825-pr.1824.fbd6f46

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

Install instructions are refreshed on each push.

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.

1 participant