Opt-in per-field base check for set-to-state actions; notes unchanged (shared, from mail plan) #919

Open
opened 2026-11-04 09:02:23 +00:00 by kayg · 5 comments
Owner

Revision 3, after reviews 77537 and 77560 (and #920 N-3).

What changes for notes

Nothing. Notes keep their merging per-note base version. Room/CRDT edits are not actions. This is an opt-in helper for set-to-state actions only.

Problem

Idempotency records expire after 7 days. A late retry of a set-to-state action (archive, mark read) can undo a newer change made elsewhere: archive → moved back on the web → a late retry archives it again.

Rule

  • An action carries:
    • base: the token at which the device last saw its targets. Required for outbox replays, optional for online adapters.
    • after: [op ids]: the device's own unconfirmed ops on these targets.
  • Per field (read, flagged, each label, placement), the plugin keeps field_seq (set to the save-log head when the value changes, never otherwise) and field_req (the op that made that change).
  • Current value and last change include pending actions (accepted, not yet applied), with their seqs. Rows rebuilt from scratch get field_seq = head at rebuild; seqs stamped before an epoch rotation count as "head at rotation". So after a cache rebuild every value-changing queued action is stale (safe).
  • For each field the action sets:
    • current = requested → success, no change;
    • else, if field_seq ≥ base (or base is from an older epoch) and field_req ∉ after → stale. A stale caused only by an older-epoch base doesn't clear the outbox: the device re-stamps the op with the new token and replays it (#918);
    • else → apply.
  • A device's own unconfirmed change never makes its next one stale. Another tab's change is never in after, so the undo stays blocked.
  • Outbox merging touches only ops not yet sent. A sent op keeps its key and payload. A merged op keeps the oldest base of its parts. A merge that ends at the original value is dropped only if none of its parts was sent.

Tests

  • Flag racing mark-read: both apply.
  • Two devices mark read: no stale.
  • Lost reply, then the opposite toggle: applies.
  • Stale-base action racing a pending action before the delta: stale.
  • Two tabs + retry after the prune: no undo.
  • Rebuild, then a late retry after a pre-rebuild change: stale.
  • Notes patch with a concurrent edit: still merges.
**Revision 3, after reviews 77537 and 77560 (and #920 N-3).** ## What changes for notes **Nothing.** Notes keep their merging per-note `base` version. Room/CRDT edits are not actions. This is an opt-in helper for set-to-state actions only. ## Problem Idempotency records expire after 7 days. A late retry of a set-to-state action (archive, mark read) can undo a newer change made elsewhere: archive → moved back on the web → a late retry archives it again. ## Rule - An action carries: - `base`: the token at which the device last saw its targets. Required for outbox replays, optional for online adapters. - `after: [op ids]`: the device's **own unconfirmed** ops on these targets. - Per field (`read`, `flagged`, each label, placement), the plugin keeps `field_seq` (set to the save-log head when the *value* changes, never otherwise) and `field_req` (the op that made that change). - **Current value and last change include pending actions** (accepted, not yet applied), with their seqs. Rows rebuilt from scratch get `field_seq` = head at rebuild; seqs stamped before an epoch rotation count as "head at rotation". So after a cache rebuild every *value-changing* queued action is `stale` (safe). - For each field the action sets: - current = requested → success, no change; - else, if `field_seq ≥ base` (or `base` is from an older epoch) and `field_req ∉ after` → `stale`. A `stale` caused **only** by an older-epoch base doesn't clear the outbox: the device re-stamps the op with the new token and replays it (#918); - else → apply. - A device's own unconfirmed change never makes its next one `stale`. Another tab's change is never in `after`, so the undo stays blocked. - **Outbox merging** touches only ops not yet sent. A sent op keeps its key and payload. A merged op keeps the oldest `base` of its parts. A merge that ends at the original value is dropped only if none of its parts was sent. ## Tests - Flag racing mark-read: both apply. - Two devices mark read: no `stale`. - Lost reply, then the opposite toggle: applies. - Stale-base action racing a pending action before the delta: `stale`. - Two tabs + retry after the prune: no undo. - Rebuild, then a late retry after a pre-rebuild change: `stale`. - Notes patch with a concurrent edit: still merges.
Author
Owner

Review note from the mail v4 review (full text on #920, finding P1-3). As written, the rule has three problems:

  1. Whole-target staleness refuses actions that commute. A flag racing a mark-read, a provider push that only adds a label, and two devices both marking read all come back stale. A cache.db rebuild re-stamps every row, so every queued offline action goes stale.
  2. The same-Installation exemption leaves the undo it should close. Web tabs share an Installation. A tab retries its own old "archive" after the 7-day receipt prune, it's exempt, and it undoes the other tab's newer un-archive.
  3. It must not touch notes. Notes use a per-note version base: i64 that merges (notes.patch/replace), and room/CRDT edits aren't actions at all. An executor-wide check would refuse note edits that merge today.

Proposed narrowest rule:

  • Make it an opt-in helper for set-to-state actions only.
  • Track seq per field, bumped only when the value really changes.
  • A target is stale only if its field changed after base and the current value ≠ the requested value. If they're equal, return success with no change.
  • No Installation exemption. The device outbox coalesces per (target, field), so "toggle N times offline" sends one state.
  • A base from an older epoch counts as "changed".
  • base is optional for online adapters (CLI, MCP, bulk jobs).

Extra tests:

  • Flag vs read race: both apply.
  • Two devices mark read: no stale.
  • Two tabs plus a retry after the prune: no undo.
  • Cache rebuild, then an offline action whose value is unchanged: applies.
  • Notes patch with a concurrent edit: still merges.
Review note from the mail v4 review (full text on #920, finding P1-3). As written, the rule has three problems: 1. **Whole-target staleness refuses actions that commute.** A flag racing a mark-read, a provider push that only adds a label, and two devices both marking read all come back `stale`. A `cache.db` rebuild re-stamps every row, so every queued offline action goes stale. 2. **The same-Installation exemption leaves the undo it should close.** Web tabs share an Installation. A tab retries its own old "archive" after the 7-day receipt prune, it's exempt, and it undoes the other tab's newer un-archive. 3. **It must not touch notes.** Notes use a per-note version `base: i64` that *merges* (`notes.patch`/`replace`), and room/CRDT edits aren't actions at all. An executor-wide check would refuse note edits that merge today. **Proposed narrowest rule:** - Make it an opt-in helper for set-to-state actions only. - Track `seq` per *field*, bumped only when the value really changes. - A target is `stale` only if its field changed after `base` **and** the current value ≠ the requested value. If they're equal, return success with no change. - No Installation exemption. The device outbox coalesces per `(target, field)`, so "toggle N times offline" sends one state. - A `base` from an older epoch counts as "changed". - `base` is optional for online adapters (CLI, MCP, bulk jobs). Extra tests: - Flag vs read race: both apply. - Two devices mark read: no `stale`. - Two tabs plus a retry after the prune: no undo. - Cache rebuild, then an offline action whose value is unchanged: applies. - Notes patch with a concurrent edit: still merges.
kayg changed title from Actions name their base save-log position; stale retries never undo newer changes (shared, from mail plan R-2) to Opt-in per-field base check for set-to-state actions; notes unchanged (shared, from mail plan) 2026-11-04 09:15:02 +00:00
Author
Owner

Revised the issue body per 77537: opt-in, per field, stale only if the field changed since base and the current value differs; no Installation exemption (the outbox coalesces); notes behaviour unchanged.

Revised the issue body per 77537: opt-in, per field, stale only if the field changed since base and the current value differs; no Installation exemption (the outbox coalesces); notes behaviour unchanged.
Author
Owner

Second review (full text on #920, finding N-3). This issue needs changes:

  • Own unconfirmed ops. Example: mark read is accepted at seq 70 but the reply is lost, then mark unread goes out with base 69. It comes back stale about the person's own change. Fix: actions carry after: [op ids], the device's unconfirmed ops on those targets. Keep field_req (the request id of the last change) next to each field seq. A change after base by an op in after doesn't count. Another tab's change is never in that list, so the undo stays blocked.
  • Coalescing touches only the unsent tail. A sent op keeps its key and payload. The merged op uses the oldest base of its parts. A merge back to the original value is dropped only if no part was sent.
  • Current/changed = cache.db ⊕ pending mail.acted (with their seq). The cache only changes on the next delta.
  • Rebuilt rows get field_seq = head at rebuild, not 0.
  • Tests to add: lost reply then the opposite toggle (applies, not stale); a stale-base action racing a pending action before the delta (stale); rebuild then a late retry after a pre-rebuild change (stale).
Second review (full text on #920, finding N-3). This issue needs changes: - **Own unconfirmed ops.** Example: mark read is accepted at seq 70 but the reply is lost, then mark unread goes out with `base` 69. It comes back `stale` about the person's own change. Fix: actions carry `after: [op ids]`, the device's unconfirmed ops on those targets. Keep `field_req` (the request id of the last change) next to each field seq. A change after `base` by an op in `after` doesn't count. Another tab's change is never in that list, so the undo stays blocked. - **Coalescing** touches only the unsent tail. A sent op keeps its key and payload. The merged op uses the oldest `base` of its parts. A merge back to the original value is dropped only if no part was sent. - **Current/changed** = `cache.db` ⊕ pending `mail.acted` (with their seq). The cache only changes on the next delta. - **Rebuilt rows** get `field_seq` = head at rebuild, not 0. - Tests to add: lost reply then the opposite toggle (applies, not `stale`); a stale-base action racing a pending action before the delta (`stale`); rebuild then a late retry after a pre-rebuild change (`stale`).
Author
Owner

Revision 3 per 77560: after: [own unconfirmed op ids] + field_req; pending actions count in current/changed; rebuilt rows take the head; merging only of unsent ops. Notes unchanged.

Revision 3 per 77560: `after: [own unconfirmed op ids]` + `field_req`; pending actions count in current/changed; rebuilt rows take the head; merging only of unsent ops. Notes unchanged.
Author
Owner

Per final review 77574 F-1/P3-2: a stale caused only by an older-epoch base doesn't clear the outbox, and the op is re-stamped and replayed. Seqs stamped before a rotation count as head at rotation, and after a cache rebuild every value-changing queued action is stale (safe).

Per final review 77574 F-1/P3-2: a `stale` caused only by an older-epoch base doesn't clear the outbox, and the op is re-stamped and replayed. Seqs stamped before a rotation count as head at rotation, and after a cache rebuild every value-changing queued action is `stale` (safe).
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#919
No description provided.