Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(trust): agent edits stop stamping trust_tier: unknown on legacy pages (#6550)
time-attackeb0aee5b3573
diff --git a/src/core/trust/page-write.ts b/src/core/trust/page-write.tsindex 169706ccca..8bf40fb58b 100644--- a/src/core/trust/page-write.ts+++ b/src/core/trust/page-write.ts@@ -6,7 +6,8 @@* frontmatter markers (they can only lower).* - CEO-21: an ordinary content write below operator_curated stamps the* lower-only `trust_tier` marker into the frontmatter it publishes, at the- * lower of the write's tier and the page's stored tier, so the tier survives+ * lower of the write's tier and the page's stored tier (an unclassified+ * `unknown` row does not lower it, #6550), so the tier survives* the git round trip (sync honors it; an owner restamp needs the marker gone).* - ENG-1: gbrain-managed fence edits (remember / takes fence appends, proposal* accept strikes, loop and relink maintenance) and add_timeline_entry rewrite@@ -60,10 +61,15 @@ export async function storedPageTier(engine: BrainEngine, sourceId: string, slugreturn row ? storedTrustTier(row.trust_tier) : null;}-/** CEO-21: the marker an ordinary content write stamps (in place); `stored` is the page's tier before the write. */+/**+ * CEO-21: the marker an ordinary content write stamps (in place); `stored` is the page's tier before the write.+ * #6550: a stored `unknown` means "not classified yet", not "known unverified", so it does not lower the+ * marker: the marker names the writer's tier and the row keeps min(writer, stored) until the backfill+ * classifies it. An explicit `trust_tier: unknown` already in the frontmatter still caps (stampTrustMarker).+ */export function stampPageTrustMarker(row: Pick<WriteRequest, 'operation'>, frontmatter: Record<string, unknown>, trust: WriteTrust | undefined, stored: TrustTier | null): void {if (!trust || !CONTENT_OPERATIONS.includes(row.operation)) return;- const tier = stored ? minTrust(trust.tier, stored) : trust.tier;+ const tier = stored && stored !== 'unknown' ? minTrust(trust.tier, stored) : trust.tier;const stamped = stampTrustMarker(frontmatter, tier);if (stamped !== frontmatter) frontmatter.trust_tier = stamped.trust_tier;}diff --git a/test/trust-channel-writes.test.ts b/test/trust-channel-writes.test.tsindex 75a88aec82..51f2ba5e83 100644--- a/test/trust-channel-writes.test.ts+++ b/test/trust-channel-writes.test.ts@@ -255,6 +255,39 @@ describe('channel tiers on journaled writes', () => {}}), 90_000);+ test('#6550: an agent edit of an unclassified legacy page stamps the writer tier, and the round trip after backfill files no lower_page', async () => withEnv({ GBRAIN_HOME: home }, async () => {+ for (const engine of engines) {+ const b = await brain(engine);+ const slug = 'notes/legacy-example';+ await run(b.local, 'put_page', { slug, content: page('Legacy', 'Written before trust tiers.') });+ await b.engine.transaction(tx => withCoordinatedWrite(tx, [b.sourceId], () => tx.executeRaw(+ `UPDATE pages SET trust_tier='unknown', frontmatter = frontmatter - 'trust_tier' - 'source_kind' - 'ingested_via' WHERE source_id=$1 AND slug=$2`,+ [b.sourceId, slug]), TEST_WRITE_ATTRIBUTION));+ const edit = async (body: string) => {+ const got = await run(b.remote, 'get_page', { slug, include_content: true });+ const content = String(got.content ?? got.markdown ?? '');+ expect(content).toContain('title: Legacy');+ return run(b.remote, 'put_page', { slug, content: content.replace(/\n[^\n]*\n?$/, `\n${body}\n`), force: true });+ };+ await edit('Edited by an agent.');+ const first = await pageRow(b, slug);+ expect(first.frontmatter.trust_tier).toBe('agent_written');+ expect(first.trust_tier).toBe('unknown');+ // The backfill classifies the row from its signals (the agent write request).+ await b.engine.transaction(tx => withCoordinatedWrite(tx, [b.sourceId], () => withTrustBackfill(tx, () => tx.executeRaw(+ `UPDATE pages SET trust_tier='agent_written' WHERE source_id=$1 AND slug=$2`, [b.sourceId, slug])), TEST_WRITE_ATTRIBUTION));+ const second = await edit('Edited again after the backfill.');+ expect(second.trust_lowered).toBeUndefined();+ expect((await pageRow(b, slug)).trust_tier).toBe('agent_written');+ expect(await listTrustProposals(engine, { sourceId: b.sourceId, action: 'lower_page' })).toHaveLength(0);+ // An explicit unknown marker stays a lower-only cap.+ await run(b.remote, 'put_page', { slug: 'notes/marked-unknown', content: page('Marked', 'Unverified on purpose.', 'trust_tier: unknown\n') });+ const marked = await pageRow(b, 'notes/marked-unknown');+ expect(marked.trust_tier).toBe('unknown');+ expect(marked.frontmatter.trust_tier).toBe('unknown');+ }+ }), 90_000);+test('CEO-12: an agent rewrite of an owner page lowers it and files one lower_page proposal that later edits fold into', async () => withEnv({ GBRAIN_HOME: home }, async () => {for (const engine of engines) {const b = await brain(engine);diff --git a/test/trust-unknown-marker.test.ts b/test/trust-unknown-marker.test.tsnew file mode 100644index 0000000000..5a8df36334--- /dev/null+++ b/test/trust-unknown-marker.test.ts@@ -0,0 +1,26 @@+/** #6550: an unclassified (`unknown`) stored tier never becomes a permanent `trust_tier: unknown` marker. */+import { expect, test } from 'bun:test';+import { pageWriteTrust, stampPageTrustMarker } from '../src/core/trust/page-write.ts';++const row = { id: 'r1', operation: 'put_page', authority: { remote: true }, intent: {} } as never;++test('an agent write to an unclassified legacy page stamps the writer tier, so the round trip keeps it', () => {+ const first = pageWriteTrust(row, {});+ expect(first?.tier).toBe('agent_written');+ const fm: Record<string, unknown> = {};+ stampPageTrustMarker(row, fm, first, 'unknown');+ expect(fm.trust_tier).toBe('agent_written');+ expect(pageWriteTrust(row, fm)?.tier).toBe('agent_written');+});++test('a classified lower stored tier and an explicit unknown marker still lower the marker', () => {+ const trust = pageWriteTrust(row, {});+ const external: Record<string, unknown> = {};+ stampPageTrustMarker(row, external, trust, 'external_untrusted');+ expect(external.trust_tier).toBe('external_untrusted');+ const explicit: Record<string, unknown> = { trust_tier: 'unknown' };+ const capped = pageWriteTrust(row, explicit);+ expect(capped?.tier).toBe('unknown');+ stampPageTrustMarker(row, explicit, capped, 'unknown');+ expect(explicit.trust_tier).toBe('unknown');+});