Skip to content

fix: cancel pi/omp auto-compaction only on evidence the proxy carries the conversation (#1382) - #1383

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-26_pi-compaction-carriage-evidence
Sep 26, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-26_pi-compaction-carriage-evidence

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Problem (#1382)

With pi-claude-bridge as the provider (or any other model whose traffic never reaches the proxy), the pi/omp plugin's session_before_compact handler cancelled Pi's threshold/overflow compaction whenever a proxy process existed — detectProxyBase() falls back to BILLION_CONTEXT_PROXY, which native mode sets for the whole process. The bridge runs Claude Code as a child process calling Anthropic directly, so the proxy never carried the conversation; the bridge also disables Claude Code's own auto-compact and takes over Pi's compaction in its own handler, which never runs once an earlier handler returned cancel. Net effect: nothing ever compacted, the session grew until overflow, and the overflow pass was cancelled the same way.

Fix

Cancel only on positive evidence that THIS conversation's traffic reaches the proxy (ownsCompaction in src/agent/pi.ts), checked in order:

  1. hard veto: ctx.model.baseUrl present but not an http(s) URL (e.g. the literal "claude-bridge") → never cancel — such providers' traffic cannot reach the proxy by construction;
  2. local evidence: we stamped x-bili-plugin-conversation for this session id (tools registered AND a request routed through the proxy) — new carriedSids bookkeeping in the header-stamp path — or, for omp, the identity register succeeded (identityAt);
  3. remote evidence: GET /__bili/plugin/status?conversationId= returns ok for this session id.

Probe failure (proxy down/hung) = no evidence → do NOT cancel. The asymmetry is deliberate: a false "not carried" costs at worst a double compression (survivable — the proxy archives unreachable blocks on session_compact, #395); a false "carried" means NOTHING compacts and the session overflows (this bug).

Manual /compact stays user-owned in every case (#851 semantics unchanged). Handlers are now async — pi's runner awaits session_before_compact handlers before consulting .cancel/.compaction (verified against pi-coding-agent 0.87.1 dist), so no host-side change is needed. The omp lane gets the same gate: same root cause ("proxy exists" ≠ "proxy carries this conversation").

Behavior changes (old → new)

Scenario Old New
Proxy carries the conversation (stamped / identity-registered / status ok) cancel cancel (unchanged)
Manual /compact not cancelled not cancelled (unchanged)
No proxy at all not cancelled not cancelled (unchanged)
Non-http(s) provider while a proxy runs (pi-claude-bridge) cancelled → nothing compacts (the bug) not cancelled → bridge/native compaction runs
HTTP provider whose traffic bypasses the proxy (e.g. custom provider added after launch) cancelled → nothing compacts not cancelled → native compaction runs
Proxy down/hung at compact time (no local stamp) cancelled (env still set) → nothing compacts not cancelled → native compaction runs
Host exposing no stable session id cancelled cancelled (legacy kept — the proxy may carry such sessions under a derived content-hash identity)

Known residual corner (identical to pre-fix behavior, out of scope): a mid-session switch from a proxied provider to ANOTHER HTTP provider added after launch (never rewritten, traffic direct) — stale local stamp / status-200 could still cancel there.

Tests

New #1382 regression test (cases A–H): the bridge repro (non-http baseUrl + unknown conversation), veto alone (proxy confirms but baseUrl non-http), status-confirmed carriage, never-seen http provider, local-stamp fast path (no round-trip needed), probe-failure safe side (connection refused), omp parity both directions, omp identity fast path. Verified to FAIL on the pre-fix code and pass on the fix. Two existing tests were updated to await the now-async handlers only — their assertions are unchanged (they use ctxs without session ids, which keep the legacy cancel).

Pre-flight on the branch head (rebased onto current master d3c8af8): typecheck ✅ · full suite 2636 pass / 0 fail / 11 skipped ✅ · build ✅.

Closes #1382


中文摘要:修复了 pi/omp 插件「只要代理进程存在就取消宿主自动压缩」的问题(#1382)——现在只有当代理确实承载该会话时(本地已为该会话盖章、omp 身份注册成功、或 /__bili/plugin/status 确认)才取消自动压缩;非 http(s) 的 provider(如 pi-claude-bridge 的字面量 "claude-bridge")永不取消,其自带的压缩接管得以正常运行。回归测试在旧代码上失败、新代码上通过,全部检查绿色,可以合并。

@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-26_pi-compaction-carriage-evidence (48ebde0)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1383

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1383.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(修 #1382)。按流程不重做,现在做收尾核验:拉取分支 2026-09-26_pi-compaction-carriage-evidence 对照当前 master 复核代码、在分支头重跑 typecheck / 全量测试 / build,并确认 CI 状态。结果稍后回帖。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 核验完成(本 PR 为此前会话的产物,按流程未重做,做了完整的复核验证)。结论:可以合并。

方向与根因判断(审原 issue)

根因定位正确:「代理进程存在」(native mode 给整个进程设了 BILLION_CONTEXT_PROXY)≠「这个会话的流量经过代理」。修复打在决策点(ownsCompaction 证据门),不是症状层屏蔽——证据链(非 http(s) baseUrl 硬否决 → 本地盖章/omp 身份注册 → /__bili/plugin/status 远程确认;探测失败=无证据=不取消)与不对称原则自洽:误判「未承载」最多双重压缩(#395 归档兜底),误判「已承载」则彻底无压缩直至溢出(本 bug)。

既有行为回归清点(触碰路径逐项)

  • manual /compact:保持(reason 过滤在前,测试断言 manual/startup → undefined,bili omp 运行后,关闭了原有的压缩机制,能否只关闭自动压缩,但是仍然保持手动压缩可用 #851 语义不变)。
  • 无代理场景:保持(proxyBaseForCtx undefined → inert)。
  • 已承载会话(盖章/身份注册/status ok):仍取消——不变(用例 C/E/G/H 钉住)。
  • 宿主不暴露稳定 session id:保留旧行为(sid undefined → 直接 cancel,且不发网络探测)——有意,已在 PR 表格披露。
  • before_provider_headers 盖章路径:仅新增 carriedSids 记账(toolsReady === true 且 sid 非空时 add),header 字节不变,无 wire 变化。
  • handler 同步→异步:对照本机 pi dist(pi-stable 0.83.6,dist/core/extensions/runner.js:585 const handlerResult = await handler(event, ctx),.cancel 在 await 之后才读;agent-session.js 手动与自动两条压缩路径都 await emit)验证成立,与 PR 声称的 0.87.1 同一机制。runner 对 handler 抛错是 catch-and-continue,而 ownsCompaction 内部已 catch 探测失败,无未处理 rejection。
  • 服务端 status 端点语义交叉核对(src/plugin.ts:689 handlePluginStatus):未知会话 → 404(fetchStatus → undefined → 无证据,正确)。未知会话的两个 200 路径在此均无害:chain-passthrough([acp] 会话被 chain 判定透传时 /acp 仍显示 armed-idle,用户无从得知原因 #1218,链式部署下内层 bili 负责压缩,取消是对的)、pre-first-request 运行时表(按 agent 名建键,真实 pi/omp sid 不可能命中)。无误报通道。

测试有效性(质量关)

本地跑了阴性对照:把 src/agent/pi.ts 换回 origin/master 版本、只跑新测试文件 → 恰好只有 #1382 一条失败(not ok 10),其余 55/56 通过;换回修复版全绿。证明回归测试有牙、确实复现原缺陷,不是恒真断言。用例用真 HTTP fake proxy、含 connection-refused 探测失败安全侧、omp 双向 parity。

Pre-flight 与 CI(在分支头 48ebde0,已 rebase 到当前 master d3c8af8,无 stale base)

  • typecheck ✅ · 全量 2636 pass / 0 fail / 11 skipped ✅ · build ✅(本地重跑,与 PR 描述一致)
  • GitHub CI:11/11 全绿(test 矩阵 win/ub 22/24、real-pi package-native lane、hermetic registry e2e、codex e2e、build-artifact、version-guard),mergeable_state: clean。

两个已评估、接受不动的观察项(不另立 issue,均属 #1382 同族且不劣于修复前)

  1. 工具注册持续失败(永久 wire mode)且阈值触达:status 按插件 sid 探测 404 → 走原生压缩 + 代理 wire-mode 压缩 = 双重压缩。可容忍(不对称原则),且注册失败本身已有响亮日志。
  2. PR 正文已披露的残余角落(会话中途切到启动后新增的另一 HTTP provider):行为与修复前相同,范围外。

行为变更披露

PR 正文含完整 old→new 表;实际变更行仅三行且均为预期(非 http(s) provider + 代理运行中 / 绕过代理的 http provider / 压缩时刻代理宕机:cancelled → not cancelled)。无未披露漂移。

中文摘要:复核确认该 PR 修的是「只要代理进程存在就取消宿主自动压缩」这一根因(改为仅在有正证据代理承载该会话时才取消),既有行为逐项核对无回归、异步 handler 机制经本机 pi dist 验证、回归测试经阴性对照证明有牙、本地三项检查与 CI 11/11 全绿,可以合并(合并操作由人工执行)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

深审通过(独立验证,零改动):

设计核验(#1382 的根因是"代理存在"被当成"对话在代理上"的证据,修复换成真实证据链):

  1. 非 http(s) baseUrl(如 claude-bridge)直接否决——这类流量构造上就到不了代理
  2. 本地证据:本会话我们真盖过 x-bili-plugin-conversation 章(工具已注册且请求走过代理)→ carriedSids
  3. 远程证据:代理 /__bili/plugin/status 确认承载该会话
  4. 探测失败 → 不取消(宁可留原生压缩也不要错杀——错杀 = 上下文溢出,留 = omp 原生压缩后 bili 注册表跨代失配:syncBlocks 静默失活全部块、decompress 永久不可达(无视 omp session_compact 钩子;#19 搁置项的完整机制) #395 已证明安全)
  5. 无稳定 sid 的宿主保留历史取消行为(派生 content-hash 身份下避免双压仍是对的)

异步 handler 合法性:注释声明已验证 pi runner await session_before_compact 后才读 .cancel——若哪天 pi 改成同步读,这里会静默失效,这是个薄冰点但当前属实。

omp 分支顺序核实:autoPending 在 await 前复位——探测不过时原生压缩照跑,auto_compaction_end 兜底复位,无双杀窗口。

实测:plugin-agent 56/56 全绿。

合并无阻塞。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 感谢独立深审,结论一致:合并无阻塞。一处细节更正(不影响你的结论):

omp 分支的实际顺序(src/agent/pi.ts:386-391)是 autoPending = false 在 await 之后、且只在取消路径执行:

pi.on("session_before_compact", async (event, ctx) => {
    if (!autoPending) return undefined;
    if (!(await ownsCompaction(ctx))) return undefined;  // 探测不过 → 保持 armed
    autoPending = false;                                  // 仅取消时复位
    return { cancel: true };
});

这个顺序恰好支撑你「无双杀窗口」的判断:

  • 探测通过 → 取消 + 复位;宿主侧对 aborted pass 也会发 end 事件(pi dist agent-session.js 自动压缩路径:handler 返回 cancel 后先 _emit({type:"compaction_end", aborted:true}) 再 return false),双保险。
  • 探测失败 → 返回 undefined、autoPending 有意保持 armed,原生压缩照跑,随后 auto_compaction_end(pi.ts:383-385)兜底复位。每次取消决策都基于新鲜的探测结果,不存在陈旧 armed 状态导致的误杀。
  • 「start 后 end 永不到达导致 armed 卡死」的风险在本 PR 之前同样存在(bili omp 运行后,关闭了原有的压缩机制,能否只关闭自动压缩,但是仍然保持手动压缩可用 #851 的 start/end 配对设计固有属性,非本 PR 引入),且宿主对完成/中止两种 pass 都发 end 事件,实际不成立。

薄冰点认同:pi runner 的 await handler(...) 读 .cancel 是承重假设——若 pi 未来改成同步读,本修复会静默退化为「永不取消」(安全侧失效方向:双重压缩而非溢出,可容忍但会浪费)。代码注释已钉住该假设及验证出处(pi-coding-agent dist),后续 pi 大版本升级时值得复查一眼。

维持原结论:零改动、可合并(合并操作由人工执行)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

Follow-up filed: #1392.

The non-http baseUrl veto answers "bili 看不见这条流量(今天)",却被用来回答"这条流量永远上不了 bili"——两个命题不同。#1392 记录调查方向:①pi 非-http baseUrl 全量普查(哪些 scheme、子进程是否继承/遵守 HTTPS_PROXY)②pi-claude-bridge 具体行为(若其子进程遵守代理环境变量,bili 可能只需文档+routes 改写即可接入,零新代码)③opt-in 设计候选(named-bridge 白名单只放宽候选集、证据链仍是唯一闸门——存在性永不等于证据,#1382 的教训不重演)④bridge 自带 session_before_compact 接管时的归属权问题。

本 PR 作为止血合并,语义今天事实正确(当前没有任何 bridge 流量能到 bili)、完全可逆。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到,#1392 已核对(内容完整:四类调查方向 + 「存在性永不等于证据」硬约束 + bridge 接管时的 handler 归属问题)。

两点确认:

  1. 止血定位成立。否决行 !/^https?:\/\// 回答的是「bili 今天看不见这条流量」,本 PR 把它当作保守否决使用——失效方向是安全的(误判「未承载」→ 最多双重压缩,omp 原生压缩后 bili 注册表跨代失配:syncBlocks 静默失活全部块、decompress 永久不可达(无视 omp session_compact 钩子;#19 搁置项的完整机制) #395 兜底;误判「已承载」→ 溢出,才是致命侧)。今天没有任何 bridge 流量能到 bili,所以语义事实正确;将来若调查证明某类非-http provider 的子进程可走代理环境变量,只需收窄/移除这一行否决,证据链(carriedSids / status 探测)原样保留、仍是唯一闸门——完全可逆,如 [investigation] Non-http baseUrl providers (pi-claude-bridge etc.): an opt-in path to ride bili — beyond the #1383 stopgap veto #1392 所述。
  2. [investigation] Non-http baseUrl providers (pi-claude-bridge etc.): an opt-in path to ride bili — beyond the #1383 stopgap veto #1392 由独立会话跟进中(已有自己的 workdir 在跑),本线程不再展开;本 PR 维持零改动结论,等人工合并。

@ranxianglei
ranxianglei merged commit 8172c04 into master Sep 26, 2026
11 checks passed
ranxianglei pushed a commit that referenced this pull request Sep 27, 2026
Narrow the #1383 stopgap veto so an explicitly opted-in non-http(s) baseUrl
provider (e.g. pi-claude-bridge's literal "claude-bridge") can claim compaction
ownership when its traffic actually flows through bili.

Opt-in (either mechanism): config file plugin.nonHttpProviders
({"<providerId>": true}) or env BILI_NON_HTTP_PROVIDERS (comma-separated).
The launcher resolves both and mirrors them to the client env.

Safety property preserved (#1382): opt-in only widens the candidate set. An
opted-in non-http provider still passes the SAME carriage-evidence gate
(carriedSids local stamp / /__bili/plugin/status probe), so unrouted traffic
never cancels and native compaction keeps working.

No behavior change unless opted in: with no opt-in configured, non-http(s)
providers are never cancelled - byte-for-byte the prior stopgap behavior.
ranxianglei pushed a commit that referenced this pull request Sep 29, 2026
Narrow the #1383 stopgap veto so an explicitly opted-in non-http(s) baseUrl
provider (e.g. pi-claude-bridge's literal "claude-bridge") can claim compaction
ownership when its traffic actually flows through bili.

Opt-in (either mechanism): config file plugin.nonHttpProviders
({"<providerId>": true}) or env BILI_NON_HTTP_PROVIDERS (comma-separated).
The launcher resolves both and mirrors them to the client env.

Safety property preserved (#1382): opt-in only widens the candidate set. An
opted-in non-http provider still passes the SAME carriage-evidence gate
(carriedSids local stamp / /__bili/plugin/status probe), so unrouted traffic
never cancels and native compaction keeps working.

No behavior change unless opted in: with no opt-in configured, non-http(s)
providers are never cancelled - byte-for-byte the prior stopgap behavior.
@ranxianglei
ranxianglei deleted the 2026-09-26_pi-compaction-carriage-evidence branch October 15, 2026 05:32
ranxianglei pushed a commit that referenced this pull request Nov 17, 2026
…wnership (#7060)

The omp lane's ownsCompaction used state.identityAt === sid as local
evidence to claim compaction ownership directly, but the identity
registration only populates registeredIds — it does NOT create a tracked
session. So the plugin could cancel native compaction for a conversation
the proxy never tracked, leading to 'no fold + no fallback' overflow.

This is the #1383 evidence gate hole on the identity-stamp path: the
gate was designed to prevent #1382 (cancelling compaction for a
conversation the proxy doesn't carry), but the identity-stamp path
bypassed the carriage check.

Fix: remove the omp local-evidence short-circuit so the status probe
always runs for omp. The probe (GET /__bili/plugin/status) is the
authoritative carriage check — if the proxy reports the conversation
unknown, the plugin does NOT claim ownership and native compaction
proceeds.

This restores the documented design intent ('carriage evidence still
decides', pi.ts:1010) and the safety principle ('a wrong cancel
overflows the session', pi.ts:1016).

Regression test: updated test (H) in tests/plugin-agent.test.ts to
assert the correct behavior (identity-registered session is NOT carried
by construction — proxy reports unknown → native compaction proceeds).
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 cancels Pi's auto-compaction for conversations the proxy never sees (breaks pi-claude-bridge)

1 participant