Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(repair): segmented-tiling apply records the republished revision as imported from HEAD
garrytanc465075429e9
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f5b7fbc35b..4062019dc5 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -19,6 +19,7 @@ identifiers and attribution are available in the pre-removal Git revision- `src/core/persistence/sync-fault-class.ts`, `sync-holds.ts` (`gitHoldFix`), `sync-entry-fault.ts`: the `freeze_page_body_rows_unreadable` verdict, the hold's fix and its message name the segmented-tiling repair; when doctor `segmented_tiling` does not list the page, the fix says to grant the reading role access and run retry-held. `gbrain sync unblock` refuses the hold by name with that fix.- `gbrain sync status`: each `page_write_failed` hold names its `fault_code`, `holds_summary.by_fault_code` counts them, and `--slug <slug>` / `--code <code>` (a hold code or a fault code) list one hold; a filtered page's cursor and `next_command` keep the filter.- Doctor: `segmented_tiling` runs as its own entry with its own budget, so a default run reports it even when the wave checks of `timeline_history` run out of theirs; an entry's last known result recorded before a check was added no longer hides that check (it reports unknown, or is named in `doctor_last_known`).+- `gbrain repair segmented-tiling --apply` records the republished revision as imported from the HEAD blob when the file imports to the page's stored title, type, frontmatter and tags, so the next sync holds no `file_drift` for the page it just repaired. A file whose metadata changed since the last import records nothing, and sync still guards the stored version.- #8504 items 6 and 8: `untiledSegmentedPages` (doctor and the repair) skips archived sources; doctor's fix always names a `--source` and lists the other damaged sources' commands; a repair preview over 1,000 pages reports `truncated` and says to preview again after the apply.## [0.60.565.0] - 2026-11-03diff --git a/docs/guides/repair.md b/docs/guides/repair.mdindex 5b92e7812f..59f00444f9 100644--- a/docs/guides/repair.md+++ b/docs/guides/repair.md@@ -478,7 +478,9 @@ makes the page `changed_since_preview`) as a new generation through the same flirefuses a generation that does not cover its body, keeps the page's title,type, frontmatter, tags and trust tier, clears the sync hold the preview saw(a newer hold stays until the next sync re-imports the file),-asks the next sync to screen the file again (so a file that differs from the+records the new revision as imported from the HEAD blob when the file imports+to the stored title, type, frontmatter and tags (so the next sync holds no+`file_drift` for it), asks the next sync to screen the file again (so a file that differs from thestored page in anything else imports normally) and writes the page's receipt(`op_checkpoints` op `segmented-tiling-receipt`) and a `segmented-tiling` auditline. A rerun with the same hash skips pages that already have a receipt.diff --git a/src/core/repair/segmented-tiling.ts b/src/core/repair/segmented-tiling.tsindex ff27fb72b7..1ae7b1950a 100644--- a/src/core/repair/segmented-tiling.ts+++ b/src/core/repair/segmented-tiling.ts@@ -21,7 +21,8 @@* versions the damaged generation by reference, screens the blob again and stages the body the screen's fence step and* the purge and withdrawal overlay admit (a purged claim the file still carries stays out), publishes it as a new generation through the* generation flip (which refuses one that does not tile), keeps the page's title, type, frontmatter, tags and trust tier,- * clears the sync hold the preview saw and asks the next sync to re-screen the file (so a file that differs from the+ * clears the sync hold the preview saw, records the new revision as imported from the HEAD blob when the file imports to+ * the stored metadata (so the next sync holds no file_drift), asks the next sync to re-screen the file (so a file that differs from the* stored page in anything else imports normally), and writes the page's receipt, also appended to the `segmented-tiling`* audit log. Explicit-only: never from doctor, a remediation plan or an upgrade.*/@@ -33,6 +34,8 @@ import { OperationError, opError } from '../ops/contract.ts';import { trustedCliRequired } from '../ops/op-fix.ts';import { parseMarkdown } from '../markdown.ts';import { maintenanceAttribution } from '../persistence/attribution.ts';+import { stableJson } from '../persistence/digest.ts';+import { withoutSafetyAssessments } from '../persistence/reconcile-safety.ts';import { withCoordinatedWrite, withWriteAttribution } from '../persistence/context.ts';import { readManagedMode } from '../persistence/managed-mode.ts';import { getWorktreeBinding } from '../persistence/ownership.ts';@@ -40,7 +43,9 @@ import { checkOwner, ownerUnavailableError } from '../persistence/owner-refusal.import { hostIdFor } from '../persistence/identity.ts';import { gitChildEnv } from '../git-env.ts';import { loadRepairBusySet, repairBusy, repairBusyError } from '../persistence/repair-busy.ts';-import { clearGitHold, readGitHold, requestGitHoldRetry, type GitHoldRecord } from '../persistence/sync-holds.ts';+import { clearGitHold, readGitHold, recordSyncImportProvenance, requestGitHoldRetry, syncImportParserFor, type GitHoldRecord } from '../persistence/sync-holds.ts';+import { loadActivePackForEngine } from '../schema-pack/engine-resolution.ts';+import { VERSION } from '../../version.ts';import { unreadPageImportScreen } from '../persistence/sync-screen.ts';import { PAGE_BODY_FAULT_CODES } from '../persistence/sync-fault-class.ts';import { preserveWithdrawnFenceRows } from '../facts/withdrawal.ts';@@ -141,14 +146,15 @@ const segmentedReadHold = (hold: GitHoldRecord) => hold.code === 'page_write_fai* fence step and the purge and withdrawal overlay an import applies, so a purged claim the file still carries stays out.*/async function stageable(engine: BrainEngine, page: { source_id: string; slug: string; source_path: string }, checkout: { root: string; incarnation: string },- content: string): Promise<{ held: string } | { truth: string; timeline: string; hold: GitHoldRecord | null }> {+ content: string): Promise<{ held: string } | { truth: string; timeline: string; hold: GitHoldRecord | null; asStored: boolean }> {const hold = await readGitHold(engine, page.source_id, checkout.incarnation, page.source_path);if (hold && !segmentedReadHold(hold)) return { held: `its sync hold (${hold.code}) withholds the file; resolve that hold first (gbrain sources status ${page.source_id})` };const screen = await unreadPageImportScreen(engine, { sourceId: page.source_id, root: checkout.root, path: page.source_path, slug: page.slug, content,snapshot: await engine.readPageSnapshot(page.slug, { sourceId: page.source_id }) });if (screen.status === 'refused') return { held: `the next sync would hold the file (${screen.refusal.code}): ${screen.refusal.message}` };const parsed = screen.status === 'importable' && screen.parsed ? screen.parsed : parseMarkdown(content, `${page.slug}.md`);- const [stored] = await engine.executeRaw<{ type: string; frontmatter: Record<string, unknown> | null }>('SELECT type, frontmatter FROM pages WHERE source_id=$1 AND slug=$2', [page.source_id, page.slug]);+ const [stored] = await engine.executeRaw<{ type: string; title: string; frontmatter: Record<string, unknown> | null; tags: string[] }>(`SELECT p.type, p.title, p.frontmatter,+ COALESCE((SELECT jsonb_agg(t.tag) FROM tags t WHERE t.page_id=p.id), '[]'::jsonb) AS tags FROM pages p WHERE p.source_id=$1 AND p.slug=$2`, [page.source_id, page.slug]);const storedFrontmatter = stored?.frontmatter ?? {};const access = (fm: Record<string, unknown>) => JSON.stringify(['visibility', 'readable_by', 'pinned_question', ...Object.keys(PRIVACY_ALIASES)].map(key => fm[key] ?? null));if (access(parsed.frontmatter) !== access(storedFrontmatter)@@ -164,7 +170,11 @@ async function stageable(engine: BrainEngine, page: { source_id: string; slug: sconst marked = [QUARANTINE_KEY, EMBED_SKIP_KEY, CONTENT_FLAG_KEY].filter(key => settled.frontmatter[key] != null && storedFrontmatter[key] == null);if (marked.length) return { held: `the next sync's content gate would mark the page ${marked.join(', ')}, and this repair republishes the body under the stored frontmatter` };const body = screen.status === 'importable' && screen.fences ? screen.fences.after : { compiled_truth: parsed.compiled_truth, timeline: parsed.timeline ?? '' };- return { hold, truth: (await preserveWithdrawnFenceRows(engine, page.source_id, body.compiled_truth, page.slug)).trim(),+ // The republished page is what an import of the file makes only when the metadata it keeps is what the file imports (an import keeps tags it lacks).+ const storedTags = new Set(stored?.tags ?? []);+ const asStored = settled.title === stored?.title && settled.type === stored?.type && settled.tags.every(tag => storedTags.has(tag))+ && stableJson(withoutSafetyAssessments(settled.frontmatter)) === stableJson(withoutSafetyAssessments(storedFrontmatter));+ return { hold, asStored, truth: (await preserveWithdrawnFenceRows(engine, page.source_id, body.compiled_truth, page.slug)).trim(),timeline: (await preserveWithdrawnFenceRows(engine, page.source_id, body.timeline ?? '', page.slug)).trim() };}@@ -201,7 +211,7 @@ export async function survey(engine: BrainEngine, selection: Selection, limit =* Republishes one page's body (`stageable`'s) in the caller's transaction and clears the sync hold the preview saw (a newer* one stays for the requested re-screen); false when the page, or a fact withdrawal it carries, changed since the preview.*/-async function republish(tx: BrainEngine, page: HealPage, body: { truth: string; timeline: string }): Promise<{ generation: number; unsealed: boolean; hold_cleared: boolean } | false> {+async function republish(tx: BrainEngine, page: HealPage, body: { truth: string; timeline: string; asStored: boolean }): Promise<{ generation: number; unsealed: boolean; hold_cleared: boolean } | false> {await tx.lockPageKeys([{ sourceId: page.source_id, slug: page.slug }]);const [now] = await untiledSegmentedPages(tx, { sourceIds: [page.source_id], slugs: [page.slug] });if (!now || now.page_id !== page.page_id || now.revision !== page.revision || now.body_generation !== page.body_generation) return false;@@ -227,6 +237,15 @@ async function republish(tx: BrainEngine, page: HealPage, body: { truth: string;pageId: page.page_id, prepared, promotion: false, retireMaxRows: flipRetireMaxRows,meta: { title: row!.title, type: row!.type as never, frontmatter: frontmatter as never, page_kind: row!.page_kind as never } }));if (applied.complete) await sealSegmentedPage(tx, page.page_id);+ // When the republished revision is what the file at HEAD imports, the import record names that blob and revision, so the next sync+ // prescreens the file instead of holding it file_drift as a database-only edit. Kept metadata the file no longer imports stays guarded.+ if (body.asStored) {+ const [after] = await tx.executeRaw<{ revision: string }>('SELECT knowledge_revision::text AS revision FROM pages WHERE id=$1', [page.page_id]);+ const pack = await loadActivePackForEngine(tx, { remote: false, sourceId: page.source_id }).catch(() => null);+ await recordSyncImportProvenance(tx, { source_id: page.source_id, incarnation: page.incarnation, page_id: page.page_id, origin: page.source_path,+ raw_sha256: page.blob_sha256, blob_oid: page.blob_oid, working: false, gbrain_version: VERSION, revision: after!.revision,+ parser: syncImportParserFor(page.source_path, pack?.identity ?? null), disk_sha256: page.blob_sha256, disk_blob_oid: page.blob_oid });+ }const hold_cleared = page.hold_observed_at !== null&& await clearGitHold(tx, { sourceId: page.source_id, incarnation: page.incarnation, path: page.source_path, observedAt: page.hold_observed_at });await requestGitHoldRetry(tx, page.source_id, page.incarnation, [page.source_path]);diff --git a/test/large-pages/segmented-tiling-repair.test.ts b/test/large-pages/segmented-tiling-repair.test.tsindex 5355e89285..b733c6014f 100644--- a/test/large-pages/segmented-tiling-repair.test.ts+++ b/test/large-pages/segmented-tiling-repair.test.ts@@ -14,14 +14,14 @@import { afterAll, beforeAll, expect, test } from 'bun:test';import { randomUUID } from 'node:crypto';import { execFileSync } from 'node:child_process';-import { appendFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { appendFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { PGLiteEngine } from '../../src/core/pglite-engine.ts';import { claimWorktree } from '../../src/core/persistence/ownership.ts';import { performManagedSync } from '../../src/core/persistence/sync-run.ts';import { disposePersistenceConsumer } from '../../src/core/persistence/service.ts';-import { readGitHold, writeGitHold } from '../../src/core/persistence/sync-holds.ts';+import { readGitHold, readSyncImportProvenance, writeGitHold } from '../../src/core/persistence/sync-holds.ts';import { importFromContent } from '../../src/core/import-file.ts';import { materializeSegmentedBody } from '../../src/core/page-body/ingest.ts';import { untiledSegmentedPages } from '../../src/core/page-body/body-tiling.ts';@@ -227,6 +227,47 @@ test('a claim the owner purged stays out: the apply republishes the file throughexpect(await engine.executeRaw('SELECT count(*)::int AS n FROM content_chunks WHERE page_id=$1 AND chunk_text LIKE $2', [f.pageId, `%${PURGED}%`])).toEqual([{ n: 0 }]);}), 300_000);+test('the republished page is recorded as imported from HEAD: the next sync holds no file_drift for a page the file imports', async () => withEnv(env, async () => {+ const f = await managedFixture();+ // A tag only the database holds: every import keeps it, so importing the file would also write the tag back into it.+ await withManagedFixtureWrite(engine, [f.sourceId], tx => tx.addTag(SLUG, 'kept-example', { sourceId: f.sourceId }));+ appendFileSync(join(f.root, FILE), '- **2026-11-01** | meeting — first follow-up\n');+ commit(f.root);+ expect((await performManagedSync(engine, { sourceId: f.sourceId, noPull: true, noEmbed: true, noExtract: true })).held ?? []).toEqual([]);+ await damage(f.sourceId, f.pageId);+ appendFileSync(join(f.root, FILE), '- **2026-11-02** | meeting — second\n');+ commit(f.root);+ expect(((await performManagedSync(engine, { sourceId: f.sourceId, noPull: true, noEmbed: true, noExtract: true })).held ?? []).map(hold => hold.code)).toEqual(['page_write_failed']);+ const scope = await resolveRepairScope(engine, f.sourceId);+ const run = (apply: boolean, expect?: string) => repairRunner(engine, { apply, logger }).then(runner => runner.run('segmented-tiling', scope, { explicit: true, sourceFlag: f.sourceId, ...(expect ? { expect } : {}) }));+ expect(await run(true, (await run(false)).preview_hash!)).toMatchObject({ applied: 1 });+ const [source] = await engine.executeRaw<{ incarnation: string }>('SELECT incarnation::text AS incarnation FROM sources WHERE id=$1', [f.sourceId]);+ const [republished] = await engine.executeRaw<{ revision: string }>('SELECT knowledge_revision::text AS revision FROM pages WHERE id=$1', [f.pageId]);+ const blob = git(f.root, 'rev-parse', `HEAD:${FILE}`);+ expect(await readSyncImportProvenance(engine, f.sourceId, source!.incarnation, f.pageId)).toMatchObject({ revision: republished!.revision, disk_blob_oid: blob, blob_oid: blob, origin: FILE });++ const after = await performManagedSync(engine, { sourceId: f.sourceId, noPull: true, noEmbed: true, noExtract: true });+ expect(after.held ?? []).toEqual([]);+ expect(after.status).toBe('synced');+ expect((await readSyncStatus(engine, f.sourceId, { detail: false })).holds).toEqual([]);+}), 300_000);++test('a republished page whose stored frontmatter the file no longer imports records no provenance, so sync guards the stored frontmatter', async () => withEnv(env, async () => {+ const f = await managedFixture();+ await damage(f.sourceId, f.pageId);+ writeFileSync(join(f.root, FILE), readFileSync(join(f.root, FILE), 'utf8').replace('type: person', 'type: person\nstatus: active') + '- **2026-11-02** | meeting — second\n');+ commit(f.root);+ await performManagedSync(engine, { sourceId: f.sourceId, noPull: true, noEmbed: true, noExtract: true });+ const [source] = await engine.executeRaw<{ incarnation: string }>('SELECT incarnation::text AS incarnation FROM sources WHERE id=$1', [f.sourceId]);+ const before = await readSyncImportProvenance(engine, f.sourceId, source!.incarnation, f.pageId);+ const scope = await resolveRepairScope(engine, f.sourceId);+ const run = (apply: boolean, expect?: string) => repairRunner(engine, { apply, logger }).then(runner => runner.run('segmented-tiling', scope, { explicit: true, sourceFlag: f.sourceId, ...(expect ? { expect } : {}) }));+ expect(await run(true, (await run(false)).preview_hash!)).toMatchObject({ applied: 1 });+ expect(await readSyncImportProvenance(engine, f.sourceId, source!.incarnation, f.pageId)).toEqual(before);+ const after = await performManagedSync(engine, { sourceId: f.sourceId, noPull: true, noEmbed: true, noExtract: true });+ expect((after.held ?? []).map(hold => hold.code)).toEqual(['file_drift']);+}), 300_000);+test('#8504: an archived source is never listed, a multi-source fix names a source, a preview over its limit says so, an unknown source refuses', async () => withEnv(env, async () => {const f = await managedFixture();const second = 'people/other-example.md';