@workflow/core 5.3.0 → 5.4.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 (88) hide show
  1. package/dist/create-hook.d.ts +15 -0
  2. package/dist/create-hook.d.ts.map +1 -1
  3. package/dist/create-hook.js +1 -1
  4. package/dist/events-consumer.d.ts +1 -1
  5. package/dist/events-consumer.js +1 -1
  6. package/dist/flushable-stream.js +11 -2
  7. package/dist/private.d.ts +2 -2
  8. package/dist/private.d.ts.map +1 -1
  9. package/dist/private.js +1 -1
  10. package/dist/runtime/after-terminal-write.d.ts +33 -0
  11. package/dist/runtime/after-terminal-write.d.ts.map +1 -0
  12. package/dist/runtime/after-terminal-write.js +23 -0
  13. package/dist/runtime/child-run-completion-notify.d.ts +36 -0
  14. package/dist/runtime/child-run-completion-notify.d.ts.map +1 -0
  15. package/dist/runtime/child-run-completion-notify.js +97 -0
  16. package/dist/runtime/child-run-completion.d.ts +45 -0
  17. package/dist/runtime/child-run-completion.d.ts.map +1 -0
  18. package/dist/runtime/child-run-completion.js +192 -0
  19. package/dist/runtime/delivery.d.ts +5 -2
  20. package/dist/runtime/delivery.d.ts.map +1 -1
  21. package/dist/runtime/delivery.js +6 -4
  22. package/dist/runtime/deployment-guard.d.ts.map +1 -1
  23. package/dist/runtime/deployment-guard.js +3 -3
  24. package/dist/runtime/helpers.d.ts +9 -1
  25. package/dist/runtime/helpers.d.ts.map +1 -1
  26. package/dist/runtime/helpers.js +6 -1
  27. package/dist/runtime/out-of-band-observation.d.ts +6 -0
  28. package/dist/runtime/out-of-band-observation.d.ts.map +1 -1
  29. package/dist/runtime/out-of-band-observation.js +12 -3
  30. package/dist/runtime/quickjs-runtime.d.ts +5 -2
  31. package/dist/runtime/quickjs-runtime.d.ts.map +1 -1
  32. package/dist/runtime/quickjs-runtime.js +23 -111
  33. package/dist/runtime/quickjs-serde.js +2 -2
  34. package/dist/runtime/replay-budget.d.ts.map +1 -1
  35. package/dist/runtime/replay-budget.js +4 -4
  36. package/dist/runtime/resume-hook.d.ts +9 -1
  37. package/dist/runtime/resume-hook.d.ts.map +1 -1
  38. package/dist/runtime/resume-hook.js +20 -10
  39. package/dist/runtime/run.d.ts +25 -2
  40. package/dist/runtime/run.d.ts.map +1 -1
  41. package/dist/runtime/run.js +72 -7
  42. package/dist/runtime/runs.d.ts +1 -1
  43. package/dist/runtime/runs.d.ts.map +1 -1
  44. package/dist/runtime/runs.js +42 -26
  45. package/dist/runtime/spec8/delivery.d.ts.map +1 -1
  46. package/dist/runtime/spec8/delivery.js +15 -10
  47. package/dist/runtime/spec8/quickjs-entrypoint.d.ts.map +1 -1
  48. package/dist/runtime/spec8/quickjs-entrypoint.js +4 -4
  49. package/dist/runtime/spec8/suspension-handler.d.ts.map +1 -1
  50. package/dist/runtime/spec8/suspension-handler.js +4 -1
  51. package/dist/runtime/spec9/delivery.d.ts.map +1 -1
  52. package/dist/runtime/spec9/delivery.js +15 -10
  53. package/dist/runtime/spec9/quickjs-entrypoint.d.ts.map +1 -1
  54. package/dist/runtime/spec9/quickjs-entrypoint.js +4 -4
  55. package/dist/runtime/start-hook-binding.d.ts +2 -2
  56. package/dist/runtime/start-hook-binding.js +1 -1
  57. package/dist/runtime/start-hook-delivery.d.ts +3 -3
  58. package/dist/runtime/start-hook-delivery.js +4 -4
  59. package/dist/runtime/start-hook-holder.d.ts +1 -1
  60. package/dist/runtime/start-hook-holder.js +2 -2
  61. package/dist/runtime/start.d.ts +5 -5
  62. package/dist/runtime/start.d.ts.map +1 -1
  63. package/dist/runtime/start.js +57 -22
  64. package/dist/runtime.d.ts.map +1 -1
  65. package/dist/runtime.js +6 -3
  66. package/dist/serialization/types.d.ts +1 -1
  67. package/dist/serialization/types.d.ts.map +1 -1
  68. package/dist/serialization/types.js +1 -1
  69. package/dist/serialization.js +3 -3
  70. package/dist/step.js +4 -4
  71. package/dist/telemetry/semantic-conventions.d.ts +9 -9
  72. package/dist/telemetry/semantic-conventions.d.ts.map +1 -1
  73. package/dist/telemetry/semantic-conventions.js +10 -10
  74. package/dist/version.d.ts +1 -1
  75. package/dist/version.js +2 -2
  76. package/dist/workflow/abort-controller.d.ts.map +1 -1
  77. package/dist/workflow/abort-controller.js +3 -4
  78. package/dist/workflow/hook.d.ts.map +1 -1
  79. package/dist/workflow/hook.js +7 -3
  80. package/dist/workflow.d.ts.map +1 -1
  81. package/dist/workflow.js +7 -120
  82. package/docs/api-reference/create-hook.mdx +1 -1
  83. package/docs/api-reference/create-webhook.mdx +1 -1
  84. package/docs/api-reference/fetch.mdx +1 -5
  85. package/docs/foundations/hooks.mdx +1 -1
  86. package/docs/foundations/idempotency.mdx +11 -11
  87. package/docs/foundations/starting-workflows.mdx +3 -7
  88. package/package.json +3 -3
