workflow 5.7.2 → 5.8.1

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 (32) hide show
  1. package/dist/internal/errors.d.ts +1 -1
  2. package/dist/internal/errors.js +2 -2
  3. package/docs/advanced/quickjs-runtime.mdx +4 -4
  4. package/docs/ai/sleep-and-delays.mdx +1 -1
  5. package/docs/api-reference/workflow/fetch.mdx +1 -1
  6. package/docs/api-reference/workflow/index.mdx +3 -0
  7. package/docs/api-reference/workflow/rate-limit.mdx +114 -0
  8. package/docs/api-reference/workflow-errors/in-band-superseded-error.mdx +5 -1
  9. package/docs/api-reference/workflow-errors/precondition-failed-error.mdx +6 -4
  10. package/docs/api-reference/workflow-errors/throttle-error.mdx +1 -1
  11. package/docs/api-reference/workflow-runtime/world/queue.mdx +5 -1
  12. package/docs/comparisons/workflow-sdk-vs-aws-step-functions.mdx +1 -1
  13. package/docs/comparisons/workflow-sdk-vs-inngest.mdx +49 -5
  14. package/docs/comparisons/workflow-sdk-vs-temporal.mdx +1 -0
  15. package/docs/comparisons/workflow-sdk-vs-trigger-dev.mdx +3 -1
  16. package/docs/configuration/runtime-tuning.mdx +478 -168
  17. package/docs/cookbook/common-patterns/batching.mdx +2 -2
  18. package/docs/cookbook/common-patterns/rate-limiting.mdx +27 -2
  19. package/docs/cookbook/index.mdx +1 -1
  20. package/docs/cookbook/integrations/chat-sdk.mdx +1 -1
  21. package/docs/errors/max-events-exceeded.mdx +1 -0
  22. package/docs/errors/rate-limit-max-wait.mdx +31 -0
  23. package/docs/foundations/errors-and-retries.mdx +2 -0
  24. package/docs/foundations/meta.json +1 -0
  25. package/docs/foundations/rate-limits.mdx +181 -0
  26. package/docs/performance.mdx +93 -59
  27. package/docs/worlds/building-a-world.mdx +6 -3
  28. package/docs/worlds/local.mdx +6 -2
  29. package/docs/worlds/postgres.mdx +9 -3
  30. package/docs/worlds/upgrading-to-v5.mdx +2 -2
  31. package/docs/worlds/vercel.mdx +2 -2
  32. package/package.json +11 -11
@@ -274,7 +274,7 @@ The runtime handles a rejection wherever it arrives. It restarts the replay in-p
274
274
 
275
275
  The in-band writer fence keeps a run to one orchestrator writer when two orchestrator invocations of the same run overlap, for example a delivery that outlives its queue lease while a redelivery of the run is already running. A write made by the run's orchestrator is **in-band** (`params.inBand: true`). Everything else is **out-of-band**: a background step's own events, `hook_received` from `resumeHook()` and webhooks, `run_cancelled` from `cancel()`, and `attr_set` from `setAttributes()` outside the workflow.
276
276
 
277
- The fence is optional. The runtime sends `inBand` only on runs of the experimental spec 9 runtime, so a World without the fence stays fully supported and keeps creating spec 8 runs. Declare `capabilities.inBandFence: true` when you implement all of the following:
277
+ The fence is optional. The runtime sends `inBand` only on spec 9 runs, so a World without the fence stays fully supported and keeps creating spec 8 runs. Declare `capabilities.inBandFence: true` when you implement all of the following:
278
278
 
279
279
  - **Two counters per run.** Next to whatever allocates event positions, keep `seq`, the highest position allocated to any write, and `seqInBand`, the number of positions allocated to in-band writes. `run_created` counts as in-band and is never fenced itself, so a new run holds `seq = 1, seqInBand = 1` (`IN_BAND_SEQ_AT_RUN_CREATION` from `@workflow/world`).
