Editor: the selection jumps to the top when a server change arrives just after opening a note #276

Closed
opened 2026-10-14 12:53:49 +00:00 by kayg · 1 comment
Owner

Found by the CI work (#11, proof runs 206 and 209). In shortcuts.spec (Chromium), the test selects a word and presses ⌘B or ⌘⌥1, and the formatting lands on the frontmatter --- at the very start of the note instead of on the selected word.

Suspected cause: right after a note opens, the server rewrites its frontmatter (calternal-id, date, last edited). If that arrives as a whole-document replace, or as a big delete plus insert, rather than a minimal diff, the local selection is mapped to position 0.

Risk to people: typing or formatting just after opening a note could land at the top of the note.

Fix:

  1. Reproduce it deterministically.
  2. Make sure the server's frontmatter edits travel as minimal Yjs ops (a diff, never a replace), and that remote changes map the local selection correctly. The #150 read-only to live swap and the #3 lineage replay are suspects too.
  3. Add a regression test: select text, have the remote frontmatter rewrite arrive, press ⌘B, and assert the selection and the formatting stay in place. Run it in Chromium and WebKit.
Found by the CI work (#11, proof runs 206 and 209). In shortcuts.spec (Chromium), the test selects a word and presses ⌘B or ⌘⌥1, and the formatting lands on the frontmatter `---` at the very start of the note instead of on the selected word. **Suspected cause:** right after a note opens, the server rewrites its frontmatter (calternal-id, date, last edited). If that arrives as a whole-document replace, or as a big delete plus insert, rather than a minimal diff, the local selection is mapped to position 0. **Risk to people:** typing or formatting just after opening a note could land at the top of the note. **Fix:** 1. Reproduce it deterministically. 2. Make sure the server's frontmatter edits travel as minimal Yjs ops (a diff, never a replace), and that remote changes map the local selection correctly. The #150 read-only to live swap and the #3 lineage replay are suspects too. 3. Add a regression test: select text, have the remote frontmatter rewrite arrive, press ⌘B, and assert the selection and the formatting stay in place. Run it in Chromium and WebKit.
Author
Owner

Reproduced deterministically and fixed in PR #299.

Cause. It isn't a replace: the server sends a minimal Yjs insert, and CodeMirror maps the selection across it correctly. The problem is timing. A note made with text got its frontmatter only at its first save, about 750 ms after notes.create. Anyone who opened it in that gap was editing at offset 0 of a note with no frontmatter, so their edit and the server's ---…--- block became two inserts at the same spot. Yjs orders those by writer id, which is random. That gave the three CI symptoms: **---…one**, # ---, and a caret left above the block.

Deterministic repro (Rust, tests/fresh.rs). A client with the lowest writer id bolds the first word while the first save is on its way. It failed 5/5 before the fix with the exact CI text:

"**---\ncalternal-id: …\ndate: …\nlast edited: …\n---\none** two\n"

Browser repro (e2e/fresh-note.spec.ts), pre-fix binary. Chromium failed 6/6: the editor showed one two three with no frontmatter after open. WebKit passed, because it opened the note slowly enough that the save had already run, which is the race itself. With the fix: Chromium and WebKit, 12/12 pass.

Fix. An API edit to a note with no file yet adds the server keys in the same room turn. No client change was needed.

Perf. notes.create with text, 500 calls: p50 3.18 → 4.28 ms, p95 6.6 → 9.3 ms. The same three edits now run at create instead of at the first save.

Regression check. Full wide e2e (Chromium + WebKit) on the fix. The only failures are glass, tabs, tag-rename and toast-bar (WebKit), and all of them fail the same way on the pre-fix binary. cargo test --workspace, fmt and clippy are clean.

**Reproduced deterministically and fixed in PR #299.** **Cause.** It isn't a replace: the server sends a minimal Yjs insert, and CodeMirror maps the selection across it correctly. The problem is timing. A note made with text got its frontmatter only at its first save, about 750 ms after `notes.create`. Anyone who opened it in that gap was editing at offset 0 of a note with no frontmatter, so their edit and the server's `---…---` block became two inserts at the same spot. Yjs orders those by writer id, which is random. That gave the three CI symptoms: `**---…one**`, `# ---`, and a caret left above the block. **Deterministic repro (Rust, `tests/fresh.rs`).** A client with the lowest writer id bolds the first word while the first save is on its way. It failed 5/5 before the fix with the exact CI text: ``` "**---\ncalternal-id: …\ndate: …\nlast edited: …\n---\none** two\n" ``` **Browser repro (`e2e/fresh-note.spec.ts`), pre-fix binary.** Chromium failed 6/6: the editor showed `one two three` with no frontmatter after open. WebKit passed, because it opened the note slowly enough that the save had already run, which is the race itself. With the fix: Chromium and WebKit, 12/12 pass. **Fix.** An API edit to a note with no file yet adds the server keys in the same room turn. No client change was needed. **Perf.** `notes.create` with text, 500 calls: p50 3.18 → 4.28 ms, p95 6.6 → 9.3 ms. The same three edits now run at create instead of at the first save. **Regression check.** Full wide e2e (Chromium + WebKit) on the fix. The only failures are glass, tabs, tag-rename and toast-bar (WebKit), and all of them fail the same way on the pre-fix binary. `cargo test --workspace`, fmt and clippy are clean.
kayg closed this issue 2026-10-14 18:04:35 +00:00
Sign in to join this conversation.
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#276
No description provided.