Skip to content

perf(serving): company pages' first calls don't wait for who-am-I - #766

Merged
allisonmahmood merged 4 commits into
mainfrom
perf/760-pinned-ops-skip-me
Oct 18, 2026
Merged

allisonmahmood merged 4 commits into
mainfrom
perf/760-pinned-ops-skip-me

Conversation

@allisonmahmood

@allisonmahmood allisonmahmood commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

Since #759, company and local documents send subscriptions without waiting for the broker's me round trip. Everything else still waited for it: calls, mutations, actions, tier 1 direct reads and downloads. So a page that never asks for me still 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. me stays lazy, and route.set still checks the account through me first, because it changes history without calling Runtime.

Unchanged:

  • the failure() handling for session_expired, principal_changed and shell_outdated;
  • the fresh me probe after access_denied. A refused shared source leaves the document usable; losing access to the document ends it;
  • public directory binding;
  • tier 2's execution-ready and closed gates.

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 dispatch in packages/runtime/src/Runtime.ts. That includes /api/runtime/call and the file GET/PUT routes for files.get, files.put, shared.files.get, files.stage and files.redeem. Before anything runs, dispatch:

  • authenticates the session, or refuses session_expired;
  • refuses access_denied when the version isn't served or the viewer can't open it, such as a deactivated viewer or another company's;
  • compares X-Patchy-Principal with the session's user, or refuses principal_changed. On POST, it also requires the envelope principal to match the header.

When those checks run:

  • Reads and mutations, including tables.*, files.*, shared.* and members.*: before the handler runs.
  • server.call: before Invocation.call. A new unit test pins this.
  • Integrations (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 team environment signed in as its admin, with two starter patches that never call me:

  • a tier 2 page whose first operation is a server.call query, or a mutation at /write;
  • a tier 1 page whose first operation is tables.list.

A Playwright route delay 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.

First operation main this branch
tier 2 server.call (query) 316 ms 198 ms
tier 2 server.call (mutation) 318 ms 209 ms
tier 1 tables.list 301 ms 193 ms

A 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:

main                         this branch
  0–13   document              0–12   document
 10–22   /~shell/broker.js     9–21   /~shell/broker.js
 30–41   /~content/…          29–41   /~content/…
 53–179  me                   48–188  server.call
180–317  server.call

The tier 1 load has the same shape: main sends me from 45 to 171 ms and tables.list from 171 to 298 ms; this branch sends tables.list from 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.ts is renamed to pinned-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 for me:

  • The first write and, on a reload, the first read and a subscription all complete with no me. When the patch then asks, exactly one me goes out. This replaces perf(serving): company pages subscribe without waiting for who-am-I #759's lazy-me case.
  • After an account switch, the first write gets a 409 and ends the page as principal_changed. No me is sent, and the table stays empty.
  • After the session expires, the first read gets a 401 and ends the page as session_expired.
  • With its shared source retired, a first shared read is refused with access_denied. The probe me answers 200, and an owned write then succeeds on the same page. After a reload, with the viewer deactivated, the first read gets a 403, the probe is refused, and the page ends as access_denied.

Against main's broker, all four new cases fail in both scopes. #759's three held-me cases still pass.

packages/runtime/src/InvocationAdmission.test.ts adds one unit test. A server.call whose principal isn't the session's viewer is refused with principal_changed, and Invocation never runs. It fails if server.call skips admission. The browser cases cover tier 1 operations only.

pnpm verify passes on this head, rebased on main: format, lint, typecheck, synth, test, postgres-concurrency, cli-smoke, tier2-smoke and browser (75 tests).

Surfaces

  • Wire contract and runtime schemas: unchanged. No request, response or schema moved.
  • CLI: none. patchy dev serves this same broker in local scope, which the local cases cover.
  • Vocabulary and product shape: ADR-0010's pinned-viewer sentence is updated.
  • Served pages: the broker script and its digest.

Closes #760

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

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]>
@github-actions github-actions Bot added area:server Touches the server area area:ci CI, shared testing infrastructure, development scripts and tooling area:docs Touches the docs area size:m 100-500 changed lines labels Oct 18, 2026
allisonmahmood and others added 3 commits October 18, 2026 14:54
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]>
@allisonmahmood
allisonmahmood merged commit ac2184f into main Oct 18, 2026
13 checks passed
@allisonmahmood
allisonmahmood deleted the perf/760-pinned-ops-skip-me branch October 18, 2026 14:32
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ci CI, shared testing infrastructure, development scripts and tooling 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.

Company documents' first calls still wait for a me round trip

1 participant