diff --git a/tests/test_live_core.py b/tests/test_live_core.py index 4fc5de73..36d0df2f 100644 --- a/tests/test_live_core.py +++ b/tests/test_live_core.py @@ -699,6 +699,19 @@ def test_the_opencode_side_call_adds_only_the_flags_its_cli_lists(monkeypatch): ("git -c core.autocrlf=false --no-pager apply fix.diff", ["(patch)"]), ("git -C repo checkout HEAD -- pkg/mod.py", ["mod.py"]), ("git --git-dir=.git --work-tree . restore pkg/mod.py", ["mod.py"]), + # a wrapper's own options and values before the command (`sudo -u bob`, `env -u VAR`) + ("env -u CURSOR_AGENT sed -i 's/a/b/' pkg/mod.py", ["mod.py"]), + ("sudo -u bob sed -i 's/a/b/' pkg/mod.py", ["mod.py"]), + ("sudo -Eu bob -- perl -pi -e 's/a/b/' src/a.rs", ["a.rs"]), + ("timeout 60 sed -i 's/a/b/' pkg/mod.py", ["mod.py"]), + ("timeout -k 5 --signal=KILL 1m git apply fix.diff", ["(patch)"]), + ("nice -n 10 sed -i 's/a/b/' x.py", ["x.py"]), + ("nice -5 tee -a setup.cfg", ["setup.cfg"]), + ("time -p sed -i 's/a/b/' x.py", ["x.py"]), + ("FOO=1 env -C sub -u X BAR=2 nice -n5 sed -i 's/a/b/' x.py", ["x.py"]), + ("sudo -E git checkout -- pkg/mod.py", ["mod.py"]), + ("env -u X sed -n '1,20p' pkg/mod.py", []), + ("timeout 60 python -m pytest -q > /tmp/out.txt", []), # not edits: output captures, scratch, reads, and operators inside quotes ("python -m pytest -q > /tmp/out.txt 2>&1; tail -5 /tmp/out.txt", []), ("grep -n 'x' a.py > hits.txt", []), @@ -734,6 +747,20 @@ def test_a_git_command_run_with_dash_c_still_counts_its_edit_and_its_revert(): assert g.whole.edit_counts == {"(patch)": 1, "mod.py": 2} +def test_an_edit_run_through_a_wrapper_with_options_still_counts(): + """`env -u VAR`, `sudo -u bob`, `timeout 60` and `nice -n 10` run the command after their + own options. Dropping only the wrapper's name left `-u` or `60` as the command, so the + `sed -i` was no edit and the `git checkout --` that put the file back was no revert.""" + revert = "sudo -u bob git checkout -- pkg/mod.py" + assert shell_edits.shell_revert_targets(revert) == ["mod.py"] + g = guard.TurnGuard(turn_key="t") + g.fold("Bash", {"command": "env -u CURSOR_AGENT sed -i 's/a/b/' pkg/mod.py"}, "", 0) + g.fold("Bash", {"command": "timeout 60 sed -i 's/b/c/' pkg/mod.py"}, "", 0) + g.fold("Bash", {"command": revert}, "", 0) + assert g.edits == 3 + assert g.whole.edit_counts == {"mod.py": 3} + + def test_an_edit_after_a_here_string_is_counted(): """`<<<` feeds one word to a command; reading it as a heredoc tag made every later line of the command a heredoc body, so a `sed -i` after it was not counted as an edit.""" diff --git a/tokeneconomizer/loopguard/shell_edits.py b/tokeneconomizer/loopguard/shell_edits.py index 23a4746c..34676d9c 100644 --- a/tokeneconomizer/loopguard/shell_edits.py +++ b/tokeneconomizer/loopguard/shell_edits.py @@ -204,13 +204,57 @@ def _restored(tokens): return [t for t in rest if not t.startswith("-")] +# The commands that run another one, and which of their own options take the next word as a +# value: `sudo -u bob sed -i ...`, `env -u VAR ...`, `timeout -s KILL 60 ...`, `nice -n 10 ...`. +_WRAPPERS = { + "sudo": {"-u", "-g", "-p", "-C", "-D", "-R", "-r", "-t", "-T", "-U", "--user", "--group", + "--prompt", "--close-from", "--chdir", "--chroot", "--role", "--type", + "--command-timeout", "--other-user"}, + "env": {"-u", "-C", "-S", "--unset", "--chdir", "--split-string"}, + "timeout": {"-s", "-k", "--signal", "--kill-after"}, + "nice": {"-n", "--adjustment"}, + "time": {"-f", "-o", "--format", "--output"}, + "command": set(), + "nohup": set(), +} + + +def _past_wrapper(tokens): + """The words after a wrapper such as `sudo` or `timeout`, its own options and, for + `timeout`, the duration.""" + takes_value = _WRAPPERS[tokens[0]] + i = 1 + while i < len(tokens) and tokens[i].startswith("-"): + t = tokens[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 tokens[0] == "timeout": + i += 1 + return tokens[i:] + + def _bare(cmds): - """Each command with the `sudo`, `env`, `VAR=1` ... before it dropped; empty ones left out.""" + """Each command with the `sudo -u bob`, `env -u VAR`, `timeout 60`, `VAR=1` ... before it + dropped; empty ones left out.""" out = [] for tokens in cmds: - while tokens and (tokens[0] in ("sudo", "command", "env", "time", "nohup") - or re.match(r"^\w+=", tokens[0])): - tokens = tokens[1:] + while tokens: + if tokens[0] in _WRAPPERS: + tokens = _past_wrapper(tokens) + elif re.match(r"^\w+=", tokens[0]): + tokens = tokens[1:] + else: + break if tokens: out.append(tokens) return out