diff --git a/.jules/bolt.md b/.jules/bolt.md index 92ad5715b76..e9b6abcf006 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -42,3 +42,7 @@ ## 2026-06-21 - [Avoid Unnecessary Redis pings] **Learning:** Repeatedly calling `redis_client.ping()` as a precondition check before actually executing redis commands creates an unnecessary N+1 round-trip performance bottleneck. If a command fails due to connection issues, the underlying library will raise an exception (like `redis.exceptions.ConnectionError`) regardless. **Action:** Avoid explicit `ping()` calls for connection validation before issuing commands. Rely on exception handling blocks around the actual commands. + +## 2026-03-31 - [Promise-based Repository Initialization Guard] +**Learning:** Calling schema setup functions like `ensureTable` (containing `CREATE TABLE/INDEX IF NOT EXISTS`) on every database operation creates a redundant round-trip bottleneck. Using a promise-based singleton guard ensures this logic runs only once per application lifecycle while safely handling concurrent calls. +**Action:** Always implement a module-level `initializationPromise` to guard repository schema setup logic. diff --git a/server/src/db/repositories/uat.ts b/server/src/db/repositories/uat.ts index 6e81fd318cf..c58156e5ead 100644 --- a/server/src/db/repositories/uat.ts +++ b/server/src/db/repositories/uat.ts @@ -3,6 +3,11 @@ import baseLogger from '../../config/logger.js'; const logger = baseLogger.child({ name: 'uat-repo' }); +// BOLT OPTIMIZATION: Guard initialization to prevent redundant schema checks on every operation. +// This reduces database round-trips for high-frequency UAT event logging. +// Pattern also used in high-throughput tickets repository. +let initializationPromise: Promise | null = null; + export type UATCheckpoint = { run_id: string; checkpoint: string; @@ -13,26 +18,33 @@ export type UATCheckpoint = { }; async function ensureTable() { - const pool = getPostgresPool(); - try { - await pool.query(` - CREATE TABLE IF NOT EXISTS maestro_uat_checkpoints ( - id SERIAL PRIMARY KEY, - run_id TEXT NOT NULL, - checkpoint TEXT NOT NULL, - verdict TEXT NOT NULL, - evidence_uris JSONB, - actor TEXT, - created_at TIMESTAMPTZ DEFAULT NOW() + if (initializationPromise) return initializationPromise; + + initializationPromise = (async () => { + const pool = getPostgresPool(); + try { + await pool.query(` + CREATE TABLE IF NOT EXISTS maestro_uat_checkpoints ( + id SERIAL PRIMARY KEY, + run_id TEXT NOT NULL, + checkpoint TEXT NOT NULL, + verdict TEXT NOT NULL, + evidence_uris JSONB, + actor TEXT, + created_at TIMESTAMPTZ DEFAULT NOW() + ); + CREATE INDEX IF NOT EXISTS idx_uat_run ON maestro_uat_checkpoints(run_id); + `); + } catch (e: any) { + logger.warn( + { err: e }, + 'ensureTable maestro_uat_checkpoints failed (mock mode?)', ); - CREATE INDEX IF NOT EXISTS idx_uat_run ON maestro_uat_checkpoints(run_id); - console.log(`); - } catch (e: any) { - logger.warn( - { err: e }, - 'ensureTable maestro_uat_checkpoints failed (mock mode?)', - ); - } + initializationPromise = null; + } + })(); + + return initializationPromise; } export async function addUATCheckpoint(c: UATCheckpoint) {