Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(sync): check-ignore reads paths on stdin; tests for #7130 holds, caps and write-through
garrytan70455efa9b66
diff --git a/src/core/persistence/sync-origin-evidence.ts b/src/core/persistence/sync-origin-evidence.tsindex 186bbfd1f3..6f6f8ad89a 100644--- a/src/core/persistence/sync-origin-evidence.ts+++ b/src/core/persistence/sync-origin-evidence.ts@@ -8,7 +8,7 @@* Both reads are bounded Git plumbing over the candidate paths alone. A read that fails or times out answers `unknown`* for every path it covered, which the classifier treats like a missing commit (fail closed: hold, never delete).*/-import { execFile } from 'node:child_process';+import { execFile, spawn } from 'node:child_process';import { promisify } from 'node:util';import { gitChildEnv } from '../git-env.ts';@@ -22,9 +22,10 @@ const BATCH = 500;const GIT_TIMEOUT_MS = 120_000;const GIT_MAX_BUFFER = 64 * 1024 ** 2;-async function git(gitRoot: string, args: string[]): Promise<{ code: number; stdout: string }> {+/** `literal`: the paths are pathspecs read literally (`log`); `check-ignore` takes plain pathnames and refuses the magic. */+async function git(gitRoot: string, args: string[], literal = false): Promise<{ code: number; stdout: string }> {try {- const { stdout } = await execFileAsync('git', ['-c', 'core.quotepath=false', '--literal-pathspecs', '-C', gitRoot, ...args],+ const { stdout } = await execFileAsync('git', ['-c', 'core.quotepath=false', ...(literal ? ['--literal-pathspecs'] : []), '-C', gitRoot, ...args],{ env: gitChildEnv(), encoding: 'utf8', timeout: GIT_TIMEOUT_MS, maxBuffer: GIT_MAX_BUFFER });return { code: 0, stdout };} catch (error) {@@ -33,6 +34,22 @@ async function git(gitRoot: string, args: string[]): Promise<{ code: number; std}}+/** `git check-ignore --no-index -z --stdin` over `paths` (NUL-separated both ways, so any byte in a name survives). */+function checkIgnore(gitRoot: string, paths: readonly string[]): Promise<{ code: number; stdout: string }> {+ return new Promise(resolve => {+ const child = spawn('git', ['-c', 'core.quotepath=false', '-C', gitRoot, 'check-ignore', '--no-index', '-z', '--stdin'], { env: gitChildEnv(), stdio: ['pipe', 'pipe', 'ignore'] });+ const chunks: Buffer[] = [];+ let size = 0, settled = false;+ const done = (code: number) => { if (!settled) { settled = true; clearTimeout(timer); resolve({ code, stdout: Buffer.concat(chunks).toString('utf8') }); } };+ const timer = setTimeout(() => { child.kill('SIGKILL'); done(-1); }, GIT_TIMEOUT_MS);+ child.stdout.on('data', (chunk: Buffer) => { size += chunk.length; if (size > GIT_MAX_BUFFER) { child.kill('SIGKILL'); done(-1); } else chunks.push(chunk); });+ child.on('error', () => done(-1));+ child.on('close', code => done(code ?? -1));+ child.stdin.on('error', () => undefined);+ child.stdin.end(paths.map(path => `${path}\0`).join(''));+ });+}+/*** #7130 (write-through): whether the Git work tree around `root` ignores `relativePath` and does not track it, so a* file written there would never be committed. False outside a work tree or when Git cannot answer (the write keeps its@@ -53,9 +70,9 @@ export async function readOriginEvidence(gitRoot: string, commits: ReadonlyArrayconst paths = [...new Set(gitPaths)];for (let i = 0; i < paths.length; i += BATCH) {const batch = paths.slice(i, i + BATCH);- const touched = tips.length ? await git(gitRoot, ['log', ...tips, '--format=', '--name-only', '--no-renames', '-z', '--', ...batch]) : { code: 0, stdout: '' };+ const touched = tips.length ? await git(gitRoot, ['log', ...tips, '--format=', '--name-only', '--no-renames', '-z', '--', ...batch], true) : { code: 0, stdout: '' };const committed = new Set(touched.stdout.split('\0').map(path => path.trim()).filter(Boolean));- const ignoredRead = await git(gitRoot, ['check-ignore', '--no-index', '-z', '--', ...batch]);+ const ignoredRead = await checkIgnore(gitRoot, batch);const ignored = new Set(ignoredRead.stdout.split('\0').filter(Boolean));// check-ignore exits 1 when no path is ignored; anything else but 0 is a failed read.const ignoreKnown = ignoredRead.code === 0 || ignoredRead.code === 1;diff --git a/test/sync-uncommitted-origin-7130.test.ts b/test/sync-uncommitted-origin-7130.test.tsnew file mode 100644index 0000000000..4ed05cf890--- /dev/null+++ b/test/sync-uncommitted-origin-7130.test.ts@@ -0,0 +1,177 @@+/**+ * #7130 (reported by @liuwenyong1985): a managed sync soft-deleted thousands of pages whose recorded file was absent but+ * had never been committed (agent-written pages whose write-through file landed in a .gitignore'd directory), and the+ * 50% valve never tripped because they were 4% of a large source. On a real managed brain (PGLite, and Postgres with+ * DATABASE_URL), this protects:+ * - a live page whose file is absent and was never committed is held (`file_drift`, side deleted, reason+ * `never_committed`), never deleted, in committed mode, in working mode, under `--full`, and in the dry run's+ * discovery; one under a path .gitignore excludes is held with reason `ignored`; a committed then removed file still+ * deletes its page;+ * - `sync.max_deletes` (and its per-source override) withholds a pass below the 50% valve through the deletes_withheld+ * record, and status's next step for it is ask_user;+ * - put_page into a .gitignore'd directory writes no file and binds no origin (the page is database-only).+ */+import { describe, expect, test } from 'bun:test';+import { execFileSync } from 'node:child_process';+import { randomUUID } from 'node:crypto';+import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';+import { join } from 'node:path';+import type { BrainEngine } from '../src/core/engine.ts';+import type { OperationContext } from '../src/core/ops/contract.ts';+import { performManagedSync } from '../src/core/persistence/sync-run.ts';+import { discoverManagedSync } from '../src/core/persistence/sync-discovery.ts';+import { submitPageMutation } from '../src/core/persistence/page-mutations.ts';+import { readSyncStatus } from '../src/core/persistence/sync-status.ts';+import { SYNC_DELETES_WITHHELD_OP } from '../src/core/persistence/sync-pass-row.ts';+import { cliRenderContext, renderAction } from '../src/core/agent-output.ts';+import { DEFAULT_SYNC_MAX_DELETES, DEFAULT_SYNC_MAX_DELETE_RATIO, readSyncDeleteCaps, syncDeleteTrip } from '../src/core/sync-reconcile.ts';+import { gitIgnoresUntracked, readOriginEvidence } from '../src/core/persistence/sync-origin-evidence.ts';+import { managedBrain } from './helpers/managed-brain.ts';+import { requirePostgresTestDatabase, testBackends } from './helpers/test-backends.ts';+import { waitFor } from './helpers/wait-for.ts';+import { withEnv } from './helpers/with-env.ts';++const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();+const commit = (root: string, message: string) => { git(root, 'add', '-A'); git(root, '-c', 'user.name=Example', '-c', '[email protected]', 'commit', '-qm', message); };+const note = (title: string, body: string) => `---\ntype: concept\ntitle: ${title}\n---\n\n${body}\n`;+const OPTS = { sourceId: 'default', noPull: true, noEmbed: true, noExtract: true };+const setupNotes = (names: string[], gitignore = '') => ({ root }: { root: string }) => {+ mkdirSync(join(root, 'notes'), { recursive: true }); git(root, 'init', '-q');+ if (gitignore) writeFileSync(join(root, '.gitignore'), gitignore);+ for (const name of names) writeFileSync(join(root, `notes/${name}.md`), note(`notes/${name}`, `The committed body of notes/${name}.`));+ commit(root, 'notes');+};+const liveSlugs = async (engine: BrainEngine) => (await engine.executeRaw<{ slug: string }>("SELECT slug FROM pages WHERE source_id='default' AND deleted_at IS NULL ORDER BY slug")).map(row => row.slug);+async function holds(engine: BrainEngine) {+ const rows = await engine.executeRaw<{ fingerprint: string; record: { code: string; slug: string | null; message: string; meta: Record<string, unknown> } }>(+ "SELECT fingerprint, completed_keys->0 AS record FROM op_checkpoints WHERE op='sync-hold' ORDER BY fingerprint");+ return Object.fromEntries(rows.map(row => [row.fingerprint.split(':').slice(2).join(':'), row.record]));+}+/** Imports untracked files from the working tree (working mode), then returns to committed mode. */+async function importUntracked(engine: BrainEngine, root: string, files: Record<string, string>) {+ for (const [path, body] of Object.entries(files)) { mkdirSync(join(root, path, '..'), { recursive: true }); writeFileSync(join(root, path), note(path.replace(/\.md$/, ''), body)); }+ await engine.setConfig('sync.include_working_tree', 'true');+ expect((await performManagedSync(engine, OPTS)).status).toBe('synced');+ await engine.setConfig('sync.include_working_tree', 'false');+}++for (const backend of testBackends()) describe(`[${backend}] #7130: an absent file Git never committed is no deletion evidence`, () => {+ const databaseUrl = backend === 'postgres' ? requirePostgresTestDatabase() : undefined;++ test('never committed, ignored, and committed-then-removed: only the last deletes its page, in a pass and under --full; the dry run agrees', () => managedBrain(async ({ engine, root }) => {+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ await importUntracked(engine, root, { 'notes/draft.md': 'An untracked draft only the brain holds.', 'scratch/agent.md': 'An agent page written to scratch/.' });+ expect(await liveSlugs(engine)).toEqual(['notes/a', 'notes/b', 'notes/c', 'notes/draft', 'scratch/agent']);+ // scratch/ becomes ignored; the untracked files leave the disk; notes/c is removed in a commit.+ writeFileSync(join(root, '.gitignore'), 'scratch/\n');+ rmSync(join(root, 'notes/draft.md')); rmSync(join(root, 'scratch'), { recursive: true });+ git(root, 'rm', '-q', 'notes/c.md');+ commit(root, 'ignore scratch, remove c');++ const discovery = await discoverManagedSync(engine, { ...OPTS, full: true, dryRun: true } as never);+ expect(discovery.entries.filter(entry => entry.action === 'delete').map(entry => entry.sourcePath).sort()).toEqual(['notes/c.md']);+ expect([...discovery.uncommittedKept ?? []].sort()).toEqual(['notes/draft.md', 'scratch/agent.md']);++ expect((await performManagedSync(engine, OPTS)).status).toBe('synced');+ expect(await liveSlugs(engine)).toEqual(['notes/a', 'notes/b', 'notes/draft', 'scratch/agent']);+ const held = await holds(engine);+ expect(held['notes/draft.md']).toMatchObject({ code: 'file_drift', slug: 'notes/draft', meta: { side: 'deleted', state: 'file_gone', reason: 'never_committed', deleted: true } });+ expect(held['scratch/agent.md']).toMatchObject({ code: 'file_drift', slug: 'scratch/agent', meta: { side: 'deleted', state: 'file_gone', reason: 'ignored', deleted: true } });+ expect(held['notes/draft.md']!.message).toContain('no evidence');+ // Held at their level: the next tick and a full re-walk delete nothing.+ expect((await performManagedSync(engine, OPTS)).status).toBe('up_to_date');+ await performManagedSync(engine, { ...OPTS, full: true });+ expect(await liveSlugs(engine)).toEqual(['notes/a', 'notes/b', 'notes/draft', 'scratch/agent']);+ }, { databaseUrl, setup: setupNotes(['a', 'b', 'c']) }), 240_000);++ test('working mode: an untracked file removed from the disk holds its page instead of deleting it', () => managedBrain(async ({ engine, root }) => {+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ await engine.setConfig('sync.include_working_tree', 'true');+ writeFileSync(join(root, 'notes/w.md'), note('notes/w', 'An untracked working-tree note.'));+ expect((await performManagedSync(engine, OPTS)).status).toBe('synced');+ rmSync(join(root, 'notes/w.md'));+ await performManagedSync(engine, OPTS);+ expect(await liveSlugs(engine)).toContain('notes/w');+ expect((await holds(engine))['notes/w.md']).toMatchObject({ code: 'file_drift', meta: { reason: 'never_committed' } });+ }, { databaseUrl, setup: setupNotes(['a']) }), 240_000);++ const names = Array.from({ length: 24 }, (_, i) => `n${String(i).padStart(2, '0')}`);+ test('sync.max_deletes withholds a pass below the 50% valve; next is ask_user; a source override and the env valve apply it', () => managedBrain(async ({ engine, root }) => {+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ await engine.setConfig('sync.max_deletes', '3');+ for (const name of names.slice(0, 4)) git(root, 'rm', '-q', `notes/${name}.md`);+ commit(root, 'remove four of 24');++ expect((await performManagedSync(engine, OPTS)).status).toBe('synced');+ expect((await liveSlugs(engine)).length).toBe(24);+ const [withheld] = await engine.executeRaw<{ value: { count: number; trip: string; caps: { maxDeletes: number } } }>('SELECT completed_keys->0 AS value FROM op_checkpoints WHERE op=$1', [SYNC_DELETES_WITHHELD_OP]);+ expect(withheld!.value).toMatchObject({ count: 4, trip: 'max_deletes', caps: { maxDeletes: 3 } });+ const status = await readSyncStatus(engine, 'default', { detail: false });+ expect({ state: status.state, reason: status.reason }).toEqual({ state: 'blocked', reason: 'deletes_withheld' });+ expect(renderAction(status.next!, cliRenderContext()).next).toBe('ask_user');+ expect(status.next!.user_message).toContain('sync.max_deletes');++ await engine.setConfig('sync.max_deletes.source.default', '10');+ expect((await performManagedSync(engine, { ...OPTS, full: true })).status).toBe('synced');+ expect((await liveSlugs(engine)).length).toBe(20);+ expect(await engine.executeRaw('SELECT 1 FROM op_checkpoints WHERE op=$1', [SYNC_DELETES_WITHHELD_OP])).toHaveLength(0);++ await engine.setConfig('sync.max_deletes.source.default', '1');+ for (const name of names.slice(4, 6)) git(root, 'rm', '-q', `notes/${name}.md`);+ commit(root, 'remove two more');+ await performManagedSync(engine, OPTS);+ expect((await liveSlugs(engine)).length).toBe(20);+ await withEnv({ GBRAIN_ALLOW_MASS_RECONCILE: '1' }, async () => { await performManagedSync(engine, OPTS); });+ expect((await liveSlugs(engine)).length).toBe(18);+ }, { databaseUrl, setup: setupNotes(names) }), 240_000);++ test('put_page into a .gitignore\'d directory writes no file and binds no origin; sync never deletes it', () => managedBrain(async ({ engine, ctx, root }) => {+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ const requestId = randomUUID();+ await submitPageMutation(ctx as OperationContext, { operation: 'put_page', params: { slug: 'ingested/report', request_id: requestId, content: note('ingested/report', 'An agent-written report.') } });+ await waitFor(async () => (await engine.executeRaw<{ state: string }>('SELECT state FROM persistence_requests WHERE request_id=$1::uuid', [requestId]))[0]?.state === 'committed',+ { timeoutMs: 20_000, label: 'put_page commits' });+ expect(existsSync(join(root, 'ingested/report.md'))).toBe(false);+ const [page] = await engine.executeRaw<{ source_path: string | null }>("SELECT source_path FROM pages WHERE source_id='default' AND slug='ingested/report' AND deleted_at IS NULL");+ expect(page).toEqual({ source_path: null });+ await performManagedSync(engine, { ...OPTS, full: true });+ expect(await liveSlugs(engine)).toContain('ingested/report');+ }, { databaseUrl, setup: setupNotes(['a'], 'ingested/\n') }), 240_000);+});++describe('#7130: origin evidence', () => {+ test('committed, never committed and ignored paths; a read that fails answers unknown (the classifier holds it)', async () => {+ const { mkdtempSync } = await import('node:fs');+ const { tmpdir } = await import('node:os');+ const root = mkdtempSync(join(tmpdir(), 'gbrain-7130-evidence-'));+ try {+ git(root, 'init', '-q'); mkdirSync(join(root, 'notes'));+ writeFileSync(join(root, '.gitignore'), 'scratch/\n'); writeFileSync(join(root, 'notes/gone "quoted" é.md'), 'x');+ commit(root, 'one'); git(root, 'rm', '-q', 'notes/gone "quoted" é.md'); commit(root, 'two');+ const head = git(root, 'rev-parse', 'HEAD');+ const evidence = await readOriginEvidence(root, [head, null], ['notes/gone "quoted" é.md', 'notes/never.md', 'scratch/agent.md']);+ expect(Object.fromEntries(evidence)).toEqual({ 'notes/gone "quoted" é.md': 'committed', 'notes/never.md': 'never_committed', 'scratch/agent.md': 'ignored' });+ expect(Object.fromEntries(await readOriginEvidence(join(root, 'missing'), [head], ['notes/never.md']))).toEqual({ 'notes/never.md': 'unknown' });+ expect(await gitIgnoresUntracked(root, 'scratch/agent.md')).toBe(true);+ expect(await gitIgnoresUntracked(root, 'notes/never.md')).toBe(false);+ } finally { rmSync(root, { recursive: true, force: true }); }+ });+});++describe('#7130: delete caps', () => {+ test('defaults and trips', async () => {+ expect({ max: DEFAULT_SYNC_MAX_DELETES, ratio: DEFAULT_SYNC_MAX_DELETE_RATIO }).toEqual({ max: 500, ratio: 0.01 });+ const caps = { maxDeletes: 500, maxDeleteRatio: 0.01 };+ expect(syncDeleteTrip(0, 478_000, caps)).toBeNull();+ expect(syncDeleteTrip(19_325, 478_000, caps)).toBe('max_deletes');+ expect(syncDeleteTrip(400, 30_000, caps)).toBe('max_delete_ratio');+ expect(syncDeleteTrip(50, 2_000, caps)).toBeNull();+ expect(syncDeleteTrip(51, 2_000, caps)).toBe('max_delete_ratio');+ expect(syncDeleteTrip(14, 24, caps)).toBe('mass_ratio');+ expect(syncDeleteTrip(3, 24, caps)).toBeNull();+ const config = new Map<string, string>([['sync.max_deletes', '40'], ['sync.max_delete_ratio', 'nonsense'], ['sync.max_deletes.source.other', '7']]);+ const engine = { getConfig: async (key: string) => config.get(key) ?? null };+ expect(await readSyncDeleteCaps(engine, 'default')).toEqual({ maxDeletes: 40, maxDeleteRatio: 0.01 });+ expect(await readSyncDeleteCaps(engine, 'other')).toEqual({ maxDeletes: 7, maxDeleteRatio: 0.01 });+ });+});