Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(ingest-capture): re-check the applied capture after the snapshot read (fix-check#1)

garrytan0370af7a747e
diff --git a/src/core/minions/handlers/ingest-capture.ts b/src/core/minions/handlers/ingest-capture.ts
index f9f8f454e4..9ea225ed5e 100644
--- a/src/core/minions/handlers/ingest-capture.ts
+++ b/src/core/minions/handlers/ingest-capture.ts
@@ -267,8 +267,8 @@ export function makeIngestCaptureHandler(engine: BrainEngine) {
// nothing, so it never reverts a newer edit. Tombstones and jobs queued without a key keep their old behavior.
const appliedKey = !isTombstone && job.idempotency_key ? job.idempotency_key : null;
const managedOutcome = (outcome: { noop?: boolean; chunks?: number }) => captured(outcome.noop ? 'skipped' : 'imported', Number(outcome.chunks ?? 0));
- const applied = appliedKey ? await readAppliedCapture<IngestCaptureResult>(engine, appliedKey,
- managed ? { sourceIds: [...new Set([sourceId ?? 'default', 'default'])], slug: slug.toLowerCase(), result: managedOutcome } : undefined) : null;
+ const journal = managed ? { sourceIds: [...new Set([sourceId ?? 'default', 'default'])], slug: slug.toLowerCase(), result: managedOutcome } : undefined;
+ const applied = appliedKey ? await readAppliedCapture<IngestCaptureResult>(engine, appliedKey, journal) : null;
if (applied) return applied;
const webhookWrite = untrustedPayload && event.source_kind === 'webhook' && !isTombstone;
@@ -330,10 +330,15 @@ export function makeIngestCaptureHandler(engine: BrainEngine) {
// GST-1856: each run of a keyed capture sends a new request_id under the capture's prefix, so the applied check above
// finds a committed run in the door's journal even when the worker died before recording it. The id is new per run
// because a stall re-run keeps attempts_made and `gbrain jobs retry` resets it: an id reused for an earlier request
- // that failed would replay that failure instead of writing.
+ // that failed would replay that failure instead of writing. Another run of the key can commit after the check above
+ // (a stalled original resuming after its re-run), so the check repeats after the snapshot read: any edit that
+ // snapshot holds then post-dates a committed run, and expected_revision refuses a run committing after it.
const requestId = appliedKey ? `${appliedCaptureRequestPrefix(appliedKey)}${job.attempts_made}:${randomUUID()}` : undefined;
+ let appliedSince: IngestCaptureResult | null = null;
const put = async (sid: string, request?: string) => {
const snapshot = await engine.readPageSnapshot(slug, { sourceId: sid, includeDeleted: true });
+ appliedSince = appliedKey ? await readAppliedCapture<IngestCaptureResult>(engine, appliedKey, journal) : null;
+ if (appliedSince) return {};
return door('put_page', sid, { content: event.content, content_origin: 'tool_output', source_kind: event.source_kind, source_uri: event.source_uri,
ingested_via: 'ingest_capture', ...(snapshot ? { expected_revision: snapshot.revision } : {}), ...(request ? { request_id: request } : {}),
// #2044/#4548: as `remote: untrustedPayload` does unmanaged, an untrusted body keeps the non-world facts rows and takes rows its sender could not see.
@@ -351,6 +356,7 @@ export function makeIngestCaptureHandler(engine: BrainEngine) {
await checkWriter('default');
response = await put('default', requestId && `${requestId}:default`);
}
+ if (appliedSince) return appliedSince;
if (sourceFallback) {
console.error(
`[WARN] ingest_capture: requested source '${sourceFallback.requested}' unavailable ` +
diff --git a/test/ingest-capture-applied-once.test.ts b/test/ingest-capture-applied-once.test.ts
index a8fdaa4f55..bd652585c6 100644
--- a/test/ingest-capture-applied-once.test.ts
+++ b/test/ingest-capture-applied-once.test.ts
@@ -81,6 +81,18 @@ const asWriter = (engine: BrainEngine, sql: string, params: unknown[]) => engine
await tx.executeRaw("SELECT set_config('gbrain.persistence_protocol', '2', true)");
await tx.executeRaw(sql, params);
});
+type CaptureResult = Awaited<ReturnType<ReturnType<typeof makeIngestCaptureHandler>>>;
+/** `engine` whose first readPageSnapshot (a managed capture's read just before its put) runs `meanwhile` first: the run stalls there. */
+const stallBeforeSnapshot = (engine: BrainEngine, meanwhile: () => Promise<unknown>) => {
+ let stalled = false;
+ return new Proxy(engine, { get(target, prop) {
+ const value = Reflect.get(target, prop, target);
+ if (typeof value !== 'function') return value;
+ if (prop !== 'readPageSnapshot' || stalled) return value.bind(target);
+ stalled = true;
+ return async (...args: unknown[]) => { await meanwhile(); return value.apply(target, args); };
+ } });
+};
const jsonData = (data: unknown) => (typeof data === 'string' ? JSON.parse(data) : data) as Record<string, unknown>;
const note = (title: string, line: string) => `---\ntype: note\ntitle: ${title}\n---\n# ${title}\n\n${line}\n`;
/** The data a webhook capture job carries for `content` sent to `slug` in `sourceId`. */
@@ -338,5 +350,36 @@ for (const backend of backends) {
const own = await engine.executeRaw<{ principal_kind: string }>("SELECT principal_kind FROM persistence_requests WHERE client_request_id LIKE $1 AND client_request_id <> $2", [`${prefix}%`, `${prefix}0:forged`]);
expect(own.map(r => r.principal_kind)).toEqual(['local_cli']);
}, { databaseUrl }), 120_000);
+
+ test('an original run that stalled before its put and resumes after the re-run committed and a newer edit landed keeps the newer edit', () => managedBrain(async ({ engine, ctx }) => {
+ const slug = 'notes/managed-stalled';
+ const data = captureData(note('Managed stalled', 'Managed stalled capture.'), slug, 'default');
+ const key = 'ingest:webhook:client-example:default:managed-stalled';
+ const pageText = async () => (await engine.readPageSnapshot(slug, { sourceId: 'default' }))?.page.compiled_truth ?? '';
+ let rerun: CaptureResult | undefined;
+ // The original passed the applied check and stalled; its lock expired, the stall re-run (same attempts_made)
+ // committed the capture, and a newer edit landed before the original went on.
+ const original = await makeIngestCaptureHandler(stallBeforeSnapshot(engine, async () => {
+ rerun = await makeIngestCaptureHandler(engine)(jobCtx(8, data, key));
+ const snapshot = await engine.readPageSnapshot(slug, { sourceId: 'default' });
+ await submitPageMutation(ctx, { operation: 'put_page', params: { slug, content: note('Managed stalled', 'Managed newer edit.'), expected_revision: snapshot!.revision } });
+ }))(jobCtx(8, data, key));
+ expect(rerun!.status).toBe('imported');
+ expect(original).toEqual(rerun!);
+ expect(await pageText()).toContain('Managed newer edit.');
+ expect(await pageText()).not.toContain('Managed stalled capture.');
+ }, { databaseUrl }), 120_000);
+
+ test('a re-run that the original run commits ahead of, after its applied check, returns the original result', () => managedBrain(async ({ engine }) => {
+ const slug = 'notes/managed-overtaken';
+ const data = captureData(note('Managed overtaken', 'Managed overtaken capture.'), slug, 'default');
+ const key = 'ingest:webhook:client-example:default:managed-overtaken';
+ let original: CaptureResult | undefined;
+ const rerun = await makeIngestCaptureHandler(stallBeforeSnapshot(engine, async () => {
+ original = await makeIngestCaptureHandler(engine)(jobCtx(9, data, key));
+ }))(jobCtx(9, data, key));
+ expect(original!.status).toBe('imported');
+ expect(rerun).toEqual(original!);
+ }, { databaseUrl }), 120_000);
});
}