@jbkjr/relay 0.0.0-stage → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +386 -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 +1525 -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 +320 -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 +637 -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 +329 -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 +629 -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 +1502 -0
  186. package/docs/pinned-codex.md +321 -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,248 @@
1
+ // Relay's build identity: what a Codex qualification says it qualified. A pin used to record the git
2
+ // commit of the checkout it ran from, which an npm install does not have. It now records relay's
3
+ // version and one sha256 over the installed build that a qualification exercises (QUALIFIED_BUILD):
4
+ // relay's compiled code, the qualifier, and the files its stage-2 run loads. The same bytes give the
5
+ // same identity in a git checkout and in an npm install, hashed the same way every time
6
+ // (BUILD_SCHEME): every regular file under those paths, sorted by its relative path, each as
7
+ // `<path>\0<sha256 of its bytes>\n` after the line `relay-build-v2\n`. Names npm never packs
8
+ // (dotfiles, node_modules, *.orig and the like) are left out the same way on both sides; a symlink or
9
+ // a special file is refused rather than followed. package.json is not hashed: its version is the
10
+ // identity's own field, and npm may rewrite the rest of the file when it packs.
11
+ //
12
+ // relay-build-v2 adds npm-shrinkwrap.json, the lock npm installs relay's dependencies from (exact
13
+ // versions and sha512 integrities, which npm checks on install), so that an identity, and a pin that
14
+ // names one, also fixes relay's dependency tree. relay-build-v1, written by the builds before it, hashed the same paths
15
+ // without it. A v1 identity is still read as a record of what a local pin ran with
16
+ // (isBuildRecord), but it never equals a v2 one, so it cannot stand for a build of today's relay.
17
+ import crypto from "node:crypto";
18
+ import fs from "node:fs";
19
+ import path from "node:path";
20
+ export const BUILD_SCHEME = "relay-build-v2";
21
+ /** Schemes an earlier relay wrote into local pins and failure records; read as records, never matched to a build. */
22
+ export const LEGACY_BUILD_SCHEMES = Object.freeze(["relay-build-v1"]);
23
+ /**
24
+ * The stage-2 test files a qualification runs (scripts/codex-qualify/stage2-escalation-cases.mjs
25
+ * names them; codex-stage2.mjs runs them from relay's tree) and the modules they import. A test
26
+ * holds this list to that file and to their imports, so a new one cannot be missed.
27
+ */
28
+ export const STAGE2_TEST_FILES = Object.freeze([
29
+ "codex-permissions", "mediated-network", "mediated-proxy", "never-policy", "never-tools", "proxy",
30
+ "restart-overlay", "restart-overlay-live", "restart-overlay-profile", "stable-surfaces",
31
+ "stage2-escalation", "stage2-escalation-live", "stage2-escalation-qualification", "stage2-executor",
32
+ "stage2-grant", "stage2-native-live", "stage2-network-decisions", "stage2-probe", "stage2-standing-live",
33
+ "wp4-adapters", "wp4-interlock",
34
+ ].map((name) => `test/${name}.test.ts`));
35
+ /** What a qualification exercises, relative to relay's package root. A trailing slash is a directory, taken whole. */
36
+ export const QUALIFIED_BUILD = Object.freeze([
37
+ "dist/",
38
+ // The dependency lock (relay-build-v2): npm installs relay's dependencies from it, by integrity.
39
+ "npm-shrinkwrap.json",
40
+ "scripts/codex-qualify/",
41
+ ...STAGE2_TEST_FILES,
42
+ ...["helpers", "host-env", "keychain-decoy", "live-scope", "native-temp-alias", "process-home", "temp-root"].map((name) => `test/${name}.ts`),
43
+ "test/fixtures/",
44
+ "test/stubs/",
45
+ // The red-team runner's oracles, which test/stage2-escalation-qualification.test.ts imports.
46
+ "experiments/stage2-redteam/lib/oracles.mjs",
47
+ "experiments/stage2-redteam/lib/report.mjs",
48
+ ]);
49
+ /** What npm never puts in a package, whatever `files` says; left out of the hash on both sides alike. */
50
+ const unpacked = (name) => name.startsWith(".") || name.endsWith(".orig") ||
51
+ ["node_modules", "CVS", "npm-debug.log", "config.gypi", "package-lock.json"].includes(name);
52
+ const sha256 = (data) => crypto.createHash("sha256").update(data).digest("hex");
53
+ /** Every file of the qualified build under `root`, as sorted relative paths with "/" separators. Throws on a missing entry, a symlink or a special file. */
54
+ export function buildFiles(root) {
55
+ const out = [];
56
+ const visit = (rel) => {
57
+ const st = fs.lstatSync(path.join(root, rel));
58
+ if (st.isSymbolicLink())
59
+ throw new Error(`relay's build holds a symlink, which its identity does not follow: ${rel}`);
60
+ if (st.isFile()) {
61
+ out.push(rel);
62
+ return;
63
+ }
64
+ if (!st.isDirectory())
65
+ throw new Error(`relay's build holds a special file: ${rel}`);
66
+ for (const name of fs.readdirSync(path.join(root, rel)))
67
+ if (!unpacked(name))
68
+ visit(`${rel}/${name}`);
69
+ };
70
+ for (const entry of QUALIFIED_BUILD) {
71
+ const rel = entry.replace(/\/$/, "");
72
+ try {
73
+ visit(rel);
74
+ }
75
+ catch (err) {
76
+ if (err.code === "ENOENT")
77
+ throw new Error(`relay's build at ${root} is incomplete: ${rel} is missing (an npm install ships it; a git checkout must be built with npm run build)`);
78
+ throw err;
79
+ }
80
+ }
81
+ return out.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
82
+ }
83
+ /** The version in relay's package.json under `root`. */
84
+ export function packageVersion(root) {
85
+ const version = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"))?.version;
86
+ if (typeof version !== "string" || !/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(version))
87
+ throw new Error(`relay's package.json under ${root} has no valid version`);
88
+ return version;
89
+ }
90
+ /** Relay's build identity under `root` (relay's package root: a git checkout or an npm install). */
91
+ export function buildIdentity(root) {
92
+ const hash = crypto.createHash("sha256").update(`${BUILD_SCHEME}\n`);
93
+ for (const rel of buildFiles(root))
94
+ hash.update(`${rel}\0${sha256(fs.readFileSync(path.join(root, rel)))}\n`);
95
+ return { scheme: BUILD_SCHEME, version: packageVersion(root), sha256: hash.digest("hex") };
96
+ }
97
+ const wellFormed = (v) => !!v && typeof v === "object" && typeof v.scheme === "string" && typeof v.version === "string" &&
98
+ /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(v.version) && typeof v.sha256 === "string" && /^[a-f0-9]{64}$/.test(v.sha256);
99
+ /** Whether `value` is a well-formed build identity of the current scheme, the only kind a build can be matched to. */
100
+ export function isBuildIdentity(value) {
101
+ return wellFormed(value) && value.scheme === BUILD_SCHEME;
102
+ }
103
+ /** Whether `value` is a well-formed record of a build: the current scheme or a legacy one (LEGACY_BUILD_SCHEMES). */
104
+ export function isBuildRecord(value) {
105
+ return wellFormed(value) && (isBuildIdentity(value) || LEGACY_BUILD_SCHEMES.includes(value.scheme));
106
+ }
107
+ /** Whether two identities name the same build. A legacy record never equals a current one: the schemes hash different files. */
108
+ export const sameBuild = (a, b) => a.scheme === b.scheme && a.version === b.version && a.sha256 === b.sha256;
109
+ /** One line for people: "relay 1.2.0, build 0123456789abcdef", naming the scheme when it is a legacy one. */
110
+ export const describeBuild = (b) => `relay ${b.version}, build ${b.sha256.slice(0, 16)}${b.scheme === BUILD_SCHEME ? "" : ` (${b.scheme})`}`;
111
+ /**
112
+ * relay's lock (npm-shrinkwrap.json) under `root`, checked to be the lock of this package and version.
113
+ * Throws when it is missing, or names another package or version than package.json.
114
+ */
115
+ function readLock(root) {
116
+ const pkg = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"));
117
+ const lock = JSON.parse(fs.readFileSync(path.join(root, "npm-shrinkwrap.json"), "utf8"));
118
+ if (lock?.name !== pkg.name || lock?.version !== pkg.version || lock?.packages?.[""]?.version !== pkg.version) {
119
+ throw new Error(`relay's npm-shrinkwrap.json under ${root} is not the lock of ${pkg.name} ${pkg.version}: regenerate it with npm install`);
120
+ }
121
+ return lock;
122
+ }
123
+ /** The lock's entry a package at lock key `from` gets for `name`: the nearest node_modules/<name> at or above it, as npm lays the tree out. */
124
+ function lockResolve(packages, from, name) {
125
+ for (let at = from;; at = at.includes("/node_modules/") ? at.slice(0, at.lastIndexOf("/node_modules/")) : "") {
126
+ const key = `${at ? `${at}/` : ""}node_modules/${name}`;
127
+ if (packages[key])
128
+ return key;
129
+ if (!at)
130
+ return null;
131
+ }
132
+ }
133
+ /**
134
+ * Whether the dependencies relay would load from `root` are the versions its lock names: null when
135
+ * they are, else what differs. The build identity covers the lock (npm-shrinkwrap.json), not
136
+ * node_modules, so this is what ties the dependencies that run to it. It walks relay's runtime
137
+ * dependency graph from package.json's dependencies: each dependency is found on disk as Node
138
+ * finds it (from the real directory of the package that requires it, upward), and in the lock as
139
+ * npm lays it out (the nearest entry at or above the requiring one), and the two versions must
140
+ * agree, transitive dependencies included. So a hoisted project install, or a tree of links to the
141
+ * real packages, is judged by what Node would load. Dev-only entries are never reached; an optional
142
+ * or peer dependency that is absent is fine. package.json also names each direct dependency at
143
+ * exactly its locked version, since npm reads a dependency's shrinkwrap only for a registry install
144
+ * of a package that ships one.
145
+ */
146
+ export function dependencyDrift(root) {
147
+ let packages;
148
+ try {
149
+ packages = readLock(root).packages;
150
+ }
151
+ catch (err) {
152
+ return err.message;
153
+ }
154
+ const pkg = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"));
155
+ const off = [];
156
+ const seen = new Set();
157
+ const queue = [{
158
+ key: "", dir: fs.realpathSync(root),
159
+ deps: [...Object.keys(pkg.dependencies ?? {}).map((n) => [n, "required"]), ...Object.keys(pkg.optionalDependencies ?? {}).map((n) => [n, "optional"])],
160
+ }];
161
+ while (queue.length) {
162
+ const { key, dir, deps } = queue.shift();
163
+ for (const [name, kind] of deps) {
164
+ const lockKey = lockResolve(packages, key, name);
165
+ const found = dependencyDir(dir, name);
166
+ const label = (lockKey ?? `node_modules/${name}`).slice("node_modules/".length);
167
+ if (!found) {
168
+ if (kind === "required")
169
+ off.push(`${label} is not installed, and the lock names ${lockKey ? packages[lockKey].version : "no version"}`);
170
+ continue;
171
+ }
172
+ if (!lockKey) {
173
+ off.push(`${label} is installed at ${found}, and the lock has no entry for it`);
174
+ continue;
175
+ }
176
+ let installed;
177
+ try {
178
+ installed = JSON.parse(fs.readFileSync(path.join(found, "package.json"), "utf8"))?.version;
179
+ }
180
+ catch {
181
+ installed = undefined;
182
+ }
183
+ if (installed !== packages[lockKey].version) {
184
+ off.push(`${label} is ${installed ?? "unreadable"} at ${found}, and the lock names ${packages[lockKey].version}`);
185
+ continue;
186
+ }
187
+ if (seen.has(`${lockKey}\0${found}`))
188
+ continue;
189
+ seen.add(`${lockKey}\0${found}`);
190
+ const e = packages[lockKey];
191
+ queue.push({ key: lockKey, dir: found, deps: [
192
+ ...Object.keys(e.dependencies ?? {}).map((n) => [n, "required"]),
193
+ ...Object.keys(e.optionalDependencies ?? {}).map((n) => [n, "optional"]),
194
+ ...Object.keys(e.peerDependencies ?? {}).map((n) => [n, "peer"]),
195
+ ] });
196
+ }
197
+ }
198
+ return off.length ? `relay's dependencies are not the ones its lock (npm-shrinkwrap.json) names: ${off.join("; ")}` : null;
199
+ }
200
+ /**
201
+ * Where node would find `name` from relay's package root: its own node_modules, or a parent's when
202
+ * npm hoisted it (a project install). Null when it is not installed.
203
+ */
204
+ function dependencyDir(root, name) {
205
+ for (let dir = fs.realpathSync(root);; dir = path.dirname(dir)) {
206
+ const candidate = path.join(dir, "node_modules", name);
207
+ if (fs.existsSync(path.join(candidate, "package.json")))
208
+ return fs.realpathSync(candidate);
209
+ if (path.dirname(dir) === dir)
210
+ return null;
211
+ }
212
+ }
213
+ /**
214
+ * Copies relay's qualified build from `root` to `dest` (which must not exist), with package.json
215
+ * and a node_modules of links to the dependencies `root` resolves, and returns the copy's identity,
216
+ * which must equal the source's. Node strips TypeScript types only outside node_modules, so a
217
+ * qualification of an npm install runs its stage-2 tests from such a copy; a checkout's runs from
218
+ * one too, so that both run the same way and an edit to the checkout cannot reach a running qualification.
219
+ */
220
+ export function copyBuild(root, dest) {
221
+ const source = buildIdentity(root);
222
+ fs.mkdirSync(dest, { mode: 0o700 });
223
+ for (const rel of [...buildFiles(root), "package.json"]) {
224
+ const to = path.join(dest, rel);
225
+ fs.mkdirSync(path.dirname(to), { recursive: true, mode: 0o700 });
226
+ fs.copyFileSync(path.join(root, rel), to, fs.constants.COPYFILE_EXCL);
227
+ fs.chmodSync(to, fs.statSync(path.join(root, rel)).mode & 0o111 ? 0o755 : 0o644);
228
+ }
229
+ const deps = Object.keys(JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8")).dependencies ?? {});
230
+ for (const name of deps)
231
+ if (!dependencyDir(root, name))
232
+ throw new Error(`relay's dependency ${name} is not installed beside ${root}`);
233
+ // The identity fixes the lock, not node_modules: a qualification runs only with the dependency
234
+ // versions the lock names.
235
+ const drift = dependencyDrift(root);
236
+ if (drift)
237
+ throw new Error(`${drift}: run npm ci`);
238
+ for (const name of deps) {
239
+ const found = dependencyDir(root, name);
240
+ const link = path.join(dest, "node_modules", name);
241
+ fs.mkdirSync(path.dirname(link), { recursive: true, mode: 0o700 });
242
+ fs.symlinkSync(found, link);
243
+ }
244
+ const copy = buildIdentity(dest);
245
+ if (!sameBuild(copy, source))
246
+ throw new Error(`the copy of relay's build at ${dest} does not match its source ${root}`);
247
+ return copy;
248
+ }