Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

wip(#6712 PR 4a): doctor managed_sync_convergence counts (deep tier)

garrytanc7722b7c7be5
diff --git a/src/commands/doctor/checks/managed-sync-convergence.ts b/src/commands/doctor/checks/managed-sync-convergence.ts
new file mode 100644
index 0000000000..e32d3d7615
--- /dev/null
+++ b/src/commands/doctor/checks/managed-sync-convergence.ts
@@ -0,0 +1,47 @@
+/**
+ * managed_sync_convergence (#6712 PR 4a): the read-only shadow of the level-triggered managed-sync pass, as counts. For
+ * every managed source this host owns it reads the level (sync-pass.ts) and reports the per-class counts, the predicted
+ * decisions, `legacy_unverified`, `would_hold` and the delete valve's verdict (sync-convergence.ts). It is
+ * informational in PR 4a: always `ok`, never a warning, and it writes nothing. A full level read is heavy, so the
+ * check runs in the `deep` tier (`gbrain doctor --tier deep --json`, or `--only managed_sync_convergence`).
+ */
+import type { Check } from '../../doctor.ts';
+import { connectedEngine, type DoctorContext, type DoctorEntry } from '../context.ts';
+import { readManagedMode } from '../../../core/persistence/ownership.ts';
+import { statusConvergence } from '../../../core/persistence/sync-pass.ts';
+import { checkError } from '../check-fix.ts';
+import { localHostId } from '../../../core/persistence/identity.ts';
+
+async function runManagedSyncConvergenceCheck(ctx: DoctorContext): Promise<Check[]> {
+ const checks: Check[] = [];
+ try {
+ const engine = connectedEngine(ctx);
+ if (!await readManagedMode(engine)) {
+ checks.push({ name: 'managed_sync_convergence', status: 'ok', message: 'Not a managed brain; there is no managed sync to shadow.', details: { sources: [] } });
+ return checks;
+ }
+ const sources = await engine.executeRaw<{ id: string }>(`SELECT s.id FROM sources s JOIN persistence_source_bindings b ON b.source_id=s.id
+ JOIN persistence_worktrees w ON w.id=b.worktree_id WHERE NOT s.archived AND s.config->>'kind' IS NULL AND w.owner_host_id=$1::uuid ORDER BY s.id`, [localHostId()]);
+ const rows = [];
+ for (const { id } of sources) {
+ const receipt = await statusConvergence(engine, id);
+ if (!receipt) continue;
+ rows.push('status' in receipt ? { source_id: id, status: 'unavailable', code: receipt.code }
+ : { source_id: id, status: 'read', mode: receipt.mode, target: receipt.target, candidates: receipt.candidates, in_flight: receipt.in_flight, counts: receipt.counts,
+ decisions: receipt.decisions, legacy_unverified: receipt.legacy_unverified, would_hold: receipt.would_hold, deletes: { count: receipt.deletes.count, verdict: receipt.deletes.verdict },
+ duration_ms: receipt.duration_ms });
+ }
+ const read = rows.filter(row => row.status === 'read') as Array<{ source_id: string; candidates: number; would_hold: number }>;
+ checks.push({ name: 'managed_sync_convergence', status: 'ok', severity: 'info', details: { dry_run: true, sources: rows },
+ message: rows.length
+ ? `Convergence shadow (read-only): ${read.map(row => `${row.source_id} ${row.candidates} unsettled, ${row.would_hold} would hold`).join('; ') || 'no source could be read'}`
+ + `${rows.length > read.length ? `; ${rows.length - read.length} source(s) not readable on this host` : ''}. Detail per source: gbrain sync --dry-run --json --source <id>.`
+ : 'No managed source is owned here.' });
+ } catch (error) {
+ checks.push(checkError('managed_sync_convergence', 'read the convergence shadow', error, { details: { dry_run: true } }));
+ }
+ return checks;
+}
+
+/** A full level read per source: the deep tier only. */
+export const managedSyncConvergenceEntry: DoctorEntry = { name: 'managed_sync_convergence', emits: ['managed_sync_convergence'], tier: 'deep', run: runManagedSyncConvergenceCheck };
diff --git a/src/commands/doctor/registry.ts b/src/commands/doctor/registry.ts
index 75cf57568b..a32c407c4e 100644
--- a/src/commands/doctor/registry.ts
+++ b/src/commands/doctor/registry.ts
@@ -119,6 +119,7 @@ import { syncConfigKeysEntry } from './checks/sync-config-keys.ts';
import { ghostPagesEntry } from './checks/ghost-pages.ts';
import { missingRecordedFilesEntry } from './checks/missing-recorded-files.ts';
import { managedSyncMovementEntry } from './checks/managed-sync-movement.ts';
+import { managedSyncConvergenceEntry } from './checks/managed-sync-convergence.ts';
import { managedSyncBlockedEntry } from './checks/managed-sync-blocked.ts';
import { atomHeldFailedEntry } from './checks/atom-holds.ts';
import { extractionInputTruncatedEntry } from './checks/extraction-input.ts';
@@ -239,6 +240,8 @@ export const DOCTOR_CHECK_REGISTRY: readonly DoctorEntry[] = [
missingRecordedFilesEntry,
syncConfigKeysEntry,
fenceIntegrityEntry,
+ // #6712 PR 4a: the read-only convergence shadow (deep tier: a full level read per source).
+ managedSyncConvergenceEntry,
searchModeEntry,
];
diff --git a/src/core/doctor-categories.ts b/src/core/doctor-categories.ts
index 9e486f5988..1d08de891b 100644
--- a/src/core/doctor-categories.ts
+++ b/src/core/doctor-categories.ts
@@ -291,6 +291,8 @@ export const OPS_CHECK_NAMES: ReadonlySet<string> = new Set([
'managed_sync_not_moving',
// #6858: a managed sync run stopped on a page that did not commit, failed past an hour (doctor/checks/managed-sync-blocked.ts).
'managed_sync_blocked',
+ // #6712 PR 4a: the read-only convergence shadow's counts (doctor/checks/managed-sync-convergence.ts).
+ 'managed_sync_convergence',
'persistence_session_timeouts',
// #6317: consumer heartbeat rows and host identity (doctor/checks/persistence-consumers.ts).
'two_consumers_on_host',
diff --git a/test/doctor-managed-sync-convergence.test.ts b/test/doctor-managed-sync-convergence.test.ts
new file mode 100644
index 0000000000..a42213252f
--- /dev/null
+++ b/test/doctor-managed-sync-convergence.test.ts
@@ -0,0 +1,36 @@
+/**
+ * #6712 PR 4a: doctor's managed_sync_convergence counts (deep tier).
+ *
+ * Authoring gate: (1) protects doctor's read-only convergence counts per owned managed source; (2) a check that warns,
+ * writes, or misses a source's unsettled paths fails here; (3) the shadow tests cover the receipt, not the doctor
+ * surface; (4) no seam.
+ */
+import { expect, test } from 'bun:test';
+import { execFileSync } from 'node:child_process';
+import { writeFileSync } from 'node:fs';
+import { join } from 'node:path';
+import type { DoctorContext } from '../src/commands/doctor/context.ts';
+import { managedSyncConvergenceEntry } from '../src/commands/doctor/checks/managed-sync-convergence.ts';
+import { performManagedSync } from '../src/core/persistence/sync-run.ts';
+import { managedBrain } from './helpers/managed-brain.ts';
+
+const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
+const commit = (root: string) => { git(root, 'add', '.'); git(root, '-c', 'user.name=Example', '-c', '[email protected]', 'commit', '-qm', 'notes'); };
+
+test('managed_sync_convergence reports each owned source\'s shadow counts, informational only', () => managedBrain(async ({ engine, root }) => {
+ expect((await performManagedSync(engine, { sourceId: 'default', noPull: true, noEmbed: true, noExtract: true })).status).toBe('first_sync');
+ writeFileSync(join(root, 'one.md'), '---\ntitle: One\n---\n\nSynthetic one, edited.\n');
+ commit(root);
+ expect(managedSyncConvergenceEntry.tier).toBe('deep');
+ const checks = await managedSyncConvergenceEntry.run({ engine, args: [] } as unknown as DoctorContext);
+ expect(Array.isArray(checks)).toBe(true);
+ const [check] = checks as Array<{ name: string; status: string; message: string; details: { dry_run: boolean; sources: Array<Record<string, unknown>> } }>;
+ expect(check).toMatchObject({ name: 'managed_sync_convergence', status: 'ok', details: { dry_run: true } });
+ expect(check!.details.sources).toEqual([expect.objectContaining({ source_id: 'default', status: 'read', candidates: 1, counts: { matched: 1, import: 1 }, would_hold: 0 })]);
+ expect(check!.message).toContain('default 1 unsettled, 0 would hold');
+}, { setup: ({ root }) => {
+ git(root, 'init', '-q');
+ writeFileSync(join(root, 'one.md'), '---\ntitle: One\n---\n\nSynthetic one.\n');
+ writeFileSync(join(root, 'two.md'), '---\ntitle: Two\n---\n\nSynthetic two.\n');
+ commit(root);
+} }), 120_000);
diff --git a/test/fixtures/goldens/doctor/registry-build-checks.json b/test/fixtures/goldens/doctor/registry-build-checks.json
index 9a28d5b82a..fec0e56f2d 100644
--- a/test/fixtures/goldens/doctor/registry-build-checks.json
+++ b/test/fixtures/goldens/doctor/registry-build-checks.json
@@ -857,6 +857,10 @@
"category": "brain",
"name": "fence_integrity"
},
+ {
+ "category": "ops",
+ "name": "managed_sync_convergence"
+ },
{
"category": "ops",
"name": "search_mode"