-
Notifications
You must be signed in to change notification settings - Fork 115
feat(core): create/load distinction mechanism for snapshot restoration (FEP-2548) #723
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
47 commits
Select commit
Hold shift + click to select a range
71814a8
add context
ENvironmentSet 33e0805
docs: add FEP-2548 mechanism design run plan, extend domain glossary
ENvironmentSet 16e6a5c
docs: add FEP-2548 init/load distinction mechanism design
ENvironmentSet fb20c55
docs: revise FEP-2548 mechanism — drop dedicated init-interception ho…
ENvironmentSet 0006fb3
add impl plan
ENvironmentSet a353f51
opus 4.8
ENvironmentSet 3100588
test(core): add create/load mechanism harness + §3 type-signature stubs
ENvironmentSet d83506e
feat(core): implement create/load distinction mechanism
ENvironmentSet fc9fbce
fix(core): allow undefined/void return from provideSnapshot (C1#8)
ENvironmentSet 66705fe
docs(changeset): fix hook signature typos, cause.kind notation, and s…
ENvironmentSet 86351e2
test(core): pin raw propagation when provideSnapshot/onLoadError thro…
ENvironmentSet ac5c7ba
test(core): verify recover:create resumes the full create pipeline (C…
ENvironmentSet 2268a24
test(core): pin load contract edges — non-create recovery rethrows, d…
ENvironmentSet 62cef36
test(core): cover pop/step navigation after load (C2#3)
ENvironmentSet 382a209
test(core): load postcondition for a snapshot captured while paused (…
ENvironmentSet ab22fe4
chore(core): trim HOW-restating comments to WHY-only (C1#11)
ENvironmentSet 6bb72db
fix(core): rebase snapshot events into the window after static events…
ENvironmentSet 8cc091b
plans
ENvironmentSet b4be141
refactor(core): rename SnapshotLoadError cause kinds to read as the s…
ENvironmentSet 1a74f70
refactor(core): promote onInit's initializedBy string to an initInfo …
ENvironmentSet c0f3d32
feat(core): run the overrideInitialEvents chain on the load path with…
ENvironmentSet 1ff0b48
refactor(core): drop the dead setPrototypeOf workaround in SnapshotLo…
ENvironmentSet 7929873
refactor(core): fold the load registration check into validateEvents …
ENvironmentSet 7f2c7b2
refactor(core): rebase static events with the navigation events and d…
ENvironmentSet c382340
fix(core): capture only committed navigation in captureSnapshot (revi…
ENvironmentSet c03e3ee
fix(core): drop a whole uncommitted activity's events when capturing,…
ENvironmentSet 4c11f32
docs(core): note the unified registration check now covers Replaced (…
ENvironmentSet 6068107
fix(core): capture every recorded navigation event, excluding only un…
ENvironmentSet bd58f04
feat(core): carry every runtime event in snapshots, excluding only st…
ENvironmentSet 1c58ead
feat(core): replay snapshot events at their recorded dates, backdatin…
ENvironmentSet 5dbf3d7
refactor(core): type SNAPSHOT_EVENT_NAMES members as SnapshotEvent na…
ENvironmentSet ec02f6b
fix(core): carry the raw replay error as incompatible-events detail, …
ENvironmentSet 135f2c7
refactor(core): drop needless optional chaining on snapshot.$schema (…
ENvironmentSet 88f41c5
refactor(core)!: rename StackflowPluginHook to StackflowPluginInitHoo…
ENvironmentSet 5e0b1ba
refactor(core)!: narrow provideSnapshot return to StackSnapshot | nul…
ENvironmentSet 7e45c37
refactor(core)!: make onLoadError choose recover/propagate via an exp…
ENvironmentSet d3efb15
refactor(core): split initial events by isSnapshotEvent (non-static/s…
ENvironmentSet 79ddc00
refactor(core)!: rename onInitialActivityIgnored's stale initialPushe…
ENvironmentSet 9653237
refactor(core): stop normalizing in captureSnapshot; export the log a…
ENvironmentSet f15daac
style(core): apply biome formatting to isSnapshotEvent/StackflowPlugi…
ENvironmentSet 4d4532d
docs(core): fix two spec comments that still claimed capture normaliz…
ENvironmentSet 3c784df
docs(core): describe provideSnapshot/onLoadError as new additive hook…
ENvironmentSet 382fb08
Merge branch 'main' into feature/fep-2548
ENvironmentSet aef6460
delete docs
ENvironmentSet 69646d9
test(core): extract FEP-2548 test suites into a stacked PR
ENvironmentSet fd12207
docs(core): condense FEP-2548 changeset to the change surface only
ENvironmentSet e8cb83d
docs(core): name the added APIs and broken types in the FEP-2548 chan…
ENvironmentSet 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,17 @@ | ||
| --- | ||
| "@stackflow/core": major | ||
| --- | ||
|
|
||
| Distinguish how a stack is initialized — created fresh (`create`) or restored from a snapshot (`load`) — and add the surface a snapshot round-trip needs: `StackSnapshot`, `actions.captureSnapshot()`, the `provideSnapshot` / `onLoadError` plugin hooks, and `initInfo` on `onInit`. Runtime behavior is unchanged when no snapshot is provided; the breaks are type-level only — the `StackflowPluginHook` → `StackflowPluginInitHook` rename, plus new required members on `StackflowActions` and on the `onInit` / `overrideInitialEvents` hook signatures — hence the major. | ||
|
|
||
| **Added** | ||
|
|
||
| - `StackSnapshot` (with the `SnapshotEvent` / `NavigationEvent` unions) — a plain-data record of the events a stack recorded at runtime — plus `SnapshotLoadError`. | ||
| - `actions.captureSnapshot()` to export that record, and the `provideSnapshot` / `onLoadError` plugin hooks to supply a snapshot at creation time and route a failed load. | ||
| - `initInfo: { kind: "create" | "load" }` on `onInit`; `overrideInitialEvents` now runs on the load path too and receives the same `initInfo`. | ||
|
|
||
| **Changed** | ||
|
|
||
| - Renamed the `StackflowPluginHook` type to `StackflowPluginInitHook`. | ||
| - `StackflowActions` now requires `captureSnapshot`, the `onInit` / `overrideInitialEvents` hook arguments now require `initInfo`, and `overrideInitialEvents`' parameter widens from `(PushedEvent | StepPushedEvent)[]` to `SnapshotEvent[]` — hand-written mocks and wrap-and-forward plugins must adopt these. | ||
| - `validateEvents` now also rejects a `Replaced` that names an unregistered activity (does not fire in config-first usage). |
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,39 @@ | ||
| /** | ||
| * Why a snapshot load failed, in three kinds that read as the | ||
| * snapshot → events → stack pipeline: | ||
| * - `unrecognized-snapshot`: the value is not a snapshot structure core | ||
| * recognizes — a catch-all over the structural checks (`$schema` mismatch, | ||
| * `events` not being an array, or an item that is not one of the six | ||
| * navigation events, including a missing `id`/`name`). `detail` names the | ||
| * check that failed. | ||
| * - `incompatible-events`: the structure is recognized but the event sequence | ||
| * is incompatible with the current config (e.g. it materializes an | ||
| * unregistered activity) — a relational failure against the config, not a | ||
| * defect intrinsic to the events. `detail` is the raw error thrown by the | ||
| * replay machinery (`aggregate`/`validateEvents`), carried unflattened — | ||
| * hence `unknown`. Narrowing it to the offending events would drop the | ||
| * thrown error's call stack, which is the useful diagnostic here. | ||
| * - `empty-stack`: replay succeeded but left zero activities in an enter | ||
| * state, so there is nothing to show. Note the condition is "zero | ||
| * enter-state activities", not an empty `activities` array — exit-done | ||
| * activities may remain. | ||
| */ | ||
| export type SnapshotLoadErrorCause = | ||
| | { kind: "unrecognized-snapshot"; detail: string } | ||
| | { kind: "incompatible-events"; detail: unknown } | ||
| | { kind: "empty-stack" }; | ||
|
|
||
| /** | ||
| * Thrown when loading a provided snapshot fails. Routed to the providing | ||
| * plugin's `onLoadError` first (R5); if unrecovered, thrown out of | ||
| * `makeCoreStore` (R4). | ||
| */ | ||
| export class SnapshotLoadError extends Error { | ||
| cause: SnapshotLoadErrorCause; | ||
|
|
||
| constructor(cause: SnapshotLoadErrorCause, message?: string) { | ||
| super(message ?? `failed to load snapshot: ${cause.kind}`); | ||
| this.name = "SnapshotLoadError"; | ||
| this.cause = cause; | ||
| } | ||
| } | ||
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,53 @@ | ||
| import type { | ||
| PausedEvent, | ||
| PoppedEvent, | ||
| PushedEvent, | ||
| ReplacedEvent, | ||
| ResumedEvent, | ||
| StepPoppedEvent, | ||
| StepPushedEvent, | ||
| StepReplacedEvent, | ||
| } from "./event-types"; | ||
|
|
||
| /** | ||
| * The six navigation events — a subset union of the existing domain event | ||
| * types (no new vocabulary is introduced). | ||
| */ | ||
| export type NavigationEvent = | ||
| | PushedEvent | ||
| | ReplacedEvent | ||
| | PoppedEvent | ||
| | StepPushedEvent | ||
| | StepReplacedEvent | ||
| | StepPoppedEvent; | ||
|
|
||
| /** | ||
| * The events a snapshot carries: every domain event except the static ones | ||
| * (`Initialized`, `ActivityRegistered`). Statics are config/source-grade | ||
| * information — they may legitimately differ after a reload, so the current | ||
| * config re-derives them at load time instead of trusting the snapshot. | ||
| * Everything the stack recorded at runtime, `Paused`/`Resumed` included, is | ||
| * exported as-is. | ||
| */ | ||
| export type SnapshotEvent = NavigationEvent | PausedEvent | ResumedEvent; | ||
|
|
||
| /** | ||
| * A plain-data value whose structure is owned by core. Encoding to a | ||
| * persistence medium (codec) is the consumer's responsibility. | ||
| */ | ||
| export type StackSnapshot = { | ||
| /** | ||
| * Structural discriminator tag. A mismatch fails the load as | ||
| * `SnapshotLoadError` — version migration is a non-goal. | ||
| */ | ||
| $schema: "stackflow.snapshot.v1"; | ||
|
|
||
| /** | ||
| * The event log as recorded, minus the static events the current config | ||
| * re-derives at load time. Left in recorded order — load replays through | ||
| * `aggregate`, which sorts by eventDate and dedupes by id. Whether to | ||
| * capture a paused stack is the caller's choice — core exports the stack it | ||
| * is asked about, pause state and all. | ||
| */ | ||
| events: SnapshotEvent[]; | ||
| }; |
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export * from "./dispatchEvent"; | ||
| export * from "./filterEvents"; | ||
| export * from "./isSnapshotEvent"; | ||
| export * from "./makeEvent"; | ||
| export * from "./validateEvents"; |
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,36 @@ | ||
| import type { DomainEvent } from "../event-types"; | ||
| import type { SnapshotEvent } from "../StackSnapshot"; | ||
|
|
||
| /** | ||
| * The events a snapshot carries — every domain event except the static ones | ||
| * (`Initialized`, `ActivityRegistered`), which the current config re-derives | ||
| * at load time. Single source of truth for the capture-side filter and the | ||
| * load-side structure check. | ||
| * | ||
| * Its members are constrained to `SnapshotEvent["name"]` at construction (a | ||
| * typo or a non-snapshot name fails to compile), but the set itself is typed | ||
| * `ReadonlySet<string>` so both membership checks below can pass a plain event | ||
| * name — a `DomainEvent["name"]` or a runtime `string` — without a cast. | ||
| */ | ||
| const SNAPSHOT_EVENT_NAMES: ReadonlySet<string> = new Set< | ||
| SnapshotEvent["name"] | ||
| >([ | ||
| "Pushed", | ||
| "Replaced", | ||
| "Popped", | ||
| "StepPushed", | ||
| "StepReplaced", | ||
| "StepPopped", | ||
| "Paused", | ||
| "Resumed", | ||
| ]); | ||
|
|
||
| /** Whether an event is one a snapshot carries (i.e. not a static event). */ | ||
| export function isSnapshotEvent(event: DomainEvent): event is SnapshotEvent { | ||
| return SNAPSHOT_EVENT_NAMES.has(event.name); | ||
| } | ||
|
|
||
| /** Whether a value is the name of an event a snapshot carries. */ | ||
| export function isSnapshotEventName(name: unknown): boolean { | ||
| return typeof name === "string" && SNAPSHOT_EVENT_NAMES.has(name); | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.