Skip to content

Use cases that write several documents each write out their own re-plan loop, with retry counts that differ for no stated reason and no pause between tries #1086

Done

Description

@ixuz

The problem

After #995, #997 and #1011, an edit of one document has one write policy: editDocument in datasets.ts. Writes that span several documents were left out of #997 on purpose: they plan across two documents and pass on the versions they read. Each one still writes out the same loop by hand. It plans from versioned reads, prepares every write (made and checked, none sent), sends them in a fixed order, and plans again on a 412.

Where Writes, in order Plans again on Tries Records what was sent with
cards.ts:325 writeCardEdit cards, reviews 412, only before the cards are sent CARD_EDIT_ATTEMPTS = 3 a cardsWritten callback
cards.ts:598 transferCards → transferOnce target cards, target reviews, source reviews, source cards 412, also after writes were sent CARD_EDIT_ATTEMPTS nothing (planning again finishes the transfer)
library.ts:681 applyLibraryUpgrade cards, reviews, entry never 1 a changed flag and a write() wrapper over unsent
cards.ts:637 importDeckFile cards (absent), reviews, entry createdElsewhere, with a new deck id 2, inline a written list
releaseDrafts.ts:854 editReleaseDraft one repository write 412 EDIT_ATTEMPTS = 3 cut, merged on the next try

The same lines recur:

const writeX = cond ? await repo.prepareXChanges(deck, changes, wholeAt(version)) : noWrite; // 9 times: noWrite, undefined or null
if (!cardsChangedElsewhere(error) || attempt === CARD_EDIT_ATTEMPTS) throw error;            // 4 times, in two spellings

The retry counts drift. The table below lists every retry count in the app:

Count Where Means
3 editDocument (datasets.ts:500), CARD_EDIT_ATTEMPTS (cards.ts:240), EDIT_ATTEMPTS (releaseDrafts.ts:233) the document changed since it was read (412)
3 and 2 NAME_ATTEMPTS (releaseDrafts.ts:236), the import's inline attempt === 2 (cards.ts:663) the place is taken, so the next name or id is used
6, with a growing pause the digest (solidDigestRepository.ts:48) 412 while another page writes the digest several times in a row
1 the upgrade the learner approved the plan they were offered, so it is never re-planned silently

Only the last two have a reason written beside them. The two "place taken" loops are the same kind of retry with different counts, and nothing says why the import's count is 2.

Other policy is repeated too:

No pause between tries. Every 412 loop but the digest's tries again at once. Two tabs or devices that conflict once are likely to conflict again on the very next try.

Retries may stack. A transfer re-plans up to 3 times, even after some writes were sent. Once the cards are sent, the port docs say a 412 on the reviews document is retried by the adapter, up to 3 tries. So in the worst case one reviews document could be tried 9 times. This is worked out from the port docs, not checked in the adapter.

So a change to the policy, such as a count or a pause, has to be made in up to five places, and each use case tests its own 412 path. I found no defect a learner would notice. The cost is the drift.

Your proposal

One ordered write set, in writes.ts. The loop stays in the application layer, since these writes span documents, but it is written once:

interface WriteSet<T> {
  result: T;
  /** Made and checked, not sent, in the order they are sent; undefined: nothing to write. */
  writes: readonly ((() => Promise<void>) | undefined)[];
}

interface WriteSetPolicy {
  attempts?: number;                       // default EDIT_ATTEMPTS
  replanOn?: (error: unknown) => boolean;  // default: changedElsewhere or createdElsewhere
  replanAfterSent?: boolean;               // default false: only while nothing went out
}

export function writeInOrder<T>(
  plan: (attempt: number) => Promise<WriteSet<T>>,
  policy?: WriteSetPolicy,
): Promise<{ result: T; sent: number }>;   // a failure carries `sent`, beside unsent()

export function atVersion(version: string | null): { version?: string };
  • writeCardEdit uses the defaults.
  • transferCards passes replanAfterSent: true. This is safe because planCardTransfer marks a card already in the target as present.
  • The upgrade passes attempts: 1. It uses sent in place of its own changed/write() and keeps its step-by-step progress.
  • importDeckFile passes replanOn for createdElsewhere and attempts: PLACE_ATTEMPTS. Its plan adds the taken id. finishCutOffImport stays as it is.
  • editReleaseDraft keeps its own loop, since it merges the try that was cut off. It uses only the shared test, constant and pause.
  • wholeAt, reviewsAt and the inline copy become atVersion.
  • cardsChangedElsewhere and raced become one predicate in writes.ts.

Harmonized retry counts, with one meaning per constant:

  • EDIT_ATTEMPTS = 3 for a document that changed since it was read. Both editDocument and the application's loops use it. solid may import application, so it lives there; npm run check:boundaries decides which entry point it goes through.
  • PLACE_ATTEMPTS = 3 for a place taken elsewhere, used by the draft name and the import's deck id. The import goes from 2 to 3.
  • Two named exceptions, each with its reason written beside it: the digest's 6 tries with a pause, and the upgrade's single try.
  • AT_FAULT_ATTEMPTS in answerStore.ts:95 stays as it is. It counts how often the pod refuses an answer, not 412s.

A jittered exponential pause between tries after a 412:

  • One pauseBefore(attempt) in writes.ts: a random wait between 0 and base × 2^(attempt − 2), with a base of tens of milliseconds, so a conflict between two tabs or devices breaks up rather than repeating.
  • writeInOrder and editDocument (as its default pause) both use it.
  • The digest keeps its larger count and uses the same pause.
  • The waits stay short: a learner should never notice them.

One change per commit: the predicate and constants first, then the pause, then each use case moved to writeInOrder. Nothing the app writes to a pod changes. Each use case's 412 tests move to writes.test.ts, apart from its own rules (order, present, sameCardChanges).

Before choosing the base and the count, check in the reviews adapter whether retries stack (transfer re-plan × the adapter's retry).

Alternatives

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?

Yes, I would like to open a pull request

Activity

  1. added
    enhancement
    on Nov 28, 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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions