@archmax-ai/harness 0.0.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 (351) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +219 -0
  3. package/dist/agent.d.ts +174 -0
  4. package/dist/agent.js +140 -0
  5. package/dist/agent.js.map +1 -0
  6. package/dist/assembly/compose.d.ts +170 -0
  7. package/dist/assembly/compose.js +391 -0
  8. package/dist/assembly/compose.js.map +1 -0
  9. package/dist/assembly/delegation-registry.d.ts +5 -0
  10. package/dist/assembly/delegation-registry.js +118 -0
  11. package/dist/assembly/delegation-registry.js.map +1 -0
  12. package/dist/assembly/index.d.ts +192 -0
  13. package/dist/assembly/index.js +265 -0
  14. package/dist/assembly/index.js.map +1 -0
  15. package/dist/assembly/plain.d.ts +7 -0
  16. package/dist/assembly/plain.js +41 -0
  17. package/dist/assembly/plain.js.map +1 -0
  18. package/dist/authoring-skill/archmax-harness/README.md +76 -0
  19. package/dist/authoring-skill/archmax-harness/SKILL.md +241 -0
  20. package/dist/authoring-skill/archmax-harness/references/backend-integration.md +626 -0
  21. package/dist/authoring-skill/archmax-harness/references/cases.md +41 -0
  22. package/dist/authoring-skill/archmax-harness/references/diagnostics.md +51 -0
  23. package/dist/authoring-skill/archmax-harness/references/hook-and-test-scripts.md +1070 -0
  24. package/dist/authoring-skill/archmax-harness/references/hooks-and-scripts.md +82 -0
  25. package/dist/authoring-skill/archmax-harness/references/wiring.md +101 -0
  26. package/dist/authoring-skill/archmax-harness/references/workflow-schema.md +1631 -0
  27. package/dist/authoring-skill/archmax-harness/references/workflow-yaml.md +241 -0
  28. package/dist/behaviour/support.d.ts +232 -0
  29. package/dist/behaviour/support.js +339 -0
  30. package/dist/behaviour/support.js.map +1 -0
  31. package/dist/cli/banner.d.ts +11 -0
  32. package/dist/cli/banner.js +29 -0
  33. package/dist/cli/banner.js.map +1 -0
  34. package/dist/cli/bootstrap.d.ts +33 -0
  35. package/dist/cli/bootstrap.js +153 -0
  36. package/dist/cli/bootstrap.js.map +1 -0
  37. package/dist/cli/command.d.ts +39 -0
  38. package/dist/cli/command.js +97 -0
  39. package/dist/cli/command.js.map +1 -0
  40. package/dist/cli/state-flow.d.ts +46 -0
  41. package/dist/cli/state-flow.js +268 -0
  42. package/dist/cli/state-flow.js.map +1 -0
  43. package/dist/cli/style.d.ts +41 -0
  44. package/dist/cli/style.js +64 -0
  45. package/dist/cli/style.js.map +1 -0
  46. package/dist/cli/turn.d.ts +41 -0
  47. package/dist/cli/turn.js +161 -0
  48. package/dist/cli/turn.js.map +1 -0
  49. package/dist/cli.d.ts +10 -0
  50. package/dist/cli.js +426 -0
  51. package/dist/cli.js.map +1 -0
  52. package/dist/core/code-description.d.ts +22 -0
  53. package/dist/core/code-description.js +38 -0
  54. package/dist/core/code-description.js.map +1 -0
  55. package/dist/core/deepagents.d.ts +183 -0
  56. package/dist/core/deepagents.js +128 -0
  57. package/dist/core/deepagents.js.map +1 -0
  58. package/dist/core/events.d.ts +350 -0
  59. package/dist/core/events.js +205 -0
  60. package/dist/core/events.js.map +1 -0
  61. package/dist/core/frontmatter.d.ts +15 -0
  62. package/dist/core/frontmatter.js +32 -0
  63. package/dist/core/frontmatter.js.map +1 -0
  64. package/dist/core/listing-redact.d.ts +37 -0
  65. package/dist/core/listing-redact.js +88 -0
  66. package/dist/core/listing-redact.js.map +1 -0
  67. package/dist/core/match.d.ts +26 -0
  68. package/dist/core/match.js +75 -0
  69. package/dist/core/match.js.map +1 -0
  70. package/dist/core/message-readers.d.ts +84 -0
  71. package/dist/core/message-readers.js +159 -0
  72. package/dist/core/message-readers.js.map +1 -0
  73. package/dist/core/messages.d.ts +49 -0
  74. package/dist/core/messages.js +85 -0
  75. package/dist/core/messages.js.map +1 -0
  76. package/dist/core/mount-prompt.d.ts +28 -0
  77. package/dist/core/mount-prompt.js +55 -0
  78. package/dist/core/mount-prompt.js.map +1 -0
  79. package/dist/core/mount-redact.d.ts +28 -0
  80. package/dist/core/mount-redact.js +32 -0
  81. package/dist/core/mount-redact.js.map +1 -0
  82. package/dist/core/mounts.d.ts +88 -0
  83. package/dist/core/mounts.js +147 -0
  84. package/dist/core/mounts.js.map +1 -0
  85. package/dist/core/path-mapping.d.ts +52 -0
  86. package/dist/core/path-mapping.js +113 -0
  87. package/dist/core/path-mapping.js.map +1 -0
  88. package/dist/core/platform-prompt.generated.d.ts +2 -0
  89. package/dist/core/platform-prompt.generated.js +4 -0
  90. package/dist/core/platform-prompt.generated.js.map +1 -0
  91. package/dist/core/prompt.d.ts +42 -0
  92. package/dist/core/prompt.js +75 -0
  93. package/dist/core/prompt.js.map +1 -0
  94. package/dist/core/session-id.d.ts +38 -0
  95. package/dist/core/session-id.js +61 -0
  96. package/dist/core/session-id.js.map +1 -0
  97. package/dist/core/session-store.d.ts +85 -0
  98. package/dist/core/session-store.js +149 -0
  99. package/dist/core/session-store.js.map +1 -0
  100. package/dist/core/session-zone.d.ts +54 -0
  101. package/dist/core/session-zone.js +82 -0
  102. package/dist/core/session-zone.js.map +1 -0
  103. package/dist/core/skill-prompt.d.ts +31 -0
  104. package/dist/core/skill-prompt.js +54 -0
  105. package/dist/core/skill-prompt.js.map +1 -0
  106. package/dist/core/skill-redact.d.ts +15 -0
  107. package/dist/core/skill-redact.js +27 -0
  108. package/dist/core/skill-redact.js.map +1 -0
  109. package/dist/core/skills.d.ts +172 -0
  110. package/dist/core/skills.js +214 -0
  111. package/dist/core/skills.js.map +1 -0
  112. package/dist/core/tool-mocks.d.ts +23 -0
  113. package/dist/core/tool-mocks.js +39 -0
  114. package/dist/core/tool-mocks.js.map +1 -0
  115. package/dist/core/tool-telemetry.d.ts +16 -0
  116. package/dist/core/tool-telemetry.js +40 -0
  117. package/dist/core/tool-telemetry.js.map +1 -0
  118. package/dist/core/usage.d.ts +114 -0
  119. package/dist/core/usage.js +226 -0
  120. package/dist/core/usage.js.map +1 -0
  121. package/dist/core/workspace-context.d.ts +136 -0
  122. package/dist/core/workspace-context.js +178 -0
  123. package/dist/core/workspace-context.js.map +1 -0
  124. package/dist/core/workspace-prompt.d.ts +7 -0
  125. package/dist/core/workspace-prompt.js +102 -0
  126. package/dist/core/workspace-prompt.js.map +1 -0
  127. package/dist/core/workspace-router.d.ts +61 -0
  128. package/dist/core/workspace-router.js +145 -0
  129. package/dist/core/workspace-router.js.map +1 -0
  130. package/dist/core/workspace.d.ts +85 -0
  131. package/dist/core/workspace.js +193 -0
  132. package/dist/core/workspace.js.map +1 -0
  133. package/dist/core/zones.d.ts +181 -0
  134. package/dist/core/zones.js +202 -0
  135. package/dist/core/zones.js.map +1 -0
  136. package/dist/env.d.ts +99 -0
  137. package/dist/env.js +271 -0
  138. package/dist/env.js.map +1 -0
  139. package/dist/index.d.ts +265 -0
  140. package/dist/index.js +197 -0
  141. package/dist/index.js.map +1 -0
  142. package/dist/kernel/kernel.d.ts +192 -0
  143. package/dist/kernel/kernel.js +751 -0
  144. package/dist/kernel/kernel.js.map +1 -0
  145. package/dist/lifecycle/hook-shape.d.ts +44 -0
  146. package/dist/lifecycle/hook-shape.js +66 -0
  147. package/dist/lifecycle/hook-shape.js.map +1 -0
  148. package/dist/lifecycle/runner.d.ts +177 -0
  149. package/dist/lifecycle/runner.js +372 -0
  150. package/dist/lifecycle/runner.js.map +1 -0
  151. package/dist/lifecycle/transcript.d.ts +27 -0
  152. package/dist/lifecycle/transcript.js +43 -0
  153. package/dist/lifecycle/transcript.js.map +1 -0
  154. package/dist/machine/allow.d.ts +65 -0
  155. package/dist/machine/allow.js +107 -0
  156. package/dist/machine/allow.js.map +1 -0
  157. package/dist/machine/delegation.d.ts +9 -0
  158. package/dist/machine/delegation.js +10 -0
  159. package/dist/machine/delegation.js.map +1 -0
  160. package/dist/machine/diagnostic.d.ts +14 -0
  161. package/dist/machine/diagnostic.js +2 -0
  162. package/dist/machine/diagnostic.js.map +1 -0
  163. package/dist/machine/lint-spec.d.ts +4 -0
  164. package/dist/machine/lint-spec.js +266 -0
  165. package/dist/machine/lint-spec.js.map +1 -0
  166. package/dist/machine/load-spec.d.ts +90 -0
  167. package/dist/machine/load-spec.js +120 -0
  168. package/dist/machine/load-spec.js.map +1 -0
  169. package/dist/machine/machine.d.ts +273 -0
  170. package/dist/machine/machine.js +551 -0
  171. package/dist/machine/machine.js.map +1 -0
  172. package/dist/machine/mount-grants.d.ts +25 -0
  173. package/dist/machine/mount-grants.js +24 -0
  174. package/dist/machine/mount-grants.js.map +1 -0
  175. package/dist/machine/slug.d.ts +14 -0
  176. package/dist/machine/slug.js +17 -0
  177. package/dist/machine/slug.js.map +1 -0
  178. package/dist/machine/spec-schema.d.ts +662 -0
  179. package/dist/machine/spec-schema.js +717 -0
  180. package/dist/machine/spec-schema.js.map +1 -0
  181. package/dist/machine/tool-names.d.ts +98 -0
  182. package/dist/machine/tool-names.js +144 -0
  183. package/dist/machine/tool-names.js.map +1 -0
  184. package/dist/machine/triggers.d.ts +114 -0
  185. package/dist/machine/triggers.js +165 -0
  186. package/dist/machine/triggers.js.map +1 -0
  187. package/dist/machine/types.d.ts +186 -0
  188. package/dist/machine/types.js +12 -0
  189. package/dist/machine/types.js.map +1 -0
  190. package/dist/machine/validate-spec.d.ts +42 -0
  191. package/dist/machine/validate-spec.js +29 -0
  192. package/dist/machine/validate-spec.js.map +1 -0
  193. package/dist/machine/variables.d.ts +196 -0
  194. package/dist/machine/variables.js +415 -0
  195. package/dist/machine/variables.js.map +1 -0
  196. package/dist/public/cli.d.ts +15 -0
  197. package/dist/public/cli.js +14 -0
  198. package/dist/public/cli.js.map +1 -0
  199. package/dist/public/messages.d.ts +33 -0
  200. package/dist/public/messages.js +33 -0
  201. package/dist/public/messages.js.map +1 -0
  202. package/dist/public/sandbox.d.ts +99 -0
  203. package/dist/public/sandbox.js +32 -0
  204. package/dist/public/sandbox.js.map +1 -0
  205. package/dist/public/spec.d.ts +147 -0
  206. package/dist/public/spec.js +149 -0
  207. package/dist/public/spec.js.map +1 -0
  208. package/dist/public/testing.d.ts +71 -0
  209. package/dist/public/testing.js +39 -0
  210. package/dist/public/testing.js.map +1 -0
  211. package/dist/rubrics/rubrics.d.ts +135 -0
  212. package/dist/rubrics/rubrics.js +145 -0
  213. package/dist/rubrics/rubrics.js.map +1 -0
  214. package/dist/runtime/contract.d.ts +73 -0
  215. package/dist/runtime/contract.js +96 -0
  216. package/dist/runtime/contract.js.map +1 -0
  217. package/dist/sandbox/assets/parts/core.js +22 -0
  218. package/dist/sandbox/assets/parts/lifecycle-hook.js +47 -0
  219. package/dist/sandbox/bridge.d.ts +16 -0
  220. package/dist/sandbox/bridge.js +95 -0
  221. package/dist/sandbox/bridge.js.map +1 -0
  222. package/dist/sandbox/executor.d.ts +46 -0
  223. package/dist/sandbox/executor.js +119 -0
  224. package/dist/sandbox/executor.js.map +1 -0
  225. package/dist/sandbox/imports.d.ts +33 -0
  226. package/dist/sandbox/imports.js +67 -0
  227. package/dist/sandbox/imports.js.map +1 -0
  228. package/dist/sandbox/prelude.d.ts +12 -0
  229. package/dist/sandbox/prelude.js +51 -0
  230. package/dist/sandbox/prelude.js.map +1 -0
  231. package/dist/sandbox/ptc-gateway.d.ts +104 -0
  232. package/dist/sandbox/ptc-gateway.js +167 -0
  233. package/dist/sandbox/ptc-gateway.js.map +1 -0
  234. package/dist/sandbox/runtime.d.ts +59 -0
  235. package/dist/sandbox/runtime.js +51 -0
  236. package/dist/sandbox/runtime.js.map +1 -0
  237. package/dist/sandbox/tools.d.ts +34 -0
  238. package/dist/sandbox/tools.js +147 -0
  239. package/dist/sandbox/tools.js.map +1 -0
  240. package/dist/sessions/operations.d.ts +79 -0
  241. package/dist/sessions/operations.js +121 -0
  242. package/dist/sessions/operations.js.map +1 -0
  243. package/dist/sessions/resolve.d.ts +97 -0
  244. package/dist/sessions/resolve.js +145 -0
  245. package/dist/sessions/resolve.js.map +1 -0
  246. package/dist/sessions/resume.d.ts +242 -0
  247. package/dist/sessions/resume.js +288 -0
  248. package/dist/sessions/resume.js.map +1 -0
  249. package/dist/sessions/scope.d.ts +99 -0
  250. package/dist/sessions/scope.js +145 -0
  251. package/dist/sessions/scope.js.map +1 -0
  252. package/dist/sessions/summary.d.ts +65 -0
  253. package/dist/sessions/summary.js +56 -0
  254. package/dist/sessions/summary.js.map +1 -0
  255. package/dist/testing/assertions.d.ts +30 -0
  256. package/dist/testing/assertions.js +231 -0
  257. package/dist/testing/assertions.js.map +1 -0
  258. package/dist/testing/case-document.d.ts +141 -0
  259. package/dist/testing/case-document.js +51 -0
  260. package/dist/testing/case-document.js.map +1 -0
  261. package/dist/testing/case-schema.d.ts +29 -0
  262. package/dist/testing/case-schema.js +519 -0
  263. package/dist/testing/case-schema.js.map +1 -0
  264. package/dist/testing/discovery.d.ts +10 -0
  265. package/dist/testing/discovery.js +24 -0
  266. package/dist/testing/discovery.js.map +1 -0
  267. package/dist/testing/driver.d.ts +78 -0
  268. package/dist/testing/driver.js +117 -0
  269. package/dist/testing/driver.js.map +1 -0
  270. package/dist/testing/grade.d.ts +116 -0
  271. package/dist/testing/grade.js +261 -0
  272. package/dist/testing/grade.js.map +1 -0
  273. package/dist/testing/interpreter.d.ts +55 -0
  274. package/dist/testing/interpreter.js +103 -0
  275. package/dist/testing/interpreter.js.map +1 -0
  276. package/dist/testing/mock-middleware.d.ts +5 -0
  277. package/dist/testing/mock-middleware.js +33 -0
  278. package/dist/testing/mock-middleware.js.map +1 -0
  279. package/dist/testing/runner.d.ts +205 -0
  280. package/dist/testing/runner.js +266 -0
  281. package/dist/testing/runner.js.map +1 -0
  282. package/dist/testing/target.d.ts +27 -0
  283. package/dist/testing/target.js +21 -0
  284. package/dist/testing/target.js.map +1 -0
  285. package/dist/validate/validate.d.ts +41 -0
  286. package/dist/validate/validate.js +669 -0
  287. package/dist/validate/validate.js.map +1 -0
  288. package/dist/workflow/agent-tools.d.ts +10 -0
  289. package/dist/workflow/agent-tools.js +10 -0
  290. package/dist/workflow/agent-tools.js.map +1 -0
  291. package/dist/workflow/checkpointer.d.ts +52 -0
  292. package/dist/workflow/checkpointer.js +200 -0
  293. package/dist/workflow/checkpointer.js.map +1 -0
  294. package/dist/workflow/control-tools.d.ts +174 -0
  295. package/dist/workflow/control-tools.js +468 -0
  296. package/dist/workflow/control-tools.js.map +1 -0
  297. package/dist/workflow/governance.d.ts +104 -0
  298. package/dist/workflow/governance.js +546 -0
  299. package/dist/workflow/governance.js.map +1 -0
  300. package/dist/workflow/middleware.d.ts +85 -0
  301. package/dist/workflow/middleware.js +283 -0
  302. package/dist/workflow/middleware.js.map +1 -0
  303. package/dist/workflow/on-error.d.ts +25 -0
  304. package/dist/workflow/on-error.js +58 -0
  305. package/dist/workflow/on-error.js.map +1 -0
  306. package/dist/workflow/parks.d.ts +46 -0
  307. package/dist/workflow/parks.js +513 -0
  308. package/dist/workflow/parks.js.map +1 -0
  309. package/dist/workflow/paths.d.ts +67 -0
  310. package/dist/workflow/paths.js +108 -0
  311. package/dist/workflow/paths.js.map +1 -0
  312. package/dist/workflow/prompt-cache.d.ts +70 -0
  313. package/dist/workflow/prompt-cache.js +103 -0
  314. package/dist/workflow/prompt-cache.js.map +1 -0
  315. package/dist/workflow/prompt-pruning.d.ts +20 -0
  316. package/dist/workflow/prompt-pruning.js +83 -0
  317. package/dist/workflow/prompt-pruning.js.map +1 -0
  318. package/dist/workflow/render-prompt.d.ts +45 -0
  319. package/dist/workflow/render-prompt.js +140 -0
  320. package/dist/workflow/render-prompt.js.map +1 -0
  321. package/dist/workflow/runtime-config.d.ts +7 -0
  322. package/dist/workflow/runtime-config.js +9 -0
  323. package/dist/workflow/runtime-config.js.map +1 -0
  324. package/dist/workflow/session-artifacts.d.ts +90 -0
  325. package/dist/workflow/session-artifacts.js +134 -0
  326. package/dist/workflow/session-artifacts.js.map +1 -0
  327. package/dist/workflow/session-view.d.ts +31 -0
  328. package/dist/workflow/session-view.js +94 -0
  329. package/dist/workflow/session-view.js.map +1 -0
  330. package/dist/workflow/snapshot.d.ts +14 -0
  331. package/dist/workflow/snapshot.js +97 -0
  332. package/dist/workflow/snapshot.js.map +1 -0
  333. package/dist/workflow/state.d.ts +347 -0
  334. package/dist/workflow/state.js +322 -0
  335. package/dist/workflow/state.js.map +1 -0
  336. package/dist/workflow/sub-workflow.d.ts +174 -0
  337. package/dist/workflow/sub-workflow.js +392 -0
  338. package/dist/workflow/sub-workflow.js.map +1 -0
  339. package/dist/workflow/tool-service.d.ts +48 -0
  340. package/dist/workflow/tool-service.js +291 -0
  341. package/dist/workflow/tool-service.js.map +1 -0
  342. package/dist/workflow/turn-boundary.d.ts +39 -0
  343. package/dist/workflow/turn-boundary.js +128 -0
  344. package/dist/workflow/turn-boundary.js.map +1 -0
  345. package/dist/workflow/turn-runner.d.ts +40 -0
  346. package/dist/workflow/turn-runner.js +122 -0
  347. package/dist/workflow/turn-runner.js.map +1 -0
  348. package/dist/workflow/workflow-tools.d.ts +46 -0
  349. package/dist/workflow/workflow-tools.js +90 -0
  350. package/dist/workflow/workflow-tools.js.map +1 -0
  351. package/package.json +119 -0
