Repository navigation
Description
Summary
packages/db/src/data/threads.ts already imports prepareCachedQuery (used by getThread, getThreadStatus, and listActiveHostThreads) to avoid synchronous Drizzle AST compilation and SQLite statement preparation on Node's main event loop. However, four fixed-shape hot-path queries in the same file were missed and still recompile and re-prepare SQL on every invocation:
deleteThreadSearchSegmentById(called byupsertThreadSearchSegmentwhen trimmed text is empty)upsertThreadSearchSegment(called inside every daemon/server event-append transaction viapackages/db/src/data/events.ts#L922and#L1233)getThreadStartupContext(called viaapps/server/src/services/threads/thread-startup-store.ts#L192)isThreadCopyingForkHistory(called on every fork-history copy transaction batch viaapps/server/src/services/threads/thread-fork-history.ts#L376)
Versions & Environment
- Commit:
272957819d(origin/main) - Node:
v26.13.1 - OS: Linux x64
Root Cause
All four functions build fresh Drizzle query objects and call .run() / .get() directly instead of using prepareCachedQuery with sql.placeholder(...) bindings like sibling functions getThread (#L461), listActiveHostThreads (#L1882), and getThreadStatus (#L2588). prepareTransaction (packages/db/src/connection.ts#L443-L455) shares the per-connection compiledQueries cache across tx instances, so caching works inside write transactions too.
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 { createThread, getThread, upsertThreadSearchSegments, getThreadStartupContext, isThreadCopyingForkHistory } 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; }
}
getThread(db, thread.id);
const control10 = countPrepares(() => { for (let i = 0; i < 10; i++) getThread(db, thread.id); });
upsertThreadSearchSegments(db, { segments: [{ threadId: thread.id, sourceKind: "user-message", sourceKey: "k-w", sourceSeq: 1, text: "warm" }] });
const upsert10 = countPrepares(() => {
for (let i = 0; i < 10; i++) {
upsertThreadSearchSegments(db, { segments: [{ threadId: thread.id, sourceKind: "user-message", sourceKey: `k-${i}`, sourceSeq: i + 2, text: `hello ${i}` }] });
}
});
upsertThreadSearchSegments(db, { segments: [{ threadId: thread.id, sourceKind: "user-message", sourceKey: "k-d", sourceSeq: 1, text: "" }] });
const delete10 = countPrepares(() => {
for (let i = 0; i < 10; i++) {
upsertThreadSearchSegments(db, { segments: [{ threadId: thread.id, sourceKind: "user-message", sourceKey: `k-${i}`, sourceSeq: i + 2, text: "" }] });
}
});
getThreadStartupContext(db, thread.id);
const startup10 = countPrepares(() => { for (let i = 0; i < 10; i++) getThreadStartupContext(db, thread.id); });
isThreadCopyingForkHistory(db, thread.id);
const fork10 = countPrepares(() => { for (let i = 0; i < 10; i++) isThreadCopyingForkHistory(db, thread.id); });
console.log(JSON.stringify({ getThread_control_10warm: control10, upsertSearch_10warm: upsert10, deleteSearch_10warm: delete10, getStartupContext_10warm: startup10, isCopyingFork_10warm: fork10 }));
db.$client.close();
'Actual output
{"getThread_control_10warm":0,"upsertSearch_10warm":10,"deleteSearch_10warm":10,"getStartupContext_10warm":10,"isCopyingFork_10warm":10}Expected behavior
- All four fixed-shape functions reuse per-connection prepared statements via
prepareCachedQuerywithsql.placeholder(...)bindings (keepingupsertThreadSearchSegmentBatchuntouched). - Warm calls execute
0newSQLiteSyncDialect.prototype.sqlToQuerycompilations and0newdb.$client.prepare()calls, matching thegetThreadcontrol.
What was ruled out
- Confirmed all four queries have static SQL structure and fixed parameter counts; no dynamic
IN (...)lists or optionalWHEREclauses.
AGENT GENERATED
Activity
bb-slop-cop commentedon Dec 1, 2026
🚨 SLOP COP 🚨 ·
new-issue-autopilotthreads,perf, andconfirmed-repro; no existing classifications were replaced.Cache prepared statements for thread indexing and startup reads (get-…
Cache prepared statements for thread indexing and startup reads (get-…