Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes
garrytan0945c326cd6e
diff --git a/src/core/context/corpus-segments.ts b/src/core/context/corpus-segments.tsindex fc86c17d6b..93e3812d07 100644--- a/src/core/context/corpus-segments.ts+++ b/src/core/context/corpus-segments.ts@@ -748,6 +748,8 @@ export interface CorpusFailureRecord {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;+ /** #9279: true once a deterministic failure held the window; retention keeps its file until it commits or an operator retries it. */+ held?: boolean;}/** The failure record, or null when absent or unparseable. Never throws. */@@ -759,7 +761,8 @@ export function readCorpusFailure(full: string): CorpusFailureRecord | null {|| !(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')- || !(r.upgrade_retried === undefined || typeof r.upgrade_retried === 'boolean')) return null;+ || !(r.upgrade_retried === undefined || typeof r.upgrade_retried === 'boolean')+ || !(r.held === undefined || typeof r.held === 'boolean')) return null;return r;} catch {return null;@@ -768,7 +771,7 @@ export function readCorpusFailure(full: string): CorpusFailureRecord | null {/** #9279: a window stopped by a deterministic failure: its file is never extracted until a retry, so retention keeps it. */export function corpusFailureHeld(record: CorpusFailureRecord | null): boolean {- return !!record && record.count >= CORPUS_WINDOW_FAILURE_CAP && CORPUS_DETERMINISTIC_FAILURE_CODES.includes(record.last_code);+ return !!record && (record.held === true || (record.count >= CORPUS_WINDOW_FAILURE_CAP && CORPUS_DETERMINISTIC_FAILURE_CODES.includes(record.last_code)));}/** Every corpus `.txt` in `dir` with a failure record. Missing dir = empty; never throws. */diff --git a/src/core/context/corpus-windows.ts b/src/core/context/corpus-windows.tsindex 7c015ba28b..7774edb0d7 100644--- a/src/core/context/corpus-windows.ts+++ b/src/core/context/corpus-windows.ts@@ -521,7 +521,8 @@ function failureCode(e: unknown): string {* (CORPUS_DETERMINISTIC_FAILURE_CODES) stops the window at once, stamped with* 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.+ * one there starts a fresh count, so it still gets the ordinary retries, and+ * the window stays held (its file kept) until it commits or an operator retries.* Returns the record written.*/export async function recordCorpusFailure(@@ -552,6 +553,7 @@ export async function recordCorpusFailure(rearmed: same ? prev!.rearmed || rearm : false,...(deterministic ? { build: VERSION } : {}),...(retried ? { upgrade_retried: true } : {}),+ ...(deterministic || held ? { held: true } : {}),};const path = full + CORPUS_FAILURE_SUFFIX;const tmp = `${path}.tmp-${process.pid}`;diff --git a/src/core/google/google-source.ts b/src/core/google/google-source.tsindex c65dbb478e..4a5df66472 100644--- a/src/core/google/google-source.ts+++ b/src/core/google/google-source.ts@@ -165,13 +165,14 @@ function loopRecoveryState(state: GoogleSourceState): Record<string, unknown> {/*** #9011: a thread whose import was still pending when processThread ran detection, then refused as purged content,* leaves what detection derived from a page that never landed. Drops its extract candidate and grace hold, and the open- * loops of that slug while no live page holds it (open_loops.page_slug has no foreign key).+ * loops of that slug while no live page holds it (open_loops.page_slug has no foreign key); the loops also for a refusal+ * an earlier run recorded, which may have resolved after that run's cleanup.*/async function forgetRefusedPurgedThreads(deps: GoogleSyncDeps, managed: ManagedConnectorSync, state: GoogleSourceState): Promise<void> {await managed.drainPending();- const slugs = managed.refusedPurgedSlugs();+ const slugs = managed.refusedPurgedSlugs(true);if (!slugs.length) return;- const refused = new Set(slugs);+ const refused = new Set(managed.refusedPurgedSlugs());deps.extractCandidates = deps.extractCandidates.filter((c) => !refused.has(c.slug));for (const [tid, hold] of Object.entries(state.loop_grace_holds ?? {})) if (hold.slug && refused.has(hold.slug)) delete state.loop_grace_holds![tid];await deps.engine.executeRaw(`DELETE FROM open_loops WHERE source_id=$1 AND page_slug=ANY($2::text[])diff --git a/src/core/persistence/connector-sync.ts b/src/core/persistence/connector-sync.tsindex ab39997b18..9521bda305 100644--- a/src/core/persistence/connector-sync.ts+++ b/src/core/persistence/connector-sync.ts@@ -830,9 +830,13 @@ export class ManagedConnectorSync {process.stderr.write(`[connector] ${this.sourceId}: skipped ${slug}: its content matches a page purge tombstone${row ? ` (request ${row.request_id})` : ''}; `+ `the rest of the sync continues. To let it back in: gbrain pages unpurge ${slug}\n`);}- /** #9011: slugs this run skipped because the pages guard refused their admitted receipt as purged content. */- refusedPurgedSlugs(): string[] {- return [...new Set(this.purged.filter(item => item.request_id !== null).map(item => item.slug))];+ /**+ * #9011: slugs this run skipped because the pages guard refused their admitted receipt as purged content; with+ * `persisted`, also those earlier runs recorded (a refusal can resolve after a run's cleanup).+ */+ refusedPurgedSlugs(persisted = false): string[] {+ const items = persisted ? [...this.purged, ...(this.connectorState.purged_items ?? [])] : this.purged;+ return [...new Set(items.filter(item => item.request_id !== null).map(item => item.slug))];}/*** #6831: screens a page import against the source's page purge tombstones before admission, with the content hashdiff --git a/test/google-purged-pending-open-loops-9011.test.ts b/test/google-purged-pending-open-loops-9011.test.tsindex ecbc4b7549..0aff340bb4 100644--- a/test/google-purged-pending-open-loops-9011.test.ts+++ b/test/google-purged-pending-open-loops-9011.test.ts@@ -71,6 +71,7 @@ describe('#9011: a purged Gmail thread refused at publication after a pending imconst [slug] = await livePages();expect(slug).toBeDefined();expect(await subjectLoops()).toBe(1);+ const [{ row: loopRow }] = await engine.executeRaw<{ row: string }>('SELECT to_jsonb(o)::text AS row FROM open_loops o WHERE source_id=$1', [f.id]) as [{ row: string }];// 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(); }@@ -108,6 +109,14 @@ describe('#9011: a purged Gmail thread refused at publication after a pending imexpect(state.purged_items?.filter(item => item.request_id !== null).map(item => item.slug)).toContain(slug!);expect(await subjectLoops()).toBe(0);expect(await quotedLoops()).toBe(0);++ // A loop left by a refusal that resolved after its run's cleanup goes on a later run, from the recorded refusal.+ await engine.executeRaw('INSERT INTO open_loops SELECT * FROM jsonb_populate_record(NULL::open_loops, $1::text::jsonb)', [loopRow]);+ expect(await subjectLoops()).toBe(1);+ await run();+ await settleConnectorWrites(engine, f.id).catch(() => {});+ await disposePersistenceConsumer(engine);+ expect(await subjectLoops()).toBe(0);}}), TEST_TIMEOUT_MS);});diff --git a/test/sweep-segmented-entity-9279.test.ts b/test/sweep-segmented-entity-9279.test.tsindex ac7d977464..e7bbb41e95 100644--- a/test/sweep-segmented-entity-9279.test.ts+++ b/test/sweep-segmented-entity-9279.test.ts@@ -266,8 +266,13 @@ describe('a deterministic refusal after the model call (#9279)', () => {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(record).toMatchObject({ count: 1, last_code: 'unavailable', upgrade_retried: true, held: true });expect(corpusFailureGate(record, record.key, null, null, Date.now())).toBe('run');+ const full = join(dir, 'held-session.txt');+ const old = (Date.now() - (RETENTION_DAYS * CORPUS_UNINGESTED_RETENTION_FACTOR + 1) * DAY_MS) / 1000;+ utimesSync(full, old, old);+ gcCorpusTurnFiles(dir, RETENTION_DAYS * DAY_MS);+ expect(existsSync(full)).toBe(true);} finally { rmSync(dir, { recursive: true, force: true }); }}, TEST_MS);});