Skip to content

fix: fold-reconcile survives cross-model re-serialization churn (pi model switch) — assistant-turn key + image-capability variant passes - #2644

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-10_model-switch-reconcile
Oct 10, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-10-10_model-switch-reconcile

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #2636

What breaks today

A host replaying one conversation onto a different model re-serializes every thinking-bearing assistant turn (pi's transformMessages: isSameModel === false inlines thinking as plain text parts; same-model replay keeps the structured reasoning channel) and replaces image parts with fixed placeholder lines when the target is text-only. The kernel derives message ids over the resulting core shapes, so every covered assistant/user core goes missing at once — on the reporting session 211/211 covered ids missed, the originals replayed unfolded (context blow-up), and after 3 passes the drift escalated to compression substrate appears destroyed (fold state effectively lost).

Why the existing passes don't catch it

The fix — two exact-match passes on top of the existing ladder

Pass 3 — assistant-turn key (#2636). The anchor now carries a turn key: the normalized FULL turn text (reasoning + seam + reply), whichever wire form the turn was replayed in. pi joins the parts with "", other hosts with "\n" — pairs carry both seam forms (p/p2) and the matcher probes both; equality only when normalization collapses the difference. Matches both directions:

  • pair→merged: both member ids claim the single merged witness (claims: Map<old, new[]>);
  • merged→pair: one old id claims both new cores.
  • Duplicated turns (identical key, >1 witness) stay unmatched — never fuzzy.
  • thinking-only turns (reasoning core with no text part) register their isolated key on both sides, so the contentType text↔reasoning flip re-anchors too.

Pass 4 — image-capability variants (#2636). A covered user text flips between text and text + "\n" + (image omitted: model does not support images) across the switch (pi's NON_VISION_USER_IMAGE_PLACEHOLDER, byte-stable). The pass tries one trailing placeholder line stripped/appended against the anchor's normalized identity — unique witness, or k-th family pairing when K anchors share one key with K candidates (identical "Attached image" wrappers). Tool-role messages keep going through pass 1 (toolCallId, no length guard).

Plumbing. claims is now Map<old, new[]> (1→N and N→1); rewriteBlocks splices targets in, dedupes, and REMOVES the second member of an N→1 collapse so a block never lists an id twice. Turn keys are seeded lazily (#2334: steady state pays zero extra normalizations — perf suite pins this) and backfilled once for anchors persisted before this change (bare assistant cores only; user/tool anchors never carry a turn key, so their p === undefined steady shape must not re-trigger a rebuild every pass).

Evidence

  • Offline sim on the reporting session's real jsonl (692 messages, qwen3.8-27b ↔ glm-5.3, both replay directions mixed — the harness replays the same corpus through pi's real convertMessages for each model and diffs the kernel cores): unmatched 347 → 16. All 16 residuals are pi's dropped "Attached image(s) from tool result:" wrappers — honestly absent from the new wire (pi synthesizes them only for vision targets), kept unmatched by design. 377 assistant turns re-anchored via the turn key, 16 tool results via toolCallId.
  • Unit tests: new #2636 describes in tests/fold-reconcile.test.ts (10 plan-level cases + 2 integration cases incl. the no-escalation regression). tests/fold-reconcile.test.ts + tests/fold-reconcile-positional.test.ts = 80/80.
  • Adjacent suites (all fold-reconcile consumers): issue2202-coverage-accrual, cache-ledger-prompt-switch, codex-local-compaction, issue2360-compress-receipt, issue2432-breaker-awareness, fold-reconcile-perf = 29/29.
  • Full suite: 32 failures on this branch — byte-identical set (A/B via worktree) to clean origin/master (2d31e42): all are the known sandbox-network class in tests/dsh-native.test.ts / tests/claude-native.test.ts ("manifest registration failed (fetch failed)", 15s timeouts). Zero branch-only failures.
  • typecheck clean, npm run build clean (new code present in dist).

Non-goals / boundaries

…odel switch) — assistant-turn key + image-capability variant passes (#2636)

A host replaying one conversation onto a different model re-serializes every
thinking-bearing assistant turn (pi's transformMessages: isSameModel=false
inlines thinking as plain text) and swaps image parts for fixed placeholder
lines on text-only targets. The kernel mints ids over the changed shapes, so
every covered assistant/user core goes missing at once: 211/211 on the
reporting session — the originals replayed unfolded and the fold substrate
escalated to destroyed (#2636).

Pass 0 positional identity cannot pair these (a pair↔merged shape change
misaligns positions at the first thinking turn), toolCallId only saves tool
results (16 there). Two new exact-match passes close the gap:

- Pass 3 — assistant-turn key: the anchor carries the normalized FULL turn
  text (reasoning + seam + reply; both seam forms, pi joins with ""), so
  pair→merged claims both member ids onto the one merged witness and
  merged→pair claims both new cores. Duplicated turns stay unmatched.
- Pass 4 — image-capability variants: a trailing NON_VISION placeholder
  line stripped/appended, exact normalized identity, unique witness or
  k-th family pairing.

Claims are now Map<old, new[]> (1→N and N→1); rewriteBlocks splices targets
in and removes the second member of an N→1 collapse so a block never lists
an id twice. Turn keys are seeded lazily (#2334: steady state pays zero)
and backfilled once for pre-existing anchors (bare assistant cores only —
user/tool anchors never carry one).

Offline sim on the reporting session's real jsonl (692 messages, qwen→glm
both directions mixed): 347 unmatched → 16, all 16 = pi's dropped
"Attached image" wrappers (honestly absent from the new wire). 377 turns
re-anchored via the turn key, 16 tool results via toolCallId.

Fixes #2636
@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-10_model-switch-reconcile (adbd9ae)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2861-pr.2644.891d603

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-pr2644.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] 🏷 Review 结论(结构化八问):可合 —— 方向对、根因分层正确、实现正确且防护到位、测试有牙、既有 reconcile 路径零回归、稳态成本不变、持久化向后兼容。合并前需 rebase(当前 mergeStateStatus=behind)+ 等 CI 全量绿。未 auto-merge:触碰 wire/message-shape 协调逻辑 + 持久化 anchor 格式,属 §7.4 human tier。

① 是什么问题:pi 会话切模型后 fold-reconcile 把整段已压缩历史判成「丢失」并升级成 substrate-destroyed。#2636 的真实触发不是模型能力变化本身,而是 pi transformMessages() 跨模型重序列化把 assistant turn 的形状改了(thinking→内联文本 part、redacted/thoughtSignature 丢弃、图片在 text-only 目标下→占位符)。

② 什么原因导致的:内核 message id = role|contentType|toolName|toolCallId|text 的内容哈希,形状一变 id 就变。同模型下是「reasoning core + text core」两核,跨模型下 pi 用 "" join 成一个 merged text core。既有三道 pass 全落空:positional 在第一个形状断裂处停、toolCallId 只救 tool result、normalizedIdentity 含 contentType+全文 → 211/211 covered id 判 missing → 误升级。根因在 bili 协调层(疏),不在 host 侧(host 重序列化是合法的单模型 wire 约束,不可也不应改)。分层判断正确。

③ 影响范围与严重程度:仅影响「同一 pi session 内切换了不同模型」的场景;单模型长会话完全不受影响(稳态 early-return 不触碰新路径)。严重度:特定条件下功能受损——该场景下旧会话的压缩历史被误判销毁、触发 substrate-destroyed 降级,但不会静默丢数据(有 guard,拿不准就不认领)。

④ 怎么修复的:在既有 ladder 之上加两道精确匹配 pass(绝不 fuzzy、不动内核):

  • Pass 3 assistant-turn key:anchor 携带归一化后的整轮文本哈希(p=seamed R\nT / p2=seamless RT / x=sibling core id);候选侧同时登记 single(bare text core / thinking-only reasoning core)与 pair(reasoning+text 双核,两种 seam 形式);仅当 singles+pairs===1 才认领——重复 turn 保持 unmatched。覆盖 pair→merged、merged→pair、thinking-only flip 三个方向。
  • Pass 4 image-capability variant:新增 identityWithText(布局与 normalizedIdentity 逐字节一致),对无 toolCallId 的 user text core 做 strip/append 一行 USER_IMAGE_PLACEHOLDER 的双向变体;唯一 witness 或 k-th family 配对(计数相等才配)。
  • claims 由 Map<string,string> 升级为 Map<string,string[]>(支持 1→N 与 N→1);rewriteBlocks splice+去重+移除 N→1 折叠的第二成员,保证一个 block 永不二次列同一 id。

⑤ 是否有回归问题:无。逐项核查既有行为:

  • identityWithText 哈希布局与 normalizedIdentity(src/fold-reconcile.ts:241)逐字节一致 → Pass 4 key 正确碰撞 ✓
  • turn-key seam 逻辑(pair→merged / merged→pair / thinking-only)逐一 trace 一致,歧义 guard 成立 ✓
  • 长度 guard(single +64>=b、pair |Δ|<=max(256,b>>2)、image len+PH+64>=b)齐全,防短文本假命中 ✓
  • 外部消费者:ReconciliationPlan/claims map 是模块私有,唯一导出 reconcileFoldCoverage() 返回的是加法字段(byTurn/byImage);调用方(server.ts / prepare-{openai,responses,google,anthropic}.ts)无人解构具体字段 → 无破坏性变更 ✓
  • perf: 长会话每次请求重复计算并丢弃超上限的压缩锚点 #2334 稳态零成本 pin 保住:missing.length===0 在 src/fold-reconcile.ts:412 提前 return,早于 candidates 构建;pass 3/4 均 gated on missingMiddle>0 && candidates>0,稳态零额外归一化 ✓
  • FoldAnchor 的 p/p2/x 为可选加法字段,旧 session 缺则 backfill 一次、再停(p 变 defined);旧版忽略未知字段 → 双向兼容 ✓
  • message-id 不可复用契约不受影响(claims 只改写 fold-block 里的 id 引用,从不改 message id)✓

⑥ 是否破坏缓存:不涉及。缓存账本(cache-ledger-prompt-switch 29/29 通过)不受影响;改动只在协调层重写 fold-block 的 id 引用列表,不触碰 prompt/wire 字节。

⑦ 是否改了设计方向:不涉及架构转向。是在既有「多 pass 精确协调」框架内新增两个 pass,方向与 #2334/#2390/#2476 一脉相承,边界清晰(host churn 明确划出 scope)。

⑧ 是否引入了新的配置:不涉及。无新 env/config/默认值。USER_IMAGE_PLACEHOLDER 是内部常量(镜像 pi 常量,注释标注 kept-in-sync)。


测试有效性(④):高质、有牙。10 个 plan-level case 断言精确 claim 映射(如 assert.deepEqual(plan.claims.get("r1"), ["m1"]),非常真断言),覆盖双方向、never-fuzzy 歧义 guard、两类长度 guard、k-th 配对;2 个 integration case 走真实 reconcileFoldCoverage:pair→merged 折叠去重到 [m1,m2,m3] 且 anchor 带 p;churn 不再升级 substrate-destroyed(foldDriftStreak undefined、零 error log)——直接钉住 #2636 症状。非 tautology。

我的独立定向验证:两棵 worktree 均无 node_modules(全量 npm ci 属 CI 职责,本地不预跑)。改为零依赖 standalone 脚本跑纯函数真实场景(verbatim 函数体,node:crypto only):8/8 PASS —— pi seamless 无尾换行 cand==p2 且 !=p(seamed)、尾换行 cand==p&p2 双中、显式 \n seam cand==p、image append 方向 stripped==anchor.n、image strip 方向 appended==anchor.n、不同 turn key 不相撞(never-fuzzy 前提)。独立佐证了作者离线 sim 的核心碰撞机制。

两处非阻塞小观察(不建议为此推 commit,PR 正 behind 待 rebase):

  1. Pass 3 里局部 const byPos = new Map<number,cand>() 与外层 plan.byPos 计数器同名,纯命名易混(block 作用域、无 shadow、非 bug)。
  2. USER_IMAGE_PLACEHOLDER 硬编码镜像 pi 常量(pi 若改名会失同步;注释已声明 'never changed since introduction')。

判定:可合。请 rebase 到最新 master 后等 CI 全量绿再人工合并。

@ranxianglei
ranxianglei merged commit fefb16f into master Oct 10, 2026
24 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.229-master.fefb16f (rolling master tag).

npm install -g billion-context@0.0.229-master.fefb16f  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

ranxianglei added a commit that referenced this pull request Oct 10, 2026
@ranxianglei
ranxianglei deleted the 2026-10-10_model-switch-reconcile branch October 15, 2026 05:33
ranxianglei pushed a commit that referenced this pull request Oct 15, 2026
A host metadata-table refresh can re-serialize every past thinking turn of
one session in a different wire form (reasoning_content field vs reasoning
spliced into content), minting new ids for unchanged content. Fold re-anchor
then reports 'covered id(s) missing with no anchor match' on every request:
the turn-key pass only understood bare-concat merges, and pre-#2644 anchors
lack p/x entirely — while the wire-side backfill can never fire because the
flip puts the old id off the wire on the very pass that also rolls the order
backbone forward without it.

- fold-reconcile: extractThinkingMerge mirrors the kernel's demoted-thinking
  FORMS so merged-form cores register the same turn keys as split-form
  anchors (turnContexts + Pass 3 candidate side); ReconcileOptions.storedMessages
  rebuilds eligible legacy anchors from stored decompositions BEFORE the plan
  runs, so the flip pass itself claims.
- storage: UnifiedStore.messagesForIds (+seq±1 neighbors for rc-pair
  adjacency), shadow/persist pass-throughs gated on full mode.
- new fold-decomposition-lookup: per-pass capped lookup with a tried-set
  (index absence is stable, index unavailability retries).
- import entry: record/session/ingest carry an optional decompositionLog
  (external converter payload) interned first at ingest; consume-once at
  save keeps the #3133 delta fast path intact; buildRecord never emits it.

No new config surface. Additive optional fields only (ReconcileOptions.
storedMessages, LegacySessionLike.decompositionLog, PersistedSession.
decompositionLog) plus an internal metadata bookkeeping key foldStoredTried.
No kernel/ changes.

Fixes #3301
ranxianglei pushed a commit that referenced this pull request Oct 16, 2026
A host metadata-table refresh can re-serialize every past thinking turn of
one session in a different wire form (reasoning_content field vs reasoning
spliced into content), minting new ids for unchanged content. Fold re-anchor
then reports 'covered id(s) missing with no anchor match' on every request:
the turn-key pass only understood bare-concat merges, and pre-#2644 anchors
lack p/x entirely — while the wire-side backfill can never fire because the
flip puts the old id off the wire on the very pass that also rolls the order
backbone forward without it.

- fold-reconcile: extractThinkingMerge mirrors the kernel's demoted-thinking
  FORMS so merged-form cores register the same turn keys as split-form
  anchors (turnContexts + Pass 3 candidate side); ReconcileOptions.storedMessages
  rebuilds eligible legacy anchors from stored decompositions BEFORE the plan
  runs, so the flip pass itself claims.
- storage: UnifiedStore.messagesForIds (+seq±1 neighbors for rc-pair
  adjacency), shadow/persist pass-throughs gated on full mode.
- new fold-decomposition-lookup: per-pass capped lookup with a tried-set
  (index absence is stable, index unavailability retries).
- import entry: record/session/ingest carry an optional decompositionLog
  (external converter payload) interned first at ingest; consume-once at
  save keeps the #3133 delta fast path intact; buildRecord never emits it.

No new config surface. Additive optional fields only (ReconcileOptions.
storedMessages, LegacySessionLike.decompositionLog, PersistedSession.
decompositionLog) plus an internal metadata bookkeeping key foldStoredTried.
No kernel/ changes.

Fixes #3301
yanhuojiyusheng pushed a commit to yanhuojiyusheng/billion-context that referenced this pull request Oct 24, 2026
…ei#3301)

A host metadata-table refresh can re-serialize every past thinking turn of
one session in a different wire form (reasoning_content field vs reasoning
spliced into content), minting new ids for unchanged content. Fold re-anchor
then reports 'covered id(s) missing with no anchor match' on every request:
the turn-key pass only understood bare-concat merges, and pre-ranxianglei#2644 anchors
lack p/x entirely — while the wire-side backfill can never fire because the
flip puts the old id off the wire on the very pass that also rolls the order
backbone forward without it.

- fold-reconcile: extractThinkingMerge mirrors the kernel's demoted-thinking
  FORMS so merged-form cores register the same turn keys as split-form
  anchors (turnContexts + Pass 3 candidate side); ReconcileOptions.storedMessages
  rebuilds eligible legacy anchors from stored decompositions BEFORE the plan
  runs, so the flip pass itself claims.
- storage: UnifiedStore.messagesForIds (+seq±1 neighbors for rc-pair
  adjacency), shadow/persist pass-throughs gated on full mode.
- new fold-decomposition-lookup: per-pass capped lookup with a tried-set
  (index absence is stable, index unavailability retries).
- import entry: record/session/ingest carry an optional decompositionLog
  (external converter payload) interned first at ingest; consume-once at
  save keeps the ranxianglei#3133 delta fast path intact; buildRecord never emits it.

No new config surface. Additive optional fields only (ReconcileOptions.
storedMessages, LegacySessionLike.decompositionLog, PersistedSession.
decompositionLog) plus an internal metadata bookkeeping key foldStoredTried.
No kernel/ changes.

Fixes ranxianglei#3301

(cherry picked from commit b5756bb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant