@archmax-ai/harness 1.0.1 → 2.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 (94) hide show
  1. package/README.md +167 -165
  2. package/dist/agent.d.ts +2 -2
  3. package/dist/agent.js +2 -2
  4. package/dist/assembly/compose.js +5 -3
  5. package/dist/assembly/compose.js.map +1 -1
  6. package/dist/authoring-skill/archmax-harness/README.md +13 -15
  7. package/dist/authoring-skill/archmax-harness/SKILL.md +174 -261
  8. package/dist/authoring-skill/archmax-harness/references/backend-integration.md +244 -764
  9. package/dist/authoring-skill/archmax-harness/references/cases.md +178 -53
  10. package/dist/authoring-skill/archmax-harness/references/code-states-and-scripts.md +211 -100
  11. package/dist/authoring-skill/archmax-harness/references/diagnostics.md +57 -78
  12. package/dist/authoring-skill/archmax-harness/references/workflow-schema.md +614 -1977
  13. package/dist/core/attachments.d.ts +2 -0
  14. package/dist/core/attachments.js +2 -1
  15. package/dist/core/attachments.js.map +1 -1
  16. package/dist/core/deepagents.d.ts +1 -2
  17. package/dist/core/deepagents.js +1 -2
  18. package/dist/core/deepagents.js.map +1 -1
  19. package/dist/core/file-operations.d.ts +6 -0
  20. package/dist/core/file-operations.js +29 -0
  21. package/dist/core/file-operations.js.map +1 -1
  22. package/dist/index.d.ts +5 -1
  23. package/dist/index.js +4 -0
  24. package/dist/index.js.map +1 -1
  25. package/dist/kernel/kernel.js +2 -2
  26. package/dist/kernel/kernel.js.map +1 -1
  27. package/dist/machine/lint-spec.js +6 -5
  28. package/dist/machine/lint-spec.js.map +1 -1
  29. package/dist/machine/machine.d.ts +8 -10
  30. package/dist/machine/machine.js +11 -12
  31. package/dist/machine/machine.js.map +1 -1
  32. package/dist/machine/signature-pattern.d.ts +24 -0
  33. package/dist/machine/signature-pattern.js +67 -0
  34. package/dist/machine/signature-pattern.js.map +1 -0
  35. package/dist/machine/signature.d.ts +137 -35
  36. package/dist/machine/signature.js +476 -25
  37. package/dist/machine/signature.js.map +1 -1
  38. package/dist/machine/spec-schema.d.ts +203 -5
  39. package/dist/machine/spec-schema.js +209 -51
  40. package/dist/machine/spec-schema.js.map +1 -1
  41. package/dist/machine/tool-names.d.ts +24 -34
  42. package/dist/machine/tool-names.js +43 -63
  43. package/dist/machine/tool-names.js.map +1 -1
  44. package/dist/machine/types.d.ts +11 -10
  45. package/dist/machine/types.js.map +1 -1
  46. package/dist/public/spec.d.ts +10 -6
  47. package/dist/public/spec.js +9 -5
  48. package/dist/public/spec.js.map +1 -1
  49. package/dist/validate/validate.js +3 -3
  50. package/dist/validate/validate.js.map +1 -1
  51. package/dist/workflow/automated.js +6 -1
  52. package/dist/workflow/automated.js.map +1 -1
  53. package/dist/workflow/control-tools.js +3 -3
  54. package/dist/workflow/control-tools.js.map +1 -1
  55. package/dist/workflow/file-values.d.ts +4 -1
  56. package/dist/workflow/file-values.js +2 -0
  57. package/dist/workflow/file-values.js.map +1 -1
  58. package/dist/workflow/grader-state.d.ts +2 -2
  59. package/dist/workflow/grader-state.js +5 -15
  60. package/dist/workflow/grader-state.js.map +1 -1
  61. package/dist/workflow/middleware.d.ts +2 -0
  62. package/dist/workflow/middleware.js +3 -26
  63. package/dist/workflow/middleware.js.map +1 -1
  64. package/dist/workflow/on-error.js +0 -1
  65. package/dist/workflow/on-error.js.map +1 -1
  66. package/dist/workflow/parks.js +0 -1
  67. package/dist/workflow/parks.js.map +1 -1
  68. package/dist/workflow/render-prompt.d.ts +4 -5
  69. package/dist/workflow/render-prompt.js +31 -29
  70. package/dist/workflow/render-prompt.js.map +1 -1
  71. package/dist/workflow/signature-checks.d.ts +8 -3
  72. package/dist/workflow/signature-checks.js +17 -4
  73. package/dist/workflow/signature-checks.js.map +1 -1
  74. package/dist/workflow/state.d.ts +0 -9
  75. package/dist/workflow/state.js +0 -3
  76. package/dist/workflow/state.js.map +1 -1
  77. package/dist/workflow/sub-workflow.js +4 -1
  78. package/dist/workflow/sub-workflow.js.map +1 -1
  79. package/dist/workflow/turn-boundary.js +0 -1
  80. package/dist/workflow/turn-boundary.js.map +1 -1
  81. package/package.json +2 -2
  82. package/skills/archmax-harness/README.md +13 -15
  83. package/skills/archmax-harness/SKILL.md +174 -261
  84. package/skills/archmax-harness/references/backend-integration.md +244 -764
  85. package/skills/archmax-harness/references/cases.md +178 -53
  86. package/skills/archmax-harness/references/code-states-and-scripts.md +211 -100
  87. package/skills/archmax-harness/references/diagnostics.md +57 -78
  88. package/skills/archmax-harness/references/workflow-schema.md +614 -1977
  89. package/dist/authoring-skill/archmax-harness/references/code-state-and-test-scripts.md +0 -1424
  90. package/dist/authoring-skill/archmax-harness/references/wiring.md +0 -119
  91. package/dist/authoring-skill/archmax-harness/references/workflow-yaml.md +0 -399
  92. package/skills/archmax-harness/references/code-state-and-test-scripts.md +0 -1424
  93. package/skills/archmax-harness/references/wiring.md +0 -119
  94. package/skills/archmax-harness/references/workflow-yaml.md +0 -399
@@ -1,804 +1,284 @@
1
- # Wiring @archmax-ai/harness into a backend — integration reference
1
+ # Wiring the harness into a backend
2
2
 
3
- How to embed the `@archmax-ai/harness` package in a service so a worker can
4
- **execute** workflows and an API/frontend can **visualize** run state. All names
5
- below are the real public API (`src/index.ts`); confirm against the installed
6
- package's `dist/index.d.ts`.
3
+ Load this when you embed `@archmax-ai/harness` in a service: a worker that sends turns,
4
+ an API that answers parked sessions, a frontend that draws them. Every name here is an
5
+ export of the installed package. When in doubt, check its `dist/index.d.ts`.
7
6
 
8
7
  ## Contents
9
8
 
