Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(reconcile): a merge that asks for private in a spelling page privacy does not read refuses visibility_not_applied (#7897)
garrytan442fa66614ed
diff --git a/src/core/persistence/reconcile-prepare.ts b/src/core/persistence/reconcile-prepare.tsindex 848937fd6d..6c54153e1f 100644--- a/src/core/persistence/reconcile-prepare.ts+++ b/src/core/persistence/reconcile-prepare.ts@@ -8,6 +8,7 @@ import { parseMarkdown, serializePageToMarkdown } from '../markdown.ts';import { alignWithStored } from './align-stored.ts';import { parseFactsFence, renderFactsTable, replaceOrInsertFactsFence, restoreHiddenFactRows, type ParsedFact } from '../facts-fence.ts';import { opError } from '../ops/contract.ts';+import { privateRequestRefusal } from './visibility-request.ts';import { readFix, trustedCliRequired } from '../ops/op-fix.ts';import type { Action } from '../agent-output.ts';import { sealPageTextProjection } from '../page-state/projections.ts';@@ -107,8 +108,9 @@ export async function prepareReconcileResult(engine: BrainEngine, state: Reconcifilename: basename(state.path).replace(/\.mdx?$/i, ''), noEmbed: true, remote: false, preserveGateMarkers: true, allowEmptyOverwrite: true,overrideTier: (await ownerSourceGateInput(engine, state.pins.source_id, result.frontmatter)).tier,prepareFrontmatter: page => stabilizeSafetyAssessments(page.frontmatter, state.snapshot.page.frontmatter, state.pins.assessment_at),- prepare: async prepared => { ready = prepared; return prepared.result; },+ prepare: async prepared => { ready = prepared; return prepared.result; }, holdNoncanonicalPrivate: true,});+ if (imported.refusal?.code === 'visibility_noncanonical') throw privateRequestRefusal(state.pins, result.frontmatter);if (!ready || ready.slug !== state.pins.slug) throw opError('invalid_params', imported.error ?? 'Reconciliation cannot change page identity or deduplicate to another page.',`The reconciled content of ${state.pins.slug} in source ${state.pins.source_id} does not import as that same page (a changed frontmatter slug or a match to another page), and reconcile never changes page identity. Keep its slug as ${state.pins.slug} in the file. ${freshPreview(state.pins.source_id, state.pins.slug)}`,{ fix: pageFix(state.pins.source_id, state.pins.slug) });diff --git a/test/e2e/reconcile-private-hold-7897-postgres.test.ts b/test/e2e/reconcile-private-hold-7897-postgres.test.tsnew file mode 100644index 0000000000..9f77200133--- /dev/null+++ b/test/e2e/reconcile-private-hold-7897-postgres.test.ts@@ -0,0 +1,3 @@+import { registerPostgresTests } from '../helpers/test-backends.ts';++await registerPostgresTests(() => import('../reconcile-private-hold-7897.test.ts'));diff --git a/test/reconcile-private-hold-7897.test.ts b/test/reconcile-private-hold-7897.test.tsnew file mode 100644index 0000000000..e76e019db6--- /dev/null+++ b/test/reconcile-private-hold-7897.test.ts@@ -0,0 +1,106 @@+/**+ * #7897: `gbrain sources reconcile` publishes the merge of a canonical file and its stored page through+ * `importFromContent` (reconcile-prepare.ts), whose screen did not hold a private request page privacy does not read.+ * Only the lowercase `visibility` key is protected from the file side, so a file that adds `Visibility: private` merged+ * in as an ordinary field (an additive one under `--auto-additive`) and the page was published world-readable while its+ * frontmatter asked for private.+ *+ * Protects, on a source rooted at a plain directory (no Git) and at a Git checkout: such a merge refuses+ * `visibility_not_applied` and nothing is written; a page stored `visibility: private` still reconciles and stays+ * private; a merge that asks for nothing private still reconciles.+ *+ * PGLite here; Postgres through test/e2e/reconcile-private-hold-7897-postgres.test.ts.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { randomUUID } from 'node:crypto';+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import type { BrainEngine } from '../src/core/engine.ts';+import { importFromContent } from '../src/core/import-file.ts';+import { serializePageToMarkdown } from '../src/core/markdown.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { registerLocalWriter, withVerifiedLocalRegistration } from '../src/core/persistence/identity.ts';+import { claimWorktree } from '../src/core/persistence/ownership.ts';+import { runReconcileApply, runReconcilePreview } from '../src/core/persistence/reconcile.ts';+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import { isPrivatePage } from '../src/core/search/private-visibility.ts';+import { durableGitRepo } from './helpers/git-publication.ts';+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';+import { testBackends } from './helpers/test-backends.ts';+import { withEnv } from './helpers/with-env.ts';++const home = mkdtempSync(join(tmpdir(), 'gbrain-reconcile-7897-'));+const backends = testBackends();+const engines: BrainEngine[] = [];+let closePostgres: (() => Promise<void>) | undefined;+beforeAll(async () => {+ if (backends.includes('pglite')) { const pglite = new PGLiteEngine(); await pglite.connect({}); await pglite.initSchema(); engines.push(pglite); }+ if (backends.includes('postgres')) { const pg = await isolatedPersistencePostgres(process.env.DATABASE_URL!); engines.push(pg.engine); closePostgres = pg.close; }+}, 120_000);+afterAll(async () => {+ await withEnv({ GBRAIN_HOME: home }, async () => {+ for (const engine of engines) { await disposePersistenceConsumer(engine); if (engine.kind === 'pglite') await engine.disconnect(); }+ await closePostgres?.();+ });+ rmSync(home, { recursive: true, force: true });+});++const SLUG = 'people/alice-example';+const stored = (extra = '') => `---\ntype: person\ntitle: Alice Example\n${extra}---\nA synthetic biography of alice-example at acme-example.\n`;++/** A managed source whose file is the stored page plus `frontmatter` and one appended body line. */+async function drifted(engine: BrainEngine, repo: boolean, storedExtra: string, frontmatter: Record<string, unknown>) {+ await disposePersistenceConsumer(engine);+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');+ const id = `source-${randomUUID().slice(0, 8)}`;+ const root = join(home, id);+ mkdirSync(join(root, 'people'), { recursive: true });+ await engine.executeRaw('INSERT INTO sources(id,name,local_path,config) VALUES($1,$1,$2,\'{}\')', [id, root]);+ await importFromContent(engine, SLUG, stored(storedExtra), { sourceId: id, sourcePath: `${SLUG}.md`, noEmbed: true });+ const snapshot = (await engine.readPageSnapshot(SLUG, { sourceId: id }))!;+ writeFileSync(join(root, `${SLUG}.md`), serializePageToMarkdown({ ...snapshot.page, frontmatter: { ...snapshot.page.frontmatter, ...frontmatter },+ compiled_truth: `${snapshot.page.compiled_truth}\nAlso advises beta-example.` }, snapshot.tags));+ if (repo) durableGitRepo(root, [`${SLUG}.md`]);+ await claimWorktree(engine, id, root);+ await engine.executeRaw('UPDATE persistence_brain SET enabled=true WHERE singleton=1');+ return { id, registration: await registerLocalWriter(engine, 'cli'), before: snapshot };+}++/** The --auto-additive preview, with every remaining conflict taken from the file. */+async function preview(engine: BrainEngine, id: string) {+ const first = await runReconcilePreview(engine, { source_id: id, slug: SLUG, auto_additive: true, accept_suggested: true });+ const decisions = ((first.conflict_paths as string[] | undefined) ?? []).map(path => ({ path, action: 'take_file' }));+ return decisions.length ? runReconcilePreview(engine, { source_id: id, slug: SLUG, from: first.preview, decisions }) : first;+}+const apply = async (engine: BrainEngine, id: string) =>+ runReconcileApply(engine, { source_id: id, slug: SLUG, preview: (await preview(engine, id)).preview, request_id: randomUUID() });++for (const backend of backends) for (const repo of [false, true]) describe(`[${backend}] sources reconcile, ${repo ? 'Git checkout' : 'plain directory'} (#7897)`, () => {+ const engine = () => engines[backends.indexOf(backend)]!;++ test('a file adding Visibility: private refuses visibility_not_applied and publishes nothing', async () => withEnv({ GBRAIN_HOME: home }, async () => {+ const f = await drifted(engine(), repo, '', { Visibility: 'private' });+ const refused = await withVerifiedLocalRegistration(engine(), f.registration, () => apply(engine(), f.id))+ .then(() => null, (error: { code?: string; message?: string }) => ({ code: error.code, message: error.message }));+ expect(refused).toEqual({ code: 'visibility_not_applied', message: expect.stringContaining(`page ${SLUG} would stay world-readable`) });+ const after = (await engine().readPageSnapshot(SLUG, { sourceId: f.id }))!;+ expect({ revision: after.revision, body: after.page.compiled_truth, frontmatter: after.page.frontmatter })+ .toEqual({ revision: f.before.revision, body: f.before.page.compiled_truth, frontmatter: f.before.page.frontmatter });+ }), 120_000);++ test('a page stored visibility: private reconciles and stays private', async () => withEnv({ GBRAIN_HOME: home }, async () => {+ const f = await drifted(engine(), repo, 'visibility: private\n', {});+ expect((await withVerifiedLocalRegistration(engine(), f.registration, () => apply(engine(), f.id))).state).toBe('committed');+ const after = (await engine().readPageSnapshot(SLUG, { sourceId: f.id }))!;+ expect({ body: after.page.compiled_truth.includes('Also advises beta-example.'), private: isPrivatePage(after.page) }).toEqual({ body: true, private: true });+ }), 120_000);++ test('a merge that asks for nothing private still reconciles', async () => withEnv({ GBRAIN_HOME: home }, async () => {+ const f = await drifted(engine(), repo, '', { phone: '555-0100' });+ expect((await withVerifiedLocalRegistration(engine(), f.registration, () => apply(engine(), f.id))).state).toBe('committed');+ const after = (await engine().readPageSnapshot(SLUG, { sourceId: f.id }))!;+ expect({ body: after.page.compiled_truth.includes('Also advises beta-example.'), phone: after.page.frontmatter.phone, private: isPrivatePage(after.page) })+ .toEqual({ body: true, phone: '555-0100', private: false });+ }), 120_000);+});