diff --git a/tests/integration/provider-guardian-transaction.test.ts b/tests/integration/provider-guardian-transaction.test.ts index 50543deb..bc29241e 100644 --- a/tests/integration/provider-guardian-transaction.test.ts +++ b/tests/integration/provider-guardian-transaction.test.ts @@ -1,6 +1,6 @@ import { spawn } from "node:child_process"; import { createHash } from "node:crypto"; -import { closeSync, constants, fchmodSync, openSync, watch } from "node:fs"; +import { closeSync, constants, fchmodSync, openSync, watch, writeFileSync } from "node:fs"; import { lstat, mkdir, @@ -29,9 +29,11 @@ const PROCESS_HEAVY_TIMEOUT_MS = 30_000; * Test watchdog only. This is not a provider protocol deadline. Child-process * scheduling can be delayed when the repository suite is under load, so the * watchdog must not encode a one-second protocol requirement that does not - * exist in production. + * exist in production. It also bounds the polls that wait for a child to start. */ const PROCESS_HANDSHAKE_WATCHDOG_MS = 5_000; +/** The extra pipe on which spawnGuardian's wrapper reports the guardian loaded. */ +const GUARDIAN_LOADED_FD = 7; const roots: string[] = []; @@ -203,10 +205,9 @@ describe("provider guardian transaction protocol", () => { }); it("creates and returns owned raw and sealed-temp identities before READY", async () => { - const { - decodeProviderGuardianReady, - encodeProviderGuardianGuard, - } = await import("../../scripts/lib/provider-guardian-protocol.ts"); + const { decodeProviderGuardianReady } = await import( + "../../scripts/lib/provider-guardian-protocol.ts" + ); const workspace = await createWorkspace("provider-guardian-ready-"); const rawPath = path.join( workspace, @@ -216,11 +217,7 @@ describe("provider guardian transaction protocol", () => { const child = spawnGuardian(workspace, nonce); const completion = waitForChild(child); const readyPayload = readFrame(child.stdout!); - child.stdin!.write(encodeProviderGuardianGuard({ - kind: "vulnerability", - nonce, - deadlineEpochMs: Date.now() + 2_000, - })); + await sendGuard(child, nonce, 2_000); const ready = decodeProviderGuardianReady( await within(readyPayload, PROCESS_HANDSHAKE_WATCHDOG_MS, "guardian READY"), @@ -314,18 +311,11 @@ describe("provider guardian transaction protocol", () => { ); it("cleans guardian-created objects when the parent closes immediately after guard", async () => { - const { encodeProviderGuardianGuard } = await import( - "../../scripts/lib/provider-guardian-protocol.ts" - ); const workspace = await createWorkspace("provider-guardian-parent-startup-death-"); const nonce = Buffer.alloc(32, 0x2b); const child = spawnGuardian(workspace, nonce); const completion = waitForChild(child); - child.stdin!.end(encodeProviderGuardianGuard({ - kind: "vulnerability", - nonce, - deadlineEpochMs: Date.now() + 2_000, - })); + await sendGuard(child, nonce, 2_000, { closeInput: true }); await expect(within(completion, PROCESS_HANDSHAKE_WATCHDOG_MS, "guardian parent startup death")) .resolves.toEqual({ code: 125, signal: null }); @@ -338,7 +328,6 @@ describe("provider guardian transaction protocol", () => { decodeProviderGuardianPublished, decodeProviderGuardianReady, encodeProviderGuardianCommit, - encodeProviderGuardianGuard, encodeProviderGuardianPublish, } = await import("../../scripts/lib/provider-guardian-protocol.ts"); const workspace = await createWorkspace("provider-guardian-publish-"); @@ -349,11 +338,7 @@ describe("provider guardian transaction protocol", () => { const child = spawnGuardian(workspace, nonce); const completion = waitForChild(child); const readyPayload = readFrame(child.stdout!); - child.stdin!.write(encodeProviderGuardianGuard({ - kind: "vulnerability", - nonce, - deadlineEpochMs: Date.now() + 3_000, - })); + await sendGuard(child, nonce, 3_000); const ready = decodeProviderGuardianReady( await within(readyPayload, PROCESS_HANDSHAKE_WATCHDOG_MS, "guardian READY"), nonce, @@ -616,6 +601,10 @@ describe("provider guardian transaction protocol", () => { .resolves.toEqual(["untrusted"]); }); + // The loser is rejected only by the client's fixed 5s READY limit + // (RESPONSE_TIMEOUT_MS): its guardian fails the link but does not exit while + // its stdin read is still pending. That fixed wait would leave half of the + // 10s default for three guardian start-ups. it("allows exactly one same-kind guardian without deleting the winner", async () => { const { startProviderGuardian } = await import( "../../scripts/lib/provider-guardian-client.ts" @@ -651,13 +640,12 @@ describe("provider guardian transaction protocol", () => { .resolves.toEqual([]); await expect(readdir(path.join(workspace, "provider-evidence"))) .resolves.toEqual(["untrusted"]); - }); + }, PROCESS_HEAVY_TIMEOUT_MS); it("exits nonzero after cleanup when the diagnostic stderr pipe is closed", async () => { - const { - decodeProviderGuardianReady, - encodeProviderGuardianGuard, - } = await import("../../scripts/lib/provider-guardian-protocol.ts"); + const { decodeProviderGuardianReady } = await import( + "../../scripts/lib/provider-guardian-protocol.ts" + ); const workspace = await createWorkspace("provider-guardian-closed-stderr-"); const evidenceRoot = path.join(workspace, "provider-evidence"); const rawDirectory = path.join(evidenceRoot, "untrusted"); @@ -665,11 +653,7 @@ describe("provider guardian transaction protocol", () => { const child = spawnGuardian(workspace, nonce); const completion = waitForChild(child); const readyPayload = readFrame(child.stdout!); - child.stdin!.write(encodeProviderGuardianGuard({ - kind: "vulnerability", - nonce, - deadlineEpochMs: Date.now() + 3_000, - })); + await sendGuard(child, nonce, 3_000); const ready = decodeProviderGuardianReady( await within(readyPayload, PROCESS_HANDSHAKE_WATCHDOG_MS, "closed-stderr guardian READY"), nonce, @@ -687,10 +671,9 @@ describe("provider guardian transaction protocol", () => { }); it("expires an uncommitted lease only after cleaning every owned object", async () => { - const { - decodeProviderGuardianReady, - encodeProviderGuardianGuard, - } = await import("../../scripts/lib/provider-guardian-protocol.ts"); + const { decodeProviderGuardianReady } = await import( + "../../scripts/lib/provider-guardian-protocol.ts" + ); const workspace = await createWorkspace("provider-guardian-deadline-v2-"); const evidenceRoot = path.join(workspace, "provider-evidence"); const rawPath = path.join(evidenceRoot, "untrusted/vulnerability-report.json"); @@ -698,11 +681,7 @@ describe("provider guardian transaction protocol", () => { const child = spawnGuardian(workspace, nonce); const completion = waitForChild(child); const readyPayload = readFrame(child.stdout!); - child.stdin!.write(encodeProviderGuardianGuard({ - kind: "vulnerability", - nonce, - deadlineEpochMs: Date.now() + 350, - })); + await sendGuard(child, nonce, 350); const ready = decodeProviderGuardianReady( await within(readyPayload, PROCESS_HANDSHAKE_WATCHDOG_MS, "deadline guardian READY"), nonce, @@ -721,13 +700,18 @@ describe("provider guardian transaction protocol", () => { "../../scripts/lib/provider-guardian-client.ts" ); const workspace = await createWorkspace("provider-guardian-near-timeout-"); + // The client stamps the deadline after it spawns the guardian, so the + // deadline is at least 2s from here. Waking 1s from here, not 1s after + // start-up returns, keeps publish and commit 1s on a slow host too. + const startedAt = Date.now(); const lease = await startProviderGuardian({ kind: "vulnerability", workspaceRoot: workspace, leaseMs: 2_000, guardianScript: path.resolve("scripts/lib/provider-raw-guardian.ts"), }); - await new Promise((resolve) => setTimeout(resolve, 1_000)); + await new Promise((resolve) => + setTimeout(resolve, Math.max(0, startedAt + 1_000 - Date.now()))); const bytes = Buffer.from('{"validated":"near-timeout"}\n'); await lease.publish(bytes); @@ -836,7 +820,7 @@ async function createPausedGuardianFixture(workspace: string): Promise { } async function waitForFile(target: string): Promise { - const deadline = Date.now() + 1_000; + const deadline = Date.now() + PROCESS_HANDSHAKE_WATCHDOG_MS; while (Date.now() <= deadline) { try { return await readFile(target); @@ -866,7 +850,6 @@ async function establishPublishedGuardian( const { decodeProviderGuardianPublished, decodeProviderGuardianReady, - encodeProviderGuardianGuard, encodeProviderGuardianPublish, } = await import("../../scripts/lib/provider-guardian-protocol.ts"); const evidenceRoot = path.join(workspace, "provider-evidence"); @@ -875,11 +858,7 @@ async function establishPublishedGuardian( const child = spawnGuardian(workspace, nonce); const completion = waitForChild(child); const readyPayload = readFrame(child.stdout!); - child.stdin!.write(encodeProviderGuardianGuard({ - kind: "vulnerability", - nonce, - deadlineEpochMs: Date.now() + 3_000, - })); + await sendGuard(child, nonce, 3_000); const ready = decodeProviderGuardianReady( await within(readyPayload, PROCESS_HANDSHAKE_WATCHDOG_MS, "guardian READY"), nonce, @@ -986,16 +965,58 @@ function spawnGuardian( constants.O_RDWR | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600, )); - return spawn(process.execPath, [path.resolve("scripts/lib/provider-raw-guardian.ts")], { + // The wrapper imports the unchanged guardian, then writes one byte to an + // extra pipe and closes it: by then bootstrap has run and the stdin reader + // is attached (sendGuard). + const loader = path.join(workspace, "loaded-signal-guardian.mjs"); + writeFileSync(loader, [ + 'import { closeSync, writeSync } from "node:fs";', + `await import(${JSON.stringify( + pathToFileURL(path.resolve("scripts/lib/provider-raw-guardian.ts")).href, + )});`, + `writeSync(${GUARDIAN_LOADED_FD}, "1");`, + `closeSync(${GUARDIAN_LOADED_FD});`, + "", + ].join("\n")); + return spawn(process.execPath, [loader], { cwd: workspace, env: {}, - stdio: ["pipe", "pipe", "pipe", ...openedFds], + stdio: ["pipe", "pipe", "pipe", ...openedFds, "pipe"], }); } finally { for (const fd of openedFds) closeSync(fd); } } +/** + * A guard carries an absolute deadline, so it goes out only once the guardian + * reads stdin. Sent at spawn, the lease also pays for process start-up, which + * has no bound on a loaded host: a 350ms lease then expires before the + * guardian decodes the guard. + */ +async function sendGuard( + child: ReturnType, + nonce: Buffer, + leaseMs: number, + options: Readonly<{ closeInput?: boolean }> = {}, +): Promise { + const { encodeProviderGuardianGuard } = await import( + "../../scripts/lib/provider-guardian-protocol.ts" + ); + const loaded = (child.stdio as readonly unknown[])[GUARDIAN_LOADED_FD] as NodeJS.ReadableStream; + await within(new Promise((resolve, reject) => { + loaded.once("data", () => resolve()); + loaded.once("end", () => reject(new Error("guardian exited before it loaded"))); + }), PROCESS_HANDSHAKE_WATCHDOG_MS, "guardian start-up"); + const guard = encodeProviderGuardianGuard({ + kind: "vulnerability", + nonce, + deadlineEpochMs: Date.now() + leaseMs, + }); + if (options.closeInput) child.stdin!.end(guard); + else child.stdin!.write(guard); +} + function spawnGuardianWithPrivateModes( workspace: string, nonce: Buffer, @@ -1095,7 +1116,7 @@ async function directChildPids(parentPid: number): Promise> { } async function waitForNewDirectChild(previous: ReadonlySet): Promise { - const deadline = Date.now() + 1_000; + const deadline = Date.now() + PROCESS_HANDSHAKE_WATCHDOG_MS; while (Date.now() <= deadline) { for (const pid of await directChildPids(process.pid)) { if (!previous.has(pid)) return pid; @@ -1106,7 +1127,7 @@ async function waitForNewDirectChild(previous: ReadonlySet): Promise { - const deadline = Date.now() + 1_000; + const deadline = Date.now() + PROCESS_HANDSHAKE_WATCHDOG_MS; while (Date.now() <= deadline) { const status = await readFile(`/proc/${pid}/status`, "utf8"); if (/^State:\s+T/mu.test(status)) return;