workflow 5.0.0-beta.2 → 5.0.0-beta.21

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 (170) hide show
  1. package/dist/api-workflow.d.ts +1 -1
  2. package/dist/api-workflow.d.ts.map +1 -1
  3. package/dist/api-workflow.js +2 -2
  4. package/dist/api.d.ts +5 -1
  5. package/dist/api.d.ts.map +1 -1
  6. package/dist/api.js +14 -2
  7. package/dist/internal/builtins.d.ts +17 -0
  8. package/dist/internal/builtins.d.ts.map +1 -1
  9. package/dist/internal/builtins.js +65 -1
  10. package/dist/observability.d.ts +1 -1
  11. package/dist/observability.js +2 -2
  12. package/dist/runtime.d.ts +1 -1
  13. package/dist/runtime.d.ts.map +1 -1
  14. package/dist/runtime.js +2 -2
  15. package/docs/ai/index.mdx +27 -23
  16. package/docs/api-reference/index.mdx +24 -0
  17. package/docs/api-reference/meta.json +8 -0
  18. package/docs/api-reference/vitest/index.mdx +28 -7
  19. package/docs/api-reference/workflow/create-hook.mdx +38 -0
  20. package/docs/api-reference/workflow/create-webhook.mdx +1 -0
  21. package/docs/api-reference/workflow/experimental-set-attributes.mdx +65 -0
  22. package/docs/api-reference/workflow/fetch.mdx +5 -0
  23. package/docs/api-reference/workflow/index.mdx +3 -0
  24. package/docs/api-reference/workflow-ai/durable-agent.mdx +7 -45
  25. package/docs/api-reference/workflow-api/get-hook-by-token.mdx +7 -0
  26. package/docs/api-reference/workflow-api/get-run.mdx +6 -0
  27. package/docs/api-reference/workflow-api/index.mdx +6 -8
  28. package/docs/api-reference/workflow-api/resume-hook.mdx +57 -0
  29. package/docs/api-reference/workflow-api/start.mdx +13 -5
  30. package/docs/api-reference/workflow-astro/index.mdx +18 -0
  31. package/docs/api-reference/workflow-astro/meta.json +4 -0
  32. package/docs/api-reference/workflow-astro/workflow.mdx +45 -0
  33. package/docs/api-reference/workflow-errors/hook-conflict-error.mdx +60 -0
  34. package/docs/api-reference/workflow-errors/index.mdx +85 -0
  35. package/docs/api-reference/workflow-errors/meta.json +5 -0
  36. package/docs/api-reference/workflow-errors/run-not-supported-error.mdx +58 -0
  37. package/docs/api-reference/workflow-errors/workflow-error.mdx +52 -0
  38. package/docs/api-reference/workflow-errors/workflow-run-failed-error.mdx +16 -6
  39. package/docs/api-reference/workflow-errors/workflow-run-not-completed-error.mdx +58 -0
  40. package/docs/api-reference/workflow-errors/workflow-runtime-error.mdx +58 -0
  41. package/docs/api-reference/workflow-nest/configure-workflow-controller.mdx +33 -0
  42. package/docs/api-reference/workflow-nest/index.mdx +31 -0
  43. package/docs/api-reference/workflow-nest/meta.json +9 -0
  44. package/docs/api-reference/workflow-nest/nest-local-builder.mdx +64 -0
  45. package/docs/api-reference/workflow-nest/workflow-controller.mdx +40 -0
  46. package/docs/api-reference/workflow-nest/workflow-module.mdx +74 -0
  47. package/docs/api-reference/workflow-next/with-workflow.mdx +56 -2
  48. package/docs/api-reference/workflow-nitro/index.mdx +59 -0
  49. package/docs/api-reference/workflow-nuxt/index.mdx +47 -0
  50. package/docs/api-reference/workflow-observability/hydrate-data.mdx +35 -0
  51. package/docs/api-reference/workflow-observability/hydrate-resource-io.mdx +62 -0
  52. package/docs/api-reference/workflow-observability/index.mdx +64 -0
  53. package/docs/api-reference/workflow-observability/meta.json +11 -0
  54. package/docs/api-reference/workflow-observability/observability-revivers.mdx +50 -0
  55. package/docs/api-reference/workflow-observability/parse-class-name.mdx +41 -0
  56. package/docs/api-reference/workflow-observability/parse-step-name.mdx +40 -0
  57. package/docs/api-reference/workflow-observability/parse-workflow-name.mdx +55 -0
  58. package/docs/api-reference/workflow-runtime/create-world.mdx +39 -0
  59. package/docs/api-reference/workflow-runtime/get-world-handlers.mdx +44 -0
  60. package/docs/api-reference/{workflow-api → workflow-runtime}/get-world.mdx +7 -10
  61. package/docs/api-reference/workflow-runtime/health-check.mdx +50 -0
  62. package/docs/api-reference/workflow-runtime/index.mdx +43 -0
  63. package/docs/api-reference/workflow-runtime/meta.json +12 -0
  64. package/docs/api-reference/workflow-runtime/set-world.mdx +49 -0
  65. package/docs/api-reference/workflow-runtime/workflow-entrypoint.mdx +42 -0
  66. package/docs/api-reference/{workflow-api → workflow-runtime}/world/index.mdx +5 -8
  67. package/docs/api-reference/workflow-runtime/world/meta.json +4 -0
  68. package/docs/api-reference/{workflow-api → workflow-runtime}/world/queue.mdx +2 -2
  69. package/docs/api-reference/{workflow-api → workflow-runtime}/world/storage.mdx +11 -4
  70. package/docs/api-reference/{workflow-api → workflow-runtime}/world/streams.mdx +2 -2
  71. package/docs/api-reference/workflow-serde/index.mdx +0 -1
  72. package/docs/api-reference/workflow-serde/workflow-deserialize.mdx +1 -2
  73. package/docs/api-reference/workflow-serde/workflow-serialize.mdx +1 -2
  74. package/docs/api-reference/workflow-sveltekit/index.mdx +18 -0
  75. package/docs/api-reference/workflow-sveltekit/meta.json +4 -0
  76. package/docs/api-reference/workflow-sveltekit/workflow-plugin.mdx +42 -0
  77. package/docs/api-reference/workflow-vite/index.mdx +18 -0
  78. package/docs/api-reference/workflow-vite/meta.json +4 -0
  79. package/docs/api-reference/workflow-vite/workflow.mdx +48 -0
  80. package/docs/changelog/attributes-mvp.mdx +380 -0
  81. package/docs/changelog/eager-processing.mdx +269 -0
  82. package/docs/changelog/index.mdx +2 -1
  83. package/docs/changelog/lazy-event-creation.md +127 -0
  84. package/docs/changelog/meta.json +7 -1
  85. package/docs/changelog/resilient-start.mdx +31 -283
  86. package/docs/changelog/turbo-mode.md +87 -0
  87. package/docs/cookbook/advanced/child-workflows.mdx +315 -0
  88. package/docs/cookbook/advanced/meta.json +2 -3
  89. package/docs/cookbook/advanced/publishing-libraries.mdx +87 -29
  90. package/docs/cookbook/advanced/serializable-steps.mdx +17 -5
  91. package/docs/cookbook/advanced/upgrading-workflows.mdx +195 -0
  92. package/docs/cookbook/agent-patterns/agent-cancellation.mdx +156 -0
  93. package/docs/cookbook/agent-patterns/durable-agent.mdx +11 -184
  94. package/docs/cookbook/agent-patterns/human-in-the-loop.mdx +150 -173
  95. package/docs/cookbook/agent-patterns/meta.json +1 -7
  96. package/docs/cookbook/common-patterns/batching.mdx +44 -118
  97. package/docs/cookbook/common-patterns/idempotency.mdx +36 -52
  98. package/docs/cookbook/common-patterns/meta.json +4 -4
  99. package/docs/cookbook/common-patterns/rate-limiting.mdx +1 -1
  100. package/docs/cookbook/common-patterns/saga.mdx +128 -33
  101. package/docs/cookbook/common-patterns/scheduling.mdx +77 -193
  102. package/docs/cookbook/common-patterns/sequential-and-parallel.mdx +155 -0
  103. package/docs/cookbook/common-patterns/timeouts.mdx +100 -0
  104. package/docs/cookbook/common-patterns/workflow-composition.mdx +117 -0
  105. package/docs/cookbook/index.mdx +14 -17
  106. package/docs/cookbook/integrations/ai-sdk.mdx +330 -142
  107. package/docs/cookbook/integrations/chat-sdk.mdx +264 -151
  108. package/docs/cookbook/integrations/sandbox.mdx +482 -81
  109. package/docs/cookbook/meta.json +1 -1
  110. package/docs/deploying/building-a-world.mdx +1 -1
  111. package/docs/deploying/world/postgres-world.mdx +5 -3
  112. package/docs/deploying/world/vercel-world.mdx +2 -0
  113. package/docs/errors/abort-signal-timeout-in-workflow.mdx +80 -0
  114. package/docs/errors/corrupted-event-log.mdx +5 -5
  115. package/docs/errors/hook-conflict.mdx +56 -4
  116. package/docs/errors/index.mdx +9 -0
  117. package/docs/errors/replay-divergence.mdx +27 -0
  118. package/docs/errors/runtime-decryption-failed.mdx +77 -0
  119. package/docs/errors/step-executed-multiple-times.mdx +23 -0
  120. package/docs/errors/step-not-registered.mdx +1 -1
  121. package/docs/foundations/cancellation.mdx +459 -0
  122. package/docs/foundations/errors-and-retries.mdx +7 -3
  123. package/docs/foundations/hooks.mdx +29 -0
  124. package/docs/foundations/idempotency.mdx +236 -11
  125. package/docs/foundations/index.mdx +3 -3
  126. package/docs/foundations/meta.json +3 -2
  127. package/docs/foundations/serialization.mdx +78 -42
  128. package/docs/foundations/starting-workflows.mdx +6 -2
  129. package/docs/foundations/streaming.mdx +14 -23
  130. package/docs/foundations/versioning.mdx +263 -0
  131. package/docs/getting-started/astro.mdx +6 -0
  132. package/docs/getting-started/index.mdx +6 -7
  133. package/docs/getting-started/meta.json +1 -0
  134. package/docs/getting-started/nestjs.mdx +9 -0
  135. package/docs/getting-started/next.mdx +5 -3
  136. package/docs/getting-started/nitro.mdx +22 -0
  137. package/docs/getting-started/sveltekit.mdx +6 -0
  138. package/docs/getting-started/tanstack-start.mdx +241 -0
  139. package/docs/how-it-works/cancellation.mdx +287 -0
  140. package/docs/how-it-works/code-transform.mdx +2 -2
  141. package/docs/how-it-works/encryption.mdx +2 -2
  142. package/docs/how-it-works/event-sourcing.mdx +2 -2
  143. package/docs/how-it-works/meta.json +2 -1
  144. package/docs/internal/index.mdx +21 -0
  145. package/docs/internal/meta.json +10 -0
  146. package/docs/internal/nitro-native-build.mdx +38 -0
  147. package/docs/internal/nitro-web-ui.mdx +24 -0
  148. package/docs/internal/serializable-abort-controller.mdx +148 -0
  149. package/docs/migration-guides/migrating-from-aws-step-functions.mdx +63 -16
  150. package/docs/migration-guides/migrating-from-inngest.mdx +44 -22
  151. package/docs/migration-guides/migrating-from-temporal.mdx +43 -14
  152. package/docs/migration-guides/migrating-from-trigger-dev.mdx +59 -27
  153. package/docs/observability/attributes.mdx +87 -0
  154. package/docs/observability/index.mdx +25 -1
  155. package/docs/observability/meta.json +1 -1
  156. package/docs/observability/tracing.mdx +106 -0
  157. package/docs/testing/index.mdx +2 -2
  158. package/package.json +14 -13
  159. package/docs/api-reference/workflow-api/world/meta.json +0 -4
  160. package/docs/api-reference/workflow-api/world/observability.mdx +0 -164
  161. package/docs/cookbook/advanced/custom-serialization.mdx +0 -168
  162. package/docs/cookbook/advanced/durable-objects.mdx +0 -148
  163. package/docs/cookbook/advanced/isomorphic-packages.mdx +0 -145
  164. package/docs/cookbook/agent-patterns/stop-workflow.mdx +0 -216
  165. package/docs/cookbook/agent-patterns/tool-orchestration.mdx +0 -255
  166. package/docs/cookbook/agent-patterns/tool-streaming.mdx +0 -181
  167. package/docs/cookbook/common-patterns/child-workflows.mdx +0 -372
  168. package/docs/cookbook/common-patterns/content-router.mdx +0 -207
  169. package/docs/cookbook/common-patterns/fan-out.mdx +0 -208
  170. package/docs/foundations/common-patterns.mdx +0 -265
@@ -1,128 +1,529 @@
1
1
  ---
2
2
  title: Sandbox
3
- description: Orchestrate Vercel Sandbox lifecycle -- creation, code execution, snapshotting -- inside durable workflows.
3
+ description: Model one Vercel Sandbox per workflow run durable, idle-efficient, and not bound by the 5-hour sandbox hard cap.
4
4
  type: guide
5
- summary: Use workflow steps to provision sandboxes, run code, and manage sandbox lifecycle with automatic cleanup on failure.
5
+ summary: Own a sandbox for the lifetime of a workflow run. Hibernate on idle via snapshot(), proactively refresh before the sandbox hard cap, and reconnect by runId so one logical session can run effectively forever.
6
6
  related:
7
7
  - /docs/ai/defining-tools
8
8
  - /docs/foundations/errors-and-retries
9
- - /docs/api-reference/workflow-ai/durable-agent
9
+ - /docs/cookbook/common-patterns/scheduling
10
+ - /docs/cookbook/agent-patterns/durable-agent
10
11
  ---
11
12
 
12
- [Vercel Sandbox](https://vercel.com/docs/sandbox) provides isolated code execution environments. The `@vercel/sandbox` package implements first-class support for the Workflow SDK -- the `Sandbox` class is serializable, and its methods (`create`, `runCommand`, `destroy`, etc.) implicitly run as steps. This means you can interact with sandboxes directly inside workflow functions without wrapping each operation in a separate `"use step"` function.
13
+ [Vercel Sandbox](https://vercel.com/docs/sandbox) provides isolated code execution environments. The `@vercel/sandbox` package has first-class support for the Workflow SDK the `Sandbox` class is serializable, and its methods (`create`, `runCommand`, `stop`, `snapshot`) implicitly run as steps. You can use `Sandbox` directly inside a workflow function without wrapping each call in a separate `"use step"` function.
13
14
 
14
- ## What It Enables
15
+ ## Why Workflow + Sandbox
15
16
 
16
- - **Durable sandbox sessions** -- Sandbox provisioning and teardown survive cold starts
17
- - **Automatic cleanup** -- Saga-style compensation ensures sandboxes are destroyed on failure
18
- - **Multi-step code execution** -- Run a sequence of commands in the same sandbox with each step logged
19
- - **Agent-driven sandboxes** -- Give your DurableAgent a tool that spins up sandboxes on demand
17
+ A sandbox alone gets you an isolated VM. A workflow around it gets you a **durable controller** for that VM's entire lifetime:
20
18
 
21
- ## When to Use
19
+ - **One workflow run = one sandbox session.** The `runId` is the only state you need to persist on the client. Close the tab, come back a week later, POST the same `runId` and you're back in the same session.
20
+ - **Efficient resource use.** Active sandboxes cost money; hibernated workflows cost nothing. The workflow races a command hook against a `sleep()` timer — when idle, it calls `sandbox.snapshot()` (which also stops the VM) and waits indefinitely. Next command → spin a new sandbox from the snapshot with filesystem, installed packages, and git history intact.
21
+ - **Beyond the 5-hour hard cap.** Every Vercel Sandbox has a maximum lifetime. The workflow tracks that deadline and proactively snapshots + recreates *before* the cap, so the logical session outlives any one VM. Effectively unbounded session duration on top of time-bounded infrastructure.
22
+ - **Automatic cleanup.** `try/finally` in the workflow guarantees the VM is stopped on failure or destroy.
22
23
 
23
- Use this integration when your workflow needs to:
24
+ <Callout type="info">
25
+ An effectively unbounded sandbox session is still one workflow run, so it stays on the deployment that started it. If the controller or agent code should upgrade over time, use an explicit version boundary and pass the serialized state or stream handles forward. See [Versioning](/docs/foundations/versioning).
26
+ </Callout>
24
27
 
25
- - Execute user-provided or AI-generated code safely
26
- - Run multi-step build/test pipelines in isolated environments
27
- - Provision temporary environments for interactive sessions
28
- - Snapshot sandbox state between steps for reproducibility
28
+ ## Use Case: Coding Agents
29
29
 
30
- ## Sandbox Lifecycle in a Workflow
30
+ This is the pattern [Open Agents](https://open-agents.dev/) uses to spawn coding agents that run "infinitely in the cloud." Each agent session gets its own sandbox — full filesystem, network, and runtime access — and the durable workflow keeps the agent loop resumable across restarts, auto-hibernates when the user walks away, and reconnects instantly when they return.
31
31
 
32
- Because `@vercel/sandbox` methods are implicit steps, each call is automatically persisted to the event log. If a failure occurs partway through, the workflow replays from where it left off.
32
+ Most coding-agent workloads look like this:
33
+
34
+ - User sends a task → agent plans, reads files, runs shell commands, commits.
35
+ - User walks away mid-run → agent keeps going, eventually goes idle waiting for input.
36
+ - User comes back days later → same branch, same filesystem, same conversation history.
37
+
38
+ Without durable workflows you'd need a separate state store for the agent loop, a separate job queue for retries, a separate scheduler for idle cleanup, and bespoke reconnection logic. With the pattern below, all of it is one file.
39
+
40
+ ## Quickstart: One-shot Pipeline
41
+
42
+ Before the full session pattern, the simplest shape. Each sandbox method is an implicit step, so the event log records every command and the workflow replays from the last completed call on restart.
33
43
 
34
44
  ```typescript title="workflows/sandbox-pipeline.ts" lineNumbers
35
45
  import { Sandbox } from "@vercel/sandbox";
36
46
 
37
- export async function sandboxPipeline(input: {
38
- template: string;
39
- commands: string[];
40
- }) {
47
+ export async function sandboxPipeline(input: { commands: string[] }) {
41
48
  "use workflow";
42
49
 
43
- const sandbox = await Sandbox.create({ template: input.template }); // [!code highlight]
50
+ const sandbox = await Sandbox.create({ runtime: "node22" }); // [!code highlight]
44
51
 
45
52
  try {
46
53
  const results = [];
47
54
  for (const command of input.commands) {
48
- const result = await sandbox.runCommand(command); // [!code highlight]
49
- results.push(result);
55
+ const result = await sandbox.runCommand({ // [!code highlight]
56
+ cmd: "bash",
57
+ args: ["-c", command],
58
+ });
59
+ results.push({
60
+ command,
61
+ exitCode: result.exitCode,
62
+ stdout: await result.stdout(),
63
+ stderr: await result.stderr(),
64
+ });
50
65
  }
51
66
  return { status: "completed", results };
52
- } catch (error) {
53
- await sandbox.destroy(); // [!code highlight]
54
- throw error;
67
+ } finally {
68
+ await sandbox.stop(); // [!code highlight]
55
69
  }
56
70
  }
57
71
  ```
58
72
 
59
- ## Sandbox as an Agent Tool
73
+ ## Session Pattern: Persistent Sandbox Beyond the Hard Cap
60
74
 
61
- Give a DurableAgent the ability to create and use sandboxes. The agent decides when to spin up a sandbox, what code to run, and when to tear it down. Since sandbox methods are implicit steps, the tool execute functions can call them directly.
75
+ One workflow run owns a sandbox for its whole lifetime. The workflow's loop does two jobs simultaneously:
62
76
 
63
- ```typescript title="workflows/code-agent.ts" lineNumbers
64
- import { Sandbox } from "@vercel/sandbox";
65
- import { DurableAgent } from "@workflow/ai/agent";
66
- import { convertToModelMessages, type UIMessage, type UIMessageChunk } from "ai";
67
- import { getWritable } from "workflow";
68
- import z from "zod/v4";
77
+ 1. **Command pipeline** — await a hook, run the next user command, stream output, loop.
78
+ 2. **Sandbox lifecycle** race the hook against a `sleep()` timer armed for whichever comes first: the idle deadline or the sandbox's refresh deadline (a safety margin before its hard cap).
79
+
80
+ When the timer wins:
81
+
82
+ - **Idle** `sandbox.snapshot()` and wait indefinitely for the next command. No compute while asleep.
83
+ - **Near sandbox hard cap** → `sandbox.snapshot()` and immediately create a new sandbox from the snapshot. The session appears continuous; the underlying VM just rotated.
84
+
85
+ The only way out is an explicit `/destroy` command.
86
+
87
+ <Tabs items={['Workflow', 'API Routes', 'Client']}>
88
+
89
+ <Tab value="Workflow">
90
+
91
+ ```typescript title="workflows/sandbox-session.ts" lineNumbers
92
+ import { defineHook, sleep, getWritable, getWorkflowMetadata } from "workflow";
93
+ import { Sandbox, type Snapshot } from "@vercel/sandbox";
94
+ import { z } from "zod";
95
+
96
+ export const commandHook = defineHook({ // [!code highlight]
97
+ schema: z.object({ command: z.string() }),
98
+ });
99
+
100
+ const RUNTIME = "node22";
101
+ const HIBERNATE_AFTER_MS = 30 * 60_000; // 30 min idle → hibernate
102
+ const SANDBOX_TIMEOUT_MS = 5 * 60 * 60_000; // sandbox hard cap (5h)
103
+ const REFRESH_SAFETY_MS = 5 * 60_000; // refresh 5 min before the cap
69
104
 
70
- export async function codeAgent(messages: UIMessage[]) {
105
+ export type SandboxEvent =
106
+ | {
107
+ type: "created";
108
+ sandboxId: string;
109
+ runtime: string;
110
+ startedAt: number;
111
+ sandboxExpiresAt: number;
112
+ hibernateAfterMs: number;
113
+ }
114
+ | {
115
+ type: "status";
116
+ state:
117
+ | "active"
118
+ | "hibernating"
119
+ | "hibernated"
120
+ | "resuming"
121
+ | "refreshing"
122
+ | "destroyed";
123
+ at: number;
124
+ sandboxId?: string;
125
+ sandboxExpiresAt?: number;
126
+ snapshotId?: string;
127
+ }
128
+ | { type: "activity"; at: number }
129
+ | { type: "command_start"; id: string; command: string; at: number }
130
+ | { type: "command_output"; id: string; stream: "stdout" | "stderr"; data: string }
131
+ | { type: "command_end"; id: string; exitCode: number | null; durationMs: number }
132
+ | { type: "result"; status: "destroyed"; durationMs: number };
133
+
134
+ async function emit(event: SandboxEvent) {
135
+ "use step";
136
+ const writer = getWritable<SandboxEvent>().getWriter();
137
+ try {
138
+ await writer.write(event);
139
+ } finally {
140
+ writer.releaseLock();
141
+ }
142
+ }
143
+
144
+ async function runCommandAndStream(sandbox: Sandbox, id: string, command: string) {
145
+ "use step";
146
+ const writer = getWritable<SandboxEvent>().getWriter();
147
+ const startedAt = Date.now();
148
+ try {
149
+ await writer.write({ type: "command_start", id, command, at: startedAt });
150
+ const result = await sandbox.runCommand({ cmd: "bash", args: ["-c", command] });
151
+ const stdout = await result.stdout();
152
+ if (stdout) await writer.write({ type: "command_output", id, stream: "stdout", data: stdout });
153
+ const stderr = await result.stderr();
154
+ if (stderr) await writer.write({ type: "command_output", id, stream: "stderr", data: stderr });
155
+ await writer.write({
156
+ type: "command_end", id,
157
+ exitCode: result.exitCode,
158
+ durationMs: Date.now() - startedAt,
159
+ });
160
+ } finally {
161
+ writer.releaseLock();
162
+ }
163
+ }
164
+
165
+ export async function sandboxSessionWorkflow() {
71
166
  "use workflow";
72
167
 
73
- let activeSandbox: Sandbox | null = null;
74
-
75
- const agent = new DurableAgent({
76
- model: "anthropic/claude-sonnet-4-20250514",
77
- instructions:
78
- "You are a coding assistant. You can create sandboxes to run code. " +
79
- "Always create a sandbox first, then execute code in it. " +
80
- "Clean up the sandbox when you are done.",
81
- tools: {
82
- createSandbox: {
83
- description: "Create an isolated sandbox environment for running code",
84
- inputSchema: z.object({
85
- template: z.string().describe("The sandbox template (e.g., 'node', 'python')"),
86
- }),
87
- execute: async ({ template }) => {
88
- activeSandbox = await Sandbox.create({ template }); // [!code highlight]
89
- return { sandboxId: activeSandbox.id };
90
- },
91
- },
92
- executeCode: {
93
- description: "Execute a command in the active sandbox",
94
- inputSchema: z.object({
95
- command: z.string().describe("The command to execute"),
96
- }),
97
- execute: async ({ command }) => {
98
- if (!activeSandbox) throw new Error("No active sandbox");
99
- return activeSandbox.runCommand(command); // [!code highlight]
100
- },
101
- },
102
- cleanupSandbox: {
103
- description: "Destroy the active sandbox when finished",
104
- inputSchema: z.object({}),
105
- execute: async () => {
106
- if (!activeSandbox) throw new Error("No active sandbox");
107
- await activeSandbox.destroy(); // [!code highlight]
108
- activeSandbox = null;
109
- return { cleaned: true };
168
+ const { workflowRunId } = getWorkflowMetadata();
169
+ // Create the hook once, outside the loop — reusing the same token from inside // [!code highlight]
170
+ // the loop would throw HookConflictError. // [!code highlight]
171
+ const hook = commandHook.create({ token: workflowRunId });
172
+
173
+ const startedAt = Date.now();
174
+
175
+ let sandbox: Sandbox = await Sandbox.create({
176
+ runtime: RUNTIME,
177
+ timeout: SANDBOX_TIMEOUT_MS,
178
+ });
179
+ let sandboxCreatedAt = Date.now();
180
+ let sandboxExpiresAt = sandboxCreatedAt + SANDBOX_TIMEOUT_MS;
181
+
182
+ await emit({
183
+ type: "created",
184
+ sandboxId: sandbox.sandboxId,
185
+ runtime: RUNTIME,
186
+ startedAt,
187
+ sandboxExpiresAt,
188
+ hibernateAfterMs: HIBERNATE_AFTER_MS,
189
+ });
190
+ await emit({
191
+ type: "status", state: "active", at: Date.now(),
192
+ sandboxId: sandbox.sandboxId, sandboxExpiresAt,
193
+ });
194
+
195
+ let snapshot: Snapshot | null = null;
196
+ let hibernated = false;
197
+ let lastActivityAt = startedAt;
198
+ let counter = 0;
199
+ let destroyed = false;
200
+
201
+ try {
202
+ while (!destroyed) {
203
+ if (hibernated && snapshot) {
204
+ // While hibernated, the VM is already stopped. Just wait for the next
205
+ // command — no idle timer, no compute cost.
206
+ const payload = await hook;
207
+ if (payload.command === "/destroy") { destroyed = true; break; }
208
+
209
+ await emit({ type: "status", state: "resuming", at: Date.now() });
210
+ sandbox = await Sandbox.create({ // [!code highlight]
211
+ source: { type: "snapshot", snapshotId: snapshot.snapshotId }, // [!code highlight]
212
+ timeout: SANDBOX_TIMEOUT_MS, // [!code highlight]
213
+ });
214
+ sandboxCreatedAt = Date.now();
215
+ sandboxExpiresAt = sandboxCreatedAt + SANDBOX_TIMEOUT_MS;
216
+ hibernated = false;
217
+ snapshot = null;
218
+ await emit({
219
+ type: "status", state: "active", at: Date.now(),
220
+ sandboxId: sandbox.sandboxId, sandboxExpiresAt,
221
+ });
222
+
223
+ counter += 1;
224
+ await runCommandAndStream(sandbox, `cmd-${counter}`, payload.command);
225
+ lastActivityAt = Date.now();
226
+ await emit({ type: "activity", at: lastActivityAt });
227
+ continue;
228
+ }
229
+
230
+ // Active — wake at whichever comes first: idle-deadline or refresh-deadline.
231
+ const idleDeadline = lastActivityAt + HIBERNATE_AFTER_MS;
232
+ const refreshDeadline = sandboxExpiresAt - REFRESH_SAFETY_MS;
233
+ const wakeAt = Math.min(idleDeadline, refreshDeadline);
234
+ const sleepMs = Math.max(0, wakeAt - Date.now());
235
+
236
+ const outcome = await Promise.race([ // [!code highlight]
237
+ hook.then((p) => ({ type: "command" as const, command: p.command })),
238
+ sleep(`${sleepMs}ms`).then(() => ({ type: "timer" as const })),
239
+ ]);
240
+
241
+ if (outcome.type === "timer") {
242
+ const nearExpiry = Date.now() >= refreshDeadline;
243
+
244
+ if (nearExpiry) {
245
+ // Proactive refresh — snapshot and immediately recreate so the
246
+ // session outlives the sandbox hard cap.
247
+ await emit({ type: "status", state: "refreshing", at: Date.now() });
248
+ const snap = await sandbox.snapshot(); // [!code highlight]
249
+ sandbox = await Sandbox.create({ // [!code highlight]
250
+ source: { type: "snapshot", snapshotId: snap.snapshotId }, // [!code highlight]
251
+ timeout: SANDBOX_TIMEOUT_MS, // [!code highlight]
252
+ });
253
+ sandboxCreatedAt = Date.now();
254
+ sandboxExpiresAt = sandboxCreatedAt + SANDBOX_TIMEOUT_MS;
255
+ await emit({
256
+ type: "status", state: "active", at: Date.now(),
257
+ sandboxId: sandbox.sandboxId, sandboxExpiresAt,
258
+ snapshotId: snap.snapshotId,
259
+ });
260
+ lastActivityAt = Date.now();
261
+ } else {
262
+ // Idle — snapshot and hibernate indefinitely.
263
+ await emit({ type: "status", state: "hibernating", at: Date.now() });
264
+ snapshot = await sandbox.snapshot(); // [!code highlight]
265
+ hibernated = true;
266
+ await emit({
267
+ type: "status", state: "hibernated", at: Date.now(),
268
+ snapshotId: snapshot.snapshotId,
269
+ });
270
+ }
271
+ continue;
272
+ }
273
+
274
+ if (outcome.command === "/destroy") { destroyed = true; break; }
275
+
276
+ counter += 1;
277
+ await runCommandAndStream(sandbox, `cmd-${counter}`, outcome.command);
278
+ lastActivityAt = Date.now();
279
+ await emit({ type: "activity", at: lastActivityAt });
280
+ }
281
+ } finally {
282
+ if (!hibernated) {
283
+ try {
284
+ if (sandbox.status === "running") await sandbox.stop();
285
+ } catch { /* best-effort */ }
286
+ }
287
+ await emit({ type: "status", state: "destroyed", at: Date.now() });
288
+ await emit({
289
+ type: "result",
290
+ status: "destroyed",
291
+ durationMs: Date.now() - startedAt,
292
+ });
293
+ }
294
+ }
295
+ ```
296
+
297
+ </Tab>
298
+
299
+ <Tab value="API Routes">
300
+
301
+ Two endpoints. `/start` accepts an optional `{ runId }` — if the run still exists, it replays the event log from index 0 so a returning client fully rehydrates. `/command` resumes the hook and returns immediately; command output lands on the `/start` stream.
302
+
303
+ <Callout type="info">
304
+ This example starts a fresh sandbox session when no `runId` is provided. If your product needs one sandbox session per user, project, or task, use a deterministic hook token derived from that session key and route retries through the active hook. See [Run idempotency](/docs/foundations/idempotency#run-idempotency).
305
+ </Callout>
306
+
307
+ ```typescript title="app/api/sandbox/start/route.ts" lineNumbers
308
+ import { start, getRun } from "workflow/api";
309
+ import { sandboxSessionWorkflow } from "@/workflows/sandbox-session";
310
+
311
+ export async function POST(req: Request) {
312
+ let body: { runId?: string } = {};
313
+ try {
314
+ const text = await req.text();
315
+ if (text) body = JSON.parse(text);
316
+ } catch { /* ignore malformed body */ }
317
+
318
+ // Reconnect path: if the client sends a known runId, stream the durable
319
+ // event log from the beginning so the UI can rehydrate.
320
+ if (body.runId) {
321
+ const run = getRun(body.runId);
322
+ if (await run.exists) { // [!code highlight]
323
+ const readable = run.getReadable({ startIndex: 0 }); // [!code highlight]
324
+ return new Response(readable.pipeThrough(ndjson()), {
325
+ headers: {
326
+ "Content-Type": "application/x-ndjson",
327
+ "x-workflow-run-id": body.runId,
328
+ "x-workflow-reconnected": "true",
329
+ "Cache-Control": "no-cache, no-transform",
110
330
  },
111
- },
331
+ });
332
+ }
333
+ // Stale runId — fall through to start fresh.
334
+ }
335
+
336
+ const run = await start(sandboxSessionWorkflow, []);
337
+ return new Response(run.readable.pipeThrough(ndjson()), {
338
+ headers: {
339
+ "Content-Type": "application/x-ndjson",
340
+ "x-workflow-run-id": run.runId,
341
+ "Cache-Control": "no-cache, no-transform",
112
342
  },
113
343
  });
344
+ }
114
345
 
115
- const result = await agent.stream({
116
- messages: await convertToModelMessages(messages),
117
- writable: getWritable<UIMessageChunk>(),
346
+ function ndjson<T>() {
347
+ return new TransformStream<T, string>({
348
+ transform(chunk, controller) {
349
+ controller.enqueue(JSON.stringify(chunk) + "\n");
350
+ },
118
351
  });
352
+ }
353
+ ```
354
+
355
+ ```typescript title="app/api/sandbox/command/route.ts" lineNumbers
356
+ import { commandHook } from "@/workflows/sandbox-session";
119
357
 
120
- return { messages: result.messages };
358
+ export async function POST(req: Request) {
359
+ const { runId, command } = (await req.json()) as { runId?: string; command?: string };
360
+
361
+ if (!runId || typeof command !== "string") {
362
+ return Response.json({ error: "runId and command are required" }, { status: 400 });
363
+ }
364
+
365
+ try {
366
+ await commandHook.resume(runId, { command }); // [!code highlight]
367
+ return Response.json({ ok: true });
368
+ } catch (error) {
369
+ const msg = error instanceof Error ? error.message.toLowerCase() : "";
370
+ if (msg.includes("not found") || msg.includes("expired")) {
371
+ return Response.json({ ok: false, note: "session expired" }, { status: 410 });
372
+ }
373
+ throw error;
374
+ }
121
375
  }
122
376
  ```
123
377
 
124
- ## Saga Pattern for Cleanup
378
+ </Tab>
379
+
380
+ <Tab value="Client">
381
+
382
+ On mount, if a `runId` is stashed in `localStorage`, reconnect to the existing run. Otherwise start fresh. Commands are POSTed to `/command` — output lands on the `/start` stream.
383
+
384
+ ```tsx title="components/sandbox-runner.tsx" lineNumbers
385
+ "use client";
386
+
387
+ import { useCallback, useEffect, useRef, useState } from "react";
388
+ import type { SandboxEvent } from "@/workflows/sandbox-session";
389
+
390
+ const RUN_ID_KEY = "sandbox.runId";
391
+
392
+ export function SandboxRunner() {
393
+ const [events, setEvents] = useState<SandboxEvent[]>([]);
394
+ const runIdRef = useRef<string | null>(null);
395
+ const didReconnectRef = useRef(false);
396
+
397
+ const consume = useCallback(async (res: Response) => {
398
+ if (!res.ok || !res.body) return;
399
+ runIdRef.current = res.headers.get("x-workflow-run-id");
400
+ if (runIdRef.current) {
401
+ localStorage.setItem(RUN_ID_KEY, runIdRef.current); // [!code highlight]
402
+ }
403
+
404
+ const reader = res.body.getReader();
405
+ const decoder = new TextDecoder();
406
+ let buffer = "";
407
+
408
+ while (true) {
409
+ const { done, value } = await reader.read();
410
+ if (done) break;
411
+ buffer += decoder.decode(value, { stream: true });
412
+ const lines = buffer.split("\n");
413
+ buffer = lines.pop() ?? "";
414
+ for (const line of lines) {
415
+ if (!line.trim()) continue;
416
+ try {
417
+ setEvents((prev) => [...prev, JSON.parse(line) as SandboxEvent]);
418
+ } catch { /* malformed line */ }
419
+ }
420
+ }
421
+ }, []);
422
+
423
+ const openStream = useCallback(
424
+ async (runId?: string) => {
425
+ setEvents([]);
426
+ const res = await fetch("/api/sandbox/start", {
427
+ method: "POST",
428
+ headers: runId ? { "Content-Type": "application/json" } : undefined,
429
+ body: runId ? JSON.stringify({ runId }) : undefined,
430
+ });
431
+ await consume(res);
432
+ },
433
+ [consume]
434
+ );
435
+
436
+ // Auto-reconnect on mount if a runId is stashed.
437
+ useEffect(() => {
438
+ if (didReconnectRef.current) return;
439
+ didReconnectRef.current = true;
440
+ const stored = localStorage.getItem(RUN_ID_KEY);
441
+ if (stored) openStream(stored); // [!code highlight]
442
+ }, [openStream]);
443
+
444
+ const start = useCallback(() => {
445
+ localStorage.removeItem(RUN_ID_KEY);
446
+ runIdRef.current = null;
447
+ openStream();
448
+ }, [openStream]);
449
+
450
+ const sendCommand = useCallback(async (command: string) => {
451
+ if (!runIdRef.current) return;
452
+ const res = await fetch("/api/sandbox/command", {
453
+ method: "POST",
454
+ headers: { "Content-Type": "application/json" },
455
+ body: JSON.stringify({ runId: runIdRef.current, command }),
456
+ });
457
+ if (res.status === 410) localStorage.removeItem(RUN_ID_KEY);
458
+ }, []);
459
+
460
+ const destroy = useCallback(async () => {
461
+ await sendCommand("/destroy");
462
+ localStorage.removeItem(RUN_ID_KEY);
463
+ }, [sendCommand]);
464
+
465
+ // Render events as a terminal-style log. Drive UI state from `status` events
466
+ // (active / hibernating / hibernated / resuming / refreshing / destroyed).
467
+ return null;
468
+ }
469
+ ```
470
+
471
+ </Tab>
472
+
473
+ </Tabs>
474
+
475
+ ## How It Works
476
+
477
+ 1. **One workflow = one session.** The workflow owns a sandbox for its entire lifetime. The `runId` is the only state the client has to remember.
478
+ 2. **Hook created once.** `commandHook.create({ token: workflowRunId })` outside the loop. Creating it twice with the same token throws `HookConflictError`.
479
+ 3. **Two timer branches.** The active-state race wakes on the earlier of `idleDeadline` and `refreshDeadline`. The hibernated state awaits the hook alone — no timer, no compute.
480
+ 4. **Proactive refresh.** `refreshDeadline = sandboxExpiresAt - REFRESH_SAFETY_MS`. Hitting this triggers a snapshot + immediate new sandbox from that snapshot, rolling over the hard cap without user intervention.
481
+ 5. **`sandbox.snapshot()` stops the VM.** It's documented as part of the snapshot process — don't call `stop()` separately.
482
+ 6. **Resume = new sandbox.** `Sandbox.create({ source: { type: "snapshot", snapshotId } })` creates a fresh VM from the snapshot. The new sandbox has a different `sandboxId`; filesystem, installed packages, and git history are preserved.
483
+ 7. **Reconnect by runId.** `getRun(runId).getReadable({ startIndex: 0 })` replays the durable event log to a returning client, who rebuilds UI state from the replay.
484
+ 8. **Exit only on `/destroy`.** The workflow loop has no hard deadline of its own. Individual sandboxes time out; the session doesn't.
485
+
486
+ ## Pitfalls
487
+
488
+ ### `sandbox.stop()` is terminal
489
+
490
+ A stopped sandbox cannot be restarted — you have to create a new one. Hibernation is only possible via `snapshot()` + new-sandbox-from-snapshot. Don't try to "pause" an active sandbox with `stop()` and resume later.
491
+
492
+ ### `snapshot()` already stops the VM
493
+
494
+ Calling `stop()` after `snapshot()` either errors or is a no-op depending on timing. Snapshot takes care of it.
495
+
496
+ ### New `sandboxId` after resume and refresh
497
+
498
+ Both `resuming` (idle → command) and `refreshing` (near-hard-cap rotation) create a new sandbox with a new `sandboxId`. Emit it on the subsequent `status: "active"` event and have the UI read from there, not from the initial `created` event.
499
+
500
+ ### Keep the refresh margin generous
501
+
502
+ `snapshot()` + `Sandbox.create({ source })` takes real time (typically tens of seconds). If `REFRESH_SAFETY_MS` is too small, the old sandbox hits its hard cap mid-snapshot. Leave at least 60–90 seconds; 5 minutes is comfortable.
503
+
504
+ ### Don't call `writable.close()` inside a workflow function
505
+
506
+ Stream closure must happen inside a `"use step"` function. Calling `writable.close()` directly in the workflow body throws `Not supported in workflow functions`. The runtime closes the underlying writable when the workflow returns.
507
+
508
+ ### Handle stale `runId` gracefully
509
+
510
+ Clients can hold `runId`s from long-gone workflow runs (localStorage, back button, server restart). Gate the reconnect path on `run.exists` and fall through to starting fresh. On `hook.resume`, catch `not found` / `expired` and return 410 so the client clears its state.
511
+
512
+ ### Decide whether `/start` should be idempotent
513
+
514
+ The sample treats a missing or stale `runId` as a request for a new session. For one-session-per-resource behavior, use a durable resource key, such as `projectId` or `taskId`, to claim or retrieve the run before starting a new one.
515
+
516
+ ### Keep the hook outside the loop
517
+
518
+ Each iteration's `hook.then(...)` attaches a listener to the same hook instance. Creating a new hook per iteration with the same token throws `HookConflictError`. One hook, one token (`workflowRunId`), reused every iteration.
125
519
 
126
- Combine sandbox orchestration with the [saga pattern](/docs/cookbook/common-patterns/saga) to ensure sandboxes are always cleaned up, even when a step in the middle of your pipeline fails.
520
+ ## Key APIs
127
521
 
128
- The example above uses a try/catch around the command execution loop. For more complex pipelines with multiple resources (sandbox + database + external API), push compensation functions onto a stack as shown in the [saga recipe](/docs/cookbook/common-patterns/saga).
522
+ - [`Sandbox.create`](https://vercel.com/docs/sandbox) provision a VM (runtime, source, timeout)
523
+ - [`sandbox.runCommand`](https://vercel.com/docs/sandbox) — execute a command; implicit step
524
+ - [`sandbox.snapshot`](https://vercel.com/docs/sandbox) — save state and stop the VM; returns `Snapshot`
525
+ - [`defineHook()`](/docs/api-reference/workflow/define-hook) — suspension point for user commands
526
+ - [`sleep()`](/docs/api-reference/workflow/sleep) — durable timer that powers both idle hibernation and proactive refresh
527
+ - [`getRun()`](/docs/api-reference/workflow-api/get-run) — look up a run and replay its event log for reconnection
528
+ - [`getWritable()`](/docs/api-reference/workflow/get-writable) — resumable NDJSON event stream
529
+ - [Idempotency](/docs/foundations/idempotency) — choose when `/start` should reuse an existing run
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "title": "Cookbook",
3
3
  "defaultOpen": true,
4
- "pages": ["common-patterns", "agent-patterns", "integrations", "advanced"]
4
+ "pages": ["agent-patterns", "common-patterns", "integrations", "advanced"]
5
5
  }
@@ -93,7 +93,7 @@ interface Storage {
93
93
 
94
94
  **Run Creation:** For `run_created` events, the `runId` parameter may be a client-provided string or `null`. When `null`, your World generates and returns a new `runId`.
95
95
 
96
- **Hook Tokens:** Hook tokens must be unique. If a `hook_created` event conflicts with an existing token, return a `hook_conflict` event instead.
96
+ **Hook Tokens:** Hook tokens must be unique. If a `hook_created` event conflicts with an existing token, return a `hook_conflict` event instead and include the active hook owner's run ID as `eventData.conflictingRunId`.
97
97
 
98
98
  **Automatic Hook Disposal:** When a workflow reaches a terminal state (`completed`, `failed`, or `cancelled`), automatically dispose of all associated hooks to release tokens for reuse.
99
99
 
@@ -161,7 +161,9 @@ Prefix for graphile-worker queue job names. Useful when sharing a database betwe
161
161
 
162
162
  ### `WORKFLOW_POSTGRES_WORKER_CONCURRENCY`
163
163
 
164
- Number of concurrent workers polling for jobs. Default: `10`
164
+ Number of concurrent workers polling for jobs. Default: `50`.
165
+
166
+ This value also bounds how many parent→child workflow polls can be in flight simultaneously. Every `await childRun.returnValue` inside a workflow holds a worker slot until the child run terminates — if you expect recursive or highly-fanned-out parent/child workflows, raise this ceiling above the peak number of concurrent polls. With the default of 50, the included `fibonacciWorkflow` e2e test (fib(6), ~24 concurrent polls at peak) passes; deeper recursion or larger fanouts need a correspondingly larger setting.
165
167
 
166
168
  ### `WORKFLOW_POSTGRES_MAX_POOL_SIZE`
167
169
 
@@ -179,8 +181,8 @@ import { createWorld } from "@workflow/world-postgres";
179
181
  const world = createWorld({
180
182
  connectionString: "postgres://user:password@host:5432/database",
181
183
  jobPrefix: "myapp_",
182
- queueConcurrency: 20,
183
- maxPoolSize: 20, // overrides WORKFLOW_POSTGRES_MAX_POOL_SIZE
184
+ queueConcurrency: 50,
185
+ maxPoolSize: 52, // overrides WORKFLOW_POSTGRES_MAX_POOL_SIZE
184
186
  });
185
187
  ```
186
188