Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

feat(trust): consolidate partitions external facts and writes takes at the cluster's derived tier (#5575 L1a-taint, ENG-3, ENG-7)

garrytan323875284503
diff --git a/src/core/cycle/phases/consolidate.ts b/src/core/cycle/phases/consolidate.ts
index ffae1ec8b3..a596de7386 100644
--- a/src/core/cycle/phases/consolidate.ts
+++ b/src/core/cycle/phases/consolidate.ts
@@ -30,7 +30,7 @@ import { createHash } from 'node:crypto';
import { isAborted } from '../../abort-check.ts';
import { maintenancePreflight, submitMaintenanceConsolidation } from '../../persistence/prepared-maintenance.ts';
import { managedPersistenceEnabled } from '../../persistence/ownership.ts';
-import { maintenanceTransaction } from '../../persistence/attribution.ts';
+import { declareDerivation, derivedMaintenanceTransaction, derivedWriteTrust, isExternalTier, readTaintInputs } from '../../trust/taint.ts';
export interface ConsolidatePhaseOpts {
dryRun?: boolean;
@@ -165,7 +165,11 @@ export async function runPhaseConsolidate(
bucketsProcessed += 1;
const claims = await loadClaimShapes(engine, unconsolidated.map(f => f.id));
- const clusters = clusterFacts(unconsolidated, threshold, claims);
+ // #5575 ENG-3: a take's tier is the least trusted fact in its cluster, so
+ // external facts cluster apart from owner facts and their takes are external.
+ const taint = new Map((await readTaintInputs(engine, unconsolidated.map(f => ({ table: 'facts' as const, id: f.id })))).map(i => [Number(i.id), i]));
+ const clusters = [false, true].flatMap(external =>
+ clusterFacts(unconsolidated.filter(f => isExternalTier(taint.get(f.id)!.tier) === external), threshold, claims));
// Resolve entity_slug → page_id. If page missing in this source, skip.
const pageRows = await engine.executeRaw<{ id: number }>(
@@ -195,6 +199,8 @@ export async function runPhaseConsolidate(
.reduce((min, d) => (d < min ? d : min))
.toISOString()
.slice(0, 10);
+ const inputs = cluster.map(f => taint.get(f.id)!);
+ const derivation = { inputs, trust: derivedWriteTrust({ channel: 'derive:consolidate', inputs }) };
if (dryRun) {
// Pretend we did it.
@@ -206,7 +212,8 @@ export async function runPhaseConsolidate(
if (maintenance) {
const receipt = await submitMaintenanceConsolidation(engine, maintenance, b.entity_slug, cluster,
- { claim: best.fact, weight: clamp01(avgWeight), source: sources.slice(0, 200), since: sinceISO });
+ { claim: best.fact, weight: clamp01(avgWeight), source: sources.slice(0, 200), since: sinceISO },
+ declareDerivation(derivation.trust, inputs));
factsConsolidated += Number(receipt.facts_consolidated ?? 0);
takesWritten += Number(receipt.takes_written ?? 0);
if (receipt.reason === 'retired_take') clustersSkippedRetired++;
@@ -247,73 +254,68 @@ export async function runPhaseConsolidate(
[pageId, best.fact],
);
- let takeId: number;
- if (existing.length > 0) {
- // Re-promotion of a cluster we already wrote a take for. Refresh
- // the source-aggregation string (new fact rows may carry new
- // source_session values that the prior run didn't see); leave
- // row_num + weight untouched to keep the take's identity stable.
- takeId = existing[0].id;
- // A resolved take is immutable everywhere else (the engines throw
- // TAKE_RESOLVED_IMMUTABLE / TAKE_ALREADY_RESOLVED); this raw UPDATE
- // would bypass that guard. Reuse its id so the facts still consolidate
- // into it, but leave the row untouched.
- if (existing[0].resolved_at === null) {
- await maintenanceTransaction(engine, tx => tx.executeRaw(
- `UPDATE takes SET source = $1, updated_at = now() WHERE id = $2`,
- [sources.slice(0, 200), takeId],
- ));
- }
- } else {
- const inserted = await maintenanceTransaction(engine, tx => tx.addTakesBatch([{
- page_id: pageId,
- row_num: nextRowNum,
- claim: best.fact,
- kind: 'fact',
- holder: 'self',
- weight: clamp01(avgWeight),
- since_date: sinceISO,
- source: sources.slice(0, 200),
- active: true,
- }]));
- if (inserted < 1) continue;
-
- const idRows = await engine.executeRaw<{ id: number }>(
- `SELECT id FROM takes WHERE page_id = $1 AND row_num = $2`,
- [pageId, nextRowNum],
- );
- if (idRows.length === 0) {
- nextRowNum += 1;
- continue;
+ // #5575: the take, its source refresh, the consolidation marks and the
+ // valid_until writeback are one maintenance transaction at the cluster's
+ // derived tier. A re-promotion changes no content column, so the take is
+ // lowered explicitly to min(existing, cluster): an external cluster
+ // cannot keep an owner take's tier. Edges: take -> every cluster fact.
+ const written = await derivedMaintenanceTransaction<{ created: boolean; takeId: number | null } | null>(engine, derivation, async tx => {
+ let takeId: number;
+ let created = false;
+ if (existing.length > 0) {
+ // Re-promotion of a cluster we already wrote a take for. Refresh
+ // the source-aggregation string (new fact rows may carry new
+ // source_session values that the prior run didn't see); leave
+ // row_num + weight untouched to keep the take's identity stable.
+ takeId = existing[0].id;
+ // A resolved take is immutable everywhere else (the engines throw
+ // TAKE_RESOLVED_IMMUTABLE / TAKE_ALREADY_RESOLVED); this raw UPDATE
+ // would bypass that guard. Reuse its id so the facts still consolidate
+ // into it, but leave the row untouched.
+ if (existing[0].resolved_at === null) {
+ await tx.executeRaw(`UPDATE takes SET source = $1, updated_at = now() WHERE id = $2`, [sources.slice(0, 200), takeId]);
+ }
+ } else {
+ const inserted = await tx.addTakesBatch([{
+ page_id: pageId,
+ row_num: nextRowNum,
+ claim: best.fact,
+ kind: 'fact',
+ holder: 'self',
+ weight: clamp01(avgWeight),
+ since_date: sinceISO,
+ source: sources.slice(0, 200),
+ active: true,
+ }]);
+ if (inserted < 1) return { result: null, rows: [] };
+ const idRows = await tx.executeRaw<{ id: number }>(
+ `SELECT id FROM takes WHERE page_id = $1 AND row_num = $2`,
+ [pageId, nextRowNum],
+ );
+ if (idRows.length === 0) return { result: { created: true, takeId: null }, rows: [] };
+ takeId = idRows[0].id;
+ created = true;
}
- takeId = idRows[0].id;
- nextRowNum += 1;
- takesWritten += 1;
- }
- // Mark all contributing facts consolidated.
- await maintenanceTransaction(engine, async tx => {
+ // Mark all contributing facts consolidated.
for (const f of cluster) await tx.consolidateFact(f.id, takeId);
- });
- factsConsolidated += cluster.length;
- // v0.35.4 (D-CDX-4 part 2) — chronological valid_until writeback.
- // Sort the cluster by (valid_from ASC, id ASC); walk consecutive
- // pairs; stamp the older fact's valid_until = next_newer.valid_from.
- // The newest fact keeps valid_until = NULL. This makes the facts
- // table a proper bitemporal record without the contradiction probe
- // having to mutate it (preserves auto-supersession.ts:4 invariant —
- // see also R8 test guard).
- //
- // Idempotent: re-running on the same cluster produces the same
- // chronological order and the same valid_until values. No-op if
- // valid_until is already correct.
- const chronological = [...cluster].sort((a, b) => {
- const t = a.valid_from.getTime() - b.valid_from.getTime();
- if (t !== 0) return t;
- return a.id - b.id;
- });
- await maintenanceTransaction(engine, async tx => {
+ // v0.35.4 (D-CDX-4 part 2) — chronological valid_until writeback.
+ // Sort the cluster by (valid_from ASC, id ASC); walk consecutive
+ // pairs; stamp the older fact's valid_until = next_newer.valid_from.
+ // The newest fact keeps valid_until = NULL. This makes the facts
+ // table a proper bitemporal record without the contradiction probe
+ // having to mutate it (preserves auto-supersession.ts:4 invariant —
+ // see also R8 test guard).
+ //
+ // Idempotent: re-running on the same cluster produces the same
+ // chronological order and the same valid_until values. No-op if
+ // valid_until is already correct.
+ const chronological = [...cluster].sort((a, b) => {
+ const t = a.valid_from.getTime() - b.valid_from.getTime();
+ if (t !== 0) return t;
+ return a.id - b.id;
+ });
for (let i = 0; i < chronological.length - 1; i++) {
const older = chronological[i];
const newer = chronological[i + 1];
@@ -328,7 +330,12 @@ export async function runPhaseConsolidate(
[newer.valid_from, older.id],
);
}
+ return { result: { created, takeId }, rows: [{ table: 'takes' as const, id: Number(takeId), sourceId: b.source_id }] };
});
+ if (written?.created) nextRowNum += 1;
+ if (!written?.takeId) continue;
+ if (written.created) takesWritten += 1;
+ factsConsolidated += cluster.length;
}
}