The open-source app everyone uses to manage agents at work
TypeScript acpxagent-orchestrationagent-skillsagentic-aiai-agentsai-engineeringai-governanceai-toolsautonomous-agentsclaudeclaude-codeclaude-skillscodexhermesllmmcpmulti-agentopenclawproject-managementworkflow-automation
import { createHash } from "node:crypto";
import { once } from "node:events";
import { constants } from "node:fs";
import { execFileSync } from "node:child_process";
import { chmod, copyFile, link, mkdir, mkdtemp, open, readFile, readdir, realpath, rename, rm, stat, symlink, writeFile, type FileHandle } from "node:fs/promises";
import { createServer, type Server } from "node:net";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import type { ChildProcess } from "node:child_process";
import { afterEach, describe, expect, it, vi } from "vitest";
import { awaitVerifiedAcpxProviderExit, awaitVerifiedAcpxProviderOwnership, verifyNativeAcpxInstallation } from "./installation-integrity.js";
import { createNativeAcpxDistributionSnapshot, readNativeAcpxDistributionEntries, parseNativeAcpxDistributionEntries, type NativeAcpxDistributionInput, type NativeAcpxDistributionEntry } from "./native-distribution-integrity.js";
import { openCodexAcpxRuntime } from "./codex-runtime-adapter.js";
import { createAcpxCommandLeaseOwner } from "./command-lease-owner.js";
import { resolveQualifiedAcpxProfile } from "./qualified-profiles.js";
vi.mock("node:fs/promises", async (importOriginal) => {
const original = await importOriginal<typeof import("node:fs/promises")>();
return { ...original, open: vi.fn(original.open), rm: vi.fn(original.rm), mkdir: vi.fn(original.mkdir), chmod: vi.fn(original.chmod) };
});
const roots: string[] = [];
const hash = (value: string | Buffer) => createHash("sha256").update(value).digest("hex");
afterEach(async () => { vi.restoreAllMocks(); await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true }))); });
async function fixture(options: { node?: boolean; script?: string } = {}): Promise<NativeAcpxDistributionInput> {
const root = await mkdtemp(join(tmpdir(), "native-acpx-test-")); roots.push(root);
const entries: NativeAcpxDistributionEntry[] = [];
if (options.node) {
await copyFile(process.execPath, join(root, "runtime")); await chmod(join(root, "runtime"), 0o700);
const bytes = await readFile(join(root, "runtime")); entries.push({ path: "runtime", sha256: hash(bytes), size: bytes.length, executable: true });
// Homebrew's test-host Node uses an @rpath libnode dylib. Include that
// dependency in this fixture's closure instead of relying on its old path.
const libraryRoot = join(dirname(process.execPath), "..", "lib");
for (const name of (await readdir(libraryRoot).catch(() => [] as string[])).filter(name => /^libnode\..*\.dylib$/.test(name))) {
await copyFile(join(libraryRoot, name), join(root, name)); await chmod(join(root, name), 0o600);
const library = await readFile(join(root, name)); entries.push({ path: name, sha256: hash(library), size: library.length, executable: false });
}
const script = options.script ?? 'console.log("qualified-entry");';
await writeFile(join(root, "entry.cjs"), script, { mode: 0o600 });
entries.push({ path: "entry.cjs", sha256: hash(script), size: Buffer.byteLength(script), executable: false });
} else {
const script = options.script ?? '#!/bin/sh\nprintf "native:%s:%s" "$1" "${COPILOT_PKG_CACHE_HOME:-none}"\n';
await writeFile(join(root, "runtime"), script, { mode: 0o700 });
entries.push({ path: "runtime", sha256: hash(script), size: Buffer.byteLength(script), executable: true });
}
entries.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
const manifestPath = join(root, "closure.json");
await writeFile(manifestPath, JSON.stringify({ entries }));
return { distributionRoot: root, manifestPath, expectedClosureSha256: hash(JSON.stringify(entries)), executable: "runtime", ...(options.node ? { entrypoint: "entry.cjs" } : {}), fixedArguments: options.node ? [] : ["fixed"] };
}
async function manyFileFixture(sizes: number[]) {
const declaration = await fixture();
const entries = await readNativeAcpxDistributionEntries(declaration);
for (let index = 0; index < sizes.length; index++) {
const path = `file-${String(index).padStart(2, "0")}`;
const bytes = Buffer.alloc(sizes[index]!, index + 1);
await writeFile(join(declaration.distributionRoot, path), bytes, { mode: 0o600 });
entries.push({ path, sha256: hash(bytes), size: bytes.length, executable: false });
}
entries.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
await writeFile(declaration.manifestPath, JSON.stringify({ entries }));
return { declaration: { ...declaration, expectedClosureSha256: hash(JSON.stringify(entries)) }, entries };
}
async function directoryFixture() {
const declaration = await fixture();
const entries = await readNativeAcpxDistributionEntries(declaration);
for (let index = 0; index < 40; index++) {
const parent = `dir-${String(index).padStart(2, "0")}/nested`;
await mkdir(join(declaration.distributionRoot, parent), { recursive: true });
for (const name of ["a", "b"]) {
const path = `${parent}/${name}`; const bytes = Buffer.from(path);
await writeFile(join(declaration.distributionRoot, path), bytes, { mode: 0o600 });
entries.push({ path, sha256: hash(bytes), size: bytes.length, executable: false });
}
}
entries.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
await writeFile(declaration.manifestPath, JSON.stringify({ entries }));
return { declaration: { ...declaration, expectedClosureSha256: hash(JSON.stringify(entries)) }, entries };
}
async function filePrototype(path: string): Promise<FileHandle> {
const probe = await open(path, "r"); const prototype = Object.getPrototypeOf(probe) as FileHandle;
await probe.close(); return prototype;
}
function gate() {
let release!: () => void;
const promise = new Promise<void>((resolve) => { release = resolve; });
return { promise, release };
}
async function output(child: ChildProcess): Promise<{ text: string; error: string; code: number | null }> {
let text = ""; let error = "";
child.stdout!.on("data", value => { text += String(value); });
child.stderr!.on("data", value => { error += String(value); });
const [code] = await once(child, "close") as [number | null];
return { text, error, code };
}
describe("native ACPX execution closure", () => {
it("reuses shared parent prefixes with exactly the original levels, insertion order and sorted batches", async () => {
const declaration = await fixture();
const entries = await readNativeAcpxDistributionEntries(declaration);
const deep = Array.from({ length: 36 }, (_, index) => `d${index}`).join("/");
const paths = ["plain", "a-/one", "a.b/two", "a/a", "a/b/a", "a/b/c/a", "a/b/c/b", "a/bb/a", "a/c/a", "a0/a", "space dir/@scope/pkg/a", "space dir/@scope/pkg/b", "日本/é/a", `${deep}/a`, `${deep}/b`, `${deep}/more/c`];
for (const path of paths) {
await mkdir(join(declaration.distributionRoot, dirname(path)), { recursive: true });
await writeFile(join(declaration.distributionRoot, path), path, { mode: 0o600 });
entries.push({ path, sha256: hash(path), size: Buffer.byteLength(path), executable: false });
}
entries.sort((a, b) => a.path < b.path ? -1 : 1);
const creatingStart = vi.mocked(mkdir).mock.calls.length;
const sealingStart = vi.mocked(chmod).mock.calls.length;
const created = await createNativeAcpxDistributionSnapshot({ ...declaration, expectedClosureSha256: hash(JSON.stringify(entries)) }, entries);
try {
const root = created.snapshot.roots[0]!;
const levels = new Map<number, Set<string>>();
const directories = new Set([dirname(root), root]);
// Frozen reference: the original repeated-prefix planner.
for (const entry of entries) {
const parts = entry.path.split("/");
for (let depth = 1; depth < parts.length; depth++) {
const path = join(root, ...parts.slice(0, depth));
const level = levels.get(depth) ?? new Set<string>();
level.add(path); levels.set(depth, level); directories.add(path);
}
}
const expectedCreation = [root, ...[...levels.keys()].sort((a, b) => a - b).flatMap(depth => [...levels.get(depth)!].sort())];
expect(vi.mocked(mkdir).mock.calls.slice(creatingStart)).toEqual(expectedCreation.map(path => [path, { mode: 0o700 }]));
expect(vi.mocked(chmod).mock.calls.slice(sealingStart)).toEqual([...directories].map(path => [path, 0o500]));
for (const path of directories) expect((await stat(path)).mode & 0o777).toBe(0o500);
} finally { await created.commandDirectory.close(); await created.snapshot.close(); }
});
it("rejects paths, ordering, oversized files and altered manifest pins", () => {
const entry = { path: "runtime", sha256: "a".repeat(64), size: 10, executable: true };
for (const entries of [[{ ...entry, path: "../escape" }], [{ ...entry, path: "/absolute" }], [entry, entry], [{ ...entry, size: 2 ** 40 }], [{ ...entry, unknown: 1 }]]) {
expect(() => parseNativeAcpxDistributionEntries({ entries }, hash(JSON.stringify(entries)))).toThrow();
}
expect(() => parseNativeAcpxDistributionEntries({ entries: [entry] }, "b".repeat(64))).toThrow("manifest digest mismatch");
});
it("checks every file digest and refuses links at admission", async () => {
const declaration = await fixture(); const installation = await verifyNativeAcpxInstallation(declaration);
await writeFile(join(declaration.distributionRoot, "runtime"), "altered");
await expect(installation.openCommand()).rejects.toThrow();
await rm(join(declaration.distributionRoot, "runtime"));
await symlink("closure.json", join(declaration.distributionRoot, "runtime"));
await expect(installation.openCommand()).rejects.toThrow("symbolic link");
});
it.each(["fifo", "directory", "hardlink", "symlink", "size", "mode"] as const)(
"rejects a %s swapped in immediately before open without reading it or leaking its descriptor", async kind => {
const declaration = await fixture();
const entries = await readNativeAcpxDistributionEntries(declaration);
const original = await vi.importActual<typeof import("node:fs/promises")>("node:fs/promises");
const source = await realpath(declaration.distributionRoot);
const path = join(source, "runtime");
const originalPath = join(source, "original");
const prototype = await filePrototype(path);
const read = vi.spyOn(prototype, "read");
let opened: FileHandle | undefined; let closed = false;
vi.mocked(open).mockImplementation(async (selected: any, flags: any, ...rest: any[]): Promise<any> => {
if (String(selected) !== path) return original.open(selected, flags, rest[0]);
expect(flags & constants.O_NOFOLLOW).not.toBe(0);
expect(flags & constants.O_NONBLOCK).not.toBe(0);
await rename(path, originalPath);
if (kind === "fifo") execFileSync("mkfifo", [path], { timeout: 2_000 });
else if (kind === "directory") await mkdir(path);
else if (kind === "hardlink") await link(originalPath, path);
else if (kind === "symlink") await symlink(originalPath, path);
else {
await copyFile(originalPath, path);
if (kind === "size") await writeFile(path, "short");
else await chmod(path, 0o600);
}
opened = await original.open(selected, flags, rest[0]);
const close = opened.close.bind(opened);
opened.close = async () => { await close(); closed = true; };
return opened;
});
let unexpected: Awaited<ReturnType<typeof createNativeAcpxDistributionSnapshot>> | undefined;
try {
const error = await createNativeAcpxDistributionSnapshot(declaration, entries).then(
value => { unexpected = value; return undefined; }, error => error);
expect(error).toBeInstanceOf(Error);
expect(read).not.toHaveBeenCalled();
if (kind === "symlink") expect(opened).toBeUndefined();
else { expect(opened).toBeDefined(); expect(closed).toBe(true); }
} finally {
vi.mocked(open).mockImplementation(original.open);
if (unexpected) { await unexpected.commandDirectory.close(); await unexpected.snapshot.close(); }
}
});
it("rejects a same-byte pathname replacement after reading the held descriptor", async () => {
const declaration = await fixture(); const entries = await readNativeAcpxDistributionEntries(declaration);
const path = join(declaration.distributionRoot, "runtime");
const prototype = await filePrototype(path); const originalRead = prototype.read;
let replaced = false;
vi.spyOn(prototype, "read").mockImplementation(async function (this: FileHandle, ...args: any[]): Promise<any> {
const result = await originalRead.apply(this, args as never);
if (!replaced) {
replaced = true;
await rename(path, path + ".original");
await copyFile(path + ".original", path);
}
return result;
});
await expect(createNativeAcpxDistributionSnapshot(declaration, entries)).rejects.toThrow("changed while read");
expect(replaced).toBe(true);
});
it("launches only fixed arguments from a frozen snapshot after installed files change", async () => {
const declaration = await fixture(); const lease = await (await verifyNativeAcpxInstallation(declaration)).openCommand();
expect(() => lease.spawn(["--untrusted-override"])).toThrow("fixed profile arguments");
await writeFile(join(declaration.distributionRoot, "runtime"), "changed after lease");
const result = await output(lease.spawn());
expect(result).toEqual({ text: "native:fixed:none", error: "", code: 0 });
expect(() => lease.spawn()).toThrow("closed");
await lease.close();
});
it("waits for consumed native snapshot deletion before command retirement completes", async () => {
const declaration = await fixture();
const lease = await (await verifyNativeAcpxInstallation(declaration)).openCommand();
const deleting = gate(), releaseDeletion = gate();
const originalRm = vi.mocked(rm).getMockImplementation()!;
let snapshotRoot = "";
vi.mocked(rm).mockImplementation(async (path, options) => {
if (String(path).includes("paperclip-acpx-native-")) {
snapshotRoot = String(path);
deleting.release();
await releaseDeletion.promise;
}
return originalRm(path, options);
});
let retired = false;
let closing: Promise<void> | undefined;
try {
expect((await output(lease.spawn())).code).toBe(0);
await deleting.promise;
closing = lease.close().then(() => { retired = true; });
await new Promise<void>(resolve => setImmediate(resolve));
expect(retired).toBe(false);
} finally {
releaseDeletion.release();
await closing;
}
await expect(stat(snapshotRoot)).rejects.toMatchObject({ code: "ENOENT" });
});
it("retains spawned native bytes until the exact child exits", async () => {
const declaration = await fixture({ script: '#!/bin/sh\nprintf ready\nexec sleep 1000\n' });
const creatingStart = vi.mocked(mkdir).mock.calls.length;
const lease = await (await verifyNativeAcpxInstallation(declaration)).openCommand();
const snapshotRoot = dirname(String(vi.mocked(mkdir).mock.calls[creatingStart]![0]));
const child = lease.spawn();
const exited = once(child, "close");
child.stderr!.resume();
let retired = false;
let closing: Promise<void> | undefined;
try {
await once(child.stdout!, "data");
closing = lease.close().then(() => { retired = true; });
await new Promise<void>(resolve => setImmediate(resolve));
expect(retired).toBe(false);
expect((await stat(snapshotRoot)).isDirectory()).toBe(true);
} finally {
child.kill("SIGTERM");
await exited;
await closing;
}
await expect(stat(snapshotRoot)).rejects.toMatchObject({ code: "ENOENT" });
});
it("reports exit-triggered native deletion failure and retries only its retained cleanup", async () => {
const declaration = await fixture();
const lease = await (await verifyNativeAcpxInstallation(declaration)).openCommand();
const deleting = gate();
const originalRm = vi.mocked(rm).getMockImplementation()!;
let snapshotRoot = "", failed = false;
vi.mocked(rm).mockImplementation(async (path, options) => {
if (String(path).includes("paperclip-acpx-native-") && !failed) {
failed = true;
snapshotRoot = String(path);
deleting.release();
throw new Error("native snapshot deletion denied");
}
return originalRm(path, options);
});
expect((await output(lease.spawn())).code).toBe(0);
await deleting.promise;
await expect(lease.close()).rejects.toThrow("native snapshot deletion denied");
expect((await stat(snapshotRoot)).isDirectory()).toBe(true);
await lease.close();
await expect(stat(snapshotRoot)).rejects.toMatchObject({ code: "ENOENT" });
});
it("bounds retirement of a surviving native child and retains bytes for cleanup retry", async () => {
const declaration = await fixture({ script: '#!/bin/sh\nprintf ready\nexec sleep 1000\n' });
const creatingStart = vi.mocked(mkdir).mock.calls.length;
const lease = await (await verifyNativeAcpxInstallation(declaration)).openCommand();
const snapshotRoot = dirname(String(vi.mocked(mkdir).mock.calls[creatingStart]![0]));
const child = lease.spawn();
const exited = once(child, "close");
child.stderr!.resume();
let closing: Promise<void> | undefined;
try {
await once(child.stdout!, "data");
vi.useFakeTimers();
closing = lease.close();
const disposition = Promise.race([
closing.then(() => "closed", () => "failed"),
new Promise<string>(resolve => setTimeout(() => resolve("unbounded"), 11_000)),
]);
await vi.advanceTimersByTimeAsync(11_000);
expect(await disposition).toBe("failed");
await expect(closing).rejects.toThrow("native snapshot retirement deadline");
expect((await stat(snapshotRoot)).isDirectory()).toBe(true);
} finally {
vi.useRealTimers();
child.kill("SIGTERM");
await exited;
await closing?.catch(() => undefined);
await lease.close();
}
await expect(stat(snapshotRoot)).rejects.toMatchObject({ code: "ENOENT" });
});
it("gives packaged executables a fresh private extraction cache each launch", async () => {
const declaration = { ...await fixture(), isolatedCacheEnvironmentName: "COPILOT_PKG_CACHE_HOME" as const };
const install = await verifyNativeAcpxInstallation(declaration);
const first = await output((await install.openCommand()).spawn([], { env: { COPILOT_PKG_CACHE_HOME: "/ambient/cache" } }));
const second = await output((await install.openCommand()).spawn());
expect(first.text).toMatch(/^native:fixed:.*paperclip-acpx-native-.*\/state$/);
expect(first.text).not.toBe(second.text);
});
it("reports failed deletion and retries retirement without relaunching", async () => {
const lease = await (await verifyNativeAcpxInstallation(await fixture())).openCommand();
const originalRemove = vi.mocked(rm).getMockImplementation()!;
const deleting = gate(); const holdDeletion = gate();
let privateRoot: string | undefined;
let failed = false;
vi.mocked(rm).mockImplementation(async (path, options) => {
if (String(path).includes("paperclip-acpx-native-") && !failed) {
privateRoot = String(path); deleting.release(); await holdDeletion.promise;
failed = true; throw new Error("snapshot deletion failed");
}
return await originalRemove(path, options);
});
try {
expect((await output(lease.spawn())).code).toBe(0);
await deleting.promise;
const rejected = expect(lease.close()).rejects.toThrow("snapshot deletion failed");
await new Promise<void>(resolve => setImmediate(resolve));
holdDeletion.release();
await rejected;
await expect(stat(privateRoot!)).resolves.toBeDefined();
expect(() => lease.spawn()).toThrow("closed");
await lease.close();
await expect(stat(privateRoot!)).rejects.toMatchObject({ code: "ENOENT" });
} finally {
holdDeletion.release();
vi.mocked(rm).mockImplementation(originalRemove);
await lease.close();
}
});
// Copy/hash the real Node closure and start its bootstrap + owned shim. These
// operations share the adjacent native-closure test's bounded allowance;
// Vitest's 5s default is not a provider startup or permission deadline.
it("loads the owned Copilot distribution through a guarded private shim and ignores ambient overrides", async () => {
const declaration = await fixture({ node: true });
const entries = await readNativeAcpxDistributionEntries(declaration);
await mkdir(join(declaration.distributionRoot, "distribution"));
const source = 'console.log(JSON.stringify({owned:__dirname,dist:process.env.COPILOT_CLI_DIST_DIR,version:process.env.COPILOT_CLI_VERSION??null,options:process.env.NODE_OPTIONS??null}));';
await writeFile(join(declaration.distributionRoot, "distribution/index.js"), source, { mode: 0o600 });
entries.push({ path: "distribution/index.js", sha256: hash(source), size: Buffer.byteLength(source), executable: false });
entries.sort((a, b) => a.path < b.path ? -1 : 1);
await writeFile(declaration.manifestPath, JSON.stringify({ entries }));
const owned = { ...declaration, entrypoint: undefined, expectedClosureSha256: hash(JSON.stringify(entries)), copilotDistributionDirectory: "distribution", fixedArguments: ["-e", 'require(process.env.COPILOT_CLI_DIST_DIR+"/index.js")'] };
const installation = await verifyNativeAcpxInstallation(owned);
const lease = await installation.openCommand();
const result = await output(lease.spawn([], { env: { COPILOT_CLI_DIST_DIR: "/ambient/evil", COPILOT_CLI_VERSION: "99.0.0", NODE_OPTIONS: "--untrusted-option" } }));
expect(result.code, result.error).toBe(0);
const observed = JSON.parse(result.text);
expect(observed.owned).toMatch(/paperclip-acpx-native-.*\/distribution\/distribution$/);
expect(observed.dist).toBe(join(dirname(observed.owned), ".paperclip-copilot-entry"));
expect(observed.version).toBeNull(); expect(observed.options).toBeNull();
await lease.close();
await expect(readNativeAcpxDistributionEntries({ ...owned, copilotDistributionDirectory: "../ambient" })).rejects.toThrow("launch declaration");
await expect(readNativeAcpxDistributionEntries({ ...owned, copilotDistributionDirectory: "missing" })).rejects.toThrow("owned entrypoint");
await expect(readNativeAcpxDistributionEntries({ ...owned, entrypoint: "entry.cjs" })).rejects.toThrow("launch declaration");
}, 30_000);
it("loads a pinned Node entrypoint while rejecting unqualified external modules", async () => {
const declaration = await fixture({ node: true });
const result = await output((await (await verifyNativeAcpxInstallation(declaration)).openCommand()).spawn());
expect(result.code, result.error).toBe(0); expect(result.text).toBe("qualified-entry\n");
const outside = join(declaration.distributionRoot, "outside.cjs"); await writeFile(outside, "module.exports='ambient';");
const evil = await fixture({ node: true, script: `require(${JSON.stringify(outside)});` });
const denied = await output((await (await verifyNativeAcpxInstallation(evil)).openCommand()).spawn());
expect(denied.code).not.toBe(0); expect(denied.error).toContain("escaped its closed distribution");
}, 30_000);
it.each([false, true])("runs real Node module hooks with interleaved formats and tamper=%s", async tamper => {
const script = tamper ? `
const fs = require("node:fs");
const { registerHooks } = require("node:module");
const { fileURLToPath } = require("node:url");
registerHooks({ resolve(specifier, context, next) {
const result = next(specifier, context);
if (result.url.endsWith("/value.mjs")) {
const path = fileURLToPath(result.url);
fs.chmodSync(path, 0o600);
fs.writeFileSync(path, 'console.log("tampered-code-executed");export default 99;');
}
return result;
}});
import("./value.mjs").then(() => { process.exitCode = 9; })
.catch(error => { console.error(error.message); process.exitCode = 17; });
` : `
const resolved = require.resolve("./value.cjs");
require("node:fs");
const commonjs = require(resolved);
const json = require("./value.json");
import("./value.mjs").then(module => {
console.log(JSON.stringify([commonjs, json.value, module.default, require(resolved)]));
}).catch(error => { console.error(error); process.exitCode = 1; });
`;
const declaration = await fixture({ node: true, script });
const entries = await readNativeAcpxDistributionEntries(declaration);
for (const [path, source] of Object.entries({
"value.cjs": "module.exports = 17;",
"value.json": '{"value":31}',
"value.mjs": "export default 23;",
})) {
await writeFile(join(declaration.distributionRoot, path), source, { mode: 0o600 });
entries.push({ path, sha256: hash(source), size: Buffer.byteLength(source), executable: false });
}
entries.sort((a, b) => a.path < b.path ? -1 : 1);
await writeFile(declaration.manifestPath, JSON.stringify({ entries }));
const lease = await (await verifyNativeAcpxInstallation({ ...declaration, expectedClosureSha256: hash(JSON.stringify(entries)) })).openCommand();
try {
const result = await output(lease.spawn());
if (tamper) {
expect(result.code, result.error).toBe(17);
expect(result.error).toContain("digest mismatch");
expect(result.text).not.toContain("tampered-code-executed");
} else {
expect(result.code, result.error).toBe(0);
expect(result.text).toBe("[17,31,23,17]\n");
}
} finally { await lease.close(); }
}, 30_000);
it("creates each private parent once and seals every directory before returning", async () => {
const { declaration, entries } = await directoryFixture();
const creatingStart = vi.mocked(mkdir).mock.calls.length;
const created = await createNativeAcpxDistributionSnapshot(declaration, entries);
try {
const root = created.snapshot.roots[0]!;
const calls = vi.mocked(mkdir).mock.calls.slice(creatingStart).map(([path]) => String(path)).filter(path => path.startsWith(root));
expect(calls).toHaveLength(81); expect(new Set(calls).size).toBe(81);
for (const path of calls) expect((await stat(path)).mode & 0o777).toBe(0o500);
for (const entry of entries) {
const path = join(root, entry.path);
expect(hash(await readFile(path))).toBe(entry.sha256);
expect((await stat(path)).mode & 0o777).toBe(entry.executable ? 0o500 : 0o400);
}
} finally { await created.commandDirectory.close(); await created.snapshot.close(); }
});
it.each(["create", "seal"] as const)("bounds %s batches and drains failures before cleanup or further scheduling", async stage => {
const { declaration, entries } = await directoryFixture();
const original = await vi.importActual<typeof import("node:fs/promises")>("node:fs/promises");
const hold = gate(); let started = 0; let active = 0; let peak = 0; let settled = false;
const removalStart = vi.mocked(rm).mock.calls.length;
const operation = async (path: any, mode: any) => {
const selected = stage === "create"
? /paperclip-acpx-native-.*\/distribution\/dir-\d+$/.test(String(path))
: mode === 0o500;
if (!selected) return stage === "create" ? original.mkdir(path, mode) : original.chmod(path, mode);
const index = started++; active++; peak = Math.max(peak, active);
try {
if (index === 0) throw new Error(`fixture ${stage} failure`);
await hold.promise;
return stage === "create" ? await original.mkdir(path, mode) : await original.chmod(path, mode);
} finally { active--; }
};
if (stage === "create") vi.mocked(mkdir).mockImplementation(operation as typeof mkdir);
else vi.mocked(chmod).mockImplementation(operation as typeof chmod);
const creating = createNativeAcpxDistributionSnapshot(declaration, entries);
void creating.then(() => { settled = true; }, () => { settled = true; });
try {
await vi.waitFor(() => expect(started).toBe(32));
expect(peak).toBeGreaterThan(1); expect(peak).toBeLessThanOrEqual(32); expect(settled).toBe(false);
expect(vi.mocked(rm).mock.calls.slice(removalStart).filter(([path]) => String(path).includes("paperclip-acpx-native-"))).toHaveLength(0);
} finally { hold.release(); }
try {
await expect(creating).rejects.toThrow(`fixture ${stage} failure`);
expect(active).toBe(0); expect(started).toBe(32);
const removed = vi.mocked(rm).mock.calls.slice(removalStart).map(([path]) => String(path)).filter(path => path.includes("paperclip-acpx-native-"));
expect(removed).toHaveLength(1);
await expect(stat(removed[0]!)).rejects.toMatchObject({ code: "ENOENT" });
} finally { vi.mocked(mkdir).mockImplementation(original.mkdir); vi.mocked(chmod).mockImplementation(original.chmod); }
});
it("copies concurrently within its descriptor bound and retains canonical manifest order", async () => {
const { declaration, entries } = await manyFileFixture(Array.from({ length: 40 }, (_, index) => 100 + index));
const prototype = await filePrototype(join(declaration.distributionRoot, "runtime"));
const originalRead = prototype.read;
const hold = gate(); const sizes: number[] = []; let active = 0; let peak = 0;
vi.spyOn(prototype, "read").mockImplementation(async function (this: FileHandle, ...args: any[]): Promise<any> {
sizes.push(args[0].length); active++; peak = Math.max(peak, active);
try { await hold.promise; return await originalRead.apply(this, args as never); }
finally { active--; }
});
const creating = createNativeAcpxDistributionSnapshot(declaration, entries);
try {
await vi.waitFor(() => expect(sizes).toHaveLength(32));
expect(sizes.toSorted()).toEqual(Array.from({ length: 32 }, (_, index) => 100 + index));
} finally { hold.release(); }
const created = await creating;
try {
expect(peak).toBe(32);
expect(Object.keys(created.snapshot.digests).slice(0, entries.length)).toEqual(entries.map(entry => join(created.snapshot.roots[0]!, entry.path)));
for (const entry of entries) expect(hash(await readFile(join(created.snapshot.roots[0]!, entry.path)))).toBe(entry.sha256);
} finally { await created.commandDirectory.close(); await created.snapshot.close(); }
});
it("uses released capacity while an earlier copy is still pending", async () => {
const { declaration, entries } = await manyFileFixture(Array.from({ length: 40 }, (_, index) => 100 + index));
const prototype = await filePrototype(join(declaration.distributionRoot, "runtime"));
const originalRead = prototype.read; const hold = gate(); let laterReached = false;
let active = 0; let peak = 0;
vi.spyOn(prototype, "read").mockImplementation(async function (this: FileHandle, ...args: any[]): Promise<any> {
active++; peak = Math.max(peak, active);
try {
if (args[0].length === 100) await hold.promise;
if (args[0].length === 139) laterReached = true;
return await originalRead.apply(this, args as never);
} finally { active--; }
});
const creating = createNativeAcpxDistributionSnapshot(declaration, entries);
let created: Awaited<typeof creating> | undefined;
try {
try { await vi.waitFor(() => expect(laterReached).toBe(true)); expect(active).toBeGreaterThan(0); expect(peak).toBeLessThanOrEqual(32); }
finally { hold.release(); created = await creating; }
expect(Object.keys(created.snapshot.digests).slice(0, entries.length)).toEqual(entries.map(entry => join(created!.snapshot.roots[0]!, entry.path)));
} finally { if (created) { await created.commandDirectory.close(); await created.snapshot.close(); } }
});
it("bounds simultaneous buffers and gives an oversized admitted file exclusive capacity", async () => {
const mib = 1024 * 1024;
const { declaration, entries } = await manyFileFixture([17 * mib, 17 * mib, 33 * mib]);
const prototype = await filePrototype(join(declaration.distributionRoot, "runtime"));
const originalRead = prototype.read;
const retained = new Map<FileHandle, number>(); const observed: number[] = [];
vi.spyOn(prototype, "read").mockImplementation(async function (this: FileHandle, ...args: any[]): Promise<any> {
if (!retained.has(this)) {
const file = this; const originalClose = file.close.bind(file);
file.close = async () => { try { await originalClose(); } finally { retained.delete(file); } };
}
retained.set(this, args[0].length);
const total = [...retained.values()].reduce((sum, size) => sum + size, 0); observed.push(total);
expect(total).toBeLessThanOrEqual(Math.max(32 * mib, args[0].length));
if (args[0].length > 32 * mib) expect(retained.size).toBe(1);
return originalRead.apply(this, args as never);
});
const created = await createNativeAcpxDistributionSnapshot(declaration, entries);
try { expect(Math.max(...observed)).toBe(33 * mib); }
finally { await created.commandDirectory.close(); await created.snapshot.close(); }
});
it("drains every admitted copy before failed-snapshot cleanup and schedules no later batch", async () => {
const { declaration, entries } = await manyFileFixture(Array.from({ length: 40 }, (_, index) => 91 + index));
await writeFile(join(declaration.distributionRoot, "file-00"), Buffer.alloc(91, 99));
const prototype = await filePrototype(join(declaration.distributionRoot, "runtime"));
const originalRead = prototype.read;
const hold = gate(); const entered = gate(); const invalidClosed = gate();
const readSizes: number[] = []; const removalStart = vi.mocked(rm).mock.calls.length;
vi.spyOn(prototype, "read").mockImplementation(async function (this: FileHandle, ...args: any[]): Promise<any> {
const size = args[0].length; readSizes.push(size);
if (size === 91) {
const originalClose = this.close.bind(this);
this.close = async () => { await originalClose(); invalidClosed.release(); };
await entered.promise;
}
if (size !== 91) { entered.release(); await hold.promise; }
return originalRead.apply(this, args as never);
});
const creating = createNativeAcpxDistributionSnapshot(declaration, entries);
let settled = false;
void creating.then(() => { settled = true; }, () => { settled = true; });
try {
await entered.promise; await invalidClosed.promise;
expect(settled).toBe(false);
} finally { hold.release(); }
await expect(creating).rejects.toThrow("digest mismatch: file-00");
expect(readSizes).not.toContain(123);
const removals = vi.mocked(rm).mock.calls.slice(removalStart).map(([path]) => String(path)).filter(path => /paperclip-acpx-native-/.test(path));
expect(removals).toHaveLength(1);
await new Promise<void>(resolve => setImmediate(resolve));
await expect(stat(removals[0]!)).rejects.toMatchObject({ code: "ENOENT" });
});
it("aborts a pending native copy, drains admitted reads and removes its partial snapshot", async () => {
const { declaration, entries } = await manyFileFixture(Array.from({ length: 40 }, (_, index) => 91 + index));
const prototype = await filePrototype(join(declaration.distributionRoot, "runtime"));
const originalRead = prototype.read;
const entered = gate(); const hold = gate(); const controller = new AbortController();
const readSizes: number[] = []; const removalStart = vi.mocked(rm).mock.calls.length;
vi.spyOn(prototype, "read").mockImplementation(async function (this: FileHandle, ...args: any[]): Promise<any> {
readSizes.push(args[0].length); entered.release(); await hold.promise;
return originalRead.apply(this, args as never);
});
const creating = createNativeAcpxDistributionSnapshot(declaration, entries, controller.signal);
let settled = false; let unexpected: Awaited<typeof creating> | undefined;
void creating.then(value => { settled = true; unexpected = value; }, () => { settled = true; });
try {
await entered.promise;
controller.abort(new Error("owned command refresh cancelled"));
await new Promise<void>(resolve => setImmediate(resolve));
expect(settled).toBe(false);
expect(vi.mocked(rm).mock.calls).toHaveLength(removalStart);
hold.release();
await expect(creating).rejects.toThrow("owned command refresh cancelled");
expect(readSizes).not.toContain(123);
const removals = vi.mocked(rm).mock.calls.slice(removalStart).map(([path]) => String(path)).filter(path => /paperclip-acpx-native-/.test(path));
expect(removals).toHaveLength(1);
await expect(stat(removals[0]!)).rejects.toMatchObject({ code: "ENOENT" });
} finally {
hold.release(); await creating.catch(() => undefined);
if (unexpected) { await unexpected.commandDirectory.close(); await unexpected.snapshot.close(); }
}
});
it("rejects a source mutation while another file is being copied", async () => {
const { declaration, entries } = await manyFileFixture([101, 102]);
const prototype = await filePrototype(join(declaration.distributionRoot, "runtime"));
const originalRead = prototype.read; const entered = gate(); const hold = gate();
vi.spyOn(prototype, "read").mockImplementation(async function (this: FileHandle, ...args: any[]): Promise<any> {
if (args[0].length === 101) { entered.release(); await hold.promise; }
return originalRead.apply(this, args as never);
});
const creating = createNativeAcpxDistributionSnapshot(declaration, entries);
const rejected = expect(creating).rejects.toThrow("changed while read");
try {
await entered.promise;
await writeFile(join(declaration.distributionRoot, "file-00"), Buffer.alloc(101, 7));
} finally { hold.release(); }
await rejected;
});
it("uses the existing guardian ownership and provider-exit proof for native children", async () => {
const declaration = await fixture({ script: '#!/bin/sh\nprintf "ready"\nwhile :; do sleep 1; done\n' });
const fences = await Promise.all([listen(), listen()]);
const fds = fences.map(server => (server as Server & { _handle?: { fd?: number } })._handle!.fd!);
const owners: number[] = [];
const child = (await (await verifyNativeAcpxInstallation(declaration)).openCommand()).spawn([], {}, {
credentialFenceFds: [fds[0]!, fds[1]!], activateCredentialFenceOwner: async pid => { owners.push(pid); },
});
let stderr = ""; child.stderr!.on("data", value => { stderr += String(value); });
try {
await awaitVerifiedAcpxProviderOwnership(child);
expect(owners).toEqual([child.pid]);
const [chunk] = await once(child.stdout!, "data"); expect(String(chunk), stderr).toBe("ready");
const exited = once(child, "exit"); child.kill(); await exited;
await awaitVerifiedAcpxProviderExit(child);
} finally {
child.kill(); await Promise.all(fences.map(server => new Promise<void>((resolve, reject) => server.close(error => error ? reject(error) : resolve()))));
}
}, 15_000);
it("proves native child death before any caller cleanup, despite the launcher's pending guardian read", async () => {
const declaration = await fixture({ node: true, script: 'console.log(process.pid);setInterval(()=>{},1000);' });
const fences = await Promise.all([listen(), listen()]);
const fds = fences.map(server => (server as Server & { _handle?: { fd?: number } })._handle!.fd!);
const child = (await (await verifyNativeAcpxInstallation(declaration)).openCommand()).spawn([], {}, {
credentialFenceFds: [fds[0]!, fds[1]!], activateCredentialFenceOwner: async () => undefined,
});
const exited = once(child, "exit");
let deadline: ReturnType<typeof setTimeout> | undefined;
try {
const ready = once(child.stdout!, "data");
await awaitVerifiedAcpxProviderOwnership(child);
const [chunk] = await ready;
const nativePid = Number(String(chunk).trim());
expect(Number.isSafeInteger(nativePid) && nativePid > 0 && nativePid !== child.pid).toBe(true);
const proof = awaitVerifiedAcpxProviderExit(child);
process.kill(nativePid, "SIGKILL");
// No guardian.kill/close is allowed to manufacture the lifetime proof.
await Promise.race([proof, new Promise((_, reject) => {
deadline = setTimeout(() => reject(new Error("Native exit proof remained open after child death")), 3_000);
})]);
await exited;
expect(() => process.kill(nativePid, 0)).toThrow();
} finally {
clearTimeout(deadline);
if (child.exitCode === null && child.signalCode === null) child.kill("SIGKILL");
await exited;
await Promise.all(fences.map(server => new Promise<void>((resolve, reject) => server.close(error => error ? reject(error) : resolve()))));
}
}, 15_000);
it("expires a real ACPX permission on native child death without replaying its mutation", async () => {
// This is a deterministic ACP peer in a verified native distribution. No
// provider credential, remote service or model is involved.
const declaration = await fixture({ node: true, script: blockingCopilotPeer });
const fences = await Promise.all([listen(), listen()]);
const fds = fences.map(server => (server as Server & { _handle?: { fd?: number } })._handle!.fd!);
const installation = await verifyNativeAcpxInstallation(declaration);
const owner = createAcpxCommandLeaseOwner(await installation.openCommand(), installation.openCommand);
const pidFile = join(declaration.distributionRoot, "native-pid");
let port: Awaited<ReturnType<typeof openCodexAcpxRuntime>> | undefined;
let deadline: ReturnType<typeof setTimeout> | undefined;
try {
port = await openCodexAcpxRuntime({
command: owner.command, refreshConsumedCommand: owner.refreshConsumedCommand,
profile: resolveQualifiedAcpxProfile("copilot", "gpt-5.6-luna"),
cwd: declaration.distributionRoot, stateDirectory: join(declaration.distributionRoot, "state"),
providerSessionKey: "native-death-fixture", permissionMode: "approve-reads",
permissionPolicy: { autoApprove: ["read"], escalate: ["write"], defaultAction: "escalate" },
launchEnvironment: { PATH: "/usr/bin:/bin", NATIVE_TEST_PID_FILE: pidFile },
credentialFenceFds: [fds[0]!, fds[1]!], activateCredentialFenceOwner: async () => undefined,
systemInstructions: "Read task context before editing.", mcpServers: [], retainFailedAdmissionCleanup: () => undefined,
});
let pending!: () => void;
const requested = new Promise<void>(resolve => { pending = resolve; });
let callbackSignal!: AbortSignal;
let lateAnswer!: (value: { outcome: "allow_once" }) => void;
const handler = vi.fn((_request, context) => {
callbackSignal = context.signal; pending();
return new Promise<{ outcome: "allow_once" }>(resolve => { lateAnswer = resolve; });
});
const turn = port.startTurn({ text: "Attempt one edit", requestId: "native-death-turn", onPermissionRequest: handler });
const settled = expect(turn.result).rejects.toMatchObject({ code: "ACPX_PROVIDER_PROCESS_LOST" });
const draining = expect((async () => { for await (const _event of turn.events) { /* drain */ } })())
.rejects.toMatchObject({ code: "ACPX_PROVIDER_PROCESS_LOST" });
const bounded = new Promise<never>((_, reject) => {
deadline = setTimeout(() => reject(new Error("Real native ACPX death remained unsettled")), 5_000);
});
await Promise.race([requested, bounded]);
const nativePid = Number(await readFile(pidFile, "utf8"));
expect(Number.isSafeInteger(nativePid) && nativePid > 0).toBe(true);
process.kill(nativePid, "SIGKILL");
await Promise.race([Promise.all([settled, draining]), bounded]);
expect(callbackSignal.aborted).toBe(true);
lateAnswer({ outcome: "allow_once" });
expect(() => port!.startTurn({ text: "Replay", requestId: "replay" })).toThrow("provider exited");
expect(handler).toHaveBeenCalledOnce();
expect(() => process.kill(nativePid, 0)).toThrow();
} finally {
clearTimeout(deadline);
await port?.close({ reason: "offline native-death fixture complete" });
await owner.command.close();
await Promise.all(fences.map(server => new Promise<void>((resolve, reject) => server.close(error => error ? reject(error) : resolve()))));
}
}, 15_000);
});
const blockingCopilotPeer = String.raw`
const send=m=>process.stdout.write(JSON.stringify({jsonrpc:'2.0',...m})+'\n');
require('node:fs').writeFileSync(process.env.NATIVE_TEST_PID_FILE,String(process.pid));
const mode='https://agentclientprotocol.com/protocol/session-modes#agent';
const config=[{id:'mode',name:'Mode',type:'select',currentValue:mode,options:[{value:mode,name:'Agent'}]},
{id:'allow_all',name:'Allow all',type:'select',currentValue:'off',options:[{value:'off',name:'Off'}]},
{id:'model',name:'Model',type:'select',currentValue:'gpt-5.6-luna',options:[{value:'gpt-5.6-luna',name:'Luna'}]}];
const snapshot={sessionId:'copilot-death-session',modes:{currentModeId:mode,availableModes:[{id:mode,name:'Agent'}]},configOptions:config,
models:{currentModelId:'gpt-5.6-luna',availableModels:[{modelId:'gpt-5.6-luna',name:'Luna'}]}};
require('node:readline').createInterface({input:process.stdin}).on('line',line=>{
const m=JSON.parse(line);
if(m.method==='initialize')send({id:m.id,result:{protocolVersion:1,agentCapabilities:{loadSession:true},authMethods:[]}});
else if(['session/new','session/load'].includes(m.method))send({id:m.id,result:snapshot});
else if(m.method==='session/set_model'||m.method==='session/close')send({id:m.id,result:{}});
else if(m.method==='session/set_config_option')send({id:m.id,result:{configOptions:config}});
else if(m.method==='session/prompt')send({id:'pending-edit',method:'session/request_permission',params:{sessionId:'copilot-death-session',
toolCall:{toolCallId:'native-edit',title:'Write file',kind:'edit',status:'pending',rawInput:{path:'target.txt',content:'FORBIDDEN'}},
options:[{optionId:'allow',name:'Allow',kind:'allow_once'},{optionId:'deny',name:'Deny',kind:'reject_once'}]}});
});`;
async function listen(): Promise<Server> {
const server = createServer(); server.listen(0, "127.0.0.1"); await once(server, "listening"); return server;
}