Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(postgres): keep onexecute ahead of write() in execute() (the #5466/#5560 backpressure hunk the #1236 rewrite had reverted)
garrytane3c3a3d86a8a
diff --git a/patches/[email protected] b/patches/[email protected]index 7dba03bd8d..d237f27d93 100644--- a/patches/[email protected]+++ b/patches/[email protected]@@ -1,7 +1,7 @@-diff --git a/cjs/src/connection.js b/cjs/src/connection.js-index 07f67167..9224858c 100644+diff --git a/cjs/src/connection.js b/vendor/postgres/cjs/src/connection.js+index 07f67167..472ac81e 100644--- a/cjs/src/connection.js-+++ b/cjs/src/connection.js++++ b/vendor/postgres/cjs/src/connection.js@@ -20,6 +20,7 @@ const Sync = b().S().end(), SSLRequest = b().i32(8).i32(80877103).end(8), ExecuteUnnamed = Buffer.concat([b().E().str(b.N).i32(0).end(), Sync])@@ -63,7 +63,7 @@ index 07f67167..9224858c 100644} catch (error) {reject(error)}-@@ -167,7 +176,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -167,21 +176,70 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseq.state = backendquery? sent.push(q)@@ -71,10 +71,13 @@ index 07f67167..9224858c 100644+ : (query = q, query.active = true, armInflight())build(q)++ const mayPipeline = !q.options.onexecute || q.options.onexecute(connection)return write(toBuffer(q))-@@ -176,12 +185,60 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+ && !q.describeFirst+ && !q.cursorFn&& sent.length < max_pipeline- && (!q.options.onexecute || q.options.onexecute(connection))+- && (!q.options.onexecute || q.options.onexecute(connection))++ && mayPipeline} catch (error) {- sent.length === 0 && write(Sync)- errored(error)@@ -135,7 +138,7 @@ index 07f67167..9224858c 100644function toBuffer(q) {if (q.parameters.length >= 65534)throw Errors.generic('MAX_PARAMETERS_EXCEEDED', 'Max number of parameters (65534) exceeded')-@@ -235,10 +292,15 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -235,10 +293,15 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseq.onlyDescribe && (delete statements[q.signature])q.parameters = q.parameters || parametersq.prepared = q.prepare && q.signature in statements@@ -153,7 +156,7 @@ index 07f67167..9224858c 100644typeof options.debug === 'function' && options.debug(id, string, parameters, types)}-@@ -295,10 +357,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -295,10 +358,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose/* c8 ignore next 3 */function drain() {@@ -166,7 +169,7 @@ index 07f67167..9224858c 100644if (incomings) {incomings.push(x)remaining -= x.length-@@ -379,6 +442,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -379,6 +443,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose}function error(err) {@@ -175,7 +178,7 @@ index 07f67167..9224858c 100644if (connection.queue === queues.connecting && options.host[retries + 1])return-@@ -388,6 +453,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -388,6 +454,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose}function errored(err) {@@ -184,7 +187,7 @@ index 07f67167..9224858c 100644stream && (stream.destroy(err), stream = null)query && queryError(query, err)initial && (queryError(initial, err), initial = null)-@@ -438,19 +505,38 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -438,19 +506,38 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseremaining = 0incomings = nullclearImmediate(nextWriteTimer)@@ -224,7 +227,7 @@ index 07f67167..9224858c 100644closedTime = performance.now()hadError && options.shared.retries++delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000-@@ -533,8 +619,13 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -533,8 +620,13 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose}function ReadyForQuery(x) {@@ -238,7 +241,7 @@ index 07f67167..9224858c 100644query.retried? errored(query.retried): query.prepared && retryRoutines.has(errorResponse.routine)-@@ -550,6 +641,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -550,6 +642,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclosequery = results = errorResponse = nullresult = new Result()connectTimer.cancel()@@ -246,7 +249,7 @@ index 07f67167..9224858c 100644if (initial) {if (target_session_attrs) {-@@ -564,23 +656,25 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -564,23 +657,25 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclosereturn fetchArrayTypes()}@@ -276,7 +279,7 @@ index 07f67167..9224858c 100644: connection.reserved(): ending? terminate()-@@ -630,6 +724,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -630,6 +725,9 @@ 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)@@ -286,7 +289,7 @@ index 07f67167..9224858c 100644query.describeFirst && !query.onlyDescribe && (write(prepared(query)), query.describeFirst = false)}-@@ -767,14 +864,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -767,14 +865,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseasync function fetchArrayTypes() {needsTypes = false@@ -316,10 +319,10 @@ index 07f67167..9224858c 100644types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))}-diff --git a/cjs/src/index.js b/cjs/src/index.js+diff --git a/cjs/src/index.js b/vendor/postgres/cjs/src/index.jsindex f09c61c7..abb4a158 100644--- a/cjs/src/index.js-+++ b/cjs/src/index.js++++ b/vendor/postgres/cjs/src/index.js@@ -62,10 +62,34 @@ function Postgres(a, b) {, full = Queue(), queues = { connecting, reserved, closed, ended, open, busy, full }@@ -684,10 +687,10 @@ index f09c61c7..abb4a158 100644...mergeUserTypes(o.types)}}-diff --git a/cjs/src/query.js b/cjs/src/query.js+diff --git a/cjs/src/query.js b/vendor/postgres/cjs/src/query.jsindex 45327f2f..1d092504 100644--- a/cjs/src/query.js-+++ b/cjs/src/query.js++++ b/vendor/postgres/cjs/src/query.js@@ -23,11 +23,12 @@ const Query = module.exports.Query = class Query extends Promise {this.state = nullthis.statement = null@@ -728,10 +731,10 @@ index 45327f2f..1d092504 100644})this.execute()return promise-diff --git a/cjs/src/queue.js b/cjs/src/queue.js+diff --git a/cjs/src/queue.js b/vendor/postgres/cjs/src/queue.jsindex 8438f5da..c444310f 100644--- a/cjs/src/queue.js-+++ b/cjs/src/queue.js++++ b/vendor/postgres/cjs/src/queue.js@@ -15,6 +15,7 @@ function Queue(initial = []) {: (xs.splice(index, 1), x)},@@ -740,10 +743,10 @@ index 8438f5da..c444310f 100644shift: () => {const out = xs[index++]-diff --git a/src/connection.js b/src/connection.js-index 1b1cccde..c1a9075a 100644+diff --git a/src/connection.js b/vendor/postgres/src/connection.js+index 1b1cccde..a178b3b6 100644--- a/src/connection.js-+++ b/src/connection.js++++ b/vendor/postgres/src/connection.js@@ -20,6 +20,7 @@ const Sync = b().S().end(), SSLRequest = b().i32(8).i32(80877103).end(8), ExecuteUnnamed = Buffer.concat([b().E().str(b.N).i32(0).end(), Sync])@@ -805,7 +808,7 @@ index 1b1cccde..c1a9075a 100644} catch (error) {reject(error)}-@@ -167,7 +176,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -167,21 +176,70 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseq.state = backendquery? sent.push(q)@@ -813,10 +816,13 @@ index 1b1cccde..c1a9075a 100644+ : (query = q, query.active = true, armInflight())build(q)++ const mayPipeline = !q.options.onexecute || q.options.onexecute(connection)return write(toBuffer(q))-@@ -176,12 +185,60 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+ && !q.describeFirst+ && !q.cursorFn&& sent.length < max_pipeline- && (!q.options.onexecute || q.options.onexecute(connection))+- && (!q.options.onexecute || q.options.onexecute(connection))++ && mayPipeline} catch (error) {- sent.length === 0 && write(Sync)- errored(error)@@ -877,7 +883,7 @@ index 1b1cccde..c1a9075a 100644function toBuffer(q) {if (q.parameters.length >= 65534)throw Errors.generic('MAX_PARAMETERS_EXCEEDED', 'Max number of parameters (65534) exceeded')-@@ -235,10 +292,15 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -235,10 +293,15 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseq.onlyDescribe && (delete statements[q.signature])q.parameters = q.parameters || parametersq.prepared = q.prepare && q.signature in statements@@ -895,7 +901,7 @@ index 1b1cccde..c1a9075a 100644typeof options.debug === 'function' && options.debug(id, string, parameters, types)}-@@ -295,10 +357,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -295,10 +358,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose/* c8 ignore next 3 */function drain() {@@ -908,7 +914,7 @@ index 1b1cccde..c1a9075a 100644if (incomings) {incomings.push(x)remaining -= x.length-@@ -379,6 +442,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -379,6 +443,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose}function error(err) {@@ -917,7 +923,7 @@ index 1b1cccde..c1a9075a 100644if (connection.queue === queues.connecting && options.host[retries + 1])return-@@ -388,6 +453,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -388,6 +454,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose}function errored(err) {@@ -926,7 +932,7 @@ index 1b1cccde..c1a9075a 100644stream && (stream.destroy(err), stream = null)query && queryError(query, err)initial && (queryError(initial, err), initial = null)-@@ -438,19 +505,38 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -438,19 +506,38 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseremaining = 0incomings = nullclearImmediate(nextWriteTimer)@@ -966,7 +972,7 @@ index 1b1cccde..c1a9075a 100644closedTime = performance.now()hadError && options.shared.retries++delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000-@@ -533,8 +619,13 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -533,8 +620,13 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose}function ReadyForQuery(x) {@@ -980,7 +986,7 @@ index 1b1cccde..c1a9075a 100644query.retried? errored(query.retried): query.prepared && retryRoutines.has(errorResponse.routine)-@@ -550,6 +641,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -550,6 +642,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclosequery = results = errorResponse = nullresult = new Result()connectTimer.cancel()@@ -988,7 +994,7 @@ index 1b1cccde..c1a9075a 100644if (initial) {if (target_session_attrs) {-@@ -564,23 +656,25 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -564,23 +657,25 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclosereturn fetchArrayTypes()}@@ -1018,7 +1024,7 @@ index 1b1cccde..c1a9075a 100644: connection.reserved(): ending? terminate()-@@ -630,6 +724,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -630,6 +725,9 @@ 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)@@ -1028,7 +1034,7 @@ index 1b1cccde..c1a9075a 100644query.describeFirst && !query.onlyDescribe && (write(prepared(query)), query.describeFirst = false)}-@@ -767,14 +864,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose+@@ -767,14 +865,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, oncloseasync function fetchArrayTypes() {needsTypes = false@@ -1058,10 +1064,10 @@ index 1b1cccde..c1a9075a 100644types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))}-diff --git a/src/index.js b/src/index.js+diff --git a/src/index.js b/vendor/postgres/src/index.jsindex c7fba3da..32efd8bf 100644--- a/src/index.js-+++ b/src/index.js++++ b/vendor/postgres/src/index.js@@ -62,10 +62,34 @@ function Postgres(a, b) {, full = Queue(), queues = { connecting, reserved, closed, ended, open, busy, full }@@ -1426,10 +1432,10 @@ index c7fba3da..32efd8bf 100644...mergeUserTypes(o.types)}}-diff --git a/src/query.js b/src/query.js+diff --git a/src/query.js b/vendor/postgres/src/query.jsindex 0d44a15c..96967780 100644--- a/src/query.js-+++ b/src/query.js++++ b/vendor/postgres/src/query.js@@ -23,11 +23,12 @@ export class Query extends Promise {this.state = nullthis.statement = null@@ -1470,10 +1476,10 @@ index 0d44a15c..96967780 100644})this.execute()return promise-diff --git a/src/queue.js b/src/queue.js+diff --git a/src/queue.js b/vendor/postgres/src/queue.jsindex c4ef9716..8fba891b 100644--- a/src/queue.js-+++ b/src/queue.js++++ b/vendor/postgres/src/queue.js@@ -15,6 +15,7 @@ function Queue(initial = []) {: (xs.splice(index, 1), x)},@@ -1482,10 +1488,10 @@ index c4ef9716..8fba891b 100644shift: () => {const out = xs[index++]-diff --git a/types/index.d.ts b/types/index.d.ts+diff --git a/types/index.d.ts b/vendor/postgres/types/index.d.tsindex 13c3432f..36233121 100644--- a/types/index.d.ts-+++ b/types/index.d.ts++++ b/vendor/postgres/types/index.d.ts@@ -119,6 +119,32 @@ interface BaseOptions<T extends Record<string, postgres.PostgresType>> {*/publications: stringdiff --git a/vendor/postgres/cf/src/connection.js b/vendor/postgres/cf/src/connection.jsindex 96f2277dad..93ac386482 100644--- a/vendor/postgres/cf/src/connection.js+++ b/vendor/postgres/cf/src/connection.js@@ -181,11 +181,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose: (query = q, query.active = true, armInflight())build(q)+ const mayPipeline = !q.options.onexecute || q.options.onexecute(connection)return write(toBuffer(q))&& !q.describeFirst&& !q.cursorFn&& sent.length < max_pipeline- && (!q.options.onexecute || q.options.onexecute(connection))+ && mayPipeline} catch (error) {// GBrain (#6383), the approach of porsager/postgres#1236: q already has its place in the queue of// queries awaiting an answer, but nothing of it was written. The stock catch rejected the connection'sdiff --git a/vendor/postgres/cjs/src/connection.js b/vendor/postgres/cjs/src/connection.jsindex 9224858c32..472ac81eb2 100644--- a/vendor/postgres/cjs/src/connection.js+++ b/vendor/postgres/cjs/src/connection.js@@ -179,11 +179,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose: (query = q, query.active = true, armInflight())build(q)+ const mayPipeline = !q.options.onexecute || q.options.onexecute(connection)return write(toBuffer(q))&& !q.describeFirst&& !q.cursorFn&& sent.length < max_pipeline- && (!q.options.onexecute || q.options.onexecute(connection))+ && mayPipeline} catch (error) {// GBrain (#6383), the approach of porsager/postgres#1236: q already has its place in the queue of// queries awaiting an answer, but nothing of it was written. The stock catch rejected the connection'sdiff --git a/vendor/postgres/src/connection.js b/vendor/postgres/src/connection.jsindex c1a9075a4f..a178b3b686 100644--- a/vendor/postgres/src/connection.js+++ b/vendor/postgres/src/connection.js@@ -179,11 +179,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose: (query = q, query.active = true, armInflight())build(q)+ const mayPipeline = !q.options.onexecute || q.options.onexecute(connection)return write(toBuffer(q))&& !q.describeFirst&& !q.cursorFn&& sent.length < max_pipeline- && (!q.options.onexecute || q.options.onexecute(connection))+ && mayPipeline} catch (error) {// GBrain (#6383), the approach of porsager/postgres#1236: q already has its place in the queue of// queries awaiting an answer, but nothing of it was written. The stock catch rejected the connection's