Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

sources: managed removal drops managed-sync cursors; backlog matches incarnation

garrytan2d0841810873
diff --git a/src/core/persistence/source-lifecycle.ts b/src/core/persistence/source-lifecycle.ts
index e8ccb10596..c8796861b0 100644
--- a/src/core/persistence/source-lifecycle.ts
+++ b/src/core/persistence/source-lifecycle.ts
@@ -281,6 +281,7 @@ export async function runManagedSourceLifecycle(engine:BrainEngine,input:SourceL
pagesDeleted=Number(impact.count);
await tx.executeRaw('DELETE FROM sources WHERE id=$1 AND incarnation=$2::uuid',[input.sourceId,incarnation]);
await tx.executeRaw('DELETE FROM persistence_source_bindings WHERE source_id=$1 AND source_incarnation=$2::uuid',[input.sourceId,incarnation]);
+ await tx.executeRaw("DELETE FROM op_checkpoints WHERE op='managed-sync' AND completed_keys->0->>'sourceId'=$1",[input.sourceId]);
}
const contentReceipts=input.operation==='rebind'?await rebindContentReceipts(tx,worktrees,hostIdFor(tx),relocated?undefined:[input.sourceId]):[];
await advanceTopology(tx,worktrees);
diff --git a/src/core/persistence/sync-drain.ts b/src/core/persistence/sync-drain.ts
index 62852f24e8..88efddb023 100644
--- a/src/core/persistence/sync-drain.ts
+++ b/src/core/persistence/sync-drain.ts
@@ -1236,7 +1236,8 @@ export interface ManagedSyncBacklog {
/**
* Unfinished managed cursors of registered sources with their remaining
* entries and the rate of their latest drain window (CEO-A18), readable from
- * any process. A cursor left by a removed source (#8741) is not a backlog.
+ * any process. A cursor left by a removed source (#8741), or recorded for an
+ * earlier incarnation of a source re-created under the same id, is not a backlog.
* #6712 PR 4b (design v3.2 §3.7): plus every source whose pass row is not
* `nothing_pending` (`passHealth`: converged at the checkout's HEAD and the
* newest pass ended `synced` or `up_to_date`); a source's pass entry replaces
@@ -1246,7 +1247,8 @@ export async function readManagedSyncBacklog(engine: BrainEngine, sourceIds?: st
const rows = await engine.executeRaw<{ header: { sourceId: string; index: number; total: number; done?: boolean; discoveredAt?: string; progress?: { startedAt: number; startIndex: number; lastAt: number; lastIndex: number };
counts?: { held?: number }; processingOptions?: { noEmbed?: boolean; noExtract?: boolean; noSchemaPack?: boolean }; syncOptions?: Parameters<typeof managedSyncResumeArgs>[0]['syncOptions'] } }>(
`SELECT completed_keys->0 AS header FROM op_checkpoints WHERE op='managed-sync' AND COALESCE(completed_keys->0->>'done','false')<>'true'
- AND EXISTS (SELECT 1 FROM sources s WHERE s.id = completed_keys->0->>'sourceId')`);
+ AND EXISTS (SELECT 1 FROM sources s WHERE s.id = completed_keys->0->>'sourceId'
+ AND s.incarnation::text = COALESCE(completed_keys->0->>'incarnation', s.incarnation::text))`);
const { readPassReadings, passResumeArgs, passStuck, passStuckSince } = await import('./sync-pass-readers.ts');
const passes: ManagedSyncBacklog[] = (await readPassReadings(engine, { sourceIds })).filter(r => !r.health.nothing_pending).map(({ source_id, row, head, health }) => {
const t = row.value.telemetry, args = passResumeArgs(source_id, row), stuck = passStuck(health) ? health.reason as 'blocked' | 'not_converged' | 'settle_mismatch' : null;
diff --git a/test/source-delete-managed-cursor-8741.test.ts b/test/source-delete-managed-cursor-8741.test.ts
index de6b1412da..28549e8c06 100644
--- a/test/source-delete-managed-cursor-8741.test.ts
+++ b/test/source-delete-managed-cursor-8741.test.ts
@@ -3,22 +3,35 @@
* left its unfinished managed-sync cursor behind. Doctor managed_sync_backlog
* then warned forever with a resume command that fails `unknown_source`, and
* the managed-sync-orphans repair (which joins sources) never saw the row.
- * The cursor now goes with the source row, and the backlog reader skips a
- * cursor whose source no longer exists.
+ * The cursor now goes with the source row on both removal paths
+ * (deleteSourceRow and the managed lifecycle remove), and the backlog reader
+ * skips a cursor whose source no longer exists or whose recorded incarnation
+ * is not the live source's.
*/
import { afterAll, beforeAll, expect, test } from 'bun:test';
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { randomUUID } from 'node:crypto';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { checkManagedSyncBacklog } from '../src/commands/doctor/checks/managed-sync-backlog.ts';
import { deleteSourceRow } from '../src/core/source-delete.ts';
+import { registerLocalWriter } from '../src/core/persistence/identity.ts';
+import { claimWorktree, getWorktreeBinding } from '../src/core/persistence/ownership.ts';
+import { runManagedSourceLifecycle } from '../src/core/persistence/source-lifecycle.ts';
+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
+import { withEnv } from './helpers/with-env.ts';
let engine: PGLiteEngine;
const twoHoursAgo = () => new Date(Date.now() - 120 * 60_000).toISOString();
-const putCursor = (fingerprint: string, sourceId: string) => engine.executeRaw(
+const putCursor = (fingerprint: string, sourceId: string, incarnation?: string) => engine.executeRaw(
"INSERT INTO op_checkpoints(op, fingerprint, completed_keys, updated_at) VALUES('managed-sync', $1, jsonb_build_array($2::jsonb), now())",
- [fingerprint, JSON.stringify({ sourceId, index: 1, total: 4, done: false, discoveredAt: twoHoursAgo() })]);
+ [fingerprint, JSON.stringify({ sourceId, ...(incarnation ? { incarnation } : {}), index: 1, total: 4, done: false, discoveredAt: twoHoursAgo() })]);
+const incarnationOf = async (id: string) => (await engine.executeRaw<{ incarnation: string }>('SELECT incarnation::text FROM sources WHERE id = $1', [id]))[0]!.incarnation;
+const cursorCount = async (fingerprint: string) => (await engine.executeRaw("SELECT 1 FROM op_checkpoints WHERE op = 'managed-sync' AND fingerprint = $1", [fingerprint])).length;
beforeAll(async () => { engine = new PGLiteEngine(); await engine.connect({}); await engine.initSchema(); }, 60_000);
-afterAll(async () => { await engine.disconnect(); });
+afterAll(async () => { await disposePersistenceConsumer(engine); await engine.disconnect(); });
test('removing a source drops its unfinished managed-sync cursor', async () => {
await engine.executeRaw("INSERT INTO sources (id, name) VALUES ('source-a', 'source-a'), ('source-b', 'source-b')");
@@ -36,3 +49,47 @@ test('a cursor already left behind by a removed source is not a backlog', async
expect(await checkManagedSyncBacklog(engine, ['source-gone'])).toBeNull();
expect((await checkManagedSyncBacklog(engine))?.message ?? '').not.toContain('source-gone');
});
+
+test('a cursor of an earlier incarnation of a re-created source is not a backlog', async () => {
+ await engine.executeRaw("INSERT INTO sources (id, name) VALUES ('source-c', 'source-c')");
+ await putCursor('cursor-c-old', 'source-c', await incarnationOf('source-c'));
+ expect(await checkManagedSyncBacklog(engine, ['source-c'])).toMatchObject({ status: 'warn' });
+ await engine.executeRaw("DELETE FROM sources WHERE id = 'source-c'");
+ await engine.executeRaw("INSERT INTO sources (id, name) VALUES ('source-c', 'source-c')");
+ expect(await cursorCount('cursor-c-old')).toBe(1);
+ expect(await checkManagedSyncBacklog(engine, ['source-c'])).toBeNull();
+ await putCursor('cursor-c-new', 'source-c', await incarnationOf('source-c'));
+ expect(await checkManagedSyncBacklog(engine, ['source-c'])).toMatchObject({ status: 'warn' });
+});
+
+test('deleteSourceRow then re-creating the same id leaves no backlog', async () => {
+ await engine.executeRaw("INSERT INTO sources (id, name) VALUES ('source-d', 'source-d')");
+ await putCursor('cursor-d', 'source-d', await incarnationOf('source-d'));
+ expect(await deleteSourceRow(engine, 'source-d')).toBe(true);
+ await engine.executeRaw("INSERT INTO sources (id, name) VALUES ('source-d', 'source-d')");
+ expect(await cursorCount('cursor-d')).toBe(0);
+ expect(await checkManagedSyncBacklog(engine, ['source-d'])).toBeNull();
+});
+
+test('managed source removal drops its cursors, and re-adding the same id leaves no backlog', async () => {
+ const home = mkdtempSync(join(tmpdir(), 'gbrain-8741-'));
+ try {
+ await withEnv({ GBRAIN_HOME: home, DATABASE_URL: undefined, GBRAIN_DATABASE_URL: undefined }, async () => {
+ const source = 'source-managed', root = join(home, 'canonical');
+ mkdirSync(root);
+ writeFileSync(join(root, 'example.md'), '---\ntitle: Example\ntype: note\n---\n\nCanonical\n');
+ await registerLocalWriter(engine, 'cli');
+ await engine.executeRaw('INSERT INTO sources(id, name, local_path) VALUES($1, $1, $2)', [source, root]);
+ await claimWorktree(engine, source, root);
+ await engine.executeRaw('UPDATE persistence_brain SET enabled=true WHERE singleton=1');
+ const original = (await getWorktreeBinding(engine, source))!;
+ await putCursor('cursor-managed', source, original.source_incarnation);
+ expect(await checkManagedSyncBacklog(engine, [source])).toMatchObject({ status: 'warn' });
+ await runManagedSourceLifecycle(engine, { operation: 'remove', sourceId: source, confirmDestructive: true, requestId: randomUUID() });
+ expect(await cursorCount('cursor-managed')).toBe(0);
+ await runManagedSourceLifecycle(engine, { operation: 'add', sourceId: source, path: root });
+ expect((await getWorktreeBinding(engine, source))!.source_incarnation).not.toBe(original.source_incarnation);
+ expect(await checkManagedSyncBacklog(engine, [source])).toBeNull();
+ });
+ } finally { rmSync(home, { recursive: true, force: true }); }
+}, 60_000);