Skip to content

Commit ebdd142

Browse files
authored
Merge pull request #112 from Refactron-ai/fix/110-narrowed-testcmd-false-safe
fix(verify): a narrowed test command cannot earn SAFE
2 parents a344f2b + df24d00 commit ebdd142

14 files changed

Lines changed: 1723 additions & 50 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,41 @@ Versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
---
99

10+
## [Unreleased]
11+
12+
### Changed
13+
14+
**What `SAFE` means is narrower.** A test command that names a subset of the
15+
suite can no longer earn `SAFE`.
16+
17+
Passing a `testCmd` such as `pytest tests/unit/test_foo.py`, or one using `-k`,
18+
`-m`, `-t` or `--onlyChanged`, scopes the entire verification run. Coverage
19+
could report the changed code as fully exercised while the one test that would
20+
have caught the change was never selected. Reproduced: one repo, one diff, two
21+
test files, only the command differing:
22+
23+
```bash
24+
python3 -m pytest -q # UNSAFE — the change breaks a test
25+
python3 -m pytest -q tests/test_scale.py # was SAFE, now UNPROVEN
26+
```
27+
28+
Refactron now classifies the command as `full`, `narrowed` or `unknown` and
29+
reports it on the new `testScope` field. `narrowed` floors the verdict at
30+
`UNPROVEN`. `unknown` (an unparsed wrapper such as `make test`) does not floor,
31+
and that gap is documented rather than hidden. A `PYTHONPATH=` prefix is not
32+
narrowing, so the remedy for shadow bypass is unaffected.
33+
34+
Exit codes are unchanged: `SAFE` and `UNPROVEN` both exit `0`. Pipelines that
35+
parse the verdict string will see more `UNPROVEN`.
36+
37+
### Added
38+
39+
- `testScope` on `VerdictReport`, carried by `verify-diff --json` and the MCP
40+
`verify_change` tool. Additive; `reportVersion` stays `1`.
41+
- A CLI note naming the filter that cost a run its `SAFE`.
42+
43+
---
44+
1045
## [0.4.0] — 2026-08-06
1146

