Skip to content
Merged
2 changes: 1 addition & 1 deletion scripts/test-layout/layout.json
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,6 @@
"responses-claude-preflight-lease.test.ts": "responses",
"injection-routing-drift.test.ts": "codex-integration", "injection-routing-healer.test.ts": "codex-integration",
"injection-routing-heal-apply.test.ts": "codex-integration", "cli-start-routing-heal-wiring.test.ts": "cli",
"cli-status-codex-routing-drift.test.ts": "cli", "advisor-settings.test.ts": "advisor", "advisor-context.test.ts": "advisor", "advisor-state.test.ts": "advisor", "advisor-guard.test.ts": "advisor", "advisor-consult.test.ts": "advisor", "advisor-plan.test.ts": "advisor", "advisor-responses-wiring.test.ts": "advisor", "advisor-routes.test.ts": "server", "advisor-internal-authority.test.ts": "advisor", "advisor-core-boundary.test.ts": "advisor", "advisor-continuation-limit.test.ts": "advisor", "advisor-authority-transport.test.ts": "advisor", "advisor-evidence.test.ts": "advisor", "advisor-budget.test.ts": "advisor", "advisor-adaptive.test.ts": "advisor", "advisor-scope-transport.test.ts": "advisor", "local-credentials.test.ts": "cli", "local-credential-fence.test.ts": "config", "local-credential-generation.test.ts": "config", "link-websocket-upgrade.test.ts": "server", "link-websocket-gate.test.ts": "server", "link-websocket-drain.test.ts": "server", "link-websocket-pump.test.ts": "server", "link-websocket-rss.test.ts": "e2e-style", "link-websocket-holds.test.ts": "server", "link-websocket-traffic.test.ts": "server", "link-ws-activity-holds.test.ts": "clients", "link-websocket-fingerprints.test.ts": "server", "link-activity-holds-routes.test.ts": "server", "link-ws-activity-holds-operator.test.ts": "clients", "cli-link-activity.test.ts": "cli", "link-child-ops-ignore-activity.test.ts": "clients", "link-request-log-fields.test.ts": "server", "link-websocket-transport-status.test.ts": "server", "link-websocket-release-gate.test.ts": "e2e-style"
"cli-status-codex-routing-drift.test.ts": "cli", "advisor-settings.test.ts": "advisor", "advisor-context.test.ts": "advisor", "advisor-state.test.ts": "advisor", "advisor-guard.test.ts": "advisor", "advisor-consult.test.ts": "advisor", "advisor-plan.test.ts": "advisor", "advisor-responses-wiring.test.ts": "advisor", "advisor-routes.test.ts": "server", "advisor-internal-authority.test.ts": "advisor", "advisor-core-boundary.test.ts": "advisor", "advisor-continuation-limit.test.ts": "advisor", "advisor-authority-transport.test.ts": "advisor", "advisor-evidence.test.ts": "advisor", "advisor-budget.test.ts": "advisor", "advisor-adaptive.test.ts": "advisor", "advisor-scope-transport.test.ts": "advisor", "local-credentials.test.ts": "cli", "local-credential-fence.test.ts": "config", "local-credential-generation.test.ts": "config", "link-websocket-upgrade.test.ts": "server", "link-websocket-gate.test.ts": "server", "link-websocket-drain.test.ts": "server", "link-websocket-pump.test.ts": "server", "link-websocket-rss.test.ts": "e2e-style", "link-websocket-holds.test.ts": "server", "link-websocket-traffic.test.ts": "server", "link-ws-activity-holds.test.ts": "clients", "link-websocket-fingerprints.test.ts": "server", "link-activity-holds-routes.test.ts": "server", "link-ws-activity-holds-operator.test.ts": "clients", "cli-link-activity.test.ts": "cli", "link-child-ops-ignore-activity.test.ts": "clients", "link-request-log-fields.test.ts": "server", "link-websocket-transport-status.test.ts": "server", "link-websocket-release-gate.test.ts": "e2e-style", "link-gate-codex-binary.test.ts": "e2e-style"
}
}
2 changes: 1 addition & 1 deletion structure/remote-link.md

