Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(frontmatter): --fix keeps correct slug fields (#5053)
garrytana2d2c7896a40
diff --git a/src/commands/frontmatter.ts b/src/commands/frontmatter.tsindex 4258b57d5f..350d468652 100644--- a/src/commands/frontmatter.ts+++ b/src/commands/frontmatter.ts@@ -216,7 +216,8 @@ async function runValidate(rest: string[]): Promise<void> {const rel = relative(brainRoot, file);// Files above/outside the brain root fall back to basename rather than// emitting a "../"-prefixed slug for non-brain files.- const expectedSlug = slugifyPath(rel && !rel.startsWith('..') ? rel : basename(file));+ const slugPath = rel && !rel.startsWith('..') ? rel : basename(file);+ const expectedSlug = slugifyPath(slugPath);const parsed = parseMarkdown(content, file, { validate: true, expectedSlug });const errs = parsed.errors ?? [];const result: FileValidation = {@@ -225,7 +226,9 @@ async function runValidate(rest: string[]): Promise<void> {};if (flags.fix && errs.length > 0) {- const { content: fixed, fixes } = autoFixFrontmatter(content, { filePath: file });+ // #5053: the fixer derives the slug from the same path validate used;+ // the absolute path re-keyed every declared slug as a mismatch.+ const { content: fixed, fixes } = autoFixFrontmatter(content, { filePath: slugPath });result.fixesApplied = fixes;if (fixes.length > 0 && !flags.dryRun) {assertManagedFilesystemWrite(file);diff --git a/src/core/brain-writer.ts b/src/core/brain-writer.tsindex 86b9aed01f..0ed94705df 100644--- a/src/core/brain-writer.ts+++ b/src/core/brain-writer.ts@@ -126,6 +126,9 @@ export function createFrontmatterBackup(filePath: string, opts: FrontmatterBacku* - MISSING_CLOSE — insert `---` before the first heading found inside* the YAML zone* - SLUG_MISMATCH — remove `slug:` line (gbrain derives slug from path)+ * when `opts.filePath` is given. It must be the path+ * the slug derives from (relative to the brain or+ * source root, #5053), never an absolute path.** Idempotent: running twice is a no-op on already-clean input. Any error class* not in the list above is left untouched (e.g. EMPTY_FRONTMATTER, YAML_PARSE,@@ -374,7 +377,7 @@ export function writeBrainPage(let toWrite = content;let fixes: AuditFix[] = [];if (opts.autoFix) {- const result = autoFixFrontmatter(content, { filePath });+ const result = autoFixFrontmatter(content, { filePath: relative(resolvedSource, resolvedTarget) });toWrite = result.content;fixes = result.fixes;}diff --git a/test/frontmatter-fix-slug-5053.test.ts b/test/frontmatter-fix-slug-5053.test.tsnew file mode 100644index 0000000000..632a38158b--- /dev/null+++ b/test/frontmatter-fix-slug-5053.test.ts@@ -0,0 +1,85 @@+/**+ * #5053 — `frontmatter validate --fix` passed the ABSOLUTE file path to the+ * fixer, whose slug check then saw every declared `slug:` as a mismatch and+ * deleted it (re-keying the page on the next import). Both callers of+ * `autoFixFrontmatter` now pass the path the slug derives from: validate+ * passes the brain-root-relative path (or the basename outside a brain), and+ * `writeBrainPage` passes the path relative to its source.+ */+import { afterEach, beforeEach, describe, expect, test } from 'bun:test';+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { spawnSync } from 'node:child_process';+import { writeBrainPage } from '../src/core/brain-writer.ts';++// A NESTED_QUOTES error gives --fix something to repair, so the fixer runs.+const page = (slug: string) => `---\ntype: note\ntitle: "a "quoted" title"\nslug: ${slug}\n---\n\nbody\n`;++let scratch: string;+let brain: string;++beforeEach(() => {+ scratch = mkdtempSync(join(tmpdir(), 'fm-5053-'));+ brain = join(scratch, 'brain');+ mkdirSync(join(brain, '.git'), { recursive: true });+ mkdirSync(join(brain, 'notes'), { recursive: true });+});++afterEach(() => { rmSync(scratch, { recursive: true, force: true }); });++function validateFix(target: string): string {+ const result = spawnSync(process.execPath, ['run', 'src/cli.ts', 'frontmatter', 'validate', target, '--fix'], {+ encoding: 'utf8', cwd: process.cwd(),+ env: { ...process.env, GBRAIN_HOME: join(scratch, 'home'), DATABASE_URL: '', GBRAIN_DATABASE_URL: '' },+ });+ return (result.stdout ?? '') + (result.stderr ?? '');+}++describe('#5053 frontmatter validate --fix', () => {+ test('a correct slug survives --fix while the real error is repaired', () => {+ const file = join(brain, 'notes', 'foo-bar.md');+ writeFileSync(file, page('notes/foo-bar'));+ const output = validateFix(file);+ const fixed = readFileSync(file, 'utf8');+ expect(fixed).toContain('slug: notes/foo-bar');+ expect(fixed).not.toContain('"a "quoted" title"');+ expect(output).not.toContain('Removed mismatched slug');+ }, 60_000);++ test('a legacy-identity slug whose slugified spelling matches the path survives (#3772)', () => {+ const file = join(brain, 'notes', 'foo-bar.md');+ writeFileSync(file, page('Notes/Foo Bar'));+ validateFix(file);+ expect(readFileSync(file, 'utf8')).toContain('slug: Notes/Foo Bar');+ }, 60_000);++ test('a truly mismatched slug is still removed', () => {+ const file = join(brain, 'notes', 'foo-bar.md');+ writeFileSync(file, page('other/thing'));+ validateFix(file);+ expect(readFileSync(file, 'utf8')).not.toContain('slug:');+ }, 60_000);++ test('a file outside any brain root keeps a slug that matches its basename', () => {+ const loose = join(scratch, 'loose');+ mkdirSync(loose);+ const file = join(loose, 'note.md');+ writeFileSync(file, page('note'));+ validateFix(file);+ expect(readFileSync(file, 'utf8')).toContain('slug: note');+ }, 60_000);+});++describe('#5053 writeBrainPage autoFix', () => {+ test('derives the slug relative to its source: a correct slug survives, a mismatched one is removed', () => {+ const backupRoot = join(scratch, 'backups');+ const kept = writeBrainPage(join(brain, 'notes', 'foo-bar.md'), page('notes/foo-bar'), { sourcePath: brain, autoFix: true, backupRoot });+ expect(kept.fixes.map((fix) => fix.code)).not.toContain('SLUG_MISMATCH');+ expect(readFileSync(join(brain, 'notes', 'foo-bar.md'), 'utf8')).toContain('slug: notes/foo-bar');++ const removed = writeBrainPage(join(brain, 'notes', 'other.md'), page('notes/foo-bar'), { sourcePath: brain, autoFix: true, backupRoot });+ expect(removed.fixes.map((fix) => fix.code)).toContain('SLUG_MISMATCH');+ expect(readFileSync(join(brain, 'notes', 'other.md'), 'utf8')).not.toContain('slug:');+ });+});