Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(transcripts): replay a committed part receipt only while its page is still live, so a purged page is re-created (GBRA-130 A-2)
garrytan1270b11bae3a
diff --git a/src/core/transcripts/ingest.ts b/src/core/transcripts/ingest.tsindex 3949a7234b..608b8a5ddf 100644--- a/src/core/transcripts/ingest.ts+++ b/src/core/transcripts/ingest.ts@@ -542,9 +542,12 @@ const PART_ATTEMPT_PAGE = 8;* One part's write keeps one request identity across runs. The id is derived* from the source, slug, expected revision and content, so a rerun while that* write is still queued or publishing gets it back (`prior`) and waits on it;- * a committed one is replayed. A write that ended without committing- * (conflict, failed, cancelled) is retried under the next attempt's id, so a- * cleared cause re-queues the part instead of replaying its old refusal.+ * a committed one is replayed only while the live page is still the one it+ * produced (same source incarnation, same revision). A committed write whose+ * page has since been deleted and purged, or rewritten, is passed over like a+ * write that ended without committing (conflict, failed, cancelled): the part+ * is submitted under the next attempt's id, so a cleared cause or an absent+ * page gets a fresh, normally admitted write instead of an old receipt.*/async function transcriptPartRequest(ctx: OperationContext, sourceId: string, slug: string, params: Record<string, unknown>):Promise<{ requestId: string; prior: WriteRequest | null }> {@@ -559,7 +562,10 @@ async function transcriptPartRequest(ctx: OperationContext, sourceId: string, slconst byId = new Map(rows.map(row => [String(row.request_id).toLowerCase(), row]));for (const requestId of names) {const prior = byId.get(clientRequestUuid(requestId)) ?? null;- if (!prior || !isTerminal(prior) || prior.state === 'committed') return { requestId, prior };+ if (!prior || !isTerminal(prior)) return { requestId, prior };+ if (prior.state !== 'committed') continue;+ const live = await ctx.engine.readPageSnapshot(slug, { sourceId });+ if (live?.sourceIncarnation === prior.source_incarnation && live.revision === prior.outcome?.revision) return { requestId, prior };}}}diff --git a/test/pathological/a2-transcripts-ingest.test.ts b/test/pathological/a2-transcripts-ingest.test.tsindex 5ce74086b1..5f1dcd456f 100644--- a/test/pathological/a2-transcripts-ingest.test.ts+++ b/test/pathological/a2-transcripts-ingest.test.ts@@ -6,10 +6,14 @@* its typed code while the other sessions land; an accepted write that* outlasts the wait is a pending session (never a failure, never synced);* a rerun waits on that write instead of admitting a duplicate; a pending- * write that ends without committing is re-queued; the connector watermark- * never passes a pending or failed conversation.+ * write that ends without committing is re-queued; a page deleted and+ * retention-purged since its write is created again instead of answered+ * with the old committed receipt, while an owner purge still refuses it+ * purged_content; the connector watermark never passes a pending or+ * failed conversation.* 2. Regression: the per-part catch treating anything but /too large|invalid- * byte sequence/ as a run abort, or a random request id per run.+ * byte sequence/ as a run abort, a random request id per run, or a+ * committed receipt replayed for a page that no longer exists.* 3. Existing coverage stubs the ingest result (connectors-ingest-failure) or* pins the regex seam (transcripts-nul); none drives a real coordinator* refusal or a real accepted write that commits after the wait.@@ -27,6 +31,8 @@ import { saveCredential } from '../../src/core/connectors/credentials.ts';import { connectorSourceKey } from '../../src/core/connectors/config-keys.ts';import { CHATGPT_BASE_URL } from '../../src/core/connectors/providers/chatgpt.ts';import { waitForWrites } from '../../src/core/persistence/service.ts';+import { purgeDeletedPagesCoordinated } from '../../src/core/persistence/purge-deleted.ts';+import { operations } from '../../src/core/operations.ts';import type { WriteRequest } from '../../src/core/persistence/model.ts';import { chatgptHandler, newFixtureState, startFixture, type FixtureConversation, type FixtureState } from '../fixtures/connectors/fixture-server.ts';import { expectItemFailure, expectOthersUnchanged, expectUnitNotAborted, managedBrain, pathologicalArms, runScopedErrors, snapshotItems } from './harness.ts';@@ -124,6 +130,41 @@ for (const { backend, databaseUrl } of pathologicalArms()) {expect((await putPageRequests(engine)).map(row => row.state)).toEqual(['committed', 'conflict', 'committed']);}, { databaseUrl }), 180_000);+ test('recovery: an identical re-ingest after the page was deleted and retention-purged admits a new creation instead of replaying the old receipt', async () => managedBrain(async ({ engine, ctx, root }) => {+ const path = exportFile(root, { 'c-1': 'one' });+ expect(await ingest(engine, ctx, path)).toMatchObject({ sessionsImported: 1, sessionsErrored: 0 });+ const { slug } = await slugOf(engine, 'c-1');+ const snapshot = await engine.readPageSnapshot(slug, { sourceId: 'default' });+ await operations.find(op => op.name === 'delete_page')!.handler(ctx, { slug, expected_revision: snapshot!.revision, request_id: crypto.randomUUID() });+ expect((await purgeDeletedPagesCoordinated(engine, 0)).slugs).toEqual([slug]);+ expect(await engine.readPageSnapshot(slug, { sourceId: 'default', includeDeleted: true })).toBeNull();+ const again = await expectUnitNotAborted(() => ingest(engine, ctx, path));+ expect(again).toMatchObject({ sessionsImported: 1, sessionsErrored: 0, sessionsPending: 0, cleanScan: true });+ expect((await slugOf(engine, 'c-1')).slug).toBe(slug);+ expect((await putPageRequests(engine)).map(row => row.state)).toEqual(['committed', 'committed']);+ const rerun = await ingest(engine, ctx, path);+ expect(rerun).toMatchObject({ sessionsImported: 1, sessionsErrored: 0, pages: { imported: 0, skipped: 1 } });+ expect((await putPageRequests(engine)).length).toBe(2);+ }, { databaseUrl }), 180_000);++ test('item: an identical re-ingest after the owner purged the page is refused purged_content, never replayed or re-created', async () => managedBrain(async ({ engine, ctx, root }) => {+ const path = exportFile(root, { 'c-1': 'one' });+ await ingest(engine, ctx, path);+ const { slug } = await slugOf(engine, 'c-1');+ const snapshot = await engine.readPageSnapshot(slug, { sourceId: 'default' });+ await operations.find(op => op.name === 'delete_page')!.handler(ctx, { slug, purge: true, expected_revision: snapshot!.revision, request_id: crypto.randomUUID() });+ await purgeDeletedPagesCoordinated(engine, 0);+ const again = await expectUnitNotAborted(() => ingest(engine, ctx, path));+ const c1 = sessions(again).get('c-1')!;+ expectItemFailure({ key: c1.sessionId, code: c1.code }, { key: 'c-1', code: 'purged_content' });+ expect(again).toMatchObject({ sessionsErrored: 1, sessionsImported: 0 });+ expect((await putPageRequests(engine)).map(row => [row.state, row.error_code])).toEqual([['committed', null], ['failed', 'purged_content']]);+ expect(await engine.readPageSnapshot(slug, { sourceId: 'default', includeDeleted: true })).toBeNull();+ const rerun = await ingest(engine, ctx, path);+ expect(sessions(rerun).get('c-1')?.code).toBe('purged_content');+ expect(await engine.readPageSnapshot(slug, { sourceId: 'default', includeDeleted: true })).toBeNull();+ }, { databaseUrl }), 180_000);+for (const injected of runScopedErrors()) {test(`run: a run-scoped failure inside a session stops the ingest (${injected.name})`, async () => managedBrain(async ({ engine, ctx, root }) => {const path = exportFile(root, { 'c-1': 'one', 'c-2': 'two', 'c-3': 'three' });