Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(dream): the unmanaged summary stores its frontmatter tags so its own file matches it; unfenced-facts re-checks the file right before rename
garrytane4d136e3c2e4
diff --git a/docs/architecture/system-of-record.md b/docs/architecture/system-of-record.mdindex c2f663a548..8d0a09c2dc 100644--- a/docs/architecture/system-of-record.md+++ b/docs/architecture/system-of-record.md@@ -231,7 +231,7 @@ or a physical projection that leaves attribution untouched):- `src/core/chronicle/publish.ts` (3): Life Chronicle event generations: maintenance requests on managed brains; the unmanaged event write, retirement soft delete and retired stamp run in `maintenanceTransaction`.- `src/core/company-brain/profile.ts` (1): legacy sync soft deletes (`softDeleteSyncPages`) run in `maintenanceTransaction`, inside the company-brain source check when one is active.- `src/core/cycle/dream-provenance.ts` (1): the dream provenance frontmatter stamp runs per page in `maintenanceTransaction`, or `derivedMaintenanceTransaction` at the child's derived tier when the ref carries its taint (unmanaged brains only; managed synthesis publishes through maintenance requests).-- `src/core/cycle/dream-taint.ts` (2): the unmanaged dream summary page, and the page_versions row of the different body it replaces (#6982), run in `derivedMaintenanceTransaction` at the tier of the pages it indexes; managed brains publish it through a maintenance request.+- `src/core/cycle/dream-taint.ts` (3): the unmanaged dream summary page, its frontmatter tags, and the page_versions row of the different body it replaces (#6982), run in `derivedMaintenanceTransaction` at the tier of the pages it indexes; managed brains publish it through a maintenance request.- `src/core/cycle/drift.ts` (1): the unmanaged drift report page runs in `maintenanceTransaction`; managed brains publish it through a maintenance request.- `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.diff --git a/src/core/cycle/dream-taint.ts b/src/core/cycle/dream-taint.tsindex 8c31835ddd..0b9567ead3 100644--- a/src/core/cycle/dream-taint.ts+++ b/src/core/cycle/dream-taint.ts@@ -73,11 +73,12 @@ export async function summaryDerivation(engine: BrainEngine, sourceId: string, s* #6982: a live body it replaces with a different one is kept in page_versions first (putPage itself keeps none).*/export async function putDerivedPage(engine: BrainEngine, derivation: Derivation, slug: string,- page: Parameters<BrainEngine['putPage']>[1], opts: { sourceId: string }): Promise<void> {+ page: Parameters<BrainEngine['putPage']>[1], opts: { sourceId: string; tags?: readonly string[] }): Promise<void> {await derivedMaintenanceTransaction(engine, derivation, async tx => {const prior = await readStoredBody(tx, opts.sourceId, slug, true);if (prior && (prior.compiled_truth !== page.compiled_truth || (prior.timeline ?? '') !== (page.timeline ?? ''))) await tx.createVersion(slug, opts);- const written = await tx.putPage(slug, page, opts);+ const written = await tx.putPage(slug, page, { sourceId: opts.sourceId });+ for (const tag of opts.tags ?? []) await tx.addTag(slug, tag, { sourceId: opts.sourceId, tagSource: 'frontmatter' });return { result: undefined, rows: [{ table: 'pages' as const, id: Number(written.id), sourceId: opts.sourceId }] };});}diff --git a/src/core/cycle/synthesize.ts b/src/core/cycle/synthesize.tsindex 1eb2926067..8033aecb0b 100644--- a/src/core/cycle/synthesize.ts+++ b/src/core/cycle/synthesize.ts@@ -3104,7 +3104,7 @@ async function writeSummaryPage(compiled_truth: parsed.compiled_truth,timeline: parsed.timeline,frontmatter: parsed.frontmatter,- }, { sourceId });+ }, { sourceId, tags: parsed.tags });const fileWriteRaw = (await engine.getConfig('dream.synthesize.summary_file_write'))?.trim().toLowerCase();const fileWriteEnabled = !(fileWriteRaw === 'false' || fileWriteRaw === '0' || fileWriteRaw === 'off');diff --git a/src/core/facts/unfenced-facts.ts b/src/core/facts/unfenced-facts.tsindex 790f01a2d2..c08df9986c 100644--- a/src/core/facts/unfenced-facts.ts+++ b/src/core/facts/unfenced-facts.ts@@ -557,6 +557,9 @@ async function fenceDatabaseOnlyPage(engine: BrainEngine, sourceId: string, enti* comparison, nothing is written. Otherwise the prior body is kept in page_versions, the body is mirrored, the rows are* stamped, and the `.tmp` is renamed into place last, so a rolled-back transaction leaves the file as it was.*/+/** Thrown inside the fence transaction when the file changed after the mirror, so the mirror and row stamps roll back. */+class FileChangedDuringFence extends Error {}+async function fenceFilePage(engine: BrainEngine, sourceId: string, entitySlug: string, filePath: string,group: UnfencedFactRow[], outcome: UnfencedFactsOutcome, lockRoot?: string): Promise<void> {const target = await resolvePageWriteTarget(engine, entitySlug, sourceId);@@ -609,9 +612,10 @@ async function fenceFilePage(engine: BrainEngine, sourceId: string, entitySlug:);}assertSourceFilesystemActive();+ if (readFileSync(filePath, 'utf-8') !== bytes) throw new FileChangedDuringFence();renameSync(tmpPath, filePath);return true;- });+ }).catch(error => { if (error instanceof FileChangedDuringFence) return false; throw error; });if (!written) {rmSync(tmpPath, { force: true });outcome.failed_pages.push(fileDatabaseDriftToken(entitySlug, sourceId, 'during'));diff --git a/test/cycle-synthesize-summary-page.test.ts b/test/cycle-synthesize-summary-page.test.tsindex 6113f4315e..4d134724e0 100644--- a/test/cycle-synthesize-summary-page.test.ts+++ b/test/cycle-synthesize-summary-page.test.ts@@ -47,6 +47,7 @@ async function renderSummary(writtenSlugs: string[]): Promise<{ body: string; magetConfig: async (_key: string) => null,// The summary write runs in a maintenance transaction; the stub is its own transaction engine.executeRaw: async () => [],+ addTag: async () => {},transaction: async <T>(fn: (tx: BrainEngine) => Promise<T>) => fn(engine),} as unknown as BrainEngine;try {