1247
**Breaking, despite being a minor.** Refactron is now only a verification layer.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# ADR-12: A narrowed test command floors the verdict at UNPROVEN
2+
3+
> Status: **Accepted**
4+
> Date: 2026-08-17
5+
> Deciders: @omsherikar
6+
7+
## Context
8+
9+
`--test-cmd` / `testCmd` is handed straight to a shell (`src/verify/runners/detect.ts:27`) and nothing inspected it. With no override, every detected runner is whole-suite by construction: `npx vitest run`, `npx jest`, `python3 -m pytest -q`, with no path arguments and no filters. The override was the only way a subset could ever run, and it was unguarded.
10+
11+
Issue #110 reproduced the consequence against the published 0.4.0 binary. One repo, one diff (`return x * 2` becomes `return x * 3`), two test files: `test_scale.py` executes the changed line without pinning its value, `test_report.py` pins it and fails. The only variable is the command.
12+
13+
```
14+
python3 -m pytest -q -> UNSAFE test_report_formats: 'value=6' == 'value=4'
15+
python3 -m pytest -q tests/test_scale.py -> SAFE "Tests pass and the changed code is covered."
16+
```
17+
18+
That is a false SAFE on a change that provably breaks a test sitting in the same repository, reachable 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 issue #109 (ADR not yet written) does not touch this case. The two defects are independent.
19+
20+
The engine performs no automatic test selection anywhere; a grep of `src/` for impacted / affected / dependency-graph / call-graph vocabulary returns nothing. This decision is strictly about a caller-supplied command.
21+
22+
The override cannot simply be removed. Issues #95 and #98 established `PYTHONPATH=. python3 -m pytest -q` as the documented remedy for shadow bypass under editable installs, and over MCP the prefix form is the only in-band way to supply it. Any guard that misfires on those forms breaks the documented fix for a different correctness bug.
23+
24+
## Decision
25+
26+
**A test command classified as `narrowed` disqualifies SAFE and floors the verdict at UNPROVEN. A command classified as `unknown` does not.**
27+
28+
Classification is three-valued and lives in `src/verify/test-scope.ts` as a pure function over the command string. `full` means the command was parsed and names no filter. `narrowed` means a filter was positively identified: a positional path or node id, or a selection flag (`-k`, `-m`, `--last-failed`, `--ignore`, `--deselect` for pytest; `-t`, `--changed`, `--related`, `--shard`, `--project` for vitest; `-t`, `--testPathPattern`, `--onlyChanged`, `--findRelatedTests`, `--changedSince` and friends for jest). `unknown` means the command was not recognised: an unparsed wrapper (`make test`, `npm test`, `./run-tests.sh`), an unrecognised flag, or a directly executed script.
29+
30+
`unknown` deliberately does not floor, because the commands that land there are dominated by plugin flags on a full suite (`pytest --doctest-modules`), and flooring them would turn every unrecognised flag in the wild into a SAFE-killer fixable only by a pull request to us. See Alternative B for the measured version of this argument, including the two rows it leaves open. Every `unknown` carries a signal string saying why it was unrecognised, so the carve-out can be checked against field data instead of re-argued from intuition.
31+
32+
`--collect-only` and `--help` are classified as **narrowing**, not boolean. A flag that exits 0 without running the suite selects zero tests, still imports every test module, and coverage then marks module-level changed lines as executed. That combination produced a real false SAFE during review: a changed module constant read UNSAFE under `python3 -m pytest -q` and SAFE under the same command plus `--collect-only`.
33+
34+
**A leading assignment is inert only when its variable is.** `PYTHONPATH=` changes import resolution and is correctly ignored, which is what keeps the #95/#98 remedy working. `PYTEST_ADDOPTS=` is different: pytest appends its value to its own argv, so `PYTEST_ADDOPTS="-k test_scale" python3 -m pytest -q` runs one test. Discarding it unexamined answered `full`, the classifier's strongest claim, on a narrowed run: a false SAFE reached through the exact form users are told to adopt, and the only in-band way to pass environment over MCP. Values of `PYTEST_ADDOPTS`, `VITEST_ADDOPTS` and `JEST_ADDOPTS` are now scanned with the same argument scanner as the command line. Narrowing configured in the AMBIENT environment, rather than in the command string, remains out of scope and undetectable.
35+
36+
This rule is evadable in one step. An agent that wants SAFE can write `sh -c "pytest tests/x.py"` and land in `unknown`. The floor is a mistake-catcher, not an adversary-stopper: careless narrowing is written in the plain form, and that is what it catches. Nobody should read this ADR as claiming otherwise.
37+
38+
The assessment rides on `VerdictReport.testScope` as `{ scope, source, signals }`. The field is additive, so `reportVersion` stays `1`; `src/contracts.ts` is untouched. `fuseVerdict` takes it as a REQUIRED fourth parameter: an optional one would make "a new call site forgot to pass it" a silent false SAFE, which is the mechanism behind two of the three false SAFEs this project has already shipped. The FIELD stays optional, which is load-bearing for a different reason — its absence is how a consumer distinguishes a pre-0.5.0 stored report from one produced by an engine that floors.
39+
40+
Because `narrowed` moves a verdict, every unrecognised token degrades to `unknown` rather than being guessed at. A false `narrowed` costs a user their SAFE on a legitimate command, which is the failure mode this classifier must not have.
41+
42+
## Alternatives considered
43+
44+
### Alternative A: classify and disclose only, leave the verdict alone
45+
46+
Report the scope and print a CLI note, but let a narrowed run still return SAFE. This was the original scope of issue #110, with the flooring deferred to a later decision.
47+
48+
Rejected because the defect is reachable in a shipped release through an unauthenticated tool. Shipping disclosure alone would leave a known false SAFE live for another release cycle, and the project's cardinal rule is that a false SAFE is the one unforgivable defect. The classifier accuracy evidence that deferral was meant to buy is instead delivered in the same change, as the two negative test tables described under Compliance.
49+
50+
### Alternative B: floor both `narrowed` and `unknown`
51+
52+
The strongest integrity position: anything not provably whole-suite abstains.
53+
54+
Rejected, but **not** for the reason it is tempting to give. The obvious argument is "wrapper commands like `make test` are everywhere and would all lose SAFE." That argument is false, and it was measured rather than assumed. `toCoverageRunArgs` (`src/analyze/coverage/python-line-coverage.ts`) declines those commands outright, so they already land on `changedLinesCovered: 'unknown'` and therefore UNPROVEN, for an unrelated reason:
55+
56+
| testCmd | coverage plan | can reach SAFE today | classifier |
57+
| ----------------------------------- | ------------- | -------------------- | ---------- |
58+
| `make test` | declined | no | unknown |
59+
| `npm test` / `yarn test` | declined | no | unknown |
60+
| `tox` | declined | no | unknown |
61+
| `sh -c "pytest -q"` | declined | no | unknown |
62+
| `pytest --doctest-modules` | wrapped | **yes** | unknown |
63+
| `python -m unittest tests.test_foo` | wrapped | **yes** | unknown |
64+
| `python3 tests/runtests.py auth` | wrapped | **yes** | unknown |
65+
66+
Flooring `unknown` would cost the wrapper commands nothing. The real cost is row 5: `pytest --doctest-modules` and `pytest -q --forked` are whole suites that classify `unknown` only because our flag table is incomplete. Flooring `unknown` turns every pytest plugin flag in the wild into a SAFE-killer fixable only by a pull request to us. That is the actual reason to reject, and it is a good one.
67+
68+
The last two rows are the price of that rejection: they are live false SAFEs of the same shape as #110, under a different runner. They are not closed by this ADR. See Alternative C and the follow-ups.
69+
70+
### Alternative C: split `unknown` into three buckets and floor only the dangerous one
71+
72+
`unknown` is not one situation. It is three:
73+
74+
1. Unrecognised runner with no arguments (`make test`, `tox`) — cannot reach SAFE anyway, so flooring gains nothing and costs nothing.
75+
2. Recognised runner, unrecognised flag (`pytest --doctest-modules`) — a full suite in fact, SAFE reachable. Must not floor.
76+
3. Unrecognised runner **with positional arguments** (`python -m unittest tests.test_foo`, `python3 tests/runtests.py auth`) — SAFE reachable and narrowing highly likely.
77+
78+
Flooring only bucket 3 closes the unittest and Django-style holes, leaves bucket 2 untouched, and degrades with a named reason rather than a guess, which is the house doctrine everywhere else in this engine.
79+
80+
Not rejected on merit, deferred on risk. This branch already ships one change to what SAFE means, alongside the statement rule proposed in issue #109. A third, resting on a heuristic about positional arguments to an unrecognised runner, needs its own red-first evidence: a Django-shaped fixture proving the false SAFE before the fix. Tracked as a follow-up.
81+
82+
### Alternative D: per-test attribution — floor only when the changed statements were covered solely by tests the narrowing selected
83+
84+
The most precise rule: it would floor exactly the dangerous cases and nothing else.
85+
86+
Rejected as not currently buildable. It requires per-test coverage attribution, which the engine does not have; coverage.py is run once over the whole suite and reports a single merged line set. Building per-test attribution is a substantial feature with its own performance cost, and it cannot gate a fix for a live false SAFE.
87+
88+
## Consequences
89+
90+
- **Positive**: the reproduced false SAFE in #110 is closed. A caller who narrows the suite gets UNPROVEN with a reason naming the signal that cost them the verdict.
91+
- **Positive**: the scope of every run is now recorded on the report, so a stored verdict can be audited for narrowing after the fact. Previously nothing distinguished a subset SAFE from a full-suite SAFE.
92+
- **Positive**: the fail-safe direction is preserved. Every verdict this rule changes moves SAFE to UNPROVEN; no path can move UNSAFE or UNPROVEN to SAFE.
93+
- **Negative**: users who deliberately narrow their suite, for instance on a large repo where a full run is impractical, can no longer reach SAFE at all. They get UNPROVEN with an explanation. This is intended, and it is the cost of the rule.
94+
- **Negative**: a classifier false positive costs a user their SAFE. The two negative test tables exist to hold that risk down, and they must grow whenever a new flag is added.
95+
- **Neutral**: CLI exit codes do not change. SAFE and UNPROVEN both exit `0`; only UNSAFE exits `1`. No CI pipeline breaks on this change, though pipelines that parse the verdict string will see more UNPROVEN.
96+
- **Neutral**: `unknown` commands behave exactly as before.
97+
98+
## Compliance
99+
100+
- `tests/unit/verify/test-scope.test.ts` holds the classification table, including two negative tables that are the real enforcement:
101+
- **AC-3**: `PYTHONPATH=.` / `PYTHONPATH=src` / `PYTHONPATH=./src` forms from issues #95 and #98 classify as `full`. A regression here silently breaks the documented remedy for shadow bypass.
102+
- **AC-4**: `-n auto`, `-n 4`, `-n4`, `-x`, `--maxfail=1` classify as `full`. `-n` is xdist worker count, not a filter, and fail-fast flags only stop early on failure, so a run that stopped early is red and cannot reach SAFE anyway.
103+
- `tests/integration/verify-diff.test.ts` pins the #110 reproduction end to end: the full suite returns UNSAFE and the narrowed command returns UNPROVEN on the identical diff, plus an assertion that the narrowed run still measured `changedStatements {total: 1, covered: 1}`, which is what proves the issue #109 rule does not cover this case.
104+
- `tests/unit/verify/verdict-fuse.test.ts` pins the fusion rule in both directions, including that omitting the scope leaves every pre-existing verdict and reason byte-identical.
105+
- Any new flag added to a `FlagTable` in `src/verify/test-scope.ts` must arrive with a test row. The file comment says so at the top.
106+
107+
## Rollout / migration
108+
109+
- **Consumers**: a diff verified with a narrowed `testCmd` that previously read SAFE now reads UNPROVEN. The reason string names the signal and tells the reader to re-run without the filter.
110+
- **New report field**: `testScope` appears in `--json` and in the MCP tool output. Additive; consumers ignoring unknown keys are unaffected.
111+
- **Version**: minor. The project is pre-1.0, so behaviour changes land in the minor per semver's 0.x rule. Ships in 0.5.0, intended alongside the issue #109 statement rule, with both called out in the changelog under a single "what SAFE now means" heading, since shipping two independent tightenings of SAFE in one release is otherwise confusing.
112+
- **Docs**: `docs/verification/verdicts.mdx` and `docs/mcp/tool-reference.mdx` state that a narrowed `testCmd` cannot reach SAFE, and that `unknown` commands are not floored.
113+
114+
## Open questions / follow-ups
115+
116+
- [ ] Issue #109 — the independent statement-coverage rule. Decision issue #108; its ADR is not written yet, so nothing here depends on it.
117+
- [ ] **Floor bucket 3 of `unknown`** (Alternative C): an unrecognised runner carrying positional arguments. `python -m unittest tests.test_foo` and `python3 tests/runtests.py auth` are live false SAFEs of the #110 shape that this ADR does not close. Needs a Django-shaped fixture proving the false SAFE red-first before the rule changes.
118+
- [ ] **Add `engineVersion` to `VerdictReport`.** This release changes what `SAFE` means without changing the report shape, and issue #109 proposes a second such change. `reportVersion` answers "can I parse this?", not "what did SAFE mean when this was written?" A producer-version field is the general answer, and it stops the next semantic tightening from relitigating the same question.
119+
- [ ] **Measure the `unknown` rate in the field.** Every `unknown` now carries a signal string. If the population is dominated by unrecognised flags on recognised runners, the carve-out was right and the fix is to grow the flag tables. If it is dominated by unrecognised runners with arguments, bucket 3 shipped too late.
120+
- [ ] The `unknown` hole: narrowing hidden inside `make test` or a wrapper script is not caught. Revisit if evidence shows it matters in practice; the classifier already separates `unknown` from `full`, so flooring it later is a one-line change.
121+
- [ ] Narrowing configured outside the command string (`pytest.ini` `addopts`, `PYTEST_ADDOPTS`, vitest `include`, jest `testPathIgnorePatterns`) is out of scope. `full` means "this command names no filters", not "this run was the whole suite", and the docs must not overstate it.

