-
Notifications
You must be signed in to change notification settings - Fork 3
feat(durably): add @effectionx/durably — durable execution for Effection #171
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
524dd94
feat(durably): add @effectionx/durably package — durable execution fo…
taras b8ef075
feat(durably): add useDurableStream resource for HTTP-backed persiste…
taras 0366e57
chore(durably): remove PLAN.md — implementation is complete
taras d864f79
refactor(durably): replace call/async with until for Operation-native…
taras bb974a4
refactor(durably): simplify error forwarding with action, fix lint er…
taras 637cfb4
test(durably): verify multiple durable streams in one main operation
taras 1f5cdb0
fix(durably): address CodeRabbit review feedback and fix root lifecyc…
taras ec3bea1
refactor(durably): address remaining CodeRabbit review feedback
taras ffa475c
refactor(durably): address CodeRabbit review round 4
taras 87b0e0f
fix(test-adapter): destructure createScope() for effection 4.1 compat…
taras 59526ce
fix(durably): exception-safe effect entry and scopeParents cleanup
taras 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,152 @@ | ||
| # durably | ||
|
|
||
| Record, replay, and resume Effection workflows with durable streams. | ||
|
|
||
| --- | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @effectionx/durably effection | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Wrap any Effection operation with `durably()` to record every effect | ||
| resolution to a durable stream. When resumed with the same stream, stored | ||
| results are replayed without re-executing effects, enabling mid-workflow | ||
| resume after restarts. | ||
|
|
||
| ```ts | ||
| import { main } from "effection"; | ||
| import { durably, InMemoryDurableStream } from "@effectionx/durably"; | ||
| import { sleep } from "effection"; | ||
|
|
||
| let stream = new InMemoryDurableStream(); | ||
|
|
||
| await main(function* () { | ||
| let result = yield* durably(function* () { | ||
| yield* sleep(1000); | ||
| return "hello"; | ||
| }, { stream }); | ||
|
|
||
| console.log(result); // "hello" | ||
| }); | ||
| ``` | ||
|
|
||
| ### Mid-workflow resume | ||
|
|
||
| Pass a stream that already contains recorded events. The workflow replays | ||
| stored results instantly, then continues live from where it left off: | ||
|
|
||
| ```ts | ||
| import { durably, InMemoryDurableStream } from "@effectionx/durably"; | ||
| import { sleep, action } from "effection"; | ||
|
|
||
| // First run — records events to the stream | ||
| let stream = new InMemoryDurableStream(); | ||
|
|
||
| yield* durably(function* () { | ||
| yield* sleep(1000); // recorded | ||
| yield* action(function* (resolve) { | ||
| // ... long-running work that gets interrupted | ||
| }); | ||
| }, { stream }); | ||
|
|
||
| // Second run — replays the sleep instantly, resumes from the action | ||
| yield* durably(function* () { | ||
| yield* sleep(1000); // replayed from stream (instant) | ||
| yield* action(function* (resolve) { | ||
| resolve("done"); // executes live | ||
| }); | ||
| }, { stream }); | ||
| ``` | ||
|
|
||
| ### Divergence detection | ||
|
|
||
| If the workflow code changes between runs, mismatched effects throw a | ||
| `DivergenceError`: | ||
|
|
||
| ```ts | ||
| import { durably, InMemoryDurableStream, DivergenceError } from "@effectionx/durably"; | ||
| import { sleep, action } from "effection"; | ||
|
|
||
| let stream = new InMemoryDurableStream(); | ||
|
|
||
| // First run records sleep(100) | ||
| yield* durably(function* () { | ||
| yield* sleep(100); | ||
| return "v1"; | ||
| }, { stream }); | ||
|
|
||
| // Second run yields action() where sleep(100) was expected | ||
| try { | ||
| yield* durably(function* () { | ||
| yield* action(function* (resolve) { resolve("v2"); }); | ||
| }, { stream }); | ||
| } catch (error) { | ||
| // DivergenceError: expected "sleep(100)" but got "action" | ||
| } | ||
| ``` | ||
|
|
||
| ## How it works | ||
|
|
||
| Effection's architecture routes every effect through a single **Reducer**. | ||
| `durably()` injects a **DurableReducer** that intercepts this point: | ||
|
|
||
| - **Recording**: When a generator yields an effect, the reducer writes | ||
| `effect:yielded` to the stream. When it resolves, `effect:resolved`. | ||
| Scope lifecycle events (`scope:created`, `scope:destroyed`) are also | ||
| recorded. | ||
|
|
||
| - **Replay**: When the stream already has events, the reducer feeds stored | ||
| results back to generators via `iterator.next(storedResult)` without | ||
| calling `effect.enter()`. The generator cannot tell whether it is | ||
| replaying or running live. | ||
|
|
||
| - **Transition**: When stored events run out, the reducer seamlessly | ||
| switches to live execution. All subsequent effects are recorded normally. | ||
|
|
||
| Only user-facing effects (`action`, `sleep`, `spawn`, `resource`, etc.) are | ||
| recorded. Infrastructure effects (`useCoroutine`, `useScope`, context | ||
| mutations) always execute live. | ||
|
|
||
| ## API | ||
|
|
||
| ### `durably(operation, options?)` | ||
|
|
||
| Execute an operation with durable execution semantics. Returns a `Task<T>`. | ||
|
|
||
| - `operation` — a function returning an `Operation<T>` | ||
| - `options.stream` — a `DurableStream` for persistence (defaults to an | ||
| ephemeral `InMemoryDurableStream`) | ||
|
|
||
| ### `InMemoryDurableStream` | ||
|
|
||
| An in-memory implementation of `DurableStream`. Events are stored in an | ||
| array and lost when the process exits. Useful for testing. | ||
|
|
||
| - `append(event)` — add an event to the stream | ||
| - `read()` — return all stored entries | ||
|
|
||
| ### `DurableStream` (interface) | ||
|
|
||
| Implement this interface to provide persistent storage: | ||
|
|
||
| ```ts | ||
| interface DurableStream { | ||
| append(event: DurableEvent): void; | ||
| read(): StreamEntry[]; | ||
| } | ||
| ``` | ||
|
|
||
| ### `DivergenceError` | ||
|
|
||
| Thrown when a replayed effect's description does not match what was | ||
| recorded. Indicates the workflow code has changed between runs. | ||
|
|
||
| ## Requirements | ||
|
|
||
| - Node.js >= 22 | ||
| - Effection ^4 (requires [PR 1127](https://github.com/thefrontside/effection/pull/1127) | ||
| for `effection/experimental` reducer exports) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
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.
🧹 Nitpick | 🔵 Trivial
DurableStreaminterface shows synchronousappendandread, which may limit persistent storage implementations.The interface as documented is synchronous. Real persistent backends (databases, file systems) typically need async I/O. Consider whether
appendshould returnOperation<void>(or at minimumvoid | Promise<void>) andreadshould returnOperation<StreamEntry[]>to support durable storage beyond in-memory. If this is intentional for the initial release, a brief note in the docs explaining the synchronous design choice would help consumers.🤖 Prompt for AI Agents