Skip to content

db: 4 of 6 timeline window queries in events.ts were left uncached and recompile Drizzle SQL on every thread open and scroll #8439

Description

@Tiberriver256

Summary

While two timeline window queries in packages/db/src/data/events.ts already use prepareCachedQueryVariant (advanceTimelineWindowBudgetFloorSequence and listStoredTimelineWindowEventRows), the other four hot-path timeline window queries called by selectStandardTimelineEventRows (apps/server/src/services/threads/timeline.ts#L1080) on every GET /api/v1/threads/:id/timeline open and backward scroll were left uncached:

On every thread open and scroll, all four synchronously rebuild Drizzle query ASTs and re-prepare better-sqlite3 statements on Node's main event loop.

Versions & Environment

  • Commit: 272957819d (origin/main)
  • Node: v26.13.1
  • OS: Linux x64

Root Cause

All four functions invoke .all()/.get() on freshly built Drizzle query builders instead of using prepareCachedQueryVariant keyed on their finite SQL-shape dimensions (matching listStoredTimelineWindowEventRows at events.ts#L4113 and advanceTimelineWindowBudgetFloorSequence at events.ts#L3641-L3665):

  • maxInlineOutputChars (since truncatedEventDataColumn in packages/db/src/data/event-output-truncation.ts#L20-L38 inlines maxInlineOutputChars and omits json_replace when null)
  • hasBeforeSequence (beforeSequence !== undefined)
  • excludeDiagnosticEvents === true
  • excludedTypes (empty vs. fixed key excludedTypes.join(","))

Reproduction

Run from repo root at 272957819d:

pnpm exec tsx -e '
import { createMigratedConnection } from "./packages/db/src/testing.ts";
import { noopNotifier } from "./packages/db/src/notifier.ts";
import { findTimelineWindowBudgetFloorSequence, listTimelineRootWindowTurnIds, listStoredTimelineThreadWindowEventRows, listStoredThreadNoticeAndInteractionRows } from "./packages/db/src/data/events.ts";
import { createThread } from "./packages/db/src/data/threads.ts";
import { ensurePersonalProject } from "./packages/db/src/data/projects.ts";

const db = createMigratedConnection();
const project = ensurePersonalProject(db);
const thread = createThread(db, noopNotifier, { projectId: project.id, providerId: "codex" });

function countPrepares(fn: () => void) {
  let count = 0;
  const orig = db.$client.prepare.bind(db.$client);
  db.$client.prepare = ((...a: Parameters<typeof orig>) => { count++; return orig(...a); }) as typeof orig;
  try { fn(); return count; } finally { db.$client.prepare = orig; }
}

findTimelineWindowBudgetFloorSequence(db, { threadId: thread.id, sequenceStart: 0, eventBudget: 100, excludedTypes: [] });
const floor10 = countPrepares(() => { for (let i = 0; i < 10; i++) findTimelineWindowBudgetFloorSequence(db, { threadId: thread.id, sequenceStart: 0, eventBudget: 100, excludedTypes: [] }); });
listTimelineRootWindowTurnIds(db, { threadId: thread.id, sequenceStart: 0, maxInlineOutputChars: 8192 });
const roots10 = countPrepares(() => { for (let i = 0; i < 10; i++) listTimelineRootWindowTurnIds(db, { threadId: thread.id, sequenceStart: 0, maxInlineOutputChars: 8192 }); });
listStoredTimelineThreadWindowEventRows(db, { threadId: thread.id, sequenceStart: 0, maxInlineOutputChars: 8192 });
const win10 = countPrepares(() => { for (let i = 0; i < 10; i++) listStoredTimelineThreadWindowEventRows(db, { threadId: thread.id, sequenceStart: 0, maxInlineOutputChars: 8192 }); });
listStoredThreadNoticeAndInteractionRows(db, { threadId: thread.id, sequenceStart: 0, maxInlineOutputChars: 8192 });
const notice10 = countPrepares(() => { for (let i = 0; i < 10; i++) listStoredThreadNoticeAndInteractionRows(db, { threadId: thread.id, sequenceStart: 0, maxInlineOutputChars: 8192 }); });
console.log(JSON.stringify({ findFloor_10warm: floor10, listRoots_10warm: roots10, listThreadWin_10warm: win10, listNotice_10warm: notice10 }));
db.$client.close();
'

Actual output

{"findFloor_10warm":10,"listRoots_10warm":10,"listThreadWin_10warm":10,"listNotice_10warm":10}

Expected behavior

  • All four functions reuse per-connection prepared statements via prepareCachedQueryVariant, matching sibling advanceTimelineWindowBudgetFloorSequence (#L3641-L3665) and listStoredTimelineWindowEventRows (#L4105-L4140).
  • Warm calls with identical shapes execute 0 new SQLiteSyncDialect.prototype.sqlToQuery compilations and 0 new db.$client.prepare() calls.

What was ruled out

  • Confirmed sequenceStart is always required in storedTimelineWindowConditions (events.ts#L3893-L3909), and all four queries have bounded variant keys matching the existing sibling pattern at events.ts#L4113.

AGENT GENERATED

Activity

  1. added theissue type on Dec 1, 2026
  2. added
    threads
    confirmed-repro
    on Dec 1, 2026
  3. bb-slop-cop commented on Dec 1, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · Priority: Medium · Effort: Low · Labels: threads, perf, confirmed-repro.
    • Report: https://get-bb.github.io/reports/issues/8439.html — REPRODUCED, high root-cause confidence; repeated by the same agent in a second clean trusted checkout.
    • Root cause: Four timeline readers execute fresh Drizzle builders rather than cached prepared variants. Each reader performs 108 compilations and preparations across 108 warmed reads; the regression expects zero.
    • Ready fix: Cache prepared statements for timeline window readers #8453 — 422 changed text lines across two DB files; not merged.
    • Verification: Warm-reuse regressions fail before and pass after; 117 relevant DB tests, 45 server timeline tests, typecheck, type-aware lint, and diff checks pass. Two unrelated full-DB queue-plan suite failures also reproduce on unchanged main.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-reproperfthreads

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions