Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(extract_atoms): a run that admits second waits on the batch another run of the origin admitted (#7923)

garrytanfd0b781f0f99
diff --git a/src/core/cycle/extract-atoms.ts b/src/core/cycle/extract-atoms.ts
index fe9a412626..23da89ad5c 100644
--- a/src/core/cycle/extract-atoms.ts
+++ b/src/core/cycle/extract-atoms.ts
@@ -88,7 +88,7 @@ import { utcDate } from './cycle-date.ts';
import { atomSourceDate } from './atom-source-date.ts';
import { normalizeForGrounding } from './synthesize-verify.ts';
import type { TranscriptPageIndex } from '../transcripts/discover.ts';
-import { atomOriginPin, atomWriteFailure, type AtomWriteFailure, managedAtomSession, readAtomOrigin, resumeManagedAtoms, publishManagedAtoms, publishManagedAtomGeneration, resumeManagedGenerationMembers,
+import { atomOriginPin, atomWriteFailure, type AtomWriteFailure, concurrentAtomBatch, managedAtomSession, readAtomOrigin, resumeManagedAtoms, publishManagedAtoms, publishManagedAtomGeneration, resumeManagedGenerationMembers,
MANAGED_ATOM_DISCOVERY_SQL, managedAtomDiscoverySql, type AtomOrigin } from '../persistence/atom-maintenance.ts';
import { effectiveVisibility } from '../search/private-visibility.ts';
import { OperationError } from '../ops/contract.ts';
@@ -1345,6 +1345,10 @@ export async function runPhaseExtractAtoms(
else pagesSkipped++;
continue;
}
+ const concurrent = managed && origin ? await concurrentAtomBatch(engine, managed, origin, err) : null; // #7923
+ if (concurrent === 'done') { duplicatesSkipped++; continue; }
+ if (concurrent === 'held') { heldFailed++; continue; }
+ if (concurrent === 'pending') { writesPending++; continue; }
const written = managed && origin ? await atomWriteFailure(engine, managed, origin, err) : null; // #7514: never a provider error
if (written) { hardFailureCount++; failures.push({ source: originLabel, ...written }); continue; }
// gbrain#4148: classify. Transient provider/infra errors (timeouts,
diff --git a/src/core/persistence/atom-maintenance.ts b/src/core/persistence/atom-maintenance.ts
index c63c32aa28..4e356b69df 100644
--- a/src/core/persistence/atom-maintenance.ts
+++ b/src/core/persistence/atom-maintenance.ts
@@ -563,6 +563,28 @@ async function resumeAtomBatch(engine: BrainEngine, session: ManagedAtomSession,
return 'done';
}
+/**
+ * #7923: two runs of one origin under one run key (a force-evicted phase still running beside the next run, a manual
+ * drain beside the cycle) both find no accepted batch and both extract; the run that admits second reuses the batch's
+ * deterministic request ids with other intents (a new extracted_at in each atom, another runId, other children or
+ * atoms), and the journal refuses it with
+ * idempotency_conflict. When a batch completion already holds this run key, that refusal means another run published
+ * the origin: wait on its batch like a resume. Null for any other error, when no batch holds the run key (a real
+ * collision stays a failure), or when waiting on that batch fails (the refusal is reported as before).
+ */
+export async function concurrentAtomBatch(engine: BrainEngine, session: ManagedAtomSession, origin: AtomOrigin,
+ error: unknown): Promise<'done' | 'held' | 'pending' | null> {
+ if (!(error instanceof OperationError) || error.code !== 'idempotency_conflict') return null;
+ const completionId = atomRequestId(atomClaim(session, origin).runKey, '__managed_atom_complete__');
+ if (!await getWriteRequest(engine, session.authority.principal, completionId)) return null;
+ try {
+ const resumed = await resumeManagedAtoms(engine, session, origin);
+ return resumed === 'run' ? null : resumed;
+ } catch (waited) {
+ return acceptedPendingReceipt(waited) ? 'pending' : null;
+ }
+}
+
export interface ManagedAtomRetirement {
slug: string;
pageId: number;
diff --git a/test/extract-atoms-concurrent-run-7923.test.ts b/test/extract-atoms-concurrent-run-7923.test.ts
new file mode 100644
index 0000000000..3de6f655ec
--- /dev/null
+++ b/test/extract-atoms-concurrent-run-7923.test.ts
@@ -0,0 +1,68 @@
+/**
+ * #7923: two extract_atoms runs of one origin under the same run key (a
+ * force-evicted phase still running when the next run starts, or a manual
+ * drain beside autopilot's cycle). Both see no accepted batch, both call the
+ * model, and the one that admits second reuses the batch's deterministic
+ * completion request id with a different intent (other children, possibly
+ * another atom set). The journal refused that as idempotency_conflict, the
+ * phase counted it as a hard failure and the round halted (item_error),
+ * although the origin's atoms had just been published by the other run.
+ * Now the second run finds the batch the first one admitted under its run
+ * key and waits on it, so the round completes and nothing is admitted twice.
+ *
+ * Managed brain, PGLite and, with DATABASE_URL, Postgres. Synthetic data only.
+ */
+import { describe, expect, test } from 'bun:test';
+import { mkdirSync, writeFileSync } from 'node:fs';
+import { dirname, join } from 'node:path';
+import type { ChatResult } from '../src/core/ai/gateway.ts';
+import { runPhaseExtractAtoms } from '../src/core/cycle/extract-atoms.ts';
+import { ensureCorpusSpoolDir, sessionCorpusFileName } from '../src/core/context/corpus-segments.ts';
+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
+import { sha256 } from '../src/core/persistence/digest.ts';
+import { managedBrain } from './helpers/managed-brain.ts';
+import { requirePostgresTestDatabase, testBackends } from './helpers/test-backends.ts';
+
+const body = (label: string) => (label + ': the team ships small changes, measures each launch and reviews outcomes weekly. ').repeat(12);
+
+function reply(titles: string[]): ChatResult {
+ const text = JSON.stringify(titles.map(title => ({ title, body: title + '.', atom_type: 'insight' })));
+ return { text, blocks: [{ type: 'text', text }], stopReason: 'end',
+ usage: { input_tokens: 10, output_tokens: 10, cache_read_tokens: 0, cache_creation_tokens: 0 }, model: 'anthropic:claude-haiku-4-5', providerId: 'anthropic' };
+}
+
+for (const backend of testBackends()) {
+ const databaseUrl = backend === 'postgres' ? requirePostgresTestDatabase() : undefined;
+ describe(`#7923 a second run of one origin waits on the batch the first admitted (${backend})`, () => {
+ for (const [label, second] of [['a different atom set', ['Ship small changes', 'Measure each launch']], ['the same atom set', ['Ship small changes', 'Review outcomes weekly']]] as const) {
+ test(`the run that admits second, with ${label}, completes instead of halting on idempotency_conflict`, async () => {
+ await managedBrain(async ({ engine, root }) => {
+ const corpus = join(dirname(root), 'corpus');
+ mkdirSync(corpus);
+ await engine.setConfig('dream.synthesize.session_corpus_dir', corpus);
+ const filePath = join(ensureCorpusSpoolDir(corpus), sessionCorpusFileName('sess-a', 'default'));
+ const content = body('race');
+ writeFileSync(filePath, content);
+ const transcripts = [{ filePath, content, contentHash: sha256(content) }];
+ let first: Awaited<ReturnType<typeof runPhaseExtractAtoms>> | undefined;
+ const outer = await runPhaseExtractAtoms(engine, { sourceId: 'default', _pages: [], _transcripts: transcripts,
+ _chat: async () => {
+ // The other run extracts and publishes this origin while this run waits on its model call.
+ first = await runPhaseExtractAtoms(engine, { sourceId: 'default', _pages: [], _transcripts: transcripts,
+ _chat: async () => reply(['Ship small changes', 'Review outcomes weekly']) });
+ return reply([...second]);
+ } });
+ await disposePersistenceConsumer(engine);
+ expect(first?.status).not.toBe('fail');
+ expect(outer.details.failures).toEqual([]);
+ expect(outer.status).toBe('ok');
+ expect(outer.details.duplicates_skipped).toBe(1);
+ const atoms = await engine.executeRaw<{ slug: string }>(
+ "SELECT slug FROM pages WHERE source_id='default' AND type='atom' AND deleted_at IS NULL ORDER BY slug");
+ expect(atoms.map(row => row.slug).filter(slug => /ship-small-changes|review-outcomes-weekly/.test(slug))).toHaveLength(2);
+ expect(atoms.some(row => /measure-each-launch/.test(row.slug))).toBe(false);
+ }, { databaseUrl });
+ }, 240_000);
+ }
+ });
+}
diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txt
index 306d1894d5..57a2b89093 100644
--- a/test/postgres-unit-arms.txt
+++ b/test/postgres-unit-arms.txt
@@ -65,6 +65,7 @@ test/embed-stale-images-managed-6529.test.ts
test/embedding-completion-atomic.serial.test.ts
test/engine-sql-facts-keyset.test.ts
test/extract-all-stamp-intersection.serial.test.ts
+test/extract-atoms-concurrent-run-7923.test.ts
test/extract-atoms-input-truncated-6503.test.ts
test/extract-atoms-raised-cap-drain-7391.test.ts
test/extract-endpoint-revision.serial.test.ts