Skip to content

Cold start after provider switch loses the session's compression ledger; refold hits local 413 | [Bug][v0.1.197] 冷启动后切换供应商漏载同一会话的压缩账本,重新折叠后触发本地 413 #3399

Closed

Description

@nsdbjh

v0.1.197 Windows / Responses 补充证据:bounded cold load 按 host 查找,丢失同一 native conversation 的压缩连续性

补充到已有 #2960,关联 #2671 defect-1、#2847、#2803;关联 #2960。本地先做了一个保守热补丁,以下把生产证据、隔离复现和推断分开。

版本/运行条件

  • npm billion-context@0.1.197,对应 3aeddc160864ca774e54817559d6984310e2a9ab。
  • Windows npm daemon,Codex 原生稳定 conversation ID,responses 协议,切换 upstream host。
  • 实际运行 storage.unified=shadow,bounded boot 只预热少量状态。legacy 模式隔离控制也复现;full/unified-first 命中路径不作同样故障断言。
  • 当前 master 83197e913a9229b765bc0a144cb777ee49560430 的 loadEnvelope 候选筛选仍相同。

1. 生产启动日志(真实摘录)

2026-10-16T10:02:26.661Z [info] [v=0.1.197] [storage] unified shadow mode ENABLED (storage.unified=shadow or BILI_STORAGE_UNIFIED): every session write is double-ingested into index.db and digest-reconciled [engine: node:sqlite/builtin]
2026-10-16T10:02:29.802Z [info] [v=0.1.197] [persist] indexed 248 state files; warmed 8 sessions within 67108864 decoded bytes; older states load on demand
2026-10-16T10:02:30.127Z [info] [v=0.1.197] acp-proxy v0.1.197 (pid 2772) listening on http://127.0.0.1:8787 [nonessential listener/banner fields omitted]

同一原生 ID 的两个有效 envelope(version 3;以下字段取自 payload,不是 envelope 顶层):

字段 旧 provider-A 快照 新 provider-B 快照
createdAt (UTC) 2026-10-11T23:49:04.580Z 2026-10-16T06:36:11.780Z
savedAt (UTC) 2026-10-16T02:36:48.637Z 2026-10-16T08:28:22.420Z
文件字节 31,148,251 23,744,791
requests 3,697 2
历史 blocks / active blocks 274 / 25 91 / 91
active summary UTF-8 字节 62,099 806,302
compressionCount 276 91
lastInputTokens 72,613 777,767
lastInputTokensSource usage estimate

两份快照有 50 个相同 message ID,但没有完全相同的 block summary。新文件的 91 个块全部 tier-1,创建集中在 06:36:21.523–06:38:05.513Z。这支持“同会话重新建了一套折叠账本”的判断;不会把 estimate 当上游实际计费 token。

413 证据边界:用户截图显示 payload core text alone is ~2949521 bytes、NOT forwarded even for evidence。v0.1.197 的 doomed-probe gate 在正常 preflightCompress 之前,用 coreTextBytes > max(2_000_000, limit * 7.5) 拒绝当前请求。这里 7.5 bytes/token 是启发式,不是真 tokenizer;UI 的 72,613 来自上一笔成功 usage。没有找到该次目标请求的完整可关联服务日志,因此不声称重放了那一条 2,949,521-byte 请求,也不拿其他会话的日志补洞。#2928 处理另一层 guard 恢复问题,本补丁不改变 guard 或其拒绝契约。

2. 已确认的代码路径

src/persist.ts:

const expected = path.basename(relPathFor(id, meta?.protocol, meta?.upstreamOrigin));
const candidates = this.diskFiles.filter(file => fileNameMatchesId(path.basename(file), id, meta?.protocol, meta?.upstreamOrigin)
    || (!meta?.protocol && path.basename(file).endsWith(`_${expected}`)));
  • namespaced 文件名是 <protocol>/<host>_<sha256(id)[:24]>.json。
  • 请求已有 meta.protocol,所以无法命中其他 host 的有效同-ID 文件。
  • bounded boot 建立 diskFiles,不预热所有 Session,也不为所有冷文件填 kernel discovered-path map。
  • native id === label 不会保留为 legacy alias。
  • 最后的 kernel loadSync(id, new-route-hint) 仍找不到旧 host 文件,随后 getSession 创建 requests=0 / blocks=0 的会话。
  • 对照:先把旧 Session 放进 resident map 后切供应商,同一对象保留 274 blocks。内存按 id、冷盘查找按 host,行为不一致。

3. 隔离真实快照复现(只操作临时目录副本)

仅复制 provider-A 文件到全新临时目录,new SessionStore,boot({maxSessions: 0, maxBytes: 0}),使用同 native ID + provider-B metadata。不能在同一个写入对象上测试:writeNow 会填充 kernel discovered map,掩盖故障。

补丁前(legacy 和实际 shadow 两种模式均复现):

{"case":"cold new route, only old route snapshot","warmed":0,"newRouteLoad":null,"newRouteRequests":0,"newRouteBlocks":0,"oldRouteRequests":3697,"oldRouteBlocks":274}
{"case":"resident route switch","sameObject":true,"blocks":274,"requests":3697}

补丁后(shadow;生产 bundle 的实际替换方法也跑过同样副本):

2026-10-16T11:57:06.582Z [info] [v=0.1.197] [storage] unified shadow mode ENABLED (storage.unified=shadow or BILI_STORAGE_UNIFIED): every session write is double-ingested into index.db and digest-reconciled [engine: node:sqlite/builtin]
{"case":"cold new route, only old route snapshot","warmed":0,"newRouteLoadFound":true,"newRouteRequests":3697,"newRouteBlocks":274,"oldRouteRequests":3697,"oldRouteBlocks":274}
2026-10-16T11:57:08.576Z [info] [v=0.1.197] [storage] unified shadow mode ENABLED (storage.unified=shadow or BILI_STORAGE_UNIFIED): every session write is double-ingested into index.db and digest-reconciled [engine: node:sqlite/builtin]
{"case":"resident route switch","sameObject":true,"blocks":274,"requests":3697}
installed-bundle method: real snapshot cold reload, old four patches, idempotence and changed-anchor refusal PASS

4. 保守本地补丁与边界

在 exact/current-route 探测失配后、legacy alias/kernel fallback 前,只查找同协议目录下同 ID digest 候选;解码后再次验证 envelope/body ID、持久化 protocol 和 canonical filename。live 优先于 cold;同级多个旧 route 选最新 savedAt,平局按稳定文件顺序。记录 sourceFiles,保留伴随 CCR originals 的查找路径。

补丁源码:

diff --git a/src/persist.ts b/src/persist.ts
index 31864c61..a6267ede 100644
--- a/src/persist.ts
+++ b/src/persist.ts
@@ -875,6 +875,33 @@ export class SessionStore {
                 return envelope;
             }
         }
