Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

sql-trace: write each pool's records to the file it was created with

garrytan01e0122307bb
diff --git a/src/core/sql-trace.ts b/src/core/sql-trace.ts
index 082031683c..4865d2eb97 100644
--- a/src/core/sql-trace.ts
+++ b/src/core/sql-trace.ts
@@ -25,7 +25,8 @@ interface TraceConnection {
}
let connections = 0;
-let buffered: string[] = [];
+/** Unwritten records by trace file: each pool's records go to the file it was created with. */
+const buffered = new Map<string, string[]>();
let flushOnExit = false;
const clock = () => performance.timeOrigin + performance.now();
const SSL_REQUEST = 80877103;
@@ -37,14 +38,20 @@ function cstring(buf: Buffer, at: number): [string, number] {
}
function emit(file: string, record: Record<string, unknown>): void {
- buffered.push(JSON.stringify(record));
- if (!flushOnExit) { flushOnExit = true; process.on('exit', () => flush(file)); setInterval(() => flush(file), 1000).unref(); }
- if (buffered.length >= 500) flush(file);
+ let lines = buffered.get(file);
+ if (!lines) buffered.set(file, lines = []);
+ lines.push(JSON.stringify(record));
+ if (!flushOnExit) { flushOnExit = true; process.on('exit', flushAll); setInterval(flushAll, 1000).unref(); }
+ if (lines.length >= 500) flush(file);
+}
+function flushAll(): void {
+ for (const file of [...buffered.keys()]) flush(file);
}
function flush(file: string): void {
- if (!buffered.length) return;
- const lines = buffered.join('\n') + '\n';
- buffered = [];
+ const pending = buffered.get(file);
+ if (!pending?.length) return;
+ const lines = pending.join('\n') + '\n';
+ buffered.delete(file);
try { appendFileSync(file, lines); } catch { /* a trace that cannot be written never fails the traced process */ }
}
diff --git a/test/e2e/managed-import-statement-budget.test.ts b/test/e2e/managed-import-statement-budget.test.ts
index 92af214879..70c8ec11fe 100644
--- a/test/e2e/managed-import-statement-budget.test.ts
+++ b/test/e2e/managed-import-statement-budget.test.ts
@@ -44,7 +44,7 @@ function measured(scale: number): Promise<Measured> {
return run;
}
async function measure(scale: number): Promise<Measured> {
- // One file for every size: the trace buffers process-wide and flushes into the first file it was given.
+ // One file for every size: the trace writes each pool's records to the file it was created with.
const trace = join(home, 'trace.jsonl');
return withEnv({ GBRAIN_SQL_TRACE: trace, GBRAIN_SQL_TRACE_LABEL: 'import-budget', GBRAIN_HOME: join(home, `home-${scale}`) }, async () => {
const pg = await isolatedPersistencePostgres(process.env.DATABASE_URL!, 'instance', 6);