fix(cli): SKILL.md frontmatter 用 YAML 引号标量,防 description 破坏解析
runInit 此前裸拼 `description: <raw>`,含 ASCII 冒号或前导 */#/& 等的 description 会破坏 Claude 读取 SKILL.md frontmatter。改为 yamlScalar 双引号 + 转义 \ 与 "(name 同处理)。当前 7 个 description 用全角冒号本无碍,此为加固。 测试 +1(tricky description 含冒号/星号仍输出合法引号标量)。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -722,7 +722,8 @@ public final class Eip4Cli {
|
||||
String rendered = body.replace("{{EIP4_BASE_URL}}", baseUrl);
|
||||
Path skillDir = claudeSkills.resolve(name);
|
||||
Files.createDirectories(skillDir);
|
||||
String doc = "---\nname: " + name + "\ndescription: " + description + "\n---\n\n" + rendered;
|
||||
String doc = "---\nname: " + yamlScalar(name)
|
||||
+ "\ndescription: " + yamlScalar(description) + "\n---\n\n" + rendered;
|
||||
Files.writeString(skillDir.resolve("SKILL.md"), doc, StandardCharsets.UTF_8);
|
||||
catalog.append("- **").append(name).append("** — ").append(description).append('\n');
|
||||
bodies.append("\n---\n\n### Skill: ").append(name).append("\n\n").append(rendered).append('\n');
|
||||
@@ -739,6 +740,11 @@ public final class Eip4Cli {
|
||||
private static final String SKILLS_BEGIN = "<!-- EIP4-SKILLS:BEGIN (managed by `eip4 init`) -->";
|
||||
private static final String SKILLS_END = "<!-- EIP4-SKILLS:END -->";
|
||||
|
||||
/** YAML 双引号标量:转义 \ 与 ",使含 `:`/前导特殊字符的 description 不破坏 SKILL.md frontmatter。 */
|
||||
private static String yamlScalar(String value) {
|
||||
return "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
|
||||
}
|
||||
|
||||
private Path writeAgentsMd(Path targetDir, String baseUrl, String catalog, String bodies) throws IOException {
|
||||
String block = SKILLS_BEGIN + "\n"
|
||||
+ "## EIP4 平台接入(AI Agent)\n\n"
|
||||
|
||||
@@ -397,12 +397,39 @@ class Eip4CliTest {
|
||||
assertTrue(Files.exists(skill));
|
||||
String content = Files.readString(skill, StandardCharsets.UTF_8);
|
||||
assertTrue(content.startsWith("---"), "should carry YAML frontmatter");
|
||||
assertTrue(content.contains("name: eip4-demo"));
|
||||
assertTrue(content.contains("description: 演示用 skill"));
|
||||
assertTrue(content.contains("name: \"eip4-demo\""));
|
||||
assertTrue(content.contains("description: \"演示用 skill\""));
|
||||
assertTrue(content.contains("http://localhost:8093/api/ai/features/auto-develop"));
|
||||
assertFalse(content.contains("{{EIP4_BASE_URL}}"), "placeholder must be rendered");
|
||||
}
|
||||
|
||||
@Test
|
||||
void initQuotesFrontmatterSoTrickyDescriptionStaysValidYaml() throws Exception {
|
||||
Path dir = tempDir.resolve("skills-tricky");
|
||||
Files.createDirectories(dir);
|
||||
Files.writeString(dir.resolve("manifest.json"), """
|
||||
{
|
||||
"skills": [
|
||||
{"file": "x.md", "name": "eip4-x", "description": "建页: 维护/树 *含* 引用列"}
|
||||
]
|
||||
}
|
||||
""", StandardCharsets.UTF_8);
|
||||
Files.writeString(dir.resolve("x.md"), "# Skill: x\n", StandardCharsets.UTF_8);
|
||||
Path target = tempDir.resolve("proj");
|
||||
|
||||
CliResult result = run(new CapturingClient(new Eip4HttpResponse(200, "{}")),
|
||||
"init", "--dir", target.toString(), "--skills-dir", dir.toString(),
|
||||
"--base-url", "http://localhost:8084");
|
||||
assertEquals(0, result.code());
|
||||
|
||||
// 含 ASCII 冒号 / 前导 * 的 description 必须以双引号标量输出,否则破坏 Claude 的 frontmatter 解析。
|
||||
String fm = Files.readString(target.resolve(".claude/skills/eip4-x/SKILL.md"),
|
||||
StandardCharsets.UTF_8);
|
||||
assertTrue(fm.contains("description: \"建页: 维护/树 *含* 引用列\""),
|
||||
"tricky description must be double-quoted:\n" + fm);
|
||||
assertFalse(fm.contains("description: 建页:"), "must not emit an unquoted colon-bearing scalar");
|
||||
}
|
||||
|
||||
@Test
|
||||
void initGeneratesAgentsMdWithSkillCatalog() throws Exception {
|
||||
Path skillsDir = writeSkillsFixture();
|
||||
|
||||
Reference in New Issue
Block a user