@culture.dev/agents 0.0.0-stage → 0.15.0

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 (290) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +59 -2
  3. package/dist/contract-schemas/agent.json +128 -0
  4. package/dist/contract-schemas/allowlist.json +25 -0
  5. package/dist/contract-schemas/api.json +501 -0
  6. package/dist/contract-schemas/capabilities.json +208 -0
  7. package/dist/contract-schemas/common.json +123 -0
  8. package/dist/contract-schemas/control-action.json +279 -0
  9. package/dist/contract-schemas/error.json +269 -0
  10. package/dist/contract-schemas/event.json +316 -0
  11. package/dist/contract-schemas/run.json +336 -0
  12. package/dist/contract-schemas/session.json +77 -0
  13. package/dist/contract-schemas/version.json +22 -0
  14. package/dist/src/contract/api.d.ts +41 -0
  15. package/dist/src/contract/api.js +35 -0
  16. package/dist/src/contract/api.js.map +1 -0
  17. package/dist/src/contract/errors.d.ts +21 -0
  18. package/dist/src/contract/errors.js +25 -0
  19. package/dist/src/contract/errors.js.map +1 -0
  20. package/dist/src/contract/generated/agent.d.ts +31 -0
  21. package/dist/src/contract/generated/agent.js +3 -0
  22. package/dist/src/contract/generated/agent.js.map +1 -0
  23. package/dist/src/contract/generated/allowlist.d.ts +10 -0
  24. package/dist/src/contract/generated/allowlist.js +3 -0
  25. package/dist/src/contract/generated/allowlist.js.map +1 -0
  26. package/dist/src/contract/generated/capabilities.d.ts +46 -0
  27. package/dist/src/contract/generated/capabilities.js +3 -0
  28. package/dist/src/contract/generated/capabilities.js.map +1 -0
  29. package/dist/src/contract/generated/common.d.ts +40 -0
  30. package/dist/src/contract/generated/common.js +3 -0
  31. package/dist/src/contract/generated/common.js.map +1 -0
  32. package/dist/src/contract/generated/control-action.d.ts +73 -0
  33. package/dist/src/contract/generated/control-action.js +3 -0
  34. package/dist/src/contract/generated/control-action.js.map +1 -0
  35. package/dist/src/contract/generated/error.d.ts +25 -0
  36. package/dist/src/contract/generated/error.js +3 -0
  37. package/dist/src/contract/generated/error.js.map +1 -0
  38. package/dist/src/contract/generated/event.d.ts +80 -0
  39. package/dist/src/contract/generated/event.js +3 -0
  40. package/dist/src/contract/generated/event.js.map +1 -0
  41. package/dist/src/contract/generated/index.d.ts +81 -0
  42. package/dist/src/contract/generated/index.js +3 -0
  43. package/dist/src/contract/generated/index.js.map +1 -0
  44. package/dist/src/contract/generated/run.d.ts +63 -0
  45. package/dist/src/contract/generated/run.js +3 -0
  46. package/dist/src/contract/generated/run.js.map +1 -0
  47. package/dist/src/contract/generated/session.d.ts +12 -0
  48. package/dist/src/contract/generated/session.js +3 -0
  49. package/dist/src/contract/generated/session.js.map +1 -0
  50. package/dist/src/contract/generated/version.d.ts +9 -0
  51. package/dist/src/contract/generated/version.js +3 -0
  52. package/dist/src/contract/generated/version.js.map +1 -0
  53. package/dist/src/contract/ids.d.ts +36 -0
  54. package/dist/src/contract/ids.js +85 -0
  55. package/dist/src/contract/ids.js.map +1 -0
  56. package/dist/src/contract/index.d.ts +17 -0
  57. package/dist/src/contract/index.js +13 -0
  58. package/dist/src/contract/index.js.map +1 -0
  59. package/dist/src/contract/run-state.d.ts +13 -0
  60. package/dist/src/contract/run-state.js +24 -0
  61. package/dist/src/contract/run-state.js.map +1 -0
  62. package/dist/src/contract/schemas.d.ts +18 -0
  63. package/dist/src/contract/schemas.js +37 -0
  64. package/dist/src/contract/schemas.js.map +1 -0
  65. package/dist/src/contract/validate.d.ts +17 -0
  66. package/dist/src/contract/validate.js +68 -0
  67. package/dist/src/contract/validate.js.map +1 -0
  68. package/dist/src/daemon/allowlist.d.ts +23 -0
  69. package/dist/src/daemon/allowlist.js +57 -0
  70. package/dist/src/daemon/allowlist.js.map +1 -0
  71. package/dist/src/daemon/daemon.d.ts +31 -0
  72. package/dist/src/daemon/daemon.js +141 -0
  73. package/dist/src/daemon/daemon.js.map +1 -0
  74. package/dist/src/daemon/errors.d.ts +14 -0
  75. package/dist/src/daemon/errors.js +26 -0
  76. package/dist/src/daemon/errors.js.map +1 -0
  77. package/dist/src/daemon/harness-catalog.d.ts +11 -0
  78. package/dist/src/daemon/harness-catalog.js +21 -0
  79. package/dist/src/daemon/harness-catalog.js.map +1 -0
  80. package/dist/src/daemon/http.d.ts +15 -0
  81. package/dist/src/daemon/http.js +76 -0
  82. package/dist/src/daemon/http.js.map +1 -0
  83. package/dist/src/daemon/lock.d.ts +47 -0
  84. package/dist/src/daemon/lock.js +167 -0
  85. package/dist/src/daemon/lock.js.map +1 -0
  86. package/dist/src/daemon/main.d.ts +32 -0
  87. package/dist/src/daemon/main.js +204 -0
  88. package/dist/src/daemon/main.js.map +1 -0
  89. package/dist/src/daemon/paths.d.ts +29 -0
  90. package/dist/src/daemon/paths.js +26 -0
  91. package/dist/src/daemon/paths.js.map +1 -0
  92. package/dist/src/daemon/ports.d.ts +58 -0
  93. package/dist/src/daemon/ports.js +2 -0
  94. package/dist/src/daemon/ports.js.map +1 -0
  95. package/dist/src/daemon/routes.d.ts +15 -0
  96. package/dist/src/daemon/routes.js +259 -0
  97. package/dist/src/daemon/routes.js.map +1 -0
  98. package/dist/src/daemon/sse.d.ts +8 -0
  99. package/dist/src/daemon/sse.js +56 -0
  100. package/dist/src/daemon/sse.js.map +1 -0
  101. package/dist/src/harness/availability.d.ts +9 -0
  102. package/dist/src/harness/availability.js +52 -0
  103. package/dist/src/harness/availability.js.map +1 -0
  104. package/dist/src/harness/claude-code/agent.d.ts +4 -0
  105. package/dist/src/harness/claude-code/agent.js +33 -0
  106. package/dist/src/harness/claude-code/agent.js.map +1 -0
  107. package/dist/src/harness/claude-code/backend.d.ts +48 -0
  108. package/dist/src/harness/claude-code/backend.js +83 -0
  109. package/dist/src/harness/claude-code/backend.js.map +1 -0
  110. package/dist/src/harness/claude-code/capabilities.d.ts +7 -0
  111. package/dist/src/harness/claude-code/capabilities.js +11 -0
  112. package/dist/src/harness/claude-code/capabilities.js.map +1 -0
  113. package/dist/src/harness/claude-code/index.d.ts +15 -0
  114. package/dist/src/harness/claude-code/index.js +20 -0
  115. package/dist/src/harness/claude-code/index.js.map +1 -0
  116. package/dist/src/harness/claude-code/map.d.ts +30 -0
  117. package/dist/src/harness/claude-code/map.js +108 -0
  118. package/dist/src/harness/claude-code/map.js.map +1 -0
  119. package/dist/src/harness/common/capabilities.d.ts +34 -0
  120. package/dist/src/harness/common/capabilities.js +60 -0
  121. package/dist/src/harness/common/capabilities.js.map +1 -0
  122. package/dist/src/harness/common/guard.d.ts +18 -0
  123. package/dist/src/harness/common/guard.js +31 -0
  124. package/dist/src/harness/common/guard.js.map +1 -0
  125. package/dist/src/harness/common/processes.d.ts +21 -0
  126. package/dist/src/harness/common/processes.js +46 -0
  127. package/dist/src/harness/common/processes.js.map +1 -0
  128. package/dist/src/harness/common/queue.d.ts +20 -0
  129. package/dist/src/harness/common/queue.js +76 -0
  130. package/dist/src/harness/common/queue.js.map +1 -0
  131. package/dist/src/harness/common/run-driver.d.ts +88 -0
  132. package/dist/src/harness/common/run-driver.js +317 -0
  133. package/dist/src/harness/common/run-driver.js.map +1 -0
  134. package/dist/src/harness/common/sandbox-workspace.d.ts +72 -0
  135. package/dist/src/harness/common/sandbox-workspace.js +155 -0
  136. package/dist/src/harness/common/sandbox-workspace.js.map +1 -0
  137. package/dist/src/harness/common/text.d.ts +9 -0
  138. package/dist/src/harness/common/text.js +49 -0
  139. package/dist/src/harness/common/text.js.map +1 -0
  140. package/dist/src/harness/common/types.d.ts +111 -0
  141. package/dist/src/harness/common/types.js +2 -0
  142. package/dist/src/harness/common/types.js.map +1 -0
  143. package/dist/src/harness/config.d.ts +43 -0
  144. package/dist/src/harness/config.js +200 -0
  145. package/dist/src/harness/config.js.map +1 -0
  146. package/dist/src/harness/index.d.ts +21 -0
  147. package/dist/src/harness/index.js +89 -0
  148. package/dist/src/harness/index.js.map +1 -0
  149. package/dist/src/harness/opencode/backend.d.ts +37 -0
  150. package/dist/src/harness/opencode/backend.js +114 -0
  151. package/dist/src/harness/opencode/backend.js.map +1 -0
  152. package/dist/src/harness/opencode/capabilities.d.ts +8 -0
  153. package/dist/src/harness/opencode/capabilities.js +11 -0
  154. package/dist/src/harness/opencode/capabilities.js.map +1 -0
  155. package/dist/src/harness/opencode/client.d.ts +70 -0
  156. package/dist/src/harness/opencode/client.js +189 -0
  157. package/dist/src/harness/opencode/client.js.map +1 -0
  158. package/dist/src/harness/opencode/index.d.ts +13 -0
  159. package/dist/src/harness/opencode/index.js +19 -0
  160. package/dist/src/harness/opencode/index.js.map +1 -0
  161. package/dist/src/harness/opencode/lines.d.ts +14 -0
  162. package/dist/src/harness/opencode/lines.js +99 -0
  163. package/dist/src/harness/opencode/lines.js.map +1 -0
  164. package/dist/src/harness/opencode/map.d.ts +13 -0
  165. package/dist/src/harness/opencode/map.js +84 -0
  166. package/dist/src/harness/opencode/map.js.map +1 -0
  167. package/dist/src/harness/opencode/turn.d.ts +23 -0
  168. package/dist/src/harness/opencode/turn.js +143 -0
  169. package/dist/src/harness/opencode/turn.js.map +1 -0
  170. package/dist/src/harness/reclaim.d.ts +13 -0
  171. package/dist/src/harness/reclaim.js +18 -0
  172. package/dist/src/harness/reclaim.js.map +1 -0
  173. package/dist/src/harness/stub/backend.d.ts +11 -0
  174. package/dist/src/harness/stub/backend.js +125 -0
  175. package/dist/src/harness/stub/backend.js.map +1 -0
  176. package/dist/src/harness/stub/index.d.ts +18 -0
  177. package/dist/src/harness/stub/index.js +21 -0
  178. package/dist/src/harness/stub/index.js.map +1 -0
  179. package/dist/src/harness/stub/workspace.d.ts +29 -0
  180. package/dist/src/harness/stub/workspace.js +92 -0
  181. package/dist/src/harness/stub/workspace.js.map +1 -0
  182. package/dist/src/index.d.ts +2 -0
  183. package/dist/src/index.js +3 -0
  184. package/dist/src/index.js.map +1 -0
  185. package/dist/src/runs/agents.d.ts +10 -0
  186. package/dist/src/runs/agents.js +49 -0
  187. package/dist/src/runs/agents.js.map +1 -0
  188. package/dist/src/runs/control.d.ts +31 -0
  189. package/dist/src/runs/control.js +105 -0
  190. package/dist/src/runs/control.js.map +1 -0
  191. package/dist/src/runs/index.d.ts +8 -0
  192. package/dist/src/runs/index.js +7 -0
  193. package/dist/src/runs/index.js.map +1 -0
  194. package/dist/src/runs/ports.d.ts +31 -0
  195. package/dist/src/runs/ports.js +5 -0
  196. package/dist/src/runs/ports.js.map +1 -0
  197. package/dist/src/runs/scan.d.ts +38 -0
  198. package/dist/src/runs/scan.js +149 -0
  199. package/dist/src/runs/scan.js.map +1 -0
  200. package/dist/src/runs/service.d.ts +70 -0
  201. package/dist/src/runs/service.js +239 -0
  202. package/dist/src/runs/service.js.map +1 -0
  203. package/dist/src/sandbox/harness-session.d.ts +11 -0
  204. package/dist/src/sandbox/harness-session.js +170 -0
  205. package/dist/src/sandbox/harness-session.js.map +1 -0
  206. package/dist/src/sandbox/host-command.d.ts +19 -0
  207. package/dist/src/sandbox/host-command.js +41 -0
  208. package/dist/src/sandbox/host-command.js.map +1 -0
  209. package/dist/src/sandbox/index.d.ts +11 -0
  210. package/dist/src/sandbox/index.js +15 -0
  211. package/dist/src/sandbox/index.js.map +1 -0
  212. package/dist/src/sandbox/naming.d.ts +49 -0
  213. package/dist/src/sandbox/naming.js +106 -0
  214. package/dist/src/sandbox/naming.js.map +1 -0
  215. package/dist/src/sandbox/openshell-client.d.ts +81 -0
  216. package/dist/src/sandbox/openshell-client.js +49 -0
  217. package/dist/src/sandbox/openshell-client.js.map +1 -0
  218. package/dist/src/sandbox/policy.d.ts +26 -0
  219. package/dist/src/sandbox/policy.js +100 -0
  220. package/dist/src/sandbox/policy.js.map +1 -0
  221. package/dist/src/sandbox/run-sandbox.d.ts +38 -0
  222. package/dist/src/sandbox/run-sandbox.js +247 -0
  223. package/dist/src/sandbox/run-sandbox.js.map +1 -0
  224. package/dist/src/sandbox/run-sandboxes.d.ts +18 -0
  225. package/dist/src/sandbox/run-sandboxes.js +225 -0
  226. package/dist/src/sandbox/run-sandboxes.js.map +1 -0
  227. package/dist/src/sandbox/timeout.d.ts +12 -0
  228. package/dist/src/sandbox/timeout.js +39 -0
  229. package/dist/src/sandbox/timeout.js.map +1 -0
  230. package/dist/src/sandbox/types.d.ts +125 -0
  231. package/dist/src/sandbox/types.js +15 -0
  232. package/dist/src/sandbox/types.js.map +1 -0
  233. package/dist/src/sandbox/worktree.d.ts +72 -0
  234. package/dist/src/sandbox/worktree.js +157 -0
  235. package/dist/src/sandbox/worktree.js.map +1 -0
  236. package/dist/src/store/derive.d.ts +39 -0
  237. package/dist/src/store/derive.js +68 -0
  238. package/dist/src/store/derive.js.map +1 -0
  239. package/dist/src/store/event-log.d.ts +79 -0
  240. package/dist/src/store/event-log.js +202 -0
  241. package/dist/src/store/event-log.js.map +1 -0
  242. package/dist/src/store/files.d.ts +51 -0
  243. package/dist/src/store/files.js +103 -0
  244. package/dist/src/store/files.js.map +1 -0
  245. package/dist/src/store/index.d.ts +15 -0
  246. package/dist/src/store/index.js +11 -0
  247. package/dist/src/store/index.js.map +1 -0
  248. package/dist/src/store/liveness.d.ts +61 -0
  249. package/dist/src/store/liveness.js +150 -0
  250. package/dist/src/store/liveness.js.map +1 -0
  251. package/dist/src/store/run-store.d.ts +67 -0
  252. package/dist/src/store/run-store.js +211 -0
  253. package/dist/src/store/run-store.js.map +1 -0
  254. package/dist/src/util/sequence.d.ts +58 -0
  255. package/dist/src/util/sequence.js +119 -0
  256. package/dist/src/util/sequence.js.map +1 -0
  257. package/dist/src/worker/control.d.ts +60 -0
  258. package/dist/src/worker/control.js +205 -0
  259. package/dist/src/worker/control.js.map +1 -0
  260. package/dist/src/worker/delivery.d.ts +17 -0
  261. package/dist/src/worker/delivery.js +43 -0
  262. package/dist/src/worker/delivery.js.map +1 -0
  263. package/dist/src/worker/driver.d.ts +88 -0
  264. package/dist/src/worker/driver.js +6 -0
  265. package/dist/src/worker/driver.js.map +1 -0
  266. package/dist/src/worker/fake-driver.d.ts +65 -0
  267. package/dist/src/worker/fake-driver.js +123 -0
  268. package/dist/src/worker/fake-driver.js.map +1 -0
  269. package/dist/src/worker/index.d.ts +15 -0
  270. package/dist/src/worker/index.js +11 -0
  271. package/dist/src/worker/index.js.map +1 -0
  272. package/dist/src/worker/main.d.ts +1 -0
  273. package/dist/src/worker/main.js +57 -0
  274. package/dist/src/worker/main.js.map +1 -0
  275. package/dist/src/worker/retained.d.ts +13 -0
  276. package/dist/src/worker/retained.js +40 -0
  277. package/dist/src/worker/retained.js.map +1 -0
  278. package/dist/src/worker/run-loop.d.ts +63 -0
  279. package/dist/src/worker/run-loop.js +325 -0
  280. package/dist/src/worker/run-loop.js.map +1 -0
  281. package/dist/src/worker/spawn.d.ts +68 -0
  282. package/dist/src/worker/spawn.js +201 -0
  283. package/dist/src/worker/spawn.js.map +1 -0
  284. package/image/Dockerfile +81 -0
  285. package/image/build.sh +62 -0
  286. package/image/pnpm-shim.sh +25 -0
  287. package/package.json +54 -4
  288. package/policy/profiles/culture-agents-claude-subscription.yaml +59 -0
  289. package/policy/profiles/culture-agents-local-gateway.yaml +49 -0
  290. package/policy/run-sandbox.policy.json +10 -0
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Steer and cancel: steps 5 to 9 of the control order (contract/README.md). The server has already
3
+ * checked the version header, the run id, that the run exists, the body, and the deferred actions.
4
+ *
5
+ * 5. the run's harness capability document supports the action, else `unsupported` naming it;
6
+ * 6. the state allows it, else `conflict` (`steer` needs `running`; `cancel` needs an active state);
7
+ * 7. live control (false after a core restart), else `unsupported`, capability `reconnect`;
8
+ * 8. delivery over the worker's control channel, else `unavailable`;
9
+ * 9. the accepted action record (the worker appends it to the log as `run.control`).
10
+ *
11
+ * Refusals at 4, 5 and 6 against a non-terminal run whose worker this core controls are also
12
+ * written to the run's log as `run.refused`, by the worker (the log's only writer while it lives).
13
+ * The server decides step 4 (`pause`, `resume`, `answer`) and hands the refusal to
14
+ * `recordDeferredRefusal`. A refused action is never forwarded to the harness.
15
+ */
16
+ import { buildActionId, isActive, newUlid, } from "../contract/index.js";
17
+ import { ControlError, unavailable, unsupported } from "../daemon/errors.js";
18
+ import { ControlUnavailableError } from "../worker/control.js";
19
+ function record(run, kind, request, now) {
20
+ return {
21
+ id: buildActionId(run.agent_id, newUlid(now().getTime())),
22
+ run_id: run.id,
23
+ kind,
24
+ caller: request.caller,
25
+ requested_at: now().toISOString(),
26
+ idempotency_key: request.idempotency_key ?? null,
27
+ outcome: "accepted",
28
+ error: null,
29
+ };
30
+ }
31
+ /** Step 5: the capability refusal, or null. An unverifiable capability counts as unsupported. */
32
+ function capabilityRefusal(kind, target) {
33
+ const capability = target.capabilities?.capabilities[kind];
34
+ if (capability?.supported)
35
+ return null;
36
+ const why = target.capabilities === null ? `harness ${target.run.harness} is not known to this core` : (capability?.reason ?? "not supported");
37
+ return unsupported(kind, `The ${target.run.harness} harness does not support ${kind}: ${why}.`);
38
+ }
39
+ /** Step 6: the state refusal, or null. */
40
+ function stateRefusal(kind, run) {
41
+ if (kind === "steer" && run.state !== "running") {
42
+ return new ControlError("conflict", `A steer needs a running run; run ${run.id} is ${run.state}.`, { run_id: run.id, retryable: run.state === "queued" });
43
+ }
44
+ if (kind === "cancel" && !isActive(run.state)) {
45
+ return new ControlError("conflict", `A cancel needs an active run; run ${run.id} is ${run.state}.`, { run_id: run.id, retryable: false });
46
+ }
47
+ return null;
48
+ }
49
+ /**
50
+ * Have the worker write `run.refused`; a failure to record never changes the refusal itself.
51
+ * Nothing is written for a terminal run or a run this core holds no live control of.
52
+ */
53
+ async function recordRefusal(target, action, error) {
54
+ if (target.worker === null || target.run.terminal || !target.run.live_control)
55
+ return;
56
+ try {
57
+ await target.worker.refuse({ ...action, outcome: "refused", error });
58
+ }
59
+ catch {
60
+ // The worker is unreachable or already done; the caller still gets the typed refusal.
61
+ }
62
+ }
63
+ /** Step 4: record the server's refusal of a deferred action (`pause`, `resume`, `answer`). Never throws. */
64
+ export async function recordDeferredRefusal(kind, target, request, error) {
65
+ await recordRefusal(target, record(target.run, kind, request, target.now), error);
66
+ }
67
+ function replyError(run, reply) {
68
+ if (reply.ok)
69
+ return null;
70
+ const body = reply.error;
71
+ // The worker re-checks state; a race (the run moved on) comes back as its own `conflict`.
72
+ if (body.code === "conflict")
73
+ return new ControlError("conflict", body.message, { run_id: run.id, retryable: body.retryable });
74
+ return unavailable(`The worker of run ${run.id} did not accept the action: ${body.message}`);
75
+ }
76
+ /** Run steps 5 to 9 for one action and return the accepted record, or throw the typed refusal. */
77
+ export async function control(kind, target, request) {
78
+ const { run } = target;
79
+ const action = record(run, kind, request, target.now);
80
+ const refusal = capabilityRefusal(kind, target) ?? stateRefusal(kind, run);
81
+ if (refusal !== null) {
82
+ await recordRefusal(target, action, refusal.body);
83
+ throw refusal;
84
+ }
85
+ if (!run.live_control || target.worker === null) {
86
+ throw unsupported("reconnect", `Run ${run.id} survived a core restart; this core cannot reconnect to its worker, so live control is unavailable.`);
87
+ }
88
+ let reply;
89
+ try {
90
+ reply =
91
+ kind === "steer"
92
+ ? await target.worker.steer(action, request.message)
93
+ : await target.worker.cancel(action, request.reason);
94
+ }
95
+ catch (err) {
96
+ if (err instanceof ControlUnavailableError)
97
+ throw unavailable(`The worker of run ${run.id} did not answer: ${err.message}. The ${kind} may still take effect; check the run's events before sending it again.`);
98
+ throw err;
99
+ }
100
+ const failed = replyError(run, reply);
101
+ if (failed !== null)
102
+ throw failed;
103
+ return { ...action, outcome: "accepted" };
104
+ }
105
+ //# sourceMappingURL=control.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"control.js","sourceRoot":"","sources":["../../../src/runs/control.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EACL,aAAa,EAEb,QAAQ,EACR,OAAO,GAQR,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAE7E,OAAO,EAAE,uBAAuB,EAAqB,MAAM,sBAAsB,CAAC;AAclF,SAAS,MAAM,CAAC,GAAQ,EAA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AAC,CAAC;IAC9E,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,GAAG,YAAY,uBAAuB;YAAE,MAAM,WAAW,CAAC,qBAAqB,GAAG,CAAC,EAAE,oBAAoB,GAAG,CAAC,OAAO,SAAS,IAAI,yEAAyE,CAAC,CAAC;QAChN,MAAM,GAAG,CAAC;IACZ,CAAC;IACD,MAAM,MAAM,GAAG,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACtC,IAAI,MAAM,KAAK,IAAI;QAAE,MAAM,MAAM,CAAC;IAClC,OAAO,EAAE,GAAG,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;AAC5C,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Run orchestration: the daemon's `RunService` over the run store, the per-run workers and the
3
+ * harness catalog port.
4
+ */
5
+ export { emptyCatalog } from "./ports.js";
6
+ export type { HarnessCatalog, HarnessEntry } from "./ports.js";
7
+ export { StoreRunService, createRunService } from "./service.js";
8
+ export type { RunServiceOptions } from "./service.js";
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Run orchestration: the daemon's `RunService` over the run store, the per-run workers and the
3
+ * harness catalog port.
4
+ */
5
+ export { emptyCatalog } from "./ports.js";
6
+ export { StoreRunService, createRunService } from "./service.js";
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/runs/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE1C,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The harness port of the run service. The run service knows a harness only through this shape:
3
+ * its id, its measured capability document, and the compiled driver module a worker loads for it.
4
+ * The real catalog lives elsewhere and is connected by `daemon/main.ts`; nothing here imports it.
5
+ */
6
+ import type { Capabilities } from "../contract/index.js";
7
+ export interface HarnessEntry {
8
+ /** The harness id as submitted (`harness` in the request), for example `opencode`. */
9
+ id: string;
10
+ /** The capability document as this core drives the harness, measured now (README: honest refusal). */
11
+ capabilities(): Promise<Capabilities>;
12
+ driver: {
13
+ /** Absolute path of a compiled ES module exporting `createDriver`. */
14
+ module: string;
15
+ /** JSON-serializable options passed to `createDriver` through the run's `driver.json`. */
16
+ options: Record<string, unknown>;
17
+ };
18
+ }
19
+ export interface HarnessCatalog {
20
+ list(): HarnessEntry[];
21
+ get(id: string): HarnessEntry | undefined;
22
+ /**
23
+ * Delete whatever the harnesses left running for these finished runs (a sandbox whose deletion
24
+ * failed, or whose worker was killed). The run service leaves out finished runs whose sandbox is
25
+ * retained (`retained.json`) and not yet expired. Returns the run ids something was deleted for. Optional:
26
+ * a catalog without workspaces of its own has nothing to reclaim.
27
+ */
28
+ reclaim?(finishedRunIds: string[]): Promise<string[]>;
29
+ }
30
+ /** A catalog with no harness: every submit is refused with `not_found` for an unknown harness. */
31
+ export declare function emptyCatalog(): HarnessCatalog;
@@ -0,0 +1,5 @@
1
+ /** A catalog with no harness: every submit is refused with `not_found` for an unknown harness. */
2
+ export function emptyCatalog() {
3
+ return { list: () => [], get: () => undefined };
4
+ }
5
+ //# sourceMappingURL=ports.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ports.js","sourceRoot":"","sources":["../../../src/runs/ports.ts"],"names":[],"mappings":"AAgCA,kGAAkG;AAClG,MAAM,UAAU,YAAY;IAC1B,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,SAAS,EAAE,CAAC;AAClD,CAAC"}
@@ -0,0 +1,38 @@
1
+ import { type Run, type RunId, type RunResult } from "../contract/index.js";
2
+ export interface ScanOptions {
3
+ stateDir: string;
4
+ /** Whether this core holds live control of the run's worker. */
5
+ controllable: (runId: RunId) => boolean;
6
+ /** Where to report a run that cannot be read (corrupt log or record). */
7
+ warn: (line: string) => void;
8
+ }
9
+ /** A run directory exists but its record or log cannot be read. The run is not "absent": it is unreadable. */
10
+ export declare class UnreadableRunError extends Error {
11
+ readonly runId: string;
12
+ readonly detail: string;
13
+ readonly name = "UnreadableRunError";
14
+ constructor(runId: string, detail: string);
15
+ }
16
+ /**
17
+ * One run as stored now, settling it when orphaned; null when unknown or not established. A run
18
+ * whose stored state cannot be read is skipped with a warning, or thrown as `UnreadableRunError`
19
+ * with `strict` (a caller asking for that one run must not be told it does not exist).
20
+ */
21
+ export declare function readRun(options: ScanOptions, runId: string, strict?: boolean): Run | null;
22
+ /**
23
+ * An unreadable run that may still be working in `repo`: its worker is alive, and its record names
24
+ * this repository or cannot be read either. Such a run keeps the repository busy; treating it as
25
+ * absent would let a second worker into the same repository.
26
+ */
27
+ export declare function unreadableRunHolding(options: ScanOptions, repo: string): string | null;
28
+ /** Every established run in the store, newest first by `created_at` (README: runs.list). */
29
+ export declare function scanRuns(options: ScanOptions): Run[];
30
+ /** The active run holding a canonical repository, if any. A run whose worker survived a restart counts. */
31
+ export declare function activeRunFor(options: ScanOptions, repo: string): Run | null;
32
+ /**
33
+ * The outcome of a terminal run, from its log. `summary` is the concatenation of the harness's
34
+ * `text` output deltas (null when there were none); `error` is the last fatal `run.error`, and a
35
+ * `failed` run without one reports its state reason as `internal`. `branch` and `commits` are what
36
+ * the worker recorded when it collected the run's commits; a run that collected none has neither.
37
+ */
38
+ export declare function resultOf(options: ScanOptions, run: Run): RunResult;
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Store reads for the run service. Every run reported here is derived from its on-disk event log
3
+ * and its worker's liveness (`deriveRun`); nothing is inferred another way. Reads settle an
4
+ * orphaned run (no live worker, no terminal event) by appending `unknown`, because the core is
5
+ * then the log's only writer (README: `unknown`).
6
+ */
7
+ import { existsSync, readFileSync } from "node:fs";
8
+ import { isActive, isRunId, } from "../contract/index.js";
9
+ import { readLog } from "../store/event-log.js";
10
+ import { runDir, runFile } from "../store/files.js";
11
+ import { checkLiveness } from "../store/liveness.js";
12
+ import { listRunIds, newestFirst, showRunAt } from "../store/run-store.js";
13
+ /** A run directory exists but its record or log cannot be read. The run is not "absent": it is unreadable. */
14
+ export class UnreadableRunError extends Error {
15
+ runId;
16
+ detail;
17
+ name = "UnreadableRunError";
18
+ constructor(runId, detail) {
19
+ super(`run ${runId} cannot be read: ${detail}`);
20
+ this.runId = runId;
21
+ this.detail = detail;
22
+ }
23
+ }
24
+ /**
25
+ * One run as stored now, settling it when orphaned; null when unknown or not established. A run
26
+ * whose stored state cannot be read is skipped with a warning, or thrown as `UnreadableRunError`
27
+ * with `strict` (a caller asking for that one run must not be told it does not exist).
28
+ */
29
+ export function readRun(options, runId, strict = false) {
30
+ if (!isRunId(runId))
31
+ return null;
32
+ const dir = runDir(options.stateDir, runId);
33
+ if (!existsSync(dir))
34
+ return null;
35
+ try {
36
+ return showRunAt(dir, { controllable: options.controllable(runId), settle: true })?.run ?? null;
37
+ }
38
+ catch (err) {
39
+ const detail = err instanceof Error ? err.message : String(err);
40
+ if (strict)
41
+ throw new UnreadableRunError(runId, detail);
42
+ options.warn(`culture-agents core: run ${runId} cannot be read: ${detail}`);
43
+ return null;
44
+ }
45
+ }
46
+ /**
47
+ * An unreadable run that may still be working in `repo`: its worker is alive, and its record names
48
+ * this repository or cannot be read either. Such a run keeps the repository busy; treating it as
49
+ * absent would let a second worker into the same repository.
50
+ */
51
+ export function unreadableRunHolding(options, repo) {
52
+ for (const id of listRunIds(options.stateDir)) {
53
+ const dir = runDir(options.stateDir, id);
54
+ try {
55
+ showRunAt(dir, { controllable: false, settle: false });
56
+ continue;
57
+ }
58
+ catch {
59
+ // unreadable: decided below
60
+ }
61
+ let alive = true;
62
+ try {
63
+ alive = checkLiveness(dir).alive;
64
+ }
65
+ catch {
66
+ // liveness unknown counts as alive
67
+ }
68
+ if (!alive)
69
+ continue;
70
+ let recorded;
71
+ try {
72
+ recorded = JSON.parse(readFileSync(runFile(dir, "record"), "utf8")).repo;
73
+ }
74
+ catch {
75
+ return id;
76
+ }
77
+ if (typeof recorded !== "string" || recorded === repo)
78
+ return id;
79
+ }
80
+ return null;
81
+ }
82
+ /** Every established run in the store, newest first by `created_at` (README: runs.list). */
83
+ export function scanRuns(options) {
84
+ const runs = [];
85
+ for (const id of listRunIds(options.stateDir)) {
86
+ const run = readRun(options, id);
87
+ if (run !== null)
88
+ runs.push(run);
89
+ }
90
+ return runs.sort(newestFirst);
91
+ }
92
+ /** The active run holding a canonical repository, if any. A run whose worker survived a restart counts. */
93
+ export function activeRunFor(options, repo) {
94
+ return scanRuns(options).find((run) => run.repo === repo && isActive(run.state)) ?? null;
95
+ }
96
+ const SHA = /^[0-9a-f]{40}([0-9a-f]{24})?$/;
97
+ /** The commits the worker recorded for a run's branch, oldest first; empty when none were recorded or the file is unusable. */
98
+ function collectedCommits(dir, branch) {
99
+ if (branch === null)
100
+ return [];
101
+ try {
102
+ const value = JSON.parse(readFileSync(runFile(dir, "collected"), "utf8"));
103
+ if (value.branch !== branch || !Array.isArray(value.commits))
104
+ return [];
105
+ return value.commits.every((c) => typeof c === "string" && SHA.test(c)) ? value.commits : [];
106
+ }
107
+ catch {
108
+ return [];
109
+ }
110
+ }
111
+ /**
112
+ * The outcome of a terminal run, from its log. `summary` is the concatenation of the harness's
113
+ * `text` output deltas (null when there were none); `error` is the last fatal `run.error`, and a
114
+ * `failed` run without one reports its state reason as `internal`. `branch` and `commits` are what
115
+ * the worker recorded when it collected the run's commits; a run that collected none has neither.
116
+ */
117
+ export function resultOf(options, run) {
118
+ const dir = runDir(options.stateDir, run.id);
119
+ const log = readLog(dir, run.id);
120
+ let text = null;
121
+ let fatal = null;
122
+ for (const event of log.events) {
123
+ if (event.kind === "run.output") {
124
+ const data = event.data;
125
+ if (data.channel === "text" && typeof data.text === "string")
126
+ text = (text ?? "") + data.text;
127
+ }
128
+ else if (event.kind === "run.error") {
129
+ const data = event.data;
130
+ if (data.fatal)
131
+ fatal = data.error;
132
+ }
133
+ }
134
+ const state = run.state;
135
+ let error = state === "succeeded" ? null : fatal;
136
+ if (state === "failed" && error === null) {
137
+ error = { code: "internal", message: run.state_reason ?? "The run failed without a recorded error.", retryable: false };
138
+ }
139
+ return {
140
+ run_id: run.id,
141
+ state,
142
+ summary: text,
143
+ branch: run.branch,
144
+ commits: collectedCommits(dir, run.branch),
145
+ ended_at: run.ended_at ?? run.updated_at,
146
+ error,
147
+ }; // `state` is a union value here; TS cannot correlate it with the `error` variants
148
+ }
149
+ //# sourceMappingURL=scan.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scan.js","sourceRoot":"","sources":["../../../src/runs/scan.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEnD,OAAO,EACL,QAAQ,EACR,OAAO,GAQR,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAU3E,8GAA8G;AAC9G,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAGhC;IACA;IAHO,IAAI,GAAG,oBAAoB,CAAC;IAC9C,YACW,KAAa,EACb,MAAc;QAEvB,KAAK,CAAC,OAAO,KAAK,oBAAoB,MAAM,EAAE,CAAC,CAAC;QAHvC,UAAK,GAAL,KAAK,CAAQ;QACb,WAAM,GAAN,MAAM,CAAQ;IAGzB,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,UAAU,OAAO,CAAC,OAAoB,EAAE,KAAa,EAAE,MAAM,GAAG,KAAK;IACzE,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IACjC,MAAM,GAAG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AAI,KAAK,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;YACtC,MAAM,IAAI,GAAG,KAAK,CAAC,IAAiB,CAAC;YACrC,IAAI,IAAI,CAAC,KAAK;gBAAE,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACrC,CAAC;IACH,CAAC;IACD,MAAM,KAAK,GAAG,GAAG,CAAC,KAAsB,CAAC;IACzC,IAAI,KAAK,GAAqB,KAAK,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IACnE,IAAI,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACzC,KAAK,GAAG,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,CAAC,YAAY,IAAI,0CAA0C,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAC1H,CAAC;IACD,OAAO;QACL,MAAM,EAAE,GAAG,CAAC,EAAE;QACd,KAAK;QACL,OAAO,EAAE,IAAI;QACb,MAAM,EAAE,GAAG,CAAC,MAAM;QAClB,OAAO,EAAE,gBAAgB,CAAC,GAAG,EAAE,GAAG,CAAC,MAAM,CAAC;QAC1C,QAAQ,EAAE,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,UAAU;QACxC,KAAK;KACO,CAAC,CAAC,kFAAkF;AACpG,CAAC"}
@@ -0,0 +1,70 @@
1
+ /**
2
+ * The run service: the daemon's `RunService` over the run store, per-run worker processes and the
3
+ * harness catalog port. It performs the service-side steps of contract/README.md's order of checks.
4
+ *
5
+ * Live control is held only for workers this process spawned. After a core restart the map is
6
+ * empty, so a surviving worker's run reports `live_control: false` and its steer and cancel are
7
+ * refused naming `reconnect`; the worker is never signalled. Every reported state is derived from
8
+ * the store (scan.ts), and a run with neither a live worker nor a terminal event settles `unknown`.
9
+ */
10
+ import { type AcceptedResponse, type Agent, type AgentList, type CancelRequest, type Capabilities, type ErrorBody, type EventPage, type Run, type RunEvent, type RunResult, type SteerRequest, type SubmitRequest } from "../contract/index.js";
11
+ import type { AllowlistState } from "../daemon/allowlist.js";
12
+ import type { DeferredKind, DeferredRequest, RunListQuery, RunService } from "../daemon/ports.js";
13
+ import type { HarnessCatalog } from "./ports.js";
14
+ export interface RunServiceOptions {
15
+ /** The run store's state directory. */
16
+ stateDir: string;
17
+ /** This core's node name (the `<node>` of local agents). */
18
+ node: string;
19
+ catalog: HarnessCatalog;
20
+ /** The allow-list state as the daemon reads it (agents are the agents it names). */
21
+ allowlist: () => AllowlistState;
22
+ now?: () => Date;
23
+ /** Passed to every worker. */
24
+ worker?: {
25
+ heartbeatMs?: number;
26
+ cancelGraceMs?: number;
27
+ readyTimeoutMs?: number;
28
+ env?: NodeJS.ProcessEnv;
29
+ };
30
+ /** Poll interval of a live event stream. Default 20 ms. */
31
+ followPollMs?: number;
32
+ warn?: (line: string) => void;
33
+ }
34
+ export declare class StoreRunService implements RunService {
35
+ private readonly options;
36
+ private readonly controls;
37
+ private readonly scan;
38
+ private readonly now;
39
+ /** Serializes submits so the busy check and the spawn are one step per repository. */
40
+ private submitChain;
41
+ constructor(options: RunServiceOptions);
42
+ /**
43
+ * Startup reconciliation: rebuild every run from disk. Orphaned runs (no live worker, no
44
+ * terminal event) get `unknown` appended; surviving workers are only read, never signalled, and
45
+ * report `live_control: false`. Existing events are never rewritten. Returns the runs.
46
+ */
47
+ reconcile(): Run[];
48
+ /** Ask the harnesses to delete what finished runs of this store left behind (README: cancel, sandbox). */
49
+ reclaim(): Promise<string[]>;
50
+ submit(request: SubmitRequest): Promise<Run>;
51
+ private submitNow;
52
+ listRuns(query: RunListQuery): Promise<Run[]>;
53
+ /** One run, or null when there is none. A run that exists but cannot be read is `internal` on every route, never "no such run". */
54
+ private read;
55
+ getRun(runId: string): Promise<Run | null>;
56
+ readEvents(runId: string, after: number, limit: number): Promise<EventPage | null>;
57
+ streamEvents(runId: string, after: number, signal: AbortSignal): AsyncIterable<RunEvent>;
58
+ getResult(runId: string): Promise<RunResult>;
59
+ steer(runId: string, request: SteerRequest): Promise<AcceptedResponse["action"]>;
60
+ cancel(runId: string, request: CancelRequest): Promise<AcceptedResponse["action"]>;
61
+ private act;
62
+ /** Step 4, decided by the server: write the refusal to the log of a live run this core controls. Never rejects. */
63
+ recordRefusal(runId: string, kind: DeferredKind, request: DeferredRequest, error: ErrorBody): Promise<void>;
64
+ listAgents(): Promise<AgentList>;
65
+ getAgent(agentId: string): Promise<Agent | null>;
66
+ listHarnesses(): Promise<Capabilities[]>;
67
+ getHarness(harness: string): Promise<Capabilities | null>;
68
+ }
69
+ /** A run service; no I/O until used. Call `reconcile()` once the daemon holds its lock. */
70
+ export declare function createRunService(options: RunServiceOptions): StoreRunService;