Skip to content

Commit 149a185

Browse files
fix(skills): recover colon-rich text frontmatter (#122884)
Restore shipped skill discovery compatibility for colon-rich read_when and summary text. Keep structured YAML strict and preserve the existing description recovery contract. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent a5e563e commit 149a185

2 files changed

Lines changed: 141 additions & 13 deletions

File tree

‎packages/markdown-core/src/frontmatter.test.ts‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,101 @@ description: Use anime style IMPORTANT: Must be kawaii
9494
expect(result.issues).toEqual([]);
9595
});
9696

97+
it("normalizes colon-rich free-form fields other than description", () => {
98+
const content = `---
99+
name: sample-skill
100+
read_when: signals: user announcing a conversion task
101+
---`;
102+
const result = parseFrontmatterBlockResult(content);
103+
104+
expect(result.frontmatter.read_when).toBe("signals: user announcing a conversion task");
105+
expect(result.issues).toEqual([]);
106+
});
107+
108+
it("normalizes multiple colon-rich pure-text fields in one block", () => {
109+
const content = `---
110+
name: sample-skill
111+
read_when: signals: user announcing a conversion task
112+
summary: handles formats: pdf and docx
113+
---`;
114+
const result = parseFrontmatterBlockResult(content);
115+
116+
expect(result.frontmatter.read_when).toBe("signals: user announcing a conversion task");
117+
expect(result.frontmatter.summary).toBe("handles formats: pdf and docx");
118+
expect(result.issues).toEqual([]);
119+
});
120+
121+
it("keeps structured fields strict when a pure-text field is normalized", () => {
122+
const result = parseFrontmatterBlockResult(`---
123+
name: sample-skill
124+
read_when: signals: user announcing a conversion task
125+
metadata: owner: value
126+
---`);
127+
128+
expect(result.issues[0]).toMatchObject({ code: "BLOCK_AS_IMPLICIT_KEY" });
129+
});
130+
131+
it("preserves valid structured siblings when pure-text fields are normalized", () => {
132+
const result = parseFrontmatterBlockResult(`---
133+
name: sample-skill
134+
read_when: signals: user announcing a conversion task
135+
metadata:
136+
openclaw:
137+
emoji: beaker
138+
---`);
139+
140+
expect(result.frontmatter.metadata).toBe('{"openclaw":{"emoji":"beaker"}}');
141+
expect(result.issues).toEqual([]);
142+
});
143+
144+
it("does not rewrite valid sibling values when recovering a malformed field", () => {
145+
const content = `---
146+
name: sample-skill
147+
description: text # note
148+
read_when: signals: user announcing a conversion task
149+
---`;
150+
const result = parseFrontmatterBlockResult(content);
151+
152+
expect(result.frontmatter.description).toBe("text");
153+
expect(result.frontmatter.read_when).toBe("signals: user announcing a conversion task");
154+
expect(result.issues).toEqual([]);
155+
});
156+
157+
it("does not rewrite valid quoted sibling scalars with escape sequences", () => {
158+
const content = `---
159+
name: sample-skill
160+
description: "line\\n: detail"
161+
read_when: signals: user announcing a conversion task
162+
---`;
163+
const result = parseFrontmatterBlockResult(content);
164+
165+
expect(result.frontmatter.description).toBe("line\n: detail");
166+
expect(result.frontmatter.read_when).toBe("signals: user announcing a conversion task");
167+
expect(result.issues).toEqual([]);
168+
});
169+
170+
it("recovers quoted freeform keys with colon-rich values", () => {
171+
const content = `---
172+
name: sample-skill
173+
"description": signals: user announcing a conversion task
174+
---`;
175+
const result = parseFrontmatterBlockResult(content);
176+
177+
expect(result.frontmatter.description).toBe("signals: user announcing a conversion task");
178+
expect(result.issues).toEqual([]);
179+
});
180+
181+
it("recovers single-quoted freeform keys with colon-rich values", () => {
182+
const content = `---
183+
name: sample-skill
184+
'read_when': signals: user announcing a conversion task
185+
---`;
186+
const result = parseFrontmatterBlockResult(content);
187+
188+
expect(result.frontmatter.read_when).toBe("signals: user announcing a conversion task");
189+
expect(result.issues).toEqual([]);
190+
});
191+
97192
it("leaves valid YAML description semantics untouched", () => {
98193
expect(
99194
parseFrontmatterBlock(`---

‎packages/markdown-core/src/frontmatter.ts‎

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Markdown Core module implements frontmatter behavior.
22
import { isRecord } from "@openclaw/normalization-core/record-coerce";
3-
import { isMap, isNode, isScalar, parseDocument } from "yaml";
3+
import { isAlias, isMap, isNode, isScalar, parseDocument } from "yaml";
44

55
type ParsedFrontmatter = Record<string, string>;
66

@@ -80,28 +80,48 @@ function parseLineFrontmatter(block: string): ParsedFrontmatter {
8080
return result;
8181
}
8282

83-
function normalizeFreeformDescription(block: string): string {
83+
const FREEFORM_TEXT_FIELDS = new Set(["description", "read_when", "summary"]);
84+
85+
function normalizeFreeformFieldAtError(block: string): string {
8486
const doc = parseDocument(block, { schema: "core", prettyErrors: false });
8587
if (!isMap(doc.contents)) {
8688
return block;
8789
}
88-
const descriptionPair = doc.contents.items.find(
89-
(pair) => isScalar(pair.key) && pair.key.value === "description",
90+
const error = doc.errors.find((candidate) => candidate.pos?.[0] !== undefined);
91+
// Aliases fail during toJS without adding a document error position.
92+
const descriptionAlias = doc.contents.items.find(
93+
(candidate) =>
94+
isScalar(candidate.key) && candidate.key.value === "description" && isAlias(candidate.value),
9095
);
91-
const keyStart = isNode(descriptionPair?.key) ? descriptionPair.key.range?.[0] : undefined;
92-
if (keyStart === undefined) {
96+
const pos =
97+
error?.pos?.[0] ??
98+
(isNode(descriptionAlias?.key) ? descriptionAlias.key.range?.[0] : undefined);
99+
if (pos === undefined) {
93100
return block;
94101
}
95-
const lineStart = block.lastIndexOf("\n", keyStart - 1) + 1;
96-
const lineEnd = block.indexOf("\n", keyStart);
102+
const lineStart = block.lastIndexOf("\n", pos) + 1;
103+
const lineEnd = block.indexOf("\n", pos);
97104
const end = lineEnd === -1 ? block.length : lineEnd;
98105
const line = block.slice(lineStart, end);
99-
const match = line.match(/^(?:description|"description"|'description'):\s*(.*)$/);
100-
const rawValue = match?.[1]?.trim();
101-
if (!rawValue || /^[|>](?:[1-9][+-]?|[+-][1-9]?)?$/.test(rawValue)) {
106+
const match = line.match(/^(?:([\w-]+)|"([\w-]+)"|'([\w-]+)'):\s*(.*)$/);
107+
const keyName = match?.[1] ?? match?.[2] ?? match?.[3];
108+
const rawValue = match?.[4]?.trim();
109+
const isTopLevelField = doc.contents.items.some(
110+
(pair) => isScalar(pair.key) && pair.key.value === keyName && pair.key.range?.[0] === lineStart,
111+
);
112+
// Keep shipped description recovery; other text fields only recover colon-rich parser errors.
113+
const recoverColonRichText = error?.code === "BLOCK_AS_IMPLICIT_KEY" && rawValue?.includes(": ");
114+
if (
115+
!keyName ||
116+
!rawValue ||
117+
!FREEFORM_TEXT_FIELDS.has(keyName) ||
118+
!isTopLevelField ||
119+
(keyName !== "description" && !recoverColonRichText) ||
120+
/^[|>](?:[1-9][+-]?|[+-][1-9]?)?$/.test(rawValue)
121+
) {
102122
return block;
103123
}
104-
const replacement = `description: ${JSON.stringify(stripQuotes(rawValue))}`;
124+
const replacement = `${keyName}: ${JSON.stringify(stripQuotes(rawValue))}`;
105125
return `${block.slice(0, lineStart)}${replacement}${block.slice(end)}`;
106126
}
107127

@@ -184,7 +204,20 @@ function parseYamlFrontmatter(block: string): ParsedFrontmatterBlockResult {
184204
if (parsed.issues.length === 0) {
185205
return parsed;
186206
}
187-
const recoveredBlock = normalizeFreeformDescription(block);
207+
// Recover one error-located field per iteration, retrying parse each time,
208+
// so multiple colon-rich fields are fixed without rewriting valid siblings.
209+
let recoveredBlock = block;
210+
for (let i = 0; i < FREEFORM_TEXT_FIELDS.size; i += 1) {
211+
const next = normalizeFreeformFieldAtError(recoveredBlock);
212+
if (next === recoveredBlock) {
213+
break;
214+
}
215+
recoveredBlock = next;
216+
const reparsed = parseYamlFrontmatterOnce(recoveredBlock, fallback);
217+
if (reparsed.issues.length === 0) {
218+
return reparsed;
219+
}
220+
}
188221
return recoveredBlock === block ? parsed : parseYamlFrontmatterOnce(recoveredBlock, fallback);
189222
}
190223

0 commit comments

Comments
 (0)