Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(google): a forgotten fact no longer holds its connector page as file drift
garrytan35719d303137
diff --git a/src/core/facts/file-body-guard.ts b/src/core/facts/file-body-guard.tsindex bf7bc6e056..be9e718029 100644--- a/src/core/facts/file-body-guard.ts+++ b/src/core/facts/file-body-guard.ts@@ -133,12 +133,13 @@ function fileHeldBy(fileBytes: string, slug: string, state: StoredState): boolea* `expectedFileBytes` is what the caller passes to `writePageThrough` (null: the file must still be absent), so a file* that changes between this check and the write is refused there too.*- * `alsoHeldAs` (GBRA-133, #7292): other bytes the file stands for, or null. A connector passes the file with the live- * page's `## Facts` fence put back: that fence is gbrain's own content, which the provider's render never had, so a- * file that lacks only it holds no edit the database lacks.+ * `alsoHeldAs` (GBRA-133, #7292): other bytes the file stands for when compared with one stored state, or null. A+ * connector passes the file with that state's `## Facts` fence put back: that fence is gbrain's own content, which the+ * provider's render never had, so a file that lacks only it holds no edit the database lacks. Per state (#8645): the+ * live row stores the fence without the withdrawal overlay a page read adds, while versions store the overlaid view.*/export async function replaceableFile(engine: BrainEngine, sourceId: string, slug: string, filePath: string,- alsoHeldAs?: (fileBytes: string) => string | null): Promise<{ ok: true; expectedFileBytes: string | null } | { ok: false }> {+ alsoHeldAs?: (fileBytes: string, state: StoredBody) => string | null): Promise<{ ok: true; expectedFileBytes: string | null } | { ok: false }> {if (!existsSync(filePath)) return { ok: true, expectedFileBytes: null };const bytes = readFileSync(filePath, 'utf-8');const states = await engine.executeRaw<StoredState>(@@ -149,7 +150,9 @@ export async function replaceableFile(engine: BrainEngine, sourceId: string, slu(SELECT v.compiled_truth, v.timeline, v.frontmatter, v.title, v.type, v.tags FROM page_versions v JOIN pages p ON p.id = v.page_idWHERE p.source_id = $1 AND p.slug = $2 AND v.compiled_truth IS NOT NULL ORDER BY v.id DESC LIMIT ${HISTORY_ROWS})`,[sourceId, slug]);- const candidate = alsoHeldAs?.(bytes) ?? null;- return states.some(state => fileHeldBy(bytes, slug, state) || candidate !== null && fileHeldBy(candidate, slug, state))- ? { ok: true, expectedFileBytes: bytes } : { ok: false };+ return states.some(state => {+ if (fileHeldBy(bytes, slug, state)) return true;+ const candidate = alsoHeldAs?.(bytes, state) ?? null;+ return candidate !== null && fileHeldBy(candidate, slug, state);+ }) ? { ok: true, expectedFileBytes: bytes } : { ok: false };}diff --git a/src/core/google/sweep-shared.ts b/src/core/google/sweep-shared.tsindex 16a5c8c108..5e91da5a7d 100644--- a/src/core/google/sweep-shared.ts+++ b/src/core/google/sweep-shared.ts@@ -13,10 +13,9 @@ import type { ManagedConnectorSync } from '../persistence/connector-sync.ts';import { connectorRender } from '../connectors/connector-text.ts';import { isWriteTargetContained } from '../path-confine.ts';import { mkdirPrivate } from '../atomic-write.ts';-import { replaceableFile } from '../facts/file-body-guard.ts';+import { replaceableFile, type StoredBody } from '../facts/file-body-guard.ts';import { FACTS_FENCE_BEGIN, FACTS_FENCE_END, replaceOrInsertFactsFence } from '../facts-fence.ts';import { carryStoredFences, hasFence } from '../connectors/fence-carry.ts';-import type { Page } from '../types.ts';import { assertInlineBody } from '../page-body/page-shape.ts';import { OperationError } from '../ops/contract.ts';import { slugifyPath } from '../sync.ts';@@ -123,14 +122,13 @@ function writePrivateTemp(tmpPath: string, markdown: string): void {}-/** GBRA-133 (#7292): the checkout file with the live page's `## Facts` fence put back, for `replaceableFile`; null when it has a fence of its own. */-function withStoredFactsFence(existing: Page): (fileBytes: string) => string | null {- assertInlineBody(existing, 'Google connector file comparison');- const body = existing.compiled_truth ?? '';+/** GBRA-133 (#7292, #8645): the checkout file with one stored state's `## Facts` fence put back, for `replaceableFile`; null when it has a fence of its own. */+function withStoredFactsFence(fileBytes: string, state: StoredBody): string | null {+ const body = state.compiled_truth ?? '';const start = body.indexOf(FACTS_FENCE_BEGIN);const stop = start === -1 ? -1 : body.indexOf(FACTS_FENCE_END, start + FACTS_FENCE_BEGIN.length);const block = stop === -1 ? null : body.slice(start, stop + FACTS_FENCE_END.length);- return fileBytes => block === null || hasFence(fileBytes) ? null : replaceOrInsertFactsFence(fileBytes, block);+ return block === null || hasFence(fileBytes) ? null : replaceOrInsertFactsFence(fileBytes, block);}export async function importRendered(@@ -165,7 +163,7 @@ export async function importRendered(if (existing) assertInlineBody(existing, 'Google connector materialization');markdown = await carryStoredFences(deps.sourceId, slug, relPath, existing, markdown, 'Google connector fence carry');const databaseOnly = await isWriteThroughDisabled(deps.engine) || isSourceDbOnlySlug(deps.cfg.dir, slug, 'refuse');- const guard = databaseOnly ? null : await replaceableFile(deps.engine, deps.sourceId, slug, filePath, existing ? withStoredFactsFence(existing) : undefined);+ const guard = databaseOnly ? null : await replaceableFile(deps.engine, deps.sourceId, slug, filePath, existing ? withStoredFactsFence : undefined);const drift = () => new OperationError('file_database_drift',`${slug}: the checkout file has an edit not retained in the database, or changed during import. The file was not replaced. If the file is newer, run gbrain sync --source ${deps.sourceId}; if the database is newer, re-save its page with write-through on; if both changed, ask the user which version to keep before retrying.`,'Import the newer file with gbrain sync, or ask the user which version to keep; the item is held until then.');diff --git a/test/google-withdrawn-fence-carry-8645.test.ts b/test/google-withdrawn-fence-carry-8645.test.tsnew file mode 100644index 0000000000..82616adcfe--- /dev/null+++ b/test/google-withdrawn-fence-carry-8645.test.ts@@ -0,0 +1,99 @@+/**+ * #8645: an unmanaged Google connector page whose checkout file lacks only gbrain's own `## Facts` fence is+ * replaceable (#7292), but once one of that fence's facts was forgotten the re-render was refused+ * file_database_drift on every run and held after three. The guard compared the file plus the live page's fence,+ * which carries the withdrawal overlay, against the raw stored body, which does not. Now each stored state is compared+ * with the file plus that state's own fence; a real file edit is still held. The file holds the provider render from+ * before the facts, and the page carries more than 20 of them, so that render is no longer among the retained versions+ * the guard also compares.+ * Real PGLite and Postgres (when DATABASE_URL is set), the real Google importer, a fake People API.+ */+import { describe, expect, test } from 'bun:test';+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import type { BrainEngine } from '../src/core/engine.ts';+import { parseGoogleSourceConfig, readGoogleState, runGoogleSync } from '../src/core/google/google-source.ts';+import { writeSingleFact } from '../src/core/facts/write-single.ts';+import { recordFactWithdrawal } from '../src/core/facts/withdrawal.ts';+import { FACTS_FENCE_BEGIN } from '../src/core/facts-fence.ts';+import { _resetWriteThroughCacheForTest } from '../src/core/write-through.ts';+import { contact, json, options, withGoogleAccount } from './helpers/connector-fixture.ts';+import { isolatedSharedSkillsEngine } from './helpers/shared-skills-engine.ts';+import { requirePostgresTestDatabase, testBackends } from './helpers/test-backends.ts';+import { withEnv } from './helpers/with-env.ts';++const SOURCE = 'source-a';+const SLUG = 'people/alice-example';+const KEPT = 'Alice will send the signed deck by Friday';+const FORGOTTEN = 'Alice moved to the acme-example design team';+const CONFIG = { kind: 'google', g_account: '[email protected]', g_services: 'contacts', g_access: 'env', g_token_env: 'CONNECTOR_TEST_TOKEN' };++async function withBrain(databaseUrl: string | undefined, run: (b: { engine: BrainEngine; root: string; path: string; render: string; bump(): void; sync(): ReturnType<typeof runGoogleSync> }) => Promise<void>): Promise<void> {+ const root = mkdtempSync(join(tmpdir(), 'gbrain-8645-'));+ try {+ await withEnv({ GBRAIN_HOME: root, DATABASE_URL: undefined, GBRAIN_DATABASE_URL: undefined, CONNECTOR_TEST_TOKEN: 'synthetic-local-fixture' }, async () => {+ _resetWriteThroughCacheForTest();+ const { engine, close } = await isolatedSharedSkillsEngine(databaseUrl);+ try {+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');+ const dir = join(root, 'google');+ await engine.executeRaw('INSERT INTO sources(id,name,local_path,config) VALUES($1,$1,$2,$3::text::jsonb)', [SOURCE, dir, JSON.stringify(CONFIG)]);+ let generation = 0;+ const fetch = withGoogleAccount(async (url: string) => {+ if (new URL(url).pathname === '/v1/people/me/connections') return json({ connections: [+ { ...contact('c1', 'Alice Example'), organizations: [{ name: `Upstream ${generation}` }] }], nextSyncToken: `token-${generation}` });+ throw new Error(`Unexpected Google fixture route: ${url}`);+ }, CONFIG.g_account);+ const cfg = parseGoogleSourceConfig(CONFIG, dir);+ const sync = () => runGoogleSync(engine, SOURCE, cfg, { ...options, sourceId: SOURCE }, fetch);+ expect((await sync()).failedFiles ?? 0).toBe(0);+ const path = join(dir, `${SLUG}.md`);+ await run({ engine, root, path, render: readFileSync(path, 'utf8'), bump: () => { generation++; }, sync });+ } finally { await close(); }+ });+ } finally { rmSync(root, { recursive: true, force: true }); }+}++const holds = (root: string) => ((readGoogleState(join(root, 'google')).item_holds ?? { items: {} }) as { items: Record<string, { code: string; state: string }> }).items;++async function rememberAndForget(engine: BrainEngine): Promise<void> {+ const others = Array.from({ length: 20 }, (_, i) => `Alice example note ${i + 1}`);+ for (const fact of [KEPT, ...others, FORGOTTEN]) {+ await writeSingleFact(engine, SOURCE, { fact, kind: 'fact', entity: 'Alice Example', visibility: 'private', provenance: 'email thread (synthetic)', remote: false });+ }+ const [row] = await engine.executeRaw<{ id: number }>('SELECT id FROM facts WHERE source_id=$1 AND fact=$2', [SOURCE, FORGOTTEN]);+ expect((await recordFactWithdrawal(engine, Number(row!.id), SOURCE)).withdrawn).toBe(true);+ const raw = (await engine.executeRaw<{ compiled_truth: string }>('SELECT compiled_truth FROM pages WHERE source_id=$1 AND slug=$2', [SOURCE, SLUG]))[0]!.compiled_truth;+ const live = (await engine.getPage(SLUG, { sourceId: SOURCE }))!.compiled_truth;+ expect(raw).toContain(FORGOTTEN);+ expect(live).not.toBe(raw);+}++for (const backend of testBackends()) describe(`[${backend}] #8645: a forgotten fact on an unmanaged Google connector page`, () => {+ const databaseUrl = backend === 'postgres' ? requirePostgresTestDatabase() : undefined;++ test('a file that lacks only the fence re-renders instead of being held, and the withdrawal stays', async () => withBrain(databaseUrl, async b => {+ await rememberAndForget(b.engine);+ writeFileSync(b.path, b.render);+ expect(b.render).not.toContain(FACTS_FENCE_BEGIN);+ b.bump();+ for (let i = 0; i < 3; i++) expect((await b.sync()).failedFiles ?? 0).toBe(0);+ expect(holds(b.root)['contact:people/c1']).toBeUndefined();+ const file = readFileSync(b.path, 'utf8');+ expect(file).toContain('Upstream 1');+ expect(file).toContain(KEPT);+ const active = await b.engine.executeRaw<{ fact: string }>('SELECT fact FROM facts WHERE source_id=$1 AND expired_at IS NULL ORDER BY fact', [SOURCE]);+ expect(active.map(r => r.fact)).not.toContain(FORGOTTEN);+ expect(active.map(r => r.fact)).toContain(KEPT);+ }), 180_000);++ test('a real file edit next to the missing fence is still held', async () => withBrain(databaseUrl, async b => {+ await rememberAndForget(b.engine);+ writeFileSync(b.path, `${b.render}\nFILE-ONLY-EDIT: kept by the user.\n`);+ b.bump();+ for (let i = 0; i < 3; i++) expect((await b.sync()).failedFiles).toBe(1);+ expect(holds(b.root)['contact:people/c1']).toMatchObject({ code: 'file_database_drift', state: 'held' });+ expect(readFileSync(b.path, 'utf8')).toContain('FILE-ONLY-EDIT');+ }), 180_000);+});diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txtindex 72ad5657dd..17fa3b9c91 100644--- a/test/postgres-unit-arms.txt+++ b/test/postgres-unit-arms.txt@@ -120,6 +120,7 @@ test/git-door-write-cost.test.tstest/git-gap-census.test.tstest/git-gap-direction-evidence-d4.test.tstest/google-connector-facts-fence-7292.test.ts+test/google-withdrawn-fence-carry-8645.test.tstest/grade-takes-window.test.tstest/graduation-copy.test.tstest/graduation-digest.test.ts