@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,218 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Stands in for the `claude` and `codex` host CLIs in the installer tests. It keeps what the real
|
|
3
|
+
// ones keep (the MCP server named relay, plugin marketplaces, installed plugins) in a JSON file,
|
|
4
|
+
// prints what the real ones print (formats copied from Claude Code 2.1.286 and codex-cli 0.160.1),
|
|
5
|
+
// keeps a copy of an installed plugin where each keeps it (Claude Code 2.1.302 and codex-cli 0.162.1:
|
|
6
|
+
// a fresh copy of the plugin's directory on every install, under its home's plugins/cache, which
|
|
7
|
+
// Claude Code names in `plugin list --json` as installPath and Codex does not),
|
|
8
|
+
// records every call it gets, and fails where a test tells it to. It is strict where the real
|
|
9
|
+
// behaviour is not known: adding something that is there, or removing something that is not, fails.
|
|
10
|
+
//
|
|
11
|
+
// FAKE_DIR where its state and the call log live
|
|
12
|
+
// FAKE_FAIL fail any call whose command line contains this text
|
|
13
|
+
// FAKE_TOUCH a file to append a line to during `plugin list`, as something else editing it mid-run would
|
|
14
|
+
// FAKE_CREATE a file to create while a plugin is being added, as something else installing there mid-run would
|
|
15
|
+
// FAKE_CLAUDE_VERSION, FAKE_CODEX_VERSION, FAKE_CODEX_LOGIN, FAKE_LOGGED_OUT
|
|
16
|
+
import fs from "node:fs";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import { parse } from "smol-toml";
|
|
19
|
+
|
|
20
|
+
// The version a host reports for an installed plugin: this checkout's, unless a test sets an older one.
|
|
21
|
+
const VERSION = process.env.FAKE_PLUGIN_VERSION ?? JSON.parse(fs.readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version;
|
|
22
|
+
|
|
23
|
+
const [name, ...args] = process.argv.slice(2);
|
|
24
|
+
const dir = process.env.FAKE_DIR;
|
|
25
|
+
const stateFile = path.join(dir, `${name}.json`);
|
|
26
|
+
const state = fs.existsSync(stateFile) ? JSON.parse(fs.readFileSync(stateFile, "utf8")) : { server: null, markets: [], plugins: [] };
|
|
27
|
+
const save = () => fs.writeFileSync(stateFile, JSON.stringify(state, null, 2));
|
|
28
|
+
const out = (text) => process.stdout.write(`${text}\n`);
|
|
29
|
+
const fail = (text, code = 1) => {
|
|
30
|
+
process.stderr.write(`${text}\n`);
|
|
31
|
+
process.exit(code);
|
|
32
|
+
};
|
|
33
|
+
const line = args.join(" ");
|
|
34
|
+
const is = (...words) => words.every((w, i) => args[i] === w);
|
|
35
|
+
|
|
36
|
+
fs.appendFileSync(path.join(dir, "calls.jsonl"), `${JSON.stringify({ bin: name, args })}\n`);
|
|
37
|
+
if (process.env.FAKE_FAIL && `${name} ${line}`.includes(process.env.FAKE_FAIL)) fail(`${name}: told to fail on "${process.env.FAKE_FAIL}"`);
|
|
38
|
+
// What a Codex turn's own check asks of Codex (relay doctor's codex launch line, codexLaunchCheck):
|
|
39
|
+
// its app server and `mcp list`, served by the turn tests' stub Codex, which reports the effective
|
|
40
|
+
// configuration from the flags it is given. Recorded as calls, like the rest; they change nothing.
|
|
41
|
+
if (name === "codex" && (args[0] === "app-server" || (args.includes("mcp") && args.includes("list")))) {
|
|
42
|
+
const { spawnSync } = await import("node:child_process");
|
|
43
|
+
// A test's stand-in for a configuration layer above relay's flags (codex-effective.json, codex-shell-policy.json)
|
|
44
|
+
// or below them (codex-features.json, the person's own features), and for the login (codex-account.txt), from files in FAKE_DIR: the turn's
|
|
45
|
+
// environment, which this process has, carries no STUB_* name (relay's environment policy drops them).
|
|
46
|
+
const overlay = (file, key) => (fs.existsSync(path.join(dir, file)) ? { [key]: fs.readFileSync(path.join(dir, file), "utf8") } : {});
|
|
47
|
+
const env = { ...process.env, STUB_DIR: process.env.STUB_DIR ?? dir, ...overlay("codex-effective.json", "STUB_CODEX_EFFECTIVE"), ...overlay("codex-shell-policy.json", "STUB_CODEX_SHELL_POLICY"), ...overlay("codex-features.json", "STUB_CODEX_FEATURES"), ...overlay("codex-account.txt", "STUB_ACCOUNT") };
|
|
48
|
+
const r = spawnSync(process.execPath, [new URL("./stub-codex", import.meta.url).pathname, ...args], { stdio: "inherit", env });
|
|
49
|
+
process.exit(r.status ?? 1);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Where the host keeps its copy of an installed plugin: <its home>/plugins/cache/<marketplace>/<plugin>/<version>. */
|
|
53
|
+
const copyOf = (id) => {
|
|
54
|
+
const home = name === "claude" ? path.join(process.env.CLAUDE_CONFIG_DIR ?? path.join(process.env.HOME ?? "", ".claude")) : process.env.CODEX_HOME ?? "";
|
|
55
|
+
const [plugin, market] = id.split("@");
|
|
56
|
+
return path.join(home, "plugins", "cache", market, plugin, VERSION);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** The marketplace manifest of an adapters directory, as each CLI would read it. */
|
|
60
|
+
const manifest = (source) => JSON.parse(fs.readFileSync(path.join(source, name === "claude" ? ".claude-plugin/marketplace.json" : ".agents/plugins/marketplace.json"), "utf8"));
|
|
61
|
+
|
|
62
|
+
// Codex keeps marketplaces and plugins in config.toml itself, so its commands rewrite the file relay edits.
|
|
63
|
+
const codexConfig = path.join(process.env.CODEX_HOME || path.join(process.env.HOME, ".codex"), "config.toml");
|
|
64
|
+
const marketSnippet = (m) => `\n[marketplaces.${m.name}]\nsource = ${JSON.stringify(m.path)}\n`;
|
|
65
|
+
const pluginSnippet = (id) => `\n[plugins.${JSON.stringify(id)}]\nenabled = true\n`;
|
|
66
|
+
// It writes in the file's own line endings, and takes its tables out again wherever they ended up.
|
|
67
|
+
const codexEol = () => (fs.existsSync(codexConfig) && fs.readFileSync(codexConfig, "utf8").includes("\r\n") ? "\r\n" : "\n");
|
|
68
|
+
const codexAppend = (snippet) => name === "codex" && fs.appendFileSync(codexConfig, snippet.replace(/\n/g, codexEol()));
|
|
69
|
+
const codexCut = (snippet) => {
|
|
70
|
+
if (name !== "codex" || !fs.existsSync(codexConfig)) return;
|
|
71
|
+
let text = fs.readFileSync(codexConfig, "utf8");
|
|
72
|
+
for (const form of [snippet, snippet.slice(1)].flatMap((f) => [f.replace(/\n/g, "\r\n"), f])) {
|
|
73
|
+
if (!text.includes(form)) continue;
|
|
74
|
+
text = text.replace(form, "");
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
fs.writeFileSync(codexConfig, text);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
function marketplace() {
|
|
81
|
+
if (is("plugin", "marketplace", "list", "--json")) {
|
|
82
|
+
const rows = name === "claude"
|
|
83
|
+
? state.markets.map((m) => m.foreign ?? { name: m.name, source: "directory", path: m.path, installLocation: m.path })
|
|
84
|
+
: state.markets.map((m) => m.foreign ?? { name: m.name, root: m.path, marketplaceSource: { sourceType: "local", source: m.path } });
|
|
85
|
+
return out(JSON.stringify(name === "claude" ? rows : { marketplaces: rows }, null, 2));
|
|
86
|
+
}
|
|
87
|
+
if (is("plugin", "marketplace", "add")) {
|
|
88
|
+
const source = args[3];
|
|
89
|
+
const market = { name: manifest(source).name, path: source };
|
|
90
|
+
if (state.markets.some((m) => m.name === market.name)) fail(`marketplace ${market.name} is already added`);
|
|
91
|
+
state.markets.push(market);
|
|
92
|
+
codexAppend(marketSnippet(market));
|
|
93
|
+
return save();
|
|
94
|
+
}
|
|
95
|
+
if (is("plugin", "marketplace", "remove")) {
|
|
96
|
+
const market = state.markets.find((m) => m.name === args[3]);
|
|
97
|
+
if (!market) fail(`no marketplace named ${args[3]}`);
|
|
98
|
+
state.markets = state.markets.filter((m) => m !== market);
|
|
99
|
+
codexCut(marketSnippet(market));
|
|
100
|
+
return save();
|
|
101
|
+
}
|
|
102
|
+
fail(`${name}: unexpected arguments: ${line}`, 2);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function plugin() {
|
|
106
|
+
if (args[1] === "marketplace") return marketplace();
|
|
107
|
+
if (is("plugin", "list", "--json")) {
|
|
108
|
+
if (process.env.FAKE_TOUCH) fs.appendFileSync(process.env.FAKE_TOUCH, "# edited meanwhile\n");
|
|
109
|
+
return out(JSON.stringify(name === "claude"
|
|
110
|
+
? state.plugins.map((id) => ({ id, version: VERSION, scope: "user", enabled: true, installPath: copyOf(id) }))
|
|
111
|
+
: { installed: state.plugins.map((id) => ({ pluginId: id, name: id.split("@")[0], marketplaceName: id.split("@")[1], version: VERSION, installed: true, enabled: true })), available: [] }, null, 2));
|
|
112
|
+
}
|
|
113
|
+
const [add, remove] = name === "claude" ? ["install", "uninstall"] : ["add", "remove"];
|
|
114
|
+
const id = args[2];
|
|
115
|
+
if (is("plugin", add)) {
|
|
116
|
+
if (process.env.FAKE_CREATE) {
|
|
117
|
+
fs.mkdirSync(path.dirname(process.env.FAKE_CREATE), { recursive: true });
|
|
118
|
+
fs.writeFileSync(process.env.FAKE_CREATE, "someone else's program\n");
|
|
119
|
+
}
|
|
120
|
+
if (state.plugins.includes(id)) fail(`plugin ${id} is already installed`);
|
|
121
|
+
const market = state.markets.find((m) => m.name === id.split("@")[1]);
|
|
122
|
+
if (!market) fail(`no marketplace for ${id}`);
|
|
123
|
+
// A fresh copy of the plugin's directory, replacing whatever was at that place.
|
|
124
|
+
const from = path.join(market.path, id.split("@")[0]);
|
|
125
|
+
fs.rmSync(copyOf(id), { recursive: true, force: true });
|
|
126
|
+
if (fs.existsSync(from)) fs.cpSync(from, copyOf(id), { recursive: true });
|
|
127
|
+
state.plugins.push(id);
|
|
128
|
+
codexAppend(pluginSnippet(id));
|
|
129
|
+
return save();
|
|
130
|
+
}
|
|
131
|
+
if (is("plugin", remove)) {
|
|
132
|
+
if (!state.plugins.includes(id)) fail(`plugin ${id} is not installed`);
|
|
133
|
+
fs.rmSync(copyOf(id), { recursive: true, force: true });
|
|
134
|
+
state.plugins = state.plugins.filter((p) => p !== id);
|
|
135
|
+
codexCut(pluginSnippet(id));
|
|
136
|
+
return save();
|
|
137
|
+
}
|
|
138
|
+
fail(`${name}: unexpected arguments: ${line}`, 2);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function claude() {
|
|
142
|
+
if (is("--version")) return out(process.env.FAKE_CLAUDE_VERSION ?? "2.1.302 (Claude Code)");
|
|
143
|
+
if (is("-p")) {
|
|
144
|
+
const value = (flag) => args[args.indexOf(flag) + 1];
|
|
145
|
+
const settings = JSON.parse(fs.readFileSync(value("--settings"), "utf8"));
|
|
146
|
+
fs.writeFileSync(path.join(dir, "claude-probe.json"), JSON.stringify({ pid: process.pid, args, settings, cwd: process.cwd() }));
|
|
147
|
+
if (state.initProbe === "exit") return;
|
|
148
|
+
const fixture = JSON.parse(fs.readFileSync(new URL("../fixtures/native-skill-isolation.json", import.meta.url), "utf8"));
|
|
149
|
+
const init = { type: "system", subtype: "init", ...(state.initReport ?? fixture.claude[1].init) };
|
|
150
|
+
// Keep it alive until relay stops it. "result": the local /skills reply already queued behind
|
|
151
|
+
// init in the same chunk, as real Claude Code 2.1.303 does; relay reads nothing after init.
|
|
152
|
+
// "result-first": a result before any init report, which must fail stop verification.
|
|
153
|
+
const result = '{"type":"result","subtype":"success"}';
|
|
154
|
+
out(state.initProbe === "result-first" ? `${result}\n${JSON.stringify(init)}` : JSON.stringify(init) + (state.initProbe === "result" ? `\n${result}` : ""));
|
|
155
|
+
setInterval(() => {}, 1000);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (is("auth", "status", "--json")) {
|
|
159
|
+
const loggedIn = !process.env.FAKE_LOGGED_OUT;
|
|
160
|
+
out(JSON.stringify(loggedIn ? { loggedIn, authMethod: "claude.ai", apiProvider: "firstParty", email: "someone@example.com", orgName: "Private Org", subscriptionType: "max" } : { loggedIn }));
|
|
161
|
+
return process.exit(loggedIn ? 0 : 1);
|
|
162
|
+
}
|
|
163
|
+
if (is("mcp", "get", "relay")) {
|
|
164
|
+
const s = state.server;
|
|
165
|
+
if (!s) {
|
|
166
|
+
out('No MCP server named "relay". Configured servers: email, macos');
|
|
167
|
+
return process.exit(1);
|
|
168
|
+
}
|
|
169
|
+
return out([
|
|
170
|
+
"relay:",
|
|
171
|
+
` Scope: ${s.scope}`,
|
|
172
|
+
" Status: ✔ Connected",
|
|
173
|
+
` Type: ${s.type}`,
|
|
174
|
+
...(s.type === "stdio" ? [` Command: ${s.command}`, ` Args: ${s.args.join(" ")}`, " Environment:", ...s.env.map((e) => ` ${e}`)] : [` URL: ${s.url}`]),
|
|
175
|
+
"",
|
|
176
|
+
"To remove this server, run: claude mcp remove relay -s user",
|
|
177
|
+
].join("\n"));
|
|
178
|
+
}
|
|
179
|
+
if (is("mcp", "add", "-s", "user", "relay")) {
|
|
180
|
+
if (state.server) fail("MCP server relay already exists in user config");
|
|
181
|
+
const dashes = args.indexOf("--");
|
|
182
|
+
const env = args.slice(5, dashes).filter((a) => a !== "-e");
|
|
183
|
+
const [command, ...rest] = args.slice(dashes + 1);
|
|
184
|
+
state.server = { scope: "User config (available in all your projects)", type: "stdio", command, args: rest, env };
|
|
185
|
+
return save();
|
|
186
|
+
}
|
|
187
|
+
if (is("mcp", "remove", "-s", "user", "relay")) {
|
|
188
|
+
if (!state.server) fail('No MCP server found with name: "relay"');
|
|
189
|
+
state.server = null;
|
|
190
|
+
return save();
|
|
191
|
+
}
|
|
192
|
+
if (is("plugin")) return plugin();
|
|
193
|
+
fail(`claude: unexpected arguments: ${line}`, 2);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function codex() {
|
|
197
|
+
if (is("--version")) return out(process.env.FAKE_CODEX_VERSION ?? "codex-cli 0.163.0");
|
|
198
|
+
if (is("login", "status")) {
|
|
199
|
+
if (process.env.FAKE_LOGGED_OUT) fail("Not logged in");
|
|
200
|
+
return out(process.env.FAKE_CODEX_LOGIN ?? "Logged in using ChatGPT");
|
|
201
|
+
}
|
|
202
|
+
if (is("mcp", "get", "relay", "--json")) {
|
|
203
|
+
const server = fs.existsSync(codexConfig) ? parse(fs.readFileSync(codexConfig, "utf8")).mcp_servers?.relay : undefined;
|
|
204
|
+
if (!server) fail("Error: No MCP server named 'relay' found.");
|
|
205
|
+
return out(JSON.stringify({
|
|
206
|
+
name: "relay", enabled: true, disabled_reason: null,
|
|
207
|
+
transport: { type: "stdio", command: server.command, args: server.args ?? [], env: server.env ?? null, env_vars: server.env_vars ?? [], cwd: null },
|
|
208
|
+
enabled_tools: null, disabled_tools: null,
|
|
209
|
+
startup_timeout_sec: server.startup_timeout_sec ?? null, tool_timeout_sec: server.tool_timeout_sec ?? null,
|
|
210
|
+
}, null, 2));
|
|
211
|
+
}
|
|
212
|
+
if (is("plugin")) return plugin();
|
|
213
|
+
fail(`codex: unexpected arguments: ${line}`, 2);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (name === "claude") claude();
|
|
217
|
+
else if (name === "codex") codex();
|
|
218
|
+
else fail(`fake-host-cli: unknown CLI ${name}`, 2);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// What any process can learn of another's command line and environment: the sysctl
|
|
2
|
+
// kern.procargs2 (CTL_KERN, KERN_PROCARGS2, pid), which holds the arguments and the environment the
|
|
3
|
+
// process was started with (ps reads it). Asked through osascript's JavaScript bridge, which every
|
|
4
|
+
// Mac has, as relay's canary asks its native checks (src/confine/canary.ts). Used by the stub Gemini
|
|
5
|
+
// agent, from inside relay's sandbox, on its own process; its positive control is a variable relay
|
|
6
|
+
// sets in that environment (GEMINI_CLI_HOME), which the read must show.
|
|
7
|
+
import { execFileSync } from "node:child_process";
|
|
8
|
+
|
|
9
|
+
const JXA = `ObjC.bindFunction("sysctl", ["int", ["void *", "unsigned int", "void *", "long *", "void *", "long"]]);
|
|
10
|
+
ObjC.bindFunction("malloc", ["void *", ["long"]]);
|
|
11
|
+
ObjC.bindFunction("__error", ["int *", []]);
|
|
12
|
+
function run(argv) {
|
|
13
|
+
var mib = $.NSData.alloc.initWithBase64EncodedStringOptions(argv[0], 0);
|
|
14
|
+
var size = Ref();
|
|
15
|
+
if ($.sysctl(mib.bytes, 3, null, size, null, 0) < 0) return JSON.stringify({ errno: $.__error()[0] });
|
|
16
|
+
var buf = $.malloc(size[0]);
|
|
17
|
+
if ($.sysctl(mib.bytes, 3, buf, size, null, 0) < 0) return JSON.stringify({ errno: $.__error()[0] });
|
|
18
|
+
return JSON.stringify({ data: $.NSData.dataWithBytesLength(buf, size[0]).base64EncodedStringWithOptions(0).js });
|
|
19
|
+
}`;
|
|
20
|
+
|
|
21
|
+
/** `pid`'s start-up arguments and environment as one string, or the errno that refused them. */
|
|
22
|
+
export function procArgs(pid) {
|
|
23
|
+
const mib = Buffer.from(new Int32Array([1, 49, pid]).buffer).toString("base64");
|
|
24
|
+
try {
|
|
25
|
+
const out = JSON.parse(execFileSync("/usr/bin/osascript", ["-l", "JavaScript", "-e", JXA, mib], { encoding: "utf8", timeout: 20000 }));
|
|
26
|
+
return out.errno !== undefined ? { errno: out.errno } : { text: Buffer.from(out.data, "base64").toString("latin1") };
|
|
27
|
+
} catch (err) {
|
|
28
|
+
return { error: String(err?.stderr ?? err?.message ?? err).slice(0, 300) };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
// Stub agent used by the tests in place of the real CLIs. It parses the real flag sets, records
|
|
2
|
+
// what it was given, and plays back steps from a scenario file, so no model tokens are spent. A
|
|
3
|
+
// step can call a relay tool through the MCP config the stub was launched with, which is how
|
|
4
|
+
// nested behaviour is exercised across real process boundaries.
|
|
5
|
+
//
|
|
6
|
+
// This file is what every target's stub shares: the scenario, the session files, the record and
|
|
7
|
+
// the course of a turn. What differs by target (its flags, its event dialect, how it asks for
|
|
8
|
+
// approval, its version) is in targets/<id>.mjs, and each stub-<id> launcher joins the two.
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import crypto from "node:crypto";
|
|
12
|
+
|
|
13
|
+
const DIR = process.env.STUB_DIR;
|
|
14
|
+
/** Output held back by a stub that is about to vanish (see main's leave), or null when output goes straight out. */
|
|
15
|
+
let heldBack = null;
|
|
16
|
+
// Written synchronously, so that what an agent said is out before anything it does next.
|
|
17
|
+
export const emit = (obj) => {
|
|
18
|
+
const line = JSON.stringify(obj) + "\n";
|
|
19
|
+
if (heldBack !== null) heldBack += line;
|
|
20
|
+
else fs.writeSync(1, line);
|
|
21
|
+
};
|
|
22
|
+
/** What of the environment reached this agent, for tests of what relay passes down. */
|
|
23
|
+
export const SEEN_ENV = {
|
|
24
|
+
tmpdir: process.env.TMPDIR ?? null, claudeTmpdir: process.env.CLAUDE_CODE_TMPDIR ?? null,
|
|
25
|
+
anthropicKey: Boolean(process.env.ANTHROPIC_API_KEY), openaiKey: Boolean(process.env.OPENAI_API_KEY),
|
|
26
|
+
worktrees: process.env.RELAY_WORKTREES ?? null, lang: process.env.LC_ALL ?? null, home: process.env.HOME ?? null,
|
|
27
|
+
inbox: Boolean(process.env.CLAUDE_CODE_MESSAGING_SOCKET || process.env.CLAUDE_CODE_MESSAGING_TOKEN),
|
|
28
|
+
anthropicBaseUrl: Boolean(process.env.ANTHROPIC_BASE_URL),
|
|
29
|
+
hostMarkers: Boolean(process.env.CLAUDE_CODE_DESKTOP_APP_VERSION || process.env.CLAUDE_AGENT_SDK_VERSION),
|
|
30
|
+
baseUrls: Object.keys(process.env).filter((k) => /BASE_URL$/.test(k)).sort(),
|
|
31
|
+
hostVars: ["CLAUDE_CODE_DESKTOP_APP_VERSION", "CLAUDE_AGENT_SDK_VERSION"].filter((k) => k in process.env),
|
|
32
|
+
autoBackground: process.env.CLAUDE_AUTO_BACKGROUND_TASKS ?? null,
|
|
33
|
+
// The proxy variables, with any credentials in them replaced, and Claude's sandbox debug switch.
|
|
34
|
+
proxies: Object.fromEntries(Object.keys(process.env).filter((k) => /^(https?|all|no)_proxy$/i.test(k)).sort().map((k) => [k, process.env[k].replace(/\/\/[^@/]*@/, "//***@")])),
|
|
35
|
+
srtDebug: process.env.SRT_DEBUG ?? null,
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Set by an `ignoreSignals` step: from then on the agent does not stop when asked, whether by a
|
|
39
|
+
* signal (it records each one it ignores) or in-band (the Codex app server's turn/interrupt), so
|
|
40
|
+
* that only a kill ends it.
|
|
41
|
+
*/
|
|
42
|
+
export const stubborn = { on: false };
|
|
43
|
+
/** Polls `fn` until it is true or `ms` pass; false if they did. How a step waits on something instead of sleeping. */
|
|
44
|
+
const waitFor = async (fn, ms) => {
|
|
45
|
+
for (const end = Date.now() + ms; Date.now() < end; await new Promise((r) => setTimeout(r, 50))) if (await fn()) return true;
|
|
46
|
+
return false;
|
|
47
|
+
};
|
|
48
|
+
/** A mark is a file in the stub's directory: set by a step (`mark`) or by the test (Harness.mark), and waited on. */
|
|
49
|
+
const markFile = (name) => path.join(DIR, `mark-${name}`);
|
|
50
|
+
const get = (o, key) => key.split(".").reduce((v, k) => (v == null ? v : v[k]), o);
|
|
51
|
+
|
|
52
|
+
/** Writes all of `buf` to `fd`, waiting while the reader is behind (the descriptor may be non-blocking). */
|
|
53
|
+
async function writeAll(fd, buf) {
|
|
54
|
+
for (let off = 0; off < buf.length;) {
|
|
55
|
+
try {
|
|
56
|
+
off += fs.writeSync(fd, buf, off);
|
|
57
|
+
} catch (err) {
|
|
58
|
+
if (err.code !== "EAGAIN") return; // The reader is gone: nothing is waiting for the rest.
|
|
59
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const record = (obj) => fs.appendFileSync(path.join(DIR, "log.jsonl"), JSON.stringify({ t: Date.now(), pid: process.pid, ...obj }) + "\n");
|
|
65
|
+
|
|
66
|
+
export function loadSession(target, parsed) {
|
|
67
|
+
const sessDir = path.join(DIR, "sessions");
|
|
68
|
+
fs.mkdirSync(sessDir, { recursive: true });
|
|
69
|
+
const sessionId = parsed.sessionId ?? crypto.randomUUID();
|
|
70
|
+
const sessFile = path.join(sessDir, `${target}-${sessionId}.json`);
|
|
71
|
+
if (parsed.resumed && !fs.existsSync(sessFile)) return { sessionId, missing: true };
|
|
72
|
+
const sess = fs.existsSync(sessFile) ? JSON.parse(fs.readFileSync(sessFile, "utf8")) : { turns: 0 };
|
|
73
|
+
sess.turns += 1;
|
|
74
|
+
fs.writeFileSync(sessFile, JSON.stringify(sess));
|
|
75
|
+
return { sessionId, sess };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Waits for the test to create a file of that name in the stub's directory. Not for good: a test
|
|
80
|
+
* that was interrupted, or failed, before letting the stub go may never do so, and nothing else
|
|
81
|
+
* would stop it. So the stub gives up, failing, once its directory is gone (the harness removes it
|
|
82
|
+
* when it closes) or after two minutes, as long as a test's tool call may take.
|
|
83
|
+
*/
|
|
84
|
+
async function held(name) {
|
|
85
|
+
const deadline = Date.now() + Number(process.env.STUB_HOLD_MS ?? 120000);
|
|
86
|
+
while (!fs.existsSync(path.join(DIR, name))) {
|
|
87
|
+
if (!fs.existsSync(DIR) || Date.now() > deadline) {
|
|
88
|
+
console.error(`stub agent: never let go at "${name}"; giving up`);
|
|
89
|
+
process.exit(1);
|
|
90
|
+
}
|
|
91
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Says a checkpoint of that name on the agent's output, as an event relay logs and otherwise
|
|
97
|
+
* ignores, and is held there until the test lets it go (helpers.ts: passed).
|
|
98
|
+
*/
|
|
99
|
+
async function checkpoint(name) {
|
|
100
|
+
emit({ type: "stub_checkpoint", method: "stub/checkpoint", params: { checkpoint: name } });
|
|
101
|
+
await held(name);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Plays the scenario steps for one turn. `ask` sends an approval request the way this host would. */
|
|
105
|
+
export async function runSteps(emulator, parsed, prompt, sess, ask) {
|
|
106
|
+
const target = emulator.id;
|
|
107
|
+
const scenario = process.env.STUB_SCENARIO && fs.existsSync(process.env.STUB_SCENARIO) ? JSON.parse(fs.readFileSync(process.env.STUB_SCENARIO, "utf8")) : { rules: [] };
|
|
108
|
+
const rule = scenario.rules.find((r) =>
|
|
109
|
+
(!r.target || r.target === target) && (!r.turn || r.turn === sess.turns) && (!r.match || new RegExp(r.match, "s").test(prompt)));
|
|
110
|
+
const steps = rule?.steps ?? [{ reply: `stub ${target} reply (turn ${sess.turns})` }];
|
|
111
|
+
|
|
112
|
+
let client = null;
|
|
113
|
+
const connect = async () => {
|
|
114
|
+
if (client) return client;
|
|
115
|
+
// Loaded only by a turn that calls relay: the SDK is most of a stub's start-up time (scripts/bench).
|
|
116
|
+
const [{ Client }, { StdioClientTransport }] = await Promise.all([import("@modelcontextprotocol/sdk/client/index.js"), import("@modelcontextprotocol/sdk/client/stdio.js")]);
|
|
117
|
+
const base = emulator.mcpEnv ? emulator.mcpEnv(process.env, parsed.relay.env_vars) : process.env;
|
|
118
|
+
const transport = new StdioClientTransport({ command: parsed.relay.command, args: parsed.relay.args, env: { ...base, ...parsed.relay.env }, cwd: process.cwd() });
|
|
119
|
+
client = new Client({ name: emulator.mcpClientName ?? `stub-${target}`, version: "0" });
|
|
120
|
+
await client.connect(transport);
|
|
121
|
+
return client;
|
|
122
|
+
};
|
|
123
|
+
// A harness that takes relay's tools another way than through relay's MCP server (Gemini's tool
|
|
124
|
+
// discovery) calls and lists them its own way (the emulator's relayTools); otherwise, over MCP.
|
|
125
|
+
const viaHarness = emulator.relayTools;
|
|
126
|
+
const callTool = viaHarness ? (name, args) => viaHarness.call(name, args) : async (name, args) => {
|
|
127
|
+
const res = await (await connect()).callTool({ name, arguments: args }, undefined, { timeout: 120000 });
|
|
128
|
+
if (res.isError) return { error: res.content?.[0]?.text ?? "tool error" };
|
|
129
|
+
try { return JSON.parse(res.content[0].text); } catch { return { raw: res.content }; }
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const vars = { prompt };
|
|
133
|
+
const calls = [];
|
|
134
|
+
let reply = "";
|
|
135
|
+
let failure = null;
|
|
136
|
+
let vanish = false;
|
|
137
|
+
let after;
|
|
138
|
+
const fill = (s) => s.replace(/\{\{([\w.]+)\}\}/g, (_, key) => {
|
|
139
|
+
const v = get(vars, key);
|
|
140
|
+
return typeof v === "string" ? v : JSON.stringify(v);
|
|
141
|
+
});
|
|
142
|
+
for (const step of steps) {
|
|
143
|
+
// Print exactly the credentials this turn received, without a name or other identifying shape.
|
|
144
|
+
if (step.bareTurnSecrets) {
|
|
145
|
+
const values = [new URL(process.env.HTTPS_PROXY).password, parsed.relay.env.RELAY_BRIDGE_SECRET];
|
|
146
|
+
fs.writeFileSync("turn-secrets.json", JSON.stringify(values));
|
|
147
|
+
reply = values.join("\n");
|
|
148
|
+
if (step.bareTurnSecrets === "proxy") reply = values[0];
|
|
149
|
+
if (step.bareTurnSecrets === "bridge") reply = values[1];
|
|
150
|
+
fs.writeSync(2, reply + "\n");
|
|
151
|
+
if (step.fail) { failure = reply; vanish = true; }
|
|
152
|
+
}
|
|
153
|
+
if (step.bareEnvironmentSecret) reply = process.env[step.bareEnvironmentSecret];
|
|
154
|
+
if (step.orphan) {
|
|
155
|
+
// A detached child that shares this agent's output and outlives it, as a runaway tool process would.
|
|
156
|
+
// Its arguments name it and this stub's directory, so that the harness can tell it is still that child
|
|
157
|
+
// (killOrphans in helpers.ts); every one is listed in `orphans`, the latest in `orphan.pid`.
|
|
158
|
+
const { spawn } = await import("node:child_process");
|
|
159
|
+
const c = spawn(process.execPath, ["-e", "setInterval(()=>{},1000)", "relay-stub-orphan", DIR], { detached: true, stdio: ["ignore", "inherit", "inherit"] });
|
|
160
|
+
c.unref();
|
|
161
|
+
fs.appendFileSync(path.join(DIR, "orphans"), `${c.pid}\n`);
|
|
162
|
+
fs.writeFileSync(path.join(DIR, "orphan.pid"), String(c.pid));
|
|
163
|
+
}
|
|
164
|
+
// A shell command run in the agent's directory, as an agent's shell tool would run it.
|
|
165
|
+
if (step.run) {
|
|
166
|
+
const { execSync } = await import("node:child_process");
|
|
167
|
+
try {
|
|
168
|
+
execSync(step.run, { cwd: process.cwd(), stdio: "ignore", shell: "/bin/sh" });
|
|
169
|
+
} catch {
|
|
170
|
+
// The scenario decides what a failure means.
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// A raw event on the agent's output, such as an error it goes on to retry.
|
|
174
|
+
if (step.emit) emit(step.emit);
|
|
175
|
+
// Text on the agent's stderr, as a CLI's diagnostics would be.
|
|
176
|
+
if (step.stderr) fs.writeSync(2, step.stderr);
|
|
177
|
+
if (step.ignoreSignals) {
|
|
178
|
+
stubborn.on = true;
|
|
179
|
+
for (const sig of ["SIGINT", "SIGTERM"]) {
|
|
180
|
+
process.removeAllListeners(sig);
|
|
181
|
+
process.on(sig, () => record({ event: "signal", target: emulator.id, signal: sig, ignored: true }));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// Waits for the test (or another agent) to set a mark: explicit synchronisation, where a sleep would be a guess.
|
|
185
|
+
if (step.awaitMark && !(await waitFor(() => fs.existsSync(markFile(step.awaitMark)), step.within ?? 120000))) {
|
|
186
|
+
failure = `the stub waited for mark ${step.awaitMark} in vain`;
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
if (step.sleep) await new Promise((r) => setTimeout(r, step.sleep));
|
|
190
|
+
// Something only this target's emulator knows how to do (an ACP agent switching its model, say).
|
|
191
|
+
if (step.hook) await emulator.hook?.(step.hook);
|
|
192
|
+
// Tells the test this agent got this far: a file of that name in the stub's directory.
|
|
193
|
+
if (step.signal) fs.writeFileSync(path.join(DIR, step.signal), String(process.pid));
|
|
194
|
+
// Held here until the test lets it go, by creating a file of that name in the stub's directory.
|
|
195
|
+
if (step.await) await held(step.await);
|
|
196
|
+
if (step.checkpoint) await checkpoint(step.checkpoint);
|
|
197
|
+
if (step.crash !== undefined) process.exit(step.crash);
|
|
198
|
+
if (step.write) fs.writeFileSync(path.resolve(process.cwd(), step.write), step.content ?? "stub\n");
|
|
199
|
+
if (step.call || step.tools) {
|
|
200
|
+
let result;
|
|
201
|
+
if (!parsed.relay && !viaHarness) result = { error: "no relay server was provided to this agent" };
|
|
202
|
+
else if (step.tools) result = { tools: viaHarness ? viaHarness.list() : (await (await connect()).listTools()).tools.map((t) => t.name).sort() };
|
|
203
|
+
else {
|
|
204
|
+
result = await callTool(step.call, JSON.parse(fill(JSON.stringify(step.args ?? {}))));
|
|
205
|
+
// `until`: the call is repeated until the result has something at that path (or `within` ms pass, and the last result stands).
|
|
206
|
+
if (step.until && get(result, step.until) == null) {
|
|
207
|
+
await waitFor(async () => get((result = await callTool(step.call, JSON.parse(fill(JSON.stringify(step.args ?? {}))))), step.until) != null, step.within ?? 120000);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
calls.push({ tool: step.call ?? "(list tools)", args: step.args, result });
|
|
211
|
+
if (step.as) vars[step.as] = result;
|
|
212
|
+
vars.last = result;
|
|
213
|
+
}
|
|
214
|
+
// What relay's server tells the agent about itself (its MCP instructions), as the harness shows it.
|
|
215
|
+
if (step.instructions) {
|
|
216
|
+
const result = parsed.relay ? { instructions: (await connect()).getInstructions() ?? null } : { error: "no relay server was provided to this agent" };
|
|
217
|
+
calls.push({ tool: "(instructions)", result });
|
|
218
|
+
if (step.as) vars[step.as] = result;
|
|
219
|
+
vars.last = result;
|
|
220
|
+
}
|
|
221
|
+
if (step.requests) {
|
|
222
|
+
// Several approval requests issued at the same moment, as parallel tool calls would be.
|
|
223
|
+
const results = await Promise.all(step.requests.map((r) => ask(r, callTool)));
|
|
224
|
+
calls.push({ tool: "(parallel approval requests)", args: step.requests, result: results });
|
|
225
|
+
vars.last = results;
|
|
226
|
+
}
|
|
227
|
+
if (step.request) {
|
|
228
|
+
// The agent hit its sandbox and asks for approval, through whatever channel its host has.
|
|
229
|
+
const result = await ask(step.request, callTool);
|
|
230
|
+
calls.push({ tool: "(approval request)", args: step.request, result });
|
|
231
|
+
vars.last = result;
|
|
232
|
+
}
|
|
233
|
+
// Set once everything else in the step is done, so a test waiting on it knows the step has happened.
|
|
234
|
+
if (step.mark) fs.writeFileSync(markFile(step.mark), String(Date.now()));
|
|
235
|
+
if (step.reply !== undefined) reply = fill(step.reply);
|
|
236
|
+
if (step.vanish) vanish = true;
|
|
237
|
+
// A checkpoint said after its last word, for a test that must know relay read that word while the agent was still there.
|
|
238
|
+
if (step.after) after = step.after;
|
|
239
|
+
if (step.fail) failure = step.bareTurnSecrets ? reply : step.fail;
|
|
240
|
+
}
|
|
241
|
+
if (client) await client.close().catch(() => {});
|
|
242
|
+
return { calls, reply, failure, vanish, after };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Runs the stub as one target's CLI. The emulator, from targets/<id>.mjs, has: id; version (what
|
|
247
|
+
* `--version` prints); parse(argv); start(argv, parsed, sessionId), the events the CLI starts a
|
|
248
|
+
* turn with; ask(parsed), how it sends an approval request; fail(message, sessionId) and
|
|
249
|
+
* finish(reply, sessionId), the events it ends one with. Optionally: intercept(argv), for a
|
|
250
|
+
* command that is not a turn (true when handled); serve(argv), for a server mode (a promise, or
|
|
251
|
+
* null when argv is not one); mcpEnv(env), the environment it gives MCP servers.
|
|
252
|
+
*/
|
|
253
|
+
export async function main(emulator) {
|
|
254
|
+
const target = emulator.id;
|
|
255
|
+
const argv = process.argv.slice(2);
|
|
256
|
+
if (emulator.intercept?.(argv)) return;
|
|
257
|
+
// The versions relay was verified with, unless a test asks for another.
|
|
258
|
+
if (argv[0] === "--version") return void console.log(process.env.STUB_VERSION ?? emulator.version);
|
|
259
|
+
process.on("SIGINT", () => process.exit(130));
|
|
260
|
+
process.on("SIGTERM", () => process.exit(143));
|
|
261
|
+
const served = emulator.serve?.(argv);
|
|
262
|
+
if (served) return served;
|
|
263
|
+
|
|
264
|
+
const parsed = emulator.parse(argv);
|
|
265
|
+
let prompt = "";
|
|
266
|
+
for await (const chunk of process.stdin) prompt += chunk;
|
|
267
|
+
// What the CLI does once it has its prompt, before it reports anything (Claude's sandbox reporting its proxy ports).
|
|
268
|
+
await emulator.afterPrompt?.(parsed);
|
|
269
|
+
|
|
270
|
+
const { sessionId, sess, missing } = loadSession(target, parsed);
|
|
271
|
+
if (missing) {
|
|
272
|
+
record({ event: "error", target, argv, error: "unknown session" });
|
|
273
|
+
console.error(`No conversation found with session ID: ${sessionId}`);
|
|
274
|
+
process.exit(1);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
record({ event: "start", env: SEEN_ENV, target, argv, prompt, sessionId, resumed: parsed.resumed, turn: sess.turns, access: parsed.access, cwd: process.cwd(), hasRelay: Boolean(parsed.relay), strict: parsed.strict ?? null, promptTool: parsed.promptTool ?? null, settings: parsed.settings ?? null, ...(parsed.extra ?? {}) });
|
|
278
|
+
emulator.start(argv, parsed, sessionId);
|
|
279
|
+
const { calls, reply, failure, vanish, after } = await runSteps(emulator, parsed, prompt, sess, emulator.ask(parsed));
|
|
280
|
+
|
|
281
|
+
record({ event: "end", target, sessionId, calls, reply, failure });
|
|
282
|
+
// The real CLIs take a while to shut down after their last event (measured 2026-10-13: claude -p
|
|
283
|
+
// about 475 ms, codex exec about 1.5 s). The stub lingers too, unless a step says to vanish at once.
|
|
284
|
+
const leave = async (code) => {
|
|
285
|
+
// To vanish is to be gone before anyone can look: killed outright, with no shutdown at all.
|
|
286
|
+
// Relay looks when it reads the last word, so the stub must already be gone by then, however
|
|
287
|
+
// loaded the machine. Its last events are therefore written by a helper that shares its output
|
|
288
|
+
// and waits on a pipe only the stub holds: the pipe closes when the kernel ends the stub, and
|
|
289
|
+
// only then does the helper pass the events on. Relay gives the output only a second after the
|
|
290
|
+
// stub exits, so the helper is the one that kills it: by then the helper is running, and what is
|
|
291
|
+
// left for it to do uses shell builtins only, with nothing more to start, however slowly
|
|
292
|
+
// programs start on a loaded machine.
|
|
293
|
+
if (vanish) {
|
|
294
|
+
const { spawn } = await import("node:child_process");
|
|
295
|
+
const helper = spawn("/bin/sh", ["-c", 'kill -9 "$STUB_PID"; while IFS= read -r _; do :; done; printf "%s" "$STUB_LAST_WORDS"'], {
|
|
296
|
+
stdio: ["pipe", "inherit", "inherit"], env: { ...process.env, STUB_PID: String(process.pid), STUB_LAST_WORDS: heldBack ?? "" },
|
|
297
|
+
});
|
|
298
|
+
// A helper that cannot start, or ends without having killed the stub, leaves it to vanish alone, without its last words.
|
|
299
|
+
const alone = () => process.kill(process.pid, "SIGKILL");
|
|
300
|
+
helper.on("error", alone);
|
|
301
|
+
helper.on("exit", alone);
|
|
302
|
+
return new Promise(() => {});
|
|
303
|
+
}
|
|
304
|
+
if (after) await checkpoint(after);
|
|
305
|
+
// A real CLI is still there when relay reads its last word, which is when relay takes its last
|
|
306
|
+
// look at what the agent started. A timed linger only makes that likely: on a loaded machine
|
|
307
|
+
// relay can take longer than any linger to read the line. So, first, more output than a pipe
|
|
308
|
+
// holds, which relay logs and ignores: the write returns only once relay has read past the last
|
|
309
|
+
// word, having taken its look while the stub was still here.
|
|
310
|
+
await writeAll(1, Buffer.from(`${" ".repeat(1023)}\n`.repeat(128)));
|
|
311
|
+
// STUB_LINGER_MS: the benchmark (scripts/bench) sets 0, so that a turn's time is relay's own.
|
|
312
|
+
await new Promise((r) => setTimeout(r, Number(process.env.STUB_LINGER_MS ?? 200)));
|
|
313
|
+
process.exit(code);
|
|
314
|
+
};
|
|
315
|
+
if (vanish) heldBack = "";
|
|
316
|
+
if (failure) {
|
|
317
|
+
emulator.fail(failure, sessionId);
|
|
318
|
+
return leave(1);
|
|
319
|
+
}
|
|
320
|
+
emulator.finish(reply, sessionId);
|
|
321
|
+
return leave(0);
|
|
322
|
+
}
|