From 98c79e72c310e7e0c97dffa66d8fcc7a1d6ae7cd Mon Sep 17 00:00:00 2001 From: mz Date: Sat, 31 Oct 2026 15:19:10 +0300 Subject: [PATCH 1/2] fix(instance): re-hash the entry script when the file changes (#5354) entryScriptFingerprint() cached a script's sha256 by path for the life of the process. The omp plugin runs inside the long-lived agent host, so after an in-place `npm i -g` the host kept the first build's digest and #4125 handed it to every proxy it respawned (BILI_SPAWN_FINGERPRINT): children running the same new code registered different fingerprints, could not attach to one another and laddered onto the next lane port each time. Key the cache on the file's identity (dev/ino/size/mtime/ctime) and re-hash when it changes; an unreadable file drops its entry and returns undefined. Fixes #5354 --- CHANGELOG.md | 1 + src/instance.ts | 21 ++++++-- ...e5354-fingerprint-follows-the-file.test.ts | 48 +++++++++++++++++++ 3 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 tests/issue5354-fingerprint-follows-the-file.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 978e98698a..ce0599984c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag - **系统架构文档体系落地(owner 指令 2026-11-08):`docs/architecture/` 成为实时维护的架构枢纽**:新增 `docs/architecture.md` / `docs/architecture.zh-CN.md` 总枢纽(整体架构图中英双版转正 + 8 模块总表),首个样板模块页 `docs/architecture/storage.{md,zh-CN.md}`(统一存储:职责/原理/工作机制/硬不变量,配模块图中英双版)。文件级地图 `reference/architecture.md` 补齐 125 个缺失条目(新存储层 / marker / fastpath / lanes 等全部新模块)并升级为 CI 强制全量覆盖。新增 CI 车道 `architecture-docs`(`tools/check-architecture-docs.mjs`):地图覆盖全 `src/` 文件、总表↔页面成对、图 svg 优先 + png 备选齐备、SVG 基础良构(未转义 `&` 曾让英文图线上破图)。AGENTS.md「Documentation Layout」新增实时维护规则:改结构 ⇒ 同 PR 更新页 + 图 + 总表。其余 7 个模块页(pipeline / lanes / kernel / identity / fastpaths / webui / update)同一 PR 全部补齐——总表 8 行全 ✅(每页 = 中英双版文档 + 中英双版图)。 ### Fixed +- **原地更新后,长驻宿主重拉的 lane 代理不再被打上旧构建的代码指纹(#5354)**:`entryScriptFingerprint()` 按路径把入口脚本的 sha256 缓存到进程退出,之后再也不看盘上的文件。omp 插件就跑在长驻的 agent 宿主进程里——`npm i -g` 把新 `dist/index.js` 原地换上之后,宿主重拉 lane 代理时仍把**旧**摘要经 `BILI_SPAWN_FINGERPRINT`(#4125)交给子进程:子进程跑的是新代码、登记的却是旧指纹,其他宿主一律判「代码指纹不一致」接不上,只好各自另起代理,每重拉一次端口就往后跳一格、`port-zone.json` 的 `lanes.omp` 随之漂移(实测一天之内 18792 → 18797,三个同构建代理各占一口)。现在缓存以文件身份(dev/ino/size/mtime/ctime)为键:文件被替换或改写即重算,读不到时返回 `undefined` 走无指纹路径。tests:`tests/issue5354-fingerprint-follows-the-file.test.ts`(改名替换/原地改写/删除三种退化)。 - **纯 SQLite 姿态下证实损坏不再静默降级 JSON,改为响亮拒启(owner 策略 2026-11-21,#7156 生产事故)**:quick_check 证实 index.db 损坏(含「首个 post-open 写入」撞损坏的大库/缓存判决分支,#7156 FIX4)且进程有效姿态是**纯 SQLite**(有效 mode full + sqlite 后端 + 无加密 + jsonWriter off = 2026-11-13 缺省翻转后的缺省,或显式 off)时,旧契约(响亮告警 + 不挂索引照常启动、JSON 信封服务)在这里会服务一个近乎为空的非持久视图,其后每一轮都只落在非持久副本里——静默丢持久历史,比损坏本身更糟(实测现场:0.1.216 的 3.2GB index.db 损坏后自动「降级 JSON」,面板显示 requests:2/上下文 0,而真实 1563 请求/124k 上下文/$12.41 成本全在索引里)。现在该姿态 REFUSES TO START:文件原样不动(不搬不拷不重建),日志与诊断面只给可操作的手工修复(备份 index.db/-wal/-shm → 手工移开 → 再启动,从双写期信封重建);`BILI_STORAGE_ALLOW_LEGACY_DEGRADE=1` 可有意识地恢复旧降级路径(永不缺省、不被任何配置值隐含)。其余姿态(显式双写 full、shadow、legacy、网络后端、加密)信封仍是真恢复来源,旧契约逐字节保持。姿态判定是纯函数(与 persist 的 jsonWriterOff 同一公式,两开关永不分歧);拒启被记录进诊断面(`/__bili/status` 与 diagnostics 可见「REFUSING TO START (pure-sqlite stance)」前缀)且每次启动都重新生效(latch 只短路进程内重扫)。回归:tests/issue7156-pure-sqlite-refusal.test.ts(姿态真值表 + 两种纯 sqlite 拼法与 file 层的损坏拒启/逃生阀/重复拒启 + 字节不变断言 + 双写姿态保留降级契约)、tests/issue4816-integrity-throw-quarantine.test.ts、tests/acp-status-storage-degraded.test.ts、tests/web-storage-banner.test.ts、tests/storage-durability.test.ts 与 storage 全量套件绿。 - **信封导入不再顶掉 index 已有的压缩块谱系与生命周期统计(#7156 事故第二现场,2026-11-21)**:#7156 的生产现场里,降级窗口(不挂索引、纯信封服务)内的一次会话保存把信封里的 32 个粗块结构写进了 `importRecord`——它把信封当成权威谱系:按 incoming 集合先删后插,index 里 103 个更细的压缩块(及其 summary/content 引用链)被整批删除,生命周期统计也从 requests:1563/124k 上下文/$12.41 成本回退成 requests:2/0。之后每一轮 preflight 都在 32 粗块的地基上折叠,上下文装不进模型窗口、会话不可用。现在 `ingestSession` 新增 `envelopeImport` 标记(仅 `UnifiedShadow.importRecord`/`importRecordAsync` 两个导入入口传):存储侧已有块谱系时**只采纳信封里 seq 更高的新消息**,块谱系与 stale 清理一律跳过(index 无块的真正首开导入不变,仍整体采纳信封结构);stats_json 同带单调守卫——envelopeImport 下已存 requests ≥ 传入 requests 时保留已存值(仅导入路径生效,organic 保存仍是权威且零额外开销)。回归:tests/envelope-import-block-lineage.test.ts(细块保留 + stats 只升不降 + 首开导入采纳 + organic 权威语义不变 + importRecord wiring 五用例)、tests/storage-ingest-watermark.test.ts。 - **dsh 运行时注册的 preset 继承压缩完全禁止(#7190,关 #7178 / #7062 / #7169)**:dsh 的 preset 注册表 `AgentPresetRegistry.register(definition)` 是**启动后才出生**的 preset 行唯一的出生点,它把嵌套行直接挂进内存 PresetTree(其 `commit()` 为空实现),从不经过 loader 的 `internal/config` 变换——bili 的 #4147 结构性 disable 重写(`bili-compaction-gate`,#3354/#3662)永远碰不到这些行。于是运行时注册 preset 的压缩引擎是全副武装的(每步压力触发 + 模型切换前置压缩 + 溢出恢复三条触发器都在 dsh 的 surface 计量上点火),而 bili 侧 wire guard 拒绝每一次压缩调用:#7062 的逐步 422 风暴、#7178 的模型切换死局(`MODEL_SWITCH_COMPACTION_FAILED` 永不满足前提)、#7169 的 landed-checkpoint 触发 rebase 抹掉 bili 自己的折叠块再 DIGEST DRIFT 冻结信封,全是这台引擎在开火。修法与 boot-time 行同一判决、同一机制:gate 入口(always-on,bili-native 关着也活着)现在包一层 `agentPresets.register`——定义里的 compaction-basic / command-compact 行在挂入前就地改写为 `disabled: true`。判决按每次注册调用检查(bili-native 没宣告持线 = 定义原样通过,dsh 原生压缩照常);无 compaction 行 / 畸形定义按身份原样通过(注册表自己的校验看到字节级相同的输入);Symbol-for 标记保证重 apply 不双重包裹;宿主没有该注册表 = 无运行时 preset 可注册,静默不包裹。遥测新增 `runtime` / `runtimeWrapped` 维度,#6770 覆盖一行据此报告「register() 已覆盖——尚无运行时注册」或「N 个运行时注册被改写」,wrap 未知时不报假缺口。**非模型可见文本改动**(只动 dsh 侧 web banner 的覆盖一行与日志)。dsh 源码依据(deepseek-ai/deepseek-harness @ 2e39e70,与现场 0.2.2-rc.3 逐点核验一致):`packages/preset/agent-preset-registry/src/index.ts:96`(register 立即激活、重复 id 抛错)、`mount.ts:22-23`(PresetTree 空 commit)、per-fiber `internal/config` waterfall(`vendor/cordis/src/fiber.ts:691-700`)——preset 作用域 fiber 在自己的上下文里解析配置,根上注册的 global hook 不触发。回归:tests/issue7190-dsh-runtime-preset-coverage.test.ts(新增 10 条行驱动正反用例:改写/身份保持/判决关/重包裹/`this` 转发/无注册表降级/get 回退/遥测去重 + 覆盖文案四形态)、tests/issue3354-dsh-compaction-disable.test.ts、tests/issue6770-compaction-coverage-text.test.ts、tests/issue4147-dsh-overlay-structural.test.ts 全绿。 diff --git a/src/instance.ts b/src/instance.ts index 7fa0dd5749..7b872a29cb 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -321,20 +321,31 @@ export async function findPresentUnverifiedSameLaneTwin( * compares it against the hash of the script it would spawn — so "same * version" is never enough: two installs of 0.1.x with different dist * contents (local rebuild, npm link, unpublished branch) must not be - * confused for one codebase. Unreadable file → undefined → never attach. */ -const scriptFingerprints = new Map(); + * confused for one codebase. Unreadable file → undefined → never attach. + * #5354: the cache follows the FILE, not the process — it is keyed on the + * script's identity (dev/ino/size/mtime/ctime) and re-hashes when that + * changes. The omp plugin lives inside the long-running agent host; a + * path-only cache kept the first build's digest across an in-place update + * (`npm i -g` renames the new dist over the old path), and #4125 then + * stamped every respawned child with it — same code, different registered + * fingerprints, no attach, a laddered lane port per respawn. */ +const scriptFingerprints = new Map(); export function entryScriptFingerprint(scriptPath?: string): string | undefined { if (!scriptPath) return undefined; const resolved = path.resolve(scriptPath); - const cached = scriptFingerprints.get(resolved); - if (cached !== undefined) return cached; + let identity: string; let digest: string; try { + const stat = fs.statSync(resolved); + identity = `${stat.dev}:${stat.ino}:${stat.size}:${stat.mtimeMs}:${stat.ctimeMs}`; + const cached = scriptFingerprints.get(resolved); + if (cached?.identity === identity) return cached.digest; digest = createHash("sha256").update(fs.readFileSync(resolved)).digest("hex"); } catch { + scriptFingerprints.delete(resolved); return undefined; } - scriptFingerprints.set(resolved, digest); + scriptFingerprints.set(resolved, { identity, digest }); return digest; } diff --git a/tests/issue5354-fingerprint-follows-the-file.test.ts b/tests/issue5354-fingerprint-follows-the-file.test.ts new file mode 100644 index 0000000000..567e495c02 --- /dev/null +++ b/tests/issue5354-fingerprint-follows-the-file.test.ts @@ -0,0 +1,48 @@ +// #5354: entryScriptFingerprint() cached a script's digest by path for the +// life of the process. The omp plugin lives inside the long-running agent +// host, so after an in-place update (`npm i -g` renames the new dist over the +// old path) the host kept the FIRST build's digest — and #4125 then stamped +// every child it respawned with it (BILI_SPAWN_FINGERPRINT). Children running +// the same new code registered different fingerprints, could not attach to +// one another (code-fingerprint-mismatch), and each respawn laddered onto the +// next lane port. The digest must follow the file on disk, not the process. +import { test, after } from "node:test"; +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { rmrf } from "./tmp-rm.ts"; +import { entryScriptFingerprint } from "../src/instance.ts"; + +const DIR = fs.mkdtempSync(path.join(os.tmpdir(), "bili-5354-fp-")); +after(() => rmrf(DIR)); + +const sha256 = (text: string): string => createHash("sha256").update(text).digest("hex"); + +function script(name: string, text: string): string { + const file = path.join(DIR, name); + fs.writeFileSync(file, text); + assert.equal(entryScriptFingerprint(file), sha256(text), "first read hashes the bytes on disk"); + return file; +} + +test("a script replaced by rename — how npm installs an update — yields the new build's digest", () => { + const file = script("rename.js", "// build A\n"); + const staged = `${file}.new`; + fs.writeFileSync(staged, "// build B\n"); + fs.renameSync(staged, file); + assert.equal(entryScriptFingerprint(file), sha256("// build B\n")); +}); + +test("a script rewritten in place yields the new build's digest", () => { + const file = script("in-place.js", "// build A\n"); + fs.writeFileSync(file, "// build B, a longer one\n"); + assert.equal(entryScriptFingerprint(file), sha256("// build B, a longer one\n")); +}); + +test("a script removed after it was fingerprinted is unreadable → undefined, never the old digest", () => { + const file = script("removed.js", "// build A\n"); + fs.rmSync(file); + assert.equal(entryScriptFingerprint(file), undefined); +}); From 962a1450751d3e0837a11d999104236dcf62a708 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 21 Nov 2026 20:58:12 +0800 Subject: [PATCH 2/2] test(#5608): clear stale "old" chain from shared affinity tracker (Windows-CI race) The #5608 wiring e2e test re-hydrates its "old" chain into the module-singleton prefixAffinity tracker in phase 2 but only forgot "new" on cleanup, leaving "old" behind (violating its own "leave no chain behind" comment). On the slow Windows runner a persist/hydration interleaving let that stale depth-8 "old" win the next test's phase-2 view, so the equal-depth replay was classified as a prefix-break and the "equal-depth shape" log never fired (CI red: windows-22 shard 4). - wiring test: forget "old" in cleanup (root cause) - gate test: drop any stale "old" at start (defense in depth) --- tests/resume-miss-timestamp-seed.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/resume-miss-timestamp-seed.test.ts b/tests/resume-miss-timestamp-seed.test.ts index c73731bd74..12a0dba6a2 100644 --- a/tests/resume-miss-timestamp-seed.test.ts +++ b/tests/resume-miss-timestamp-seed.test.ts @@ -275,6 +275,7 @@ test("resume-inheritance MISS seeds the timestamp ledger from the closest tracke store2.closeQuietly(); // shared singleton hygiene: leave no chain behind for later tests in this file prefixAffinity.forget("new"); + prefixAffinity.forget("old"); await closeServers([proxy2, upstream], "miss-seed phase 2"); if (prevStampEnv === undefined) delete process.env.BILI_MESSAGE_TIMESTAMPS; else process.env.BILI_MESSAGE_TIMESTAMPS = prevStampEnv; @@ -334,6 +335,10 @@ test("weak-adoption gate: equal-depth replays and truncated subviews start FRESH const prevStampEnv = process.env.BILI_MESSAGE_TIMESTAMPS; delete process.env.BILI_MESSAGE_TIMESTAMPS; _resetSessionsForTest(); + // Defense in depth: drop any stale "old" chain a previous test left in the + // shared singleton tracker so this test's phase-1 chain is the only "old" + // the phase-2 hydration can see (the #5608 Windows-CI stale-tracker race). + prefixAffinity.forget("old"); // Ten distinct messages: the miss diagnostics only judge chains at or above // MIN_RESUME_PREFIX (8) — the truncated subview below stays just above that // floor so it still produces a closest candidate.