@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.
Files changed (296) hide show
  1. package/LICENSE +21 -0
  2. package/LICENSES/Apache-2.0.txt +201 -0
  3. package/LICENSES/README.md +19 -0
  4. package/LICENSES/codex-NOTICE.txt +6 -0
  5. package/README.md +393 -2
  6. package/SECURITY.md +145 -0
  7. package/adapters/briefs/claude.md +43 -0
  8. package/adapters/briefs/codex.md +56 -0
  9. package/adapters/briefs/models.json +33 -0
  10. package/adapters/build.mjs +8 -0
  11. package/adapters/claude/.claude-plugin/marketplace.json +13 -0
  12. package/adapters/claude/relay/.claude-plugin/plugin.json +5 -0
  13. package/adapters/claude/relay/agents/codex.md +21 -0
  14. package/adapters/claude/relay/agents/operator.md +21 -0
  15. package/adapters/claude/relay/skills/ask/SKILL.md +20 -0
  16. package/adapters/claude/relay/skills/chat/SKILL.md +13 -0
  17. package/adapters/claude/relay/skills/delegate/SKILL.md +21 -0
  18. package/adapters/claude/relay/skills/discuss/SKILL.md +19 -0
  19. package/adapters/claude/relay/skills/review/SKILL.md +21 -0
  20. package/adapters/claude/relay/skills/tree/SKILL.md +10 -0
  21. package/adapters/claude/relay/skills/write-for-codex/SKILL.md +78 -0
  22. package/adapters/codex/.agents/plugins/marketplace.json +20 -0
  23. package/adapters/codex/AGENTS.snippet.md +5 -0
  24. package/adapters/codex/relay/.codex-plugin/plugin.json +6 -0
  25. package/adapters/codex/relay/skills/relay-ask/SKILL.md +20 -0
  26. package/adapters/codex/relay/skills/relay-chat/SKILL.md +13 -0
  27. package/adapters/codex/relay/skills/relay-delegate/SKILL.md +21 -0
  28. package/adapters/codex/relay/skills/relay-discuss/SKILL.md +19 -0
  29. package/adapters/codex/relay/skills/relay-review/SKILL.md +21 -0
  30. package/adapters/codex/relay/skills/relay-tree/SKILL.md +10 -0
  31. package/adapters/codex/relay/skills/relay-write-for-claude/SKILL.md +66 -0
  32. package/adapters/generate.mjs +124 -0
  33. package/bin/relay +59 -0
  34. package/codex-pin.json +12 -0
  35. package/dist/backends/acp.js +1424 -0
  36. package/dist/backends/claude.js +890 -0
  37. package/dist/backends/cli-version.js +112 -0
  38. package/dist/backends/codex-app.js +1539 -0
  39. package/dist/backends/codex-daybreak.js +75 -0
  40. package/dist/backends/codex-env.js +656 -0
  41. package/dist/backends/codex-network.js +304 -0
  42. package/dist/backends/codex-pin.js +345 -0
  43. package/dist/backends/codex-shell-policy.js +182 -0
  44. package/dist/backends/codex-stage2.js +998 -0
  45. package/dist/backends/codex-subagents.js +47 -0
  46. package/dist/backends/codex.js +641 -0
  47. package/dist/backends/copilot.js +246 -0
  48. package/dist/backends/env.js +147 -0
  49. package/dist/backends/gemini.js +607 -0
  50. package/dist/backends/grok.js +337 -0
  51. package/dist/backends/index.js +8 -0
  52. package/dist/backends/native-temp.js +187 -0
  53. package/dist/backends/never-admission.js +90 -0
  54. package/dist/backends/run.js +67 -0
  55. package/dist/backends/stream.js +230 -0
  56. package/dist/backends/tool-discovery.js +15 -0
  57. package/dist/backends/types.js +264 -0
  58. package/dist/build-info.json +5 -0
  59. package/dist/cli.js +344 -0
  60. package/dist/confine/bridge.js +113 -0
  61. package/dist/confine/broker-core.js +76 -0
  62. package/dist/confine/broker-http.js +650 -0
  63. package/dist/confine/broker-names.js +9 -0
  64. package/dist/confine/broker.js +258 -0
  65. package/dist/confine/canary.js +380 -0
  66. package/dist/confine/diagnostics.js +122 -0
  67. package/dist/confine/gemini-tools-main.js +8 -0
  68. package/dist/confine/gemini-tools.js +181 -0
  69. package/dist/confine/hardlinks.js +58 -0
  70. package/dist/confine/harness-config.js +16 -0
  71. package/dist/confine/index.js +117 -0
  72. package/dist/confine/leases.js +217 -0
  73. package/dist/confine/paths.js +6 -0
  74. package/dist/confine/peer.js +115 -0
  75. package/dist/confine/port-policy.js +8 -0
  76. package/dist/confine/ports.js +365 -0
  77. package/dist/confine/private-root.js +208 -0
  78. package/dist/confine/probe-stage2.js +501 -0
  79. package/dist/confine/probe.js +1109 -0
  80. package/dist/confine/proxy.js +783 -0
  81. package/dist/confine/sbcheck.js +208 -0
  82. package/dist/confine/seatbelt-never.js +41 -0
  83. package/dist/confine/seatbelt-stage2.js +139 -0
  84. package/dist/confine/seatbelt.js +1129 -0
  85. package/dist/confine/sockets.js +36 -0
  86. package/dist/confine/turn-probe.js +19 -0
  87. package/dist/confine/turn.js +86 -0
  88. package/dist/core/adoption.js +124 -0
  89. package/dist/core/api-keys.js +423 -0
  90. package/dist/core/approvals.js +740 -0
  91. package/dist/core/build-identity.js +248 -0
  92. package/dist/core/caller.js +749 -0
  93. package/dist/core/calls.js +102 -0
  94. package/dist/core/claude-settings.js +1045 -0
  95. package/dist/core/config.js +453 -0
  96. package/dist/core/context.js +186 -0
  97. package/dist/core/db.js +753 -0
  98. package/dist/core/dispatch.js +110 -0
  99. package/dist/core/env-broker.js +505 -0
  100. package/dist/core/env-policy.js +182 -0
  101. package/dist/core/escalation-authority.js +76 -0
  102. package/dist/core/escalation-contract.js +13 -0
  103. package/dist/core/escalation-person-path.js +131 -0
  104. package/dist/core/escalation-policy.js +79 -0
  105. package/dist/core/escalation-runtime.js +135 -0
  106. package/dist/core/escalation-state.js +42 -0
  107. package/dist/core/escalations.js +191 -0
  108. package/dist/core/events.js +235 -0
  109. package/dist/core/executables.js +147 -0
  110. package/dist/core/extensions.js +645 -0
  111. package/dist/core/families.js +204 -0
  112. package/dist/core/git.js +657 -0
  113. package/dist/core/identity.js +113 -0
  114. package/dist/core/liveness.js +68 -0
  115. package/dist/core/mediated-network-provider.js +81 -0
  116. package/dist/core/never-checkpoint.js +76 -0
  117. package/dist/core/never-coverage.js +97 -0
  118. package/dist/core/never-network.js +49 -0
  119. package/dist/core/never-policy.js +302 -0
  120. package/dist/core/never-proxy-policy.js +109 -0
  121. package/dist/core/never-schema.js +61 -0
  122. package/dist/core/never-tools.js +56 -0
  123. package/dist/core/notices.js +48 -0
  124. package/dist/core/ops.js +959 -0
  125. package/dist/core/outcome.js +224 -0
  126. package/dist/core/params.js +32 -0
  127. package/dist/core/presence.js +174 -0
  128. package/dist/core/procs.js +546 -0
  129. package/dist/core/prompts.js +363 -0
  130. package/dist/core/protected-config.js +238 -0
  131. package/dist/core/raw-network.js +192 -0
  132. package/dist/core/redact.js +152 -0
  133. package/dist/core/refusal.js +33 -0
  134. package/dist/core/relay-command.js +310 -0
  135. package/dist/core/reported.js +3 -0
  136. package/dist/core/restart-overlay.js +651 -0
  137. package/dist/core/retention.js +1135 -0
  138. package/dist/core/secrets.js +329 -0
  139. package/dist/core/session-token.js +175 -0
  140. package/dist/core/session.js +744 -0
  141. package/dist/core/shell.js +2 -0
  142. package/dist/core/shipped-pin.js +47 -0
  143. package/dist/core/shown.js +42 -0
  144. package/dist/core/slots.js +67 -0
  145. package/dist/core/stage2-executors.js +453 -0
  146. package/dist/core/stage2-switch.js +5 -0
  147. package/dist/core/steering.js +149 -0
  148. package/dist/core/storage.js +358 -0
  149. package/dist/core/toml.js +175 -0
  150. package/dist/core/views.js +324 -0
  151. package/dist/core/wait.js +114 -0
  152. package/dist/core/wake.js +207 -0
  153. package/dist/core/watch.js +247 -0
  154. package/dist/core/worktree.js +213 -0
  155. package/dist/hosts/claude-desktop.js +274 -0
  156. package/dist/hosts/claude.js +249 -0
  157. package/dist/hosts/codex.js +345 -0
  158. package/dist/hosts/desktop-shared.js +20 -0
  159. package/dist/hosts/index.js +11 -0
  160. package/dist/hosts/types.js +4 -0
  161. package/dist/install/basics.js +18 -0
  162. package/dist/install/claude-init.js +133 -0
  163. package/dist/install/common.js +655 -0
  164. package/dist/install/doctor.js +635 -0
  165. package/dist/install/qualify-codex.js +205 -0
  166. package/dist/install.js +524 -0
  167. package/dist/mcp/elicit.js +209 -0
  168. package/dist/mcp/gateway.js +218 -0
  169. package/dist/mcp/host.js +253 -0
  170. package/dist/mcp/server.js +1124 -0
  171. package/dist/mcp/stdio.js +37 -0
  172. package/dist/modes/discussion.js +393 -0
  173. package/dist/modes/review.js +722 -0
  174. package/dist/native/relay-sbcheck +0 -0
  175. package/dist/targets/briefs.js +86 -0
  176. package/dist/targets/claude.js +94 -0
  177. package/dist/targets/codex.js +66 -0
  178. package/dist/targets/copilot.js +93 -0
  179. package/dist/targets/gemini.js +119 -0
  180. package/dist/targets/grok.js +126 -0
  181. package/dist/targets/index.js +192 -0
  182. package/dist/targets/types.js +18 -0
  183. package/dist/targets/verified.js +35 -0
  184. package/dist/worker.js +113 -0
  185. package/docs/UPGRADING.md +1518 -0
  186. package/docs/pinned-codex.md +333 -0
  187. package/experiments/stage2-redteam/lib/oracles.mjs +214 -0
  188. package/experiments/stage2-redteam/lib/report.mjs +35 -0
  189. package/npm-shrinkwrap.json +1632 -0
  190. package/package.json +82 -4
  191. package/scripts/codex-qualify/codex-pin-store.mjs +353 -0
  192. package/scripts/codex-qualify/codex-qualifier-process.mjs +265 -0
  193. package/scripts/codex-qualify/codex-qualifier.example.json +5 -0
  194. package/scripts/codex-qualify/codex-qualifier.mjs +281 -0
  195. package/scripts/codex-qualify/codex-shipped-pin.mjs +158 -0
  196. package/scripts/codex-qualify/codex-stage2-pin.mjs +48 -0
  197. package/scripts/codex-qualify/codex-stage2.mjs +2381 -0
  198. package/scripts/codex-qualify/install-codex-qualifier.mjs +98 -0
  199. package/scripts/codex-qualify/main-module.mjs +14 -0
  200. package/scripts/codex-qualify/relay-codex-qualifier.plist +37 -0
  201. package/scripts/codex-qualify/scratch-env.mjs +31 -0
  202. package/scripts/codex-qualify/session.mjs +96 -0
  203. package/scripts/codex-qualify/stage2-escalation-cases.mjs +125 -0
  204. package/test/codex-permissions.test.ts +875 -0
  205. package/test/fixtures/boundary-probes.mjs +17 -0
  206. package/test/fixtures/denial-stream-host.mjs +7 -0
  207. package/test/fixtures/families.json +246 -0
  208. package/test/fixtures/install-snapshots/claude-desktop-foreign.txt +158 -0
  209. package/test/fixtures/install-snapshots/claude-desktop-fresh.txt +101 -0
  210. package/test/fixtures/install-snapshots/claude-desktop-installed.txt +161 -0
  211. package/test/fixtures/install-snapshots/claude-foreign.txt +133 -0
  212. package/test/fixtures/install-snapshots/claude-fresh.txt +77 -0
  213. package/test/fixtures/install-snapshots/claude-installed.txt +133 -0
  214. package/test/fixtures/install-snapshots/codex-foreign.txt +159 -0
  215. package/test/fixtures/install-snapshots/codex-fresh.txt +107 -0
  216. package/test/fixtures/install-snapshots/codex-installed.txt +161 -0
  217. package/test/fixtures/link-parent.mjs +6 -0
  218. package/test/fixtures/mediated-command.cjs +72 -0
  219. package/test/fixtures/native-skill-isolation.json +280 -0
  220. package/test/fixtures/pinned-codex-app-server.mjs +25 -0
  221. package/test/fixtures/qualified-ext-adapters.mjs +36 -0
  222. package/test/fixtures/refusals/README.md +52 -0
  223. package/test/fixtures/refusals/claude-source-result.json +7 -0
  224. package/test/fixtures/refusals/openai-app-capacity.json +24 -0
  225. package/test/fixtures/refusals/openai-app-cyberPolicy.json +24 -0
  226. package/test/fixtures/refusals/openai-gpt-6-astra-max.json +12 -0
  227. package/test/fixtures/refusals/openai-gpt-6.1-sol-xhigh.json +12 -0
  228. package/test/fixtures/refusals/openai-review-capacity.json +24 -0
  229. package/test/fixtures/relay-0.2.0.sql +57 -0
  230. package/test/fixtures/relay-0.9.1.gen.mjs +212 -0
  231. package/test/fixtures/relay-0.9.1.sql +340 -0
  232. package/test/fixtures/slow-boundary-probes.mjs +37 -0
  233. package/test/fixtures/stable-surfaces/config.json +1122 -0
  234. package/test/fixtures/stable-surfaces/escalation.json +128 -0
  235. package/test/fixtures/stable-surfaces/events.json +37 -0
  236. package/test/fixtures/stable-surfaces/gemini-native-resume.json +4 -0
  237. package/test/fixtures/stable-surfaces/inbox.jsonl +2 -0
  238. package/test/fixtures/stable-surfaces/notice.txt +1 -0
  239. package/test/fixtures/stable-surfaces/refusal-item.json +28 -0
  240. package/test/fixtures/stable-surfaces/refusal-review.json +23 -0
  241. package/test/fixtures/stable-surfaces/registry.json +49 -0
  242. package/test/fixtures/stable-surfaces/tools-desktop.json +575 -0
  243. package/test/fixtures/stable-surfaces/tools.json +716 -0
  244. package/test/fixtures/stable-surfaces/usage.txt +46 -0
  245. package/test/fixtures/stage2-acp-native.mjs +138 -0
  246. package/test/fixtures/stage2-app-server.mjs +23 -0
  247. package/test/fixtures/stage2-catalog.json +91 -0
  248. package/test/fixtures/stage2-hostile-web.mjs +35 -0
  249. package/test/fixtures/stage2-native-network.mjs +62 -0
  250. package/test/fixtures/stage2-worker.ts +87 -0
  251. package/test/fixtures/status-views/expected.json +7825 -0
  252. package/test/fixtures/status-views/registries.json +1 -0
  253. package/test/fixtures/steering.json +209 -0
  254. package/test/helpers.ts +740 -0
  255. package/test/host-env.ts +7 -0
  256. package/test/keychain-decoy.ts +197 -0
  257. package/test/live-scope.ts +44 -0
  258. package/test/mediated-network.test.ts +192 -0
  259. package/test/mediated-proxy.test.ts +264 -0
  260. package/test/native-temp-alias.ts +10 -0
  261. package/test/never-policy.test.ts +629 -0
  262. package/test/never-tools.test.ts +136 -0
  263. package/test/process-home.ts +18 -0
  264. package/test/proxy.test.ts +531 -0
  265. package/test/restart-overlay-live.test.ts +85 -0
  266. package/test/restart-overlay-profile.test.ts +66 -0
  267. package/test/restart-overlay.test.ts +416 -0
  268. package/test/stable-surfaces.test.ts +249 -0
  269. package/test/stage2-escalation-live.test.ts +544 -0
  270. package/test/stage2-escalation-qualification.test.ts +170 -0
  271. package/test/stage2-escalation.test.ts +747 -0
  272. package/test/stage2-executor.test.ts +849 -0
  273. package/test/stage2-grant.test.ts +603 -0
  274. package/test/stage2-native-live.test.ts +647 -0
  275. package/test/stage2-network-decisions.test.ts +70 -0
  276. package/test/stage2-probe.test.ts +845 -0
  277. package/test/stage2-standing-live.test.ts +203 -0
  278. package/test/stubs/broker-host.mjs +59 -0
  279. package/test/stubs/broker-peer.mjs +20 -0
  280. package/test/stubs/confined-harness.mjs +91 -0
  281. package/test/stubs/ext-broken-server.mjs +2 -0
  282. package/test/stubs/ext-odd-server.mjs +21 -0
  283. package/test/stubs/ext-server.mjs +31 -0
  284. package/test/stubs/fake-host-cli.mjs +218 -0
  285. package/test/stubs/procargs.mjs +30 -0
  286. package/test/stubs/stub-acp +4 -0
  287. package/test/stubs/stub-agent.mjs +322 -0
  288. package/test/stubs/stub-claude +4 -0
  289. package/test/stubs/stub-codex +4 -0
  290. package/test/stubs/targets/acp.mjs +307 -0
  291. package/test/stubs/targets/claude.mjs +149 -0
  292. package/test/stubs/targets/codex.mjs +479 -0
  293. package/test/stubs/targets/gemini.mjs +604 -0
  294. package/test/temp-root.ts +11 -0
  295. package/test/wp4-adapters.test.ts +353 -0
  296. package/test/wp4-interlock.test.ts +136 -0
