Skip to content

fix(#2110): pi/omp compaction takeover watchdog — release native compaction when no proxy fold lands - #2113

Closed
ranxianglei wants to merge 3 commits into
masterfrom
2026-10-04_pi-compaction-takeover-watchdog
Closed

ranxianglei wants to merge 3 commits into
masterfrom
2026-10-04_pi-compaction-takeover-watchdog

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Oct 4, 2026 •

Copy link
Copy Markdown
Owner

Part of #2110

What

The pi/omp plugin's session_before_compact unconditionally cancels host auto-compaction once bili claims ownership (ownsCompaction true). If the proxy then never folds — e.g. nudge double-gate starvation in slow-growth sessions (#2110 RC2), or a preflight that exhausts its budget/time before a fold lands (#2110 RC3) — NOTHING compresses anymore: pi's safety net is cancelled and bili's replacement never fires. Production symptom: context grew to 348% of the effective window while Auto-compaction cancelled repeated.

This PR adds the missing takeover reliability protocol (issue fix 1): the cancellation is only trusted while the proxy is demonstrably folding.

How

Constants are hardcoded (TAKEOVER_MAX_STALE_CANCELS=8, TAKEOVER_TIMEOUT_MS=600_000) — see Config surface below.

Behavior change (old → new)

case old new
owned + proxy folding cancel cancel (unchanged)
owned + proxy NOT folding, ≥8 passes or >10 min cancel forever release native compaction (self-healing)
owned + older proxy (no field) unconditional cancel unconditional cancel (unchanged)
manual compact / not-owned / empty sid user-owned / native unchanged

Scope: pi + omp plugin lanes only. Proxy mode (no plugin header) has no session_before_compact hook — zero change there.

Interaction with fold-state (#1921) — disclosure

Releasing a native pass on a session that already carries bili folds lets pi's native compaction rewrite/remove those folded originals from its history. Two EXISTING mechanisms keep bili consistent — this PR merely makes them reachable for owned sessions, where they previously could not fire:

Residuals a debugger should expect (pre-existing, not new defects introduced here): (a) worst case some folds re-expand (token re-growth) rather than being retained; (b) the #395 notification is fire-and-forget, so the next request can race ahead of it and log an [acp-drift] warn before the archive lands. Net: a released native pass is safe (no data loss) but can surface #1921 drift warnings + possible fold re-expansion in sessions that had prior folds.

Config surface

None added. No new env var / config key / CLI flag. The two watchdog thresholds are module constants, deliberately hardcoded per Configuration Surface Discipline; a createBiliPlugin opts seam exists for tests only. The only wire change is the additive read-only lastCompressAt field on the diagnostic status endpoint (never model-consumed; unknown fields are ignored by older plugins).

Regression review (existing behaviors audited)

Human review required: the wire-shape addition (status endpoint) puts this outside the §7.4 auto-merge gate.

Tests

Rebased onto current master; no conflict with the #2029 status-reader work — the new field sits alongside the provenance-aware fields.

中文摘要

pi/omp 插件在判定 bili 接管压缩后无条件取消宿主自动压缩;若代理侧随后一直不折叠(nudge 双闸门饿死、preflight 超预算等),上下文会失去所有兜底(#2110 生产事故:涨到有效窗口的 348%)。本 PR 补上「接管可靠性协议」:status 端点新增 lastCompressAt(最近一次落地折叠的时间戳,模型 compress 与 preflight 两条路径都写入);插件每轮接管后核对——连续 8 次取消或 10 分钟内没有新折叠落地,即放行原生压缩(可自愈:原生跑完上下文降到阈值以下,钩子自然停跳;代理恢复折叠则下一轮重新接管)。旧版代理(响应无该字段)保持原有无条件取消行为不变;手动 /compact、未接管、空 sid 路径全部不变。不新增任何配置项(两个阈值为模块常量,按配置面纪律硬编码)。

@github-actions

github-actions Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-04_pi-compaction-takeover-watchdog (a75d60c)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2160-pr.2113.f326134

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-pr2113.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 #2113 review 结论(结构化七问)

审前核对:分支已 rebase 到当前 master(3a9ec22e,0 behind / 原 1 commit ahead);CI 在 c54507cb 上 16/16 check-runs 全绿(含 kernel-guard、version-guard、build-artifact、ubuntu+windows 双矩阵 test、全部 real-client lane)。本地复核:定向测试 tests/plugin-agent.test.ts + tests/issue2110-status-lastcompress.test.ts + tests/plugin-status-provenance.test.ts 共 73 项全过,typecheck 干净。

① 原 issue 方向:正确。#2110 三个根因中 RC1(takeover 无释放路径)是用户直接症状的根因,本 PR 只修 RC1;RC2(nudge 双门槛饥饿,kernel 默认值属产品决策)与 RC3(#1818 preflight 修复未合入)明确排除在外、各自跟踪——范围切分干净,没有顺手扩大。

② 回归审查(逐项既有行为):

③ 代码审查(超出 diff 的验证):

  • "landed fold" 语义属实:lastCompress 只在 src/stream.ts:471 成功路径(blocksCreated > 0,失败在 :449 提前返回)落章;全部 5 个 applyRanges 调用点(stream/stream-openai/stream-responses/loop-core/preflight)都是真实 fold 执行,无误落章路径。
  • fetchStatus 软失败契约(src/agent/shared.ts:268:down/unknown/坏 JSON → undefined)与 watchdog 的 stale 路径吻合;连续 8 次 loopback 失败≈代理真死,此时释放给 native 恰是正确处置。
  • Map 卫生:64 上限按插入序逐出最老;credit 重置保留位置;释放即删 entry → 下一周期全新 streak;knownFoldAt=-Infinity 每周期最多送一次"基线免费额"(测试 A 精确 pin 了这个 off-by-one 行为)。
  • 常量硬编码 + factory opts 测试缝(非 env var),符合 config-surface/env-var 纪律;console.error 在该 agent 侧文件符合既有惯例(插件进程,非 server 模块)。

④ 测试有效性:7 个场景 (A–G) 各钉一个独立分支(stale streak 边界、healthy 持续武装、timeout OR 支路、legacy 兼容、null≠证据、unknown conversation、omp 对等+复用),断言有牙——每一步边界都精确区分 {cancel:true} vs undefined,bug 回归时抓得住。状态端点测试 pin 了"字段存在(null)"vs"值透传"的契约区分。两个小问题我已在 PR 分支直接修掉(小问题直接修,各自独立标注 commit):

  1. test(#2110): harden watchdog unit tests —— 场景 (C) 原 timeoutMs:30/sleep(40) 在负载高的 runner 上 pass 间隔可能超过 30ms 导致中途断言翻转(确定性规则 feat: bili CLI + debug in config + README rewrite #7.2),放宽到 500/600;场景 (G) 声称"复用探针 status、不二次往返"但原测试没数请求——FakeProxy 加了 statusCalls() 计数并断言 4 次 fire 恰好 4 次读(已验证成立)。
  2. docs(#2110): document lastCompressAt in the plugin status contract —— PLUGIN.md 的 status 契约节漏了新字段,而"字段存在性本身是跨版本兼容契约"(absent vs null 语义不同),补了示例行 + 一句契约说明。
    修后定向测试 65 项全过,已推送(c54507cb..a75d60c2),CI 在新 head 上重跑中。

⑤ diff 整洁度:4 文件全部与本 PR 目的相关(src/agent/pi.ts、src/plugin.ts、两个测试文件),无锁文件/生成物漂移、无 version 触碰、无无关重排。干净。


结构化七问:

  1. 是什么问题:pi/omp 插件无条件接管 native auto-compaction 后,若 proxy 端从未真正 fold(nudge 双门槛饥饿或 preflight fold 未落地),没有任何回退——上下文一路涨到溢出(生产实例 348% of effective window,"Auto-compaction cancelled" 刷屏)。
  2. 什么原因导致的:takeover 设计只有"声明"没有"校验"——cancel 是确定性的,但 bili 的替代物(proxy fold)不是;系统从不监控 takeover 本身是否还在产出折叠,声明所有权后就没有再看过证据(RC1)。
  3. 怎么修复的:在 ownership 之上加 watchdog 层:每次被 cancel 的 pass 必须出示"有更新的 fold 落地"证据(/__bili/plugin/status 新增 lastCompressAt,由 applyRanges 仅在成功 fold 时落章,model-driven 与 preflight 两条路都覆盖);连续 ≥8 次无证据 cancel 或 >10min → 把该 pass 释放回宿主 native compaction。双向自愈:native pass 后上下文降到阈值下 hook 停火;proxy 恢复折叠则 credit 路径立即重新武装。旧代理无此字段保持历史无条件 cancel(无法区分"旧代理正常折叠"与"旧代理饥饿",且避免混合版本安装静默变行为)。
  4. 是否有回归问题:无意外回归(②节逐项清单)。唯一行为变更是修复本身且已披露:永久 cancel → 有界 streak/超时后释放;另新增本地证据路径每 event 一次 loopback status 读(成本可忽略)。
  5. 是否破坏缓存:否。lastCompress 落章逻辑是既有的且未改动(成功-only);本 PR 只读不写;nudge baseline、cache ledger、prefix-cache anchor、重试策略全部未触碰。
  6. 是否改了设计方向:无架构转向。watchdog 是既有插件侧 ownership 框架内的防御层,证据源复用既有 status 端点与既有 session 字段;RC2/RC3 明确留给各自的 issue/PR。
  7. 是否引入了新的配置:无。TAKEOVER_MAX_STALE_CANCELS=8、TAKEOVER_TIMEOUT_MS=600_000 硬编码(config-surface 纪律;补救手段是修/更新 proxy 而非调参),测试缝走 factory opts 而非 env var。

判定:可合并。wire-shape 增量(status 端点新字段)使其落在 §7.4 auto-merge 门外,需人工合并;合并前等 a75d60c2 的 CI 转绿即可(原 head 16/16 已绿,新增的两个 commit 只动了测试时序余量与文档)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 补充确认:两个 review 小修 commit(ad2375ba 测试加固、a75d60c2 文档)推送后,新 head 16/16 check-runs 全绿(ubuntu+windows 双矩阵 test、kernel-guard、version-guard、build-artifact、全部 e2e lane)。CI 已转绿,可合并。

@namelessvon

Copy link
Copy Markdown

Review: #2113 — takeover watchdog

先说结论:方向我认可,这是我在这类插件里第一次看到的"证据化降级"。lastCompressAt 时间戳比"加个定时器"高一个层次——前者是可验证的事实,后者只是猜。takeovers.delete(sid) 双向自愈、测试 A–G 逐分支 pin(尤其你自己发现并修掉的 statusCalls() 计数缺失)都很扎实。批准这个方向。

下面三点是想在合并前讨论的,按重要性排序。


1. 交还给原生压缩时,bili 侧的 block / visibility marker 账本谁来兜?

review 里写的是:

A surviving native pass is safe (#395 archives the blocks it makes unreachable)

我怀疑这个引用不对。#395 讲的是 pi 自己的 block 存档机制——原生压缩的产物存进 pi 会话。但这里真正的风险面是 bili 侧:

也就是说这个 PR 修好了「不压缩」,但可能引入「压缩了、账本烂掉」。

我同意在 255616 已经越线的场景下这属于两害相权,原生压缩不触发只会更糟。但「两害相权」不等于「无风险」——建议在 PR 描述里把这层交互明确披露出来(哪怕只是一段 warning),而不是用一句 #395 带过。否则后续有人 debug fold 漂移时,不会联想到这个 PR 曾经放行过一轮原生压缩。

(不阻塞合并,但希望写进文档。)


2. MAX_STALE_CANCELS = 8 的实际代价可能比预期高一个量级

我核了一下 host 侧触发频率。pi 的 _compactBeforeNextAssistantResponse(context) 在每次 assistant 响应前执行,一旦越过阈值,每一轮都会触发一次 session_before_compact:

agent-session.js:418  await this._runAutoCompaction("threshold", false)

所以 staleCancels 累加的不是"分钟",是"轮次"。MAX_STALE_CANCELS = 8 等价于:

连续 8 轮超阈值的请求全部被取消并 outbound 一次,才放行第 9 轮

贴一下我这个 issue 的实际现场(gpt-6.1-sol,openai-codex):

指标 实测值
会话请求数 101
nudge idle / 实际 fold 99 / 1
峰值 usage 348%(709,995 / 204,000)
峰值 outbound 946.4 KiB / 541 msgs
preflight 一次耗时 748,357 ms

在这种状态下,8 次 streak 意味着 8 次 ~946KB 的无效上游请求,按我这里观察到的单轮延迟算要等好几分钟——而这段时间用户看到的仍然是 Auto-compaction cancelled 刷屏。watchdog 生效的那一刻,症状已经持续很久了。

TAKEOVER_TIMEOUT_MS = 600_000 能兜一部分,但注意它只在 session_before_compact 事件触发时才被检查(activeSince 在无 fold 时不重置),所以它和 streak 是同时生效的竞争条件,不是"6 分钟内必然放行"。

建议(如果标定还有余地):

  • MAX_STALE_CANCELS 降到 2~3。它的作用是防抖动——防止"恰好一两次没 fold"就交出控制权。2 已经足够,8 偏保守。
  • 或者保留 streak 但把 TAKEOVER_TIMEOUT_MS 压到 2~3 分钟,让 timeout 主导、streak 只做兜底。因为两者代价不对称:streak 错了多烧几轮巨型请求;timeout 错了顶多多交还一次原生压缩。

(这条我有实际数据支撑,如果标定已经定了、理解取舍,那当我没说。)


3. 旧代理兼容分支:在 npm 同包安装下近乎不可达

lastCompressAt 缺失时保持无条件 cancel 的保守取向我完全同意——无法区分「旧代理折叠正常」与「旧代理饥饿」,宁可不动。

不过有个观察:插件和代理来自同一个 npm 包(billion-context),同步升级,混合版本窗口很窄。所以这个分支的论证成本 > 它覆盖的概率。它真正的效果是让**"新插件 + 旧代理"的用户继续裸奔**——但考虑到 auto-update 默认开,这个组合本身就短暂。

不是说要去掉(保守是对的),只是如果这个分支有对应测试(场景 D 看起来有),权重大概可以降低,把注意力留给上面第 1、2 点。


一个小的确认

knownFoldAt: Number.NEGATIVE_INFINITY + entry === undefined 分支,代理重启后 lastCompressAt 归 null,第一次会被当作"有新折叠"白送一次取消额度。你在评论里说了"每周期最多送一次基线免费额",测试 A 也 pin 了。

想确认:release 之后 takeovers.delete(sid) 会清掉 entry,那下一轮又变成 -Infinity 基线 again → 是不是每放行一轮就要多烧一轮注定失败的取消?如果是「释放 → 基线 → 8 → 释放」循环,代价可以接受;如果基线额度会累积复用,那是个 bug。倾向于是前者,但想听你确认。


最后

合并这个 PR 是对的。 只是想说明:它修的是「接管后永不降级」,没修 nudge 双闸门饿死本身(那个涉及 acp-kernel 和 KDD #8「flat 50K by design」的契约,应该单独决策)。所以 watchdog 是兜底,不是治本——合并后我这边大概率还需要保留 compress.nudgeGrowthTokens 的 workaround。

合并发版后我会回来验证,并在 #2110 留结论。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 三点都对着代码核过了,逐条回。先说结论:第 1 点你指得对、我已改 PR 描述;第 2 点机制成立、我认这个代价,但标定值按仓库规则留给 owner 定(我没擅改);确认问题答案是「前者」,无累积 bug。


1. fold-state 交互 —— 你对了一半,我已补披露

核了代码,这里其实是两套独立机制在兜底,不是单一 #395:

所以你说的核心成立:我那句一行带过确实不准,只点了归档路径、没surface #1921 的 drift 交互。但我想对「账本烂掉」稍微纠一下措辞——按代码,最坏结果是部分 fold 重新展开(token 回涨)+ warn 级 drift 日志,不是账本损坏/丢数。而且这两套机制都早于本 PR 存在,watchdog 只是让它们在 owned session 上变得可达(以前 bili 持有压缩权时原生压缩根本不会跑)。

已做:PR 描述加了一节 ### Interaction with fold-state (#1921),把两条机制点全 + 两个 debugger 该预期的残留(①最坏部分 fold 重展开;②#395 通知是 fire-and-forget,下一个请求可能抢在归档落地前打出一次 [acp-drift] warn)。这样以后有人 debug fold 漂移能直接联想到是这 PR 放行过一轮原生压缩。非阻塞,已推。


2. 标定代价 —— 机制成立,我认,但值留给 owner

代码确认你的两个子判断都对:释放判定在 src/agent/pi.ts:591,只在 session_before_compact 里跑,即每个超阈值 turn 一次——所以 staleCancels 是按轮次累加,不是分钟。且 activeSince 只在健康折叠时重置(:574),stale pass 不推进它,所以 timeout 是每次 firing 才检查、且从 takeover 起点/上次折叠算起——正是你说的「竞争条件,不是必然 6 分钟内放行」。

代价不对称我也独立认同:放太早 = 多一轮原生压缩(有界、#395 安全、自愈);放太晚 = 又一轮完整巨型 outbound 请求零压缩 + 用户继续看刷屏,逼近原 bug。你那个场景(~946KB/pass)下 8 轮 ≈ 好几分钟无效上游 + 持续刷屏,第一次放行前症状已经跑很久了。这是我当初选 8 时低估的真实成本。

但我没有擅改常量:这是个降级阈值/产品决策,按本仓库规则(fallback/默认值变更归 human)留给 owner。我的建议——若合并前想收紧:TAKEOVER_MAX_STALE_CANCELS 8 → 3(仍吸收一两次漏折的抖动,最坏浪费轮次砍约 60%),或保留 streak、把 TAKEOVER_TIMEOUT_MS 600s → 120s 让墙钟主导。两者都是既有 factory-opts 缝后的一行改动,都不动配置面。@ranxianglei 这个值你拍板;选哪个我都作为标注 commit 推上去。若 8 是我没看到的刻意取舍,说一声我就保持不动。


3. 旧代理兼容分支 —— 同意降权,但不删

同意同包同步升级 + auto-update 默认开让这个分支概率很低。它是刻意的保守默认(无法区分「旧代理折得好」vs「旧代理饿死」,跨版本中途翻转会静默改变混合安装行为),由测试 (D) 钉住(tests/plugin-agent.test.ts:582,field-absent → 历史无条件 cancel,连打确认永不释放)。留着无害,我不删;注意力优先给 1、2 没问题。


确认问题:release → baseline → release 循环

答案是前者,无累积 bug。 证据:释放时 takeovers.delete(sid)(src/agent/pi.ts:594)把 entry 删掉;下一轮 takeover 重建时 knownFoldAt = Number.NEGATIVE_INFINITY(:587)。基线额度因此每周期重置、绝不跨周期携带。

一个精确细节值得说清:-Infinity 免费额只在 lastCompressAt 是数字时才兑现——

  • 代理本进程从未折过(lastCompressAt === null):null 不是 number,健康分支(:572)根本不进,没有免费额,每一轮从 pass 1 就算 stale → 第 8 轮放行。(这就是你生产现场的情形。)
  • 代理很久前折过一次然后饿死(固定旧时间戳 N):新 entry 见 N > -Inf → 送一次健康取消(knownFoldAt←N),之后 N > N 为假 → 转 stale → 每周期 1 免费 + 8 stale = 9 轮。

两种都是严格 per-cycle、不累积。不是 bug;每周期成本 ≈ 8(null)或 9(旧时间戳)次注定失败的取消——正好就是第 2 点要收紧的那个量级。


立场

同意你的定性:本 PR 修的是「接管后永不降级」(RC1),不修 nudge 双闸门饿死本身(RC2,kernel KDD #8 flat-50K 契约,单独决策)也不修 preflight 预算(RC3,#1818)。watchdog 是兜底不是治本,合并发版后你保留 compress.nudgeGrowthTokens workaround 合理。

合并判定不变:该合。 唯一建议在合并前定的就是第 2 点的标取值(owner 拍板即可);第 1 点的披露已进 PR 描述;当前 head CI 全绿。

@ranxianglei

Copy link
Copy Markdown
Owner Author

Closed per owner decision on #2110 (2026-10-06): a release-back watchdog masks proxy-side folding failures (nudge starvation / fold rollback / preflight timeout) as silent degradation — "348% is an alarm; replacing it with a silencer keeps the root cause hidden". Takeover safety must come from making the replacement genuinely reliable (#1818 for RC3; separate decision track for RC2), not from post-facto fallback. Branch 2026-10-04_pi-compaction-takeover-watchdog left in place should the direction ever change.

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.

2 participants