Large diffs are not rendered by default.

104 changes: 104 additions & 0 deletions tests/e2e-style/link-gate-codex-binary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { afterEach, describe, expect, test } from "bun:test";
import { chmodSync, copyFileSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, statSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { delimiter, isAbsolute, join, resolve } from "node:path";
import { createHash } from "node:crypto";
import { removeTreeWithRetry } from "../helpers/remove-tree";
import { preparePinnedCodex } from "../helpers/link-gate-binary";

// The link WebSocket release gate labels a run "release gate" when the sha256 of OCX_LINK_GATE_CODEX matches the
// reviewed digest (tests/fixtures/link-gate-codex.json). That label is only honest if the bytes it hashed are the bytes
// it runs. These cases need no real Codex: copies of the running Bun executable, named codex, stand in for it, and each
// copy prints its own path, so a run shows which file executed. That works the same on every platform.

const NAME = process.platform === "win32" ? "codex.exe" : "codex";
const PRINT_SELF = ["-e", "console.log(process.execPath)"];

let root = "";
afterEach(() => { if (root) removeTreeWithRetry(root); root = ""; });

/** A copy of this Bun named codex in `dir`. */
function fakeCodex(dir: string): string {
mkdirSync(dir, { recursive: true });
const file = join(dir, NAME);
copyFileSync(process.execPath, file);
chmodSync(file, 0o755);
return file;
}

const sha256 = (file: string) => createHash("sha256").update(readFileSync(file)).digest("hex");

/** Runs `path` from `cwd` with only `pathDir` on PATH; resolves with the path of the executable that ran. */
async function whoRan(path: string, cwd: string, pathDir: string): Promise<string> {
const child = Bun.spawn([path, ...PRINT_SELF], { cwd, env: { PATH: pathDir + delimiter + (process.env.PATH ?? "") }, stdout: "pipe", stderr: "pipe" });
await child.exited;
return realpathSync((await new Response(child.stdout).text()).trim());
}

describe("the pinned Codex binary of the release gate: what is hashed is what runs", () => {
test("a bare name runs the file that was hashed, not another codex on PATH", async () => {
root = mkdtempSync(join(tmpdir(), "ocx-gate-binary-"));
const cwd = join(root, "work"); const onPath = join(root, "bin"); const scratch = join(root, "scratch");
const hashed = fakeCodex(cwd);
// The codex on PATH has other bytes than the hashed file, so the digest tells the two apart. It is executable
// (0755) but never has to run: the gate must not reach it.
mkdirSync(onPath);
const onPathFile = join(onPath, NAME);
writeFileSync(onPathFile, "another codex on PATH, not the hashed one\n");
chmodSync(onPathFile, 0o755);
mkdirSync(scratch);
const prepared = preparePinnedCodex(NAME, { cwd, scratch })!;
expect(prepared.digest).toBe(sha256(hashed));
expect(prepared.digest).not.toBe(sha256(onPathFile));
// The executable that runs is the file the gate names (the copy of the hashed file), never the codex on PATH.
expect(await whoRan(prepared.path, cwd, onPath)).toBe(realpathSync(resolve(cwd, prepared.path)));
expect(isAbsolute(prepared.path)).toBe(true);
expect(sha256(prepared.path)).toBe(prepared.digest);
});

test("replacing the file after it was hashed does not change what runs", async () => {
root = mkdtempSync(join(tmpdir(), "ocx-gate-binary-"));
const cwd = join(root, "work"); const scratch = join(root, "scratch");
const original = fakeCodex(cwd);
mkdirSync(scratch);
const prepared = preparePinnedCodex(original, { cwd, scratch })!;
writeFileSync(original, "replaced after hashing");
expect(await whoRan(prepared.path, cwd, join(root, "none"))).toBe(realpathSync(prepared.path));
expect(sha256(prepared.path)).toBe(prepared.digest);
// The copy that runs is read-only.
if (process.platform !== "win32") expect(statSync(prepared.path).mode & 0o222).toBe(0);
});

test("the copy never replaces an existing file: a second copy into the same scratch directory is refused", () => {
root = mkdtempSync(join(tmpdir(), "ocx-gate-binary-"));
const cwd = join(root, "work"); const scratch = join(root, "scratch");
const original = fakeCodex(cwd);
mkdirSync(scratch);
const prepared = preparePinnedCodex(original, { cwd, scratch })!;
// Writable again, so only the exclusive flag (not the copy's 0555 mode) can refuse the second copy.
chmodSync(prepared.path, 0o755);
writeFileSync(original, "other bytes for the second copy");
chmodSync(original, 0o755);
let refused: unknown = null;
try { preparePinnedCodex(original, { cwd, scratch }); } catch (error) { refused = error; }
expect((refused as NodeJS.ErrnoException | null)?.code).toBe("EEXIST");
expect(sha256(prepared.path)).toBe(prepared.digest);
});

test("a value that names no file is refused", () => {
root = mkdtempSync(join(tmpdir(), "ocx-gate-binary-"));
const scratch = join(root, "scratch");
mkdirSync(scratch);
expect(preparePinnedCodex("", { cwd: root, scratch })).toBeNull();
expect(preparePinnedCodex("missing", { cwd: root, scratch })).toBeNull();
expect(preparePinnedCodex(root, { cwd: root, scratch })).toBeNull();
});

test.skipIf(process.platform === "win32")("a file without execute permission is refused (POSIX permissions)", () => {
root = mkdtempSync(join(tmpdir(), "ocx-gate-binary-"));
const scratch = join(root, "scratch");
mkdirSync(scratch);
writeFileSync(join(root, "plain"), "not executable");
expect(preparePinnedCodex("plain", { cwd: root, scratch })).toBeNull();
});
});
34 changes: 24 additions & 10 deletions tests/e2e-style/link-websocket-release-gate.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { afterEach, describe, expect, setDefaultTimeout, test } from "bun:test";
import { accessSync, constants, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { afterAll, afterEach, describe, expect, setDefaultTimeout, test } from "bun:test";
import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createHash } from "node:crypto";
import type { OcxConfig } from "../../src/types";
import { setLinkWebsocketLimitsForTests, setLinkWebsocketTransportReadyForTests } from "../../src/server/index/link-websocket-limits";
import { serviceApiTokenFingerprint } from "../../src/lib/service-secrets";
Expand All @@ -12,6 +11,7 @@ import { installIsolatedCodexHome, type IsolatedCodexHome } from "../helpers/iso
import { removeTreeWithRetry } from "../helpers/remove-tree";
import { STANDIN_MAX_MESSAGE_BYTES, startRelayStandin, type RelayStandin } from "../fixtures/link-child-relay-standin";
import { fixturePath } from "../helpers/repo-root";
import { preparePinnedCodex } from "../helpers/link-gate-binary";
import { startChild } from "../helpers/link-ws-child";
import { probeLinkRelayPreflight } from "../../src/client/link-relay-transport";
import { forgetLinkWebsocketAdvertisementsForTests } from "../../src/client/link-ws-advertisement";
Expand All @@ -20,22 +20,36 @@ import type { LinkDiagnosticRecord } from "../../src/link/diagnostics";
import type { Server } from "bun";

// Lane T5f: the link WebSocket release gate of design T4 11.1 item 13, with the pinned Codex binary as the client (U13).
// The pinned binary is codex-rs built at openai/codex 88c5856 (design section 0). These tests run only when
// OCX_LINK_GATE_CODEX names an executable; otherwise each is skipped with that reason. A run is release evidence only
// The pinned binary is codex-rs built at openai/codex 88c5856 (design section 0). The Codex scenarios run only when
// OCX_LINK_GATE_CODEX names an executable file; otherwise every Codex scenario is skipped with that reason, and the pin
// test always runs. A run is release evidence only
// when the binary's sha256 equals the digest recorded for that revision in tests/fixtures/link-gate-codex.json, a file
// that changes only through review; any other executable runs as a smoke check, and every test title says which. Codex reaches the Home through a test-only
// that changes only through review; any other executable runs as a smoke check, and each Codex scenario's title says which. Codex reaches the Home through a test-only
// stand-in for the Child's relay (tests/fixtures/link-child-relay-standin.ts), except in the 503 sequence, which needs the
// Child's own relay and tunnel gate (lane T6, src/client/link-websocket.ts) and runs the real Child listener.
// Codex is an external process with no clock to inject, so each test carries a per-test bound instead of sleeps.

const BIN = process.env.OCX_LINK_GATE_CODEX ?? "";
const usable = (() => { if (!BIN) return false; try { accessSync(BIN, constants.X_OK); return true; } catch { return false; } })();
// OCX_LINK_GATE_CODEX is resolved once to an executable file and copied read-only into a scratch directory; that copy is
// what is hashed and what every scenario spawns, by absolute path (tests/helpers/link-gate-binary.ts).
// The scratch copy is removed after the file, and at once if preparing it throws or the value is refused.
const BIN_SCRATCH = process.env.OCX_LINK_GATE_CODEX ? mkdtempSync(join(tmpdir(), "ocx-link-gate-bin-")) : "";
afterAll(() => { if (BIN_SCRATCH) removeTreeWithRetry(BIN_SCRATCH); });
const PREPARED = (() => {
if (!BIN_SCRATCH) return null;
try {
const prepared = preparePinnedCodex(process.env.OCX_LINK_GATE_CODEX!, { scratch: BIN_SCRATCH });
if (!prepared) removeTreeWithRetry(BIN_SCRATCH);
return prepared;
} catch (error) { removeTreeWithRetry(BIN_SCRATCH); throw error; }
})();
const BIN = PREPARED?.path ?? "";
const usable = PREPARED !== null;
const PIN = JSON.parse(readFileSync(fixturePath("link-gate-codex.json"), "utf8")) as { revision: string; sha256: string | null };
const PINNED_SHA256 = typeof PIN.sha256 === "string" ? PIN.sha256.toLowerCase() : "";
const digest = usable ? createHash("sha256").update(readFileSync(BIN)).digest("hex") : "";
const digest = PREPARED?.digest ?? "";
const mode: "absent" | "gate" | "smoke" = !usable ? "absent" : PINNED_SHA256 && digest === PINNED_SHA256 ? "gate" : "smoke";
const LABEL = {
absent: " [skipped: pinned Codex binary absent (U13); set OCX_LINK_GATE_CODEX to codex-rs built at openai/codex 88c5856]",
absent: " [skipped: pinned Codex binary absent (U13); set OCX_LINK_GATE_CODEX to an executable file of codex-rs built at openai/codex 88c5856]",
gate: " [release gate: Codex sha256 " + digest + " matches the reviewed digest for openai/codex " + PIN.revision.slice(0, 7) + " in tests/fixtures/link-gate-codex.json]",
smoke: " [smoke only, not release evidence: Codex sha256 " + digest + " is not the reviewed digest for openai/codex " + PIN.revision.slice(0, 7) + " (tests/fixtures/link-gate-codex.json" + (PINNED_SHA256 ? "" : " records none yet") + ")]",
}[mode];
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/link-gate-codex.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"revision": "88c58562580aa4abe2da6ce4d7676bf171682230",
"sha256": null,
"note": "The sha256 of codex-rs built at this openai/codex revision, recorded by whoever builds and attests it, in a reviewed change to this file. While it is null, no run of tests/e2e-style/link-websocket-release-gate.test.ts is release evidence."
"sha256": "c80d5ba4a92ffef0ef1ad1e9345429740ada15fa158b1792900e36892573a23c",
"note": "The sha256 of codex-rs/target/release/codex built at this openai/codex revision, attested by OpenCodex agent lane G on its build VM on 2026-11-13 in #8668 (system clock, UTC; the successful build ran 05:35:07Z to 06:54:37Z). Command, in codex-rs of a clean checkout of this revision: cargo build --release --locked -j 1 -p codex-cli --bin codex. Toolchain: rustc 1.95.0 (59807616e 2026-04-14, LLVM 22.1.2) and cargo 1.95.0, selected by the revision's rust-toolchain.toml. Target x86_64-unknown-linux-gnu; Ubuntu 24.04.5 LTS, Linux 6.12.113, glibc 2.39, cc clang 18.1.3, libcap-dev 2.66 and libssl-dev 3.0.13 from Ubuntu. The revision's release profile is unchanged (thin LTO, 4 codegen units, line-table debug info, not stripped); -j 1 was used only because the kernel's memory-cgroup OOM killer killed rustc at the default width on the 16 GiB VM. The binary is 1618990456 bytes and prints codex-cli 0.0.0 for --version. This is one attested build, not a reproducible one: the digest identifies this Linux x86_64 binary only. A build on another platform, or with another toolchain, C compiler or system library, or in another build directory, may have a different digest; only a binary whose sha256 matches this one exactly gets the release-gate label, and any other runs as a smoke check by design. While this digest is recorded, a run of tests/e2e-style/link-websocket-release-gate.test.ts with this binary is release evidence for that test file only. This file changes only through review."
}
1 change: 1 addition & 0 deletions tests/fixtures/test-layout-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,7 @@
"link-request-log-fields.test.ts": "server",
"link-websocket-transport-status.test.ts": "server",
"link-websocket-release-gate.test.ts": "e2e-style",
"link-gate-codex-binary.test.ts": "e2e-style",
"link-websocket-drain.test.ts": "server",
"link-websocket-pump.test.ts": "server",
"link-websocket-rss.test.ts": "e2e-style",
Expand Down
36 changes: 36 additions & 0 deletions tests/helpers/link-gate-binary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createHash } from "node:crypto";
import { accessSync, chmodSync, closeSync, constants, copyFileSync, openSync, readSync, statSync } from "node:fs";
import { basename, join, resolve } from "node:path";

