Skip to content

[PR] fix: preserve native Pi compress carrier identity and cached prefix - #3592

Closed
WSXYT wants to merge 3 commits into
ranxianglei:masterfrom
WSXYT:2026-10-17_pi-compress-call-identity
Closed

WSXYT wants to merge 3 commits into
ranxianglei:masterfrom
WSXYT:2026-10-17_pi-compress-call-identity

Conversation

@WSXYT

@WSXYT WSXYT commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #3587

Pi's native extension discards execute(toolCallId, ...) and the plugin tool endpoint assigns every compression block an unrelated plugin_<timestamp> id. The kernel therefore treats still-active successful compress calls as orphan calls. After two newer attempts, even failures, it removes the success pair and the host must reintroduce an early summary anchor. An otherwise append-only conversation then loses its cached prefix.

This change carries Pi's native call id through the existing internal tool transport and uses it as the block's compressCallId. Callers without a native id retain the synthetic fallback. An optional id is validated before any mutation; tool arguments and reasoning bytes are unchanged.

For existing Responses/plugin sessions, legacy synthetic blocks are rebound before orphan cleanup only when the active range has exactly one block and one resent call, and that call has a successful receipt naming the exact block. Ambiguous candidates, foreign receipts, absent receipts, and compaction-generation turns are left alone. No kernel behavior is changed; proxy-loop cleanup and the bounded orphan window remain intact.

Validation: on the deployed v0.1.197 base, red tests reproduced the wrong block identity and active-carrier eviction. The final focused run passed 121/121, zero failures/skips, with clean typecheck and build. On current master b10c5257 (v0.1.198), the focused run passed 114/114, zero failures/skips, plus clean dependency builds, typecheck and application build. The route test captures upstream input and verifies exact prefix preservation after two failed calls. Native-extension transport, malformed-id rejection, fallback, ambiguity, and matching block receipt checks are covered.

Five real requests through the configured Responses service, using only an isolated synthetic conversation, produced post-fold warm / two-failure / continuation hit rates of 98.3% / 97.1% / 97.8%. This is a small focused service check, not a large-context or all-cache-loss claim. The production session transcript was not used in the experiment.

Related: #1567/#1570 handle summary-carrier fallback; #2970/#2977 handle MCP tool-name cleanup. This change fixes native Pi's dropped call identity and preserves compatibility for existing bare-name compress histories.

Config surface: none. The optional internal tool-request envelope gains toolCallId; no user config, environment variable, CLI switch, model-output schema, persistence-version or kernel change.

Limits: folds and native compaction can still replace history intentionally; later cleanup of genuinely orphaned/consumed records may change a prefix, and provider cache serving is outside this fix. This PR does not claim every low-cache event has the same cause. Wire/message and identity changes require human review; no auto-merge.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重结论(独立复核,先于一切):不重复。全仓 open/closed PR 检索后,#3592 是唯一针对「Pi 原生压缩调用身份丢失」的修复。相邻但机制不同——#1567/#1570 是「名字对不上时按 range 兜底认 carrier」(面向 foreign-name),#3222/#3225 是「识别 MCP mcp__bili__compress 名称」;本 PR 是「把真实 id 带过去让 id 直接对上」,属互补而非重复。无并行在途工作。

已独立复核:拉取 PR head 5c0e6bd(单 commit,base=master b10c525,落后 0 commit = 新 base)、通读 6 文件 diff、在 kernel 源码里钉死根因、本地定向跑改动相关的两个测试文件 + 全量 typecheck。结论如下。


1. 是什么问题:Pi 原生插件把模型发起的 compress 调用 id 丢掉了(execute(toolCallId,…) 没用上),代理端给每个压缩块另发一个不相干的 plugin_<时间戳> id。于是内核把「仍然有效的成功压缩调用」当成孤儿;再冒出两次新的尝试(哪怕失败),这个成功对就被清掉,早期 summary 锚点被重新塞回历史——一条本应只追加的会话就此丢了前缀缓存。