+        // Provider host is mutable metadata, not conversation identity. A
+        // cold bounded boot has no kernel discovered-path entry for the old
+        // route. Recover only on an exact-route miss; never merge or overwrite
+        // an existing route's branch. Keep protocol/body/filename ownership.
+        if (meta?.protocol) {
+            const protocol = meta.protocol;
+            const suffix = `_${path.basename(relPathFor(id))}`;
+            for (const root of [this.sessionsDir, `${this.sessionsDir}-cold`]) {
+                let recovered: { file: string; envelope: PersistedEnvelope<PersistedSession> } | undefined;
+                const routeFiles = this.diskFiles.filter(file => path.dirname(file) === path.join(root, protocol)
+                    && path.basename(file).replace(/\.fb\.json$/, ".json").endsWith(suffix)).sort();
+                for (const file of routeFiles) {
+                    if (!existsSync(file)) continue;
+                    const envelope = this.readEnvelopeFile(file, id);
+                    if (!envelope) continue;
+                    const savedMeta = envelope.payload.meta ?? envelope.payload;
+                    if (savedMeta.protocol !== meta.protocol
+                        || !fileNameMatchesId(path.basename(file), id, savedMeta.protocol, savedMeta.upstreamOrigin)) continue;
+                    if (!recovered || envelope.savedAt > recovered.envelope.savedAt) recovered = { file, envelope };
+                }
+                if (recovered) {
+                    this.sourceFiles.set(id, recovered.file);
+                    this.log("info", `[persist] restored ${id} from another upstream route (same protocol, exact body id)`);
+                    return recovered.envelope;
+                }
+            }
+        }
         const alias = this.legacyAliases.get(id);
         if (alias) {
             const envelope = this.readEnvelopeFile(alias.file, alias.id);

5. 回归与安装验证

本地 v0.1.197:

node --import ./tests/preload-storage-baseline.mjs --import tsx --test tests/bounded-storage.test.ts tests/storage-ccr.test.ts
npx tsc --noEmit --project tsconfig.json
npm run build
  • 新增 4 个测试组:真正 fresh reader 的跨供应商冷恢复及 messageRefs/block originals/CCR sidecar 精确保留;多 route 最新选择与 exact-route 优先级;协议/body-ID/改名隔离;cold archive 和损坏匹配 fail-closed。
  • 补丁前新增组 3 失败、1 通过;补丁后 targeted 18/18 通过(15 bounded + 3 CCR),typecheck/build 退出 0。未运行全套本地测试,也不把 targeted 当完整 CI verdict。
  • Windows npm dist 仅作 scoped 方法替换;node --check 通过,五补丁可共存、重复执行不改变文件,anchor 漂移会拒绝写入。升级 watcher 已接入第五补丁。
  • 安装使用 NoRestart;安装前后 8787 listener 都是 PID 2772。watcher 自身已重新加载,Bili/Node/app-server 未重启。写入磁盘不等于当前进程已经加载新方法。

测试末尾原文:

✔ ccr: legacy mode keeps the file as the read of record (#2960 ③ control) (8.769862ms)
ℹ tests 18
ℹ suites 0
ℹ pass 18
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 4068.931379

当前 master 复核补充

已在独立 worktree、当前 master 83197e913a9229b765bc0a144cb777ee49560430 应用同一源码修复;npm run build、npx tsc --noEmit --project tsconfig.json 均退出 0,bounded-storage + storage-ccr targeted 18/18 通过。

版本差异已保留:master 的 readEnvelopeFile 已增加损坏状态 quarantine-and-fresh 行为,master 回归对应断言隔离文件保留原始损坏字节;v0.1.197 Windows 热补丁保留旧版 fail-closed 异常契约。没有把一个版本的损坏处理假设强加给另一个版本。

安装后 Windows 核验原文:

PASS: installed bundle syntax; five patch markers; Bili listener PID 2772 unchanged; one reloaded watcher PID 6704

Activity

  1. changed the title [-][Bug][v0.1.197] 冷启动后切换供应商漏载同一会话的压缩账本,重新折叠后触发本地 413[/-] [+]Cold start after provider switch loses the session's compression ledger; refold hits local 413 | [Bug][v0.1.197] 冷启动后切换供应商漏载同一会话的压缩账本,重新折叠后触发本地 413[/+] on Oct 16, 2026
  2. ranxianglei commented on Oct 16, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

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

    查重结论:非完全重复,同家族保留打开。 与以下条目同机制/症状家族,已互相关联:


    ① 独立验证(未采信楼主结论,全部重做)

    1. 版本核对:v0.1.197 tag = 3aeddc160(release PR release v0.1.197 #3130),与楼主声明一致;该 tag 的 loadEnvelope 四段探测结构与当前 master 逐行一致 ✅
    2. hermetic 复现(master ce9bc90a3,三腿全过):同 conversation id 在 responses/<hostA>_<hash>.json 落盘(requests=3697 + fold 块)→ 新 store 实例 + 生产同款 bounded boot(maxSessions=8, maxBytes=64MB)→ loadSync(id, {protocol:"responses", upstreamOrigin:<hostB>}):
      • shadow(当前默认模式)→ 返回 null ⇒ 空会话 ❌
      • legacy(显式)→ 返回 null ⇒ 空会话 ❌
      • 对照腿(origin 不变)→ 正常载回 requests=3697 ✅
        复现成立,机制与楼主分析一致。空会话 ⇒ 宿主全量重发 ⇒ 要么重建第二套折叠账本(楼主现场的 91 块 B 快照),要么裸文本 > max(2MB, limit×7.5) 被本地 doomed-probe 门拒 413(fix: doomed preflight rejection answers 413, not 502 (#2847) #2921 起的语义)。
    3. 触发面时间线(git 核实,这是本案的关键增量):≤v0.1.195 的 boot() 是全量 store.loadAll()——kernel discovered 映射按 body id 索引全部文件,跨路由冷加载能自愈([Evidence] pi 车道切供应商(同 modelId)后 fold 不随会话迁移:裸上下文 2.23MB(≥298K est tokens)被 preflight 10 秒拒 3 次;账面 ≤157K 与裸水位背离 10.5h #2847 floor 3「文件存在后启动的新实例会被 boot walk 自愈」只在那个时代成立);c3b212b(v0.1.196 起)改 bounded boot,discovered 不再于启动填充 ⇒ 「切供应商 + 重启」从特殊拓扑问题变成确定性触发。楼主跑 v0.1.197 正落在这个窗口内。
    4. 代码级因果链(master 行号):loadEnvelope(src/persist.ts:1083-1122)四段探测集全部由 f(id, protocol, origin) 派生——stage 1 三条活路径(当前 host / _unknown/ / flat)、stage 2 diskFiles 过滤在 meta.protocol 存在时要求 host 精确匹配(:1101)、stage 3 label alias、stage 4 kernel discovered(bounded boot 下为空)→ 全 miss → loadSync 返回 null(:1137/:1153)→ getSession 建空会话。恢复窗口还会自我关闭:空/降级孪生一旦向新路由落盘一次,kernel loadAll 的 savedAt 择优(kernel/src/persist/store.ts:393-397)永久旁路完好旧文件——楼主现场 A(3697 req, 02:36) vs B(2 req, 08:28) 就是孪生已落盘后的形态。
    5. 模式覆盖面(影响面的关键):full 模式免疫(行键 (agent, conv_id),owner 已在 [Evidence] pi 车道切供应商(同 modelId)后 fold 不随会话迁移:裸上下文 2.23MB(≥298K est tokens)被 preflight 10 秒拒 3 次;账面 ≤157K 与裸水位背离 10.5h #2847 floor 42 真机验证);但 shadow 是当前默认模式(Inbound markers: one declarative strip engine for all four wires (#2789, #2798, #2811 plan B) #2815 rollout 步,读路径仍走文件树,readsUnified gate 仅 full 生效)⇒ 默认用户全体暴露,不是 opt-in 少数派。

    ② 层次判断(楼主判层正确,我补一层时间线归因)

    ③ 方案评估与处置请求(等拍板,三个选项)

    楼主补丁方向与边界我核过:严格校验、fail-closed(#3157 隔离语义保留)、不合并/覆盖、sourceFiles 保 CCR sidecar——都对。两处需要修正后再进 PR:

    • 仲裁器必须用 requests→nextBlockId(复用 staleDiskPayload 比较器,src/persist.ts:1536-1551),不能用 savedAt——楼主补丁取 newest-savedAt,在本案数据上恰好选中降级的 B 账本(2 req 赢 3697 req),止血变帮凶;
    • 跨协议扫描先不做:wire 切换(responses→anthropic)属同类损失,但保守起见 v1 只扫同协议目录,跨协议留作后续证据驱动扩展。
    选项 内容 我的判断
    A(推荐) 止血 PR:loadEnvelope 精确路由 miss 后加「同协议目录 + 同 24-hex id 后缀」兄弟探测,body id + protocol 严格复核,多候选按 staleDiskPayload 比较器仲裁,sourceFiles 保 sidecar。纯读路径、无配置、无 wire 变更;full-default 翻转后此路径自然休眠 唯一能救回存量孤儿账本的路径,同时保护 flip 前的默认用户;diff 预计 <100 行 + 回归测试
    B 仅文档:写清 shadow 不提供切换保护,引导开 full 零风险但不救存量,默认用户在 soak 窗口继续裸奔
    C 不动,等 #2960 的 full-default soak 翻转 同上,且窗口长短不受我们控制

    为什么先请示而不是直接动手:此改动改变全体默认用户的会话加载语义(§7.4 identity/session-binding 人工门禁区);你在 #2847 对同一个止血提议留了未决(当时以统一存储收口);persist.ts 正在你的 R3(#3050 lazy-load)和 #3432(StorageBackend 抽取)重构区内,并行改动有冲突成本。点头即开工(楼主测试 + 上述两处修正 + red/green 回归钉住),并与 #3432 协调合入顺序。


    结构化八问(决策请求体例,第 3/5 问按选项分列)

    1. 是什么问题:长会话中途换供应商入口后重启 daemon,同一会话的折叠账本载不回来——要么从零重建一套新账本,要么裸上下文超门被本地 413 拒掉,会话不可用。
    2. 什么原因导致的:身份=id 而磁盘寻址/读探测=f(id,protocol,origin),origin 一变历史记录对读路径不可达(Storage layer: four concrete defects (filename coupling, multi-writer heuristic, non-atomic content-store write, immortal compressed sessions) #2671 defect-1);v0.1.196 起 bounded boot 又移除了旧版全量走查的事故性自愈,触发从罕见变常规;孪生 savedAt 择优再关闭事后恢复窗口。
    3. 影响范围与严重程度:触发条件 = 长会话 × 中途换 origin × 切换后重启(或新进程 memory-miss 重载)。受影响面 = shadow(当前默认)+ legacy 用户,即所有未显式开 full 的用户;full 用户免疫。严重度 = 高(数据面连续性丢失 + 硬 413;数据在盘上但不可达,属可恢复类)。按选项:A 消除该暴露面并救回存量孤儿账本;B/C 暴露面维持现状直到 full-default 翻转。
    4. 怎么修复的(推荐选项 A 的修法思路):读路径补一层「同协议同 id 后缀兄弟探测」——把 README 身份契约(host 是可变元数据,不参与身份)回填到 legacy 读路径上;严格 body-id/protocol 复核防误认,计数器仲裁防选错孪生,fail-closed 防坏文件。根修仍是统一存储(已落地,等 soak 翻转),两者不冲突。
    5. 回归风险(按选项):A——审过的既有行为逐项:同路由加载不变(对照腿钉住)、_unknown//flat 探测顺序不变、alias 路径不变、坏文件处置保持 [bug] 会话状态写入缺 fsync:内核崩溃后留下 size 正常但 VDL=0 的全零状态文件,loadSync 永久抛错 → 502 acp-proxy failure 且不自愈(含 7 处 tmp+rename 写路径盘点) #3157 隔离语义、多实例共享目录沿用既有 staleDiskPayload 咨询式计数器边界(无新增风险维度)、full 模式不受影响(readsUnified 先行短路);主要残余风险是与 [PR] refactor(storage): extract StorageBackend contract behind UnifiedStore (#3388 Phase A) #3432/R3 的 hot-file 时序冲突(用 rebase+CI 兜)。B/C——零代码改动,零回归,代价是暴露面延续。
    6. 是否破坏缓存:不涉及(纯存储读路径,wire/prefix-cache 零接触;恢复加载只是让既有 fold 重新生效,等价于切换前行为延续)。
    7. 是否改了设计方向:无转向——A 是把既有身份契约补到 legacy 读路径,与 Identity pipeline: key sessions by (agent, conversation id) in the unified store — decouple fold ledgers from protocol/host origin (#2938 / #2847 / #2671 defect-1) #2960「host 降级为可变元数据」方向一致;B/C 维持现状。
    8. 是否引入新配置:无(A/B/C 均不加 env/config/默认值;B 仅加文档说明)。

    收工判定:triage 完成、机制坐实(hermetic 复现红/绿对照)、恢复路径与止血方案就绪,等你拍板 A/B/C。另:标题已按规范改为「English | 原文」格式,正文未动。

  3. nsdbjh commented on Oct 17, 2026

    @nsdbjh
    Author

    补充现场恢复结果:安装本地 v2 补丁并重启 Bili 后,原故障会话的 413 已消失,可以继续正常对话。

    认可您指出的 v1 仲裁问题:按最新 savedAt 选择可能误选退化账本。v2 没有继续采用该规则,而是仅针对这条已取证的会话,通过原始快照完整文件 SHA256、身份及成功 usage 等校验,恢复完整旧账本;不合并两套 messageRefs/blocks,不改写历史内容。这是定向恢复,不代表通用修复已经完成。

    另请在方案 A 的回归测试中覆盖:当前路由已经存在退化快照,同时旧路由仍有完整账本。若兄弟探测只在精确路由 miss 后执行,此场景仍会先命中退化快照,无法恢复。本次现场正是这种情况。

    当前保留本地补丁,等待正式修复;请确认最终方案是否覆盖上述已有分叉场景。

  4. ranxianglei commented on Oct 17, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

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

    收到现场恢复结果(413 消失、会话可继续对话),记下。你的两点我逐一回应:

    ① 直接确认:方案 A 的最终形态覆盖「当前路由已有退化快照 + 旧路由仍有完整账本」的分叉场景——你指出的问题成立,我原表述要修正。 「兄弟探测只在精确路由 miss 后执行」这个说法不对:你现场两个文件都在,精确路由会先命中文法正确的退化快照,永远走不到兄弟探测。修正后的规格:

    • 单一候选池:精确路由文件与同协议目录、同 id 后缀的兄弟文件一起进候选池(不再是「先精确、miss 才兜底」的两段式);
    • 统一仲裁:池内按 stats.requests → state.nextBlockId 比较器择优(复用既有 staleDiskPayload 比较器,src/persist.ts:1536-1551),精确路由文件不再短路;
    • 两种形态各有归宿:你现场形状(旧路由 3697 req vs 当前路由 2 req)→ 完整账本胜出、恢复连续性;正常延续形状(切换后会话在新路由继续积累、请求数反超)→ 新路由胜出,行为与今天一致;
    • 回归测试矩阵按此钉死五腿:① 双文件并存且旧路由更丰富(= 你的案发形状,必须载回完整账本)② 双文件并存且当前路由更丰富(正常延续)③ 仅旧路由存在(纯 miss 恢复)④ 同路由对照(行为不变)⑤ 坏文件 fail-closed([bug] 会话状态写入缺 fsync:内核崩溃后留下 size 正常但 VDL=0 的全零状态文件,loadSync 永久抛错 → 502 acp-proxy failure 且不自愈(含 7 处 tmp+rename 写路径盘点) #3157 隔离语义不漂移)。

    ② 一个配套细节一并说清:跨路由恢复后,加载出的会话要采纳当前请求的路由 meta(而不是沿用旧账本里记录的 origin)——否则下次保存还会写回旧路由路径,孪生永不收敛;采纳后下一次保存会用完整内容重写当前路由文件,退化快照被自然覆盖,最终收敛为单文件。这也是为什么仲裁必须用计数器而非 savedAt:savedAt 只反映「谁最后动过磁盘」,计数器才反映「谁携带的折叠进度更多」。

    ③ 你的 v2 定向恢复(原始快照全文件 SHA256 + 身份 + usage 校验、单会话定向、不合并两套 refs/blocks)作为该会话的止血是合理的,保留本地补丁等正式修复没问题;逐会话哈希钉死不可泛化,不需要也不适合并入主线。

    状态不变:A/B/C 决策请求已在楼上回帖中呈给 owner(八问按选项分列),拍板后按上面修正后的规格开 PR(diff <100 行 + 五腿回归测试)。

  5. nsdbjh commented on Oct 19, 2026

    @nsdbjh
    Author

    确认,修订后的方案 A 已覆盖本次双文件并存、当前路由退化的现场。请推进实现并提供 PR,保留上述五项回归测试,同时明确已成功延续的新分支如何避免被旧账本回退。当前继续保留本地 v2,待正式版本通过真实快照冷加载、保存及再次重启验证后再撤除。

  6. ranxianglei commented on Oct 19, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

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

    实现完成,PR 已开:#3765 — https://github.comranxianglei/billion-context/pull/3765 (Closes #3399,等 CI + 你 review/merge)。按修订方案 A 落地,只动 src/persist.ts。

    修法(两层)

    1. 候选池仲裁:请求带 protocol meta 时,loadEnvelope 先把同一 id 的全部同协议文件收进一个候选池(精确路由名 ∪ 目录扫描到的兄弟文件;.fb.json spill 按 savedAt 归并到 canonical,沿用 kernel loadAll 约定)。每个候选重验 body id + protocol(跨协议隔离),坏文件按 [bug] 会话状态写入缺 fsync:内核崩溃后留下 size 正常但 VDL=0 的全零状态文件,loadSync 永久抛错 → 502 acp-proxy failure 且不自愈(含 7 处 tmp+rename 写路径盘点) #3157 隔离。胜者按折叠进度选:requests → nextBlockId → 平手取精确路由 → 最后才看 savedAt——不按探测顺序、也不单看文件新鲜度。
    2. 路由收敛:从 off-route 文件恢复的会话采纳请求的 upstream origin(getSession 的 fill-if-absent 覆盖不了「已存在的旧 origin」),下一次保存就把完整内容写回当前路由文件,双文件状态自然收敛。label/title 是身份,不动;no-meta 调用方保持逐字节相同的探测行为。

    你问的「已成功延续的新分支如何避免被旧账本回退」:仲裁量是折叠进度(副本承载了多少已折叠历史),不是写入时间——requests=3697 的新分支必然压过 requests=2 的旧账本;只有两边计数完全相同时才确定性地取当前路由。测试第 3 腿专门钉住这个方向(新分支延续中不被回退),第 1 腿钉反向(空新分支不吞旧账本)。

    测试:新增 tests/issue3399-cross-route-load.test.ts 共 11 腿——双文件双向仲裁 / 保存收敛到请求路由 / 纯丢失恢复 / 同路由对照 / 坏文件隔离+降级回退 / 全坏→诚实 fresh start / 计数平手 / 跨协议隔离 / CCR sidecar 跟随 / legacy 模式通道——本地 11/11 绿;storage/persist 定向套件 16 个文件 0 失败;typecheck/build 干净。全量回归在远程 CI(PR check runs)。

    行为变更披露(有意修复,明细表在 PR 正文):

    • 跨路由冷加载:空会话→重新折叠/413 ⇒ 恢复更丰富账本并收敛到新路由;
    • 分叉双文件并存:恒取请求路由 ⇒ 取折叠进度高者;
    • 恢复会话立即采纳请求 origin(此前旧 origin 留到下次写入造成再分叉);
    • 新增两行 info 日志(restored / adopted)。无新配置、无持久化格式变化、无 kernel 改动。

    关于你的本地 v2 热补丁:按你说的保留即可,待正式版通过真实快照冷加载/保存/再重启验证后撤除。注意一处与本地补丁的行为差异:双方计数相等但新鲜度不同时,本版优先当前路由(savedAt 只做最后 tie-break);另外本 PR 只修读侧查找路径,写侧仍按当前路由命名(#2671 defect-1 的写侧 facet,另行跟踪)。

    结构化八问

    1. 是什么问题:会话中途换供应商后,daemon 重启/驱逐再冷启动时按「新供应商文件名」查账本,找不到旧供应商名下那份,会话变空;客户端重发全量历史后要么整段重新折叠,要么被本地 413 门禁直接拒掉。
    2. 什么原因导致的:文件名里的 host 前缀只是由请求 origin 派生的展示元数据(身份是 conversation id 本身,Codex 长会话在 macOS 休眠唤醒后 Bili 未触发压缩,导致上下文窗口超限 #280/会话身份不稳定:credential/upstream 维度导致长会话状态孤儿化(sessions 缺失) #286 契约),查找层却把它当成必配条件;bounded boot 又不填充 kernel 的 discovered 索引,四段探测全部落空。内存键是 id(所以热切换没事)、磁盘键含 host(所以冷启动断),两层不一致。
    3. 影响范围与严重程度:所有「中途切换上游 host + daemon 重启/LRU 驱逐」的用户,覆盖默认 shadow 与 legacy 两种存储模式(多数安装);full 模式有 R2 索引保护(Identity pipeline: key sessions by (agent, conversation id) in the unified store — decouple fold ledgers from protocol/host origin (#2938 / #2847 / #2671 defect-1) #2960)不受影响。严重程度:高——折叠账本丢失(等效数据丢失)+ 硬 413 使该会话直接不可用。
    4. 怎么修复的:把磁盘查找拉回身份契约——同 id 同协议文件收成一个候选池,按折叠进度选举胜者(疏而非堵:改的是「查找用错了键」这个根因,不是给 413 或空会话打补丁);恢复会话采纳当前路由 origin 使后续写入收敛。
    5. 是否有回归问题:逐项审过既有行为——同路由加载、_unknown/flat 回退、alias 路径、kernel discovered 路径、no-meta 调用方(loadStateForSearch 等)、CCR sidecar 解析、[bug] 会话状态写入缺 fsync:内核崩溃后留下 size 正常但 VDL=0 的全零状态文件,loadSync 永久抛错 → 502 acp-proxy failure 且不自愈(含 7 处 tmp+rename 写路径盘点) #3157 隔离语义、golden-storage-render 字节级 golden:全部保持。高危模式逐项过:无阈值/时序/输出格式漂移;fail-closed 不变(坏文件仍拒绝猜测,回退到其他候选或诚实 fresh start)。定向 16 套件 0 失败兜底。
    6. 是否破坏缓存:不涉及。前缀缓存/重试/增量游标逻辑未触碰;恢复账本恰恰是在保住前缀连续性。
    7. 是否改了设计方向:没有——是向既有设计方向纠偏(README 身份契约 + Identity pipeline: key sessions by (agent, conversation id) in the unified store — decouple fold ledgers from protocol/host origin (#2938 / #2847 / #2671 defect-1) #2960 根治路线:host 是可变元数据)。写侧文件名耦合仍在(Storage layer: four concrete defects (filename coupling, multi-writer heuristic, non-atomic content-store write, immortal compressed sessions) #2671 defect-1),不在本 PR 范围。
    8. 是否引入新配置:没有。无新 env/config/默认值,仅两行 info 日志。

    标签:bug / codex / scope/storage / p1(换供应商+重启后会话完全不可用、413 硬失败,按 p1 判)。

    判定:修复闭环(PR #3765 待 CI 绿 + owner merge);merge 后建议用你的真实快照走一遍冷加载/保存/再重启验收,通过即撤本地 v2。

  7. ranxianglei commented on Oct 19, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

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

    更正上一条的笔误:PR 链接应为 #3765 (#3765)。其余内容不变。

  8. ranxianglei commented on Oct 19, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

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

    收工更新:PR #3765 已 rebase 到最新 master,全量 CI 绿。

    期间 master 前进了数笔(含 #3623 诊断增强给冷加载加了 logColdLoad 日志行、#3759 snapshot-render 等),与我的改动在 loadSync 两个返回点发生文本冲突。冲突解法 = 意图并集:保留 master 的 logColdLoad 观测行 + 我的 adoptRequestedRoute 路由采纳调用(两者无交互,顺序为日志在前)。rebase 后定向复验:typecheck 干净、tests/issue3399-cross-route-load.test.ts 11/11 通过,随后推送。

    CI(eac33b5a0)18 项检查全部通过:build / typecheck / kernel-guard / pi-subagents-guard / pr-labels / update-wire-contract / config-doc-consistency / test ubuntu+windows × node22+24 × 3 shards。

    八问结论与此前最终回复一致,无变化(rebase 未引入任何行为变更;唯一新增交互是与 #3623 日志行的共存,属叠加不属修改)。

    PR 可合并:#3765 (Closes #3399)。您的本地 v2 热补丁按原计划待该版本经真实快照冷加载验证后再撤除。

  9. ranxianglei commented on Oct 19, 2026

    @ranxianglei
    Owner

    拉最新 master 看 snapshot 方案是否可解决

  10. darkness-A commented on Oct 19, 2026

    @darkness-A
    Contributor

    独立确认:OpenCode + 0.1.199/shadow;局部回移后生产冷加载恢复成功

    补充一个与原报告不同的运行环境:Ubuntu、OpenCode 自定义桥接、官方 npm billion-context@0.1.199(commit 5cdb28d8)、Responses 协议、默认 shadow 存储。不是原报告的 Windows/Codex 环境,也不是 #3765 的作者。

    这里固定了现有版本做临时修复;已看到维护者关于最新 master snapshot 方案的建议,但未测试最新 master/snapshot 路径,不据此声称最新主干仍有这个问题。本次回移参考 #3765 head 7b825c800198764b6b17c0c709535c07c8ea7048,没有安装整份 PR 测试包。

    脱敏证据与根因

    桥接在 HTTP 请求中确实传递真实执行会话 ID;未发现桥接删除块。相同 ID 在同一个 responses/ 目录有两份 V3 账本:

    文件所属路由 requests nextBlockId 块总数 活跃块
    provider-A(旧、完整) 1470 157 156 85
    provider-B(较新、退化) 3 3 2 1

    旧 JSON 的 meta.upstreamOrigin 仍指 provider-A,但 lastInputTokensOrigin 已指 provider-B,说明热内存曾按真实 ID 跨路由复用。重启日志显示 bounded boot warmed 0;此后带 provider-B meta 的冷加载未找到旧账本,生成较新的退化文件。旧 156 个块并未从磁盘删除。

    原包 getSession() 的内存键是 ID,持久层 legacy/shadow 冷加载却依赖协议 + host + ID 摘要。未修复的真实 bundle 在零预热隔离测试中重现:只有 A 的文件、带 B 的 meta 加载,返回 null。两份文件并存时,仅在 exact miss 后 fallback 也不够,因为 B 的退化文件已经存在。

    退化后原始上下文估计 2,452,186 tokens,对有效窗口 285,000 触发 preflight;救援压缩仍被本地 preflight fail-fast 413 (retryable=false) 拒绝。这不是“上游正常接收过请求”的证据。修复没有关闭 413 检查、扩大窗口或绕过安全门。

    实施的局部修复

    1. 同协议、同真实 ID 的当前与旧路由文件一起仲裁:requests → nextBlockId → exact route → savedAt;不以最新时间作为主标准,不合并两个账本。
    2. 验证解码后的 body/envelope ID、协议和文件归属;保留 0.1.199 的损坏文件处理。兼容 live/cold、canonical/spill、旧 fallback,保留 unified-full 优先路径。
    3. 保留胜出文件 sourceFiles,采纳请求路由后通过官方 writeNow/保存链和 shadow 双写收敛,不手工改 SQLite。
    4. 建议 [PR] fix: cold load after provider switch recovers the session fold ledger across routes (#3399) #3765 额外关注 CCR 采纳:仅改 origin + scheduleSave 时,loadContentStore() 会先读新路由路径;若新路由已有不完整原件,它可遮蔽胜出旧账本的原件。本地补丁在改 origin 前先读源侧 CCR,置 contentStoreDirty,由官方顺序先保存原件再保存状态;spill 账本使用 canonical CCR 文件名。源侧原件缺失而新路由另有原件时 fail closed,不静默拼接。具体两项回归已包含在下面代码。

    该进度仲裁沿用现有防陈旧覆盖规则,是受约束的恢复启发式,不声称是任意独立分叉的通用合并算法。主干的 snapshot/统一存储方案仍需独立验收。

    验证边界与结果

    • 局部回归 23/23:22 个合成用例 + 1 个不公开的真实快照用例;每次 fresh reader 使用 boot({maxSessions: 0, maxBytes: 0}) 并等待 remainder scan,不能先用 writeNow() 给同一个 reader 种 discovered 命中来掩盖缺陷。
    • 真实副本两次冷加载/保存:恢复 156 块、85 活跃块,blocks、messageRefs、摘要块内容逐项深比较一致,旧源 SHA256 不变,usage origin 保留,DIGEST DRIFT=0。
    • 已有 SQLite 备份副本的维护演练通过,无警告;另有 1/1 实际旧代理→正常停机→补丁→官方保存恢复→重启→指纹/状态验收的合成集成测试,无模型流量。
    • 既有桥接测试 24/24、typecheck 通过。桥接测试曾一次遇到启动健康竞态,未修改生产来掩盖,随后完整重跑通过。这些不是官方全量 CI 结论。
    • 生产在全部会话 inFlight=0 连续三秒后正常停机、补充账本/SQLite 备份,恢复后重启该独立代理;未重启 OpenCode。新进程实际代码指纹、watchdog、精确 ID 状态均复验通过:156 块、85 活跃块,旧源未动,shadow 视图一致,无 DIGEST DRIFT。
    • 未主动向目标 OpenCode 会话发送模型提示词;真实上游对话及 413 是否消失尚未验证。 原件缺失和其他大内容引发的 413 不在这个恢复结果的保证范围内。

    可审阅的固定版本补丁

    下面是本地使用的方法实现、指纹保护应用器与合成测试部分,不含私有账本/数据库/对话/配置/地址/真实会话 ID。方法是插入现有 SessionStore 的 JS 回移,不是已在最新 master 上 typecheck 的 TypeScript PR。应用器只接受这一份官方 bundle,拒绝未知版本/指纹,重复执行幂等;方法级 diff 可由原方法和下列实现生成。

    原 SHA256:0bda86e4af6bb363ec3e644ab341f9ed079a7dcefab6631c63e1e8418067cd1c。

    补丁 SHA256:a0304700dbb614b58acc36b13a2333bfdca5dde068bed09828c9c51b565ae375。

    依赖升级可能覆盖此热补丁;不能无校验地重放到新版本。建议正式发布时纳入上述 cold-zero-warm、退化 twin、源侧 CCR/spill 与父子/协议隔离回归。

    patches/bili-0.1.199-cross-route.methods.js
    // Backport of billion-context PR #3765 (7b825c8), with strict identity checks
    // and source-first CCR adoption. Not a generic fork-merging algorithm.
    export class CrossRouteHotfix {
        loadEnvelope(id, meta) {
            if (meta?.protocol) {
                const pooled = this.loadNamespacedPool(id, meta);
                if (pooled) return pooled;
            }
            const accepts = envelope => envelope && (!meta?.protocol
                || !(envelope.payload.meta?.protocol ?? envelope.payload.protocol)
                || (envelope.payload.meta?.protocol ?? envelope.payload.protocol) === meta.protocol);
            const livePaths = new Set([
                path.join(this.sessionsDir, relPathFor(id, meta?.protocol, meta?.upstreamOrigin)),
                path.join(this.sessionsDir, relPathFor(id)),
                path.join(this.sessionsDir, flatFileNameFor(id)),
            ]);
            for (const file of livePaths) {
                if (!existsSync(file)) continue;
                const envelope = this.readEnvelopeFile(file, id);
                if (accepts(envelope)) {
                    this.sourceFiles.set(id, file);
                    return envelope;
                }
            }
            const expected = path.basename(relPathFor(id, meta?.protocol, meta?.upstreamOrigin));
            const candidates = this.diskFiles.filter(file => fileNameMatchesId(path.basename(file), id, meta?.protocol, meta?.upstreamOrigin)
                || (!meta?.protocol && path.basename(file).endsWith(`_${expected}`)));
            for (const file of candidates) {
                if (!existsSync(file)) continue;
                const envelope = this.readEnvelopeFile(file, id);
                if (accepts(envelope)) {
                    this.sourceFiles.set(id, file);
                    return envelope;
                }
            }
            const alias = this.legacyAliases.get(id);
            if (alias) {
                const envelope = this.readEnvelopeFile(alias.file, alias.id);
                if (envelope && (!meta?.protocol || envelope.payload.meta?.protocol === meta.protocol)) {
                    this.sourceFiles.set(id, alias.file);
                    return { ...envelope, id, payload: { ...envelope.payload, id } };
                }
            }
            const envelope = this.store.loadSync(id, relPathFor(id, meta?.protocol, meta?.upstreamOrigin));
            return accepts(envelope) ? envelope : null;
        }
    
        loadNamespacedPool(id, meta) {
            const proto = meta.protocol;
            const exact = path.join(this.sessionsDir, relPathFor(id, proto, meta.upstreamOrigin));
            const dirs = new Set([path.join(this.sessionsDir, proto), path.join(`${this.sessionsDir}-cold`, proto)]);
            const logicalFor = file => file.endsWith('.fb.json') ? `${file.slice(0, -8)}.json` : file;
            const candidates = new Set([exact, exact.replace(/\.json$/, '.fb.json')]);
            const suffix = `_${path.basename(relPathFor(id))}`;
            for (const file of this.diskFiles) {
                const base = path.basename(file);
                if (dirs.has(path.dirname(file)) && logicalFor(base).endsWith(suffix)) candidates.add(file);
            }
            const valid = new Map();
            for (const file of candidates) {
                if (!existsSync(file)) continue;
                // Keep the installed version's validation/quarantine contract.
                const env = this.readEnvelopeFile(file, id);
                if (!env) continue;
                const savedProtocol = env.payload.meta?.protocol ?? env.payload.protocol;
                const savedOrigin = env.payload.meta?.upstreamOrigin ?? env.payload.upstreamOrigin;
                if (savedProtocol && savedProtocol !== proto) continue;
                if (savedOrigin && !fileNameMatchesId(path.basename(file), id, proto, savedOrigin)) continue;
                const logical = logicalFor(file);
                const prior = valid.get(logical);
                if (!prior || (env.savedAt ?? 0) > (prior.env.savedAt ?? 0)) valid.set(logical, { file, env });
            }
            const winner = [...valid.values()].sort((a, b) => {
                const requests = item => item.env.payload.stats?.requests ?? item.env.payload.requests ?? 0;
                const nextBlock = item => item.env.payload.state?.nextBlockId ?? 0;
                return requests(b) - requests(a)
                    || nextBlock(b) - nextBlock(a)
                    || Number(logicalFor(b.file) === exact) - Number(logicalFor(a.file) === exact)
                    || (b.env.savedAt ?? 0) - (a.env.savedAt ?? 0)
                    || a.file.localeCompare(b.file);
            })[0];
            if (!winner) return null;
            this.sourceFiles.set(id, winner.file);
            if (winner.file !== exact) this.log('info', `[persist] cross-route hotfix restored ${id} (${winner.env.payload.stats?.requests ?? 0} requests, ${winner.env.payload.state?.blocks?.length ?? 0} blocks)`);
            return winner.env;
        }
    
        adoptRequestedRoute(id, session, meta) {
            if (!meta?.protocol || !meta.upstreamOrigin) return;
            const src = this.sourceFiles.get(id);
            const exact = path.join(this.sessionsDir, relPathFor(id, meta.protocol, meta.upstreamOrigin));
            if (!src || src === exact) return;
            // Read the selected ledger's originals BEFORE looking under the new
            // route. A degraded twin may already have its own, incomplete CCR file.
            const sourceRel = path.relative(this.sessionsDir, src).replace(/(?:\.fb)?\.json$/, '.content-store.json');
            const originals = this.readContentStoreRels(id, [sourceRel]);
            if (originals) {
                session.contentStore = originals;
                session.contentStoreDirty = true;
            }
            if (!originals && !session.contentStore && existsSync(path.join(this.sessionsDir,
                contentStoreRelPathFor(id, meta.protocol, meta.upstreamOrigin)))) {
                throw new Error('Selected-source originals are missing; refusing to adopt another route\'s content store');
            }
            session.meta.upstreamOrigin = meta.upstreamOrigin;
            this.log('info', `[persist] cross-route hotfix adopting requested route for ${id}`);
            this.scheduleSave(session);
        }
    
        loadSync(id, meta) {
            if (!this.enabled) return null;
            if (this.shadow?.readsUnified) {
                const unified = this.loadUnified(id);
                if (unified) return this.afterLoad(id, unified);
                const envelope = this.loadEnvelope(id, meta);
                if (!envelope) return null;
                const imported = this.shadow.importRecord(envelope.payload);
                if (imported) {
                    const view = this.shadow.viewFor(id);
                    if (view) return this.afterLoad(id, view);
                }
                const session = buildSession(envelope.payload);
                if (hasNegativePersistedTokens(envelope.payload)) {
                    this.scheduleSave(session);
                    this.log('info', `[persist] clamped negative token stats on reload for ${id} (#408)`);
                }
                this.adoptRequestedRoute(id, session, meta);
                return session;
            }
            const envelope = this.loadEnvelope(id, meta);
            if (!envelope) return null;
            const session = buildSession(envelope.payload);
            if (hasNegativePersistedTokens(envelope.payload)) {
                this.scheduleSave(session);
                this.log('info', `[persist] clamped negative token stats on reload for ${id} (#408)`);
            }
            this.adoptRequestedRoute(id, session, meta);
            return session;
        }
    }
    scripts/apply-bili-cross-route-hotfix.mjs
    import assert from 'node:assert/strict';
    import { createHash } from 'node:crypto';
    import { readFileSync, writeFileSync, renameSync, mkdirSync } from 'node:fs';
    import path from 'node:path';
    import { fileURLToPath } from 'node:url';
    import ts from 'typescript';
    
    export const ORIGINAL_SHA256 = '0bda86e4af6bb363ec3e644ab341f9ed079a7dcefab6631c63e1e8418067cd1c';
    export const MARKER = 'bili-cross-route-hotfix-0199-v1';
    export const sha256 = text => createHash('sha256').update(text).digest('hex');
    const root = fileURLToPath(new URL('../', import.meta.url));
    const methodHashes = {
        loadEnvelope: '152107c351b3e833604ec603305d544cd3a65a2bcc12075080cd5f6ec01d8106',
        loadSync: '0e54954f5648bb4e74832b8bf08d23c4cb0fbfeef191ad2a2310a2a9e8e30ad1',
    };
    
    export function makePatchedBundle(original) {
        assert.equal(sha256(original), ORIGINAL_SHA256, 'Unsupported official bundle; refusing an unverified patch');
        const ast = ts.createSourceFile('index.js', original, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS);
        let store;
        function visit(node) {
            if (ts.isClassExpression(node) && node.members.some(m => m.name?.getText(ast) === 'loadEnvelope')) {
                assert.equal(store, undefined, 'Ambiguous SessionStore class');
                store = node;
            }
            ts.forEachChild(node, visit);
        }
        visit(ast);
        assert.ok(store, 'SessionStore not found');
        const template = readFileSync(path.join(root, 'patches/bili-0.1.199-cross-route.methods.js'), 'utf8');
        const methodsAst = ts.createSourceFile('hotfix.js', template, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS);
        const cls = methodsAst.statements.find(ts.isClassDeclaration);
        const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
        const replacements = [];
        for (const method of cls.members) {
            const name = method.name.getText(methodsAst);
            const transformed = ts.transform(method, [context => {
                const walk = node => {
                    if (ts.isIdentifier(node) && ['path', 'existsSync'].includes(node.text)) {
                        return ts.factory.createIdentifier(node.text === 'path' ? 'path15' : 'existsSync4');
                    }
                    return ts.visitEachChild(node, walk, context);
                };
                return node => ts.visitNode(node, walk);
            }]);
            const printed = printer.printNode(ts.EmitHint.Unspecified, transformed.transformed[0], methodsAst);
            transformed.dispose();
            const old = store.members.find(m => m.name?.getText(ast) === name);
            if (old) {
                assert.equal(sha256(old.getText(ast)), methodHashes[name], `Method drift: ${name}`);
                replacements.push({ start: old.getStart(ast), end: old.end, text: printed });
            } else {
                assert.ok(['loadNamespacedPool', 'adoptRequestedRoute'].includes(name));
                replacements.push({ start: store.end - 1, end: store.end - 1, text: `\n${printed}\n` });
            }
        }
        let output = original;
        for (const r of replacements.sort((a, b) => b.start - a.start)) output = output.slice(0, r.start) + r.text + output.slice(r.end);
        // Deliberately do not point to the vendor's now-inaccurate source map.
        output = output.replace('//# sourceMappingURL=index.js.map', `// ${MARKER}; see patches/bili-0.1.199-cross-route.methods.js`);
        return output;
    }
    
    export function instrumentBundle(bundle) {
        return bundle + '\nexport { SessionStore, createSession, buildRecord, relPathFor, contentStoreRelPathFor };\n';
    }
    
    async function main() {
        const [mode, target] = process.argv.slice(2);
        assert.ok(['--stage', '--apply', '--check'].includes(mode) && target,
            'Usage: node scripts/apply-bili-cross-route-hotfix.mjs --stage <directory> | --apply/--check <package-directory>');
        const packageDir = mode === '--stage' ? path.join(root, 'runtime/node_modules/billion-context') : path.resolve(target);
        const pkg = JSON.parse(readFileSync(path.join(packageDir, 'package.json'), 'utf8'));
        assert.equal(pkg.version, '0.1.199', 'Unsupported runtime version');
        const file = path.join(packageDir, 'dist/index.js');
        const original = readFileSync(file, 'utf8');
        const pristine = original.includes(MARKER)
            ? readFileSync(path.join(packageDir, 'dist/index.js.before-cross-route-hotfix'), 'utf8') : original;
        const patched = makePatchedBundle(pristine);
        assert.ok(original === pristine || original === patched, 'Runtime differs from the verified original/patched bundle');
        if (mode === '--check') {
            assert.equal(original, patched, 'Hotfix is not applied');
        } else if (mode === '--stage') {
            mkdirSync(target, { recursive: true, mode: 0o700 });
            writeFileSync(path.join(target, 'index.js'), patched, { mode: 0o600 });
            writeFileSync(path.join(target, 'testable.mjs'), instrumentBundle(patched), { mode: 0o600 });
            writeFileSync(path.join(target, 'original-testable.mjs'), instrumentBundle(pristine), { mode: 0o600 });
        } else if (original !== patched) {
            writeFileSync(file + '.before-cross-route-hotfix', pristine, { flag: 'wx', mode: 0o600 });
            writeFileSync(file + '.hotfix-tmp', patched, { flag: 'wx', mode: 0o644 });
            renameSync(file + '.hotfix-tmp', file);
        }
        console.log(JSON.stringify({ mode, originalSHA256: sha256(pristine), patchedSHA256: sha256(patched), marker: MARKER }));
    }
    
    if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) await main();
    tests/bili-cross-route-hotfix.test.mjs(仅合成部分,22 项)
    import assert from 'node:assert/strict';
    import test from 'node:test';
    import fs from 'node:fs';
    import path from 'node:path';
    import { pathToFileURL } from 'node:url';
    import { makePatchedBundle, instrumentBundle, sha256 } from '../scripts/apply-bili-cross-route-hotfix.mjs';
    
    const work = fs.mkdtempSync('/tmp/opencode/bili-hotfix-test-');
    fs.mkdirSync(path.join(work, 'dist'));
    fs.writeFileSync(path.join(work, 'package.json'), JSON.stringify({ version: '0.1.199', type: 'module' }));
    const official = fs.readFileSync(new URL('../runtime/node_modules/billion-context/dist/index.js', import.meta.url), 'utf8');
    const pristine = official.includes('bili-cross-route-hotfix-0199-v1')
        ? fs.readFileSync(new URL('../runtime/node_modules/billion-context/dist/index.js.before-cross-route-hotfix', import.meta.url), 'utf8') : official;
    const patched = makePatchedBundle(pristine);
    fs.writeFileSync(path.join(work, 'dist/testable.mjs'), instrumentBundle(patched));
    fs.writeFileSync(path.join(work, 'dist/original.mjs'), instrumentBundle(pristine));
    process.env.BILI_STORAGE_UNIFIED = 'shadow';
    process.env.BILI_PERSIST_ZSTD = '0';
    process.env.ACP_AUTO_UPDATE = '0';
    const mod = await import(pathToFileURL(path.join(work, 'dist/testable.mjs')).href);
    const baseline = await import(pathToFileURL(path.join(work, 'dist/original.mjs')).href);
    const { SessionStore, createSession, buildRecord, relPathFor, contentStoreRelPathFor } = mod;
    const id = 'synthetic-cold-resume';
    const a = { protocol: 'responses', upstreamOrigin: 'https://provider-a.example' };
    const b = { protocol: 'responses', upstreamOrigin: 'https://provider-b.example' };
    const logs = [];
    let seq = 0;
    function dir() { const d = path.join(work, `case-${seq++}`); fs.mkdirSync(d); return d; }
    function seed(d, meta, requests, nextBlockId = 1, opts = {}) {
        const session = createSession(opts.id ?? id, meta);
        session.stats.requests = requests;
        session.state.nextBlockId = nextBlockId;
        const record = buildRecord(session);
        record.savedAt = opts.savedAt ?? 100;
        if (opts.mutate) opts.mutate(record);
        const file = path.join(d, opts.rel ?? relPathFor(session.id, meta.protocol, meta.upstreamOrigin));
        fs.mkdirSync(path.dirname(file), { recursive: true });
        fs.writeFileSync(file, JSON.stringify({ version: 3, id: session.id, savedAt: record.savedAt, payload: record }));
        return file;
    }
    async function reader(d, cls = SessionStore) {
        const s = new cls({ dir: d, debounceMs: 60000, log: (level, msg) => logs.push({ level, msg }) });
        assert.equal((await s.boot({ maxSessions: 0, maxBytes: 0 })).size, 0, 'must be a real zero-warm cold reader');
        if (s.remainderScan) await s.remainderScan;
        return s;
    }
    function ccr(d, meta, value) {
        const file = path.join(d, contentStoreRelPathFor(id, meta.protocol, meta.upstreamOrigin));
        fs.mkdirSync(path.dirname(file), { recursive: true });
        fs.writeFileSync(file, JSON.stringify(value));
        return file;
    }
    
    test('unpatched release reproduces cold miss with old-route-only ledger', async () => {
        const d = dir(); seed(d, a, 1470, 157);
        const s = await reader(d, baseline.SessionStore);
        assert.equal(s.loadSync(id, b), null); s.closeQuietly();
    });
    test('old rich ledger beats newer degraded exact-route file', async () => {
        const d = dir(); seed(d, a, 1470, 157); seed(d, b, 3, 3, { savedAt: 999999 });
        const s = await reader(d); const hit = s.loadSync(id, b);
        assert.equal(hit.stats.requests, 1470); assert.equal(hit.state.nextBlockId, 157);
        assert.equal(hit.meta.upstreamOrigin, b.upstreamOrigin); s.closeQuietly();
    });
    test('legitimately progressed current route is never rolled back', async () => {
        const d = dir(); seed(d, a, 10, 9); seed(d, b, 11, 10);
        const s = await reader(d); assert.equal(s.loadSync(id, b).stats.requests, 11); s.closeQuietly();
    });
    test('equal requests compare nextBlockId before timestamps', async () => {
        const d = dir(); seed(d, a, 10, 20); seed(d, b, 10, 2, { savedAt: 999999 });
        const s = await reader(d); assert.equal(s.loadSync(id, b).state.nextBlockId, 20); s.closeQuietly();
    });
    test('equal progress prefers current route, not older host with latest savedAt', async () => {
        const d = dir(); seed(d, a, 10, 9, { savedAt: 999999 }); seed(d, b, 10, 9);
        const s = await reader(d); s.loadSync(id, b);
        assert.equal(s.sourceFiles.get(id), path.join(d, relPathFor(id, b.protocol, b.upstreamOrigin))); s.closeQuietly();
    });
    test('old-only route and pure miss', async () => {
        const d = dir(); seed(d, a, 8);
        const s = await reader(d); assert.equal(s.loadSync(id, b).stats.requests, 8);
        assert.equal(s.loadSync('unrelated', b), null); s.closeQuietly();
    });
    test('wrong body ID is rejected even with matching filename and envelope ID', async () => {
        const d = dir(); seed(d, a, 9999, 100, { mutate: r => { r.id = id + '|sub:other'; } });
        const s = await reader(d); assert.equal(s.loadSync(id, b), null); s.closeQuietly();
    });
    test('payload protocol cannot escape pool or exact-path fallback validation', async () => {
        const d = dir(); seed(d, b, 9999, 100, { mutate: r => { r.meta.protocol = 'anthropic'; } });
        const s = await reader(d); assert.equal(s.loadSync(id, b), null); s.closeQuietly();
    });
    test('different protocol directory, parent/child and different ID remain isolated', async () => {
        const d = dir(); seed(d, { ...a, protocol: 'anthropic' }, 99);
        seed(d, a, 100, 20, { id: id + '|sub:other' }); seed(d, a, 200, 50, { id: 'different-session' });
        const s = await reader(d); assert.equal(s.loadSync(id, b), null); s.closeQuietly();
    });
    test('host prefix must belong to the persisted upstream', async () => {
        const d = dir(); seed(d, a, 99, 20, { rel: relPathFor(id, a.protocol, 'https://misnamed.example') });
        const s = await reader(d); assert.equal(s.loadSync(id, b), null); s.closeQuietly();
    });
    test('bad sibling is quarantined while valid ledger survives', async () => {
        const d = dir(); seed(d, a, 9); const broken = seed(d, b, 1); fs.writeFileSync(broken, '{broken');
        const s = await reader(d); assert.equal(s.loadSync(id, b).stats.requests, 9);
        assert.ok(fs.readdirSync(path.dirname(broken)).some(n => n.includes('.corrupt-'))); s.closeQuietly();
    });
    test('all bad candidates retain release quarantine-and-miss behavior', async () => {
        const d = dir(); const broken = seed(d, b, 1); fs.writeFileSync(broken, '\0\0');
        const s = await reader(d); assert.equal(s.loadSync(id, b), null); s.closeQuietly();
    });
    test('canonical/spill reconciles by savedAt before cross-route progress comparison', async () => {
        const d = dir(); const canonical = seed(d, a, 9, 20);
        seed(d, a, 10, 21, { rel: path.relative(d, canonical).replace(/\.json$/, '.fb.json'), savedAt: 200 });
        const s = await reader(d); assert.equal(s.loadSync(id, b).stats.requests, 10); s.closeQuietly();
    });
    test('same-protocol cold-root ledger recovers across host switch', async () => {
        const d = dir(); seed(d + '-cold', a, 9);
        const s = await reader(d); assert.equal(s.loadSync(id, b).stats.requests, 9); s.closeQuietly();
    });
    test('unindexed exact route created after boot remains discoverable', async () => {
        const d = dir(); const s = await reader(d); seed(d, b, 10);
        assert.equal(s.loadSync(id, b).stats.requests, 10); s.closeQuietly();
    });
    test('unknown legacy fallback still loads without metadata', async () => {
        const d = dir(); seed(d, a, 6, 1, { rel: relPathFor(id) });
        const s = await reader(d); assert.equal(s.loadSync(id).stats.requests, 6); s.closeQuietly();
    });
    test('selected source CCR beats incomplete current-route originals and survives restart', async () => {
        const d = dir(); seed(d, a, 20, 10); seed(d, b, 1, 2);
        const originals = { version: 1, byRef: { m00001: 'sha-full', m00002: 'sha-2' }, byHash: { 'sha-full': { text: 'original A' }, 'sha-2': { text: 'original B' } } };
        ccr(d, a, originals); ccr(d, b, { version: 1, byRef: { m00001: 'sha-wrong' }, byHash: { 'sha-wrong': { text: 'incomplete twin' } } });
        let s = await reader(d); const hit = s.loadSync(id, b);
        assert.deepEqual(hit.contentStore, originals);
        await s.flushAll([hit]);
        assert.deepEqual(JSON.parse(fs.readFileSync(path.join(d, contentStoreRelPathFor(id, b.protocol, b.upstreamOrigin)), 'utf8')), originals);
        s = await reader(d); const again = s.loadSync(id, b);
        assert.equal(again.stats.requests, 20); assert.deepEqual(s.loadContentStore(again), originals);
        const unified = s.shadow.viewFor(id); assert.equal(unified.stats.requests, 20);
        s.closeQuietly();
    });
    test('corrupt source originals fail closed instead of replacing originals', async () => {
        const d = dir(); seed(d, a, 20); ccr(d, a, {});
        const s = await reader(d); assert.throws(() => s.loadSync(id, b), /Content store.*refusing to replace its originals/); s.closeQuietly();
    });
    test('spill ledger reads canonical source CCR before degraded current-route originals', async () => {
        const d = dir(); const canonical = relPathFor(id, a.protocol, a.upstreamOrigin);
        seed(d, a, 20, 10, { rel: canonical.replace(/\.json$/, '.fb.json') }); seed(d, b, 1, 2);
        const full = { version: 1, byRef: { m00001: 'sha-full' }, byHash: { 'sha-full': { text: 'full original' } } };
        ccr(d, a, full); ccr(d, b, { version: 1, byRef: {}, byHash: {} });
        const s = await reader(d); assert.deepEqual(s.loadSync(id, b).contentStore, full); s.closeQuietly();
    });
    test('missing source originals cannot silently adopt an unrelated degraded CCR', async () => {
        const d = dir(); seed(d, a, 20); seed(d, b, 1);
        ccr(d, b, { version: 1, byRef: {}, byHash: {} });
        const s = await reader(d); assert.throws(() => s.loadSync(id, b), /Selected-source originals are missing/); s.closeQuietly();
    });
    test('full-mode unified-first lane remains unchanged', async () => {
        const d = dir(); process.env.BILI_STORAGE_UNIFIED = 'full';
        try {
            const s = await reader(d); const hit = createSession(id, a); hit.stats.requests = 8;
            await s.writeNow(hit); s.closeQuietly();
            const r = await reader(d); assert.equal(r.loadSync(id, b).stats.requests, 8); r.closeQuietly();
        } finally { process.env.BILI_STORAGE_UNIFIED = 'shadow'; }
    });
    test('source fingerprint guard rejects drift rather than blindly applying', () => {
        assert.throws(() => makePatchedBundle(pristine + '\n'), /Unsupported official bundle/);
        assert.throws(() => makePatchedBundle(patched), /Unsupported official bundle/);
    });
    
    test.after(() => {
        assert.equal(logs.filter(x => /DIGEST DRIFT/.test(x.msg)).length, 0, 'must not introduce shadow digest drift');
        console.log(JSON.stringify({ patchedSHA256: sha256(patched), fixtureRoot: work, digestDrift: 0, note: 'Fixtures may contain private snapshots; contents are never printed or published' }));
    });
  11. pkuqiuning commented on Oct 19, 2026

    @pkuqiuning

    Supplementary evidence for the route-rekey / state-identity family, from an independent occurrence on v0.1.201 (proxy instance managed by the client plugin, OpenCode client, legacy/shadow file mode). It reaches the same end state as #3399 (rich old-route ledger orphaned, empty twin under the new route, full-history resend) via a different observed trigger, and adds three details not covered there.

    Occurrence. A 780+ request session holding 121 fold blocks (55 active) was persisted under <protocol>/<relay-host>_<sha24>.json (<protocol> = the per-protocol state directory). During routine state maintenance the proxy was killed; it was respawned by its plugin. On the next save, the session was persisted under a different filename — <protocol>/api.anthropic.com_<sha24>.json — while every actual request still routes to the relay origin. The pre-existing rich ledger under the relay-host filename was never probed at load (no cold-load log line resolves the old file; this is inferred from the empty load result); the session came back empty (0 blocks, compression counter 0, request counter restarted and observed at 3), and the next request resent the full unfolded history: 1292 messages / ~2.0M chars of token-dense code/JSON, tripping the input limit declared to the proxy as 1,000,000 tokens with an HTTP 400 (surfaced by the relay as ContextWindowExceeded).

    Detail 1 — where the wrong origin comes from. The state filename's host label derives from session.meta.upstreamOrigin, whose resolution chain ends in a hardcoded default: env.ACP_UPSTREAM ?? fileConfig.upstream ?? "https://api.anthropic.com". In plugin deployments where the real upstream is a relay, a restart that loses per-request route context can silently substitute that default — and the default then becomes the session's route identity. Our config sets no upstream, so the default was always live and only needed one context-loss to win. (The "respawn → lost route context → default origin" step is inference: the re-key itself emitted no log line.)

    Detail 2 — the shadow path can precede the re-key. Three seconds before the drifted save (respawn at T, digest-drift line at T+3s by log timestamps), the log printed:

    [storage] unified shadow DIGEST DRIFT for <sid>: unified replay != legacy load view — dropped the ingest watermark, the next save re-ingests the full tail
    

    In this occurrence the legacy file had been hand-edited (state surgery to prune dead ids), so the digest mismatch was expected — but it demonstrates a second path to the same end state: any legacy/shadow disagreement drops the watermark, and the following save can land under a different (default) route.

    Detail 3 — silence + the gate. The re-key itself produced no warning line (no drift entry, no banner); the only symptoms were the empty session report and then the upstream 4xx. And at the exact overflow moment, the model's own compress tool call was refused by the proxy with gate:no-visible-context (receipt text begins "no visible context — the proxy holds"; remainder elided) — the anti-loop gate closed the only in-band recovery path precisely when compression was most needed, and the next request 400'd. Both feel worth addressing alongside #3765's restore fix: the route label is treated as display metadata, yet it determines which state file a cold load resolves — and therefore whether a 121-block ledger exists at all.

    Recovery in this occurrence: copying the rich old-route file over the new-route filename restored the session immediately — consistent with #3765's "fold-progress winner" arbitration order (requests → nextBlockId → exact-route → savedAt).

  12. added a commit that references this issue on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions