@jbkjr/relay 0.0.0-stage → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/LICENSES/Apache-2.0.txt +201 -0
- package/LICENSES/README.md +19 -0
- package/LICENSES/codex-NOTICE.txt +6 -0
- package/README.md +393 -2
- package/SECURITY.md +145 -0
- package/adapters/briefs/claude.md +43 -0
- package/adapters/briefs/codex.md +56 -0
- package/adapters/briefs/models.json +33 -0
- package/adapters/build.mjs +8 -0
- package/adapters/claude/.claude-plugin/marketplace.json +13 -0
- package/adapters/claude/relay/.claude-plugin/plugin.json +5 -0
- package/adapters/claude/relay/agents/codex.md +21 -0
- package/adapters/claude/relay/agents/operator.md +21 -0
- package/adapters/claude/relay/skills/ask/SKILL.md +20 -0
- package/adapters/claude/relay/skills/chat/SKILL.md +13 -0
- package/adapters/claude/relay/skills/delegate/SKILL.md +21 -0
- package/adapters/claude/relay/skills/discuss/SKILL.md +19 -0
- package/adapters/claude/relay/skills/review/SKILL.md +21 -0
- package/adapters/claude/relay/skills/tree/SKILL.md +10 -0
- package/adapters/claude/relay/skills/write-for-codex/SKILL.md +78 -0
- package/adapters/codex/.agents/plugins/marketplace.json +20 -0
- package/adapters/codex/AGENTS.snippet.md +5 -0
- package/adapters/codex/relay/.codex-plugin/plugin.json +6 -0
- package/adapters/codex/relay/skills/relay-ask/SKILL.md +20 -0
- package/adapters/codex/relay/skills/relay-chat/SKILL.md +13 -0
- package/adapters/codex/relay/skills/relay-delegate/SKILL.md +21 -0
- package/adapters/codex/relay/skills/relay-discuss/SKILL.md +19 -0
- package/adapters/codex/relay/skills/relay-review/SKILL.md +21 -0
- package/adapters/codex/relay/skills/relay-tree/SKILL.md +10 -0
- package/adapters/codex/relay/skills/relay-write-for-claude/SKILL.md +66 -0
- package/adapters/generate.mjs +124 -0
- package/bin/relay +59 -0
- package/codex-pin.json +12 -0
- package/dist/backends/acp.js +1424 -0
- package/dist/backends/claude.js +890 -0
- package/dist/backends/cli-version.js +112 -0
- package/dist/backends/codex-app.js +1539 -0
- package/dist/backends/codex-daybreak.js +75 -0
- package/dist/backends/codex-env.js +656 -0
- package/dist/backends/codex-network.js +304 -0
- package/dist/backends/codex-pin.js +345 -0
- package/dist/backends/codex-shell-policy.js +182 -0
- package/dist/backends/codex-stage2.js +998 -0
- package/dist/backends/codex-subagents.js +47 -0
- package/dist/backends/codex.js +641 -0
- package/dist/backends/copilot.js +246 -0
- package/dist/backends/env.js +147 -0
- package/dist/backends/gemini.js +607 -0
- package/dist/backends/grok.js +337 -0
- package/dist/backends/index.js +8 -0
- package/dist/backends/native-temp.js +187 -0
- package/dist/backends/never-admission.js +90 -0
- package/dist/backends/run.js +67 -0
- package/dist/backends/stream.js +230 -0
- package/dist/backends/tool-discovery.js +15 -0
- package/dist/backends/types.js +264 -0
- package/dist/build-info.json +5 -0
- package/dist/cli.js +344 -0
- package/dist/confine/bridge.js +113 -0
- package/dist/confine/broker-core.js +76 -0
- package/dist/confine/broker-http.js +650 -0
- package/dist/confine/broker-names.js +9 -0
- package/dist/confine/broker.js +258 -0
- package/dist/confine/canary.js +380 -0
- package/dist/confine/diagnostics.js +122 -0
- package/dist/confine/gemini-tools-main.js +8 -0
- package/dist/confine/gemini-tools.js +181 -0
- package/dist/confine/hardlinks.js +58 -0
- package/dist/confine/harness-config.js +16 -0
- package/dist/confine/index.js +117 -0
- package/dist/confine/leases.js +217 -0
- package/dist/confine/paths.js +6 -0
- package/dist/confine/peer.js +115 -0
- package/dist/confine/port-policy.js +8 -0
- package/dist/confine/ports.js +365 -0
- package/dist/confine/private-root.js +208 -0
- package/dist/confine/probe-stage2.js +501 -0
- package/dist/confine/probe.js +1109 -0
- package/dist/confine/proxy.js +783 -0
- package/dist/confine/sbcheck.js +208 -0
- package/dist/confine/seatbelt-never.js +41 -0
- package/dist/confine/seatbelt-stage2.js +139 -0
- package/dist/confine/seatbelt.js +1129 -0
- package/dist/confine/sockets.js +36 -0
- package/dist/confine/turn-probe.js +19 -0
- package/dist/confine/turn.js +86 -0
- package/dist/core/adoption.js +124 -0
- package/dist/core/api-keys.js +423 -0
- package/dist/core/approvals.js +740 -0
- package/dist/core/build-identity.js +248 -0
- package/dist/core/caller.js +749 -0
- package/dist/core/calls.js +102 -0
- package/dist/core/claude-settings.js +1045 -0
- package/dist/core/config.js +453 -0
- package/dist/core/context.js +186 -0
- package/dist/core/db.js +753 -0
- package/dist/core/dispatch.js +110 -0
- package/dist/core/env-broker.js +505 -0
- package/dist/core/env-policy.js +182 -0
- package/dist/core/escalation-authority.js +76 -0
- package/dist/core/escalation-contract.js +13 -0
- package/dist/core/escalation-person-path.js +131 -0
- package/dist/core/escalation-policy.js +79 -0
- package/dist/core/escalation-runtime.js +135 -0
- package/dist/core/escalation-state.js +42 -0
- package/dist/core/escalations.js +191 -0
- package/dist/core/events.js +235 -0
- package/dist/core/executables.js +147 -0
- package/dist/core/extensions.js +645 -0
- package/dist/core/families.js +204 -0
- package/dist/core/git.js +657 -0
- package/dist/core/identity.js +113 -0
- package/dist/core/liveness.js +68 -0
- package/dist/core/mediated-network-provider.js +81 -0
- package/dist/core/never-checkpoint.js +76 -0
- package/dist/core/never-coverage.js +97 -0
- package/dist/core/never-network.js +49 -0
- package/dist/core/never-policy.js +302 -0
- package/dist/core/never-proxy-policy.js +109 -0
- package/dist/core/never-schema.js +61 -0
- package/dist/core/never-tools.js +56 -0
- package/dist/core/notices.js +48 -0
- package/dist/core/ops.js +959 -0
- package/dist/core/outcome.js +224 -0
- package/dist/core/params.js +32 -0
- package/dist/core/presence.js +174 -0
- package/dist/core/procs.js +546 -0
- package/dist/core/prompts.js +363 -0
- package/dist/core/protected-config.js +238 -0
- package/dist/core/raw-network.js +192 -0
- package/dist/core/redact.js +152 -0
- package/dist/core/refusal.js +33 -0
- package/dist/core/relay-command.js +310 -0
- package/dist/core/reported.js +3 -0
- package/dist/core/restart-overlay.js +651 -0
- package/dist/core/retention.js +1135 -0
- package/dist/core/secrets.js +329 -0
- package/dist/core/session-token.js +175 -0
- package/dist/core/session.js +744 -0
- package/dist/core/shell.js +2 -0
- package/dist/core/shipped-pin.js +47 -0
- package/dist/core/shown.js +42 -0
- package/dist/core/slots.js +67 -0
- package/dist/core/stage2-executors.js +453 -0
- package/dist/core/stage2-switch.js +5 -0
- package/dist/core/steering.js +149 -0
- package/dist/core/storage.js +358 -0
- package/dist/core/toml.js +175 -0
- package/dist/core/views.js +324 -0
- package/dist/core/wait.js +114 -0
- package/dist/core/wake.js +207 -0
- package/dist/core/watch.js +247 -0
- package/dist/core/worktree.js +213 -0
- package/dist/hosts/claude-desktop.js +274 -0
- package/dist/hosts/claude.js +249 -0
- package/dist/hosts/codex.js +345 -0
- package/dist/hosts/desktop-shared.js +20 -0
- package/dist/hosts/index.js +11 -0
- package/dist/hosts/types.js +4 -0
- package/dist/install/basics.js +18 -0
- package/dist/install/claude-init.js +133 -0
- package/dist/install/common.js +655 -0
- package/dist/install/doctor.js +635 -0
- package/dist/install/qualify-codex.js +205 -0
- package/dist/install.js +524 -0
- package/dist/mcp/elicit.js +209 -0
- package/dist/mcp/gateway.js +218 -0
- package/dist/mcp/host.js +253 -0
- package/dist/mcp/server.js +1124 -0
- package/dist/mcp/stdio.js +37 -0
- package/dist/modes/discussion.js +393 -0
- package/dist/modes/review.js +722 -0
- package/dist/native/relay-sbcheck +0 -0
- package/dist/targets/briefs.js +86 -0
- package/dist/targets/claude.js +94 -0
- package/dist/targets/codex.js +66 -0
- package/dist/targets/copilot.js +93 -0
- package/dist/targets/gemini.js +119 -0
- package/dist/targets/grok.js +126 -0
- package/dist/targets/index.js +192 -0
- package/dist/targets/types.js +18 -0
- package/dist/targets/verified.js +35 -0
- package/dist/worker.js +113 -0
- package/docs/UPGRADING.md +1518 -0
- package/docs/pinned-codex.md +333 -0
- package/experiments/stage2-redteam/lib/oracles.mjs +214 -0
- package/experiments/stage2-redteam/lib/report.mjs +35 -0
- package/npm-shrinkwrap.json +1632 -0
- package/package.json +82 -4
- package/scripts/codex-qualify/codex-pin-store.mjs +353 -0
- package/scripts/codex-qualify/codex-qualifier-process.mjs +265 -0
- package/scripts/codex-qualify/codex-qualifier.example.json +5 -0
- package/scripts/codex-qualify/codex-qualifier.mjs +281 -0
- package/scripts/codex-qualify/codex-shipped-pin.mjs +158 -0
- package/scripts/codex-qualify/codex-stage2-pin.mjs +48 -0
- package/scripts/codex-qualify/codex-stage2.mjs +2381 -0
- package/scripts/codex-qualify/install-codex-qualifier.mjs +98 -0
- package/scripts/codex-qualify/main-module.mjs +14 -0
- package/scripts/codex-qualify/relay-codex-qualifier.plist +37 -0
- package/scripts/codex-qualify/scratch-env.mjs +31 -0
- package/scripts/codex-qualify/session.mjs +96 -0
- package/scripts/codex-qualify/stage2-escalation-cases.mjs +125 -0
- package/test/codex-permissions.test.ts +875 -0
- package/test/fixtures/boundary-probes.mjs +17 -0
- package/test/fixtures/denial-stream-host.mjs +7 -0
- package/test/fixtures/families.json +246 -0
- package/test/fixtures/install-snapshots/claude-desktop-foreign.txt +158 -0
- package/test/fixtures/install-snapshots/claude-desktop-fresh.txt +101 -0
- package/test/fixtures/install-snapshots/claude-desktop-installed.txt +161 -0
- package/test/fixtures/install-snapshots/claude-foreign.txt +133 -0
- package/test/fixtures/install-snapshots/claude-fresh.txt +77 -0
- package/test/fixtures/install-snapshots/claude-installed.txt +133 -0
- package/test/fixtures/install-snapshots/codex-foreign.txt +159 -0
- package/test/fixtures/install-snapshots/codex-fresh.txt +107 -0
- package/test/fixtures/install-snapshots/codex-installed.txt +161 -0
- package/test/fixtures/link-parent.mjs +6 -0
- package/test/fixtures/mediated-command.cjs +72 -0
- package/test/fixtures/native-skill-isolation.json +280 -0
- package/test/fixtures/pinned-codex-app-server.mjs +25 -0
- package/test/fixtures/qualified-ext-adapters.mjs +36 -0
- package/test/fixtures/refusals/README.md +52 -0
- package/test/fixtures/refusals/claude-source-result.json +7 -0
- package/test/fixtures/refusals/openai-app-capacity.json +24 -0
- package/test/fixtures/refusals/openai-app-cyberPolicy.json +24 -0
- package/test/fixtures/refusals/openai-gpt-6-astra-max.json +12 -0
- package/test/fixtures/refusals/openai-gpt-6.1-sol-xhigh.json +12 -0
- package/test/fixtures/refusals/openai-review-capacity.json +24 -0
- package/test/fixtures/relay-0.2.0.sql +57 -0
- package/test/fixtures/relay-0.9.1.gen.mjs +212 -0
- package/test/fixtures/relay-0.9.1.sql +340 -0
- package/test/fixtures/slow-boundary-probes.mjs +37 -0
- package/test/fixtures/stable-surfaces/config.json +1122 -0
- package/test/fixtures/stable-surfaces/escalation.json +128 -0
- package/test/fixtures/stable-surfaces/events.json +37 -0
- package/test/fixtures/stable-surfaces/gemini-native-resume.json +4 -0
- package/test/fixtures/stable-surfaces/inbox.jsonl +2 -0
- package/test/fixtures/stable-surfaces/notice.txt +1 -0
- package/test/fixtures/stable-surfaces/refusal-item.json +28 -0
- package/test/fixtures/stable-surfaces/refusal-review.json +23 -0
- package/test/fixtures/stable-surfaces/registry.json +49 -0
- package/test/fixtures/stable-surfaces/tools-desktop.json +575 -0
- package/test/fixtures/stable-surfaces/tools.json +716 -0
- package/test/fixtures/stable-surfaces/usage.txt +46 -0
- package/test/fixtures/stage2-acp-native.mjs +138 -0
- package/test/fixtures/stage2-app-server.mjs +23 -0
- package/test/fixtures/stage2-catalog.json +91 -0
- package/test/fixtures/stage2-hostile-web.mjs +35 -0
- package/test/fixtures/stage2-native-network.mjs +62 -0
- package/test/fixtures/stage2-worker.ts +87 -0
- package/test/fixtures/status-views/expected.json +7825 -0
- package/test/fixtures/status-views/registries.json +1 -0
- package/test/fixtures/steering.json +209 -0
- package/test/helpers.ts +740 -0
- package/test/host-env.ts +7 -0
- package/test/keychain-decoy.ts +197 -0
- package/test/live-scope.ts +44 -0
- package/test/mediated-network.test.ts +192 -0
- package/test/mediated-proxy.test.ts +264 -0
- package/test/native-temp-alias.ts +10 -0
- package/test/never-policy.test.ts +629 -0
- package/test/never-tools.test.ts +136 -0
- package/test/process-home.ts +18 -0
- package/test/proxy.test.ts +531 -0
- package/test/restart-overlay-live.test.ts +85 -0
- package/test/restart-overlay-profile.test.ts +66 -0
- package/test/restart-overlay.test.ts +416 -0
- package/test/stable-surfaces.test.ts +249 -0
- package/test/stage2-escalation-live.test.ts +544 -0
- package/test/stage2-escalation-qualification.test.ts +170 -0
- package/test/stage2-escalation.test.ts +747 -0
- package/test/stage2-executor.test.ts +849 -0
- package/test/stage2-grant.test.ts +603 -0
- package/test/stage2-native-live.test.ts +647 -0
- package/test/stage2-network-decisions.test.ts +70 -0
- package/test/stage2-probe.test.ts +845 -0
- package/test/stage2-standing-live.test.ts +203 -0
- package/test/stubs/broker-host.mjs +59 -0
- package/test/stubs/broker-peer.mjs +20 -0
- package/test/stubs/confined-harness.mjs +91 -0
- package/test/stubs/ext-broken-server.mjs +2 -0
- package/test/stubs/ext-odd-server.mjs +21 -0
- package/test/stubs/ext-server.mjs +31 -0
- package/test/stubs/fake-host-cli.mjs +218 -0
- package/test/stubs/procargs.mjs +30 -0
- package/test/stubs/stub-acp +4 -0
- package/test/stubs/stub-agent.mjs +322 -0
- package/test/stubs/stub-claude +4 -0
- package/test/stubs/stub-codex +4 -0
- package/test/stubs/targets/acp.mjs +307 -0
- package/test/stubs/targets/claude.mjs +149 -0
- package/test/stubs/targets/codex.mjs +479 -0
- package/test/stubs/targets/gemini.mjs +604 -0
- package/test/temp-root.ts +11 -0
- package/test/wp4-adapters.test.ts +353 -0
- package/test/wp4-interlock.test.ts +136 -0
|
@@ -0,0 +1,849 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { execFileSync, spawn, spawnSync } from "node:child_process";
|
|
3
|
+
import crypto from "node:crypto";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import net from "node:net";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { test } from "node:test";
|
|
9
|
+
import "./helpers.ts";
|
|
10
|
+
import { executorEnv, executorPolicy, executorToken, openStage2, setInterfaceReaderForTests, setLeaseOptionsForTests, STAGE2_ENVIRONMENT_ID, sweepTurnSurvivors } from "../dist/backends/codex-env.js";
|
|
11
|
+
import { startCodexAppTurn } from "../dist/backends/codex-app.js";
|
|
12
|
+
import { sandboxAllows, sandboxCheckHelper, sandboxCheckUnavailable, sandboxReports, setSandboxCheckHelperForTests } from "../dist/confine/sbcheck.js";
|
|
13
|
+
import { ensurePrivateRoot, nativeTempRoot } from "../dist/confine/private-root.js";
|
|
14
|
+
import { privateTurnTemp } from "../dist/confine/turn.js";
|
|
15
|
+
import { busyPorts, interfaceAddresses, portHeld, rangePorts } from "../dist/confine/ports.js";
|
|
16
|
+
import { executorRecords, liveExecutorRecords, stopNodeExecutorsNow } from "../dist/core/stage2-executors.js";
|
|
17
|
+
import { dirIdentity } from "../dist/confine/seatbelt.js";
|
|
18
|
+
import { redact, withTurnSecrets } from "../dist/core/secrets.js";
|
|
19
|
+
import { relayHome, withDefaults } from "../dist/core/config.js";
|
|
20
|
+
import { createRun, insertNode, insertTurn, setParam } from "../dist/core/db.js";
|
|
21
|
+
import { escalationRuntime } from "../dist/core/escalation-contract.js";
|
|
22
|
+
import { createMediatedNetworkProvider, setMediatedNetworkProviderForTests } from "../dist/core/mediated-network-provider.js";
|
|
23
|
+
import { checkpointNever } from "../dist/core/never-checkpoint.js";
|
|
24
|
+
import { rawNetworkKey } from "../dist/core/raw-network.js";
|
|
25
|
+
import { targetDef } from "../dist/targets/index.js";
|
|
26
|
+
import type { Stage2Network } from "../dist/confine/seatbelt-stage2.js";
|
|
27
|
+
import type { TurnSpec } from "../dist/backends/types.js";
|
|
28
|
+
import { parse } from "smol-toml";
|
|
29
|
+
import { codexFlags } from "../dist/backends/codex.js";
|
|
30
|
+
import { configProblem } from "../dist/backends/codex-app.js";
|
|
31
|
+
import { Stage2TurnGuard, STAGE2_FEATURES_OFF, stage2Admission, stage2Plan, stage2Permissions } from "../dist/backends/codex-stage2.js";
|
|
32
|
+
import { codexToolTimeoutS } from "../dist/targets/codex.js";
|
|
33
|
+
|
|
34
|
+
test("BB: flags and config preserve the exact required HTTP broker under relay-only", () => {
|
|
35
|
+
const dir = scratch();
|
|
36
|
+
try {
|
|
37
|
+
const spec = specFor(dir, "/bin/true");
|
|
38
|
+
const plan = stage2Plan(spec, {} as never);
|
|
39
|
+
plan.broker = { url: "http://127.0.0.1:47205/mcp", toolTimeoutS: codexToolTimeoutS(spec.config.approval_timeout_s) };
|
|
40
|
+
const flags = codexFlags(spec, ["relay", "relay_tools", "foreign"], {}, [], undefined, plan);
|
|
41
|
+
const settings = new Map(flags.c.filter((_, i) => flags.c[i - 1] === "-c").map((s) => [s.slice(0, s.indexOf("=")), s]));
|
|
42
|
+
const eff = parse([...settings.values()].join("\n")) as any;
|
|
43
|
+
eff.features.plugins = false;
|
|
44
|
+
eff.features.skill_mcp_dependency_install = false;
|
|
45
|
+
assert.deepEqual({ ...eff.mcp_servers.relay_tools }, {
|
|
46
|
+
url: plan.broker.url, bearer_token_env_var: "RELAY_BROKER_TOKEN", environment_id: "local",
|
|
47
|
+
enabled: true, required: true, startup_readiness: "connection", default_tools_approval_mode: "approve",
|
|
48
|
+
tool_timeout_sec: plan.broker.toolTimeoutS, startup_timeout_sec: 30,
|
|
49
|
+
});
|
|
50
|
+
assert.equal(eff.agents.max_concurrent_threads_per_session, 6);
|
|
51
|
+
assert.equal(eff.agents.max_threads, undefined);
|
|
52
|
+
assert.equal(eff.mcp_servers.relay.enabled, false);
|
|
53
|
+
assert.equal(eff.mcp_servers.foreign.enabled, false);
|
|
54
|
+
assert.equal(configProblem(eff, flags.expect), null);
|
|
55
|
+
eff.mcp_servers.relay_tools.required = false;
|
|
56
|
+
assert.match(configProblem(eff, flags.expect) ?? "", /required/);
|
|
57
|
+
eff.mcp_servers.relay_tools.required = true;
|
|
58
|
+
eff.mcp_servers.relay_tools.http_headers = { Authorization: "private" };
|
|
59
|
+
const wrong = configProblem(eff, flags.expect) ?? "";
|
|
60
|
+
assert.match(wrong, /unexpected keys/);
|
|
61
|
+
assert.doesNotMatch(wrong, /private/);
|
|
62
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }); }
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("BB: open order, isolated credential, fatal watch and teardown after failed executor launch", async () => {
|
|
66
|
+
const { setStage2DepsForTests } = await import("../dist/backends/codex-env.js");
|
|
67
|
+
const dir = scratch(), order: string[] = [];
|
|
68
|
+
let fatal: ((why: string) => void) | undefined;
|
|
69
|
+
let launchFails = false;
|
|
70
|
+
let served = 0;
|
|
71
|
+
let snapshotFails = false, brokerFails = false;
|
|
72
|
+
let stoppedOptions: any;
|
|
73
|
+
let launchedEnvironment: any, compiledNetwork: any;
|
|
74
|
+
let proxyLeased = false;
|
|
75
|
+
const broker = {
|
|
76
|
+
url: "http://127.0.0.1:47205/mcp", env: Object.freeze({ RELAY_BROKER_TOKEN: "unit-token" }),
|
|
77
|
+
bindAppServer(p: unknown) { order.push("bind-app"); assert.deepEqual(p, { pid: 123, start: "start" }); },
|
|
78
|
+
arm() { order.push("arm"); }, disarm() { order.push("disarm"); },
|
|
79
|
+
sessionsServed: () => served, listTools: async () => [{ name: "status" }],
|
|
80
|
+
onFatal(f: typeof fatal) { fatal = f; }, async close() { order.push("broker-close"); },
|
|
81
|
+
};
|
|
82
|
+
setStage2DepsForTests({
|
|
83
|
+
admission: () => (order.push("admission"), null), sweepProblem: () => null,
|
|
84
|
+
confinement: () => ({ mechanism: {} }), probe: () => async () => ({ launch: {}, probes: {} }), resolveBin: () => "/bin/true",
|
|
85
|
+
lease: (proxy) => (proxyLeased = !!proxy, order.push("lease"), { exec: 47204, broker: 47205, proxy: proxy ? 47206 : undefined, block: [47200, 47201, 47202, 47203], range: [47204, 47205, 47206], release() { order.push("release"); } }),
|
|
86
|
+
deniedTcp: () => { order.push("snapshot"); if (snapshotFails) throw new Error("snapshot collision"); return []; }, interfaces: () => [],
|
|
87
|
+
openBroker: async () => { order.push("broker-open"); if (brokerFails) throw new Error("broker failed"); return broker; }, policy: (_s, _t, network) => { compiledNetwork = network; return {}; }, appStart: () => "start",
|
|
88
|
+
launch: async (o: any) => { order.push("executor"); launchedEnvironment = o.env; assert.equal(o.env.RELAY_BROKER_TOKEN, undefined); if (launchFails) throw new Error("executor failed"); return { url: "ws://127.0.0.1:47204", exited: new Promise(() => {}) }; },
|
|
89
|
+
stop: async (_executor: unknown, options: unknown) => { stoppedOptions = options; order.push("executor-stop"); return { survivors: [] }; },
|
|
90
|
+
watchInterfaces: () => ({ changed: () => null, onChange() {}, stop() { order.push("watch-stop"); } }),
|
|
91
|
+
} as never);
|
|
92
|
+
try {
|
|
93
|
+
const spec = specFor(dir, "/bin/true");
|
|
94
|
+
const turnId = insertTurn(spec.markers!.nodeId, "test", "", "write", "running");
|
|
95
|
+
spec.relay = { nodeId: spec.markers!.nodeId, turnId, mayDelegate: false, mayAsk: true } as never;
|
|
96
|
+
await withTurnSecrets(async () => {
|
|
97
|
+
const s = await openStage2(spec, { env: { NO_PROXY: "example.org", no_proxy: "localhost" }, servers: [] }, dir);
|
|
98
|
+
assert.deepEqual(order, ["admission", "lease", "snapshot", "broker-open", "executor"]);
|
|
99
|
+
assert.deepEqual(compiledNetwork.bindPorts, [47200, 47201, 47202, 47203], "person-consented raw network may bind its reserved block");
|
|
100
|
+
assert.equal(launchedEnvironment.PORT, "47200"); assert.equal(launchedEnvironment.PORTS, "47200,47201,47202,47203");
|
|
101
|
+
assert.equal(s.plan.broker?.toolTimeoutS, codexToolTimeoutS(spec.config.approval_timeout_s));
|
|
102
|
+
assert.ok(Object.isFrozen(s.session.env));
|
|
103
|
+
assert.equal(s.session.env.RELAY_BROKER_TOKEN, "unit-token");
|
|
104
|
+
assert.equal(s.session.env.CODEX_EXEC_SERVER_URL, "none");
|
|
105
|
+
for (const key of ["NO_PROXY", "no_proxy"]) assert.deepEqual(new Set(s.session.env[key]!.split(",")), new Set(["example.org", "localhost", "127.0.0.1"]));
|
|
106
|
+
const eff = { approval_policy: "on-request", agents: { max_concurrent_threads_per_session: 6 },
|
|
107
|
+
features: Object.fromEntries(STAGE2_FEATURES_OFF.map((f) => [f, false])),
|
|
108
|
+
mcp_servers: { relay: { enabled: false }, relay_tools: {
|
|
109
|
+
url: broker.url, bearer_token_env_var: "RELAY_BROKER_TOKEN", environment_id: "local", enabled: true,
|
|
110
|
+
required: true, default_tools_approval_mode: "approve", startup_timeout_sec: 30, tool_timeout_sec: s.plan.broker!.toolTimeoutS,
|
|
111
|
+
} },
|
|
112
|
+
};
|
|
113
|
+
const rpc = { request: async (method: string, params: any) => {
|
|
114
|
+
if (method === "environment/status") return { status: "unknown" };
|
|
115
|
+
if (method === "experimentalFeature/list") return { data: STAGE2_FEATURES_OFF.map((name) => ({ name, enabled: false })) };
|
|
116
|
+
if (method === "hooks/list") return { data: [{ hooks: [], errors: [] }] };
|
|
117
|
+
assert.equal(method, "mcpServerStatus/list");
|
|
118
|
+
if (!params.threadId) served++;
|
|
119
|
+
return { data: [{ name: "relay_tools", runtimeStatus: "connected", authStatus: "bearerToken", httpOrigin: "http://127.0.0.1:47205", tools: { status: { name: "status" } } }] };
|
|
120
|
+
} };
|
|
121
|
+
assert.equal(await s.session.readback(rpc, eff), null, "passes the real BC readback the broker and expected timeout");
|
|
122
|
+
const checkpoint = s.session.sessionsServed();
|
|
123
|
+
assert.equal(checkpoint, 1);
|
|
124
|
+
assert.match(await s.session.threadMcpProblem(rpc, eff, "thread", checkpoint) ?? "", /no authenticated.*thread session/);
|
|
125
|
+
served++;
|
|
126
|
+
assert.equal(await s.session.threadMcpProblem(rpc, eff, "thread", checkpoint), null, "thread session evidence is measured from before startup");
|
|
127
|
+
s.session.bindAppServer(123);
|
|
128
|
+
assert.equal(escalationRuntime(spec.markers!.nodeId, turnId), undefined);
|
|
129
|
+
s.session.armTools("checked-thread", "checked-native-turn");
|
|
130
|
+
const runtime = escalationRuntime(spec.markers!.nodeId, turnId); assert.ok(runtime);
|
|
131
|
+
assert.equal(runtime.snapshot().native_thread, "checked-thread");
|
|
132
|
+
assert.equal(runtime.snapshot().native_turn, "checked-native-turn");
|
|
133
|
+
assert.equal(runtime.snapshot().overlay_budget_s, 3600);
|
|
134
|
+
s.session.disarmTools();
|
|
135
|
+
const failures: string[] = [];
|
|
136
|
+
s.session.watch((why) => failures.push(why));
|
|
137
|
+
fatal!("peer checker failed"); fatal!("again");
|
|
138
|
+
assert.equal(failures.length, 1);
|
|
139
|
+
assert.match(failures[0], /peer checker failed/);
|
|
140
|
+
await s.session.teardown(); await s.session.teardown();
|
|
141
|
+
assert.equal(escalationRuntime(spec.markers!.nodeId, turnId), undefined, "the trusted worker unregisters after teardown");
|
|
142
|
+
assert.ok(order.indexOf("executor-stop") < order.indexOf("broker-close"));
|
|
143
|
+
assert.ok(order.indexOf("broker-close") < order.indexOf("release"));
|
|
144
|
+
assert.equal(order.filter((x) => x === "broker-close").length, 1);
|
|
145
|
+
order.length = 0; launchFails = true;
|
|
146
|
+
await assert.rejects(openStage2(spec, { env: {}, servers: [] }, dir), /executor failed/);
|
|
147
|
+
assert.deepEqual(order, ["admission", "lease", "snapshot", "broker-open", "executor", "disarm", "broker-close", "release"]);
|
|
148
|
+
order.length = 0; launchFails = false;
|
|
149
|
+
snapshotFails = true;
|
|
150
|
+
await assert.rejects(openStage2(spec, { env: {}, servers: [] }, dir), /snapshot collision/);
|
|
151
|
+
assert.deepEqual(order, ["admission", "lease", "snapshot", "release"], "no broker or executor starts after a snapshot collision");
|
|
152
|
+
order.length = 0; snapshotFails = false; brokerFails = true;
|
|
153
|
+
await assert.rejects(openStage2(spec, { env: {}, servers: [] }, dir), /broker failed/);
|
|
154
|
+
assert.deepEqual(order, ["admission", "lease", "snapshot", "broker-open", "release"], "a failed broker cannot start an executor");
|
|
155
|
+
order.length = 0; brokerFails = false;
|
|
156
|
+
const noBroker = await openStage2({ ...spec, relay: null }, { env: {}, servers: [] }, dir);
|
|
157
|
+
assert.equal(noBroker.plan.broker, undefined);
|
|
158
|
+
assert.equal(noBroker.session.env.RELAY_BROKER_TOKEN, undefined);
|
|
159
|
+
assert.ok(!order.includes("broker-open"));
|
|
160
|
+
await noBroker.session.teardown({ survivors: [98765], descendants: [{ pid: 98765, start: "pinned-app-descendant" }] });
|
|
161
|
+
assert.match(stoppedOptions.quarantine, /app-server descendants/);
|
|
162
|
+
assert.deepEqual(stoppedOptions.appDescendants, [{ pid: 98765, start: "pinned-app-descendant" }], "survivor identities must remain durable for reap, not just their numeric pids");
|
|
163
|
+
|
|
164
|
+
// The fake admission above isolates the executor/provider seam; production routing and
|
|
165
|
+
// admission are exercised by stage2-network-decisions.test.ts.
|
|
166
|
+
setMediatedNetworkProviderForTests(undefined);
|
|
167
|
+
const mediatedSpec = { ...spec, relay: null, grant: { network: true }, neverPolicy: checkpointNever(spec.markers!.runId, "launch") };
|
|
168
|
+
order.length = 0;
|
|
169
|
+
await assert.rejects(openStage2(mediatedSpec, { env: {}, servers: [] }, dir), /no qualified mediated provider is available/);
|
|
170
|
+
assert.ok(!order.includes("executor"));
|
|
171
|
+
let unsafe = true;
|
|
172
|
+
setMediatedNetworkProviderForTests({
|
|
173
|
+
qualify: () => null,
|
|
174
|
+
async prepare(context) {
|
|
175
|
+
order.push("mediated-prepare");
|
|
176
|
+
assert.equal(context.baseline?.network, true);
|
|
177
|
+
return { network: { ...context.network, egress: unsafe ? "raw" as const : "none" as const, connectPorts: [49234] },
|
|
178
|
+
executorEnv: { STUB_MEDIATED_EXECUTOR: "1" }, appEnv: { STUB_MEDIATED_APP: "1" },
|
|
179
|
+
async close() { order.push("mediated-close"); },
|
|
180
|
+
};
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
order.length = 0;
|
|
184
|
+
await assert.rejects(openStage2(mediatedSpec, { env: {}, servers: [] }, dir), /must not substitute raw network/);
|
|
185
|
+
assert.ok(!order.includes("executor")); assert.ok(order.includes("mediated-close"));
|
|
186
|
+
unsafe = false; order.length = 0;
|
|
187
|
+
const mediated = await openStage2(mediatedSpec, { env: {}, servers: [] }, dir);
|
|
188
|
+
assert.equal(compiledNetwork.egress, "none"); assert.deepEqual(compiledNetwork.connectPorts, [49234]);
|
|
189
|
+
assert.equal(launchedEnvironment.STUB_MEDIATED_EXECUTOR, "1");
|
|
190
|
+
assert.equal(mediated.session.env.STUB_MEDIATED_APP, "1");
|
|
191
|
+
assert.equal(mediated.session.env.CODEX_EXEC_SERVER_URL, "none");
|
|
192
|
+
await mediated.session.teardown();
|
|
193
|
+
assert.ok(order.indexOf("executor-stop") < order.indexOf("mediated-close"));
|
|
194
|
+
assert.ok(order.indexOf("mediated-close") < order.indexOf("release"));
|
|
195
|
+
// Exercise the built-in provider with a fake listener, including child_network and resume.
|
|
196
|
+
for (const child_network of [false, true]) for (const sessionId of [undefined, "kept-thread"]) for (const access of ["read", "write"]) {
|
|
197
|
+
let closed = 0;
|
|
198
|
+
setMediatedNetworkProviderForTests(createMediatedNetworkProvider({ open: async options => {
|
|
199
|
+
assert.deepEqual(options.listen, { port: 47206, ipv4Only: true });
|
|
200
|
+
return { port: 47206, close: async () => { closed++; } };
|
|
201
|
+
} }));
|
|
202
|
+
const notes: string[] = [];
|
|
203
|
+
const turn = { ...mediatedSpec, access, sessionId, config: { ...mediatedSpec.config, child_network }, ...(child_network ? { grant: undefined } : {}), onNote: note => notes.push(note) };
|
|
204
|
+
const standing = await openStage2(turn, { env: {}, servers: [] }, dir);
|
|
205
|
+
assert.equal(proxyLeased, true);
|
|
206
|
+
assert.equal(compiledNetwork.egress, "none"); assert.equal(compiledNetwork.mediatedPort, 47206);
|
|
207
|
+
assert.deepEqual(compiledNetwork.bindPorts, [], `${sessionId ? "resumed" : "fresh"} mediated ${access} turn cannot bind its reserved block`);
|
|
208
|
+
for (const port of [47200, 47201, 47202, 47203]) assert.ok(compiledNetwork.relayRange.includes(port), `closed block port ${port} remains in the final reserved-range deny`);
|
|
209
|
+
assert.equal(launchedEnvironment.PORT, undefined); assert.equal(launchedEnvironment.PORTS, undefined, "closed blocks are not advertised to commands");
|
|
210
|
+
for (const key of ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"]) assert.equal(launchedEnvironment[key], "http://127.0.0.1:47206");
|
|
211
|
+
assert.equal(standing.session.env.HTTP_PROXY, undefined);
|
|
212
|
+
assert.match(notes.join(" "), /No Go\/Swift TLS.*Release B/);
|
|
213
|
+
assert.match(notes.join(" "), /ignore proxy variables fail closed/);
|
|
214
|
+
await standing.session.teardown(); assert.equal(closed, 1);
|
|
215
|
+
}
|
|
216
|
+
setMediatedNetworkProviderForTests(undefined);
|
|
217
|
+
});
|
|
218
|
+
} finally { setMediatedNetworkProviderForTests(undefined); setStage2DepsForTests(null); fs.rmSync(dir, { recursive: true, force: true }); }
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
for (const mode of ["normal", "fatal", "self-start", "readback-failure", "resume", "provider-cleanup-failure"] as const) {
|
|
222
|
+
test(`BB: app lifecycle ${mode}: bind before initialize, arm before readback, kill before fail, reap before close`, async (t) => {
|
|
223
|
+
const dir = scratch(), events: string[] = [];
|
|
224
|
+
let fail!: (why: string) => void;
|
|
225
|
+
let appPid = 0, armed = false;
|
|
226
|
+
const eventsFile = path.join(dir, "events");
|
|
227
|
+
const bin = path.join(dir, "fake-app");
|
|
228
|
+
fs.writeFileSync(bin, `#!${process.execPath}\n` + fs.readFileSync(path.join(import.meta.dirname, "fixtures", "stage2-app-server.mjs"), "utf8"), { mode: 0o755 });
|
|
229
|
+
const env = Object.freeze({ PATH: process.env.PATH, BB_EVENTS: eventsFile, BB_CONFIG: JSON.stringify({ default_permissions: ":danger-full-access", approvals_reviewer: "user" }), ...(mode === "self-start" ? { BB_SELF_START: "1" } : {}) });
|
|
230
|
+
const kill = process.kill.bind(process);
|
|
231
|
+
t.mock.method(process, "kill", (pid: number, sig?: NodeJS.Signals | number) => {
|
|
232
|
+
if (pid === -appPid && sig === "SIGKILL") events.push("kill");
|
|
233
|
+
return kill(pid, sig);
|
|
234
|
+
});
|
|
235
|
+
const session = {
|
|
236
|
+
env, guard: new Stage2TurnGuard(),
|
|
237
|
+
bindAppServer(pid: number) { appPid = pid; events.push("bind-app"); assert.ok(pid > 1); assert.ok(!fs.existsSync(eventsFile)); },
|
|
238
|
+
bind: async () => null, readback: async () => null,
|
|
239
|
+
threadParams: () => ({}), resumeProblem: async () => null, threadProblem: () => null,
|
|
240
|
+
sessionsServed() { events.push("checkpoint"); assert.ok(!/thread\/(start|resume)/.test(fs.readFileSync(eventsFile, "utf8"))); return 7; },
|
|
241
|
+
threadMcpProblem: async (_rpc: unknown, _eff: unknown, _id: string, checkpoint: number) => {
|
|
242
|
+
assert.equal(checkpoint, 7);
|
|
243
|
+
assert.ok(fs.readFileSync(eventsFile, "utf8").includes(mode === "resume" ? "thread/resume" : "thread/start"));
|
|
244
|
+
events.push("thread-mcp"); return null;
|
|
245
|
+
},
|
|
246
|
+
turnParams: () => ({}),
|
|
247
|
+
armTools() { armed = true; events.push("arm"); },
|
|
248
|
+
disarmTools() { armed = false; events.push("disarm"); },
|
|
249
|
+
async turnProblem(rpc: any) {
|
|
250
|
+
events.push("readback"); assert.ok(armed, "the first tool call while readback waits must already be permitted");
|
|
251
|
+
if (mode === "fatal") { fail("peer checker failed"); return null; }
|
|
252
|
+
if (mode === "readback-failure") return "bad environments";
|
|
253
|
+
await rpc.request("bb/complete", {}); return null;
|
|
254
|
+
},
|
|
255
|
+
approvalProblem: () => null, approvalDetail: () => ({}),
|
|
256
|
+
watch(f: typeof fail) { fail = f; },
|
|
257
|
+
async teardown() {
|
|
258
|
+
assert.ok(gone(appPid), "the app server must be reaped before broker closure"); assert.ok(!armed); events.push("broker-close");
|
|
259
|
+
if (mode === "provider-cleanup-failure") throw new Error("provider cleanup uncertain");
|
|
260
|
+
return { survivors: [], warnings: [] };
|
|
261
|
+
},
|
|
262
|
+
};
|
|
263
|
+
try {
|
|
264
|
+
const spec = specFor(dir, bin, { ...(mode === "resume" ? { sessionId: "thread" } : {}) });
|
|
265
|
+
const handle = startCodexAppTurn(spec, { off: [], c: [], expect: { permissions: stage2Permissions(true), relay: null, relayOnly: false, accountLogin: false } }, env, session);
|
|
266
|
+
const result = await handle.done;
|
|
267
|
+
assert.equal(result.status, ["normal", "resume", "provider-cleanup-failure"].includes(mode) ? "ok" : "failed", result.error);
|
|
268
|
+
if (mode === "provider-cleanup-failure") {
|
|
269
|
+
assert.ok(result.unseenMs !== undefined, "uncertain provider teardown cannot be accounted as verified cleanup");
|
|
270
|
+
assert.match(result.warnings?.join(" ") ?? "", /provider cleanup uncertain/);
|
|
271
|
+
}
|
|
272
|
+
assert.equal(events[0], "bind-app");
|
|
273
|
+
assert.equal(events.at(-1), "broker-close");
|
|
274
|
+
if (mode === "self-start") { assert.ok(!events.includes("arm")); assert.match(result.error ?? "", /started a turn by itself/); }
|
|
275
|
+
else assert.ok(events.indexOf("arm") < events.indexOf("readback"));
|
|
276
|
+
if (["fatal", "self-start", "readback-failure"].includes(mode)) {
|
|
277
|
+
assert.ok(events.indexOf("kill") >= 0);
|
|
278
|
+
assert.ok(events.indexOf("disarm") < events.indexOf("kill"), JSON.stringify(events));
|
|
279
|
+
}
|
|
280
|
+
} finally { t.mock.restoreAll(); fs.rmSync(dir, { recursive: true, force: true }); }
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// WP-C (docs/codex-confinement-design.md §13.2): the executor, its token, its teardown and the
|
|
285
|
+
// sandbox_check sweep, through the real packages: D's admission and plan, A's lease and network
|
|
286
|
+
// rules, B's stage-2 probes (prepareLaunch). Only what comes from outside relay is replaced: the
|
|
287
|
+
// executor binary (a fake that answers --version as Codex 0.164.0), this Mac's addresses
|
|
288
|
+
// (setInterfaceReaderForTests) and the port range (setLeaseOptionsForTests: a scratch range, since
|
|
289
|
+
// leases are kept per relay home and other relay processes run beside the tests). Live cases with
|
|
290
|
+
// Codex itself and a fake model are in stage2-executor-live.test.ts.
|
|
291
|
+
|
|
292
|
+
const darwin = process.platform === "darwin";
|
|
293
|
+
const gone = (pid: number) => {
|
|
294
|
+
try {
|
|
295
|
+
process.kill(pid, 0);
|
|
296
|
+
return false;
|
|
297
|
+
} catch (err) {
|
|
298
|
+
return (err as NodeJS.ErrnoException).code === "ESRCH";
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
302
|
+
|
|
303
|
+
/** Codex 0.164 fixture: version, empty mcp list, then executor startup; failures apply only to their named operation. */
|
|
304
|
+
function fakeExecutor(dir: string, o: { name?: string; mcpExit?: number; execExit?: number } = {}): string {
|
|
305
|
+
const file = path.join(dir, o.name ?? "fake-exec-server");
|
|
306
|
+
fs.writeFileSync(file, `#!${process.execPath}
|
|
307
|
+
const net = await import("node:net");
|
|
308
|
+
const { spawn } = await import("node:child_process");
|
|
309
|
+
const fs = await import("node:fs");
|
|
310
|
+
if (process.argv[2] === "--version") { console.log("codex-cli 0.164.0"); process.exit(0); }
|
|
311
|
+
const args = process.argv.slice(2);
|
|
312
|
+
if (args.includes("mcp") && args[args.indexOf("mcp") + 1] === "list" && args.includes("--json")) {
|
|
313
|
+
// Codex 0.164 cli/src/mcp_cmd.rs run_list serializes a top-level array, [] without servers.
|
|
314
|
+
if (${o.mcpExit ?? 0}) process.exit(${o.mcpExit ?? 0});
|
|
315
|
+
console.log("[]"); process.exit(0);
|
|
316
|
+
}
|
|
317
|
+
if (${o.execExit !== undefined}) process.exit(${o.execExit ?? 0});
|
|
318
|
+
const url = process.argv[process.argv.indexOf("--listen") + 1];
|
|
319
|
+
const port = Number(/:(\\d+)$/.exec(url)[1]);
|
|
320
|
+
const s = net.createServer((c) => { c.on("error", () => {}); c.end(); });
|
|
321
|
+
s.listen(port, "127.0.0.1", () => {
|
|
322
|
+
console.log(url);
|
|
323
|
+
if (process.env.FAKE_EXEC_CHILDREN) {
|
|
324
|
+
// One child in the group, one that leaves the session and the tree at once (double fork + setsid).
|
|
325
|
+
spawn("/bin/sleep", ["4291"], { stdio: "ignore" });
|
|
326
|
+
const p = spawn("/usr/bin/perl", ["-MPOSIX", "-e", "exit 0 if fork; POSIX::setsid(); open(F, '>', $ARGV[0]); print F $$; close F; exec '/bin/sleep', '4292'", process.env.FAKE_EXEC_CHILDREN], { stdio: "ignore" });
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
`, { mode: 0o755 });
|
|
330
|
+
return file;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
test("executor fixture: mcp list --json returns Codex's empty top-level array before executor startup", () => {
|
|
334
|
+
const dir = scratch();
|
|
335
|
+
try {
|
|
336
|
+
for (const bin of [fakeExecutor(dir), fakeExecutor(dir, { name: "exits", execExit: 3 })]) {
|
|
337
|
+
for (const args of [["mcp", "list", "--json"], ["--disable", "plugins", "mcp", "list", "--json"]]) {
|
|
338
|
+
const listed = spawnSync(bin, args, { encoding: "utf8", env: { PATH: "/usr/bin:/bin" } });
|
|
339
|
+
assert.equal(listed.status, 0, listed.stderr);
|
|
340
|
+
assert.deepEqual(JSON.parse(listed.stdout), []);
|
|
341
|
+
}
|
|
342
|
+
assert.equal(stage2Admission(specFor(dir, bin), { PATH: "/usr/bin:/bin" }, { interlock: () => null, aliases: () => null }), null,
|
|
343
|
+
"real version, consent and MCP admission probes pass even when executor startup will fail");
|
|
344
|
+
}
|
|
345
|
+
assert.equal(spawnSync(path.join(dir, "exits"), ["exec-server"], { stdio: "ignore" }).status, 3);
|
|
346
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }); }
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
test("executor fixture: a failing mcp list refuses real admission before any executor starts", () => {
|
|
350
|
+
const dir = scratch();
|
|
351
|
+
try {
|
|
352
|
+
const bin = fakeExecutor(dir, { mcpExit: 7 });
|
|
353
|
+
const refusal = stage2Admission(specFor(dir, bin), { PATH: "/usr/bin:/bin" }, { interlock: () => null, aliases: () => null });
|
|
354
|
+
assert.match(refusal ?? "", /could not read the person's MCP server names \(codex mcp list exited with 7\).*cannot rule out a relay_tools entry/);
|
|
355
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }); }
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
/** The registry's run for this file's jobs (the scratch relay home of helpers.ts), made on first use. */
|
|
359
|
+
let runId: string | null = null;
|
|
360
|
+
let nodes = 0;
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* A write turn granted raw network, for a job registered in the registry with the person's consent
|
|
364
|
+
* stored for exactly its grant, as session.ts records the person's answer (review R2: admission reads
|
|
365
|
+
* it). `consent`: "none" leaves it out, "other" stores one for another grant.
|
|
366
|
+
*/
|
|
367
|
+
function specFor(dir: string, bin: string, extra: Partial<TurnSpec> = {}, o: { consent?: "none" | "other" } = {}): TurnSpec {
|
|
368
|
+
const cwd = path.join(dir, "w");
|
|
369
|
+
fs.mkdirSync(cwd, { recursive: true });
|
|
370
|
+
runId ??= createRun(dir, withDefaults({}), "s2c-unit");
|
|
371
|
+
const nodeId = `j_s2c${String(++nodes).padStart(4, "0")}`;
|
|
372
|
+
insertNode({ id: nodeId, run_id: runId, kind: "job", target: "codex", mode: "write", depth: 1, task: "stage-2 unit", cwd, status: "running" });
|
|
373
|
+
const spec = {
|
|
374
|
+
target: targetDef("codex"), prompt: "x", cwd, dir, logPath: path.join(dir, "agent.log"), access: "write", relay: null,
|
|
375
|
+
config: withDefaults({ targets: { codex: { bin, stage2: true } } }), markers: { runId, nodeId, depth: 1 },
|
|
376
|
+
environment: { PATH: "/usr/bin:/bin" }, grant: { network: true, raw_network: true } as TurnSpec["grant"], ...extra,
|
|
377
|
+
} as TurnSpec;
|
|
378
|
+
if (o.consent !== "none") {
|
|
379
|
+
const consented = o.consent === "other" ? { ...spec.grant, write_paths: ["/elsewhere"] } : spec.grant;
|
|
380
|
+
setParam(nodeId, "raw_network_consent", { grant: rawNetworkKey(consented as never), by: "by the user", at: Date.now() });
|
|
381
|
+
}
|
|
382
|
+
return spec;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** A scratch lease range of 16 high ports nothing listens on, so these tests' leases never meet another relay process's executors. */
|
|
386
|
+
function scratchRange(): { first: number; count: number } {
|
|
387
|
+
for (let i = 0; i < 50; i++) {
|
|
388
|
+
const first = 50000 + 16 * Math.floor(Math.random() * 600);
|
|
389
|
+
const all = Array.from({ length: 16 }, (_, k) => first + k);
|
|
390
|
+
if (!busyPorts(all).size) return { first, count: 16 };
|
|
391
|
+
}
|
|
392
|
+
throw new Error("no free scratch range");
|
|
393
|
+
}
|
|
394
|
+
/** The ports of the scratch range whose lease is held now (A's lease directory; leases change hands by generation, R6). */
|
|
395
|
+
let range = { first: 0, count: 0 };
|
|
396
|
+
const leased = (): number[] => rangePorts(range).filter((p) => portHeld(p, { dir: path.join(relayHome(), "ports") }));
|
|
397
|
+
/** A fresh scratch range for the next openStage2 calls of this test. */
|
|
398
|
+
const useScratchRange = () => setLeaseOptionsForTests({ range: (range = scratchRange()) });
|
|
399
|
+
/** The executor this test process started: its pid, its port and its argv and environment. */
|
|
400
|
+
function ourExecutor(bin: string): { pid: number; port: number; ps: string } {
|
|
401
|
+
const pids = spawnSync("/usr/bin/pgrep", ["-P", String(process.pid), "-f", `${bin} exec-server --listen`], { encoding: "utf8" }).stdout.trim().split("\n").filter(Boolean).map(Number);
|
|
402
|
+
assert.equal(pids.length, 1, `one executor: ${pids}`);
|
|
403
|
+
const ps = execFileSync("/bin/ps", ["eww", "-o", "command=", "-p", String(pids[0])], { encoding: "utf8" });
|
|
404
|
+
return { pid: pids[0], port: Number(/--listen ws:\/\/127\.0\.0\.1:(\d+) /.exec(ps)![1]), ps };
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const scratch = () => fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "s2c-")));
|
|
408
|
+
|
|
409
|
+
test("executorToken: 32 random bytes, its SHA-256 for argv, registered as a turn secret", () => {
|
|
410
|
+
withTurnSecrets(() => {
|
|
411
|
+
const a = executorToken(), b = executorToken();
|
|
412
|
+
assert.match(a.token, /^[0-9a-f]{64}$/);
|
|
413
|
+
assert.notEqual(a.token, b.token);
|
|
414
|
+
assert.equal(a.hash, crypto.createHash("sha256").update(a.token).digest("hex"));
|
|
415
|
+
assert.equal(redact(`error: Bearer ${a.token}`), "error: Bearer [redacted: executor token]");
|
|
416
|
+
assert.equal(redact(a.hash), a.hash, "the hash is not secret");
|
|
417
|
+
});
|
|
418
|
+
assert.throws(() => executorToken(), /redaction scope/, "outside a turn there is nowhere to register it, so nothing is made");
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
test("executorEnv: PORT and PORTS are advertised only when the block is bindable", () => {
|
|
422
|
+
const agent = { PATH: "/usr/bin", CODEX_HOME: "/x/.codex", CODEX_EXEC_SERVER_URL: "ws://x", RELAY_RUN_ID: "r" };
|
|
423
|
+
const ports = { block: [50001, 50002, 50003, 50004] };
|
|
424
|
+
assert.deepEqual(executorEnv(agent, "/t/turn", ports, true), { PATH: "/usr/bin", RELAY_RUN_ID: "r", CODEX_HOME: "/t/turn/codex-exec-home", TMPDIR: "/t/turn", PORT: "50001", PORTS: "50001,50002,50003,50004" });
|
|
425
|
+
assert.deepEqual(executorEnv(agent, "/t/turn", ports, false), { PATH: "/usr/bin", RELAY_RUN_ID: "r", CODEX_HOME: "/t/turn/codex-exec-home", TMPDIR: "/t/turn" });
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
test("executorPolicy: the turn's policy with the stage-2 network, ~/.codex, the app server's CODEX_HOME and the credential stores denied", () => {
|
|
429
|
+
const dir = scratch();
|
|
430
|
+
const tmpDir = privateTurnTemp(ensurePrivateRoot(nativeTempRoot()), "");
|
|
431
|
+
try {
|
|
432
|
+
const spec = specFor(dir, "/bin/true", { environment: { PATH: "/usr/bin", CODEX_HOME: path.join(dir, "apphome") } });
|
|
433
|
+
const n: Stage2Network = { mode: "stage2", deniedTcp: [22], deniedUdp: [1900], relayRange: [50000], execPort: 50000, bindPorts: [50001], worktreeSockets: false };
|
|
434
|
+
const p = executorPolicy(spec, tmpDir, n);
|
|
435
|
+
assert.equal(p.network, n);
|
|
436
|
+
assert.equal(p.tmpDir, tmpDir);
|
|
437
|
+
assert.equal(p.worktree, spec.cwd);
|
|
438
|
+
assert.equal(p.worktreeWritable, true);
|
|
439
|
+
assert.equal(p.state?.home, "real", "commands see the real HOME, read-only, as under Codex's own sandbox");
|
|
440
|
+
assert.equal(p.keychain, false);
|
|
441
|
+
assert.ok(!p.certificateTrust);
|
|
442
|
+
assert.deepEqual(p.stateDirs, [path.join(tmpDir, "codex-exec-home")]);
|
|
443
|
+
assert.ok(fs.statSync(path.join(tmpDir, "codex-exec-home")).isDirectory());
|
|
444
|
+
for (const d of [".codex", ".ssh", ".aws", ".gnupg", ".netrc"]) assert.ok(p.denied.includes(path.join(os.homedir(), d)), d);
|
|
445
|
+
assert.ok(p.denied.includes(path.join(dir, "apphome")));
|
|
446
|
+
assert.equal(executorPolicy({ ...spec, access: "read" }, tmpDir, n).worktreeWritable, false);
|
|
447
|
+
} finally {
|
|
448
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
449
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
test("sbcheck: the helper is built, passes its known-answer test, and answers per path", { skip: !darwin }, async () => {
|
|
454
|
+
assert.equal(sandboxCheckUnavailable(), null, `helper at ${sandboxCheckHelper()}`);
|
|
455
|
+
const dir = scratch();
|
|
456
|
+
const a = path.join(dir, "a"), d = path.join(dir, "d");
|
|
457
|
+
fs.mkdirSync(a);
|
|
458
|
+
fs.mkdirSync(d);
|
|
459
|
+
const child = spawn("/usr/bin/sandbox-exec", ["-p", `(version 1)(allow default)(deny file-write* (subpath "${d}"))`, "/bin/sleep", "30"], { stdio: "ignore" });
|
|
460
|
+
try {
|
|
461
|
+
for (let i = 0; i < 40 && sandboxAllows(child.pid!, "file-write-data", d) !== "deny"; i++) await sleep(50);
|
|
462
|
+
assert.equal(sandboxAllows(child.pid!, "file-write-data", a), "allow");
|
|
463
|
+
assert.equal(sandboxAllows(child.pid!, "file-write-data", d), "deny");
|
|
464
|
+
assert.equal(sandboxAllows(process.pid, "file-write-data", d), "allow", "an unsandboxed process may write anything");
|
|
465
|
+
const r = sandboxReports("file-write-data", [a, d], [child.pid!, process.pid]);
|
|
466
|
+
assert.ok(typeof r !== "string");
|
|
467
|
+
assert.deepEqual(r.get(child.pid!), { sandboxed: true, answers: ["allow", "deny"] });
|
|
468
|
+
assert.deepEqual(r.get(process.pid), { sandboxed: false, answers: ["allow", "allow"] });
|
|
469
|
+
assert.match(String(sandboxReports("file-write-data", ["relative"], [1])), /absolute/);
|
|
470
|
+
} finally {
|
|
471
|
+
child.kill("SIGKILL");
|
|
472
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
test("sbcheck: relay runs only its own package's helper, and only while it is the file, with the contents, relay recorded at its start (review r_0c11d860 X1)", { skip: !darwin }, () => {
|
|
477
|
+
// The running package's, whichever worktree the tests run from.
|
|
478
|
+
assert.equal(sandboxCheckHelper(), path.join(path.resolve(import.meta.dirname, ".."), "dist", "native", "relay-sbcheck"));
|
|
479
|
+
const dir = scratch();
|
|
480
|
+
try {
|
|
481
|
+
const copy = path.join(dir, "relay-sbcheck");
|
|
482
|
+
fs.copyFileSync(sandboxCheckHelper(), copy);
|
|
483
|
+
fs.chmodSync(copy, 0o755);
|
|
484
|
+
setSandboxCheckHelperForTests(copy);
|
|
485
|
+
assert.equal(sandboxCheckUnavailable(), null, "a recorded, unchanged helper passes its known-answer test");
|
|
486
|
+
assert.ok(typeof sandboxReports("file-write-data", [dir], [process.pid]) !== "string");
|
|
487
|
+
// Changed in place after relay recorded it: refused before every run, also after the self-test passed.
|
|
488
|
+
fs.appendFileSync(copy, "\0");
|
|
489
|
+
assert.match(sandboxCheckUnavailable() ?? "", /changed since relay started/);
|
|
490
|
+
assert.match(String(sandboxReports("file-write-data", [dir], [process.pid])), /changed since relay started/);
|
|
491
|
+
assert.match(sandboxAllows(process.pid, "file-write-data", dir), /^error: .*changed since relay started/);
|
|
492
|
+
// Replaced by another file of the same contents: a new identity, refused too.
|
|
493
|
+
const other = path.join(dir, "other");
|
|
494
|
+
setSandboxCheckHelperForTests(copy);
|
|
495
|
+
fs.copyFileSync(copy, other);
|
|
496
|
+
fs.chmodSync(other, 0o755);
|
|
497
|
+
fs.renameSync(other, copy);
|
|
498
|
+
assert.match(String(sandboxReports("file-write-data", [dir], [process.pid])), /replaced or changed since relay started/);
|
|
499
|
+
} finally {
|
|
500
|
+
setSandboxCheckHelperForTests(null);
|
|
501
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
test("sbcheck: a missing or unsafe helper is reported, never run", { skip: !darwin }, () => {
|
|
506
|
+
const dir = scratch();
|
|
507
|
+
try {
|
|
508
|
+
setSandboxCheckHelperForTests(path.join(dir, "missing"));
|
|
509
|
+
assert.match(sandboxCheckUnavailable() ?? "", /is not there/);
|
|
510
|
+
assert.match(sandboxAllows(process.pid, "file-write-data", dir), /^error: .*is not there/);
|
|
511
|
+
const writable = path.join(dir, "writable");
|
|
512
|
+
fs.copyFileSync(path.join(import.meta.dirname, "..", "dist", "native", "relay-sbcheck"), writable);
|
|
513
|
+
fs.chmodSync(writable, 0o777);
|
|
514
|
+
setSandboxCheckHelperForTests(writable);
|
|
515
|
+
assert.match(sandboxCheckUnavailable() ?? "", /writable by others/);
|
|
516
|
+
setSandboxCheckHelperForTests(path.join(dir));
|
|
517
|
+
assert.match(sandboxCheckUnavailable() ?? "", /not a regular file/);
|
|
518
|
+
} finally {
|
|
519
|
+
setSandboxCheckHelperForTests(null);
|
|
520
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
test("sweepTurnSurvivors: finds this turn's detached processes by their sandbox, and nothing else", { skip: !darwin }, async () => {
|
|
525
|
+
const dir = scratch();
|
|
526
|
+
const tmpDir = path.join(dir, "turn"), other = path.join(dir, "other");
|
|
527
|
+
fs.mkdirSync(tmpDir);
|
|
528
|
+
fs.mkdirSync(other);
|
|
529
|
+
// Each one detached (double fork, setsid), so it is in no tree of this test's, as a survivor would be.
|
|
530
|
+
const detached = (prefix: string[], tag: string) => {
|
|
531
|
+
const pidFile = path.join(dir, `${tag}.pid`);
|
|
532
|
+
spawnSync("/usr/bin/perl", ["-MPOSIX", "-e", "exit 0 if fork; POSIX::setsid(); open(F, '>', shift @ARGV); print F $$; close F; exec @ARGV", pidFile, ...prefix, "/bin/sleep", "4290"], { stdio: "ignore" });
|
|
533
|
+
for (let i = 0; i < 100 && !fs.existsSync(pidFile); i++) execFileSync("/bin/sleep", ["0.05"]);
|
|
534
|
+
return Number(fs.readFileSync(pidFile, "utf8"));
|
|
535
|
+
};
|
|
536
|
+
const turnProfile = `(version 1)(allow default)(deny file-write* (subpath "${dir}"))(allow file-write* (subpath "${tmpDir}"))`;
|
|
537
|
+
const otherProfile = `(version 1)(allow default)(deny file-write* (subpath "${dir}"))(allow file-write* (subpath "${other}"))`;
|
|
538
|
+
const pids: number[] = [];
|
|
539
|
+
try {
|
|
540
|
+
const ours = detached(["/usr/bin/sandbox-exec", "-p", turnProfile], "ours");
|
|
541
|
+
const otherTurn = detached(["/usr/bin/sandbox-exec", "-p", otherProfile], "other");
|
|
542
|
+
const broad = detached(["/usr/bin/sandbox-exec", "-p", "(version 1)(allow default)"], "broad");
|
|
543
|
+
const plain = detached([], "plain");
|
|
544
|
+
pids.push(ours, otherTurn, broad, plain);
|
|
545
|
+
await sleep(300);
|
|
546
|
+
const found = sweepTurnSurvivors(tmpDir).map((p) => p.pid);
|
|
547
|
+
assert.deepEqual(found, [ours], JSON.stringify({ ours, otherTurn, broad, plain, found }));
|
|
548
|
+
} finally {
|
|
549
|
+
for (const p of pids) try { process.kill(p, "SIGKILL"); } catch { /* gone */ }
|
|
550
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
551
|
+
}
|
|
552
|
+
for (const p of pids) assert.ok(gone(p) || (await sleep(200), gone(p)), `pid ${p} stopped`);
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
test("openStage2: executor sandboxed on its leased port with the token's hash only; bind, hooks, teardown sweep, ports released", { skip: !darwin }, async () => {
|
|
556
|
+
const dir = scratch();
|
|
557
|
+
const tmpDir = privateTurnTemp(ensurePrivateRoot(nativeTempRoot()), "");
|
|
558
|
+
useScratchRange();
|
|
559
|
+
const bin = fakeExecutor(dir);
|
|
560
|
+
const detachedPid = path.join(dir, "w", "detached.pid");
|
|
561
|
+
const spec = specFor(dir, bin, { environment: { PATH: "/usr/bin:/bin", FAKE_EXEC_CHILDREN: detachedPid } });
|
|
562
|
+
const prepared = { PATH: "/usr/bin:/bin", FAKE_EXEC_CHILDREN: detachedPid, CODEX_HOME: path.join(dir, "apphome") };
|
|
563
|
+
let executorPid = 0, stray = 0;
|
|
564
|
+
let s2: Awaited<ReturnType<typeof openStage2>> | undefined;
|
|
565
|
+
try {
|
|
566
|
+
await withTurnSecrets(async () => {
|
|
567
|
+
s2 = await openStage2(spec, { env: prepared, servers: [] }, tmpDir);
|
|
568
|
+
if (!s2) throw new Error("no launch");
|
|
569
|
+
assert.equal(s2.plan.permissions.enforcement, "relay");
|
|
570
|
+
// The app server gets exactly what admission checked, a frozen copy of the agent's environment,
|
|
571
|
+
// with relay's own CODEX_EXEC_SERVER_URL=none over it: Codex registers no `local` environment
|
|
572
|
+
// (review r_0c11d860 C1+X2). The executor's environment never carries it.
|
|
573
|
+
assert.ok(Object.isFrozen(s2.session.env));
|
|
574
|
+
assert.deepEqual({ ...s2.session.env }, { ...prepared, CODEX_EXEC_SERVER_URL: "none" });
|
|
575
|
+
// The executor: whose argv carries the hash, never the token, on a port of the lease with the job's block after it.
|
|
576
|
+
const ex = ourExecutor(bin);
|
|
577
|
+
executorPid = ex.pid;
|
|
578
|
+
const ps = ex.ps;
|
|
579
|
+
assert.ok(ex.port >= range.first && ex.port < range.first + range.count, `${ex.port} in ${JSON.stringify(range)}`);
|
|
580
|
+
assert.match(ps, /--ws-auth capability-token --ws-token-sha256 [0-9a-f]{64}/);
|
|
581
|
+
const block = /PORTS=([\d,]+)/.exec(ps)![1].split(",").map(Number);
|
|
582
|
+
assert.equal(block.length, 4);
|
|
583
|
+
assert.ok(block.every((p, i) => p >= range.first && p < range.first + range.count && (i === 0 || p === block[i - 1] + 1) && p !== ex.port), JSON.stringify(block));
|
|
584
|
+
assert.match(ps, new RegExp(`PORT=${block[0]} `));
|
|
585
|
+
assert.equal(leased().length, 6, "executor, broker and block are leased");
|
|
586
|
+
assert.ok([ex.port, ...block].every((p) => leased().includes(p)));
|
|
587
|
+
// Its record on disk (core/stage2-executors.ts), for whoever must stop it should this process die.
|
|
588
|
+
const recs = executorRecords().records.map((r) => r.record);
|
|
589
|
+
assert.equal(recs.length, 1);
|
|
590
|
+
assert.deepEqual({ pid: recs[0].pid, owner: recs[0].owner.pid, nodeId: recs[0].nodeId, ports: recs[0].ports, tmpDir: recs[0].tmpDir }, { pid: executorPid, owner: process.pid, nodeId: spec.markers.nodeId, ports: { exec: ex.port, block }, tmpDir });
|
|
591
|
+
assert.match(ps, new RegExp(`CODEX_HOME=${tmpDir}/codex-exec-home`));
|
|
592
|
+
assert.doesNotMatch(ps, /CODEX_EXEC_SERVER_URL/, "the executor is not told about any other executor, relay's \"none\" included");
|
|
593
|
+
assert.doesNotMatch(redact(ps), /redacted: executor token/, "the token is in neither argv nor environment");
|
|
594
|
+
const report = sandboxReports("file-write-data", [tmpDir, path.dirname(tmpDir)], [executorPid]);
|
|
595
|
+
assert.deepEqual(typeof report === "string" ? report : report.get(executorPid), { sandboxed: true, answers: ["allow", "deny"] });
|
|
596
|
+
// bind: environment/add with the token, which only that call carries.
|
|
597
|
+
const calls: [string, any][] = [];
|
|
598
|
+
assert.equal(await s2.session.bind({ request: async (m, p) => void calls.push([m, p]) }), null);
|
|
599
|
+
assert.equal(calls.length, 1);
|
|
600
|
+
assert.equal(calls[0][0], "environment/add");
|
|
601
|
+
assert.equal(calls[0][1].environmentId, STAGE2_ENVIRONMENT_ID);
|
|
602
|
+
assert.equal(calls[0][1].execServerUrl, `ws://127.0.0.1:${ex.port}`);
|
|
603
|
+
assert.equal(redact(calls[0][1].authBearerToken), "[redacted: executor token]");
|
|
604
|
+
assert.equal(crypto.createHash("sha256").update(calls[0][1].authBearerToken).digest("hex"), /--ws-token-sha256 ([0-9a-f]{64})/.exec(ps)![1]);
|
|
605
|
+
const failed = await s2.session.bind({ request: async () => { throw new Error(`refused Bearer ${calls[0][1].authBearerToken}`); } });
|
|
606
|
+
assert.match(failed ?? "", /environment\/add: refused Bearer \[redacted/);
|
|
607
|
+
assert.ok(!failed!.includes(calls[0][1].authBearerToken));
|
|
608
|
+
// The thread's and every turn's overlay: relay's executor alone.
|
|
609
|
+
assert.deepEqual(s2.session.threadParams("/w"), { environments: [{ environmentId: "relay", cwd: "/w" }], approvalPolicy: "on-request" });
|
|
610
|
+
assert.deepEqual(s2.session.turnParams("/w"), { environments: [{ environmentId: "relay", cwd: "/w" }] });
|
|
611
|
+
assert.deepEqual(s2.session.approvalDetail("command"), { confined: true });
|
|
612
|
+
assert.deepEqual(s2.session.approvalDetail("file"), {}, "a file change names no environment: unmarked (review R4)");
|
|
613
|
+
// Before the watch runs, relay cannot say the addresses held: an approval fails the turn.
|
|
614
|
+
assert.match(s2.session.approvalProblem("command", "relay") ?? "", /not watching this Mac's network addresses/);
|
|
615
|
+
let fail: string | null = null;
|
|
616
|
+
s2.session.watch((e) => (fail = e));
|
|
617
|
+
// Approvals (review R4): a command must be bound to relay's executor; a file change names no environment.
|
|
618
|
+
assert.equal(s2.session.approvalProblem("command", "relay"), null);
|
|
619
|
+
assert.equal(s2.session.approvalProblem("file", undefined), null);
|
|
620
|
+
for (const other of ["local", undefined, null, "relay2"]) assert.match(s2.session.approvalProblem("command", other) ?? "", /not relay's sandboxed executor/, String(other));
|
|
621
|
+
for (let i = 0; i < 100 && !fs.existsSync(detachedPid); i++) await sleep(50);
|
|
622
|
+
stray = Number(fs.readFileSync(detachedPid, "utf8"));
|
|
623
|
+
assert.ok(!gone(stray));
|
|
624
|
+
const t = await s2.session.teardown();
|
|
625
|
+
assert.equal(fail, null, "the executor's exit during teardown fails nothing");
|
|
626
|
+
assert.deepEqual(t.survivors, []);
|
|
627
|
+
assert.ok(t.warnings.some((w) => w.includes(String(stray))), `the detached process was swept and reported: ${JSON.stringify(t.warnings)}`);
|
|
628
|
+
assert.ok(gone(executorPid), "executor stopped");
|
|
629
|
+
assert.ok(gone(stray), "the setsid child stopped");
|
|
630
|
+
assert.deepEqual(leased(), [], "the lease is released");
|
|
631
|
+
assert.deepEqual(executorRecords().records, [], "the record goes with the executor");
|
|
632
|
+
assert.equal(await s2.session.bind({ request: async () => {} }), "relay's executor for this turn is no longer available, so it stopped the agent.", "the token is dropped with the turn");
|
|
633
|
+
assert.deepEqual(await s2.session.teardown(), t, "teardown runs once");
|
|
634
|
+
});
|
|
635
|
+
} finally {
|
|
636
|
+
await s2?.session.teardown();
|
|
637
|
+
for (const p of [executorPid, stray]) if (p) try { process.kill(p, "SIGKILL"); } catch { /* gone */ }
|
|
638
|
+
setLeaseOptionsForTests(null);
|
|
639
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
640
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
test("openStage2: the executor's exit, or a change of addresses, calls fail once; an approval sees a change at once; abort stops it before the app server starts", { skip: !darwin }, async () => {
|
|
645
|
+
const dir = scratch();
|
|
646
|
+
const bin = fakeExecutor(dir);
|
|
647
|
+
const spec = specFor(dir, bin);
|
|
648
|
+
useScratchRange();
|
|
649
|
+
const base = interfaceAddresses();
|
|
650
|
+
let now = base;
|
|
651
|
+
setInterfaceReaderForTests(() => now);
|
|
652
|
+
try {
|
|
653
|
+
await withTurnSecrets(async () => {
|
|
654
|
+
for (const how of ["exit", "addresses", "approval", "abort"] as const) {
|
|
655
|
+
now = base;
|
|
656
|
+
const tmpDir = privateTurnTemp(ensurePrivateRoot(nativeTempRoot()), "");
|
|
657
|
+
const s2 = await openStage2(spec, { env: { PATH: "/usr/bin:/bin" }, servers: [] }, tmpDir);
|
|
658
|
+
const { pid } = ourExecutor(bin);
|
|
659
|
+
assert.ok(pid > 1);
|
|
660
|
+
if (how === "abort") {
|
|
661
|
+
await s2.abort("startCodexAppTurn threw");
|
|
662
|
+
assert.ok(gone(pid));
|
|
663
|
+
assert.deepEqual(executorRecords().records, []);
|
|
664
|
+
assert.deepEqual(leased(), []);
|
|
665
|
+
} else {
|
|
666
|
+
const fails: string[] = [];
|
|
667
|
+
s2.session.watch((e) => fails.push(e));
|
|
668
|
+
if (how === "exit") process.kill(pid, "SIGKILL");
|
|
669
|
+
else now = [...base, "192.0.2.99"];
|
|
670
|
+
if (how === "approval") {
|
|
671
|
+
// InterfaceWatch.changed() runs before each approval: the change is seen without waiting for the poll.
|
|
672
|
+
assert.match(s2.session.approvalProblem("command", "relay") ?? "", /addresses changed during the turn: new 192\.0\.2\.99/);
|
|
673
|
+
assert.match(s2.session.approvalProblem("file") ?? "", /addresses changed during the turn/);
|
|
674
|
+
} else {
|
|
675
|
+
// The poll (INTERFACE_POLL_MS, 3 s) or the exit.
|
|
676
|
+
for (let i = 0; i < 400 && !fails.length; i++) await sleep(20);
|
|
677
|
+
await sleep(100);
|
|
678
|
+
assert.equal(fails.length, 1, JSON.stringify(fails));
|
|
679
|
+
assert.match(fails[0], how === "exit" ? /executor .* exited during the turn \(SIGKILL\)/ : /addresses changed during the turn: new 192\.0\.2\.99/);
|
|
680
|
+
}
|
|
681
|
+
await s2.session.teardown();
|
|
682
|
+
assert.ok(gone(pid));
|
|
683
|
+
assert.deepEqual(leased(), []);
|
|
684
|
+
}
|
|
685
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
686
|
+
}
|
|
687
|
+
});
|
|
688
|
+
} finally {
|
|
689
|
+
setInterfaceReaderForTests(null);
|
|
690
|
+
setLeaseOptionsForTests(null);
|
|
691
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
test("openStage2: a refusal from admission (the person's consent missing or for another grant included), a missing binary or a failed launch leaves nothing running and the ports released", { skip: !darwin }, async () => {
|
|
696
|
+
const dir = scratch();
|
|
697
|
+
const tmpDir = privateTurnTemp(ensurePrivateRoot(nativeTempRoot()), "");
|
|
698
|
+
useScratchRange();
|
|
699
|
+
try {
|
|
700
|
+
await withTurnSecrets(async () => {
|
|
701
|
+
const bin = fakeExecutor(dir);
|
|
702
|
+
// D's admission, as it is: a read job, a Codex that cannot say its version, an environments.toml.
|
|
703
|
+
await assert.rejects(openStage2(specFor(dir, bin, { access: "read" }), { env: { PATH: "/usr/bin" }, servers: [] }, tmpDir), /raw network is for write jobs/);
|
|
704
|
+
// The person's consent (review R2): never given, given for another grant, or a job relay does not know.
|
|
705
|
+
await assert.rejects(openStage2(specFor(dir, bin, {}, { consent: "none" }), { env: { PATH: "/usr/bin" }, servers: [] }, tmpDir), /the person has not consented to this job's raw network/);
|
|
706
|
+
await assert.rejects(openStage2(specFor(dir, bin, {}, { consent: "other" }), { env: { PATH: "/usr/bin" }, servers: [] }, tmpDir), /consent is for another grant than the one this turn holds/);
|
|
707
|
+
const unknown = specFor(dir, bin);
|
|
708
|
+
await assert.rejects(openStage2({ ...unknown, markers: { ...unknown.markers, nodeId: "j_notthere" } }, { env: { PATH: "/usr/bin" }, servers: [] }, tmpDir), /cannot find the job in its registry/);
|
|
709
|
+
await assert.rejects(openStage2(specFor(dir, "no-such-codex-binary"), { env: { PATH: "/usr/bin" }, servers: [] }, tmpDir), /could not read Codex's version/);
|
|
710
|
+
const home = path.join(dir, "apphome");
|
|
711
|
+
fs.mkdirSync(home);
|
|
712
|
+
fs.writeFileSync(path.join(home, "environments.toml"), "");
|
|
713
|
+
await assert.rejects(openStage2(specFor(dir, bin), { env: { PATH: "/usr/bin", CODEX_HOME: home }, servers: [] }, tmpDir), /environments\.toml exists/);
|
|
714
|
+
await assert.rejects(openStage2(specFor(dir, bin), { env: { PATH: "/usr/bin", CODEX_EXEC_SERVER_URL: "none" }, servers: [] }, tmpDir), /CODEX_EXEC_SERVER_URL/);
|
|
715
|
+
assert.deepEqual(leased(), [], "admission refuses before any port is leased");
|
|
716
|
+
assert.deepEqual(executorRecords().records, [], "and before any executor is recorded");
|
|
717
|
+
assert.equal(spawnSync("/usr/bin/pgrep", ["-P", String(process.pid), "-f", `${bin} exec-server`]).status, 1, "or started");
|
|
718
|
+
// An executor that answers --version and never prints its address: stopped, and the lease released.
|
|
719
|
+
const exited = fakeExecutor(dir, { name: "exits", execExit: 3 });
|
|
720
|
+
await assert.rejects(openStage2(specFor(dir, exited), { env: { PATH: "/usr/bin" }, servers: [] }, tmpDir), /exited before it was ready/);
|
|
721
|
+
assert.deepEqual(leased(), []);
|
|
722
|
+
assert.equal(spawnSync("/usr/bin/pgrep", ["-f", `${exited} exec-server`]).status, 1);
|
|
723
|
+
assert.deepEqual(executorRecords().records, [], "a failed launch leaves no record");
|
|
724
|
+
});
|
|
725
|
+
} finally {
|
|
726
|
+
setLeaseOptionsForTests(null);
|
|
727
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
728
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
729
|
+
}
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
test("openStage2 (review r_0c11d860 C2, C5): refused without a working sandbox_check helper; a teardown that cannot sweep keeps the record tainted and the ports quarantined until a clean sweep", { skip: !darwin }, async () => {
|
|
733
|
+
const dir = scratch();
|
|
734
|
+
const tmpDir = privateTurnTemp(ensurePrivateRoot(nativeTempRoot()), "");
|
|
735
|
+
useScratchRange();
|
|
736
|
+
const bin = fakeExecutor(dir);
|
|
737
|
+
const missing = path.join(dir, "no-helper");
|
|
738
|
+
let stray = 0, executorPid = 0;
|
|
739
|
+
try {
|
|
740
|
+
await withTurnSecrets(async () => {
|
|
741
|
+
setSandboxCheckHelperForTests(missing);
|
|
742
|
+
await assert.rejects(openStage2(specFor(dir, bin), { env: { PATH: "/usr/bin" }, servers: [] }, tmpDir), /only where it can find every process they leave behind, and relay's sandbox_check helper .* is not there/);
|
|
743
|
+
setSandboxCheckHelperForTests(null);
|
|
744
|
+
assert.deepEqual(leased(), [], "refused before any port is leased");
|
|
745
|
+
assert.deepEqual(executorRecords().records, []);
|
|
746
|
+
|
|
747
|
+
const detachedPid = path.join(dir, "w", "detached.pid");
|
|
748
|
+
const spec = specFor(dir, bin, { environment: { PATH: "/usr/bin:/bin", FAKE_EXEC_CHILDREN: detachedPid } });
|
|
749
|
+
const s2 = await openStage2(spec, { env: { PATH: "/usr/bin:/bin", FAKE_EXEC_CHILDREN: detachedPid }, servers: [] }, tmpDir);
|
|
750
|
+
executorPid = ourExecutor(bin).pid;
|
|
751
|
+
for (let i = 0; i < 100 && !fs.existsSync(detachedPid); i++) await sleep(50);
|
|
752
|
+
stray = Number(fs.readFileSync(detachedPid, "utf8"));
|
|
753
|
+
const held = leased();
|
|
754
|
+
assert.equal(held.length, 6, "executor, broker and block stay quarantined");
|
|
755
|
+
// The helper goes away mid-turn: teardown cannot show that nothing is left.
|
|
756
|
+
setSandboxCheckHelperForTests(missing);
|
|
757
|
+
let t: Awaited<ReturnType<typeof s2.session.teardown>>;
|
|
758
|
+
try {
|
|
759
|
+
t = await s2.session.teardown();
|
|
760
|
+
} finally {
|
|
761
|
+
setSandboxCheckHelperForTests(null);
|
|
762
|
+
}
|
|
763
|
+
assert.ok(gone(executorPid), "the executor is stopped all the same");
|
|
764
|
+
assert.ok(!gone(stray), "the detached process could not be found");
|
|
765
|
+
assert.ok(t.warnings.some((w) => /could not confirm that none of this turn's commands is still running \(relay's sandbox_check helper/.test(w)), JSON.stringify(t.warnings));
|
|
766
|
+
assert.ok(t.warnings.some((w) => /keeps this turn's executor record and its ports reserved/.test(w)), JSON.stringify(t.warnings));
|
|
767
|
+
const [rec] = executorRecords().records;
|
|
768
|
+
assert.match(rec?.record.tainted ?? "", /sandbox_check helper/);
|
|
769
|
+
assert.deepEqual(leased(), held, "this process let its claim go, and the record still holds every port");
|
|
770
|
+
assert.equal(liveExecutorRecords().length, 1, "local mode stays refused");
|
|
771
|
+
// A clean sweep (reap or retention; the owner's taint lets it in at once) finds the stray, then lets everything go.
|
|
772
|
+
assert.equal(stopNodeExecutorsNow(spec.markers.nodeId), "");
|
|
773
|
+
for (let i = 0; i < 40 && !gone(stray); i++) await sleep(50);
|
|
774
|
+
assert.ok(gone(stray));
|
|
775
|
+
assert.deepEqual(executorRecords().records, []);
|
|
776
|
+
assert.deepEqual(leased(), []);
|
|
777
|
+
});
|
|
778
|
+
} finally {
|
|
779
|
+
setSandboxCheckHelperForTests(null);
|
|
780
|
+
for (const p of [executorPid, stray]) if (p) try { process.kill(p, "SIGKILL"); } catch { /* gone */ }
|
|
781
|
+
setLeaseOptionsForTests(null);
|
|
782
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
783
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
784
|
+
}
|
|
785
|
+
});
|
|
786
|
+
|
|
787
|
+
test("openStage2: prepareLaunch refuses a hard link out of the worktree or a granted write path (hardLinkProblem skips relay-enforced turns)", { skip: !darwin }, async () => {
|
|
788
|
+
const dir = scratch();
|
|
789
|
+
useScratchRange();
|
|
790
|
+
try {
|
|
791
|
+
await withTurnSecrets(async () => {
|
|
792
|
+
const bin = fakeExecutor(dir);
|
|
793
|
+
const outside = path.join(dir, "outside");
|
|
794
|
+
fs.mkdirSync(outside);
|
|
795
|
+
fs.writeFileSync(path.join(outside, "secret"), "not the job's\n");
|
|
796
|
+
const granted = path.join(dir, "granted");
|
|
797
|
+
fs.mkdirSync(granted);
|
|
798
|
+
for (const where of ["worktree", "grant"] as const) {
|
|
799
|
+
const tmpDir = privateTurnTemp(ensurePrivateRoot(nativeTempRoot()), "");
|
|
800
|
+
const spec = specFor(dir, bin, where === "grant" ? { grant: { network: true, raw_network: true, write_paths: [granted], write_path_pins: [dirIdentity(granted)] } as TurnSpec["grant"] } : {});
|
|
801
|
+
const link = path.join(where === "grant" ? granted : spec.cwd, "alias");
|
|
802
|
+
fs.linkSync(path.join(outside, "secret"), link);
|
|
803
|
+
try {
|
|
804
|
+
await assert.rejects(openStage2(spec, { env: { PATH: "/usr/bin:/bin" }, servers: [] }, tmpDir), (err: Error) => /hard link/.test(err.message) && err.message.includes("alias"), where);
|
|
805
|
+
assert.equal(spawnSync("/usr/bin/pgrep", ["-P", String(process.pid), "-f", `${bin} exec-server`]).status, 1, "no executor started");
|
|
806
|
+
assert.deepEqual(leased(), []);
|
|
807
|
+
} finally {
|
|
808
|
+
fs.rmSync(link, { force: true });
|
|
809
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
// The control: without the link the same turn launches.
|
|
813
|
+
const tmpDir = privateTurnTemp(ensurePrivateRoot(nativeTempRoot()), "");
|
|
814
|
+
const s2 = await openStage2(specFor(dir, bin, { grant: { network: true, raw_network: true, write_paths: [granted], write_path_pins: [dirIdentity(granted)] } as TurnSpec["grant"] }), { env: { PATH: "/usr/bin:/bin" }, servers: [] }, tmpDir);
|
|
815
|
+
await s2.abort("done");
|
|
816
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
817
|
+
});
|
|
818
|
+
} finally {
|
|
819
|
+
setLeaseOptionsForTests(null);
|
|
820
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
|
|
824
|
+
test("startCodexAppTurn: a stage-2 app server starts only with the environment its admission checked", { skip: !darwin }, async () => {
|
|
825
|
+
const dir = scratch();
|
|
826
|
+
const tmpDir = privateTurnTemp(ensurePrivateRoot(nativeTempRoot()), "");
|
|
827
|
+
useScratchRange();
|
|
828
|
+
try {
|
|
829
|
+
await withTurnSecrets(async () => {
|
|
830
|
+
const bin = fakeExecutor(dir);
|
|
831
|
+
const spec = specFor(dir, bin);
|
|
832
|
+
const s2 = await openStage2(spec, { env: { PATH: "/usr/bin:/bin" }, servers: [] }, tmpDir);
|
|
833
|
+
try {
|
|
834
|
+
const flags = { off: [], c: [], expect: {} as never };
|
|
835
|
+
// An equal copy is not the object admission checked; one with something added even less so.
|
|
836
|
+
for (const env of [{ ...s2.session.env }, { ...s2.session.env, CODEX_EXEC_SERVER_URL: "ws://127.0.0.1:1" }]) {
|
|
837
|
+
assert.throws(() => startCodexAppTurn(spec, flags, env as NodeJS.ProcessEnv, s2.session), /in an environment other than the one its admission checked/);
|
|
838
|
+
}
|
|
839
|
+
assert.ok(!fs.existsSync(spec.logPath), "nothing started, not even the log");
|
|
840
|
+
} finally {
|
|
841
|
+
await s2.abort("done");
|
|
842
|
+
}
|
|
843
|
+
});
|
|
844
|
+
} finally {
|
|
845
|
+
setLeaseOptionsForTests(null);
|
|
846
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
847
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
848
|
+
}
|
|
849
|
+
});
|