Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(connectors): carry the repaired trust_tier marker through a connector re-render (#8052)
garrytan521b934eec59
diff --git a/src/core/persistence/connector-sync.ts b/src/core/persistence/connector-sync.tsindex 3cc1488d28..be935078e6 100644--- a/src/core/persistence/connector-sync.ts+++ b/src/core/persistence/connector-sync.ts@@ -1179,12 +1179,15 @@ export async function prepareConnectorMutation(engine: BrainEngine, row: WriteRe? serializePageToMarkdown({ ...snapshot.page, ...parsed, compiled_truth: fenced, timeline: carried.timeline, type: parsed.typeExplicit ? parsed.type : snapshot.page.type }, parsed.tags): p.content;let prepared: PreparedContentImport | undefined;+ const tier = 'external_untrusted';const result = await importFromContent(engine, row.slug, content, { sourceId: row.source_id, sourcePath: p.sourcePath,filename: basename(p.sourcePath).replace(/\.mdx?$/i, ''), noEmbed: true, allowEmptyOverwrite: true, activePack,// #5575 A3: connector free text (mail bodies, invite descriptions, issue text) is external and gated.- writeGate: { tier: 'external_untrusted', origin: { channel: `connector:${p.connector}`, connector: String(p.connector), source_uri: p.sourcePath }, requestId: row.id },+ writeGate: { tier, origin: { channel: `connector:${p.connector}`, connector: String(p.connector), source_uri: p.sourcePath }, requestId: row.id },prepareFrontmatter: page => {if (snapshot?.page.frontmatter.visibility === 'private') page.frontmatter.visibility = 'private';+ // #8052: keep the marker `gbrain repair provenance` stamped, only when it names the tier this import assigns.+ if (snapshot?.page.frontmatter.trust_tier === tier) page.frontmatter.trust_tier = tier;},prepare: async value => { prepared = value; return value.result; } });if (!prepared || prepared.slug !== row.slug) throw connectorPublicationRefusal('revision_conflict', result.error ?? 'A different page owns this connector content.', row,diff --git a/test/connector-trust-marker-8052.test.ts b/test/connector-trust-marker-8052.test.tsnew file mode 100644index 0000000000..fb89baffb7--- /dev/null+++ b/test/connector-trust-marker-8052.test.ts@@ -0,0 +1,80 @@+/**+ * #8052: `gbrain repair provenance --apply` stamps a connector page's stored tier as its `trust_tier` frontmatter+ * marker (`restamp`, #7918), and the next managed connector import re-rendered the page without it, so doctor+ * `provenance_stamp_drift` listed the page again after every import and could never reach zero on a brain with+ * active connector sources.+ *+ * Protects: the managed connector re-render carries a stored `trust_tier` marker forward when it names the tier the+ * connector import itself assigns (`external_untrusted`), so the repaired page stays clear; a stored marker naming+ * any other tier is not carried, so a stored marker can never raise what the import assigns.+ * Seams: none; the real managed Google importer with a fake People API, PGLite always and Postgres when+ * DATABASE_URL is set.+ */+import { afterAll, beforeAll, expect, test } from 'bun:test';+import type { BrainEngine } from '../src/core/engine.ts';+import { parseGoogleSourceConfig, runGoogleSync } from '../src/core/google/google-source.ts';+import { scanProvenanceDrift } from '../src/core/trust/provenance-drift.ts';+import { repairPageMarker } from '../src/core/trust/page-handlers.ts';+import { contact, createConnectorFixture, googleConfig, json, options, withGoogleAccount } from './helpers/connector-fixture.ts';+import { withEnv } from './helpers/with-env.ts';++const { engines, env, source, setup, teardown } = createConnectorFixture();+beforeAll(setup, 120_000);+afterAll(teardown);++async function connectorSource(engine: BrainEngine) {+ const f = await source(engine, googleConfig);+ let generation = 0;+ const fetcher = withGoogleAccount(async (url: string) => {+ if (url.includes('/settings/sendAs')) return json({ sendAs: [] });+ if (url.includes('/people/me/connections')) {+ return json({ connections: [{ ...contact('c1', 'Alice Example'), organizations: [{ name: `Acme Example ${generation}` }] }], nextSyncToken: `token-${generation}` });+ }+ throw new Error(`Unexpected Google fixture route: ${url}`);+ }, googleConfig.g_account);+ const cfg = parseGoogleSourceConfig(googleConfig, f.dir);+ const sync = async () => {+ const result = await runGoogleSync(engine, f.id, cfg, options, fetcher);+ expect(result.failedFiles ?? 0).toBe(0);+ return result;+ };+ await sync();+ const [page] = await engine.executeRaw<{ slug: string }>('SELECT slug FROM pages WHERE source_id=$1 AND deleted_at IS NULL', [f.id]);+ const read = async () => (await engine.executeRaw<{ trust_tier: string; marker: string | null; org: string }>(+ "SELECT trust_tier, frontmatter->>'trust_tier' AS marker, compiled_truth AS org FROM pages WHERE source_id=$1 AND slug=$2", [f.id, page!.slug]))[0]!;+ const resync = async () => { generation++; await sync(); expect((await read()).org).toContain(`Acme Example ${generation}`); };+ return { id: f.id, slug: page!.slug, read, resync };+}++test('a repaired connector page keeps its trust_tier marker across a re-import, and the drift check stays clear', async () => withEnv(env, async () => {+ for (const engine of engines) {+ const s = await connectorSource(engine);+ expect(await s.read()).toMatchObject({ trust_tier: 'external_untrusted', marker: null });+ const before = await scanProvenanceDrift(engine, s.id);+ expect(before.marker_missing).toBe(1);++ const item = before.items.find(i => i.slug === s.slug)!;+ expect(item.verdict.class).toBe('restamp');+ await repairPageMarker(engine, { sourceId: s.id, slug: s.slug, expectedRevision: item.revision, confirmation: { via: 'preview_hash' } });+ expect(await s.read()).toMatchObject({ trust_tier: 'external_untrusted', marker: 'external_untrusted' });+ expect((await scanProvenanceDrift(engine, s.id)).marker_missing).toBe(0);++ await s.resync();+ expect(await s.read()).toMatchObject({ trust_tier: 'external_untrusted', marker: 'external_untrusted' });+ const after = await scanProvenanceDrift(engine, s.id);+ expect(after.marker_missing).toBe(0);+ expect(after.items).toEqual([]);+ }+}), 180_000);++test('a stored marker naming a tier above the connector tier is not carried into the re-render', async () => withEnv(env, async () => {+ for (const engine of engines) {+ const s = await connectorSource(engine);+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');+ await engine.executeRaw(`UPDATE pages SET frontmatter = frontmatter || '{"trust_tier":"agent_written"}'::jsonb WHERE source_id=$1 AND slug=$2`, [s.id, s.slug]);+ await engine.executeRaw('UPDATE persistence_brain SET enabled=true WHERE singleton=1');+ expect((await s.read()).marker).toBe('agent_written');+ await s.resync();+ expect(await s.read()).toMatchObject({ trust_tier: 'external_untrusted', marker: null });+ }+}), 180_000);diff --git a/test/e2e/connector-trust-marker-8052.test.ts b/test/e2e/connector-trust-marker-8052.test.tsnew file mode 100644index 0000000000..71486907a4--- /dev/null+++ b/test/e2e/connector-trust-marker-8052.test.ts@@ -0,0 +1,4 @@+import { registerPostgresTests } from '../helpers/test-backends.ts';++// #8052: the PostgreSQL arm of the connector re-render keeping the repaired trust_tier marker.+await registerPostgresTests(() => import('../connector-trust-marker-8052.test.ts'));