Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(trust): legacy extract_atoms atoms classify as agent_written (#6533)

time-attack13667f8eb38b
diff --git a/src/core/trust/backfill.ts b/src/core/trust/backfill.ts
index 1fcf544edf..88489168f3 100644
--- a/src/core/trust/backfill.ts
+++ b/src/core/trust/backfill.ts
@@ -11,8 +11,9 @@
* - pages.source_kind: webhook / file-watcher / inbox-folder / cron-scheduler
* (ingestion captures) external_untrusted; mcp:* / put_page / capture-cli
* agent_written;
- * - frontmatter transcript_import, provenance: auto-extracted, dream_generated:
- * agent_written (model- or session-derived);
+ * - frontmatter transcript_import, provenance: auto-extracted, dream_generated,
+ * extracted_by: extract_atoms-* (legacy atoms, #6533): agent_written (model-
+ * or session-derived);
* - facts.source lane tags (the facts backstop, conversation-facts and
* extract_facts lanes) and takes.source take_proposals#: agent_written;
* - post-v193 rows through write_request_id -> persistence_requests (CEO-17):
@@ -42,7 +43,7 @@ import { CONNECTOR_SOURCE_KINDS } from '../persistence/connector-identity.ts';
import { withCoordinatedWrite, withTrustBackfill } from '../persistence/context.ts';
import { maintenanceAttribution } from '../persistence/attribution.ts';
import { readManagedMode } from '../persistence/managed-mode.ts';
-import { frontmatterTrustCapsSql } from './channel.ts';
+import { EXTRACT_ATOMS_EXTRACTOR_PREFIX, frontmatterTrustCapsSql } from './channel.ts';
import { claimedSourceSql, legacyRowSql, notConnectorSourceSql, readLegacyCeilings, type LegacyCeilings } from './claim-state.ts';
import { TRUST_BACKFILL_COMPLETED_KEY, type TrustTable } from './schema.ts';
import { TRUST_TIER_RANK, TRUST_TIERS, isTrustTier, trustRankSql, trustTierFromRankSql, type TrustTier } from './tier.ts';
@@ -163,7 +164,9 @@ function classifier(cols: Columns, assumeClaimed: readonly string[] = [], ceilin
if (cols.has('pages', 'frontmatter')) {
signals.add('frontmatter_provenance');
terms.push(`CASE WHEN ${p}.frontmatter ? 'transcript_import' OR ${p}.frontmatter->>'provenance' = 'auto-extracted'
- OR ${p}.frontmatter->>'dream_generated' = 'true' THEN ${rank('agent_written')} END`);
+ OR ${p}.frontmatter->>'dream_generated' = 'true'
+ OR (CASE WHEN jsonb_typeof(${p}.frontmatter->'extracted_by') = 'string' THEN btrim(${p}.frontmatter->>'extracted_by', E' \\t\\n\\r') END)
+ LIKE '${EXTRACT_ATOMS_EXTRACTOR_PREFIX.replace(/_/g, '\\_')}%' THEN ${rank('agent_written')} END`);
}
if (cols.has('pages', 'revision_write_request_id')) { const q = request(r); if (q) terms.push(q); }
return terms;
diff --git a/src/core/trust/channel.ts b/src/core/trust/channel.ts
index ee772050e9..b3cff4e2b6 100644
--- a/src/core/trust/channel.ts
+++ b/src/core/trust/channel.ts
@@ -105,6 +105,12 @@ export function requestChannelTrust(row: Pick<WriteRequest, 'id' | 'operation' |
const str = (value: unknown) => (typeof value === 'string' ? value.trim() : '');
+/**
+ * #6533: the `extracted_by` prefix extract_atoms stamps on every atom it writes. Legacy atoms
+ * (before `dream_generated`) carry only this, so it is an agent_written signal on its own.
+ */
+export const EXTRACT_ATOMS_EXTRACTOR_PREFIX = 'extract_atoms';
+
/** The tier a single channel stamp names (`source_kind`, `ingested_via`, `captured_via`), or undefined. */
function stampTier(value: unknown): TrustTier | undefined {
const v = str(value);
@@ -135,7 +141,8 @@ export function frontmatterTrustCaps(frontmatter: Record<string, unknown> | null
if (str(fm.source_url) && (str(fm.clipped_at) || str(fm.clipper) || str(fm.captured_via) === 'clipper' || str(fm.type) === 'clipping')) {
caps.push('external_untrusted');
}
- if (str(fm.provenance) === 'auto-extracted' || fm.dream_generated === true || str(fm.dream_generated) === 'true') caps.push('agent_written');
+ if (str(fm.provenance) === 'auto-extracted' || fm.dream_generated === true || str(fm.dream_generated) === 'true'
+ || str(fm.extracted_by).startsWith(EXTRACT_ATOMS_EXTRACTOR_PREFIX)) caps.push('agent_written');
return caps;
}
@@ -160,7 +167,7 @@ export function frontmatterTrustCapsSql(fm: string): string[] {
`CASE WHEN ${text('source_url')} IS NOT NULL AND (${text('clipped_at')} IS NOT NULL OR ${text('clipper')} IS NOT NULL
OR ${text('captured_via')} = 'clipper' OR ${text('type')} = 'clipping') THEN ${TRUST_TIER_RANK.external_untrusted} END`,
`CASE WHEN ${text('provenance')} = 'auto-extracted' OR ${fm}->'dream_generated' = 'true'::jsonb OR ${text('dream_generated')} = 'true'
- THEN ${TRUST_TIER_RANK.agent_written} END`,
+ OR ${text('extracted_by')} LIKE '${EXTRACT_ATOMS_EXTRACTOR_PREFIX.replace(/_/g, '\\_')}%' THEN ${TRUST_TIER_RANK.agent_written} END`,
];
}
diff --git a/test/trust-backfill.test.ts b/test/trust-backfill.test.ts
index fcc527d965..47aa6a8d85 100644
--- a/test/trust-backfill.test.ts
+++ b/test/trust-backfill.test.ts
@@ -64,6 +64,7 @@ async function seed(engine: BrainEngine) {
await addPage('dream', src, null, { dream_generated: true });
await addPage('transcript', src, null, { transcript_import: { harness: 'codex', session_id: 's1' } });
await addPage('extracted', src, null, { provenance: 'auto-extracted' });
+ await addPage('atom', src, null, { type: 'atom', extracted_by: 'extract_atoms-v0.41.2.1', source_slug: 'p/plain' });
await addPage('plain', src, null);
await addPage('connector', connector, null);
@@ -98,7 +99,7 @@ async function seed(engine: BrainEngine) {
return { src, connector, pages, facts, take: Number(take!.id), entry: Number(entry!.id) };
}
type Fixture = Awaited<ReturnType<typeof seed>>;
-const EXPECTED_PAGES = { webhook: 'external_untrusted', mcp: 'agent_written', dream: 'agent_written', transcript: 'agent_written', extracted: 'agent_written', plain: 'unknown', connector: 'external_untrusted' };
+const EXPECTED_PAGES = { webhook: 'external_untrusted', mcp: 'agent_written', dream: 'agent_written', transcript: 'agent_written', extracted: 'agent_written', atom: 'agent_written', plain: 'unknown', connector: 'external_untrusted' };
const EXPECTED_FACTS = {
sync_request: 'operator_curated', put_page_request: 'agent_written', remote_request: 'agent_written', connector_request: 'external_untrusted',
other_request: 'unknown', compacted_request: 'unknown', lane: 'agent_written', fenced_external: 'external_untrusted', plain: 'unknown', connector: 'external_untrusted',
@@ -216,7 +217,7 @@ describe('trust backfill dry run before the trust migration', () => {
expect(Number(after!.n)).toBe(Number(before!.n));
expect(report.schema).toBe('pre_trust');
expect(report.tables.every(t => t.current === undefined)).toBe(true);
- expect(report.projected).toEqual({ user_confirmed: 0, operator_curated: 1, tool_observed: 0, agent_written: 8, unknown: 4, external_untrusted: 6 });
+ expect(report.projected).toEqual({ user_confirmed: 0, operator_curated: 1, tool_observed: 0, agent_written: 9, unknown: 4, external_untrusted: 6 });
expect(report.rows).toBe(Object.keys(f.pages).length + Object.keys(f.facts).length + 2);
expect((await doctor(engine)).details).toEqual({ schema: 'pre_trust' });
await expect(runTrustBackfill(engine, { log: quiet })).rejects.toMatchObject({ code: 'migrations_pending' });
diff --git a/test/trust-claim-legacy-atoms.test.ts b/test/trust-claim-legacy-atoms.test.ts
new file mode 100644
index 0000000000..863ebd1cd5
--- /dev/null
+++ b/test/trust-claim-legacy-atoms.test.ts
@@ -0,0 +1,30 @@
+import { afterAll, beforeAll, expect, test } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import type { BrainEngine } from '../src/core/engine.ts';
+import { liftClaimedSources, recordSourceClaims } from '../src/core/trust/claim.ts';
+import { runTrustBackfill } from '../src/core/trust/backfill.ts';
+import { WRITE_GATE_SCAN_BASELINE_KEY } from '../src/core/write-gate-schema.ts';
+
+let engine: BrainEngine;
+beforeAll(async () => {
+ const pg = new PGLiteEngine(); await pg.connect({}); await pg.initSchema(); engine = pg;
+ await engine.executeRaw('UPDATE config SET value = $2 WHERE key = $1', [WRITE_GATE_SCAN_BASELINE_KEY,
+ JSON.stringify({ detector_version: 0, until: { pages: 1e12, facts: 1e12, takes: 1e12, timeline_entries: 1e12 } })]);
+}, 120_000);
+afterAll(async () => { await engine.disconnect(); });
+
+test('#6533 claim never lifts a legacy extract_atoms page to operator_curated; backfill classifies it', async () => {
+ await engine.executeRaw(`INSERT INTO sources(id,name,local_path) VALUES('own','own','/home/alice-example/notes')`);
+ const ins = async (slug: string, fm: object) => Number((await engine.executeRaw<{ id: number }>(`INSERT INTO pages(slug,type,title,compiled_truth,source_id,frontmatter)
+ VALUES($1,'atom','T','An atom.', 'own', $2::text::jsonb) RETURNING id`, [slug, JSON.stringify(fm)]))[0]!.id);
+ const atom = await ins('atoms/2026-09-07/x', { type: 'atom', extracted_by: 'extract_atoms-v0.41.2.1', extracted_at: '2026-09-07T00:17:03.757Z', source_slug: 'originals/x' });
+ const unclaimed = await ins('atoms/2026-09-07/y', { type: 'atom', extracted_by: 'extract_atoms-v0.41.2.1', source_slug: 'originals/y' });
+ await engine.executeRaw(`INSERT INTO sources(id,name,local_path) VALUES('other','other','/home/alice-example/other')`);
+ await engine.executeRaw(`UPDATE pages SET source_id='other' WHERE id=$1`, [unclaimed]);
+ await runTrustBackfill(engine, { sources: ['other'] });
+ await recordSourceClaims(engine, ['own']);
+ await liftClaimedSources(engine);
+ const tier = async (id: number) => (await engine.executeRaw<{ t: string }>(`SELECT trust_tier t FROM pages WHERE id=$1`, [id]))[0]!.t;
+ expect(await tier(atom)).toBe('agent_written');
+ expect(await tier(unclaimed)).toBe('agent_written');
+});
diff --git a/test/trust-claim.test.ts b/test/trust-claim.test.ts
index e2e7b8e679..d600c9f0cf 100644
--- a/test/trust-claim.test.ts
+++ b/test/trust-claim.test.ts
@@ -105,6 +105,7 @@ async function seed() {
await page('marker', src, null, { trust_tier: 'agent_written' });
await page('confirmed-marker', src, null, { trust_tier: 'user_confirmed' });
await page('connector-stamp', src, null, { ingested_via: 'connector:gmail' });
+ await page('atom', src, null, { type: 'atom', extracted_by: 'extract_atoms-v0.41.2.1', source_slug: 'p/plain' });
await page('gh-plain', gh, null);
const fact = async (source: string, fenced: string | null) => {
const [row] = await engine.executeRaw<{ id: number }>(`INSERT INTO facts(source_id,entity_slug,fact,kind,source,source_markdown_slug,row_num)
@@ -138,6 +139,8 @@ describe('trust claim-sources', () => {
{ transcript_import: 'yes' }, { source_url: 'https://x', clipped_at: '2026' }, { source_url: 'https://x' }, { source_url: ' ', clipper: 'x' },
{ source_url: 'https://x', captured_via: 'clipper' }, { source_url: 'https://x', type: 'clipping' }, { provenance: 'auto-extracted' },
{ dream_generated: true }, { dream_generated: 'true' }, { dream_generated: false }, { provenance: 'manual', source_kind: 'inbox-folder', trust_tier: 'tool_observed' },
+ { extracted_by: 'extract_atoms-v0.41.2.1' }, { extracted_by: ' extract_atoms-v0.42 ' }, { extracted_by: 'extractXatoms-v1' },
+ { extracted_by: 'gpt-extractor' }, { extracted_by: 5 }, { extracted_by: 'extract_atoms-v0.41.2.1', trust_tier: 'external_untrusted' },
];
const sql = `SELECT LEAST(${frontmatterTrustCapsSql('v.fm').join(', ')}) AS r FROM (SELECT $1::text::jsonb AS fm) v`;
for (const fm of samples) {
@@ -155,12 +158,12 @@ describe('trust claim-sources', () => {
const { out } = await runCli(['--dry-run']);
expect(await snapshot(s.src)).toBe(before);
const own = listing.sources.find(x => x.id === s.src)!;
- expect(own).toMatchObject({ local_path: '/home/alice-example/notes', pages: 9, claimable: true, claimed: false });
+ expect(own).toMatchObject({ local_path: '/home/alice-example/notes', pages: 10, claimable: true, claimed: false });
expect(own.current!.unknown).toBe(own.legacy_unknown);
expect(own.projected_if_claimed.user_confirmed).toBe(0);
expect(own.projected_if_claimed.operator_curated).toBe(5);
expect(listing.sources.find(x => x.id === s.gh)).toMatchObject({ claimable: false, blocker: 'connector' });
- expect(out).toContain(`${s.src} (/home/alice-example/notes): 9 page(s)`);
+ expect(out).toContain(`${s.src} (/home/alice-example/notes): 10 page(s)`);
expect(out).toContain('github connector: cannot be claimed');
});
@@ -193,13 +196,13 @@ describe('trust claim-sources', () => {
plain: await tierOf('pages', p.plain!), mcp: await tierOf('pages', p.mcp!), capture: await tierOf('pages', p.capture!),
ownTranscript: await tierOf('pages', p['own-transcript']!), otherTranscript: await tierOf('pages', p['other-transcript']!),
clipped: await tierOf('pages', p.clipped!), marker: await tierOf('pages', p.marker!), confirmedMarker: await tierOf('pages', p['confirmed-marker']!),
- connectorStamp: await tierOf('pages', p['connector-stamp']!), gh: await tierOf('pages', p['gh-plain']!),
+ connectorStamp: await tierOf('pages', p['connector-stamp']!), atom: await tierOf('pages', p.atom!), gh: await tierOf('pages', p['gh-plain']!),
factPlain: await tierOf('facts', s.facts.plain), factBackstop: await tierOf('facts', s.facts.backstop),
factOnPlain: await tierOf('facts', s.facts.onPlain), factOnMcp: await tierOf('facts', s.facts.onMcp),
take: await tierOf('takes', s.take), entry: await tierOf('timeline_entries', s.entry),
}).toEqual({
plain: 'operator_curated', mcp: 'agent_written', capture: 'agent_written', ownTranscript: 'agent_written', otherTranscript: 'external_untrusted',
- clipped: 'external_untrusted', marker: 'agent_written', confirmedMarker: 'operator_curated', connectorStamp: 'external_untrusted', gh: 'unknown',
+ clipped: 'external_untrusted', marker: 'agent_written', confirmedMarker: 'operator_curated', connectorStamp: 'external_untrusted', atom: 'agent_written', gh: 'unknown',
factPlain: 'operator_curated', factBackstop: 'agent_written', factOnPlain: 'operator_curated', factOnMcp: 'agent_written',
take: 'operator_curated', entry: 'external_untrusted',
});