Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(persistence): a member a group interruption charged is never reclaimed by the same pass

garrytana35c80820bca
diff --git a/src/core/persistence/group-publish.ts b/src/core/persistence/group-publish.ts
index f0547d01e0..b366cb9a32 100644
--- a/src/core/persistence/group-publish.ts
+++ b/src/core/persistence/group-publish.ts
@@ -213,7 +213,9 @@ export interface GroupHooks {
/**
* Publishes the whole group or nothing. `done` null means the transaction did
* not commit; `requeued` lists members whose claims the file restoration
- * released, which the caller may claim again while it holds the worktree.
+ * released, which the caller may claim again while it holds the worktree. A
+ * member charged a transient interruption is never listed: its next claim
+ * waits for its backoff and prepares again, however long the restoration took.
*/
/** Runs one transaction: `engine.transaction`, or a reserved connection's (Phase 4.4). */
export type TransactionRunner = <T>(fn: (tx: BrainEngine) => Promise<T>) => Promise<T>;
@@ -420,7 +422,7 @@ export async function publishGroup(engine: BrainEngine, rows: WriteRequest[], pr
const failure = charged?.failure ? withAttempt(charged.failure, 'publication') : undefined;
await markRecovering(engine, row, failure ? 'publication_failed' : 'publication_not_started', failure);
const recovered = await recoverPublication(engine, row.id, hostId, true, failure, true);
- if (recovered.state === 'queued' && !recovered.recovery) requeued.push(row.id);
+ if (!charged && recovered.state === 'queued' && !recovered.recovery) requeued.push(row.id);
} catch { /* the recovery record stays; the worktree waits for the recovery scan */ }
}
await hooks.rolledBack?.(rows);
diff --git a/test/pathological/lp-c-publication-ceiling-retry.test.ts b/test/pathological/lp-c-publication-ceiling-retry.test.ts
index 3078019c37..85bab57104 100644
--- a/test/pathological/lp-c-publication-ceiling-retry.test.ts
+++ b/test/pathological/lp-c-publication-ceiling-retry.test.ts
@@ -258,6 +258,47 @@ for (const { backend, databaseUrl } of pathologicalArms()) describe(`[${backend}
for (const { sourcePath } of files) expect(await engine.readPageSnapshot(sourcePath.replace(/\.md$/, ''), { sourceId })).not.toBeNull();
}, 180_000);
+ test('a member a group interruption charged is claimed and prepared again even when its backoff elapses before the pass ends', async () => {
+ await engine.setConfig('persistence.max_transient_retries', '2');
+ resetWriteSwitches();
+ const dir = `grp-slow-${++n}`;
+ mkdirSync(join(brainDir, dir), { recursive: true });
+ const files = ['a', 'b'].map(name => {
+ const sourcePath = `${dir}/${name}.md`;
+ writeFileSync(join(brainDir, sourcePath), page(`Slow ${name} ${n}`));
+ return { filePath: join(brainDir, sourcePath), sourcePath };
+ });
+ // As above, and each member's charge is followed by a pause longer than the first backoff (1 s), as on a loaded host:
+ // the first member's backoff has elapsed before the pass is done with the group, so only the charge itself keeps the
+ // pass from taking that claim back and publishing its old preparation under it.
+ const unpublished = new Set<string>(), claims = new Map<string, number>(), grouped: string[][] = [];
+ installFaultHook(async (point, detail) => {
+ if (detail.sourceId !== sourceId) return;
+ if (point === 'consumer:preparing') { unpublished.add(detail.requestId!); claims.set(detail.requestId!, (claims.get(detail.requestId!) ?? 0) + 1); return; }
+ if (point === 'persistence:recovery_interruption_charged') { await new Promise(resolve => setTimeout(resolve, 1_200)); return; }
+ if (point !== 'publication:before_commit') return;
+ const together = unpublished.size;
+ unpublished.delete(detail.requestId!);
+ if (together < 2 || grouped.length >= GUARD) return;
+ grouped.push([...unpublished, detail.requestId!]);
+ unpublished.clear();
+ await new Promise(resolve => setTimeout(resolve, PARK_MS));
+ throw discarded();
+ });
+ const results = await withEnv({ GBRAIN_HOME: home }, () => importManagedFiles(engine, files, { sourceId, noEmbed: true }));
+ installFaultHook(undefined);
+ expect(results.map(result => result.status === 'rejected' ? (result.reason as { code?: string }).code : result.status)).toEqual(['fulfilled', 'fulfilled']);
+ expect(grouped.length).toBe(1);
+ const rows = await engine.executeRaw<{ request_id: string; state: string; transient_interruptions: number }>(
+ "SELECT request_id::text AS request_id,state,transient_interruptions FROM persistence_requests WHERE source_id=$1 AND intent->>'import_batch' IS NOT NULL AND slug LIKE $2 ORDER BY sequence",
+ [sourceId, `${dir}/%`]);
+ expect(rows.length).toBe(2);
+ for (const member of rows) {
+ expect(member).toMatchObject({ state: 'committed', transient_interruptions: 1 });
+ expect(claims.get(member.request_id)).toBe(2);
+ }
+ }, 180_000);
+
test('a single overrun is retried, never a terminal storage_error (on Postgres the retried publish surfaces as a bare AbortError), and commits', async () => {
const slug = `wt/overrun-once-${++n}`;
expect(await call({ slug, content: page(`Prior ${n}`), request_id: randomUUID() }, sourceId)).toMatchObject({ state: 'committed' });