Skip to content

fix(skills): parse SKILL.md frontmatter when --- is not the first byte - #147

Merged
eldonm merged 3 commits into
mainfrom
fix/skill-frontmatter-leading-whitespace
Aug 8, 2026
Merged

fix(skills): parse SKILL.md frontmatter when --- is not the first byte#147
eldonm merged 3 commits into
mainfrom
fix/skill-frontmatter-leading-whitespace

Conversation

@eldonm

@eldonm eldonm commented Aug 8, 2026

Copy link
Copy Markdown
Member

Summary

A SKILL.md whose --- is not the very first byte silently loses its entire frontmatter. _parse_frontmatter tested the delimiter on the unstripped text while using the stripped text for everything else:

content = raw.strip()
if not raw.startswith("---"):      # raw, not content
    return {}, content

A leading blank line, an indent, or an editor-inserted UTF-8 BOM therefore reads as "no frontmatter", and the whole block is returned as body.

Related issues

Diagnosed from a field report that skill_only_tools and channel_overrides "were not working", with allowed-tools appearing in the prompt. Neither feature was at fault — see below.

Type of change

  • Bug fix (non-breaking)
  • New feature (non-breaking)
  • Breaking change
  • Documentation
  • Chore / tooling / CI

Description

The bug. Two consequences, both silent, and they compound into one confusing symptom:

  1. allowed-tools is never read, so SkillDoc.requires_tools is empty and the skill owns nothing. Any tool named in skill_only_tools then has no owning skill and fails closed — the gate looks broken when the declaration simply never loaded.
  2. The frontmatter text lands in SkillDoc.body, which is rendered into the prompt's skills section, so allowed-tools: appears verbatim to the model.

The chain is _parse_frontmatterallowed_toolsSkillDoc.requires_toolsbuild_skill_gate ownership, and separately contentSkillDoc.body → skills section. One broken parse, both symptoms.

Reproduced with a real four-tool declaration:

Input tools parsed frontmatter leaked into body
clean 4 no
leading newline 0 yes
leading spaces 0 yes
UTF-8 BOM 0 yes
CRLF 4 no

The fix. Strip the BOM, then test and split the stripped text. sop_extend.py:138 in this same codebase already does raw.strip().startswith("---")skill_resolve was the outlier, which is what makes this look like an oversight rather than intent.

Checklist

  • I read CONTRIBUTING.md and the local CLAUDE.md.
  • pre-commit run --all-files passes.
  • pytest tests/ passes — 0 failures.
  • Bug fixes cite file:line in the description.
  • CHANGELOG.md — happy to add an entry; held off since this restores intended behaviour rather than changing it. Say the word.
  • No ADR was modified in place.

Notes for reviewers

Written test-first. Five parametrised leading-character cases (newline, blank lines, spaces, BOM, BOM+newline) confirmed red before the fix, plus three controls so the fix cannot over-reach:

  • a clean file still parses
  • a --- horizontal rule inside the body is not treated as a delimiter
  • a file with no frontmatter is untouched

That middle control matters: split("---", 2) with maxsplit=2 is what keeps a markdown rule in the procedure from being eaten, and it would be easy to break while "fixing" the delimiter check.

Diagnostic tip for anyone hitting this before the fix ships:

head -c 16 path/to/SKILL.md | xxd | head -2

efbbbf at the start is a BOM; 0a is a leading newline. Removing either makes the skill work immediately, which is also the fastest way to confirm this is the cause.

Steps to test

pytest tests/scaffold/test_skill_resolve.py -q

Eldon Marks and others added 3 commits August 8, 2026 09:28
_parse_frontmatter tested the delimiter on the unstripped text while using
the stripped text for everything else:

    content = raw.strip()
    if not raw.startswith("---"):
        return {}, content

So a SKILL.md beginning with a blank line, an indent, or an editor-inserted
UTF-8 BOM parsed as having no frontmatter at all, and the whole block was
returned as body. Both consequences are silent, and they compound:

- allowed-tools is never read, so SkillDoc.requires_tools is empty and the
  skill owns nothing. Any tool named in skill_only_tools then has no owning
  skill and fails closed -- the gate looks broken when the declaration
  simply never loaded.
- the frontmatter text lands in SkillDoc.body, which is rendered into the
  prompt's skills section, so "allowed-tools:" appears verbatim to the
  model.

That is the reported symptom exactly: allowed-tools showing up in the
prompt while skill gating misbehaves. Reproduced with a real four-tool
declaration -- leading newline, leading spaces and BOM each yield zero
parsed tools and a leaked block; clean and CRLF files are unaffected.

Strip the BOM, then test and split the stripped text. sop_extend.py
already strips before its check; this was the outlier.

Tests written first: five parametrised leading-character cases, red before
the fix, plus controls for a clean file, a --- horizontal rule inside the
body, and a file with no frontmatter, so the fix cannot over-reach.
The BOM/leading-whitespace fix in 7ea7edd closed one way for a SKILL.md
frontmatter block to be dropped without a word. This closes the other.

Six keys had a hand-written underscore fallback at their read site
(`task_lock`, `lock_companions`, `requires_tasks`, `allowed_channels`,
`denied_channels`, `deny_access_directive` — skill_resolve.py:311-372) and
the remaining twenty did not. `allowed_tools:` therefore parsed to nothing:
the skill loaded, exposed no tools, and owned nothing `skill_only_tools`
gates — indistinguishable from a skill that declares none, with no warning
anywhere. Same end state as the BOM bug, reached a different way.

`_normalize_frontmatter_keys()` now runs on the parsed mapping before
`_parse_frontmatter` returns:

- underscores accepted for every key in `_KNOWN_FRONTMATTER_KEYS` (26 keys,
  enumerated from the `frontmatter.get(...)` read sites), logged at INFO so
  the assumption is visible and the file can be corrected
- hyphenated spelling wins when a file carries both, independent of YAML key
  order
- an unknown key within difflib ratio 0.8 of a known one (`allowed-tool`) is
  still ignored, but logs a WARNING naming the key it was probably meant to
  be. Unrecognized keys resembling nothing are passed through untouched, so
  custom frontmatter stays legal and silent.

Tests mutation-checked three ways: unwiring the normalizer, replacing
`setdefault` with plain assignment, and disabling the near-miss lookup each
turn the relevant cases red while the controls stay green.

docs/scaffolding.md documents both tolerances under skill discovery.
@eldonm
eldonm merged commit 3c51340 into main Aug 8, 2026
3 checks passed
@eldonm
eldonm deleted the fix/skill-frontmatter-leading-whitespace branch August 8, 2026 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant