Skip to content

Coverage silently fails when testCmd has a leading NAME=VALUE prefix #95

Closed

Description

@omsherikar

Problem

Coverage measurement silently fails whenever testCmd carries a leading environment assignment, for example PYTHONPATH=. python3 -m pytest -q. The tests gate passes, coverage reports tool: "none", and the verdict caps at UNPROVEN. Nothing in the output attributes this to the command shape.

Same change, same suite, only the prefix differs:

testCmd verdict coverage.tool changed statements
python3 -m pytest -q SAFE coverage.py 4 of 5 covered
PYTHONPATH=. python3 -m pytest -q UNPROVEN none not determined

UNPROVEN reason: "Tests pass, but coverage of the changed code could not be determined."

Reproduced via the refactron MCP server against playground/legacy-python, with a behaviour-preserving f-string modernization of formatting.py.

Why this matters now

The PYTHONPATH= prefix is our own documented remedy for shadow bypass. docs/verification/verdicts.mdx:209 ("Make sure the tests run the code being verified") exists because a project installed into the environment, editable pip install -e . included, makes the suite import the installed copy instead of the shadow copy, so the run proves nothing about the change.

So the documented remedy for one correctness problem, written in the prefix form, silently disables the measurement the verdict depends on. The affected population is exactly the projects that need the remedy, and nothing looks broken because the tests gate stays green.

Over MCP this is the only in-band form available. src/mcp/tools/verify-change.ts:15 declares testCmd: z.string().optional() with no per-call env field, so an agent that wants the shadow tree first on sys.path has no way to express it except the prefix. Nine pages under docs/mcp/ instruct exactly that, and they are on main as of #94.

Evidence

The two execution paths disagree about what a command is.

  1. Tests gate: src/verify/runners/detect.ts:27 returns { cmd: 'sh', args: ['-c', opts.override], ... }. Shell semantics apply, the assignment is honored, tests run correctly.

  2. Coverage runner: src/analyze/coverage/python-line-coverage.ts uses no shell. toCoverageRunArgs tokenizes and classifies module-form vs script-form, and has no concept of a leading NAME=VALUE token. PYTHONPATH=. is not python3, does not end in .py, and contains no slash, so it falls through to python-line-coverage.ts:114, return ['-m', ...rest], treating the assignment as a module name.

Classifier output, produced by running the exported function against dist/analyze/coverage/python-line-coverage.js:

'python3 -m pytest -q'                   -> coverage run -m pytest -q                              correct
'PYTHONPATH=. python3 -m pytest -q'      -> coverage run -m PYTHONPATH=. python3 -m pytest -q      broken
'PYTHONPATH=src python3 -m pytest -q'    -> coverage run -m PYTHONPATH=src python3 -m pytest -q    broken
'PYTHONPATH=./src python3 -m pytest -q'  -> coverage run PYTHONPATH=./src python3 -m pytest -q     broken (script-form branch, because of the slash)
'COVERAGE_CORE=sysmon python3 -m pytest' -> coverage run -m COVERAGE_CORE=sysmon python3 -m pytest broken

coverage run cannot import a module by that name, writes no data file, measurementFailed is set, and verdict-fuse honestly degrades to UNPROVEN.

Nuance: only the inline prefix form is affected. python-line-coverage.ts:188 builds const env = { ...process.env, COVERAGE_FILE: dataFile }, so a PYTHONPATH exported into the shell before invoking refactron is inherited and works. That is why docs/verification/verdicts.mdx:215 (the export form) is correct today and the MCP prompts are not.