@@ -188,7 +188,7 @@ Custom hook tokens are the recommended way to coordinate active workflow runs. U
188
188
 
189
189
  ### Open the run's start Hook
190
190
 
191
- A run started with [`start(workflow, args, { hook: { token } })`](/docs/api-reference/workflow-api/start#prevent-duplicate-starts) already owns its token through its start Hook. Calling `createHook({ token })` with that token in the run returns a handle bound to the start Hook instead of registering a new one: it adds no step, `await hook.getConflict()` resolves `null`, and payloads resumed before the call are delivered in order. Set `metadata` and `experimental_minRetention` on `start({ hook })`; a start Hook can't be a webhook or force-claim its token. Passing any of `metadata`, `experimental_minRetention`, `isWebhook` or `experimental_force` to this `createHook()` throws. Open it with `const`: disposing the handle frees the token, as for any Hook, and a later `start()` with it is then admitted as a new run (see [Take care with the start Hook's lifecycle](/docs/api-reference/workflow-api/start#take-care-with-the-start-hooks-lifecycle)). See [Receive the payloads in the workflow](/docs/foundations/idempotency#receive-the-payloads-in-the-workflow).
191
+ A run started with [`start(workflow, args, { experimental_hook: { token } })`](/docs/api-reference/workflow-api/start#prevent-duplicate-starts) already owns its token through its start Hook. Calling `createHook({ token })` with that token in the run returns a handle bound to the start Hook instead of registering a new one: it adds no step, `await hook.getConflict()` resolves `null`, and payloads resumed before the call are delivered in order. Set `metadata` and `experimental_minRetention` on `start({ experimental_hook })`; a start Hook can't be a webhook or force-claim its token. Passing any of `metadata`, `experimental_minRetention`, `isWebhook` or `experimental_force` to this `createHook()` throws. Open it with `const`: disposing the handle frees the token, as for any Hook, and a later `start()` with it is then admitted as a new run (see [Take care with the start Hook's lifecycle](/docs/api-reference/workflow-api/start#take-care-with-the-start-hooks-lifecycle)). See [Receive the payloads in the workflow](/docs/foundations/idempotency#receive-the-payloads-in-the-workflow).
192
192
 
193
193
  ### Keep a token unavailable after the run ends
194
194
 
@@ -53,7 +53,7 @@ showSections={['returns']}
53
53
 
54
54
  The returned `Webhook` object has:
55
55
 
56
- - `url`: The HTTP endpoint URL that external systems can call
56
+ - `url`: The HTTP endpoint URL that external systems can call. On Vercel, it uses the deployment URL. Elsewhere, it's `http://localhost` on the `PORT` environment variable, or on the detected server port when `PORT` is unset
57
57
  - `token`: The unique token identifying this webhook
58
58
  - `getConflict()`: A promise that resolves with the conflicting run if another active hook already owns this token, or `null` once the webhook endpoint has been registered. The endpoint is registered alongside the steps the workflow starts at the same time, not ahead of them, so await `getConflict()` before a step that hands `url` to a caller who may request it right away. See [Registering a hook before a step uses it](/docs/api-reference/workflow/create-hook#registering-a-hook-before-a-step-uses-it).
59
59
  - Implements `AsyncIterable<T>` for handling multiple requests, where `T` is `Request` (default) or `RequestWithResponse` (manual mode)
@@ -91,11 +91,7 @@ We call `fetch()` with a URL and optional request options, like the standard fet
91
91
 
92
92
  This API lets you use `fetch` in a workflow. You can also implement a custom fetch for different error handling and retry logic.
93
93
 
94
- ### Reading the response body
95
-
96
- Inside a workflow function, `response.json()`, `response.text()`, and `response.arrayBuffer()` each read the body through a step, so the result is recorded in the event log and replays deterministically.
97
-
98
- `response.body` can also be read with `getReader()` or `for await`, which is how libraries such as the AI SDK read responses. The whole body is loaded through a single step and delivered as one chunk. The body is not marked as used after a read: each new reader or body method runs its own step, so read the body once and reuse the result. Incremental streaming APIs such as `pipeThrough()` and `tee()` are not available in workflow functions. To process a response as a stream, read it inside a step function.
94
+ Libraries that call `fetch` internally, like AI SDK calls, should be called from a step function. For agents, use AI SDK's [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent). See [`fetch-in-workflow`](/docs/errors/fetch-in-workflow).
99
95
 
100
96
  ### Customizing fetch behavior
101
97
 
@@ -112,7 +112,7 @@ export async function orderWorkflow(orderId: string) {
112
112
  }
113
113
  ```
114
114
 
115
- Calling `createHook()` on its own does not register the hook; registration is only committed when the workflow suspends. Awaiting `hook.getConflict()` suspends the workflow to commit the hook registration, then resolves with `null` once the hook is registered and ready to receive payloads, or with a `Run` handle for the run that owns the token (see [`HookConflictError`](/docs/errors/hook-conflict)). For `hook_conflict` events persisted by older worlds that did not record the owning run's ID, `getConflict()` rejects with `HookConflictError` instead of resolving with an incomplete handle. The conflicting run's accessors are durable steps, so the workflow can inspect `await conflict.status`, wait on `await conflict.returnValue`, or cancel the owner with `await conflict.cancel()`. See [Run idempotency](/docs/foundations/idempotency#run-idempotency) for these strategies. A run started with [`start({ hook })`](/docs/api-reference/workflow-api/start#prevent-duplicate-starts) already owns its token: `createHook({ token })` with that token binds to the run's start Hook, and `getConflict()` resolves `null` without suspending.
115
+ Calling `createHook()` on its own does not register the hook; registration is only committed when the workflow suspends. Awaiting `hook.getConflict()` suspends the workflow to commit the hook registration, then resolves with `null` once the hook is registered and ready to receive payloads, or with a `Run` handle for the run that owns the token (see [`HookConflictError`](/docs/errors/hook-conflict)). For `hook_conflict` events persisted by older worlds that did not record the owning run's ID, `getConflict()` rejects with `HookConflictError` instead of resolving with an incomplete handle. The conflicting run's accessors are durable steps, so the workflow can inspect `await conflict.status`, wait on `await conflict.returnValue`, or cancel the owner with `await conflict.cancel()`. See [Run idempotency](/docs/foundations/idempotency#run-idempotency) for these strategies. A run started with [`start({ experimental_hook })`](/docs/api-reference/workflow-api/start#prevent-duplicate-starts) already owns its token: `createHook({ token })` with that token binds to the run's start Hook, and `getConflict()` resolves `null` without suspending.
116
116
 
117
117
  Awaiting the registration costs one write, not a round trip through the queue: the invocation that commits the registration is the one that carries on past the awaiter, and it runs the steps the workflow started beside the awaiter — as in `await Promise.all([hook.getConflict(), prepare()])` — in that same invocation, once the registration is recorded. That covers the steps the invocation runs inline; a fan-out wider than the free inline slots ([`WORKFLOW_MAX_INLINE_STEPS`](/docs/configuration/runtime-tuning#workflow_max_inline_steps), less any inline steps the invocation is still running) queues the rest alongside the hook, so await `hook.getConflict()` before the step when the ordering itself matters. See [`createHook()`](/docs/api-reference/workflow/create-hook#registering-a-hook-before-a-step-uses-it).
118
118
 
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  title: Idempotency
3
- description: Make step retries safe, and start a workflow run at most once per key with start({ hook }).
3
+ description: Make step retries safe, and start a workflow run at most once per key with start({ experimental_hook }).
4
4
  type: conceptual
5
5
  summary: Use step IDs for retry-safe external calls, and pass a deterministic hook token to start() so a duplicate request is refused or routed to the run that owns it.
6
6
  prerequisites:
@@ -71,7 +71,7 @@ export async function POST(request: Request) {
71
71
 
72
72
  try {
73
73
  const run = await start(processOrder, [orderId], {
74
- hook: { token: `order:${orderId}` }, // [!code highlight]
74
+ experimental_hook: { token: `order:${orderId}` }, // [!code highlight]
75
75
  });
76
76
  return Response.json({ runId: run.runId, duplicate: false });
77
77
  } catch (error) {
@@ -81,7 +81,7 @@ export async function POST(request: Request) {
81
81
  }
82
82
  ```
83
83
 
84
- `start({ hook })` admits the run and creates its **start Hook** in one atomic step. Exactly one run owns the token. A duplicate `start()` with the same token throws [`HookConflictError`](/docs/api-reference/workflow-errors/hook-conflict-error) with the owner's `conflictingRunId`. The duplicate never becomes a run, and none of its code runs.
84
+ `start({ experimental_hook })` admits the run and creates its **start Hook** in one atomic step. Exactly one run owns the token. A duplicate `start()` with the same token throws [`HookConflictError`](/docs/api-reference/workflow-errors/hook-conflict-error) with the owner's `conflictingRunId`. The duplicate never becomes a run, and none of its code runs.
85
85
 
86
86
  ### Start or route to the owner
87
87
 
@@ -99,7 +99,7 @@ export async function POST(request: Request) {
99
99
  let owner: string | undefined;
100
100
  for (let attempt = 0; attempt < 3; attempt++) {
101
101
  try {
102
- const run = await start(processOrder, [orderId, note], { hook: { token } });
102
+ const run = await start(processOrder, [orderId, note], { experimental_hook: { token } });
103
103
  return Response.json({ runId: run.runId, started: true });
104
104
  } catch (error) {
105
105
  if (!HookConflictError.is(error)) throw error;
@@ -152,7 +152,7 @@ export async function processOrder(orderId: string, note: string) {
152
152
  - `createHook({ token })` with the run's start token returns a handle bound to the start Hook. It doesn't register a new Hook, so it adds no step, and `await hook.getConflict()` resolves `null`.
153
153
  - Payloads resumed before the workflow opens the Hook are kept, and are delivered in order once it does.
154
154
  - If the workflow never opens it, the token was only an idempotency key. The run completes normally, and unread payloads are simply left unread.
155
- - `metadata` and `experimental_minRetention` belong on `start({ hook })`; a start Hook can't be a webhook or force-claim its token. Passing any of `metadata`, `experimental_minRetention`, `isWebhook` or `experimental_force` to this `createHook()` throws.
155
+ - `metadata` and `experimental_minRetention` belong on `start({ experimental_hook })`; a start Hook can't be a webhook or force-claim its token. Passing any of `metadata`, `experimental_minRetention`, `isWebhook` or `experimental_force` to this `createHook()` throws.
156
156
 
157
157
  ### Keep the token after the run ends
158
158
 
@@ -166,7 +166,7 @@ import { processOrder } from "./workflows/process-order";
166
166
  declare const orderId: string; // @setup
167
167
 
168
168
  const run = await start(processOrder, [orderId, ""], {
169
- hook: {
169
+ experimental_hook: {
170
170
  token: `order:${orderId}`,
171
171
  experimental_minRetention: "30 days", // [!code highlight]
172
172
  metadata: { orderId },
@@ -222,17 +222,17 @@ Disposing the start Hook ends the run's idempotency: from the end of that block,
222
222
 
223
223
  ### When `start()` can't confirm the outcome
224
224
 
225
- If `start({ hook })` can't confirm whether its candidate run was queued or admitted, for example because the World timed out, it throws [`WorkflowStartError`](/docs/api-reference/workflow-errors/workflow-start-error) with the candidate's `runId`. The outcome is unknown, not failed. Retrying with the same token is safe: the retry either becomes the owner, or is refused with a `HookConflictError` naming the owner. Inside a workflow, the `start()` step retries it on its own.
225
+ If `start({ experimental_hook })` can't confirm whether its candidate run was queued or admitted, for example because the World timed out, it throws [`WorkflowStartError`](/docs/api-reference/workflow-errors/workflow-start-error) with the candidate's `runId`. The outcome is unknown, not failed. Retrying with the same token is safe: the retry either becomes the owner, or is refused with a `HookConflictError` naming the owner. Inside a workflow, the `start()` step retries it on its own.
226
226
 
227
227
  ### World support
228
228
 
229
- `start({ hook })` requires a World that supports atomic start Hooks (`capabilities.atomicStartHook`), and Hook retention when you set `experimental_minRetention`. Otherwise `start()` throws before it enqueues anything.
229
+ `start({ experimental_hook })` requires a World that supports atomic start Hooks (`capabilities.atomicStartHook`), and Hook retention when you set `experimental_minRetention`. Otherwise `start()` throws before it enqueues anything.
230
230
 
231
231
  | World | Support |
232
232
  | --- | --- |
233
233
  | [Local World](/worlds/local) | Supported. |
234
- | [Postgres World](/worlds/postgres) | Opt in with [`WORKFLOW_POSTGRES_EXPERIMENTAL_START_HOOK=1`](/worlds/postgres#workflow_postgres_experimental_start_hook), after applying migration `0028_add_start_hook_admissions` (required for every Postgres World on this version, with or without the flag). Stop workers running a previous version first. |
235
- | [Vercel World](/worlds/vercel) | Opt in with [`WORKFLOW_VERCEL_EXPERIMENTAL_START_HOOK=1`](/worlds/vercel#workflow_vercel_experimental_start_hook), which needs a backend that supports start Hooks. |
234
+ | [Postgres World](/worlds/postgres) | Supported, after applying migration `0028_add_start_hook_admissions`. Stop workers running a previous version first; see [Start Hooks](/worlds/postgres#start-hooks). |
235
+ | [Vercel World](/worlds/vercel) | Supported. |
236
236
 
237
237
  See [`start()`](/docs/api-reference/workflow-api/start#prevent-duplicate-starts) for the full option reference.
238
238
 
@@ -278,7 +278,7 @@ export async function processOrder(orderId: string): Promise<OrderResult> {
278
278
  Outside the workflow, try `resumeHook()` first, and on `HookNotFoundError` call `start()` and retry the resume until the new run registers its hook.
279
279
 
280
280
  <Callout type="warn">
281
- Unlike `start({ hook })`, this doesn't stop a duplicate from becoming a run. Two concurrent requests can both see "no hook yet" and each call `start()`. The losing run is still created, queued, and started, and returns once `getConflict()` resolves with the owner. The route can detect it by comparing the resumed hook's `runId` with the run it started.
281
+ Unlike `start({ experimental_hook })`, this doesn't stop a duplicate from becoming a run. Two concurrent requests can both see "no hook yet" and each call `start()`. The losing run is still created, queued, and started, and returns once `getConflict()` resolves with the owner. The route can detect it by comparing the resumed hook's `runId` with the run it started.
282
282
  </Callout>
283
283
 
284
284
  The token becomes available when its workflow ends, unless you set `experimental_minRetention` on `createHook()`. See [`createHook()` minimum retention](/docs/api-reference/workflow/create-hook#keep-a-token-unavailable-after-the-run-ends).
@@ -100,16 +100,12 @@ export async function parentWorkflow(inputValue: number) {
100
100
  }
101
101
  ```
102
102
 
103
- When you call `start()` inside a workflow function, it automatically executes through an internal step to maintain deterministic replay. The returned `Run` object works as it does outside workflows. Properties such as `.runId`, `.status`, and `.returnValue`, and methods such as `.cancel()`, are all available. Each property access or method call executes as a separate step.
103
+ When you call `start()` inside a workflow function, it automatically executes through an internal step to maintain deterministic replay. The returned `Run` object works as it does outside workflows. Properties such as `.runId`, `.status`, and `.returnValue`, and methods such as `.cancel()`, are all available.
104
104
 
105
- If the child run fails or is canceled, `await childRun.returnValue` throws [`WorkflowRunFailedError`](/docs/api-reference/workflow-errors/workflow-run-failed-error) or [`WorkflowRunCancelledError`](/docs/api-reference/workflow-errors/workflow-run-cancelled-error) on the first attempt.
105
+ Awaiting `childRun.returnValue` suspends the parent on a system hook that the child resumes when it finishes, so the parent waits without polling, uses no compute while it waits, and resumes as soon as the child is done. A child canceled without going through the SDK is noticed later, at the parent's next safety-net check; see [Cancellations that bypass the SDK](/cookbook/advanced/child-workflows#cancellations-that-bypass-the-sdk). If the child run fails or is canceled, `await childRun.returnValue` throws [`WorkflowRunFailedError`](/docs/api-reference/workflow-errors/workflow-run-failed-error) or [`WorkflowRunCancelledError`](/docs/api-reference/workflow-errors/workflow-run-cancelled-error) on the first attempt.
106
106
 
107
107
  <Callout type="info">
108
- Inside workflow functions, each `Run` property access (e.g., `run.status`, `run.returnValue`) triggers a workflow step. This means each access is recorded in the event log and replayed deterministically.
109
- </Callout>
110
-
111
- <Callout type="warn">
112
- Awaiting `returnValue` polls the child run every second, and the polling step holds its worker slot open until the child finishes. Size worker-based Worlds to cover the peak number of these polls in flight. If the child workflow is long-running, spawn it without awaiting `returnValue` and have it resume a [hook](/docs/foundations/hooks) when it completes. See the [`startAndWait()` pattern](/cookbook/advanced/child-workflows).
108
+ Inside workflow functions, each `Run` property access (e.g., `run.status`, `run.returnValue`) is recorded in the event log and replayed deterministically. Most accessors run as one step; `returnValue` records the system hook and the steps that read the child's result.
113
109
  </Callout>
114
110
 
115
111
  ### Fire and forget
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workflow/core",
3
- "version": "5.3.0",
3
+ "version": "5.4.0",
4
4
  "description": "Core runtime and engine for Workflow SDK",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -93,8 +93,8 @@
93
93
  "@workflow/serde": "5.0.0",
94
94
  "@workflow/utils": "5.0.2",
95
95
  "@workflow/world": "5.1.0",
96
- "@workflow/world-local": "5.2.0",
97
- "@workflow/world-vercel": "5.3.0",
96
+ "@workflow/world-local": "5.2.1",
97
+ "@workflow/world-vercel": "5.4.0",
98
98
  "acorn": "8.15.0",
99
99
  "debug": "4.4.3",
100
100
  "devalue": "6.0.3",