Skip to content

perf(runtime): a replace during a run keeps its work - #769

Merged
allisonmahmood merged 3 commits into
mainfrom
perf/750-replace-keeps-run
Oct 18, 2026
Merged

allisonmahmood merged 3 commits into
mainfrom
perf/750-replace-keeps-run

Conversation

@allisonmahmood

@allisonmahmood allisonmahmood commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

When a shell's replace named a subscription that was already running with the same signature, the server bumped the subscription's epoch. The running read's result was thrown away, and the query ran again from the back of the queue. A tier 2 query that overlapped a slow-path replace cost a full extra run, and its panel waited twice as long.

The work can't simply be kept, though. The run took its fence before it read. A replace may be recovering a committed write whose wake was lost. If the old read answered the new fence, reconciliation would finish on stale data and nothing would run the query again.

Fix

A same-signature replace during a run no longer bumps epoch. The run keeps its work, but it answers the replace only once it proves that work fresh:

  • When the read finishes, the run takes a new fence over the keys the read saw, after the replace was admitted. If it equals the read's vector, the result is the answer: a snapshot, or up-to-date if unchanged.
  • If not, the result is discarded and the subscription runs again, dated from the replace. The client's vector from the replace stays, so the next run compares against what the client actually has.
  • A run takes at most one such fence. A replace that lands during it, during the final authority check, or before a read in the revision phase ends the run. The run then runs again from the replace, and its document rotates behind any queued ones, so repeated replaces can't hold a slot.
  • A failure, or an up-to-date from the revision phase, answers only if no replace arrived after the run started. Otherwise it is dropped and the subscription runs again, as before.

Design choice: a server-side check, not frames that name their sequence. The shell already counts the first answer after admitted as answering the fence. So the server never sends an answer-bearing frame it can't prove fresh, and the wire contract, the shell and docs/API.md stay as they are. A tab running an older shell gets the same guarantee. Frames that name their sequence could also send a stale read as data-only, but an older shell would ignore the new field and count stale data as the answer. That would also need a wire bump. The only thing given up is early stale data in the rare case where a write lands during an overlapped run; that case costs the same rerun it does on main.

What stays:

  • Full-snapshot recovery. A replace whose revision differs still clears the stored result, so the kept run sends a full snapshot, never up-to-date.
  • Epoch invalidation. A rebind still bumps epoch. A changed signature, an unsubscribe/re-add, or a document close or replacement still fails the presence check.
  • The duringRun wake intersection is unchanged.

The runtime and analytics glossaries and ADR-0010 now say how a resync treats a read it overlaps.

Tests

New deterministic cases in Subscriptions.test.ts, using gates on the read and on the revision fence:

  • Cheap path. A replace during a read with no write gets that read's snapshot as its answer, with one read in total.
  • Lost wake + replace. A write commits during the read, its wake is lost, and a replace follows. The stale read is discarded, and the first answer after the replace carries the write.
  • Missed full snapshot + unchanged rerun. The kept read sends a full snapshot (revision 3), not up-to-date, in three reads rather than four.
  • Overlapping replaces. Two replaces land during the read, then a write and a third replace land during the freshness check. The read can't answer the third, and the rerun delivers the write.
  • Rebind, unsubscribe/re-add and document close during a run each discard the read and send nothing from it.
  • Fairness. Both company slots are held by one document's checks while another replace lands. The runs end, and a queued document gets a slot.
  • A replace during the final authority check, after a successful freshness check, and during a revision-only up-to-date's check. Neither old answer is sent; a second run answers.
  • An overlapped read failure and a failing freshness check. Neither error is sent, and the next run starts at once, without any time passing.
  • A key-aware reader. The freshness fence covers the read's own dependencies: a write to a dependency only the read revealed, or an absent (-1) resource appearing, forces a rerun; nothing changed answers with one read.
  • The existing coalesced-wait test now expects the overlapped runs to deliver.

