@bastani/atomic 0.5.34 → 0.6.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 +329 -50
  2. package/dist/commands/cli/session.d.ts +67 -0
  3. package/dist/commands/cli/session.d.ts.map +1 -0
  4. package/dist/commands/cli/workflow-status.d.ts +63 -0
  5. package/dist/commands/cli/workflow-status.d.ts.map +1 -0
  6. package/dist/sdk/commander.d.ts +74 -0
  7. package/dist/sdk/commander.d.ts.map +1 -0
  8. package/dist/sdk/components/workflow-picker-panel.d.ts +14 -17
  9. package/dist/sdk/components/workflow-picker-panel.d.ts.map +1 -1
  10. package/dist/sdk/define-workflow.d.ts +18 -9
  11. package/dist/sdk/define-workflow.d.ts.map +1 -1
  12. package/dist/sdk/index.d.ts +4 -3
  13. package/dist/sdk/index.d.ts.map +1 -1
  14. package/dist/sdk/management-commands.d.ts +42 -0
  15. package/dist/sdk/management-commands.d.ts.map +1 -0
  16. package/dist/sdk/registry.d.ts +27 -0
  17. package/dist/sdk/registry.d.ts.map +1 -0
  18. package/dist/sdk/runtime/attached-footer.d.ts +1 -1
  19. package/dist/sdk/runtime/executor-env.d.ts +20 -0
  20. package/dist/sdk/runtime/executor-env.d.ts.map +1 -0
  21. package/dist/sdk/runtime/executor.d.ts +61 -10
  22. package/dist/sdk/runtime/executor.d.ts.map +1 -1
  23. package/dist/sdk/types.d.ts +147 -4
  24. package/dist/sdk/types.d.ts.map +1 -1
  25. package/dist/sdk/worker-shared.d.ts +42 -0
  26. package/dist/sdk/worker-shared.d.ts.map +1 -0
  27. package/dist/sdk/workflow-cli.d.ts +103 -0
  28. package/dist/sdk/workflow-cli.d.ts.map +1 -0
  29. package/dist/sdk/workflows/builtin-registry.d.ts +113 -0
  30. package/dist/sdk/workflows/builtin-registry.d.ts.map +1 -0
  31. package/dist/sdk/workflows/index.d.ts +5 -5
  32. package/dist/sdk/workflows/index.d.ts.map +1 -1
  33. package/package.json +12 -8
  34. package/src/cli.ts +85 -144
  35. package/src/commands/cli/chat/index.ts +10 -0
  36. package/src/commands/cli/workflow-command.test.ts +279 -938
  37. package/src/commands/cli/workflow-inputs.test.ts +41 -11
  38. package/src/commands/cli/workflow-inputs.ts +47 -12
  39. package/src/commands/cli/workflow-list.test.ts +234 -0
  40. package/src/commands/cli/workflow-list.ts +0 -0
  41. package/src/commands/cli/workflow.ts +11 -798
  42. package/src/scripts/constants.ts +2 -1
  43. package/src/sdk/commander.ts +161 -0
  44. package/src/sdk/components/workflow-picker-panel.tsx +78 -258
  45. package/src/sdk/define-workflow.test.ts +104 -11
  46. package/src/sdk/define-workflow.ts +47 -11
  47. package/src/sdk/errors.test.ts +16 -0
  48. package/src/sdk/index.ts +8 -8
  49. package/src/sdk/management-commands.ts +151 -0
  50. package/src/sdk/registry.ts +132 -0
  51. package/src/sdk/runtime/attached-footer.ts +1 -1
  52. package/src/sdk/runtime/executor-env.ts +45 -0
  53. package/src/sdk/runtime/executor.test.ts +37 -0
  54. package/src/sdk/runtime/executor.ts +147 -68
  55. package/src/sdk/types.ts +169 -4
  56. package/src/sdk/worker-shared.test.ts +163 -0
  57. package/src/sdk/worker-shared.ts +155 -0
  58. package/src/sdk/workflow-cli.ts +409 -0
  59. package/src/sdk/workflows/builtin/deep-research-codebase/claude/index.ts +1 -1
  60. package/src/sdk/workflows/builtin/deep-research-codebase/copilot/index.ts +1 -1
  61. package/src/sdk/workflows/builtin/deep-research-codebase/opencode/index.ts +1 -1
  62. package/src/sdk/workflows/builtin/open-claude-design/claude/index.ts +1 -1
  63. package/src/sdk/workflows/builtin/open-claude-design/copilot/index.ts +1 -1
  64. package/src/sdk/workflows/builtin/open-claude-design/opencode/index.ts +1 -1
  65. package/src/sdk/workflows/builtin/ralph/claude/index.ts +1 -1
  66. package/src/sdk/workflows/builtin/ralph/copilot/index.ts +1 -1
  67. package/src/sdk/workflows/builtin/ralph/opencode/index.ts +1 -1
  68. package/src/sdk/workflows/builtin-registry.ts +23 -0
  69. package/src/sdk/workflows/index.ts +10 -20
  70. package/src/services/system/auth.test.ts +63 -1
  71. package/.agents/skills/workflow-creator/SKILL.md +0 -334
  72. package/.agents/skills/workflow-creator/references/agent-sessions.md +0 -888
  73. package/.agents/skills/workflow-creator/references/computation-and-validation.md +0 -201
  74. package/.agents/skills/workflow-creator/references/control-flow.md +0 -470
  75. package/.agents/skills/workflow-creator/references/discovery-and-verification.md +0 -232
  76. package/.agents/skills/workflow-creator/references/failure-modes.md +0 -903
  77. package/.agents/skills/workflow-creator/references/getting-started.md +0 -275
  78. package/.agents/skills/workflow-creator/references/running-workflows.md +0 -235
  79. package/.agents/skills/workflow-creator/references/session-config.md +0 -384
  80. package/.agents/skills/workflow-creator/references/state-and-data-flow.md +0 -357
  81. package/.agents/skills/workflow-creator/references/user-input.md +0 -234
  82. package/.agents/skills/workflow-creator/references/workflow-inputs.md +0 -272
  83. package/dist/sdk/runtime/discovery.d.ts +0 -132
  84. package/dist/sdk/runtime/discovery.d.ts.map +0 -1
  85. package/dist/sdk/runtime/executor-entry.d.ts +0 -11
  86. package/dist/sdk/runtime/executor-entry.d.ts.map +0 -1
  87. package/dist/sdk/runtime/loader.d.ts +0 -70
  88. package/dist/sdk/runtime/loader.d.ts.map +0 -1
  89. package/dist/version.d.ts +0 -2
  90. package/dist/version.d.ts.map +0 -1
  91. package/src/commands/cli/workflow.test.ts +0 -317
  92. package/src/sdk/runtime/discovery.ts +0 -368
  93. package/src/sdk/runtime/executor-entry.ts +0 -18
  94. package/src/sdk/runtime/loader.ts +0 -267
