Skip to content

fix #590: pi plugin mode reports folded usage, suppress host backfill - #591

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-06_pi-host-usage-folded
Sep 6, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-06_pi-host-usage-folded

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

Fixes #590 — pi plugin 模式下宿主 usage 回填未压缩基线,footer 显示 302.7%/1M 与实际请求(折叠后 ~57K)不符。

Root cause

PR #425 (#408) 的 host usage backfill 在所有模式都 armed hostCreditTokens,但 billion-context-pi 的 wireCompactionDisable 取消了 pi 自身的 auto-compact(pi.on("session_before_compact", {cancel:true}),ACP owns compression)——未压缩基线在 pi 上不驱动任何东西,纯粹造成显示混乱。omp(recordAnchoredHistoryRewrite 触发)与 codex(native-compact 拦截)需要真实未压缩口径作为触发器,#408 行为保留。

Fix

在唯一的 arming chokepoint 加门:src/server.ts 新增 armHostUsageCredit(session, originalMessages, processedMessages, log) —— session.metadata.pluginAgent === "pi" 时直接 return(credit=0),三个 prepare* 站点(anthropic/openai/responses)统一走该 helper。所有下游消费者(adapters / loop/core / plugin.ts 的 SSE·JSON patch / /acp panel)自动看到 folded 口径,无需逐点修改。

Verification

  • 新增 e2e #590: pi plugin mode reports folded usage — host backfill suppressed(tests/host-usage-backfill.test.ts):plugin 头 + /__bili/plugin/tool compress m00001–m00002 → 断言 fold 发生(upstream body 无 sentinel)且客户端 message_start input_tokens 恒等于上游原始值 100(无回填)。sentinel 放在 turn-1 assistant(首条 user 消息是 kernel 永不折叠的 conversation anchor)。
  • pre-flight:typecheck ✓ · 20/20 本文件 ✓ · 全量 1137/1139(2 个已知 plugin-agent.test.ts 环境失败)✓ · build ✓
  • 生产环境 pi 插件发送的 agent 值恰为 pi(src/agent/pi.ts:59 BILLION_CONTEXT_PLUGIN_AGENT === "omp" ? "omp" : "pi",:408 原样 stamp),所以 === "pi" 精确命中、零误伤。

The #408 backfill armed hostCreditTokens in every mode, but billion-context-pi
cancels pi's own auto-compaction (wireCompactionDisable) so the uncompressed
baseline drives nothing on pi — it only put a >100% footer (302.7%/1M) on the
host while the wire actually carried the folded ~57K.

Gate the arming at the single chokepoint (armHostUsageCredit): sessions bound
to pluginAgent "pi" report the folded request's own usage. omp (anchored-
history rewrite accounting) and codex (native-compact interception) keep the
#408 uncompressed baseline, as do plain proxy clients.
@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-06_pi-host-usage-folded (12d62b6)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-591

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr591.tgz
npm install -g package

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] 🏷 收到,开始审核 PR #591(fix #590)。计划:checkout 分支 → 核对 diff 与描述一致性 → typecheck / test / build 本地复跑 → 检查两种压缩模式(plugin/proxy)影响面。有结论会尽快回帖。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

审核完成,结论:approve,可以合并。以下是我的验证记录(全部在 PR head 12d62b6 上独立复跑,base 为当前 master tip 95b1c08,PR 基于 tip、无需 rebase)。

根因与层次判断

#590 的症状是 footer 显示未压缩基线(302.7%/1M)而 wire 上只有折叠后 ~57K。我查证了代码路径:#425/#408 的 backfill arming 内联在三个 prepare* 里、对所有模式一视同仁地 arm hostCreditTokens;而 pi 插件端取消了 pi 自身 auto-compact(ACP owns compression),这个未压缩口径在 pi 侧不驱动任何触发器,纯粹是显示噪声。omp(anchored-history-rewrite 记账需要真实口径)与 codex(native-compact 拦截)确实依赖它。层次判断:用户定位准确——病因在 arming 侧无模式区分,而非显示侧算错;修法(arming 加模式门)打到了核心,不是治标。

