Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): events stop maintaining two indexes no query needs #19623

ClosedKiroshiAiviom(GitHub profile, opens in a new tab)wants to merge 3 commits intomain fromrefs/pull/19623/headopened View on GitHub

Problem

Every event commit updates all 9 indexes on orchestration_events. Two of them serve no query: nothing filters, joins or sorts on correlation_id, and every reader of idx_orch_events_stream_sequence has an equal or cheaper index available. Together they cost about 2.6 WAL pages per event (part of #19105).

Change

  • Migration 063 drops idx_orch_events_correlation_id and idx_orch_events_stream_sequence.
  • Three per-thread agent reads are pinned with INDEXED BY idx_orchestration_events_agent_stream_sequence, like the file's existing pins. Without planner statistics, SQLite otherwise picks application_sequence for them and ranges over every V2 event (160× slower). The pinned queries always include the partial index's application_event_version = 2 AND aggregate_kind = 'thread' terms, so the pin always prepares.
  • idx_orchestration_events_application_sequence stays: latestAgentSequence() and the shell MAX(sequence) need it.

Scope and approval

#19105 is open and triaged by maintainers as a performance bug, naming "fewer pages per commit" as the remaining lever. This PR only removes index maintenance; it does not change commit grouping or projections.

Verification

  • Migration test: migrate to 62, then 63; both indexes are gone, the other six remain.
  • Plan test (25k events, no statistics): the per-thread read, the read with eventType and getAgentReplayStats return the right rows and plan SEARCH ... USING INDEX idx_orchestration_events_agent_stream_sequence. It fails with the migration alone, reproducing the regression the pins prevent.
  • 79 persistence/migration tests and 435 tests in callers (ShellStream, ClaudeAdapterV2, ThreadFind, recovery, ingestion) pass. Typecheck and lint clean.

Query plans before/after on a replica of the real schema (200k events), all 33 orchestration_events read shapes, in three stats modes (none / ANALYZE / the server's PRAGMA optimize). Times are ms in no-stats mode:

QueryBeforeAfter
readAgentEvents(threadId)stream_sequence, 0.78agent_stream_sequence, 0.67
readAgentEvents(threadId, eventType)stream_sequence, 0.67agent_stream_sequence, 0.61
readAgentEvents(threadId, bounded)stream_sequence, 0.41agent_stream_sequence, 0.41
getAgentReplayStatsstream_sequence, 0.65agent_stream_sequence, 0.67
Drop only, no pins: the three reads above—application_sequence, 161–192
LegacyV1 existing-rows read, migrate-dev-dbstream_sequencestream_version, same cost
The other 26 shapes—same plan

WAL per event, one event per commit, 3,000 events:

PagesBytesTime
Before23.2795.9 KB0.286 ms
After20.68 (−11%)85.2 KB0.176 ms

An older server opening a migrated database stays fast: the server runs PRAGMA optimize right after migrating, which gives it stats that steer those reads to the agent index (0.87 ms).

Collisions: #19451 also adds migration 063, and #19407 lists both indexes in plannerStatistics.ts. Whichever lands second renumbers or removes those two rows.

Not checked: plans against a production database with stale statistics.

Written by Claude Opus 5.5 in Claude Code.

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

    2. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      Important

      Review skipped

      Review was skipped as selected files did not have any reviewable changes.

      ⚙️ Run configuration
      • Configuration used: Path: .coderabbit.config.ts
      • Review profile: CHILL
      • Plan: Advanced
      • Run ID: 3091d75f-5432-42ad-b713-fbb8d46d58d5
      📥 Commits

      Reviewing files that changed from the base of the PR and between a396768240699f6cd5b38ec9049afbd2b98f4e01 and b48bfc0abdc7cf69d2c3c230a7927613b8efc975.

      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

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration
      • Configuration used: Path: .coderabbit.config.ts
      • Review profile: CHILL
      • Plan: Advanced
      • Run ID: 8f0f1284-d9e0-4b52-9d84-d28c9712a097
      📥 Commits

      Reviewing files that changed from the base of the PR and between 348f217a853dd4a23d7fa4834dedb05f545c2075 and 07ab2ce24c982ab5996dbe3da9e537a2307780e3.

      📒 Files selected for processing: 8
      • apps/server/src/orchestration-v2/FoundationPersistence.test.ts
      • apps/server/src/persistence/Migrations.ts
      • apps/server/src/persistence/Migrations/055_OrchestrationV2.test.ts
      • apps/server/src/persistence/Migrations/063_DropUnusedEventIndexes.test.ts
      • apps/server/src/persistence/Migrations/063_DropUnusedEventIndexes.ts
      • apps/server/src/persistence/OrchestrationEventStore.sequence.test.ts
      • apps/server/src/persistence/OrchestrationEventStore.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; 7 remain after this review.

      📝 Walkthrough

      The event store now selects indexes for thread-scoped reads and replay statistics. Migration 63 drops two event indexes and is included in the migration registry and migration tests.

      Changes: Orchestration event indexes

      Layer / File(s)Summary
      Thread-scoped event read indexes
      apps/server/src/persistence/OrchestrationEventStore.ts, apps/server/src/persistence/OrchestrationEventStore.sequence.test.ts
      Application-row reads and replay statistics use the agent stream/sequence index for thread-scoped queries. The test checks returned events, replay counts, and query plans.
      Register and verify index removal
      apps/server/src/persistence/Migrations/063_DropUnusedEventIndexes.ts, apps/server/src/persistence/Migrations.ts, apps/server/src/persistence/Migrations/063_DropUnusedEventIndexes.test.ts, apps/server/src/persistence/Migrations/055_OrchestrationV2.test.ts, apps/server/src/persistence/reconcileV2PreviewMigration.test.ts, apps/server/src/orchestration-v2/FoundationPersistence.test.ts
      Migration 63 drops the correlation-ID and stream-sequence indexes. Migration registration and expectations include migration 63. The migration test checks which indexes remain.

      Priority: ➖ Normal

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Bug fix

      Suggested reviewers: juliusmarminge

      Merge Risk: ⚪ Minimal · up to 07ab2

      The change drops two unused event indexes and pins three thread-scoped reads to a retained index. It reduces per-event write cost, and no concrete merge-blocking risk was found.

      Security architecture risk: 🔵 Low · up to 07ab2

      The change preserves event-identity constraints and query filtering while removing two lookup indexes. No introduced security issue was established. Interrupted or concurrent migration execution and application-version rollback remain only partially validated.

      Retained concerns No architecture-level concerns identified.

      Security review details

      Security Blast Radius

      • inferred — The directly affected asset is the server's orchestration-event database and its read workload. The inspected changes alter index maintenance and access plans, not row ownership, caller authority, or the set of rows selected by existing filters.

      Trust Boundaries and Controls

      • observed — Index names are selected from fixed SQL fragments. Thread IDs, command IDs, event types, and sequence bounds remain SQL value interpolations; caller input does not become an index identifier or raw SQL in the changed queries.

      Resilience and Maintainability Implications

      • inferred — If interruption leaves only one index dropped, the remaining integrity constraints are unaffected. Repeating the two DROP INDEX IF EXISTS statements can converge without restoring or rewriting event rows. This property does not independently establish migration-ledger atomicity or concurrent-migrator behavior.
      Pre-merge checks: Passed 4
      Passed Passed checks: 4 passed
      Check nameStatusExplanation
      Title checkPassedThe title clearly and concisely describes the main performance change: removing unnecessary event indexes. It follows the conventional commit format.
      Description checkPassedThe description covers the problem, implementation, scope, migration effects, query-plan safeguards, verification results, performance measurements, known collision risks, and an unchecked production-…
      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.
      ✨ 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.
    3. Note

      Written by claude-opus-5-5 on behalf of Maria

      Thanks for this. I'm closing it as part of PR triage. Right now we're only taking focused bug fixes from outside contributors (see Read this first), and this PR is not one.

      From the review: Dropping the correlation and stream-sequence indexes and forcing query indexes is performance work. Schema-replica timings and synthetic commits do not establish improvement through an actual provider turn; maintainer agreement is needed.

      If you think this was a mistake, message me on Twitter at @maria_rcks and I'll take another look.

    4. maria-rcks closed this

Sign in to join this conversation.