@@ -1,888 +0,0 @@
1
- # Agent Sessions
2
-
3
- Each `ctx.stage()` call inside a workflow's `.run()` callback creates an isolated agent session. The runtime auto-initializes the provider client and session before invoking your callback — the callback receives `s` (a `SessionContext`) with `s.client` (the pre-created SDK client) and `s.session` (the pre-created session) ready to use. Auto-cleanup (disconnect, stop) is handled by the runtime after the callback completes. This is the programmatic equivalent of defining agent stages — you have full access to every SDK feature through `s.client` and `s.session`.
4
-
5
- `ctx.stage()` takes four arguments: `ctx.stage(stageOpts, clientOpts, sessionOpts, callback)`.
6
-
7
- ## Claude Agent SDK
8
-
9
- Claude runs as a full interactive TUI in a tmux pane. The runtime auto-starts the Claude CLI (via `s.client`) and creates a session wrapper (`s.session`) before the callback runs. Pass CLI flags via `clientOpts` (2nd arg). Claude has **no per-session options** — the 3rd arg must be `{}`.
10
-
11
- ### Session lifecycle
12
-
13
- ```ts
14
- import { defineWorkflow } from "@bastani/atomic/workflows";
15
-
16
- // ...
17
- .run(async (ctx) => {
18
- await ctx.stage(
19
- { name: "implement", description: "Implement the feature" },
20
- {}, // clientOpts: chatFlags and readyTimeoutMs go here
21
- {}, // sessionOpts: must be {} for Claude — no per-session options exist
22
- async (s) => {
23
- // s.client — Claude CLI wrapper (already started by runtime)
24
- // s.session — session wrapper (ready to accept queries via s.session.query())
25
-
26
- // Send queries — Claude maintains conversation context across calls
27
- // Returns SessionMessage[] (native SDK type from @anthropic-ai/claude-agent-sdk)
28
- const result = await s.session.query((s.inputs.prompt ?? ""));
29
-
30
- // Save transcript
31
- s.save(s.sessionId);
32
- },
33
- );
34
- })
35
- ```
36
-
37
- The runtime handles:
38
- 1. Starting the Claude CLI in the tmux pane
39
- 2. Creating a session wrapper bound to the pane (exposes `s.session.query()`)
40
- 3. Auto-cleanup after the callback returns
41
-
42
- Client options (2nd arg to `ctx.stage()`):
43
- - `chatFlags` — CLI flags (default: `["--allow-dangerously-skip-permissions", "--dangerously-skip-permissions"]`)
44
- - `readyTimeoutMs` — timeout waiting for TUI readiness (default: 30s)
45
-
46
- Session options (3rd arg to `ctx.stage()`): **none**. The Claude session wrapper takes no configuration — interactive delivery is driven entirely by the CLI's Stop hook, and idle detection watches the Stop-hook marker automatically. Pass `{}`.
47
-
48
- For **headless Claude** (`headless: true` in `stageOpts`), `s.session.query(prompt, options)` forwards its second argument as `Partial<SDKOptions>` to the Agent SDK — that's where you pass `agent`, `permissionMode`, `allowDangerouslySkipPermissions`, etc. (see the headless example further down).
49
-
50
- No manual timeout is needed — idle detection watches for the pane prompt to return, and the session transcript is used to extract the response text.
51
-
52
- ### Basic usage with `s.session.query()`
53
-
54
- ```ts
55
- import { defineWorkflow } from "@bastani/atomic/workflows";
56
-
57
- export default defineWorkflow({
58
- name: "implement",
59
- inputs: [{ name: "prompt", type: "text", required: true, description: "task prompt" }],
60
- })
61
- .for<"claude">()
62
- .run(async (ctx) => {
63
- await ctx.stage(
64
- { name: "implement", description: "Implement the feature" },
65
- {},
66
- {},
67
- async (s) => {
68
- const messages = await s.session.query((s.inputs.prompt ?? ""));
69
- // messages is SessionMessage[] — native SDK type
70
- // Use extractAssistantText(messages, 0) to get the text response
71
- s.save(s.sessionId);
72
- },
73
- );
74
- })
75
- .compile();
76
- ```
77
-
78
- `s.session.query(prompt)` sends text to the Claude pane, verifies delivery, retries if needed, and waits for output stabilization. Returns `SessionMessage[]` (the native transcript messages from this turn, imported from `@anthropic-ai/claude-agent-sdk`). Use `extractAssistantText(messages, 0)` to extract the plain text response.
79
-
80
- ### Multi-turn conversations
81
-
82
- Claude maintains conversation context across calls within the same pane. Call `s.session.query()` multiple times in one stage for multi-turn conversations:
83
-
84
- ```ts
85
- .run(async (ctx) => {
86
- await ctx.stage({ name: "implement" }, {}, {}, async (s) => {
87
- // Turn 1: Plan
88
- await s.session.query("Plan the implementation.");
89
- // Turn 2: Execute (Claude remembers the plan)
90
- await s.session.query("Now implement the plan.");
91
- // Turn 3: Verify
92
- await s.session.query("Run the tests.");
93
- s.save(s.sessionId);
94
- });
95
- })
96
- ```
97
-
98
- ### Advanced: Claude Agent SDK `query()` option surface (reference only)
99
-
100
- **Do not import `query` from `@anthropic-ai/claude-agent-sdk` inside a `ctx.stage()` callback.** In a non-headless stage it double-spawns Claude (idle TUI pane + in-process SDK call) — see `failure-modes.md` §F16. In a headless stage it bypasses the runtime's wiring. Always go through `s.session.query()`; the runtime forwards options to the SDK for headless stages and routes the interactive TUI for non-headless stages.
101
-
102
- Two correct routes:
103
-
104
- 1. **Headless + SDK options** — `s.session.query(prompt, sdkOptions)` inside `{ headless: true }`.
105
- 2. **Interactive TUI + `--agent`** — `chatFlags: ["--agent", "<name>", ...]` in `clientOpts`.
106
-
107
- For the full SDK option surface, see `session-config.md` §"`query()` options".
108
-
109
- Example workflow usage in a headless stage:
110
-
111
- ```ts
112
- await ctx.stage({ name: "implement", headless: true }, {}, {}, async (s) => {
113
- const messages = await s.session.query(s.inputs.prompt ?? "", {
114
- model: "claude-opus-4-6",
115
- permissionMode: "acceptEdits",
116
- allowedTools: ["Read", "Write", "Edit", "Bash", "Grep", "Glob"],
117
- outputFormat: {
118
- type: "json_schema",
119
- schema: { type: "object", properties: { tasks: { type: "array", items: { type: "string" } } } },
120
- },
121
- });
122
- s.save(s.sessionId);
123
- return extractAssistantText(messages, 0);
124
- });
125
- ```
126
-
127
- Key `query()` options:
128
- - `model` — model ID (`"claude-opus-4-6"`, `"claude-sonnet-4-6"`) or alias (`"opus"`, `"sonnet"`, `"haiku"`)
129
- - `effort` — reasoning effort (`"low"`, `"medium"`, `"high"`, `"xhigh"`, `"max"` — `"max"` is Opus 4.6/4.7 only)
130
- - `thinking` — thinking/reasoning config: `{ type: "adaptive" }` (default for supported models), `{ type: "enabled", budgetTokens: N }`, or `{ type: "disabled" }`
131
- - `maxTurns` — maximum conversation turns
132
- - `maxBudgetUsd` — spending cap in USD
133
- - `permissionMode` — `"default"`, `"dontAsk"`, `"acceptEdits"`, `"bypassPermissions"`, `"plan"`
134
- - `allowedTools` / `disallowedTools` — tool access control
135
- - `tools` — base set of available built-in tools: `string[]` for specific tools, `[]` to disable all, or `{ type: "preset", preset: "claude_code" }` for defaults
136
- - `systemPrompt` — custom system prompt (`string`) or preset with additions (`{ type: "preset", preset: "claude_code", append: "..." }`)
137
- - `outputFormat` — structured output: `{ type: "json_schema", schema: { ... } }`
138
- - `agents` — `Record<string, AgentDefinition>` — named subagents for orchestration
139
- - `agent` — main thread agent name (must be defined in `agents` or settings)
140
- - `resume` — session ID to resume a prior session
141
- - `forkSession` — `boolean` — when `true` with `resume`, forks to a new session instead of continuing
142
- - `mcpServers` — MCP server configurations
143
- - `hooks` — `Partial<Record<HookEvent, HookCallbackMatcher[]>>` — event-driven callbacks (see `session-config.md`)
144
- - `sandbox` — sandboxed command execution settings
145
- - `betas` — enable beta features (e.g. `["context-1m-2025-08-07"]` for 1M context on Sonnet)
146
-
147
- ### Subagents
148
-
149
- Claude supports parallel subagents via the `agents` option (a
150
- `Record<string, AgentDefinition>` keyed by agent name). In a workflow,
151
- pass the option through `s.session.query(prompt, sdkOptions)` in a
152
- **headless** stage — see §F16 for why the raw SDK `query()` import is
153
- an anti-pattern:
154
-
155
- ```ts
156
- await ctx.stage(
157
- { name: "implement-and-review", headless: true },
158
- {}, {},
159
- async (s) => {
160
- const messages = await s.session.query(
161
- "Implement and review the feature",
162
- {
163
- agents: {
164
- worker: {
165
- description: "Implement a single task",
166
- prompt: "You are a task implementer...",
167
- tools: ["Read", "Write", "Edit", "Bash"],
168
- },
169
- reviewer: {
170
- description: "Review code changes",
171
- prompt: "You are a code reviewer...",
172
- tools: ["Read", "Grep", "Glob"],
173
- },
174
- },
175
- },
176
- );
177
- s.save(s.sessionId);
178
- return extractAssistantText(messages, 0);
179
- },
180
- );
181
- ```
182
-
183
- ### Session continuity
184
-
185
- Resume or fork prior sessions through `s.session.query()` in a headless
186
- stage (same reasoning as Subagents above — never import `query` directly):
187
-
188
- ```ts
189
- // Resume a session (continues the same conversation)
190
- await ctx.stage({ name: "continue", headless: true }, {}, {}, async (s) => {
191
- const messages = await s.session.query("Continue...", { resume: sessionId });
192
- s.save(s.sessionId);
193
- return extractAssistantText(messages, 0);
194
- });
195
-
196
- // Fork a session (creates a new branch from the session's history)
197
- await ctx.stage({ name: "fork", headless: true }, {}, {}, async (s) => {
198
- const messages = await s.session.query(
199
- "Try a different approach",
200
- { resume: sessionId, forkSession: true },
201
- );
202
- s.save(s.sessionId);
203
- return extractAssistantText(messages, 0);
204
- });
205
- ```
206
-
207
- ### Subagent delegation
208
-
209
- For stages that call a single subagent, use `--agent` (interactive) or the SDK `agent` option (headless) to route all prompts through that agent. The agent must be defined in `.claude/agents/` or `.agents/skills/`.
210
-
211
- **Interactive stages** — pass `--agent` via `chatFlags` in client opts (2nd arg):
212
-
213
- ```ts
214
- .run(async (ctx) => {
215
- await ctx.stage(
216
- { name: "plan" },
217
- { chatFlags: ["--agent", "planner", "--allow-dangerously-skip-permissions", "--dangerously-skip-permissions"] },
218
- {},
219
- async (s) => {
220
- await s.session.query(`Create a plan for: ${(s.inputs.prompt ?? "")}`);
221
- s.save(s.sessionId);
222
- },
223
- );
224
- })
225
- ```
226
-
227
- **Headless stages** — pass `agent` via SDK options in the `query()` call:
228
-
229
- ```ts
230
- .run(async (ctx) => {
231
- const handle = await ctx.stage(
232
- { name: "locate", headless: true },
233
- {}, {},
234
- async (s) => {
235
- const result = await s.session.query(
236
- "Find all API endpoint files",
237
- { agent: "codebase-locator", permissionMode: "bypassPermissions", allowDangerouslySkipPermissions: true },
238
- );
239
- s.save(s.sessionId);
240
- return extractAssistantText(result, 0);
241
- },
242
- );
243
- })
244
- ```
245
-
246
- > **Note:** The `@"agent-name (agent)"` prompt prefix is for multi-agent conversations in a single stage where you switch between agents mid-session. For single-agent stages, prefer `--agent` (interactive) or the `agent` SDK option (headless) as shown above.
247
-
248
- ### Headless mode (background stages)
249
-
250
- Claude headless stages use the Agent SDK's `query()` API directly in-process instead of automating a tmux pane. Set `headless: true` in the stage options. SDK options like `agent`, `permissionMode`, and `allowDangerouslySkipPermissions` can be passed directly in the `query()` call:
251
-
252
- ```ts
253
- import { defineWorkflow, extractAssistantText } from "@bastani/atomic/workflows";
254
-
255
- // ...
256
- await ctx.stage(
257
- { name: "background-analysis", headless: true },
258
- {}, {},
259
- async (s) => {
260
- const result = await s.session.query(
261
- "Analyze the codebase.",
262
- { agent: "codebase-analyzer", permissionMode: "bypassPermissions", allowDangerouslySkipPermissions: true },
263
- );
264
- s.save(s.sessionId);
265
- return extractAssistantText(result, 0);
266
- },
267
- );
268
- ```
269
-
270
- The callback interface is identical to interactive stages — `s.session.query()` returns `SessionMessage[]` in both cases. Internally, the runtime uses `HeadlessClaudeSessionWrapper` which calls `query()` from `@anthropic-ai/claude-agent-sdk` directly. No tmux pane is created, and the stage is invisible in the workflow graph.
271
-
272
- **Design principle:** Never create custom message types. All provider return types are native SDK types — `SessionMessage[]` for Claude, `SessionEvent[]` for Copilot, `SessionPromptResponse` for OpenCode. Use `extractAssistantText()` to extract plain text from Claude's `SessionMessage[]`.
273
-
274
- ## Copilot SDK
275
-
276
- Copilot uses a client-server architecture. The runtime auto-creates a `CopilotClient` (as `s.client`) and a `CopilotSession` (as `s.session`) before invoking your callback. Auto-cleanup (`session.disconnect()` and `client.stop()`) is handled by the runtime after the callback completes.
277
-
278
- ### Basic usage
279
-
280
- ```ts
281
- import { defineWorkflow } from "@bastani/atomic/workflows";
282
-
283
- export default defineWorkflow({
284
- name: "implement",
285
- inputs: [{ name: "prompt", type: "text", required: true, description: "task prompt" }],
286
- })
287
- .for<"copilot">()
288
- .run(async (ctx) => {
289
- await ctx.stage(
290
- { name: "implement" },
291
- {}, // clientOpts: CopilotClientOptions (excluding cliUrl, which is auto-injected)
292
- {}, // sessionOpts: CopilotSessionConfig (model, agent, tools, hooks, etc.)
293
- async (s) => {
294
- // s.client — CopilotClient (already started by runtime)
295
- // s.session — CopilotSession (already created, foreground session set)
296
-
297
- await s.session.send({ prompt: (s.inputs.prompt ?? "") });
298
-
299
- s.save(await s.session.getMessages());
300
- },
301
- );
302
- })
303
- .compile();
304
- ```
305
-
306
- ### `send` vs `sendAndWait`: choosing the right method
307
-
308
- **Always use `send`** for Copilot workflow stages. Inside a stage callback the
309
- Atomic runtime wraps `s.session.send()` so the returned promise only resolves
310
- when the session emits `session.idle` — the same semantics as Claude's
311
- `query()` and OpenCode's `session.prompt()`. The wrapper has **no timeout**,
312
- so long-running planners and reviewers are safe. This is different from the
313
- raw Copilot SDK, where `send` is fire-and-forget; the wrapper is installed
314
- per-stage by the runtime (`wrapCopilotSend` in `src/sdk/runtime/executor.ts`).
315
-
316
- ```ts
317
- // Default pattern — blocks until the agent is idle, no timeout
318
- await s.session.send({ prompt });
319
- const messages = await s.session.getMessages(); // safe to read now
320
- ```
321
-
322
- **Do not use `sendAndWait` in Atomic workflows.** It keeps the SDK's native
323
- 60-second default timeout, which is almost never enough for real agent work —
324
- planners, reviewers, and orchestrators routinely exceed it, and the throw
325
- propagates out of `run()` and halts the whole workflow (see
326
- `failure-modes.md` §F10). `send` already blocks until idle with no timeout,
327
- so `sendAndWait` buys you nothing but a failure mode. If you think you need
328
- it, you almost certainly want `send`.
329
-
330
- ### Critical pitfall: session lifecycle controls what context is available
331
-
332
- A workflow is not just a sequence of agent calls — it is an **information
333
- flow problem**. The single most common failure mode in Copilot workflows is
334
- assuming context carries across session boundaries when it doesn't.
335
- Designing a workflow without thinking about information flow produces
336
- subagents that hallucinate, repeat work, or drop requirements silently.
337
-
338
- **Treat this section as load-bearing**, not decorative. If you skip it, your
339
- workflow will ship broken in subtle, non-deterministic ways.
340
-
341
- #### Session lifecycle states
342
-
343
- For normal workflow authoring, use the **3-state rubric** from SKILL.md:
344
- `Fresh` / `Continued` / `Closed`. Every new `ctx.stage()` call is fresh; if
345
- you need full history, prefer another turn inside the same stage callback.
346
-
347
- Copilot also exposes an advanced `Resumed` state at the provider level. Each
348
- state determines what context the model sees on its next turn:
349
-
350
- | State | How you get there | Context available | Action needed |
351
- | ------------------------ | ---------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------- |
352
- | **Fresh** | `client.createSession(...)` (what `ctx.stage()` does) | **None** — empty conversation | You MUST inject everything the agent needs in the first prompt |
353
- | **Continued** | Same session, additional `send` calls | All prior turns in this session | Nothing — but watch total token usage |
354
- | **Resumed** *(advanced)* | `client.resumeSession(sessionId)` | All persisted turns from the prior session of the SAME agent | Nothing — full history is reattached. Use only for same-role continuation |
355
- | **Closed** | `session.disconnect()` or `client.stop()` (auto-handled by runtime after the stage callback returns) | **Gone** from the live client; persisted on disk if the host enables it | Either resume by ID (same agent) or start fresh and re-inject context |
356
-
357
- The failure mode: you close a session, create a new one, and assume the new
358
- one "remembers" the previous conversation. It doesn't. `client` is just the
359
- transport — each session is a fully independent conversation. Resume/fork
360
- APIs are provider-specific escape hatches, not the default stage-to-stage
361
- handoff path.
362
-
363
- ```ts
364
- // Buggy — the orchestrator stage is fresh and knows NOTHING about what
365
- // the planner just produced, because each ctx.stage() starts a brand-new
366
- // conversation for Copilot.
367
- await ctx.stage({ name: "planner" }, {}, { agent: "planner" }, async (s) => {
368
- await s.session.send({ prompt: buildPlannerPrompt((s.inputs.prompt ?? "")) });
369
- s.save(await s.session.getMessages());
370
- });
371
- await ctx.stage({ name: "orchestrator" }, {}, { agent: "orchestrator" }, async (s) => {
372
- await s.session.send({ prompt: buildOrchestratorPrompt() });
373
- s.save(await s.session.getMessages());
374
- });
375
- // ↑ orchestrator only sees buildOrchestratorPrompt() — no planner output,
376
- // no original user spec, no context.
377
- ```
378
-
379
- #### Three reliable ways to avoid losing context
380
-
381
- Pick the one that fits the data you need to hand off. These are not
382
- mutually exclusive — ralph uses (1) + (2) together as belt-and-braces.
383
-
384
- **1. Explicit prompt handoff** — capture the prior session's last assistant
385
- message and inject it (or a summary) into the next session's first prompt.
386
- Simplest and most common fix. Use `ctx.stage()` — the runtime auto-creates
387
- and cleans up each session, so never call `client.createSession()` directly
388
- (see Structural Rule 7 in SKILL.md):
389
-
390
- ```ts
391
- // Correct — forward the planner's output into the orchestrator prompt
392
- const plannerHandle = await ctx.stage(
393
- { name: "planner" },
394
- {},
395
- { agent: "planner" },
396
- async (s) => {
397
- await s.session.send({ prompt: buildPlannerPrompt((s.inputs.prompt ?? "")) });
398
- const messages = await s.session.getMessages();
399
- s.save(messages);
400
- return getAssistantText(messages); // see failure-modes.md §F1 for getAssistantText
401
- },
402
- );
403
-
404
- await ctx.stage(
405
- { name: "orchestrator" },
406
- {},
407
- { agent: "orchestrator" },
408
- async (s) => {
409
- await s.session.send({
410
- prompt: buildOrchestratorPrompt(
411
- (s.inputs.prompt ?? ""),
412
- { plannerNotes: plannerHandle.result },
413
- ),
414
- });
415
- s.save(await s.session.getMessages());
416
- },
417
- );
418
- ```
419
-
420
- **2. External shared state** — write results to a medium both sessions can
421
- read: the task list (`TaskCreate` / `TaskList`), files on disk, a git
422
- working tree, or a database. The planner writes; the orchestrator reads.
423
- Ralph uses `TaskCreate`/`TaskList` as its primary coordination medium.
424
-
425
- **3. Keep the follow-up in the same stage callback** — if the next step needs
426
- the full live conversation, don't cross a stage boundary. Send another turn to
427
- the same session instead. This is the standard workflow-API way to preserve
428
- history across related steps.
429
-
430
- ```ts
431
- // Same stage, multi-turn — full history stays attached
432
- await s.session.send({ prompt: "Plan the implementation." });
433
- await s.session.send({ prompt: "Follow up on the plan above." });
434
- ```
435
-
436
- If you deliberately drop down to provider-specific resume/fork APIs, keep them
437
- within the same agent role. They are advanced escape hatches, not the normal
438
- way stages communicate.
439
-
440
- #### When context grows too large: compaction and clearing
441
-
442
- Even within a single continued session, context can grow past the window.
443
- Symptoms include lost-in-middle failures, repeated questions, and the model
444
- "forgetting" earlier decisions. When that happens, you have two levers:
445
-
446
- - **Compaction** — summarize the prior transcript into a shorter form and
447
- feed it forward (either into a new session, or by starting a follow-up
448
- session seeded with the summary). Most SDKs expose this as a built-in
449
- command (Claude Code's `/compact` slash command, or programmatic helpers
450
- in the OpenCode SDK). If the SDK you're using doesn't, roll your own with
451
- a summarization call and start a fresh session with the summary in the
452
- first prompt.
453
- - **Clearing** — drop old turns entirely when they're no longer load-bearing
454
- (e.g. one-shot tool outputs whose results were already captured to files).
455
- Claude's `/clear`, per-SDK `clearHistory`-style APIs, or simply starting a
456
- new session with only the essentials in prompt 1 all work.
457
-
458
- Neither is free: compaction loses detail, clearing loses provenance. The
459
- `context-compression` and `context-optimization` skills below cover the
460
- trade-offs in depth.
461
-
462
- #### Context engineering skills — consult these BEFORE writing code
463
-
464
- Information flow is a design problem, not an implementation detail. Before
465
- committing to a session layout, pull in the relevant skills:
466
-
467
- | When you're deciding... | Consult |
468
- | ------------------------------------------------------------------------ | ---------------------- |
469
- | What context each session actually needs (anatomy + token budget) | `context-fundamentals` |
470
- | How many sessions and how they hand off (orchestrator vs peers vs swarm) | `multi-agent-patterns` |
471
- | How to compress large planner/reviewer output before re-injecting | `context-compression` |
472
- | How to detect and prevent lost-in-middle, poisoning, and distraction | `context-degradation` |
473
- | How to use files as coordination medium across sessions | `filesystem-context` |
474
- | How to persist knowledge across whole workflow runs | `memory-systems` |
475
- | Which turns to drop, which to cache, when to compact | `context-optimization` |
476
-
477
- These aren't optional reading — they're the difference between a workflow
478
- that works on day one and a workflow that silently degrades as inputs grow.
479
- If you're about to write a multi-session workflow and you haven't consulted
480
- at least `context-fundamentals` and `multi-agent-patterns`, **stop and read
481
- them first.**
482
-
483
- ### Multi-turn conversations
484
-
485
- Send multiple prompts to the same session:
486
-
487
- ```ts
488
- .run(async (ctx) => {
489
- await ctx.stage({ name: "implement" }, {}, {}, async (s) => {
490
- // Turn 1
491
- await s.session.send({ prompt: "Plan the implementation." });
492
- // Turn 2
493
- await s.session.send({ prompt: "Now implement the plan." });
494
- // Turn 3
495
- await s.session.send({ prompt: "Run the tests." });
496
-
497
- s.save(await s.session.getMessages());
498
- });
499
- })
500
- ```
501
-
502
- ### Session configuration
503
-
504
- Pass session config options as the 3rd arg to `ctx.stage()` (`sessionOpts`). These are forwarded to `client.createSession()`:
505
-
506
- ```ts
507
- await ctx.stage(
508
- { name: "audit" },
509
- {}, // clientOpts
510
- {
511
- model: "claude-sonnet-4.6",
512
- reasoningEffort: "high",
513
- systemMessage: "You are a security auditor...",
514
- onUserInputRequest: (request) => { /* handle user input */ },
515
- hooks: {
516
- onPreToolUse: (event) => { /* before tool execution */ },
517
- onPostToolUse: (event) => { /* after tool execution */ },
518
- },
519
- }, // sessionOpts
520
- async (s) => {
521
- await s.session.send({ prompt: (s.inputs.prompt ?? "") });
522
- s.save(await s.session.getMessages());
523
- },
524
- );
525
- ```
526
-
527
- ### Custom tools
528
-
529
- ```ts
530
- import { defineTool } from "@github/copilot-sdk";
531
-
532
- const myTool = defineTool({
533
- name: "check-coverage",
534
- description: "Check test coverage",
535
- parameters: { type: "object", properties: { path: { type: "string" } } },
536
- execute: async (params) => {
537
- // Run coverage check
538
- return { content: "Coverage: 85%" };
539
- },
540
- });
541
-
542
- // Pass tools via sessionOpts (3rd arg to ctx.stage())
543
- await ctx.stage(
544
- { name: "implement" },
545
- {},
546
- { tools: [myTool] },
547
- async (s) => {
548
- await s.session.send({ prompt: (s.inputs.prompt ?? "") });
549
- s.save(await s.session.getMessages());
550
- },
551
- );
552
- ```
553
-
554
- ### Extracting response text
555
-
556
- Do **not** just grab `.at(-1).data.content` — a Copilot turn's final
557
- `assistant.message` often has empty `content` (tool-calls-only) and
558
- subagent messages can pollute the stream via `parentToolCallId`. Concatenate
559
- every top-level turn's non-empty content instead.
560
-
561
- The canonical `getAssistantText` helper lives in `failure-modes.md` §F1 —
562
- copy it into a sibling `helpers/parsers.ts` and import it. Usage:
563
-
564
- ```ts
565
- import { getAssistantText } from "../helpers/parsers.ts";
566
-
567
- const messages = await s.session.getMessages();
568
- const text = getAssistantText(messages);
569
- ```
570
-
571
- ### Streaming events
572
-
573
- ```ts
574
- // s.session is the CopilotSession — subscribe to events directly
575
- s.session.on("assistant.message_delta", (event) => {
576
- process.stdout.write(event.data.content);
577
- });
578
-
579
- s.session.on("assistant.reasoning_delta", (event) => {
580
- // Access reasoning output
581
- });
582
- ```
583
-
584
- ### Subagent delegation
585
-
586
- Pass the `agent` parameter in `sessionOpts` (3rd arg to `ctx.stage()`) to bind the session to a named subagent:
587
-
588
- ```ts
589
- .run(async (ctx) => {
590
- await ctx.stage(
591
- { name: "plan" },
592
- {},
593
- { agent: "planner" }, // sessionOpts — binds the session to the "planner" agent
594
- async (s) => {
595
- await s.session.send({ prompt: (s.inputs.prompt ?? "") });
596
- s.save(await s.session.getMessages());
597
- },
598
- );
599
- })
600
- ```
601
-
602
- ### Headless mode (background stages)
603
-
604
- Copilot headless stages let the SDK spawn its own CLI subprocess internally — no tmux pane is needed. Set `headless: true`:
605
-
606
- ```ts
607
- await ctx.stage(
608
- { name: "background-task", headless: true },
609
- {}, {},
610
- async (s) => {
611
- // s.session.send() works identically
612
- await s.session.send({ prompt: "Analyze the codebase." });
613
- s.save(await s.session.getMessages());
614
- },
615
- );
616
- ```
617
-
618
- The SDK creates a `CopilotClient` without a `cliUrl` — it spawns its own CLI process internally rather than connecting to a tmux-hosted server. The callback interface is identical.
619
-
620
- ## OpenCode SDK
621
-
622
- OpenCode uses a client-server model. The runtime auto-creates an `OpencodeClient` (as `s.client`) and an OpenCode session (as `s.session`) before invoking your callback. Use `s.client.session.prompt({ sessionID: s.session.id, ... })` to send prompts.
623
-
624
- ### Basic usage
625
-
626
- ```ts
627
- import { defineWorkflow } from "@bastani/atomic/workflows";
628
-
629
- export default defineWorkflow({
630
- name: "implement",
631
- inputs: [{ name: "prompt", type: "text", required: true, description: "task prompt" }],
632
- })
633
- .for<"opencode">()
634
- .run(async (ctx) => {
635
- await ctx.stage(
636
- { name: "implement" },
637
- {}, // clientOpts: directory, experimental_workspaceID
638
- { title: "implement" }, // sessionOpts: title, parentID, workspaceID
639
- async (s) => {
640
- // s.client — OpencodeClient (already connected)
641
- // s.session — OpenCode Session (already created, TUI selected)
642
-
643
- const result = await s.client.session.prompt({
644
- sessionID: s.session.id,
645
- parts: [{ type: "text", text: (s.inputs.prompt ?? "") }],
646
- });
647
-
648
- s.save(result.data!);
649
- },
650
- );
651
- })
652
- .compile();
653
- ```
654
-
655
- ### Critical pitfall: session lifecycle controls what context is available
656
-
657
- OpenCode sessions have **exactly the same isolation semantics as Copilot
658
- sessions**. Every call to `client.session.create(...)` returns a fresh,
659
- empty conversation. Creating a new session for the next subagent wipes
660
- everything the prior session knew — conversation history, tool-call
661
- results, intermediate reasoning — unless you forward it explicitly.
662
-
663
- The full explanation, the four lifecycle states (Fresh / Continued /
664
- Resumed / Closed), the three valid ways to carry context across a session
665
- boundary, compaction & clearing guidance, and the context engineering
666
- skill-map live in the **Copilot** section above under
667
- ["Critical pitfall: session lifecycle controls what context is
668
- available"](#critical-pitfall-session-lifecycle-controls-what-context-is-available).
669
- Every principle there applies to OpenCode without modification — just
670
- substitute the OpenCode API equivalents:
671
-
672
- | Concept | Copilot API | OpenCode API |
673
- | ---------------------------- | ------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
674
- | Fresh session (auto-created) | `s.session` (runtime creates via `createSession`) | `s.session` (runtime creates via `session.create`) |
675
- | Send a turn | `s.session.send({ prompt })` | `s.client.session.prompt({ sessionID: s.session.id, parts })` |
676
- | Close / disconnect | Auto-handled by runtime | session lifecycle managed via server; no explicit disconnect in typical flow |
677
- | Continue prior conversation | `s.client.resumeSession(sessionId)` (provider API; advanced) | Reuse the same `sessionID` with `s.client.session.prompt()` inside the same logical conversation. `ctx.stage()` itself still creates a fresh session every time |
678
- | Extract final text | `getAssistantText(messages)` (see `failure-modes.md` §F1) | `extractResponseText(result.data!.parts)` |
679
-
680
- **Multi-agent handoff example (applies the same pattern as Copilot):**
681
-
682
- ```ts
683
- // Buggy — orchestrator stage is fresh; it has no idea what the planner
684
- // produced because each ctx.stage() starts a brand-new session.
685
- await ctx.stage({ name: "planner" }, {}, { title: "planner" }, async (s) => {
686
- const result = await s.client.session.prompt({
687
- sessionID: s.session.id,
688
- parts: [{ type: "text", text: buildPlannerPrompt((s.inputs.prompt ?? "")) }],
689
- agent: "planner",
690
- });
691
- s.save(result.data!);
692
- });
693
- await ctx.stage({ name: "orchestrator" }, {}, { title: "orchestrator" }, async (s) => {
694
- await s.client.session.prompt({
695
- sessionID: s.session.id,
696
- parts: [{ type: "text", text: buildOrchestratorPrompt() }],
697
- agent: "orchestrator",
698
- });
699
- s.save(/* ... */);
700
- });
701
-
702
- // Correct — capture planner output and forward it into orchestrator prompt
703
- const plannerHandle = await ctx.stage(
704
- { name: "planner" },
705
- {},
706
- { title: "planner" },
707
- async (s) => {
708
- const result = await s.client.session.prompt({
709
- sessionID: s.session.id,
710
- parts: [{ type: "text", text: buildPlannerPrompt((s.inputs.prompt ?? "")) }],
711
- agent: "planner",
712
- });
713
- s.save(result.data!);
714
- return extractResponseText(result.data!.parts); // see failure-modes.md §F3
715
- },
716
- );
717
-
718
- await ctx.stage(
719
- { name: "orchestrator" },
720
- {},
721
- { title: "orchestrator" },
722
- async (s) => {
723
- const result = await s.client.session.prompt({
724
- sessionID: s.session.id,
725
- parts: [{
726
- type: "text",
727
- text: buildOrchestratorPrompt(
728
- (s.inputs.prompt ?? ""),
729
- { plannerNotes: plannerHandle.result },
730
- ),
731
- }],
732
- agent: "orchestrator",
733
- });
734
- s.save(result.data!);
735
- },
736
- );
737
- ```
738
-
739
- When planner output is large enough to strain the orchestrator's context
740
- window, compress before forwarding — consult `context-compression`. When a
741
- single long-running OpenCode session starts showing lost-in-middle
742
- symptoms, consult `context-optimization` for compaction/masking strategies
743
- before reaching for "just start a new session", which loses all history.
744
-
745
- **Read the Copilot section for the full write-up.** The pitfall applies
746
- identically here; the only thing that changes is the method names.
747
-
748
- ### Multi-turn conversations
749
-
750
- Send multiple prompts to the same session using `s.client.session.prompt()` with `s.session.id`:
751
-
752
- ```ts
753
- .run(async (ctx) => {
754
- await ctx.stage({ name: "multi-turn" }, {}, { title: "multi-turn" }, async (s) => {
755
- // Turn 1
756
- await s.client.session.prompt({
757
- sessionID: s.session.id,
758
- parts: [{ type: "text", text: "Plan the implementation." }],
759
- });
760
- // Turn 2
761
- await s.client.session.prompt({
762
- sessionID: s.session.id,
763
- parts: [{ type: "text", text: "Now implement the plan." }],
764
- });
765
- // Turn 3
766
- const result = await s.client.session.prompt({
767
- sessionID: s.session.id,
768
- parts: [{ type: "text", text: "Run the tests." }],
769
- });
770
-
771
- s.save(result.data!);
772
- });
773
- })
774
- ```
775
-
776
- ### Structured output
777
-
778
- ```ts
779
- // Inside a ctx.stage callback:
780
- const result = await s.client.session.prompt({
781
- sessionID: s.session.id,
782
- parts: [{ type: "text", text: "List all API endpoints as JSON" }],
783
- format: {
784
- type: "json_schema",
785
- schema: {
786
- type: "object",
787
- properties: {
788
- endpoints: {
789
- type: "array",
790
- items: { type: "object", properties: { path: { type: "string" }, method: { type: "string" } } },
791
- },
792
- },
793
- },
794
- retryCount: 3,
795
- },
796
- });
797
- ```
798
-
799
- ### Context injection (no-reply)
800
-
801
- Inject context into a session without triggering a response:
802
-
803
- ```ts
804
- // Inside a ctx.stage callback:
805
- await s.client.session.prompt({
806
- sessionID: s.session.id,
807
- parts: [{ type: "text", text: "Here is the background context..." }],
808
- noReply: true,
809
- });
810
- // Now send the actual prompt
811
- const result = await s.client.session.prompt({
812
- sessionID: s.session.id,
813
- parts: [{ type: "text", text: "Based on the context, implement..." }],
814
- });
815
- ```
816
-
817
- ### Extracting response text
818
-
819
- Non-text parts (`tool`, `file`, `reasoning`, …) coexist with `text` parts in
820
- `result.data!.parts`; naive `.map(p => p.text)` emits `undefined` for them.
821
- The canonical `extractResponseText` helper lives in `failure-modes.md` §F3 —
822
- copy it into a sibling `helpers/parsers.ts` and import it. Usage:
823
-
824
- ```ts
825
- import { extractResponseText } from "../helpers/parsers.ts";
826
-
827
- const result = await s.client.session.prompt({
828
- sessionID: s.session.id,
829
- parts: [{ type: "text", text: (s.inputs.prompt ?? "") }],
830
- });
831
- const text = extractResponseText(result.data!.parts);
832
- ```
833
-
834
- ### Event streaming
835
-
836
- ```ts
837
- // Inside a ctx.stage callback:
838
- const unsubscribe = await s.client.event.subscribe((event) => {
839
- if (event.type === "session.updated") {
840
- console.log("Session updated:", event.data);
841
- }
842
- });
843
- ```
844
-
845
- ### Subagent delegation
846
-
847
- Pass the `agent` parameter to `s.client.session.prompt()` to route a prompt to a named subagent:
848
-
849
- ```ts
850
- .run(async (ctx) => {
851
- await ctx.stage(
852
- { name: "plan" },
853
- {},
854
- { title: "plan" },
855
- async (s) => {
856
- // Route the prompt to the "planner" agent
857
- const result = await s.client.session.prompt({
858
- sessionID: s.session.id,
859
- parts: [{ type: "text", text: (s.inputs.prompt ?? "") }],
860
- agent: "planner",
861
- });
862
-
863
- s.save(result.data!);
864
- },
865
- );
866
- })
867
- ```
868
-
869
- ### Headless mode (background stages)
870
-
871
- OpenCode headless stages use `createOpencode()` from the SDK to start both server and client in-process. Set `headless: true`:
872
-
873
- ```ts
874
- await ctx.stage(
875
- { name: "background-task", headless: true },
876
- {}, { title: "background-task" },
877
- async (s) => {
878
- // s.client.session.prompt() works identically
879
- const result = await s.client.session.prompt({
880
- sessionID: s.session.id,
881
- parts: [{ type: "text", text: "Analyze the codebase." }],
882
- });
883
- s.save(result.data!);
884
- },
885
- );
886
- ```
887
-
888
- Internally, the runtime uses `createOpencode({ port: 0 })` to start both the OpenCode server and client in-process. A cleanup callback closes the server when the stage completes. The callback interface is identical.