Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(orchestration): speed up new-chat propagation and offline catch-up #4177

MergedRusiruSadathana(GitHub profile, opens in a new tab)merged 3 commits intomain fromrefs/pull/4177/headmerged by juliusmarminge View on GitHub

What Changed

  • Coalesces shell events per aggregate over a 50 ms / 512-event window with bounded shell-refetch concurrency. Bursty activity for one thread now collapses to one shell refetch without blocking unrelated new threads.
  • Captures the authoritative shell head when a client resumes. Gaps larger than SHELL_RESUME_MAX_GAP (1,000), or cursors ahead of that head, receive a fresh projected snapshot.
  • Replays small gaps only through the captured head. Events committed afterward are delivered by the already-attached live subscription, so catch-up cannot chase a moving event-store head while an unbounded live buffer grows.
  • Starts the live PubSub consumer immediately before snapshot or replay work, preserving events published during a synchronous snapshot load.
  • Keeps deletion/archive coalescing correct by emitting thread-removed when the current active-thread projection is absent.

Why

New chats could be slow to appear because every high-frequency thread event triggered its own shell refetch, serializing unrelated thread creation behind redundant database reads. Offline catch-up could also replay an effectively unbounded event range before reaching the live stream.

This keeps replay and WebSocket delivery, but makes replay explicitly bounded and uses an authoritative snapshot when replay would be more expensive.

Tests

Integration coverage verifies:

  • large-gap and invalid-cursor snapshot fallback;
  • replay is limited to the captured head;
  • replay burst coalescing and deletion/removal behavior;
  • actual post-synchronization streamDomainEvents burst coalescing;
  • events published during a synchronous snapshot load are buffered before the synchronization marker;
  • OrchestrationEngine.latestSequence follows the authoritative command read model.

UI Changes

No rendered UI changes. Shell and thread status indicators added by #4163 now receive a bounded, explicit synchronization completion boundary.

Checklist

  • This PR is focused
  • The synchronization and performance behavior is covered by integration tests
  • No screenshots or animation video are applicable

Related to #2761 and #2750.


Note

Medium Risk Changes real-time WebSocket shell synchronization and resume semantics (snapshot fallback, coalescing, removal emissions); behavior is heavily integration-tested but clients depend on ordering and sequence handling.

Overview Improves subscribeShell so sidebar/shell updates stay fast under bursts and after long offline periods.

The orchestration engine exposes latestSequence from the authoritative command read model. On resume, the handler compares the client cursor to that head: gaps over 1,000 events or cursors ahead of the head get a fresh shell snapshot instead of per-event replay; smaller gaps replay only through the captured head so catch-up cannot chase a moving store while the live buffer grows.

Live subscription now starts before snapshot or replay work, with events buffered and coalesced (50 ms / 512-event windows, one refetch per aggregate per batch, concurrency 8). High-frequency deltas on one thread no longer block unrelated thread.created updates. Shell projection refetches retry once on transient DB errors; a missing row after coalescing emits thread-removed / project-removed instead of silently dropping deletes.

Extensive server.test.ts coverage exercises snapshot fallback, bounded replay, coalescing, live bursts after sync, and deletion edge cases.

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

Note

Speed up new-chat propagation and offline catch-up in subscribeShell

  • Reworks the subscribeShell WebSocket RPC in ws.ts to buffer live domain events before loading state, so no events are missed during startup.
  • Adds a latestSequence accessor to OrchestrationEngineShape so the handler can bound catch-up to the captured head sequence and avoid unbounded replay.
  • Introduces SHELL_RESUME_MAX_GAP (1,000 events): if the client's cursor is too far behind (or ahead), a fresh shell snapshot is sent instead of incremental replay.
  • Coalesces multiple events for the same aggregate into a single refetch during both catch-up (coalesceShellEvents) and live streaming (coalesceShellLiveStream), reducing redundant shell reads.
  • Thread and project events now emit a removal when the projected shell row is absent, rather than silently dropping the event.
  • retryShellProjectionRead retries a failing shell read once before returning Option.none, preventing transient errors from propagating as stream failures.
Macroscope summarized 465efdc.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro

      Run ID: 37d04697-16ea-450b-a7a9-36aaf0fb7a85

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

      Fix All in Cursor ❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot).

      Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.

      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 25e3d3d255fd8bfabd627796b2f29ba2914e8b50. Configure [here](https://www.cursor.com/dashboard/bugbot).
    4. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Needs human review

      This PR introduces significant runtime behavior changes to client synchronization: event coalescing to batch shell updates, a new threshold (1000 events) that switches offline catch-up from replay to fresh snapshots, and changed deletion handling. While comprehensive tests are added, these are non-trivial changes to core real-time infrastructure that warrant human review.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    5. juliusmarminge merged thisdb4b2d8

    6. juliusmarminge closed this

Sign in to join this conversation.