Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(item-failure): a missing page source (PageSourceMissingError, #6808) is the item's content failure unknown_source (GBRA-130 A-1)
garrytanba49e8c2fdf7
diff --git a/src/core/item-failure.ts b/src/core/item-failure.tsindex d6c6afddac..89e9972cea 100644--- a/src/core/item-failure.ts+++ b/src/core/item-failure.ts@@ -27,6 +27,7 @@ import { acceptedPendingReceipt } from './persistence/accepted-pending.ts';/** Every per-item loop that isolates its items, with the inventory row it fixes (reports/GBRA-130-inventory.md). */export const ISOLATE_SITES = {+ 'links.extract_stale': 'A-1',} as const satisfies Record<string, string>;export type IsolateSite = keyof typeof ISOLATE_SITES;@@ -74,11 +75,14 @@ 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' };+/** 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' };function classifyOne(error: unknown): ClassifiedItemFailure | null {const message = error instanceof Error ? error.message : String(error);const name = (error as { name?: unknown } | null)?.name;if (typeof name === 'string' && RUN_ERROR_NAMES[name]) return { code: RUN_ERROR_NAMES[name]!, scope: 'run', class: 'transient', message, requestId: 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 };if (error instanceof OperationError) {diff --git a/src/core/page-state/guards.ts b/src/core/page-state/guards.tsindex 281b99abe2..feb72f3456 100644--- a/src/core/page-state/guards.ts+++ b/src/core/page-state/guards.ts@@ -3,6 +3,14 @@ import { validateSlug } from '../utils.ts';import type { PageKey } from './types.ts';import { pipelined } from './transactions.ts';+/** A missing source refuses every write; bulk derived work may defer that origin. */+export class PageSourceMissingError extends Error {+ constructor(readonly sourceId: string) {+ super(`Page source does not exist: ${sourceId}`);+ this.name = 'PageSourceMissingError';+ }+}+/*** Source locks precede auth/request locks in callers; repeat held locks safely.* #5984: the guard statements are pipelined in the sorted key order. Keys that@@ -22,7 +30,7 @@ export async function lockPageKeys(engine: Pick<BrainEngine, 'executeRaw'> & { kfor (const { sourceId, incarnation } of ordered) if (incarnation && (expected.get(sourceId) ?? incarnation) === incarnation) expected.set(sourceId, incarnation);const hinted = ordered.every(key => key.incarnation && expected.get(key.sourceId) === key.incarnation);const readSource = (sourceId: string) => engine.executeRaw<{ incarnation: string }>('SELECT incarnation FROM sources WHERE id=$1 FOR SHARE', [sourceId]).then(rows => {- if (!rows.length) throw new Error(`Page source does not exist: ${sourceId}`);+ if (!rows.length) throw new PageSourceMissingError(sourceId);if (hinted && rows[0]!.incarnation !== expected.get(sourceId)) throw new Error(`Page source was recreated: ${sourceId}`);return rows[0]!.incarnation;});@@ -33,7 +41,7 @@ export async function lockPageKeys(engine: Pick<BrainEngine, 'executeRaw'> & { kawait pipelined({ kind: engine.kind ?? '' }, [async () => {const rows = await engine.executeRaw<{ incarnation: string }>('SELECT incarnation FROM sources WHERE id=$1 FOR SHARE', [key.sourceId]);- if (!rows.length) throw new Error(`Page source does not exist: ${key.sourceId}`);+ if (!rows.length) throw new PageSourceMissingError(key.sourceId);},() => engine.executeRaw('INSERT INTO page_write_guards(source_incarnation,slug) SELECT incarnation,$2 FROM sources WHERE id=$1 ON CONFLICT DO NOTHING', [key.sourceId, key.slug]),() => engine.executeRaw('SELECT g.slug FROM page_write_guards g JOIN sources s ON s.incarnation=g.source_incarnation WHERE s.id=$1 AND g.slug=$2 FOR UPDATE OF g', [key.sourceId, key.slug]),diff --git a/test/pathological/item-failure.test.ts b/test/pathological/item-failure.test.tsindex d72ed92e47..aec007ab3b 100644--- a/test/pathological/item-failure.test.ts+++ b/test/pathological/item-failure.test.ts@@ -6,6 +6,7 @@ import { describe, expect, test } from 'bun:test';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 { runScopedErrors } from './harness.ts';const op = (code: string, extra: Partial<OperationError> = {}) => Object.assign(new OperationError(code as never, `refused: ${code}`), extra);@@ -34,6 +35,11 @@ describe('classifyItemFailure', () => {expect(classifyItemFailure(new TypeError('x is undefined'))).toMatchObject({ scope: 'item', class: 'transient', code: 'internal' });});+ test('a missing page source (#6808, PageSourceMissingError) is a content failure of the item; an untyped error with its text is not', () => {+ expect(classifyItemFailure(new PageSourceMissingError('ghost-example'))).toMatchObject({ scope: 'item', class: 'content', code: 'unknown_source' });+ expect(classifyItemFailure(new Error('Page source does not exist: ghost-example'))).toMatchObject({ scope: 'item', class: 'transient', code: 'internal' });+ });+test('the cause chain is walked', () => {const wrapped = new Error('wrapper', { cause: sql('25P02') });expect(classifyItemFailure(wrapped)).toMatchObject({ scope: 'run', code: 'database_contention', message: 'wrapper' });