@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,603 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import { harness, hostFor, INTERACTIVE_CLAUDE, task, until, type Harness, type Rec } from "./helpers.ts";
|
|
7
|
+
import {
|
|
8
|
+
localModeInterlock, localModeRetired, stage2Enabled, RAW_NETWORK_CONSENT_TEXT, RAW_NETWORK_TURN_NOTE, rawNetworkConsent, rawNetworkGrantProblem, rawNetworkKey, rawNetworkText, STAGE2_FLAG_ENV,
|
|
9
|
+
} from "../dist/core/raw-network.js";
|
|
10
|
+
import { STATIC_DENIED_TCP, STATIC_DENIED_UDP } from "../dist/confine/ports.js";
|
|
11
|
+
import { ConfigSchema, withDefaults } from "../dist/core/config.js";
|
|
12
|
+
|
|
13
|
+
// Stage 2's grant, the person's consent to it and the local-mode interlock
|
|
14
|
+
// (docs/codex-confinement-design.md §5.4, §6, §13.2 WP-E), called directly.
|
|
15
|
+
|
|
16
|
+
/** Runs `fn` with stage 2 switched on (or off) through the environment override, restoring it after. */
|
|
17
|
+
async function withSwitch<T>(on: boolean, fn: () => T | Promise<T>): Promise<T> {
|
|
18
|
+
const saved = process.env[STAGE2_FLAG_ENV];
|
|
19
|
+
if (on) process.env[STAGE2_FLAG_ENV] = "1";
|
|
20
|
+
else delete process.env[STAGE2_FLAG_ENV];
|
|
21
|
+
try {
|
|
22
|
+
return await fn();
|
|
23
|
+
} finally {
|
|
24
|
+
if (saved === undefined) delete process.env[STAGE2_FLAG_ENV];
|
|
25
|
+
else process.env[STAGE2_FLAG_ENV] = saved;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A scratch registry in this process, closed and removed afterwards. */
|
|
30
|
+
async function withRegistry<T>(fn: (db: typeof import("../dist/core/db.js")) => T | Promise<T>): Promise<T> {
|
|
31
|
+
const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "relay-s2e-")));
|
|
32
|
+
const saved = process.env.RELAY_HOME;
|
|
33
|
+
process.env.RELAY_HOME = home;
|
|
34
|
+
const dbm = await import("../dist/core/db.js");
|
|
35
|
+
try {
|
|
36
|
+
return await fn(dbm);
|
|
37
|
+
} finally {
|
|
38
|
+
dbm.closeDbForTests();
|
|
39
|
+
if (saved === undefined) delete process.env.RELAY_HOME;
|
|
40
|
+
else process.env.RELAY_HOME = saved;
|
|
41
|
+
fs.rmSync(home, { recursive: true, force: true });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
test("raw network grant: Codex write jobs only, with network: true, never with local_network, app server only, and only while stage 2 is switched on", async () => {
|
|
46
|
+
const ok = { network: true, raw_network: true };
|
|
47
|
+
await withSwitch(true, () => {
|
|
48
|
+
assert.equal(rawNetworkGrantProblem(undefined, "write", "codex"), null);
|
|
49
|
+
assert.equal(rawNetworkGrantProblem({ network: true }, "write", "codex"), null);
|
|
50
|
+
assert.equal(rawNetworkGrantProblem({ network: true, raw_network: false }, "write", "codex"), null);
|
|
51
|
+
assert.equal(rawNetworkGrantProblem(ok, "write", "codex"), null);
|
|
52
|
+
assert.equal(rawNetworkGrantProblem(ok, "write", "codex", withDefaults({})), null);
|
|
53
|
+
assert.match(rawNetworkGrantProblem({ network: true, raw_network: "yes" } as never, "write", "codex") ?? "", /must be true or false/);
|
|
54
|
+
assert.match(rawNetworkGrantProblem(ok, "write", "claude") ?? "", /for Codex write jobs only: Claude has no mode/);
|
|
55
|
+
assert.match(rawNetworkGrantProblem(ok, "read", "codex") ?? "", /for Codex write jobs only: a read-only session/);
|
|
56
|
+
assert.match(rawNetworkGrantProblem({ raw_network: true }, "write", "codex") ?? "", /needs network: true in the same grant/);
|
|
57
|
+
assert.match(rawNetworkGrantProblem({ network: false, raw_network: true }, "write", "codex") ?? "", /needs network: true/);
|
|
58
|
+
assert.match(rawNetworkGrantProblem({ ...ok, local_network: true }, "write", "codex") ?? "", /cannot be combined: choose one/);
|
|
59
|
+
assert.match(rawNetworkGrantProblem(ok, "write", "codex", withDefaults({ codex_backend: "exec" })) ?? "", /needs codex_backend "app-server"/);
|
|
60
|
+
});
|
|
61
|
+
await withSwitch(false, () => {
|
|
62
|
+
assert.match(rawNetworkGrantProblem(ok, "write", "codex") ?? "", /not available: relay's stage 2 .* is switched off/);
|
|
63
|
+
assert.equal(rawNetworkGrantProblem({ network: true }, "write", "codex"), null, "nothing changes without the key");
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("raw network consent: bound to the grant's network, raw_network and write paths, in any order, and to the text the person saw", () => {
|
|
68
|
+
const a = rawNetworkKey({ network: true, raw_network: true, write_paths: ["/b", "/a"], write_path_pins: [{ path: "/a", dev: 1, ino: 2 }] } as never);
|
|
69
|
+
assert.equal(a, rawNetworkKey({ network: true, raw_network: true, write_paths: ["/a", "/b"] }));
|
|
70
|
+
assert.equal(a, rawNetworkKey({ network: true, raw_network: true, write_paths: ["/a", "/b"], env: ["X"] }), "a turn's environment names are not the grant");
|
|
71
|
+
assert.notEqual(a, rawNetworkKey({ network: true, raw_network: true, write_paths: ["/a"] }));
|
|
72
|
+
assert.notEqual(a, rawNetworkKey({ network: true, raw_network: false, write_paths: ["/a", "/b"] }));
|
|
73
|
+
assert.notEqual(a, rawNetworkKey({ network: false, raw_network: true, write_paths: ["/a", "/b"] }));
|
|
74
|
+
assert.match(JSON.parse(a).text, /^[0-9a-f]{16}$/);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("raw network consent text: names everything the design says the person consents to, and claims nothing more", async () => {
|
|
78
|
+
const t = RAW_NETWORK_CONSENT_TEXT;
|
|
79
|
+
// Raw network, and that it is relay's sandbox rather than Codex's.
|
|
80
|
+
assert.match(t, /raw network/);
|
|
81
|
+
assert.match(t, /TCP, UDP, ssh and DNS to any host/);
|
|
82
|
+
assert.match(t, /inside relay's own sandbox instead of Codex's/);
|
|
83
|
+
// The LAN and the tailnet.
|
|
84
|
+
assert.match(t, /every device and service on your local network \(LAN\) and your tailnet, on any port/);
|
|
85
|
+
// The denied ports: every static one, the snapshot (D4), relay's range, the UDP mappings.
|
|
86
|
+
for (const p of STATIC_DENIED_TCP) assert.match(t, new RegExp(`\\b${p}\\b`), String(p));
|
|
87
|
+
for (const p of STATIC_DENIED_UDP) assert.match(t, new RegExp(`UDP .*\\b${p}\\b`), String(p));
|
|
88
|
+
assert.match(t, /every TCP port anything on this Mac \(your processes or the system's\) is listening on when each turn starts/);
|
|
89
|
+
// The person's own never.ports, held there as the built-ins are (seatbelt-never.ts).
|
|
90
|
+
assert.match(t, /the ports your never\.ports entries name/);
|
|
91
|
+
assert.match(t, /relay's own reserved ports/);
|
|
92
|
+
assert.match(t, /A service you start after a turn starts is reachable/);
|
|
93
|
+
// The IPv6 cost of closing the IPv4-mapped spellings (review R1), and the forwarding and NAT-hairpin residual.
|
|
94
|
+
assert.match(t, /Over IPv6 those TCP ports are refused at every host/);
|
|
95
|
+
assert.match(t, /IPv4 that a program sends through an IPv6 socket \(as Java does\), fails/);
|
|
96
|
+
assert.match(t, /only this Mac's own addresses as destinations: a router port forward reached back through NAT hairpin, a tailnet subnet router or a VM bridge/);
|
|
97
|
+
// Listening: the block only, and its exposure on 0.0.0.0 (D3).
|
|
98
|
+
assert.match(t, /listen only on its own block of 4 TCP ports \(given to it as PORT and PORTS\)/);
|
|
99
|
+
assert.match(t, /binds there on 0\.0\.0\.0 can be reached from your LAN and tailnet/);
|
|
100
|
+
// D6: git writes fail, relay commits.
|
|
101
|
+
assert.match(t, /git commands that write .* fail: relay commits the job's work at the end of each turn/);
|
|
102
|
+
// No stronger words than the design's.
|
|
103
|
+
assert.doesNotMatch(t, /\b(guarantee|cannot reach|secure|safe)\b/i);
|
|
104
|
+
assert.match(rawNetworkText({ id: "j_1", target: "codex" }), /^allow j_1 \(Codex, a write job\) raw network from its shell \(TCP, UDP, ssh, DNS\) inside relay's sandbox, with your local network and tailnet reachable, for the rest of the job$/);
|
|
105
|
+
assert.match(RAW_NETWORK_TURN_NOTE, /^\[relay\] .*\$PORTS .*port 0, are refused.*git commands that write .* fail here/);
|
|
106
|
+
assert.doesNotMatch(RAW_NETWORK_TURN_NOTE, /tools.*not available|cannot delegate or ask/);
|
|
107
|
+
// Raw network retains the same delegation and approval policy as strict network.
|
|
108
|
+
const { delegationPreamble } = await import("../dist/core/prompts.js");
|
|
109
|
+
const node = { id: "j_1", target: "codex", mode: "write", depth: 1, allow_delegation: 1, branch: "relay/j_1", cwd: "/w", worktree: "/w", params_json: "{}" } as never;
|
|
110
|
+
const cfg = withDefaults({ max_depth: 3 });
|
|
111
|
+
const raw = delegationPreamble(node, cfg, "root", "parent", undefined, { network: true, raw_network: true }, true);
|
|
112
|
+
assert.match(raw, /You may delegate further using the relay tools/);
|
|
113
|
+
assert.match(raw, /Jobs you start keep running/);
|
|
114
|
+
assert.doesNotMatch(raw, /tools are not available|cannot delegate or ask/);
|
|
115
|
+
const strict = delegationPreamble(node, cfg, "root", "parent", undefined, { network: true }, true);
|
|
116
|
+
assert.match(strict, /You may delegate further using the relay tools/);
|
|
117
|
+
assert.doesNotMatch(strict, /while your grant has raw network/);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("raw network consent: person-only, and shown with its effect in the approval view", async () => {
|
|
121
|
+
await withRegistry(async ({ createRun, insertNode }) => {
|
|
122
|
+
const { personOnly } = await import("../dist/core/extensions.js");
|
|
123
|
+
const { requestView } = await import("../dist/core/approvals.js");
|
|
124
|
+
const run = createRun("/", withDefaults({}), "s2e-person");
|
|
125
|
+
const n = insertNode({ id: "j_person", run_id: run, kind: "job", target: "codex", task: "t", cwd: "/", mode: "write", depth: 1 });
|
|
126
|
+
assert.equal(personOnly({ kind: "raw_network", node_id: n.id }), true);
|
|
127
|
+
const view = requestView({ id: "r_1", run_id: run, node_id: n.id, kind: "raw_network", summary: rawNetworkText(n), reason: null, detail_json: null, status: "pending", decided_by: null, note: null, created_at: Date.now(), decided_at: null, turn_id: null } as never);
|
|
128
|
+
assert.equal(view.person_only, true);
|
|
129
|
+
assert.equal(view.effect, RAW_NETWORK_CONSENT_TEXT);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("raw network consent: once per grant, gone when the grant changes, and asked again when raw network comes back (toggle away and back); read turns are never asked", async () => {
|
|
134
|
+
await withSwitch(true, () => withRegistry(async ({ createRun, insertNode, insertTurn, getNode, setParam, db }) => {
|
|
135
|
+
const { sessionParams } = await import("../dist/core/params.js");
|
|
136
|
+
const run = createRun("/", withDefaults({}), "s2e-consent");
|
|
137
|
+
const grant = { network: true, raw_network: true, write_paths: [] as string[] };
|
|
138
|
+
const n = insertNode({ id: "j_consent", run_id: run, kind: "job", target: "codex", task: "t", cwd: "/", mode: "write", depth: 1, status: "running", params_json: JSON.stringify({ grant }) });
|
|
139
|
+
const turn = insertTurn(n.id, "caller", "go", "write", "running");
|
|
140
|
+
const consent = () => sessionParams(getNode(n.id)!).raw_network_consent ?? null;
|
|
141
|
+
// Asked: a person-only request is filed; the turn is cancelled once it is, so it is not taken as given.
|
|
142
|
+
const filed = () => (db().prepare("SELECT COUNT(*) AS c FROM requests WHERE node_id = ? AND kind = 'raw_network'").get(n.id) as { c: number }).c;
|
|
143
|
+
const onceFiled = (count: number) => () => filed() >= count;
|
|
144
|
+
const asked = await rawNetworkConsent(getNode(n.id)!, turn, "write", onceFiled(1));
|
|
145
|
+
assert.deepEqual(asked, { cancelled: "cancelled while waiting for the user's consent to its raw network" });
|
|
146
|
+
assert.equal(filed(), 1, "a raw_network request was filed");
|
|
147
|
+
// The consent for this grant, as the person's answer records it: not asked again.
|
|
148
|
+
setParam(n.id, "raw_network_consent", { grant: rawNetworkKey(grant), by: "by the user", at: 1 });
|
|
149
|
+
assert.equal(await rawNetworkConsent(getNode(n.id)!, turn, "write", () => { throw new Error("asked again"); }), null);
|
|
150
|
+
// A read turn of the job keeps it, unasked.
|
|
151
|
+
assert.equal(await rawNetworkConsent(getNode(n.id)!, turn, "read", () => { throw new Error("asked on a read turn"); }), null);
|
|
152
|
+
assert.ok(consent());
|
|
153
|
+
// Raw network taken away: the consent goes, even on a read turn.
|
|
154
|
+
setParam(n.id, "grant", { network: true, write_paths: [] });
|
|
155
|
+
assert.equal(await rawNetworkConsent(getNode(n.id)!, turn, "read", () => true), null);
|
|
156
|
+
assert.equal(consent(), null);
|
|
157
|
+
// And back: asked again, not taken from the earlier consent.
|
|
158
|
+
setParam(n.id, "grant", grant);
|
|
159
|
+
assert.deepEqual(await rawNetworkConsent(getNode(n.id)!, turn, "write", onceFiled(2)), { cancelled: "cancelled while waiting for the user's consent to its raw network" });
|
|
160
|
+
assert.equal(filed(), 2, "asked again");
|
|
161
|
+
// A consent for another grant (other write paths) is gone too.
|
|
162
|
+
setParam(n.id, "raw_network_consent", { grant: rawNetworkKey({ ...grant, write_paths: ["/x"] }), by: "by the user", at: 1 });
|
|
163
|
+
await rawNetworkConsent(getNode(n.id)!, turn, "write", () => true);
|
|
164
|
+
assert.equal(consent(), null);
|
|
165
|
+
// Not a raw-network grant: nothing to ask.
|
|
166
|
+
setParam(n.id, "grant", { network: true });
|
|
167
|
+
assert.equal(await rawNetworkConsent(getNode(n.id)!, turn, "write", () => { throw new Error("asked"); }), null);
|
|
168
|
+
// A malformed legacy grant (raw network without network) is refused, never run with less.
|
|
169
|
+
setParam(n.id, "grant", { raw_network: true });
|
|
170
|
+
assert.match((await rawNetworkConsent(getNode(n.id)!, turn, "write", () => true) as { refusal: string }).refusal, /holds raw_network without network, so its agent was not started/);
|
|
171
|
+
}));
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("raw network consent: decided and keyed on the turn's own grant, not the session's current one (resume while queued, review R2)", async () => {
|
|
175
|
+
await withSwitch(true, () => withRegistry(async ({ createRun, insertNode, insertTurn, getNode, setParam, db }) => {
|
|
176
|
+
const { sessionParams } = await import("../dist/core/params.js");
|
|
177
|
+
const run = createRun("/", withDefaults({}), "s2e-queued");
|
|
178
|
+
const g1 = { network: true, raw_network: true, write_paths: [] as string[] };
|
|
179
|
+
const n = insertNode({ id: "j_queued", run_id: run, kind: "job", target: "codex", task: "t", cwd: "/", mode: "write", depth: 1, status: "running", params_json: JSON.stringify({ grant: g1 }) });
|
|
180
|
+
const turn = insertTurn(n.id, "caller", "go", "write", "running");
|
|
181
|
+
// The turn read the job (and so its grant) when it was picked up; a resume then replaced the session's grant.
|
|
182
|
+
const asPickedUp = getNode(n.id)!;
|
|
183
|
+
setParam(n.id, "grant", { network: true, write_paths: [] });
|
|
184
|
+
const filed = () => (db().prepare("SELECT COUNT(*) AS c FROM requests WHERE node_id = ? AND kind = 'raw_network'").get(n.id) as { c: number }).c;
|
|
185
|
+
// The turn launches with G1, so the person is asked about G1 (before the fix: nothing asked, null).
|
|
186
|
+
assert.deepEqual(await rawNetworkConsent(asPickedUp, turn, "write", () => filed() >= 1), { cancelled: "cancelled while waiting for the user's consent to its raw network" });
|
|
187
|
+
assert.equal(filed(), 1);
|
|
188
|
+
// A consent for G1 (as the person's answer to that request records it) ends with the next turn, under G2.
|
|
189
|
+
setParam(n.id, "raw_network_consent", { grant: rawNetworkKey(g1), by: "by the user", at: 1 });
|
|
190
|
+
assert.equal(await rawNetworkConsent(getNode(n.id)!, turn, "write", () => { throw new Error("asked"); }), null);
|
|
191
|
+
assert.equal(sessionParams(getNode(n.id)!).raw_network_consent ?? null, null);
|
|
192
|
+
}));
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("raw network consent: with stage 2 switched off, a job still holding the grant is refused, not run with less", async () => {
|
|
196
|
+
await withSwitch(false, () => withRegistry(async ({ createRun, insertNode, insertTurn, getNode }) => {
|
|
197
|
+
const run = createRun("/", withDefaults({}), "s2e-off");
|
|
198
|
+
const n = insertNode({ id: "j_off", run_id: run, kind: "job", target: "codex", task: "t", cwd: "/", mode: "write", depth: 1, status: "running", params_json: JSON.stringify({ grant: { network: true, raw_network: true } }) });
|
|
199
|
+
const turn = insertTurn(n.id, "caller", "go", "write", "running");
|
|
200
|
+
assert.match((await rawNetworkConsent(getNode(n.id)!, turn, "write", () => true) as { refusal: string }).refusal, /stage 2 .* is switched off, so its agent was not started/);
|
|
201
|
+
}));
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("local-mode interlock: no local-mode proxy while a raw-network turn runs, and no raw-network turn while a local-mode one runs", async () => {
|
|
205
|
+
await withRegistry(async ({ createRun, insertNode, insertTurn, db }) => {
|
|
206
|
+
const run = createRun("/", withDefaults({}), "s2e-interlock");
|
|
207
|
+
const job = (id: string, target = "codex") => insertNode({ id, run_id: run, kind: "job", target, task: "t", cwd: "/", mode: "write", depth: 1, status: "running" });
|
|
208
|
+
const turn = (node: string, grant: unknown, access: "read" | "write" = "write", status: "running" | "done" | "queued" = "running") => {
|
|
209
|
+
const id = insertTurn(node, "caller", "go", access, status);
|
|
210
|
+
db().prepare("UPDATE turns SET grant_json = ? WHERE id = ?").run(grant === undefined ? null : typeof grant === "string" ? grant : JSON.stringify(grant), id);
|
|
211
|
+
return id;
|
|
212
|
+
};
|
|
213
|
+
const end = (id: number) => db().prepare("UPDATE turns SET status = 'done' WHERE id = ?").run(id);
|
|
214
|
+
assert.equal(localModeInterlock("local"), null);
|
|
215
|
+
assert.equal(localModeInterlock("stage2"), null);
|
|
216
|
+
|
|
217
|
+
// A raw-network turn running: local mode is held off, stage 2 is not.
|
|
218
|
+
job("j_raw");
|
|
219
|
+
const raw = turn("j_raw", { network: true, raw_network: true });
|
|
220
|
+
assert.match(localModeInterlock("local") ?? "", /^j_raw is running a turn with raw network \(relay's stage 2\), and relay does not open Codex's local-network proxy while one runs/);
|
|
221
|
+
assert.equal(localModeInterlock("stage2"), null, "two raw-network turns may run together");
|
|
222
|
+
end(raw);
|
|
223
|
+
assert.equal(localModeInterlock("local"), null, "once it has ended");
|
|
224
|
+
|
|
225
|
+
// A local-mode turn running: stage 2 is refused, local mode is not.
|
|
226
|
+
job("j_local");
|
|
227
|
+
const local = turn("j_local", { network: true, local_network: true });
|
|
228
|
+
assert.match(localModeInterlock("stage2") ?? "", /^j_local is running a turn in local network mode, and relay does not start a raw-network turn/);
|
|
229
|
+
assert.equal(localModeInterlock("local"), null);
|
|
230
|
+
end(local);
|
|
231
|
+
|
|
232
|
+
// Not counted: queued or ended turns, read turns, other targets, strict network.
|
|
233
|
+
turn("j_raw", { network: true, raw_network: true }, "write", "queued");
|
|
234
|
+
turn("j_raw", { network: true, raw_network: true }, "read");
|
|
235
|
+
job("j_claude", "claude");
|
|
236
|
+
turn("j_claude", { network: true, local_network: true });
|
|
237
|
+
job("j_strict");
|
|
238
|
+
turn("j_strict", { network: true });
|
|
239
|
+
turn("j_strict", undefined);
|
|
240
|
+
assert.equal(localModeInterlock("local"), null);
|
|
241
|
+
assert.equal(localModeInterlock("stage2"), null);
|
|
242
|
+
|
|
243
|
+
// WP4: malformed relevant records fail both directions closed; flags are not captured policy.
|
|
244
|
+
job("j_bad");
|
|
245
|
+
const bad = turn("j_bad", "{not json");
|
|
246
|
+
assert.match(localModeInterlock("local") ?? "", /cannot read the grant of a running turn of j_bad/);
|
|
247
|
+
assert.match(localModeInterlock("stage2") ?? "", /cannot read the grant of a running turn of j_bad/);
|
|
248
|
+
end(bad);
|
|
249
|
+
const onRun = createRun("/", withDefaults({ targets: { codex: { stage2: true } } } as never), "s2e-interlock-on");
|
|
250
|
+
insertNode({ id: "j_bad_on", run_id: onRun, kind: "job", target: "codex", task: "t", cwd: "/", mode: "write", depth: 1, status: "running" });
|
|
251
|
+
const badOn = turn("j_bad_on", "{not json");
|
|
252
|
+
assert.match(localModeInterlock("local") ?? "", /cannot read the grant of a running turn of j_bad_on/);
|
|
253
|
+
assert.match(localModeInterlock("stage2") ?? "", /cannot read the grant of a running turn of j_bad_on/);
|
|
254
|
+
end(badOn);
|
|
255
|
+
assert.equal(localModeInterlock("local"), null);
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test("local mode while stage 2 is switched on: refused outright; switched off, unchanged (review R3)", () => {
|
|
260
|
+
const on = withDefaults({ targets: { codex: { stage2: true } } } as never);
|
|
261
|
+
return withSwitch(false, () => {
|
|
262
|
+
assert.equal(localModeRetired(withDefaults({})), null);
|
|
263
|
+
assert.equal(localModeRetired(undefined), null);
|
|
264
|
+
assert.match(localModeRetired(on) ?? "", /^grant\.local_network is not available while relay's stage 2 is switched on .* Use grant\.raw_network .* or grant\.network alone/);
|
|
265
|
+
// The environment override switches it on too.
|
|
266
|
+
process.env[STAGE2_FLAG_ENV] = "1";
|
|
267
|
+
try {
|
|
268
|
+
assert.match(localModeRetired(withDefaults({})) ?? "", /not available while relay's stage 2 is switched on/);
|
|
269
|
+
} finally {
|
|
270
|
+
delete process.env[STAGE2_FLAG_ENV];
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
test("local mode while a stage-2 executor or its survivor still runs: refused even with stage 2 switched off (review R3)", async () => {
|
|
276
|
+
const { recordExecutor, forgetExecutor } = await import("../dist/core/stage2-executors.js");
|
|
277
|
+
const { procStart } = await import("../dist/core/procs.js");
|
|
278
|
+
return withSwitch(false, () => {
|
|
279
|
+
assert.equal(localModeRetired(withDefaults({})), null);
|
|
280
|
+
// This test process stands in for a live executor: the record names it by pid and start time.
|
|
281
|
+
const file = recordExecutor({ pid: process.pid, start: procStart(process.pid, { fresh: true })!, runId: "r_r3", nodeId: "j_r3", turnId: 1, ports: { exec: 61001, block: [61002] }, tmpDir: os.tmpdir() });
|
|
282
|
+
try {
|
|
283
|
+
assert.match(localModeRetired(withDefaults({})) ?? "", /^grant\.local_network is not available: executor .*j_r3.*state running or awaiting verified teardown.*reap\/cancel/);
|
|
284
|
+
} finally {
|
|
285
|
+
forgetExecutor(file);
|
|
286
|
+
}
|
|
287
|
+
assert.equal(localModeRetired(withDefaults({})), null, "gone once the record is");
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
// The switch in config, and the grant through relay's MCP tools.
|
|
292
|
+
|
|
293
|
+
// These tests exercise standing grants/consent and native profile accounting, not the
|
|
294
|
+
// separate escalation executor. Keep network-off jobs native without changing routing.
|
|
295
|
+
const STAGE2_ON = { targets: { codex: { stage2: true } }, escalation: false };
|
|
296
|
+
const RAW_CAPABLE_STUB = { STUB_VERSION: "codex-cli 0.164.0 (stub)" };
|
|
297
|
+
|
|
298
|
+
test("stage 2 switch: targets.codex.stage2 in relay's config, Codex only, off by default; the environment override still works", async () => {
|
|
299
|
+
await withSwitch(false, () => {
|
|
300
|
+
assert.equal(stage2Enabled(process.env, withDefaults({})), false);
|
|
301
|
+
assert.equal(stage2Enabled(process.env, withDefaults({ targets: { codex: { stage2: false } } } as never)), false);
|
|
302
|
+
assert.equal(stage2Enabled(process.env, withDefaults(STAGE2_ON as never)), true);
|
|
303
|
+
assert.equal(stage2Enabled({ [STAGE2_FLAG_ENV]: "1" }, withDefaults({})), true);
|
|
304
|
+
assert.equal(rawNetworkGrantProblem({ network: true, raw_network: true }, "write", "codex", withDefaults(STAGE2_ON as never)), null);
|
|
305
|
+
assert.match(rawNetworkGrantProblem({ network: true, raw_network: true }, "write", "codex", withDefaults({})) ?? "", /switched off \(targets\.codex\.stage2/);
|
|
306
|
+
});
|
|
307
|
+
assert.equal(ConfigSchema.safeParse(STAGE2_ON).success, true);
|
|
308
|
+
assert.equal(ConfigSchema.safeParse({ targets: { codex: { stage2: "true" } } }).success, false, "a quoted value is an error");
|
|
309
|
+
assert.equal(ConfigSchema.safeParse({ targets: { claude: { stage2: true } } }).success, false, "Codex only");
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
/** A root session in a host that shows relay's questions to a person, and the person at it (as codex-network.test.ts personSession). */
|
|
313
|
+
async function personSession(h: Harness) {
|
|
314
|
+
const open: { message: string; signal: AbortSignal; resolve: (r: Rec) => void }[] = [];
|
|
315
|
+
const inst = await h.instance({
|
|
316
|
+
...INTERACTIVE_CLAUDE, args: ["--host", hostFor("codex")],
|
|
317
|
+
elicit: (message, signal) => new Promise((resolve) => {
|
|
318
|
+
open.push({ message, signal, resolve });
|
|
319
|
+
signal.addEventListener("abort", () => resolve({ action: "cancel" }), { once: true });
|
|
320
|
+
}),
|
|
321
|
+
});
|
|
322
|
+
const question = (req: Rec) => open.find((q) => !q.signal.aborted && q.message.includes(` ${req.from} asks`));
|
|
323
|
+
return {
|
|
324
|
+
...inst,
|
|
325
|
+
async answers(req: Rec, decision: "allow" | "deny") {
|
|
326
|
+
let q: ReturnType<typeof question>;
|
|
327
|
+
await until(() => Boolean((q = question(req))), 30000, `relay's question about ${req.request_id}`);
|
|
328
|
+
open.splice(open.indexOf(q!), 1);
|
|
329
|
+
q!.resolve({ action: "accept", content: { decision } });
|
|
330
|
+
await until(() => h.sql("SELECT status FROM requests WHERE id = ?", req.request_id)[0]?.status !== "pending", 30000, `${req.request_id} decided`);
|
|
331
|
+
return q!.message;
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async function pendingOf(h: Pick<Harness, "call">, id: string, kind: string): Promise<Rec> {
|
|
337
|
+
let req: Rec | undefined;
|
|
338
|
+
await until(async () => {
|
|
339
|
+
const s = await h.call("status", { id, wait_s: 5 });
|
|
340
|
+
req = (s.needs_approval as Rec[] | undefined)?.find((r) => r.kind === kind && r.from === id);
|
|
341
|
+
return Boolean(req);
|
|
342
|
+
}, 60000, `a ${kind} request from ${id}`);
|
|
343
|
+
return req!;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const settled = async (h: Pick<Harness, "call">, id: string): Promise<Rec> => {
|
|
347
|
+
let s: Rec = {};
|
|
348
|
+
await until(async () => {
|
|
349
|
+
s = await h.call("status", { id, wait_s: 10 });
|
|
350
|
+
return !["queued", "running"].includes(s.status);
|
|
351
|
+
}, 60000, `${id} to settle`);
|
|
352
|
+
return s;
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
test("raw network grant (MCP): refused where it cannot run, and where nobody could consent; switched off, not available", async () => {
|
|
356
|
+
const h = await harness([{ match: task("RAW"), steps: [{ reply: "done" }] }], STAGE2_ON, RAW_CAPABLE_STUB);
|
|
357
|
+
try {
|
|
358
|
+
const refused = async (args: Rec, re: RegExp) => {
|
|
359
|
+
const r = await h.call("delegate", { task: "RAW", background: false, ...args });
|
|
360
|
+
assert.equal(r.refused, true, JSON.stringify(r));
|
|
361
|
+
assert.match(r.reason, re, JSON.stringify(r));
|
|
362
|
+
};
|
|
363
|
+
await refused({ target: "codex", mode: "read", grant: { network: true, raw_network: true } }, /grant\.raw_network is for Codex write jobs only: a read-only session/);
|
|
364
|
+
await refused({ target: "codex", mode: "write", grant: { raw_network: true } }, /grant\.raw_network needs network: true in the same grant/);
|
|
365
|
+
await refused({ target: "claude", mode: "write", grant: { network: true, raw_network: true } }, /grant\.raw_network is for Codex write jobs only: Claude has no mode/);
|
|
366
|
+
await refused({ target: "codex", mode: "write", grant: { network: true, raw_network: true, local_network: true } }, /cannot be combined: choose one/);
|
|
367
|
+
// Stage 2 switched on: local mode is refused outright (review R3, decision D2).
|
|
368
|
+
await refused({ target: "codex", mode: "write", grant: { network: true, local_network: true } }, /grant\.local_network is not available while relay's stage 2 is switched on \(targets\.codex\.stage2 in relay's config\): Codex's local-network proxy has no authentication/);
|
|
369
|
+
// This harness's root session has no person path: refused up front.
|
|
370
|
+
await refused({ target: "codex", mode: "write", grant: { network: true, raw_network: true } }, /grant\.raw_network needs the user's consent through confirm or a qualified approval dialog/);
|
|
371
|
+
assert.equal(h.starts().length, 0, "nothing started");
|
|
372
|
+
} finally {
|
|
373
|
+
await h.close();
|
|
374
|
+
}
|
|
375
|
+
const off = await harness([{ match: task("RAW"), steps: [{ reply: "done" }] }]);
|
|
376
|
+
try {
|
|
377
|
+
const r = await off.call("delegate", { target: "codex", task: "RAW", mode: "write", background: false, grant: { network: true, raw_network: true } });
|
|
378
|
+
assert.match(r.reason, /grant\.raw_network is not available: relay's stage 2 .* is switched off/, JSON.stringify(r));
|
|
379
|
+
// Switched off, local mode is as in 1.0.5: here refused only because this root has no way to ask the person.
|
|
380
|
+
const local = await off.call("delegate", { target: "codex", task: "RAW", mode: "write", background: false, grant: { network: true, local_network: true } });
|
|
381
|
+
assert.match(local.reason, /grant\.local_network needs the user's consent/, JSON.stringify(local));
|
|
382
|
+
} finally {
|
|
383
|
+
await off.close();
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
test("raw network consent (MCP): the person's alone, before the agent starts; a denial refuses the turn; dropping the grant ends the consent and bringing it back asks again", async () => {
|
|
388
|
+
const h = await harness([{ match: task("RAWJOB"), steps: [{ reply: "done" }] }], { ...STAGE2_ON, escalation: true }, RAW_CAPABLE_STUB);
|
|
389
|
+
try {
|
|
390
|
+
const person = await personSession(h);
|
|
391
|
+
const consentOf = (id: string) => JSON.parse(h.sql("SELECT params_json FROM nodes WHERE id = ?", id)[0].params_json).raw_network_consent ?? null;
|
|
392
|
+
const job = await person.call("delegate", { target: "codex", task: "RAWJOB", mode: "write", grant: { network: true, raw_network: true } });
|
|
393
|
+
assert.ok(job.job_id, JSON.stringify(job));
|
|
394
|
+
assert.deepEqual(job.grant, { network: true, raw_network: true, write_paths: [] });
|
|
395
|
+
const req = await pendingOf(person, job.job_id, "raw_network");
|
|
396
|
+
assert.equal(req.person_only, true);
|
|
397
|
+
assert.equal(req.effect, RAW_NETWORK_CONSENT_TEXT);
|
|
398
|
+
assert.match(req.wants, /raw network from its shell \(TCP, UDP, ssh, DNS\) inside relay's sandbox, with your local network and tailnet reachable/);
|
|
399
|
+
assert.equal(h.starts().length, 0, "nothing runs before consent");
|
|
400
|
+
// The root agent's word is not the person's.
|
|
401
|
+
assert.match(JSON.stringify(await person.call("approve", { request_id: req.request_id, decision: "allow" })), /only the user can allow .*: it gives a job's shell raw network/);
|
|
402
|
+
assert.equal(h.sql("SELECT status FROM requests WHERE id = ?", req.request_id)[0].status, "pending");
|
|
403
|
+
await person.answers(req, "deny");
|
|
404
|
+
const denied = await settled(person, job.job_id);
|
|
405
|
+
assert.equal(denied.status, "failed", JSON.stringify(denied));
|
|
406
|
+
assert.match(denied.error, /consent to this job's raw network was refused .*, so its agent was not started\. Continue it to be asked again/);
|
|
407
|
+
assert.equal(h.starts().length, 0);
|
|
408
|
+
|
|
409
|
+
// Continued under the same grant: asked again, and allowed this time; the consent is kept for that grant.
|
|
410
|
+
await person.call("resume", { job_id: job.job_id, message: "RAWJOB again" });
|
|
411
|
+
const again = await pendingOf(person, job.job_id, "raw_network");
|
|
412
|
+
assert.notEqual(again.request_id, req.request_id);
|
|
413
|
+
await person.answers(again, "allow");
|
|
414
|
+
await settled(person, job.job_id);
|
|
415
|
+
const given = consentOf(job.job_id);
|
|
416
|
+
assert.equal(given?.grant, rawNetworkKey({ network: true, raw_network: true, write_paths: [] }));
|
|
417
|
+
|
|
418
|
+
// Raw network taken away on resume: the consent ends there.
|
|
419
|
+
await person.call("resume", { job_id: job.job_id, message: "RAWJOB strict", grant: { network: true } });
|
|
420
|
+
assert.equal(consentOf(job.job_id), null);
|
|
421
|
+
await settled(person, job.job_id);
|
|
422
|
+
// And given back: asked again, as for a grant never consented to.
|
|
423
|
+
await person.call("resume", { job_id: job.job_id, message: "RAWJOB back", grant: { network: true, raw_network: true } });
|
|
424
|
+
const back = await pendingOf(person, job.job_id, "raw_network");
|
|
425
|
+
assert.ok(![req.request_id, again.request_id].includes(back.request_id));
|
|
426
|
+
await person.answers(back, "deny");
|
|
427
|
+
assert.equal((await settled(person, job.job_id)).status, "failed");
|
|
428
|
+
assert.equal(h.sql("SELECT id FROM requests WHERE kind = 'raw_network'").length, 3);
|
|
429
|
+
await person.close();
|
|
430
|
+
} finally {
|
|
431
|
+
await h.close();
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
test("raw network consent (MCP): a resume while the turn waits for its slot does not skip the person's consent (review R2)", async () => {
|
|
436
|
+
const h = await harness([
|
|
437
|
+
{ match: task("HOLDQ"), steps: [{ awaitMark: "go" }, { reply: "held" }] },
|
|
438
|
+
{ match: task("RAWQ"), steps: [{ reply: "done" }] },
|
|
439
|
+
], {
|
|
440
|
+
...STAGE2_ON, escalation: true, max_concurrent: 1,
|
|
441
|
+
// RAW_CAPABLE_STUB's Codex version must not make the native Claude holder
|
|
442
|
+
// report 0.164.0 and fail Claude's 2.1.302 minimum before the stub starts.
|
|
443
|
+
targets: { ...STAGE2_ON.targets, claude: { child_env: { drop: ["STUB_VERSION"] } } },
|
|
444
|
+
}, RAW_CAPABLE_STUB);
|
|
445
|
+
try {
|
|
446
|
+
const person = await personSession(h);
|
|
447
|
+
// Another job of the same run (slots are per run) holds the one slot.
|
|
448
|
+
const holder = await person.call("delegate", { target: "claude", task: "HOLDQ", mode: "read" });
|
|
449
|
+
assert.ok(holder.job_id, JSON.stringify(holder));
|
|
450
|
+
await until(() => {
|
|
451
|
+
const status = h.sql("SELECT status FROM nodes WHERE id = ?", holder.job_id)[0]?.status;
|
|
452
|
+
return h.starts().length === 1 || ["done", "failed", "cancelled"].includes(status);
|
|
453
|
+
}, 30000, "the holder to start or fail");
|
|
454
|
+
assert.equal(h.starts().length, 1, `the holder must start: ${JSON.stringify(await person.call("status", { id: holder.job_id }))}`);
|
|
455
|
+
assert.equal(h.starts()[0].target, "claude");
|
|
456
|
+
// The raw-network job's first turn is picked up under G1 and waits for the slot.
|
|
457
|
+
const job = await person.call("delegate", { target: "codex", task: "RAWQ", mode: "write", grant: { network: true, raw_network: true } });
|
|
458
|
+
assert.ok(job.job_id, JSON.stringify(job));
|
|
459
|
+
const first = () => h.sql("SELECT id, status, grant_json, error FROM turns WHERE node_id = ? ORDER BY id", job.job_id)[0];
|
|
460
|
+
await until(() => String(first()?.grant_json ?? "").includes("raw_network"), 30000, "the first turn to record its grant");
|
|
461
|
+
assert.equal(h.sql("SELECT id FROM requests WHERE kind = 'raw_network'").length, 0, "still waiting for the slot, not yet asked");
|
|
462
|
+
// The caller drops raw network while that turn waits.
|
|
463
|
+
const r = await person.call("resume", { job_id: job.job_id, message: "RAWQ strict", grant: { network: true } });
|
|
464
|
+
assert.ok(!r.refused, JSON.stringify(r));
|
|
465
|
+
h.mark("go");
|
|
466
|
+
// The first turn still launches with G1, so the person is asked about it; denied, it does not start.
|
|
467
|
+
const req = await pendingOf(person, job.job_id, "raw_network");
|
|
468
|
+
await person.answers(req, "deny");
|
|
469
|
+
await until(() => first()?.status !== "queued" && first()?.status !== "running", 30000, "the first turn to end");
|
|
470
|
+
assert.equal(first().status, "failed", JSON.stringify(first()));
|
|
471
|
+
assert.match(first().error, /consent to this job's raw network was refused/);
|
|
472
|
+
// A failed turn cancels the queued inbox (outcome.settle). Denying G1 must not silently
|
|
473
|
+
// continue the queued resume: explicitly resume under G2 after that failure.
|
|
474
|
+
const denied = await settled(person, job.job_id);
|
|
475
|
+
assert.equal(denied.status, "failed", JSON.stringify(denied));
|
|
476
|
+
const turns = h.sql("SELECT status, grant_json FROM turns WHERE node_id = ? ORDER BY id", job.job_id);
|
|
477
|
+
assert.deepEqual(turns.map(t => t.status), ["failed", "cancelled"]);
|
|
478
|
+
assert.equal(h.starts().filter(s => s.target === "codex").length, 0, "denied consent starts no Codex agent");
|
|
479
|
+
const strict = await person.call("resume", { job_id: job.job_id, message: "RAWQ strict", grant: { network: true } });
|
|
480
|
+
assert.ok(!strict.refused && !strict.error, JSON.stringify(strict));
|
|
481
|
+
// With stage 2 on, a standing network grant runs on the executor, which this stub Codex doesn't
|
|
482
|
+
// serve, so the turn ends failed at the executor. What matters here happened before that: it
|
|
483
|
+
// ran under G2, with no raw network and no second question.
|
|
484
|
+
await settled(person, job.job_id);
|
|
485
|
+
assert.equal(h.sql("SELECT id FROM requests WHERE kind = 'raw_network'").length, 1, "the explicit G2 resume is not asked");
|
|
486
|
+
const last = h.sql("SELECT status, grant_json FROM turns WHERE node_id = ? ORDER BY id", job.job_id).at(-1);
|
|
487
|
+
assert.ok(!String(last.grant_json ?? "").includes("raw_network"), JSON.stringify(last));
|
|
488
|
+
assert.equal(h.starts().filter((s) => s.target === "codex").length, 0, "the denied and cancelled turns never ran");
|
|
489
|
+
await person.close();
|
|
490
|
+
} finally {
|
|
491
|
+
await h.close();
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
/** Replaces a job's params_json grant directly, as an earlier relay (or a run with stage 2 off) would have stored it, retrying while the worker holds the registry. */
|
|
496
|
+
async function storeGrant(h: Harness, id: string, grant: Rec) {
|
|
497
|
+
await until(() => {
|
|
498
|
+
try {
|
|
499
|
+
const params = JSON.parse(h.sql("SELECT params_json FROM nodes WHERE id = ?", id)[0].params_json);
|
|
500
|
+
h.sql("UPDATE nodes SET params_json = ? WHERE id = ?", JSON.stringify({ ...params, grant }), id);
|
|
501
|
+
return true;
|
|
502
|
+
} catch (e) {
|
|
503
|
+
if ((e as { errcode?: number }).errcode === 5) return false;
|
|
504
|
+
throw e;
|
|
505
|
+
}
|
|
506
|
+
}, 10000, "the registry to take the grant");
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
test("local mode while stage 2 is switched on (MCP): a job holding local_network from before the switch is refused at its turn, asking nobody (review R3)", async () => {
|
|
510
|
+
const h = await harness([{ match: task("PLAIN"), steps: [{ reply: "done" }] }], STAGE2_ON);
|
|
511
|
+
try {
|
|
512
|
+
const job = await h.call("delegate", { target: "codex", task: "PLAIN", mode: "write", background: false });
|
|
513
|
+
assert.equal(job.status, "done", JSON.stringify(job));
|
|
514
|
+
await storeGrant(h, job.job_id, { network: true, local_network: true, write_paths: [] });
|
|
515
|
+
const before = h.starts().length;
|
|
516
|
+
const r = await h.call("resume", { job_id: job.job_id, message: "PLAIN again" });
|
|
517
|
+
assert.ok(!r.refused, JSON.stringify(r));
|
|
518
|
+
const s = await settled(h, job.job_id);
|
|
519
|
+
assert.equal(s.status, "failed", JSON.stringify(s));
|
|
520
|
+
assert.match(s.error, /grant\.local_network is not available while relay's stage 2 is switched on .*So this job's agent was not started: continue it with one of those grants instead of local_network/);
|
|
521
|
+
assert.equal(h.sql("SELECT id FROM requests WHERE kind = 'local_network'").length, 0, "nobody is asked");
|
|
522
|
+
assert.equal(h.starts().length, before, "the agent was not started again");
|
|
523
|
+
} finally {
|
|
524
|
+
await h.close();
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
test("stage 2 switched on by the config alone, no environment variable: the session path records the turn's enforcement and doctor reports stage 2 (review R5)", async () => {
|
|
529
|
+
assert.equal(process.env[STAGE2_FLAG_ENV], undefined, "this test runs without the environment override");
|
|
530
|
+
for (const on of [true, false]) {
|
|
531
|
+
const h = await harness([{ match: task("PLAIN"), steps: [{ reply: "done" }] }], on ? STAGE2_ON : {});
|
|
532
|
+
try {
|
|
533
|
+
const job = await h.call("delegate", { target: "codex", task: "PLAIN", mode: "write", background: false });
|
|
534
|
+
assert.equal(job.status, "done", JSON.stringify(job));
|
|
535
|
+
const turn = h.sql("SELECT id FROM turns WHERE node_id = ? ORDER BY id", job.job_id)[0].id;
|
|
536
|
+
const recorded = JSON.parse(h.sql("SELECT params_json FROM nodes WHERE id = ?", job.job_id)[0].params_json).codex_confinement;
|
|
537
|
+
const doctor = h.cli("doctor");
|
|
538
|
+
if (on) {
|
|
539
|
+
// Codex's own sandbox confined it (no raw network), and the record says so.
|
|
540
|
+
assert.deepEqual(recorded, { turn, enforcement: "codex" });
|
|
541
|
+
assert.match(doctor, /codex stage 2 +/);
|
|
542
|
+
} else {
|
|
543
|
+
assert.equal(recorded, undefined);
|
|
544
|
+
assert.doesNotMatch(doctor, /codex stage 2/);
|
|
545
|
+
}
|
|
546
|
+
} finally {
|
|
547
|
+
await h.close();
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
test("raw_network: false with stage 2 switched off is no grant at all, as before stage 2 (review R9); switched on, it is a grant", async () => {
|
|
553
|
+
for (const on of [false, true]) {
|
|
554
|
+
const h = await harness([{ match: task("PARENT"), steps: [{ write: "parent.txt" }, { reply: "done" }] }, { steps: [{ reply: "done" }] }], on ? STAGE2_ON : {});
|
|
555
|
+
try {
|
|
556
|
+
// The root: off, no grant is stored; on, a grant without raw network (so a resume with it drops raw network).
|
|
557
|
+
const root = await h.call("delegate", { target: "codex", task: "ROOT", mode: "write", background: false, grant: { raw_network: false } });
|
|
558
|
+
assert.ok(root.job_id, JSON.stringify(root));
|
|
559
|
+
assert.deepEqual(root.grant, on ? { network: false, write_paths: [] } : undefined, JSON.stringify(root));
|
|
560
|
+
// A launched agent: off, accepted as no grant; on, refused as any grant from it is.
|
|
561
|
+
const parent = await h.call("delegate", { target: "claude", task: "PARENT", mode: "write", background: false });
|
|
562
|
+
assert.equal(parent.status, "done", JSON.stringify(parent));
|
|
563
|
+
const asParent = await h.instance(await h.childOf(parent.job_id, { access: "write" }));
|
|
564
|
+
const kid = await asParent.call("delegate", { target: "codex", task: "KID", mode: "write", background: false, grant: { raw_network: false } });
|
|
565
|
+
if (on) assert.match(kid.reason, /only the root session can grant extra permissions/, JSON.stringify(kid));
|
|
566
|
+
else assert.ok(kid.job_id && !kid.grant, JSON.stringify(kid));
|
|
567
|
+
await asParent.close();
|
|
568
|
+
} finally {
|
|
569
|
+
await h.close();
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
test("raw network grant (MCP): a launched agent cannot give it, its jobs do not inherit it, and without it an agent cannot drive a job that has it", async () => {
|
|
575
|
+
// The parent leaves work in its worktree, which keeps it for the instance that acts as the parent.
|
|
576
|
+
const h = await harness([{ match: task("PARENT"), steps: [{ write: "parent.txt" }, { reply: "done" }] }, { steps: [{ reply: "done" }] }], STAGE2_ON);
|
|
577
|
+
try {
|
|
578
|
+
// A write job of the root's (Claude, so that the job it starts crosses no family), and the job it starts.
|
|
579
|
+
const parent = await h.call("delegate", { target: "claude", task: "PARENT", mode: "write", background: false });
|
|
580
|
+
assert.equal(parent.status, "done", JSON.stringify(parent));
|
|
581
|
+
const asParent = await h.instance(await h.childOf(parent.job_id, { access: "write" }));
|
|
582
|
+
const given = await asParent.call("delegate", { target: "codex", task: "KID", mode: "write", grant: { network: true, raw_network: true } });
|
|
583
|
+
assert.match(given.reason, /only the root session can grant extra permissions/, JSON.stringify(given));
|
|
584
|
+
const kid = await asParent.call("delegate", { target: "codex", task: "KID", mode: "write", background: false });
|
|
585
|
+
assert.ok(kid.job_id, JSON.stringify(kid));
|
|
586
|
+
assert.equal(JSON.parse(h.sql("SELECT params_json FROM nodes WHERE id = ?", kid.job_id)[0].params_json).grant?.raw_network, undefined, "not inherited");
|
|
587
|
+
// The kid given raw network (as the root's resume would, after the person consented): the parent, without it, can no longer drive it.
|
|
588
|
+
await until(() => {
|
|
589
|
+
try {
|
|
590
|
+
const params = JSON.parse(h.sql("SELECT params_json FROM nodes WHERE id = ?", kid.job_id)[0].params_json);
|
|
591
|
+
h.sql("UPDATE nodes SET params_json = ? WHERE id = ?", JSON.stringify({ ...params, grant: { network: true, raw_network: true, write_paths: [] } }), kid.job_id);
|
|
592
|
+
return true;
|
|
593
|
+
} catch (e) {
|
|
594
|
+
if ((e as { errcode?: number }).errcode === 5) return false;
|
|
595
|
+
throw e;
|
|
596
|
+
}
|
|
597
|
+
}, 10000, "the registry to take the grant");
|
|
598
|
+
assert.match((await asParent.call("resume", { job_id: kid.job_id, message: "go on" })).reason, /more access than your current turn/);
|
|
599
|
+
assert.match((await asParent.call("cancel", { id: kid.job_id })).reason, /more access than your current turn/);
|
|
600
|
+
} finally {
|
|
601
|
+
await h.close();
|
|
602
|
+
}
|
|
603
|
+
});
|