Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

item-failure: content-class provider refusals (request_too_large, content_rejected) and transient provider_error; ISOLATE sites for A-18/A-24/A-25

garrytan84011532ff0c
diff --git a/src/core/ai/errors.ts b/src/core/ai/errors.ts
index 85d3fb28cd..7ee8792eed 100644
--- a/src/core/ai/errors.ts
+++ b/src/core/ai/errors.ts
@@ -219,6 +219,26 @@ export function providerContentBlockReason(err: unknown): string | undefined {
return undefined;
}
+const CONTEXT_OVERFLOW_RE = /context[_ ]length|maximum context|prompt is too long|too many (?:input )?tokens|input (?:is )?too long/i;
+
+/**
+ * GBRA-130: a provider refusal of one request's own input, which fails the same
+ * way until that input changes: a content-policy block (`content_rejected`), or
+ * a 413 / a context-length 400 (`request_too_large`). A 400 without a
+ * context-length phrase can be a malformed request every call shares, so it
+ * stays unclassified.
+ */
+export function providerItemRefusal(err: unknown): 'content_rejected' | 'request_too_large' | null {
+ if (providerContentBlockReason(err)) return 'content_rejected';
+ const { status } = readProviderFailureSignals(err);
+ if (status === 413) return 'request_too_large';
+ if (status !== 400) return null;
+ for (const layer of wrappedErrorLayers(err)) {
+ if (typeof layer.message === 'string' && CONTEXT_OVERFLOW_RE.test(layer.message)) return 'request_too_large';
+ }
+ return null;
+}
+
/** Whole-run LLM failure classes — see classifyGlobalLlmError. */
export type GlobalLlmErrorClass = 'auth' | 'billing' | 'rate_limit' | 'model_not_found';
@@ -244,11 +264,11 @@ const PER_ITEM_HTTP_STATUSES = new Set([400, 408, 413, 422]);
// EMBEDDING_QUOTA regex (which matches bare /billing/): a global-error hit
// halts an entire cycle phase, so a stray word inside a raw-output slice
// must not trip it. Only specific provider phrasings qualify.
-const BILLING_MESSAGE_RE =
+export const BILLING_MESSAGE_RE =
/insufficient_quota|quota exceeded|exceeded your (?:current )?quota|credit balance is too low|spend limit|billing_not_active|billing hard limit|payment required/i;
const AUTH_MESSAGE_RE =
/authentication_error|permission_error|invalid (?:x-)?api[-_ ]?key|api key (?:is )?(?:invalid|expired|missing)|unauthorized/i;
-const RATE_MESSAGE_RE = /rate[-_ ]?limit(?:ed|_error)?\b|too many requests/i;
+export const RATE_MESSAGE_RE = /rate[-_ ]?limit(?:ed|_error)?\b|too many requests/i;
// Provider phrasings for an unknown or inaccessible model: OpenAI
// `model_not_found` / "does not exist or you do not have access" (404),
// Anthropic `not_found_error`, and the project-level access denial OpenAI
diff --git a/src/core/item-failure.ts b/src/core/item-failure.ts
index d5cff083c8..c1978f944f 100644
--- a/src/core/item-failure.ts
+++ b/src/core/item-failure.ts
@@ -24,6 +24,7 @@
import { OperationError, opError } from './ops/contract.ts';
import { CODES } from './error-registry.ts';
import { acceptedPendingReceipt } from './persistence/accepted-pending.ts';
+import { providerItemRefusal } from './ai/errors.ts';
/** Every per-item loop that isolates its items, with the inventory row it fixes (reports/GBRA-130-inventory.md). */
export const ISOLATE_SITES = {
@@ -43,6 +44,9 @@ export const ISOLATE_SITES = {
'repair.all_kinds': 'A-45',
'repair.content_lane_kinds': 'A-46',
'migration.credential_reseal': 'A-27',
+ 'propose_takes.pages': 'A-25',
+ 'sync.import_files': 'A-18',
+ 'extract_facts.phantom_redirect': 'A-24',
} as const satisfies Record<string, string>;
export type IsolateSite = keyof typeof ISOLATE_SITES;
@@ -90,6 +94,8 @@ function sqlstateFailure(state: string): { code: string; scope: 'run' | 'item';
const RUN_ERRNOS = new Set(['ENOSPC', 'EMFILE', 'ENFILE', 'EROFS', 'EDQUOT']);
const ITEM_ERRNOS = new Set(['EACCES', 'EPERM', 'ENOENT', 'ELOOP', 'EISDIR', 'ENOTDIR', 'ENAMETOOLONG', 'EILSEQ']);
const RUN_ERROR_NAMES: Record<string, string> = { AbortError: 'aborted', LockStolenError: 'lock_stolen', BudgetExhausted: 'budget_exhausted' };
+/** A model or embedding provider's own failure (the gateway's error classes, an HTTP fetch that failed): never the database connection; a provider outage trips the breaker. */
+const AI_ERROR_NAMES = new Set(['AIServiceError', 'AITransientError', 'AIConfigError']);
/** Typed refusals of one item raised as plain errors: content, because the item fails the same way until it or its source changes. */
const ITEM_ERROR_NAMES: Record<string, string> = { PageSourceMissingError: 'unknown_source' };
@@ -100,6 +106,11 @@ function classifyOne(error: unknown): ClassifiedItemFailure | null {
if (typeof name === 'string' && ITEM_ERROR_NAMES[name]) return { code: ITEM_ERROR_NAMES[name]!, scope: 'item', class: 'content', message, requestId: null };
const pending = acceptedPendingReceipt(error);
if (pending) return { code: 'write_pending', scope: 'pending', class: 'transient', message, requestId: pending.request_id };
+ const refusal = providerItemRefusal(error);
+ if (refusal) return { code: refusal, scope: 'item', class: 'content', message, requestId: null };
+ if ((typeof name === 'string' && AI_ERROR_NAMES.has(name)) || /\bfetch failed\b/i.test(message)) {
+ return { code: 'provider_error', scope: 'item', class: 'transient', message, requestId: null };
+ }
if (error instanceof OperationError) {
const code = String(error.code === 'storage_error' && error.writeError ? error.writeError : error.canonicalCode);
const requestId = error.writeRequest?.request_id ?? null;
diff --git a/test/pathological/catalogue.ts b/test/pathological/catalogue.ts
index 57423125e6..45125b2d18 100644
--- a/test/pathological/catalogue.ts
+++ b/test/pathological/catalogue.ts
@@ -63,4 +63,10 @@ export const CATALOGUE: Record<IsolateSite, SiteCases> = {
'repair.all_kinds': { file: 'test/pathological/a45-repair-all-kinds.test.ts', item: 'item case: a kind whose plan throws', run: 'run case: a run-scoped' },
'repair.content_lane_kinds': { file: 'test/pathological/a46-content-lane-kinds.test.ts', item: 'item case: fences throwing is recorded', run: 'run case: a run-scoped' },
'migration.credential_reseal': { file: 'test/pathological/a27-credential-reseal.test.ts', item: 'item case: the unbuildable page is held', run: 'run case: a run-scoped' },
+ 'sync.import_files': { file: 'test/pathological/a18-ledger-path-text.test.ts',
+ item: 'A-18 item: billing/q3.md (mode 000) is PERMISSION_DENIED', run: 'A-18 run: a run-scoped' },
+ 'propose_takes.pages': { file: 'test/pathological/a25-propose-takes-poison-pages.test.ts',
+ item: 'A-25 item: the 5 newest pages fail every run', run: 'A-25 run: a run-scoped' },
+ 'extract_facts.phantom_redirect': { file: 'test/pathological/a24-phantom-redirect-cursor.test.ts',
+ item: 'A-24 item: a phantom whose redirect throws is reported with its code and fix', run: 'A-24 run: a run-scoped' },
};
diff --git a/test/pathological/item-failure.test.ts b/test/pathological/item-failure.test.ts
index 642603552f..3aedf6e716 100644
--- a/test/pathological/item-failure.test.ts
+++ b/test/pathological/item-failure.test.ts
@@ -7,6 +7,7 @@ import { CODES } from '../../src/core/error-registry.ts';
import { classifyItemFailure, CONTENT_ITEM_CODES, ItemRun, RUN_SCOPED_CODES } from '../../src/core/item-failure.ts';
import { OperationError } from '../../src/core/ops/contract.ts';
import { PageSourceMissingError } from '../../src/core/page-state/guards.ts';
+import { AIConfigError, AITransientError } from '../../src/core/ai/errors.ts';
import { runScopedErrors } from './harness.ts';
const op = (code: string, extra: Partial<OperationError> = {}) => Object.assign(new OperationError(code as never, `refused: ${code}`), extra);
@@ -100,6 +101,25 @@ describe('ItemRun breaker', () => {
});
});
+test('a provider refusal of one request\'s own input is the item\'s content failure; other provider 400s stay transient (A-25)', () => {
+ const status = (error: Error, code: number) => Object.assign(error, { status: code });
+ expect(classifyItemFailure(status(new AIConfigError('[chat(anthropic:claude-example)] prompt is too long: 250000 tokens > 200000 maximum'), 400)))
+ .toMatchObject({ code: 'request_too_large', scope: 'item', class: 'content' });
+ expect(classifyItemFailure(status(new AIConfigError('[chat(openai:gpt-example)] Request Entity Too Large'), 413))).toMatchObject({ code: 'request_too_large', class: 'content' });
+ const blocked = Object.assign(new Error('Bad Request'), { statusCode: 400, responseBody: JSON.stringify({ error: { code: 'content_policy_violation' } }) });
+ expect(classifyItemFailure(new AIConfigError('[chat(openai:gpt-example)] Bad Request', undefined, blocked))).toMatchObject({ code: 'content_rejected', class: 'content' });
+ expect(classifyItemFailure(status(new AIConfigError('[chat(openai:gpt-example)] Unknown parameter: temperature'), 400))).toMatchObject({ scope: 'item', class: 'transient' });
+ expect(classifyItemFailure(new AITransientError('[chat(openai:gpt-example)] context length exceeded upstream'))).toMatchObject({ class: 'transient' });
+});
+
+test('a model provider\'s transport failure is the item\'s transient failure, never a lost database connection (A-25)', () => {
+ expect(classifyItemFailure(new Error('fetch failed: ECONNRESET'))).toMatchObject({ code: 'provider_error', scope: 'item', class: 'transient' });
+ expect(classifyItemFailure(new AITransientError('[chat(openai:gpt-example)] socket hang up', Object.assign(new Error('read ECONNRESET'), { code: 'ECONNRESET' }))))
+ .toMatchObject({ code: 'provider_error', scope: 'item' });
+ expect(classifyItemFailure(Object.assign(new Error('Connection terminated unexpectedly'), { code: '08006' })).scope).toBe('run');
+ expect(classifyItemFailure(new Error('read ECONNRESET')).scope).toBe('run');
+});
+
test('RUN_SCOPED_CODES holds item_failures_systemic so a tripped inner loop stops its outer loop', () => {
expect(RUN_SCOPED_CODES.has('item_failures_systemic')).toBe(true);
});