From 8415e497d040d4ffc125660dceec4101690a83b9 Mon Sep 17 00:00:00 2001 From: abiwab Date: Thu, 26 Nov 2026 15:36:16 +0100 Subject: [PATCH] fix(cli): drop comments from the import prompt's frontmatter example --- .changeset/import-prompt-frontmatter.md | 5 ++ packages/cli/src/prompts/gram-spec.ts | 25 +++--- packages/cli/tests/ai-spec-examples.test.ts | 93 +++++++++++++++++++++ 3 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 .changeset/import-prompt-frontmatter.md create mode 100644 packages/cli/tests/ai-spec-examples.test.ts diff --git a/.changeset/import-prompt-frontmatter.md b/.changeset/import-prompt-frontmatter.md new file mode 100644 index 00000000..05e7a0b4 --- /dev/null +++ b/.changeset/import-prompt-frontmatter.md @@ -0,0 +1,5 @@ +--- +"@gram-lang/cli": patch +--- + +**CLI**: `gram import` no longer shows the AI model `#` comments in the frontmatter example, which it could copy into the recipe, where they became part of the values (or emptied the whole frontmatter); the prompt now says the frontmatter has no comments, and its reference recipe uses yolks in the lemon curd and reuses the butter with `@&butter`. diff --git a/packages/cli/src/prompts/gram-spec.ts b/packages/cli/src/prompts/gram-spec.ts index 0c133af6..18a56968 100644 --- a/packages/cli/src/prompts/gram-spec.ts +++ b/packages/cli/src/prompts/gram-spec.ts @@ -75,19 +75,19 @@ The file starts with a YAML-like block delimited by --- (see the flat-key rule b \`\`\` --- title: 'Lemon Meringue Pie' -originalTitle: 'Tarte au Citron Meringuée' # original language if different (free-form, informational only) +originalTitle: 'Tarte au Citron Meringuée' description: 'A classic French tart with silky lemon curd and toasted meringue.' -author: 'Auguste Kerflec' # or array: ['Name1', 'Name2'] -source: ['https://example.com/recipe'] # always an array +author: 'Auguste Kerflec' +source: ['https://example.com/recipe'] category: 'Dessert' tags: ['tart', 'lemon', 'french'] date: '2025-06-23' lastUpdated: '2025-06-23' language: 'en' -makes: '24cm tart tin' # yield description or mold dimensions (NOT "size") -portions: 8 # INTEGER — used for scaling and nutrition +makes: '24cm tart tin' +portions: 8 notes: 'Tested June 2025. Reduce sugar by 10g next time.' -densities: [flour:0.55, water:1.0] # optional custom density overrides (g/mL) +densities: [flour:0.55, water:1.0] --- \`\`\` @@ -96,9 +96,14 @@ Rules: block syntax. The only structured value form is an inline array: \`key: [a, b, c]\`. \`densities\` MUST use this array form with \`name:value\` entries, e.g. \`[flour:0.55, water:1.0]\`. A YAML-style indented list (\`- flour: 0.55\`) does NOT parse — never emit it. +- The frontmatter has NO comments. A \`# …\` at the end of a line becomes part of the value, and + a \`# …\` line (or any indented line) silently empties the whole block — never emit either. - String values must be single-quoted when they contain special characters. -- "portions" is the canonical field for serving count (not "servings"). -- "makes" is the canonical field for yield/mold description (the old field name "size" is obsolete — never emit it). +- "originalTitle" is the title in the original language, when it differs (free-form, informational only). +- "author" is a string or an array (\`['Name1', 'Name2']\`); "source" is always an array. +- "portions" is the canonical field for serving count (not "servings"): an INTEGER, used for scaling and nutrition. +- "makes" is the canonical field for yield/mold description, e.g. '24cm tart tin' (the old field name "size" is obsolete — never emit it). +- "densities" is optional: custom density overrides in g/mL. - Time fields (prep/active/total in minutes) are NOT part of the frontmatter — they are derived by the compiler from timers. - Omit any field for which there is no data. Do not invent values. @@ -730,9 +735,9 @@ makes: '24cm tart tin' ## Lemon Curd ->&curd -[Cook] In a #saucepan over ^{medium heat}, whisk @lemon juice{1}<@lemon{3}, @lemon zest{1}<@lemon{3}, @sugar{150g}, and @egg white{3}<@egg{3} continuously. +[Cook] In a #saucepan over ^{medium heat}, whisk @lemon juice{1}<@lemon{3}, @lemon zest{1}<@lemon{3}, @sugar{150g}, and @egg yolk{3}<@egg{3} continuously. -[Thicken] Remove from heat and whisk in @butter{50g} until the curd is glossy and coats a spoon. +[Thicken] Remove from heat and whisk in @&butter{50g} until the curd is glossy and coats a spoon. ## Assembly and Meringue diff --git a/packages/cli/tests/ai-spec-examples.test.ts b/packages/cli/tests/ai-spec-examples.test.ts new file mode 100644 index 00000000..d423617b --- /dev/null +++ b/packages/cli/tests/ai-spec-examples.test.ts @@ -0,0 +1,93 @@ +import { describe, expect, it } from "bun:test"; +import { existsSync, readFileSync } from "node:fs"; +import { join } from "node:path"; +import { getAST } from "@gram-lang/parser"; +import { GRAM_SPEC_PROMPT } from "../src/prompts/gram-spec"; + +// The frontmatter has no comments: a `# …` line (or an indented one) makes the +// whole block fall back to a text step, and a trailing `# …` becomes part of +// the value. The examples the AI copies (`gram import`'s prompt, the +// gram-authoring skill) once had both, so every frontmatter they show must be +// read back key for key, with no `#` in any value. + +const REPO_ROOT = join(import.meta.dir, "..", "..", ".."); +// `.agents/` and `.claude/` are git-ignored: the skill lives only in a +// maintainer's checkout, so CI checks gram-spec.ts alone and skips the rest. +const SKILL_FILES = [ + ".agents/skills/gram-authoring/SKILL.md", + ".claude/skills/gram-authoring/SKILL.md", +]; +const HAS_SKILL = SKILL_FILES.every((f) => existsSync(join(REPO_ROOT, f))); + +/** The body of every fence that opens at column 0 with ``` (any language). */ +function fences(text: string, lang?: string): string[] { + const bodies: string[] = []; + let buffer: string[] | null = null; + for (const line of text.split("\n")) { + if (buffer === null) { + if ( + line.startsWith("```") && + (lang === undefined || line === `\`\`\`${lang}`) + ) + buffer = []; + } else if (line === "```") { + bodies.push(`${buffer.join("\n")}\n`); + buffer = null; + } else { + buffer.push(line); + } + } + return bodies; +} + +/** The `key: value` lines between the two `---` of a snippet that opens with one. */ +function writtenFrontmatter(source: string): string[] | null { + const lines = source.split("\n"); + if (lines[0] !== "---") return null; + const end = lines.indexOf("---", 1); + return end === -1 ? null : lines.slice(1, end); +} + +function frontmatterExamples(): { origin: string; source: string }[] { + const examples = fences(GRAM_SPEC_PROMPT) + .filter((body) => writtenFrontmatter(body) !== null) + .map((source) => ({ origin: "gram-spec.ts", source })); + for (const file of HAS_SKILL ? SKILL_FILES : []) { + const text = readFileSync(join(REPO_ROOT, file), "utf8"); + for (const source of fences(text, "gram")) + if (writtenFrontmatter(source) !== null) + examples.push({ origin: file, source }); + } + return examples; +} + +describe("frontmatter examples of the AI specs", () => { + const examples = frontmatterExamples(); + + it("finds one in gram-spec.ts and one in each SKILL.md", () => { + expect(examples.map((e) => e.origin).sort()).toEqual( + ["gram-spec.ts", ...(HAS_SKILL ? SKILL_FILES : [])].sort(), + ); + }); + + it.skipIf(!HAS_SKILL)("keeps the two SKILL.md mirrors identical", () => { + const [agents, claude] = SKILL_FILES.map((f) => + readFileSync(join(REPO_ROOT, f), "utf8"), + ); + expect(claude).toBe(agents); + }); + + for (const { origin, source } of examples) { + it(`${origin}: every written key is read, with no comment in its value`, () => { + const lines = writtenFrontmatter(source) ?? []; + for (const line of lines) expect(line).toMatch(/^[A-Za-z]\w*: \S/); + const meta = getAST(source).meta ?? {}; + const keys = lines.map((line) => line.slice(0, line.indexOf(":"))); + expect(Object.keys(meta).sort()).toEqual([...keys].sort()); + const withComment = Object.entries(meta).filter(([, value]) => + JSON.stringify(value).includes("#"), + ); + expect(withComment).toEqual([]); + }); + } +}); -- 2.54.0