Skip to content

fix: keep status baselines usage-grade after failures - #2032

Closed
xiaozhu1337 wants to merge 2 commits into
ranxianglei:masterfrom
xiaozhu1337:2026-10-04_status-usage-provenance
Closed

xiaozhu1337 wants to merge 2 commits into
ranxianglei:masterfrom
xiaozhu1337:2026-10-04_status-usage-provenance

Conversation

@xiaozhu1337

@xiaozhu1337 xiaozhu1337 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2029. Follow-up to #1846.

Root cause and change

The main request path already distinguishes measured input usage from failure-time estimates. The status readers did not: acp_status and /__bili/plugin/status passed lastInputTokens straight into pressure decisions, and the plugin panel/JSON exposed that estimate as current context. A failed turn can therefore still produce a false over-window status after the request-path fix.

Use one statusInputBaseline reader for all four status consumers:

  • usage and overflow-arm: preserve the current baseline.
  • estimate and legacy missing provenance: use the last usage-grade anchor, or zero when unknown.

This does not cap percentages, replace unknown with a confident zero measurement, mutate session state/billing counters, or alter failure rescue. The base status report independently counts visible messages. With no usage-grade anchor, the status nudge remains idle and JSON contextTokens=0 represents an unknown baseline, not a measured zero; there is no message-estimate fallback inside decideNudge. Real 174k usage in a 150k window remains visible; credit-adjusted measured usage can fall below the older anchor.

