Skip to content

fix(verify): a narrowed test command cannot earn SAFE - #112

Merged
omsherikar merged 2 commits into
mainfrom
fix/110-narrowed-testcmd-false-safe
Aug 17, 2026
Merged

omsherikar merged 2 commits into
mainfrom
fix/110-narrowed-testcmd-false-safe

Conversation

@omsherikar

@omsherikar omsherikar commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

What

A caller-supplied testCmd that names a subset of the suite scoped the entire verification run, and the report said nothing about it. Coverage could report the changed code as fully exercised while the one test that would have caught the change was never selected, so the verdict read SAFE.

This adds a pure classifier (src/verify/test-scope.ts) that reads the command as full / narrowed / unknown, floors narrowed at UNPROVEN, and discloses the result on a new additive VerdictReport.testScope.

Closes #110
Closes #111

Why

Reproduced against the published 0.4.0 binary before any code was written. One repo, one diff (return x * 2 becomes return x * 3), two tests: test_scale executes the changed line without pinning its value, test_report pins it and fails. Only the command differs.

python3 -m pytest -q                       -> UNSAFE   'value=6' == 'value=4' fails
python3 -m pytest -q tests/test_scale.py   -> SAFE     "Tests pass and the changed code is covered."

A change that provably breaks a test sitting in the same repository returned SAFE, through the MCP verify_change tool, which applies no authentication.

The narrowed run reported changedStatements {total: 1, covered: 1} — full statement coverage — so the stricter statement rule proposed in #109 does not touch this case. The two defects are independent, and there is a test pinning that fact.

Three false SAFEs, not one

The first was the reported bug. Review found two more, each reproduced end to end before being fixed:

--collect-only. It selects zero tests, exits 0 so the tests gate passes on exit code alone, and still imports every test module, so coverage marks module-level changed lines as executed. The original flag table listed it as an ordinary boolean, i.e. explicitly non-narrowing.

python3 -m pytest -q                 -> UNSAFE
python3 -m pytest -q --collect-only  -> SAFE, coverage 1/1

PYTEST_ADDOPTS. pytest appends this variable's value to its own argv, so it is a filter. The leading NAME=VALUE strip discarded it and the classifier then answered full, its strongest claim.

PYTEST_ADDOPTS="-k test_scale" python3 -m pytest -q  -> SAFE

This one matters more than an ordinary gap: the leading-assignment prefix is what issues #95 and #98 tell users to adopt for shadow bypass, and it is the only in-band way to pass environment over MCP. PYTEST_ADDOPTS / VITEST_ADDOPTS / JEST_ADDOPTS values now go through the same argument scanner as the command line. PYTHONPATH stays inert, with a regression row guarding it.

Evidence

Red-first, verified by restoring main's src/ under the branch's tests:

× the same diff under a narrowed command is UNPROVEN, not SAFE
  → expected 'SAFE' to be 'UNPROVEN'
× the full suite catches the change: UNSAFE
  → expected undefined to deeply equal { scope: 'full', … }

The --collect-only test is red against the pre-fix classifier in the same way (expected 'SAFE' not to be 'SAFE').

Gate: typecheck, lint --max-warnings 0, format:check, and 407 tests across 27 files, all green. Suite was 333 before this branch.

The rule

Scope Meaning Can be SAFE
full Parsed, names no filter yes
narrowed Names a path, node id, -k, -m, -t, --onlyChanged, --collect-only, … no
unknown Not recognised (make test, a plugin flag, a wrapper script) yes

unknown deliberately does not floor. ADR-12 originally justified that by saying wrapper commands would otherwise all lose SAFE; review showed that is false — toCoverageRunArgs already declines make test, npm test and tox, so they cannot reach SAFE today anyway. The real cost is unrecognised plugin flags on recognised runners (pytest --doctest-modules), which flooring would turn into SAFE-killers fixable only by a PR to us. The ADR now carries the measured table rather than the intuition.

