perf(server): user-only message reads use the user-message index for ordinals #33009
main fromrefs/pull/33009/head·merged by juliusmarminge View on GitHubCross-machine waitForThread re-reads a thread's user messages on every evaluation. To order them, ProjectionStore looks up each message's first turn-item ordinal with type IN ('user_message', 'assistant_message'). That predicate can't use the partial orchestration_v2_projection_turn_items_user_message_idx, so SQLite walks every turn item of the thread through thread_run_idx and reads each payload.
Fleet traces (24h ending 2026-11-08 09:00) show it as the top slow personal sqlite statement: 103 executions over 150 ms on nucbox-1 (max 320 ms, 16.5 s total), up from 38 the day before. They come from OrchestratorMcpService.waitForThread → ThreadManagementService.waitForThread → orchestrationV2.threadManagement.evaluate → OrchestratorV2.getThreadRecords (messageRoles: ["user"]). settleAfterRun uses the same filter.
Fix: when every loaded message is a user message, query type = 'user_message'. The literal equality matches the partial index's WHERE. Mixed or assistant reads keep the old predicate. The rows are the same: on a real 14 GB database, all 8,053 user-role messages map only to user_message turn items.
Plan, before:
SEARCH orchestration_v2_projection_turn_items USING INDEX orchestration_v2_projection_turn_items_thread_run_idx (thread_id=?)
USE TEMP B-TREE FOR GROUP BY
Plan, after:
SEARCH orchestration_v2_projection_turn_items USING INDEX orchestration_v2_projection_turn_items_user_message_idx (thread_id=?)
USE TEMP B-TREE FOR GROUP BY
Timings: real database opened read-only, warm, all user messages of the five largest threads, with identical results before and after:
| thread turn items | user msgs | before | after |
|---|---|---|---|
| 64,194 | 13 | 136.9 ms | 0.45 ms |
| 19,619 | 139 | 52.2 ms | 1.12 ms |
| 12,353 | 304 | 29.6 ms | 1.25 ms |
| — | 122 | 17.9 ms | 0.73 ms |
| — | 38 | 18.1 ms | 0.48 ms |
Test: the existing role-filter case now asserts the user-only ordinal lookup plans onto the user-message index; it fails on main. The bounded-history fixture inserted assistant_message items that point at user messages, which real data never has, so those items are now user_message. The test still covers duplicate-ordinal and cross-thread handling.
Opus 5.5 via Claude Code (daily fleet performance audit)
🤖 Generated with Claude Code
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XS
Approvability#
Verdict: Approved at
e59d3dfMacroscope's review found this PR approvable — User-only message reads now use a narrower ordinal query that can leverage the existing user-message index. Mixed-role reads remain unchanged, with no schema, persisted-data, or product-default changes in the assessed diff.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Provider Metric Main baseline This PR Impact PR ceiling Codex Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅ Codex Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Codex Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅ Codex Live turn WebSocket decoded 21.5 KiB 21.5 KiB 0 B (0.0%) 24.4 KiB ✅ Codex Live turn messages 1 1 0 (0.0%) 8 ✅ Claude Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅ Claude Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Claude Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅ Claude Live turn WebSocket decoded 21.9 KiB 21.9 KiB 0 B (0.0%) 24.4 KiB ✅ Claude Live turn messages 1 1 0 (0.0%) 8 ✅ Baseline:
ade4730· PR result:e59d3df· Source CI: successScenario 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.
No actionable comments were generated in the recent review.
Walkthrough
-
The message-order lookup now filters for user-message items when all loaded messages have the user role. Mixed-role reads retain both message types. Tests verify the user-only read results, index use, and a bounded-history fixture.
Changes: Message ordering lookup
Layer / File(s) Summary User-only ordinal lookup
apps/server/src/orchestration-v2/ProjectionStore.ts,apps/server/src/orchestration-v2/ProjectionStore.test.tsThe ordinal query filters for user_messageitems when all loaded messages have the user role. Mixed-role reads continue to includeassistant_messageitems. Tests check the user-only result count and use ofturn_items_user_message_idx; the bounded-history fixture row is now typed asuser_message.Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Refactor
Possibly related issues
pingdotgg/t3code#29468— The issue identifies the same message-order query and reports that user-only reads do not use the partial user-message index. This change addresses that query for user-only reads, but not the other reads listed in the issue.pingdotgg/t3code#23000— The issue describes the same message-order aggregate. This change uses the existing user-message partial index for user-only reads; it does not address the issue’s thread-shell and recovery queries.pingdotgg/t3code#24895— The issue reports query cost in the same aggregate. This change narrows the item types for user-only reads, but does not address the other reads listed in the issue.
Possibly related PRs
- pingdotgg/t3code#27495 — That PR adds the loaded-message ID filter to this ordinal lookup and introduces the bounded-history test fixture that this PR updates.
- pingdotgg/t3code#30873 — That PR adds an expression index and query predicate to this lookup. This PR changes the lookup’s type predicate, which interacts with index matching.
Suggested reviewers:
motaghi,macroscopeapp,yash-singh1
Merge Risk:
Minimal · up toe59d3-
User-only reads use the partial index. No merge-blocking behavior change is established.
Pre-merge checks:
4-
Check name Status Explanation Title check The title names the change: user-only message reads use the user-message index to compute ordinals. It matches the diff, which adds a branch in ProjectionStore.readCanonicalProjection that filters on … Description check The description follows the template's main sections in substance. Problem: it explains that the mixed type IN predicate cannot use the partial user-message index, causing SQLite to scan every turn it… Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Full details: Title check
-
Explanation
The title names the change: user-only message reads use the user-message index to compute ordinals. It matches the diff, which adds a branch in ProjectionStore.readCanonicalProjection that filters on type = 'user_message' when all loaded messages are user-role, and updates the test to assert the index is used. The conventional commit prefix is present and the title is specific. It does not mention the query-plan or latency motivation, which is acceptable.
Full details: Description check
-
Explanation
The description follows the template's main sections in substance. Problem: it explains that the mixed type IN predicate cannot use the partial user-message index, causing SQLite to scan every turn item of the thread, with fleet trace evidence. Change: it describes the conditional predicate for user-only reads and states that mixed or assistant reads keep the old predicate. Verification: it gives before and after query plans, a timing table on a real database with identical results, and a test that fails on main. Scope and approval: this section is not filled in. It does not link a triaged issue or maintainer-approved discussion, and it does not explain why the change qualifies without one. The template also asks for the agent model and harness; the author lists 'Opus 5.5 via Claude Code', which satisfies that item. Because the approval section is missing, the description is not fully complete, but the rest is thorough enough that the check passes.
-
Finishing Touches
-
Generate docstrings
-
- Commit to this branch
- Create a new PR
Generate unit tests (beta)
-
- Commit to this branch
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: pingdotgg/t3code
- Branch: perf/user-message-ordinals
- File: .coderabbit.config.ts
- Commit: e59d3df
- Layers: Repository TS config
- Review profile: CHILL
- Plan: Team
- Run ID:
51aaf3fe-8346-4b08-908d-b040cf112be9
Commits-
Reviewing files that changed from the base of the PR and between ade473018d32003658b7d38693e083ee2e314bd8 and e59d3df56944267faea8e261b9468438c48823d3.
Files selected for processing: 2-
apps/server/src/orchestration-v2/ProjectionStore.test.tsapps/server/src/orchestration-v2/ProjectionStore.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
-
-
juliusmarminge merged this5f2bb93
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes