Repository navigation
Description
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:
findTimelineWindowBudgetFloorSequencelistTimelineRootWindowTurnIdslistStoredTimelineThreadWindowEventRowslistStoredThreadNoticeAndInteractionRows
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(sincetruncatedEventDataColumninpackages/db/src/data/event-output-truncation.ts#L20-L38inlinesmaxInlineOutputCharsand omitsjson_replacewhennull)hasBeforeSequence(beforeSequence !== undefined)excludeDiagnosticEvents === trueexcludedTypes(empty vs. fixed keyexcludedTypes.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 siblingadvanceTimelineWindowBudgetFloorSequence(#L3641-L3665) andlistStoredTimelineWindowEventRows(#L4105-L4140). - Warm calls with identical shapes execute
0newSQLiteSyncDialect.prototype.sqlToQuerycompilations and0newdb.$client.prepare()calls.
What was ruled out
- Confirmed
sequenceStartis always required instoredTimelineWindowConditions(events.ts#L3893-L3909), and all four queries have bounded variant keys matching the existing sibling pattern atevents.ts#L4113.
AGENT GENERATED
Activity
bb-slop-cop commentedon Dec 1, 2026
🚨 SLOP COP 🚨 ·
new-issue-autopilotthreads,perf,confirmed-repro.Cache prepared statements for timeline window readers (get-bb#8453)
Cache prepared statements for timeline window readers (get-bb#8453)