Skip to content

An open tab nobody uses keeps its company's server running #800

Closed

Description

@allisonmahmood

What happens

A tier 2 tab that stays open holds its company's server task for as long as it's connected. Nobody has to be using it.

  • Each stream generation calls ExecutionLifecycle.connect, which holds the company activity lock until the stream closes (RuntimeStream.ts, the tier 2 block). So the binding never reaches execution.company.idle.
  • Since perf(execution): an open patch keeps its process warm instead of reloading it #775, the same generation also holds the version's keepalive (ConnectedVersions.connect), so its process stays loaded too.

In production one tab stayed connected for 36.5 hours (Oct 17–19), and the company's binding lived exactly as long. Only the deploy ended it. That's a half-vCPU, 2 GiB Fargate task, billed to a company for a tab someone forgot.

Hidden tabs already let go after stream.hidden.suspend (30 s). A window the browser considers visible never does.

Fix

A connected document keeps its company task and its version's process warm only while its viewer is active. After a window with no activity it lets go. The screen and the stream stay, and live updates still arrive. The model is per document, because subscriptions, admission and generations already are.

  1. Activity. Each of these marks the document active on its host.

    • Its first stream: a new document, with no X-Patchy-Generation.
    • A change to its logical desired set. Keep the last admitted set with the document's activity record, across generations. Compare subscription ids, operations and canonical args, ignoring order, revision and vector.
      • Count a change when admitted deltas are applied, including buffered ones.
      • Refused, stale and no-op requests don't count.
      • The replace the shell sends after every reconnect normally repeats the logical set, so it doesn't count. If it carries changes made while disconnected, those count.
    • A server.call from that viewer for that patch version on this host, recorded at the authenticated external call boundary. This is not inside Invocation.call, which also serves subscription reads and nested calls. Calls carry no documentId, so it applies to the viewer's connected documents of that version here.

    These don't count:

    • the reauthentication reconnect every ~50 s;
    • periodic reconcile checks;
    • re-runs caused by someone else's write.
  2. The window. Add a new operating limit: default 10 minutes, scoped to the company, overridable, tuned later from wide events.

    • While active, the document holds what it holds today: the company activity lock via connect, and the version's keepalive via ConnectedVersions.connect.
    • When the window passes with no activity, it releases both.
    • The keepalive stops once no connected, active document of that version is left on the host. The process is then reaped after execution.process.idle.
    • The binding becomes eligible for release after execution.company.idle, plus housekeeping and shutdown time.
  3. One owner for the holds.

    • Each document has at most one current warm-hold owner.
    • Serialize activity, expiry and generation replacement, and fence late completions and finalizers by owner identity. An old generation's expiry, acquisition or finalizer must never touch the replacement's holds.
    • Expiry or disconnect cancels a pending re-hold and releases only that owner's holds.
    • Invocation admissions, including re-runs already acquiring, stay independent.
    • Reactivation re-holds in the background with warm: false. If acquisition fails, it keeps the latest activity time and lets the next activity retry.
  4. Reconnects and readiness.

    • An idle document's reconnect neither connects nor warms. It opens its server-side dispatch gate (executionReady), sends ready, and schedules subscription reconciliation.
    • Going idle leaves both dispatch and the shell ready.
    • Background re-hold failures send no startup frames.
    • Initial startup, and any cancelled startup attempt, still settle the broker's held calls through ready, start_failed or closure. Held calls have no other timeout.
    • A reconnect on a host with no record of the document, such as after a drain, starts active. fix(infra): the load balancer keeps a browser on one host #798's sticky routing is the prerequisite for multi-host behaviour; activity records aren't replicated.
  5. Bounded records.

    • Keep disconnected activity metadata in a cache with a fixed capacity and expiry.
    • Allocate a record only after successful stream admission, bound to company, patch, version and viewer, and evict disconnected records first. An expiry must not delete a record that has since reattached.
    • Grace keeps metadata only. A disconnect, including hidden suspension, releases warm holds at once, so a call can't re-hold a hidden document.
    • Idle connected streams still count toward stream.documents. Only connected active documents count as keepalive holders.
  6. Live updates while idle are unchanged. A wake dirties the subscription, and when the revision vector changed, its re-run acquires the fleet like a call, binding the task on demand. Such reads can extend execution's idle timer while the document stays inactive. That's demand, not a forgotten hold.

  7. Wide events.

    • The keepalive event says whether it ended because its last active document went idle or left.
    • The stream event counts the document's idle and active transitions during that generation.
    • Record the effective window and its configuration revision.
    • Keep it to what tuning the window needs.
  8. Every surface.

    • ADR-0010's readiness contract and ADR-0012 › Fleet controller;
    • OPERATIONS › Keeping open versions warm;
    • the execution.company.idle description, which says "no connected tier 2 documents";
    • the new limit's entry, with limits docs regenerated (pnpm --filter @patchy/limits render-docs);
    • the runtime stream description in packages/api, with docs/API.md regenerated;
    • analytics schemas and field descriptions;
    • the runtime glossary's keepalive and touch vocabulary;
    • docs/product.md if its tier 2 sentence implies warm-while-open.

Product effect: a forgotten tab stops holding execution 10 minutes after its last qualifying activity. With no other holders and no execution demand, the company task then becomes eligible for release after execution.company.idle, plus housekeeping and shutdown. The next action after that may pay process loading and company binding latency, bounded by existing admission behaviour.

Tests

  • TestClock, in the existing RuntimeStream / ConnectedVersions suites:
    • An active document holds the company lock and touches. With no activity past the window it releases both, and the keepalive event says idle.
    • A non-empty set whose revisions and vectors advance, reconnecting repeatedly across the deadline, stays idle: no re-hold, touch or warm.
    • A logical set change re-holds. A same-set replace doesn't.
    • A server.call from the viewer re-holds that viewer's connected documents of the version on this host, and not other viewers' or hidden ones.
    • Another viewer's write reaches an idle document through an on-demand acquire, without making it active.
    • An unknown document reconnecting on a new host starts active.
  • Barrier-controlled races: expiry against reactivation against generation replacement. Late finalizers never release the new owner's holds.
  • Cache: the bounded cache evicts disconnected records first and never deletes a reattached one.
  • Fleet: an active holder or an admitted invocation on another host prevents release.
  • Browser (test/browser-tier1/starting.spec.ts): hidden suspension, then an idle resume. A queued call completes exactly once without waiting for a background hold.

Done means

  • A connected, inactive tier 2 document releases its warm holds after the configured window, despite routine reconnects.
  • Its screen stays usable, live updates arrive, and the next action runs without hanging or replay.
  • Hidden suspension and document limits keep their existing behaviour.

Activity

  1. added
    ready-for-agent
    area:server
    on Oct 19, 2026
  2. added 3 commits that reference this issue on Oct 19, 2026
    26b3f34
    6b779b3
    5edc351
  3. added a commit that references this issue on Oct 19, 2026
    9e4b0df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:serverready-for-agent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions