notes_crash_typing: file differs from the note right after notes.save #289

Closed
opened 2026-10-14 14:00:41 +00:00 by kayg · 1 comment
Owner

cargo test -p calternal --test notes_crash_typing failed once on a train branch at notes_crash_typing.rs:392: "after a save, the file is exactly the note". The disk differed from the text right after notes.save answered 200. It passed 3 of 3 when run alone.

The question: does write_unless_changed (#264), its swap-back and retry, or anything else let notes.save answer 200 before the file holds the note's text?

Owned by a Claude agent. Findings below.

`cargo test -p calternal --test notes_crash_typing` failed once on a train branch at `notes_crash_typing.rs:392`: "after a save, the file is exactly the note". The disk differed from the text right after `notes.save` answered 200. It passed 3 of 3 when run alone. The question: does `write_unless_changed` (#264), its swap-back and retry, or anything else let `notes.save` answer 200 before the file holds the note's text? Owned by a Claude agent. Findings below.
Author
Owner

This is a test race, not a save bug, and #264 is not involved.

Reproduction

I ran the test binary in a loop (40 runs at a time, under load from other agents):

  • Failure rate: 6 of 40, then 11 of 40, 3 of 12 and 3 of 8. Always line 392.
  • What fails: in every failing run, text (from notes.get) has no frontmatter, and the file has calternal-id / date / last edited / title followed by exactly text.
  • Instrumented runs, all 17 failures:
    • disk == text is false;
    • disk == notes.get after the save is true;
    • the note after the save, minus its frontmatter, equals text.

Why

The test's 3 tabs type without pausing for the 15 ms save delay, and the server is killed every 60–300 ms. In some runs the note is never saved during the whole session, so it has none of the keys the server owns yet. The test then:

  1. reads text with notes.get;
  2. rejoins the tabs (meanwhile the first auto-save may run);
  3. calls notes.save;
  4. compares the file with text.

The first save stamps calternal-id, date, last edited and title into the note, by design (ADR 0025 / the save recipe). The note, the history and the file all hold the stamped text. Only the test's text snapshot is out of date. The same race also shows up as "c never converged": the auto-save lands between the read and a tab's rejoin, and the tab then gets the frontmatter.

Proof that #264's swap-back and retry path played no part

I copied the data folder of 3 failing runs right before the final save and read history.db:

  • 2,744 updates, from the user plus 4 server updates (the key stamps, the last 4 entries);
  • no outside updates (a swap-back always leads to an outside ingest);
  • base_hash was set and no save was pending.

So the file was written by one normal save (the stamps), and notes.save answered 200 with the file equal to the note.

Fix (test only)

Save before reading text, so the keys are stamped before any comparison. The rest of the test is unchanged.

Something to look at separately (no data at risk)

The room saves only after flush_quiet of quiet. Production waits 750 ms, so people typing continuously in one note delay the file save until they pause. History has everything, so nothing is at risk. But the file on disk can lag behind for as long as the typing lasts. A maximum delay might be worth adding.

Result

With the test fix, 100 of 100 runs pass in the same loop. Before the fix, about 1 run in 5 failed.

**This is a test race, not a save bug, and #264 is not involved.** ### Reproduction I ran the test binary in a loop (40 runs at a time, under load from other agents): - **Failure rate:** 6 of 40, then 11 of 40, 3 of 12 and 3 of 8. Always line 392. - **What fails:** in every failing run, `text` (from `notes.get`) has **no frontmatter**, and the file has `calternal-id / date / last edited / title` followed by exactly `text`. - **Instrumented runs, all 17 failures:** - `disk == text` is false; - `disk == notes.get after the save` is true; - the note after the save, minus its frontmatter, equals `text`. ### Why The test's 3 tabs type without pausing for the 15 ms save delay, and the server is killed every 60–300 ms. In some runs the note is **never saved** during the whole session, so it has none of the keys the server owns yet. The test then: 1. reads `text` with `notes.get`; 2. rejoins the tabs (meanwhile the first auto-save may run); 3. calls `notes.save`; 4. compares the file with `text`. The first save stamps `calternal-id`, `date`, `last edited` and `title` into the note, by design (ADR 0025 / the save recipe). The note, the history and the file all hold the stamped text. Only the test's `text` snapshot is out of date. The same race also shows up as "`c` never converged": the auto-save lands between the read and a tab's rejoin, and the tab then gets the frontmatter. ### Proof that #264's swap-back and retry path played no part I copied the data folder of 3 failing runs right before the final save and read history.db: - 2,744 updates, from the user plus **4 `server` updates (the key stamps, the last 4 entries)**; - **no `outside` updates** (a swap-back always leads to an `outside` ingest); - `base_hash` was set and no save was pending. So the file was written by one normal save (the stamps), and `notes.save` answered 200 with the file equal to the note. ### Fix (test only) Save before reading `text`, so the keys are stamped before any comparison. The rest of the test is unchanged. ### Something to look at separately (no data at risk) The room saves only after `flush_quiet` of quiet. Production waits 750 ms, so people typing continuously in one note delay the file save until they pause. History has everything, so nothing is at risk. But the file on disk can lag behind for as long as the typing lasts. A maximum delay might be worth adding. ### Result With the test fix, **100 of 100** runs pass in the same loop. Before the fix, about 1 run in 5 failed.
kayg closed this issue 2026-10-14 14:40:34 +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#289
No description provided.