fix(core): preserve yaml flow collections during frontmatter repair (#65119)

This commit is contained in:
Filip Hejmowski
2026-11-20 10:38:50 +01:00
committed by GitHub
Unverified
parent f8738a13bb
commit 7aed488e41
3 changed files with 180 additions and 4 deletions
+77 -3
View File
@@ -23,19 +23,93 @@ export function parseOption(content: string) {
// Other coding agents accept unquoted colons and leading tags like `[Tag] text`
// in frontmatter values. Retry those values as YAML block scalars so existing
// config files keep working.
// config files keep working without rewriting valid YAML flow collections.
export function sanitize(content: string) {
const match = content.match(/^---\r?\n([\s\S]*?)\r?\n---/)
if (!match) return content
const frontmatter = match[1]
let flow: FlowState | undefined
const result = frontmatter.split(/\r?\n/).flatMap((line) => {
if (flow) {
flow = scanFlow(line, flow).state
return [line]
}
if (line.trim().startsWith("#") || line.trim() === "" || /^\s+/.test(line)) return [line]
const entry = line.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*(.*)$/)
if (!entry) return [line]
const value = entry[2].trim()
if (value === "" || value === ">" || value === "|" || value.startsWith('"') || value.startsWith("'")) return [line]
if (!value.includes(":") && !/^[[{].*[\]}]\s*\S/.test(value)) return [line]
if (value === "" || value.startsWith('"') || value.startsWith("'")) return [line]
if (value.startsWith("[") || value.startsWith("{")) {
const scan = scanFlow(value, { stack: [], quote: undefined })
if (scan.status === "open") flow = scan.state
if (scan.status !== "trailing") return [line]
return [`${entry[1]}: |-`, ` ${value}`]
}
if (!value.includes(":") || isValidYaml(line)) return [line]
return [`${entry[1]}: |-`, ` ${value}`]
})
return content.replace(frontmatter, () => result.join("\n"))
}
function isValidYaml(line: string) {
try {
matter(`---\n${line}\n---`, {})
return true
} catch {
return false
}
}
type FlowState = {
stack: Array<"]" | "}">
quote: '"' | "'" | undefined
}
function scanFlow(text: string, state: FlowState) {
const stack = [...state.stack]
let quote = state.quote
for (let i = 0; i < text.length; i++) {
const ch = text[i]
if (quote === '"') {
if (ch === "\\") {
i++
continue
}
if (ch === '"') quote = undefined
continue
}
if (quote === "'") {
if (ch === "'") {
if (text[i + 1] === "'") {
i++
continue
}
quote = undefined
}
continue
}
if ((ch === '"' || ch === "'") && (i === 0 || /[\s[{,:]/.test(text[i - 1]))) {
quote = ch
continue
}
if (ch === "#" && (i === 0 || /\s/.test(text[i - 1]))) break
if (ch === "[") {
stack.push("]")
continue
}
if (ch === "{") {
stack.push("}")
continue
}
if (ch === "]" || ch === "}") {
if (stack.at(-1) !== ch) return { status: "invalid" as const, state: undefined }
stack.pop()
if (stack.length === 0) {
const rest = text.slice(i + 1).trim()
if (rest === "" || rest.startsWith("#")) return { status: "closed" as const, state: undefined }
return { status: "trailing" as const, state: undefined }
}
}
}
return { status: "open" as const, state: { stack, quote } }
}
+14
View File
@@ -130,6 +130,20 @@ permissions:
}),
)
it.live("loads Markdown agents with flow collections alongside unquoted-colon descriptions", () =>
Effect.gen(function* () {
const agent = yield* loadMarkdownAgent(
`description: Review changes. Keywords: review, audit
model: { providerID: example, model: chat, variant: high }
permissions: [{ action: edit, resource: "*", effect: deny }]`,
)
expect(agent.description).toBe("Review changes. Keywords: review, audit")
expect(agent.model).toEqual(Model.Ref.parse("example/chat#high"))
expect(agent.permissions).toContainEqual({ action: "edit", resource: "*", effect: "deny" })
expect(Permission.evaluate("edit", "example.txt", agent.permissions).effect).toBe("deny")
}),
)
for (const variant of [undefined, "high"]) {
it.live(`loads Markdown legacy temperature ${variant ? "with" : "without"} a separate variant`, () =>
Effect.gen(function* () {
+89 -1
View File
@@ -19,6 +19,94 @@ describe("ConfigMarkdown.parse", () => {
expect(parsed.data.tools).toEqual(["read", "edit"])
})
test("preserves flow mappings and flow sequences when repairing another field", () => {
const parsed = ConfigMarkdown.parse(`---
description: Review changes. Keywords: review, audit
model: { providerID: example, model: chat, variant: high }
tools: { edit: false }
metadata: { "opencode/autoinvoke": false }
permissions: [{ action: edit, resource: "*", effect: deny }]
---
body`)
expect(parsed.data.description).toBe("Review changes. Keywords: review, audit")
expect(parsed.data.model).toEqual({ providerID: "example", model: "chat", variant: "high" })
expect(parsed.data.tools).toEqual({ edit: false })
expect(parsed.data.metadata).toEqual({ "opencode/autoinvoke": false })
expect(parsed.data.permissions).toEqual([{ action: "edit", resource: "*", effect: "deny" }])
expect(parsed.content.trim()).toBe("body")
})
test("preserves nested flow collections, quoted delimiters, and inline comments during repair", () => {
const parsed = ConfigMarkdown.parse(`---
description: Review changes. Keywords: review, audit
nested: [[read], [edit]]
tools: [read, edit] # comment: with colon
quoted: ["a]b", 'c''}d', don't]
---
body`)
expect(parsed.data.description).toBe("Review changes. Keywords: review, audit")
expect(parsed.data.nested).toEqual([["read"], ["edit"]])
expect(parsed.data.tools).toEqual(["read", "edit"])
expect(parsed.data.quoted).toEqual(["a]b", "c'}d", "don't"])
})
test("preserves multi-line flow collections while repairing fields before and after", () => {
const parsed = ConfigMarkdown.parse(`---
description: Review changes. Keywords: review, audit
model: { providerID: example, # note: don't close } here
model: chat,
variant: high
} # trailing: comment
permissions: [{ action: edit,
resource: "*",
effect: deny }]
note: Remember: check tests
---
body`)
expect(parsed.data.description).toBe("Review changes. Keywords: review, audit")
expect(parsed.data.model).toEqual({ providerID: "example", model: "chat", variant: "high" })
expect(parsed.data.permissions).toEqual([{ action: "edit", resource: "*", effect: "deny" }])
expect(parsed.data.note).toBe("Remember: check tests")
})
test("recovers leading bracketed and braced tags with colons or apostrophes", () => {
const parsed = ConfigMarkdown.parse(`---
description: [Team: Core] Does a thing
summary: {Team} Review changes
owner: [User's] Follow up
model: { providerID: example, model: chat }
---
body`)
expect(parsed.data.description).toBe("[Team: Core] Does a thing")
expect(parsed.data.summary).toBe("{Team} Review changes")
expect(parsed.data.owner).toBe("[User's] Follow up")
expect(parsed.data.model).toEqual({ providerID: "example", model: "chat" })
})
test("preserves flow collections with CRLF line endings during repair", () => {
const parsed = ConfigMarkdown.parse(
"---\r\ndescription: Review changes. Keywords: review, audit\r\nmodel: { providerID: example, model: chat }\r\n---\r\nbody",
)
expect(parsed.data.description).toBe("Review changes. Keywords: review, audit")
expect(parsed.data.model).toEqual({ providerID: "example", model: "chat" })
})
test("keeps block scalar headers with commented colons during repair", () => {
const parsed = ConfigMarkdown.parse(`---
description: Review changes. Keywords: review, audit
prompt: |- # see: docs
Review carefully.
---
body`)
expect(parsed.data.description).toBe("Review changes. Keywords: review, audit")
expect(parsed.data.prompt).toBe("Review carefully.")
})
test("still throws on unclosed or mismatched flow collections", () => {
expect(() => ConfigMarkdown.parse("---\nmodel: { providerID: example\n---\nbody")).toThrow()
expect(() => ConfigMarkdown.parse("---\ndescription: [Tag} text\n---\nbody")).toThrow()
})
test("recovers the same content again after a previous failed parse", () => {
// gray-matter caches by content before parsing; a poisoned entry used to
// make every later parse of the same text return silently without data.
@@ -38,4 +126,4 @@ describe("ConfigMarkdown.parse", () => {
expect(parsed.content).toBe("just body")
expect(parsed.data).toEqual({})
})
})
})