Against main's Subscriptions.ts, the cheap-path, missed-snapshot and coalesced-wait tests fail; the guarantee tests pass on both. With the freshness check removed, the lost-wake and overlapping-replace tests fail. Each later guard has its own test, which fails alone when the guard is removed: a looping freshness check, the final, up-to-date and failure replace checks, and a fence over only the keys known before the read.

Validation

I ran a local instance with a tier 2 patch whose notes.slow query reads a table and then waits 1.5 s. A script opened a document stream, subscribed with replace seq 0, and sent the same set as replace seq 1 500 ms later, while the query ran. Runs come from the re-run wide events.

main (3 runs) this branch (3 runs)
Query runs 2: initial → discarded, then replace → delivered 1: initial → delivered
Slot wait of the second run 1.02–1.11 s —
Subscribe to the replace's answer 3.03–3.14 s 1.53–1.68 s

A committed write during the run. A notes.create mutation ran 300 ms into the read, before the replace. On both, the first run was discarded, a second run (invalidation → delivered) followed, and the answer at about 3.0 s contained the new note.

pnpm verify passes locally: format, lint, typecheck, synth, test, postgres-concurrency, cli-smoke, tier2-smoke and browser. It ran before rebasing onto #766 and #768, which don't touch this code; the subscription suites passed again after the rebase.

Closes #750

Made by Claude Opus 5.5 via Claude Code; reviewed with GPT-6.1-Sol.

A same-signature replace no longer bumps the subscription's epoch. The
running read keeps its work and answers the replace only if the revisions
it read still match a fence taken after the replace; otherwise it is
discarded and the subscription runs again from the replace.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added area:server Touches the server area area:docs Touches the docs area size:m 100-500 changed lines labels Oct 18, 2026
allisonmahmood and others added 2 commits October 18, 2026 18:11
…s slot

Review round 1 on #769: a replace landing during each freshness check
sent the run straight into another, so a document under repeated
replaces could hold every company slot without answering or rotating.

- A run takes at most one post-replace fence. A replace during it fails
  the final answerable check, so the run ends, runs again from the
  replace and its document rotates behind queued ones.
- The analytics disposition entry now covers every replace discard,
  including one that overtakes the final authority check.
- Tests: a queued document gets a slot while the noisy one replaces; a
  replace during the final authority check and during a revision-only
  up-to-date's check; an overlapped read failure and a failing freshness
  check send no error and run again at once; a key-aware fixture checks
  a replaced read against the dependencies it revealed and an absent
  resource.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@allisonmahmood
allisonmahmood merged commit fb2bb19 into main Oct 18, 2026
13 checks passed
@allisonmahmood
allisonmahmood deleted the perf/750-replace-keeps-run branch October 18, 2026 16:54
allisonmahmood added a commit that referenced this pull request Oct 19, 2026
…ng Reconnecting (#783)

Release 14's What's new entry, written from \`7694bab\`. It covers
everything merged since release 13's \`through\` (\`c9b8fc4\`), and sets
the \`patchy\` release to \`0.14.0\` (\`packages/patchy/package.json\`
and \`CURRENT_RELEASE\`).

| Change | PRs |
| --- | --- |
| Improved: Tools with server code answer faster | #756, #764, #768,
#769, #771, #772, #773, #775, #778 |
| Improved: Your company's tools and pages open sooner | #759, #761,
#766, #770 |
| Fixed: Tools stop flashing Reconnecting while they load | #755, #776,
#780 |
| Improved: Publishing is quicker | #729 |
| Improved: New tools start with a tidy layout | #763 |
| Improved: Your usual is easier to scan | #721 |
| Fixed: A page that isn't there says what to do | #762 |
| Fixed: A logo Patchy can't read is refused, not a crash | #728 |
| Behind the scenes: richer logs for live data | #758 |

#720 is release 13's own entry, so it's left out.

Made by Claude Opus 5.5 via Claude Code.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:docs Touches the docs area area:server Touches the server area size:m 100-500 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A replace overlapping a run can throw away its result

1 participant