Known limitations, stated rather than implied

  • The residual hole. python -m unittest tests.test_foo and python3 tests/runtests.py auth both narrow, both reach SAFE, and both classify unknown. Recorded as Alternative C in ADR-12 with a follow-up; it needs its own red-first fixture and should not ride in on this branch.
  • Evadable in one step. An agent that wants SAFE can write sh -c "pytest tests/x.py". This is a mistake-catcher, not an adversary-stopper, and the ADR says so.
  • Ambient environment is invisible. Narrowing set in pytest.ini addopts, an exported PYTEST_ADDOPTS, or a vitest include is not detected. full means "this command names no filters", not "this run was the whole suite".
  • pytest tests/ counts as narrowed, deliberately: tests can live outside that directory, and calling it full would be a false SAFE. pytest . is full. Documented.
  • The vitest and jest tables cannot change a verdict today, since coverage is Python-only and a JS diff can never reach SAFE. They buy disclosure only.

Blast radius

  • VerdictReport.testScope is additive; reportVersion stays 1. src/contracts.ts untouched.
  • The field stays optional on purpose: its absence is how a consumer distinguishes a pre-0.5.0 stored report from one produced by an engine that floors.
  • fuseVerdict's scope parameter is required, so a future call site that forgets it is a compile error rather than a silent false SAFE.
  • Exit codes unchanged. SAFE and UNPROVEN both exit 0. Pipelines that parse the verdict string will see more UNPROVEN.
  • Real diffs verified with a narrowed command that read SAFE will now read UNPROVEN. That is the point, and it is a behaviour change every user of --test-cmd sees.

What I could not verify locally

  • Windows and Linux behaviour; everything here ran on macOS. The classifier is pure string handling, but basename splits on / only.
  • The MCP surface carries testScope structurally (the report is serialized verbatim) but no test asserts it on that surface; tests/unit/mcp/verify-change.test.ts does not mention the field.
  • runVerifyDiffCommand has no test coverage, so the CLI wiring of the new note is unasserted even though formatTestScopeNote itself is tested.
  • src/mcp/tools/verify-change.ts still describes testCmd as "Override the test command" with no caveat. That description is what an agent reads, and the ADR's threat model is an agent choosing its own command. Worth a follow-up.

Summary by CodeRabbit

  • New Features

    • Added test-scope reporting that classifies commands as full, narrowed, or unknown.
    • Narrowed test commands can no longer receive a SAFE verdict; they are capped at UNPROVEN.
    • CLI output now explains when test selection limits prevent a SAFE result.
    • Added testScope details to verification reports.
  • Documentation

    • Documented test-scope behavior, limitations, examples, and command handling.
  • Tests

    • Added comprehensive coverage for command classification, verdicts, reporting, and CLI messaging.

A caller-supplied testCmd naming a subset of the suite scoped the whole
verification run. Coverage could report the changed code as fully
exercised while the one test that would have caught the change was never
selected, so the verdict read SAFE.

Reproduced against the published 0.4.0 binary: one repo, one diff, two
test files, only the command differing.

  python3 -m pytest -q                      -> UNSAFE
  python3 -m pytest -q tests/test_scale.py  -> SAFE

The narrowed run reported changedStatements 1/1, full statement
coverage, so the stricter rule in #109 does not cover this case.

Classify the command as full / narrowed / unknown in a pure module and
floor narrowed at UNPROVEN. unknown does not floor: that would put SAFE
out of reach for any project running `make test`. The gap is disclosed
on the new testScope report field and recorded in ADR-12.

The classifier degrades to unknown on anything it does not recognise,
because a false narrowed costs a user their SAFE. Two negative tables
guard the PYTHONPATH= forms established by #95 and #98, and the
parallelism flags: -n is xdist worker count, not a filter.

testScope is additive, so reportVersion stays 1 and contracts.ts is
untouched. Exit codes are unchanged; SAFE and UNPROVEN both exit 0.

Closes #110
Closes #111
Three-lens review found the classifier's `full` verdict is now the
permission to be SAFE, and two table entries granted it wrongly. Both
reproduced end to end before fixing.

--collect-only selects zero tests, exits 0 so the tests gate passes on
exit code alone, and still imports every test module, so coverage marks
module-level changed lines executed:

  python3 -m pytest -q                 -> UNSAFE
  python3 -m pytest -q --collect-only  -> SAFE, coverage 1/1

PYTEST_ADDOPTS is appended to pytest's argv, so its value is a filter.
The leading NAME=VALUE strip discarded it and answered `full`:

  PYTEST_ADDOPTS="-k test_scale" python3 -m pytest -q  -> SAFE

