Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(#6858): a resumed run is not blocked; only a sync import's --keep disk names the plain sync first (review)
garrytaned6afcea4725
diff --git a/src/core/persistence/recovery-resolve.ts b/src/core/persistence/recovery-resolve.tsindex 5617be38d2..159844d8a1 100644--- a/src/core/persistence/recovery-resolve.ts+++ b/src/core/persistence/recovery-resolve.ts@@ -312,10 +312,11 @@ async function abandonWrite(engine: BrainEngine, row: WriteRequest, previewHash:const sameIdentity = (row: WriteRequest | null, item: StuckRecovery) => !!row?.recovery && row.recovery.version === 1&& digest(identityOf(recoveryFiles(row.recovery), row.recovery)) === digest(item.identity);-function nextStep(item: StuckRecovery, keep: KeepChoice, state: string): string {+function nextStep(item: StuckRecovery, keep: KeepChoice, request: WriteRequest): string {+ const state = request.state;const sync = `gbrain sync --source ${item.source_id} --no-pull`;// #6858: a managed sync import is the next sync's to finish: it retries the page after the manifest end (sync-page-fault.ts fileChangedAfterAdmission).- if (keep === 'disk' && item.operation === 'submit_job') return `The bytes on disk are kept and the sync import's publication was abandoned (request ${state}). This command does not mark the page repaired. `+ if (keep === 'disk' && request.intent?.kind === 'managed_sync_import') return `The bytes on disk are kept and the sync import's publication was abandoned (request ${state}). This command does not mark the page repaired. `+ `Next: ${sync}. That sync retries ${item.slug} after the rest of its run and imports the kept file when it is committed at the run's target; an uncommitted edit is held (worktree_dirty) and the rest of the source syncs. `+ `If a hold remains, gbrain sync status --source ${item.source_id} --json names it and its exit (commit the file, or gbrain sources reconcile ${item.source_id} ${item.slug} --brain host --preview).`;if (keep === 'disk') return `The bytes on disk are kept and the write's publication was abandoned (request ${state}); the database page still holds the content before the write, `@@ -400,7 +401,7 @@ Promise<ResolutionResult & { replayed: boolean }> {const result: ResolutionResult = { outcome: blocked ? 'still_blocked' : 'resolved', keep: opts.keep, request_state: recovered.state,blocked_reason: blocked ? recovered.blocked_reason ?? null : null, finished_by: ours ? 'this_run' : 'another_pass', backup_dir: journal.backup_dir,next: blocked ? `The recovery is still blocked (${recovered.blocked_reason ?? 'recovering'}); rerun the same apply, or inspect it with gbrain sources writer status --source ${item.source_id} --json.`- : nextStep(item, opts.keep, recovered.state) };+ : nextStep(item, opts.keep, recovered) };if (!blocked) await writeJournal(engine, key, { ...journal, phase: 'done', result });return { ...result, replayed: false };} finally { await lock.release(); }diff --git a/src/core/persistence/sync-blocked.ts b/src/core/persistence/sync-blocked.tsindex 30ac3d5dd5..e66e6c1831 100644--- a/src/core/persistence/sync-blocked.ts+++ b/src/core/persistence/sync-blocked.ts@@ -2,8 +2,9 @@* #6858 ask 3: the managed sync runs that are stopped, not draining. An unfinished cursor whose stop is recorded (a* `managed-sync-failure` row under its key) or whose pending page receipt ended without committing resumes nothing by* itself; doctor `managed_sync_blocked` and get_brain_health read it. An unfinished cursor waiting on a queued or- * running request (`sync_incomplete`) is draining and not listed, and a failure whose cursor is gone is an orphan- * (`gbrain repair managed-sync-orphans`), not a blocked run.+ * running request (`sync_incomplete`) is draining and not listed, and neither is a cursor saved after its recorded stop:+ * the failure row stays until the whole run finishes, so a resumed run that is moving again would otherwise still read+ * as blocked. A failure whose cursor is gone is an orphan (`gbrain repair managed-sync-orphans`), not a blocked run.*/import type { BrainEngine } from '../engine.ts';import { readManagedSyncFailures } from './sync-failures.ts';@@ -14,8 +15,9 @@ export const MANAGED_SYNC_BLOCKED_FAIL_MS = 60 * 60_000;export interface BlockedManagedSync { source_id: string; code: string; run_id: string; blocked_since: string }export async function readBlockedManagedSyncs(engine: BrainEngine, sourceIds?: string[]): Promise<BlockedManagedSync[]> {- const cursors = new Set((await engine.executeRaw<{ fingerprint: string }>(- "SELECT fingerprint FROM op_checkpoints WHERE op='managed-sync' AND COALESCE(completed_keys->0->>'done','false')<>'true'")).map(row => row.fingerprint));+ const cursors = new Set((await engine.executeRaw<{ fingerprint: string }>(`+ SELECT c.fingerprint FROM op_checkpoints c LEFT JOIN op_checkpoints f ON f.op='managed-sync-failure' AND f.fingerprint=c.fingerprint+ WHERE c.op='managed-sync' AND COALESCE(c.completed_keys->0->>'done','false')<>'true' AND (f.updated_at IS NULL OR f.updated_at>=c.updated_at)`)).map(row => row.fingerprint));return (await readManagedSyncFailures(engine, sourceIds)).filter(failure => failure.code !== 'sync_incomplete' && cursors.has(failure.cursor_key)).map(failure => ({ source_id: failure.source_id, code: failure.code, run_id: failure.run_id, blocked_since: failure.first_seen }));diff --git a/test/managed-sync-blocked-job-6858.test.ts b/test/managed-sync-blocked-job-6858.test.tsindex e29e41c8eb..ee30c84d3c 100644--- a/test/managed-sync-blocked-job-6858.test.ts+++ b/test/managed-sync-blocked-job-6858.test.ts@@ -110,6 +110,16 @@ test('#6858: a freshness sync job whose managed run is blocked ends dead with thexpect(failed!.message).toContain(`${s.id} (source_changed, blocked 1`);const stale = await readBrainHealthBands(engine, { sourceIds: [s.id] }, Date.now() + 2_000_000 + nth);expect(stale).toMatchObject({ status: 'attention', sync_blocked: { band: '1-9' } });++ // A resumed run saves its cursor after the recorded stop while the failure row stays until the run finishes: it is+ // draining, not blocked. A stop recorded again after that save is blocked again.+ const [{ key }] = await engine.executeRaw<{ key: string }>("SELECT fingerprint AS key FROM op_checkpoints WHERE op='managed-sync-failure' AND completed_keys->0->>'source_id'=$1", [s.id]);+ await engine.executeRaw("UPDATE op_checkpoints SET updated_at=now()+interval '1 minute' WHERE op='managed-sync' AND fingerprint=$1", [key]);+ expect(await readBlockedManagedSyncs(engine, [s.id])).toEqual([]);+ expect(await check()).toEqual([expect.objectContaining({ name: 'managed_sync_blocked', status: 'ok' })]);+ expect((await readBrainHealthBands(engine, { sourceIds: [s.id] }, Date.now() + 3_000_000 + nth)).sync_blocked.band).toBe('none');+ await engine.executeRaw("UPDATE op_checkpoints SET updated_at=now()+interval '2 minutes' WHERE op='managed-sync-failure' AND fingerprint=$1", [key]);+ expect(await readBlockedManagedSyncs(engine, [s.id])).toEqual([expect.objectContaining({ source_id: s.id, code: 'source_changed' })]);nth++;await disposePersistenceConsumer(engine);}diff --git a/test/repair-stuck-recovery.test.ts b/test/repair-stuck-recovery.test.tsindex a81882566a..3e8219df21 100644--- a/test/repair-stuck-recovery.test.ts+++ b/test/repair-stuck-recovery.test.ts@@ -152,6 +152,18 @@ describe('gbrain repair stuck-recovery', () => {expect(readFileSync(join(String(result.outcome_items![0]!.detail!.backup_dir), '0.prior'), 'utf8')).toBe(PRIOR);}));+ test('#6858: --keep disk on a maintenance submit_job (not a sync import) still hands off to reconcile first', () => run(async () => {+ const s = await stuck({ prior: PRIOR, after: AFTER, disk: EDITED });+ await engine.executeRaw(`UPDATE persistence_requests SET operation='submit_job', intent=intent || '{"kind":"managed_maintenance_page"}'::jsonb WHERE id=$1`, [s.id]);+ const preview = await repair(s.sourceId);+ const result = await repair(s.sourceId, { apply: true, expect: preview.preview_hash, keep: 'disk' });+ expect(result.outcomes).toEqual({ resolved: 1 });+ const next = String(result.outcome_items![0]!.detail!.next);+ expect(next).not.toContain("sync import's publication");+ expect(next).toContain(`gbrain sources reconcile ${s.sourceId} page --brain host --preview --auto-additive`);+ expect(next.indexOf('--auto-additive')).toBeLessThan(next.indexOf(`gbrain sync --source ${s.sourceId} --no-pull`));+ }));+test('absent prior (the write was a create): the preview says so, and --keep prior unlinks the edited file after backing it up', () => run(async () => {const s = await stuck({ prior: null, after: AFTER, disk: EDITED });const out = await cli(['stuck-recovery', '--source', s.sourceId]);