From 21b7822808d1d9d2bbe968012c166c3f504e84aa Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 16 Nov 2026 17:11:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20dsh=20attach-to-exiting-agent=20race=20?= =?UTF-8?q?=E2=80=94=20self-heal=20dead=20base,=20skip=20draining=20instan?= =?UTF-8?q?ces=20(#6881)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a dsh host restarts within seconds of the old host stopping, the new host can attach to the old proxy's draining tail (listener closed, established keep-alive conns still answering). register.base then points at a dead address; the recovery timer only re-fetches the dead manifest (backoff 10→20→40s), never respawns; and the first model request's plugin headers were computed pre-send to the dead addr → proxy treats it as identity-less → new ANONYMOUS session. Fixes: - probeLiveInstances: probe on a FRESH connection (Connection: close) so a draining instance (listener closed) is detected dead; a BUSY instance (accept queue still drains) still TCP-connects, so the #3654/#4811 BUSY keep is unaffected. - discoverLiveInstances: skip registry entries stamped portReleasedAt (draining) — mirrors classifySameLanePeer's skip. - registerTools catch: a manifest failure against a base that no longer accepts connections (refused connect) clears the dead base so the respawn self-heal takes over; a BUSY base (connectable) stays put. - Fresh-spawn landings (bootstrap/spawnFallback/maybeRetry) clear the old base's retryAt/failStreak so the new base's first registration is not gated behind the dead base's wall. - attachTo: log the watcher registration result (ok/failed/refused) on the host-safe channel — it was invisible in bili.log before. Regression test: tests/issue6881-dsh-attach-draining-selfheal.test.ts --- src/agent/dsh-native.ts | 37 +- src/instance.ts | 8 + src/launcher.ts | 25 +- ...e6881-dsh-attach-draining-selfheal.test.ts | 330 ++++++++++++++++++ 4 files changed, 397 insertions(+), 3 deletions(-) create mode 100644 tests/issue6881-dsh-attach-draining-selfheal.test.ts diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 5d83069fb5..06ee279654 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -62,6 +62,7 @@ import { adminRelayEnabled, resolveResignSettings } from "../config.js"; import { decodeRequestBody } from "../content-encoding.js"; import { dshContextTabEnabled, dshContextTabLabel, limitsDecodedBytes } from "../knobs.js"; import { MIN_ASSUMED_WINDOW_TOKENS } from "../util.js"; +import { tcpConnectable } from "../liveness.js"; import { markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, singleFlight } from "./native-bootstrap.js"; import { bodyOverWindowDetail as sharedBodyOverWindowDetail, getDeclaredModelEndpoints, installNativeFetchIntercept, liveStateSlotEffect, noteRoutedOrigin, observeRoutedOrigin, routedBiliModelUrl, setDeclaredModelEndpoints, type GateVerdict, type NativeInterceptState } from "./native-intercept.js"; import { matchModelEndpoint, loadDeclaredModelEndpoints } from "../model-endpoints.js"; @@ -714,6 +715,12 @@ async function bootstrap(): Promise { if (gen !== originGen) return handle.origin; state.origin = handle.origin; register.base = handle.origin; + // #6881: a fresh-spawn landing must clear the old base's backoff — a + // stale retryAt/failStreak from a dead base would gate the NEW base's + // first registration behind the old base's wall (the incident's + // backoff carried into the new world, delaying the first registration). + register.retryAt = 0; + register.failStreak = 0; // #983: do NOT write BILLION_CONTEXT_PROXY — dsh has no reader for it // here (tools use register.base, /acp uses it too), and a frozen env // turns a later same-process re-apply (cordis deactivate/reactivate) @@ -871,6 +878,11 @@ async function spawnFallback(attachOrigin: string, note: string, gen: number): P if (landingOwnsRegister(attachOrigin)) { register.base = origin; state.origin = origin; + // #6881: a fresh-spawn landing clears the old base's backoff so the + // new base's first registration is not gated behind the dead base's + // wall (the incident's backoff carried into the new world). + register.retryAt = 0; + register.failStreak = 0; } return origin; }); @@ -1073,7 +1085,7 @@ async function registerTools(ctx: PluginContext): Promise { register.failStreak = 0; register.lastRegistrationError = undefined; })() - .catch((err: unknown) => { + .catch(async (err: unknown) => { if (owner !== toolsOwner || owner.closed) return; register.toolsReady = false; // Cordis deactivates the plugin context while the host tears down @@ -1094,6 +1106,23 @@ async function registerTools(ctx: PluginContext): Promise { if (register.failStreak === REG_GIVE_UP_NOTICE_STREAK) { persistClientEvent(`bili tools still unavailable after ${register.failStreak} failed registrations (last: ${register.lastRegistrationError}) — model requests ride bili WITHOUT identity headers (wire mode: no compression, anonymous sessions). Restart dsh to restore; retries continue in the background.`); } + // #6881: a manifest failure against a base that no longer accepts + // connections is a DEAD base, not a transient fault — and backing + // off against it strands the register: maybeRetry's respawn branch + // only runs when register.base is undefined, so a dead-but-set base + // re-fetches the dead manifest on every tick and never respawns + // (the first model request then rides wire mode into an anonymous + // session). Probe the listening side directly: a refused connect + // (a draining instance closed its listener) clears the base so the + // respawn self-heal takes over; a BUSY instance (accept queue still + // drains while its loop is inside a large fold) stays put (#3654). + if (register.base === base && !(await tcpConnectable(base))) { + persistClientEvent(`manifest base ${base} stopped accepting connections — clearing the dead base so the respawn self-heal takes over (#6881)`); + register.base = undefined; + register.retryAt = 0; + armRecoveryTimer(); + return; + } // Scope the back-off to the base that actually failed: if the // register moved on while this manifest fetch was in flight (the // apply chain unfroze a dead preset, maybeRetry healed elsewhere), @@ -1189,6 +1218,12 @@ function maybeRetry(ctx: PluginContext): void { if (gen !== originGen) return; register.base = origin; state.origin = origin; + // #6881: a fresh-spawn landing clears the old base's backoff so + // the new base's first registration is not gated behind the + // dead base's wall (the incident's backoff carried into the new + // world, delaying the first registration). + register.retryAt = 0; + register.failStreak = 0; void registerTools(ctx).catch(() => {}); }) .catch(() => {}); diff --git a/src/instance.ts b/src/instance.ts index 7fa0dd5749..8e09a5a944 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -792,6 +792,14 @@ export function discoverLiveInstances(): ProxyInstanceFile[] { const out: ProxyInstanceFile[] = []; for (const e of readAllRegistryEntries()) { if (!isPidAlive(e.pid)) continue; + // #6881: a draining instance stamped portReleasedAt when its listener + // closed (#4982) — its pid still lives but the socket no longer does, + // so it must not surface as a live attach candidate (mirrors + // classifySameLanePeer's skip: a stale "I hold port N" marker would + // otherwise let discovery attach to a dead address). The fresh-conn + // health probe (#6881) is the robust catch for the single-origin-file + // path that never stamps this; this is the cheap registry-path guard. + if (e.portReleasedAt !== undefined) continue; out.push({ origin: e.origin, instanceId: e.instanceId, diff --git a/src/launcher.ts b/src/launcher.ts index b09d4a80ad..64dff2e84b 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -4229,7 +4229,7 @@ function instanceCompatible(inst: ProxyInstanceFile, opts: LaunchOptions, codeFi * health-probes each candidate in parallel. */ async function probeLiveInstances( readInstance: () => ProxyInstanceFile | { origin: string } | undefined, - fetchHealthInfo: (origin: string) => Promise, + fetchHealthInfo: (origin: string, opts?: { noKeepAlive?: boolean }) => Promise, diag?: (msg: string) => void, isConnectable: (origin: string) => Promise = tcpConnectable, ): Promise> { @@ -4240,7 +4240,16 @@ async function probeLiveInstances( const checked = await Promise.all( [...seen.values()].map(async (c): Promise<{ inst: ProxyInstanceFile; health: HealthInfo | undefined } | undefined> => { if (!isPidAlive(c.pid)) { diag?.(`native-attach: drop ${c.origin} (pid ${c.pid}): owner process gone`); return undefined; } - const health = await fetchHealthInfo(c.origin); + // #6881: probe on a FRESH connection (Connection: close) — a + // draining instance has closed its listener but still answers its + // established keep-alive connections, so a pooled socket would + // report it "alive" and discovery would attach to a dead address + // (the incident: H2 attached to P1's draining tail). A fresh + // connect is refused once the listener is closed, so the instance + // is correctly dropped. A BUSY instance (accept queue still drains) + // still TCP-connects on a fresh socket, so the #3654/#4811 BUSY + // keep below is unaffected. + const health = await fetchHealthInfo(c.origin, { noKeepAlive: true }); if (!health || !health.ok) { // #3654: a refused connection and a saturated event loop both // fail this probe — tell them apart in the log: connect-but- @@ -5621,6 +5630,18 @@ export async function ensureProxyRunning( // e.g. into Pi's input draft after herdr session restore. attachDiag(`bili: attaching to running proxy at ${inst.origin} (pid ${inst.pid})`); const reg = await registerOwner(inst); + // #6881: the watcher registration result was invisible in bili.log — + // success logged nothing and failure only hit console.error (a raw + // console.* on the attach path, #3404). Log it on the host-safe + // channel so a "failed" (watchdog not active) or "refused" (no + // watchdog) attach is visible right next to the attach line above. + if (reg === "ok") { + attachDiag(`bili: session-watcher registered at ${inst.origin} (pid ${inst.pid}) — lifecycle watchdog active`); + } else if (reg === "failed") { + attachDiag(`bili: session-watcher registration FAILED at ${inst.origin} (pid ${inst.pid}) — lifecycle watchdog NOT active; the proxy will outlive this session's cleanup until a heartbeat recovers it`); + } else { + attachDiag(`bili: session-watcher registration refused (409) at ${inst.origin} (pid ${inst.pid}) — proxy has no session-lifecycle watchdog (started without BILI_PARENT_PID)`); + } // #3544: keep re-asserting our liveness so the proxy's watchdog ages us // out by HEARTBEAT expiry, not just kill(pid,0) (identity-blind under // Windows pid reuse). No-op unless we own the pid. diff --git a/tests/issue6881-dsh-attach-draining-selfheal.test.ts b/tests/issue6881-dsh-attach-draining-selfheal.test.ts new file mode 100644 index 0000000000..fe19532965 --- /dev/null +++ b/tests/issue6881-dsh-attach-draining-selfheal.test.ts @@ -0,0 +1,330 @@ +// #6881: dsh host restart race — a new host (H2) attached to the old proxy (P1) +// while P1 was in its 5s idle-grace tail (listener already closed, established +// keep-alive connections still answering). register.base then pointed at a dead +// address, and the register could never self-heal: +// 1) the recovery timer only re-fetched the manifest (backoff 10→20→40s) and +// never respawned — maybeRetry's respawn branch only runs when +// register.base is undefined, so a dead-but-set base re-fetched the dead +// manifest on every tick; +// 2) the first model request then rode wire mode (plugin headers computed +// pre-send to the dead addr, tools not yet registered) into a new +// ANONYMOUS session, and the proxy ran preflight on the raw history before +// a 413; +// 3) after a respawn finally landed, the old base's backoff (retryAt/failStreak) +// carried into the new world, gating the new base's first registration. +// +// This suite pins the self-heal: +// A) a manifest failure against a base that no longer accepts connections +// (a draining instance's closed listener) clears the dead base so the +// respawn branch re-bases onto a replacement — and the fresh-spawn landing +// clears the old base's backoff so the new base's first registration is not +// gated behind the dead base's wall. A BUSY base (accept queue still drains) +// is NOT cleared (the #3654/#4811 keep is preserved). +// B) a registry entry stamped portReleasedAt (draining: listener closed, pid +// still alive) does not surface as a live attach candidate. +// C) the session-watcher registration result (ok/failed/refused) is logged on +// the host-safe attach channel — it was invisible in bili.log before. + +import test, { after, 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"; + +// The module-level guard reads NODE_TEST_CONTEXT while the module EVALUATES, +// and static imports hoist above any assignment — so the value must be set +// first and the modules loaded dynamically (same discipline as the #3654 suite). +process.env.NODE_TEST_CONTEXT = "1"; +delete process.env.BILLION_CONTEXT_ATTACH; +delete process.env.BILLION_CONTEXT_PROXY; + +const STATE_ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "bili-6881-state-")); +const PREV_XDG_STATE_HOME = process.env.XDG_STATE_HOME; +process.env.XDG_STATE_HOME = STATE_ROOT; +after(() => { + if (PREV_XDG_STATE_HOME === undefined) delete process.env.XDG_STATE_HOME; else process.env.XDG_STATE_HOME = PREV_XDG_STATE_HOME; + fs.rmSync(STATE_ROOT, { recursive: true, force: true }); +}); + +const dsh = await import("../src/agent/dsh-native.ts"); +const { discoverLiveInstances, registerInstanceAndWarn, markPortReleased } = await import("../src/instance.ts"); +const { ensureProxyRunning } = await import("../src/launcher.ts"); +import type { SpawnChild } from "../src/launcher.ts"; +import { rmrf } from "./tmp-rm.ts"; + +afterEach(() => { void dsh._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; +}; + +/** A mock bili proxy: /__bili/health always answers ok (so the attach health + * gate passes); /__bili/plugin/manifest answers 200 with a valid version but + * with `hasTools` controlling whether the anthropic tool list is empty. An + * empty tool list makes fetchManifest throw (so registerTools fails) while + * fetchProxyVersion still returns the version (so verifyAttachAndRecover + * attaches instead of falling back to a spawn). */ +function mockBiliHandler(counters: { manifestRequests: number }, hasTools: boolean): (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/health") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, instanceId: "inst-6881", pid: process.pid })); + return; + } + if (url === "/__bili/plugin/manifest") { + counters.manifestRequests += 1; + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + // 999.999.999 is deliberately far newer than the running build: + // isVersionNewer(VERSION, "999.999.999") is false, so + // verifyAttachAndRecover ATTACHES (not rotate-and-spawn). + version: "999.999.999", + tools: { anthropic: hasTools ? manifestTools.map((t) => ({ name: t.name, description: t.description, input_schema: t.inputSchema })) : [] }, + })); + return; + } + res.writeHead(404); + res.end("{}"); + }; +} + +async function startMockProxy(hasTools: boolean): Promise<{ origin: string; close: () => void; manifestRequests: () => number }> { + const counters = { manifestRequests: 0 }; + const server = http.createServer(mockBiliHandler(counters, hasTools)); + 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(), manifestRequests: () => counters.manifestRequests }); + }); + }); +} + +type HostCtx = Parameters[0]; + +function mockCtx() { + 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) => { 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)); + } +} + +// ———— A) dead-base self-heal + backoff clearing on a fresh-spawn landing ———— + +test("A: a manifest failure against a base that stopped accepting connections clears the dead base and respawns; the landing clears the old backoff (#6881)", async () => { + // P1: healthy (health ok, version ok) but the manifest serves no tools — + // so the register backs off (failStreak/retryAt) while the base is still + // connectable (Fix #2 must NOT clear it yet). P2: fully healthy. + const proxy1 = await startMockProxy(false); + const proxy2 = await startMockProxy(true); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-6881-")); + try { + await withEnv({ + DSH_HOME: home, + BILLION_CONTEXT_PROXY: proxy1.origin, + BILI_DSH_RECOVERY_INTERVAL_MS: "20", + BILI_DSH_RETRY_INTERVAL_MS: "20", + }, async () => { + dsh._resetRegisterForTest(proxy1.origin, false); + const ctx = mockCtx(); + dsh.apply(ctx); + // phase 1: the base is connectable but the manifest fails — the + // register backs off (failStreak=1, retryAt armed) and the base is + // KEPT (a refused connect, not a 500, is what clears it). + // failStreak is set before the await tcpConnectable; retryAt is set + // after it — wait for both so the assertion reads a settled state. + await waitFor(() => dsh._registerStateForTest().failStreak === 1 && dsh._registerStateForTest().retryAt > 0, "first failed registration backs off"); + const st = dsh._registerStateForTest(); + assert.equal(st.base, proxy1.origin, "a connectable base is not cleared by a manifest failure (the #3654 BUSY keep)"); + assert.ok(st.retryAt > 0, "the backoff is armed against the connectable base"); + + // phase 2: the base dies (listener closed — the draining tail). The + // next manifest failure now probes the base, finds it unconnectable, + // and clears it so the respawn branch re-bases onto the replacement. + proxy1.close(); + let attempts = 0; + dsh._setSpawnForTest(() => { + attempts += 1; + return Promise.resolve(proxy2.origin); + }); + try { + await waitFor(() => dsh._registerStateForTest().base === proxy2.origin, "timer-driven re-base onto the replacement after the dead base is cleared"); + assert.equal(dsh._nativeStateForTest().origin, proxy2.origin, "interceptor origin follows the healed base"); + assert.ok(attempts >= 1, "the respawn drove the stubbed spawn"); + // the fresh-spawn landing cleared the old base's backoff — the + // new base's first registration is not gated behind the dead + // base's wall (the incident's backoff carried into the new world). + assert.equal(dsh._registerStateForTest().retryAt, 0, "the old base's backoff does not carry into the new world"); + assert.equal(dsh._registerStateForTest().failStreak, 0, "the old base's fail streak does not carry into the new world"); + await waitFor(() => ctx.registeredTools.length === 1, "tool registration against the replacement"); + // churn pin: once healthy again the timer must clear itself. + const settledManifest = proxy2.manifestRequests(); + const settledAttempts = attempts; + await new Promise((r) => setTimeout(r, 150)); + assert.equal(attempts, settledAttempts, "no background respawn churn after the heal"); + assert.equal(proxy2.manifestRequests(), settledManifest, "no background re-registration after the heal"); + } finally { + dsh._setSpawnForTest(undefined); + } + }); + } finally { + proxy1.close(); + proxy2.close(); + rmrf(home); + dsh._resetRegisterForTest(undefined); + } +}); + +test("A-neg: a BUSY base (connectable, manifest still failing) is NOT cleared — the #3654/#4811 keep is preserved", async () => { + // A base that accepts connections (TCP connect succeeds) but whose manifest + // fetch keeps failing (no tools) must stay in place — clearing it would + // respawn a second writer over a live (saturated) incumbent. + const proxy1 = await startMockProxy(false); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-6881-busy-")); + try { + await withEnv({ + DSH_HOME: home, + BILLION_CONTEXT_PROXY: proxy1.origin, + BILI_DSH_RECOVERY_INTERVAL_MS: "20", + BILI_DSH_RETRY_INTERVAL_MS: "20", + }, async () => { + dsh._resetRegisterForTest(proxy1.origin, false); + dsh.apply(mockCtx()); + await waitFor(() => dsh._registerStateForTest().failStreak === 2, "two failed registrations back off"); + // the base is still connectable (the accept queue drains) — it must + // NOT be cleared into a respawn; it stays and keeps backing off. + assert.equal(dsh._registerStateForTest().base, proxy1.origin, "a connectable (BUSY) base is kept, not cleared"); + assert.ok(dsh._registerStateForTest().retryAt > 0, "the backoff keeps escalating against the BUSY base"); + }); + } finally { + proxy1.close(); + rmrf(home); + dsh._resetRegisterForTest(undefined); + } +}); + +// ———— B) a draining registry entry (portReleasedAt) is not a live candidate — + +test("B: a registry entry stamped portReleasedAt (draining) is skipped by discovery (#6881/#4982)", () => { + const st = fs.mkdtempSync(path.join(os.tmpdir(), "bili-6881-reg-")); + const prev = process.env.XDG_STATE_HOME; + process.env.XDG_STATE_HOME = st; + try { + registerInstanceAndWarn( + { instanceId: "live-6881", pid: process.pid, port: 1, origin: "http://127.0.0.1:1", startedAt: 1, lane: "dsh" }, + () => {}, + ); + registerInstanceAndWarn( + { instanceId: "draining-6881", pid: process.pid, port: 2, origin: "http://127.0.0.1:2", startedAt: 2, lane: "dsh" }, + () => {}, + ); + // the draining instance's listener closed — its port claim is withdrawn + // but its pid still lives (the drain outlives the release). + markPortReleased("draining-6881"); + const live = discoverLiveInstances(); + const ids = live.map((e) => e.instanceId); + assert.ok(ids.includes("live-6881"), "a live (unreleased) entry still surfaces"); + assert.ok(!ids.includes("draining-6881"), "a draining (portReleasedAt) entry must not surface as a live attach candidate"); + } finally { + if (prev === undefined) delete process.env.XDG_STATE_HOME; else process.env.XDG_STATE_HOME = prev; + fs.rmSync(st, { recursive: true, force: true }); + } +}); + +// ———— C) the watcher registration result is logged on the host-safe channel — + +test("C: the session-watcher registration result (ok/failed) is logged via the host-safe attach channel (#6881)", async () => { + const FP_SCRIPT = path.join(os.tmpdir(), `bili-fp-6881-${process.pid}.js`); + fs.writeFileSync(FP_SCRIPT, "// fingerprint fixture\n"); + const { entryScriptFingerprint } = await import("../src/instance.ts"); + const FP = entryScriptFingerprint(FP_SCRIPT); + assert.ok(FP !== undefined, "fixture script must fingerprint"); + const TWIN_ORIGIN = "http://127.0.0.1:18916"; + const inst = { + origin: TWIN_ORIGIN, + instanceId: "inst-6881", + pid: process.pid, + startedAt: Date.now(), + host: "127.0.0.1", + port: 18916, + passthrough: false, + mitmDomains: [] as string[], + modelWindows: {} as Record, + lane: "dsh", + codeFingerprint: FP, + }; + + async function runCase(watcherResult: "ok" | "failed"): Promise { + const diag: string[] = []; + const pending = ensureProxyRunning( + { host: "127.0.0.1", port: 0, passthrough: false, debug: false, lane: "dsh" }, + { + fetchImpl: async () => ({ ok: true }), + fetchHealthInfo: async (origin) => (origin === TWIN_ORIGIN ? { ok: true, instanceId: "inst-6881", watchdog: { armed: true } } : undefined), + readInstanceFile: () => inst, + listRegistryInstances: () => [], + spawnImpl: (_cmd: string, _args: readonly string[]): SpawnChild => { throw new Error("no spawn in this case"); }, + now: () => Date.now(), + sleep: (ms) => new Promise((r) => setTimeout(r, ms)), + registerWatcher: async () => watcherResult, + attachDiag: (msg) => { diag.push(msg); }, + scriptPath: FP_SCRIPT, + listAdoptCandidates: () => [], + isConnectable: async () => true, + }, + ); + const handle = await pending.then((h) => h, () => undefined); + assert.notEqual(handle, undefined, "the case must attach to a running proxy"); + assert.equal(handle?.attached, true, "the case must attach (not spawn)"); + return diag; + } + + const okDiag = await runCase("ok"); + assert.ok(okDiag.some((d) => d.includes("session-watcher registered") && d.includes("lifecycle watchdog active")), `the ok result must be logged: ${JSON.stringify(okDiag)}`); + + const failedDiag = await runCase("failed"); + assert.ok(failedDiag.some((d) => d.includes("session-watcher registration FAILED") && d.includes("lifecycle watchdog NOT active")), `the failed result must be logged: ${JSON.stringify(failedDiag)}`); +});