@relayflows/sdk 2.0.30 → 2.0.32

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 (347) hide show
  1. package/dist/agent-artifacts.d.ts.map +1 -1
  2. package/dist/agent-artifacts.js +2 -9
  3. package/dist/agent-artifacts.js.map +1 -1
  4. package/dist/agent-cwd.d.ts +69 -0
  5. package/dist/agent-cwd.d.ts.map +1 -0
  6. package/dist/agent-cwd.js +140 -0
  7. package/dist/agent-cwd.js.map +1 -0
  8. package/dist/artifact-scan-policy.d.ts +35 -0
  9. package/dist/artifact-scan-policy.d.ts.map +1 -0
  10. package/dist/artifact-scan-policy.js +49 -0
  11. package/dist/artifact-scan-policy.js.map +1 -0
  12. package/dist/authored-completion-record.d.ts +35 -0
  13. package/dist/authored-completion-record.d.ts.map +1 -0
  14. package/dist/authored-completion-record.js +92 -0
  15. package/dist/authored-completion-record.js.map +1 -0
  16. package/dist/authored-completion.d.ts +99 -0
  17. package/dist/authored-completion.d.ts.map +1 -0
  18. package/dist/authored-completion.js +184 -0
  19. package/dist/authored-completion.js.map +1 -0
  20. package/dist/authored-flow-error.d.ts +13 -1
  21. package/dist/authored-flow-error.d.ts.map +1 -1
  22. package/dist/authored-flow-error.js +12 -0
  23. package/dist/authored-flow-error.js.map +1 -1
  24. package/dist/authored-flow-executor.d.ts +7 -30
  25. package/dist/authored-flow-executor.d.ts.map +1 -1
  26. package/dist/authored-flow-executor.js +54 -47
  27. package/dist/authored-flow-executor.js.map +1 -1
  28. package/dist/authored-human.d.ts.map +1 -1
  29. package/dist/authored-human.js +1 -3
  30. package/dist/authored-human.js.map +1 -1
  31. package/dist/authored-node-entry.js +4 -1
  32. package/dist/authored-node-entry.js.map +1 -1
  33. package/dist/authored-node-runner.d.ts +9 -1
  34. package/dist/authored-node-runner.d.ts.map +1 -1
  35. package/dist/authored-node-runner.js +92 -17
  36. package/dist/authored-node-runner.js.map +1 -1
  37. package/dist/authored-preflight.d.ts +4 -0
  38. package/dist/authored-preflight.d.ts.map +1 -0
  39. package/dist/authored-preflight.js +50 -0
  40. package/dist/authored-preflight.js.map +1 -0
  41. package/dist/authored-root.d.ts +5 -0
  42. package/dist/authored-root.d.ts.map +1 -1
  43. package/dist/authored-root.js +20 -2
  44. package/dist/authored-root.js.map +1 -1
  45. package/dist/authored-step-output.d.ts +35 -10
  46. package/dist/authored-step-output.d.ts.map +1 -1
  47. package/dist/authored-step-output.js +108 -40
  48. package/dist/authored-step-output.js.map +1 -1
  49. package/dist/authored-verdict.d.ts +28 -0
  50. package/dist/authored-verdict.d.ts.map +1 -0
  51. package/dist/authored-verdict.js +79 -0
  52. package/dist/authored-verdict.js.map +1 -0
  53. package/dist/authored-worker-step.d.ts +5 -2
  54. package/dist/authored-worker-step.d.ts.map +1 -1
  55. package/dist/authored-worker-step.js +51 -13
  56. package/dist/authored-worker-step.js.map +1 -1
  57. package/dist/cli/check-worker-surface.d.ts +27 -0
  58. package/dist/cli/check-worker-surface.d.ts.map +1 -0
  59. package/dist/cli/check-worker-surface.js +63 -0
  60. package/dist/cli/check-worker-surface.js.map +1 -0
  61. package/dist/cli/check.d.ts +28 -3
  62. package/dist/cli/check.d.ts.map +1 -1
  63. package/dist/cli/check.js +27 -140
  64. package/dist/cli/check.js.map +1 -1
  65. package/dist/cli/cli-probe.d.ts +20 -0
  66. package/dist/cli/cli-probe.d.ts.map +1 -0
  67. package/dist/cli/cli-probe.js +166 -0
  68. package/dist/cli/cli-probe.js.map +1 -0
  69. package/dist/cli/cloud-format.d.ts +9 -0
  70. package/dist/cli/cloud-format.d.ts.map +1 -0
  71. package/dist/cli/cloud-format.js +77 -0
  72. package/dist/cli/cloud-format.js.map +1 -0
  73. package/dist/cli/cloud-live.d.ts +39 -0
  74. package/dist/cli/cloud-live.d.ts.map +1 -0
  75. package/dist/cli/cloud-live.js +325 -0
  76. package/dist/cli/cloud-live.js.map +1 -0
  77. package/dist/cli/cloud-mirror-session.d.ts +97 -0
  78. package/dist/cli/cloud-mirror-session.d.ts.map +1 -0
  79. package/dist/cli/cloud-mirror-session.js +264 -0
  80. package/dist/cli/cloud-mirror-session.js.map +1 -0
  81. package/dist/cli/cloud-read.d.ts +3 -7
  82. package/dist/cli/cloud-read.d.ts.map +1 -1
  83. package/dist/cli/cloud-read.js +14 -266
  84. package/dist/cli/cloud-read.js.map +1 -1
  85. package/dist/cli/cloud-refusal.d.ts +39 -0
  86. package/dist/cli/cloud-refusal.d.ts.map +1 -0
  87. package/dist/cli/cloud-refusal.js +95 -0
  88. package/dist/cli/cloud-refusal.js.map +1 -0
  89. package/dist/cli/cloud-status-view.d.ts +7 -0
  90. package/dist/cli/cloud-status-view.d.ts.map +1 -0
  91. package/dist/cli/cloud-status-view.js +214 -0
  92. package/dist/cli/cloud-status-view.js.map +1 -0
  93. package/dist/cli/direct-run.d.ts.map +1 -1
  94. package/dist/cli/direct-run.js +16 -2
  95. package/dist/cli/direct-run.js.map +1 -1
  96. package/dist/cli/hn-monitor.d.ts +0 -13
  97. package/dist/cli/hn-monitor.d.ts.map +1 -1
  98. package/dist/cli/hn-monitor.js +2 -1
  99. package/dist/cli/hn-monitor.js.map +1 -1
  100. package/dist/cli/local-agent-remedy.d.ts +80 -0
  101. package/dist/cli/local-agent-remedy.d.ts.map +1 -0
  102. package/dist/cli/local-agent-remedy.js +116 -0
  103. package/dist/cli/local-agent-remedy.js.map +1 -0
  104. package/dist/cli/observer-session.d.ts +41 -0
  105. package/dist/cli/observer-session.d.ts.map +1 -0
  106. package/dist/cli/observer-session.js +78 -0
  107. package/dist/cli/observer-session.js.map +1 -0
  108. package/dist/cli/run.d.ts +30 -2
  109. package/dist/cli/run.d.ts.map +1 -1
  110. package/dist/cli/run.js +186 -24
  111. package/dist/cli/run.js.map +1 -1
  112. package/dist/cli/status.d.ts +9 -0
  113. package/dist/cli/status.d.ts.map +1 -1
  114. package/dist/cli/status.js +31 -2
  115. package/dist/cli/status.js.map +1 -1
  116. package/dist/cli/step-evidence.d.ts +90 -0
  117. package/dist/cli/step-evidence.d.ts.map +1 -0
  118. package/dist/cli/step-evidence.js +259 -0
  119. package/dist/cli/step-evidence.js.map +1 -0
  120. package/dist/cli/step-failure.d.ts +14 -4
  121. package/dist/cli/step-failure.d.ts.map +1 -1
  122. package/dist/cli/step-failure.js +41 -76
  123. package/dist/cli/step-failure.js.map +1 -1
  124. package/dist/cli-commands.d.ts +12 -0
  125. package/dist/cli-commands.d.ts.map +1 -1
  126. package/dist/cli-commands.js +3 -0
  127. package/dist/cli-commands.js.map +1 -1
  128. package/dist/cli.d.ts +17 -0
  129. package/dist/cli.d.ts.map +1 -1
  130. package/dist/cli.js +134 -57
  131. package/dist/cli.js.map +1 -1
  132. package/dist/cloud-deploy.d.ts +1 -1
  133. package/dist/cloud-deploy.d.ts.map +1 -1
  134. package/dist/cloud-deploy.js +24 -8
  135. package/dist/cloud-deploy.js.map +1 -1
  136. package/dist/cloud-mirror-ledger.d.ts +40 -0
  137. package/dist/cloud-mirror-ledger.d.ts.map +1 -0
  138. package/dist/cloud-mirror-ledger.js +142 -0
  139. package/dist/cloud-mirror-ledger.js.map +1 -0
  140. package/dist/cloud-mirror-step.d.ts +168 -0
  141. package/dist/cloud-mirror-step.d.ts.map +1 -0
  142. package/dist/cloud-mirror-step.js +504 -0
  143. package/dist/cloud-mirror-step.js.map +1 -0
  144. package/dist/cloud-mirror-transport.d.ts +94 -0
  145. package/dist/cloud-mirror-transport.d.ts.map +1 -0
  146. package/dist/cloud-mirror-transport.js +163 -0
  147. package/dist/cloud-mirror-transport.js.map +1 -0
  148. package/dist/cloud-mirror.d.ts +132 -0
  149. package/dist/cloud-mirror.d.ts.map +1 -0
  150. package/dist/cloud-mirror.js +556 -0
  151. package/dist/cloud-mirror.js.map +1 -0
  152. package/dist/cloud-read.d.ts +15 -0
  153. package/dist/cloud-read.d.ts.map +1 -1
  154. package/dist/cloud-read.js +20 -1
  155. package/dist/cloud-read.js.map +1 -1
  156. package/dist/cloud-run-record.d.ts +27 -0
  157. package/dist/cloud-run-record.d.ts.map +1 -0
  158. package/dist/cloud-run-record.js +56 -0
  159. package/dist/cloud-run-record.js.map +1 -0
  160. package/dist/cloud-run.d.ts +2 -9
  161. package/dist/cloud-run.d.ts.map +1 -1
  162. package/dist/cloud-run.js +3 -20
  163. package/dist/cloud-run.js.map +1 -1
  164. package/dist/communication/local.d.ts.map +1 -1
  165. package/dist/communication/local.js +2 -1
  166. package/dist/communication/local.js.map +1 -1
  167. package/dist/communication/worker.d.ts +1 -1
  168. package/dist/communication/worker.d.ts.map +1 -1
  169. package/dist/communication/worker.js +13 -5
  170. package/dist/communication/worker.js.map +1 -1
  171. package/dist/compile.d.ts.map +1 -1
  172. package/dist/compile.js +28 -6
  173. package/dist/compile.js.map +1 -1
  174. package/dist/direct-input.d.ts.map +1 -1
  175. package/dist/direct-input.js +21 -1
  176. package/dist/direct-input.js.map +1 -1
  177. package/dist/failure-kinds.d.ts +96 -6
  178. package/dist/failure-kinds.d.ts.map +1 -1
  179. package/dist/failure-kinds.js +29 -2
  180. package/dist/failure-kinds.js.map +1 -1
  181. package/dist/flow-requirements.d.ts.map +1 -1
  182. package/dist/flow-requirements.js +24 -9
  183. package/dist/flow-requirements.js.map +1 -1
  184. package/dist/gate-contract.d.ts +7 -0
  185. package/dist/gate-contract.d.ts.map +1 -1
  186. package/dist/gate-contract.js +6 -1
  187. package/dist/gate-contract.js.map +1 -1
  188. package/dist/helper-preflight.d.ts.map +1 -1
  189. package/dist/helper-preflight.js +10 -3
  190. package/dist/helper-preflight.js.map +1 -1
  191. package/dist/helper-reference.d.ts +25 -0
  192. package/dist/helper-reference.d.ts.map +1 -0
  193. package/dist/helper-reference.js +129 -0
  194. package/dist/helper-reference.js.map +1 -0
  195. package/dist/hosted-extension-runtime.js +1 -1
  196. package/dist/index.d.ts +1 -1
  197. package/dist/index.d.ts.map +1 -1
  198. package/dist/index.js.map +1 -1
  199. package/dist/journal-client.d.ts +3 -1
  200. package/dist/journal-client.d.ts.map +1 -1
  201. package/dist/journal-client.js +17 -2
  202. package/dist/journal-client.js.map +1 -1
  203. package/dist/journal-projection.d.ts +16 -0
  204. package/dist/journal-projection.d.ts.map +1 -0
  205. package/dist/journal-projection.js +108 -0
  206. package/dist/journal-projection.js.map +1 -0
  207. package/dist/llm-worker.js +1 -1
  208. package/dist/llm-worker.js.map +1 -1
  209. package/dist/local-agent.d.ts +1 -1
  210. package/dist/local-agent.d.ts.map +1 -1
  211. package/dist/local-agent.js +4 -3
  212. package/dist/local-agent.js.map +1 -1
  213. package/dist/named-gate-lowering.js +35 -5
  214. package/dist/named-gate-lowering.js.map +1 -1
  215. package/dist/named-gate-preflight.d.ts +27 -0
  216. package/dist/named-gate-preflight.d.ts.map +1 -0
  217. package/dist/named-gate-preflight.js +47 -0
  218. package/dist/named-gate-preflight.js.map +1 -0
  219. package/dist/named-gates.d.ts +1 -1
  220. package/dist/named-gates.d.ts.map +1 -1
  221. package/dist/named-gates.js +3 -0
  222. package/dist/named-gates.js.map +1 -1
  223. package/dist/observer-link.d.ts +7 -0
  224. package/dist/observer-link.d.ts.map +1 -1
  225. package/dist/observer-link.js +3 -1
  226. package/dist/observer-link.js.map +1 -1
  227. package/dist/package-version.d.ts +10 -0
  228. package/dist/package-version.d.ts.map +1 -0
  229. package/dist/package-version.js +22 -0
  230. package/dist/package-version.js.map +1 -0
  231. package/dist/preflight.d.ts +14 -0
  232. package/dist/preflight.d.ts.map +1 -1
  233. package/dist/preflight.js +25 -3
  234. package/dist/preflight.js.map +1 -1
  235. package/dist/protocol.d.ts +2 -0
  236. package/dist/protocol.d.ts.map +1 -1
  237. package/dist/redact.d.ts +42 -0
  238. package/dist/redact.d.ts.map +1 -1
  239. package/dist/redact.js +128 -0
  240. package/dist/redact.js.map +1 -1
  241. package/dist/relay-cli.d.ts.map +1 -1
  242. package/dist/relay-cli.js +1 -21
  243. package/dist/relay-cli.js.map +1 -1
  244. package/dist/run-projection.d.ts +83 -0
  245. package/dist/run-projection.d.ts.map +1 -0
  246. package/dist/run-projection.js +199 -0
  247. package/dist/run-projection.js.map +1 -0
  248. package/dist/shell-word.d.ts +19 -0
  249. package/dist/shell-word.d.ts.map +1 -0
  250. package/dist/shell-word.js +23 -0
  251. package/dist/shell-word.js.map +1 -0
  252. package/dist/spec.d.ts +52 -3
  253. package/dist/spec.d.ts.map +1 -1
  254. package/dist/spec.js.map +1 -1
  255. package/dist/step-fields.d.ts +1 -1
  256. package/dist/step-fields.js +1 -1
  257. package/dist/step-fields.js.map +1 -1
  258. package/dist/steps-green.d.ts +27 -0
  259. package/dist/steps-green.d.ts.map +1 -0
  260. package/dist/steps-green.js +164 -0
  261. package/dist/steps-green.js.map +1 -0
  262. package/dist/validate.d.ts.map +1 -1
  263. package/dist/validate.js +39 -8
  264. package/dist/validate.js.map +1 -1
  265. package/dist/worker-lease.d.ts +8 -1
  266. package/dist/worker-lease.d.ts.map +1 -1
  267. package/dist/worker-lease.js +34 -4
  268. package/dist/worker-lease.js.map +1 -1
  269. package/dist/worker.d.ts +8 -0
  270. package/dist/worker.d.ts.map +1 -1
  271. package/dist/worker.js +17 -9
  272. package/dist/worker.js.map +1 -1
  273. package/package.json +3 -7
  274. package/src/agent-artifacts.ts +2 -10
  275. package/src/agent-cwd.ts +139 -0
  276. package/src/artifact-scan-policy.ts +51 -0
  277. package/src/authored-completion-record.ts +119 -0
  278. package/src/authored-completion.ts +203 -0
  279. package/src/authored-flow-error.ts +13 -1
  280. package/src/authored-flow-executor.ts +91 -58
  281. package/src/authored-human.ts +1 -4
  282. package/src/authored-node-entry.ts +4 -1
  283. package/src/authored-node-runner.ts +96 -22
  284. package/src/authored-preflight.ts +46 -0
  285. package/src/authored-root.ts +20 -2
  286. package/src/authored-step-output.ts +139 -42
  287. package/src/authored-verdict.ts +87 -0
  288. package/src/authored-worker-step.ts +63 -14
  289. package/src/cli/check-worker-surface.ts +66 -0
  290. package/src/cli/check.ts +59 -166
  291. package/src/cli/cli-probe.ts +194 -0
  292. package/src/cli/cloud-format.ts +82 -0
  293. package/src/cli/cloud-live.ts +366 -0
  294. package/src/cli/cloud-mirror-session.ts +329 -0
  295. package/src/cli/cloud-read.ts +13 -282
  296. package/src/cli/cloud-refusal.ts +113 -0
  297. package/src/cli/cloud-status-view.ts +213 -0
  298. package/src/cli/direct-run.ts +19 -2
  299. package/src/cli/hn-monitor.ts +2 -1
  300. package/src/cli/local-agent-remedy.ts +160 -0
  301. package/src/cli/observer-session.ts +107 -0
  302. package/src/cli/run.ts +233 -28
  303. package/src/cli/status.ts +59 -3
  304. package/src/cli/step-evidence.ts +310 -0
  305. package/src/cli/step-failure.ts +57 -81
  306. package/src/cli-commands.ts +3 -0
  307. package/src/cli.ts +155 -60
  308. package/src/cloud-deploy.ts +26 -8
  309. package/src/cloud-mirror-ledger.ts +161 -0
  310. package/src/cloud-mirror-step.ts +618 -0
  311. package/src/cloud-mirror-transport.ts +261 -0
  312. package/src/cloud-mirror.ts +633 -0
  313. package/src/cloud-read.ts +25 -1
  314. package/src/cloud-run-record.ts +64 -0
  315. package/src/cloud-run.ts +5 -24
  316. package/src/communication/local.ts +2 -1
  317. package/src/communication/worker.ts +13 -5
  318. package/src/compile.ts +28 -6
  319. package/src/direct-input.ts +22 -1
  320. package/src/failure-kinds.ts +104 -2
  321. package/src/flow-requirements.ts +24 -9
  322. package/src/gate-contract.ts +13 -1
  323. package/src/helper-preflight.ts +10 -3
  324. package/src/helper-reference.ts +130 -0
  325. package/src/hosted-extension-runtime.ts +1 -1
  326. package/src/index.ts +2 -0
  327. package/src/journal-client.ts +19 -2
  328. package/src/journal-projection.ts +106 -0
  329. package/src/llm-worker.ts +1 -1
  330. package/src/local-agent.ts +4 -2
  331. package/src/named-gate-lowering.ts +35 -5
  332. package/src/named-gate-preflight.ts +47 -0
  333. package/src/named-gates.ts +3 -0
  334. package/src/observer-link.ts +10 -1
  335. package/src/package-version.ts +23 -0
  336. package/src/preflight.ts +38 -7
  337. package/src/protocol.ts +2 -0
  338. package/src/redact.ts +111 -0
  339. package/src/relay-cli.ts +1 -24
  340. package/src/run-projection.ts +247 -0
  341. package/src/shell-word.ts +24 -0
  342. package/src/spec.ts +54 -3
  343. package/src/step-fields.ts +1 -1
  344. package/src/steps-green.ts +168 -0
  345. package/src/validate.ts +35 -8
  346. package/src/worker-lease.ts +35 -5
  347. package/src/worker.ts +27 -11