@@ -0,0 +1,347 @@
1
+ import { z } from "zod";
2
+ import { type UsageSummary } from "../core/usage.js";
3
+ import type { VariableStore } from "../machine/variables.js";
4
+ /**
5
+ * The checkpointed workflow channels, carried beside `messages` on the one
6
+ * compiled graph. The governance middleware is the only writer.
7
+ *
8
+ * **Every hook echoes the whole state.** A middleware hook returns
9
+ * `{ ...state, ...result }`, so a hook that touches one field rewrites every
10
+ * other with the value it read. Every accumulating reducer here is therefore
11
+ * idempotent on an echo — folding the value it already holds changes nothing —
12
+ * and that property is what the reducer tests pin.
13
+ */
14
+ /** The pending-decision record for a session parked at a human state; the machine-authoritative source for presenting it. */
15
+ export declare const pendingDecisionSchema: z.ZodObject<{
16
+ state: z.ZodString;
17
+ node: z.ZodString;
18
+ title: z.ZodOptional<z.ZodString>;
19
+ seq: z.ZodNumber;
20
+ transitions: z.ZodArray<z.ZodObject<{
21
+ to: z.ZodString;
22
+ description: z.ZodOptional<z.ZodString>;
23
+ type: z.ZodOptional<z.ZodEnum<{
24
+ approve: "approve";
25
+ reject: "reject";
26
+ refine: "refine";
27
+ none: "none";
28
+ }>>;
29
+ }, z.core.$strip>>;
30
+ evidence: z.ZodOptional<z.ZodArray<z.ZodString>>;
31
+ createdAt: z.ZodString;
32
+ }, z.core.$strip>;
33
+ export type PendingDecision = z.infer<typeof pendingDecisionSchema>;
34
+ /** The pending-input record for a session parked with `archmax_wait`; a delivery resumes the parked state itself. */
35
+ export declare const pendingInputSchema: z.ZodObject<{
36
+ state: z.ZodString;
37
+ node: z.ZodString;
38
+ title: z.ZodOptional<z.ZodString>;
39
+ reason: z.ZodString;
40
+ parkedAt: z.ZodString;
41
+ resumeAt: z.ZodOptional<z.ZodString>;
42
+ }, z.core.$strip>;
43
+ export type PendingInput = z.infer<typeof pendingInputSchema>;
44
+ /**
45
+ * A delegation call whose sub-run parked. The head of the list is the decision
46
+ * the session currently shows; the run parks again while any remain.
47
+ */
48
+ export declare const pendingDelegationSchema: z.ZodObject<{
49
+ state: z.ZodString;
50
+ node: z.ZodString;
51
+ workflow: z.ZodString;
52
+ toolCallId: z.ZodString;
53
+ identity: z.ZodString;
54
+ dispatchId: z.ZodString;
55
+ decision: z.ZodUnion<readonly [z.ZodObject<{
56
+ state: z.ZodString;
57
+ node: z.ZodString;
58
+ title: z.ZodOptional<z.ZodString>;
59
+ seq: z.ZodNumber;
60
+ transitions: z.ZodArray<z.ZodObject<{
61
+ to: z.ZodString;
62
+ description: z.ZodOptional<z.ZodString>;
63
+ type: z.ZodOptional<z.ZodEnum<{
64
+ approve: "approve";
65
+ reject: "reject";
66
+ refine: "refine";
67
+ none: "none";
68
+ }>>;
69
+ }, z.core.$strip>>;
70
+ evidence: z.ZodOptional<z.ZodArray<z.ZodString>>;
71
+ createdAt: z.ZodString;
72
+ }, z.core.$strip>, z.ZodObject<{
73
+ state: z.ZodString;
74
+ node: z.ZodString;
75
+ title: z.ZodOptional<z.ZodString>;
76
+ reason: z.ZodString;
77
+ parkedAt: z.ZodString;
78
+ resumeAt: z.ZodOptional<z.ZodString>;
79
+ }, z.core.$strip>]>;
80
+ }, z.core.$strip>;
81
+ /**
82
+ * Whether a delegated child's park is a decision rather than a wait. Told apart
83
+ * structurally, on the field only a decision carries, so a checkpoint written
84
+ * before the union widened still reads correctly.
85
+ */
86
+ export declare function isDecisionPark(park: PendingDelegation["decision"] | null | undefined): park is PendingDecision;
87
+ export type PendingDelegation = z.infer<typeof pendingDelegationSchema>;
88
+ /** The state a park record names, across both spellings. */
89
+ export declare function parkedStateOf(record: {
90
+ state?: string;
91
+ node?: string;
92
+ } | null | undefined): string;
93
+ /** Which channel a suspended run awaits: a person's `decision`, or external `input`. */
94
+ export type ParkChannel = "decision" | "input";
95
+ /**
96
+ * Where a park stands: `closing` — the record is committed and the run owes one
97
+ * tool-free message before it suspends; `suspend` — the next hook site suspends
98
+ * the session; `null` — no park in progress.
99
+ */
100
+ export declare const parkPhaseSchema: z.ZodEnum<{
101
+ closing: "closing";
102
+ suspend: "suspend";
103
+ }>;
104
+ /** Model calls made in `state` since it was entered; `budget.maxTurns` is enforced against the visit. */
105
+ export declare const stateTurnsSchema: z.ZodObject<{
106
+ state: z.ZodString;
107
+ count: z.ZodNumber;
108
+ }, z.core.$strip>;
109
+ export declare const TRAIL_STEP_KINDS: readonly ["trigger", "agent", "human", "sub-workflow", "reset", "on_error"];
110
+ /** One committed traversal step: the state entered, the kind of edge, and the rationale when given. */
111
+ export declare const trailStepSchema: z.ZodObject<{
112
+ to: z.ZodString;
113
+ kind: z.ZodEnum<{
114
+ human: "human";
115
+ trigger: "trigger";
116
+ agent: "agent";
117
+ "sub-workflow": "sub-workflow";
118
+ reset: "reset";
119
+ on_error: "on_error";
120
+ }>;
121
+ reason: z.ZodOptional<z.ZodString>;
122
+ workflow: z.ZodOptional<z.ZodString>;
123
+ status: z.ZodOptional<z.ZodEnum<{
124
+ error: "error";
125
+ ok: "ok";
126
+ }>>;
127
+ ts: z.ZodNumber;
128
+ }, z.core.$strip>;
129
+ export type TrailStep = z.infer<typeof trailStepSchema>;
130
+ /** One run variable as checkpointed. Locking is monotonic. */
131
+ export declare const variableEntrySchema: z.ZodObject<{
132
+ value: z.ZodUnknown;
133
+ locked: z.ZodBoolean;
134
+ reseed: z.ZodOptional<z.ZodBoolean>;
135
+ }, z.core.$strip>;
136
+ /** The run's usage as persisted — structurally identical to {@link UsageSummary} (guarded below). */
137
+ export declare const usageSummarySchema: z.ZodObject<{
138
+ inputTokens: z.ZodNumber;
139
+ outputTokens: z.ZodNumber;
140
+ cacheReadTokens: z.ZodNumber;
141
+ cacheCreationTokens: z.ZodNumber;
142
+ costUsd: z.ZodOptional<z.ZodNumber>;
143
+ }, z.core.$strip>;
144
+ /** The workflow fields persisted in the checkpointer alongside `messages`. */
145
+ declare const workflowStateFields: {
146
+ /** The machine's position. */
147
+ workflowState: z.ZodOptional<z.ZodString>;
148
+ /** Why the current turn was rejected; cleared on every transition and turn. */
149
+ rejected: z.ZodOptional<z.ZodNullable<z.ZodString>>;
150
+ iterations: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
151
+ beforeDone: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodBoolean>>;
152
+ /** The trigger that started (or last resumed) this run. Its input lives in `variables`. */
153
+ trigger: z.ZodOptional<z.ZodNullable<z.ZodObject<{
154
+ id: z.ZodString;
155
+ }, z.core.$strip>>>;
156
+ /** The state this run began in. Written once, so the reset tool has a fixed target. */
157
+ entryState: z.ZodOptional<z.ZodString>;
158
+ /** Hash of the machine spec the session last ran under. Introspection only, never a guard. */
159
+ specHash: z.ZodOptional<z.ZodString>;
160
+ /** Coarse lifecycle status; see {@link WORKFLOW_STATUSES}. */
161
+ status: z.ZodOptional<z.ZodString>;
162
+ /** The pending decision — the run's own, or a delegated child's. Cleared on decision. */
163
+ pendingDecision: z.ZodOptional<z.ZodNullable<z.ZodObject<{
164
+ state: z.ZodString;
165
+ node: z.ZodString;
166
+ title: z.ZodOptional<z.ZodString>;
167
+ seq: z.ZodNumber;
168
+ transitions: z.ZodArray<z.ZodObject<{
169
+ to: z.ZodString;
170
+ description: z.ZodOptional<z.ZodString>;
171
+ type: z.ZodOptional<z.ZodEnum<{
172
+ approve: "approve";
173
+ reject: "reject";
174
+ refine: "refine";
175
+ none: "none";
176
+ }>>;
177
+ }, z.core.$strip>>;
178
+ evidence: z.ZodOptional<z.ZodArray<z.ZodString>>;
179
+ createdAt: z.ZodString;
180
+ }, z.core.$strip>>>;
181
+ /** Monotonic count of human decisions this session has parked on. */
182
+ decisionCount: z.ZodOptional<z.ZodNumber>;
183
+ /** The pending input for a session parked with `archmax_wait`. Cleared on delivery. */
184
+ pendingInput: z.ZodOptional<z.ZodNullable<z.ZodObject<{
185
+ state: z.ZodString;
186
+ node: z.ZodString;
187
+ title: z.ZodOptional<z.ZodString>;
188
+ reason: z.ZodString;
189
+ parkedAt: z.ZodString;
190
+ resumeAt: z.ZodOptional<z.ZodString>;
191
+ }, z.core.$strip>>>;
192
+ /** Sub-runs waiting on a person, in presentation order. */
193
+ pendingDelegations: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodObject<{
194
+ state: z.ZodString;
195
+ node: z.ZodString;
196
+ workflow: z.ZodString;
197
+ toolCallId: z.ZodString;
198
+ identity: z.ZodString;
199
+ dispatchId: z.ZodString;
200
+ decision: z.ZodUnion<readonly [z.ZodObject<{
201
+ state: z.ZodString;
202
+ node: z.ZodString;
203
+ title: z.ZodOptional<z.ZodString>;
204
+ seq: z.ZodNumber;
205
+ transitions: z.ZodArray<z.ZodObject<{
206
+ to: z.ZodString;
207
+ description: z.ZodOptional<z.ZodString>;
208
+ type: z.ZodOptional<z.ZodEnum<{
209
+ approve: "approve";
210
+ reject: "reject";
211
+ refine: "refine";
212
+ none: "none";
213
+ }>>;
214
+ }, z.core.$strip>>;
215
+ evidence: z.ZodOptional<z.ZodArray<z.ZodString>>;
216
+ createdAt: z.ZodString;
217
+ }, z.core.$strip>, z.ZodObject<{
218
+ state: z.ZodString;
219
+ node: z.ZodString;
220
+ title: z.ZodOptional<z.ZodString>;
221
+ reason: z.ZodString;
222
+ parkedAt: z.ZodString;
223
+ resumeAt: z.ZodOptional<z.ZodString>;
224
+ }, z.core.$strip>]>;
225
+ }, z.core.$strip>>>>;
226
+ parkPhase: z.ZodOptional<z.ZodNullable<z.ZodEnum<{
227
+ closing: "closing";
228
+ suspend: "suspend";
229
+ }>>>;
230
+ /**
231
+ * This model call is a reply-only turn: no tools, one text reply. Set by both
232
+ * park moments (the closing turn and a reply to a parked session) so
233
+ * disclosure and governance read one condition. Cleared when spent.
234
+ */
235
+ replyOnly: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
236
+ /** Parks per state, keyed by slug — what `budget.maxParks` is enforced against. */
237
+ parkCounts: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
238
+ stateTurns: z.ZodOptional<z.ZodNullable<z.ZodObject<{
239
+ state: z.ZodString;
240
+ count: z.ZodNumber;
241
+ }, z.core.$strip>>>;
242
+ /** Ordered audit trail. Accumulates: a writer sends only the steps it adds. */
243
+ auditTrail: z.ZodOptional<z.ZodArray<z.ZodObject<{
244
+ to: z.ZodString;
245
+ kind: z.ZodEnum<{
246
+ human: "human";
247
+ trigger: "trigger";
248
+ agent: "agent";
249
+ "sub-workflow": "sub-workflow";
250
+ reset: "reset";
251
+ on_error: "on_error";
252
+ }>;
253
+ reason: z.ZodOptional<z.ZodString>;
254
+ workflow: z.ZodOptional<z.ZodString>;
255
+ status: z.ZodOptional<z.ZodEnum<{
256
+ error: "error";
257
+ ok: "ok";
258
+ }>>;
259
+ ts: z.ZodNumber;
260
+ }, z.core.$strip>>>;
261
+ /**
262
+ * Cumulative token usage (and cost). A last-value channel: the one writer adds
263
+ * its delta to the total it reads, because an additive reducer cannot tell a
264
+ * hook echoing the total from a delta that happens to equal it.
265
+ */
266
+ usage: z.ZodOptional<z.ZodObject<{
267
+ inputTokens: z.ZodNumber;
268
+ outputTokens: z.ZodNumber;
269
+ cacheReadTokens: z.ZodNumber;
270
+ cacheCreationTokens: z.ZodNumber;
271
+ costUsd: z.ZodOptional<z.ZodNumber>;
272
+ }, z.core.$strip>>;
273
+ /** The run's variables, keyed by name. The channel merges, so a writer sends only its delta. */
274
+ variables: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
275
+ value: z.ZodUnknown;
276
+ locked: z.ZodBoolean;
277
+ reseed: z.ZodOptional<z.ZodBoolean>;
278
+ }, z.core.$strip>>>;
279
+ };
280
+ export type WorkflowStateFields = z.infer<z.ZodObject<typeof workflowStateFields>>;
281
+ /**
282
+ * Merge a variable delta into the store: a host seeding boundary (`reseed`)
283
+ * replaces anything, a locked entry is otherwise kept, any other write replaces.
284
+ * Only runtime-owned boundaries set the marker (never the variable tools), so no
285
+ * agent-reachable write breaks a lock. Folding a store onto itself changes nothing.
286
+ */
287
+ export declare function mergeVariables(acc: VariableStore, update: VariableStore): VariableStore;
288
+ /** Mark a delta as written at a host seeding boundary (a turn's opening, a delivery), so it replaces even a locked entry. */
289
+ export declare function reseedAll(delta: VariableStore): VariableStore;
290
+ /**
291
+ * The trail's fold: an update equal to the list held is a hook's echo (no-op);
292
+ * an update extending it replaces it; anything else (a tool seam sending only
293
+ * its own steps, two of them in one super-step) is appended.
294
+ */
295
+ export declare function foldTrail(acc: TrailStep[] | undefined, update: TrailStep[] | undefined): TrailStep[];
296
+ /**
297
+ * A `pendingDelegations` update: records to add (union by identity, so a hook's
298
+ * echo is a no-op), `null` to clear, or `{ remaining }` — the list after a
299
+ * decision resolved its head.
300
+ */
301
+ export type PendingDelegationsUpdate = PendingDelegation[] | null | {
302
+ remaining: PendingDelegation[];
303
+ };
304
+ export declare function foldPendingDelegations(acc: PendingDelegation[] | null | undefined, update: PendingDelegationsUpdate | undefined): PendingDelegation[];
305
+ /**
306
+ * The graph's and the middleware's `stateSchema`, with the accumulating
307
+ * channels' reducers applied. Without the wrap every field is last-value, and a
308
+ * tool committing a delta from inside a batch would replace the channel.
309
+ */
310
+ export declare const workflowStateSchema: z.ZodObject<typeof workflowStateFields>;
311
+ /** The checkpointed variable store; a checkpoint predating the field reads as empty. */
312
+ export declare function readVariables(state: unknown): VariableStore;
313
+ /** What a completed run returned, by declared name; `undefined` when nothing is declared. */
314
+ export declare function readReturns(state: unknown, declared: string[] | undefined): Record<string, unknown> | undefined;
315
+ /** Coarse session lifecycle values written to {@link WorkflowStateFields.status}. */
316
+ export declare const WORKFLOW_STATUSES: {
317
+ readonly running: "running";
318
+ readonly completed: "completed";
319
+ readonly rejected: "rejected";
320
+ readonly awaitingDecision: "awaiting_decision";
321
+ readonly awaitingInput: "awaiting_input";
322
+ };
323
+ export type WorkflowStatus = (typeof WORKFLOW_STATUSES)[keyof typeof WORKFLOW_STATUSES];
324
+ /** The statuses of a run that is over; a parked session is open. */
325
+ export declare const FINISHED_STATUSES: ReadonlySet<string>;
326
+ export type SessionClassification = "finished" | "open";
327
+ export declare function isFinished(status: string | undefined): boolean;
328
+ export declare function classifyStatus(status: string | undefined): SessionClassification;
329
+ /** The checkpointed usage totals; a checkpoint predating the field reads as zeroed. */
330
+ export declare function readRunUsage(state: unknown): UsageSummary;
331
+ export declare function readAuditTrail(state: unknown): TrailStep[];
332
+ export declare function readWorkflowState(state: unknown): WorkflowStateFields;
333
+ export declare function currentWorkflowState(state: unknown, fallback: string): string;
334
+ /** One reader for disclosure, the prompt section and the kernel, so a parked run cannot be disclosed one way and governed another. */
335
+ export declare function isReplyOnly(state: unknown): boolean;
336
+ /** What a hook or a serviced tool call commits: a channel delta, appended messages, and where the loop goes next. */
337
+ export type WorkflowUpdate = Partial<Omit<WorkflowStateFields, "pendingDelegations">> & {
338
+ pendingDelegations?: PendingDelegationsUpdate;
339
+ messages?: unknown[];
340
+ jumpTo?: "model" | "end";
341
+ };
342
+ /** The park record a session currently holds, on either channel, or `null`. */
343
+ export declare function pendingParkOf(state: unknown): {
344
+ channel: ParkChannel;
345
+ record: PendingDecision | PendingInput;
346
+ } | null;
347
+ export {};
@@ -0,0 +1,322 @@
1
+ import { withLangGraph } from "@langchain/langgraph/zod";
2
+ import { z } from "zod";
3
+ import { emptyUsage } from "../core/usage.js";
4
+ /**
5
+ * The checkpointed workflow channels, carried beside `messages` on the one
6
+ * compiled graph. The governance middleware is the only writer.
7
+ *
8
+ * **Every hook echoes the whole state.** A middleware hook returns
9
+ * `{ ...state, ...result }`, so a hook that touches one field rewrites every
10
+ * other with the value it read. Every accumulating reducer here is therefore
11
+ * idempotent on an echo — folding the value it already holds changes nothing —
12
+ * and that property is what the reducer tests pin.
13
+ */
14
+ // --- Park records ------------------------------------------------------------
15
+ /** The pending-decision record for a session parked at a human state; the machine-authoritative source for presenting it. */
16
+ export const pendingDecisionSchema = z.object({
17
+ /** Slug of the human state the session is parked at — the resume routes against this. */
18
+ state: z.string(),
19
+ /** @deprecated Older spelling of {@link state}; read from checkpoints written before the rename. */
20
+ node: z.string(),
21
+ /** The parked state's `title`, when declared. A label, never a routing target. */
22
+ title: z.string().optional(),
23
+ /** 1-based ordinal of this decision within the session. */
24
+ seq: z.number(),
25
+ /** The selectable outgoing transitions. */
26
+ transitions: z.array(z.object({
27
+ to: z.string(),
28
+ description: z.string().optional(),
29
+ type: z.enum(["approve", "reject", "refine", "none"]).optional(),
30
+ })),
31
+ /** Evidence artifacts surfaced to the person (agent-visible run paths). */
32
+ evidence: z.array(z.string()).optional(),
33
+ createdAt: z.string(),
34
+ });
35
+ /** The pending-input record for a session parked with `archmax_wait`; a delivery resumes the parked state itself. */
36
+ export const pendingInputSchema = z.object({
37
+ /** Slug of the state the session is parked in — the state a delivery resumes. */
38
+ state: z.string(),
39
+ /** @deprecated Older spelling of {@link state}; read from checkpoints written before the rename. */
40
+ node: z.string(),
41
+ title: z.string().optional(),
42
+ /** Why the agent is waiting, in its own words. */
43
+ reason: z.string(),
44
+ parkedAt: z.string(),
45
+ /** When the run asked to be resumed (absolute). A hint for whoever schedules — the runtime holds no timer. */
46
+ resumeAt: z.string().optional(),
47
+ });
48
+ /**
49
+ * A delegation call whose sub-run parked. The head of the list is the decision
50
+ * the session currently shows; the run parks again while any remain.
51
+ */
52
+ export const pendingDelegationSchema = z.object({
53
+ /** The state that made the call — where the run parks, and resumes. */
54
+ state: z.string(),
55
+ /** @deprecated Older spelling of {@link state}; read from checkpoints written before the rename. */
56
+ node: z.string(),
57
+ workflow: z.string(),
58
+ /** The tool call this sub-run answers. */
59
+ toolCallId: z.string(),
60
+ /** The sub-run's identity (`<state>:<workflow>:<ordinal>`); the child's session id derives from it. */
61
+ identity: z.string(),
62
+ dispatchId: z.string(),
63
+ /**
64
+ * The child's own park, presented to the person unchanged — a decision when it
65
+ * parked at a human state, an input park when it called `archmax_wait`. The
66
+ * parent parks on whichever channel the child asked for, so an `archmax_wait`
67
+ * child is delivered to rather than decided on.
68
+ */
69
+ decision: z.union([pendingDecisionSchema, pendingInputSchema]),
70
+ });
71
+ /**
72
+ * Whether a delegated child's park is a decision rather than a wait. Told apart
73
+ * structurally, on the field only a decision carries, so a checkpoint written
74
+ * before the union widened still reads correctly.
75
+ */
76
+ export function isDecisionPark(park) {
77
+ return Array.isArray(park?.transitions);
78
+ }
79
+ /** The state a park record names, across both spellings. */
80
+ export function parkedStateOf(record) {
81
+ return record?.state ?? record?.node ?? "";
82
+ }
83
+ /**
84
+ * Where a park stands: `closing` — the record is committed and the run owes one
85
+ * tool-free message before it suspends; `suspend` — the next hook site suspends
86
+ * the session; `null` — no park in progress.
87
+ */
88
+ export const parkPhaseSchema = z.enum(["closing", "suspend"]);
89
+ /** Model calls made in `state` since it was entered; `budget.maxTurns` is enforced against the visit. */
90
+ export const stateTurnsSchema = z.object({ state: z.string(), count: z.number() });
91
+ // --- Trail --------------------------------------------------------------------
92
+ export const TRAIL_STEP_KINDS = ["trigger", "agent", "human", "sub-workflow", "reset", "on_error"];
93
+ /** One committed traversal step: the state entered, the kind of edge, and the rationale when given. */
94
+ export const trailStepSchema = z.object({
95
+ to: z.string(),
96
+ kind: z.enum(TRAIL_STEP_KINDS),
97
+ reason: z.string().optional(),
98
+ /** `sub-workflow` steps only: the child slug and whether it completed — the parent's whole record of the sub-run. */
99
+ workflow: z.string().optional(),
100
+ status: z.enum(["ok", "error"]).optional(),
101
+ /** Epoch milliseconds at commit. */
102
+ ts: z.number(),
103
+ });
104
+ /** One run variable as checkpointed. Locking is monotonic. */
105
+ export const variableEntrySchema = z.object({
106
+ value: z.unknown(),
107
+ locked: z.boolean(),
108
+ /** Host-boundary marker; see {@link mergeVariables}. Never persisted as meaningful state. */
109
+ reseed: z.boolean().optional(),
110
+ });
111
+ /** The run's usage as persisted — structurally identical to {@link UsageSummary} (guarded below). */
112
+ export const usageSummarySchema = z.object({
113
+ inputTokens: z.number(),
114
+ outputTokens: z.number(),
115
+ cacheReadTokens: z.number(),
116
+ cacheCreationTokens: z.number(),
117
+ costUsd: z.number().optional(),
118
+ });
119
+ const _trailStepPublicParity = true;
120
+ void _trailStepPublicParity;
121
+ const _usageParity = true;
122
+ void _usageParity;
123
+ // --- Fields -------------------------------------------------------------------
124
+ /** The workflow fields persisted in the checkpointer alongside `messages`. */
125
+ const workflowStateFields = {
126
+ /** The machine's position. */
127
+ workflowState: z.string().optional(),
128
+ /** Why the current turn was rejected; cleared on every transition and turn. */
129
+ rejected: z.string().nullable().optional(),
130
+ iterations: z.record(z.string(), z.number()).optional(),
131
+ beforeDone: z.record(z.string(), z.boolean()).optional(),
132
+ /** The trigger that started (or last resumed) this run. Its input lives in `variables`. */
133
+ trigger: z.object({ id: z.string() }).nullable().optional(),
134
+ /** The state this run began in. Written once, so the reset tool has a fixed target. */
135
+ entryState: z.string().optional(),
136
+ /** Hash of the machine spec the session last ran under. Introspection only, never a guard. */
137
+ specHash: z.string().optional(),
138
+ /** Coarse lifecycle status; see {@link WORKFLOW_STATUSES}. */
139
+ status: z.string().optional(),
140
+ /** The pending decision — the run's own, or a delegated child's. Cleared on decision. */
141
+ pendingDecision: pendingDecisionSchema.nullable().optional(),
142
+ /** Monotonic count of human decisions this session has parked on. */
143
+ decisionCount: z.number().optional(),
144
+ /** The pending input for a session parked with `archmax_wait`. Cleared on delivery. */
145
+ pendingInput: pendingInputSchema.nullable().optional(),
146
+ /** Sub-runs waiting on a person, in presentation order. */
147
+ pendingDelegations: z.array(pendingDelegationSchema).nullable().optional(),
148
+ parkPhase: parkPhaseSchema.nullable().optional(),
149
+ /**
150
+ * This model call is a reply-only turn: no tools, one text reply. Set by both
151
+ * park moments (the closing turn and a reply to a parked session) so
152
+ * disclosure and governance read one condition. Cleared when spent.
153
+ */
154
+ replyOnly: z.boolean().nullable().optional(),
155
+ /** Parks per state, keyed by slug — what `budget.maxParks` is enforced against. */
156
+ parkCounts: z.record(z.string(), z.number()).optional(),
157
+ stateTurns: stateTurnsSchema.nullable().optional(),
158
+ /** Ordered audit trail. Accumulates: a writer sends only the steps it adds. */
159
+ auditTrail: z.array(trailStepSchema).optional(),
160
+ /**
161
+ * Cumulative token usage (and cost). A last-value channel: the one writer adds
162
+ * its delta to the total it reads, because an additive reducer cannot tell a
163
+ * hook echoing the total from a delta that happens to equal it.
164
+ */
165
+ usage: usageSummarySchema.optional(),
166
+ /** The run's variables, keyed by name. The channel merges, so a writer sends only its delta. */
167
+ variables: z.record(z.string(), variableEntrySchema).optional(),
168
+ };
169
+ // --- Reducers -----------------------------------------------------------------
170
+ /**
171
+ * Merge a variable delta into the store: a host seeding boundary (`reseed`)
172
+ * replaces anything, a locked entry is otherwise kept, any other write replaces.
173
+ * Only runtime-owned boundaries set the marker (never the variable tools), so no
174
+ * agent-reachable write breaks a lock. Folding a store onto itself changes nothing.
175
+ */
176
+ export function mergeVariables(acc, update) {
177
+ const merged = { ...acc };
178
+ for (const [name, entry] of Object.entries(update)) {
179
+ const existing = merged[name];
180
+ if (existing !== undefined && entry.reseed !== true && existing.locked)
181
+ continue;
182
+ // The marker is an instruction to the merge, not part of the variable.
183
+ merged[name] = { value: entry.value, locked: entry.locked };
184
+ }
185
+ return merged;
186
+ }
187
+ /** Mark a delta as written at a host seeding boundary (a turn's opening, a delivery), so it replaces even a locked entry. */
188
+ export function reseedAll(delta) {
189
+ const out = {};
190
+ for (const [name, entry] of Object.entries(delta))
191
+ out[name] = { ...entry, reseed: true };
192
+ return out;
193
+ }
194
+ /** Whether two steps are the same record; every field counts, so two children delegated on one millisecond stay two steps. */
195
+ function sameStep(a, b) {
196
+ return (a === b ||
197
+ (a.to === b.to &&
198
+ a.kind === b.kind &&
199
+ a.ts === b.ts &&
200
+ a.reason === b.reason &&
201
+ a.workflow === b.workflow &&
202
+ a.status === b.status));
203
+ }
204
+ /**
205
+ * The trail's fold: an update equal to the list held is a hook's echo (no-op);
206
+ * an update extending it replaces it; anything else (a tool seam sending only
207
+ * its own steps, two of them in one super-step) is appended.
208
+ */
209
+ export function foldTrail(acc, update) {
210
+ const current = acc ?? [];
211
+ if (!update || update.length === 0)
212
+ return current;
213
+ if (update.length === current.length && update.every((s, i) => sameStep(s, current[i]))) {
214
+ return current;
215
+ }
216
+ if (update.length > current.length && current.every((s, i) => sameStep(s, update[i]))) {
217
+ return [...update];
218
+ }
219
+ const fresh = update.filter((step) => !current.includes(step));
220
+ return fresh.length > 0 ? [...current, ...fresh] : current;
221
+ }
222
+ export function foldPendingDelegations(acc, update) {
223
+ const current = acc ?? [];
224
+ if (update === undefined)
225
+ return current;
226
+ if (update === null)
227
+ return [];
228
+ if (!Array.isArray(update))
229
+ return [...update.remaining];
230
+ if (update.length === 0)
231
+ return current;
232
+ const incoming = new Set(update.map((record) => record.identity));
233
+ return [...current.filter((record) => !incoming.has(record.identity)), ...update];
234
+ }
235
+ const pendingDelegationsUpdateSchema = z.union([
236
+ z.array(pendingDelegationSchema),
237
+ z.null(),
238
+ z.object({ remaining: z.array(pendingDelegationSchema) }),
239
+ ]);
240
+ /** A field wrapped with its channel's fold; typed loosely, the object below is re-typed as a whole. */
241
+ function reduced(field, updateSchema, fn, initial) {
242
+ return withLangGraph(field, {
243
+ reducer: { schema: updateSchema, fn },
244
+ default: initial,
245
+ });
246
+ }
247
+ /**
248
+ * The graph's and the middleware's `stateSchema`, with the accumulating
249
+ * channels' reducers applied. Without the wrap every field is last-value, and a
250
+ * tool committing a delta from inside a batch would replace the channel.
251
+ */
252
+ export const workflowStateSchema = z.object({
253
+ ...workflowStateFields,
254
+ pendingDelegations: reduced(workflowStateFields.pendingDelegations, pendingDelegationsUpdateSchema, (acc, update) => foldPendingDelegations(acc, update), () => []),
255
+ variables: reduced(workflowStateFields.variables, workflowStateFields.variables, (acc, update) => update
256
+ ? mergeVariables(acc ?? {}, update)
257
+ : (acc ?? {}), () => ({})),
258
+ auditTrail: reduced(workflowStateFields.auditTrail, workflowStateFields.auditTrail, (acc, update) => foldTrail(acc, update), () => []),
259
+ // The wrap widens the reduced fields; what a hook reads and writes is the field's own shape.
260
+ });
261
+ // --- Readers ------------------------------------------------------------------
262
+ /** The checkpointed variable store; a checkpoint predating the field reads as empty. */
263
+ export function readVariables(state) {
264
+ return readWorkflowState(state).variables ?? {};
265
+ }
266
+ /** What a completed run returned, by declared name; `undefined` when nothing is declared. */
267
+ export function readReturns(state, declared) {
268
+ if (!declared || declared.length === 0)
269
+ return undefined;
270
+ const variables = readVariables(state);
271
+ return Object.fromEntries(declared.map((name) => [name, variables[name]?.value]));
272
+ }
273
+ /** Coarse session lifecycle values written to {@link WorkflowStateFields.status}. */
274
+ export const WORKFLOW_STATUSES = {
275
+ running: "running",
276
+ completed: "completed",
277
+ rejected: "rejected",
278
+ awaitingDecision: "awaiting_decision",
279
+ awaitingInput: "awaiting_input",
280
+ };
281
+ /** The statuses of a run that is over; a parked session is open. */
282
+ export const FINISHED_STATUSES = new Set([
283
+ WORKFLOW_STATUSES.completed,
284
+ WORKFLOW_STATUSES.rejected,
285
+ ]);
286
+ export function isFinished(status) {
287
+ return status !== undefined && FINISHED_STATUSES.has(status);
288
+ }
289
+ export function classifyStatus(status) {
290
+ return isFinished(status) ? "finished" : "open";
291
+ }
292
+ /** The checkpointed usage totals; a checkpoint predating the field reads as zeroed. */
293
+ export function readRunUsage(state) {
294
+ const usage = readWorkflowState(state).usage;
295
+ return usage ? { ...emptyUsage(), ...usage } : emptyUsage();
296
+ }
297
+ export function readAuditTrail(state) {
298
+ const trail = readWorkflowState(state).auditTrail;
299
+ return Array.isArray(trail) ? trail : [];
300
+ }
301
+ export function readWorkflowState(state) {
302
+ if (!state || typeof state !== "object")
303
+ return {};
304
+ return state;
305
+ }
306
+ export function currentWorkflowState(state, fallback) {
307
+ return readWorkflowState(state).workflowState ?? fallback;
308
+ }
309
+ /** One reader for disclosure, the prompt section and the kernel, so a parked run cannot be disclosed one way and governed another. */
310
+ export function isReplyOnly(state) {
311
+ return readWorkflowState(state).replyOnly === true;
312
+ }
313
+ /** The park record a session currently holds, on either channel, or `null`. */
314
+ export function pendingParkOf(state) {
315
+ const fields = readWorkflowState(state);
316
+ if (fields.pendingDecision)
317
+ return { channel: "decision", record: fields.pendingDecision };
318
+ if (fields.pendingInput)
319
+ return { channel: "input", record: fields.pendingInput };
320
+ return null;
321
+ }
322
+ //# sourceMappingURL=state.js.map