Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
feat(trust): I2 taint helpers and maintenance derivation declarations (#5575 L1a-taint, ENG-3, CEO-1, ENG-7)
garrytan94ecb09b04b4
diff --git a/src/core/persistence/prepared-maintenance.ts b/src/core/persistence/prepared-maintenance.tsindex d27d48fc07..8ec1a251f1 100644--- a/src/core/persistence/prepared-maintenance.ts+++ b/src/core/persistence/prepared-maintenance.ts@@ -23,6 +23,8 @@ import { nativeLockCapability } from './native-lock.ts';import { assertPhysicalRoot } from './physical-root.ts';import { isConnectorSourceKind } from './connector-identity.ts';import { MaintenanceWriteWait } from './maintenance-wait.ts';+import { withTrustKeep } from './context.ts';+import { declaredWriteTrust, lowerToDerivedTier, readDerivationDeclaration, recordTaintEdges, type DerivationDeclaration } from '../trust/taint.ts';export interface MaintenanceAuthority {writer: WriteAuthority;@@ -139,13 +141,15 @@ async function submitMaintenance(engine: BrainEngine, authority: MaintenanceAuth/** #5523: a Life Chronicle timeline row projected onto the depth page in the same publication. */export interface MaintenanceEventProjection { depth_slug: string; date: string; summary: string; }+/** `derivation` (#5575 I2): the deriver's taint declaration; the page and its edges are stamped from it at publication. */export async function publishMaintenancePage(engine: BrainEngine, authority: MaintenanceAuthority, slug: string,content: string, options: { requestId?: string; expectedRevision: string | null; file?: boolean;- eventProjection?: MaintenanceEventProjection }): Promise<Record<string, unknown>> {+ eventProjection?: MaintenanceEventProjection; derivation?: DerivationDeclaration }): Promise<Record<string, unknown>> {const projection = options.eventProjection ? { event_projection: options.eventProjection } : {};+ const derivation = options.derivation ? { derivation: options.derivation } : {};return submitMaintenance(engine, authority, slug, { kind: 'managed_maintenance_page', content,- expected_revision: options.expectedRevision, ...projection }, options.requestId ?? maintenanceRequestId({ authority: authority.writer,- slug, content, revision: options.expectedRevision, file: options.file ?? true, ...projection }), options.file);+ expected_revision: options.expectedRevision, ...projection, ...derivation }, options.requestId ?? maintenanceRequestId({ authority: authority.writer,+ slug, content, revision: options.expectedRevision, file: options.file ?? true, ...projection, ...derivation }), options.file);}/** A maintenance request with its own intent kind, keyed by the intent (a retry replays its receipt). */@@ -214,7 +218,8 @@ export async function readFacts(engine: BrainEngine, sourceId: string, ids: numb}export async function submitMaintenanceConsolidation(engine: BrainEngine, authority: MaintenanceAuthority,- slug: string, cluster: FactRow[], take: { claim: string; weight: number; source: string; since: string }): Promise<Record<string, unknown>> {+ slug: string, cluster: FactRow[], take: { claim: string; weight: number; source: string; since: string },+ derivation?: DerivationDeclaration): Promise<Record<string, unknown>> {const sourceId = authority.writer.sourceId;const facts = await readFacts(engine, sourceId, cluster.map(f => f.id));if (facts.length !== cluster.length || facts.some(f => f.value.visibility !== 'world' || f.value.expired_at || f.value.consolidated_at)) {@@ -238,7 +243,7 @@ export async function submitMaintenanceConsolidation(engine: BrainEngine, authorpages.push({ slug: pageSlug, revision: snapshot.revision, id: snapshot.page.id });}const target = pages.find(p => p.slug === slug)!;- const intent = { kind: 'managed_maintenance_consolidate', expected_revision: target.revision, facts, pages, ...take };+ const intent = { kind: 'managed_maintenance_consolidate', expected_revision: target.revision, facts, pages, ...take, ...(derivation ? { derivation } : {}) };const requestId = maintenanceRequestId({ source: authority.writer.sourceIncarnation, slug, intent });return submitMaintenance(engine, authority, slug, intent, requestId);}@@ -335,7 +340,33 @@ export async function applyPreservingTakeResolutions(tx: BrainEngine, pageId: nureturn outcome;}+/**+ * #5575 I2: a maintenance intent that carries a derivation declaration+ * publishes at the declared tier (never above agent_written), lowers its+ * derived row when the publication changed no content column, and records+ * the complete input edges (ENG-7). A consolidation rewrites the entity+ * page's takes fence without authoring the page, so the page keeps its tier.+ */export async function prepareMaintenanceMutation(engine: BrainEngine, row: WriteRequest, config: GBrainConfig): Promise<PreparedMutation> {+ const prepared = await prepareMaintenanceKind(engine, row, config);+ const declaration = readDerivationDeclaration(row.intent?.derivation);+ if (!declaration) return prepared;+ const trust = declaredWriteTrust(declaration);+ const consolidation = row.intent?.kind === 'managed_maintenance_consolidate';+ return { ...prepared, trust, apply: async (tx, preimage) => {+ const outcome = consolidation ? await withTrustKeep(tx, ['pages'], () => prepared.apply(tx, preimage)) : await prepared.apply(tx, preimage);+ const [derived] = consolidation ? (outcome.take_id ? [{ table: 'takes' as const, id: Number(outcome.take_id) }] : [])+ : (await tx.executeRaw<{ id: number }>('SELECT id FROM pages WHERE source_id=$1 AND slug=$2 AND deleted_at IS NULL', [row.source_id, row.slug]))+ .map(page => ({ table: 'pages' as const, id: Number(page.id) }));+ if (derived) {+ await lowerToDerivedTier(tx, derived.table, [derived.id], trust);+ await recordTaintEdges(tx, { ...derived, sourceId: row.source_id }, declaration.inputs);+ }+ return outcome;+ } };+}++async function prepareMaintenanceKind(engine: BrainEngine, row: WriteRequest, config: GBrainConfig): Promise<PreparedMutation> {if (row.authority.remote) throw trustedCliRequired('Remote maintenance publication is not supported.');if (row.intent?.kind === 'managed_maintenance_restore_extractor_facts') return (await import('../repair/extractor-facts.ts')).prepareExtractorFactsRestore(engine, row);if (row.intent?.kind === 'managed_maintenance_expire_captured_facts') return (await import('../repair/captured-facts.ts')).prepareCapturedFactsExpiry(engine, row);diff --git a/src/core/trust/taint.ts b/src/core/trust/taint.tsnew file mode 100644index 0000000000..d2032ab4f3--- /dev/null+++ b/src/core/trust/taint.ts@@ -0,0 +1,192 @@+/**+ * I2 taint (#5575: ENG-3, CEO-1, ENG-7): the tier of a derived row is the+ * minimum over every input placed in the model's context for that+ * derivation, capped at agent_written, decided at prompt-build time. A+ * deriver collects the rows it put in the prompt (pages, facts, takes,+ * timeline entries), reads their stored tiers here, declares the result to+ * the attribution seam, and records the complete input edges+ * (`derivation_inputs`, facts/derivation-inputs.ts) right after it writes.+ *+ * Deterministic projections that restate an input without a model (a fence+ * or timeline parse) take the input's tier, capped at operator_curated,+ * instead of the agent_written cap.+ *+ * Text that is not a row (a transcript file, a caller's turn text) enters as+ * a lowering cap: `lowerTo`.+ */+import type { BrainEngine } from '../engine.ts';+import { recordDerivationInputs, type DerivationRef } from '../facts/derivation-inputs.ts';+import {+ OWNER_TIER_FLOOR, compareTrust, effectiveWriteTrust, isTrustTier, minTrust, storedTrustTier, trustRankSql,+ type TaintInput, type TaintTable, type TrustTier, type WriteOrigin, type WriteTrust,+} from './tier.ts';+import { frontmatterTrustCaps } from './channel.ts';+import { maintenanceTransaction } from '../persistence/attribution.ts';++/** A row placed in a model's context: by id, or a page by its (source, slug) coordinate. */+export type TaintRef = { table: TaintTable; id: number } | { table: 'pages'; sourceId: string; slug: string };++type Sql = Pick<BrainEngine, 'executeRaw'>;++/**+ * The stored tier of every referenced row, deduplicated. A row that no longer+ * exists reads as `unknown` (a page by slug keeps `source:slug` as its id), so+ * a vanished input can never raise the derived tier.+ */+export async function readTaintInputs(engine: Sql, refs: readonly TaintRef[]): Promise<TaintInput[]> {+ const ids = new Map<TaintTable, Set<number>>();+ const slugs = new Map<string, { source_id: string; slug: string }>();+ for (const ref of refs) {+ if ('slug' in ref) slugs.set(`${ref.sourceId}:${ref.slug}`, { source_id: ref.sourceId, slug: ref.slug });+ else if (Number.isSafeInteger(Number(ref.id))) (ids.get(ref.table) ?? ids.set(ref.table, new Set()).get(ref.table)!).add(Number(ref.id));+ }+ const found = new Map<string, TaintInput>();+ for (const [table, set] of ids) {+ const rows = await engine.executeRaw<{ id: number | string; trust_tier: string | null }>(+ `SELECT id,trust_tier FROM ${table} WHERE id=ANY($1::bigint[])`, [[...set]]);+ for (const row of rows) found.set(`${table}:${row.id}`, { table, id: Number(row.id), tier: storedTrustTier(row.trust_tier) });+ for (const id of set) if (!found.has(`${table}:${id}`)) found.set(`${table}:${id}`, { table, id, tier: 'unknown' });+ }+ if (slugs.size) {+ const rows = await engine.executeRaw<{ id: number | string; source_id: string; slug: string; trust_tier: string | null }>(+ `SELECT p.id,p.source_id,p.slug,p.trust_tier FROM pages p+ JOIN jsonb_to_recordset($1::text::jsonb) AS r(source_id text,slug text) ON p.source_id=r.source_id AND p.slug=r.slug`,+ [JSON.stringify([...slugs.values()])]);+ const bySlug = new Map(rows.map(row => [`${row.source_id}:${row.slug}`, row]));+ for (const [key] of slugs) {+ const row = bySlug.get(key);+ if (row) found.set(`pages:${row.id}`, { table: 'pages', id: Number(row.id), tier: storedTrustTier(row.trust_tier) });+ else found.set(`pages:${key}`, { table: 'pages', id: key, tier: 'unknown' });+ }+ }+ return [...found.values()];+}++/** The tier the frontmatter of a not-yet-imported page (a transcript file read from disk) names, or undefined. */+export function frontmatterTaint(frontmatter: Record<string, unknown> | null | undefined): TrustTier | undefined {+ const caps = frontmatterTrustCaps(frontmatter);+ return caps.length ? minTrust(caps[0], ...caps.slice(1)) : undefined;+}++export interface DerivedTrustInput {+ /** The write path, e.g. `derive:synthesize`, `derive:facts_backstop`. */+ channel: string;+ /** Every row placed in the model's context (ENG-3). */+ inputs: readonly TaintInput[];+ /** Tiers of context text that is not a row (caller turn text, a transcript file). */+ lowerTo?: readonly TrustTier[];+ requestId?: string | null;+ sourceUri?: string | null;+ /** A deterministic restatement of its inputs (no model): capped at operator_curated, not agent_written. */+ projection?: boolean;+}++/** The WriteTrust of one derivation, computed once (trust/tier.ts effectiveWriteTrust). */+export function derivedWriteTrust(input: DerivedTrustInput): WriteTrust {+ const lowerTo = [...input.lowerTo ?? []];+ if (input.projection && !input.inputs.length && !lowerTo.length) lowerTo.push('unknown');+ return effectiveWriteTrust({+ channel: input.projection ? OWNER_TIER_FLOOR : 'agent_written',+ derived: !input.projection,+ inputs: input.inputs,+ lowerTo,+ origin: { channel: input.channel, ...(input.requestId ? { request_id: input.requestId } : {}),+ ...(input.sourceUri ? { source_uri: input.sourceUri } : {}) },+ });+}++/** Reads the inputs' tiers and builds the derivation's WriteTrust in one call. */+export async function deriveTrust(engine: Sql, refs: readonly TaintRef[], input: Omit<DerivedTrustInput, 'inputs'>):+ Promise<{ trust: WriteTrust; inputs: TaintInput[] }> {+ const inputs = await readTaintInputs(engine, refs);+ return { trust: derivedWriteTrust({ ...input, inputs }), inputs };+}++/**+ * ENG-7: the complete input edges of one derived row, written in the same+ * transaction right after the row. Inputs read as `unknown` because they+ * vanished keep their recorded reference.+ */+export async function recordTaintEdges(tx: BrainEngine, derived: { table: TaintTable; id: number; sourceId?: string | null },+ inputs: readonly Pick<TaintInput, 'table' | 'id'>[]): Promise<void> {+ if (!inputs.length) return;+ await recordDerivationInputs(tx, derived, inputs.map(({ table, id }): DerivationRef => ({ table, id })));+}++/**+ * Lowers rows to a derivation's tier, never raising. A derived write that+ * changes no content column (a re-promotion that only refreshes `source`, a+ * provenance republish with identical content) keeps the stored tier under+ * the trigger rule, so the deriver lowers explicitly; the trigger allows any+ * lowering. Returns the ids lowered.+ */+export async function lowerToDerivedTier(tx: Sql, table: TaintTable, ids: readonly number[], trust: WriteTrust): Promise<number[]> {+ if (!ids.length) return [];+ const rows = await tx.executeRaw<{ id: number | string }>(+ `UPDATE ${table} SET trust_tier=$2,write_origin=$3::text::jsonb+ WHERE id=ANY($1::bigint[]) AND ${trustRankSql('trust_tier')}>${trustRankSql('$2::text')} RETURNING id`,+ [[...ids], trust.tier, trust.origin ? JSON.stringify(trust.origin) : null]);+ return rows.map(row => Number(row.id));+}++/** A row a derivation wrote, for the lowering and edge pass. */+export interface DerivedRowRef { table: TaintTable; id: number; sourceId?: string | null }++/**+ * An unmanaged deriver's write: one attributed maintenance transaction at the+ * derivation's tier; every row `fn` reports is lowered to that tier (when its+ * content did not change) and gets its complete input edges.+ */+export async function derivedMaintenanceTransaction<T>(engine: BrainEngine, derivation: { trust: WriteTrust; inputs: readonly TaintInput[] },+ fn: (tx: BrainEngine) => Promise<{ result: T; rows: readonly DerivedRowRef[] }>): Promise<T> {+ return maintenanceTransaction(engine, async tx => {+ const { result, rows } = await fn(tx);+ for (const row of rows) {+ await lowerToDerivedTier(tx, row.table, [row.id], derivation.trust);+ await recordTaintEdges(tx, row, derivation.inputs);+ }+ return result;+ }, derivation.trust);+}++// ---------------------------------------------------------------------------+// Declarations carried in a journaled maintenance intent+// ---------------------------------------------------------------------------++/**+ * A derivation declared by the deriver at prompt-build time and carried in its+ * maintenance or facts request intent, so the preparer stamps the same tier+ * and records the same edges when the coordinator publishes it.+ */+export interface DerivationDeclaration { tier: TrustTier; origin: WriteOrigin; inputs: Array<Pick<TaintInput, 'table' | 'id'>> }++export function declareDerivation(trust: WriteTrust, inputs: readonly TaintInput[]): DerivationDeclaration {+ return { tier: trust.tier, origin: trust.origin ?? { channel: 'derive:unnamed' }, inputs: inputs.map(({ table, id }) => ({ table, id })) };+}++const TAINT_TABLES: readonly string[] = ['facts', 'takes', 'pages', 'timeline_entries'];++/** A declaration read back from a stored intent, or null when absent or malformed. */+export function readDerivationDeclaration(value: unknown): DerivationDeclaration | null {+ const v = value as Partial<DerivationDeclaration> | null | undefined;+ if (!v || typeof v !== 'object' || !isTrustTier(v.tier) || !v.origin || typeof v.origin.channel !== 'string' || !Array.isArray(v.inputs)) return null;+ if (v.inputs.some(i => !i || !TAINT_TABLES.includes(i.table as string) || (typeof i.id !== 'number' && typeof i.id !== 'string'))) return null;+ return v as DerivationDeclaration;+}++/**+ * The WriteTrust a preparer declares for a stored derivation: never above+ * `ceiling` (agent_written for model output) and never above any input in its+ * sample. A missing declaration (an intent queued before tiers) is `unknown`.+ */+export function declaredWriteTrust(declaration: DerivationDeclaration | null, ceiling: TrustTier = 'agent_written'): WriteTrust {+ if (!declaration) return { tier: minTrust('unknown', ceiling), origin: null };+ const sample = (declaration.origin.taint_inputs ?? []).map(i => storedTrustTier(i.tier));+ const tier = minTrust(declaration.tier, ceiling, ...sample);+ return { tier, origin: declaration.origin };+}++/** Whether `tier` is external_untrusted (or lower): the partition rule's test (ENG-3). */+export function isExternalTier(tier: TrustTier): boolean {+ return compareTrust(tier, 'external_untrusted') <= 0;+}