Skip to content

Optimize streaming message projection path#1688

Open
juliusmarminge wants to merge 1 commit intot3code/perf-checkpoint-reactor-fanoutfrom
t3code/perf-projection-thread-message-hot-path
Open

Optimize streaming message projection path#1688
juliusmarminge wants to merge 1 commit intot3code/perf-checkpoint-reactor-fanoutfrom
t3code/perf-projection-thread-message-hot-path

Conversation

@juliusmarminge
Copy link
Copy Markdown
Member

@juliusmarminge juliusmarminge commented Apr 2, 2026

Summary

  • replace full-thread message scans in the projection hot path with per-message lookup and SQL delta append
  • add repository coverage for getByMessageId and streaming appendTextDelta
  • keep the perf artifacts and result log updated in performance.md

Perf impact

  • create-turn-spam-8x thread.create dispatch -> thread.created: 3.61ms / 58.38ms -> 2.38ms / 20.36ms
  • create-turn-spam-8x thread.archive dispatch -> thread.archived: 5.71ms / 50.80ms -> 0.97ms / 16.53ms
  • biggest win here is the control-plane tail: ack -> event for thread.create dropped from 56.95ms p95 to 19.22ms p95

Testing

  • bun run --cwd apps/server test src/persistence/Layers/ProjectionThreadMessages.test.ts src/orchestration/Layers/ProjectionPipeline.test.ts
  • bun run --cwd apps/server test:perf
  • bun run fmt .
  • bun run lint
  • bun run typecheck

Stacked on the checkpoint-reactor PR.


Note

Medium Risk
Changes the message projection persistence/write path and SQL upsert semantics for streaming updates, which could affect message text/attachment consistency if edge cases are missed. Scope is limited to projection read-model storage and is covered by added repository tests.

Overview
Optimizes the thread.message-sent projection hot path for streaming messages. Instead of scanning all messages in a thread to find the current one, the pipeline now either appends streaming text via a new SQL appendTextDelta operation or does a direct per-message lookup (getByMessageId) before a final upsert.

The thread message projection repository API is extended with getByMessageId and appendTextDelta, with new SQL queries to support single-row lookup and conflict-based delta concatenation while preserving existing createdAt/attachments. Tests are added to cover the new lookup and streaming-delta behavior (including attachment/createdAt preservation).

Written by Cursor Bugbot for commit 3bec3d2. This will update automatically on new commits. Configure here.

Note

Optimize streaming message projection by appending text deltas instead of reconstructing full rows

  • Streaming thread.message-sent events now call appendTextDelta on ProjectionThreadMessageRepository, concatenating incoming text onto existing rows via SQL COALESCE rather than upserting a full reconstruction.
  • Non-streaming upserts now fetch the existing row via getByMessageId to preserve createdAt and retain prior attachments when the event omits them.
  • Adds getByMessageId and appendTextDelta to the repository shape in ProjectionThreadMessages.ts with corresponding input schemas.
  • Behavioral Change: streaming events no longer fall back to previously persisted attachments at the projection stage; attachments are only carried forward when explicitly present on the event.

Macroscope summarized 3bec3d2.

Co-authored-by: codex <codex@users.noreply.github.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 2, 2026

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: 989fbad6-e2a6-4562-818d-4f6fc3200b93

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
  • Commit unit tests in branch t3code/perf-projection-thread-message-hot-path

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions bot added size:L 100-499 changed lines (additions + deletions). vouch:trusted PR author is trusted by repo permissions or the VOUCHED list. labels Apr 2, 2026
@macroscopeapp
Copy link
Copy Markdown
Contributor

macroscopeapp bot commented Apr 2, 2026

Approvability

Verdict: Approved

Performance optimization that refactors streaming message persistence from JavaScript-based text concatenation to database-level upserts. The change is self-contained to the projection pipeline, includes comprehensive tests, and the author is the primary owner of this code.

You can customize Macroscope's approvability policy. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:trusted PR author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant