Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(#7183): read-write transactions keep GBRA-156's pipelined first statement (begin({ first }) at the master merge)
garrytane88987bf45e8
diff --git a/src/core/postgres-engine/read-write-transactions.ts b/src/core/postgres-engine/read-write-transactions.tsindex e3afa217e8..c50dc40891 100644--- a/src/core/postgres-engine/read-write-transactions.ts+++ b/src/core/postgres-engine/read-write-transactions.ts@@ -43,20 +43,23 @@ function standbyRefusal(error: unknown): boolean {return codeOf(error) === '0A000' && /read-write mode during recovery/i.test(String((error as { message?: unknown }).message ?? ''));}-export async function beginReadWrite<T>(pool: Sql, fn: (tx: postgres.TransactionSql) => Promise<T>): Promise<T> {+/** `options.first` is the pipelined-first-statement test (GBRA-156), kept beside the read-write mode. */+export async function beginReadWrite<T>(pool: Sql, fn: (tx: postgres.TransactionSql) => Promise<T>, options?: { first?: unknown }): Promise<T> {let entered = false;+ const run = (tx: postgres.TransactionSql) => { entered = true; return fn(tx); };try {- return await pool.begin('read write', tx => { entered = true; return fn(tx); }) as T;+ return await (options ? pool.begin({ ...options, mode: 'read write' } as never, run) : pool.begin('read write', run)) as T;} catch (error) {if (entered || !standbyRefusal(error)) throw error;- return await pool.begin(fn) as T;+ return await (options ? pool.begin(options as never, fn) : pool.begin(fn)) as T;}}-/** A pool whose `begin` opens read-write transactions; a transaction or reserved handle is returned unchanged. */+/** A pool whose `begin` (with or without options) opens read-write transactions; a transaction or reserved handle is returned unchanged. */export function readWriteTransactions(conn: Sql): Sql {if (!isPool(conn)) return conn;- return { begin: (fn: (tx: postgres.TransactionSql) => Promise<unknown>) => beginReadWrite(conn, fn) } as unknown as Sql;+ type Body = (tx: postgres.TransactionSql) => Promise<unknown>;+ return { begin: (options: Body | { first?: unknown }, fn?: Body) => typeof options === 'function' ? beginReadWrite(conn, options) : beginReadWrite(conn, fn!, options) } as unknown as Sql;}export async function retryReadOnlyAutocommit<T>(conn: Sql, run: (conn: Sql) => Promise<T>): Promise<T> {diff --git a/test/inherited-read-only-7183.test.ts b/test/inherited-read-only-7183.test.tsindex 99ee416a43..b42c9d7dd9 100644--- a/test/inherited-read-only-7183.test.ts+++ b/test/inherited-read-only-7183.test.ts@@ -20,17 +20,19 @@ const readOnly = (statement = 'INSERT') => Object.assign(new Error(`cannot execu/** A pool double: `begin(options, fn)` records its options and runs `fn` on a transaction handle. */function fakePool(opts: { beginError?: unknown } = {}) {const begins: string[] = [];+ const raw: unknown[] = [];const tx = { tag: 'tx' };const pool = {reserve: () => { throw new Error('unused'); },begin: async (options: unknown, fn?: (tx: unknown) => Promise<unknown>) => {const run = typeof options === 'function' ? options as (tx: unknown) => Promise<unknown> : fn!;begins.push(typeof options === 'string' ? options : '');+ raw.push(options);if (opts.beginError && typeof options === 'string') throw opts.beginError;return run(tx);},};- return { pool: pool as never, begins, tx };+ return { pool: pool as never, begins, raw, tx };}describe('#7183 read-write transaction seams', () => {@@ -47,6 +49,14 @@ describe('#7183 read-write transaction seams', () => {expect(begins).toEqual(['read write', '']);});+ test('a pipelined-first transaction (GBRA-156 `{ first }`) keeps its first-statement test and opens READ WRITE', async () => {+ const { pool, raw, tx } = fakePool();+ const first = (text: string) => text.startsWith('select');+ const begin = readWriteTransactions(pool).begin as unknown as (options: unknown, fn: (handle: unknown) => Promise<unknown>) => Promise<unknown>;+ expect(await begin({ first }, async handle => handle)).toBe(tx);+ expect(raw).toEqual([{ first, mode: 'read write' }]);+ });+test('a transaction or reserved handle is not wrapped (a savepoint keeps its parent transaction)', () => {const handle = { begin: () => undefined } as never;expect(readWriteTransactions(handle)).toBe(handle);