Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(consumer): only a discarded connection past the deadline is reclassified; a server-honoured cancel keeps its 57014, and onError still sees one failure per tick

garrytan44ed294912de
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d9157e290f..2ef285066e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -40,7 +40,7 @@ GBRAIN_CANCEL_SETTLE_MS=5000 gbrain serve # a longer settle
### Itemized changes
- `src/core/postgres-engine.ts`: `runUnsafe` arms a settle timer after `cancel()`; a statement still pending at `cancelSettleMs()` has its reserved connection discarded (`CONNECTION_DESTROYED`), once. `GBRAIN_CANCEL_SETTLE_MS` (>= 100) overrides the 2 s default.
-- `src/core/persistence/consumer.ts`: a phase past its deadline that ends with 57014, `CONNECTION_DESTROYED` or `CONNECTION_CLOSED` throws `PhaseDeadlineError` (`code: deadline_exceeded`); `report()` records it under the phase without a second log line.
+- `src/core/persistence/consumer.ts`: a phase past its deadline that ends with `CONNECTION_DESTROYED` or `CONNECTION_CLOSED` (the discarded connection) throws `PhaseDeadlineError` (`code: deadline_exceeded`); `report()` records it under the phase without a second log line.
- `src/core/persistence/bounded-reads.ts` and `coordinator.ts`: a discarded connection after the budget is the preparation deadline, never `storage_error` or `database_contention`.
- Doctor `persistence_session_timeouts` gains the `transaction_mode_pooler` warning (`resolvePrepare(url) === false`).
- Tests: `test/postgres-engine-cancel-settle.test.ts` (never-settling statement is discarded at the window; a honoured cancel is not), `test/persistence-consumer-log.test.ts` (the consumer logs one `deadline_exceeded`, no `storage_error`, and ticks again), `test/e2e/persistence-pooler-wedge-postgres.test.ts` (a wedged `expired_claims` statement inside a real 60-page managed catch-up: cancel swallowed, discarded at the window, the run drains `synced`; hangs on the previous release).
diff --git a/src/core/persistence/consumer.ts b/src/core/persistence/consumer.ts
index b0bc194390..6968751fdd 100644
--- a/src/core/persistence/consumer.ts
+++ b/src/core/persistence/consumer.ts
@@ -98,9 +98,10 @@ export class PhaseDeadlineError extends Error {
this.name = 'PhaseDeadlineError';
}
}
-const PHASE_DEADLINE_CODES = new Set(['57014', 'CONNECTION_DESTROYED', 'CONNECTION_CLOSED']);
+/** A server-honoured cancel (57014) keeps its SQLSTATE as before; only a connection the engine had to discard is reclassified. */
+const PHASE_DEADLINE_CODES = new Set(['CONNECTION_DESTROYED', 'CONNECTION_CLOSED']);
function isPhaseDeadlineOutcome(error: { name?: unknown; code?: unknown } | null): boolean {
- return !!error && (error.name === 'AbortError' || typeof error.code === 'string' && PHASE_DEADLINE_CODES.has(error.code));
+ return !!error && typeof error.code === 'string' && PHASE_DEADLINE_CODES.has(error.code);
}
export class PersistenceConsumer {
@@ -638,8 +639,8 @@ export class PersistenceConsumer {
throw this.abort.signal.reason;
}
this.lastPhaseError = name; this.lastPhaseTiming = this.timingText(observation, startedAt);
- // #6278: past the phase deadline, the statement's end (the server's 57014, or the client-side discard of a
- // round-trip a pooler never completed) is the deadline itself: the tick moves on instead of reporting storage_error.
+ // #6278: past the phase deadline, the client-side discard of a round-trip a pooler never completed is the
+ // deadline itself: the tick moves on instead of reporting storage_error.
if (observation.deadline_exceeded && isPhaseDeadlineOutcome(cancelled)) throw new PhaseDeadlineError(name, error);
throw error;
}
@@ -648,9 +649,11 @@ export class PersistenceConsumer {
private report(error: unknown): void {
if (this.stopping && error === this.abort.signal.reason) return;
if (error instanceof PhaseDeadlineError) {
- // The timer already logged deadline_exceeded for this phase; record it and let the next tick run.
+ // The timer already logged deadline_exceeded for this phase: record it under the phase, hand it to onError
+ // (fail-closed scheduling still sees one failure per tick), write no second line, and let the next tick run.
this.lastError = { code: 'deadline_exceeded', at: new Date().toISOString(), phase: error.phase };
this.lastPhaseError = undefined; this.lastPhaseTiming = undefined;
+ this.opts.onError?.(error);
return;
}
const code = (error as { code?: unknown })?.code;