2. 什么原因导致的(分层):表层是「缓存命中率掉」;机制层是身份在源头就没对上——块的 compressCallId 永远不等于重放载体的 id。而内核 hideConsumedCompressCalls 里对裸名 compress 的孤儿判定只按 id 匹配、不走 range 兜底(resolveFate:bare-name ⇒ 仅 id 命中才算 active,否则 orphan),所以一旦 id 对不上就是 orphan,超过 KEEP_LAST_ORPHANED=2 窗口即被隐藏。根因在「真实 id 中途被丢弃 + 传输没带它」,不在内核判定逻辑本身。

3. 影响范围与严重程度:仅限 plugin-mode Pi 会话;触发条件 = 折叠过至少一次 + 之后又有 ≥2 次 compress 尝试(成功或失败)。后果是稳定前缀被重新计费(性能/成本退化,非数据丢失、非不可用),可用性完好。属「特定条件下有问题」。PR 自述 5 个隔离请求实测 warm/两败/续写命中 98.3%/97.1%/97.8%,并明确声明是小规模服务验证、不代表大上下文/全缓存丢失——口径克制,认可。

4. 怎么修复的(策略层):把客户端真实 toolCallId 沿既有内部工具传输带进 handlePluginTool,直接用作块的 compressCallId——在身份源头修,不是中途改字节遮症状(符合「疏不堵」)。id 可选、且在任何状态变更前校验(非字符串/空/超长 512 → 400,不动状态);没有 id 的调用方保留合成兜底。另加一个保守的一次性存量迁移 reconcilePluginFoldCallIds:只在「同一 range 恰好一个 active 块 + 恰好一个重放调用认领 + 该调用有成功回执且回执点名了确切的 blockId」时才把旧合成块重新绑到真实载体上;歧义候选/外来回执/缺回执一律不动。这是治存量,不是新增机制。

