The editor's strip regex anchored on \\A — a Python-ism JS treats as a literal — so it never matched: the editor showed the raw frontmatter and every save re-wrapped the file in one more frontmatter layer. Extract the strip into lib/skillFrontmatter.ts with a ^ anchor, import it from the page, and guard it with scripts/check-skill-frontmatter.mjs (wired into npm test): strips the render_skill_md block shape, exact split boundary, idempotent, save-reload round-trip adds no layer, and the inline \\A pattern is banned statically. Also per review notes: skill toggles are now the spec-v2 §5 34x19 capsule (amber track + dark knob when on), and version-history timestamps render localized instead of the raw SQLite UTC string. Co-authored-by: multica-agent <github@multica.ai>
122 lines
3.8 KiB
JavaScript
122 lines
3.8 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Regression guard for the HEL-613 rework: the skill editor must load the
|
|
* SKILL.md BODY only — never the YAML frontmatter — so saving cannot wrap
|
|
* the file in a second frontmatter layer (each edit used to nest one more
|
|
* `---\nname: …` block into the content: the old strip regex anchored on
|
|
* `\A`, a Python-ism JS treats as a literal, so it never matched).
|
|
*
|
|
* Layers:
|
|
* 1. behavioural — stripSkillFrontmatter() must strip the exact block
|
|
* shape the backend's render_skill_md writes, split at the right
|
|
* boundary, be idempotent (the no-doubling invariant), and pass
|
|
* plain bodies through unchanged.
|
|
* 2. round-trip — strip → re-render (frontmatter + body) → strip again
|
|
* yields the identical body: editing twice adds zero layers.
|
|
* 3. static — the editor page must use the shared helper; the inline
|
|
* `\A`-anchored regex cannot creep back in.
|
|
*
|
|
* Run with `npm test` (or `node scripts/check-skill-frontmatter.mjs`).
|
|
*/
|
|
import { readFileSync } from "node:fs";
|
|
import { dirname, join } from "node:path";
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
|
|
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
const ROOT = join(HERE, "..");
|
|
|
|
let failed = false;
|
|
function check(name, cond, detail = "") {
|
|
console.log(
|
|
`${cond ? "ok " : "FAIL "} ${name}${cond ? "" : ` :: ${detail}`}`,
|
|
);
|
|
if (!cond) failed = true;
|
|
}
|
|
|
|
// Mirror of the backend's render_skill_md output shape
|
|
// (calliope-backend/src/calliope/agent/skills_store.py).
|
|
const rendered = [
|
|
"---",
|
|
"name: cyberpunk-style-kit",
|
|
'description: "赛博朋克风格词库"',
|
|
"version: 1.0.1",
|
|
"metadata:",
|
|
" author: user",
|
|
" tags: [neon, rain]",
|
|
" type: template",
|
|
"---",
|
|
"",
|
|
"# 赛博朋克风格词库",
|
|
"neon, rain, hologram",
|
|
].join("\n");
|
|
|
|
const { stripSkillFrontmatter } = await import(
|
|
pathToFileURL(join(ROOT, "src/lib/skillFrontmatter.ts"))
|
|
);
|
|
|
|
// 1. Editor load: body only, no frontmatter.
|
|
const body = stripSkillFrontmatter(rendered);
|
|
check(
|
|
"edit load strips the frontmatter block",
|
|
!body.includes("name: cyberpunk-style-kit") &&
|
|
!body.trimStart().startsWith("---"),
|
|
JSON.stringify(body.slice(0, 40)),
|
|
);
|
|
check(
|
|
"edit load keeps the body",
|
|
body.includes("# 赛博朋克风格词库") && body.includes("neon, rain, hologram"),
|
|
JSON.stringify(body),
|
|
);
|
|
|
|
// 2. Strip splits at the exact boundary: frontmatter + body reassembles
|
|
// the file byte-identically (proves no body text was eaten).
|
|
const fm = rendered.slice(0, rendered.length - body.length);
|
|
check(
|
|
"strip boundary is exact",
|
|
fm + body === rendered && fm.trimEnd().endsWith("---"),
|
|
"frontmatter/body split drifted",
|
|
);
|
|
|
|
// 3. No-doubling invariant: idempotent strip + round-trip stability.
|
|
check(
|
|
"strip is idempotent",
|
|
stripSkillFrontmatter(body) === body,
|
|
"second strip changed the body",
|
|
);
|
|
const savedAgain = fm + body; // render_skill_md re-wraps the same frontmatter
|
|
check(
|
|
"second edit-save-reload adds no layer",
|
|
stripSkillFrontmatter(savedAgain) === body,
|
|
"round-trip grew the content",
|
|
);
|
|
|
|
// 4. Body without frontmatter passes through unchanged.
|
|
const plain = "# Just a body\nwith text\n";
|
|
check(
|
|
"plain body unchanged",
|
|
stripSkillFrontmatter(plain) === plain,
|
|
JSON.stringify(stripSkillFrontmatter(plain)),
|
|
);
|
|
|
|
// 5. Static: the page uses the shared helper; no `\A` regex remains.
|
|
const page = readFileSync(
|
|
join(ROOT, "src/routes/agents/+page.svelte"),
|
|
"utf-8",
|
|
);
|
|
check(
|
|
"page imports the shared strip helper",
|
|
page.includes("stripSkillFrontmatter"),
|
|
"helper import missing",
|
|
);
|
|
check(
|
|
"no \\A-anchored strip regex left behind",
|
|
!/\\A---/.test(page),
|
|
"inline \\A regex present",
|
|
);
|
|
|
|
if (failed) {
|
|
console.error("skill frontmatter checks FAILED");
|
|
process.exit(1);
|
|
}
|
|
console.log("skill frontmatter checks passed.");
|