@workflow/core 5.0.0-beta.56 → 5.0.0-beta.58

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 (80) hide show
  1. package/README.md +7 -0
  2. package/dist/runtime/constants.d.ts +19 -3
  3. package/dist/runtime/constants.d.ts.map +1 -1
  4. package/dist/runtime/constants.js +30 -4
  5. package/dist/runtime/deployment-guard.d.ts.map +1 -1
  6. package/dist/runtime/deployment-guard.js +9 -5
  7. package/dist/runtime/dynamic-workflow.d.ts +148 -0
  8. package/dist/runtime/dynamic-workflow.d.ts.map +1 -0
  9. package/dist/runtime/dynamic-workflow.js +290 -0
  10. package/dist/runtime/helpers.d.ts +30 -0
  11. package/dist/runtime/helpers.d.ts.map +1 -1
  12. package/dist/runtime/helpers.js +34 -2
  13. package/dist/runtime/hook-wake.d.ts +69 -36
  14. package/dist/runtime/hook-wake.d.ts.map +1 -1
  15. package/dist/runtime/hook-wake.js +95 -59
  16. package/dist/runtime/lifecycle-hooks.d.ts +25 -22
  17. package/dist/runtime/lifecycle-hooks.d.ts.map +1 -1
  18. package/dist/runtime/lifecycle-hooks.js +74 -38
  19. package/dist/runtime/quickjs-entrypoint.d.ts.map +1 -1
  20. package/dist/runtime/quickjs-entrypoint.js +66 -32
  21. package/dist/runtime/quickjs-serde.d.ts.map +1 -1
  22. package/dist/runtime/quickjs-serde.js +14 -1
  23. package/dist/runtime/resume-hook.d.ts.map +1 -1
  24. package/dist/runtime/resume-hook.js +5 -3
  25. package/dist/runtime/run-spec-version.d.ts +16 -0
  26. package/dist/runtime/run-spec-version.d.ts.map +1 -0
  27. package/dist/runtime/run-spec-version.js +19 -0
  28. package/dist/runtime/run.d.ts.map +1 -1
  29. package/dist/runtime/run.js +5 -3
  30. package/dist/runtime/runs.d.ts.map +1 -1
  31. package/dist/runtime/runs.js +27 -7
  32. package/dist/runtime/start.d.ts +63 -1
  33. package/dist/runtime/start.d.ts.map +1 -1
  34. package/dist/runtime/start.js +415 -84
  35. package/dist/runtime/step-executor.d.ts.map +1 -1
  36. package/dist/runtime/step-executor.js +17 -10
  37. package/dist/runtime/step-latency.d.ts +3 -2
  38. package/dist/runtime/step-latency.d.ts.map +1 -1
  39. package/dist/runtime/step-latency.js +1 -1
  40. package/dist/runtime/suspension-handler.d.ts +34 -12
  41. package/dist/runtime/suspension-handler.d.ts.map +1 -1
  42. package/dist/runtime/suspension-handler.js +230 -174
  43. package/dist/runtime.d.ts +1 -1
  44. package/dist/runtime.d.ts.map +1 -1
  45. package/dist/runtime.js +239 -30
  46. package/dist/schemas.d.ts +1 -1
  47. package/dist/schemas.d.ts.map +1 -1
  48. package/dist/schemas.js +1 -1
  49. package/dist/serialization/reducers/common-vm.d.ts.map +1 -1
  50. package/dist/serialization/reducers/common-vm.js +7 -1
  51. package/dist/serialization/reducers/common.d.ts.map +1 -1
  52. package/dist/serialization/reducers/common.js +21 -1
  53. package/dist/serialization/types.d.ts +1 -0
  54. package/dist/serialization/types.d.ts.map +1 -1
  55. package/dist/serialization/types.js +1 -1
  56. package/dist/serialization-format.d.ts.map +1 -1
  57. package/dist/serialization-format.js +15 -1
  58. package/dist/serialization.d.ts +55 -2
  59. package/dist/serialization.d.ts.map +1 -1
  60. package/dist/serialization.js +94 -4
  61. package/dist/telemetry/semantic-conventions.d.ts +48 -0
  62. package/dist/telemetry/semantic-conventions.d.ts.map +1 -1
  63. package/dist/telemetry/semantic-conventions.js +31 -1
  64. package/dist/types.d.ts +1 -0
  65. package/dist/types.d.ts.map +1 -1
  66. package/dist/types.js +4 -1
  67. package/dist/version.d.ts +1 -1
  68. package/dist/version.js +2 -2
  69. package/dist/workflow.d.ts +3 -9
  70. package/dist/workflow.d.ts.map +1 -1
  71. package/dist/workflow.js +22 -4
  72. package/docs/api-reference/create-hook.mdx +30 -1
  73. package/docs/api-reference/create-webhook.mdx +1 -1
  74. package/docs/api-reference/define-hook.mdx +2 -0
  75. package/docs/foundations/errors-and-retries.mdx +3 -1
  76. package/docs/foundations/idempotency.mdx +44 -21
  77. package/docs/foundations/serialization.mdx +1 -0
  78. package/docs/foundations/starting-workflows.mdx +2 -0
  79. package/docs/how-it-works/event-sourcing.mdx +7 -1
  80. package/package.json +6 -5
