fix(notes,tasks): set-status unanswered after ~200 ticks: journal re-read per write; replan refusals the client can't fix (#1182) #1184

Open
kayg wants to merge 6 commits from fix/server-1182 into dev
Owner

Fixes the server side of #1182 and #1179. In the e2e runs, tasks.set-status going unanswered was not a deadlock. Every Home write got slower in proportion to every earlier write. Separately, under concurrent writes the server refused plans that the client couldn't fix.

Cause 1: each executor write re-read the whole executor journal (the "never answered" hang)

The note write executor keeps a journal record (users/<id>/notes/write-executor/*.json) for every Home transaction, including its full pre/post bytes and CRDT images. Home payload compaction is withheld (docs/implementation/note-write-executor-home.md), so the journal only grows. One tasks.set-status loaded the journal about 7 times: replay lookup, recovery ×3, admission, after commit. Each load read, parsed, re-serialised and re-hashed every record. So the cost per write was O(journal bytes), and the journal grew by one record (about 300 KB at 200 tasks) per tick.

Found by sampling stacks of the live e2e server on dev-02 (gdb -p, 8 samples): every busy thread was in serde_json inside journal::read / NoteWriteExecutor::load. The data dir had 196 records, 33 MB, after about 22 repeats.

Fix: journal::read keeps the validated records per journal directory, shared as Arc<Record>. Each entry is keyed by the record file's identity (FileStamp: inode, size, mtime, ctime), and the stamp is taken from the open handle that read the bytes.

  • Correctness: records are only ever replaced by a temp-file + rename (persist), so new bytes always come with a new identity and are parsed and validated again. The cross-record undo-ancestry check still runs on every load. Only a fully valid journal is remembered.
  • Cost: a load is now one stat per file, plus a parse of new or replaced files only.
  • Memory: the parsed records of journals in use. That is the same data each load used to build and drop, about 7 times per write. The cache is released when the Home unloads after going idle (unload.rs).
  • Callers that change a record (recover, rollback, publish after commit, compact) copy only that one record.

Cause 2: refusals the client can't fix (409 block_changed / 503 not_saved_yet under concurrent writes)

Every task's owning line lives in the same daily note, so concurrent ticks and captures all write that note.

  • 409: the executor refuses a plan, before recording anything, when a participant room, its binding or the note index changed between the request's reads and admission. Its own comment says "callers may plan again". tasks.set-status / tasks.move instead answered 409, although the client's version of the task was still right.
  • 503: a plan that read the day's note while another tick's transaction held it got 503.

Fix: status_action::execute plans again, up to 4 times (10–160 ms apart, 300 ms in total), for exactly those executor refusals (changed room revision, changed note inventory, changed binding, changed room bytes or binding) and for not_saved_yet met while planning.

  • Every new plan re-checks the client's own task version, so a real change to the task is still refused at once (test).
  • A retry after a lost answer still replays a recorded plan first.
  • A Home that keeps moving still gets the old 409/503 after 5 plans (test).

Plan notes

  • One writer, no buffer layer: the jobs mutex and the executor DataLock are unchanged. The cache holds only what the files already say, and nothing is written later than before.
  • No feature dropped: replay, undo, Trash restore, recovery and compaction read the same records as before.
  • Failure table:
Failure Effect Detection Recovery Test
Record replaced on disk New stamp, so it is re-parsed and validated FileStamp differs Same as before (refuse a corrupt journal) a_record_file_replaced_with_other_bytes_is_read_and_refused_again
Cache dropped Next load parses everything once n/a Automatic a_forgotten_journal_is_parsed_again_and_reads_the_same
Owner note changed between plan and submit Replan (≤4) Executor pre-PREPARE refusal 409 after 5 plans a_write_to_an_owner_between_plan_and_submit_is_planned_again, a_home_that_keeps_moving_is_refused_after_a_few_plans
Note busy while planning Replan (≤4) 503 not_saved_yet 503 after 5 plans a_busy_note_while_planning_is_planned_again
Stale task version from the client Refused at once, 1 plan plan_one version check Client re-reads a_stale_task_version_is_refused_without_planning_again
  • Known limit (not changed here): an outside tool rewriting a journal file in place, with the same length, inside one filesystem timestamp tick, would not be noticed. Calternal never writes journal files in place.
  • Still open, separate: the journal still grows until its 1 GiB budget, and then refuses Home writes. That needs the withheld payload-compaction policy (a reviewed retention decision).

Commits alternate spec (red) and fix where the red can be shown. The journal test is red at 0db09e0dc ("one write parsed 40 record files"). The replan test is red with REPLANS = 0, which is exactly the old single pass.

#1181: not needed for these failures any more (see the evidence comment on #1182). Its 503 retry still matches the server contract (retryable: true), but it is now rare and short.

🤖 Generated with Claude Code

Fixes the server side of #1182 and #1179. In the e2e runs, `tasks.set-status` going unanswered was not a deadlock. **Every Home write got slower in proportion to every earlier write.** Separately, under concurrent writes the server refused plans that the client couldn't fix. ## Cause 1: each executor write re-read the whole executor journal (the "never answered" hang) The note write executor keeps a journal record (`users/<id>/notes/write-executor/*.json`) for every Home transaction, including its full pre/post bytes and CRDT images. Home payload compaction is withheld (docs/implementation/note-write-executor-home.md), so the journal only grows. One `tasks.set-status` loaded the journal about 7 times: replay lookup, recovery ×3, admission, after commit. Each load read, parsed, re-serialised and re-hashed **every** record. So the cost per write was O(journal bytes), and the journal grew by one record (about 300 KB at 200 tasks) per tick. Found by sampling stacks of the live e2e server on dev-02 (`gdb -p`, 8 samples): every busy thread was in `serde_json` inside `journal::read` / `NoteWriteExecutor::load`. The data dir had 196 records, 33 MB, after about 22 repeats. **Fix:** `journal::read` keeps the validated records per journal directory, shared as `Arc<Record>`. Each entry is keyed by the record file's identity (`FileStamp`: inode, size, mtime, ctime), and the stamp is taken from the open handle that read the bytes. - **Correctness:** records are only ever replaced by a temp-file + rename (`persist`), so new bytes always come with a new identity and are parsed and validated again. The cross-record undo-ancestry check still runs on every load. Only a fully valid journal is remembered. - **Cost:** a load is now one `stat` per file, plus a parse of new or replaced files only. - **Memory:** the parsed records of journals in use. That is the same data each load used to build and drop, about 7 times per write. The cache is released when the Home unloads after going idle (`unload.rs`). - Callers that change a record (`recover`, `rollback`, publish after commit, `compact`) copy only that one record. ## Cause 2: refusals the client can't fix (409 `block_changed` / 503 `not_saved_yet` under concurrent writes) Every task's owning line lives in the same daily note, so concurrent ticks and captures all write that note. - **409:** the executor refuses a plan, before recording anything, when a participant room, its binding or the note index changed between the request's reads and admission. Its own comment says "callers may plan again". `tasks.set-status` / `tasks.move` instead answered 409, although the client's version of the task was still right. - **503:** a plan that read the day's note while another tick's transaction held it got 503. **Fix:** `status_action::execute` plans again, up to 4 times (10–160 ms apart, 300 ms in total), for exactly those executor refusals (`changed room revision`, `changed note inventory`, `changed binding`, `changed room bytes or binding`) and for `not_saved_yet` met while planning. - Every new plan re-checks the client's own task version, so a real change to the task is still refused at once (test). - A retry after a lost answer still replays a recorded plan first. - A Home that keeps moving still gets the old 409/503 after 5 plans (test). ## Plan notes - **One writer, no buffer layer:** the jobs mutex and the executor `DataLock` are unchanged. The cache holds only what the files already say, and nothing is written later than before. - **No feature dropped:** replay, undo, Trash restore, recovery and compaction read the same records as before. - **Failure table:** | Failure | Effect | Detection | Recovery | Test | |---|---|---|---|---| | Record replaced on disk | New stamp, so it is re-parsed and validated | `FileStamp` differs | Same as before (refuse a corrupt journal) | `a_record_file_replaced_with_other_bytes_is_read_and_refused_again` | | Cache dropped | Next load parses everything once | n/a | Automatic | `a_forgotten_journal_is_parsed_again_and_reads_the_same` | | Owner note changed between plan and submit | Replan (≤4) | Executor pre-PREPARE refusal | 409 after 5 plans | `a_write_to_an_owner_between_plan_and_submit_is_planned_again`, `a_home_that_keeps_moving_is_refused_after_a_few_plans` | | Note busy while planning | Replan (≤4) | 503 `not_saved_yet` | 503 after 5 plans | `a_busy_note_while_planning_is_planned_again` | | Stale task version from the client | Refused at once, 1 plan | `plan_one` version check | Client re-reads | `a_stale_task_version_is_refused_without_planning_again` | - **Known limit (not changed here):** an outside tool rewriting a journal file in place, with the same length, inside one filesystem timestamp tick, would not be noticed. Calternal never writes journal files in place. - **Still open, separate:** the journal still grows until its 1 GiB budget, and then refuses Home writes. That needs the withheld payload-compaction policy (a reviewed retention decision). Commits alternate spec (red) and fix where the red can be shown. The journal test is red at `0db09e0dc` ("one write parsed 40 record files"). The replan test is red with `REPLANS = 0`, which is exactly the old single pass. **#1181:** not needed for these failures any more (see the evidence comment on #1182). Its 503 retry still matches the server contract (`retryable: true`), but it is now rare and short. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Red: with 40 committed records, one more write parses every record file
several times over (admission, recovery, publication), so each write costs
O(journal bytes). Adds a test-only per-directory parse counter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every Home write loaded the whole executor journal about seven times
(replay lookup, recovery, admission, publication), and each load parsed,
re-serialized and re-hashed every record. Payload compaction is withheld,
so the journal only grows: after ~200 task ticks (33 MB of records) one
tasks.set-status took longer than the client's 20 s wait, and every later
tick in the e2e run went unanswered.

journal::read now keeps the validated records per journal directory,
keyed by the file's identity (inode, size, times, taken from the open
handle). Records are only ever replaced by rename, so new bytes always
mean a new identity and are parsed and validated again; the ancestry
check still runs over all records. Records are shared as Arc<Record>;
the few paths that change a record copy that one. The cache is released
when the Home unloads after going idle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The write executor refuses a plan, before recording anything, when a
participant room, its binding or the note index changed between the
request's reads and admission. Its own comment says callers may plan
again; tasks.set-status/move instead answered every such refusal as
409 block_changed. The client's version of the task was still right
(the change was usually another capture appending to the same daily
note), so the tick failed for nothing under concurrent writes.

execute now plans again up to 4 times (10-160 ms apart) for exactly
those refusals. Each plan re-checks the client's task version, so a
real change to the task is still refused at once, and a retry after a
lost answer still replays a recorded plan first. Tests: an owner
write between plan and submit is planned again (red with REPLANS=0);
a Home that keeps moving is refused after 5 plans; a stale task
version is refused after one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fix(tasks): plan again when a note read while planning is busy (#1182)
Some checks failed
CI / server (pull_request) Has been cancelled
CI / rust (pull_request) Has been cancelled
CI / web (pull_request) Has been cancelled
Markdown extraction contract / markdown-native-contract (pull_request) Has been cancelled
Markdown extraction contract / markdown-editor-contract (pull_request) Has been cancelled
3e51db3ac0
With two ticks at once, the second one's plan reads the day's note while
the first one's transaction holds it, and the room answers 503
not_saved_yet ("note action in progress; retry"). That went straight
to the client. The same bounded replan loop now covers it: a note busy
for a few milliseconds is waited out, one that stays busy still gets
the 503. Seen on dev-02 with --workers=2 (1 of 80 ticks).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Some checks failed
CI / server (pull_request) Has been cancelled
CI / rust (pull_request) Has been cancelled
CI / web (pull_request) Has been cancelled
Markdown extraction contract / markdown-native-contract (pull_request) Has been cancelled
Markdown extraction contract / markdown-editor-contract (pull_request) Has been cancelled
This pull request is broken due to missing fork information.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'fix/server-1182:fix/server-1182'
git switch 'fix/server-1182'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'dev'
git merge --no-ff 'fix/server-1182'
git switch 'fix/server-1182'
git rebase 'dev'
git switch 'dev'
git merge --ff-only 'fix/server-1182'
git switch 'fix/server-1182'
git rebase 'dev'
git switch 'dev'
git merge --no-ff 'fix/server-1182'
git switch 'dev'
git merge --squash 'fix/server-1182'
git switch 'dev'
git merge --ff-only 'fix/server-1182'
git switch 'dev'
git merge 'fix/server-1182'
git push origin 'dev'
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!1184
No description provided.