@@ -0,0 +1,139 @@
1
+ import { realpathSync, statSync } from 'node:fs';
2
+ import { resolve, sep } from 'node:path';
3
+
4
+ /**
5
+ * Where an agent step runs.
6
+ *
7
+ * A declared `cwd` is a path relative to the **run root** — the working
8
+ * directory of the process running the agent worker, which is the directory
9
+ * `flows run` was invoked from and, on the Cloud path, the uploaded tree
10
+ * (docs/CLOUD.md, "Code sync"). Absent, a step runs in the run root itself.
11
+ *
12
+ * Two checks, in two places, because they are two different facts:
13
+ *
14
+ * - The **declaration** is lexical and filesystem-free, so `flows check`, the
15
+ * kernel's `validate()` and this worker all answer identically for the same
16
+ * spec. `testdata/agent-cwd-cases.json` is the shared corpus both dialects
17
+ * are tested against.
18
+ * - The **target** is resolved here, in the worker that spawns the CLI — the
19
+ * one process provably sharing the agent's filesystem. A directory that
20
+ * exists, and whose symlink-free path is inside the symlink-free run root.
21
+ * Repeated at execution even when a preflight already passed, because the
22
+ * filesystem moves underneath a spec.
23
+ *
24
+ * This contains a declaration; it does not sandbox an agent. Nothing stops a
25
+ * CLI from writing outside the directory it was started in. Per-step scoping
26
+ * is `permissions`, which is recorded and not enforced (gate 8 / #442).
27
+ */
28
+ export class AgentCwdError extends Error {
29
+ constructor(message: string) {
30
+ super(message);
31
+ this.name = 'AgentCwdError';
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Why a declared `cwd` is not a run-root-relative path, or `undefined` when it
37
+ * is one. Pure and lexical: the exact rule
38
+ * `relayflowd_core::spec::is_run_root_relative_path` applies at the kernel
39
+ * boundary, so a declaration that passes `flows check` is not refused later by
40
+ * the daemon for its shape.
41
+ */
42
+ export function agentCwdDeclarationError(cwd: unknown): string | undefined {
43
+ const expected = 'expected a run-root-relative path';
44
+ if (typeof cwd !== 'string') return `${expected} (got ${cwd === null ? 'null' : typeof cwd})`;
45
+ if (cwd === '') return `${expected}, not an empty string`;
46
+ if (PADDING.test(cwd)) return `${expected} without surrounding whitespace`;
47
+ if (cwd.includes('\0')) return `${expected} without NUL`;
48
+ if (cwd.startsWith('/')) return `${expected}, not an absolute path`;
49
+ if (cwd.includes('://')) return `${expected}, not a URI-like mount identity`;
50
+ if (cwd.split('/').some((part) => part === '' || part === '.' || part === '..')) {
51
+ return `${expected} without empty, "." or ".." components`;
52
+ }
53
+ return undefined;
54
+ }
55
+
56
+ /**
57
+ * Whitespace at either end, spelled so both dialects refuse the same set.
58
+ * JavaScript's `\s` is Unicode `White_Space` without U+0085 and with U+FEFF;
59
+ * Rust's `char::is_whitespace` is `White_Space` exactly. Naming U+0085 here
60
+ * and U+FEFF there makes both `White_Space ∪ {U+FEFF}`. Left to `trim()`
61
+ * alone, a path padded with either character would pass `flows check` and be
62
+ * refused by the kernel — the accept-then-refuse split this contract closes.
63
+ */
64
+ const PADDING = /^[\s\u0085]|[\s\u0085]$/u;
65
+
66
+ /**
67
+ * The refusal for a `cwd` declared on a step dispatched over the relay
68
+ * transport, or `undefined` when there is nothing to refuse.
69
+ *
70
+ * The relay agent runs on another host. This process can neither resolve that
71
+ * host's filesystem nor establish that the directory is inside that run's
72
+ * tree, and forwarding the string as `worker_cwd` would let a declaration this
73
+ * contract promises to contain go unchecked. Fail closed instead of pretending.
74
+ */
75
+ export function agentCwdTransportError(cwd: unknown, transport: unknown): string | undefined {
76
+ if (cwd === undefined || transport !== 'relay') return undefined;
77
+ return 'cwd is not supported with transport "relay": the agent runs on another host, '
78
+ + 'where this worker cannot resolve the directory or hold it inside the run root';
79
+ }
80
+
81
+ /**
82
+ * The directory to spawn the CLI in: `root` when nothing is declared, else the
83
+ * symlink-free directory `cwd` names beneath `root`.
84
+ *
85
+ * @throws AgentCwdError naming the declaration and what is wrong with it.
86
+ */
87
+ export function resolveAgentCwd(root: string, cwd: unknown, transport?: unknown): string | undefined {
88
+ if (cwd === undefined) return undefined;
89
+ const refusal = (detail: string): AgentCwdError =>
90
+ new AgentCwdError(`cwd ${JSON.stringify(cwd)}: ${detail}`);
91
+ const declaration = agentCwdDeclarationError(cwd);
92
+ if (declaration !== undefined) throw refusal(declaration);
93
+ const unsupported = agentCwdTransportError(cwd, transport);
94
+ if (unsupported !== undefined) throw new AgentCwdError(unsupported);
95
+
96
+ let realRoot: string;
97
+ try {
98
+ realRoot = realpathSync(root);
99
+ } catch {
100
+ throw refusal(`the run root ${JSON.stringify(root)} cannot be resolved on this host`);
101
+ }
102
+ let target: string;
103
+ try {
104
+ target = realpathSync(resolve(realRoot, cwd as string));
105
+ } catch {
106
+ throw refusal(`no such directory under the run root ${JSON.stringify(realRoot)}`);
107
+ }
108
+ // Component-aware, on the symlink-free forms of both: `checkout-b-old` is
109
+ // not inside `checkout-b`, and a symlink pointing out of the tree is not
110
+ // inside it either however it is spelled.
111
+ const prefix = realRoot.endsWith(sep) ? realRoot : realRoot + sep;
112
+ if (target !== realRoot && !target.startsWith(prefix)) {
113
+ throw refusal(`resolves to ${JSON.stringify(target)}, outside the run root ${JSON.stringify(realRoot)}`);
114
+ }
115
+ if (!statSync(target).isDirectory()) throw refusal(`${JSON.stringify(target)} is not a directory`);
116
+ return target;
117
+ }
118
+
119
+ /** Either the directory an agent step runs in, or why the step is refused. */
120
+ export type AgentCwdOutcome = { directory: string | undefined } | { refusal: string };
121
+
122
+ /**
123
+ * The dispatch-time form: resolve a dispatched agent step's `cwd` against the
124
+ * run root, or report the refusal for the worker to complete the step with.
125
+ * Reporting rather than throwing keeps the refusal on the step's completion —
126
+ * `worker_error`, with the reason journaled — instead of on the worker.
127
+ */
128
+ export function agentStepCwd(
129
+ spec: { cwd?: unknown; transport?: unknown },
130
+ stepId: string,
131
+ root: string = process.cwd(),
132
+ ): AgentCwdOutcome {
133
+ try {
134
+ return { directory: resolveAgentCwd(root, spec.cwd, spec.transport) };
135
+ } catch (error) {
136
+ if (!(error instanceof AgentCwdError)) throw error;
137
+ return { refusal: `agent step "${stepId}": ${error.message}` };
138
+ }
139
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The bundled agent worker's artifact-scan policy, as a pure predicate.
3
+ *
4
+ * `agent-artifacts.ts` walks an agent step's cwd and records every regular
5
+ * file it finds, except entries named exactly `.git`, `.relayflowd` or
6
+ * `node_modules` — files as well as directories, in both cases. Dotfiles and
7
+ * dot-directories ARE eligible: `.workflow-artifacts/` is the conventional
8
+ * place a flow tells its agents to write. What that walk records is one
9
+ * source of the journaled `output.artifacts` list, which is the only thing an
10
+ * `artifact_exists` gate reads (`named-gate-lowering.ts` tests exact
11
+ * membership in that list and never touches disk).
12
+ *
13
+ * Static analysis needs the same rule without importing filesystem traversal,
14
+ * so the rule lives here and both consume it. It bounds the *scan*, not the
15
+ * list: `worker.ts` journals object-shaped JSON stdout and completed Relay
16
+ * task output verbatim instead of the scanned wrapper, and
17
+ * `JournalClient.stepComplete` accepts any `output` from any worker — so a
18
+ * path this predicate excludes can still reach the list by those routes
19
+ * (`named-gate-preflight.ts` warns rather than refusing for exactly that
20
+ * reason).
21
+ */
22
+
23
+ /**
24
+ * Entry names the walk never descends into or records, matched exactly, at
25
+ * any depth, before the entry's type is consulted — so a `.git` *file* (a
26
+ * worktree's gitdir pointer) is skipped as surely as a `.git` directory.
27
+ * Exact names, not prefixes: `.github`, `.relayflowd-notes` and every other
28
+ * author-chosen name that merely starts the same way stays eligible.
29
+ */
30
+ const SKIPPED_ENTRY_NAMES = new Set(['.git', '.relayflowd', 'node_modules']);
31
+
32
+ export function isUnscannedEntryName(name: string): boolean {
33
+ return SKIPPED_ENTRY_NAMES.has(name);
34
+ }
35
+
36
+ /**
37
+ * The shortest `/`-prefix of a relative POSIX `path` the scan excludes, or
38
+ * `undefined` when every segment is scanned.
39
+ *
40
+ * Segments are compared exactly, with no normalization: the gate matches the
41
+ * author's literal string against the worker's literal list, so `node_modules`
42
+ * is excluded and `node_modules-copy` is not, and a backslash is an ordinary
43
+ * filename character rather than a separator. The whole prefix is returned,
44
+ * not the offending segment alone, because that prefix is the directory an
45
+ * author has to move the artifact out of.
46
+ */
47
+ export function unscannedArtifactPrefix(path: string): string | undefined {
48
+ const segments = path.split('/');
49
+ const index = segments.findIndex(isUnscannedEntryName);
50
+ return index === -1 ? undefined : segments.slice(0, index + 1).join('/');
51
+ }
@@ -0,0 +1,119 @@
1
+ // The authored verdict, written to the root BEFORE the marker is admitted.
2
+ //
3
+ // The terminal marker's command embeds the detail, and the marker run is
4
+ // opened under a stable admission key (`authored-child:<hash(root, step)>`).
5
+ // Those two facts together mean the detail has to be a durable decision, not a
6
+ // recomputed one: normalization redacts against `process.env`, so a credential
7
+ // rotated or removed while this process was down makes the SAME authored
8
+ // sentence normalize differently. A body resumed into that environment would
9
+ // retry the marker's admission key with a drifted spec, and the kernel refuses
10
+ // that as `run_admission_conflict` — losing an explanation the root had
11
+ // already journaled, for a flow whose every step succeeded.
12
+ //
13
+ // So the verdict is appended to a stream on the root run before the marker run
14
+ // is opened, and a later attempt reaching the same terminal step reuses what
15
+ // is recorded. That is exactly the durability the executor already gives a
16
+ // predicate gate (`applyPredicateGate`): record the decision first, so the
17
+ // spec built from it is identical on every attempt and author code is never
18
+ // asked the question twice.
19
+ //
20
+ // Nothing is recorded for a `done()` with no detail. Without one the marker
21
+ // command is a function of the reason alone — no environment, no drift, and
22
+ // nothing to recover — so a flow that does not opt in writes exactly the
23
+ // journal it has always written.
24
+
25
+ import {
26
+ isDurableCompletionDetail,
27
+ isLoweredCompletion,
28
+ type LoweredCompletionReason,
29
+ } from './authored-completion.js';
30
+ import type { JournalClient } from './journal-client.js';
31
+
32
+ export const AUTHORED_VERDICT_STREAM = 'authored-verdict';
33
+
34
+ /** Versioned so a reader can tell this record from a future one. */
35
+ const RECORD_KIND = 'relayflows.authored-verdict.v1';
36
+
37
+ export interface AuthoredVerdictRecord {
38
+ readonly reason: LoweredCompletionReason;
39
+ /** Recorded only when the body passed one; see the module comment. */
40
+ readonly detail?: string;
41
+ }
42
+
43
+ /**
44
+ * Commit this body's verdict, or recover the one this root already committed.
45
+ *
46
+ * Returns the verdict the marker must be built from — the recorded one when
47
+ * the root holds it, this attempt's otherwise. With no root run there is
48
+ * nothing durable to write to (the non-durable executor seam), so that path
49
+ * passes straight through without a round trip. A root run is always read
50
+ * first: a committed record stays authoritative even for an attempt that has
51
+ * no detail of its own — an optional source can be present on the first
52
+ * execution and gone on the retry, and "this attempt has no detail" is not
53
+ * "this root never recorded one". Only when no record exists AND this attempt
54
+ * has no detail does it return without appending, preserving the no-detail
55
+ * marker, spec hash, and journal exactly as before.
56
+ */
57
+ export async function commitAuthoredVerdict(
58
+ journal: JournalClient,
59
+ rootRunId: string | undefined,
60
+ step: string,
61
+ verdict: AuthoredVerdictRecord,
62
+ ): Promise<AuthoredVerdictRecord> {
63
+ if (rootRunId === undefined) return verdict;
64
+ const committed = await readAuthoredVerdict(journal, rootRunId, step);
65
+ if (committed !== undefined) return committed;
66
+ if (verdict.detail === undefined) return verdict;
67
+ await journal.streamAppend(rootRunId, AUTHORED_VERDICT_STREAM, {
68
+ verdict: RECORD_KIND, step, reason: verdict.reason, detail: verdict.detail,
69
+ });
70
+ return verdict;
71
+ }
72
+
73
+ /**
74
+ * The verdict this root recorded for `step`, or `undefined`.
75
+ *
76
+ * The FIRST valid record wins: it is the one the marker was — or was about to
77
+ * be — admitted from, and a later attempt's differently-redacted text must not
78
+ * displace it. Records this version does not recognise, and details that fail
79
+ * the durable check every other read boundary applies, are skipped rather than
80
+ * trusted; the marker then drifts and the kernel refuses it loudly, which is
81
+ * the right outcome for a record this process cannot read.
82
+ */
83
+ export async function readAuthoredVerdict(
84
+ journal: JournalClient,
85
+ rootRunId: string,
86
+ step: string,
87
+ ): Promise<AuthoredVerdictRecord | undefined> {
88
+ let offset = 0;
89
+ for (;;) {
90
+ const page = await journal.streamRead(rootRunId, AUTHORED_VERDICT_STREAM, offset, 1000);
91
+ for (const message of page.messages) {
92
+ // `stream.read` returns either the envelope or the bare message,
93
+ // matching how the executor reads predicate verdicts.
94
+ const raw = (message as { message?: unknown }).message ?? message;
95
+ if (isVerdictRecord(raw) && raw.step === step) {
96
+ return Object.freeze({ reason: raw.reason, detail: raw.detail });
97
+ }
98
+ }
99
+ if (page.messages.length === 0 || page.next_offset <= offset) break;
100
+ offset = page.next_offset;
101
+ }
102
+ return undefined;
103
+ }
104
+
105
+ interface StoredVerdict {
106
+ readonly verdict: typeof RECORD_KIND;
107
+ readonly step: string;
108
+ readonly reason: LoweredCompletionReason;
109
+ readonly detail: string;
110
+ }
111
+
112
+ function isVerdictRecord(value: unknown): value is StoredVerdict {
113
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) return false;
114
+ const record = value as Partial<StoredVerdict>;
115
+ return record.verdict === RECORD_KIND
116
+ && typeof record.step === 'string' && record.step.length > 0
117
+ && isLoweredCompletion(record.reason)
118
+ && isDurableCompletionDetail(record.detail);
119
+ }
@@ -0,0 +1,203 @@
1
+ // The authored verdict: which completions this runtime lowers, the durable
2
+ // marker that carries one, and the optional detail that says why.
3
+ //
4
+ // Split out of `authored-flow-executor.ts` so the CLI report and the status
5
+ // projection can read this vocabulary without importing the executor — and
6
+ // with it every provider adapter, MCP client and helper the executor pulls
7
+ // in. Nothing here does I/O or reads a clock.
8
+
9
+ import { AuthoredFlowExecutionError } from './authored-flow-error.js';
10
+ import { redact } from './redact.js';
11
+
12
+ /**
13
+ * The completion reasons an authored body may declare and this executor lowers.
14
+ *
15
+ * `FlowCompletionReason` is wider than this on purpose — it is the journal's
16
+ * run vocabulary plus authored verdicts — but the two sets drifting silently is
17
+ * exactly what made a type-valid `done("step_failed")` die at runtime as
18
+ * `unsupported_completion`. Every gate that asks "is this a completion this
19
+ * runtime can lower?" now asks this one function, so a reason cannot be
20
+ * accepted in one place and rejected in another.
21
+ *
22
+ * These are internal cross-module helpers for the authored seam (the executor,
23
+ * the durable root, the IPC verifier, the CLI report and `flows status`), NOT
24
+ * public SDK surface. `src/index.ts` deliberately re-exports nothing from this
25
+ * module — keep it that way, or the whole authored seam leaks with them.
26
+ */
27
+ export const LOWERED_COMPLETIONS = ['success', 'needs_human', 'step_failed', 'declined'] as const;
28
+ export type LoweredCompletionReason = (typeof LOWERED_COMPLETIONS)[number];
29
+
30
+ export function isLoweredCompletion(value: unknown): value is LoweredCompletionReason {
31
+ return typeof value === 'string' && (LOWERED_COMPLETIONS as readonly string[]).includes(value);
32
+ }
33
+
34
+ /**
35
+ * The bound on a normalized completion detail: 2,000 Unicode code points in
36
+ * the FINAL string, {@link COMPLETION_DETAIL_TRUNCATED_SUFFIX} included.
37
+ *
38
+ * Code points, not `String.length`: `.length` counts UTF-16 code units, so an
39
+ * emoji-heavy detail measured that way is half the length a reader would call
40
+ * it. The number matches `@relayflows/surface`'s
41
+ * `COMPLETION_DETAIL_MAX_CODE_POINTS`, which is what an author sees; it is the
42
+ * same order as the kernel's gate-detail cap
43
+ * (`kernel/relayflowd/src/engine/remote.rs`) but deliberately NOT the same
44
+ * bound — that one takes 2,000 characters and then appends its suffix, so its
45
+ * final string is longer than the number it advertises.
46
+ */
47
+ export const COMPLETION_DETAIL_MAX_CODE_POINTS = 2000;
48
+
49
+ /** Fixed width, so the bound above can reserve exactly its room. */
50
+ export const COMPLETION_DETAIL_TRUNCATED_SUFFIX = '… (truncated)';
51
+
52
+ /**
53
+ * Read `done()`'s optional second argument into the detail the marker carries.
54
+ *
55
+ * Absence has four spellings — no argument, `undefined`, `{}`, and
56
+ * `{ detail: undefined }` — and all four mean the same thing: no detail, and a
57
+ * marker command byte-identical to the one the one-argument call has always
58
+ * produced. Whitespace-only joins them: say nothing, or say something.
59
+ *
60
+ * A `detail` that is present and not a string is a programming error the type
61
+ * system already catches for TypeScript callers; the runtime check is here for
62
+ * the same reason `isSurfaceFlowCompletionReason` is, and it refuses with the
63
+ * existing closed `unsupported_completion` code rather than widening the
64
+ * taxonomy. Refusals name the type they got, never the value — a malformed
65
+ * detail is exactly the kind of text that may carry a credential.
66
+ *
67
+ * Redaction runs BEFORE truncation, and the order is load-bearing rather than
68
+ * cosmetic: truncating first can split a token so no pattern matches it any
69
+ * more, which is a truncation that *causes* a leak. What redaction recognises
70
+ * is the SDK's existing policy — known token shapes, named credential fields,
71
+ * and the values of secret-looking environment variables (`redact.ts`) — not a
72
+ * guarantee to recognise every possible secret.
73
+ *
74
+ * Lone surrogates are substituted here too, so what leaves this function is a
75
+ * well-formed string the journal protocol can carry — see {@link wellFormed}.
76
+ */
77
+ export function normalizeCompletionDetail(
78
+ options: unknown,
79
+ env: NodeJS.ProcessEnv = process.env,
80
+ ): string | undefined {
81
+ if (options === undefined) return undefined;
82
+ if (typeof options !== 'object' || options === null || Array.isArray(options)) {
83
+ throw new AuthoredFlowExecutionError(
84
+ 'unsupported_completion',
85
+ `done() options must be an object with an optional string "detail"; received ${typeName(options)}`,
86
+ );
87
+ }
88
+ const detail = (options as { detail?: unknown }).detail;
89
+ if (detail === undefined) return undefined;
90
+ if (typeof detail !== 'string') {
91
+ throw new AuthoredFlowExecutionError(
92
+ 'unsupported_completion',
93
+ `done() detail must be a string; received ${typeName(detail)}`,
94
+ );
95
+ }
96
+ const redacted = wellFormed(redact(detail, env)).trim();
97
+ return redacted.length === 0 ? undefined : bound(redacted);
98
+ }
99
+
100
+ /**
101
+ * Lone UTF-16 surrogates, as code points.
102
+ *
103
+ * Under `u` a subject string is iterated by CODE POINT, so a well-formed pair
104
+ * is one code point above U+FFFF and never enters this class. What the class
105
+ * matches is exactly a surrogate with no partner.
106
+ */
107
+ const LONE_SURROGATE = /[\uD800-\uDFFF]/u;
108
+ const LONE_SURROGATES = /[\uD800-\uDFFF]/gu;
109
+
110
+ /**
111
+ * Replace every lone surrogate with U+FFFD, the replacement character.
112
+ *
113
+ * A JavaScript string is UTF-16 code units, not text. Slicing a character off
114
+ * the end of an agent's output — `(prose + '\u{1F642}').slice(0, -1)` — leaves
115
+ * a high surrogate with no partner, and the type system calls the result a
116
+ * `string`. Nothing downstream does.
117
+ * `JSON.stringify` escapes it, so the marker COMMAND is admitted; the raw
118
+ * detail then travels in the root's `step.complete` output, where the kernel's
119
+ * JSON decoder refuses it ("unexpected end of hex escape") and answers with a
120
+ * `bad_request` carrying a null request id — which resolves no pending
121
+ * request, so the call hangs and the authored explanation never lands.
122
+ *
123
+ * Substituting is right here where refusing is not: the flow has already done
124
+ * its work and reached a verdict, and losing the whole explanation over one
125
+ * broken code unit destroys more than it protects. One code unit in, one code
126
+ * unit out, so the bound below still counts what a reader would count.
127
+ */
128
+ function wellFormed(text: string): string {
129
+ return LONE_SURROGATE.test(text) ? text.replace(LONE_SURROGATES, '\uFFFD') : text;
130
+ }
131
+
132
+ /**
133
+ * Is this a detail a durable record may carry?
134
+ *
135
+ * The gate every read boundary uses — the completed root's output, and the
136
+ * IPC frame the Bun runner returns — so an over-long or wrong-typed value
137
+ * fails closed there instead of flowing into a report as if it had been
138
+ * journaled by this process.
139
+ */
140
+ export function isDurableCompletionDetail(value: unknown): value is string {
141
+ return typeof value === 'string'
142
+ && value.length > 0
143
+ && !LONE_SURROGATE.test(value)
144
+ && [...value].length <= COMPLETION_DETAIL_MAX_CODE_POINTS;
145
+ }
146
+
147
+ /**
148
+ * The detail as ONE line, for a diagnostic message.
149
+ *
150
+ * Cloud renders a run's `error` through a line-oriented view that elides the
151
+ * middle of a long one (`cli/cloud-read.ts` `errorLines`), so a forty-line
152
+ * detail can lose the very finding it exists to carry. Escaping the breaks
153
+ * keeps the whole detail on the single line that renderer cannot elide, and
154
+ * keeps control characters out of a terminal. The unescaped normalized detail
155
+ * still travels in the structured fields beside it.
156
+ */
157
+ export function singleLineCompletionDetail(detail: string): string {
158
+ return detail.replace(/[\u0000-\u001F\u007F-\u009F]/gu, (char) =>
159
+ NAMED_ESCAPE[char] ?? `\\u${char.codePointAt(0)!.toString(16).padStart(4, '0')}`);
160
+ }
161
+
162
+ const NAMED_ESCAPE: Record<string, string> = { '\n': '\\n', '\r': '\\r', '\t': '\\t' };
163
+
164
+ /**
165
+ * The deterministic command that carries an authored verdict into the journal.
166
+ *
167
+ * `success` with no detail lowers to `:` because success needs no marker: the
168
+ * marker run's own kernel `success` already IS that record. Every other
169
+ * lowered verdict is something the kernel's completion vocabulary cannot
170
+ * express on a step that *succeeded*, so it travels as data on stdout and is
171
+ * read back from `step.completed`. It is deliberately not lowered as a failing
172
+ * command: no step failed here, and a fabricated failure would put bogus
173
+ * evidence in the journal for a flow whose steps all ran correctly.
174
+ *
175
+ * A detail rides in the same JSON object, so the marker stays the one durable
176
+ * record of the verdict and the IPC verifier's existing command comparison
177
+ * checks the detail against the journal for free. With no detail the command
178
+ * is byte-identical to the one this function has always returned, which is
179
+ * what keeps `spec_hash` stable for every flow that does not opt in.
180
+ */
181
+ export function completionMarker(reason: LoweredCompletionReason, detail?: string): string {
182
+ if (detail === undefined) {
183
+ return reason === 'success' ? ':' : `printf '%s' '{"completionReason":"${reason}"}'`;
184
+ }
185
+ // `JSON.stringify` escapes quotes, backslashes and every control character,
186
+ // so the command is one line and the only shell-significant character left
187
+ // in it is `'` — which the single-quote escape below closes.
188
+ const json = JSON.stringify({ completionReason: reason, detail });
189
+ return `printf '%s' '${json.replaceAll("'", "'\\''")}'`;
190
+ }
191
+
192
+ function typeName(value: unknown): string {
193
+ if (value === null) return 'null';
194
+ if (Array.isArray(value)) return 'an array';
195
+ return `a ${typeof value}`;
196
+ }
197
+
198
+ function bound(text: string): string {
199
+ const points = [...text];
200
+ if (points.length <= COMPLETION_DETAIL_MAX_CODE_POINTS) return text;
201
+ const keep = COMPLETION_DETAIL_MAX_CODE_POINTS - [...COMPLETION_DETAIL_TRUNCATED_SUFFIX].length;
202
+ return `${points.slice(0, keep).join('')}${COMPLETION_DETAIL_TRUNCATED_SUFFIX}`;
203
+ }
@@ -1,5 +1,5 @@
1
1
  import type { HumanRecipient } from './human-to.js';
2
- import type { StepFailedDetails } from './failure-kinds.js';
2
+ import type { ParkCause, StepFailedDetails } from './failure-kinds.js';
3
3
  import type {
4
4
  CompletionReason as ProtocolCompletionReason,
5
5
  RunCompletionReason as ProtocolRunCompletionReason,
@@ -42,6 +42,18 @@ export type AuthoredFlowExecutionErrorCode =
42
42
  export class AuthoredFlowExecutionError extends Error {
43
43
  /** Set by the durable root driver after the child error crosses any IPC boundary. */
44
44
  rootRunId?: string;
45
+ /**
46
+ * Why an `agent_parked`/`llm_parked` child run parked, as the child's own
47
+ * classification established it (`RunReport.parkCause`).
48
+ *
49
+ * Set by the worker runner beside the code, because the code alone does not
50
+ * say: `agent_parked` covers both "nothing is attached to run this step" and
51
+ * the kernel's `needs_human` recovery wait, and only the first is fixed by
52
+ * attaching a worker. The CLI boundary reads this field rather than matching
53
+ * the message, so the remedy it prints cannot drift from the cause. Absent
54
+ * means unestablished — the boundary then says nothing about workers.
55
+ */
56
+ parkCause?: ParkCause;
45
57
  constructor(
46
58
  readonly code: AuthoredFlowExecutionErrorCode,
47
59
  message: string,