That one arrives through the form issues #95 and #98 tell users to
adopt, and the only in-band way to pass env over MCP. Values of
PYTEST_ADDOPTS / VITEST_ADDOPTS / JEST_ADDOPTS now go through the same
argument scanner as the command line. PYTHONPATH stays inert.

Also fixed, each with rows:
- pytest -o/--override-ini testpaths= and addopts= were consumed
  unexamined and read `full`
- the zero-test-run rule applied to pytest only; `npx jest -h` was `full`
- vitest -w is --watch and takes no value; filed as value-taking it
  swallowed the next token and read `full`
- pytest --cov takes an optional value, so `--cov src` was a false
  `narrowed` on a very common CI command
- shell composites read operators as paths; now share
  SHELL_COMPOSITE_RE with the coverage runner so the two parsers cannot
  disagree
- `pytest .`, `pytest ""` and `pytest -q #comment` were false `narrowed`
- every `unknown` now carries a reason; the CLI says so instead of
  letting a SAFE on an unparsed command read as clean
- an empty override is falsy at detect.ts:26, so it is `detected`, not a
  claim about a caller command
- fuseVerdict's scope parameter is required: an optional one makes a
  forgotten argument a silent false SAFE

ADR-12's rejection of flooring `unknown` cited a cost that does not
exist. Measured: coverage already declines make/npm/tox, so flooring
them costs nothing; the real cost is unrecognised plugin flags on
recognised runners. Rationale rewritten, and the residual hole
(unrecognised runner carrying arguments) recorded as Alternative C with
follow-ups rather than left implied.

Suite 370 -> 407.
Copilot AI lite review requested due to automatic review settings August 17, 2026 07:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

Review Change Stack

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Free

Run ID: caf71d48-7db8-4f00-9c27-4bd14ece41cd

📥 Commits

Reviewing files that changed from the base of the PR and between a344f2b and df24d00.

📒 Files selected for processing (14)
  • CHANGELOG.md
  • dev-docs/decisions/12-narrowed-testcmd-policy.md
  • docs/mcp/tool-reference.mdx
  • docs/verification/verdicts.mdx
  • src/analyze/coverage/python-line-coverage.ts
  • src/cli/verify-diff-command.ts
  • src/index.ts
  • src/verify/test-scope.ts
  • src/verify/verdict-fuse.ts
  • src/verify/verify-diff.ts
  • tests/integration/verify-diff.test.ts
  • tests/unit/cli/verify-diff-command.test.ts
  • tests/unit/verify/test-scope.test.ts
  • tests/unit/verify/verdict-fuse.test.ts

Included review availability: Your plan includes up to 1 review per rolling hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

Changes

Test Scope Policy

Layer / File(s) Summary
Test command scope classifier
src/verify/test-scope.ts, src/analyze/coverage/python-line-coverage.ts, tests/unit/verify/test-scope.test.ts
Classifies supported test commands as full, narrowed, or unknown.
Scope-aware verdict fusion
src/verify/verdict-fuse.ts, src/verify/verify-diff.ts, tests/unit/verify/verdict-fuse.test.ts, tests/integration/verify-diff.test.ts
Adds testScope to reports and floors narrowed test results at UNPROVEN.
CLI reporting and policy documentation
src/cli/verify-diff-command.ts, src/index.ts, CHANGELOG.md, dev-docs/decisions/12-narrowed-testcmd-policy.md, docs/..., tests/unit/cli/verify-diff-command.test.ts
Reports scope advisories, exports scope types, and documents classification and verdict behavior.

Estimated code review effort: 4 (Complex) | ~45 minutes

Merge Risk: ⚪ Minimal · up to df24d

The change makes narrowed test commands report UNPROVEN instead of SAFE and adds scope disclosure; no actionable merge-blocking risk remains after normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant verifyDiff
  participant assessTestScope
  participant fuseVerdict
  participant CLI
  Caller->>verifyDiff: provide testCmd
  verifyDiff->>assessTestScope: classify testCmd
  assessTestScope-->>verifyDiff: TestScopeAssessment
  verifyDiff->>fuseVerdict: combine results, coverage, and scope
  fuseVerdict-->>CLI: VerdictReport with testScope
  CLI-->>Caller: verdict and scope advisory
Loading

Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

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

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.

Decide whether a narrowed testCmd should floor the verdict False SAFE: a narrowed --test-cmd scopes the verdict silently

2 participants