Fix 正确性(逐点核对)

  • 唯一 chokepoint 成立:全仓 grep 确认 hostCreditTokens 的其他写入点全是 reset(prepare* 入口、session load/create),arming 只此一处。下游消费者(pickAdapter 传 credit、loop/core、plugin.ts 的 SSE/JSON usage patch、/acp panel 的 hostContextTokens = total + credit)全部读 session 值,credit=0 ⇒ 处处自动 folded,无需逐点改——PR 描述属实。
  • 门精确命中、零误伤:src/agent/pi.ts 确认生产 pi 插件 stamp 的 agent 值恰为 "pi"(omp 场景为 "omp"),=== "pi" 只命中 pi 插件会话。omp/codex/claude/opencode 及 proxy 模式(无 header)行为不变,代理压缩不上报改写量,宿主上下文会计塌缩至 ~0(omps recordAnchoredHistoryRewrite 零调用);且持久化 lastInputTokens 恢复无钳制,旧版负值升级后复活 #408 完整保留。时序也核对过:session.metadata.pluginAgent 在 prepare* 运行前已 stamp(server.ts sticky 绑定处)。
  • 双模式检查(AGENTS.md §6):proxy 模式不受影响;nudge/preflight 读自己的 folded 口径、不受 credit 影响;/acp panel 对 pi 会话变为 folded 口径,与 pi footer 对齐——验收标准满足。
  • 风格合规:commit message fix #590: ✓、version 未动 ✓、无 as any/@ts-ignore/console.error ✓。

测试质量

新增的 #590: pi plugin mode reports folded usage 是真集成测试(真 startServer + 假 Anthropic 上游),不是单测糊弄:断言了 fold 真实发生(sentinel 从上游 body 消失,防 vacuous pass)+ 客户端 input_tokens 恒等于原始 100(无回填)。sentinel 放 turn-1 assistant 而非 user 是对的——m00001 首条 user 是 kernel 永不折叠的 anchor,放 user 侧会导致 sentinel 幸存、测试假失败。设计干净。

独立复跑 pre-flight

项 结果
typecheck ✓
tests/host-usage-backfill.test.ts 20/20 ✓(含新测试)
全量 npm test 1138/1139,唯一失败见下
build (tsup) ✓

全量那 1 个失败与本 PR 无关:resolveClientCommand: codex/claude resolve to themselves(tests/launcher.test.ts:1029)。我在 base 95b1c08 上复跑同一文件,同样失败——预存在的环境相关失败(本沙箱 /usr/bin/codex、/usr/bin/claude 是 npm-global symlink,resolveOnPath 返回全路径,而该测试对硬编码 /usr/bin 期望 bare name)。本 PR 零回归。(你描述里写的 2 个 plugin-agent.test.ts 环境失败与我沙箱观察到的不完全一致,属环境差异,不影响结论。)

顺带发现的小问题(仅报告,不另开 issue):launcher.test.ts:1029 该测试硬编码 { PATH: "/usr/bin" } 并期望找不到二进制,在任何按标准方式装了 codex/claude(npm global → /usr/bin symlink)的机器上都会挂。运行时行为本身没问题(返回全路径反而更稳),建议后续改成像相邻 pi 测试那样用 mkdtemp 临时目录构造受控 PATH。

E2E 备注

本沙箱无可用上游(默认 127.0.0.1:8199 不可达、无 E2E_UPSTREAM_URL),e2e-codex 套件无法在此执行。考虑到改动是纯展示侧门控、且被路径已被新集成测试以 HTTP 级覆盖,风险很低;如按 AGENTS.md 要求合并前跑 e2e,可用 CI 手动触发的 ci-e2e workflow 补一道。

可以合了,merge 留给你。

@ranxianglei
ranxianglei merged commit 3f57ce4 into master Sep 6, 2026
6 checks passed
@ranxianglei ranxianglei mentioned this pull request Sep 6, 2026
ranxianglei pushed a commit that referenced this pull request Sep 6, 2026
Semantic merge onto current master: master's #554 regex superset kept in
util.ts (exceed[_\s]?context[_\s]?size); the raw-body fallback for
side-passthrough payload estimates kept, now writing through the
confirmed-channel naming; import union with backfillHostUsage (#591).
Stale #554 test assertion updated to the confirmed channel (#572).
ranxianglei pushed a commit that referenced this pull request Sep 6, 2026
Semantic merge onto current master: master's #554 regex superset kept in
util.ts (exceed[_\s]?context[_\s]?size); the raw-body fallback for
side-passthrough payload estimates kept, now writing through the
confirmed-channel naming; import union with backfillHostUsage (#591).
Stale #554 test assertion updated to the confirmed channel (#572).
@ranxianglei
ranxianglei deleted the 2026-09-06_pi-host-usage-folded 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.

pi plugin 模式宿主 usage 回填未压缩基线:footer 显示 302.7%/1M 与实际请求(折叠后 ~57K)不符

1 participant