Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): speed up long thread message sync #17387

MergedYash-Singh1(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/17387/headmerged by Yash-Singh1(GitHub profile, opens in a new tab) View on GitHub

Problem

Long forked threads can spend more than two seconds syncing messages before the client shows history. Profiling a long thread against a 57 GB database found repeated visibility work, unnecessary ancestor hydration, extra payload lookups, and repeated serialization in the bounded snapshot path.

Change

  • Build the existing visibility lookup once and calculate local visibility once per projection.
  • Load only timeline rows, runs, and attempts from fork ancestors during bounded reads.
  • Sort retained node IDs before fetching payloads through the existing primary key, then restore their order without sorting large JSON bodies in SQLite.
  • Carry SQLite row IDs through timeline selection to avoid a second string-ID index lookup for every payload.
  • Skip the final full-projection budget serialization when the already-measured control state exceeds the cap.

Uses existing indexes. No migrations, schema changes, new dependencies, or response-format changes.

Scope and approval

Requested through the T3 Code development thread to investigate message-sync latency and open this draft. These changes address the same server snapshot-read bottleneck and preserve history paging, visibility, and actionable state.

Verification

Combined benchmark

Compared the starting implementation with all changes together on the same frozen database, using four warmup pairs and 15 measured alternating pairs per thread. Timings include SQL reads, decoding, wire projection, bounded selection, schema encoding, and JSON serialization. They exclude process startup, network transfer, and client rendering. Every encoded response body matched byte for byte.

ThreadStarting medianCurrent medianReduction
Long forked thread395.6 ms303.5 ms23%
Medium thread176.6 ms140.4 ms21%
Shorter thread40.7 ms41.1 msEssentially unchanged

The long response remains 12.46 MB. Its measured p90 preparation time fell from 544 to 400 ms.

Live database and component measurements

Seven alternating fresh processes per query version on the real database, opened read-only, measured median snapshot reads of 2,123 to 927 ms and response preparation of 2,428 to 1,601 ms, a 34% response-time reduction. This comparison preceded the final timeline row-ID change and used the new budget calculation on both sides. OS caches were not cleared, live data could change between processes, and timings varied widely. Response equivalence was checked separately within pinned read transactions.

Independent component comparisons measured payload-budget calculation at 99.5 to 25.3 ms across 15 alternating pairs on frozen data, and total timeline SQL at 49.5 to 41.3 ms across nine alternating fresh connections with warm OS caches. These percentages describe different measurements and should not be added.

The production client still reproduced a 2.09-second click-to-sync-completion wait with optimized code against an isolated copy of the long thread. Optimized fresh server responses in the earlier live test ranged from 0.78 to 4.13 seconds. This does not establish a matching UI speedup or eliminate every slow first read.

Correctness checks

  • 112 focused tests passed across ProjectionStore, boundedSnapshotTransport, ThreadStream, threadHistoryPaging, WireProjection, and orchestrationV2Timeline.
  • Query-plan checks confirm indexed user-message boundary selection, direct row-ID timeline payload reads, direct node primary-key reads, and no final payload sort.
  • Coverage includes complete user turns, equal-ordinal ID ordering with reversed insertion order, null/equal node timestamps, duplicate node references, rollback and interrupt visibility, nested forks, cursor paging, and oversized actionable state.
  • 30 full/window/history-anchor snapshot comparisons were byte-identical. The budget shortcut also produced 68 byte-identical bounded responses across paging configurations and payload caps.
  • Server typecheck, targeted lint and formatting, and git diff checks passed.

Focused test command:

vp test run apps/server/src/orchestration-v2/ProjectionStore.test.ts apps/server/src/orchestration-v2/boundedSnapshotTransport.test.ts apps/server/src/orchestration-v2/ThreadStream.test.ts apps/server/src/orchestration-v2/threadHistoryPaging.test.ts apps/server/src/orchestration-v2/WireProjection.test.ts packages/shared/src/orchestrationV2Timeline.test.ts

Implemented with GPT-6.1-sol through the Codex harness in T3 Code.

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    2. juliusmarminge added labelsmacroscope-review

    3. Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      CodexThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      CodexLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      CodexLive turn WebSocket decoded20.9 KiB20.9 KiB0 B (0.0%)29.3 KiB✅
      CodexLive turn messages220 (0.0%)8✅
      ClaudeTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      ClaudeThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      ClaudeLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      ClaudeLive turn WebSocket decoded21.2 KiB21.2 KiB0 B (0.0%)29.3 KiB✅
      ClaudeLive turn messages220 (0.0%)8✅

      Baseline: b707eeb · PR result: bc7cead · 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: 108.5 KiB
      • Claude decoded thread snapshot: 108.8 KiB

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

    4. Review in Change Stack →

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration
      • Configuration used: Path: .coderabbit.config.ts
      • Review profile: CHILL
      • Plan: Team
      • Run ID: fd791855-94df-42b9-9b43-ef5cdb9c2920
      📥 Commits

      Reviewing files that changed from the base of the PR and between b707eeb052782cfd8b1ff0445f84b2aaf01da38b and bc7ceadb959b82c32294e7f477d257edf083caac.

      📒 Files selected for processing (5)
      • apps/server/src/orchestration-v2/ProjectionStore.test.ts
      • apps/server/src/orchestration-v2/ProjectionStore.ts
      • apps/server/src/orchestration-v2/WireProjection.test.ts
      • apps/server/src/orchestration-v2/threadHistoryPaging.test.ts
      • apps/server/src/orchestration-v2/threadHistoryPaging.ts

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.


      📝 Walkthrough

      Walkthrough

      Projection reads now apply shared turn-item visibility and use revised bounded SQL queries for timeline items and nodes. Thread history paging also marks the payload budget as exceeded when the control projection alone exceeds the configured byte limit.

      Changes

      Projection reads and bounded paging

      Layer / File(s)Summary
      Projection visibility and fork inputs
      apps/server/src/orchestration-v2/ProjectionStore.ts, apps/server/src/orchestration-v2/WireProjection.test.ts
      Local turn items use the shared visibility predicate. Canonical reads no longer append local visible items, and windowed fork reads request only runs, attempts, and turn items from ancestors.
      Bounded window selection and node reads
      apps/server/src/orchestration-v2/ProjectionStore.ts, apps/server/src/orchestration-v2/ProjectionStore.test.ts
      Window queries carry turn-item rowids into payload retrieval. Node reads fetch payloads by primary key in retained-node order. Tests cover ordering, payloads, and SQL plans.
      Thread history payload budget
      apps/server/src/orchestration-v2/threadHistoryPaging.ts, apps/server/src/orchestration-v2/threadHistoryPaging.test.ts
      The exceeded-budget flag now includes the control projection size. The test checks that bounded paging retains visible timeline items.

      Priority: ➖ Normal

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Refactor

      Suggested reviewers: juliusmarminge

      Merge Risk: ⚪ Minimal · up to bc7ce

      No actionable issue is established that would prevent merging after normal checks.

      🚥 Pre-merge checks | ✅ 4
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title checkPassedThe title clearly and concisely describes the main change: improving server performance for long thread message synchronization.
      Description checkPassedThe description includes the required Problem, Change, Scope and approval, and Verification sections. It explains the bottleneck, implementation, scope, benchmark results, correctness checks, limitati…
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      ✨ Finishing Touches
      📝 Generate docstrings
      • Commit to this branch
      • Create a new PR
      🧪 Generate unit tests (beta)
      • Commit to this branch
      • Create a new PR
      • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      Comment `@coderabbitai help` to get the list of available commands.
    5. Approvability

      Verdict: Approved at bc7cead

      Macroscope's review found this PR approvable — This is a contained server-side performance fix that optimizes indexed history and node reads while preserving projection contents, ordering, and wire output. The accompanying tests cover the affected paging, fork, visibility, query-plan, and budget paths without changing product defaults or introducing new capabilities.

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

Sign in to join this conversation.