From 044d4fbd541078d1e920eda56c223cdca7e80102 Mon Sep 17 00:00:00 2001 From: Codex Date: Sun, 15 Nov 2026 11:33:47 -0600 Subject: [PATCH] Pin reviewed setup fixtures in CI graph qualification --- .../fixtures/isolated-test-home.reviewed.txt | 31 ++++++++ .../ci/fixtures/setup-supertest.reviewed.txt | 78 +++++++++++++++++++ .../ci/test-selection.integration.test.mjs | 6 +- 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 scripts/ci/fixtures/isolated-test-home.reviewed.txt create mode 100644 scripts/ci/fixtures/setup-supertest.reviewed.txt diff --git a/scripts/ci/fixtures/isolated-test-home.reviewed.txt b/scripts/ci/fixtures/isolated-test-home.reviewed.txt new file mode 100644 index 00000000000..0463f1a6b6d --- /dev/null +++ b/scripts/ci/fixtures/isolated-test-home.reviewed.txt @@ -0,0 +1,31 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +export function createIsolatedTestHome() { + const { mkdtempSync, lstatSync, openSync, fchmodSync, closeSync, readdirSync, rmSync, constants } = fs; + const home = mkdtempSync(path.join(os.tmpdir(), "paperclip-vitest-home-")); + const identity = lstatSync(home); + const cleanup = () => { + let current; + try { current = lstatSync(home); } + catch (error) { + if ((error as NodeJS.ErrnoException).code === "ENOENT") return; + throw error; + } + if (!current.isDirectory() || current.dev !== identity.dev || current.ino !== identity.ino) { + throw new Error("Refusing to clean a replaced isolated test home"); + } + const makeDirectoriesWritable = (directory: string) => { + const fd = openSync(directory, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW); + try { fchmodSync(fd, 0o700); } + finally { closeSync(fd); } + for (const entry of readdirSync(directory, { withFileTypes: true })) { + if (entry.isDirectory()) makeDirectoriesWritable(path.join(directory, entry.name)); + } + }; + makeDirectoriesWritable(home); + rmSync(home, { recursive: true, force: true, maxRetries: 8, retryDelay: 50 }); + }; + return { home, cleanup }; +} diff --git a/scripts/ci/fixtures/setup-supertest.reviewed.txt b/scripts/ci/fixtures/setup-supertest.reviewed.txt new file mode 100644 index 00000000000..be189a6abe0 --- /dev/null +++ b/scripts/ci/fixtures/setup-supertest.reviewed.txt @@ -0,0 +1,78 @@ +import fs from "node:fs"; +import { createIsolatedTestHome } from "./helpers/isolated-test-home.js"; +import { createRequire } from "node:module"; +import type { AddressInfo, Server as NetServer } from "node:net"; +import os from "node:os"; +import path from "node:path"; +import { Server as TlsServer } from "node:tls"; + + +type SupertestServer = NetServer & { + address(): ReturnType; + listen(port: number): NetServer; +}; + +type SupertestTestInstance = { + _server?: SupertestServer; +}; + +type SupertestTestConstructor = { + prototype: { + serverAddress(this: SupertestTestInstance, app: SupertestServer, path: string): string; + __paperclipLoopbackPatched?: boolean; + }; +}; + +// Keep the original home available only for read-only host port-lease checks. +process.env.PAPERCLIP_TEST_HOST_HOME ??= process.env.PAPERCLIP_HOME?.trim() || path.join(os.homedir(), ".paperclip"); + +// Receipt-spool recovery must never scan a developer's instance during tests. +const { home: paperclipTestHome, cleanup: cleanupPaperclipTestHome } = createIsolatedTestHome(); +process.env.PAPERCLIP_HOME = paperclipTestHome; +process.env.PAPERCLIP_CONFIG = path.join(paperclipTestHome, "config.json"); +process.env.PAPERCLIP_INSTANCE_ID = `vt-${process.pid}`; +// Setup-file afterAll hooks run before suites drain asynchronous heartbeats. +// Cleanup at worker exit, after all suite teardown, avoids deleting live state. +process.once("exit", cleanupPaperclipTestHome); + +const require = createRequire(import.meta.url); +const SupertestTest = require("supertest/lib/test.js") as SupertestTestConstructor; + +if (!process.env.CODEX_HOME) { + const codexHome = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-vitest-codex-home-")); + fs.writeFileSync(path.join(codexHome, "auth.json"), '{"OPENAI_API_KEY":"sk-vitest"}\n', { mode: 0o600 }); + process.env.CODEX_HOME = codexHome; +} + +// The automatic Tailscale HTTPS default (PAP-17158) probes for a real host +// broker socket, so leaving it enabled would make every test that starts a +// service named `paperclip-dev` behave differently on a broker-capable host +// than on CI. Tests that exercise the default opt in explicitly. +if (!process.env.PAPERCLIP_MANAGED_RUNTIME_HTTPS) { + process.env.PAPERCLIP_MANAGED_RUNTIME_HTTPS = "off"; +} + +if (!SupertestTest.prototype.__paperclipLoopbackPatched) { + SupertestTest.prototype.serverAddress = function serverAddress(app, path) { + const addr = app.address(); + + if (!addr) { + this._server = app.listen(0) as SupertestServer; + } + + const listeningAddress = app.address() as AddressInfo | string | null; + if (!listeningAddress || typeof listeningAddress === "string") { + throw new Error("Expected Supertest server to listen on a TCP port"); + } + + const host = listeningAddress.address === "::" + ? "[::1]" + : listeningAddress.address === "0.0.0.0" + ? "127.0.0.1" + : listeningAddress.address; + const protocol = app instanceof TlsServer ? "https" : "http"; + return `${protocol}://${host}:${listeningAddress.port}${path}`; + }; + + SupertestTest.prototype.__paperclipLoopbackPatched = true; +} diff --git a/scripts/ci/test-selection.integration.test.mjs b/scripts/ci/test-selection.integration.test.mjs index 38818dd5b34..7a9940f26ae 100644 --- a/scripts/ci/test-selection.integration.test.mjs +++ b/scripts/ci/test-selection.integration.test.mjs @@ -89,7 +89,11 @@ test("real graph retains direct and indirect filesystem, process and opaque depe await assert.rejects(select(["ui/src/leaf.ts"]), /Opaque setup builtin/, "setup filesystem reads can affect every test outside the import graph"); mkdirSync(path.join(directory, "server/src/__tests__/helpers"), { recursive: true }); for (const file of ["server/src/__tests__/setup-supertest.ts", "server/src/__tests__/helpers/isolated-test-home.ts"]) { - write(file, readFileSync(path.join(root, file), "utf8")); + // Exercise the exact reviewed setup contract, independently of later product setup edits. + const source = file === "server/src/__tests__/setup-supertest.ts" + ? new URL("./fixtures/setup-supertest.reviewed.txt", import.meta.url) + : new URL("./fixtures/isolated-test-home.reviewed.txt", import.meta.url); + write(file, readFileSync(source, "utf8")); } write("ui/setup.ts", 'import "../server/src/__tests__/setup-supertest.ts";'); const reviewed = await select(["ui/src/leaf.ts"]);