@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,1135 @@
1
+ // Retention (keep_records_days) and the sweep of relay's own temporary directories: what relay
2
+ // removes of its own accord. Records go only when the person has set how long to keep them, and
3
+ // only for runs nothing can use any more; a temporary directory only once whatever made it is
4
+ // provably gone.
5
+ import fs from "node:fs";
6
+ import { launchReservationProblem } from "./restart-overlay.js";
7
+ import net from "node:net";
8
+ import os from "node:os";
9
+ import path from "node:path";
10
+ import { DatabaseSync } from "node:sqlite";
11
+ import { relaySocketsDir } from "../confine/sockets.js";
12
+ import { nativeTempRoot, privateStateRoot } from "../confine/private-root.js";
13
+ import { harnessHome, relayHome, runDir, worktreesHome } from "./config.js";
14
+ import { adoptionTickets, ownerOpen } from "./adoption.js";
15
+ import { CONTEXT_DIR } from "./context.js";
16
+ import { busy, db, getNode, getRun, getTurn, reap, runNodes, tx, workerLive } from "./db.js";
17
+ import { git } from "./git.js";
18
+ import { groupAlive, pidAlive, procStart, sameProcess } from "./procs.js";
19
+ import { sessionParams } from "./params.js";
20
+ import { dropSkillAssets, nodeHasLiveExecutor, sweepOrphanedExecutors } from "./stage2-executors.js";
21
+ const DAY = 86_400_000;
22
+ const HOUR = 3_600_000;
23
+ /** The prefix of a forgotten run's directories, moved aside in the transaction that forgets it and removed after. */
24
+ const ASIDE = ".forgotten-";
25
+ /** A run id as relay makes one: only such a name is joined to a path of relay's, so nothing outside its own directories is ever moved. */
26
+ const RUN_ID = /^run_[0-9a-f]+$/;
27
+ /** A plain directory of this user's: not a link to one, nor someone else's. */
28
+ const mine = (st) => st.isDirectory() && !st.isSymbolicLink() && (typeof process.getuid !== "function" || st.uid === process.getuid());
29
+ /** When anything last happened in a run: it, or a node, turn, request or event of it, was made, started, decided or ended. */
30
+ function lastActive(runId) {
31
+ const row = db().prepare(`SELECT MAX(t) AS t FROM (
32
+ SELECT created_at AS t FROM runs WHERE id = ?1
33
+ UNION ALL SELECT MAX(created_at, COALESCE(started_at, 0), COALESCE(ended_at, 0)) FROM nodes WHERE run_id = ?1
34
+ UNION ALL SELECT MAX(created_at, COALESCE(started_at, 0), COALESCE(ended_at, 0)) FROM turns WHERE node_id IN (SELECT id FROM nodes WHERE run_id = ?1)
35
+ UNION ALL SELECT MAX(created_at, COALESCE(decided_at, 0)) FROM requests WHERE run_id = ?1
36
+ UNION ALL SELECT MAX(created_at, COALESCE(noticed_at, 0), COALESCE(seen_at, 0)) FROM events WHERE run_id = ?1)`).get(runId);
37
+ return row.t ?? 0;
38
+ }
39
+ /**
40
+ * Why a run's records must stay because of what is on disk: a job's worktree, or its branch, still
41
+ * there, or a branch relay cannot look for. A branch is gone only when git looked and found none
42
+ * (`rev-parse -q --verify` exits 1). An unavailable repository (on a volume not mounted, say),
43
+ * even when its parent exists, or a git that fails or does not finish, says nothing either way,
44
+ * and the run is kept.
45
+ */
46
+ function heldOnDisk(nodes) {
47
+ for (const n of nodes) {
48
+ if (n.worktree && fs.existsSync(n.worktree))
49
+ return `${n.id} still has its worktree (${n.worktree})`;
50
+ // Only a branch is on disk to protect: a read job in a repository since removed holds nothing.
51
+ if (!n.branch || !n.repo)
52
+ continue;
53
+ if (!fs.existsSync(n.repo)) {
54
+ return `relay cannot look for ${n.id}'s branch ${n.branch}: its repository ${n.repo} is not there (a volume not mounted?), and its absence does not establish deletion, so the run is kept until relay can look`;
55
+ }
56
+ const r = git(n.repo, ["rev-parse", "-q", "--verify", `refs/heads/${n.branch}`]);
57
+ if (r.ok)
58
+ return `${n.id}'s branch ${n.branch} is still in ${n.repo} (relay clean deletes it once merged)`;
59
+ if (r.code !== 1)
60
+ return `relay could not check whether ${n.id}'s branch ${n.branch} is still in ${n.repo} (${r.err || `git exited with ${r.code}`}), so the run is kept until it can`;
61
+ }
62
+ return null;
63
+ }
64
+ /**
65
+ * Why a run's records stay because relay quarantined login-store entries of it after a turn
66
+ * (backends/acp.ts, quarantine/<run>/<node>/<turn>/): its records say how they came to be there,
67
+ * so they are kept until the person has looked and removed that directory. One that relay cannot
68
+ * look for keeps the run too.
69
+ */
70
+ function quarantined(id) {
71
+ const dir = path.join(relayHome(), "quarantine", id);
72
+ try {
73
+ if (!fs.lstatSync(dir, { throwIfNoEntry: false }))
74
+ return null;
75
+ }
76
+ catch (err) {
77
+ return `relay could not look for login-store entries of it that it quarantined in ${dir} (${err.message}), so the run is kept until it can`;
78
+ }
79
+ return `relay quarantined login-store entries of it in ${dir}, which its records explain; delete that directory once you have looked at it`;
80
+ }
81
+ /**
82
+ * Why a run is still in use, by the registry: its session, anything in it that is not over, or a
83
+ * transfer of it to another session waiting to be taken up. Its session is open as adopt counts
84
+ * it (adoption.ts ownerOpen): while its owning connection's process runs, or may (one whose start
85
+ * relay cannot read, or a legacy owner's that has none).
86
+ */
87
+ function stillActive(run, nodes) {
88
+ const reserved = launchReservationProblem(run.id);
89
+ if (reserved)
90
+ return reserved;
91
+ if (ownerOpen(run))
92
+ return "its session is still open";
93
+ if (adoptionTickets(run.id).waiting)
94
+ return "a transfer of it to another session, which relay adopt authorized, is waiting to be taken up";
95
+ for (const n of nodes) {
96
+ if (busy(n))
97
+ return `${n.id} is ${n.status}`;
98
+ if (n.status === "open")
99
+ return `${n.id} is an open ${n.kind}, waiting for its next message`;
100
+ if (workerLive(n))
101
+ return `the worker of ${n.id} is still running`;
102
+ if (groupAlive(n.agent_pgid))
103
+ return `an agent process of ${n.id} (group ${n.agent_pgid}) may still be running`;
104
+ }
105
+ const procs = db().prepare("SELECT pid, start, kind, node_id FROM relay_procs WHERE node_id IN (SELECT id FROM nodes WHERE run_id = ?)").all(run.id);
106
+ const live = procs.find((p) => sameProcess(p.pid, p.start));
107
+ if (live)
108
+ return `relay's ${live.kind} process ${live.pid} for ${live.node_id} is still running`;
109
+ // Keep the mapping if removal failed, so a later sweep can still prove whose directory it is.
110
+ const temps = db().prepare("SELECT tmp_dir FROM turns WHERE tmp_dir IS NOT NULL AND node_id IN (SELECT id FROM nodes WHERE run_id = ?)").all(run.id);
111
+ for (const { tmp_dir } of temps)
112
+ if (fs.lstatSync(tmp_dir, { throwIfNoEntry: false }))
113
+ return `a turn's private temporary directory still needs sweeping (${tmp_dir})`;
114
+ if (db().prepare("SELECT 1 FROM requests WHERE run_id = ? AND status = 'pending' LIMIT 1").get(run.id))
115
+ return "it has an approval request waiting";
116
+ return null;
117
+ }
118
+ /** Removes every row of a run from the registry. Inside the transaction that found it unused. */
119
+ function deleteRun(id) {
120
+ const inRun = "(SELECT id FROM nodes WHERE run_id = ?)";
121
+ for (const sql of [
122
+ `DELETE FROM injections WHERE discussion_id IN ${inRun}`,
123
+ `DELETE FROM calls WHERE node_id IN ${inRun}`,
124
+ "DELETE FROM elicitations WHERE request_id IN (SELECT id FROM requests WHERE run_id = ?)",
125
+ "DELETE FROM requests WHERE run_id = ?",
126
+ "DELETE FROM events WHERE run_id = ?",
127
+ "DELETE FROM slots WHERE run_id = ?",
128
+ "DELETE FROM launch_leaders WHERE run_id = ?",
129
+ `DELETE FROM turns WHERE node_id IN ${inRun}`,
130
+ `DELETE FROM relay_procs WHERE node_id IN ${inRun}`,
131
+ "DELETE FROM nodes WHERE run_id = ?",
132
+ "DELETE FROM runs WHERE id = ?",
133
+ ])
134
+ db().prepare(sql).run(id);
135
+ }
136
+ /** A run's directories that hold its records and its harnesses' state, each with where it is moved aside to. */
137
+ function runDirs(id) {
138
+ return [runDir(id), path.join(harnessHome(), id)].map((dir) => ({ dir, aside: path.join(path.dirname(dir), `${ASIDE}${id}`) }));
139
+ }
140
+ /** Moves a run's directories aside; returns those it moved. A move that fails puts back the others and throws. */
141
+ function moveAside(dirs) {
142
+ const moved = [];
143
+ for (const d of dirs) {
144
+ try {
145
+ fs.renameSync(d.dir, d.aside);
146
+ moved.push(d);
147
+ }
148
+ catch (err) {
149
+ if (err.code === "ENOENT")
150
+ continue;
151
+ putBack(moved);
152
+ throw new Error(`relay could not move ${d.dir} aside to remove it (${err.message})`);
153
+ }
154
+ }
155
+ return moved;
156
+ }
157
+ function putBack(moved) {
158
+ for (const d of moved) {
159
+ try {
160
+ fs.renameSync(d.aside, d.dir);
161
+ }
162
+ catch {
163
+ // Left aside: a run whose rows are kept still lists, and its directory is named in the error.
164
+ }
165
+ }
166
+ }
167
+ /**
168
+ * Removes a forgotten run's directories, already moved aside, its transfer authorizations and,
169
+ * unless `treesDir` is false, its directory of worktrees, once empty. Returns what was left.
170
+ */
171
+ function removeFiles(id, moved, treesDir = true) {
172
+ const left = [];
173
+ try {
174
+ for (const f of adoptionTickets(id).files) {
175
+ try {
176
+ fs.rmSync(f, { force: true });
177
+ }
178
+ catch (err) {
179
+ left.push(`${f} (${err.message})`);
180
+ }
181
+ }
182
+ }
183
+ catch (err) {
184
+ left.push(err.message);
185
+ }
186
+ for (const d of moved) {
187
+ try {
188
+ fs.rmSync(d.aside, { recursive: true, force: true });
189
+ }
190
+ catch (err) {
191
+ left.push(`${d.aside} (${err.message}; relay clean tries again)`);
192
+ }
193
+ }
194
+ try {
195
+ if (treesDir)
196
+ fs.rmdirSync(path.join(worktreesHome(), id));
197
+ }
198
+ catch (err) {
199
+ const code = err.code;
200
+ if (code !== "ENOENT")
201
+ left.push(`${path.join(worktreesHome(), id)} (${code === "ENOTEMPTY" ? "not empty" : err.message})`);
202
+ }
203
+ return left;
204
+ }
205
+ /**
206
+ * Forgets the runs (all, or `runIds`) in which nothing has happened for more than `days`: their
207
+ * rows in the registry, their records directory, their harnesses' state. A run still in use is
208
+ * never forgotten (stillActive), nor one that a job's worktree or branch still needs (heldOnDisk), nor
209
+ * one with login-store entries relay quarantined (quarantined).
210
+ * What the registry says is checked again in the transaction that removes the rows, so a run taken
211
+ * up again at that moment (a resume, say) either is seen to be in use or finds its items gone. The
212
+ * run's directories are moved aside in that transaction, and put back if it does not commit, so
213
+ * that none is removed for a run the registry still lists.
214
+ */
215
+ export function forgetOldRuns(days, o = {}) {
216
+ const cutoff = (o.at ?? Date.now()) - days * DAY;
217
+ const ids = o.runIds ?? db().prepare("SELECT id FROM runs ORDER BY created_at").all().map((r) => r.id);
218
+ const out = { forgotten: [], kept: [], left: [] };
219
+ const rebuild = { problem: null };
220
+ overwriting(() => forgetEach(ids, cutoff, out, () => { rebuild.problem = scheduleRebuild() ?? rebuild.problem; }));
221
+ if (rebuild.problem && out.forgotten.length)
222
+ out.left.push(rebuild.problem);
223
+ rebuildRegistry(out.left);
224
+ return out;
225
+ }
226
+ /**
227
+ * Runs `fn`, which forgets runs, with the rows it deletes overwritten as they are deleted (the
228
+ * registry's setting is put back after). Deleted rows are not left readable in the registry's
229
+ * file: they are overwritten so, and when no run is active the file is rebuilt (rebuildRegistry,
230
+ * VACUUM), which also drops the copies that earlier updates of their rows left in its free space,
231
+ * and its write-ahead log, which holds them too, is emptied.
232
+ */
233
+ function overwriting(fn) {
234
+ const before = db().prepare("PRAGMA secure_delete").get().secure_delete;
235
+ db().exec("PRAGMA secure_delete = ON");
236
+ try {
237
+ return fn();
238
+ }
239
+ finally {
240
+ db().exec(`PRAGMA secure_delete = ${Number(before)}`);
241
+ }
242
+ }
243
+ const rebuildMarker = () => path.join(relayHome(), "registry-rebuild");
244
+ /**
245
+ * Records that the registry's file is to be rebuilt (rebuildRegistry), before a run's rows are
246
+ * deleted, so that a relay stopped right after deleting them leaves the rebuild to a later clean or
247
+ * housekeeping. Never taken back when nothing was deleted after all: another relay process may have
248
+ * deleted rows meanwhile and relied on it; a rebuild with nothing to drop costs only its time.
249
+ * Returns what to say if it could not be recorded, or null.
250
+ */
251
+ function scheduleRebuild() {
252
+ try {
253
+ fs.writeFileSync(rebuildMarker(), "pending\n", { mode: 0o600 });
254
+ return null;
255
+ }
256
+ catch (err) {
257
+ return `the deferred registry rebuild could not be scheduled (${err.message}); forgotten records may remain readable in relay.db and relay.db-wal`;
258
+ }
259
+ }
260
+ /**
261
+ * Rebuilds the registry's file and empties its write-ahead log while a rebuild is pending
262
+ * (scheduleRebuild); what could not be done goes in `left`.
263
+ */
264
+ function rebuildRegistry(left) {
265
+ const rebuild = rebuildMarker();
266
+ if (!fs.existsSync(rebuild))
267
+ return;
268
+ // VACUUM takes a write lock for its entire rebuild. Defer it while any session or work is
269
+ // live, including runs unrelated to those forgotten. The marker makes a later clean or
270
+ // housekeeping retry even when no further runs are forgotten.
271
+ const active = db().prepare("SELECT * FROM runs").all()
272
+ .some((run) => stillActive(run, runNodes(run.id)) !== null);
273
+ if (active) {
274
+ left.push("the registry rebuild and log truncation are deferred while runs are active; forgotten records may remain readable in relay.db and relay.db-wal until relay clean or housekeeping next finds all runs idle");
275
+ return;
276
+ }
277
+ try {
278
+ db().exec("VACUUM");
279
+ // Blocked (busy) while another relay process reads or writes the registry for longer than
280
+ // the busy timeout: the pages of the file as it was then stay in it, and the log is not emptied.
281
+ const cp = db().prepare("PRAGMA wal_checkpoint(TRUNCATE)").get();
282
+ if (cp.busy !== 0 || cp.checkpointed < cp.log) {
283
+ left.push("the registry's write-ahead log could not be emptied (another relay process was using the registry), so what the forgotten runs held may stay readable in relay.db and relay.db-wal until later writes to the registry replace it, or the next time runs are forgotten");
284
+ }
285
+ else
286
+ fs.rmSync(rebuild, { force: true });
287
+ }
288
+ catch (err) {
289
+ left.push(`the registry's file was not rebuilt, so what the forgotten runs held may stay readable in its free space until the next time (${err.message})`);
290
+ }
291
+ }
292
+ function forgetEach(ids, cutoff, out, beforeDelete) {
293
+ for (const id of ids) {
294
+ if (!getRun(id) || lastActive(id) > cutoff)
295
+ continue;
296
+ if (!RUN_ID.test(id)) {
297
+ out.kept.push(`${id}: it is not a run id relay makes, so relay leaves its records alone`);
298
+ continue;
299
+ }
300
+ const disk = quarantined(id) ?? heldOnDisk(runNodes(id));
301
+ if (disk) {
302
+ out.kept.push(`${id}: ${disk}`);
303
+ continue;
304
+ }
305
+ let moved = [];
306
+ let why;
307
+ beforeDelete();
308
+ try {
309
+ why = tx(() => {
310
+ const run = getRun(id);
311
+ if (!run || lastActive(id) > cutoff)
312
+ return "skip";
313
+ const active = stillActive(run, runNodes(id));
314
+ if (active)
315
+ return active;
316
+ moved = moveAside(runDirs(id));
317
+ deleteRun(id);
318
+ return null;
319
+ });
320
+ }
321
+ catch (err) {
322
+ if (moved.length && getRun(id))
323
+ putBack(moved);
324
+ out.kept.push(`${id}: ${err.message}`);
325
+ continue;
326
+ }
327
+ if (why === "skip")
328
+ continue;
329
+ if (why) {
330
+ out.kept.push(`${id}: ${why}`);
331
+ continue;
332
+ }
333
+ out.forgotten.push(id);
334
+ const left = removeFiles(id, moved);
335
+ if (left.length)
336
+ out.left.push(`${id}: its rows are gone from the registry, but not all its files: ${left.join("; ")}`);
337
+ }
338
+ }
339
+ /** What relay clean says about records: the policy, and what it did under it. */
340
+ export function recordsReport(cfg, runIds) {
341
+ if (cfg.keep_records_days === null)
342
+ return { records_note: "kept: keep_records_days is not set, so relay keeps every run's prompts, logs and transcripts until you delete them" };
343
+ const r = forgetOldRuns(cfg.keep_records_days, { runIds });
344
+ return {
345
+ records_note: `forgotten after ${cfg.keep_records_days} days without activity (keep_records_days)`,
346
+ records_forgotten: r.forgotten, records_kept: r.kept, ...(r.left.length ? { records_left: r.left } : {}),
347
+ };
348
+ }
349
+ /**
350
+ * Why relay clean --forget does not take a run, going by its repositories, or null when every
351
+ * repository a job of it has a branch or worktree in is not there. One that is there is relay
352
+ * clean's to deal with; one relay cannot look at says nothing either way. Looked at before the
353
+ * transaction that removes the run's rows, never in it: a repository on a stale network mount can
354
+ * take longer to answer than another relay process waits for the registry.
355
+ */
356
+ function reposGone(id, nodes) {
357
+ const repos = [...new Set(nodes.flatMap((n) => (n.repo && (n.branch || n.worktree) ? [n.repo] : [])))];
358
+ if (!repos.length)
359
+ return "no job of it has a branch or a worktree in a repository, so no missing repository keeps it (keep_records_days forgets it as it does any other run)";
360
+ for (const repo of repos) {
361
+ let there;
362
+ try {
363
+ there = fs.statSync(repo, { throwIfNoEntry: false }) !== undefined;
364
+ }
365
+ catch (err) {
366
+ if (err.code !== "ENOTDIR")
367
+ return `relay cannot tell whether its repository ${repo} is there (${err.message})`;
368
+ there = false;
369
+ }
370
+ if (there)
371
+ return `its repository ${repo} is there, so relay clean ${id} is what applies: it removes the run's finished worktrees and its relay/* branches already merged, and keep_records_days forgets the run once nothing holds it`;
372
+ }
373
+ return null;
374
+ }
375
+ /**
376
+ * What reap would settle in a run, for a preview, which must not settle it (reap records a job
377
+ * lost, fails its turn, expires requests and stops agents left without a supervisor): a job whose
378
+ * worker has died, or an approval request that no job can take up any more. Either keeps the run
379
+ * in use until it is settled.
380
+ */
381
+ function unsettled(id, nodes) {
382
+ const next = "run relay tree or relay clean first, or run this again with --write";
383
+ for (const n of nodes)
384
+ if (busy(n) && n.worker_pid !== null && !workerLive(n))
385
+ return `${n.id}'s worker is gone; ${next}`;
386
+ const r = db().prepare(`SELECT id, node_id FROM requests WHERE run_id = ?1 AND status = 'pending'
387
+ AND node_id IN (SELECT id FROM nodes WHERE run_id = ?1 AND status NOT IN ('queued', 'running')) LIMIT 1`).get(id);
388
+ return r ? `its approval request ${r.id} waits on ${r.node_id}, which is no longer running; ${next}` : null;
389
+ }
390
+ const pin = (p, st) => ({ path: p, dev: st.dev, ino: st.ino });
391
+ /** Throws unless `p` is still the plain directory of this user's that relay found there. */
392
+ function still(p) {
393
+ const st = fs.lstatSync(p.path, { throwIfNoEntry: false });
394
+ if (!st || !mine(st) || st.dev !== p.dev || st.ino !== p.ino)
395
+ throw new Error(`${p.path} changed after relay looked at it`);
396
+ }
397
+ /**
398
+ * What relay made for a run in its worktrees home, each pinned by identity. The worktrees home
399
+ * itself must be a plain directory of this user's: through a link, what relay would remove is not
400
+ * in its own directory, so it throws, as it does for what relay cannot look at. What relay did not
401
+ * make, a link among them, and a job's worktree recorded outside the worktrees home (made while that
402
+ * was elsewhere) are left alone, each said with why.
403
+ */
404
+ function runTrees(id, nodes) {
405
+ const home = worktreesHome();
406
+ const dir = path.join(home, id);
407
+ const look = (p) => {
408
+ try {
409
+ return fs.lstatSync(p, { throwIfNoEntry: false });
410
+ }
411
+ catch (err) {
412
+ throw new Error(`relay could not look at ${p} (${err.message})`);
413
+ }
414
+ };
415
+ const t = { root: null, dir: null, made: [], others: false, alone: [] };
416
+ for (const n of nodes) {
417
+ if (n.worktree && path.resolve(n.worktree) !== path.join(dir, n.id) && look(n.worktree)) {
418
+ t.alone.push(`${n.worktree}: ${n.id}'s worktree, outside relay's worktrees home ${home}, so relay does not remove it`);
419
+ }
420
+ }
421
+ const root = look(home);
422
+ if (!root)
423
+ return t;
424
+ if (!mine(root))
425
+ throw new Error(`relay's worktrees home ${home} is not a plain directory of this user's (a link, say), so relay will not look for the run's worktrees in it, nor remove anything there`);
426
+ t.root = pin(home, root);
427
+ const st = look(dir);
428
+ if (!st)
429
+ return t;
430
+ if (!mine(st)) {
431
+ t.alone.push(`${dir}: not a plain directory of this user's, as relay makes a run's directory of worktrees, so relay leaves it as it is`);
432
+ return t;
433
+ }
434
+ t.dir = pin(dir, st);
435
+ let names;
436
+ try {
437
+ names = fs.readdirSync(dir).sort();
438
+ }
439
+ catch (err) {
440
+ throw new Error(`relay could not read ${dir} (${err.message})`);
441
+ }
442
+ const jobs = new Set(nodes.map((n) => n.id));
443
+ for (const name of names) {
444
+ const p = path.join(dir, name);
445
+ const e = look(p);
446
+ if (!e)
447
+ continue;
448
+ if (jobs.has(name) || CONTEXT_BUILD.test(name)) {
449
+ if (mine(e)) {
450
+ t.made.push(pin(p, e));
451
+ continue;
452
+ }
453
+ t.alone.push(`${p}: not a plain directory of this user's (a link, say), so relay leaves it`);
454
+ }
455
+ else
456
+ t.alone.push(`${p}: not a name relay gives what it makes for a job, so relay leaves it`);
457
+ t.others = true;
458
+ }
459
+ return t;
460
+ }
461
+ /**
462
+ * Moves what relay made for a run in its worktrees home aside, into <worktrees home>/.forgotten-<run>/,
463
+ * in the transaction that removes the run's rows: once they commit, a relay stopped before removing
464
+ * it leaves it for sweepTemps, which puts it back should the registry still list the run. Each is
465
+ * moved only while it, the run's directory and the worktrees home are the directories relay looked
466
+ * at. Returns the directory it was moved to, or null for nothing to move; throws, having put back
467
+ * what it moved, when it cannot.
468
+ */
469
+ function moveTreesAside(id, t) {
470
+ if (!t.made.length || !t.root || !t.dir)
471
+ return null;
472
+ const aside = path.join(t.root.path, `${ASIDE}${id}`);
473
+ const moved = [];
474
+ let made = false;
475
+ try {
476
+ still(t.root);
477
+ still(t.dir);
478
+ fs.mkdirSync(aside, { mode: 0o700 });
479
+ made = true;
480
+ for (const m of t.made) {
481
+ still(m);
482
+ fs.renameSync(m.path, path.join(aside, path.basename(m.path)));
483
+ moved.push(m);
484
+ }
485
+ return pin(aside, fs.lstatSync(aside));
486
+ }
487
+ catch (err) {
488
+ for (const m of moved) {
489
+ try {
490
+ fs.renameSync(path.join(aside, path.basename(m.path)), m.path);
491
+ }
492
+ catch {
493
+ // Left aside: sweepTemps puts it back while the registry lists the run.
494
+ }
495
+ }
496
+ try {
497
+ if (made)
498
+ fs.rmdirSync(aside);
499
+ }
500
+ catch {
501
+ // Not empty: as above.
502
+ }
503
+ throw new Error(`relay could not move the run's worktrees aside to remove them (${err.message})`);
504
+ }
505
+ }
506
+ /**
507
+ * Puts what moveTreesAside moved to `aside` back into the run's directory in the worktrees home
508
+ * `root`, making that directory again if it is gone. Throws what it cannot put back, leaving the
509
+ * rest aside.
510
+ */
511
+ function putTreesBack(root, id, aside) {
512
+ const dir = path.join(root, id);
513
+ const st = fs.lstatSync(dir, { throwIfNoEntry: false });
514
+ if (!st)
515
+ fs.mkdirSync(dir);
516
+ else if (!mine(st))
517
+ throw new Error(`${dir} is not a plain directory of this user's`);
518
+ for (const name of fs.readdirSync(aside)) {
519
+ const to = path.join(dir, name);
520
+ if (fs.lstatSync(to, { throwIfNoEntry: false }))
521
+ throw new Error(`${to} is already there`);
522
+ fs.renameSync(path.join(aside, name), to);
523
+ }
524
+ fs.rmdirSync(aside);
525
+ }
526
+ /**
527
+ * Removes a forgotten run's worktrees, moved aside, and its directory in the worktrees home once
528
+ * empty, each only while the worktrees home and it are the directories relay looked at. A link
529
+ * inside is removed as a link (rmSync goes by lstat), never what it leads to. Returns what was left.
530
+ */
531
+ function removeTreesAside(t, aside) {
532
+ const left = [];
533
+ const attempt = (p, remove, again) => {
534
+ try {
535
+ still(t.root);
536
+ still(p);
537
+ remove();
538
+ }
539
+ catch (err) {
540
+ const code = err.code;
541
+ if (code !== "ENOENT")
542
+ left.push(`${p.path} (${code === "ENOTEMPTY" ? "not empty" : err.message}${again})`);
543
+ }
544
+ };
545
+ if (aside)
546
+ attempt(aside, () => fs.rmSync(aside.path, { recursive: true, force: true }), "; relay clean tries again");
547
+ if (t.dir && !t.others)
548
+ attempt(t.dir, () => fs.rmdirSync(t.dir.path), "");
549
+ return left;
550
+ }
551
+ /**
552
+ * relay clean --forget <run_id>: forgets one run that keep_records_days keeps only because a
553
+ * repository a job of it has a branch or worktree in is not there (heldOnDisk), which relay cannot
554
+ * tell from a volume not mounted, so only the person can say it is gone. Refused for a run still in
555
+ * use (stillActive), with login-store entries relay quarantined, or whose repositories are not all
556
+ * gone (reposGone). Without `write` it only reads: it settles nothing reap would settle (unsettled
557
+ * says so instead) and says what it would do. With `write`, after reaping, it removes the run's rows
558
+ * (overwritten, and the registry's rebuild recorded before they go, as keep_records_days does), its
559
+ * records and its harnesses' state, and what relay made for it in its worktrees home (runTrees):
560
+ * all moved aside in the transaction that removes the rows, in which what the registry says is
561
+ * checked again (the repositories were looked at before it), and removed after; then the transfers
562
+ * of it relay adopt authorized. Nothing outside relay's own directories, nothing through a link,
563
+ * and no branch: those are in repositories that are not there.
564
+ */
565
+ export function forgetRun(id, o) {
566
+ if (!getRun(id))
567
+ return { error: `unknown run ${id}` };
568
+ const refused = (why) => ({ run_id: id, refused: true, reason: `relay will not forget ${id}: ${why}` });
569
+ if (!RUN_ID.test(id))
570
+ return refused("it is not a run id relay makes, so relay leaves its records alone");
571
+ // Only the acting forget settles, as relay clean and relay tree do first: a job whose worker has
572
+ // died is recorded lost (its agents left without a supervisor stopped), and an approval request
573
+ // that no job can take up any more expires.
574
+ if (o.write)
575
+ reap(id);
576
+ const nodes = runNodes(id);
577
+ /** What the plan below rests on, of the registry: checked again in the transaction. */
578
+ const held = (ns) => JSON.stringify(ns.map((n) => [n.id, n.repo, n.branch, n.worktree]));
579
+ const planned = held(nodes);
580
+ let trees;
581
+ let files;
582
+ try {
583
+ const run = getRun(id);
584
+ if (!run)
585
+ return { error: `unknown run ${id}` };
586
+ const no = (o.write ? null : unsettled(id, nodes)) ?? stillActive(run, nodes) ?? quarantined(id) ?? reposGone(id, nodes);
587
+ if (no)
588
+ return refused(no);
589
+ trees = runTrees(id, nodes);
590
+ const own = runDirs(id).flatMap((d) => {
591
+ try {
592
+ return fs.lstatSync(d.dir, { throwIfNoEntry: false }) ? [d.dir] : [];
593
+ }
594
+ catch (err) {
595
+ throw new Error(`relay could not look at ${d.dir} (${err.message})`);
596
+ }
597
+ });
598
+ files = [...own, ...trees.made.map((m) => m.path), ...(trees.dir && !trees.others ? [trees.dir.path] : []), ...adoptionTickets(id).files];
599
+ }
600
+ catch (err) {
601
+ return refused(err.message);
602
+ }
603
+ const report = {
604
+ run_id: id,
605
+ dry_run: !o.write,
606
+ registry_rows: { ...db().prepare(`SELECT (SELECT COUNT(*) FROM nodes WHERE run_id = ?1) AS items,
607
+ (SELECT COUNT(*) FROM turns WHERE node_id IN (SELECT id FROM nodes WHERE run_id = ?1)) AS turns,
608
+ (SELECT COUNT(*) FROM requests WHERE run_id = ?1) AS approval_requests,
609
+ (SELECT COUNT(*) FROM events WHERE run_id = ?1) AS events`).get(id) },
610
+ relay_files: files,
611
+ ...(trees.alone.length ? { left_alone: trees.alone } : {}),
612
+ branches_not_touched: nodes.flatMap((n) => (n.branch && n.repo ? [`${n.branch} in ${n.repo}`] : [])),
613
+ };
614
+ const alone = trees.alone.length ? " What left_alone names is not relay's to remove: delete it yourself once you have looked at it." : "";
615
+ const branches = "Its branches (branches_not_touched) are in repositories that are not there, which relay cannot reach, so it does not touch them: a repository that was moved, or is on a volume not mounted, still has them.";
616
+ if (!o.write) {
617
+ return {
618
+ ...report, forgotten: false,
619
+ note: `Nothing was changed. With --write, relay removes the run's rows from the registry and relay_files from disk${trees.made.length ? " (work never committed in its worktrees goes with them)" : ""}, and nothing else.${alone} ${branches} If its repository can be put back, do that instead: relay clean then deals with the run as with any other.`,
620
+ };
621
+ }
622
+ const left = [];
623
+ const unscheduled = scheduleRebuild();
624
+ let moved = [];
625
+ const treesAside = { pin: null };
626
+ let no;
627
+ try {
628
+ no = overwriting(() => tx(() => {
629
+ const run = getRun(id);
630
+ if (!run)
631
+ return "it is no longer in the registry (another relay process forgot it)";
632
+ const now = runNodes(id);
633
+ if (held(now) !== planned)
634
+ return "its jobs' repositories, branches or worktrees changed while relay was looking; run this again";
635
+ const active = stillActive(run, now);
636
+ if (active)
637
+ return active;
638
+ moved = moveAside(runDirs(id));
639
+ treesAside.pin = moveTreesAside(id, trees);
640
+ deleteRun(id);
641
+ return null;
642
+ }));
643
+ }
644
+ catch (err) {
645
+ // moveTreesAside puts back what it moved before it throws. What cannot be put back here is
646
+ // put back by sweepTemps while the registry lists the run.
647
+ if (getRun(id)) {
648
+ putBack(moved);
649
+ try {
650
+ if (treesAside.pin)
651
+ putTreesBack(trees.root.path, id, treesAside.pin.path);
652
+ }
653
+ catch {
654
+ // As above.
655
+ }
656
+ }
657
+ return refused(err.message);
658
+ }
659
+ if (no)
660
+ return refused(no);
661
+ if (unscheduled)
662
+ left.push(unscheduled);
663
+ left.push(...removeTreesAside(trees, treesAside.pin), ...removeFiles(id, moved, false));
664
+ rebuildRegistry(left);
665
+ return {
666
+ ...report, forgotten: true, ...(left.length ? { left } : {}),
667
+ note: `${id} is forgotten: its rows are gone from the registry, and relay_files from disk${left.length ? ", but for what left names" : ""}.${alone} ${branches} In such a repository, git worktree prune drops git's record of the worktrees removed.`,
668
+ };
669
+ }
670
+ /**
671
+ * A worker's housekeeping once its session's turns are done, at most once a day across every
672
+ * relay process of this control directory (a stamp file there says when it last ran): the sweep of
673
+ * relay's leftover temporary directories, and keep_records_days where it is set, each done whatever
674
+ * became of the other. What it did, and what went wrong, goes to the worker's log.
675
+ */
676
+ export async function housekeepDaily(cfg) {
677
+ const stamp = path.join(relayHome(), "housekeeping");
678
+ const st = fs.statSync(stamp, { throwIfNoEntry: false });
679
+ if (st && Date.now() - st.mtimeMs < DAY)
680
+ return;
681
+ // Stamped first, so that the workers finishing at the same moment do not all do it. A stamp that
682
+ // cannot be written (a full disk, say) does not stop it: the next worker then does it again.
683
+ try {
684
+ fs.writeFileSync(stamp, `${new Date().toISOString()}\n`);
685
+ }
686
+ catch (err) {
687
+ console.error(`relay: could not write ${stamp} (${err.message}), so the next worker does this housekeeping again`);
688
+ }
689
+ try {
690
+ const swept = await sweepTemps();
691
+ if (swept.removed.length || swept.problems.length)
692
+ console.error(`relay: removed leftover temporary directories: ${swept.removed.join(", ") || "none"}${swept.problems.length ? `; ${swept.problems.join("; ")}` : ""}`);
693
+ }
694
+ catch (err) {
695
+ console.error(`relay: leftover temporary directories were not swept: ${err.message}`);
696
+ }
697
+ if (cfg.keep_records_days === null)
698
+ return;
699
+ const r = forgetOldRuns(cfg.keep_records_days);
700
+ if (r.forgotten.length || r.left.length)
701
+ console.error(`relay: keep_records_days ${cfg.keep_records_days}: forgot ${r.forgotten.join(", ") || "nothing"}${r.left.length ? `; ${r.left.join("; ")}` : ""}`);
702
+ }
703
+ /** Whether something listens on a Unix socket. A socket that neither refuses nor answers within 2 s counts as listening. */
704
+ function listening(socket) {
705
+ return new Promise((resolve) => {
706
+ const c = net.createConnection(socket);
707
+ const done = (live) => {
708
+ c.destroy();
709
+ resolve(live);
710
+ };
711
+ c.once("connect", () => done(true));
712
+ c.once("error", () => done(false));
713
+ c.setTimeout(2000, () => done(true));
714
+ });
715
+ }
716
+ /** Directories made with mkdtemp under these prefixes (six random characters after the prefix). */
717
+ const BROKER_DIR = /^relay-(?:turn|probe)-[A-Za-z0-9]{6}$/;
718
+ const PROBE_SCRATCH = /^relay-probe-[A-Za-z0-9]{6}$/;
719
+ /** A worktree identity file's build directory, beside the worktree (context.ts writeWorktreeContext). */
720
+ const CONTEXT_BUILD = new RegExp(`^\\..+${CONTEXT_DIR.replace(/\./g, "\\.")}\\.[0-9a-f]{16}$`);
721
+ /** Also recognizes a rename whose registry transaction rolled back after a crash. */
722
+ function matchingTempTurns(registry, dir, st) {
723
+ return registry.prepare("SELECT * FROM turns WHERE tmp_dir IN (?, ?) AND tmp_dev = ? AND tmp_ino = ?")
724
+ .all(dir, dir.endsWith(".swept") ? dir.slice(0, -6) : dir, st.dev, st.ino);
725
+ }
726
+ /** Read-only inventory, shared by doctor and the sweep. Never read a marker from agent-writable temp space. */
727
+ function privateTurnTemps(find = (dir, st) => matchingTempTurns(db(), dir, st)) {
728
+ const out = [];
729
+ // Also inventory the old shared-temp location, so upgrades report leftovers safely.
730
+ for (const parent of new Set([os.tmpdir(), privateStateRoot(), nativeTempRoot()])) {
731
+ let root;
732
+ try {
733
+ if (parent !== os.tmpdir() && fs.lstatSync(parent).isSymbolicLink()) {
734
+ out.push({ dir: parent, problem: `${parent} is a symbolic link; left alone` });
735
+ continue;
736
+ }
737
+ root = fs.realpathSync(parent);
738
+ }
739
+ catch (err) {
740
+ if (err.code !== "ENOENT")
741
+ out.push({ dir: parent, problem: `the temporary directory could not be read (${err.message})` });
742
+ continue;
743
+ }
744
+ let names;
745
+ try {
746
+ names = fs.readdirSync(root);
747
+ }
748
+ catch (err) {
749
+ out.push({ dir: root, problem: `the temporary directory could not be read (${err.message})` });
750
+ continue;
751
+ }
752
+ for (const name of names) {
753
+ if (!name.startsWith("relay-tmp-") && !(parent === nativeTempRoot() && /^[A-Za-z0-9]{6}(?:\.swept)?$/.test(name)))
754
+ continue;
755
+ const dir = path.join(root, name);
756
+ try {
757
+ const st = fs.lstatSync(dir, { throwIfNoEntry: false });
758
+ if (!st || !st.isDirectory() || st.isSymbolicLink() || (typeof process.getuid === "function" && st.uid !== process.getuid()))
759
+ continue;
760
+ const matches = find(dir, st);
761
+ out.push(matches.length === 1 ? { dir, st, turn: matches[0] } : { dir, st, problem: `${dir} cannot be matched to a turn in the registry; left alone`, left: { root, shared: parent === os.tmpdir(), reason: null } });
762
+ }
763
+ catch (err) {
764
+ const reason = err.message;
765
+ out.push({ dir, problem: `${dir} could not be checked against the registry (${reason}); left alone`, left: { root, shared: parent === os.tmpdir(), reason } });
766
+ }
767
+ }
768
+ }
769
+ return out;
770
+ }
771
+ /**
772
+ * Unknown/legacy private temp directories require a person's investigation; doctor never removes
773
+ * them. One line for each directory they are in (and each reason one could not be checked),
774
+ * however many there are: one Mac had 333 left by builds before 1.0 (leftTempsLine).
775
+ */
776
+ export function turnTempProblems() {
777
+ let registry;
778
+ let checked = false;
779
+ let mapped = false;
780
+ let unreadable;
781
+ try {
782
+ const entries = privateTurnTemps((dir, st) => {
783
+ if (!checked) {
784
+ checked = true;
785
+ const file = path.join(relayHome(), "relay.db");
786
+ try {
787
+ if (fs.existsSync(file)) {
788
+ // Doctor must neither create a registry nor back up/migrate an older one. Legacy
789
+ // registries cannot match these directories; the write-mode sweep migrates normally.
790
+ registry = new DatabaseSync(file, { readOnly: true });
791
+ const columns = registry.prepare("PRAGMA table_info(turns)").all().map((c) => c.name);
792
+ mapped = ["tmp_dir", "tmp_dev", "tmp_ino"].every((c) => columns.includes(c));
793
+ }
794
+ }
795
+ catch (err) {
796
+ // Every directory then goes unchecked, not just the first: none can be matched.
797
+ unreadable = err;
798
+ }
799
+ }
800
+ if (unreadable)
801
+ throw unreadable;
802
+ return registry && mapped ? matchingTempTurns(registry, dir, st) : [];
803
+ });
804
+ const lines = [];
805
+ const groups = new Map();
806
+ for (const e of entries) {
807
+ if (!e.left) {
808
+ if (e.problem)
809
+ lines.push(e.problem);
810
+ continue;
811
+ }
812
+ const key = JSON.stringify([e.left.root, e.left.reason]);
813
+ let group = groups.get(key);
814
+ if (!group) {
815
+ group = { where: e.left, names: [] };
816
+ groups.set(key, group);
817
+ lines.push(group);
818
+ }
819
+ group.names.push(path.basename(e.dir));
820
+ }
821
+ return lines.map((l) => typeof l === "string" ? l : leftTempsLine(l.where, l.names));
822
+ }
823
+ finally {
824
+ registry?.close();
825
+ }
826
+ }
827
+ /** The most directory names one of doctor's lines about them gives. */
828
+ const NAMED = 3;
829
+ /**
830
+ * Doctor's line about turns' directories in one directory that relay leaves alone for one reason:
831
+ * how many, where, a few of their names, and what to make of them. relay clean's temp_dirs_left
832
+ * still names each one.
833
+ */
834
+ export function leftTempsLine(where, names, platform = process.platform) {
835
+ const one = names.length === 1;
836
+ const named = [...names].sort().slice(0, NAMED);
837
+ const more = names.length - named.length;
838
+ const list = ` (${named.join(", ")}${more ? `, and ${more} more` : ""})`;
839
+ const what = one ? path.join(where.root, names[0]) : `${names.length} directories in ${where.root}${list}`;
840
+ if (where.reason !== null)
841
+ return `${what} could not be checked against the registry (${where.reason}); left alone`;
842
+ const head = `${what} cannot be matched to a turn in the registry; left alone.`;
843
+ if (!where.shared) {
844
+ return `${head} Relay removes a turn's directory here only once it matches a settled turn in this registry and nothing still uses it; a directory of a turn run under another control directory (RELAY_HOME), or of one no longer recorded, never matches.`;
845
+ }
846
+ // An observation, not a promise: on one Mac with macOS 27 (2026-10-21) nothing in the per-user
847
+ // temporary directory was older than the Mac's last start, while files there untouched for days
848
+ // stayed. dirhelper(8) documents no cleanup at restart, and its launchd job's 3-day rule
849
+ // (CLEAN_FILES_OLDER_THAN_DAYS) had not removed those files.
850
+ const restart = platform === "darwin" && /^\/private\/var\/folders\/[^/]+\/[^/]+\/T$/.test(where.root)
851
+ ? ` On macOS 27 this directory has been seen emptied when the Mac restarts (2026-10-21).` : "";
852
+ const them = one ? "it" : "them";
853
+ return `${head} Relay builds before 1.0 made turns' directories here without recording which turn made which, so relay cannot tell what made ${them} or whether anything still uses ${them}; 1.0 makes and uses none here.${restart}`;
854
+ }
855
+ /** A failed process inspection is unknown liveness, never evidence that a directory is unused. */
856
+ function mayLive(pid, start) {
857
+ return sameProcess(pid, start) || (pidAlive(pid) && procStart(pid) === null);
858
+ }
859
+ function turnTempUnused(turn) {
860
+ if (!["done", "refused", "failed", "cancelled", "lost"].includes(turn.status))
861
+ return false;
862
+ const node = getNode(turn.node_id);
863
+ if (!node || mayLive(node.worker_pid, node.worker_start) || mayLive(node.agent_pid, node.agent_start))
864
+ return false;
865
+ if (launchReservationProblem(node.run_id, node.id))
866
+ return false;
867
+ if (groupAlive(node.agent_pgid)) {
868
+ // A leader known to have a different start time is a reused process-group number. Without
869
+ // that positive evidence, a group whose leader has exited may still contain descendants.
870
+ if (!node.agent_start || !pidAlive(node.agent_pgid) || !procStart(node.agent_pgid) || sameProcess(node.agent_pgid, node.agent_start))
871
+ return false;
872
+ }
873
+ const procs = db().prepare("SELECT pid, start FROM relay_procs WHERE node_id = ?").all(node.id);
874
+ if (procs.some((p) => mayLive(p.pid, p.start)))
875
+ return false;
876
+ // A stage-2 executor of the node that is still running (its record not yet acted on): its commands may use the turn's directories.
877
+ return !nodeHasLiveExecutor(node.id);
878
+ }
879
+ /** A stage-2 turn's copy of its skill package (codex-stage2.ts stage2SkillAssets), made with mkdtemp. */
880
+ const SKILL_ASSETS = /^relay-ext-[A-Za-z0-9]{6}$/;
881
+ /** The turn whose skill-asset copy `dir` is, as its node's params name it (SessionParams.ext_assets), or null. */
882
+ function skillAssetsTurn(dir) {
883
+ const rows = db().prepare("SELECT id, params_json FROM nodes WHERE params_json LIKE '%ext_assets%'").all();
884
+ for (const r of rows) {
885
+ for (const [turn, d] of Object.entries(sessionParams(r).ext_assets ?? {}))
886
+ if (d === dir && Number.isInteger(Number(turn)))
887
+ return { nodeId: r.id, turnId: Number(turn) };
888
+ }
889
+ return null;
890
+ }
891
+ /**
892
+ * Removes temporary directories relay made and no longer needs, whatever the config, and returns
893
+ * them, with what could not be looked at or removed. It never throws: a part of the sweep that
894
+ * cannot be done (a temporary directory that is not there, say) is said, and the others are still
895
+ * done. A crash (a worker killed, a machine stopped) leaves them behind; otherwise relay removes
896
+ * each itself. Each is relay's by its name and owned by this user, and none is touched while
897
+ * whatever made it may still use it:
898
+ * - a confined turn's broker directory, or a probe's decoy of one (relay-sockets/relay-turn-*,
899
+ * relay-probe-*), from more than an hour ago, whose socket nobody listens on any more or that
900
+ * never got one; only where relay-sockets is a plain directory of this user's, not a link;
901
+ * - a confinement probe's scratch directory ($TMPDIR/relay-probe-*), from more than an hour ago
902
+ * (a probe takes seconds);
903
+ * - a turn's private relay-tmp-* directory, matched by path/device/inode in the registry, only
904
+ * after the turn settles and its worker, agent, group and registered processes are gone;
905
+ * - the build directory of a job's identity file (beside its worktree), from more than ten minutes
906
+ * ago, which relay renames into place a moment after making it;
907
+ * - a forgotten run's directories that could not be removed when it was forgotten (.forgotten-*),
908
+ * or put back if the registry still lists the run;
909
+ * - stage 2: an executor whose relay process is gone (its record in `<relay home>/executors`), stopped
910
+ * with its group, descendants and detached processes, its ports given back (stage2-executors.ts),
911
+ * first, before any turn's directory is looked at; and a turn's skill-asset copy
912
+ * (relay-ext-* in relay's private root), only where the registry names it for a turn that
913
+ * is over, as for relay-tmp-*. One no registry entry names is left alone and said.
914
+ */
915
+ export async function sweepTemps(o = {}) {
916
+ const at = o.at ?? Date.now();
917
+ const out = { removed: [], problems: [] };
918
+ const entries = (dir) => {
919
+ try {
920
+ return fs.readdirSync(dir);
921
+ }
922
+ catch (err) {
923
+ if (err.code !== "ENOENT")
924
+ out.problems.push(`${dir} could not be read (${err.message})`);
925
+ return [];
926
+ }
927
+ };
928
+ const remove = (dir, reported = dir) => {
929
+ try {
930
+ fs.rmSync(dir, { recursive: true, force: true });
931
+ out.removed.push(reported);
932
+ }
933
+ catch (err) {
934
+ out.problems.push(`${dir} could not be removed (${err.message})`);
935
+ }
936
+ };
937
+ /** The directory `name` in `parent`, if it is one of this user's, last changed more than `age` ago. */
938
+ const old = (parent, name, age) => {
939
+ const dir = path.join(parent, name);
940
+ let st;
941
+ try {
942
+ st = fs.lstatSync(dir, { throwIfNoEntry: false });
943
+ }
944
+ catch (err) {
945
+ out.problems.push(`${dir} could not be looked at (${err.message})`);
946
+ return null;
947
+ }
948
+ return st && mine(st) && at - st.mtimeMs >= age ? dir : null;
949
+ };
950
+ /** One part of the sweep: what stops it is said, and the parts after it are still done. */
951
+ const part = async (what, sweep) => {
952
+ try {
953
+ await sweep();
954
+ }
955
+ catch (err) {
956
+ out.problems.push(`${what()} was not swept (${err.message})`);
957
+ }
958
+ };
959
+ const tmpDir = () => `the temporary directory ${os.tmpdir()}`;
960
+ // First: an executor left running by a relay process that died. Only one whose owner is gone, and
961
+ // only the process recorded (pid and start time); its turn's directories wait until it is stopped.
962
+ await part(() => "stage-2 executors left by relay processes that have gone", () => {
963
+ const r = sweepOrphanedExecutors();
964
+ out.removed.push(...r.stopped);
965
+ out.problems.push(...r.problems);
966
+ });
967
+ await part(() => `relay's sockets directory in ${tmpDir()}`, async () => {
968
+ const sockets = relaySocketsDir();
969
+ // Only relay's own, as ensureRelaySocketsDir makes it: through a link, or in a directory of
970
+ // someone else's, what would be removed is not in relay's sockets directory.
971
+ const st = fs.lstatSync(sockets, { throwIfNoEntry: false });
972
+ if (!st)
973
+ return;
974
+ if (!mine(st)) {
975
+ out.problems.push(`${sockets} is not a plain directory of this user's, so relay did not sweep it`);
976
+ return;
977
+ }
978
+ for (const name of entries(sockets)) {
979
+ const dir = BROKER_DIR.test(name) ? old(sockets, name, HOUR) : null;
980
+ if (!dir)
981
+ continue;
982
+ const socket = path.join(dir, "relay.sock");
983
+ if (fs.existsSync(socket) && (await listening(socket)))
984
+ continue;
985
+ remove(dir);
986
+ }
987
+ });
988
+ for (const parent of new Set([os.tmpdir(), privateStateRoot()]))
989
+ await part(() => parent, () => {
990
+ if (parent === privateStateRoot()) {
991
+ const st = fs.lstatSync(parent, { throwIfNoEntry: false });
992
+ if (!st)
993
+ return;
994
+ if (!mine(st))
995
+ throw new Error("not a plain private directory of this user's");
996
+ }
997
+ const tmp = fs.realpathSync(parent);
998
+ for (const name of entries(tmp)) {
999
+ const dir = PROBE_SCRATCH.test(name) ? old(tmp, name, HOUR) : null;
1000
+ if (dir)
1001
+ remove(dir);
1002
+ }
1003
+ });
1004
+ await part(() => `stage-2 skill copies in ${privateStateRoot()}`, () => {
1005
+ const parent = privateStateRoot();
1006
+ const st = fs.lstatSync(parent, { throwIfNoEntry: false });
1007
+ if (!st)
1008
+ return;
1009
+ if (!mine(st))
1010
+ throw new Error("not a plain private directory of this user's");
1011
+ for (const name of entries(parent)) {
1012
+ if (!SKILL_ASSETS.test(name))
1013
+ continue;
1014
+ const dir = path.join(parent, name);
1015
+ const dst = fs.lstatSync(dir, { throwIfNoEntry: false });
1016
+ if (!dst || !mine(dst))
1017
+ continue;
1018
+ const owner = skillAssetsTurn(dir);
1019
+ if (!owner) {
1020
+ out.problems.push(`${dir} cannot be matched to a turn in the registry; left alone`);
1021
+ continue;
1022
+ }
1023
+ const turn = getTurn(owner.turnId);
1024
+ if (!turn || turn.node_id !== owner.nodeId) {
1025
+ out.problems.push(`${dir} is named for turn ${owner.turnId} of ${owner.nodeId}, which the registry does not have; left alone`);
1026
+ continue;
1027
+ }
1028
+ if (!turnTempUnused(turn))
1029
+ continue;
1030
+ remove(dir);
1031
+ if (!fs.existsSync(dir))
1032
+ dropSkillAssets(owner.nodeId, owner.turnId);
1033
+ }
1034
+ });
1035
+ await part(() => `turns' private directories in ${tmpDir()}`, () => {
1036
+ for (const entry of privateTurnTemps()) {
1037
+ if (entry.problem) {
1038
+ out.problems.push(entry.problem);
1039
+ continue;
1040
+ }
1041
+ if (!entry.turn || !entry.st)
1042
+ continue;
1043
+ // Recheck and rename under the registry lock; recursively delete only after committing.
1044
+ // Keep the new path with the same device/inode so a crash or failed removal can be retried.
1045
+ const aside = tx(() => {
1046
+ const turn = getTurn(entry.turn.id);
1047
+ if (!turn || turn.tmp_dir !== entry.turn.tmp_dir || turn.tmp_dev !== entry.st.dev || turn.tmp_ino !== entry.st.ino || !turnTempUnused(turn))
1048
+ return;
1049
+ const st = fs.lstatSync(entry.dir, { throwIfNoEntry: false });
1050
+ if (!st?.isDirectory() || st.isSymbolicLink() || st.dev !== entry.st.dev || st.ino !== entry.st.ino)
1051
+ return;
1052
+ const aside = entry.dir.endsWith(".swept") ? entry.dir : `${entry.dir}.swept`;
1053
+ if (aside !== entry.dir) {
1054
+ if (fs.lstatSync(aside, { throwIfNoEntry: false }))
1055
+ throw new Error(`${aside} already exists; left alone`);
1056
+ fs.renameSync(entry.dir, aside);
1057
+ }
1058
+ try {
1059
+ db().prepare("UPDATE turns SET tmp_dir = ? WHERE id = ?").run(aside, turn.id);
1060
+ }
1061
+ catch (err) {
1062
+ if (aside !== entry.dir)
1063
+ fs.renameSync(aside, entry.dir);
1064
+ throw err;
1065
+ }
1066
+ return aside;
1067
+ });
1068
+ if (aside)
1069
+ remove(aside, entry.dir);
1070
+ }
1071
+ });
1072
+ /** The worktrees home, when it is there and a plain directory of this user's: through a link, what would be removed is not in relay's own directory. Looked at once. */
1073
+ let treesChecked;
1074
+ const treesHome = () => {
1075
+ if (treesChecked)
1076
+ return treesChecked.trees;
1077
+ treesChecked = { trees: null };
1078
+ const trees = worktreesHome();
1079
+ const st = fs.lstatSync(trees, { throwIfNoEntry: false });
1080
+ if (st && !mine(st))
1081
+ out.problems.push(`${trees} is not a plain directory of this user's, so relay did not sweep it`);
1082
+ if (st && mine(st))
1083
+ treesChecked.trees = trees;
1084
+ return treesChecked.trees;
1085
+ };
1086
+ await part(() => `the worktrees' directory ${worktreesHome()}`, () => {
1087
+ const trees = treesHome();
1088
+ if (!trees)
1089
+ return;
1090
+ for (const run of entries(trees)) {
1091
+ if (!old(trees, run, 0))
1092
+ continue;
1093
+ for (const name of entries(path.join(trees, run))) {
1094
+ const dir = CONTEXT_BUILD.test(name) ? old(path.join(trees, run), name, 10 * 60_000) : null;
1095
+ if (dir)
1096
+ remove(dir);
1097
+ }
1098
+ }
1099
+ });
1100
+ // A forgotten run's directory left aside; or, should the registry still list its run (relay was
1101
+ // killed in the transaction that forgot it), put back. Decided, and put back, under the registry's
1102
+ // lock: a run another relay process is forgetting at this moment (its directories aside, its rows
1103
+ // not yet gone) is waited for, so that nothing is put back for a run the registry then loses.
1104
+ // A forgotten run's worktrees (relay clean --forget) are put back into its directory there.
1105
+ await part(() => "the directories of forgotten runs", () => {
1106
+ const trees = treesHome();
1107
+ for (const parent of [path.join(relayHome(), "runs"), harnessHome(), ...(trees ? [trees] : [])]) {
1108
+ for (const name of entries(parent)) {
1109
+ const dir = name.startsWith(ASIDE) ? old(parent, name, 0) : null;
1110
+ if (!dir)
1111
+ continue;
1112
+ const id = name.slice(ASIDE.length);
1113
+ let listed;
1114
+ try {
1115
+ listed = tx(() => {
1116
+ if (!getRun(id))
1117
+ return false;
1118
+ if (parent === trees)
1119
+ putTreesBack(trees, id, dir);
1120
+ else
1121
+ fs.renameSync(dir, path.join(parent, id));
1122
+ return true;
1123
+ });
1124
+ }
1125
+ catch (err) {
1126
+ out.problems.push(`${dir} was left as it is: relay could not check it against the registry or put it back for ${id} (${err.message})`);
1127
+ continue;
1128
+ }
1129
+ if (!listed)
1130
+ remove(dir);
1131
+ }
1132
+ }
1133
+ });
1134
+ return out;
1135
+ }