/**
* The executable a release-gate run hashes and runs (tests/e2e-style/link-websocket-release-gate.test.ts). `value` is
* OCX_LINK_GATE_CODEX: it is resolved against `cwd` once and must name an executable regular file; anything else is
* refused (null). That file is copied read-only into `scratch`, and the copy is both what is hashed and what is spawned,
* by absolute path. So a bare name cannot hash one file and run another found on PATH, and replacing the original after
* it was hashed cannot change what runs.
*/
export function preparePinnedCodex(value: string, options: { cwd?: string; scratch: string }): { path: string; digest: string } | null {
if (!value) return null;
const file = resolve(options.cwd ?? process.cwd(), value);
try {
if (!statSync(file).isFile()) return null;
accessSync(file, constants.X_OK);
} catch { return null; }
// The copy keeps the file name, so an extension a platform needs to execute it (codex.exe) stays.
const copy = join(options.scratch, basename(file));
// Exclusive: never over an existing file. FICLONE makes the copy a reflink where the file system has them (APFS, btrfs,
// XFS), so the 1.6 GB binary costs no space there; elsewhere it is an ordinary copy.
copyFileSync(file, copy, constants.COPYFILE_EXCL | constants.COPYFILE_FICLONE);
chmodSync(copy, 0o555);
return { path: copy, digest: sha256File(copy) };
}

function sha256File(file: string): string {
const hash = createHash("sha256");
const buffer = Buffer.allocUnsafe(8 * 1024 * 1024);
const fd = openSync(file, "r");
try {
for (let read = readSync(fd, buffer); read > 0; read = readSync(fd, buffer)) hash.update(buffer.subarray(0, read));
} finally { closeSync(fd); }
return hash.digest("hex");
}
Loading