Skip to content

Harden the CLI Usage Gate Against False Greens - #2944

Merged
ptr727 merged 13 commits into
developfrom
feature/cli-usage-gate-2831
Oct 13, 2026
Merged

ptr727 merged 13 commits into
developfrom
feature/cli-usage-gate-2831

Conversation

@ptr727

@ptr727 ptr727 commented Oct 13, 2026 •

Copy link
Copy Markdown
Owner

Hardens tests/test_cli_usage.py, the CLI usage gate, against the false greens #2831 lists, and fixes the one usage defect it names.

Closes on promotion: #2831

What changed

  • Unknown flag. The refusal must name --bogus-flag as an unknown option (REFUSED_AS_OPTION, which accepts argparse's, getopt's, PowerShell's and this repository's shell wordings). A script needing a subcommand or positionals is first given arguments that satisfy its parser (BEFORE_FLAG), so the refusal observed is of the flag rather than of a missing argument. A control requires each BEFORE_FLAG set alone to exit 0 or leave an effect, so a listed script that warns on the flag and carries on still fails. An unlisted script that needs an argument is held only to a non-zero exit with no effect, since its output cannot show whether the flag or the missing argument stopped it. Hold an Unlisted CLI That Needs an Argument to Refusing the Unknown Flag Itself #2948 tracks closing that. The run with no prefix still runs too, so an unrecognized first argument is still held to a non-zero exit with no effect.
  • Help. The very first stdout line must be a usage line with the script's name as a whole word, and --help and -h must print the same text, so a default-mode report that merely mentions "usage" fails.
  • Tree check. It runs per run rather than per test method, so a change names the script that made it. It includes ignored paths (git status --ignored, with size and time per listed path) and every directory's time, which catches an empty directory git never lists.
  • Homes. HOME, USERPROFILE, APPDATA, LOCALAPPDATA, the four XDG homes, GH_CONFIG_DIR, CLAUDE_HOME, CLAUDE_CONFIG_DIR, AGENTS_HOME and CODEX_HOME point into empty scratch directories, and GIT_CONFIG_GLOBAL at .gitconfig in the scratch HOME. Any entry a run leaves in a scratch home is an effect, apart from a pwsh run's own startup cache (under XDG_CACHE_HOME off Windows, under USERPROFILE on Windows) and the directories leading to it.
  • Detached grandchildren. Each run carries a token in its environment. After the direct child exits, every process still carrying it, a setsid one included, is found through /proc, named as an effect and killed, with the scan repeating until it finds none. Output goes to files rather than pipes, so a survivor holding the inherited stream no longer stalls the run. This check runs only where /proc exists, so not on Windows or macOS.
  • Discovery. A tracked file with no extension that opens on a shebang is an entry point, run by the interpreter its shebang names, with env flags such as -S and assignments skipped. catalog/snippets/husky/pre-commit joins NOT_COMMAND_LINE as a Git hook.
  • Windows. The whole case no longer skips there. PowerShell entry points run behind .cmd stubs that PowerShell's lookup finds ahead of the real command, and a control runs a bare git through pwsh and requires the stub to record it. Python and bash entry points still skip on Windows, since a process Python starts is found by its .exe name and a stub cannot shadow it. A call naming its .exe explicitly is not shadowed either. wsl and fsutil are now stubbed.
  • Dash-leading name. repo-config/configure.sh passes -- before spec/resolve_description.py's positionals, so a repo name starting with - is read as a name. A test runs configure.sh's own lifted lines with such a name, reusing lift and run_bash from tests/test_configure_archived.py.

Each new check has a control in ControlCase that fails when the check is removed. Mutation runs against a real script confirmed each hardened assertion catches its case: a default-mode "usage" report, a "cannot read --bogus-flag" refusal, a write into an ignored directory, a detached grandchild, and a -h that differs from --help.

No real script failed the hardened gate, so no script needed an expected-failure marker.

Local review findings

