Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(repair): rename-identity reads the new file's fence from the checkout, never writes valid_until, and is inventoried (#6729)
garrytan8ed089530fee
diff --git a/docs/architecture/canonical-writers.tsv b/docs/architecture/canonical-writers.tsvindex 94057b0f8f..a0fa32276c 100644--- a/docs/architecture/canonical-writers.tsv+++ b/docs/architecture/canonical-writers.tsv@@ -220,6 +220,7 @@ src/core/persistence/atom-maintenance.ts 1 coordinator A managed atom retirementsrc/core/repair/stale-atoms.ts 2 coordinator gbrain repair stale-atoms retires an atom on a managed brain through a managed_maintenance_retire_stale_atoms coordinated request (the marker stamp runs in its publication); only an unmanaged brain soft-deletes and stamps in a direct page-locked transaction, where no canonical writer enforcement applies.src/core/repair/ghost-pages.ts 1 coordinator `gbrain repair ghost-pages` (#6696 B5) retires a preview-approved ghost page (its canonical file is gone): on a managed brain only through a managed_maintenance_retire_ghost_page request whose preparer reruns the classifier under the approved owner and prepares a delete_page with allowMissingFile that refuses unless the file target is the approved path with no bytes (a restored file refuses at preparation or at filesystem publication); only an unmanaged brain versions and soft-deletes directly, inside maintenanceTransaction under the page lock after the revision, page id and file-absence recheck.src/core/repair/aliases.ts 2 coordinator `gbrain repair aliases` (#6619) deletes preview-approved page_aliases / slug_aliases rows whose target page no longer exists (soft-deleted targets are never touched), under the page key: inside withCoordinatedWrite with maintenance attribution on a managed brain, one transaction on an unmanaged brain.+src/core/repair/rename-identity.ts 5 early_refusal `gbrain repair rename-identity` (#6729) carries a previewed old page's identity onto its new slug only on an unmanaged brain: the plan and the apply refuse `managed_remedy_unavailable` on a managed brain before any canonical write. The unmanaged carry runs in one maintenanceTransaction under both page locks after the plan-hash, open-record and fence-row recheck: it merges matching fence rows onto the old rows (never valid_until), retires the provisional page under its purged slug, moves the old page to the new slug (updateSlug) and imports the new file it read from the checkout; it writes no file, so there is no filesystem ordering to keep.src/core/repair/frontmatter.ts 2 coordinator `gbrain repair frontmatter --apply` (#5988) publishes a managed source's repaired file only through submitManagedFileRepair (the managed_file_repair request, preview-hash bound). On an unmanaged (legacy) source it writes the previewed bytes after a backup, moves a renamed page with updateSlug and its source_path in one maintenanceTransaction, then imports the file through importFromFile; no canonical writer enforcement applies there.src/commands/sources-mirror.ts 1 engine_guard `gbrain sources mirror-readonly|mirror-writable` (#5409) updates only sources.config.mirror_read_only, exactly as `sources federate` updates sources.config.federated; the sources managed_writer_guard trigger governs the row and no page, fact or file is written.src/core/sync-upstream.ts 1 engine_guard `recordUpstreamObservation` (#5255/#5176) updates only sources.upstream_checked_at, upstream_commit and upstream_behind, observations of the checkout's Git upstream outside managed_writer_guard's protected topology and checkpoint tuples; no page, fact or file is written, and a failure never fails the sync.diff --git a/docs/architecture/system-of-record.md b/docs/architecture/system-of-record.mdindex 856076ef95..15ff0851cd 100644--- a/docs/architecture/system-of-record.md+++ b/docs/architecture/system-of-record.md@@ -289,6 +289,7 @@ or a physical projection that leaves attribution untouched):- `src/core/repair/frontmatter.ts` (1): a `managed_file_repair` request on managed sources; the unmanaged rename runs in `maintenanceTransaction`.- `src/core/repair/ghost-pages.ts` (2): `gbrain repair ghost-pages` retirement: a receipted `managed_maintenance_retire_ghost_page` request (a prepared `delete_page` with no file bytes) on managed brains; the unmanaged version and soft delete run in `maintenanceTransaction` after the same recheck.- `src/core/repair/ontology-facts.ts` (1): a coordinated database-only write (`coordinatedDatabaseWrite`, the `ontology_propose` path) on managed brains; the unmanaged ontology-facts restore runs in `maintenanceTransaction`.+- `src/core/repair/rename-identity.ts` (5): `gbrain repair rename-identity` carry, unmanaged brains only (a managed brain refuses `managed_remedy_unavailable` before writing); the fence-row merge, the provisional page's retirement, the slug move and the import run in one `maintenanceTransaction` under both page locks after the plan-hash recheck.- `src/core/repair/stale-atoms.ts` (3): a request on managed brains; the unmanaged retirement runs in `maintenanceTransaction`.- `src/core/repair/take-supersession.ts` (1): `gbrain repair take-supersession` reprojection: a receipted `managed_maintenance_take_reproject` request on managed brains, `maintenanceTransaction` on unmanaged ones; fence edits go through a revision-bound `put_page`.- `src/core/repair/timeline-comments.ts` (1): `gbrain repair timeline-comments` row cleanup: `withCoordinatedWrite` under the page key on managed brains, `maintenanceTransaction` otherwise.diff --git a/src/core/repair/rename-identity.ts b/src/core/repair/rename-identity.tsindex a115d93d43..aa8d1ea7e3 100644--- a/src/core/repair/rename-identity.ts+++ b/src/core/repair/rename-identity.ts@@ -14,9 +14,10 @@* an edit (a new version). The provisional page is retired: re-keyed purge-style (`<slug>~purged-<id>`) and* soft-deleted, its history, timeline and edges moved onto the carried page, nothing hard-deleted. An old -> new* slug redirect is recorded, the old page's slug-keyed facts, aliases and withdrawals move, and the record settles `remedied`.- * Facts: every fence row of the old page must be in the new file's fence with the same claim, and the provisional- * page's stored row there must carry that claim too. A matching pair is one fact: the old row (id, created_at,- * valid_from) survives with the new row's fence fields. Anything else (a different claim at a row number, or an old+ * Facts: every fence row of the old page must be in the new file's fence (read from the checkout, the bytes the carry+ * imports) with the same claim, and the provisional page's stored row there must carry that claim too. A matching+ * pair is one fact: the old row (id, created_at and its validity window, valid_from and valid_until) survives with+ * the new row's other fence fields; the carry never writes valid_until. Anything else (a different claim at a row number, or an old* row the new file does not hold) refuses `fact_rows_collide` before anything is written, naming the rows.* Classic sources only: on a managed brain page tables take writes only through coordinated publications, so carrying* is unavailable there (`managed_remedy_unavailable`): the preview says so and names `--close`, and an apply refuses.@@ -26,7 +27,7 @@* run it (`explicit_kind_required`).*/import { randomUUID } from 'node:crypto';-import { existsSync } from 'node:fs';+import { existsSync, readFileSync } from 'node:fs';import { join } from 'node:path';import type { BrainEngine } from '../engine.ts';import { OperationError } from '../ops/contract.ts';@@ -75,22 +76,22 @@ const refuse = (code: string, message: string, suggestion: string, fix: Action,return error;};-/** Claims the facts fences of a page hold, by row number (body and timeline). */-function fenceClaims(page: { compiled_truth: string; timeline: string | null }): Map<number, string> {- const claims = new Map<number, string>();- for (const field of [page.compiled_truth, page.timeline ?? '']) for (const fact of parseFactsFence(field).facts) if (!claims.has(fact.rowNum)) claims.set(fact.rowNum, fact.claim);- return claims;+/** The new file as the carry imports it, from the source checkout; a missing file refuses: there is nothing to carry onto. */+async function readNewFile(engine: Pick<BrainEngine, 'executeRaw'>, record: IdentityDowngrade): Promise<{ path: string; text: string }> {+ const [source] = await engine.executeRaw<{ local_path: string | null }>('SELECT local_path FROM sources WHERE id = $1', [record.source_id]);+ const path = source?.local_path ? join(source.local_path, record.to) : null;+ if (!path || !existsSync(path)) throw changedSincePreview(record, `the new file ${record.to} is not in the source checkout`);+ return { path, text: readFileSync(path, 'utf8') };}-/** Each fence row of the old page against the new file's fence (the provisional page's stored body) and the provisional's stored row. */-export async function fenceRowChecks(engine: Pick<BrainEngine, 'executeRaw'>, sourceId: string, oldSlug: string, newPageId: number): Promise<FenceRowCheck[]> {- const [fresh] = await engine.executeRaw<{ slug: string; compiled_truth: string; timeline: string | null }>('SELECT slug, compiled_truth, timeline FROM pages WHERE id = $1', [newPageId]);- if (!fresh) return [];- const file = fenceClaims(fresh);+/** Each fence row of the old page against the new file's facts fence and the provisional page's stored row at that row number. */+export async function fenceRowChecks(engine: Pick<BrainEngine, 'executeRaw'>, sourceId: string, oldSlug: string, newSlug: string, newFile: string): Promise<FenceRowCheck[]> {+ const file = new Map<number, string>();+ for (const fact of parseFactsFence(newFile).facts) if (!file.has(fact.rowNum)) file.set(fact.rowNum, fact.claim);const rows = await engine.executeRaw<{ row_num: number; old_id: number | string; old_fact: string; new_id: number | string | null; new_fact: string | null }>(`SELECT o.row_num, o.id AS old_id, o.fact AS old_fact, n.id AS new_id, n.fact AS new_fact FROM facts oLEFT JOIN facts n ON n.source_id = o.source_id AND n.source_markdown_slug = $3 AND n.row_num = o.row_num- WHERE o.source_id = $1 AND o.source_markdown_slug = $2 AND o.row_num IS NOT NULL ORDER BY o.row_num`, [sourceId, oldSlug, fresh.slug]);+ WHERE o.source_id = $1 AND o.source_markdown_slug = $2 AND o.row_num IS NOT NULL ORDER BY o.row_num`, [sourceId, oldSlug, newSlug]);return rows.map(row => ({ row: Number(row.row_num), old_id: Number(row.old_id), old_claim: row.old_fact, file_claim: file.get(Number(row.row_num)) ?? null,new_id: row.new_id == null ? null : Number(row.new_id), new_claim: row.new_fact }));}@@ -222,7 +223,7 @@ export const renameIdentityRepair = {/** A carry must be possible before a preview hash is printed: both pages as recorded, and no fence row that would drop a fact. */async function assertCarryable(engine: BrainEngine, record: IdentityDowngrade, parts: IdentityRemedyParts): Promise<void> {if (!parts.old?.deleted || !parts.new) throw changedSincePreview(record, 'the old page is no longer soft-deleted or the new page is gone');- const checks = await fenceRowChecks(engine, record.source_id, parts.old.slug, parts.new.page_id);+ const checks = await fenceRowChecks(engine, record.source_id, parts.old.slug, parts.new.slug, (await readNewFile(engine, record)).text);if (checks.some(collides)) throw factRowsCollide(record, checks);}@@ -233,9 +234,7 @@ async function assertCarryable(engine: BrainEngine, record: IdentityDowngrade, p*/async function carryIdentity(engine: BrainEngine, record: IdentityDowngrade, hash: string): Promise<RepairItemOutcome> {if (await managedPersistenceEnabled(engine)) throw managedRemedyRefusal(record);- const [source] = await engine.executeRaw<{ local_path: string | null }>('SELECT local_path FROM sources WHERE id = $1', [record.source_id]);- const file = source?.local_path ? join(source.local_path, record.to) : null;- if (!file || !existsSync(file)) throw changedSincePreview(record, `the new file ${record.to} is not in the source checkout`);+ const file = await readNewFile(engine, record);const sourceId = record.source_id, oldSlug = record.old_slug, newSlug = record.new_slug!;const publicationId = randomUUID();const report = { appended: 0 };@@ -244,14 +243,14 @@ async function carryIdentity(engine: BrainEngine, record: IdentityDowngrade, hasconst live = await identityRemedyParts(tx, record, 'carry');const [open] = await tx.executeRaw(`SELECT 1 FROM op_checkpoints WHERE op = $1 AND fingerprint = $2 AND completed_keys->0->>'disposition' IS NULL`, [IDENTITY_DOWNGRADE_OP, String(record.old_page_id)]);if (previewHash(live) !== hash || !open || !live.old || !live.new) throw changedSincePreview(record, 'a page of the pair changed or its record closed');- const checks = await fenceRowChecks(tx, sourceId, oldSlug, live.new.page_id);+ const checks = await fenceRowChecks(tx, sourceId, oldSlug, live.new.slug, file.text);if (checks.some(collides)) throw factRowsCollide(record, checks);const oldId = live.old.page_id, provisionalId = live.new.page_id, retired = purgedSlug(newSlug, provisionalId);const merged = checks.filter(check => check.new_id !== null);await lockedBeliefBatch(tx, { sourceId, slugs: [oldSlug, newSlug], publicationId, op: 'rename_identity', remote: false, report }, async () => {for (const check of merged) {await tx.executeRaw('UPDATE facts SET superseded_by = $1 WHERE superseded_by = $2', [check.old_id, check.new_id]);- await tx.executeRaw(`UPDATE facts o SET kind = n.kind, visibility = n.visibility, notability = n.notability, context = n.context, valid_until = n.valid_until,+ await tx.executeRaw(`UPDATE facts o SET kind = n.kind, visibility = n.visibility, notability = n.notability, context = n.context,expired_at = n.expired_at, source = n.source, confidence = n.confidence, claim_metric = n.claim_metric, claim_value = n.claim_value, claim_unit = n.claim_unit,claim_period = n.claim_period, event_type = n.event_type, attributed_to = n.attributed_to FROM facts n WHERE o.id = $1 AND n.id = $2`, [check.old_id, check.new_id]);}@@ -260,7 +259,7 @@ async function carryIdentity(engine: BrainEngine, record: IdentityDowngrade, hasawait tx.executeRaw('UPDATE pages SET deleted_at = NULL, source_path = $1, updated_at = now() WHERE id = $2', [record.to, oldId]);await movePageIdReferences(tx, provisionalId, oldId);});- const imported = await importFile(tx, file, record.to, { noEmbed: true, sourceId });+ const imported = await importFile(tx, file.path, record.to, { noEmbed: true, sourceId });if (imported.slug !== newSlug || imported.error) throw changedSincePreview(record, `the new file did not import onto ${newSlug} (${imported.error ?? imported.slug})`);if (!await settleIdentityDowngrade(tx, oldId, 'remedied')) throw changedSincePreview(record, 'its record was settled since the preview');return { applied: true, outcome: 'carried', detail: { old_page_id: oldId, from: oldSlug, to: newSlug, retired_page_id: provisionalId, retired_slug: retired,diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txtindex c6da097220..ccc0dd3865 100644--- a/test/postgres-unit-arms.txt+++ b/test/postgres-unit-arms.txt@@ -173,6 +173,7 @@ test/repair-fences.test.tstest/repair-frontmatter-keys-sweep-6454.test.tstest/repair-frontmatter.test.tstest/repair-receipt-compaction.test.ts+test/repair-rename-identity-6729.test.tstest/scripts/test-stress-postgres.test.tstest/search/page-rank-cut.test.tstest/search/rerank-gate-hybrid.test.ts