Repository navigation
Description
Summary
getNonDestroyedHost (packages/db/src/data/hosts.ts), getThreadStatus, and listActiveHostThreads (packages/db/src/data/threads.ts) run synchronously on Node's main event loop during host/entity lookups (apps/server/src/routes/hosts.ts#L90, apps/server/src/services/lib/entity-lookup.ts#L172, apps/server/src/ws/host-shared-ports.ts#L238,L279), thread lifecycle checks (apps/server/src/services/threads/thread-startup-store.ts#L144), and daemon reconnects (apps/server/src/internal/session-owner-side-effects.ts#L352), yet rebuild their Drizzle SQL ASTs and call sqlite3_prepare_v2 (better-sqlite3 prepare()) on every single call.
On clean origin/main (b0ddaad731), 1,000 calls on an empty table show extreme synchronous main-thread overhead compared to their already-compiled siblings getHost and getThread in the exact same files:
getHost: 2.2 ms (1prepare) vsgetNonDestroyedHost: 256.6 ms (1,000prepares — 116× slower)getThread: 7.4 ms (1prepare) vslistActiveHostThreads: 367.9 ms (50× slower) andgetThreadStatus: 50.1 ms (7× slower)
Versions & Environment
- Commit:
b0ddaad731565d7e4de68d824120d802fc78f7d5(origin/main) - Node:
v26.13.1 - OS: Linux x64
Root Cause
packages/db/src/data/hosts.ts#L177-L196:getHostusesprepareCachedQuery(db, prepareGetHost)withplaceholder("id"), whilegetNonDestroyedHostright below it rebuildsdb.select().from(hosts).where(and(eq(hosts.id, id), isNull(hosts.destroyedAt))).get()on every call.packages/db/src/data/threads.ts#L1869-L1884&packages/db/src/data/threads.ts#L2576-L2587:getThread(#L461) usesprepareCachedQuery, whilelistActiveHostThreadsandgetThreadStatusrebuild their Drizzle queries and re-prepare on every call.
Reproduction
Run from repo root at b0ddaad731565d7e4de68d824120d802fc78f7d5:
node --conditions=source --import tsx -e '
import { performance } from "node:perf_hooks";
import { createMigratedConnection } from "./packages/db/src/testing.ts";
import { ensurePersonalProject } from "./packages/db/src/data/projects.ts";
import { createThread, getThread, getThreadStatus, listActiveHostThreads } from "./packages/db/src/data/threads.ts";
import { getHost, getNonDestroyedHost } from "./packages/db/src/data/hosts.ts";
import { noopNotifier } from "./packages/db/src/notifier.ts";
function measure(n: number, fn: (db: ReturnType<typeof createMigratedConnection>, threadId: string) => void) {
const db = createMigratedConnection();
const project = ensurePersonalProject(db);
const thread = createThread(db, noopNotifier, { projectId: project.id, providerId: "codex", status: "idle" });
let prepares = 0;
const orig = db.$client.prepare.bind(db.$client);
db.$client.prepare = ((sql: string) => { prepares++; return orig(sql); }) as typeof db.$client.prepare;
const t0 = performance.now();
for (let i = 0; i < n; i++) fn(db, thread.id);
const ms = +(performance.now() - t0).toFixed(1);
db.$client.close();
return { prepares, ms };
}
console.log(JSON.stringify({
getHost_cached: measure(1000, (db) => getHost(db, "h1")),
getNonDestroyedHost: measure(1000, (db) => getNonDestroyedHost(db, "h1")),
getThread_cached: measure(1000, (db, id) => getThread(db, id)),
getThreadStatus: measure(1000, (db, id) => getThreadStatus(db, id)),
listActiveHostThreads: measure(1000, (db) => listActiveHostThreads(db, { hostId: "h1" })),
}));
'Actual output
{"getHost_cached":{"prepares":1,"ms":2.2},"getNonDestroyedHost":{"prepares":1000,"ms":256.6},"getThread_cached":{"prepares":1,"ms":7.4},"getThreadStatus":{"prepares":1000,"ms":50.1},"listActiveHostThreads":{"prepares":1000,"ms":367.9}}Expected behavior
getNonDestroyedHost,getThreadStatus, andlistActiveHostThreadsshould compile and prepare once per connection (prepares: 1for 1,000 calls) viaprepareCachedQuery+placeholder(...)(already imported in bothhosts.tsandthreads.ts), bringing latency in line withgetHostandgetThread.- Scope is limited to
packages/db/src/data/hosts.tsandpackages/db/src/data/threads.ts(pnpm exec turbo run test --filter=@bb/db).
What was ruled out
- Confirmed all 3 queries have fixed SQL shapes with a single scalar parameter (
id/hostId).
AGENT GENERATED
Activity
bb-slop-cop commentedon Nov 29, 2026
🚨 SLOP COP 🚨 ·
new-issue-autopilotthreads,host·perf.confirmed-repro.Cache fixed-shape lifecycle database queries (get-bb#8366)
Cache fixed-shape lifecycle database queries (get-bb#8366)