Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Retry the loops job when the write door gives up on a passing database error, instead of saving the loop with no fact (GST-2692)

garrytanb2b258aed5f4
diff --git a/src/core/google/loops-extract.ts b/src/core/google/loops-extract.ts
index 8c04d28a40..c551a5198c 100644
--- a/src/core/google/loops-extract.ts
+++ b/src/core/google/loops-extract.ts
@@ -39,6 +39,8 @@ import { decideFactWrite } from '../write-gate-store.ts';
import { transientDatabaseFailure, transientInterruption } from '../persistence/transient-retry.ts';
export const LOOPS_EXTRACT_JOB = 'loops_extract';
+/** GST-2692: a passing database condition as the managed write door reports it (retries ran out, still pending, outcome unknown). */
+const DOOR_TRANSIENT_CODES = new Set(['write_interrupted', 'write_pending', 'write_outcome_unknown']);
/**
* Historical batch size. NO LONGER an enqueue cap — every eligible thread is
* queued (up to the generous safety ceiling below) and the worker's
@@ -531,7 +533,7 @@ export async function runLoopsExtract(
} catch (err) {
const code = (err as { code?: unknown })?.code;
// GST-2692: a passing database error (a cancel, a stale plan, contention, a lost connection) retries the job, so the fact is not dropped.
- if (transientDatabaseFailure(err) || transientInterruption(err)) {
+ if (transientDatabaseFailure(err) || transientInterruption(err) || (typeof code === 'string' && DOOR_TRANSIENT_CODES.has(code))) {
throw new LoopsExtractRetryableError('fact_transient',
`loops_extract: a passing database error (${typeof code === 'string' ? code : 'transient'}) while saving a commitment fact of ${payload.slug} — retryable`);
}
diff --git a/test/strict-fact-paths-gst2692.test.ts b/test/strict-fact-paths-gst2692.test.ts
index 168cc6f0b4..62bd1a9e9e 100644
--- a/test/strict-fact-paths-gst2692.test.ts
+++ b/test/strict-fact-paths-gst2692.test.ts
@@ -59,8 +59,8 @@ const commits = (root: string, claim: string) => git(root, 'log', '--all', '--fo
const holdStatus = async (engine: BrainEngine, ref: string) => (await engine.executeRaw<{ status: string }>('SELECT status FROM write_gate_holds WHERE id=$1', [Number(ref.slice(1))]))[0]?.status;
const releaseRequests = (engine: BrainEngine) => engine.executeRaw("SELECT 1 FROM persistence_requests WHERE operation='trust_release'");
-/** Throws `error()` from holdingPageFor's access read outside an extract_facts preparation (at admission), at most `times` times. */
-function failAccessReadAtAdmission(engine: BrainEngine, error: () => Error, times: number) {
+/** Throws `error()` from holdingPageFor's access read outside an extract_facts preparation (at admission), or inside it, at most `times` times. */
+function failAccessReadAtAdmission(engine: BrainEngine, error: () => Error, times: number, inPreparation = false) {
let preparing = false;
let thrown = 0;
installFaultHook(async (point, detail) => {
@@ -70,7 +70,7 @@ function failAccessReadAtAdmission(engine: BrainEngine, error: () => Error, time
});
const original = engine.executeRaw as (this: BrainEngine, ...args: unknown[]) => Promise<unknown>;
const spy = spyOn(engine, 'executeRaw').mockImplementation((async function (this: BrainEngine, sql: string, params?: unknown[], opts?: unknown) {
- if (!preparing && thrown < times && sql.includes(ACCESS_READ)) { thrown++; throw error(); }
+ if (preparing === inPreparation && thrown < times && sql.includes(ACCESS_READ)) { thrown++; throw error(); }
return original.call(this, sql, params, opts);
}) as typeof engine.executeRaw);
return { thrown: () => thrown, restore() { spy.mockRestore(); installFaultHook(undefined); } };
@@ -142,6 +142,22 @@ for (const backend of testBackends()) describe(`[${backend}] GST-2692: held and
expect(commits(root, claim)).toHaveLength(1);
}), 300_000);
+ for (const [arm, inPreparation, error] of [
+ ['a statement cancel in preparation with no retries left (write_interrupted)', true, () => Object.assign(new Error('canceling statement due to statement timeout'), { code: '57014' })],
+ ] as const) test(`${arm} makes the loops job retry instead of saving the loop with no fact`, () => brain(true, async ({ engine, ctx }) => {
+ await engine.setConfig('loops.extraction_enabled', 'true');
+ await engine.setConfig('persistence.max_transient_retries', '0');
+ await operationsByName.put_page!.handler(ctx, { slug: EMAIL, request_id: randomUUID(),
+ content: '---\ntitle: Invoice thread\ntype: email\nthread_id: invoice-thread\nfrom: [email protected]\n---\nI will send Bob the invoice summary by Friday.\n' });
+ await settleManagedGitEffects(engine);
+ const claim = 'Send Bob example the invoice summary by Friday.';
+ const fault = failAccessReadAtAdmission(engine, error, 1, inPreparation);
+ const first = await runLoopsJob(engine, claim).finally(() => fault.restore());
+ expect(fault.thrown()).toBe(1);
+ expect({ error: first.error && { name: first.error.name, reason: first.error.reason }, loops: first.loops, log: first.log, rows: await factRows(engine, claim) })
+ .toEqual({ error: { name: 'LoopsExtractRetryableError', reason: 'fact_transient' }, loops: [], log: [], rows: [] });
+ }), 300_000);
+
test('managed persistence off: an owner\'s release refuses git_home_required with nothing written, for an entity with a page and one without', () => brain(true, async ({ engine, ctx, root }) => {
await operationsByName.put_page!.handler(ctx, { slug: PAGED, request_id: randomUUID(), content: '---\ntype: person\ntitle: Carol Example\n---\n\nNotes about Carol Example.\n' });
await settleManagedGitEffects(engine);