Skip to content

db: 5 fixed-shape timeline readers in events.ts still recompile Drizzle SQL and re-prepare SQLite statements on every call #8497

Done

Description

@Tiberriver256

Summary

Following #8437 / #8449 and #8439 / #8453, a full-suite duplicate-prepare() sweep and V8 CPU profile of apps/server/test/services/threads/timeline-read-equivalence-interleaved.test.ts on bdd066bc50 show that 5 fixed-shape timeline readers in packages/db/src/data/events.ts still build fresh Drizzle queries and call db.$client.prepare() on every invocation:

  1. isTimelineCursorSequencePresent (lines 2228–2244) — 6,200 duplicate prepares across the suite
  2. listTodoSnapshotEventRowsForThread (lines 2791–2808) — 6,688 duplicate prepares
  3. listTimelineInterruptionRows (lines 3703–3720) — 9,048 duplicate prepares
  4. listTimelineWindowHintsDescending (lines 3892–3906) — 9,084 duplicate prepares
  5. getLatestModelFallbackRowSinceTurnRequest (lines 4573–4601) — 6,688 duplicate prepares

All 5 take strictly fixed scalar parameters (threadId, sequence, sequenceStart, maxSeq, beforeSequence, limit) and have no dynamic IN (...) lists or optional predicates.

Environment

  • Commit: bdd066bc5076b5bdcc6d37ecec52c17ad59e9493
  • Node: v26.14.0, Linux x64

Reproduction

Run from repo root:

node --conditions=source --import tsx -e '
import { performance } from "node:perf_hooks";
import { createMigratedConnection } from "./packages/db/src/testing.ts";
import {
  isTimelineCursorSequencePresent,
  listTodoSnapshotEventRowsForThread,
  listTimelineInterruptionRows,
  listTimelineWindowHintsDescending,
  getLatestModelFallbackRowSinceTurnRequest,
} from "./packages/db/src/data/events.ts";

const db = createMigratedConnection();
const now = Date.now();
db.$client.prepare("INSERT INTO projects (id, name, created_at, updated_at) VALUES (?, ?, ?, ?)").run("p1", "P", now, now);
db.$client.prepare("INSERT INTO threads (id, project_id, provider_id, title, status, created_at, updated_at, latest_attention_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)").run("t1", "p1", "claude", "T", "idle", now, now, now);
const ins = db.$client.prepare("INSERT INTO events (id, thread_id, environment_id, scope_kind, turn_id, provider_thread_id, sequence, type, item_id, item_kind, parent_tool_call_id, data, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
for (let s = 1; s <= 200; s++) {
  const type = s % 10 === 1 ? "client/turn/requested" : "item/completed";
  ins.run(`e${s}`, "t1", null, "turn", `turn_${Math.ceil(s/10)}`, null, s, type, `i${s}`, "agentMessage", null, JSON.stringify({ type }), now + s);
}

const orig = db.$client.prepare.bind(db.$client);
let prepares = 0;
db.$client.prepare = (...a) => { prepares++; return orig(...a); };

const cases = [
  ["isTimelineCursorSequencePresent", () => isTimelineCursorSequencePresent(db, { threadId: "t1", sequence: 100 })],
  ["listTodoSnapshotEventRowsForThread", () => listTodoSnapshotEventRowsForThread(db, { threadId: "t1" })],
  ["listTimelineInterruptionRows", () => listTimelineInterruptionRows(db, { threadId: "t1", sequenceStart: 1, maxSeq: 200 })],
  ["listTimelineWindowHintsDescending", () => listTimelineWindowHintsDescending(db, { threadId: "t1", sequenceStart: 1, beforeSequence: 200, limit: 20 })],
  ["getLatestModelFallbackRowSinceTurnRequest", () => getLatestModelFallbackRowSinceTurnRequest(db, { threadId: "t1" })],
];

for (const [name, fn] of cases) {
  fn();
  prepares = 0;
  const t0 = performance.now();
  for (let i = 0; i < 1000; i++) fn();
  const ms = performance.now() - t0;
  console.log(`${name}: prepares=${prepares}/1000 (${(prepares/1000).toFixed(1)}/call), ${ms.toFixed(1)}ms (${ms.toFixed(1)}us/call)`);
}
db.$client.close();
'

Observed Output

isTimelineCursorSequencePresent: prepares=1000/1000 (1.0/call), 391.5ms (391.5us/call)
listTodoSnapshotEventRowsForThread: prepares=1000/1000 (1.0/call), 1301.6ms (1301.6us/call)
listTimelineInterruptionRows: prepares=1000/1000 (1.0/call), 610.5ms (610.5us/call)
listTimelineWindowHintsDescending: prepares=1000/1000 (1.0/call), 112.6ms (112.6us/call)
getLatestModelFallbackRowSinceTurnRequest: prepares=1000/1000 (1.0/call), 1114.0ms (1114.0us/call)

Total: 5,000 duplicate prepare() calls and 3,530.2 ms across 1,000 warm calls each (3.53 ms combined per call cycle).

Expected Behavior

Cache all 5 fixed-shape statements per connection using prepareCachedQuery / prepareCachedRawStatement (matching the pattern used in #8453 for the sibling timeline window readers in events.ts), so warm calls perform 0 SQLite prepare() calls and 0 Drizzle SQL compilations.

AGENT GENERATED

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions