Skip to content

Commit 8fc57f0

Browse files
committed
fix(config): keep frontmatter values that start with an unquoted flow indicator
Review follow-up: - leave a valid flow collection alone (`tools: {write: false}` was being rewritten to a block scalar by the colon rule); - move the new cases into test/config/markdown.test.ts; - re-point the two cache-guard tests at an input that still cannot parse.
1 parent ef4c008 commit 8fc57f0

3 files changed

Lines changed: 52 additions & 36 deletions

File tree

‎packages/core/src/config/markdown.test.ts‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎packages/core/src/config/markdown.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ export function sanitize(content: string) {
3434
const value = entry[2].trim()
3535
if (value === "" || value === ">" || value === "|" || value.startsWith('"') || value.startsWith("'")) return [line]
3636
// A plain scalar beginning with a flow indicator ([ or {) is read as a
37-
// flow collection; when it is not valid flow syntax (e.g.
37+
// flow collection. When it is not valid flow syntax (e.g.
3838
// `description: [Team] Does a thing`) the whole frontmatter block fails to
39-
// parse and the document is dropped silently. Quote it so the value is
40-
// kept as written.
41-
if ((value.startsWith("[") || value.startsWith("{")) && !isParseableValue(value))
39+
// parse and the document is dropped silently; quote it so the value is
40+
// kept as written. When it *is* valid flow syntax, leave the line alone so
41+
// a real flow collection (e.g. `tools: {write: false}`) is not rewritten
42+
// into a block scalar by the colon rule below.
43+
if (value.startsWith("[") || value.startsWith("{")) {
44+
if (isParseableValue(value)) return [line]
4245
return [`${entry[1]}: ${JSON.stringify(value)}`]
46+
}
4347
if (!value.includes(":")) return [line]
4448
return [`${entry[1]}: |-`, ` ${value}`]
4549
})

‎packages/core/test/config/markdown.test.ts‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ description: Use when the user needs to crawl pages. Keywords: crawl, scrape
66
---
77
body`
88

9+
// YAML reads a leading `[`/`{` as a flow collection. An unquoted plain scalar
10+
// like `[Team] Does a thing` is not valid flow syntax, so the whole frontmatter
11+
// block used to throw and the document (e.g. an agent) was dropped silently.
12+
const flowScalar = `---
13+
description: [Team] Does a thing
14+
mode: subagent
15+
---
16+
body`
17+
18+
// A value that still cannot be parsed after the fallback (`*ref` is a YAML
19+
// alias that is never defined here), used to keep the throw-on-every-call
20+
// guard meaningful.
21+
const unparseable = `---
22+
description: *ref thing
23+
---
24+
body`
25+
926
describe("ConfigMarkdown.parse", () => {
1027
test("recovers unquoted-colon frontmatter via the sanitize fallback", () => {
1128
const parsed = ConfigMarkdown.parse(invalidYaml)
@@ -16,20 +33,42 @@ describe("ConfigMarkdown.parse", () => {
1633
test("recovers the same content again after a previous failed parse", () => {
1734
// gray-matter caches by content before parsing; a poisoned entry used to
1835
// make every later parse of the same text return silently without data.
19-
expect(() => ConfigMarkdown.parse("---\ndescription: [unclosed\n---\nbody")).toThrow()
36+
expect(() => ConfigMarkdown.parse(unparseable)).toThrow()
2037
const parsed = ConfigMarkdown.parse(invalidYaml)
2138
expect(parsed.data.description).toBe("Use when the user needs to crawl pages. Keywords: crawl, scrape")
2239
})
2340

2441
test("keeps throwing for the same unparseable content on every call", () => {
25-
const input = "---\ndescription: [unclosed\n---\nbody"
26-
expect(() => ConfigMarkdown.parse(input)).toThrow()
27-
expect(() => ConfigMarkdown.parse(input)).toThrow()
42+
expect(() => ConfigMarkdown.parse(unparseable)).toThrow()
43+
expect(() => ConfigMarkdown.parse(unparseable)).toThrow()
2844
})
2945

3046
test("parses plain content without frontmatter", () => {
3147
const parsed = ConfigMarkdown.parse("just body")
3248
expect(parsed.content).toBe("just body")
3349
expect(parsed.data).toEqual({})
3450
})
35-
})
51+
52+
test("keeps a frontmatter value that starts with an unquoted [", () => {
53+
const parsed = ConfigMarkdown.parse(flowScalar)
54+
expect(parsed.data.description).toBe("[Team] Does a thing")
55+
expect(parsed.data.mode).toBe("subagent")
56+
expect(parsed.content.trim()).toBe("body")
57+
})
58+
59+
test("still parses a real flow sequence", () => {
60+
const parsed = ConfigMarkdown.parse(`---\ndescription: ['a', 'b']\n---\nbody`)
61+
expect(parsed.data.description).toEqual(["a", "b"])
62+
})
63+
64+
test("still parses a real flow mapping", () => {
65+
const parsed = ConfigMarkdown.parse(`---\ndescription: [Team] x\ntools: {write: false}\n---\nbody`)
66+
expect(parsed.data.description).toBe("[Team] x")
67+
expect(parsed.data.tools).toEqual({ write: false })
68+
})
69+
70+
test("still keeps unquoted colons working", () => {
71+
const parsed = ConfigMarkdown.parse(invalidYaml)
72+
expect(parsed.data.description).toBe("Use when the user needs to crawl pages. Keywords: crawl, scrape")
73+
})
74+
})

0 commit comments

Comments
 (0)