Editor engine: markdown text as the live doc, hidden syntax, offline, sync #35

Closed
kayg wants to merge 0 commits from issue/7-editor-core-5 into dev
Owner

Part of #7 (engine only; the look waits for the mockup in #13). Replaces #34, #33 and #21 (pushes to an open PR's branch don't trigger CI here): rebased onto dev with notes storage, sign-in and the CI image.

New since #33: the layout shift, fixed at the root

CI saw a tiny shift (#text, ~1 s after load, chromium-390). A CPU-throttled run (×6–8) reproduced it every time; a mutation trace showed list/heading lines drawn raw for one frame and decorated the next. Root cause: decorations were built from syntaxTree(state), which doesn't see the parsing ensureSyntaxTree just did, so lines past the old parse point had no tree yet.

  • Decorations now use the tree returned by the synchronous parse, over CodeMirror's whole drawn range (not only on-screen lines).
  • Prefix and block lookups parse their own line before reading the tree.
  • The note editor is its own scroll container (CodeMirror can anchor while it measures); the status line sits in a slot that can't move layout.
  • New regression test: the layout-shift test also runs at CPU ×6. With the old code it fails 4/4; with the fix it passes. The original test is unchanged (still zero).
  • Cost check: rebuilding decorations for a 6000-char drawn range takes ~1 ms.

Second CI catch (on #34): the status changing "Saving…" → "Saved" in a right-aligned, text-sized slot moved its start edge. It now has a fixed-width slot whose text starts at a fixed edge. A new test slows the server's confirmations so the status flips well after the last keystroke and asserts nothing moves; it fails with the old CSS and passes now.

Also: formatDay → "Wednesday, 7 October" (year only if not the current year). Neither the engine nor the shell shows dates yet; this is the one formatter they'll use.

What's in it

packages/editor (CodeMirror 6 + Yjs, ADR 0018–0022, 0036)

  • One shared text (md) per note. The editor is built from it and rebuilt from it on reattach, never the other way round (inSync() checks it).
  • Default mode hides markdown: #, **, _, ~~, inline code marks, link syntax, >, bullets become a bullet, - [ ]/[x]/[/]/[-] become a checkbox, --- becomes a rule, fences are hidden. Replace decorations + atomic ranges, built from a fully parsed tree before first paint. A lone cursor is kept out of hidden leading syntax. Frontmatter is hidden as a block. Source mode shows everything.
  • Block controls in the margins (handle, "Make it a note", "More"), absolutely positioned, one measure per hover, never on the H1, blank lines or frontmatter; plain unstyled buttons with labels.
  • Typing shortcuts: finishing [[Title]] and picking from [[/@ both write [Title](<Title.md>), paths relative to the note, no ./. Only on the person's own keystrokes (input handler + completion source); remote text is never transformed.
  • Undo tracks only this editor's changes. IME: remote and other-tab changes wait behind a gate while a composition is open.
  • Sync client: y-websocket protocol, plus save checks 100 <id> → 101 <id> so "Saved" is claimed only after the server confirms (works with servers that don't know them: they just never say saved). Lineage on the URL; a 4409 close means the server's copy was replaced, and only the changes since the last confirmed base are replayed (diff-match-patch), or kept as a copy if they can't be placed.
  • Local copy: own IndexedDB persistence (replaces y-indexeddb). Found and fixed a data-loss trap: a tab closed within milliseconds of typing offline lost its last letters (4/40 runs). Unconfirmed changes are now written synchronously to localStorage on pagehide/hidden, and rescued on open (now 40/40). Compaction merges what's stored, so another tab's changes are never dropped.
  • Tabs share a note through BroadcastChannel. Client ids are never stored. Persistent storage is requested.
  • test-server/: a stand-in note room server speaking exactly the contract above, for tests until #6.

apps/web: plain Svelte 5 + Vite shell: tiny router (/, /notes/<id>, ?mode=source, #block), one store keyed by id fed by journal entries (dedupes by seq, resets on expired token), live socket client for /api/v1/live, a note page with a source-mode toggle and an honest status line (role=status), explicit ready signals (html[data-ready], [data-synced]).

Contract for #6 (the Rust notes server)

  • GET /api/v1/notes/<id>/sync?lineage=<l> websocket, y-websocket messages; send step 1 on connect; broadcast updates to every connection.
  • Answer 100 <id> with 101 <id> in order, after the changes before it are durable (history written).
  • Close with 4409 (reason = current lineage) when the client's lineage differs. Only the server seeds a doc from file text, and it writes meta.lineage.

Tests

  • Unit (vitest): 48 passing (incl. dates and a decoration cost guard): hidden-syntax ranges, prefixes, blocks, links/paths, replay and emoji-safe edits, sync (convergence, saved-only-when-confirmed, offline + reconnect, offline delete not resurrected, IME gate, lineage refusal, lineage replay), editor (shared text, [[ ]], remote text untouched, undo only own typing, list continuation, modes, cursor), local copy (reopen, rescue, pagehide flush, compaction across tabs).
  • Browser (Playwright, Chromium + WebKit, 390 + 1440): 12 tests × 4 projects, all green, run 3× without a flake. Locators by role and name, waits on the ready signal. CLS and IME tests are Chromium-only (WebKit has no Layout Instability API; IME is driven through CDP).
  • Typecheck: tsc (editor) and svelte-check (web), clean.

Perf (keystroke to next frame, 200-paragraph note, PERF=1, 1 worker)

p50 p95 max
Chromium 1440 11.6 ms 18.7 ms 21.5 ms
Chromium 390 10.7 ms 18.2 ms 19.5 ms
WebKit 1440 (software GL) 20 ms 47 ms 204 ms

Measured on the shared dev box under load from other jobs; the real gate belongs on the perf VM. Chromium p95 < 50 ms is asserted; WebKit is reported only (software rendering). Bundle: 524 KB min / 172 KB gzip (avoided markdown(), which pulled in HTML/CSS/JS languages, ~180 KB).

CI

Adds a web job using calternal-ci-web:1.63.0 built from ci/web.Dockerfile (Playwright 1.63.0 image + bun 1.4.2). The image needs building on the CI host before this job can go green.

Deferred

  • Multiplexing note sync over the one live socket (the Transport interface is ready; needs #6).
  • Leader-tab election (tabs share via BroadcastChannel; each tab writes its own changes).
  • Note picker UI for [[/@ (engine exposes search/resolve), block actions behaviour (→ make note, ⋯ menu), keyboard access to block controls, all styling: after #13.
  • Images/embeds rendering.

🤖 Generated with Claude Code

https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T

Part of #7 (engine only; the look waits for the mockup in #13). Replaces #34, #33 and #21 (pushes to an open PR's branch don't trigger CI here): rebased onto dev with notes storage, sign-in and the CI image. ## New since #33: the layout shift, fixed at the root CI saw a tiny shift (`#text`, ~1 s after load, chromium-390). A CPU-throttled run (×6–8) reproduced it every time; a mutation trace showed list/heading lines drawn **raw for one frame** and decorated the next. Root cause: decorations were built from `syntaxTree(state)`, which doesn't see the parsing `ensureSyntaxTree` just did, so lines past the old parse point had no tree yet. - Decorations now use the tree returned by the synchronous parse, over CodeMirror's whole drawn range (not only on-screen lines). - Prefix and block lookups parse their own line before reading the tree. - The note editor is its own scroll container (CodeMirror can anchor while it measures); the status line sits in a slot that can't move layout. - New regression test: the layout-shift test also runs at CPU ×6. With the old code it fails 4/4; with the fix it passes. The original test is unchanged (still zero). - Cost check: rebuilding decorations for a 6000-char drawn range takes ~1 ms. **Second CI catch (on #34):** the status changing "Saving…" → "Saved" in a right-aligned, text-sized slot moved its start edge. It now has a fixed-width slot whose text starts at a fixed edge. A new test slows the server's confirmations so the status flips well after the last keystroke and asserts nothing moves; it fails with the old CSS and passes now. Also: `formatDay` → "Wednesday, 7 October" (year only if not the current year). Neither the engine nor the shell shows dates yet; this is the one formatter they'll use. ## What's in it **`packages/editor`** (CodeMirror 6 + Yjs, ADR 0018–0022, 0036) - One shared text (`md`) per note. The editor is built from it and rebuilt from it on reattach, never the other way round (`inSync()` checks it). - **Default mode** hides markdown: `#`, `**`, `_`, `~~`, inline code marks, link syntax, `>`, bullets become a bullet, `- [ ]`/`[x]`/`[/]`/`[-]` become a checkbox, `---` becomes a rule, fences are hidden. Replace decorations + atomic ranges, built from a fully parsed tree before first paint. A lone cursor is kept out of hidden leading syntax. Frontmatter is hidden as a block. **Source mode** shows everything. - **Block controls** in the margins (handle, "Make it a note", "More"), absolutely positioned, one measure per hover, never on the H1, blank lines or frontmatter; plain unstyled buttons with labels. - **Typing shortcuts**: finishing `[[Title]]` and picking from `[[`/`@` both write `[Title](<Title.md>)`, paths relative to the note, no `./`. Only on the person's own keystrokes (input handler + completion source); remote text is never transformed. - **Undo** tracks only this editor's changes. **IME**: remote and other-tab changes wait behind a gate while a composition is open. - **Sync client**: y-websocket protocol, plus save checks `100 <id>` → `101 <id>` so "Saved" is claimed only after the server confirms (works with servers that don't know them: they just never say saved). Lineage on the URL; a 4409 close means the server's copy was replaced, and only the changes since the last confirmed base are replayed (diff-match-patch), or kept as a copy if they can't be placed. - **Local copy**: own IndexedDB persistence (replaces y-indexeddb). Found and fixed a data-loss trap: a tab closed within milliseconds of typing offline lost its last letters (4/40 runs). Unconfirmed changes are now written synchronously to localStorage on `pagehide`/hidden, and rescued on open (now 40/40). Compaction merges what's stored, so another tab's changes are never dropped. - Tabs share a note through BroadcastChannel. Client ids are never stored. Persistent storage is requested. - `test-server/`: a stand-in note room server speaking exactly the contract above, for tests until #6. **`apps/web`**: plain Svelte 5 + Vite shell: tiny router (`/`, `/notes/<id>`, `?mode=source`, `#block`), one store keyed by id fed by journal entries (dedupes by seq, resets on expired token), live socket client for `/api/v1/live`, a note page with a source-mode toggle and an honest status line (`role=status`), explicit ready signals (`html[data-ready]`, `[data-synced]`). ## Contract for #6 (the Rust notes server) - `GET /api/v1/notes/<id>/sync?lineage=<l>` websocket, y-websocket messages; send step 1 on connect; broadcast updates to every connection. - Answer `100 <id>` with `101 <id>` in order, **after** the changes before it are durable (history written). - Close with **4409** (reason = current lineage) when the client's lineage differs. Only the server seeds a doc from file text, and it writes `meta.lineage`. ## Tests - Unit (vitest): 48 passing (incl. dates and a decoration cost guard): hidden-syntax ranges, prefixes, blocks, links/paths, replay and emoji-safe edits, sync (convergence, saved-only-when-confirmed, offline + reconnect, offline delete not resurrected, IME gate, lineage refusal, lineage replay), editor (shared text, `[[ ]]`, remote text untouched, undo only own typing, list continuation, modes, cursor), local copy (reopen, rescue, pagehide flush, compaction across tabs). - Browser (Playwright, Chromium + WebKit, 390 + 1440): 12 tests × 4 projects, all green, run 3× without a flake. Locators by role and name, waits on the ready signal. CLS and IME tests are Chromium-only (WebKit has no Layout Instability API; IME is driven through CDP). - Typecheck: `tsc` (editor) and `svelte-check` (web), clean. ## Perf (keystroke to next frame, 200-paragraph note, `PERF=1`, 1 worker) | | p50 | p95 | max | |---|---|---|---| | Chromium 1440 | 11.6 ms | 18.7 ms | 21.5 ms | | Chromium 390 | 10.7 ms | 18.2 ms | 19.5 ms | | WebKit 1440 (software GL) | 20 ms | 47 ms | 204 ms | Measured on the shared dev box under load from other jobs; the real gate belongs on the perf VM. Chromium p95 < 50 ms is asserted; WebKit is reported only (software rendering). Bundle: 524 KB min / 172 KB gzip (avoided `markdown()`, which pulled in HTML/CSS/JS languages, ~180 KB). ## CI Adds a `web` job using **`calternal-ci-web:1.63.0`** built from `ci/web.Dockerfile` (Playwright 1.63.0 image + bun 1.4.2). The image needs building on the CI host before this job can go green. ## Deferred - Multiplexing note sync over the one live socket (the `Transport` interface is ready; needs #6). - Leader-tab election (tabs share via BroadcastChannel; each tab writes its own changes). - Note picker UI for `[[`/`@` (engine exposes `search`/`resolve`), block actions behaviour (→ make note, ⋯ menu), keyboard access to block controls, all styling: after #13. - Images/embeds rendering. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
Editor engine: markdown text as the live doc, hidden syntax, offline, sync
Some checks failed
CI / rust (pull_request) Successful in 37s
CI / web (pull_request) Failing after 44s
1b0aa28953
packages/editor (CodeMirror 6 + Yjs):
- One shared text per note; the editor is built from it and rebuilt on
  reattach, never the other way round.
- Default mode hides markdown syntax (replace decorations + atomic ranges,
  parsed before first paint); source mode shows it all, frontmatter too.
- Margin block controls (handle, make-note, more) that never move text and
  never show on the title or blank lines.
- [[Title]] and @ produce standard links, only on the person's own typing.
- Undo tracks only local changes; remote changes wait during IME composition.
- y-websocket sync plus save checks (100/101) so "Saved" is only claimed
  when the server confirms; lineage mismatch replays only offline changes.
- Local copy in IndexedDB with a synchronous write-ahead list on page hide
  (fixes losing the last keystrokes when a tab closes right after typing).
- Tabs share the note over BroadcastChannel.
- A stand-in note server for tests until #6 lands.

apps/web: plain Svelte 5 shell with a tiny router, one journal-fed store,
the live socket client, and a note page. The look waits for #13.

CI: a web job in calternal-ci-web (ci/web.Dockerfile, to be built on the
CI host).

Part of #7

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
Editor: no line is ever drawn before it is decorated
Some checks failed
CI / rust (pull_request) Successful in 39s
CI / web (pull_request) Failing after 56s
85506797e1
CI caught a sub-pixel layout shift while a note loaded. Root cause:
decorations were built from `syntaxTree(state)`, which does not see the
parsing `ensureSyntaxTree` just did, so lines past the old parse point
were drawn raw for a frame and reflowed (`- item` to `• item`). Now:
- decorations use the tree returned by the synchronous parse, and cover
  CodeMirror's whole drawn range, not only the on-screen lines;
- prefix and block lookups parse their own line before reading the tree;
- the note editor is its own scroll container, so CodeMirror can anchor
  the view while it measures, and the status line can never move layout.
A CPU-throttled (×6) variant of the layout-shift test reproduced the bug
every time and now guards it.

Also: `formatDay` writes days as "Wednesday, 7 October" (year only when
not the current one), per kayg's decision.

Part of #7

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
Shell: the status line can't move anything when its words change
All checks were successful
CI / rust (pull_request) Successful in 40s
CI / web (pull_request) Successful in 59s
4d606ec992
CI caught "Saving…" turning into "Saved" as a layout shift: the status sat
in a right-aligned slot sized by its text, so shorter words moved its start.
It now has a fixed-width slot with its text starting at a fixed edge. A new
test slows the server's confirmations so the status flips well after the
last keystroke, and asserts nothing moves (it fails with the old CSS).

Part of #7

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
Author
Owner

Reviewed by Claude: zero layout shift now also proven under 6x CPU throttle (fresh parse tree for decorations; fixed status slot). CI green (rust + web: 48 unit, 44 browser tests). Keystroke latency 14-20 ms p50 unloaded; the loaded p95 reading was taken at load avg 24 on the dev box, so the perf-VM gate (#11) must confirm before any perf claim. Merging. Next: #29.

Reviewed by Claude: zero layout shift now also proven under 6x CPU throttle (fresh parse tree for decorations; fixed status slot). CI green (rust + web: 48 unit, 44 browser tests). Keystroke latency 14-20 ms p50 unloaded; the loaded p95 reading was taken at load avg 24 on the dev box, so the perf-VM gate (#11) must confirm before any perf claim. Merging. Next: #29.
Author
Owner

Closing: this branch is already fully contained in dev (it landed through a merge train). Branch cleanup, 2026-10-15.

Closing: this branch is already fully contained in `dev` (it landed through a merge train). Branch cleanup, 2026-10-15.
kayg closed this pull request 2026-10-15 08:35:38 +00:00
All checks were successful
CI / rust (pull_request) Successful in 40s
CI / web (pull_request) Successful in 59s

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
kayg/calternal!35
No description provided.