Repository navigation
fix: gc dead message refs after each turn (#390) - #491
ranxianglei wants to merge 1 commit into
Conversation
Message ids are content fingerprints, so every edit/truncation/dedup orphans the old id's byRaw/byRef entry. Nothing recycled them: the ref space (m00001..m99999) grew monotonically toward the kernel's capacity throw, whose prepare-side catch degrades to 'kernel transform failed, forwarding unchanged' — compression silently stalls for the rest of the session. Dead tokenSnapshot entries (keyed by ref) leaked the same way; orphan-gc'd blocks kept their effectiveMessageIds mappings forever. gcMessageRefs runs after processTurn + reapOrphanBlocks on every prepare path (anthropic/openai/responses) and prunes every mapping whose raw id is no longer reachable: incoming or outgoing view (outgoing includes rendered summaries), an ACTIVE block's effectiveMessageIds (tombstones for decompress), or cited as mNNNNN inside outgoing-view text (so a freed slot is never re-allocated onto a tag a live summary still shows — that would misattribute on decompress). tokenSnapshot is pruned to the surviving refs. The kernel recomputes its allocation cursor as highestUsedIndex(map)+1 each turn (assignRefsNode), so pruning makes freed slots reusable automatically — no cursor surgery. The capacity throw now needs 99999 genuinely live refs; a warn fires at 90k so the pathological case is never silent.
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-491Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr491.tgz
npm install -g packageThis comment is automatically updated on each push. |
[bot] 🏷 审核结论:LGTM,可合并(附 3 条非阻塞小注)。 Triage 三步(先审核后实现的流程记录)1. 复现/查证 —— 根因链属实。 逐行核对了 acp-kernel 0.0.47 源码与代理侧全部 ref-map 消费点:
2. 层次判断 —— 方案打到核心。 存活集设计正确且是最小充分集:入站∪出站视图 id、活跃块 3. 方案评估 —— 成立。 放置位置(processTurn + reapOrphanBlocks 之后)保证被 reap 块的 id 同回合回收; 独立验证结果(PR 分支 668cff3)
一个数字出入:PR 写 993/995(2 个 plugin-agent 失败),我这里 plugin-agent 27/27 全过、失败的换成了 launcher.test.ts——同类环境抖动,不影响结论,但说明那 2 个"已知永久失败"实际是环境相关的。 非阻塞小注(report only,不另开 issue)
结论:根因真实、机制证明扎实、测试覆盖了全部三条验收标准路径,验证齐备——可以合并。 |
|
按维护者指示关闭:这个宿主侧补偿层设计(三条件存活集 + 墓碑 + 文本引用扫描 + 空槽复用)被认为过于脆弱,不适合作为 kernel 行为的外部补丁合入。#390 缺陷本身仍然成立(幽灵 ref 单调膨胀、逼近 99999 时 kernel throw、orphan 块映射滞留),留待 kernel 侧根治(如加宽 ref 编号空间或稳定 id 方案)。 |
Closes #390
根因链
消息 id 是内容指纹(kernel
wire/message-id),所以每次编辑/截断/去重都会让旧 id 死亡,但旧 id 的byRaw/byRef条目没有任何路径回收 → 编号单调逼近 99999,kernelallocateFreeRef抛ref capacity exhausted,prepare 侧 catch 退化成 "kernel transform failed, forwarding unchanged" → 该会话压缩静默停摆。tokenSnapshot(按 ref 键控)同样泄漏;orphan-gc 失活块的effectiveMessageIds映射也永远滞留。关键发现:清条目即复用
kernel 的
assignRefsNode每回合用nextIndex: highestUsedIndex(map)+1重算游标(不持久递增)——所以 只要清掉死条目,空槽复用自动发生,无需任何游标手术;capacity throw 只在真有 99999 个活 ref 时才触发。修复
新增
src/ref-gc.tsgcMessageRefs(session, incoming, outgoing, log),在三条 prepare 路径(anthropic/openai/responses)的processTurn+reapOrphanBlocks之后执行,剪除所有 raw id 不再可达的映射。存活集 =effectiveMessageIds(墓碑,保 decompress 可用);mNNNNN(活跃 summary 引用的标签必须保映射——否则空槽复用后旧引用会错误指向新消息,decompress 张冠李戴)。tokenSnapshot同步剪到幸存 ref;highestUsedIndex ≥ 90000时 warn 一次,病态场景不再静默。测试
tests/issue390-ref-gc.test.ts9/9assignRefs复用空槽(验收标准 2 的机制证明)验证
typecheck ✓;全量 993/995(2 个失败为 plugin-agent.test.ts 已知环境永久失败,干净 master 同样复现);build ✓;E2E 4-phase 核心链路(codex 真客户端)PASS。
备注
applyCompactionArchive(fix: archive blocks across omp native compaction instead of silently deactivating them (#395) #421)的 byRaw/byRef 剪除现在被每回合的 gc 覆盖(它是 liveRawIds-更窄版);保留不动,后续可简化。