Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes
garrytan5b5942a70049
diff --git a/src/core/import-file.ts b/src/core/import-file.tsindex 2c44b2984b..a8a394031f 100644--- a/src/core/import-file.ts+++ b/src/core/import-file.ts@@ -488,7 +488,7 @@ export async function importFromContent(// put (get_page → edit body → put_page without `type:`) silently regressed// a curated type to the path-inferred default ('concept' for bare slugs).// Explicit frontmatter type stays an override; new pages still infer. The stored subtype and grandfather stamp are kept too.- if (existing) alignWithStored(parsed, existing, false, opts.sourcePath !== undefined);+ if (existing) alignWithStored(parsed, existing, false, opts.sourcePath !== undefined, true);else resolveParsedSubtype(parsed, existing);// Alias-footgun visibility: an explicit frontmatter `type:` that is an// ALIAS of a canonical pack type (or entirely undeclared) is storeddiff --git a/src/core/persistence/align-stored.ts b/src/core/persistence/align-stored.tsindex 10d811b6f4..f4d3f014ad 100644--- a/src/core/persistence/align-stored.ts+++ b/src/core/persistence/align-stored.ts@@ -9,15 +9,15 @@ import { inferTypeFromPack, resolveParsedSubtype, type ParsedMarkdown } from '..* content a caller writes with no file behind it (import-file.ts without `sourcePath`: `put_page`) passes `retypeStamp` false and is* read as written, so an explicit type there still wins.*/-export function alignWithStored(parsed: ParsedMarkdown, stored: { type: string; frontmatter?: Record<string, unknown> | null }, inferred: boolean, retypeStamp = true): void {- if (!(retypeStamp && (keepRetypeStamp(parsed, stored) || keepPackRenamedType(parsed, stored))) && (inferred || !parsed.typeExplicit)) parsed.type = stored.type as ParsedMarkdown['type'];+export function alignWithStored(parsed: ParsedMarkdown, stored: { type: string; frontmatter?: Record<string, unknown> | null }, inferred: boolean, retypeStamp = true, write = false): void {+ if (!(retypeStamp && (keepRetypeStamp(parsed, stored) || keepPackRenamedType(parsed, stored, write))) && (inferred || !parsed.typeExplicit)) parsed.type = stored.type as ParsedMarkdown['type'];resolveParsedSubtype(parsed, stored);keepGrandfatherStamp(parsed.frontmatter, stored);}/** Sync's reading of a file over the stored page (sync-prepare.ts): the retype stamp, the subtype and the grandfather stamp kept as `alignWithStored` keeps them; an explicit file type is otherwise read as written. */export function keepStoredStamps(parsed: ParsedMarkdown, stored?: { type: string; frontmatter?: Record<string, unknown> | null } | null): void {- if (!keepRetypeStamp(parsed, stored)) keepPackRenamedType(parsed, stored);+ if (!keepRetypeStamp(parsed, stored)) keepPackRenamedType(parsed, stored, true);resolveParsedSubtype(parsed, stored);keepGrandfatherStamp(parsed.frontmatter, stored);}@@ -44,12 +44,15 @@ export function keepRetypeStamp(parsed: ParsedMarkdown, stored?: { type: string;* (`writing/` is `writing`). A later pack infers another type for the same path and the column took it, so the file and the* stored copy still say the 1.0.0 name. A file whose explicit type is that name, equal to the stored copy and to what* gbrain-base 1.0.0 infers for its path, while the column says otherwise, is the page as renamed: it keeps the stored type.- * Any other explicit type (a person's) is read as written. Returns whether the stored type was kept.+ * Any other explicit type (a person's) is read as written. A write (`write`) records the rename as a retype stamp+ * (`legacy_type`: the 1.0.0 name), because the stored copy does not survive the import, so every later import keeps the type+ * through `keepRetypeStamp`; a comparison only reads it. Returns whether the stored type was kept.*/-export function keepPackRenamedType(parsed: ParsedMarkdown, stored?: { type: string; frontmatter?: Record<string, unknown> | null } | null): boolean {+export function keepPackRenamedType(parsed: ParsedMarkdown, stored?: { type: string; frontmatter?: Record<string, unknown> | null } | null, write = false): boolean {const copy = stored?.frontmatter?.type;if (!stored || parsed.typeExplicit !== true || typeof copy !== 'string' || parsed.type !== copy || parsed.type === stored.type || inferTypeFromPack(parsed.slug, { page_types: [] }) !== parsed.type) return false;parsed.type = stored.type as ParsedMarkdown['type'];+ if (write) parsed.frontmatter.legacy_type = copy;return true;}diff --git a/src/core/persistence/moved-pages.ts b/src/core/persistence/moved-pages.tsindex e807fac595..4c7db76186 100644--- a/src/core/persistence/moved-pages.ts+++ b/src/core/persistence/moved-pages.ts@@ -11,9 +11,11 @@* 2. the stale side is untouched: the old page still holds what that blob imports (`fileMatchesSnapshot`, which reads* gbrain's own database-only artefacts as derived: `alignWithStored`). A page edited in the database after the move* is `both_changed`.- * Exactly one live page of the source may descend from the blob; two or more are `several_candidates`. The file name+ * Exactly one live page of the source may descend from the blob; two or more are `several_candidates`, and a moved page that+ * another old page with the same content also claims is `shared_survivor`. A first commit counts only after the old path existed. The file name* only explains a result with no candidate (a move can rename the file too), and a frontmatter `id` alone never pairs- * two pages. A history too shallow to read the move, or one Git cannot answer, keeps the page held and says so.+ * two pages. A page stored in segments (over 1 MiB) is `segmented_versions`: its versions name body generations of their+ * own page, so they cannot move to another. A history too shallow to read the move, or one Git cannot answer, keeps the page held and says so.*/import { basename } from 'node:path';import type { BrainEngine } from '../engine.ts';@@ -25,8 +27,8 @@ import { literalPathspecs } from '../git-env.ts';export interface MovedPageRef { page_id: number; slug: string; revision: string; path: string }export type MovedEvidence = 'same_commit' | 'first_commit';-export const MOVED_HELD_REASONS = ['not_moved', 'several_candidates', 'both_changed', 'first_blob_differs', 'same_id_body_differs', 'evidence_conflicts',- 'history_shallow', 'history_unknown', 'deletion_not_committed'] as const;+export const MOVED_HELD_REASONS = ['not_moved', 'several_candidates', 'shared_survivor', 'both_changed', 'first_blob_differs', 'same_id_body_differs', 'evidence_conflicts',+ 'segmented_versions', 'history_shallow', 'history_unknown', 'deletion_not_committed'] as const;export type MovedHeldReason = typeof MOVED_HELD_REASONS[number];export type MovedPageVerdict =| { verdict: 'moved'; survivor: MovedPageRef; old_blob: string; move_commit: string; evidence: MovedEvidence; evidence_commit: string; detail: string }@@ -36,17 +38,18 @@ const git = (root: string, args: string[]): string | null => { try { return syncconst shallow = (root: string) => git(root, ['rev-parse', '--is-shallow-repository'])?.trim() === 'true';const short = (sha: string) => sha.slice(0, 12);-/** Paths (relative to `root`) that took `blob` in `commit` itself (added or changed to it there). */-function sameCommitPaths(root: string, commit: string, blob: string): string[] | null {+/** Paths (relative to `root`) that took `blob` in `commit` itself (added or changed to it there), and those it deleted holding `blob`. */+function sameCommitPaths(root: string, commit: string, blob: string): { paths: string[]; deleted: string[] } | null {const out = git(root, ['diff-tree', '-r', '--no-renames', '--no-commit-id', '--relative', '-z', `${commit}^`, commit]);if (out === null) return null;const fields = out.split('\0');- const paths: string[] = [];+ const paths: string[] = [], deleted: string[] = [];for (let i = 0; i + 1 < fields.length; i += 2) {- const [, , , next, status] = fields[i]!.split(' ');+ const [, , previous, next, status] = fields[i]!.split(' ');if (next === blob && status !== 'D') paths.push(fields[i + 1]!);+ if (previous === blob && status === 'D') deleted.push(fields[i + 1]!);}- return paths;+ return { paths, deleted };}/** Paths (relative to `root`) whose first commit in HEAD's history added them with `blob`, with that commit. */@@ -96,10 +99,16 @@ export async function readMovedPage(engine: BrainEngine, input: { sourceId: stri: held('history_unknown', `Git could not read ${old.path} before commit ${short(commit)}, which deleted it`);const same = sameCommitPaths(root, commit, blob);const first = firstCommitPaths(root, blob);- if (same === null || first === null) return held('history_unknown', `Git could not read which paths took ${old.path}'s content (blob ${short(blob)})`);+ const added = git(root, ['log', '--no-renames', '--relative', '--diff-filter=A', '--format=%H', '-1', `${commit}^`, '--', ...literalPathspecs([old.path])])?.trim();+ if (same === null || first === null || !added) return held('history_unknown', `Git could not read which paths took ${old.path}'s content (blob ${short(blob)})`);const evidence = new Map<string, { evidence: MovedEvidence; commit: string }>();- for (const [path, at] of first) if (path !== old.path) evidence.set(path, { evidence: 'first_commit', commit: at });- for (const path of same) if (path !== old.path) evidence.set(path, { evidence: 'same_commit', commit });+ for (const [path, at] of first) {+ if (path === old.path || at === added) continue;+ const base: string | undefined = git(root, ['merge-base', added, at])?.trim();+ if (!base) return held('history_unknown', `Git could not order ${path}'s first commit ${short(at)} against the commit that added ${old.path}`);+ if (base === added) evidence.set(path, { evidence: 'first_commit', commit: at });+ }+ for (const path of same.paths) if (path !== old.path) evidence.set(path, { evidence: 'same_commit', commit });const present = inHead(root, [...evidence.keys()]);const rows = present.size ? await engine.executeRaw<{ id: number; slug: string; revision: string; source_path: string }>(`SELECT id, slug, knowledge_revision::text AS revision, source_pathFROM pages WHERE source_id=$1 AND deleted_at IS NULL AND id<>$2 AND source_path=ANY($3::text[]) ORDER BY id`, [sourceId, old.page_id, [...present]]) : [];@@ -119,6 +128,11 @@ export async function readMovedPage(engine: BrainEngine, input: { sourceId: strireturn held('not_moved', `no live page of the source started from ${old.path}'s last committed content (blob ${short(blob)}), so commit ${short(commit)} deleted it rather than moving it`);}const row = rows[0]!;+ const others = same.deleted.filter(path => path !== old.path);+ if (others.length) return held('shared_survivor', `commit ${short(commit)} deleted ${[old.path, ...others].join(', ')} with the same content (blob ${short(blob)}), so which of them moved to ${row.source_path} is a person's decision`, [row.slug]);+ const [storage] = await engine.executeRaw<{ segmented: boolean }>(`SELECT EXISTS (SELECT 1 FROM pages WHERE id=$1 AND body_storage<>'inline')+ OR EXISTS (SELECT 1 FROM page_versions WHERE page_id=$1 AND body_storage<>'inline') AS segmented`, [old.page_id]);+ if (storage?.segmented) return held('segmented_versions', `${old.path} moved to ${row.source_path}, but page ${old.slug} or one of its saved versions is stored in segments (over 1 MiB), whose body generations belong to ${old.slug} and cannot move to ${row.slug}; fold it by hand with the user`, [row.slug]);const survivor: MovedPageRef = { page_id: Number(row.id), slug: row.slug, revision: String(row.revision), path: row.source_path };const proof = evidence.get(row.source_path)!;const snapshot = await engine.readPageSnapshot(old.slug, { sourceId });diff --git a/src/core/repair/moved-pages.ts b/src/core/repair/moved-pages.tsindex 4fefd1743d..9bc55312ec 100644--- a/src/core/repair/moved-pages.ts+++ b/src/core/repair/moved-pages.ts@@ -87,7 +87,7 @@ export const movedPagesRepair: RepairHandler = {const sourceId = scope.source_ids[0]!;const command = shellQuote(movedPagesPreviewArgv(sourceId));const slugs = opts?.slugs?.length ? [...new Set(opts.slugs)].sort() : undefined;- const scan = await scanGhostSource(engine, sourceId);+ const scan = await scanGhostSource(engine, sourceId, { candidateLimit: GHOST_REPAIR_LIMIT });if (scan.status === 'not_applicable' && scan.reason === 'not_applicable') {const hash = previewHash({ kind: 'moved-pages-v1', brain_id: scope.brain_id, selection: { source_ids: scope.source_ids, slugs: slugs ?? null }, empty: true });return { items: [], preview_hash: opts?.expect ?? hash, residuals: { moved: 0 },@@ -96,17 +96,25 @@ export const movedPagesRepair: RepairHandler = {const refusal = ghostScanRefusal(scan);if (refusal) throw refusal;const owner = scan.owner!, root = scan.root!;- const ghosts = scan.ghosts.filter(ghost => !slugs || slugs.includes(ghost.slug)).slice(0, GHOST_REPAIR_LIMIT);+ const named = slugs ? await Promise.all(slugs.map(async slug => (await ghostPageNow(engine, sourceId, slug)).ghost)) : [];+ const ghosts = slugs ? named.filter(ghost => ghost !== null) : scan.ghosts.slice(0, GHOST_REPAIR_LIMIT);const provenance = await readDeletionProvenance(root, ghosts.map(ghost => ghost.path));const observedAt = new Date().toISOString();const pairs: MovedPair[] = [], held: HeldPage[] = [];+ const found: MovedPair[] = [];for (const ghost of ghosts) {const old: MovedPageRef = { page_id: ghost.page_id, slug: ghost.slug, revision: ghost.revision, path: ghost.path };const verdict = await readMovedPage(engine, { sourceId, root, old, provenance: provenance.get(ghost.path) ?? { kind: 'unknown' } });- if (verdict.verdict === 'moved') pairs.push({ source_id: sourceId, old, survivor: verdict.survivor, old_blob: verdict.old_blob, move_commit: verdict.move_commit,+ if (verdict.verdict === 'moved') found.push({ source_id: sourceId, old, survivor: verdict.survivor, old_blob: verdict.old_blob, move_commit: verdict.move_commit,evidence: verdict.evidence, evidence_commit: verdict.evidence_commit, observed_at: observedAt });else held.push({ source_id: sourceId, slug: ghost.slug, path: ghost.path, reason: verdict.reason, detail: verdict.detail, ...(verdict.candidates ? { candidates: verdict.candidates } : {}) });}+ for (const pair of found) {+ const claims = found.filter(other => other.survivor.page_id === pair.survivor.page_id);+ if (claims.length === 1) { pairs.push(pair); continue; }+ held.push({ source_id: sourceId, slug: pair.old.slug, path: pair.old.path, reason: 'shared_survivor', candidates: [pair.survivor.slug],+ detail: `${claims.map(other => other.old.path).join(', ')} all moved to ${pair.survivor.path} by their content, so which one it is is a person's decision` });+ }const missing = slugs?.filter(slug => !ghosts.some(ghost => ghost.slug === slug)) ?? [];const mass = scan.file_backed > MASS_RECONCILE_MIN_PAGES && pairs.length > scan.file_backed * MASS_RECONCILE_RATIO;if (!opts?.apply) {@@ -115,7 +123,7 @@ export const movedPagesRepair: RepairHandler = {const warnings = [...(missing.length ? [`Not a live page whose file is gone in source ${sourceId}, so nothing to fold: ${missing.join(', ')}.`] : []),...(mass ? [`${pairs.length} fold(s) against ${scan.file_backed} file-backed page(s) (more than ${Math.round(MASS_RECONCILE_RATIO * 100)}%): the apply refuses unless GBRAIN_ALLOW_MASS_RECONCILE=1; check the source first.`] : []),- ...(scan.ghosts.length > ghosts.length && !slugs ? [`More pages whose file is gone may exist past the first ${GHOST_REPAIR_LIMIT} listed; apply this set, then preview again.`] : []),+ ...((scan.truncated || scan.ghosts.length > ghosts.length) && !slugs ? [`More pages whose file is gone may exist past the first ${GHOST_REPAIR_LIMIT} checked; apply this set, then preview again.`] : []),...(held.length ? [`${held.length} page(s) whose file is gone are not proven moved and are never folded by this apply; each lists why.`] : []),...(pairs.length ? [MOVED_PAGES_CONSENT_TEXT.risk] : []),...(!scan.ghosts.length ? [`Source ${sourceId} has no live page whose file is gone; nothing to fold.`] : []),@@ -124,7 +132,7 @@ export const movedPagesRepair: RepairHandler = {residuals: { held: held.length, ...Object.fromEntries([...new Set(held.map(page => page.reason))].map(reason => [reason, held.filter(page => page.reason === reason).length])) },listing: [...pairs.map(pair => ({ item: `${pair.source_id}:${pair.old.slug}`, class: 'moved_to', detail: pairLine(pair) })),...held.map(page => ({ item: `${page.source_id}:${page.slug}`, class: `held:${page.reason}`, detail: page.detail }))],- details: { pairs, held, mass_fold: mass, purge_fuse_hours: MOVED_PAGES_PURGE_FUSE_HOURS, docs: DOCS } };+ details: { pairs, held, mass_fold: mass, truncated: scan.truncated, purge_fuse_hours: MOVED_PAGES_PURGE_FUSE_HOURS, docs: DOCS } };}if (!opts.expect) {throw opError('invalid_params', 'gbrain repair moved-pages --apply folds only the set a preview printed.',diff --git a/test/moved-pages-gst2584.test.ts b/test/moved-pages-gst2584.test.tsindex 5c85eb706a..640d181bb1 100644--- a/test/moved-pages-gst2584.test.ts+++ b/test/moved-pages-gst2584.test.ts@@ -248,7 +248,15 @@ for (const backend of testBackends()) {await performManagedSync(engine, SYNC);expect((await engine.readPageSnapshot('writing/essay', { sourceId: 'default' }))?.page.compiled_truth).toContain('A later line.');expect((await page(engine, 'writing/essay'))!.type).toBe('media');-+ // The rename is kept as a retype stamp, so a second edit keeps the type too, and the file still matches.+ const [stamp] = await engine.executeRaw<{ legacy: string | null }>("SELECT frontmatter->>'legacy_type' AS legacy FROM pages WHERE source_id='default' AND slug='writing/essay'");+ expect(stamp!.legacy).toBe('writing');+ writeFileSync(join(root, 'writing/essay.md'), `${readFileSync(join(root, 'writing/essay.md'), 'utf8')}\nA second line.\n`); git(root, 'commit', '-qam', 'edit again');+ await performManagedSync(engine, SYNC);+ expect((await engine.readPageSnapshot('writing/essay', { sourceId: 'default' }))?.page.compiled_truth).toContain('A second line.');+ expect((await page(engine, 'writing/essay'))!.type).toBe('media');+ const after = (await engine.readPageSnapshot('writing/essay', { sourceId: 'default' }))!;+ expect(await fileMatchesSnapshot(engine, 'writing/essay', readFileSync(join(root, 'writing/essay.md'), 'utf8'), after, 'writing/essay.md')).toBe(true);}, opts(backend, { 'writing/essay.md': '---\ntype: writing\ntitle: Essay\n---\n\nAn essay gbrain wrote out.\n', 'notes/beta.md': BETA })), 240_000);test(`${backend}: a moved page carrying the type rename and the grandfather stamp folds`, () => managedBrain(async ({ engine, root }) => {@@ -262,4 +270,58 @@ for (const backend of testBackends()) {expect(listed(preview, 'writing/essay')).toMatchObject({ class: 'moved_to' });expect((await runner(engine, 'moved-pages', { expect: preview.preview_hash })).outcomes).toEqual({ folded: 1 });}, opts(backend, { 'writing/essay.md': '---\ntype: writing\ntitle: Essay\n---\n\nAn essay gbrain wrote out.\n', 'notes/beta.md': BETA })), 240_000);++ test(`${backend}: a page edited between the preview and the apply is not folded`, () => managedBrain(async ({ engine, root }) => {+ await movedAway(engine, root, mv(root, 'archive/alpha.md'));+ const preview = await runner(engine, 'moved-pages', {});+ expect(preview.affected).toBe(1);+ await raw(engine, "UPDATE pages SET timeline='An edit after the preview.', knowledge_revision=gen_random_uuid() WHERE source_id='default' AND slug='notes/alpha'");+ expect((await runner(engine, 'moved-pages', { expect: preview.preview_hash })).outcomes).toEqual({ changed_since_preview: 1 });+ expect((await page(engine, 'notes/alpha'))!.deleted).toBe(false);+ expect(await engine.executeRaw("SELECT 1 FROM slug_aliases WHERE source_id='default' AND alias_slug='notes/alpha'")).toEqual([]);+ }, opts(backend)), 240_000);++ test(`${backend}: --apply without the preview's hash refuses and folds nothing`, () => managedBrain(async ({ engine, root }) => {+ await movedAway(engine, root, mv(root, 'archive/alpha.md'));+ await expect(cli(engine, async () => (await repairRunner(engine, { apply: true, logger })).run('moved-pages', await resolveRepairScope(engine, 'default'), { explicit: true, sourceFlag: 'default' })))+ .rejects.toMatchObject({ code: 'invalid_params' });+ expect((await page(engine, 'notes/alpha'))!.deleted).toBe(false);+ }, opts(backend)), 240_000);++ test(`${backend}: two old pages with the same content moved to one page stay held (shared survivor)`, () => managedBrain(async ({ engine, root }) => {+ await registerLocalWriter(engine, 'cli');+ expect((await performManagedSync(engine, SYNC)).status).toBe('first_sync');+ const before = await engine.executeRaw<{ slug: string; timeline: string }>("SELECT slug, timeline FROM pages WHERE source_id='default' AND slug IN ('notes/alpha', 'notes/alpha-copy')");+ await raw(engine, "UPDATE pages SET timeline='An edit only the database holds.', knowledge_revision=gen_random_uuid() WHERE source_id='default' AND slug IN ('notes/alpha', 'notes/alpha-copy')");+ mkdirSync(join(root, 'archive'), { recursive: true }); writeFileSync(join(root, 'archive/alpha.md'), ALPHA);+ git(root, 'rm', '-q', 'notes/alpha.md', 'notes/alpha-copy.md'); git(root, 'add', '-A'); git(root, 'commit', '-qm', 'merge two into one');+ await performManagedSync(engine, SYNC);+ for (const row of before) await raw(engine, "UPDATE pages SET timeline=$1, knowledge_revision=gen_random_uuid() WHERE source_id='default' AND slug=$2", [row.timeline, row.slug]);+ const preview = await runner(engine, 'moved-pages', {});+ expect(preview.affected).toBe(0);+ expect(listed(preview, 'notes/alpha')).toMatchObject({ class: 'held:shared_survivor' });+ expect(listed(preview, 'notes/alpha-copy')).toMatchObject({ class: 'held:shared_survivor' });+ const one = await runner(engine, 'moved-pages', { slugs: ['notes/alpha'] });+ expect(listed(one, 'notes/alpha')).toMatchObject({ class: 'held:shared_survivor' });+ }, opts(backend, { 'notes/alpha.md': ALPHA, 'notes/alpha-copy.md': ALPHA, 'notes/beta.md': BETA })), 240_000);++ test(`${backend}: a page with a version stored in segments stays held`, () => managedBrain(async ({ engine, root }) => {+ await movedAway(engine, root, mv(root, 'archive/alpha.md'));+ const old = (await page(engine, 'notes/alpha'))!;+ await raw(engine, "INSERT INTO page_versions (page_id, compiled_truth, timeline, body_storage, body_generation) VALUES ($1, NULL, NULL, 'segmented', 1)", [old.id]);+ const preview = await runner(engine, 'moved-pages', {});+ expect(preview.affected).toBe(0);+ expect(listed(preview, 'notes/alpha')).toMatchObject({ class: 'held:segmented_versions' });+ }, opts(backend)), 240_000);++ test(`${backend}: an older page that started from the same content is not where the file moved`, () => managedBrain(async ({ engine, root }) => {+ await movedAway(engine, root, () => { git(root, 'rm', '-q', 'notes/alpha.md'); git(root, 'commit', '-qm', 'delete'); });+ const preview = await runner(engine, 'moved-pages', {});+ expect(preview.affected).toBe(0);+ expect(listed(preview, 'notes/alpha')).toMatchObject({ class: 'held:not_moved' });+ }, { ...opts(backend), setup: ({ root }: { root: string }) => {+ setup({ 'other/stub.md': ALPHA })({ root });+ for (const [path, content] of Object.entries({ 'notes/alpha.md': ALPHA, 'notes/beta.md': BETA })) { mkdirSync(join(root, path, '..'), { recursive: true }); writeFileSync(join(root, path), content); }+ git(root, 'add', '-A'); git(root, 'commit', '-qm', 'later');+ } }), 240_000);}