Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(pathological): A-36 red probe: one refused entity fails the whole managed facts batch on every replay
garrytan2ea983f832d6
diff --git a/test/pathological/_gbra60-shim.ts b/test/pathological/_gbra60-shim.tsnew file mode 100644index 0000000000..c24f35d8c8--- /dev/null+++ b/test/pathological/_gbra60-shim.ts@@ -0,0 +1,60 @@+/**+ * Lane GBRA-60 stand-in for the GBRA-130 pathological harness (test/pathological/harness.ts on+ * capy/gbra130-item-isolation): the subset these row files use, under the same names, so the integration+ * switches imports without touching a case. Remove it once that harness is on master. Row files call testBackends()+ * themselves instead of pathologicalArms(): check:postgres-lanes only registers a Postgres arm from that call.+ */+import { expect } from 'bun:test';+import type { BrainEngine } from '../../src/core/engine.ts';+import { OperationError } from '../../src/core/ops/contract.ts';++export interface ItemSnapshotRow { slug: string; content_hash: string | null; updated_at: string; deleted_at: string | null; links: number; chunks: number }++/** Every page of a source with what a wedge could lose or change. */+export async function snapshotItems(engine: BrainEngine, sourceId: string): Promise<Map<string, ItemSnapshotRow>> {+ const rows = await engine.executeRaw<ItemSnapshotRow>(+ `SELECT p.slug, p.content_hash, p.updated_at::text AS updated_at, p.deleted_at::text AS deleted_at,+ (SELECT count(*)::int FROM links l WHERE l.from_page_id = p.id) AS links,+ (SELECT count(*)::int FROM content_chunks c WHERE c.page_id = p.id) AS chunks+ FROM pages p WHERE p.source_id = $1 ORDER BY p.slug`, [sourceId]);+ return new Map(rows.map(row => [row.slug, row]));+}++/** Pages outside `changed` are unchanged; each `expectProgress` page moved in at least one of `progressFields`. */+export function expectOthersUnchanged(before: Map<string, ItemSnapshotRow>, after: Map<string, ItemSnapshotRow>,+ opts: { changed?: Iterable<string>; expectProgress?: Iterable<string>; progressFields?: Array<keyof ItemSnapshotRow> } = {}): void {+ const skip = new Set([...(opts.changed ?? []), ...(opts.expectProgress ?? [])]);+ for (const [slug, row] of before) if (!skip.has(slug)) expect({ slug, row: after.get(slug) }).toEqual({ slug, row });+ const fields = opts.progressFields ?? ['content_hash', 'updated_at', 'links', 'chunks'];+ for (const slug of opts.expectProgress ?? []) {+ expect(after.get(slug)).toBeDefined();+ expect({ slug, moved: fields.some(field => before.get(slug)?.[field] !== after.get(slug)?.[field]) }).toEqual({ slug, moved: true });+ }+}++/** The unit returned instead of throwing. */+export async function expectUnitNotAborted<T>(run: () => Promise<T>): Promise<T> {+ let thrown: unknown;+ let result: T | undefined;+ try { result = await run(); } catch (error) { thrown = error; }+ expect({ aborted: thrown === undefined ? null : thrown instanceof Error ? `${thrown.name}: ${thrown.message}` : String(thrown) }).toEqual({ aborted: null });+ return result as T;+}++/** A recorded item failure names its item and a typed code; `fix`, when given, matches. */+export function expectItemFailure(failure: { key?: string; slug?: string; item?: string; code?: string; reason?: string; fix?: unknown } | undefined,+ want: { key: string; code: string; fix?: RegExp }): void {+ expect(failure).toBeDefined();+ expect({ key: failure!.key ?? failure!.item ?? failure!.slug, code: failure!.code ?? failure!.reason }).toEqual({ key: want.key, code: want.code });+ if (want.fix) expect(typeof failure!.fix === 'string' ? failure!.fix : JSON.stringify(failure!.fix)).toMatch(want.fix);+}++/** Run-scoped errors a negative case injects at a site: each must stop the unit and hold nothing. */+export function runScopedErrors(): Array<{ name: string; error: () => unknown }> {+ return [+ { name: 'connection lost', error: () => Object.assign(new Error('Connection terminated unexpectedly'), { code: '08006' }) },+ { name: 'abort', error: () => Object.assign(new Error('The operation was aborted'), { name: 'AbortError' }) },+ { name: 'failed transaction (25P02)', error: () => Object.assign(new Error('current transaction is aborted, commands ignored until end of transaction block'), { code: '25P02' }) },+ { name: 'owner_unavailable', error: () => new OperationError('owner_unavailable', 'The canonical writer for this source is unavailable.') },+ ];+}diff --git a/test/pathological/a-36-facts-batch-partial-refusal.test.ts b/test/pathological/a-36-facts-batch-partial-refusal.test.tsnew file mode 100644index 0000000000..f064d6a0ff--- /dev/null+++ b/test/pathological/a-36-facts-batch-partial-refusal.test.ts@@ -0,0 +1,177 @@+/**+ * GBRA-130 A-36: a managed facts batch whose one entity request ended terminal on a code other than+ * `source_changed` or a withdrawal refusal (here `revision_conflict`: the entity page changed between+ * admission and publication) used to throw on the first uncommitted row (`collectManagedFacts` called+ * `writeResponse` per row). The first run threw, every replay under the same request id threw again, and a+ * facts-absorb job dead-lettered after five attempts, although the other entity's facts committed and stayed.+ *+ * Automatic extraction now collects every row: committed entities are counted, an entity refused for its own+ * page is reported under `refused` (slug, code, request id), and the batch's completion receipt (refused as+ * incomplete) is not an error of its own. A run-scoped failure, or a write still pending, throws after the+ * collection. Explicit fact writers (remember, the fence writers) keep the refusal.+ *+ * Seam: the persistence fault hook runs another coordinated write on entity B as B's fact request is prepared,+ * which is what any write landing on B between admission and publication does. PGLite; Postgres when+ * DATABASE_URL is set.+ */+import { afterAll, afterEach, beforeAll, describe, expect, test } from 'bun:test';+import { randomUUID } from 'node:crypto';+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import type { BrainEngine } from '../../src/core/engine.ts';+import { PGLiteEngine } from '../../src/core/pglite-engine.ts';+import { configureGateway, resetGateway, __setChatTransportForTests, type ChatResult } from '../../src/core/ai/gateway.ts';+import { operationsByName } from '../../src/core/operations.ts';+import type { OperationContext } from '../../src/core/ops/contract.ts';+import { disposePersistenceConsumer } from '../../src/core/persistence/service.ts';+import { installFaultHook } from '../../src/core/persistence/fault-points.ts';+import { withCoordinatedWrite } from '../../src/core/persistence/context.ts';+import { publishManagedEntityFacts } from '../../src/core/facts/managed-fact-write.ts';+import { isolatedPersistencePostgres } from '../helpers/persistence-postgres.ts';+import { withEnv } from '../helpers/with-env.ts';+import { requirePostgresTestDatabase, testBackends } from '../helpers/test-backends.ts';+import { expectItemFailure, expectOthersUnchanged, expectUnitNotAborted, runScopedErrors, snapshotItems } from './_gbra60-shim.ts';++const A = 'people/alice-example';+const B = 'companies/acme-example';+const BYSTANDER = 'notes/a36-bystander';+const COMPLETE = '__managed_facts_complete__';+let extractions = 0;++beforeAll(() => {+ configureGateway({ chat_model: 'anthropic:claude-sonnet-4-6', env: { ANTHROPIC_API_KEY: 'sk-ant-test' } });+ __setChatTransportForTests(async (): Promise<ChatResult> => {+ extractions++;+ return { text: JSON.stringify({ facts: [+ { fact: 'Alice Example leads the platform team.', kind: 'fact', entity: A, confidence: 1, notability: 'high' },+ { fact: 'Acme Example opened a second office.', kind: 'fact', entity: B, confidence: 1, notability: 'high' },+ ] }), blocks: [], stopReason: 'end', usage: { input_tokens: 0, output_tokens: 0, cache_read_tokens: 0, cache_creation_tokens: 0 }, model: 'test:stub', providerId: 'test' };+ });+});+afterEach(() => { installFaultHook(undefined); });+afterAll(() => { __setChatTransportForTests(null); resetGateway(); });++type AtB = (engine: BrainEngine, sourceId: string) => Promise<void>;+interface Source { sourceId: string; ctx: OperationContext; reachedB: () => number }++/** A managed, database-only source holding both entity pages and a bystander; `atB` runs once as B's first fact request is prepared. */+async function managedSource(engine: BrainEngine, atB: AtB, run: (s: Source) => Promise<void>) {+ const sourceId = `a36-${Math.random().toString(36).slice(2, 10)}`;+ const home = mkdtempSync(join(tmpdir(), 'gbrain-a36-'));+ let reached = 0;+ try {+ await withEnv({ GBRAIN_HOME: home }, async () => {+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');+ await engine.executeRaw('INSERT INTO sources(id,name) VALUES($1,$1)', [sourceId]);+ await engine.putPage(A, { type: 'person', title: 'Alice Example', compiled_truth: 'Alice Example is a person.', timeline: '', frontmatter: {} }, { sourceId });+ await engine.putPage(B, { type: 'company', title: 'Acme Example', compiled_truth: 'Acme Example is a company.', timeline: '', frontmatter: {} }, { sourceId });+ await engine.putPage(BYSTANDER, { type: 'note', title: 'Bystander', compiled_truth: 'An unrelated note.', timeline: '', frontmatter: {} }, { sourceId });+ await engine.executeRaw('UPDATE persistence_brain SET enabled=true WHERE singleton=1');+ installFaultHook(async (point, detail) => {+ if (point !== 'consumer:preparing' || detail.sourceId !== sourceId || detail.operation !== 'extract_facts' || reached) return;+ const [row] = await engine.executeRaw<{ slug: string }>('SELECT slug FROM persistence_requests WHERE id=$1::uuid', [detail.writeId]);+ if (row?.slug !== B) return;+ reached++;+ await atB(engine, sourceId);+ });+ const ctx = { engine, config: { engine: engine.kind }, remote: false as const, sourceId, dryRun: false,+ logger: { info() {}, warn() {}, error() {} } } as unknown as OperationContext;+ await run({ sourceId, ctx, reachedB: () => reached });+ });+ } finally {+ installFaultHook(undefined);+ await disposePersistenceConsumer(engine);+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');+ rmSync(home, { recursive: true, force: true });+ }+}++/** Another coordinated write lands on B between the batch's admission and its publication. */+const editB: AtB = (engine, sourceId) => engine.transaction(tx => withCoordinatedWrite(tx, [sourceId], () => tx.putPage(B, { type: 'company', title: 'Acme Example',+ compiled_truth: 'Acme Example is a company. It moved offices.', timeline: '', frontmatter: {} }, { sourceId }), { requestId: null, principal: { kind: 'local_cli', id: 'a36-editor' } })).then(() => undefined);++/**+ * The fixture's pages are written before the brain turns managed, so the consumer's chunk projection catches them up+ * whenever it next runs; chunk counts are left out of the comparison here, content, liveness and links are not.+ */+async function settledItems(engine: BrainEngine, sourceId: string) {+ return new Map([...await snapshotItems(engine, sourceId)].map(([slug, { chunks: _chunks, ...row }]) => [slug, { ...row, chunks: 0 }]));+}++const requests = (engine: BrainEngine, sourceId: string) => engine.executeRaw<{ slug: string; state: string; error_code: string | null }>(+ "SELECT slug, state, error_code FROM persistence_requests WHERE source_id=$1 AND operation='extract_facts' ORDER BY sequence", [sourceId]);+const factEntities = async (engine: BrainEngine, sourceId: string) => (await engine.executeRaw<{ entity_slug: string }>(+ 'SELECT entity_slug FROM facts WHERE source_id=$1 AND expired_at IS NULL ORDER BY entity_slug', [sourceId])).map(row => row.entity_slug);++// testBackends() directly (not pathologicalArms()): check:postgres-lanes registers a Postgres arm only from this call.+for (const backend of testBackends()) {+ const databaseUrl = backend === 'postgres' ? requirePostgresTestDatabase() : undefined;+ describe(`A-36 managed facts batch with one refused entity (${backend})`, () => {+ let engine: BrainEngine;+ let close: () => Promise<void>;+ const dbDir = mkdtempSync(join(tmpdir(), 'gbrain-a36-db-'));+ beforeAll(async () => {+ if (databaseUrl) ({ engine, close } = await isolatedPersistencePostgres(databaseUrl));+ else {+ const pglite = new PGLiteEngine();+ await pglite.connect({ database_path: dbDir });+ await pglite.initSchema();+ engine = pglite;+ close = () => pglite.disconnect();+ }+ }, 120_000);+ afterAll(async () => { await close?.(); rmSync(dbDir, { recursive: true, force: true }); });++ test('A-36 item: an entity refused for its own page is reported, the committed entity is counted, and the replay returns the same without a model call', async () => {+ await managedSource(engine, editB, async ({ sourceId, ctx, reachedB }) => {+ extractions = 0;+ const requestId = randomUUID();+ const extract = () => operationsByName.extract_facts.handler(ctx, { turn_text: 'Synthetic meeting record about Alice Example and Acme Example.',+ visibility: 'world', request_id: requestId }) as Promise<{ inserted: number; refused?: Array<{ slug: string; code: string; request_id: string }> }>;+ const before = await settledItems(engine, sourceId);+ const first = await expectUnitNotAborted(extract);+ expect(reachedB()).toBe(1);+ expect(first.inserted).toBe(1);+ expect(first.refused).toHaveLength(1);+ expectItemFailure({ ...first.refused![0], key: first.refused![0]!.slug }, { key: B, code: 'revision_conflict' });+ expect(await requests(engine, sourceId)).toEqual([+ { slug: A, state: 'committed', error_code: null },+ { slug: B, state: 'conflict', error_code: 'revision_conflict' },+ { slug: COMPLETE, state: 'conflict', error_code: 'revision_conflict' },+ ]);+ expect(await factEntities(engine, sourceId)).toEqual([A]);+ expectOthersUnchanged(before, await settledItems(engine, sourceId), { changed: [A, B] });+ const replay = await expectUnitNotAborted(extract);+ expect(replay).toMatchObject({ inserted: 1, refused: first.refused });+ expect(extractions).toBe(1);+ });+ }, 180_000);++ for (const { name, error } of runScopedErrors()) {+ test(`A-36 run: a run-scoped failure at an entity's publication is never reported as that entity's refusal (${name})`, async () => {+ await managedSource(engine, async () => { throw error(); }, async ({ sourceId, ctx, reachedB }) => {+ const before = await settledItems(engine, sourceId);+ const outcome = await (operationsByName.extract_facts.handler(ctx, { turn_text: 'Synthetic meeting record about Alice Example and Acme Example.',+ visibility: 'world', request_id: randomUUID() }) as Promise<{ refused?: unknown }>).then(result => ({ result }), (thrown: Error) => ({ thrown }));+ expect(reachedB()).toBe(1);+ const b = (await requests(engine, sourceId)).find(row => row.slug === B)!;+ const after = await settledItems(engine, sourceId);+ // The extraction stops on the failure and B is left as it was, or the consumer retried the transient one and B committed; never a refusal.+ if ('result' in outcome) expect({ refused: outcome.result.refused ?? null, b: b.state }).toEqual({ refused: null, b: 'committed' });+ else expect({ b: b.state === 'committed', content: after.get(B)?.content_hash === before.get(B)?.content_hash }).toEqual({ b: false, content: true });+ expectOthersUnchanged(before, after, { changed: [A, B] });+ });+ }, 180_000);+ }++ test('A-36 explicit: a fact writer that names its entity still refuses when that entity changed after admission', async () => {+ await managedSource(engine, editB, async ({ sourceId, reachedB }) => {+ const facts = [{ fact: 'Acme Example hired a new CFO.', kind: 'fact', source: 'test', notability: 'high', sessionId: null, visibility: 'world', confidence: 1 }];+ const refusal = await publishManagedEntityFacts(engine, sourceId, B, facts as never).then(() => null, (thrown: { code?: string }) => thrown);+ expect(reachedB()).toBe(1);+ expect(refusal).toMatchObject({ code: 'revision_conflict' });+ });+ }, 180_000);+ });+}diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txtindex be99a7ec6b..80f7229010 100644--- a/test/postgres-unit-arms.txt+++ b/test/postgres-unit-arms.txt@@ -218,6 +218,7 @@ test/page-prepare-shared-origin-6517.test.tstest/page-purge-batch-sweep-6884.test.tstest/page-purge-journal-timeout-6511.test.tstest/page-state-engine.test.ts+test/pathological/a-36-facts-batch-partial-refusal.test.tstest/pathological/c-9-facts-absorb-dead.test.tstest/persistence-background-pause.test.tstest/persistence-bounded-read-session.test.ts