From 5896998e4237adc21a01221052e5fdb7e4e0972d Mon Sep 17 00:00:00 2001 From: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:47:32 +0000 Subject: [PATCH] loopguard: count a test run in a conda environment as a verification `conda run -n ml pytest`, `mamba run -p ./env python -m pytest` and `micromamba run -n ml pytest` run the tests in a named or prefixed environment, as `uv run` and `poetry run` do in theirs. The prefix pattern knew those launchers but not conda's, so the run was not a verification, and a failing run after the last edit left the turn counted as ended green. The micro-skills observer already learns and injects the `conda run -n ` form, so an agent following that fact ran a command the loop guard did not read. conda, mamba and micromamba `run` are now passed over with their options; `-n`, `-p`, `-r`, `--name`, `--prefix`, `--root-prefix` and `--cwd` take a value. `conda run -n ml python train.py` and `conda install pytest` are still not test runs. --- tests/test_loopguard_conda_run.py | 34 +++++++++++++++++++++++++++++++ tokeneconomizer/loopguard/turn.py | 4 ++++ 2 files changed, 38 insertions(+) create mode 100644 tests/test_loopguard_conda_run.py diff --git a/tests/test_loopguard_conda_run.py b/tests/test_loopguard_conda_run.py new file mode 100644 index 0000000..6c3ebbc --- /dev/null +++ b/tests/test_loopguard_conda_run.py @@ -0,0 +1,34 @@ +"""A test run started in a conda environment is a verification. + +`conda run -n ` (and `mamba run`, `micromamba run`) runs a command in a named or prefixed +environment, as `uv run` and `poetry run` do in theirs. The micro-skills observer already learns +and injects that form (`conda run -n pytest`), so a run the agent types as told must count +as the turn's verification too.""" +import pytest + +from tokeneconomizer import loopguard as guard +from tokeneconomizer.loopguard import turn + + +@pytest.mark.parametrize("cmd", ["conda run -n ml pytest -q", + "conda run -n ml python -m pytest tests", + "conda run --no-capture-output -n ml python -m pytest", + "conda run --name ml --live-stream pytest", + "conda run -p ./env pytest", "conda run --prefix=/opt/env pytest", + "conda run --cwd src -n ml python -m unittest", + "mamba run -n ml pytest", "micromamba run -n ml pytest", + "micromamba run -r /opt/mamba -n ml pytest", + "conda run pytest"]) +def test_a_runner_in_a_conda_environment_counts_as_a_verification(cmd): + assert turn.status_of_tests(cmd) == "own" + g = guard.TurnGuard(turn_key="t") + g.fold("Edit", {"file_path": "/r/a.py", "old_string": "x", "new_string": "y"}, "ok") + g.fold("Bash", {"command": cmd}, "FAILED (failures=1)", 1) + assert g.verifications == 1 and not g.resolved_proxy() + + +@pytest.mark.parametrize("cmd", ["conda run -n ml python train.py", "conda install -n ml pytest", + "conda run -n ml pytest --collect-only", + "micromamba create -n ml pytest"]) +def test_conda_leaves_what_it_runs_to_decide(cmd): + assert turn.status_of_tests(cmd) is None diff --git a/tokeneconomizer/loopguard/turn.py b/tokeneconomizer/loopguard/turn.py index 5495c89..f54f620 100644 --- a/tokeneconomizer/loopguard/turn.py +++ b/tokeneconomizer/loopguard/turn.py @@ -75,6 +75,10 @@ # env's options; `-u`, `-C` and their long forms take a value r"env(?:\s+(?:(?:-[uC]|--unset|--chdir)(?:=|\s*)[^\s-]\S*|-[-\w]*))*|" r"timeout(?:\s+(?:-\S+|[A-Z]+|\d\S*))+|" + # `conda run -n ml`, `mamba run -p ./env`, `micromamba run -r /opt -n ml`: + # the environment's name or path, and the other options that take a value + r"(?:conda|mamba|micromamba)\s+run(?:\s+(?:(?:-[npr]|--name|--prefix|" + r"--root-prefix|--cwd)(?:=|\s*)[^\s-]\S*|-[-\w]*))*|" r"(?:(?:uv|poetry|pipenv|pdm|hatch|rye)\s+run|npx|bunx|" r"(?:npm|pnpm|yarn)\s+(?:exec|dlx)|bundle\s+exec|xvfb-run)" r"(?:\s+-\S+)*|"