From e3ac9ba523ed36cba797dacbe0e7c1f9078fe7e0 Mon Sep 17 00:00:00 2001 From: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com> Date: Sun, 22 Nov 2026 17:36:19 +0000 Subject: [PATCH 1/2] loopguard: read a test run spelled the Windows way On Windows a runner is named with backslashes, a drive letter and `.exe`: `C:\repo\.venv\Scripts\python.exe -m pytest`, `.venv\Scripts\pytest.exe`, `python.exe -m pytest`, as Claude Code's PowerShell tool records it. The micro-skills observer reads these as test runs (`test_a_windows_interpreter_path_is_the_interpreter`), but the loop guard's verification pattern did not, so a failing one after the last edit left the turn counted as ended green. Each command is now matched as it reads with forward slashes, no drive letter and no `.exe` (`_posix_spelling`); the patterns themselves are unchanged. `python.exe repro.py`, `.venv\Scripts\python.exe -m pip install x` and `pytest.exe --collect-only` are still not test runs. Over the 5,107 strings in the test suites, only Windows-spelled runs read differently. --- tests/test_loopguard_windows_runs.py | 40 ++++++++++++++++++++++++++++ tokeneconomizer/loopguard/turn.py | 14 +++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/test_loopguard_windows_runs.py diff --git a/tests/test_loopguard_windows_runs.py b/tests/test_loopguard_windows_runs.py new file mode 100644 index 0000000..3718ecc --- /dev/null +++ b/tests/test_loopguard_windows_runs.py @@ -0,0 +1,40 @@ +"""A test run spelled the Windows way is a verification. + +On Windows a runner is named with backslashes, a drive letter and `.exe` +(`C:\\repo\\.venv\\Scripts\\python.exe -m pytest`, `.venv\\Scripts\\pytest.exe`), as Claude Code's +PowerShell tool and Codex record it. The micro-skills observer already reads these as test runs; +the loop guard did not, so a failing one after the last edit left the turn counted as ended +green.""" +import pytest + +from tokeneconomizer import loopguard as guard +from tokeneconomizer.loopguard import turn +from tokeneconomizer.microskills import observe + +RUNS = [r"C:\repo\.venv\Scripts\python.exe -m pytest tests -q", + r".venv\Scripts\python.exe -m pytest", ".venv/Scripts/python.exe -m pytest", + r".venv\Scripts\pytest.exe -q", "D:/work/.venv/Scripts/pytest.exe", "python.exe -m pytest", + r"C:\Python312\python.exe -m unittest discover", r".venv\Scripts\python.exe manage.py test", + r"cd C:\repo && .venv\Scripts\python.exe -m pytest"] + + +@pytest.mark.parametrize("cmd", RUNS) +def test_a_windows_test_run_counts_as_a_verification(cmd): + assert turn.status_of_tests(cmd) == "own" + g = guard.TurnGuard(turn_key="t") + g.fold("Edit", {"file_path": r"C:\repo\a.py", "old_string": "x", "new_string": "y"}, "ok") + g.fold("PowerShell", {"command": cmd}, "FAILED tests/test_a.py::test_x - AssertionError", 1) + assert g.verifications == 1 and not g.resolved_proxy() + + +@pytest.mark.parametrize("cmd", RUNS[:7]) +def test_the_observer_and_the_loop_guard_agree(cmd): + assert "run_tests" in [o.intent for o in observe.classify_command(cmd)] + assert turn.status_of_tests(cmd) == "own" + + +@pytest.mark.parametrize("cmd", [r"python.exe repro.py", r".venv\Scripts\python.exe -m pip install x", + r"C:\tools\pytest-runner.exe", r"notepad.exe tests\test_a.py", + r".venv\Scripts\pytest.exe --collect-only"]) +def test_a_windows_command_that_runs_no_tests_is_not_one(cmd): + assert turn.status_of_tests(cmd) is None diff --git a/tokeneconomizer/loopguard/turn.py b/tokeneconomizer/loopguard/turn.py index 670c8fa..d04379e 100644 --- a/tokeneconomizer/loopguard/turn.py +++ b/tokeneconomizer/loopguard/turn.py @@ -219,6 +219,17 @@ def exit_status_covers(cmd): return True +# A command spelled the Windows way (`C:\repo\.venv\Scripts\python.exe -m pytest`, +# `.venv\Scripts\pytest.exe`) is read with forward slashes, no drive letter and no `.exe`, as the +# micro-skills observer reads it. +_DRIVE_RE = re.compile(r"(? Date: Thu, 26 Nov 2026 17:39:53 +0000 Subject: [PATCH 2/2] loopguard: read the Windows py launcher as python in a test run `py -m pytest`, `py -3.12 -m pytest` and `py -V:3.12 -m pytest` run the tests through the Windows `py` launcher, which starts the default Python or the one a `-V:` or `-3.x` option chooses (CPython's "Using Python on Windows"). The loop guard read none of them as a test run. The launcher, by name or path and with its version option, is now read as `python`. `py repro.py`, `py -m pip install pytest`, `py -3.12 --version` and `pyright` are still not test runs, and `py.test` is pytest's own name as before. --- tests/test_loopguard_windows_runs.py | 16 ++++++++++++++-- tokeneconomizer/loopguard/turn.py | 13 +++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/tests/test_loopguard_windows_runs.py b/tests/test_loopguard_windows_runs.py index 3718ecc..71d0abb 100644 --- a/tests/test_loopguard_windows_runs.py +++ b/tests/test_loopguard_windows_runs.py @@ -4,7 +4,9 @@ (`C:\\repo\\.venv\\Scripts\\python.exe -m pytest`, `.venv\\Scripts\\pytest.exe`), as Claude Code's PowerShell tool and Codex record it. The micro-skills observer already reads these as test runs; the loop guard did not, so a failing one after the last edit left the turn counted as ended -green.""" +green. The same holds for the Windows `py` launcher, which runs the default Python (`py -m +pytest`) or the one a `-V:` or `-3.x` option chooses ("Using Python on Windows", CPython's +documentation).""" import pytest from tokeneconomizer import loopguard as guard @@ -27,6 +29,15 @@ def test_a_windows_test_run_counts_as_a_verification(cmd): assert g.verifications == 1 and not g.resolved_proxy() +PY_RUNS = ["py -m pytest", "py -3.12 -m pytest tests", "py -V:3.12 -m pytest -q", + "py -3 -m unittest discover", "py.exe -m pytest", r"C:\Windows\py.exe -m pytest"] + + +@pytest.mark.parametrize("cmd", PY_RUNS) +def test_a_test_run_through_the_py_launcher_counts_as_a_verification(cmd): + assert turn.status_of_tests(cmd) == "own" + + @pytest.mark.parametrize("cmd", RUNS[:7]) def test_the_observer_and_the_loop_guard_agree(cmd): assert "run_tests" in [o.intent for o in observe.classify_command(cmd)] @@ -35,6 +46,7 @@ def test_the_observer_and_the_loop_guard_agree(cmd): @pytest.mark.parametrize("cmd", [r"python.exe repro.py", r".venv\Scripts\python.exe -m pip install x", r"C:\tools\pytest-runner.exe", r"notepad.exe tests\test_a.py", - r".venv\Scripts\pytest.exe --collect-only"]) + r".venv\Scripts\pytest.exe --collect-only", "py repro.py", + "py -m pip install pytest", "pyright src", "py -3.12 --version"]) def test_a_windows_command_that_runs_no_tests_is_not_one(cmd): assert turn.status_of_tests(cmd) is None diff --git a/tokeneconomizer/loopguard/turn.py b/tokeneconomizer/loopguard/turn.py index d04379e..530f577 100644 --- a/tokeneconomizer/loopguard/turn.py +++ b/tokeneconomizer/loopguard/turn.py @@ -226,10 +226,20 @@ def exit_status_covers(cmd): _EXE_RE = re.compile(r"\.exe(?=\s|$)", re.I) +# The Windows `py` launcher runs the default Python, or the one a `-V:` or `-3.x` option +# chooses (`py -3.12 -m pytest`): it is read as `python`. +_PY_LAUNCHER_RE = re.compile(r"^(\s*)(?:\S*/)?py(?:\s+-(?:V:\S+|3\S*))?(?=\s)") + + def _posix_spelling(stage): return _EXE_RE.sub("", _DRIVE_RE.sub("/", stage.replace("\\", "/"))) +def _as_run(stage): + """The command a stage runs, past its prefix and in its POSIX spelling.""" + return _PY_LAUNCHER_RE.sub(r"\1python", _PREFIX_RE.sub("", _posix_spelling(stage))) + + def status_of_tests(cmd): """What `cmd`'s exit status says of the test runs in it: None when it runs none; "own" when it is theirs (a run is last, or followed only by `&&`, or piped under `pipefail`); "hidden" @@ -242,8 +252,7 @@ def status_of_tests(cmd): found = None for i in range(0, len(pieces), 2): stage = pieces[i] - if (not VERIFY_RE.match(_PREFIX_RE.sub("", _posix_spelling(stage))) - or _NOT_A_RUN_RE.search(stage)): + if not VERIFY_RE.match(_as_run(stage)) or _NOT_A_RUN_RE.search(stage): continue status = "own" for sep, then in zip(pieces[i + 1::2], pieces[i + 2::2]):