Skip to content

Review-state edits are made from one read and saved If-Match another, so rescheduling a card can undo a grade made meanwhile #1011

Done

Description

@ixuz

The problem

Each write of a deck's reviews document but a grade is built from a read the use case makes, then handed to applyReviewChanges or prepareReviewChanges. The adapter reads the document again and saves If-Match that second read (solidReviewStateRepository.ts:91). So the If-Match never covers the read the change was made from: the defect #997 fixed for instance metadata, the catalog and the deck entry.

Site Write What it sends How it differs
cards.ts:587 rescheduleCards Studio: set due whole states ({...state, due}), SM-2 and FSRS-6 can undo a grade, below
cards.ts:581 resetCards Studio: forget progress removals harmless: the adapter removes what it read
study.ts:276 resetStudyDay reset the day restored states and removals the answer log is written between the read and the save
cards.ts:334, cards.ts:377-392 writeCardEdit, writeCardEditReviews bulk card edit and its undo removals; an undo restores removed cards' states on a 412 sends the same changes again
cards.ts:433-512 transferOnce copy or move cards the target's states, the source's removals its comment says each write is "sent If-Match the read it was made from": true of the cards, not of the reviews
library.ts:441-451 upgrade a copy's newer release removals on a 412 sends the same changes again
study.ts:262 undoReview undo an answer the state before the answer, kept in memory no read; writing over is what it does

Each site has the same shape:

const states = await reviewStateRepository.listReviewStates(deck); // read 1, no version
const changes = planFrom(states);
await reviewStateRepository.applyReviewChanges(deck, changes);   // the adapter's read 2, If-Match read 2

The ports beside it already take a change: gradeReviewState(deck, key, grade) and writeFsrsStates(deck, edit) (ports.ts:948-997). The cards document threads its version through: readCardsSince gives one, and prepareCardChanges takes it back (wholeAt(read.version)). readReviewStatesSince gives a version too, but no port of the reviews document takes it.

I found this by reading the code; I have not reproduced it:

  1. In the Studio, the user sets a card due on a later day. rescheduleCards reads the deck's states and makes {...state, due} of each.
  2. Meanwhile the learner grades that card on a phone, which writes a new interval, ease and repetition count.
  3. The adapter reads the document, now with that grade, writes the states from step 1 over it, and saves If-Match its own read. The save succeeds.
  4. The grade is gone: the card has its old interval and ease, with the new due day, while its answer stays in the log, so the statistics and the card's state disagree.

The window is one round trip, so this is rare. resetStudyDay has a longer one, as answerStore.removeDay runs between its read and its save. The retries in the card edit and the upgrade can't lose a grade today, since they mostly send removals, which the adapter applies to its own read. But they write out by hand the retry editDocument (#995) already makes.

Your proposal

Give the reviews document the ports the cards document and the deck entry have: an edit that takes a change, built on editDocument, and the read's version on the write prepared beside the cards.

interface ReviewStateRepository {
  /** The changes `change` makes of the states as read, saved If-Match that read;
   *  read and changed again on a 412 (editDocument). null: nothing to write. */
  editReviewStates<T>(
    deck: Deck,
    change: (states: ReviewStates) => { changes: ReviewChanges; result: T } | null,
  ): Promise<T | null>;

  /** As now, sent If-Match `at`, the version the changes were made from
   *  (readReviewStatesSince), as prepareCardChanges takes wholeAt(version). */
  prepareReviewChanges(deck: Deck, changes: ReviewChanges, at: string | null): Promise<() => Promise<void>>;
}
  • rescheduleCards, resetCards and resetStudyDay become editReviewStates(deck, (states) => …). The adapter reads, changes, saves If-Match that read, and on a 412 does it again. resetStudyDay keeps writing the log first, so a failure still leaves the day to reset again.
  • writeCardEdit and transferOnce read the states with readReviewStatesSince and pass its version, so a 412 on the reviews document plans the edit again, as a 412 on the cards document does.
  • writeCardEditReviews and the upgrade's retry wrapper go. CARD_EDIT_ATTEMPTS then counts one loop, not three.
  • undoReview stays on applyReviewChanges, or becomes an edit whose change ignores what it reads: writing the state back over a later one is what an undo does.

One decision is left open. In a write of both documents, the cards are sent first. If the reviews document then answers 412, the cards can't be planned again. Either its changes are planned again alone from a fresh read (an editReviewStates of the reviews' part), or the edit fails, made in part, as a write that stops part-way does now.

Alternatives

  • Read with readReviewStatesSince at each site and pass the version to applyReviewChanges, with no change function. That closes the window, but each site still writes its own retry on a 412, and the reviews document stays the only deck document whose edits aren't made by editDocument.
  • Fix rescheduleCards alone, the one site that sends whole states. That leaves the others' shape, and the transfer's comment, as they are.

Data

  • This changes the vocabulary (ns/vocab/), the shapes (ns/shapes/) or the data the app writes to a pod.

Would you like to make it?

None

Activity

  1. added
    bug
    enhancement
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions