fix(agents): parse frontmatter on the --- delimiter line, not any --- substring (#3590)

CommandRegistrar.parse_frontmatter located the closing delimiter with
content.find("---", 3), a raw substring search. It stopped at the first
"---" anywhere after the opening — including one embedded in a
frontmatter value (e.g. a description "Separate sections with ---
markers") or inside an indented literal block — which truncated the
frontmatter and spilled the remainder into the body, silently corrupting
both the parsed metadata and the rendered command body.

Match the closing "---" on line boundaries, mirroring the line-anchored
scan already used by VibeIntegration._inject_frontmatter_flag.
This commit is contained in:
Andrew Chen
2026-07-21 23:22:52 +08:00
committed by GitHub
parent eabfabb490
commit 7873c447bd
2 changed files with 31 additions and 5 deletions

View File

@@ -114,13 +114,24 @@ class CommandRegistrar:
if not content.startswith("---"):
return {}, content
# Find second ---
end_marker = content.find("---", 3)
if end_marker == -1:
# The closing delimiter is a line that is exactly ``---`` (a YAML
# document separator), not any ``---`` substring. Scanning with
# ``content.find("---", 3)`` stops at the first ``---`` *anywhere* —
# including one embedded in a frontmatter value (e.g. a description like
# "Separate sections with ---") or inside an indented literal block —
# which truncates the frontmatter and spills the remainder into the
# body. Match on line boundaries instead, mirroring the line-anchored
# scan in ``VibeIntegration._inject_frontmatter_flag``.
lines = content.splitlines(keepends=True)
end_line = next(
(i for i in range(1, len(lines)) if lines[i].rstrip() == "---"),
None,
)
if end_line is None:
return {}, content
frontmatter_str = content[3:end_marker].strip()
body = content[end_marker + 3 :].strip()
frontmatter_str = "".join(lines[1:end_line]).strip()
body = "".join(lines[end_line + 1 :]).strip()
try:
frontmatter = yaml.safe_load(frontmatter_str) or {}

View File

@@ -2418,6 +2418,21 @@ $ARGUMENTS
assert frontmatter == {}
assert "Command body" in body
def test_parse_frontmatter_dash_in_value(self):
"""A ``---`` inside a frontmatter value must not close the block early."""
content = """---
description: Separate sections with --- markers
argument-hint: "[name]"
---
Real body starts here.
"""
registrar = CommandRegistrar()
frontmatter, body = registrar.parse_frontmatter(content)
assert frontmatter["description"] == "Separate sections with --- markers"
assert frontmatter["argument-hint"] == "[name]"
assert body == "Real body starts here."
def test_render_frontmatter(self):
"""Test rendering frontmatter to YAML."""
frontmatter = {