Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(sync): GBRA-130 A-17/B-3/B-4 remote-pass probe: no hold, no write at the paths, then the owner pass holds them (#6712 PR 4b)
garrytan2693284bfd17
diff --git a/test/sync-page-identity-hold-gbra130.test.ts b/test/sync-page-identity-hold-gbra130.test.tsindex fbd9c123c8..eb5d50664c 100644--- a/test/sync-page-identity-hold-gbra130.test.ts+++ b/test/sync-page-identity-hold-gbra130.test.ts@@ -7,17 +7,21 @@* Protects (red before: the sync rejects page_identity_changed and notes/other.md never imports): the file alone is held* page_identity_changed with meta.reason several_tombstones / spelling / identity_differs, needs a person* (sync status page_identity_decision), nothing is imported, revived or deleted at its slug, the unrelated file imports,- * and a later sync holds it again without stopping. A dry run still refuses the source, as discovery does. Runs on PGLite- * and, with DATABASE_URL, on Postgres.+ * and a later sync holds it again without stopping. A dry run still refuses the source, as discovery does. A remote pass+ * (which writes no hold) leaves the paths alone, imports the unrelated file, and the next owner pass holds them. Runs on+ * PGLite and, with DATABASE_URL, on Postgres.*/import { describe, expect, test } from 'bun:test';import { execFileSync } from 'node:child_process';import { mkdirSync, writeFileSync } from 'node:fs';import { join } from 'node:path';+import { randomUUID } from 'node:crypto';import type { BrainEngine } from '../src/core/engine.ts';+import type { OperationContext } from '../src/core/ops/contract.ts';+import { prepareRemoteJob, withSubmissionAuthority } from '../src/core/minions/submission-authority.ts';import { withCoordinatedWrite } from '../src/core/persistence/context.ts';import { performManagedSync } from '../src/core/persistence/sync-run.ts';-import { readGitHold } from '../src/core/persistence/sync-holds.ts';+import { readGitHold, readGitSourceHolds } from '../src/core/persistence/sync-holds.ts';import { readSyncStatus } from '../src/core/persistence/sync-status.ts';import { managedBrain } from './helpers/managed-brain.ts';import { testBackends } from './helpers/test-backends.ts';@@ -39,6 +43,51 @@ const requests = async (engine: BrainEngine) => (await engine.executeRaw<{ path:"SELECT intent->>'path' AS path FROM persistence_requests WHERE source_id='default' AND intent->>'path' IS NOT NULL ORDER BY sequence")).map(row => row.path);const incarnation = async (engine: BrainEngine) => (await engine.executeRaw<{ incarnation: string }>("SELECT incarnation::text AS incarnation FROM sources WHERE id='default'"))[0]!.incarnation;+type Reason = 'several_tombstones' | 'spelling' | 'identity_differs';+/** Commits one undecidable case beside the unrelated notes/other.md; returns its paths and the no-takeover / no-guess checks. */+const ARRANGE: Record<Reason, (engine: BrainEngine, root: string) => Promise<{ paths: string[]; unchanged: () => Promise<void> }>> = {+ several_tombstones: async (engine, root) => {+ for (const slug of ['archive/twin-one', 'archive/twin-two']) await engine.transaction(tx => withCoordinatedWrite(tx, ['default'], async () => {+ await tx.putPage(slug, { type: 'note', title: slug, compiled_truth: `Deleted ${slug}.`, timeline: '', frontmatter: {}, content_hash: `h-${slug}` }, { sourceId: 'default' });+ await tx.executeRaw("UPDATE pages SET source_path='notes/twin.md', deleted_at=now() WHERE source_id='default' AND slug=$1", [slug]);+ }, TEST_WRITE_ATTRIBUTION));+ writeFileSync(join(root, 'notes/twin.md'), note('notes/twin'));+ writeFileSync(join(root, 'notes/other.md'), note('notes/other'));+ commit(root, 'twin and other');+ return { paths: ['notes/twin.md'], unchanged: async () => {+ expect(await rows(engine, 'notes/twin')).toEqual([]);+ for (const slug of ['archive/twin-one', 'archive/twin-two']) expect((await rows(engine, slug))[0]?.deleted_at).not.toBeNull();+ } };+ },+ spelling: async (engine, root) => {+ writeFileSync(join(root, 'notes/Case.md'), note('Upper'));+ writeFileSync(join(root, 'notes/case.md'), note('Lower'));+ writeFileSync(join(root, 'notes/other.md'), note('notes/other'));+ commit(root, 'two spellings and other');+ return { paths: ['notes/Case.md', 'notes/case.md'], unchanged: async () => expect(await rows(engine, 'notes/case')).toEqual([]) };+ },+ identity_differs: async (engine, root) => {+ const [kept] = await rows(engine, 'notes/kept-doc');+ expect(kept).toMatchObject({ source_path: 'notes/Kept Doc.md', deleted_at: null });+ git(root, 'rm', '-q', 'notes/Kept Doc.md');+ writeFileSync(join(root, 'notes/kept-doc.md'), note('Another document', 'id: kept-two\n'));+ writeFileSync(join(root, 'notes/other.md'), note('notes/other'));+ commit(root, 'replace kept with another document');+ return { paths: ['notes/kept-doc.md'], unchanged: async () => expect(await rows(engine, 'notes/kept-doc')).toEqual([kept]) };+ },+};+const TITLES: Record<Reason, string> = {+ several_tombstones: 'a file two deleted pages record',+ spelling: 'each of two files differing only by case',+ identity_differs: 'a file whose id differs from the page at its slug',+};+async function remoteCtx(engine: BrainEngine): Promise<OperationContext> {+ const clientId = `sync-client-${randomUUID()}`;+ await engine.executeRaw(`INSERT INTO oauth_clients(client_id,client_name,client_secret_hash,scope,source_id,allowed_operations) VALUES($1,'example-client','test-only','admin','default',ARRAY['submit_job'])`, [clientId]);+ return { engine, remote: true, sourceId: 'default', auth: { clientId, principal: { kind: 'oauth_client', id: clientId }, scopes: ['admin'], sourceId: 'default', allowedOperations: ['submit_job'] } } as unknown as OperationContext;+}+const sourceHolds = async (engine: BrainEngine) => ((await readGitSourceHolds(engine, { sourceIds: ['default'] }))[0]?.holds ?? []).map(hold => [hold.path, hold.code, hold.meta.reason ?? null]);+for (const backend of testBackends()) describe(`[${backend}] GBRA-130 A-17/B-3/B-4: one file whose page identity is undecidable`, () => {const databaseUrl = backend === 'postgres' ? process.env.DATABASE_URL : undefined;async function heldAlone(engine: BrainEngine, root: string, paths: string[], reason: string) {@@ -64,39 +113,32 @@ for (const backend of testBackends()) describe(`[${backend}] GBRA-130 A-17/B-3/Bexpect(await rows(engine, 'notes/later')).toMatchObject([{ deleted_at: null }]);}- test('several_tombstones: a file two deleted pages record is held, neither is revived, the rest imports', () => managedBrain(async ({ engine, root }) => {- expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');- for (const slug of ['archive/twin-one', 'archive/twin-two']) await engine.transaction(tx => withCoordinatedWrite(tx, ['default'], async () => {- await tx.putPage(slug, { type: 'note', title: slug, compiled_truth: `Deleted ${slug}.`, timeline: '', frontmatter: {}, content_hash: `h-${slug}` }, { sourceId: 'default' });- await tx.executeRaw("UPDATE pages SET source_path='notes/twin.md', deleted_at=now() WHERE source_id='default' AND slug=$1", [slug]);- }, TEST_WRITE_ATTRIBUTION));- writeFileSync(join(root, 'notes/twin.md'), note('notes/twin'));- writeFileSync(join(root, 'notes/other.md'), note('notes/other'));- commit(root, 'twin and other');- await heldAlone(engine, root, ['notes/twin.md'], 'several_tombstones');- expect(await rows(engine, 'notes/twin')).toEqual([]);- for (const slug of ['archive/twin-one', 'archive/twin-two']) expect((await rows(engine, slug))[0]?.deleted_at).not.toBeNull();- }, { databaseUrl, setup }), 240_000);+ for (const reason of Object.keys(ARRANGE) as Reason[]) {+ test(`${reason}: ${TITLES[reason]} is held alone, nothing is imported, revived or taken over, the rest imports`, () => managedBrain(async ({ engine, root }) => {+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ const { paths, unchanged } = await ARRANGE[reason](engine, root);+ await heldAlone(engine, root, paths, reason);+ await unchanged();+ }, { databaseUrl, setup }), 240_000);- test('spelling: two files differing only by case are both held, nothing is imported at their slug, the rest imports', () => managedBrain(async ({ engine, root }) => {- expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');- writeFileSync(join(root, 'notes/Case.md'), note('Upper'));- writeFileSync(join(root, 'notes/case.md'), note('Lower'));- writeFileSync(join(root, 'notes/other.md'), note('notes/other'));- commit(root, 'two spellings and other');- await heldAlone(engine, root, ['notes/Case.md', 'notes/case.md'], 'spelling');- expect(await rows(engine, 'notes/case')).toEqual([]);- }, { databaseUrl, setup }), 240_000);-- test('identity_differs: a file whose id differs from the page at its slug is held, the page keeps its id and body, the rest imports', () => managedBrain(async ({ engine, root }) => {- expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');- const [kept] = await rows(engine, 'notes/kept-doc');- expect(kept).toMatchObject({ source_path: 'notes/Kept Doc.md', deleted_at: null });- git(root, 'rm', '-q', 'notes/Kept Doc.md');- writeFileSync(join(root, 'notes/kept-doc.md'), note('Another document', 'id: kept-two\n'));- writeFileSync(join(root, 'notes/other.md'), note('notes/other'));- commit(root, 'replace kept with another document');- await heldAlone(engine, root, ['notes/kept-doc.md'], 'identity_differs');- expect(await rows(engine, 'notes/kept-doc')).toEqual([kept]);- }, { databaseUrl, setup }), 240_000);+ // A remote pass writes no hold (the owner's pass does): it leaves the paths alone, importing, deleting and holding+ // nothing there, while the unrelated file it may sync imports; the next owner pass holds each path with its reason.+ test(`${reason} under remote authority: no hold, no write at the paths, the rest imports; the owner pass then holds them`, () => managedBrain(async ({ engine, root }) => {+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ const { paths, unchanged } = await ARRANGE[reason](engine, root);+ const before = await requests(engine);+ const accepted = await prepareRemoteJob(await remoteCtx(engine), 'sync', { noPull: true });+ const remote = await withSubmissionAuthority(accepted.authority, () => performManagedSync(engine, { ...accepted.data, noEmbed: true, noExtract: true }));+ expect(remote.reason).toBe('reconcile_host_required');+ expect(await sourceHolds(engine)).toEqual([]);+ expect((await requests(engine)).filter(path => !before.includes(path))).toEqual(['notes/other.md']);+ expect(await rows(engine, 'notes/other')).toMatchObject([{ deleted_at: null }]);+ await unchanged();+ const owner = await performManagedSync(engine, OPTS);+ expect(owner.status).toBe('synced');+ expect((await sourceHolds(engine)).sort()).toEqual(paths.map(path => [path, 'page_identity_changed', reason]).sort());+ expect((await requests(engine)).filter(path => !before.includes(path))).toEqual(['notes/other.md']);+ await unchanged();+ }, { databaseUrl, setup }), 240_000);+ }});