Skip to content

fix: reject whitespace-only plan templates and plan.md across scripts - #5515

Open
Gaoshou101 wants to merge 2 commits into
github:mainfrom
Gaoshou101:fix/5035-whitespace-plan-validation
Open

Gaoshou101 wants to merge 2 commits into
github:mainfrom
Gaoshou101:fix/5035-whitespace-plan-validation

Conversation

@Gaoshou101

@Gaoshou101 Gaoshou101 commented Nov 22, 2026 •

Copy link
Copy Markdown

Fixes #5035

Description

Previously, setup-plan and check-prerequisites checked for an empty plan-template.md or plan.md by byte length (-s / Length -eq 0 / template_content == "" / st_size == 0), allowing whitespace-only files (such as \n \n\t\n, CRLF whitespace, or UTF-8 Unicode spaces like U+00A0 / U+2003 / U+3000) to pass validation.

This PR aligns all three script ports (Bash, PowerShell, and Python) so whitespace-only content is treated as empty while preserving existing behavior for missing or valid non-whitespace files and failing explicitly on unreadable plan.md files:

  • scripts/python/setup_plan.py: Check not template_content.strip() before writing plan.md.
  • scripts/python/check_prerequisites.py: Read plan.md (OSError fails explicitly with exit 1) and check not plan_content.strip().
  • scripts/bash/common.sh, scripts/bash/setup-plan.sh & scripts/bash/check-prerequisites.sh: Add file_has_non_whitespace helper so Bash matches Python/PowerShell whitespace semantics across ASCII and UTF-8 Unicode whitespace (LC_ALL=C grep -q '[!-~]' fast path for visible ASCII, plus a pure-POSIX LC_ALL=C sed + tr UTF-8 Unicode-whitespace strip with zero Python dependency), while treating status 1 as empty/whitespace-only and emitting ERROR: Failed to read ... + propagating grep error status (> 1) on unreadable/I/O errors.
  • scripts/powershell/setup-plan.ps1 & scripts/powershell/check-prerequisites.ps1: Check [string]::IsNullOrWhiteSpace(...) (with explicit [System.IO.File]::ReadAllText(..., [System.Text.Encoding]::UTF8) read error handling in check-prerequisites.ps1).
  • tests/test_setup_plan_python_parity.py & tests/test_check_prerequisites_python_parity.py: Update test_plan_prerequisite_diagnostics, test_plan_template_failure_and_retry, test_existing_plan_is_preserved, and replace test_whitespace_plan_template_is_not_empty with test_whitespace_plan_template_is_rejected (covering LF, CRLF, and UTF-8 Unicode whitespace across template, override, Bash no-Python fallback, and plan.md), plus add deterministic plan.md read-failure negative tests (test_plan_prerequisite_read_error_fails_explicitly and test_bash_plan_prerequisite_propagates_grep_read_error).

Windows PowerShell 5.1 compatibility & test scope

  • Windows PowerShell 5.1 (.NET Framework 4.x) compatibility:
    • Both .ps1 files remain strictly ASCII-only (tests/test_ps1_encoding.py passed) and avoid .NET Core-only [System.IO.Path] members (tests/test_init_dir.py static 5.1 guard passed).
    • The PowerShell APIs used in this PR—[string]::IsNullOrWhiteSpace(string) (.NET Framework 4.0+), [System.IO.File]::ReadAllText(string, System.Text.Encoding) (.NET Framework 2.0+, already used throughout scripts/powershell/common.ps1), [System.Text.Encoding]::UTF8, [Console]::Error.WriteLine(...), and Test-Path -LiteralPath ... -PathType Leaf (PowerShell 3.0+)—are native to Windows PowerShell 5.1.
  • Runtime environment limitation: This Linux environment does not have pwsh, Windows PowerShell 5.1, or shellcheck installed, so the 105 PowerShell runtime parity cases were skipped locally by pytest (they run on windows-latest / pwsh in CI). Bash + Python parity (298 passed), .ps1 ASCII/static PS 5.1 checks, bash -n, Bash 3.2 case-modification portability checks, check_file_helper_migration.py, git diff --check, and uvx ruff@0.15.0 check passed locally. Existing setup-plan preservation of an already-present empty or whitespace-only plan.md remains unchanged.

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)
  • Ran relevant existing tests with .venv/bin/python -m pytest tests -q (Windows PowerShell: .\.venv\Scripts\python.exe -m pytest tests -q); focused suite (tests/test_setup_plan_python_parity.py, tests/test_setup_plan_no_overwrite.py, tests/test_setup_plan_feature_json.py, tests/test_check_prerequisites_python_parity.py, tests/test_check_prerequisites_paths_only.py, tests/test_ps1_encoding.py, tests/test_init_dir.py, tests/test_agent_config_consistency.py, tests/contract/test_wheel_core_pack_scripts.py, tests/contract/test_file_helper_migration_guard.py) passed: 298 passed, 105 skipped (PowerShell unavailable on Linux)
  • Tested with a sample project (if applicable)

