-
Notifications
You must be signed in to change notification settings - Fork 7
feat(local-first): A0.1 perf harness → B0.1 PoC → ADR-0008 → B1.1 shadow sync #140
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
16 commits
Select commit
Hold shift + click to select a range
cfd4203
test(perf): A0.1 large-manuscript performance harness
qnbs 0c3f474
feat(local-first): B0.1 CRDT shadow-doc PoC + decision-gate proofs
qnbs 2ada64b
docs(adr): ADR-0008 — local-first data model (Y.Doc as source of truth)
qnbs e502c06
feat(local-first): B1.1 binding seam — incremental shadow write-through
qnbs 28815f1
feat(local-first): B1.1 wiring — enableLocalFirstSync flag + shadow s…
qnbs 9ff710e
test(local-first): deterministic docPersistence waits + clarify IDB b…
qnbs 87fc1eb
test(local-first): guarantee provider teardown via try/finally (CodeA…
qnbs ea2acbb
fix(local-first): docBinding + shadow-sync review fixes (CodeAnt #140)
qnbs 9ee0e9d
fix(local-first): entity ids order, test isolation, post-await flag g…
qnbs 0fffdac
fix(local-first): full-section verify, fixture goal, test cleanup (Co…
qnbs 3bcc7d1
fix(local-first): private-mode IDB fallback + cold-start init (CodeAn…
qnbs 14c8ff2
test(perf): exact-size fixture generation + constant-size bench edit …
qnbs 86bf96e
fix(local-first): handle y-indexeddb whenSynced async rejection (#140…
qnbs 6c308b3
fix(local-first): verify stale meta + write entities in canonical ord…
qnbs 54857fb
fix(local-first): encryption-aware persistence + harden teardown/cont…
qnbs 715eeb3
fix(local-first): re-check encryption on handle reuse + prove persist…
qnbs 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 |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ dist-ssr | |
| *.local | ||
| coverage/ | ||
| reports/ | ||
| tests/bench/baseline/ | ||
|
|
||
| # Editor directories and files | ||
| .vscode/* | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # ADR 0008 — Local-first data model: Yjs document as source of truth | ||
|
|
||
| - **Status:** Accepted (decision made; migration staged behind a gate) | ||
| - **Date:** 2026-06-15 | ||
| - **Deciders:** Maintainer + Claude Code | ||
| - **Context tags:** architecture, state, local-first, data-model, migration | ||
|
|
||
| ## Context | ||
|
|
||
| StoryCraft is **offline-first** (encrypted IndexedDB + Tauri FS persistence, `redux-undo` history) | ||
| but **not local-first** in the CRDT sense. `services/collaborationService.ts` instantiates an | ||
| *ephemeral* `Y.Doc` only on `connect()` and exposes one `getSharedText('manuscript')` seam; the | ||
| manuscript's source of truth is a plain Redux object (`ProjectData`, `StorySection.content: string`). | ||
| The editor never reads or writes through Yjs — so multi-device sync, offline-merge, and conflict-free | ||
| collaboration are unbuilt, and the Yjs dependency is inert *as a data-model technology*. | ||
|
|
||
| [[0001-state-management-boundaries]] assigns persisted domain data (project, manuscript, characters, | ||
| world…) to Redux Toolkit + `redux-undo`. That was correct for a single-device serialization model, | ||
| but a serialization-shaped POJO is the wrong shape for fine-grained merge: two array/string LWW | ||
| replicas cannot converge a paragraph edited concurrently without losing a side. | ||
|
|
||
| A proof of concept (B0.1, strategic plan §5/§8) validated the alternative before committing: | ||
| `services/localFirst/projectDoc.ts` maps `ProjectData ↔ Y.Doc` and `tests/unit/localFirst/projectDoc.test.ts` | ||
| proves round-trip fidelity, **char-level concurrent same-section merge keeping both edits**, | ||
| section-level merge, and `Y.UndoManager` undo/redo. The gate passed. | ||
|
|
||
| ## Decision | ||
|
|
||
| **One `Y.Doc` per project becomes the canonical store; Redux is demoted to a derived read-model.** | ||
|
|
||
| Schema (per project): | ||
|
|
||
| | Domain field | Yjs type | Notes | | ||
| |---|---|---| | ||
| | `manuscript` | `Y.Array<Y.Map>` | one map per section; order = array order | | ||
| | `StorySection.content` | `Y.Text` | char-level CRDT merge — the headline win | | ||
| | other section scalars | `Y.Map` keys | LWW per key | | ||
| | `characters`, `worlds` | `Y.Map<id, entity>` | `EntityState` rebuilt via the RTK adapters on read | | ||
| | everything else (`title`, `logline`, `outline`, goals…) | `meta` `Y.Map` | small, LWW | | ||
| | ephemeral UI (plotBoard viewport, copilot, voice, command palette…) | **stays Redux/Zustand** | **never** CRDT — [[0001-state-management-boundaries]] unchanged | | ||
|
|
||
| Supporting decisions: | ||
|
|
||
| - **Yjs, not Automerge.** Yjs is already vendored, security-reviewed, and transport-integrated (the | ||
| `packages/collab-transport` y-webrtc fork with RTCDataChannel E2E). Switching libraries would | ||
| discard a year of hardening for ergonomic gains that do not change the outcome. | ||
| - **`redux-undo` → `Y.UndoManager` at the flip.** Two undo models cannot both own the same truth; the | ||
| CRDT-native history becomes the single source once a project is on Yjs. | ||
| - **Redux selectors and component reads are unchanged.** A binding (`docBinding`) observes the doc and | ||
| dispatches a single `project/hydrateFromDoc`; the editor writes through Yjs transactions. This | ||
| preserves the entire existing UI and test surface — essential for an indie team. | ||
| - **The flip is gated and per-project, behind `enableLocalFirstSync` (off by default).** A project | ||
| that has flipped keeps a POJO backup until confirmed stable; the flip is a **one-way door** per | ||
| project, so it ships dark and is enabled only after the B0.1 gate + a Phase-1 CI read-verify both | ||
| pass. Persistence (`y-indexeddb` + Tauri update-log) and sync (file-based → P2P → optional relay) | ||
| layer on after the flip. | ||
|
|
||
| This ADR **supersedes the persistence / source-of-truth half** of [[0001-state-management-boundaries]] | ||
| for project *domain* data. It does **not** change the ephemeral-state half: transient UI stays in | ||
| Zustand/Redux exactly as before. | ||
|
|
||
| ## Consequences | ||
|
|
||
| - **Positive:** true offline-merge and multi-device become possible on the existing E2E transport; a | ||
| single coherent edit history; char-level conflict resolution that a string model cannot provide; | ||
| the substrate (Yjs, crypto, worker bus) is already present. | ||
| - **Negative:** the migration is the highest-risk work item in the roadmap; flipping the canonical | ||
| store is irreversible per project; during the transition two mental models coexist (mitigated by the | ||
| shadow phase keeping `redux-undo` until the flip, and by this ADR). | ||
| - **Rejected — stay offline-only / single-device:** caps the product at "an excellent single-device | ||
| writer" when the parts for the local-first category leader already exist. | ||
| - **Rejected — shadow doc permanent (Redux stays SoT):** a derived Redux cannot merge; you get sync | ||
| plumbing without the conflict-free guarantee, and you maintain two histories forever. | ||
| - **Rejected — Automerge:** see above; discards vendored, security-reviewed Yjs + transport. | ||
|
|
||
| ## References | ||
|
|
||
| - PoC: `services/localFirst/projectDoc.ts`, `tests/unit/localFirst/projectDoc.test.ts` (B0.1 gate) | ||
| - Baseline: A0.1 perf harness (`tests/bench/`) — the before/after gate for the migration | ||
| - Strategic plan: `/home/pc/.claude/plans/master-prompt-strategic-snazzy-duckling.md` (§3, §5, §7, §8) | ||
| - [[0001-state-management-boundaries]] (superseded in part), `services/collaborationService.ts`, | ||
| `packages/collab-transport` | ||
| - **0009 (planned)** — sync threat model (signaling metadata, E2E guarantees, opt-in posture) |
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
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.