Skip to content

fix(#2241): continuity-aware dsh persona anchor — model switches migrate the anchor instead of forking the main lane - #2247

Merged
ranxianglei merged 5 commits into
masterfrom
2026-10-05_dsh-persona-continuity
Oct 6, 2026
Merged

ranxianglei merged 5 commits into
masterfrom
2026-10-05_dsh-persona-continuity

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #2241 (root fix; complements the routing-side stopgap #2244).

Problem

db21309 keyed dsh traffic by id + system-text hash with kernel first-seen
anchor semantics, assuming "dsh composes its main system once per session,
so main turns never fork mid-conversation". /model breaks the assumption:
dsh rebuilds the main system per model, so every model switch forked the
MAIN lane onto <id>|sub:<fp>. The host only stamps the bare id, so
compression kept hitting the frozen husk on the raw key — #2241's incident:
266,966 pendingT1 tokens, 0 folds for 38 minutes, 9/9 compress rejections.

Fix — prefix-affinity identity semantics at the anchor decision

The fork trigger changes from "system text changed" to "system text changed
AND the incoming history does not continue the raw key's chain":

  • src/prefix-affinity.ts — new chainContinues(messages, sessionId):
    the stored chain is a byte-exact head-anchored prefix of the incoming
    list (equal-depth byte-exact replay counts too). Cryptographic prefix
    equality is the discriminator; no new state.
  • src/persona-anchor.ts (new) — resolvePersonaKey: first-seen claims
    the raw key; same system matches; system change + continuing history
    migrates the anchor (same conversation — blocks/refs/ledger stay);
    system change + discontinuous history (the auto-review flattened blob)
    forks onto <id>|sub:<fp> exactly as before. Empty system stays
    non-anchoring (kernel semantics).
  • src/server.ts — both dsh persona call sites (anthropic + openai) route
    through it; one info log per migration.
  • Fork id format <rawId>|sub:<sha256(system)[0..16]> unchanged → [PR] fix: follow the live persona fork when a host-stamped dsh id hits a frozen anchor (#2241) #2244
    personaForkTarget routing stays compatible (defense in depth for
    restart races).

Restart race self-heals: chains are permanent (#1724), so even if a review
claims the raw anchor after a proxy restart, the next main turn's
continuity check runs against the persisted chain and migrates the anchor
back — strictly better than the old first-seen-only rule.

Behavior matrix

scenario before after
/model switch mid-conversation main lane forks, compression lost (#2241) anchor migrates, raw key keeps blocks/refs
auto-review blob forks <id>|sub:<fp>, stable across calls unchanged
title/side requests verbatim (#2156/#388) unchanged
system-less aux calls ride main session unchanged
non-dsh agents verbatim keying (#1106 allowlist) unchanged
diverged tail + system change forks still forks (conservative)

Tests

Local: targeted suites 67/67; typecheck clean. Full suite via CI.

…continues the raw key's chain keeps the raw key

The dsh persona fingerprint (db21309) forked on ANY system-text change
under the id, on the assumption that dsh composes its main system once
per session. /model breaks that assumption: dsh rebuilds the main system
per model, so every model switch forked the MAIN lane off the raw key
while the host kept stamping the bare id — compression then hit the
frozen husk forever (#2241: 266,966 pendingT1 tokens, 0 folds).

Swap the fork trigger for prefix-affinity identity semantics
(#1106/#1148: content is identity, system drift is evolution):

- src/prefix-affinity.ts: chainContinues(messages, sessionId) — the
  stored chain is a byte-exact head-anchored prefix of the incoming
  list (equal-depth byte-exact replay counts too).
- src/persona-anchor.ts (new): resolvePersonaKey — first-seen claims,
  same-system matches, and on system change: history continues the raw
  key's chain → MIGRATE the anchor (compression state stays); history
  discontinuous (auto-review flattened blob) → fork onto
  <id>|sub:<fp> exactly as before. Empty system stays non-anchoring.
  Fork id format unchanged (#2244 personaForkTarget compatible).
- src/server.ts: both dsh persona call sites (anthropic + openai) now
  route through dshPersonaNamespace; migrations log one info line.
- Restart race self-heals: chains are permanent (#1724), so even if a
  review claims the raw anchor after a restart, the next main turn's
  continuity check runs against the persisted chain and migrates back.

Tests: dsh-persona-continuity (unit state machine + openai e2e: switch/
switch-back/review-forks/diverged-forks; anthropic e2e); fingerprint
suite unchanged (review isolation, side-request verbatim, allowlist);
side-lane-invariants (D) now uses the realistic flattened review blob
(a history-replaying different-system request is the same conversation
evolving post-fix, not a persona fork).
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-05_dsh-persona-continuity (16644fd)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2336-pr.2247.28c2976

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-pr2247.tgz
npm install -g package

Install instructions are refreshed on each push.

ranxianglei and others added 2 commits October 6, 2026 15:57
The new e2e rigs shared the ambient prefix-affinity snapshot: startServer's
hydratePrefixAffinity() imports the chains the PREVIOUS local run of this
file persisted (deeper dshc-* entries), so chainContinues rejects the
shorter replay (msgs.length < entry.depth) and forks instead of migrating —
green on a clean machine/CI, deterministic red on any second run. Sandbox
XDG_* per file and per rig, matching tests/side-lane-invariants.test.ts.
No behavior change — review fix for PR #2247.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR review 完成。方向正确、机制成立,已在 PR 分支上直接修掉两个 review 发现的小问题(带标注 commit),当前 head c74175d17 的 CI 已基本全绿(详见末尾)。

结构化七问

1. 是什么问题
dsh /model 切换会按模型重建主 system text,旧指纹规则(db21309,KDD#9)把「system 变了」当成新 persona,把主车道fork 到 <id>|sub:<fp>;而宿主工具调用只盖裸 id,压缩永远打在冻结的 7 条消息空壳上(#2241 事故:266,966 pendingT1、38 分钟 0 fold、9/9 拒绝)。

2. 什么原因导致的
根因不是「锚点丢了」,而是判别器选错了:first-seen 锚点语义假设「dsh 每会话只组一次主 system」,/model 打破了该假设。真正的区分维度是「是不是同一个对话」——而本仓库已有现成的同一性判据(#1106/#1148:内容即身份,prefix-affinity 链)。旧方案把表面信号(system 文本变化)当根本判据,属于把症状层当机制层修。

3. 怎么修复的
把 fork 触发条件从「system 变了」换成「system 变了 且 incoming 历史不延续裸 key 的链」。chainContinues 用密码学前缀相等(头锚定、等深逐字节重放也算延续)对持久化身份链做判定:延续 → 迁移锚点(blocks/refs/ledger 全留在裸 key);不延续(auto-review 扁平 blob)→ 照旧 fork。两个 dsh 调用点统一走 resolvePersonaKey;fork id 格式不变,#2244 的路由兼容保留为纵深防御。关键设计点核实过:normalizeAffinityMessages 剥掉的是 leading system/developer run,所以链对「头部 system 文本变化」天然免疫——这正是 model switch 能通过而 review blob 不能通过的原因。key 解析发生在 identified-chain note 之前(server.ts ~2270 vs ~2531),读到的永远是请求前的链,无自引用。

4. 是否有回归问题
逐项审过既有行为:非 dsh 车道字节级不变(allowlist 未动,仅 x-bili-plugin: dsh);同 system / first-seen / 空 system(side request #2156)路径不变;diverged 尾部 + system 变化仍 fork(保守,已文档化);wire 格式、持久化格式、config 面均无变化;kernel 未触碰(kernel-guard 绿)。唯一有意行为变更就是矩阵第一行(fork→migrate),PR 已披露。存量空壳会话升级后自愈:dsh 全量重放历史,下一主轮次对持久化的 7 条 husk 链做 continuity 判定通过 → 锚点迁回裸 key,压缩恢复——这点 PR 正文没明说,我推演确认成立。重启竞态自愈同样成立(链跨重启持久化,#499/#1724);残留窗口(review 抢先认领后、下一主轮次前)仅限裸 key 短暂被 blob 污染(refs 不复用、自清理),#2244 从路由侧兜底。

review 中发现并直接在分支上修的两个小问题:

  • TS2532 typecheck 失败(tests/dsh-persona-continuity.test.ts:205,peekSession() 返回可空)。我定位后作者已并行推送了完全相同的修复(c71296d1e),我丢弃了自己的重复 commit——此修复归功于原提交,我只做了独立复现验证。
  • 新 e2e rig 非幂等(我的发现,已修,commit c74175d17):新测试文件缺少 XDG state-dir 隔离(仓库既有模式,~10 个测试文件都有,sibling 的 side-lane-invariants 也有)。后果:rig 启动时 hydratePrefixAffinity() 会导入上一次本地运行自己写下的更深 dshc-* 链 → msgs.length < entry.depth → chainContinues 拒绝 → fork 而非 migrate → 第二次本地运行确定性红(实测复现:首跑 3/3 绿,二跑 openai e2e 1 !== 2;CI 因容器全新永远绿,所以 CI 抓不到)。这正违反 AGENTS.md §7.2「测试必须确定性,不许环境运气」。修法:按 house pattern 给文件级 + rig 级各一个 mkdtemp XDG 沙箱。验证:连续双跑 14/14 ×2 全绿,typecheck 干净。

5. 是否破坏缓存
不涉及。无缓存/重试/增量游标/去重键变更;上游 prefix cache 在 model switch 时的 miss 行为与之前一致(本来就 miss),压缩账本反而跨切换更稳定。

6. 是否改了设计方向
没有转向。是把既有的「内容即身份」语义(#1106/#1148,匿名 affinity 已在用)应用到 persona 锚点决策点。结构性变化:dsh 流量改走 bili 自己的进程级锚点表(persona-anchor.ts),不再写 kernel 的 subagentNamespace 锚点表;kernel 表仅剩 codex/claude-over-Responses(instructionsFingerprintApplies)一条调用方,无双表分叉风险。

7. 是否引入了新的配置
无。无新 env/config/默认值。

验证状态(head c74175d)

  • 本地:定向三套件(dsh-persona-continuity / dsh-persona-fingerprint / side-lane-invariants)14/14,连续两跑全绿;typecheck 干净。
  • CI:ubuntu-22 矩阵格整格绿(typecheck + kernel suite + 全量 npm test + build);其余 3 格 typecheck/kernel 已过、全量套件在跑;ci-image、e2e-native-dsh(真实 dsh 客户端)、e2e-dsh-plugin-lane、e2e-local-registry、pi/opencode/codex 各 lane、kernel/version/lane/docs guard 全部 success。

顺带提醒(不阻塞)

#2244 与本 PR 都写了 Closes #2241,先合谁由你定。我的判断:先合本 PR(根治);#2244 在 #2247 之后价值收窄到「live 车道确实在 |sub: 且宿主盖父 id」的重启竞态窄窗(dsh review 无工具调用,实际几乎不触发),作为 lane-gated 纵深防御留着无害,合并或关闭均可。

收工判定:可合(待剩余 3 个矩阵格套件跑完;按目前证据——含一格全量套件已绿——预期全绿)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 补充:head c74175d17 的 CI 主 run 已整条 success——4/4 矩阵格(ubuntu/windows × node 22/24)全量 typecheck + kernel suite + npm test + build 全绿,加上此前已绿的 ci-image、e2e-native-dsh 等全部 lane。无回归信号,可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

新提交审核了吗

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 审了。刚重新 fetch 确认:分支 head 仍是我上一轮审核的 c74175d17,没有更新的提交(base 也还是 c23f365c7,未落后)。

审核期间新增的两个 commit 都覆盖在结论里:

  • c71296d1e(typecheck 修复):已审 diff、本地 typecheck + 全量 CI 验证;
  • c74175d17(e2e rig XDG 隔离修复):我本人提交并验证(双跑 14/14 ×2 + typecheck 干净)。

c74175d17 上的 CI 整条 success(4/4 矩阵格含全量 npm test + e2e 各 lane)。结论不变:可合。

…conversation key

Adds tests/e2e/e2e-native-dsh-persona.test.ts to the real-dsh native lane
(runs on every PR): two turns of one real dsh headless session (--session-id
adoption) with the profile default model flipped between them, so dsh
recomposes its main system per model — the exact incident trigger. Asserts
one stable conversation id on the wire, a real model switch, a continued
history, and exactly ONE persisted bili session under the RAW key (no
|sub: sibling, requests >= 2, refs accumulated). Verified both directions
locally: pre-fix master fails with the |sub: fork, the fix build passes.

The lane's dsh install bumps 0.1.5-rc.3 -> 0.2.0-rc.2 for --session-id; the
existing base-lane suite passes against 0.2.0-rc.2 (verified locally).
…eview scenario behind a verified blocker gate

Scenario A now asserts dsh's title-generation side request rides the same
conversation id without stealing the raw key or minting a persisted |sub:.

Scenario B (auto-review still forks) is written and kept ready, but gated
behind ACP_TEST_E2E_DSH_AUTO_REVIEW: verified against dsh 0.2.0-rc.2 that
the review gate arms only when permissionPresets.current(session) ===
"auto", headless sessions carry no preset, Auto has no default/env/CLI
surface (web-UI /permission auto only), and the settings.yaml legacy import
does not settle within a one-shot run. The review fork shape stays pinned
by the harness e2e in dsh-persona-continuity.test.ts until dsh grows a
headless preset surface.
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.74-master.ad514ed (rolling master tag).

npm install -g billion-context@0.0.74-master.ad514ed  # 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.

yingzhongshuoqi pushed a commit to yingzhongshuoqi/billion-context that referenced this pull request Oct 6, 2026
…he codex / claude-over-Responses instructions lanes (ranxianglei#2250)

An instructions drift mid-conversation (model switch, AGENTS.md edit, -c
override, codex upgrade reassembly) forked the main lane off its raw key
onto |sub:<fp> — zero compression state, refs re-minted (ranxianglei#2241 exposure
shape). The drift now MIGRATES the anchor when the replayed input continues
the raw key's prefix-affinity chain; a genuinely fresh task reusing the id
(ranxianglei#150) still forks. Kernel subagentNamespace swap at the responses call
site; dsh lanes unchanged (already ranxianglei#2247); fork id format identical.
ranxianglei pushed a commit that referenced this pull request Oct 10, 2026
…semantics (#2601)

The fixture's switch turn was a byte-exact extension of the anchor turn, so
on master (with #2247's continuity-aware migration, which postdates this
branch's base) it MIGRATES the anchor instead of forking — all five routing
tests died in setup. The field incident (#2601) proves the real switch breaks
the chain (history rewrite or cross-lane normalization), so the switch turn
now drifts the first historical message to reproduce a genuine fork. Adds one
test pinning the boundary: a byte-stable same-lane switch migrates (no fork),
and bare-id native calls stay on the raw key with nothing to reroute to.
@ranxianglei
ranxianglei deleted the 2026-10-05_dsh-persona-continuity branch October 15, 2026 05:32
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