From 41fc470072d441eefc3536a3de4a3ebb7902b5e4 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:06:14 -0500 Subject: [PATCH] fix(ci): verify packaged Agent Environment bridges (#12874) * test(agentenv): observe timeout before polling a pending run * test(agentenv): verify bridge files at package boundary --- bridge/agentenv/test_capture.py | 22 +++++++------ scripts/ci/scopes.mjs | 1 + scripts/verify-packed-bridge.mjs | 9 +++++ .../desktop/AgentEnvNoteRecords.test.ts | 2 -- src/desktop/AgentEnvBridgeFiles.test.ts | 30 ----------------- src/desktop/AgentEnvBridgeSources.ts | 10 ++++++ src/desktop/AgentEnvComputerHelper.ts | 5 ++- src/desktop/AgentEnvSolveHelper.ts | 5 ++- src/desktop/BuildComputerController.ts | 5 ++- src/desktop/SolveSandboxRoot.ts | 5 ++- .../boundary/filesystem/ciChangeScope.test.ts | 4 +++ .../filesystem/ghidraBridgeSources.test.ts | 33 ------------------- .../process/packageTarListing.test.ts | 4 +++ .../process/packedBridgeVerification.test.ts | 17 ++++++---- 14 files changed, 58 insertions(+), 94 deletions(-) delete mode 100644 src/desktop/AgentEnvBridgeFiles.test.ts create mode 100644 src/desktop/AgentEnvBridgeSources.ts diff --git a/bridge/agentenv/test_capture.py b/bridge/agentenv/test_capture.py index dce661eea3..6c1fd05ca4 100644 --- a/bridge/agentenv/test_capture.py +++ b/bridge/agentenv/test_capture.py @@ -88,10 +88,6 @@ class LongRunTest(unittest.TestCase): "if len(sys.argv) > 1 and sys.argv[1] == 'wait':\n" " open(sys.argv[2], 'w').close()\n" " time.sleep(30)\n" - "if len(sys.argv) > 1 and sys.argv[1] == 'ignore':\n" - " signal.signal(signal.SIGINT, signal.SIG_IGN)\n" - " open(sys.argv[2], 'w').close()\n" - " while not os.path.exists(sys.argv[3]): time.sleep(0.02)\n" "if len(sys.argv) > 1 and sys.argv[1] == 'stderr':\n" " sys.stderr.write('x' * 100000 + 'TAIL')\n" " print(hashlib.sha256(json.dumps(sys.argv[2:]).encode()).hexdigest())\n" @@ -251,16 +247,18 @@ class LongRunTest(unittest.TestCase): self.assertTrue(result["pending"]) def test_reports_a_timed_out_child_that_has_not_stopped(self): - ready, release = self.root / "ready", self.root / "release" - self.assertEqual(self.begin(["ignore", str(ready), str(release)], timeout=3), {"started": True}) + release = self.start_resistant_child() try: - result = self.command("run-poll", job=str(self.job), token="owner-token", timeout_s=4) + self.assertTrue(self.wait_until((self.job / "timed_out").exists)) + self.assertTrue(self.wait_until((self.root / "release.pid").is_file)) + result = self.command("run-poll", job=str(self.job), token="owner-token", timeout_s=0) self.assertTrue(result["pending"]) self.assertTrue(result["timed_out"]) - self.assertTrue(ready.exists()) finally: release.touch() - self.command("run-poll", job=str(self.job), token="owner-token", timeout_s=5) + finished = self.command("run-poll", job=str(self.job), token="owner-token", timeout_s=5) + self.assertTrue(finished["finished"] and finished["settled"]) + self.assertEqual(finished["exit_code"], 0) def start_foreign_stderr_holder(self, timeout): release = self.root / "release" @@ -380,7 +378,7 @@ class LongRunTest(unittest.TestCase): release.touch() closer.join(timeout=2) - def test_kills_a_leader_that_ignores_sigint_and_sigterm(self): + def start_resistant_child(self): release = self.root / "release" # Inherit the mask until the command installs both ignore handlers. # A slow startup can cross the real deadline before Python reaches them. @@ -391,6 +389,10 @@ class LongRunTest(unittest.TestCase): self.assertEqual(self.begin(["resist", str(release)], timeout=1), {"started": True}) finally: signal.pthread_sigmask(signal.SIG_SETMASK, previous_mask) + return release + + def test_kills_a_leader_that_ignores_sigint_and_sigterm(self): + self.start_resistant_child() result = self.command( "run-poll", job=str(self.job), token="owner-token", timeout_s=12, command_timeout=20, ) diff --git a/scripts/ci/scopes.mjs b/scripts/ci/scopes.mjs index 23f4b484dc..d368242523 100644 --- a/scripts/ci/scopes.mjs +++ b/scripts/ci/scopes.mjs @@ -244,6 +244,7 @@ const PATH_SCOPES = { "tests/process-global/runnerCancellation.test.ts", "tests/process-global/windowsOwnedCommand.test.ts", "tests/process-global/curatedWindowsLane.test.ts", + "src/desktop/AgentEnvBridgeSources.ts", "src/ghidra/GhidraBridgeSources.ts", "native/windows/**", "src/process/capture/FilesystemSnapshot.ts", diff --git a/scripts/verify-packed-bridge.mjs b/scripts/verify-packed-bridge.mjs index edad495044..64514d5d37 100644 --- a/scripts/verify-packed-bridge.mjs +++ b/scripts/verify-packed-bridge.mjs @@ -1,7 +1,15 @@ +import { relative, sep } from "node:path"; +import { fileURLToPath } from "node:url"; + +import { AGENTENV_BRIDGE_SOURCES } from "../src/desktop/AgentEnvBridgeSources.ts"; import { GHIDRA_BRIDGE_SOURCES } from "../src/ghidra/GhidraBridgeSources.ts"; const packedHopperBridge = "package/bridge/hopper_bridge.py"; const packedCutterBridge = "package/bridge/cutter/rea_bridge.py"; +const root = fileURLToPath(new URL("../", import.meta.url)); +const packedAgentEnvSources = Object.values(AGENTENV_BRIDGE_SOURCES).map( + (url) => `package/${relative(root, fileURLToPath(url)).split(sep).join("/")}`, +); /** Check shipped bridge files; real-provider lanes verify runtime behavior. */ export async function verifyPackedBridge({ tarball, packedFiles, commands }) { @@ -15,6 +23,7 @@ export async function verifyPackedBridge({ tarball, packedFiles, commands }) { for (const path of [ packedHopperBridge, packedCutterBridge, + ...packedAgentEnvSources, ...Object.values(GHIDRA_BRIDGE_SOURCES).map( (name) => `package/bridge/ghidra/${name}`, ), diff --git a/src/application/desktop/AgentEnvNoteRecords.test.ts b/src/application/desktop/AgentEnvNoteRecords.test.ts index eaba042c9f..c8cd668752 100644 --- a/src/application/desktop/AgentEnvNoteRecords.test.ts +++ b/src/application/desktop/AgentEnvNoteRecords.test.ts @@ -8,7 +8,6 @@ it("keeps complete note records and an unfinished trailing fragment", () => { expect(parseAgentEnvNotes(`${JSON.stringify(record)}\n{"id":`)).toEqual([ record, ]); - expect(parseAgentEnvNotes(`${JSON.stringify(record)}\n`)).toEqual([record]); expect(parseAgentEnvNotes("")).toEqual([]); }); @@ -16,5 +15,4 @@ it("rejects a newline-terminated note that is not a record", () => { expect(() => parseAgentEnvNotes(`${JSON.stringify(record)}\n{bad}\n`), ).toThrow(SyntaxError); - expect(() => parseAgentEnvNotes("{bad}\n")).toThrow(SyntaxError); }); diff --git a/src/desktop/AgentEnvBridgeFiles.test.ts b/src/desktop/AgentEnvBridgeFiles.test.ts deleted file mode 100644 index 736e9520bb..0000000000 --- a/src/desktop/AgentEnvBridgeFiles.test.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { readdir, readFile } from "node:fs/promises"; - -import { expect, it } from "vitest"; - -const ROOT = new URL("../../", import.meta.url); - -it("ships every agentenv helper the desktop code launches in the package", async () => { - const files = ( - JSON.parse(await readFile(new URL("package.json", ROOT), "utf8")) as { - files: string[]; - } - ).files; - const sources = await readdir(new URL("src/desktop/", ROOT)); - const launched = new Set(); - for (const source of sources.filter( - (name) => name.endsWith(".ts") && !name.endsWith(".test.ts"), - )) - for (const [, helper] of ( - await readFile(new URL(`src/desktop/${source}`, ROOT), "utf8") - ).matchAll(/"\.\.\/\.\.\/(bridge\/agentenv\/[\w.-]+\.py)"/gu)) - launched.add(helper as string); - - expect([...launched].sort()).toEqual([ - "bridge/agentenv/capture.py", - "bridge/agentenv/computer.py", - "bridge/agentenv/solve.py", - "bridge/agentenv/solve_cleanup.py", - ]); - for (const helper of launched) expect(files).toContain(helper); -}); diff --git a/src/desktop/AgentEnvBridgeSources.ts b/src/desktop/AgentEnvBridgeSources.ts new file mode 100644 index 0000000000..c7996e4f46 --- /dev/null +++ b/src/desktop/AgentEnvBridgeSources.ts @@ -0,0 +1,10 @@ +/** Bridge files shared by the runtime and the package verifier. */ +export const AGENTENV_BRIDGE_SOURCES = Object.freeze({ + capture: new URL("../../bridge/agentenv/capture.py", import.meta.url).href, + computer: new URL("../../bridge/agentenv/computer.py", import.meta.url).href, + solve: new URL("../../bridge/agentenv/solve.py", import.meta.url).href, + solveCleanup: new URL( + "../../bridge/agentenv/solve_cleanup.py", + import.meta.url, + ).href, +} as const); diff --git a/src/desktop/AgentEnvComputerHelper.ts b/src/desktop/AgentEnvComputerHelper.ts index 7778f8b03b..6dbb76b198 100644 --- a/src/desktop/AgentEnvComputerHelper.ts +++ b/src/desktop/AgentEnvComputerHelper.ts @@ -25,12 +25,11 @@ import { import { errorMessage } from "../kernel/errorMessage.js"; import type { ComputerRelease } from "../domain/desktop/computerRelease.js"; import { AGENTENV_PROVIDER } from "./AgentEnvProviderIdentity.js"; +import { AGENTENV_BRIDGE_SOURCES } from "./AgentEnvBridgeSources.js"; const execFileAsync = promisify(execFile); -const HELPER = fileURLToPath( - new URL("../../bridge/agentenv/computer.py", import.meta.url), -); +const HELPER = fileURLToPath(AGENTENV_BRIDGE_SOURCES.computer); const HANDSHAKE_BYTES = 64 * 1024; const STDERR_BYTES = 16 * 1024; const TEARDOWN_MS = 120_000; diff --git a/src/desktop/AgentEnvSolveHelper.ts b/src/desktop/AgentEnvSolveHelper.ts index 63c30b8bf3..50c3e53412 100644 --- a/src/desktop/AgentEnvSolveHelper.ts +++ b/src/desktop/AgentEnvSolveHelper.ts @@ -20,10 +20,9 @@ import { } from "./AgentEnvComputerHelper.js"; import { AGENTENV_PROVIDER } from "./AgentEnvProviderIdentity.js"; import { errorMessage } from "../kernel/errorMessage.js"; +import { AGENTENV_BRIDGE_SOURCES } from "./AgentEnvBridgeSources.js"; -const HELPER = fileURLToPath( - new URL("../../bridge/agentenv/solve.py", import.meta.url), -); +const HELPER = fileURLToPath(AGENTENV_BRIDGE_SOURCES.solve); const REPORT_BYTES = 256 * 1024; const STDERR_BYTES = 16 * 1024; /** The longest one solve may take, its deploy and teardown included, before REA stops it. */ diff --git a/src/desktop/BuildComputerController.ts b/src/desktop/BuildComputerController.ts index c14848620c..1819f1c64e 100644 --- a/src/desktop/BuildComputerController.ts +++ b/src/desktop/BuildComputerController.ts @@ -17,10 +17,9 @@ import type { BuildControllerAnswer, ControlledComputer, } from "../ports/BuildComputerControl.js"; +import { AGENTENV_BRIDGE_SOURCES } from "./AgentEnvBridgeSources.js"; -const CONTROLLER = fileURLToPath( - new URL("../../bridge/agentenv/capture.py", import.meta.url), -); +const CONTROLLER = fileURLToPath(AGENTENV_BRIDGE_SOURCES.capture); /** The most file bytes one `data/get` brings back, so a large bundle arrives in several answers. */ const FETCH_BATCH_BYTES = 8 * 1024 * 1024; /** Default time allowed for one REA command in the build computer. */ diff --git a/src/desktop/SolveSandboxRoot.ts b/src/desktop/SolveSandboxRoot.ts index 37c7bf798e..922b903571 100644 --- a/src/desktop/SolveSandboxRoot.ts +++ b/src/desktop/SolveSandboxRoot.ts @@ -11,10 +11,9 @@ import { spawnOwnedProviderProcess, } from "../process/ProviderProcess.js"; import { errorMessage } from "../kernel/errorMessage.js"; +import { AGENTENV_BRIDGE_SOURCES } from "./AgentEnvBridgeSources.js"; -const CLEANUP = fileURLToPath( - new URL("../../bridge/agentenv/solve_cleanup.py", import.meta.url), -); +const CLEANUP = fileURLToPath(AGENTENV_BRIDGE_SOURCES.solveCleanup); const resultSchema = z.discriminatedUnion("ok", [ z.object({ ok: z.literal(true) }), z.object({ ok: z.literal(false), error: z.string() }), diff --git a/tests/boundary/filesystem/ciChangeScope.test.ts b/tests/boundary/filesystem/ciChangeScope.test.ts index 29b0d57d5a..31a960876c 100644 --- a/tests/boundary/filesystem/ciChangeScope.test.ts +++ b/tests/boundary/filesystem/ciChangeScope.test.ts @@ -1478,6 +1478,10 @@ it.each([ "src/application/DshCordisYaml.ts", { code: true, package: true, runtime: true, apple: false }, ], + [ + "src/desktop/AgentEnvBridgeSources.ts", + { code: true, package: true, runtime: true, apple: false }, + ], [ "scripts/verify-packed-bridge.mjs", { diff --git a/tests/boundary/filesystem/ghidraBridgeSources.test.ts b/tests/boundary/filesystem/ghidraBridgeSources.test.ts index 0c7a15a100..2a9e21b207 100644 --- a/tests/boundary/filesystem/ghidraBridgeSources.test.ts +++ b/tests/boundary/filesystem/ghidraBridgeSources.test.ts @@ -211,39 +211,6 @@ it.skipIf(process.platform === "win32").each([ }, ); -it("rejects an omitted auxiliary source at the actual package verification boundary", async () => { - const source = GHIDRA_BRIDGE_SOURCES.prepareCom; - const packedFiles = [ - "package/bridge/hopper_bridge.py", - "package/bridge/cutter/rea_bridge.py", - ...Object.values(GHIDRA_BRIDGE_SOURCES) - .filter((name) => name !== source) - .map((name) => `package/bridge/ghidra/${name}`), - ]; - await expect( - execute(process.execPath, [ - "--input-type=module", - "-e", - `import { verifyPackedBridge } from "./scripts/verify-packed-bridge.mjs"; - import { createPackageVerifierCommands } from "./scripts/lib/verify-package-commands.mjs"; - import { withVerifierCleanup } from "./scripts/lib/verifier-cleanup.mjs"; - import { PrivateRuntimeRoot } from "./dist/process/PrivateRuntimeRoot.js"; - const root = await PrivateRuntimeRoot.create({ prefix: "rea-bridge-admission-" }); - let commands; - await withVerifierCleanup(async () => { - commands = await createPackageVerifierCommands({ workspace: root }); - await verifyPackedBridge({ tarball: "unused-because-admission-must-fail", packedFiles: JSON.parse(process.argv[1]), commands }); - }, [() => commands === undefined ? root.close() : commands.close()]);`, - JSON.stringify(packedFiles), - ]), - ).rejects.toMatchObject({ - code: 1, - stderr: expect.stringContaining( - `package omitted package/bridge/ghidra/${source}`, - ), - }); -}); - it("binds the workflow SDK pin and verified compile mode to the canonical release", async () => { const { jobs } = z .object({ diff --git a/tests/boundary/process/packageTarListing.test.ts b/tests/boundary/process/packageTarListing.test.ts index df65e5016e..685ab7c625 100644 --- a/tests/boundary/process/packageTarListing.test.ts +++ b/tests/boundary/process/packageTarListing.test.ts @@ -18,6 +18,10 @@ const bridge = "package/bridge/hopper_bridge.py"; const members = [ bridge, "package/bridge/cutter/rea_bridge.py", + "package/bridge/agentenv/capture.py", + "package/bridge/agentenv/computer.py", + "package/bridge/agentenv/solve.py", + "package/bridge/agentenv/solve_cleanup.py", ...Object.values(GHIDRA_BRIDGE_SOURCES).map( (name) => `package/bridge/ghidra/${name}`, ), diff --git a/tests/boundary/process/packedBridgeVerification.test.ts b/tests/boundary/process/packedBridgeVerification.test.ts index 9bf1029bf5..420b7a8e0f 100644 --- a/tests/boundary/process/packedBridgeVerification.test.ts +++ b/tests/boundary/process/packedBridgeVerification.test.ts @@ -32,6 +32,10 @@ const exec = promisify(execFile); const packedFiles = [ "package/bridge/hopper_bridge.py", "package/bridge/cutter/rea_bridge.py", + "package/bridge/agentenv/capture.py", + "package/bridge/agentenv/computer.py", + "package/bridge/agentenv/solve.py", + "package/bridge/agentenv/solve_cleanup.py", ...Object.values(GHIDRA_BRIDGE_SOURCES).map( (name) => `package/bridge/ghidra/${name}`, ), @@ -167,19 +171,18 @@ try { }, ); -it.each(packedFiles)( - "rejects an unadvertised required member: %s", - async (member) => { - const input = await fixture(); +it("rejects each omitted required bridge before parsing the archive", async () => { + const input = await fixture(); + for (const member of packedFiles) await expect( verifyPackedBridge({ ...input, + tarball: join(input.workspace, "missing.tgz"), packedFiles: packedFiles.filter((path) => path !== member), }), ).rejects.toThrow(`package omitted ${member}`); - expect(await readdir(input.workspace)).toEqual([]); - }, -); + expect(await readdir(input.workspace)).toEqual([]); +}); it("rejects a missing actual Python member even when the listing advertises it", async () => { const input = await fixture(validSource, true);