Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): bound slow-client event buffers #9715

Mergedt3dotgg(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/9715/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHub

Slow WebSocket clients can keep accumulating shell and thread events without a memory bound.

Add a shared budget of 1,000 live items and 8 MiB of serialized payload per subscription. Count queued work, coalescing, and the batch waiting for an ACK. On overflow, stop the producer, release queued work, and return OrchestrationGetSnapshotError. Clients retry from their last applied sequence after 250 ms. This budget does not cap authoritative snapshots. Wire contracts and replay queries stay unchanged.

Verified with 30 focused tests, server typecheck, and file lint. Real WebSocket tests hold an ACK during overflow and verify producer cleanup, exact replay, and snapshot recovery after deletion.

With no consumer, 20,000 events previously retained 20.48 MB of text. The new code accepts at most 1,000 items and clears queued payload. The small-message coalescer benchmark is 10% faster. Cold byte sizing adds about 1.8 ms per 1 MiB tool event. Other subscriptions reuse that size.

Part of https://github.com/pingdotgg/t3code/issues/9661.

Created with GPT-6 Astra (preview) in Codex.


Note

Medium Risk Changes live WebSocket subscription buffering, teardown, and error semantics on the orchestration hot path; behavior is heavily tested but mis-handling overflow cleanup could drop or duplicate events for slow clients.

Overview Adds a per-subscription live stream budget (default 1,000 items and 8 MiB of serialized payload) so shell and thread WebSocket tails cannot grow without limit when a client is slow or stops ACKing batches.

New LiveStreamBudget charges queued, coalescing, and in-flight (pre-ACK) work, uses atomic replace during coalescing, and on overflow fails with OrchestrationGetSnapshotError (“resume from the last received sequence”), tears down the delivery source, and releases buffered events while keeping the current unacknowledged batch accounted for until scope cleanup.

subscribeShell and subscribeThread now retain live inputs through the budget, race PubSub consumption against budget failure so producers detach even without another pull, and wrap outbound streams with budget.deliver. ThreadLiveEventCoalescer is reworked around offer/offerAll, budget-backed output, and coalescer-scoped flush timers instead of offerAndWait/takeAll.

Integration tests simulate a held first WebSocket ACK during overflow and assert producer detach plus thread event replay and shell snapshot recovery on resubscribe.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 8f228395cbfb7c44b8702f80534bb12f26308746. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Bound slow-client event buffers with LiveStreamBudget in server live subscriptions

  • Adds LiveStreamBudget with configurable item (default 1,000) and serialized-byte (default 8 MiB) limits; retained items stay charged until acknowledged by the next delivery pull or released by scope cleanup
  • Rewrites ThreadLiveEventCoalescer and the shell subscription path in ws.ts to route all retained events, coalesced batches, and sync markers through the shared budget instead of unbounded queues
  • On overflow the budget latches OrchestrationGetSnapshotError, closes the underlying source, releases pending non-delivered items even while the consumer holds an unacked batch, and terminates the delivery stream
  • Replaces offerAndWait/takeAll APIs with offerAll/offer + an ACK-aware stream; completion markers now go through the same buffered stream
  • Behavioral Change: live thread and shell subscriptions that exceed budget now fail with OrchestrationGetSnapshotError and detach their PubSub subscriber instead of buffering indefinitely; offerAndWait and takeAll are removed from the coalescer API
Macroscope summarized 8f22839.
    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    2. Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire13.6 KiB13.8 KiB+184 B (+1.3%)15.1 KiB✅
      CodexThread snapshot wire7.0 KiB7.0 KiB+5 B (+0.1%)7.3 KiB✅
      CodexLive turn WebSocket wire6.6 KiB6.7 KiB+179 B (+2.7%)7.8 KiB✅
      CodexLive turn WebSocket decoded57.1 KiB58.5 KiB+1.4 KiB (+2.5%)66.4 KiB✅
      CodexLive turn messages10100 (0.0%)21✅
      ClaudeTotal thread wire13.6 KiB13.6 KiB+25 B (+0.2%)15.1 KiB✅
      ClaudeThread snapshot wire7.0 KiB7.0 KiB+8 B (+0.1%)7.3 KiB✅
      ClaudeLive turn WebSocket wire6.5 KiB6.5 KiB+17 B (+0.3%)7.8 KiB✅
      ClaudeLive turn WebSocket decoded57.8 KiB57.9 KiB+44 B (+0.1%)66.4 KiB✅
      ClaudeLive turn messages910+1 (+11.1%)21✅

      Baseline: 7839140 · PR result: 8f22839 · Source CI: success

      Scenario and decoded snapshot size

      10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

      • Codex decoded thread snapshot: 113.8 KiB
      • Claude decoded thread snapshot: 114.5 KiB

      Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

    3. Approvability

      Verdict: Not approved

      Macroscope's review found this PR not approvable — This PR introduces a shared ACK-aware buffer budget across existing thread and shell WebSocket paths, with new default limits and disconnect/replay recovery behavior when slow clients overflow. It also adds a static-analysis diagnostic suppression, so the runtime and tooling changes warrant human review.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. Note

      🤖 GPT-6 Astra (preview) responding on behalf of Theo

      I reviewed the new limits and recovery paths. Pending work, queued output, and the batch waiting for ACK share one budget. Overflow stops the producer, closes grouped source work, and keeps only the actual in-flight batch until delivery ends. Recovery uses the existing typed error and cursor retry, with no silent continuation after overflow.

      The one-line diagnostic suppression is intentional. Stream.fromPull requires the outer effect to return the pull effect. Yielding that effect there would consume data during setup.

      All 30 focused tests pass. I independently reran the 10 budget/coalescer tests and both real withheld-ACK recovery tests. CI, Cursor, and Macroscope correctness/conventions pass on 8f228395. Automatic eligibility is neutral.

      The limits cover serialized live payload, not total V8 heap. Authoritative snapshots keep their existing behavior. Cold sizing adds about 1.8 ms per 1 MiB tool update in the local coalescer benchmark; later subscribers reuse the measured size. This cost is accepted to bound retained live data.

Sign in to join this conversation.