Skip to content

perf(serving): company pages subscribe without waiting for who-am-I - #759

Merged
allisonmahmood merged 4 commits into
mainfrom
perf/744-subscriptions-skip-me
Oct 17, 2026
Merged

allisonmahmood merged 4 commits into
mainfrom
perf/744-subscriptions-skip-me

Conversation

@allisonmahmood

@allisonmahmood allisonmahmood commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

On company and local pages, the shell held every subscription until the viewer's me round trip came back, about 125 ms in production. That wait bought nothing. These shells already pin the viewer when they render, and the document stream checks that viewer on every open and subscription update. The production traces in #754 show all of a page's subscription POSTs leaving the instant me returned.

Now the broker forwards subscribe and unsubscribe on company and local pages without waiting for me. me stays lazy: it runs when the patch asks for it or a direct operation needs it, and the shell adds no background request. A stale cookie still refreshes and an account change still ends the page as principal_changed, now through the stream. Public pages are unchanged, because they bind the directory principal first.

The local shell also stops asking me eagerly when the frame is ready, so it behaves like company. ADR-0010 said the local shell got its viewer through me; it pins the viewer like company, and the ADR now says so.

Not changed: other direct operations still wait for me. A page that never calls me itself now pays that round trip on its first direct operation, such as a mutation, instead of during load.

Measured

A five-query tier 2 page that also calls me(), on this worktree's dev instance, with 120 ms added to every /api/runtime/call through a Playwright route delay. The figures are medians of 8 warm loads. The main column is the same instance restarted with main's broker.

main this branch
Navigation → first subscription POST 216 ms 87 ms
me finished → first subscription POST −1 ms (they leave together) −128 ms (before me returns)
Navigation → all five panels ready 297 ms 180 ms

A second branch run gave 89 ms, −130 ms and 165 ms.

Tests

test/browser-tier1/pinned-subscriptions.spec.ts runs each case in company and local scope:

  • with me held in the browser, the first subscription is posted and answered while me is pending, and me then answers usr_dev;
  • on a page whose patch does not ask me, a subscription completes with no me request attempted; when the patch then asks, exactly one goes out and answers usr_dev;
  • with me held, a stale cookie on a subscription refreshes through the stream, with no notice;
  • with me held, switching accounts ends the page as principal_changed.

The held-me cases fail against main's broker. The lazy case fails when the eager local bootstrap call is restored. The local cases serve the company shell with data-scope="local", since patchy dev runs the same broker; this drives its local branch against the real runtime, whose sessions can expire and change account. The full browser suite passes (69 tests). There are no broker unit tests: its logic lives in the browser mount closure.

pnpm verify passed on every suite before #755 and #756 merged. On the current head, merged with main, the full browser suite and the test typecheck pass.

Closes #744

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

Every frame operation, subscriptions included, awaited the broker's
identify(), a `me` round trip per document. Company and local documents
already pin their viewer at mount, and their stream authenticates each open
and subscription update against it, so a page's first subscriptions sat
idle for one round trip and left together when `me` returned.

Pinned documents now forward subscribe and unsubscribe without awaiting
identify(). `me` stays lazy: the patch or a direct operation still asks it.
The local shell no longer asks `me` eagerly at ready either, matching
company. ADR-0010 said the local shell got its principal through `me`; it
pins the viewer like company, and the ADR now says so.

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 17, 2026
allisonmahmood and others added 3 commits October 17, 2026 22:04
The old count could not tell a shell-initiated `me` from the patch's,
because the fixture asks `me` at once and the broker caches it. A page
published with `<body data-me="lazy">` now skips that call, and a new case
counts browser-attempted `me` requests: none while a subscription
completes, one when the patch asks. Restoring the eager local bootstrap
call fails it.

The stale-cookie and account-change cases now run in local scope too,
through the same scope rewrite.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@allisonmahmood
allisonmahmood merged commit c701e0a into main Oct 17, 2026
13 checks passed
@allisonmahmood
allisonmahmood deleted the perf/744-subscriptions-skip-me branch October 17, 2026 22:38
allisonmahmood added a commit that referenced this pull request Oct 18, 2026
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 #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.

---------

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' subscriptions wait for a me round trip

1 participant