Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Name statements the PGLite cache prepared before a count starts
garrytan1b28ee0ba657
diff --git a/test/helpers/statement-counter.ts b/test/helpers/statement-counter.tsindex 442b659912..0ccbe01a10 100644--- a/test/helpers/statement-counter.ts+++ b/test/helpers/statement-counter.ts@@ -7,8 +7,10 @@* `execProtocolRaw` (every PGLite query, named or unnamed, every simple query* and the transaction's BEGIN and COMMIT go through it) and decodes the frontend messages the same way the wire* trace does: an Execute or a simple Query is one statement, carrying the text- * its Parse named. Both produce the same `StatementRecord` shape, so one- * classifier serves both engines.+ * its Parse named. A Bind of a statement the engine's cache (pglite-statements.ts)+ * prepared before counting started takes its text from that cache, so a warm+ * cache never relabels it as the statement before it. Both produce the same+ * `StatementRecord` shape, so one classifier serves both engines.*/import { readFileSync } from 'node:fs';import type { PGLiteEngine } from '../../src/core/pglite-engine.ts';@@ -33,6 +35,9 @@ export function countPgliteStatements(engine: PGLiteEngine): { records: Statemenconst db = engine.db as unknown as { execProtocolRaw: (message: Uint8Array, opts?: unknown) => Promise<unknown> };const original = db.execProtocolRaw;const prepared = new Map<string, string>();+ const cache = (engine as unknown as { _statements?: { statements: Map<string, { name: string }> } | null })._statements;+ const textOf = (name: string) => prepared.get(name) ?? [...cache?.statements ?? []].find(([, statement]) => statement.name === name)?.[0]+ ?? `<statement ${name} prepared before counting>`;const records: StatementRecord[] = [];let text = '';let t = 0;@@ -42,7 +47,7 @@ export function countPgliteStatements(engine: PGLiteEngine): { records: Statemenconst length = new DataView(message.buffer, message.byteOffset + at + 1, 4).getInt32(0);const body = message.subarray(at + 5, at + 1 + length);if (type === 0x50) { const [name, next] = cstring(body, 0); text = cstring(body, next)[0]; prepared.set(name, text); }- else if (type === 0x42) { const [, next] = cstring(body, 0); text = prepared.get(cstring(body, next)[0]) ?? text; }+ else if (type === 0x42) { const [, next] = cstring(body, 0); text = textOf(cstring(body, next)[0]); }else if (type === 0x45) records.push({ t: ++t, conn: 1, kind: 'execute', sql: text });else if (type === 0x51) records.push({ t: ++t, conn: 1, kind: 'simple', sql: cstring(body, 0)[0] });at += 1 + length;diff --git a/test/statement-counter-warm-cache.test.ts b/test/statement-counter-warm-cache.test.tsnew file mode 100644index 0000000000..9c589cb94f--- /dev/null+++ b/test/statement-counter-warm-cache.test.ts@@ -0,0 +1,29 @@+/**+ * GST-2422: the PGLite statement counter names a statement the engine's cache prepared before counting started.+ *+ * Protects: test/helpers/statement-counter.ts `countPgliteStatements`. pglite-statements.ts prepares a statement the+ * second time it runs and binds it by name from then on. A Bind whose Parse ran before the counter started used to take+ * the text of the statement before it, so a golden comparison (test/fold-hook-noop-6712.test.ts) failed whenever+ * something read config twice before its window: `SELECT value FROM config WHERE key = $1` came out as the+ * write-context `set_config` that preceded it.+ * Fails when: a statement prepared before `countPgliteStatements` starts is recorded with any text but its own.+ */+import { afterAll, beforeAll, expect, test } from 'bun:test';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { countPgliteStatements } from './helpers/statement-counter.ts';++let engine: PGLiteEngine;+beforeAll(async () => { engine = new PGLiteEngine(); await engine.connect({}); await engine.initSchema(); });+afterAll(async () => { await engine?.disconnect(); });++test('a statement prepared before counting starts is recorded with its own text', async () => {+ await engine.getConfig('gst2422.warm');+ await engine.getConfig('gst2422.warm');+ const counter = countPgliteStatements(engine);+ try {+ await engine.executeRaw("SELECT set_config('gst2422.before',$1,true)", ['x']);+ await engine.getConfig('gst2422.warm');+ } finally { counter.stop(); }+ expect(counter.records.filter(record => record.kind === 'execute').map(record => record.sql))+ .toEqual(["SELECT set_config('gst2422.before',$1,true)", 'SELECT value FROM config WHERE key = $1']);+});