Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(skills): strip only the leading frontmatter block in brain-pdf, portably (#5772)
garrytanbbf03f5062e7
diff --git a/plugin/skills/brain-pdf/SKILL.md b/plugin/skills/brain-pdf/SKILL.mdindex f18f9eb8c5..a63f73a36c 100644--- a/plugin/skills/brain-pdf/SKILL.md+++ b/plugin/skills/brain-pdf/SKILL.md@@ -83,10 +83,13 @@ elsegbrain get "$SLUG" > "$RAW" # prints the page as markdown with frontmatterfi-# 3. Strip YAML frontmatter — sed: skip the opening '---' through the-# closing '---' (lines 1..N), then keep everything after.+# 3. Strip the leading YAML frontmatter block only: line 1 '---' through+# the next '---'. A later '---' (a horizontal rule) and a page without+# frontmatter are kept as written. Plain awk, so it behaves the same with+# BSD (macOS) and GNU tools. Stop if nothing is left to render.CLEAN=$(mktemp /tmp/brain-page-clean-XXXXXX.md)-sed '1{/^---$/!q}; /^---$/,/^---$/d' "$RAW" > "$CLEAN"+awk 'NR==1 && /^---[[:space:]]*$/ {f=1; next} f==1 && /^---[[:space:]]*$/ {f=2; next} f!=1' "$RAW" > "$CLEAN"+[ -s "$CLEAN" ] || { echo "brain-pdf: nothing to render for $SLUG after removing frontmatter" >&2; exit 1; }# 4. Render. NO --cover, NO --toc by default — they look corporate# and waste space. Add them only if explicitly requested.diff --git a/skills/brain-pdf/SKILL.md b/skills/brain-pdf/SKILL.mdindex f18f9eb8c5..a63f73a36c 100644--- a/skills/brain-pdf/SKILL.md+++ b/skills/brain-pdf/SKILL.md@@ -83,10 +83,13 @@ elsegbrain get "$SLUG" > "$RAW" # prints the page as markdown with frontmatterfi-# 3. Strip YAML frontmatter — sed: skip the opening '---' through the-# closing '---' (lines 1..N), then keep everything after.+# 3. Strip the leading YAML frontmatter block only: line 1 '---' through+# the next '---'. A later '---' (a horizontal rule) and a page without+# frontmatter are kept as written. Plain awk, so it behaves the same with+# BSD (macOS) and GNU tools. Stop if nothing is left to render.CLEAN=$(mktemp /tmp/brain-page-clean-XXXXXX.md)-sed '1{/^---$/!q}; /^---$/,/^---$/d' "$RAW" > "$CLEAN"+awk 'NR==1 && /^---[[:space:]]*$/ {f=1; next} f==1 && /^---[[:space:]]*$/ {f=2; next} f!=1' "$RAW" > "$CLEAN"+[ -s "$CLEAN" ] || { echo "brain-pdf: nothing to render for $SLUG after removing frontmatter" >&2; exit 1; }# 4. Render. NO --cover, NO --toc by default — they look corporate# and waste space. Add them only if explicitly requested.diff --git a/skills/skills.lock.json b/skills/skills.lock.jsonindex 4195df7127..2c63ec52fd 100644--- a/skills/skills.lock.json+++ b/skills/skills.lock.json@@ -21,7 +21,7 @@"brain-link-discipline/SKILL.md": "489268f71a01abb6213df8a8bb5f4d16d587b09dd670ac3c47876a8ed808b5ae","brain-link-discipline/routing-eval.jsonl": "0adea4c6d72fdb849c12fe2cad718568b9ab704299b00b093c40c2284faf684c","brain-ops/SKILL.md": "075666e81df0b5f8ab23493d4972ef5ba42bc789ae213d3bc03e5d3b9c38a3bf",- "brain-pdf/SKILL.md": "68eb85679718afe504179be780ced483b1fe5916b7b294bd202dceca2d9c9398",+ "brain-pdf/SKILL.md": "c24727ade7c222606249bbf690ddede1bebe218122c9cf9060a2baa6b63d3bc8","brain-pdf/routing-eval.jsonl": "119e4fa113ea45783cee4499e63a729fdeecb4d9a45d47497754b4f5b21d0734","brain-taxonomist/SKILL.md": "dea4557b540868ec2c56bf43ee7f63c5d03a22d4047cd0dfbeaf19adef334f60","brain-taxonomist/routing-eval.jsonl": "8b485b3d735aace60be703854f0f2e9d97c52d52564efdaf7334a0c39e8d20ae",diff --git a/test/brain-pdf-frontmatter-strip.test.ts b/test/brain-pdf-frontmatter-strip.test.tsnew file mode 100644index 0000000000..d4ae5508bc--- /dev/null+++ b/test/brain-pdf-frontmatter-strip.test.ts@@ -0,0 +1,58 @@+/**+ * #5772: brain-pdf stripped frontmatter with a GNU-only sed range that BSD sed+ * (macOS) rejects (empty PDF) and that GNU sed also got wrong: a body `---`+ * rule restarted the range and deleted everything after it, and a page+ * without frontmatter kept only its first line. These run the strip step as+ * written in skills/brain-pdf/SKILL.md (and its generated plugin copy).+ */++import { describe, test, expect } from 'bun:test';+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';+import { spawnSync } from 'child_process';+import { join, resolve } from 'path';+import { tmpdir } from 'os';++const REPO = resolve(import.meta.dir, '..');++/** The skill's strip step: the line that writes "$CLEAN", plus its guard. */+function stripStep(skillPath: string): string {+ const lines = readFileSync(join(REPO, skillPath), 'utf-8').split('\n');+ const i = lines.findIndex(l => l.includes('"$RAW" > "$CLEAN"'));+ if (i < 0) throw new Error(`no strip step in ${skillPath}`);+ return lines.slice(i, i + 2).filter(l => l.includes('"$CLEAN"')).join('\n');+}++function strip(skillPath: string, page: string): { out: string; status: number | null; stderr: string } {+ const dir = mkdtempSync(join(tmpdir(), 'gbrain-5772-'));+ try {+ writeFileSync(join(dir, 'raw.md'), page);+ const r = spawnSync('bash', ['-c', `${stripStep(skillPath)}\ncat "$CLEAN"`], {+ env: { PATH: process.env.PATH ?? '/usr/bin:/bin', RAW: join(dir, 'raw.md'), CLEAN: join(dir, 'clean.md'), SLUG: 'notes/x' },+ encoding: 'utf-8',+ });+ return { out: r.stdout, status: r.status, stderr: r.stderr };+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+}++for (const skill of ['skills/brain-pdf/SKILL.md', 'plugin/skills/brain-pdf/SKILL.md']) {+ describe(`#5772 brain-pdf frontmatter strip (${skill})`, () => {+ test('removes only the leading frontmatter block; a body --- rule and what follows survive', () => {+ const r = strip(skill, '---\ntitle: X\ntype: note\n---\n# X\n\npara1\n\n---\n\npara2 after hr\n');+ expect(r.status).toBe(0);+ expect(r.out).toBe('# X\n\npara1\n\n---\n\npara2 after hr\n');+ });++ test('a page without frontmatter keeps every line', () => {+ const page = '# Plain page\n\nline two\n\nline three\n';+ expect(strip(skill, page).out).toBe(page);+ });++ test('a page that is only frontmatter stops instead of rendering an empty PDF', () => {+ const r = strip(skill, '---\ntitle: X\n---\n');+ expect(r.status).toBe(1);+ expect(r.stderr).toContain('nothing to render for notes/x');+ });+ });+}