diff --git a/docs/development/client-verification.md b/docs/development/client-verification.md index 7ac52514f9..0e8497f80b 100644 --- a/docs/development/client-verification.md +++ b/docs/development/client-verification.md @@ -322,6 +322,9 @@ records the model fixture's answer, and `clientFinalSeen` records the exact answer in the terminal headless JSON event. A passing receipt also requires successful client termination, uninstall, and verifier cleanup. +Passing native verifier receipts require verified lineage with no live +descendants. + The DSH acceptance journey also exercises public setup, approval, doctor, generated-launcher MCP startup and uninstall without an installed Harness. That journey proves REA workflow behavior, not native Cordis expression diff --git a/scripts/verify-deepseek-client.mjs b/scripts/verify-deepseek-client.mjs index 697012c605..c51e5f3817 100644 --- a/scripts/verify-deepseek-client.mjs +++ b/scripts/verify-deepseek-client.mjs @@ -19,7 +19,11 @@ import { findClientResult, isClientJavaScriptEvidence, } from "./verify/clients/result-discovery.mjs"; -import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs"; +import { + assertVerifierCleanupComplete, + completeVerifierRun, + createVerifierRun, +} from "./lib/verifier-run.mjs"; import { importVerifierSource } from "./lib/verifier-source-loader.mjs"; import { createOpenAiModelFixture } from "./verify/clients/model-fixture.mjs"; import { VerifierFixture } from "./lib/verifier-fixture.mjs"; @@ -456,15 +460,24 @@ await withVerifierCleanup(async () => { fixtureFailure: fixture?.failure, verifier: completed, }; - await withVerifierCleanup( - () => - writeFile(join(lab, "receipt.json"), JSON.stringify(receipt, null, 2)), - [ - () => { - console.log(JSON.stringify(receipt)); - }, - ], - ); + const publishReceipt = () => + withVerifierCleanup( + () => + writeFile( + join(lab, "receipt.json"), + JSON.stringify(receipt, null, 2), + ), + [() => console.log(JSON.stringify(receipt))], + ); + await withVerifierCleanup(() => { + if (!receipt.passed) return; + try { + assertVerifierCleanupComplete(receipt.verifier.process_lineage); + } catch (cause) { + receipt.passed = false; + throw cause; + } + }, [publishReceipt]); }, () => { const reason = commands.finish(); diff --git a/tests/boundary/process/deepseekVerifierLineage.test.ts b/tests/boundary/process/deepseekVerifierLineage.test.ts new file mode 100644 index 0000000000..a8f9e84bb6 --- /dev/null +++ b/tests/boundary/process/deepseekVerifierLineage.test.ts @@ -0,0 +1,138 @@ +import { readFile, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; + +import { expect } from "vitest"; + +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { processTest } from "../../support/process/processFixture.js"; + +const script = new URL( + "../../../scripts/verify-deepseek-client.mjs", + import.meta.url, +); +const contracts = new URL("../dist/contracts/toolContracts.js", script).href; +const runModule = new URL("./lib/verifier-run.mjs", script).href; +const moduleUrl = (source: string) => + `data:text/javascript;base64,${Buffer.from(source).toString("base64")}`; + +const model = moduleUrl(` +import { TOOL_CONTRACTS } from ${JSON.stringify(contracts)}; +export async function createOpenAiModelFixture({ onRequest }) { + await onRequest({ + body: { messages: [] }, + tools: TOOL_CONTRACTS.map(({ name }) => ({ + name: "mcp__rea__" + name, + parameters: { type: "string", pattern: "^fixture$" }, + })), + }); + return { baseUrl: "http://127.0.0.1:1/v1", async close() {} }; +} +`); + +processTest.skipIf(process.platform === "win32").for([ + ["empty", "verified", undefined], + [ + "live owned process", + "verified", + "Owned descendants survived verifier cleanup", + ], + ["foreign token", "unavailable", "Verifier cleanup could not be verified"], +] as const)( + "gates the DeepSeek receipt on %s lineage", + async ([scenario, status, failure], { processes }) => { + const state = await createTestTempDirectory("rea-deepseek-lineage-"); + const client = join(state, "headless"); + const hook = join(state, "hook.mjs"); + const lineage = moduleUrl(` +import { spawn } from "node:child_process"; +import { once } from "node:events"; +import { completeVerifierRun as complete } from ${JSON.stringify(runModule)}; +export { createVerifierRun, assertVerifierCleanupComplete } from ${JSON.stringify(runModule)}; + +export async function completeVerifierRun(run) { + if (${scenario === "empty"}) return complete(run); + const child = spawn(process.execPath, ["-e", "process.stdin.resume()"], { + env: { REA_PROCESS_RUN_ID: ${scenario === "foreign token" ? '"foreign-probe-token"' : "run.run_id"} }, + stdio: ["pipe", "ignore", "ignore"], + }); + const closed = once(child, "close"); + try { + await once(child, "spawn"); + return await complete(run); + } finally { + child.stdin.end(); + await closed; + } +} +`); + await writeFile( + hook, + ` +import { registerHooks } from "node:module"; +registerHooks({ + resolve(specifier, context, next) { + if (context.parentURL === ${JSON.stringify(script.href)}) { + if (specifier === "./lib/verifier-run.mjs") + return { url: ${JSON.stringify(lineage)}, shortCircuit: true }; + if (specifier === "./verify/clients/model-fixture.mjs") + return { url: ${JSON.stringify(model)}, shortCircuit: true }; + } + return next(specifier, context); + }, +}); +`, + ); + await writeFile( + client, + `#!/bin/sh +if [ "$1" = --version ]; then + printf '%s\\n' 'lineage fixture' +else + printf '%s\\n' '{"type":"final","text":"REA_CLIENT_COMPATIBILITY_OK"}' +fi +`, + { mode: 0o700 }, + ); + + const result = await processes.run( + process.execPath, + ["--import", hook, fileURLToPath(script), "chat"], + { + env: { + ...process.env, + TMPDIR: state, + REA_VERIFY_DEEPSEEK_COMMAND: client, + }, + }, + ); + const receipt = JSON.parse(result.stdout); + expect(receipt, result.stderr).toMatchObject({ + lifecycle: [ + "dry-run", + "setup", + "idempotent-setup", + "doctor", + "native-chat", + "uninstall", + ], + verifier: { process_lineage: { status } }, + }); + if (status === "verified") + expect(receipt.verifier.process_lineage.descendants).toHaveLength( + failure === undefined ? 0 : 1, + ); + else + expect(receipt.verifier.process_lineage.reason).toMatch( + /descendant \d+ run token did not match/u, + ); + expect( + JSON.parse( + await readFile(join(receipt.artifacts, "receipt.json"), "utf8"), + ), + ).toEqual(receipt); + expect(receipt.passed).toBe(failure === undefined); + expect(result.exitCode).toBe(failure === undefined ? 0 : 1); + if (failure !== undefined) expect(result.stderr).toContain(failure); + }, +);