Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(GST-1209): review pass 2: 24 MiB screen bound, access-changing files held, withdrawal re-check in the apply transaction, previewed hold binding
garrytanbdd187fcfc3f
diff --git a/docs/guides/repair.md b/docs/guides/repair.mdindex f292f2e7ae..adef4631e0 100644--- a/docs/guides/repair.md+++ b/docs/guides/repair.md@@ -448,7 +448,10 @@ is listed `held` and never written. So is a file that another sync holdwithholds (any hold but the `page_write_failed` one a sync opens when itcannot read the stored body), and a file the next sync's import screen wouldhold: purged page content, a write-gate reject, private visibility it would-not store, a fence it refuses. A source without a Git checkout, a+not store, a fence it refuses. A file that changes who can read the page+(`visibility` or `readable_by`) is held too, because the apply keeps the+stored frontmatter, and so is a file over 24 MiB, the most the import screen+reads whole. A source without a Git checkout, arepo-less brain included, refuses with `not_a_git_repo`: this repairrepublishes the copy Git holds, and there the database has no other copy.On such a brain, rewrite the page from a copy you hold (`put_page`).@@ -459,9 +462,11 @@ page, in one transaction under the page's key lock (a coordinated maintenancewrite on a managed brain), it versions the damaged generation by reference,screens the blob again, publishes the body the screen's fence step and thepurge and withdrawal overlay admit (a claim the owner purged stays out even-when the file still carries it) as a new generation through the same flip that+when the file still carries it; a withdrawal committed after the preview+makes the page `changed_since_preview`) as a new generation through the same flip thatrefuses 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,+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 thestored page in anything else imports normally) and writes the page's receipt(`op_checkpoints` op `segmented-tiling-receipt`) and a `segmented-tiling` auditdiff --git a/src/core/repair/segmented-tiling.ts b/src/core/repair/segmented-tiling.tsindex ae03e416a3..969b3715a9 100644--- a/src/core/repair/segmented-tiling.ts+++ b/src/core/repair/segmented-tiling.ts@@ -10,7 +10,8 @@* the sync hold on it. The hash binds the pages, their revisions and generations, the blobs and the holds. A page with no* file, a file HEAD does not hold or one over pages.max_bytes is listed held and never written, as is one another sync* hold withholds (any but the unreadable-body `page_write_failed` hold) or one the next sync's import screen would hold- * (purged page content, the write gate, private visibility, a fence it refuses). A source without a Git+ * (purged page content, the write gate, private visibility, a fence it refuses), one that changes who can read the page+ * (the apply keeps the stored frontmatter) or one over the 24 MiB the screen reads whole. A source without a Git* checkout (a repo-less brain) refuses: this repair republishes the copy Git holds, and the database has no other.** `--apply --expect <hash>` re-checks every page (`preview_changed`), then per page, in one transaction under the page's@@ -45,6 +46,8 @@ import { applySegmentedBody, sealSegmentedPage, segmentedLivePage } from '../perimport { withPageTierKept } from '../trust/fence-append.ts';import { prepareSegmentedBody, stringBodyFlags, stringRegion } from '../page-body/ingest.ts';import { readPageBodyPolicy } from '../page-body/limits.ts';+import { PAGE_MAX_BYTES } from '../page-size-policy.ts';+import { assertPreparedFactWithdrawals } from '../facts/withdrawal.ts';import { buildSegmentedProjection } from '../page-body/projection.ts';import { untiledSegmentedPages, type UntiledPage } from '../page-body/body-tiling.ts';import { isQuarantined } from '../quarantine.ts';@@ -100,6 +103,7 @@ function headBlob(root: string, path: string, maxBytes: number): { head: string;if (!oid || gitText(root, ['cat-file', '-t', oid]) !== 'blob') return { held: `HEAD (${head.slice(0, 12)}) holds no file at ${path}` };const bytes = Number(gitText(root, ['cat-file', '-s', oid]));if (bytes > maxBytes) return { held: `the file at HEAD is ${bytes} bytes, over pages.max_bytes (${maxBytes})` };+ if (bytes > PAGE_MAX_BYTES) return { held: `the file at HEAD is ${bytes} bytes, over the ${PAGE_MAX_BYTES} bytes this repair screens whole as the next sync's import would` };return { head, oid, bytes };}@@ -137,6 +141,11 @@ async function stageable(engine: BrainEngine, page: { source_id: string; slug: ssnapshot: 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<{ frontmatter: Record<string, unknown> | null }>('SELECT frontmatter FROM pages WHERE source_id=$1 AND slug=$2', [page.source_id, page.slug]);+ const access = (fm: Record<string, unknown> | null | undefined) => JSON.stringify([fm?.visibility ?? null, fm?.readable_by ?? null]);+ if (access(parsed.frontmatter) !== access(stored?.frontmatter)) {+ return { held: 'the file changes who can read the page (visibility or readable_by), 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(),timeline: (await preserveWithdrawnFenceRows(engine, page.source_id, body.timeline ?? '', page.slug)).trim() };@@ -164,11 +173,18 @@ async function survey(engine: BrainEngine, selection: Selection): Promise<{ pagereturn { pages: pages.sort((a, b) => a.page_id - b.page_id), listing, held };}-/** Republishes one page's body (`stageable`'s) in the caller's transaction and clears `hold`; false when the page changed since the preview. */-async function republish(tx: BrainEngine, page: HealPage, body: { truth: string; timeline: string }, hold: GitHoldRecord | null): Promise<{ generation: number; unsealed: boolean; hold_cleared: boolean } | false> {+/**+ * 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> {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;+ try { await assertPreparedFactWithdrawals(tx, page.source_id, body.truth, body.timeline, page.slug); } catch (error) {+ if (error instanceof OperationError && error.code === 'revision_conflict') return false;+ throw error;+ }const [row] = await tx.executeRaw<{ title: string; type: string; page_kind: string; frontmatter: Record<string, unknown> | null; contextual_retrieval_mode: string | null;corpus_generation: string | null; tags: string[] }>(`SELECT p.title, p.type, p.page_kind, p.frontmatter, p.contextual_retrieval_mode, p.corpus_generation,COALESCE((SELECT jsonb_agg(t.tag ORDER BY t.tag) FROM tags t WHERE t.page_id=p.id), '[]'::jsonb) AS tags FROM pages p WHERE p.id=$1`, [page.page_id]);@@ -187,8 +203,8 @@ 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);- const hold_cleared = hold !== null- && await clearGitHold(tx, { sourceId: page.source_id, incarnation: page.incarnation, path: page.source_path, observedAt: hold.observed_at });+ 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]);return { generation: prepared.newGeneration, unsealed: !applied.complete, hold_cleared };}@@ -258,7 +274,7 @@ export const segmentedTilingRepair: RepairHandler = {const written = await ctx.engine.transaction(async tx => {const attribution = await maintenanceAttribution(tx);const write = async () => {- const result = await republish(tx, page, staged, staged.hold);+ const result = await republish(tx, page, staged);if (!result) return null;const [after] = await tx.executeRaw<{ revision: string }>('SELECT knowledge_revision::text AS revision FROM pages WHERE id=$1', [page.page_id]);const receipt = { kind: 'segmented-tiling', hash, source_id: page.source_id, slug: page.slug, page_id: page.page_id, revision_before: page.revision,diff --git a/test/large-pages/segmented-tiling-repair.test.ts b/test/large-pages/segmented-tiling-repair.test.tsindex 83e9c031a0..4bdcdf14b8 100644--- a/test/large-pages/segmented-tiling-repair.test.ts+++ b/test/large-pages/segmented-tiling-repair.test.ts@@ -159,6 +159,22 @@ test('a page HEAD no longer holds, or whose file another sync hold withholds, isexpect(await readGitHold(engine, f.sourceId, source!.incarnation, FILE)).toMatchObject({ code: 'write_gate_rejected' });}), 300_000);+test('a file the next sync would hold, or one that changes who can read the page, is listed held and never written', async () => withEnv(env, async () => {+ const f = await managedFixture();+ await damage(f.sourceId, 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 } : {}) }));+ const [damaged] = await untiledSegmentedPages(engine, { sourceIds: [f.sourceId] });+ for (const [frontmatter, detail] of [['slug: people/other-example', 'the next sync would hold the file ('], ['readable_by: [alice-example]', 'changes who can read the page']] as const) {+ writeFileSync(join(f.root, FILE), person(1260 * 1024).replace('type: person\n', `type: person\n${frontmatter}\n`));+ commit(f.root);+ const preview = await run(false);+ expect(preview.listing).toEqual([expect.objectContaining({ class: 'held', detail: expect.stringContaining(detail) })]);+ expect(await run(true, preview.preview_hash!)).toMatchObject({ applied: 0 });+ expect(await untiledSegmentedPages(engine, { sourceIds: [f.sourceId] })).toEqual([damaged!]);+ }+}), 300_000);+test('the apply refuses preview_changed when HEAD moved after the preview, and writes nothing', async () => withEnv(env, async () => {const f = await managedFixture();await damage(f.sourceId, f.pageId);