@@ -201,12 +201,14 @@ try {
201
201
  | Code | Meaning |
202
202
  | --- | --- |
203
203
  | `USER_ERROR` | An error thrown in your workflow or step code (including propagated step failures like `FatalError`) |
204
- | `MAX_EVENTS_EXCEEDED` | The run reached the World's per-run event ceiling (25,000 on the Local and Vercel Worlds). Split unbounded loops into [child workflows](/cookbook/advanced/child-workflows); see [Limits](/docs/configuration/runtime-tuning#limits) |
204
+ | `MAX_EVENTS_EXCEEDED` | The run reached the World's per-run event ceiling (for the Vercel World, see [Workflow run limits](https://vercel.com/docs/workflows/pricing#workflow-run-limits)). Split unbounded loops into [child workflows](/cookbook/advanced/child-workflows) well before the ceiling; see [Vercel World limits](/worlds/vercel#per-run-limits) and [Limits](/docs/configuration/runtime-tuning#limits) |
205
205
  | `MAX_DELIVERIES_EXCEEDED` | The run exceeded the maximum number of queue deliveries |
206
206
  | `REPLAY_TIMEOUT` | A workflow replay exceeded the maximum allowed duration |
207
207
  | `REPLAY_DIVERGENCE` | A replay could not consume the event log deterministically, usually because of non-deterministic workflow code. |
208
208
  | `CORRUPTED_EVENT_LOG` | The event log cannot be replayed: it contains orphaned or mismatched events, or one of its stored payloads is no longer readable from the World's storage. If you see this, please [file an issue](https://github.com/vercel/workflow/issues) |
209
+ | `STREAM_ERROR` | Workflow stream infrastructure failed while reading or writing data. This is an SDK or backend failure rather than an error in workflow code |
209
210
  | `WORLD_CONTRACT_ERROR` | A World response violated the SDK contract; points at a World implementation bug |
211
+ | `DEPLOYMENT_MISMATCH` | The run was delivered to a deployment other than the one it is pinned to, and automatic re-routing did not recover it |
210
212
  | `RUNTIME_ERROR` | An internal runtime error. If you see this, please [file an issue](https://github.com/vercel/workflow/issues) |
211
213
 
212
214
  <Callout type="info">
@@ -61,7 +61,7 @@ Because [hooks](/docs/foundations/hooks) already ensure globally unique active t
61
61
 
62
62
  Use a hook token as the idempotency key for an active workflow run. Hook tokens are globally unique while they are active: if another run tries to create a hook with the same token, the runtime records a conflict, `hook.getConflict()` resolves with a `Run` handle for the run that owns the token, and the hook rejects with [`HookConflictError`](/docs/errors/hook-conflict) when the workflow awaits or iterates its payload.
63
63
 
64
- The token should come from your domain, such as an order ID, invoice ID, import ID, or request ID. Create the hook near the beginning of the workflow and check `await hook.getConflict()` before doing duplicate-sensitive work that depends on owning the active token. Calling `createHook()` alone does not register the hook; awaiting `getConflict()` suspends the workflow to commit the registration.
64
+ The token should come from your domain, such as an order ID, invoice ID, import ID, or request ID. Create the hook near the beginning of the workflow and check `await hook.getConflict()` before doing duplicate-sensitive work that depends on owning the active token. Calling `createHook()` alone does not register the hook; awaiting `getConflict()` suspends the workflow to commit the registration. Check it before calling any step: steps the workflow calls before it suspends are started alongside the hook's registration, so a duplicate run that only learns of the conflict later (for example, by awaiting the hook and letting `HookConflictError` end the run) may already have started them. See [Registering a hook before a step uses it](/docs/api-reference/workflow/create-hook#registering-a-hook-before-a-step-uses-it).
65
65
 
66
66
  ```typescript lineNumbers
67
67
  import { createHook } from "workflow";
@@ -242,7 +242,7 @@ export async function processOrder(orderId: string, confirmed: boolean) {
242
242
  }
243
243
  ```
244
244
 
245
- **Supersede the owner.** Without minimum retention, cancel the active run, then claim the released token. The retry loop covers the window where cancellation cleanup has not propagated yet:
245
+ **Supersede the owner.** Create the Hook with [`experimental_force: true`](/docs/api-reference/workflow/create-hook#take-over-a-token-another-run-holds) so the newest run takes the token over from the active owner. This also works for a finished run holding the token under `experimental_minRetention`:
246
246
 
247
247
  ```typescript lineNumbers
248
248
  import { createHook } from "workflow";
@@ -253,31 +253,54 @@ declare function chargeOrder(orderId: string): Promise<void>; // @setup
253
253
  export async function processOrderNewestWins(orderId: string) {
254
254
  "use workflow";
255
255
 
256
- const token = `order:${orderId}`;
257
-
258
- for (let attempt = 0; attempt < 3; attempt++) {
259
- using request = createHook<OrderRequest>({ token });
260
-
261
- const conflict = await request.getConflict();
262
- if (!conflict) {
263
- // Token claimed: this run is now the owner.
264
- const { confirmed } = await request;
265
- if (confirmed) {
266
- await chargeOrder(orderId);
267
- }
268
- return { status: "processed" as const };
269
- }
256
+ using request = createHook<OrderRequest>({
257
+ token: `order:${orderId}`,
258
+ experimental_force: true, // [!code highlight]
259
+ });
270
260
 
271
- await conflict.cancel(); // [!code highlight]
261
+ // This run now owns the token.
262
+ const { confirmed } = await request;
263
+ if (confirmed) {
264
+ await chargeOrder(orderId);
272
265
  }
266
+ return { status: "processed" as const };
267
+ }
268
+ ```
269
+
270
+ The previous owner's `await request` rejects with [`HookForceClaimedError`](/docs/api-reference/workflow-errors/hook-force-claimed-error), and every `resumeHook()` for the token reaches the new run from then on. Any run of this workflow can be superseded by a later one, so catch the error and exit cleanly:
271
+
272
+ ```typescript lineNumbers
273
+ import { createHook } from "workflow";
274
+ import { HookForceClaimedError } from "workflow/errors";
273
275
 
274
- throw new Error(`Could not claim ${token} after canceling the owner`);
276
+ type OrderRequest = { confirmed: boolean };
277
+ declare function chargeOrder(orderId: string): Promise<void>; // @setup
278
+
279
+ export async function processOrderNewestWins(orderId: string) {
280
+ "use workflow";
281
+
282
+ using request = createHook<OrderRequest>({
283
+ token: `order:${orderId}`,
284
+ experimental_force: true,
285
+ });
286
+
287
+ try {
288
+ const { confirmed } = await request;
289
+ if (confirmed) {
290
+ await chargeOrder(orderId);
291
+ }
292
+ return { status: "processed" as const };
293
+ } catch (error) {
294
+ if (HookForceClaimedError.is(error)) { // [!code highlight]
295
+ // A newer run for this order owns the token now. Stop here.
296
+ return { status: "superseded" as const, runId: error.claimedByRunId }; // [!code highlight]
297
+ }
298
+ throw error;
299
+ }
275
300
  }
276
301
  ```
277
302
 
278
- <Callout type="warn">
279
- This pattern does not work with `experimental_minRetention`: canceling the old run does not make its token available early.
280
- </Callout>
303
+ A run started at a Workflow spec version below 8, including runs started by older SDK releases, can't be taken from. In that case the forced Hook rejects with `HookConflictError`, as it would without `experimental_force`.
281
304
 
282
305
  If duplicate requests should only reuse the active run without sending data, use [`getHookByToken()`](/docs/api-reference/workflow-api/get-hook-by-token) as an advisory pre-check before calling `start()`. The workflow should still check `hook.getConflict()`, because the lookup and `start()` are not atomic.
283
306
 
@@ -34,6 +34,7 @@ The following types can be serialized and passed through workflow functions:
34
34
  - `bigint`
35
35
  - `ArrayBuffer`
36
36
  - `BigInt64Array`, `BigUint64Array`
37
+ - `DataView`
37
38
  - `Date`
38
39
  - `Float32Array`, `Float64Array`
39
40
  - `Int8Array`, `Int16Array`, `Int32Array`
@@ -102,6 +102,8 @@ export async function parentWorkflow(inputValue: number) {
102
102
 
103
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.
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.
106
+
105
107
  <Callout type="info">
106
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.
107
109
  </Callout>
@@ -188,7 +188,7 @@ Events are categorized by the entity type they affect. Each event contains metad
188
188
  | `hook_created` | Creates a new hook in `active` state. Contains the hook token and optional metadata. |
189
189
  | `hook_conflict` | Records that hook creation failed because another run owns the token. Contains the token and, for current worlds, the owner's run ID. The hook is not created: `hook.getConflict()` resolves with the conflicting run, and awaiting the hook payload rejects with a `HookConflictError`. |
190
190
  | `hook_received` | Records that a payload was delivered to the hook. The hook remains `active` and can receive more payloads. |
191
- | `hook_disposed` | Deletes the hook from storage (conceptually transitioning to `disposed` state). The token is released for reuse by future workflows. |
191
+ | `hook_disposed` | Deletes the hook from storage (conceptually transitioning to `disposed` state). The token is released for reuse by future workflows. When another run takes the token over with [`experimental_force`](/docs/api-reference/workflow/create-hook#take-over-a-token-another-run-holds), the event names the new owner in `forceClaimedBy`, and the token moves to that run instead of being released. |
192
192
 
193
193
  ### Wait events
194
194
 
@@ -203,6 +203,12 @@ Events are categorized by the entity type they affect. Each event contains metad
203
203
  |-------|-------------|
204
204
  | `noop` | Seals an abandoned log position (`specVersion` 7 and above). Only the backend writes this event; the create endpoints reject it. See [Sealed positions](#sealed-positions-noop-events). |
205
205
 
206
+ ### How fast a log grows
207
+
208
+ Counting the events above is how you size a run. A step that succeeds on the first attempt contributes three events; a retry adds another `step_started`, preceded by a `step_retrying` when the World emits one; a sleep contributes two, and a hook at least two.
209
+
210
+ A run's log is capped — the [Vercel World](/worlds/vercel#per-run-limits) fails anything past its ceiling with `MAX_EVENTS_EXCEEDED`. Replay reads the whole log, so a run gets slower as it grows and is worth splitting well before it fails: split into [child workflows](/cookbook/advanced/child-workflows) once a run would grow past a few thousand events.
211
+
206
212
  ## Terminal states
207
213
 
208
214
  Terminal states represent the end of an entity's lifecycle. Once an entity reaches a terminal state, no further events can transition it to another state.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workflow/core",
3
- "version": "5.0.0-beta.56",
3
+ "version": "5.0.0-beta.58",
4
4
  "description": "Core runtime and engine for Workflow SDK",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -89,12 +89,13 @@
89
89
  "@standard-schema/spec": "1.0.0",
90
90
  "@types/ms": "2.1.0",
91
91
  "@vercel/functions": "^3.8.0",
92
- "@workflow/errors": "5.0.0-beta.23",
92
+ "@workflow/errors": "5.0.0-beta.25",
93
93
  "@workflow/serde": "5.0.0-beta.2",
94
94
  "@workflow/utils": "5.0.0-beta.10",
95
- "@workflow/world": "5.0.0-beta.38",
96
- "@workflow/world-local": "5.0.0-beta.47",
97
- "@workflow/world-vercel": "5.0.0-beta.51",
95
+ "@workflow/world": "5.0.0-beta.40",
96
+ "@workflow/world-local": "5.0.0-beta.49",
97
+ "@workflow/world-vercel": "5.0.0-beta.53",
98
+ "acorn": "8.15.0",
98
99
  "debug": "4.4.3",
99
100
  "devalue": "5.9.2",
100
101
  "ms": "2.1.3",