Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
transcripts: delete stale parts through the coordinator on a managed brain (#5235)
garrytan25bcbddc0e1e
diff --git a/src/core/transcripts/ingest.ts b/src/core/transcripts/ingest.tsindex e677a17133..511b024cdb 100644--- a/src/core/transcripts/ingest.ts+++ b/src/core/transcripts/ingest.ts@@ -28,6 +28,7 @@*/import type { BrainEngine } from '../engine.ts';+import { randomUUID } from 'node:crypto';import { importFromContent } from '../import-file.ts';import type { OperationContext } from '../ops/contract.ts';import { managedPersistenceEnabled } from '../persistence/ownership.ts';@@ -431,7 +432,10 @@ export async function runTranscriptsIngest(const m = /^-p(\d+)$/.exec(suffix);const num = m ? Number(m[1]) : NaN;if (Number.isFinite(num) && num > rendered.parts.length) {- await engine.deletePage(row.slug, { sourceId: opts.sourceId });+ // #5235: on a managed brain the stale part goes through the coordinator like the parts+ // themselves, bound to its revision; the direct delete is refused there.+ if (coordinator) await deleteStaleTranscriptPart(coordinator, engine, opts.sourceId, row.slug);+ else await engine.deletePage(row.slug, { sourceId: opts.sourceId });result.partsDeleted++;}}@@ -489,6 +493,16 @@ async function submitTranscriptPart(return { slug, status: receipt.noop === true || receipt.status === 'skipped' ? 'skipped' : 'imported' };}+/** A stale higher-numbered part on a managed brain: a revision-bound `delete_page` write request (soft delete, recoverable). */+async function deleteStaleTranscriptPart(ctx: OperationContext, engine: BrainEngine, sourceId: string, slug: string): Promise<void> {+ const snapshot = await engine.readPageSnapshot(slug, { sourceId });+ if (!snapshot) return;+ const receipt = await submitPageMutation(ctx, { operation: 'delete_page', params: { slug, source_id: sourceId, expected_revision: snapshot.revision, request_id: randomUUID() } });+ if (receipt.state !== 'committed') {+ throw new Error(`stale part ${slug} was not deleted: the coordinator returned ${String(receipt.state)}${receipt.error_code ? ` (${String(receipt.error_code)})` : ''}`);+ }+}+/*** The slug embeds the title (and start day) for export formats, so a rename* renders a new slug for the same session. Import at the slug the session'sdiff --git a/test/transcripts-managed-ingest.test.ts b/test/transcripts-managed-ingest.test.tsindex da2bc0b37e..fd4136f9b6 100644--- a/test/transcripts-managed-ingest.test.ts+++ b/test/transcripts-managed-ingest.test.ts@@ -106,6 +106,24 @@ describe('transcripts ingest on a writer-claimed brain', () => {expect(await conversationPages()).toEqual([]);}, 120_000);+ test('a stale higher-numbered part is deleted through the coordinator as a revision-bound delete_page request (#5235)', async () => {+ // Unmanaged first import, so the base slug is known; the stale part is seeded directly while direct writes are still allowed.+ const first = await ingest(exportFile('a.json', false));+ expect(first.pages).toMatchObject({ imported: 1 });+ const [page] = await conversationPages();+ const stale = `${page!.slug}-p2`;+ await engine.putPage(stale, { type: 'note', title: 'Stale part', compiled_truth: 'left behind by a re-split', timeline: '', frontmatter: {} }, { sourceId: 'default' });+ await claimBrain();+ const again = await ingest(exportFile('b.json', false));+ expect(again).toMatchObject({ sessionsErrored: 0, erroredFiles: 0, partsDeleted: 1 });+ expect((await conversationPages()).map(row => row.slug)).toEqual([page!.slug]);+ const deletes = await engine.executeRaw<{ slug: string; state: string; principal_kind: string }>(+ `SELECT slug, state, principal_kind FROM persistence_requests WHERE operation = 'delete_page' ORDER BY sequence`);+ expect(deletes).toEqual([{ slug: stale, state: 'committed', principal_kind: 'local_cli' }]);+ const [row] = await engine.executeRaw<{ deleted: boolean }>('SELECT deleted_at IS NOT NULL AS deleted FROM pages WHERE slug=$1', [stale]);+ expect(row.deleted).toBe(true);+ }, 120_000);+test('a dry run on a managed brain plans without submitting anything', async () => {await claimBrain();const planned = await ingest(exportFile('a.json', false), { dryRun: true });