Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): user-only message reads use the user-message index for ordinals #33009

Mergedjuliusmarmingemerged 1 commit intomain fromrefs/pull/33009/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

Cross-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 itemsuser msgsbeforeafter
64,19413136.9 ms0.45 ms
19,61913952.2 ms1.12 ms
12,35330429.6 ms1.25 ms
—12217.9 ms0.73 ms
—3818.1 ms0.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

    2. Approvability

      Verdict: Approved at e59d3df

      Macroscope'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).
    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 decoded21.5 KiB21.5 KiB0 B (0.0%)24.4 KiB✅
      CodexLive turn messages110 (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.9 KiB21.9 KiB0 B (0.0%)24.4 KiB✅
      ClaudeLive turn messages110 (0.0%)8✅

      Baseline: ade4730 · PR result: e59d3df · 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. Explain Change →

      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.ts
      The ordinal query filters for user_message items when all loaded messages have the user role. Mixed-role reads continue to include assistant_message items. Tests check the user-only result count and use of turn_items_user_message_idx; the bounded-history fixture row is now typed as user_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 to e59d3

      User-only reads use the partial index. No merge-blocking behavior change is established.

      Pre-merge checks: Passed 4

      Check nameStatusExplanation
      Title checkPassedThe 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 checkPassedThe 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 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.
      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

      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.ts
      • apps/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.

    5. juliusmarminge merged this5f2bb93

    6. juliusmarminge closed this

Sign in to join this conversation.