Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes
garrytanba619229e7ba
diff --git a/src/core/context/corpus-segments.ts b/src/core/context/corpus-segments.tsindex 48eb1306fe..fc86c17d6b 100644--- a/src/core/context/corpus-segments.ts+++ b/src/core/context/corpus-segments.ts@@ -746,6 +746,8 @@ export interface CorpusFailureRecord {rearmed: boolean;/** #9279: the gbrain version that recorded a deterministic failure (CORPUS_DETERMINISTIC_FAILURE_CODES). */build?: string;+ /** #9279: true once the automatic retry on a newer build ran; only an operator retry runs the window after that. */+ upgrade_retried?: boolean;}/** The failure record, or null when absent or unparseable. Never throws. */@@ -756,7 +758,8 @@ export function readCorpusFailure(full: string): CorpusFailureRecord | null {|| !Number.isFinite(r.last_at) || !Number.isFinite(r.first_at) || typeof r.last_code !== 'string'|| !(r.source_incarnation === null || typeof r.source_incarnation === 'string')|| !(r.fingerprint === null || typeof r.fingerprint === 'string') || typeof r.rearmed !== 'boolean'- || !(r.build === undefined || typeof r.build === 'string')) return null;+ || !(r.build === undefined || typeof r.build === 'string')+ || !(r.upgrade_retried === undefined || typeof r.upgrade_retried === 'boolean')) return null;return r;} catch {return null;diff --git a/src/core/context/corpus-windows.ts b/src/core/context/corpus-windows.tsindex 4d122b031b..7c015ba28b 100644--- a/src/core/context/corpus-windows.ts+++ b/src/core/context/corpus-windows.ts@@ -36,7 +36,7 @@ import { withAIInvocationGuard } from '../ai/invocation-guard.ts';import { parseCorpusTurns, type CorpusTurn } from './corpus-turns.ts';import { VERSION } from '../../version.ts';import {- CORPUS_DETERMINISTIC_FAILURE_CODES, CORPUS_FAILURE_SUFFIX, CORPUS_NON_TERMINAL_SKIPS, CORPUS_PROGRESS_LOCK_SUFFIX, CORPUS_PROGRESS_SUFFIX, CORPUS_WINDOW_FAILURE_CAP,+ CORPUS_DETERMINISTIC_FAILURE_CODES, CORPUS_FAILURE_SUFFIX, corpusFailureHeld, CORPUS_NON_TERMINAL_SKIPS, CORPUS_PROGRESS_LOCK_SUFFIX, CORPUS_PROGRESS_SUFFIX, CORPUS_WINDOW_FAILURE_CAP,corpusFileCompletion, corpusFileFinished, parseCorpusProgress, parseWbFileName, readCorpusFailure, type CorpusFailureRecord,} from './corpus-segments.ts';@@ -419,7 +419,8 @@ function windowKey(turns: CorpusTurn[], at: { turn: number; offset: number }): s* changed since its last failure. #9279: a window stopped by a deterministic* failure runs once on a gbrain build other than the one that recorded it* (a record from before #9279 carries no build), so an upgrade that fixes the- * refusal extracts it with no command.+ * refusal extracts it with no command. That automatic retry happens once per+ * record (upgrade_retried); after it only an operator retry runs the window.*/export function corpusFailureGate(record: CorpusFailureRecord | null,@@ -430,7 +431,7 @@ export function corpusFailureGate(): 'run' | 'backoff' | 'dead_lettered' {if (!record || record.key !== key || record.source_incarnation !== incarnation) return 'run';if (record.count >= CORPUS_WINDOW_FAILURE_CAP) {- const rebuilt = CORPUS_DETERMINISTIC_FAILURE_CODES.includes(record.last_code) && record.build !== VERSION;+ const rebuilt = CORPUS_DETERMINISTIC_FAILURE_CODES.includes(record.last_code) && record.build !== VERSION && record.upgrade_retried !== true;return rebuilt || rearms(record, fingerprint) ? 'run' : 'dead_lettered';}if (record.count === 1) return 'run';@@ -518,8 +519,10 @@ function failureCode(e: unknown): string {* count; the error label never resets it. A capped window re-armed by a* fingerprint change starts over once. #9279: a deterministic failure* (CORPUS_DETERMINISTIC_FAILURE_CODES) stops the window at once, stamped with- * this build, instead of paying for four more identical refusals. Returns the- * record written.+ * this build, instead of paying for four more identical refusals. A failure on+ * the automatic retry of a held window marks it upgrade_retried; a transient+ * one there starts a fresh count, so it still gets the ordinary retries.+ * Returns the record written.*/export async function recordCorpusFailure(full: string,@@ -534,7 +537,9 @@ export async function recordCorpusFailure(const rearm = same && prev!.count >= CORPUS_WINDOW_FAILURE_CAP && rearms(prev!, fingerprint);const code = failureCode(error);const deterministic = CORPUS_DETERMINISTIC_FAILURE_CODES.includes(code);- const count = same && !rearm ? prev!.count + 1 : 1;+ const held = same && corpusFailureHeld(prev);+ const retried = same && (prev!.upgrade_retried === true || (held && prev!.build !== VERSION));+ const count = same && !rearm && !(held && !deterministic) ? prev!.count + 1 : 1;const record: CorpusFailureRecord = {version: 1,key,@@ -546,6 +551,7 @@ export async function recordCorpusFailure(fingerprint,rearmed: same ? prev!.rearmed || rearm : false,...(deterministic ? { build: VERSION } : {}),+ ...(retried ? { upgrade_retried: true } : {}),};const path = full + CORPUS_FAILURE_SUFFIX;const tmp = `${path}.tmp-${process.pid}`;diff --git a/src/core/facts/subject-infer-write.ts b/src/core/facts/subject-infer-write.tsindex 64979bc2c6..2c79d93921 100644--- a/src/core/facts/subject-infer-write.ts+++ b/src/core/facts/subject-infer-write.ts@@ -97,6 +97,14 @@ async function targetFenceClean(engine: BrainEngine, sourceId: string, slug: strconst { isSegmentedPage } = await import('../page-body/page-shape.ts');const { materializedSnapshot, segmentedOverCeiling } = await import('../persistence/segmented-apply.ts');if (isSegmentedPage(snapshot.page) && (!managed || segmentedOverCeiling(snapshot.page))) return false;+ let body: string;+ try {+ body = (await materializedSnapshot(engine, snapshot))!.page.compiled_truth;+ } catch (error) {+ const { OperationError } = await import('../ops/contract.ts');+ if (error instanceof OperationError) return false;+ throw error;+ }const { parseFactsFence } = await import('../facts-fence.ts');- return parseFactsFence((await materializedSnapshot(engine, snapshot))!.page.compiled_truth).warnings.length === 0;+ return parseFactsFence(body).warnings.length === 0;}diff --git a/test/google-purged-pending-open-loops-9011.test.ts b/test/google-purged-pending-open-loops-9011.test.tsindex 312689d160..ecbc4b7549 100644--- a/test/google-purged-pending-open-loops-9011.test.ts+++ b/test/google-purged-pending-open-loops-9011.test.ts@@ -71,6 +71,12 @@ describe('#9011: a purged Gmail thread refused at publication after a pending imconst [slug] = await livePages();expect(slug).toBeDefined();expect(await subjectLoops()).toBe(1);+ // A refusal naming a slug whose page is live keeps that page's loops.+ const live = spyOn(ManagedConnectorSync.prototype, 'refusedPurgedSlugs').mockReturnValue([slug!]);+ try { await run(); } finally { live.mockRestore(); }+ await settleConnectorWrites(engine, f.id);+ await disposePersistenceConsumer(engine);+ expect(await subjectLoops()).toBe(1);const snapshot = (await engine.readPageSnapshot(slug!, { sourceId: f.id }))!;await submitPageMutation(ctx(engine, f.id), { operation: 'delete_page', params: { slug: slug!, purge: true, expected_revision: snapshot.revision } });await settleConnectorWrites(engine, f.id);diff --git a/test/sweep-segmented-entity-9279.test.ts b/test/sweep-segmented-entity-9279.test.tsindex ac8eb08da4..ac7d977464 100644--- a/test/sweep-segmented-entity-9279.test.ts+++ b/test/sweep-segmented-entity-9279.test.ts@@ -233,4 +233,41 @@ describe('a deterministic refusal after the model call (#9279)', () => {expect(corpusFailureGate({ ...record, last_code: 'provider_error' }, record.key, null, null, Date.now())).toBe('dead_lettered');} finally { rmSync(dir, { recursive: true, force: true }); }}, TEST_MS);++ async function retryHeld(error: () => Error) {+ const dir = mkdtempSync(join(tmpdir(), 'gbrain-9279-retry-'));+ const full = join(dir, 'held-session.txt');+ writeFileSync(full, transcript());+ const key = await corpusFrontierKey(full, 'held-session.txt');+ writeFileSync(full + CORPUS_FAILURE_SUFFIX, JSON.stringify({ version: 1, key, source_incarnation: null, count: CORPUS_WINDOW_FAILURE_CAP,+ first_at: 1, last_at: 1, last_code: 'page_body_segmented', fingerprint: null, rearmed: false, build: '0.0.0.1' }) + '\n');+ let paid = 0;+ await runCorpusWindows({+ full, raw: readFileSync(full, 'utf8'), fileStat: corpusFileStat((await import('node:fs')).statSync(full)), maxWindows: 1,+ overBudget: () => false, signal: new AbortController().signal, failures: { fingerprint: null },+ extract: async () => {+ await invokeAI({ operation: 'extract', model: 'anthropic:test', kind: 'chat' }, async () => { paid++; return 'facts'; }, () => null);+ throw error();+ },+ }).catch((e: unknown) => e);+ return { dir, paid, record: readCorpusFailure(full)! };+ }++ test('retries a held window automatically once, not on every later build', async () => {+ const { dir, paid, record } = await retryHeld(() => opError('page_body_segmented', 'The entity page is stored in segments.', 'Retrying refuses the same way.'));+ try {+ expect(paid).toBe(1);+ expect(record).toMatchObject({ count: CORPUS_WINDOW_FAILURE_CAP + 1, last_code: 'page_body_segmented', build: VERSION, upgrade_retried: true });+ expect(corpusFailureGate({ ...record, build: '0.0.0.2' }, record.key, null, null, Date.now())).toBe('dead_lettered');+ } finally { rmSync(dir, { recursive: true, force: true }); }+ }, TEST_MS);++ test('a transient failure on that retry starts a fresh count instead of stopping the window for good', async () => {+ const { dir, paid, record } = await retryHeld(() => opError('unavailable', 'The provider timed out.', 'Retry later.'));+ try {+ expect(paid).toBe(1);+ expect(record).toMatchObject({ count: 1, last_code: 'unavailable', upgrade_retried: true });+ expect(corpusFailureGate(record, record.key, null, null, Date.now())).toBe('run');+ } finally { rmSync(dir, { recursive: true, force: true }); }+ }, TEST_MS);});