Delete and restore: answer before the last flush; no watcher stalls (#125) #253

Closed
kayg wants to merge 10 commits from issue/125-fast-delete into dev
Owner

Closes #125.

Delete and restore were slow for three reasons, and only one of them was the job's last flush:

  1. The job's last save waited for the disk. A job now answers once its steps are applied; its "done" save is queued as a lazy history write that rides along with the next write's flush (or goes alone after 100 ms). Reading a job waits for such a save if it is still in flight, so nobody sees a finished job as running (undo right after a delete works). A failed lazy save is retried once in the background; if the server dies first, the job reads running on restart and bulk::resume replays it, where every step is already idempotent.
  2. The watcher folded every indexed path for every deleted file, holding the index lock (10k names::fold calls per vanished path, every 150 ms batch). That was the 100–150 ms p95 spikes. Index::under now compares the stored lookup keys instead.
  3. Index lines nobody waits for (delete's and restore's) were flushed at once, competing with the flushes the caller does wait for. Index::put_soon / remove_soon let them wait up to 100 ms for company (lost to a crash, the startup scan already repairs them, as before).

Also: a delete reads the note's history only when the note was never saved, and the trash bench now measures rename and make-note too and waits for a new Home's background work before measuring.

kill -9 test (crates/notes/src/tests/kill9.rs): the test re-runs its own binary as a child, which sets notes up, arms a hold that stops history just before the job's "done" save, runs the job, prints the answer and waits. The parent reads the answer, sends SIGKILL, checks in history.db that the job is still running (the save never landed), reopens the data and checks the job finishes on restart with nothing lost and nothing twice — for delete (and undo after), restore, rename (link repointed once) and make-note (one new note, source link once).

Before/after numbers from the perf VM are on #125.

🤖 Generated with Claude Code

https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T

Closes #125. Delete and restore were slow for three reasons, and only one of them was the job's last flush: 1. **The job's last save waited for the disk.** A job now answers once its steps are applied; its "done" save is queued as a *lazy* history write that rides along with the next write's flush (or goes alone after 100 ms). Reading a job waits for such a save if it is still in flight, so nobody sees a finished job as running (undo right after a delete works). A failed lazy save is retried once in the background; if the server dies first, the job reads `running` on restart and `bulk::resume` replays it, where every step is already idempotent. 2. **The watcher folded every indexed path for every deleted file**, holding the index lock (10k `names::fold` calls per vanished path, every 150 ms batch). That was the 100–150 ms p95 spikes. `Index::under` now compares the stored lookup keys instead. 3. **Index lines nobody waits for** (delete's and restore's) were flushed at once, competing with the flushes the caller does wait for. `Index::put_soon` / `remove_soon` let them wait up to 100 ms for company (lost to a crash, the startup scan already repairs them, as before). Also: a delete reads the note's history only when the note was never saved, and the trash bench now measures rename and make-note too and waits for a new Home's background work before measuring. **kill -9 test** (`crates/notes/src/tests/kill9.rs`): the test re-runs its own binary as a child, which sets notes up, arms a hold that stops history just before the job's "done" save, runs the job, prints the answer and waits. The parent reads the answer, sends SIGKILL, checks in `history.db` that the job is still `running` (the save never landed), reopens the data and checks the job finishes on restart with nothing lost and nothing twice — for delete (and undo after), restore, rename (link repointed once) and make-note (one new note, source link once). Before/after numbers from the perf VM are on #125. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
kayg added 10 commits 2026-10-14 11:41:15 +00:00
A job's final "done" save is queued to the history writer instead of
awaited: every step is applied and the plan is on disk, so a crash
before it lands leaves the job "running" and the restart replays it,
where every step sees it already happened. Writes after it are stored
after it, and reading a job waits for a queued save that is still in
flight, so nobody sees a finished job as running. A save that fails is
retried once in the background.

kill -9 tests run the job in a child process, kill it right after the
answer with the save held back, check the save really never landed,
and reopen: delete, restore, rename and make-note each finish once,
nothing lost, nothing twice.

The trash bench now also measures rename and make-note.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
A delete read the note's whole history to learn whether it was ever
typed in, but only a note made and never saved needs to know.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
A finished job's last save no longer costs a flush of its own: the
writer holds it up to 100 ms to share the next transaction (usually the
next job's start), keeping order. The writer loop moves to its own file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
After the first scan, the note list re-reads every new note in the
background; the bench measured deletes during that, which measured the
disk queue. It now waits until the process stops reading files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
Each file that went away made the watcher fold all 10,000 indexed paths
while holding the index lock, so a burst of deletes stalled every other
use of the index (including the deletes themselves) for tens of ms. It
now compares the stored lookup keys instead (Index::under).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
Delete and restore never wait for their id-index line; it used to be
flushed at once, competing with the flushes they do wait for. Such lines
(Index::put_soon, remove_soon) now wait up to 100 ms for company.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
# Conflicts:
#	crates/notes/src/history.rs
Merge dev: make_note takes a time zone; links live in the list store (#125)
Some checks failed
CI / web (pull_request) Failing after 17s
CI / rust (pull_request) Successful in 1m47s
5c0ebc20bc
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
Author
Owner

Closed by kayg (2026-10-16). Codex will re-measure delete/restore on current dev and redo whatever still helps (#125).

Closed by kayg (2026-10-16). Codex will re-measure delete/restore on current dev and redo whatever still helps (#125).
kayg closed this pull request 2026-10-16 03:57:35 +00:00
Some checks failed
CI / web (pull_request) Failing after 17s
CI / rust (pull_request) Successful in 1m47s

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