Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
#6712 PR 1: a lost manifest re-discovers against HEAD; forced probes for the prescreen and drift guard
garrytan0307869fad56
diff --git a/src/core/persistence/sync-run.ts b/src/core/persistence/sync-run.tsindex c84b9a8a14..95fae43cf7 100644--- a/src/core/persistence/sync-run.ts+++ b/src/core/persistence/sync-run.ts@@ -1472,14 +1472,10 @@ async function runManagedSync(engine: BrainEngine, opts: SyncOpts, slice: { maxPassertActive();try { cursor = await readCursor(engine, key); }catch (error) {- if (!(error instanceof MissingSyncManifest) || !opts.retryFailed || opts.dryRun || company) throw error;- missingManifestCursor = error.cursor;- assertActive();- phase = 'discovery';- discoveryTarget = syncGit(context.gitRoot, ['rev-parse', 'HEAD']).trim();- const discovery = await discoverManagedSync(engine, opts, context);- assertActive();- const replaced = await replaceCursor(engine, key, error.cursor, { ...discovery, authority, processingOptions, syncOptions, runId: discoveryRun, index: 0, counts: { added: 0, modified: 0, deleted: 0, chunks: 0 } }, assertActive);+ if (!(error instanceof MissingSyncManifest) || opts.dryRun || company) throw error;+ missingManifestCursor = error.cursor; phase = 'discovery'; discoveryTarget = syncGit(context.gitRoot, ['rev-parse', 'HEAD']).trim();+ // #6712 PR 1 (L1): a lost manifest re-discovers against HEAD; the prescreen passes what the lost run imported, so the old cursor is a skip-ahead.+ const replaced = await replaceCursor(engine, key, error.cursor, { ...await discoverManagedSync(engine, opts, context), authority, processingOptions, syncOptions, runId: discoveryRun, index: 0, counts: { added: 0, modified: 0, deleted: 0, chunks: 0 } }, assertActive);if ('wait' in replaced) return { ...result(error.cursor, 'partial', 'writer_pending'), ...(authority.writer.remote ? {} : { retryWait: replaced.wait }) };cursor = replaced;}diff --git a/test/persistence-sync-failures.serial.test.ts b/test/persistence-sync-failures.serial.test.tsindex 1f835cff3f..8785f07212 100644--- a/test/persistence-sync-failures.serial.test.ts+++ b/test/persistence-sync-failures.serial.test.ts@@ -464,18 +464,19 @@ test('remote pending sync keeps its private receipt off the response while resum}}), 120_000);-test('missing manifest is a durable nonfile blocker and only explicit idle retry rediscovers it', async () => withEnv(env, async () => {+// #6712 PR 1 (L1): a lost manifest no longer blocks until an explicit idle retry. The next plain sync re-discovers against+// HEAD and finishes under a new run; the page the lost run already imported is prescreened (the old cursor is a skip-ahead).+test('missing manifest re-discovers against HEAD on the next sync; the page the lost run imported is prescreened', async () => withEnv(env, async () => {for (const engine of engines) {const f = await fixture(engine, { 'a.md': 'First committed observation.\n', 'b.md': 'Second committed observation.\n' });const options = { sourceId: f.id, noPull: true };const partial = await performManagedSync(engine, options, { maxPages: 1, maxMs: 1000 });await engine.executeRaw("DELETE FROM op_checkpoints WHERE op='managed-sync-manifest' AND fingerprint=$1", [partial.runId]);- await expect(performManagedSync(engine, options)).rejects.toMatchObject({ code: 'storage_error' });- expect(await readManagedSyncFailures(engine, [f.id])).toEqual([expect.objectContaining({ phase: 'resume', path: '<resume>', run_id: partial.runId, target: f.head })]);- expect((await engine.executeRaw<{ last_commit: string | null }>('SELECT last_commit FROM sources WHERE id=$1', [f.id]))[0].last_commit).toBeNull();- const repaired = await performManagedSync(engine, { ...options, retryFailed: true });- expect(repaired.status).toBe('first_sync'); expect(repaired.runId).not.toBe(partial.runId);+ const resumed = await performManagedSync(engine, options);+ expect(resumed.status).toBe('first_sync'); expect(resumed.runId).not.toBe(partial.runId);+ expect(resumed.prescreened).toBe(1);expect(await readManagedSyncFailures(engine, [f.id])).toHaveLength(0);+ expect((await engine.executeRaw<{ last_commit: string | null }>('SELECT last_commit FROM sources WHERE id=$1', [f.id]))[0].last_commit).toBe(f.head);}}), 120_000);diff --git a/test/sync-prescreen-drift-6712.test.ts b/test/sync-prescreen-drift-6712.test.tsnew file mode 100644index 0000000000..eac8ad75de--- /dev/null+++ b/test/sync-prescreen-drift-6712.test.ts@@ -0,0 +1,192 @@+/**+ * #6712 PR 1 (L1): the content-addressed discovery prescreen, the `file_drift` drift guard and the re-discovery of a+ * lost manifest against HEAD.+ *+ * Protects:+ * - a page whose import provenance matches the pinned blob, the frozen revision and this parser is prescreened in+ * discovery and passed by the guarded no-op kernel: no request, no preparation, no new revision;+ * - a fingerprint change, `--full`, a byte change and a revision change each fall through to the ordinary path (the+ * prescreen fails toward importing);+ * - a database-only edit to a page whose file did not change is held `file_drift` and not imported; under a+ * `storage.keep_db` prefix it is neither held nor imported (the keep_db worker gets it as a drift candidate);+ * - cold upgrade: a page with missing, partial (pre-PR 0) or obsolete-fingerprint provenance and a database-only edit+ * that predates discovery is held, in a tree walk and under `--full`, instead of being overwritten by the file; the+ * equal-content controls (no edit, an unattributed revision whose content equals the file, a changed file on a page+ * sync wrote last) still skip or import as before;+ * - a cursor whose manifest is gone re-discovers against HEAD and finishes, the pages the lost run imported prescreened.+ *+ * Each test fails on the base: there `prescreened` is absent, the tree walk overwrites the database edit, and the+ * missing manifest stops the run with `storage_error`.+ * PGLite always; Postgres too when DATABASE_URL is set.+ */+import { describe, expect, test } from 'bun:test';+import { execFileSync } from 'node:child_process';+import { randomUUID } from 'node:crypto';+import { mkdirSync, 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 { submitPageMutation } from '../src/core/persistence/page-mutations.ts';+import { readGitHold, readSyncImportProvenance, SYNC_IMPORT_PARSER_VERSION, SYNC_IMPORT_PROVENANCE_OP } from '../src/core/persistence/sync-holds.ts';+import { readDriftSkips } from '../src/core/persistence/drift-conflicts.ts';+import { managedBrain } from './helpers/managed-brain.ts';+import { waitFor } from './helpers/wait-for.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 = (slug: string, body: string) => `---\ntype: concept\ntitle: ${slug}\n---\n\n${body}\n`;+const SLUGS = ['notes/a', 'notes/b', 'notes/c', 'notes/d', 'notes/e', 'notes/f'];+const OPTS = { sourceId: 'default', noPull: true, noEmbed: true, noExtract: true };+/** A history rewrite (HEAD's tree as a new root commit, then gc) makes the checkpoint unreachable, so the next sync walks the whole tree at HEAD. */+function rewriteHistory(root: string): void {+ const rewritten = git(root, '-c', 'user.name=Example', '-c', '[email protected]', 'commit-tree', 'HEAD^{tree}', '-m', `rewritten ${randomUUID().slice(0, 8)}`);+ git(root, 'reset', '-q', '--soft', rewritten);+ git(root, 'reflog', 'expire', '--expire=now', '--all'); git(root, 'gc', '-q', '--prune=now');+}+const setup = ({ root }: { root: string }) => {+ mkdirSync(join(root, 'notes'), { recursive: true }); git(root, 'init', '-q');+ for (const slug of SLUGS) writeFileSync(join(root, `${slug}.md`), note(slug, `The committed body of ${slug}.`));+ commit(root, 'notes');+};+const incarnationOf = async (engine: BrainEngine) => (await engine.executeRaw<{ incarnation: string }>("SELECT incarnation::text FROM sources WHERE id='default'"))[0]!.incarnation;+const pageIdOf = async (engine: BrainEngine, slug: string) => Number((await engine.readPageSnapshot(slug, { sourceId: 'default' }))!.page.id);+const bodyOf = async (engine: BrainEngine, slug: string) => (await engine.getPage(slug, { sourceId: 'default' }))?.compiled_truth ?? '';+const revisionOf = async (engine: BrainEngine, slug: string) => (await engine.readPageSnapshot(slug, { sourceId: 'default' }))!.revision;+const importRequests = async (engine: BrainEngine) => Number((await engine.executeRaw<{ n: number | string }>(+ "SELECT count(*) AS n FROM persistence_requests WHERE intent->>'kind'='managed_sync_import'"))[0]!.n);+/** Rewrites one page's provenance record in place (`set` is a jsonb path → value list), or deletes it. */+async function tamper(engine: BrainEngine, slug: string, change: Array<[string, unknown]> | 'delete' | { drop: string[] }): Promise<void> {+ const fingerprint = `default:${await incarnationOf(engine)}:${await pageIdOf(engine, slug)}`;+ if (change === 'delete') { await engine.executeRaw('DELETE FROM op_checkpoints WHERE op=$1 AND fingerprint=$2', [SYNC_IMPORT_PROVENANCE_OP, fingerprint]); return; }+ if ('drop' in change) {+ for (const key of change.drop) await engine.executeRaw(`UPDATE op_checkpoints SET completed_keys=jsonb_build_array((completed_keys->0)-$3::text) WHERE op=$1 AND fingerprint=$2`, [SYNC_IMPORT_PROVENANCE_OP, fingerprint, key]);+ return;+ }+ for (const [path, value] of change) await engine.executeRaw(`UPDATE op_checkpoints SET completed_keys=jsonb_set(completed_keys,$3::text[],$4::text::jsonb) WHERE op=$1 AND fingerprint=$2`,+ [SYNC_IMPORT_PROVENANCE_OP, fingerprint, `{0,${path}}`, JSON.stringify(value)]);+}+/** A database-only `put_page` (write-through off): the page moves, its file does not. */+async function databaseOnlyEdit(engine: BrainEngine, ctx: OperationContext, slug: string, body: string): Promise<string> {+ const requestId = randomUUID();+ const current = await engine.readPageSnapshot(slug, { sourceId: 'default' });+ await submitPageMutation(ctx, { operation: 'put_page', params: { slug, request_id: requestId, expected_revision: current!.revision, content: note(slug, body) } });+ 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: `the database-only put_page of ${slug} commits` });+ return requestId;+}+const heldOf = (result: Awaited<ReturnType<typeof performManagedSync>>) => (result.held ?? []).map(hold => ({ path: hold.path, code: hold.code })).sort((a, b) => a.path.localeCompare(b.path));++for (const databaseUrl of process.env.DATABASE_URL ? [undefined, process.env.DATABASE_URL] : [undefined]) describe(`#6712 PR 1 prescreen and drift guard (${databaseUrl ? 'postgres' : 'pglite'})`, () => {+ test('a provenance match is prescreened and passed by the kernel: no request, no preparation, no new revision', () => managedBrain(async ({ engine, root }) => {+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ const revisions = new Map(await Promise.all(SLUGS.map(async slug => [slug, await revisionOf(engine, slug)] as const)));+ const requests = await importRequests(engine);+ rewriteHistory(root);+ const walk = await performManagedSync(engine, OPTS);+ expect(walk).toMatchObject({ status: 'synced', prescreened: SLUGS.length, waived: { imports: SLUGS.length, deletes: 0 }, added: 0, modified: 0 });+ expect(await importRequests(engine)).toBe(requests);+ for (const slug of SLUGS) expect(await revisionOf(engine, slug)).toBe(revisions.get(slug)!);+ }, { databaseUrl, setup }), 180_000);++ test('a fingerprint change, a revision change, a byte change and --full each fall through to the ordinary path', () => managedBrain(async ({ engine, root }) => {+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ await tamper(engine, 'notes/a', [['parser,parser_version', 'an-older-parser']]);+ await tamper(engine, 'notes/b', [['revision', '1']]);+ writeFileSync(join(root, 'notes/c.md'), note('notes/c', 'A changed body of notes/c.')); commit(root, 'change c');+ rewriteHistory(root);+ const walk = await performManagedSync(engine, OPTS);+ // d, e and f are prescreened; a and b are screened (and waived) by the ordinary path; c is imported.+ expect(walk).toMatchObject({ status: 'synced', prescreened: 3, waived: { imports: 5 } });+ expect(heldOf(walk)).toEqual([]);+ expect(await bodyOf(engine, 'notes/c')).toContain('A changed body of notes/c.');+ const incarnation = await incarnationOf(engine);+ expect((await readSyncImportProvenance(engine, 'default', incarnation, await pageIdOf(engine, 'notes/a')))?.parser?.parser_version).toBe(SYNC_IMPORT_PARSER_VERSION);+ expect((await readSyncImportProvenance(engine, 'default', incarnation, await pageIdOf(engine, 'notes/b')))?.revision).toBe(await revisionOf(engine, 'notes/b'));+ const full = await performManagedSync(engine, { ...OPTS, full: true });+ expect(full).toMatchObject({ prescreened: 0, waived: { imports: SLUGS.length } });+ }, { databaseUrl, setup }), 180_000);++ test('a database-only edit to a page whose file did not change is held file_drift and not imported', () => managedBrain(async ({ engine, ctx, root }) => {+ await engine.setConfig('sync.write_through', 'false');+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ const requestId = await databaseOnlyEdit(engine, ctx, 'notes/a', 'The database-only edit of notes/a.');+ rewriteHistory(root);+ const walk = await performManagedSync(engine, OPTS);+ expect(walk.status).not.toBe('blocked_by_failures');+ expect(heldOf(walk)).toEqual([{ path: 'notes/a.md', code: 'file_drift' }]);+ expect(walk.holds_escalated ?? false).toBe(false);+ const item = walk.held![0]!;+ expect(item.fix.argv).toEqual(['gbrain', 'repair', 'file-drift', '--source', 'default', '--slug', 'notes/a', '--prefer', 'database']);+ expect(item.fix.why).toContain(requestId);+ expect(await bodyOf(engine, 'notes/a')).toContain('The database-only edit of notes/a.');+ expect(await readGitHold(engine, 'default', await incarnationOf(engine), 'notes/a.md')).toMatchObject({ code: 'file_drift',+ meta: { sides: 'database', competing_request_id: requestId, competing_operation: 'put_page', page_revision: await revisionOf(engine, 'notes/a') } });+ // Another tree walk keeps it held; the database version is never overwritten.+ rewriteHistory(root);+ await performManagedSync(engine, OPTS);+ expect(await bodyOf(engine, 'notes/a')).toContain('The database-only edit of notes/a.');+ // A further database edit moves the page: the hold is re-screened (a delta run, the file unchanged) and held again.+ await databaseOnlyEdit(engine, ctx, 'notes/a', 'A second database-only edit of notes/a.');+ writeFileSync(join(root, 'notes/f.md'), note('notes/f', 'A changed body of notes/f.')); commit(root, 'change f');+ const delta = await performManagedSync(engine, OPTS);+ expect(heldOf(delta)).toEqual([{ path: 'notes/a.md', code: 'file_drift' }]);+ expect(await bodyOf(engine, 'notes/a')).toContain('A second database-only edit of notes/a.');+ }, { databaseUrl, setup }), 180_000);++ test('under a storage.keep_db prefix the drifted page is neither held nor imported; the keep_db worker gets it as a candidate', () => managedBrain(async ({ engine, ctx, root }) => {+ await engine.setConfig('sync.write_through', 'false');+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ await databaseOnlyEdit(engine, ctx, 'notes/a', 'The database-only edit of notes/a.');+ writeFileSync(join(root, 'gbrain.yml'), 'storage:\n keep_db:\n - notes/\n'); commit(root, 'keep_db');+ rewriteHistory(root);+ const walk = await performManagedSync(engine, OPTS);+ expect(heldOf(walk)).toEqual([]);+ expect(await bodyOf(engine, 'notes/a')).toContain('The database-only edit of notes/a.');+ expect(Object.keys(await readDriftSkips(engine))).toContain('default:notes/a');+ expect(walk).toMatchObject({ prescreened: SLUGS.length - 1 });+ }, { databaseUrl, setup }), 180_000);++ test('cold upgrade: missing, partial and obsolete provenance with a database-only edit are held, in a tree walk and under --full', () => managedBrain(async ({ engine, ctx, root }) => {+ await engine.setConfig('sync.write_through', 'false');+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ for (const slug of ['notes/a', 'notes/b', 'notes/c']) await databaseOnlyEdit(engine, ctx, slug, `The database-only edit of ${slug}.`);+ await tamper(engine, 'notes/a', 'delete');+ await tamper(engine, 'notes/b', { drop: ['revision', 'disk_sha256', 'disk_blob_oid', 'parser'] });+ await tamper(engine, 'notes/c', [['parser,parser_version', 'an-older-parser']]);+ // Equal-content controls: d has no record and no edit (sync wrote its revision); e has no record and an unattributed+ // revision whose content still equals the file; f has no record and a changed file on a page sync wrote last.+ await tamper(engine, 'notes/d', 'delete');+ await tamper(engine, 'notes/e', 'delete');+ await engine.executeRaw("UPDATE pages SET revision_write_request_id=NULL WHERE source_id='default' AND slug='notes/e'");+ await tamper(engine, 'notes/f', 'delete');+ writeFileSync(join(root, 'notes/f.md'), note('notes/f', 'A changed body of notes/f.')); commit(root, 'change f');+ rewriteHistory(root);+ const walk = await performManagedSync(engine, OPTS);+ expect(heldOf(walk)).toEqual([{ path: 'notes/a.md', code: 'file_drift' }, { path: 'notes/b.md', code: 'file_drift' }, { path: 'notes/c.md', code: 'file_drift' }]);+ const incarnation = await incarnationOf(engine);+ expect((await readGitHold(engine, 'default', incarnation, 'notes/a.md'))?.meta.sides).toBe('unknown');+ expect((await readGitHold(engine, 'default', incarnation, 'notes/b.md'))?.meta.sides).toBe('unknown');+ expect((await readGitHold(engine, 'default', incarnation, 'notes/c.md'))?.meta.sides).toBe('database');+ for (const slug of ['notes/a', 'notes/b', 'notes/c']) expect(await bodyOf(engine, slug)).toContain(`The database-only edit of ${slug}.`);+ expect(await bodyOf(engine, 'notes/f')).toContain('A changed body of notes/f.');+ expect(walk).toMatchObject({ waived: { imports: 2 } });+ for (const slug of ['notes/d', 'notes/e']) expect(await readSyncImportProvenance(engine, 'default', incarnation, await pageIdOf(engine, slug))).not.toBeNull();+ const full = await performManagedSync(engine, { ...OPTS, full: true });+ expect(heldOf(full)).toEqual([{ path: 'notes/a.md', code: 'file_drift' }, { path: 'notes/b.md', code: 'file_drift' }, { path: 'notes/c.md', code: 'file_drift' }]);+ for (const slug of ['notes/a', 'notes/b', 'notes/c']) expect(await bodyOf(engine, slug)).toContain(`The database-only edit of ${slug}.`);+ }, { databaseUrl, setup }), 240_000);++ test('a cursor whose manifest is gone re-discovers against HEAD and finishes; what the lost run imported is prescreened', () => managedBrain(async ({ engine }) => {+ const abort = new AbortController();+ const partial = await performManagedSync(engine, { ...OPTS, signal: abort.signal, onProgress: p => { if (p.bankedFiles === 2) abort.abort(); } });+ expect(partial).toMatchObject({ status: 'partial', filesImported: 2 });+ await engine.executeRaw("DELETE FROM op_checkpoints WHERE op='managed-sync-manifest'");+ // The lost run's own admitted request may still be finishing: the swap waits for it (writer_pending), then re-discovers.+ let resumed = await performManagedSync(engine, OPTS);+ for (let i = 0; i < 5 && resumed.status === 'partial'; i++) { await new Promise(resolve => setTimeout(resolve, 500)); resumed = await performManagedSync(engine, OPTS); }+ expect(resumed.status).toBe('first_sync');+ expect(resumed.prescreened).toBeGreaterThanOrEqual(2);+ for (const slug of SLUGS) expect(await bodyOf(engine, slug)).toContain(`The committed body of ${slug}.`);+ }, { databaseUrl, setup }), 180_000);+});