280
280
  - **List snapshot, or counters on a refusal.** On every `events.list()` page of a slot-numbered run, return `snapshot: { seq, seqInBand }`, read before the listing. A full load, followed to `hasMore: false`, covers every position up to the first page's `seq`. A fenced writer takes its count from that first page, never from counting events, because a count cannot tell which positions were in-band. If your store cannot read the counters on a list, you may list no snapshot, as long as every refusal carries both counters, read together (see below).
@@ -282,6 +282,7 @@ The fence is optional. The runtime sends `inBand` only on runs of the experiment
282
282
  - **Fence first.** Decide the fence before anything else the write does. A stale writer must touch no entity row. If the write updates entity state as well as appending, make the check, the update and the append one atomic operation (in Postgres, one transaction that locks the run's counter row first).
283
283
  - **Missing count.** Answer an in-band write without `expectedSeqInBand` with a 400 (`WorkflowWorldError` with `status: 400`).
284
284
  - **Out-of-band creates** (`inBand: false` or absent) allocate unconditionally, move `seq`, and never move `seqInBand`. The fence never refuses them.
285
+ - **Out-of-band `wait_completed`.** On a run stamped at spec version 9 or later, refuse an out-of-band `wait_completed` with a 409 (`WorkflowWorldError` with `status: 409`, `code: 'wait-completion-in-band-only'`) and allocate nothing. The orchestrator runs ahead past a wait that is not about to come due, which is only sound if it is the one that completes it.
285
286
 
286
287
  `world-local`, `world-postgres` and `world-vercel` declare the fence. `world-vercel`'s backend fences in-band writes on every run with an event sequencer (spec version 7 and later). On an older run it answers an in-band write with a 400 (`WorkflowWorldError`, `code: 'in-band-fence-unsupported'`) rather than writing it unfenced, so a writer decides whether to fence from the run's spec version. Its list responses carry the snapshot only where the backend reports one, so a writer there can find none.
287
288
 
@@ -759,6 +760,8 @@ Your World will then appear on the [Worlds ecosystem](/worlds) page with its com
759
760
  }
760
761
  ```
761
762
 
762
- ## Supporting spec 9 runs (experimental)
763
+ ## Supporting spec 9 runs
763
764
 
764
- Everything above is the contract for spec 8 runs, which every World needs. A World may also let its users opt in to the experimental spec 9 runtime (`WORKFLOW_SPEC_VERSION=9`). To do so it implements the [in-band writer fence](#optional-the-in-band-writer-fence), serializes each spec 9 run's orchestrator deliveries, follows the spec 9 queue and deduplication rules, declares `capabilities.inBandFence: true`, and passes it on as `mintedSpecVersion(process.env, { inBandFence: true })`. `mintedSpecVersion()` called with no options never returns 9, so a World that does none of this keeps creating spec 8 runs.
765
+ Everything above is the contract for spec 8 runs, which every World needs. A World may also support spec 9 runs, which it then uses for new runs by default (`WORKFLOW_SPEC_VERSION=8` opts its users out). The Vercel, Local and Postgres Worlds support them, so the rest of the documentation describes spec 9 behavior, and a note marks each place where a spec 8 run behaves differently. To do so it implements the [in-band writer fence](#optional-the-in-band-writer-fence), serializes each spec 9 run's orchestrator deliveries, follows the spec 9 queue and deduplication rules, declares `capabilities.inBandFence: true`, and passes it on as `mintedSpecVersion(process.env, { inBandFence: true })`. `mintedSpecVersion()` called with no options never returns 9, so a World that does none of this keeps creating spec 8 runs.
766
+
767
+ A World that also reserves rate limit slots for [`experimental_rateLimit()`](/docs/api-reference/workflow/rate-limit) declares `capabilities.rateLimits: true` and passes `mintedSpecVersion(process.env, { inBandFence: true, rateLimits: true })`, which makes spec 10 its default instead. A spec 10 run is a spec 9 run that may hold rate events, so everything in this section applies to it. `WorldCapabilities.rateLimits` in `@workflow/world` documents the rate events such a World decides.
@@ -147,9 +147,13 @@ Whether pending and running runs found in the data directory are re-enqueued whe
147
147
 
148
148
  Change it when: several processes or workers run workflows on one data directory. Turn recovery off, or give each one its own `tag` (see [Programmatic configuration](#programmatic-configuration)).
149
149
 
150
- The Local World's queue is in memory, so a restart loses the messages runs had queued. Recovery re-enqueues each run, and that delivery re-arms the run's wait timers (a `sleep()` or a timeout): a spec 8 run suspends on its open waits again, and the recovery marker on the message makes a single-orchestrator (spec 9) delivery send the run's step messages and wait timers again. As a backstop, recovery also sends each recovered run a wake for every open wait due within about 24.8 days (the queue's longest timer), due when the wait is.
150
+ The Local World's queue is in memory, so a restart loses the messages runs had queued. Recovery re-enqueues each run, and the recovery marker on the message makes that delivery send the run's step messages and wait timers (a `sleep()` or a timeout) again. As a backstop, recovery also sends each recovered run a wake for every open wait due within about 24.8 days (the queue's longest timer), due when the wait is.
151
151
 
152
- Recovery also hands back to the queue any step that was running inline when the previous process exited. The step runs again right away instead of after the inline-ownership lease (about 14 minutes), and the interrupted attempt counts toward its retries. A step with `maxRetries: 0` that was interrupted mid-body therefore fails on restart with an "exceeded max retries" error instead of running again once the lease expires, as it already does after a crash on the Vercel World.
152
+ A step that was running inline when the previous process exited runs again on the recovered delivery, and the interrupted attempt counts toward its retries. A step with `maxRetries: 0` that was interrupted mid-body therefore fails instead of running again.
153
+
154
+ <Spec8Note>
155
+ the recovered delivery re-arms the run's wait timers by suspending on its open waits again. Recovery also hands back to the queue any step that was running inline when the previous process exited, so the step runs again right away instead of after the inline-ownership lease (about 14 minutes). A step with `maxRetries: 0` that was interrupted mid-body fails on restart with an "exceeded max retries" error.
156
+ </Spec8Note>
153
157
 
154
158
  If you turn recovery off and re-enqueue runs yourself, call `releaseOrphanedSteps(runId)` before re-enqueueing each run. It is a method of `LocalWorld`, the type `createWorld()` from `@workflow/world-local` returns. The `World` type that `getWorld()` returns does not declare it, so a caller holding a `World` needs the `LocalWorld` type to call it.
155
159
 
@@ -312,7 +312,7 @@ The defaults work for a single process on a local database, so most apps set onl
312
312
  | [`WORKFLOW_POSTGRES_SKIP_WORKERS`](#workflow_postgres_skip_workers) | off | Send work without executing it |
313
313
  | [`WORKFLOW_POSTGRES_HEADERS_TIMEOUT_MS`](#workflow_postgres_headers_timeout_ms) | `0` ms (no deadline) | Hung delivery detection |
314
314
  | [`WORKFLOW_POSTGRES_BODY_TIMEOUT_MS`](#workflow_postgres_body_timeout_ms) | `0` ms (no deadline) | Stalled response detection |
315
- | [`WORKFLOW_POSTGRES_JOB_LOCK_STALE_SECONDS`](#workflow_postgres_job_lock_stale_seconds) | unset: 300 s for spec 9 run-lease jobs only | Redelivery after a process dies |
315
+ | [`WORKFLOW_POSTGRES_JOB_LOCK_STALE_SECONDS`](#workflow_postgres_job_lock_stale_seconds) | unset: 300 s for run-lease jobs only | Redelivery after a process dies |
316
316
  | [`WORKFLOW_POSTGRES_HOOK_RETENTION_LIMIT_DAYS`](#workflow_postgres_hook_retention_limit_days) | `30` days | Maximum hook retention |
317
317
  | [`WORKFLOW_QUEUE_NAMESPACE`](#workflow_queue_namespace) | unset | Queue topic namespace |
318
318
  | [`WORKFLOW_STREAM_FLUSH_INTERVAL_MS`](#workflow_stream_flush_interval_ms) | `0` ms | Stream write batching |
@@ -343,6 +343,8 @@ Change it when: more than one application shares the database. Give each one its
343
343
 
344
344
  A World claims jobs only under its own prefix and stamps that prefix on every run it creates. On `start()`, it re-enqueues only the active runs stamped with its prefix, so restarting one application does not hand another application's runs to its worker. `WORKFLOW_QUEUE_NAMESPACE` does not separate applications that share a database.
345
345
 
346
+ The prefix also scopes [`experimental_rateLimit()`](/docs/api-reference/workflow/rate-limit) keys: every deployment of an application shares a key's rate, and applications with different prefixes do not.
347
+
346
348
  Set the prefix through `WORKFLOW_POSTGRES_JOB_PREFIX` rather than only in code. The CLI and the observability UI build their World from the environment, so a run they create, such as a re-run, is stamped with the prefix they see, and only a World with that prefix runs and recovers it.
347
349
 
348
350
  ### `WORKFLOW_POSTGRES_DEPLOYMENT_ID`
@@ -444,7 +446,11 @@ Change it when: more than one process shares the database, once every one of the
444
446
 
445
447
  A running delivery renews its lock every quarter of the window, and at least every 10 seconds. When a process dies mid-delivery, another process redelivers its jobs within about one and a half windows (about 45 seconds at `30`), instead of after Graphile Worker's fixed 4 hour stale-lock reset or the next restart.
446
448
 
447
- Unset, every delivery still renews its lock, and its worker registers as renewing in `workflow.workflow_job_lock_holders` (migration 0030, which `bootstrap` applies). Then only the jobs holding a spec 9 run's delivery lease (its orchestrator deliveries, and invoke mode's executor lane; migration 0031) are released, after 300 seconds unrenewed, and only those whose holder registered. A spec 9 run whose worker was killed therefore continues within about 8 minutes (one and a half windows) instead of 4 hours. A job held by a process on an earlier version, which does not register, is never released this way, so the default is safe during a rolling upgrade. `0` turns renewal and release off.
449
+ Unset, every delivery still renews its lock, and its worker registers as renewing in `workflow.workflow_job_lock_holders` (migration 0030, which `bootstrap` applies). Then only the jobs holding a run's delivery lease (its orchestrator deliveries, and invoke mode's executor lane; migration 0031) are released, after 300 seconds unrenewed, and only those whose holder registered. A run whose worker was killed therefore continues within about 8 minutes (one and a half windows) instead of 4 hours. A job held by a process on an earlier version, which does not register, is never released this way, so the default is safe during a rolling upgrade. `0` turns renewal and release off.
450
+
451
+ <Spec8Note>
452
+ a run's jobs hold no delivery lease, so unset releases none of them: a job a killed worker held waits for Graphile Worker's 4 hour reset or the next restart. Set this option to have them released.
453
+ </Spec8Note>
448
454
 
449
455
  Keep the value well above the longest a process can go without running its timers (synchronous work that blocks the event loop) or getting a pool connection. A delivery whose renewals stall past the window is redelivered while it still runs.
450
456
 
@@ -593,7 +599,7 @@ The Postgres World uses PostgreSQL as a durable backend:
593
599
 
594
600
  - **Storage**: Workflow runs, events, steps, and hooks are stored in PostgreSQL tables.
595
601
  - **Job queue**: [graphile-worker](https://github.com/graphile/worker) handles reliable job processing with retries.
596
- - **Crash recovery**: The next `start()` re-enqueues active runs, except a run parked on a hook or a wait that is not yet due with no wake left to deliver and no delivery in progress. A run's recovery job is keyed by the run, so repeated boots do not stack duplicates. A running delivery also renews its job lock. If its process dies, any other process redelivers a spec 9 run's orchestrator job once its lock has gone unrenewed for 300 seconds, and with [`WORKFLOW_POSTGRES_JOB_LOCK_STALE_SECONDS`](#workflow_postgres_job_lock_stale_seconds) set, any job of this World once its lock has gone unrenewed for that many seconds.
602
+ - **Crash recovery**: The next `start()` re-enqueues active runs, except a run parked on a hook or a wait that is not yet due with no wake left to deliver and no delivery in progress. A run's recovery job is keyed by the run, so repeated boots do not stack duplicates. A running delivery also renews its job lock. If its process dies, any other process redelivers a run's orchestrator job once its lock has gone unrenewed for 300 seconds, and with [`WORKFLOW_POSTGRES_JOB_LOCK_STALE_SECONDS`](#workflow_postgres_job_lock_stale_seconds) set, any job of this World once its lock has gone unrenewed for that many seconds.
597
603
  - **Streaming**: PostgreSQL NOTIFY/LISTEN enables real-time event distribution. If the database drops the `LISTEN` connection, the World reconnects every second and re-reads persisted chunks for open stream readers, so readers don't miss chunks written during the outage.
598
604
 
599
605
  This architecture ensures workflows survive application restarts with all state reliably persisted. For implementation details, see the [source code](https://github.com/vercel/workflow/tree/main/packages/world-postgres).
@@ -185,6 +185,6 @@ The cases worth covering explicitly:
185
185
 
186
186
  The first-party implementations in `packages/world-local` and `packages/world-postgres` are the reference for everything else, and their test suites are the closest thing to full conformance while the compatibility tests are being rebuilt.
187
187
 
188
- ## Spec 9 (experimental)
188
+ ## Spec 9
189
189
 
190
- A World does not need to implement anything for the experimental spec 9 runtime to migrate to v5: `mintedSpecVersion()` with no options keeps creating spec 8 runs. A World that opts in must tell its users to upgrade every deployment and process that shares it before they set `WORKFLOW_SPEC_VERSION=9`, and not to roll back below that release once spec 9 runs exist.
190
+ A World does not need to implement anything for spec 9 to migrate to v5: `mintedSpecVersion()` with no options keeps creating spec 8 runs. A World that opts in creates spec 9 runs by default (spec 10 if it also declares `capabilities.rateLimits`), so it must tell its users to upgrade every deployment and process that shares it together (with `WORKFLOW_SPEC_VERSION=8` on the upgraded ones until all of them run that release), and not to roll back below that release once spec 9 or 10 runs exist.
@@ -626,11 +626,11 @@ For more details, see the [Vercel Workflow documentation](https://vercel.com/doc
626
626
 
627
627
  With [`WORKFLOW_STREAMS_TRANSPORT`](#workflow_streams_transport) at its default, the client only offers the upgrade. The server accepts or declines each one, and a writer whose upgrade is declined or fails writes over HTTP for the rest of its lifetime. This setting does not decide which tenants may upgrade and does not infer server support from package versions. The protocol route (`/websockets/v1`) is versioned independently from REST v2/v4 and persisted workflow `specVersion` values.
628
628
 
629
- A writer's first write group always goes over HTTP, and so does its second. The socket starts opening in the background once the second group's request is sent, and later groups keep going over HTTP until it opens, so no write waits for the upgrade. A writer that closes after one or two groups never uses the socket. If the socket has not opened within 10 seconds, or the upgrade fails or is declined, the writer stays on HTTP.
629
+ A writer that starts without a socket sends its first and second write groups over HTTP. The socket starts opening in the background once the second group's request is sent, and later groups keep going over HTTP until it opens, so no write waits for the upgrade. If the socket has not opened within 10 seconds, or the upgrade fails or is declined, the writer stays on HTTP.
630
630
 
631
631
  Writes and close requests execute serially, so a write never overtakes an earlier one on the other transport. After the socket accepts a write, a missing acknowledgement has an unknown outcome: the writer fails rather than replaying the write over HTTP and risking a duplicate append. A throttled (429) write or close did not apply, so the writer retries it after the server's `Retry-After`, as the HTTP writer does, on the socket if it stays open (or its replacement, after a drain) and otherwise over HTTP. Past 30 seconds of cumulative wait, the write moves to HTTP, whose own 429 retry applies. A close that fails with a 5xx is retried over HTTP, since close is idempotent.
632
632
 
633
- Once a released writer's writes drain, its socket closes without closing the shared stream, and later writes through that same handle go over HTTP. Writers acquired with `getWritable()` inside a step release their socket when the step completes. External `getRun(runId).getWritable()` handles release it on the first `releaseLock()` after pending writes drain, so hold the writer lock for the whole burst to keep writing over the socket.
633
+ Once a released writer's writes drain, it hands its socket, without closing the shared stream, to the next writer of the same run and stream in the same function instance, and later writes through the released handle go over HTTP. The next writer sends its writes, starting with its first group, over that socket, so consecutive steps that write to one stream pay for one handshake. A writer whose first operation is a close sends it over HTTP. A socket that is still opening when its writer is released is handed on once it opens. Only one writer uses a socket at a time. An idle socket keeps its function invocation open and closes after 2 seconds, or as soon as the server drains or closes it. The server attributes a connection's requests to the writer that opened it, so on a reused socket server-side records show the earlier writer's id. Writers acquired with `getWritable()` inside a step release their socket when the step completes. External `getRun(runId).getWritable()` handles release it on the first `releaseLock()` after pending writes drain, so hold the writer lock for the whole burst to keep writing over the socket.
634
634
 
635
635
  Before routine authentication expiry or server max duration, the server sends a v1 `drain` control. The client stops sending, lets an already-admitted request receive its reply, and reconnects after the server closes with code 1001. If the new socket is not open within 250 ms, the writer continues over HTTP. Authentication drains re-resolve a fresh bearer. A request that was sent but receives no reply before close still has an unknown outcome and is never replayed.
636
636
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "workflow",
3
- "version": "5.7.2",
3
+ "version": "5.8.1",
4
4
  "description": "Workflow SDK - Build durable, resilient, and observable workflows",
5
5
  "main": "dist/typescript-plugin.cjs",
6
6
  "type": "module",
@@ -60,19 +60,19 @@
60
60
  }
61
61
  },
62
62
  "dependencies": {
63
- "@workflow/astro": "5.0.14",
64
- "@workflow/cli": "5.3.2",
65
- "@workflow/core": "5.7.2",
66
- "@workflow/errors": "5.1.6",
63
+ "@workflow/astro": "5.0.16",
64
+ "@workflow/cli": "5.3.4",
65
+ "@workflow/core": "5.8.1",
66
+ "@workflow/errors": "5.2.1",
67
67
  "@workflow/typescript-plugin": "5.0.1",
68
68
  "@workflow/utils": "5.1.1",
69
69
  "ms": "2.1.3",
70
- "@workflow/next": "5.1.5",
71
- "@workflow/nest": "5.1.12",
72
- "@workflow/nitro": "5.0.14",
73
- "@workflow/nuxt": "5.0.14",
74
- "@workflow/sveltekit": "5.0.14",
75
- "@workflow/rollup": "5.0.14"
70
+ "@workflow/next": "5.1.7",
71
+ "@workflow/nest": "5.1.14",
72
+ "@workflow/nitro": "5.0.16",
73
+ "@workflow/nuxt": "5.0.16",
74
+ "@workflow/sveltekit": "5.0.16",
75
+ "@workflow/rollup": "5.0.16"
76
76
  },
77
77
  "devDependencies": {
78
78
  "@types/ms": "2.1.0",