Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(postgres): reset the driver row count at ReadyForQuery so a read after a mid-stream failure gets only its own rows

garrytan1aa2b56e04d5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7f26d3c6bb..71efe3208b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,15 @@ credits are retained; no result has been reassigned to another provider. Origina
identifiers and attribution are available in the pre-removal Git revision
`6040075c6cb95be5881cc2e1b76ef7d71f4e5d29` (retained on 2026-09-23).
+## [0.60.191.0] - 2026-10-13
+
+**A Postgres read that follows a failed statement on the same pooled connection now gets back only its own rows.** A statement that streamed rows and then failed (a runtime error after its first rows, or a cancel or timeout while it was still producing them) left its row count behind in the driver. The next statement on that connection then returned its rows after that many empty slots. So `persistence_brain`'s one-row read came back as two rows, and a managed connector sync refused with `storage_error` ("requires exactly one known boolean persistence state") before its sweep started. Before the strict readers, the same read came back as `enabled = false` and the run took the unmanaged path (#6482). A cancel at consumer stop is enough to leave the count behind.
+
+### Itemized changes
+
+- **Driver row count.** The vendored driver resets a statement's row count at every ReadyForQuery, not only at CommandComplete. The fix went in through `patches/[email protected]`. Three tests fail on the base driver: an error after the first rows, a cancel mid-stream, and a statement pipelined behind a mid-stream failure.
+- **Connector retry flake.** In the one-off `persistence-connector-retry` failure, the `--retry-failed` run threw `storage_error` with no sweep and left one failed receipt instead of two. That signature now reproduces deterministically: every pooled connection's last statement streams a row and then fails before the retry run. The new case fails before this fix and passes after it.
+
## [0.60.190.0] - 2026-10-13
**No Postgres statement outlives its budget now, a stalled managed sync says what is blocking it, and doctor stops misleading on managed and image-heavy brains (#6543, part 2).** v0.60.187.0 stopped the Supavisor wedge itself. This release closes the gaps the reporter hit around it. A statement that kept receiving bytes was never retired, so `/health?deep=1` stayed red forever. A sync stalled 300-600 s with "claimable here: no" no matter the cause. `repair failed-writes --apply` stopped at every pending write. Doctor recommended a `--retry-failed` that did nothing on a managed brain, and reported existing images as missing when only their letter case differed.
diff --git a/patches/[email protected] b/patches/[email protected]
index e7f1485d24..e4a8a7e12a 100644
--- a/patches/[email protected]
+++ b/patches/[email protected]
@@ -1,5 +1,5 @@
diff --git a/cjs/src/connection.js b/vendor/postgres/cjs/src/connection.js
-index 07f6716..16f1373 100644
+index 07f6716..a15e780 100644
--- a/cjs/src/connection.js
+++ b/vendor/postgres/cjs/src/connection.js
@@ -20,6 +20,7 @@ const Sync = b().S().end()
@@ -418,16 +418,24 @@ index 07f6716..16f1373 100644
} else {
query.resolve(results || result)
}
-@@ -550,6 +783,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -547,9 +780,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+ errored(errorResponse)
+ }
+
++ // GBrain: a statement that streamed rows and then failed (a cancel, a timeout or an error after its first rows) never
++ // reached CommandComplete, which is where the row count resets. Left in place, the next statement on this connection
++ // stored its rows after that many empty slots: a one-row read came back as [empty, row], so a reader that requires
++ // exactly one row refused and `const [row] = rows` read nothing.
query = results = errorResponse = null
result = new Result()
++ rows = 0
connectTimer.cancel()
+ inflightTimer.cancel()
+ ageTimer.cancel()
if (initial) {
if (target_session_attrs) {
-@@ -564,23 +799,43 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -564,23 +804,43 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
return fetchArrayTypes()
}
@@ -477,7 +485,7 @@ index 07f6716..16f1373 100644
: connection.reserved()
: ending
? terminate()
-@@ -630,7 +885,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -630,7 +890,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
!query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4))
query.prepare && (statements[query.signature] = query.statement)
@@ -489,7 +497,7 @@ index 07f6716..16f1373 100644
}
function RowDescription(x) {
-@@ -767,14 +1026,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -767,14 +1031,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
async function fetchArrayTypes() {
needsTypes = false
@@ -519,7 +527,7 @@ index 07f6716..16f1373 100644
types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))
}
-@@ -821,7 +1087,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -821,7 +1092,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
function retry(q, error) {
delete statements[q.signature]
q.retried = error
@@ -537,7 +545,7 @@ index 07f6716..16f1373 100644
}
function NotificationResponse(x) {
-@@ -1039,6 +1314,59 @@ function xor(a, b) {
+@@ -1039,6 +1319,59 @@ function xor(a, b) {
return buffer
}
@@ -1051,7 +1059,7 @@ index 0578284..e80a868 100644
return '$' + (types.push(
diff --git a/src/connection.js b/vendor/postgres/src/connection.js
-index 1b1cccd..9fc39c2 100644
+index 1b1cccd..7b6372c 100644
--- a/src/connection.js
+++ b/vendor/postgres/src/connection.js
@@ -20,6 +20,7 @@ const Sync = b().S().end()
@@ -1470,16 +1478,24 @@ index 1b1cccd..9fc39c2 100644
} else {
query.resolve(results || result)
}
-@@ -550,6 +783,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -547,9 +780,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+ errored(errorResponse)
+ }
+
++ // GBrain: a statement that streamed rows and then failed (a cancel, a timeout or an error after its first rows) never
++ // reached CommandComplete, which is where the row count resets. Left in place, the next statement on this connection
++ // stored its rows after that many empty slots: a one-row read came back as [empty, row], so a reader that requires
++ // exactly one row refused and `const [row] = rows` read nothing.
query = results = errorResponse = null
result = new Result()
++ rows = 0
connectTimer.cancel()
+ inflightTimer.cancel()
+ ageTimer.cancel()
if (initial) {
if (target_session_attrs) {
-@@ -564,23 +799,43 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -564,23 +804,43 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
return fetchArrayTypes()
}
@@ -1529,7 +1545,7 @@ index 1b1cccd..9fc39c2 100644
: connection.reserved()
: ending
? terminate()
-@@ -630,7 +885,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -630,7 +890,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
!query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4))
query.prepare && (statements[query.signature] = query.statement)
@@ -1541,7 +1557,7 @@ index 1b1cccd..9fc39c2 100644
}
function RowDescription(x) {
-@@ -767,14 +1026,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -767,14 +1031,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
async function fetchArrayTypes() {
needsTypes = false
@@ -1571,7 +1587,7 @@ index 1b1cccd..9fc39c2 100644
types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))
}
-@@ -821,7 +1087,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -821,7 +1092,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
function retry(q, error) {
delete statements[q.signature]
q.retried = error
@@ -1589,7 +1605,7 @@ index 1b1cccd..9fc39c2 100644
}
function NotificationResponse(x) {
-@@ -1039,6 +1314,59 @@ function xor(a, b) {
+@@ -1039,6 +1319,59 @@ function xor(a, b) {
return buffer
}
diff --git a/test/e2e/postgres-driver-row-count-postgres.test.ts b/test/e2e/postgres-driver-row-count-postgres.test.ts
new file mode 100644
index 0000000000..47874e83c1
--- /dev/null
+++ b/test/e2e/postgres-driver-row-count-postgres.test.ts
@@ -0,0 +1,55 @@
+/**
+ * The vendored driver counts a statement's rows as they arrive and resets the count at CommandComplete. A statement
+ * that streamed rows and then failed (a runtime error after its first rows, or a cancel or timeout while it was still
+ * producing them) never reaches CommandComplete, so the count stayed behind and the next statement on that connection
+ * stored its rows after that many empty slots: a one-row read came back as `[empty, row]`.
+ *
+ * gbrain's strict readers refuse such a result. A managed connector sync's first read of `persistence_brain` threw
+ * `storage_error` ("requires exactly one known boolean persistence state") before the sweep started (the
+ * `persistence-connector-retry` flake), and before those readers were strict the same read came back as
+ * `enabled = false` (#6482). The driver now resets the count at every ReadyForQuery.
+ *
+ * Regression that fails it on the base driver: each follower reads back more rows than it returned, with empty
+ * leading slots.
+ */
+import { afterEach, describe, expect, test } from 'bun:test';
+import postgres from '#postgres';
+import { assertSafeE2eDatabaseUrl } from '../helpers/db-guard.ts';
+
+const url = process.env.DATABASE_URL;
+const opened: Array<{ end: (o?: { timeout?: number }) => Promise<void> }> = [];
+afterEach(async () => { while (opened.length) await opened.pop()!.end({ timeout: 1 }).catch(() => {}); });
+
+function pool() {
+ assertSafeE2eDatabaseUrl(url!);
+ const sql = postgres(url!, { max: 1, onnotice: () => {} });
+ opened.push(sql);
+ return sql;
+}
+const code = (query: PromiseLike<unknown>) => Promise.resolve(query).then(() => 'resolved', (e: { code?: string }) => e.code ?? 'error');
+
+describe.skipIf(!url)('vendored driver row count after a statement that failed mid-stream', () => {
+ test('a runtime error after the first rows leaves the next statement its own rows', async () => {
+ const sql = pool();
+ expect(await code(sql.unsafe('SELECT g, 1 / (g - 3) AS v FROM generate_series(1, 5) g'))).toBe('22012');
+ expect(Array.from(await sql.unsafe('SELECT true AS enabled'))).toEqual([{ enabled: true }]);
+ });
+
+ test('a statement cancelled while it was still producing rows leaves the next statement its own rows', async () => {
+ const sql = pool();
+ const running = sql.unsafe('SELECT g, pg_sleep(CASE WHEN g = 2 THEN 30 ELSE 0 END) FROM generate_series(1, 2) g');
+ const settled = code(running);
+ await new Promise(resolve => setTimeout(resolve, 500));
+ running.cancel();
+ expect(await settled).toBe('57014');
+ expect(Array.from(await sql.unsafe('SELECT true AS enabled'))).toEqual([{ enabled: true }]);
+ });
+
+ test('a statement pipelined behind one that fails mid-stream gets exactly its own rows', async () => {
+ const sql = pool();
+ const failing = code(sql.unsafe('SELECT g, 1 / (g - 3) AS v FROM generate_series(1, 5) g'));
+ const follower = sql.unsafe('SELECT 1 AS n UNION ALL SELECT 2');
+ expect(await failing).toBe('22012');
+ expect(Array.from(await follower)).toEqual([{ n: 1 }, { n: 2 }]);
+ });
+});
diff --git a/test/persistence-connector-retry.test.ts b/test/persistence-connector-retry.test.ts
index 6b344b5dc9..84fdd828be 100644
--- a/test/persistence-connector-retry.test.ts
+++ b/test/persistence-connector-retry.test.ts
@@ -249,6 +249,24 @@ test('a retry pointer survives real purge and compaction without silently renewi
}
}), 120_000);
+test('a pooled connection whose last statement failed after streaming a row does not stop the retry run before its sweep', async () => withEnv(env, async () => {
+ for (const engine of engines) {
+ if (engine.kind !== 'postgres') continue;
+ const f = await retryFixture(engine, 'google', false);
+ await engine.executeRaw('UPDATE connector_retry_faults SET pages=true WHERE source_id=$1', [f.id]);
+ await expect(f.run()).rejects.toMatchObject({ code: 'storage_error' });
+ await disposePersistenceConsumer(engine);
+ // Every pooled connection's last statement streams one row and then fails (as a cancel at consumer stop can).
+ await Promise.allSettled(Array.from({ length: 16 }, () =>
+ engine.executeRaw('SELECT g, 1 / (g - 2) AS v FROM (SELECT g FROM generate_series(1, 2) g, pg_sleep(0.2)) s')));
+ await expect(f.run(true)).rejects.toMatchObject({ code: 'storage_error', writeRequest: { state: 'failed' } });
+ await disposePersistenceConsumer(engine);
+ const failures = await engine.executeRaw<WriteRequest>("SELECT * FROM persistence_requests WHERE source_id=$1 AND state='failed' ORDER BY sequence", [f.id]);
+ expect(failures).toHaveLength(2);
+ expect(failures[1].intent?.retryOf).toBe(failures[0].request_id);
+ }
+}), 120_000);
+
test('concurrent connector approvals and a restarted paused owner retain one pending replacement receipt', async () => withEnv(env, async () => {
for (const engine of engines) {
const f = await retryFixture(engine, 'github', true);
diff --git a/vendor/README.md b/vendor/README.md
index a6a21b97c0..4fb82db38a 100644
--- a/vendor/README.md
+++ b/vendor/README.md
@@ -74,6 +74,13 @@ to share types in: GBrain passes one per database target, so every pool in a
process that reaches one database shares it, and `src/core/pg-type-cache.ts`
saves it for the next process.
+Each statement's row count resets at its ReadyForQuery. Stock postgres.js
+resets it only at CommandComplete. A statement that streams rows and then fails
+never gets there: a runtime error after its first rows, or a cancel or timeout
+while it is still producing them. The next statement on that connection then
+stored its rows after that many empty slots, so a one-row read came back as
+`[empty, row]` (`test/e2e/postgres-driver-row-count-postgres.test.ts`).
+
## Reproduce and update
Run `bash vendor/update-postgres.sh --check` to download the pinned upstream
diff --git a/vendor/postgres/cf/src/connection.js b/vendor/postgres/cf/src/connection.js
index 41437f1e3a..0d102fe1f3 100644
--- a/vendor/postgres/cf/src/connection.js
+++ b/vendor/postgres/cf/src/connection.js
@@ -782,8 +782,13 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
errored(errorResponse)
}
+ // GBrain: a statement that streamed rows and then failed (a cancel, a timeout or an error after its first rows) never
+ // reached CommandComplete, which is where the row count resets. Left in place, the next statement on this connection
+ // stored its rows after that many empty slots: a one-row read came back as [empty, row], so a reader that requires
+ // exactly one row refused and `const [row] = rows` read nothing.
query = results = errorResponse = null
result = new Result()
+ rows = 0
connectTimer.cancel()
inflightTimer.cancel()
ageTimer.cancel()
diff --git a/vendor/postgres/cjs/src/connection.js b/vendor/postgres/cjs/src/connection.js
index 16f1373b64..a15e780e19 100644
--- a/vendor/postgres/cjs/src/connection.js
+++ b/vendor/postgres/cjs/src/connection.js
@@ -780,8 +780,13 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
errored(errorResponse)
}
+ // GBrain: a statement that streamed rows and then failed (a cancel, a timeout or an error after its first rows) never
+ // reached CommandComplete, which is where the row count resets. Left in place, the next statement on this connection
+ // stored its rows after that many empty slots: a one-row read came back as [empty, row], so a reader that requires
+ // exactly one row refused and `const [row] = rows` read nothing.
query = results = errorResponse = null
result = new Result()
+ rows = 0
connectTimer.cancel()
inflightTimer.cancel()
ageTimer.cancel()
diff --git a/vendor/postgres/src/connection.js b/vendor/postgres/src/connection.js
index 9fc39c2cb6..7b6372cbe4 100644
--- a/vendor/postgres/src/connection.js
+++ b/vendor/postgres/src/connection.js
@@ -780,8 +780,13 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
errored(errorResponse)
}
+ // GBrain: a statement that streamed rows and then failed (a cancel, a timeout or an error after its first rows) never
+ // reached CommandComplete, which is where the row count resets. Left in place, the next statement on this connection
+ // stored its rows after that many empty slots: a one-row read came back as [empty, row], so a reader that requires
+ // exactly one row refused and `const [row] = rows` read nothing.
query = results = errorResponse = null
result = new Result()
+ rows = 0
connectTimer.cancel()
inflightTimer.cancel()
ageTimer.cancel()