Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(distill): run a wrapped command in the shell it was written for (#2448)
Raghav Chamadiyab95acd47b8ac
diff --git a/packages/cli/src/repowise/cli/agent_adapters/base.py b/packages/cli/src/repowise/cli/agent_adapters/base.pyindex e9edd3cd1..604749c18 100644--- a/packages/cli/src/repowise/cli/agent_adapters/base.py+++ b/packages/cli/src/repowise/cli/agent_adapters/base.py@@ -14,6 +14,50 @@ from typing import TYPE_CHECKING, ClassVarif TYPE_CHECKING:from pathlib import Path+#: The shell dialects a :class:`RewriteRequest` can name. Constants rather+#: than literals because the value is load-bearing twice over, across two+#: processes: the rewrite hook decides which bailouts apply from it, and the+#: ``repowise distill`` it writes decides which interpreter actually executes+#: the command from it. A misspelling would not fail loudly -- it+#: would read as "not powershell" and run the command in the wrong shell.+SHELL_POSIX = "posix"+SHELL_POWERSHELL = "powershell"++#: Savings-ledger ``--source`` labels the rewrite hook derives from the shell+#: dialect, rather than taking from an adapter's ``savings_source``.+#:+#: These two and the two dialects above are one vocabulary with two spellings,+#: and the pair of functions below is the only place they are tied together.+#: That matters more than it looks: ``_decide`` writes the label from the+#: dialect and ``repowise distill`` reads the dialect back out of the label to+#: choose an interpreter, so a rename that touched only one side would not+#: fail — it would run a command in the wrong shell and report success.+HOOK_SOURCE_POSIX = "hook-bash"+HOOK_SOURCE_POWERSHELL = "hook-powershell"+++def hook_source_for(shell: str) -> str:+ """The ledger label for a command the hook rewrote in *shell*."""+ return HOOK_SOURCE_POWERSHELL if shell == SHELL_POWERSHELL else HOOK_SOURCE_POSIX+++def dialect_for_hook_source(source: str) -> str | None:+ """The dialect *source* names, or None when it names none.++ The inverse of :func:`hook_source_for`, and deliberately narrow. An+ adapter that sets its own ``savings_source`` is absent on purpose:+ ``hook-codex`` says nothing about a dialect, because Codex picks one from+ the host (``codex.py``), and it needs no answer here either way — on+ Windows it reports PowerShell, and everywhere else the host shell is+ already POSIX. None means "no opinion", which every caller reads as the+ host default, so an unknown label can never select an interpreter.+ """+ if source == HOOK_SOURCE_POSIX:+ return SHELL_POSIX+ if source == HOOK_SOURCE_POWERSHELL:+ return SHELL_POWERSHELL+ return None+class RewriteRequest:"""Agent-agnostic view of one shell command an agent is about to run."""diff --git a/packages/cli/src/repowise/cli/agent_adapters/claude_code.py b/packages/cli/src/repowise/cli/agent_adapters/claude_code.pyindex c775fdf96..5c75e88ea 100644--- a/packages/cli/src/repowise/cli/agent_adapters/claude_code.py+++ b/packages/cli/src/repowise/cli/agent_adapters/claude_code.py@@ -15,7 +15,13 @@ import osimport os.pathfrom typing import TYPE_CHECKING, ClassVar-from repowise.cli.agent_adapters.base import AgentAdapter, RewriteRequest, RewriteResult+from repowise.cli.agent_adapters.base import (+ SHELL_POSIX,+ SHELL_POWERSHELL,+ AgentAdapter,+ RewriteRequest,+ RewriteResult,+)if TYPE_CHECKING:from pathlib import Path@@ -91,7 +97,7 @@ class ClaudeCodeAdapter(AgentAdapter):return RewriteRequest(command=command,cwd=cwd if isinstance(cwd, str) else "",- shell="powershell" if tool_name == "PowerShell" else "posix",+ shell=SHELL_POWERSHELL if tool_name == "PowerShell" else SHELL_POSIX,session_id=session_id if isinstance(session_id, str) else "",)diff --git a/packages/cli/src/repowise/cli/agent_adapters/codex.py b/packages/cli/src/repowise/cli/agent_adapters/codex.pyindex b14b23abf..732107dd3 100644--- a/packages/cli/src/repowise/cli/agent_adapters/codex.py+++ b/packages/cli/src/repowise/cli/agent_adapters/codex.py@@ -27,7 +27,13 @@ import jsonimport osfrom typing import TYPE_CHECKING, ClassVar-from repowise.cli.agent_adapters.base import AgentAdapter, RewriteRequest, RewriteResult+from repowise.cli.agent_adapters.base import (+ SHELL_POSIX,+ SHELL_POWERSHELL,+ AgentAdapter,+ RewriteRequest,+ RewriteResult,+)if TYPE_CHECKING:from pathlib import Path@@ -127,7 +133,7 @@ class CodexAdapter(AgentAdapter):# `decide`'s PowerShell-alias bailout, which exists precisely to# stop an alias command being re-run through cmd.exe. Dead code# until the gate above started matching; live now.- shell="powershell" if os.name == "nt" else "posix",+ shell=SHELL_POWERSHELL if os.name == "nt" else SHELL_POSIX,)def render_response(self, result: RewriteResult) -> str:diff --git a/packages/cli/src/repowise/cli/commands/distill_cmd.py b/packages/cli/src/repowise/cli/commands/distill_cmd.pyindex e1bba1422..1157a2227 100644--- a/packages/cli/src/repowise/cli/commands/distill_cmd.py+++ b/packages/cli/src/repowise/cli/commands/distill_cmd.py@@ -5,6 +5,19 @@ errors-first rendering with an omission marker pointing at the stashed rawoutput (``repowise expand <ref>`` round-trips it). The wrapped command'sexit code is always preserved, so this is a drop-in replacement in scriptsand agent tool calls alike.++The wrapped command runs in the shell it was *written* for, not the one this+host happens to default to. ``--source hook-bash`` names a POSIX command+line, so on Windows a command carrying POSIX syntax of its own executes+through the agent's Git Bash rather than ``cmd.exe``, where quoting means+something else and ``&&`` binds differently. That is the constraint the+rewrite hook used to defend by declining to rewrite anything with a+metacharacter on Windows.++The dialect rides on ``--source`` rather than a flag of its own so that an+older ``repowise distill`` on PATH still understands every command a newer+hook writes. A flag it did not know would land at the front of the wrapped+command and be run as the program."""from __future__ import annotations@@ -17,6 +30,7 @@ from pathlib import Pathimport click+from repowise.cli.agent_adapters.base import SHELL_POSIX, dialect_for_hook_sourcefrom repowise.cli.helpers import find_repowise_repo_root@@ -31,7 +45,12 @@ from repowise.cli.helpers import find_repowise_repo_root"--source",default="cli",hidden=True,- help="Ledger surface label (the rewrite hook tags hook-bash / hook-powershell).",+ help=(+ "Ledger surface label (the rewrite hook tags hook-bash / "+ "hook-powershell). Load-bearing beyond the ledger: 'hook-bash' names a "+ "POSIX command line, so on Windows a command carrying POSIX shell "+ "syntax is run by a POSIX shell rather than cmd.exe."+ ),)@click.argument("command", nargs=-1, required=True, type=click.UNPROCESSED)def distill_command(source: str, command: tuple[str, ...]) -> None:@@ -51,17 +70,43 @@ def distill_command(source: str, command: tuple[str, ...]) -> None:restore it with ``repowise expand <ref>``. On any filter problem the rawoutput is printed unchanged. The command's exit code is preserved."""+ # Only a command handed over as ONE token carries shell syntax of its+ # own — that is the shape the hook wraps a chain or a pipeline in, and the+ # shape a user quotes deliberately. A multi-token argv is the same command+ # in either dialect, and rendering it for this host is what has always+ # worked, so the POSIX shell — and the refusal when there is none — stays+ # confined to exactly the commands that need it.+ needs_posix_shell = (+ dialect_for_hook_source(source) == SHELL_POSIX+ and sys.platform == "win32"+ and len(command) == 1+ )try:command_str = _render_command(command)except UnrenderableCommandError as exc:# Refusing is the safe half of the trade: running a command the user# did not type is worse than not running one they did.raise click.ClickException(str(exc)) from exc- # shell=True on purpose: the user's own command may be a shell builtin- # or a .cmd shim (npm on Windows); we execute exactly what they typed.+ if needs_posix_shell:+ shell_exe = _posix_shell()+ if shell_exe is None:+ # cmd.exe would accept most of these and mean something else by+ # them, which is the failure this whole path exists to avoid. Not+ # running the command is recoverable; running a different one is+ # not.+ raise click.ClickException(+ "this command is POSIX shell syntax and no POSIX shell could be "+ "located, so it was not run; re-run it without `repowise distill`"+ )+ argv: str | list[str] = [shell_exe, "-c", command_str]+ use_shell = False+ else:+ # shell=True on purpose: the user's own command may be a shell builtin+ # or a .cmd shim (npm on Windows); we execute exactly what they typed.+ argv, use_shell = command_str, Trueproc = subprocess.run(- command_str,- shell=True,+ argv,+ shell=use_shell,capture_output=True,text=True,encoding="utf-8",@@ -76,6 +121,63 @@ def distill_command(source: str, command: tuple[str, ...]) -> None:sys.exit(proc.returncode)+#: Shells that read a POSIX command line from ``-c``. An allowlist rather+#: than a "does this file exist" test, because the wrong answer here is not an+#: error — it is a different command running and reporting success.+_POSIX_SHELL_NAMES = frozenset({"bash", "sh"})+++def _posix_shell() -> str | None:+ """Absolute path to the POSIX shell that launched this process, or None.++ Read out of the environment rather than looked up on ``PATH``, and that is+ the whole of the design. ``PATH`` on Windows answers ``bash`` with+ System32's ``bash.exe`` -- WSL, which is a different filesystem with a+ different ``git`` and no idea what a ``C:`` path means -- often before it+ answers with Git Bash. MSYS translates ``SHELL`` to a real Windows path+ for its children, so a command that arrived from the agent's Bash tool is+ handed back to the very interpreter that started us.++ Existence is not the test. ``-c`` is also how ``powershell.exe`` takes a+ command, so the name has to be one of ``_POSIX_SHELL_NAMES``, and WSL's+ ``System32\\bash.exe`` is excluded by path even though it passes that.++ None when nothing in the environment names a POSIX shell this process can+ use. The caller must refuse rather than substitute one: the whole point is+ that the command means something different in the wrong shell.+ """+ shell = os.environ.get("SHELL")+ if not shell or not os.path.isfile(shell):+ return None+ name = os.path.basename(shell).lower()+ if name.endswith(".exe"):+ name = name[:-4]+ if name not in _POSIX_SHELL_NAMES:+ # ``-c`` is also how powershell.exe and pwsh.exe take a command, so an+ # existence check alone would hand a POSIX command line to PowerShell+ # and get a plausible answer to a different question.+ return None+ if _under_system32(shell):+ # System32's ``bash.exe`` is WSL: another filesystem, another ``git``,+ # and no reading of a ``C:`` path that matches this process's. It is a+ # POSIX shell and still the wrong one.+ return None+ return shell+++def _under_system32(path: str) -> bool:+ # os.environ is case-insensitive on Windows, which is the only host+ # this is reached on.+ root = os.environ.get("SYSTEMROOT")+ if not root:+ return False+ try:+ prefix = os.path.normcase(os.path.join(os.path.abspath(root), "system32")) + os.sep+ return os.path.normcase(os.path.abspath(path)).startswith(prefix)+ except (OSError, ValueError):+ return False++# cmd.exe consumes these before the child ever sees them. ``^`` escapes each# one for exactly one round of parsing, which is all ``cmd /c`` does._CMD_METACHARS = frozenset('"&|<>^()')diff --git a/packages/cli/src/repowise/cli/rewrite_hook.py b/packages/cli/src/repowise/cli/rewrite_hook.pyindex ac9a07509..a28675889 100644--- a/packages/cli/src/repowise/cli/rewrite_hook.py+++ b/packages/cli/src/repowise/cli/rewrite_hook.py@@ -40,7 +40,7 @@ Bailouts — commands never rewritten:- the ignore-list of trivial or interactive commands (cd, echo, vim, …);- anything already invoking ``repowise``.-Compound commands, on POSIX hosts, are rewritten when every top-level+Compound commands, in the POSIX dialect, are rewritten when every top-levelsegment is separately safe — see ``_chain_families``. ``a && b``,``a; b``, and ``a | b`` are wrapped whole, as one single-quoted token, sothe operators bind inside distill's own shell and the exit code and@@ -59,7 +59,12 @@ import tempfile# Stdlib-only module by design (see hot-path discipline above) — safe to# import at module scope. (No pathlib: it costs double-digit milliseconds# of interpreter startup, which this hook pays on every Bash call.)-from repowise.cli.agent_adapters.base import RewriteResult+from repowise.cli.agent_adapters.base import (+ SHELL_POSIX,+ SHELL_POWERSHELL,+ RewriteResult,+ hook_source_for,+)# Free at module scope, and it has to be here rather than lazy: this module's# import is where the ledger's clock starts, so a firing's recorded cost covers@@ -259,28 +264,31 @@ _INERT_SEGMENT_TOKENS = frozenset({"cd", "echo", "printf", "pwd", "true", ":"})#: purpose: distill would capture the output of a job that has not run yet._CHAIN_OPS = frozenset({"&&", "||", ";"})-# distill executes via the system shell (cmd.exe on Windows, where-# head/tail/grep don't exist), so the safe-pipeline rewrite is-# POSIX-hosts-only. Module constant so tests can pin both platforms.-_POSIX_HOST = os.name == "posix"--# Windows keeps the blunt character bail, quoted or not. Two reasons:+# The PowerShell dialect keeps the blunt character bail, quoted or not. Two+# reasons, and both belong to the dialect rather than to the host:## - PowerShell has no backslash escape, so a Windows path ending in ``\``# does not extend a quoted run the way the POSIX rules here assume, and# the lexer would split the command in a place PowerShell would not.-# - Defense in depth on the renderer. ``distill_cmd._render_command`` now-# caret-escapes what it hands cmd.exe, but it still has to refuse a-# couple of shapes outright (a ``%NAME%`` cmd would expand, an embedded-# newline). A rewrite is auto-allowed, so this side stays conservative-# rather than depending on the far side getting every case right.+# - Defense in depth on the renderer. A PowerShell-dialect command is still+# handed to the system shell by ``distill``, and+# ``distill_cmd._render_command`` caret-escapes what it gives cmd.exe but+# still has to refuse a couple of shapes outright (a ``%NAME%`` cmd would+# expand, an embedded newline). A rewrite is auto-allowed, so this side+# stays conservative rather than depending on the far side getting every+# case right.#-# So the lexer's false-bail win is a POSIX win. On Windows it still buys the-# structural bailouts, just not the widening.-_WIN_SHELL_METACHAR_RE = re.compile(r"[|&;<>`^\n]|\$\(")+# This used to key on ``os.name`` instead, which declined every compound+# command on a Windows host — including the ones the agent's Bash tool wrote+# for bash, 2,203 commands worth 738,123 tokens over 30 days, 97.5% of them+# for this one reason. The premise was real: ``distill`` ran everything+# through cmd.exe. What removes it is ``distill`` reading the dialect back+# out of the ``--source`` label and running a POSIX-dialect command in the+# POSIX shell it was written for.+_PS_SHELL_METACHAR_RE = re.compile(r"[|&;<>`^\n]|\$\(")-def _split_safe_tail(command: str) -> tuple[str, bool] | None:+def _split_safe_tail(command: str, shell: str = SHELL_POSIX) -> tuple[str, bool] | None:"""Split *command* into (classifiable head, needs_inner_shell).Returns None when the command carries shell syntax the wrapper can't@@ -292,11 +300,11 @@ def _split_safe_tail(command: str) -> tuple[str, bool] | None:The structural decisions (chaining, substitution, redirects, how manystages, which tool ends the pipeline) come from ``shell_lexer``; what isleft here is the policy the lexer deliberately does not own — the- stderr-merge carve-out, the POSIX-host gate, and the quoting rules for- the one shape that gets re-quoted.+ stderr-merge carve-out, the PowerShell-dialect gate, and the quoting+ rules for the one shape that gets re-quoted."""declawed = _STDERR_MERGE_RE.sub("", command.strip())- if not _POSIX_HOST and _WIN_SHELL_METACHAR_RE.search(declawed):+ if shell == SHELL_POWERSHELL and _PS_SHELL_METACHAR_RE.search(declawed):return Nonepipeline = analyze_pipeline(declawed)if pipeline is None or pipeline.redirects:@@ -307,7 +315,7 @@ def _split_safe_tail(command: str) -> tuple[str, bool] | None:return pipeline.producer, False# A pipeline is re-quoted as one token. `_single_quote` makes the quoting# itself airtight, so only re-expansion is left to bail on.- if not _POSIX_HOST or _EXPANSION_CHAR in command:+ if shell == SHELL_POWERSHELL or _EXPANSION_CHAR in command:return Nonereturn pipeline.producer, True@@ -347,14 +355,15 @@ def _chain_families(command: str) -> tuple[str, ...] | None:command passes through, because wrapping it would auto-allow the partnobody vetted.+ A chain is POSIX shell syntax by construction, so callers check the+ dialect before asking and there is no host test here — the host never+ decided it. ``distill`` reads the same dialect back off the ``--source``+ label and runs the wrapped token through that shell, so the operators+ mean on the far side what they mean here.+Returns None when the chain is not admissible; otherwise the recognizedfamilies in order, whose first element names the rewrite."""- if not _POSIX_HOST:- # Same reason ``_split_safe_tail`` gates the pipeline shape: distill- # re-runs the wrapped token through the system shell, which is- # cmd.exe here, and these are POSIX command lines.- return Nonedeclawed = _STDERR_MERGE_RE.sub("", command.strip())if _EXPANSION_CHAR in declawed:return None@@ -449,13 +458,20 @@ _ACTION_FLAG_RES = ()-def classify(command: str) -> str | None:- """Return the distill family for *command*, or None to pass through."""+def classify(command: str, shell: str = SHELL_POSIX) -> str | None:+ """Return the distill family for *command*, or None to pass through.++ *shell* is the dialect the command was written for — the same value+ :func:`decide` takes, and for the same reason: a chain is POSIX syntax,+ so it is only ever offered to the POSIX dialect.+ """if not command:return None- split = _split_safe_tail(command)+ split = _split_safe_tail(command, shell)if split is not None:return _classify_head(split[0])+ if shell == SHELL_POWERSHELL:+ return Nonefamilies = _chain_families(command)return families[0] if families else None@@ -607,18 +623,18 @@ def _decide(``decide`` contract is a result or None. On a rewrite the reason is thedistill family, so one column carries both distributions."""- split = _split_safe_tail(command) if command else None+ split = _split_safe_tail(command, shell) if command else Nonechain: tuple[str, ...] = ()if split is not None:head_command, needs_inner_shell = splitfamily = _classify_head(head_command)if family is None:return None, BAIL_UNRECOGNIZED, _find_repo_root(cwd)- if shell == "powershell":+ if shell == SHELL_POWERSHELL:first = _normalize(head_command).split(None, 1)[0]if first in _PS_ALIAS_TOKENS:return None, BAIL_PS_ALIAS, _find_repo_root(cwd)- elif command and shell != "powershell":+ elif command and shell != SHELL_POWERSHELL:# A chain is POSIX shell syntax by construction (`&&`, `;`, `|`), so# it is only ever offered to the POSIX dialect.chain = _chain_families(command) or ()@@ -648,14 +664,18 @@ def _decide(permission = family_setting# The --source tag lands in the savings ledger so `repowise saved- # --by source` can tell hook surfaces apart from direct CLI use.+ # --by source` can tell hook surfaces apart from direct CLI use — and,+ # for the two labels derived from the dialect, it is also how `distill`+ # knows which shell to run the command in. Derived through the shared+ # helper rather than spelled here, so the writer and the reader cannot+ # drift apart.if source is None:- source = "hook-powershell" if shell == "powershell" else "hook-bash"+ source = hook_source_for(shell)# A pipeline or chain is passed as ONE quoted token so its operators bind- # inside distill's shell (distill re-runs a single token verbatim via- # shell=True) instead of binding to the wrapper. Single quotes, so the- # inner shell reads the token back byte for byte; `$` already bailed, so- # nothing in it re-expands.+ # inside distill's shell (distill re-runs a single token verbatim, through+ # the shell the `--source` label names) instead of binding to the wrapper.+ # Single quotes, so the inner shell reads the token back byte for byte;+ # `$` already bailed, so nothing in it re-expands.wrapped = _single_quote(command.strip()) if needs_inner_shell else command.strip()# The reason on a rewrite is the family: one ledger column then carries# both distributions, what we rewrite and what we decline.diff --git a/tests/unit/distill/test_agent_adapters.py b/tests/unit/distill/test_agent_adapters.pyindex 6561d5985..734d4b405 100644--- a/tests/unit/distill/test_agent_adapters.py+++ b/tests/unit/distill/test_agent_adapters.py@@ -14,7 +14,14 @@ import pytestfrom repowise.cli.agent_adapters import claude_code as claude_modulefrom repowise.cli.agent_adapters import codex as codex_module-from repowise.cli.agent_adapters.base import RewriteResult, unmatched_tool_names+from repowise.cli.agent_adapters.base import (+ SHELL_POSIX,+ SHELL_POWERSHELL,+ RewriteResult,+ dialect_for_hook_source,+ hook_source_for,+ unmatched_tool_names,+)from repowise.cli.agent_adapters.claude_code import ClaudeCodeAdapterfrom repowise.cli.agent_adapters.codex import SHELL_TOOL_MATCHER, CodexAdapterfrom repowise.cli.editor_integrations import claude_config, codex_config@@ -34,6 +41,7 @@ from repowise.cli.editor_integrations.codex_config import (remove_agents_md_distill_section,uninstall_codex_rewrite_hook,)+from repowise.cli.rewrite_hook import decide@pytest.fixture@@ -743,3 +751,53 @@ class TestAgentsMdDistillSection:def test_remove_when_absent(self, tmp_path) -> None:assert remove_agents_md_distill_section(tmp_path) is Falseassert agents_md_distill_section_installed(tmp_path) is False+++class TestSourceCarriesTheDialect:+ """The ``--source`` label is how ``repowise distill`` learns which shell to+ run a command in, so the two vocabularies have to stay inverse.++ This is not a style point. ``_decide`` writes the label from the dialect+ and ``distill_cmd`` reads the dialect back out of the label to pick an+ interpreter, in a different process. A rename on one side alone would not+ fail — it would run a POSIX command line in cmd.exe and report success.+ """++ @pytest.mark.parametrize("dialect", [SHELL_POSIX, SHELL_POWERSHELL])+ def test_the_label_round_trips_to_the_dialect_that_wrote_it(self, dialect) -> None:+ assert dialect_for_hook_source(hook_source_for(dialect)) == dialect++ def test_every_source_the_hook_can_emit_names_a_dialect(self, tmp_path) -> None:+ """Pinned through ``decide`` rather than the helper, so the assertion+ covers the string the agent actually receives."""+ (tmp_path / ".repowise").mkdir()+ for dialect in (SHELL_POSIX, SHELL_POWERSHELL):+ result = decide("git status", str(tmp_path), dialect)+ assert result is not None+ source = result.command.split("--source ", 1)[1].split(None, 1)[0]+ assert dialect_for_hook_source(source) == dialect++ def test_an_adapter_supplied_source_names_no_dialect(self) -> None:+ """Codex sets its own ``savings_source``, and ``hook-codex`` says+ nothing about a shell. None is the honest answer, and every caller+ reads it as the host default, so an unknown label can never pick an+ interpreter."""+ assert CodexAdapter.savings_source == "hook-codex"+ assert dialect_for_hook_source(CodexAdapter.savings_source) is None+ assert dialect_for_hook_source("cli") is None++ def test_an_older_distill_still_understands_what_the_hook_writes(self, tmp_path) -> None:+ """Why the dialect rides on ``--source`` instead of a flag of its own.++ ``distill_command`` takes ``ignore_unknown_options`` with+ ``allow_interspersed_args=False``, which puts an unrecognized option+ at the *front of the wrapped command* — so a flag an older build did+ not know would be run as the program name and the agent's command+ would never execute. Every option the hook emits must therefore be one+ that has been there all along.+ """+ (tmp_path / ".repowise").mkdir()+ result = decide("ls src && git diff a.ts", str(tmp_path), SHELL_POSIX)+ assert result is not None+ options = [tok for tok in result.command.split() if tok.startswith("--")]+ assert options == ["--source"]diff --git a/tests/unit/distill/test_cli.py b/tests/unit/distill/test_cli.pyindex f563a2046..5a1be18ec 100644--- a/tests/unit/distill/test_cli.py+++ b/tests/unit/distill/test_cli.py@@ -5,6 +5,7 @@ from __future__ import annotationsimport jsonimport osimport shlex+import shutilimport subprocessimport sysfrom pathlib import Path@@ -12,7 +13,11 @@ from pathlib import Pathimport pytestfrom click.testing import CliRunner-from repowise.cli.commands.distill_cmd import _render_command, distill_command+from repowise.cli.commands.distill_cmd import (+ _posix_shell,+ _render_command,+ distill_command,+)from repowise.cli.commands.expand_cmd import expand_commandfrom repowise.core.distill.markers import parse_marker_refsfrom repowise.core.distill.store import OmissionStore@@ -221,6 +226,113 @@ def test_render_command_refuses_newlines(payload: str) -> None:_render_command(("git", "log", f"--grep={payload}"))+# ---------------------------------------------------------------------------+# --source also names a dialect: the command runs in the shell it was+# written for+#+# The rewrite hook declined every compound command on a Windows host because+# distill handed everything to cmd.exe, where a POSIX command line means+# something else. These pin the replacement: the dialect travels with the+# command, and when the shell it names cannot be found distill refuses rather+# than running a different command than the one it was given.+# ---------------------------------------------------------------------------++[email protected](sys.platform != "win32", reason="cmd.exe is only the default on Windows")+ "shell_value",+ [+ pytest.param(None, id="unset"),+ pytest.param("powershell.exe", id="powershell-also-takes--c"),+ pytest.param("system32-bash", id="wsl-is-a-posix-shell-and-still-wrong"),+ ],+)+def test_only_a_real_posix_shell_is_accepted(monkeypatch, shell_value) -> None:+ """Breaking what the guard protects, one substitute at a time.++ ``-c`` is how ``powershell.exe`` takes a command too, so an+ existence check would hand a POSIX pipeline to PowerShell and get a+ plausible answer to a different question. WSL's ``System32\bash.exe``+ passes a name test and is still another machine.+ """+ if shell_value is None:+ monkeypatch.delenv("SHELL", raising=False)+ elif shell_value == "system32-bash":+ root = os.environ.get("SYSTEMROOT", r"C:\Windows")+ wsl = os.path.join(root, "System32", "bash.exe")+ if not os.path.isfile(wsl):+ pytest.skip("WSL bash.exe not installed on this host")+ monkeypatch.setenv("SHELL", wsl)+ else:+ found = shutil.which(shell_value)+ if found is None:+ pytest.skip(f"{shell_value} not on this host")+ monkeypatch.setenv("SHELL", found)+ assert _posix_shell() is None++[email protected](sys.platform != "win32", reason="cmd.exe is only the default on Windows")+def test_a_plain_command_never_needs_a_posix_shell(repo_cwd: Path, monkeypatch) -> None:+ """The refusal must not reach commands that were always fine in cmd.exe.++ A multi-token argv is the same command in either dialect. Only the+ single-token shape the hook wraps a chain in carries POSIX syntax of its+ own, so only that shape may ever refuse to run.+ """+ monkeypatch.delenv("SHELL", raising=False)+ result = CliRunner().invoke(+ distill_command, ["--source", "hook-bash", *_py("print('still runs')")]+ )+ assert result.exit_code == 0, result.output+ assert "still runs" in result.output++[email protected](sys.platform != "win32", reason="cmd.exe is only the default on Windows")+def test_posix_dialect_runs_in_a_posix_shell(repo_cwd: Path) -> None:+ """``echo "a b" | tr " " "_"`` is the cheapest proof the shell changed.++ cmd.exe keeps the quotes literal and answers ``"a_b"_``; bash answers+ ``a_b``. Both exit 0, so a wrong shell here is silent corruption rather+ than an error — which is why the hook used to decline the command+ outright.+ """+ if _posix_shell() is None:+ pytest.skip("no POSIX shell on this host")+ command = ['echo "a b" | tr " " "_"']+ posix = CliRunner().invoke(distill_command, ["--source", "hook-bash", *command])+ assert posix.exit_code == 0, posix.output+ assert posix.output.strip() == "a_b"++ host = CliRunner().invoke(distill_command, command)+ assert host.output.strip() != "a_b"++[email protected](sys.platform != "win32", reason="cmd.exe is only the default on Windows")+def test_posix_dialect_refuses_rather_than_falling_back_to_cmd(+ repo_cwd: Path, monkeypatch+) -> None:+ """Breaking what the guard protects: with no POSIX shell it must not run.++ Falling back to the host shell is the exact bug this path exists to+ close, and it fails silently, so the refusal is the safer half of the+ trade. A command that did not run is recoverable; a command that ran and+ meant something else is not.+ """+ monkeypatch.delenv("SHELL", raising=False)+ result = CliRunner().invoke(distill_command, ["--source", "hook-bash", "echo ran-anyway"])+ assert result.exit_code != 0+ assert "ran-anyway" not in result.output+ assert "POSIX shell" in result.output+++def test_no_shell_flag_keeps_the_host_default(repo_cwd: Path) -> None:+ """Plain ``repowise distill ...`` from a terminal is untouched by any of+ this: no dialect named means the shell this host would have used."""+ result = CliRunner().invoke(distill_command, _py("print('host default')"))+ assert result.exit_code == 0+ assert "host default" in result.output++def test_distill_and_expand_roundtrip(repo_cwd: Path, fixtures_dir: Path) -> None:raw = (fixtures_dir / "distill" / "git_log_full.txt").read_text(encoding="utf-8-sig")from repowise.core.distill import distill_outputdiff --git a/tests/unit/distill/test_rewrite_hook.py b/tests/unit/distill/test_rewrite_hook.pyindex def6ba531..3831ecfcd 100644--- a/tests/unit/distill/test_rewrite_hook.py+++ b/tests/unit/distill/test_rewrite_hook.py@@ -15,6 +15,7 @@ import pytestimport yamlfrom repowise.cli import rewrite_hook+from repowise.cli.agent_adapters.base import SHELL_POWERSHELLfrom repowise.cli.rewrite_hook import FAMILY_PATTERNS, _normalize, classify, decide# ---------------------------------------------------------------------------@@ -202,19 +203,13 @@ class TestSafeTails:"""The two shell-syntax carve-outs: trailing ``2>&1`` and one pipe intoa bare stdin filter (head/tail/grep/egrep/fgrep/rg).- ``2>&1`` is platform-neutral (distill merges stderr into its capture- anyway). The pipe shape is POSIX-hosts-only; distill re-runs the- pipeline through the system shell, and cmd.exe has no head/tail/grep.+ ``2>&1`` is dialect-neutral (distill merges stderr into its capture+ anyway). The pipe shape is POSIX-dialect-only, and the dialect is the+ command's, not the host's: the ``--source`` label carries the dialect the+ hook decided from, so a bash pipeline runs in bash on Windows too. Every+ assertion below therefore holds on every platform."""- @pytest.fixture- def posix_host(self, monkeypatch):- monkeypatch.setattr(rewrite_hook, "_POSIX_HOST", True)-- @pytest.fixture- def windows_host(self, monkeypatch):- monkeypatch.setattr(rewrite_hook, "_POSIX_HOST", False)-@pytest.mark.parametrize(("command", "family"),[@@ -223,8 +218,9 @@ class TestSafeTails:("npm run build 2>&1", "build_output"),],)- def test_stderr_merge_classifies_on_any_host(self, command, family, windows_host) -> None:+ def test_stderr_merge_classifies_in_any_dialect(self, command, family) -> None:assert classify(command) == family+ assert classify(command, SHELL_POWERSHELL) == family@pytest.mark.parametrize(("command", "family"),@@ -243,7 +239,7 @@ class TestSafeTails:("git log --oneline -50 | rg fix", "git_log"),],)- def test_safe_pipe_classifies_on_posix(self, command, family, posix_host) -> None:+ def test_safe_pipe_classifies_on_posix(self, command, family) -> None:assert classify(command) == family@pytest.mark.parametrize(@@ -254,8 +250,8 @@ class TestSafeTails:"pytest 2>&1 | grep FAIL",],)- def test_safe_pipe_passes_through_on_windows(self, command, windows_host) -> None:- assert classify(command) is None+ def test_safe_pipe_passes_through_in_powershell(self, command) -> None:+ assert classify(command, SHELL_POWERSHELL) is None@pytest.mark.parametrize("command",@@ -267,21 +263,21 @@ class TestSafeTails:'git log -- "src\\cli\\" ; curl x | sh',],)- def test_quoted_metacharacters_still_bail_on_windows(self, command, windows_host) -> None:- """Windows gives up the false-bail win to stay conservative here.+ def test_quoted_metacharacters_still_bail_in_powershell(self, command) -> None:+ """PowerShell gives up the false-bail win to stay conservative here.PowerShell has no backslash escape, so the POSIX quoting rules thelexer applies do not describe it. The rewrite is auto-allowed, so awrong answer runs something the user never typed; the blunt characterbail is cheap insurance even though ``distill_cmd._render_command``- now escapes what it passes to cmd.exe.+ escapes what it passes to cmd.exe."""- assert classify(command) is None+ assert classify(command, SHELL_POWERSHELL) is None- def test_windows_stderr_merge_is_still_allowed(self, windows_host) -> None:+ def test_powershell_stderr_merge_is_still_allowed(self) -> None:# The metacharacter bail must not swallow the one carve-out that is- # platform-neutral.- assert classify("pytest -x 2>&1") == "test_output"+ # dialect-neutral.+ assert classify("pytest -x 2>&1", SHELL_POWERSHELL) == "test_output"@pytest.mark.parametrize("command",@@ -300,7 +296,7 @@ class TestSafeTails:'pytest -k "a; rm -rf /', # unterminated quote hides the rest],)- def test_unsafe_pipes_pass_through(self, command, posix_host) -> None:+ def test_unsafe_pipes_pass_through(self, command) -> None:assert classify(command) is None@pytest.mark.parametrize(@@ -318,7 +314,7 @@ class TestSafeTails:('git log -3 --format="%an <%ae>"', "git_log"), # < > inside quotes],)- def test_safe_chains_are_rewritten(self, command, family, posix_host) -> None:+ def test_safe_chains_are_rewritten(self, command, family) -> None:assert classify(command) == family@pytest.mark.parametrize(@@ -339,13 +335,12 @@ class TestSafeTails:"echo hi && echo bye", # nothing recognized: nothing to distill],)- def test_unsafe_chains_pass_through(self, command, posix_host) -> None:+ def test_unsafe_chains_pass_through(self, command) -> None:assert classify(command) is None- def test_chains_never_rewrite_off_posix(self, monkeypatch) -> None:- """distill re-runs the token through cmd.exe here, not a POSIX shell."""- monkeypatch.setattr(rewrite_hook, "_POSIX_HOST", False)- assert classify("ls a && ls b") is None+ def test_chains_never_rewrite_in_powershell(self) -> None:+ """``&&`` is POSIX syntax; distill would hand this to cmd.exe."""+ assert classify("ls a && ls b", SHELL_POWERSHELL) is Nonedef test_single_quote_survives_a_shell_round_trip(self) -> None:"""The wrap has to hand the inner shell back exactly what was typed."""@@ -375,7 +370,7 @@ class TestSafeTails:"npm test | tail --follow",],)- def test_follow_modes_never_rewrite(self, command, posix_host) -> None:+ def test_follow_modes_never_rewrite(self, command) -> None:assert classify(command) is None@pytest.mark.parametrize(@@ -387,7 +382,7 @@ class TestSafeTails:("cat server.log", "logs"),],)- def test_follow_check_does_not_overreach(self, command, family, posix_host) -> None:+ def test_follow_check_does_not_overreach(self, command, family) -> None:assert classify(command) == family@pytest.mark.parametrize(@@ -398,7 +393,7 @@ class TestSafeTails:'echo "a; b"',],)- def test_quoted_operators_are_not_bailouts(self, command, posix_host) -> None:+ def test_quoted_operators_are_not_bailouts(self, command) -> None:"""An operator inside quotes is text, so these classify normally.None of them is a distill family, so the visible outcome is still a@@ -413,7 +408,7 @@ class TestSafeTails:assert analysis.final_tool is Noneassert classify(command) is None- def test_quoted_operator_in_a_family_command_rewrites(self, repo, posix_host) -> None:+ def test_quoted_operator_in_a_family_command_rewrites(self, repo) -> None:"""The false-bail fix is visible on a family command: a quoted ``|``no longer stops ``pytest -k`` from being recognized."""result = decide('pytest -k "a|b"', str(repo))@@ -428,7 +423,7 @@ class TestSafeTails:assert result is not Noneassert result.command == "repowise distill --source hook-bash pytest -x 2>&1"- def test_decide_quotes_safe_pipeline(self, repo, posix_host) -> None:+ def test_decide_quotes_safe_pipeline(self, repo) -> None:result = decide("pytest tests/unit -q | head -50", str(repo))assert result is not Noneassert result.command == (@@ -436,7 +431,7 @@ class TestSafeTails:)assert result.permission == "allow"- def test_decide_quotes_grep_pipeline(self, repo, posix_host) -> None:+ def test_decide_quotes_grep_pipeline(self, repo) -> None:# The whole pipeline stays one token, so grep still filters distill's# rendering inside distill's own shell and the omission marker it# emits survives to the agent.@@ -445,12 +440,12 @@ class TestSafeTails:assert result.command == "repowise distill --source hook-bash 'pytest 2>&1 | grep FAIL'"assert "repowise expand" in result.reason- def test_decide_wraps_a_chain_as_one_token(self, repo, posix_host) -> None:+ def test_decide_wraps_a_chain_as_one_token(self, repo) -> None:result = decide("ls src && git diff a.ts", str(repo))assert result is not Noneassert result.command == "repowise distill --source hook-bash 'ls src && git diff a.ts'"- def test_a_family_set_off_cannot_be_reached_by_chaining(self, repo, posix_host) -> None:+ def test_a_family_set_off_cannot_be_reached_by_chaining(self, repo) -> None:"""Otherwise `git_diff: off` is bypassable by prefixing anything."""_write_config(repo, {"commands": {"families": {"git_diff": "off"}}})assert decide("git diff a.py", str(repo)) is Nonediff --git a/tests/unit/distill/test_rewrite_ledger.py b/tests/unit/distill/test_rewrite_ledger.pyindex 9a0b02b21..2a4c14fe9 100644--- a/tests/unit/distill/test_rewrite_ledger.py+++ b/tests/unit/distill/test_rewrite_ledger.py@@ -28,7 +28,6 @@ def repo(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:(tmp_path / ".repowise").mkdir()# One ledger connection is cached per repo path for the life of the# process; the fixture gives each test its own path, so nothing leaks.- monkeypatch.setattr(rewrite_hook, "_POSIX_HOST", True)return tmp_path@@ -133,7 +132,6 @@ def test_nothing_is_written_outside_a_repowise_repo() -> None:"""The hook is installed globally. A directory that never opted in gets nosidecar, which is also why those commands can never be counted."""- monkeypatch.setattr(rewrite_hook, "_POSIX_HOST", True)_fire(tmp_path, "pytest -q", monkeypatch)assert not (tmp_path / ".repowise").exists()diff --git a/tests/unit/distill/test_shell_lexer.py b/tests/unit/distill/test_shell_lexer.pyindex 82c59a31d..67d5c0f40 100644--- a/tests/unit/distill/test_shell_lexer.py+++ b/tests/unit/distill/test_shell_lexer.py@@ -220,24 +220,23 @@ class TestPowerShellEdgeCases:def test_powershell_sourced_commands_are_never_rewritten(self, command, tmp_path) -> None:"""The PowerShell guarantee belongs to ``decide``, not ``classify``.- ``classify`` takes no shell argument and never encoded a PowerShell- rule: it answers "which family is this", and a statement separator is- also a perfectly good POSIX ``;``. What must hold is that a command- *sourced from the PowerShell tool* is never rewritten, and that gate- lives in ``decide``. Asserting it on ``classify`` passed on Windows- only because ``_POSIX_HOST`` is False there, which made every compound- command bail for a reason that had nothing to do with PowerShell.+ ``classify`` defaults to the POSIX dialect and answers "which family+ is this"; a statement separator is also a perfectly good POSIX ``;``.+ What must hold is that a command *sourced from the PowerShell tool* is+ never rewritten, and that gate lives in ``decide``. This used to pass+ on Windows for the wrong reason — a host test made every compound+ command bail whatever dialect it came from."""(tmp_path / ".repowise").mkdir()assert rewrite_hook.decide(command, str(tmp_path), "powershell") is None- def test_a_posix_shell_may_still_rewrite_the_separator_shape(self, tmp_path, monkeypatch):+ def test_a_posix_shell_may_still_rewrite_the_separator_shape(self, tmp_path):"""…and the same text from bash is two recognized commands, so it does.This is the one shape above that is not PowerShell-specific, and it- pins the distinction the test above rests on.+ pins the distinction the test above rests on — on every host, which is+ the point: the dialect decides this, not ``os.name``."""- monkeypatch.setattr(rewrite_hook, "_POSIX_HOST", True)(tmp_path / ".repowise").mkdir()command = "git status; git log --oneline -5"assert rewrite_hook.decide(command, str(tmp_path), "powershell") is None