Repository navigation
perf(serving): company pages' first calls don't wait for who-am-I - #766
Merged
Merged
Conversation
Company and local shells pin their viewer, and Runtime checks that principal on every operation before running it. The broker now forwards calls, mutations, actions, direct reads and downloads without first asking `me`. `me` itself and `route.set`, which changes history without calling Runtime, still go through `identify()`. Closes #760 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The ADR and broker comment said the shell asks `me` only for the patch and before a route change, which ruled out the fresh probe after `access_denied`. Runtime's check also covers server-bound operations, not the local generated download. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 18, 2026
allisonmahmood
added a commit
that referenced
this pull request
Oct 18, 2026
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. --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since #759, company and local documents send subscriptions without waiting for the broker's
meround trip. Everything else still waited for it: calls, mutations, actions, tier 1 direct reads and downloads. So a page that never asks formestill paid that round trip, about 125 ms in production, on its first save or read.The wait added nothing. Runtime's dispatch admission checks every one of those operations against the pinned viewer before running it. Company and local shells now forward them straight away.
mestays lazy, androute.setstill checks the account throughmefirst, because it changes history without calling Runtime.Unchanged:
failure()handling forsession_expired,principal_changedandshell_outdated;meprobe afteraccess_denied. A refused shared source leaves the document usable; losing access to the document ends it;There are no new retries. A stale first mutation racing a session refresh is still refused, as before.
What Runtime checks, per forwarded operation
Every forwarded operation reaches
dispatchinpackages/runtime/src/Runtime.ts. That includes/api/runtime/calland the fileGET/PUTroutes forfiles.get,files.put,shared.files.get,files.stageandfiles.redeem. Before anything runs, dispatch:session_expired;access_deniedwhen the version isn't served or the viewer can't open it, such as a deactivated viewer or another company's;X-Patchy-Principalwith the session's user, or refusesprincipal_changed. On POST, it also requires the envelope principal to match the header.When those checks run:
tables.*,files.*,shared.*andmembers.*: before the handler runs.server.call: beforeInvocation.call. A new unit test pins this.postgres.*,posthog.*): before the query runs, inside the recorded attempt. A refused first integration call therefore leaves a refused row in the integration log, attributed to the session's actual viewer. That already happens today for any integration call after the first one.Measured
On this worktree's dev instance, a
teamenvironment signed in as its admin, with two starter patches that never callme:server.callquery, or a mutation at/write;tables.list.A Playwright
routedelay added 120 ms to every/api/runtime/call, as in #759. Each figure is the median of 8 warm loads, measured from the document request to the first operation's response. For the main column, I restarted the same instance with main's broker.server.call(query)server.call(mutation)tables.listA second branch run, after restarting the instance, gave 199, 189 and 175 ms. On main, every load sent one
me. On this branch, none did.The waterfall for one tier 2 load, in ms from the document request:
The tier 1 load has the same shape: main sends
mefrom 45 to 171 ms andtables.listfrom 171 to 298 ms; this branch sendstables.listfrom 50 to 177 ms.The shell's broker URL changed from
?v=39732119…to?v=a2596dfd…. The digest is a hash of the built script, so the immutable cache from #761 busts by itself.Tests
test/browser-tier1/pinned-subscriptions.spec.tsis renamed topinned-viewer.spec.ts, since it now covers more than subscriptions. Each new case runs in company and local scope, on a page whose patch never asks forme:me. When the patch then asks, exactly onemegoes out. This replaces perf(serving): company pages subscribe without waiting for who-am-I #759's lazy-mecase.409and ends the page asprincipal_changed. Nomeis sent, and the table stays empty.401and ends the page assession_expired.access_denied. The probemeanswers200, and an owned write then succeeds on the same page. After a reload, with the viewer deactivated, the first read gets a403, the probe is refused, and the page ends asaccess_denied.Against main's broker, all four new cases fail in both scopes. #759's three held-
mecases still pass.packages/runtime/src/InvocationAdmission.test.tsadds one unit test. Aserver.callwhose principal isn't the session's viewer is refused withprincipal_changed, andInvocationnever runs. It fails ifserver.callskips admission. The browser cases cover tier 1 operations only.pnpm verifypasses on this head, rebased on main: format, lint, typecheck, synth, test, postgres-concurrency, cli-smoke, tier2-smoke and browser (75 tests).Surfaces
patchy devserves this same broker in local scope, which the local cases cover.Closes #760
Made by Claude Opus 5.5 via Claude Code; reviewed with GPT-6.1-Sol.