10
- - [Install (registry & local tarball)](#install)
11
- - [Public exports](#public-exports)
12
- - [createAgent options](#createworkflowagent-options)
13
- - [Agent](#workflowruntime)
14
- - [Running a workflow](#running-a-workflow)
15
- - [Human-in-the-loop](#human-in-the-loop)
16
- - [Exposing a workflow to outside callers (MCP, forms, APIs)](#exposing-a-workflow-to-outside-callers)
17
- - [Lifecycle events (live visualization)](#lifecycle-events)
18
- - [Session artifacts (historical visualization)](#run-artifacts)
19
- - [Model configuration](#model-configuration)
20
- - [Custom backends](#custom-backends)
9
+ - [Install](#install)
10
+ - [The smallest program](#the-smallest-program)
11
+ - [`createAgent` options](#createagent-options)
12
+ - [Models](#models)
13
+ - [Outcomes and parked sessions](#outcomes-and-parked-sessions)
14
+ - [Events](#events)
15
+ - [Sessions, storage and artifacts](#sessions-storage-and-artifacts)
16
+ - [Host tools that use the workspace](#host-tools-that-use-the-workspace)
17
+ - [Exposing a workflow to callers](#exposing-a-workflow-to-callers)
21
18
  - [Cases against your agent](#cases-against-your-agent)
22
- - [Suggested worker / API / frontend split](#suggested-split)
23
-
24
- ---
19
+ - [Sub-workflows in a service](#sub-workflows-in-a-service)
20
+ - [A typical split](#a-typical-split)
25
21
 
26
22
  ## Install
27
23
 
28
- **Registry** (once published): `npm i @archmax-ai/harness`.
29
-
30
- **Local / pre-publish** — install exactly what the registry would ship (a real copy
31
- in `node_modules`, not a symlink), so behavior matches production:
32
-
33
- ```bash
34
- # in the harness repo
35
- npm run build
36
- npm pack --pack-destination /tmp # → /tmp/archmax-ai-harness-<version>.tgz
37
-
38
- # in the consuming service
39
- npm add /tmp/archmax-ai-harness-<version>.tgz
40
- # or pnpm --filter <pkg> add /tmp/archmax-ai-harness-<version>.tgz
41
- ```
42
-
43
- The published tarball contains only `dist/` + `README.md` (the package `files`
44
- allowlist), so `dist/index.d.ts` types travel with it — a shared types package can
45
- re-export the run-artifact/event shapes for an API/frontend without importing the
46
- runtime. Because the version rarely changes during development, `remove` then `add`
47
- (or prune the store) so a repack of the same version actually reinstalls.
48
-
49
- **This skill ships inside the package** (staged into `dist/authoring-skill/archmax-harness/`):
50
- resolve it from the install via the `BUNDLED_AUTHORING_SKILL_DIR` export — the
51
- `dist/authoring-skill` directory holding `archmax-harness/`, so this file is
52
- `<BUNDLED_AUTHORING_SKILL_DIR>/archmax-harness/SKILL.md` — e.g.
53
- `npx skills add ./node_modules/@archmax-ai/harness/dist/authoring-skill/archmax-harness`.
54
- Prefer this over
55
- syncing a copy from a source checkout, so the skill can never drift from the
56
- installed SDK's validator. Tooling that needs the install root without importing
57
- the runtime can use `require.resolve("@archmax-ai/harness/package.json")` (exposed in the
58
- `exports` map).
59
-
60
- ## Public exports
61
-
62
- From `@archmax-ai/harness` (`src/index.ts`):
63
-
64
- | Export | Kind | Purpose |
65
- | --- | --- | --- |
66
- | `createAgent` | fn | Assemble a runnable workflow-governed agent |
67
- | `Agent` | type | Returned runtime (`graph`, `decide`, `deliver`, `resolveSession`, `runs`, `emitRunArtifacts`, `dispose`, `resolveTrigger`) |
68
- | `CreateAgentParams` | type | Assembly options |
69
- | `DecideOutcome` | type | Return of `decide()` |
70
- | `DeliverOutcome`, `TriggerDelivery` | type | Argument / return of `deliver()` |
71
- | `WorkflowLoadError` | class | Thrown when the workflow spec (`workflow.yaml`) is missing/invalid (fails closed) |
72
- | `DEFAULT_WORKFLOW` | const | `"order-lookup"` |
73
- | `BUNDLED_AUTHORING_SKILL_DIR` | const | The packaged `dist/authoring-skill` directory holding `archmax-harness/` (`<dir>/archmax-harness/SKILL.md`). The example workspace is not shipped in `dist/`; clone the repo to use it. |
74
- | `WORKFLOW_STATUSES` | const | `running` \| `completed` \| `failed` (the agent called `archmax_raise`) \| `rejected` \| `awaiting_decision` (`awaitingDecision`) \| `awaiting_input` (`awaitingInput`) |
75
- | `SessionExit` | type | `Outcome.exit` / `DecideOutcome.exit` / `SessionSummary.exit`: `{ success: true }` on a completed turn, `{ success: false, code, reason }` on a failed one |
76
- | `isFinished`, `classifyStatus` | fn | The open/finished partition — a parked session is **open** |
77
- | `WorkflowLifecycleEvent`, `WorkflowEventHandler` | type | Event stream types |
78
- | `createWorkflowEventEmitter`, `renderEventLine` | fn | Event emitter + console formatter |
79
- | `createWorkspaceContext` | fn | Compose the workspace backends (session zone + mounts + authoring backend) |
80
- | `createFilesystemSessionStore`, `createBackendSessionStore`, `createMemorySessionStore` | fn | Run-store factories (physical storage for the session zone) |
81
- | `SessionStoreRequiredError`, `SessionStoreCapabilityError` | class | Assembly / capability errors for run storage |
82
- | `workflowStateSchema`, `workflowPaths`, `sessionPaths`, `runArtifactPaths` | schema/fn | State schema + path helpers |
83
- | `runTests`, `RunTestsOptions`, `CaseResult` | fn/type | Run a workflow's cases — [against your own agent](#cases-against-your-agent) |
84
- | `toolsFromMap`, `AgentToolDescriptor`, `ToolContext`, `ToolPaths`, `PathAccess`, `ToolPathsError` | fn/type/class | Host tools: a `name → descriptor` map, each descriptor's declared `paths` and a handler handed the turn's workspace — [host tools](#host-tools) |
85
- | `SIGNATURE_TYPES`, `normalizeSignature`, `signatureForTrigger`, `signatureJsonSchema`, `signatureValueIssues` | const/fn | A trigger's typed signature as JSON Schema and its conformance rule — [exposing a workflow](#exposing-a-workflow-to-outside-callers); also on `@archmax-ai/harness/spec` |
86
-
87
- ## createAgent options
88
-
89
- All optional (`CreateAgentParams`, `src/assembly/index.ts`):
90
-
91
- | Option | Type | Notes |
92
- | --- | --- | --- |
93
- | `rootDir` | `string` | Workspace root for the default filesystem backend. Defaults to cwd. |
94
- | `workflow` | `string` | Named workflow under `workflows/<name>/`. |
95
- | `trigger` | `TriggerInput` | `{ id, args? }`; defaults to `{ id: "manual" }`. Selects the start state. |
96
- | `model` | `BaseChatModel` | Defaults to the env-configured OpenAI-compatible model. Inject your own to bypass env. An explicit instance outranks every model id a spec declares, grader states' included. |
97
- | `modelFactory` | `(role, env, requested?) => BaseChatModel` | Supplies the chat model per role: `agent` (the session's turns), `judge` (the case judge for `grade:`) and `grader` (a grader state's subagent). `requested` is the id the state or `settings.model` declares, for the `agent` and `grader` roles; a grader state's model is `modelFactory("grader", env, id)`, resolved once per composition. `env` is a thunk, so a factory with its own credentials never loads `.env`. |
98
- | `backend` | `BackendProtocolV2` | Serves the authored workspace. Defaults to a `FilesystemBackend` over `rootDir`. |
99
- | `authoring` | `BackendProtocolV2` | Serves the **authoring plane** — `workflows/**` (machine specs, `WORKFLOW.md`, code-state `scripts/`, `tests/`) — to the runtime only. Never a route in the agent's workspace, so no tool call or PTC call can reach it. Defaults to `backend` when you supply one, else a `FilesystemBackend` over the resolved root. Handing it to a **writable** mount throws `AuthoringBackendExposedError`; a read-only mount may share it. |
100
- | `mounts` | `Record<string, MountSpec>` | Authored mounts as a `CompositeBackend` route table: key → backend, or `{ backend, readOnly, governed, searchable }`. Trailing slash = directory mount; no slash = exact-path file mount (`"/AGENTS.md"`). Read-only unless declared otherwise. `governed: true` hands the spec's `mounts` block the decision of **which states** may reach it (closed by default, like a skill bundle) and whether each may write there; a mount not so declared is visible in every state exactly as today, so a table that marks nothing behaves unchanged. `readOnly` is a **ceiling** the spec cannot lift: a governed writable mount is written only by a workflow whose own grants ask `access: read_write` (a bare grant reads it, so serving one route writable for one workflow in a delegation tree opens it to no other), and a grant may narrow a writable mount to reads in a state, never open a read-only one. A key may be several segments deep (`"/catalogs/eu/"`); its **first** segment is what is reserved, so `/workflows/x/` fails as `/workflows/` does. Omitted on the default backend: `defaultMounts(rootDir)` (the conventional table — `/skills/`, `/.platform/`, `/AGENTS.md`, none governed — extend with `{ ...defaultMounts(root), "/templates/": … }`). Omitted with a custom `backend`: nothing authored is served; expose subtrees with `mountSubtree(backend, "skills")`. A key colliding with a run area — or naming the authoring-plane prefix (`/workflows/`) — throws `MountCollisionError`. `searchable: false` declares a mount the runtime never searches on its own initiative: a root-wide `grep`/`glob` fans out over the other mounts only, while a search the agent addresses at the mount (or a path inside it) still reaches its backend and returns the backend's own answer — matches or `{ error }` — verbatim. Use it for a backend that must refuse searches (a remote folder served live). Listing and reading are unchanged, the prompt marks it "browse only", names arrive as `MountPrefixes.unsearchable`, and it is ignored on a file mount. |
101
- | `sessionStore` | `SessionStore` | Physical storage for run state (checkpoints, artifacts, the per-session `scratchpad/`). Zero-config default: `createFilesystemSessionStore({ dir: "<rootDir>/sessions" })`. Required with a custom `backend` (else `SessionStoreRequiredError`). Build with `createBackendSessionStore({ backend, prefix? })` for S3-style storage or `createMemorySessionStore()` for ephemeral runs. |
102
- | `systemPrompt` | `string` | Appended after workspace `AGENTS.md`. |
103
- | `tools` | `StructuredTool[]` | Host tools, granted per state by `tools.allow`. Build them with `toolsFromMap` — see [host tools](#host-tools). A name starting `archmax_`, or naming a runtime file operation (`copy_file`, `move_file`, `remove_file`), throws `ReservedToolNameError`. |
104
- | `essentialTools` | `string[]` | Host tools treated as always on: disclosed and permitted in every state, whatever the workflow's `tools.allow_always` declares; a state's own entry still narrows one. |
105
- | `clock` | `string` | Pins the "now" every model call states, for cases and evaluations: an ISO 8601 date (`2026-03-20`, midnight UTC) or a date-time with `Z`/an offset; zoneless or impossible values fail assembly, `""` means the real clock. Rendered in UTC, rounded to ten minutes, no local time. Recorded timestamps, `resumeAt` and the sandbox's `Date` stay real. Not for production: a pinned session resumed days later still thinks it is the pinned day. A case's `clock:` outranks it. |
106
- | `images` | `boolean \| ImageReadOptions` | Show a model with vision the images it reads: the model's `read_file` of a PNG/JPEG/GIF/WebP answers with one line and the image is added to each later request as a `user` message after that batch of tool results, read per request — no bytes in history, checkpoints, events or scripts. `{ maxBytes?, keep? }` (default 10 MB, every image). Off by default; every other binary stays refused. |
107
- | `toolPaths` | `Record<string, ToolPaths>` | Each host tool's path arguments and how a call uses each (`read`, `list`, `search`, `write`, `remove`, `execute`): `{ get_markdown: { path: "read" } }`. Every path rule then governs them like the built-ins'. Wins over a descriptor's `paths`; declaring a built-in's paths throws `ToolPathsError`. |
108
- | `checkpointer` | `BaseCheckpointSaver` | Custom-adapter escape hatch; takes precedence over the session store for checkpoint persistence. Defaults to a durable `BackendCheckpointSaver` writing through the session store. Use `MemorySaver` for ephemeral runs. |
109
- | `middleware` | `AgentMiddleware[]` | Extra middleware appended after workflow instrumentation. |
110
- | `onEvent` | `WorkflowEventHandler` | Subscribe to all lifecycle diagnostics; suppresses console output when set. |
111
- | `promptCache` | `PromptCacheOptions` | `{ enabled?, ttl?: "5m" \| "1h" }`. Provider prompt caching for the stable prefix; on by default. Falls back to `settings.prompt_cache`, then `ARCHMAX_PROMPT_CACHE`/`_TTL`. Turning it off does not stop `ChatAnthropic` or `ChatBedrockConverse` caching: Deep Agents installs LangChain's caching middleware for them itself (5-minute lifetime). |
112
- | `pricing` | `PricingTable` | USD per 1M tokens, keyed by model id (`default` matches any): `{ input?, output?, cacheRead?, cacheWrite? }`. Looked up by the id the response reported, else the id the runtime was configured to run — so a keyed table prices a workload behind a proxy that echoes no model back. Input rate applies to the input tokens the cache did not serve. Makes `costUsd` appear on usage events, run metadata, and the CLI footer. Falls back to `ARCHMAX_PRICE_*`. Unpriced → tokens only. |
113
-
114
- Fails closed: throws `WorkflowLoadError` if the named workflow's `workflow.yaml`
115
- is missing or has no valid machine spec (never silently runs ungoverned; a
116
- `WORKFLOW.md` carrying frontmatter with no `workflow.yaml` — the removed
117
- single-file layout — is a load error with migration guidance).
118
-
119
- ## Agent
120
-
121
- ```ts
122
- interface Agent {
123
- graph: /* LangGraph runnable */;
124
- workflow: string;
125
- runtimeContract: ResolvedRuntimeContract;
126
- resolveTrigger(trigger?: TriggerInput): ResolvedTrigger;
127
- emitRunArtifacts(sessionId: string, trajectory: Trajectory): Promise<string | null>;
128
- runs: { // run handles over the configured session store
129
- list(): Promise<SessionSummary[]>; // [] when the store/checkpointer can't list
130
- get(sessionId: string): Promise<SessionSummary | null>;
131
- delete(sessionId: string): Promise<boolean>; // throws SessionStoreCapabilityError
132
- };
133
- decide(sessionId: string, resolution: DecisionResolution): Promise<DecideOutcome>;
134
- dispose(sessionId: string): void;
135
- }
136
- ```
137
-
138
- ## Running a workflow
139
-
140
- ```ts
141
- import { createAgent, WORKFLOW_STATUSES } from "@archmax-ai/harness";
142
-
143
- const agent = await createAgent({ workflow, onEvent, workspace: { rootDir } });
144
- const sessionId = `run-${runId}`;
145
-
146
- try {
147
- const result = await agent.invoke(
148
- { messages: [{ role: "user", content: prompt }] },
149
- { configurable: { thread_id: sessionId } },
150
- );
151
- // result: { messages, status?, workflowState?, pendingDecision? }
152
- if (result.status === WORKFLOW_STATUSES.awaitingDecision) {
153
- // parked — see Human-in-the-loop
154
- } else {
155
- return lastAssistantText(result.messages);
156
- }
157
- } finally {
158
- agent.dispose(sessionId); // frees the session's sandbox/PTC state and its checkpoints in memory; the next turn reloads them from the store
159
- }
160
- ```
161
-
162
- A turn runs under LangGraph's `recursionLimit` — 400 unless the config passes its
163
- own, which wins. It is the one bound on a whole turn (every model call, tool batch
164
- and automated state is a step; a state's `budget.maxTurns`/`maxParks` bound that
165
- state); a turn that hits it ends the session `failed` with exit code
166
- `recursion-limit`.
167
-
168
- To cancel a turn, pass an `AbortSignal` as `signal` in the same config (or in
169
- `agent.workflow.send(sessionId, input, { signal })`) and abort it. The turn's
170
- scripts stop with it, in the session and its child sessions: a pending `tools.*`
171
- call fails and later ones are refused. The `dispose` in the `finally` is safe even
172
- while a cancelled script's call is still pending.
173
-
174
- Use `agent.stream(...)` instead of `invoke` when you want to record a
175
- trajectory or push incremental updates.
176
-
177
- `lastAssistantText` walks `result.messages` from the end and returns the last
178
- assistant/AI message's text.
179
-
180
- ## Multi-turn conversations (chat history)
181
-
182
- `agent.invoke` accepts an **array of role-tagged messages**, not just one user
183
- string — the `messages` channel is a LangChain `add_messages` channel, so
184
- whatever you pass is appended to the session's transcript. There is no need to
185
- concatenate a prior chat into one giant user prompt; give the model real turns.
186
-
187
- **Preferred — session persistence.** Sessions are durable by default (the
188
- `BackendCheckpointSaver` writes each turn to the session store). Drive a live
189
- conversation by re-invoking the **same `thread_id`** once per user message,
190
- sending only the new message each time — prior turns are already in the
191
- checkpoint:
192
-
193
- ```ts
194
- // turn 1
195
- await agent.invoke(
196
- { messages: [{ role: "user", content: "Look up order 1234" }] },
197
- { configurable: { thread_id: "chat-42" } },
198
- );
199
- // turn 2 — SAME thread_id; only the new message
200
- await agent.invoke(
201
- { messages: [{ role: "user", content: "And when did it ship?" }] },
202
- { configurable: { thread_id: "chat-42" } },
203
- );
204
- ```
205
-
206
- The `init` state re-runs on each turn, resetting `workflowState` to the trigger's
207
- start state while **preserving the accumulated messages** — exactly right for
208
- chat: each user message re-enters the workflow with full history retained.
209
-
210
- **Alternative — seed prior history on the first invoke.** When the transcript
211
- lives in your own store (not a harness session), pass it as the initial `messages`
212
- array with the newest user turn last:
213
-
214
- ```ts
215
- await agent.invoke(
216
- {
217
- messages: [
218
- { role: "user", content: "Test" },
219
- { role: "assistant", content: "Test complete…" },
220
- { role: "user", content: "who are you?" },
221
- { role: "assistant", content: "I'm an agent that…" },
222
- { role: "user", content: "what was my first message?" }, // the new turn
223
- ],
224
- },
225
- { configurable: { thread_id: "chat-42" } },
226
- );
227
- ```
24
+ Install `@archmax-ai/harness`, plus `langchain` and `zod` to write host tools. It needs
25
+ Node 22 or later. Before a release is published, `npm pack` the harness and install the
26
+ tarball: a real copy behaves like production, and a symlink does not.
228
27
 
229
- Gotchas:
230
-
231
- - **An automated state reads the whole session.** A code state and a grader
232
- both call `archmax_get_history` for `{ messages, trail }` (a script:
233
- `JSON.parse(await tools.archmax_get_history())`). Its `messages` are the whole
234
- transcript, oldest first, which can span turns: a script that keys off "the
235
- current request" should take the *last* `user` entry, not the first — the
236
- first is the message that opened the session. A tool entry carries its tool,
237
- the call `id` and the result as its `text`. Neither's own calls or messages
238
- enter the session's transcript.
239
- - **A definition edit is adopted, not refused.** A session takes its next turn
240
- under the `workflow.yaml` in force now; the recorded `specHash` says which
241
- definition each turn ran under but gates nothing. A turn positioned in a state
242
- the edit removed reopens at its trigger's entry state rather than failing.
243
- - **Seed assistant turns as text only.** Don't fabricate dangling `tool_calls`
244
- in seeded history, or the provider may reject the turn.
245
- - **Not every message in a session's transcript is a person's.** The runtime
246
- writes **runtime notes** of its own — `[decision]`, `[event]`, `[error]`,
247
- `[grader]`, `[code]`, `[sub-workflow: …]` — carried as a synthetic `archmax_note` tool call
248
- and its result, so a model cannot read them as a person speaking. Ask
249
- `isRuntimeNote(message)` / `runtimeNoteKind(message)` rather than inferring
250
- authorship from role or a `[bracket]` prefix: a turn's lower bound is a human
251
- message **or** a note, and a transcript renderer should draw a note as a
252
- runtime line, not as the user's own bubble. The one message a person really
253
- sends into a running session — a reply to a run parked at a human state — is an
254
- ordinary human message with no marker.
28
+ This skill ships inside the package. `BUNDLED_AUTHORING_SKILL_DIR` is the directory that
29
+ holds it (`<dir>/archmax-harness/SKILL.md`), and
30
+ `npx skills add ./node_modules/@archmax-ai/harness` installs the copy that matches the
31
+ installed version.
255
32
 
256
- ```ts
257
- import { isRuntimeNote, runtimeNoteKind } from "@archmax-ai/harness";
33
+ ## The smallest program
258
34
 
259
- const opensTurn = (m: unknown) => isRuntimeNote(m) || m.role === "user";
260
- const label = (m: unknown) => runtimeNoteKind(m); // "decision" | "event" | "grader" | "code" | … | null
261
- ```
35
+ [`examples/refund/`](https://github.com/archmax-ai/harness/tree/main/examples/refund)
36
+ runs a four-state workflow with two host tools. A host tool is an ordinary LangChain
37
+ tool, built with `tool(fn, { name, description, schema })` from `langchain`, and a state
38
+ grants it in `tools.allow`. The program, `index.js`:
262
39
 
263
- ## Human-in-the-loop
40
+ ```js
41
+ import { createAgent } from "@archmax-ai/harness";
42
+ import { lookupCustomer, sendEmail } from "./tools.js";
264
43
 
265
- ```ts
266
- if (result.status === WORKFLOW_STATUSES.awaitingDecision && result.pendingDecision) {
267
- const { state, transitions, evidence } = result.pendingDecision;
268
- // Present `transitions` (each { to, description?, type? }) + evidence to a human.
269
- // `evidence` is the state's declared paths followed by whatever the advancing
270
- // agent attached via `archmax_advance({ evidence })` — one merged list, no
271
- // host-side assembly, and no need to re-read the spec.
272
- // When they pick:
273
- const outcome = await agent.workflow.decide(sessionId, { target: chosenTo, comment });
274
- // DecideOutcome: { status?, workflowState?, reparked, state?, reply, rejected?, exit?, messages }
275
- // `reply` is every assistant text of the turn, in order ("" when it said nothing).
276
- // `rejected` says why when status is "rejected"; Outcome from send() carries it too.
277
- // `exit` is { success: false, code, reason } when the agent ended the run with
278
- // archmax_raise (status "failed"), { success: true } when it completed.
279
- if (outcome.reparked) { /* parked again at another human state */ }
280
- }
281
- ```
282
-
283
- `decide` throws `SessionNotParkedError` if the session isn't awaiting a decision, and
284
- `InvalidDecisionTargetError` if `target` isn't a declared transition of the parked
285
- state. `sessions.list()` enumerates durable sessions (empty with a `MemorySaver` or a non-listable session store).
286
-
287
- ## Event-in-the-loop: a run that parked itself
288
-
289
- The agent can park the run with `archmax_wait({ reason })` — in any state, terminal
290
- ones included — when the work cannot continue without something from outside. The
291
- session's status becomes `awaiting_input` (classified **open**, never finished) and
292
- `result.pendingInput` carries `{ state, title?, reason, parkedAt }`: the state it
293
- stopped in and, in its own words, what it is waiting for.
294
-
295
- ```ts
296
- if (result.status === WORKFLOW_STATUSES.awaitingInput && result.pendingInput) {
297
- const { state, reason } = result.pendingInput; // show "waiting in <state>: <reason>"
298
- }
44
+ const agent = await createAgent({
45
+ workflow: "refund", // workflows/refund/
46
+ tools: [lookupCustomer, sendEmail], // host tools
47
+ workspace: { rootDir: import.meta.dirname }, // sessions are stored under sessions/
48
+ });
299
49
 
300
- // When the event arrives, deliver it. The run continues in the state it parked in;
301
- // the trigger id becomes its current trigger and the variables arrive locked.
302
- const outcome = await agent.workflow.deliver(sessionId, {
303
- trigger: { id: "email_reply" },
304
- variables: { reply_body: "It's ORD-1001." },
50
+ const outcome = await agent.workflow.send("ticket-1001", {
51
+ message: "Hi, I'd like a refund for my last order.",
52
+ variables: { email: "ann@example.com" }, // the trigger's requires, seeded locked
305
53
  });
306
- // DeliverOutcome: DecideOutcome + { variables, parkedChannel? }
307
- if (outcome.reparked) { /* parked again — outcome.parkedChannel says which channel */ }
54
+ console.log(outcome.kind); // "completed"
308
55
  ```
309
56
 
310
- ### Waking a scheduled park
311
-
312
- A park may declare when it wants to be resumed (`archmax_wait({ reason, until: "1d" })`),
313
- which lands on the record, the summary, and the park event as an absolute
314
- `resumeAt`. **The SDK schedules nothing** — no timer, no cron — so this is your
315
- job, and it is a query:
316
-
317
- ```ts
318
- const now = new Date().toISOString();
319
- for (const t of await agent.sessions.list()) {
320
- if (t.status === WORKFLOW_STATUSES.awaitingInput && t.resumeAt && t.resumeAt <= now) {
321
- await agent.workflow.deliver(t.sessionId, { trigger: { id: "timer" }, variables: { now } });
322
- }
323
- }
324
- ```
57
+ - One `send` is one turn. The session id is yours to choose (a ticket, a conversation).
58
+ `send` with the same id continues the session, and each turn carries only the new
59
+ message: the session already holds the transcript.
60
+ - `send` works out what the session needs: a new turn for a new or finished session, a
61
+ reply for one parked at a human state, a delivery for one parked with `archmax_wait`.
62
+ - In TypeScript, `agent.workflow` is `undefined` on an ungoverned agent: narrow it
63
+ before you call it.
325
64
 
326
- A duplicate firing is safe: `deliver` refuses a session that is no longer parked. A
327
- `resumeAt` gates nothing — an event arriving earlier resumes the park normally — and
328
- a state that polls can cap its own retry loop with `budget.maxParks`, whose overrun
329
- routes through the state's `on_error`.
330
-
331
- `deliver` throws `SessionNotAwaitingInputError` if the session isn't parked awaiting
332
- an event and `MissingDeliveryTriggerError` if no id is supplied. The id is
333
- **recorded, never matched**: any trigger resumes a park, so there is no awaited-id
334
- list to check against — an id no state declares is deliverable exactly like a
335
- declared one. `resolveSession({ trigger, variables })` tells you which of
336
- the two a firing is — a `turn` to invoke, or a `resume` to deliver into — always on
337
- the conversation's own id.
338
-
339
- An event that must **never start** a run is therefore declared nowhere in the spec:
340
- every trigger a state declares starts a session at that state. Name the session
341
- yourself — `send(sessionId, { delivery })` / `deliver(sessionId, …)` — or pass
342
- `sessionPath` with the firing (`resolveSession({ trigger, variables, sessionPath })`)
343
- and let the runtime read the id out of the firing's own variables. A firing that
344
- names neither, on a trigger no state declares, throws
345
- `UnknownSessionTriggerError`: there is nowhere for it to go.
346
-
347
- One turn per invoke, on **one** session. A finished session re-invoked with a new
348
- message takes its next turn on the same id, continuing in the state the previous
349
- turn ended in — the position, variables, transcript and files are already there, so
350
- **never seed a previous transcript into the `messages` channel**. Pass only the new
351
- message.
352
-
353
- The runtime does not reposition a later turn, so nothing is injected into the
354
- transcript to explain a jump. When the current state can serve the new message, the
355
- agent answers there without a reset. That includes a terminal state that is also the
356
- state the conversation began in, such as a one-state chat workflow. When a turn needs
357
- a path the current state cannot reach, the agent calls `archmax_reset` to return to
358
- the state the conversation began in (available in every state). To continue *mid-state* instead — inside the state
359
- that stopped, with the arrival in its transcript — the turn must park rather than
360
- finish.
361
-
362
- ## Exposing a workflow to outside callers
363
-
364
- A workflow's trigger signature (`requires`/`returns`, optionally typed) is the
365
- contract the runtime already enforces at the session boundary. A host that
366
- exposes a workflow as an MCP tool, an OpenAPI operation or a typed start form
367
- builds its schema **from that signature**, with the helpers on the browser-safe
368
- `@archmax-ai/harness/spec` subpath (also on the root, as the same bindings), so
369
- the published contract and the enforced one cannot drift:
65
+ ## `createAgent` options
370
66
 
371
- ```ts
372
- import {
373
- parseMachineSpec,
374
- signatureForTrigger,
375
- signatureJsonSchema,
376
- signatureValueIssues,
377
- } from "@archmax-ai/harness/spec";
378
-
379
- const parsed = parseMachineSpec(yaml.parse(workflowYaml));
380
- if (!parsed.ok) throw new Error(parsed.issues.map((i) => `${i.path}: ${i.message}`).join("\n"));
381
- const signature = signatureForTrigger(parsed.spec, "manual"); // { description?, requires, returns }
382
-
383
- // MCP tool (or an OpenAPI request/response body): the same mapping the delegation tool uses.
384
- const mcpTool = {
385
- name: "refund-order",
386
- description: signature?.description, // caller-facing; never shown to the session's agent
387
- inputSchema: signatureJsonSchema(signature?.requires ?? []),
388
- outputSchema: signatureJsonSchema(signature?.returns ?? []),
389
- };
390
-
391
- // On a request, before any session opens: the rule the turn boundary applies.
392
- const issues = signatureValueIssues(signature?.requires ?? [], body);
393
- if (issues.length) return reply(400, issues.map((i) => i.message)); // { kind, name, type?, found?, message }
394
-
395
- // A turn needs a message; an MCP call has none of its own, so say what was asked.
396
- const outcome = await agent.workflow!.send(sessionId, { message: "Refund the order.", variables: body });
397
- ```
67
+ Deep Agents' own options keep their names: `model`, `tools`, `systemPrompt`,
68
+ `middleware`, `backend`, `checkpointer`, `store`, `skills`, `interruptOn`, `name`,
69
+ `description`. The harness adds:
398
70
 
399
- - `signatureJsonSchema(entries)` → `{ type: "object", properties, required }`:
400
- one property per entry in declaration order, every name required,
401
- `date`/`date-time`/`file` as `{ type: "string", format }`, an untyped entry as `{}`,
402
- an entry's `description` carried over. It adds no `additionalProperties`; set
403
- it for your own ingress.
404
- - `signatureValueIssues(entries, values)` → one issue per missing name and per
405
- non-conforming value; `[]` means the turn boundary will not refuse the map for
406
- its signature. It **never coerces**: a form that collects text converts
407
- `"4"` to `4` before it validates or seeds.
408
- - A trigger's `description` is written for the caller. Use it as the MCP tool
409
- or operation description. The session's own model never sees it.
410
- - **A `file` entry** (`format: "file"` in the schema) takes a workspace path. To
411
- hand a session an uploaded file, pass it on the turn rather than writing into
412
- the store yourself:
413
- `send(sessionId, { message, files: { invoice: { name: "invoice.pdf", content: bytes } } })`.
414
- The runtime places the bytes in the session's `scratchpad/attachments/` (a taken
415
- name gets `-1`, `-2`, …), seeds `invoice`, locked, with the placed path, and the
416
- turn boundary then checks that the file is there and readable. `send` throws
417
- `InvalidTurnFileError`, before anything is written, for a bad variable or file
418
- name, a variable also given in `variables`, a session parked at a human state, or
419
- bytes the store cannot hold exactly (a non-text file needs a store with
420
- `uploadFiles`). `signatureValueIssues` checks only a `file` value's shape, so a
421
- start form validates the path the upload will produce, or skips the entry and
422
- lets `files` supply it.
423
- - A host that reads specs itself must read `requires`/`returns` through
424
- `normalizeSignature` (both spellings → `{ name, type?, description? }[]`),
425
- never as `string[]`: an entry may be an object. Hosts must be on the release
426
- that introduced typed entries before any workspace they read uses them.
427
-
428
- ## Lifecycle events
429
-
430
- Pass `onEvent`; every `WorkflowLifecycleEvent` carries a `level`
431
- (`info`/`warn`/`error`) plus a typed payload. Map these to a live run UI:
432
-
433
- | Event `type` | Meaning |
71
+ | Option | What it does |
434
72
  | --- | --- |
435
- | `state-enter` / `state-leave` | state entered / left (`{ state, next? }`) |
436
- | `advance` | transition committed (`{ from, to, reason?, callId? }` — `callId` names the `archmax_advance` call that drove it; absent on the turn's opening arrival) |
437
- | `code-start` / `code-output` / `code-result` | a code state's run: `{ state, script }`, one `{ state, line }` per `console.log` line, then `{ state, status: "ok" \| "error", durationMs, reason? }` |
438
- | `grader-start` / `grader-result` | a grader state's run: `{ state }`, then `{ state, status, durationMs, reason? }`. `status` is `ok` when the run settled on a decision or a completion; a move then emits `advance`/`state-leave`/`state-enter` (a code state's tool calls carry `origin: "script"`, a grader's none) |
439
- | `parked` / `decided` | human state parked / resolved (`{ state, sessionId, awaiting, to? }` — a human park carries no `callId`, since no call asked for it) |
440
- | `parked` (`awaiting: "input"`) / `delivered` | the agent parked the run (`{ state, sessionId, reason, resumeAt?, callId }` — the `archmax_wait` call) / an event resumed it (`{ state, trigger, to }`, where `to` is the same state) |
441
- | `raised` | the agent ended the session as a failure (`{ state, sessionId, code, reason, callId }` — the `archmax_raise` call); a `state-leave` follows, and the outcome is `failed` |
442
- | `agent-text` | complete assistant message in a state (`{ state, text, messageId }` — always the id the message is stored under; absent only on a `partial: true` event from a failed turn) |
443
- | `agent-text-delta` | streaming text chunk (`{ state, text, messageId? }`) — flows for every run, even when the graph is driven with `invoke`; a sub-workflow child's chunks carry the child's `sessionId` and `subWorkflowDispatchId`, never the caller's state |
444
- | `tool-called` / `tool-result` / `tool-blocked` | tool passed governance (`callId`, `args`) / settled (`callId`, `status`, `durationMs`, `output` preview) / denied. **Every** governed call, `archmax_advance` included — so a UI drawing a row per call draws one per transition; filter it if you already draw transitions from `advance`/`state-leave`. A tool that **throws** settles `status: "error"` with the error's message as `output` while the turn **continues**: the model reads the same message as the call's answer, and nothing is thrown out of `send` — read tool failures here |
445
- | `model-usage` | per model call — the agent's or a grader's, under its state — provider token usage: `inputTokens` (the total, of which the cache counts are a breakdown), `outputTokens`, `cacheReadTokens`, `cacheCreationTokens`, `model?` (the id the call was priced against), `costUsd?` |
446
- | `prompt-shaping` | assembly-time payload summary: resolved `profile`, prompt-cache mechanism, `withheld` built-ins |
447
- | `skills-loaded`, `graph-topology`, `workflow-reset`, `warning` | assembly-time diagnostics |
448
-
449
- For a browser client, forward these over SSE/WebSocket keyed by `sessionId`. To
450
- render a live chat transcript: accumulate `agent-text-delta` into a bubble
451
- keyed by `messageId`, replace it with the final `agent-text` bearing the same
452
- `messageId`, and interleave tool cards from `tool-called`/`tool-result` paired
453
- by `callId` (order everything by `seq`).
454
-
455
- Building a **durable record** rather than a live view? The ids join the stream to
456
- the stored messages exactly: `callId` equals the issuing AI message's
457
- `tool_calls[].id` and the settling `ToolMessage.tool_call_id`, and
458
- `agent-text.messageId` equals the assistant message's `id`. Every event a call
459
- *causes* carries that call's id too (`advance`, `parked`, `variables-set`,
460
- `title-set`, and the sub-workflow pair's `toolCallId`), so the events one call
461
- produced group from ids alone — no scanning back to a message boundary. An event
462
- of those types with no `callId` is one the runtime caused, not a call: a turn
463
- opening, a human park, run-start seeding.
464
-
465
- `title-set` is the one event carrying a variable's **value** — the reserved
466
- `title`, whenever it changes. `variables-set` still carries names only; the value
467
- rides its own event so that rule needs no exception, and it is safe there because
468
- a title is bounded to one short line by its write check. Indexing a run's title on
469
- your own record is a handler for this event and nothing else — no checkpoint read,
470
- which matters when you install your own checkpointer. A sub-workflow's child is not
471
- asked to name itself, so it emits none unless its caller passed a `title` down.
472
-
473
- ### Reading tokens and cost
474
-
475
- Usage figures are the **provider's own** (LangChain `usage_metadata`), not
476
- estimates — including how many input tokens were served from the prompt cache.
477
- Use the exported tracker instead of summing events yourself:
478
-
479
- ```ts
480
- import { createAgent, createUsageTracker } from "@archmax-ai/harness";
481
-
482
- // Wrap your own subscriber, or omit `onEvent` if you have none.
483
- const usage = createUsageTracker({ onEvent: forwardToClient });
484
- const agent = await createAgent({ onEvent: usage.handler, /* … */ });
485
-
486
- await agent.invoke(input, { configurable: { thread_id: sessionId } });
487
-
488
- const totals = usage.totals(sessionId);
489
- // { inputTokens, outputTokens, cacheReadTokens, cacheCreationTokens, costUsd? }
490
- usage.bySession(); // Map<sessionId, UsageSummary> for concurrent runs
491
- usage.reset(sessionId); // drop a finished run's totals
492
- ```
493
-
494
- Rising `cacheReadTokens` is how you confirm prompt caching is actually working;
495
- `costUsd` is present only when pricing is configured — report tokens rather than a
496
- guessed cost when it is absent. `formatUsage`, `addSummaries`, `emptyUsage` and
497
- `hasUsage` are exported for rendering and combining summaries; the extraction and
498
- pricing internals are not part of the public surface.
73
+ | `workflow` | The workflow's slug under `workflows/`. When omitted, the default workflow governs the agent if it exists; `false` assembles a plain Deep Agent. |
74
+ | `workspace` | `{ rootDir, mounts?, sessionStore? }`. `rootDir` defaults to the current directory. |
75
+ | `authoring` | The backend that serves `workflows/**` (specs, `WORKFLOW.md`, code-state scripts, cases) to the runtime only. It defaults to `backend`, else the files under `rootDir`. Exposing it through a writable mount throws `AuthoringBackendExposedError`. |
76
+ | `trigger`, `variables`, `sessionPath` | The default trigger, seeded variables (locked) and session path for turns. |
77
+ | `modelFactory` | `(role, env, requested?) => model`, called per role: `agent`, `judge` (a case's `grade`) and `grader` (a grader state, asked with the state's model id). |
78
+ | `essentialTools` | Host tools that are on in every state, whatever `allow_always` says. A state's entry still narrows one. |
79
+ | `toolPaths` | Each host tool's path arguments and how it uses them (`{ get_markdown: { path: "read" } }`), governed like a built-in tool's. |
80
+ | `images` | Shows a vision model the images it reads with `read_file`. Off by default. |
81
+ | `policyRules`, `sandboxRuntime` | Extra governance rules, and a custom sandbox runtime. |
82
+ | `onEvent` | Receives the event stream, in place of the console subscriber. |
83
+ | `promptCache` | `{ enabled, ttl: "5m" \| "1h" }`, on by default. |
84
+ | `pricing` | USD per million tokens by model id (`{ input, output, cacheRead, cacheWrite }`; `default` matches any id). Without it, usage reports tokens and no cost. |
85
+ | `clock` | Pins "now" for cases. Not for production. |
86
+
87
+ Assembly fails closed. A missing or invalid spec throws `WorkflowLoadError`, a custom
88
+ `backend` without a `sessionStore` throws `SessionStoreRequiredError`, and a host tool
89
+ named `archmax_*`, or after a file operation, throws `ReservedToolNameError`.
90
+
91
+ ## Models
92
+
93
+ By default the model comes from the environment, for any OpenAI-compatible endpoint:
94
+ `ARCHMAX_API_BASE_URL`, `ARCHMAX_API_KEY` and `ARCHMAX_MODEL`, optionally
95
+ `ARCHMAX_TEMPERATURE`, `ARCHMAX_MAX_TOKENS` and `ARCHMAX_STREAMING`. They are read from
96
+ the workspace's `.env`, and the process environment wins.
97
+
98
+ Pass `model` to use your own instance; it then outranks every model id a spec declares.
99
+ Pass `modelFactory` instead to honour those ids, and to put grader states on their own
100
+ model, as the service below does.
101
+
102
+ ## Outcomes and parked sessions
103
+
104
+ `send`, `decide`, `reply` and `deliver` each settle one turn and return what it did:
105
+
106
+ | Field | Meaning |
107
+ | --- | --- |
108
+ | `kind` | `completed`; `parked` (waiting on a person or an event); `failed` (the agent, a grader or a code state called `archmax_raise`, or the turn threw); `rejected` (governance ended it). |
109
+ | `exit` | `{ success: true }` when completed, `{ success: false, code, reason }` when failed. |
110
+ | `reply` | Everything the agent said this turn, in order; `""` when it said nothing. |
111
+ | `state`, `parkedChannel`, `pending` | Where the session is. For a park: `"decision"` or `"input"`, and the park record. |
112
+ | `rejected` | Why, when the outcome is `rejected`. |
113
+ | `delegation` | When a sub-workflow child is the one parked: which child, and which call. |
114
+ | `auditTrail`, `variables`, `usage`, `messages` | The trail, the variables, the cumulative usage and the transcript. |
499
115
 
500
- ### Reading a run's cost
116
+ A host that reads "not rejected" as success must check for `failed` too.
501
117
 
502
- A run's usage is **checkpointed**, so a session handle answers it for a run that
503
- parked and resumed — including in another process:
118
+ A service that waits for people:
504
119
 
505
120
  ```ts
506
- const summary = await agent.sessions.get(sessionId);
507
- summary?.usage;
508
- // { inputTokens, outputTokens, cacheReadTokens, cacheCreationTokens, costUsd? }
509
- ```
510
-
511
- `costUsd` appears only when pricing is configured; without it, tokens are
512
- reported and cost is omitted, never guessed. A parked run reports what it has
513
- spent so far and keeps accumulating on resume. The same totals land in
514
- `sessions/<sessionId>/artifacts/metadata.json` as `usage`, and `createUsageTracker()`
515
- gives the same figure live in process.
516
-
517
- ### Measuring anything else — fold the stream
121
+ import { createAgent, createChatModel, createUsageTracker } from "@archmax-ai/harness";
518
122
 
519
- The SDK keeps no metrics ledger. Tool calls, governance refusals, the path a run
520
- took, every send-back by a grader or code state, and how long a session
521
- sat waiting for a person are all already on the typed event stream, and a backend
522
- counts what it needs:
523
-
524
- ```ts
525
- const tally = { toolCalls: 0, toolBlocks: 0, path: [] as string[] };
123
+ const usage = createUsageTracker({ onEvent: publish }); // publish: your event fan-out
526
124
  const agent = await createAgent({
527
- onEvent: (e) => {
528
- forwardToClient(e);
529
- if (e.type === "tool-called") tally.toolCalls++;
530
- if (e.type === "tool-blocked") tally.toolBlocks++;
531
- if (e.type === "state-enter") tally.path.push(e.state);
532
- },
533
- /* … */
125
+ workflow: "order-lookup",
126
+ workspace: { rootDir },
127
+ tools: hostTools,
128
+ onEvent: usage.handler,
129
+ modelFactory: (role, env, requested) =>
130
+ role === "grader" ? graderModel : createChatModel({ ...env(), model: requested ?? env().model }),
534
131
  });
535
- ```
536
-
537
- Two properties make this the right home for it: the events are the ones the
538
- runtime acts on, so a fold cannot drift from behavior the way a parallel ledger
539
- can; and a backend counts what it cares about — a per-tenant tool budget, a
540
- per-state SLA — rather than what the SDK guessed.
541
-
542
- A fold is a **live view**: it sees only events this process observed. Cost is the
543
- deliberate exception, because it is checkpointed.
544
-
545
- ## Session artifacts
546
-
547
- Each run persists under its own folder in the session store — which is also the
548
- agent's workspace root for that run (`sessions/<sessionId>/` on the filesystem default):
549
-
550
- ```
551
- sessions/<sessionId>/
552
- checkpoints/ # LangGraph checkpoint records (durable resume; also
553
- # the sole record of a parked human decision)
554
- scratchpad/ # the run's working area — intermediate files AND the
555
- # artifacts a state produces (agent-visible as
556
- # `scratchpad/…`; the only such area)
557
- large_tool_results/ # runtime-offloaded oversized tool results (agent reads only)
558
- conversation_history/ # runtime-offloaded history (agent reads only)
559
- artifacts/
560
- graph.json # SerializedGraph (topology; includes mermaid)
561
- graph.mmd # Mermaid diagram
562
- trajectory.json # Trajectory (per-state messages + tool calls)
563
- trail.json # Audit trail (committed transitions + rationale)
564
- variables.json # The run's variables: name -> { value, locked }.
565
- # `locked` is the attribution: locked = host-established
566
- # (variables, a delivery's seeds, the built-in
567
- # `trigger`), unlocked = set by the agent. Written for a
568
- # governed run; an empty store still writes `{}`.
569
- metadata.json # RunMetadata (runtime contract, package version,
570
- # specHash, and the run's `usage` summary — tokens
571
- # by kind plus costUsd when priced)
572
- ```
573
-
574
- Schemas:
132
+ const flow = agent.workflow!;
575
133
 
576
- ```ts
577
- interface SerializedGraph {
578
- nodes: string[];
579
- edges: { source: string; target: string; conditional: boolean; label?: string }[];
580
- entry?: string; finals: string[]; mermaid: string;
581
- }
582
- interface Trajectory {
583
- sessionId?: string; workflow: string; prompt?: string; finalAnswer: string;
584
- segments: { state: string; enteredState?: string; exitedState?: string;
585
- items: (MessageItem | ToolCallItem)[] }[];
134
+ const outcome = await flow.send(sessionId, { message });
135
+ if (outcome.kind === "parked" && outcome.parkedChannel === "decision") {
136
+ // outcome.pending.transitions: [{ to, description, type }], one button each
137
+ await flow.decide(sessionId, { target: chosen.to, comment });
586
138
  }
587
- // MessageItem: { kind:"message", role, content }
588
- // ToolCallItem: { kind:"tool_call", name, args, result: string|null }
589
- interface RunMetadata { sessionId: string; workflow: string;
590
- runtimeContract: ResolvedRuntimeContract; packageVersion: string; }
591
- // trail.json: { sessionId, workflow, steps: TrailStep[] } — the audit trail,
592
- // re-derived from checkpointed state on every artifact write.
593
- // TrailStep: { to, kind: "trigger"|"agent"|"human"|"grader"|"code"|
594
- // "sub-workflow"|"reset"|"on_error", reason?, workflow?, status?, ts }
139
+ usage.totals(sessionId); // tokens, plus costUsd when pricing is set
140
+ agent.dispose(sessionId); // frees the session's in-memory resources
595
141
  ```
596
142
 
597
- An **API** reads these files (through the same session store, or via `runArtifactPaths(sessionId)`
598
- path helpers) to render historical runs: `graph.json`/`graph.mmd` for the diagram,
599
- `trajectory.json` for the step-by-step timeline, `trail.json` for the decision
600
- path (which edges committed, by whom, and why), `variables.json` for the run's
601
- inputs and established facts (also what a test author reads to reproduce a real
602
- firing's payload — see
603
- [code-state-and-test-scripts.md](code-state-and-test-scripts.md#recovering-a-cases-inputs-from-previous-runs)).
604
- Note it holds whatever the run's variables held, verbatim: a host that must not
605
- persist an input should not seed it. To persist artifacts yourself,
606
- stream the run and call `agent.emitRunArtifacts(sessionId, trajectory)`.
607
-
608
- ## Model configuration
609
-
610
- Env (loaded from the workspace root `.env`, real process env wins):
611
- `ARCHMAX_API_BASE_URL`, `ARCHMAX_API_KEY`, `ARCHMAX_MODEL`, optional
612
- `ARCHMAX_TEMPERATURE`, `ARCHMAX_MAX_TOKENS` — any OpenAI-compatible endpoint.
613
- Or inject a model directly:
143
+ - **`decide(sessionId, { target, comment? })`** moves a session parked at a human state
144
+ along the chosen transition. It throws `SessionNotParkedError` when the session awaits
145
+ no decision, and `InvalidDecisionTargetError` for a target that is not one of the
146
+ state's transitions.
147
+ - **`reply(sessionId, message)`** answers a person who writes to a parked session. The
148
+ session stays parked.
149
+ - **`deliver(sessionId, { trigger: { id }, variables? })`** continues a session parked
150
+ with `archmax_wait`, in the state that parked, with the variables locked. Any trigger
151
+ id continues a park. It throws `SessionNotAwaitingInputError` when nothing waits.
152
+ - **The same operations** are payloads of `send`: `{ decision }` and `{ delivery }`.
153
+ - **Scheduled parks.** A park with `until` carries `resumeAt`. The harness schedules
154
+ nothing: your scheduler lists `agent.sessions.list()`, picks the parks whose
155
+ `resumeAt` has passed, and delivers to them. A duplicate delivery is refused.
156
+ - **`resolveSession({ trigger, variables, sessionPath? })`** says which session a
157
+ trigger's arrival belongs to, and whether it is a turn or a delivery, without sending
158
+ it.
159
+ - **Files.** `send(id, { message, files: { invoice: { name, content } } })` places the
160
+ bytes under the session's `scratchpad/attachments/` and seeds `invoice`, locked, with
161
+ the path: what a `file` input takes.
162
+ - **A turn that throws** (a provider failure, a cancellation, the recursion limit)
163
+ rejects with the error, and the session ends `failed` with `turn-error`,
164
+ `turn-cancelled` or `recursion-limit`. The `recursionLimit` is 400 unless your config
165
+ sets one; `send`'s third argument merges into the invoke config. Cancel a turn with an
166
+ `AbortSignal` passed as `{ signal }`: its scripts stop with it. `dispose` in a
167
+ `finally` is always safe.
168
+
169
+ ## Events
170
+
171
+ `onEvent` receives one typed stream, `WorkflowLifecycleEvent`, each event carrying its
172
+ `sessionId`:
173
+
174
+ | Event | Meaning |
175
+ | --- | --- |
176
+ | `state-enter`, `state-leave`, `advance` | The session's movement. `advance` carries `from`, `to`, `reason` and `callId`. |
177
+ | `code-start`, `code-output`, `code-result` | A code state's run: its script, each `console.log` line, its `status` and `reason`. |
178
+ | `grader-start`, `grader-result` | A grader state's run. |
179
+ | `parked`, `decided`, `delivered`, `raised`, `state-error-routed` | Parks, decisions, deliveries, raises and `on_error` routes. |
180
+ | `tool-called`, `tool-result`, `tool-blocked` | Every governed call, `archmax_advance` included, grouped by `callId`. A code state's calls carry `origin: "script"`. |
181
+ | `agent-text-delta`, `agent-text` | Streamed text and the complete message, grouped by `messageId`. A grader's tokens never stream. |
182
+ | `model-usage` | The provider's token counts per model call (a grader's under its state), with `costUsd` when priced. |
183
+ | `variables-set`, `title-set` | Variable writes, by name; the session's `title`, with its value. |
184
+ | `sub-workflow-start`, `sub-workflow-result` | A child session's dispatch and its result. |
185
+ | `warning`, `skills-loaded`, `graph-topology`, `prompt-shaping` | Diagnostics. |
186
+
187
+ There is no end-of-turn event: `send` returning is the end. The ids join events to the
188
+ stored messages: `callId` is the tool call's id, and `messageId` the message's.
189
+
190
+ The harness keeps no metrics ledger: count what you need by folding the stream. Cost is
191
+ the exception. Usage is checkpointed, so `(await agent.sessions.get(id))?.usage` answers
192
+ it for any session.
193
+
194
+ The runtime writes its own messages into the transcript, as tool-call pairs: runtime
195
+ notes such as `[decision]`, `[event]`, `[error]`, `[grader]` and `[code]`. Tell them
196
+ apart with `isRuntimeNote(message)` and `runtimeNoteKind(message)`, never by role or by
197
+ a bracket prefix.
198
+
199
+ ## Sessions, storage and artifacts
200
+
201
+ - **Sessions** are durable by default, under `<rootDir>/sessions/<id>/`.
202
+ `agent.sessions.list()`, `.get(id)` and `.delete(id)` work over the session store.
203
+ Delete through them, never through the file system.
204
+ - **Session stores**: `createFilesystemSessionStore({ dir })`,
205
+ `createBackendSessionStore({ backend, prefix? })` for S3 or any Deep Agents backend
206
+ (how a worker fleet shares sessions), and `createMemorySessionStore()`. A custom
207
+ `backend` needs an explicit `sessionStore`.
208
+ - **Session ids**: a session's files sit at `<id>/<path>` in the store.
209
+ `sessionIdRejection(id)` is the rule every ingress applies. Mint top-level ids without
210
+ `~`, which separates a child session's number (`<parent>~<n>`).
211
+ - **Mounts**: `workspace.mounts` is a route table, key → backend or
212
+ `{ backend, readOnly?, governed?, searchable? }`. `defaultMounts(rootDir)` is the
213
+ conventional table (`skills/`, `.platform/`, `AGENTS.md`), and
214
+ `mountSubtree(backend, "skills")` rebases a backend. Mounts are read-only unless
215
+ declared otherwise. `governed: true` hands the spec's `mounts` lists the decision of
216
+ which states reach a mount. A key over a session zone or `workflows/` throws
217
+ `MountCollisionError`. Pass the same table to `validateWorkflow({ mounts })` so that
218
+ validation checks the spec's mount names.
219
+ - **Artifacts**: `agent.emitRunArtifacts(sessionId, trajectory)` writes `graph.json`,
220
+ `graph.mmd`, `trajectory.json`, `trail.json`, `variables.json` (each variable with its
221
+ value and lock) and `metadata.json` (the contract, the package version, the
222
+ `specHash` and usage) under `sessions/<id>/artifacts/`. An API reads them to draw past
223
+ sessions.
224
+ - **Spec edits** are adopted. The next turn runs under the current `workflow.yaml`, the
225
+ recorded `specHash` says which version a turn ran under (`agent.getSpecSnapshot(hash)`
226
+ returns it), and a session positioned in a removed state reopens at its trigger's
227
+ entry.
228
+
229
+ ## Host tools that use the workspace
230
+
231
+ `toolsFromMap({ <name>: { description, inputSchema, paths?, handler } })` builds host
232
+ tools whose `handler(input, { workspace, sessionId })` reads and writes through the
233
+ turn's workspace, bound to the session. Inside a sub-workflow, `sessionId` is the child's.
234
+ Declared `paths` (`{ path: "read" }`) are governed like a built-in tool's path
235
+ arguments, so a refused call never runs the handler.
236
+
237
+ ## Exposing a workflow to callers
238
+
239
+ A trigger's signature is the contract the runtime enforces, so build an MCP tool, an API
240
+ operation or a start form from it. `@archmax-ai/harness/spec` is browser-safe and holds
241
+ the helpers (they are also on the root):
242
+
243
+ - `parseMachineSpec(raw)` parses a spec; `signatureForTrigger(spec, "manual")` returns
244
+ `{ description?, requires, returns }`.
245
+ - `signatureJsonSchema(entries)` returns a JSON Schema object with every name required,
246
+ `date`, `date-time` and `file` as formatted strings, and each rule JSON Schema can
247
+ express as its keyword (date bounds, `accept` and `maxSize` are left out; the runtime
248
+ still enforces them).
249
+ - `signatureValueIssues(entries, values, options?)` returns one issue per missing or
250
+ non-conforming value, a broken rule as an `invalid` issue carrying `rule`. A
251
+ `pattern` runs in RE2, so it is safe on anyone's input. It never coerces, so convert
252
+ form text first.
253
+ - Use the trigger's `description` as the tool's description: the session never sees it.
254
+ Read `requires` and `returns` through `normalizeSignature`, never as `string[]`.
614
255
 
615
- ```ts
616
- import { ChatOpenAI } from "@langchain/openai";
617
- const model = new ChatOpenAI({
618
- model: "claude-sonnet-5",
619
- apiKey: process.env.MY_KEY,
620
- configuration: { baseURL: "https://api.example.com/v1" },
621
- });
622
- const agent = await createAgent({ model });
623
- ```
256
+ ## Cases against your agent
624
257
 
625
- ## Custom backends
626
-
627
- `BackendProtocolV2` (from `deepagents`) decouples storage — implement
628
- `readRaw`/`write`/`edit`/`ls` over filesystem, a store, a sandbox, S3, or a remote.
629
- Pass it as `backend` (authored workspace); run state goes through a `sessionStore`
630
- (e.g. `createBackendSessionStore({ backend: s3Backend, prefix: "tenants/a/runs" })`).
631
- A custom `backend` **requires** an explicit `sessionStore` — assembly throws
632
- `SessionStoreRequiredError` instead of inferring storage. The SDK owns the logical
633
- `sessions/<sessionId>/…` namespace; the store owns where it physically lives.
634
- Persisting the session zone off-box (an S3-backed session store) is how you share run
635
- state across a worker fleet and an API.
636
-
637
- A session's files are stored under `<sessionId>/` in the store (beneath its
638
- `prefix`): the agent's `scratchpad/x.txt` in session `s1` is `s1/scratchpad/x.txt`.
639
- That address is a contract, so a host may read or write a session's files through
640
- its own handle on the store (ranged reads, recursive listings, presigned URLs). A
641
- host tool keys them by `ToolContext.sessionId`, which inside a sub-workflow is the
642
- child's id, never the session the turn was sent to. `sessionIdRejection` refuses an
643
- id holding `~` in any shape but a child's (`<parent>~<n>`): mint top-level ids
644
- without it.
645
-
646
- `downloadFiles`, `uploadFiles` and `delete` are optional in the protocol and stay
647
- optional in the workspace: a store with only the required methods serves skills,
648
- reads and text copies. `downloadFiles` is always present and reads a store without
649
- it through `readRaw` (exact bytes or `permission_denied`, never altered text);
650
- `uploadFiles` is present when the session store has it, and a mount without it
651
- refuses that file; `delete` answers a store without it with an error. What each
652
- buys the file operations (`copy_file`, `move_file`, `remove_file`): a write to a
653
- binary-typed path (an image, a PDF) carries **base64**, which the backend's
654
- `write` must decode — Deep Agents' convention, with the extensions in
655
- `BINARY_MIME_TYPES` on `@archmax-ai/harness/spec`. Other bytes the text channel
656
- cannot carry (a `.docx`, a `.zip`, Latin-1 text) need `uploadFiles`, and a move
657
- or removal needs `delete`. Every copy is read back; a store that kept something
658
- else is refused, never reported as copied. A read-only mount refuses an upload
659
- and a delete itself. Deep Agents is handed the workspace without `delete`, so its
660
- own recursive `delete` tool never exists: `remove_file` is the one way to delete
661
- a file.
662
-
663
- A store's `grep` receives the call's match cap as its fourth argument,
664
- `maxCount` (the agent's `max_count`, else Deep Agents' 1,000). A store may stop
665
- there and return `truncated: true`; the workspace caps one that ignores it.
666
-
667
- ### Host tools
668
-
669
- A host tool built with `toolsFromMap` reaches the workspace the way the
670
- built-in file tools do — no second routing table:
258
+ `runTests` from `@archmax-ai/harness/testing` runs a workflow's cases with the discovery,
259
+ judge and verdicts of `archmax test`. By default it builds its own agent, with none of
260
+ your tools, mounts or stores. Supply yours:
671
261
 
672
- ```ts
673
- const tools = toolsFromMap({
674
- get_markdown: {
675
- description: "Read a document as Markdown.",
676
- inputSchema: { type: "object", properties: { path: { type: "string" } }, required: ["path"] },
677
- paths: { path: "read" }, // governed like read_file's file_path
678
- handler: async (input, { workspace }) => {
679
- const raw = await workspace.readRaw(String(input.path));
680
- return raw.error ? `Error: ${raw.error}` : toMarkdown(raw.data!);
681
- },
682
- },
683
- });
684
- const agent = await createAgent({ tools, essentialTools: ["get_markdown"] });
685
- ```
262
+ | Option | Why |
263
+ | --- | --- |
264
+ | `createTarget` | Your agent, or a factory `({ workflow, rootDir, onEvent }) => agent`. It must be workflow-governed. Wire `createToolMockMiddleware()` into its `middleware`, or a case that declares `mocks:` is refused before it runs. |
265
+ | `authoring` | Where the spec, the cases and their fixtures are read. When omitted, they are read from files under `rootDir`, so a workspace kept in a store finds no cases. |
266
+ | `sessionIdForCase` | The session id each case runs on, so its files and usage join your records. |
686
267
 
687
- - `paths` declares the arguments that name workspace paths and how the call
688
- uses each. Every path rule binds them: the read-only zone, the runtime's own
689
- areas, the skills and mounts the state was given (`mount.not-allowed`,
690
- `skill.not-allowed`), a mount's `access: read` or a governed mount no grant
691
- of the workflow asks `read_write` for (`mount.read-only` for a
692
- `write` or `remove`), inherited denials and `paths:` guards. A refused call
693
- never runs the handler. No declaration, no path rules. An argument may hold a
694
- list of paths (`files: string[]`), each governed; any other shape is refused.
695
- Declaring paths only narrows: `scratchpad/` is open in every state to the
696
- default file tools the state is granted and to `essentialTools`, so a tool a
697
- state must grant stays refused where it is not.
698
- - `context.workspace` is the turn's workspace, bound to the session — the
699
- instance `read_file` uses — with `downloadFiles`, `uploadFiles` (when the
700
- session store has it) and `delete` besides the text methods. The plain agent
701
- hands the same context.
702
- - `context.sessionId` is the session the call is made in: inside a sub-workflow,
703
- the child's own id (`<parent>~<n>`), for the model's call and a script's alike.
704
- Key any session file your host stores itself by it (`<sessionId>/<path>` in the
705
- session store), never by the session you sent the turn to.
706
- - Outside a turn, reading either throws; both are non-enumerable there, so
707
- comparing or logging the context does not.
268
+ Cases run one at a time, and `tests.caseTimeoutMs` bounds each.
708
269
 
709
- ## Cases against your agent
270
+ ## Sub-workflows in a service
710
271
 
711
- `runTests` runs a workflow's cases — declarative YAML documents
712
- under `workflows/<name>/tests/*.test.yaml`, interpreted host-side — with the
713
- same discovery, case interpretation, judge, and verdicts `archmax test` uses.
714
- By default it builds its own agent, which is *not* your agent: no connection
715
- tools, no mounts, no session store, no checkpointer — and it reads the suite off
716
- the local filesystem under `rootDir`, which is *not* your authored tree if that
717
- tree lives in a store. Supply both:
272
+ A child session is composed from its caller's inputs: the model and `modelFactory`, the
273
+ backend and mounts, the session store and checkpointer, the host tools, the policy
274
+ rules, the prompt cache and the pricing. Composition is lazy, per slug, so a child
275
+ whose spec is broken fails its dispatch, not the assembly. The child is its own session
276
+ (`<parent>~<n>`) in the same store, listed by `sessions.list()` with `parentSessionId`.
718
277
 
719
- ```ts
720
- import { runTests, createAgent, createToolMockMiddleware } from "@archmax-ai/harness";
721
-
722
- const { results, exitCode } = await runTests({
723
- workflow,
724
- rootDir,
725
- // Where the spec, the case documents and their `from:` fixtures are read
726
- // from. Omit it only when they really are files under `rootDir`.
727
- authoring,
728
- createTarget: async ({ workflow, rootDir, onEvent }) =>
729
- createAgent({
730
- workflow,
731
- onEvent,
732
- backend,
733
- tools: await resolveConnectionTools(tenant),
734
- checkpointer,
735
- middleware: [createToolMockMiddleware()],
736
- // REQUIRED — see below,
737
- workspace: { rootDir, sessionStore },
738
- }),
739
- sessionIdForCase: (file) => `${hostRunId}/${file}`,
740
- });
741
- ```
278
+ ## A typical split
742
279
 
743
- | Option | Why |
744
- | --- | --- |
745
- | `authoring` | The backend the **suite itself** is read from: the machine spec, `workflows/<name>/tests/*.test.yaml`, and the fixtures their `from:` entries name. The same option `createAgent` takes, and normally the same backend — the agent under test runs the authored tree the cases were read from. Defaults to a filesystem backend over the resolved root, so a host whose authored tree lives in a store and omits it discovers **no cases at all** and gets an empty run rather than a failure. |
746
- | `createTarget` | The agent under test — an already-built runtime, or a factory that builds one. Must be **workflow-governed** — a plain agent is refused before any case runs. In the factory form, `rootDir` in the context is the resolved workspace root. |
747
- | `sessionIdForCase` | Names the *agent* session the case runs on — a case is one conversation on one session (default `test-default-<runId>-<file>`), invoked once per case file — so its checkpoints, `scratchpad/…` files, and token usage join your own run record. |
748
-
749
- **Your target owns its mocks — and the runtime checks.** A case's `mocks:`
750
- list intercepts agent-initiated tool calls through
751
- `createToolMockMiddleware()`, which the default target wires for you.
752
- `createAgent` stamps `capabilities.toolMocks` from the middleware
753
- actually wired; a case that declares mocks against a target without the
754
- capability **fails loudly before the agent runs**, naming the fix — never run
755
- with partial interception (scripts' PTC calls mocked, the agent's own calls
756
- real). A bespoke target that intercepts by other means sets
757
- `capabilities.toolMocks` itself. Mockless cases run against any
758
- workflow-governed target.
759
-
760
- **Sub-workflows inherit the assembly.** When a workflow delegates (by calling
761
- an `archmax_workflow_<slug>` tool), the child runtime is
762
- composed from the *same* resolved inputs as the parent — model and
763
- `modelFactory`, `backend` and `mounts`, `sessionStore` and `checkpointer`, host
764
- `tools` and `essentialTools`, `sandboxRuntime`,
765
- `policyRules`, `promptCache` and `pricing` — differing only in
766
- the machine that governs it and the system prompt that machine renders. So a
767
- custom backend serves the child's spec too, and a `modelFactory` supplies its
768
- model. Composition is **lazy and memoized per workflow slug**: a workflow that is
769
- never delegated to is never loaded, and one delegated from many places is
770
- composed once. A child whose spec is broken surfaces as a *dispatch* failure, not
771
- an assembly failure — a parent still assembles.
772
-
773
- The child runs as a **separate session**, `<parent>~<n>`, in the same session
774
- store, with its own checkpoints, its own token accounting and its own file space:
775
- its `scratchpad/` is not its caller's, so it can neither read nor overwrite a file
776
- of its caller's. `n` is the dispatch's number in the calling session, counted from 1
777
- across all its turns in its checkpointed state, so an agent built per turn never
778
- reuses one; a grandchild is `<parent>~<n>~<m>`. The `sub-workflow-*` events and a
779
- pending delegation name it by its `dispatchId`, the id below the root session (`1`,
780
- `1~1`). A host tool called in the child gets the child's id as
781
- `ToolContext.sessionId`, and its file work belongs to the child. `sessions.list()` reports it beside its caller
782
- with `parentSessionId` naming the caller, and `sessions.delete` removes it by its
783
- own id. The only files that cross are the runtime's copies of declared `file` arguments and
784
- `file` returns. `capabilities.subWorkflows` is stamped `true` for a governed
785
- assembly and `false` for the plain one.
786
-
787
- Omit both options and behavior is identical to `archmax test`. Cases run
788
- sequentially (`tests.maxConcurrency` above 1 is rejected, not silently
789
- serialized) and there is no cancellation signal: call `runTests` once per file
790
- (`filter`) if you need to interrupt between cases; a single case is bounded by
791
- `tests.caseTimeoutMs`.
792
-
793
- ## Suggested split
794
-
795
- - **Worker**: `createAgent` + `agent.invoke`/`stream`; `agent.workflow.decide` for
796
- approvals. Owns execution and the durable session store/checkpointer.
797
- - **API**: reads `sessions/<sessionId>/artifacts/*` (shared session store) and/or relays
798
- the `onEvent` stream; exposes `sessions.list()`, run detail, and a decide endpoint.
799
- - **Frontend**: renders `graph.json`/`graph.mmd`, the `trajectory.json` timeline,
800
- the `variables.json` inputs, and the live event stream; posts human decisions
801
- back to the API.
802
-
803
- Keep the heavy runtime import (LangChain/LangGraph) in the worker; the API/frontend
804
- only need the artifact/event *types*, which ship in `dist/index.d.ts`.
280
+ - **Worker**: `createAgent` and `send`. It owns the session store.
281
+ - **API**: reads artifacts from the shared session store, relays `onEvent`, and exposes
282
+ `sessions.list()`, a session's detail and a decide endpoint.
283
+ - **Frontend**: draws `graph.json`, the trail and the live stream, and posts decisions to
284
+ the API. It needs only the types, which ship in `dist/index.d.ts`.