5. 是否有回归问题:逐项过既有行为——

  • 新会话(已是原生 id):迁移立即跳过,no-op,零影响;
  • 迁移只碰 active 的 plugin_ 合成块,且只改归属元数据(compressCallId),不动 block 内容/summary/messageRefs/blockId/startRef/endRef,幂等(绑过一次就不再是 plugin_ 前缀);
  • 运行位置:prepareResponses 在 runPreparedPipeline 内,已被 withSessionLock 包住(handle.ts:2532/2547),与既有每轮状态变更同锁,未引入新竞争;且在 kernel 管线之前执行,顺序正确;
  • proxy mode(服务端执行、tool call 短暂)完全不经过这条路径,未触碰;上游模型 wire 输出的 tool arguments / reasoning 字节不变;
  • 未发现回归。一处轻微脆弱性(非回归):迁移耦合了 kernel 回执格式串([Compressed … + 某行以 · <blockId> full summary 开头);若将来 kernel 改了这个格式,迁移会静默 no-op → 退化为「现状行为」,是 fail-safe 方向,不是破坏。建议后续把这两处格式串抽成共享常量对齐(可选,不阻塞)。

6. 是否破坏缓存:这本身就是恢复前缀缓存稳定的修复,不破坏任何缓存/重试/游标/去重策略。迁移用 markDirty(bump revisionEpoch)正确失效 bili 自己的修订缓存,方向恰当。

7. 是否改了设计方向:无架构转向。只是给内部工具传输信封加了一个可选字段 + 一条身份承载路径,与 KDD#7(plugin-mode 由 agent 拥有压缩)、#1567 的 carrier 识别设计一致,是补强不是改向。

8. 是否引入新配置:无。仅内部信封加可选 toolCallId;无用户配置/env/CLI/persistence-version/kernel 变更。与 PR 所述「Config surface: none」一致,已核对属实。


验证证据(本地,改动相关范围):

质量判定:diff 干净(6 文件 +222/-14,每行都服务于目的,无无关/空行/lockfile churn);命名达意、注释讲 why、错误处理完整(fail-safe)、无死代码/魔法值滥用;外部提交从严清单逐项达标。不需要对齐重构。

非阻塞跟进项(供参考,不扩大本 PR):dsh/opencode/kimi 目前也都不传原生 call id——其中 opencode-v2 宿主 API 的 tctx 只有 sessionID、根本不暴露 per-call id,dsh 也没传。它们是否同样触发这次驱逐,取决于各自宿主的「是否重放裸名 compress + #1567 range 兜底是否已覆盖」,需要按仓库纪律拿各自的流量证据确认。若确认受影响,属同一机制家族,应按 owner 政策归并跟踪;但不建议塞进这个 Pi-scoped 的 PR。另:reconcile 在每个 plugin 请求都会扫一遍全部 messages(即使没有任何 plugin_ 块),可在建完 blocks 后加一行「无 plugin_ 块即 return 0」短路长会话的每轮开销——纯微优化,可选。

收工判定:方向正确、根因修在源头、测试绿、typecheck 干净、base 最新、锁安全、双模式推理到位、diff 整洁、质量达标。可合并——因涉及 wire/identity 变更,按 §7.4 走人工合并(不可 auto-merge),请人工 sign-off 后合并 #3592 。

@ranxianglei ranxianglei mentioned this pull request Oct 17, 2026
6 tasks
@WSXYT

WSXYT commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

(ai: openai/gpt-6.1-sol)

Added two follow-up commits on this existing PR branch, without replacing the original contribution: ce447b97 and 9254bad7.

The legacy reconciliation originally required uniqueness among all calls with the same range before validating their receipts. In the reporter's saved real incoming traffic, 13 range/block cases each had 2–12 same-range attempts, but exactly one successful receipt naming the specific block. Failed retries or successful receipts for another block therefore prevented an otherwise unambiguous migration.

The fix filters candidates by the existing exact successful block-receipt evidence before checking uniqueness. Duplicate valid successful receipts and multiple active blocks for the same range still fail closed. Native call-ID propagation is unchanged; no kernel/config/schema/wire-argument changes.

The new regression failed red on the original implementation (plugin_* instead of the evidenced host call ID), and now passes. It checks failed/foreign receipts before migration and two later retries; the active carrier remains byte-identical. The assertion deliberately permits the existing, intentional cleanup of old failed orphan pairs rather than changing that policy.

Validation on the PR checkout: 89/89 focused tests, 0 failed/skipped; TypeScript and build both exit 0. These are targeted checks, not a full-suite verdict. The previous head's Windows Node 22 shard 1 had six cancelledByParent cases in the Google lane-matrix suite (Promise resolution is still pending but the event loop has already resolved), not a carrier assertion failure. Rerun was denied due to repository permissions; this additive push retriggers CI. Unrelated Google tests were not changed. Please retain human review; no merge/release requested.

Deployment/acceptance remains separate: the repaired standard local installation is hash-verified, and a local resource-filter mismatch that had kept its extension disabled has been corrected using Pi's resolver. The actual affected session must still reload normally and produce sustained real routed traffic before claiming the low-cache symptom is resolved. Private captures/session contents are not attached.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

复核了你新增的两个 additive commit(ce447b97 fix + 9254bad7 test;原 commit 5c0e6bd 保留、未替换)。拉了新 head、通读逻辑 diff、本地重跑改动相关的两个测试文件 + typecheck,并额外验证了它与当前 master 的整合。结论如下。

这次改了什么(增量):存量迁移 reconcilePluginFoldCallIds 原先要求「同一 range 的调用总数恰为 1」才去校验回执——但你保存的真实流量里同一 range 常有 2–12 次重试,这个过严门槛把本该迁移的块全挡了。改为先按「有效成功回执且点名本 blockId」过滤候选,再要求恰好一个幸存者;失败重试(无 [Compressed … 回执)、指向别的块的 success、同块两个有效成功回执、同 range 多个 active 块,一律 fail-closed。

八问:

  1. 是什么问题:上一版迁移对「重试多的 range」过度保守,带失败重试的真实会话无法把旧合成块绑回真实载体,前缀照样丢。
  2. 什么原因:旧判据把「唯一性」建在全部尝试上,而不是建在有效证据上;重试噪声把唯一的真主淹没了。
  3. 影响范围:仅存量 Responses/plugin 会话中「折叠过 + 同 range 有 ≥1 次失败重试」的场景;后果是缓存命中(性能/成本),非数据丢失、非不可用。
  4. 怎么修复:把唯一性判定从「所有尝试」收窄到「持有本块确切成功回执的尝试」——按证据归因,不是放宽成模糊匹配;多重证据依旧 fail-closed。方向正确。
  5. 回归风险:逐项过——新逻辑只在「恰好一个有效成功回执点名本块」时绑定,而一个 block 的 summary 只由一次成功执行产生,故该情形下绑定必然正确;≥2 有效回执 / 多块 / 无回执都跳过(同等或更保守);只改归属元数据、幂等、在 withSessionLock 内、位于 kernel 管线之前。未发现回归。测试侧:原「歧义」用例从「一成一败」改成「两成」,正好钉住新的「多有效回执 fail-closed」路径;新增回归用例复现你的真实场景(1 次失败在前 + 1 个外块 success + 1 个真 success),断言绑到真主、且首个 pair 之前的前缀字节不变。
  6. 是否破坏缓存:不破坏,仍是恢复前缀稳定;markDirty 正确失效修订缓存。
  7. 设计方向:无转向,只是把既有迁移的证据门槛调准。
  8. 新配置:无。

验证(本地,新 head):两个改动测试文件 76/76 通过(比上轮 +1 即那条新回归),typecheck exit 0。red→green 我核对了机制(旧码 size≠1→skip→compressCallId 停在 plugin_*,断言必红),与你所述一致;为省共享资源未单独回退复跑,全量回归按 §7.2 交远程 CI。

⚠️ 关键挡路项:base 已落后。当前 PR head 仍基于旧 master b10c525,master 已推进到 21a860f(落后 93 commit),期间上游改过 src/plugin.ts(§7.2 点名的热文件)。我已在本地把 PR rebase 到当前 master tip 实测:3 个 commit 全部干净应用、零冲突,rebase 后两个测试文件仍 76/76 绿、typecheck exit 0 —— 即上游那批改动与本 PR 无语义冲突。但远端分支尚未 rebase,CI 现在测的是旧 base 树。建议合并前先推一次 rebase,让 CI 验的是实际合入的树。我没擅自 force-push 到你的在途分支(那会改写你刚引用的 SHA);你点头或 owner 拍板我就推,或你自己 rebase 一下也行。

关于 CI 那个 Windows Node22 shard 的 cancelledByParent:那是 Google lane-matrix 里 node:test 的事件循环取消竞态(Promise resolution is still pending but the event loop has already resolved),属基建 flake,不是 carrier 断言;本 diff 只碰 plugin.ts 迁移 + identity 测试,完全不涉及 Google lane,与之无关。本次 additive push 已会重触发 CI,以重触发后的结果为权威;若重触发后仍在同一 shard 复现,再单独排查/立项。

收工判定:代码正确、测试有牙、质量达标,上轮结论维持。当前唯一挡路项是 base 落后需 rebase(我已本地验证可干净 rebase 且保持绿)。rebase 上去 + 人工 sign-off(wire/identity 变更不可 auto-merge)后即可合并。部署/验收(真实受影响会话 reload + 持续真实路由流量)仍独立于本 PR,按你说的另行确认。

@ranxianglei

Copy link
Copy Markdown
Owner

(ai: zhipuai-lb/glm-5.3)

已并入 #4590。关单收尾;误判请 reopen。

@ranxianglei ranxianglei changed the title fix: preserve native Pi compress carrier identity and cached prefix [PR] fix: preserve native Pi compress carrier identity and cached prefix Oct 24, 2026
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.

[Bug][Pi] lost native compress call identity evicts active carriers and resurrects early summary anchors

2 participants