Spike yrs.wasm under wazero for Yjs compaction (ADR-3: go)
Compile yrs 0.28 to wasm32-wasip1 and run it in Go with wazero to compact Yjs update logs and extract ProseMirror JSON and plain text. Fixtures come from real Yjs, and y-prosemirror's output is the oracle. - Output matches y-prosemirror on every fixture; Yjs loads yrs snapshots - Snapshot + 200 updates compacts in 0.6-47 ms; WebAssembly costs at most 2x native - merge_updates is quadratic, so the server must compact incrementally Results and design rules are recorded in docs/adr/0003-yrs-wasm-wazero.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
03f3434c10
commit
4cb313aa95
18 files changed
+1778
-2
No files matched your search
+2
-2
@@ -47,7 +47,7 @@ Docwell is an open-source, self-hostable knowledge base and collaborative docume
|
||||
|---|---|---|
|
||||
| ADR-1 | **Editor = Tiptap 2/3 (ProseMirror) for web and desktop**, with `@tiptap/vue-3` | This is the stack Docmost uses and close to what Outline uses. It has a large extension ecosystem (tables, math, mentions, collaboration) and is MIT licensed. |
|
||||
| ADR-2 | **CRDT = Yjs** (`y-prosemirror`, `y-indexeddb`, Awareness for cursors) | Industry standard, and both Docmost and Outline use it. |
|
||||
| ADR-3 | **The Go server relays the y-websocket / Hocuspocus-compatible protocol** and stores incremental updates. Compaction (merging updates into a snapshot, and extracting plain text and JSON for search and exports) runs **Yrs compiled to WASM inside Go via `wazero`**, so there is no cgo and no Node sidecar. | There is no mature pure-Go Yjs port. Options considered: (a) a Node Hocuspocus sidecar, which breaks the "Go only" goal; (b) cgo + yffi, which complicates cross-compiling; (c) client-side compaction, which you can't trust. **(d) wazero + yrs.wasm** was chosen. Spike it in Phase 0. |
|
||||
| ADR-3 | **The Go server relays the y-websocket / Hocuspocus-compatible protocol** and stores incremental updates. Compaction (merging updates into a snapshot, and extracting plain text and JSON for search and exports) runs **Yrs compiled to WASM inside Go via `wazero`**, so there is no cgo and no Node sidecar. | There is no mature pure-Go Yjs port. Options considered: (a) a Node Hocuspocus sidecar, which breaks the "Go only" goal; (b) cgo + yffi, which complicates cross-compiling; (c) client-side compaction, which you can't trust. **(d) wazero + yrs.wasm** was chosen — **spike passed** ([ADR-3](adr/0003-yrs-wasm-wazero.md)): output matches y-prosemirror, steady-state compaction takes milliseconds, WebAssembly costs ≤2× native. Compact incrementally (snapshot + new updates); never `merge_updates` on long logs, which is quadratic. |
|
||||
| ADR-4 | **Mobile editor = the same Tiptap editor bundle running inside a Flutter WebView** (`webview_flutter` or `flutter_inappwebview`), bridged over JS channels. Navigation, lists, search, settings and comments are native Flutter. | Writing a second rich-text engine in Dart with identical schema and Yjs semantics is months of work and a constant source of divergence. AppFlowy built its own Flutter editor, but its data model is native to that editor. Revisit a native Dart editor in v2+. |
|
||||
| ADR-5 | **Search = Bleve** (pure Go, embedded in the server) with an optional **Meilisearch** adapter | PocketBase does not expose SQLite FTS5 through its API. Bleve keeps the "no extra service" promise. The search code sits behind an interface so you can swap the backend. |
|
||||
| ADR-6 | **Authentication = PocketBase's built-in `users` auth collection** — **decided**. PocketBase stores the credentials and issues the tokens, and also handles password, OTP email codes, MFA, OAuth2, verification and password-reset emails (via its own SMTP settings). PocketBase stays private: clients call `/api/v1/auth/*` on docwell-server, which forwards to PocketBase. The server checks bearer tokens through a small hook route (`pb_hooks/docwell.pb.js`, cached 30 s) and adds Docwell's rules: signup policy and domain allow-list, `status = disabled` blocks sign-in (PocketBase `authRule`), and the first account becomes the superadmin (see ADR-12). **Authorization** (workspace, space and page permissions) stays in docwell-server. | Less security-critical code to write and maintain, and auth features (MFA, OTP, OAuth2) come out of the box. Trade-offs: tokens are PocketBase JWTs, so sign-out is client-side (to revoke every session, rotate the user's `tokenKey`), and swapping PocketBase for Postgres later would need a token migration plan. |
|
||||
@@ -221,5 +221,5 @@ Time estimates assume ~2 full-time developers. Scale them for your team.
|
||||
|
||||
1. ~~Confirm the license and the Wails version~~ — decided: AGPL-3.0 for everything, Wails v3. Add the official AGPL-3.0 `LICENSE` text and SPDX headers (`AGPL-3.0-only` or `-or-later`) during scaffolding.
|
||||
2. Scaffold the monorepo (Phase 0 layout above) and docker-compose with the three services.
|
||||
3. Run the three Phase 0 spikes and record the results as ADRs in `docs/adr/`.
|
||||
3. Run the three Phase 0 spikes and record the results as ADRs in `docs/adr/`. Done: yrs.wasm under wazero ([ADR-3](adr/0003-yrs-wasm-wazero.md), go). Still to do: Tiptap in a Flutter WebView, Wails v3 tray/deep link/auto-update.
|
||||
4. Write `api/openapi.yaml` v0 and the first PocketBase migrations.
|
||||
@@ -0,0 +1,65 @@
|
||||
# ADR-3: Yjs compaction with yrs compiled to WebAssembly, run by wazero
|
||||
|
||||
- Status: **accepted** (Phase 0 spike passed, 2026-10-30)
|
||||
- Spike code: [`Spikes/yrs-wasm/`](../../Spikes/yrs-wasm/README.md)
|
||||
|
||||
## Context
|
||||
|
||||
The Go server relays Yjs updates between editors and stores them. It also has to read the documents itself: compact the update log into a snapshot, extract ProseMirror JSON for exports and plain text for search, and answer sync step 2 (send a peer what its state vector is missing). There is no mature pure-Go Yjs port. We wanted to avoid a Node sidecar and cgo, so the plan was to compile yrs (the Rust port of Yjs) to WebAssembly and run it in-process with wazero. This spike decides go or no-go.
|
||||
|
||||
## What was built
|
||||
|
||||
- `rust/`: a ~250-line wrapper around yrs 0.28 compiled to `wasm32-wasip1` (577 KiB). It exports `compact`, `merge`, `state_vector`, `diff`, `to_json` and `to_text` through a tiny pointer-and-length ABI.
|
||||
- `go/`: a wazero host (`yrs.go`) that embeds the module, plus tests and benchmarks.
|
||||
- `fixtures/`: generates realistic edit logs with real Yjs (keystroke-by-keystroke typing with headings, lists, bold and links, backspaces and edits to earlier blocks, and three clients editing concurrently). y-prosemirror's `yXmlFragmentToProsemirrorJSON` supplies the expected output. `verify.mjs` checks that Yjs loads what yrs wrote and times Yjs as a baseline.
|
||||
|
||||
| Fixture | Updates | Log size | Snapshot | Notes |
|
||||
|---|---:|---:|---:|---|
|
||||
| small | 2,277 | 56 KB | 5 KB | ~2k characters |
|
||||
| medium | 30,804 | 785 KB | 66 KB | ~30k characters, a long page |
|
||||
| large | 255,566 | 6.8 MB | 553 KB | ~250k characters, a book-length page |
|
||||
| concurrent | 19,999 | 697 KB | 597 KB | 3 clients, random sync order |
|
||||
|
||||
## Results
|
||||
|
||||
**Correctness: all pass.**
|
||||
- yrs JSON matches y-prosemirror exactly on every fixture. This holds whether yrs reads the raw log, a merged update, a compacted snapshot, or a snapshot rebuilt in batches of 500 updates (the server's real pattern).
|
||||
- Yjs loads every yrs snapshot and produces identical JSON. Snapshots are the same size as Yjs's own but not byte-identical, because encoding order differs. That doesn't matter.
|
||||
- Sync step 2 (`diff` against a client's state vector) brings a half-synced client to the same document.
|
||||
- Malformed updates return an error message, and the instance keeps working afterwards.
|
||||
- 8 goroutines, each with its own instance of one compiled module, run concurrently without problems.
|
||||
|
||||
**Speed** (AMD Threadripper 2950X, Go 1.26, wazero 1.12 compiler mode):
|
||||
|
||||
| Operation | small | medium | large | concurrent |
|
||||
|---|---:|---:|---:|---:|
|
||||
| Compact the full log (one-off) | 4.4 ms | 71 ms | 2.7 s | 80 ms |
|
||||
| **Compact snapshot + 200 new updates (steady state)** | **0.6 ms** | **2.9 ms** | **23 ms** | **47 ms** |
|
||||
| JSON from snapshot (export, indexing) | 0.3 ms | 3.5 ms | 33 ms | 42 ms |
|
||||
| Plain text from snapshot (search) | 0.25 ms | 2.6 ms | 23 ms | 39 ms |
|
||||
| `merge_updates_v1` on the full log | 67 ms | **13.2 s** | not run | **5.7 s** |
|
||||
|
||||
Baselines for the same machine and fixtures:
|
||||
- Native yrs (no WebAssembly) compacts in 2.7 / 38 / 2,500 / 49 ms and builds JSON in 0.27 / 2.9 / 32 / 32 ms. WebAssembly costs 1.0–1.9×, and the large case is limited by yrs itself.
|
||||
- Yjs in Node compacts in 20 / 103 / 1,468 / 166 ms, so yrs is in the same range. Its `mergeUpdates` is quadratic too (21.7 s on medium).
|
||||
|
||||
**Startup and memory:**
|
||||
- Compiling the module takes ~350 ms, once per process. Each instance takes 0.5 ms and 1.1 MiB to create.
|
||||
- Memory grows with the largest document an instance has loaded: 17 MiB after medium, 55 MiB after large. WebAssembly memory never shrinks.
|
||||
|
||||
## Decision
|
||||
|
||||
**Go.** Keep ADR-3 as planned: yrs.wasm under wazero, with no cgo and no Node sidecar. The rules below come from the spike.
|
||||
|
||||
1. **Compact incrementally; never use `merge` on long logs.** `merge_updates_v1` (and Yjs's `mergeUpdates`) is quadratic in the number of updates. The compaction job loads the latest snapshot plus the pending updates, applies them to a document and re-encodes it (`Compact`). That keeps the steady-state cost to milliseconds even for very long pages. Run it after N updates or T seconds of idle time per document.
|
||||
2. **One instance per worker, with a pool.** Instances aren't safe for concurrent use. Compile once at startup. Retire an instance when its memory passes a limit (start at 64 MiB) and **after any error that came from a trap**, because the module is built with `panic = "abort"`.
|
||||
3. **Reuse the startup cost.** Turn on wazero's file compilation cache so restarts skip most of the 350 ms compile.
|
||||
4. **Text offsets are UTF-16, as in Yjs.** When an edit splits a surrogate pair (an emoji), Yjs keeps the broken half or replaces it with U+FFFD, and yrs keeps the character whole. ProseMirror never splits a surrogate pair, so real editors don't hit this. Our own code must never produce such edits either: no server-side text edits at raw offsets.
|
||||
5. **Build:** pin `yrs` (0.28.x) and build the module in CI and the Docker images with the `wasm32-wasip1` target, then embed it with `go:embed`. Don't commit the binary. yrs is MIT licensed, which is compatible with AGPL-3.0.
|
||||
6. **Platforms:** wazero's compiler covers amd64 and arm64, which are the published image architectures. Other architectures fall back to wazero's interpreter, which is much slower; that's acceptable for hobby use but not supported.
|
||||
|
||||
## Not covered (follow up in Phase 1)
|
||||
|
||||
- Tiptap extensions beyond StarterKit, links and headings (tables, mentions, images). The JSON conversion is generic over element names and attributes, so they should work, but test them against the real editor schema.
|
||||
- Subdocuments, and the awareness protocol (cursors). The awareness protocol is relayed without decoding, so it doesn't need yrs.
|
||||
- Long-running memory behaviour under a real workload, and the pool's retirement threshold.
|
||||
Reference in new issue
Block a user