@jbkjr/relay 0.0.0-stage → 1.2.1
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 +386 -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 +1525 -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 +320 -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 +637 -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 +329 -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 +629 -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 +1502 -0
- package/docs/pinned-codex.md +321 -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,647 @@
|
|
|
1
|
+
// Host-only qualification for item 29. One real, installed Claude turn (with stage 2 on, and again off) checks effective settings
|
|
2
|
+
// and native tools; its Bash carrier reaches owned listeners only through Relay's proxy, whose dial
|
|
3
|
+
// runs outside the confinement. Two deterministic ACP-v1 turns check the generic adapter: inside
|
|
4
|
+
// the granted profile, a parent listener synchronised to start after profile compilation is the
|
|
5
|
+
// positive loopback control and pre-start listeners are negative controls. The parent separately drives the production public
|
|
6
|
+
// proxy for exact built-in/user never decisions; that evidence is outside ACP confinement.
|
|
7
|
+
import "./helpers.ts";
|
|
8
|
+
import assert from "node:assert/strict";
|
|
9
|
+
import dns from "node:dns";
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import http from "node:http";
|
|
12
|
+
import net from "node:net";
|
|
13
|
+
import os from "node:os";
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
import { test, type TestContext } from "node:test";
|
|
16
|
+
import { startAcpTurn, genericApproval, type AcpProfile } from "../dist/backends/acp.js";
|
|
17
|
+
import { CLAUDE_KEPT_BUILTIN, claudeBackend, claudeSkillsProblem, keepsPrivacyPlugin } from "../dist/backends/claude.js";
|
|
18
|
+
import { managedSettingsPaths } from "../dist/core/claude-settings.js";
|
|
19
|
+
import { withDefaults } from "../dist/core/config.js";
|
|
20
|
+
import { resolveNeverPolicy } from "../dist/core/never-policy.js";
|
|
21
|
+
import { neverKeychainIpc } from "../dist/core/never-coverage.js";
|
|
22
|
+
import { spawnSync } from "node:child_process";
|
|
23
|
+
import { attempt, decoyKeychain, keychainSkip, retrieved, withTeardown, wrapped, type DecoyKeychain } from "./keychain-decoy.ts";
|
|
24
|
+
import { openProxy, type TurnProxy } from "../dist/confine/proxy.js";
|
|
25
|
+
import { busyPorts } from "../dist/confine/ports.js";
|
|
26
|
+
import { targetDef } from "../dist/targets/index.js";
|
|
27
|
+
import { withTurnSecrets } from "../dist/core/secrets.js";
|
|
28
|
+
import type { TurnHandle, TurnResult, TurnSpec } from "../dist/backends/types.js";
|
|
29
|
+
|
|
30
|
+
const skip = process.env.RELAY_TEST_STAGE2_NATIVE_LIVE !== "1" ? "host only: RELAY_TEST_STAGE2_NATIVE_LIVE=1"
|
|
31
|
+
: process.platform !== "darwin" ? "macOS only" : false;
|
|
32
|
+
// The installed Claude Code: the first `claude` on PATH, else the native installer's place under the
|
|
33
|
+
// real home folder (from the user database: HOME is a scratch folder here).
|
|
34
|
+
const CLAUDE = (process.env.PATH ?? "").split(":").filter(Boolean).map((dir) => path.join(dir, "claude"))
|
|
35
|
+
.find((candidate) => fs.existsSync(candidate)) ?? path.join(os.userInfo().homedir, ".local/bin/claude");
|
|
36
|
+
const ACP_FIXTURE = path.join(import.meta.dirname, "fixtures/stage2-acp-native.mjs");
|
|
37
|
+
const NETWORK_FIXTURE = path.join(import.meta.dirname, "fixtures/stage2-native-network.mjs");
|
|
38
|
+
const SERVICE_PORTS = [22, 5900] as const;
|
|
39
|
+
const ALLOWED_HOST = "relay-stage2-owned.invalid";
|
|
40
|
+
const MODEL_HOST = "model.relay-stage2.invalid";
|
|
41
|
+
|
|
42
|
+
type Sink = { server: net.Server; port: number; accepts: string[]; payloads: string[]; close(): Promise<void> };
|
|
43
|
+
|
|
44
|
+
function stringsIn(value: unknown): string[] {
|
|
45
|
+
if (typeof value === "string") return [value];
|
|
46
|
+
if (Array.isArray(value)) return value.flatMap(stringsIn);
|
|
47
|
+
if (value && typeof value === "object") return Object.values(value).flatMap(stringsIn);
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function sink(port = 0): Promise<Sink> {
|
|
52
|
+
const accepts: string[] = [], payloads: string[] = [];
|
|
53
|
+
const server = net.createServer((socket) => {
|
|
54
|
+
accepts.push(`${socket.remoteAddress ?? "?"}:${socket.remotePort ?? 0}`);
|
|
55
|
+
socket.setEncoding("utf8");
|
|
56
|
+
socket.once("data", (data) => {
|
|
57
|
+
payloads.push(String(data).slice(0, 200));
|
|
58
|
+
if (/^GET /.test(data)) socket.end("HTTP/1.1 200 OK\r\nContent-Length: 17\r\nConnection: close\r\n\r\nOWNED_ALLOWED_OK\n");
|
|
59
|
+
else socket.end("OWNED_ALLOWED_OK\n");
|
|
60
|
+
});
|
|
61
|
+
socket.once("error", () => {});
|
|
62
|
+
});
|
|
63
|
+
await new Promise<void>((resolve, reject) => {
|
|
64
|
+
server.once("error", reject);
|
|
65
|
+
server.listen({ host: "127.0.0.1", port, exclusive: true }, () => { server.off("error", reject); resolve(); });
|
|
66
|
+
});
|
|
67
|
+
return {
|
|
68
|
+
server, port: (server.address() as net.AddressInfo).port, accepts, payloads,
|
|
69
|
+
close: () => new Promise<void>((resolve) => { server.close(() => resolve()); }),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function ownedNeverSinks(count = 2): Promise<Sink[]> {
|
|
74
|
+
const out: Sink[] = [];
|
|
75
|
+
try {
|
|
76
|
+
for (let attempts = 0; out.length < count && attempts < 200; attempts++) {
|
|
77
|
+
const port = 30_000 + Math.floor(Math.random() * 10_000);
|
|
78
|
+
if (out.some(item => item.port === port)) continue;
|
|
79
|
+
if (busyPorts([port]).has(port)) continue;
|
|
80
|
+
try { out.push(await sink(port)); }
|
|
81
|
+
catch (error) {
|
|
82
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
83
|
+
if (code !== "EADDRINUSE") throw error;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
assert.equal(out.length, count, `could not bind ${count} exclusively owned high-port never decoys`);
|
|
87
|
+
return out;
|
|
88
|
+
} catch (error) {
|
|
89
|
+
await Promise.all(out.map(item => item.close()));
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function lateSink(excluded: ReadonlySet<number>): Promise<Sink> {
|
|
95
|
+
for (let attempt = 0; attempt < 20; attempt++) {
|
|
96
|
+
const candidate = await sink();
|
|
97
|
+
if (!excluded.has(candidate.port)) return candidate;
|
|
98
|
+
await candidate.close();
|
|
99
|
+
}
|
|
100
|
+
throw new Error("could not open a post-start listener outside the denied port sets");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function scratchHome(): string {
|
|
104
|
+
// At this length Claude's private TMPDIR plus its claude-<uid> suffix is at most 44 bytes, so the
|
|
105
|
+
// qualification never creates or changes relay's /Users/Shared native-temp alias.
|
|
106
|
+
const home = fs.realpathSync(fs.mkdtempSync("/Users/Shared/r-"));
|
|
107
|
+
return home;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function saveProcessEnv() {
|
|
111
|
+
const names = ["HOME", "RELAY_HOME", "RELAY_CONFIG", "RELAY_WORKTREES", "RELAY_DEV_MODEL_STUB", "RELAY_EXPERIMENTAL_CODEX_STAGE2"] as const;
|
|
112
|
+
const saved = Object.fromEntries(names.map((name) => [name, process.env[name]]));
|
|
113
|
+
return () => {
|
|
114
|
+
for (const name of names) {
|
|
115
|
+
const value = saved[name];
|
|
116
|
+
if (value === undefined) delete process.env[name]; else process.env[name] = value;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function useScratch(home: string, stateRoot = home, modelStub = false) {
|
|
122
|
+
process.env.HOME = home;
|
|
123
|
+
process.env.RELAY_HOME = path.join(stateRoot, "relay");
|
|
124
|
+
process.env.RELAY_CONFIG = path.join(stateRoot, "relay-config.json");
|
|
125
|
+
process.env.RELAY_WORKTREES = path.join(stateRoot, "worktrees");
|
|
126
|
+
if (modelStub) process.env.RELAY_DEV_MODEL_STUB = "1";
|
|
127
|
+
else delete process.env.RELAY_DEV_MODEL_STUB;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
type ModelStep = { name: "Read" | "Edit" | "Bash"; input: Record<string, unknown> };
|
|
131
|
+
type ScriptedModel = {
|
|
132
|
+
server: http.Server;
|
|
133
|
+
port: number;
|
|
134
|
+
issued: string[];
|
|
135
|
+
results: Set<string>;
|
|
136
|
+
errors: string[];
|
|
137
|
+
close(): Promise<void>;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/** A prompt-independent Anthropic Messages stub: one tool call per response, then end_turn. */
|
|
141
|
+
async function scriptedModel(steps: readonly ModelStep[]): Promise<ScriptedModel> {
|
|
142
|
+
const issued: string[] = [], results = new Set<string>(), errors: string[] = [];
|
|
143
|
+
let next = 0;
|
|
144
|
+
const server = http.createServer((req, res) => {
|
|
145
|
+
let body = "";
|
|
146
|
+
req.setEncoding("utf8");
|
|
147
|
+
req.on("data", chunk => { body += chunk; });
|
|
148
|
+
req.on("end", () => {
|
|
149
|
+
if (req.url?.includes("count_tokens")) {
|
|
150
|
+
res.writeHead(200, { "content-type": "application/json", connection: "close" });
|
|
151
|
+
return void res.end('{"input_tokens":1}');
|
|
152
|
+
}
|
|
153
|
+
if (!req.url?.startsWith("/v1/messages")) {
|
|
154
|
+
res.writeHead(404, { "content-type": "application/json", connection: "close" });
|
|
155
|
+
return void res.end('{"type":"error","error":{"type":"not_found_error","message":"stage2 stub"}}');
|
|
156
|
+
}
|
|
157
|
+
let request: any;
|
|
158
|
+
try { request = JSON.parse(body); }
|
|
159
|
+
catch {
|
|
160
|
+
errors.push("model request was not JSON");
|
|
161
|
+
res.writeHead(400, { connection: "close" });
|
|
162
|
+
return void res.end();
|
|
163
|
+
}
|
|
164
|
+
for (const message of request.messages ?? []) for (const block of Array.isArray(message.content) ? message.content : []) {
|
|
165
|
+
if (block?.type === "tool_result" && /^toolu_stage2_\d+$/.test(block.tool_use_id ?? "")) results.add(block.tool_use_id);
|
|
166
|
+
}
|
|
167
|
+
const hasNativeTools = (request.tools ?? []).some((tool: any) => tool?.name === "Read")
|
|
168
|
+
&& (request.tools ?? []).some((tool: any) => tool?.name === "Edit")
|
|
169
|
+
&& (request.tools ?? []).some((tool: any) => tool?.name === "Bash");
|
|
170
|
+
let block: any;
|
|
171
|
+
if (!hasNativeTools) {
|
|
172
|
+
block = { type: "text", text: "stage2 auxiliary request" };
|
|
173
|
+
} else if (next > 0 && !results.has(`toolu_stage2_${next - 1}`)) {
|
|
174
|
+
errors.push(`request for step ${next} arrived before toolu_stage2_${next - 1}'s result`);
|
|
175
|
+
res.writeHead(409, { "content-type": "application/json", connection: "close" });
|
|
176
|
+
return void res.end('{"type":"error","error":{"type":"invalid_request_error","message":"missing scripted tool result"}}');
|
|
177
|
+
} else if (next < steps.length) {
|
|
178
|
+
const step = steps[next];
|
|
179
|
+
block = { type: "tool_use", id: `toolu_stage2_${next}`, ...step };
|
|
180
|
+
issued.push(step.name);
|
|
181
|
+
next++;
|
|
182
|
+
} else {
|
|
183
|
+
block = { type: "text", text: "QUALIFICATION_DONE" };
|
|
184
|
+
}
|
|
185
|
+
const stop = block.type === "tool_use" ? "tool_use" : "end_turn";
|
|
186
|
+
const message = { id: `msg_stage2_${next}`, type: "message", role: "assistant",
|
|
187
|
+
model: request.model ?? "claude-haiku-stub", content: [], stop_reason: null, stop_sequence: null,
|
|
188
|
+
usage: { input_tokens: 1, output_tokens: 1 } };
|
|
189
|
+
if (!request.stream) {
|
|
190
|
+
res.writeHead(200, { "content-type": "application/json", connection: "close" });
|
|
191
|
+
return void res.end(JSON.stringify({ ...message, content: [block], stop_reason: stop }));
|
|
192
|
+
}
|
|
193
|
+
res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache", connection: "close" });
|
|
194
|
+
const event = (type: string, data: Record<string, unknown>) =>
|
|
195
|
+
res.write(`event: ${type}\ndata: ${JSON.stringify({ type, ...data })}\n\n`);
|
|
196
|
+
event("message_start", { message });
|
|
197
|
+
event("content_block_start", { index: 0, content_block: block.type === "tool_use"
|
|
198
|
+
? { type: "tool_use", id: block.id, name: block.name, input: {} } : { type: "text", text: "" } });
|
|
199
|
+
event("content_block_delta", { index: 0, delta: block.type === "tool_use"
|
|
200
|
+
? { type: "input_json_delta", partial_json: JSON.stringify(block.input) }
|
|
201
|
+
: { type: "text_delta", text: block.text } });
|
|
202
|
+
event("content_block_stop", { index: 0 });
|
|
203
|
+
event("message_delta", { delta: { stop_reason: stop, stop_sequence: null }, usage: { output_tokens: 1 } });
|
|
204
|
+
event("message_stop", {});
|
|
205
|
+
res.end();
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
await new Promise<void>((resolve, reject) => {
|
|
209
|
+
server.once("error", reject);
|
|
210
|
+
server.listen(0, "127.0.0.1", () => { server.off("error", reject); resolve(); });
|
|
211
|
+
});
|
|
212
|
+
return { server, port: (server.address() as net.AddressInfo).port, issued, results, errors,
|
|
213
|
+
close: () => new Promise<void>(resolve => {
|
|
214
|
+
server.closeAllConnections();
|
|
215
|
+
server.close(() => resolve());
|
|
216
|
+
}) };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Established development route: set only the stub endpoint/key, then exec the exact installed CLI. */
|
|
220
|
+
function claudeStubLauncher(root: string): string {
|
|
221
|
+
const launcher = path.join(root, "claude-stage2-stub");
|
|
222
|
+
fs.writeFileSync(launcher, `#!/bin/sh
|
|
223
|
+
export ANTHROPIC_BASE_URL=${JSON.stringify(`http://${MODEL_HOST}`)}
|
|
224
|
+
export ANTHROPIC_API_KEY=sk-ant-stub-0000
|
|
225
|
+
exec ${JSON.stringify(CLAUDE)} "$@"
|
|
226
|
+
`, { mode: 0o700 });
|
|
227
|
+
return launcher;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function fixtureFiles(home: string) {
|
|
231
|
+
const cwd = path.join(home, "work");
|
|
232
|
+
const outside = path.join(home, "outside");
|
|
233
|
+
fs.mkdirSync(cwd); fs.mkdirSync(outside);
|
|
234
|
+
const allowedFile = path.join(cwd, "allowed.txt");
|
|
235
|
+
const forbiddenFile = path.join(outside, "forbidden.txt");
|
|
236
|
+
const networkProbe = path.join(cwd, "stage2-native-network.mjs");
|
|
237
|
+
fs.writeFileSync(allowedFile, "ALLOWED_READ_NONCE_29\n");
|
|
238
|
+
fs.writeFileSync(forbiddenFile, "FORBIDDEN_UNCHANGED_29\n");
|
|
239
|
+
fs.copyFileSync(NETWORK_FIXTURE, networkProbe);
|
|
240
|
+
return { cwd, allowedFile, forbiddenFile, allowedWrite: path.join(cwd, "acp-write.txt"), networkProbe };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function policy(home: string, forbiddenFile: string, neverPorts: readonly number[]) {
|
|
244
|
+
const p = resolveNeverPolicy({ home, never: {
|
|
245
|
+
paths: [{ path: forbiddenFile }],
|
|
246
|
+
ports: neverPorts.map(port => ({ port, transport: "both" as const, destinations: "this-machine" as const })),
|
|
247
|
+
} });
|
|
248
|
+
assert.deepEqual(p.ports.map(entry => entry.port).sort((a, b) => a - b),
|
|
249
|
+
[...SERVICE_PORTS, ...neverPorts].sort((a, b) => a - b), "the exact built-in list plus owned user-never decoys");
|
|
250
|
+
assert.deepEqual(p.ports.filter(entry => entry.id.startsWith("service.")).map(entry => entry.port).sort((a, b) => a - b),
|
|
251
|
+
[...SERVICE_PORTS].sort((a, b) => a - b), "every built-in service port remains in the floor");
|
|
252
|
+
return p;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async function complete(starting: Promise<TurnHandle>): Promise<TurnResult> {
|
|
256
|
+
const handle = await starting;
|
|
257
|
+
let done = false;
|
|
258
|
+
try { const result = await handle.done; done = true; return result; }
|
|
259
|
+
finally { if (!done) await handle.cancel(); }
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function redirectOwnedDial(t: TestContext, allowed: Sink, nestedProxyPort?: number) {
|
|
263
|
+
const connect = net.connect;
|
|
264
|
+
t.mock.method(net, "connect", function(...args: any[]) {
|
|
265
|
+
const options = args[0];
|
|
266
|
+
if (options && typeof options === "object" && options.host === "8.8.8.8") {
|
|
267
|
+
if (nestedProxyPort !== undefined && options.port === nestedProxyPort)
|
|
268
|
+
return connect.call(net, { ...options, host: "127.0.0.1", port: nestedProxyPort }, ...args.slice(1));
|
|
269
|
+
if (options.port === allowed.port || options.port === 443)
|
|
270
|
+
return connect.call(net, { ...options, host: "127.0.0.1", port: allowed.port }, ...args.slice(1));
|
|
271
|
+
}
|
|
272
|
+
return connect.apply(net, args as any);
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function proxyTunnel(raw: string, host: string, port: number, payload = ""): Promise<{ ok: boolean; code?: string; data?: string }> {
|
|
277
|
+
return new Promise(resolve => {
|
|
278
|
+
const proxy = new URL(raw);
|
|
279
|
+
const auth = Buffer.from(`${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`).toString("base64");
|
|
280
|
+
const socket = net.connect({ host: proxy.hostname, port: Number(proxy.port) });
|
|
281
|
+
let data = "", established = false, settled = false;
|
|
282
|
+
const done = (answer: { ok: boolean; code?: string; data?: string }) => {
|
|
283
|
+
if (settled) return;
|
|
284
|
+
settled = true; socket.destroy(); resolve(answer);
|
|
285
|
+
};
|
|
286
|
+
socket.setTimeout(3000, () => done({ ok: false, code: "ETIMEDOUT" }));
|
|
287
|
+
socket.once("connect", () => socket.write(
|
|
288
|
+
`CONNECT ${host}:${port} HTTP/1.1\r\nHost: ${host}:${port}\r\nProxy-Authorization: Basic ${auth}\r\nConnection: close\r\n\r\n`,
|
|
289
|
+
));
|
|
290
|
+
socket.on("data", chunk => {
|
|
291
|
+
data += chunk;
|
|
292
|
+
if (!established) {
|
|
293
|
+
const end = data.indexOf("\r\n\r\n");
|
|
294
|
+
if (end < 0) return;
|
|
295
|
+
const head = data.slice(0, end); data = data.slice(end + 4);
|
|
296
|
+
if (!/^HTTP\/1\.[01] 200\b/.test(head)) return done({ ok: false, code: /^HTTP\/1\.[01] (\d+)/.exec(head)?.[1] ?? "BAD_PROXY_REPLY" });
|
|
297
|
+
established = true;
|
|
298
|
+
if (payload) socket.write(payload); else return done({ ok: true });
|
|
299
|
+
}
|
|
300
|
+
if (data.includes("\n") || data.includes("ALLOWED")) done({ ok: true, data: data.slice(0, 120) });
|
|
301
|
+
});
|
|
302
|
+
socket.once("error", error => done({ ok: false, code: String((error as NodeJS.ErrnoException).code ?? error.message) }));
|
|
303
|
+
socket.once("close", () => done({ ok: false, code: established ? "ECONNCLOSED" : "NO_PROXY_REPLY" }));
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Claude takes the floor whatever stage 2 says (core/never-coverage.ts), so the same installed-CLI
|
|
308
|
+
// case runs with stage 2 on and off. Item 29 (the Codex pin) runs the stage-2-on variant; the
|
|
309
|
+
// stage-2-off one is release check claude-floor, part nativeStage2Off.
|
|
310
|
+
for (const stage2 of [true, false]) test(`stage2 native live: installed Claude settings and native tools enforce the never floor (stage 2 ${stage2 ? "on" : "off"})`, { skip, timeout: 300_000 }, async (t) => {
|
|
311
|
+
const restore = saveProcessEnv();
|
|
312
|
+
if (!stage2) delete process.env.RELAY_EXPERIMENTAL_CODEX_STAGE2;
|
|
313
|
+
const home = scratchHome();
|
|
314
|
+
const runRoot = fs.realpathSync(fs.mkdtempSync(path.join(home, "run-")));
|
|
315
|
+
let allowed: Sink | undefined;
|
|
316
|
+
let neverSinks: Sink[] = [];
|
|
317
|
+
let publicProxy: TurnProxy | undefined;
|
|
318
|
+
let model: ScriptedModel | undefined;
|
|
319
|
+
try {
|
|
320
|
+
useScratch(home, runRoot, true);
|
|
321
|
+
fs.mkdirSync(process.env.RELAY_HOME!, { recursive: true });
|
|
322
|
+
const files = fixtureFiles(runRoot);
|
|
323
|
+
allowed = await sink();
|
|
324
|
+
neverSinks = await ownedNeverSinks();
|
|
325
|
+
const lookup = dns.promises.lookup.bind(dns.promises);
|
|
326
|
+
t.mock.method(dns.promises, "lookup", ((host: string, options: any) => host === ALLOWED_HOST
|
|
327
|
+
? Promise.resolve(options?.all ? [{ address: "8.8.8.8", family: 4 }] : { address: "8.8.8.8", family: 4 })
|
|
328
|
+
: lookup(host, options)) as typeof dns.promises.lookup);
|
|
329
|
+
const floor = policy(home, files.forbiddenFile, neverSinks.map(item => item.port));
|
|
330
|
+
for (let attempt = 0; attempt < 10; attempt++) {
|
|
331
|
+
publicProxy = await openProxy({ neverPolicy: floor, mode: "public", allow: [],
|
|
332
|
+
logFile: path.join(runRoot, "claude-public-proxy.jsonl"), label: "claude-native/public" });
|
|
333
|
+
if (!floor.authorityPorts.includes(publicProxy.port)) break;
|
|
334
|
+
await publicProxy.close(); publicProxy = undefined;
|
|
335
|
+
}
|
|
336
|
+
assert.ok(publicProxy, "could not open the Claude test's nested public proxy outside relay's reserved port range");
|
|
337
|
+
redirectOwnedDial(t, allowed, publicProxy.port);
|
|
338
|
+
const publicProxyUrl = `http://relay:${publicProxy.token}@127.0.0.1:${publicProxy.port}`;
|
|
339
|
+
// Built-ins are never bound or dialled directly: the Bash fixture explicitly sends CONNECT
|
|
340
|
+
// requests through Claude's own proxy to a second production public proxy and each must log a
|
|
341
|
+
// service.* refusal. The outer proxy's final public-IP dial is deterministically redirected to
|
|
342
|
+
// that owned proxy. The two owned high ports are the actual-dial canaries: nested proxy requests
|
|
343
|
+
// and harmless direct connects must leave their accept logs empty. The allowed sink must accept.
|
|
344
|
+
const networkPlan = JSON.stringify({ carrierHost: "8.8.8.8", carrierPort: publicProxy.port,
|
|
345
|
+
publicHost: ALLOWED_HOST, allowedPort: allowed.port, servicePorts: SERVICE_PORTS,
|
|
346
|
+
neverPorts: neverSinks.map(item => item.port) });
|
|
347
|
+
const bash = [
|
|
348
|
+
`/bin/cat ${JSON.stringify(files.forbiddenFile)}`,
|
|
349
|
+
`${JSON.stringify(process.execPath)} ${JSON.stringify(files.networkProbe)} ${JSON.stringify(networkPlan)}`,
|
|
350
|
+
].join(" ; ");
|
|
351
|
+
const steps: ModelStep[] = [
|
|
352
|
+
{ name: "Read", input: { file_path: files.allowedFile } },
|
|
353
|
+
{ name: "Read", input: { file_path: files.forbiddenFile } },
|
|
354
|
+
{ name: "Edit", input: { file_path: files.forbiddenFile, old_string: "FORBIDDEN_UNCHANGED_29\n", new_string: "CHANGED_BY_CLAUDE\n" } },
|
|
355
|
+
{ name: "Bash", input: { command: bash, description: "Run the fixed stage2 native network probe" } },
|
|
356
|
+
];
|
|
357
|
+
model = await scriptedModel(steps);
|
|
358
|
+
const stubFile = path.join(process.env.RELAY_HOME!, "dev", "claude-model-stub.json");
|
|
359
|
+
fs.mkdirSync(path.dirname(stubFile), { recursive: true, mode: 0o700 });
|
|
360
|
+
fs.writeFileSync(stubFile, JSON.stringify({ host: MODEL_HOST, port: model.port }));
|
|
361
|
+
const launcher = claudeStubLauncher(runRoot);
|
|
362
|
+
const logPath = path.join(runRoot, "claude-agent.log");
|
|
363
|
+
assert.ok(fs.existsSync(CLAUDE), `installed Claude missing at ${CLAUDE}`);
|
|
364
|
+
assert.deepEqual(managedSettingsPaths().filter(candidate => fs.existsSync(candidate)), [],
|
|
365
|
+
"an existing system managed-settings source is never overwritten; relay's floored Claude route refuses it instead");
|
|
366
|
+
const config = withDefaults({ allow_temp_home: true, escalation: false, child_settings: "isolated", child_hooks: false,
|
|
367
|
+
child_mcp: "relay-only", child_env: { pass: ["STAGE2_CLAUDE_NESTED_PROXY"] },
|
|
368
|
+
targets: { claude: { bin: launcher, child_auth: "any" }, codex: { stage2 } } });
|
|
369
|
+
// The prompt carries no tool instructions. The owned Messages stub above drives the exact
|
|
370
|
+
// sequence and advances only after Claude Code returns each tool result, including refusals.
|
|
371
|
+
// This uses the real installed CLI and Relay's production settings/proxy, but deliberately
|
|
372
|
+
// does not prove real Anthropic/model transport, provider login, quota, or model behaviour.
|
|
373
|
+
const spec: TurnSpec = { target: targetDef("claude"), config, cwd: files.cwd, access: "write", grant: { network: true },
|
|
374
|
+
model: "haiku", prompt: "stage2 native qualification", dir: runRoot, logPath, relay: null, markers: { runId: "stage2-native", nodeId: "claude-native", depth: 1 },
|
|
375
|
+
neverPolicy: floor, environment: { HOME: home, USER: os.userInfo().username,
|
|
376
|
+
PATH: [...new Set([path.dirname(CLAUDE), "/opt/homebrew/bin", "/usr/bin", "/bin", "/usr/sbin", "/sbin"])].join(":"), SHELL: "/bin/zsh", LANG: "C.UTF-8", STAGE2_CLAUDE_NESTED_PROXY: publicProxyUrl },
|
|
377
|
+
environmentReleased: ["STAGE2_CLAUDE_NESTED_PROXY"],
|
|
378
|
+
signal: t.signal, onApproval: async () => { throw new Error("item 29 has no approval route"); } };
|
|
379
|
+
const result = await withTurnSecrets(() => complete(claudeBackend.start(spec)));
|
|
380
|
+
const log = fs.readFileSync(logPath, "utf8");
|
|
381
|
+
assert.equal(result.status, "ok", JSON.stringify({ result, log: log.slice(-8000) }));
|
|
382
|
+
assert.ok(result.sessionId, "installed Claude returned a real session id");
|
|
383
|
+
assert.match(log, /"--setting-sources" ""/, "production launch isolated user/project setting sources");
|
|
384
|
+
assert.match(log, /"--settings" "[^"]+\/claude-settings\.json"/, "production launch handed Claude relay's compiled runtime settings");
|
|
385
|
+
assert.deepEqual(result.served, undefined, "Claude's native stream does not synthesize ACP served-model evidence");
|
|
386
|
+
assert.equal(fs.readFileSync(files.forbiddenFile, "utf8"), "FORBIDDEN_UNCHANGED_29\n", "Read/Edit/Bash never changed the owned forbidden file");
|
|
387
|
+
assert.ok(allowed.accepts.length > 0, "the allowed network control reached its listener");
|
|
388
|
+
assert.ok(allowed.payloads.some(payload => /CLAUDE_PROXY_PING/.test(payload)), allowed.payloads.join("\n"));
|
|
389
|
+
for (const decoy of neverSinks) assert.deepEqual(decoy.accepts, [], `Claude made no actual dial to owned user-never port ${decoy.port}`);
|
|
390
|
+
|
|
391
|
+
const events = log.split("\n").filter(line => line.trim().startsWith("{")).map(line => JSON.parse(line));
|
|
392
|
+
const init = events.find(event => event.type === "system" && event.subtype === "init");
|
|
393
|
+
assert.equal(init?.permissionMode, "acceptEdits", "installed Claude read back relay's effective permission mode");
|
|
394
|
+
for (const tool of ["Bash", "Read", "Edit"]) assert.ok(init?.tools?.includes(tool), `installed Claude readback includes ${tool}`);
|
|
395
|
+
// On a version where relay read cc-plugin-privacy, that built-in stays on; nothing else may load.
|
|
396
|
+
const kept = keepsPrivacyPlugin(String(init?.claude_code_version ?? ""))
|
|
397
|
+
? [{ name: CLAUDE_KEPT_BUILTIN, path: "builtin", source: `${CLAUDE_KEPT_BUILTIN}@builtin` }] : [];
|
|
398
|
+
assert.deepEqual(init?.plugins, kept, "installed Claude loaded no unapproved plugin");
|
|
399
|
+
assert.equal(claudeSkillsProblem(init as Record<string, unknown>, undefined), null, "relay's own init check admits what installed Claude loaded");
|
|
400
|
+
assert.deepEqual(init?.skills, [], "installed Claude loaded no unapproved skill");
|
|
401
|
+
assert.equal(init?.apiKeySource, "ANTHROPIC_API_KEY", "the stub uses only its owned dummy key under child_auth any");
|
|
402
|
+
const uses = events.flatMap(event => event.type === "assistant" && Array.isArray(event.message?.content) ? event.message.content : [])
|
|
403
|
+
.filter(block => block?.type === "tool_use");
|
|
404
|
+
assert.deepEqual(uses.filter(block => /^toolu_stage2_\d+$/.test(block.id ?? "")).map(block => block.name),
|
|
405
|
+
steps.map(step => step.name), "the real native tool attempts appeared in the stream exactly once and in scripted order");
|
|
406
|
+
for (const tool of ["Bash", "Read", "Edit"]) assert.ok(uses.some(block => block.name === tool), `real turn attempted native ${tool}`);
|
|
407
|
+
assert.ok(uses.some(block => block.name === "Read" && block.input?.file_path === files.allowedFile), "positive Read control used the exact allowed file");
|
|
408
|
+
assert.ok(uses.some(block => block.name === "Read" && block.input?.file_path === files.forbiddenFile), "native Read tried the exact never file");
|
|
409
|
+
assert.ok(uses.some(block => block.name === "Edit" && block.input?.file_path === files.forbiddenFile), "native Edit tried the exact never file");
|
|
410
|
+
assert.ok(uses.some(block => block.name === "Bash" && block.input?.command === bash), "native Bash ran the fixed file/network probe");
|
|
411
|
+
assert.deepEqual(model.issued, steps.map(step => step.name), "the stub issued every scripted tool call exactly once and in order");
|
|
412
|
+
assert.deepEqual([...model.results].sort(), steps.map((_, i) => `toolu_stage2_${i}`), "Claude Code returned a result for every real tool attempt");
|
|
413
|
+
assert.deepEqual(model.errors, [], "the installed CLI followed the scripted Messages/tool-result protocol");
|
|
414
|
+
assert.match(log, /ALLOWED_READ_NONCE_29/, "the allowed Read tool returned its owned nonce");
|
|
415
|
+
const networkText = events.flatMap(stringsIn).find(text => text.includes("STAGE2_NATIVE_NETWORK:")) ?? "";
|
|
416
|
+
const networkLine = /STAGE2_NATIVE_NETWORK:(\{[^\r\n]+\})/.exec(networkText);
|
|
417
|
+
assert.ok(networkLine, "the fixed Bash network fixture returned evidence");
|
|
418
|
+
const networkEvidence = JSON.parse(networkLine[1]);
|
|
419
|
+
assert.equal(networkEvidence.allowed.ok, true, JSON.stringify(networkEvidence.allowed));
|
|
420
|
+
for (const { port } of neverSinks) {
|
|
421
|
+
const evidence = networkEvidence.neverPorts.find((entry: any) => entry.port === port);
|
|
422
|
+
assert.ok(evidence && evidence.proxy.ok === false && evidence.direct.ok === false, JSON.stringify(networkEvidence));
|
|
423
|
+
}
|
|
424
|
+
assert.ok(result.denials?.some(denial => denial.includes(files.forbiddenFile) && /Read/.test(denial)), JSON.stringify(result.denials));
|
|
425
|
+
assert.ok(result.denials?.some(denial => denial.includes(files.forbiddenFile) && /Edit/.test(denial)), JSON.stringify(result.denials));
|
|
426
|
+
const modelDecisions = fs.readFileSync(path.join(runRoot, "proxy.jsonl"), "utf8").trim().split("\n").map(line => JSON.parse(line));
|
|
427
|
+
assert.ok(modelDecisions.some(d => d.event === "allow" && d.target === `${MODEL_HOST}:80` && d.token === true && d.stub === true),
|
|
428
|
+
`the installed CLI reached the owned model only through Relay's authenticated stub mapping: ${JSON.stringify(modelDecisions)}`);
|
|
429
|
+
const decisions = fs.readFileSync(path.join(runRoot, "claude-public-proxy.jsonl"), "utf8").trim().split("\n").map(line => JSON.parse(line));
|
|
430
|
+
for (const port of SERVICE_PORTS) assert.ok(decisions.some(d => d.event === "deny" && d.target === `127.0.0.1:${port}` && d.reason === "refused"), `proxy recorded the built-in service denial for ${port}: ${JSON.stringify(decisions)}`);
|
|
431
|
+
for (const { port } of neverSinks) assert.ok(decisions.some(d => d.event === "deny" && d.target === `127.0.0.1:${port}` && d.reason === "refused"), `proxy recorded the user-never denial for ${port}: ${JSON.stringify(decisions)}`);
|
|
432
|
+
t.diagnostic("installed Claude binary with owned Messages stub: no provider login, quota, or real model transport");
|
|
433
|
+
} finally {
|
|
434
|
+
t.mock.restoreAll();
|
|
435
|
+
await publicProxy?.close();
|
|
436
|
+
await model?.close();
|
|
437
|
+
await Promise.all([...neverSinks, ...(allowed ? [allowed] : [])].map(item => item.close()));
|
|
438
|
+
restore();
|
|
439
|
+
fs.rmSync(home, { recursive: true, force: true });
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
test("stage2 native live: scripted generic ACP profile and proxy enforce the never floor", { skip, timeout: 120_000 }, async (t) => {
|
|
444
|
+
const restore = saveProcessEnv();
|
|
445
|
+
const home = scratchHome();
|
|
446
|
+
let allowed: Sink | undefined;
|
|
447
|
+
let neverSinks: Sink[] = [];
|
|
448
|
+
let publicProxy: TurnProxy | undefined;
|
|
449
|
+
let lateAllowed: Sink | undefined;
|
|
450
|
+
let directHandle: TurnHandle | undefined, directDone = false;
|
|
451
|
+
try {
|
|
452
|
+
useScratch(home);
|
|
453
|
+
const files = fixtureFiles(home);
|
|
454
|
+
allowed = await sink();
|
|
455
|
+
neverSinks = await ownedNeverSinks();
|
|
456
|
+
const lookup = dns.promises.lookup.bind(dns.promises);
|
|
457
|
+
t.mock.method(dns.promises, "lookup", ((host: string, options: any) => host === ALLOWED_HOST
|
|
458
|
+
? Promise.resolve(options?.all ? [{ address: "8.8.8.8", family: 4 }] : { address: "8.8.8.8", family: 4 })
|
|
459
|
+
: lookup(host, options)) as typeof dns.promises.lookup);
|
|
460
|
+
redirectOwnedDial(t, allowed);
|
|
461
|
+
const floor = policy(home, files.forbiddenFile, neverSinks.map(item => item.port));
|
|
462
|
+
// Exact built-in/user proxy evidence is driven here, outside the confined ACP turn. The direct
|
|
463
|
+
// turn receives no route to this proxy; its synchronised post-start listener is the control.
|
|
464
|
+
publicProxy = await openProxy({ neverPolicy: floor, mode: "public", allow: [],
|
|
465
|
+
logFile: path.join(home, "acp-public-proxy.jsonl"), label: "acp-native/public" });
|
|
466
|
+
const publicProxyUrl = `http://relay:${publicProxy.token}@127.0.0.1:${publicProxy.port}`;
|
|
467
|
+
const parentProxyAllowed = await proxyTunnel(publicProxyUrl, ALLOWED_HOST, allowed.port, "ACP_PARENT_PROXY_PING\n");
|
|
468
|
+
assert.equal(parentProxyAllowed.ok, true, JSON.stringify(parentProxyAllowed));
|
|
469
|
+
assert.match(parentProxyAllowed.data ?? "", /OWNED_ALLOWED_OK/);
|
|
470
|
+
// Built-in and user never-ports here are "this-machine" rules, so the probes name this Mac: the
|
|
471
|
+
// same port on a public host is correctly allowed. The proxy refuses before dialling anything.
|
|
472
|
+
for (const port of SERVICE_PORTS) assert.equal((await proxyTunnel(publicProxyUrl, "127.0.0.1", port)).code, "403", `built-in ${port}`);
|
|
473
|
+
for (const { port } of neverSinks) assert.equal((await proxyTunnel(publicProxyUrl, "127.0.0.1", port)).code, "403", `user-never ${port}`);
|
|
474
|
+
await proxyTunnel(publicProxyUrl, "127.0.0.1", allowed.port);
|
|
475
|
+
const base = targetDef("gemini");
|
|
476
|
+
const target = { ...base, name: "Scripted ACP qualification fixture", product: "scripted ACP fixture", apiHosts: [ALLOWED_HOST] };
|
|
477
|
+
const config = withDefaults({ allow_temp_home: true, escalation: false, child_auth: "any", targets: {
|
|
478
|
+
codex: { stage2: true }, gemini: { enabled: true, bin: process.execPath, child_auth: "any" },
|
|
479
|
+
} });
|
|
480
|
+
const profile: AcpProfile = { args: () => [ACP_FIXTURE], scrub: () => false,
|
|
481
|
+
prepareState: () => ({ stateDirs: [], keychain: false }),
|
|
482
|
+
login: { how: "scripted fixture has no provider login" }, normalizeApproval: genericApproval };
|
|
483
|
+
const common = { target, config, cwd: files.cwd, access: "write" as const, dir: home, relay: null,
|
|
484
|
+
markers: { runId: "stage2-native", nodeId: "acp-native", depth: 1 }, neverPolicy: floor,
|
|
485
|
+
environment: { HOME: home, USER: os.userInfo().username, PATH: `${path.dirname(process.execPath)}:/usr/bin:/bin:/usr/sbin:/sbin`, SHELL: "/bin/zsh", LANG: "C.UTF-8" },
|
|
486
|
+
signal: t.signal, onApproval: async () => { throw new Error("item 29 has no approval route"); } };
|
|
487
|
+
const turnSpec = (mode: "proxy" | "direct", extra: Record<string, unknown> = {}) => {
|
|
488
|
+
const logPath = path.join(home, `acp-${mode}.log`);
|
|
489
|
+
const prompt = JSON.stringify({ mode, allowedHost: ALLOWED_HOST, allowedPort: allowed.port,
|
|
490
|
+
neverPorts: neverSinks.map(item => item.port), ...files, ...extra });
|
|
491
|
+
const direct = mode === "direct" ? { grant: { network: true } } : {};
|
|
492
|
+
return { spec: { ...common, ...direct, prompt, logPath } as TurnSpec, logPath };
|
|
493
|
+
};
|
|
494
|
+
const run = async (mode: "proxy") => {
|
|
495
|
+
const { spec, logPath } = turnSpec(mode);
|
|
496
|
+
const result = await complete(startAcpTurn(spec, profile));
|
|
497
|
+
return { result, log: fs.readFileSync(logPath, "utf8"), evidence: JSON.parse(result.finalMessage) };
|
|
498
|
+
};
|
|
499
|
+
const proxied = await run("proxy");
|
|
500
|
+
const allowedBeforeDirect = allowed.accepts.length;
|
|
501
|
+
// NETWORK_ON denies all of this Mac's own addresses. To show that does not depend on when a
|
|
502
|
+
// listener started, the ready file is written from inside the prompt after the profile is live;
|
|
503
|
+
// only then is a parent-owned listener opened and its port written back, and it must be refused.
|
|
504
|
+
const readyFile = path.join(files.cwd, "direct-ready");
|
|
505
|
+
const goFile = path.join(files.cwd, "direct-go.json");
|
|
506
|
+
const directTurn = turnSpec("direct", { readyFile, goFile, publicControlHost: "1.1.1.1", publicControlPort: 443 });
|
|
507
|
+
directHandle = await startAcpTurn(directTurn.spec, profile);
|
|
508
|
+
let early: TurnResult | undefined;
|
|
509
|
+
directHandle.done.then(result => { early = result; directDone = true; });
|
|
510
|
+
for (const deadline = Date.now() + 60_000; !fs.existsSync(readyFile) && !early && Date.now() < deadline;) {
|
|
511
|
+
await new Promise(resolve => setTimeout(resolve, 25));
|
|
512
|
+
}
|
|
513
|
+
assert.ok(fs.existsSync(readyFile), `direct ACP turn ended before its post-start control (${JSON.stringify(early)})`);
|
|
514
|
+
lateAllowed = await lateSink(new Set([...SERVICE_PORTS, ...neverSinks.map(item => item.port), ...floor.authorityPorts]));
|
|
515
|
+
fs.writeFileSync(`${goFile}.tmp`, JSON.stringify({ port: lateAllowed.port }));
|
|
516
|
+
fs.renameSync(`${goFile}.tmp`, goFile);
|
|
517
|
+
const directResult = await directHandle.done;
|
|
518
|
+
directDone = true;
|
|
519
|
+
const direct = { result: directResult, log: fs.readFileSync(directTurn.logPath, "utf8"), evidence: JSON.parse(directResult.finalMessage) };
|
|
520
|
+
for (const turn of [proxied, direct]) {
|
|
521
|
+
assert.equal(turn.result.status, "ok", JSON.stringify(turn.result));
|
|
522
|
+
assert.equal(turn.evidence.files.allowedRead, "ALLOWED_READ_NONCE_29\n");
|
|
523
|
+
assert.match(turn.evidence.files.forbiddenRead, /^ERROR:/);
|
|
524
|
+
assert.match(turn.evidence.files.forbiddenWrite, /^ERROR:/);
|
|
525
|
+
assert.equal(turn.evidence.files.allowedWrite, "WROTE");
|
|
526
|
+
assert.match(turn.log, /# relay boundary probes:/, "production ACP launch verified the exact outer profile");
|
|
527
|
+
}
|
|
528
|
+
assert.equal(proxied.evidence.allowed.ok, true, JSON.stringify(proxied.evidence.allowed));
|
|
529
|
+
assert.match(proxied.evidence.allowed.data, /OWNED_ALLOWED_OK/);
|
|
530
|
+
assert.deepEqual(proxied.evidence.services.map((entry: any) => entry.port).sort((a: number, b: number) => a - b),
|
|
531
|
+
neverSinks.map(item => item.port).sort((a, b) => a - b));
|
|
532
|
+
assert.ok(proxied.evidence.services.every((entry: any) => entry.direct?.ok === false && entry.direct?.code === "EPERM"), JSON.stringify(proxied.evidence.services));
|
|
533
|
+
assert.deepEqual(direct.evidence.publicControl, { ok: true }, "the granted profile really opened the network (needs this Mac online)");
|
|
534
|
+
assert.deepEqual(direct.evidence.allowed, { ok: false, code: "EPERM" }, "a pre-start listener is refused in the granted profile");
|
|
535
|
+
assert.equal(allowed.accepts.length, allowedBeforeDirect, "the granted turn made no connection to the pre-start allowed sink");
|
|
536
|
+
assert.equal(direct.evidence.lateLoopback.port, lateAllowed.port);
|
|
537
|
+
assert.equal(direct.evidence.lateLoopback.ok, false, JSON.stringify(direct.evidence.lateLoopback));
|
|
538
|
+
assert.equal(direct.evidence.lateLoopback.code, "EPERM", JSON.stringify(direct.evidence.lateLoopback));
|
|
539
|
+
assert.deepEqual(lateAllowed.accepts, [], "a listener opened after the profile is live is refused too: all of loopback is closed");
|
|
540
|
+
for (const { port } of neverSinks) {
|
|
541
|
+
const evidence = direct.evidence.services.find((entry: any) => entry.port === port);
|
|
542
|
+
assert.ok(evidence && evidence.direct.ok === false && evidence.direct.code === "EPERM", JSON.stringify(direct.evidence.services));
|
|
543
|
+
}
|
|
544
|
+
assert.match(proxied.log, new RegExp(`# relay network: off, except relay's proxy to ${ALLOWED_HOST.replace(/\./g, "\\.")}`));
|
|
545
|
+
assert.match(direct.log, /# relay network: on \(granted\)/);
|
|
546
|
+
assert.equal(fs.readFileSync(files.forbiddenFile, "utf8"), "FORBIDDEN_UNCHANGED_29\n");
|
|
547
|
+
assert.equal(fs.readFileSync(files.allowedWrite, "utf8"), "ACP_ALLOWED_WRITE\n");
|
|
548
|
+
assert.ok(allowed.payloads.some(data => /ACP_PROXY_PING/.test(data)), "permitted owned port accepted the ACP allowlist-proxy positive control");
|
|
549
|
+
assert.ok(allowed.payloads.some(data => /ACP_PARENT_PROXY_PING/.test(data)), "parent-driven production proxy reached the permitted owned sink");
|
|
550
|
+
for (const decoy of neverSinks) assert.deepEqual(decoy.accepts, [], `ACP made no actual dial to owned user-never port ${decoy.port}`);
|
|
551
|
+
const decisions = fs.readFileSync(path.join(home, "proxy.jsonl"), "utf8").trim().split("\n").map(line => JSON.parse(line));
|
|
552
|
+
assert.ok(decisions.some(d => d.event === "allow" && d.target === `${ALLOWED_HOST}:443`), JSON.stringify(decisions));
|
|
553
|
+
const publicDecisions = fs.readFileSync(path.join(home, "acp-public-proxy.jsonl"), "utf8").trim().split("\n").map(line => JSON.parse(line));
|
|
554
|
+
for (const port of SERVICE_PORTS) assert.ok(publicDecisions.some(d => d.event === "deny" && d.target === `127.0.0.1:${port}` && d.reason === "refused"), `ACP public proxy recorded the built-in service denial for ${port}: ${JSON.stringify(publicDecisions)}`);
|
|
555
|
+
for (const { port } of neverSinks) assert.ok(publicDecisions.some(d => d.event === "deny" && d.target === `127.0.0.1:${port}` && d.reason === "refused"), `ACP public proxy recorded the user-never denial for ${port}: ${JSON.stringify(publicDecisions)}`);
|
|
556
|
+
assert.ok(publicDecisions.some(d => d.event === "deny" && d.target === `127.0.0.1:${allowed.port}` && d.reason === "private-address"), "a loopback port outside the floor reads private-address, so \"refused\" above is the never floor");
|
|
557
|
+
t.diagnostic("ACP boundary: scripted ACP-v1 fixture only; no vendor binary or provider credentials were used");
|
|
558
|
+
} finally {
|
|
559
|
+
t.mock.restoreAll();
|
|
560
|
+
if (directHandle && !directDone) await directHandle.cancel().catch(() => {});
|
|
561
|
+
await publicProxy?.close();
|
|
562
|
+
await Promise.all([...neverSinks, ...(allowed ? [allowed] : []), ...(lateAllowed ? [lateAllowed] : [])].map(item => item.close()));
|
|
563
|
+
restore();
|
|
564
|
+
fs.rmSync(home, { recursive: true, force: true });
|
|
565
|
+
}
|
|
566
|
+
});
|
|
567
|
+
|
|
568
|
+
// Release check claude-floor, part keychainDecoy (host only, RELAY_TEST_KEYCHAIN_LIVE=1; not a Codex
|
|
569
|
+
// pin requirement): the keychain from a floored Claude turn with stage 2 off, in two turns. The
|
|
570
|
+
// installed CLI's Bash asks the security daemons for a decoy keychain's item. Floored: the decoy's
|
|
571
|
+
// directory is in the floor's never.paths, as ~/Library/Keychains is, so the item must be refused
|
|
572
|
+
// whatever the keychain IPC, since reading an item in a file-based keychain needs its file as well as
|
|
573
|
+
// the daemons. Control: the same floor without that entry, which must match what doctor documents
|
|
574
|
+
// (core/never-coverage.ts neverKeychainIpc): Claude Code's sandbox profile allows
|
|
575
|
+
// com.apple.SecurityServer and com.apple.securityd.xpc, so "open", and the item is retrieved. The
|
|
576
|
+
// control shows that a floored refusal comes from the file deny and not from a broken command: a
|
|
577
|
+
// causal control. A turn needs the network grant here only so the CLI reaches the owned model stub
|
|
578
|
+
// through relay's proxy, so both turns are network-on: doctor's "open" for Claude's network-off
|
|
579
|
+
// turns is the recorded binary-profile reading, which these turns do not measure.
|
|
580
|
+
// Teardown goes through withTeardown (keychain-decoy.ts), in this order: the decoy is cleaned up
|
|
581
|
+
// while HOME is still the scratch one (its mutating security commands use the inherited HOME), and
|
|
582
|
+
// only then is the environment restored and the scratch home removed; a failing step skips none of
|
|
583
|
+
// the later ones, and the test's own failure is kept beside it.
|
|
584
|
+
for (const floored of [true, false]) test(`keychain decoy live (claude, stage 2 off, network on, ${floored ? "floored" : "causal control without the file deny"}): ${floored ? "a floored Claude Bash command cannot retrieve a decoy keychain item in a never path, whatever the keychain IPC" : "without that never path, retrieving the decoy item from a Claude Bash command matches the documented keychain IPC"}`, { skip: keychainSkip, timeout: 300_000 }, async (t) => {
|
|
585
|
+
const restore = saveProcessEnv();
|
|
586
|
+
delete process.env.RELAY_EXPERIMENTAL_CODEX_STAGE2;
|
|
587
|
+
const home = scratchHome();
|
|
588
|
+
let decoy: DecoyKeychain | undefined;
|
|
589
|
+
let model: ScriptedModel | undefined;
|
|
590
|
+
await withTeardown(async () => {
|
|
591
|
+
const runRoot = fs.realpathSync(fs.mkdtempSync(path.join(home, "run-")));
|
|
592
|
+
decoy = decoyKeychain(path.join(home, "keychains"));
|
|
593
|
+
useScratch(home, runRoot, true);
|
|
594
|
+
fs.mkdirSync(process.env.RELAY_HOME!, { recursive: true });
|
|
595
|
+
const outside = spawnSync(decoy.command[0], decoy.command.slice(1), { encoding: "utf8", timeout: 30000 });
|
|
596
|
+
assert.ok(retrieved(decoy, outside.stdout), `positive control outside any sandbox: ${outside.stderr}`);
|
|
597
|
+
const cwd = path.join(home, "work"); fs.mkdirSync(cwd);
|
|
598
|
+
// The same floor either way (the built-ins, keychain files among them); only the decoy's entry differs.
|
|
599
|
+
const floor = resolveNeverPolicy({ home, never: { paths: floored ? [{ path: decoy.dir }] : [] } });
|
|
600
|
+
// The exit marker shows the command ran; a refusal without it is not evidence (keychain-decoy.ts attempt).
|
|
601
|
+
const bash = wrapped(decoy);
|
|
602
|
+
const steps: ModelStep[] = [{ name: "Bash", input: { command: bash, description: "Try the decoy keychain item" } }];
|
|
603
|
+
model = await scriptedModel(steps);
|
|
604
|
+
const stubFile = path.join(process.env.RELAY_HOME!, "dev", "claude-model-stub.json");
|
|
605
|
+
fs.mkdirSync(path.dirname(stubFile), { recursive: true, mode: 0o700 });
|
|
606
|
+
fs.writeFileSync(stubFile, JSON.stringify({ host: MODEL_HOST, port: model.port }));
|
|
607
|
+
const launcher = claudeStubLauncher(runRoot);
|
|
608
|
+
const logPath = path.join(runRoot, "claude-agent.log");
|
|
609
|
+
assert.ok(fs.existsSync(CLAUDE), `installed Claude missing at ${CLAUDE}`);
|
|
610
|
+
const config = withDefaults({ allow_temp_home: true, escalation: false, child_settings: "isolated", child_hooks: false, child_mcp: "relay-only",
|
|
611
|
+
targets: { claude: { bin: launcher, child_auth: "any" }, codex: { stage2: false } } });
|
|
612
|
+
const spec: TurnSpec = { target: targetDef("claude"), config, cwd, access: "write", grant: { network: true },
|
|
613
|
+
model: "haiku", prompt: "keychain decoy qualification", dir: runRoot, logPath, relay: null, markers: { runId: "keychain-decoy", nodeId: "claude-keychain", depth: 1 },
|
|
614
|
+
neverPolicy: floor, environment: { HOME: home, USER: os.userInfo().username,
|
|
615
|
+
PATH: [...new Set([path.dirname(CLAUDE), path.dirname(process.execPath), "/usr/bin", "/bin", "/usr/sbin", "/sbin"])].join(":"), SHELL: "/bin/zsh", LANG: "C.UTF-8" },
|
|
616
|
+
signal: t.signal, onApproval: async () => { throw new Error("the keychain decoy check has no approval route"); } };
|
|
617
|
+
const result = await withTurnSecrets(() => complete(claudeBackend.start(spec)));
|
|
618
|
+
const log = fs.readFileSync(logPath, "utf8");
|
|
619
|
+
assert.equal(result.status, "ok", JSON.stringify({ result, log: log.slice(-8000) }));
|
|
620
|
+
const settings = JSON.parse(fs.readFileSync(path.join(runRoot, "claude-settings.json"), "utf8"));
|
|
621
|
+
const denyRead: string[] = settings.sandbox.filesystem.denyRead;
|
|
622
|
+
if (floored) assert.ok(denyRead.includes(decoy.dir), "the decoy's directory is in the floor's denyRead, as the keychains are");
|
|
623
|
+
else assert.ok(!denyRead.some(p => decoy!.dir === p || decoy!.dir.startsWith(`${p}/`)), `the control's denyRead does not cover the decoy's directory: ${JSON.stringify(denyRead)}`);
|
|
624
|
+
const events = log.split("\n").filter(line => line.trim().startsWith("{")).map(line => JSON.parse(line));
|
|
625
|
+
// The Bash tool_use the stub issued, and the tool_result Claude Code returned for that id: the
|
|
626
|
+
// command's text (which names both markers) is never mistaken for its output.
|
|
627
|
+
const blocks = (type: string) => events.flatMap(event => Array.isArray(event.message?.content) ? event.message.content : []).filter(block => block?.type === type);
|
|
628
|
+
const use = blocks("tool_use").find(block => block.name === "Bash" && block.input?.command === bash);
|
|
629
|
+
assert.ok(use?.id, "the installed CLI attempted the scripted Bash command");
|
|
630
|
+
const results = blocks("tool_result").filter(block => block.tool_use_id === use.id);
|
|
631
|
+
assert.equal(results.length, 1, `exactly one tool_result for ${use.id}`);
|
|
632
|
+
const output = stringsIn(results[0].content).join("\n");
|
|
633
|
+
// attempt() throws unless the output carries exactly one numeric exit marker, so a tool error
|
|
634
|
+
// (the command never ran, or was cut off) is never read as a refusal.
|
|
635
|
+
const observed = attempt(decoy, output);
|
|
636
|
+
const documented = neverKeychainIpc("claude", config, {})!;
|
|
637
|
+
const expected = floored ? "refused" : documented.networkOn === "open" ? "retrieved" : "refused";
|
|
638
|
+
t.diagnostic(`keychain decoy (claude, stage 2 off, ${floored ? "floored" : "control"}): ${observed}; documented keychain IPC ${documented.networkOn}; expected ${expected}`);
|
|
639
|
+
assert.equal(observed, expected,
|
|
640
|
+
`${floored ? "floored" : "control"}, documented keychain IPC ${documented.networkOn}; observed ${observed}: ${JSON.stringify(output.replaceAll(decoy!.secret, "<secret>").slice(-600))}`);
|
|
641
|
+
}, [
|
|
642
|
+
["model stub", () => model?.close()],
|
|
643
|
+
["decoy cleanup", () => decoy?.cleanup()],
|
|
644
|
+
["environment", () => restore()],
|
|
645
|
+
["scratch home", () => fs.rmSync(home, { recursive: true, force: true })],
|
|
646
|
+
]);
|
|
647
|
+
});
|