Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(#6712 PR 4b): overwritten-edits seed writes the cursor engine's overwrite history directly; a pass-route row asserts #6887 (the edit kept, the file held file_drift)
garrytan70756aee40b1
diff --git a/test/overwritten-edits-6712.test.ts b/test/overwritten-edits-6712.test.tsindex 40b97fd25f..62e43e0f25 100644--- a/test/overwritten-edits-6712.test.ts+++ b/test/overwritten-edits-6712.test.ts@@ -1,11 +1,11 @@/*** #6712 follow-up: find and restore database edits an incremental managed sync silently overwrote.*- * The overwrite is seeded the way it happens on a released brain: a database-only `put_page` (`sync.write_through`- * off), then a committed edit of the same file, then an incremental sync that publishes the file over the edit with no- * hold. On a base without #6887 any page reproduces it; the seed also drops the page's import provenance record (a page- * last imported before PR 0 recorded one), which no released classifier guards in a delta sync, so the seed reproduces- * with or without #6887 and these tests never depend on that fix.+ * The overwrite is seeded as a released brain recorded it: a database-only `put_page` (`sync.write_through` off), a+ * committed edit of the same file, and the history the cursor engine's incremental sync left when it published the file+ * over the edit with no hold (its import receipt, the archived version, the import record and `last_commit`). #6712 PR 4b's+ * pass no longer publishes over the edit, so the seed writes that history directly and these tests never depend on a+ * sync reproducing the bug; one row asserts the pass route's #6887 guarantee on the same scenario instead.** Protects:* - a seeded overwrite is detected `high` (also after receipt compaction nulled every intent), and `repair@@ -26,13 +26,16 @@*/import { describe, expect, test } from 'bun:test';import { execFileSync } from 'node:child_process';-import { randomUUID } from 'node:crypto';+import { createHash, randomUUID } from 'node:crypto';import { mkdirSync, readFileSync, 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 { compactWriteReceipts } from '../src/core/persistence/journal.ts';import { performManagedSync } from '../src/core/persistence/sync-run.ts';+import { managedCursorKey } from '../src/core/persistence/sync-cursor-key.ts';+import { withCoordinatedWrite } from '../src/core/persistence/context.ts';+import type { WriteAttribution } from '../src/core/persistence/attribution.ts';import { submitPageMutation } from '../src/core/persistence/page-mutations.ts';import { declarePersistenceProtocol } from '../src/core/persistence/protocol.ts';import { scanOverwrittenEdits } from '../src/core/persistence/overwritten-edits.ts';@@ -67,16 +70,57 @@ async function putPage(engine: BrainEngine, ctx: OperationContext, slug: string,await submitPageMutation(ctx, { operation: 'put_page', params: { slug, request_id: requestId, expected_revision: await revisionOf(engine, slug), 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: 30_000 });}-/** A database-only edit of `slug`, then a committed edit of its file and an incremental sync that publishes it over the edit. */+/**+ * A database-only edit of `slug`, then a committed edit of its file and the history the cursor engine left when its+ * incremental sync published that file over the edit: a committed `managed_sync_import` receipt (a cursor intent) whose+ * write archived the edit's revision and wrote the file's content, the page's import record of that blob and revision,+ * and `last_commit` at the edit. #6712 PR 4b's pass holds such an import instead (see the last row), so the seed writes+ * the history directly; no sync publishes over the edit.+ */async function seedOverwrite(engine: BrainEngine, ctx: OperationContext, root: string, slug: string): Promise<void> {await engine.setConfig('sync.write_through', 'false');await putPage(engine, ctx, slug, 'DBONLYMARKER the agent edit.');await engine.setConfig('sync.write_through', 'true');- const [page] = await engine.executeRaw<{ id: number }>("SELECT id FROM pages WHERE source_id='default' AND slug=$1", [slug]);- await engine.executeRaw('DELETE FROM op_checkpoints WHERE op=$1 AND fingerprint=$2', [SYNC_IMPORT_PROVENANCE_OP, `default:${await incarnationOf(engine)}:${page!.id}`]);- writeFileSync(join(root, `${slug}.md`), note(slug, 'The second committed body.'));+ // The edit's own effects (its embedding) settle before the later import, as they had on a released brain.+ await waitFor(async () => !(await engine.executeRaw("SELECT 1 FROM persistence_effects WHERE source_id='default' AND state<>'committed' LIMIT 1")).length,+ { timeoutMs: 30_000, label: "the database edit's effects settle" });+ const content = note(slug, 'The second committed body.');+ writeFileSync(join(root, `${slug}.md`), content);commit(root, `edit ${slug}`);- await performManagedSync(engine, OPTS);+ const head = git(root, 'rev-parse', 'HEAD'), blob = git(root, 'rev-parse', `HEAD:${slug}.md`);+ const rawHash = createHash('sha256').update(content).digest('hex');+ const incarnation = await incarnationOf(engine);+ const [earlier] = await engine.executeRaw<{ id: string; principal_kind: string; principal_id: string }>(`SELECT id::text AS id, principal_kind, principal_id FROM persistence_requests+ WHERE source_id='default' AND slug=$1 AND state='committed' AND intent->>'kind'='managed_sync_import' ORDER BY sequence DESC LIMIT 1`, [slug]);+ const [checkpoint] = await engine.executeRaw<{ id: string }>(`SELECT id::text AS id FROM persistence_requests+ WHERE source_id='default' AND state='committed' AND intent->>'kind'='managed_sync_checkpoint' ORDER BY sequence DESC LIMIT 1`);+ const importId = randomUUID(), checkpointId = randomUUID();+ const run = { cursorKey: managedCursorKey(incarnation, undefined), runId: randomUUID(), target: head };+ /** A copy of a committed receipt of this source as a cursor run's receipt: the run's fields instead of the pass's. */+ const receipt = (tx: BrainEngine, from: string, id: string, fields: Record<string, unknown>) => tx.executeRaw(`INSERT INTO persistence_requests+ SELECT (jsonb_populate_record(NULL::persistence_requests, to_jsonb(r) || jsonb_build_object('id', $2::uuid, 'request_id', gen_random_uuid(),+ 'digest', md5(r.digest || $2), 'sequence', nextval(pg_get_serial_sequence('persistence_requests', 'sequence')),+ 'intent', (r.intent - 'passId' - 'level') || $3::text::jsonb, 'created_at', now(), 'updated_at', now(), 'completed_at', now(), 'published_at', now()))).*+ FROM persistence_requests r WHERE r.id = $1::uuid`, [from, id, JSON.stringify(fields)]);+ await engine.transaction(async tx => {+ await declarePersistenceProtocol(tx);+ await receipt(tx, earlier!.id, importId, { ...run, index: 0, total: 1, blobOid: blob, content, rawHash });+ await withCoordinatedWrite(tx, ['default'], async () => { await tx.createVersion(slug, { sourceId: 'default' }); await tx.putPage(slug, { type: 'concept', title: slug, compiled_truth: 'The second committed body.', timeline: '',+ frontmatter: {}, content_hash: rawHash, source_path: `${slug}.md` }, { sourceId: 'default' }); },+ { requestId: importId, principal: { kind: earlier!.principal_kind, id: earlier!.principal_id } } as WriteAttribution);+ await tx.executeRaw(`UPDATE persistence_requests SET outcome = outcome || jsonb_build_object('status', 'updated', 'revision',+ (SELECT knowledge_revision::text FROM pages WHERE source_id = 'default' AND slug = $2)) WHERE id = $1::uuid`, [importId, slug]);+ await receipt(tx, checkpoint!.id, checkpointId, run);+ });+ const [page] = await engine.executeRaw<{ id: number; revision: string }>("SELECT id, knowledge_revision::text AS revision FROM pages WHERE source_id='default' AND slug=$1", [slug]);+ // The import's record and the cursor's last_commit, written outside the owner's write guards (only its publication may write them).+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');+ try {+ await engine.executeRaw(`UPDATE op_checkpoints SET completed_keys = jsonb_set(completed_keys, '{0}', completed_keys->0 || jsonb_build_object('blob_oid', $3::text,+ 'disk_blob_oid', $3::text, 'revision', $4::text, 'raw_sha256', $5::text, 'disk_sha256', $5::text)) WHERE op = $1 AND fingerprint = $2`,+ [SYNC_IMPORT_PROVENANCE_OP, `default:${incarnation}:${page!.id}`, blob, page!.revision, rawHash]);+ await engine.executeRaw("UPDATE sources SET last_commit = $1 WHERE id = 'default'", [head]);+ } finally { await engine.executeRaw('UPDATE persistence_brain SET enabled=true WHERE singleton=1'); }expect(await bodyOf(engine, slug)).toContain('The second committed body.');}const doctor = async (engine: BrainEngine) => ((await overwrittenEditsEntry.run({ engine, tier: 'standard', doFix: false, dryRun: false } as unknown as DoctorContext)) as Check[])[0]!;@@ -128,6 +172,26 @@ for (const databaseUrl of process.env.DATABASE_URL ? [undefined, process.env.DATexpect((await doctor(engine)).status).toBe('ok');}, { setup, databaseUrl }), 180_000);+ test('#6887 on the pass route: a sync of the same scenario keeps the database edit and holds the file file_drift', () => managedBrain(async ({ engine, ctx, root }) => {+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ // The scenario the seed records, synced on #6712 PR 4b: a database-only edit, the page's import record gone (a page last+ // imported before PR 0), and a committed edit of its file.+ await engine.setConfig('sync.write_through', 'false');+ await putPage(engine, ctx, 'notes/a', 'DBONLYMARKER the agent edit.');+ await engine.setConfig('sync.write_through', 'true');+ const [page] = await engine.executeRaw<{ id: number }>("SELECT id FROM pages WHERE source_id='default' AND slug='notes/a'");+ await engine.executeRaw('DELETE FROM op_checkpoints WHERE op=$1 AND fingerprint=$2', [SYNC_IMPORT_PROVENANCE_OP, `default:${await incarnationOf(engine)}:${page!.id}`]);+ writeFileSync(join(root, 'notes/a.md'), note('notes/a', 'The second committed body.'));+ commit(root, 'edit notes/a');+ const synced = await performManagedSync(engine, OPTS);+ expect((synced.held ?? []).map(item => [item.path, item.code, item.slug])).toEqual([['notes/a.md', 'file_drift', 'notes/a']]);+ expect(await bodyOf(engine, 'notes/a')).toContain('DBONLYMARKER the agent edit.');+ expect(await bodyOf(engine, 'notes/a')).not.toContain('The second committed body.');+ expect(readFileSync(join(root, 'notes/a.md'), 'utf8')).toContain('The second committed body.');+ expect((await holdOf(engine, 'notes/a.md'))?.code).toBe('file_drift');+ expect((await scanOverwrittenEdits(engine, 'default')).edits).toEqual([]);+ }, { setup, databaseUrl }), 180_000);+test('a normal sync-written history is not flagged', () => managedBrain(async ({ engine, ctx, root }) => {expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');await putPage(engine, ctx, 'notes/c', 'A write-through edit.');