Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
feat(trust): doctor trust_tiers reports the external share of derived rows (CEO-24 diagnostic); attribution inventory lists the three derivers as attributed (#5575 L1a-taint)
garrytan0d8820e31c10
diff --git a/docs/architecture/system-of-record.md b/docs/architecture/system-of-record.mdindex 1558f29f7c..48815da859 100644--- a/docs/architecture/system-of-record.md+++ b/docs/architecture/system-of-record.md@@ -167,8 +167,8 @@ are stored. Names, operations and times are joined at read time by[write attribution](../mcp/ADMIN.md#write-attribution). `get_versions` returnsattribution only to trusted local and `admin` callers. A NULL request with aprincipal is a maintenance write; all NULL is `unrecorded`: written before-attribution existed or by one of the three writers listed under "unattributed"-below. Attribution names the creator and the last writer; it is not an audit+attribution existed (older releases also left `gbrain extract` timeline walks,+meeting timeline extraction and enrichment unattributed). Attribution names the creator and the last writer; it is not an auditlog of every write in between.Nothing is inferred for older rows. `gbrain repair attribution-backfill` fills@@ -195,14 +195,12 @@ start `unrecorded`; version history is not copied).whole run. A row such a writer changes keeps its creator and moves its lastwriter. The maintenance principal is the local CLI registration(`local_cli`), else this host's identity (`application`, `host:<id>`), else- `host:unregistered`: attribution never creates an identity file. Only the- three writers listed under "unattributed" below write without an actor.+ `host:unregistered`: attribution never creates an identity file. No direct+ writer is left unattributed.Agents: treat `NULL` attribution on an unmanaged brain as "written before-attribution existed or by `gbrain extract`, meeting timeline extraction or-enrichment", not as evidence of tampering. To get attribution for those-writers now, run the work on a managed brain or through a journaled-operation. When you add a direct writer, run it in+attribution existed, or before `gbrain extract`, meeting timeline extraction+and enrichment were attributed (#5575)", not as evidence of tampering. When you add a direct writer, run it in`maintenanceTransaction(engine, fn)` (`persistence/attribution.ts`) or route itthrough the coordinator, then update these lists with the count`test/write-attribution-legacy.test.ts` reports. The test compares both lists@@ -214,7 +212,7 @@ Attributed (inside a request, a coordinated scope or `maintenanceTransaction`,or a physical projection that leaves attribution untouched):<!-- write-attribution-covered:start -->-- `src/commands/extract-conversation-facts.ts` (3): the conversation fact index writes through `writeDerivedFacts`: one `maintenanceTransaction` on unmanaged brains, `withDerivedFactsWrite` on managed brains.+- `src/commands/extract-conversation-facts.ts` (2): the conversation fact index's audit rows write through `writeDerivedFacts` (one `maintenanceTransaction` on unmanaged brains, `withDerivedFactsWrite` on managed brains); its extracted rows go through `insertDerivedFacts` (`persistence/derived-facts.ts`), which stamps the page's taint and runs the write gate.- `src/commands/extract-timeline-db.ts` (2): `gbrain extract timeline --source db` commits one `maintenanceTransaction` per 100-row batch on unmanaged brains; managed brains publish per page through a maintenance request.- `src/commands/migrate-engine.ts` (5): engine copy keeps the source rows' attribution values verbatim.- `src/commands/sync/holds.ts` (1): a full sync's move of a held rename (`updateSlug` and its `source_path`) runs in one `maintenanceTransaction`; managed sources publish renames through the sync request preparer.@@ -228,7 +226,9 @@ or a physical projection that leaves attribution untouched):- `src/core/cycle/extract-atoms-page-state.ts` (1): the atom completion stamp runs in `maintenanceTransaction`.- `src/core/cycle/extract-atoms.ts` (1): stale atom retirement soft-deletes in 500-page `maintenanceTransaction` chunks.- `src/core/cycle/extract-facts.ts` (7): the unmanaged page reconcile and the expiry of fence facts whose page was soft-deleted run in `maintenanceTransaction`; managed brains write through `withDerivedFactsWrite`.-- `src/core/cycle/extract-takes.ts` (2): coordinated per page on managed brains; the unmanaged batch upsert runs in `maintenanceTransaction`.+- `src/core/enrichment-service.ts` (2): the enrichment stub fallback write and its auto-timeline entry run in `maintenanceTransaction` at the source page's derived tier (the stub import is lowered to it afterwards).+- `src/core/extract-timeline-from-meetings.ts` (1): each meeting timeline batch runs in `maintenanceTransaction`, one meeting tier per batch.+- `src/core/cycle/extract-takes.ts` (1): one projected-takes writer stamps each page's tier and runs the write gate: coordinated per page on managed brains, one `maintenanceTransaction` per page tier for the unmanaged batch upsert.- `src/core/cycle/grade-takes.ts` (1): unmanaged take auto-resolution runs in `maintenanceTransaction`; managed brains publish a `takes_resolve` request.- `src/core/cycle/phantom-redirect.ts` (3): the unmanaged phantom fact move, canonical body refresh and phantom soft delete run in `maintenanceTransaction`; the managed redirect moves facts inside its coordinated write.- `src/core/cycle/phases/consolidate.ts` (4): unmanaged consolidation's take insert or re-promotion, consolidation marks and valid_until writeback run in one `derivedMaintenanceTransaction` per cluster at the cluster's derived tier; managed brains submit a maintenance consolidation request.@@ -244,6 +244,7 @@ or a physical projection that leaves attribution untouched):- `src/core/facts/relink-publish.ts` (2): `relink_facts` request preparer.- `src/core/facts/unfenced-facts.ts` (2): the cycle and v0.32.2 fence of `row_num`-NULL facts: a maintenance request on managed brains; the unmanaged body mirror and row-number stamp run in `maintenanceTransaction`.- `src/core/facts/withdrawal.ts` (1): runs inside the `forget` request's coordinated write.+- `src/core/trust/timeline-projection.ts` (2): `gbrain extract` timeline walks (file, incremental, stale) and the per-entry fallback write through it: one `maintenanceTransaction` per page tier.- `src/core/facts/write-single.ts` (2): the legacy single-fact insert and its supersession link each run in `maintenanceTransaction`; managed brains publish through the fact request.- `src/core/import-file.ts` (11): direct markdown, code and image imports, the moved-file rename (`updateSlug`) and the #3694 legacy-hash re-stamp run in `maintenanceTransaction`; managed imports go through the page request preparer.- `src/core/minions/handlers/ingest-capture.ts` (1): the capture ingest tombstone soft delete runs in `maintenanceTransaction`.@@ -256,7 +257,7 @@ or a physical projection that leaves attribution untouched):- `src/core/persistence/canonical-projections.ts` (7): fence projection inside the request's publication.- `src/core/persistence/connector-google-receipts.ts` (2): request preparer.- `src/core/persistence/connector-sync.ts` (2): connector publication inside `withCoordinatedWrite`.-- `src/core/persistence/derived-facts.ts` (1): `withDerivedFactsWrite` (managed derived facts); `writeDerivedFacts` runs legacy writers in `maintenanceTransaction` on unmanaged brains.+- `src/core/persistence/derived-facts.ts` (1): `withDerivedFactsWrite` (managed derived facts); `writeDerivedFacts` runs legacy writers in `maintenanceTransaction` on unmanaged brains; the gated conversation fact insert stamps the page's taint.- `src/core/persistence/facts-prepare.ts` (3): managed `extract_facts` request preparer.- `src/core/persistence/file-repair.ts` (2): `managed_file_repair` request preparer (#5988).- `src/core/persistence/grandfather.ts` (2): request preparer.@@ -287,14 +288,12 @@ or a physical projection that leaves attribution untouched):- `src/core/timeline-write-through.ts` (2): the timeline write-through's page row splice and entry insert run in one `maintenanceTransaction`.<!-- write-attribution-covered:end -->-Unattributed (on unmanaged brains these write with `NULL` attribution). These-three writers share the link-extraction and mention-linking code, and they are-routed together with that code's rework:+Unattributed (on unmanaged brains these would write with `NULL` attribution).+The list is empty: `gbrain extract` timeline walks, enrichment and meeting+timeline extraction run as tiered derivers since #5575 (I2). A new writer+that cannot be attributed yet is declared here:<!-- write-attribution-unattributed:start -->-- `src/commands/extract.ts` (4): `gbrain extract` timeline walks (file, incremental, stale) and the per-entry fallback.-- `src/core/enrichment-service.ts` (2): enrichment page and auto-timeline entry.-- `src/core/extract-timeline-from-meetings.ts` (1): meeting timeline batch.<!-- write-attribution-unattributed:end -->## The privacy boundarydiff --git a/src/commands/doctor/checks/trust-tiers.ts b/src/commands/doctor/checks/trust-tiers.tsindex 4be7c54cd2..108c71a6ca 100644--- a/src/commands/doctor/checks/trust-tiers.ts+++ b/src/commands/doctor/checks/trust-tiers.ts@@ -6,12 +6,16 @@* After a complete backfill, rows no signal could classify stay `unknown`* and the check is ok with the percentage. A brain before the trust* migration is ok (tiers arrive with it). Read-only.+ *+ * CEO-24 diagnostic (never changes the status): the share of derived rows+ * (`write_origin` carries `taint_inputs`) stamped external_untrusted.*/import type { Check } from '../../doctor.ts';import { agentFix, checkError } from '../check-fix.ts';import { connectedEngine, type DoctorContext, type DoctorEntry } from '../context.ts';import { readTrustBackfillState, readTrustTierCounts } from '../../../core/trust/backfill.ts';import { TRUST_TIERS } from '../../../core/trust/tier.ts';+import { readDerivedTaintShare } from '../../../core/trust/taint.ts';import { isUndefinedColumnError } from '../../../core/utils.ts';@@ -24,19 +28,21 @@ async function runTrustTiers(ctx: DoctorContext): Promise<Check[]> {const rows = tables.reduce((n, t) => n + t.rows, 0);const unknownPct = rows === 0 ? 0 : Math.round((totals.unknown / rows) * 10_000) / 100;const state = await readTrustBackfillState(engine);+ const derived = await readDerivedTaintShare(engine);const details = { rows, by_tier: totals, unknown_pct: unknownPct, tables: tables.map(t => ({ table: t.table, rows: t.rows, by_tier: t.counts })),- backfill: { interrupted: state.interrupted, completed_at: state.completedAt } };+ backfill: { interrupted: state.interrupted, completed_at: state.completedAt }, derived };+ const derivedNote = derived.rows ? ` Derived rows: ${derived.external_pct}% of ${derived.rows} carry an external, untrusted tier (diagnostic).` : '';const needsBackfill = totals.unknown > 0 && (state.interrupted || !state.completedAt);if (!needsBackfill) {checks.push({ name: 'trust_tiers', status: 'ok', details,message: rows === 0 ? 'No facts, takes, timeline entries or pages yet.'- : `${unknownPct}% of ${rows} row(s) have an unverified origin (unknown tier)${state.completedAt ? '; the backfill has classified every row it has a signal for' : ''}.` });+ : `${unknownPct}% of ${rows} row(s) have an unverified origin (unknown tier)${state.completedAt ? '; the backfill has classified every row it has a signal for' : ''}.${derivedNote}` });return checks;}const argv = state.interrupted ? ['gbrain', 'trust', 'backfill', '--resume'] : ['gbrain', 'trust', 'backfill'];checks.push({ name: 'trust_tiers', status: 'warn', details,message: `${totals.unknown} of ${rows} row(s) (${unknownPct}%) have an unverified origin and the trust backfill has ${state.interrupted ? 'not finished' : 'not run'}. `- + `Run ${argv.join(' ')} on the brain host (gbrain trust backfill --dry-run previews it); it classifies rows from deterministic signals only and never marks anything confirmed.`,+ + `Run ${argv.join(' ')} on the brain host (gbrain trust backfill --dry-run previews it); it classifies rows from deterministic signals only and never marks anything confirmed.${derivedNote}`,fix: agentFix(argv, 'Classifies legacy rows from deterministic signals in resumable batches; it never raises a row to confirmed and changes no content.', 'trust_tiers',{ preview_argv: ['gbrain', 'trust', 'backfill', '--dry-run', '--json'] }) });} catch (err) {diff --git a/src/core/trust/taint.ts b/src/core/trust/taint.tsindex 3deaba7b0f..45ddc902a9 100644--- a/src/core/trust/taint.ts+++ b/src/core/trust/taint.ts@@ -202,3 +202,21 @@ export function declaredWriteTrust(declaration: DerivationDeclaration | null, ceexport function isExternalTier(tier: TrustTier): boolean {return compareTrust(tier, 'external_untrusted') <= 0;}++/**+ * CEO-24 diagnostic (never gated): how many derived rows (those whose+ * `write_origin` carries `taint_inputs`) are stamped external_untrusted, across+ * facts, takes, timeline entries and pages. One scan per table, like the tier+ * counts beside it in doctor.+ */+export async function readDerivedTaintShare(engine: Sql): Promise<{ rows: number; external_untrusted: number; external_pct: number }> {+ let rows = 0, external = 0;+ for (const table of ['facts', 'takes', 'timeline_entries', 'pages'] as const) {+ const [row] = await engine.executeRaw<{ derived: number | string; external: number | string }>(+ `SELECT count(*) FILTER (WHERE write_origin ? 'taint_inputs') AS derived,+ count(*) FILTER (WHERE write_origin ? 'taint_inputs' AND trust_tier = 'external_untrusted') AS external FROM ${table}`);+ rows += Number(row?.derived ?? 0);+ external += Number(row?.external ?? 0);+ }+ return { rows, external_untrusted: external, external_pct: rows === 0 ? 0 : Math.round((external / rows) * 10_000) / 100 };+}diff --git a/test/fixtures/goldens/doctor/json-pglite-degraded.json b/test/fixtures/goldens/doctor/json-pglite-degraded.jsonindex 9db1ed3079..f2d0e2fc55 100644--- a/test/fixtures/goldens/doctor/json-pglite-degraded.json+++ b/test/fixtures/goldens/doctor/json-pglite-degraded.json@@ -1673,6 +1673,11 @@"unknown": 0,"user_confirmed": 0},+ "derived": {+ "external_pct": 0,+ "external_untrusted": 0,+ "rows": 0+ },"rows": 0,"tables": [{@@ -1839,7 +1844,7 @@},{"category": "brain",- "message": "1 retrieval-affecting file(s) changed since HEAD: src/core/operations.ts. Re-run `gbrain eval run-all` after committing these changes.",+ "message": "No retrieval-affecting files changed in working tree.","name": "eval_drift","status": "ok"},diff --git a/test/fixtures/goldens/doctor/json-pglite-fresh.json b/test/fixtures/goldens/doctor/json-pglite-fresh.jsonindex e50ce60e31..9cd6dc6ad2 100644--- a/test/fixtures/goldens/doctor/json-pglite-fresh.json+++ b/test/fixtures/goldens/doctor/json-pglite-fresh.json@@ -1750,6 +1750,11 @@"unknown": 0,"user_confirmed": 0},+ "derived": {+ "external_pct": 0,+ "external_untrusted": 0,+ "rows": 0+ },"rows": 0,"tables": [{@@ -1916,7 +1921,7 @@},{"category": "brain",- "message": "1 retrieval-affecting file(s) changed since HEAD: src/core/operations.ts. Re-run `gbrain eval run-all` after committing these changes.",+ "message": "No retrieval-affecting files changed in working tree.","name": "eval_drift","status": "ok"},