Skip to content

fix(presets): tolerate trailing whitespace on command frontmatter fences - #5660

Merged
mnriem merged 1 commit into
github:mainfrom
pengzh1:fix/presets-command-frontmatter-fence-trailing-whitespace
Dec 2, 2026
Merged

mnriem merged 1 commit into
github:mainfrom
pengzh1:fix/presets-command-frontmatter-fence-trailing-whitespace

Conversation

@pengzh1

@pengzh1 pengzh1 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Description

PresetResolver strips the YAML frontmatter off each command file before composing a preset over a core command, so that only the top layer's metadata goes back into the result. It recognised the --- lines only when they were exactly ---. With one stray space or tab after the closing line (easy to pick up when editing by hand) the block was never seen as closed, and the preset's description: / strategy: lines were appended to the composed command body. The same line also hid a strategy: in the command file from the legacy lookup (no strategy in preset.yml), which then stayed at replace and dropped the core command's content. The opening line had the same exact-match rule and the same failure.

All four delimiter checks in _resolver.py now share one predicate: --- on its own line, trailing whitespace ignored. That is the rule CommandRegistrar.parse_frontmatter in agents.py already applies to the closing line, and the same tolerance #5609 brought to replace-section. Leading whitespace is still rejected (an indented --- can be scalar content), so lines such as ---- , --- text or --- are not delimiters.

Testing

Set up this checkout's .venv and editable install using the local development guide. Run commands from the checkout root.

  • Tested locally with .venv/bin/specify --help (Windows PowerShell: .\.venv\Scripts\specify.exe --help) — not run by a human; the agent ran .venv/bin/specify --help (exit 0)
  • Ran relevant existing tests with .venv/bin/python -m pytest tests -q (Windows PowerShell: .\.venv\Scripts\python.exe -m pytest tests -q); use affected test paths for focused checks and LC_ALL=en_US.UTF-8 on macOS — not run by a human; the agent ran the automated tests listed below
  • Tested with a sample project (if applicable) — not done

Automated checks run by the agent on this branch:

  • Three new tests in tests/specify_cli/presets/test_resolver.py, 17 rows in total. The two positive tests (composed command, legacy strategy) have four rows each — a space or a tab after the closing or the opening line — and the composed-command test runs with both LF and CRLF files. On main (397d9d1) the 12 positive rows fail: strategy: append is found in the composed body, and layers[0]["strategy"] is replace instead of append. With this change all 17 pass.
  • The other 5 rows are boundary cases: ---- , --- text and an indented --- as closing line, and ---- and an indented --- as opening line. They pass on main too (the layer is appended unchanged) and fail if the rule is widened to a prefix or a strip match.
  • tests/specify_cli/presets/test_resolver.py: 121 passed.
  • tests/specify_cli/presets/: 886 passed, 1 skipped.
  • The 10 test files outside that directory that reference PresetResolver, resolve_content, collect_all_layers or parse_frontmatter: 1188 passed, 91 skipped.
  • Full suite (LC_ALL=en_US.UTF-8 .venv/bin/python -m pytest tests -q -p no:cacheprovider): 17622 passed, 665 skipped.
  • uvx ruff@0.15.0 check src tests and git diff --check: clean. uvx ruff@0.15.0 format --check reports both changed files, as it already does on main (16 and 66 hunks, the same before and after); none of the added lines is inside a hunk. Repository pre-commit hooks on the changed files: end-of-file-fixer and trailing-whitespace passed, two hooks skipped as not applicable.

AI Disclosure

  • I did not use AI assistance for this contribution
  • I did use AI assistance (fill in the disclosure below)

AI disclosure: Implemented with ZCode (an agent driven by GLM-5.4, reasoning effort max) in autonomous mode on behalf of the account holder. Extent: code generation — the agent found the bug by reading the code, wrote the fix and the regression tests, and ran the automated tests listed above; no docs changes. No human reviewed the code or ran manual tests before this pull request was submitted.

PresetResolver recognised a command file's frontmatter only when the
delimiter lines were exactly ``---``. A stray space or tab after the
closing line meant _split_frontmatter never saw the end of the block,
so a preset's YAML (description, strategy) was appended to the composed
command body, and the legacy frontmatter strategy lookup kept the
default "replace". The opening line had the same exact-match rule and
the same failure.

Both delimiters are now matched with one predicate: ``---`` on its own
line, trailing whitespace ignored. That is the rule
CommandRegistrar.parse_frontmatter already applies to the closing line.
Leading whitespace is still rejected because an indented ``---`` can be
scalar content. The rstrip("\r\n") on the reattached block only trims
its line terminator and is unchanged.

Assisted-by: ZCode (model: GLM-5.4, autonomous)
@pengzh1
pengzh1 requested a review from mnriem as a code owner December 1, 2026 21:29
Copilot AI adaptive review requested due to automatic review settings December 1, 2026 21:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change centralizes whitespace-tolerant fence handling across preset resolution and command composition and includes regression coverage for newline formats, legacy lookup, and invalid delimiters, with no active issues identified.

No open issues 🎉

What changed in this PR

This pull request makes preset command frontmatter fences tolerate trailing whitespace while preserving strict delimiter matching, preventing metadata leakage and restoring legacy strategy detection.

Changes:

  • Adds a shared fence predicate for preset resolution and command composition.
  • Adds regression coverage for whitespace, LF/CRLF input, legacy strategy lookup, and invalid fence variants.
  • The CRLF fixture should write content without platform newline translation so it reliably tests Windows behavior.
File Description
tests/​specify_cli/​presets/​test_resolver.py Adds regression tests for whitespace-tolerant frontmatter fences, LF/CRLF input, legacy strategy handling, and invalid delimiter variants.
src/​specify_cli/​presets/​_resolver.py Centralizes tolerant frontmatter fence detection and applies it to preset strategy lookup and command composition.

Review effort: Lite (adaptive)

@mnriem mnriem added the triage-can-wait Verdict: valid and in-scope but deprioritized; held behind the evidence gate label Dec 2, 2026
@mnriem
mnriem requested a balanced review from Copilot December 2, 2026 12:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the frontmatter delimiter-handling change.

No open issues 🎉

Review effort: Balanced

@mnriem
mnriem merged commit 0320857 into github:main Dec 2, 2026
14 checks passed
@mnriem

mnriem commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

triage-can-wait Verdict: valid and in-scope but deprioritized; held behind the evidence gate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants