@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,651 @@
|
|
|
1
|
+
/** Restart-only, one-destination capabilities. Authority decisions belong to WP3, never to this module. */
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { canonical, foldedPath } from "./config.js";
|
|
7
|
+
import { cancelledAbove, db, getNode, getRun, getTurn, insertTurn, runConfig, tx, workCwd } from "./db.js";
|
|
8
|
+
import { assertNeverCurrent, checkpointNever } from "./never-checkpoint.js";
|
|
9
|
+
import { neverDigest, neverEndpointProblem, neverGrantProblem, neverPathIdentity } from "./never-policy.js";
|
|
10
|
+
import { sessionParams } from "./params.js";
|
|
11
|
+
import { pidAlive, procStart, sameProcess } from "./procs.js";
|
|
12
|
+
import { stage2Enabled } from "./stage2-switch.js";
|
|
13
|
+
import { assertEscalationConsumable, recordEscalationOutcome } from "./escalation-state.js";
|
|
14
|
+
import { mediatedBaseline, mediatedNetworkProblem, MEDIATED_NETWORK_EFFECT } from "./mediated-network-provider.js";
|
|
15
|
+
import { localhostConnectEffect, localhostListenEffect, PORT0_APPROVAL_TEXT } from "./escalation-policy.js";
|
|
16
|
+
import { executorRecords } from "./stage2-executors.js";
|
|
17
|
+
import { listeningSnapshot } from "../confine/ports.js";
|
|
18
|
+
import { STATIC_DENIED_TCP } from "../confine/port-policy.js";
|
|
19
|
+
let controlSnapshot = listeningSnapshot;
|
|
20
|
+
/** Code-only fixture seam; no configuration or child environment can replace the snapshot. */
|
|
21
|
+
export function setOverlayControlSnapshotForTests(snapshot) { controlSnapshot = snapshot ?? listeningSnapshot; }
|
|
22
|
+
/** Before prompting, reject static service ports and control listeners outside relay's reserved range. */
|
|
23
|
+
export function localhostControlProblem(capabilities) {
|
|
24
|
+
const local = capabilities.filter(c => c.kind === "localhost" && (c.operation === "connect" || c.operation === "listen"));
|
|
25
|
+
const staticallyDenied = local.find(c => STATIC_DENIED_TCP.includes(c.port));
|
|
26
|
+
if (staticallyDenied)
|
|
27
|
+
return `never relay.authority: static denied TCP port ${staticallyDenied.port} cannot be a localhost ${staticallyDenied.operation} exception`;
|
|
28
|
+
if (!local.length)
|
|
29
|
+
return null;
|
|
30
|
+
try {
|
|
31
|
+
const snapshot = controlSnapshot();
|
|
32
|
+
return local.some(c => snapshot.controlPlanes.includes(c.port)) ? "never relay.authority: control listeners cannot be localhost exceptions" : null;
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
return `capability_unavailable: relay cannot check localhost control listeners (${err.message})`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export const OVERLAY_DEFAULT_BUDGET_MS = 3_600_000;
|
|
39
|
+
const Port = z.number().int().min(1).max(65535);
|
|
40
|
+
export const OverlayCapabilitySchema = z.discriminatedUnion("kind", [
|
|
41
|
+
z.object({ kind: z.literal("localhost"), operation: z.enum(["connect", "listen"]), port: Port, host: z.literal("127.0.0.1").default("127.0.0.1") }).strict(),
|
|
42
|
+
z.object({ kind: z.literal("port0") }).strict(),
|
|
43
|
+
z.object({ kind: z.literal("write"), root: z.string().min(1).max(4096), scope: z.enum(["file", "tree"]) }).strict(),
|
|
44
|
+
z.object({ kind: z.literal("network"), mode: z.enum(["mediated", "raw", "local"]) }).strict(),
|
|
45
|
+
]);
|
|
46
|
+
// Kernel monotonic time is comparable across workers. Boot fingerprint detects reboot/clock jumps.
|
|
47
|
+
const monotonicMs = () => Number(process.hrtime.bigint() / 1000000n);
|
|
48
|
+
const REAL_CLOCK = { wall: Date.now, mono: monotonicMs, boot: () => String(Math.round((Date.now() - monotonicMs()) / 10_000)) };
|
|
49
|
+
let clock = REAL_CLOCK;
|
|
50
|
+
export function setOverlayClockForTests(c) { clock = c ?? REAL_CLOCK; }
|
|
51
|
+
const active = (r) => !["expired", "denied", "cancelled", "failed"].includes(r.state);
|
|
52
|
+
const overlap = (a, b) => { a = foldedPath(a); b = foldedPath(b); return a === b || a.startsWith(b + "/") || b.startsWith(a + "/"); };
|
|
53
|
+
const clone = (x) => JSON.parse(JSON.stringify(x));
|
|
54
|
+
function records(runId) {
|
|
55
|
+
const run = getRun(runId);
|
|
56
|
+
if (!run)
|
|
57
|
+
throw new Error("overlay run is unavailable");
|
|
58
|
+
const saved = JSON.parse(run.config_json).params_json?.overlays;
|
|
59
|
+
if (saved === undefined)
|
|
60
|
+
return {};
|
|
61
|
+
if (!saved || typeof saved !== "object" || Array.isArray(saved))
|
|
62
|
+
throw new Error("malformed overlay records");
|
|
63
|
+
for (const [id, r] of Object.entries(saved)) {
|
|
64
|
+
if (!r || r.v !== 1 || r.id !== id || r.runId !== runId || !r.nodeId || !Number.isSafeInteger(r.originTurn)
|
|
65
|
+
|| !["requested", "pending_restart", "stopping", "ready", "running", "tainted", "expired", "denied", "cancelled", "failed"].includes(r.state)
|
|
66
|
+
|| !r.request || neverDigest(r.request) !== r.digest || !Number.isFinite(r.budgetMs) || r.budgetMs <= 0
|
|
67
|
+
|| !r.owner || !Number.isInteger(r.owner.pid) || typeof r.owner.start !== "string"
|
|
68
|
+
|| (r.state === "running" && (!Number.isSafeInteger(r.destinationTurn) || !Number.isFinite(r.monotonicDeadline) || !Number.isFinite(r.recoveryDeadline))))
|
|
69
|
+
throw new Error(`malformed overlay ${id}`);
|
|
70
|
+
}
|
|
71
|
+
return saved;
|
|
72
|
+
}
|
|
73
|
+
function save(r) {
|
|
74
|
+
const all = records(r.runId);
|
|
75
|
+
all[r.id] = r;
|
|
76
|
+
db().prepare("UPDATE runs SET config_json = json_set(config_json, '$.params_json.overlays', json(?)) WHERE id = ?").run(JSON.stringify(all), r.runId);
|
|
77
|
+
const bound = r.request.escalation;
|
|
78
|
+
if (bound && db().prepare("SELECT status FROM requests WHERE id = ?").get(bound.requestId)?.status === "allowed") {
|
|
79
|
+
const terminal = ["failed", "expired", "cancelled", "tainted"].includes(r.state);
|
|
80
|
+
const status = r.destinationTurn === undefined ? undefined : getTurn(r.destinationTurn)?.status;
|
|
81
|
+
recordEscalationOutcome(bound.requestId, {
|
|
82
|
+
destination_turn: r.destinationTurn ?? null, launched: r.launchedAt !== undefined,
|
|
83
|
+
outcome: r.state === "tainted" ? "tainted" : r.state === "running" ? r.launchedAt === undefined ? "pending" : "running"
|
|
84
|
+
: !terminal ? "pending" : r.outcome?.includes("deadline") ? "expired" : status === "done" && r.launchedAt !== undefined ? "completed"
|
|
85
|
+
: status === "cancelled" || r.outcome?.includes("cancelled") ? "cancelled" : "failed",
|
|
86
|
+
cleanup: r.cleanup ? r.cleanup.survivors?.length ? "survivors" : r.cleanup.clean ? "verified" : "uncertain" : "not_started",
|
|
87
|
+
effective_scope: `Captured baseline grant: ${getTurn(r.originTurn)?.grant_json ?? "null"}. Approved additions: ${JSON.stringify(r.request.capabilities)}; ${r.request.disclosures.join(" ")}`,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return clone(r);
|
|
91
|
+
}
|
|
92
|
+
function assertHandoffOwner(r) {
|
|
93
|
+
const start = clock.owner?.().start ?? procStart(process.pid);
|
|
94
|
+
if (r.owner.pid !== process.pid || !start || r.owner.start !== start)
|
|
95
|
+
throw new Error("overlay supervisor restarted during handoff; never replay a launch");
|
|
96
|
+
}
|
|
97
|
+
function assertAuthority(r, policy) {
|
|
98
|
+
const binding = r.request.escalation;
|
|
99
|
+
if (binding)
|
|
100
|
+
assertEscalationConsumable(binding.requestId, binding.digest, policy.hash);
|
|
101
|
+
}
|
|
102
|
+
export function getOverlay(runId, id) {
|
|
103
|
+
const r = records(runId)[id];
|
|
104
|
+
if (!r)
|
|
105
|
+
throw new Error("overlay is unavailable");
|
|
106
|
+
return clone(r);
|
|
107
|
+
}
|
|
108
|
+
function nodeFor(r) {
|
|
109
|
+
const n = getNode(r.nodeId);
|
|
110
|
+
if (!n || n.run_id !== r.runId || cancelledAbove(n, { self: true }))
|
|
111
|
+
throw new Error("overlay owner was cancelled or lost");
|
|
112
|
+
return n;
|
|
113
|
+
}
|
|
114
|
+
function grantHash(n) { return neverDigest(sessionParams(n).grant ?? null); }
|
|
115
|
+
function liveOrigin(r) {
|
|
116
|
+
const n = nodeFor(r), t = getTurn(r.originTurn);
|
|
117
|
+
if (n.status !== "running" || t?.node_id !== n.id || t.status !== "running")
|
|
118
|
+
throw new Error("overlay origin is no longer live");
|
|
119
|
+
}
|
|
120
|
+
function validatePlan(r, policy) {
|
|
121
|
+
const n = nodeFor(r);
|
|
122
|
+
if (grantHash(n) !== r.request.grantHash)
|
|
123
|
+
throw new Error("overlay baseline grant changed; request a fresh decision");
|
|
124
|
+
if (policy.hash !== r.request.neverHash)
|
|
125
|
+
throw new Error("overlay never policy changed; request a fresh decision");
|
|
126
|
+
assertNeverCurrent(r.runId, r.request.neverHash);
|
|
127
|
+
const baseline = neverGrantProblem(policy, sessionParams(n).grant ?? {}, n.mode === "write" ? [workCwd(n)] : []);
|
|
128
|
+
if (baseline)
|
|
129
|
+
throw new Error(baseline);
|
|
130
|
+
const mediated = mediatedNetworkProblem({ capabilities: r.request.capabilities, baseline: mediatedBaseline(sessionParams(n).grant, runConfig(n.run_id)), policy });
|
|
131
|
+
if (mediated)
|
|
132
|
+
throw new Error(`capability_unavailable: ${mediated}`);
|
|
133
|
+
const controls = localhostControlProblem(r.request.capabilities);
|
|
134
|
+
if (controls)
|
|
135
|
+
throw new Error(controls);
|
|
136
|
+
for (const c of r.request.capabilities) {
|
|
137
|
+
const why = c.kind === "localhost" ? neverEndpointProblem(policy, { host: "127.0.0.1", port: c.port, transport: "tcp", operation: c.operation })
|
|
138
|
+
: c.kind === "write" ? neverGrantProblem(policy, { write_paths: [c.root] })
|
|
139
|
+
: c.kind === "network" ? neverGrantProblem(policy, { raw_network: c.mode !== "mediated" }) : null;
|
|
140
|
+
if (why)
|
|
141
|
+
throw new Error(why);
|
|
142
|
+
}
|
|
143
|
+
for (const root of r.request.roots)
|
|
144
|
+
assertOverlayRoot(root);
|
|
145
|
+
}
|
|
146
|
+
/** Strict namespace/identity pins include absence; a newly created root requires a fresh decision. */
|
|
147
|
+
export function assertOverlayRoot(root) {
|
|
148
|
+
if (canonical(root.root) !== root.root || neverDigest(neverPathIdentity(root.root)) !== neverDigest(root.pins))
|
|
149
|
+
throw new Error(`overlay write root identity changed: ${root.root}`);
|
|
150
|
+
const st = fs.lstatSync(root.root, { throwIfNoEntry: false });
|
|
151
|
+
if (Boolean(st) !== root.existed || (st && (st.isSymbolicLink() || (root.scope === "file" ? !st.isFile() || st.nlink !== 1 : !st.isDirectory()))))
|
|
152
|
+
throw new Error(`overlay write root identity changed: ${root.root}`);
|
|
153
|
+
}
|
|
154
|
+
/** WP3 calls after trusted normalization/identity derivation; no approval or widening occurs here. */
|
|
155
|
+
export function createOverlayRequest(input) {
|
|
156
|
+
const n = getNode(input.nodeId), turn = getTurn(input.originTurn);
|
|
157
|
+
if (!n || turn?.node_id !== n.id || turn.status !== "running" || n.status !== "running")
|
|
158
|
+
throw new Error("overlay origin is not live");
|
|
159
|
+
if (n.target !== "codex")
|
|
160
|
+
throw new Error("capability_unavailable: restart overlays require the qualified Codex executor");
|
|
161
|
+
const config = JSON.parse(getRun(n.run_id).config_json);
|
|
162
|
+
if (!stage2Enabled(process.env, config) || config.codex_backend === "exec")
|
|
163
|
+
throw new Error("capability_unavailable: overlays require enabled stage2 app-server execution");
|
|
164
|
+
const capabilities = z.array(OverlayCapabilitySchema).min(1).max(32).parse(input.capabilities);
|
|
165
|
+
const authority = z.object({ owningRoot: z.string().min(1).max(256), tokenBinding: z.string().min(1).max(512), required_decider: z.enum(["owning_run_root", "person"]) }).strict().parse(input.authority);
|
|
166
|
+
const reason = z.string().min(1).max(8192).parse(input.reason);
|
|
167
|
+
const continuation = z.string().max(262144).parse(input.continuation);
|
|
168
|
+
const intended = input.intended === undefined ? undefined : z.object({ argv: z.array(z.string().max(65536)).max(1024).optional(), script: z.string().max(262144).optional(), cwd: z.string().max(4096).optional() }).strict().parse(input.intended);
|
|
169
|
+
if (!input.authority?.owningRoot || !input.authority.tokenBinding || !["owning_run_root", "person"].includes(input.authority.required_decider))
|
|
170
|
+
throw new Error("overlay requires authenticated root identity bindings");
|
|
171
|
+
if (capabilities.some(c => c.kind === "network" && ["raw", "local"].includes(c.mode)) && input.authority.required_decider !== "person")
|
|
172
|
+
throw new Error("raw/local overlay requires a person decision");
|
|
173
|
+
const policy = checkpointNever(n.run_id, "approval");
|
|
174
|
+
const mediated = mediatedNetworkProblem({ capabilities, baseline: mediatedBaseline(sessionParams(n).grant, config), policy });
|
|
175
|
+
if (mediated)
|
|
176
|
+
throw new Error(`capability_unavailable: ${mediated}`);
|
|
177
|
+
const roots = [];
|
|
178
|
+
const controls = localhostControlProblem(capabilities);
|
|
179
|
+
if (controls)
|
|
180
|
+
throw new Error(controls);
|
|
181
|
+
for (const c of capabilities) {
|
|
182
|
+
if (c.kind === "network" && c.mode === "local")
|
|
183
|
+
throw new Error("capability_unavailable: no qualified local overlay provider");
|
|
184
|
+
if (c.kind === "write") {
|
|
185
|
+
if (!path.isAbsolute(c.root) || path.normalize(c.root) !== c.root || canonical(c.root) !== c.root || overlap(c.root, n.worktree ?? n.cwd))
|
|
186
|
+
throw new Error("capability_unavailable: write root must be an exact canonical root outside the worktree");
|
|
187
|
+
const root = { root: c.root, scope: c.scope, pins: neverPathIdentity(c.root), existed: fs.existsSync(c.root) };
|
|
188
|
+
assertOverlayRoot(root);
|
|
189
|
+
roots.push(root);
|
|
190
|
+
if (c.scope === "tree" && !root.existed)
|
|
191
|
+
throw new Error("capability_unavailable: a writable tree must already exist as a pinned real directory");
|
|
192
|
+
if (c.scope === "file" && (!fs.statSync(path.dirname(c.root)).isDirectory() || c.root.split(path.sep).some(p => /^\.(git|claude|codex|agents|gemini|mcp|envrc|vscode|bashrc|zshrc|gitconfig)(\.|$)/.test(p))))
|
|
193
|
+
throw new Error("capability_unavailable: exact file overlaps protected tool/git configuration");
|
|
194
|
+
if (turn.access !== "write")
|
|
195
|
+
throw new Error("capability_unavailable: read jobs require an explicit access change before outside writes");
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const cfg = runConfig(n.run_id);
|
|
199
|
+
const configuredLimit = n.kind === "job" ? cfg.job_timeout_s : cfg.turn_timeout_s;
|
|
200
|
+
const configuredRemaining = configuredLimit === null ? Infinity : Math.max(0, configuredLimit * 1000 - Math.max(0, clock.wall() - (n.kind === "job" ? n.started_at ?? n.created_at : turn.started_at ?? turn.created_at)));
|
|
201
|
+
const remainingMs = Math.min(input.remainingMs ?? Infinity, configuredRemaining);
|
|
202
|
+
const budgetMs = Math.min(OVERLAY_DEFAULT_BUDGET_MS, remainingMs);
|
|
203
|
+
const pendingMs = Math.min(input.pendingMs ?? (2 * cfg.approval_timeout_s + 300) * 1000, remainingMs);
|
|
204
|
+
if (!Number.isFinite(budgetMs) || budgetMs <= 0 || !Number.isFinite(pendingMs) || pendingMs <= 0)
|
|
205
|
+
throw new Error("overlay deadline already expired");
|
|
206
|
+
const at = clock.wall(), start = clock.owner?.().start ?? procStart(process.pid);
|
|
207
|
+
if (!start)
|
|
208
|
+
throw new Error("overlay supervisor identity unavailable");
|
|
209
|
+
const request = {
|
|
210
|
+
capabilities, disclosures: ["The addition applies to every operation and descendant in one replacement turn. The command runs worktree code that the agent can edit after approval.", ...capabilities.flatMap(c => c.kind === "port0" ? [`${PORT0_APPROVAL_TEXT} It adds no outbound network.`]
|
|
211
|
+
: c.kind === "localhost" && c.operation === "listen" ? [localhostListenEffect(c.port)]
|
|
212
|
+
: c.kind === "localhost" && c.operation === "connect" ? [localhostConnectEffect(c.port)]
|
|
213
|
+
: c.kind === "write" ? [`Writes to exact ${c.scope} ${c.root} persist after the overlay ends.`]
|
|
214
|
+
: c.kind === "network" ? [c.mode === "raw" ? "Raw network adds direct TCP/UDP/DNS and LAN/tailnet reachability, including forwarding and exfiltration effects; person-only authority."
|
|
215
|
+
: MEDIATED_NETWORK_EFFECT] : [])], reason, continuation, authority, ...(intended ? { intended } : {}),
|
|
216
|
+
grantHash: grantHash(n), neverHash: policy.hash, access: turn.access,
|
|
217
|
+
originConfinement: sessionParams(n).codex_confinement?.turn === turn.id && sessionParams(n).codex_confinement?.enforcement === "relay" ? "relay" : "native",
|
|
218
|
+
roots, createdAt: at, pendingDeadline: at + pendingMs, pendingMonotonicDeadline: clock.mono() + pendingMs, clockBoot: clock.boot(), budgetMs,
|
|
219
|
+
...(Number.isFinite(remainingMs) ? { hardDeadline: at + remainingMs } : {}),
|
|
220
|
+
...(input.escalation ? { escalation: clone(input.escalation) } : {}),
|
|
221
|
+
};
|
|
222
|
+
const r = { v: 1, id: `ov_${crypto.randomBytes(8).toString("hex")}`, runId: n.run_id, nodeId: n.id, originTurn: turn.id, state: "requested", request, digest: neverDigest(request), budgetMs, owner: { pid: process.pid, start } };
|
|
223
|
+
return tx(() => {
|
|
224
|
+
if (Object.values(records(n.run_id)).some(x => x.nodeId === n.id && active(x) && !(x.state === "running" && x.destinationTurn === turn.id)))
|
|
225
|
+
throw new Error("overlay already pending or active for this node");
|
|
226
|
+
liveOrigin(r);
|
|
227
|
+
validatePlan(r, policy);
|
|
228
|
+
return save(r);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
function pendingCurrent(r) {
|
|
232
|
+
if (r.request.clockBoot !== clock.boot() || clock.mono() >= r.request.pendingMonotonicDeadline || clock.wall() < r.request.createdAt || clock.wall() >= r.request.pendingDeadline || (r.request.hardDeadline !== undefined && clock.wall() >= r.request.hardDeadline))
|
|
233
|
+
throw new Error("overlay approval/restart deadline expired");
|
|
234
|
+
}
|
|
235
|
+
/** WP3 MUST authenticate decider and commit its terminal decision in the SAME registry transaction.
|
|
236
|
+
* Call checkpointNever outside that transaction, then this function inside it. */
|
|
237
|
+
export function allowOverlayInTransaction(runId, id, digest, decision) {
|
|
238
|
+
const r = getOverlay(runId, id);
|
|
239
|
+
if (r.state !== "requested" || r.digest !== digest)
|
|
240
|
+
throw new Error("overlay decision already terminal or digest changed");
|
|
241
|
+
liveOrigin(r);
|
|
242
|
+
pendingCurrent(r);
|
|
243
|
+
validatePlan(r, assertNeverCurrent(runId, r.request.neverHash));
|
|
244
|
+
if (!decision.decisionId || !decision.decidedBy || !Number.isFinite(decision.at) || !["person", "supervisor"].includes(decision.route))
|
|
245
|
+
throw new Error("overlay missing decision provenance");
|
|
246
|
+
if (r.request.authority.required_decider === "person" && decision.route !== "person")
|
|
247
|
+
throw new Error("overlay requires a person decision");
|
|
248
|
+
if (decision.route === "supervisor" && decision.decidedBy !== r.request.authority.owningRoot)
|
|
249
|
+
throw new Error("overlay supervisor is not the owning root");
|
|
250
|
+
r.decision = clone(decision);
|
|
251
|
+
r.state = "pending_restart";
|
|
252
|
+
r.receipt = { status: "restart_pending", id: r.id, digest, effects: clone(r.request.capabilities), disclosures: clone(r.request.disclosures), decision: clone(decision) };
|
|
253
|
+
return save(r);
|
|
254
|
+
}
|
|
255
|
+
/** Standalone wrapper; WP3 uses the InTransaction form for atomic competing decisions. */
|
|
256
|
+
export function allowOverlay(runId, id, digest, decision) {
|
|
257
|
+
checkpointNever(runId, "decision");
|
|
258
|
+
return tx(() => allowOverlayInTransaction(runId, id, digest, decision));
|
|
259
|
+
}
|
|
260
|
+
export function markOverlayReceipt(runId, id, delivered) {
|
|
261
|
+
return tx(() => { const r = getOverlay(runId, id); if (r.state !== "pending_restart" || !r.receipt || r.receipt.delivered !== undefined)
|
|
262
|
+
throw new Error("overlay receipt already settled"); r.receipt.delivered = delivered; r.state = "stopping"; if (!delivered)
|
|
263
|
+
r.outcome = "receipt delivery failed; allowed, not executed"; return save(r); });
|
|
264
|
+
}
|
|
265
|
+
/** WP3's atomic deny/cancel loser protection; never silently overwrite an allow. */
|
|
266
|
+
export function settleOverlayRequestInTransaction(runId, id, digest, outcome, decision) {
|
|
267
|
+
const r = getOverlay(runId, id);
|
|
268
|
+
if (r.digest !== digest || r.state !== "requested")
|
|
269
|
+
throw new Error("overlay decision already terminal or digest changed");
|
|
270
|
+
r.state = outcome;
|
|
271
|
+
if (decision)
|
|
272
|
+
r.decision = clone(decision);
|
|
273
|
+
return save(r);
|
|
274
|
+
}
|
|
275
|
+
export function originOverlay(nodeId, turnId) {
|
|
276
|
+
const n = getNode(nodeId);
|
|
277
|
+
if (!n)
|
|
278
|
+
return null;
|
|
279
|
+
return Object.values(records(n.run_id)).find(r => r.nodeId === nodeId && r.originTurn === turnId && active(r) && r.state !== "requested") ?? null;
|
|
280
|
+
}
|
|
281
|
+
/** Called only after handle.done, whose executor teardown/sweep has finished. No automatic crash replay. */
|
|
282
|
+
export function prepareOverlayRestart(runId, id, cleanup) {
|
|
283
|
+
const policy = checkpointNever(runId, "launch");
|
|
284
|
+
return tx(() => {
|
|
285
|
+
const r = getOverlay(runId, id);
|
|
286
|
+
if (!["pending_restart", "stopping"].includes(r.state) || !r.receipt || getTurn(r.originTurn)?.status === "running")
|
|
287
|
+
throw new Error("overlay needs a receipt and terminal origin before teardown/restart");
|
|
288
|
+
r.state = "stopping";
|
|
289
|
+
r.cleanup = cleanup;
|
|
290
|
+
if (!cleanup.clean) {
|
|
291
|
+
r.state = "tainted";
|
|
292
|
+
return save(r);
|
|
293
|
+
}
|
|
294
|
+
if (r.receipt.delivered !== true || r.outcome) {
|
|
295
|
+
r.state = "failed";
|
|
296
|
+
return save(r);
|
|
297
|
+
}
|
|
298
|
+
assertHandoffOwner(r);
|
|
299
|
+
assertAuthority(r, policy);
|
|
300
|
+
pendingCurrent(r);
|
|
301
|
+
validatePlan(r, policy);
|
|
302
|
+
// No same-job exemption for a live local proxy; interlock includes captured overlay records.
|
|
303
|
+
const interlock = overlayInterlock("stage2");
|
|
304
|
+
if (interlock)
|
|
305
|
+
throw new Error(interlock);
|
|
306
|
+
r.destinationTurn = insertTurn(r.nodeId, `overlay:${r.id}`, overlayPrompt(r), r.request.access);
|
|
307
|
+
r.state = "ready";
|
|
308
|
+
return save(r);
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
export function overlayPrompt(r) {
|
|
312
|
+
const launch = r.launchedAt !== undefined ? "Validated destination turn launched." : r.consumedAt === undefined ? "Allowed, not executed." : "Allowed; launch outcome unknown without a validated model-turn reply.";
|
|
313
|
+
return `[relay] One-turn overlay ${r.id}; approved effects: ${JSON.stringify(r.request.capabilities)}. Budget at most ${r.budgetMs / 1000}s including approval pauses. Decision: ${JSON.stringify(r.decision)}. ${launch}\n${r.request.disclosures.join("\n")}\n${r.request.capabilities.some(c => c.kind === "port0") ? r.launchedAt === undefined ? "The approved replacement turn may allow listeners to bind 0.0.0.0, so they could be on the LAN.\n" : "This turn allowed listeners to bind 0.0.0.0, so they could be on the LAN.\n" : ""}Continuation (untrusted task context):\n${r.request.continuation}`;
|
|
314
|
+
}
|
|
315
|
+
/** Record actual model-turn delivery separately from approval and executor preparation. */
|
|
316
|
+
export function markOverlayLaunched(captured, turnId) {
|
|
317
|
+
return tx(() => {
|
|
318
|
+
assertOverlayLaunch(captured, turnId);
|
|
319
|
+
const r = getOverlay(captured.runId, captured.id);
|
|
320
|
+
if (r.launchedAt !== undefined)
|
|
321
|
+
throw new Error("overlay destination launch already recorded");
|
|
322
|
+
r.launchedAt = clock.wall();
|
|
323
|
+
return save(r);
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
export function destinationOverlay(nodeId, turnId) {
|
|
327
|
+
const n = getNode(nodeId);
|
|
328
|
+
if (!n)
|
|
329
|
+
return null;
|
|
330
|
+
return Object.values(records(n.run_id)).find(r => r.nodeId === nodeId && r.destinationTurn === turnId && active(r)) ?? null;
|
|
331
|
+
}
|
|
332
|
+
/** The assigned replacement has priority over unrelated already-queued baseline messages. */
|
|
333
|
+
export function nextOverlayTurn(nodeId) {
|
|
334
|
+
const n = getNode(nodeId);
|
|
335
|
+
if (!n)
|
|
336
|
+
return undefined;
|
|
337
|
+
const pending = Object.values(records(n.run_id)).find(r => r.nodeId === nodeId && r.state === "ready");
|
|
338
|
+
if (!pending?.destinationTurn)
|
|
339
|
+
return undefined;
|
|
340
|
+
const t = getTurn(pending.destinationTurn);
|
|
341
|
+
return t?.status === "queued" ? t : undefined;
|
|
342
|
+
}
|
|
343
|
+
/** Persist deadline and destination consumption atomically BEFORE any async preparation/model work. */
|
|
344
|
+
export function consumeOverlay(runId, id, destinationTurn) {
|
|
345
|
+
const policy = checkpointNever(runId, "launch");
|
|
346
|
+
return tx(() => {
|
|
347
|
+
const r = getOverlay(runId, id);
|
|
348
|
+
if (r.destinationTurn !== destinationTurn)
|
|
349
|
+
throw new Error("overlay belongs to another destination");
|
|
350
|
+
if (r.state !== "ready" || r.consumedAt !== undefined)
|
|
351
|
+
throw new Error("overlay is not ready or was already consumed; no crash retry");
|
|
352
|
+
const t = getTurn(destinationTurn);
|
|
353
|
+
if (!t || t.node_id !== r.nodeId || !["queued", "running"].includes(t.status) || t.source !== `overlay:${id}`)
|
|
354
|
+
throw new Error("overlay destination is not live");
|
|
355
|
+
assertHandoffOwner(r);
|
|
356
|
+
assertAuthority(r, policy);
|
|
357
|
+
pendingCurrent(r);
|
|
358
|
+
validatePlan(r, policy);
|
|
359
|
+
const conflict = overlayInterlock("stage2");
|
|
360
|
+
if (conflict)
|
|
361
|
+
throw new Error(conflict);
|
|
362
|
+
const at = clock.wall(), ms = Math.min(r.budgetMs, r.request.hardDeadline === undefined ? Infinity : r.request.hardDeadline - at);
|
|
363
|
+
if (ms <= 0)
|
|
364
|
+
throw new Error("overlay deadline expired");
|
|
365
|
+
r.budgetMs = ms;
|
|
366
|
+
r.consumedAt = at;
|
|
367
|
+
r.recoveryDeadline = at + ms;
|
|
368
|
+
r.monotonicDeadline = clock.mono() + ms;
|
|
369
|
+
r.boot = clock.boot();
|
|
370
|
+
r.state = "running";
|
|
371
|
+
const start = clock.owner?.().start ?? procStart(process.pid);
|
|
372
|
+
if (!start)
|
|
373
|
+
throw new Error("overlay supervisor identity unavailable");
|
|
374
|
+
r.owner = { pid: process.pid, start };
|
|
375
|
+
return save(r);
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
/** A clock discrepancy cannot renew consent. Wall time is recovery evidence; hrtime enforces elapsed time. */
|
|
379
|
+
export function overlayDeadlineExpired(r) {
|
|
380
|
+
if (r.consumedAt === undefined)
|
|
381
|
+
return r.request.clockBoot !== clock.boot() || clock.mono() >= r.request.pendingMonotonicDeadline || clock.wall() < r.request.createdAt || clock.wall() >= r.request.pendingDeadline;
|
|
382
|
+
return r.boot !== clock.boot() || clock.wall() < r.consumedAt || clock.wall() >= r.recoveryDeadline || clock.mono() >= r.monotonicDeadline;
|
|
383
|
+
}
|
|
384
|
+
export function stopOverlay(runId, id, outcome) {
|
|
385
|
+
const change = () => { const r = getOverlay(runId, id); if (active(r)) {
|
|
386
|
+
r.state = "stopping";
|
|
387
|
+
r.outcome = outcome;
|
|
388
|
+
return save(r);
|
|
389
|
+
} return r; };
|
|
390
|
+
return db().isTransaction ? change() : tx(change);
|
|
391
|
+
}
|
|
392
|
+
export function finishOverlay(runId, id, cleanup) {
|
|
393
|
+
return tx(() => { const r = getOverlay(runId, id); if (!active(r))
|
|
394
|
+
return r; if (!r.outcome && overlayDeadlineExpired(r))
|
|
395
|
+
r.outcome = "overlay deadline expired"; r.cleanup = cleanup; r.state = cleanup.clean ? (r.consumedAt === undefined ? "failed" : "expired") : "tainted"; return save(r); });
|
|
396
|
+
}
|
|
397
|
+
/** Tool/approval and launch gate; leaves stopping durable before returning the refusal. */
|
|
398
|
+
export function overlayTurnProblem(nodeId, turnId) {
|
|
399
|
+
const n = getNode(nodeId);
|
|
400
|
+
if (!n)
|
|
401
|
+
return null;
|
|
402
|
+
for (const r of Object.values(records(n.run_id)).filter(r => r.nodeId === nodeId && active(r))) {
|
|
403
|
+
if (r.state === "requested")
|
|
404
|
+
continue;
|
|
405
|
+
if (r.destinationTurn === turnId && r.state === "ready")
|
|
406
|
+
continue;
|
|
407
|
+
if (r.destinationTurn === turnId && r.state === "running" && !overlayDeadlineExpired(r))
|
|
408
|
+
continue;
|
|
409
|
+
if (r.state === "running" && overlayDeadlineExpired(r)) {
|
|
410
|
+
stopOverlay(r.runId, r.id, "overlay deadline expired");
|
|
411
|
+
return `overlay ${r.id} deadline expired; stopping before further work`;
|
|
412
|
+
}
|
|
413
|
+
return `overlay ${r.id} ${r.state}: turn is closing or awaiting verified overlay teardown`;
|
|
414
|
+
}
|
|
415
|
+
return null;
|
|
416
|
+
}
|
|
417
|
+
function reservations(runId) {
|
|
418
|
+
const saved = JSON.parse(getRun(runId).config_json).params_json?.launch_reservations;
|
|
419
|
+
if (saved === undefined)
|
|
420
|
+
return {};
|
|
421
|
+
if (!saved || typeof saved !== "object" || Array.isArray(saved))
|
|
422
|
+
throw new Error(`malformed launch reservations in ${runId}`);
|
|
423
|
+
for (const [id, r] of Object.entries(saved)) {
|
|
424
|
+
if (!r || r.v !== 1 || r.id !== id || !r.nodeId || !Number.isSafeInteger(r.turnId)
|
|
425
|
+
|| !["local", "stage2"].includes(r.launchClass) || !["starting", "running", "stopping", "tainted"].includes(r.state)
|
|
426
|
+
|| typeof r.launching !== "boolean" || !r.owner || !Number.isInteger(r.owner.pid) || r.owner.pid < 2 || typeof r.owner.start !== "string" || !r.owner.start
|
|
427
|
+
|| r.cleanup !== undefined && (!r.cleanup || typeof r.cleanup !== "object" || typeof r.cleanup.clean !== "boolean"
|
|
428
|
+
|| r.cleanup.problem !== undefined && typeof r.cleanup.problem !== "string"
|
|
429
|
+
|| r.cleanup.appClean !== undefined && typeof r.cleanup.appClean !== "boolean"
|
|
430
|
+
|| r.cleanup.survivors !== undefined && (!Array.isArray(r.cleanup.survivors) || r.cleanup.survivors.some(p => !Number.isInteger(p) || p < 2))))
|
|
431
|
+
throw new Error(`malformed launch reservation ${id} in ${runId}; reap/cancel and verify cleanup`);
|
|
432
|
+
}
|
|
433
|
+
return saved;
|
|
434
|
+
}
|
|
435
|
+
function saveReservations(runId, saved) {
|
|
436
|
+
db().prepare("UPDATE runs SET config_json = json_set(config_json, '$.params_json.launch_reservations', json(?)) WHERE id = ?").run(JSON.stringify(saved), runId);
|
|
437
|
+
}
|
|
438
|
+
function capturedNetworkGrant(json) {
|
|
439
|
+
const grant = JSON.parse(json ?? "{}");
|
|
440
|
+
if (!grant || typeof grant !== "object" || Array.isArray(grant) || [grant.network, grant.local_network, grant.raw_network].some(x => x !== undefined && typeof x !== "boolean") || grant.local_network && grant.raw_network)
|
|
441
|
+
throw new Error("invalid captured network grant");
|
|
442
|
+
return grant;
|
|
443
|
+
}
|
|
444
|
+
export function capturedLaunchClass(nodeId, turnId) {
|
|
445
|
+
const n = getNode(nodeId), t = getTurn(turnId);
|
|
446
|
+
if (!n || !t || t.node_id !== nodeId)
|
|
447
|
+
throw new Error("captured launch turn is unavailable");
|
|
448
|
+
if (n.target !== "codex")
|
|
449
|
+
return null;
|
|
450
|
+
const grant = capturedNetworkGrant(t.grant_json);
|
|
451
|
+
if (destinationOverlay(nodeId, turnId))
|
|
452
|
+
return "stage2";
|
|
453
|
+
if (t.access !== "write")
|
|
454
|
+
return null;
|
|
455
|
+
return grant.raw_network ? "stage2" : grant.local_network ? "local" : null;
|
|
456
|
+
}
|
|
457
|
+
/** Capture effective policy before consent. BEGIN IMMEDIATE makes opposing starts mutually exclusive. */
|
|
458
|
+
export function reserveLaunch(nodeId, turnId, launchClass) {
|
|
459
|
+
tx(() => {
|
|
460
|
+
const n = getNode(nodeId), t = getTurn(turnId);
|
|
461
|
+
if (!n || !t || t.node_id !== nodeId || !["queued", "running"].includes(t.status))
|
|
462
|
+
throw new Error("cannot reserve an inactive launch");
|
|
463
|
+
if (capturedLaunchClass(nodeId, turnId) !== launchClass)
|
|
464
|
+
throw new Error("launch class differs from captured turn policy/overlay");
|
|
465
|
+
const conflict = overlayInterlock(launchClass);
|
|
466
|
+
if (conflict)
|
|
467
|
+
throw new Error(conflict);
|
|
468
|
+
const all = reservations(n.run_id), id = String(turnId);
|
|
469
|
+
if (all[id])
|
|
470
|
+
throw new Error(`launch reservation ${id} already exists; verify teardown before retry`);
|
|
471
|
+
const start = clock.owner?.().start ?? procStart(process.pid, { fresh: true });
|
|
472
|
+
if (!start)
|
|
473
|
+
throw new Error("cannot reserve launch without supervisor identity");
|
|
474
|
+
all[id] = { v: 1, id, nodeId, turnId, launchClass, state: "starting", launching: false, owner: { pid: process.pid, start } };
|
|
475
|
+
saveReservations(n.run_id, all);
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
export function launchReservationState(runId, turnId, state) {
|
|
479
|
+
tx(() => {
|
|
480
|
+
const all = reservations(runId), r = all[String(turnId)];
|
|
481
|
+
if (!r)
|
|
482
|
+
throw new Error(`launch reservation ${turnId} is unavailable`);
|
|
483
|
+
if (state === "running") {
|
|
484
|
+
if (r.state === "tainted" || r.state === "stopping")
|
|
485
|
+
throw new Error(`launch reservation ${r.id} ${r.state}; reap/cancel and verify cleanup`);
|
|
486
|
+
const conflict = overlayInterlock(r.launchClass);
|
|
487
|
+
if (conflict)
|
|
488
|
+
throw new Error(conflict);
|
|
489
|
+
r.launching = true;
|
|
490
|
+
}
|
|
491
|
+
if (r.state !== "tainted")
|
|
492
|
+
r.state = state;
|
|
493
|
+
saveReservations(runId, all);
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
/** Revocation closes the launch gate before any process signalling or terminal turn update. */
|
|
497
|
+
export function stopNodeLaunchReservations(nodeId) {
|
|
498
|
+
const change = () => {
|
|
499
|
+
const n = getNode(nodeId);
|
|
500
|
+
if (!n)
|
|
501
|
+
return;
|
|
502
|
+
const all = reservations(n.run_id);
|
|
503
|
+
for (const r of Object.values(all))
|
|
504
|
+
if (r.nodeId === nodeId && r.state !== "tainted")
|
|
505
|
+
r.state = "stopping";
|
|
506
|
+
saveReservations(n.run_id, all);
|
|
507
|
+
};
|
|
508
|
+
if (db().isTransaction)
|
|
509
|
+
change();
|
|
510
|
+
else
|
|
511
|
+
tx(change);
|
|
512
|
+
}
|
|
513
|
+
export function finishLaunchReservation(runId, turnId, cleanup) {
|
|
514
|
+
tx(() => {
|
|
515
|
+
const all = reservations(runId), r = all[String(turnId)];
|
|
516
|
+
if (!r)
|
|
517
|
+
return;
|
|
518
|
+
if (cleanup.clean)
|
|
519
|
+
delete all[r.id];
|
|
520
|
+
else {
|
|
521
|
+
r.state = "tainted";
|
|
522
|
+
r.cleanup = { ...r.cleanup, ...cleanup };
|
|
523
|
+
}
|
|
524
|
+
saveReservations(runId, all);
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
/** Existing executor reap/cancel/retention must retain its lease when app-server cleanup is unknown. */
|
|
528
|
+
export function launchExecutorQuarantine(runId, turnId) {
|
|
529
|
+
try {
|
|
530
|
+
if (turnId === null || !getRun(runId))
|
|
531
|
+
return undefined;
|
|
532
|
+
const r = reservations(runId)[String(turnId)];
|
|
533
|
+
return r?.launching && r.cleanup?.appClean !== true ? `untraceable app-server cleanup: launch reservation ${r.id} ${r.state}; quarantine retained` : undefined;
|
|
534
|
+
}
|
|
535
|
+
catch (err) {
|
|
536
|
+
return `untraceable app-server cleanup: launch reservation state unreadable (${err.message})`;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
/** Called only after the existing executor sweep proved cleanup, before releasing its lease. */
|
|
540
|
+
export function finishLaunchExecutor(runId, turnId) {
|
|
541
|
+
if (turnId === null || !getRun(runId))
|
|
542
|
+
return;
|
|
543
|
+
const r = reservations(runId)[String(turnId)];
|
|
544
|
+
if (r?.cleanup?.appClean === true)
|
|
545
|
+
finishLaunchReservation(runId, turnId, { clean: true });
|
|
546
|
+
}
|
|
547
|
+
/** Reap owns recovery; an absent native leader cannot prove detached processes stopped. */
|
|
548
|
+
export function reapLaunchReservations(runId, stop) {
|
|
549
|
+
for (const r of Object.values(reservations(runId))) {
|
|
550
|
+
if (sameProcess(r.owner.pid, r.owner.start, { fresh: true }) || pidAlive(r.owner.pid) && procStart(r.owner.pid, { fresh: true }) === null)
|
|
551
|
+
continue;
|
|
552
|
+
const cleanup = r.launching ? stop(r) : { clean: true };
|
|
553
|
+
finishLaunchReservation(runId, r.turnId, cleanup);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
/** Retention must not erase quarantine just because the node/turn is terminal. */
|
|
557
|
+
export function launchReservationProblem(runId, nodeId) {
|
|
558
|
+
try {
|
|
559
|
+
const r = Object.values(reservations(runId)).find(r => !nodeId || r.nodeId === nodeId);
|
|
560
|
+
if (r)
|
|
561
|
+
return `launch reservation ${r.id} of ${r.nodeId} ${r.state}; reap/cancel and verify cleanup`;
|
|
562
|
+
const overlay = Object.values(records(runId)).find(r => active(r) && (!nodeId || r.nodeId === nodeId));
|
|
563
|
+
return overlay ? `overlay ${overlay.id} ${overlay.state}; reap/cancel and verify cleanup` : null;
|
|
564
|
+
}
|
|
565
|
+
catch (err) {
|
|
566
|
+
return `launch reservation state unreadable: ${err.message}`;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
/** WP4 interlock: persisted effective class survives mutable flags/grants and worker death. */
|
|
570
|
+
export function overlayInterlock(starting) {
|
|
571
|
+
try {
|
|
572
|
+
for (const run of db().prepare("SELECT id FROM runs").all()) {
|
|
573
|
+
for (const r of Object.values(records(run.id))) {
|
|
574
|
+
if (starting === "local" && active(r))
|
|
575
|
+
return `overlay ${r.id} ${r.state} reserves stage2; reap/cancel and verify cleanup before local launch`;
|
|
576
|
+
}
|
|
577
|
+
for (const r of Object.values(reservations(run.id))) {
|
|
578
|
+
if (r.launchClass !== starting)
|
|
579
|
+
return `launch reservation ${r.id} of ${r.nodeId} ${r.state} reserves ${r.launchClass}; reap/cancel and verify cleanup before ${starting} launch`;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
// Legacy/captured turns, including flag-off leftovers. Never consult mutable node grants/config.
|
|
583
|
+
for (const row of db().prepare("SELECT t.grant_json, t.id AS turnId, t.status, n.id FROM nodes n JOIN turns t ON t.node_id=n.id WHERE n.target='codex' AND t.access='write' AND (t.status='running' OR n.agent_pgid IS NOT NULL AND t.status NOT IN ('queued'))").all()) {
|
|
584
|
+
let grant;
|
|
585
|
+
try {
|
|
586
|
+
grant = capturedNetworkGrant(row.grant_json);
|
|
587
|
+
}
|
|
588
|
+
catch {
|
|
589
|
+
return `relay cannot read the grant of a running turn of ${row.id} (turn ${row.turnId}, state ${row.status}); reap/cancel and verify cleanup before launch`;
|
|
590
|
+
}
|
|
591
|
+
if (starting === "stage2" && grant.local_network)
|
|
592
|
+
return `${row.id} is running a turn in local network mode, and relay does not start a raw-network turn or overlay while Codex's local-network proxy runs: the proxy has no authentication (turn ${row.turnId}, state ${row.status === "running" ? "running" : "tainted; terminal turn retains agent group"}). Reap/cancel and verify that turn and its proxy have stopped`;
|
|
593
|
+
if (starting === "local" && grant.raw_network)
|
|
594
|
+
return `${row.id} is running a turn with raw network (relay's stage 2), and relay does not open Codex's local-network proxy while one runs (turn ${row.turnId}, state ${row.status === "running" ? "running" : "tainted; terminal turn retains agent group"}). Reap/cancel and verify cleanup before local launch`;
|
|
595
|
+
}
|
|
596
|
+
if (starting === "local") {
|
|
597
|
+
const { records: executors, problems } = executorRecords();
|
|
598
|
+
if (problems.length)
|
|
599
|
+
return `executor record state unreadable: ${problems.join("; ")}; reap/cancel and verify cleanup before local launch`;
|
|
600
|
+
// A record is released only by successful teardown/sweep, never by an absent leader.
|
|
601
|
+
const first = executors[0];
|
|
602
|
+
if (first)
|
|
603
|
+
return `executor ${first.file} of ${first.record.nodeId} state ${first.record.tainted ? "tainted" : "running or awaiting verified teardown"}; reap/cancel and verify cleanup before local launch`;
|
|
604
|
+
}
|
|
605
|
+
return null;
|
|
606
|
+
}
|
|
607
|
+
catch (err) {
|
|
608
|
+
return `overlay interlock state unreadable: ${err.message}; reap/cancel and verify cleanup before launch`;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
/** Synchronous recovery hook used by registry reap; stop MUST kill app server before executor/sweep. */
|
|
612
|
+
export function reapOverlays(runId, deps) {
|
|
613
|
+
for (const r of Object.values(records(runId))) {
|
|
614
|
+
if (!active(r))
|
|
615
|
+
continue;
|
|
616
|
+
const turn = getTurn(r.destinationTurn ?? r.originTurn);
|
|
617
|
+
const over = !turn || !["queued", "running"].includes(turn.status);
|
|
618
|
+
const handedOff = r.consumedAt === undefined && r.state === "stopping" && r.receipt?.delivered;
|
|
619
|
+
if (!over && !["tainted", "stopping"].includes(r.state) && !overlayDeadlineExpired(r) && deps.ownerAlive(r))
|
|
620
|
+
continue;
|
|
621
|
+
// A live worker owns the acknowledged origin handoff. Reap must not turn its clean restart into
|
|
622
|
+
// an expired overlay; deadlines and worker death still take precedence.
|
|
623
|
+
if (handedOff && !overlayDeadlineExpired(r) && deps.ownerAlive(r))
|
|
624
|
+
continue;
|
|
625
|
+
stopOverlay(runId, r.id, r.consumedAt === undefined ? "interrupted before launch; allowed, not executed" : "deadline, worker death or uncertain cleanup");
|
|
626
|
+
let cleanup;
|
|
627
|
+
try {
|
|
628
|
+
cleanup = deps.stop(r);
|
|
629
|
+
}
|
|
630
|
+
catch (err) {
|
|
631
|
+
cleanup = { clean: false, problem: err.message };
|
|
632
|
+
}
|
|
633
|
+
finishOverlay(runId, r.id, cleanup);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
export const overlayOwnerAlive = (r) => sameProcess(r.owner.pid, r.owner.start, { fresh: true });
|
|
637
|
+
export const overlayRemainingMs = (r) => Math.max(0, Math.min(r.monotonicDeadline - clock.mono(), r.recoveryDeadline - clock.wall()));
|
|
638
|
+
/** Final synchronous environment/executor gate; use after every asynchronous preparation. */
|
|
639
|
+
export function assertOverlayLaunch(captured, turnId) {
|
|
640
|
+
const r = getOverlay(captured.runId, captured.id);
|
|
641
|
+
if (r.state !== "running" || r.destinationTurn !== turnId || r.digest !== captured.digest || r.recoveryDeadline !== captured.recoveryDeadline || r.monotonicDeadline !== captured.monotonicDeadline || r.consumedAt !== captured.consumedAt || r.boot !== captured.boot || overlayDeadlineExpired(r))
|
|
642
|
+
throw new Error("overlay launch is expired, changed or not its consumed destination");
|
|
643
|
+
assertHandoffOwner(r);
|
|
644
|
+
const policy = assertNeverCurrent(r.runId, r.request.neverHash);
|
|
645
|
+
if (r.launchedAt === undefined)
|
|
646
|
+
assertAuthority(r, policy);
|
|
647
|
+
validatePlan(r, policy);
|
|
648
|
+
const conflict = overlayInterlock("stage2");
|
|
649
|
+
if (conflict)
|
|
650
|
+
throw new Error(conflict);
|
|
651
|
+
}
|