Skip to content

refactor(application): one ordered write set, one retry count, and a random pause before a 412 is tried again - #1088

Merged
ixuz merged 3 commits into
mainfrom
write-set
Nov 28, 2026
Merged

ixuz merged 3 commits into
mainfrom
write-set

Conversation

@ixuz

@ixuz ixuz commented Nov 28, 2026

Copy link
Copy Markdown
Member

What and why

A bulk edit of cards, a transfer of cards, a library upgrade and a deck file's import each wrote out the same loop by hand. Each one plans from versioned reads, makes and checks every write, sends them in a fixed order, and plans again on a 412. The retry counts had drifted, with nothing written down to say why: 3 in three places, 2 for the import, and two names for the same 412 test. Every loop except the digest's tried again at once, so two tabs or devices whose writes clashed once were likely to clash again on the next try.

Three commits:

  • One test and two counts (writes.ts). changedOrCreatedElsewhere replaces cardsChangedElsewhere and raced. EDIT_ATTEMPTS = 3 is for a document that changed since it was read, and editDocument in solid now uses it too. PLACE_ATTEMPTS = 3 is for a place taken elsewhere: a new draft's name, or an imported deck's id, which goes from 2 tries to 3. The digest's 6 tries and the upgrade's single try keep their reasons in a comment beside them.

  • A random, growing pause before a 412 is tried again. pauseBefore waits up to 25 ms before the second try, and up to twice as long before each try after it, short enough that a learner never notices. editDocument waits it by default, and so do the use cases that plan again (the bulk edit, the transfer, a draft's edit); createUseCases injects it so their tests don't wait. The digest keeps its old growing wait as a minimum and adds the random part on top, so no wait is shorter than before.

  • writeInOrder and atVersion. The bulk edit, transfer, upgrade and import now run the loop through writeInOrder, each with its own policy:

    • The bulk edit uses the defaults: it plans again only while nothing has been sent.
    • The transfer plans again even after some writes were sent. This is safe because a card the target already has is not written again.
    • The upgrade tries once, since the learner approved its plan. It works out whether the deck changed from how far the set got (cutOffOf), and still reports its steps.
    • The import plans again, without a pause, only when its deck's place was taken.
    • A draft's edit keeps its own loop, which merges the try that was cut short.

    wholeAt, reviewsAt and the inline copy in library.ts become atVersion.

A failed write set throws the error it failed with, unchanged, so the screens' messages stay as they are. A bulk edit or undo whose preview no longer fits the cards stops at once, as it does today, instead of planning again.

Nothing the app writes to a pod changes. The 412 policy is tested once, in writes.test.ts. Each use case keeps the tests of its own rules, and the transfer gains a test that plans again after the target was written. docs/data-model.md (write discipline) and docs/studio.md describe the new counts and the pause.

One thing for review: one reviews document can be tried up to 12 times in a transfer. The adapter sends one If-Match PUT, then falls back to editDocument's 3 tries, and the transfer plans again up to 3 times: 3 × (1 + 3). This branch doesn't change that. Counting the adapter's first PUT as its first try would bring it down to 9.

npm run check passes on each commit.

Closes: #1086

ixuz added 3 commits November 28, 2026 10:21
The use cases tested a 412 under two names with one body,
cardsChangedElsewhere in cards.ts and raced in releaseDrafts.ts, and
counted their tries with three constants that meant the same: the
cards' CARD_EDIT_ATTEMPTS, the drafts' EDIT_ATTEMPTS and editDocument's
own. A change to the policy had to be made in each.

writes.ts now holds changedOrCreatedElsewhere, EDIT_ATTEMPTS for a
document that changed since it was read, and PLACE_ATTEMPTS for a
place taken elsewhere. editDocument takes EDIT_ATTEMPTS from there,
which solid may import. The draft's name and the imported deck's id
are the same kind of retry, so both use PLACE_ATTEMPTS: the import
goes from two tries to three, as nothing said why it had one fewer.
The digest's six tries and the upgrade's single try stay, each with
its reason written beside it.

Refs: #1086
Every loop that tries a write again on a 412 but the digest's tried at
once. Two tabs or devices whose writes met once are then likely to meet
again on the very next try, and lose all their tries together.

pauseBefore in writes.ts waits a random time before each try again: up
to 25 ms before the second, up to twice as long before each after, so
a learner never notices it. editDocument waits it by default, and so
do the use cases that plan a write again (a bulk edit of cards, a
transfer, an edit of a draft), injected through createUseCases so their
tests don't wait. The digest keeps its six tries and its growing wait,
which outlasts another page writing it several times running, and adds
the same random time on top, so no wait is shorter than before.

Refs: #1086
…ite set

A bulk edit of cards, a transfer of cards, a library upgrade and a deck
file's import each wrote out the same loop by hand: plan from versioned
reads, make and check every write, send them in a fixed order, and plan
again on a 412. Each turned a version into a write condition its own
way and recorded what was sent its own way, so a change to the policy
had to be made in four places, and each tested its own 412 path.

writeInOrder in writes.ts now runs that loop once. A plan gives its
result and its writes in the order they are sent; the policy says how
often it is planned again, on which failures, whether also once a write
went out, and the pause before. It says how many writes were sent, and
cutOffOf a failure says how far the set got. atVersion turns a read's
version into a write condition.

The bulk edit takes the defaults and tells of its changes once the
cards are written. The transfer plans again also after writes were
sent, as a card already in the target is not written again. The
upgrade is tried once and takes whether the deck changed from what was
sent, its steps still reported as each write is reached. The import
plans again, at once, only when its deck's place is taken. Its entry
stays the last write, and finishCutOffImport takes back what it wrote.
editReleaseDraft keeps its own loop, which merges the try cut short.
Nothing written to a pod changes. The policy is tested once, in
writes.test.ts; each use case keeps the tests of its own rules.

Closes: #1086
@ixuz ixuz added the enhancement New feature or request label Nov 28, 2026
@ixuz
ixuz added this pull request to the merge queue Nov 28, 2026
Merged via the queue into main with commit 3bac497 Nov 28, 2026
57 checks passed
@ixuz
ixuz deleted the write-set branch November 28, 2026 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

1 participant