diff --git a/tests/test_envfacts.py b/tests/test_envfacts.py index 3ac1b9c..d7ba483 100644 --- a/tests/test_envfacts.py +++ b/tests/test_envfacts.py @@ -311,6 +311,44 @@ def test_env_assignments_after_env_are_part_of_how_a_command_was_typed(): assert o.env is False +def test_a_wrappers_own_options_are_not_the_command(): + for cmd in ("sudo -u bob pytest -q", "sudo -Eu bob pytest", "nice -n 10 pytest", + "nice -n5 pytest", "nice -10 pytest", "timeout -s KILL 60 pytest", + "timeout --kill-after 5 2m pytest", "time -o t.txt pytest", "exec -a x pytest", + "sudo -- pytest"): + (o,) = observe.classify_command(cmd) + assert (o.intent, o.form, o.env) == ("run_tests", "pytest", False), cmd + for cmd in ("env -u CURSOR_AGENT python -m pytest -q", "env -i PATH=/bin python -m pytest", + "env -C sub python -m pytest", "sudo env --unset=X timeout 60 python -m pytest"): + (o,) = observe.classify_command(cmd) + assert (o.intent, o.form, o.env) == ("run_tests", "python -m pytest", True), cmd + + +def test_a_form_typed_behind_a_wrapper_with_options_is_evidence(tmp_path): + """`nice -n 10 python -m pytest` is the same repair as `python -m pytest`; one that only + worked with `env -u VAR` in front is not named, as with `VAR=value` (see above).""" + def niced(tmp_path, i): + return _task(tmp_path, i, [ + _edit(f"pkg/n{i}.py"), + _cmd("pytest -q", "bash: pytest: command not found", 127), + _cmd("nice -n 10 python -m pytest -q", "4 passed in 0.01s", 0)]) + d = _stream(tmp_path, 4, niced) + f = d.facts["form_repair:run_tests:pytest"] + assert f.params["b_counts"] == {"python -m pytest": 4} + assert "`python -m pytest …` works" in d.render() + + def unset(tmp_path, i): + return _task(tmp_path, i, [ + _edit(f"pkg/u{i}.py"), + _cmd("pytest -q", "bash: pytest: command not found", 127), + _cmd(f"env -u PYTHONHOME {PROJ} -m pytest -q", "4 passed in 0.01s", 0)]) + d = _stream(tmp_path, 3, _clean) + for i in range(3, 6): + d.render() + d.update(unset(tmp_path, i), iid=f"u-{i}", resolved=True) + assert PROJ + " -m pytest" not in d.facts["form_repair:run_tests:pytest"].params["b_counts"] + + # ---- persistence, other transcript schemas, stage wiring --------------------------------- def test_save_load_roundtrip(tmp_path): d = _stream(tmp_path, 4, _broken_env_task) diff --git a/tokeneconomizer/microskills/observe.py b/tokeneconomizer/microskills/observe.py index f916330..e25ded5 100644 --- a/tokeneconomizer/microskills/observe.py +++ b/tokeneconomizer/microskills/observe.py @@ -98,7 +98,24 @@ class Vocabulary: GIT_FETCH_SUBS = frozenset({"fetch", "clone", "pull", "ls-remote"}) GIT_PROBE_SUBS = frozenset({"remote", "branch", "tag", "fsck", "stash", "show-ref", "for-each-ref"}) -PREFIX_HEADS = ("sudo", "env", "time", "exec", "nice", "nohup", "timeout") +# Wrappers a command can be typed behind, each with its options that take a value: `sudo -u bob +# pytest` and `nice -n 10 pytest` are pytest runs, not a command named `-u` or `-n`. +# loopguard/shell_edits.py keeps the same table for the edits it reads (`_WRAPPERS`). +PREFIX_HEADS = { + "sudo": frozenset({"-u", "-g", "-p", "-C", "-D", "-R", "-r", "-t", "-T", "-U", "--user", + "--group", "--prompt", "--close-from", "--chdir", "--chroot", "--role", + "--type", "--command-timeout", "--other-user"}), + "env": frozenset({"-u", "-C", "-S", "--unset", "--chdir", "--split-string"}), + "time": frozenset({"-f", "-o", "--format", "--output"}), + "exec": frozenset({"-a"}), + "nice": frozenset({"-n", "--adjustment"}), + "nohup": frozenset(), + "timeout": frozenset({"-s", "-k", "--signal", "--kill-after"}), +} +# `env` options that change the environment or the directory the command runs in, which the +# command's form leaves out, as it leaves out a `VAR=value`. +_ENV_CHANGES_RE = re.compile(r"^(?:-$|-[a-zA-Z0-9]*[iuCS]|" + r"--(?:ignore-environment|unset|chdir|split-string)\b)") _PASSTHROUGH = frozenset({"cd", "echo", "head", "tail", "cat", "sort", "uniq", "tee", "true", "printf", "less", "more"}) @@ -360,7 +377,11 @@ def classify_command(cmd, call_index=0, vocab=None, cwd="", root=None, top=None) if _ASSIGN_TOKEN_RE.match(toks[0]): # `env VAR=value cmd` assigned, toks = True, toks[1:] else: - toks = toks[1:] if toks[0] != "timeout" else toks[2:] + rest = _past_prefix(toks) + if toks[0] == "env" and any(_ENV_CHANGES_RE.match(t) + for t in toks[1:len(toks) - len(rest)]): + assigned = True # `env -u VAR cmd`, `env -i cmd` + toks = rest if not toks: continue head = toks[0] @@ -373,6 +394,30 @@ def classify_command(cmd, call_index=0, vocab=None, cwd="", root=None, top=None) return obs +def _past_prefix(toks): + """The words after a wrapper such as `sudo` or `timeout`: past its own options (`-u bob`, + `-Eu bob`, `-n10`, `--signal KILL`, `--`) and, for `timeout`, the duration.""" + takes_value = PREFIX_HEADS[toks[0]] + i = 1 + while i < len(toks) and toks[i].startswith("-"): + t = toks[i] + i += 1 + if t == "--": + break + if t.startswith("--"): + if t in takes_value: # `--user bob`, not `--user=bob` + i += 1 + continue + for n, letter in enumerate(t[1:], 2): # `-Eu bob`, `-ubob`, `-n10`, `-10` + if "-" + letter in takes_value: + if n == len(t): # the value is the next word + i += 1 + break + if toks[0] == "timeout": + i += 1 + return toks[i:] + + def _runner_script(rest, vocab): for t in rest: if t.startswith("-"):