Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Stale plans retry with deadlines off and on non-English servers (GST-2314)
garrytanc6dbea637b24
diff --git a/src/core/persistence/coordinator.ts b/src/core/persistence/coordinator.tsindex 10e3c3b906..680f7410ac 100644--- a/src/core/persistence/coordinator.ts+++ b/src/core/persistence/coordinator.ts@@ -19,6 +19,7 @@ import { PREPARATION_BUDGET_KEYS, preparationKind } from './preparation-budget.timport { chargeTransientInterruption, chargedInterruptionDetail, transientInterruption, type TransientInterruption } from './transient-retry.ts';import { readWriteSwitchSnapshot } from './switches.ts';import { isSharedReadFailure } from './bounded-reads.ts';+import { isStalePlanError } from '../retry-matcher.ts';import { isBundleRecovery, isTerminal, PAGES_RECOVERY_MAX_FILES, principalKey, requestPrincipal, recoveryFiles, type FileRecoveryRecord, type RecoveryRecord, type WriteRequest } from './model.ts';import { isLockOpenFailure, noteLockOpenFailure, type NativeLockHandle } from './native-lock.ts';import { withCoordinatedWrite } from './context.ts';@@ -255,7 +256,7 @@ function conflictCode(code: string): boolean { return ['revision_required','revifunction failureState(code: string): 'conflict' | 'failed' | 'cancelled' { return code === 'admission_paused' ? 'cancelled' : conflictCode(code) ? 'conflict' : 'failed'; }export function transientDatabaseFailure(error: unknown): boolean {return ['40001','40P01','55P03','57014','53300','57P01','57P02','57P03','08000','08003','08006','08001','08004',- 'ECONNRESET','ECONNREFUSED','ETIMEDOUT','CONNECTION_CLOSED','CONNECTION_ENDED'].includes(String((error as {code?:string})?.code)) || connectionCapFailure(error);+ 'ECONNRESET','ECONNREFUSED','ETIMEDOUT','CONNECTION_CLOSED','CONNECTION_ENDED'].includes(String((error as {code?:string})?.code)) || connectionCapFailure(error) || isStalePlanError(error);}/*** GBRA-133: a pooler's client cap (Supavisor `EMAXCONNSESSION`, "too many clients already") is the same contention asdiff --git a/src/core/retry-matcher.ts b/src/core/retry-matcher.tsindex fe5f4c0238..a11e63b3c5 100644--- a/src/core/retry-matcher.ts+++ b/src/core/retry-matcher.ts@@ -106,10 +106,12 @@ export function getMessage(err: unknown): string {* GST-2314: a prepared statement whose cached plan went stale because a migration changed a table it reads (SQLSTATE* 0A000 "cached plan must not change result type"). Outside a transaction the driver re-prepares it; inside one the* transaction is already aborted, so the caller sees the error and a re-run on a fresh transaction succeeds. Every other- * 0A000 (feature_not_supported) stays a refusal.+ * 0A000 (feature_not_supported) stays a refusal. The C routine (RevalidateCachedQuery) is matched first because the+ * server translates the message under a non-English lc_messages.*/export function isStalePlanError(err: unknown): boolean {- return getCode(err) === '0A000' && /cached plan must not change result type/i.test(getMessage(err));+ if (getCode(err) !== '0A000') return false;+ return (err as { routine?: unknown }).routine === 'RevalidateCachedQuery' || /cached plan must not change result type/i.test(getMessage(err));}/**diff --git a/test/stale-plan-classification-gst2314.test.ts b/test/stale-plan-classification-gst2314.test.tsnew file mode 100644index 0000000000..c34de6b36d--- /dev/null+++ b/test/stale-plan-classification-gst2314.test.ts@@ -0,0 +1,27 @@+/**+ * GST-2314 review round: the stale-plan error (SQLSTATE 0A000, "cached plan must not change result type") is+ * recognised by the untranslated C routine as well as the English message, so a server with non-English+ * lc_messages still retries it, and an uncharged stale plan (persistence.preparation_deadlines false) is released+ * as database contention instead of failing the accepted write.+ */+import { expect, test } from 'bun:test';+import { transientDatabaseFailure } from '../src/core/persistence/coordinator.ts';+import { isStalePlanError } from '../src/core/retry-matcher.ts';++const pgError = (fields: { code: string; message: string; routine?: string }) =>+ Object.assign(new Error(fields.message), { code: fields.code, ...(fields.routine ? { routine: fields.routine } : {}) });++test('a stale plan is recognised by its routine when the server translates the message', () => {+ expect(isStalePlanError(pgError({ code: '0A000', message: 'der gecachte Plan darf den Ergebnistyp nicht ändern', routine: 'RevalidateCachedQuery' }))).toBe(true);+ expect(isStalePlanError(pgError({ code: '0A000', message: 'cached plan must not change result type' }))).toBe(true);+});++test('every other 0A000 stays a refusal', () => {+ expect(isStalePlanError(pgError({ code: '0A000', message: 'cannot use RETURNING with a view', routine: 'transformReturningList' }))).toBe(false);+ expect(isStalePlanError(pgError({ code: 'XX000', message: 'internal', routine: 'RevalidateCachedQuery' }))).toBe(false);+});++test('an uncharged stale plan is transient database contention', () => {+ expect(transientDatabaseFailure(pgError({ code: '0A000', message: 'cached plan must not change result type', routine: 'RevalidateCachedQuery' }))).toBe(true);+ expect(transientDatabaseFailure(pgError({ code: '0A000', message: 'cannot use RETURNING with a view', routine: 'transformReturningList' }))).toBe(false);+});