Acceptance criteria

  • A leading run of NAME=VALUE tokens is hoisted off the command into the child environment before classification, and the remainder classifies exactly as today. Unit tests assert the classified result for all five commands in Evidence, proven red against the current code.
  • Hoisting stops at the first token that is not an assignment. pytest -k a=b keeps a=b as an argument and hoists nothing. Test.
  • Hoisted vars are merged into the inherited env, not substituted for it: COVERAGE_FILE is still set to the temp data file and inherited PATH survives. Test.
  • End to end: a verify-diff run over a committed fixture with testCmd PYTHONPATH=. python3 -m pytest -q reports coverage.tool "coverage.py" and reaches the same verdict as the unprefixed form. Red before the fix, where it reports tool: "none" and UNPROVEN.
  • The coverage run and the tests gate execute with the same effective environment for the same testCmd. If they diverge, the measured run is not the verified run. State how this is enforced, not just that it holds.
  • Relative values such as PYTHONPATH=. resolve against the shadow root. Proof: the changed file appears in report.measuredFiles, so the shadow-bypass guard in src/verify/verify-diff.ts does not fire.
  • A command containing real shell metacharacters still returns null and reports unknown. Existing SHELL_COMPOSITE_RE tests stay green, unmodified.

Use a committed fixture for the end-to-end test. playground/ is untracked, so a CI test cannot reach it. tests/fixtures/verify-diff-mini and tests/fixtures/coverage-mini are the candidates; playground/legacy-python stays as the manual repro only.

Verdict integrity

The existing bug fails safe. measurementFailed routes through unknownCoverage() to UNPROVEN and cannot produce a false SAFE. This is a capability defect, not a trust defect, and the cardinal rule holds today.

The fix moves in the dangerous direction: it converts UNPROVEN into SAFE for a class of commands that previously could not reach SAFE at all. That is the risk this issue must discharge.

  • SAFE is produced only when coverage was genuinely measured. The end-to-end test asserts coverage.tool === 'coverage.py' and a non-empty measuredFiles containing the changed file, not merely the verdict string. A test that only checks verdict === 'SAFE' does not discharge this.
  • Adversarial case: a hoisted variable that points the suite at code outside the shadow tree, for example an absolute PYTHONPATH=<original repoRoot>, must not read SAFE. Expected: the changed file is never measured under the shadow-relative path, the shadow-bypass guard fires, and the verdict is UNPROVEN. If it reads SAFE, stop and escalate to principal-engineer before continuing. That is a false SAFE introduced by this fix, which is the exact shape of the two regressions rounds 3 and 4 shipped.
  • src/verify/verdict-fuse.ts is untouched. This change is confined to coverage acquisition.

Blast radius

  • VerdictReport is unchanged: no new, renamed, retyped, or removed fields. Not a public-contract change, no reportVersion bump.
  • Locked contracts untouched: src/contracts.ts, src/core/models.ts, src/adapters/interface.ts.
  • toCoverageRunArgs is exported from src/analyze/coverage/python-line-coverage.ts but is not re-exported from src/index.ts, and package.json exports publishes . only. A signature change, returning args plus env, is therefore internal. One existing consumer to update: tests/integration/coverage-reporter.test.ts:192.
  • reportCoverage has a second caller, src/analyze/engine.ts:99, which passes no testCmd and defaults to 'pytest -q'. It must remain unaffected; confirm rather than assume.

Non-goals

  • Shell emulation. Only a leading run of assignments is handled. Commands with real metacharacters remain declined by SHELL_COMPOSITE_RE and correctly report unknown.
  • Quoting or escaping beyond what tokenizeCommand already does.
  • Any documentation change. Tracked separately and blocked on this one.
  • Adding an env field to the MCP verify_change input schema. If that turns out to be the better shape, it is a public-contract decision for principal-engineer and gets its own issue.
  • Making TypeScript or mixed-language diffs measurable.

Tests to write first

Unit, on the classifier, all five commands from Evidence, plus the pytest -k a=b non-hoist case and the env-merge case. Then the end-to-end verify-diff assertion. Each must be shown failing against main before implementation. test-engineer owns the red-first proof.

Activity

  1. added
    bug
    on Aug 4, 2026
  2. omsherikar commented on Aug 4, 2026

    @omsherikar
    ContributorAuthor

    Docs reconciliation split out to #96, which is blocked on this one: the correct wording differs depending on whether this lands or is declined.

    Note that 11 of the 13 doc mentions already recommend the prefix form, so they become correct the moment this merges. #96 covers the two that would remain inconsistent.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions