Repository navigation
Description
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 spellingsThe 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:
- Three ways to turn a version into a write condition:
wholeAt,reviewsAt, and an inline copy at library.ts:467. - Two names for one test:
cardsChangedElsewhereandracedhave the same body, and both sit besidechangedElsewhere.
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 };writeCardEdituses the defaults.transferCardspassesreplanAfterSent: true. This is safe because planCardTransfer marks a card already in the target aspresent.- The upgrade passes
attempts: 1. It usessentin place of its ownchanged/write()and keeps its step-by-step progress. importDeckFilepassesreplanOnforcreatedElsewhereandattempts: PLACE_ATTEMPTS. Its plan adds the taken id.finishCutOffImportstays as it is.editReleaseDraftkeeps its own loop, since it merges the try that was cut off. It uses only the shared test, constant and pause.wholeAt,reviewsAtand the inline copy becomeatVersion.cardsChangedElsewhereandracedbecome one predicate inwrites.ts.
Harmonized retry counts, with one meaning per constant:
EDIT_ATTEMPTS = 3for a document that changed since it was read. BotheditDocumentand the application's loops use it.solidmay importapplication, so it lives there;npm run check:boundariesdecides which entry point it goes through.PLACE_ATTEMPTS = 3for 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_ATTEMPTSin 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)inwrites.ts: a random wait between 0 andbase × 2^(attempt − 2), with a base of tens of milliseconds, so a conflict between two tabs or devices breaks up rather than repeating. writeInOrderandeditDocument(as its defaultpause) 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
- Only harmonize the counts and add the pause, keeping the loops. This is a smaller change, but the next use case that writes several documents copies the loop again.
- Move these writes into the adapters, as Use cases edit instance metadata, the catalog and a deck's entry through a read port and a separate save port, so the If-Match never covers the read the change was made from #997 did for single documents. They span the cards and reviews documents and plan between tries using domain rules (
planCardTransfer,planLibraryUpgrade), so the plan belongs in the application layer. - Leave it. Every loop works and is covered. The cost is a policy spread over five places, and retries that collide again because nothing pauses between them.
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