Merge fix/cli-frontmatter-yaml: SKILL.md frontmatter YAML 引号加固
Some checks failed
ci / build-test-parity (push) Has been cancelled

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-23 09:57:05 +08:00
2 changed files with 36 additions and 3 deletions

View File

@@ -722,7 +722,8 @@ public final class Eip4Cli {
String rendered = body.replace("{{EIP4_BASE_URL}}", baseUrl); String rendered = body.replace("{{EIP4_BASE_URL}}", baseUrl);
Path skillDir = claudeSkills.resolve(name); Path skillDir = claudeSkills.resolve(name);
Files.createDirectories(skillDir); 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); Files.writeString(skillDir.resolve("SKILL.md"), doc, StandardCharsets.UTF_8);
catalog.append("- **").append(name).append("** — ").append(description).append('\n'); catalog.append("- **").append(name).append("** — ").append(description).append('\n');
bodies.append("\n---\n\n### Skill: ").append(name).append("\n\n").append(rendered).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_BEGIN = "<!-- EIP4-SKILLS:BEGIN (managed by `eip4 init`) -->";
private static final String SKILLS_END = "<!-- EIP4-SKILLS:END -->"; 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 { private Path writeAgentsMd(Path targetDir, String baseUrl, String catalog, String bodies) throws IOException {
String block = SKILLS_BEGIN + "\n" String block = SKILLS_BEGIN + "\n"
+ "## EIP4 平台接入(AI Agent)\n\n" + "## EIP4 平台接入(AI Agent)\n\n"

View File

@@ -397,12 +397,39 @@ class Eip4CliTest {
assertTrue(Files.exists(skill)); assertTrue(Files.exists(skill));
String content = Files.readString(skill, StandardCharsets.UTF_8); String content = Files.readString(skill, StandardCharsets.UTF_8);
assertTrue(content.startsWith("---"), "should carry YAML frontmatter"); assertTrue(content.startsWith("---"), "should carry YAML frontmatter");
assertTrue(content.contains("name: eip4-demo")); assertTrue(content.contains("name: \"eip4-demo\""));
assertTrue(content.contains("description: 演示用 skill")); assertTrue(content.contains("description: \"演示用 skill\""));
assertTrue(content.contains("http://localhost:8093/api/ai/features/auto-develop")); assertTrue(content.contains("http://localhost:8093/api/ai/features/auto-develop"));
assertFalse(content.contains("{{EIP4_BASE_URL}}"), "placeholder must be rendered"); 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 @Test
void initGeneratesAgentsMdWithSkillCatalog() throws Exception { void initGeneratesAgentsMdWithSkillCatalog() throws Exception {
Path skillsDir = writeSkillsFixture(); Path skillsDir = writeSkillsFixture();