Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(sync): #6712 PR 4b lane C: a drain re-enters sliced passes; a stopped slice's backlog counts the entries it settled
garrytan19e89c75feaf
diff --git a/src/core/persistence/sync-drain.ts b/src/core/persistence/sync-drain.tsindex edbc366250..41f9c70236 100644--- a/src/core/persistence/sync-drain.ts+++ b/src/core/persistence/sync-drain.ts@@ -533,7 +533,9 @@ export async function runDrain(input: DrainInput): Promise<SyncResult> {if ('deadline' in governed) return finish(last ?? NO_PASS_RESULT, 'resumable', 'deadline');result = governed.result;attempt = 0; capacity = null;- if (last?.runId && result.runId && result.runId !== last.runId) {+ // A cursor slice's counts are its run's so far; a pass result carries its own pass's (each resumed slice is a new pass_id).+ const run = (r: SyncResult | null) => r?.runId ?? r?.pass?.pass_id;+ if (run(last) && run(result) && run(result) !== run(last)) {const sum: RunCounts = carried ?? { added: 0, modified: 0, deleted: 0, renamed: 0, chunksCreated: 0 };carried = { added: sum.added + last.added, modified: sum.modified + last.modified, deleted: sum.deleted + last.deleted, renamed: sum.renamed + last.renamed, chunksCreated: sum.chunksCreated + last.chunksCreated };}diff --git a/src/core/persistence/sync-pass-run.ts b/src/core/persistence/sync-pass-run.tsindex 6f95d253d9..053812b908 100644--- a/src/core/persistence/sync-pass-run.ts+++ b/src/core/persistence/sync-pass-run.ts@@ -391,8 +391,8 @@ export async function runSyncPass(engine: BrainEngine, input: SyncPassInput): Prconst collisions: SyncSlugCollision[] = [];const S = new Map<string, string>();const settledKey = (path: string, level: string | null) => `${path}\0${level ?? ''}`;- // The entries this round's batches settled before the round folds its candidates into S: a slice that yields mid-round- // (writer_yield) reports them (yieldSettled); a blocked stop reports S as it replays.+ // The entries this round's batches settled before the round folds its candidates into S: a slice that stops mid-round+ // reports them (yieldSettled); a blocked stop reports S as it replays.let roundSettled = 0, yieldSettled = 0;const retryConsumed = new Set<string>(retry);const sliceBase = { started: performance.now(), pages: 0 };@@ -461,7 +461,13 @@ export async function runSyncPass(engine: BrainEngine, input: SyncPassInput): Prrenamed: counts.renamed + drained.renamed, chunks: counts.chunks + drained.chunksCreated, waivedImports: counts.waivedImports + (drained.waived?.imports ?? 0),waivedDeletes: counts.waivedDeletes + (drained.waived?.deletes ?? 0) });sliceBase.pages += drained.filesImported ?? 0;- if (drained.reason === 'writer_yield') yieldSettled = roundSettled + (drained.managedCursor?.index ?? 0);+ // A slice that stops mid-batch (a writer yield, a deadline, an abort) settled the entries its cursor passed: they count as+ // settled and leave the round's remaining; a blocked stop reports what its replay reports.+ if (drained.status !== 'blocked_by_failures') {+ const passed = drained.managedCursor?.index ?? 0;+ yieldSettled = roundSettled + passed;+ value.telemetry.remaining = Math.max(0, value.telemetry.remaining - passed);+ }const outcome: PassOutcome = drained.status === 'blocked_by_failures' ? 'blocked' : 'resumable';const blockedOn = drained.managedWrite?.path ? drained.managedWrite.write_request.request_id : null;if (outcome === 'blocked' && blockedOn && obligations.blocked?.request_id !== blockedOn) obligations.blocked = { request_id: blockedOn, pass_id: passId };diff --git a/test/managed-sync-drain.test.ts b/test/managed-sync-drain.test.tsindex bbf92453e9..a898588309 100644--- a/test/managed-sync-drain.test.ts+++ b/test/managed-sync-drain.test.ts@@ -67,13 +67,20 @@ test('a stopped drain is resumable, its backlog is readable from any process, anconst first = await runDrain({ signal: stop.signal, pass: (signal, onProgress) => performManagedSync(engine, { sourceId: f.id, noPull: true, noEmbed: true, signal, drainStartedAt,onProgress: e => { onProgress(e); if (e.phase === 'managed_sync.page_committed' && ++committed === 2) stop.abort(); } }, { maxPages: 1, maxMs: 60_000 }) });expect(first.drain).toMatchObject({ outcome: 'resumable', stop_reason: 'deadline' });+ // #6712 PR 4b (design v3.2 §3.7): a pass's backlog is its newest round (total = |C_k|, remaining = |C_k| - |S|); the rate and+ // ETA are null for a non-company source (§9: round-local progress lives in convergence).+ const imported = async () => (await engine.executeRaw<{ n: number }>('SELECT count(*)::int AS n FROM pages WHERE source_id=$1 AND deleted_at IS NULL', [f.id]))[0]!.n;+ const before = await imported();+ expect(before).toBeGreaterThan(0);const [backlog] = await readManagedSyncBacklog(engine, [f.id]);- expect(backlog).toMatchObject({ source_id: f.id, total: 6, resume_command: `gbrain sync --source ${f.id} --no-pull --no-embed` });- expect(backlog!.remaining).toBeGreaterThan(0);- expect(backlog!.rate_pages_per_min).not.toBeNull();+ expect(backlog).toMatchObject({ source_id: f.id, remaining: 6 - before, resume_command: `gbrain sync --source ${f.id} --no-pull --no-embed`,+ rate_pages_per_min: null, pass: { outcome: 'resumable', stop_reason: 'timeout' } });+ expect(backlog!.index + backlog!.remaining).toBe(backlog!.total);const second = await performSync(engine, { sourceId: f.id, noPull: true, noEmbed: true, drain: true });expect(second.drain?.outcome).toBe('synced');- expect(second.added).toBe(6);+ expect(second.added).toBe(6 - before);+ expect(await imported()).toBe(6);+ expect((await engine.executeRaw<{ n: number }>("SELECT count(*)::int AS n FROM persistence_requests WHERE source_id=$1 AND intent->>'kind'='managed_sync_import'", [f.id]))[0]!.n).toBe(6);expect(await readManagedSyncBacklog(engine, [f.id])).toEqual([]);}}), 180_000);