The server's frontmatter never pulls an edit to the top of a new note (#276) #299

Closed
kayg wants to merge 0 commits from issue/276-selection-jump into dev
Owner

Fixes #276.

Cause. The server's edits were never a replace: frontmatter::set adds the block as one small insert at offset 0, and CodeMirror maps a selection across it correctly. The problem was when it came. A note made with text (notes.create, Make note) got its frontmatter only at its first save, about 750 ms later. Someone who opened the note in that gap was editing at the top of a note that had no frontmatter yet, so their edit and the block landed in the same spot. Yjs orders two inserts at one spot by writer id, which is random:

  • ** from ⌘B could end up before the block: **---\ncalternal-id…\n---\none** (the exact CI text);
  • # from ⌘⌥1 could end up before ---;
  • a caret at the start of the body stayed at 0, above the incoming block, so typing went above the frontmatter.

Fix (server, 10 lines). When an API edit lands on a note that has no file yet, the room adds the server's keys (calternal-id, date, last edited, title) in the same room turn. Anyone who joins finds the frontmatter already there. At the first save the keys are already fresh, so it adds nothing.

Tests

  • crates/notes/src/tests/fresh.rs reproduces the CI failure exactly, every time (5/5 before the fix): a device with the lowest writer id bolds the first word while the first save is on its way. It passes after the fix.
  • apps/web/e2e/fresh-note.spec.ts, Chromium + WebKit: the note opens with its frontmatter, typing at the start of the body stays in the body, and a frontmatter change from elsewhere (pin) leaves the selection on its word, so ⌘B bolds it. Before the fix: 6/6 Chromium failures. WebKit opened the note after the save had already run, which shows the race. After the fix: 12/12 pass.
  • Six Rust tests compared a new note's whole text. They now compare its body (clients::body).

Perf: notes.create with text, 500 calls: p50 3.18 → 4.28 ms, p95 6.6 → 9.3 ms. The three key edits now run at create time instead of at the first save, so the total work is the same. No budget covers create.

🤖 Generated with Claude Code

Fixes #276. **Cause.** The server's edits were never a replace: `frontmatter::set` adds the block as one small insert at offset 0, and CodeMirror maps a selection across it correctly. The problem was *when* it came. A note made with text (`notes.create`, Make note) got its frontmatter only at its first save, about 750 ms later. Someone who opened the note in that gap was editing at the top of a note that had no frontmatter yet, so their edit and the block landed in the same spot. Yjs orders two inserts at one spot by writer id, which is random: - `**` from ⌘B could end up before the block: `**---\ncalternal-id…\n---\none**` (the exact CI text); - `# ` from ⌘⌥1 could end up before `---`; - a caret at the start of the body stayed at 0, above the incoming block, so typing went above the frontmatter. **Fix (server, 10 lines).** When an API edit lands on a note that has no file yet, the room adds the server's keys (`calternal-id`, `date`, `last edited`, `title`) in the same room turn. Anyone who joins finds the frontmatter already there. At the first save the keys are already fresh, so it adds nothing. **Tests** - `crates/notes/src/tests/fresh.rs` reproduces the CI failure exactly, every time (5/5 before the fix): a device with the lowest writer id bolds the first word while the first save is on its way. It passes after the fix. - `apps/web/e2e/fresh-note.spec.ts`, Chromium + WebKit: the note opens with its frontmatter, typing at the start of the body stays in the body, and a frontmatter change from elsewhere (pin) leaves the selection on its word, so ⌘B bolds it. Before the fix: 6/6 Chromium failures. WebKit opened the note after the save had already run, which shows the race. After the fix: 12/12 pass. - Six Rust tests compared a new note's whole text. They now compare its body (`clients::body`). Perf: `notes.create` with text, 500 calls: p50 3.18 → 4.28 ms, p95 6.6 → 9.3 ms. The three key edits now run at create time instead of at the first save, so the total work is the same. No budget covers create. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A note made with text gets its frontmatter with that text (#276)
Some checks failed
CI / server (pull_request) Successful in 22s
CI / web (pull_request) Failing after 23s
CI / rust (pull_request) Successful in 2m17s
1a3ea062f5
The server added a new note's frontmatter (calternal-id, date, last
edited) only at its first save, about a second after notes.create. A
person who opened the note in between edited at the very top of a note
without one, and the block landed in the same spot: Yjs orders two
inserts at one spot by writer id, so bold on the first word could wrap
the whole block (`**---…one**`), a heading key could prefix `---`, and a
caret at the start stayed above the block, so typing went there.

The server's edits were already minimal (an insert, never a replace),
and CodeMirror maps a selection across them correctly. The fix: an API
edit to a note with no file yet brings the server's keys up to date in
the same room turn, so whoever joins finds the frontmatter in place.

Tests: a Rust test that reproduces the CI failure exactly, every time
(a device with the lowest writer id bolds the first word while the
first save is on its way), and a browser test in Chromium and WebKit:
the note opens with its frontmatter, typing at the body's start stays
in the body, and a frontmatter change from elsewhere (pin) leaves the
selection on its word so ⌘B bolds it. Tests that compared a new note's
whole text now compare its body.

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

Landed in merge train #332.

Landed in merge train #332.
kayg closed this pull request 2026-10-14 18:04:24 +00:00
Some checks failed
CI / server (pull_request) Successful in 22s
CI / web (pull_request) Failing after 23s
CI / rust (pull_request) Successful in 2m17s

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!299
No description provided.