@workflow/core 5.0.0-beta.55 → 5.0.0-beta.57
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.
- package/README.md +7 -0
- package/dist/create-hook.d.ts +38 -1
- package/dist/create-hook.d.ts.map +1 -1
- package/dist/create-hook.js +1 -1
- package/dist/global.d.ts +2 -0
- package/dist/global.d.ts.map +1 -1
- package/dist/global.js +1 -1
- package/dist/runtime/constants.d.ts +13 -3
- package/dist/runtime/constants.d.ts.map +1 -1
- package/dist/runtime/constants.js +54 -4
- package/dist/runtime/deployment-guard.d.ts.map +1 -1
- package/dist/runtime/deployment-guard.js +9 -5
- package/dist/runtime/helpers.d.ts +23 -0
- package/dist/runtime/helpers.d.ts.map +1 -1
- package/dist/runtime/helpers.js +23 -2
- package/dist/runtime/hook-wake.d.ts +122 -0
- package/dist/runtime/hook-wake.d.ts.map +1 -0
- package/dist/runtime/hook-wake.js +225 -0
- package/dist/runtime/lifecycle-hooks.d.ts +25 -22
- package/dist/runtime/lifecycle-hooks.d.ts.map +1 -1
- package/dist/runtime/lifecycle-hooks.js +74 -38
- package/dist/runtime/open-hook-wait-state.d.ts +55 -0
- package/dist/runtime/open-hook-wait-state.d.ts.map +1 -0
- package/dist/runtime/open-hook-wait-state.js +93 -0
- package/dist/runtime/quickjs-entrypoint.d.ts.map +1 -1
- package/dist/runtime/quickjs-entrypoint.js +89 -16
- package/dist/runtime/quickjs-runtime.d.ts +2 -0
- package/dist/runtime/quickjs-runtime.d.ts.map +1 -1
- package/dist/runtime/quickjs-runtime.js +103 -4
- package/dist/runtime/quickjs-serde.d.ts.map +1 -1
- package/dist/runtime/quickjs-serde.js +63 -1
- package/dist/runtime/resume-hook.d.ts.map +1 -1
- package/dist/runtime/resume-hook.js +466 -314
- package/dist/runtime/run-spec-version.d.ts +16 -0
- package/dist/runtime/run-spec-version.d.ts.map +1 -0
- package/dist/runtime/run-spec-version.js +19 -0
- package/dist/runtime/run.d.ts.map +1 -1
- package/dist/runtime/run.js +5 -3
- package/dist/runtime/runs.d.ts.map +1 -1
- package/dist/runtime/runs.js +15 -6
- package/dist/runtime/start.d.ts +59 -1
- package/dist/runtime/start.d.ts.map +1 -1
- package/dist/runtime/start.js +271 -44
- package/dist/runtime/step-executor.d.ts.map +1 -1
- package/dist/runtime/step-executor.js +16 -10
- package/dist/runtime/step-latency.d.ts +3 -2
- package/dist/runtime/step-latency.d.ts.map +1 -1
- package/dist/runtime/step-latency.js +1 -1
- package/dist/runtime/suspension-handler.d.ts +42 -13
- package/dist/runtime/suspension-handler.d.ts.map +1 -1
- package/dist/runtime/suspension-handler.js +244 -146
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +228 -73
- package/dist/serialization/reducers/common-vm.d.ts.map +1 -1
- package/dist/serialization/reducers/common-vm.js +40 -1
- package/dist/serialization/reducers/common.d.ts.map +1 -1
- package/dist/serialization/reducers/common.js +27 -2
- package/dist/serialization/types.d.ts +8 -0
- package/dist/serialization/types.d.ts.map +1 -1
- package/dist/serialization/types.js +1 -1
- package/dist/serialization.d.ts +15 -2
- package/dist/serialization.d.ts.map +1 -1
- package/dist/serialization.js +5 -4
- package/dist/telemetry/semantic-conventions.d.ts +29 -0
- package/dist/telemetry/semantic-conventions.d.ts.map +1 -1
- package/dist/telemetry/semantic-conventions.js +20 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +4 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +2 -2
- package/dist/workflow/hook.d.ts.map +1 -1
- package/dist/workflow/hook.js +76 -4
- package/docs/api-reference/create-hook.mdx +77 -0
- package/docs/api-reference/create-webhook.mdx +1 -1
- package/docs/api-reference/define-hook.mdx +2 -0
- package/docs/foundations/errors-and-retries.mdx +3 -1
- package/docs/foundations/hooks.mdx +34 -0
- package/docs/foundations/idempotency.mdx +44 -21
- package/docs/foundations/starting-workflows.mdx +2 -0
- package/docs/how-it-works/event-sourcing.mdx +7 -1
- package/package.json +5 -5
|
@@ -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.**
|
|
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
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
|
@@ -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.
|
|
3
|
+
"version": "5.0.0-beta.57",
|
|
4
4
|
"description": "Core runtime and engine for Workflow SDK",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -89,12 +89,12 @@
|
|
|
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.
|
|
92
|
+
"@workflow/errors": "5.0.0-beta.24",
|
|
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.
|
|
96
|
-
"@workflow/world-local": "5.0.0-beta.
|
|
97
|
-
"@workflow/world-vercel": "5.0.0-beta.
|
|
95
|
+
"@workflow/world": "5.0.0-beta.39",
|
|
96
|
+
"@workflow/world-local": "5.0.0-beta.48",
|
|
97
|
+
"@workflow/world-vercel": "5.0.0-beta.52",
|
|
98
98
|
"debug": "4.4.3",
|
|
99
99
|
"devalue": "5.9.2",
|
|
100
100
|
"ms": "2.1.3",
|