@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,656 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { confinement } from "../confine/index.js";
|
|
7
|
+
import { deniedTcpPorts, INTERFACE_POLL_MS, interfaceAddresses, leaseTurnPorts, listeningSnapshot, STATIC_DENIED_UDP, watchInterfaces } from "../confine/ports.js";
|
|
8
|
+
import { sandboxCheckUnavailable } from "../confine/sbcheck.js";
|
|
9
|
+
import { boundaryProbe, NO_PROBE } from "../confine/turn-probe.js";
|
|
10
|
+
import { turnPolicy } from "../confine/turn.js";
|
|
11
|
+
import { getNode, getTurn, recordLaunchLeader } from "../core/db.js";
|
|
12
|
+
import { registerEscalationRuntime } from "../core/escalation-contract.js";
|
|
13
|
+
import { createEscalationRuntime } from "../core/escalation-runtime.js";
|
|
14
|
+
import { escalationDigest } from "../core/escalation-policy.js";
|
|
15
|
+
import { mediatedBaseline, mediatedNetworkProvider, needsMediatedNetwork, NO_MEDIATED_PROVIDER, MEDIATED_NETWORK_EFFECT } from "../core/mediated-network-provider.js";
|
|
16
|
+
import { drainGroup, groupAlive, isGroupSignalRefusal, pidAlive, procStart, sameProcess, sendGroupSignal, stopProcs } from "../core/procs.js";
|
|
17
|
+
import { diagnosticTail } from "../core/redact.js";
|
|
18
|
+
import { assertOverlayLaunch, overlayTurnProblem } from "../core/restart-overlay.js";
|
|
19
|
+
import { dirIdentity } from "../confine/seatbelt.js";
|
|
20
|
+
import { redact, registerTurnSecret } from "../core/secrets.js";
|
|
21
|
+
import { executorQuarantine, forgetExecutor, noteDescendants, recordExecutor, sweepSurvivorsNow, sweepTurnSurvivors as turnSurvivors, taintExecutor } from "../core/stage2-executors.js";
|
|
22
|
+
import { deniedCredentials } from "./codex.js";
|
|
23
|
+
import { codexToolTimeoutS } from "../targets/codex.js";
|
|
24
|
+
import { watchProcess } from "./types.js";
|
|
25
|
+
import { Stage2TurnGuard, stage2Admission, stage2AppServerEnv, stage2ApprovalDetail, stage2CommandEnvironmentProblem, stage2Plan, stage2QueueProblem, stage2Readback, stage2ThreadMcpProblem, stage2ThreadParams, stage2ThreadProblem, stage2TurnParams, stage2TurnReadback } from "./codex-stage2.js";
|
|
26
|
+
/**
|
|
27
|
+
* Stage 2's executor: `codex exec-server` inside relay's Seatbelt, bound to the app server's thread
|
|
28
|
+
* as its only environment, supervised and torn down with the turn (docs/codex-confinement-design.md
|
|
29
|
+
* §2.2, §3.1; review C5, X3, X7, C8). Owner: WP-C. codex.ts calls openStage2; codex-app.ts calls the
|
|
30
|
+
* Stage2Session hooks at the points named on each.
|
|
31
|
+
*/
|
|
32
|
+
/** The environment id relay adds and the thread selects. */
|
|
33
|
+
export const STAGE2_ENVIRONMENT_ID = "relay";
|
|
34
|
+
const REAL = {
|
|
35
|
+
admission: stage2Admission, plan: stage2Plan, lease: (proxy) => leaseTurnPorts({ proxy }), deniedTcp: (own) => deniedTcpPorts(listeningSnapshot(), own), snapshot: listeningSnapshot,
|
|
36
|
+
interfaces: interfaceAddresses, watchInterfaces: (baseline) => watchInterfaces(baseline),
|
|
37
|
+
// Loaded with the first turn that opens one (it brings the MCP server and SDK), not with every worker.
|
|
38
|
+
openBroker: async (...a) => (await import("../confine/broker-http.js")).openHttpBroker(...a), sweepProblem: sandboxCheckUnavailable, confinement, probe: boundaryProbe,
|
|
39
|
+
resolveBin, policy: executorPolicy, launch: launchExecutor, stop: stopExecutor, appStart: procStart, turnReadback: stage2TurnReadback,
|
|
40
|
+
};
|
|
41
|
+
// Unit tests replace host resources; live tests leave every resource real unless explicitly testing a failure.
|
|
42
|
+
let testDeps = null;
|
|
43
|
+
export function setStage2DepsForTests(d) { testDeps = d; }
|
|
44
|
+
/**
|
|
45
|
+
* The normal live-test overrides: this Mac's interface
|
|
46
|
+
* addresses (a unit test must change them mid-turn), and the port range leases come from (tests
|
|
47
|
+
* running beside other relay processes take a scratch range, so their leases, kept per relay home,
|
|
48
|
+
* cannot collide with those processes' executors). The lease, the profile and every check stay
|
|
49
|
+
* relay's own. Nothing in relay calls the setters.
|
|
50
|
+
*/
|
|
51
|
+
let interfaceReader = null;
|
|
52
|
+
let leaseOptions = null;
|
|
53
|
+
/** Test-only: read this Mac's interface addresses from `read` (for the baseline and the watch), or null for the real ones. */
|
|
54
|
+
export function setInterfaceReaderForTests(read) {
|
|
55
|
+
interfaceReader = read;
|
|
56
|
+
}
|
|
57
|
+
/** Test-only: lease ports with these options (a scratch range or lease directory), or null for relay's reserved range. */
|
|
58
|
+
export function setLeaseOptionsForTests(o) {
|
|
59
|
+
leaseOptions = o;
|
|
60
|
+
}
|
|
61
|
+
let testHook = null;
|
|
62
|
+
export function setStage2HookForTests(hook) {
|
|
63
|
+
testHook = hook;
|
|
64
|
+
}
|
|
65
|
+
const deps = () => {
|
|
66
|
+
const read = interfaceReader, lease = leaseOptions;
|
|
67
|
+
return {
|
|
68
|
+
...REAL,
|
|
69
|
+
...(read ? { interfaces: () => read(), watchInterfaces: (baseline) => watchInterfaces(baseline, INTERFACE_POLL_MS, read) } : {}),
|
|
70
|
+
...(lease ? { lease: (proxy) => leaseTurnPorts({ ...lease, proxy }) } : {}),
|
|
71
|
+
...testDeps,
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
/** How long relay waits for the executor to print its URL, and for the kernel to report it sandboxed. */
|
|
75
|
+
const READY_MS = 15000;
|
|
76
|
+
const CONFINED_WAIT_MS = 5000;
|
|
77
|
+
/** The executor's grace after SIGTERM at teardown: it holds nothing of the person's that needs saving. */
|
|
78
|
+
const EXECUTOR_GRACE_MS = 500;
|
|
79
|
+
/** An executable named on PATH (or given as a path), as an absolute, resolved path, or null. Seatbelt needs it absolute. */
|
|
80
|
+
function resolveBin(bin, env) {
|
|
81
|
+
const ok = (p) => {
|
|
82
|
+
try {
|
|
83
|
+
fs.accessSync(p, fs.constants.X_OK);
|
|
84
|
+
return fs.statSync(p).isFile();
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
if (bin.includes("/"))
|
|
91
|
+
return ok(path.resolve(bin)) ? fs.realpathSync(path.resolve(bin)) : null;
|
|
92
|
+
for (const dir of (env.PATH ?? "").split(path.delimiter)) {
|
|
93
|
+
if (dir && path.isAbsolute(dir) && ok(path.join(dir, bin)))
|
|
94
|
+
return fs.realpathSync(path.join(dir, bin));
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
/** The executor's own CODEX_HOME: inside the turn's temp dir, so nothing of ~/.codex is needed or reachable (§3.1, D8). */
|
|
99
|
+
const executorHome = (tmpDir) => path.join(tmpDir, "codex-exec-home");
|
|
100
|
+
/**
|
|
101
|
+
* The executor's environment, from which it builds every command's (review C8): relay's scrubbed
|
|
102
|
+
* agent environment and its own CODEX_HOME and TMPDIR. When the turn may bind the job's port block,
|
|
103
|
+
* it is advertised as PORT and PORTS (§5.5); closed turns are not told to use denied ports. The
|
|
104
|
+
* launch adds the sandbox's own variables on top. Every route that would point Codex at another
|
|
105
|
+
* executor is left out.
|
|
106
|
+
*/
|
|
107
|
+
export function executorEnv(agent, tmpDir, ports, blockBindAllowed) {
|
|
108
|
+
const env = {};
|
|
109
|
+
for (const [k, v] of Object.entries(agent)) {
|
|
110
|
+
if (v === undefined || k === "CODEX_HOME" || k === "CODEX_EXEC_SERVER_URL" || /^CODEX_.*NOISE/i.test(k))
|
|
111
|
+
continue;
|
|
112
|
+
env[k] = v;
|
|
113
|
+
}
|
|
114
|
+
env.CODEX_HOME = executorHome(tmpDir);
|
|
115
|
+
env.TMPDIR = tmpDir;
|
|
116
|
+
if (blockBindAllowed) {
|
|
117
|
+
env.PORT = String(ports.block[0]);
|
|
118
|
+
env.PORTS = ports.block.join(",");
|
|
119
|
+
}
|
|
120
|
+
return env;
|
|
121
|
+
}
|
|
122
|
+
/** The policy for the executor: turnPolicy with the stage-2 network, AF_UNIX by turn kind, CODEX_HOME in the turn's temp dir, ~/.codex and the credential stores denied (§2.3). */
|
|
123
|
+
export function executorPolicy(spec, tmpDir, net) {
|
|
124
|
+
const home = executorHome(tmpDir);
|
|
125
|
+
fs.mkdirSync(home, { recursive: true, mode: 0o700 });
|
|
126
|
+
// The person's real HOME, read-only, as Codex's own sandbox gives commands today; the executor
|
|
127
|
+
// writes only its temp CODEX_HOME. No keychain, no certificate trust (§3.1: trustd fetches past the profile).
|
|
128
|
+
const state = { home: "real", stateDirs: [home], keychain: false };
|
|
129
|
+
const policy = turnPolicy(spec, state, tmpDir, net, os.homedir(), "Codex's executor");
|
|
130
|
+
// The 1.0.5 credential denies, and all of ~/.codex and the app server's CODEX_HOME (decision D8).
|
|
131
|
+
const appHome = spec.environment?.CODEX_HOME;
|
|
132
|
+
const codexHomes = [path.join(os.homedir(), ".codex"), ...(appHome && path.isAbsolute(appHome) ? [appHome] : [])];
|
|
133
|
+
const credentials = Object.keys(deniedCredentials(undefined, spec.config)).filter((p) => !p.endsWith("/auth.json")).map((p) => p.replace(/^~(?=\/)/, os.homedir()));
|
|
134
|
+
const roots = spec.overlay?.request.roots ?? [];
|
|
135
|
+
const trees = roots.filter(r => r.scope === "tree");
|
|
136
|
+
return { ...policy, ...(spec.neverPolicy ? { neverPolicy: spec.neverPolicy } : {}),
|
|
137
|
+
writable: [...(policy.writable ?? []), ...trees.map(r => r.root)],
|
|
138
|
+
...((policy.writablePins?.length || trees.length) ? { writablePins: [...(policy.writablePins ?? []), ...trees.map(r => dirIdentity(r.root))] } : {}),
|
|
139
|
+
overlayFiles: roots.filter(r => r.scope === "file"),
|
|
140
|
+
overlayRoots: roots,
|
|
141
|
+
denied: [...new Set([...policy.denied, ...credentials, ...codexHomes])] };
|
|
142
|
+
}
|
|
143
|
+
/** A fresh executor token: 32 random bytes, registered as a turn secret, and its SHA-256 for argv. Never written anywhere. */
|
|
144
|
+
export function executorToken() {
|
|
145
|
+
const token = crypto.randomBytes(32).toString("hex");
|
|
146
|
+
registerTurnSecret(token, "executor token");
|
|
147
|
+
return { token, hash: crypto.createHash("sha256").update(token).digest("hex") };
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* What relay keeps of a running executor beyond the interface: the watch on its descendants, its
|
|
151
|
+
* output tail, its process, and its record on disk (core/stage2-executors.ts) with the timer that
|
|
152
|
+
* copies newly seen descendants into it.
|
|
153
|
+
*/
|
|
154
|
+
const running = new WeakMap();
|
|
155
|
+
/** How often newly seen descendants are copied into the executor's record, for whoever must stop them if this process dies. */
|
|
156
|
+
const PERSIST_MS = 1000;
|
|
157
|
+
/** The last lines the executor wrote, redacted: for a failure message. */
|
|
158
|
+
const lastWords = (tail) => tail.text().trim().split("\n").slice(-3).join(" ");
|
|
159
|
+
/** Spawns the executor under `launch` as a detached, registered launch leader with relay's scrubbed env, checks it is sandboxed and reads its URL (§2.2 step 9). */
|
|
160
|
+
export async function launchExecutor(o) {
|
|
161
|
+
const { spec, launch, ports } = o;
|
|
162
|
+
// The launch must run exactly the executor relay asked for, on its leased port, with the token's hash.
|
|
163
|
+
const want = [o.bin, "exec-server", "--listen", `ws://127.0.0.1:${ports.exec}`, "--ws-auth", "capability-token", "--ws-token-sha256", o.hash];
|
|
164
|
+
const tailArgs = launch.args.slice(-want.length);
|
|
165
|
+
if (want.some((a, i) => tailArgs[i] !== a))
|
|
166
|
+
throw new Error("relay's executor launch is not the command it prepared, so it does not start it");
|
|
167
|
+
let mechanism = o.mechanism;
|
|
168
|
+
if (!mechanism) {
|
|
169
|
+
const found = confinement();
|
|
170
|
+
if ("reason" in found)
|
|
171
|
+
throw new Error(`relay cannot confine Codex's commands: ${found.reason}`);
|
|
172
|
+
mechanism = found.mechanism;
|
|
173
|
+
}
|
|
174
|
+
const tail = diagnosticTail();
|
|
175
|
+
const child = spawn(launch.command, launch.args, { cwd: spec.cwd, env: { ...o.env, ...launch.env }, detached: true, stdio: ["ignore", "pipe", "pipe"] });
|
|
176
|
+
let out = "";
|
|
177
|
+
let spawnError = null;
|
|
178
|
+
child.on("error", (err) => (spawnError = err));
|
|
179
|
+
child.stdout.on("data", (d) => {
|
|
180
|
+
if (out.length < 4096)
|
|
181
|
+
out += d.toString();
|
|
182
|
+
tail.write(d.toString());
|
|
183
|
+
});
|
|
184
|
+
child.stderr.on("data", (d) => tail.write(d.toString()));
|
|
185
|
+
let gone = null;
|
|
186
|
+
const exited = new Promise((resolve) => child.once("exit", (code, signal) => resolve((gone = { code, signal }))));
|
|
187
|
+
const pid = child.pid ?? -1;
|
|
188
|
+
let watch = null;
|
|
189
|
+
let record = null;
|
|
190
|
+
let persist = null;
|
|
191
|
+
try {
|
|
192
|
+
if (pid <= 1) {
|
|
193
|
+
await new Promise((r) => setImmediate(r));
|
|
194
|
+
throw new Error(`relay could not start Codex's executor: ${spawnError?.message ?? "no process"}`);
|
|
195
|
+
}
|
|
196
|
+
// A launch leader like the agents relay starts (session.ts): a process left in its group or
|
|
197
|
+
// session is placed inside the launched tree after it exits (core/presence.ts).
|
|
198
|
+
recordLaunchLeader(spec.markers.runId, pid, "agent", spec.markers.nodeId);
|
|
199
|
+
const start = procStart(pid, { fresh: true });
|
|
200
|
+
if (!start)
|
|
201
|
+
throw new Error("relay could not read the identity of Codex's executor, so it stopped it");
|
|
202
|
+
// Recorded on disk, and named on its ports' lease, as soon as its identity is known: should this
|
|
203
|
+
// process die from here on, reap, cancel or the retention sweep stops it and gives the ports back
|
|
204
|
+
// (core/stage2-executors.ts), and no other relay process leases its ports while it lives.
|
|
205
|
+
// The record first, so that the lease can name it: while the record is there (a teardown that
|
|
206
|
+
// was not sure keeps it, tainted), the ports stay quarantined.
|
|
207
|
+
record = recordExecutor({ pid, start, runId: spec.markers.runId, nodeId: spec.markers.nodeId, turnId: spec.turnId ?? null, ports: { exec: ports.exec, block: ports.block }, tmpDir: o.tmpDir });
|
|
208
|
+
ports.hold?.(pid, start, record);
|
|
209
|
+
watch = watchProcess(pid);
|
|
210
|
+
const w = watch, file = record;
|
|
211
|
+
let noted = 0;
|
|
212
|
+
persist = setInterval(() => {
|
|
213
|
+
const procs = w.procs();
|
|
214
|
+
if (procs.length === noted)
|
|
215
|
+
return;
|
|
216
|
+
try {
|
|
217
|
+
noteDescendants(file, procs);
|
|
218
|
+
noted = procs.length;
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
// Tried again at the next tick; teardown does not depend on it.
|
|
222
|
+
}
|
|
223
|
+
}, PERSIST_MS);
|
|
224
|
+
persist.unref();
|
|
225
|
+
// sandbox-exec applies the profile and execs Codex in the same process: that pid is the one to ask.
|
|
226
|
+
const until = Date.now() + CONFINED_WAIT_MS;
|
|
227
|
+
while (!mechanism.confined(pid)) {
|
|
228
|
+
if (gone || Date.now() > until)
|
|
229
|
+
throw new Error(`relay could not confirm from the kernel that Codex's executor runs under its sandbox, so it stopped it. ${lastWords(tail)}`.trim());
|
|
230
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
231
|
+
}
|
|
232
|
+
const ready = Date.now() + READY_MS;
|
|
233
|
+
for (;;) {
|
|
234
|
+
if (o.signal?.aborted)
|
|
235
|
+
throw new Error("the turn was stopped while relay was starting Codex's executor");
|
|
236
|
+
const m = /ws:\/\/127\.0\.0\.1:(\d+)\b/.exec(out);
|
|
237
|
+
if (m) {
|
|
238
|
+
if (Number(m[1]) !== ports.exec)
|
|
239
|
+
throw new Error(`Codex's executor listens on port ${m[1]}, not the port relay reserved for it (${ports.exec}), so relay stopped it`);
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
if (gone)
|
|
243
|
+
throw new Error(`Codex's executor exited before it was ready (${JSON.stringify(gone)}). ${lastWords(tail)}`.trim());
|
|
244
|
+
if (Date.now() > ready)
|
|
245
|
+
throw new Error(`Codex's executor did not report its address within ${READY_MS / 1000} s, so relay stopped it. ${lastWords(tail)}`.trim());
|
|
246
|
+
await new Promise((r) => setTimeout(r, 25));
|
|
247
|
+
}
|
|
248
|
+
const ex = { pid, start, url: `ws://127.0.0.1:${ports.exec}`, ports, launch, probes: o.probes, exited };
|
|
249
|
+
running.set(ex, { watch, tail, child, record: record, persist: persist });
|
|
250
|
+
return ex;
|
|
251
|
+
}
|
|
252
|
+
catch (err) {
|
|
253
|
+
if (persist)
|
|
254
|
+
clearInterval(persist);
|
|
255
|
+
watch?.look();
|
|
256
|
+
watch?.stop();
|
|
257
|
+
const refused = pid > 1 && !await drainGroup(pid);
|
|
258
|
+
const left = refused ? [pid, ...(watch?.procs().map(p => p.pid) ?? [])] : watch ? await stopProcs(watch.procs()) : [];
|
|
259
|
+
child.stdout?.destroy();
|
|
260
|
+
child.stderr?.destroy();
|
|
261
|
+
// Kept only if something would not stop, with what that is, for a later sweep to try again once
|
|
262
|
+
// this process is gone (and for liveExecutorRecords meanwhile).
|
|
263
|
+
if (record) {
|
|
264
|
+
try {
|
|
265
|
+
if (!left.length && !groupAlive(pid))
|
|
266
|
+
forgetExecutor(record);
|
|
267
|
+
else
|
|
268
|
+
taintExecutor(record, `processes of a failed executor launch would not stop: ${[...left, ...(groupAlive(pid) ? [pid] : [])].join(", ")}`, watch ? watch.procs().filter((p) => left.includes(p.pid)) : []);
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
// A record left as it is still names the executor by start time; a sweep finds it.
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
throw err;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Every process of this user whose sandbox lets it write `tmpDir` and not the directory holding it:
|
|
279
|
+
* this turn's, wherever they detached to (sbcheck.ts). No other relay turn's profile allows another
|
|
280
|
+
* turn's temp dir; a broader sandbox (one allowing the home, say) also allows the parent and is left
|
|
281
|
+
* alone; unsandboxed processes are never counted. A string, never an empty list, when the look
|
|
282
|
+
* could not be made. In core/stage2-executors.ts, which stops an orphaned executor's the same way.
|
|
283
|
+
*/
|
|
284
|
+
export function sweepTurnSurvivors(tmpDir) {
|
|
285
|
+
return turnSurvivors(tmpDir);
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Teardown (§2.2 step 14): the executor's group, every recorded descendant, then the survivor sweep
|
|
289
|
+
* (core/stage2-executors.ts sweepSurvivorsNow). `survivors` are the pids that would not die; `swept`
|
|
290
|
+
* those the sweep found outside the group and the record (detached with setsid, say) and stopped;
|
|
291
|
+
* `sweep` why the sweep cannot be trusted, if so. `tainted` says why the record was kept: then its
|
|
292
|
+
* lease stays quarantined (the lease names the record) and the caller must not release it.
|
|
293
|
+
*/
|
|
294
|
+
export async function stopExecutor(ex, o) {
|
|
295
|
+
const r = running.get(ex);
|
|
296
|
+
if (r && o.appDescendants?.length)
|
|
297
|
+
noteDescendants(r.record, o.appDescendants);
|
|
298
|
+
if (r)
|
|
299
|
+
clearInterval(r.persist);
|
|
300
|
+
const quarantine = r ? executorQuarantine(r.record, o.quarantine) : o.quarantine;
|
|
301
|
+
const refused = (why) => {
|
|
302
|
+
r?.watch.stop();
|
|
303
|
+
r?.child.stdout?.destroy();
|
|
304
|
+
r?.child.stderr?.destroy();
|
|
305
|
+
r?.child.unref();
|
|
306
|
+
const watched = r?.watch.procs() ?? [];
|
|
307
|
+
const tainted = quarantine ?? why;
|
|
308
|
+
if (r)
|
|
309
|
+
taintExecutor(r.record, tainted, [...watched, ...(o.appDescendants ?? [])]);
|
|
310
|
+
return { survivors: [ex.pid, ...watched.map(p => p.pid)], tainted };
|
|
311
|
+
};
|
|
312
|
+
// An app-group refusal forbids cleanup derived from that target, including executor signals
|
|
313
|
+
// and sandbox sweeps. Keep the watch's identities and the record's lease holds instead.
|
|
314
|
+
if (o.refused)
|
|
315
|
+
return refused(o.refused);
|
|
316
|
+
// The last look while what it started can still be traced to it.
|
|
317
|
+
r?.watch.look();
|
|
318
|
+
if (sameProcess(ex.pid, ex.start)) {
|
|
319
|
+
try {
|
|
320
|
+
sendGroupSignal(ex.pid, "SIGTERM");
|
|
321
|
+
}
|
|
322
|
+
catch (err) {
|
|
323
|
+
if (isGroupSignalRefusal(err))
|
|
324
|
+
return refused(err.message);
|
|
325
|
+
// Already gone.
|
|
326
|
+
}
|
|
327
|
+
const until = Date.now() + o.graceMs;
|
|
328
|
+
while (pidAlive(ex.pid) && Date.now() < until)
|
|
329
|
+
await new Promise((res) => setTimeout(res, 25));
|
|
330
|
+
}
|
|
331
|
+
r?.watch.look();
|
|
332
|
+
const unseenMs = r?.watch.stop();
|
|
333
|
+
if (groupAlive(ex.pid) && !await drainGroup(ex.pid))
|
|
334
|
+
return refused(`relay refused cleanup for process group ${ex.pid}`);
|
|
335
|
+
r?.child.stdout?.destroy();
|
|
336
|
+
r?.child.stderr?.destroy();
|
|
337
|
+
const watched = r ? r.watch.procs() : [];
|
|
338
|
+
const survivors = new Set(await stopProcs(watched));
|
|
339
|
+
// The sweep, until a look finds nothing new, with everything it finds frozen first: a survivor can fork between looks.
|
|
340
|
+
const result = sweepSurvivorsNow(o.tmpDir, { exclude: survivors });
|
|
341
|
+
for (const p of result.survivors)
|
|
342
|
+
survivors.add(p.pid);
|
|
343
|
+
const swept = result.swept.map((p) => p.pid);
|
|
344
|
+
const sweep = result.problem;
|
|
345
|
+
if (groupAlive(ex.pid))
|
|
346
|
+
survivors.add(ex.pid);
|
|
347
|
+
const tainted = quarantine ?? sweep ?? (survivors.size ? `processes would not stop: ${[...survivors].join(", ")}` : undefined);
|
|
348
|
+
// The record goes with the executor only when everything stopped and the sweep showed it. Otherwise
|
|
349
|
+
// it is kept, tainted, with what is left: the lease that names it stays quarantined, local mode stays
|
|
350
|
+
// refused, and reap or retention sweeps it again.
|
|
351
|
+
if (r) {
|
|
352
|
+
try {
|
|
353
|
+
if (!tainted)
|
|
354
|
+
forgetExecutor(r.record);
|
|
355
|
+
else
|
|
356
|
+
taintExecutor(r.record, tainted, [...watched, ...result.survivors].filter((p) => survivors.has(p.pid)));
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
// A record left behind is swept later; it names a process by start time, so it never stops another.
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return { survivors: [...survivors], ...(unseenMs !== undefined ? { unseenMs } : {}), ...(swept.length ? { swept } : {}), ...(sweep ? { sweep } : {}), ...(tainted ? { tainted } : {}) };
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Admission, ports, policy, probes, token and executor, in the order of §2.2 steps 1-9. Throws the
|
|
366
|
+
* reason the turn cannot run. `prepared` is codex.ts's preparation (the agent's scrubbed env and the
|
|
367
|
+
* person's MCP servers).
|
|
368
|
+
*/
|
|
369
|
+
export async function openStage2(spec, prepared, tmpDir) {
|
|
370
|
+
const d = deps();
|
|
371
|
+
if (spec.overlay)
|
|
372
|
+
assertOverlayLaunch(spec.overlay, spec.turnId);
|
|
373
|
+
// Freeze what admission checks. Only relay's no-local setting, broker secret and proxy bypass
|
|
374
|
+
// are added for the app server; the executor gets the checked environment without those additions.
|
|
375
|
+
// Admission reads the person's environment, where any executor variable is refused.
|
|
376
|
+
const checked = Object.freeze({ ...prepared.env });
|
|
377
|
+
const refusal = d.admission(spec, checked);
|
|
378
|
+
if (refusal)
|
|
379
|
+
throw new Error(refusal);
|
|
380
|
+
// Teardown must be able to ask the kernel which processes are this turn's (the survivor sweep,
|
|
381
|
+
// §2.2 step 15): without a working sandbox_check helper it could not show that nothing is left.
|
|
382
|
+
const noSweep = d.sweepProblem();
|
|
383
|
+
if (noSweep)
|
|
384
|
+
throw new Error(`relay runs Codex's commands with raw network only where it can find every process they leave behind, and ${noSweep}, so it does not start the agent. Rebuild relay where Xcode's command line tools are installed (\`npm run build\`), then restart it.`);
|
|
385
|
+
const found = d.confinement();
|
|
386
|
+
if ("reason" in found)
|
|
387
|
+
throw new Error(`relay runs Codex's commands with raw network only inside its own sandbox, and ${found.reason}, so it does not start the agent.`);
|
|
388
|
+
const mechanism = found.mechanism;
|
|
389
|
+
const probe = d.probe();
|
|
390
|
+
if (!probe)
|
|
391
|
+
throw new Error(`${NO_PROBE}, so it does not start Codex with raw network.`);
|
|
392
|
+
const bin = d.resolveBin(spec.config.targets.codex.bin, checked);
|
|
393
|
+
if (!bin)
|
|
394
|
+
throw new Error(`relay could not find ${spec.config.targets.codex.bin} as an executable, so it cannot start Codex's executor.`);
|
|
395
|
+
const overlayCaps = spec.overlay?.request.capabilities ?? [];
|
|
396
|
+
const networkBaseline = mediatedBaseline(spec.grant, spec.config);
|
|
397
|
+
const wantsMediated = needsMediatedNetwork(overlayCaps, networkBaseline, spec.neverPolicy);
|
|
398
|
+
const ports = d.lease(wantsMediated);
|
|
399
|
+
let ex = null;
|
|
400
|
+
let broker;
|
|
401
|
+
let mediated;
|
|
402
|
+
let token = null;
|
|
403
|
+
let iw = null;
|
|
404
|
+
let closed = null;
|
|
405
|
+
let closing = false;
|
|
406
|
+
let releaseEscalation;
|
|
407
|
+
const close = (quarantine, appDescendants, refused) => closed ??= (async () => {
|
|
408
|
+
closing = true;
|
|
409
|
+
broker?.disarm();
|
|
410
|
+
iw?.stop();
|
|
411
|
+
const warnings = [];
|
|
412
|
+
let survivors = [];
|
|
413
|
+
try {
|
|
414
|
+
if (ex) {
|
|
415
|
+
const stopped = await d.stop(ex, { tmpDir, graceMs: EXECUTOR_GRACE_MS, ...(quarantine ? { quarantine } : {}), ...(appDescendants?.length ? { appDescendants } : {}), ...(refused ? { refused } : {}) });
|
|
416
|
+
survivors = stopped.survivors;
|
|
417
|
+
if (stopped.swept?.length)
|
|
418
|
+
warnings.push(`relay found ${stopped.swept.length} process(es) of this turn's commands outside the executor's process tree (pids ${stopped.swept.join(", ")}), still in the turn's sandbox, and stopped them.`);
|
|
419
|
+
if (stopped.sweep)
|
|
420
|
+
warnings.push(`relay could not confirm that none of this turn's commands is still running (${stopped.sweep}); it stopped the executor's process group and the descendants it saw, but a command that detached itself may still be running, inside the turn's sandbox.`);
|
|
421
|
+
if (survivors.length)
|
|
422
|
+
warnings.push(refused ? `relay left this turn's executor processes alone because ${refused}: ${survivors.join(", ")}.` : `processes of this turn's commands would not stop: ${survivors.join(", ")}.`);
|
|
423
|
+
if (stopped.tainted) {
|
|
424
|
+
warnings.push("relay keeps this turn's executor record and its ports reserved, and refuses local-network jobs, until a later sweep (when the job is reaped, or by relay's daily housekeeping) confirms that nothing of the turn still runs.");
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
finally {
|
|
429
|
+
// This process gives up its own claim. A tainted teardown's ports stay quarantined all the
|
|
430
|
+
// same: the lease names the executor's record (PortLease.hold), a release keeps that line, and
|
|
431
|
+
// the lease stays held while the record is there, until a clean sweep removes it.
|
|
432
|
+
try {
|
|
433
|
+
await mediated?.close();
|
|
434
|
+
}
|
|
435
|
+
finally {
|
|
436
|
+
try {
|
|
437
|
+
await broker?.close();
|
|
438
|
+
}
|
|
439
|
+
finally {
|
|
440
|
+
try {
|
|
441
|
+
releaseEscalation?.();
|
|
442
|
+
}
|
|
443
|
+
finally {
|
|
444
|
+
releaseEscalation = undefined;
|
|
445
|
+
ports.release();
|
|
446
|
+
token = null;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return { survivors, warnings, ...(refused ? { uncertain: true } : {}) };
|
|
452
|
+
})();
|
|
453
|
+
try {
|
|
454
|
+
// Snapshot after the lease, before opening any listener of this turn (§4.3).
|
|
455
|
+
const deniedTcp = d.deniedTcp(ports);
|
|
456
|
+
const exact = spec.overlay?.request.capabilities.filter(c => c.kind === "localhost") ?? [];
|
|
457
|
+
if (exact.length) {
|
|
458
|
+
const snapshot = d.snapshot();
|
|
459
|
+
if (exact.some(c => snapshot.controlPlanes.includes(c.port)))
|
|
460
|
+
throw new Error("never relay.authority: control listeners cannot be localhost exceptions");
|
|
461
|
+
if (exact.some(c => c.operation === "listen" && snapshot.ports.includes(c.port)))
|
|
462
|
+
throw new Error("capability_unavailable: requested listen port is occupied");
|
|
463
|
+
}
|
|
464
|
+
if (spec.relay) {
|
|
465
|
+
if (spec.relay.turnId === undefined)
|
|
466
|
+
throw new Error("relay has no turn to serve this agent's relay tools for");
|
|
467
|
+
broker = await d.openBroker({
|
|
468
|
+
nodeId: spec.relay.nodeId, turnId: spec.relay.turnId,
|
|
469
|
+
mayDelegate: spec.relay.mayDelegate ?? false, mayAsk: spec.relay.mayAsk ?? false,
|
|
470
|
+
}, ports.broker);
|
|
471
|
+
}
|
|
472
|
+
const baseline = d.interfaces();
|
|
473
|
+
const rawAllowed = Boolean(spec.grant?.raw_network || spec.overlay?.request.capabilities.some(c => c.kind === "network" && c.mode === "raw"));
|
|
474
|
+
const blockBindAllowed = rawAllowed || exact.some(c => c.operation === "listen") || Boolean(spec.overlay?.request.capabilities.some(c => c.kind === "port0"));
|
|
475
|
+
let net = {
|
|
476
|
+
mode: "stage2",
|
|
477
|
+
deniedTcp,
|
|
478
|
+
deniedUdp: [...STATIC_DENIED_UDP],
|
|
479
|
+
// Without listener authority the reserved block is denied like the rest of relay's range.
|
|
480
|
+
// With authority it is carved out and carried by bindPorts below.
|
|
481
|
+
relayRange: blockBindAllowed ? [...ports.range] : [...ports.range, ...ports.block],
|
|
482
|
+
execPort: ports.exec,
|
|
483
|
+
bindPorts: blockBindAllowed ? [...ports.block] : [],
|
|
484
|
+
// Unix sockets in the worktree only for write turns in a worktree relay created and pinned (review C4).
|
|
485
|
+
worktreeSockets: spec.access === "write" && Boolean(spec.worktreePin),
|
|
486
|
+
egress: rawAllowed ? "raw" : "none",
|
|
487
|
+
...(spec.overlay ? {
|
|
488
|
+
egress: rawAllowed ? "raw" : "none",
|
|
489
|
+
connectPorts: exact.filter(c => c.operation === "connect").map(c => c.port),
|
|
490
|
+
listenPorts: exact.filter(c => c.operation === "listen").map(c => c.port),
|
|
491
|
+
broadBind: spec.overlay.request.capabilities.some(c => c.kind === "port0"),
|
|
492
|
+
} : {}),
|
|
493
|
+
};
|
|
494
|
+
if (wantsMediated) {
|
|
495
|
+
const selected = mediatedNetworkProvider();
|
|
496
|
+
if (!selected)
|
|
497
|
+
throw new Error(`capability_unavailable: ${NO_MEDIATED_PROVIDER}`);
|
|
498
|
+
if (!spec.neverPolicy)
|
|
499
|
+
throw new Error("mediated launch has no checked never policy");
|
|
500
|
+
const context = { capabilities: overlayCaps, baseline: networkBaseline, policy: spec.neverPolicy };
|
|
501
|
+
const unavailable = selected.qualify(context);
|
|
502
|
+
if (unavailable)
|
|
503
|
+
throw new Error(`capability_unavailable: ${unavailable}`);
|
|
504
|
+
mediated = await selected.prepare({ ...context, spec, env: checked, tmpDir, network: net, proxyPort: ports.proxy });
|
|
505
|
+
if (spec.overlay)
|
|
506
|
+
assertOverlayLaunch(spec.overlay, spec.turnId);
|
|
507
|
+
if (!rawAllowed && mediated.network.egress !== "none")
|
|
508
|
+
throw new Error("mediated provider must not substitute raw network");
|
|
509
|
+
net = mediated.network;
|
|
510
|
+
spec.onNote?.(`Codex network: stage-2 mediated. ${MEDIATED_NETWORK_EFFECT}`);
|
|
511
|
+
}
|
|
512
|
+
const plan = d.plan(spec, net);
|
|
513
|
+
const brokerReadback = broker ? {
|
|
514
|
+
url: broker.url, toolTimeoutS: codexToolTimeoutS(spec.config.approval_timeout_s),
|
|
515
|
+
listTools: () => broker.listTools(), sessionsServed: () => broker.sessionsServed(),
|
|
516
|
+
} : undefined;
|
|
517
|
+
if (brokerReadback)
|
|
518
|
+
plan.broker = { url: brokerReadback.url, toolTimeoutS: brokerReadback.toolTimeoutS };
|
|
519
|
+
const policy = d.policy(spec, tmpDir, net);
|
|
520
|
+
const made = executorToken();
|
|
521
|
+
token = made.token;
|
|
522
|
+
const args = ["exec-server", "--listen", `ws://127.0.0.1:${ports.exec}`, "--ws-auth", "capability-token", "--ws-token-sha256", made.hash];
|
|
523
|
+
const env = { ...executorEnv(checked, tmpDir, ports, blockBindAllowed), ...mediated?.executorEnv };
|
|
524
|
+
const { launch, probes } = await probe(mechanism, policy, bin, args, spec.signal, env);
|
|
525
|
+
if (spec.overlay)
|
|
526
|
+
assertOverlayLaunch(spec.overlay, spec.turnId);
|
|
527
|
+
ex = await d.launch({ spec, bin, launch, probes, ports, hash: made.hash, env, tmpDir, signal: spec.signal, mechanism });
|
|
528
|
+
// Only the app server receives the broker secret. Merge both proxy spellings so neither
|
|
529
|
+
// reqwest's choice of case nor an inherited proxy can divert this credential (§5.1, §5.8).
|
|
530
|
+
const bypass = broker ? [...new Set([
|
|
531
|
+
...[checked.NO_PROXY, checked.no_proxy].flatMap((v) => (v ?? "").split(",").map((v) => v.trim()).filter(Boolean)),
|
|
532
|
+
"127.0.0.1",
|
|
533
|
+
])].join(",") : undefined;
|
|
534
|
+
const appEnv = Object.freeze({ ...stage2AppServerEnv({ ...checked, ...mediated?.appEnv }), ...broker?.env,
|
|
535
|
+
...(bypass ? { NO_PROXY: bypass, no_proxy: bypass } : {}),
|
|
536
|
+
});
|
|
537
|
+
const executor = ex;
|
|
538
|
+
const session = {
|
|
539
|
+
bindAppServer(pid) {
|
|
540
|
+
if (!broker)
|
|
541
|
+
return;
|
|
542
|
+
const start = d.appStart(pid, { fresh: true });
|
|
543
|
+
if (!start)
|
|
544
|
+
throw new Error("relay could not read the identity of Codex's app server");
|
|
545
|
+
broker.bindAppServer({ pid, start });
|
|
546
|
+
},
|
|
547
|
+
sessionsServed: () => broker?.sessionsServed() ?? 0,
|
|
548
|
+
threadMcpProblem: (rpc, eff, threadId, checkpoint) => stage2ThreadMcpProblem(rpc, eff, threadId, brokerReadback, checkpoint),
|
|
549
|
+
armTools(threadId, nativeTurn) {
|
|
550
|
+
if (spec.relay && threadId && nativeTurn && !releaseEscalation) {
|
|
551
|
+
const node = getNode(spec.relay.nodeId), turn = getTurn(spec.relay.turnId);
|
|
552
|
+
if (!node || !turn || turn.node_id !== node.id || turn.status !== "running")
|
|
553
|
+
throw new Error("escalation source turn is unavailable");
|
|
554
|
+
const deadline = Math.min(spec.overlay?.recoveryDeadline ?? Infinity, spec.config.job_timeout_s === null ? Infinity : (node.started_at ?? node.created_at) + spec.config.job_timeout_s * 1000, spec.config.turn_timeout_s === null ? Infinity : (turn.started_at ?? turn.created_at) + spec.config.turn_timeout_s * 1000);
|
|
555
|
+
const binding = createEscalationRuntime({ nodeId: node.id, turnId: turn.id,
|
|
556
|
+
baselineGrant: spec.grant,
|
|
557
|
+
snapshot: { native_thread: threadId, native_turn: nativeTurn,
|
|
558
|
+
base_policy_hash: escalationDigest(policy), launch_plan: escalationDigest(plan),
|
|
559
|
+
overlay_budget_s: Math.min(3600, (deadline - Date.now()) / 1000),
|
|
560
|
+
...(Number.isFinite(deadline) ? { deadline_ms: deadline } : {}),
|
|
561
|
+
},
|
|
562
|
+
});
|
|
563
|
+
const unregister = registerEscalationRuntime(node.id, turn.id, binding.runtime);
|
|
564
|
+
releaseEscalation = () => { unregister(); binding.dispose(); };
|
|
565
|
+
}
|
|
566
|
+
broker?.arm();
|
|
567
|
+
},
|
|
568
|
+
disarmTools: () => broker?.disarm(),
|
|
569
|
+
async bind(rpc) {
|
|
570
|
+
const secret = token;
|
|
571
|
+
if (!secret)
|
|
572
|
+
return "relay's executor for this turn is no longer available, so it stopped the agent.";
|
|
573
|
+
try {
|
|
574
|
+
await rpc.request("environment/add", { environmentId: STAGE2_ENVIRONMENT_ID, execServerUrl: executor.url, authBearerToken: secret });
|
|
575
|
+
return null;
|
|
576
|
+
}
|
|
577
|
+
catch (err) {
|
|
578
|
+
// Codex warns that executor connection errors can carry credentials (review C9): cut the token.
|
|
579
|
+
return `relay could not bind Codex's thread to its sandboxed executor (environment/add: ${redact(err.message).replaceAll(secret, "[redacted]")}), so it stopped the agent.`;
|
|
580
|
+
}
|
|
581
|
+
},
|
|
582
|
+
// hooks/list in the turn's directory, as the turn will see it.
|
|
583
|
+
readback: (rpc, eff) => stage2Readback(rpc, eff, spec.cwd, brokerReadback, Object.keys(plan.features)),
|
|
584
|
+
threadParams: (cwd) => stage2ThreadParams(cwd, STAGE2_ENVIRONMENT_ID),
|
|
585
|
+
async resumeProblem(rpc, threadId) {
|
|
586
|
+
const queued = await stage2QueueProblem(rpc, threadId);
|
|
587
|
+
if (!queued && testHook)
|
|
588
|
+
await testHook("queue-checked", { rpc, threadId });
|
|
589
|
+
return queued;
|
|
590
|
+
},
|
|
591
|
+
threadProblem(t, cwd, resumed) {
|
|
592
|
+
void testHook?.("thread-reply", { reply: t, resumed });
|
|
593
|
+
return stage2ThreadProblem(t, cwd, STAGE2_ENVIRONMENT_ID, { resumed });
|
|
594
|
+
},
|
|
595
|
+
turnParams: (cwd) => stage2TurnParams(cwd, STAGE2_ENVIRONMENT_ID),
|
|
596
|
+
async turnProblem(rpc, threadId, cwd) {
|
|
597
|
+
const unbound = await d.turnReadback(rpc, threadId, cwd, STAGE2_ENVIRONMENT_ID);
|
|
598
|
+
if (!unbound && testHook)
|
|
599
|
+
await testHook("turn-bound", { rpc, threadId });
|
|
600
|
+
return unbound;
|
|
601
|
+
},
|
|
602
|
+
guard: new Stage2TurnGuard(),
|
|
603
|
+
approvalProblem(kind, environmentId) {
|
|
604
|
+
if (spec.markers && spec.turnId !== undefined) {
|
|
605
|
+
const problem = overlayTurnProblem(spec.markers.nodeId, spec.turnId);
|
|
606
|
+
if (problem)
|
|
607
|
+
return problem;
|
|
608
|
+
}
|
|
609
|
+
// The watch is set up when the app server starts (watch): without it, relay cannot say the addresses held.
|
|
610
|
+
if (!iw)
|
|
611
|
+
return "relay stopped the Codex agent with raw network and failed the turn, because it was not watching this Mac's network addresses when an approval came in.";
|
|
612
|
+
const moved = iw.changed();
|
|
613
|
+
if (moved)
|
|
614
|
+
return `relay stopped the Codex agent with raw network and failed the turn, because ${moved}.`;
|
|
615
|
+
return kind === "command" ? stage2CommandEnvironmentProblem(environmentId, STAGE2_ENVIRONMENT_ID) : null;
|
|
616
|
+
},
|
|
617
|
+
approvalDetail: (kind) => stage2ApprovalDetail(kind),
|
|
618
|
+
env: appEnv,
|
|
619
|
+
watch(fail) {
|
|
620
|
+
let failed = false;
|
|
621
|
+
const once = (error) => {
|
|
622
|
+
if (closing || failed)
|
|
623
|
+
return;
|
|
624
|
+
failed = true;
|
|
625
|
+
broker?.disarm();
|
|
626
|
+
fail(error);
|
|
627
|
+
};
|
|
628
|
+
if (spec.overlay) {
|
|
629
|
+
const abort = () => once("overlay deadline expired or launch cancelled; stopping app server before executor");
|
|
630
|
+
spec.signal?.addEventListener("abort", abort, { once: true });
|
|
631
|
+
if (spec.signal?.aborted)
|
|
632
|
+
abort();
|
|
633
|
+
void executor.exited.finally(() => spec.signal?.removeEventListener("abort", abort));
|
|
634
|
+
}
|
|
635
|
+
void executor.exited.then(({ code, signal }) => {
|
|
636
|
+
const said = running.get(executor);
|
|
637
|
+
once(`relay's sandboxed executor for this Codex turn's commands exited during the turn (${signal ?? `code ${code}`}), so relay stopped the agent and failed the turn.${said ? ` ${lastWords(said.tail)}` : ""}`.trim());
|
|
638
|
+
});
|
|
639
|
+
broker?.onFatal((why) => once(`${why}; relay stopped the Codex app server and failed the turn.`));
|
|
640
|
+
try {
|
|
641
|
+
iw = d.watchInterfaces(baseline);
|
|
642
|
+
iw.onChange((what) => once(`this Mac's network addresses changed during the turn (${what}); relay's network rules for this turn were not checked against them, so it stopped the agent and failed the turn.`));
|
|
643
|
+
}
|
|
644
|
+
catch (err) {
|
|
645
|
+
once(`relay could not watch this Mac's network addresses during the turn (${err.message}), so it stopped the agent.`);
|
|
646
|
+
}
|
|
647
|
+
},
|
|
648
|
+
teardown: (appCleanup) => close(appCleanup?.refused ? `untraceable app-server cleanup: ${appCleanup.refused}` : appCleanup?.uncertain ? "untraceable app-server cleanup: keep executor record and leases quarantined" : appCleanup?.survivors.length ? "app-server descendants remain; keep executor record and leases quarantined" : undefined, appCleanup?.descendants, appCleanup?.refused),
|
|
649
|
+
};
|
|
650
|
+
return { plan, session, abort: async () => void (await close()) };
|
|
651
|
+
}
|
|
652
|
+
catch (err) {
|
|
653
|
+
await close();
|
|
654
|
+
throw err;
|
|
655
|
+
}
|
|
656
|
+
}
|