Validation

  • Added 14 deterministic regression/control checks across the tool and plugin status endpoint, including panel/JSON values and read-only state preservation. Eight regression cases fail against unmodified master; all 14 pass with the patch. Focused file: 20/20 passed.
  • Typecheck and build: passed.
  • Full-suite A/B before the final rebase (base 1d95d31c): 3,926 passed, 18 skipped, 4 failed with the patch. The same four failures also occur against the unmodified source (proxy config override, MITM tunnel error text, overview transport recovery, Web UI controls).
  • A/B full-suite baseline with the new tests: 13 failures (the eight regressions, those four existing failures, and one intermittent #1214 tool-registration failure); patched run: only the four existing failures.
  • Rebased to e6b28571 (direct esbuild devDependency change). Rebased typecheck/build and focused suite pass. The rebased full-suite run exceeded the 300-second limit in preflight tests; all 14 new cases passed before the timeout. That local run was incomplete; the PR remained draft until CI validation. CI on this exact PR head subsequently passed all 14 checks, including full suites on Ubuntu/Windows with Node 22/24 and native-client/build/registry checks. The local failures and timeout above remain recorded as local evidence, not hidden. Final independent review completed: the focused suite passes 20/20 and no state/billing mutation or dependency-cycle issue was found. It raised an unresolved presentation concern: no-anchor zero is an internal unknown sentinel, but the shared panel renders it as Context: 0% (0 / 150k) and the public JSON provides no availability marker. This PR remains draft pending maintainer direction on preserving the specified numeric-zero contract versus coordinating explicit unknown/null presentation with the shared panel and feat(plugin): expose isolated forks and attributed context observations #2017. CI passing is not presented as resolution of that review concern.

Scope / config surface

No config, persistence, dependency/version, wire-message, or request-pipeline change. This is the missing status-reader consistency fix, not a replacement for the broader context-observation work in #2017. That PR may need to reconcile the contextTokens assignment in src/plugin.ts when rebasing.

AI-assisted contribution under human direction. Draft for maintainer review/CI; not merged or published.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

Reviewed #2032 (draft) against #2029. I read the full diff, cross-checked the kernel's nudge math, ran the focused suite (20/20) and typecheck (clean), and swept every lastInputTokens reader in src/ for completeness. Verdict: correct, complete, well-scoped, well-tested — merge once CI is green.

结构化七问

  1. 是什么问题: fix: keep estimate-grade values out of context sizing denominators (#1839) #1846 修好了主请求路径(区分实测 usage 与失败时的估计值),但两个只读状态读取器(acp_status 工具、/__bili/plugin/status)仍把 stats.lastInputTokens 原样喂给内核做压力判定——失败轮次 arm 出的估计值被当成"当前上下文"展示,产生假的超窗状态。

  2. 什么原因导致的: 状态读取器缺了主路径已具备的 provenance 门控。根因是"同一份 lastInputTokens,请求路径按来源分级、状态路径不分级",不是某个数值算错。

  3. 怎么修复的: 新增唯一读取器 statusInputBaseline()(src/acp-status.ts:28)——usage/overflow-arm 保留当前值,否则回退到最近实测锚点 lastUsageGradeTokens,无锚点则 0(未知)。四个展示消费者(acp_status nudge、plugin nudge、panel、JSON contextTokens)统一走它,替换裸 lastInputTokens。与主路径 effectiveTokenCount(src/server.ts:3572)及既有 usageGradeInputBaseline(src/server.ts:3610)同源、同"0=未知"约定。

  4. 是否回归问题: 无回归,逐项过——

  5. 是否破坏缓存: 不涉及。状态读取器纯只读,不碰前缀缓存键 / 重试 / 去重 / 增量游标;真正决定缓存相关 sizing 的主请求路径未动。

  6. 是否改了设计方向: 不涉及。把 fix: keep estimate-grade values out of context sizing denominators (#1839) #1846 已有的"provenance 感知基线"从请求路径平移到状态读取器,同一机制,无架构转向。

  7. 是否引入新配置: 不涉及。无新 env / config / CLI / 默认值。

测试有效性(质量关)

14 条新用例真的咬合:包装 processTurn 捕获实际传入的 tokenCount(非恒真断言),覆盖 7 种 provenance 矩阵且含 3 个对照场景(over-window usage / credit-adjusted / overflow-arm —— 应保持原值),并断言只读保持。我独立核算:恰 8 条在未打补丁的 master 上失败(estimate/legacy-no-anchor × 两消费者),与 PR 声明一致。本地定向跑 20/20 通过、typecheck 干净;全量套件按本地纪律交给 CI(PR 已注明 rebase 后本地全量超 300s,故保持 draft,合理)。

一处低优先措辞建议(非阻塞)

PR 描述里 "The kernel retains its normal message-estimate fallback for pressure calculation when the baseline is unknown" 不够精确:我核对了 acp-kernel 0.0.101 dist/index.js:5736 decideNudge,tokenCount=0 时 usage=0 → 所有 pressure/growth 带全假,nudge 结构性 idle。真正在 0 时仍报真实尺寸的是基础报告 buildStatusReport(按消息重数,与 tokenCount 无关)。对 never-reporting / legacy 会话,状态 nudge 行会显示 idle、contextTokens 显示 0 —— 这是诚实的"未知"表达,符合 issue 明确规格("or unknown (0)"),不是缺陷;只是这句描述易让维护者误以为 nudge 在 0 时仍会触发。建议措辞改成"基础报告仍按消息重数给出真实尺寸;nudge 建议在无实测基线时保持 idle"。

合并判定

代码正确、完整、测试扎实,方向与 #1846 一致。等 CI 全量转绿即可合并(merge 归人)。提示:#2017 rebase 时需留意其提到的 src/plugin.ts contextTokens 赋值与本 PR 的交叠。

@xiaozhu1337

Copy link
Copy Markdown
Contributor Author

Follow-up: Pi's local configuration problem has been corrected and the independent review was rerun successfully, without installing context-compactor or changing dependencies/configuration.

  • Reviewed head: 6cc1da35a473a2a0668ac7b37848045585e57c2a; all 14 CI checks pass, including Ubuntu/Windows Node 22/24 full-suite lanes.
  • Independent reviewer reran the focused suite: 20/20 pass. No new import cycle, session/billing write, loss of actual over-window usage, or loss of credit-adjusted current measurements was found.
  • One unresolved presentation concern: for the no-anchor cases the kernel's 0 correctly suppresses measured-pressure claims, but acp-kernel/panel renders Context: 0% (0 / 150k) while the sent-view estimate is 15k. The JSON scalar is also contextTokens: 0 without availability attribution. This should not be interpreted as a measured empty context.

The original issue/spec explicitly allowed unknown (0), and the existing maintainer review considered that intentional. The independent reviewer instead recommends keeping zero only for the kernel and displaying unknown / returning null externally. I am recording this disagreement rather than claiming unanimous review or silently changing the public API. The Pi fallback renderer already ignores a null scalar, but the shared panel's typed input requires a number; changing its unknown presentation needs coordinated support, not brittle string replacement. #2017 separately adds attributed context observations, so an independent scalar-contract change here could conflict with it.

Maintainer decision requested: preserve the scoped numeric-zero contract with this documented limitation, or coordinate explicit unknown/null presentation in this PR / the observation work? Until resolved this remains draft; no merge or publication.

The separate anonymous-path gap #2033 is already addressed upstream by #2036, whose current checks are all green; no duplicate implementation is needed. The inaccurate original sentence about decideNudge falling back to message estimates was corrected after checking the installed kernel implementation.

AI-assisted investigation and review under human direction.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

Rerun confirmed (14 CI lanes green incl. Ubuntu/Windows Node 22/24, focused 20/20, no regressions) — thanks for closing Pi's local-config gap without touching deps/config. On the open presentation question, here's the maintainer-side decision.

Decision: Option A — preserve the scoped numeric-zero contract in this PR

I checked the actual kernel rendering before deciding, and it resolves the disagreement in favor of keeping zero:

  • The panel does not misrepresent. buildStatusPanel puts tokenCount only on the line literally labeled Context (session accounting, host footer scale). The honest message-counted estimate renders on the separate Sent to LLM (after compression, est.) line, computed independently of tokenCount. So for the no-anchor case the panel shows Context …: 0% (0 / 150k) and, one line down, Sent to LLM (est.): 15k. The two-scale "never mixed in arithmetic" design (issue release v0.1.6 #18) means the 0 is already attributed to the session-accounting scale and the real size is visible right beside it. It is not claiming a measured empty context — the reviewer's concern is largely answered by the existing layout.
  • Option B is not expressible in this PR anyway. StatusPanelInput.tokenCount is a required number in acp-kernel (no null/unknown slot). Rendering "unknown" needs a kernel type+render change → pin bump + release (load-bearing per §7.4), or brittle string post-processing of the rendered box (forbidden by the wire-fidelity rule). Neither belongs in a status-reader bugfix.
  • The one genuine gap is exactly feat(plugin): expose isolated forks and attributed context observations #2017's job. The bare JSON contextTokens: 0 has no availability attribution (no sibling "sent-view estimate" scalar in the object) — a programmatic consumer can't distinguish "no measurement" from "measured empty." Attributed context observations are precisely what feat(plugin): expose isolated forks and attributed context observations #2017 introduces. Doing a parallel number → number|null scalar-contract change here would collide with it (concur with your read). Route explicit unknown/null presentation through feat(plugin): expose isolated forks and attributed context observations #2017 as a coordinated kernel+host change, not this PR.
  • Residual severity is low. No-anchor sessions (never-reporting backend / legacy pre-provenance) are rare; the status readers are read-only (drive no compression); and pre-PR these showed 0 or — worse — a ghost failure estimate. The PR strictly improves it.

Pre-merge ask (docs-only, no API/behavior change)

To close the practical misread of the bare JSON scalar without waiting on #2017, add a short clarification:

  1. a comment on statusInputBaseline() stating 0 means "no usage-grade measurement available" (not measured-empty), and
  2. a one-liner in the /__bili/plugin/status JSON semantics (and TECHNICAL-NOTES) that contextTokens: 0 = unknown on the session-accounting scale, with the sent-view estimate on the panel's Sent to LLM (est.) line.

That keeps the diff auto-merge-clean (comment/docs only). Say the word and I'll push it as a labeled commit on this branch; or fold it in yourself since you're mid-iteration.

Acknowledged

Boundary

I won't merge (human-only). If you'd rather have explicit unknown/null presentation now, that's a cross-repo kernel+host contract change best sequenced with #2017 (owner-gated) — not something to bolt onto this bugfix. As-is, my review stands: correct, complete, well-tested; ship with the doc note above once CI is green.

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.

Status readers still trust estimate-grade failure baselines after #1846

2 participants