diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index d23cb04ae0..1d52966573 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -47,6 +47,13 @@ export const name = "bili-native"; export const inject = ["tools", "commands", "agents"]; const RETRY_INTERVAL_MS = 10000; +/** #2082: BILI_DSH_RETRY_INTERVAL_MS — test/ops knob to shrink the retry + * wall without wall-clock waits (same pattern as BILI_MODEL_INFO_RETRY_MS). */ +function retryIntervalMs(): number { + const raw = process.env.BILI_DSH_RETRY_INTERVAL_MS; + const parsed = raw === undefined ? Number.NaN : Number(raw); + return Number.isFinite(parsed) && parsed > 0 ? parsed : RETRY_INTERVAL_MS; +} type AgentLike = { session?: { id?: unknown } | undefined }; // #1677: DSH's command executor hands the invoking agent to the handler via the @@ -465,7 +472,7 @@ async function registerTools(ctx: PluginContext): Promise { // apply chain unfroze a dead preset, maybeRetry healed elsewhere), // an armed retryAt would gate the NEXT base's first heal behind a // 10s wall — exactly the windows-22 CI deadlock (#1783). - if (register.base === base) register.retryAt = Date.now() + RETRY_INTERVAL_MS; + if (register.base === base) register.retryAt = Date.now() + retryIntervalMs(); console.error(`bili-native-dsh: manifest registration failed (${errMessage(err)}) — retrying; requests stay in wire mode until it succeeds`); }) .finally(() => { @@ -474,7 +481,37 @@ async function registerTools(ctx: PluginContext): Promise { return register.pending; } +// #2082: recovery retries are traffic-driven (headersFor on model +// requests, statusOutcome on /acp). When the user goes idle — or the model +// already gave up on the missing tools, exactly the #2082 report — nothing +// re-arms them, so a proxy that recovered in the meantime sits unused until +// the next request. A detached repeating timer keeps the loop running while +// the tools are down and clears itself on the first healthy (or dead) tick. +// BILI_DSH_RECOVERY_INTERVAL_MS: test hook to shrink the cadence (also an +// ops knob); absent it tracks RETRY_INTERVAL_MS. Tests that do not opt in +// never start a timer at all (NODE_TEST_CONTEXT guard keeps suites quiet). +let recoveryTimer: NodeJS.Timeout | undefined; +function recoveryIntervalMs(): number { + const raw = process.env.BILI_DSH_RECOVERY_INTERVAL_MS; + const parsed = raw === undefined ? Number.NaN : Number(raw); + return Number.isFinite(parsed) && parsed > 0 ? parsed : RETRY_INTERVAL_MS; +} +function armRecoveryTimer(ctx: PluginContext): void { + if (recoveryTimer !== undefined) return; + if (process.env.NODE_TEST_CONTEXT !== undefined && process.env.BILI_DSH_RECOVERY_INTERVAL_MS === undefined) return; + recoveryTimer = setInterval(() => { + if (register.dead || register.toolsReady) { + if (recoveryTimer !== undefined) clearInterval(recoveryTimer); + recoveryTimer = undefined; + return; + } + maybeRetry(ctx); + }, recoveryIntervalMs()); + recoveryTimer.unref?.(); +} + function maybeRetry(ctx: PluginContext): void { + if (!register.dead && !register.toolsReady) armRecoveryTimer(ctx); if (register.dead || register.toolsReady) return; if (register.pending !== undefined) return; if (Date.now() < register.retryAt) return; @@ -483,9 +520,18 @@ function maybeRetry(ctx: PluginContext): void { // without this branch the plugin never recovers and tools die for // good. Self-heal: re-arm the respawn every retry interval until a // proxy comes back (attach mode arms one since #1130). - const respawn = state.respawn; - if (respawn === undefined) return; - register.retryAt = Date.now() + RETRY_INTERVAL_MS; + let respawn = state.respawn; + if (respawn === undefined) { + // #2082: nothing armed the respawn hook — a plan edge (attach + // landing after a dead spawn chain) or an apply() that ran under + // the NODE_TEST_CONTEXT spawn guard. A base-less register with no + // respawn armed stays dead forever; arm the fallback here so the + // retry chain below has something to drive. + if (_spawnForTest === undefined && process.env.NODE_TEST_CONTEXT !== undefined) return; + respawn = singleFlight(_spawnForTest ?? bootstrap); + state.respawn = respawn; + } + register.retryAt = Date.now() + retryIntervalMs(); void trackChain(respawn()) .then((origin) => { if (origin === undefined) return; @@ -659,6 +705,15 @@ export function apply(ctx: PluginContext): void { const plan = planNativeDsh(process.env); if (plan.mode === "off") return; + // #2082: a fresh activation invalidates the "host tearing down" verdict. + // Without this reset, ONE registerTools rejection carrying "inactive + // context" (a cordis deactivate/reactivate cycle — dsh reloading the + // plugin after the desktop profile copy was swapped in place, a profile + // switch, a settings toggle) poisons register.dead for the whole process + // lifetime: every later maybeRetry() returns immediately and the bili + // tools never come back until dsh restarts. + register.dead = false; + // #1590: the dsh web-profile settings panel shows a "bili设置" entry // (dsh-native-client.js) that opens this proxy's Web UI. The origin is // only published while known — attach mode binds register.base @@ -1006,6 +1061,10 @@ export function apply(ctx: PluginContext): void { /** Test hook: reset the module-level registration lifecycle so suites can * drive apply() repeatedly with a fresh mock ctx. */ export function _resetRegisterForTest(base: string | undefined): void { + if (recoveryTimer !== undefined) { + clearInterval(recoveryTimer); + recoveryTimer = undefined; + } register.base = base; register.toolsReady = false; register.dead = false; @@ -1016,6 +1075,13 @@ export function _resetRegisterForTest(base: string | undefined): void { modelInfo.refreshing = false; } +/** Test hook (#2082): drive the recovery loop directly — production callers + * are traffic-side (headersFor / statusOutcome); tests trigger the same + * path without fabricating model traffic. */ +export function maybeRetryForTest(ctx: PluginContext): void { + maybeRetry(ctx); +} + /** Test hook (#1797): resolve once every in-flight attach/recovery chain has * settled, plus one macrotask so their .then state-writes have run. */ export function _settleNativeForTest(): Promise { diff --git a/src/update.ts b/src/update.ts index 3fdc1e4455..f3be78c154 100644 --- a/src/update.ts +++ b/src/update.ts @@ -21,13 +21,14 @@ * cycle automatically. */ import { readFile, writeFile, mkdir, access, constants, rm, cp, unlink, lstat, rename } from "node:fs/promises"; -import { execFile } from "node:child_process"; +import { execFile, spawn } from "node:child_process"; import { realpathSync } from "node:fs"; import crypto from "node:crypto"; import * as tar from "tar"; import path from "node:path"; import os from "node:os"; import { fileURLToPath } from "node:url"; +import { safeSuffix } from "./text-safe.js"; import { cacheDir } from "./paths.js"; import { log as loggerLog, type Logger } from "./logger.js"; import { refreshDshProfileBundles, isDshProfileCopy, dshProfileDirs, dshProfileDependsOnBili, dshProfileDepSpec, isRegistryDepSpec, DSH_PACKAGE, DSH_DESKTOP_PROFILE } from "./dsh-channel.js"; @@ -842,9 +843,9 @@ export async function refreshDshDesktopCopy( log("warn", `[update] dsh ${DSH_DESKTOP_PROFILE}: in-place refresh to ${targetVersion} failed \u2014 no dist.tarball for that version on the registry; retrying next cycle`); return; } - const result = await installViaTarball(targetVersion, doc.tarball, flat, doc.integrity, doc.shasum, egressDispatcher({ resolveProxy }, doc.tarball), env); + const result = await installViaTarball(targetVersion, doc.tarball, flat, doc.integrity, doc.shasum, egressDispatcher({ resolveProxy }, doc.tarball), env, { bootSmoke: true }); if (result.ok) { - log("info", `[update] refreshed dsh ${DSH_DESKTOP_PROFILE} profile copy in place (${diskVersion ?? "?"} \u2192 ${targetVersion}) \u2014 restart dsh to load it`); + log("info", `[update] refreshed dsh ${DSH_DESKTOP_PROFILE} profile copy in place (${diskVersion ?? "?"} \u2192 ${targetVersion}) \u2014 restart dsh to load it (the running app keeps the old code in memory; across the handoff bili tools may fail once until dsh restarts, #2082)`); } else { log("warn", `[update] dsh ${DSH_DESKTOP_PROFILE}: in-place refresh to ${targetVersion} failed: ${result.error}; retrying next cycle`); } @@ -1177,6 +1178,63 @@ export function verifyTarballIntegrity(buf: Buffer, integrity?: string, shasum?: * over the install directory. `dispatcher` (optional) routes the download * through a proxy (#609); omitted = direct connection. */ +const BOOT_SMOKE_TIMEOUT_MS = 20_000; + +/** #2082: run the freshly installed copy's CLI entry with --version in a + * clean child process. Loading the module graph and exiting 0 is the smoke; + * any non-zero exit, timeout, or spawn failure returns a short reason (with + * the last stderr lines) for the caller to log before rolling back. + * Env scrub: the child must not inherit the parent's BILLION_CONTEXT_* / + * BILI_* knobs (a preset proxy origin would make --version spin up lanes) + * nor NODE_OPTIONS / NODE_TEST_CONTEXT interference. */ +async function bootSmoke(installDir: string, env: NodeJS.ProcessEnv): Promise { + let entry: string | undefined; + try { + const pkg = JSON.parse(await readFile(path.join(installDir, "package.json"), "utf8")) as { main?: string }; + if (typeof pkg.main === "string" && pkg.main.length > 0) entry = pkg.main; + } catch (err) { + return `package.json unreadable: ${err instanceof Error ? err.message : String(err)}`; + } + if (entry === undefined) return "package.json declares no main entry to smoke"; + const childEnv: NodeJS.ProcessEnv = {}; + for (const [key, value] of Object.entries(env)) { + if (value === undefined) continue; + if (key.startsWith("BILI_") || key.startsWith("BILLION_CONTEXT_") || key.startsWith("ACP_")) continue; + if (key === "NODE_OPTIONS" || key === "NODE_TEST_CONTEXT" || key === "NODE_ENV") continue; + childEnv[key] = value; + } + return await new Promise((resolve) => { + const child = spawn(process.execPath, [path.join(installDir, entry!), "--version"], { + cwd: installDir, + env: childEnv, + stdio: ["ignore", "ignore", "pipe"], + }); + let stderr = ""; + child.stderr?.on("data", (chunk: Buffer) => { + stderr += chunk.toString(); + if (stderr.length > 8192) stderr = safeSuffix(stderr, 8192); + }); + const timer = setTimeout(() => { + child.kill(); + resolve(`boot smoke timed out after ${BOOT_SMOKE_TIMEOUT_MS}ms`); + }, BOOT_SMOKE_TIMEOUT_MS); + child.on("error", (err) => { + clearTimeout(timer); + resolve(`boot smoke could not spawn ${process.execPath}: ${err.message}`); + }); + child.on("close", (code, signal) => { + clearTimeout(timer); + if (code === 0) { + resolve(null); + return; + } + const lines = stderr.split("\n").filter((l) => l.trim().length > 0 && !l.trim().startsWith("at ") && !/^Node\.js v/.test(l.trim())); + const tail = lines.length > 0 ? `: ${safeSuffix(lines.join(" | "), 600)}` : ""; + resolve(`boot smoke exited ${code ?? signal}${tail}`); + }); + }); +} + export async function installViaTarball( version: string, tarballUrl: string, @@ -1185,6 +1243,7 @@ export async function installViaTarball( shasum?: string, dispatcher?: object, env: NodeJS.ProcessEnv = process.env, + opts: { bootSmoke?: boolean } = {}, ): Promise<{ ok: boolean; error?: string }> { if (!installDir) { return { ok: false, error: "cannot determine install directory (package.json not found walking up from running binary)" }; @@ -1396,6 +1455,23 @@ export async function installViaTarball( return { ok: false, error: rb ?? postEntryErr }; } + // #2082: optional boot smoke. verifyEntries proves the declared entries + // PARSE; it cannot prove the artifact BOOTS (a top-level throw, a broken + // import graph, a runtime dependency the flattened copy no longer + // resolves — all syntactically valid). The desktop lane swaps this copy + // in place under a RUNNING dsh whose plugin respawns from it on the next + // proxy death; an unbootable copy turns that respawn into an infinite + // give-up loop and the bili tools vanish until dsh restarts. Run the new + // copy's CLI entry with --version once — the module graph loads and exits + // immediately. Any failure rolls the working copy back to the backup. + if (opts.bootSmoke) { + const smokeErr = await bootSmoke(installDir, env); + if (smokeErr !== null) { + const rb = await restoreFromBackup(); + return { ok: false, error: rb ?? `boot smoke failed (rolled back to the previous copy): ${smokeErr}` }; + } + } + // Success: the backup is no longer needed, and neither is the displaced // pnpm link (the store copy underneath it was left byte-identical). await rm(backupDir, { recursive: true, force: true }); diff --git a/tests/issue2082-desktop-boot-smoke.test.ts b/tests/issue2082-desktop-boot-smoke.test.ts new file mode 100644 index 0000000000..ca824a2373 --- /dev/null +++ b/tests/issue2082-desktop-boot-smoke.test.ts @@ -0,0 +1,174 @@ +// #2082: the dsh desktop profile refresh (refreshDshDesktopCopy, #1575) now +// runs a BOOT SMOKE over the freshly staged copy before committing to it — +// verifyEntries proves the declared entries parse, but a top-level throw or a +// broken import graph is syntactically valid and slips through. The desktop +// copy is what a RUNNING dsh respawns from when its proxy dies; committing an +// unbootable copy turns that respawn into an infinite give-up loop and the +// bili tools vanish until dsh restarts. A failed smoke rolls the working copy +// back to the previous version. + +import { test } from "node:test"; +import assert from "node:assert/strict"; +import crypto from "node:crypto"; +import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, symlinkSync, lstatSync, readdirSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import * as tar from "tar"; +import { installViaTarball, readDiskVersion, refreshDshDesktopCopy } from "../src/update.ts"; +import { rmrf } from "./tmp-rm.ts"; + +function integrityField(buf: Buffer, alg = "sha512"): string { + return `${alg}-${crypto.createHash(alg).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); +} + +const GOOD_ENTRY = "export const loaded = '__VERSION__';\n"; +const BOOT_BROKEN_ENTRY = 'throw new Error("boom at boot (#2082 fixture)");\n'; + +/** Windows junctions need no SeCreateSymbolicLinkPrivilege (same trick as + * update-desktop-inplace.test.ts). */ +function makePnpmLink(target: string, linkPath: string): void { + symlinkSync(target, linkPath, process.platform === "win32" ? "junction" : "dir"); +} + +interface Fixture { root: string; dshHome: string; storeCopy: string; flat: string; tgz: Buffer; } + +function buildFixture(root: string, storeVersion: string, tarballVersion: string, tarballEntry: string): Fixture { + const dshHome = path.join(root, "dsh-home"); + const profileDir = path.join(dshHome, "profiles", "desktop"); + const storeCopy = path.join(profileDir, ".pnpm", `billion-context@${storeVersion}`, "node_modules", "billion-context"); + writePkg(storeCopy, storeVersion, GOOD_ENTRY.replace("__VERSION__", storeVersion)); + mkdirSync(path.join(profileDir, "node_modules"), { recursive: true }); + const flat = path.join(profileDir, "node_modules", "billion-context"); + makePnpmLink(storeCopy, flat); + const src = path.join(root, "pkg", "package"); + writePkg(src, tarballVersion, tarballEntry.replace("__VERSION__", tarballVersion)); + const tgzPath = path.join(root, "pkg.tgz"); + tar.c({ cwd: path.join(root, "pkg"), file: tgzPath, gzip: true, sync: true }, ["package"]); + return { root, dshHome, storeCopy, flat, tgz: readFileSync(tgzPath) }; +} + +function stubRegistryFetch(tgz: Buffer, tarballUrl: string, version: string, integrity: string): void { + globalThis.fetch = ((url: unknown) => { + const u = String(url); + if (u.endsWith(`/billion-context/${version}`)) { + return Promise.resolve(new Response(JSON.stringify({ dist: { tarball: tarballUrl, integrity } }), { headers: { "content-type": "application/json" } })); + } + if (u === tarballUrl) { + return Promise.resolve(new Response(tgz)); + } + return Promise.reject(new Error(`unexpected fetch in test: ${u}`)); + }) as unknown as typeof fetch; +} + +type CaptureLog = (level: string, msg: string) => void; + +function captureLog(): { lines: string[]; log: CaptureLog } { + const lines: string[] = []; + const log: CaptureLog = (level, msg) => { lines.push(`${level}: ${msg}`); }; + return { lines, log }; +} + +test("installViaTarball bootSmoke: an entry that throws at boot fails the install and rolls the copy back (#2082)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-2082-smoke-bad-")); + const prevDshHome = process.env.DSH_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + rmrf(root); + }); + try { + const fx = buildFixture(root, "1.2.3", "2.0.0", BOOT_BROKEN_ENTRY); + process.env.DSH_HOME = fx.dshHome; + stubRegistryFetch(fx.tgz, "https://registry.test/bc-2.0.0.tgz", "2.0.0", integrityField(fx.tgz)); + + const r = await installViaTarball("2.0.0", "https://registry.test/bc-2.0.0.tgz", fx.flat, integrityField(fx.tgz), undefined, undefined, process.env, { bootSmoke: true }); + 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/); + + // the working copy was rolled back to the previous version + assert.equal(await readDiskVersion(fx.flat), "1.2.3"); + assert.equal(readFileSync(path.join(fx.storeCopy, "dist", "index.js"), "utf8"), "export const loaded = '1.2.3';\n"); + // no displaced-link litter + for (const entry of readdirSync(path.dirname(fx.flat))) { + assert.ok(!entry.startsWith("billion-context.pnpm-"), `leftover displaced link: ${entry}`); + } + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); + +test("installViaTarball bootSmoke: a loadable entry passes and the copy lands (#2082)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-2082-smoke-ok-")); + const prevDshHome = process.env.DSH_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + rmrf(root); + }); + try { + const fx = buildFixture(root, "1.2.3", "2.0.0", GOOD_ENTRY); + process.env.DSH_HOME = fx.dshHome; + stubRegistryFetch(fx.tgz, "https://registry.test/bc-2.0.0.tgz", "2.0.0", integrityField(fx.tgz)); + + const r = await installViaTarball("2.0.0", "https://registry.test/bc-2.0.0.tgz", fx.flat, integrityField(fx.tgz), undefined, undefined, process.env, { bootSmoke: true }); + assert.equal(r.ok, true, r.error ?? "install failed without reason"); + assert.equal(lstatSync(fx.flat).isSymbolicLink(), false, "the pnpm link must be replaced, not followed"); + assert.equal(await readDiskVersion(fx.flat), "2.0.0"); + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); + +test("refreshDshDesktopCopy: a boot-broken registry tarball keeps the working copy and warns loudly (#2082)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-2082-drive-")); + const prevDshHome = process.env.DSH_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + rmrf(root); + }); + try { + const fx = buildFixture(root, "1.2.3", "2.0.0", BOOT_BROKEN_ENTRY); + process.env.DSH_HOME = fx.dshHome; + stubRegistryFetch(fx.tgz, "https://registry.test/bc-2.0.0.tgz", "2.0.0", integrityField(fx.tgz)); + const { lines, log } = captureLog(); + await refreshDshDesktopCopy("2.0.0", log, process.env); + + assert.equal(await readDiskVersion(fx.flat), "1.2.3", "the working copy must survive a boot-broken tarball"); + assert.ok( + lines.some((l) => l.startsWith("warn:") && l.includes("boot smoke")), + `expected a boot-smoke warn line, got: ${lines.join(" | ")}`, + ); + assert.ok(!lines.some((l) => l.includes("in place")), `no in-place success line: ${lines.join(" | ")}`); + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); diff --git a/tests/issue2082-dsh-recovery.test.ts b/tests/issue2082-dsh-recovery.test.ts new file mode 100644 index 0000000000..56e1ba85ef --- /dev/null +++ b/tests/issue2082-dsh-recovery.test.ts @@ -0,0 +1,187 @@ +// #2082: the plugin-side recovery hardening. +// +// 1. apply() resets register.dead — one "inactive context" rejection during a +// cordis deactivate/reactivate cycle used to poison the register for the +// whole process lifetime (every later maybeRetry returned immediately; the +// bili tools never came back until dsh restarted). +// 2. maybeRetry() arms a fallback respawn when state.respawn is undefined — +// a base-less register with nothing armed used to return and stay dead. +// 3. A detached recovery timer keeps retrying while the tools are down even +// with zero model traffic (the reported case: the model already gave up on +// the missing tools, so nothing re-armed the loop). + +import test, { afterEach } 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 { apply, maybeRetryForTest, _resetRegisterForTest, _setSpawnForTest, _settleNativeForTest, _stateHeadersForTest, _stateRespawnForTest } from "../src/agent/dsh-native.ts"; +import { rmrf } from "./tmp-rm.ts"; + +afterEach(() => { void _settleNativeForTest(); }); + +type MockTool = { name: string; description?: string; inputSchema: Record }; +type RegisteredTool = { + name: string; + description?: string; + parameters: unknown; + output: { schema: unknown; render: (args: unknown, value: unknown) => Array<{ type: string; text: string }> }; + execute: (args: Record, exec: { agent?: { session?: { id?: unknown } }; signal?: AbortSignal }) => Promise; +}; + +function mockBiliHandler(): (req: http.IncomingMessage, res: http.ServerResponse) => void { + const manifestTools: MockTool[] = [ + { + name: "compress", + description: "Compress a range of messages", + inputSchema: { type: "object", properties: { summary: { type: "string" } }, required: ["summary"] }, + }, + ]; + return (req, res) => { + const url = req.url ?? ""; + if (url === "/__bili/plugin/manifest") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ version: "0.1.119", tools: { anthropic: manifestTools.map((t) => ({ name: t.name, description: t.description, input_schema: t.inputSchema })) } })); + return; + } + res.writeHead(404); + res.end("{}"); + }; +} + +async function startMockProxy(): Promise<{ origin: string; close: () => void }> { + const server = http.createServer(mockBiliHandler()); + 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}`, close: () => server.close() }); + }); + }); +} + +type HostCtx = Parameters[0]; + +function mockCtx(opts: { registerThrows?: () => boolean } = {}) { + const tools: RegisteredTool[] = []; + const commands: Array<{ name: string; handler: () => Promise<{ kind: string; text: string }> }> = []; + let initiator: { session?: { id?: unknown } } | undefined = undefined; + return { + tools: { + register: (t: RegisteredTool) => { + if (opts.registerThrows?.()) { + throw new Error('cannot get required service "tools" in inactive context'); + } + tools.push(t); + }, + }, + commands: { register: (c: { name: string; handler: () => Promise<{ kind: string; text: string }> }) => commands.push(c) }, + agents: { currentInitiator: () => initiator }, + setInitiator: (i: { session?: { id?: unknown } } | undefined) => (initiator = i), + registeredTools: tools, + registeredCommands: commands, + inject: (_deps: readonly string[], _cb: (sub: HostCtx) => void) => {}, + }; +} + +async function withEnv(env: Record, fn: () => Promise | T): Promise { + const saved: Record = {}; + for (const k of Object.keys(env)) { + saved[k] = process.env[k]; + const v = env[k]; + if (v === undefined) delete process.env[k]; + else process.env[k] = v; + } + try { + return await fn(); + } finally { + for (const [k, v] of Object.entries(saved)) { + if (v === undefined) delete process.env[k]; + else process.env[k] = v; + } + } +} + +async function waitFor(cond: () => boolean, what: string): Promise { + const deadline = Date.now() + 15000; + while (!cond()) { + if (Date.now() > deadline) throw new Error(`timeout waiting for ${what}`); + await new Promise((r) => setTimeout(r, 10)); + } +} + +test("#2082 maybeRetry arms a fallback respawn when state.respawn was never armed, and the recovery timer drives retries with zero model traffic (#2082)", async () => { + const proxy = await startMockProxy(); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-2082-fallback-")); + const savedInterval = process.env.BILI_DSH_RECOVERY_INTERVAL_MS; + const savedRetry = process.env.BILI_DSH_RETRY_INTERVAL_MS; + process.env.BILI_DSH_RECOVERY_INTERVAL_MS = "20"; + process.env.BILI_DSH_RETRY_INTERVAL_MS = "20"; + try { + await withEnv({ DSH_HOME: home }, async () => { + _resetRegisterForTest(undefined); // base-less + // first respawn attempt fails (proxy down); the next one lands — + // only a RETRY can recover, and with no model traffic in this + // test the retry can only come from the recovery timer + let attempts = 0; + _setSpawnForTest(() => { + attempts += 1; + return Promise.resolve(attempts === 1 ? undefined : proxy.origin); + }); + try { + assert.equal(_stateRespawnForTest(), undefined, "precondition: nothing armed"); + const ctx = mockCtx(); + maybeRetryForTest(ctx); + // the fallback armed a respawn (first attempt already failed) + await waitFor(() => _stateRespawnForTest() !== undefined, "fallback respawn armed"); + // the timer keeps driving maybeRetry until the second attempt + // lands, the base binds, and the manifest registers the tool + await waitFor(() => ctx.registeredTools.length === 1, "timer-driven registration with zero model traffic"); + assert.ok(attempts >= 2, `expected a retry, got ${attempts} attempts`); + assert.equal(ctx.registeredTools[0].name, "compress"); + } finally { + _setSpawnForTest(undefined); + } + }); + } finally { + for (const [k, v] of [["BILI_DSH_RECOVERY_INTERVAL_MS", savedInterval], ["BILI_DSH_RETRY_INTERVAL_MS", savedRetry]] as const) { + if (v === undefined) delete process.env[k]; + else process.env[k] = v; + } + proxy.close(); + rmrf(home); + _resetRegisterForTest(undefined); + } +}); + +test("#2082 apply() clears register.dead: a re-activated context recovers after an inactive-context teardown (#2082)", async () => { + const proxy = await startMockProxy(); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-2082-dead-")); + try { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: proxy.origin }, async () => { + _resetRegisterForTest(proxy.origin); + // phase 1: the context is inactive during teardown — registration + // throws cordis's inactive-context error and the register dies + let tearing = true; + const ctx1 = mockCtx({ registerThrows: () => tearing }); + apply(ctx1); + await new Promise((r) => setTimeout(r, 50)); + assert.equal(ctx1.registeredTools.length, 0); + + // phase 2: the host re-activates the plugin (fresh context). The + // dead verdict must NOT survive — tools register again. + tearing = false; + const ctx2 = mockCtx(); + ctx2.setInitiator({ session: { id: "dsh-2082-session" } }); + apply(ctx2); + await waitFor(() => ctx2.registeredTools.length === 1, "tool registration after re-activation"); + assert.equal(ctx2.registeredTools[0].name, "compress"); + // headers are stamped again — plugin mode is back + await waitFor(() => _stateHeadersForTest()?.("http://example.test/v1/messages") !== undefined, "headers return after re-activation"); + }); + } finally { + proxy.close(); + rmrf(home); + _resetRegisterForTest(undefined); + } +});