@@ -0,0 +1,192 @@
1
+ import crypto from "node:crypto";
2
+ import { getNode, now, runConfig, setParam } from "./db.js";
3
+ import { sessionParams } from "./params.js";
4
+ import { requestApproval } from "./approvals.js";
5
+ import { JOB_BLOCK_SIZE, STATIC_DENIED_TCP, STATIC_DENIED_UDP } from "../confine/ports.js";
6
+ import { targetDef, targetLabel } from "../targets/index.js";
7
+ import { overlayInterlock } from "./restart-overlay.js";
8
+ /**
9
+ * Stage 2's grant, consent and interlock (docs/codex-confinement-design.md §6, decisions D1, D2;
10
+ * review X5, C2). Owner: WP-E.
11
+ *
12
+ * The switch: `targets.codex.stage2: true` in relay's config (the run's, which is fixed when the run
13
+ * starts). RELAY_EXPERIMENTAL_CODEX_STAGE2=1 in relay's environment also switches it on, for tests
14
+ * and development.
15
+ */
16
+ export { STAGE2_FLAG_ENV, stage2Enabled } from "./stage2-switch.js";
17
+ import { stage2Enabled } from "./stage2-switch.js";
18
+ /** What the static denied ports are, for the person (§5.3, decision D5). A port without a name here is listed by number. */
19
+ const PORT_NAMES = {
20
+ 22: "ssh", 5900: "screen sharing", 3283: "Remote Management",
21
+ 3031: "Remote Apple Events", 445: "SMB file sharing", 548: "AFP file sharing", 631: "printing",
22
+ };
23
+ const portList = (ports) => ports.map((p) => (PORT_NAMES[p] ? `${p} (${PORT_NAMES[p]})` : String(p))).join(", ");
24
+ /**
25
+ * What consenting to a Codex job's raw network does (§6, §5.2-5.9; decisions D3, D4, D6, D9): the
26
+ * effect relay shows the person with the request (approvals.ts effectOf). Every clause is a
27
+ * consequence the design states; none claims more than the profile enforces.
28
+ */
29
+ export const RAW_NETWORK_CONSENT_TEXT = "lets this Codex job's shell use raw network for the rest of the job, inside relay's own sandbox instead of Codex's: " +
30
+ "TCP, UDP, ssh and DNS to any host, not only HTTP(S) through a proxy. " +
31
+ "It can reach every device and service on your local network (LAN) and your tailnet, on any port, with your account's reach. " +
32
+ "On this Mac it can reach every service on loopback except the denied ports: " +
33
+ `TCP ${portList(STATIC_DENIED_TCP)}, the ports your never.ports entries name, every TCP port anything on this Mac (your processes or the system's) is listening on when each turn starts, and relay's own reserved ports; ` +
34
+ `UDP ${STATIC_DENIED_UDP.join(" and ")} (port mappings on your router) to every host. ` +
35
+ "A service you start after a turn starts is reachable. " +
36
+ "Over IPv6 those TCP ports are refused at every host, since relay's sandbox cannot otherwise refuse the IPv6 forms of this Mac's addresses: IPv6 to those port numbers anywhere, and IPv4 that a program sends through an IPv6 socket (as Java does), fails. " +
37
+ "Otherwise the deny covers only this Mac's own addresses as destinations: a router port forward reached back through NAT hairpin, a tailnet subnet router or a VM bridge can still lead to a denied service. " +
38
+ `It can listen only on its own block of ${JOB_BLOCK_SIZE} TCP ports (given to it as PORT and PORTS), and a server it binds there on 0.0.0.0 can be reached from your LAN and tailnet. ` +
39
+ "Inside the job, git commands that write (git add, commit, stash, checkout -b) fail: relay commits the job's work at the end of each turn, with its own message. " +
40
+ "Without it, give the job grant.network alone: public internet through relay's stage-2 mediated proxy, and nothing local without a separately approved addition; no Go/Swift TLS until Release B, and tools ignoring proxy variables fail closed";
41
+ /**
42
+ * What a stage-2 turn's agent is told at the top of its input (WP-C prepends it in
43
+ * codexBackend.start, as chooseNetwork's prefix is for proxied turns): the network it has, where it
44
+ * may listen, and that git cannot write (decision D6).
45
+ */
46
+ export const RAW_NETWORK_TURN_NOTE = "[relay] This turn your shell has raw network (TCP, UDP, ssh, DNS) to the internet, the local network and this Mac's loopback, except some denied local ports. " +
47
+ "Listen only on the TCP ports in $PORTS ($PORT is the first), bound to 127.0.0.1: other ports, and port 0, are refused. " +
48
+ "git commands that write (add, commit, stash, checkout -b, rebase) fail here: leave your changes in the working tree, and relay commits them when the turn ends.";
49
+ /**
50
+ * Admission of a grant with raw_network, for a caller's delegate or resume (caller.ts admitWork,
51
+ * after checkGrant has refused a grant from anyone but the root session). Null when the grant does
52
+ * not ask for it, or asks for it where it can run. The person's consent is asked later, before the
53
+ * job's first turn under it (rawNetworkConsent); where nobody could be asked, admitWork refuses up
54
+ * front (personConsentAdmission).
55
+ *
56
+ * It needs `network: true` in the same grant. Raw network is more network, not another kind: every
57
+ * other place in relay that asks whether a job has network (networkOn, the web tools, capable, the
58
+ * MCP-server rule, authority) reads grant.network, so they all treat the job as networked without
59
+ * each learning about raw_network; and a resume that drops raw_network falls back to strict network,
60
+ * not to none. A grant of raw network without network would contradict itself.
61
+ */
62
+ export function rawNetworkGrantProblem(grant, mode, target, cfg) {
63
+ if (grant?.raw_network === undefined)
64
+ return null;
65
+ if (typeof grant.raw_network !== "boolean")
66
+ return "grant.raw_network must be true or false.";
67
+ if (!grant.raw_network)
68
+ return null;
69
+ if (!stage2Enabled(process.env, cfg))
70
+ return "grant.raw_network is not available: relay's stage 2 (Codex commands in relay's own sandbox) is switched off (targets.codex.stage2 in relay's config). Omit it; grant.network gives internet over HTTP(S).";
71
+ if (target !== "codex")
72
+ return `grant.raw_network is for Codex write jobs only: ${targetLabel(targetDef(target))} has no mode in which relay's own sandbox gives its shell raw network. Omit it.`;
73
+ if (mode !== "write")
74
+ return "grant.raw_network is for Codex write jobs only: a read-only session's shell never gets raw network. Use grant.network for internet over HTTP(S).";
75
+ if (grant.network !== true)
76
+ return "grant.raw_network needs network: true in the same grant: it widens the job's network to raw TCP, UDP, ssh and DNS.";
77
+ if (grant.local_network)
78
+ return "grant.raw_network and grant.local_network cannot be combined: choose one. raw_network gives raw network inside relay's sandbox; local_network gives HTTP(S) through Codex's proxy with this Mac's loopback and local network open.";
79
+ if (cfg?.codex_backend === "exec")
80
+ return 'grant.raw_network needs codex_backend "app-server": relay runs raw-network Codex jobs through the app server only.';
81
+ return null;
82
+ }
83
+ /**
84
+ * The key a consent is bound to: the parts of the grant that decide what the job's shell can reach
85
+ * or write, and the consent text the person was shown. A resume whose grant changes any of them,
86
+ * or a relay whose consent text says something else, asks again.
87
+ */
88
+ export function rawNetworkKey(grant) {
89
+ return JSON.stringify({
90
+ network: grant.network === true, raw_network: grant.raw_network === true, write_paths: [...(grant.write_paths ?? [])].sort(),
91
+ text: crypto.createHash("sha256").update(RAW_NETWORK_CONSENT_TEXT).digest("hex").slice(0, 16),
92
+ });
93
+ }
94
+ /** The question the person is asked about a Codex job's raw network (the request's `wants`). */
95
+ export function rawNetworkText(node) {
96
+ return `allow ${node.id} (${node.target ? targetLabel(targetDef(node.target)) : "the agent"}, a write job) raw network from its shell (TCP, UDP, ssh, DNS) inside relay's sandbox, with your local network and tailnet reachable, for the rest of the job`;
97
+ }
98
+ /**
99
+ * Before a turn of a job whose grant says raw_network: the person's consent, once for the grant the
100
+ * turn launches with, stored in the session's params as `raw_network_consent` (params.ts). Same
101
+ * shape as session.ts localNetworkConsent. Null when there is nothing to ask.
102
+ *
103
+ * `node` is the job as the turn read it when it was picked up (session.ts), whose grant the turn
104
+ * records and launches with (grantFor). The decision and the key are that grant's, never the
105
+ * session's current one: a resume while the turn waits (for its slot, or for consent to its
106
+ * extensions) replaces the session's grant at once, and the waiting turn still launches with the
107
+ * one it read (review R2). stage2Admission checks the stored consent against the turn's grant
108
+ * again, last thing before the launch (rawNetworkConsentProblem).
109
+ *
110
+ * A consent is for the grant it was given under (review r_17565c33 C4, as for local network): once
111
+ * the session holds another grant, whatever this turn's access, it is gone, and a grant that comes
112
+ * back later is asked about again. ops.ts resume clears it too, as it stores the new grant.
113
+ */
114
+ export async function rawNetworkConsent(node, turnId, access, cancelCheck) {
115
+ const params = sessionParams(getNode(node.id));
116
+ const standing = params.grant ?? undefined;
117
+ const given = params.raw_network_consent ?? null;
118
+ if (given && (!standing?.raw_network || rawNetworkKey(standing) !== given.grant))
119
+ setParam(node.id, "raw_network_consent", null);
120
+ // The turn's own grant, as it read it (above).
121
+ const grant = sessionParams(node).grant ?? undefined;
122
+ if (!grant?.raw_network)
123
+ return null;
124
+ // A read turn of the job (a discussion on its session, say) keeps only grant.network (session.ts grantFor).
125
+ if (node.target !== "codex" || access !== "write")
126
+ return null;
127
+ const off = "so its agent was not started. Continue it with a grant without raw_network (grant.network gives internet over HTTP(S))";
128
+ if (grant.network !== true)
129
+ return { refusal: `this job's grant holds raw_network without network, ${off}.` };
130
+ if (!stage2Enabled(process.env, runConfig(node.run_id)))
131
+ return { refusal: `relay's stage 2 (raw network in relay's own sandbox) is switched off, ${off}.` };
132
+ const key = rawNetworkKey(grant);
133
+ if (sessionParams(getNode(node.id)).raw_network_consent?.grant === key)
134
+ return null;
135
+ const answer = await requestApproval(node.id, turnId, { kind: "raw_network", summary: rawNetworkText(node) }, cancelCheck);
136
+ if (!answer.allow) {
137
+ if (cancelCheck())
138
+ return { cancelled: "cancelled while waiting for the user's consent to its raw network" };
139
+ const why = answer.decider
140
+ ? `consent to this job's raw network was refused ${answer.decider}${answer.deciderNote ? ` (${answer.deciderNote})` : ""}`
141
+ : `nobody consented to this job's raw network (${answer.note ?? "no answer"})`;
142
+ return { refusal: `${why}, so its agent was not started. Continue it to be asked again, or continue it with a grant without raw_network (grant.network gives internet over HTTP(S)).` };
143
+ }
144
+ setParam(node.id, "raw_network_consent", { grant: key, by: answer.decider ?? "by the user", at: now() });
145
+ return null;
146
+ }
147
+ /**
148
+ * Admission step 1 (§2.2): the person's consent, as stored, is for the grant this turn launches
149
+ * with. Null when it is; otherwise why not. Read last thing before the launch (stage2Admission), so
150
+ * a consent that a resume ended while the turn started (ops.ts), or a turn that reached the launch
151
+ * without being asked, refuses rather than runs.
152
+ */
153
+ export function rawNetworkConsentProblem(nodeId, grant) {
154
+ if (!grant?.raw_network)
155
+ return "the turn's grant does not hold raw_network";
156
+ const node = nodeId ? getNode(nodeId) : undefined;
157
+ if (!node)
158
+ return "relay cannot find the job in its registry, so it cannot read the person's consent";
159
+ const given = sessionParams(node).raw_network_consent ?? null;
160
+ if (!given)
161
+ return "the person has not consented to this job's raw network, or a resume that changed its grant ended the consent while this turn started. Continue it to be asked again";
162
+ if (given.grant !== rawNetworkKey(grant))
163
+ return "the person's consent is for another grant than the one this turn holds (a resume changed it while this turn started). Continue it to be asked again";
164
+ return null;
165
+ }
166
+ /**
167
+ * Local mode (1.0.5's grant.local_network: Codex's proxy with allow_local_binding) while stage 2 is
168
+ * switched on: refused outright, a step towards retiring it (decision D2; review R3, approved by
169
+ * the maintainer 2026-10-28). Codex's local-mode proxy has no authentication and reaches every port on
170
+ * loopback and the LAN, so a raw-network job could go through it to every denied port (§5.4, C2).
171
+ * `cfg`: the run's config. Null when local mode may run, as in 1.0.5 with stage 2 off.
172
+ *
173
+ * Refused at admission (caller.ts admitWork), before the turn of a job that already holds it
174
+ * (session.ts localNetworkConsent) and at the launch (codex.ts chooseNetwork). The other direction
175
+ * stays with localModeInterlock: a raw-network turn is refused while a local-mode turn that started
176
+ * before the switch still runs.
177
+ */
178
+ export function localModeRetired(cfg) {
179
+ if (stage2Enabled(process.env, cfg)) {
180
+ return "grant.local_network is not available while relay's stage 2 is switched on (targets.codex.stage2 in relay's config): " +
181
+ "Codex's local-network proxy has no authentication, so a raw-network job could reach every local service through it. " +
182
+ "Use grant.raw_network (raw network in relay's own sandbox, with your consent) or grant.network alone (internet over HTTP(S))";
183
+ }
184
+ const overlay = overlayInterlock("local");
185
+ if (overlay)
186
+ return `grant.local_network is not available: ${overlay}`;
187
+ return null;
188
+ }
189
+ /** Per-registry launch/overlay/executor interlock. Captures survive flag/grant changes. */
190
+ export function localModeInterlock(starting) {
191
+ return overlayInterlock(starting);
192
+ }
@@ -0,0 +1,152 @@
1
+ import { redact } from "./secrets.js";
2
+ /**
3
+ * Redact only a relay review tool call's own_review argument. Ordinary data, prose and code
4
+ * examples are not submissions. Diagnostic lines can wrap a call in a prefix or JSON quoting;
5
+ * final replies are inspected only when the whole reply is a structured echo of a call.
6
+ */
7
+ export function redactOwnReview(text, diagnostic = false) {
8
+ const hidden = (value) => /^\[own_review: \d+ chars, withheld\]$/.test(value) ? value : `[own_review: ${value.length} chars, withheld]`;
9
+ const object = (v) => !!v && typeof v === "object" && !Array.isArray(v);
10
+ function submission(value, depth = 0) {
11
+ if (depth > 64)
12
+ return false;
13
+ if (typeof value === "string") {
14
+ try {
15
+ return submission(JSON.parse(value), depth + 1);
16
+ }
17
+ catch {
18
+ return false;
19
+ }
20
+ }
21
+ return object(value) && Object.hasOwn(value, "review_id") && Object.hasOwn(value, "own_review");
22
+ }
23
+ const permissionRequest = (value) => object(value) && value.method === "session/request_permission"
24
+ && object(value.params) && object(value.params.toolCall) && submission(value.params.toolCall.rawInput);
25
+ const argumentField = (value) => !object(value) ? null
26
+ : value.type === "tool_use" && value.name === "mcp__relay__review" ? "input"
27
+ : (value.type === "mcpToolCall" || value.type === "mcp_tool_call") && value.server === "relay" && value.tool === "review" ? "arguments"
28
+ : (value.sessionUpdate === "tool_call" || value.sessionUpdate === "tool_call_update") && submission(value.rawInput) ? "rawInput"
29
+ : value.tool_name === "mcp__relay__review" && Object.hasOwn(value, "tool_input") ? "tool_input" : null;
30
+ function argument(value, depth) {
31
+ if (depth > 64)
32
+ return value;
33
+ if (typeof value === "string") {
34
+ try {
35
+ const parsed = JSON.parse(value), clean = argument(parsed, depth + 1);
36
+ return clean === parsed ? value : JSON.stringify(clean);
37
+ }
38
+ catch {
39
+ return value;
40
+ }
41
+ }
42
+ return object(value) && Object.hasOwn(value, "own_review") ? { ...value, own_review: hidden(typeof value.own_review === "string" ? value.own_review : JSON.stringify(value.own_review)) } : value;
43
+ }
44
+ function walk(value, depth, permissionParams = false) {
45
+ if (depth > 64)
46
+ return value;
47
+ if (typeof value === "string")
48
+ return structured(value, depth + 1, true) ?? value;
49
+ if (Array.isArray(value))
50
+ return value.map((v) => walk(v, depth + 1));
51
+ if (!object(value))
52
+ return value;
53
+ const field = argumentField(value);
54
+ return Object.fromEntries(Object.entries(value).map(([key, v]) => {
55
+ if (key === field)
56
+ return [key, argument(v, depth + 1)];
57
+ // ACP permission requests carry a toolCall without sessionUpdate or necessarily a title.
58
+ if (permissionParams && key === "toolCall" && object(v) && submission(v.rawInput)) {
59
+ return [key, { ...v, rawInput: argument(v.rawInput, depth + 1) }];
60
+ }
61
+ return [key, walk(v, depth + 1, value.method === "session/request_permission" && key === "params")];
62
+ }));
63
+ }
64
+ function structured(s, depth, echoOnly = false) {
65
+ if (depth > 64)
66
+ return s;
67
+ try {
68
+ const value = JSON.parse(s);
69
+ if ((!diagnostic || echoOnly) && typeof value !== "string" && !argumentField(value) && !permissionRequest(value))
70
+ return s;
71
+ const clean = walk(value, depth + 1);
72
+ return JSON.stringify(clean) === JSON.stringify(value) ? s : JSON.stringify(clean);
73
+ }
74
+ catch {
75
+ return null;
76
+ }
77
+ }
78
+ const clean = structured(text, 0);
79
+ if (clean !== null)
80
+ return clean;
81
+ if (!diagnostic)
82
+ return text;
83
+ // Recognise complete JSON values inside stderr/error prefixes, including debug-quoted JSON.
84
+ // No key-name regex or whole-message fallback: an unrecognised fragment stays ordinary text.
85
+ // Bound total diagnostic scanning work, including failed candidate scans. Malformed input
86
+ // with many unmatched openers must not repeatedly rescan an arbitrarily long suffix.
87
+ const budget = { left: 4 * 1024 * 1024 };
88
+ let out = "", from = 0;
89
+ for (let i = 0; i < text.length && budget.left-- > 0; i++) {
90
+ if (!'{["'.includes(text[i]))
91
+ continue;
92
+ const end = jsonEnd(text, i, budget);
93
+ if (end === null)
94
+ continue;
95
+ const candidate = text.slice(i, end), redacted = structured(candidate, 0);
96
+ if (redacted !== null && redacted !== candidate) {
97
+ out += text.slice(from, i) + redacted;
98
+ from = end;
99
+ }
100
+ // An unchanged string may just start with a stray diagnostic quote: still inspect any
101
+ // envelope inside it. Objects and arrays have already been walked in full.
102
+ if (redacted !== null && (redacted !== candidate || text[i] !== '"'))
103
+ i = end - 1;
104
+ }
105
+ return out + text.slice(from);
106
+ }
107
+ /** End of a complete JSON object, array or quoted string; braces inside strings are data. */
108
+ function jsonEnd(text, start, budget) {
109
+ let depth = 0, quoted = false, escaped = false;
110
+ for (let i = start; i < text.length && budget.left-- > 0; i++) {
111
+ const c = text[i];
112
+ if (quoted) {
113
+ if (escaped)
114
+ escaped = false;
115
+ else if (c === "\\")
116
+ escaped = true;
117
+ else if (c === '"') {
118
+ quoted = false;
119
+ if (!depth)
120
+ return i + 1;
121
+ }
122
+ }
123
+ else if (c === '"')
124
+ quoted = true;
125
+ else if (c === "{" || c === "[")
126
+ depth++;
127
+ else if ((c === "}" || c === "]") && --depth === 0)
128
+ return i + 1;
129
+ }
130
+ return null;
131
+ }
132
+ /**
133
+ * Keep error tails only after filtering complete stderr lines, before truncation loses context:
134
+ * a review's own_review arguments withheld, and the secrets relay knows the shape of cut
135
+ * (secrets.ts), so that the cut at `limit` can leave neither part of a secret nor a secret whose
136
+ * context (an Authorization header's name, say) fell before it.
137
+ */
138
+ export function diagnosticTail(limit = 2000) {
139
+ let tail = "", pending = "";
140
+ const clean = (line) => redact(redactOwnReview(line, true));
141
+ return {
142
+ write(text) {
143
+ pending += text;
144
+ let end;
145
+ while ((end = pending.indexOf("\n")) !== -1) {
146
+ tail = (tail + clean(pending.slice(0, end)) + "\n").slice(-limit);
147
+ pending = pending.slice(end + 1);
148
+ }
149
+ },
150
+ text: () => (tail + clean(pending)).slice(-limit),
151
+ };
152
+ }
@@ -0,0 +1,33 @@
1
+ export const REFUSAL_POLICY = "Relay neither retried nor rephrased the request, switched models, nor used a fallback target. A caller may explicitly resume the session.";
2
+ export const refusalNote = (r) => `Stopped by ${r.vendor === "openai" ? "OpenAI" : "Anthropic"}'s safety system. ${REFUSAL_POLICY}`;
3
+ /**
4
+ * Observed in j_9b6a622c's app-server log (cyberPolicy), and the 2026-10-19
5
+ * red-team exec records (message only). Keep the text match narrow: capacity, authentication,
6
+ * crashes and an agent merely discussing safety are not refusals. No other OpenAI codes or
7
+ * ACP refusal shapes have been verified; do not infer them from arbitrary error prose.
8
+ */
9
+ export function openaiRefusal(error) {
10
+ if (!error || typeof error !== "object")
11
+ return undefined;
12
+ const e = error;
13
+ if (typeof e.message !== "string")
14
+ return undefined;
15
+ if (e.codexErrorInfo === "cyberPolicy" || (e.codexErrorInfo === undefined && e.message.startsWith("This content was flagged for possible cybersecurity risk."))) {
16
+ return { vendor: "openai", message: e.message, code: "cyberPolicy" };
17
+ }
18
+ return undefined;
19
+ }
20
+ export const refusalError = (r) => `Stopped by ${r.vendor === "openai" ? "OpenAI" : "Anthropic"}'s safety system: ${r.message || `the vendor reported ${r.code} without a message`}`;
21
+ /** Source-derived from Claude Code 2.1.302; see test/fixtures/refusals/README.md. */
22
+ export function anthropicRefusal(event) {
23
+ const message = event.message;
24
+ if (event.type === "assistant" && message?.stop_reason === "refusal") {
25
+ const text = (Array.isArray(message.content) ? message.content : []).filter((b) => b?.type === "text" && typeof b.text === "string").map((b) => b.text).join("\n");
26
+ return { vendor: "anthropic", code: "refusal", message: text };
27
+ }
28
+ if (event.type === "result" && event.stop_reason === "refusal") {
29
+ const text = typeof event.result === "string" ? event.result : Array.isArray(event.errors) ? event.errors.filter((e) => typeof e === "string").join("; ") : "";
30
+ return { vendor: "anthropic", code: "refusal", message: text };
31
+ }
32
+ return undefined;
33
+ }