Every local finding has an outcome. Fixed in later commits on this branch: the three listed here earlier (non-file home entries, the over-wide PowerShell exclusion, and the first commit's "every home variable" wording, which the squash message written from this body replaces), the pwsh cache on Windows, a sibling control for the cache set-aside, the WITHHELD wording, and the global git config path.

Declined or deferred, with reasons:

  • Moving the configure.sh case into a tests/test_configure_*.py file, and consolidating the shebang reader with scripts/docker_lint.py's. This change may edit only the three files it touches, and docker_lint's helper names only bash and sh.
  • Sharing tree_state's porcelain parsing with scripts/carry.py's git_status_paths: deferred to Share One Porcelain Status Parser Between carry.py and the CLI Usage Gate #2947, for the same file-scope reason.
  • Treating the -- fix as over-hardening. Harden the CLI Usage Gate Against False Greens #2831 lists it as a defect to fix.
  • Treating GIT_CONFIG_GLOBAL's placement as over-hardening. Harden the CLI Usage Gate Against False Greens #2831 asks for tool config homes to be redirected, and placing it in the scratch HOME is what lets the home check see a write.
  • Controls that pin a helper rather than the one-line gate assertion calling it. The mutation runs above exercised each gate assertion against a real script.
  • A word list over the output to catch an unlisted script that warns on the flag and then stops on a missing argument: dropped, since the repository's own missing-argument refusals use wording such a list would miss. Deferred to Hold an Unlisted CLI That Needs an Argument to Refusing the Unknown Flag Itself #2948.
  • The gate reading CODESTYLE.md's argument rule more strictly than its words: the module docstring says so, and stating the stricter reading in CODESTYLE.md itself is deferred to State the CLI Usage Gate's Stricter Reading in CODESTYLE's Argument Rule #2949, outside this change's files.
  • Refusal wordings no tracked script uses: a new wording fails loudly with the output printed, and is added when a script brings it. getopt's "unrecognized option" is accepted already.
  • Walking env -u/-C operands in the shebang reader: that shape already fails the case loudly as naming no runtime, and the shebangs are trusted tracked input.

Verification

  • python3 -m unittest discover -s tests: 2713 tests, OK (7 skipped) on Linux with pwsh present.
  • ruff check and format, mypy, pyright, shellcheck, shfmt, prose_lint.py --diff with and without --check sentence-length, repo_gate.py --check eol and eol-coverage, spec/validate.py: clean.
  • The Windows leg (windows-test) is the native check for the PowerShell path, and it passes. Its first run on this branch showed pwsh writing its startup cache under the scratch USERPROFILE, which the set-aside now covers. Its skipped count is consistent with the PowerShell cases running rather than skipping, and the control requiring the .cmd stub to record a bare git passes there.

🤖 Generated with Claude Code

ptr727 and others added 3 commits October 13, 2026 08:39
The unknown-flag case now requires a refusal naming the flag as an
unknown option, and a script needing a subcommand or positionals gets
arguments that satisfy its parser first, so the refusal seen is of the
flag. Help must print the same text for both spellings, opening on a
usage line naming the script. Each run is checked on its own for a
changed path, ignored paths included, and for a process left running,
found by a token in its inherited environment. Every home and
config-home variable points into the scratch directory.

Discovery also finds an extensionless file opening on a shebang, and on
Windows the PowerShell entry points now run behind .cmd stubs instead
of the whole case skipping.

configure.sh passes -- before resolve_description.py's positionals, so
a repo name starting with a dash is read as a name.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
BEFORE_FLAG now uses arguments that alone exit 0 or reach a stub, and
a control case holds that, so a script that warns on the flag and
carries on fails. Output goes to files rather than pipes, so a
survivor holding an inherited stream is named at once instead of
timing the run out, and the survivor scan repeats until it finds none.
The tree check also records every directory's time, which catches an
empty directory git never lists. Controls pin the refusal pattern, the
usage-line check, the ignored-path check, and the survivor check.

The env shebang reader skips env's own flags and assignments, wsl and
fsutil are stubbed, and the configure.sh case reuses the lift and
run_bash helpers of the archived-repository tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A file a run leaves in a scratch home is now an effect, PowerShell's
own startup cache aside, so redirecting a home no longer hides a write
into it. A control runs a bare git through pwsh and requires the stub
to record it, which on Windows proves the .cmd stub is found ahead of
the real command. The docstring now names HOMES rather than claiming
every home variable.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI lite review requested due to automatic review settings October 13, 2026 16:14
@ptr727

ptr727 commented Oct 13, 2026

Copy link
Copy Markdown
Owner Author

A recorded local strict-review pass covers head 8726c1b782d3391599d21aaed77e13391ed81f86, the content this pull request carries at that commit against develop, and it recorded 3 findings.

@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 15 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: ptr727/ProjectTemplate/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f98469bc-bec6-49c8-8fce-9d37d1a319db
📥 Commits

Reviewing files that changed from the base of the PR and between e999d3e and f97f859.

📒 Files selected for processing (3)
  • repo-config/configure.sh
  • spec/resolve_description.py
  • tests/test_cli_usage.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

home_writes now reports every entry a run leaves in a scratch home, an
empty directory or a symlink included, rather than only regular files.
The exclusion is now only the powershell directory under
XDG_CACHE_HOME, and only for a pwsh run, so a Python or bash script
writing under a powershell directory is reported. The control covers
all three shapes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.96%. Comparing base (4f4ca8c) to head (f97f859).
⚠️ Report is 2 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #2944   +/-   ##
========================================
  Coverage    59.96%   59.96%           
========================================
  Files           17       17           
  Lines         8777     8778    +1     
========================================
+ Hits          5263     5264    +1     
  Misses        3514     3514           
Flag Coverage Δ
python-3.13 59.96% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Unresolved findings remain in output validation, shebang parsing, filesystem-effect detection, and Git config tracking.

3 open findings
What changed in this PR

Hardens the CLI usage gate against false positives and fixes handling of repository names beginning with -.

Changes:

  • Strengthens help, option, sandbox, process, and discovery checks.
  • Adds Windows and extensionless shebang coverage.
  • Passes -- before description resolver positional arguments.
File Description
tests/​test_cli_usage.py Adds hardened CLI sandbox and discovery checks.
spec/​resolve_description.py Documents end-of-options handling.
repo-config/​configure.sh Passes -- before positional arguments.

🧠 Review effort: Lite


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tests/test_cli_usage.py Resolved
Comment thread tests/test_cli_usage.py Resolved
Comment thread tests/test_cli_usage.py Resolved
@ptr727

ptr727 commented Oct 13, 2026

Copy link
Copy Markdown
Owner Author

A recorded local strict-review pass covers head a7cda70069cb4b0138d002772976228b1f74340c, the content this pull request carries at that commit against develop, and it recorded 2 findings.

ptr727 and others added 2 commits October 13, 2026 09:31
…Reads

On Windows pwsh writes its startup cache under the scratch USERPROFILE,
so a pwsh run now sets that path aside, with the directories leading to
it, beside the XDG_CACHE_HOME one used elsewhere. The usage check reads
the very first line and needs the script's stem as a whole word, so
usage for another script whose name contains it no longer passes. An
env shebang skips the operand of -u and -C before naming the command.
Controls cover each.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A control now calls home_writes for a pwsh run directly, with a sibling
under AppData/Local and a powershell directory under XDG_CONFIG_HOME,
and requires both still to be reported, so a widened set-aside fails.
The env operand walk is dropped again: an env shebang whose -u or -C
operand the reader takes for the command already fails the case loudly
as naming no runtime.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ptr727

ptr727 commented Oct 13, 2026

Copy link
Copy Markdown
Owner Author

A recorded local strict-review pass covers head 1eff4916d582890df1f8a6cb1aa26a3eff3be157, the content this pull request carries at that commit against develop, and it recorded 3 findings.

ptr727 and others added 3 commits October 13, 2026 09:44
…thheld

The set-aside control now plants a sibling of each cache leaf as well
as of its parents, so widening the set-aside to either cache's parent
fails it. The module docstring names the WITHHELD variables instead of
claiming no credential reaches a run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The control accepts any effect from a BEFORE_FLAG set run alone, so the
docstrings now say an effect rather than a stub call.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ptr727

ptr727 commented Oct 13, 2026

Copy link
Copy Markdown
Owner Author

A recorded local strict-review pass covers head b81be493e07d51f4e30ffd8e9b1d3cb9ac7e1cb9, the content this pull request carries at that commit against develop, and it recorded 2 findings.

…l Config in HOME

The run that must name the unknown flag now also fails where its output
reports a missing or required argument, so a script outside
BEFORE_FLAG that warns on the flag and then stops on a missing argument
no longer passes. GIT_CONFIG_GLOBAL now points at .gitconfig in the
scratch HOME, so a global git config write lands where the home check
reports it. Controls cover both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A word list cannot show whether the flag or a missing argument stopped
a run, and this repository's own missing-argument refusals use wording
it would not match. The check and its control are removed, and the
docstring now says an unlisted script that needs an argument is held
only to a non-zero exit with no effect. #2948 tracks closing that.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ptr727

ptr727 commented Oct 13, 2026

Copy link
Copy Markdown
Owner Author

A recorded local strict-review pass covers head ead93aef8f6ccd6972f81c003ffa6849f3f90675, the content this pull request carries at that commit against develop, and it recorded 1 finding.

An unlisted script is still held to naming the flag as an unknown
option. What it is not held to is a run without the flag, so the
docstring now says that one warning on the flag and then stopping on a
missing argument passes until it is listed in BEFORE_FLAG.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e Rule Strictly

A shell script parsing with getopt refuses an unknown long option as
"unrecognized option", which the refusal pattern now accepts, with a
control. The module docstring says the checks read CODESTYLE.md's rule
strictly, so meeting the rule's words is not always enough here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ptr727

ptr727 commented Oct 13, 2026

Copy link
Copy Markdown
Owner Author

A recorded local strict-review pass covers head f97f859d4a9a70aa5509e7e7383602ea6d310930, the content this pull request carries at that commit against develop, and it recorded 0 findings.

@ptr727

ptr727 commented Oct 13, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
⚠️ Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@ptr727

ptr727 commented Oct 13, 2026

Copy link
Copy Markdown
Owner Author

Answering the Copilot round on 8726c1b (#2944 (review)), whose headline names four areas against 3 open findings.

The fourth headline area, "Git config tracking", has no thread of its own. Read against that head: GIT_CONFIG_GLOBAL pointed at a file outside every scratch home, so a global git config write from a git that bypassed the stub would have reported no effect. Fixed in 6be5804: it now points at .gitconfig in the scratch HOME, which the home check scans, and ControlCase.test_a_global_git_config_write_is_seen holds it.

The three threaded findings were answered and resolved in their threads (one declined with evidence, two fixed).

@ptr727
ptr727 merged commit 7fd06c5 into develop Oct 13, 2026
13 checks passed
ptr727 added a commit that referenced this pull request Oct 13, 2026
…nd Attest Sleep Alias to Main (#2950)

- #2945 makes `gh-write-guard` fail closed on the push shapes it could
not read: a push inside an `eval`/`source` argument, a quoted command
handed to `bisect run` or `submodule foreach`, a here-string or attached
`-c` wrapper body, a `-c`/`--config-env` override on a push, a bare push
inside a wrapper body or after a head-moving command, `HEAD`/`@`/`:`
destinations, and `--admin` inside a wrapper. It also denies a mutating
git command whose `-C`, `--work-tree`, `--git-dir`, `GIT_DIR=`, or
leading `cd` holds a `..` component. A differential over 1043 commands
found no command the new guard allows that the previous one denied. The
over-denials it introduces are listed in that pull request with their
workarounds, and the shared gaps outside both issues are tracked on
#2943
- #2944 hardens `tests/test_cli_usage.py`: the unknown-flag case must
see the flag itself refused, the help case must open on a usage line
naming the script, the tree check sees ignored paths and empty
directories, every config home is redirected, detached grandchildren are
found through `/proc`, extensionless shebang CLIs are discovered, and
PowerShell entry points run natively on Windows.
`repo-config/configure.sh` passes `--` before a name
- #2942 sleeps through an alias in `pr_review.py`'s attest head wait, so
the `TestAttest` timing tests no longer record subprocess polling
sleeps, and states in `scripts/README.md` when a genuine review outranks
a refusal on the same head
- #2952 answers this promotion's first review round: the guard reads
every `-c`/`--config-env` spelling and `--attr-source`, judges a head
move per push (scanning the whole command where a pipe, background job,
loop, or fused redirection can reorder it), stops reading a lone `:` as
a deletion, and the CLI usage gate sets aside only the host's own
PowerShell startup cache. Three precision findings from the second
round, each a broader deny rather than a new allow, are deferred to
#2953

Closes #2824
Closes #2831
Closes #2932
Closes #2934
Closes #2936

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@ptr727
ptr727 deleted the feature/cli-usage-gate-2831 branch October 13, 2026 20:20
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.

2 participants