Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes
garrytan71d4aa31b0f8
diff --git a/src/core/postgres-engine.ts b/src/core/postgres-engine.tsindex 0b485334e4..5b5ae576b1 100644--- a/src/core/postgres-engine.ts+++ b/src/core/postgres-engine.ts@@ -3002,7 +3002,7 @@ export class PostgresEngine implements BrainEngine {if (opts.jitOff && await needsJitOff(this.sql, this._pageTransaction)) return await runJitOffStatement<T>(this.sql, this._pageTransaction, sql, params, opts, (c, s, p, o) => this.runUnsafe(c, s, p, o), () => this.checkoutGauge.checkedOut());// #6278: a transaction-local statement_timeout, which holds through a transaction-mode pooler (postgres-engine/bounded-statement.ts).return opts.timeoutMs !== undefined && !this._pageTransaction ? await runBoundedStatement<T>(this.sql, sql, params, { ...opts, timeoutMs: opts.timeoutMs }, () => this.checkoutGauge.checkedOut()) : await retryReadOnlyAutocommit(this.sql, c => this.runUnsafe<T>(c, sql, params, opts));- });+ }, opts.signal);return await retryReadOnlyAutocommit(this.sql, c => this.runUnsafe<T>(c, sql, params, opts));} finally {this.checkoutGauge.release('raw');diff --git a/src/core/search/connection-budget.ts b/src/core/search/connection-budget.tsindex 741fd75efa..fccb6da6bf 100644--- a/src/core/search/connection-budget.ts+++ b/src/core/search/connection-budget.ts@@ -38,12 +38,25 @@ export function withUrgentSearchConnections<T>(fn: () => Promise<T>): Promise<T>return urgency.run(true, fn);}-/** Hold one slot of the current budget while `fn` runs; without a budget, run it directly. */-export async function holdSearchConnection<T>(fn: () => Promise<T>): Promise<T> {+/**+ * Hold one slot of the current budget while `fn` runs; without a budget, run it directly. An abort of `signal` while+ * the read waits for a slot leaves the queue and rejects AbortError, as an abort while waiting for a pooled+ * connection does.+ */+export async function holdSearchConnection<T>(fn: () => Promise<T>, signal?: AbortSignal): Promise<T> {const budget = budgets.getStore();if (!budget) return fn();if (budget.free > 0) budget.free -= 1;- else await new Promise<void>(resolve => (urgency.getStore() ? budget.urgent : budget.waiters).push(resolve));+ else {+ if (signal?.aborted) throw new DOMException('aborted', 'AbortError');+ const queue = urgency.getStore() ? budget.urgent : budget.waiters;+ await new Promise<void>((resolve, reject) => {+ const onAbort = () => { queue.splice(queue.indexOf(granted), 1); reject(new DOMException('aborted', 'AbortError')); };+ const granted = () => { signal?.removeEventListener('abort', onAbort); resolve(); };+ queue.push(granted);+ signal?.addEventListener('abort', onAbort, { once: true });+ });+ }try {throwIfRequestAborted();return await budgets.run(null, fn);diff --git a/test/search-connection-budget-urgent.test.ts b/test/search-connection-budget-urgent.test.tsindex 04bcdbc9a6..955840023e 100644--- a/test/search-connection-budget-urgent.test.ts+++ b/test/search-connection-budget-urgent.test.ts@@ -35,6 +35,26 @@ describe('search connection budget: urgent reads', () => {expect(peak).toBe(SEARCH_CONNECTION_LIMIT);});+ test('a read aborted while it waits for a slot rejects AbortError at once and never takes the slot', async () => {+ const gates: Array<() => void> = [];+ let ran = false;+ await withSearchConnectionBudget(async () => {+ const holders = Array.from({ length: SEARCH_CONNECTION_LIMIT }, () => holdSearchConnection(() => new Promise<void>(resolve => gates.push(resolve))));+ await Bun.sleep(0);+ const controller = new AbortController();+ const waiting = holdSearchConnection(async () => { ran = true; }, controller.signal);+ const next = holdSearchConnection(async () => 'next');+ controller.abort();+ await expect(waiting).rejects.toMatchObject({ name: 'AbortError' });+ await expect(holdSearchConnection(async () => 'late', controller.signal)).rejects.toMatchObject({ name: 'AbortError' });+ gates.shift()!();+ expect(await next).toBe('next');+ gates.splice(0).forEach(open => open());+ await Promise.all(holders);+ });+ expect(ran).toBe(false);+ });+test('without waiters an urgent read takes a free slot like any other, and outside a budget nothing waits', async () => {expect(await withSearchConnectionBudget(() => withUrgentSearchConnections(() => holdSearchConnection(async () => 'ran')))).toBe('ran');expect(await withUrgentSearchConnections(() => holdSearchConnection(async () => 'unbudgeted'))).toBe('unbudgeted');