fix(skill_engine): re-emit non-string frontmatter via first dump line

Avoid PyYAML document-end markers leaking into SKILL.md frontmatter
when normalizing dates and other non-string scalars.
This commit is contained in:
santhreal 2026-07-18 17:43:06 -07:00
parent 965f795757
commit ead81ed881
2 changed files with 17 additions and 17 deletions

View file

@ -66,23 +66,14 @@ def _yaml_quote(value: str) -> str:
def _format_frontmatter_value(value: Any) -> str:
"""Serialize a frontmatter value for re-emit after PyYAML parse."""
if isinstance(value, bool):
return "true" if value else "false"
if value is None:
return "null"
if isinstance(value, (int, float)):
return str(value)
if isinstance(value, str):
return _yaml_quote(value)
try:
import yaml
import yaml
dumped = yaml.safe_dump(
value, default_flow_style=True, allow_unicode=True
).strip()
return dumped
except Exception:
return _yaml_quote(str(value))
# First line only: scalar dumps include a trailing '...' document end.
return yaml.safe_dump(
value, default_flow_style=True, allow_unicode=True
).splitlines()[0]
def _yaml_unquote(value: str) -> str:
@ -185,9 +176,9 @@ def normalize_frontmatter(content: str) -> str:
"""Re-serialize frontmatter with proper YAML quoting.
Parses the existing frontmatter, then re-writes each value through
:func:`_yaml_quote` so that colons, hashes, and other special
characters are safely double-quoted. The body after ``---`` is
preserved verbatim.
:func:`_format_frontmatter_value` so scalars (bool/int/date) and
strings with special characters re-emit safely. The body after
``---`` is preserved verbatim.
Returns *content* unchanged if no frontmatter is found.
"""

View file

@ -11,6 +11,15 @@ def test_normalize_frontmatter_bool_and_int_scalars() -> None:
assert "enabled: true" in out
assert "version: 2" in out
assert "# body" in out
assert "..." not in out
def test_normalize_frontmatter_date_scalar_no_document_end() -> None:
raw = "---\nname: demo\ncreated: 2024-01-01\nenabled: true\n---\n"
out = normalize_frontmatter(raw)
assert "created: 2024-01-01" in out
assert "..." not in out
assert "enabled: true" in out
def test_normalize_frontmatter_string_still_quoted_when_needed() -> None: