Corruption traps round 2: fuzz, kill -9 and browser tests, and the bugs they found (#3) #126

Closed
kayg wants to merge 0 commits from issue/3-trap-hunt-r2 into dev
Owner

Round 2 of the corruption trap hunt (#3). The issue holds the full table (trap, guard, test, status). This PR adds the tests that were missing and fixes the bugs they found.

New tests

Test Kind Traps
crates/notes/src/tests/writers.rs multi-client fuzz, 22 seeds C7, C8, D11, F29
crates/calternal/tests/notes_crash_typing.rs kill -9 of the real binary, websocket G21, #32
crates/notes/src/tests/pending.rs unit and room D (new: out-of-order changes)
saving.rs: a_write_error_keeps_the_change, an_error_after_the_write_does_not_double_text disk fault injection G22
rooms.rs: retried_create_makes_one_note retry D10
diff.rs: an_inserted_line_starts_at_a_line_start unit C7
packages/editor/test/traps.test.ts multi-client, lineage A2, A3
apps/web/e2e/traps.spec.ts real browser (Chromium, WebKit) A2, A3, G23

writers.rs runs these at once on one open note:

  • 3 clients typing, with updates held back and delivered out of order;
  • outside edits to the file, both fresh and stale write-backs, with the watcher on and off;
  • agent append (retried), patch, and a replace made from an old read;
  • saves at random moments.

It checks that every word appears exactly once, that everyone converges, that the file matches the note, and that every version matches its text.

Bugs found and fixed

  1. The browser never knew which lineage its copy came from. The server sends the lineage with the join answer, not inside the document. The fake test room put it inside the document, so the unit tests passed, but in the real app session.lineage was always null. The guard against merging a replaced copy was therefore never active. The lineage is now learned on join and stored in the note's metadata on the device. The fake room now matches the real server.

  2. A tab on the old lineage fed its whole text into a tab that was recovering, so the recovering tab held the text twice. Tabs now share changes only with tabs on the same lineage, and a tab joins the channel only once it knows its lineage.

  3. After a lineage change, text a collaborator typed since the last save was replayed a second time. The base is now updated whenever a server change arrives while nothing of ours is pending.

  4. A write that reported an error after the rename doubled the text. This happens when the folder fsync fails. The file held our own bytes, and the next check treated them as an outside edit. After a failed write, the file is now read back and its hash compared before deciding.

  5. A retried notes.create made a second note. This happened when the server died after creating the note but before storing the answer. The note id is now derived from the idempotency key, the same way job ids already are.

  6. Outside edits and replaces could garble lines. They were diffed character by character, and the diff could slide across line breaks. A line typed at the same moment then landed inside another line (<1.4.stale1<1.5.typed1.1>). The diff now compares lines first and only diffs characters inside a line that changed in place.

  7. yrs 0.28 handles out-of-order changes from one writer badly.

    • It loses a change that was parked behind a second one waiting on the same gap. pending.rs reproduces this in plain yrs.
    • It can also place a change with a hole in it. After that, the diffs it reports leave text out, so history misses text, a version no longer matches its text, and a replace built from that version doubled the text.

    The fix:

    • The room no longer applies a client change that skips ahead. It asks that client to resend everything the server lacks, and holds that client's "Saved" until the resend arrives.
    • ydoc::apply re-offers parked changes after every update.

I checked that each fix's test fails without the fix (I reverted one fix at a time). For 1 to 3, the new tests failed before the fix.

Performance

Typing path (fanout_bench, release, 300 samples, interleaved before/after runs on the shared machine):

before after
Collaborator sees typing, p50 (no disk load) 0.27–0.39 ms 0.33–0.43 ms
Typed until Saved, p50 (no disk load) 0.39–0.57 ms 0.43–0.55 ms
Throughput 4.2k–18k changes/s 4.8k–16k changes/s

Throughput is dominated by fsync noise on the shared disk, and the two ranges overlap. The bench's own typists occasionally get dropped under load, both before and after the change; that comes from the bench itself.

Outside edits and replaces (diff::edits, release, best of 20):

Note Change before after
100 lines one-word edit 0.51 ms 0.016 ms
100 lines half the lines changed 2.9 ms 0.46 ms
2000 lines one-word edit 25.8 ms 0.12 ms
2000 lines half the lines changed 215 ms (hit the deadline) 6.5 ms

Verified locally

  • cargo fmt --check, cargo clippy --workspace --all-targets -D warnings, cargo test --workspace: all pass.
  • notes_crash_typing passed 11 runs in a row: 12 kills each, about 3,000 to 3,400 words, none lost or doubled.
  • bun run check (editor and web) and the editor unit tests (77) pass.
  • Playwright traps, editor, pwa and links specs, chromium-1440 and webkit-1440:
    • Earlier, at normal load: all 39 non-skipped tests passed except the first version of my new offline-tabs test, whose assertion I then fixed. traps.spec then passed on its own at 1440 and 390 in both browsers (6 passed).
    • Later, with the shared host at a load average of about 100: WebKit timed out widely (clicks, hovers, waitFor). CI should confirm.

Not fixed here: several gaps are design questions or larger changes. They are listed on #3.

🤖 Generated with Claude Code

Round 2 of the corruption trap hunt (#3). The issue holds the full table (trap, guard, test, status). This PR adds the tests that were missing and fixes the bugs they found. ## New tests | Test | Kind | Traps | |---|---|---| | `crates/notes/src/tests/writers.rs` | multi-client fuzz, 22 seeds | C7, C8, D11, F29 | | `crates/calternal/tests/notes_crash_typing.rs` | kill -9 of the real binary, websocket | G21, #32 | | `crates/notes/src/tests/pending.rs` | unit and room | D (new: out-of-order changes) | | `saving.rs`: `a_write_error_keeps_the_change`, `an_error_after_the_write_does_not_double_text` | disk fault injection | G22 | | `rooms.rs`: `retried_create_makes_one_note` | retry | D10 | | `diff.rs`: `an_inserted_line_starts_at_a_line_start` | unit | C7 | | `packages/editor/test/traps.test.ts` | multi-client, lineage | A2, A3 | | `apps/web/e2e/traps.spec.ts` | real browser (Chromium, WebKit) | A2, A3, G23 | `writers.rs` runs these at once on one open note: - 3 clients typing, with updates held back and delivered out of order; - outside edits to the file, both fresh and stale write-backs, with the watcher on and off; - agent append (retried), patch, and a replace made from an old read; - saves at random moments. It checks that every word appears exactly once, that everyone converges, that the file matches the note, and that **every version matches its text**. ## Bugs found and fixed 1. **The browser never knew which lineage its copy came from.** The server sends the lineage with the join answer, not inside the document. The fake test room put it inside the document, so the unit tests passed, but in the real app `session.lineage` was always null. The guard against merging a replaced copy was therefore never active. The lineage is now learned on join and stored in the note's metadata on the device. The fake room now matches the real server. 2. **A tab on the old lineage fed its whole text into a tab that was recovering**, so the recovering tab held the text twice. Tabs now share changes only with tabs on the same lineage, and a tab joins the channel only once it knows its lineage. 3. **After a lineage change, text a collaborator typed since the last save was replayed a second time.** The base is now updated whenever a server change arrives while nothing of ours is pending. 4. **A write that reported an error after the rename doubled the text.** This happens when the folder fsync fails. The file held our own bytes, and the next check treated them as an outside edit. After a failed write, the file is now read back and its hash compared before deciding. 5. **A retried `notes.create` made a second note.** This happened when the server died after creating the note but before storing the answer. The note id is now derived from the idempotency key, the same way job ids already are. 6. **Outside edits and replaces could garble lines.** They were diffed character by character, and the diff could slide across line breaks. A line typed at the same moment then landed inside another line (`<1.4.stale1<1.5.typed1.1>`). The diff now compares lines first and only diffs characters inside a line that changed in place. 7. **yrs 0.28 handles out-of-order changes from one writer badly.** - It loses a change that was parked behind a second one waiting on the same gap. `pending.rs` reproduces this in plain yrs. - It can also place a change with a hole in it. After that, the diffs it reports leave text out, so history misses text, a version no longer matches its text, and a replace built from that version doubled the text. The fix: - The room no longer applies a client change that skips ahead. It asks that client to resend everything the server lacks, and holds that client's "Saved" until the resend arrives. - `ydoc::apply` re-offers parked changes after every update. I checked that each fix's test fails without the fix (I reverted one fix at a time). For 1 to 3, the new tests failed before the fix. ## Performance **Typing path** (`fanout_bench`, release, 300 samples, interleaved before/after runs on the shared machine): | | before | after | |---|---|---| | Collaborator sees typing, p50 (no disk load) | 0.27–0.39 ms | 0.33–0.43 ms | | Typed until Saved, p50 (no disk load) | 0.39–0.57 ms | 0.43–0.55 ms | | Throughput | 4.2k–18k changes/s | 4.8k–16k changes/s | Throughput is dominated by fsync noise on the shared disk, and the two ranges overlap. The bench's own typists occasionally get dropped under load, both before and after the change; that comes from the bench itself. **Outside edits and replaces** (`diff::edits`, release, best of 20): | Note | Change | before | after | |---|---|---|---| | 100 lines | one-word edit | 0.51 ms | 0.016 ms | | 100 lines | half the lines changed | 2.9 ms | 0.46 ms | | 2000 lines | one-word edit | 25.8 ms | 0.12 ms | | 2000 lines | half the lines changed | 215 ms (hit the deadline) | 6.5 ms | ## Verified locally - `cargo fmt --check`, `cargo clippy --workspace --all-targets -D warnings`, `cargo test --workspace`: all pass. - `notes_crash_typing` passed 11 runs in a row: 12 kills each, about 3,000 to 3,400 words, none lost or doubled. - `bun run check` (editor and web) and the editor unit tests (77) pass. - Playwright `traps`, `editor`, `pwa` and `links` specs, chromium-1440 and webkit-1440: - Earlier, at normal load: all 39 non-skipped tests passed except the first version of my new offline-tabs test, whose assertion I then fixed. `traps.spec` then passed on its own at 1440 and 390 in both browsers (6 passed). - Later, with the shared host at a load average of about 100: WebKit timed out widely (clicks, hovers, `waitFor`). CI should confirm. Not fixed here: several gaps are design questions or larger changes. They are listed on #3. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Corruption traps, round 2 (#3): new fuzz, kill -9 and browser tests, and the bugs they found
Some checks failed
CI / rust (pull_request) Successful in 3m16s
CI / web (pull_request) Failing after 8m30s
b63e310c7c
Tests:
- Every writer at once on one open note (writers.rs): typing delivered
  late and out of order, outside edits (fresh and stale), agent append,
  patch and stale replace, random saves; with and without the watcher.
  Checks every word once, convergence, file == note, version == text.
- kill -9 over the websocket while three tabs type (notes_crash_typing.rs):
  12 kills, ~3000 words, none lost or doubled, file == note.
- Out-of-order changes from one writer (pending.rs), injected write
  errors (saving.rs), a retried create (rooms.rs).
- Editor: a new lineage with a collaborator's text, and a tab still on
  the old lineage (traps.test.ts). Browser: the editor knows its lineage;
  two tabs typing with the network down (traps.spec.ts).

Fixes:
- The browser never learned its copy's lineage (the server sends it with
  the join, never in the document; the fake test room put it in the
  document), so the guard against merging a replaced copy was off.
- A tab on an old lineage fed its whole text into a recovering tab.
  Tabs now share changes only with tabs of the same lineage.
- After a lineage change, a collaborator's text received since the last
  save was replayed a second time: the base now follows confirmed state.
- A write that errors after the rename (folder fsync) left our own bytes
  to be merged back as an outside edit, doubling the typing.
- A retried notes.create made a second note: the id now comes from the
  idempotency key.
- Outside edits and replaces were diffed by character, sliding across
  line breaks, so a line typed at the same moment landed inside another.
  Lines first, then characters within a changed line (also much faster).
- yrs 0.28 loses a change parked behind another from the same writer and
  can place a change with a hole, after which history misses text and a
  version no longer matches its text (a stale replace then doubled it).
  The room no longer applies a client change that skips ahead; it asks
  for a resend and holds that client's Saved until it arrives. Parked
  changes are offered again after every update.

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

CI on b63e310: rust passed (fmt, clippy and every test, including the kill -9 typing test). web failed.

The new browser tests passed in CI:

  • traps.spec.ts "the editor knows which copy of the note it holds" passed in all four projects.
  • "two tabs typing while the network is down" passed in chromium-1440 and chromium-390. It is skipped in WebKit by design.

The 13 web failures look like host load, not this change:

  • 10 are in webkit-1440 and are almost all locator.click timeouts: day.spec:40, editor.spec:4/43/151, info.spec:7, and links.spec:11/34/57/101/125.
  • The other 3 are editor.spec:55 (cpu ×6) in chromium at 1440 and 390, and editor.spec:101 in chromium-390.
  • The trash PR (#98) ran on the same host at the same time and failed with the same webkit-1440 timeouts (day:40, editor:4, info:7, links:11/34/57) and the same editor:55 (cpu ×6) timeouts.
  • Locally, at a load average of about 100, I saw the same WebKit timeouts. At lower load, earlier, the same specs passed with this change: 39 passed.

The #98 run also failed editor.spec:185 "two tabs and an outside edit converge" in webkit-390 on an equality check (toBe), not a timeout. That may be a real flake, and the trap tests here are aimed at exactly that kind of failure. Worth watching.

I'd rerun web CI once the host is quieter. The forge ignores new pushes to an open PR, so a rerun needs either a rerun from the UI or a fresh branch.

CI on b63e310: **rust passed** (fmt, clippy and every test, including the kill -9 typing test). **web failed.** **The new browser tests passed in CI:** - `traps.spec.ts` "the editor knows which copy of the note it holds" passed in all four projects. - "two tabs typing while the network is down" passed in chromium-1440 and chromium-390. It is skipped in WebKit by design. **The 13 web failures look like host load, not this change:** - 10 are in webkit-1440 and are almost all `locator.click` timeouts: `day.spec:40`, `editor.spec:4/43/151`, `info.spec:7`, and `links.spec:11/34/57/101/125`. - The other 3 are `editor.spec:55 (cpu ×6)` in chromium at 1440 and 390, and `editor.spec:101` in chromium-390. - The trash PR (#98) ran on the same host at the same time and failed with the same webkit-1440 timeouts (`day:40`, `editor:4`, `info:7`, `links:11/34/57`) and the same `editor:55 (cpu ×6)` timeouts. - Locally, at a load average of about 100, I saw the same WebKit timeouts. At lower load, earlier, the same specs passed with this change: 39 passed. The #98 run also failed `editor.spec:185` "two tabs and an outside edit converge" in webkit-390 on an equality check (`toBe`), not a timeout. That may be a real flake, and the trap tests here are aimed at exactly that kind of failure. Worth watching. I'd rerun web CI once the host is quieter. The forge ignores new pushes to an open PR, so a rerun needs either a rerun from the UI or a fresh branch.
Author
Owner

Review: I read the diff for the room, ydoc, save, watch, lib and editor sync changes.

  • Server: the gap check with a resync request holds a client's save checks only while that connection lives. The write error after rename is told apart by the disk hash.
  • Create ids: the id for a create comes from a per-user hash of the idempotency key.
  • Browser lineage: the browser now learns the lineage on join, and tabs only share changes when their lineage matches. The base is debounced, and it advances on a collaborator's change only when nothing of ours is pending.

Checks after merging dev: the tests.rs mod list clashed with trash; I kept both. Then:

  • 131 calternal-notes tests pass, and so do the calternal crash tests.
  • The editor's vitest run passes 77 of 77.

Merging as #130.

Open items: the ones listed on #3 (leader tab, protocol handshake, unique block ids, NFD names and the rest) go to their own issues.

Review: I read the diff for the room, ydoc, save, watch, lib and editor sync changes. - **Server:** the gap check with a resync request holds a client's save checks only while that connection lives. The write error after rename is told apart by the disk hash. - **Create ids:** the id for a create comes from a per-user hash of the idempotency key. - **Browser lineage:** the browser now learns the lineage on join, and tabs only share changes when their lineage matches. The base is debounced, and it advances on a collaborator's change only when nothing of ours is pending. **Checks after merging dev:** the `tests.rs` mod list clashed with trash; I kept both. Then: - 131 `calternal-notes` tests pass, and so do the `calternal` crash tests. - The editor's vitest run passes 77 of 77. Merging as #130. **Open items:** the ones listed on #3 (leader tab, protocol handshake, unique block ids, NFD names and the rest) go to their own issues.
kayg closed this pull request 2026-10-14 05:02:25 +00:00
Some checks failed
CI / rust (pull_request) Successful in 3m16s
CI / web (pull_request) Failing after 8m30s

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