Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): filter pending items before fetching history payloads #23926

Closedazogz(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/23926/headopened View on GitHub

Problem

getThreadRecords(..., { turnItemStatuses: ['pending', 'running', 'waiting'] }) scans the thread's ordering index, then seeks every historical table row to read status. Large tool payloads make this expensive even when only a few items are pending. Idle-compaction checks use this read inside a writer transaction, which can delay other database work.

Change

Migration 069 extends the existing (thread_id, ordinal, turn_item_id) index with status. SQLite can reject finished items from the index before fetching their payloads, while preserving the existing search and ordering prefix. The narrower index is replaced because leaving both versions available can cause SQLite to keep choosing the slow plan.

The query, returned data, and wire contract stay the same. The migration rebuilds one index on upgrade; status changes now maintain that index as well.

Scope and approval

This is a focused performance fix for an existing read, submitted under the small, obvious-bug exception in CONTRIBUTING.md. It changes one index through the normal migration path. It is separate from the shell stop-request optimization in #23751.

Verification

  • Added the regression test first. With migration 069 omitted, it fails because SQLite reads status from the table. With the migration, it verifies the actual traced query reads status from the ordering-index cursor, and returns the same ordered filtered records, including tied ordinals and empty selections.
  • Upgrade test preserves an existing history row, checks a second migration run applies nothing, and passes SQLite quick_check. The regular projection tests cover fresh database creation.
  • vp run check:changed passed format, lint, knip, GraphQL generation, server/scripts type checks, and all 50 tests across ProjectionStore.test.ts, Migrations.test.ts, and reconcileV2PreviewMigration.test.ts.
  • Synthetic reproduction and runtime transcript: Node 26.12.1, in-memory SQLite, 46,000 rows with 4 KiB text payloads and five running items. Five warm samples after one warm-up: median 8.07 ms → 1.63 ms, identical results, quick_check: ok. Run the attached script with node benchmark-pending-status.cjs. This is an isolated SQL benchmark; it does not establish an end-to-end reconnect improvement. Write throughput was not benchmarked. The evidence contains synthetic data only.

Built with GPT-6 in Codex.

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

    2. Approvability

      Verdict: Approved at 1a99344

      Macroscope's review found this PR approvable — This is a narrowly scoped persistence performance fix that replaces one existing SQLite index while preserving query results, ordering, stored data, and the API contract. Added tests cover both the optimized query plan and upgrade/idempotence behavior; no product defaults or static-analysis configuration are affected.

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

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration
      📥 Commits

      Reviewing files that changed from the base of the PR and between 18e27425195686a5215fdc2cfa2bca088cdb8972 and 1a99344eb7809822641ba257f363923a03a63eef.

      📒 Files selected for processing: 5
      • apps/server/src/orchestration-v2/ProjectionStore.test.ts
      • apps/server/src/persistence/Migrations.test.ts
      • apps/server/src/persistence/Migrations.ts
      • apps/server/src/persistence/Migrations/069_TurnItemOrderedStatusIndex.ts
      • apps/server/src/persistence/reconcileV2PreviewMigration.test.ts

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

      📝 Walkthrough

      Migration 69 recreates the projection turn-item index with a status column. The migration registry includes it, and tests check status-filtered query planning, data preservation, index column order, and migration results.

      Changes: Turn-item status index

      Layer / File(s)Summary
      Index migration and validation
      apps/server/src/persistence/Migrations/069_TurnItemOrderedStatusIndex.ts, apps/server/src/persistence/Migrations.ts, apps/server/src/orchestration-v2/ProjectionStore.test.ts, apps/server/src/persistence/Migrations.test.ts, apps/server/src/persistence/reconcileV2PreviewMigration.test.ts
      Migration 69 drops and recreates the turn-item index with columns (thread_id, ordinal, turn_item_id, status), and the registry includes the migration. Tests check query-plan index use, status filtering, row preservation, index column order, and migration results.

      Priority: ⬇️ Low

      Estimated code review effort: 2 (Simple) | ~12 minutes

      Change: Refactor

      Possibly related issues

      • pingdotgg/t3code#21578 — The issue reports slow recovery reads for unfinished turn items. This change adds status to the ordered index used by a status-filtered read.
      • pingdotgg/t3code#23000 — The issue proposes an ordered turn-item index for pending reads. This change recreates that index with an additional status column.
      • pingdotgg/t3code#20427 — The issue concerns repeated payload fetching during history reads. This change tests status filtering before payload lookup, but does not remove the full-thread page rebuild.

      Suggested reviewers: juliusmarminge, t3dotgg

      Merge Risk: ⚪ Minimal · up to 1a993

      The index change is ready to merge after normal checks; no actionable risk remains in the supplied evidence.

      Pre-merge checks: Passed 4
      Check nameStatusExplanation
      Title checkPassedThe title clearly and concisely describes the main server performance fix: filtering pending items before fetching history payloads.
      Description checkPassedThe description includes the required Problem, Change, Scope and approval, and Verification sections. It explains the performance issue, migration design, scope rationale, focused tests, benchmark res…
      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: Description check

      Explanation

      The description includes the required Problem, Change, Scope and approval, and Verification sections. It explains the performance issue, migration design, scope rationale, focused tests, benchmark results, and verification limits.

      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
      Comment `@coderabbitai help` to get the list of available commands.
    4. tanstack-source added labelsbug

    5. This PR conflicts with main. Rebase it or merge main into it. If it still conflicts after 7 days with no activity, it will be closed automatically.

      See the conflict window.

    6. Closing: this PR still conflicts with main and has had no activity for 7 days since the rebase request.

      To continue, rebase the branch and open a new PR that links this one. See the conflict window.

    7. github-actions[bot](GitHub profile, opens in a new tab) removed labelsneeds-rebase

Sign in to join this conversation.