-
Notifications
You must be signed in to change notification settings - Fork 258
implement event-sourced architecture #621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
dc586f4
perf: implement event-sourced architecture
pranaygp 8fa31fa
Apply suggestions from code review
pranaygp dcdb95d
Improve invalid event log handling in step/hook/wait
pranaygp 4a77d5f
Handle serialized workflow run errors correctly
pranaygp e6af5cf
log error in failing test
pranaygp 70177ff
Handle queue idempotency in vercel world
pranaygp 5fa3080
hotfix for error propogation
pranaygp b5627b7
Fix: Incorrect HTTP status code 409 should be 410 for terminal run st…
vercel[bot] dbedf2e
Fix: The code attempts to pass an unsupported `fatal` property when c…
vercel[bot] d7d4df8
Fix: Code silently skips updating workflowRun if result.run is undefi…
vercel[bot] 8c663c5
Add hook_conflict event type for duplicate token detection
pranaygp 51a466c
Add changeset for hook_conflict events
pranaygp 81bb3f0
Add unit tests for hook_conflict handling
pranaygp cc9f581
Improve hook-conflict.mdx error guide
pranaygp fd7168a
Fix docs validation: add hook-conflict to errors index
pranaygp 2336d33
Fix world-local tests for hook_conflict event behavior
pranaygp 1c1b856
Add specVersion property to World interface for backwards compatibility
pranaygp e6ab07b
Add migration for spec_version column in postgres schema
pranaygp 878f0c2
Add drizzle migration journal and snapshot for spec_version column
pranaygp 6f8f856
Regenerate postgres migration using drizzle-kit
pranaygp e21c1d1
Add backwards compatibility for event-sourced runs
pranaygp f12da37
Refactor spec version from semver strings to integers
pranaygp b7d27a2
Store error as CBOR in postgres world for consistency with input/output
pranaygp 504debd
Use WorkflowRuntimeError and improve run entity handling in core runtime
pranaygp 1ba581a
Add specVersion to Step, Hook, and Event entities
pranaygp 1590f13
Refactor world-local storage into modular files
pranaygp df32886
Remove genversion and World.specVersion property
pranaygp f1478df
Remove genversion from world-local, world-postgres, and world-vercel
pranaygp 6897743
Remove version.ts from .gitignore files
pranaygp 3e2f889
Add legacy/backwards compatibility tests
pranaygp 00f2ab1
Add hook_received support for legacy runs
pranaygp 6b3055e
Fix missing genversion in world-vercel
VaguelySerious 3417d86
Remove deprecated workflow_completed, workflow_failed, and workflow_s…
pranaygp a349701
Add specVersion to EventWithRefsSchema in world-vercel
pranaygp e61d37f
Wire specVersion from client through world backends
pranaygp e43250b
Move specVersion to event object level, propagate to entities
pranaygp ed59415
Update specVersion to be optional in types for backwards compatibility
pranaygp 4e41387
Fix world-vercel queue tests missing VERCEL_DEPLOYMENT_ID setup
pranaygp b7a352a
Add specVersion to all event creation calls in core package
pranaygp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --- | ||
| "@workflow/world": minor | ||
| "@workflow/world-local": patch | ||
| "@workflow/world-postgres": patch | ||
| "@workflow/errors": patch | ||
| --- | ||
|
|
||
| Add backwards compatibility for runs created with different spec versions | ||
|
|
||
| - Add `RunNotSupportedError` for runs requiring newer world versions | ||
| - Add semver-based version comparison utilities | ||
| - Legacy runs (< 4.1): route to legacy handlers | ||
| - `run_cancelled`: skip event storage, directly update run | ||
| - `wait_completed`: store event only (no entity mutation) | ||
| - Unknown legacy events: throw error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@workflow/cli": patch | ||
| --- | ||
|
|
||
| Use `events.create()` for run cancellation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "@workflow/core": patch | ||
| --- | ||
|
|
||
| Runtime uses event-sourced entity creation | ||
|
|
||
| - Suspension handler creates entities via `events.create()` | ||
| - Track `hasCreatedEvent` flag to avoid duplicate event creation on replay | ||
| - Handle `hook_conflict` events during replay to reject duplicate token hooks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| "@workflow/world": minor | ||
| --- | ||
|
|
||
| **BREAKING**: Storage interface is now read-only; all mutations go through `events.create()` | ||
|
|
||
| - Remove `cancel`, `pause`, `resume` from `runs` | ||
| - Remove `create`, `update` from `runs`, `steps`, `hooks` | ||
| - Add run lifecycle events: `run_created`, `run_started`, `run_completed`, `run_failed`, `run_cancelled` | ||
| - Add `step_created` event type | ||
| - Remove `fatal` field from `step_failed` (terminal failure is now implicit) | ||
| - Add `step_retrying` event with error info for retriable failures | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "@workflow/world": patch | ||
| "@workflow/errors": patch | ||
| --- | ||
|
|
||
| Add `hook_conflict` event type for duplicate token detection | ||
|
|
||
| - World returns `hook_conflict` event when `hook_created` uses an existing token | ||
| - Add `HOOK_CONFLICT` error slug |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| "@workflow/world": patch | ||
| "@workflow/world-local": patch | ||
| "@workflow/world-postgres": patch | ||
| "@workflow/web-shared": patch | ||
| --- | ||
|
|
||
| Remove deprecated `workflow_completed`, `workflow_failed`, and `workflow_started` events in favor of `run_completed`, `run_failed`, and `run_started` events. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| "@workflow/world": patch | ||
| "@workflow/world-local": patch | ||
| "@workflow/world-postgres": patch | ||
| "@workflow/world-vercel": patch | ||
| "@workflow/web-shared": patch | ||
| --- | ||
|
|
||
| Add `specVersion` property to World interface | ||
|
|
||
| - All worlds expose `@workflow/world` package version for protocol compatibility | ||
| - Stored in `run_created` event and `WorkflowRun` schema | ||
| - Displayed in observability UI |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "@workflow/world-local": patch | ||
| --- | ||
|
|
||
| Implement event-sourced entity creation in `events.create()` | ||
|
|
||
| - Atomically create run/step/hook entities when processing corresponding events | ||
| - Return `hook_conflict` event when hook token already exists | ||
| - Remove direct entity mutation methods from storage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "@workflow/world-postgres": patch | ||
| --- | ||
|
|
||
| Implement event-sourced entity creation in `events.create()` | ||
|
|
||
| - Atomically create run/step/hook entities when processing corresponding events | ||
| - Return `hook_conflict` event when hook token already exists | ||
| - Add `spec_version` column to runs table |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| "@workflow/world-vercel": patch | ||
| --- | ||
|
|
||
| Route entity mutations through v2 events API | ||
|
|
||
| - `events.create()` calls v2 endpoint for atomic entity creation | ||
| - Remove `cancel`, `pause`, `resume` from storage interface |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| --- | ||
| title: hook-conflict | ||
| --- | ||
|
|
||
| This error occurs when you try to create a hook with a token that is already in use by another active workflow run. Hook tokens must be unique across all running workflows in your project. | ||
|
|
||
| ## Error Message | ||
|
|
||
| ``` | ||
| Hook token conflict: Hook with token <token> already exists for this project | ||
| ``` | ||
|
|
||
| ## Why This Happens | ||
|
|
||
| Hooks use tokens to identify incoming webhook payloads. When you create a hook with `createHook({ token: "my-token" })`, the Workflow runtime reserves that token for your workflow run. If another workflow run is already using that token, a conflict occurs. | ||
|
|
||
| This typically happens when: | ||
|
|
||
| 1. **Two workflows start simultaneously** with the same hardcoded token | ||
| 2. **A previous workflow run is still waiting** for a hook when a new run tries to use the same token | ||
|
|
||
| ## Common Causes | ||
|
|
||
| ### Hardcoded Token Values | ||
|
|
||
| {/* @skip-typecheck: incomplete code sample */} | ||
| ```typescript lineNumbers | ||
| // Error - multiple concurrent runs will conflict | ||
| export async function processPayment() { | ||
| "use workflow"; | ||
|
|
||
| const hook = createHook({ token: "payment-hook" }); // [!code highlight] | ||
| // If another run is already waiting on "payment-hook", this will fail | ||
| const payment = await hook; | ||
| } | ||
| ``` | ||
|
|
||
| **Solution:** Use unique tokens that include the run ID or other unique identifiers. | ||
|
|
||
| ```typescript lineNumbers | ||
| export async function processPayment(orderId: string) { | ||
| "use workflow"; | ||
|
|
||
| // Include unique identifier in token | ||
| const hook = createHook({ token: `payment-${orderId}` }); // [!code highlight] | ||
| const payment = await hook; | ||
| } | ||
| ``` | ||
|
|
||
| ### Omitting the Token (Auto-generated) | ||
|
|
||
| The safest approach is to let the Workflow runtime generate a unique token automatically: | ||
|
|
||
| ```typescript lineNumbers | ||
| export async function processPayment() { | ||
| "use workflow"; | ||
|
|
||
| const hook = createHook(); // Auto-generated unique token // [!code highlight] | ||
| console.log(`Send webhook to token: ${hook.token}`); | ||
| const payment = await hook; | ||
| } | ||
| ``` | ||
|
|
||
| ## Handling Hook Conflicts in Your Workflow | ||
|
|
||
| When a hook conflict occurs, awaiting the hook will throw a `WorkflowRuntimeError`. You can catch this error to handle the conflict gracefully: | ||
|
|
||
| ```typescript lineNumbers | ||
| import { WorkflowRuntimeError } from "@workflow/errors"; | ||
|
|
||
| export async function processPayment(orderId: string) { | ||
| "use workflow"; | ||
|
|
||
| const hook = createHook({ token: `payment-${orderId}` }); | ||
|
|
||
| try { | ||
| const payment = await hook; // [!code highlight] | ||
| return { success: true, payment }; | ||
| } catch (error) { | ||
| if (error instanceof WorkflowRuntimeError && error.slug === "hook-conflict") { // [!code highlight] | ||
| // Another workflow is already processing this order | ||
| return { success: false, reason: "duplicate-processing" }; | ||
| } | ||
| throw error; // Re-throw other errors | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| This pattern is useful when you want to detect and handle duplicate processing attempts instead of letting the workflow fail. | ||
|
|
||
| ## When Hook Tokens Are Released | ||
|
|
||
| Hook tokens are automatically released when: | ||
|
|
||
| - The workflow run **completes** (successfully or with an error) | ||
| - The workflow run is **cancelled** | ||
| - The hook is explicitly **disposed** | ||
|
|
||
| After a workflow completes, its hook tokens become available for reuse by other workflows. | ||
|
|
||
| ## Best Practices | ||
|
|
||
| 1. **Use auto-generated tokens** when possible - they are guaranteed to be unique | ||
| 2. **Include unique identifiers** if you need custom tokens (order ID, user ID, etc.) | ||
| 3. **Avoid reusing the same token** across multiple concurrent workflow runs | ||
| 4. **Consider using webhooks** (`createWebhook`) if you need a fixed, predictable URL that can receive multiple payloads | ||
|
|
||
| ## Related | ||
|
|
||
| - [Hooks](/docs/foundations/hooks) - Learn more about using hooks in workflows | ||
| - [createWebhook](/docs/api-reference/workflow/create-webhook) - Alternative for fixed webhook URLs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: this should cause a minor bump transitive to everything that depends on the
worldpackage