From 3518c2f175e3d98f03efe9b4b51f4b96195e6587 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Fri, 30 Oct 2026 22:12:57 +0800 Subject: [PATCH 1/4] tests: pin the startup contract surface before the adapter extraction (#5241 phase 0) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Phase-0 characterization stakes for umbrella #5241 — tests only, zero product changes. They freeze TODAY's behavior (including the inconsistencies) so the phase-1 lifecycle extraction has an explicit equivalence net: - contract-v2-stake-verify-attach: the four verifyAttachAndRecover copies (pi/omp/opencode/dsh) answer a common scenario matrix (healthy equal-version / strictly-older / dead target). Pins the S2 divergence: pi/omp/opencode attach an older resident untouched, dsh rotates+respawns (already pinned by issue3544 suite). Each lane runs in its own child process (contract-v2-stake-lane-probe.mts) — the four lanes share the host process's global fetch/env/module state and pollute each other when driven in-process, which is itself part of the characterization. - contract-v2-stake-latches: the two one-way latches. shadow stats.disabled has NO in-process recovery (healthy records after the latch are permanent no-ops — the C3 target); logger stderrDead flips once, warns once, and never touches stderr again (driven in a child process because the latch owns the whole process lifetime). - contract-v2-stake-health: /__bili/health answers exactly 503 {ok,reason:"starting"} pre-boot and an 11-field snapshot post-boot — no lifecycle/degradation vocabulary exists yet (the C1 gap, pinned so adding fields is a visible, deliberate change). --- tests/contract-v2-stake-health.test.ts | 97 ++++++++++ tests/contract-v2-stake-lane-probe.mts | 144 +++++++++++++++ tests/contract-v2-stake-latches.test.ts | 168 ++++++++++++++++++ tests/contract-v2-stake-verify-attach.test.ts | 108 +++++++++++ 4 files changed, 517 insertions(+) create mode 100644 tests/contract-v2-stake-health.test.ts create mode 100644 tests/contract-v2-stake-lane-probe.mts create mode 100644 tests/contract-v2-stake-latches.test.ts create mode 100644 tests/contract-v2-stake-verify-attach.test.ts diff --git a/tests/contract-v2-stake-health.test.ts b/tests/contract-v2-stake-health.test.ts new file mode 100644 index 0000000000..edef7c0ea0 --- /dev/null +++ b/tests/contract-v2-stake-health.test.ts @@ -0,0 +1,97 @@ +/** + * #5241 阶段 0 特征化打桩(不改产品代码):/__bili/health 的应答词汇表 + * —— C1 契约(生命周期单源、健康度可表达)改造前的现状基准。 + * + * 现状只有两个状态:bootReady 之前 503 {ok:false, reason:"starting"}, + * 之后 200 全量快照。没有 degraded / degraded-reason / recovery-etf 字段 + * —— sidecar 被闩死、stderr 被闩死、lane 降级直发,这些都发生在进程 + * 内部,health 一个字都不说。这正是 C1 要改的:桩先把这个「不可表达」 + * 钉死,阶段 1 加字段时本桩的键集断言必须同步更新(有意为之的账本)。 + * + * 客户端三态探测(alive/busy/dead,src/liveness.ts)已有专项桩 + * tests/issue3654-health-probe-three-state.test.ts,不在此重钉。 + */ + +import { test, before, after } from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import http from "node:http"; +import { handleAdminRoute } from "../src/server/admin.ts"; +import { VERSION } from "../src/version.ts"; + +const HERMETIC = fs.mkdtempSync(path.join(os.tmpdir(), "bili-c2-health-")); +process.env.BILI_CONFIG_FILE = path.join(HERMETIC, "no-config.json"); + +after(() => { + fs.rmSync(HERMETIC, { recursive: true, force: true }); +}); + +type AdminCtxLike = Parameters[2]; + +/** The health branch only reads these fields (auxProxyShot resolves inside a + * try/catch, so a minimal opts is safe). Everything else gets a stub. */ +function makeCtx(bootReady: boolean): AdminCtxLike { + return { + opts: { upstream: "https://api.example.invalid" }, + core: {}, + config: {}, + log: () => {}, + instanceId: "stake-instance", + instanceStartedAt: 1_710_000_000_000, + proxyWatchers: new Set([4242]), + initialWatcherPid: 4242, + watcherLastSeen: new Map(), + bootReady: () => bootReady, + } as unknown as AdminCtxLike; +} + +async function getHealth(bootReady: boolean): Promise<{ status: number; body: Record }> { + const ctx = makeCtx(bootReady); + const server = http.createServer((req, res) => { + void handleAdminRoute(req, res, ctx).catch(() => { + if (!res.headersSent) { res.statusCode = 500; res.end("{}"); } + }); + }); + const origin = await new Promise((resolve) => { + server.listen(0, "127.0.0.1", () => { + const addr = server.address() as { port: number }; + resolve(`http://127.0.0.1:${addr.port}`); + }); + }); + try { + const res = await fetch(`${origin}/__bili/health`); + return { status: res.status, body: (await res.json()) as Record }; + } finally { + await new Promise((r) => server.close(() => r())); + } +} + +test("pre-boot /__bili/health: exactly 503 {ok:false, reason:\"starting\"} — nothing else is expressible yet", async () => { + const { status, body } = await getHealth(false); + assert.equal(status, 503); + assert.deepEqual(Object.keys(body).sort(), ["ok", "reason"], "the starting answer carries exactly two keys"); + assert.deepEqual(body, { ok: false, reason: "starting" }); +}); + +test("post-boot /__bili/health: exactly the 11-field snapshot — no lifecycle/degradation vocabulary exists today", async () => { + const { status, body } = await getHealth(true); + assert.equal(status, 200); + assert.deepEqual( + Object.keys(body).sort(), + ["auxProxy", "auxProxySource", "blindTunnels", "commit", "instanceId", "ok", "pid", "startedAt", "upstream", "version", "watchdog"], + "the ready answer carries exactly the current field set (C1: adding lifecycle fields is a deliberate, visible change)", + ); + assert.equal(body.ok, true); + assert.equal(body.version, VERSION); + assert.equal(body.instanceId, "stake-instance"); + assert.equal(body.startedAt, 1_710_000_000_000); + assert.equal(body.pid, process.pid, "in-process stub server: the pid is this test process"); + assert.equal(body.upstream, "https://api.example.invalid"); + // The C1 gap, pinned explicitly: today's payload CANNOT say degraded. + for (const absent of ["lifecycle", "state", "mode", "degraded", "degradations", "recoveryEtaMs", "reason"]) { + assert.equal(absent in body, false, `no "${absent}" field exists in today's health vocabulary`); + } + assert.deepEqual(body.watchdog, { armed: true, parentPid: 4242, watchers: [4242] }, "#1322 watchdog shape pinned as-is"); +}); diff --git a/tests/contract-v2-stake-lane-probe.mts b/tests/contract-v2-stake-lane-probe.mts new file mode 100644 index 0000000000..24635cd055 --- /dev/null +++ b/tests/contract-v2-stake-lane-probe.mts @@ -0,0 +1,144 @@ +/** + * #5241 阶段 0 打桩配套探针:在「干净子进程」里驱动单条宿主车道的 + * verifyAttachAndRecover,把结果以一行 JSON 报告给父测试 + * (tests/contract-v2-stake-verify-attach.test.ts)。 + * + * 为什么子进程:四条车道共享同一个进程的 global fetch、env 和模块级 + * 状态,同进程顺序驱动会互相污染(实测:一条车道的后台异步会改写另一条 + * 车道的 env/spawn 计数)。生产里每条车道都在自己的宿主进程里启动, + * 子进程隔离就是被测现实本身。 + * + * 用法: node --import tsx contract-v2-stake-lane-probe.mts + * lane = pi | omp | opencode | dsh + * scenario = s1 (healthy equal-version mock) + * | s2 (strictly older mock) + * | s3 (dead target, port 9) + * 输出: 单行 JSON { lane, scenario, landed, spawns, shutdowns, envAfter, registerBase } + */ + +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import http from "node:http"; + +const HERMETIC = fs.mkdtempSync(path.join(os.tmpdir(), "bili-c2-probe-")); +process.env.BILI_CONFIG_FILE = path.join(HERMETIC, "no-config.json"); +process.env.NODE_TEST_CONTEXT = process.env.NODE_TEST_CONTEXT ?? "contract-stake"; +process.env.BILI_ATTACH_EVIDENCE_GRACE_MS = "30"; +process.env.BILI_ATTACH_HEALTH_DEADLINE_MS = "150"; +delete process.env.BILLION_CONTEXT_ATTACH; +delete process.env.BILLION_CONTEXT_NATIVE; +delete process.env.BILLION_CONTEXT_PROXY; + +const [laneName, scenario] = process.argv.slice(2) as [string, string]; +if (laneName === undefined || scenario === undefined) { + process.stderr.write("usage: lane-probe.mts \n"); + process.exit(2); +} + +function startMockBili(version: string): Promise<{ origin: string; shutdowns: () => number }> { + let shutdowns = 0; + const server = http.createServer((req, res) => { + const url = req.url ?? ""; + if (req.method === "GET" && url === "/__bili/plugin/manifest") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ version, tools: {} })); + return; + } + if (req.method === "POST" && url === "/__bili/shutdown") { + shutdowns += 1; + res.writeHead(200, { "content-type": "application/json" }); + res.end("{}"); + return; + } + res.writeHead(404, { "content-type": "application/json" }); + res.end("{}"); + }); + return new Promise((resolve) => { + server.listen(0, "127.0.0.1", () => { + const addr = server.address() as { port: number }; + resolve({ origin: `http://127.0.0.1:${addr.port}`, shutdowns: () => shutdowns }); + }); + }); +} + +const ni = await import("../src/agent/native-intercept.ts"); +const { VERSION } = await import("../src/version.ts"); +const counter = { n: 0 }; +const countingSpawn = async (): Promise => { + counter.n += 1; + return "http://127.0.0.1:7777"; +}; + +let mock: { origin: string; shutdowns: () => number } | undefined; +let origin: string; +if (scenario === "s1") { mock = await startMockBili(VERSION); origin = mock.origin; } +else if (scenario === "s2") { mock = await startMockBili("0.0.1"); origin = mock.origin; } +else { origin = "http://127.0.0.1:9"; } + +let landed: string | undefined; +let registerBase: string | undefined | "unset"; + +if (laneName === "pi") { + const pi = await import("../src/agent/pi-native.ts"); + ni._resetForTest(); + pi._resetNativeStateForTest(); + pi._setSpawnForTest(countingSpawn); + pi.armNativePi({ mode: "attach", attachOrigin: origin }); + const respawn = pi._stateRespawnForTest(); + if (respawn === undefined) throw new Error("pi arm did not expose the respawn seam"); + landed = await respawn(); + pi._setSpawnForTest(undefined); +} else if (laneName === "omp") { + const omp = await import("../src/agent/omp-native.ts"); + ni._resetForTest(); + omp._resetNativeStateForTest(); + omp._setSpawnForTest(countingSpawn); + omp.armNativeOmp({ mode: "attach", attachOrigin: origin }); + const respawn = omp._stateRespawnForTest(); + if (respawn === undefined) throw new Error("omp arm did not expose the respawn seam"); + landed = await respawn(); + omp._setSpawnForTest(undefined); +} else if (laneName === "opencode") { + const oc = await import("../src/agent/opencode-native.ts"); + ni._resetForTest(); + oc._resetNativeStateForTest(); + oc._setSpawnForTest(countingSpawn); + landed = await oc.verifyAttachAndRecover(origin); + oc._setSpawnForTest(undefined); +} else if (laneName === "dsh") { + const dsh = await import("../src/agent/dsh-native.ts"); + ni._resetForTest(); + dsh._resetRoutedForTest(); + dsh._resetRegisterForTest(origin); + dsh._setSpawnForTest(countingSpawn); + // dsh rides a preset origin (issue3544 harness): verify must keep it on + // attach and UNFREE it on spawn fallback. + process.env.BILLION_CONTEXT_PROXY = origin; + process.env.DSH_HOME = path.join(HERMETIC, "dsh-home"); + process.env.XDG_STATE_HOME = path.join(HERMETIC, "dsh-state"); + landed = await dsh.verifyAttachAndRecover(origin); + registerBase = dsh._registerStateForTest().base ?? "unset"; + dsh._setSpawnForTest(undefined); +} else { + process.stderr.write(`unknown lane: ${laneName}\n`); + process.exit(2); +} + +// Capture right after the lane ran (no reset helpers in between — the +// reset helpers delete BILLION_CONTEXT_PROXY; process.exit handles timers). +const envAfter = process.env.BILLION_CONTEXT_PROXY ?? null; + +const report = { + lane: laneName, + scenario, + landed: landed ?? null, + spawns: counter.n, + shutdowns: mock?.shutdowns() ?? 0, + envAfter, + registerBase: registerBase ?? null, +}; +process.stdout.write(`\nPROBE ${JSON.stringify(report)}`); +// Force-exit: lane modules leave background timers (recovery/reprobe loops) +// that would otherwise hold the child open for their full TTL. +process.exit(0); diff --git a/tests/contract-v2-stake-latches.test.ts b/tests/contract-v2-stake-latches.test.ts new file mode 100644 index 0000000000..a35b9e5be9 --- /dev/null +++ b/tests/contract-v2-stake-latches.test.ts @@ -0,0 +1,168 @@ +/** + * #5241 阶段 0 特征化打桩(不改产品代码):两把「一次性闩锁」的永久性 + * —— C3 契约(降级必须有告警、可恢复、有 TTL)改造前的现状基准。 + * + * - shadow.ts stats.disabled:3 次结构性 ingest 失败闩死统一 sidecar。 + * issue5140 已钉「3 strike 才闩、闩后 warn 只一条」;这里补钉的是 + * C3 关心的另一半事实:**进程内不存在任何恢复路径** —— 故障消失后 + * record() 永久 no-op、读路径全部回退、fork 直接 throw,直到进程重启。 + * - logger.ts stderrDead:stderr 写失败(同步抛或流 error 事件)把 + * 整个进程的日志降级为 file-only,一次性、幂等、同样无复位。 + * markStderrDead 未导出且无 reset —— 进程内翻闩会污染同进程后续 + * 测试,所以这半边用子进程桩:子进程自己翻闩,把 capture 记录和 + * 「翻闩后 stderr 还写过几次」回报给父进程断言。 + * + * 阶段 2(闩锁治理)要改行为时,这两个桩就是「改了什么」的显式账本。 + */ + +import { test, before, after } from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { spawn } from "node:child_process"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { UnifiedShadow } from "../src/storage/shadow.ts"; +import { isStreamWriteError } from "../src/logger.ts"; +import type { LegacySessionLike } from "../src/storage/ingest.ts"; + +const HERMETIC = fs.mkdtempSync(path.join(os.tmpdir(), "bili-c2-latch-")); +process.env.BILI_CONFIG_FILE = path.join(HERMETIC, "no-config.json"); + +before(() => { + delete process.env.BILI_STORAGE_UNIFIED; + delete process.env.BILI_ENCRYPTION_KEY; +}); + +after(() => { + fs.rmSync(HERMETIC, { recursive: true, force: true }); +}); + +function mkShadow(): UnifiedShadow { + const dir = fs.mkdtempSync(path.join(HERMETIC, "sh-")); + const shadow = UnifiedShadow.maybeOpen(dir, { BILI_STORAGE_UNIFIED: "shadow" }, (r) => r as LegacySessionLike); + assert.ok(shadow !== null, "hermetic shadow must open (real sqlite engine present)"); + return shadow; +} + +let seq = 0; +function fix(): LegacySessionLike { + seq++; + return { + id: `latch-s${seq}`, + createdAt: 1_000, + lastSeen: 2_000 + seq, + meta: { protocol: "anthropic" }, + stats: { requests: 1 }, + metadata: {}, + state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} }, nextBlockId: 1, nextRunId: 1 }, + blockContents: new Map(), + lastMessages: [], + lastMessagesFolded: false, + } as unknown as LegacySessionLike; +} + +/** Same fault seam as issue5140: the verify leg's first read-back. */ +type SyncLike = { getBlocks: (id: string, opts?: { limit: number; offset?: number }) => unknown[] }; +function installFault(shadow: UnifiedShadow, message: string): () => void { + const sync = (shadow as unknown as { sync: SyncLike }).sync; + const orig = sync.getBlocks; + sync.getBlocks = () => { throw new Error(message); }; + return () => { sync.getBlocks = orig; }; +} + +test("shadow disable latch: once disabled, healthy records are permanent no-ops (no re-enable path exists)", () => { + const shadow = mkShadow(); + try { + const off = installFault(shadow, "file is not a database (stake: structural)"); + for (let i = 0; i < 3; i++) shadow.record(fix()); + off(); + assert.equal(shadow.stats.disabled, true, "three structural strikes latch the sidecar"); + const ingestsFrozen = shadow.stats.ingests; + const lastErr = String(shadow.stats.lastError); + assert.match(lastErr, /file is not a database/); + + // The fault is GONE now. Today there is no recovery: every later + // record() early-returns at the disabled gate before it can succeed, + // so the streak can never reset and the latch can never clear. + for (let i = 0; i < 5; i++) shadow.record(fix()); + assert.equal(shadow.stats.disabled, true, "still disabled after healthy records — the latch never re-arms"); + assert.equal(shadow.stats.ingests, ingestsFrozen, "disabled record() is a no-op: the ingest counter never moves again"); + assert.equal(String(shadow.stats.lastError), lastErr, "lastError is frozen at the strike that latched"); + + // Every read/degrade path funnels back to legacy: + assert.equal(shadow.readsUnified, false, "reads go legacy-only"); + assert.equal(shadow.viewFor("anything"), null, "viewFor degrades to null (callers fall back to legacy)"); + + // And the strict fork publication path fails closed instead: + assert.throws(() => shadow.createForkAsync("parent", fix()), /fork storage unavailable/, "fork publication throws once disabled"); + } finally { + shadow.close(); + } +}); + +// --------------------------------------------------------------------------- +// stderr-dead latch: driven in a CHILD process (markStderrDead is module- +// private, has no reset, and flipping it here would poison every later test +// in this process — itself part of the characterization: the latch owns the +// whole process for its lifetime). +// --------------------------------------------------------------------------- + +const CHILD_SRC = ` +import { log, setLogCapture } from ${JSON.stringify(pathToFileURL(fileURLToPath(new URL("../src/logger.ts", import.meta.url))).href)}; +const captured: string[] = []; +setLogCapture((level, msg) => { captured.push(level + " " + msg); }); +const origWrite = process.stderr.write.bind(process.stderr); +let writes = 0; +const counting = (chunk: unknown, enc?: unknown, cb?: unknown) => { writes += 1; return origWrite(chunk as never, enc as never, cb as never); }; +process.stderr.write = counting as typeof process.stderr.write; +log("info", "stake warmup"); // healthy: counted, latch not yet flipped +// 1) a synchronous stderr write failure flips the latch… +process.stderr.write = (() => { throw new Error("EPIPE (stake)"); }) as typeof process.stderr.write; +log("info", "stake before flip"); +// 2) …after which the stderr leg is skipped even though stderr works again… +process.stderr.write = counting as typeof process.stderr.write; +log("info", "stake after flip"); +// 3) …and a second trigger (the async stream 'error' event path) stays idempotent. +process.stderr.emit("error", new Error("EPIPE async (stake)")); +log("info", "stake after second trigger"); +process.stdout.write(JSON.stringify({ captured, writes })); +`; + +test("stderr-dead latch: one failure permanently skips the stderr leg, exactly one warn, logging itself keeps working", async () => { + const childPath = path.join(HERMETIC, "stderr-flip-child.mts"); + fs.writeFileSync(childPath, CHILD_SRC, "utf8"); + const repoRoot = fileURLToPath(new URL("..", import.meta.url)); + const done = new Promise<{ captured: string[]; writes: number }>((resolve, reject) => { + const child = spawn(process.execPath, ["--import", "tsx", childPath], { cwd: repoRoot, stdio: ["ignore", "pipe", "pipe"] }); + let out = ""; + child.stdout.on("data", (c) => { out += c; }); + child.stderr.on("data", () => { /* counted in-child */ }); + child.on("error", reject); + child.on("close", (code) => { + const line = out.split("\n").find((l) => l.startsWith("{")); + if (code !== 0 || line === undefined) { reject(new Error(`child exited ${code}; stdout=${out.slice(0, 500)}`)); return; } + resolve(JSON.parse(line) as { captured: string[]; writes: number }); + }); + }); + const { captured, writes } = await done; + const flips = captured.filter((l) => l.startsWith("warn stderr unavailable (")); + assert.equal(flips.length, 1, "the file-only degradation warns exactly once, idempotent across both trigger paths"); + assert.match(flips[0]!, /continuing with file-only logging/); + assert.equal(writes, 1, "only the PRE-flip warmup line reached stderr — nothing after the flip, even though stderr works again"); + // The logger itself never degrades to silence: later lines still flow + // through capture (and the file leg). + assert.ok(captured.some((l) => l === "info stake after flip"), "post-flip lines still reach the capture sink"); + assert.ok(captured.some((l) => l === "info stake after second trigger"), "lines after the second trigger still reach the capture sink"); +}); + +test("isStreamWriteError truth table (pure predicate, kept beside the latch it serves)", () => { + assert.equal(isStreamWriteError(Object.assign(new Error("broken pipe"), { code: "EPIPE" })), true); + assert.equal(isStreamWriteError(Object.assign(new Error("io"), { code: "EIO" })), true); + assert.equal(isStreamWriteError(Object.assign(new Error("bad fd"), { code: "EBADF" })), true); + assert.equal(isStreamWriteError(Object.assign(new Error("destroyed"), { code: "ERR_STREAM_DESTROYED" })), true); + assert.equal(isStreamWriteError(new Error("plain error without code")), false); + assert.equal(isStreamWriteError(Object.assign(new Error("other code"), { code: "ENOENT" })), false); + assert.equal(isStreamWriteError("not an error object"), false); + assert.equal(isStreamWriteError(undefined), false); +}); diff --git a/tests/contract-v2-stake-verify-attach.test.ts b/tests/contract-v2-stake-verify-attach.test.ts new file mode 100644 index 0000000000..1b4d435dff --- /dev/null +++ b/tests/contract-v2-stake-verify-attach.test.ts @@ -0,0 +1,108 @@ +/** + * #5241 阶段 0 特征化打桩(不改产品代码):四条宿主车道 + * verifyAttachAndRecover 的行为矩阵 —— 抽取共享 adapter/lifecycle 之前的 + * 现状基准。 + * + * 桩钉的是"现在怎样"(包括不一致),不是"应该怎样": + * - S1 健康等版本 target:四车道一致 → attach 原地,不 spawn、不发 shutdown; + * - S2 target 跑严格旧版本:pi/omp/opencode 照样 attach(无版本轮换); + * dsh 车道会轮换 + 重生 —— 那条分歧已有专项桩 + * (tests/issue3544-stale-proxy-rotation.test.ts:392),此处不重跑, + * 只记录四车道对同一输入给出不同答案这一事实; + * - S3 target 死亡:四车道一致 → spawnFallback,spawn 恰一次;env 侧 + * pi/omp/opencode 把 BILLION_CONTEXT_PROXY 重发布到新 origin,dsh 则 + * 解除 preset 并改写 register(车道间又一个形态差异)。 + * + * 每条车道在自己的子进程里跑(contract-v2-stake-lane-probe.mts):四车道 + * 共享同进程的 global fetch / env / 模块状态,同进程顺序驱动会互相污染 + * (实测:一条车道的后台异步会改写另一条的 env 与 spawn 计数)—— + * 生产现实也是每车道一个宿主进程。 + * + * 阶段 1 合并四份拷贝时,本矩阵是行为等价性的验收网;S2 的跨车道分歧 + * 必须在实现方案讨论里显式裁决(统一成轮换,还是保留 dsh 特权)。 + */ + +import { test } from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { spawn } from "node:child_process"; +import { fileURLToPath } from "node:url"; + +const PROBE = fileURLToPath(new URL("./contract-v2-stake-lane-probe.mts", import.meta.url)); +const REPO_ROOT = fileURLToPath(new URL("..", import.meta.url)); + +interface ProbeReport { + lane: string; + scenario: string; + landed: string | null; + spawns: number; + shutdowns: number; + envAfter: string | null; + registerBase: string | null; +} + +async function probe(lane: string, scenario: string): Promise { + return new Promise((resolve, reject) => { + const child = spawn(process.execPath, ["--import", "tsx", PROBE, lane, scenario], { + cwd: REPO_ROOT, + stdio: ["ignore", "pipe", "pipe"], + }); + let out = ""; + child.stdout.on("data", (c) => { out += c; }); + child.on("error", reject); + child.on("close", (code) => { + const line = out.split("\n").find((l) => l.startsWith("PROBE ")); + if (line === undefined) { reject(new Error(`${lane}/${scenario} probe produced no report (exit ${code}); stdout=${out.slice(0, 400)}`)); return; } + resolve(JSON.parse(line.slice("PROBE ".length)) as ProbeReport); + }); + }); +} + +const SPAWNED = "http://127.0.0.1:7777"; +const LANES = ["pi", "omp", "opencode", "dsh"] as const; + +test("S1: a healthy equal-version target attaches in place on all four lanes (no spawn, no shutdown)", async () => { + for (const lane of LANES) { + const r = await probe(lane, "s1"); + assert.notEqual(r.landed, null, `${lane}: healthy target must land somewhere`); + assert.equal(r.spawns, 0, `${lane}: no spawn for a healthy target`); + assert.equal(r.shutdowns, 0, `${lane}: no shutdown request for a healthy equal-version resident`); + if (lane === "dsh") { + assert.equal(r.envAfter, r.landed, "dsh: the preset origin survives the attach (frozen)"); + assert.equal(r.registerBase, r.landed, "dsh: register stays based on the resident"); + } else { + assert.equal(r.landed, r.envAfter, `${lane}: landed origin is published to BILLION_CONTEXT_PROXY`); + } + } +}); + +test("S2: a strictly OLDER target attaches untouched on pi/omp/opencode — no rotation exists outside dsh", async () => { + for (const lane of ["pi", "omp", "opencode"] as const) { + const r = await probe(lane, "s2"); + assert.notEqual(r.landed, null, `${lane}: an older-but-healthy resident is attached as-is (today's behavior)`); + assert.equal(r.spawns, 0, `${lane}: no spawn for an older-but-healthy resident`); + assert.equal(r.shutdowns, 0, `${lane}: no shutdown request — rotation machinery does not exist on this lane`); + assert.equal(r.landed, r.envAfter, `${lane}: landed origin published`); + } + // DIVERGENCE STAKE: dsh answers the SAME input differently (rotate + + // respawn). That arm is pinned by tests/issue3544-stale-proxy-rotation.test.ts + // ("a strictly older resident is rotated out and replaced"). Phase 1 + // extraction must decide ONE policy for the shared adapter; until then + // the four copies are not behaviorally interchangeable. +}); + +test("S3: a dead target falls back to exactly one spawn on all four lanes", async () => { + for (const lane of LANES) { + const r = await probe(lane, "s3"); + assert.equal(r.landed, SPAWNED, `${lane}: dead target lands on the spawned origin`); + assert.equal(r.spawns, 1, `${lane}: spawn fallback ran exactly once`); + if (lane === "dsh") { + assert.equal(r.envAfter, null, "dsh: spawn fallback UNFREEZES the preset (register becomes the source of truth)"); + assert.equal(r.registerBase, SPAWNED, "dsh: register re-based onto the fresh spawn"); + } else { + assert.equal(r.envAfter, SPAWNED, `${lane}: env republished onto the spawn`); + } + } +}); From d750a8900884dc4cf337f07718792a740a904e94 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 31 Oct 2026 04:10:55 +0800 Subject: [PATCH 2/4] fix(ci): register the two spawning stake suites in LIVE_FILES MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit contract-v2-stake-latches.test.ts and contract-v2-stake-verify-attach.test.ts both spawn child processes but were never added to scripts/live-files.mjs, so run-ci-tests.mjs's SPAWN_RE tripwire (#1248) dies the test lane with "process-spawning test files missing from LIVE_FILES". Appended both entries (append-only: slot=index drives each file's zone port band, so mid-list inserts would renumber later bands). Neither suite binds a shared port — the mock bili servers are listen(0) and the spawns are stubbed — so no real zone band is needed; the entries exist for the tripwire alone, like #3890's. --- scripts/live-files.mjs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/scripts/live-files.mjs b/scripts/live-files.mjs index 079538786e..80c283c437 100644 --- a/scripts/live-files.mjs +++ b/scripts/live-files.mjs @@ -99,6 +99,13 @@ export const LIVE_FILES = [ // #4612: spawns an idle node child as the "other live process" fixture; // binds no ports, mkdtemp-isolated per test "tests/issue4612-gc-online-guard.test.ts", + // #5241 phase-0 startup-contract stakes: both spawn children — verify-attach + // drives one child per lane (sequential await), latches drives the stderr + // flip child. No shared ports (mock bili server + spawn stubs are listen(0) + // or unbound), so no real zone band is needed — the entries exist for the + // spawn tripwire alone, like #3890's + "tests/contract-v2-stake-latches.test.ts", + "tests/contract-v2-stake-verify-attach.test.ts", ]; /** Band width per live file: the zone ladder drifts +1 per EADDRINUSE retry; From f922f13e82d2a0ec2067ae36409619a30023a60d Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 12 Nov 2026 02:04:22 +0800 Subject: [PATCH 3/4] test: pin landed never-give-up shadow contract in #5241 phase-0 stake (CI fix for #5242) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The phase-0 stake pinned the 3-strike self-disable latch that owner policy b9eb27a448 (2026-10-30, "never give up on index writes") removed from src/storage/shadow.ts hours before this branch forked from its base — the stale expectation red every CI shard after rebase onto master. Rewritten to pin the landed contract on the same fault seam: structural strikes never disable (stats.disabled stays false), one warn per distinct message (#5140 dedup), lastError carries the latest failure, healthy records resume ingesting once the fault clears, full-mode reads stay unified-first, and fork publication does not fail closed. Fixture now opens in FULL mode so readsUnified is a live gate (shadow mode made it structurally false). Tests-only; no product code touched. --- tests/contract-v2-stake-latches.test.ts | 49 ++++++++++++++----------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/tests/contract-v2-stake-latches.test.ts b/tests/contract-v2-stake-latches.test.ts index a35b9e5be9..f62e053401 100644 --- a/tests/contract-v2-stake-latches.test.ts +++ b/tests/contract-v2-stake-latches.test.ts @@ -2,10 +2,12 @@ * #5241 阶段 0 特征化打桩(不改产品代码):两把「一次性闩锁」的永久性 * —— C3 契约(降级必须有告警、可恢复、有 TTL)改造前的现状基准。 * - * - shadow.ts stats.disabled:3 次结构性 ingest 失败闩死统一 sidecar。 - * issue5140 已钉「3 strike 才闩、闩后 warn 只一条」;这里补钉的是 - * C3 关心的另一半事实:**进程内不存在任何恢复路径** —— 故障消失后 - * record() 永久 no-op、读路径全部回退、fork 直接 throw,直到进程重启。 + * - shadow.ts ingest 失败**永不自我禁用**(owner policy 2026-10-30, + * b9eb27a448 "never give up on index writes"):结构性失败每次 save 都重试、 + * warn 按消息去重(同一消息一条,#5140)、读保持 unified-first、fork 不 + * fail-close;stats.disabled 恒 false(面板兼容字段)。这里钉的正是旧 + * 3-strike 闩锁被政策取代后的现状——含旧桩的反面事实:故障消失后 + * record() **能恢复**(不是永久 no-op)。 * - logger.ts stderrDead:stderr 写失败(同步抛或流 error 事件)把 * 整个进程的日志降级为 file-only,一次性、幂等、同样无复位。 * markStderrDead 未导出且无 reset —— 进程内翻闩会污染同进程后续 @@ -23,7 +25,7 @@ import path from "node:path"; import { spawn } from "node:child_process"; import { fileURLToPath, pathToFileURL } from "node:url"; import { UnifiedShadow } from "../src/storage/shadow.ts"; -import { isStreamWriteError } from "../src/logger.ts"; +import { isStreamWriteError, setLogCapture } from "../src/logger.ts"; import type { LegacySessionLike } from "../src/storage/ingest.ts"; const HERMETIC = fs.mkdtempSync(path.join(os.tmpdir(), "bili-c2-latch-")); @@ -40,7 +42,9 @@ after(() => { function mkShadow(): UnifiedShadow { const dir = fs.mkdtempSync(path.join(HERMETIC, "sh-")); - const shadow = UnifiedShadow.maybeOpen(dir, { BILI_STORAGE_UNIFIED: "shadow" }, (r) => r as LegacySessionLike); + // FULL mode on purpose: only there is readsUnified a live gate, so the + // stake can prove ingest health never flips full-mode reads legacy-only. + const shadow = UnifiedShadow.maybeOpen(dir, { BILI_STORAGE_UNIFIED: "full" }, (r) => r as LegacySessionLike); assert.ok(shadow !== null, "hermetic shadow must open (real sqlite engine present)"); return shadow; } @@ -71,32 +75,33 @@ function installFault(shadow: UnifiedShadow, message: string): () => void { return () => { sync.getBlocks = orig; }; } -test("shadow disable latch: once disabled, healthy records are permanent no-ops (no re-enable path exists)", () => { +test("shadow ingest failures never self-disable: retry-forever with recovery is the landed contract (owner policy 2026-10-30)", async () => { const shadow = mkShadow(); + const warns: string[] = []; + setLogCapture((level, msg) => { if (level === "warn") warns.push(msg); }); try { const off = installFault(shadow, "file is not a database (stake: structural)"); for (let i = 0; i < 3; i++) shadow.record(fix()); off(); - assert.equal(shadow.stats.disabled, true, "three structural strikes latch the sidecar"); - const ingestsFrozen = shadow.stats.ingests; - const lastErr = String(shadow.stats.lastError); - assert.match(lastErr, /file is not a database/); + assert.equal(shadow.stats.disabled, false, "structural strikes never disable the sidecar (b9eb27a448: never give up on index writes)"); + assert.match(String(shadow.stats.lastError), /file is not a database/, "lastError carries the latest failure detail"); + assert.equal(warns.filter((w) => w.includes("file is not a database")).length, 1, "three strikes, ONE warn per distinct message (#5140 dedup)"); - // The fault is GONE now. Today there is no recovery: every later - // record() early-returns at the disabled gate before it can succeed, - // so the streak can never reset and the latch can never clear. + // The inverse of the pre-policy stake: recovery IS possible. The fault + // is gone, so the next saves retry unconditionally and succeed again. + const ingestsBefore = shadow.stats.ingests; for (let i = 0; i < 5; i++) shadow.record(fix()); - assert.equal(shadow.stats.disabled, true, "still disabled after healthy records — the latch never re-arms"); - assert.equal(shadow.stats.ingests, ingestsFrozen, "disabled record() is a no-op: the ingest counter never moves again"); - assert.equal(String(shadow.stats.lastError), lastErr, "lastError is frozen at the strike that latched"); + assert.equal(shadow.stats.disabled, false, "healthy records find nothing to re-enable — no latch exists"); + assert.ok(shadow.stats.ingests > ingestsBefore, "healthy records keep ingesting after the fault clears (retry-forever, not a permanent no-op)"); - // Every read/degrade path funnels back to legacy: - assert.equal(shadow.readsUnified, false, "reads go legacy-only"); - assert.equal(shadow.viewFor("anything"), null, "viewFor degrades to null (callers fall back to legacy)"); + // Reads never flipped legacy-only during or after the failure streak: + assert.equal(shadow.readsUnified, true, "reads stay unified-first regardless of ingest health"); + assert.ok(shadow.viewFor("latch-s1") !== null, "viewFor still serves the unified view after failed saves"); - // And the strict fork publication path fails closed instead: - assert.throws(() => shadow.createForkAsync("parent", fix()), /fork storage unavailable/, "fork publication throws once disabled"); + // Fork publication does not fail closed on a failing sidecar: + assert.equal(await shadow.createForkAsync("latch-s1", fix()), true, "fork publication runs normally despite the earlier failure streak"); } finally { + setLogCapture(null); shadow.close(); } }); From d18e0f55800167a22bf6ed36948af697d627d839 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 12 Nov 2026 05:48:28 +0800 Subject: [PATCH 4/4] fix: drop unused imports in the two contract-v2 stake suites --- tests/contract-v2-stake-health.test.ts | 2 +- tests/contract-v2-stake-verify-attach.test.ts | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/contract-v2-stake-health.test.ts b/tests/contract-v2-stake-health.test.ts index edef7c0ea0..a01d86c542 100644 --- a/tests/contract-v2-stake-health.test.ts +++ b/tests/contract-v2-stake-health.test.ts @@ -12,7 +12,7 @@ * tests/issue3654-health-probe-three-state.test.ts,不在此重钉。 */ -import { test, before, after } from "node:test"; +import { test, after } from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; diff --git a/tests/contract-v2-stake-verify-attach.test.ts b/tests/contract-v2-stake-verify-attach.test.ts index 1b4d435dff..6215feecea 100644 --- a/tests/contract-v2-stake-verify-attach.test.ts +++ b/tests/contract-v2-stake-verify-attach.test.ts @@ -24,9 +24,6 @@ import { test } from "node:test"; import assert from "node:assert/strict"; -import fs from "node:fs"; -import os from "node:os"; -import path from "node:path"; import { spawn } from "node:child_process"; import { fileURLToPath } from "node:url";