@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,749 @@
|
|
|
1
|
+
// Admission: who is calling relay, and what it may do. A relay MCP instance's view of itself
|
|
2
|
+
// (Instance), the family it calls from, the run it owns, and the checks every tool call that
|
|
3
|
+
// starts or continues work crosses, in order: which agent it reaches, what the caller's turn
|
|
4
|
+
// allows, its authority over an item, what the work may be given, and whether the target can honour
|
|
5
|
+
// it (admitNew, admitExisting, admitWork). Each tool keeps its own checks between those, in place.
|
|
6
|
+
import crypto from "node:crypto";
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { ensurePrivateRoot, nativeTempAlias, nativeTempRoot, privateStateRoot } from "../confine/private-root.js";
|
|
10
|
+
import { configProtection, overlapsPart, pathOverlaps, relayInstallation, relayPathDirs, relayCodexProtected } from "./protected-config.js";
|
|
11
|
+
import { consultHome, expandHome, foldedPath, harnessHome, insideAnyCase, loadConfig, networkOn, overlapsEntryAnyCase, relayHome, underTemp, worktreesHome } from "./config.js";
|
|
12
|
+
import { createRun, descendants, getNode, getRun, nodeTurns, queuedTurnIds, runConfig, setParam, turnEnded, workCwd } from "./db.js";
|
|
13
|
+
import { webGranted } from "./approvals.js";
|
|
14
|
+
import { queuedSpeeds, sessionParams, speedOf } from "./params.js";
|
|
15
|
+
import { CALLER_LABEL, incapable, resolveTarget, SPEEDS, speedRank, targetDef, targetLabel, vendorFor } from "../targets/index.js";
|
|
16
|
+
import { confinement, dirIdentity } from "../confine/index.js";
|
|
17
|
+
import { codexWriteRootProblem } from "../targets/codex.js";
|
|
18
|
+
import { keyAuthority } from "./api-keys.js";
|
|
19
|
+
import { extensionKeys, holdsServers, PERSON_ROUTES, resolveExtensions, takesNone, touchesConfig } from "./extensions.js";
|
|
20
|
+
import { boundaryProbe, NO_PROBE } from "../confine/turn-probe.js";
|
|
21
|
+
import { environmentAdmission, personConsentAdmission } from "./env-broker.js";
|
|
22
|
+
import { localModeRetired, rawNetworkGrantProblem, stage2Enabled } from "./raw-network.js";
|
|
23
|
+
import { ENVIRONMENT_NAME } from "./env-policy.js";
|
|
24
|
+
import { checkpointNever } from "./never-checkpoint.js";
|
|
25
|
+
import { neverGrantProblem } from "./never-policy.js";
|
|
26
|
+
import { refreshEscalationPersonPath } from "./escalation-contract.js";
|
|
27
|
+
import { nativeCodexNeverNetworkProblem } from "../backends/never-admission.js";
|
|
28
|
+
/** How a call is refused: the reason, worded so the agent simply carries on by itself. */
|
|
29
|
+
export const refused = (reason) => ({ refused: true, reason: `Refused: ${reason}` });
|
|
30
|
+
export const SINGLETON_REASON = "This dedicated connection has carried multiple conversations. Item access and event delivery are disabled; restart with a dedicated registration.";
|
|
31
|
+
export function newInstance(ctx, cwd, family = null) {
|
|
32
|
+
return {
|
|
33
|
+
ctx, cwd, family, runId: ctx.kind === "child" ? ctx.runId : null, token: crypto.randomBytes(12).toString("hex"),
|
|
34
|
+
handedOff: new Set(), host: { recognised: false }, asking: new Map(), noted: new Set(), since: new Map(), waiting: new Map(),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** Which family of agent is calling: a launched agent's own, or the host's for a root session. */
|
|
38
|
+
export const familyOf = (inst) => inst.ctx.kind === "child" ? (getNode(inst.ctx.nodeId)?.target ?? null) : inst.ctx.kind === "root" ? inst.family : null;
|
|
39
|
+
/**
|
|
40
|
+
* Which agent a call reaches (see resolveTarget), under the explicit-target steering policy. Judged under the config that governs the
|
|
41
|
+
* call: the user's for a root session, the run's for a launched agent. `existing` is set when the
|
|
42
|
+
* call continues a session that already exists.
|
|
43
|
+
*/
|
|
44
|
+
function reach(inst, requested, opts = {}) {
|
|
45
|
+
if (inst.ctx.kind === "poisoned") {
|
|
46
|
+
return { reason: `relay context for this agent could not be loaded (${inst.ctx.reason}). Continue without delegating, consulting, or starting discussions.` };
|
|
47
|
+
}
|
|
48
|
+
if (inst.ctx.kind === "child" && !getRun(inst.ctx.runId))
|
|
49
|
+
return { reason: "this agent is no longer in the relay registry. Continue on your own." };
|
|
50
|
+
const cfg = inst.ctx.kind === "root" ? loadConfig() : runConfig(inst.ctx.runId);
|
|
51
|
+
return resolveTarget(familyOf(inst), requested, cfg, { ...opts, callerVendor: callerVendor(inst) });
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The calling agent's family. A launched agent on a harness of several families' models is of
|
|
55
|
+
* the family of its latest reported model only if it pinned a listed model. Auto, absent and
|
|
56
|
+
* unlisted pins stay unknown even after reports: the harness may route without reporting it.
|
|
57
|
+
*/
|
|
58
|
+
export function callerVendor(inst) {
|
|
59
|
+
const own = familyOf(inst);
|
|
60
|
+
if (!own)
|
|
61
|
+
return null;
|
|
62
|
+
const def = targetDef(own);
|
|
63
|
+
if (def.vendor)
|
|
64
|
+
return def.vendor;
|
|
65
|
+
if (inst.ctx.kind !== "child")
|
|
66
|
+
return null;
|
|
67
|
+
const n = getNode(inst.ctx.nodeId);
|
|
68
|
+
if (!n)
|
|
69
|
+
return null;
|
|
70
|
+
if (!n.model || n.model === def.autoModel || !vendorFor(def, n.model))
|
|
71
|
+
return null;
|
|
72
|
+
const p = sessionParams(n);
|
|
73
|
+
return vendorFor(def, Object.hasOwn(p, "served_model") ? p.served_model : n.model);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Admission by capability: why the target cannot honour what the call needs, or null. A target
|
|
77
|
+
* confined by relay itself is refused where relay's confinement cannot enforce, and an
|
|
78
|
+
* unqualified one unless targets.<id>.experimental is set; nothing is ever launched on less than it promises.
|
|
79
|
+
*/
|
|
80
|
+
function capable(target, cfg, needs) {
|
|
81
|
+
const def = targetDef(target);
|
|
82
|
+
const no = incapable(def, needs);
|
|
83
|
+
if (no)
|
|
84
|
+
return no;
|
|
85
|
+
if (def.capabilities.confinement === "relay-enforced") {
|
|
86
|
+
const c = confinement();
|
|
87
|
+
if ("reason" in c)
|
|
88
|
+
return `${targetLabel(def)} runs only inside relay's own sandbox, and ${c.reason}, so relay does not launch it. Use another agent, or do this part yourself.`;
|
|
89
|
+
if (!boundaryProbe())
|
|
90
|
+
return `${NO_PROBE}, so ${targetLabel(def)} cannot be used yet. Use another agent, or do this part yourself.`;
|
|
91
|
+
}
|
|
92
|
+
if (!def.qualified && !cfg.targets[target]?.experimental) {
|
|
93
|
+
return `${targetLabel(def)} is experimental: relay has not yet qualified it with a signed-in run (its login, model selection and protocol behaviour are unverified). Set targets.${target}.experimental to true in relay's config to use it anyway.`;
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Under strict policy, why relay refuses a caller whose family it cannot tell, or null. Continuing a session that exists
|
|
99
|
+
* (resuming a job, following up a thread, carrying on a discussion or review) needs only that,
|
|
100
|
+
* besides authority over the item: it may be of the caller's own family (a root session
|
|
101
|
+
* supervises everything in its run, whoever started it). Only starting an agent keeps to families.
|
|
102
|
+
*/
|
|
103
|
+
export function knownCaller(inst) {
|
|
104
|
+
if (inst.ctx.kind === "poisoned")
|
|
105
|
+
return `relay context for this agent could not be loaded (${inst.ctx.reason}). Continue without delegating, consulting, or starting discussions.`;
|
|
106
|
+
if (callerVendor(inst))
|
|
107
|
+
return null;
|
|
108
|
+
const cfg = inst.ctx.kind === "child" ? runConfig(inst.ctx.runId) : loadConfig();
|
|
109
|
+
if (cfg.same_family !== "refuse")
|
|
110
|
+
return null;
|
|
111
|
+
const r = resolveTarget(null, undefined, { same_family: "refuse" });
|
|
112
|
+
return "reason" in r ? r.reason : null;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The calling agent's side label in a discussion or review with an agent of target `other`: its
|
|
116
|
+
* target id, or CALLER_LABEL where that is `other`'s too (same_family "allow", or a session of the
|
|
117
|
+
* caller's own family that already exists), so that the two sides never share markers, files or
|
|
118
|
+
* item ids. An unknown caller uses CALLER_LABEL as well.
|
|
119
|
+
*/
|
|
120
|
+
export const callerSide = (inst, other) => {
|
|
121
|
+
const own = familyOf(inst) ?? CALLER_LABEL;
|
|
122
|
+
return own === other ? CALLER_LABEL : own;
|
|
123
|
+
};
|
|
124
|
+
/** A root's cwd as given: ~ is the home folder, and a relative path is relay's own working directory's. */
|
|
125
|
+
export const expand = (p) => path.resolve(expandHome(p));
|
|
126
|
+
/**
|
|
127
|
+
* True while the turn a launched agent's relay server was started for is still running. Deliberately
|
|
128
|
+
* weaker than turnGone (approvals.ts): a cancelled run or ancestor is worded apart by admit.
|
|
129
|
+
*/
|
|
130
|
+
const turnLive = (ctx) => turnEnded(ctx.nodeId, ctx.turnId) === null;
|
|
131
|
+
/** Checks existing ownership. Recovery is exclusively the explicit flow in adoption.ts. */
|
|
132
|
+
export function claimRun(inst, runId) {
|
|
133
|
+
return ownsRun(inst, runId) ? null : "item unavailable";
|
|
134
|
+
}
|
|
135
|
+
export const ownsRun = (inst, runId) => Boolean(runId && getRun(runId)?.root_token === inst.token);
|
|
136
|
+
/** Identical for a missing item and a foreign one; no ids, titles, paths or run details. */
|
|
137
|
+
export const UNAVAILABLE = () => ({
|
|
138
|
+
error: "Item unavailable.",
|
|
139
|
+
recovery: "If this is your own work from before a restart or reconnect, ask the person to adopt it: adopt with this id (they confirm), or relay adopt <id> --to <fingerprint> in a terminal.",
|
|
140
|
+
});
|
|
141
|
+
/** Explicit child read allowlist: its injected run, including its supervisor and siblings.
|
|
142
|
+
* Mutation authority remains the narrower descendant/grant/turn check below. */
|
|
143
|
+
export function canReadRun(inst, runId) {
|
|
144
|
+
if (inst.singletonViolation)
|
|
145
|
+
return false;
|
|
146
|
+
return inst.ctx.kind === "root" ? ownsRun(inst, runId)
|
|
147
|
+
: inst.ctx.kind === "child" ? inst.ctx.runId === runId && Boolean(getNode(inst.ctx.nodeId)) : false;
|
|
148
|
+
}
|
|
149
|
+
export function canReadItem(inst, id) {
|
|
150
|
+
const runId = id.startsWith("run_") ? getRun(id)?.id : getNode(id)?.run_id;
|
|
151
|
+
return Boolean(runId && canReadRun(inst, runId));
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Admission for every call that starts or changes something: the fail-closed context check, the
|
|
155
|
+
* two hard limits, and what the caller's current turn allows. `existing` is given when the call
|
|
156
|
+
* names items that already exist, all of which must be in one run. Returns a reason when refused,
|
|
157
|
+
* worded so the agent simply carries on by itself.
|
|
158
|
+
*/
|
|
159
|
+
/**
|
|
160
|
+
* A new, empty directory for one consult thread that names no project, where relay was started
|
|
161
|
+
* outside any (the desktop app): its own, so that nothing an agent of one conversation leaves there
|
|
162
|
+
* is where another's starts (review r_f1c46bd4 C1+X2). A follow-up goes on in it. It is made under
|
|
163
|
+
* consultHome, which must be a plain directory of this user's (C3+X1) holding only such directories,
|
|
164
|
+
* and which no grant or file approval may write (grantPathProblem, approvals.ts offLimits).
|
|
165
|
+
*/
|
|
166
|
+
function newConsultDir() {
|
|
167
|
+
try {
|
|
168
|
+
const root = consultHome();
|
|
169
|
+
fs.mkdirSync(path.dirname(root), { recursive: true });
|
|
170
|
+
ensurePrivateRoot(root);
|
|
171
|
+
// Every new thread's directory is below this one, where an agent can load instructions from its
|
|
172
|
+
// directory's parents (CLAUDE.md, AGENTS.md): it holds only the directories relay made.
|
|
173
|
+
const stray = fs.readdirSync(root, { withFileTypes: true }).find((e) => !(e.isDirectory() && /^c-[A-Za-z0-9]{6}$/.test(e.name)));
|
|
174
|
+
if (stray)
|
|
175
|
+
return { reason: `${root} holds ${JSON.stringify(stray.name)}, which relay did not put there, and which an agent asking below it could load. Remove it, or pass cwd with the absolute path of a directory to ask in.` };
|
|
176
|
+
return { dir: fs.mkdtempSync(path.join(root, "c-")) };
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
return { reason: `relay could not make a directory to ask in without a project: ${err.message}. Pass cwd with the absolute path of a directory to ask in.` };
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function admit(inst, opts = {}) {
|
|
183
|
+
const starts = opts.starts !== false;
|
|
184
|
+
if (inst.ctx.kind === "poisoned") {
|
|
185
|
+
return { reason: `relay context for this agent could not be loaded (${inst.ctx.reason}). Continue without delegating, consulting, or starting discussions.` };
|
|
186
|
+
}
|
|
187
|
+
if (inst.singletonViolation)
|
|
188
|
+
return { reason: SINGLETON_REASON };
|
|
189
|
+
if (inst.ctx.kind === "root") {
|
|
190
|
+
const cfg = loadConfig();
|
|
191
|
+
// Both agents' sandboxes can write to temp directories. Control state there could be edited by a job.
|
|
192
|
+
if (underTemp(relayHome()) && !cfg.allow_temp_home) {
|
|
193
|
+
return { reason: `relay's control directory (${relayHome()}) is inside a temporary directory, where a launched agent's sandbox can write. Move it (unset RELAY_HOME) before starting agents.` };
|
|
194
|
+
}
|
|
195
|
+
let cwd = inst.cwd;
|
|
196
|
+
if (opts.cwd) {
|
|
197
|
+
cwd = expand(opts.cwd);
|
|
198
|
+
if (!fs.existsSync(cwd) || !fs.statSync(cwd).isDirectory())
|
|
199
|
+
return { reason: `cwd ${cwd} is not an existing directory.` };
|
|
200
|
+
}
|
|
201
|
+
else if (path.parse(cwd).root === cwd) {
|
|
202
|
+
// Hosts that do not start relay inside the project (desktop apps) name the directory per call.
|
|
203
|
+
// A call about an existing item works where that item works.
|
|
204
|
+
if (opts.existing)
|
|
205
|
+
cwd = opts.existing.cwd;
|
|
206
|
+
// A new consult needs no project: it asks in a new, empty directory of its own.
|
|
207
|
+
else if (opts.consult) {
|
|
208
|
+
const made = newConsultDir();
|
|
209
|
+
if ("reason" in made)
|
|
210
|
+
return made;
|
|
211
|
+
cwd = made.dir;
|
|
212
|
+
}
|
|
213
|
+
else
|
|
214
|
+
return { reason: "relay was started outside any project directory, so it does not know where to work. Pass cwd with the absolute path of the project." };
|
|
215
|
+
}
|
|
216
|
+
if (opts.existing) {
|
|
217
|
+
const problem = claimRun(inst, opts.existing.runId);
|
|
218
|
+
if (problem)
|
|
219
|
+
return { reason: `that item cannot be used from this session: ${problem}.` };
|
|
220
|
+
return { runId: opts.existing.runId, node: null, depth: 0, cwd, access: "write" };
|
|
221
|
+
}
|
|
222
|
+
if (!ownsRun(inst, inst.runId) || getRun(inst.runId)?.status === "cancelled") {
|
|
223
|
+
inst.runId = createRun(cwd, cfg, inst.token);
|
|
224
|
+
inst.since.set(inst.runId, getRun(inst.runId).created_at);
|
|
225
|
+
refreshEscalationPersonPath(inst.token, { confirm: inst.host.confirmTool === true, dialog: inst.host.personDialog === true && !!inst.host.elicit });
|
|
226
|
+
}
|
|
227
|
+
return { runId: inst.runId, node: null, depth: 0, cwd, access: "write" };
|
|
228
|
+
}
|
|
229
|
+
const ctx = inst.ctx;
|
|
230
|
+
// A launched agent works where relay put it; only a root session chooses a directory.
|
|
231
|
+
if (opts.cwd)
|
|
232
|
+
return { reason: "cwd can only be set by the root session. Omit it; you work in your own directory." };
|
|
233
|
+
const node = getNode(ctx.nodeId);
|
|
234
|
+
const run = getRun(ctx.runId);
|
|
235
|
+
if (!node || !run)
|
|
236
|
+
return { reason: "this agent is no longer in the relay registry. Continue on your own." };
|
|
237
|
+
// The relay server of a turn that has ended keeps none of that turn's authority.
|
|
238
|
+
if (!turnLive(ctx))
|
|
239
|
+
return { reason: "the turn this relay connection belonged to has ended, so it can no longer start or change anything." };
|
|
240
|
+
if (run.status === "cancelled" || node.status === "cancelled")
|
|
241
|
+
return { reason: "this run has been cancelled. Stop and report what you have." };
|
|
242
|
+
if (opts.existing && opts.existing.runId !== run.id)
|
|
243
|
+
return { reason: "that item belongs to a different run." };
|
|
244
|
+
// A session holding MCP servers starts and steers nothing: an agent it started would have none
|
|
245
|
+
// of its limits (no network, every request the person's), and so would be a way round them.
|
|
246
|
+
if (holdsServers(node))
|
|
247
|
+
return { reason: SERVERS_START_NOTHING };
|
|
248
|
+
if (!ctx.mayDelegate)
|
|
249
|
+
return { reason: "you were started with allow_delegation = false, so you cannot start or steer other agents. Do this part yourself." };
|
|
250
|
+
const cfg = runConfig(run.id);
|
|
251
|
+
if (starts && node.depth >= cfg.max_depth) {
|
|
252
|
+
return { reason: `you are at depth ${node.depth} of ${cfg.max_depth}, the maximum, so you cannot start further agents. Do this part yourself.` };
|
|
253
|
+
}
|
|
254
|
+
return { runId: run.id, node, depth: node.depth, cwd: workCwd(node), access: ctx.access, turnId: ctx.turnId };
|
|
255
|
+
}
|
|
256
|
+
export const grantOf = (n) => sessionParams(n).grant ?? {};
|
|
257
|
+
/** Environment requests belong to queued turns, never a standing session grant. */
|
|
258
|
+
export const persistentGrant = (g) => g ? (({ env, ...rest }) => rest)(g) : null;
|
|
259
|
+
/** Session grants can change while a turn runs. Only its launch snapshot is its authority. */
|
|
260
|
+
function runningGrant(n, turnId) {
|
|
261
|
+
const active = nodeTurns(n.id).filter((t) => t.status === "running" && (turnId === undefined || t.id === turnId));
|
|
262
|
+
const grants = active.map((t) => {
|
|
263
|
+
if (t.grant_json === null)
|
|
264
|
+
return {};
|
|
265
|
+
try {
|
|
266
|
+
return JSON.parse(t.grant_json);
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
throw new Error("invalid effective turn grant");
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
return { network: grants.some((g) => g.network), local_network: grants.some((g) => g.local_network), raw_network: grants.some((g) => g.raw_network), env: [...new Set(grants.flatMap((g) => g.env ?? []))], write_paths: [...new Set(grants.flatMap((g) => g.write_paths ?? []))] };
|
|
273
|
+
}
|
|
274
|
+
/** The grant as a caller sees it: without the per-turn directory pins, which are relay's internal plumbing for confinement (write_path_pins). */
|
|
275
|
+
export const publicGrant = (g) => ({ network: g.network, ...(g.local_network ? { local_network: true } : {}), ...(g.raw_network ? { raw_network: true } : {}), ...(g.env?.length ? { env: g.env } : {}), ...(g.write_paths ? { write_paths: g.write_paths } : {}) });
|
|
276
|
+
/**
|
|
277
|
+
* Whether a launched agent may steer, cancel, merge or converse with an existing item. It must be
|
|
278
|
+
* something the agent started, directly or through agents it started, and must not hold more than
|
|
279
|
+
* the agent's own current turn does: a confined agent cannot act through a less confined one.
|
|
280
|
+
*/
|
|
281
|
+
export function authority(caller, target, verb) {
|
|
282
|
+
if (!caller.node)
|
|
283
|
+
return null;
|
|
284
|
+
if (!descendants(caller.node.id).some((d) => d.id === target.id)) {
|
|
285
|
+
return `you can ${verb} only work that you started, or that agents you started went on to start; ${target.id} is not that.`;
|
|
286
|
+
}
|
|
287
|
+
const mine = runningGrant(caller.node, caller.turnId);
|
|
288
|
+
if (nodeTurns(target.id).some((t) => t.status === "running" && t.grant_json === null)) {
|
|
289
|
+
return `${target.id} has a running turn without a recorded effective grant, so relay cannot establish its authority and you cannot ${verb} it.`;
|
|
290
|
+
}
|
|
291
|
+
const current = runningGrant(target);
|
|
292
|
+
const next = grantOf(target);
|
|
293
|
+
const theirs = { network: current.network || next.network, local_network: current.local_network || next.local_network, raw_network: current.raw_network || next.raw_network, write_paths: [...(current.write_paths ?? []), ...(next.write_paths ?? [])] };
|
|
294
|
+
// Extensions count as grants do (review X2): a skill, an MCP server or a tool that runs without
|
|
295
|
+
// asking that the target's session holds and the caller's does not is more than the caller has.
|
|
296
|
+
const myExtensions = new Set(extensionKeys(sessionParams(caller.node).extensions ?? undefined));
|
|
297
|
+
// The web tools are compared apart from the shell's network: a web request allowed for the rest
|
|
298
|
+
// of a job lets its web searches and fetches through (webGranted) without giving its shell the network.
|
|
299
|
+
const more = (target.mode === "write" && caller.access === "read") ||
|
|
300
|
+
(Boolean(theirs.network) && !mine.network) ||
|
|
301
|
+
(Boolean(theirs.local_network) && !mine.local_network) ||
|
|
302
|
+
(Boolean(theirs.raw_network) && !mine.raw_network) ||
|
|
303
|
+
(current.env ?? []).some((name) => !(mine.env ?? []).includes(name)) ||
|
|
304
|
+
(webGranted(target.id) && !webGranted(caller.node.id)) ||
|
|
305
|
+
(theirs.write_paths ?? []).some((p) => !(mine.write_paths ?? []).includes(p)) ||
|
|
306
|
+
extensionKeys(sessionParams(target).extensions ?? undefined).some((k) => !myExtensions.has(k));
|
|
307
|
+
if (more)
|
|
308
|
+
return `${target.id} has more access than your current turn does, so you cannot ${verb} it. Report it to whoever started you.`;
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
// Codex starts a relay server for each of its threads, so a native Codex subagent has a root session
|
|
312
|
+
// of its own, with no person in its conversation (ToolCall.codexSubagent). From one, relay takes a
|
|
313
|
+
// denial but not an allowance, nor a grant (review C6), nor extensions.
|
|
314
|
+
const SUBAGENT_ALLOW = "this call comes from a Codex subagent thread, which no person is working in, so it cannot allow a launched agent's request (whatever it is told the user said). " +
|
|
315
|
+
"Return the request, with its request_id, to the agent that started you. The user can allow it by answering relay's question where Codex shows it, or with `relay approve <request_id>` in a terminal, except a request marked person_only, which relay takes from no command (the user allows it in relay's question, or through confirm). You may deny it.";
|
|
316
|
+
/**
|
|
317
|
+
* Why a Codex subagent thread may not give a grant (review C6). A grant widens what a launched
|
|
318
|
+
* agent may do as an allowance does, and the thread may not allow (SUBAGENT_ALLOW): no person is in
|
|
319
|
+
* its conversation, and a grant it passes on as its parent's carries nothing relay can check (Codex
|
|
320
|
+
* marks the thread as a subagent's, not who started it or what they said). The parent passes it on
|
|
321
|
+
* verifiably by making the call itself, from its own thread.
|
|
322
|
+
*/
|
|
323
|
+
const SUBAGENT_GRANT = "this call comes from a Codex subagent thread, which no person is working in, so it cannot grant network access or write paths: relay cannot tell that the user authorized them, whatever the agent that started you said. " +
|
|
324
|
+
"Make the call without grant (the launched agent then asks relay for what it needs, and the user decides), or return to the agent that started you and ask it to make this call itself with the grant, from its own thread; it can hand you the job_id to wait on with status.";
|
|
325
|
+
/**
|
|
326
|
+
* Why a Codex subagent thread may not give extensions (as it may not give a grant): only a root
|
|
327
|
+
* session gives them (docs/extensions.md X2), and a subagent thread's root session has no person
|
|
328
|
+
* in its conversation, so it would choose, unchecked, which session holds skills and servers.
|
|
329
|
+
*/
|
|
330
|
+
const SUBAGENT_EXTENSIONS = "this call comes from a Codex subagent thread, which no person is working in, so it cannot give extensions: relay cannot tell that the user wants this work to have them, whatever the agent that started you said. " +
|
|
331
|
+
"Make the call without extensions, or return to the agent that started you and ask it to make this call itself, from its own thread; it can hand you the id to wait on with status.";
|
|
332
|
+
/** Why a call from a Codex subagent thread may not allow an approval request, give a grant or give extensions; null for any other call. */
|
|
333
|
+
export function subagentRefusal(call, what) {
|
|
334
|
+
if (!call.codexSubagent)
|
|
335
|
+
return null;
|
|
336
|
+
return what === "allow" ? SUBAGENT_ALLOW : what === "grant" ? SUBAGENT_GRANT : SUBAGENT_EXTENSIONS;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Why `paths`, a grant's write paths, may not be given, or kept, for overlapping a directory that
|
|
340
|
+
* decides which API key relay hands a key-only target (keyAuthority, core/api-keys.ts): relay's
|
|
341
|
+
* config directory with its local config, the default key directory, and where the local config
|
|
342
|
+
* moves a key file. config.json, which holds the per-target opt-in that lets relay hand a key over,
|
|
343
|
+
* is protected with relay's other config files (protected-config.ts): the configured file and the
|
|
344
|
+
* one a link leads to (as when the person syncs it from a dotfiles repository), and their directory where it
|
|
345
|
+
* is relay's alone.
|
|
346
|
+
*/
|
|
347
|
+
function keyGrantProblem(paths) {
|
|
348
|
+
if (!paths.length)
|
|
349
|
+
return null;
|
|
350
|
+
const keys = keyAuthority();
|
|
351
|
+
// Fail closed: a grant is kept with its job, so one given while relay cannot tell where its keys
|
|
352
|
+
// are would still be there once the local config is fixed, and might then cover a key directory.
|
|
353
|
+
if (keys.problem)
|
|
354
|
+
return `relay cannot load its local config, so it cannot tell which directories decide its API keys, and no job may hold a write path until that is fixed: ${keys.problem}`;
|
|
355
|
+
for (const p of paths) {
|
|
356
|
+
let real = p;
|
|
357
|
+
try {
|
|
358
|
+
real = fs.realpathSync.native(p);
|
|
359
|
+
}
|
|
360
|
+
catch {
|
|
361
|
+
// Gone since: judged by its name.
|
|
362
|
+
}
|
|
363
|
+
// On macOS a name in another case, or under the data volume's name, is the same directory.
|
|
364
|
+
for (const [why, dir] of keys.dirs)
|
|
365
|
+
if (insideAnyCase(real, dir) || insideAnyCase(dir, real))
|
|
366
|
+
return `grant.write_paths entry ${real} overlaps ${why} (${dir}), which no job may be given.`;
|
|
367
|
+
}
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
/** The exclusions for both new and kept grants, by every name relay protects. */
|
|
371
|
+
function grantPathProblem(p, installed, cfg) {
|
|
372
|
+
const codex = relayCodexProtected(cfg);
|
|
373
|
+
const protectedCodex = [...codex.homes, ...codex.stores].find((x) => overlapsPart(p, x.path));
|
|
374
|
+
if (protectedCodex)
|
|
375
|
+
return `grant.write_paths entry ${p} overlaps ${protectedCodex.what} (${protectedCodex.path}), which no job may be given.`;
|
|
376
|
+
for (const [what, dir] of [["relay's control directory", relayHome()], ["the directory holding job worktrees", worktreesHome()], ["the directory holding confined agents' state", harnessHome()], ["the directory questions without a project are asked in", consultHome()], ["relay's private turn state", privateStateRoot()], ["native turns' private temporary state", nativeTempRoot()]]) {
|
|
377
|
+
if (insideAnyCase(p, dir) || insideAnyCase(dir, p))
|
|
378
|
+
return `grant.write_paths entry ${p} overlaps ${what} (${dir}), which no job may be given.`;
|
|
379
|
+
}
|
|
380
|
+
// The link itself, by its own name: a job that could write where it is could point it elsewhere.
|
|
381
|
+
const link = nativeTempAlias();
|
|
382
|
+
if (overlapsEntryAnyCase(p, link))
|
|
383
|
+
return `grant.write_paths entry ${p} overlaps relay's short link to native turns' private temporary state (${link}), which no job may be given.`;
|
|
384
|
+
if (underTemp(p) || ["/tmp", "/private/tmp", "/var/tmp", "/private/var/tmp", "/private/var/folders", "/var/folders"].some((dir) => insideAnyCase(p, dir)))
|
|
385
|
+
return `grant.write_paths entry ${p} is under a shared temporary directory, which other agents can write; move it outside shared temp before granting it.`;
|
|
386
|
+
const config = touchesConfig(p);
|
|
387
|
+
if (config)
|
|
388
|
+
return `grant.write_paths entry ${p} overlaps relay's config file ${config}, which no job may be given.`;
|
|
389
|
+
const record = installed.find((c) => pathOverlaps(p, c) || insideAnyCase(p, c) || insideAnyCase(c, p));
|
|
390
|
+
if (record)
|
|
391
|
+
return `grant.write_paths entry ${p} overlaps relay's config or install files (${record}), which no job may be given.`;
|
|
392
|
+
// What relay runs outside every sandbox (review r_0c11d860 X1): its own package, the Node it runs
|
|
393
|
+
// on, and the target CLIs it starts, by the name relay starts each, the file it leads to and the
|
|
394
|
+
// installation holding it. A job that could write any of them would run its code unconfined.
|
|
395
|
+
const part = relayInstallation().find((x) => overlapsPart(p, x.path));
|
|
396
|
+
if (part)
|
|
397
|
+
return `grant.write_paths entry ${p} overlaps ${part.what} (${part.path}), which relay runs outside any sandbox, so no job may be given it. Grant a directory outside it.`;
|
|
398
|
+
// A directory on relay's PATH, one holding it, or one inside it: a program a job put there (or a
|
|
399
|
+
// directory under the program's name, which a lookup skips only if it is not runnable) would be
|
|
400
|
+
// found before the one relay runs now, by a later relay process or your own shell, and run
|
|
401
|
+
// outside any sandbox.
|
|
402
|
+
const onPath = relayPathDirs().find((d) => insideAnyCase(d, p) || insideAnyCase(p, d));
|
|
403
|
+
if (onPath)
|
|
404
|
+
return `grant.write_paths entry ${p} ${foldedPath(onPath) === foldedPath(p) ? "is" : insideAnyCase(onPath, p) ? "holds" : "is inside"} ${onPath}, a directory on relay's PATH, where a program a job put would be run outside any sandbox, so no job may be given it. Grant a directory outside it.`;
|
|
405
|
+
}
|
|
406
|
+
/** Preserve X1/config/key explanations and recovery when a previously granted root becomes unsafe. */
|
|
407
|
+
function keptGrantProblem(paths, cfg) {
|
|
408
|
+
const installed = paths.length ? configProtection().paths : [];
|
|
409
|
+
return paths.map(p => grantPathProblem(p, installed, cfg)).find(Boolean) ?? keyGrantProblem(paths);
|
|
410
|
+
}
|
|
411
|
+
const keptGrantRefusal = (why) => `the grant this session keeps from when it was given can no longer be used: ${why} Continue it with a grant of its own (grant: {write_paths: [...]}) that leaves that out, or start a new job.`;
|
|
412
|
+
/**
|
|
413
|
+
* Validates a grant. Only the root session can give one: a launched agent is itself confined, and
|
|
414
|
+
* letting it widen a child's permissions would let a child hold more than its parent. Nor can a
|
|
415
|
+
* Codex subagent thread's root session (SUBAGENT_GRANT).
|
|
416
|
+
*/
|
|
417
|
+
function checkGrant(caller, grant, mode, call, cfg) {
|
|
418
|
+
const stage2 = stage2Enabled(process.env, cfg);
|
|
419
|
+
// raw_network: false asks for nothing. With stage 2 off it is no grant at all, as before stage 2
|
|
420
|
+
// added the key (review R9). With it on, it counts as a grant, so a root's resume with it
|
|
421
|
+
// replaces the job's grant and so drops raw network rather than keeping it.
|
|
422
|
+
const raw = stage2 ? grant?.raw_network !== undefined : grant?.raw_network !== undefined && grant?.raw_network !== false;
|
|
423
|
+
if (!grant || (!grant.network && !grant.write_paths?.length && !grant.env?.length && !grant.local_network && !raw))
|
|
424
|
+
return {};
|
|
425
|
+
if (caller.node)
|
|
426
|
+
return { reason: "only the root session can grant extra permissions. Omit grant; if the work needs more than a worktree allows, say so in your report." };
|
|
427
|
+
const sub = subagentRefusal(call, "grant");
|
|
428
|
+
if (sub)
|
|
429
|
+
return { reason: sub };
|
|
430
|
+
if (grant.env && (!Array.isArray(grant.env) || grant.env.some((n) => typeof n !== "string" || !ENVIRONMENT_NAME.test(n))))
|
|
431
|
+
return { reason: "grant.env requires exact environment variable names, never values" };
|
|
432
|
+
if (mode !== "write" && grant.write_paths?.length)
|
|
433
|
+
return { reason: "a read-only session can be granted network access but not write paths." };
|
|
434
|
+
if (grant.local_network !== undefined && typeof grant.local_network !== "boolean")
|
|
435
|
+
return { reason: "grant.local_network must be true or false." };
|
|
436
|
+
if (grant.local_network && mode !== "write")
|
|
437
|
+
return { reason: "grant.local_network is for Codex write jobs only: a read-only session's shell never reaches this Mac's loopback or the local network." };
|
|
438
|
+
// grant.raw_network is checked in admitWork (raw-network.ts rawNetworkGrantProblem), after this has refused anyone but the root.
|
|
439
|
+
if (grant.raw_network !== undefined && typeof grant.raw_network !== "boolean")
|
|
440
|
+
return { reason: "grant.raw_network must be true or false." };
|
|
441
|
+
if (grant.local_network && grant.network !== true)
|
|
442
|
+
return { reason: "grant.local_network needs network: true in the same grant: it widens the job's network to this Mac's loopback and the local network." };
|
|
443
|
+
const paths = [];
|
|
444
|
+
const protectedPaths = configProtection().paths;
|
|
445
|
+
for (const raw of grant.write_paths ?? []) {
|
|
446
|
+
const expanded = expandHome(raw);
|
|
447
|
+
if (!path.isAbsolute(expanded))
|
|
448
|
+
return { reason: `grant.write_paths must be absolute paths (got ${raw}).` };
|
|
449
|
+
const p = path.resolve(expanded);
|
|
450
|
+
// Relay's config and install files (protected-config.ts, as the sandboxes protect them), also
|
|
451
|
+
// where the entry is one of them rather than a directory.
|
|
452
|
+
const installed = protectedPaths.find((c) => pathOverlaps(p, c) || insideAnyCase(p, c) || insideAnyCase(c, p));
|
|
453
|
+
const configReason = installed ? `grant.write_paths entry ${p} overlaps relay's config or install files (${installed}), which no job may be given.` : null;
|
|
454
|
+
if (!fs.existsSync(p) || !fs.statSync(p).isDirectory())
|
|
455
|
+
return { reason: configReason ?? `grant.write_paths entry ${p} is not an existing directory.` };
|
|
456
|
+
// Its name on disk: on macOS's default disks a name in another case is the same directory.
|
|
457
|
+
const real = fs.realpathSync.native(p);
|
|
458
|
+
// Relay's own state decides who may do what; other jobs' worktrees belong to other jobs. Its
|
|
459
|
+
// config directory (the local config there says which key file relay reads) and the key
|
|
460
|
+
// directories decide which key relay hands a key-only target, and so whose account it runs on;
|
|
461
|
+
// config.json, which says whether it may, is among relay's config files (configReason).
|
|
462
|
+
// Relay's own config files (config.json, mcp.json; a linked one by either name, and on macOS in
|
|
463
|
+
// any case or under the data volume's name) say what agents may be given and which servers run
|
|
464
|
+
// outside their sandboxes.
|
|
465
|
+
const off = grantPathProblem(real, protectedPaths, cfg) ?? configReason;
|
|
466
|
+
if (off)
|
|
467
|
+
return { reason: off };
|
|
468
|
+
paths.push(real);
|
|
469
|
+
}
|
|
470
|
+
const keys = keyGrantProblem(paths);
|
|
471
|
+
if (keys)
|
|
472
|
+
return { reason: keys };
|
|
473
|
+
// Pin each grant by its resolved identity now, so a later turn refuses it if the directory has been
|
|
474
|
+
// swapped or an intermediate component turned into a link (relay-enforced confinement, review C4/X2).
|
|
475
|
+
return { grant: { network: grant.network === true, ...(grant.local_network ? { local_network: true } : {}), ...(grant.raw_network ? { raw_network: true } : {}), write_paths: paths, ...(grant.env?.length ? { env: [...new Set(grant.env)] } : {}), ...(paths.length ? { write_path_pins: paths.map((p) => dirIdentity(p)) } : {}) } };
|
|
476
|
+
}
|
|
477
|
+
// The order of these is the order a caller is refused in (CT1).
|
|
478
|
+
/**
|
|
479
|
+
* Admission for a call that starts a session: reach, then admit. With `existing`, it starts a
|
|
480
|
+
* discussion with a session that exists: reach as for that session, admit in its run with the depth
|
|
481
|
+
* limit still applied (the discussion is new), then authority over the session.
|
|
482
|
+
*/
|
|
483
|
+
export function admitNew(inst, o) {
|
|
484
|
+
const s = o.existing ?? null;
|
|
485
|
+
// An existing session keeps its own model; a new one is judged by the model asked for.
|
|
486
|
+
const to = reach(inst, s?.target ?? o.target, { existing: Boolean(s), model: s ? s.model : o.model });
|
|
487
|
+
if ("reason" in to)
|
|
488
|
+
return to;
|
|
489
|
+
const caller = admit(inst, { cwd: o.cwd, existing: s ? { runId: s.run_id, cwd: workCwd(s) } : undefined, consult: o.consult });
|
|
490
|
+
if ("reason" in caller)
|
|
491
|
+
return caller;
|
|
492
|
+
const no = s ? authority(caller, s, "bring into a discussion") : null;
|
|
493
|
+
return no ? { reason: no } : { caller, to };
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Admission for a call that continues an item that exists (resume, a follow-up, a discussion or
|
|
497
|
+
* review carried on): admit in its run, then authority over it. Continuing is not a new start: the
|
|
498
|
+
* depth limit was applied when the item began.
|
|
499
|
+
*/
|
|
500
|
+
export function admitExisting(inst, n, verb, cwd) {
|
|
501
|
+
const caller = admit(inst, { cwd, existing: { runId: n.run_id, cwd: workCwd(n) }, starts: false });
|
|
502
|
+
if ("reason" in caller)
|
|
503
|
+
return caller;
|
|
504
|
+
const no = authority(caller, n, verb);
|
|
505
|
+
return no ? { reason: no } : caller;
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* What the work may be given, and whether its target can honour that: checkGrant, then capable
|
|
509
|
+
* under the run's config. `kept` is the grant the session already holds, which a call that gives
|
|
510
|
+
* none goes on with. Write paths count only for write work. Returns the call's own grant, checked.
|
|
511
|
+
*/
|
|
512
|
+
export function admitWork(caller, target, o, call) {
|
|
513
|
+
const cfg = runConfig(caller.runId);
|
|
514
|
+
// Keep grant authority and the established config/key recovery wording ahead of the
|
|
515
|
+
// cumulative floor, as approval requests do for relay's own protected resources.
|
|
516
|
+
// Every admitted grant still has to pass the checkpoint and its exclusions below.
|
|
517
|
+
const g = checkGrant(caller, o.grant, o.mode, call, cfg);
|
|
518
|
+
if (g.reason)
|
|
519
|
+
return { reason: g.reason };
|
|
520
|
+
let never;
|
|
521
|
+
try {
|
|
522
|
+
never = checkpointNever(caller.runId, "grant");
|
|
523
|
+
}
|
|
524
|
+
catch (err) {
|
|
525
|
+
return { reason: err.message };
|
|
526
|
+
}
|
|
527
|
+
// Check caller authority before examining a requested root's contents/identities.
|
|
528
|
+
const neverProblem = neverGrantProblem(never, g.grant ?? o.kept ?? {}, o.mode === "write" && o.worktree ? [o.worktree] : []);
|
|
529
|
+
if (neverProblem)
|
|
530
|
+
return { reason: !g.grant && o.mode === "write" ? keptGrantRefusal(keptGrantProblem(o.kept?.write_paths ?? [], cfg) ?? neverProblem) : neverProblem };
|
|
531
|
+
const envProblem = environmentAdmission(caller.runId, target, g.grant?.env ?? []);
|
|
532
|
+
if (envProblem)
|
|
533
|
+
return { reason: envProblem };
|
|
534
|
+
// Local network is a Codex-only mode of its network proxy (backends/codex-network.ts), which only
|
|
535
|
+
// the person can allow, before the job's first turn under it (session.ts): refused up front where
|
|
536
|
+
// nobody could be asked.
|
|
537
|
+
// Raw network (stage 2) is a Codex write job's, in relay's own sandbox, which only the person can
|
|
538
|
+
// allow, before the job's first turn under it (raw-network.ts rawNetworkConsent): the same.
|
|
539
|
+
const raw = rawNetworkGrantProblem(g.grant, o.mode, target, cfg);
|
|
540
|
+
if (raw)
|
|
541
|
+
return { reason: raw };
|
|
542
|
+
if (g.grant?.raw_network) {
|
|
543
|
+
const person = personConsentAdmission(caller.runId, "grant.raw_network");
|
|
544
|
+
if (person)
|
|
545
|
+
return { reason: person };
|
|
546
|
+
}
|
|
547
|
+
if (g.grant?.local_network) {
|
|
548
|
+
// While stage 2 is switched on, local mode is refused outright (raw-network.ts localModeRetired; decision D2).
|
|
549
|
+
const retired = localModeRetired(cfg);
|
|
550
|
+
if (retired)
|
|
551
|
+
return { reason: `${retired}.` };
|
|
552
|
+
if (target !== "codex")
|
|
553
|
+
return { reason: `grant.local_network is for Codex write jobs only: ${targetLabel(targetDef(target))} has no mode in which its shell reaches this Mac's loopback and the local network. Omit it.` };
|
|
554
|
+
const person = personConsentAdmission(caller.runId, "grant.local_network");
|
|
555
|
+
if (person)
|
|
556
|
+
return { reason: person };
|
|
557
|
+
}
|
|
558
|
+
const grant = g.grant ?? o.kept;
|
|
559
|
+
const nativeNetwork = nativeCodexNeverNetworkProblem({ target: targetDef(target), config: cfg, access: o.mode, grant, neverPolicy: never });
|
|
560
|
+
if (nativeNetwork)
|
|
561
|
+
return { reason: nativeNetwork };
|
|
562
|
+
// Config links and key locations may have changed since authorisation, or a legacy grant may
|
|
563
|
+
// predate these exclusions. Recheck keep-outs without granting anew or replacing identity pins.
|
|
564
|
+
if (!g.grant && o.mode === "write") {
|
|
565
|
+
const kept = keptGrantProblem(o.kept?.write_paths ?? [], cfg);
|
|
566
|
+
if (kept)
|
|
567
|
+
return { reason: keptGrantRefusal(kept) };
|
|
568
|
+
}
|
|
569
|
+
// A session holding MCP servers has no network, and writes nowhere outside its worktree but by the person's leave (docs/extensions.md).
|
|
570
|
+
if (o.servers && networkOn(cfg, grant))
|
|
571
|
+
return { reason: NO_NETWORK_WITH_SERVERS };
|
|
572
|
+
if (o.servers && o.mode === "write" && grant?.write_paths?.length)
|
|
573
|
+
return { reason: NO_WRITE_PATHS_WITH_SERVERS };
|
|
574
|
+
const unable = capable(target, cfg, { mode: o.mode, network: networkOn(cfg, grant), writePaths: o.mode === "write" && Boolean(grant?.write_paths?.length), continues: o.continues });
|
|
575
|
+
if (unable)
|
|
576
|
+
return { reason: unable };
|
|
577
|
+
// A directory Codex's sandbox cannot make writable, by its name (codexWriteRootProblem).
|
|
578
|
+
if (target === "codex" && o.mode === "write") {
|
|
579
|
+
for (const p of grant?.write_paths ?? []) {
|
|
580
|
+
const why = codexWriteRootProblem(p);
|
|
581
|
+
if (why)
|
|
582
|
+
return { reason: `grant.write_paths entry ${JSON.stringify(p)} ${why}. Rename the directory, or give the work to another target.` };
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
return { grant: g.grant };
|
|
586
|
+
}
|
|
587
|
+
/** Why a session holding MCP servers may not start or steer other agents (review C2/X1, X2). */
|
|
588
|
+
export const SERVERS_START_NOTHING = "your session holds MCP servers, so it may not start or steer other agents: they would not have its limits, and could reach the services past relay's gate. Do this part yourself, or say in your report what you needed.";
|
|
589
|
+
/**
|
|
590
|
+
* Why a session holding MCP servers is given no write paths (review C2/X1): every request of such a
|
|
591
|
+
* session, a file outside its worktree included, is the person's alone to allow, and a grant is the
|
|
592
|
+
* root agent's.
|
|
593
|
+
*/
|
|
594
|
+
const NO_WRITE_PATHS_WITH_SERVERS = "a session given MCP servers gets no write paths outside its worktree: only the user may let it change a file outside it (it asks, and they decide each time), since such a file could be a way round relay's gate. Leave out either the write paths or the extensions.";
|
|
595
|
+
/** Why a session holding MCP servers is given no network (review C2/X1). */
|
|
596
|
+
const NO_NETWORK_WITH_SERVERS = "a session given MCP servers gets no network: with it, the agent could use credentials it can read to reach a server's service directly, past relay's gate, where only the user may allow each call. Leave out either the network grant or the extensions.";
|
|
597
|
+
/**
|
|
598
|
+
* The extensions a call that starts a session asks for (extensions: true), resolved from the run's
|
|
599
|
+
* config for its target, or why they cannot be given: only a root session gives them, as with
|
|
600
|
+
* grants, so that nothing a launched agent starts holds one (review X2); only where relay can put
|
|
601
|
+
* the consent to the person; not from a Codex subagent thread (SUBAGENT_EXTENSIONS); and a session
|
|
602
|
+
* with servers gets no network, no write paths and may not delegate.
|
|
603
|
+
*/
|
|
604
|
+
export function admitExtensions(inst, caller, target, o, call) {
|
|
605
|
+
if (caller.node)
|
|
606
|
+
return { reason: "only the root session can give extensions. Omit extensions; if the work needs a skill or an MCP server, say so in your report." };
|
|
607
|
+
const sub = subagentRefusal(call, "extensions");
|
|
608
|
+
if (sub)
|
|
609
|
+
return { reason: sub };
|
|
610
|
+
// A target that takes none is said first: starting the work where the user can be asked would not change it.
|
|
611
|
+
const none = takesNone(target);
|
|
612
|
+
if (none)
|
|
613
|
+
return { reason: none };
|
|
614
|
+
// Consent is the person's alone to give, so a session where relay cannot put it to them gives none.
|
|
615
|
+
if (!inst.host.confirmTool && !inst.host.personDialog) {
|
|
616
|
+
const why = [inst.host.noElicit, inst.host.confirmWithheld].filter(Boolean).join("; ");
|
|
617
|
+
return {
|
|
618
|
+
reason: `only the user can consent to extensions, and relay asks them only ${PERSON_ROUTES}: this session has neither${why ? ` (${why})` : ""}. Omit extensions, or have the user start the work from a session where relay can ask them.`,
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
// Sources belong to the person in this root process. A selected child environment would
|
|
622
|
+
// replace CODEX_HOME with relay's config/login home, which is not a skill source catalog.
|
|
623
|
+
const r = resolveExtensions(runConfig(caller.runId), target, process.env);
|
|
624
|
+
if ("reason" in r)
|
|
625
|
+
return r;
|
|
626
|
+
if (r.mcp_servers.length && o.network)
|
|
627
|
+
return { reason: NO_NETWORK_WITH_SERVERS };
|
|
628
|
+
if (r.mcp_servers.length && o.writePaths)
|
|
629
|
+
return { reason: NO_WRITE_PATHS_WITH_SERVERS };
|
|
630
|
+
if (r.mcp_servers.length && o.allowDelegation) {
|
|
631
|
+
return { reason: "a session given MCP servers may not start other agents: they would not have its limits. Omit allow_delegation (it is off for such a session), or leave out extensions." };
|
|
632
|
+
}
|
|
633
|
+
return { extensions: r };
|
|
634
|
+
}
|
|
635
|
+
/** The speeds `def` runs at, slowest first, no faster than `upTo`. */
|
|
636
|
+
const speedsOf = (def, upTo = "ultrafast") => SPEEDS.filter((s) => (s === "standard" || def.speeds?.[s]) && speedRank(s) <= speedRank(upTo));
|
|
637
|
+
/** `pass speed: "fast" or "standard"`, for a remedy: the speeds given, fastest first. */
|
|
638
|
+
const passSpeed = (speeds) => `pass speed: ${[...speeds].reverse().map((s) => JSON.stringify(s)).join(" or ")}`;
|
|
639
|
+
const capitalised = (s) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
640
|
+
/**
|
|
641
|
+
* Why `def` cannot run at `speed`, from what relay knows up front, or null: a speed it does not
|
|
642
|
+
* offer at all, or (Claude) a model that has no fast mode by its name. Whether the account offers
|
|
643
|
+
* it is the harness's to say: Codex's catalog when the turn starts, Claude's report when it ends.
|
|
644
|
+
* `upTo`: the fastest speed a remedy may name (a launched agent's own). `continues`: the call
|
|
645
|
+
* carries on a session, whose model it cannot change.
|
|
646
|
+
*/
|
|
647
|
+
function speedUnavailable(def, target, speed, o) {
|
|
648
|
+
if (speed === "standard")
|
|
649
|
+
return null;
|
|
650
|
+
const from = {
|
|
651
|
+
call: "",
|
|
652
|
+
session: ` The session this call carries on runs at ${speed}.`,
|
|
653
|
+
inherited: ` Your own session runs at ${speed}, and the work you start runs at your speed unless you pass a slower one.`,
|
|
654
|
+
configured: ` Relay's config sets targets.${target}.speed to ${speed}, the speed of a call that names none.`,
|
|
655
|
+
}[o.source];
|
|
656
|
+
const config = o.source === "configured" ? `, or change targets.${target}.speed` : "";
|
|
657
|
+
const option = def.speeds?.[speed];
|
|
658
|
+
if (!option) {
|
|
659
|
+
const has = speedsOf(def, o.upTo);
|
|
660
|
+
return `${def.name} has no ${speed} speed in relay: it runs at ${speedsOf(def).join(" or ")}.${from} ${capitalised(passSpeed(has))} instead${config}.`;
|
|
661
|
+
}
|
|
662
|
+
if (option.models && o.model && option.models.has(o.model) === false) {
|
|
663
|
+
const remedy = passSpeed(speedsOf(def, "standard"));
|
|
664
|
+
return `${def.name}'s ${speed} speed is for ${option.models.names} only, and this call would run ${JSON.stringify(o.model)}.${from} ` +
|
|
665
|
+
`${o.continues ? capitalised(remedy) : `Name one of those with model, or ${remedy}`}${config}.`;
|
|
666
|
+
}
|
|
667
|
+
return null;
|
|
668
|
+
}
|
|
669
|
+
/** Why a Codex subagent thread may not ask for `speed` (review C6, as for grants), above what the person set up for it. */
|
|
670
|
+
const subagentSpeed = (cap, def) => `this call comes from a Codex subagent thread, which no person is working in, so it cannot raise speed above what the user set up (${cap} for ${def.name}): a faster speed costs them money or allowance, and relay cannot tell that they asked for it. ` +
|
|
671
|
+
`Leave speed out or ${passSpeed(speedsOf(def, cap))}, or return to the agent that started you and ask it to make this call itself, from its own thread.`;
|
|
672
|
+
/**
|
|
673
|
+
* The speed a call's work runs at, or why it may not run at the one asked for. A faster speed
|
|
674
|
+
* costs the person money or allowance, so only the root session raises it (decided 2026-10-16): the
|
|
675
|
+
* work a launched agent starts or carries on runs at the agent's own speed unless it asks for a
|
|
676
|
+
* slower one, and a Codex subagent thread's root session goes no higher than what the person set
|
|
677
|
+
* up. Otherwise the call's speed, else the speed the session it continues goes on at (`current`,
|
|
678
|
+
* db.ts speedAfter), else targets.<id>.speed, else standard. A launched agent's own speed is that of
|
|
679
|
+
* the turn it is making the call from (speedOf: a speed the root has since asked for applies only
|
|
680
|
+
* from the turn that call queued). `model`: the model the session would run, for a speed some
|
|
681
|
+
* models lack. Every call that continues a session comes here, with `current`, whether or not it
|
|
682
|
+
* names a speed: a launched agent the root has since slowed down may not carry on a faster session
|
|
683
|
+
* (as authority refuses one with more access).
|
|
684
|
+
*/
|
|
685
|
+
export function admitSpeed(caller, target, o, call) {
|
|
686
|
+
const cfg = runConfig(caller.runId);
|
|
687
|
+
const def = targetDef(target);
|
|
688
|
+
const configured = cfg.targets[target]?.speed ?? "standard";
|
|
689
|
+
const own = caller.node ? speedOf(caller.node) : null;
|
|
690
|
+
const report = "if the work needs to be faster, say so in your report.";
|
|
691
|
+
if (own !== null) {
|
|
692
|
+
// Asked for, or carried on: either way no faster than the agent itself.
|
|
693
|
+
const faster = o.speed ?? o.current;
|
|
694
|
+
if (faster && speedRank(faster) > speedRank(own)) {
|
|
695
|
+
const why = o.speed ? `you asked for ${o.speed}` : `the session this call carries on runs at ${o.current}`;
|
|
696
|
+
return {
|
|
697
|
+
reason: `only the root session can raise speed, since a faster speed costs the user money or allowance: ${why}, and you run at ${own}, so the work you start or carry on runs at ${own}${own === "standard" ? "" : " or slower"}. ` +
|
|
698
|
+
`${capitalised(passSpeed(speedsOf(def, own)))} instead; ${report}`,
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
if (o.speed && !caller.node && call.codexSubagent) {
|
|
703
|
+
const cap = speedRank(o.current ?? "standard") > speedRank(configured) ? o.current : configured;
|
|
704
|
+
if (speedRank(o.speed) > speedRank(cap))
|
|
705
|
+
return { reason: subagentSpeed(cap, def) };
|
|
706
|
+
}
|
|
707
|
+
// A session that is continued keeps its speed; new work of a launched agent takes the agent's own.
|
|
708
|
+
const speed = o.speed ?? o.current ?? own ?? configured;
|
|
709
|
+
const source = o.speed ? "call" : o.current ? "session" : own !== null ? "inherited" : "configured";
|
|
710
|
+
const no = speedUnavailable(def, target, speed, { model: o.model ?? cfg.targets[target]?.default_model, source, upTo: own ?? "ultrafast", continues: o.current !== undefined });
|
|
711
|
+
return no ? { reason: no } : { speed };
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Keeps the speed a call that continues a session admitted (admitSpeed) for the turn `turnId` it
|
|
715
|
+
* queues: inside the transaction that queues it, so that a call whose message is not taken changes
|
|
716
|
+
* nothing. The session takes it only as that turn starts (session.ts startSpeed), so the turns
|
|
717
|
+
* queued before it, and the one running now (whose launched agent's own speed caps what it starts),
|
|
718
|
+
* keep the speed they were queued at; a turn that never starts changes nothing. Speeds kept for
|
|
719
|
+
* turns no longer queued are dropped here.
|
|
720
|
+
*/
|
|
721
|
+
export function changeSpeed(nodeId, turnId, to) {
|
|
722
|
+
const n = getNode(nodeId);
|
|
723
|
+
if (!n)
|
|
724
|
+
return;
|
|
725
|
+
const live = queuedTurnIds(nodeId);
|
|
726
|
+
const kept = queuedSpeeds(n).filter(([t]) => live.has(t) && t !== turnId);
|
|
727
|
+
setParam(nodeId, "speed_queued", Object.fromEntries([...kept, [turnId, to]].map(([t, s]) => [String(t), s])));
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* The launched agent making a call, as a caller, while the turn its relay connection belongs to
|
|
731
|
+
* lasts; null once it has ended. For cancel and merge, which are offered only to a turn that may
|
|
732
|
+
* delegate and so check no depth and no allow_delegation.
|
|
733
|
+
*/
|
|
734
|
+
export function liveChild(ctx) {
|
|
735
|
+
const node = getNode(ctx.nodeId);
|
|
736
|
+
if (!node || !turnLive(ctx))
|
|
737
|
+
return null;
|
|
738
|
+
return { runId: ctx.runId, node, depth: node.depth, cwd: workCwd(node), access: ctx.access, turnId: ctx.turnId };
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Whose request_ids these are: a root session's relay server process, one conversation of a shared
|
|
742
|
+
* one, or one launched agent. A dedicated root's scope is its server process (its token). On a
|
|
743
|
+
* shared process (the Claude desktop app's relay-desktop) each conversation's view carries the
|
|
744
|
+
* owner hash of its session token instead, so conversations' ids never meet, and a restart keeps
|
|
745
|
+
* them (session-token.ts).
|
|
746
|
+
*/
|
|
747
|
+
export function callScope(inst) {
|
|
748
|
+
return inst.ctx.kind === "root" ? `root:${inst.token}` : inst.ctx.kind === "child" ? `node:${inst.ctx.nodeId}` : null;
|
|
749
|
+
}
|