‎docs/mcp/tool-reference.mdx‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@ The Refactron MCP server exposes exactly one tool. Every example below was captu
88

99
## Arguments
1010

11-
| Argument | Type | Required | Description |
12-
| ------------- | ------------------------ | -------- | --------------------------------------------------------------------------- |
13-
| `repoRoot` | string | yes | Absolute path to the repository root. |
14-
| `edits` | `[{ path, newContent }]` | see note | Proposed full-file contents. `path` is repo-relative. |
15-
| `unifiedDiff` | string | see note | A unified (git) diff to apply and verify. |
16-
| `testCmd` | string | no | Override the test command. Drives both the tests gate and the coverage run. |
11+
| Argument | Type | Required | Description |
12+
| ------------- | ------------------------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
13+
| `repoRoot` | string | yes | Absolute path to the repository root. |
14+
| `edits` | `[{ path, newContent }]` | see note | Proposed full-file contents. `path` is repo-relative. |
15+
| `unifiedDiff` | string | see note | A unified (git) diff to apply and verify. |
16+
| `testCmd` | string | no | Override the test command. Drives both the tests gate and the coverage run. A command that names a subset of the suite floors the verdict at `UNPROVEN`; see below. |
1717

1818
`repoRoot` is the only argument the schema marks required, but the handler needs a change to verify: supply **either** `edits` **or** `unifiedDiff`. Supplying neither returns an error result, not a verdict.
1919

20+
<Warning>
21+
Do not narrow `testCmd` to the tests you just wrote. A command naming specific test paths, or
22+
using `-k` / `-m` / `-t` / `--onlyChanged`, is classified as `narrowed` and can never return
23+
`SAFE` — the best available verdict becomes `UNPROVEN`. Coverage cannot detect this on its own:
24+
the changed code can be fully exercised by the subset you chose while the test that would have
25+
failed was never selected. The result is reported on `testScope`. A `PYTHONPATH=` prefix is not
26+
narrowing.
27+
</Warning>
28+
2029
## Request: the `edits` form
2130

2231
Use this when the agent already holds the full new contents of each file.

0 commit comments

Comments
 (0)