From 4e7ac095197f7d88ea21fb07d356d225bbffe477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=B4=E7=8C=AB?= Date: Thu, 12 Nov 2026 05:33:50 +0900 Subject: [PATCH] fix(update): run the boot smoke when the dsh desktop copy updates itself in place (#2082) The #2082 boot smoke (PR #2091) was wired into refreshDshDesktopCopy only, the lane where another bili process refreshes the dsh desktop profile copy. When the running proxy IS that copy, hostManagedInstall exempts the layout (#1575) and the copy updates itself through the plain self-update lanes: checkForUpdate (periodic check, `bili update`) and forceInstallVersion (advisory force-install, web-panel install). Both called installViaTarball without bootSmoke, so a tarball whose entry parses but throws at load was swapped into the copy dsh respawns its plugin from. Arm the smoke on both lanes when the install dir is the dsh desktop copy. Other install layouts keep their current behaviour. --- CHANGELOG.md | 1 + src/update.ts | 25 ++- .../issue2082-self-update-boot-smoke.test.ts | 212 ++++++++++++++++++ 3 files changed, 236 insertions(+), 2 deletions(-) create mode 100644 tests/issue2082-self-update-boot-smoke.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 710ce2fabe..a9ed4548b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,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 +- **dsh 桌面副本自己更新自己时也跑 boot smoke(#2082 后续)**:#2091 的 boot smoke 门只接在 `refreshDshDesktopCopy` 上(别的 bili 进程替桌面副本刷新的那条路)。代理本身就从桌面副本运行时,`hostManagedInstall` 按 #1575 放行,走的是普通自更新路径——周期检查 / `bili update`(`checkForUpdate`)和 advisory 强制安装 / 面板安装(`forceInstallVersion`)——这两处调 `installViaTarball` 没带 `bootSmoke`,入口能解析但加载即抛错的包只过 `node --check` 就被换上去,正是这道门要挡的状态。现在两条路径在安装目录是 dsh 桌面副本时同样启用 boot smoke,失败回滚到旧副本;其他安装布局行为不变。 - **压缩树图:节点标题不再一闪即逝,点击压缩块能真正看到被压缩的消息内容**:此前树节点只有原生 SVG `` 悬停提示——选中节点或 5s 轮询重画时 DOM 一变提示就消失,实测「显示一下马上消失」;右侧 Block details 里块的消息行虽带消息编号(mNNNNN)但内容全是一句「消息已不在会话中」(宿主原生压缩把消息从上下文剪掉后,字节无处可取)。现在:① 悬停提示改为页面内常驻浮层(.g-tip)——跟随指针、显示完整块号/tier/消息范围/完整主题/tokens/消息数,选中节点或轮询重画都不再打断,离开图形区才隐藏;悬停时节点卡片上的主题行同时原地扩宽(~20 字 → 20 字上限的完整截断,离开复原);② 5s 轮询只回传瘦节点(无主题/摘要/tokens),重画前先与详情富块按 blockId 合并——节点标题、浮层元信息、消息数不再在每次 tick 后退化成光秃秃的块号;③ 服务端新增**冻结快照水合**:压缩块落盘时已把原始消息逐条存进 blockContents(`[role]`/`[role • tool]` 头 + 原文,pi 宿主 user 消息还带 mNNNNN 标记行)——水合按**标记锚定**对齐:pi 宿主打进 user 消息正文的 mNNNNN 标记是身份戳,每个标记用两条独立证据之一锚死——**恒等锚**(标记号=该位当前 ref,从未被重编号)或**冻结 span 算术锚**(标记的冻结编号 − 块冻结 span 起点 = 快照位序;span 与标记同批写入,冷加载 restamp(#5196 parked 段换新号)重编了整块 ref 也照样成立——实测 b27 全块 127 条因此恢复,旧规则因标记号已不在 ref 表里整块拒水合);锚点之间的段仅在条数平衡时按位 zip。标记既非恒等也非算术锚(冻结上下文混入外来消息/span 无法证明)、锚点乱序、无锚非叶块(快照 span 的是 effective 超集)一律拒绝,未证明的位置保留诚实的 missing 标记——宁可不显示,不冒错位张冠李戴的风险(预览 + 点击展开取原文,`/message?ref=` 同享该层)。实测大会话(253 块/9771 个直接消息引用):7789 条(80%)恢复出真实预览与原文,其余保持 missing。回归:tests/web-blocks-tree.test.ts 冻结快照水合 7 用例(按位水合 / 条数不符拒 / 标记移位旧锚 / 宿主剪块中消息的锚定恢复 / restamp 整块恢复 / 算术锚优先于巧合 ref 碰撞 / 无锚标记整块拒(含 click-to-open 双向断言)),源码级断言更新为合并后重画(renderCollapSvg(merged)。 - **交接文档「Compressed block summaries」附录不再被吞进最后一条消息卡片**:客户端 handoff 渲染器只认 `### user/assistant/tool` 三种角色分隔线(#2065 语义,正文内其他 `###` 标题是内容),export 端 #845 追加在对话之后的 `## Compressed block summaries` 整节(长会话可达 80+ 个 `### Block bN` 条目、数百行)因此全部落进最后一个 assistant 卡片,看起来像「压缩消息被追加成一条巨型消息」。现在客户端把 Conversation 之后的 `## ` 级标题识别为独立分节:`### Block bN — topic` 逐条渲染为小块卡片(mono 块号 + 主题截断标题 + tier/token 元行 + Markdown 正文),角色卡片只保留真实对话。回归:tests/web-handoff-roles.test.ts 新增附录分节用例(角色卡片数不膨胀、条目内容保真、#2065 不变量不受影响)。 - **会话历史的「条数」维度与超大未压缩请求:按真实维度拦下,判死信息点名超重字段(#5000 #5794 #5885)**:#5000 的 26,903 条消息会话(55.2MiB)穿过了**所有**字节/token 闸门、在上游死于消息条数上限——因为条数这个维度从来没有任何闸(它无法被任何字节/token 估算表达:每条消息还各带 ~69B 的 m-ref 标记,26,903 条 ≈ 1.8MB 纯标记)。现在新增按上游配置的**条数闸**(`network.maxMessages` / `BILI_MAX_MESSAGES`,默认 0=关闭——不存在全局「通用上限」,条数上限因上游而异):超限直接 413,信息点名 COUNT 维度并给出可执行指引(重启会话或回退到更早的时点;折叠在一轮 preflight 内关不掉 ~20K 条的缺口,所以是纯判死、不做条数折叠),且在 token 触发器**之前**运行——token 达标但条数超标的体(正是 #5000 的形状)也只有这道闸看得见。同时所有超窗判死信息(#5932 的三道计量闸)现在**点名超重的是哪个字段**——按 #554 计量器同源分桶(messages / tools[] / system / other),不再只给一个总数:#5000 的形状会显示 `tools[] ~X`,巨大粘贴/工具结果会显示 `messages ~X`,运维一眼知道该删什么。另新增 **32MiB 解码摄入成本带**(`network.ingestBodyBytes` / `BILI_INGEST_BODY_BYTES`):#5794 的 51.2MiB/29,619 条请求每轮独占事件循环 5.5–7.2s(本机 CPU 54.6% → 747s FATAL 自重启、全员缓存冷),成本带在 JSON 解析扫掠**之前**按解码后字节 413(stage=ingest)——任何可解析的**文本为主**的 32MiB+ 体都已是 ≥~8M token,超出所有真实模型窗口,放行必然烧钱再死。带图片的体在所有车道豁免(base64 像素按设计不进 token 计量——#496/#1800/#857「图片单独计费、绝不在原始字节上判死」;#3909 的 ~48MiB 图片密集 chat 会话与 #3701 的 ~38.5MiB WS 截图体都是受支持工作负载,由槽位预算/收件上限/车道自身的预发送字节预算+丢弃逻辑治理;Responses 车道无论如何整体豁免);并发槽位预算与 5GiB 收件上限继续独立生效。回归:tests/issue5000-count-gate(3 e2e:token 达标+条数超标→413 零上游 / 无 model 路径 / 条数达标照常转发)、tests/issue5794-ingest-cap(3 e2e:超带文本→413 stage ingest / 带内照常 / 超带带图不拦)、tests/side-request-isolation 分桶计量单测;既有 332 用例(含 #3701/#857/#496/#4079/#4185/#2490 全部回归面)全绿。 diff --git a/src/update.ts b/src/update.ts index 5a7b28e828..2886deeb12 100644 --- a/src/update.ts +++ b/src/update.ts @@ -776,6 +776,23 @@ function isDshDesktopBiliCopy(installDir: string, real: string, env: NodeJS.Proc return dshDesktopCopyPathMatches(installDir, real, anchor, `/node_modules/${DSH_PACKAGE}`, process.platform === "win32"); } +/** #2082: true when `installDir` is the dsh DESKTOP profile copy that bili + * owns in place (#1575). The self-update lanes use it to arm the boot smoke: + * a proxy running FROM that copy takes the plain self-update path + * (hostManagedInstall exempts the layout), yet it swaps the very copy a + * running dsh respawns its plugin from — the same exposure + * refreshDshDesktopCopy gates. */ +function isDshDesktopInstall(installDir: string | undefined, env: NodeJS.ProcessEnv = process.env): boolean { + if (!installDir) return false; + let real = installDir; + try { + real = realpathSync(installDir); + } catch { + // nonexistent or unreadable — evaluate the literal path + } + return isDshDesktopBiliCopy(installDir, real, env); +} + /** #1234/#2199: true when `installDir` is the OpenCode v2 plugin-manager CACHE * copy of billion-context — under $XDG_CACHE_HOME/opencode (default * ~/.cache/opencode) at .../node_modules/billion-context. A bare @@ -1740,7 +1757,10 @@ export async function checkForUpdate(opts: UpdateOptions, force = false, checkOn return; } try { - const result = await installViaTarball(target, tarballUrl, installDir, integrity, shasum, egressDispatcher(opts, tarballUrl)); + // #2082: when this proxy runs FROM the dsh desktop copy, the + // self-update swaps the copy dsh respawns from — same boot smoke + // as the refreshDshDesktopCopy lane. + const result = await installViaTarball(target, tarballUrl, installDir, integrity, shasum, egressDispatcher(opts, tarballUrl), process.env, { bootSmoke: isDshDesktopInstall(installDir) }); if (result.ok) { await clearInstallBackoff(bkey); await persistLastInstall(currentVersion, target, force ? "manual" : "auto-update"); @@ -2268,7 +2288,8 @@ export async function forceInstallVersion( // install between the pre-lock read and lock acquisition. const diskUnderLock = await readDiskVersion(installDir); if (diskUnderLock === targetVersion) return { ok: true }; - const result = await installViaTarball(targetVersion, doc.tarball, installDir, doc.integrity, doc.shasum, egressDispatcher(opts, doc.tarball)); + // #2082: same boot smoke as the other lanes when the target is the dsh desktop copy. + const result = await installViaTarball(targetVersion, doc.tarball, installDir, doc.integrity, doc.shasum, egressDispatcher(opts, doc.tarball), process.env, { bootSmoke: isDshDesktopInstall(installDir) }); if (result.ok) { const channel = installSource ?? `advisory:${advisoryId}`; await persistLastInstall(diskUnderLock ?? opts.currentVersion, targetVersion, channel); diff --git a/tests/issue2082-self-update-boot-smoke.test.ts b/tests/issue2082-self-update-boot-smoke.test.ts new file mode 100644 index 0000000000..53afc3e187 --- /dev/null +++ b/tests/issue2082-self-update-boot-smoke.test.ts @@ -0,0 +1,212 @@ +// #2082 follow-up: the boot smoke was wired into refreshDshDesktopCopy only — +// the lane where ANOTHER bili process refreshes the dsh desktop profile copy. +// When the running proxy IS the desktop copy, hostManagedInstall exempts the +// layout (#1575) and the copy updates itself through the plain self-update +// lanes: checkForUpdate (periodic / `bili update`) and forceInstallVersion +// (advisory force-install, web panel install). Those called installViaTarball +// without bootSmoke, so a tarball whose entry parses but throws at load was +// swapped in under the running dsh — exactly the state the gate exists to +// prevent. These pin that both self lanes arm the smoke when (and only when) +// the install dir is the dsh desktop copy. +// +// CHECK_INTERVAL_MS / THROTTLE_FILE / BACKOFF_STATE_FILE / ... are frozen at +// import time, so the cache-dir override MUST be set before loading +// src/update.ts (same discipline as issue3754-install-attribution.test.ts). +// DSH_HOME points at a per-file tree so the desktop-copy path check resolves +// inside the fixture and never at this machine's real dsh home. + +import { test, after } from "node:test"; +import assert from "node:assert/strict"; +import crypto from "node:crypto"; +import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, readdirSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import * as tar from "tar"; +import { rmrf } from "./tmp-rm.ts"; + +const root = mkdtempSync(path.join(tmpdir(), "bc-2082-self-")); +process.env.XDG_CACHE_HOME = path.join(root, "cache"); +process.env.DSH_HOME = path.join(root, "dsh-home"); +process.env.BILI_UPDATE_POLICY_URL = "http://127.0.0.1:1/no-policy.json"; +process.env.BILI_UPDATE_STAGGER_MIN_MS = "0"; +process.env.BILI_UPDATE_STAGGER_MAX_MS = "0"; +const { + checkForUpdate, + forceInstallVersion, + getLastInstall, + readDiskVersion, + registryUrlFor, + _resetAdvisoryRefusalWarnsForTest, + _resetInstallBackoffForTest, + _resetLastInstallForTest, + _resetPolicyGateForTest, + _resetUpdateThrottleForTest, +} = await import("../src/update.ts"); + +after(() => { + rmrf(root); + delete process.env.XDG_CACHE_HOME; + delete process.env.DSH_HOME; + delete process.env.BILI_UPDATE_POLICY_URL; + delete process.env.BILI_UPDATE_STAGGER_MIN_MS; + delete process.env.BILI_UPDATE_STAGGER_MAX_MS; +}); + +const DESKTOP_COPY = path.join(root, "dsh-home", "profiles", "desktop", "node_modules", "billion-context"); + +const goodEntry = (version: string): string => `export const loaded = '${version}';\n`; +const BOOT_BROKEN_ENTRY = 'throw new Error("boom at boot (#2082 self-lane fixture)");\n'; + +function integrityField(buf: Buffer): string { + return `sha512-${crypto.createHash("sha512").update(buf).digest("base64")}`; +} + +function writePkg(dir: string, version: string, entry: string): void { + mkdirSync(path.join(dir, "dist"), { recursive: true }); + writeFileSync( + path.join(dir, "package.json"), + JSON.stringify({ name: "billion-context", version, type: "module", main: "dist/index.js", bin: { bili: "./dist/index.js" } }), + ); + writeFileSync(path.join(dir, "dist", "index.js"), entry); +} + +/** (Re)seed the dsh desktop profile copy as a real directory at `version` — + * the shape it has once bili owns it in place (#1575). */ +function seedDesktopCopy(version: string): string { + rmrf(DESKTOP_COPY); + writePkg(DESKTOP_COPY, version, goodEntry(version)); + return DESKTOP_COPY; +} + +/** A plain (non-dsh) install dir, like a global npm install. */ +function seedPlainInstall(version: string, label: string): string { + const dir = path.join(root, `plain-${label}`, "node_modules", "billion-context"); + writePkg(dir, version, goodEntry(version)); + return dir; +} + +interface Pkg { docUrl: string; tarballUrl: string; doc: object; tgz: Buffer; } + +let pkgSeq = 0; + +/** Registry doc + tarball pair for one published version. `tag` re-keys the + * doc under a dist-tag URL (checkForUpdate follows the dist-tag channel); + * omitted = the exact-version doc forceInstallVersion resolves. */ +function makeRegistry(version: string, entry: string, tag?: string): Pkg { + const src = path.join(root, `pkg-src-${++pkgSeq}`); + writePkg(path.join(src, "package"), version, entry); + const tgzPath = path.join(root, `pkg-${pkgSeq}.tgz`); + tar.c({ cwd: src, file: tgzPath, gzip: true, sync: true }, ["package"]); + const tgz = readFileSync(tgzPath); + const tarballUrl = `https://registry.test/${pkgSeq}/billion-context-${version}.tgz`; + return { + docUrl: registryUrlFor("billion-context", tag ?? version), + tarballUrl, + doc: { version, dist: { tarball: tarballUrl, integrity: integrityField(tgz) } }, + tgz, + }; +} + +/** Serve the crafted registry through global fetch. Anything else (the + * unroutable policy feed) is rejected in-process — the policy gate fails + * open — so the file never opens a socket. */ +async function withRegistryFetch(pkgs: Pkg[], fn: () => Promise<void>): Promise<void> { + const original = globalThis.fetch; + globalThis.fetch = ((input: unknown) => { + const url = String(input); + for (const p of pkgs) { + if (url === p.docUrl) return Promise.resolve(new Response(JSON.stringify(p.doc), { status: 200, headers: { "content-type": "application/json" } })); + if (url === p.tarballUrl) return Promise.resolve(new Response(p.tgz)); + } + return Promise.reject(new Error(`unexpected fetch in test: ${url}`)); + }) as unknown as typeof fetch; + try { + await fn(); + } finally { + globalThis.fetch = original; + } +} + +async function resetUpdaterState(): Promise<void> { + await _resetUpdateThrottleForTest(); + await _resetInstallBackoffForTest(); + await _resetLastInstallForTest(); + _resetPolicyGateForTest(); + _resetAdvisoryRefusalWarnsForTest(); +} + +function assertNoUpdateLitter(installDir: string): void { + for (const entry of readdirSync(path.dirname(installDir))) { + assert.equal(entry, path.basename(installDir), `leftover next to the install dir: ${entry}`); + } +} + +test("checkForUpdate from the dsh desktop copy: a boot-broken tarball is rolled back, not swapped in (#2082)", { timeout: 60_000 }, async () => { + await resetUpdaterState(); + const installDir = seedDesktopCopy("1.2.3"); + const pkgs = [makeRegistry("2.0.0", BOOT_BROKEN_ENTRY, "latest")]; + await withRegistryFetch(pkgs, async () => { + await checkForUpdate({ packageName: "billion-context", currentVersion: "1.2.3", autoUpdate: true, installDir }, false); + }); + assert.equal(await readDiskVersion(installDir), "1.2.3", "the working desktop copy must survive a boot-broken tarball"); + assert.equal(readFileSync(path.join(installDir, "dist", "index.js"), "utf8"), goodEntry("1.2.3")); + assert.equal(await getLastInstall(), undefined, "a rolled-back install must not be attributed as installed"); + assertNoUpdateLitter(installDir); +}); + +test("checkForUpdate (forced, `bili update`) from the dsh desktop copy: same rollback (#2082)", { timeout: 60_000 }, async () => { + await resetUpdaterState(); + const installDir = seedDesktopCopy("1.2.3"); + const pkgs = [makeRegistry("2.0.0", BOOT_BROKEN_ENTRY, "latest")]; + await withRegistryFetch(pkgs, async () => { + await checkForUpdate({ packageName: "billion-context", currentVersion: "1.2.3", autoUpdate: false, installDir }, true); + }); + assert.equal(await readDiskVersion(installDir), "1.2.3"); + assert.equal(readFileSync(path.join(installDir, "dist", "index.js"), "utf8"), goodEntry("1.2.3")); + assert.equal(await getLastInstall(), undefined); +}); + +test("forceInstallVersion onto the dsh desktop copy: a boot-broken tarball fails the install and rolls back (#2082)", { timeout: 60_000 }, async () => { + await resetUpdaterState(); + const installDir = seedDesktopCopy("2.0.0"); + const pkgs = [makeRegistry("1.9.0", BOOT_BROKEN_ENTRY)]; + await withRegistryFetch(pkgs, async () => { + const r = await forceInstallVersion("1.9.0", installDir, { packageName: "billion-context", currentVersion: "2.0.0", autoUpdate: true }, "adv-2082"); + assert.equal(r.ok, false, "a boot-broken artifact must not install"); + assert.match(r.error ?? "", /boot smoke/); + assert.match(r.error ?? "", /boom at boot/); + }); + assert.equal(await readDiskVersion(installDir), "2.0.0", "the working desktop copy must survive a boot-broken tarball"); + assert.equal(readFileSync(path.join(installDir, "dist", "index.js"), "utf8"), goodEntry("2.0.0")); + assert.equal(await getLastInstall(), undefined); + assertNoUpdateLitter(installDir); +}); + +test("self-update of the dsh desktop copy: a loadable tarball passes the smoke and lands on both lanes (#2082)", { timeout: 60_000 }, async () => { + await resetUpdaterState(); + const installDir = seedDesktopCopy("1.2.3"); + await withRegistryFetch([makeRegistry("2.0.0", goodEntry("2.0.0"), "latest")], async () => { + await checkForUpdate({ packageName: "billion-context", currentVersion: "1.2.3", autoUpdate: true, installDir }, false); + }); + assert.equal(await readDiskVersion(installDir), "2.0.0"); + assert.equal((await getLastInstall())?.source, "auto-update"); + + await withRegistryFetch([makeRegistry("1.9.0", goodEntry("1.9.0"))], async () => { + const r = await forceInstallVersion("1.9.0", installDir, { packageName: "billion-context", currentVersion: "2.0.0", autoUpdate: true }, "adv-2082-ok"); + assert.equal(r.ok, true, r.error); + }); + assert.equal(await readDiskVersion(installDir), "1.9.0"); + assert.equal((await getLastInstall())?.source, "advisory:adv-2082-ok"); +}); + +test("self-update outside the dsh desktop copy keeps the parse-only verification (scope pin for #2082)", { timeout: 60_000 }, async () => { + // The smoke is armed for the desktop copy only; a plain install keeps its + // existing behaviour. This pins the scope of the change, not a claim that + // parse-only verification is sufficient there. + await resetUpdaterState(); + const installDir = seedPlainInstall("1.2.3", "auto"); + await withRegistryFetch([makeRegistry("2.0.0", BOOT_BROKEN_ENTRY, "latest")], async () => { + await checkForUpdate({ packageName: "billion-context", currentVersion: "1.2.3", autoUpdate: true, installDir }, false); + }); + assert.equal(await readDiskVersion(installDir), "2.0.0"); +});