Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(doctor): managed brains get sync commands they accept (#5477)
time-attackc30e5d5e94a9
diff --git a/scripts/structural-suites.tsv b/scripts/structural-suites.tsvindex 9a74b0272f..883566b418 100644--- a/scripts/structural-suites.tsv+++ b/scripts/structural-suites.tsv@@ -122,7 +122,7 @@ test/doctor.test.ts stub_guard_24h check (v0.34.5) 5 doctor-source-helpertest/doctor.test.ts supervisor crash classifier wiring (v0.35.x) 4 doctor-source-helper,source-surface-helpertest/doctor.test.ts sync_freshness — clone-unavailable content-lag fallback 6 doctor-source-helpertest/doctor.test.ts v0.31.8 — wedge migration force-retry hint (D19) 4 doctor-source-helper-test/doctor.test.ts v0.32.4 — sync_freshness check 12 doctor-source-helper+test/doctor.test.ts v0.32.4 — sync_freshness check 13 doctor-source-helpertest/doctor.test.ts v0.40.4 — graph_signals_coverage check 7 doctor-source-helpertest/doctor.test.ts v0.41.27.0 — sync_freshness git short-circuit 9 doctor-source-helpertest/doctor.test.ts v0.41.32.0 — commit-relative staleness 5 doctor-source-helperdiff --git a/src/commands/doctor/checks/extraction-sync.ts b/src/commands/doctor/checks/extraction-sync.tsindex 1a9b81bb22..8697cfe796 100644--- a/src/commands/doctor/checks/extraction-sync.ts+++ b/src/commands/doctor/checks/extraction-sync.ts@@ -1462,7 +1462,7 @@ export async function checkSyncFreshness(return {name: 'sync_freshness',status: 'fail',- message: `${issues.join('; ')}. Run \`gbrain sync --source <id>\` for each stale source${inProgressNote}`,+ message: `${issues.join('; ')}. Run \`gbrain sync --source <id>${managed ? ' --no-pull' : ''}\` for each stale source${inProgressNote}`,details,};}@@ -1470,7 +1470,7 @@ export async function checkSyncFreshness(return {name: 'sync_freshness',status: 'warn',- message: `${issues.join('; ')}. Run \`gbrain sync --source <id>\` to refresh${inProgressNote}`,+ message: `${issues.join('; ')}. Run \`gbrain sync --source <id>${managed ? ' --no-pull' : ''}\` to refresh${inProgressNote}`,details,};}diff --git a/src/commands/doctor/checks/local-audits.ts b/src/commands/doctor/checks/local-audits.tsindex 455dbf3429..77427205ff 100644--- a/src/commands/doctor/checks/local-audits.ts+++ b/src/commands/doctor/checks/local-audits.ts@@ -536,7 +536,7 @@ async function runDefaultSourcePath(ctx: DoctorContext): Promise<Check[]> {engine!,nonDefaultWithPath.map(s => ({ id: s.id, local_path: s.local_path as string })),);- checks.push(multiSourceDriftCheck(result, nonDefaultWithPath.length, 'local'));+ checks.push(multiSourceDriftCheck(result, nonDefaultWithPath.length, 'local', await managedPersistenceEnabled(engine!).catch(() => false)));}} catch (e) {// A broken sources table must not stop doctor, but the check stilldiff --git a/src/commands/doctor/report-remote.ts b/src/commands/doctor/report-remote.tsindex 0349f7839f..1871f88c8d 100644--- a/src/commands/doctor/report-remote.ts+++ b/src/commands/doctor/report-remote.ts@@ -65,6 +65,7 @@ import {multiSourceDriftCheck,multiSourceDriftNotVerified,} from './schema-pack-checks.ts';+import { managedPersistenceEnabled } from '../../core/persistence/ownership.ts';import { brainScorePlanFix, checkError } from './check-fix.ts';import type { RenderContext } from '../../core/agent-output.ts';@@ -308,7 +309,7 @@ export async function doctorReportRemote(engine,nonDefaultWithPath.map(s => ({ id: s.id, local_path: s.local_path as string })),);- checks.push(multiSourceDriftCheck(result, nonDefaultWithPath.length, 'remote'));+ checks.push(multiSourceDriftCheck(result, nonDefaultWithPath.length, 'remote', await managedPersistenceEnabled(engine).catch(() => false)));}} catch (e) {// Best-effort, but the check reports that it verified nothing (#5432).diff --git a/src/commands/doctor/schema-pack-checks.ts b/src/commands/doctor/schema-pack-checks.tsindex 785fb8e864..56dc705942 100644--- a/src/commands/doctor/schema-pack-checks.ts+++ b/src/commands/doctor/schema-pack-checks.ts@@ -186,11 +186,13 @@ export async function checkSchemaPackSourceDrift(engine: BrainEngine): Promise<C* targets the ACTIVE source — following it literally on a multi-source* brain deletes the correctly-routed row).*/-export function multiSourceDriftAdvice(count: number, sampleStr: string): string {+export function multiSourceDriftAdvice(count: number, sampleStr: string, managed = false): string {// #4490: cause (3) + the --include-gitignored pointer must precede the// delete step — an operator whose file is simply not git-tracked would// otherwise re-sync (which imports nothing for that file) and then delete- // a row nothing will recreate.+ // a row nothing will recreate. A managed brain's sync refuses a git pull+ // and --include-gitignored, so its advice names `--no-pull` and drops the+ // ignored-file walk.return (`${count} page slug(s) appear at 'default' but NOT at the intended source ` +`(e.g., ${sampleStr}). Three possible causes: (1) pre-v0.30.3 putPage misroutes; ` +@@ -198,9 +200,11 @@ export function multiSourceDriftAdvice(count: number, sampleStr: string): string`(3) the file behind the slug is not git-tracked in the source repo — the sync walker ` +`reads through git objects, so a re-sync imports nothing for it. ` +`Verify with 'gbrain sources status', then re-sync with ` +- `'gbrain sync --source <id> --full' (reconciles drift without deleting data); ` +- `for cause (3), commit the file or use 'gbrain sync --source <id> --include-gitignored' ` +- `(full filesystem walk that also picks up ignored/untracked syncable files). ` ++ `'gbrain sync --source <id>${managed ? ' --no-pull' : ''} --full' (reconciles drift without deleting data); ` ++ (managed+ ? `for cause (3), commit the file (managed sync imports only committed files). `+ : `for cause (3), commit the file or use 'gbrain sync --source <id> --include-gitignored' ` ++ `(full filesystem walk that also picks up ignored/untracked syncable files). `) +`Only if a misrouted default-source row remains after that, remove it with ` +`'GBRAIN_SOURCE=default gbrain delete <slug> --force' — delete targets the active source, ` +`so pin it to 'default' explicitly (--force: page writes are revisioned, and a delete ` +@@ -236,6 +240,7 @@ export function multiSourceDriftCheck(result: MisroutedResult,candidateSources: number,host: 'local' | 'remote',+ managed = false,): Check {const details = {walk_truncated: result.walk_truncated,@@ -267,10 +272,10 @@ export function multiSourceDriftCheck(if (result.count > 0) {const sampleStr = result.sample.map((s) => `${s.slug} (intended=${s.intended_source})`).join(', ');const advice = host === 'local'- ? multiSourceDriftAdvice(result.count, sampleStr)+ ? multiSourceDriftAdvice(result.count, sampleStr, managed): `${result.count} page slug(s) appear at 'default' but NOT at the intended source ` +`(e.g., ${sampleStr}). Likely pre-v0.30.3 misroutes OR an incomplete initial sync. ` +- `Verify on the brain host: \`gbrain sources status\` then \`gbrain sync --source <id> --full\`.`;+ `Verify on the brain host: \`gbrain sources status\` then \`gbrain sync --source <id>${managed ? ' --no-pull' : ''} --full\`.`;return { name: 'multi_source_drift', status: 'warn', message: advice + skipNote + unreadableNote, details: { ...details, code: 'drift_detected' } };}if (unreadable.length > 0) {diff --git a/test/doctor-drift-advice.test.ts b/test/doctor-drift-advice.test.tsindex 27b97a0021..9179559c55 100644--- a/test/doctor-drift-advice.test.ts+++ b/test/doctor-drift-advice.test.ts@@ -53,4 +53,27 @@ describe('#1123 — multiSourceDriftAdvice references only real surfaces', () =>expect(flagIdx).toBeGreaterThan(-1);expect(deleteIdx).toBeGreaterThan(flagIdx);});++ // #5477: managed sync refuses a git pull and --include-gitignored, so the+ // managed advice names `--no-pull` and drops the ignored-file walk, while+ // the unmanaged advice stays byte-identical.+ test('managed advice names --no-pull and no --include-gitignored', () => {+ const managed = multiSourceDriftAdvice(45, 'foo (intended=wiki)', true);+ expect(managed).toContain("'gbrain sync --source <id> --no-pull --full'");+ expect(managed).not.toContain('--include-gitignored');+ expect(managed).toContain("'GBRAIN_SOURCE=default gbrain delete <slug> --force'");+ });++ test('unmanaged advice is byte-identical to the default', () => {+ expect(multiSourceDriftAdvice(45, 'foo (intended=wiki)', false)).toBe(advice);+ expect(advice).toBe(+ "45 page slug(s) appear at 'default' but NOT at the intended source (e.g., foo (intended=wiki)). " ++ 'Three possible causes: (1) pre-v0.30.3 putPage misroutes; (2) the intended source never completed initial sync and the default page is unrelated; ' ++ '(3) the file behind the slug is not git-tracked in the source repo — the sync walker reads through git objects, so a re-sync imports nothing for it. ' ++ "Verify with 'gbrain sources status', then re-sync with 'gbrain sync --source <id> --full' (reconciles drift without deleting data); " ++ "for cause (3), commit the file or use 'gbrain sync --source <id> --include-gitignored' (full filesystem walk that also picks up ignored/untracked syncable files). " ++ "Only if a misrouted default-source row remains after that, remove it with 'GBRAIN_SOURCE=default gbrain delete <slug> --force' — delete targets the active source, " ++ "so pin it to 'default' explicitly (--force: page writes are revisioned, and a delete naming neither --force nor --expected-revision is refused with revision_conflict).",+ );+ });});diff --git a/test/doctor-report-remote.serial.test.ts b/test/doctor-report-remote.serial.test.tsindex 704ca4251f..efda0cfaff 100644--- a/test/doctor-report-remote.serial.test.ts+++ b/test/doctor-report-remote.serial.test.ts@@ -8,7 +8,7 @@*/import { describe, test, expect, beforeAll, afterAll } from 'bun:test';-import { mkdtempSync, rmSync } from 'fs';+import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'fs';import { tmpdir } from 'os';import { join } from 'path';import { PGLiteEngine } from '../src/core/pglite-engine.ts';@@ -219,6 +219,37 @@ describe('doctorReportRemote — source scope (#4592)', () => {});});+describe('doctorReportRemote — managed drift advice (#5477)', () => {+ test('a managed brain is told --no-pull without --include-gitignored; unmanaged wording is unchanged', async () => {+ const root = mkdtempSync(join(tmpdir(), 'gbrain-doctor-drift-'));+ const sourceId = 'doctor-drift-advice-test';+ try {+ mkdirSync(join(root, 'notes'), { recursive: true });+ writeFileSync(join(root, 'notes', 'misrouted.md'), '# Misrouted\n');+ await engine.executeRaw('INSERT INTO sources (id, name, local_path) VALUES ($1, $1, $2) ON CONFLICT (id) DO UPDATE SET local_path = EXCLUDED.local_path', [sourceId, root]);+ await engine.putPage('notes/misrouted', { title: 'Misrouted', type: 'note', compiled_truth: '# Misrouted\n' });+ const drift = async () => (await doctorReportRemote(engine)).checks.find(c => c.name === 'multi_source_drift')!;++ const unmanaged = await drift();+ expect(unmanaged.message).toBe(+ "1 page slug(s) appear at 'default' but NOT at the intended source " ++ '(e.g., notes/misrouted (intended=doctor-drift-advice-test)). Likely pre-v0.30.3 misroutes OR an incomplete initial sync. ' ++ 'Verify on the brain host: `gbrain sources status` then `gbrain sync --source <id> --full`.',+ );++ await engine.executeRaw('UPDATE persistence_brain SET enabled = true WHERE singleton = 1');+ const managed = await drift();+ expect(managed.message).toContain('then `gbrain sync --source <id> --no-pull --full`.');+ expect(managed.message).not.toContain('--include-gitignored');+ } finally {+ await engine.executeRaw('UPDATE persistence_brain SET enabled = false WHERE singleton = 1');+ await engine.executeRaw("DELETE FROM pages WHERE slug = 'notes/misrouted' AND source_id = 'default'");+ await engine.executeRaw('DELETE FROM sources WHERE id = $1', [sourceId]);+ rmSync(root, { recursive: true, force: true });+ }+ });+});+describe('run_doctor canonical projection readiness', () => {test('the real handler excludes private and ungranted pending pages while local callers can diagnose them', async () => {await engine.executeRaw("INSERT INTO sources(id,name) VALUES ('readiness-visible','readiness-visible'),('readiness-hidden','readiness-hidden') ON CONFLICT DO NOTHING");diff --git a/test/doctor.test.ts b/test/doctor.test.tsindex b4eb864330..0aa98934fb 100644--- a/test/doctor.test.ts+++ b/test/doctor.test.ts@@ -824,8 +824,8 @@ describe('v0.31.8 — wedge migration force-retry hint (D19)', () => {describe('v0.32.4 — sync_freshness check', () => {// Stub engine: only checkSyncFreshness's executeRaw matters. Per-case rows// shape is `{id, name, local_path, last_sync_at}`.- function makeStubEngine(rows: any[]): any {- return { executeRaw: async () => rows };+ function makeStubEngine(rows: any[], managed = false): any {+ return { executeRaw: async (sql: string) => (sql.includes('FROM persistence_brain') ? [{ enabled: managed }] : rows) };}function agoMs(ms: number): Date {@@ -851,6 +851,16 @@ describe('v0.32.4 — sync_freshness check', () => {expect(result.message).toContain('gbrain sync --source <id>');});+ test('#5477: a managed brain is told to sync with --no-pull; unmanaged wording is unchanged', async () => {+ const { checkSyncFreshness } = await import('../src/commands/doctor.ts');+ const rows = [{ id: 'wiki', name: '', local_path: '/tmp/wiki', last_sync_at: null }];+ const unmanaged = await checkSyncFreshness(makeStubEngine(rows));+ expect(unmanaged.message).toBe("Source 'wiki' has never been synced. Run `gbrain sync --source <id>` for each stale source");+ const managed = await checkSyncFreshness(makeStubEngine(rows, true));+ expect(managed.status).toBe('fail');+ expect(managed.message).toBe("Source 'wiki' has never been synced. Run `gbrain sync --source <id> --no-pull` for each stale source");+ });+test('last_sync_at > 72h ago → fail with day-rounded "Nd ago"', async () => {const { checkSyncFreshness } = await import('../src/commands/doctor.ts');const result = await checkSyncFreshness(makeStubEngine([