perf(queue): prepare the transition wake lookup once per repository (#2013)

Queue history reads call the wake lookup once per transition. Preparing the same indexed statement for each call added avoidable work to health-diagnosis reads with long histories. Prepare it lazily once per repository instance and keep every lookup fresh; no query results are cached. Availability, ordering, misses and errors retain their behavior.

Validation: 140 affected tests and daemon typecheck pass. Independent Codex and Claude source reviews passed on the identical two-file change. A real-database test checks a warmed miss, insertion, update and deletion.

In one synthetic daemon replay with 100 owned diagnoses and 6,900 history transitions, reference/candidate/candidate/reference evaluation means were 918/759/745/1,014 ms: combined 966 → 752 ms. All 12 evaluations retained complete coverage and all 16 native-consumer checks passed. Reference drift was 96 ms and individual ranges overlapped. This is a local improvement on that workload; the recurring severe-stall cause remains unresolved.
This commit is contained in:
Mike Schwarz authored and GitHub committed 2026-10-25 08:02:25 -07:00
1 parent b7f8833b5a
commit fcd8db792e
2 files changed
+28 -3

No files matched your search

@@ -56,6 +56,9 @@ interface WakeRow {
/** Persistence + live-state projection for transition-bound park wakes. */
export class QueueWakeRepository {
private readonly available: boolean;
/** One lookup statement for this connection, prepared on first use: history reads call it once per transition, and
* compiling it each time dominated their cost. It holds no result; every call reads afresh. */
private lookup: Database.Statement | undefined;
constructor(private readonly db: Database.Database) {
this.available = this.db.prepare(
@@ -81,9 +84,8 @@ export class QueueWakeRepository {
getForTransition(transitionId: number): QueueWakeRecord | null {
if (!this.available) return null;
const row = this.db.prepare(
"SELECT * FROM queue_transition_wakes WHERE transition_id = ?",
).get(transitionId) as WakeRow | undefined;
this.lookup ??= this.db.prepare("SELECT * FROM queue_transition_wakes WHERE transition_id = ?");
const row = this.lookup.get(transitionId) as WakeRow | undefined;
return row ? this.row(row) : null;
}
@@ -1452,3 +1452,26 @@ describe("S03 R25 — a park records its wake on the append-only transition", ()
});
});
});
describe("wake lookup reads afresh on every call", () => {
it("a warmed miss, then a recorded wake, then an update are each seen by the next lookup", async () => {
const { ALL_MIGRATIONS } = await import("../src/db/all-migrations.js");
const { QueueWakeRepository } = await import("../src/domain/queue-wake-repository.js");
const db = createDb();
try {
migrate(db, ALL_MIGRATIONS);
db.exec("INSERT INTO queue_items (qitem_id, ts_created, ts_updated, source_session, destination_session, state, priority, body) VALUES ('q1', '2026-01-01T00:00:00Z', '2026-01-01T00:00:00Z', 'a@r', 'b@r', 'blocked', 'routine', 'x')");
const transitionId = Number(db.prepare("INSERT INTO queue_transitions (qitem_id, ts, state, actor_session) VALUES ('q1', '2026-01-01T00:00:00Z', 'blocked', 'b@r')").run().lastInsertRowid);
const wakes = new QueueWakeRepository(db);
// Warm the lookup with a miss, and an unknown id.
expect(wakes.getForTransition(transitionId)).toBeNull();
expect(wakes.getForTransition(transitionId + 999)).toBeNull();
wakes.record({ transitionId, qitemId: "q1", phase: "armed", kind: "timer", ref: "30s", deliveryStatus: null });
expect(wakes.getForTransition(transitionId)).toMatchObject({ transitionId, qitemId: "q1", phase: "armed", kind: "timer", ref: "30s", deliveryStatus: null });
db.prepare("UPDATE queue_transition_wakes SET delivery_status = 'delivered' WHERE transition_id = ?").run(transitionId);
expect(wakes.getForTransition(transitionId)?.deliveryStatus).toBe("delivered");
db.prepare("DELETE FROM queue_transition_wakes WHERE transition_id = ?").run(transitionId);
expect(wakes.getForTransition(transitionId)).toBeNull();
} finally { db.close(); }
});
});