AI Disclosure

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

AI disclosure: Assisted by Hermes Agent (gemini-4-hydrogen-xhigh / gemini-4-carbon) for root-cause analysis, cross-port patch generation (Bash/PowerShell/Python), and parity/negative test updates, with independent review passes by gpt-6.2-sol and claude-opus-5-5. All code edits and Bash/Python parity tests were executed and verified locally in the repository .venv.

@Gaoshou101
Gaoshou101 requested a review from mnriem as a code owner November 22, 2026 05:30
Copilot AI adaptive review requested due to automatic review settings November 22, 2026 05:30

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.

🟡 Changes recommended

Unicode-whitespace handling differs across ports, and prerequisite read-error paths lack deterministic negative test coverage.

2 open issues
What changed in this PR

Addresses #5035 by replacing byte-length checks with whitespace-content validation in Spec Kit’s plan setup and prerequisite scripts.

Changes:

  • Reject whitespace-only templates and plans across Bash, PowerShell, and Python.
  • Handle prerequisite read failures explicitly.
  • Expand parity tests for LF/CRLF whitespace, template overrides, and preservation of existing plans.
File Description
tests/​test_setup_plan_python_parity.py Tests whitespace-template rejection, retry, and existing-plan preservation.
tests/​test_check_prerequisites_python_parity.py Adds LF/CRLF whitespace-plan rejection cases.
scripts/​python/​setup_plan.py Uses strip() to reject whitespace-only templates.
scripts/​python/​check_prerequisites.py Reads plan content and handles whitespace and read failures.
scripts/​powershell/​setup-plan.ps1 Uses IsNullOrWhiteSpace for template validation.
scripts/​powershell/​check-prerequisites.ps1 Adds content validation and explicit read-error handling.
scripts/​bash/​setup-plan.sh Checks template content with grep and propagates errors.
scripts/​bash/​check-prerequisites.sh Checks plan content with grep and propagates errors.

Review effort: Balanced (adaptive)

Comment thread scripts/bash/setup-plan.sh Outdated
if resolve_template_content "plan-template" "$REPO_ROOT" > "$template_file"; then
if [[ ! -s "$template_file" ]]; then
_template_grep_status=0
LC_ALL=C grep -q '[^[:space:]]' "$template_file" || _template_grep_status=$?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed in 6c68b64:

  • Added file_has_non_whitespace in scripts/bash/common.sh and wired both scripts/bash/setup-plan.sh and scripts/bash/check-prerequisites.sh to it. It keeps a fast LC_ALL=C grep -q '[!-~]' check for visible ASCII and strips UTF-8 Unicode whitespace (U+0085, U+00A0, U+1680, U+2000–U+200A, U+2028, U+2029, U+202F, U+205F, U+3000) plus ASCII whitespace via pure POSIX LC_ALL=C sed + tr (no Python 3 runtime dependency).
  • Added UTF-8 Unicode-whitespace cases (U+00A0 + U+2003 LF, U+3000 + U+202F CRLF, override templates, and no-Python Bash tests) to both tests/test_setup_plan_python_parity.py and tests/test_check_prerequisites_python_parity.py.

Comment on lines +242 to +244
except OSError as exc:
print(f"ERROR: Failed to read {paths.impl_plan}: {exc}", file=sys.stderr)
return 1

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed in 6c68b64:

  • Added test_plan_prerequisite_read_error_fails_explicitly across Bash, PowerShell, and Python (chmod 0 on POSIX when enforced; msvcrt.locking exclusive byte-range lock on Windows) plus test_bash_plan_prerequisite_propagates_grep_read_error, asserting empty stdout, explicit ERROR: Failed to read ... diagnostic (not plan.md is empty), unchanged plan.md bytes/mtime, exit 1 for Python/PowerShell, and propagated status > 1 for Bash (scripts/bash/check-prerequisites.sh also now emits ERROR: Failed to read ... before exiting).
  • Updated the PR description with Windows PowerShell 5.1 (.NET Framework 4.x) API compatibility verification ([string]::IsNullOrWhiteSpace, [System.IO.File]::ReadAllText(..., [System.Text.Encoding]::UTF8), ASCII-only .ps1 check in tests/test_ps1_encoding.py, and .NET Core-only member guard in tests/test_init_dir.py) along with local Linux test limitations.

…tests

- Add pure-POSIX file_has_non_whitespace in scripts/bash/common.sh (visible
  ASCII grep fast path + UTF-8 Unicode whitespace sed/tr strip) so
  setup-plan.sh and check-prerequisites.sh reject Unicode whitespace
  (such as U+00A0, U+2003, U+3000, U+202F) consistently with Python and
  PowerShell without requiring Python 3
- Emit explicit 'ERROR: Failed to read ...' diagnostic on unreadable
  plan.md in Bash check-prerequisites.sh while propagating error status > 1
- Expand setup-plan and check-prerequisites parity suites with Unicode
  whitespace template, override, and plan.md cases
- Add deterministic negative tests for unreadable plan.md across ports and
  propagated Bash grep read errors
Copilot AI adaptive review requested due to automatic review settings November 22, 2026 07:40
@Gaoshou101

Copy link
Copy Markdown
Author

Thanks for the review feedback! Both findings have been addressed in 6c68b64:

  1. Cross-port Unicode whitespace alignment (discussion_r4656834787)

    • Added a pure-POSIX file_has_non_whitespace helper in scripts/bash/common.sh (used by scripts/bash/setup-plan.sh and scripts/bash/check-prerequisites.sh) that preserves a fast LC_ALL=C grep -q '[!-~]' check for visible ASCII and strips UTF-8 Unicode whitespace (U+0085, U+00A0, U+1680, U+2000–U+200A, U+2028, U+2029, U+202F, U+205F, U+3000) before checking remaining non-whitespace bytes—aligning Bash with Python .strip() and PowerShell [string]::IsNullOrWhiteSpace(...) even when Python 3 is unavailable.
    • Added UTF-8 Unicode-whitespace test cases (U+00A0 + U+2003, U+3000 + U+202F, template override, and Bash without Python) to tests/test_setup_plan_python_parity.py and tests/test_check_prerequisites_python_parity.py.
  2. Deterministic check-prerequisites read-error tests & Windows PowerShell 5.1 compatibility evidence (discussion_r4656834803)

    • Added test_plan_prerequisite_read_error_fails_explicitly (covering Bash, PowerShell, and Python in JSON and text modes) and test_bash_plan_prerequisite_propagates_grep_read_error in tests/test_check_prerequisites_python_parity.py, verifying empty stdout, explicit ERROR: Failed to read ... diagnostics rather than plan.md is empty, unchanged file bytes/mtime, exit 1 for Python/PowerShell, and propagated exit status > 1 for Bash.
    • Documented Windows PowerShell 5.1 (.NET Framework 4.x) API & encoding compatibility evidence and local Linux runtime skip scope (pwsh unavailable locally) in the PR description.

Verification

  • Focused pytest suite (tests/test_setup_plan_python_parity.py, tests/test_setup_plan_no_overwrite.py, tests/test_setup_plan_feature_json.py, tests/test_check_prerequisites_python_parity.py, tests/test_check_prerequisites_paths_only.py, tests/test_ps1_encoding.py, tests/test_init_dir.py, tests/test_agent_config_consistency.py, tests/contract/test_wheel_core_pack_scripts.py, tests/contract/test_file_helper_migration_guard.py): 298 passed, 105 skipped
  • uvx ruff@0.15.0 check --config pyproject.toml src tests scripts/python/setup_plan.py scripts/python/check_prerequisites.py: All checks passed!
  • .github/scripts/check_file_helper_migration.py --base origin/main --head HEAD: passed
  • bash -n, Bash 3.2 case-modification check, and git diff --check origin/main HEAD: passed

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.

🟡 Changes recommended

Missing pipefail allows intermediate tr or wc failures to be masked, causing valid Unicode-only plans to be reported as empty rather than unreadable.

3 open issues

Review effort: Balanced (adaptive)

Comment thread scripts/bash/common.sh
rm -f "$_tmp_in" "$_tmp_out"
return 2
fi
_rem_len=$(LC_ALL=C tr -d '[:space:]' < "$_tmp_out" | wc -c | tr -d '[:space:]') || { rm -f "$_tmp_in" "$_tmp_out"; return 2; }
@mnriem mnriem added the triage-can-wait Verdict: valid and in-scope but deprioritized; held behind the evidence gate label Nov 22, 2026
@mnriem

mnriem commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Please address Copilot feedback

This branch has not been deployed

No deployments
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.

[Bug]: Plan setup and prerequisite checks accept a whitespace-only plan template or plan.md

3 participants