Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Driver: one stale cached plan makes every pooled connection prepare its statements again
garrytan5b8c6e7cd8ba
diff --git a/patches/[email protected] b/patches/[email protected]index b176c86ba3..13960369bf 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..2978d0b 100644+index 07f6716..222249b 100644--- a/cjs/src/connection.js+++ b/vendor/postgres/cjs/src/connection.js@@ -20,6 +20,7 @@@@ -10,7 +10,21 @@ index 07f6716..2978d0b 100644, noop = () => { /* noop */ }const retryRoutines = new Set([-@@ -49,7 +50,7 @@+@@ -28,6 +29,13 @@+ 'transformAssignedExpr'+ ])+++// GBrain (GST-2260): a statement whose result type changed (another session added, renamed or dropped a column of a++// table it reads with SELECT * or RETURNING *) fails on every connection that prepared it. Stock postgres.js forgets it++// only on the connection that failed, so each pooled connection failed once, and inside a transaction the caller saw++// the failure. The first one in the process starts a new plan generation: each connection drops its prepared++// statements before it builds its next query, and prepares them again.++let planGeneration = 0+++ const errorFields = {+ 83 : 'severity_local', // S+ 86 : 'severity', // V+@@ -49,7 +57,7 @@82 : 'routine' // R}@@ -19,7 +33,7 @@ index 07f6716..2978d0b 100644const {sslnegotiation,ssl,-@@ -64,6 +65,7 @@+@@ -64,6 +72,7 @@onnotify,onparameter,max_pipeline,@@ -27,7 +41,7 @@ index 07f6716..2978d0b 100644keep_alive,backoff,target_session_attrs-@@ -75,9 +77,14 @@+@@ -75,9 +84,14 @@, idleTimer = timer(end, options.idle_timeout), lifeTimer = timer(end, options.max_lifetime), connectTimer = timer(connectTimedOut, options.connect_timeout)@@ -42,7 +56,15 @@ index 07f6716..2978d0b 100644, errorResponse = null, result = new Result(), incoming = Buffer.alloc(0)-@@ -95,6 +102,9 @@+@@ -86,6 +100,7 @@+ , statements = {}+ , statementId = Math.random().toString(36).slice(2)+ , statementCount = 1++ , statementGeneration = planGeneration+ , closedTime = 0+ , remaining = 0+ , hostIndex = 0+@@ -95,6 +110,9 @@, rows = 0, serverSignature = null, nextWriteTimer = null@@ -52,7 +74,7 @@ index 07f6716..2978d0b 100644, terminated = false, incomings = null, results = null-@@ -105,11 +115,19 @@+@@ -105,11 +123,19 @@, ended = null, nonce = null, query = null@@ -72,7 +94,7 @@ index 07f6716..2978d0b 100644connect(query) {initial = queryreconnect()-@@ -143,11 +161,13 @@+@@ -143,11 +169,13 @@}async function cancel({ pid, secret }, resolve, reject) {@@ -88,7 +110,7 @@ index 07f6716..2978d0b 100644} catch (error) {reject(error)}-@@ -163,22 +183,174 @@+@@ -163,22 +191,174 @@if (q.cancelled)return@@ -269,7 +291,11 @@ index 07f6716..2978d0b 100644}}-@@ -235,10 +407,15 @@+@@ -232,13 +412,19 @@+ q.prepare = options.prepare && ('prepare' in q.options ? q.options.prepare : true)+ q.string = string+ q.signature = q.prepare && types + string++ statementGeneration !== planGeneration && (statements = {}, statementGeneration = planGeneration)q.onlyDescribe && (delete statements[q.signature])q.parameters = q.parameters || parametersq.prepared = q.prepare && q.signature in statements@@ -287,7 +313,7 @@ index 07f6716..2978d0b 100644typeof options.debug === 'function' && options.debug(id, string, parameters, types)}-@@ -295,10 +472,11 @@+@@ -295,10 +481,11 @@/* c8 ignore next 3 */function drain() {@@ -300,7 +326,7 @@ index 07f6716..2978d0b 100644if (incomings) {incomings.push(x)remaining -= x.length-@@ -330,6 +508,9 @@+@@ -330,6 +517,9 @@remaining = 0incomings = null}@@ -310,7 +336,7 @@ index 07f6716..2978d0b 100644}async function connect() {-@@ -366,6 +547,7 @@+@@ -366,6 +556,7 @@try {statements = {}needsTypes = options.fetch_types@@ -318,7 +344,7 @@ index 07f6716..2978d0b 100644statementId = Math.random().toString(36).slice(2)statementCount = 1lifeTimer.start()-@@ -379,20 +561,35 @@+@@ -379,20 +570,35 @@}function error(err) {@@ -354,7 +380,7 @@ index 07f6716..2978d0b 100644function queryError(query, err) {if (query.reserve)return query.reject(err)-@@ -437,20 +634,53 @@+@@ -437,20 +643,53 @@incoming = Buffer.alloc(0)remaining = 0incomings = null@@ -410,7 +436,7 @@ index 07f6716..2978d0b 100644closedTime = performance.now()hadError && options.shared.retries++delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000-@@ -459,6 +689,10 @@+@@ -459,6 +698,10 @@/* Handlers */function handle(xs, x = xs[0]) {@@ -421,7 +447,7 @@ index 07f6716..2978d0b 100644(x === 68 ? DataRow : // Dx === 100 ? CopyData : // d-@@ -489,6 +723,39 @@+@@ -489,6 +732,39 @@)(xs)}@@ -461,7 +487,7 @@ index 07f6716..2978d0b 100644function DataRow(x) {let index = 7let length-@@ -533,13 +800,25 @@+@@ -533,13 +809,26 @@}function ReadyForQuery(x) {@@ -475,6 +501,7 @@ index 07f6716..2978d0b 100644if (errorResponse) {+ // GBrain: a failed statement describes again next time instead of trusting shared parameter types.+ query.sharedTypes && options.shared_types.delete(query.typesKey)++ errorResponse.routine === 'RevalidateCachedQuery' && planGeneration++query.retried? errored(query.retried)- : query.prepared && retryRoutines.has(errorResponse.routine)@@ -488,7 +515,7 @@ index 07f6716..2978d0b 100644} else {query.resolve(results || result)}-@@ -547,9 +826,16 @@+@@ -547,9 +836,16 @@errored(errorResponse)}@@ -505,7 +532,7 @@ index 07f6716..2978d0b 100644if (initial) {if (target_session_attrs) {-@@ -559,28 +845,54 @@+@@ -559,28 +855,54 @@return terminate()}@@ -566,7 +593,7 @@ index 07f6716..2978d0b 100644: connection.reserved(): ending? terminate()-@@ -630,7 +942,11 @@+@@ -630,7 +952,11 @@!query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4))query.prepare && (statements[query.signature] = query.statement)@@ -578,7 +605,7 @@ index 07f6716..2978d0b 100644}function RowDescription(x) {-@@ -767,15 +1083,23 @@+@@ -767,15 +1093,23 @@async function fetchArrayTypes() {needsTypes = false@@ -610,7 +637,7 @@ index 07f6716..2978d0b 100644}function addArrayType(oid, typarray) {-@@ -821,7 +1145,16 @@+@@ -821,7 +1155,16 @@function retry(q, error) {delete statements[q.signature]q.retried = error@@ -628,7 +655,7 @@ index 07f6716..2978d0b 100644}function NotificationResponse(x) {-@@ -1039,6 +1372,59 @@+@@ -1039,6 +1382,59 @@return buffer}@@ -1266,7 +1293,7 @@ index 0578284..e80a868 100644return '$' + (types.push(diff --git a/src/connection.js b/vendor/postgres/src/connection.js-index 1b1cccd..28f3e50 100644+index 1b1cccd..084ac14 100644--- a/src/connection.js+++ b/vendor/postgres/src/connection.js@@ -20,6 +20,7 @@@@ -1277,7 +1304,21 @@ index 1b1cccd..28f3e50 100644, noop = () => { /* noop */ }const retryRoutines = new Set([-@@ -49,7 +50,7 @@+@@ -28,6 +29,13 @@+ 'transformAssignedExpr'+ ])+++// GBrain (GST-2260): a statement whose result type changed (another session added, renamed or dropped a column of a++// table it reads with SELECT * or RETURNING *) fails on every connection that prepared it. Stock postgres.js forgets it++// only on the connection that failed, so each pooled connection failed once, and inside a transaction the caller saw++// the failure. The first one in the process starts a new plan generation: each connection drops its prepared++// statements before it builds its next query, and prepares them again.++let planGeneration = 0+++ const errorFields = {+ 83 : 'severity_local', // S+ 86 : 'severity', // V+@@ -49,7 +57,7 @@82 : 'routine' // R}@@ -1286,7 +1327,7 @@ index 1b1cccd..28f3e50 100644const {sslnegotiation,ssl,-@@ -64,6 +65,7 @@+@@ -64,6 +72,7 @@onnotify,onparameter,max_pipeline,@@ -1294,7 +1335,7 @@ index 1b1cccd..28f3e50 100644keep_alive,backoff,target_session_attrs-@@ -75,9 +77,14 @@+@@ -75,9 +84,14 @@, idleTimer = timer(end, options.idle_timeout), lifeTimer = timer(end, options.max_lifetime), connectTimer = timer(connectTimedOut, options.connect_timeout)@@ -1309,7 +1350,15 @@ index 1b1cccd..28f3e50 100644, errorResponse = null, result = new Result(), incoming = Buffer.alloc(0)-@@ -95,6 +102,9 @@+@@ -86,6 +100,7 @@+ , statements = {}+ , statementId = Math.random().toString(36).slice(2)+ , statementCount = 1++ , statementGeneration = planGeneration+ , closedTime = 0+ , remaining = 0+ , hostIndex = 0+@@ -95,6 +110,9 @@, rows = 0, serverSignature = null, nextWriteTimer = null@@ -1319,7 +1368,7 @@ index 1b1cccd..28f3e50 100644, terminated = false, incomings = null, results = null-@@ -105,11 +115,19 @@+@@ -105,11 +123,19 @@, ended = null, nonce = null, query = null@@ -1339,7 +1388,7 @@ index 1b1cccd..28f3e50 100644connect(query) {initial = queryreconnect()-@@ -143,11 +161,13 @@+@@ -143,11 +169,13 @@}async function cancel({ pid, secret }, resolve, reject) {@@ -1355,7 +1404,7 @@ index 1b1cccd..28f3e50 100644} catch (error) {reject(error)}-@@ -163,22 +183,174 @@+@@ -163,22 +191,174 @@if (q.cancelled)return@@ -1536,7 +1585,11 @@ index 1b1cccd..28f3e50 100644}}-@@ -235,10 +407,15 @@+@@ -232,13 +412,19 @@+ q.prepare = options.prepare && ('prepare' in q.options ? q.options.prepare : true)+ q.string = string+ q.signature = q.prepare && types + string++ statementGeneration !== planGeneration && (statements = {}, statementGeneration = planGeneration)q.onlyDescribe && (delete statements[q.signature])q.parameters = q.parameters || parametersq.prepared = q.prepare && q.signature in statements@@ -1554,7 +1607,7 @@ index 1b1cccd..28f3e50 100644typeof options.debug === 'function' && options.debug(id, string, parameters, types)}-@@ -295,10 +472,11 @@+@@ -295,10 +481,11 @@/* c8 ignore next 3 */function drain() {@@ -1567,7 +1620,7 @@ index 1b1cccd..28f3e50 100644if (incomings) {incomings.push(x)remaining -= x.length-@@ -330,6 +508,9 @@+@@ -330,6 +517,9 @@remaining = 0incomings = null}@@ -1577,7 +1630,7 @@ index 1b1cccd..28f3e50 100644}async function connect() {-@@ -366,6 +547,7 @@+@@ -366,6 +556,7 @@try {statements = {}needsTypes = options.fetch_types@@ -1585,7 +1638,7 @@ index 1b1cccd..28f3e50 100644statementId = Math.random().toString(36).slice(2)statementCount = 1lifeTimer.start()-@@ -379,20 +561,35 @@+@@ -379,20 +570,35 @@}function error(err) {@@ -1621,7 +1674,7 @@ index 1b1cccd..28f3e50 100644function queryError(query, err) {if (query.reserve)return query.reject(err)-@@ -437,20 +634,53 @@+@@ -437,20 +643,53 @@incoming = Buffer.alloc(0)remaining = 0incomings = null@@ -1677,7 +1730,7 @@ index 1b1cccd..28f3e50 100644closedTime = performance.now()hadError && options.shared.retries++delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000-@@ -459,6 +689,10 @@+@@ -459,6 +698,10 @@/* Handlers */function handle(xs, x = xs[0]) {@@ -1688,7 +1741,7 @@ index 1b1cccd..28f3e50 100644(x === 68 ? DataRow : // Dx === 100 ? CopyData : // d-@@ -489,6 +723,39 @@+@@ -489,6 +732,39 @@)(xs)}@@ -1728,7 +1781,7 @@ index 1b1cccd..28f3e50 100644function DataRow(x) {let index = 7let length-@@ -533,13 +800,25 @@+@@ -533,13 +809,26 @@}function ReadyForQuery(x) {@@ -1742,6 +1795,7 @@ index 1b1cccd..28f3e50 100644if (errorResponse) {+ // GBrain: a failed statement describes again next time instead of trusting shared parameter types.+ query.sharedTypes && options.shared_types.delete(query.typesKey)++ errorResponse.routine === 'RevalidateCachedQuery' && planGeneration++query.retried? errored(query.retried)- : query.prepared && retryRoutines.has(errorResponse.routine)@@ -1755,7 +1809,7 @@ index 1b1cccd..28f3e50 100644} else {query.resolve(results || result)}-@@ -547,9 +826,16 @@+@@ -547,9 +836,16 @@errored(errorResponse)}@@ -1772,7 +1826,7 @@ index 1b1cccd..28f3e50 100644if (initial) {if (target_session_attrs) {-@@ -559,28 +845,54 @@+@@ -559,28 +855,54 @@return terminate()}@@ -1833,7 +1887,7 @@ index 1b1cccd..28f3e50 100644: connection.reserved(): ending? terminate()-@@ -630,7 +942,11 @@+@@ -630,7 +952,11 @@!query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4))query.prepare && (statements[query.signature] = query.statement)@@ -1845,7 +1899,7 @@ index 1b1cccd..28f3e50 100644}function RowDescription(x) {-@@ -767,15 +1083,23 @@+@@ -767,15 +1093,23 @@async function fetchArrayTypes() {needsTypes = false@@ -1877,7 +1931,7 @@ index 1b1cccd..28f3e50 100644}function addArrayType(oid, typarray) {-@@ -821,7 +1145,16 @@+@@ -821,7 +1155,16 @@function retry(q, error) {delete statements[q.signature]q.retried = error@@ -1895,7 +1949,7 @@ index 1b1cccd..28f3e50 100644}function NotificationResponse(x) {-@@ -1039,6 +1372,59 @@+@@ -1039,6 +1382,59 @@return buffer}diff --git a/vendor/README.md b/vendor/README.mdindex bc75f1c459..88da88d897 100644--- a/vendor/README.md+++ b/vendor/README.md@@ -142,6 +142,22 @@ while it is still producing them. The next statement on that connection thenstored 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`).+A statement whose result type changed heals every connection in the process,+not only the one that failed. When another session adds, renames or drops a+column of a table that a prepared `SELECT *` or `RETURNING *` reads, Postgres+answers that statement's next run with SQLSTATE 0A000 "cached plan must not+change result type" (routine `RevalidateCachedQuery`) on every connection+that prepared it. Stock postgres.js forgets the statement only on the failing+connection and runs it again, which cannot work inside a transaction the error+already aborted, so each pooled connection failed once. With the patch the+first such error starts a new process-wide plan generation, and each+connection drops its prepared statements before it builds its next query (the+server-side statements stay until the connection closes; new ones take new+names). A `prepare: false` pool keeps no prepared statements, so nothing+changes there. GBrain's write retry loops treat the aborted transaction as+transient (`isStalePlanError` in `src/core/retry-matcher.ts`;+`test/stale-plan-heal.test.ts`).+An idle connection keeps nothing from its last statement. Stock postgres.jsleft its receive buffer as an empty view over the last message it joined fromseveral socket reads, so the buffer stayed allocated until the connection'sdiff --git a/vendor/postgres/cf/src/connection.js b/vendor/postgres/cf/src/connection.jsindex ec56c8c022..4c661265f6 100644--- a/vendor/postgres/cf/src/connection.js+++ b/vendor/postgres/cf/src/connection.js@@ -31,6 +31,13 @@ const retryRoutines = new Set(['transformAssignedExpr'])+// GBrain (GST-2260): a statement whose result type changed (another session added, renamed or dropped a column of a+// table it reads with SELECT * or RETURNING *) fails on every connection that prepared it. Stock postgres.js forgets it+// only on the connection that failed, so each pooled connection failed once, and inside a transaction the caller saw+// the failure. The first one in the process starts a new plan generation: each connection drops its prepared+// statements before it builds its next query, and prepares them again.+let planGeneration = 0+const errorFields = {83 : 'severity_local', // S86 : 'severity', // V@@ -95,6 +102,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose, statements = {}, statementId = Math.random().toString(36).slice(2), statementCount = 1+ , statementGeneration = planGeneration, closedTime = 0, remaining = 0, hostIndex = 0@@ -406,6 +414,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseq.prepare = options.prepare && ('prepare' in q.options ? q.options.prepare : true)q.string = stringq.signature = q.prepare && types + string+ statementGeneration !== planGeneration && (statements = {}, statementGeneration = planGeneration)q.onlyDescribe && (delete statements[q.signature])q.parameters = q.parameters || parametersq.prepared = q.prepare && q.signature in statements@@ -812,6 +821,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseif (errorResponse) {// GBrain: a failed statement describes again next time instead of trusting shared parameter types.query.sharedTypes && options.shared_types.delete(query.typesKey)+ errorResponse.routine === 'RevalidateCachedQuery' && planGeneration++query.retried? errored(query.retried): query.prepared && retryRoutines.has(errorResponse.routine) && !query.cancelleddiff --git a/vendor/postgres/cjs/src/connection.js b/vendor/postgres/cjs/src/connection.jsindex 9580c67878..222249b912 100644--- a/vendor/postgres/cjs/src/connection.js+++ b/vendor/postgres/cjs/src/connection.js@@ -29,6 +29,13 @@ const retryRoutines = new Set(['transformAssignedExpr'])+// GBrain (GST-2260): a statement whose result type changed (another session added, renamed or dropped a column of a+// table it reads with SELECT * or RETURNING *) fails on every connection that prepared it. Stock postgres.js forgets it+// only on the connection that failed, so each pooled connection failed once, and inside a transaction the caller saw+// the failure. The first one in the process starts a new plan generation: each connection drops its prepared+// statements before it builds its next query, and prepares them again.+let planGeneration = 0+const errorFields = {83 : 'severity_local', // S86 : 'severity', // V@@ -93,6 +100,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose, statements = {}, statementId = Math.random().toString(36).slice(2), statementCount = 1+ , statementGeneration = planGeneration, closedTime = 0, remaining = 0, hostIndex = 0@@ -404,6 +412,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseq.prepare = options.prepare && ('prepare' in q.options ? q.options.prepare : true)q.string = stringq.signature = q.prepare && types + string+ statementGeneration !== planGeneration && (statements = {}, statementGeneration = planGeneration)q.onlyDescribe && (delete statements[q.signature])q.parameters = q.parameters || parametersq.prepared = q.prepare && q.signature in statements@@ -810,6 +819,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseif (errorResponse) {// GBrain: a failed statement describes again next time instead of trusting shared parameter types.query.sharedTypes && options.shared_types.delete(query.typesKey)+ errorResponse.routine === 'RevalidateCachedQuery' && planGeneration++query.retried? errored(query.retried): query.prepared && retryRoutines.has(errorResponse.routine) && !query.cancelleddiff --git a/vendor/postgres/src/connection.js b/vendor/postgres/src/connection.jsindex 89345ba83b..084ac14e51 100644--- a/vendor/postgres/src/connection.js+++ b/vendor/postgres/src/connection.js@@ -29,6 +29,13 @@ const retryRoutines = new Set(['transformAssignedExpr'])+// GBrain (GST-2260): a statement whose result type changed (another session added, renamed or dropped a column of a+// table it reads with SELECT * or RETURNING *) fails on every connection that prepared it. Stock postgres.js forgets it+// only on the connection that failed, so each pooled connection failed once, and inside a transaction the caller saw+// the failure. The first one in the process starts a new plan generation: each connection drops its prepared+// statements before it builds its next query, and prepares them again.+let planGeneration = 0+const errorFields = {83 : 'severity_local', // S86 : 'severity', // V@@ -93,6 +100,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose, statements = {}, statementId = Math.random().toString(36).slice(2), statementCount = 1+ , statementGeneration = planGeneration, closedTime = 0, remaining = 0, hostIndex = 0@@ -404,6 +412,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseq.prepare = options.prepare && ('prepare' in q.options ? q.options.prepare : true)q.string = stringq.signature = q.prepare && types + string+ statementGeneration !== planGeneration && (statements = {}, statementGeneration = planGeneration)q.onlyDescribe && (delete statements[q.signature])q.parameters = q.parameters || parametersq.prepared = q.prepare && q.signature in statements@@ -810,6 +819,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseif (errorResponse) {// GBrain: a failed statement describes again next time instead of trusting shared parameter types.query.sharedTypes && options.shared_types.delete(query.typesKey)+ errorResponse.routine === 'RevalidateCachedQuery' && planGeneration++query.retried? errored(query.retried): query.prepared && retryRoutines.has(errorResponse.routine) && !query.cancelled