Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix assistant streaming degradation on long threads #2631

Closedjustsomelegs(GitHub profile, opens in a new tab)wants to merge 20 commits intomain fromrefs/pull/2631/headopened View on GitHub

What Changed

Optimizes assistant streaming message projection so streaming deltas are appended directly to the existing projected message instead of forcing the broader thread refresh path for every delta.

The change keeps the persisted message output the same, but avoids repeatedly rebuilding/read-refreshing the full thread state during high-volume assistant streaming.

Why

Long threads can contain very large numbers of assistant streaming delta events. The old path spent most of its time refreshing thread read-model state after each tiny text delta.

Benchmark from a large production thread:

thread messages: 1,430
thread activities: 9,766
assistant streaming delta events: 165,130
legacy mode: sampled, 500 samples per 10k-event window

OVERALL

optimized: mean 0.0047ms/event, p50 0.0040ms, p90 0.0060ms, p99 0.0113ms
legacy:    mean 39.8074ms/event, p50 38.4388ms, p90 46.3055ms, p99 59.6003ms
speedup:   mean ~8,555x, p99 ~5,274x

SAMPLE BREAKDOWN

events 1-10,000:
  optimized mean 0.0046ms/event, p99 0.0091ms
  legacy    mean 36.5874ms/event, p99 46.1719ms

events 50,001-60,000:
  optimized mean 0.0042ms/event, p99 0.0081ms
  legacy    mean 36.9265ms/event, p99 44.2846ms

events 100,001-110,000:
  optimized mean 0.0044ms/event, p99 0.0114ms
  legacy    mean 39.9709ms/event, p99 53.0225ms

events 130,001-140,000:
  optimized mean 0.0052ms/event, p99 0.0175ms
  legacy    mean 42.7856ms/event, p99 68.0208ms

events 160,001-165,130:
  optimized mean 0.0061ms/event, p99 0.0299ms
  legacy    mean 41.0519ms/event, p99 62.3399ms

BEFORE

https://github.com/user-attachments/assets/b970931c-12b7-48dd-abb7-6c63a025beb3

AFTER

https://github.com/user-attachments/assets/29be8e47-9480-45c4-a934-016f9a30360b

This keeps the hot path proportional to the incoming delta instead of proportional to the size of the thread.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes
  • I included a video for animation/interaction changes

Note

Fix assistant streaming degradation on long threads by appending deltas in-place

  • Adds an appendText method to ProjectionThreadMessageRepositoryShape that inserts or concatenates streaming text deltas directly in the DB, preserving existing attachments unless replacements are provided.
  • The projection pipeline uses this fast-path for thread.message-sent events with role=assistant and streaming=true, skipping the previous read-then-write pattern that caused degradation on long threads.
  • The applyProjectsProjection projector also skips refreshing the thread shell summary for streaming assistant deltas.
  • Behavioral Change: streaming assistant messages now go through appendText instead of a full upsert; malformed inputs will now surface as decode errors rather than silently persisting.
Macroscope summarized 7cd48c7.
    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: a3834e94-3664-438e-b4ca-be536cfa0d07

      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 and usage tips.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Needs human review

      This PR changes how streaming assistant messages are stored in the database, switching from individual row inserts to in-place text concatenation via UPSERT. While well-tested, this fundamental change to message persistence and projection behavior warrants human review.

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

    5. react-doctor[bot](GitHub profile, opens in a new tab)commented

      Note

      No issues found

    6. Closing in favor of #2829 (orchestration V2).

      #2829 deletes the V1 orchestration layer this PR builds on — apps/server/src/orchestration/**, provider/Layers/*Adapter.ts and provider/Services/** are removed and replaced by apps/server/src/orchestration-v2/**, with the IPC surface renamed to ORCHESTRATION_V2_WS_METHODS. The files this PR touches either no longer exist or are rewritten, so it can't be rebased — it would need reimplementing against the V2 adapters.

      This is not a judgement on the change itself. Several of these are real gaps we still want fixed; the base just moved out from under them.

      Once #2829 merges, please rebase onto main, port the change to the V2 equivalent, and reopen (or open a fresh PR). Ping me and I'll prioritise the review.

    7. juliusmarminge closed this

Sign in to join this conversation.