From 513c92d21b5949146d493eb963c7ec311704508a Mon Sep 17 00:00:00 2001 From: Trecek Date: Thu, 5 Nov 2026 13:30:44 -0800 Subject: [PATCH 01/46] Classify Git ref outcomes through option tables, not option tokens Phase A of the git outcome and effective-target immunity work (#4751). A parse-options engine over per-subcommand option tables now maps branch, checkout, switch, symbolic-ref, update-ref, reset, push and fetch argv to what Git will do: which branch ref is rewritten and whether HEAD moves. Bundled, stuck, abbreviated, negated and reordered spellings are read the way Git reads them, and unknown or ambiguous options classify as ambiguous instead of falling through to allow. git_ops_guard now fails closed on any unexpected exception while classifying a command. Co-Authored-By: Claude Sonnet 5.5 --- docs/safety/hooks.md | 2 +- .../hooks/_classification/AGENTS.md | 5 +- .../hooks/_classification/_option_grammar.py | 160 ++++ src/autoskillit/hooks/_runtime/AGENTS.md | 4 + .../_runtime/_git_command_classification.py | 381 +++----- .../hooks/_runtime/_git_option_tables.py | 368 ++++++++ .../hooks/_runtime/_git_outcome.py | 367 ++++++++ src/autoskillit/hooks/guards/AGENTS.md | 6 +- src/autoskillit/hooks/guards/git_ops_guard.py | 91 +- tests/_test_filter.py | 2 +- tests/arch/test_guard_flag_spec_coverage.py | 24 +- .../arch/test_subpackage_isolation_facades.py | 2 + tests/hooks/test_git_effect_differential.py | 858 ++++++++++++++++++ tests/hooks/test_git_option_table_contract.py | 175 ++++ tests/hooks/test_git_option_tables.py | 141 +++ tests/hooks/test_git_outcome.py | 349 +++++++ tests/hooks/test_option_grammar.py | 140 +++ tests/infra/test_git_ops_guard.py | 289 ++++-- 18 files changed, 2974 insertions(+), 390 deletions(-) create mode 100644 src/autoskillit/hooks/_classification/_option_grammar.py create mode 100644 src/autoskillit/hooks/_runtime/_git_option_tables.py create mode 100644 src/autoskillit/hooks/_runtime/_git_outcome.py create mode 100644 tests/hooks/test_git_effect_differential.py create mode 100644 tests/hooks/test_git_option_table_contract.py create mode 100644 tests/hooks/test_git_option_tables.py create mode 100644 tests/hooks/test_git_outcome.py create mode 100644 tests/hooks/test_option_grammar.py diff --git a/docs/safety/hooks.md b/docs/safety/hooks.md index 6a5d05603e..9fe77e507e 100644 --- a/docs/safety/hooks.md +++ b/docs/safety/hooks.md @@ -687,7 +687,7 @@ as a defense-in-depth measure against privilege escalation: | `background_exec_guard.py` | Unrecognized `AUTOSKILLIT_SESSION_TYPE` | Unknown session type should not bypass `run_in_background` prohibition | | `github_mutation_guard.py` | Ambiguous or unresolved GitHub mutation command, or unexpected classifier error | Unknown mutation scope must not bypass the structured review publisher | | `exploration_request_identity_guard.py` | A supported exploration event lacks bounded native identity or its one-shot record cannot be written | A Claude exploration call must not execute without request-correlated authority | -| `git_ops_guard.py` | Unexpected runtime error during the checked-out-ref preflight (OSError, subprocess.SubprocessError, TypeError, UnicodeDecodeError, ValueError); or, in the separate headless destructive-op-blocking preflight, an unrecognized global git flag that leaves the real subcommand unresolved | An unhandled exception must not silently allow a checked-out ref mutation — use exit 2 + stderr to hard-block. Separately, `_git_command_classification._contains_blocked_git_op` cannot match `_BLOCKED_GIT_OPS`'s literal subcommand tuples against an unresolved subcommand, so it denies unconditionally the moment `extract_git_subcommand_and_flags` reports `""`, rather than silently falling through to "not blocked" | +| `git_ops_guard.py` | Any unexpected runtime error (any `Exception`) while classifying a Git command, in the checked-out-ref preflight or the headless destructive-op phase; or, in the separate headless destructive-op-blocking preflight, an unrecognized global git flag that leaves the real subcommand unresolved | An unhandled exception must not silently allow a checked-out ref mutation — use exit 2 + stderr to hard-block. Separately, `_git_command_classification._contains_blocked_git_op` cannot match `_BLOCKED_GIT_OPS`'s literal subcommand tuples against an unresolved subcommand, so it denies unconditionally the moment `extract_git_subcommand_and_flags` reports `""`, rather than silently falling through to "not blocked" | | `pr_create_guard.py` | Hook config unreadable or malformed while the kitchen is open (OSError, JSONDecodeError, AttributeError, TypeError) | An unresolvable `recipe_allows_pr_create` authorization must not be read as permission to bypass the prepare_pr → compose_pr pipeline | | `unsafe_install_guard.py` | An unrecognized global pip flag leaves `pip`'s `install` token position unresolved | `_find_pip_install` cannot tell whether the command is a pip install at all; treating that the same as "definitely not an install" would silently skip the editable/system-install checks entirely, so it is threaded through as a distinct `"unresolved-pip-flags"` kind and denied unconditionally, matching the pre-existing `"unresolved-subprocess"` kind's treatment | | `write_guard.py` | A write-verb operand or redirect target cannot be resolved while a write-prefix policy applies | An unresolved target could escape the allowed prefix; the denial explains how to substitute a literal path. | diff --git a/src/autoskillit/hooks/_classification/AGENTS.md b/src/autoskillit/hooks/_classification/AGENTS.md index 0eaa2ddc03..3c52d66581 100644 --- a/src/autoskillit/hooks/_classification/AGENTS.md +++ b/src/autoskillit/hooks/_classification/AGENTS.md @@ -21,6 +21,9 @@ block B bootstrap so hook scripts can keep importing via `EvaluatedSegment`, `StdinLiteral`, `strip_heredoc_bodies`). - `_flags.py` — flag-parsing tables, shell-substitution regexes, and protected-read classification. +- `_option_grammar.py` — Git parse-options engine (`parse_options`, `last_option`, + `OptionArity`, `OptionDef`, `ParsedArgv`): bundled, stuck, `=`-joined, + abbreviated and negated options over per-subcommand tables; leaf module. - `_flag_arity_classification.py` — leaf sibling holding the `_FlagArity` enum referenced by `_flags.py` and `_interpreters.py`. Lifted out so neither classification module owns the cross-import that would otherwise create a @@ -52,7 +55,7 @@ the facade's relative imports and any bare-name bootstrap (hook scripts that push `hooks/_classification/` onto `sys.path`) resolve the same symbols. Within-folder imports of leaf siblings (`_tokenizer.py`, `_flag_arity_classification.py`, -`_source_map.py`) +`_source_map.py`, `_option_grammar.py`) stay at module top-level — they have no back-references into the facade or into other `_classification/` modules, so the relative-import dance isn't required. diff --git a/src/autoskillit/hooks/_classification/_option_grammar.py b/src/autoskillit/hooks/_classification/_option_grammar.py new file mode 100644 index 0000000000..7d5cf41b04 --- /dev/null +++ b/src/autoskillit/hooks/_classification/_option_grammar.py @@ -0,0 +1,160 @@ +"""Git parse-options engine over per-subcommand option tables. + +Members: OptionArity, OptionDef, ParsedOption, ParsedArgv, parse_options, +last_option. + +Models what a Git subcommand's argv means rather than which marker tokens it +contains: interleaved options and operands, ``--`` and the ``-`` operand, +bundled short options, stuck short values (``-Bx``), ``--opt=value``, unique +long-option prefixes and ``--no-`` negation. Abbreviations are always on +because the guard must over-approximate Git. + +Tables map a canonical dashless name to an ``OptionDef``. A row that has a +long spelling is named by it; a short-only row is named by its letter. + +``OptionArity`` is a ``StrEnum`` and ``OptionDef`` / ``ParsedArgv`` are +``NamedTuple``s. A module loaded under both its dotted and bare names yields +two classes, so compare members with ``==``, never ``is`` or ``isinstance``. + +Stdlib-only leaf: imports nothing from the package. +""" + +from __future__ import annotations + +from collections.abc import Mapping, Sequence +from enum import StrEnum +from typing import NamedTuple + + +class OptionArity(StrEnum): + NONE = "none" + REQUIRED = "required" + # A value arrives only via ``--opt=v`` or a stuck ``-Xv``; the next token + # is never consumed. + OPTIONAL = "optional" + + +class OptionDef(NamedTuple): + name: str + short: str | None + arity: OptionArity + negatable: bool + has_long: bool + + +class ParsedOption(NamedTuple): + name: str + value: str | None + negated: bool + + +class ParsedArgv(NamedTuple): + options: tuple[ParsedOption, ...] + operands: tuple[str, ...] + operands_before_dashdash: int | None + # unknown_option | ambiguous_option | missing_value | unexpected_value + unresolved: str | None + + +def _match_long(spelling: str, longs: Mapping[str, OptionDef]) -> tuple[OptionDef, bool] | str: + """Return ``(row, negated)`` for *spelling*, or an ``unresolved`` reason.""" + candidates: list[tuple[OptionDef, bool, str]] = [] + for definition in longs.values(): + candidates.append((definition, False, definition.name)) + if definition.negatable: + candidates.append((definition, True, f"no-{definition.name}")) + for definition, negated, full in candidates: + if full == spelling: + return definition, negated + matches = {(d.name, n): (d, n) for d, n, full in candidates if full.startswith(spelling)} + if not spelling or not matches: + return "unknown_option" + if len(matches) > 1: + return "ambiguous_option" + return next(iter(matches.values())) + + +def _resolve_value( + arity: OptionArity, inline: str | None, args: Sequence[str], index: int +) -> tuple[str | None, int, str | None]: + """Value, next index and ``unresolved`` reason for one matched option.""" + if arity == OptionArity.NONE: + return None, index, ("unexpected_value" if inline is not None else None) + if inline is not None or arity == OptionArity.OPTIONAL: + return inline, index, None + if index < len(args): + return args[index], index + 1, None + return None, index, "missing_value" + + +def _consume_long( + token: str, + args: Sequence[str], + index: int, + longs: Mapping[str, OptionDef], + options: list[ParsedOption], +) -> tuple[int, str | None]: + spelling, has_value, value = token[2:].partition("=") + matched = _match_long(spelling, longs) + if isinstance(matched, str): + return index, matched + definition, negated = matched + arity = OptionArity.NONE if negated else definition.arity + resolved, index, unresolved = _resolve_value(arity, value if has_value else None, args, index) + if unresolved is None: + options.append(ParsedOption(definition.name, resolved, negated)) + return index, unresolved + + +def _consume_short_bundle( + token: str, + args: Sequence[str], + index: int, + shorts: Mapping[str, OptionDef], + options: list[ParsedOption], +) -> tuple[int, str | None]: + for position in range(1, len(token)): + definition = shorts.get(token[position]) + if definition is None: + return index, "unknown_option" + if definition.arity == OptionArity.NONE: + options.append(ParsedOption(definition.name, None, False)) + continue + resolved, index, unresolved = _resolve_value( + definition.arity, token[position + 1 :] or None, args, index + ) + if unresolved is None: + options.append(ParsedOption(definition.name, resolved, False)) + return index, unresolved + return index, None + + +def parse_options(args: Sequence[str], table: Mapping[str, OptionDef]) -> ParsedArgv: + longs = {name: definition for name, definition in table.items() if definition.has_long} + shorts = {definition.short: definition for definition in table.values() if definition.short} + options: list[ParsedOption] = [] + operands: list[str] = [] + before_dashdash: int | None = None + unresolved: str | None = None + index = 0 + while index < len(args) and unresolved is None: + token = args[index] + index += 1 + if token == "--": + before_dashdash = len(operands) + operands.extend(args[index:]) + break + if token.startswith("--"): + index, unresolved = _consume_long(token, args, index, longs, options) + elif token.startswith("-") and len(token) > 1: + index, unresolved = _consume_short_bundle(token, args, index, shorts, options) + else: + operands.append(token) + return ParsedArgv(tuple(options), tuple(operands), before_dashdash, unresolved) + + +def last_option(parsed: ParsedArgv, name: str) -> ParsedOption | None: + for option in reversed(parsed.options): + if option.name == name: + return option + return None diff --git a/src/autoskillit/hooks/_runtime/AGENTS.md b/src/autoskillit/hooks/_runtime/AGENTS.md index e1fba743d4..e80e84f284 100644 --- a/src/autoskillit/hooks/_runtime/AGENTS.md +++ b/src/autoskillit/hooks/_runtime/AGENTS.md @@ -48,6 +48,10 @@ logic, with zero consumers outside this one file. - `_git_command_classification.py` — Git operation classification used by `guards/git_ops_guard.py`; it shares the runtime command and mutation primitives. + It reads option semantics through `_git_outcome.py` (what an argv does: + rewritten ref and HEAD effect), which reads `_git_option_tables.py` (data-only + per-subcommand option tables) through the `_classification/_option_grammar.py` + parse-options engine. - `_github_mutation_analysis.py`, `_command_classification.py` → `hooks/_classification/` (a sibling subpackage outside the move set, not itself moved) diff --git a/src/autoskillit/hooks/_runtime/_git_command_classification.py b/src/autoskillit/hooks/_runtime/_git_command_classification.py index 1ff0daae7a..292b0d4e45 100644 --- a/src/autoskillit/hooks/_runtime/_git_command_classification.py +++ b/src/autoskillit/hooks/_runtime/_git_command_classification.py @@ -1,41 +1,39 @@ """Classification primitives for git operations used by git_ops_guard. -Step 9 (#4733) of the decompose-hooks-files plan (#4665). All `_classify_*` -primitives and the helpers used exclusively by them moved from -`git_ops_guard.py` (1,002 lines) into this sibling module. The orchestrator -(`git_ops_guard.py`) imports a controlled subset of the public surface -(`_classify_git_segment`, `_contains_blocked_git_op`, `_git_result`, -`_git_text`, `_parse_worktree_owners`, `_resolve_attempted_sha`, -`_resolve_git_common_dir`) and dispatches into the inner classifiers -transitively through `_classify_git_segment`. - -This module is stdlib-only at the package level: it imports seven symbols -from `_command_classification` (the tokenization/classification primitives -`_command_position_candidate_spans`, `_consume_str_flag`, -`all_evaluated_segments`, `command_verb_and_args`, -`extract_git_subcommand_and_flags`, `extract_interpreter_command_payloads`, -`live_command_text`, and the structural type `_FlagArity`) and one symbol -from `_github_mutation_analysis` (`_DYNAMIC_SHELL_TOKEN_RE`). Both are -bare-name flat-mode siblings resolved via `git_ops_guard.py`'s `sys.path` -bootstrap (or the test-side bootstrap centralized in `tests/conftest.py`). -It does NOT re-export any `_command_classification` symbol. +The orchestrator (`git_ops_guard.py`) imports a controlled subset of the public +surface (`_classify_git_segment`, `_classify_git_invocation`, +`_contains_blocked_git_op`, `_git_result`, `_git_text`, `_parse_worktree_owners`, +`_resolve_attempted_sha`, `_resolve_git_common_dir`) and dispatches into the inner +classifiers transitively through `_classify_git_invocation`. + +Ref-rewriting subcommands are classified by outcome: branch, checkout, switch, +symbolic-ref, update-ref, reset, push and fetch read their argv through the +option tables in `_git_option_tables` and the parsers in `_git_outcome`, never by +matching option tokens. + +This module is stdlib-only at the package level: it imports the +tokenization/classification primitives from `_command_classification`, the +outcome parsers from `_git_outcome` and one symbol from +`_github_mutation_analysis` (`_DYNAMIC_SHELL_TOKEN_RE`). All are bare-name +flat-mode siblings resolved via `git_ops_guard.py`'s `sys.path` bootstrap (or +the test-side bootstrap centralized in `tests/conftest.py`). It does NOT +re-export any `_command_classification` symbol. `_contains_blocked_git_op` reads the command exclusively through -`all_evaluated_segments`/`live_command_text` (rectify #4941 Part A) rather -than a raw `shlex.split`/text-bag scan, so a `git` invocation delivered via +`all_evaluated_segments`/`live_command_text` (rectify #4941 Part A) rather than +a raw `shlex.split`/text-bag scan, so a `git` invocation delivered via `bash -c`, a heredoc, a herestring, or a pipe is seen the same way a direct invocation is. Public API surface: - Classifiers (7): - _classify_git_segment, _classify_fetch, _classify_push, - _classify_update_ref, _classify_branch_position, _classify_reset, - _contains_blocked_git_op + Classifiers: _classify_git_segment, _classify_git_invocation, + _classify_fetch, _classify_push, _classify_update_ref, _classify_reset, + _classify_outcome, _contains_blocked_git_op Helpers (consumed transitively by the classifiers above): _git_result, _git_text, _parse_worktree_owners, _resolve_git_common_dir, _normal_branch_ref, _symbolic_head, - _resolve_attempted_sha, _consume_option_value, _refspec_targets, - _same_repository, _resolve_remote_url, _local_path_from_remote_url + _resolve_attempted_sha, _refspec_targets, _same_repository, + _resolve_remote_url, _local_path_from_remote_url """ from __future__ import annotations @@ -46,107 +44,25 @@ from pathlib import Path from _command_classification import ( + GitInvocation, _command_position_candidate_spans, - _consume_str_flag, - _FlagArity, all_evaluated_segments, command_verb_and_args, extract_git_subcommand_and_flags, extract_interpreter_command_payloads, live_command_text, ) +from _git_outcome import ( + git_ref_outcome, + parse_fetch, + parse_push, + parse_reset, + parse_update_ref, +) from _github_mutation_analysis import ( _DYNAMIC_SHELL_TOKEN_RE, ) -# git fetch's own flag spec (distinct from _GIT_GLOBAL_FLAG_SPEC, git's -# top-level global flags), verified against a live `git fetch -h` read. -# Covers every fetch flag, including its `--no-X` negation form for each -# boolean flag git itself documents as `--[no-]X`. Used by _classify_fetch -# to correctly skip past an unrecognized fetch flag's value instead of -# misreading it as the remote/refspec positional. -_GIT_FETCH_BOOLEAN_FLAGS: frozenset[str] = frozenset( - { - "-v", - "--verbose", - "-q", - "--quiet", - "--all", - "--set-upstream", - "-a", - "--append", - "--atomic", - "-f", - "--force", - "-m", - "--multiple", - "-t", - "--tags", - "-n", - "--prefetch", - "-p", - "--prune", - "-P", - "--prune-tags", - "--dry-run", - "--porcelain", - "--write-fetch-head", - "-k", - "--keep", - "-u", - "--update-head-ok", - "--progress", - "--unshallow", - "--refetch", - "--update-shallow", - "-4", - "--ipv4", - "-6", - "--ipv6", - # --recurse-submodules[=] is optional-value; see - # _GIT_GLOBAL_FLAG_SPEC's --exec-path precedent for why BOOLEAN is - # the correct default (its `=`-form safely fails closed instead). - "--recurse-submodules", - "--negotiate-only", - "--auto-maintenance", - "--auto-gc", - "--show-forced-updates", - "--write-commit-graph", - "--stdin", - } -) -_GIT_FETCH_VALUE_FLAGS: frozenset[str] = frozenset( - { - "-j", - "--jobs", - "--depth", - "--shallow-since", - "--shallow-exclude", - "--deepen", - "--refmap", - "-o", - "--server-option", - "--negotiation-tip", - "--filter", - "--upload-pack", - } -) -_GIT_FETCH_FLAG_SPEC: dict[str, _FlagArity] = { - **{flag: _FlagArity.BOOLEAN for flag in _GIT_FETCH_BOOLEAN_FLAGS}, - **{ - f"--no-{flag.removeprefix('--')}": _FlagArity.BOOLEAN - for flag in _GIT_FETCH_BOOLEAN_FLAGS - if flag.startswith("--") - }, - **{flag: _FlagArity.VALUE for flag in _GIT_FETCH_VALUE_FLAGS}, - **{ - f"--no-{flag.removeprefix('--')}": _FlagArity.BOOLEAN - for flag in _GIT_FETCH_VALUE_FLAGS - if flag.startswith("--") - }, -} - - _DELIMITERS_RE = re.compile(r"[\s,\[\]'\"()]+") @@ -285,61 +201,27 @@ def _symbolic_head(cwd: str) -> str: def _resolve_attempted_sha(cwd: str, value: str) -> str: - if value in ("", "", ""): + if value in ("", "", "", ""): return "" return _git_text(cwd, "rev-parse", "--verify", f"{value}^{{commit}}") -def _consume_option_value(args: list[str], index: int, option: str) -> int: - return index + 2 if args[index] == option and index + 1 < len(args) else index + 1 - - -def _parse_update_ref_args( - args: list[str], -) -> tuple[bool, bool, str, str] | None: - no_deref = False - delete = False - positional: list[str] = [] - index = 0 - while index < len(args): - token = args[index] - if token == "--stdin": - return (True, False, "", "") - if token == "--no-deref": - no_deref = True - elif token in ("-d", "--delete"): - delete = True - elif token == "-m": - index = _consume_option_value(args, index, token) - continue - elif token == "--create-reflog": - # Boolean flag — do NOT consume the next token as its value. - index += 1 - continue - elif token.startswith("-"): - pass - else: - positional.append(token) - index += 1 - if not positional: - return None - target = positional[0] - attempted = "" if delete else (positional[1] if len(positional) > 1 else "") - if not attempted: - return None - return (False, no_deref, target, attempted) - - def _classify_update_ref(args: list[str], cwd: str) -> tuple[str, str, bool] | None: - parsed = _parse_update_ref_args(args) - if parsed is None: - return None - unresolved, no_deref, target, attempted = parsed - if unresolved: + parsed = parse_update_ref(args) + if parsed.unresolved is not None or parsed.stdin: return ("", "", True) + if not parsed.operands: + return None + target = parsed.operands[0] + if parsed.delete: + attempted = "" + elif len(parsed.operands) > 1: + attempted = parsed.operands[1] + else: + return None if _DYNAMIC_SHELL_TOKEN_RE.search(target) or _DYNAMIC_SHELL_TOKEN_RE.search(attempted): return ("", "", True) - if target == "HEAD" and no_deref: + if target == "HEAD" and parsed.no_deref: return ("HEAD", attempted, False) if target == "HEAD": target = _symbolic_head(cwd) @@ -348,53 +230,33 @@ def _classify_update_ref(args: list[str], cwd: str) -> tuple[str, str, bool] | N return (_normal_branch_ref(target), attempted, False) -def _classify_branch_position(subcommand: str, args: list[str]) -> tuple[str, str, bool] | None: - if subcommand == "branch": - if not any(token in ("-f", "--force") for token in args): - return None - # `git branch -f []` — `-f` is a flag that - # may appear anywhere in the arg list, so gather positionals - # across the whole list. Target is the first positional, start - # point (if any) is the second. - positional = [token for token in args if not token.startswith("-")] - if not positional: - return None - target = positional[0] - attempted = positional[1] if len(positional) > 1 else "HEAD" - else: - marker = "-B" if subcommand == "checkout" else "-C" - try: - marker_index = args.index(marker) - except ValueError: - return None - # `git checkout -B []` and `git switch -C - # []` — the marker MUST be followed by the - # new branch name (next positional token), but the start-point - # may appear anywhere else (before or after the marker). Do NOT - # conflate "any positional after marker" with "target", since - # start-points placed before the marker would silently bypass - # detection (`git checkout -B main` must still deny against - # `refs/heads/main`). - if marker_index + 1 >= len(args): - return None - target = args[marker_index + 1] - if target.startswith("-"): - return None - other_positionals = [ - token - for index, token in enumerate(args) - if index != marker_index + 1 and not token.startswith("-") - ] - attempted = other_positionals[0] if other_positionals else "HEAD" - if _DYNAMIC_SHELL_TOKEN_RE.search(target) or _DYNAMIC_SHELL_TOKEN_RE.search(attempted): - return ("", "", True) - return (_normal_branch_ref(target), attempted, False) +def _classify_outcome(subcommand: str, args: list[str], cwd: str) -> list[tuple[str, str, bool]]: + """Ref rewrites of a branch/checkout/switch/symbolic-ref argv. + + A HEAD effect with no ref rewrite (``checkout other``, ``checkout -b x``, + ``switch --detach``) yields nothing: Git itself refuses to check out a + branch another worktree has checked out, and a primary-HEAD move is the + cook rule's concern, not the shared-ref preflight's. + """ + outcome = git_ref_outcome(subcommand, args) + if outcome is None: + return [] + if outcome.ref_ambiguous: + return [("", "", True)] + if outcome.rewrites_current_branch: + return [(_symbolic_head(cwd) or "HEAD", "", False)] + if outcome.rewritten_ref is not None: + return [(_normal_branch_ref(outcome.rewritten_ref), outcome.start_point, False)] + return [] def _classify_reset(args: list[str], cwd: str) -> tuple[str, str, bool] | None: - if "--" in args or any(token.startswith("--pathspec-from-file") for token in args): + parsed = parse_reset(args) + if parsed.unresolved is not None: + return ("", "", True) + if parsed.path_mode: return None - attempted = next((token for token in args if not token.startswith("-")), "HEAD") + attempted = parsed.commit target = _symbolic_head(cwd) if not target: # Fail-closed: ambiguous on rev-parse failure — route to _all_threatened. @@ -427,33 +289,6 @@ def _refspec_targets(refspec: str, owned_refs: list[str]) -> list[str]: return [ref for ref in owned_refs if ref.startswith(prefix) and ref.endswith(suffix)] -def _parse_fetch_args(args: list[str]) -> tuple[list[str], list[str]] | None: - refmaps: list[str] = [] - positional: list[str] = [] - index = 0 - while index < len(args): - token = args[index] - if token == "--stdin": - return None - if token == "--refmap" and index + 1 < len(args): - refmaps.append(args[index + 1]) - index += 2 - continue - if token.startswith("--refmap="): - refmaps.append(token.split("=", 1)[1]) - index += 1 - continue - if token.startswith("-"): - _, next_index, recognized = _consume_str_flag(args, index, _GIT_FETCH_FLAG_SPEC) - if not recognized: - return None - index = next_index - continue - positional.append(token) - index += 1 - return refmaps, positional - - def _fetch_mappings( refmaps: list[str], command_refspecs: list[str], cwd: str, remote: str ) -> list[str] | None: @@ -476,15 +311,13 @@ def _fetch_mappings( def _classify_fetch( args: list[str], cwd: str, owned_refs: list[str] ) -> list[tuple[str, str, bool]]: - parsed = _parse_fetch_args(args) - if parsed is None: + fetch = parse_fetch(args) + if fetch.unresolved is not None: return [("", "", True)] - refmaps, positional = parsed - if not positional: + if fetch.remote is None: return [] - remote = positional[0] - command_refspecs = positional[1:] - mappings = _fetch_mappings(refmaps, command_refspecs, cwd, remote) + refmaps = [option.value or "" for option in fetch.parsed.options if option.name == "refmap"] + mappings = _fetch_mappings(refmaps, list(fetch.refspecs), cwd, fetch.remote) if mappings is None: return [("", "", True)] result: list[tuple[str, str, bool]] = [] @@ -563,10 +396,12 @@ def _same_repository(candidate: str, context: dict[str, object]) -> bool | None: def _classify_push( args: list[str], context: dict[str, object], owned_refs: list[str] ) -> list[tuple[str, str, bool]]: - positional = [token for token in args if not token.startswith("-")] - if not positional: + push = parse_push(args) + if push.unresolved is not None: + return [("", "", True)] + if push.remote is None: return [] - same_repo = _same_repository(positional[0], context) + same_repo = _same_repository(push.remote, context) if same_repo is False: return [] if same_repo is None: @@ -575,10 +410,10 @@ def _classify_push( # `git push origin`) uses remote.pushDefault / branch..merge to # resolve the target — we cannot enumerate without reading config, so # treat as ambiguous: route through _all_threatened rather than fail-open. - if len(positional) < 2: + if not push.refspecs: return [("", "", True)] result: list[tuple[str, str, bool]] = [] - for refspec in positional[1:]: + for refspec in push.refspecs: # Colonless refspec (e.g. `git push . feature-branch`) defaults to # `feature-branch:feature-branch` and still updates a checked-out # ref if it lands in owned_refs — normalize before classifying. @@ -592,37 +427,11 @@ def _classify_push( return result -def _classify_symbolic_ref(args: list[str]) -> tuple[str, str, bool] | None: - positional: list[str] = [] - index = 0 - while index < len(args): - token = args[index] - if token == "-m": - index += 2 - continue - if token.startswith("-"): - index += 1 - continue - positional.append(token) - index += 1 - if len(positional) >= 2 and positional[0] == "HEAD": - return (_normal_branch_ref(positional[1]), positional[1], False) - return None - - -def _classify_git_segment( - segment: list[str], context: dict[str, object] +def _classify_git_invocation( + invocation: GitInvocation, context: dict[str, object] ) -> list[tuple[str, str, bool]]: - parsed = extract_git_subcommand_and_flags(segment) - if parsed is None: - return [] - subcommand = parsed.subcommand - args = parsed.flags - if subcommand == "": - # An unrecognized global git flag means the real subcommand could - # not be found -- ambiguous-deny (route through _all_threatened - # against every owned ref), not "nothing to check here". - return [("", "", True)] + subcommand = invocation.subcommand + args = invocation.flags cwd = str(context["execution_cwd"]) owners = context["owners"] if not isinstance(owners, dict): @@ -631,14 +440,26 @@ def _classify_git_segment( one: tuple[str, str, bool] | None = None if subcommand == "update-ref": one = _classify_update_ref(args, cwd) - elif subcommand in ("branch", "checkout", "switch"): - one = _classify_branch_position(subcommand, args) + elif subcommand in ("branch", "checkout", "switch", "symbolic-ref"): + return _classify_outcome(subcommand, args, cwd) elif subcommand == "reset": one = _classify_reset(args, cwd) elif subcommand == "fetch": return _classify_fetch(args, cwd, owned_refs) elif subcommand == "push": return _classify_push(args, context, owned_refs) - elif subcommand == "symbolic-ref": - one = _classify_symbolic_ref(args) return [one] if one is not None else [] + + +def _classify_git_segment( + segment: list[str], context: dict[str, object] +) -> list[tuple[str, str, bool]]: + parsed = extract_git_subcommand_and_flags(segment) + if parsed is None: + return [] + if parsed.subcommand == "": + # An unrecognized global git flag means the real subcommand could + # not be found -- ambiguous-deny (route through _all_threatened + # against every owned ref), not "nothing to check here". + return [("", "", True)] + return _classify_git_invocation(parsed, context) diff --git a/src/autoskillit/hooks/_runtime/_git_option_tables.py b/src/autoskillit/hooks/_runtime/_git_option_tables.py new file mode 100644 index 0000000000..baa744dfd5 --- /dev/null +++ b/src/autoskillit/hooks/_runtime/_git_option_tables.py @@ -0,0 +1,368 @@ +"""Option tables for the Git subcommands whose argv the guard classifies. + +Members: _GIT_CHECKOUT_FLAG_SPEC, _GIT_SWITCH_FLAG_SPEC, _GIT_BRANCH_FLAG_SPEC, +_GIT_SYMBOLIC_REF_FLAG_SPEC, _GIT_UPDATE_REF_FLAG_SPEC, _GIT_RESET_FLAG_SPEC, +_GIT_PUSH_FLAG_SPEC, _GIT_FETCH_FLAG_SPEC, _GIT_COMMIT_FLAG_SPEC, +_GIT_CLEAN_FLAG_SPEC, _SED_FLAG_SPEC, _GIT_SUBCOMMAND_TABLES. + +Data only. Rows come from ``git -h`` and ``git +--git-completion-helper-all`` on ``_GIT_TABLES_REFERENCE_VERSION``, including +hidden and short-only rows. An arity must agree with the live completion +helper: a trailing ``=`` there means REQUIRED, and a ``--no-`` after its +bare ``--`` means the row is negatable. + +branch's ``merged``, ``no-merged``, ``contains`` and ``no-contains`` are +OPTIONAL although Git declares them ``PARSE_OPT_LASTARG_DEFAULT`` (the next +token is their value unless they are last, so ``branch --contains --list`` +exits 129). The engine has no fourth arity for that, and the divergence never +changes a verdict because every list-mode form classifies as no ref outcome. +""" + +from __future__ import annotations + +from collections.abc import Mapping +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from autoskillit.hooks._classification._option_grammar import OptionArity, OptionDef +else: + if __package__: + from .._classification import _option_grammar + else: + from _classification import _option_grammar + + OptionArity = _option_grammar.OptionArity + OptionDef = _option_grammar.OptionDef + +_GIT_TABLES_REFERENCE_VERSION: tuple[int, int] = (2, 43) + +_NONE = OptionArity.NONE +_REQUIRED = OptionArity.REQUIRED +_OPTIONAL = OptionArity.OPTIONAL + + +def _opt( + name: str, + arity: OptionArity = _NONE, + *, + short: str | None = None, + negatable: bool = True, +) -> OptionDef: + return OptionDef(name, short, arity, negatable, True) + + +def _short(letter: str, arity: OptionArity = _NONE) -> OptionDef: + return OptionDef(letter, letter, arity, False, False) + + +_GIT_CHECKOUT_FLAG_SPEC = { + row.name: row + for row in ( + _short("b", _REQUIRED), + _short("B", _REQUIRED), + _short("l"), + _opt("guess"), + _opt("overlay"), + _opt("quiet", short="q"), + _opt("recurse-submodules", _OPTIONAL), + _opt("progress"), + _opt("merge", short="m"), + _opt("conflict", _REQUIRED), + _opt("detach", short="d"), + _opt("track", _OPTIONAL, short="t"), + _opt("force", short="f"), + _opt("orphan", _REQUIRED), + _opt("overwrite-ignore"), + _opt("ignore-other-worktrees"), + _opt("ours", short="2", negatable=False), + _opt("theirs", short="3", negatable=False), + _opt("patch", short="p"), + _opt("ignore-skip-worktree-bits"), + _opt("pathspec-from-file", _REQUIRED), + _opt("pathspec-file-nul"), + ) +} + +_GIT_SWITCH_FLAG_SPEC = { + row.name: row + for row in ( + _opt("create", _REQUIRED, short="c"), + _opt("force-create", _REQUIRED, short="C"), + _opt("guess"), + _opt("discard-changes"), + _opt("quiet", short="q"), + _opt("recurse-submodules", _OPTIONAL), + _opt("progress"), + _opt("merge", short="m"), + _opt("conflict", _REQUIRED), + _opt("detach", short="d"), + _opt("track", _OPTIONAL, short="t"), + _opt("force", short="f"), + _opt("orphan", _REQUIRED), + _opt("overwrite-ignore"), + _opt("ignore-other-worktrees"), + ) +} + +_GIT_BRANCH_FLAG_SPEC = { + row.name: row + for row in ( + _opt("verbose", short="v"), + _opt("quiet", short="q"), + _opt("track", _OPTIONAL, short="t"), + _opt("set-upstream"), + _opt("set-upstream-to", _REQUIRED, short="u"), + _opt("unset-upstream"), + _opt("color", _OPTIONAL), + _opt("remotes", short="r", negatable=False), + _opt("contains", _OPTIONAL, negatable=False), + _opt("no-contains", _OPTIONAL, negatable=False), + _opt("with", _OPTIONAL, negatable=False), + _opt("without", _OPTIONAL, negatable=False), + _opt("abbrev", _OPTIONAL), + _opt("all", short="a", negatable=False), + _opt("delete", short="d"), + _short("D"), + _opt("move", short="m"), + _short("M"), + _opt("omit-empty"), + _opt("copy", short="c"), + _short("C"), + _opt("list", short="l"), + _opt("show-current"), + _opt("create-reflog"), + _opt("edit-description"), + _opt("force", short="f"), + _opt("merged", _OPTIONAL, negatable=False), + _opt("no-merged", _OPTIONAL, negatable=False), + _opt("column", _OPTIONAL), + _opt("sort", _REQUIRED), + _opt("points-at", _REQUIRED), + _opt("ignore-case", short="i"), + _opt("recurse-submodules"), + _opt("format", _REQUIRED), + ) +} + +_GIT_SYMBOLIC_REF_FLAG_SPEC = { + row.name: row + for row in ( + _opt("quiet", short="q"), + _opt("delete", short="d"), + _opt("short"), + _opt("recurse"), + _short("m", _REQUIRED), + ) +} + +_GIT_UPDATE_REF_FLAG_SPEC = { + row.name: row + for row in ( + _short("m", _REQUIRED), + _short("d"), + _short("z"), + _opt("no-deref", negatable=False), + _opt("deref", negatable=False), + _opt("stdin"), + _opt("create-reflog"), + ) +} + +_GIT_RESET_FLAG_SPEC = { + row.name: row + for row in ( + _opt("quiet", short="q"), + _opt("no-refresh", negatable=False), + _opt("refresh", negatable=False), + _opt("mixed", negatable=False), + _opt("soft", negatable=False), + _opt("hard", negatable=False), + _opt("merge", negatable=False), + _opt("keep", negatable=False), + _opt("recurse-submodules", _OPTIONAL), + _opt("patch", short="p"), + _opt("intent-to-add", short="N"), + _opt("pathspec-from-file", _REQUIRED), + _opt("pathspec-file-nul"), + ) +} + +_GIT_PUSH_FLAG_SPEC = { + row.name: row + for row in ( + _opt("verbose", short="v"), + _opt("quiet", short="q"), + _opt("repo", _REQUIRED), + _opt("all"), + _opt("branches"), + _opt("mirror"), + _opt("delete", short="d"), + _opt("tags"), + _opt("dry-run", short="n"), + _opt("porcelain"), + _opt("force", short="f"), + _opt("force-with-lease", _OPTIONAL), + _opt("force-if-includes"), + _opt("recurse-submodules", _REQUIRED), + _opt("thin"), + _opt("receive-pack", _REQUIRED), + _opt("exec", _REQUIRED), + _opt("set-upstream", short="u"), + _opt("progress"), + _opt("prune"), + _opt("no-verify", negatable=False), + _opt("verify", negatable=False), + _opt("follow-tags"), + _opt("signed", _OPTIONAL), + _opt("atomic"), + _opt("push-option", _REQUIRED, short="o"), + _opt("ipv4", short="4", negatable=False), + _opt("ipv6", short="6", negatable=False), + ) +} + +_GIT_FETCH_FLAG_SPEC = { + row.name: row + for row in ( + _opt("verbose", short="v"), + _opt("quiet", short="q"), + _opt("all"), + _opt("set-upstream"), + _opt("append", short="a"), + _opt("atomic"), + _opt("upload-pack", _REQUIRED), + _opt("force", short="f"), + _opt("multiple", short="m"), + _opt("tags", short="t"), + _short("n"), + _opt("jobs", _REQUIRED, short="j"), + _opt("prefetch"), + _opt("prune", short="p"), + _opt("prune-tags", short="P"), + _opt("recurse-submodules", _OPTIONAL), + _opt("dry-run"), + _opt("porcelain"), + _opt("write-fetch-head"), + _opt("keep", short="k"), + _opt("update-head-ok", short="u"), + _opt("progress"), + _opt("depth", _REQUIRED), + _opt("shallow-since", _REQUIRED), + _opt("shallow-exclude", _REQUIRED), + _opt("deepen", _REQUIRED), + _opt("unshallow", negatable=False), + _opt("refetch", negatable=False), + _opt("submodule-prefix", _REQUIRED), + _opt("recurse-submodules-default", _REQUIRED), + _opt("update-shallow"), + _opt("refmap", _REQUIRED, negatable=False), + _opt("server-option", _REQUIRED, short="o"), + _opt("ipv4", short="4", negatable=False), + _opt("ipv6", short="6", negatable=False), + _opt("negotiation-tip", _REQUIRED), + _opt("negotiate-only"), + _opt("filter", _REQUIRED), + _opt("auto-maintenance"), + _opt("auto-gc"), + _opt("show-forced-updates"), + _opt("write-commit-graph"), + _opt("stdin"), + ) +} + +_GIT_COMMIT_FLAG_SPEC = { + row.name: row + for row in ( + _opt("quiet", short="q"), + _opt("verbose", short="v"), + _opt("file", _REQUIRED, short="F"), + _opt("author", _REQUIRED), + _opt("date", _REQUIRED), + _opt("message", _REQUIRED, short="m"), + _opt("reedit-message", _REQUIRED, short="c"), + _opt("reuse-message", _REQUIRED, short="C"), + _opt("fixup", _REQUIRED), + _opt("squash", _REQUIRED), + _opt("reset-author"), + _opt("trailer", _REQUIRED, negatable=False), + _opt("signoff", short="s"), + _opt("template", _REQUIRED, short="t"), + _opt("edit", short="e"), + _opt("cleanup", _REQUIRED), + _opt("status"), + _opt("gpg-sign", _OPTIONAL, short="S"), + _opt("all", short="a"), + _opt("include", short="i"), + _opt("interactive"), + _opt("patch", short="p"), + _opt("only", short="o"), + _opt("no-verify", short="n", negatable=False), + _opt("verify", negatable=False), + _opt("dry-run"), + _opt("short"), + _opt("branch"), + _opt("ahead-behind"), + _opt("porcelain"), + _opt("long"), + _opt("null", short="z"), + _opt("amend"), + _opt("no-post-rewrite", negatable=False), + _opt("post-rewrite", negatable=False), + _opt("untracked-files", _OPTIONAL, short="u"), + _opt("pathspec-from-file", _REQUIRED), + _opt("pathspec-file-nul"), + _opt("allow-empty"), + _opt("allow-empty-message"), + ) +} + +_GIT_CLEAN_FLAG_SPEC = { + row.name: row + for row in ( + _opt("quiet", short="q"), + _opt("dry-run", short="n"), + _opt("force", short="f"), + _opt("interactive", short="i"), + _opt("exclude", _REQUIRED, short="e", negatable=False), + _short("d"), + _short("x"), + _short("X"), + ) +} + +_SED_FLAG_SPEC = { + row.name: row + for row in ( + _opt("quiet", short="n", negatable=False), + _opt("silent", negatable=False), + _short("E"), + _opt("regexp-extended", short="r", negatable=False), + _opt("separate", short="s", negatable=False), + _opt("unbuffered", short="u", negatable=False), + _opt("null-data", short="z", negatable=False), + _opt("binary", short="b", negatable=False), + _opt("posix", negatable=False), + _opt("debug", negatable=False), + _opt("sandbox", negatable=False), + _opt("follow-symlinks", negatable=False), + _opt("help", negatable=False), + _opt("version", negatable=False), + _opt("expression", _REQUIRED, short="e", negatable=False), + _opt("file", _REQUIRED, short="f", negatable=False), + _opt("line-length", _REQUIRED, short="l", negatable=False), + _opt("in-place", _OPTIONAL, short="i", negatable=False), + ) +} + +_GIT_SUBCOMMAND_TABLES: Mapping[str, Mapping[str, OptionDef]] = { + "checkout": _GIT_CHECKOUT_FLAG_SPEC, + "switch": _GIT_SWITCH_FLAG_SPEC, + "branch": _GIT_BRANCH_FLAG_SPEC, + "symbolic-ref": _GIT_SYMBOLIC_REF_FLAG_SPEC, + "update-ref": _GIT_UPDATE_REF_FLAG_SPEC, + "reset": _GIT_RESET_FLAG_SPEC, + "push": _GIT_PUSH_FLAG_SPEC, + "fetch": _GIT_FETCH_FLAG_SPEC, + "commit": _GIT_COMMIT_FLAG_SPEC, + "clean": _GIT_CLEAN_FLAG_SPEC, +} diff --git a/src/autoskillit/hooks/_runtime/_git_outcome.py b/src/autoskillit/hooks/_runtime/_git_outcome.py new file mode 100644 index 0000000000..1e77a663cd --- /dev/null +++ b/src/autoskillit/hooks/_runtime/_git_outcome.py @@ -0,0 +1,367 @@ +"""Git command outcomes: what an argv does, not which tokens it contains. + +Members: GitHeadEffect, GitRefOutcome, git_ref_outcome, UpdateRefArgs, +parse_update_ref, ResetArgs, parse_reset, PushArgs, parse_push, FetchArgs, +parse_fetch, is_ref_mutation. + +Every classification reads options through the ``_option_grammar`` engine over +the tables in ``_git_option_tables``, so a stuck, bundled, abbreviated or +reordered spelling means what it means to Git. ``git_ref_outcome`` maps +branch, checkout, switch and symbolic-ref argv to the branch ref a command +rewrites and the effect on ``HEAD``; the other parsers expose the operands the +classifiers in ``_git_command_classification`` need. + +Expansion word-splitting into options (``git checkout $FLAGS``) is outside this +bounded classification; a dynamic deciding operand is reported ambiguous. +""" + +from __future__ import annotations + +from collections.abc import Sequence +from enum import StrEnum +from typing import TYPE_CHECKING, NamedTuple + +from _git_option_tables import ( + _GIT_BRANCH_FLAG_SPEC, + _GIT_CHECKOUT_FLAG_SPEC, + _GIT_FETCH_FLAG_SPEC, + _GIT_PUSH_FLAG_SPEC, + _GIT_RESET_FLAG_SPEC, + _GIT_SWITCH_FLAG_SPEC, + _GIT_SYMBOLIC_REF_FLAG_SPEC, + _GIT_UPDATE_REF_FLAG_SPEC, +) +from _github_mutation_analysis import _DYNAMIC_SHELL_TOKEN_RE + +if TYPE_CHECKING: + from autoskillit.hooks._classification._option_grammar import ( + ParsedArgv, + ParsedOption, + last_option, + parse_options, + ) +else: + if __package__: + from .._classification import _option_grammar + else: + from _classification import _option_grammar + + ParsedArgv = _option_grammar.ParsedArgv + ParsedOption = _option_grammar.ParsedOption + last_option = _option_grammar.last_option + parse_options = _option_grammar.parse_options + + +class GitHeadEffect(StrEnum): + NONE = "none" + MOVES = "moves" + PATH_RESTORE = "path_restore" + AMBIGUOUS = "ambiguous" + + +class GitRefOutcome(NamedTuple): + rewritten_ref: str | None + rewrites_current_branch: bool + start_point: str + ref_ambiguous: bool + head_effect: GitHeadEffect + + +def _dynamic(token: str) -> bool: + return bool(_DYNAMIC_SHELL_TOKEN_RE.search(token)) + + +def _active(parsed: ParsedArgv, name: str) -> ParsedOption | None: + """The last occurrence of *name*, unless a later ``--no-`` negates it.""" + option = last_option(parsed, name) + return option if option is not None and not option.negated else None + + +def _head(effect: GitHeadEffect) -> GitRefOutcome: + return GitRefOutcome(None, False, "HEAD", False, effect) + + +def _rewrite(ref: str, start_point: str, effect: GitHeadEffect) -> GitRefOutcome: + ambiguous = _dynamic(ref) or _dynamic(start_point) + return GitRefOutcome(ref, False, start_point, ambiguous, effect) + + +def _ambiguous_outcome() -> GitRefOutcome: + return GitRefOutcome(None, False, "HEAD", True, GitHeadEffect.AMBIGUOUS) + + +def _single_operand_effect(operand: str) -> GitHeadEffect: + """Effect of ``git checkout `` with no ``--``. + + A branch, a remote-tracking guess, a commit and a path need repository + queries to tell apart, so only the unambiguous spellings are decided. + """ + if _dynamic(operand): + return GitHeadEffect.AMBIGUOUS + if operand == "-" or "..." in operand or (operand.startswith("@{-") and operand.endswith("}")): + return GitHeadEffect.MOVES + # ``:/`` is the top-of-tree pathspec; ``:/.`` and other ``:/`` are + # revisions naming a commit by message, so they fall through. + if operand in (".", ":/") or operand.startswith(("./", "/")) or "/./" in operand: + return GitHeadEffect.PATH_RESTORE + return GitHeadEffect.AMBIGUOUS + + +def _dashdash_effect(before: int, after: int) -> GitHeadEffect: + """Effect of ``git checkout -- ``.""" + if before > 1: + return GitHeadEffect.AMBIGUOUS + if after: + return GitHeadEffect.PATH_RESTORE + return GitHeadEffect.NONE if before == 0 else GitHeadEffect.MOVES + + +def _checkout_operand_effect(parsed: ParsedArgv) -> GitHeadEffect: + operands = parsed.operands + before = parsed.operands_before_dashdash + if before is not None: + return _dashdash_effect(before, len(operands) - before) + if not operands: + return GitHeadEffect.NONE + if len(operands) >= 2: + return GitHeadEffect.PATH_RESTORE + return _single_operand_effect(operands[0]) + + +def _checkout_outcome(args: Sequence[str]) -> GitRefOutcome: + parsed = parse_options(args, _GIT_CHECKOUT_FLAG_SPEC) + if parsed.unresolved: + return _ambiguous_outcome() + operands = parsed.operands + forced = _active(parsed, "B") + if forced is not None: + return _rewrite( + forced.value or "", operands[0] if operands else "HEAD", GitHeadEffect.MOVES + ) + if any(_active(parsed, name) for name in ("b", "orphan", "detach", "track")): + return _head(GitHeadEffect.MOVES) + if _active(parsed, "pathspec-from-file"): + return _head(GitHeadEffect.AMBIGUOUS) + if any(_active(parsed, name) for name in ("patch", "ours", "theirs")): + return _head(GitHeadEffect.PATH_RESTORE) + return _head(_checkout_operand_effect(parsed)) + + +def _switch_outcome(args: Sequence[str]) -> GitRefOutcome: + parsed = parse_options(args, _GIT_SWITCH_FLAG_SPEC) + if parsed.unresolved: + return _ambiguous_outcome() + operands = parsed.operands + forced = _active(parsed, "force-create") + if forced is not None: + return _rewrite( + forced.value or "", operands[0] if operands else "HEAD", GitHeadEffect.MOVES + ) + if any(_active(parsed, name) for name in ("create", "orphan", "detach", "track")): + return _head(GitHeadEffect.MOVES) + if not operands: + return _head(GitHeadEffect.NONE) + return _head(GitHeadEffect.MOVES if len(operands) == 1 else GitHeadEffect.AMBIGUOUS) + + +_BRANCH_NON_REWRITING_MODES = ( + "delete", + "D", + "list", + "all", + "remotes", + "show-current", + "set-upstream-to", + "set-upstream", + "unset-upstream", + "edit-description", + "contains", + "no-contains", + "with", + "without", + "merged", + "no-merged", +) + + +def _branch_outcome(args: Sequence[str]) -> GitRefOutcome | None: + parsed = parse_options(args, _GIT_BRANCH_FLAG_SPEC) + if parsed.unresolved: + return _ambiguous_outcome() + operands = parsed.operands + if _active(parsed, "move") or _active(parsed, "M"): + if len(operands) == 1: + return GitRefOutcome(None, True, "HEAD", False, GitHeadEffect.MOVES) + if len(operands) == 2: + return _rewrite(operands[0], "HEAD", GitHeadEffect.NONE) + return None + if _active(parsed, "copy") or _active(parsed, "C"): + if 1 <= len(operands) <= 2: + return _rewrite(operands[-1], operands[0], GitHeadEffect.NONE) + return None + if any(_active(parsed, name) for name in _BRANCH_NON_REWRITING_MODES): + return None + if _active(parsed, "force") and operands: + return _rewrite( + operands[0], operands[1] if len(operands) > 1 else "HEAD", GitHeadEffect.NONE + ) + return None + + +def _symbolic_ref_outcome(args: Sequence[str]) -> GitRefOutcome | None: + parsed = parse_options(args, _GIT_SYMBOLIC_REF_FLAG_SPEC) + if parsed.unresolved: + return _ambiguous_outcome() + operands = parsed.operands + if _active(parsed, "delete") and operands: + return _rewrite(operands[0], "HEAD", GitHeadEffect.NONE) + if len(operands) < 2: + return None + if _dynamic(operands[0]): + return _ambiguous_outcome() + if operands[0] == "HEAD": + return _rewrite(operands[1], operands[1], GitHeadEffect.MOVES) + return _rewrite(operands[0], operands[1], GitHeadEffect.NONE) + + +def git_ref_outcome(subcommand: str, args: Sequence[str]) -> GitRefOutcome | None: + """The ref a branch/checkout/switch/symbolic-ref argv rewrites and its HEAD effect. + + ``None`` means the argv rewrites no branch ref and has no HEAD effect to + report (list, delete and similar modes). + """ + if subcommand == "checkout": + return _checkout_outcome(args) + if subcommand == "switch": + return _switch_outcome(args) + if subcommand == "branch": + return _branch_outcome(args) + if subcommand == "symbolic-ref": + return _symbolic_ref_outcome(args) + return None + + +class UpdateRefArgs(NamedTuple): + stdin: bool + delete: bool + no_deref: bool + operands: tuple[str, ...] + unresolved: str | None + + +def parse_update_ref(args: Sequence[str]) -> UpdateRefArgs: + parsed = parse_options(args, _GIT_UPDATE_REF_FLAG_SPEC) + deref = next( + (option for option in reversed(parsed.options) if option.name in ("deref", "no-deref")), + None, + ) + return UpdateRefArgs( + stdin=_active(parsed, "stdin") is not None, + delete=_active(parsed, "d") is not None, + no_deref=deref is not None and deref.name == "no-deref", + operands=parsed.operands, + unresolved=parsed.unresolved, + ) + + +class ResetArgs(NamedTuple): + mode: str | None + path_mode: bool + commit: str + unresolved: str | None + + +def parse_reset(args: Sequence[str]) -> ResetArgs: + parsed = parse_options(args, _GIT_RESET_FLAG_SPEC) + unresolved = parsed.unresolved + if unresolved is None and _active(parsed, "pathspec-from-file"): + unresolved = "pathspec_from_file" + operands = parsed.operands + before = parsed.operands_before_dashdash + modes = ("soft", "mixed", "hard", "merge", "keep") + mode = next((o.name for o in reversed(parsed.options) if o.name in modes), None) + path_mode = ( + _active(parsed, "patch") is not None + or (before is not None and len(operands) > before) + or len(operands) >= 2 + ) + has_commit = bool(operands) and before != 0 + return ResetArgs(mode, path_mode, operands[0] if has_commit else "HEAD", unresolved) + + +def _remote_and_refspecs( + parsed: ParsedArgv, repo: ParsedOption | None +) -> tuple[str | None, tuple[str, ...]]: + operands = parsed.operands + # A positional repository overrides --repo (builtin/push.c). + if operands: + return operands[0], operands[1:] + return (repo.value, ()) if repo is not None else (None, ()) + + +class PushArgs(NamedTuple): + parsed: ParsedArgv + remote: str | None + refspecs: tuple[str, ...] + unresolved: str | None + + +def parse_push(args: Sequence[str]) -> PushArgs: + parsed = parse_options(args, _GIT_PUSH_FLAG_SPEC) + remote, refspecs = _remote_and_refspecs(parsed, _active(parsed, "repo")) + return PushArgs(parsed, remote, refspecs, parsed.unresolved) + + +class FetchArgs(NamedTuple): + parsed: ParsedArgv + remote: str | None + refspecs: tuple[str, ...] + unresolved: str | None + + +def parse_fetch(args: Sequence[str]) -> FetchArgs: + parsed = parse_options(args, _GIT_FETCH_FLAG_SPEC) + unresolved = parsed.unresolved + if unresolved is None and _active(parsed, "stdin"): + unresolved = "stdin" + remote, refspecs = _remote_and_refspecs(parsed, None) + return FetchArgs(parsed, remote, refspecs, unresolved) + + +def _path_like_remote(remote: str) -> bool: + return remote == "." or remote.startswith(("/", "./", "../", "file://")) or _dynamic(remote) + + +def is_ref_mutation(subcommand: str, args: Sequence[str]) -> bool: + """Whether the command can rewrite a local branch ref, for an unknown cwd. + + Used when the repository the command acts on cannot be resolved. A push or + fetch to a *named* remote does not count: it cannot write local refs. + """ + if subcommand == "" or _dynamic(subcommand): + return True + if subcommand == "update-ref": + return True + if subcommand == "reset": + reset = parse_reset(args) + return reset.unresolved is not None or not reset.path_mode + if subcommand in ("branch", "checkout", "switch", "symbolic-ref"): + outcome = git_ref_outcome(subcommand, args) + return outcome is not None and ( + outcome.rewritten_ref is not None + or outcome.rewrites_current_branch + or outcome.ref_ambiguous + ) + if subcommand == "push": + push = parse_push(args) + return push.unresolved is not None or ( + push.remote is not None and _path_like_remote(push.remote) + ) + if subcommand == "fetch": + fetch = parse_fetch(args) + return ( + fetch.unresolved is not None + or _active(fetch.parsed, "update-head-ok") is not None + or any(":" in refspec for refspec in fetch.refspecs) + ) + return False diff --git a/src/autoskillit/hooks/guards/AGENTS.md b/src/autoskillit/hooks/guards/AGENTS.md index 94ee3f2d92..9d24dd17ab 100644 --- a/src/autoskillit/hooks/guards/AGENTS.md +++ b/src/autoskillit/hooks/guards/AGENTS.md @@ -17,6 +17,10 @@ The package initializer remains import-free. worktree in the same Git common directory. It derives repository identity and command cwd from the parsed tool payload plus read-only Git resolution; vendor `session_id` and `transcript_path` values are diagnostic only. Detached worktrees own no branch ref. + Branch, checkout, switch, symbolic-ref, update-ref, reset, push and fetch are + classified by outcome (which branch ref a command rewrites and whether `HEAD` + moves), read through Git's own option semantics (bundled, stuck, abbreviated and + negated spellings), never by matching option tokens. In headless skill sessions, the later legacy Git phase blocks `commit --amend`, `push --force`, `reset --hard`, `clean -f`, and `checkout .`. Raw GitHub review publication is fail-closed across Bash and `run_cmd`: the guard denies @@ -81,7 +85,7 @@ defense-in-depth measure against privilege escalation: | `github_mutation_guard.py` | Ambiguous or unresolved GitHub mutation command | Unknown mutation scope must not bypass the structured review publisher | | `exploration_request_identity_guard.py` | A supported exploration event lacks bounded native identity or its one-shot record cannot be written | A Claude exploration call must not execute without request-correlated authority | | `join_stop_guard.py` | Malformed/non-object payload or missing `session_id` | Stop must not release when the guard cannot identify the binding whose wave completion authorizes release | -| `git_ops_guard.py` | Unexpected runtime error during the checked-out-ref preflight (OSError, subprocess.SubprocessError, TypeError, UnicodeDecodeError, ValueError); or, in the separate headless destructive-op-blocking preflight, an unrecognized global git flag that leaves the real subcommand unresolved, or a Python subprocess/os call whose argument could not be resolved to a literal argv or string while `live_command_text` still mentions "git" | An unhandled exception must not silently allow a checked-out ref mutation — use exit 2 + stderr to hard-block. `_git_command_classification._contains_blocked_git_op` reads the command exclusively through `all_evaluated_segments`, the evaluated-segment blocklist authority (rectify #4941 Part A): it cannot match `_BLOCKED_GIT_OPS`'s literal subcommand tuples against an unresolved subcommand, so it denies unconditionally the moment `extract_git_subcommand_and_flags` reports `""`, and separately denies when an unresolved interpreter payload cannot rule out a hidden `git` invocation, rather than silently falling through to "not blocked" | +| `git_ops_guard.py` | Any unexpected runtime error (any `Exception`) while classifying a Git command, in the checked-out-ref preflight or the headless destructive-op phase; or, in the separate headless destructive-op-blocking preflight, an unrecognized global git flag that leaves the real subcommand unresolved, or a Python subprocess/os call whose argument could not be resolved to a literal argv or string while `live_command_text` still mentions "git" | An unhandled exception must not silently allow a checked-out ref mutation — use exit 2 + stderr to hard-block. `_git_command_classification._contains_blocked_git_op` reads the command exclusively through `all_evaluated_segments`, the evaluated-segment blocklist authority (rectify #4941 Part A): it cannot match `_BLOCKED_GIT_OPS`'s literal subcommand tuples against an unresolved subcommand, so it denies unconditionally the moment `extract_git_subcommand_and_flags` reports `""`, and separately denies when an unresolved interpreter payload cannot rule out a hidden `git` invocation, rather than silently falling through to "not blocked" | | `pr_create_guard.py` | Hook config unreadable or malformed while the kitchen is open (OSError, JSONDecodeError, AttributeError, TypeError) | An unresolvable `recipe_allows_pr_create` authorization must not be read as permission to bypass the prepare_pr → compose_pr pipeline | | `unsafe_install_guard.py` | An unrecognized global pip flag leaves `pip`'s `install` token position unresolved | `_find_pip_install` cannot tell whether the command is a pip install at all; treating that the same as "definitely not an install" would silently skip the editable/system-install checks entirely, so it is threaded through as a distinct `"unresolved-pip-flags"` kind and denied unconditionally, matching the pre-existing `"unresolved-subprocess"` kind's treatment | | `write_guard.py` | A write-verb operand or redirect target cannot be resolved, or a Bash command cannot be parsed, while a write-prefix policy applies | An unresolved target could escape the allowed prefix; the denial explains how to substitute a literal path. | diff --git a/src/autoskillit/hooks/guards/git_ops_guard.py b/src/autoskillit/hooks/guards/git_ops_guard.py index 30508e2a91..87ae71657e 100644 --- a/src/autoskillit/hooks/guards/git_ops_guard.py +++ b/src/autoskillit/hooks/guards/git_ops_guard.py @@ -10,9 +10,9 @@ from __future__ import annotations import json +import logging import os import re -import subprocess import sys from collections import deque from pathlib import Path @@ -77,6 +77,7 @@ GIT_OPS_DENY_TRIGGER: str = DENY_TRIGGER_BY_GUARD["git_ops_guard"] CHECKED_OUT_REF_DENY_PREFIX: str = "Checked-out ref mutation blocked: " +_LOGGER = logging.getLogger(__name__) # noqa: TID251 - standalone stdlib guard _DENY_REASON_TEMPLATE = DENY_REASON_BY_GUARD["git_ops_guard"] @@ -584,57 +585,51 @@ def main() -> None: if not cmd: sys.exit(0) - project_root = resolve_state_root(parsed.payload_cwd) + # Any unexpected classification error denies. SystemExit raised by the + # deny helpers is BaseException, not Exception, so it bypasses this + # handler and serves as the explicit verdict signal. try: - cfg_path = project_root / ".autoskillit" / "temp" / ".hook_config.json" - kitchen_open = cfg_path.exists() - except OSError: - sys.exit(0) - - if kitchen_open: + project_root = resolve_state_root(parsed.payload_cwd) try: - _preflight_checked_out_ref_mutation(data, cmd, parsed.execution_cwd) - except ( - OSError, - subprocess.SubprocessError, - TypeError, - UnicodeDecodeError, - ValueError, - ) as exc: - # Fail-closed on preflight exception. SystemExit raised by - # _deny_checked_out_ref is BaseException, not Exception, so it - # bypasses this handler and serves as the explicit deny signal. - block(f"git_ops_guard: preflight failed: {exc}") - - headless, tier = hook_session_shape() - if not headless: - sys.exit(0) - - blocked = _contains_blocked_git_op(cmd, _BLOCKED_GIT_OPS) - if blocked is None: - sys.exit(0) - - skill_name = os.environ.get("AUTOSKILLIT_SKILL_NAME", "") - if skill_name in _EXEMPT_SKILLS: - sys.exit(0) - - # Orchestrator-typed headless sessions bypass the destructive-op deny - # but remain subject to the all-session ref preflight above (which runs - # unconditionally before this branch). - if tier in _DESTRUCTIVE_OP_EXEMPT_TIERS: - sys.exit(0) + cfg_path = project_root / ".autoskillit" / "temp" / ".hook_config.json" + kitchen_open = cfg_path.exists() + except OSError: + sys.exit(0) - # Hook config file is written by open_kitchen and removed by close_kitchen. - # Its presence reliably signals an open kitchen without needing session ID. - if not kitchen_open: - sys.exit(0) - - if _git_op_policy_allows(project_root, blocked): - sys.exit(0) + if kitchen_open: + _preflight_checked_out_ref_mutation(data, cmd, parsed.execution_cwd) - op_str = " ".join(("git",) + blocked) - deny_reason = _DENY_REASON_TEMPLATE.format(op=op_str) - deny_tool_use(deny_reason) + headless, tier = hook_session_shape() + if not headless: + sys.exit(0) + + blocked = _contains_blocked_git_op(cmd, _BLOCKED_GIT_OPS) + if blocked is None: + sys.exit(0) + + skill_name = os.environ.get("AUTOSKILLIT_SKILL_NAME", "") + if skill_name in _EXEMPT_SKILLS: + sys.exit(0) + + # Orchestrator-typed headless sessions bypass the destructive-op deny + # but remain subject to the all-session ref preflight above (which runs + # unconditionally before this branch). + if tier in _DESTRUCTIVE_OP_EXEMPT_TIERS: + sys.exit(0) + + # Hook config file is written by open_kitchen and removed by close_kitchen. + # Its presence reliably signals an open kitchen without needing session ID. + if not kitchen_open: + sys.exit(0) + + if _git_op_policy_allows(project_root, blocked): + sys.exit(0) + + op_str = " ".join(("git",) + blocked) + deny_tool_use(_DENY_REASON_TEMPLATE.format(op=op_str)) + except Exception as exc: + _LOGGER.error("git_ops_guard classification failed", exc_info=True) + block(f"git_ops_guard: {type(exc).__name__}: {exc}") if __name__ == "__main__": diff --git a/tests/_test_filter.py b/tests/_test_filter.py index 72dcf0e516..c4efdb6224 100644 --- a/tests/_test_filter.py +++ b/tests/_test_filter.py @@ -157,7 +157,7 @@ class SourceMapRejection(enum.StrEnum): "tests/fleet/_reaper_test_support.py": frozenset({"fleet"}), "tests/hooks/_interactive_guard_harness.py": frozenset({"hooks", "skills"}), "tests/_git_topology.py": frozenset( - {"core", "execution", "server", "integration", "workspace", "fleet"} + {"core", "execution", "server", "integration", "workspace", "fleet", "hooks", "infra"} ), "tests/fleet/_codex_mcp_env.py": frozenset({"fleet", "integration"}), "tests/fleet/_descendant_worker.py": frozenset({"fleet"}), diff --git a/tests/arch/test_guard_flag_spec_coverage.py b/tests/arch/test_guard_flag_spec_coverage.py index 8a75e016ef..e312f84037 100644 --- a/tests/arch/test_guard_flag_spec_coverage.py +++ b/tests/arch/test_guard_flag_spec_coverage.py @@ -4,8 +4,13 @@ _CURL_FLAG_SPEC in hooks/_github_mutation_analysis.py; _GIT_GLOBAL_FLAG_SPEC and _PIP_GLOBAL_FLAG_SPEC in hooks/_command_classification.py; _SHELL_INVOCATION_FLAG_SPEC and _PYTHON_INVOCATION_FLAG_SPEC in -hooks/_classification/_interpreters.py, rectify #4941 Part A) must have a -corresponding test parametrized directly from the table's own keys (e.g. +hooks/_classification/_interpreters.py, rectify #4941 Part A; and the Git and +sed option tables in hooks/_runtime/_git_option_tables.py: _GIT_CHECKOUT_FLAG_SPEC, +_GIT_SWITCH_FLAG_SPEC, _GIT_BRANCH_FLAG_SPEC, _GIT_SYMBOLIC_REF_FLAG_SPEC, +_GIT_UPDATE_REF_FLAG_SPEC, _GIT_RESET_FLAG_SPEC, _GIT_PUSH_FLAG_SPEC, +_GIT_FETCH_FLAG_SPEC, _GIT_COMMIT_FLAG_SPEC, _GIT_CLEAN_FLAG_SPEC and +_SED_FLAG_SPEC) must have a corresponding test parametrized directly from +the table's own keys (e.g. `@pytest.mark.parametrize("flag", sorted(_GH_API_FLAG_SPEC))`), not a hand-maintained flag list a future table update could silently drift out of sync with -- adding a flag to the table then automatically extends the @@ -14,7 +19,8 @@ This mirrors tests/arch/test_fail_closed_guard_contract.py's AST-scan and batch-report shape: a structural test about the test suite's own completeness, not about runtime behavior (the generative tests themselves, -in tests/hooks/test_command_classification.py and +in tests/hooks/test_command_classification.py, +tests/hooks/test_git_option_tables.py and tests/infra/test_unsafe_install_guard.py, exercise runtime behavior). """ @@ -37,10 +43,20 @@ "_GH_ISSUE_EDIT_FLAG_SPEC": ("hooks/test_command_classification.py",), "_CURL_FLAG_SPEC": ("hooks/test_command_classification.py",), "_GIT_GLOBAL_FLAG_SPEC": ("hooks/test_command_classification.py",), - "_GIT_FETCH_FLAG_SPEC": ("infra/test_git_ops_guard.py",), "_PIP_GLOBAL_FLAG_SPEC": ("infra/test_unsafe_install_guard.py",), "_SHELL_INVOCATION_FLAG_SPEC": ("hooks/test_command_classification.py",), "_PYTHON_INVOCATION_FLAG_SPEC": ("hooks/test_command_classification.py",), + "_GIT_CHECKOUT_FLAG_SPEC": ("hooks/test_git_option_tables.py",), + "_GIT_SWITCH_FLAG_SPEC": ("hooks/test_git_option_tables.py",), + "_GIT_BRANCH_FLAG_SPEC": ("hooks/test_git_option_tables.py",), + "_GIT_SYMBOLIC_REF_FLAG_SPEC": ("hooks/test_git_option_tables.py",), + "_GIT_UPDATE_REF_FLAG_SPEC": ("hooks/test_git_option_tables.py",), + "_GIT_RESET_FLAG_SPEC": ("hooks/test_git_option_tables.py",), + "_GIT_PUSH_FLAG_SPEC": ("hooks/test_git_option_tables.py",), + "_GIT_FETCH_FLAG_SPEC": ("hooks/test_git_option_tables.py",), + "_GIT_COMMIT_FLAG_SPEC": ("hooks/test_git_option_tables.py",), + "_GIT_CLEAN_FLAG_SPEC": ("hooks/test_git_option_tables.py",), + "_SED_FLAG_SPEC": ("hooks/test_git_option_tables.py",), } diff --git a/tests/arch/test_subpackage_isolation_facades.py b/tests/arch/test_subpackage_isolation_facades.py index f4cb7a0ad6..5ed98866ea 100644 --- a/tests/arch/test_subpackage_isolation_facades.py +++ b/tests/arch/test_subpackage_isolation_facades.py @@ -274,6 +274,8 @@ def test_hooks_runtime_decomposition_has_expected_siblings() -> None: "_hook_output", "_guard_decision_diagnostics", "_git_command_classification", + "_git_option_tables", + "_git_outcome", "_path_identity", "_session_registry_bridge", "_write_target_scan", diff --git a/tests/hooks/test_git_effect_differential.py b/tests/hooks/test_git_effect_differential.py new file mode 100644 index 0000000000..85ce27cae2 --- /dev/null +++ b/tests/hooks/test_git_effect_differential.py @@ -0,0 +1,858 @@ +"""Real-Git differential soundness of the guard's ref-effect classification. + +A generated Git argv is classified by the guard's own machinery, run for real +in a repository with a primary and a linked worktree, and Git's observable +effect on every worktree is compared with what the classifier predicted. + +Members: ``_build_base`` and ``_sandbox`` (fixture repositories), +``_run_differential`` (the shared driver returning a ``_Run``), ``_git_cases`` +(argv strategy), and the Property A section (shared refs). +""" + +from __future__ import annotations + +import contextlib +import os +import shlex +import shutil +import subprocess +from collections.abc import Callable, Iterator, Mapping +from dataclasses import dataclass +from pathlib import Path +from typing import NamedTuple + +import pytest + +# The sys.path bootstrap for bare-name hook-sibling imports is centralized in +# tests/conftest.py -- it must run before this module's own top-level imports, +# which a fixture cannot do. +from _classification._option_grammar import ( # type: ignore[import-not-found] + OptionArity, + OptionDef, +) +from _git_option_tables import _GIT_SUBCOMMAND_TABLES # type: ignore[import-not-found] +from _git_outcome import ( # type: ignore[import-not-found] + GitHeadEffect, + GitRefOutcome, + git_ref_outcome, +) +from hypothesis import event, example, given, settings +from hypothesis import strategies as st + +from autoskillit.hooks.guards import git_ops_guard +from tests._git_topology import commit_file, init_empty_checkout + +pytestmark = [ + pytest.mark.layer("hooks"), + pytest.mark.medium, + pytest.mark.skipif(shutil.which("git") is None, reason="git is not installed"), +] + +_GIT_TIMEOUT_SECONDS = 30 + +# --------------------------------------------------------------------------- +# Repository fixtures +# --------------------------------------------------------------------------- + +_STRIPPED_VARIABLES = ( + "GIT_DIR", + "GIT_WORK_TREE", + "GIT_COMMON_DIR", + "GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", +) +_COMMIT_DATE = "2026-01-01T00:00:00+0000" + + +def _pinned_variables(home: Path) -> dict[str, str]: + return { + "GIT_CONFIG_GLOBAL": os.devnull, + "GIT_CONFIG_NOSYSTEM": "1", + "HOME": str(home), + "XDG_CONFIG_HOME": str(home / ".config"), + "XDG_DATA_HOME": str(home / ".local" / "share"), + "XDG_CACHE_HOME": str(home / ".cache"), + "GIT_AUTHOR_NAME": "Differential Test", + "GIT_AUTHOR_EMAIL": "differential@example.invalid", + "GIT_AUTHOR_DATE": _COMMIT_DATE, + "GIT_COMMITTER_NAME": "Differential Test", + "GIT_COMMITTER_EMAIL": "differential@example.invalid", + "GIT_COMMITTER_DATE": _COMMIT_DATE, + "GIT_EDITOR": "true", + "GIT_TERMINAL_PROMPT": "0", + "LC_ALL": "C", + } + + +@contextlib.contextmanager +def _pinned_git_environment(home: Path) -> Iterator[None]: + """Pin the process environment every Git subprocess in this module inherits. + + The classifier's own Git queries and ``tests/_git_topology.py`` helpers read + ``os.environ``, so the pinned environment must be the process environment, + not a per-call dict. + """ + with pytest.MonkeyPatch.context() as patch: + for name in _STRIPPED_VARIABLES: + patch.delenv(name, raising=False) + for name, value in _pinned_variables(home).items(): + patch.setenv(name, value) + yield + + +def _git(cwd: Path, *args: str, check: bool = True) -> subprocess.CompletedProcess[str]: + """Run Git under the environment pinned by ``_pinned_git_environment``.""" + return subprocess.run( + ["git", *args], + cwd=cwd, + check=check, + capture_output=True, + text=True, + errors="replace", + stdin=subprocess.DEVNULL, + timeout=_GIT_TIMEOUT_SECONDS, + ) + + +@dataclass(frozen=True, slots=True) +class _Base: + tree: Path + home: Path + sha_a: str + sha_b: str + git_version: str + + +@dataclass(frozen=True, slots=True) +class _Sandbox: + primary: Path + linked: Path + sha_a: str + sha_b: str + git_version: str + + @property + def worktrees(self) -> dict[str, Path]: + return {"primary": self.primary, "linked": self.linked} + + +def _build_base(root: Path) -> _Base: + """Build the repository copied for every example: no linked worktree yet. + + ``develop`` holds commits A and B. ``feat`` diverges from A, ``other`` is + ahead of ``develop`` and ``review`` sits at A, free to be checked out by the + per-example linked worktree. ``y`` exists only on ``origin``, and + ``origin/develop`` is stale behind the remote's ``develop``, so a fetch has + something to move. The reflog records ``other`` as the previous branch, so + ``-`` and ``@{-1}`` resolve. + """ + home = root / "home" + home.mkdir(parents=True) + tree = root / "tree" + with _pinned_git_environment(home): + primary = init_empty_checkout(tree / "primary") + _git(primary, "symbolic-ref", "HEAD", "refs/heads/develop") + sha_a = commit_file(primary, "file.txt", "a\n", "A") + sha_b = commit_file(primary, "d/x", "x\n", "B") + _git(primary, "branch", "review", sha_a) + _git(primary, "checkout", "-q", "-b", "feat", sha_a) + commit_file(primary, "file.txt", "feat\n", "feat commit") + _git(primary, "checkout", "-q", "-b", "other", "develop") + commit_file(primary, "d/x", "other\n", "other commit") + _git(primary, "checkout", "-q", "develop") + (primary / "empty.lst").touch() + # Git's defaults refuse pushes that update or delete a checked-out branch + # and ignore push options; relaxing them lets `push .` actually rewrite a + # checked-out ref, which is what the classifier must not miss. + origin = tree / "origin.git" + _git(tree, "clone", "-q", "--bare", str(primary), str(origin)) + for repo in (primary, origin): + for key, value in ( + ("receive.denyCurrentBranch", "ignore"), + ("receive.denyDeleteCurrent", "ignore"), + ("receive.advertisePushOptions", "true"), + ): + _git(repo, "config", key, value) + _git(origin, "branch", "y", sha_a) + _git(primary, "remote", "add", "origin", str(origin)) + _git(primary, "fetch", "-q", "origin") + _git(primary, "branch", "-q", "-u", "origin/develop", "develop") + other_sha = _git(primary, "rev-parse", "other").stdout.strip() + _git(origin, "update-ref", "refs/heads/develop", other_sha) + git_version = _git(primary, "--version").stdout.strip() + return _Base(tree, home, sha_a, sha_b, git_version) + + +@contextlib.contextmanager +def _sandbox(base: _Base, make_dir: Callable[[str], Path]) -> Iterator[_Sandbox]: + """A private copy of the base with its own linked worktree on ``review``. + + The copied repository's remote and worktree links are absolute, so the + remote is repointed and the linked worktree recreated; otherwise a mutation + would leak into the shared base. + """ + root = make_dir("differential") + try: + with _pinned_git_environment(base.home): + shutil.copytree(base.tree, root, symlinks=True, dirs_exist_ok=True) + primary = root / "primary" + linked = root / "linked" + _git(primary, "remote", "set-url", "origin", str(root / "origin.git")) + _git(primary, "worktree", "add", "-q", str(linked), "review") + yield _Sandbox(primary, linked, base.sha_a, base.sha_b, base.git_version) + finally: + shutil.rmtree(root, ignore_errors=True) + + +# --------------------------------------------------------------------------- +# Driver: classify, run, observe +# --------------------------------------------------------------------------- + +_SHA_A = "" +_SHA_B = "" + +_Case = tuple[str, tuple[str, ...]] +_Tuples = tuple[tuple[str, str, bool], ...] +_RefValue = tuple[str, str] + + +class _View(NamedTuple): + head_ref: str | None + head_sha: str | None + # None when the worktree is no longer a repository, e.g. after its HEAD is deleted. + branches: Mapping[str, _RefValue] | None + + +def _observe_worktree(worktree: Path) -> _View: + head_ref = _git(worktree, "symbolic-ref", "-q", "HEAD", check=False).stdout.strip() + head_sha = _git(worktree, "rev-parse", "--verify", "-q", "HEAD", check=False).stdout.strip() + listing = _git( + worktree, + "for-each-ref", + "--format=%(refname)%09%(objectname)%09%(symref)", + "refs/heads", + check=False, + ) + if listing.returncode: + return _View(head_ref or None, head_sha or None, None) + branches: dict[str, _RefValue] = {} + for line in listing.stdout.splitlines(): + ref, sha, symref = line.split("\t") + branches[ref] = (sha, symref) + return _View(head_ref or None, head_sha or None, branches) + + +def _observe(sandbox: _Sandbox) -> dict[str, _View]: + return {label: _observe_worktree(path) for label, path in sandbox.worktrees.items()} + + +@dataclass(frozen=True, slots=True) +class _Run: + subcommand: str + argv: tuple[str, ...] + returncode: int + stderr: str + owners: tuple[str, ...] + tuples: _Tuples + outcome: GitRefOutcome | None + before: Mapping[str, _View] + after: Mapping[str, _View] + git_version: str + + @property + def command(self) -> str: + return shlex.join(["git", self.subcommand, *self.argv]) + + @property + def named_refs(self) -> set[str]: + return {target for target, _, _ in self.tuples} + + @property + def ambiguous(self) -> bool: + return any(flag for _, _, flag in self.tuples) + + @property + def checked_out_ref_changes(self) -> dict[str, tuple[_RefValue | None, _RefValue | None]]: + """Branches some worktree had checked out whose value changed or vanished.""" + changes: dict[str, tuple[_RefValue | None, _RefValue | None]] = {} + for label, before in self.before.items(): + after = self.after[label] + if before.branches is None or after.branches is None: + continue + for ref in self.owners: + if ref not in changes and before.branches.get(ref) != after.branches.get(ref): + changes[ref] = (before.branches.get(ref), after.branches.get(ref)) + return changes + + @property + def head_changes(self) -> list[tuple[str, _View, _View]]: + """Worktrees whose HEAD name, attachment or identity changed.""" + return [ + (label, view, self.after[label]) + for label, view in self.before.items() + if (view.head_ref, view.head_sha) + != (self.after[label].head_ref, self.after[label].head_sha) + ] + + +def _run_differential(sandbox: _Sandbox, subcommand: str, case_argv: tuple[str, ...]) -> _Run: + """Classify ``git `` with the guard, then run it for real.""" + argv = tuple( + token.replace(_SHA_A, sandbox.sha_a).replace(_SHA_B, sandbox.sha_b) for token in case_argv + ) + context = git_ops_guard._repository_context(str(sandbox.primary)) + assert context is not None, f"no repository context for {sandbox.primary}" + owners = context["owners"] + assert isinstance(owners, dict) + tuples = tuple(git_ops_guard._classify_git_segment(["git", subcommand, *argv], context)) + before = _observe(sandbox) + completed = _git(sandbox.primary, subcommand, *argv, check=False) + after = _observe(sandbox) + return _Run( + subcommand=subcommand, + argv=argv, + returncode=completed.returncode, + stderr=completed.stderr, + owners=tuple(sorted(owners)), + tuples=tuples, + outcome=git_ref_outcome(subcommand, argv), + before=before, + after=after, + git_version=sandbox.git_version, + ) + + +# --------------------------------------------------------------------------- +# Argv strategy +# --------------------------------------------------------------------------- + +_SUBCOMMANDS = ( + "checkout", + "switch", + "branch", + "symbolic-ref", + "update-ref", + "reset", + "push", + "fetch", +) + +_BRANCH_OPERANDS = ( + "develop", + "feat", + "other", + "review", + "y", + _SHA_A, + "HEAD~1", + "file.txt", + ".", + "-", + "@{-1}", + "new-0", + "new-1", + "new-2", + "refs/heads/feat", + "HEAD:develop", + "develop:develop", +) +_FULL_REFS = ( + "HEAD", + "refs/heads/develop", + "refs/heads/feat", + "refs/heads/other", + "refs/heads/review", + "refs/heads/new-0", +) +_COMMITS = (_SHA_A, _SHA_B, "develop", "feat", "other", "HEAD", "HEAD~1") +_PATHS = ("file.txt", "d/x", ".") +_REMOTES = (".", "origin") +_REFSPECS = ( + "develop", + "feat", + "HEAD:develop", + "develop:develop", + "other:develop", + "feat:review", + "other:review", + "+feat:develop", + "+other:review", + "+feat:review", + f"{_SHA_A}:refs/heads/review", + ":feat", + "HEAD:refs/heads/new-0", +) +_OPERAND_SLOTS: Mapping[str, tuple[tuple[str, ...], ...]] = { + "checkout": (_BRANCH_OPERANDS,) * 3, + "switch": (_BRANCH_OPERANDS,) * 2, + "branch": (_BRANCH_OPERANDS,) * 3, + "symbolic-ref": (_FULL_REFS, _FULL_REFS), + "update-ref": (_FULL_REFS, _COMMITS, _COMMITS), + "reset": (_COMMITS, _PATHS, _PATHS), + "push": (_REMOTES, _REFSPECS, _REFSPECS), + "fetch": (_REMOTES, _REFSPECS, _REFSPECS), +} + +_BRANCHES = ("develop", "feat", "other", "review", "y") +_NEW = ("new-0", "new-1", "new-2") +_START = (_SHA_A, _SHA_B, "feat", "other", "HEAD~1", "origin/y") +_TARGETS = (*_BRANCHES, "-", "@{-1}", "develop...other", "refs/heads/feat", _SHA_A, "HEAD~1") +_SYMREFS = ("refs/heads/develop", "refs/heads/feat", "refs/heads/review", "refs/heads/new-0") +_Skeleton = tuple[tuple[str, ...], int, tuple[tuple[str, ...], ...]] + + +def _sk(rows: str, minimum: int, *pools: tuple[str, ...]) -> _Skeleton: + """A well-formed invocation shape: option rows to spell, then operand pools. + + ``rows`` lists space-separated table rows, each a ``|``-separated choice; + between *minimum* and all of the pools supply operands. + """ + return tuple(rows.split()), minimum, pools + + +# Most generated argv should be accepted by Git, because a rejected command +# changes nothing and proves nothing. +_SKELETONS: Mapping[str, tuple[_Skeleton, ...]] = { + "checkout": ( + _sk("", 0, _TARGETS), + _sk("b", 0, _START), + _sk("B", 1, _START), + _sk("detach", 0, _START), + _sk("orphan", 0), + _sk("b track", 1, ("origin/y", "origin/develop")), + _sk("force|ignore-other-worktrees B", 1, _START), + _sk("pathspec-from-file|force|merge|quiet", 0, _BRANCHES), + _sk("ours|theirs|patch", 0, _PATHS), + _sk("", 2, _START, _PATHS), + ), + "switch": ( + _sk("", 1, _TARGETS), + _sk("create", 0, _START), + _sk("force-create", 1, _START), + _sk("detach", 0, _START), + _sk("orphan", 0), + _sk("track", 1, ("origin/y",)), + _sk("force|ignore-other-worktrees|merge|guess", 1, _BRANCHES), + _sk("force-create force", 1, _START), + ), + "branch": ( + _sk("move|M|copy|C", 1, _BRANCHES, _NEW), + _sk("force move|M", 1, _BRANCHES, _NEW), + _sk("force", 1, (*_BRANCHES, *_NEW), _START), + _sk("", 1, _NEW, _START), + _sk("delete|D", 1, _BRANCHES), + _sk("list|all|remotes|verbose|show-current|merged|contains|quiet", 0, _BRANCHES), + _sk("set-upstream-to|unset-upstream|edit-description", 0, _BRANCHES), + ), + "symbolic-ref": ( + _sk("", 2, ("HEAD",), _SYMREFS), + _sk("m", 2, ("HEAD",), _SYMREFS), + _sk("", 2, _SYMREFS, _SYMREFS), + _sk("delete", 1, ("HEAD", *_SYMREFS)), + _sk("quiet|short", 1, ("HEAD",)), + ), + "update-ref": ( + _sk("", 2, _FULL_REFS, _COMMITS, _COMMITS), + _sk("m", 2, _FULL_REFS, _COMMITS), + _sk("d", 1, _FULL_REFS, _COMMITS), + _sk("no-deref|create-reflog", 2, ("HEAD",), _COMMITS), + ), + "reset": ( + _sk("soft|mixed|hard|keep|merge", 0, _COMMITS), + _sk("", 0, _COMMITS), + _sk("", 2, _COMMITS, _PATHS), + _sk("patch|quiet|refresh", 0, _COMMITS), + ), + "push": ( + _sk("", 2, _REMOTES, _REFSPECS, _REFSPECS), + _sk( + "force|dry-run|set-upstream|atomic|porcelain|force-with-lease|quiet", + 2, + _REMOTES, + _REFSPECS, + ), + _sk("push-option force|follow-tags", 2, _REMOTES, _REFSPECS), + _sk("delete", 2, _REMOTES, _BRANCHES), + _sk("repo", 2, _REMOTES, _REFSPECS), + _sk("all|tags|mirror|prune", 1, _REMOTES), + ), + "fetch": ( + _sk("", 0, _REMOTES, _REFSPECS, _REFSPECS), + _sk("update-head-ok|force", 2, _REMOTES, _REFSPECS), + _sk("refmap", 2, _REMOTES, ("develop", "feat", "review")), + _sk("prune|tags|dry-run|all|quiet|verbose|atomic", 0, _REMOTES), + ), +} + +_NEW_BRANCHES = ("develop", "feat", "other", "review", "new-0", "new-1") +_REWRITTEN_BRANCHES = ("develop", "review", "feat", "other", "new-0") +_OPTION_VALUES: Mapping[str, tuple[str, ...]] = { + "b": _NEW_BRANCHES, + "B": _REWRITTEN_BRANCHES, + "orphan": _NEW_BRANCHES, + "create": _NEW_BRANCHES, + "force-create": _REWRITTEN_BRANCHES, + "conflict": ("merge", "diff3"), + "track": ("direct", "inherit"), + "recurse-submodules": ("no",), + "pathspec-from-file": ("empty.lst",), + "m": ("why",), + "set-upstream-to": ("origin/develop",), + "color": ("never",), + "abbrev": ("7",), + "column": ("never",), + "sort": ("refname",), + "points-at": ("HEAD",), + "format": ("%(refname)",), + "contains": ("HEAD", "feat"), + "no-contains": ("HEAD", "feat"), + "with": ("HEAD", "feat"), + "without": ("HEAD", "feat"), + "merged": ("HEAD", "feat"), + "no-merged": ("HEAD", "feat"), + "repo": _REMOTES, + "force-with-lease": ("develop",), + "signed": ("no",), + "push-option": ("x",), + "server-option": ("x",), + "receive-pack": ("git-receive-pack",), + "exec": ("git-receive-pack",), + "upload-pack": ("git-upload-pack",), + "jobs": ("1",), + "depth": ("1",), + "deepen": ("1",), + "shallow-since": ("2000-01-01",), + "shallow-exclude": ("develop",), + "recurse-submodules-default": ("no",), + "refmap": ("refs/heads/*:refs/heads/*", "refs/heads/feat:refs/heads/feat"), + "negotiation-tip": ("HEAD",), + "filter": ("blob:none",), +} + + +def _accepted_prefixes(table: Mapping[str, OptionDef], spelling: str) -> list[str]: + """The abbreviations of *spelling* Git resolves to it: unique, or the exact name.""" + candidates = [row.name for row in table.values() if row.has_long] + candidates += [f"no-{row.name}" for row in table.values() if row.has_long and row.negatable] + return [ + spelling[:length] + for length in range(1, len(spelling) + 1) + if length == len(spelling) + or sum(candidate.startswith(spelling[:length]) for candidate in candidates) == 1 + ] + + +def _short_forms(row: OptionDef, bundle: str, value: str) -> list[list[str]]: + flag = f"-{bundle}{row.short}" + if row.arity == OptionArity.NONE: + return [[flag]] + if row.arity == OptionArity.REQUIRED: + return [[flag, value], [f"{flag}{value}"]] + return [[flag], [f"{flag}{value}"]] + + +def _long_forms(row: OptionDef, spelling: str, negated: bool, value: str) -> list[list[str]]: + flag = f"--{spelling}" + if negated or row.arity == OptionArity.NONE: + return [[flag]] + if row.arity == OptionArity.REQUIRED: + return [[f"{flag}={value}"], [flag, value]] + return [[flag], [f"{flag}={value}"]] + + +def _short_categories( + table: Mapping[str, OptionDef], row: OptionDef, value: str +) -> tuple[list[list[str]], list[list[str]]]: + """Plain short spellings of *row*, and the same bundled behind boolean shorts.""" + if not row.short: + return [], [] + bundled: list[list[str]] = [] + for other in table.values(): + if other.short and other.arity == OptionArity.NONE and other.short != row.short: + bundled.extend(_short_forms(row, other.short, value)) + return _short_forms(row, "", value), bundled + + +def _long_categories( + table: Mapping[str, OptionDef], row: OptionDef, value: str +) -> tuple[list[list[str]], list[list[str]], list[list[str]]]: + """Full, abbreviated and ``--no-`` spellings of *row*'s long option.""" + full: list[list[str]] = [] + abbreviated: list[list[str]] = [] + negated: list[list[str]] = [] + if not row.has_long: + return full, abbreviated, negated + for spelling in _accepted_prefixes(table, row.name): + (full if spelling == row.name else abbreviated).extend( + _long_forms(row, spelling, False, value) + ) + if row.negatable: + for spelling in _accepted_prefixes(table, f"no-{row.name}"): + negated.extend(_long_forms(row, spelling, True, value)) + return full, abbreviated, negated + + +def _spelling_categories( + table: Mapping[str, OptionDef], row: OptionDef, value: str +) -> list[list[list[str]]]: + """Argv spellings of *row* grouped by kind: short, bundled, long, abbreviated, negated.""" + short, bundled = _short_categories(table, row, value) + full, abbreviated, negated = _long_categories(table, row, value) + return [short, bundled, full, abbreviated, negated] + + +@st.composite +def _spelled_row(draw: st.DrawFn, subcommand: str, name: str) -> list[str]: + table = _GIT_SUBCOMMAND_TABLES[subcommand] + row = table[name] + value = draw(st.sampled_from(_OPTION_VALUES.get(name, ("x",)))) + category = draw(st.sampled_from([c for c in _spelling_categories(table, row, value) if c])) + return draw(st.sampled_from(category)) + + +@st.composite +def _git_cases(draw: st.DrawFn) -> _Case: + """A subcommand with options and operands in any order and an optional ``--``. + + Three in four draws start from a well-formed skeleton and add at most one + stray option; the rest are free-form combinations of table rows and operands. + """ + subcommand = draw(st.sampled_from(_SUBCOMMANDS)) + table = _GIT_SUBCOMMAND_TABLES[subcommand] + structured = draw(st.sampled_from((True, True, True, False))) + if structured: + rows, minimum, pools = draw(st.sampled_from(_SKELETONS[subcommand])) + stray = 1 + else: + rows, minimum, pools, stray = (), 0, _OPERAND_SLOTS[subcommand], 3 + operands = pools[: draw(st.integers(minimum, len(pools)))] + items = [(False, [draw(st.sampled_from(pool))]) for pool in operands] + groups = [ + draw(_spelled_row(subcommand, draw(st.sampled_from(alternatives.split("|"))))) + for alternatives in rows + ] + stray_option = st.sampled_from(sorted(table)).flatmap( + lambda name: _spelled_row(subcommand, name) + ) + groups += draw(st.lists(stray_option, max_size=stray)) + for group in groups: + items.insert(draw(st.integers(0, len(items))), (True, group)) + if draw(st.sampled_from((False, False, True))): + # Only operands may follow ``--``, so a skeleton keeps its options in front. + floor = 1 + max((i for i, (is_option, _) in enumerate(items) if is_option), default=-1) + items.insert(draw(st.integers(floor if structured else 0, len(items))), (False, ["--"])) + return subcommand, tuple(token for _, group in items for token in group) + + +# --------------------------------------------------------------------------- +# Property A: shared refs +# --------------------------------------------------------------------------- + +_HEAD_STABLE_EFFECTS = (GitHeadEffect.NONE, GitHeadEffect.PATH_RESTORE) +_OUTCOME_SUBCOMMANDS = ("branch", "checkout", "switch", "symbolic-ref") + +_PINNED_CASES: tuple[_Case, ...] = ( + # Spellings that rewrite a branch ref (develop is checked out in the primary). + ("checkout", ("-Bdevelop", _SHA_A)), + ("checkout", ("-fB", "develop", _SHA_A)), + ("checkout", ("--ignore-other-worktrees", "-Bdevelop")), + ("checkout", ("--ignore-other-worktrees", "-Bdevelop", "feat")), + ("checkout", ("feat", "-B", "develop")), + ("checkout", (_SHA_A, "-B", "develop")), + ("checkout", ("-B", "review", _SHA_B)), + ("switch", ("-Cdevelop",)), + ("switch", ("-C", "develop", "feat")), + ("switch", ("-fC", "develop", "feat")), + ("switch", ("--force-create=develop", "feat")), + ("switch", ("--force-c", "develop", "feat")), + ("switch", ("--force-create", "develop", "feat")), + ("branch", ("-m", "develop", "renamed")), + ("branch", ("-M", "develop", "renamed")), + ("branch", ("--move", "review", "renamed")), + ("branch", ("-fm", "feat", "renamed")), + ("branch", ("-m", "renamed")), + ("branch", ("-c", "feat", "copy")), + ("branch", ("-f", "feat", _SHA_B)), + ("branch", ("feat", _SHA_B, "-f")), + ("branch", ("-fq", "feat")), + ("branch", ("--forc", "feat", _SHA_B)), + ("symbolic-ref", ("HEAD", "refs/heads/feat")), + ("symbolic-ref", ("-q", "HEAD", "refs/heads/feat")), + ("symbolic-ref", ("-m", "why", "HEAD", "refs/heads/feat")), + ("symbolic-ref", ("-mwhy", "HEAD", "refs/heads/feat")), + ("symbolic-ref", ("refs/heads/new-0", "refs/heads/feat")), + ("update-ref", ("-dm", "why", "refs/heads/develop")), + ("update-ref", ("-d", "refs/heads/review")), + ("update-ref", ("refs/heads/review", _SHA_B)), + ("update-ref", ("--no-deref", "HEAD", _SHA_A)), + ("update-ref", ("-d", "--no-deref", "HEAD", _SHA_B)), + ("update-ref", ("HEAD", _SHA_A)), + ("reset", ("--hard", _SHA_A)), + ("reset", ("--soft", "HEAD~1")), + ("reset", ("--har", "other")), + ("push", ("-o", "x", ".", "HEAD:develop")), + ("push", ("-o", "x", ".", "other:develop")), + ("push", (".", "other:review")), + ("push", ("--repo=origin", ".", "other:develop")), + ("push", ("-d", ".", "review")), + ("fetch", ("-u", ".", "other:develop")), + ("fetch", ("--update-head-ok", ".", "other:review")), + ("fetch", ("-u", "origin", "develop:develop")), + # Head effects with no rewrite of a checked-out ref. + ("checkout", ("-b", "new-1", _SHA_A)), + ("checkout", ("-fb", "new-1")), + ("checkout", ("-bnew-1",)), + ("checkout", ("--orphan", "new-1")), + ("checkout", ("--orph=new-1",)), + ("checkout", ("-d",)), + ("checkout", ("--detach",)), + ("checkout", ("-t", "origin/y")), + ("checkout", ("-b", "new-1", "-t", "origin/y")), + ("checkout", ("y",)), + ("switch", ("y",)), + ("checkout", ("develop", "--")), + ("checkout", ("-",)), + ("checkout", ("@{-1}",)), + ("checkout", ("develop...other",)), + ("checkout", ("other", "--pathspec-from-file=empty.lst")), + ("checkout", ("--detach", "--no-detach", "other")), + ("switch", ("-c", "new-1")), + ("switch", ("-cnew-1",)), + ("switch", ("--create=new-1",)), + ("switch", ("feat",)), + ("switch", ("-",)), + ("switch", ("--detach",)), + ("switch", ("-d",)), + # NONE and PATH_RESTORE outcomes leave every HEAD and branch alone. + ("checkout", ()), + ("checkout", ("-f",)), + ("checkout", ("-q",)), + ("checkout", ("--", "file.txt")), + ("checkout", ("develop", "--", "file.txt")), + ("checkout", ("feat", "file.txt")), + ("checkout", (".",)), + ("checkout", (":/",)), + ("checkout", ("-p",)), + ("switch", ()), + ("branch", ()), + ("branch", ("-a",)), + ("branch", ("--list",)), + ("branch", ("-vv",)), + ("branch", ("--show-current",)), + ("branch", ("-d", "feat")), + ("branch", ("-D", "feat")), + ("branch", ("-D", "review")), + ("branch", ("--contains",)), + ("branch", ("--merged",)), + ("branch", ("--no-merged",)), + ("symbolic-ref", ("HEAD",)), + ("reset", ("--", "file.txt")), + ("reset", ("-p",)), + ("fetch", ("origin",)), + ("push", ("origin", "develop:develop")), +) + + +def _with_pinned_cases(test: Callable[..., None]) -> Callable[..., None]: + for case in reversed(_PINNED_CASES): + test = example(case=case)(test) + return test + + +def _head_effect(run: _Run) -> GitHeadEffect | None: + """The classifier's HEAD effect; ``None`` where no outcome concept applies. + + An absent outcome for the four outcome-classified subcommands means the argv + rewrites no branch and has no HEAD effect. + """ + if run.subcommand not in _OUTCOME_SUBCOMMANDS: + return None + return run.outcome.head_effect if run.outcome is not None else GitHeadEffect.NONE + + +def _describe_ref(value: _RefValue | None) -> str: + if value is None: + return "absent" + sha, symref = value + return f"symref {symref}" if symref else sha[:7] + + +def _describe_head(view: _View) -> str: + sha = (view.head_sha or "unborn")[:7] + return f"{view.head_ref or 'detached'}@{sha}" + + +def _shared_ref_violations(run: _Run) -> list[str]: + """Where the classifier under-reports what Git did to shared refs. + + A checked-out branch that changed must be named by a tuple, or a tuple must + be ambiguous. When the HEAD effect is NONE or PATH_RESTORE, no HEAD may + change except that of a worktree sitting on a branch the command rewrites + by name: renaming a checked-out branch rewrites that worktree's HEAD symref + as part of the rewrite, which the named tuple already accounts for. + """ + problems: list[str] = [] + if not run.ambiguous: + for ref, (before, after) in run.checked_out_ref_changes.items(): + if ref not in run.named_refs: + problems.append( + f"checked-out {ref} changed {_describe_ref(before)} -> " + f"{_describe_ref(after)} but is not named" + ) + if _head_effect(run) in _HEAD_STABLE_EFFECTS: + for label, before_view, after_view in run.head_changes: + if before_view.head_ref not in run.named_refs: + problems.append( + f"HEAD effect {_head_effect(run)} but {label} HEAD changed " + f"{_describe_head(before_view)} -> {_describe_head(after_view)}" + ) + return problems + + +def _failure_message(run: _Run, problems: list[str]) -> str: + return ( + f"Property A violated by `{run.command}` (exit {run.returncode})\n" + " expected: every checked-out branch that changed is named by a classifier tuple " + "or a tuple is ambiguous; a NONE/PATH_RESTORE HEAD effect changes no HEAD\n" + f" actual: {'; '.join(problems)}\n" + f" classifier tuples: {list(run.tuples)}\n" + f" outcome: {run.outcome}\n" + f" git stderr: {run.stderr.strip()!r}\n" + f" {run.git_version}" + ) + + +def _over_block_label(run: _Run) -> str | None: + """A low-cardinality label when the guard would deny although nothing changed.""" + denies = run.ambiguous or any( + target in run.owners or target == "HEAD" for target in run.named_refs + ) + if not denies or run.checked_out_ref_changes or run.head_changes: + return None + return f"over-block:{run.subcommand}:{'ambiguous' if run.ambiguous else 'names-ref'}" + + +def _assert_shared_refs_sound(run: _Run) -> None: + event(f"git:{run.subcommand}:{'ok' if run.returncode == 0 else 'rejected'}") + problems = _shared_ref_violations(run) + assert not problems, _failure_message(run, problems) + label = _over_block_label(run) + if label is not None: + event(label) + + +@pytest.fixture(scope="module") +def git_base(tmp_path_factory: pytest.TempPathFactory) -> _Base: + return _build_base(tmp_path_factory.mktemp("differential-base")) + + +@pytest.mark.timeout(300) +@settings(max_examples=200, deadline=None) +@_with_pinned_cases +@given(case=_git_cases()) +def test_classifier_accounts_for_shared_ref_effects( + git_base: _Base, tmp_path_factory: pytest.TempPathFactory, case: _Case +) -> None: + subcommand, argv = case + with _sandbox(git_base, tmp_path_factory.mktemp) as sandbox: + run = _run_differential(sandbox, subcommand, argv) + _assert_shared_refs_sound(run) diff --git a/tests/hooks/test_git_option_table_contract.py b/tests/hooks/test_git_option_table_contract.py new file mode 100644 index 0000000000..55f6c5dd40 --- /dev/null +++ b/tests/hooks/test_git_option_table_contract.py @@ -0,0 +1,175 @@ +"""Live completeness of the Git option tables against ``--git-completion-helper-all``. + +Members: _parse_completion_helper, _audit_table, git_env, git_repo, git_version, +test_table_agrees_with_live_completion_helper. + +``git --git-completion-helper-all`` prints one line of whitespace-separated +tokens. Before the first bare ``--`` come the long options, where a trailing +``=`` marks a required value; after it come the ``--no-`` negations (a few +``--no-`` spellings are listed before it). Short options are never printed, so +the check covers long rows only. The comparison runs only on the Git the tables +were derived from: at runtime an unknown option already fails closed, so a newer +Git produces denies, not bypasses. +""" + +from __future__ import annotations + +import os +import re +import shutil +import subprocess +from collections.abc import Mapping, Sequence +from pathlib import Path +from typing import NamedTuple + +import pytest + +from autoskillit.hooks._classification._option_grammar import OptionArity, OptionDef +from autoskillit.hooks._runtime._git_option_tables import ( + _GIT_SUBCOMMAND_TABLES, + _GIT_TABLES_REFERENCE_VERSION, +) + +pytestmark = [ + pytest.mark.layer("hooks"), + pytest.mark.medium, + pytest.mark.skipif(shutil.which("git") is None, reason="git binary required"), +] + +_AMBIENT_GIT_VARIABLES = ( + "GIT_DIR", + "GIT_WORK_TREE", + "GIT_COMMON_DIR", + "GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", +) +_MIN_LIVE_NAMES = 3 + + +class _Audit(NamedTuple): + unknown: list[str] + arity: list[str] + negations: list[str] + + +def _parse_completion_helper(output: str) -> tuple[list[str], list[str]]: + """Split helper output into (long options, negations after the bare ``--``).""" + tokens = output.split() + if "--" not in tokens: + return tokens, [] + divider = tokens.index("--") + return tokens[:divider], tokens[divider + 1 :] + + +def _negation_row(name: str, table: Mapping[str, OptionDef]) -> OptionDef | None: + """The negatable long row whose ``--no-`` spelling is *name*.""" + if not name.startswith("no-"): + return None + row = table.get(name[3:]) + return row if row is not None and row.has_long and row.negatable else None + + +def _audit_table( + table: Mapping[str, OptionDef], long_options: Sequence[str], negations: Sequence[str] +) -> _Audit: + unknown: list[str] = [] + arity: list[str] = [] + bad_negations: list[str] = [] + for token in long_options: + name = token.removeprefix("--").removesuffix("=") + row = table.get(name) + if row is not None and row.has_long: + if token.endswith("=") != (row.arity == OptionArity.REQUIRED): + arity.append(f"{token} (table arity: {row.arity})") + elif _negation_row(name, table) is None or token.endswith("="): + unknown.append(token) + # NONE and OPTIONAL both print bare, so the converse covers REQUIRED rows only. + for name, row in table.items(): + if row.has_long and row.arity == OptionArity.REQUIRED and f"--{name}=" not in long_options: + arity.append(f"--{name} (table arity: {row.arity}, not listed with '=')") + for token in negations: + name = token.removeprefix("--") + if _negation_row(name, table) is None: + bad_negations.append(token) + return _Audit(unknown, arity, bad_negations) + + +def _run_git(env: Mapping[str, str], cwd: Path, *args: str) -> subprocess.CompletedProcess[str]: + return subprocess.run( + ["git", *args], cwd=cwd, env=dict(env), capture_output=True, text=True, timeout=30 + ) + + +@pytest.fixture(scope="module") +def git_env(tmp_path_factory: pytest.TempPathFactory) -> dict[str, str]: + root = tmp_path_factory.mktemp("git-option-contract-home") + home = root / "home" + xdg_config_home = root / "xdg-config" + home.mkdir() + xdg_config_home.mkdir() + env = {k: v for k, v in os.environ.items() if k not in _AMBIENT_GIT_VARIABLES} + env.update( + HOME=str(home), + XDG_CONFIG_HOME=str(xdg_config_home), + GIT_CONFIG_GLOBAL=os.devnull, + GIT_CONFIG_NOSYSTEM="1", + ) + return env + + +@pytest.fixture(scope="module") +def git_repo(tmp_path_factory: pytest.TempPathFactory, git_env: dict[str, str]) -> Path: + # The helper only needs a repository to run in, not one with history. + repo = tmp_path_factory.mktemp("git-option-contract-repo") + init = _run_git(git_env, repo, "init", "-q") + assert init.returncode == 0, f"git init failed: {init.stderr!r}" + return repo + + +@pytest.fixture(scope="module") +def git_version(git_env: dict[str, str], git_repo: Path) -> tuple[str, tuple[int, int]]: + result = _run_git(git_env, git_repo, "--version") + text = result.stdout.strip() + assert result.returncode == 0, f"git --version failed: {result.stderr!r}" + match = re.search(r"(\d+)\.(\d+)", text) + assert match is not None, f"unparseable git --version output: {text!r}" + return text, (int(match.group(1)), int(match.group(2))) + + +@pytest.mark.parametrize("subcommand", sorted(_GIT_SUBCOMMAND_TABLES)) +def test_table_agrees_with_live_completion_helper( + subcommand: str, + git_env: dict[str, str], + git_repo: Path, + git_version: tuple[str, tuple[int, int]], +) -> None: + version_text, version = git_version + table = _GIT_SUBCOMMAND_TABLES[subcommand] + result = _run_git(git_env, git_repo, subcommand, "--git-completion-helper-all") + assert result.returncode == 0, ( + f"{version_text}: git {subcommand} --git-completion-helper-all failed: " + f"rc={result.returncode}, stderr={result.stderr!r}" + ) + long_options, negations = _parse_completion_helper(result.stdout) + assert len(long_options) >= _MIN_LIVE_NAMES, ( + f"{version_text}: git {subcommand} helper listed too few options: {result.stdout!r}" + ) + audit = _audit_table(table, long_options, negations) + + if version != _GIT_TABLES_REFERENCE_VERSION: + pytest.skip( + f"{version_text} differs from the tables' reference Git " + f"{_GIT_TABLES_REFERENCE_VERSION}; live names missing from the " + f"{subcommand} table: {audit.unknown + audit.negations}" + ) + + assert not audit.unknown, ( + f"{version_text}: git {subcommand} lists long options with no table row: {audit.unknown}" + ) + assert not audit.arity, ( + f"{version_text}: git {subcommand} value arity disagrees with the table " + f"(a trailing '=' marks REQUIRED): {audit.arity}" + ) + assert not audit.negations, ( + f"{version_text}: git {subcommand} lists negations whose table row is " + f"missing or not negatable: {audit.negations}" + ) diff --git a/tests/hooks/test_git_option_tables.py b/tests/hooks/test_git_option_tables.py new file mode 100644 index 0000000000..e47be309dd --- /dev/null +++ b/tests/hooks/test_git_option_tables.py @@ -0,0 +1,141 @@ +"""Convention check for the option tables in hooks/_runtime/_git_option_tables.py. + +Members: _spellings, _assert_row_spellings_parse and one parametrized test per +table: _GIT_CHECKOUT_FLAG_SPEC, _GIT_SWITCH_FLAG_SPEC, _GIT_BRANCH_FLAG_SPEC, +_GIT_SYMBOLIC_REF_FLAG_SPEC, _GIT_UPDATE_REF_FLAG_SPEC, _GIT_RESET_FLAG_SPEC, +_GIT_PUSH_FLAG_SPEC, _GIT_FETCH_FLAG_SPEC, _GIT_COMMIT_FLAG_SPEC, +_GIT_CLEAN_FLAG_SPEC and _SED_FLAG_SPEC. + +Each parametrize iterates its table's own keys, so a new row is covered without +a test edit: every long, short and ``--no-`` spelling a row declares must parse +back to that row with no ``unresolved`` reason. Tables are imported by bare name +because tests/arch/test_guard_flag_spec_coverage.py looks for the table name +inside the parametrize call. +""" + +from __future__ import annotations + +from collections.abc import Mapping + +import pytest + +from autoskillit.hooks._classification._option_grammar import ( + OptionArity, + OptionDef, + parse_options, +) +from autoskillit.hooks._runtime._git_option_tables import ( + _GIT_BRANCH_FLAG_SPEC, + _GIT_CHECKOUT_FLAG_SPEC, + _GIT_CLEAN_FLAG_SPEC, + _GIT_COMMIT_FLAG_SPEC, + _GIT_FETCH_FLAG_SPEC, + _GIT_PUSH_FLAG_SPEC, + _GIT_RESET_FLAG_SPEC, + _GIT_SWITCH_FLAG_SPEC, + _GIT_SYMBOLIC_REF_FLAG_SPEC, + _GIT_UPDATE_REF_FLAG_SPEC, + _SED_FLAG_SPEC, +) + +pytestmark = [pytest.mark.layer("hooks"), pytest.mark.small] + +_VALUE = "VALUE" + + +def _spellings(row: OptionDef) -> list[tuple[list[str], str | None, bool]]: + """Every argv that must parse to *row*, with the value and negation it carries.""" + spellings: list[tuple[list[str], str | None, bool]] = [] + if row.has_long: + long = f"--{row.name}" + if row.arity == OptionArity.NONE: + spellings.append(([long], None, False)) + else: + spellings.append(([f"{long}={_VALUE}"], _VALUE, False)) + if row.arity == OptionArity.REQUIRED: + spellings.append(([long, _VALUE], _VALUE, False)) + else: + spellings.append(([long], None, False)) + if row.negatable: + spellings.append(([f"--no-{row.name}"], None, True)) + if row.short: + short = f"-{row.short}" + if row.arity == OptionArity.NONE: + spellings.append(([short], None, False)) + else: + spellings.append(([f"{short}{_VALUE}"], _VALUE, False)) + if row.arity == OptionArity.REQUIRED: + spellings.append(([short, _VALUE], _VALUE, False)) + else: + spellings.append(([short], None, False)) + return spellings + + +def _assert_row_spellings_parse(table: Mapping[str, OptionDef], name: str) -> None: + row = table[name] + assert row.name == name + assert not name.startswith("-") + # A short-only row is named by its letter. + assert row.has_long or row.short == name + spellings = _spellings(row) + assert spellings, f"{name!r} declares no spelling" + for argv, value, negated in spellings: + parsed = parse_options(argv, table) + assert parsed.unresolved is None, f"{argv}: {parsed.unresolved}" + assert [tuple(option) for option in parsed.options] == [(name, value, negated)], argv + assert parsed.operands == (), argv + + +@pytest.mark.parametrize("name", sorted(_GIT_CHECKOUT_FLAG_SPEC)) +def test_checkout_row_spellings_parse(name: str) -> None: + _assert_row_spellings_parse(_GIT_CHECKOUT_FLAG_SPEC, name) + + +@pytest.mark.parametrize("name", sorted(_GIT_SWITCH_FLAG_SPEC)) +def test_switch_row_spellings_parse(name: str) -> None: + _assert_row_spellings_parse(_GIT_SWITCH_FLAG_SPEC, name) + + +@pytest.mark.parametrize("name", sorted(_GIT_BRANCH_FLAG_SPEC)) +def test_branch_row_spellings_parse(name: str) -> None: + _assert_row_spellings_parse(_GIT_BRANCH_FLAG_SPEC, name) + + +@pytest.mark.parametrize("name", sorted(_GIT_SYMBOLIC_REF_FLAG_SPEC)) +def test_symbolic_ref_row_spellings_parse(name: str) -> None: + _assert_row_spellings_parse(_GIT_SYMBOLIC_REF_FLAG_SPEC, name) + + +@pytest.mark.parametrize("name", sorted(_GIT_UPDATE_REF_FLAG_SPEC)) +def test_update_ref_row_spellings_parse(name: str) -> None: + _assert_row_spellings_parse(_GIT_UPDATE_REF_FLAG_SPEC, name) + + +@pytest.mark.parametrize("name", sorted(_GIT_RESET_FLAG_SPEC)) +def test_reset_row_spellings_parse(name: str) -> None: + _assert_row_spellings_parse(_GIT_RESET_FLAG_SPEC, name) + + +@pytest.mark.parametrize("name", sorted(_GIT_PUSH_FLAG_SPEC)) +def test_push_row_spellings_parse(name: str) -> None: + _assert_row_spellings_parse(_GIT_PUSH_FLAG_SPEC, name) + + +@pytest.mark.parametrize("name", sorted(_GIT_FETCH_FLAG_SPEC)) +def test_fetch_row_spellings_parse(name: str) -> None: + _assert_row_spellings_parse(_GIT_FETCH_FLAG_SPEC, name) + + +@pytest.mark.parametrize("name", sorted(_GIT_COMMIT_FLAG_SPEC)) +def test_commit_row_spellings_parse(name: str) -> None: + _assert_row_spellings_parse(_GIT_COMMIT_FLAG_SPEC, name) + + +@pytest.mark.parametrize("name", sorted(_GIT_CLEAN_FLAG_SPEC)) +def test_clean_row_spellings_parse(name: str) -> None: + _assert_row_spellings_parse(_GIT_CLEAN_FLAG_SPEC, name) + + +@pytest.mark.parametrize("name", sorted(_SED_FLAG_SPEC)) +def test_sed_row_spellings_parse(name: str) -> None: + _assert_row_spellings_parse(_SED_FLAG_SPEC, name) diff --git a/tests/hooks/test_git_outcome.py b/tests/hooks/test_git_outcome.py new file mode 100644 index 0000000000..15cb45a517 --- /dev/null +++ b/tests/hooks/test_git_outcome.py @@ -0,0 +1,349 @@ +"""Unit matrix for the Git outcome classifiers in hooks/_runtime/_git_outcome.py. + +Members: checkout, switch, branch and symbolic-ref outcome matrices +(``git_ref_outcome``), dynamic-token ambiguity rows, the ``parse_update_ref``, +``parse_reset``, ``parse_push`` and ``parse_fetch`` parser rows, and the +``is_ref_mutation`` rows. + +Rows follow Git 2.43's case partitions. Argv is space-separated shorthand split +on whitespace; ``5f3c9e1`` stands for a commit and ``feat`` for a branch name. +""" + +from __future__ import annotations + +import pytest + +from autoskillit.hooks._runtime._git_outcome import ( + GitHeadEffect, + git_ref_outcome, + is_ref_mutation, + parse_fetch, + parse_push, + parse_reset, + parse_update_ref, +) + +pytestmark = [pytest.mark.layer("hooks"), pytest.mark.small] + +_NONE = GitHeadEffect.NONE +_MOVES = GitHeadEffect.MOVES +_PATH_RESTORE = GitHeadEffect.PATH_RESTORE +_AMBIGUOUS = GitHeadEffect.AMBIGUOUS + + +def _argv(text: str) -> list[str]: + return text.split() + + +def _id(argv: str) -> str: + return argv or "no-arguments" + + +def _head_cases( + effect: GitHeadEffect, argvs: tuple[str, ...], rewritten: str | None = None +) -> list[object]: + return [pytest.param(argv, effect, rewritten, id=_id(argv)) for argv in argvs] + + +def _head_and_rewrite(subcommand: str, argv: str) -> tuple[GitHeadEffect, str | None]: + outcome = git_ref_outcome(subcommand, _argv(argv)) + assert outcome is not None, f"git {subcommand} {argv} produced no outcome" + return outcome.head_effect, outcome.rewritten_ref + + +_CHECKOUT_ROWS = [ + *_head_cases( + _MOVES, + ( + "-b x 5f3c9e1", + "-fb x", + "-bx", + "--orphan x", + "--orph=x", + "-d", + "--detach", + "-t origin/y", + "-b x -t origin/y", + ), + ), + *_head_cases( + _MOVES, + ( + "-B develop 5f3c9e1", + "-Bdevelop", + "-fB develop", + "5f3c9e1 -B develop", + "--ignore-other-worktrees -Bdevelop", + ), + "develop", + ), + *_head_cases( + _PATH_RESTORE, + ( + "-- f", + "feat -- f", + "feat f1 f2", + "-p", + "-p other", + "--ours f", + "--theirs f", + "-2 f", + "-3 f", + "--ours feat", + ), + ), + *_head_cases(_AMBIGUOUS, ("--pathspec-from-file=x", "other --pathspec-from-file=empty.lst")), + *_head_cases(_NONE, ("", "-f", "-q")), + *_head_cases(_MOVES, ("feat --", "-", "@{-1}", "main...other")), + *_head_cases(_AMBIGUOUS, ("a b -- f", "--bogus x", "--o x")), + *_head_cases(_AMBIGUOUS, ("feat", "file.txt", ":/.")), + *_head_cases(_PATH_RESTORE, (".", "./sub/f", "/abs", ":/")), + *_head_cases(_AMBIGUOUS, ("--detach --no-detach other",)), +] + +_SWITCH_ROWS = [ + *_head_cases(_MOVES, ("-c x", "-cx", "--create=x", "feat", "-", "--detach", "-d")), + *_head_cases( + _MOVES, + ( + "-C develop", + "-Cdevelop", + "-fC develop", + "--force-create develop", + "--force-create=develop", + "--force-c develop", + ), + "develop", + ), + *_head_cases(_NONE, ("",)), + *_head_cases(_AMBIGUOUS, ("a b",)), +] + + +@pytest.mark.parametrize(("argv", "effect", "rewritten"), _CHECKOUT_ROWS) +def test_checkout_outcome(argv: str, effect: GitHeadEffect, rewritten: str | None) -> None: + assert _head_and_rewrite("checkout", argv) == (effect, rewritten) + + +@pytest.mark.parametrize(("argv", "effect", "rewritten"), _SWITCH_ROWS) +def test_switch_outcome(argv: str, effect: GitHeadEffect, rewritten: str | None) -> None: + assert _head_and_rewrite("switch", argv) == (effect, rewritten) + + +def _branch_view(argv: str) -> tuple[str | None, bool, GitHeadEffect]: + outcome = git_ref_outcome("branch", _argv(argv)) + assert outcome is not None, f"git branch {argv} produced no outcome" + return outcome.rewritten_ref, outcome.rewrites_current_branch, outcome.head_effect + + +def _branch_cases(rewritten: str, argvs: tuple[str, ...]) -> list[object]: + return [pytest.param(argv, rewritten, id=argv) for argv in argvs] + + +_BRANCH_REWRITE_ROWS = [ + *_branch_cases("x", ("-f x 5f3c9e1", "x 5f3c9e1 -f", "-fq x", "--forc x")), + *_branch_cases("old", ("-m old new", "-M old new", "--move old new", "-fm old new")), + *_branch_cases("b", ("-c a b",)), +] + +_BRANCH_NO_OUTCOME_ARGVS = ( + "", + "-a", + "--list", + "-vv", + "--show-current", + "-d x", + "-D x", + "--contains", + "--merged", + "--no-merged", + # Git takes --list as the value of --contains and exits 129; the table reads + # two options. Both readings are list mode, so the verdict is the same. + "--contains --list", +) + + +@pytest.mark.parametrize(("argv", "rewritten"), _BRANCH_REWRITE_ROWS) +def test_branch_rewrites_the_named_ref_without_touching_head(argv: str, rewritten: str) -> None: + assert _branch_view(argv) == (rewritten, False, _NONE) + + +def test_branch_move_with_one_operand_renames_the_current_branch() -> None: + assert _branch_view("-m new") == (None, True, _MOVES) + + +@pytest.mark.parametrize("argv", _BRANCH_NO_OUTCOME_ARGVS, ids=_id) +def test_branch_list_and_delete_modes_have_no_outcome(argv: str) -> None: + assert git_ref_outcome("branch", _argv(argv)) is None + + +_SYMBOLIC_REF_MOVES_ARGVS = ( + "HEAD refs/heads/x", + "-q HEAD refs/heads/x", + "-m why HEAD refs/heads/x", + "-mwhy HEAD refs/heads/x", +) + + +@pytest.mark.parametrize("argv", _SYMBOLIC_REF_MOVES_ARGVS, ids=_id) +def test_symbolic_ref_head_assignment_moves_head_to_the_target(argv: str) -> None: + outcome = git_ref_outcome("symbolic-ref", _argv(argv)) + + assert outcome is not None + assert outcome.rewritten_ref == "refs/heads/x" + assert outcome.start_point == "refs/heads/x" + assert outcome.head_effect == _MOVES + assert not outcome.ref_ambiguous + + +def test_symbolic_ref_of_another_ref_rewrites_that_ref_only() -> None: + outcome = git_ref_outcome("symbolic-ref", _argv("refs/heads/a refs/heads/b")) + + assert outcome is not None + assert outcome.rewritten_ref == "refs/heads/a" + assert outcome.head_effect == _NONE + assert not outcome.ref_ambiguous + + +def test_symbolic_ref_read_has_no_outcome() -> None: + assert git_ref_outcome("symbolic-ref", _argv("HEAD")) is None + + +_DYNAMIC_REF_ROWS = [ + pytest.param("checkout", "-B $X", True, id="checkout-B-dynamic-name"), + pytest.param("checkout", "-B x $SHA", True, id="checkout-B-dynamic-start-point"), + pytest.param("checkout", "-B x 5f3c9e1", False, id="checkout-B-literal"), + pytest.param("switch", "-C $X", True, id="switch-C-dynamic-name"), + pytest.param("branch", "-m $X new", True, id="branch-move-dynamic-source"), + pytest.param("branch", "-m old new", False, id="branch-move-literal"), + pytest.param("symbolic-ref", "HEAD $X", True, id="symbolic-ref-dynamic-target"), + pytest.param("symbolic-ref", "$X refs/heads/y", True, id="symbolic-ref-dynamic-name"), + pytest.param("symbolic-ref", "HEAD refs/heads/y", False, id="symbolic-ref-literal"), + pytest.param("checkout", "--bogus x", True, id="checkout-unresolved"), + pytest.param("switch", "--bogus x", True, id="switch-unresolved"), + pytest.param("branch", "--bogus x", True, id="branch-unresolved"), + pytest.param("symbolic-ref", "--bogus x", True, id="symbolic-ref-unresolved"), +] + + +@pytest.mark.parametrize(("subcommand", "argv", "ambiguous"), _DYNAMIC_REF_ROWS) +def test_dynamic_or_unresolved_tokens_make_the_ref_ambiguous( + subcommand: str, argv: str, ambiguous: bool +) -> None: + outcome = git_ref_outcome(subcommand, _argv(argv)) + + assert outcome is not None + assert outcome.ref_ambiguous is ambiguous + + +def test_dynamic_deciding_operand_is_an_ambiguous_head_effect() -> None: + assert _head_and_rewrite("checkout", "$X") == (_AMBIGUOUS, None) + + +def test_unresolved_checkout_is_an_ambiguous_head_effect() -> None: + outcome = git_ref_outcome("checkout", _argv("--bogus x")) + + assert outcome is not None + assert outcome.head_effect == _AMBIGUOUS + assert outcome.ref_ambiguous + + +@pytest.mark.parametrize("subcommand", ["update-ref", "reset", "push", "fetch", "commit"]) +def test_other_subcommands_have_no_ref_outcome(subcommand: str) -> None: + assert git_ref_outcome(subcommand, _argv("origin main")) is None + + +@pytest.mark.parametrize( + ("argv", "delete", "stdin"), + [ + pytest.param("-dm why refs/heads/d", True, False, id="-dm-delete"), + pytest.param("--std", False, True, id="--std-prefix"), + ], +) +def test_parse_update_ref(argv: str, delete: bool, stdin: bool) -> None: + parsed = parse_update_ref(_argv(argv)) + + assert parsed.unresolved is None + assert parsed.delete is delete + assert parsed.stdin is stdin + + +def test_parse_update_ref_keeps_the_ref_as_an_operand_after_a_reason() -> None: + assert parse_update_ref(_argv("-dm why refs/heads/d")).operands == ("refs/heads/d",) + + +@pytest.mark.parametrize( + ("argv", "mode", "path_mode"), + [ + pytest.param("--soft feat --", "soft", False, id="soft-commit-mode"), + pytest.param("feat -- f", None, True, id="paths-after-dashdash"), + pytest.param("feat f", None, True, id="two-operands"), + pytest.param("-p", None, True, id="patch"), + pytest.param("--har", "hard", False, id="mode-prefix"), + ], +) +def test_parse_reset(argv: str, mode: str | None, path_mode: bool) -> None: + parsed = parse_reset(_argv(argv)) + + assert parsed.unresolved is None + assert parsed.mode == mode + assert parsed.path_mode is path_mode + + +def test_parse_reset_names_the_commit_before_dashdash() -> None: + assert parse_reset(_argv("--soft feat --")).commit == "feat" + + +def test_parse_reset_pathspec_from_file_is_unresolved() -> None: + assert parse_reset(_argv("--pathspec-from-file=x feat")).unresolved is not None + + +@pytest.mark.parametrize( + ("argv", "remote", "refspecs"), + [ + pytest.param("-o origin . HEAD:develop", ".", ("HEAD:develop",), id="push-option-value"), + pytest.param("--repo=r", "r", (), id="repo-option"), + pytest.param("--repo=r . HEAD:develop", ".", ("HEAD:develop",), id="operand-wins"), + pytest.param("origin HEAD", "origin", ("HEAD",), id="named-remote"), + ], +) +def test_parse_push(argv: str, remote: str, refspecs: tuple[str, ...]) -> None: + parsed = parse_push(_argv(argv)) + + assert parsed.unresolved is None + assert parsed.remote == remote + assert parsed.refspecs == refspecs + + +def test_parse_fetch_reports_the_refspec_destination() -> None: + parsed = parse_fetch(_argv("origin develop:develop")) + + assert parsed.unresolved is None + assert parsed.remote == "origin" + assert parsed.refspecs == ("develop:develop",) + + +_REF_MUTATION_ROWS = [ + pytest.param("update-ref", "refs/heads/x HEAD", True, id="update-ref"), + pytest.param("reset", "HEAD~", True, id="reset-commit"), + pytest.param("reset", "-- f", False, id="reset-paths"), + pytest.param("checkout", "-Bdevelop", True, id="checkout-B"), + pytest.param("branch", "-m a b", True, id="branch-move"), + pytest.param("checkout", "-b f", False, id="checkout-b"), + pytest.param("branch", "-a", False, id="branch-list"), + pytest.param("push", "origin HEAD", False, id="push-named-remote"), + pytest.param("push", "-u origin main", False, id="push-set-upstream-named-remote"), + pytest.param("push", ". HEAD:develop", True, id="push-dot-remote"), + pytest.param("push", "../repo x", True, id="push-relative-path-remote"), + pytest.param("push", "$R x", True, id="push-dynamic-remote"), + pytest.param("fetch", "origin", False, id="fetch-no-refspec"), + pytest.param("fetch", "origin develop:develop", True, id="fetch-refspec-destination"), + pytest.param("fetch", ". a:b", True, id="fetch-dot-refspec"), + pytest.param("", "", True, id="unresolved-subcommand"), + pytest.param("$SUB", "", True, id="dynamic-subcommand"), +] + + +@pytest.mark.parametrize(("subcommand", "argv", "expected"), _REF_MUTATION_ROWS) +def test_is_ref_mutation(subcommand: str, argv: str, expected: bool) -> None: + assert is_ref_mutation(subcommand, _argv(argv)) is expected diff --git a/tests/hooks/test_option_grammar.py b/tests/hooks/test_option_grammar.py new file mode 100644 index 0000000000..6831d0f8e2 --- /dev/null +++ b/tests/hooks/test_option_grammar.py @@ -0,0 +1,140 @@ +"""Unit matrix for the parse-options engine in hooks/_classification/_option_grammar.py. + +Members: _TABLE (a checkout-like fixture table), _CASES, test_parse_options_matrix, +test_last_option_prefers_the_final_occurrence_and_keeps_negation, +test_exact_name_wins_over_longer_prefix_candidates. +""" + +from __future__ import annotations + +import pytest + +from autoskillit.hooks._classification._option_grammar import ( + OptionArity, + OptionDef, + last_option, + parse_options, +) + +pytestmark = [pytest.mark.layer("hooks"), pytest.mark.small] + +_NONE = OptionArity.NONE +_REQUIRED = OptionArity.REQUIRED +_OPTIONAL = OptionArity.OPTIONAL + + +def _row( + name: str, + arity: OptionArity = _NONE, + *, + short: str | None = None, + negatable: bool = False, + has_long: bool = True, +) -> OptionDef: + return OptionDef(name, short, arity, negatable, has_long) + + +def _table(*rows: OptionDef) -> dict[str, OptionDef]: + return {row.name: row for row in rows} + + +# Short-only rows are named by their letter; rows with a long spelling by it. +_TABLE = _table( + _row("b", _REQUIRED, short="b", has_long=False), + _row("B", _REQUIRED, short="B", has_long=False), + _row("orphan", _REQUIRED), + _row("force", short="f"), + _row("quiet", short="q"), + _row("track", _OPTIONAL, short="t"), + _row("detach", short="d", negatable=True), + _row("guess", negatable=True), + _row("overlay"), +) + +_OptionTuple = tuple[str, str | None, bool] + + +def _o(name: str, value: str | None = None, negated: bool = False) -> _OptionTuple: + return (name, value, negated) + + +def _case( + argv: str, + options: tuple[_OptionTuple, ...] = (), + *, + operands: tuple[str, ...] = (), + before_dashdash: int | None = None, + unresolved: str | None = None, +) -> object: + return pytest.param(argv, options, operands, before_dashdash, unresolved, id=argv) + + +_CASES = [ + _case("-fB x", (_o("force"), _o("B", "x"))), + _case("-Bx", (_o("B", "x"),)), + _case("-B -q", (_o("B", "-q"),)), + _case("-qqq", (_o("quiet"),) * 3), + _case("--orphan=x", (_o("orphan", "x"),)), + _case("--orphan x", (_o("orphan", "x"),)), + _case("--orph x", (_o("orphan", "x"),)), + _case("--o x", unresolved="ambiguous_option"), + _case("--no-guess", (_o("guess", negated=True),)), + _case("--no-gu", (_o("guess", negated=True),)), + _case("--no-orphan", unresolved="unknown_option"), + _case("-tb", (_o("track", "b"),)), + _case("--track", (_o("track"),)), + _case("--track=direct", (_o("track", "direct"),)), + _case("--track direct", (_o("track"),), operands=("direct",)), + _case("-- -b x", operands=("-b", "x"), before_dashdash=0), + _case("a -b x -- -q", (_o("b", "x"),), operands=("a", "-q"), before_dashdash=1), + _case("-", operands=("-",)), + _case("--bogus", unresolved="unknown_option"), + _case("-z", unresolved="unknown_option"), + _case("-qz", (_o("quiet"),), unresolved="unknown_option"), + _case("--b x", unresolved="unknown_option"), + _case("-b", unresolved="missing_value"), + _case("--orphan", unresolved="missing_value"), + _case("--force=1", unresolved="unexpected_value"), + _case("--no-detach=1", unresolved="unexpected_value"), + _case("--detach --no-detach", (_o("detach"), _o("detach", negated=True))), + _case("-B$X", (_o("B", "$X"),)), + _case("$X", operands=("$X",)), +] + + +@pytest.mark.parametrize(("argv", "options", "operands", "before_dashdash", "unresolved"), _CASES) +def test_parse_options_matrix( + argv: str, + options: tuple[_OptionTuple, ...], + operands: tuple[str, ...], + before_dashdash: int | None, + unresolved: str | None, +) -> None: + parsed = parse_options(argv.split(), _TABLE) + + assert tuple((o.name, o.value, o.negated) for o in parsed.options) == options + assert parsed.operands == operands + assert parsed.operands_before_dashdash == before_dashdash + assert parsed.unresolved == unresolved + + +def test_last_option_prefers_the_final_occurrence_and_keeps_negation() -> None: + parsed = parse_options(["--detach", "--no-detach"], _TABLE) + + final = last_option(parsed, "detach") + + assert final is not None + assert tuple(final) == _o("detach", negated=True) + assert last_option(parsed, "guess") is None + + +def test_exact_name_wins_over_longer_prefix_candidates() -> None: + table = _table(_row("force", short="f"), _row("force-create", _REQUIRED, short="C")) + + exact = parse_options(["--force"], table) + prefixed = parse_options(["--force-c", "x"], table) + + assert [tuple(o) for o in exact.options] == [_o("force")] + assert exact.unresolved is None + assert [tuple(o) for o in prefixed.options] == [_o("force-create", "x")] + assert prefixed.unresolved is None diff --git a/tests/infra/test_git_ops_guard.py b/tests/infra/test_git_ops_guard.py index 3e16357a83..88cafa9113 100644 --- a/tests/infra/test_git_ops_guard.py +++ b/tests/infra/test_git_ops_guard.py @@ -16,6 +16,7 @@ import subprocess import sys import unittest.mock +from collections.abc import Mapping from pathlib import Path import pytest @@ -24,11 +25,10 @@ # internal bare-name sibling imports (_github_mutation_analysis, # _command_classification) is centralized in tests/conftest.py -- it must # run before this module's own top-level imports, which a fixture cannot do. -from autoskillit.hooks._runtime._command_classification import _FlagArity -from autoskillit.hooks._runtime._git_command_classification import ( - _GIT_FETCH_FLAG_SPEC, - _classify_fetch, -) +from autoskillit.hooks._classification._option_grammar import OptionArity, OptionDef +from autoskillit.hooks._runtime._git_command_classification import _classify_fetch +from autoskillit.hooks._runtime._git_option_tables import _GIT_FETCH_FLAG_SPEC +from autoskillit.hooks.guards import git_ops_guard pytestmark = [pytest.mark.layer("infra"), pytest.mark.medium] @@ -164,6 +164,42 @@ def _assert_ref_unchanged(repo: Path, ref: str, old_sha: str) -> None: assert _git(repo, "rev-parse", ref) == old_sha +def _guard_verdict(output: str) -> str: + """Classify captured guard stdout as allow, checked-out-ref or other-deny.""" + if not output.strip(): + return "allow" + assert _is_denied(output), output + reason = json.loads(output)["hookSpecificOutput"]["permissionDecisionReason"] + if reason.startswith(git_ops_guard.CHECKED_OUT_REF_DENY_PREFIX): + return "checked-out-ref" + return "other-deny" + + +def _guard_exit_code(cmd: str, tmpdir: Path) -> int | str | None: + """Run git_ops_guard.main() in a headless skill session with the kitchen open + and return the code it exits with. + + _run_guard requires a clean exit; this expects main() to terminate through + SystemExit and leaves stdout and stderr to the caller's capsys. + """ + hook_cfg = tmpdir / _HOOK_CONFIG_RELPATH + hook_cfg.parent.mkdir(parents=True, exist_ok=True) + hook_cfg.write_text(json.dumps({"kitchen": "open"})) + payload = { + "session_id": "guard-test-session", + "tool_name": _TOOL_NAME, + "tool_input": {"cmd": cmd, "cwd": str(tmpdir)}, + } + with ( + unittest.mock.patch.dict(os.environ, _make_clean_env(None), clear=True), + unittest.mock.patch("sys.stdin", io.StringIO(json.dumps(payload))), + unittest.mock.patch("pathlib.Path.cwd", return_value=tmpdir), + pytest.raises(SystemExit) as exit_info, + ): + git_ops_guard.main() + return exit_info.value.code + + # --------------------------------------------------------------------------- # Denied cases: commit --amend # --------------------------------------------------------------------------- @@ -901,11 +937,10 @@ def test_denies_fetch_with_unrecognized_value_flag_space_separated( self, linked_repo: dict[str, Path | str] ) -> None: """--negotiation-tip in space-separated form (NOT --flag=value, - which is a single self-contained shlex token) previously fell through - _classify_fetch's flag parsing as unrecognized, so its value was - appended to `positional` and misread as the remote name. The wrong - remote..fetch lookup returned nothing, mappings stayed empty, - and the fetch passed through unclassified. See _GIT_FETCH_FLAG_SPEC. + which is a single self-contained shlex token) must consume its value. + A value misread as the remote name sends the remote..fetch lookup + to the wrong remote, leaves the mappings empty, and lets the fetch pass + through unclassified. See _GIT_FETCH_FLAG_SPEC in _git_option_tables. """ linked = linked_repo["linked"] assert isinstance(linked, Path) @@ -1517,60 +1552,210 @@ def test_checked_out_ref_preflight_precedes_scope_exemptions_and_policy( _checked_out_ref_result(out) +_SESSION_SHAPES = ( + (True, "skill"), + (True, "orchestrator"), + (False, "orchestrator"), + (False, "skill"), +) + + +class TestCheckedOutRefOutcomeImmunity: + """A ref rewrite is judged by the checked-out ref it rewrites, not by how its + options are spelled; HEAD effects with no ref rewrite and list forms stay allowed. + """ + + @pytest.fixture(autouse=True) + def _unowned_branch(self, linked_repo: dict[str, Path | str]) -> None: + _git(Path(str(linked_repo["primary"])), "branch", "spare", str(linked_repo["old_sha"])) + + @pytest.mark.parametrize( + "command_template", + [ + "git checkout -Bdevelop {sha}", + "git checkout -fB develop {sha}", + "git checkout --ignore-other-worktrees -Bdevelop", + "git checkout feat -B develop", + "git switch -Cdevelop", + "git switch --force-create=develop", + "git switch --force-c develop", + "git branch -m develop renamed", + "git branch -M develop renamed", + "git update-ref -dm why refs/heads/develop", + "git push -o x {primary} HEAD:develop", + ], + ) + def test_denies_rewrite_of_ref_owned_by_primary( + self, linked_repo: dict[str, Path | str], command_template: str + ) -> None: + linked = linked_repo["linked"] + assert isinstance(linked, Path) + command = command_template.format( + sha=linked_repo["new_sha"], primary=shlex.quote(str(linked_repo["primary"])) + ) + out = _run_guard(command, kitchen_open=True, tmpdir=linked) + assert _guard_verdict(out) == "checked-out-ref", command + threatened = _checked_out_ref_result(out)["threatened_refs"] + assert isinstance(threatened, list) + assert [row["target_ref"] for row in threatened] == ["refs/heads/develop"], command + + @pytest.mark.parametrize( + "command_template", + [ + "git branch -m spare spare2", + "git checkout -b fresh {sha}", + "git checkout spare", + "git switch spare", + "git checkout --detach", + "git branch -a", + "git branch --merged", + "git symbolic-ref HEAD", + ], + ) + def test_allows_unowned_rewrites_head_effects_and_list_forms( + self, linked_repo: dict[str, Path | str], command_template: str + ) -> None: + linked = linked_repo["linked"] + assert isinstance(linked, Path) + command = command_template.format(sha=linked_repo["new_sha"]) + out = _run_guard(command, kitchen_open=True, tmpdir=linked) + assert _guard_verdict(out) == "allow", command + + @pytest.mark.parametrize( + ("command", "verdict"), + [ + ("git checkout -Bdevelop", "checked-out-ref"), + ("git branch -m develop renamed", "checked-out-ref"), + ("git update-ref -dm why refs/heads/develop", "checked-out-ref"), + ("git branch -a", "allow"), + ("git checkout -b fresh", "allow"), + ("git checkout spare", "allow"), + ], + ) + def test_verdict_is_the_same_in_every_session_shape( + self, linked_repo: dict[str, Path | str], command: str, verdict: str + ) -> None: + linked = linked_repo["linked"] + assert isinstance(linked, Path) + verdicts = { + shape: _guard_verdict( + _run_guard( + command, + kitchen_open=True, + tmpdir=linked, + headless=shape[0], + session_type=shape[1], + ) + ) + for shape in _SESSION_SHAPES + } + assert verdicts == dict.fromkeys(_SESSION_SHAPES, verdict) + + +class TestGuardFailsClosed: + """An unexpected classification error blocks (exit 2, reason on stderr).""" + + def test_error_in_ref_classification_blocks( + self, + linked_repo: dict[str, Path | str], + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], + ) -> None: + linked = linked_repo["linked"] + assert isinstance(linked, Path) + + def _raise(*_args: object, **_kwargs: object) -> None: + raise IndexError("injected classification failure") + + monkeypatch.setattr(git_ops_guard, "_classify_git_segment", _raise) + + code = _guard_exit_code(f"git update-ref refs/heads/x {linked_repo['new_sha']}", linked) + + captured = capsys.readouterr() + assert code == 2 + assert captured.out == "" + assert "git_ops_guard: IndexError: injected classification failure" in captured.err + + def test_error_in_blocked_operation_match_blocks( + self, + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], + ) -> None: + def _raise(*_args: object, **_kwargs: object) -> None: + raise KeyError("injected matcher failure") + + monkeypatch.setattr(git_ops_guard, "_contains_blocked_git_op", _raise) + + code = _guard_exit_code("git status", tmp_path) + + captured = capsys.readouterr() + assert code == 2 + assert captured.out == "" + assert "git_ops_guard: KeyError: 'injected matcher failure'" in captured.err + + # --------------------------------------------------------------------------- -# Spec table coverage: every entry in _GIT_FETCH_FLAG_SPEC must be recognized -# by _classify_fetch's spec-driven consume loop. Parametrized directly from -# the table's own keys so a future flag added to the spec extends coverage +# Spec table coverage: every row of _GIT_FETCH_FLAG_SPEC must be recognized by +# _classify_fetch in each spelling it admits. Parametrized directly from the +# table's own keys so a row added to the table extends coverage # automatically. Registered with test_guard_flag_spec_coverage.py via # _SPEC_TABLE_TEST_FILES so the standing-invariant AST scanner confirms the # parametrize genuinely iterates the table rather than a hand-copied list. # --------------------------------------------------------------------------- -@pytest.mark.parametrize("flag", sorted(_GIT_FETCH_FLAG_SPEC)) -def test_every_git_fetch_spec_flag_is_recognized(flag: str) -> None: - """Generative coverage (Part D): every flag in _GIT_FETCH_FLAG_SPEC must be +def _fetch_flag_spellings(row: OptionDef) -> list[list[str]]: + """Return the argv fragments that spell *row*: long, short and negated. - recognized by _classify_fetch rather than misread as the remote/refspec - positional. An unrecognized flag's value would have been misclassified as - the fetch's remote URL and silently passed the destructive-op preflight; - that must not happen for any spec member. - - --stdin is intentionally short-circuited by _classify_fetch's first - special case (it makes destinations ambiguous by design, independent of - spec-table recognition), so we assert the SPECIFIC ambiguous result shape - for it rather than the generic non-unresolved invariant. + A REQUIRED value is stuck to the long spelling (``--name=VALUE``) and + follows the short one as a separate token. """ - args = [flag] - if _GIT_FETCH_FLAG_SPEC[flag] == _FlagArity.VALUE: - args.append("someval") - args.append("origin") - - result = _classify_fetch(args, cwd="/tmp", owned_refs=[]) - - if flag == "--stdin": - # --stdin is special-cased at the top of _classify_fetch to return the - # ambiguous-deny idiom; the spec table entry exists so the generic - # consume loop doesn't misread it as a positional remote, not because - # the function's downstream logic actually consumes it. - assert result == [("", "", True)] - else: - # Any presence of an `` entry from the consume loop - # means the flag was NOT recognized -- the precise failure mode the - # spec table is supposed to prevent. - assert not any(entry[1] == "" for entry in result) + value = ["someval"] if row.arity == OptionArity.REQUIRED else [] + spellings: list[list[str]] = [] + if row.has_long: + spellings.append([f"--{row.name}=someval"] if value else [f"--{row.name}"]) + if row.short: + spellings.append([f"-{row.short}", *value]) + if row.has_long and row.negatable: + spellings.append([f"--no-{row.name}"]) + return spellings + + +@pytest.mark.parametrize("name", sorted(_GIT_FETCH_FLAG_SPEC)) +def test_every_git_fetch_spec_flag_is_recognized(name: str) -> None: + """Generative coverage: every row of _GIT_FETCH_FLAG_SPEC, in each spelling + it admits, must be recognized by _classify_fetch rather than misread as the + remote/refspec operand. An unrecognized flag's value would have been + misclassified as the fetch's remote URL and silently passed the + destructive-op preflight; that must not happen for any table row. + + ``--stdin`` makes the destinations ambiguous by design (the refspecs arrive + on stdin), so we assert that specific result shape for it rather than the + generic non-unresolved invariant; its negation is an ordinary flag. + """ + spellings = _fetch_flag_spellings(_GIT_FETCH_FLAG_SPEC[name]) + assert spellings, f"{name} has no spelling" + + for spelling in spellings: + result = _classify_fetch([*spelling, "origin"], cwd="/tmp", owned_refs=[]) + + if spelling == ["--stdin"]: + assert result == [("", "", True)] + else: + # An `` entry means the flag was NOT recognized -- the + # precise failure mode the option table is supposed to prevent. + assert not any(entry[1] == "" for entry in result), spelling def test_git_ops_guard_classify_imports_from_new_sibling() -> None: """Step 9 (#4733) regression: every classification primitive the orchestrator consumes from autoskillit.hooks._runtime._git_command_classification must remain reachable from the package import surface. Mirrors the orchestrator's import - block at src/autoskillit/hooks/guards/git_ops_guard.py:37. + block at src/autoskillit/hooks/guards/git_ops_guard.py:37. The fetch option + table the classifier reads lives in _git_option_tables as OptionDef rows. """ from autoskillit.hooks._runtime._git_command_classification import ( - _GIT_FETCH_BOOLEAN_FLAGS, - _GIT_FETCH_FLAG_SPEC, - _GIT_FETCH_VALUE_FLAGS, _classify_git_segment, _contains_blocked_git_op, _git_result, @@ -1592,13 +1777,9 @@ def test_git_ops_guard_classify_imports_from_new_sibling() -> None: for symbol in expected_callables: assert callable(symbol), f"_git_command_classification.{symbol.__name__} must be callable" - assert isinstance(_GIT_FETCH_FLAG_SPEC, dict) - assert all(isinstance(k, str) for k in _GIT_FETCH_FLAG_SPEC) - assert all( - v == _FlagArity.BOOLEAN or v == _FlagArity.VALUE for v in _GIT_FETCH_FLAG_SPEC.values() - ) - assert isinstance(_GIT_FETCH_BOOLEAN_FLAGS, frozenset) - assert isinstance(_GIT_FETCH_VALUE_FLAGS, frozenset) + assert isinstance(_GIT_FETCH_FLAG_SPEC, Mapping) + assert all(isinstance(row, OptionDef) for row in _GIT_FETCH_FLAG_SPEC.values()) + assert all(name == row.name for name, row in _GIT_FETCH_FLAG_SPEC.items()) def test_git_ops_guard_imports_with_hooks_directory_alone() -> None: From 46ce6af2a986012617942ad066669f53dc6f83bb Mon Sep 17 00:00:00 2001 From: Trecek Date: Thu, 5 Nov 2026 14:22:53 -0800 Subject: [PATCH 02/46] Attribute Git commands to a proven working directory and repository Phase B of the git outcome and effective-target immunity work (#4751). The write scanner's shell walk becomes the single cwd authority (_shell_segment_walk). Beyond the shell's logical directory it records whether a cd certainly ran, so a failed, dynamic, conditional, piped, backgrounded, function-body or compound-body cd leaves the cwd uncertain instead of being guessed. The tokenizer now keeps the operator that joined each segment, keeps it across newlines, and no longer lets a trailing comment swallow the command boundary. analyze_git_segment (_git_target) returns one analysis per segment: the invocation, the target (resolved, not a repository, or unresolved) and the outcome. It expands aliases, covers git-core executables and dynamic command words, and treats layout overrides and unparseable global options as unresolved rather than as another repository. git_ops_guard drops its own cwd tracking, judges every raw write target against its own repository, and skips commands that mention neither Git nor ref storage. Co-Authored-By: Claude Sonnet 5.5 --- docs/safety/hooks.md | 2 +- .../hooks/_classification/AGENTS.md | 13 +- .../hooks/_classification/_flags.py | 10 + .../hooks/_classification/_interpreters.py | 1 + .../hooks/_classification/_shell_structure.py | 2 +- .../hooks/_classification/_tokenizer.py | 43 +- src/autoskillit/hooks/_runtime/AGENTS.md | 9 + .../_runtime/_git_command_classification.py | 19 +- src/autoskillit/hooks/_runtime/_git_target.py | 282 +++++++++ .../hooks/_runtime/_hook_payload.py | 19 +- .../hooks/_runtime/_shell_segment_walk.py | 463 ++++++++++++++ .../hooks/_runtime/_write_target_scan.py | 334 +--------- src/autoskillit/hooks/guards/AGENTS.md | 23 +- src/autoskillit/hooks/guards/git_ops_guard.py | 597 ++++++++---------- tests/_ambient_env_surface.py | 27 + tests/arch/test_git_ref_qualification.py | 1 + tests/arch/test_hook_path_identity_guard.py | 6 +- .../test_hook_raw_command_scan_inventory.py | 10 +- tests/arch/test_regex_guards.py | 39 +- .../arch/test_subpackage_isolation_facades.py | 2 + tests/hooks/test_command_classification.py | 25 + tests/hooks/test_git_effect_differential.py | 22 +- tests/hooks/test_git_target.py | 583 +++++++++++++++++ tests/hooks/test_hook_payload.py | 149 +++++ tests/hooks/test_shell_segment_walk.py | 332 ++++++++++ tests/infra/_git_ops_guard_harness.py | 236 +++++++ tests/infra/test_git_ops_guard.py | 427 ++++++------- tests/infra/test_git_ops_guard_attribution.py | 320 ++++++++++ 28 files changed, 3025 insertions(+), 971 deletions(-) create mode 100644 src/autoskillit/hooks/_runtime/_git_target.py create mode 100644 src/autoskillit/hooks/_runtime/_shell_segment_walk.py create mode 100644 tests/hooks/test_git_target.py create mode 100644 tests/hooks/test_hook_payload.py create mode 100644 tests/hooks/test_shell_segment_walk.py create mode 100644 tests/infra/_git_ops_guard_harness.py create mode 100644 tests/infra/test_git_ops_guard_attribution.py diff --git a/docs/safety/hooks.md b/docs/safety/hooks.md index 9fe77e507e..3562313f93 100644 --- a/docs/safety/hooks.md +++ b/docs/safety/hooks.md @@ -687,7 +687,7 @@ as a defense-in-depth measure against privilege escalation: | `background_exec_guard.py` | Unrecognized `AUTOSKILLIT_SESSION_TYPE` | Unknown session type should not bypass `run_in_background` prohibition | | `github_mutation_guard.py` | Ambiguous or unresolved GitHub mutation command, or unexpected classifier error | Unknown mutation scope must not bypass the structured review publisher | | `exploration_request_identity_guard.py` | A supported exploration event lacks bounded native identity or its one-shot record cannot be written | A Claude exploration call must not execute without request-correlated authority | -| `git_ops_guard.py` | Any unexpected runtime error (any `Exception`) while classifying a Git command, in the checked-out-ref preflight or the headless destructive-op phase; or, in the separate headless destructive-op-blocking preflight, an unrecognized global git flag that leaves the real subcommand unresolved | An unhandled exception must not silently allow a checked-out ref mutation — use exit 2 + stderr to hard-block. Separately, `_git_command_classification._contains_blocked_git_op` cannot match `_BLOCKED_GIT_OPS`'s literal subcommand tuples against an unresolved subcommand, so it denies unconditionally the moment `extract_git_subcommand_and_flags` reports `""`, rather than silently falling through to "not blocked" | +| `git_ops_guard.py` | Any unexpected runtime error (any `Exception`) while classifying a Git command, in the checked-out-ref preflight or the headless destructive-op phase; or an unresolved target (unknown cwd, layout override or unparseable global option) for a ref mutation; or, in the separate headless destructive-op-blocking preflight, an unrecognized global git flag that leaves the real subcommand unresolved | An unhandled exception must not silently allow a checked-out ref mutation — use exit 2 + stderr to hard-block. Separately, `_git_command_classification._contains_blocked_git_op` cannot match `_BLOCKED_GIT_OPS`'s literal subcommand tuples against an unresolved subcommand, so it denies unconditionally the moment `extract_git_subcommand_and_flags` reports `""`, rather than silently falling through to "not blocked" | | `pr_create_guard.py` | Hook config unreadable or malformed while the kitchen is open (OSError, JSONDecodeError, AttributeError, TypeError) | An unresolvable `recipe_allows_pr_create` authorization must not be read as permission to bypass the prepare_pr → compose_pr pipeline | | `unsafe_install_guard.py` | An unrecognized global pip flag leaves `pip`'s `install` token position unresolved | `_find_pip_install` cannot tell whether the command is a pip install at all; treating that the same as "definitely not an install" would silently skip the editable/system-install checks entirely, so it is threaded through as a distinct `"unresolved-pip-flags"` kind and denied unconditionally, matching the pre-existing `"unresolved-subprocess"` kind's treatment | | `write_guard.py` | A write-verb operand or redirect target cannot be resolved while a write-prefix policy applies | An unresolved target could escape the allowed prefix; the denial explains how to substitute a literal path. | diff --git a/src/autoskillit/hooks/_classification/AGENTS.md b/src/autoskillit/hooks/_classification/AGENTS.md index 3c52d66581..cf084ebc2a 100644 --- a/src/autoskillit/hooks/_classification/AGENTS.md +++ b/src/autoskillit/hooks/_classification/AGENTS.md @@ -43,8 +43,17 @@ block B bootstrap so hook scripts can keep importing via Sole producer of these symbols; the facade re-exports them. `scan_write_targets` and `WriteTargetScan` are defined in `_runtime/_write_target_scan.py` and re-exported by -`_runtime/_command_classification.py`. The scanner tracks mutable cwd and scope -state across evaluated commands. +`_runtime/_command_classification.py`. The scanner consumes +`_runtime/_shell_segment_walk.py`, the single authority for the cwd in effect for +each evaluated segment: it tracks mutable cwd and scope state and, beyond the +shell's logical directory, whether a `cd` certainly ran (`cwd_certain`). + +`_CommandSegment` carries the logical operator that joined it to the previous +segment (`connector`: `&&`, `||`, `|`, `&`, `;` or empty) and whether it is the first +statement after a dropped bare keyword line (`after_keyword`). The first operator +after a command wins, so a newline after `&&`, `||`, `|` or `&` keeps that operator. +`_shell_structure` emits the newline boundary before its `;`, so a trailing `#` +comment cannot swallow the boundary. ## Bootstrap Conventions diff --git a/src/autoskillit/hooks/_classification/_flags.py b/src/autoskillit/hooks/_classification/_flags.py index 9b9b610416..38b8384886 100644 --- a/src/autoskillit/hooks/_classification/_flags.py +++ b/src/autoskillit/hooks/_classification/_flags.py @@ -259,6 +259,16 @@ def _consume_str_flag( "--namespace": _FlagArity.VALUE, "--config-env": _FlagArity.VALUE, "--super-prefix": _FlagArity.VALUE, + "--attr-source": _FlagArity.VALUE, + "--list-cmds": _FlagArity.VALUE, + "--no-optional-locks": _FlagArity.BOOLEAN, + "--literal-pathspecs": _FlagArity.BOOLEAN, + "--no-literal-pathspecs": _FlagArity.BOOLEAN, + "--glob-pathspecs": _FlagArity.BOOLEAN, + "--noglob-pathspecs": _FlagArity.BOOLEAN, + "--icase-pathspecs": _FlagArity.BOOLEAN, + "--no-lazy-fetch": _FlagArity.BOOLEAN, + "--no-advice": _FlagArity.BOOLEAN, # --exec-path[=] is optional-value (usable bare, or with `=`); this # module's binary arity model can't express "optional". BOOLEAN is the # correct default for its common bare usage; the rare `=`-form diff --git a/src/autoskillit/hooks/_classification/_interpreters.py b/src/autoskillit/hooks/_classification/_interpreters.py index 3716c1b01e..aefd23aede 100644 --- a/src/autoskillit/hooks/_classification/_interpreters.py +++ b/src/autoskillit/hooks/_classification/_interpreters.py @@ -630,6 +630,7 @@ def all_evaluated_segments_with_provenance( item.argv_tokens, scope, cwd_override=cwd_override, + syntax=item, ) ) else: diff --git a/src/autoskillit/hooks/_classification/_shell_structure.py b/src/autoskillit/hooks/_classification/_shell_structure.py index 8545e695d9..15a91b4e5d 100644 --- a/src/autoskillit/hooks/_classification/_shell_structure.py +++ b/src/autoskillit/hooks/_classification/_shell_structure.py @@ -110,7 +110,7 @@ def _normalize_newlines_for_tokenize(source: SourceMappedText) -> SourceMappedTe elif c == '"' and not in_single: in_double = not in_double elif c == "\n" and not in_single and not in_double: - builder.emit(" ; \n", i, i + 1) + builder.emit("\n ; ", i, i + 1) i += 1 continue builder.copy(i, i + 1) diff --git a/src/autoskillit/hooks/_classification/_tokenizer.py b/src/autoskillit/hooks/_classification/_tokenizer.py index 86dae0ce28..a2a789c15e 100644 --- a/src/autoskillit/hooks/_classification/_tokenizer.py +++ b/src/autoskillit/hooks/_classification/_tokenizer.py @@ -45,6 +45,8 @@ # Operators that terminate a shlex token and split command segments. _SHELL_OPERATORS: frozenset[str] = frozenset({"&&", "||", ";", "|", "&"}) +_LOGICAL_CONNECTORS: frozenset[str] = frozenset({"&&", "||", "|", "&"}) +_BARE_KEYWORDS: frozenset[str] = frozenset({"if", "while", "until", "then", "else", "elif", "do"}) _HEREDOC_PLACEHOLDER_RE = re.compile(r"__AUTOSKILLIT_HEREDOC_(\d+)__") @@ -372,6 +374,8 @@ class _CommandSegment: piped_from_previous: bool = False function_body: bool = False subshell_path: tuple[int, ...] = () + connector: str = "" + after_keyword: bool = False @dataclass(frozen=True, slots=True) @@ -383,6 +387,8 @@ class EvaluatedSegment: is absent only for literal Python subprocess argv, which was not shell-lexed. ``subshell_path`` is None when the payload's owning segment is unknown. ``cwd_override`` is a Python subprocess's explicitly configured initial cwd. + ``syntax`` is the tokenized segment this record came from, including segments + of evaluated payloads; it is None for literal Python subprocess argv. """ tokens: list[str] @@ -391,6 +397,7 @@ class EvaluatedSegment: argv_tokens: list[ArgvToken] | None subshell_path: tuple[int, ...] | None cwd_override: str | None = None + syntax: _CommandSegment | None = None def __post_init__(self) -> None: if not self.tokens: @@ -621,6 +628,22 @@ def _restore_substitutions( return token, raw_span, quoted +def _connector_after_operator(pending: str, token: str) -> str: + """The operator joining the next segment, given the one already pending. + + The first operator after a command wins: a newline (rewritten to ``;``) + after ``&&``/``||``/``|``/``&`` must not erase it. + """ + if pending in _LOGICAL_CONNECTORS and token == ";": + return pending + return token if token in _SHELL_OPERATORS else ";" + + +def _connector_after_group(kind: str, pending: str) -> str: + """A closing brace or subshell ends the operator context it was opened in.""" + return "" if kind in {"close_brace", "close_subshell"} else pending + + def _advance_group_context( kind: str, group_id: int, @@ -664,16 +687,16 @@ def _tokenize_command_segments_with_redirects(command: str) -> list[_CommandSegm subshell_path: tuple[int, ...] = () function_depth = 0 brace_functions: list[bool] = [] + connector = "" + after_keyword = False def flush() -> None: nonlocal current_tokens, current_redirect_syntax, current_argv_tokens - nonlocal current_stdin_literals + nonlocal current_stdin_literals, connector, after_keyword + bare_keyword = len(current_tokens) == 1 and current_tokens[0] in _BARE_KEYWORDS if ( current_tokens - and not ( - len(current_tokens) == 1 - and current_tokens[0] in {"if", "while", "until", "then", "else", "elif", "do"} - ) + and not bare_keyword and not ( len(current_tokens) == 1 and re.fullmatch(r"[A-Za-z_][A-Za-z_0-9]*\(\)", current_tokens[0]) @@ -689,8 +712,14 @@ def flush() -> None: piped_from_previous, function_body=function_depth > 0, subshell_path=subshell_path, + connector=connector, + after_keyword=after_keyword, ) ) + connector = "" + after_keyword = False + else: + after_keyword = after_keyword or bare_keyword current_tokens = [] current_redirect_syntax = [] current_argv_tokens = [] @@ -711,6 +740,7 @@ def flush() -> None: kind, group_id, subshell_path, function_depth, brace_functions ) piped_from_previous = False + connector = _connector_after_group(kind, connector) index += 1 continue @@ -729,7 +759,8 @@ def flush() -> None: if token in _SHELL_OPERATORS or _is_case_terminator(token): flush() - piped_from_previous = token == "|" + connector = _connector_after_operator(connector, token) + piped_from_previous = connector == "|" index += 1 continue diff --git a/src/autoskillit/hooks/_runtime/AGENTS.md b/src/autoskillit/hooks/_runtime/AGENTS.md index e80e84f284..c240bcce45 100644 --- a/src/autoskillit/hooks/_runtime/AGENTS.md +++ b/src/autoskillit/hooks/_runtime/AGENTS.md @@ -45,6 +45,15 @@ logic, with zero consumers outside this one file. - `_write_target_scan.py` — scope-aware write-target scanning across evaluated commands. It imports `_command_classification` inside scan operations, after the facade has loaded, so the facade can re-export its result type without an import cycle. + It consumes `_shell_segment_walk.py`, which owns the cwd walk (`walk_evaluated_segments`, + `WalkedSegment`, `cwd_certain`) and reaches the facade only through its lazy + `_classification_module()` accessor. + +- `_git_target.py` — which repository a walked Git segment acts on and what it does + there (`analyze_git_segment`): RESOLVED, NOT_A_REPOSITORY or UNRESOLVED, never a + guessed repository. It imports `_shell_segment_walk`, `_git_command_classification`, + `_git_outcome`, `_command_classification`, `_github_mutation_analysis` and + `_path_identity`; `guards/git_ops_guard.py` imports it. - `_git_command_classification.py` — Git operation classification used by `guards/git_ops_guard.py`; it shares the runtime command and mutation primitives. diff --git a/src/autoskillit/hooks/_runtime/_git_command_classification.py b/src/autoskillit/hooks/_runtime/_git_command_classification.py index 292b0d4e45..53644826a0 100644 --- a/src/autoskillit/hooks/_runtime/_git_command_classification.py +++ b/src/autoskillit/hooks/_runtime/_git_command_classification.py @@ -1,7 +1,7 @@ """Classification primitives for git operations used by git_ops_guard. The orchestrator (`git_ops_guard.py`) imports a controlled subset of the public -surface (`_classify_git_segment`, `_classify_git_invocation`, +surface (`_classify_git_invocation`, `_contains_blocked_git_op`, `_git_result`, `_git_text`, `_parse_worktree_owners`, `_resolve_attempted_sha`, `_resolve_git_common_dir`) and dispatches into the inner classifiers transitively through `_classify_git_invocation`. @@ -26,7 +26,7 @@ invocation is. Public API surface: - Classifiers: _classify_git_segment, _classify_git_invocation, + Classifiers: _classify_git_invocation, _classify_fetch, _classify_push, _classify_update_ref, _classify_reset, _classify_outcome, _contains_blocked_git_op Helpers (consumed transitively by the classifiers above): @@ -143,6 +143,7 @@ def _git_result(cwd: str, *args: str) -> subprocess.CompletedProcess[bytes]: check=False, text=False, timeout=10, + env={**os.environ, "LC_ALL": "C", "LANGUAGE": ""}, ) @@ -449,17 +450,3 @@ def _classify_git_invocation( elif subcommand == "push": return _classify_push(args, context, owned_refs) return [one] if one is not None else [] - - -def _classify_git_segment( - segment: list[str], context: dict[str, object] -) -> list[tuple[str, str, bool]]: - parsed = extract_git_subcommand_and_flags(segment) - if parsed is None: - return [] - if parsed.subcommand == "": - # An unrecognized global git flag means the real subcommand could - # not be found -- ambiguous-deny (route through _all_threatened - # against every owned ref), not "nothing to check here". - return [("", "", True)] - return _classify_git_invocation(parsed, context) diff --git a/src/autoskillit/hooks/_runtime/_git_target.py b/src/autoskillit/hooks/_runtime/_git_target.py new file mode 100644 index 0000000000..ff280a3c2b --- /dev/null +++ b/src/autoskillit/hooks/_runtime/_git_target.py @@ -0,0 +1,282 @@ +"""Which repository a Git command acts on, and what it does there. + +Members: GitTargetKind, GitTarget, ChannelFlags, GitSegmentFacts, +command_channel_flags, analyze_git_segment, resolve_git_target_for_path, +expand_git_alias, _GIT_BUILTIN_SUBCOMMANDS, _GIT_MUTATION_SUBCOMMANDS. + +``analyze_git_segment`` is the single analysis entry for a walked shell +segment. The target is RESOLVED only from a proven working directory; an +unknown cwd, a dynamic ``-C``, a repository-layout override or an unparseable +global option is UNRESOLVED, never a guessed repository. A path that is not +inside any repository is NOT_A_REPOSITORY. Callers decide what each kind +means for the command at hand. + +Every Git query goes through ``_git_result`` (locale-stable), once per +canonical path per ``cache``. +""" + +from __future__ import annotations + +import os +import re +from enum import StrEnum +from typing import TYPE_CHECKING, Any, Literal, NamedTuple + +from _command_classification import ( + GitInvocation, + _git_subcommand_index, + extract_git_subcommand_and_flags, + tokenize_command_segments, +) +from _git_command_classification import _git_result +from _git_outcome import GitHeadEffect, GitRefOutcome, git_ref_outcome +from _github_mutation_analysis import _DYNAMIC_SHELL_TOKEN_RE +from _path_identity import PathIdentityError, canonical_existing_path + +if TYPE_CHECKING: + from autoskillit.hooks._runtime._shell_segment_walk import WalkedSegment + +# Git subcommands that can mutate checked-out refs. Read-only commands (log, +# status, diff, show, ...) must not be denied just because a preceding cd failed. +_GIT_MUTATION_SUBCOMMANDS: frozenset[str] = frozenset( + {"update-ref", "branch", "checkout", "switch", "reset", "fetch", "push", "symbolic-ref"} +) + +# ``git --list-cmds=builtins`` on the reference Git. Aliases cannot shadow these. +_GIT_BUILTIN_SUBCOMMANDS: frozenset[str] = frozenset( + ( + "add am annotate apply archive bisect blame branch bugreport bundle cat-file " + "check-attr check-ignore check-mailmap check-ref-format checkout checkout--worker " + "checkout-index cherry cherry-pick clean clone column commit commit-graph commit-tree " + "config count-objects credential credential-cache credential-cache--daemon " + "credential-store describe diagnose diff diff-files diff-index diff-tree difftool " + "fast-export fast-import fetch fetch-pack fmt-merge-msg for-each-ref for-each-repo " + "format-patch fsck fsck-objects fsmonitor--daemon gc get-tar-commit-id grep " + "hash-object help hook index-pack init init-db interpret-trailers log ls-files " + "ls-remote ls-tree mailinfo mailsplit maintenance merge merge-base merge-file " + "merge-index merge-ours merge-recursive merge-recursive-ours merge-recursive-theirs " + "merge-subtree merge-tree mktag mktree multi-pack-index mv name-rev notes " + "pack-objects pack-redundant pack-refs patch-id pickaxe prune prune-packed pull push " + "range-diff read-tree rebase receive-pack reflog remote remote-ext remote-fd repack " + "replace rerere reset restore rev-list rev-parse revert rm send-pack shortlog show " + "show-branch show-index show-ref sparse-checkout stage stash status stripspace " + "submodule--helper switch symbolic-ref tag unpack-file unpack-objects update-index " + "update-ref update-server-info upload-archive upload-archive--writer upload-pack var " + "verify-commit verify-pack verify-tag version whatchanged worktree write-tree" + ).split() +) + +_LAYOUT_ENV_NAMES: tuple[str, ...] = ("GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR") +_LAYOUT_CHANNEL_RE = re.compile( + r"\b(?:GIT_DIR|GIT_WORK_TREE|GIT_COMMON_DIR)=|--git-dir\b|--work-tree\b|--bare\b" + r"|core\.(?:worktree|bare)|include(?:If)?\." +) +_ALIAS_CHANNEL_RE = re.compile(r"alias\.|GIT_CONFIG|\bHOME=|XDG_CONFIG_HOME=") +_ALIAS_KEY_RE = re.compile(r"[A-Za-z0-9][A-Za-z0-9-]*") + + +class GitTargetKind(StrEnum): + RESOLVED = "resolved" + NOT_A_REPOSITORY = "not_a_repository" + UNRESOLVED = "unresolved" + + +class GitTarget(NamedTuple): + kind: GitTargetKind + # "" unless UNRESOLVED: cwd_unknown | dynamic_cwd | layout_override | + # unparsed_global_options | git_query_failed + reason: str + cwd: str + worktree_git_dir: str + common_git_dir: str + is_main_worktree: bool + toplevel: str | None + + +class ChannelFlags(NamedTuple): + layout_override: bool + alias_channel: bool + + +class GitSegmentFacts(NamedTuple): + invocation: GitInvocation + target: GitTarget + outcome: GitRefOutcome | None + identity_unknown: bool + + +def _unresolved(reason: str, cwd: str = "") -> GitTarget: + return GitTarget(GitTargetKind.UNRESOLVED, reason, cwd, "", "", False, None) + + +def command_channel_flags(live_text: str) -> ChannelFlags: + """Command-scoped channels that can redirect a Git command's repository or identity. + + The layout channel also trips on a layout variable in the hook's own + environment, since that redirects the guard's own Git queries. The alias + channel is read from the command text only, never from an ambient ``HOME``. + """ + layout = _LAYOUT_CHANNEL_RE.search(live_text) is not None + if not layout: + for name, value in os.environ.items(): + if name in _LAYOUT_ENV_NAMES and value: + layout = True + break + return ChannelFlags(layout, _ALIAS_CHANNEL_RE.search(live_text) is not None) + + +def _nearest_existing_directory(path: str) -> str: + candidate = path + while candidate and not os.path.isdir(candidate): + parent = os.path.dirname(candidate) + if parent == candidate: + return "" + candidate = parent + return candidate + + +def _probe_repository(path: str) -> GitTarget: + probe = _git_result(path, "rev-parse", "--absolute-git-dir", "--git-common-dir") + if probe.returncode != 0: + if b"not a git repository" in probe.stderr: + return GitTarget(GitTargetKind.NOT_A_REPOSITORY, "", path, "", "", False, None) + return _unresolved("git_query_failed", path) + git_dir_raw, common_raw = probe.stdout.decode("utf-8", errors="strict").splitlines()[:2] + worktree_git_dir = canonical_existing_path(git_dir_raw) + # ``--git-common-dir`` is relative to the queried path (``.`` inside ``.git``). + common_git_dir = canonical_existing_path(os.path.join(path, common_raw)) + # ``--show-toplevel`` fails inside ``.git`` and in a bare repository, so it + # is a separate query. + top = _git_result(path, "rev-parse", "--show-toplevel") + toplevel = top.stdout.decode("utf-8", errors="strict").strip() if top.returncode == 0 else None + return GitTarget( + GitTargetKind.RESOLVED, + "", + path, + str(worktree_git_dir), + str(common_git_dir), + worktree_git_dir == common_git_dir, + toplevel, + ) + + +def _query_repository(path: str, cache: dict[Any, Any]) -> GitTarget: + key = ("repository", path) + if key not in cache: + cache[key] = _probe_repository(path) + target: GitTarget = cache[key] + return target + + +def resolve_git_target_for_path(path: str, cache: dict[Any, Any]) -> GitTarget: + """The repository containing *path*: its nearest existing ancestor directory.""" + directory = _nearest_existing_directory(path) + if not directory: + return _unresolved("cwd_unknown") + try: + canonical = str(canonical_existing_path(directory)) + except PathIdentityError: + return _unresolved("cwd_unknown") + return _query_repository(canonical, cache) + + +def _git_invocation(seg: WalkedSegment) -> tuple[GitInvocation | None, bool]: + """The Git invocation a segment runs, and whether the command word is dynamic.""" + argv = seg.executable[seg.start :] if seg.start is not None else [] + if not argv: + return None, False + verb = argv[0] + if verb == "git" or verb.endswith("/git"): + return extract_git_subcommand_and_flags(argv), False + name = os.path.basename(verb) + if name.startswith("git-") and name[4:] in _GIT_BUILTIN_SUBCOMMANDS: + return GitInvocation(name[4:], argv[1:], [], seg.executable[: seg.start]), False + if ( + _DYNAMIC_SHELL_TOKEN_RE.search(verb) + and len(argv) > 1 + and argv[1] in _GIT_MUTATION_SUBCOMMANDS + ): + return GitInvocation(argv[1], argv[2:], [], seg.executable[: seg.start]), True + return None, False + + +def _segment_target( + seg: WalkedSegment, invocation: GitInvocation, flags: ChannelFlags, cache: dict[Any, Any] +) -> GitTarget: + if invocation.subcommand == "": + return _unresolved("unparsed_global_options") + if flags.layout_override: + return _unresolved("layout_override") + if not seg.cwd_certain or not seg.verb_cwd: + return _unresolved("cwd_unknown") + start = seg.start or 0 + base = os.path.normpath(seg.verb_cwd) + tokens = None if seg.executable_tokens is None else seg.executable_tokens[start:] + # ``-C`` is applied without normalization: chdir resolves ``..`` physically. + _, cwd, _ = _git_subcommand_index(seg.executable[start:], base, tokens) + if not cwd: + return _unresolved("dynamic_cwd") + return resolve_git_target_for_path(cwd, cache) + + +def expand_git_alias( + target: GitTarget, invocation: GitInvocation +) -> GitInvocation | Literal["unknown"] | None: + """Expand ``alias.``; None for an external command, "unknown" when opaque.""" + result = _git_result(target.cwd, "config", "--get", f"alias.{invocation.subcommand}") + if result.returncode == 1: + return None + if result.returncode != 0: + return "unknown" + value = result.stdout.decode("utf-8", errors="strict").strip() + if value.startswith("!"): + return "unknown" + segments = tokenize_command_segments(value) + if len(segments) != 1: + return "unknown" + expanded = extract_git_subcommand_and_flags(["git", *segments[0], *invocation.flags]) + if expanded is None or expanded.subcommand not in _GIT_BUILTIN_SUBCOMMANDS: + return "unknown" + return expanded + + +def _identity_unknown(invocation: GitInvocation, flags: ChannelFlags, dynamic_verb: bool) -> bool: + subcommand = invocation.subcommand + if dynamic_verb: + return True + if subcommand == "": + return False + return ( + _DYNAMIC_SHELL_TOKEN_RE.search(subcommand) is not None + or _ALIAS_KEY_RE.fullmatch(subcommand) is None + or (flags.alias_channel and subcommand not in _GIT_BUILTIN_SUBCOMMANDS) + ) + + +def analyze_git_segment( + seg: WalkedSegment, flags: ChannelFlags, cache: dict[Any, Any] +) -> GitSegmentFacts | None: + """Invocation, target and outcome of a walked segment; None for a non-Git command.""" + invocation, dynamic_verb = _git_invocation(seg) + if invocation is None: + return None + identity_unknown = _identity_unknown(invocation, flags, dynamic_verb) + target = _segment_target(seg, invocation, flags, cache) + if ( + target.kind == GitTargetKind.RESOLVED + and not identity_unknown + and invocation.subcommand not in _GIT_BUILTIN_SUBCOMMANDS + ): + expanded = expand_git_alias(target, invocation) + if expanded is None: + return None + if expanded == "unknown": + identity_unknown = True + else: + invocation = expanded + outcome = ( + GitRefOutcome(None, False, "HEAD", True, GitHeadEffect.AMBIGUOUS) + if identity_unknown + else git_ref_outcome(invocation.subcommand, invocation.flags) + ) + return GitSegmentFacts(invocation, target, outcome, identity_unknown) diff --git a/src/autoskillit/hooks/_runtime/_hook_payload.py b/src/autoskillit/hooks/_runtime/_hook_payload.py index 9f5d2fbfb3..f1232bb47d 100644 --- a/src/autoskillit/hooks/_runtime/_hook_payload.py +++ b/src/autoskillit/hooks/_runtime/_hook_payload.py @@ -22,6 +22,7 @@ class PayloadAnomaly(StrEnum): NON_STRING_COMMAND = "non_string_command" NON_STRING_CWD = "non_string_cwd" RELATIVE_CWD = "relative_cwd" + RELATIVE_WORKDIR = "relative_workdir" class ParsedHookCommand(NamedTuple): @@ -58,6 +59,22 @@ def _absolute_string_or_blank(value: Any) -> tuple[str, PayloadAnomaly | None]: return ("", PayloadAnomaly.RELATIVE_CWD) +def _execution_cwd( + tool_kind: str, raw_cwd: Any, tool_input: dict[str, Any] +) -> tuple[str, PayloadAnomaly | None]: + """The directory the command runs in. + + A Bash-shaped payload may carry a per-call ``workdir`` (Codex); when it + does, that, not the session cwd, is where the command runs. + """ + workdir = tool_input.get("workdir") + if tool_kind == "bash" and isinstance(workdir, str): + if os.path.isabs(workdir): + return (workdir, None) + return ("", PayloadAnomaly.RELATIVE_WORKDIR) + return _absolute_string_or_blank(raw_cwd) + + def normalize_payload_cwd(value: Any) -> str: """Return an absolute payload cwd, or blank for missing or malformed values.""" cwd, _ = _absolute_string_or_blank(value) @@ -116,7 +133,7 @@ def parse_hook_command(data: dict[str, Any]) -> ParsedHookCommand: command = None anomalies.append(PayloadAnomaly.NON_STRING_COMMAND) - execution_cwd, cwd_anomaly = _absolute_string_or_blank(raw_execution_cwd) + execution_cwd, cwd_anomaly = _execution_cwd(tool_kind, raw_execution_cwd, tool_input) if cwd_anomaly is not None: anomalies.append(cwd_anomaly) diff --git a/src/autoskillit/hooks/_runtime/_shell_segment_walk.py b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py new file mode 100644 index 0000000000..0068ce5c7d --- /dev/null +++ b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py @@ -0,0 +1,463 @@ +"""Scope-aware walk over evaluated shell segments: the single cwd authority. + +Members: ShellDirectoryState, WalkedSegment, walk_evaluated_segments. + +Every segment that will actually execute is visited once, in order, with the +working directory in effect for it. ``verb_cwd`` is the shell's logical +directory (what the write scan consumes); ``cwd_certain`` additionally says +whether that directory is *proven*: a ``cd`` counts only when it certainly ran +and succeeded before the segment. A failed, dynamic, conditional, piped, +backgrounded, function-body or compound-body ``cd`` leaves the cwd uncertain, +and an uncertain cwd must never be used to attribute a command to a +repository. + +The facade and ``_interpreters`` are reached only through +``_classification_module()``: a top-level import would cycle, because the +facade imports ``_write_target_scan`` (which imports this module) at its bottom. +""" + +from __future__ import annotations + +import os +import re +from collections.abc import Sequence +from dataclasses import dataclass +from types import ModuleType +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from autoskillit.hooks._classification._output_redirect import OutputRedirectPartition + from autoskillit.hooks._classification._tokenizer import ArgvToken, EvaluatedSegment + +_DIRECTORY_COMMANDS: frozenset[str] = frozenset({"cd", "pushd", "popd"}) +_LOGICAL_CONNECTORS: frozenset[str] = frozenset({"&&", "||", "|", "&"}) +_TAINTING_FIRST_TOKENS: frozenset[str] = frozenset( + {"if", "while", "until", "for", "select", "case", "then", "do", "else", "elif", "{"} +) +_UNWRAPPED_BLOCKERS: frozenset[str] = frozenset( + {"env", "sudo", "nice", "nohup", "timeout", "stdbuf"} +) + + +@dataclass(slots=True) +class ShellDirectoryState: + cwd: str + cdpath_unknown: bool + cwd_certain: bool = False + # True while the cwd was reached through an unbroken ``&&`` chain from a cd. + chain_pending: bool = False + # True once the scope has run a conditionally or asynchronously joined command. + scope_tainted: bool = False + # True when the last directory change did not depend on the previous cwd. + cwd_independent: bool = False + + def fork(self) -> ShellDirectoryState: + """State a child scope starts from: the cwd, not the parent's control flow.""" + return ShellDirectoryState(self.cwd, self.cdpath_unknown, self.cwd_certain) + + +@dataclass(frozen=True, slots=True) +class WalkedSegment: + record: EvaluatedSegment + partition: OutputRedirectPartition + executable: list[str] + executable_tokens: list[ArgvToken] | None + start: int | None + verb_cwd: str + cwd_certain: bool + handled_builtin: bool + position_unresolved: bool + + +def _classification_module() -> ModuleType: + if __package__: + from . import _command_classification as packaged_classification + + return packaged_classification + import _command_classification as standalone_classification + + return standalone_classification + + +def _ensure_scope_state( + scopes: dict[tuple[int, ...], ShellDirectoryState], path: tuple[int, ...] +) -> ShellDirectoryState: + for length in range(1, len(path) + 1): + prefix = path[:length] + if prefix not in scopes: + scopes[prefix] = scopes[prefix[:-1]].fork() + return scopes[path] + + +def _unwrapped_prefix(prefix: list[str]) -> bool: + """True when *prefix* contains no wrapper command — i.e. is the unwrapped shell-builtin form. + + The polarity is ``unwrapped``: returns True only when none of the wrapper + tokens (``env``, ``sudo``, ``nice``, ``nohup``, ``timeout``, ``stdbuf``) + appears in the prefix. + """ + return not any(token in _UNWRAPPED_BLOCKERS for token in prefix) + + +def _cdpath_setting(segment: list[str], start: int | None) -> tuple[bool, bool] | None: + _classification = _classification_module() + + prefix = segment if start is None else segment[:start] + if not _unwrapped_prefix(prefix): + return None + verb = segment[start] if start is not None else "" + shell_builtin = verb in { + "cd", + "pushd", + "popd", + "eval", + ".", + "source", + "export", + "unset", + "readonly", + "declare", + "typeset", + "local", + "read", + } + assignments = [ + token.partition("=")[2] + for token in prefix + if _classification._is_posix_assignment(token) and token.partition("=")[0] == "CDPATH" + ] + if assignments and (start is None or shell_builtin): + return bool(assignments[-1]), verb in {"cd", "pushd", "popd"} + if start is None or verb not in { + "export", + "unset", + "readonly", + "declare", + "typeset", + "local", + "read", + }: + return None + for token in segment[start + 1 :]: + if token.startswith("CDPATH="): + return bool(token.partition("=")[2]), False + if token == "CDPATH": + if verb == "unset": + return False, False + if verb != "export": + return True, False + return None + + +def _directory_command_path_index(argv: list[str]) -> int | None: + """Index of the ``cd`` operand in *argv*; ``len(argv)`` when absent, None when unknowable.""" + if argv[0] == "pushd": + return None if len(argv) != 2 or argv[1].startswith(("+", "-")) else 1 + path_index = 1 + while path_index < len(argv) and argv[path_index] in {"-L", "-P", "-e", "-@"}: + path_index += 1 + after_double_dash = False + if path_index < len(argv) and argv[path_index] == "--": + path_index += 1 + after_double_dash = True + if path_index == len(argv): + return path_index + if path_index != len(argv) - 1 or ( + argv[path_index] == "-" or (argv[path_index].startswith("-") and not after_double_dash) + ): + return None + return path_index + + +def _apply_directory_command( + verb: str, + argv: list[str], + argv_tokens: Sequence[ArgvToken] | None, + state: ShellDirectoryState, +) -> None: + _classification = _classification_module() + + state.cwd_independent = False + if verb == "popd": + state.cwd = "" + return + path_index = _directory_command_path_index(argv) + if path_index is None: + state.cwd = "" + return + if path_index == len(argv): + home = os.path.expanduser("~") + state.cwd = home if os.path.isabs(home) else "" + state.cwd_independent = bool(state.cwd) + return + path = argv[path_index] + if state.cdpath_unknown and not os.path.isabs(path): + state.cwd = "" + return + source = _classification._shell_source(argv_tokens, path_index) + state.cwd = _classification.resolve_write_target(path, state.cwd, shell_source=source) or "" + state.cwd_independent = ( + _classification.resolve_write_target(path, "", shell_source=source) is not None + ) + + +def _seed_child_scope( + path: tuple[int, ...] | None, + scopes: dict[tuple[int, ...], ShellDirectoryState], + pending_children: dict[ + tuple[int, ...], tuple[ShellDirectoryState, ShellDirectoryState | None] + ], +) -> None: + if path is None: + return + for length in range(len(path) - 1, -1, -1): + if path[length] >= 0: + continue + owner = path[:length] + pending = pending_children.get(owner) + if pending is None: + continue + child_scope = path[: length + 1] + snapshot, invoked = pending + if child_scope not in scopes: + scopes[child_scope] = (invoked or snapshot).fork() + pending_children[owner] = (snapshot, None) + return + + +def _override_state(state: ShellDirectoryState, override: str) -> None: + """Apply a Python ``cwd=`` override: certain when absolute, or relative to a certain cwd.""" + _classification = _classification_module() + + independent = _classification.resolve_write_target(override, "") is not None + state.cwd = _classification.resolve_write_target(override, state.cwd) or "" + state.cwd_certain = bool(state.cwd) and (independent or state.cwd_certain) + + +def _record_state( + record: EvaluatedSegment, + scopes: dict[tuple[int, ...], ShellDirectoryState], + overridden_scopes: set[tuple[int, ...]], + pending_children: dict[ + tuple[int, ...], tuple[ShellDirectoryState, ShellDirectoryState | None] + ], +) -> ShellDirectoryState: + path = record.subshell_path + _seed_child_scope(path, scopes, pending_children) + if path is None: + state = ShellDirectoryState("", True) + if record.cwd_override is not None: + _override_state(state, record.cwd_override) + return state + if record.cwd_override is not None: + negative_components = [index for index, component in enumerate(path) if component < 0] + override_scope = path[: negative_components[-1] + 1] if negative_components else path + if override_scope not in overridden_scopes: + _override_state(_ensure_scope_state(scopes, override_scope), record.cwd_override) + overridden_scopes.add(override_scope) + return _ensure_scope_state(scopes, path) + + +def _apply_shell_builtin( + executable: list[str], + start: int, + argv_tokens: Sequence[ArgvToken] | None, + state: ShellDirectoryState, +) -> bool: + if argv_tokens is None or not _unwrapped_prefix(executable[:start]): + return False + if start >= len(executable): + state.cwd = "" + state.cdpath_unknown = True + return True + argv = executable[start:] + verb = argv[0] + if verb in _DIRECTORY_COMMANDS: + _apply_directory_command(verb, argv, argv_tokens[start:], state) + return True + if verb in {".", "source"}: + state.cwd = "" + state.cdpath_unknown = True + return True + if verb == "eval": + if any( + "$" in token.raw_span or "`" in token.raw_span for token in argv_tokens[start + 1 :] + ): + state.cwd = "" + state.cdpath_unknown = True + return True + return False + + +def _invoked_child_state( + verb: str, + verb_cwd: str, + verb_certain: bool, + state: ShellDirectoryState, + path: tuple[int, ...] | None, +) -> ShellDirectoryState | None: + _classification = _classification_module() + + if path is None or verb_cwd == state.cwd: + return None + if _classification._is_shell_interpreter(verb) or re.fullmatch( + r"python(?:3(?:\.\d+)?)?", _classification._normalize_executable(verb) + ): + return ShellDirectoryState(verb_cwd, state.cdpath_unknown, verb_certain) + return None + + +def _connector(record: EvaluatedSegment) -> str: + return record.syntax.connector if record.syntax is not None else "" + + +def _next_scope_connector(records: Sequence[EvaluatedSegment], position: int) -> str: + """Connector of the next record in the same scope; child-scope records are skipped.""" + scope = records[position].subshell_path + for later in records[position + 1 :]: + if later.subshell_path == scope: + return _connector(later) + return "" + + +def _apply_connector_rules(state: ShellDirectoryState, record: EvaluatedSegment) -> None: + syntax = record.syntax + if ( + _connector(record) in _LOGICAL_CONNECTORS + or (syntax is not None and (syntax.function_body or syntax.after_keyword)) + or record.tokens[0] in _TAINTING_FIRST_TOKENS + ): + state.scope_tainted = True + if state.chain_pending and _connector(record) != "&&": + state.cwd_certain = False + state.chain_pending = False + + +def _settle_directory_command( + state: ShellDirectoryState, + record: EvaluatedSegment, + next_connector: str, + parent_certain: bool, +) -> None: + """Decide whether a ``cd``/``pushd``/``popd`` certainly ran before what follows.""" + connector = _connector(record) + piped = record.syntax is not None and record.syntax.piped_from_previous + if not state.cwd or not (state.cwd_independent or parent_certain): + state.cwd_certain = False + state.chain_pending = False + return + # An unbroken ``&&`` chain runs a segment only if the cd ran and succeeded. + chained = connector != "||" and not piped and next_connector == "&&" + certain = ( + connector in {"", ";"} + and not state.scope_tainted + and not piped + and next_connector not in {"|", "&"} + and os.path.isdir(state.cwd) + ) + state.cwd_certain = chained or certain + state.chain_pending = chained + + +def _unmodelled_directory_command(executable: list[str], start: int) -> bool: + verb = executable[start] + if verb == "builtin": + return len(executable) > start + 1 and executable[start + 1] in _DIRECTORY_COMMANDS + return verb in _DIRECTORY_COMMANDS and "command" in executable[:start] + + +def _walk_executable( + record: EvaluatedSegment, + partition: OutputRedirectPartition, + executable: list[str], + executable_tokens: list[ArgvToken] | None, + state: ShellDirectoryState, + next_connector: str, +) -> tuple[WalkedSegment, ShellDirectoryState | None]: + _classification = _classification_module() + + def walked(start: int | None, verb_cwd: str, certain: bool, handled: bool) -> WalkedSegment: + return WalkedSegment( + record, + partition, + executable, + executable_tokens, + start, + verb_cwd, + certain and bool(verb_cwd), + handled, + False, + ) + + start = _classification._verb_start_index(executable) + setting = _cdpath_setting(executable, start) if executable_tokens is not None else None + previous_cdpath = state.cdpath_unknown + if setting is not None: + state.cdpath_unknown = setting[0] + if start is None: + return walked(None, state.cwd, state.cwd_certain, False), None + parent_certain = state.cwd_certain + handled = _apply_shell_builtin(executable, start, executable_tokens, state) + if setting is not None and setting[1]: + state.cdpath_unknown = previous_cdpath + if handled and executable[start] in _DIRECTORY_COMMANDS: + _settle_directory_command(state, record, next_connector, parent_certain) + if _unmodelled_directory_command(executable, start): + state.cwd_certain = False + state.chain_pending = False + if handled or _classification.is_gh_command(executable): + return walked(start, state.cwd, state.cwd_certain, handled), None + verb_cwd = _classification._wrapped_verb_cwd(executable, start, state.cwd, executable_tokens) + absolute_override = _classification._wrapped_verb_cwd(executable, start, "", executable_tokens) + verb_certain = bool(verb_cwd) and (bool(absolute_override) or state.cwd_certain) + invoked = _invoked_child_state( + executable[start], verb_cwd, verb_certain, state, record.subshell_path + ) + return walked(start, verb_cwd, verb_certain, False), invoked + + +def walk_evaluated_segments(command: str, cwd: str) -> list[WalkedSegment] | None: + """Walk every evaluated segment of *command*, or None when it cannot be tokenized.""" + _classification = _classification_module() + + records = _classification._all_evaluated_segments_with_provenance_impl( + command, include_process_substitutions=True + ) + if records is None: + return None + scopes: dict[tuple[int, ...], ShellDirectoryState] = { + (): ShellDirectoryState(cwd, bool(os.environ.get("CDPATH")), bool(cwd)) + } + overridden_scopes: set[tuple[int, ...]] = set() + pending_children: dict[ + tuple[int, ...], tuple[ShellDirectoryState, ShellDirectoryState | None] + ] = {} + segments: list[WalkedSegment] = [] + for position, record in enumerate(records): + state = _record_state(record, scopes, overridden_scopes, pending_children) + _apply_connector_rules(state, record) + pre_command_state = state.fork() + partition = _classification._partition_output_redirect_indices( + record.tokens, + cwd=state.cwd, + redirect_syntax=record.redirect_syntax, + argv_tokens=record.argv_tokens, + ) + executable = [record.tokens[index] for index in partition.segments] + executable_tokens = ( + None + if record.argv_tokens is None + else [record.argv_tokens[index] for index in partition.segments] + ) + segment, invoked = _walk_executable( + record, + partition, + executable, + executable_tokens, + state, + _next_scope_connector(records, position), + ) + segments.append(segment) + if record.subshell_path is not None: + pending_children[record.subshell_path] = (pre_command_state, invoked) + return segments diff --git a/src/autoskillit/hooks/_runtime/_write_target_scan.py b/src/autoskillit/hooks/_runtime/_write_target_scan.py index 01f21f6985..2b586589db 100644 --- a/src/autoskillit/hooks/_runtime/_write_target_scan.py +++ b/src/autoskillit/hooks/_runtime/_write_target_scan.py @@ -3,10 +3,8 @@ from __future__ import annotations import os -import re from collections.abc import Sequence from dataclasses import dataclass -from types import ModuleType from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -43,20 +41,15 @@ def __post_init__(self) -> None: ) -@dataclass(slots=True) -class _ShellDirectoryState: - cwd: str - cdpath_unknown: bool - - -def _classification_module() -> ModuleType: +if TYPE_CHECKING: + from autoskillit.hooks._runtime import _shell_segment_walk +else: if __package__: - from . import _command_classification as packaged_classification - - return packaged_classification - import _command_classification as standalone_classification + from . import _shell_segment_walk + else: + import _shell_segment_walk - return standalone_classification +_classification_module = _shell_segment_walk._classification_module @dataclass(frozen=True, slots=True) @@ -361,228 +354,6 @@ def _npm_install_targets( return ([target], False, True) if target is not None else ([], True, True) -def _ensure_scope_state( - scopes: dict[tuple[int, ...], _ShellDirectoryState], path: tuple[int, ...] -) -> _ShellDirectoryState: - for length in range(1, len(path) + 1): - prefix = path[:length] - if prefix not in scopes: - inherited = scopes[prefix[:-1]] - scopes[prefix] = _ShellDirectoryState(inherited.cwd, inherited.cdpath_unknown) - return scopes[path] - - -def _unwrapped_prefix(prefix: list[str]) -> bool: - """True when *prefix* contains no wrapper command — i.e. is the unwrapped shell-builtin form. - - The polarity is ``unwrapped``: returns True only when none of the wrapper - tokens (``env``, ``sudo``, ``nice``, ``nohup``, ``timeout``, ``stdbuf``) - appears in the prefix. - """ - return not any( - token in {"env", "sudo", "nice", "nohup", "timeout", "stdbuf"} for token in prefix - ) - - -def _cdpath_setting(segment: list[str], start: int | None) -> tuple[bool, bool] | None: - _classification = _classification_module() - - prefix = segment if start is None else segment[:start] - if not _unwrapped_prefix(prefix): - return None - verb = segment[start] if start is not None else "" - shell_builtin = verb in { - "cd", - "pushd", - "popd", - "eval", - ".", - "source", - "export", - "unset", - "readonly", - "declare", - "typeset", - "local", - "read", - } - assignments = [ - token.partition("=")[2] - for token in prefix - if _classification._is_posix_assignment(token) and token.partition("=")[0] == "CDPATH" - ] - if assignments and (start is None or shell_builtin): - return bool(assignments[-1]), verb in {"cd", "pushd", "popd"} - if start is None or verb not in { - "export", - "unset", - "readonly", - "declare", - "typeset", - "local", - "read", - }: - return None - for token in segment[start + 1 :]: - if token.startswith("CDPATH="): - return bool(token.partition("=")[2]), False - if token == "CDPATH": - if verb == "unset": - return False, False - if verb != "export": - return True, False - return None - - -def _apply_directory_command( - verb: str, - argv: list[str], - argv_tokens: Sequence[ArgvToken] | None, - state: _ShellDirectoryState, -) -> None: - _classification = _classification_module() - - if verb == "popd": - state.cwd = "" - return - - if verb == "pushd": - if len(argv) != 2 or argv[1].startswith(("+", "-")): - state.cwd = "" - return - path_index = 1 - else: - path_index = 1 - while path_index < len(argv) and argv[path_index] in {"-L", "-P", "-e", "-@"}: - path_index += 1 - after_double_dash = False - if path_index < len(argv) and argv[path_index] == "--": - path_index += 1 - after_double_dash = True - if path_index == len(argv): - home = os.path.expanduser("~") - state.cwd = home if os.path.isabs(home) else "" - return - if path_index != len(argv) - 1 or ( - argv[path_index] == "-" or (argv[path_index].startswith("-") and not after_double_dash) - ): - state.cwd = "" - return - - path = argv[path_index] - if state.cdpath_unknown and not os.path.isabs(path): - state.cwd = "" - return - state.cwd = ( - _classification.resolve_write_target( - path, state.cwd, shell_source=_classification._shell_source(argv_tokens, path_index) - ) - or "" - ) - - -def _seed_child_scope( - path: tuple[int, ...] | None, - scopes: dict[tuple[int, ...], _ShellDirectoryState], - pending_children: dict[ - tuple[int, ...], tuple[_ShellDirectoryState, _ShellDirectoryState | None] - ], -) -> None: - if path is None: - return - for length in range(len(path) - 1, -1, -1): - if path[length] >= 0: - continue - owner = path[:length] - pending = pending_children.get(owner) - if pending is None: - continue - child_scope = path[: length + 1] - snapshot, invoked = pending - if child_scope not in scopes: - seed = invoked or snapshot - scopes[child_scope] = _ShellDirectoryState(seed.cwd, seed.cdpath_unknown) - pending_children[owner] = (snapshot, None) - return - - -def _record_state( - record: _classification.EvaluatedSegment, - scopes: dict[tuple[int, ...], _ShellDirectoryState], - overridden_scopes: set[tuple[int, ...]], - pending_children: dict[ - tuple[int, ...], tuple[_ShellDirectoryState, _ShellDirectoryState | None] - ], -) -> _ShellDirectoryState: - _classification = _classification_module() - - path = record.subshell_path - _seed_child_scope(path, scopes, pending_children) - if path is None: - state = _ShellDirectoryState("", True) - if record.cwd_override is not None: - state.cwd = _classification.resolve_write_target(record.cwd_override, "") or "" - return state - if record.cwd_override is not None: - negative_components = [index for index, component in enumerate(path) if component < 0] - override_scope = path[: negative_components[-1] + 1] if negative_components else path - if override_scope not in overridden_scopes: - override_state = _ensure_scope_state(scopes, override_scope) - override_state.cwd = ( - _classification.resolve_write_target(record.cwd_override, override_state.cwd) or "" - ) - overridden_scopes.add(override_scope) - return _ensure_scope_state(scopes, path) - - -def _apply_shell_builtin( - executable: list[str], - start: int, - argv_tokens: Sequence[ArgvToken] | None, - state: _ShellDirectoryState, -) -> bool: - if argv_tokens is None or not _unwrapped_prefix(executable[:start]): - return False - if start >= len(executable): - state.cwd = "" - state.cdpath_unknown = True - return True - argv = executable[start:] - verb = argv[0] - if verb in {"cd", "pushd", "popd"}: - _apply_directory_command(verb, argv, argv_tokens[start:], state) - return True - if verb in {".", "source"}: - state.cwd = "" - state.cdpath_unknown = True - return True - if verb == "eval": - if any( - "$" in token.raw_span or "`" in token.raw_span for token in argv_tokens[start + 1 :] - ): - state.cwd = "" - state.cdpath_unknown = True - return True - return False - - -def _invoked_child_state( - verb: str, - verb_cwd: str, - state: _ShellDirectoryState, - path: tuple[int, ...] | None, -) -> _ShellDirectoryState | None: - _classification = _classification_module() - - if path is None or verb_cwd == state.cwd: - return None - if _classification._is_shell_interpreter(verb) or re.fullmatch( - r"python(?:3(?:\.\d+)?)?", _classification._normalize_executable(verb) - ): - return _ShellDirectoryState(verb_cwd, state.cdpath_unknown) - return None - - def _scan_destination_command( verb: str, argv: list[str], @@ -599,99 +370,52 @@ def _scan_destination_command( return WriteTargetScan(tuple(targets), unresolved, True, True) -def _scan_executable( - executable: list[str], - argv_tokens: Sequence[ArgvToken] | None, - state: _ShellDirectoryState, - path: tuple[int, ...] | None, -) -> tuple[WriteTargetScan, _ShellDirectoryState | None]: +def _scan_verb(seg: _shell_segment_walk.WalkedSegment) -> WriteTargetScan: _classification = _classification_module() - start = _classification._verb_start_index(executable) - setting = _cdpath_setting(executable, start) if argv_tokens is not None else None - previous_cdpath = state.cdpath_unknown - if setting is not None: - state.cdpath_unknown = setting[0] - if start is None: - return WriteTargetScan((), False, True, False), None - handled = _apply_shell_builtin(executable, start, argv_tokens, state) - if setting is not None and setting[1]: - state.cdpath_unknown = previous_cdpath - if handled or _classification.is_gh_command(executable): - return WriteTargetScan((), False, True, False), None - argv = executable[start:] + if seg.start is None or seg.handled_builtin or _classification.is_gh_command(seg.executable): + return WriteTargetScan((), False, True, False) + argv = seg.executable[seg.start :] verb = argv[0] - verb_cwd = _classification._wrapped_verb_cwd(executable, start, state.cwd, argv_tokens) - invoked = _invoked_child_state(verb, verb_cwd, state, path) - verb_tokens = None if argv_tokens is None else argv_tokens[start:] + verb_tokens = None if seg.executable_tokens is None else seg.executable_tokens[seg.start :] verb_name = os.path.basename(verb) if verb_name == "npm": - targets, unresolved, has_write = _npm_install_targets(argv, verb_cwd, verb_tokens) - return WriteTargetScan(tuple(targets), unresolved, True, has_write), invoked + targets, unresolved, has_write = _npm_install_targets(argv, seg.verb_cwd, verb_tokens) + return WriteTargetScan(tuple(targets), unresolved, True, has_write) if verb_name in {"ln", "install"}: - return _scan_destination_command(verb_name, argv, verb_cwd, verb_tokens), invoked + return _scan_destination_command(verb_name, argv, seg.verb_cwd, verb_tokens) if verb_name in WRITE_VERBS: targets, unresolved = _classification.extract_write_verb_targets( - verb_name, argv, verb_cwd, argv_tokens=verb_tokens + verb_name, argv, seg.verb_cwd, argv_tokens=verb_tokens ) - return WriteTargetScan(tuple(targets), unresolved, True, True), invoked + return WriteTargetScan(tuple(targets), unresolved, True, True) if verb == "git" or verb.endswith("/git"): targets, unresolved, has_write = _classification._git_write_targets( - argv, verb_cwd, executable[:start], verb_tokens + argv, seg.verb_cwd, seg.executable[: seg.start], verb_tokens ) - return WriteTargetScan(tuple(targets), unresolved, True, has_write), invoked - return WriteTargetScan((), False, True, False), invoked + return WriteTargetScan(tuple(targets), unresolved, True, has_write) + return WriteTargetScan((), False, True, False) def scan_write_targets(command: str, cwd: str) -> WriteTargetScan: """Classify literal write targets and unresolved writes in evaluated shell commands.""" - _classification = _classification_module() - - records = _classification._all_evaluated_segments_with_provenance_impl( - command, include_process_substitutions=True - ) - if records is None: + walk = _shell_segment_walk.walk_evaluated_segments(command, cwd) + if walk is None: return WriteTargetScan((), False, False, False) targets: list[str] = [] entry_targets: list[str] = [] unresolved = False has_write = False - scopes: dict[tuple[int, ...], _ShellDirectoryState] = { - (): _ShellDirectoryState(cwd, bool(os.environ.get("CDPATH"))) - } - overridden_scopes: set[tuple[int, ...]] = set() - pending_children: dict[ - tuple[int, ...], tuple[_ShellDirectoryState, _ShellDirectoryState | None] - ] = {} - for record in records: - state = _record_state(record, scopes, overridden_scopes, pending_children) - pre_command_state = _ShellDirectoryState(state.cwd, state.cdpath_unknown) - partition = _classification._partition_output_redirect_indices( - record.tokens, - cwd=state.cwd, - redirect_syntax=record.redirect_syntax, - argv_tokens=record.argv_tokens, - ) - unresolved |= partition.unresolved - has_write |= bool(partition.file_redirect_count) - - executable = [record.tokens[index] for index in partition.segments] - executable_tokens = ( - None - if record.argv_tokens is None - else [record.argv_tokens[index] for index in partition.segments] - ) - verb_scan, invoked = _scan_executable( - executable, executable_tokens, state, record.subshell_path - ) + for seg in walk: + unresolved |= seg.partition.unresolved + has_write |= bool(seg.partition.file_redirect_count) + verb_scan = _scan_verb(seg) targets.extend(verb_scan.targets) entry_targets.extend(verb_scan.entry_targets) - targets.extend(partition.targets) + targets.extend(seg.partition.targets) unresolved |= verb_scan.unresolved has_write |= verb_scan.has_write - if record.subshell_path is not None: - pending_children[record.subshell_path] = (pre_command_state, invoked) return WriteTargetScan( tuple(dict.fromkeys(path for path in targets if path not in _PSEUDO_DEVICE_PATHS)), unresolved, @@ -699,7 +423,3 @@ def scan_write_targets(command: str, cwd: str) -> WriteTargetScan: has_write, tuple(dict.fromkeys(entry_targets)), ) - - -if TYPE_CHECKING: - from autoskillit.hooks._runtime import _command_classification as _classification diff --git a/src/autoskillit/hooks/guards/AGENTS.md b/src/autoskillit/hooks/guards/AGENTS.md index 9d24dd17ab..a996684628 100644 --- a/src/autoskillit/hooks/guards/AGENTS.md +++ b/src/autoskillit/hooks/guards/AGENTS.md @@ -20,7 +20,26 @@ The package initializer remains import-free. Branch, checkout, switch, symbolic-ref, update-ref, reset, push and fetch are classified by outcome (which branch ref a command rewrites and whether `HEAD` moves), read through Git's own option semantics (bundled, stuck, abbreviated and - negated spellings), never by matching option tokens. + negated spellings), never by matching option tokens. Attribution counts only a + certain cwd: a failed, dynamic, conditional, piped, backgrounded, function-body or + compound-body `cd`, a repository-layout override (`GIT_DIR`, `--git-dir`, `--bare`, + `core.worktree`), or an unparseable global option leaves the target unresolved, and an + unresolved target denies a command that can rewrite a branch ref. Aliases are + expanded; `git-core` executables are covered; each raw write target is judged against + its own repository. A Git-relevance prefilter keeps commands that mention neither Git + nor ref storage out of the classifier. Codex per-call `workdir` is honoured whenever + the hook payload provides it. + Known residuals, not pending decisions: a worktree created by the same command is + judged by its nearest existing ancestor; Python `os.chdir` is not modelled, nor is a + `cd` shadowed by a shell function defined in the same command; the conservative `cd` + rule over-denies (taint lasts for the scope); a raw write through a pre-existing + symlink alias into ref storage, from a command that names neither Git nor ref storage, + passes the prefilter; a recursive delete of a dynamic directory that contains ref + storage (`rm -rf "$REPO"`) is allowed; Git's executable repository state (hooks, + `core.hooksPath`, filter and diff drivers, `credential.helper`, merge drivers) changes + what a command does without changing its text. The guard raises the cost of mistakes + and bypasses for Git ref and `HEAD` mutations; it is not a security boundary against a + hostile shell. In headless skill sessions, the later legacy Git phase blocks `commit --amend`, `push --force`, `reset --hard`, `clean -f`, and `checkout .`. Raw GitHub review publication is fail-closed across Bash and `run_cmd`: the guard denies @@ -85,7 +104,7 @@ defense-in-depth measure against privilege escalation: | `github_mutation_guard.py` | Ambiguous or unresolved GitHub mutation command | Unknown mutation scope must not bypass the structured review publisher | | `exploration_request_identity_guard.py` | A supported exploration event lacks bounded native identity or its one-shot record cannot be written | A Claude exploration call must not execute without request-correlated authority | | `join_stop_guard.py` | Malformed/non-object payload or missing `session_id` | Stop must not release when the guard cannot identify the binding whose wave completion authorizes release | -| `git_ops_guard.py` | Any unexpected runtime error (any `Exception`) while classifying a Git command, in the checked-out-ref preflight or the headless destructive-op phase; or, in the separate headless destructive-op-blocking preflight, an unrecognized global git flag that leaves the real subcommand unresolved, or a Python subprocess/os call whose argument could not be resolved to a literal argv or string while `live_command_text` still mentions "git" | An unhandled exception must not silently allow a checked-out ref mutation — use exit 2 + stderr to hard-block. `_git_command_classification._contains_blocked_git_op` reads the command exclusively through `all_evaluated_segments`, the evaluated-segment blocklist authority (rectify #4941 Part A): it cannot match `_BLOCKED_GIT_OPS`'s literal subcommand tuples against an unresolved subcommand, so it denies unconditionally the moment `extract_git_subcommand_and_flags` reports `""`, and separately denies when an unresolved interpreter payload cannot rule out a hidden `git` invocation, rather than silently falling through to "not blocked" | +| `git_ops_guard.py` | Any unexpected runtime error (any `Exception`) while classifying a Git command, in the checked-out-ref preflight or the headless destructive-op phase; or an unresolved target (unknown cwd, layout override or unparseable global option) for a ref mutation; or, in the separate headless destructive-op-blocking preflight, an unrecognized global git flag that leaves the real subcommand unresolved, or a Python subprocess/os call whose argument could not be resolved to a literal argv or string while `live_command_text` still mentions "git" | An unhandled exception must not silently allow a checked-out ref mutation — use exit 2 + stderr to hard-block. `_git_command_classification._contains_blocked_git_op` reads the command exclusively through `all_evaluated_segments`, the evaluated-segment blocklist authority (rectify #4941 Part A): it cannot match `_BLOCKED_GIT_OPS`'s literal subcommand tuples against an unresolved subcommand, so it denies unconditionally the moment `extract_git_subcommand_and_flags` reports `""`, and separately denies when an unresolved interpreter payload cannot rule out a hidden `git` invocation, rather than silently falling through to "not blocked" | | `pr_create_guard.py` | Hook config unreadable or malformed while the kitchen is open (OSError, JSONDecodeError, AttributeError, TypeError) | An unresolvable `recipe_allows_pr_create` authorization must not be read as permission to bypass the prepare_pr → compose_pr pipeline | | `unsafe_install_guard.py` | An unrecognized global pip flag leaves `pip`'s `install` token position unresolved | `_find_pip_install` cannot tell whether the command is a pip install at all; treating that the same as "definitely not an install" would silently skip the editable/system-install checks entirely, so it is threaded through as a distinct `"unresolved-pip-flags"` kind and denied unconditionally, matching the pre-existing `"unresolved-subprocess"` kind's treatment | | `write_guard.py` | A write-verb operand or redirect target cannot be resolved, or a Bash command cannot be parsed, while a write-prefix policy applies | An unresolved target could escape the allowed prefix; the denial explains how to substitute a literal path. | diff --git a/src/autoskillit/hooks/guards/git_ops_guard.py b/src/autoskillit/hooks/guards/git_ops_guard.py index 87ae71657e..9238d47939 100644 --- a/src/autoskillit/hooks/guards/git_ops_guard.py +++ b/src/autoskillit/hooks/guards/git_ops_guard.py @@ -14,7 +14,6 @@ import os import re import sys -from collections import deque from pathlib import Path from typing import Any @@ -30,28 +29,35 @@ if _GUARDS_DIR not in sys.path: sys.path.insert(0, _GUARDS_DIR) +from _classification import _option_grammar # noqa: E402 from _command_classification import ( # noqa: E402 - _GIT_GLOBAL_FLAG_SPEC, - StdinConsumer, WriteTargetScan, - _consume_str_flag, - _extract_interpreter_segment_specs, command_verb_and_args, - evaluated_payloads, + extract_interpreter_command_payloads, extract_interpreter_write_paths, live_command_text, scan_write_targets, - tokenize_command_segments, ) from _git_command_classification import ( # noqa: E402 - _classify_git_segment, + _classify_git_invocation, _contains_blocked_git_op, _git_result, _git_text, _parse_worktree_owners, _resolve_attempted_sha, - _resolve_git_common_dir, - extract_git_subcommand_and_flags, +) +from _git_option_tables import _SED_FLAG_SPEC # noqa: E402 +from _git_outcome import is_ref_mutation # noqa: E402 +from _git_target import ( # noqa: E402 + _GIT_BUILTIN_SUBCOMMANDS, + _GIT_MUTATION_SUBCOMMANDS, + ChannelFlags, + GitSegmentFacts, + GitTarget, + GitTargetKind, + analyze_git_segment, + command_channel_flags, + resolve_git_target_for_path, ) from _github_mutation_analysis import ( # noqa: E402 _DYNAMIC_SHELL_TOKEN_RE, @@ -74,6 +80,7 @@ read_merged_hook_config, ) from _path_identity import destination_location # noqa: E402 +from _shell_segment_walk import WalkedSegment, walk_evaluated_segments # noqa: E402 GIT_OPS_DENY_TRIGGER: str = DENY_TRIGGER_BY_GUARD["git_ops_guard"] CHECKED_OUT_REF_DENY_PREFIX: str = "Checked-out ref mutation blocked: " @@ -87,37 +94,58 @@ _DESTRUCTIVE_OP_EXEMPT_TIERS: frozenset[str] = frozenset({"orchestrator"}) _RAW_WRITE_VERBS = frozenset({"cp", "mv", "tee", "truncate", "rm", "unlink", "sed", "dd"}) - -# Git subcommands that can mutate checked-out refs (the same set -# _classify_git_segment recognises). Used by _deny_outer_git_mutations -# to escalate only when the unresolved-cwd segment is actually a -# mutation — read-only commands (log, status, diff, show, ...) must -# not be denied just because a preceding cd failed. -_GIT_MUTATION_SUBCOMMANDS: frozenset[str] = frozenset( - {"update-ref", "branch", "checkout", "switch", "reset", "fetch", "push", "symbolic-ref"} +_PACK_VERBS = frozenset({"git-receive-pack", "git-upload-pack"}) +_READ_ONLY_SUBCOMMANDS = _GIT_BUILTIN_SUBCOMMANDS - _GIT_MUTATION_SUBCOMMANDS + +# Commands that mention none of these cannot reach a Git ref or repository +# classification, so they skip the preflight entirely. This bounds the blast +# radius of the classifier; it is not a security boundary. +_GIT_RELEVANT_RE = re.compile( + r"(? dict[str, object] | None: - git_dir = _git_text(execution_cwd, "rev-parse", "--absolute-git-dir") - common_path = _resolve_git_common_dir(execution_cwd) - requesting = _git_text(execution_cwd, "rev-parse", "--show-toplevel") - if not git_dir or common_path is None or not requesting: - return None - common_git_dir = str(common_path) - worktrees = _git_result(execution_cwd, "worktree", "list", "--porcelain", "-z") - if worktrees.returncode != 0: - return None - try: - owners = _parse_worktree_owners(worktrees.stdout) - except UnicodeDecodeError: - return None +def _mutation_names(subcommands: frozenset[str]) -> str: + return "|".join(sorted(re.escape(name) for name in subcommands)) + + +def _mentions_git_mutation( + text: str, subcommands: frozenset[str] = _GIT_MUTATION_SUBCOMMANDS +) -> bool: + """Whether *text* names a Git command word followed by one of *subcommands*. + + Deliberately matches inside arguments too: an opaque launcher's payload + (``xargs sh -c '...git push...'``) cannot be told apart from quoted data + without per-launcher grammar. + """ + names = _mutation_names(subcommands) + pattern = rf"(? dict[str, object]: + key = ("owners", target.cwd) + if key not in cache: + worktrees = _git_result(target.cwd, "worktree", "list", "--porcelain", "-z") + if worktrees.returncode != 0: + raise RuntimeError(f"git worktree list failed in {target.cwd}") + cache[key] = _parse_worktree_owners(worktrees.stdout) return { - "common_git_dir": common_git_dir, - "execution_cwd": execution_cwd, - "owners": owners, - "requesting_worktree_path": requesting, - "worktree_git_dir": git_dir, + "common_git_dir": target.common_git_dir, + "execution_cwd": target.cwd, + "owners": cache[key], + "requesting_worktree_path": target.toplevel or target.cwd, + "worktree_git_dir": target.worktree_git_dir, } @@ -198,97 +226,35 @@ def _deny_checked_out_ref( deny_tool_use(reason) -def _raw_write_targets( - text: str, - initial_cwd: str, - outer_segments: list[list[str]], - additional_segments: list[list[str]], - additional_owners: list[int | None], - interpreter_cwd: str, - scan: WriteTargetScan, -) -> tuple[list[tuple[str, str]], list[tuple[str, str]], bool]: - targets = [(target, initial_cwd) for target in scan.targets] - entry_targets = [(target, initial_cwd) for target in scan.entry_targets] - ambiguous = scan.unresolved - outer_cwds = _compute_outer_cwds(outer_segments, initial_cwd) - segments_with_cwds = list(zip(outer_segments, outer_cwds)) - segments_with_cwds.extend( - ( - segment, - outer_cwds[owner] if owner is not None and 0 <= owner < len(outer_cwds) else "", - ) - for segment, owner in zip(additional_segments, additional_owners) +def _deny_unresolved(data: dict[str, object]) -> None: + _deny_checked_out_ref( + data=data, context=None, attempted_value="", threatened_refs=[] ) - for segment, segment_cwd in segments_with_cwds: - verb, args = command_verb_and_args(segment) - verb = os.path.basename(verb) - if verb not in _RAW_WRITE_VERBS: - continue - if verb == "dd": - candidates = [token.split("=", 1)[1] for token in args if token.startswith("of=")] - elif verb == "sed": - candidates = args[-1:] if any(token.startswith("-i") for token in args) else [] - else: - candidates = args[-1:] - for candidate in candidates: - if _DYNAMIC_SHELL_TOKEN_RE.search(candidate): - ambiguous = True - elif candidate: - targets.append((candidate, segment_cwd)) - interpreter_paths = extract_interpreter_write_paths(text) - if interpreter_paths == [] and "open(" in text: - ambiguous = True - elif interpreter_paths: - targets.extend((path, interpreter_cwd) for path in interpreter_paths) - return (targets, entry_targets, ambiguous) -def _raw_target_mutations( - text: str, - *, - initial_cwd: str, - outer_segments: list[list[str]], - additional_segments: list[list[str]], - additional_owners: list[int | None], - interpreter_cwd: str, - context: dict[str, object] | None, - scan: WriteTargetScan, -) -> list[tuple[str, str, bool]]: - targets, entry_targets, ambiguous = _raw_write_targets( - text, - initial_cwd, - outer_segments, - additional_segments, - additional_owners, - interpreter_cwd, - scan, - ) - if ambiguous: - return [("", "", True)] - resolved_targets: list[Path] = [] - for raw_targets, is_entry in ((targets, False), (entry_targets, True)): - for raw_target, target_cwd in raw_targets: - target = Path(raw_target) - if not target.is_absolute(): - if not target_cwd: - return [("", "", True)] - target = Path(target_cwd) / target - try: - resolved_targets.append( - destination_location(target) if is_entry else target.resolve() - ) - except (OSError, RuntimeError, ValueError): - return [("", "", True)] - if context is None: - return [] - common = Path(str(context["common_git_dir"])).resolve() - worktree_git = Path(str(context["worktree_git_dir"])).resolve() - result: list[tuple[str, str, bool]] = [] - for target in resolved_targets: - mutation = _classify_raw_write_target(target, common, worktree_git) - if mutation is not None: - result.append(mutation) - return result +def _deny_for_mutations( + data: dict[str, object], context: dict[str, object], mutations: list[tuple[str, str, bool]] +) -> None: + for target_ref, attempted_value, ambiguous in mutations: + threatened = ( + _all_threatened(context) if ambiguous else _threatened_for_target(context, target_ref) + ) + if threatened: + _deny_checked_out_ref( + data=data, + context=context, + attempted_value=attempted_value, + threatened_refs=threatened, + ) + + +def _deny_ambiguous(data: dict[str, object], execution_cwd: str, cache: dict[Any, Any]) -> None: + """Deny an unattributable mutation against every owned ref of the cwd's repository.""" + target = resolve_git_target_for_path(execution_cwd, cache) if execution_cwd else None + if target is not None and target.kind == GitTargetKind.RESOLVED: + _deny_for_mutations(data, _context_for(target, cache), [("", "", True)]) + return + _deny_unresolved(data) def _classify_raw_write_target( @@ -316,242 +282,184 @@ def _relative_to(root: Path) -> Path | None: return (f"refs/heads/{relative.as_posix()}", "", False) -def _git_segment_cwd(segment: list[str], cwd: str) -> str: - verb, args = command_verb_and_args(segment) - if verb != "git" and not verb.endswith("/git"): - return cwd - current = Path(cwd) - index = 0 - while index < len(args): - token = args[index] - if token == "-C" and index + 1 < len(args): - candidate = Path(args[index + 1]) - current = candidate if candidate.is_absolute() else current / candidate - index += 2 - continue - if not token.startswith("-"): - break - # -C's own value is captured above (needed to build `current`); every - # other global flag is just skipped past, same as before -- an - # unrecognized flag advances by one token rather than stopping, this - # is cwd-tracking, not the security-critical subcommand parse. - _, next_index, recognized = _consume_str_flag(args, index, _GIT_GLOBAL_FLAG_SPEC) - index = next_index if recognized else index + 1 - return str(current.resolve()) - - -def _preflight_segments( - command: str, -) -> tuple[list[list[str]], list[list[str]], list[int | None], bool]: - outer_segments = tokenize_command_segments(command) - additional_segments: list[list[str]] = [] - additional_owners: list[int | None] = [] - interpreter_unresolved = False - payloads = evaluated_payloads(command) - shell_queue = deque( - (payload.text, payload.consumer_index) - for payload in payloads - if payload.kind == StdinConsumer.SHELL - ) - seen_shell: set[tuple[str, int | None]] = set() - while shell_queue: - text, owner = shell_queue.popleft() - if (text, owner) in seen_shell: - continue - seen_shell.add((text, owner)) - segments = tokenize_command_segments(text) - additional_segments.extend(segments) - additional_owners.extend([owner] * len(segments)) - shell_queue.extend( - (nested.text, owner) - for nested in evaluated_payloads(text) - if nested.kind == StdinConsumer.SHELL - ) - for payload in payloads: - if payload.kind == StdinConsumer.PYTHON: - specs, unresolved = _extract_interpreter_segment_specs(["python3", "-c", payload.text]) - interpreter_unresolved = interpreter_unresolved or unresolved - segments = [] - for spec in specs: - if isinstance(spec.payload, list): - segments.append(spec.payload) - else: - segments.extend(tokenize_command_segments(spec.payload)) - additional_segments.extend(segments) - additional_owners.extend([payload.consumer_index] * len(segments)) - return outer_segments, additional_segments, additional_owners, interpreter_unresolved - - -def _is_structural_mutation( - live_text: str, outer: list[list[str]], additional: list[list[str]], *, has_write: bool -) -> bool: - return bool( - re.search( - r"\bgit\b[^\n;&|]*(?:update-ref|branch\s+(?:-f|--force)|checkout\s+-B|switch\s+-C|" - r"reset\b|fetch\b|push\b|symbolic-ref\s+HEAD)\b", - live_text, - ) - or has_write - or any( - os.path.basename(command_verb_and_args(segment)[0]) in _RAW_WRITE_VERBS | {"install"} - for segment in [*outer, *additional] - ) - ) +def _verb_write_candidates(verb: str, args: list[str]) -> list[str]: + if verb == "dd": + return [token.split("=", 1)[1] for token in args if token.startswith("of=")] + if verb == "sed": + parsed = _option_grammar.parse_options(args, _SED_FLAG_SPEC) + in_place = _option_grammar.last_option(parsed, "in-place") is not None + return args[-1:] if parsed.unresolved or in_place else [] + return args[-1:] -def _advance_shell_cwd(current_cwd: str, verb: str, args: list[str]) -> tuple[str, bool]: - if verb == "cd": - if any(arg == "-" for arg in args): - return "", True - target_args = [arg for arg in args if arg not in ("-P", "--")] - if len(target_args) == 1 and not _DYNAMIC_SHELL_TOKEN_RE.search(target_args[0]): - candidate = Path(target_args[0]) - if not current_cwd and not candidate.is_absolute(): - return "", True - return ( - str( - ( - candidate if candidate.is_absolute() else Path(current_cwd) / candidate - ).resolve() - ), - True, - ) - if len(args) != 1: - return "", True - elif verb == "pushd" and len(args) == 1 and not _DYNAMIC_SHELL_TOKEN_RE.search(args[0]): - candidate = Path(args[0]) - if not current_cwd and not candidate.is_absolute(): - return "", True - return ( - str( - (candidate if candidate.is_absolute() else Path(current_cwd) / candidate).resolve() - ), - True, - ) - return current_cwd, False +def _single_python_cwd(walk: list[WalkedSegment]) -> str: + cwds = [ + seg.verb_cwd if seg.cwd_certain else "" + for seg in walk + if seg.start is not None + and _PYTHON_VERB_RE.fullmatch(os.path.basename(seg.executable[seg.start])) + ] + return cwds[0] if len(cwds) == 1 else "" -def _compute_outer_cwds(outer_segments: list[list[str]], initial_cwd: str) -> list[str]: - """Return the cwd in effect during each outer segment, walking cd/pushd.""" - cwds: list[str] = [] - current_cwd = initial_cwd - for segment in outer_segments: - verb, args = command_verb_and_args(segment) - current_cwd, _ = _advance_shell_cwd(current_cwd, verb, args) - cwds.append(current_cwd) - return cwds +def _raw_write_candidates( + text: str, walk: list[WalkedSegment], scan: WriteTargetScan, execution_cwd: str +) -> tuple[list[tuple[str, str, bool]], bool]: + """Raw write targets as ``(path, cwd, is_entry)``, and whether any is unresolvable.""" + targets = [(target, execution_cwd, False) for target in scan.targets] + targets.extend((target, execution_cwd, True) for target in scan.entry_targets) + ambiguous = scan.unresolved + for seg in walk: + if seg.start is None or seg.handled_builtin: + continue + verb, args = command_verb_and_args(seg.executable) + verb = os.path.basename(verb) + if verb not in _RAW_WRITE_VERBS: + continue + for candidate in _verb_write_candidates(verb, args): + if _DYNAMIC_SHELL_TOKEN_RE.search(candidate): + ambiguous = True + elif candidate: + targets.append((candidate, seg.verb_cwd if seg.cwd_certain else "", False)) + # None: not an interpreter write; []: a write whose path is not a literal. + interpreter_paths = extract_interpreter_write_paths(text) or [] + if not interpreter_paths and _INTERPRETER_WRITE_RE.search(text): + ambiguous = True + interpreter_cwd = _single_python_cwd(walk) + targets.extend((path, interpreter_cwd, False) for path in interpreter_paths) + return targets, ambiguous + + +def _resolve_raw_targets( + targets: list[tuple[str, str, bool]], +) -> tuple[list[Path], bool]: + resolved: list[Path] = [] + ambiguous = False + for raw_target, cwd, is_entry in targets: + path = Path(raw_target) + if not path.is_absolute(): + if not cwd: + ambiguous = True + continue + path = Path(cwd) / path + try: + resolved.append(destination_location(path) if is_entry else path.resolve()) + except (OSError, RuntimeError, ValueError): + ambiguous = True + return resolved, ambiguous -def _deny_for_mutations( - data: dict[str, object], context: dict[str, object], mutations: list[tuple[str, str, bool]] +def _preflight_raw_writes( + data: dict[str, object], + text: str, + walk: list[WalkedSegment], + scan: WriteTargetScan, + execution_cwd: str, + cache: dict[Any, Any], ) -> None: - for target_ref, attempted_value, ambiguous in mutations: - threatened = ( - _all_threatened(context) if ambiguous else _threatened_for_target(context, target_ref) + """Judge every raw write target against its own repository. + + An unresolvable target is ambiguous only when the command names ref + storage, so ordinary writes through a variable are not denied. + """ + candidates, ambiguous = _raw_write_candidates(text, walk, scan, execution_cwd) + resolved, unresolvable = _resolve_raw_targets(candidates) + names_ref_storage = _REF_STORAGE_RE.search(text) is not None + if (ambiguous or unresolvable) and names_ref_storage: + _deny_ambiguous(data, execution_cwd, cache) + for path in resolved: + target = resolve_git_target_for_path(str(path), cache) + if target.kind == GitTargetKind.UNRESOLVED and names_ref_storage: + _deny_unresolved(data) + if target.kind != GitTargetKind.RESOLVED: + continue + mutation = _classify_raw_write_target( + path, Path(target.common_git_dir), Path(target.worktree_git_dir) ) - if threatened: - _deny_checked_out_ref( - data=data, - context=context, - attempted_value=attempted_value, - threatened_refs=threatened, - ) + if mutation is not None: + _deny_for_mutations(data, _context_for(target, cache), [mutation]) -def _deny_outer_git_mutations(data: dict[str, object], segment: list[str], cwd: str) -> None: - verb = os.path.basename(command_verb_and_args(segment)[0]) - if verb not in {"git", "git-receive-pack", "git-upload-pack"}: +def _decide_shared_ref( + data: dict[str, object], facts: GitSegmentFacts, cache: dict[Any, Any], cwd: str +) -> None: + """Deny what *facts* would do to a checked-out ref; *cwd* is the proven cwd or empty.""" + target = facts.target + invocation = facts.invocation + if facts.identity_unknown: + if target.kind == GitTargetKind.UNRESOLVED: + _deny_unresolved(data) + elif target.kind == GitTargetKind.RESOLVED: + _deny_for_mutations(data, _context_for(target, cache), [("", "", True)]) return - if not cwd: - # A preceding cwd change could not be resolved; this segment cannot - # be routed to a repository with confidence. Escalate to deny only - # when the subcommand is a known ref-mutating one — read-only - # commands (log, status, diff, show, ...) must not be denied just - # because a preceding cd failed. An unparseable subcommand - # (parsed is None or "") also escalates, matching the - # `_classify_git_segment` fail-closed posture. - parsed = extract_git_subcommand_and_flags(segment) - subcommand = parsed.subcommand if parsed is not None else None - if subcommand is None or subcommand == "": - subcommand = None # ambiguous — escalate below - if subcommand is None or subcommand in _GIT_MUTATION_SUBCOMMANDS: - _deny_checked_out_ref( - data=data, - context=None, - attempted_value="", - threatened_refs=[], - ) + if target.kind == GitTargetKind.UNRESOLVED: + if target.reason == "unparsed_global_options": + _deny_ambiguous(data, cwd, cache) + elif is_ref_mutation(invocation.subcommand, invocation.flags): + _deny_unresolved(data) + return + if target.kind == GitTargetKind.RESOLVED: + context = _context_for(target, cache) + _deny_for_mutations(data, context, _classify_git_invocation(invocation, context)) + + +def _preflight_segment( + data: dict[str, object], seg: WalkedSegment, flags: ChannelFlags, cache: dict[Any, Any] +) -> None: + if seg.position_unresolved and _mentions_git_mutation(" ".join(seg.executable)): + _deny_unresolved(data) + verb = os.path.basename(seg.executable[seg.start]) if seg.start is not None else "" + if verb in _PACK_VERBS: + if not seg.cwd_certain or not seg.verb_cwd: + _deny_unresolved(data) + return + facts = analyze_git_segment(seg, flags, cache) + if facts is None: return - context = _repository_context(_git_segment_cwd(segment, cwd)) - if context is not None: - _deny_for_mutations(data, context, _classify_git_segment(segment, context)) + if facts.invocation.subcommand in _READ_ONLY_SUBCOMMANDS and not facts.identity_unknown: + return + _decide_shared_ref(data, facts, cache, seg.verb_cwd if seg.cwd_certain else "") def _preflight_checked_out_ref_mutation( - data: dict[str, object], command: str, execution_cwd: str + data: dict[str, object], command: str, execution_cwd: str, cache: dict[Any, Any] ) -> None: - outer_segments, additional_segments, additional_owners, interpreter_unresolved = ( - _preflight_segments(command) - ) # The live-text projection (rectify #4941 Part A): heredoc and herestring # bodies are blanked at their source spans and appended once when their - # consumer executes them. Inert bodies are never appended. - # Both the structural-mutation regex and - # _raw_target_mutations' write-path scan read this projection instead of - # the raw command, so an inert `cat <<'EOF'` body mentioning - # "git push --force" as prose no longer matches, while a heredoc/pipe-fed - # shell that actually runs it still does. + # consumer executes them, so an inert `cat <<'EOF'` body mentioning + # "git push --force" as prose does not match, while a heredoc/pipe-fed + # shell that actually runs it does. live_text = live_command_text(command) - scan = scan_write_targets(live_text, execution_cwd) - structural_mutation = _is_structural_mutation( - live_text, outer_segments, additional_segments, has_write=scan.has_write - ) - if not execution_cwd: - if structural_mutation: - _deny_checked_out_ref( - data=data, - context=None, - attempted_value="", - threatened_refs=[], - ) + walk = walk_evaluated_segments(command, execution_cwd) + if walk is None: + if _mentions_git_mutation(live_text): + _deny_unresolved(data) return - initial_context = _repository_context(execution_cwd) - current_cwd = execution_cwd - outer_cwds = _compute_outer_cwds(outer_segments, current_cwd) - for segment, segment_cwd in zip(outer_segments, outer_cwds): - _deny_outer_git_mutations(data, segment, segment_cwd) - current_cwd = outer_cwds[-1] if outer_cwds else execution_cwd - - context = _repository_context(current_cwd) if current_cwd else None - mutations: list[tuple[str, str, bool]] = [] - if context is not None: - for segment in additional_segments: - mutations.extend(_classify_git_segment(segment, context)) - raw_context = context or initial_context - mutations.extend( - _raw_target_mutations( - live_text, - initial_cwd=execution_cwd, - outer_segments=outer_segments, - additional_segments=additional_segments, - additional_owners=additional_owners, - interpreter_cwd=current_cwd, - context=raw_context, - scan=scan, + flags = command_channel_flags(live_text) + for seg in walk: + _preflight_segment(data, seg, flags, cache) + scan = scan_write_targets(live_text, execution_cwd) + _preflight_raw_writes(data, live_text, walk, scan, execution_cwd, cache) + if extract_interpreter_command_payloads(command)[1] and _mentions_git_mutation(live_text): + _deny_unresolved(data) + + +def _git_relevant(probe: str) -> bool: + """Whether *probe* (quotes and backslashes removed) can reach a Git classification.""" + return bool( + _GIT_RELEVANT_RE.search(probe) + or ( + ("$" in probe or "`" in probe) + and re.search(rf"\b(?:{_mutation_names(_GIT_MUTATION_SUBCOMMANDS)})\b", probe) ) ) - if interpreter_unresolved and structural_mutation: - mutations.append(("", "", True)) - if raw_context is None: - if any(ambiguous for _, _, ambiguous in mutations): - _deny_checked_out_ref( - data=data, - context=None, - attempted_value="", - threatened_refs=[], - ) - return - _deny_for_mutations(data, raw_context, mutations) + + +def _kitchen_marker_exists(project_root: Path) -> bool: + try: + return (project_root / ".autoskillit" / "temp" / ".hook_config.json").exists() + except OSError: + return False def _parse_hook_event() -> tuple[dict[str, object], Any] | None: @@ -585,19 +493,18 @@ def main() -> None: if not cmd: sys.exit(0) + if not _git_relevant(re.sub(r"['\"\\]", "", cmd)): + sys.exit(0) + # Any unexpected classification error denies. SystemExit raised by the # deny helpers is BaseException, not Exception, so it bypasses this # handler and serves as the explicit verdict signal. try: project_root = resolve_state_root(parsed.payload_cwd) - try: - cfg_path = project_root / ".autoskillit" / "temp" / ".hook_config.json" - kitchen_open = cfg_path.exists() - except OSError: - sys.exit(0) + kitchen_open = _kitchen_marker_exists(project_root) if kitchen_open: - _preflight_checked_out_ref_mutation(data, cmd, parsed.execution_cwd) + _preflight_checked_out_ref_mutation(data, cmd, parsed.execution_cwd, {}) headless, tier = hook_session_shape() if not headless: diff --git a/tests/_ambient_env_surface.py b/tests/_ambient_env_surface.py index 788edbec6c..f2f26d3324 100644 --- a/tests/_ambient_env_surface.py +++ b/tests/_ambient_env_surface.py @@ -2545,6 +2545,33 @@ class AmbientEnvDisposition: "harness itself and scrubbed to avoid inadvertent live API behavior in tests." ), ), + "GIT_COMMON_DIR": AmbientEnvDisposition( + var="GIT_COMMON_DIR", + disposition="scrub", + owner="harness", + justification=( + "Git repository-layout override read by git_ops_guard from the hook's own environment;" + "scrubbed so an ambient value cannot redirect a test's Git queries." + ), + ), + "GIT_DIR": AmbientEnvDisposition( + var="GIT_DIR", + disposition="scrub", + owner="harness", + justification=( + "Git repository-layout override read by git_ops_guard from the hook's own environment;" + "scrubbed so an ambient value cannot redirect a test's Git queries." + ), + ), + "GIT_WORK_TREE": AmbientEnvDisposition( + var="GIT_WORK_TREE", + disposition="scrub", + owner="harness", + justification=( + "Git repository-layout override read by git_ops_guard from the hook's own environment;" + "scrubbed so an ambient value cannot redirect a test's Git queries." + ), + ), "H": AmbientEnvDisposition( var="H", disposition="scrub", diff --git a/tests/arch/test_git_ref_qualification.py b/tests/arch/test_git_ref_qualification.py index 3dd0623ad1..ecf27baf0b 100644 --- a/tests/arch/test_git_ref_qualification.py +++ b/tests/arch/test_git_ref_qualification.py @@ -116,6 +116,7 @@ "", "UNRESOLVABLE", ), + ("hooks/_runtime/_git_target.py", "expand_git_alias", "", "UNRESOLVABLE"), ("hooks/guards/remove_clone_guard.py", "_git", "", "UNRESOLVABLE"), ("recipe/cmd_rpc/_cmd_rpc_guards.py", "_check_regression", "", "UNRESOLVABLE"), ("recipe/cmd_rpc/_cmd_rpc_guards.py", "_check_regression", "", "UNRESOLVABLE"), diff --git a/tests/arch/test_hook_path_identity_guard.py b/tests/arch/test_hook_path_identity_guard.py index df6c6bf941..2e05f8db0d 100644 --- a/tests/arch/test_hook_path_identity_guard.py +++ b/tests/arch/test_hook_path_identity_guard.py @@ -31,9 +31,9 @@ _ALLOWLISTED_SITES: dict[SiteKey, str] = { ("guards/git_ops_guard.py", "_relative_to"): ( - "_raw_target_mutations canonicalizes content targets with .resolve(), entry " - "targets with destination_location (resolved parent plus literal basename), " - "and common/worktree_git roots with .resolve() before classification." + "_resolve_raw_targets canonicalizes content targets with .resolve() and entry " + "targets with destination_location (resolved parent plus literal basename); the " + "common/worktree_git roots come from canonical_existing_path before classification." ), ("formatters/_fmt_response_spill.py", "_response_spill_artifact_is_trusted"): ( "resolved comes from artifact.resolve(strict=True) inline and project_temp from " diff --git a/tests/arch/test_hook_raw_command_scan_inventory.py b/tests/arch/test_hook_raw_command_scan_inventory.py index 99a1e8a416..afa22afe4f 100644 --- a/tests/arch/test_hook_raw_command_scan_inventory.py +++ b/tests/arch/test_hook_raw_command_scan_inventory.py @@ -190,7 +190,9 @@ def _scoped_source_files() -> list[Path]: files += [ HOOKS_ROOT / "_runtime" / "_command_classification.py", HOOKS_ROOT / "_runtime" / "_git_command_classification.py", + HOOKS_ROOT / "_runtime" / "_git_target.py", HOOKS_ROOT / "_runtime" / "_github_mutation_analysis.py", + HOOKS_ROOT / "_runtime" / "_shell_segment_walk.py", ] return sorted(set(files)) @@ -250,14 +252,6 @@ def _observed_raw_scans() -> tuple[tuple[str, str, str], ...]: "interpreter_invokes", "tokenize_command_segments", ), - # git_ops_guard.py tokenizes outer and nested payload segments for its - # per-segment cd/git classification. evaluated_payloads supplies each - # nested payload's consumer index so its owner is preserved. - ( - "hooks/guards/git_ops_guard.py", - "_preflight_segments", - "tokenize_command_segments", - ), # compose_pr_body_guard.py -- per-evaluated-payload segmentation # (Part B 2.10): `all_evaluated_segments` flattens every payload's # segments into one list, losing which segments belong to which diff --git a/tests/arch/test_regex_guards.py b/tests/arch/test_regex_guards.py index d74f7204a2..fea84cb5c6 100644 --- a/tests/arch/test_regex_guards.py +++ b/tests/arch/test_regex_guards.py @@ -232,18 +232,17 @@ def test_command_classifying_guards_use_shared_primitive(): def test_git_ops_guard_orchestrator_keeps_command_classification_import() -> None: - """Step 9 (#4733) regression: after decomposing the git-command - classification primitives into _git_command_classification, the - orchestrator (git_ops_guard.py) MUST continue to use the flag-spec - engine (_consume_str_flag, _GIT_GLOBAL_FLAG_SPEC), command tokenizer, - evaluated payloads with consumer provenance, and write-target scan - (tokenize_command_segments, command_verb_and_args, evaluated_payloads, - StdinConsumer, extract_interpreter_write_paths, scan_write_targets) from - _command_classification - the same shared primitives that + """git_ops_guard.py consumes the shared command-classification primitives directly. + + The orchestrator reads commands through the evaluated-segment authority + (live_command_text, extract_interpreter_command_payloads), the verb parser + (command_verb_and_args) and the write-target scan + (extract_interpreter_write_paths, scan_write_targets) from + _command_classification -- the same shared primitives that test_command_classifying_guards_use_shared_primitive enforces for - write_guard.py, pr_create_guard.py, etc. The new sibling must NOT - re-export these symbols to consumers; it receives them as bare-name - imports internally and the orchestrator consumes them directly. + write_guard.py, pr_create_guard.py, etc. _git_command_classification must + not re-export them: it receives them as bare-name imports internally and + the orchestrator consumes them directly. """ src = (SRC_ROOT / "hooks" / "guards" / "git_ops_guard.py").read_text() tree = ast.parse(src) @@ -257,18 +256,11 @@ def test_git_ops_guard_orchestrator_keeps_command_classification_import() -> Non elif node.module == "_git_command_classification": imports_from_git_command_classification.extend(a.name for a in node.names) - # The orchestrator must keep using the flag-spec engine and shared parsing - # from _command_classification. The minimal set is the engine pair - # (_consume_str_flag, _GIT_GLOBAL_FLAG_SPEC) plus the primitives - # actually consumed by the orchestrator's handlers. required_command_classification = { - "_consume_str_flag", - "_GIT_GLOBAL_FLAG_SPEC", - "tokenize_command_segments", "command_verb_and_args", - "evaluated_payloads", - "StdinConsumer", + "extract_interpreter_command_payloads", "extract_interpreter_write_paths", + "live_command_text", "scan_write_targets", } missing = required_command_classification - set(imports_from_command_classification) @@ -285,14 +277,11 @@ def test_git_ops_guard_orchestrator_keeps_command_classification_import() -> Non # the orchestrator itself still consumes directly (orchestrator's import # would be shadowed by the sibling's re-export). forbidden = { - "_GIT_GLOBAL_FLAG_SPEC", - "_consume_str_flag", "command_verb_and_args", - "evaluated_payloads", - "StdinConsumer", + "extract_interpreter_command_payloads", "extract_interpreter_write_paths", + "live_command_text", "scan_write_targets", - "tokenize_command_segments", } leaked = forbidden & set(imports_from_git_command_classification) assert not leaked, ( diff --git a/tests/arch/test_subpackage_isolation_facades.py b/tests/arch/test_subpackage_isolation_facades.py index 5ed98866ea..0122c3c872 100644 --- a/tests/arch/test_subpackage_isolation_facades.py +++ b/tests/arch/test_subpackage_isolation_facades.py @@ -276,8 +276,10 @@ def test_hooks_runtime_decomposition_has_expected_siblings() -> None: "_git_command_classification", "_git_option_tables", "_git_outcome", + "_git_target", "_path_identity", "_session_registry_bridge", + "_shell_segment_walk", "_write_target_scan", } diff --git a/tests/hooks/test_command_classification.py b/tests/hooks/test_command_classification.py index 143886b5fb..2e6fdadc3c 100644 --- a/tests/hooks/test_command_classification.py +++ b/tests/hooks/test_command_classification.py @@ -4428,6 +4428,31 @@ def test_every_git_global_spec_flag_is_recognized(flag: str) -> None: assert result.subcommand != "" +@pytest.mark.parametrize( + ("argv", "subcommand"), + [ + (["git", "--no-optional-locks", "status"], "status"), + (["git", "--literal-pathspecs", "log"], "log"), + (["git", "--no-literal-pathspecs", "log"], "log"), + (["git", "--attr-source=HEAD", "log"], "log"), + (["git", "--attr-source", "HEAD", "log"], "log"), + ], +) +def test_git_pathspec_lock_and_attribute_globals_leave_the_real_subcommand( + argv: list[str], subcommand: str +) -> None: + result = extract_git_subcommand_and_flags(argv) + + assert result is not None + assert result.subcommand == subcommand + assert result.flags == [] + + +@pytest.mark.parametrize("argv", [["git", "--list-cmds=main"], ["git", "--exec-path"]]) +def test_git_query_only_invocation_has_no_subcommand(argv: list[str]) -> None: + assert extract_git_subcommand_and_flags(argv) is None + + class TestStructuralHeredocShapes: """Regressions for structural heredoc delimiter and body handling.""" diff --git a/tests/hooks/test_git_effect_differential.py b/tests/hooks/test_git_effect_differential.py index 85ce27cae2..c80411359f 100644 --- a/tests/hooks/test_git_effect_differential.py +++ b/tests/hooks/test_git_effect_differential.py @@ -19,7 +19,7 @@ from collections.abc import Callable, Iterator, Mapping from dataclasses import dataclass from pathlib import Path -from typing import NamedTuple +from typing import Any, NamedTuple import pytest @@ -30,12 +30,22 @@ OptionArity, OptionDef, ) +from _command_classification import ( # type: ignore[import-not-found] + extract_git_subcommand_and_flags, +) +from _git_command_classification import ( # type: ignore[import-not-found] + _classify_git_invocation, +) from _git_option_tables import _GIT_SUBCOMMAND_TABLES # type: ignore[import-not-found] from _git_outcome import ( # type: ignore[import-not-found] GitHeadEffect, GitRefOutcome, git_ref_outcome, ) +from _git_target import ( # type: ignore[import-not-found] + GitTargetKind, + resolve_git_target_for_path, +) from hypothesis import event, example, given, settings from hypothesis import strategies as st @@ -299,11 +309,15 @@ def _run_differential(sandbox: _Sandbox, subcommand: str, case_argv: tuple[str, argv = tuple( token.replace(_SHA_A, sandbox.sha_a).replace(_SHA_B, sandbox.sha_b) for token in case_argv ) - context = git_ops_guard._repository_context(str(sandbox.primary)) - assert context is not None, f"no repository context for {sandbox.primary}" + cache: dict[Any, Any] = {} + target = resolve_git_target_for_path(str(sandbox.primary), cache) + assert target.kind == GitTargetKind.RESOLVED, f"no repository for {sandbox.primary}" + context = git_ops_guard._context_for(target, cache) owners = context["owners"] assert isinstance(owners, dict) - tuples = tuple(git_ops_guard._classify_git_segment(["git", subcommand, *argv], context)) + invocation = extract_git_subcommand_and_flags(["git", subcommand, *argv]) + assert invocation is not None, f"no git subcommand in {(subcommand, *argv)}" + tuples = tuple(_classify_git_invocation(invocation, context)) before = _observe(sandbox) completed = _git(sandbox.primary, subcommand, *argv, check=False) after = _observe(sandbox) diff --git a/tests/hooks/test_git_target.py b/tests/hooks/test_git_target.py new file mode 100644 index 0000000000..f0c39a59f1 --- /dev/null +++ b/tests/hooks/test_git_target.py @@ -0,0 +1,583 @@ +"""Target and identity analysis of walked Git segments (hooks/_runtime/_git_target.py). + +Members: checkout, linked-worktree, ``.git`` and bare-repository targets, the +non-repository target, unproven-cwd and ``-C`` rows, layout-override and +unparsed-global-option rows, dynamic command and subcommand identity, the alias +channel, alias expansion, ``git-`` and external commands, +``resolve_git_target_for_path`` and the builtin/mutation subcommand sets. + +Every row analyses a whole segment produced by ``walk_evaluated_segments``, in +real repositories under ``tmp_path_factory`` with Git configuration isolated +from the host. No row runs the analysed command; the analysis only queries +``rev-parse`` and ``config --get``. +""" + +from __future__ import annotations + +import functools +import os +import shutil +import subprocess +from dataclasses import dataclass +from pathlib import Path +from typing import Any + +# The sys.path bootstrap for bare-name hook-sibling imports is centralized in +# tests/conftest.py. +import _git_target # type: ignore[import-not-found] +import pytest +from _command_classification import live_command_text # type: ignore[import-not-found] +from _git_outcome import GitHeadEffect # type: ignore[import-not-found] +from _git_target import ( # type: ignore[import-not-found] + _GIT_BUILTIN_SUBCOMMANDS, + _GIT_MUTATION_SUBCOMMANDS, + GitSegmentFacts, + GitTarget, + GitTargetKind, + analyze_git_segment, + command_channel_flags, + resolve_git_target_for_path, +) +from _shell_segment_walk import walk_evaluated_segments # type: ignore[import-not-found] + +from tests._git_topology import add_linked_worktree, commit_file, init_checkout + +pytestmark = [ + pytest.mark.layer("hooks"), + pytest.mark.medium, + pytest.mark.skipif(shutil.which("git") is None, reason="git binary required"), +] + +_GIT_TIMEOUT_SECONDS = 10 +_INHERITED_REPOSITORY_ENV = ( + "GIT_DIR", + "GIT_WORK_TREE", + "GIT_COMMON_DIR", + "GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", +) +_REPOSITORY_ALIASES = { + "co": "checkout", + "nb": "checkout -b", + "ext": "lfs", + "sh": "!git checkout -b x", +} + + +@functools.cache +def _git_version() -> str: + result = subprocess.run( + ["git", "--version"], + capture_output=True, + text=True, + timeout=_GIT_TIMEOUT_SECONDS, + check=False, + ) + return result.stdout.strip() or "" + + +def _git(cwd: Path, *args: str) -> None: + subprocess.run( + ["git", *args], + cwd=cwd, + check=True, + capture_output=True, + text=True, + timeout=_GIT_TIMEOUT_SECONDS, + ) + + +def _isolate_git_environment(mp: pytest.MonkeyPatch, home: Path, ceiling: Path) -> None: + """Point Git's user, system and discovery state into the test tree.""" + home.mkdir(parents=True, exist_ok=True) + mp.setenv("HOME", str(home)) + mp.setenv("XDG_CONFIG_HOME", str(home / ".config")) + mp.setenv("GIT_CONFIG_GLOBAL", os.devnull) + mp.setenv("GIT_CONFIG_NOSYSTEM", "1") + mp.setenv("GIT_CEILING_DIRECTORIES", str(ceiling)) + for name in _INHERITED_REPOSITORY_ENV: + mp.delenv(name, raising=False) + + +@dataclass(frozen=True) +class Topology: + """Canonical paths of the repositories and directories the rows analyse.""" + + primary: Path + subdir: Path + primary_link: Path + linked: Path + dot_git: Path + bare: Path + plain: Path + symlink_parent: Path + + +@pytest.fixture(autouse=True) +def _isolated_git_environment( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path, tmp_path_factory: pytest.TempPathFactory +) -> None: + _isolate_git_environment(monkeypatch, tmp_path / "home", tmp_path_factory.getbasetemp()) + + +@pytest.fixture(scope="module") +def topology(tmp_path_factory: pytest.TempPathFactory) -> Topology: + root = tmp_path_factory.mktemp("git-target").resolve() + with pytest.MonkeyPatch.context() as mp: + _isolate_git_environment(mp, root / "build-home", tmp_path_factory.getbasetemp()) + primary = init_checkout(root / "primary") + commit_file(primary, "sub/tracked.txt", "tracked\n", "add tracked file") + _git(primary, "branch", "develop") + for name, value in _REPOSITORY_ALIASES.items(): + _git(primary, "config", f"alias.{name}", value) + linked = add_linked_worktree(primary, "review") + bare = root / "bare.git" + _git(root, "init", "--bare", "-q", str(bare)) + plain = root / "plain" + plain.mkdir() + primary_link = root / "primary-link" + primary_link.symlink_to(primary) + symlink_parent = root / "w" + symlink_parent.mkdir() + (symlink_parent / "lnk").symlink_to(primary / "sub") + return Topology( + primary=primary, + subdir=primary / "sub", + primary_link=primary_link, + linked=linked, + dot_git=primary / ".git", + bare=bare, + plain=plain, + symlink_parent=symlink_parent, + ) + + +def _context(command: str, result: object) -> str: + return f"{command!r} -> {result} ({_git_version()})" + + +def _classified(command: str, cwd: str | Path) -> list[GitSegmentFacts]: + walked = walk_evaluated_segments(command, str(cwd)) + assert walked is not None, f"{command!r} could not be tokenized ({_git_version()})" + flags = command_channel_flags(live_command_text(command)) + cache: dict[Any, Any] = {} + analysed = [analyze_git_segment(segment, flags, cache) for segment in walked] + return [facts for facts in analysed if facts is not None] + + +def _only(command: str, cwd: str | Path) -> GitSegmentFacts: + classified = _classified(command, cwd) + assert len(classified) == 1, ( + f"{command!r} classified {len(classified)} Git segments ({_git_version()})" + ) + return classified[0] + + +def _assert_resolved( + command: str, + target: GitTarget, + *, + cwd: Path, + git_dir: Path, + common_dir: Path, + is_main: bool, + toplevel: Path | None, +) -> None: + context = _context(command, target) + assert target.kind == GitTargetKind.RESOLVED, context + assert target.reason == "", context + assert target.cwd == str(cwd.resolve()), context + assert target.worktree_git_dir == str(git_dir.resolve()), context + assert target.common_git_dir == str(common_dir.resolve()), context + assert target.is_main_worktree is is_main, context + assert target.toplevel == (None if toplevel is None else str(toplevel.resolve())), context + + +def _assert_main_checkout(command: str, target: GitTarget, topology: Topology, cwd: Path) -> None: + _assert_resolved( + command, + target, + cwd=cwd, + git_dir=topology.dot_git, + common_dir=topology.dot_git, + is_main=True, + toplevel=topology.primary, + ) + + +def _assert_unresolved(command: str, target: GitTarget, reason: str) -> None: + context = _context(command, target) + assert target.kind == GitTargetKind.UNRESOLVED, context + assert target.reason == reason, context + + +def _assert_unknown_identity(command: str, facts: GitSegmentFacts) -> None: + context = _context(command, facts) + assert facts.identity_unknown is True, context + assert facts.outcome is not None, context + assert facts.outcome.ref_ambiguous is True, context + assert facts.outcome.head_effect == GitHeadEffect.AMBIGUOUS, context + + +@pytest.mark.parametrize("start", ["primary", "subdir", "primary_link"]) +def test_primary_checkout_directories_resolve_to_the_canonical_main_worktree( + topology: Topology, start: str +) -> None: + command = "git checkout -b x" + cwd: Path = getattr(topology, start) + + facts = _only(command, cwd) + + _assert_main_checkout(command, facts.target, topology, cwd) + assert facts.identity_unknown is False, _context(command, facts) + + +@pytest.mark.parametrize( + ("command_template", "start"), + [("git checkout -b x", "linked"), ("cd {linked} && git checkout -b x", "primary")], + ids=["linked-cwd", "certain-cd"], +) +def test_linked_worktree_resolves_to_a_non_main_worktree_of_the_same_repository( + topology: Topology, command_template: str, start: str +) -> None: + command = command_template.format(linked=topology.linked) + + facts = _only(command, getattr(topology, start)) + + _assert_resolved( + command, + facts.target, + cwd=topology.linked, + git_dir=topology.dot_git / "worktrees" / topology.linked.name, + common_dir=topology.dot_git, + is_main=False, + toplevel=topology.linked, + ) + + +@pytest.mark.parametrize("name", ["dot_git", "bare"]) +def test_git_directory_and_bare_repository_resolve_as_main_without_a_toplevel( + topology: Topology, name: str +) -> None: + command = "git update-ref refs/heads/d x" + path: Path = getattr(topology, name) + + facts = _only(command, path) + + _assert_resolved( + command, + facts.target, + cwd=path, + git_dir=path, + common_dir=path, + is_main=True, + toplevel=None, + ) + + +@pytest.mark.parametrize("lang", [None, "de_DE.UTF-8"], ids=["default-locale", "de_DE"]) +def test_directory_outside_any_repository_is_not_a_repository( + topology: Topology, monkeypatch: pytest.MonkeyPatch, lang: str | None +) -> None: + if lang is not None: + monkeypatch.setenv("LANG", lang) + monkeypatch.setenv("LC_ALL", lang) + monkeypatch.setenv("LANGUAGE", "de") + command = "git checkout -b x" + + facts = _only(command, topology.plain) + + context = _context(command, facts.target) + assert facts.target.kind == GitTargetKind.NOT_A_REPOSITORY, context + assert facts.target.reason == "", context + assert facts.target.cwd == str(topology.plain), context + + +@pytest.mark.parametrize( + ("command_template", "start"), + [("git checkout -b x", ""), ("true || cd {linked}; git checkout -b x", "primary")], + ids=["empty-cwd", "cd-after-or"], +) +def test_unproven_working_directory_is_unresolved_cwd_unknown( + topology: Topology, command_template: str, start: str +) -> None: + command = command_template.format(linked=topology.linked) + + facts = _only(command, getattr(topology, start) if start else "") + + _assert_unresolved(command, facts.target, "cwd_unknown") + + +def test_dash_c_relative_path_from_a_linked_worktree_resolves_to_the_main_worktree( + topology: Topology, +) -> None: + command = f"git -C {os.path.relpath(topology.primary, topology.linked)} checkout -b x" + + facts = _only(command, topology.linked) + + _assert_main_checkout(command, facts.target, topology, topology.primary) + + +def test_dash_c_tilde_path_resolves_through_the_hook_home( + topology: Topology, tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + home = tmp_path / "tilde-home" + home.mkdir() + (home / "primary").symlink_to(topology.primary) + monkeypatch.setenv("HOME", str(home)) + command = "git -C ~/primary checkout -b x" + + facts = _only(command, topology.plain) + + _assert_main_checkout(command, facts.target, topology, topology.primary) + + +def test_dash_c_dynamic_value_is_unresolved_dynamic_cwd( + topology: Topology, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.delenv("P", raising=False) + command = "git -C $P checkout -b x" + + facts = _only(command, topology.linked) + + _assert_unresolved(command, facts.target, "dynamic_cwd") + + +@pytest.mark.parametrize("tail", ["new-sub", "new-sub/deeper/still"]) +def test_dash_c_missing_path_resolves_through_its_nearest_existing_ancestor( + topology: Topology, tail: str +) -> None: + command = f"git -C {topology.primary / tail} checkout -b x" + + facts = _only(command, topology.plain) + + _assert_main_checkout(command, facts.target, topology, topology.primary) + + +def test_dash_c_missing_path_in_a_non_repository_is_not_a_repository( + topology: Topology, +) -> None: + command = f"git -C {topology.plain / 'new'} checkout -b x" + + facts = _only(command, topology.primary) + + context = _context(command, facts.target) + assert facts.target.kind == GitTargetKind.NOT_A_REPOSITORY, context + assert facts.target.cwd == str(topology.plain), context + + +def test_dash_c_dotdot_after_a_symlink_is_resolved_physically(topology: Topology) -> None: + command = f"git -C {topology.symlink_parent}/lnk/.. checkout -b x" + + facts = _only(command, topology.plain) + + _assert_main_checkout(command, facts.target, topology, topology.primary) + + +@pytest.mark.parametrize( + "command_template", + [ + "GIT_DIR={git_dir} git update-ref refs/heads/d x", + "export GIT_WORK_TREE={primary}; git checkout -b x", + "git --git-dir={git_dir} update-ref refs/heads/d x", + "git --bare update-ref refs/heads/d x", + "git -c core.worktree={primary} checkout -b x", + ], +) +def test_layout_override_channel_is_unresolved_layout_override( + topology: Topology, command_template: str +) -> None: + command = command_template.format(primary=topology.primary, git_dir=topology.dot_git) + + facts = _only(command, topology.plain) + + _assert_unresolved(command, facts.target, "layout_override") + + +def test_layout_variable_in_the_hook_environment_is_unresolved_layout_override( + topology: Topology, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("GIT_DIR", str(topology.dot_git)) + command = "git checkout -b x" + + facts = _only(command, topology.primary) + + _assert_unresolved(command, facts.target, "layout_override") + + +def test_unrecognized_global_option_is_unresolved_unparsed_global_options( + topology: Topology, +) -> None: + command = f"git --frobnicate -C {topology.primary} update-ref refs/heads/develop x" + + facts = _only(command, topology.linked) + + _assert_unresolved(command, facts.target, "unparsed_global_options") + + +@pytest.mark.parametrize( + "command", + ["P=checkout; git $P -B develop", "G=git; $G update-ref refs/heads/d x"], + ids=["dynamic-subcommand", "dynamic-command-word"], +) +def test_dynamic_command_or_subcommand_has_unknown_identity_and_an_ambiguous_outcome( + topology: Topology, command: str +) -> None: + facts = _only(command, topology.primary) + + _assert_main_checkout(command, facts.target, topology, topology.primary) + _assert_unknown_identity(command, facts) + + +@pytest.mark.parametrize( + "command", + [ + "git -c alias.x=y x", + "HOME=/h git x", + "export XDG_CONFIG_HOME=/x; git x", + "export GIT_CONFIG_COUNT=1; git x", + ], +) +def test_alias_channel_with_a_non_builtin_subcommand_has_unknown_identity( + topology: Topology, command: str +) -> None: + facts = _only(command, topology.primary) + + _assert_main_checkout(command, facts.target, topology, topology.primary) + _assert_unknown_identity(command, facts) + + +def test_alias_channel_does_not_unsettle_a_builtin_subcommand(topology: Topology) -> None: + command = "git -c alias.x=y checkout -b x" + + facts = _only(command, topology.primary) + + context = _context(command, facts) + assert facts.invocation.subcommand == "checkout", context + assert facts.identity_unknown is False, context + assert facts.outcome is not None, context + assert facts.outcome.head_effect == GitHeadEffect.MOVES, context + + +@pytest.mark.parametrize( + "command", ["git co -b x", "git nb x"], ids=["plain-alias", "alias-with-arguments"] +) +def test_repository_alias_is_expanded_to_its_builtin_invocation( + topology: Topology, command: str +) -> None: + facts = _only(command, topology.primary) + + context = _context(command, facts) + assert facts.invocation.subcommand == "checkout", context + assert facts.invocation.flags == ["-b", "x"], context + assert facts.identity_unknown is False, context + assert facts.outcome is not None, context + assert facts.outcome.head_effect == GitHeadEffect.MOVES, context + + +def test_global_alias_from_the_ambient_config_is_expanded( + topology: Topology, tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + home = tmp_path / "alias-home" + home.mkdir() + config = home / ".gitconfig" + config.write_text("[alias]\n\tgco = checkout\n", encoding="utf-8") + monkeypatch.setenv("HOME", str(home)) + monkeypatch.setenv("GIT_CONFIG_GLOBAL", str(config)) + command = "git gco -b x" + + facts = _only(command, topology.primary) + + context = _context(command, facts) + assert facts.invocation.subcommand == "checkout", context + assert facts.invocation.flags == ["-b", "x"], context + assert facts.identity_unknown is False, context + + +@pytest.mark.parametrize( + "command", + ["git sh", "git ext version"], + ids=["shell-alias", "alias-to-non-builtin"], +) +def test_opaque_alias_has_unknown_identity_and_an_ambiguous_outcome( + topology: Topology, command: str +) -> None: + facts = _only(command, topology.primary) + + _assert_main_checkout(command, facts.target, topology, topology.primary) + _assert_unknown_identity(command, facts) + + +@pytest.mark.parametrize("command", ["git lfs version", "git-frobnicate --x"]) +def test_external_git_command_is_not_classified(topology: Topology, command: str) -> None: + classified = _classified(command, topology.primary) + + assert classified == [], _context(command, classified) + + +@pytest.mark.parametrize( + ("command", "subcommand", "flags"), + [ + ("/usr/lib/git-core/git-checkout -b x", "checkout", ["-b", "x"]), + ("git-update-ref refs/heads/d x", "update-ref", ["refs/heads/d", "x"]), + ], +) +def test_git_builtin_executable_is_analysed_as_the_subcommand( + topology: Topology, command: str, subcommand: str, flags: list[str] +) -> None: + facts = _only(command, topology.primary) + + _assert_main_checkout(command, facts.target, topology, topology.primary) + context = _context(command, facts) + assert facts.invocation.subcommand == subcommand, context + assert facts.invocation.flags == flags, context + assert facts.identity_unknown is False, context + + +@pytest.mark.parametrize("start", ["plain", "linked"]) +@pytest.mark.parametrize("tail", ["develop", "feature/missing"]) +def test_ref_path_resolves_the_primary_from_any_working_directory( + topology: Topology, monkeypatch: pytest.MonkeyPatch, start: str, tail: str +) -> None: + monkeypatch.chdir(getattr(topology, start)) + heads = topology.dot_git / "refs" / "heads" + path = heads / tail + + target = resolve_git_target_for_path(str(path), {}) + + _assert_resolved( + str(path), + target, + cwd=heads, + git_dir=topology.dot_git, + common_dir=topology.dot_git, + is_main=True, + toplevel=None, + ) + + +def test_repository_is_queried_once_per_canonical_path_per_cache( + topology: Topology, monkeypatch: pytest.MonkeyPatch +) -> None: + queries: list[tuple[str, ...]] = [] + real_git_result = _git_target._git_result + + def recording_git_result(cwd: str, *args: str) -> Any: + queries.append((cwd, *args)) + return real_git_result(cwd, *args) + + monkeypatch.setattr(_git_target, "_git_result", recording_git_result) + cache: dict[Any, Any] = {} + + first = resolve_git_target_for_path(str(topology.subdir), cache) + queries_after_first = len(queries) + second = resolve_git_target_for_path(str(topology.primary_link / "sub"), cache) + + assert first == second, _git_version() + assert queries_after_first > 0, _git_version() + assert len(queries) == queries_after_first, f"{queries} ({_git_version()})" + + +def test_mutation_subcommands_are_builtin_subcommands() -> None: + assert _GIT_MUTATION_SUBCOMMANDS <= _GIT_BUILTIN_SUBCOMMANDS, sorted( + _GIT_MUTATION_SUBCOMMANDS - _GIT_BUILTIN_SUBCOMMANDS + ) diff --git a/tests/hooks/test_hook_payload.py b/tests/hooks/test_hook_payload.py new file mode 100644 index 0000000000..8102c50fa2 --- /dev/null +++ b/tests/hooks/test_hook_payload.py @@ -0,0 +1,149 @@ +"""Payload facts extracted by ``parse_hook_command`` in hooks/_runtime/_hook_payload.py. + +Members: the Bash per-call ``workdir`` rows (absolute, relative and blank), the +payload-cwd rows that carry no ``workdir``, the run_cmd rows that ignore it, and +the github_mutation_guard decisions that separate ``RELATIVE_WORKDIR`` from the +session-cwd anomalies. +""" + +from __future__ import annotations + +from pathlib import Path + +import pytest + +# The sys.path bootstrap for bare-name hook-sibling imports is centralized in +# tests/conftest.py. +from _hook_payload import ( # type: ignore[import-not-found] + PayloadAnomaly, + parse_hook_command, +) + +from autoskillit.hooks.guards.github_mutation_guard import DenyTrigger, decide + +from .conftest import _RUN_CMD_TOOL_DIRECT, _RUN_CMD_TOOL_MARKETPLACE, make_hook_event + +pytestmark = [pytest.mark.layer("hooks"), pytest.mark.small] + +_COMMAND = "echo ok" + + +def test_absolute_workdir_is_the_bash_execution_cwd(tmp_path: Path) -> None: + session = tmp_path / "session" + workdir = tmp_path / "work" + event = make_hook_event( + tool="Bash", + command=_COMMAND, + payload_cwd=str(session), + extra_tool_input={"workdir": str(workdir)}, + ) + + parsed = parse_hook_command(event) + + assert parsed.tool_kind == "bash" + assert parsed.command == _COMMAND + assert parsed.execution_cwd == str(workdir) + assert parsed.payload_cwd == str(session) + assert parsed.anomalies == () + + +@pytest.mark.parametrize("workdir", ["", " ", "work", "./work", "../work", "~/work"], ids=repr) +def test_relative_or_blank_workdir_is_a_relative_workdir_anomaly( + tmp_path: Path, workdir: str +) -> None: + event = make_hook_event( + tool="Bash", + command=_COMMAND, + payload_cwd=str(tmp_path), + extra_tool_input={"workdir": workdir}, + ) + + parsed = parse_hook_command(event) + + assert parsed.execution_cwd == "" + assert parsed.payload_cwd == str(tmp_path) + assert parsed.anomalies == (PayloadAnomaly.RELATIVE_WORKDIR,) + + +@pytest.mark.parametrize( + ("payload_cwd", "execution_cwd", "anomalies"), + [ + ("/session", "/session", ()), + (None, "", ()), + ("session", "", (PayloadAnomaly.RELATIVE_CWD,)), + (7, "", (PayloadAnomaly.NON_STRING_CWD,)), + ], + ids=["absolute", "missing", "relative", "non-string"], +) +def test_bash_payload_without_workdir_uses_the_session_cwd( + payload_cwd: object, execution_cwd: str, anomalies: tuple[PayloadAnomaly, ...] +) -> None: + event = {"tool_name": "Bash", "tool_input": {"command": _COMMAND}, "cwd": payload_cwd} + + parsed = parse_hook_command(event) + + assert parsed.execution_cwd == execution_cwd + assert parsed.anomalies == anomalies + + +@pytest.mark.parametrize( + "tool_name", [_RUN_CMD_TOOL_MARKETPLACE, _RUN_CMD_TOOL_DIRECT], ids=["marketplace", "direct"] +) +@pytest.mark.parametrize("workdir", ["/elsewhere", "relative", ""], ids=repr) +def test_run_cmd_execution_cwd_ignores_workdir(tool_name: str, workdir: str) -> None: + with_cwd = make_hook_event( + tool="run_cmd", + command=_COMMAND, + payload_cwd="/session", + tool_cwd="/target", + extra_tool_input={"workdir": workdir}, + run_cmd_tool_name=tool_name, + ) + without_cwd = make_hook_event( + tool="run_cmd", + command=_COMMAND, + payload_cwd="/session", + extra_tool_input={"workdir": workdir}, + run_cmd_tool_name=tool_name, + ) + + parsed_with_cwd = parse_hook_command(with_cwd) + parsed_without_cwd = parse_hook_command(without_cwd) + + assert parsed_with_cwd.tool_kind == "run_cmd" + assert parsed_with_cwd.execution_cwd == "/target" + assert parsed_with_cwd.anomalies == () + assert parsed_without_cwd.execution_cwd == "" + assert parsed_without_cwd.anomalies == () + + +def test_relative_workdir_does_not_trigger_the_github_guard_malformed_cwd_denial( + tmp_path: Path, +) -> None: + event = make_hook_event( + tool="Bash", + command=_COMMAND, + payload_cwd=str(tmp_path), + extra_tool_input={"workdir": "relative"}, + ) + parsed = parse_hook_command(event) + + decision = decide(parsed) + + assert parsed.anomalies == (PayloadAnomaly.RELATIVE_WORKDIR,) + assert decision.allow is True + assert decision.trigger is None + + +@pytest.mark.parametrize( + "payload_cwd", ["relative", 7], ids=["relative-session-cwd", "non-string-session-cwd"] +) +def test_session_cwd_anomalies_keep_the_github_guard_malformed_cwd_denial( + payload_cwd: object, +) -> None: + event = {"tool_name": "Bash", "tool_input": {"command": _COMMAND}, "cwd": payload_cwd} + + decision = decide(parse_hook_command(event)) + + assert decision.allow is False + assert decision.trigger is DenyTrigger.MALFORMED_CWD diff --git a/tests/hooks/test_shell_segment_walk.py b/tests/hooks/test_shell_segment_walk.py new file mode 100644 index 0000000000..fa22485cc5 --- /dev/null +++ b/tests/hooks/test_shell_segment_walk.py @@ -0,0 +1,332 @@ +"""Cwd certainty of evaluated shell segments and the tokenizer facts it rests on. + +Members: ``walk_evaluated_segments`` ``cwd_certain`` and ``verb_cwd`` rows, the +tokenizer's ``connector``, ``after_keyword`` and ``piped_from_previous`` fields +with the pipe-payload reach they keep, and the ``command_channel_flags`` rows. + +Commands name the working directory as ``@w`` (the initial cwd), the existing +directories ``@a`` and ``@b``, and ``@missing``, which is never created. Each +command is bound to real absolute paths under ``tmp_path`` before it is walked. +The segment under test is always the one that runs ``git ``. +""" + +from __future__ import annotations + +import re +from pathlib import Path + +import pytest + +from autoskillit.hooks._classification._tokenizer import ( + _CommandSegment, + _tokenize_command_segments_with_redirects, +) +from autoskillit.hooks._runtime._command_classification import ( + all_evaluated_segments, + live_command_text, +) +from autoskillit.hooks._runtime._git_target import ChannelFlags, command_channel_flags +from autoskillit.hooks._runtime._shell_segment_walk import ( + WalkedSegment, + walk_evaluated_segments, +) + +pytestmark = [pytest.mark.layer("hooks"), pytest.mark.small] + +_PLACEHOLDER_RE = re.compile(r"@(w|a|b|missing)\b") +_AMBIENT_ENV_NAMES = ("CDPATH", "GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR") + + +@pytest.fixture(autouse=True) +def _hermetic_environment(monkeypatch: pytest.MonkeyPatch) -> None: + for name in _AMBIENT_ENV_NAMES: + monkeypatch.delenv(name, raising=False) + + +@pytest.fixture +def dirs(tmp_path: Path) -> dict[str, str]: + for name in ("w", "a", "b"): + (tmp_path / name).mkdir() + return {name: str((tmp_path / name).resolve()) for name in ("w", "a", "b", "missing")} + + +def _bind(command: str, dirs: dict[str, str]) -> str: + return _PLACEHOLDER_RE.sub(lambda match: dirs[match.group(1)], command) + + +def _row_id(command: str, outcome: object) -> str: + return f"{outcome}-{command.replace(chr(10), '')}" + + +def _git_segment(walk: list[WalkedSegment], subcommand: str) -> WalkedSegment: + found = [ + seg + for seg in walk + if seg.start is not None and seg.executable[seg.start :][:2] == ["git", subcommand] + ] + assert len(found) == 1, f"expected one `git {subcommand}` segment, found {len(found)}" + return found[0] + + +def _walk(command: str, dirs: dict[str, str], cwd: str) -> list[WalkedSegment]: + walk = walk_evaluated_segments(_bind(command, dirs), cwd) + assert walk is not None + return walk + + +def _certainty_rows(certain: bool, *commands: str, subcommand: str = "x") -> list[object]: + return [ + pytest.param(command, certain, subcommand, id=_row_id(command, certain)) + for command in commands + ] + + +_SPAWN_PROGRAM = 'import subprocess; subprocess.run(["git", "x"], cwd="@a")' + +_CERTAIN_CASES = [ + # The Git segment runs only after a cd that certainly ran. A missing target + # still counts inside an unbroken && chain, which skips Git when the cd fails. + *_certainty_rows( + True, + "cd @a && git x", + "cd @a; git x", + "cd @a || exit 1; git x", + "pushd @a >/dev/null && git x", + "make && cd @a && git x", + "cd @missing && git x", + "cd @a &&\ngit x", + "cd @a # go\ngit x", + ), + *_certainty_rows( + True, "git status || true; cd @a && git checkout -b x", subcommand="checkout" + ), + # A child shell started after a certain cd inherits it. + *_certainty_rows(True, "cd @a && bash -c 'git x'; cd @b"), + # A per-invocation override seeds its own certain cwd. + *_certainty_rows( + True, + "env -C @a git x", + f"python3 -c '{_SPAWN_PROGRAM}'", + "cd $X && env -C @a git x", + ), +] + +_UNCERTAIN_CASES = [ + # The cd may not have run, may have failed, or ran in another process. + *_certainty_rows( + False, + "cd @missing; git x", + "true || cd @a; git x", + "false && cd @a; git x", + "true || cd @a && git x", + "echo | cd @a && git x", + "cd @a | git x", + "cd @a & git x", + "true ||\ncd @a\ngit x", + ), + # A cd inside a compound body or function body never certainly ran. + *_certainty_rows( + False, + "if false; then cd @a; fi; git x", + "if false; then true; cd @a; fi; git x", + "for d in a; do cd @a; done; git x", + "false && { true; cd @a; }; git x", + "f() { cd @a; }; git x", + "case $v in a) cd @a;; esac; git x", + "bash -c 'f() { cd @a; }; git x'", + ), + # Bare keyword lines leave no trace of the condition in the command text. + *_certainty_rows( + False, + "if\nfalse\nthen\ncd @a\nfi\ngit x", + "while\ntrue\ndo\ncd @a\ndone\ngit x", + ), + # The connector resets at a group close. + *_certainty_rows(False, "{ true; } || cd @a; git x"), + # A redirect's child-scope records do not hide the pipe that follows. + *_certainty_rows(False, 'cd @a >"$(echo f)" | git x'), + # Taint lasts for the scope, so an earlier pipeline over-denies. + *_certainty_rows(False, "ls | head; cd @a; git x"), + *_certainty_rows(False, "cd @a && bash -c 'false && cd @b; git x'"), + # A dynamic or relative override does not stand alone. + *_certainty_rows(False, "cd $X && env -C sub git x", 'env -C "$D" git x'), +] + + +@pytest.mark.parametrize( + ("command", "certain", "subcommand"), [*_CERTAIN_CASES, *_UNCERTAIN_CASES] +) +def test_git_segment_cwd_certainty( + dirs: dict[str, str], command: str, certain: bool, subcommand: str +) -> None: + segment = _git_segment(_walk(command, dirs, dirs["w"]), subcommand) + assert segment.cwd_certain is certain + + +@pytest.mark.parametrize( + ("command", "certain", "verb_cwd"), + [ + pytest.param("(cd @a); git x", True, "@w", id="subshell-cd-leaves-the-cwd"), + pytest.param("pushd @a; popd; git x", False, "", id="popd-leaves-the-cwd-unknown"), + pytest.param("cd $X && git x", False, "", id="dynamic-target"), + pytest.param("cd - && git x", False, "", id="previous-directory"), + pytest.param("builtin cd @a && git x", False, "@w", id="builtin-cd-is-unmodelled"), + ], +) +def test_git_segment_cwd_and_certainty_together( + dirs: dict[str, str], command: str, certain: bool, verb_cwd: str +) -> None: + segment = _git_segment(_walk(command, dirs, dirs["w"]), "x") + assert segment.cwd_certain is certain + assert segment.verb_cwd == _bind(verb_cwd, dirs) + + +def test_unknown_initial_cwd_is_never_certain(dirs: dict[str, str]) -> None: + segment = _git_segment(_walk("git x", dirs, ""), "x") + assert segment.verb_cwd == "" + assert segment.cwd_certain is False + + +def _tokenize(command: str) -> list[_CommandSegment]: + segments = _tokenize_command_segments_with_redirects(command) + assert segments is not None + return segments + + +def test_trailing_comment_does_not_join_the_next_line() -> None: + command = "echo hi # c\ngit push origin main" + segments = _tokenize(command) + assert [segment.tokens for segment in segments] == [ + ["echo", "hi"], + ["git", "push", "origin", "main"], + ] + assert segments[1].connector == ";" + assert all_evaluated_segments(command) == [["echo", "hi"], ["git", "push", "origin", "main"]] + + +def test_newline_after_and_keeps_the_and_connector() -> None: + segments = _tokenize("cd /a &&\ngit x") + assert [segment.tokens for segment in segments] == [["cd", "/a"], ["git", "x"]] + assert segments[1].connector == "&&" + + +def test_newline_after_pipe_keeps_the_pipe_and_its_payload() -> None: + command = "echo 'git push' |\nbash" + bash = _tokenize(command)[-1] + assert bash.tokens == ["bash"] + assert bash.piped_from_previous is True + assert bash.connector == "|" + assert ["git", "push"] in (all_evaluated_segments(command) or []) + + +@pytest.mark.parametrize( + ("command", "connectors"), + [ + pytest.param("a && b", ["", "&&"], id="and"), + pytest.param("a || b", ["", "||"], id="or"), + pytest.param("a | b", ["", "|"], id="pipe"), + pytest.param("a & b", ["", "&"], id="background"), + pytest.param("a ; b", ["", ";"], id="semicolon"), + pytest.param("a\nb", ["", ";"], id="newline"), + pytest.param("a && b\nc", ["", "&&", ";"], id="newline-after-a-complete-chain"), + pytest.param("a &&\nb", ["", "&&"], id="newline-after-and"), + pytest.param("a ||\n\nb", ["", "||"], id="blank-lines-after-or"), + pytest.param("a |\nb", ["", "|"], id="newline-after-pipe"), + pytest.param("a &\nb", ["", "&"], id="newline-after-background"), + pytest.param("{ a; } || b", ["", "||"], id="group-close-clears-the-pending-connector"), + pytest.param("a && { b; }", ["", "&&"], id="group-open-keeps-the-connector"), + pytest.param("a && (b)", ["", "&&"], id="subshell-open-keeps-the-connector"), + pytest.param("a ||\n{\nb\n}", ["", "||"], id="brace-group-on-its-own-lines"), + ], +) +def test_connector_joining_each_segment(command: str, connectors: list[str]) -> None: + assert [segment.connector for segment in _tokenize(command)] == connectors + + +@pytest.mark.parametrize( + ("command", "expected"), + [ + pytest.param( + "if\nfalse\nthen\ncd /a\nfi\ngit x", + [ + (["false"], True), + (["cd", "/a"], True), + (["fi"], False), + (["git", "x"], False), + ], + id="if-then-on-bare-lines", + ), + pytest.param( + "while\ntrue\ndo\ncd /a\ndone\ngit x", + [ + (["true"], True), + (["cd", "/a"], True), + (["done"], False), + (["git", "x"], False), + ], + id="while-do-on-bare-lines", + ), + pytest.param( + "if a; then b; fi", + [(["if", "a"], False), (["then", "b"], False), (["fi"], False)], + id="keyword-with-a-command-is-not-dropped", + ), + ], +) +def test_dropped_bare_keyword_marks_the_next_segment( + command: str, expected: list[tuple[list[str], bool]] +) -> None: + segments = _tokenize(command) + assert [(segment.tokens, segment.after_keyword) for segment in segments] == expected + + +def test_dropped_bare_keyword_keeps_the_pending_connector() -> None: + segments = _tokenize("a &&\nif\nb") + assert [(s.tokens, s.connector, s.after_keyword) for s in segments] == [ + (["a"], "", False), + (["b"], "&&", True), + ] + + +_LAYOUT_COMMANDS = ( + "export GIT_DIR=/r/.git; git x", + "GIT_WORK_TREE=/r git x", + "git --git-dir=/r x", + "git --bare x", + "git -c core.worktree=/r x", + "git -c include.path=/x y", +) +_ALIAS_COMMANDS = ( + "git -c alias.x=y x", + "export GIT_CONFIG_COUNT=1; git x", + "HOME=/h git x", + "export XDG_CONFIG_HOME=/x; git x", + "git config alias.ur update-ref && git ur a b", +) + + +def _flags(command: str) -> ChannelFlags: + return command_channel_flags(live_command_text(command)) + + +@pytest.mark.parametrize("command", _LAYOUT_COMMANDS) +def test_layout_channels_in_command_text(command: str) -> None: + assert _flags(command) == ChannelFlags(layout_override=True, alias_channel=False) + + +@pytest.mark.parametrize("command", _ALIAS_COMMANDS) +def test_alias_channels_in_command_text(command: str) -> None: + assert _flags(command) == ChannelFlags(layout_override=False, alias_channel=True) + + +@pytest.mark.parametrize("name", ["GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR"]) +def test_layout_variable_in_the_hook_environment_sets_layout_override( + monkeypatch: pytest.MonkeyPatch, name: str +) -> None: + monkeypatch.setenv(name, "/r/.git") + assert _flags("git x") == ChannelFlags(layout_override=True, alias_channel=False) + + +def test_ambient_home_alone_sets_neither_flag(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("HOME", "/h") + assert _flags("git x") == ChannelFlags(layout_override=False, alias_channel=False) diff --git a/tests/infra/_git_ops_guard_harness.py b/tests/infra/_git_ops_guard_harness.py new file mode 100644 index 0000000000..4cabe17910 --- /dev/null +++ b/tests/infra/_git_ops_guard_harness.py @@ -0,0 +1,236 @@ +"""Shared harness for git_ops_guard tests: payloads, a guard runner and repositories. + +Members: _make_clean_env, _build_payload, _run_guard, _guard_exit_code, +_is_denied, _checked_out_ref_result, _guard_verdict, _git, build_linked_repo. + +``UNSET`` distinguishes "use the default" from an explicit ``""`` or ``None`` for +the payload's cwd fields, so a test can send a relative or empty cwd. +""" + +from __future__ import annotations + +import contextlib +import io +import json +import os +import subprocess +import unittest.mock +from pathlib import Path + +import pytest + +from autoskillit.hooks.guards import git_ops_guard +from tests._git_topology import add_linked_worktree, commit_file, head, init_checkout + +UNSET: object = object() + +_TOOL_NAME = "mcp__autoskillit__local__autoskillit__run_cmd" +_BASH_TOOL_NAME = "Bash" +_HOOK_CONFIG_RELPATH = ".autoskillit/temp/.hook_config.json" + + +def _make_clean_env( + skill_name: str | None, + session_type: str | None = None, + headless: bool = True, +) -> dict[str, str]: + env = {k: v for k, v in os.environ.items()} + if headless: + env["AUTOSKILLIT_HEADLESS"] = "1" + else: + env.pop("AUTOSKILLIT_HEADLESS", None) + if skill_name is not None: + env["AUTOSKILLIT_SKILL_NAME"] = skill_name + else: + env.pop("AUTOSKILLIT_SKILL_NAME", None) + if session_type is not None: + env["AUTOSKILLIT_SESSION_TYPE"] = session_type + else: + env.pop("AUTOSKILLIT_SESSION_TYPE", None) + return env + + +def _build_payload( + cmd: str, + tmpdir: Path, + *, + use_bash_key: bool = False, + include_execution_cwd: bool = True, + payload_cwd: object = UNSET, + tool_cwd: object = UNSET, + tool_input_extra: dict[str, object] | None = None, +) -> dict[str, object]: + """A hook payload in the Bash or run_cmd shape. + + ``payload_cwd`` is the top-level session cwd; ``tool_cwd`` is run_cmd's own + ``tool_input.cwd``. ``None`` omits a field, ``UNSET`` takes the default. + """ + tool_input: dict[str, object] = {("command" if use_bash_key else "cmd"): cmd} + if tool_cwd is not UNSET: + if tool_cwd is not None: + tool_input["cwd"] = str(tool_cwd) + elif include_execution_cwd and not use_bash_key: + tool_input["cwd"] = str(tmpdir) + tool_input.update(tool_input_extra or {}) + payload: dict[str, object] = { + "session_id": "guard-test-session", + "tool_name": _BASH_TOOL_NAME if use_bash_key else _TOOL_NAME, + "tool_input": tool_input, + } + if payload_cwd is not UNSET: + if payload_cwd is not None: + payload["cwd"] = str(payload_cwd) + elif include_execution_cwd and use_bash_key: + payload["cwd"] = str(tmpdir) + return payload + + +def _run_guard( + cmd: str, + kitchen_open: bool, + tmpdir, + raw_stdin: str | None = None, + skill_name: str | None = None, + session_type: str | None = None, + use_bash_key: bool = False, + headless: bool = True, + include_execution_cwd: bool = True, + hook_config: dict[str, object] | None = None, + *, + extra_env: dict[str, str] | None = None, + marker_root: Path | None = None, + payload_cwd: object = UNSET, + tool_cwd: object = UNSET, + tool_input_extra: dict[str, object] | None = None, + process_cwd: Path | None = None, +) -> str: + """Invoke git_ops_guard.main() and return captured stdout. + + The kitchen marker is written under ``marker_root`` (default ``tmpdir``); + ``process_cwd`` is the patched ``Path.cwd()`` the guard falls back to when + the payload carries no cwd. + """ + if raw_stdin is not None: + stdin_content = raw_stdin + else: + stdin_content = json.dumps( + _build_payload( + cmd, + tmpdir, + use_bash_key=use_bash_key, + include_execution_cwd=include_execution_cwd, + payload_cwd=payload_cwd, + tool_cwd=tool_cwd, + tool_input_extra=tool_input_extra, + ) + ) + + if kitchen_open: + hook_cfg = (marker_root or tmpdir) / _HOOK_CONFIG_RELPATH + hook_cfg.parent.mkdir(parents=True, exist_ok=True) + hook_cfg.write_text(json.dumps(hook_config or {"kitchen": "open"})) + + clean_env = _make_clean_env(skill_name, session_type, headless=headless) + clean_env.update(extra_env or {}) + + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + with unittest.mock.patch.dict(os.environ, clean_env, clear=True): + with unittest.mock.patch("sys.stdin", io.StringIO(stdin_content)): + with unittest.mock.patch("pathlib.Path.cwd", return_value=process_cwd or tmpdir): + try: + git_ops_guard.main() + except SystemExit as exc: + assert exc.code == 0, f"Guard exited non-zero: {exc.code!r}" + + return buf.getvalue() + + +def _guard_exit_code(cmd: str, tmpdir: Path, **payload_kwargs: object) -> int | str | None: + """Run git_ops_guard.main() in a headless skill session with the kitchen open + and return the code it exits with. + + _run_guard requires a clean exit; this expects main() to terminate through + SystemExit and leaves stdout and stderr to the caller's capsys. + """ + hook_cfg = tmpdir / _HOOK_CONFIG_RELPATH + hook_cfg.parent.mkdir(parents=True, exist_ok=True) + hook_cfg.write_text(json.dumps({"kitchen": "open"})) + payload = _build_payload(cmd, tmpdir, **payload_kwargs) # type: ignore[arg-type] + with ( + unittest.mock.patch.dict(os.environ, _make_clean_env(None), clear=True), + unittest.mock.patch("sys.stdin", io.StringIO(json.dumps(payload))), + unittest.mock.patch("pathlib.Path.cwd", return_value=tmpdir), + pytest.raises(SystemExit) as exit_info, + ): + git_ops_guard.main() + return exit_info.value.code + + +def _is_denied(output: str) -> bool: + if not output: + return False + data = json.loads(output) + return data.get("hookSpecificOutput", {}).get("permissionDecision") == "deny" + + +def _checked_out_ref_result(output: str) -> dict[str, object]: + data = json.loads(output) + reason = data["hookSpecificOutput"]["permissionDecisionReason"] + prefix = git_ops_guard.CHECKED_OUT_REF_DENY_PREFIX + assert reason.startswith(prefix) + return json.loads(reason.removeprefix(prefix)) + + +def _guard_verdict(output: str) -> str: + """Classify captured guard stdout as allow, checked-out-ref or other-deny.""" + if not output.strip(): + return "allow" + assert _is_denied(output), output + reason = json.loads(output)["hookSpecificOutput"]["permissionDecisionReason"] + if reason.startswith(git_ops_guard.CHECKED_OUT_REF_DENY_PREFIX): + return "checked-out-ref" + return "other-deny" + + +def _git(cwd: Path, *args: str, input_text: str | None = None) -> str: + return subprocess.run( + ["git", "-C", str(cwd), *args], + check=True, + capture_output=True, + text=True, + input=input_text, + ).stdout.strip() + + +def build_linked_repo(tmp_path: Path, *, tracked_state_dir: bool = False) -> dict[str, Path | str]: + """A primary on ``develop``, a linked worktree on ``review`` and two bystanders. + + ``unrelated`` is a separate repository on ``main``; ``bare`` is a bare + repository. With ``tracked_state_dir`` the primary commits a + ``.autoskillit/config.yaml`` before the linked worktree is created, so both + worktrees carry their own ``.autoskillit/`` directory. + """ + primary = init_checkout(tmp_path / "repo") + _git(primary, "branch", "-m", "develop") + commit_file(primary, "tracked.txt", "initial\n", "initial") + if tracked_state_dir: + commit_file(primary, ".autoskillit/config.yaml", "{}\n", "state dir") + old_sha = head(primary) + tree = _git(primary, "rev-parse", "HEAD^{tree}") + new_sha = _git(primary, "commit-tree", tree, "-p", old_sha, input_text="next\n") + linked = add_linked_worktree(primary, "review", start=old_sha) + unrelated = init_checkout(tmp_path / "elsewhere") + _git(unrelated, "branch", "-m", "main") + bare = tmp_path / "bare.git" + _git(tmp_path, "init", "--bare", str(bare)) + return { + "primary": primary, + "linked": linked, + "unrelated": unrelated, + "bare": bare, + "old_sha": old_sha, + "new_sha": new_sha, + "worktree_git_dir": _git(linked, "rev-parse", "--absolute-git-dir"), + "common_git_dir": str((linked / _git(linked, "rev-parse", "--git-common-dir")).resolve()), + } diff --git a/tests/infra/test_git_ops_guard.py b/tests/infra/test_git_ops_guard.py index 88cafa9113..072020c896 100644 --- a/tests/infra/test_git_ops_guard.py +++ b/tests/infra/test_git_ops_guard.py @@ -8,14 +8,11 @@ from __future__ import annotations -import contextlib -import io import json import os import shlex import subprocess import sys -import unittest.mock from collections.abc import Mapping from pathlib import Path @@ -29,177 +26,29 @@ from autoskillit.hooks._runtime._git_command_classification import _classify_fetch from autoskillit.hooks._runtime._git_option_tables import _GIT_FETCH_FLAG_SPEC from autoskillit.hooks.guards import git_ops_guard +from tests.infra._git_ops_guard_harness import ( + _TOOL_NAME, + _checked_out_ref_result, + _git, + _guard_exit_code, + _guard_verdict, + _is_denied, + _run_guard, + build_linked_repo, +) pytestmark = [pytest.mark.layer("infra"), pytest.mark.medium] -_TOOL_NAME = "mcp__autoskillit__local__autoskillit__run_cmd" -_BASH_TOOL_NAME = "Bash" -_HOOK_CONFIG_RELPATH = ".autoskillit/temp/.hook_config.json" - - -def _make_clean_env( - skill_name: str | None, - session_type: str | None = None, - headless: bool = True, -) -> dict[str, str]: - env = {k: v for k, v in os.environ.items()} - if headless: - env["AUTOSKILLIT_HEADLESS"] = "1" - else: - env.pop("AUTOSKILLIT_HEADLESS", None) - if skill_name is not None: - env["AUTOSKILLIT_SKILL_NAME"] = skill_name - else: - env.pop("AUTOSKILLIT_SKILL_NAME", None) - if session_type is not None: - env["AUTOSKILLIT_SESSION_TYPE"] = session_type - else: - env.pop("AUTOSKILLIT_SESSION_TYPE", None) - return env - - -def _run_guard( - cmd: str, - kitchen_open: bool, - tmpdir, - raw_stdin: str | None = None, - skill_name: str | None = None, - session_type: str | None = None, - use_bash_key: bool = False, - headless: bool = True, - include_execution_cwd: bool = True, - hook_config: dict[str, object] | None = None, -) -> str: - """Invoke git_ops_guard.main() and return captured stdout.""" - from autoskillit.hooks.guards.git_ops_guard import main - - if raw_stdin is not None: - stdin_content = raw_stdin - else: - cmd_key = "command" if use_bash_key else "cmd" - tool_input = {cmd_key: cmd} - if include_execution_cwd and not use_bash_key: - tool_input["cwd"] = str(tmpdir) - tool_name = _BASH_TOOL_NAME if use_bash_key else _TOOL_NAME - stdin_payload = { - "session_id": "guard-test-session", - "tool_name": tool_name, - "tool_input": tool_input, - } - if include_execution_cwd and use_bash_key: - stdin_payload["cwd"] = str(tmpdir) - stdin_content = json.dumps(stdin_payload) - - if kitchen_open: - hook_cfg = tmpdir / _HOOK_CONFIG_RELPATH - hook_cfg.parent.mkdir(parents=True, exist_ok=True) - hook_cfg.write_text(json.dumps(hook_config or {"kitchen": "open"})) - - clean_env = _make_clean_env(skill_name, session_type, headless=headless) - - buf = io.StringIO() - with contextlib.redirect_stdout(buf): - with unittest.mock.patch.dict(os.environ, clean_env, clear=True): - with unittest.mock.patch("sys.stdin", io.StringIO(stdin_content)): - with unittest.mock.patch("pathlib.Path.cwd", return_value=tmpdir): - try: - main() - except SystemExit as exc: - assert exc.code == 0, f"Guard exited non-zero: {exc.code!r}" - - return buf.getvalue() - - -def _is_denied(output: str) -> bool: - if not output: - return False - data = json.loads(output) - return data.get("hookSpecificOutput", {}).get("permissionDecision") == "deny" - - -def _git(cwd: Path, *args: str, input_text: str | None = None) -> str: - return subprocess.run( - ["git", "-C", str(cwd), *args], - check=True, - capture_output=True, - text=True, - input=input_text, - ).stdout.strip() - @pytest.fixture def linked_repo(tmp_path: Path) -> dict[str, Path | str]: - primary = tmp_path / "repo" - linked = tmp_path / "review" - primary.mkdir() - _git(primary, "init", "-b", "develop") - _git(primary, "config", "user.name", "Guard Test") - _git(primary, "config", "user.email", "guard@example.invalid") - (primary / "tracked.txt").write_text("initial\n", encoding="utf-8") - _git(primary, "add", "tracked.txt") - _git(primary, "commit", "-m", "initial") - old_sha = _git(primary, "rev-parse", "HEAD") - tree = _git(primary, "rev-parse", "HEAD^{tree}") - new_sha = _git(primary, "commit-tree", tree, "-p", old_sha, input_text="next\n") - _git(primary, "worktree", "add", "-b", "review", str(linked), old_sha) - return { - "primary": primary, - "linked": linked, - "old_sha": old_sha, - "new_sha": new_sha, - "worktree_git_dir": _git(linked, "rev-parse", "--absolute-git-dir"), - "common_git_dir": str((linked / _git(linked, "rev-parse", "--git-common-dir")).resolve()), - } - - -def _checked_out_ref_result(output: str) -> dict[str, object]: - data = json.loads(output) - reason = data["hookSpecificOutput"]["permissionDecisionReason"] - prefix = "Checked-out ref mutation blocked: " - assert reason.startswith(prefix) - return json.loads(reason.removeprefix(prefix)) + return build_linked_repo(tmp_path) def _assert_ref_unchanged(repo: Path, ref: str, old_sha: str) -> None: assert _git(repo, "rev-parse", ref) == old_sha -def _guard_verdict(output: str) -> str: - """Classify captured guard stdout as allow, checked-out-ref or other-deny.""" - if not output.strip(): - return "allow" - assert _is_denied(output), output - reason = json.loads(output)["hookSpecificOutput"]["permissionDecisionReason"] - if reason.startswith(git_ops_guard.CHECKED_OUT_REF_DENY_PREFIX): - return "checked-out-ref" - return "other-deny" - - -def _guard_exit_code(cmd: str, tmpdir: Path) -> int | str | None: - """Run git_ops_guard.main() in a headless skill session with the kitchen open - and return the code it exits with. - - _run_guard requires a clean exit; this expects main() to terminate through - SystemExit and leaves stdout and stderr to the caller's capsys. - """ - hook_cfg = tmpdir / _HOOK_CONFIG_RELPATH - hook_cfg.parent.mkdir(parents=True, exist_ok=True) - hook_cfg.write_text(json.dumps({"kitchen": "open"})) - payload = { - "session_id": "guard-test-session", - "tool_name": _TOOL_NAME, - "tool_input": {"cmd": cmd, "cwd": str(tmpdir)}, - } - with ( - unittest.mock.patch.dict(os.environ, _make_clean_env(None), clear=True), - unittest.mock.patch("sys.stdin", io.StringIO(json.dumps(payload))), - unittest.mock.patch("pathlib.Path.cwd", return_value=tmpdir), - pytest.raises(SystemExit) as exit_info, - ): - git_ops_guard.main() - return exit_info.value.code - - # --------------------------------------------------------------------------- # Denied cases: commit --amend # --------------------------------------------------------------------------- @@ -252,28 +101,20 @@ def test_denies_full_path_git_push_force(self, tmp_path): assert _is_denied(out) def test_denies_push_force_with_unresolved_global_flag(self, tmp_path: Path) -> None: - """Defense regression for _contains_blocked_git_op's - - short-circuit (git_ops_guard.py:206). An unrecognized git global - flag (`--attr-source` -- a genuine git option not yet enumerated in - _GIT_GLOBAL_FLAG_SPEC) before `push --force` makes - extract_git_subcommand_and_flags return ("", ...). The - short-circuit at git_ops_guard.py:206 then must deny unconditionally - via the fail-closed sentinel; without it, the sentinel would not - match any literal entry in _BLOCKED_GIT_OPS and the destructive - `push --force` would slip through undetected. + """An unrecognized git global flag (`--frobnicate`) before `push --force` + makes extract_git_subcommand_and_flags return ("", ...): + the flag's value cannot be told from the subcommand, so the invocation + is denied fail-closed rather than read as a no-op that matches nothing + in _BLOCKED_GIT_OPS. The checked-out-ref preflight, which runs first + in a kitchen-open session, issues the deny with an unresolved value. """ out = _run_guard( - "git --attr-source foo push --force", + "git --frobnicate push --force", kitchen_open=True, tmpdir=tmp_path, ) - assert _is_denied(out) - data = json.loads(out) - reason = data["hookSpecificOutput"]["permissionDecisionReason"] - # Distinguish from the checked-out-ref denial (TestCheckedOutRef*) - # and from a no-op exit: this path uses GIT_OPS_DENY_TRIGGER. - assert "destructive" in reason.lower() or "blocked" in reason.lower() + assert _guard_verdict(out) == "checked-out-ref" + assert _checked_out_ref_result(out)["attempted_value"] == "" # --------------------------------------------------------------------------- @@ -353,15 +194,12 @@ def test_allows_git_commit_with_message(self, tmp_path): assert out.strip() == "" def test_allows_git_push_to_remote(self, tmp_path): - """Regression for #4937: `_same_repository` previously treated a - configured remote name as a filesystem path, so any push to a named - remote -- even a genuine network remote -- resolved to `None` - ("uncertain") and was denied via `_all_threatened`. Requires a real - repository topology: the original version of this test passed a - bare, non-git-inited tmp_path, which made `_repository_context` - return None and short-circuit the preflight before it ever reached - `_classify_git_segment`/`_classify_push`, so it never actually - exercised push classification. + """A push to a configured remote name is judged by the remote's URL, not + by treating the name as a filesystem path: a genuine network remote is a + different repository, so the push is allowed rather than denied as an + uncertain destination. Requires a real repository topology so that the + push reaches `_classify_push`; outside any repository the preflight has + nothing to classify. """ repo = tmp_path / "repo" repo.mkdir() @@ -387,12 +225,14 @@ def test_allows_when_kitchen_closed(self, tmp_path): out = _run_guard("git commit --amend", kitchen_open=False, tmpdir=tmp_path) assert out.strip() == "" - def test_npm_read_only_and_script_policy(self, tmp_path): + def test_npm_commands_naming_neither_git_nor_ref_storage_are_allowed(self, tmp_path): + """A command that names neither Git nor ref storage cannot reach a ref, + so npm version queries and script runs are allowed.""" out = _run_guard("npm --version", kitchen_open=True, tmpdir=tmp_path) assert out.strip() == "" out = _run_guard("npm run build", kitchen_open=True, tmpdir=tmp_path) - assert _checked_out_ref_result(out)["attempted_value"] == "" + assert out.strip() == "" # --------------------------------------------------------------------------- @@ -557,10 +397,8 @@ def test_denies_python_heredoc_update_ref_delete_preflight(self, tmp_path) -> No def test_denies_raw_write_verb_via_heredoc_preflight( self, tmp_path, include_execution_cwd: bool ) -> None: - """Pins the _raw_target_mutations segment-source change and the - no-execution_cwd early-return path (2.3): a raw-write verb (`rm`) - delivered through a heredoc must deny with or without a resolvable - execution_cwd.""" + """A raw-write verb (`rm`) delivered through a heredoc against a ref + file must deny with or without a resolvable execution_cwd.""" _git(tmp_path, "init", "-b", "develop") _git(tmp_path, "config", "user.name", "Guard Test") _git(tmp_path, "config", "user.email", "guard@example.invalid") @@ -575,14 +413,18 @@ def test_denies_raw_write_verb_via_heredoc_preflight( ) assert _is_denied(out) - def test_denies_piped_install_heredoc_without_execution_cwd(self, tmp_path) -> None: + def test_allows_piped_install_heredoc_naming_neither_git_nor_ref_storage( + self, tmp_path + ) -> None: + """A piped `install` with relative operands and no execution cwd names no + ref storage, so it cannot reach a ref and is allowed.""" out = _run_guard( "cat <<'EOF' | bash\ninstall source destination\nEOF", kitchen_open=True, tmpdir=tmp_path, include_execution_cwd=False, ) - assert _checked_out_ref_result(out)["execution_cwd"] == "" + assert out.strip() == "" def test_allows_inert_backtick_heredoc_git_push(self, tmp_path) -> None: """cat-redirect-heredoc-inline-backtick: an inert `cat` body mentioning @@ -1092,7 +934,7 @@ class TestCheckedOutRefAmbiguity: "git fetch --stdin origin", "git push . HEAD:$TARGET", "git push nonexistent-remote-name HEAD:develop", - 'printf x | tee "$TARGET"', + 'printf x | tee "$TARGET/refs/heads/x"', ], ) def test_recognized_ambiguous_mutations_fail_closed_for_all_owners( @@ -1138,27 +980,22 @@ def test_literal_asterisk_in_value_is_recognized_as_dynamic( def test_unresolved_global_flag_before_subcommand_fails_closed_for_all_owners( self, linked_repo: dict[str, Path | str] ) -> None: - # Regression test: extract_git_subcommand_and_flags() must return - # ("", []) -- not silently misparse -- when a real git - # global flag outside the guard's recognized _GIT_GLOBAL_FLAG_SPEC - # appears in space-separated form before the subcommand. - # --attr-source= is a genuine git global option (see - # `git help git`'s OPTIONS section; `git --attr-source foo status` - # runs status normally, proving git itself consumes "foo" as the - # flag's value, not a subcommand) that the guard's flag spec does - # not recognize. The prior parser advanced its cursor by exactly - # one token for any unrecognized `-`-prefixed token, so the flag's - # *value* token ("foo") was misread as the subcommand itself: `git - # --attr-source foo branch -f develop start` classified - # subcommand="foo", which matched none of _classify_git_segment's - # dispatch branches, so the real `branch -f` mutation was never - # classified at all and slipped through undetected. It must - # instead be treated as unresolved/ambiguous, matching the - # $TARGET and asterisk cases above. + # extract_git_subcommand_and_flags() must return ("", []) + # -- not silently misparse -- when a global flag outside the guard's + # recognized _GIT_GLOBAL_FLAG_SPEC appears in space-separated form + # before the subcommand. `--frobnicate` is no Git option, so the guard + # cannot know whether it takes a value. Skipping an unrecognized + # `-`-prefixed token by exactly one would misread the flag's *value* + # token ("foo") as the subcommand itself: `git --frobnicate foo branch + # -f develop start` would classify subcommand="foo", which matches none + # of _classify_git_invocation's dispatch branches, so the real + # `branch -f` mutation would never be classified and would slip + # through undetected. It must instead be treated as unresolved/ambiguous, + # matching the $TARGET and asterisk cases above. linked = linked_repo["linked"] assert isinstance(linked, Path) out = _run_guard( - "git --attr-source foo branch -f develop start", + "git --frobnicate foo branch -f develop start", kitchen_open=True, tmpdir=linked, ) @@ -1193,7 +1030,29 @@ def test_valid_mutation_without_execution_cwd_fails_closed( ], ids=["npm-prefix", "ln-entry"], ) - def test_unresolved_explicit_write_destination_fails_closed( + def test_unresolved_explicit_write_destination_naming_no_ref_storage_is_allowed( + self, + linked_repo: dict[str, Path | str], + monkeypatch: pytest.MonkeyPatch, + command: str, + ) -> None: + monkeypatch.delenv("DESTINATION", raising=False) + linked = linked_repo["linked"] + assert isinstance(linked, Path) + + out = _run_guard(command, kitchen_open=True, tmpdir=linked) + + assert out.strip() == "" + + @pytest.mark.parametrize( + "command", + [ + 'npm install --prefix "$DESTINATION/refs/heads" package', + 'ln -sf /tmp/source "$DESTINATION/.git/HEAD"', + ], + ids=["npm-prefix", "ln-entry"], + ) + def test_unresolved_explicit_write_destination_naming_ref_storage_fails_closed( self, linked_repo: dict[str, Path | str], monkeypatch: pytest.MonkeyPatch, @@ -1209,20 +1068,37 @@ def test_unresolved_explicit_write_destination_fails_closed( assert _checked_out_ref_result(out)["attempted_value"] == "" @pytest.mark.parametrize( - "command_builder", + ("command_builder", "denied"), [ - lambda root: f"npm install --prefix {shlex.quote(str(root / 'npm-root'))} package", - lambda root: f"ln -sf /tmp/source {shlex.quote(str(root / 'link'))}", - lambda root: f"install -t {shlex.quote(str(root / 'install-root'))} /tmp/source", - lambda _root: "cat > f.md <<< 'git push --force origin main'", + ( + lambda root: f"npm install --prefix {shlex.quote(str(root / 'npm-root'))} package", + False, + ), + (lambda root: f"ln -sf /tmp/source {shlex.quote(str(root / 'link'))}", False), + ( + lambda root: f"install -t {shlex.quote(str(root / 'install-root'))} /tmp/source", + False, + ), + (lambda _root: "cat > f.md <<< 'git push --force origin main'", False), + (lambda _root: "cat > refs/heads/x <<< y", True), + ], + ids=[ + "npm-prefix", + "ln-entry", + "install-target-directory", + "relative-redirect", + "relative-redirect-naming-ref-storage", ], - ids=["npm-prefix", "ln-entry", "install-target-directory", "relative-redirect"], ) - def test_structural_write_without_execution_cwd_fails_closed( + def test_structural_write_without_execution_cwd_is_classified( self, linked_repo: dict[str, Path | str], command_builder, + denied: bool, ) -> None: + """Writes to absolute non-ref paths are classified and allowed without an + execution cwd. A relative write has no cwd to resolve against, so it is + ambiguous, and denied, only when the command names ref storage.""" linked = linked_repo["linked"] assert isinstance(linked, Path) @@ -1233,20 +1109,42 @@ def test_structural_write_without_execution_cwd_fails_closed( include_execution_cwd=False, ) - assert _is_denied(out) - assert _checked_out_ref_result(out)["attempted_value"] == "" + if denied: + assert _is_denied(out) + assert _checked_out_ref_result(out)["attempted_value"] == "" + else: + assert out.strip() == "" -def test_link_entry_parent_loop_fails_closed(linked_repo: dict[str, Path | str]) -> None: +def test_link_entry_parent_loop_naming_neither_git_nor_ref_storage_is_allowed( + linked_repo: dict[str, Path | str], tmp_path: Path +) -> None: linked = linked_repo["linked"] assert isinstance(linked, Path) - loop = linked / "loop" + loop = tmp_path / "loop" loop.symlink_to(loop) out = _run_guard( f"ln -s source {shlex.quote(str(loop / 'entry'))}", kitchen_open=True, tmpdir=linked ) + assert out.strip() == "" + + +def test_link_entry_parent_loop_naming_ref_storage_fails_closed( + linked_repo: dict[str, Path | str], tmp_path: Path +) -> None: + linked = linked_repo["linked"] + assert isinstance(linked, Path) + loop = tmp_path / "loop" + loop.symlink_to(loop) + + out = _run_guard( + f"ln -s source {shlex.quote(str(loop / 'refs/heads/entry'))}", + kitchen_open=True, + tmpdir=linked, + ) + assert _is_denied(out) assert _checked_out_ref_result(out)["attempted_value"] == "" @@ -1263,7 +1161,7 @@ class TestCheckedOutRefWriteTargets: "cd - && dd if=/dev/zero of=rel.bin", ], ) - def test_unresolved_write_targets_fail_closed( + def test_unresolved_write_targets_naming_no_ref_storage_are_allowed( self, linked_repo: dict[str, Path | str], monkeypatch: pytest.MonkeyPatch, @@ -1274,6 +1172,27 @@ def test_unresolved_write_targets_fail_closed( linked = linked_repo["linked"] assert isinstance(linked, Path) out = _run_guard(command, kitchen_open=True, tmpdir=linked) + assert out.strip() == "" + + @pytest.mark.parametrize( + "command", + [ + 'echo x > "$G/refs/heads/develop"', + 'cd "$UNSET_DIR" && echo y > refs/heads/x', + "cd - && dd if=/dev/zero of=.git/HEAD", + ], + ) + def test_unresolved_write_targets_naming_ref_storage_fail_closed( + self, + linked_repo: dict[str, Path | str], + monkeypatch: pytest.MonkeyPatch, + command: str, + ) -> None: + monkeypatch.delenv("G", raising=False) + monkeypatch.delenv("UNSET_DIR", raising=False) + linked = linked_repo["linked"] + assert isinstance(linked, Path) + out = _run_guard(command, kitchen_open=True, tmpdir=linked) assert _is_denied(out) assert _checked_out_ref_result(out)["attempted_value"] == "" @@ -1315,8 +1234,8 @@ def test_read_only_git_after_failed_cd_is_allowed( self, linked_repo: dict[str, Path | str], command: str ) -> None: """Read-only git verbs must not be denied just because a preceding - cd failed (cwd resolves to empty). _deny_outer_git_mutations only - escalates when the subcommand is a known ref-mutating one. + cd failed (cwd resolves to empty). The preflight only escalates an + unknown repository when the subcommand is a known ref-mutating one. """ linked = linked_repo["linked"] assert isinstance(linked, Path) @@ -1324,26 +1243,33 @@ def test_read_only_git_after_failed_cd_is_allowed( assert out.strip() == "" @pytest.mark.parametrize( - "command", + ("command", "verdict"), [ - "cd - && git push origin develop", - "cd - && git reset --hard", - "cd - && git checkout .", + ("cd - && git push origin develop", "allow"), + ("cd - && git reset --hard", "checked-out-ref"), + ("cd - && git checkout .", "other-deny"), ], ) - def test_mutation_git_after_failed_cd_is_denied( - self, linked_repo: dict[str, Path | str], command: str + def test_git_after_failed_cd_is_denied_only_when_it_rewrites_a_ref_or_is_destructive( + self, linked_repo: dict[str, Path | str], command: str, verdict: str ) -> None: - """Mutation git verbs after a failed cd must escalate to deny — - the segment cannot be routed to a repository with confidence, and - a bare git mutation has no shell-write surface for - scan_write_targets to catch. + """After a failed cd the segment cannot be routed to a repository. + `reset --hard` outside path mode is a ref mutation, so the preflight + denies it fail-closed with an unresolved value. `checkout .` restores + paths and rewrites no ref; the headless destructive-operation phase + denies it. `push origin develop` names a remote, which is not a ref + mutation of this repository, and a push without force is not risky, + so it is allowed. """ linked = linked_repo["linked"] assert isinstance(linked, Path) out = _run_guard(command, kitchen_open=True, tmpdir=linked) - assert _is_denied(out) - assert _checked_out_ref_result(out)["attempted_value"] == "" + assert _guard_verdict(out) == verdict, command + if verdict == "checked-out-ref": + assert _checked_out_ref_result(out)["attempted_value"] == "" + elif verdict == "other-deny": + reason = json.loads(out)["hookSpecificOutput"]["permissionDecisionReason"] + assert reason == git_ops_guard._DENY_REASON_TEMPLATE.format(op="git checkout .") class TestCheckedOutRefAllows: @@ -1667,7 +1593,7 @@ def test_error_in_ref_classification_blocks( def _raise(*_args: object, **_kwargs: object) -> None: raise IndexError("injected classification failure") - monkeypatch.setattr(git_ops_guard, "_classify_git_segment", _raise) + monkeypatch.setattr(git_ops_guard, "_classify_git_invocation", _raise) code = _guard_exit_code(f"git update-ref refs/heads/x {linked_repo['new_sha']}", linked) @@ -1749,14 +1675,15 @@ def test_every_git_fetch_spec_flag_is_recognized(name: str) -> None: def test_git_ops_guard_classify_imports_from_new_sibling() -> None: - """Step 9 (#4733) regression: every classification primitive the orchestrator - consumes from autoskillit.hooks._runtime._git_command_classification must remain - reachable from the package import surface. Mirrors the orchestrator's import - block at src/autoskillit/hooks/guards/git_ops_guard.py:37. The fetch option - table the classifier reads lives in _git_option_tables as OptionDef rows. + """Every classification primitive the orchestrator consumes from + autoskillit.hooks._runtime._git_command_classification must remain + reachable from the package import surface, as must the remote-identity + helper `_same_repository` resolves through. Mirrors the orchestrator's + import block in git_ops_guard.py. The fetch option table the classifier + reads lives in _git_option_tables as OptionDef rows. """ from autoskillit.hooks._runtime._git_command_classification import ( - _classify_git_segment, + _classify_git_invocation, _contains_blocked_git_op, _git_result, _git_text, @@ -1766,7 +1693,7 @@ def test_git_ops_guard_classify_imports_from_new_sibling() -> None: ) expected_callables = ( - _classify_git_segment, + _classify_git_invocation, _contains_blocked_git_op, _git_result, _git_text, diff --git a/tests/infra/test_git_ops_guard_attribution.py b/tests/infra/test_git_ops_guard_attribution.py new file mode 100644 index 0000000000..13f77f4a49 --- /dev/null +++ b/tests/infra/test_git_ops_guard_attribution.py @@ -0,0 +1,320 @@ +"""Effective-target attribution matrix for git_ops_guard's checked-out-ref preflight. + +Members: TestEffectiveTargetAttribution. + +Each row names the directory a Bash payload starts in and a command whose effective +Git target differs from that directory, or whose target the guard cannot attribute. +A denial must carry ``CHECKED_OUT_REF_DENY_PREFIX``; an allowance must produce no +output at all. ````, ````, ```` and ```` in a command +expand to the fixture repositories and a dangling commit. +""" + +from __future__ import annotations + +import os +from pathlib import Path + +import pytest + +from tests.infra._git_ops_guard_harness import ( + _git, + _guard_verdict, + _run_guard, + build_linked_repo, +) + +pytestmark = [pytest.mark.layer("infra"), pytest.mark.medium] + +_M1 = "git update-ref refs/heads/develop " +_M2 = "git checkout -Bdevelop " + +_SESSION_SHAPES = ( + (True, "skill"), + (True, "orchestrator"), + (False, "orchestrator"), + (False, "skill"), +) + +_PYTHON_UNRESOLVED_SUBPROCESS = ( + "python3 - <<'EOF'\n" + "import subprocess\n" + 'cmd = "git update-ref refs/heads/develop ".split()\n' + "subprocess.run(cmd)\n" + "EOF" +) +_PYTHON_VARIABLE_PATH_WRITE = ( + "python3 - <<'EOF'\n" + "import pathlib\n" + 'path = pathlib.Path(".autoskillit/temp") / "x.md"\n' + 'path.write_text("x")\n' + "EOF" +) +_PYTHON_VARIABLE_PATH_WRITE_MENTIONING_GIT = _PYTHON_VARIABLE_PATH_WRITE.replace( + "import pathlib\n", "import pathlib\n# git-tracked state stays untouched\n" +) + +_DENIED_FROM_CWD = [ + # Layout overrides and -C/env -C retarget the command away from the payload cwd. + ("unrelated", f"GIT_DIR=/.git {_M1}"), + ("unrelated", f"export GIT_DIR=/.git; {_M1}"), + ("unrelated", "git --git-dir=/.git update-ref refs/heads/develop "), + ("unrelated", "git --bare update-ref refs/heads/develop "), + ("unrelated", f"env -C {_M1}"), + # The shell's working directory at the Git segment is unknown or differs from the payload's. + ("unrelated", f"cd && cd /nonexistent; {_M1}"), + ("unrelated", f"P=; cd $P && {_M1}"), + ("unrelated", f"pushd ; popd; {_M1}"), + ("unrelated", f"cd && bash -c 'cd && {_M1}'"), + ("primary", f"bash -c '{_M1}'; cd "), + ("primary", f"false && cd ; {_M1}"), + ("primary", f"if false; then true; cd ; fi; {_M1}"), + ("primary", f"true || cd && {_M1}"), + ("primary", f"cd | {_M1}"), + ("primary-git", _M1), + # An unknown command identity is ambiguous in any repository. + ("unrelated", "P=update-ref; git $P refs/heads/x "), + ("linked", "P=update-ref; git $P refs/heads/develop "), + ("linked", "G=git; $G update-ref refs/heads/develop "), + ("linked", "$(git --exec-path)/git-update-ref refs/heads/develop "), + ("linked", "git --frobnicate -C update-ref refs/heads/develop "), + # Quotes and backslashes inside the command word do not hide it from the prefilter. + ("linked", "g\\it checkout -Bdevelop "), + ("linked", 'gi""t branch -f develop '), + ("linked", "'g'it switch -Cdevelop"), + # Aliases the command word reaches through the command line. + ("linked", "git -c alias.ur=update-ref ur refs/heads/develop "), + ("linked", "git config alias.ur update-ref && git ur refs/heads/develop "), + ( + "linked", + "export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=alias.ur GIT_CONFIG_VALUE_0=update-ref; " + "git ur refs/heads/develop ", + ), + # Raw writes into ref storage, judged against the repository the path belongs to. + ("unrelated", "rm /.git/refs/heads/develop"), + ("linked", "sed -ni s/a/b/ /.git/refs/heads/develop"), + ( + "primary", + "python3 -c 'import os; " + """open(os.path.join(os.environ["G"], "refs/heads/develop"), "w")'""", + ), + ( + "primary", + "python3 -c 'import os,pathlib; " + """pathlib.Path(os.environ["G"], "refs/heads/develop").write_text("x")'""", + ), + ("primary", 'X=.git/refs/heads/develop; rm "$X"'), + ("primary", 'rm -rf "$GIT_DIR"'), + ("primary", "rm -rf $GIT_COMMON_DIR"), + ("primary", 'rm -rf "${GIT_WORK_TREE}"'), + ("primary", _PYTHON_UNRESOLVED_SUBPROCESS), +] + +_ALLOWED_FROM_CWD = [ + # A cd to a variable leaves the working directory unknown; none of these + # commands rewrites a ref. + ("unrelated", "cd $X && git branch -a"), + ("unrelated", "cd $X && git checkout -b f"), + ("unrelated", "cd $X && git lfs pull"), + ("unrelated", 'cd "$X" && git push -u origin HEAD'), + ("unrelated", 'cd "$X" && git fetch origin'), + # A subshell cd does not move the parent shell, and develop is not checked out + # in the unrelated repository. + ("unrelated", "(cd ); git update-ref refs/heads/develop "), + ("unrelated", "git clone d && cd d && git checkout -b f"), + # A bare repository checks no ref out; read-only commands may retarget the primary. + ("bare", "git symbolic-ref HEAD refs/heads/x"), + ("unrelated", "git -C status"), + ("unrelated", "GIT_DIR=/.git git log"), + ("unrelated", "git --no-optional-locks status"), + ("unrelated", "git --list-cmds=main"), + # Writes through variables that name no ref storage. + ("primary", 'rm "$TMPFILE"'), + ("primary", 'cp a "$DEST"'), + ("primary", 'cd "$X" && echo x > out.txt'), + ("primary", _PYTHON_VARIABLE_PATH_WRITE), + ("primary", """python3 -c 'import os; open(os.environ["P"],"w").write("x")'"""), + # The same writes with a `git` word in the text pass the prefilter, so the + # ref-storage rule alone must keep them allowed. + ("primary", _PYTHON_VARIABLE_PATH_WRITE_MENTIONING_GIT), + ("primary", """python3 -c 'import os; open(os.environ["P"],"w").write("x") # git'"""), +] + +# Commands the prefilter lets through without reaching the Git analysis. +_BENIGN_COMMANDS = [ + "ls", + "cat README", + "uv run pytest -q", + "echo hi > /tmp/x", + "python3 -c 'print(1)'", + "make build", + "echo legitimate digit", + "curl https://gitlab.example/api", + "cat .gitignore", + "gitk --all", + 'echo "$preset"', + 'X=$branching; echo "$X"', +] + +_RUN_CMD_UNKNOWN_CWD_DENIED = [_M1, _M2, "git push . HEAD:develop", "git-receive-pack x"] + +_RUN_CMD_UNKNOWN_CWD_ALLOWED = [ + "cd && git update-ref refs/heads/x ", + "git branch -a", + "git checkout -b fresh", + "git status", +] + + +def _expand(command: str, repo: dict[str, Path | str]) -> str: + for name in ("primary", "linked", "unrelated"): + command = command.replace(f"<{name}>", str(repo[name])) + return command.replace("", str(repo["new_sha"])) + + +def _payload_dir(repo: dict[str, Path | str], name: str) -> Path: + if name == "primary-git": + return Path(str(repo["primary"])) / ".git" + return Path(str(repo[name])) + + +def _bash_verdict( + repo: dict[str, Path | str], + tmp_path: Path, + cwd: str, + command: str, + *, + headless: bool = True, + session_type: str | None = None, + extra_env: dict[str, str] | None = None, + tool_input_extra: dict[str, object] | None = None, +) -> str: + """The guard's verdict for a Bash payload whose top-level cwd is ``cwd``. + + The kitchen marker sits at ``tmp_path``, the common ancestor every payload + directory resolves its state root to. + """ + output = _run_guard( + _expand(command, repo), + kitchen_open=True, + tmpdir=tmp_path, + use_bash_key=True, + payload_cwd=_payload_dir(repo, cwd), + headless=headless, + session_type=session_type, + extra_env=extra_env, + tool_input_extra=tool_input_extra, + ) + return _guard_verdict(output) + + +class TestEffectiveTargetAttribution: + """A command is judged by the repository and ref it reaches, whatever directory + the payload starts in; a target the guard cannot attribute is denied only when + the command can mutate a ref. + """ + + @pytest.fixture(autouse=True) + def _no_ambient_git_env(self, monkeypatch: pytest.MonkeyPatch) -> None: + for name in [name for name in os.environ if name.startswith("GIT_")]: + monkeypatch.delenv(name) + + @pytest.fixture + def linked_repo(self, tmp_path: Path) -> dict[str, Path | str]: + return build_linked_repo(tmp_path, tracked_state_dir=False) + + @pytest.mark.parametrize(("cwd", "command"), _DENIED_FROM_CWD) + def test_denies_command_reaching_a_checked_out_ref( + self, linked_repo: dict[str, Path | str], tmp_path: Path, cwd: str, command: str + ) -> None: + assert _bash_verdict(linked_repo, tmp_path, cwd, command) == "checked-out-ref" + + def test_denies_dynamic_command_word_resolved_by_hook_env( + self, linked_repo: dict[str, Path | str], tmp_path: Path + ) -> None: + verdict = _bash_verdict( + linked_repo, tmp_path, "linked", "$G checkout -Bdevelop ", extra_env={"G": "git"} + ) + assert verdict == "checked-out-ref" + + def test_denies_alias_configured_in_the_repository( + self, linked_repo: dict[str, Path | str], tmp_path: Path + ) -> None: + _git(Path(str(linked_repo["primary"])), "config", "alias.ur", "update-ref") + verdict = _bash_verdict(linked_repo, tmp_path, "linked", "git ur refs/heads/develop ") + assert verdict == "checked-out-ref" + + @pytest.mark.parametrize("tool_cwd", [".", ""]) + @pytest.mark.parametrize("command", _RUN_CMD_UNKNOWN_CWD_DENIED) + def test_denies_run_cmd_with_unknown_tool_cwd( + self, linked_repo: dict[str, Path | str], tmp_path: Path, command: str, tool_cwd: str + ) -> None: + output = _run_guard( + _expand(command, linked_repo), kitchen_open=True, tmpdir=tmp_path, tool_cwd=tool_cwd + ) + assert _guard_verdict(output) == "checked-out-ref" + + def test_denies_bash_whose_workdir_names_the_linked_worktree( + self, linked_repo: dict[str, Path | str], tmp_path: Path + ) -> None: + verdict = _bash_verdict( + linked_repo, + tmp_path, + "unrelated", + _M1, + tool_input_extra={"workdir": str(linked_repo["linked"])}, + ) + assert verdict == "checked-out-ref" + + @pytest.mark.parametrize(("cwd", "command"), _ALLOWED_FROM_CWD) + def test_allows_command_that_cannot_reach_a_checked_out_ref( + self, linked_repo: dict[str, Path | str], tmp_path: Path, cwd: str, command: str + ) -> None: + assert _bash_verdict(linked_repo, tmp_path, cwd, command) == "allow" + + @pytest.mark.parametrize("command", _RUN_CMD_UNKNOWN_CWD_ALLOWED) + def test_allows_run_cmd_with_empty_tool_cwd_when_no_ref_is_threatened( + self, linked_repo: dict[str, Path | str], tmp_path: Path, command: str + ) -> None: + output = _run_guard( + _expand(command, linked_repo), kitchen_open=True, tmpdir=tmp_path, tool_cwd="" + ) + assert _guard_verdict(output) == "allow" + + @pytest.mark.parametrize(("headless", "session_type"), _SESSION_SHAPES) + @pytest.mark.parametrize("command", _BENIGN_COMMANDS) + def test_allows_commands_that_exit_at_the_prefilter_in_every_session_shape( + self, + linked_repo: dict[str, Path | str], + tmp_path: Path, + command: str, + headless: bool, + session_type: str, + ) -> None: + verdict = _bash_verdict( + linked_repo, tmp_path, "primary", command, headless=headless, session_type=session_type + ) + assert verdict == "allow" + + @pytest.mark.parametrize( + ("cwd", "command", "verdict"), + [ + ("unrelated", f"GIT_DIR=/.git {_M1}", "checked-out-ref"), + ("primary", f"bash -c '{_M1}'; cd ", "checked-out-ref"), + ("unrelated", "cd $X && git branch -a", "allow"), + ], + ) + def test_verdict_is_the_same_in_every_session_shape( + self, + linked_repo: dict[str, Path | str], + tmp_path: Path, + cwd: str, + command: str, + verdict: str, + ) -> None: + verdicts = { + shape: _bash_verdict( + linked_repo, tmp_path, cwd, command, headless=shape[0], session_type=shape[1] + ) + for shape in _SESSION_SHAPES + } + assert verdicts == dict.fromkeys(_SESSION_SHAPES, verdict) From 5bc50b7f211cccb97d6d5b6a3b533cd6010a9f79 Mon Sep 17 00:00:00 2001 From: Trecek Date: Thu, 5 Nov 2026 14:49:14 -0800 Subject: [PATCH 03/46] Deny cook sessions from moving HEAD of the primary checkout Phase C of the git outcome and effective-target immunity work (#4751). An interactive cook session anchored at the primary checkout could move that checkout off its branch with git checkout -b , because nothing protected the primary's HEAD and the shared-ref preflight only ran while the kitchen marker existed. git_ops_guard now denies a cook-session Git segment whose outcome moves HEAD, or is ambiguous, when its target is the anchor repository's main worktree or cannot be resolved. Path-only forms and linked worktrees are unaffected. Every autoskillit-launched interactive session (cook, order, managed fleet) now runs the shared-ref preflight without depending on the project-wide marker, and every interactive launch carries AUTOSKILLIT_STATE_ROOT so hook state resolves from the project root. The make-groups skill creates its feature branch with git branch so it no longer switches the checkout. Co-Authored-By: Claude Sonnet 5.5 --- docs/safety/hooks.md | 12 +- .../cli/session/_session_launch.py | 14 +- .../types/constants/_type_constants_env.py | 7 +- .../constants/_type_invariant_registry.py | 11 + .../core/types/foundation/_type_enums.py | 6 +- src/autoskillit/execution/backends/claude.py | 6 +- .../hook_registry/_env_contract.py | 6 +- .../hooks/_runtime/_hook_settings.py | 21 + src/autoskillit/hooks/guards/AGENTS.md | 19 + src/autoskillit/hooks/guards/git_ops_guard.py | 171 +++++- .../skills_extended/make-groups/SKILL.md | 2 +- tests/cli/test_cook_interactive.py | 27 +- tests/cli/test_interactive_launch_anchor.py | 206 +++++++ tests/core/test_invariant_registry.py | 1 + tests/hooks/test_git_effect_differential.py | 118 +++- tests/hooks/test_git_primary_head_rule.py | 220 ++++++++ tests/hooks/test_session_shape_parity.py | 54 +- .../infra/test_git_ops_guard_primary_head.py | 513 ++++++++++++++++++ .../test_skill_placeholder_contracts.py | 2 +- 19 files changed, 1365 insertions(+), 51 deletions(-) create mode 100644 tests/cli/test_interactive_launch_anchor.py create mode 100644 tests/hooks/test_git_primary_head_rule.py create mode 100644 tests/infra/test_git_ops_guard_primary_head.py diff --git a/docs/safety/hooks.md b/docs/safety/hooks.md index 3562313f93..2348b162f6 100644 --- a/docs/safety/hooks.md +++ b/docs/safety/hooks.md @@ -117,14 +117,20 @@ caller to use the `prepare_pr → compose_pr` pipeline instead. ### `git_ops_guard.py` **Guarded tool:** `Bash`, `run_cmd` -Blocks destructive raw git CLI operations in headless skill sessions: +Protects checked-out refs in every session and blocks destructive raw git CLI operations +in headless skill sessions. While the kitchen is open, and in every +autoskillit-launched interactive session (cook, order, managed fleet) whether or not it +is, a preflight denies commands that rewrite a branch ref checked out in any worktree of +the repository. In an interactive cook session it also denies moving `HEAD` of the +anchor repository's primary checkout. Destructive operations blocked in headless skill +sessions: `commit --amend`, `push --force` / `-f` / `--force-with-lease`, `reset --hard`, `clean -f` / `-fd`, and `checkout .` / `checkout -- .`. Uses `shlex.split` tokenisation with global-flag skipping so `git -C /path commit --amend` and full-path invocations like `/usr/bin/git push --force` are correctly detected. Also catches interpreter-wrapped (`python3 -c "subprocess.run(['git', 'commit', '--amend'])"`) -and nested-shell forms. Session scope: headless only. Orchestrator sessions -are exempt. Per-subcommand allow-overrides are available via `git_ops_policy` +and nested-shell forms. Session scope of the destructive-operation phase: headless +skill sessions; orchestrator sessions are exempt. Per-subcommand allow-overrides are available via `git_ops_policy` in `.hook_config.json` for future recipes that legitimately need these operations. ### `resource_exhaustion_guard.py` diff --git a/src/autoskillit/cli/session/_session_launch.py b/src/autoskillit/cli/session/_session_launch.py index f7681447a7..70c5b32680 100644 --- a/src/autoskillit/cli/session/_session_launch.py +++ b/src/autoskillit/cli/session/_session_launch.py @@ -142,6 +142,13 @@ class PreparedInteractiveLaunch: pre_spawn_check: Callable[[], None] | None = None +def _interactive_anchor_env( + extra_env: Mapping[str, str] | None, project_dir: Path +) -> dict[str, str]: + """Bind hook state to the orchestrating project root for every interactive launch.""" + return {**(extra_env or {}), AUTOSKILLIT_STATE_ROOT_ENV_VAR: str(project_dir.resolve())} + + def prepare_interactive_launch( backend: CodingAgentBackend, *, @@ -157,6 +164,7 @@ def prepare_interactive_launch( mcp_tool_timeout_sec: float | None = None, ) -> PreparedInteractiveLaunch: """Probe an exact executable before sealing the capability-complete session env.""" + extra_env = _interactive_anchor_env(extra_env, project_dir) probe_env = dict(os.environ) if extra_env is not None and "PATH" in extra_env: @@ -238,6 +246,7 @@ def _finalize_interactive_launch( mcp_tool_timeout_sec: float | None = None, ) -> PreparedInteractiveLaunch: """Build and validate the finalized invocation with its exact executable binding.""" + extra_env = _interactive_anchor_env(extra_env, project_dir) from autoskillit.execution import assert_interactive_ordering, assert_resume_purity if exact_binding_probe_required: @@ -396,11 +405,6 @@ def _run_interactive_session( raise ValueError("managed launch inputs are invalid for a raw session") _project_dir = (project_dir if project_dir is not None else Path.cwd()).resolve() - # Injected so PreToolUse guards can locate `.autoskillit/` state from the - # orchestrating project root even when a command's own cwd points into a - # sibling worktree with its own checked-in `.autoskillit/` — the production - # signal resolve_state_root() (hooks/_hook_payload.py) checks first. - extra_env = {**(extra_env or {}), AUTOSKILLIT_STATE_ROOT_ENV_VAR: str(_project_dir)} tools_arg: tuple[str, ...] = ( ("AskUserQuestion",) if backend.capabilities.skill_injection_capable else () ) diff --git a/src/autoskillit/core/types/constants/_type_constants_env.py b/src/autoskillit/core/types/constants/_type_constants_env.py index c9408f0759..8edd6b98db 100644 --- a/src/autoskillit/core/types/constants/_type_constants_env.py +++ b/src/autoskillit/core/types/constants/_type_constants_env.py @@ -123,9 +123,10 @@ AUTOSKILLIT_APPLICABLE_GUARDS: str = "AUTOSKILLIT_APPLICABLE_GUARDS" AUTOSKILLIT_WRITE_GUARD_TOOL_NAMES: str = "AUTOSKILLIT_WRITE_GUARD_TOOL_NAMES" AUDIT_ADMISSION_AUTHORITY_PATH_ENV_VAR: str = "AUTOSKILLIT_AUDIT_ADMISSION_AUTHORITY_PATH" -# The orchestrating project root, injected by launch preparation so guards can -# locate `.autoskillit/` state in worktree topologies where the hook payload's -# own cwd points into a sibling worktree with its own checked-in `.autoskillit/`. +# The project root for interactive launches and the session cwd for headless ones, +# injected by launch preparation so guards can locate `.autoskillit/` state in +# worktree topologies where the hook payload's own cwd points into a sibling +# worktree with its own checked-in `.autoskillit/`. # Read as a bare string literal by hooks/_hook_payload.py's resolve_state_root # (stdlib-only boundary prevents importing this constant there) — keep the two # string values byte-identical. diff --git a/src/autoskillit/core/types/constants/_type_invariant_registry.py b/src/autoskillit/core/types/constants/_type_invariant_registry.py index 62ca3113c8..52ca0a167f 100644 --- a/src/autoskillit/core/types/constants/_type_invariant_registry.py +++ b/src/autoskillit/core/types/constants/_type_invariant_registry.py @@ -85,6 +85,17 @@ class InvariantDef: enforcement_layer="hook-deny", backends=_BOTH, ), + "git-primary-checkout-head": InvariantDef( + id="git-primary-checkout-head", + prohibition=( + "Using checkout, switch, branch or symbolic-ref to move HEAD of the cook anchor " + "repository's primary checkout is prohibited" + ), + source_doc="AGENTS.md", + gate_target="guards/git_ops_guard.py", + enforcement_layer="hook-deny", + backends=_BOTH, + ), "recipe-read-headless": InvariantDef( id="recipe-read-headless", prohibition="Must not read recipe/skill/agent files directly in headless sessions", diff --git a/src/autoskillit/core/types/foundation/_type_enums.py b/src/autoskillit/core/types/foundation/_type_enums.py index f7302b2ef4..ce91e9cdfc 100644 --- a/src/autoskillit/core/types/foundation/_type_enums.py +++ b/src/autoskillit/core/types/foundation/_type_enums.py @@ -517,8 +517,10 @@ class SessionType(StrEnum): run_skill). L0 subagents are the actual leaf nodes — they never set AUTOSKILLIT_SESSION_TYPE and cannot call run_skill. - Note: interactive L1 sessions (autoskillit cook, bare Claude Code) have no - SessionType value -- they bypass tier checks because AUTOSKILLIT_HEADLESS is unset. + Note: an interactive cook session (autoskillit cook) carries SKILL together with a + launch id and no AUTOSKILLIT_HEADLESS, which is how hooks tell it from a headless + worker. A bare Claude Code session has no SessionType value and bypasses tier + checks because AUTOSKILLIT_HEADLESS is unset. """ FLEET = "fleet" diff --git a/src/autoskillit/execution/backends/claude.py b/src/autoskillit/execution/backends/claude.py index 7b1b63be2a..da449e4355 100644 --- a/src/autoskillit/execution/backends/claude.py +++ b/src/autoskillit/execution/backends/claude.py @@ -415,9 +415,9 @@ def build_interactive_cmd( An interactive session operates at L1 (cook, ``autoskillit cook``) by default. It becomes an L2 orchestrator (order, ``autoskillit order``) when the user calls ``open_kitchen``, granting full kitchen access and the ability to dispatch - L1 ``run_skill`` workers. Unlike headless sessions, the orchestration level of - an interactive session is determined at runtime by kitchen state, not by a - ``SESSION_TYPE`` env variable. + L1 ``run_skill`` workers. The launcher sets ``SESSION_TYPE`` for cook + (``skill``) and order (``orchestrator``), together with a launch id; the + kitchen state, not that variable, decides which tools are visible. """ del generated_home builder = CmdBuilder(str(executable.path) if executable is not None else "claude") diff --git a/src/autoskillit/hook_registry/_env_contract.py b/src/autoskillit/hook_registry/_env_contract.py index a551d9a04b..af87d20339 100644 --- a/src/autoskillit/hook_registry/_env_contract.py +++ b/src/autoskillit/hook_registry/_env_contract.py @@ -220,9 +220,9 @@ def _validate_hook_env_contract(entries: tuple[HookEnvVarDef, ...]) -> None: HookEnvVarDef( "AUTOSKILLIT_STATE_ROOT", "autoskillit", - "autoskillit.cli.session._session_launch:_run_interactive_session", - "autoskillit.cli.session._session_launch:_run_interactive_session", - "Interactive launch binds all hook state to the orchestrating project root.", + "autoskillit.cli.session._session_launch:_interactive_anchor_env", + "autoskillit.cli.session._session_launch:prepare_interactive_launch", + "Every interactive launch (cook, order, fleet) binds hook state to the project root.", ), HookEnvVarDef( "AUTOSKILLIT_WRITE_GUARD_TOOL_NAMES", diff --git a/src/autoskillit/hooks/_runtime/_hook_settings.py b/src/autoskillit/hooks/_runtime/_hook_settings.py index deecfe4b71..bc77a980f9 100644 --- a/src/autoskillit/hooks/_runtime/_hook_settings.py +++ b/src/autoskillit/hooks/_runtime/_hook_settings.py @@ -453,6 +453,27 @@ def hook_session_shape() -> tuple[bool, str]: return headless, tier +def hook_is_launched_interactive() -> bool: + """True for an autoskillit-launched interactive session (cook, order, managed fleet) + and its subagents.""" + headless, _tier = hook_session_shape() + return not headless and bool(os.environ.get(_AUTOSKILLIT_LAUNCH_ID_ENV)) + + +def hook_is_interactive_cook() -> bool: + """True for an autoskillit-launched interactive cook session and its subagents. + + ``hook_session_shape`` maps an empty ``SESSION_TYPE`` to the skill tier, so the + explicit read is what separates cook from a launch that set no session type. + """ + _headless, tier = hook_session_shape() + return ( + hook_is_launched_interactive() + and tier == "skill" + and bool(os.environ.get("AUTOSKILLIT_SESSION_TYPE")) + ) + + def admit_hook_session_scope( session_scope: str, exempt_tiers: frozenset[str], diff --git a/src/autoskillit/hooks/guards/AGENTS.md b/src/autoskillit/hooks/guards/AGENTS.md index a996684628..b6b621ec0e 100644 --- a/src/autoskillit/hooks/guards/AGENTS.md +++ b/src/autoskillit/hooks/guards/AGENTS.md @@ -40,6 +40,24 @@ The package initializer remains import-free. what a command does without changing its text. The guard raises the cost of mistakes and bypasses for Git ref and `HEAD` mutations; it is not a security boundary against a hostile shell. + A cook session may not move HEAD of its anchor repository's primary checkout: in an + interactive cook session (`AUTOSKILLIT_SESSION_TYPE=skill` with a launch id and no + `AUTOSKILLIT_HEADLESS`), a Git segment whose outcome moves `HEAD`, or whose effect is + ambiguous, is denied (`Primary checkout HEAD move blocked: ...`) when its target is the + main worktree of the anchor repository, or is unresolved. The rule is + outcome-based and marker-independent: every autoskillit-launched interactive session + (cook, order, managed fleet) runs the shared-ref preflight without the kitchen marker, + and `AUTOSKILLIT_STATE_ROOT` is set for every interactive launch. Linked worktrees and + other repositories are unaffected. Limits: a linked or bare layout protects its main + worktree; `gh pr checkout`, `rebase`, `bisect` and `pull` are outside the rule; + `merge`, `cherry-pick`, `revert`, `am`, `commit` and `reset` advance the current branch + without switching branches and are outside it too; `git worktree add` leaves the + primary's HEAD alone (Git refuses `worktree add -B ` for a branch checked out + elsewhere); a bare `checkout X` is denied as ambiguous, while `git checkout -- ` + and `git restore ` always pass; a same-command clone inside the primary is judged + by its ancestor, so clone in a separate command first. Worked example: a recipe step + that runs `git checkout ` in the cook session's own checkout (the `checkout` + step of `full-audit`) is denied by design; point `workspace` at a linked worktree. In headless skill sessions, the later legacy Git phase blocks `commit --amend`, `push --force`, `reset --hard`, `clean -f`, and `checkout .`. Raw GitHub review publication is fail-closed across Bash and `run_cmd`: the guard denies @@ -105,6 +123,7 @@ defense-in-depth measure against privilege escalation: | `exploration_request_identity_guard.py` | A supported exploration event lacks bounded native identity or its one-shot record cannot be written | A Claude exploration call must not execute without request-correlated authority | | `join_stop_guard.py` | Malformed/non-object payload or missing `session_id` | Stop must not release when the guard cannot identify the binding whose wave completion authorizes release | | `git_ops_guard.py` | Any unexpected runtime error (any `Exception`) while classifying a Git command, in the checked-out-ref preflight or the headless destructive-op phase; or an unresolved target (unknown cwd, layout override or unparseable global option) for a ref mutation; or, in the separate headless destructive-op-blocking preflight, an unrecognized global git flag that leaves the real subcommand unresolved, or a Python subprocess/os call whose argument could not be resolved to a literal argv or string while `live_command_text` still mentions "git" | An unhandled exception must not silently allow a checked-out ref mutation — use exit 2 + stderr to hard-block. `_git_command_classification._contains_blocked_git_op` reads the command exclusively through `all_evaluated_segments`, the evaluated-segment blocklist authority (rectify #4941 Part A): it cannot match `_BLOCKED_GIT_OPS`'s literal subcommand tuples against an unresolved subcommand, so it denies unconditionally the moment `extract_git_subcommand_and_flags` reports `""`, and separately denies when an unresolved interpreter payload cannot rule out a hidden `git` invocation, rather than silently falling through to "not blocked" | +| `git_ops_guard.py` | In an interactive cook session, the anchor repository cannot be resolved, or a Git command that could move HEAD has an unresolved target | A command that might move the primary checkout's HEAD must not pass on a target the guard cannot prove; the deny names a literal `git -C ` or `cd &&` remedy | | `pr_create_guard.py` | Hook config unreadable or malformed while the kitchen is open (OSError, JSONDecodeError, AttributeError, TypeError) | An unresolvable `recipe_allows_pr_create` authorization must not be read as permission to bypass the prepare_pr → compose_pr pipeline | | `unsafe_install_guard.py` | An unrecognized global pip flag leaves `pip`'s `install` token position unresolved | `_find_pip_install` cannot tell whether the command is a pip install at all; treating that the same as "definitely not an install" would silently skip the editable/system-install checks entirely, so it is threaded through as a distinct `"unresolved-pip-flags"` kind and denied unconditionally, matching the pre-existing `"unresolved-subprocess"` kind's treatment | | `write_guard.py` | A write-verb operand or redirect target cannot be resolved, or a Bash command cannot be parsed, while a write-prefix policy applies | An unresolved target could escape the allowed prefix; the denial explains how to substitute a literal path. | diff --git a/src/autoskillit/hooks/guards/git_ops_guard.py b/src/autoskillit/hooks/guards/git_ops_guard.py index 9238d47939..69ee992c92 100644 --- a/src/autoskillit/hooks/guards/git_ops_guard.py +++ b/src/autoskillit/hooks/guards/git_ops_guard.py @@ -14,6 +14,7 @@ import os import re import sys +from collections.abc import Callable from pathlib import Path from typing import Any @@ -47,7 +48,7 @@ _resolve_attempted_sha, ) from _git_option_tables import _SED_FLAG_SPEC # noqa: E402 -from _git_outcome import is_ref_mutation # noqa: E402 +from _git_outcome import GitHeadEffect, is_ref_mutation # noqa: E402 from _git_target import ( # noqa: E402 _GIT_BUILTIN_SUBCOMMANDS, _GIT_MUTATION_SUBCOMMANDS, @@ -76,6 +77,8 @@ resolve_state_root, ) from _hook_settings import ( # noqa: E402 + hook_is_interactive_cook, + hook_is_launched_interactive, hook_session_shape, read_merged_hook_config, ) @@ -84,6 +87,7 @@ GIT_OPS_DENY_TRIGGER: str = DENY_TRIGGER_BY_GUARD["git_ops_guard"] CHECKED_OUT_REF_DENY_PREFIX: str = "Checked-out ref mutation blocked: " +PRIMARY_HEAD_DENY_PREFIX: str = "Primary checkout HEAD move blocked: " _LOGGER = logging.getLogger(__name__) # noqa: TID251 - standalone stdlib guard _DENY_REASON_TEMPLATE = DENY_REASON_BY_GUARD["git_ops_guard"] @@ -380,6 +384,117 @@ def _preflight_raw_writes( _deny_for_mutations(data, _context_for(target, cache), [mutation]) +def _lazy_anchor(project_root: Path, cache: dict[Any, Any]) -> Callable[[], str | None]: + """The common Git directory of the session's project repository, resolved on first use. + + A repository the anchor cannot resolve raises, which blocks only the command + that needed it. Drift is harmless: a linked worktree's state root shares the + primary's common directory. + """ + + def anchor() -> str | None: + target = resolve_git_target_for_path(str(project_root), cache) + if target.kind == GitTargetKind.UNRESOLVED: + raise RuntimeError(f"cook anchor repository unresolved: {target.reason}") + return target.common_git_dir if target.kind == GitTargetKind.RESOLVED else None + + return anchor + + +def _effective_head_effect(facts: GitSegmentFacts) -> GitHeadEffect: + if facts.identity_unknown: + return GitHeadEffect.AMBIGUOUS + if facts.outcome is None: + return GitHeadEffect.NONE + if facts.outcome.rewrites_current_branch: + return GitHeadEffect.MOVES + return facts.outcome.head_effect + + +def _primary_head_violation( + facts: GitSegmentFacts, anchor: Callable[[], str | None] +) -> str | None: + """Why *facts* would move HEAD of the anchor repository's primary checkout, if it would.""" + effect = _effective_head_effect(facts) + if effect in (GitHeadEffect.NONE, GitHeadEffect.PATH_RESTORE): + return None + target = facts.target + if target.kind == GitTargetKind.UNRESOLVED: + return "unresolved_target" + if target.kind != GitTargetKind.RESOLVED or not target.is_main_worktree: + return None + protected = anchor() + if protected is None or protected != target.common_git_dir: + return None + return "moves_primary_head" if effect == GitHeadEffect.MOVES else "ambiguous_head_effect" + + +_PRIMARY_HEAD_CLONE_NOTE = ( + "A worktree or clone created by the same command is judged by its nearest existing " + "ancestor, so create it in a separate command first." +) +_PRIMARY_HEAD_REMEDY = { + "moves_primary_head": ( + "A cook session must leave HEAD of its primary checkout alone. Run this in a linked " + "worktree: `cd && ` or `git -C `. " + + _PRIMARY_HEAD_CLONE_NOTE + ), + "ambiguous_head_effect": ( + "This could switch the primary checkout's branch, so it is denied. To restore paths use " + "`git checkout -- ` or `git restore `; to switch branches or check out a " + "commit use a linked worktree: `git -C switch ` or " + "`git -C switch --detach `. " + _PRIMARY_HEAD_CLONE_NOTE + ), + "unresolved_target": ( + "The repository this command acts on could not be proven. Name it with a literal " + "`git -C ...` or `cd && ...`. " + _PRIMARY_HEAD_CLONE_NOTE + ), +} + + +def _deny_primary_head( + *, + data: dict[str, object], + cwd: str, + subcommand: str, + head_effect: str, + reason: str, + protected_primary: str, +) -> None: + details = { + "execution_cwd": cwd, + "head_effect": head_effect, + "protected_primary": protected_primary, + "reason": reason, + "remedy": _PRIMARY_HEAD_REMEDY[reason], + "session_id": str(data.get("session_id", "")), + "subcommand": subcommand, + } + deny_tool_use( + PRIMARY_HEAD_DENY_PREFIX + json.dumps(details, sort_keys=True, separators=(",", ":")) + ) + + +def _enforce_primary_head( + data: dict[str, object], + seg: WalkedSegment, + facts: GitSegmentFacts, + anchor: Callable[[], str | None], +) -> None: + reason = _primary_head_violation(facts, anchor) + if reason is None: + return + target = facts.target + _deny_primary_head( + data=data, + cwd=target.cwd or seg.verb_cwd, + subcommand=facts.invocation.subcommand, + head_effect=str(_effective_head_effect(facts)), + reason=reason, + protected_primary=(target.toplevel or target.cwd) if reason != "unresolved_target" else "", + ) + + def _decide_shared_ref( data: dict[str, object], facts: GitSegmentFacts, cache: dict[Any, Any], cwd: str ) -> None: @@ -404,7 +519,11 @@ def _decide_shared_ref( def _preflight_segment( - data: dict[str, object], seg: WalkedSegment, flags: ChannelFlags, cache: dict[Any, Any] + data: dict[str, object], + seg: WalkedSegment, + flags: ChannelFlags, + cache: dict[Any, Any], + anchor: Callable[[], str | None] | None, ) -> None: if seg.position_unresolved and _mentions_git_mutation(" ".join(seg.executable)): _deny_unresolved(data) @@ -416,13 +535,19 @@ def _preflight_segment( facts = analyze_git_segment(seg, flags, cache) if facts is None: return + if anchor is not None: + _enforce_primary_head(data, seg, facts, anchor) if facts.invocation.subcommand in _READ_ONLY_SUBCOMMANDS and not facts.identity_unknown: return _decide_shared_ref(data, facts, cache, seg.verb_cwd if seg.cwd_certain else "") def _preflight_checked_out_ref_mutation( - data: dict[str, object], command: str, execution_cwd: str, cache: dict[Any, Any] + data: dict[str, object], + command: str, + execution_cwd: str, + cache: dict[Any, Any], + anchor: Callable[[], str | None] | None, ) -> None: # The live-text projection (rectify #4941 Part A): heredoc and herestring # bodies are blanked at their source spans and appended once when their @@ -437,7 +562,7 @@ def _preflight_checked_out_ref_mutation( return flags = command_channel_flags(live_text) for seg in walk: - _preflight_segment(data, seg, flags, cache) + _preflight_segment(data, seg, flags, cache, anchor) scan = scan_write_targets(live_text, execution_cwd) _preflight_raw_writes(data, live_text, walk, scan, execution_cwd, cache) if extract_interpreter_command_payloads(command)[1] and _mentions_git_mutation(live_text): @@ -501,35 +626,29 @@ def main() -> None: # handler and serves as the explicit verdict signal. try: project_root = resolve_state_root(parsed.payload_cwd) + # The marker is written by open_kitchen and removed by close_kitchen, but it + # is project-wide: a launched interactive session is checked without it. kitchen_open = _kitchen_marker_exists(project_root) + cache: dict[Any, Any] = {} - if kitchen_open: - _preflight_checked_out_ref_mutation(data, cmd, parsed.execution_cwd, {}) + if kitchen_open or hook_is_launched_interactive(): + anchor = _lazy_anchor(project_root, cache) if hook_is_interactive_cook() else None + _preflight_checked_out_ref_mutation(data, cmd, parsed.execution_cwd, cache, anchor) headless, tier = hook_session_shape() - if not headless: - sys.exit(0) - - blocked = _contains_blocked_git_op(cmd, _BLOCKED_GIT_OPS) - if blocked is None: - sys.exit(0) - + # Orchestrator-typed headless sessions bypass the destructive-op deny but + # remain subject to the all-session ref preflight above. skill_name = os.environ.get("AUTOSKILLIT_SKILL_NAME", "") - if skill_name in _EXEMPT_SKILLS: + if ( + not headless + or not kitchen_open + or skill_name in _EXEMPT_SKILLS + or tier in _DESTRUCTIVE_OP_EXEMPT_TIERS + ): sys.exit(0) - # Orchestrator-typed headless sessions bypass the destructive-op deny - # but remain subject to the all-session ref preflight above (which runs - # unconditionally before this branch). - if tier in _DESTRUCTIVE_OP_EXEMPT_TIERS: - sys.exit(0) - - # Hook config file is written by open_kitchen and removed by close_kitchen. - # Its presence reliably signals an open kitchen without needing session ID. - if not kitchen_open: - sys.exit(0) - - if _git_op_policy_allows(project_root, blocked): + blocked = _contains_blocked_git_op(cmd, _BLOCKED_GIT_OPS) + if blocked is None or _git_op_policy_allows(project_root, blocked): sys.exit(0) op_str = " ".join(("git",) + blocked) diff --git a/src/autoskillit/skills_extended/make-groups/SKILL.md b/src/autoskillit/skills_extended/make-groups/SKILL.md index 924bb517b4..42bc95ff08 100644 --- a/src/autoskillit/skills_extended/make-groups/SKILL.md +++ b/src/autoskillit/skills_extended/make-groups/SKILL.md @@ -299,7 +299,7 @@ should not land on the base branch until the full set is audited. After `make-groups` completes, create a feature branch before starting the pipeline: ```bash -git switch -c feature/{topic} +git branch feature/{topic} ``` Then run each group through the pipeline using the feature branch as `base_branch` for all diff --git a/tests/cli/test_cook_interactive.py b/tests/cli/test_cook_interactive.py index aa56d10fb4..651a04ac5c 100644 --- a/tests/cli/test_cook_interactive.py +++ b/tests/cli/test_cook_interactive.py @@ -9,7 +9,7 @@ from contextlib import contextmanager from pathlib import Path from types import SimpleNamespace -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch import pytest @@ -21,6 +21,7 @@ import autoskillit.cli.session._session_reload as _patch_session__session_reload import autoskillit.cli.ui._timed_input as _patch_ui__timed_input from autoskillit import cli +from autoskillit.cli.session._session_launch import _order_launch_env from autoskillit.core import ( CODEX_MODEL_ALIASES, CODEX_RESERVED_HOME_ENV_VARS, @@ -44,6 +45,10 @@ ValidatedAddDir, atomic_write, ) +from autoskillit.hooks._runtime._hook_settings import ( + hook_is_interactive_cook, + hook_is_launched_interactive, +) from tests._realistic_project import PINNED_CLAUDE_SHIM_VERSION_OUTPUT from tests.cli._cook_launch_helpers import cook_attempt_result from tests.cli._interactive_process import interactive_launch_metadata @@ -624,6 +629,26 @@ def assert_binding_open() -> None: assert binding.closed +def test_cook_launch_env_is_a_launched_interactive_cook_to_hooks( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path +) -> None: + captured = _install_harness(monkeypatch, tmp_path) + + cli.cook(backend=_Backend()) + + spec = captured["spec"] + assert isinstance(spec, CmdSpec) + with patch.dict("os.environ", spec.env, clear=True): + assert hook_is_interactive_cook() is True + assert hook_is_launched_interactive() is True + + +def test_order_launch_env_is_launched_interactive_but_not_cook_to_hooks() -> None: + with patch.dict("os.environ", _order_launch_env("0123456789abcdef"), clear=True): + assert hook_is_interactive_cook() is False + assert hook_is_launched_interactive() is True + + def test_cook_retains_projection_binding_when_launch_consumes_no_artifact( monkeypatch: pytest.MonkeyPatch, tmp_path: Path ) -> None: diff --git a/tests/cli/test_interactive_launch_anchor.py b/tests/cli/test_interactive_launch_anchor.py new file mode 100644 index 0000000000..7012027035 --- /dev/null +++ b/tests/cli/test_interactive_launch_anchor.py @@ -0,0 +1,206 @@ +"""Interactive launchers (cook on Claude and Codex, order, fleet) anchor hook state. + +Each launcher hands its child ``AUTOSKILLIT_STATE_ROOT`` set to the resolved +project root. The cook launchers also carry a launch id the daemon reaper's +``_LAUNCH_ID_RE`` accepts, so both variables of the environ join that +``_candidate_for_pid`` requires are present. +""" + +from __future__ import annotations + +import json +import shutil +from collections.abc import Callable, Iterator, Mapping +from contextlib import contextmanager +from pathlib import Path +from typing import NamedTuple + +import pytest + +import autoskillit.cli._preview as _patch_cli__preview +import autoskillit.cli.session._session_cook as _patch_session__session_cook +import autoskillit.cli.session._session_onboarding as _patch_session__session_onboarding +import autoskillit.cli.session._session_order as _patch_session__session_order +import autoskillit.cli.session._session_process as _patch_session__session_process +import autoskillit.cli.session._session_reload as _patch_session__session_reload +import autoskillit.cli.ui._timed_input as _patch_ui__timed_input +from autoskillit import cli +from autoskillit.cli.fleet import fleet_campaign +from autoskillit.core import ( + AUTOSKILLIT_STATE_ROOT_ENV_VAR, + LAUNCH_ID_ENV_VAR, + CmdSpec, + InteractiveInvocationValidation, + SessionAttemptHandle, + atomic_write, +) +from autoskillit.execution.backends.codex import CodexBackend +from autoskillit.execution.process import _daemon_orphans +from tests.cli._cook_launch_helpers import cook_attempt_result +from tests.cli._fleet_helpers import _capture_subprocess, _stub_campaign_resolution, _stub_guards +from tests.cli._logging_helpers import stub_configure_logging +from tests.cli.conftest import _SCRIPT_YAML +from tests.cli.test_cook_env_scrub import _capture_cook_spec +from tests.execution.backends._codex_fixtures import installed_catalog + +pytestmark = [ + pytest.mark.layer("cli"), + pytest.mark.medium, + pytest.mark.usefixtures("_stub_owner_binding"), +] + +_LaunchedEnv = Callable[[pytest.MonkeyPatch, Path, Path], Mapping[str, str]] + + +def _pin_cook_project_dir(monkeypatch: pytest.MonkeyPatch, project_dir: Path) -> None: + monkeypatch.chdir(project_dir) + monkeypatch.setattr(_patch_session__session_cook, "resolve_project_dir", Path.cwd) + + +def _cook_claude_env( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path, project_dir: Path +) -> Mapping[str, str]: + _pin_cook_project_dir(monkeypatch, project_dir) + return _capture_cook_spec(monkeypatch, tmp_path).env + + +def _cook_codex_env( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path, project_dir: Path +) -> Mapping[str, str]: + source_home = tmp_path / "home" / ".codex" + source_home.mkdir(parents=True) + (source_home / "config.toml").write_text( + 'cli_auth_credentials_store = "keyring"\n', encoding="utf-8" + ) + (source_home / "auth.json").write_text("{}\n", encoding="utf-8") + bundled_catalog = tmp_path / "bundled-models.json" + atomic_write(bundled_catalog, json.dumps(installed_catalog())) + codex_shim = tmp_path / "codex" + atomic_write( + codex_shim, + "#!/usr/bin/env python3\n" + "import pathlib\n" + "import sys\n" + "if sys.argv[1:] != ['debug', 'models', '--bundled']:\n" + " raise SystemExit(64)\n" + f"sys.stdout.buffer.write(pathlib.Path({str(bundled_catalog)!r}).read_bytes())\n", + ) + codex_shim.chmod(0o755) + captured: dict[str, Mapping[str, str]] = {} + + @contextmanager + def session_attempt_context( + _self: CodexBackend, **_kwargs: object + ) -> Iterator[SessionAttemptHandle]: + yield SessionAttemptHandle( + view_id="codex-view", + pass_fds=(), + _record_spawn=lambda _pid, _pgid: None, + _record_reaped=lambda _pid, _pgid: None, + _record_teardown_unproven=lambda _pid, _pgid: None, + ) + + def run_attempt(spec: CmdSpec, **kwargs: object) -> object: + captured["env"] = spec.env + kwargs["on_spawn"](101, 101) # type: ignore[operator] + kwargs["trace"].record_spawn() # type: ignore[union-attr] + kwargs["on_reaped"](101, 101) # type: ignore[operator] + return cook_attempt_result() + + _pin_cook_project_dir(monkeypatch, project_dir) + monkeypatch.setattr(shutil, "which", lambda _name, **_kwargs: str(codex_shim)) + monkeypatch.setattr("sys.stdin.isatty", lambda: True) + monkeypatch.setattr(_patch_session__session_onboarding, "is_first_run", lambda _project: False) + monkeypatch.setattr(_patch_ui__timed_input, "timed_prompt", lambda *args, **kwargs: "") + monkeypatch.setattr("autoskillit.core.write_registry_entry", lambda *_a, **_k: None) + monkeypatch.setattr(_patch_session__session_process, "run_cook_attempt", run_attempt) + monkeypatch.setattr( + _patch_session__session_reload, "consume_reload_sentinel", lambda _project: None + ) + monkeypatch.setattr(CodexBackend, "session_attempt_context", session_attempt_context) + monkeypatch.setattr( + "autoskillit.execution.backends._codex_probes._validate_mcp_probe", + lambda *_args, **_kwargs: [], + ) + monkeypatch.setattr( + CodexBackend, + "validate_interactive_invocation", + lambda _self, _spec: InteractiveInvocationValidation(errors=()), + ) + + cli.cook(backend=CodexBackend(source_codex_home=source_home)) + + return captured["env"] + + +def _order_env( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path, project_dir: Path +) -> Mapping[str, str]: + recipes_dir = project_dir / ".autoskillit" / "recipes" + recipes_dir.mkdir(parents=True) + (recipes_dir / "test-script.yaml").write_text(_SCRIPT_YAML) + monkeypatch.chdir(project_dir) + stub_configure_logging(monkeypatch) + monkeypatch.setattr(_patch_cli__preview, "show_cook_preview", lambda *a, **kw: None) + monkeypatch.setattr( + _patch_session__session_order, "_get_ingredients_table", lambda *a, **kw: "| col | val |" + ) + monkeypatch.setattr("sys.stdin.isatty", lambda: True) + monkeypatch.setattr("builtins.input", lambda _prompt="": "") + monkeypatch.setattr("autoskillit.core.write_registry_entry", lambda *_a, **_k: None) + captured = _capture_subprocess(monkeypatch) + + cli.order("test-script") + + return captured["env"] + + +def _fleet_env( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path, project_dir: Path +) -> Mapping[str, str]: + config_dir = project_dir / ".autoskillit" + config_dir.mkdir() + (config_dir / "config.yaml").write_text("features:\n fleet: true\n") + monkeypatch.chdir(project_dir) + _stub_guards(monkeypatch) + _stub_campaign_resolution(monkeypatch, tmp_path, "test-campaign") + captured = _capture_subprocess(monkeypatch) + + fleet_campaign("test-campaign") + + return captured["env"] + + +class _Launcher(NamedTuple): + launched_env: _LaunchedEnv + stubs_prelaunch: bool + reaper_joinable: bool + + +@pytest.mark.parametrize( + "launcher", + [ + pytest.param(_Launcher(_cook_claude_env, True, True), id="cook-claude"), + # The shared prelaunch stub's Codex readiness double rejects the plugin_dir + # keyword a managed home passes, so this row probes the catalog shim instead. + pytest.param(_Launcher(_cook_codex_env, False, True), id="cook-codex"), + pytest.param(_Launcher(_order_env, True, False), id="order"), + pytest.param(_Launcher(_fleet_env, True, False), id="fleet"), + ], +) +def test_interactive_launch_anchors_state_root_to_project_dir( + request: pytest.FixtureRequest, + monkeypatch: pytest.MonkeyPatch, + tmp_path: Path, + launcher: _Launcher, +) -> None: + if launcher.stubs_prelaunch: + request.getfixturevalue("_stub_interactive_prelaunch") + project_dir = tmp_path / "project" + project_dir.mkdir() + + env = launcher.launched_env(monkeypatch, tmp_path, project_dir) + + assert env.get(AUTOSKILLIT_STATE_ROOT_ENV_VAR) == str(project_dir.resolve()) + if launcher.reaper_joinable: + assert _daemon_orphans._LAUNCH_ID_RE.fullmatch(env.get(LAUNCH_ID_ENV_VAR, "")) diff --git a/tests/core/test_invariant_registry.py b/tests/core/test_invariant_registry.py index c43299c637..835db03fd5 100644 --- a/tests/core/test_invariant_registry.py +++ b/tests/core/test_invariant_registry.py @@ -162,6 +162,7 @@ def test_expected_entry_ids() -> None: "git-reset-hard", "git-clean-f", "git-checked-out-ref-mutation", + "git-primary-checkout-head", "recipe-read-headless", "write-path-prefix", "skill-orchestration-from-L1", diff --git a/tests/hooks/test_git_effect_differential.py b/tests/hooks/test_git_effect_differential.py index c80411359f..6b844be650 100644 --- a/tests/hooks/test_git_effect_differential.py +++ b/tests/hooks/test_git_effect_differential.py @@ -6,7 +6,8 @@ Members: ``_build_base`` and ``_sandbox`` (fixture repositories), ``_run_differential`` (the shared driver returning a ``_Run``), ``_git_cases`` -(argv strategy), and the Property A section (shared refs). +(argv strategy), the Property A section (shared refs) and the Property C +section (HEAD of the primary checkout). """ from __future__ import annotations @@ -32,6 +33,7 @@ ) from _command_classification import ( # type: ignore[import-not-found] extract_git_subcommand_and_flags, + live_command_text, ) from _git_command_classification import ( # type: ignore[import-not-found] _classify_git_invocation, @@ -44,9 +46,12 @@ ) from _git_target import ( # type: ignore[import-not-found] GitTargetKind, + analyze_git_segment, + command_channel_flags, resolve_git_target_for_path, ) -from hypothesis import event, example, given, settings +from _shell_segment_walk import walk_evaluated_segments # type: ignore[import-not-found] +from hypothesis import HealthCheck, event, example, given, settings from hypothesis import strategies as st from autoskillit.hooks.guards import git_ops_guard @@ -870,3 +875,112 @@ def test_classifier_accounts_for_shared_ref_effects( with _sandbox(git_base, tmp_path_factory.mktemp) as sandbox: run = _run_differential(sandbox, subcommand, argv) _assert_shared_refs_sound(run) + + +# --------------------------------------------------------------------------- +# Property C: HEAD of the primary checkout +# --------------------------------------------------------------------------- + +_HEAD_MOVING_SUBCOMMANDS = ("checkout", "switch") + +# Spellings that only restore paths, so the primary keeps what it has checked out. +_PATH_ONLY_CASES: tuple[_Case, ...] = ( + ("checkout", ("--", "file.txt")), + ("checkout", ("other", "--", "file.txt")), + ("checkout", ("other", "file.txt")), + ("checkout", (".",)), + ("checkout", ("--ours", "file.txt")), + ("checkout", ("-p",)), + ("checkout", ("-f",)), +) + +# Spellings that change the primary's HEAD name, attachment or identity. +_HEAD_MOVING_CASES: tuple[_Case, ...] = ( + ("checkout", ("-b", "new-1", _SHA_A)), + ("checkout", ("-Bdevelop", _SHA_A)), + ("checkout", ("--detach",)), + ("checkout", ("--orphan", "new-1")), + ("checkout", ("-",)), + ("checkout", ("@{-1}",)), + ("checkout", ("other",)), + ("checkout", (_SHA_A,)), + ("checkout", ("-t", "origin/y")), + ("switch", ("other",)), + ("switch", ("-c", "new-1")), + ("switch", ("-Cdevelop", "feat")), + ("switch", ("--detach", _SHA_A)), + ("switch", ("-",)), + ("switch", ("y",)), +) + + +def _with_primary_head_examples(test: Callable[..., None]) -> Callable[..., None]: + for case in reversed(_PATH_ONLY_CASES + _HEAD_MOVING_CASES): + test = example(case=case)(test) + return test + + +def _primary_head_verdict(run: _Run, primary: Path) -> str | None: + """The primary-head rule's verdict on the command *run* executed, issued from *primary*.""" + walked = walk_evaluated_segments(run.command, str(primary)) + assert walked is not None, f"`{run.command}` could not be tokenized ({run.git_version})" + flags = command_channel_flags(live_command_text(run.command)) + cache: dict[Any, Any] = {} + analysed = [analyze_git_segment(segment, flags, cache) for segment in walked] + facts = [segment_facts for segment_facts in analysed if segment_facts is not None] + assert len(facts) == 1, f"`{run.command}` classified {len(facts)} Git segments" + anchor = git_ops_guard._lazy_anchor(primary, cache) + verdict: str | None = git_ops_guard._primary_head_violation(facts[0], anchor) + return verdict + + +def _primary_head_change(run: _Run) -> tuple[_View, _View] | None: + for label, before, after in run.head_changes: + if label == "primary": + return before, after + return None + + +def _primary_head_problems(run: _Run, verdict: str | None) -> list[str]: + problems: list[str] = [] + change = _primary_head_change(run) + if change is not None and verdict is None: + problems.append( + f"primary HEAD changed {_describe_head(change[0])} -> {_describe_head(change[1])} " + "but the rule allows it" + ) + if (run.subcommand, run.argv) in _PATH_ONLY_CASES and verdict is not None: + problems.append(f"a path-only spelling is denied as {verdict!r}") + return problems + + +def _primary_head_failure_message(run: _Run, verdict: str | None, problems: list[str]) -> str: + return ( + f"Property C violated by `{run.command}` (exit {run.returncode})\n" + " expected: a checkout or switch that changed the primary's HEAD is denied, and " + "a path-only spelling is allowed\n" + f" actual: {'; '.join(problems)}\n" + f" rule verdict: {verdict!r}\n" + f" outcome: {run.outcome}\n" + f" git stderr: {run.stderr.strip()!r}\n" + f" {run.git_version}" + ) + + +@pytest.mark.timeout(300) +@settings(max_examples=200, deadline=None, suppress_health_check=[HealthCheck.filter_too_much]) +@_with_primary_head_examples +@given(case=_git_cases().filter(lambda case: case[0] in _HEAD_MOVING_SUBCOMMANDS)) +def test_primary_head_rule_denies_every_checkout_and_switch_that_moves_the_primary_head( + git_base: _Base, tmp_path_factory: pytest.TempPathFactory, case: _Case +) -> None: + subcommand, argv = case + with _sandbox(git_base, tmp_path_factory.mktemp) as sandbox: + run = _run_differential(sandbox, subcommand, argv) + verdict = _primary_head_verdict(run, sandbox.primary) + event( + f"git:{run.subcommand}:{'moved' if _primary_head_change(run) else 'kept'}:" + f"{'denied' if verdict else 'allowed'}" + ) + problems = _primary_head_problems(run, verdict) + assert not problems, _primary_head_failure_message(run, verdict, problems) diff --git a/tests/hooks/test_git_primary_head_rule.py b/tests/hooks/test_git_primary_head_rule.py new file mode 100644 index 0000000000..81ee674f0f --- /dev/null +++ b/tests/hooks/test_git_primary_head_rule.py @@ -0,0 +1,220 @@ +"""Unit matrix for the primary-checkout HEAD rule of git_ops_guard. + +Members: TestPrimaryHeadViolation. + +Each row hand-builds the facts of one Git segment, so no repository is touched. +The anchor is the callable that yields the canonical common Git directory of the +session's project repository, or ``None`` when that directory is not a repository. +""" + +from __future__ import annotations + +from collections.abc import Callable + +# The sys.path bootstrap for bare-name hook-sibling imports is centralized in +# tests/conftest.py. +import pytest +from _command_classification import GitInvocation # type: ignore[import-not-found] +from _git_outcome import GitHeadEffect, GitRefOutcome # type: ignore[import-not-found] +from _git_target import ( # type: ignore[import-not-found] + GitSegmentFacts, + GitTarget, + GitTargetKind, +) + +from autoskillit.hooks.guards import git_ops_guard + +pytestmark = [pytest.mark.layer("hooks"), pytest.mark.small] + +_COMMON = "/work/repo/.git" +_OTHER_COMMON = "/work/other/.git" + +_MAIN_WORKTREE = GitTarget( + GitTargetKind.RESOLVED, "", "/work/repo", _COMMON, _COMMON, True, "/work/repo" +) +_LINKED_WORKTREE = GitTarget( + GitTargetKind.RESOLVED, + "", + "/work/linked", + f"{_COMMON}/worktrees/linked", + _COMMON, + False, + "/work/linked", +) +_OTHER_REPOSITORY = GitTarget( + GitTargetKind.RESOLVED, "", "/work/other", _OTHER_COMMON, _OTHER_COMMON, True, "/work/other" +) +_NOT_A_REPOSITORY = GitTarget( + GitTargetKind.NOT_A_REPOSITORY, "", "/work/plain", "", "", False, None +) +_UNRESOLVED = GitTarget(GitTargetKind.UNRESOLVED, "cwd_unknown", "", "", "", False, None) + + +def _facts( + target: GitTarget, + effect: GitHeadEffect | None, + *, + identity_unknown: bool = False, + rewrites_current_branch: bool = False, +) -> GitSegmentFacts: + outcome = ( + None + if effect is None + else GitRefOutcome(None, rewrites_current_branch, "HEAD", False, effect) + ) + invocation = GitInvocation("checkout", [], [], ["git"]) + return GitSegmentFacts(invocation, target, outcome, identity_unknown) + + +def _anchored_at(common: str | None) -> Callable[[], str | None]: + return lambda: common + + +def _never_asked() -> str | None: + raise AssertionError("the anchor must not be resolved for this row") + + +_MOVES = GitHeadEffect.MOVES +_AMBIGUOUS = GitHeadEffect.AMBIGUOUS + + +class TestPrimaryHeadViolation: + @pytest.mark.parametrize( + ("facts", "expected"), + [ + pytest.param(_facts(_MAIN_WORKTREE, _MOVES), "moves_primary_head", id="moves-on-main"), + pytest.param( + _facts(_MAIN_WORKTREE, GitHeadEffect.NONE, rewrites_current_branch=True), + "moves_primary_head", + id="rewrites-current-branch-on-main", + ), + pytest.param( + _facts(_MAIN_WORKTREE, GitHeadEffect.PATH_RESTORE, rewrites_current_branch=True), + "moves_primary_head", + id="rewrites-current-branch-outranks-path-restore", + ), + pytest.param( + _facts(_MAIN_WORKTREE, _AMBIGUOUS), "ambiguous_head_effect", id="ambiguous-on-main" + ), + pytest.param( + _facts(_MAIN_WORKTREE, GitHeadEffect.NONE, identity_unknown=True), + "ambiguous_head_effect", + id="identity-unknown-on-main", + ), + pytest.param( + _facts(_MAIN_WORKTREE, _MOVES, identity_unknown=True), + "ambiguous_head_effect", + id="identity-unknown-outranks-moves-on-main", + ), + pytest.param( + _facts(_MAIN_WORKTREE, None, identity_unknown=True), + "ambiguous_head_effect", + id="identity-unknown-without-outcome-on-main", + ), + pytest.param(_facts(_UNRESOLVED, _MOVES), "unresolved_target", id="moves-unresolved"), + pytest.param( + _facts(_UNRESOLVED, _AMBIGUOUS), "unresolved_target", id="ambiguous-unresolved" + ), + pytest.param( + _facts(_UNRESOLVED, GitHeadEffect.NONE, identity_unknown=True), + "unresolved_target", + id="identity-unknown-unresolved", + ), + pytest.param( + _facts(_UNRESOLVED, GitHeadEffect.NONE, rewrites_current_branch=True), + "unresolved_target", + id="rewrites-current-branch-unresolved", + ), + ], + ) + def test_names_a_violation_of_the_anchors_main_worktree_or_an_unresolved_target( + self, facts: GitSegmentFacts, expected: str + ) -> None: + assert git_ops_guard._primary_head_violation(facts, _anchored_at(_COMMON)) == expected + + @pytest.mark.parametrize( + "facts", + [ + pytest.param(_facts(_LINKED_WORKTREE, _MOVES), id="moves-on-linked"), + pytest.param( + _facts(_LINKED_WORKTREE, GitHeadEffect.NONE, rewrites_current_branch=True), + id="rewrites-current-branch-on-linked", + ), + pytest.param(_facts(_LINKED_WORKTREE, _AMBIGUOUS), id="ambiguous-on-linked"), + pytest.param( + _facts(_LINKED_WORKTREE, GitHeadEffect.NONE, identity_unknown=True), + id="identity-unknown-on-linked", + ), + pytest.param(_facts(_OTHER_REPOSITORY, _MOVES), id="moves-on-another-common-dir"), + pytest.param( + _facts(_OTHER_REPOSITORY, _AMBIGUOUS), id="ambiguous-on-another-common-dir" + ), + pytest.param( + _facts(_OTHER_REPOSITORY, GitHeadEffect.NONE, identity_unknown=True), + id="identity-unknown-on-another-common-dir", + ), + pytest.param(_facts(_NOT_A_REPOSITORY, _MOVES), id="moves-outside-a-repository"), + pytest.param( + _facts(_NOT_A_REPOSITORY, GitHeadEffect.NONE, identity_unknown=True), + id="identity-unknown-outside-a-repository", + ), + ], + ) + def test_ignores_a_head_move_outside_the_anchors_main_worktree( + self, facts: GitSegmentFacts + ) -> None: + assert git_ops_guard._primary_head_violation(facts, _anchored_at(_COMMON)) is None + + @pytest.mark.parametrize( + "facts", + [ + pytest.param(_facts(_MAIN_WORKTREE, GitHeadEffect.PATH_RESTORE), id="path-restore"), + pytest.param(_facts(_MAIN_WORKTREE, GitHeadEffect.NONE), id="none"), + pytest.param(_facts(_MAIN_WORKTREE, None), id="no-outcome"), + pytest.param(_facts(_UNRESOLVED, GitHeadEffect.PATH_RESTORE), id="unresolved-restore"), + pytest.param(_facts(_UNRESOLVED, GitHeadEffect.NONE), id="unresolved-none"), + pytest.param(_facts(_UNRESOLVED, None), id="unresolved-no-outcome"), + ], + ) + def test_ignores_a_command_that_leaves_head_alone_without_resolving_the_anchor( + self, facts: GitSegmentFacts + ) -> None: + assert git_ops_guard._primary_head_violation(facts, _never_asked) is None + + @pytest.mark.parametrize( + "facts", + [ + pytest.param(_facts(_MAIN_WORKTREE, _MOVES), id="moves"), + pytest.param(_facts(_MAIN_WORKTREE, _AMBIGUOUS), id="ambiguous"), + pytest.param( + _facts(_MAIN_WORKTREE, GitHeadEffect.NONE, identity_unknown=True), + id="identity-unknown", + ), + pytest.param( + _facts(_MAIN_WORKTREE, GitHeadEffect.NONE, rewrites_current_branch=True), + id="rewrites-current-branch", + ), + ], + ) + def test_is_inactive_when_the_anchor_is_not_a_repository(self, facts: GitSegmentFacts) -> None: + assert git_ops_guard._primary_head_violation(facts, _anchored_at(None)) is None + + @pytest.mark.parametrize("target", [_LINKED_WORKTREE, _NOT_A_REPOSITORY], ids=lambda t: t.cwd) + def test_does_not_resolve_the_anchor_for_a_target_that_is_no_main_worktree( + self, target: GitTarget + ) -> None: + facts = _facts(target, _MOVES) + assert git_ops_guard._primary_head_violation(facts, _never_asked) is None + + def test_an_unresolvable_anchor_raises_only_for_a_command_that_needs_it(self) -> None: + def failing_anchor() -> str | None: + raise RuntimeError("cook anchor repository unresolved") + + assert ( + git_ops_guard._primary_head_violation( + _facts(_MAIN_WORKTREE, GitHeadEffect.NONE), failing_anchor + ) + is None + ) + with pytest.raises(RuntimeError, match="unresolved"): + git_ops_guard._primary_head_violation(_facts(_MAIN_WORKTREE, _MOVES), failing_anchor) diff --git a/tests/hooks/test_session_shape_parity.py b/tests/hooks/test_session_shape_parity.py index 525edc3c40..6001635c94 100644 --- a/tests/hooks/test_session_shape_parity.py +++ b/tests/hooks/test_session_shape_parity.py @@ -1,8 +1,13 @@ -"""Parity checks between core session scope and hook-runtime shims.""" +"""Parity checks between core session scope and hook-runtime shims. + +Also covers the launched-interactive identity predicates, which read the +``hook_session_shape`` result together with the launch id. +""" from __future__ import annotations import warnings +from unittest.mock import patch import pytest @@ -10,6 +15,8 @@ from autoskillit.hook_registry import HOOK_REGISTRY from autoskillit.hooks._runtime._hook_settings import ( admit_hook_session_scope, + hook_is_interactive_cook, + hook_is_launched_interactive, hook_session_shape, ) @@ -125,3 +132,48 @@ def test_hook_scope_admission_matches_core_scope() -> None: hookdef.exempt_session_types, (shape.headless, shape.tier.value), ) is scope.admits(shape) + + +_LAUNCH_ID = "0123456789abcdef" + + +@pytest.mark.parametrize( + ("headless", "session_type", "launch_id", "interactive_cook", "launched_interactive"), + [ + pytest.param(None, "skill", _LAUNCH_ID, True, True, id="cook-claude"), + pytest.param("", "skill", _LAUNCH_ID, True, True, id="cook-codex"), + pytest.param(None, "SKILL", _LAUNCH_ID, True, True, id="session-type-uppercase"), + pytest.param(None, "orchestrator", _LAUNCH_ID, False, True, id="order"), + pytest.param("0", "fleet", _LAUNCH_ID, False, True, id="managed-fleet"), + pytest.param("0", "fleet", None, False, False, id="raw-fleet"), + pytest.param("1", "skill", _LAUNCH_ID, False, False, id="headless-skill"), + pytest.param("1", "orchestrator", _LAUNCH_ID, False, False, id="headless-orchestrator"), + pytest.param(None, None, None, False, False, id="plain-plugin-session"), + pytest.param(None, None, _LAUNCH_ID, False, True, id="session-type-unset"), + pytest.param(None, "", _LAUNCH_ID, False, True, id="session-type-empty"), + pytest.param(None, "skill", "", False, False, id="launch-id-empty"), + ], +) +def test_launched_interactive_identity_predicates( + headless: str | None, + session_type: str | None, + launch_id: str | None, + interactive_cook: bool, + launched_interactive: bool, +) -> None: + """Each row fixes all three identity variables; None leaves one out of the environment. + + Only ``AUTOSKILLIT_HEADLESS == "1"`` is headless, and an empty or uppercase + ``AUTOSKILLIT_SESSION_TYPE`` normalizes to the skill tier, so cook identity also + needs the explicit non-empty session type. + """ + row = { + "AUTOSKILLIT_HEADLESS": headless, + "AUTOSKILLIT_SESSION_TYPE": session_type, + "AUTOSKILLIT_LAUNCH_ID": launch_id, + } + env = {name: value for name, value in row.items() if value is not None} + + with patch.dict("os.environ", env, clear=True): + assert hook_is_interactive_cook() is interactive_cook + assert hook_is_launched_interactive() is launched_interactive diff --git a/tests/infra/test_git_ops_guard_primary_head.py b/tests/infra/test_git_ops_guard_primary_head.py new file mode 100644 index 0000000000..06a32eaf23 --- /dev/null +++ b/tests/infra/test_git_ops_guard_primary_head.py @@ -0,0 +1,513 @@ +"""Cook primary-checkout HEAD rule of git_ops_guard, against real repositories. + +Members: TestCookPrimaryHeadDenied, TestCookPrimaryHeadAllowed, +TestPrimaryHeadMarkerIndependence, TestPrimaryHeadAnchorFailure, +TestPrimaryHeadOtherSessions. + +A cook session runs with a launch id, no headless flag and the project root as +its state root. Each row names the directory a payload starts in and a command; +a denial must carry ``PRIMARY_HEAD_DENY_PREFIX`` and an allowance must produce +no output at all. The guard denies before Git runs, so no row executes the +command it judges. ````, ````, ````, ```` and +```` in a command expand to the fixture repositories, the test directory +and a dangling commit. +""" + +from __future__ import annotations + +import functools +import io +import json +import os +import shutil +import subprocess +import unittest.mock +from pathlib import Path + +# The sys.path bootstrap for bare-name hook-sibling imports is centralized in +# tests/conftest.py. +import _git_target # type: ignore[import-not-found] +import pytest + +from autoskillit.hooks.guards import git_ops_guard +from tests._git_topology import commit_file +from tests.infra._git_ops_guard_harness import ( + _HOOK_CONFIG_RELPATH, + UNSET, + _build_payload, + _git, + _guard_verdict, + _make_clean_env, + _run_guard, + build_linked_repo, +) + +pytestmark = [ + pytest.mark.layer("infra"), + pytest.mark.medium, + pytest.mark.skipif(shutil.which("git") is None, reason="git binary required"), +] + +_LAUNCH_ID = "0123456789abcdef" +_INCIDENT = "git checkout -b pr-issue-4726-step2 " +_UPDATE_DEVELOP = "git -C update-ref refs/heads/develop " + +# name -> (headless, environment). Launched sessions also carry the state root. +_SESSIONS: dict[str, tuple[bool, dict[str, str]]] = { + "cook": (False, {"AUTOSKILLIT_SESSION_TYPE": "skill", "AUTOSKILLIT_LAUNCH_ID": _LAUNCH_ID}), + "cook-codex": ( + False, + { + "AUTOSKILLIT_HEADLESS": "", + "AUTOSKILLIT_SESSION_TYPE": "skill", + "AUTOSKILLIT_LAUNCH_ID": _LAUNCH_ID, + }, + ), + "order": ( + False, + {"AUTOSKILLIT_SESSION_TYPE": "orchestrator", "AUTOSKILLIT_LAUNCH_ID": _LAUNCH_ID}, + ), + "fleet": ( + False, + { + "AUTOSKILLIT_HEADLESS": "0", + "AUTOSKILLIT_SESSION_TYPE": "fleet", + "AUTOSKILLIT_LAUNCH_ID": _LAUNCH_ID, + }, + ), + "headless-skill": ( + True, + {"AUTOSKILLIT_SESSION_TYPE": "skill", "AUTOSKILLIT_LAUNCH_ID": _LAUNCH_ID}, + ), + "headless-orchestrator": ( + True, + {"AUTOSKILLIT_SESSION_TYPE": "orchestrator", "AUTOSKILLIT_LAUNCH_ID": _LAUNCH_ID}, + ), + "plain": (False, {}), + "headless": (True, {}), +} +_UNLAUNCHED = ("plain", "headless") + +# Commands that move HEAD of the primary checkout, with the rule's reason. +_DENIED_IN_PRIMARY = [ + ("git switch other", "moves_primary_head"), + ("git checkout --detach", "moves_primary_head"), + ("git checkout -", "moves_primary_head"), + ("git switch y", "moves_primary_head"), + ("git checkout -t origin/y", "moves_primary_head"), + ("git checkout --orphan o", "moves_primary_head"), + ("git symbolic-ref HEAD refs/heads/other", "moves_primary_head"), + ("git branch -m renamed", "moves_primary_head"), + ("git co -b x", "moves_primary_head"), + ("git checkout other", "ambiguous_head_effect"), + ("git checkout file.txt", "ambiguous_head_effect"), + ("P=checkout; git $P -b x", "ambiguous_head_effect"), +] + +# A shell whose working directory the guard cannot prove, or a command aimed at +# the primary from elsewhere. +_DENIED_BY_POSITION = [ + ("primary", "if false; then true; cd ; fi; git checkout -b x", "unresolved_target"), + ("primary", "true || cd && git checkout -b x", "unresolved_target"), + ("linked", "GIT_DIR=/.git git checkout -b x", "unresolved_target"), + ("linked", "bash -c 'cd && git checkout -b x'", "moves_primary_head"), +] + +_ALLOWED_PATH_ONLY = [ + "git checkout -- file.txt", + "git checkout other -- file.txt", + "git checkout other file.txt", + "git checkout .", + "git checkout --ours file.txt", + "git checkout -p", + "git checkout -f", + "git restore file.txt", +] +_ALLOWED_CURRENT_BRANCH_WORK = ["git commit -m x", "git merge feat"] + +_ALLOWED_OUTSIDE_THE_PRIMARY = [ + ("primary", "cd && git checkout -b x"), + ("primary", "git -C switch -c x"), + ("primary", "git -C checkout --detach "), + ("primary", "git -C switch --detach "), + ("unrelated", "git checkout -b x"), + ("primary", "git worktree add -b feat2 /wt2"), +] + + +@pytest.fixture(autouse=True) +def _isolated_git_environment(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: + home = tmp_path / "home" + home.mkdir() + for name in ( + "GIT_DIR", + "GIT_WORK_TREE", + "GIT_COMMON_DIR", + "GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", + ): + monkeypatch.delenv(name, raising=False) + monkeypatch.setenv("GIT_CONFIG_GLOBAL", os.devnull) + monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1") + monkeypatch.setenv("HOME", str(home)) + monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config")) + monkeypatch.setenv("XDG_DATA_HOME", str(home / ".local" / "share")) + monkeypatch.setenv("XDG_CACHE_HOME", str(home / ".cache")) + + +@pytest.fixture +def repo(tmp_path: Path) -> dict[str, Path | str]: + """The linked-worktree fixture plus a tracked file, two branches and ``origin/y``.""" + built = build_linked_repo(tmp_path, tracked_state_dir=True) + primary = Path(str(built["primary"])) + commit_file(primary, "file.txt", "a\n", "add file") + _git(primary, "branch", "feat") + _git(primary, "branch", "other") + _git(primary, "remote", "add", "origin", str(built["bare"])) + _git(primary, "push", "-q", "origin", "develop:refs/heads/y") + _git(primary, "fetch", "-q", "origin") + return built + + +@functools.cache +def _git_version() -> str: + return subprocess.run( + ["git", "--version"], capture_output=True, text=True, check=False + ).stdout.strip() + + +def _context(command: str, observed: object = "") -> str: + return f"{command!r} {observed!r} ({_git_version()})" + + +def _expand(command: str, repo: dict[str, Path | str], tmp_path: Path) -> str: + for name in ("primary", "linked", "unrelated"): + command = command.replace(f"<{name}>", str(repo[name])) + return command.replace("", str(tmp_path)).replace("", str(repo["new_sha"])) + + +def _session_env(session: str, repo: dict[str, Path | str], *, state_root: bool) -> dict[str, str]: + env = dict(_SESSIONS[session][1]) + if state_root and session not in _UNLAUNCHED: + env["AUTOSKILLIT_STATE_ROOT"] = str(repo["primary"]) + return env + + +def _guard_output( + repo: dict[str, Path | str], + tmp_path: Path, + command: str, + *, + cwd: str | Path = "primary", + session: str = "cook", + marker: bool = True, + state_root: bool = True, + run_cmd: bool = False, + tool_cwd: object = UNSET, +) -> str: + """The guard's stdout for *command* issued from the directory *cwd*. + + *cwd* is a fixture repository's name or a path. The kitchen marker, when + present, sits only under the primary. A run_cmd payload carries the directory + as its own ``cwd`` unless *tool_cwd* says otherwise. + """ + path = cwd if isinstance(cwd, Path) else Path(str(repo[cwd])) + return _run_guard( + _expand(command, repo, tmp_path), + kitchen_open=marker, + tmpdir=tmp_path, + marker_root=Path(str(repo["primary"])), + headless=_SESSIONS[session][0], + extra_env=_session_env(session, repo, state_root=state_root), + use_bash_key=not run_cmd, + payload_cwd=path, + tool_cwd=(path if tool_cwd is UNSET else tool_cwd) if run_cmd else UNSET, + ) + + +def _verdict(output: str) -> str: + """``allow``, ``primary-head``, ``checked-out-ref`` or ``other-deny``.""" + if output.strip(): + reason = json.loads(output)["hookSpecificOutput"]["permissionDecisionReason"] + if reason.startswith(git_ops_guard.PRIMARY_HEAD_DENY_PREFIX): + return "primary-head" + return _guard_verdict(output) + + +def _primary_head_details(output: str) -> dict[str, str]: + reason = json.loads(output)["hookSpecificOutput"]["permissionDecisionReason"] + prefix = git_ops_guard.PRIMARY_HEAD_DENY_PREFIX + assert reason.startswith(prefix), reason + details: dict[str, str] = json.loads(reason.removeprefix(prefix)) + return details + + +def _assert_verdict(output: str, expected: str, command: str) -> None: + assert _verdict(output) == expected, _context(command, output) + + +def _current_head(primary: Path) -> str: + return _git(primary, "symbolic-ref", "HEAD") + + +class TestCookPrimaryHeadDenied: + """A cook session may not move HEAD of the checkout that holds its project state.""" + + def test_denies_the_incident_and_leaves_the_primary_on_its_branch( + self, repo: dict[str, Path | str], tmp_path: Path + ) -> None: + primary = Path(str(repo["primary"])) + output = _guard_output(repo, tmp_path, _INCIDENT) + + _assert_verdict(output, "primary-head", _INCIDENT) + assert _current_head(primary) == "refs/heads/develop", _context(_INCIDENT) + details = _primary_head_details(output) + context = _context(_INCIDENT, details) + assert details["reason"] == "moves_primary_head", context + assert details["subcommand"] == "checkout", context + assert details["head_effect"] == "moves", context + assert details["protected_primary"] == str(primary), context + assert details["execution_cwd"] == str(primary), context + assert details["session_id"] == "guard-test-session", context + assert "git -C " in details["remedy"], context + assert "cd " in details["remedy"], context + + @pytest.mark.parametrize("session", ["cook", "cook-codex"]) + @pytest.mark.parametrize("run_cmd", [False, True], ids=["bash", "run_cmd"]) + def test_denies_the_incident_in_every_payload_shape( + self, repo: dict[str, Path | str], tmp_path: Path, run_cmd: bool, session: str + ) -> None: + output = _guard_output(repo, tmp_path, _INCIDENT, run_cmd=run_cmd, session=session) + _assert_verdict(output, "primary-head", _INCIDENT) + + @pytest.mark.parametrize(("command", "reason"), _DENIED_IN_PRIMARY) + def test_denies_each_kind_of_head_move_in_the_primary( + self, repo: dict[str, Path | str], tmp_path: Path, command: str, reason: str + ) -> None: + _git(Path(str(repo["primary"])), "config", "alias.co", "checkout") + output = _guard_output(repo, tmp_path, command) + + _assert_verdict(output, "primary-head", command) + assert _primary_head_details(output)["reason"] == reason, _context(command, output) + + @pytest.mark.parametrize("command", ["git checkout other", "git checkout file.txt"]) + def test_names_the_unambiguous_spellings_for_an_ambiguous_checkout( + self, repo: dict[str, Path | str], tmp_path: Path, command: str + ) -> None: + remedy = _primary_head_details(_guard_output(repo, tmp_path, command))["remedy"] + + for spelling in ( + "git checkout -- ", + "git restore ", + "git -C switch ", + "git -C switch --detach ", + ): + assert spelling in remedy, _context(command, remedy) + + def test_denies_a_clone_made_and_used_in_the_same_command( + self, repo: dict[str, Path | str], tmp_path: Path + ) -> None: + """The clone does not exist yet, so ``d`` is judged by its nearest existing + ancestor: the primary. + """ + command = "git clone d && git -C d checkout -b f" + _assert_verdict(_guard_output(repo, tmp_path, command), "primary-head", command) + + @pytest.mark.parametrize(("cwd", "command", "reason"), _DENIED_BY_POSITION) + def test_denies_a_command_whose_position_leads_to_the_primary_or_cannot_be_proven( + self, repo: dict[str, Path | str], tmp_path: Path, cwd: str, command: str, reason: str + ) -> None: + output = _guard_output(repo, tmp_path, command, cwd=cwd) + + _assert_verdict(output, "primary-head", command) + assert _primary_head_details(output)["reason"] == reason, _context(command, output) + + @pytest.mark.parametrize("tool_cwd", [".", ""]) + def test_denies_run_cmd_whose_tool_cwd_is_unknown( + self, repo: dict[str, Path | str], tmp_path: Path, tool_cwd: str + ) -> None: + command = "git checkout -b x" + output = _guard_output(repo, tmp_path, command, run_cmd=True, tool_cwd=tool_cwd) + + _assert_verdict(output, "primary-head", command) + assert _primary_head_details(output)["reason"] == "unresolved_target", _context( + command, output + ) + + +class TestCookPrimaryHeadAllowed: + @pytest.mark.parametrize("command", _ALLOWED_PATH_ONLY) + def test_allows_a_command_that_only_restores_paths( + self, repo: dict[str, Path | str], tmp_path: Path, command: str + ) -> None: + _assert_verdict(_guard_output(repo, tmp_path, command), "allow", command) + + @pytest.mark.parametrize("command", _ALLOWED_CURRENT_BRANCH_WORK) + def test_allows_work_on_the_current_branch( + self, repo: dict[str, Path | str], tmp_path: Path, command: str + ) -> None: + _assert_verdict(_guard_output(repo, tmp_path, command), "allow", command) + + @pytest.mark.parametrize(("cwd", "command"), _ALLOWED_OUTSIDE_THE_PRIMARY) + def test_allows_a_head_move_in_a_linked_worktree_or_another_repository( + self, repo: dict[str, Path | str], tmp_path: Path, cwd: str, command: str + ) -> None: + _assert_verdict(_guard_output(repo, tmp_path, command, cwd=cwd), "allow", command) + + def test_allows_work_in_a_clone_made_by_an_earlier_command( + self, repo: dict[str, Path | str], tmp_path: Path + ) -> None: + clone = "/.autoskillit/temp/c" + make = f"git clone {clone}" + use = f"git -C {clone} checkout -b f" + + _assert_verdict(_guard_output(repo, tmp_path, make), "allow", make) + _git(tmp_path, "clone", "-q", str(repo["primary"]), _expand(clone, repo, tmp_path)) + _assert_verdict(_guard_output(repo, tmp_path, use), "allow", use) + + +class TestPrimaryHeadMarkerIndependence: + """The rule follows the session, not the kitchen marker or the payload's directory.""" + + def test_protects_the_anchor_primary_from_a_payload_in_a_worktree_with_its_own_state( + self, repo: dict[str, Path | str], tmp_path: Path + ) -> None: + move = "git -C checkout -b x" + output = _guard_output(repo, tmp_path, move, cwd="linked", marker=False, state_root=False) + _assert_verdict(output, "primary-head", move) + + def test_still_protects_a_checked_out_ref_with_the_anchor_drifted_to_a_worktree( + self, repo: dict[str, Path | str], tmp_path: Path + ) -> None: + output = _guard_output( + repo, tmp_path, _UPDATE_DEVELOP, cwd="linked", marker=False, state_root=False + ) + _assert_verdict(output, "checked-out-ref", _UPDATE_DEVELOP) + + def test_denies_the_incident_with_no_marker_anywhere( + self, repo: dict[str, Path | str], tmp_path: Path + ) -> None: + _assert_verdict( + _guard_output(repo, tmp_path, _INCIDENT, marker=False), "primary-head", _INCIDENT + ) + + def test_order_session_without_a_marker_still_protects_a_checked_out_ref( + self, repo: dict[str, Path | str], tmp_path: Path + ) -> None: + output = _guard_output(repo, tmp_path, _UPDATE_DEVELOP, session="order", marker=False) + _assert_verdict(output, "checked-out-ref", _UPDATE_DEVELOP) + + +def _failing_probe(monkeypatch: pytest.MonkeyPatch, repository: Path) -> list[tuple[str, ...]]: + """Make ``rev-parse`` fail with a dubious-ownership error for *repository* itself. + + Returns the queries that reached the failure. + """ + real = _git_target._git_result + guarded = os.path.realpath(repository) + reached: list[tuple[str, ...]] = [] + + def probe(cwd: str, *args: str) -> subprocess.CompletedProcess[bytes]: + if args[:1] == ("rev-parse",) and os.path.realpath(cwd) == guarded: + reached.append(args) + message = f"fatal: detected dubious ownership in repository at '{cwd}'\n" + return subprocess.CompletedProcess( + ["git", *args], 128, stdout=b"", stderr=message.encode() + ) + return real(cwd, *args) + + monkeypatch.setattr(_git_target, "_git_result", probe) + return reached + + +def _cook_exit_code( + command: str, tmp_path: Path, repo: dict[str, Path | str], payload_cwd: Path +) -> int | str | None: + """Run main() in a cook session with the kitchen open and return its exit code. + + ``_run_guard`` requires a clean exit, which a blocked classification is not. + """ + primary = Path(str(repo["primary"])) + marker = primary / _HOOK_CONFIG_RELPATH + marker.parent.mkdir(parents=True, exist_ok=True) + marker.write_text(json.dumps({"kitchen": "open"})) + payload = _build_payload( + _expand(command, repo, tmp_path), tmp_path, use_bash_key=True, payload_cwd=payload_cwd + ) + env = {**_make_clean_env(None, headless=False), **_session_env("cook", repo, state_root=True)} + with ( + unittest.mock.patch.dict(os.environ, env, clear=True), + unittest.mock.patch("sys.stdin", io.StringIO(json.dumps(payload))), + unittest.mock.patch("pathlib.Path.cwd", return_value=tmp_path), + pytest.raises(SystemExit) as exit_info, + ): + git_ops_guard.main() + return exit_info.value.code + + +class TestPrimaryHeadAnchorFailure: + """An anchor repository Git refuses to query fails only the command that needs it.""" + + def test_allows_commands_that_never_ask_for_the_anchor( + self, repo: dict[str, Path | str], tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + primary = Path(str(repo["primary"])) + reached = _failing_probe(monkeypatch, primary) + for command in ("ls", "git status"): + _assert_verdict(_guard_output(repo, tmp_path, command), "allow", command) + reached.clear() + + _assert_verdict( + _guard_output(repo, tmp_path, "git status", cwd=primary / ".autoskillit"), + "allow", + "git status", + ) + assert reached == [], _context("git status asked for the anchor", reached) + + def test_allows_a_head_move_in_a_linked_worktree( + self, repo: dict[str, Path | str], tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + _failing_probe(monkeypatch, Path(str(repo["primary"]))) + command = "git checkout -b x" + _assert_verdict(_guard_output(repo, tmp_path, command, cwd="linked"), "allow", command) + + def test_blocks_the_incident_when_the_anchor_cannot_be_resolved( + self, + repo: dict[str, Path | str], + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], + ) -> None: + primary = Path(str(repo["primary"])) + reached = _failing_probe(monkeypatch, primary) + + code = _cook_exit_code(_INCIDENT, tmp_path, repo, primary / ".autoskillit") + + captured = capsys.readouterr() + context = _context(_INCIDENT, captured) + assert code == 2, context + assert captured.out == "", context + assert "git_ops_guard: RuntimeError: cook anchor repository unresolved" in captured.err, ( + context + ) + assert reached, context + + +class TestPrimaryHeadOtherSessions: + """Only a launched cook session is held to the rule; other shapes are unchanged.""" + + @pytest.mark.parametrize("command", [_INCIDENT, "git switch other"]) + @pytest.mark.parametrize( + "session", ["plain", "order", "fleet", "headless-skill", "headless-orchestrator"] + ) + def test_allows_a_head_move_in_the_primary( + self, repo: dict[str, Path | str], tmp_path: Path, session: str, command: str + ) -> None: + output = _guard_output(repo, tmp_path, command, session=session) + _assert_verdict(output, "allow", f"{session}: {command}") + + @pytest.mark.parametrize("session", ["plain", "headless"]) + def test_leaves_a_checked_out_ref_alone_while_the_kitchen_is_closed( + self, repo: dict[str, Path | str], tmp_path: Path, session: str + ) -> None: + output = _guard_output(repo, tmp_path, _UPDATE_DEVELOP, session=session, marker=False) + _assert_verdict(output, "allow", f"{session}: {_UPDATE_DEVELOP}") diff --git a/tests/skills/test_skill_placeholder_contracts.py b/tests/skills/test_skill_placeholder_contracts.py index 1e75011ec0..f5c46a561d 100644 --- a/tests/skills/test_skill_placeholder_contracts.py +++ b/tests/skills/test_skill_placeholder_contracts.py @@ -153,7 +153,7 @@ ("collapse-issues", "combined_url"), ("issue-splitter", "parent_url"), # source issue URL, obtained in prior step ("issue-splitter", "route"), # routing label value, from manifest - ("make-groups", "topic"), # prose: "git checkout -b feature/{topic}" + ("make-groups", "topic"), # prose: "git branch feature/{topic}" ("merge-pr", "pr_branch"), # per-PR head branch from gh pr list ("merge-pr", "file_path"), # per-file iteration in deletion check ("merge-pr", "symbol_name"), # per-symbol grep pattern From a5dd9408c1ac04471f52162c13053a990898357e Mon Sep 17 00:00:00 2001 From: Trecek Date: Thu, 5 Nov 2026 15:21:00 -0800 Subject: [PATCH 04/46] Match headless destructive Git operations by outcome Phase D of the git outcome and effective-target immunity work (#4751). The headless blocklist matched RISKY_GIT_OPERATIONS by option token, so push -fu, +refspec, --force-w, reset --har, clean -df/-xdf/--forc, commit --amen, checkout -f, clean.requireForce=false and aliases all bypassed it, while commit -m '--amend' was blocked in error. The names in RISKY_GIT_OPERATIONS are unchanged; matching moves onto outcome predicates over the same option engine and the same per-segment analysis, keyed exactly by that registry. Option values are never read as flags, any non-dry-run clean is risky (which covers every config channel), push --mirror counts as a force, and whole-tree checkout pathspecs are matched after normalization. Among several matching operations the choice is deterministic. contains_blocked_git_op moves next to its dependencies in _git_target and reads commands through the evaluated-segment walk with the shared cwd and analysis cache. Co-Authored-By: Claude Sonnet 5.5 --- docs/safety/hooks.md | 2 +- src/autoskillit/hooks/_runtime/AGENTS.md | 3 +- .../hooks/_runtime/_command_classification.py | 4 +- .../_runtime/_git_command_classification.py | 95 +-- .../hooks/_runtime/_git_outcome.py | 105 +++- src/autoskillit/hooks/_runtime/_git_target.py | 95 ++- src/autoskillit/hooks/guards/AGENTS.md | 8 +- src/autoskillit/hooks/guards/git_ops_guard.py | 24 +- tests/hooks/test_git_effect_differential.py | 550 +++++++++++++++++- tests/hooks/test_git_outcome.py | 229 +++++++- tests/infra/test_git_ops_guard.py | 275 ++++++++- tests/infra/test_risky_git_ops_coverage.py | 30 +- 12 files changed, 1253 insertions(+), 167 deletions(-) diff --git a/docs/safety/hooks.md b/docs/safety/hooks.md index 2348b162f6..c390e02a86 100644 --- a/docs/safety/hooks.md +++ b/docs/safety/hooks.md @@ -693,7 +693,7 @@ as a defense-in-depth measure against privilege escalation: | `background_exec_guard.py` | Unrecognized `AUTOSKILLIT_SESSION_TYPE` | Unknown session type should not bypass `run_in_background` prohibition | | `github_mutation_guard.py` | Ambiguous or unresolved GitHub mutation command, or unexpected classifier error | Unknown mutation scope must not bypass the structured review publisher | | `exploration_request_identity_guard.py` | A supported exploration event lacks bounded native identity or its one-shot record cannot be written | A Claude exploration call must not execute without request-correlated authority | -| `git_ops_guard.py` | Any unexpected runtime error (any `Exception`) while classifying a Git command, in the checked-out-ref preflight or the headless destructive-op phase; or an unresolved target (unknown cwd, layout override or unparseable global option) for a ref mutation; or, in the separate headless destructive-op-blocking preflight, an unrecognized global git flag that leaves the real subcommand unresolved | An unhandled exception must not silently allow a checked-out ref mutation — use exit 2 + stderr to hard-block. Separately, `_git_command_classification._contains_blocked_git_op` cannot match `_BLOCKED_GIT_OPS`'s literal subcommand tuples against an unresolved subcommand, so it denies unconditionally the moment `extract_git_subcommand_and_flags` reports `""`, rather than silently falling through to "not blocked" | +| `git_ops_guard.py` | Any unexpected runtime error (any `Exception`) while classifying a Git command, in the checked-out-ref preflight or the headless destructive-op phase; or an unresolved target (unknown cwd, layout override or unparseable global option) for a ref mutation; or, in the separate headless destructive-op-blocking preflight, an unrecognized global git flag that leaves the real subcommand unresolved | An unhandled exception must not silently allow a checked-out ref mutation — use exit 2 + stderr to hard-block. Separately, `_git_target.contains_blocked_git_op` cannot match `_BLOCKED_GIT_OPS`'s literal subcommand tuples against an unresolved subcommand, so it denies unconditionally the moment `extract_git_subcommand_and_flags` reports `""`, rather than silently falling through to "not blocked" | | `pr_create_guard.py` | Hook config unreadable or malformed while the kitchen is open (OSError, JSONDecodeError, AttributeError, TypeError) | An unresolvable `recipe_allows_pr_create` authorization must not be read as permission to bypass the prepare_pr → compose_pr pipeline | | `unsafe_install_guard.py` | An unrecognized global pip flag leaves `pip`'s `install` token position unresolved | `_find_pip_install` cannot tell whether the command is a pip install at all; treating that the same as "definitely not an install" would silently skip the editable/system-install checks entirely, so it is threaded through as a distinct `"unresolved-pip-flags"` kind and denied unconditionally, matching the pre-existing `"unresolved-subprocess"` kind's treatment | | `write_guard.py` | A write-verb operand or redirect target cannot be resolved while a write-prefix policy applies | An unresolved target could escape the allowed prefix; the denial explains how to substitute a literal path. | diff --git a/src/autoskillit/hooks/_runtime/AGENTS.md b/src/autoskillit/hooks/_runtime/AGENTS.md index c240bcce45..6b82806c3f 100644 --- a/src/autoskillit/hooks/_runtime/AGENTS.md +++ b/src/autoskillit/hooks/_runtime/AGENTS.md @@ -53,7 +53,8 @@ logic, with zero consumers outside this one file. there (`analyze_git_segment`): RESOLVED, NOT_A_REPOSITORY or UNRESOLVED, never a guessed repository. It imports `_shell_segment_walk`, `_git_command_classification`, `_git_outcome`, `_command_classification`, `_github_mutation_analysis` and - `_path_identity`; `guards/git_ops_guard.py` imports it. + `_path_identity`; `guards/git_ops_guard.py` imports it, including the headless blocklist + matcher `contains_blocked_git_op`. - `_git_command_classification.py` — Git operation classification used by `guards/git_ops_guard.py`; it shares the runtime command and mutation primitives. diff --git a/src/autoskillit/hooks/_runtime/_command_classification.py b/src/autoskillit/hooks/_runtime/_command_classification.py index 42867ea1c4..531c156878 100644 --- a/src/autoskillit/hooks/_runtime/_command_classification.py +++ b/src/autoskillit/hooks/_runtime/_command_classification.py @@ -540,9 +540,7 @@ def extract_git_subcommand_and_flags(segment: list[str]) -> GitInvocation | None unrecognized flag means the real subcommand could not be found (fail closed, ambiguous), not "there is no git subcommand here" (already safe). See is_allowed_protected_path_metadata_command and - `_git_command_classification._classify_git_segment` / - `_git_command_classification._contains_blocked_git_op` (moved from - `git_ops_guard.py` per Step 9 of #4665). + `_git_target.analyze_git_segment` / `_git_target.contains_blocked_git_op`. """ start = _command_start_index(segment) if start is None: diff --git a/src/autoskillit/hooks/_runtime/_git_command_classification.py b/src/autoskillit/hooks/_runtime/_git_command_classification.py index 53644826a0..eab562d93a 100644 --- a/src/autoskillit/hooks/_runtime/_git_command_classification.py +++ b/src/autoskillit/hooks/_runtime/_git_command_classification.py @@ -1,10 +1,10 @@ """Classification primitives for git operations used by git_ops_guard. The orchestrator (`git_ops_guard.py`) imports a controlled subset of the public -surface (`_classify_git_invocation`, -`_contains_blocked_git_op`, `_git_result`, `_git_text`, `_parse_worktree_owners`, -`_resolve_attempted_sha`, `_resolve_git_common_dir`) and dispatches into the inner -classifiers transitively through `_classify_git_invocation`. +surface (`_classify_git_invocation`, `_git_result`, `_git_text`, +`_parse_worktree_owners`, `_resolve_attempted_sha`, `_resolve_git_common_dir`) and +dispatches into the inner classifiers transitively through `_classify_git_invocation`. +`_git_target.py` also builds on `_git_result`. Ref-rewriting subcommands are classified by outcome: branch, checkout, switch, symbolic-ref, update-ref, reset, push and fetch read their argv through the @@ -19,16 +19,10 @@ the test-side bootstrap centralized in `tests/conftest.py`). It does NOT re-export any `_command_classification` symbol. -`_contains_blocked_git_op` reads the command exclusively through -`all_evaluated_segments`/`live_command_text` (rectify #4941 Part A) rather than -a raw `shlex.split`/text-bag scan, so a `git` invocation delivered via -`bash -c`, a heredoc, a herestring, or a pipe is seen the same way a direct -invocation is. - Public API surface: Classifiers: _classify_git_invocation, _classify_fetch, _classify_push, _classify_update_ref, _classify_reset, - _classify_outcome, _contains_blocked_git_op + _classify_outcome Helpers (consumed transitively by the classifiers above): _git_result, _git_text, _parse_worktree_owners, _resolve_git_common_dir, _normal_branch_ref, _symbolic_head, @@ -39,18 +33,11 @@ from __future__ import annotations import os -import re import subprocess from pathlib import Path from _command_classification import ( GitInvocation, - _command_position_candidate_spans, - all_evaluated_segments, - command_verb_and_args, - extract_git_subcommand_and_flags, - extract_interpreter_command_payloads, - live_command_text, ) from _git_outcome import ( git_ref_outcome, @@ -63,78 +50,6 @@ _DYNAMIC_SHELL_TOKEN_RE, ) -_DELIMITERS_RE = re.compile(r"[\s,\[\]'\"()]+") - - -def _tokenize_text(text: str) -> frozenset[str]: - """Split text on common delimiters for set-membership matching. - - Prevents '.' from matching periods inside filenames like 'foo.py'. - """ - return frozenset(t for t in _DELIMITERS_RE.split(text) if t) - - -def _blocked_op_in_evaluated_segment( - segment: list[str], blocked_ops: frozenset[tuple[str, ...]] -) -> tuple[str, ...] | None: - """Return the first blocked git operation in an evaluated segment.""" - for start, end in _command_position_candidate_spans(segment): - verb, args = command_verb_and_args(segment[start:end]) - if verb != "git" and not verb.endswith("/git"): - continue - result = extract_git_subcommand_and_flags([verb, *args]) - if result is None: - continue - subcommand = result.subcommand - remaining = result.flags - if subcommand == "": - return (subcommand,) - for op_tuple in blocked_ops: - if subcommand == op_tuple[0] and all(flag in remaining for flag in op_tuple[1:]): - return op_tuple - return None - - -def _contains_blocked_git_op( - cmd: str, blocked_ops: frozenset[tuple[str, ...]] -) -> tuple[str, ...] | None: - """Return the matching blocked git op tuple, or None if no match. - - Reads *cmd* exclusively through `all_evaluated_segments`, the single - authority for "what will actually execute, and as what argv" (rectify - #4941 Part A): a direct invocation, one delivered via `bash -c`/`eval`, - one fed through a heredoc/herestring/pipe to a shell, and a literal-argv - `subprocess.run(["git", ...])` are all seen the same way, at every - command-position candidate span (top level, and inside an inline - function/group body). A 'git' token (or /path/to/git, or a wrapper- or - env-assignment-prefixed invocation) is a command start via - `command_verb_and_args`. Returns `None` when `all_evaluated_segments` - cannot tokenize *cmd* (fail-open). - - `blocked_ops` is passed by the caller (the orchestrator's `main()` - passes `_BLOCKED_GIT_OPS` to keep the constant co-located with itself - per the sync-invariant test in `test_risky_git_ops_coverage.py`). - """ - segments = all_evaluated_segments(cmd) - if segments is None: - return None - - for segment in segments: - blocked_op = _blocked_op_in_evaluated_segment(segment, blocked_ops) - if blocked_op is not None: - return blocked_op - - # A Python payload whose subprocess/os call could not be resolved to a - # literal argv or string (dynamic argument, unrecognized call shape) - # means a "git" mention anywhere in what will actually run cannot be - # ruled out -- deny unconditionally rather than silently falling - # through to "not blocked". - _payloads, has_unresolved = extract_interpreter_command_payloads(cmd) - if has_unresolved and "git" in _tokenize_text(live_command_text(cmd)): - return ("",) - - return None - def _git_result(cwd: str, *args: str) -> subprocess.CompletedProcess[bytes]: return subprocess.run( diff --git a/src/autoskillit/hooks/_runtime/_git_outcome.py b/src/autoskillit/hooks/_runtime/_git_outcome.py index 1e77a663cd..38e8603c93 100644 --- a/src/autoskillit/hooks/_runtime/_git_outcome.py +++ b/src/autoskillit/hooks/_runtime/_git_outcome.py @@ -17,7 +17,9 @@ from __future__ import annotations -from collections.abc import Sequence +import os +import re +from collections.abc import Callable, Mapping, Sequence from enum import StrEnum from typing import TYPE_CHECKING, NamedTuple @@ -27,6 +29,7 @@ _GIT_FETCH_FLAG_SPEC, _GIT_PUSH_FLAG_SPEC, _GIT_RESET_FLAG_SPEC, + _GIT_SUBCOMMAND_TABLES, _GIT_SWITCH_FLAG_SPEC, _GIT_SYMBOLIC_REF_FLAG_SPEC, _GIT_UPDATE_REF_FLAG_SPEC, @@ -271,6 +274,11 @@ class ResetArgs(NamedTuple): unresolved: str | None +def _last_reset_mode(parsed: ParsedArgv) -> str | None: + modes = ("soft", "mixed", "hard", "merge", "keep") + return next((o.name for o in reversed(parsed.options) if o.name in modes), None) + + def parse_reset(args: Sequence[str]) -> ResetArgs: parsed = parse_options(args, _GIT_RESET_FLAG_SPEC) unresolved = parsed.unresolved @@ -278,8 +286,7 @@ def parse_reset(args: Sequence[str]) -> ResetArgs: unresolved = "pathspec_from_file" operands = parsed.operands before = parsed.operands_before_dashdash - modes = ("soft", "mixed", "hard", "merge", "keep") - mode = next((o.name for o in reversed(parsed.options) if o.name in modes), None) + mode = _last_reset_mode(parsed) path_mode = ( _active(parsed, "patch") is not None or (before is not None and len(operands) > before) @@ -365,3 +372,95 @@ def is_ref_mutation(subcommand: str, args: Sequence[str]) -> bool: or any(":" in refspec for refspec in fetch.refspecs) ) return False + + +_PATHSPEC_MAGIC_RE = re.compile(r"^:(?:/|\([a-z,]*\))?") + + +def _is_whole_tree_pathspec(token: str) -> bool: + """Whether a pathspec selects the whole tree: ``.``, ``./``, ``d/..``, ``:/``, ``:(top)``, + ``*`` and their equivalents once pathspec magic and redundant segments are removed.""" + normalized = os.path.normpath(_PATHSPEC_MAGIC_RE.sub("", token, count=1)) + return normalized == "." or set(normalized) == {"*"} + + +def _risky_amend(parsed: ParsedArgv) -> bool: + return _active(parsed, "amend") is not None + + +def _risky_force_push(parsed: ParsedArgv) -> bool: + forced_refspec = any(refspec.startswith("+") for refspec in parsed.operands[1:]) + overwrites_remote = _active(parsed, "force") or _active(parsed, "mirror") + return overwrites_remote is not None or forced_refspec + + +def _risky_force_with_lease(parsed: ParsedArgv) -> bool: + return _active(parsed, "force-with-lease") is not None + + +def _risky_hard_reset(parsed: ParsedArgv) -> bool: + return _last_reset_mode(parsed) == "hard" + + +def _risky_clean(parsed: ParsedArgv) -> bool: + """Any clean that is not a dry run: Git refuses an unforced clean unless config + says otherwise, so this covers every config channel without querying config.""" + return not any(_active(parsed, name) for name in ("dry-run", "interactive")) + + +def _risky_clean_directories(parsed: ParsedArgv) -> bool: + return _risky_clean(parsed) and _active(parsed, "d") is not None + + +def _risky_checkout_whole_tree(parsed: ParsedArgv) -> bool: + operands = parsed.operands + if not operands: + return _active(parsed, "force") is not None + if parsed.operands_before_dashdash is not None: + return False + return any(_is_whole_tree_pathspec(operand) for operand in operands) + + +def _risky_checkout_dashdash_whole_tree(parsed: ParsedArgv) -> bool: + before = parsed.operands_before_dashdash + return before is not None and any( + _is_whole_tree_pathspec(operand) for operand in parsed.operands[before:] + ) + + +# Keyed exactly by RISKY_GIT_OPERATIONS (tests/hooks/test_git_outcome.py pins this). +_RISKY_PREDICATES: Mapping[tuple[str, ...], Callable[[ParsedArgv], bool]] = { + ("commit", "--amend"): _risky_amend, + ("push", "--force"): _risky_force_push, + ("push", "-f"): _risky_force_push, + ("push", "--force-with-lease"): _risky_force_with_lease, + ("reset", "--hard"): _risky_hard_reset, + ("clean", "-f"): _risky_clean, + ("clean", "-fd"): _risky_clean_directories, + ("checkout", "."): _risky_checkout_whole_tree, + ("checkout", "--", "."): _risky_checkout_dashdash_whole_tree, +} + + +def risky_git_operation( + subcommand: str, args: Sequence[str], blocked_ops: frozenset[tuple[str, ...]] +) -> tuple[str, ...] | None: + """The blocked operation an invocation performs, by outcome rather than by spelling. + + An unresolved subcommand or option parse is returned as ``("",)``. + Among several matching operations the longest wins, then sorted order, so the + choice is deterministic. Option values are never read as flags, so + ``commit -m '--amend'`` is not an amend. + """ + if subcommand == "": + return ("",) + candidates = [op for op in blocked_ops if op[0] == subcommand] + if not candidates: + return None + parsed = parse_options(args, _GIT_SUBCOMMAND_TABLES[subcommand]) + if parsed.unresolved: + return ("",) + matching = sorted( + (op for op in candidates if _RISKY_PREDICATES[op](parsed)), key=lambda op: (-len(op), op) + ) + return matching[0] if matching else None diff --git a/src/autoskillit/hooks/_runtime/_git_target.py b/src/autoskillit/hooks/_runtime/_git_target.py index ff280a3c2b..993ca24c54 100644 --- a/src/autoskillit/hooks/_runtime/_git_target.py +++ b/src/autoskillit/hooks/_runtime/_git_target.py @@ -20,21 +20,21 @@ import os import re from enum import StrEnum -from typing import TYPE_CHECKING, Any, Literal, NamedTuple +from typing import Any, Literal, NamedTuple from _command_classification import ( GitInvocation, _git_subcommand_index, extract_git_subcommand_and_flags, + extract_interpreter_command_payloads, + live_command_text, tokenize_command_segments, ) from _git_command_classification import _git_result -from _git_outcome import GitHeadEffect, GitRefOutcome, git_ref_outcome +from _git_outcome import GitHeadEffect, GitRefOutcome, git_ref_outcome, risky_git_operation from _github_mutation_analysis import _DYNAMIC_SHELL_TOKEN_RE from _path_identity import PathIdentityError, canonical_existing_path - -if TYPE_CHECKING: - from autoskillit.hooks._runtime._shell_segment_walk import WalkedSegment +from _shell_segment_walk import WalkedSegment, walk_evaluated_segments # Git subcommands that can mutate checked-out refs. Read-only commands (log, # status, diff, show, ...) must not be denied just because a preceding cd failed. @@ -73,6 +73,7 @@ ) _ALIAS_CHANNEL_RE = re.compile(r"alias\.|GIT_CONFIG|\bHOME=|XDG_CONFIG_HOME=") _ALIAS_KEY_RE = re.compile(r"[A-Za-z0-9][A-Za-z0-9-]*") +_DELIMITERS_RE = re.compile(r"[\s,\[\]'\"()]+") class GitTargetKind(StrEnum): @@ -280,3 +281,87 @@ def analyze_git_segment( else git_ref_outcome(invocation.subcommand, invocation.flags) ) return GitSegmentFacts(invocation, target, outcome, identity_unknown) + + +def _mutation_names(subcommands: frozenset[str]) -> str: + return "|".join(sorted(re.escape(name) for name in subcommands)) + + +def _mentions_git_mutation( + text: str, subcommands: frozenset[str] = _GIT_MUTATION_SUBCOMMANDS +) -> bool: + """Whether *text* names a Git command word followed by one of *subcommands*. + + Deliberately matches inside arguments too: an opaque launcher's payload + (``xargs sh -c '...git push...'``) cannot be told apart from quoted data + without per-launcher grammar. + """ + names = _mutation_names(subcommands) + pattern = rf"(? frozenset[str]: + """Split text on common delimiters for set-membership matching. + + Prevents '.' from matching periods inside filenames like 'foo.py'. + """ + return frozenset(token for token in _DELIMITERS_RE.split(text) if token) + + +def _blocked_in_segment( + seg: WalkedSegment, + flags: ChannelFlags, + cache: dict[Any, Any], + blocked_ops: frozenset[tuple[str, ...]], +) -> tuple[str, ...] | None: + names = frozenset(op[0] for op in blocked_ops) + if seg.position_unresolved and _mentions_git_mutation(" ".join(seg.executable), names): + return ("",) + facts = analyze_git_segment(seg, flags, cache) + if facts is None: + return None + if facts.identity_unknown: + return ("",) + return risky_git_operation(facts.invocation.subcommand, facts.invocation.flags, blocked_ops) + + +def contains_blocked_git_op( + cmd: str, + blocked_ops: frozenset[tuple[str, ...]], + cwd: str = "", + cache: dict[Any, Any] | None = None, +) -> tuple[str, ...] | None: + """Return the blocked Git operation *cmd* performs, or None. + + Reads *cmd* exclusively through the evaluated-segment walk, the single + authority for what will actually execute and as what argv (rectify #4941 + Part A): a direct invocation, one delivered via ``bash -c``/``eval``, a + heredoc/herestring/pipe-fed shell, and a literal-argv + ``subprocess.run(["git", ...])`` are all seen the same way. Operations are + matched by outcome (``risky_git_operation``), not by option spelling. + Returns None when *cmd* cannot be tokenized (fail-open). + + ``blocked_ops`` is passed by the caller (the orchestrator's ``main()`` passes + ``_BLOCKED_GIT_OPS`` to keep the constant co-located with itself per the + sync-invariant test in ``test_risky_git_ops_coverage.py``). + """ + walk = walk_evaluated_segments(cmd, cwd) + if walk is None: + return None + live_text = live_command_text(cmd) + flags = command_channel_flags(live_text) + shared_cache: dict[Any, Any] = {} if cache is None else cache + for seg in walk: + blocked = _blocked_in_segment(seg, flags, shared_cache, blocked_ops) + if blocked is not None: + return blocked + + # A Python payload whose subprocess/os call could not be resolved to a + # literal argv or string means a "git" mention anywhere in what will + # actually run cannot be ruled out -- deny rather than silently falling + # through to "not blocked". + _payloads, has_unresolved = extract_interpreter_command_payloads(cmd) + if has_unresolved and "git" in _tokenize_text(live_text): + return ("",) + return None diff --git a/src/autoskillit/hooks/guards/AGENTS.md b/src/autoskillit/hooks/guards/AGENTS.md index b6b621ec0e..e3e235172e 100644 --- a/src/autoskillit/hooks/guards/AGENTS.md +++ b/src/autoskillit/hooks/guards/AGENTS.md @@ -59,7 +59,11 @@ The package initializer remains import-free. that runs `git checkout ` in the cook session's own checkout (the `checkout` step of `full-audit`) is denied by design; point `workspace` at a linked worktree. In headless skill sessions, the later legacy Git phase blocks - `commit --amend`, `push --force`, `reset --hard`, `clean -f`, and `checkout .`. + `commit --amend`, `push --force`, `reset --hard`, `clean -f`, and `checkout .`. It + matches those operations by outcome rather than by spelling (abbreviated, bundled and + `+refspec` forms, aliases, and any non-dry-run `clean`), and never reads an option + value as a flag. Other working-tree discards such as `git stash`, `git restore .` and + `git checkout -f ` stay outside this phase. Raw GitHub review publication is fail-closed across Bash and `run_cmd`: the guard denies review/comment writes, multiple or dynamically unresolved mutations, and relative `--input` files that cannot be resolved against the command's own execution cwd (a @@ -122,7 +126,7 @@ defense-in-depth measure against privilege escalation: | `github_mutation_guard.py` | Ambiguous or unresolved GitHub mutation command | Unknown mutation scope must not bypass the structured review publisher | | `exploration_request_identity_guard.py` | A supported exploration event lacks bounded native identity or its one-shot record cannot be written | A Claude exploration call must not execute without request-correlated authority | | `join_stop_guard.py` | Malformed/non-object payload or missing `session_id` | Stop must not release when the guard cannot identify the binding whose wave completion authorizes release | -| `git_ops_guard.py` | Any unexpected runtime error (any `Exception`) while classifying a Git command, in the checked-out-ref preflight or the headless destructive-op phase; or an unresolved target (unknown cwd, layout override or unparseable global option) for a ref mutation; or, in the separate headless destructive-op-blocking preflight, an unrecognized global git flag that leaves the real subcommand unresolved, or a Python subprocess/os call whose argument could not be resolved to a literal argv or string while `live_command_text` still mentions "git" | An unhandled exception must not silently allow a checked-out ref mutation — use exit 2 + stderr to hard-block. `_git_command_classification._contains_blocked_git_op` reads the command exclusively through `all_evaluated_segments`, the evaluated-segment blocklist authority (rectify #4941 Part A): it cannot match `_BLOCKED_GIT_OPS`'s literal subcommand tuples against an unresolved subcommand, so it denies unconditionally the moment `extract_git_subcommand_and_flags` reports `""`, and separately denies when an unresolved interpreter payload cannot rule out a hidden `git` invocation, rather than silently falling through to "not blocked" | +| `git_ops_guard.py` | Any unexpected runtime error (any `Exception`) while classifying a Git command, in the checked-out-ref preflight or the headless destructive-op phase; or an unresolved target (unknown cwd, layout override or unparseable global option) for a ref mutation; or, in the separate headless destructive-op-blocking preflight, an unrecognized global git flag that leaves the real subcommand unresolved, or a Python subprocess/os call whose argument could not be resolved to a literal argv or string while `live_command_text` still mentions "git" | An unhandled exception must not silently allow a checked-out ref mutation — use exit 2 + stderr to hard-block. `_git_target.contains_blocked_git_op` reads the command exclusively through the evaluated-segment walk, the blocklist authority (rectify #4941 Part A): it cannot match `_BLOCKED_GIT_OPS`'s literal subcommand tuples against an unresolved subcommand, so it denies unconditionally the moment `extract_git_subcommand_and_flags` reports `""`, and separately denies when an unresolved interpreter payload cannot rule out a hidden `git` invocation, rather than silently falling through to "not blocked" | | `git_ops_guard.py` | In an interactive cook session, the anchor repository cannot be resolved, or a Git command that could move HEAD has an unresolved target | A command that might move the primary checkout's HEAD must not pass on a target the guard cannot prove; the deny names a literal `git -C ` or `cd &&` remedy | | `pr_create_guard.py` | Hook config unreadable or malformed while the kitchen is open (OSError, JSONDecodeError, AttributeError, TypeError) | An unresolvable `recipe_allows_pr_create` authorization must not be read as permission to bypass the prepare_pr → compose_pr pipeline | | `unsafe_install_guard.py` | An unrecognized global pip flag leaves `pip`'s `install` token position unresolved | `_find_pip_install` cannot tell whether the command is a pip install at all; treating that the same as "definitely not an install" would silently skip the editable/system-install checks entirely, so it is threaded through as a distinct `"unresolved-pip-flags"` kind and denied unconditionally, matching the pre-existing `"unresolved-subprocess"` kind's treatment | diff --git a/src/autoskillit/hooks/guards/git_ops_guard.py b/src/autoskillit/hooks/guards/git_ops_guard.py index 69ee992c92..089f715688 100644 --- a/src/autoskillit/hooks/guards/git_ops_guard.py +++ b/src/autoskillit/hooks/guards/git_ops_guard.py @@ -41,7 +41,6 @@ ) from _git_command_classification import ( # noqa: E402 _classify_git_invocation, - _contains_blocked_git_op, _git_result, _git_text, _parse_worktree_owners, @@ -56,8 +55,11 @@ GitSegmentFacts, GitTarget, GitTargetKind, + _mentions_git_mutation, + _mutation_names, analyze_git_segment, command_channel_flags, + contains_blocked_git_op, resolve_git_target_for_path, ) from _github_mutation_analysis import ( # noqa: E402 @@ -119,24 +121,6 @@ _PYTHON_VERB_RE = re.compile(r"python(?:3(?:\.\d+)?)?") -def _mutation_names(subcommands: frozenset[str]) -> str: - return "|".join(sorted(re.escape(name) for name in subcommands)) - - -def _mentions_git_mutation( - text: str, subcommands: frozenset[str] = _GIT_MUTATION_SUBCOMMANDS -) -> bool: - """Whether *text* names a Git command word followed by one of *subcommands*. - - Deliberately matches inside arguments too: an opaque launcher's payload - (``xargs sh -c '...git push...'``) cannot be told apart from quoted data - without per-launcher grammar. - """ - names = _mutation_names(subcommands) - pattern = rf"(? dict[str, object]: key = ("owners", target.cwd) if key not in cache: @@ -647,7 +631,7 @@ def main() -> None: ): sys.exit(0) - blocked = _contains_blocked_git_op(cmd, _BLOCKED_GIT_OPS) + blocked = contains_blocked_git_op(cmd, _BLOCKED_GIT_OPS, parsed.execution_cwd, cache) if blocked is None or _git_op_policy_allows(project_root, blocked): sys.exit(0) diff --git a/tests/hooks/test_git_effect_differential.py b/tests/hooks/test_git_effect_differential.py index 6b844be650..7428232101 100644 --- a/tests/hooks/test_git_effect_differential.py +++ b/tests/hooks/test_git_effect_differential.py @@ -6,8 +6,9 @@ Members: ``_build_base`` and ``_sandbox`` (fixture repositories), ``_run_differential`` (the shared driver returning a ``_Run``), ``_git_cases`` -(argv strategy), the Property A section (shared refs) and the Property C -section (HEAD of the primary checkout). +(argv strategy), the Property A section (shared refs), the Property C section +(HEAD of the primary checkout) and the Property D section (destructive effects +on a dirty repository, with its own fixture, strategy and driver). """ from __future__ import annotations @@ -19,7 +20,7 @@ import subprocess from collections.abc import Callable, Iterator, Mapping from dataclasses import dataclass -from pathlib import Path +from pathlib import Path, PurePosixPath from typing import Any, NamedTuple import pytest @@ -43,6 +44,7 @@ GitHeadEffect, GitRefOutcome, git_ref_outcome, + risky_git_operation, ) from _git_target import ( # type: ignore[import-not-found] GitTargetKind, @@ -50,6 +52,7 @@ command_channel_flags, resolve_git_target_for_path, ) +from _hook_constants import RISKY_GIT_OPERATIONS # type: ignore[import-not-found] from _shell_segment_walk import walk_evaluated_segments # type: ignore[import-not-found] from hypothesis import HealthCheck, event, example, given, settings from hypothesis import strategies as st @@ -545,6 +548,19 @@ def _sk(rows: str, minimum: int, *pools: tuple[str, ...]) -> _Skeleton: "refmap": ("refs/heads/*:refs/heads/*", "refs/heads/feat:refs/heads/feat"), "negotiation-tip": ("HEAD",), "filter": ("blob:none",), + "message": ("x", "amended"), + "file": ("d/x",), + "template": ("d/x",), + "author": ("Other ",), + "date": ("2000-01-01T00:00:00+0000",), + "fixup": ("HEAD", "amend:HEAD", "reword:HEAD"), + "squash": ("HEAD",), + "reuse-message": ("HEAD",), + "reedit-message": ("HEAD",), + "cleanup": ("strip", "verbatim"), + "trailer": ("Acked-by: x",), + "untracked-files": ("no", "all"), + "exclude": ("*.tmp", "untracked_dir"), } @@ -629,27 +645,35 @@ def _spelled_row(draw: st.DrawFn, subcommand: str, name: str) -> list[str]: @st.composite -def _git_cases(draw: st.DrawFn) -> _Case: +def _argv_cases( + draw: st.DrawFn, + subcommands: tuple[str, ...], + slots: Mapping[str, tuple[tuple[str, ...], ...]], + skeletons: Mapping[str, tuple[_Skeleton, ...]], + strays: Mapping[str, tuple[str, ...]] | None = None, +) -> _Case: """A subcommand with options and operands in any order and an optional ``--``. Three in four draws start from a well-formed skeleton and add at most one stray option; the rest are free-form combinations of table rows and operands. + Stray options come from the whole option table unless *strays* narrows them. """ - subcommand = draw(st.sampled_from(_SUBCOMMANDS)) + subcommand = draw(st.sampled_from(subcommands)) table = _GIT_SUBCOMMAND_TABLES[subcommand] structured = draw(st.sampled_from((True, True, True, False))) if structured: - rows, minimum, pools = draw(st.sampled_from(_SKELETONS[subcommand])) + rows, minimum, pools = draw(st.sampled_from(skeletons[subcommand])) stray = 1 else: - rows, minimum, pools, stray = (), 0, _OPERAND_SLOTS[subcommand], 3 + rows, minimum, pools, stray = (), 0, slots[subcommand], 3 operands = pools[: draw(st.integers(minimum, len(pools)))] items = [(False, [draw(st.sampled_from(pool))]) for pool in operands] groups = [ draw(_spelled_row(subcommand, draw(st.sampled_from(alternatives.split("|"))))) for alternatives in rows ] - stray_option = st.sampled_from(sorted(table)).flatmap( + stray_names = sorted(table) if strays is None else strays[subcommand] + stray_option = st.sampled_from(stray_names).flatmap( lambda name: _spelled_row(subcommand, name) ) groups += draw(st.lists(stray_option, max_size=stray)) @@ -662,6 +686,10 @@ def _git_cases(draw: st.DrawFn) -> _Case: return subcommand, tuple(token for _, group in items for token in group) +def _git_cases() -> st.SearchStrategy[_Case]: + return _argv_cases(_SUBCOMMANDS, _OPERAND_SLOTS, _SKELETONS) + + # --------------------------------------------------------------------------- # Property A: shared refs # --------------------------------------------------------------------------- @@ -984,3 +1012,509 @@ def test_primary_head_rule_denies_every_checkout_and_switch_that_moves_the_prima ) problems = _primary_head_problems(run, verdict) assert not problems, _primary_head_failure_message(run, verdict, problems) + + +# --------------------------------------------------------------------------- +# Property D: destructive effects +# --------------------------------------------------------------------------- + +_EDITED_PATHS = ("file.txt", "d/x", "local.txt", "hidden/h.txt") +_EARLIER_COMMITTER_DATE = "2025-06-01T00:00:00+0000" + + +def _write(path: Path, text: str) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(text, encoding="utf-8") + + +def _build_destructive_base(base: _Base, root: Path) -> _Base: + """A copy of *base* holding the state a destructive command can destroy. + + ``develop`` gains a local commit written with an earlier committer date than + any later command uses, so it diverges from ``origin/develop`` and an amend + always writes a different commit. Four tracked files carry unstaged edits; + ``hidden/h.txt`` is named by no argv pool, so only a whole-tree operand can + revert every edit. The untracked entries are a file, a directory holding a + file, an empty directory, a nested repository and an ignored file. + """ + tree = root / "tree" + shutil.copytree(base.tree, tree, symlinks=True) + primary = tree / "primary" + with _pinned_git_environment(base.home): + _git(primary, "remote", "set-url", "origin", str(tree / "origin.git")) + with pytest.MonkeyPatch.context() as patch: + patch.setenv("GIT_COMMITTER_DATE", _EARLIER_COMMITTER_DATE) + _write(primary / "local.txt", "local\n") + _write(primary / "hidden" / "h.txt", "hidden\n") + _git(primary, "add", "local.txt", "hidden/h.txt") + _git(primary, "commit", "-q", "-m", "local") + _git(primary, "fetch", "-q", "origin") + for relpath in _EDITED_PATHS: + path = primary / relpath + _write(path, path.read_text(encoding="utf-8") + "edited\n") + _write(primary / "scratch.tmp", "scratch\n") + _write(primary / "untracked_dir" / "inner.txt", "inner\n") + (primary / "empty_dir").mkdir() + _write(primary / "ignored.log", "ignored\n") + with (primary / ".git" / "info" / "exclude").open("a", encoding="utf-8") as exclude: + exclude.write("*.log\n") + _git(primary, "init", "-q", "--template=", "nested") + _write(primary / "nested" / "n.txt", "nested\n") + return _Base(tree, base.home, base.sha_a, base.sha_b, base.git_version) + + +class _Snapshot(NamedTuple): + head_ref: str + head: str + parent: str + edits: Mapping[str, bytes | None] + untracked: frozenset[str] + disk: frozenset[str] + remote: Mapping[str, str] + + +def _disk_entries(worktree: Path) -> frozenset[str]: + entries: set[str] = set() + for directory, names, files in os.walk(worktree): + here = Path(directory) + if here == worktree and ".git" in names: + names.remove(".git") + entries.update((here / name).relative_to(worktree).as_posix() for name in (*names, *files)) + return frozenset(entries) + + +def _tracked_entries(worktree: Path) -> frozenset[str]: + """Every tracked path and each directory that contains one.""" + listing = _git(worktree, "ls-files", "-z", check=False).stdout + tracked: set[str] = set() + for relpath in filter(None, listing.split("\0")): + tracked.add(relpath) + tracked.update( + parent.as_posix() for parent in PurePosixPath(relpath).parents if parent.name + ) + return frozenset(tracked) + + +def _read_bytes(path: Path) -> bytes | None: + try: + return path.read_bytes() + except OSError: + return None + + +def _remote_heads(origin: Path) -> dict[str, str]: + listing = _git(origin, "for-each-ref", "--format=%(refname) %(objectname)", "refs/heads") + return dict(line.split(" ", 1) for line in listing.stdout.splitlines()) + + +def _snapshot(sandbox: _Sandbox) -> _Snapshot: + primary = sandbox.primary + disk = _disk_entries(primary) + return _Snapshot( + head_ref=_git(primary, "symbolic-ref", "-q", "HEAD", check=False).stdout.strip(), + head=_git(primary, "rev-parse", "--verify", "-q", "HEAD", check=False).stdout.strip(), + parent=_git(primary, "rev-parse", "--verify", "-q", "HEAD^", check=False).stdout.strip(), + edits={relpath: _read_bytes(primary / relpath) for relpath in _EDITED_PATHS}, + untracked=disk - _tracked_entries(primary), + disk=disk, + remote=_remote_heads(primary.parent / "origin.git"), + ) + + +def _rewritten_non_fast_forward( + origin: Path, before: Mapping[str, str], after: Mapping[str, str] +) -> bool: + """Whether a branch now points at a commit that does not contain its old tip.""" + for ref, old in before.items(): + new = after.get(ref) + if new is None or new == old: + continue + if _git(origin, "merge-base", "--is-ancestor", old, new, check=False).returncode: + return True + return False + + +def _observed_effect( + origin: Path, subcommand: str, before: _Snapshot, after: _Snapshot +) -> str | None: + """The destructive effect named after *subcommand*, when Git had it between the snapshots. + + An amend replaces the commit the current branch ends in. A hard reset reverts + some edit; a whole-tree restore reverts every edit. A clean removes some + untracked entry. A force push moves a remote branch to a commit that does not + contain its old tip. + """ + reverted = sum(after.edits[relpath] != before.edits[relpath] for relpath in _EDITED_PATHS) + if subcommand == "commit": + replaced = ( + after.head_ref == before.head_ref + and after.head != before.head + and after.parent == before.parent + ) + return "amend" if replaced else None + if subcommand == "reset": + return "hard-reset" if reverted else None + if subcommand == "checkout": + return "whole-tree-restore" if reverted == len(_EDITED_PATHS) else None + if subcommand == "clean": + return "clean" if before.untracked - after.disk else None + if subcommand == "push": + rewritten = _rewritten_non_fast_forward(origin, before.remote, after.remote) + return "force-push" if rewritten else None + return None + + +@dataclass(frozen=True, slots=True) +class _DestructiveRun: + subcommand: str + argv: tuple[str, ...] + returncode: int + stderr: str + effect: str | None + git_version: str + + @property + def command(self) -> str: + return shlex.join(["git", self.subcommand, *self.argv]) + + +def _run_destructive( + sandbox: _Sandbox, subcommand: str, argv: tuple[str, ...], *, require_force: bool +) -> _DestructiveRun: + """Run ``git `` in the dirty primary and name its destructive effect.""" + if not require_force: + _git(sandbox.primary, "config", "clean.requireForce", "false") + before = _snapshot(sandbox) + completed = _git(sandbox.primary, subcommand, *argv, check=False) + after = _snapshot(sandbox) + effect = _observed_effect(sandbox.primary.parent / "origin.git", subcommand, before, after) + return _DestructiveRun( + subcommand, argv, completed.returncode, completed.stderr, effect, sandbox.git_version + ) + + +_DIRTY_SUBCOMMANDS = ("commit", "reset", "clean", "checkout", "push") + +_DIRTY_REVISIONS = ( + "HEAD", + "HEAD~", + "HEAD~1", + "HEAD~2", + "develop", + "feat", + "other", + "origin/develop", +) +_DIRTY_TRACKED = ("file.txt", "d/x", "d", "local.txt") +_WHOLE_TREE_SPELLINGS = (".", "./", ":/", ":(top)", ":(top).") +_DIRTY_RESTORE = (*_DIRTY_TRACKED, *_WHOLE_TREE_SPELLINGS) +_DIRTY_UNTRACKED = ( + "scratch.tmp", + "untracked_dir", + "empty_dir", + "nested", + "ignored.log", + ".", + "file.txt", +) +_ORIGIN = ("origin",) +_DIRTY_REFSPECS = ( + "develop", + "feat", + "other", + "HEAD", + "develop:develop", + "HEAD:develop", + "feat:develop", + "other:develop", + "develop:other", + "feat:other", + "develop:feat", + "other:feat", + "feat:review", + "develop:y", + "+develop", + "+feat", + "+develop:develop", + "+feat:develop", + "+HEAD:develop", + "+develop:other", + "+feat:other", + "+develop:feat", + "+other:feat", + "+other:review", + "+feat:y", +) + +# A revision operand of checkout always has a path after it, so no generated +# checkout switches branches: a forced switch discards edits as well, but it is +# not a whole-tree restore. +_DIRTY_SLOTS: Mapping[str, tuple[tuple[str, ...], ...]] = { + "commit": (_DIRTY_TRACKED,) * 2, + "reset": (_DIRTY_REVISIONS, _DIRTY_TRACKED, _DIRTY_TRACKED), + "clean": (_DIRTY_UNTRACKED,) * 2, + "checkout": (_DIRTY_RESTORE,) * 3, + "push": (_ORIGIN, _DIRTY_REFSPECS, _DIRTY_REFSPECS), +} + +_DIRTY_SKELETONS: Mapping[str, tuple[_Skeleton, ...]] = { + "commit": ( + _sk("amend", 0, _DIRTY_TRACKED), + _sk("amend message", 0, _DIRTY_TRACKED), + _sk("amend all", 0), + _sk("amend edit|reset-author|signoff|allow-empty|quiet|date|author|dry-run", 0), + _sk("all message", 0), + _sk("allow-empty message", 0), + _sk("message", 1, _DIRTY_TRACKED, _DIRTY_TRACKED), + _sk("fixup|squash|reuse-message|reedit-message|template|cleanup all", 0), + ), + "reset": ( + _sk("hard", 0, _DIRTY_REVISIONS), + _sk("hard soft|mixed|merge|keep", 0, _DIRTY_REVISIONS), + _sk("soft|mixed|merge|keep", 0, _DIRTY_REVISIONS), + _sk("", 0, _DIRTY_REVISIONS), + _sk("", 2, _DIRTY_REVISIONS, _DIRTY_TRACKED, _DIRTY_TRACKED), + _sk("patch|quiet|refresh|intent-to-add", 0, _DIRTY_REVISIONS), + ), + "clean": ( + _sk("", 0, _DIRTY_UNTRACKED, _DIRTY_UNTRACKED), + _sk("force", 0, _DIRTY_UNTRACKED, _DIRTY_UNTRACKED), + _sk("force d", 0, _DIRTY_UNTRACKED, _DIRTY_UNTRACKED), + _sk("force x|X", 0, _DIRTY_UNTRACKED, _DIRTY_UNTRACKED), + _sk("force d x|X", 0, _DIRTY_UNTRACKED, _DIRTY_UNTRACKED), + _sk("d|x|X", 0, _DIRTY_UNTRACKED, _DIRTY_UNTRACKED), + _sk("force force d", 0, _DIRTY_UNTRACKED, _DIRTY_UNTRACKED), + _sk("force dry-run|interactive|quiet", 0, _DIRTY_UNTRACKED, _DIRTY_UNTRACKED), + _sk("force exclude", 0, _DIRTY_UNTRACKED, _DIRTY_UNTRACKED), + _sk("dry-run d|x", 0, _DIRTY_UNTRACKED, _DIRTY_UNTRACKED), + ), + "checkout": ( + _sk("force", 0, _DIRTY_RESTORE, _DIRTY_RESTORE), + _sk("force|quiet|merge|overlay|ours|theirs|patch", 0, _DIRTY_RESTORE, _DIRTY_RESTORE), + _sk("", 1, _DIRTY_RESTORE, _DIRTY_RESTORE, _DIRTY_RESTORE), + _sk("", 2, _DIRTY_REVISIONS, _DIRTY_RESTORE, _DIRTY_RESTORE), + _sk("force", 2, _DIRTY_REVISIONS, _DIRTY_RESTORE), + ), + "push": ( + _sk("", 1, _ORIGIN, _DIRTY_REFSPECS, _DIRTY_REFSPECS), + _sk("force", 0, _ORIGIN, _DIRTY_REFSPECS, _DIRTY_REFSPECS), + _sk("force set-upstream", 1, _ORIGIN, _DIRTY_REFSPECS), + _sk("force-with-lease", 0, _ORIGIN, _DIRTY_REFSPECS, _DIRTY_REFSPECS), + _sk("force-with-lease force-if-includes", 1, _ORIGIN, _DIRTY_REFSPECS), + _sk( + "force dry-run|atomic|porcelain|quiet|verbose|follow-tags|push-option", + 1, + _ORIGIN, + _DIRTY_REFSPECS, + ), + _sk( + "set-upstream|atomic|quiet|follow-tags|verify|force-if-includes", + 1, + _ORIGIN, + _DIRTY_REFSPECS, + ), + _sk("all|tags|mirror|prune|branches", 1, _ORIGIN), + _sk("all|mirror force|force-with-lease", 1, _ORIGIN), + ), +} + +# Commit, reset, clean and push may take any option of their table except those +# that wait for a terminal, sign, read a pathspec file or redirect the push away +# from origin. Checkout takes only the options that restore paths or force a +# restore: those that create, detach or switch branches stay out. +_DIRTY_STRAYS: Mapping[str, tuple[str, ...]] = { + "commit": tuple( + name + for name in sorted(_GIT_SUBCOMMAND_TABLES["commit"]) + if name + not in ("interactive", "patch", "gpg-sign", "pathspec-from-file", "pathspec-file-nul") + ), + "reset": tuple( + name + for name in sorted(_GIT_SUBCOMMAND_TABLES["reset"]) + if name not in ("pathspec-from-file", "pathspec-file-nul") + ), + "clean": tuple(sorted(_GIT_SUBCOMMAND_TABLES["clean"])), + "checkout": ( + "force", + "quiet", + "merge", + "overlay", + "ours", + "theirs", + "patch", + "guess", + "progress", + "ignore-other-worktrees", + "recurse-submodules", + ), + "push": tuple( + name + for name in sorted(_GIT_SUBCOMMAND_TABLES["push"]) + if name not in ("repo", "receive-pack", "exec", "signed") + ), +} + + +def _dirty_cases() -> st.SearchStrategy[_Case]: + return _argv_cases(_DIRTY_SUBCOMMANDS, _DIRTY_SLOTS, _DIRTY_SKELETONS, _DIRTY_STRAYS) + + +# Spellings of a destructive command, each with a state in which Git acts on it +# or refuses; the classifier must flag every one that acted. +_DESTRUCTIVE_CASES: tuple[_Case, ...] = ( + ("commit", ("--amend",)), + ("commit", ("--amen",)), + ("commit", ("--am", "--no-edit")), + ("commit", ("--amend", "-m", "amended")), + ("commit", ("-a", "--amend")), + ("commit", ("--amend", "d/x")), + ("commit", ("--amend", "--no-amend", "-a", "-m", "x")), + ("commit", ("-a", "-m", "--amend")), + ("commit", ("-a", "-m", "x", "--no-amend")), + ("commit", ("-a", "-m", "x")), + ("commit", ("--allow-empty", "-m", "x")), + ("commit", ("-m", "x")), + ("push", ("-fu", "origin", "develop")), + ("push", ("-qf", "origin", "develop")), + ("push", ("-f", "origin", "develop")), + ("push", ("origin", "develop", "-f")), + ("push", ("--force-w", "origin", "develop")), + ("push", ("--force-with-lease", "origin", "develop")), + ("push", ("--force-with-lease=develop", "origin", "develop")), + ("push", ("origin", "+develop")), + ("push", ("origin", "+HEAD:develop")), + ("push", ("origin", "+feat:develop")), + ("push", ("-f",)), + ("push", ("--forc", "origin", "develop")), + ("push", ("origin", "develop")), + ("push", ("-u", "origin", "develop")), + ("push", ("--force-if-includes", "origin", "develop")), + ("push", ("--recurse-submodules=check", "origin", "develop")), + ("push", ("origin", "+other:review")), + ("push", ("-n", "-f", "origin", "develop")), + ("push", ("origin", "feat:review")), + ("push", ("--mirror", "origin")), + ("push", ("origin", "--mirror")), + ("push", ("--all", "origin")), + ("push", ("--all", "-f", "origin")), + ("reset", ("--hard", "HEAD~")), + ("reset", ("--hard",)), + ("reset", ("--har", "HEAD~")), + ("reset", ("--h", "HEAD~")), + ("reset", ("--hard", "HEAD~2")), + ("reset", ("--hard", "other")), + ("reset", ("--soft", "--hard", "HEAD~")), + ("reset", ("--hard", "--soft", "HEAD~")), + ("reset", ("--hard-foo",)), + ("reset", ("--soft", "HEAD~")), + ("reset", ("--mixed", "HEAD~")), + ("reset", ("--merge", "HEAD~")), + ("reset", ("--keep", "HEAD~")), + ("reset", ("--", "file.txt")), + ("clean", ("--forc",)), + ("clean", ("-f",)), + ("clean", ("-df",)), + ("clean", ("-fd",)), + ("clean", ("-xdf",)), + ("clean", ("-fx",)), + ("clean", ("-fX",)), + ("clean", ("-ff",)), + ("clean", ("-ffd",)), + ("clean", ("-dff",)), + ("clean", ("-f", "-e", "*.tmp")), + ("clean", ("-f", "scratch.tmp")), + ("clean", ("-f", "file.txt")), + ("clean", ("-d",)), + ("clean", ("-n",)), + ("clean", ("-nd",)), + ("clean", ("--dry-run", "-d")), + ("clean", ("-nf",)), + ("clean", ("-i",)), + ("clean", ("-fi",)), + ("clean", ("-f", "-n", "--no-dry-run")), + ("checkout", ("-f",)), + ("checkout", ("--force",)), + ("checkout", ("--forc",)), + ("checkout", ("-f", "--")), + ("checkout", ("--",)), + ("checkout", (".",)), + ("checkout", ("./",)), + ("checkout", (":/",)), + ("checkout", (":(top)",)), + ("checkout", (":(top).",)), + ("checkout", ("--", ".")), + ("checkout", ("--", "./")), + ("checkout", ("--", ":/")), + ("checkout", (".", "--")), + ("checkout", (".", "origin", "main")), + ("checkout", ("HEAD", ".")), + ("checkout", ("feat", ".")), + ("checkout", ("HEAD", "--", ".")), + ("checkout", ("-f", ".")), + ("checkout", ("-q", "--", ".")), + ("checkout", ("--", "file.txt")), + ("checkout", ("--", "d")), + ("checkout", ("file.txt",)), + ("checkout", ("HEAD", "file.txt")), + ("checkout", ("-q",)), + ("checkout", ("feat",)), +) + +# Cleans that act without `-f` because the repository's configuration waives the +# force requirement. +_UNFORCED_CLEAN_CASES: tuple[_Case, ...] = ( + ("clean", ("-d",)), + ("clean", ("-x",)), + ("clean", ("-X",)), + ("clean", ()), + ("clean", ("-dx",)), + ("clean", ("-n",)), + ("clean", ("-i",)), +) + + +def _with_destructive_examples(test: Callable[..., None]) -> Callable[..., None]: + for case in reversed(_UNFORCED_CLEAN_CASES): + test = example(case=case, require_force=False)(test) + for case in reversed(_DESTRUCTIVE_CASES): + test = example(case=case, require_force=True)(test) + return test + + +def _destructive_failure_message(run: _DestructiveRun, risky: tuple[str, ...] | None) -> str: + return ( + f"Property D violated by `{run.command}` (exit {run.returncode})\n" + " expected: a destructive effect Git produced is named by " + "risky_git_operation as a blocked operation\n" + f" actual: effect {run.effect!r} observed but risky_git_operation returned {risky!r}\n" + f" git stderr: {run.stderr.strip()!r}\n" + f" {run.git_version}" + ) + + +@pytest.fixture(scope="module") +def destructive_base(git_base: _Base, tmp_path_factory: pytest.TempPathFactory) -> _Base: + return _build_destructive_base(git_base, tmp_path_factory.mktemp("destructive-base")) + + +@pytest.mark.timeout(300) +@settings(max_examples=200, deadline=None) +@_with_destructive_examples +@given(case=_dirty_cases(), require_force=st.booleans()) +def test_risky_operation_is_detected_for_every_destructive_effect( + destructive_base: _Base, + tmp_path_factory: pytest.TempPathFactory, + case: _Case, + require_force: bool, +) -> None: + subcommand, argv = case + with _sandbox(destructive_base, tmp_path_factory.mktemp) as sandbox: + run = _run_destructive(sandbox, subcommand, argv, require_force=require_force) + risky = risky_git_operation(subcommand, argv, RISKY_GIT_OPERATIONS) + event(f"git:{subcommand}:{'ok' if run.returncode == 0 else 'rejected'}") + event(f"effect:{subcommand}:{run.effect or 'none'}") + if run.effect is not None: + assert risky is not None, _destructive_failure_message(run, risky) + elif risky is not None: + event(f"over-block-d:{subcommand}") diff --git a/tests/hooks/test_git_outcome.py b/tests/hooks/test_git_outcome.py index 15cb45a517..3f6d5a07c2 100644 --- a/tests/hooks/test_git_outcome.py +++ b/tests/hooks/test_git_outcome.py @@ -2,8 +2,9 @@ Members: checkout, switch, branch and symbolic-ref outcome matrices (``git_ref_outcome``), dynamic-token ambiguity rows, the ``parse_update_ref``, -``parse_reset``, ``parse_push`` and ``parse_fetch`` parser rows, and the -``is_ref_mutation`` rows. +``parse_reset``, ``parse_push`` and ``parse_fetch`` parser rows, the +``is_ref_mutation`` rows, and the ``risky_git_operation`` predicate registry and +outcome rows. Rows follow Git 2.43's case partitions. Argv is space-separated shorthand split on whitespace; ``5f3c9e1`` stands for a commit and ``feat`` for a branch name. @@ -11,9 +12,12 @@ from __future__ import annotations +from collections.abc import Mapping + import pytest from autoskillit.hooks._runtime._git_outcome import ( + _RISKY_PREDICATES, GitHeadEffect, git_ref_outcome, is_ref_mutation, @@ -21,7 +25,9 @@ parse_push, parse_reset, parse_update_ref, + risky_git_operation, ) +from autoskillit.hooks._runtime._hook_constants import RISKY_GIT_OPERATIONS pytestmark = [pytest.mark.layer("hooks"), pytest.mark.small] @@ -347,3 +353,222 @@ def test_parse_fetch_reports_the_refspec_destination() -> None: @pytest.mark.parametrize(("subcommand", "argv", "expected"), _REF_MUTATION_ROWS) def test_is_ref_mutation(subcommand: str, argv: str, expected: bool) -> None: assert is_ref_mutation(subcommand, _argv(argv)) is expected + + +def missing_predicates( + predicates: Mapping[tuple[str, ...], object], ops: frozenset[tuple[str, ...]] +) -> set[tuple[str, ...]]: + """The operations in *ops* that *predicates* has no entry for.""" + return set(ops) - set(predicates) + + +def test_risky_predicates_are_keyed_exactly_by_the_risky_operations() -> None: + assert set(_RISKY_PREDICATES) == RISKY_GIT_OPERATIONS + assert not missing_predicates(_RISKY_PREDICATES, RISKY_GIT_OPERATIONS) + + +@pytest.mark.parametrize("op", sorted(RISKY_GIT_OPERATIONS), ids=lambda op: " ".join(op)) +def test_a_predicate_map_missing_one_operation_is_reported(op: tuple[str, ...]) -> None: + canary = {key: predicate for key, predicate in _RISKY_PREDICATES.items() if key != op} + + assert set(canary) != RISKY_GIT_OPERATIONS + assert missing_predicates(canary, RISKY_GIT_OPERATIONS) == {op} + + +_UNRESOLVED = ("",) +_AMEND = ("commit", "--amend") +_FORCE = ("push", "--force") +_FORCE_SHORT = ("push", "-f") +_LEASE = ("push", "--force-with-lease") +_HARD = ("reset", "--hard") +_CLEAN = ("clean", "-f") +_CLEAN_DIRECTORIES = ("clean", "-fd") +_CHECKOUT_TREE = ("checkout", ".") +_CHECKOUT_DASHDASH_TREE = ("checkout", "--", ".") + + +def _risky_cases(expected: tuple[str, ...] | None, commands: tuple[str, ...]) -> list[object]: + """Rows from ``" "`` shorthand that all expect *expected*.""" + cases = [] + for command in commands: + subcommand, _, argv = command.partition(" ") + cases.append(pytest.param(subcommand, argv, expected, id=command)) + return cases + + +_RISKY_ROWS = [ + *_risky_cases( + _FORCE, + ( + "push --force origin main", + "push -f origin main", + "push -fu origin main", + "push -qf origin main", + "push --no-force --force origin main", + "push origin +main", + "push origin +HEAD:main", + "push -u origin main +feat", + ), + ), + *_risky_cases( + _LEASE, + ( + "push --force-with-lease origin main", + "push --force-w origin main", + "push --force-with-lease=main origin main", + ), + ), + *_risky_cases(_HARD, ("reset --hard HEAD~", "reset --har HEAD~", "reset --h HEAD~")), + *_risky_cases(_HARD, ("reset --soft --hard HEAD~", "reset --mixed --hard")), + *_risky_cases( + _CLEAN, + ( + "clean -f", + "clean --forc", + "clean -df", + "clean -xdf", + "clean -fx", + "clean -ff", + "clean -d", + "clean -x", + "clean -n --no-dry-run", + "clean -e pattern -f", + ), + ), + *_risky_cases(_AMEND, ("commit --amend", "commit --amen", "commit --am --no-edit")), + *_risky_cases(_AMEND, ("commit -m x --no-amend --amend",)), + *_risky_cases( + _CHECKOUT_TREE, + ( + "checkout .", + "checkout -f", + "checkout --force", + "checkout ./", + "checkout :/", + "checkout :(top)", + "checkout :(top).", + "checkout . origin main", + "checkout feat .", + ), + ), + *_risky_cases(_CHECKOUT_DASHDASH_TREE, ("checkout -- .", "checkout -- ./", "checkout -- a .")), +] + +_ALLOWED_ROWS = [ + *_risky_cases( + None, + ( + "commit -m --amend", + "commit -m x --no-amend", + "commit --amend --no-amend", + "commit -m x", + "commit -F --amend", + ), + ), + *_risky_cases( + None, + ( + "push origin main", + "push -u origin main", + "push --force-if-includes origin main", + "push --recurse-submodules=check origin main", + "push --force --no-force origin main", + "push --repo=+r origin main", + "push origin main:+refs", + ), + ), + *_risky_cases(None, ("reset --soft HEAD~", "reset --hard --soft HEAD~", "reset HEAD~")), + *_risky_cases( + None, + ( + "clean -n", + "clean -nd", + "clean --dry-run -d", + "clean -i", + "clean --interactive -d", + "clean -fni", + "clean -f -n", + ), + ), + *_risky_cases( + None, + ( + "checkout -- a.txt", + "checkout feature", + "checkout -b x", + "checkout -f feature", + "checkout :/.", + "checkout -p", + "checkout feat -- a.txt", + ), + ), +] + +_UNRESOLVED_ROWS = [ + *_risky_cases(_UNRESOLVED, ("push --forc origin main", "push --bogus", "reset --hard-foo")), + *_risky_cases(_UNRESOLVED, ("commit --bogus", "clean --bogus", "checkout --bogus x")), +] + + +@pytest.mark.parametrize( + ("subcommand", "argv", "expected"), [*_RISKY_ROWS, *_ALLOWED_ROWS, *_UNRESOLVED_ROWS] +) +def test_risky_git_operation_judges_the_outcome_not_the_spelling( + subcommand: str, argv: str, expected: tuple[str, ...] | None +) -> None: + assert risky_git_operation(subcommand, _argv(argv), RISKY_GIT_OPERATIONS) == expected + + +@pytest.mark.parametrize("op", sorted(RISKY_GIT_OPERATIONS), ids=lambda op: " ".join(op)) +def test_each_operation_is_detected_in_its_own_spelling_when_it_is_the_only_one_blocked( + op: tuple[str, ...], +) -> None: + assert risky_git_operation(op[0], list(op[1:]), frozenset({op})) == op + + +@pytest.mark.parametrize( + ("subcommand", "argv", "blocked", "expected"), + [ + pytest.param("push", "-f origin main", RISKY_GIT_OPERATIONS, _FORCE, id="push-f-all"), + pytest.param( + "push", "-f origin main", frozenset({_FORCE_SHORT}), _FORCE_SHORT, id="push-f-short" + ), + pytest.param("push", "-f origin main", frozenset({_FORCE}), _FORCE, id="push-f-long-only"), + pytest.param("clean", "-fd", RISKY_GIT_OPERATIONS, _CLEAN, id="clean-fd-all"), + pytest.param( + "clean", "-fd", frozenset({_CLEAN_DIRECTORIES}), _CLEAN_DIRECTORIES, id="clean-fd-only" + ), + pytest.param("clean", "-f", frozenset({_CLEAN_DIRECTORIES}), None, id="clean-f-not-fd"), + pytest.param("push", "--force origin main", frozenset({_HARD}), None, id="other-op"), + pytest.param("push", "--forc origin main", frozenset({_HARD}), None, id="other-unparsed"), + pytest.param( + "checkout", "-- .", frozenset({_CHECKOUT_TREE}), None, id="dashdash-not-tree" + ), + pytest.param( + "checkout", + "-- .", + frozenset({_CHECKOUT_DASHDASH_TREE}), + _CHECKOUT_DASHDASH_TREE, + id="dashdash-tree", + ), + pytest.param( + "checkout", + ". origin main", + frozenset({_CHECKOUT_DASHDASH_TREE}), + None, + id="tree-not-dashdash", + ), + ], +) +def test_risky_git_operation_reports_only_blocked_operations_deterministically( + subcommand: str, + argv: str, + blocked: frozenset[tuple[str, ...]], + expected: tuple[str, ...] | None, +) -> None: + assert risky_git_operation(subcommand, _argv(argv), blocked) == expected + + +def test_an_unresolved_subcommand_is_reported_unresolved_for_any_blocked_set() -> None: + assert risky_git_operation("", [], RISKY_GIT_OPERATIONS) == _UNRESOLVED + assert risky_git_operation("", [], frozenset({_HARD})) == _UNRESOLVED diff --git a/tests/infra/test_git_ops_guard.py b/tests/infra/test_git_ops_guard.py index 072020c896..e62a8cc767 100644 --- a/tests/infra/test_git_ops_guard.py +++ b/tests/infra/test_git_ops_guard.py @@ -8,9 +8,11 @@ from __future__ import annotations +import functools import json import os import shlex +import shutil import subprocess import sys from collections.abc import Mapping @@ -26,6 +28,7 @@ from autoskillit.hooks._runtime._git_command_classification import _classify_fetch from autoskillit.hooks._runtime._git_option_tables import _GIT_FETCH_FLAG_SPEC from autoskillit.hooks.guards import git_ops_guard +from tests._git_topology import commit_file, init_checkout from tests.infra._git_ops_guard_harness import ( _TOOL_NAME, _checked_out_ref_result, @@ -323,10 +326,9 @@ def test_denies_nested_shell_git_amend(self, tmp_path): assert _is_denied(out) def test_denies_env_prefix_git_amend(self, tmp_path): - # rectify #4941 Part A: _contains_blocked_git_op now resolves the verb - # through command_verb_and_args, which skips POSIX assignments the - # same way the checked-out-ref preflight always has -- an env-prefixed - # invocation (VAR=1 git ...) is no longer a raw-shlex-split blind spot. + # contains_blocked_git_op resolves the verb through the evaluated-segment + # walk, which skips POSIX assignments the same way the checked-out-ref + # preflight does, so an env-prefixed invocation (VAR=1 git ...) is seen. out = _run_guard("VAR=1 git commit --amend", kitchen_open=True, tmpdir=tmp_path) assert _is_denied(out) @@ -1578,6 +1580,252 @@ def test_verdict_is_the_same_in_every_session_shape( assert verdicts == dict.fromkeys(_SESSION_SHAPES, verdict) +@functools.cache +def _git_version() -> str: + return subprocess.run( + ["git", "--version"], capture_output=True, text=True, check=False + ).stdout.strip() + + +def _context(command: str, observed: object = "") -> str: + return f"{command!r} {observed!r} ({_git_version()})" + + +def _deny_reason(output: str) -> str: + return json.loads(output)["hookSpecificOutput"]["permissionDecisionReason"] + + +def _destructive_reason(op: tuple[str, ...]) -> str: + return git_ops_guard._DENY_REASON_TEMPLATE.format(op=" ".join(("git", *op))) + + +_AMEND = ("commit", "--amend") +_FORCE_PUSH = ("push", "--force") +_LEASE_PUSH = ("push", "--force-with-lease") +_HARD_RESET = ("reset", "--hard") +_FORCE_CLEAN = ("clean", "-f") +_CHECKOUT_TREE = ("checkout", ".") +_CHECKOUT_DASHDASH_TREE = ("checkout", "--", ".") +_UNRESOLVED_OP = ("",) + + +def _git_rows(op: tuple[str, ...], *tails: str) -> list[tuple[str, tuple[str, ...]]]: + return [(f"git {tail}", op) for tail in tails] + + +_DESTRUCTIVE_ROWS = [ + *_git_rows( + _FORCE_PUSH, + "push -fu origin main", + "push -qf origin main", + "push origin +main", + "push origin +HEAD:main", + ), + *_git_rows( + _LEASE_PUSH, "push --force-w origin main", "push --force-with-lease=main origin main" + ), + *_git_rows(_HARD_RESET, "reset --har HEAD~", "reset --h HEAD~"), + *_git_rows( + _FORCE_CLEAN, + "clean --forc", + "clean -df", + "clean -xdf", + "clean -fx", + "clean -ff", + "clean -d", + "-c clean.requireForce=0 clean -d", + ), + *_git_rows(_AMEND, "commit --amen", "commit --am --no-edit"), + ("git-commit --amend", _AMEND), + *_git_rows( + _CHECKOUT_TREE, "checkout -f", "checkout ./", "checkout :/", "checkout . origin main" + ), + *_git_rows(_CHECKOUT_DASHDASH_TREE, "checkout -- ./"), + *_git_rows(_UNRESOLVED_OP, "push --forc origin main", "reset --hard-foo"), +] + +_DESTRUCTIVE_ALLOWED = [ + "git commit -m '--amend'", + "git commit -m x --no-amend", + "git push origin main", + "git push -u origin main", + "git push --force-if-includes origin main", + "git push --recurse-submodules=check origin main", + "git reset --soft HEAD~", + "git clean -n", + "git clean -nd", + "git clean --dry-run -d", + "git clean -i", + "git checkout -- a.txt", + "git checkout feature", + "git -c user.name=x commit -m x", +] + +_DENIED_SHAPE = (True, "skill") +_EXEMPT_SHAPES = tuple(shape for shape in _SESSION_SHAPES if shape != _DENIED_SHAPE) +_SHAPE_PARITY_COMMANDS = ( + "git push origin +main", + "git reset --har HEAD~", + "git clean -d", + "git commit --am --no-edit", + "git checkout ./", + "git push --forc origin main", +) + +_REPOSITORY_ALIAS_ROWS = [ + ("git ci --amend", _AMEND), + ("git pf origin main", _FORCE_PUSH), +] +_UNKNOWN_IDENTITY_COMMANDS = [ + "git sh", + "P=commit; git $P --amend", + "git -c alias.x='commit --amend' x", +] + + +@pytest.mark.skipif(shutil.which("git") is None, reason="git binary required") +class TestDestructiveOutcomeImmunity: + """The headless destructive-operation phase matches the operations + RISKY_GIT_OPERATIONS names by what they do, not by how their options are + spelled. A plain directory is no repository, so the checked-out-ref preflight + has nothing to decide and the phase alone does. + """ + + @pytest.fixture(autouse=True) + def _isolated_git_environment(self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: + home = tmp_path / "home" + home.mkdir() + for name in ( + "GIT_DIR", + "GIT_WORK_TREE", + "GIT_COMMON_DIR", + "GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", + ): + monkeypatch.delenv(name, raising=False) + monkeypatch.setenv("GIT_CONFIG_GLOBAL", os.devnull) + monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1") + monkeypatch.setenv("HOME", str(home)) + monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config")) + + @pytest.fixture + def aliased_repo(self, tmp_path: Path) -> Path: + """A checkout on ``main`` with a separate bare ``origin`` that is fetched, + and repository aliases for commit, a forced push and a shell command.""" + repo = init_checkout(tmp_path / "repo") + _git(repo, "branch", "-m", "main") + commit_file(repo, "tracked.txt", "initial\n", "initial") + origin = tmp_path / "origin.git" + _git(tmp_path, "init", "--bare", str(origin)) + _git(repo, "remote", "add", "origin", str(origin)) + _git(repo, "push", "-q", "origin", "main") + _git(repo, "fetch", "-q", "origin") + for alias, expansion in ( + ("ci", "commit"), + ("pf", "push --force"), + ("sh", "!git commit --amend"), + ): + _git(repo, "config", f"alias.{alias}", expansion) + return repo + + def test_the_plain_directory_is_not_inside_a_repository(self, tmp_path: Path) -> None: + result = subprocess.run( + ["git", "-C", str(tmp_path), "rev-parse", "--git-dir"], + capture_output=True, + text=True, + check=False, + ) + + assert result.returncode != 0, _context("rev-parse in the plain directory", result.stdout) + + @pytest.mark.parametrize(("command", "op"), _DESTRUCTIVE_ROWS) + def test_denies_each_spelling_with_the_destructive_reason( + self, tmp_path: Path, command: str, op: tuple[str, ...] + ) -> None: + out = _run_guard(command, kitchen_open=True, tmpdir=tmp_path) + + assert _guard_verdict(out) == "other-deny", _context(command, out) + assert _deny_reason(out) == _destructive_reason(op), _context(command, out) + + @pytest.mark.parametrize("command", _DESTRUCTIVE_ALLOWED) + def test_allows_each_spelling_that_destroys_nothing( + self, tmp_path: Path, command: str + ) -> None: + out = _run_guard(command, kitchen_open=True, tmpdir=tmp_path) + + assert _guard_verdict(out) == "allow", _context(command, out) + + @pytest.mark.parametrize( + "command", [command for command, _ in _DESTRUCTIVE_ROWS] + _DESTRUCTIVE_ALLOWED + ) + def test_allows_every_row_in_the_orchestrator_tier(self, tmp_path: Path, command: str) -> None: + out = _run_guard(command, kitchen_open=True, tmpdir=tmp_path, session_type="orchestrator") + + assert _guard_verdict(out) == "allow", _context(command, out) + + @pytest.mark.parametrize(("headless", "session_type"), _EXEMPT_SHAPES) + @pytest.mark.parametrize("command", _SHAPE_PARITY_COMMANDS) + def test_allows_the_destructive_rows_outside_a_headless_skill_session( + self, tmp_path: Path, command: str, headless: bool, session_type: str + ) -> None: + out = _run_guard( + command, + kitchen_open=True, + tmpdir=tmp_path, + headless=headless, + session_type=session_type, + ) + + assert _guard_verdict(out) == "allow", _context(command, (headless, session_type, out)) + + @pytest.mark.parametrize(("command", "op"), _REPOSITORY_ALIAS_ROWS) + def test_denies_a_repository_alias_for_a_destructive_operation( + self, aliased_repo: Path, command: str, op: tuple[str, ...] + ) -> None: + out = _run_guard(command, kitchen_open=True, tmpdir=aliased_repo) + + assert _guard_verdict(out) == "other-deny", _context(command, out) + assert _deny_reason(out) == _destructive_reason(op), _context(command, out) + + @pytest.mark.parametrize(("headless", "session_type"), _EXEMPT_SHAPES) + @pytest.mark.parametrize("command", [command for command, _ in _REPOSITORY_ALIAS_ROWS]) + def test_allows_a_repository_alias_outside_a_headless_skill_session( + self, aliased_repo: Path, command: str, headless: bool, session_type: str + ) -> None: + out = _run_guard( + command, + kitchen_open=True, + tmpdir=aliased_repo, + headless=headless, + session_type=session_type, + ) + + assert _guard_verdict(out) == "allow", _context(command, (headless, session_type, out)) + + @pytest.mark.parametrize(("headless", "session_type"), _SESSION_SHAPES) + @pytest.mark.parametrize("command", _UNKNOWN_IDENTITY_COMMANDS) + def test_denies_a_command_of_unknown_identity_in_every_session_shape( + self, aliased_repo: Path, command: str, headless: bool, session_type: str + ) -> None: + out = _run_guard( + command, + kitchen_open=True, + tmpdir=aliased_repo, + headless=headless, + session_type=session_type, + ) + + assert _guard_verdict(out) == "checked-out-ref", _context( + command, (headless, session_type, out) + ) + assert _checked_out_ref_result(out)["attempted_value"] == "" + + @pytest.mark.parametrize("command", ["git lfs version", "cd $X && git lfs pull"]) + def test_allows_an_external_subcommand(self, aliased_repo: Path, command: str) -> None: + out = _run_guard(command, kitchen_open=True, tmpdir=aliased_repo) + + assert _guard_verdict(out) == "allow", _context(command, out) + + class TestGuardFailsClosed: """An unexpected classification error blocks (exit 2, reason on stderr).""" @@ -1611,7 +1859,7 @@ def test_error_in_blocked_operation_match_blocks( def _raise(*_args: object, **_kwargs: object) -> None: raise KeyError("injected matcher failure") - monkeypatch.setattr(git_ops_guard, "_contains_blocked_git_op", _raise) + monkeypatch.setattr(git_ops_guard, "contains_blocked_git_op", _raise) code = _guard_exit_code("git status", tmp_path) @@ -1676,25 +1924,26 @@ def test_every_git_fetch_spec_flag_is_recognized(name: str) -> None: def test_git_ops_guard_classify_imports_from_new_sibling() -> None: """Every classification primitive the orchestrator consumes from - autoskillit.hooks._runtime._git_command_classification must remain - reachable from the package import surface, as must the remote-identity - helper `_same_repository` resolves through. Mirrors the orchestrator's - import block in git_ops_guard.py. The fetch option table the classifier - reads lives in _git_option_tables as OptionDef rows. + autoskillit.hooks._runtime._git_command_classification, and the blocked + operation matcher it consumes from autoskillit.hooks._runtime._git_target, + must remain reachable from the package import surface, as must the + remote-identity helper `_same_repository` resolves through. Mirrors the + orchestrator's import block in git_ops_guard.py. The fetch option table the + classifier reads lives in _git_option_tables as OptionDef rows. """ from autoskillit.hooks._runtime._git_command_classification import ( _classify_git_invocation, - _contains_blocked_git_op, _git_result, _git_text, _parse_worktree_owners, _resolve_attempted_sha, _resolve_git_common_dir, ) + from autoskillit.hooks._runtime._git_target import contains_blocked_git_op expected_callables = ( _classify_git_invocation, - _contains_blocked_git_op, + contains_blocked_git_op, _git_result, _git_text, _parse_worktree_owners, @@ -1702,7 +1951,7 @@ def test_git_ops_guard_classify_imports_from_new_sibling() -> None: _resolve_git_common_dir, ) for symbol in expected_callables: - assert callable(symbol), f"_git_command_classification.{symbol.__name__} must be callable" + assert callable(symbol), f"{symbol.__name__} must be callable" assert isinstance(_GIT_FETCH_FLAG_SPEC, Mapping) assert all(isinstance(row, OptionDef) for row in _GIT_FETCH_FLAG_SPEC.values()) diff --git a/tests/infra/test_risky_git_ops_coverage.py b/tests/infra/test_risky_git_ops_coverage.py index a2a0df9a13..298788d91c 100644 --- a/tests/infra/test_risky_git_ops_coverage.py +++ b/tests/infra/test_risky_git_ops_coverage.py @@ -14,6 +14,7 @@ import pytest from autoskillit.hook_registry import HOOK_REGISTRY, RISKY_GIT_OPERATIONS +from autoskillit.hooks._runtime._git_target import contains_blocked_git_op from tests._evaluation_shape_matrix import EVALUATION_SHAPE_MATRIX, wrap_git_op pytestmark = [pytest.mark.layer("infra"), pytest.mark.small] @@ -131,22 +132,17 @@ def test_risky_git_operations_authority_is_hook_constants() -> None: @pytest.mark.parametrize("op", sorted(RISKY_GIT_OPERATIONS), ids=lambda op: "-".join(op)) @pytest.mark.parametrize("shape", _EXECUTING_SHAPES, ids=lambda s: s.id) def test_every_risky_git_op_is_detected_through_every_evaluation_shape(shape, op) -> None: - """rectify #4941 Part A: every RISKY_GIT_OPERATIONS tuple, delivered through - every executing evaluation shape (direct, -c, eval, heredoc, herestring, - pipe, substitution, Python subprocess), must still be denied. + """Every RISKY_GIT_OPERATIONS tuple, delivered through every executing + evaluation shape (direct, -c, eval, heredoc, herestring, pipe, substitution, + Python subprocess), is detected as itself when it is the only blocked + operation, and is detected when all of them are blocked. """ - from autoskillit.hooks._runtime._git_command_classification import ( - _contains_blocked_git_op, - ) - cmd = wrap_git_op(shape, op) - result = _contains_blocked_git_op(cmd, RISKY_GIT_OPERATIONS) - # Some RISKY_GIT_OPERATIONS tuples structurally overlap once rendered - # with a trailing "origin main" (e.g. ("checkout", ".") also matches - # wherever ("checkout", "--", ".") does), so any detected tuple -- - # not necessarily the exact one parametrized -- proves this shape was - # correctly denied. - assert result is not None, f"{shape.id}: {op} not detected through {cmd!r}" + alone = contains_blocked_git_op(cmd, frozenset({op})) + assert alone == op, f"{shape.id}: {op} not detected through {cmd!r} (got {alone!r})" + assert contains_blocked_git_op(cmd, RISKY_GIT_OPERATIONS) is not None, ( + f"{shape.id}: {op} not detected through {cmd!r} with every operation blocked" + ) @pytest.mark.parametrize("op", sorted(RISKY_GIT_OPERATIONS), ids=lambda op: "-".join(op)) @@ -156,10 +152,6 @@ def test_inert_shapes_never_flag_a_risky_git_op(shape, op) -> None: never executes it must never trip the blocklist, however the delivery shape happens to render the operation's text. """ - from autoskillit.hooks._runtime._git_command_classification import ( - _contains_blocked_git_op, - ) - cmd = wrap_git_op(shape, op) - result = _contains_blocked_git_op(cmd, RISKY_GIT_OPERATIONS) + result = contains_blocked_git_op(cmd, RISKY_GIT_OPERATIONS) assert result is None, f"{shape.id}: {op} incorrectly denied through {cmd!r} (got {result!r})" From 6434f5441f537cce5a4ff75204b40fa59b668beb Mon Sep 17 00:00:00 2001 From: Trecek Date: Thu, 5 Nov 2026 15:35:31 -0800 Subject: [PATCH 05/46] Keep revision spellings out of the whole-tree checkout pathspecs The whole-tree pathspec normalization stripped the top-of-tree magic from :/. as well, so checkout :/. (a revision named by message that detaches HEAD) was treated as a destructive whole-tree restore. The :/ magic now counts only before nothing or a wildcard. Also declares the cook launch tests' dependency on the hooks package in the file-level test cascade map. Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/_classification/_interpreters.py | 66 ++-- .../_classification/_invocation_flags.py | 349 ++++++++++++++++++ .../_python_program_analysis.py | 23 +- src/autoskillit/hooks/_runtime/__init__.py | 12 - .../hooks/_runtime/_command_classification.py | 200 ++++------ .../hooks/_runtime/_git_outcome.py | 4 +- .../hooks/_runtime/_shell_segment_walk.py | 18 +- src/autoskillit/hooks/guards/git_ops_guard.py | 9 + tests/_test_filter.py | 2 + 9 files changed, 482 insertions(+), 201 deletions(-) create mode 100644 src/autoskillit/hooks/_classification/_invocation_flags.py diff --git a/src/autoskillit/hooks/_classification/_interpreters.py b/src/autoskillit/hooks/_classification/_interpreters.py index aefd23aede..9edd711334 100644 --- a/src/autoskillit/hooks/_classification/_interpreters.py +++ b/src/autoskillit/hooks/_classification/_interpreters.py @@ -11,6 +11,12 @@ if TYPE_CHECKING: from autoskillit.hooks._classification._flag_arity_classification import _FlagArity + from autoskillit.hooks._classification._invocation_flags import ( + _PYTHON_INVOCATION_FLAG_SPEC, + _SHELL_INVOCATION_FLAG_SPEC, + python_c_program, + shell_c_payload, + ) from autoskillit.hooks._classification._python_program_analysis import ( _InterpreterCommandSpec, _python_c_program, @@ -40,8 +46,6 @@ tokenize_command_segments, ) - _SHELL_INVOCATION_FLAG_SPEC: dict[str, _FlagArity] - _PYTHON_INVOCATION_FLAG_SPEC: dict[str, _FlagArity] else: # _flag_arity_classification, _python_program_analysis, and # _substitution_scanning are leaf siblings (no back-reference into the @@ -56,12 +60,14 @@ if __package__: from . import ( _flag_arity_classification, + _invocation_flags, _python_program_analysis, _substitution_scanning, _tokenizer, ) else: import _flag_arity_classification + import _invocation_flags import _python_program_analysis import _substitution_scanning import _tokenizer @@ -77,34 +83,10 @@ ) _scan_shell_payload_segment_groups = _substitution_scanning._iter_shell_payload_segment_groups _iter_substitution_occurrences = _substitution_scanning._iter_substitution_occurrences - - # Flags that consume a following value when the shell/Python interpreter - # itself (not a heredoc/herestring body) reads a script from a - # positional operand. Only VALUE-arity flags are listed: any other - # `-`/`+`-prefixed token is skipped as boolean by the default bucket in - # `_walk_invocation_flags`. Defined here (rather than at the bottom of - # the file) now that _FlagArity no longer forces a deferred, re-entrant - # import -- see the comment above. - _SHELL_INVOCATION_FLAG_SPEC: dict[str, _FlagArity] = { - "-o": _FlagArity.VALUE, - "+o": _FlagArity.VALUE, - "-O": _FlagArity.VALUE, - "+O": _FlagArity.VALUE, - "--rcfile": _FlagArity.VALUE, - "--init-file": _FlagArity.VALUE, - # Bash 5.2 help does not print --rcfile/--init-file's operand arity; - # covered by the generative flag-spec test instead of the - # live-`--help` contract test (see - # tests/hooks/test_gh_api_flag_spec_contract.py). - "-c": _FlagArity.VALUE, - } - _PYTHON_INVOCATION_FLAG_SPEC: dict[str, _FlagArity] = { - "-c": _FlagArity.VALUE, - "-m": _FlagArity.VALUE, - "-W": _FlagArity.VALUE, - "-X": _FlagArity.VALUE, - "--check-hash-based-pycs": _FlagArity.VALUE, - } + _SHELL_INVOCATION_FLAG_SPEC = _invocation_flags._SHELL_INVOCATION_FLAG_SPEC + _PYTHON_INVOCATION_FLAG_SPEC = _invocation_flags._PYTHON_INVOCATION_FLAG_SPEC + shell_c_payload = _invocation_flags.shell_c_payload + python_c_program = _invocation_flags.python_c_program def has_interpreter_write(command: str) -> bool: @@ -481,8 +463,11 @@ def evaluated_payloads(command: str) -> list[EvaluatedPayload]: for index, segment in enumerate(segments): verb, args = command_verb_and_args(list(segment.tokens)) - if _is_shell_interpreter(verb) and args and args[0] == "-c" and len(args) >= 2: - payloads.append(EvaluatedPayload(args[1], StdinConsumer.SHELL, index, "-c", None)) + shell_program = shell_c_payload(args)[0] if _is_shell_interpreter(verb) else None + if shell_program is not None: + payloads.append( + EvaluatedPayload(shell_program, StdinConsumer.SHELL, index, "-c", None) + ) elif verb == "eval" and args: payloads.append( EvaluatedPayload(" ".join(args), StdinConsumer.SHELL, index, "eval", None) @@ -698,17 +683,12 @@ def _extract_interpreter_segment_specs( specs: list[_InterpreterCommandSpec] = [] has_unresolved = False - try: - command_index = args.index("-c") - except ValueError: - command_index = None - if command_index is not None: - if command_index + 1 >= len(args): - has_unresolved = True - else: - found, unresolved = _python_program_command_specs(args[command_index + 1]) - specs.extend(found) - has_unresolved = has_unresolved or unresolved + program, program_unresolved = python_c_program(args) + has_unresolved = program_unresolved + if program is not None: + found, unresolved = _python_program_command_specs(program) + specs.extend(found) + has_unresolved = has_unresolved or unresolved if stdin_literals and stdin_consumer(list(segment)) == StdinConsumer.PYTHON: for literal in stdin_literals: diff --git a/src/autoskillit/hooks/_classification/_invocation_flags.py b/src/autoskillit/hooks/_classification/_invocation_flags.py new file mode 100644 index 0000000000..9d29d2ec89 --- /dev/null +++ b/src/autoskillit/hooks/_classification/_invocation_flags.py @@ -0,0 +1,349 @@ +"""Invocation grammar for the commands that run other commands. + +Members: _SHELL_INVOCATION_FLAG_SPEC, _PYTHON_INVOCATION_FLAG_SPEC, +_SHELL_INVOCATION_BOOLEAN_FLAGS, _PYTHON_INVOCATION_BOOLEAN_FLAGS, +shell_c_payload, python_c_program, _WrapperSpec, _WRAPPER_SPECS, +_wrapper_command_start, _wrapper_chdir_value, _env_splits_string, +_launcher_position_unresolved. + +A shell or Python ``-c`` program is found through its bundled, stuck and +value-taking options (``bash -lc``, ``python3 -Bc``, ``-W error -c``), and a +wrapper's command word through its own option table. Anything the tables do not +model is *unresolved*: the position of the command it runs cannot be proven, +which is reported out of band so shared consumers keep their verb. + +Leaf module: imports only ``_flag_arity_classification``. +""" + +from __future__ import annotations + +import os +import re +from collections.abc import Mapping, Sequence +from typing import TYPE_CHECKING, NamedTuple + +if TYPE_CHECKING: + from autoskillit.hooks._classification._flag_arity_classification import _FlagArity +else: + if __package__: + from . import _flag_arity_classification + else: + import _flag_arity_classification + + _FlagArity = _flag_arity_classification._FlagArity + +# Flags that consume a following value when the shell/Python interpreter itself +# (not a heredoc/herestring body) reads a script from a positional operand. Only +# VALUE-arity flags are listed: any other `-`/`+`-prefixed token is skipped as +# boolean by the default bucket in `_interpreters._walk_invocation_flags`. +_SHELL_INVOCATION_FLAG_SPEC: dict[str, _FlagArity] = { + "-o": _FlagArity.VALUE, + "+o": _FlagArity.VALUE, + "-O": _FlagArity.VALUE, + "+O": _FlagArity.VALUE, + "--rcfile": _FlagArity.VALUE, + "--init-file": _FlagArity.VALUE, + # Bash 5.2 help does not print --rcfile/--init-file's operand arity; + # covered by the generative flag-spec test instead of the + # live-`--help` contract test (see + # tests/hooks/test_gh_api_flag_spec_contract.py). + "-c": _FlagArity.VALUE, +} +_PYTHON_INVOCATION_FLAG_SPEC: dict[str, _FlagArity] = { + "-c": _FlagArity.VALUE, + "-m": _FlagArity.VALUE, + "-W": _FlagArity.VALUE, + "-X": _FlagArity.VALUE, + "--check-hash-based-pycs": _FlagArity.VALUE, +} +# Long boolean options. Short boolean letters need no entry: an unknown short +# letter is skipped as a boolean. +_SHELL_INVOCATION_BOOLEAN_FLAGS: frozenset[str] = frozenset( + { + "--debug", + "--norc", + "--noprofile", + "--login", + "--posix", + "--restricted", + "--verbose", + "--noediting", + "--dump-strings", + "--dump-po-strings", + "--version", + "--help", + } +) +_PYTHON_INVOCATION_BOOLEAN_FLAGS: frozenset[str] = frozenset({"--help", "--version"}) +_SHELL_VALUE_LETTERS = "oO" +_PYTHON_VALUE_LETTERS = "WX" + + +def shell_c_payload(args: Sequence[str]) -> tuple[str | None, bool]: + """The ``-c`` program of a shell's argv as ``(program, unresolved)``. + + A bundle containing ``c`` selects payload mode and the program is the first + operand. ``o``/``O`` (and ``+o``/``+O``) take a value. An unknown long + option, or ``-c`` with no program, is unresolved. + """ + payload_mode = False + index = 0 + while index < len(args): + token = args[index] + if token == "--": + index += 1 + break + if token.startswith("--"): + if token in _SHELL_INVOCATION_BOOLEAN_FLAGS: + index += 1 + elif token in _SHELL_INVOCATION_FLAG_SPEC: + index += 2 + else: + return None, True + continue + if token[:1] not in ("-", "+") or len(token) == 1: + break + letters = token[1:] + payload_mode = payload_mode or "c" in letters + index += 2 if letters[-1] in _SHELL_VALUE_LETTERS else 1 + if not payload_mode: + return None, False + return (args[index], False) if index < len(args) else (None, True) + + +def _python_short_cluster(args: Sequence[str], index: int) -> tuple[str, str | None, int]: + """Scan the short options of ``args[index]`` as ``(status, program, next_index)``. + + *status* is ``program`` (``-c`` found), ``stop`` (``-m`` ends the scan), + ``unresolved`` (``-c`` has no program) or ``continue``. + """ + letters = args[index][1:] + for position, letter in enumerate(letters): + rest = letters[position + 1 :] + if letter == "c": + if rest: + return "program", rest, index + 1 + if index + 1 < len(args): + return "program", args[index + 1], index + 2 + return "unresolved", None, index + 1 + if letter == "m": + return "stop", None, index + 1 + if letter in _PYTHON_VALUE_LETTERS: + return "continue", None, index + (1 if rest else 2) + return "continue", None, index + 1 + + +def python_c_program(args: Sequence[str]) -> tuple[str | None, bool]: + """The ``-c`` program of a Python argv as ``(program, unresolved)``. + + Handles bundles (``-Bc``), a stuck program (``-cPROGRAM``) and the + value-taking ``-W``/``-X``. ``-m`` or a script operand ends the scan with no + program. An unknown long option, or ``-c`` with no program, is unresolved. + """ + index = 0 + while index < len(args): + token = args[index] + if token == "--" or token[:1] != "-" or len(token) == 1: + return None, False + if token.startswith("--"): + if token in _PYTHON_INVOCATION_BOOLEAN_FLAGS: + index += 1 + elif token in _PYTHON_INVOCATION_FLAG_SPEC: + index += 2 + else: + return None, True + continue + status, program, index = _python_short_cluster(args, index) + if status == "program": + return program, False + if status in ("stop", "unresolved"): + return None, status == "unresolved" + return None, False + + +class _WrapperSpec(NamedTuple): + booleans: frozenset[str] + values: frozenset[str] + leading_operands: int + chdir: frozenset[str] + + +_NO_OPTIONS: frozenset[str] = frozenset() + +# Each row was checked against the binary's own `--help` (coreutils 9.4, sudo +# 1.9). An option whose short form carries two meanings (`sudo -h` is `--help` +# and `--host`) stays out of the table so it falls to the unmodelled rule: a +# modelled-but-wrong arity silently mislocates the command word, while an +# unmodelled option fails closed. +_WRAPPER_SPECS: Mapping[str, _WrapperSpec] = { + "timeout": _WrapperSpec( + frozenset({"--foreground", "--preserve-status", "-v", "--verbose"}), + frozenset({"-k", "--kill-after", "-s", "--signal"}), + 1, + _NO_OPTIONS, + ), + "sudo": _WrapperSpec( + frozenset( + { + "-n", + "--non-interactive", + "-E", + "--preserve-env", + "-H", + "--set-home", + "-S", + "--stdin", + "-b", + "--background", + "-i", + "--login", + "-s", + "--shell", + } + ), + frozenset( + { + "-u", + "--user", + "-g", + "--group", + "-C", + "--close-from", + "-p", + "--prompt", + "-r", + "--role", + "-t", + "--type", + "-T", + "--command-timeout", + "-U", + "--other-user", + } + ), + 0, + frozenset({"-D", "--chdir"}), + ), + "nice": _WrapperSpec(_NO_OPTIONS, frozenset({"-n", "--adjustment"}), 0, _NO_OPTIONS), + "time": _WrapperSpec( + frozenset({"-p", "-a", "-v", "-q"}), + frozenset({"-f", "--format", "-o", "--output"}), + 0, + _NO_OPTIONS, + ), + "stdbuf": _WrapperSpec( + _NO_OPTIONS, + frozenset({"-i", "-o", "-e", "--input", "--output", "--error"}), + 0, + _NO_OPTIONS, + ), + "command": _WrapperSpec(frozenset({"-p"}), _NO_OPTIONS, 0, _NO_OPTIONS), + "exec": _WrapperSpec(frozenset({"-c", "-l"}), frozenset({"-a"}), 0, _NO_OPTIONS), + "nohup": _WrapperSpec(_NO_OPTIONS, _NO_OPTIONS, 0, _NO_OPTIONS), +} + + +def _wrapper_option_width(spec: _WrapperSpec, token: str) -> int | None: + """Tokens a wrapper option occupies (1 or 2), or None when the table does not model it.""" + values = spec.values | spec.chdir + if "=" in token: + return 1 + if token in spec.booleans: + return 1 + if token in values: + return 2 + if not token.startswith("--") and len(token) > 2 and token[:2] in values: + return 1 + return None + + +def _wrapper_command_start(argv: Sequence[str], index: int) -> tuple[int, bool]: + """Index of the command word a wrapper at *index* runs, and whether that is unresolved. + + ``--`` ends wrapper options. Any other option the wrapper's table does not + model stops the walk at the wrapper word and reports the position unresolved. + ``command -v`` describes a command rather than running one, so it has none. + """ + name = argv[index] + spec = _WRAPPER_SPECS[name] + position = index + 1 + while position < len(argv): + token = argv[position] + if token == "--": + position += 1 + break + if token == "-" or not token.startswith("-"): + break + if name == "command" and token in ("-v", "-V"): + return len(argv), False + width = _wrapper_option_width(spec, token) + if width is None: + return index, True + position += width + return position + spec.leading_operands, False + + +def _wrapper_chdir_value(argv: Sequence[str], index: int) -> tuple[str, int, int] | None: + """A wrapper's last chdir value (``sudo -D``) as ``(value, token_index, prefix_length)``.""" + chdir = _WRAPPER_SPECS[argv[index]].chdir + end, _unresolved = _wrapper_command_start(argv, index) + found: tuple[str, int, int] | None = None + position = index + 1 + while position < end: + head, has_value, value = argv[position].partition("=") + if has_value and head in chdir: + found = (value, position, len(head) + 1) + elif argv[position] in chdir and position + 1 < end: + found = (argv[position + 1], position + 1, 0) + position += 1 + elif len(argv[position]) > 2 and argv[position][:2] in chdir: + found = (argv[position][2:], position, 2) + position += 1 + return found + + +def _env_splits_string(tokens: Sequence[str]) -> bool: + """Whether the consumed ``env`` options include ``-S``/``--split-string``.""" + return any(token.startswith(("-S", "--split-string")) for token in tokens) + + +_OPAQUE_LAUNCHERS: frozenset[str] = frozenset( + { + "xargs", + "watch", + "parallel", + "uvx", + "setsid", + "ionice", + "taskset", + "chrt", + "flock", + } +) +_FIND_EXEC_FLAGS: frozenset[str] = frozenset({"-exec", "-execdir", "-ok", "-okdir"}) +_EVAL_INTERPRETERS: frozenset[str] = frozenset({"node", "perl", "ruby"}) +_EVAL_FLAGS: frozenset[str] = frozenset({"-e", "-E", "--eval"}) +_SHELL_NAME_RE = re.compile(r"(?:bash|sh|zsh|dash)[\d.]*", re.IGNORECASE) +_PYTHON_NAME_RE = re.compile(r"python(?:3(?:\.\d+)?)?", re.IGNORECASE) + + +def _launcher_position_unresolved(argv: Sequence[str]) -> bool: + """Whether *argv* (starting at its command word) runs a command that cannot be located. + + Opaque launchers (``xargs``, ``find -exec``, ``uv run``, ``node -e``, ...) and a + shell or Python ``-c`` whose program cannot be found hide the command they + run. The launcher word stays the verb for every shared consumer. + """ + name = os.path.basename(argv[0]) + args = argv[1:] + if name in _OPAQUE_LAUNCHERS: + return True + if name in ("uv", "pipx"): + return args[:1] == ["run"] + if name == "find": + return any(arg in _FIND_EXEC_FLAGS for arg in args) + if name in _EVAL_INTERPRETERS: + return any(arg in _EVAL_FLAGS for arg in args) + if _SHELL_NAME_RE.fullmatch(name): + return shell_c_payload(args)[1] + return bool(_PYTHON_NAME_RE.fullmatch(name)) and python_c_program(args)[1] diff --git a/src/autoskillit/hooks/_classification/_python_program_analysis.py b/src/autoskillit/hooks/_classification/_python_program_analysis.py index a5291dc90a..9f4212dc32 100644 --- a/src/autoskillit/hooks/_classification/_python_program_analysis.py +++ b/src/autoskillit/hooks/_classification/_python_program_analysis.py @@ -2,10 +2,10 @@ Split out of `_interpreters.py` (rectify #4941 Part A) to keep that module's stdin-consumer/evaluated-payload machinery under the REQ-CNST-010 line cap. -Self-contained: parses a Python -c program (or a heredoc/herestring-fed -`python3`/`python3 -` body) via the stdlib `ast` module and returns any -literal-argv or literal-string `subprocess.*`/`os.*` call it finds, with no -dependency on the rest of `_classification/`. +Parses a Python -c program (or a heredoc/herestring-fed `python3`/`python3 -` +body) via the stdlib `ast` module and returns any literal-argv or literal-string +`subprocess.*`/`os.*` call it finds. Locating the `-c` program in an argv is the +`_invocation_flags` leaf's job; nothing else of `_classification/` is imported. """ from __future__ import annotations @@ -14,6 +14,15 @@ import re from collections.abc import Sequence from dataclasses import dataclass +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from autoskillit.hooks._classification._invocation_flags import python_c_program +else: + if __package__: + from ._invocation_flags import python_c_program + else: + from _invocation_flags import python_c_program _PYTHON_SUBPROCESS_FUNCS: frozenset[str] = frozenset( { @@ -101,11 +110,7 @@ def _python_c_program(executable: str, args: Sequence[str]) -> str | None: """Return the literal program passed to a normalized Python executable's -c.""" if re.fullmatch(r"python(?:3(?:\.\d+)?)?", executable) is None: return None - try: - index = args.index("-c") - except ValueError: - return None - return args[index + 1] if index + 1 < len(args) else None + return python_c_program(args)[0] def _python_program_command_specs( diff --git a/src/autoskillit/hooks/_runtime/__init__.py b/src/autoskillit/hooks/_runtime/__init__.py index cdbd73299a..56c486c6c1 100644 --- a/src/autoskillit/hooks/_runtime/__init__.py +++ b/src/autoskillit/hooks/_runtime/__init__.py @@ -53,7 +53,6 @@ "TOKEN_USAGE_FILE_KEYS": "_hook_settings", "_AUTOSKILLIT_LOG_DIR_ENV": "_hook_settings", "_CLAIM_PREFIX": "_exploration_request_record", - "_COMMAND_WRAPPERS": "_command_classification", "_CURL_FLAG_SPEC": "_github_mutation_analysis", "_CommandSegment": "_command_classification", "_DIRECTORY_FLAGS": "_exploration_request_record", @@ -110,10 +109,6 @@ "_TOKEN_PATTERN": "_exploration_request_record", "_V1_TOKEN_FIELD_ALIASES": "_hook_settings", "_WC_FLAG_RE": "_command_classification", - "_WRAPPERS_WITH_DURATION": "_command_classification", - "_WRAPPERS_WITH_SHORT_FLAG": "_command_classification", - "_WRAPPER_VALUE_FLAGS_ATTACHED": "_command_classification", - "_WRAPPER_VALUE_FLAGS_DETACHED": "_command_classification", "_WRITE_APIS_RE": "_command_classification", "_WRITE_CALL_SITE_RE": "_command_classification", "_WRITE_FLAGS": "_exploration_request_record", @@ -134,7 +129,6 @@ "_consume_env": "_command_classification", "_consume_str_flag": "_command_classification", "_spec_key_for_token": "_command_classification", - "_consume_wrapper_options": "_command_classification", "_default_state_root": "_hook_settings", "_extract_interpreter_command_specs": "_command_classification", "_extract_interpreter_segment_specs": "_command_classification", @@ -277,7 +271,6 @@ "TOKEN_USAGE_FILE_KEYS", "_AUTOSKILLIT_LOG_DIR_ENV", "_CLAIM_PREFIX", - "_COMMAND_WRAPPERS", "_CURL_FLAG_SPEC", "_CommandSegment", "_DIRECTORY_FLAGS", @@ -334,10 +327,6 @@ "_TOKEN_PATTERN", "_V1_TOKEN_FIELD_ALIASES", "_WC_FLAG_RE", - "_WRAPPERS_WITH_DURATION", - "_WRAPPERS_WITH_SHORT_FLAG", - "_WRAPPER_VALUE_FLAGS_ATTACHED", - "_WRAPPER_VALUE_FLAGS_DETACHED", "_WRITE_APIS_RE", "_WRITE_CALL_SITE_RE", "_WRITE_FLAGS", @@ -358,7 +347,6 @@ "_consume_env", "_consume_str_flag", "_spec_key_for_token", - "_consume_wrapper_options", "_default_state_root", "_extract_interpreter_command_specs", "_extract_interpreter_segment_specs", diff --git a/src/autoskillit/hooks/_runtime/_command_classification.py b/src/autoskillit/hooks/_runtime/_command_classification.py index 531c156878..4f81c36a8c 100644 --- a/src/autoskillit/hooks/_runtime/_command_classification.py +++ b/src/autoskillit/hooks/_runtime/_command_classification.py @@ -94,18 +94,6 @@ def tokenize_command_segments(command: str) -> list[list[str]]: ) -# Command wrappers whose only effect is to invoke the next command with -# adjusted environment/priority. The verb is the token after the wrapper. -# 'xargs' is intentionally excluded: it dispatches a downstream reader and -# adding it would let `xargs cat src/.../foo.yaml` reach the reader check, -# weakening xargs-chain bypass detection (see D1 design decision). -_COMMAND_WRAPPERS: frozenset[str] = frozenset({"command", "nice", "time", "sudo", "nohup"}) -# Wrappers that consume a mandatory DURATION as their first non-wrapper token. -_WRAPPERS_WITH_DURATION: frozenset[str] = frozenset({"timeout"}) -# Wrappers that take a single short flag as their first non-wrapper token -# (e.g. 'stdbuf -o0', 'stdbuf -i0', 'stdbuf -e0'). -_WRAPPERS_WITH_SHORT_FLAG: frozenset[str] = frozenset({"stdbuf"}) - # env option arity tables. _ENV_NO_VALUE_FLAGS: frozenset[str] = frozenset( { @@ -128,9 +116,6 @@ def tokenize_command_segments(command: str) -> list[list[str]]: "--chdir", "-S", "--split-string", - "--default-signal", - "--ignore-signal", - "--block-signal", "--argv0", } ) @@ -147,35 +132,6 @@ def tokenize_command_segments(command: str) -> list[list[str]]: "--block-signal", } ) -# Wrappers taking a value optionally attached ('-u=root', '--user=root'). -_WRAPPER_VALUE_FLAGS_ATTACHED: frozenset[str] = frozenset( - { - "-u", - "--user", - "-n", - "--adjustment", - "-k", - "--kill-after", - "-s", - "--signal", - } -) -_WRAPPER_VALUE_FLAGS_DETACHED: frozenset[str] = frozenset( - { - "-u", - "--user", - "-n", - "--adjustment", - "-k", - "--kill-after", - "-s", - "--signal", - "-g", - "--group", - "-p", - "--priority", - } -) class SearchPattern(Protocol): @@ -358,31 +314,6 @@ def _env_chdir_value(start: int, segment: list[str]) -> tuple[str, int, int] | N return chdir -def _consume_wrapper_options(start: int, segment: list[str]) -> int: - """Skip past a wrapper's attached/detached value options. - - Wrappers (sudo, nice, nohup, time, command) accept option flags before - the inner command. Many take values either attached (--user=root) or as - the next detached token (-u root). Returns the index of the first - non-option token. - """ - i = start - while i < len(segment): - token = segment[i] - if token == "--": - return i + 1 - if not token.startswith("-"): - return i - if "=" in token: - i += 1 - continue - if token in _WRAPPER_VALUE_FLAGS_DETACHED and i + 1 < len(segment): - i += 2 - continue - i += 1 - return i - - def _command_start_index(segment: list[str]) -> int | None: if not segment: return None @@ -395,67 +326,60 @@ def _command_start_index(segment: list[str]) -> int | None: if token == "env": start = _consume_env(start + 1, segment) continue - if token in _COMMAND_WRAPPERS: - start += 1 - continue - if token in _WRAPPERS_WITH_DURATION and start + 1 < len(segment): - start += 2 - continue - if ( - token in _WRAPPERS_WITH_SHORT_FLAG - and start + 1 < len(segment) - and segment[start + 1].startswith("-") - ): - start += 2 + if token in _WRAPPER_SPECS: + start, unresolved = _wrapper_command_start(segment, start) + if unresolved: + break continue break return start if start < len(segment) else None -def _verb_start_index(segment: list[str]) -> int | None: - """Return the index of the command verb in *segment*, skipping shell - - keywords (while/until/if/do/then/elif/else), POSIX assignments, `env`, - and wrapper prefixes (sudo/nice/timeout/...). Returns None when the segment is - empty or ends inside a wrapper (missing a required value) -- the same - cases command_verb_and_args reports as ("", []). Exposed separately so - a caller threading a second, index-aligned parallel array (e.g. - ArgvToken quote provenance) can slice it at the same start point - command_verb_and_args computes for *segment* itself, rather than - re-deriving that decision independently. +def _command_position(segment: list[str]) -> tuple[int | None, bool]: + """Return the index of the command verb in *segment* and whether that is unresolved. + + Skips shell keywords (while/until/if/do/then/elif/else), POSIX assignments, `env` + and wrapper prefixes through each wrapper's own option table. The index is None + when the segment is empty or ends inside a wrapper. The second value is True when + the verb cannot be proven: a wrapper option the table does not model (the walk + stops at the wrapper word), `env -S`, an opaque launcher (`xargs`, `find -exec`, + `uv run`, ...) or a shell/Python `-c` program that cannot be located. It travels + out of band so shared consumers keep their verb. """ if not segment: - return None + return None, False start = 0 + unresolved = False while start < len(segment): token = segment[start] if token in {"while", "until", "if", "do", "then", "elif", "else", "!"}: start += 1 - continue - if _is_posix_assignment(token): + elif _is_posix_assignment(token): start += 1 - continue - if token == "env": - start = _consume_env(start + 1, segment) - continue - if token in _COMMAND_WRAPPERS: - # Wrappers (sudo, nice, etc.) may consume attached/detached value options. - start = _consume_wrapper_options(start + 1, segment) - continue - if token in _WRAPPERS_WITH_DURATION: - if start + 1 >= len(segment): - return None - start += 2 - continue - if token in _WRAPPERS_WITH_SHORT_FLAG: - if start + 1 >= len(segment): - return None - if not segment[start + 1].startswith("-"): - return None - start += 2 - continue - break - return start if start < len(segment) else None + elif token == "env": + end = _consume_env(start + 1, segment) + unresolved = unresolved or _env_splits_string(segment[start + 1 : end]) + start = end + elif token in _WRAPPER_SPECS: + start, wrapper_unresolved = _wrapper_command_start(segment, start) + if wrapper_unresolved: + return start, True + else: + break + if start >= len(segment): + return None, unresolved + return start, unresolved or _launcher_position_unresolved(segment[start:]) + + +def _verb_start_index(segment: list[str]) -> int | None: + """Return the index of the command verb in *segment*, or None when there is none. + + Exposed separately so a caller threading a second, index-aligned parallel array + (e.g. ArgvToken quote provenance) can slice it at the same start point + command_verb_and_args computes for *segment* itself, rather than re-deriving + that decision independently. See `_command_position` for the skipped prefixes. + """ + return _command_position(segment)[0] def _command_position_candidate_spans(segment: Sequence[str]) -> tuple[tuple[int, int], ...]: @@ -592,8 +516,6 @@ def extract_git_subcommand_and_flags(segment: list[str]) -> GitInvocation | None is_allowed_protected_path_metadata_command, ) from autoskillit.hooks._classification._interpreters import ( - _PYTHON_INVOCATION_FLAG_SPEC, - _SHELL_INVOCATION_FLAG_SPEC, EvaluatedPayload, StdinConsumer, _extract_interpreter_segment_specs, @@ -620,6 +542,15 @@ def extract_git_subcommand_and_flags(segment: list[str]) -> GitInvocation | None from autoskillit.hooks._classification._interpreters import ( live_command_text as _live_command_text_impl, ) + from autoskillit.hooks._classification._invocation_flags import ( + _PYTHON_INVOCATION_FLAG_SPEC, + _SHELL_INVOCATION_FLAG_SPEC, + _WRAPPER_SPECS, + _env_splits_string, + _launcher_position_unresolved, + _wrapper_chdir_value, + _wrapper_command_start, + ) from autoskillit.hooks._classification._output_redirect import ( _FD_DUPLICATION_RE, _REDIRECT_OP_ONLY_RE, @@ -633,9 +564,9 @@ def extract_git_subcommand_and_flags(segment: list[str]) -> GitInvocation | None ) else: if __package__: - from .._classification import _flags, _interpreters, _output_redirect + from .._classification import _flags, _interpreters, _invocation_flags, _output_redirect else: - from _classification import _flags, _interpreters, _output_redirect + from _classification import _flags, _interpreters, _invocation_flags, _output_redirect _GIT_ADD_CONTENT_FLAGS = _flags._GIT_ADD_CONTENT_FLAGS _GIT_DIFF_CONTENT_FLAGS = _flags._GIT_DIFF_CONTENT_FLAGS @@ -678,8 +609,13 @@ def extract_git_subcommand_and_flags(segment: list[str]) -> GitInvocation | None ) _interpreter_invokes_impl = _interpreters.interpreter_invokes _live_command_text_impl = _interpreters.live_command_text - _SHELL_INVOCATION_FLAG_SPEC = _interpreters._SHELL_INVOCATION_FLAG_SPEC - _PYTHON_INVOCATION_FLAG_SPEC = _interpreters._PYTHON_INVOCATION_FLAG_SPEC + _SHELL_INVOCATION_FLAG_SPEC = _invocation_flags._SHELL_INVOCATION_FLAG_SPEC + _PYTHON_INVOCATION_FLAG_SPEC = _invocation_flags._PYTHON_INVOCATION_FLAG_SPEC + _WRAPPER_SPECS = _invocation_flags._WRAPPER_SPECS + _env_splits_string = _invocation_flags._env_splits_string + _launcher_position_unresolved = _invocation_flags._launcher_position_unresolved + _wrapper_chdir_value = _invocation_flags._wrapper_chdir_value + _wrapper_command_start = _invocation_flags._wrapper_command_start OutputRedirectPartition = _output_redirect.OutputRedirectPartition _FD_DUPLICATION_RE = _output_redirect._FD_DUPLICATION_RE _REDIRECT_OP_ONLY_RE = _output_redirect._REDIRECT_OP_ONLY_RE @@ -759,16 +695,20 @@ def _wrapped_verb_cwd( cwd: str, argv_tokens: Sequence[ArgvToken] | None = None, ) -> str: - """Resolve env chdir options for the executable without moving shell redirects.""" + """Resolve env/sudo chdir options for the executable without moving shell redirects.""" for index, token in enumerate(segment[:start]): if token == "env": chdir = _env_chdir_value(index + 1, segment) - if chdir is not None: - value, value_index, prefix_length = chdir - source = _shell_source(argv_tokens, value_index) - if source is not None: - source = source[prefix_length:] - cwd = resolve_write_target(value, cwd, shell_source=source) or "" + elif token in _WRAPPER_SPECS: + chdir = _wrapper_chdir_value(segment, index) + else: + continue + if chdir is not None: + value, value_index, prefix_length = chdir + source = _shell_source(argv_tokens, value_index) + if source is not None: + source = source[prefix_length:] + cwd = resolve_write_target(value, cwd, shell_source=source) or "" return cwd diff --git a/src/autoskillit/hooks/_runtime/_git_outcome.py b/src/autoskillit/hooks/_runtime/_git_outcome.py index 38e8603c93..6c0c1aadfa 100644 --- a/src/autoskillit/hooks/_runtime/_git_outcome.py +++ b/src/autoskillit/hooks/_runtime/_git_outcome.py @@ -374,7 +374,9 @@ def is_ref_mutation(subcommand: str, args: Sequence[str]) -> bool: return False -_PATHSPEC_MAGIC_RE = re.compile(r"^:(?:/|\([a-z,]*\))?") +# ``:/`` is top-of-tree magic only before nothing or a wildcard: ``:/.`` and other +# ``:/`` name a commit by message, a revision rather than a pathspec. +_PATHSPEC_MAGIC_RE = re.compile(r"^:(?:/(?=\*|$)|\([a-z,]*\))?") def _is_whole_tree_pathspec(token: str) -> bool: diff --git a/src/autoskillit/hooks/_runtime/_shell_segment_walk.py b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py index 0068ce5c7d..fea6ce4643 100644 --- a/src/autoskillit/hooks/_runtime/_shell_segment_walk.py +++ b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py @@ -376,7 +376,13 @@ def _walk_executable( ) -> tuple[WalkedSegment, ShellDirectoryState | None]: _classification = _classification_module() - def walked(start: int | None, verb_cwd: str, certain: bool, handled: bool) -> WalkedSegment: + def walked( + start: int | None, + verb_cwd: str, + certain: bool, + handled: bool, + position_unresolved: bool = False, + ) -> WalkedSegment: return WalkedSegment( record, partition, @@ -386,16 +392,16 @@ def walked(start: int | None, verb_cwd: str, certain: bool, handled: bool) -> Wa verb_cwd, certain and bool(verb_cwd), handled, - False, + position_unresolved, ) - start = _classification._verb_start_index(executable) + start, unresolved = _classification._command_position(executable) setting = _cdpath_setting(executable, start) if executable_tokens is not None else None previous_cdpath = state.cdpath_unknown if setting is not None: state.cdpath_unknown = setting[0] if start is None: - return walked(None, state.cwd, state.cwd_certain, False), None + return walked(None, state.cwd, state.cwd_certain, False, unresolved), None parent_certain = state.cwd_certain handled = _apply_shell_builtin(executable, start, executable_tokens, state) if setting is not None and setting[1]: @@ -406,14 +412,14 @@ def walked(start: int | None, verb_cwd: str, certain: bool, handled: bool) -> Wa state.cwd_certain = False state.chain_pending = False if handled or _classification.is_gh_command(executable): - return walked(start, state.cwd, state.cwd_certain, handled), None + return walked(start, state.cwd, state.cwd_certain, handled, unresolved), None verb_cwd = _classification._wrapped_verb_cwd(executable, start, state.cwd, executable_tokens) absolute_override = _classification._wrapped_verb_cwd(executable, start, "", executable_tokens) verb_certain = bool(verb_cwd) and (bool(absolute_override) or state.cwd_certain) invoked = _invoked_child_state( executable[start], verb_cwd, verb_certain, state, record.subshell_path ) - return walked(start, verb_cwd, verb_certain, False), invoked + return walked(start, verb_cwd, verb_certain, False, unresolved), invoked def walk_evaluated_segments(command: str, cwd: str) -> list[WalkedSegment] | None: diff --git a/src/autoskillit/hooks/guards/git_ops_guard.py b/src/autoskillit/hooks/guards/git_ops_guard.py index 089f715688..83783555f3 100644 --- a/src/autoskillit/hooks/guards/git_ops_guard.py +++ b/src/autoskillit/hooks/guards/git_ops_guard.py @@ -510,6 +510,15 @@ def _preflight_segment( anchor: Callable[[], str | None] | None, ) -> None: if seg.position_unresolved and _mentions_git_mutation(" ".join(seg.executable)): + if anchor is not None: + _deny_primary_head( + data=data, + cwd=seg.verb_cwd, + subcommand="", + head_effect=str(GitHeadEffect.AMBIGUOUS), + reason="unresolved_target", + protected_primary="", + ) _deny_unresolved(data) verb = os.path.basename(seg.executable[seg.start]) if seg.start is not None else "" if verb in _PACK_VERBS: diff --git a/tests/_test_filter.py b/tests/_test_filter.py index c4efdb6224..2de6d481e1 100644 --- a/tests/_test_filter.py +++ b/tests/_test_filter.py @@ -1537,6 +1537,8 @@ class SourceMapRejection(enum.StrEnum): "cli/test_capture_store.py", # file-level: open-kitchen bridge drives the real registry path (no in-memory stub) "cli/test_resume_registry_identity.py", + # file-level: asserts the launched cook env against the hook identity predicates + "cli/test_cook_interactive.py", "recipe", "server/test_kitchen_lifecycle.py", "server/test_tools_kitchen_gate.py", From 05531c9fde90ea6c3370cbe9e50d325b8401d215 Mon Sep 17 00:00:00 2001 From: Trecek Date: Thu, 5 Nov 2026 15:36:43 -0800 Subject: [PATCH 06/46] Resolve command position through per-wrapper grammar Phase E of the git outcome and effective-target immunity work (#4751). The source for this phase landed in the preceding commit; this commit adds its tests and documentation. Shell and Python -c programs are found through their bundled, stuck and value-taking options (bash -lc, python3 -Bc, -W error -c), and a wrapper's command word through its own option table (timeout -k 5 10, sudo -n, stdbuf -o L, nice -n 5, time -p, exec -a n, env --ignore-signal). Anything the tables do not model, and opaque launchers such as xargs, find -exec, uv run, setsid and node -e, make the command position unresolved. That signal travels out of band on the walked segment, so the other guards keep their verb, while git_ops_guard denies an unresolved position only when the command also mentions a Git mutation. Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/AGENTS.md | 6 + .../hooks/_classification/AGENTS.md | 7 +- src/autoskillit/hooks/guards/AGENTS.md | 5 + tests/hooks/test_command_classification.py | 274 +++++++++++ tests/hooks/test_hook_executability.py | 4 +- tests/hooks/test_invocation_flags.py | 426 ++++++++++++++++++ tests/hooks/test_test_runner_guard.py | 11 + tests/infra/test_git_ops_guard.py | 178 ++++++++ tests/infra/test_unsafe_install_guard.py | 2 + 9 files changed, 911 insertions(+), 2 deletions(-) create mode 100644 tests/hooks/test_invocation_flags.py diff --git a/src/autoskillit/hooks/AGENTS.md b/src/autoskillit/hooks/AGENTS.md index dba53548ea..289b469ff1 100644 --- a/src/autoskillit/hooks/AGENTS.md +++ b/src/autoskillit/hooks/AGENTS.md @@ -61,6 +61,12 @@ That inventory test makes any new raw scan under `hooks/` a conscious, reviewed `StdinLiteral` is the second instance of the `ArgvToken` "tag once at tokenization, consume tagged provenance downstream" pattern (`_tokenizer.py`, commit `6624dda71`, issue #4680): future token-level provenance should extend this path rather than add a parallel parser. +Invocation `-c` extraction (`bash -lc`, `python3 -Bc`) and wrapper command-position arity +(`_WRAPPER_SPECS`) live in `_classification/_invocation_flags.py`. Where the command a +wrapper, interpreter or launcher runs cannot be located, the position is *unresolved*; that +signal travels out of band (`WalkedSegment.position_unresolved`) so every shared consumer +keeps its verb. Git option semantics are decided through `_option_grammar` tables; +`tests/arch/test_git_option_literal_scope.py` enforces this. ## Multi-concern files diff --git a/src/autoskillit/hooks/_classification/AGENTS.md b/src/autoskillit/hooks/_classification/AGENTS.md index cf084ebc2a..5f0da20682 100644 --- a/src/autoskillit/hooks/_classification/AGENTS.md +++ b/src/autoskillit/hooks/_classification/AGENTS.md @@ -28,6 +28,11 @@ block B bootstrap so hook scripts can keep importing via referenced by `_flags.py` and `_interpreters.py`. Lifted out so neither classification module owns the cross-import that would otherwise create a bidirectional `_flags` ↔ `_interpreters` coupling. +- `_invocation_flags.py` — leaf holding the shell and Python invocation specs, the + bundle-aware `-c` extractors (`shell_c_payload`, `python_c_program`) and the + per-wrapper command-position tables (`_WRAPPER_SPECS`, `_wrapper_command_start`). + A wrapper option or launcher it does not model is reported *unresolved* out of + band, so shared consumers keep their verb. - `_python_program_analysis.py` — Python interpreter and subprocess invocation analysis (`_InterpreterCommandSpec`, `_python_c_program`, `_python_program_command_specs`, `_python_program_evaluated_specs`). @@ -64,7 +69,7 @@ the facade's relative imports and any bare-name bootstrap (hook scripts that push `hooks/_classification/` onto `sys.path`) resolve the same symbols. Within-folder imports of leaf siblings (`_tokenizer.py`, `_flag_arity_classification.py`, -`_source_map.py`, `_option_grammar.py`) +`_source_map.py`, `_option_grammar.py`, `_invocation_flags.py`) stay at module top-level — they have no back-references into the facade or into other `_classification/` modules, so the relative-import dance isn't required. diff --git a/src/autoskillit/hooks/guards/AGENTS.md b/src/autoskillit/hooks/guards/AGENTS.md index e3e235172e..5da4e855b0 100644 --- a/src/autoskillit/hooks/guards/AGENTS.md +++ b/src/autoskillit/hooks/guards/AGENTS.md @@ -58,6 +58,11 @@ The package initializer remains import-free. by its ancestor, so clone in a separate command first. Worked example: a recipe step that runs `git checkout ` in the cook session's own checkout (the `checkout` step of `full-audit`) is denied by design; point `workspace` at a linked worktree. + Opaque launchers (`xargs`, `find -exec`, `uv run`, `setsid`, `node -e`, ...) and wrapper + options the tables do not model fail closed only when the command also mentions a Git + mutation; Git-mutation text quoted inside such a launcher's arguments (for example + `find . -exec grep -l "git push" {} \;`) is therefore denied too, and the remedy is + to run the inner command directly (`grep -rl "git push" .`). In headless skill sessions, the later legacy Git phase blocks `commit --amend`, `push --force`, `reset --hard`, `clean -f`, and `checkout .`. It matches those operations by outcome rather than by spelling (abbreviated, bundled and diff --git a/tests/hooks/test_command_classification.py b/tests/hooks/test_command_classification.py index 2e6fdadc3c..7fe36ec45d 100644 --- a/tests/hooks/test_command_classification.py +++ b/tests/hooks/test_command_classification.py @@ -24,7 +24,10 @@ PROTECTED_SOURCE_PATH_PATTERNS, OutputRedirectPartition, StdinConsumer, + _command_position, _FlagArity, + _verb_start_index, + _wrapped_verb_cwd, all_evaluated_segments, command_has_blocked_protected_path_read, command_verb, @@ -53,6 +56,7 @@ GitHubMutationStatus, analyze_github_mutations, ) +from autoskillit.hooks._runtime._shell_segment_walk import walk_evaluated_segments from tests._evaluation_shape_matrix import ( EVALUATION_SHAPE_MATRIX, wrap_git_op, @@ -894,6 +898,206 @@ def test_command_verb_delegates_to_command_verb_and_args(self): verb_from_helper, _ = command_verb_and_args(seg) assert verb_from_helper == command_verb(seg) + @pytest.mark.parametrize( + ("command", "index"), + [ + pytest.param(command, index, id=command) + for command, index in [ + ("timeout -k 5 10 git s", 4), + ("timeout --kill-after=5 10 git s", 3), + ("timeout -s KILL 10 git s", 4), + ("sudo -n git s", 2), + ("sudo -u root git s", 3), + ("sudo -E -- git s", 3), + ("nice -n 5 git s", 3), + ("time -p git s", 2), + ("stdbuf -o L git s", 3), + ("stdbuf -oL git s", 2), + ("nohup git s", 1), + ("command git s", 1), + ("exec -a n git s", 3), + ("env --ignore-signal git s", 2), + ("env --ignore-signal=INT git s", 2), + ("env -i A=1 git s", 3), + ("sudo -- git s", 2), + ("env -- git s", 2), + ("nice -- git s", 2), + ("then git push", 1), + ] + ], + ) + def test_wrapper_options_are_skipped_to_the_command_word(self, command, index): + segment = command.split() + assert _command_position(segment) == (index, False) + assert _verb_start_index(segment) == index + assert segment[index] == "git" + + def test_command_v_describes_a_command_rather_than_running_one(self): + segment = ["command", "-v", "git"] + assert _command_position(segment) == (None, False) + assert _verb_start_index(segment) is None + assert command_verb_and_args(segment) == ("", []) + + @pytest.mark.parametrize( + ("command", "index"), + [ + pytest.param(command, index, id=command) + for command, index in [ + ("sudo -nu root git s", 0), + ("timeout --bogus 10 git s", 0), + ("sudo -h git s", 0), + ("FOO=1 sudo -h git s", 1), + ("env A=1 timeout --bogus 10 git s", 2), + ("timeout 10 sudo -nu root git s", 2), + ] + ], + ) + def test_unmodelled_wrapper_option_stops_at_the_wrapper_word(self, command, index): + segment = command.split() + assert _command_position(segment) == (index, True) + assert _verb_start_index(segment) == index + assert command_verb_and_args(segment)[0] == segment[index] + + @pytest.mark.parametrize( + ("command", "index"), + [ + pytest.param(command, index, id=command) + for command, index in [ + ("xargs git push", 0), + ("xargs -n1 gh pr view", 0), + ("find . -exec git s {} ;", 0), + ("find . -execdir git s {} ;", 0), + ("find . -ok git s {} ;", 0), + ("find . -okdir git s {} ;", 0), + ("watch git s", 0), + ("parallel git ::: s", 0), + ("uv run git s", 0), + ("uv run pytest -k git", 0), + ("uvx t", 0), + ("pipx run t", 0), + ("setsid git s", 0), + ("ionice -c3 git s", 0), + ("taskset 1 git s", 0), + ("chrt -o 0 git s", 0), + ("flock /tmp/l git s", 0), + ("node -e P", 0), + ("perl -e P", 0), + ("ruby -e P", 0), + ("bash --bogus -c P", 0), + ("bash -c", 0), + ("python3 --bogus -c P", 0), + ("python3 -c", 0), + ("timeout 10 xargs git push", 2), + ("sudo -n uv run pytest", 2), + ] + ], + ) + def test_opaque_launcher_is_unresolved_and_keeps_its_own_word_as_the_verb( + self, command, index + ): + segment = command.split() + assert _command_position(segment) == (index, True) + assert _verb_start_index(segment) == index + assert command_verb_and_args(segment) == (segment[index], segment[index + 1 :]) + + @pytest.mark.parametrize( + ("segment", "index", "verb"), + [ + pytest.param(["env", "-S", "git s"], None, "", id="split-string-is-the-whole-command"), + pytest.param( + ["env", "-u", "FOO", "--chdir", "/tmp", "-S", "x", "pip", "install"], + 7, + "pip", + id="split-string-short", + ), + pytest.param( + ["env", "--split-string", "FOOBAR", "pip", "install"], + 3, + "pip", + id="split-string-long", + ), + pytest.param(["env", "-S", "x", "git", "s"], 3, "git", id="split-string-then-git"), + ], + ) + def test_env_split_string_keeps_its_verb_and_is_unresolved(self, segment, index, verb): + assert _command_position(segment) == (index, True) + assert command_verb_and_args(segment)[0] == verb + + @pytest.mark.parametrize( + ("command", "index"), + [ + pytest.param(command, index, id=command) + for command, index in [ + ("sudo --user=root rm /tmp/f", 2), + ("find . -name x", 0), + ("find . -type f -print", 0), + ("uv pip install x", 0), + ("bash -lc P", 0), + ("sh -ec P", 0), + ("python3 -Bc P", 0), + ("python3 -m pip install x", 0), + ] + ], + ) + def test_position_is_resolved(self, command, index): + assert _command_position(command.split()) == (index, False) + + @pytest.mark.parametrize( + ("command", "expected"), + [ + pytest.param(command, expected, id=command) + for command, expected in [ + ("env -C /x git s", "/x"), + ("sudo -D /x git s", "/x"), + ("sudo --chdir=/x git s", "/x"), + ("sudo -D/x git s", "/x"), + ("sudo -D sub git s", "/base/sub"), + ("sudo -D /a -D /b git s", "/b"), + ("env -C /a sudo -D /b git s", "/b"), + ("sudo -D /b env -C /a git s", "/a"), + ("sudo -n git s", "/base"), + ("timeout 10 git s", "/base"), + ] + ], + ) + def test_wrapped_verb_cwd_applies_the_wrappers_chdir_options(self, command, expected): + segment = command.split() + start = _verb_start_index(segment) + assert start is not None + assert _wrapped_verb_cwd(segment, start, "/base") == expected + + @pytest.mark.parametrize( + ("command", "unresolved"), + [ + pytest.param(command, unresolved, id=command) + for command, unresolved in [ + ("xargs git push", True), + ("sudo -nu root git s", True), + ("sudo -h git s", True), + ("timeout --bogus 10 git s", True), + ("bash --bogus -c 'git s'", True), + ("python3 --bogus -c 'x = 1'", True), + ("env -S 'git s'", True), + ("find . -exec git s {} \\;", True), + ("uv run git s", True), + ("setsid git s", True), + ("node -e 'x'", True), + ("bash -lc 'git s'", False), + ("python3 -Bc 'x = 1'", False), + ("timeout -k 5 10 git s", False), + ("sudo -n git s", False), + ("sudo --user=root rm /tmp/f", False), + ("env -i A=1 git s", False), + ("find . -name x", False), + ("bash script.sh", False), + ] + ], + ) + def test_walk_reports_an_unresolved_command_position(self, tmp_path, command, unresolved): + walked = walk_evaluated_segments(command, str(tmp_path)) + assert walked is not None + assert any(segment.position_unresolved for segment in walked) is unresolved + class TestCommandPositionCandidateSpans: @pytest.mark.parametrize( @@ -1068,6 +1272,76 @@ def test_dollar_substitution_inside_bash_heredoc_remains_evaluated(self): found = [command_verb_and_args(segment) for segment in segments] assert _GIT_PUSH_FORCE_ARGV in found + _UPDATE_REF_PROGRAM = "git update-ref refs/heads/x y" + _UPDATE_REF_ARGV = ["git", "update-ref", "refs/heads/x", "y"] + _PYTHON_UPDATE_REF_PROGRAM = ( + 'import subprocess; subprocess.run(["git", "update-ref", "refs/heads/x", "y"])' + ) + + @pytest.mark.parametrize( + "invocation", + [ + "bash -lc", + "bash -xc", + "bash -ec", + "bash -e -c", + "bash --norc -c", + "bash --noprofile -c", + "bash --login -c", + "bash -O extglob -c", + "bash +O extglob -c", + "bash -c --", + "bash -D -c", + "sh -ec", + ], + ) + def test_shell_c_invocation_forms_expose_the_program_segment(self, invocation): + segments = all_evaluated_segments(f"{invocation} {shlex.quote(self._UPDATE_REF_PROGRAM)}") + assert segments is not None + assert self._UPDATE_REF_ARGV in segments + + @pytest.mark.parametrize( + "invocation", + [ + "python3 -c", + "python3 -Bc", + "python3 -Ic", + "python3 -I -c", + "python3 -R -c", + "python3 -t -c", + "python3 -W error -c", + "python3 -Werror -c", + "python3 -X dev -c", + ], + ) + def test_python_c_invocation_forms_expose_the_program_segment(self, invocation): + segments = all_evaluated_segments( + f"{invocation} {shlex.quote(self._PYTHON_UPDATE_REF_PROGRAM)}" + ) + assert segments is not None + assert self._UPDATE_REF_ARGV in segments + + def test_python_c_program_stuck_to_the_flag_exposes_the_program_segment(self): + segments = all_evaluated_segments( + f"python3 -c{shlex.quote(self._PYTHON_UPDATE_REF_PROGRAM)}" + ) + assert segments is not None + assert self._UPDATE_REF_ARGV in segments + + @pytest.mark.parametrize( + "command", + [ + f"bash script.sh {shlex.quote(_UPDATE_REF_PROGRAM)}", + f"bash -s {shlex.quote(_UPDATE_REF_PROGRAM)}", + f"python3 script.py {shlex.quote(_PYTHON_UPDATE_REF_PROGRAM)}", + f"python3 -m pip {shlex.quote(_UPDATE_REF_PROGRAM)}", + ], + ) + def test_interpreter_operands_are_not_programs(self, command): + segments = all_evaluated_segments(command) + assert segments is not None + assert self._UPDATE_REF_ARGV not in segments + @pytest.mark.parametrize( ("tokens", "expected"), [ diff --git a/tests/hooks/test_hook_executability.py b/tests/hooks/test_hook_executability.py index c5478a6a21..c20e2d302f 100644 --- a/tests/hooks/test_hook_executability.py +++ b/tests/hooks/test_hook_executability.py @@ -43,7 +43,9 @@ def _extract_hook_commands() -> list[str]: return commands -@pytest.mark.parametrize("module_name", ["_flags", "_interpreters"]) +@pytest.mark.parametrize( + "module_name", ["_flags", "_interpreters", "_option_grammar", "_invocation_flags"] +) def test_classification_submodule_imports_standalone(module_name: str) -> None: hooks_dir = pkg_root() / "hooks" runtime_dir = hooks_dir / "_runtime" diff --git a/tests/hooks/test_invocation_flags.py b/tests/hooks/test_invocation_flags.py new file mode 100644 index 0000000000..2248a48114 --- /dev/null +++ b/tests/hooks/test_invocation_flags.py @@ -0,0 +1,426 @@ +"""Invocation grammar of hooks/_classification/_invocation_flags.py. + +Members: shell_c_payload and python_c_program matrices (each row is the argv after +the interpreter word, written as space-separated shorthand where ``{P}`` stands for +a program that itself contains spaces), the ``_WRAPPER_SPECS`` table, the +``_wrapper_command_start``, ``_wrapper_chdir_value``, ``_env_splits_string`` and +``_launcher_position_unresolved`` rows. + +Every value is a ``(value, unresolved)`` pair: *unresolved* is True when the +command a launcher runs cannot be located, which is reported out of band so shared +consumers keep their verb. +""" + +from __future__ import annotations + +import pytest + +from autoskillit.hooks._classification._invocation_flags import ( + _PYTHON_INVOCATION_BOOLEAN_FLAGS, + _PYTHON_INVOCATION_FLAG_SPEC, + _SHELL_INVOCATION_BOOLEAN_FLAGS, + _SHELL_INVOCATION_FLAG_SPEC, + _WRAPPER_SPECS, + _env_splits_string, + _launcher_position_unresolved, + _wrapper_chdir_value, + _wrapper_command_start, + python_c_program, + shell_c_payload, +) + +pytestmark = [pytest.mark.layer("hooks"), pytest.mark.small] + +_PROGRAM = "git update-ref refs/heads/x y" + + +def _argv(shorthand: str) -> list[str]: + return [word.replace("{P}", _PROGRAM) for word in shorthand.split()] + + +_SHELL_PAYLOAD_FOUND = [ + "-c {P}", + "-lc {P}", + "-xc {P}", + "-ec {P}", + "-e -c {P}", + "--norc -c {P}", + "--noprofile -c {P}", + "--login -c {P}", + "-O extglob -c {P}", + "+O extglob -c {P}", + "-o pipefail -c {P}", + "+o history -c {P}", + "--rcfile rc -c {P}", + "--init-file rc -c {P}", + "-c -- {P}", + "-D -c {P}", + "-c {P} name arg", +] +_SHELL_PAYLOAD_UNRESOLVED = [ + "--bogus -c {P}", + "--bogus", + "-c", + "-lc", + "-O extglob -c", +] +_SHELL_PAYLOAD_ABSENT = [ + "", + "script.sh", + "-s", + "-x script.sh", + "script.sh -c x", + "-", + "--version", + "--help", +] + + +@pytest.mark.parametrize("shorthand", _SHELL_PAYLOAD_FOUND) +def test_shell_c_payload_finds_the_program(shorthand: str) -> None: + assert shell_c_payload(_argv(shorthand)) == (_PROGRAM, False) + + +@pytest.mark.parametrize("shorthand", _SHELL_PAYLOAD_UNRESOLVED) +def test_shell_c_payload_without_a_locatable_program_is_unresolved(shorthand: str) -> None: + assert shell_c_payload(_argv(shorthand)) == (None, True) + + +@pytest.mark.parametrize("shorthand", _SHELL_PAYLOAD_ABSENT) +def test_shell_c_payload_without_c_mode_has_no_program(shorthand: str) -> None: + assert shell_c_payload(_argv(shorthand)) == (None, False) + + +@pytest.mark.parametrize("flag", sorted(_SHELL_INVOCATION_BOOLEAN_FLAGS)) +def test_shell_long_boolean_flag_takes_no_value(flag: str) -> None: + assert shell_c_payload([flag, "-c", _PROGRAM]) == (_PROGRAM, False) + + +@pytest.mark.parametrize("flag", sorted(set(_SHELL_INVOCATION_FLAG_SPEC) - {"-c"})) +def test_shell_value_flag_takes_one_value(flag: str) -> None: + assert shell_c_payload([flag, "VALUE", "-c", _PROGRAM]) == (_PROGRAM, False) + + +_PYTHON_PROGRAM_FOUND = [ + "-c {P}", + "-Bc {P}", + "-Ic {P}", + "-c{P}", + "-B -c {P}", + "-I -c {P}", + "-R -c {P}", + "-t -c {P}", + "-W error -c {P}", + "-Werror -c {P}", + "-X dev -c {P}", + "-Xdev -c {P}", + "-W ignore -X dev -Bc {P}", + "--check-hash-based-pycs always -c {P}", + "-c {P} -m x", +] +_PYTHON_PROGRAM_UNRESOLVED = [ + "--bogus -c {P}", + "--bogus", + "-c", + "-Bc", +] +_PYTHON_PROGRAM_ABSENT = [ + "", + "-m pip", + "-Bm pip", + "-B -m pip -c x", + "script.py", + "script.py -c x", + "- -c x", + "-- -c x", + "--version", + "--help", +] + + +@pytest.mark.parametrize("shorthand", _PYTHON_PROGRAM_FOUND) +def test_python_c_program_finds_the_program(shorthand: str) -> None: + assert python_c_program(_argv(shorthand)) == (_PROGRAM, False) + + +@pytest.mark.parametrize("shorthand", _PYTHON_PROGRAM_UNRESOLVED) +def test_python_c_program_without_a_locatable_program_is_unresolved(shorthand: str) -> None: + assert python_c_program(_argv(shorthand)) == (None, True) + + +@pytest.mark.parametrize("shorthand", _PYTHON_PROGRAM_ABSENT) +def test_python_c_program_without_c_has_no_program(shorthand: str) -> None: + assert python_c_program(_argv(shorthand)) == (None, False) + + +@pytest.mark.parametrize("flag", sorted(_PYTHON_INVOCATION_BOOLEAN_FLAGS)) +def test_python_long_boolean_flag_takes_no_value(flag: str) -> None: + assert python_c_program([flag, "-c", _PROGRAM]) == (_PROGRAM, False) + + +@pytest.mark.parametrize("flag", sorted(set(_PYTHON_INVOCATION_FLAG_SPEC) - {"-c", "-m"})) +def test_python_value_flag_takes_one_value(flag: str) -> None: + assert python_c_program([flag, "VALUE", "-c", _PROGRAM]) == (_PROGRAM, False) + + +def test_boolean_flag_sets_do_not_overlap_the_value_specs() -> None: + assert not _SHELL_INVOCATION_BOOLEAN_FLAGS & set(_SHELL_INVOCATION_FLAG_SPEC) + assert not _PYTHON_INVOCATION_BOOLEAN_FLAGS & set(_PYTHON_INVOCATION_FLAG_SPEC) + + +def test_wrapper_specs_model_exactly_the_option_bearing_wrappers() -> None: + assert set(_WRAPPER_SPECS) == { + "command", + "exec", + "nice", + "nohup", + "stdbuf", + "sudo", + "time", + "timeout", + } + + +def test_wrapper_specs_option_spellings_are_flags_without_overlap() -> None: + for name, spec in _WRAPPER_SPECS.items(): + spellings = [*spec.booleans, *spec.values, *spec.chdir] + assert all(spelling.startswith("-") for spelling in spellings), name + assert len(spellings) == len(set(spellings)), name + + +def test_sudo_host_option_stays_unmodelled() -> None: + spec = _WRAPPER_SPECS["sudo"] + assert not {"-h", "--host"} & (spec.booleans | spec.values | spec.chdir) + + +def test_only_sudo_declares_a_chdir_option_and_only_timeout_a_leading_operand() -> None: + assert {name for name, spec in _WRAPPER_SPECS.items() if spec.chdir} == {"sudo"} + assert _WRAPPER_SPECS["sudo"].chdir == {"-D", "--chdir"} + assert {name for name, spec in _WRAPPER_SPECS.items() if spec.leading_operands} == {"timeout"} + assert _WRAPPER_SPECS["timeout"].leading_operands == 1 + + +_RESOLVED_COMMAND_START = [ + ("timeout 10 git s", 2), + ("timeout -k 5 10 git s", 4), + ("timeout --kill-after=5 10 git s", 3), + ("timeout -s KILL 10 git s", 4), + ("timeout --signal=KILL 10 git s", 3), + ("timeout --foreground --preserve-status -v 10 git s", 5), + ("timeout -- 10 git s", 3), + ("sudo git s", 1), + ("sudo -n git s", 2), + ("sudo -i git s", 2), + ("sudo -u root git s", 3), + ("sudo -uroot git s", 2), + ("sudo --user=root git s", 2), + ("sudo --user root git s", 3), + ("sudo --host=h git s", 2), + ("sudo -D /x git s", 3), + ("sudo --chdir=/x git s", 2), + ("sudo -E -- git s", 3), + ("sudo -- git s", 2), + ("nice git s", 1), + ("nice -n 5 git s", 3), + ("nice -n5 git s", 2), + ("nice --adjustment=5 git s", 2), + ("nice -- git s", 2), + ("time git s", 1), + ("time -p git s", 2), + ("time -f %e git s", 3), + ("time --format=%e git s", 2), + ("stdbuf -o L git s", 3), + ("stdbuf -oL git s", 2), + ("stdbuf --output=L git s", 2), + ("stdbuf -i0 -o0 -e0 git s", 4), + ("nohup git s", 1), + ("command git s", 1), + ("command -p git s", 2), + ("exec git s", 1), + ("exec -a n git s", 3), + ("exec -c -l git s", 3), +] + + +@pytest.mark.parametrize( + ("shorthand", "start"), + [pytest.param(shorthand, start, id=shorthand) for shorthand, start in _RESOLVED_COMMAND_START], +) +def test_wrapper_command_start_locates_the_command_word(shorthand: str, start: int) -> None: + assert _wrapper_command_start(shorthand.split(), 0) == (start, False) + + +_UNRESOLVED_COMMAND_START = [ + "sudo -nu root git s", + "sudo -h git s", + "sudo --bogus git s", + "timeout --bogus 10 git s", + "timeout -x 10 git s", + "nice -5 git s", + "time --portability git s", + "stdbuf -x L git s", + "command -x git s", + "exec -z git s", + "nohup --bogus git s", +] + + +@pytest.mark.parametrize("shorthand", _UNRESOLVED_COMMAND_START) +def test_wrapper_command_start_stops_at_an_unmodelled_option(shorthand: str) -> None: + assert _wrapper_command_start(shorthand.split(), 0) == (0, True) + + +def test_wrapper_command_start_unresolved_stops_at_the_wrapper_word_not_the_segment_start() -> ( + None +): + argv = "FOO=1 sudo -h git s".split() + assert _wrapper_command_start(argv, 1) == (1, True) + + +def test_wrapper_command_start_resolved_index_is_in_the_segment_domain() -> None: + argv = "FOO=1 sudo -n git s".split() + assert _wrapper_command_start(argv, 1) == (3, False) + + +@pytest.mark.parametrize( + "shorthand", + [ + "timeout", + "timeout 10", + "timeout -k 5 10", + "sudo", + "sudo -n", + "nice -n 5", + "nohup", + "command", + "command -v git", + "command -V git", + "command -p -v git", + ], +) +def test_wrapper_without_a_command_runs_past_the_segment(shorthand: str) -> None: + argv = shorthand.split() + start, unresolved = _wrapper_command_start(argv, 0) + assert start >= len(argv) + assert not unresolved + + +@pytest.mark.parametrize( + ("shorthand", "expected"), + [ + pytest.param("sudo -D /x git s", ("/x", 2, 0), id="detached"), + pytest.param("sudo --chdir=/x git s", ("/x", 1, len("--chdir=")), id="long-attached"), + pytest.param("sudo -D/x git s", ("/x", 1, 2), id="short-attached"), + pytest.param("sudo -n -D /x -u root git s", ("/x", 3, 0), id="among-other-options"), + pytest.param("sudo -D /a -D /b git s", ("/b", 4, 0), id="last-detached-wins"), + pytest.param( + "sudo -D /a --chdir=/b git s", ("/b", 3, len("--chdir=")), id="last-spelling-wins" + ), + pytest.param("sudo -n git s", None, id="no-chdir"), + pytest.param("sudo git s -D /x", None, id="after-the-command-short"), + pytest.param("sudo git s --chdir=/x", None, id="after-the-command-long"), + ], +) +def test_wrapper_chdir_value(shorthand: str, expected: tuple[str, int, int] | None) -> None: + assert _wrapper_chdir_value(shorthand.split(), 0) == expected + + +def test_wrapper_chdir_value_for_a_wrapper_without_a_chdir_option_is_none() -> None: + assert _wrapper_chdir_value("timeout 10 git s".split(), 0) is None + assert _wrapper_chdir_value("nice -n 5 git s".split(), 0) is None + + +def test_wrapper_chdir_value_indexes_the_segment_not_the_wrapper() -> None: + argv = "FOO=1 sudo -D /x git s".split() + assert _wrapper_chdir_value(argv, 1) == ("/x", 3, 0) + + +@pytest.mark.parametrize( + ("tokens", "expected"), + [ + pytest.param(tokens, expected, id=" ".join(tokens) or "empty") + for tokens, expected in [ + (["-S", "git s"], True), + (["-Sgit s"], True), + (["--split-string", "git s"], True), + (["--split-string=git s"], True), + (["-i", "-S", "x"], True), + (["-i", "A=1"], False), + (["-u", "FOO"], False), + (["--unset=FOO"], False), + (["-C", "/x"], False), + (["--ignore-signal=INT"], False), + ([], False), + ] + ], +) +def test_env_splits_string(tokens: list[str], expected: bool) -> None: + assert _env_splits_string(tokens) is expected + + +_OPAQUE_LAUNCHERS = [ + "xargs git push", + "xargs -n1 gh pr view", + "/usr/bin/xargs git push", + "watch git s", + "parallel git ::: s", + "uvx t", + "uv run git s", + "uv run pytest", + "pipx run t", + "setsid git s", + "ionice -c3 git s", + "taskset 1 git s", + "chrt -o 0 git s", + "flock /tmp/l git s", + "find . -exec git s {} ;", + "find . -execdir git s {} ;", + "find . -ok git s {} ;", + "find . -okdir git s {} ;", + "find . -name x -exec git s {} +", + "node -e {P}", + "node --eval {P}", + "perl -e {P}", + "perl -E {P}", + "ruby -e {P}", + "bash --bogus -c {P}", + "sh -c", + "zsh -lc", + "python3 --bogus -c {P}", + "python3 -c", + "/usr/bin/python3.12 -Bc", +] +_RESOLVED_LAUNCHERS = [ + "git push", + "pip install x", + "find . -name x", + "find . -type f", + "uv", + "uv pip install x", + "uv sync", + "pipx install x", + "pipx", + "node script.js", + "perl script.pl", + "bash script.sh", + "bash -lc {P}", + "bash -c {P}", + "sh -ec {P}", + "zsh -c {P}", + "python3 -c {P}", + "python3 -Bc {P}", + "python3 -m pip", + "python3 script.py -c x", + "python3 script.py", + "python script.py", +] + + +@pytest.mark.parametrize("shorthand", _OPAQUE_LAUNCHERS) +def test_launcher_position_unresolved_for_opaque_launchers(shorthand: str) -> None: + assert _launcher_position_unresolved(_argv(shorthand)) is True + + +@pytest.mark.parametrize("shorthand", _RESOLVED_LAUNCHERS) +def test_launcher_position_resolved_for_direct_commands(shorthand: str) -> None: + assert _launcher_position_unresolved(_argv(shorthand)) is False diff --git a/tests/hooks/test_test_runner_guard.py b/tests/hooks/test_test_runner_guard.py index 1bef15ef0e..a054ff932b 100644 --- a/tests/hooks/test_test_runner_guard.py +++ b/tests/hooks/test_test_runner_guard.py @@ -171,6 +171,17 @@ def test_allows_uv_run_python_c_import_pytest(self, monkeypatch): output = _run_hook(event, monkeypatch, headless=True) assert output == "" + @pytest.mark.parametrize( + "command", + ["timeout 600 uv run pytest -q", "uv run pytest -k git"], + ) + def test_blocks_uv_run_pytest_launcher_forms(self, command, monkeypatch): + # `uv run` is an opaque launcher: its position is unresolved, but `uv` + # stays the verb so the pytest operand is still matched. + event = _build_event(command) + output = _run_hook(event, monkeypatch, headless=True) + assert _is_denied(output) + def test_blocks_pytest_after_cd(self, monkeypatch): event = _build_event("cd /some/path && pytest") output = _run_hook(event, monkeypatch, headless=True) diff --git a/tests/infra/test_git_ops_guard.py b/tests/infra/test_git_ops_guard.py index e62a8cc767..621a7f928a 100644 --- a/tests/infra/test_git_ops_guard.py +++ b/tests/infra/test_git_ops_guard.py @@ -1996,3 +1996,181 @@ def test_git_ops_guard_imports_with_hooks_directory_alone() -> None: ) assert "ModuleNotFoundError" not in result.stderr assert "ImportError" not in result.stderr + + +_INVOCATION_GRAMMAR_DENIED = [ + ("bash -lc 'git push --force origin main'", _FORCE_PUSH), + ("sh -ec 'git commit --amend'", _AMEND), + ("timeout -k 5 10 git push -f origin main", _FORCE_PUSH), + ("sudo -n git commit --amend", _AMEND), + ("env --ignore-signal git push -f origin main", _FORCE_PUSH), + ("python3 -Bc \"import subprocess; subprocess.run(['git','reset','--hard'])\"", _HARD_RESET), + ("echo x | xargs git push -f origin main", _FORCE_PUSH), + ("xargs sh -c 'true&&git push -f origin main'", _FORCE_PUSH), + ("env -S 'true;git commit --amend'", _AMEND), + ("setsid git commit --amend", _AMEND), + ("node -e \"require('child_process').execSync('git push -f origin main')\"", _FORCE_PUSH), +] + +_INVOCATION_GRAMMAR_REF_REWRITES = [ + "bash -lc 'git update-ref refs/heads/develop {sha}'", + "timeout -k 5 10 git checkout -Bdevelop {sha}", + "sudo -D {primary} git update-ref refs/heads/develop {sha}", +] + +_INVOCATION_GRAMMAR_HEAD_MOVES = [ + "bash -lc 'git checkout -b x {sha}'", + "sudo -n git checkout other", +] + +_INVOCATION_GRAMMAR_ALLOWED = [ + "bash -lc 'git status'", + "timeout 600 uv run pytest -q", + "uv run pytest -k git", + "uv run pytest -q && git push origin main", + "echo x | xargs grep git", + "find . -exec git log {} \\;", + "nice -n 5 make build", + "command -v git", + "bash script.sh", +] + +_QUOTED_PUSH_TEXT_IN_LAUNCHER = 'find . -exec grep -l "git push" {} \\;' + + +def _denies_by_ref_or_destructive_phase(output: str, op: tuple[str, ...]) -> bool: + """Whether *output* denies with the checked-out-ref reason or with the + destructive-phase reason for *op* or for an unresolved operation.""" + if not _is_denied(output): + return False + reason = _deny_reason(output) + return reason.startswith(git_ops_guard.CHECKED_OUT_REF_DENY_PREFIX) or reason in { + _destructive_reason(op), + _destructive_reason(_UNRESOLVED_OP), + } + + +@pytest.mark.skipif(shutil.which("git") is None, reason="git binary required") +@pytest.mark.usefixtures("isolated_git_config") +class TestInvocationGrammarImmunity: + """A command is judged as the Git invocation it carries, however a wrapper's + options, a shell or interpreter command string, or an opaque launcher + deliver it. A launcher whose payload cannot be located denies only when its + text mentions a Git mutation; every other form keeps the verdict of the + command it runs in all four session shapes. + """ + + @pytest.fixture + def cook_repo(self, tmp_path: Path) -> dict[str, Path | str]: + built = build_linked_repo(tmp_path, tracked_state_dir=True) + _git(Path(str(built["primary"])), "branch", "other") + return built + + @pytest.fixture + def pushable_repo(self, linked_repo: dict[str, Path | str]) -> Path: + """The unrelated repository, with the bare repository as ``origin``.""" + unrelated = Path(str(linked_repo["unrelated"])) + _git(unrelated, "remote", "add", "origin", str(linked_repo["bare"])) + return unrelated + + @pytest.mark.parametrize(("command", "op"), _INVOCATION_GRAMMAR_DENIED) + def test_denies_a_git_mutation_behind_an_invocation_grammar( + self, tmp_path: Path, command: str, op: tuple[str, ...] + ) -> None: + """A plain directory is no repository, so the preflight denies only a + segment whose position is unresolved and that mentions a Git mutation; + otherwise the destructive phase denies, naming the row's operation or an + unresolved one. Either denial is a pass. + """ + out = _run_guard(command, kitchen_open=True, tmpdir=tmp_path) + + assert _denies_by_ref_or_destructive_phase(out, op), _context(command, out) + + def test_denies_git_mutation_text_quoted_in_an_opaque_launcher_argument( + self, tmp_path: Path + ) -> None: + """Text naming a Git mutation inside ``find -exec`` arguments cannot be told + apart from a payload such as ``xargs sh -c '...git push...'``, so quoted + data denies too. + """ + out = _run_guard(_QUOTED_PUSH_TEXT_IN_LAUNCHER, kitchen_open=True, tmpdir=tmp_path) + + assert _denies_by_ref_or_destructive_phase(out, _UNRESOLVED_OP), _context( + _QUOTED_PUSH_TEXT_IN_LAUNCHER, out + ) + + def test_allows_the_inner_command_the_launcher_wraps(self, tmp_path: Path) -> None: + command = 'grep -rl "git push" .' + + out = _run_guard(command, kitchen_open=True, tmpdir=tmp_path) + + assert _guard_verdict(out) == "allow", _context(command, out) + + @pytest.mark.parametrize("command_template", _INVOCATION_GRAMMAR_REF_REWRITES) + def test_denies_a_checked_out_ref_rewrite_behind_an_invocation_grammar( + self, linked_repo: dict[str, Path | str], command_template: str + ) -> None: + linked = linked_repo["linked"] + assert isinstance(linked, Path) + command = command_template.format( + sha=linked_repo["new_sha"], primary=shlex.quote(str(linked_repo["primary"])) + ) + + out = _run_guard(command, kitchen_open=True, tmpdir=linked) + + assert _guard_verdict(out) == "checked-out-ref", _context(command, out) + threatened = _checked_out_ref_result(out)["threatened_refs"] + assert isinstance(threatened, list) + assert [row["target_ref"] for row in threatened] == ["refs/heads/develop"], _context( + command, out + ) + + @pytest.mark.parametrize("command_template", _INVOCATION_GRAMMAR_HEAD_MOVES) + def test_denies_a_cook_head_move_behind_an_invocation_grammar( + self, cook_repo: dict[str, Path | str], tmp_path: Path, command_template: str + ) -> None: + primary = Path(str(cook_repo["primary"])) + command = command_template.format(sha=cook_repo["new_sha"]) + + out = _run_guard( + command, + kitchen_open=True, + tmpdir=tmp_path, + marker_root=primary, + headless=False, + extra_env={ + "AUTOSKILLIT_SESSION_TYPE": "skill", + "AUTOSKILLIT_LAUNCH_ID": "0123456789abcdef", + "AUTOSKILLIT_STATE_ROOT": str(primary), + }, + use_bash_key=True, + payload_cwd=primary, + ) + + assert _is_denied(out), _context(command, out) + reason = _deny_reason(out) + prefix = git_ops_guard.PRIMARY_HEAD_DENY_PREFIX + assert reason.startswith(prefix), _context(command, out) + details = json.loads(reason.removeprefix(prefix)) + assert details["subcommand"] == "checkout", _context(command, details) + assert details["protected_primary"] == str(primary), _context(command, details) + + @pytest.mark.parametrize("command", _INVOCATION_GRAMMAR_ALLOWED) + @pytest.mark.parametrize("directory", ["plain", "repository"]) + def test_allows_each_form_in_every_session_shape( + self, tmp_path: Path, pushable_repo: Path, directory: str, command: str + ) -> None: + verdicts = { + shape: _guard_verdict( + _run_guard( + command, + kitchen_open=True, + tmpdir=tmp_path if directory == "plain" else pushable_repo, + headless=shape[0], + session_type=shape[1], + ) + ) + for shape in _SESSION_SHAPES + } + + assert verdicts == dict.fromkeys(_SESSION_SHAPES, "allow"), _context(command, verdicts) diff --git a/tests/infra/test_unsafe_install_guard.py b/tests/infra/test_unsafe_install_guard.py index 8a254e128b..c10831356b 100644 --- a/tests/infra/test_unsafe_install_guard.py +++ b/tests/infra/test_unsafe_install_guard.py @@ -297,6 +297,8 @@ def test_system_install_false_positive_bash_guard_allowed(cmd: str) -> None: ("sudo -u root pip install -e .", "sudo-pip-install-e"), ("nice -n 5 pip install -e .", "nice-pip-install-e"), ("timeout 30 pip install -e .", "timeout-pip-install-e"), + ("timeout 60 uv pip install -e .", "timeout-uv-pip-install-e"), + ("timeout -k 5 60 uv pip install -e .", "timeout-kill-after-uv-pip-install-e"), ("stdbuf -o0 pip install -e .", "stdbuf-pip-install-e"), # Attached --python= form: must remain unsafe when path is not .venv. ("pip install -e . --python=/usr/bin/python3", "attached-system-python"), From b84b5019d233f7889557a541dedeab1daf745077 Mon Sep 17 00:00:00 2001 From: Trecek Date: Thu, 5 Nov 2026 15:36:52 -0800 Subject: [PATCH 07/46] Fail when Git-classification code tests raw option tokens Phase F of the git outcome and effective-target immunity work (#4751). A membership test, index lookup or prefix test against one literal option spelling silently allows every other spelling Git accepts. An arch test now fails when git_ops_guard or the Git classification modules compare against, index, count or prefix-test an option-looking string literal; option semantics are decided through the _option_grammar tables instead. The bare -, -- and + and dict-literal dispatch keys are exempt. Co-Authored-By: Claude Sonnet 5.5 --- tests/arch/test_git_option_literal_scope.py | 112 ++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 tests/arch/test_git_option_literal_scope.py diff --git a/tests/arch/test_git_option_literal_scope.py b/tests/arch/test_git_option_literal_scope.py new file mode 100644 index 0000000000..407d4c2409 --- /dev/null +++ b/tests/arch/test_git_option_literal_scope.py @@ -0,0 +1,112 @@ +"""Git-classification code reads option semantics from tables, not raw option tokens. + +Members: _violations, test_git_classification_code_has_no_raw_option_token_tests and +the canary tests that prove both detectors fire. + +Git, the shells and the wrappers accept bundled, stuck, ``=``-joined, abbreviated +and reordered spellings, so a membership test, index lookup or prefix test against +one literal spelling (``"-B" in args``, ``args.index("--force")``, +``token.startswith("--force")``) silently allows every other spelling. Option +semantics are decided through ``_option_grammar`` tables instead. + +Detectors, over the Git-classification modules only: + +(a) a ``Compare`` using ``in``/``not in``/``==``/``!=`` against a string literal that + looks like an option (or a tuple/set/list literal containing one); +(b) a call to ``.index``/``.count``/``.startswith``/``.endswith``/``.removeprefix``/ + ``.partition`` whose first argument is such a literal. + +The bare ``-``, ``--`` and ``+`` are exempt, as are dict-literal keys (table dispatch). +""" + +from __future__ import annotations + +import ast +import re +from pathlib import Path + +import pytest + +pytestmark = [pytest.mark.layer("arch"), pytest.mark.small] + +_HOOKS_ROOT = Path(__file__).resolve().parents[2] / "src" / "autoskillit" / "hooks" +_SCOPED_FILES = ( + _HOOKS_ROOT / "guards" / "git_ops_guard.py", + _HOOKS_ROOT / "_runtime" / "_git_command_classification.py", + _HOOKS_ROOT / "_runtime" / "_git_outcome.py", + _HOOKS_ROOT / "_runtime" / "_git_target.py", +) +_OPTION_TOKEN_RE = re.compile(r"^[-+]{1,2}[A-Za-z0-9?]") +_TOKEN_METHODS = frozenset( + {"index", "count", "startswith", "endswith", "removeprefix", "partition"} +) +_COMPARE_OPS = (ast.In, ast.NotIn, ast.Eq, ast.NotEq) + + +def _is_option_token(node: ast.expr) -> bool: + return ( + isinstance(node, ast.Constant) + and isinstance(node.value, str) + and _OPTION_TOKEN_RE.match(node.value) is not None + ) + + +def _contains_option_token(node: ast.expr) -> bool: + if isinstance(node, (ast.Tuple, ast.Set, ast.List)): + return any(_is_option_token(element) for element in node.elts) + return _is_option_token(node) + + +def _violations(source: str) -> list[tuple[int, str]]: + found: list[tuple[int, str]] = [] + for node in ast.walk(ast.parse(source)): + if isinstance(node, ast.Compare) and any(isinstance(op, _COMPARE_OPS) for op in node.ops): + if any(_contains_option_token(operand) for operand in [node.left, *node.comparators]): + found.append((node.lineno, "comparison against a raw option token")) + elif ( + isinstance(node, ast.Call) + and isinstance(node.func, ast.Attribute) + and node.func.attr in _TOKEN_METHODS + and node.args + and _contains_option_token(node.args[0]) + ): + found.append((node.lineno, f".{node.func.attr}() with a raw option token")) + return found + + +@pytest.mark.parametrize("path", _SCOPED_FILES, ids=lambda path: path.name) +def test_git_classification_code_has_no_raw_option_token_tests(path: Path) -> None: + violations = _violations(path.read_text(encoding="utf-8")) + assert not violations, ( + f"{path.name} tests raw option spellings; decide option semantics through an " + f"_option_grammar table instead:\n" + + "\n".join(f" line {line}: {reason}" for line, reason in violations) + ) + + +@pytest.mark.parametrize( + "snippet", + [ + 'args.index("-B")', + '"--force" in args', + 't.startswith("--force")', + 'token not in ("-f", "--force")', + 'args.count("-q")', + ], +) +def test_detectors_fire_on_raw_option_token_tests(snippet: str) -> None: + assert _violations(snippet) + + +@pytest.mark.parametrize( + "snippet", + [ + 'token == "--"', + 'token == "-"', + 'refspec.startswith("+")', + '{"--git-dir": 1}[key]', + 'verb in ("checkout", "branch")', + ], +) +def test_detectors_ignore_exempt_forms(snippet: str) -> None: + assert not _violations(snippet) From 5d42e3198ea1e3bd69498cf1f05df479f9971657 Mon Sep 17 00:00:00 2001 From: Trecek Date: Thu, 5 Nov 2026 16:06:01 -0800 Subject: [PATCH 08/46] Make _WrapperSpec a frozen dataclass to satisfy the *Spec naming rule The *Spec convention requires a dataclass or TypedDict; _WrapperSpec was a NamedTuple, which tests/arch/test_def_spec_naming.py rejects. Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/_classification/_invocation_flags.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/autoskillit/hooks/_classification/_invocation_flags.py b/src/autoskillit/hooks/_classification/_invocation_flags.py index 9d29d2ec89..f266b88359 100644 --- a/src/autoskillit/hooks/_classification/_invocation_flags.py +++ b/src/autoskillit/hooks/_classification/_invocation_flags.py @@ -20,7 +20,8 @@ import os import re from collections.abc import Mapping, Sequence -from typing import TYPE_CHECKING, NamedTuple +from dataclasses import dataclass +from typing import TYPE_CHECKING if TYPE_CHECKING: from autoskillit.hooks._classification._flag_arity_classification import _FlagArity @@ -161,7 +162,8 @@ def python_c_program(args: Sequence[str]) -> tuple[str | None, bool]: return None, False -class _WrapperSpec(NamedTuple): +@dataclass(frozen=True, slots=True) +class _WrapperSpec: booleans: frozenset[str] values: frozenset[str] leading_operands: int From 952d7946143a7bec83c79a6cf975045b5816e0fa Mon Sep 17 00:00:00 2001 From: Trecek Date: Thu, 5 Nov 2026 17:42:24 -0800 Subject: [PATCH 09/46] Keep session-cwd anomalies with a workdir; derive unwrapped blockers from the wrapper table A Bash payload carrying a per-call workdir no longer hides a malformed session cwd: RELATIVE_CWD and NON_STRING_CWD are still reported next to the workdir result, so github_mutation_guard's malformed-cwd deny keeps firing for that combination while RELATIVE_WORKDIR alone still does not trip it. The walk's list of wrappers that keep a cd builtin from running in the current shell is now the _WRAPPER_SPECS keys plus env, instead of a second literal set. time and command stay out of it: bash runs a builtin behind them in the current shell, so treating them as blockers would leave the tracked directory behind the real one. Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/_runtime/_hook_payload.py | 21 +++++---- .../hooks/_runtime/_shell_segment_walk.py | 22 +++++++--- tests/hooks/test_hook_payload.py | 44 ++++++++++++++++++- tests/hooks/test_shell_segment_walk.py | 15 +++++++ 4 files changed, 86 insertions(+), 16 deletions(-) diff --git a/src/autoskillit/hooks/_runtime/_hook_payload.py b/src/autoskillit/hooks/_runtime/_hook_payload.py index f1232bb47d..995e6460c7 100644 --- a/src/autoskillit/hooks/_runtime/_hook_payload.py +++ b/src/autoskillit/hooks/_runtime/_hook_payload.py @@ -61,18 +61,22 @@ def _absolute_string_or_blank(value: Any) -> tuple[str, PayloadAnomaly | None]: def _execution_cwd( tool_kind: str, raw_cwd: Any, tool_input: dict[str, Any] -) -> tuple[str, PayloadAnomaly | None]: - """The directory the command runs in. +) -> tuple[str, tuple[PayloadAnomaly, ...]]: + """The directory the command runs in, and every anomaly in the cwd-shaped fields. A Bash-shaped payload may carry a per-call ``workdir`` (Codex); when it - does, that, not the session cwd, is where the command runs. + does, that, not the session cwd, is where the command runs. A malformed + session cwd is still reported: it is a defect of the payload whether or not + a workdir overrides it. """ + session_cwd, session_anomaly = _absolute_string_or_blank(raw_cwd) + anomalies = () if session_anomaly is None else (session_anomaly,) workdir = tool_input.get("workdir") if tool_kind == "bash" and isinstance(workdir, str): if os.path.isabs(workdir): - return (workdir, None) - return ("", PayloadAnomaly.RELATIVE_WORKDIR) - return _absolute_string_or_blank(raw_cwd) + return (workdir, anomalies) + return ("", (*anomalies, PayloadAnomaly.RELATIVE_WORKDIR)) + return (session_cwd, anomalies) def normalize_payload_cwd(value: Any) -> str: @@ -133,9 +137,8 @@ def parse_hook_command(data: dict[str, Any]) -> ParsedHookCommand: command = None anomalies.append(PayloadAnomaly.NON_STRING_COMMAND) - execution_cwd, cwd_anomaly = _execution_cwd(tool_kind, raw_execution_cwd, tool_input) - if cwd_anomaly is not None: - anomalies.append(cwd_anomaly) + execution_cwd, cwd_anomalies = _execution_cwd(tool_kind, raw_execution_cwd, tool_input) + anomalies.extend(cwd_anomalies) return ParsedHookCommand( tool_kind=tool_kind, diff --git a/src/autoskillit/hooks/_runtime/_shell_segment_walk.py b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py index fea6ce4643..b91fec58a4 100644 --- a/src/autoskillit/hooks/_runtime/_shell_segment_walk.py +++ b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py @@ -26,16 +26,28 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: + from autoskillit.hooks._classification._invocation_flags import _WRAPPER_SPECS from autoskillit.hooks._classification._output_redirect import OutputRedirectPartition from autoskillit.hooks._classification._tokenizer import ArgvToken, EvaluatedSegment +else: + if __package__: + from .._classification import _invocation_flags + else: + from _classification import _invocation_flags + + _WRAPPER_SPECS = _invocation_flags._WRAPPER_SPECS _DIRECTORY_COMMANDS: frozenset[str] = frozenset({"cd", "pushd", "popd"}) _LOGICAL_CONNECTORS: frozenset[str] = frozenset({"&&", "||", "|", "&"}) _TAINTING_FIRST_TOKENS: frozenset[str] = frozenset( {"if", "while", "until", "for", "select", "case", "then", "do", "else", "elif", "{"} ) -_UNWRAPPED_BLOCKERS: frozenset[str] = frozenset( - {"env", "sudo", "nice", "nohup", "timeout", "stdbuf"} +# ``time`` and ``command`` run a shell builtin in the current shell, so ``time cd d`` +# still changes the directory; every other wrapper starts a process, where ``cd`` is +# not a command. +_BUILTIN_TRANSPARENT_WRAPPERS: frozenset[str] = frozenset({"time", "command"}) +_UNWRAPPED_BLOCKERS: frozenset[str] = ( + frozenset({"env", *_WRAPPER_SPECS}) - _BUILTIN_TRANSPARENT_WRAPPERS ) @@ -92,9 +104,9 @@ def _ensure_scope_state( def _unwrapped_prefix(prefix: list[str]) -> bool: """True when *prefix* contains no wrapper command — i.e. is the unwrapped shell-builtin form. - The polarity is ``unwrapped``: returns True only when none of the wrapper - tokens (``env``, ``sudo``, ``nice``, ``nohup``, ``timeout``, ``stdbuf``) - appears in the prefix. + The polarity is ``unwrapped``: returns True only when no process-starting + wrapper (``env`` and the ``_WRAPPER_SPECS`` commands, bar the builtin-transparent + ``time`` and ``command``) appears in the prefix. """ return not any(token in _UNWRAPPED_BLOCKERS for token in prefix) diff --git a/tests/hooks/test_hook_payload.py b/tests/hooks/test_hook_payload.py index 8102c50fa2..0b97ba206d 100644 --- a/tests/hooks/test_hook_payload.py +++ b/tests/hooks/test_hook_payload.py @@ -1,8 +1,9 @@ """Payload facts extracted by ``parse_hook_command`` in hooks/_runtime/_hook_payload.py. Members: the Bash per-call ``workdir`` rows (absolute, relative and blank), the -payload-cwd rows that carry no ``workdir``, the run_cmd rows that ignore it, and -the github_mutation_guard decisions that separate ``RELATIVE_WORKDIR`` from the +payload-cwd rows that carry no ``workdir``, the session-cwd anomalies that +survive a ``workdir``, the run_cmd rows that ignore it, and the +github_mutation_guard decisions that separate ``RELATIVE_WORKDIR`` from the session-cwd anomalies. """ @@ -135,6 +136,45 @@ def test_relative_workdir_does_not_trigger_the_github_guard_malformed_cwd_denial assert decision.trigger is None +@pytest.mark.parametrize( + ("payload_cwd", "workdir", "execution_cwd", "anomalies"), + [ + ("relative", "/work", "/work", (PayloadAnomaly.RELATIVE_CWD,)), + (7, "/work", "/work", (PayloadAnomaly.NON_STRING_CWD,)), + ( + "relative", + "work", + "", + (PayloadAnomaly.RELATIVE_CWD, PayloadAnomaly.RELATIVE_WORKDIR), + ), + ], + ids=[ + "relative-session-cwd-absolute-workdir", + "non-string-session-cwd-absolute-workdir", + "relative-session-cwd-relative-workdir", + ], +) +def test_session_cwd_anomalies_survive_a_workdir_and_keep_the_github_guard_denial( + payload_cwd: object, + workdir: str, + execution_cwd: str, + anomalies: tuple[PayloadAnomaly, ...], +) -> None: + event = { + "tool_name": "Bash", + "tool_input": {"command": _COMMAND, "workdir": workdir}, + "cwd": payload_cwd, + } + + parsed = parse_hook_command(event) + decision = decide(parsed) + + assert parsed.execution_cwd == execution_cwd + assert parsed.anomalies == anomalies + assert decision.allow is False + assert decision.trigger is DenyTrigger.MALFORMED_CWD + + @pytest.mark.parametrize( "payload_cwd", ["relative", 7], ids=["relative-session-cwd", "non-string-session-cwd"] ) diff --git a/tests/hooks/test_shell_segment_walk.py b/tests/hooks/test_shell_segment_walk.py index fa22485cc5..5721f4f526 100644 --- a/tests/hooks/test_shell_segment_walk.py +++ b/tests/hooks/test_shell_segment_walk.py @@ -17,6 +17,7 @@ import pytest +from autoskillit.hooks._classification._invocation_flags import _WRAPPER_SPECS from autoskillit.hooks._classification._tokenizer import ( _CommandSegment, _tokenize_command_segments_with_redirects, @@ -171,6 +172,11 @@ def test_git_segment_cwd_certainty( pytest.param("cd $X && git x", False, "", id="dynamic-target"), pytest.param("cd - && git x", False, "", id="previous-directory"), pytest.param("builtin cd @a && git x", False, "@w", id="builtin-cd-is-unmodelled"), + pytest.param("time cd @a; git x", True, "@a", id="time-runs-the-builtin-in-place"), + pytest.param("time -p cd @a; git x", True, "@a", id="time-option-runs-the-builtin"), + pytest.param("command cd @a; git x", False, "@a", id="command-runs-an-unmodelled-builtin"), + pytest.param("exec cd @a; git x", True, "@w", id="exec-starts-a-process"), + pytest.param("env cd @a; git x", True, "@w", id="env-starts-a-process"), ], ) def test_git_segment_cwd_and_certainty_together( @@ -181,6 +187,15 @@ def test_git_segment_cwd_and_certainty_together( assert segment.verb_cwd == _bind(verb_cwd, dirs) +@pytest.mark.parametrize("wrapper", sorted(_WRAPPER_SPECS)) +def test_only_builtin_transparent_wrappers_let_a_cd_change_the_walk_directory( + dirs: dict[str, str], wrapper: str +) -> None: + segment = _git_segment(_walk(f"{wrapper} cd @a; git x", dirs, dirs["w"]), "x") + expected = "@a" if wrapper in {"time", "command"} else "@w" + assert segment.verb_cwd == _bind(expected, dirs) + + def test_unknown_initial_cwd_is_never_certain(dirs: dict[str, str]) -> None: segment = _git_segment(_walk("git x", dirs, ""), "x") assert segment.verb_cwd == "" From e02983b33d9708e4d05beddc6c1fa166afdc4ca9 Mon Sep 17 00:00:00 2001 From: Trecek Date: Thu, 5 Nov 2026 17:42:46 -0800 Subject: [PATCH 10/46] Bring Git-spawning guard tests under the hygiene rules; pin whole-tree pathspecs Guard test modules skip without git and isolate Git config; the shared harness owns the git --version assertion context that two modules had copied, and every verdict assertion in the attribution matrix carries it. The option-table contract builds its repository with the shared topology helper. printf x | tee "$TARGET" is pinned as allowed. Whole-tree checkout spellings that Git itself treats as a full restore (*, d/.., :(top)*) are pinned in the outcome matrix and in the real-Git dirty-state differential. Docstrings name _GIT_TABLES_REFERENCE_VERSION, risky_git_operation and _RISKY_PREDICATES; the residual-risk list names core.fsmonitor and core.sshCommand; the env-contract justification regains its wording. Co-Authored-By: Claude Sonnet 5.5 --- .../hook_registry/_env_contract.py | 3 +- .../hooks/_runtime/_git_option_tables.py | 3 +- .../hooks/_runtime/_git_outcome.py | 6 ++- src/autoskillit/hooks/_runtime/_git_target.py | 6 +-- src/autoskillit/hooks/guards/AGENTS.md | 7 +-- tests/arch/test_guard_flag_spec_coverage.py | 2 +- tests/hooks/test_git_effect_differential.py | 6 ++- tests/hooks/test_git_option_table_contract.py | 15 ++++--- tests/hooks/test_git_outcome.py | 18 +++++++- tests/infra/_git_ops_guard_harness.py | 27 +++++++++--- tests/infra/test_git_ops_guard.py | 43 ++++++++++--------- tests/infra/test_git_ops_guard_attribution.py | 37 ++++++++++------ .../infra/test_git_ops_guard_primary_head.py | 13 +----- 13 files changed, 117 insertions(+), 69 deletions(-) diff --git a/src/autoskillit/hook_registry/_env_contract.py b/src/autoskillit/hook_registry/_env_contract.py index af87d20339..170ae6695b 100644 --- a/src/autoskillit/hook_registry/_env_contract.py +++ b/src/autoskillit/hook_registry/_env_contract.py @@ -222,7 +222,8 @@ def _validate_hook_env_contract(entries: tuple[HookEnvVarDef, ...]) -> None: "autoskillit", "autoskillit.cli.session._session_launch:_interactive_anchor_env", "autoskillit.cli.session._session_launch:prepare_interactive_launch", - "Every interactive launch (cook, order, fleet) binds hook state to the project root.", + "Every interactive launch (cook, order, fleet) binds hook state to the " + "orchestrating project root.", ), HookEnvVarDef( "AUTOSKILLIT_WRITE_GUARD_TOOL_NAMES", diff --git a/src/autoskillit/hooks/_runtime/_git_option_tables.py b/src/autoskillit/hooks/_runtime/_git_option_tables.py index baa744dfd5..051e99adb9 100644 --- a/src/autoskillit/hooks/_runtime/_git_option_tables.py +++ b/src/autoskillit/hooks/_runtime/_git_option_tables.py @@ -3,7 +3,8 @@ Members: _GIT_CHECKOUT_FLAG_SPEC, _GIT_SWITCH_FLAG_SPEC, _GIT_BRANCH_FLAG_SPEC, _GIT_SYMBOLIC_REF_FLAG_SPEC, _GIT_UPDATE_REF_FLAG_SPEC, _GIT_RESET_FLAG_SPEC, _GIT_PUSH_FLAG_SPEC, _GIT_FETCH_FLAG_SPEC, _GIT_COMMIT_FLAG_SPEC, -_GIT_CLEAN_FLAG_SPEC, _SED_FLAG_SPEC, _GIT_SUBCOMMAND_TABLES. +_GIT_CLEAN_FLAG_SPEC, _SED_FLAG_SPEC, _GIT_SUBCOMMAND_TABLES, +_GIT_TABLES_REFERENCE_VERSION. Data only. Rows come from ``git -h`` and ``git --git-completion-helper-all`` on ``_GIT_TABLES_REFERENCE_VERSION``, including diff --git a/src/autoskillit/hooks/_runtime/_git_outcome.py b/src/autoskillit/hooks/_runtime/_git_outcome.py index 6c0c1aadfa..090e5908df 100644 --- a/src/autoskillit/hooks/_runtime/_git_outcome.py +++ b/src/autoskillit/hooks/_runtime/_git_outcome.py @@ -2,14 +2,16 @@ Members: GitHeadEffect, GitRefOutcome, git_ref_outcome, UpdateRefArgs, parse_update_ref, ResetArgs, parse_reset, PushArgs, parse_push, FetchArgs, -parse_fetch, is_ref_mutation. +parse_fetch, is_ref_mutation, risky_git_operation, _RISKY_PREDICATES. Every classification reads options through the ``_option_grammar`` engine over the tables in ``_git_option_tables``, so a stuck, bundled, abbreviated or reordered spelling means what it means to Git. ``git_ref_outcome`` maps branch, checkout, switch and symbolic-ref argv to the branch ref a command rewrites and the effect on ``HEAD``; the other parsers expose the operands the -classifiers in ``_git_command_classification`` need. +classifiers in ``_git_command_classification`` need. ``risky_git_operation`` +names which of the operations in ``RISKY_GIT_OPERATIONS`` an argv performs, through +one predicate per operation in ``_RISKY_PREDICATES``. Expansion word-splitting into options (``git checkout $FLAGS``) is outside this bounded classification; a dynamic deciding operand is reported ambiguous. diff --git a/src/autoskillit/hooks/_runtime/_git_target.py b/src/autoskillit/hooks/_runtime/_git_target.py index 993ca24c54..d56e1bcf7c 100644 --- a/src/autoskillit/hooks/_runtime/_git_target.py +++ b/src/autoskillit/hooks/_runtime/_git_target.py @@ -335,9 +335,9 @@ def contains_blocked_git_op( """Return the blocked Git operation *cmd* performs, or None. Reads *cmd* exclusively through the evaluated-segment walk, the single - authority for what will actually execute and as what argv (rectify #4941 - Part A): a direct invocation, one delivered via ``bash -c``/``eval``, a - heredoc/herestring/pipe-fed shell, and a literal-argv + authority for what will actually execute and as what argv: a direct + invocation, one delivered via ``bash -c``/``eval``, a heredoc/herestring/ + pipe-fed shell, and a literal-argv ``subprocess.run(["git", ...])`` are all seen the same way. Operations are matched by outcome (``risky_git_operation``), not by option spelling. Returns None when *cmd* cannot be tokenized (fail-open). diff --git a/src/autoskillit/hooks/guards/AGENTS.md b/src/autoskillit/hooks/guards/AGENTS.md index 5da4e855b0..5877a40247 100644 --- a/src/autoskillit/hooks/guards/AGENTS.md +++ b/src/autoskillit/hooks/guards/AGENTS.md @@ -35,9 +35,10 @@ The package initializer remains import-free. rule over-denies (taint lasts for the scope); a raw write through a pre-existing symlink alias into ref storage, from a command that names neither Git nor ref storage, passes the prefilter; a recursive delete of a dynamic directory that contains ref - storage (`rm -rf "$REPO"`) is allowed; Git's executable repository state (hooks, - `core.hooksPath`, filter and diff drivers, `credential.helper`, merge drivers) changes - what a command does without changing its text. The guard raises the cost of mistakes + storage (`rm -rf "$REPO"`) is allowed; Git's executable repository state + (`.git/hooks/*`, `core.hooksPath`, `core.fsmonitor`, `core.sshCommand`, filter and diff + drivers, `credential.helper`, merge drivers) changes what a command does without + changing its text. The guard raises the cost of mistakes and bypasses for Git ref and `HEAD` mutations; it is not a security boundary against a hostile shell. A cook session may not move HEAD of its anchor repository's primary checkout: in an diff --git a/tests/arch/test_guard_flag_spec_coverage.py b/tests/arch/test_guard_flag_spec_coverage.py index e312f84037..9e97ff6ae7 100644 --- a/tests/arch/test_guard_flag_spec_coverage.py +++ b/tests/arch/test_guard_flag_spec_coverage.py @@ -4,7 +4,7 @@ _CURL_FLAG_SPEC in hooks/_github_mutation_analysis.py; _GIT_GLOBAL_FLAG_SPEC and _PIP_GLOBAL_FLAG_SPEC in hooks/_command_classification.py; _SHELL_INVOCATION_FLAG_SPEC and _PYTHON_INVOCATION_FLAG_SPEC in -hooks/_classification/_interpreters.py, rectify #4941 Part A; and the Git and +hooks/_classification/_invocation_flags.py, rectify #4941 Part A; and the Git and sed option tables in hooks/_runtime/_git_option_tables.py: _GIT_CHECKOUT_FLAG_SPEC, _GIT_SWITCH_FLAG_SPEC, _GIT_BRANCH_FLAG_SPEC, _GIT_SYMBOLIC_REF_FLAG_SPEC, _GIT_UPDATE_REF_FLAG_SPEC, _GIT_RESET_FLAG_SPEC, _GIT_PUSH_FLAG_SPEC, diff --git a/tests/hooks/test_git_effect_differential.py b/tests/hooks/test_git_effect_differential.py index 7428232101..b2624a34c9 100644 --- a/tests/hooks/test_git_effect_differential.py +++ b/tests/hooks/test_git_effect_differential.py @@ -1206,7 +1206,7 @@ def _run_destructive( "origin/develop", ) _DIRTY_TRACKED = ("file.txt", "d/x", "d", "local.txt") -_WHOLE_TREE_SPELLINGS = (".", "./", ":/", ":(top)", ":(top).") +_WHOLE_TREE_SPELLINGS = (".", "./", ":/", ":(top)", ":(top).", "*", "d/..", ":(top)*") _DIRTY_RESTORE = (*_DIRTY_TRACKED, *_WHOLE_TREE_SPELLINGS) _DIRTY_UNTRACKED = ( "scratch.tmp", @@ -1446,6 +1446,10 @@ def _dirty_cases() -> st.SearchStrategy[_Case]: ("checkout", ("--", ".")), ("checkout", ("--", "./")), ("checkout", ("--", ":/")), + ("checkout", ("--", "*")), + ("checkout", ("--", "d/..")), + ("checkout", ("--", ":(top)*")), + ("checkout", ("*",)), ("checkout", (".", "--")), ("checkout", (".", "origin", "main")), ("checkout", ("HEAD", ".")), diff --git a/tests/hooks/test_git_option_table_contract.py b/tests/hooks/test_git_option_table_contract.py index 55f6c5dd40..ac37b573a2 100644 --- a/tests/hooks/test_git_option_table_contract.py +++ b/tests/hooks/test_git_option_table_contract.py @@ -29,6 +29,7 @@ _GIT_SUBCOMMAND_TABLES, _GIT_TABLES_REFERENCE_VERSION, ) +from tests._git_topology import init_empty_checkout pytestmark = [ pytest.mark.layer("hooks"), @@ -118,11 +119,15 @@ def git_env(tmp_path_factory: pytest.TempPathFactory) -> dict[str, str]: @pytest.fixture(scope="module") def git_repo(tmp_path_factory: pytest.TempPathFactory, git_env: dict[str, str]) -> Path: - # The helper only needs a repository to run in, not one with history. - repo = tmp_path_factory.mktemp("git-option-contract-repo") - init = _run_git(git_env, repo, "init", "-q") - assert init.returncode == 0, f"git init failed: {init.stderr!r}" - return repo + # The helper only needs a repository to run in, not one with history. The + # topology builder reads the process environment, so it runs under the same + # isolation as every other Git call in this module. + with pytest.MonkeyPatch.context() as patch: + for name in _AMBIENT_GIT_VARIABLES: + patch.delenv(name, raising=False) + for name in ("HOME", "XDG_CONFIG_HOME", "GIT_CONFIG_GLOBAL", "GIT_CONFIG_NOSYSTEM"): + patch.setenv(name, git_env[name]) + return init_empty_checkout(tmp_path_factory.mktemp("git-option-contract-repo")) @pytest.fixture(scope="module") diff --git a/tests/hooks/test_git_outcome.py b/tests/hooks/test_git_outcome.py index 3f6d5a07c2..1e82b6edcb 100644 --- a/tests/hooks/test_git_outcome.py +++ b/tests/hooks/test_git_outcome.py @@ -449,9 +449,22 @@ def _risky_cases(expected: tuple[str, ...] | None, commands: tuple[str, ...]) -> "checkout :(top).", "checkout . origin main", "checkout feat .", + "checkout *", + "checkout d/..", + "checkout :(top)*", + ), + ), + *_risky_cases( + _CHECKOUT_DASHDASH_TREE, + ( + "checkout -- .", + "checkout -- ./", + "checkout -- a .", + "checkout -- *", + "checkout -- d/..", + "checkout -- :(top)*", ), ), - *_risky_cases(_CHECKOUT_DASHDASH_TREE, ("checkout -- .", "checkout -- ./", "checkout -- a .")), ] _ALLOWED_ROWS = [ @@ -500,6 +513,9 @@ def _risky_cases(expected: tuple[str, ...] | None, commands: tuple[str, ...]) -> "checkout :/.", "checkout -p", "checkout feat -- a.txt", + "checkout -- d/*", + "checkout -- *.txt", + "checkout -- ./d", ), ), ] diff --git a/tests/infra/_git_ops_guard_harness.py b/tests/infra/_git_ops_guard_harness.py index 4cabe17910..12cc0c4720 100644 --- a/tests/infra/_git_ops_guard_harness.py +++ b/tests/infra/_git_ops_guard_harness.py @@ -1,15 +1,21 @@ """Shared harness for git_ops_guard tests: payloads, a guard runner and repositories. Members: _make_clean_env, _build_payload, _run_guard, _guard_exit_code, -_is_denied, _checked_out_ref_result, _guard_verdict, _git, build_linked_repo. +_is_denied, _checked_out_ref_result, _guard_verdict, _git, _git_version, _context, +build_linked_repo. ``UNSET`` distinguishes "use the default" from an explicit ``""`` or ``None`` for -the payload's cwd fields, so a test can send a relative or empty cwd. +the payload's cwd fields, so a test can send a relative or empty cwd. A verdict +rests on what the installed Git does, so assertion messages carry ``_context``. +The modules that use this harness ``skipif`` without a ``git`` binary and isolate +Git config: ``GIT_CONFIG_GLOBAL`` replaces both ``~/.gitconfig`` and +``$XDG_CONFIG_HOME/git/config``, so ``isolated_git_config`` needs no HOME redirect. """ from __future__ import annotations import contextlib +import functools import io import json import os @@ -29,6 +35,17 @@ _HOOK_CONFIG_RELPATH = ".autoskillit/temp/.hook_config.json" +@functools.cache +def _git_version() -> str: + return subprocess.run( + ["git", "--version"], capture_output=True, text=True, check=False + ).stdout.strip() + + +def _context(command: str, observed: object = "") -> str: + return f"{command!r} {observed!r} ({_git_version()})" + + def _make_clean_env( skill_name: str | None, session_type: str | None = None, @@ -141,7 +158,7 @@ def _run_guard( try: git_ops_guard.main() except SystemExit as exc: - assert exc.code == 0, f"Guard exited non-zero: {exc.code!r}" + assert exc.code == 0, _context(cmd, f"guard exited {exc.code!r}") return buf.getvalue() @@ -178,7 +195,7 @@ def _checked_out_ref_result(output: str) -> dict[str, object]: data = json.loads(output) reason = data["hookSpecificOutput"]["permissionDecisionReason"] prefix = git_ops_guard.CHECKED_OUT_REF_DENY_PREFIX - assert reason.startswith(prefix) + assert reason.startswith(prefix), _context("checked-out-ref deny reason", reason) return json.loads(reason.removeprefix(prefix)) @@ -186,7 +203,7 @@ def _guard_verdict(output: str) -> str: """Classify captured guard stdout as allow, checked-out-ref or other-deny.""" if not output.strip(): return "allow" - assert _is_denied(output), output + assert _is_denied(output), _context("guard output", output) reason = json.loads(output)["hookSpecificOutput"]["permissionDecisionReason"] if reason.startswith(git_ops_guard.CHECKED_OUT_REF_DENY_PREFIX): return "checked-out-ref" diff --git a/tests/infra/test_git_ops_guard.py b/tests/infra/test_git_ops_guard.py index 621a7f928a..36010562e9 100644 --- a/tests/infra/test_git_ops_guard.py +++ b/tests/infra/test_git_ops_guard.py @@ -8,7 +8,6 @@ from __future__ import annotations -import functools import json import os import shlex @@ -32,6 +31,7 @@ from tests.infra._git_ops_guard_harness import ( _TOOL_NAME, _checked_out_ref_result, + _context, _git, _guard_exit_code, _guard_verdict, @@ -40,7 +40,12 @@ build_linked_repo, ) -pytestmark = [pytest.mark.layer("infra"), pytest.mark.medium] +pytestmark = [ + pytest.mark.layer("infra"), + pytest.mark.medium, + pytest.mark.skipif(shutil.which("git") is None, reason="git binary required"), + pytest.mark.usefixtures("isolated_git_config"), +] @pytest.fixture @@ -955,6 +960,16 @@ def test_recognized_ambiguous_mutations_fail_closed_for_all_owners( } assert all(row["old_sha"] == linked_repo["old_sha"] for row in refs) + def test_dynamic_tee_target_naming_neither_git_nor_ref_storage_is_allowed( + self, linked_repo: dict[str, Path | str] + ) -> None: + # The same dynamic target as the ref-storage row above, with no ref-storage + # text: the command is not about Git state, so the prefilter lets it through. + linked = linked_repo["linked"] + assert isinstance(linked, Path) + out = _run_guard('printf x | tee "$TARGET"', kitchen_open=True, tmpdir=linked) + assert out.strip() == "" + def test_literal_asterisk_in_value_is_recognized_as_dynamic( self, linked_repo: dict[str, Path | str] ) -> None: @@ -1522,10 +1537,12 @@ def test_denies_rewrite_of_ref_owned_by_primary( sha=linked_repo["new_sha"], primary=shlex.quote(str(linked_repo["primary"])) ) out = _run_guard(command, kitchen_open=True, tmpdir=linked) - assert _guard_verdict(out) == "checked-out-ref", command + assert _guard_verdict(out) == "checked-out-ref", _context(command, out) threatened = _checked_out_ref_result(out)["threatened_refs"] assert isinstance(threatened, list) - assert [row["target_ref"] for row in threatened] == ["refs/heads/develop"], command + assert [row["target_ref"] for row in threatened] == ["refs/heads/develop"], _context( + command, threatened + ) @pytest.mark.parametrize( "command_template", @@ -1547,7 +1564,7 @@ def test_allows_unowned_rewrites_head_effects_and_list_forms( assert isinstance(linked, Path) command = command_template.format(sha=linked_repo["new_sha"]) out = _run_guard(command, kitchen_open=True, tmpdir=linked) - assert _guard_verdict(out) == "allow", command + assert _guard_verdict(out) == "allow", _context(command, out) @pytest.mark.parametrize( ("command", "verdict"), @@ -1577,18 +1594,7 @@ def test_verdict_is_the_same_in_every_session_shape( ) for shape in _SESSION_SHAPES } - assert verdicts == dict.fromkeys(_SESSION_SHAPES, verdict) - - -@functools.cache -def _git_version() -> str: - return subprocess.run( - ["git", "--version"], capture_output=True, text=True, check=False - ).stdout.strip() - - -def _context(command: str, observed: object = "") -> str: - return f"{command!r} {observed!r} ({_git_version()})" + assert verdicts == dict.fromkeys(_SESSION_SHAPES, verdict), _context(command, verdicts) def _deny_reason(output: str) -> str: @@ -1683,7 +1689,6 @@ def _git_rows(op: tuple[str, ...], *tails: str) -> list[tuple[str, tuple[str, .. ] -@pytest.mark.skipif(shutil.which("git") is None, reason="git binary required") class TestDestructiveOutcomeImmunity: """The headless destructive-operation phase matches the operations RISKY_GIT_OPERATIONS names by what they do, not by how their options are @@ -2050,8 +2055,6 @@ def _denies_by_ref_or_destructive_phase(output: str, op: tuple[str, ...]) -> boo } -@pytest.mark.skipif(shutil.which("git") is None, reason="git binary required") -@pytest.mark.usefixtures("isolated_git_config") class TestInvocationGrammarImmunity: """A command is judged as the Git invocation it carries, however a wrapper's options, a shell or interpreter command string, or an opaque launcher diff --git a/tests/infra/test_git_ops_guard_attribution.py b/tests/infra/test_git_ops_guard_attribution.py index 13f77f4a49..f0275df26e 100644 --- a/tests/infra/test_git_ops_guard_attribution.py +++ b/tests/infra/test_git_ops_guard_attribution.py @@ -12,18 +12,25 @@ from __future__ import annotations import os +import shutil from pathlib import Path import pytest from tests.infra._git_ops_guard_harness import ( + _context, _git, _guard_verdict, _run_guard, build_linked_repo, ) -pytestmark = [pytest.mark.layer("infra"), pytest.mark.medium] +pytestmark = [ + pytest.mark.layer("infra"), + pytest.mark.medium, + pytest.mark.skipif(shutil.which("git") is None, reason="git binary required"), + pytest.mark.usefixtures("isolated_git_config"), +] _M1 = "git update-ref refs/heads/develop " _M2 = "git checkout -Bdevelop " @@ -226,22 +233,23 @@ def linked_repo(self, tmp_path: Path) -> dict[str, Path | str]: def test_denies_command_reaching_a_checked_out_ref( self, linked_repo: dict[str, Path | str], tmp_path: Path, cwd: str, command: str ) -> None: - assert _bash_verdict(linked_repo, tmp_path, cwd, command) == "checked-out-ref" + verdict = _bash_verdict(linked_repo, tmp_path, cwd, command) + assert verdict == "checked-out-ref", _context(command, (cwd, verdict)) def test_denies_dynamic_command_word_resolved_by_hook_env( self, linked_repo: dict[str, Path | str], tmp_path: Path ) -> None: - verdict = _bash_verdict( - linked_repo, tmp_path, "linked", "$G checkout -Bdevelop ", extra_env={"G": "git"} - ) - assert verdict == "checked-out-ref" + command = "$G checkout -Bdevelop " + verdict = _bash_verdict(linked_repo, tmp_path, "linked", command, extra_env={"G": "git"}) + assert verdict == "checked-out-ref", _context(command, verdict) def test_denies_alias_configured_in_the_repository( self, linked_repo: dict[str, Path | str], tmp_path: Path ) -> None: _git(Path(str(linked_repo["primary"])), "config", "alias.ur", "update-ref") - verdict = _bash_verdict(linked_repo, tmp_path, "linked", "git ur refs/heads/develop ") - assert verdict == "checked-out-ref" + command = "git ur refs/heads/develop " + verdict = _bash_verdict(linked_repo, tmp_path, "linked", command) + assert verdict == "checked-out-ref", _context(command, verdict) @pytest.mark.parametrize("tool_cwd", [".", ""]) @pytest.mark.parametrize("command", _RUN_CMD_UNKNOWN_CWD_DENIED) @@ -251,7 +259,7 @@ def test_denies_run_cmd_with_unknown_tool_cwd( output = _run_guard( _expand(command, linked_repo), kitchen_open=True, tmpdir=tmp_path, tool_cwd=tool_cwd ) - assert _guard_verdict(output) == "checked-out-ref" + assert _guard_verdict(output) == "checked-out-ref", _context(command, (tool_cwd, output)) def test_denies_bash_whose_workdir_names_the_linked_worktree( self, linked_repo: dict[str, Path | str], tmp_path: Path @@ -263,13 +271,14 @@ def test_denies_bash_whose_workdir_names_the_linked_worktree( _M1, tool_input_extra={"workdir": str(linked_repo["linked"])}, ) - assert verdict == "checked-out-ref" + assert verdict == "checked-out-ref", _context(_M1, verdict) @pytest.mark.parametrize(("cwd", "command"), _ALLOWED_FROM_CWD) def test_allows_command_that_cannot_reach_a_checked_out_ref( self, linked_repo: dict[str, Path | str], tmp_path: Path, cwd: str, command: str ) -> None: - assert _bash_verdict(linked_repo, tmp_path, cwd, command) == "allow" + verdict = _bash_verdict(linked_repo, tmp_path, cwd, command) + assert verdict == "allow", _context(command, (cwd, verdict)) @pytest.mark.parametrize("command", _RUN_CMD_UNKNOWN_CWD_ALLOWED) def test_allows_run_cmd_with_empty_tool_cwd_when_no_ref_is_threatened( @@ -278,7 +287,7 @@ def test_allows_run_cmd_with_empty_tool_cwd_when_no_ref_is_threatened( output = _run_guard( _expand(command, linked_repo), kitchen_open=True, tmpdir=tmp_path, tool_cwd="" ) - assert _guard_verdict(output) == "allow" + assert _guard_verdict(output) == "allow", _context(command, output) @pytest.mark.parametrize(("headless", "session_type"), _SESSION_SHAPES) @pytest.mark.parametrize("command", _BENIGN_COMMANDS) @@ -293,7 +302,7 @@ def test_allows_commands_that_exit_at_the_prefilter_in_every_session_shape( verdict = _bash_verdict( linked_repo, tmp_path, "primary", command, headless=headless, session_type=session_type ) - assert verdict == "allow" + assert verdict == "allow", _context(command, (headless, session_type, verdict)) @pytest.mark.parametrize( ("cwd", "command", "verdict"), @@ -317,4 +326,4 @@ def test_verdict_is_the_same_in_every_session_shape( ) for shape in _SESSION_SHAPES } - assert verdicts == dict.fromkeys(_SESSION_SHAPES, verdict) + assert verdicts == dict.fromkeys(_SESSION_SHAPES, verdict), _context(command, verdicts) diff --git a/tests/infra/test_git_ops_guard_primary_head.py b/tests/infra/test_git_ops_guard_primary_head.py index 06a32eaf23..19506e6f36 100644 --- a/tests/infra/test_git_ops_guard_primary_head.py +++ b/tests/infra/test_git_ops_guard_primary_head.py @@ -15,7 +15,6 @@ from __future__ import annotations -import functools import io import json import os @@ -35,6 +34,7 @@ _HOOK_CONFIG_RELPATH, UNSET, _build_payload, + _context, _git, _guard_verdict, _make_clean_env, @@ -168,17 +168,6 @@ def repo(tmp_path: Path) -> dict[str, Path | str]: return built -@functools.cache -def _git_version() -> str: - return subprocess.run( - ["git", "--version"], capture_output=True, text=True, check=False - ).stdout.strip() - - -def _context(command: str, observed: object = "") -> str: - return f"{command!r} {observed!r} ({_git_version()})" - - def _expand(command: str, repo: dict[str, Path | str], tmp_path: Path) -> str: for name in ("primary", "linked", "unrelated"): command = command.replace(f"<{name}>", str(repo[name])) From 4a0f460b1650562dd8e491fbfd18ce19c0d3146f Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 10:28:57 -0800 Subject: [PATCH 11/46] fix(review): drop the single-caller _python_c_program wrapper Inline the regex-gated python_c_program call in evaluated_payloads and remove the wrapper, its re-export bindings and the now-unused imports so the _invocation_flags leaf is the only Python -c extractor. Addresses review findings bce825d26d, a37c97583f, d86a35a2e0, aa978f2103 Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/_classification/AGENTS.md | 4 ++-- .../hooks/_classification/_interpreters.py | 9 +++++--- .../_python_program_analysis.py | 22 ++----------------- 3 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/autoskillit/hooks/_classification/AGENTS.md b/src/autoskillit/hooks/_classification/AGENTS.md index 5f0da20682..9d425f9cce 100644 --- a/src/autoskillit/hooks/_classification/AGENTS.md +++ b/src/autoskillit/hooks/_classification/AGENTS.md @@ -34,8 +34,8 @@ block B bootstrap so hook scripts can keep importing via A wrapper option or launcher it does not model is reported *unresolved* out of band, so shared consumers keep their verb. - `_python_program_analysis.py` — Python interpreter and subprocess invocation - analysis (`_InterpreterCommandSpec`, `_python_c_program`, - `_python_program_command_specs`, `_python_program_evaluated_specs`). + analysis (`_InterpreterCommandSpec`, `_python_program_command_specs`, + `_python_program_evaluated_specs`). - `_substitution_scanning.py` — shell-substitution / process-substitution scanning and the legacy shell-payload segment traversal. - `_github_mutation_cli_analysis.py` — `gh` / `curl` mutation grammar. diff --git a/src/autoskillit/hooks/_classification/_interpreters.py b/src/autoskillit/hooks/_classification/_interpreters.py index 9edd711334..8111468ee7 100644 --- a/src/autoskillit/hooks/_classification/_interpreters.py +++ b/src/autoskillit/hooks/_classification/_interpreters.py @@ -19,7 +19,6 @@ ) from autoskillit.hooks._classification._python_program_analysis import ( _InterpreterCommandSpec, - _python_c_program, _python_program_command_specs, _python_program_evaluated_specs, ) @@ -75,7 +74,6 @@ _strip_heredoc_bodies_mapped = _tokenizer._strip_heredoc_bodies_mapped _FlagArity = _flag_arity_classification._FlagArity _InterpreterCommandSpec = _python_program_analysis._InterpreterCommandSpec - _python_c_program = _python_program_analysis._python_c_program _python_program_command_specs = _python_program_analysis._python_program_command_specs _python_program_evaluated_specs = _python_program_analysis._python_program_evaluated_specs _extract_process_substitution_occurrences = ( @@ -473,7 +471,12 @@ def evaluated_payloads(command: str) -> list[EvaluatedPayload]: EvaluatedPayload(" ".join(args), StdinConsumer.SHELL, index, "eval", None) ) else: - program = _python_c_program(_normalize_executable(verb), args) + executable = _normalize_executable(verb) + program = ( + python_c_program(args)[0] + if re.fullmatch(r"python(?:3(?:\.\d+)?)?", executable) + else None + ) if program is not None: payloads.append( EvaluatedPayload(program, StdinConsumer.PYTHON, index, "python-c", None) diff --git a/src/autoskillit/hooks/_classification/_python_program_analysis.py b/src/autoskillit/hooks/_classification/_python_program_analysis.py index 9f4212dc32..cf9fc768df 100644 --- a/src/autoskillit/hooks/_classification/_python_program_analysis.py +++ b/src/autoskillit/hooks/_classification/_python_program_analysis.py @@ -4,25 +4,14 @@ stdin-consumer/evaluated-payload machinery under the REQ-CNST-010 line cap. Parses a Python -c program (or a heredoc/herestring-fed `python3`/`python3 -` body) via the stdlib `ast` module and returns any literal-argv or literal-string -`subprocess.*`/`os.*` call it finds. Locating the `-c` program in an argv is the -`_invocation_flags` leaf's job; nothing else of `_classification/` is imported. +`subprocess.*`/`os.*` call it finds. Self-contained: no dependency on the rest of +`_classification/`. """ from __future__ import annotations import ast -import re -from collections.abc import Sequence from dataclasses import dataclass -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - from autoskillit.hooks._classification._invocation_flags import python_c_program -else: - if __package__: - from ._invocation_flags import python_c_program - else: - from _invocation_flags import python_c_program _PYTHON_SUBPROCESS_FUNCS: frozenset[str] = frozenset( { @@ -106,13 +95,6 @@ class _InterpreterCommandSpec: invokes_shell: bool = False -def _python_c_program(executable: str, args: Sequence[str]) -> str | None: - """Return the literal program passed to a normalized Python executable's -c.""" - if re.fullmatch(r"python(?:3(?:\.\d+)?)?", executable) is None: - return None - return python_c_program(args)[0] - - def _python_program_command_specs( program: str, ) -> tuple[list[_InterpreterCommandSpec], bool]: From d01b9545bfb444b0f5ddcdf461873777635fb07d Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 10:29:59 -0800 Subject: [PATCH 12/46] fix(review): document the =-joined wrapper option width exception Addresses review finding 8e0b527df8 Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/_classification/_invocation_flags.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/autoskillit/hooks/_classification/_invocation_flags.py b/src/autoskillit/hooks/_classification/_invocation_flags.py index f266b88359..5630793cdb 100644 --- a/src/autoskillit/hooks/_classification/_invocation_flags.py +++ b/src/autoskillit/hooks/_classification/_invocation_flags.py @@ -176,7 +176,8 @@ class _WrapperSpec: # 1.9). An option whose short form carries two meanings (`sudo -h` is `--help` # and `--host`) stays out of the table so it falls to the unmodelled rule: a # modelled-but-wrong arity silently mislocates the command word, while an -# unmodelled option fails closed. +# unmodelled option fails closed. The one exception is an `=`-joined token +# (`--host=h`): it is a single token whatever the option's arity. _WRAPPER_SPECS: Mapping[str, _WrapperSpec] = { "timeout": _WrapperSpec( frozenset({"--foreground", "--preserve-status", "-v", "--verbose"}), @@ -246,7 +247,10 @@ class _WrapperSpec: def _wrapper_option_width(spec: _WrapperSpec, token: str) -> int | None: - """Tokens a wrapper option occupies (1 or 2), or None when the table does not model it.""" + """Tokens a wrapper option occupies (1 or 2), or None when its width is unknown. + + An ``=``-joined token is one token whether or not the table models its option. + """ values = spec.values | spec.chdir if "=" in token: return 1 From 64711643d5067b576ca24b396080caadf783e3bf Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 10:30:55 -0800 Subject: [PATCH 13/46] fix(review): remove dead has_unresolved initialiser in interpreter spec extraction Addresses review finding 8aebc8e3a2 Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/_classification/_interpreters.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/autoskillit/hooks/_classification/_interpreters.py b/src/autoskillit/hooks/_classification/_interpreters.py index 8111468ee7..e4d8d131d0 100644 --- a/src/autoskillit/hooks/_classification/_interpreters.py +++ b/src/autoskillit/hooks/_classification/_interpreters.py @@ -685,9 +685,7 @@ def _extract_interpreter_segment_specs( return ([], False) specs: list[_InterpreterCommandSpec] = [] - has_unresolved = False - program, program_unresolved = python_c_program(args) - has_unresolved = program_unresolved + program, has_unresolved = python_c_program(args) if program is not None: found, unresolved = _python_program_command_specs(program) specs.extend(found) From 2f72380c416854dbb9b2502df60e191ec60632d9 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 10:31:58 -0800 Subject: [PATCH 14/46] fix(review): derive the walker's logical connector set from the tokenizer Addresses review finding 11d86f5404 Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/_classification/_tokenizer.py | 2 +- .../hooks/_runtime/_shell_segment_walk.py | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/autoskillit/hooks/_classification/_tokenizer.py b/src/autoskillit/hooks/_classification/_tokenizer.py index a2a789c15e..5130627142 100644 --- a/src/autoskillit/hooks/_classification/_tokenizer.py +++ b/src/autoskillit/hooks/_classification/_tokenizer.py @@ -45,7 +45,7 @@ # Operators that terminate a shlex token and split command segments. _SHELL_OPERATORS: frozenset[str] = frozenset({"&&", "||", ";", "|", "&"}) -_LOGICAL_CONNECTORS: frozenset[str] = frozenset({"&&", "||", "|", "&"}) +_LOGICAL_CONNECTORS: frozenset[str] = _SHELL_OPERATORS - {";"} _BARE_KEYWORDS: frozenset[str] = frozenset({"if", "while", "until", "then", "else", "elif", "do"}) _HEREDOC_PLACEHOLDER_RE = re.compile(r"__AUTOSKILLIT_HEREDOC_(\d+)__") diff --git a/src/autoskillit/hooks/_runtime/_shell_segment_walk.py b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py index b91fec58a4..1383da320a 100644 --- a/src/autoskillit/hooks/_runtime/_shell_segment_walk.py +++ b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py @@ -28,17 +28,21 @@ if TYPE_CHECKING: from autoskillit.hooks._classification._invocation_flags import _WRAPPER_SPECS from autoskillit.hooks._classification._output_redirect import OutputRedirectPartition - from autoskillit.hooks._classification._tokenizer import ArgvToken, EvaluatedSegment + from autoskillit.hooks._classification._tokenizer import ( + _LOGICAL_CONNECTORS, + ArgvToken, + EvaluatedSegment, + ) else: if __package__: - from .._classification import _invocation_flags + from .._classification import _invocation_flags, _tokenizer else: - from _classification import _invocation_flags + from _classification import _invocation_flags, _tokenizer _WRAPPER_SPECS = _invocation_flags._WRAPPER_SPECS + _LOGICAL_CONNECTORS = _tokenizer._LOGICAL_CONNECTORS _DIRECTORY_COMMANDS: frozenset[str] = frozenset({"cd", "pushd", "popd"}) -_LOGICAL_CONNECTORS: frozenset[str] = frozenset({"&&", "||", "|", "&"}) _TAINTING_FIRST_TOKENS: frozenset[str] = frozenset( {"if", "while", "until", "for", "select", "case", "then", "do", "else", "elif", "{"} ) From e6796caf1b1e10703442c8b3e6e428b30204886f Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 10:33:10 -0800 Subject: [PATCH 15/46] fix(review): reuse _PYTHON_NAME_RE in the shell segment walker Addresses review finding 413584c1f0 Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/_runtime/_shell_segment_walk.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/autoskillit/hooks/_runtime/_shell_segment_walk.py b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py index 1383da320a..4f735de34a 100644 --- a/src/autoskillit/hooks/_runtime/_shell_segment_walk.py +++ b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py @@ -19,14 +19,16 @@ from __future__ import annotations import os -import re from collections.abc import Sequence from dataclasses import dataclass from types import ModuleType from typing import TYPE_CHECKING if TYPE_CHECKING: - from autoskillit.hooks._classification._invocation_flags import _WRAPPER_SPECS + from autoskillit.hooks._classification._invocation_flags import ( + _PYTHON_NAME_RE, + _WRAPPER_SPECS, + ) from autoskillit.hooks._classification._output_redirect import OutputRedirectPartition from autoskillit.hooks._classification._tokenizer import ( _LOGICAL_CONNECTORS, @@ -39,6 +41,7 @@ else: from _classification import _invocation_flags, _tokenizer + _PYTHON_NAME_RE = _invocation_flags._PYTHON_NAME_RE _WRAPPER_SPECS = _invocation_flags._WRAPPER_SPECS _LOGICAL_CONNECTORS = _tokenizer._LOGICAL_CONNECTORS @@ -316,8 +319,8 @@ def _invoked_child_state( if path is None or verb_cwd == state.cwd: return None - if _classification._is_shell_interpreter(verb) or re.fullmatch( - r"python(?:3(?:\.\d+)?)?", _classification._normalize_executable(verb) + if _classification._is_shell_interpreter(verb) or _PYTHON_NAME_RE.fullmatch( + _classification._normalize_executable(verb) ): return ShellDirectoryState(verb_cwd, state.cdpath_unknown, verb_certain) return None From 90afa2ef3f8e45f4789d2d2da649c5eb67c0b691 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 10:34:33 -0800 Subject: [PATCH 16/46] fix(review): remove unreachable out-of-range branch in _apply_shell_builtin Addresses review finding c26d24f0e7 Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/_runtime/_shell_segment_walk.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/autoskillit/hooks/_runtime/_shell_segment_walk.py b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py index 4f735de34a..d54f1d50ef 100644 --- a/src/autoskillit/hooks/_runtime/_shell_segment_walk.py +++ b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py @@ -285,10 +285,6 @@ def _apply_shell_builtin( ) -> bool: if argv_tokens is None or not _unwrapped_prefix(executable[:start]): return False - if start >= len(executable): - state.cwd = "" - state.cdpath_unknown = True - return True argv = executable[start:] verb = argv[0] if verb in _DIRECTORY_COMMANDS: From a7ed03fd3e14e1aa9efee93c729732f6b2ab4a83 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 10:36:00 -0800 Subject: [PATCH 17/46] fix(review): drop the unread WalkedSegment.record field Addresses review finding 922135c298 Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/_runtime/_shell_segment_walk.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/autoskillit/hooks/_runtime/_shell_segment_walk.py b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py index d54f1d50ef..c595047840 100644 --- a/src/autoskillit/hooks/_runtime/_shell_segment_walk.py +++ b/src/autoskillit/hooks/_runtime/_shell_segment_walk.py @@ -77,7 +77,6 @@ def fork(self) -> ShellDirectoryState: @dataclass(frozen=True, slots=True) class WalkedSegment: - record: EvaluatedSegment partition: OutputRedirectPartition executable: list[str] executable_tokens: list[ArgvToken] | None @@ -399,7 +398,6 @@ def walked( position_unresolved: bool = False, ) -> WalkedSegment: return WalkedSegment( - record, partition, executable, executable_tokens, From a77707bc812f5d9b891d3d6f4e20f5a41134a100 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 10:39:21 -0800 Subject: [PATCH 18/46] fix(review): unify the command-start walk on _verb_start_index Delete the parallel _command_start_index walk; extract_git_subcommand_and_flags and the wc protected-path arm now use _verb_start_index, so the prefix rules cover leading shell keywords too. Behaviour change (intended, strictly tighter): keyword-led segments such as `if true; then sudo wc -l ; fi` and a bare keyword-led `wc -l ` are now denied, closing a bypass of the pinned wc prefix rejection. Git keyword-led segments were already denied. Adds the keyword x prefix matrices for the wc and git arms and a parity pin between the git invocation prefix and the verb start index. Addresses review findings b223fb7253, a520362272 Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/_classification/_flags.py | 7 +- src/autoskillit/hooks/_runtime/__init__.py | 2 - .../hooks/_runtime/_command_classification.py | 27 +----- tests/hooks/test_command_classification.py | 88 +++++++++++++++++++ 4 files changed, 95 insertions(+), 29 deletions(-) diff --git a/src/autoskillit/hooks/_classification/_flags.py b/src/autoskillit/hooks/_classification/_flags.py index 38b8384886..46f1c52976 100644 --- a/src/autoskillit/hooks/_classification/_flags.py +++ b/src/autoskillit/hooks/_classification/_flags.py @@ -16,7 +16,7 @@ ArgvToken, EvaluatedSegment, SearchPattern, - _command_start_index, + _verb_start_index, command_verb, extract_git_subcommand_and_flags, ) @@ -456,7 +456,8 @@ def is_allowed_protected_path_metadata_command(segment: EvaluatedSegment) -> boo return False return _is_allowed_git_metadata_invocation(git_parts.subcommand, git_parts.flags) if verb == "wc" or verb.endswith("/wc"): - start = _command_start_index(tokens) + # Any skipped prefix (keyword, assignment, `env`, wrapper) disqualifies the arm. + start = _verb_start_index(tokens) if start != 0: return False flags = [token for token in tokens[start + 1 :] if token.startswith("-")] @@ -514,7 +515,7 @@ def command_has_blocked_protected_path_read( ArgvToken = _classification.ArgvToken EvaluatedSegment = _classification.EvaluatedSegment SearchPattern = _classification.SearchPattern - _command_start_index = _classification._command_start_index + _verb_start_index = _classification._verb_start_index command_verb = _classification.command_verb extract_git_subcommand_and_flags = _classification.extract_git_subcommand_and_flags diff --git a/src/autoskillit/hooks/_runtime/__init__.py b/src/autoskillit/hooks/_runtime/__init__.py index 56c486c6c1..3c773174b8 100644 --- a/src/autoskillit/hooks/_runtime/__init__.py +++ b/src/autoskillit/hooks/_runtime/__init__.py @@ -123,7 +123,6 @@ "_cleanup_expired": "_exploration_request_record", "_clock": "_exploration_request_record", "_command_position_candidate_spans_call": "_github_mutation_analysis", - "_command_start_index": "_command_classification", "_command_verb_and_args": "_github_mutation_analysis", "_consume_argv_flag": "_command_classification", "_consume_env": "_command_classification", @@ -341,7 +340,6 @@ "_cleanup_expired", "_clock", "_command_position_candidate_spans_call", - "_command_start_index", "_command_verb_and_args", "_consume_argv_flag", "_consume_env", diff --git a/src/autoskillit/hooks/_runtime/_command_classification.py b/src/autoskillit/hooks/_runtime/_command_classification.py index 4f81c36a8c..f6e185b223 100644 --- a/src/autoskillit/hooks/_runtime/_command_classification.py +++ b/src/autoskillit/hooks/_runtime/_command_classification.py @@ -314,27 +314,6 @@ def _env_chdir_value(start: int, segment: list[str]) -> tuple[str, int, int] | N return chdir -def _command_start_index(segment: list[str]) -> int | None: - if not segment: - return None - start = 0 - while start < len(segment): - token = segment[start] - if _is_posix_assignment(token): - start += 1 - continue - if token == "env": - start = _consume_env(start + 1, segment) - continue - if token in _WRAPPER_SPECS: - start, unresolved = _wrapper_command_start(segment, start) - if unresolved: - break - continue - break - return start if start < len(segment) else None - - def _command_position(segment: list[str]) -> tuple[int | None, bool]: """Return the index of the command verb in *segment* and whether that is unresolved. @@ -454,8 +433,8 @@ def extract_git_subcommand_and_flags(segment: list[str]) -> GitInvocation | None Skips global git flags (and their value tokens) to find the subcommand, then returns its subcommand, remaining tokens, global flags, and skipped - command prefix. Returns None if the segment - is not a git command or has no subcommand. + command prefix (leading shell keywords, assignments, `env` and wrappers). + Returns None if the segment is not a git command or has no subcommand. Returns a `""` invocation when an unrecognized `-`-prefixed global flag is encountered before the subcommand -- distinguishable from None @@ -466,7 +445,7 @@ def extract_git_subcommand_and_flags(segment: list[str]) -> GitInvocation | None safe). See is_allowed_protected_path_metadata_command and `_git_target.analyze_git_segment` / `_git_target.contains_blocked_git_op`. """ - start = _command_start_index(segment) + start = _verb_start_index(segment) if start is None: return None verb = segment[start] diff --git a/tests/hooks/test_command_classification.py b/tests/hooks/test_command_classification.py index 7fe36ec45d..e17db82c74 100644 --- a/tests/hooks/test_command_classification.py +++ b/tests/hooks/test_command_classification.py @@ -932,6 +932,47 @@ def test_wrapper_options_are_skipped_to_the_command_word(self, command, index): assert _verb_start_index(segment) == index assert segment[index] == "git" + @pytest.mark.parametrize( + ("command", "index"), + [ + pytest.param(command, index, id=command) + for command, index in [ + ("git status", 0), + ("then git status", 1), + ("then sudo git status", 2), + ("! git status", 1), + ("FOO=1 git status", 1), + ("env X=1 git status", 2), + ("env -S x git status", 3), + ("timeout 5 git status", 2), + ("sudo -n git status", 2), + ("if env X=1 sudo -u root git status", 6), + ] + ], + ) + def test_git_invocation_prefix_is_the_skipped_verb_prefix(self, command, index): + segment = command.split() + assert _verb_start_index(segment) == index + invocation = extract_git_subcommand_and_flags(segment) + assert invocation is not None + assert invocation.subcommand == "status" + assert invocation.prefix_tokens == segment[:index] + + @pytest.mark.parametrize( + "command", + [ + "sudo -h git status", + "timeout --bogus 5 git status", + "xargs git status", + "uv run git status", + "command -v git", + "wc -l f", + "then", + ], + ) + def test_git_invocation_is_absent_when_the_verb_start_is_not_git(self, command): + assert extract_git_subcommand_and_flags(command.split()) is None + def test_command_v_describes_a_command_rather_than_running_one(self): segment = ["command", "-v", "git"] assert _command_position(segment) == (None, False) @@ -2863,6 +2904,53 @@ def test_wc_metadata_arm_rejects_command_prefixes(prefix: str) -> None: assert command_has_blocked_protected_path_read(command, PROTECTED_SOURCE_PATH_PATTERNS) +# The tokenizer emits the segment a shell keyword introduces with the keyword as its +# first token, so each shape puts the command under test behind exactly one keyword. +_KEYWORD_LED_SHAPES = { + "if": "if {command}; then true; fi", + "then": "if true; then {command}; fi", + "elif": "if false; then true; elif {command}; then true; fi", + "else": "if false; then true; else {command}; fi", + "while": "while {command}; do break; done", + "until": "until {command}; do break; done", + "do": "for i in 1; do {command}; done", + "!": "! {command}", +} +_KEYWORD_LED_PREFIXES = ("", "X=1", "env X=1", "sudo", "timeout 5") + + +def _keyword_led_command(keyword: str, prefix: str, invocation: str) -> str: + command = _KEYWORD_LED_SHAPES[keyword].format(command=f"{prefix} {invocation}".strip()) + segments = all_evaluated_segments_with_provenance(command) + assert segments is not None + assert any(segment.tokens[0] == keyword for segment in segments) + return command + + +@pytest.mark.parametrize("prefix", _KEYWORD_LED_PREFIXES) +@pytest.mark.parametrize("keyword", _KEYWORD_LED_SHAPES) +def test_wc_metadata_arm_rejects_keyword_led_segments(keyword: str, prefix: str) -> None: + """A leading shell keyword is a skipped prefix like any wrapper, so the wc arm denies it. + + Before the verb start was unified, a keyword stopped the prefix walk at index 0 and + admitted `if true; then sudo wc -l ; fi` even though `sudo wc -l + ` is denied. Denying the benign bare keyword-led `wc -l ` is the + accepted cost of closing that bypass. + """ + command = _keyword_led_command(keyword, prefix, f"wc -l {_PROTECTED_RECIPE}") + assert command_has_blocked_protected_path_read(command, PROTECTED_SOURCE_PATH_PATTERNS) + + +@pytest.mark.parametrize("arm", _GIT_METADATA_ARMS) +@pytest.mark.parametrize("prefix", _KEYWORD_LED_PREFIXES) +@pytest.mark.parametrize("keyword", _KEYWORD_LED_SHAPES) +def test_git_metadata_arms_reject_keyword_led_segments( + keyword: str, prefix: str, arm: str +) -> None: + command = _keyword_led_command(keyword, prefix, f"git {arm}") + assert command_has_blocked_protected_path_read(command, PROTECTED_SOURCE_PATH_PATTERNS) + + @pytest.mark.parametrize( ("command", "blocked"), [ From 978418a60a763769c39bc1cc4c6335784b3810ab Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 10:50:55 -0800 Subject: [PATCH 19/46] fix(review): resolve unreadable rev-parse output as git_query_failed Decode the repository probe output inside one guard so non-UTF-8 paths and short output yield UNRESOLVED(git_query_failed) instead of an exception that denies even read-only Git commands. Addresses review findings 2092feb24f Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/_runtime/_git_target.py | 13 +++++++---- tests/hooks/test_git_target.py | 23 ++++++++++++++++--- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/autoskillit/hooks/_runtime/_git_target.py b/src/autoskillit/hooks/_runtime/_git_target.py index d56e1bcf7c..3af1ed2b76 100644 --- a/src/autoskillit/hooks/_runtime/_git_target.py +++ b/src/autoskillit/hooks/_runtime/_git_target.py @@ -142,14 +142,17 @@ def _probe_repository(path: str) -> GitTarget: if b"not a git repository" in probe.stderr: return GitTarget(GitTargetKind.NOT_A_REPOSITORY, "", path, "", "", False, None) return _unresolved("git_query_failed", path) - git_dir_raw, common_raw = probe.stdout.decode("utf-8", errors="strict").splitlines()[:2] - worktree_git_dir = canonical_existing_path(git_dir_raw) - # ``--git-common-dir`` is relative to the queried path (``.`` inside ``.git``). - common_git_dir = canonical_existing_path(os.path.join(path, common_raw)) # ``--show-toplevel`` fails inside ``.git`` and in a bare repository, so it # is a separate query. top = _git_result(path, "rev-parse", "--show-toplevel") - toplevel = top.stdout.decode("utf-8", errors="strict").strip() if top.returncode == 0 else None + try: + git_dir_raw, common_raw = probe.stdout.decode("utf-8").splitlines()[:2] + toplevel = top.stdout.decode("utf-8").strip() if top.returncode == 0 else None + except ValueError: + return _unresolved("git_query_failed", path) + worktree_git_dir = canonical_existing_path(git_dir_raw) + # ``--git-common-dir`` is relative to the queried path (``.`` inside ``.git``). + common_git_dir = canonical_existing_path(os.path.join(path, common_raw)) return GitTarget( GitTargetKind.RESOLVED, "", diff --git a/tests/hooks/test_git_target.py b/tests/hooks/test_git_target.py index f0c39a59f1..0ff8cf780c 100644 --- a/tests/hooks/test_git_target.py +++ b/tests/hooks/test_git_target.py @@ -2,9 +2,10 @@ Members: checkout, linked-worktree, ``.git`` and bare-repository targets, the non-repository target, unproven-cwd and ``-C`` rows, layout-override and -unparsed-global-option rows, dynamic command and subcommand identity, the alias -channel, alias expansion, ``git-`` and external commands, -``resolve_git_target_for_path`` and the builtin/mutation subcommand sets. +unparsed-global-option rows, unreadable Git output, dynamic command and +subcommand identity, the alias channel, alias expansion, ``git-`` and +external commands, ``resolve_git_target_for_path`` and the builtin/mutation +subcommand sets. Every row analyses a whole segment produced by ``walk_evaluated_segments``, in real repositories under ``tmp_path_factory`` with Git configuration isolated @@ -577,6 +578,22 @@ def recording_git_result(cwd: str, *args: str) -> Any: assert len(queries) == queries_after_first, f"{queries} ({_git_version()})" +@pytest.mark.parametrize( + "stdout", [b"/r/\xff\n/r/\xff\n", b"/only/one\n"], ids=["not_utf8", "one_line"] +) +def test_unreadable_rev_parse_output_is_an_unresolved_git_query( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, stdout: bytes +) -> None: + def fake_git_result(cwd: str, *args: str) -> subprocess.CompletedProcess[bytes]: + return subprocess.CompletedProcess(args=[], returncode=0, stdout=stdout, stderr=b"") + + monkeypatch.setattr(_git_target, "_git_result", fake_git_result) + + target = resolve_git_target_for_path(str(tmp_path), {}) + + _assert_unresolved(str(tmp_path), target, "git_query_failed") + + def test_mutation_subcommands_are_builtin_subcommands() -> None: assert _GIT_MUTATION_SUBCOMMANDS <= _GIT_BUILTIN_SUBCOMMANDS, sorted( _GIT_MUTATION_SUBCOMMANDS - _GIT_BUILTIN_SUBCOMMANDS From 92fe91dec20eab06a7b16b2eef3538b32382fc2d Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 10:51:58 -0800 Subject: [PATCH 20/46] fix(review): match only real Git include config keys as a layout channel The layout channel regex matched any "include." token, so filenames such as include.txt marked every Git segment UNRESOLVED(layout_override). Match the include.path and includeIf. config keys only. Addresses review findings 590da0ca4c Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/_runtime/_git_target.py | 2 +- tests/hooks/test_shell_segment_walk.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/autoskillit/hooks/_runtime/_git_target.py b/src/autoskillit/hooks/_runtime/_git_target.py index 3af1ed2b76..3c7c3718be 100644 --- a/src/autoskillit/hooks/_runtime/_git_target.py +++ b/src/autoskillit/hooks/_runtime/_git_target.py @@ -69,7 +69,7 @@ _LAYOUT_ENV_NAMES: tuple[str, ...] = ("GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR") _LAYOUT_CHANNEL_RE = re.compile( r"\b(?:GIT_DIR|GIT_WORK_TREE|GIT_COMMON_DIR)=|--git-dir\b|--work-tree\b|--bare\b" - r"|core\.(?:worktree|bare)|include(?:If)?\." + r"|core\.(?:worktree|bare)|include\.path|includeIf\." ) _ALIAS_CHANNEL_RE = re.compile(r"alias\.|GIT_CONFIG|\bHOME=|XDG_CONFIG_HOME=") _ALIAS_KEY_RE = re.compile(r"[A-Za-z0-9][A-Za-z0-9-]*") diff --git a/tests/hooks/test_shell_segment_walk.py b/tests/hooks/test_shell_segment_walk.py index 5721f4f526..95b42ae058 100644 --- a/tests/hooks/test_shell_segment_walk.py +++ b/tests/hooks/test_shell_segment_walk.py @@ -310,6 +310,7 @@ def test_dropped_bare_keyword_keeps_the_pending_connector() -> None: "git --bare x", "git -c core.worktree=/r x", "git -c include.path=/x y", + "git -c includeIf.gitdir:/a/.path=/x y", ) _ALIAS_COMMANDS = ( "git -c alias.x=y x", @@ -329,6 +330,17 @@ def test_layout_channels_in_command_text(command: str) -> None: assert _flags(command) == ChannelFlags(layout_override=True, alias_channel=False) +@pytest.mark.parametrize( + "command", + [ + 'git commit -m "add include.txt" && git checkout feature', + "cat include.h && git checkout x", + ], +) +def test_include_in_a_filename_is_not_a_layout_channel(command: str) -> None: + assert _flags(command) == ChannelFlags(layout_override=False, alias_channel=False) + + @pytest.mark.parametrize("command", _ALIAS_COMMANDS) def test_alias_channels_in_command_text(command: str) -> None: assert _flags(command) == ChannelFlags(layout_override=False, alias_channel=True) From a7411cadc2787bc5d2bc9d0e14aad747e20e1b28 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 10:53:00 -0800 Subject: [PATCH 21/46] fix(review): name the headless blocklist matcher in the Git target docstring List contains_blocked_git_op and the guard-imported helpers in the Members line, and say why the matcher lives in this module. Addresses review findings 78f5d21299 cf4a5419a1 610206901b Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/_runtime/_git_target.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/autoskillit/hooks/_runtime/_git_target.py b/src/autoskillit/hooks/_runtime/_git_target.py index 3c7c3718be..2a9b8ec5ee 100644 --- a/src/autoskillit/hooks/_runtime/_git_target.py +++ b/src/autoskillit/hooks/_runtime/_git_target.py @@ -2,7 +2,8 @@ Members: GitTargetKind, GitTarget, ChannelFlags, GitSegmentFacts, command_channel_flags, analyze_git_segment, resolve_git_target_for_path, -expand_git_alias, _GIT_BUILTIN_SUBCOMMANDS, _GIT_MUTATION_SUBCOMMANDS. +expand_git_alias, contains_blocked_git_op, _mentions_git_mutation, +_mutation_names, _GIT_BUILTIN_SUBCOMMANDS, _GIT_MUTATION_SUBCOMMANDS. ``analyze_git_segment`` is the single analysis entry for a walked shell segment. The target is RESOLVED only from a proven working directory; an @@ -11,6 +12,12 @@ inside any repository is NOT_A_REPOSITORY. Callers decide what each kind means for the command at hand. +``contains_blocked_git_op`` is the headless destructive-operation matcher. It +lives here because it reads every command through ``analyze_git_segment`` and +shares its cache, and this module already imports ``_git_command_classification``, +which therefore cannot import it back. It asks ``_git_outcome.risky_git_operation`` +which of the caller's blocked operations each segment performs. + Every Git query goes through ``_git_result`` (locale-stable), once per canonical path per ``cache``. """ From 6c3bb56501526b2a02227d259e1001ed3474fd77 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 10:59:37 -0800 Subject: [PATCH 22/46] fix(review): reuse _ambiguous_outcome for unknown-identity segments Replace the inline ambiguous GitRefOutcome in analyze_git_segment with the helper _git_outcome already provides, dropping the single-use GitHeadEffect import. Addresses review findings d508c2a1e4 Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/_runtime/_git_target.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/autoskillit/hooks/_runtime/_git_target.py b/src/autoskillit/hooks/_runtime/_git_target.py index 2a9b8ec5ee..624fea9f60 100644 --- a/src/autoskillit/hooks/_runtime/_git_target.py +++ b/src/autoskillit/hooks/_runtime/_git_target.py @@ -38,7 +38,7 @@ tokenize_command_segments, ) from _git_command_classification import _git_result -from _git_outcome import GitHeadEffect, GitRefOutcome, git_ref_outcome, risky_git_operation +from _git_outcome import GitRefOutcome, _ambiguous_outcome, git_ref_outcome, risky_git_operation from _github_mutation_analysis import _DYNAMIC_SHELL_TOKEN_RE from _path_identity import PathIdentityError, canonical_existing_path from _shell_segment_walk import WalkedSegment, walk_evaluated_segments @@ -286,7 +286,7 @@ def analyze_git_segment( else: invocation = expanded outcome = ( - GitRefOutcome(None, False, "HEAD", True, GitHeadEffect.AMBIGUOUS) + _ambiguous_outcome() if identity_unknown else git_ref_outcome(invocation.subcommand, invocation.flags) ) From d768382f14ebe2c9158fa46cf65764c588b42c5e Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:02:31 -0800 Subject: [PATCH 23/46] fix(review): report a non-string Bash workdir as an unknown execution cwd A present non-string workdir silently attributed the command to the session cwd. Follow the RELATIVE_WORKDIR precedent: record NON_STRING_WORKDIR and leave the execution cwd unknown so ref-mutating Git commands fail closed. Addresses review findings 16652de20e Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/_runtime/_hook_payload.py | 12 ++-- tests/hooks/test_hook_payload.py | 62 +++++++++++++++++-- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/autoskillit/hooks/_runtime/_hook_payload.py b/src/autoskillit/hooks/_runtime/_hook_payload.py index 995e6460c7..00bc28b2f2 100644 --- a/src/autoskillit/hooks/_runtime/_hook_payload.py +++ b/src/autoskillit/hooks/_runtime/_hook_payload.py @@ -21,6 +21,7 @@ class PayloadAnomaly(StrEnum): FIELD_CONFUSION = "field_confusion" NON_STRING_COMMAND = "non_string_command" NON_STRING_CWD = "non_string_cwd" + NON_STRING_WORKDIR = "non_string_workdir" RELATIVE_CWD = "relative_cwd" RELATIVE_WORKDIR = "relative_workdir" @@ -65,14 +66,17 @@ def _execution_cwd( """The directory the command runs in, and every anomaly in the cwd-shaped fields. A Bash-shaped payload may carry a per-call ``workdir`` (Codex); when it - does, that, not the session cwd, is where the command runs. A malformed - session cwd is still reported: it is a defect of the payload whether or not - a workdir overrides it. + does, that, not the session cwd, is where the command runs. A present + non-string or relative workdir is NON_STRING_WORKDIR or RELATIVE_WORKDIR + with the execution cwd unknown. A malformed session cwd is still reported: + it is a defect of the payload whether or not a workdir overrides it. """ session_cwd, session_anomaly = _absolute_string_or_blank(raw_cwd) anomalies = () if session_anomaly is None else (session_anomaly,) workdir = tool_input.get("workdir") - if tool_kind == "bash" and isinstance(workdir, str): + if tool_kind == "bash" and workdir is not None: + if not isinstance(workdir, str): + return ("", (*anomalies, PayloadAnomaly.NON_STRING_WORKDIR)) if os.path.isabs(workdir): return (workdir, anomalies) return ("", (*anomalies, PayloadAnomaly.RELATIVE_WORKDIR)) diff --git a/tests/hooks/test_hook_payload.py b/tests/hooks/test_hook_payload.py index 0b97ba206d..1f31a54bbb 100644 --- a/tests/hooks/test_hook_payload.py +++ b/tests/hooks/test_hook_payload.py @@ -1,10 +1,10 @@ """Payload facts extracted by ``parse_hook_command`` in hooks/_runtime/_hook_payload.py. -Members: the Bash per-call ``workdir`` rows (absolute, relative and blank), the -payload-cwd rows that carry no ``workdir``, the session-cwd anomalies that +Members: the Bash per-call ``workdir`` rows (absolute, relative, blank, non-string +and null), the payload-cwd rows that carry no ``workdir``, the session-cwd anomalies that survive a ``workdir``, the run_cmd rows that ignore it, and the -github_mutation_guard decisions that separate ``RELATIVE_WORKDIR`` from the -session-cwd anomalies. +github_mutation_guard decisions that separate ``RELATIVE_WORKDIR`` and +``NON_STRING_WORKDIR`` from the session-cwd anomalies. """ from __future__ import annotations @@ -66,6 +66,38 @@ def test_relative_or_blank_workdir_is_a_relative_workdir_anomaly( assert parsed.anomalies == (PayloadAnomaly.RELATIVE_WORKDIR,) +@pytest.mark.parametrize("workdir", [7, [], {}, True], ids=repr) +def test_non_string_workdir_is_a_non_string_workdir_anomaly( + tmp_path: Path, workdir: object +) -> None: + event = make_hook_event( + tool="Bash", + command=_COMMAND, + payload_cwd=str(tmp_path), + extra_tool_input={"workdir": workdir}, + ) + + parsed = parse_hook_command(event) + + assert parsed.execution_cwd == "" + assert parsed.payload_cwd == str(tmp_path) + assert parsed.anomalies == (PayloadAnomaly.NON_STRING_WORKDIR,) + + +def test_null_workdir_is_absent_and_uses_the_session_cwd(tmp_path: Path) -> None: + event = make_hook_event( + tool="Bash", + command=_COMMAND, + payload_cwd=str(tmp_path), + extra_tool_input={"workdir": None}, + ) + + parsed = parse_hook_command(event) + + assert parsed.execution_cwd == str(tmp_path) + assert parsed.anomalies == () + + @pytest.mark.parametrize( ("payload_cwd", "execution_cwd", "anomalies"), [ @@ -136,6 +168,24 @@ def test_relative_workdir_does_not_trigger_the_github_guard_malformed_cwd_denial assert decision.trigger is None +def test_non_string_workdir_does_not_trigger_the_github_guard_malformed_cwd_denial( + tmp_path: Path, +) -> None: + event = make_hook_event( + tool="Bash", + command=_COMMAND, + payload_cwd=str(tmp_path), + extra_tool_input={"workdir": 7}, + ) + parsed = parse_hook_command(event) + + decision = decide(parsed) + + assert parsed.anomalies == (PayloadAnomaly.NON_STRING_WORKDIR,) + assert decision.allow is True + assert decision.trigger is None + + @pytest.mark.parametrize( ("payload_cwd", "workdir", "execution_cwd", "anomalies"), [ @@ -147,16 +197,18 @@ def test_relative_workdir_does_not_trigger_the_github_guard_malformed_cwd_denial "", (PayloadAnomaly.RELATIVE_CWD, PayloadAnomaly.RELATIVE_WORKDIR), ), + (7, 7, "", (PayloadAnomaly.NON_STRING_CWD, PayloadAnomaly.NON_STRING_WORKDIR)), ], ids=[ "relative-session-cwd-absolute-workdir", "non-string-session-cwd-absolute-workdir", "relative-session-cwd-relative-workdir", + "non-string-session-cwd-non-string-workdir", ], ) def test_session_cwd_anomalies_survive_a_workdir_and_keep_the_github_guard_denial( payload_cwd: object, - workdir: str, + workdir: object, execution_cwd: str, anomalies: tuple[PayloadAnomaly, ...], ) -> None: From a9252b83efad7becb7a570dee31c48bf7b98be46 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:03:32 -0800 Subject: [PATCH 24/46] fix(review): scope the Git option-table module docstring to include the sed table Addresses review findings 72491a2f11 Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/_runtime/_git_option_tables.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/autoskillit/hooks/_runtime/_git_option_tables.py b/src/autoskillit/hooks/_runtime/_git_option_tables.py index 051e99adb9..6ba3e37f08 100644 --- a/src/autoskillit/hooks/_runtime/_git_option_tables.py +++ b/src/autoskillit/hooks/_runtime/_git_option_tables.py @@ -1,4 +1,8 @@ -"""Option tables for the Git subcommands whose argv the guard classifies. +"""Option tables for the commands whose argv ``git_ops_guard`` classifies. + +Covers the Git subcommands in ``_GIT_SUBCOMMAND_TABLES`` and ``sed`` (``_SED_FLAG_SPEC``), +whose in-place edit makes its last operand a raw write target in the guard's raw-write scan. +Both are read by the ``_option_grammar`` engine. Members: _GIT_CHECKOUT_FLAG_SPEC, _GIT_SWITCH_FLAG_SPEC, _GIT_BRANCH_FLAG_SPEC, _GIT_SYMBOLIC_REF_FLAG_SPEC, _GIT_UPDATE_REF_FLAG_SPEC, _GIT_RESET_FLAG_SPEC, From 3f71287d414486186d1ee3415c20ea99a8474a24 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:07:14 -0800 Subject: [PATCH 25/46] fix(review): share one remote-and-refspecs record between push and fetch parsers PushArgs and FetchArgs were field-for-field identical and unused outside _git_outcome; collapse them into RemoteArgs. Addresses review findings 709440d12d Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/_runtime/_git_outcome.py | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/autoskillit/hooks/_runtime/_git_outcome.py b/src/autoskillit/hooks/_runtime/_git_outcome.py index 090e5908df..949be1818d 100644 --- a/src/autoskillit/hooks/_runtime/_git_outcome.py +++ b/src/autoskillit/hooks/_runtime/_git_outcome.py @@ -1,8 +1,8 @@ """Git command outcomes: what an argv does, not which tokens it contains. Members: GitHeadEffect, GitRefOutcome, git_ref_outcome, UpdateRefArgs, -parse_update_ref, ResetArgs, parse_reset, PushArgs, parse_push, FetchArgs, -parse_fetch, is_ref_mutation, risky_git_operation, _RISKY_PREDICATES. +parse_update_ref, ResetArgs, parse_reset, RemoteArgs, parse_push, parse_fetch, +is_ref_mutation, risky_git_operation, _RISKY_PREDICATES. Every classification reads options through the ``_option_grammar`` engine over the tables in ``_git_option_tables``, so a stuck, bundled, abbreviated or @@ -308,33 +308,26 @@ def _remote_and_refspecs( return (repo.value, ()) if repo is not None else (None, ()) -class PushArgs(NamedTuple): +class RemoteArgs(NamedTuple): parsed: ParsedArgv remote: str | None refspecs: tuple[str, ...] unresolved: str | None -def parse_push(args: Sequence[str]) -> PushArgs: +def parse_push(args: Sequence[str]) -> RemoteArgs: parsed = parse_options(args, _GIT_PUSH_FLAG_SPEC) remote, refspecs = _remote_and_refspecs(parsed, _active(parsed, "repo")) - return PushArgs(parsed, remote, refspecs, parsed.unresolved) + return RemoteArgs(parsed, remote, refspecs, parsed.unresolved) -class FetchArgs(NamedTuple): - parsed: ParsedArgv - remote: str | None - refspecs: tuple[str, ...] - unresolved: str | None - - -def parse_fetch(args: Sequence[str]) -> FetchArgs: +def parse_fetch(args: Sequence[str]) -> RemoteArgs: parsed = parse_options(args, _GIT_FETCH_FLAG_SPEC) unresolved = parsed.unresolved if unresolved is None and _active(parsed, "stdin"): unresolved = "stdin" remote, refspecs = _remote_and_refspecs(parsed, None) - return FetchArgs(parsed, remote, refspecs, unresolved) + return RemoteArgs(parsed, remote, refspecs, unresolved) def _path_like_remote(remote: str) -> bool: From 1ae5a4eb15594bc130f2b31fbcf2768442845b50 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:11:10 -0800 Subject: [PATCH 26/46] fix(review): treat an interactive git clean as destructive Only a dry run is non-destructive: Git reads the clean menu from stdin, so a piped or scripted answer deletes untracked files with no human involved. Move the pinned interactive rows to the risky side, pin the piped forms through the guard, and let the differential feed the menu answer so it can see the effect. Addresses review findings 0852e4c2d7 Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/_runtime/_git_outcome.py | 6 ++++-- tests/hooks/test_git_effect_differential.py | 15 +++++++++++---- tests/hooks/test_git_outcome.py | 6 ++++-- tests/infra/test_git_ops_guard.py | 6 +++++- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/autoskillit/hooks/_runtime/_git_outcome.py b/src/autoskillit/hooks/_runtime/_git_outcome.py index 949be1818d..652a62345a 100644 --- a/src/autoskillit/hooks/_runtime/_git_outcome.py +++ b/src/autoskillit/hooks/_runtime/_git_outcome.py @@ -401,8 +401,10 @@ def _risky_hard_reset(parsed: ParsedArgv) -> bool: def _risky_clean(parsed: ParsedArgv) -> bool: """Any clean that is not a dry run: Git refuses an unforced clean unless config - says otherwise, so this covers every config channel without querying config.""" - return not any(_active(parsed, name) for name in ("dry-run", "interactive")) + says otherwise, so this covers every config channel without querying config. + ``-i`` is no exemption: Git reads its menu from stdin, so a piped answer cleans + with no human.""" + return _active(parsed, "dry-run") is None def _risky_clean_directories(parsed: ParsedArgv) -> bool: diff --git a/tests/hooks/test_git_effect_differential.py b/tests/hooks/test_git_effect_differential.py index b2624a34c9..cb3b4a1a8a 100644 --- a/tests/hooks/test_git_effect_differential.py +++ b/tests/hooks/test_git_effect_differential.py @@ -117,8 +117,13 @@ def _pinned_git_environment(home: Path) -> Iterator[None]: yield -def _git(cwd: Path, *args: str, check: bool = True) -> subprocess.CompletedProcess[str]: - """Run Git under the environment pinned by ``_pinned_git_environment``.""" +def _git( + cwd: Path, *args: str, check: bool = True, stdin_text: str = "" +) -> subprocess.CompletedProcess[str]: + """Run Git under the environment pinned by ``_pinned_git_environment``. + + Git reads ``stdin_text`` on stdin; the default is an immediate end of input. + """ return subprocess.run( ["git", *args], cwd=cwd, @@ -126,7 +131,7 @@ def _git(cwd: Path, *args: str, check: bool = True) -> subprocess.CompletedProce capture_output=True, text=True, errors="replace", - stdin=subprocess.DEVNULL, + input=stdin_text, timeout=_GIT_TIMEOUT_SECONDS, ) @@ -1185,7 +1190,9 @@ def _run_destructive( if not require_force: _git(sandbox.primary, "config", "clean.requireForce", "false") before = _snapshot(sandbox) - completed = _git(sandbox.primary, subcommand, *argv, check=False) + # An interactive clean takes its menu answer from stdin, and ``c`` cleans. + stdin_text = "c\n" if subcommand == "clean" else "" + completed = _git(sandbox.primary, subcommand, *argv, check=False, stdin_text=stdin_text) after = _snapshot(sandbox) effect = _observed_effect(sandbox.primary.parent / "origin.git", subcommand, before, after) return _DestructiveRun( diff --git a/tests/hooks/test_git_outcome.py b/tests/hooks/test_git_outcome.py index 1e82b6edcb..f4595882ba 100644 --- a/tests/hooks/test_git_outcome.py +++ b/tests/hooks/test_git_outcome.py @@ -433,6 +433,10 @@ def _risky_cases(expected: tuple[str, ...] | None, commands: tuple[str, ...]) -> "clean -x", "clean -n --no-dry-run", "clean -e pattern -f", + "clean -i", + "clean -fi", + "clean --interactive -d", + "clean -n -i --no-dry-run", ), ), *_risky_cases(_AMEND, ("commit --amend", "commit --amen", "commit --am --no-edit")), @@ -497,8 +501,6 @@ def _risky_cases(expected: tuple[str, ...] | None, commands: tuple[str, ...]) -> "clean -n", "clean -nd", "clean --dry-run -d", - "clean -i", - "clean --interactive -d", "clean -fni", "clean -f -n", ), diff --git a/tests/infra/test_git_ops_guard.py b/tests/infra/test_git_ops_guard.py index 36010562e9..8a1c099711 100644 --- a/tests/infra/test_git_ops_guard.py +++ b/tests/infra/test_git_ops_guard.py @@ -1640,7 +1640,11 @@ def _git_rows(op: tuple[str, ...], *tails: str) -> list[tuple[str, tuple[str, .. "clean -ff", "clean -d", "-c clean.requireForce=0 clean -d", + "clean -i", + "clean --interactive -d", ), + ("printf 'c\\n' | git clean -fdi", _FORCE_CLEAN), + ("echo 1 | git clean -id", _FORCE_CLEAN), *_git_rows(_AMEND, "commit --amen", "commit --am --no-edit"), ("git-commit --amend", _AMEND), *_git_rows( @@ -1661,7 +1665,7 @@ def _git_rows(op: tuple[str, ...], *tails: str) -> list[tuple[str, tuple[str, .. "git clean -n", "git clean -nd", "git clean --dry-run -d", - "git clean -i", + "git clean -fni", "git checkout -- a.txt", "git checkout feature", "git -c user.name=x commit -m x", From 6aa5a381b8fd7f3ce603c91c0bfbf76b7243e493 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:12:30 -0800 Subject: [PATCH 27/46] fix(review): pin the Git builtin subcommand inventory to its reference Git Add a version-gated whole-set equality test of _GIT_BUILTIN_SUBCOMMANDS against git --list-cmds=builtins, skipping when the installed Git differs from the reference version like the sibling option-table tests, and name the pin in the constant's comment. Addresses review findings d8e3b783c5 Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/_runtime/_git_target.py | 3 +- tests/hooks/test_git_option_table_contract.py | 38 ++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/autoskillit/hooks/_runtime/_git_target.py b/src/autoskillit/hooks/_runtime/_git_target.py index 624fea9f60..8718a8a063 100644 --- a/src/autoskillit/hooks/_runtime/_git_target.py +++ b/src/autoskillit/hooks/_runtime/_git_target.py @@ -49,7 +49,8 @@ {"update-ref", "branch", "checkout", "switch", "reset", "fetch", "push", "symbolic-ref"} ) -# ``git --list-cmds=builtins`` on the reference Git. Aliases cannot shadow these. +# ``git --list-cmds=builtins`` on ``_GIT_TABLES_REFERENCE_VERSION``, pinned by +# tests/hooks/test_git_option_table_contract.py. Aliases cannot shadow these. _GIT_BUILTIN_SUBCOMMANDS: frozenset[str] = frozenset( ( "add am annotate apply archive bisect blame branch bugreport bundle cat-file " diff --git a/tests/hooks/test_git_option_table_contract.py b/tests/hooks/test_git_option_table_contract.py index ac37b573a2..9594560843 100644 --- a/tests/hooks/test_git_option_table_contract.py +++ b/tests/hooks/test_git_option_table_contract.py @@ -1,7 +1,8 @@ """Live completeness of the Git option tables against ``--git-completion-helper-all``. Members: _parse_completion_helper, _audit_table, git_env, git_repo, git_version, -test_table_agrees_with_live_completion_helper. +test_table_agrees_with_live_completion_helper, +test_builtin_inventory_agrees_with_live_list_cmds. ``git --git-completion-helper-all`` prints one line of whitespace-separated tokens. Before the first bare ``--`` come the long options, where a trailing @@ -10,6 +11,9 @@ the check covers long rows only. The comparison runs only on the Git the tables were derived from: at runtime an unknown option already fails closed, so a newer Git produces denies, not bypasses. + +The builtin subcommand inventory ``_GIT_BUILTIN_SUBCOMMANDS`` is held to the same +reference Git: it must equal ``git --list-cmds=builtins`` exactly. """ from __future__ import annotations @@ -29,6 +33,7 @@ _GIT_SUBCOMMAND_TABLES, _GIT_TABLES_REFERENCE_VERSION, ) +from autoskillit.hooks._runtime._git_target import _GIT_BUILTIN_SUBCOMMANDS from tests._git_topology import init_empty_checkout pytestmark = [ @@ -178,3 +183,34 @@ def test_table_agrees_with_live_completion_helper( f"{version_text}: git {subcommand} lists negations whose table row is " f"missing or not negatable: {audit.negations}" ) + + +def test_builtin_inventory_agrees_with_live_list_cmds( + git_env: dict[str, str], + git_repo: Path, + git_version: tuple[str, tuple[int, int]], +) -> None: + version_text, version = git_version + result = _run_git(git_env, git_repo, "--list-cmds=builtins") + assert result.returncode == 0, ( + f"{version_text}: git --list-cmds=builtins failed: " + f"rc={result.returncode}, stderr={result.stderr!r}" + ) + live = frozenset(result.stdout.split()) + assert len(live) >= _MIN_LIVE_NAMES, ( + f"{version_text}: git --list-cmds=builtins listed too few names: {result.stdout!r}" + ) + missing = sorted(live - _GIT_BUILTIN_SUBCOMMANDS) + stale = sorted(_GIT_BUILTIN_SUBCOMMANDS - live) + + if version != _GIT_TABLES_REFERENCE_VERSION: + pytest.skip( + f"{version_text} differs from the tables' reference Git " + f"{_GIT_TABLES_REFERENCE_VERSION}; builtins missing from the inventory: " + f"{missing}; stale inventory names: {stale}" + ) + + assert not missing and not stale, ( + f"{version_text}: _GIT_BUILTIN_SUBCOMMANDS disagrees with git --list-cmds=builtins: " + f"missing={missing} stale={stale}" + ) From 737561702ab291481cd85f602a13719aac5e7cc8 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:21:48 -0800 Subject: [PATCH 28/46] fix(review): rename misleading _READ_ONLY_SUBCOMMANDS in git_ops_guard Finding ids: e182a71837 Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/guards/git_ops_guard.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/autoskillit/hooks/guards/git_ops_guard.py b/src/autoskillit/hooks/guards/git_ops_guard.py index 83783555f3..454559b0f3 100644 --- a/src/autoskillit/hooks/guards/git_ops_guard.py +++ b/src/autoskillit/hooks/guards/git_ops_guard.py @@ -101,7 +101,9 @@ _RAW_WRITE_VERBS = frozenset({"cp", "mv", "tee", "truncate", "rm", "unlink", "sed", "dd"}) _PACK_VERBS = frozenset({"git-receive-pack", "git-upload-pack"}) -_READ_ONLY_SUBCOMMANDS = _GIT_BUILTIN_SUBCOMMANDS - _GIT_MUTATION_SUBCOMMANDS +# Builtins outside _GIT_MUTATION_SUBCOMMANDS. Not read-only (commit, merge, rebase, stash, tag +# and am write refs of the requesting worktree); they only skip the shared-ref decision. +_NON_MUTATION_SUBCOMMANDS = _GIT_BUILTIN_SUBCOMMANDS - _GIT_MUTATION_SUBCOMMANDS # Commands that mention none of these cannot reach a Git ref or repository # classification, so they skip the preflight entirely. This bounds the blast @@ -530,7 +532,7 @@ def _preflight_segment( return if anchor is not None: _enforce_primary_head(data, seg, facts, anchor) - if facts.invocation.subcommand in _READ_ONLY_SUBCOMMANDS and not facts.identity_unknown: + if facts.invocation.subcommand in _NON_MUTATION_SUBCOMMANDS and not facts.identity_unknown: return _decide_shared_ref(data, facts, cache, seg.verb_cwd if seg.cwd_certain else "") From 46b4247ef985009dd556f694a1513a775f47ccc5 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:22:55 -0800 Subject: [PATCH 29/46] fix(review): derive git_ops_guard prefilter regex from ref-storage regex Finding ids: 7804712b96, db5f2acb68 Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/hooks/guards/git_ops_guard.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/autoskillit/hooks/guards/git_ops_guard.py b/src/autoskillit/hooks/guards/git_ops_guard.py index 454559b0f3..c20277966a 100644 --- a/src/autoskillit/hooks/guards/git_ops_guard.py +++ b/src/autoskillit/hooks/guards/git_ops_guard.py @@ -105,17 +105,15 @@ # and am write refs of the requesting worktree); they only skip the shared-ref decision. _NON_MUTATION_SUBCOMMANDS = _GIT_BUILTIN_SUBCOMMANDS - _GIT_MUTATION_SUBCOMMANDS -# Commands that mention none of these cannot reach a Git ref or repository -# classification, so they skip the preflight entirely. This bounds the blast -# radius of the classifier; it is not a security boundary. -_GIT_RELEVANT_RE = re.compile( - r"(? Date: Fri, 6 Nov 2026 11:24:42 -0800 Subject: [PATCH 30/46] fix(review): drop dead None guards after anchor env binding in prepare_interactive_launch Finding ids: 877287bcda Co-Authored-By: Claude Sonnet 5.5 --- src/autoskillit/cli/session/_session_launch.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/autoskillit/cli/session/_session_launch.py b/src/autoskillit/cli/session/_session_launch.py index 70c5b32680..90ad167902 100644 --- a/src/autoskillit/cli/session/_session_launch.py +++ b/src/autoskillit/cli/session/_session_launch.py @@ -167,10 +167,10 @@ def prepare_interactive_launch( extra_env = _interactive_anchor_env(extra_env, project_dir) probe_env = dict(os.environ) - if extra_env is not None and "PATH" in extra_env: + if "PATH" in extra_env: probe_env["PATH"] = extra_env["PATH"] selector = backend.capabilities.explicit_path_env_var - if selector and extra_env is not None and selector in extra_env: + if selector and selector in extra_env: probe_env[selector] = extra_env[selector] explicit_path_env = selector if selector and selector in probe_env else None @@ -180,7 +180,7 @@ def prepare_interactive_launch( cwd=project_dir, explicit_path_env=explicit_path_env, ) - merged_extras = dict(extra_env or {}) + merged_extras = dict(extra_env) if managed_home is not None: readiness = backend.probe_launch_readiness( session_dir=managed_home.generated_home, executable=provisional From f3e1c712b95cf450fe12d49d997f711534d68e05 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:25:21 -0800 Subject: [PATCH 31/46] fix(review): correct git_ops_guard session-scope wording and rewrap in hooks docs Finding ids: 2a35a11458, 1de02cee24 Co-Authored-By: Claude Sonnet 5.5 --- docs/safety/hooks.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/docs/safety/hooks.md b/docs/safety/hooks.md index c390e02a86..84e153c009 100644 --- a/docs/safety/hooks.md +++ b/docs/safety/hooks.md @@ -117,11 +117,11 @@ caller to use the `prepare_pr → compose_pr` pipeline instead. ### `git_ops_guard.py` **Guarded tool:** `Bash`, `run_cmd` -Protects checked-out refs in every session and blocks destructive raw git CLI operations -in headless skill sessions. While the kitchen is open, and in every -autoskillit-launched interactive session (cook, order, managed fleet) whether or not it -is, a preflight denies commands that rewrite a branch ref checked out in any worktree of -the repository. In an interactive cook session it also denies moving `HEAD` of the +Denies commands that rewrite a branch ref checked out in any worktree of the +repository, and blocks destructive raw git CLI operations in headless skill +sessions. The ref preflight runs while the kitchen is open and in every +autoskillit-launched interactive session (cook, order, managed fleet), whether or not +the kitchen is open. In an interactive cook session it also denies moving `HEAD` of the anchor repository's primary checkout. Destructive operations blocked in headless skill sessions: `commit --amend`, `push --force` / `-f` / `--force-with-lease`, `reset --hard`, @@ -130,8 +130,9 @@ tokenisation with global-flag skipping so `git -C /path commit --amend` and full-path invocations like `/usr/bin/git push --force` are correctly detected. Also catches interpreter-wrapped (`python3 -c "subprocess.run(['git', 'commit', '--amend'])"`) and nested-shell forms. Session scope of the destructive-operation phase: headless -skill sessions; orchestrator sessions are exempt. Per-subcommand allow-overrides are available via `git_ops_policy` -in `.hook_config.json` for future recipes that legitimately need these operations. +skill sessions; orchestrator sessions are exempt. Per-subcommand allow-overrides are +available via `git_ops_policy` in `.hook_config.json` for future recipes that +legitimately need these operations. ### `resource_exhaustion_guard.py` **Guarded tool:** `Bash`, `run_cmd` From 03ffe2c89c06786f21252a8a490493a3a8027113 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:25:58 -0800 Subject: [PATCH 32/46] fix(review): state real merge_worktree branch contract in make-groups feature-branch step Finding ids: a406b39b7b Co-Authored-By: Claude Sonnet 5.5 --- .../skills_extended/make-groups/SKILL.md | 17 +++++++++++++---- .../skills/test_skill_placeholder_contracts.py | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/autoskillit/skills_extended/make-groups/SKILL.md b/src/autoskillit/skills_extended/make-groups/SKILL.md index 42bc95ff08..e8c16c15f4 100644 --- a/src/autoskillit/skills_extended/make-groups/SKILL.md +++ b/src/autoskillit/skills_extended/make-groups/SKILL.md @@ -296,18 +296,27 @@ When implementing multiple groups from this skill's output, **always work on a f not directly on `main`. Group implementations take multiple plan-implement-merge cycles and should not land on the base branch until the full set is audited. -After `make-groups` completes, create a feature branch before starting the pipeline: +After `make-groups` completes, create and publish a feature branch in the checkout that +`merge_worktree` merges into. `merge_worktree` requires that checkout's main worktree to have +the base branch checked out and `origin/` to exist, so `git branch` alone is not +enough: ```bash -git branch feature/{topic} +git -C switch -c feature/{topic} +git -C push -u origin feature/{topic} ``` +Run these against the pipeline's clone, not the checkout your own cook session is anchored in: +the cook-session guard denies moving `HEAD` of that primary checkout. Alternatively, create and +check out the branch yourself before launching the session. + Then run each group through the pipeline using the feature branch as `base_branch` for all `merge_worktree` calls. The `/autoskillit:audit-impl` skill accepts the manifest as input and audits all groups at once as the final gate before merging the feature branch to `develop`. -Use the `group-implementation` bundled workflow to automate this — it creates the feature -branch, runs the group loop, and gates on audit before signalling merge-ready. +The `implementation-groups` bundled recipe automates this — it bootstraps the clone, creates +and publishes the feature branch (`create_and_publish_branch`), runs the group loop, and gates +on audit before signalling merge-ready. ## Related Skills diff --git a/tests/skills/test_skill_placeholder_contracts.py b/tests/skills/test_skill_placeholder_contracts.py index f5c46a561d..d7b9ecc266 100644 --- a/tests/skills/test_skill_placeholder_contracts.py +++ b/tests/skills/test_skill_placeholder_contracts.py @@ -153,7 +153,7 @@ ("collapse-issues", "combined_url"), ("issue-splitter", "parent_url"), # source issue URL, obtained in prior step ("issue-splitter", "route"), # routing label value, from manifest - ("make-groups", "topic"), # prose: "git branch feature/{topic}" + ("make-groups", "topic"), # prose: "git -C switch -c feature/{topic}" ("merge-pr", "pr_branch"), # per-PR head branch from gh pr list ("merge-pr", "file_path"), # per-file iteration in deletion check ("merge-pr", "symbol_name"), # per-symbol grep pattern From 157398c9745902a171f86d84f5f1b98a9f49cf80 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:45:01 -0800 Subject: [PATCH 33/46] fix(review): share the ambient Git variable list through tests/_git_topology Findings: 9435d0b167, 2ff9449345 Co-Authored-By: Claude Sonnet 5.5 --- tests/_git_topology.py | 8 ++++++++ tests/hooks/test_git_effect_differential.py | 10 ++-------- tests/hooks/test_git_option_table_contract.py | 12 +++--------- tests/hooks/test_git_target.py | 15 +++++++-------- tests/infra/test_git_ops_guard.py | 9 ++------- tests/infra/test_git_ops_guard_primary_head.py | 9 ++------- 6 files changed, 24 insertions(+), 39 deletions(-) diff --git a/tests/_git_topology.py b/tests/_git_topology.py index a0940d4e39..b93c74ab5d 100644 --- a/tests/_git_topology.py +++ b/tests/_git_topology.py @@ -7,6 +7,14 @@ _GIT_TIMEOUT_SECONDS = 10 +# Ambient variables that redirect Git or change its option parsing; Git-spawning tests clear them. +AMBIENT_GIT_VARIABLES = ( + "GIT_DIR", + "GIT_WORK_TREE", + "GIT_COMMON_DIR", + "GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", +) + def _run_git(*args: str, cwd: Path | None = None) -> subprocess.CompletedProcess[str]: return subprocess.run( diff --git a/tests/hooks/test_git_effect_differential.py b/tests/hooks/test_git_effect_differential.py index cb3b4a1a8a..ea7233fca3 100644 --- a/tests/hooks/test_git_effect_differential.py +++ b/tests/hooks/test_git_effect_differential.py @@ -58,7 +58,7 @@ from hypothesis import strategies as st from autoskillit.hooks.guards import git_ops_guard -from tests._git_topology import commit_file, init_empty_checkout +from tests._git_topology import AMBIENT_GIT_VARIABLES, commit_file, init_empty_checkout pytestmark = [ pytest.mark.layer("hooks"), @@ -72,12 +72,6 @@ # Repository fixtures # --------------------------------------------------------------------------- -_STRIPPED_VARIABLES = ( - "GIT_DIR", - "GIT_WORK_TREE", - "GIT_COMMON_DIR", - "GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", -) _COMMIT_DATE = "2026-01-01T00:00:00+0000" @@ -110,7 +104,7 @@ def _pinned_git_environment(home: Path) -> Iterator[None]: not a per-call dict. """ with pytest.MonkeyPatch.context() as patch: - for name in _STRIPPED_VARIABLES: + for name in AMBIENT_GIT_VARIABLES: patch.delenv(name, raising=False) for name, value in _pinned_variables(home).items(): patch.setenv(name, value) diff --git a/tests/hooks/test_git_option_table_contract.py b/tests/hooks/test_git_option_table_contract.py index 9594560843..4bd3749012 100644 --- a/tests/hooks/test_git_option_table_contract.py +++ b/tests/hooks/test_git_option_table_contract.py @@ -34,7 +34,7 @@ _GIT_TABLES_REFERENCE_VERSION, ) from autoskillit.hooks._runtime._git_target import _GIT_BUILTIN_SUBCOMMANDS -from tests._git_topology import init_empty_checkout +from tests._git_topology import AMBIENT_GIT_VARIABLES, init_empty_checkout pytestmark = [ pytest.mark.layer("hooks"), @@ -42,12 +42,6 @@ pytest.mark.skipif(shutil.which("git") is None, reason="git binary required"), ] -_AMBIENT_GIT_VARIABLES = ( - "GIT_DIR", - "GIT_WORK_TREE", - "GIT_COMMON_DIR", - "GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", -) _MIN_LIVE_NAMES = 3 @@ -112,7 +106,7 @@ def git_env(tmp_path_factory: pytest.TempPathFactory) -> dict[str, str]: xdg_config_home = root / "xdg-config" home.mkdir() xdg_config_home.mkdir() - env = {k: v for k, v in os.environ.items() if k not in _AMBIENT_GIT_VARIABLES} + env = {k: v for k, v in os.environ.items() if k not in AMBIENT_GIT_VARIABLES} env.update( HOME=str(home), XDG_CONFIG_HOME=str(xdg_config_home), @@ -128,7 +122,7 @@ def git_repo(tmp_path_factory: pytest.TempPathFactory, git_env: dict[str, str]) # topology builder reads the process environment, so it runs under the same # isolation as every other Git call in this module. with pytest.MonkeyPatch.context() as patch: - for name in _AMBIENT_GIT_VARIABLES: + for name in AMBIENT_GIT_VARIABLES: patch.delenv(name, raising=False) for name in ("HOME", "XDG_CONFIG_HOME", "GIT_CONFIG_GLOBAL", "GIT_CONFIG_NOSYSTEM"): patch.setenv(name, git_env[name]) diff --git a/tests/hooks/test_git_target.py b/tests/hooks/test_git_target.py index 0ff8cf780c..045250aa84 100644 --- a/tests/hooks/test_git_target.py +++ b/tests/hooks/test_git_target.py @@ -41,7 +41,12 @@ ) from _shell_segment_walk import walk_evaluated_segments # type: ignore[import-not-found] -from tests._git_topology import add_linked_worktree, commit_file, init_checkout +from tests._git_topology import ( + AMBIENT_GIT_VARIABLES, + add_linked_worktree, + commit_file, + init_checkout, +) pytestmark = [ pytest.mark.layer("hooks"), @@ -50,12 +55,6 @@ ] _GIT_TIMEOUT_SECONDS = 10 -_INHERITED_REPOSITORY_ENV = ( - "GIT_DIR", - "GIT_WORK_TREE", - "GIT_COMMON_DIR", - "GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", -) _REPOSITORY_ALIASES = { "co": "checkout", "nb": "checkout -b", @@ -95,7 +94,7 @@ def _isolate_git_environment(mp: pytest.MonkeyPatch, home: Path, ceiling: Path) mp.setenv("GIT_CONFIG_GLOBAL", os.devnull) mp.setenv("GIT_CONFIG_NOSYSTEM", "1") mp.setenv("GIT_CEILING_DIRECTORIES", str(ceiling)) - for name in _INHERITED_REPOSITORY_ENV: + for name in AMBIENT_GIT_VARIABLES: mp.delenv(name, raising=False) diff --git a/tests/infra/test_git_ops_guard.py b/tests/infra/test_git_ops_guard.py index 8a1c099711..c5dc7b2d0f 100644 --- a/tests/infra/test_git_ops_guard.py +++ b/tests/infra/test_git_ops_guard.py @@ -27,7 +27,7 @@ from autoskillit.hooks._runtime._git_command_classification import _classify_fetch from autoskillit.hooks._runtime._git_option_tables import _GIT_FETCH_FLAG_SPEC from autoskillit.hooks.guards import git_ops_guard -from tests._git_topology import commit_file, init_checkout +from tests._git_topology import AMBIENT_GIT_VARIABLES, commit_file, init_checkout from tests.infra._git_ops_guard_harness import ( _TOOL_NAME, _checked_out_ref_result, @@ -1704,12 +1704,7 @@ class TestDestructiveOutcomeImmunity: def _isolated_git_environment(self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: home = tmp_path / "home" home.mkdir() - for name in ( - "GIT_DIR", - "GIT_WORK_TREE", - "GIT_COMMON_DIR", - "GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", - ): + for name in AMBIENT_GIT_VARIABLES: monkeypatch.delenv(name, raising=False) monkeypatch.setenv("GIT_CONFIG_GLOBAL", os.devnull) monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1") diff --git a/tests/infra/test_git_ops_guard_primary_head.py b/tests/infra/test_git_ops_guard_primary_head.py index 19506e6f36..55acd6dd98 100644 --- a/tests/infra/test_git_ops_guard_primary_head.py +++ b/tests/infra/test_git_ops_guard_primary_head.py @@ -29,7 +29,7 @@ import pytest from autoskillit.hooks.guards import git_ops_guard -from tests._git_topology import commit_file +from tests._git_topology import AMBIENT_GIT_VARIABLES, commit_file from tests.infra._git_ops_guard_harness import ( _HOOK_CONFIG_RELPATH, UNSET, @@ -139,12 +139,7 @@ def _isolated_git_environment(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: home = tmp_path / "home" home.mkdir() - for name in ( - "GIT_DIR", - "GIT_WORK_TREE", - "GIT_COMMON_DIR", - "GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", - ): + for name in AMBIENT_GIT_VARIABLES: monkeypatch.delenv(name, raising=False) monkeypatch.setenv("GIT_CONFIG_GLOBAL", os.devnull) monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1") From 67bbae434b3b53ff6b2a6bc1eb402353575f8af2 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:46:00 -0800 Subject: [PATCH 34/46] fix(review): share one run-record base between the differential Git runs Findings: 2f1a68e9aa, 9832c39dad Co-Authored-By: Claude Sonnet 5.5 --- tests/hooks/test_git_effect_differential.py | 34 ++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/hooks/test_git_effect_differential.py b/tests/hooks/test_git_effect_differential.py index ea7233fca3..60d6891d31 100644 --- a/tests/hooks/test_git_effect_differential.py +++ b/tests/hooks/test_git_effect_differential.py @@ -263,22 +263,26 @@ def _observe(sandbox: _Sandbox) -> dict[str, _View]: @dataclass(frozen=True, slots=True) -class _Run: +class _GitRun: subcommand: str argv: tuple[str, ...] returncode: int stderr: str - owners: tuple[str, ...] - tuples: _Tuples - outcome: GitRefOutcome | None - before: Mapping[str, _View] - after: Mapping[str, _View] git_version: str @property def command(self) -> str: return shlex.join(["git", self.subcommand, *self.argv]) + +@dataclass(frozen=True, slots=True) +class _Run(_GitRun): + owners: tuple[str, ...] + tuples: _Tuples + outcome: GitRefOutcome | None + before: Mapping[str, _View] + after: Mapping[str, _View] + @property def named_refs(self) -> set[str]: return {target for target, _, _ in self.tuples} @@ -1164,17 +1168,8 @@ def _observed_effect( @dataclass(frozen=True, slots=True) -class _DestructiveRun: - subcommand: str - argv: tuple[str, ...] - returncode: int - stderr: str +class _DestructiveRun(_GitRun): effect: str | None - git_version: str - - @property - def command(self) -> str: - return shlex.join(["git", self.subcommand, *self.argv]) def _run_destructive( @@ -1190,7 +1185,12 @@ def _run_destructive( after = _snapshot(sandbox) effect = _observed_effect(sandbox.primary.parent / "origin.git", subcommand, before, after) return _DestructiveRun( - subcommand, argv, completed.returncode, completed.stderr, effect, sandbox.git_version + subcommand=subcommand, + argv=argv, + returncode=completed.returncode, + stderr=completed.stderr, + git_version=sandbox.git_version, + effect=effect, ) From caf63c5b229074594dc9c18b03cab321d67eb331 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:46:13 -0800 Subject: [PATCH 35/46] fix(review): drop the self-testing missing_predicates canary Findings: e11cf197a5, ef9e87e8ba Co-Authored-By: Claude Sonnet 5.5 --- tests/hooks/test_git_outcome.py | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/tests/hooks/test_git_outcome.py b/tests/hooks/test_git_outcome.py index f4595882ba..390c1a274e 100644 --- a/tests/hooks/test_git_outcome.py +++ b/tests/hooks/test_git_outcome.py @@ -12,8 +12,6 @@ from __future__ import annotations -from collections.abc import Mapping - import pytest from autoskillit.hooks._runtime._git_outcome import ( @@ -355,24 +353,8 @@ def test_is_ref_mutation(subcommand: str, argv: str, expected: bool) -> None: assert is_ref_mutation(subcommand, _argv(argv)) is expected -def missing_predicates( - predicates: Mapping[tuple[str, ...], object], ops: frozenset[tuple[str, ...]] -) -> set[tuple[str, ...]]: - """The operations in *ops* that *predicates* has no entry for.""" - return set(ops) - set(predicates) - - def test_risky_predicates_are_keyed_exactly_by_the_risky_operations() -> None: assert set(_RISKY_PREDICATES) == RISKY_GIT_OPERATIONS - assert not missing_predicates(_RISKY_PREDICATES, RISKY_GIT_OPERATIONS) - - -@pytest.mark.parametrize("op", sorted(RISKY_GIT_OPERATIONS), ids=lambda op: " ".join(op)) -def test_a_predicate_map_missing_one_operation_is_reported(op: tuple[str, ...]) -> None: - canary = {key: predicate for key, predicate in _RISKY_PREDICATES.items() if key != op} - - assert set(canary) != RISKY_GIT_OPERATIONS - assert missing_predicates(canary, RISKY_GIT_OPERATIONS) == {op} _UNRESOLVED = ("",) From d163fdc47bfe86588d283cd03d8f6d37114c1402 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:46:55 -0800 Subject: [PATCH 36/46] fix(review): require HEAD-detaching commands without an outcome concept to name HEAD Finding: ee70109347 Co-Authored-By: Claude Sonnet 5.5 --- tests/hooks/test_git_effect_differential.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/hooks/test_git_effect_differential.py b/tests/hooks/test_git_effect_differential.py index 60d6891d31..7f14abcefb 100644 --- a/tests/hooks/test_git_effect_differential.py +++ b/tests/hooks/test_git_effect_differential.py @@ -838,7 +838,8 @@ def _shared_ref_violations(run: _Run) -> list[str]: be ambiguous. When the HEAD effect is NONE or PATH_RESTORE, no HEAD may change except that of a worktree sitting on a branch the command rewrites by name: renaming a checked-out branch rewrites that worktree's HEAD symref - as part of the rewrite, which the named tuple already accounts for. + as part of the rewrite, which the named tuple already accounts for. A subcommand + with no outcome concept that detaches or re-attaches a worktree HEAD must name HEAD. """ problems: list[str] = [] if not run.ambiguous: @@ -855,6 +856,13 @@ def _shared_ref_violations(run: _Run) -> list[str]: f"HEAD effect {_head_effect(run)} but {label} HEAD changed " f"{_describe_head(before_view)} -> {_describe_head(after_view)}" ) + if _head_effect(run) is None and not run.ambiguous and "HEAD" not in run.named_refs: + for label, before_view, after_view in run.head_changes: + if before_view.head_ref is None or after_view.head_ref != before_view.head_ref: + problems.append( + f"{label} HEAD changed {_describe_head(before_view)} -> " + f"{_describe_head(after_view)} but no tuple names HEAD" + ) return problems @@ -862,7 +870,8 @@ def _failure_message(run: _Run, problems: list[str]) -> str: return ( f"Property A violated by `{run.command}` (exit {run.returncode})\n" " expected: every checked-out branch that changed is named by a classifier tuple " - "or a tuple is ambiguous; a NONE/PATH_RESTORE HEAD effect changes no HEAD\n" + "or a tuple is ambiguous; a NONE/PATH_RESTORE HEAD effect changes no HEAD; " + "a command with no HEAD effect concept that detaches or re-attaches a HEAD names HEAD\n" f" actual: {'; '.join(problems)}\n" f" classifier tuples: {list(run.tuples)}\n" f" outcome: {run.outcome}\n" From 9383bb9f85b1bcef870786c68ce611513e8968ae Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:47:27 -0800 Subject: [PATCH 37/46] fix(review): derive the raw-option-token guard scope from the _git_*.py naming Finding: c2464b834d Co-Authored-By: Claude Sonnet 5.5 --- tests/arch/test_git_option_literal_scope.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/arch/test_git_option_literal_scope.py b/tests/arch/test_git_option_literal_scope.py index 407d4c2409..2a09a8c427 100644 --- a/tests/arch/test_git_option_literal_scope.py +++ b/tests/arch/test_git_option_literal_scope.py @@ -9,7 +9,9 @@ ``token.startswith("--force")``) silently allows every other spelling. Option semantics are decided through ``_option_grammar`` tables instead. -Detectors, over the Git-classification modules only: +Detectors, over ``guards/git_ops_guard.py`` plus every ``hooks/_runtime/_git_*.py`` +sibling, so a new Git-classification module is covered by naming it ``_git_*.py`` +(the shell walk and the generic command classifier are out of scope by design): (a) a ``Compare`` using ``in``/``not in``/``==``/``!=`` against a string literal that looks like an option (or a tuple/set/list literal containing one); @@ -32,9 +34,7 @@ _HOOKS_ROOT = Path(__file__).resolve().parents[2] / "src" / "autoskillit" / "hooks" _SCOPED_FILES = ( _HOOKS_ROOT / "guards" / "git_ops_guard.py", - _HOOKS_ROOT / "_runtime" / "_git_command_classification.py", - _HOOKS_ROOT / "_runtime" / "_git_outcome.py", - _HOOKS_ROOT / "_runtime" / "_git_target.py", + *sorted((_HOOKS_ROOT / "_runtime").glob("_git_*.py")), ) _OPTION_TOKEN_RE = re.compile(r"^[-+]{1,2}[A-Za-z0-9?]") _TOKEN_METHODS = frozenset( From 5c70f994cb93ff81d566b008149691b1bda3aca4 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:48:24 -0800 Subject: [PATCH 38/46] fix(review): share Git environment isolation and the Git runner through tests/_git_topology Findings: 8277e30dc7, 434126cafd Co-Authored-By: Claude Sonnet 5.5 --- tests/_git_topology.py | 24 ++++++++++++- tests/hooks/test_git_target.py | 36 ++++--------------- .../infra/test_git_ops_guard_primary_head.py | 13 ++----- 3 files changed, 32 insertions(+), 41 deletions(-) diff --git a/tests/_git_topology.py b/tests/_git_topology.py index b93c74ab5d..eb7e5b3f46 100644 --- a/tests/_git_topology.py +++ b/tests/_git_topology.py @@ -1,9 +1,14 @@ -"""Small real-Git fixtures for checkout and linked-worktree tests.""" +"""Small real-Git fixtures and environment isolation for checkout and linked-worktree tests.""" from __future__ import annotations +import os import subprocess from pathlib import Path +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + import pytest _GIT_TIMEOUT_SECONDS = 10 @@ -27,6 +32,23 @@ def _run_git(*args: str, cwd: Path | None = None) -> subprocess.CompletedProcess ) +def isolate_git_environment( + mp: pytest.MonkeyPatch, home: Path, *, ceiling: Path | None = None +) -> None: + """Point Git's user, system and discovery state into the test tree; drop ambient overrides.""" + home.mkdir(parents=True, exist_ok=True) + mp.setenv("HOME", str(home)) + mp.setenv("XDG_CONFIG_HOME", str(home / ".config")) + mp.setenv("XDG_DATA_HOME", str(home / ".local" / "share")) + mp.setenv("XDG_CACHE_HOME", str(home / ".cache")) + mp.setenv("GIT_CONFIG_GLOBAL", os.devnull) + mp.setenv("GIT_CONFIG_NOSYSTEM", "1") + if ceiling is not None: + mp.setenv("GIT_CEILING_DIRECTORIES", str(ceiling)) + for name in AMBIENT_GIT_VARIABLES: + mp.delenv(name, raising=False) + + def init_empty_checkout(path: Path) -> Path: """Initialize a checkout with local identity but no commit.""" path.mkdir(parents=True, exist_ok=True) diff --git a/tests/hooks/test_git_target.py b/tests/hooks/test_git_target.py index 045250aa84..36a0442d06 100644 --- a/tests/hooks/test_git_target.py +++ b/tests/hooks/test_git_target.py @@ -42,10 +42,11 @@ from _shell_segment_walk import walk_evaluated_segments # type: ignore[import-not-found] from tests._git_topology import ( - AMBIENT_GIT_VARIABLES, + _run_git, add_linked_worktree, commit_file, init_checkout, + isolate_git_environment, ) pytestmark = [ @@ -75,29 +76,6 @@ def _git_version() -> str: return result.stdout.strip() or "" -def _git(cwd: Path, *args: str) -> None: - subprocess.run( - ["git", *args], - cwd=cwd, - check=True, - capture_output=True, - text=True, - timeout=_GIT_TIMEOUT_SECONDS, - ) - - -def _isolate_git_environment(mp: pytest.MonkeyPatch, home: Path, ceiling: Path) -> None: - """Point Git's user, system and discovery state into the test tree.""" - home.mkdir(parents=True, exist_ok=True) - mp.setenv("HOME", str(home)) - mp.setenv("XDG_CONFIG_HOME", str(home / ".config")) - mp.setenv("GIT_CONFIG_GLOBAL", os.devnull) - mp.setenv("GIT_CONFIG_NOSYSTEM", "1") - mp.setenv("GIT_CEILING_DIRECTORIES", str(ceiling)) - for name in AMBIENT_GIT_VARIABLES: - mp.delenv(name, raising=False) - - @dataclass(frozen=True) class Topology: """Canonical paths of the repositories and directories the rows analyse.""" @@ -116,22 +94,22 @@ class Topology: def _isolated_git_environment( monkeypatch: pytest.MonkeyPatch, tmp_path: Path, tmp_path_factory: pytest.TempPathFactory ) -> None: - _isolate_git_environment(monkeypatch, tmp_path / "home", tmp_path_factory.getbasetemp()) + isolate_git_environment(monkeypatch, tmp_path / "home", ceiling=tmp_path_factory.getbasetemp()) @pytest.fixture(scope="module") def topology(tmp_path_factory: pytest.TempPathFactory) -> Topology: root = tmp_path_factory.mktemp("git-target").resolve() with pytest.MonkeyPatch.context() as mp: - _isolate_git_environment(mp, root / "build-home", tmp_path_factory.getbasetemp()) + isolate_git_environment(mp, root / "build-home", ceiling=tmp_path_factory.getbasetemp()) primary = init_checkout(root / "primary") commit_file(primary, "sub/tracked.txt", "tracked\n", "add tracked file") - _git(primary, "branch", "develop") + _run_git("branch", "develop", cwd=primary) for name, value in _REPOSITORY_ALIASES.items(): - _git(primary, "config", f"alias.{name}", value) + _run_git("config", f"alias.{name}", value, cwd=primary) linked = add_linked_worktree(primary, "review") bare = root / "bare.git" - _git(root, "init", "--bare", "-q", str(bare)) + _run_git("init", "--bare", "-q", str(bare), cwd=root) plain = root / "plain" plain.mkdir() primary_link = root / "primary-link" diff --git a/tests/infra/test_git_ops_guard_primary_head.py b/tests/infra/test_git_ops_guard_primary_head.py index 55acd6dd98..9488e46589 100644 --- a/tests/infra/test_git_ops_guard_primary_head.py +++ b/tests/infra/test_git_ops_guard_primary_head.py @@ -29,7 +29,7 @@ import pytest from autoskillit.hooks.guards import git_ops_guard -from tests._git_topology import AMBIENT_GIT_VARIABLES, commit_file +from tests._git_topology import commit_file, isolate_git_environment from tests.infra._git_ops_guard_harness import ( _HOOK_CONFIG_RELPATH, UNSET, @@ -137,16 +137,7 @@ @pytest.fixture(autouse=True) def _isolated_git_environment(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: - home = tmp_path / "home" - home.mkdir() - for name in AMBIENT_GIT_VARIABLES: - monkeypatch.delenv(name, raising=False) - monkeypatch.setenv("GIT_CONFIG_GLOBAL", os.devnull) - monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1") - monkeypatch.setenv("HOME", str(home)) - monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config")) - monkeypatch.setenv("XDG_DATA_HOME", str(home / ".local" / "share")) - monkeypatch.setenv("XDG_CACHE_HOME", str(home / ".cache")) + isolate_git_environment(monkeypatch, tmp_path / "home") @pytest.fixture From f18fdbec5f741e418bf9b015ed8c7e898070eb96 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:48:47 -0800 Subject: [PATCH 39/46] fix(review): drop the shell-walk test's duplicate ambient-environment scrub Finding: 359caa872e Co-Authored-By: Claude Sonnet 5.5 --- tests/hooks/test_shell_segment_walk.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/hooks/test_shell_segment_walk.py b/tests/hooks/test_shell_segment_walk.py index 95b42ae058..4a497a03a6 100644 --- a/tests/hooks/test_shell_segment_walk.py +++ b/tests/hooks/test_shell_segment_walk.py @@ -35,13 +35,6 @@ pytestmark = [pytest.mark.layer("hooks"), pytest.mark.small] _PLACEHOLDER_RE = re.compile(r"@(w|a|b|missing)\b") -_AMBIENT_ENV_NAMES = ("CDPATH", "GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR") - - -@pytest.fixture(autouse=True) -def _hermetic_environment(monkeypatch: pytest.MonkeyPatch) -> None: - for name in _AMBIENT_ENV_NAMES: - monkeypatch.delenv(name, raising=False) @pytest.fixture From cbd2382d3a38b563fc717597366493edaaaa6814 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:49:28 -0800 Subject: [PATCH 40/46] fix(review): assert the proven directory on every certain-cwd row Finding: d895f6d4d7 Co-Authored-By: Claude Sonnet 5.5 --- tests/hooks/test_shell_segment_walk.py | 67 ++++++++++++++------------ 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/tests/hooks/test_shell_segment_walk.py b/tests/hooks/test_shell_segment_walk.py index 4a497a03a6..9d32ba560f 100644 --- a/tests/hooks/test_shell_segment_walk.py +++ b/tests/hooks/test_shell_segment_walk.py @@ -68,37 +68,39 @@ def _walk(command: str, dirs: dict[str, str], cwd: str) -> list[WalkedSegment]: return walk -def _certainty_rows(certain: bool, *commands: str, subcommand: str = "x") -> list[object]: +def _proven_rows(verb_cwd: str, *commands: str, subcommand: str = "x") -> list[object]: return [ - pytest.param(command, certain, subcommand, id=_row_id(command, certain)) + pytest.param(command, verb_cwd, subcommand, id=_row_id(command, verb_cwd)) for command in commands ] +def _uncertain_rows(*commands: str, subcommand: str = "x") -> list[object]: + return [pytest.param(command, subcommand, id=_row_id(command, False)) for command in commands] + + _SPAWN_PROGRAM = 'import subprocess; subprocess.run(["git", "x"], cwd="@a")' _CERTAIN_CASES = [ - # The Git segment runs only after a cd that certainly ran. A missing target - # still counts inside an unbroken && chain, which skips Git when the cd fails. - *_certainty_rows( - True, + # The Git segment runs only after a cd that certainly ran. + *_proven_rows( + "@a", "cd @a && git x", "cd @a; git x", "cd @a || exit 1; git x", "pushd @a >/dev/null && git x", "make && cd @a && git x", - "cd @missing && git x", "cd @a &&\ngit x", "cd @a # go\ngit x", ), - *_certainty_rows( - True, "git status || true; cd @a && git checkout -b x", subcommand="checkout" - ), + # A missing target still counts inside an unbroken && chain, which skips Git when the cd fails. + *_proven_rows("@missing", "cd @missing && git x"), + *_proven_rows("@a", "git status || true; cd @a && git checkout -b x", subcommand="checkout"), # A child shell started after a certain cd inherits it. - *_certainty_rows(True, "cd @a && bash -c 'git x'; cd @b"), + *_proven_rows("@a", "cd @a && bash -c 'git x'; cd @b"), # A per-invocation override seeds its own certain cwd. - *_certainty_rows( - True, + *_proven_rows( + "@a", "env -C @a git x", f"python3 -c '{_SPAWN_PROGRAM}'", "cd $X && env -C @a git x", @@ -107,8 +109,7 @@ def _certainty_rows(certain: bool, *commands: str, subcommand: str = "x") -> lis _UNCERTAIN_CASES = [ # The cd may not have run, may have failed, or ran in another process. - *_certainty_rows( - False, + *_uncertain_rows( "cd @missing; git x", "true || cd @a; git x", "false && cd @a; git x", @@ -119,8 +120,7 @@ def _certainty_rows(certain: bool, *commands: str, subcommand: str = "x") -> lis "true ||\ncd @a\ngit x", ), # A cd inside a compound body or function body never certainly ran. - *_certainty_rows( - False, + *_uncertain_rows( "if false; then cd @a; fi; git x", "if false; then true; cd @a; fi; git x", "for d in a; do cd @a; done; git x", @@ -130,31 +130,38 @@ def _certainty_rows(certain: bool, *commands: str, subcommand: str = "x") -> lis "bash -c 'f() { cd @a; }; git x'", ), # Bare keyword lines leave no trace of the condition in the command text. - *_certainty_rows( - False, + *_uncertain_rows( "if\nfalse\nthen\ncd @a\nfi\ngit x", "while\ntrue\ndo\ncd @a\ndone\ngit x", ), # The connector resets at a group close. - *_certainty_rows(False, "{ true; } || cd @a; git x"), + *_uncertain_rows("{ true; } || cd @a; git x"), # A redirect's child-scope records do not hide the pipe that follows. - *_certainty_rows(False, 'cd @a >"$(echo f)" | git x'), + *_uncertain_rows('cd @a >"$(echo f)" | git x'), # Taint lasts for the scope, so an earlier pipeline over-denies. - *_certainty_rows(False, "ls | head; cd @a; git x"), - *_certainty_rows(False, "cd @a && bash -c 'false && cd @b; git x'"), + *_uncertain_rows("ls | head; cd @a; git x"), + *_uncertain_rows("cd @a && bash -c 'false && cd @b; git x'"), # A dynamic or relative override does not stand alone. - *_certainty_rows(False, "cd $X && env -C sub git x", 'env -C "$D" git x'), + *_uncertain_rows("cd $X && env -C sub git x", 'env -C "$D" git x'), ] -@pytest.mark.parametrize( - ("command", "certain", "subcommand"), [*_CERTAIN_CASES, *_UNCERTAIN_CASES] -) -def test_git_segment_cwd_certainty( - dirs: dict[str, str], command: str, certain: bool, subcommand: str +@pytest.mark.parametrize(("command", "verb_cwd", "subcommand"), _CERTAIN_CASES) +def test_proven_git_segment_cwd_is_the_directory_the_command_names( + dirs: dict[str, str], command: str, verb_cwd: str, subcommand: str ) -> None: segment = _git_segment(_walk(command, dirs, dirs["w"]), subcommand) - assert segment.cwd_certain is certain + assert segment.cwd_certain is True + assert segment.verb_cwd == _bind(verb_cwd, dirs) + + +# verb_cwd is the shell's logical directory, so it is deliberately not asserted here. +@pytest.mark.parametrize(("command", "subcommand"), _UNCERTAIN_CASES) +def test_unproven_git_segment_cwd_is_uncertain( + dirs: dict[str, str], command: str, subcommand: str +) -> None: + segment = _git_segment(_walk(command, dirs, dirs["w"]), subcommand) + assert segment.cwd_certain is False @pytest.mark.parametrize( From c5783300c36551d002dbaf898a880092b1466a54 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:52:44 -0800 Subject: [PATCH 41/46] fix(review): move the command_channel_flags rows out of the shell-walk test module Finding: 72741edbe5 Co-Authored-By: Claude Sonnet 5.5 --- tests/hooks/test_git_channel_flags.py | 70 ++++++++++++++++++++++++++ tests/hooks/test_shell_segment_walk.py | 64 +---------------------- 2 files changed, 72 insertions(+), 62 deletions(-) create mode 100644 tests/hooks/test_git_channel_flags.py diff --git a/tests/hooks/test_git_channel_flags.py b/tests/hooks/test_git_channel_flags.py new file mode 100644 index 0000000000..d1486f0484 --- /dev/null +++ b/tests/hooks/test_git_channel_flags.py @@ -0,0 +1,70 @@ +"""Command-text and hook-environment channel flags of hooks/_runtime/_git_target.py. + +Members: the layout-override rows, the include-in-a-filename rows that are no layout +channel, the alias-channel rows, the hook-environment layout-variable rows and the +ambient-HOME row, all through ``command_channel_flags``. +""" + +from __future__ import annotations + +import pytest + +from autoskillit.hooks._runtime._command_classification import live_command_text +from autoskillit.hooks._runtime._git_target import ChannelFlags, command_channel_flags + +pytestmark = [pytest.mark.layer("hooks"), pytest.mark.small] + +_LAYOUT_COMMANDS = ( + "export GIT_DIR=/r/.git; git x", + "GIT_WORK_TREE=/r git x", + "git --git-dir=/r x", + "git --bare x", + "git -c core.worktree=/r x", + "git -c include.path=/x y", + "git -c includeIf.gitdir:/a/.path=/x y", +) +_ALIAS_COMMANDS = ( + "git -c alias.x=y x", + "export GIT_CONFIG_COUNT=1; git x", + "HOME=/h git x", + "export XDG_CONFIG_HOME=/x; git x", + "git config alias.ur update-ref && git ur a b", +) + + +def _flags(command: str) -> ChannelFlags: + return command_channel_flags(live_command_text(command)) + + +@pytest.mark.parametrize("command", _LAYOUT_COMMANDS) +def test_layout_channels_in_command_text(command: str) -> None: + assert _flags(command) == ChannelFlags(layout_override=True, alias_channel=False) + + +@pytest.mark.parametrize( + "command", + [ + 'git commit -m "add include.txt" && git checkout feature', + "cat include.h && git checkout x", + ], +) +def test_include_in_a_filename_is_not_a_layout_channel(command: str) -> None: + assert _flags(command) == ChannelFlags(layout_override=False, alias_channel=False) + + +@pytest.mark.parametrize("command", _ALIAS_COMMANDS) +def test_alias_channels_in_command_text(command: str) -> None: + assert _flags(command) == ChannelFlags(layout_override=False, alias_channel=True) + + +@pytest.mark.parametrize("name", ["GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR"]) +def test_layout_variable_in_the_hook_environment_sets_layout_override( + monkeypatch: pytest.MonkeyPatch, name: str +) -> None: + monkeypatch.setenv(name, "/r/.git") + assert _flags("git x") == ChannelFlags(layout_override=True, alias_channel=False) + + +def test_ambient_home_alone_sets_neither_flag(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("HOME", "/h") + assert _flags("git x") == ChannelFlags(layout_override=False, alias_channel=False) diff --git a/tests/hooks/test_shell_segment_walk.py b/tests/hooks/test_shell_segment_walk.py index 9d32ba560f..a8a0a3f924 100644 --- a/tests/hooks/test_shell_segment_walk.py +++ b/tests/hooks/test_shell_segment_walk.py @@ -2,7 +2,7 @@ Members: ``walk_evaluated_segments`` ``cwd_certain`` and ``verb_cwd`` rows, the tokenizer's ``connector``, ``after_keyword`` and ``piped_from_previous`` fields -with the pipe-payload reach they keep, and the ``command_channel_flags`` rows. +with the pipe-payload reach they keep. Commands name the working directory as ``@w`` (the initial cwd), the existing directories ``@a`` and ``@b``, and ``@missing``, which is never created. Each @@ -22,11 +22,7 @@ _CommandSegment, _tokenize_command_segments_with_redirects, ) -from autoskillit.hooks._runtime._command_classification import ( - all_evaluated_segments, - live_command_text, -) -from autoskillit.hooks._runtime._git_target import ChannelFlags, command_channel_flags +from autoskillit.hooks._runtime._command_classification import all_evaluated_segments from autoskillit.hooks._runtime._shell_segment_walk import ( WalkedSegment, walk_evaluated_segments, @@ -301,59 +297,3 @@ def test_dropped_bare_keyword_keeps_the_pending_connector() -> None: (["a"], "", False), (["b"], "&&", True), ] - - -_LAYOUT_COMMANDS = ( - "export GIT_DIR=/r/.git; git x", - "GIT_WORK_TREE=/r git x", - "git --git-dir=/r x", - "git --bare x", - "git -c core.worktree=/r x", - "git -c include.path=/x y", - "git -c includeIf.gitdir:/a/.path=/x y", -) -_ALIAS_COMMANDS = ( - "git -c alias.x=y x", - "export GIT_CONFIG_COUNT=1; git x", - "HOME=/h git x", - "export XDG_CONFIG_HOME=/x; git x", - "git config alias.ur update-ref && git ur a b", -) - - -def _flags(command: str) -> ChannelFlags: - return command_channel_flags(live_command_text(command)) - - -@pytest.mark.parametrize("command", _LAYOUT_COMMANDS) -def test_layout_channels_in_command_text(command: str) -> None: - assert _flags(command) == ChannelFlags(layout_override=True, alias_channel=False) - - -@pytest.mark.parametrize( - "command", - [ - 'git commit -m "add include.txt" && git checkout feature', - "cat include.h && git checkout x", - ], -) -def test_include_in_a_filename_is_not_a_layout_channel(command: str) -> None: - assert _flags(command) == ChannelFlags(layout_override=False, alias_channel=False) - - -@pytest.mark.parametrize("command", _ALIAS_COMMANDS) -def test_alias_channels_in_command_text(command: str) -> None: - assert _flags(command) == ChannelFlags(layout_override=False, alias_channel=True) - - -@pytest.mark.parametrize("name", ["GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR"]) -def test_layout_variable_in_the_hook_environment_sets_layout_override( - monkeypatch: pytest.MonkeyPatch, name: str -) -> None: - monkeypatch.setenv(name, "/r/.git") - assert _flags("git x") == ChannelFlags(layout_override=True, alias_channel=False) - - -def test_ambient_home_alone_sets_neither_flag(monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.setenv("HOME", "/h") - assert _flags("git x") == ChannelFlags(layout_override=False, alias_channel=False) From 502d940df06c5945e2be6effc0ba5ddbc5c32a20 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:53:12 -0800 Subject: [PATCH 42/46] fix(review): give the Git global-option subcommand rows readable ids Finding: 2cadf7c6a8 Co-Authored-By: Claude Sonnet 5.5 --- tests/hooks/test_command_classification.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/hooks/test_command_classification.py b/tests/hooks/test_command_classification.py index e17db82c74..38dcc1b199 100644 --- a/tests/hooks/test_command_classification.py +++ b/tests/hooks/test_command_classification.py @@ -4793,11 +4793,14 @@ def test_every_git_global_spec_flag_is_recognized(flag: str) -> None: @pytest.mark.parametrize( ("argv", "subcommand"), [ - (["git", "--no-optional-locks", "status"], "status"), - (["git", "--literal-pathspecs", "log"], "log"), - (["git", "--no-literal-pathspecs", "log"], "log"), - (["git", "--attr-source=HEAD", "log"], "log"), - (["git", "--attr-source", "HEAD", "log"], "log"), + pytest.param(argv, subcommand, id=" ".join(argv)) + for argv, subcommand in [ + (["git", "--no-optional-locks", "status"], "status"), + (["git", "--literal-pathspecs", "log"], "log"), + (["git", "--no-literal-pathspecs", "log"], "log"), + (["git", "--attr-source=HEAD", "log"], "log"), + (["git", "--attr-source", "HEAD", "log"], "log"), + ] ], ) def test_git_pathspec_lock_and_attribute_globals_leave_the_real_subcommand( @@ -4810,7 +4813,9 @@ def test_git_pathspec_lock_and_attribute_globals_leave_the_real_subcommand( assert result.flags == [] -@pytest.mark.parametrize("argv", [["git", "--list-cmds=main"], ["git", "--exec-path"]]) +@pytest.mark.parametrize( + "argv", [["git", "--list-cmds=main"], ["git", "--exec-path"]], ids=" ".join +) def test_git_query_only_invocation_has_no_subcommand(argv: list[str]) -> None: assert extract_git_subcommand_and_flags(argv) is None From 8cb019f28b1b40b0d14e91129a7468e6111815ff Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:54:04 -0800 Subject: [PATCH 43/46] fix(review): read the git_ops_guard deny reason from its public authority constant Finding: 545f6b6036 Co-Authored-By: Claude Sonnet 5.5 --- tests/infra/test_git_ops_guard.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/infra/test_git_ops_guard.py b/tests/infra/test_git_ops_guard.py index c5dc7b2d0f..9b9eab22b0 100644 --- a/tests/infra/test_git_ops_guard.py +++ b/tests/infra/test_git_ops_guard.py @@ -26,6 +26,7 @@ from autoskillit.hooks._classification._option_grammar import OptionArity, OptionDef from autoskillit.hooks._runtime._git_command_classification import _classify_fetch from autoskillit.hooks._runtime._git_option_tables import _GIT_FETCH_FLAG_SPEC +from autoskillit.hooks._runtime._hook_constants import DENY_REASON_BY_GUARD from autoskillit.hooks.guards import git_ops_guard from tests._git_topology import AMBIENT_GIT_VARIABLES, commit_file, init_checkout from tests.infra._git_ops_guard_harness import ( @@ -1285,8 +1286,7 @@ def test_git_after_failed_cd_is_denied_only_when_it_rewrites_a_ref_or_is_destruc if verdict == "checked-out-ref": assert _checked_out_ref_result(out)["attempted_value"] == "" elif verdict == "other-deny": - reason = json.loads(out)["hookSpecificOutput"]["permissionDecisionReason"] - assert reason == git_ops_guard._DENY_REASON_TEMPLATE.format(op="git checkout .") + assert _deny_reason(out) == _destructive_reason(_CHECKOUT_TREE) class TestCheckedOutRefAllows: @@ -1602,7 +1602,7 @@ def _deny_reason(output: str) -> str: def _destructive_reason(op: tuple[str, ...]) -> str: - return git_ops_guard._DENY_REASON_TEMPLATE.format(op=" ".join(("git", *op))) + return DENY_REASON_BY_GUARD["git_ops_guard"].format(op=" ".join(("git", *op))) _AMEND = ("commit", "--amend") From 999d5b66e468eea38fc304013c074c93f08eae25 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:54:22 -0800 Subject: [PATCH 44/46] fix(review): keep only the uncovered variable in the destructive-outcome isolation fixture Finding: 6067ce793c Co-Authored-By: Claude Sonnet 5.5 --- tests/infra/test_git_ops_guard.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/infra/test_git_ops_guard.py b/tests/infra/test_git_ops_guard.py index 9b9eab22b0..c63ac01f8c 100644 --- a/tests/infra/test_git_ops_guard.py +++ b/tests/infra/test_git_ops_guard.py @@ -28,7 +28,7 @@ from autoskillit.hooks._runtime._git_option_tables import _GIT_FETCH_FLAG_SPEC from autoskillit.hooks._runtime._hook_constants import DENY_REASON_BY_GUARD from autoskillit.hooks.guards import git_ops_guard -from tests._git_topology import AMBIENT_GIT_VARIABLES, commit_file, init_checkout +from tests._git_topology import commit_file, init_checkout from tests.infra._git_ops_guard_harness import ( _TOOL_NAME, _checked_out_ref_result, @@ -1701,15 +1701,9 @@ class TestDestructiveOutcomeImmunity: """ @pytest.fixture(autouse=True) - def _isolated_git_environment(self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: - home = tmp_path / "home" - home.mkdir() - for name in AMBIENT_GIT_VARIABLES: - monkeypatch.delenv(name, raising=False) - monkeypatch.setenv("GIT_CONFIG_GLOBAL", os.devnull) - monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1") - monkeypatch.setenv("HOME", str(home)) - monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config")) + def _allow_abbreviated_options(self, monkeypatch: pytest.MonkeyPatch) -> None: + # Git's own test-suite switch rejects the abbreviated options this class pins. + monkeypatch.delenv("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", raising=False) @pytest.fixture def aliased_repo(self, tmp_path: Path) -> Path: From ae3e1a7cb1c75c02f76fda1829e122a4a2b80198 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:54:43 -0800 Subject: [PATCH 45/46] fix(review): explain the dashed git-commit row in the destructive-operation table Finding: afe3096825 Co-Authored-By: Claude Sonnet 5.5 --- tests/infra/test_git_ops_guard.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/infra/test_git_ops_guard.py b/tests/infra/test_git_ops_guard.py index c63ac01f8c..73e816e14d 100644 --- a/tests/infra/test_git_ops_guard.py +++ b/tests/infra/test_git_ops_guard.py @@ -1646,6 +1646,7 @@ def _git_rows(op: tuple[str, ...], *tails: str) -> list[tuple[str, tuple[str, .. ("printf 'c\\n' | git clean -fdi", _FORCE_CLEAN), ("echo 1 | git clean -id", _FORCE_CLEAN), *_git_rows(_AMEND, "commit --amen", "commit --am --no-edit"), + # Git's dashed spelling of the same builtin; the reason names the normalized operation. ("git-commit --amend", _AMEND), *_git_rows( _CHECKOUT_TREE, "checkout -f", "checkout ./", "checkout :/", "checkout . origin main" From a18c300b246d2cf16577d46a6cd70989a6494fd6 Mon Sep 17 00:00:00 2001 From: Trecek Date: Fri, 6 Nov 2026 11:55:48 -0800 Subject: [PATCH 46/46] fix(review): share the guard invocation and command expansion in the git_ops_guard harness Findings: 163bdb8c1d, 17329eb339, a42fc88dee, 5f3c20b9c6 Co-Authored-By: Claude Sonnet 5.5 --- tests/infra/_git_ops_guard_harness.py | 66 +++++++++++-------- tests/infra/test_git_ops_guard_attribution.py | 19 +++--- .../infra/test_git_ops_guard_primary_head.py | 26 ++------ 3 files changed, 56 insertions(+), 55 deletions(-) diff --git a/tests/infra/_git_ops_guard_harness.py b/tests/infra/_git_ops_guard_harness.py index 12cc0c4720..89a3eba89a 100644 --- a/tests/infra/_git_ops_guard_harness.py +++ b/tests/infra/_git_ops_guard_harness.py @@ -1,8 +1,8 @@ """Shared harness for git_ops_guard tests: payloads, a guard runner and repositories. -Members: _make_clean_env, _build_payload, _run_guard, _guard_exit_code, -_is_denied, _checked_out_ref_result, _guard_verdict, _git, _git_version, _context, -build_linked_repo. +Members: _make_clean_env, _build_payload, _open_kitchen, _invoke_guard, _run_guard, +_guard_exit_code, _is_denied, _checked_out_ref_result, _guard_verdict, _git, +_git_version, _context, build_linked_repo, _expand. ``UNSET`` distinguishes "use the default" from an explicit ``""`` or ``None`` for the payload's cwd fields, so a test can send a relative or empty cwd. A verdict @@ -102,10 +102,29 @@ def _build_payload( return payload +def _open_kitchen(root: Path, hook_config: dict[str, object] | None = None) -> None: + marker = root / _HOOK_CONFIG_RELPATH + marker.parent.mkdir(parents=True, exist_ok=True) + marker.write_text(json.dumps(hook_config or {"kitchen": "open"})) + + +def _invoke_guard(stdin_content: str, env: dict[str, str], process_cwd: Path) -> None: + """Run git_ops_guard.main() under *env*, *stdin_content* and a patched Path.cwd(). + + SystemExit propagates to the caller. + """ + with ( + unittest.mock.patch.dict(os.environ, env, clear=True), + unittest.mock.patch("sys.stdin", io.StringIO(stdin_content)), + unittest.mock.patch("pathlib.Path.cwd", return_value=process_cwd), + ): + git_ops_guard.main() + + def _run_guard( cmd: str, kitchen_open: bool, - tmpdir, + tmpdir: Path, raw_stdin: str | None = None, skill_name: str | None = None, session_type: str | None = None, @@ -143,44 +162,32 @@ def _run_guard( ) if kitchen_open: - hook_cfg = (marker_root or tmpdir) / _HOOK_CONFIG_RELPATH - hook_cfg.parent.mkdir(parents=True, exist_ok=True) - hook_cfg.write_text(json.dumps(hook_config or {"kitchen": "open"})) + _open_kitchen(marker_root or tmpdir, hook_config) clean_env = _make_clean_env(skill_name, session_type, headless=headless) clean_env.update(extra_env or {}) buf = io.StringIO() with contextlib.redirect_stdout(buf): - with unittest.mock.patch.dict(os.environ, clean_env, clear=True): - with unittest.mock.patch("sys.stdin", io.StringIO(stdin_content)): - with unittest.mock.patch("pathlib.Path.cwd", return_value=process_cwd or tmpdir): - try: - git_ops_guard.main() - except SystemExit as exc: - assert exc.code == 0, _context(cmd, f"guard exited {exc.code!r}") + try: + _invoke_guard(stdin_content, clean_env, process_cwd or tmpdir) + except SystemExit as exc: + assert exc.code == 0, _context(cmd, f"guard exited {exc.code!r}") return buf.getvalue() -def _guard_exit_code(cmd: str, tmpdir: Path, **payload_kwargs: object) -> int | str | None: +def _guard_exit_code(cmd: str, tmpdir: Path) -> int | str | None: """Run git_ops_guard.main() in a headless skill session with the kitchen open and return the code it exits with. _run_guard requires a clean exit; this expects main() to terminate through SystemExit and leaves stdout and stderr to the caller's capsys. """ - hook_cfg = tmpdir / _HOOK_CONFIG_RELPATH - hook_cfg.parent.mkdir(parents=True, exist_ok=True) - hook_cfg.write_text(json.dumps({"kitchen": "open"})) - payload = _build_payload(cmd, tmpdir, **payload_kwargs) # type: ignore[arg-type] - with ( - unittest.mock.patch.dict(os.environ, _make_clean_env(None), clear=True), - unittest.mock.patch("sys.stdin", io.StringIO(json.dumps(payload))), - unittest.mock.patch("pathlib.Path.cwd", return_value=tmpdir), - pytest.raises(SystemExit) as exit_info, - ): - git_ops_guard.main() + _open_kitchen(tmpdir) + payload = _build_payload(cmd, tmpdir) + with pytest.raises(SystemExit) as exit_info: + _invoke_guard(json.dumps(payload), _make_clean_env(None), tmpdir) return exit_info.value.code @@ -251,3 +258,10 @@ def build_linked_repo(tmp_path: Path, *, tracked_state_dir: bool = False) -> dic "worktree_git_dir": _git(linked, "rev-parse", "--absolute-git-dir"), "common_git_dir": str((linked / _git(linked, "rev-parse", "--git-common-dir")).resolve()), } + + +def _expand(command: str, repo: dict[str, Path | str], tmp_path: Path) -> str: + """Expand , , , and in *command*.""" + for name in ("primary", "linked", "unrelated"): + command = command.replace(f"<{name}>", str(repo[name])) + return command.replace("", str(tmp_path)).replace("", str(repo["new_sha"])) diff --git a/tests/infra/test_git_ops_guard_attribution.py b/tests/infra/test_git_ops_guard_attribution.py index f0275df26e..82e82e939e 100644 --- a/tests/infra/test_git_ops_guard_attribution.py +++ b/tests/infra/test_git_ops_guard_attribution.py @@ -19,6 +19,7 @@ from tests.infra._git_ops_guard_harness import ( _context, + _expand, _git, _guard_verdict, _run_guard, @@ -172,12 +173,6 @@ ] -def _expand(command: str, repo: dict[str, Path | str]) -> str: - for name in ("primary", "linked", "unrelated"): - command = command.replace(f"<{name}>", str(repo[name])) - return command.replace("", str(repo["new_sha"])) - - def _payload_dir(repo: dict[str, Path | str], name: str) -> Path: if name == "primary-git": return Path(str(repo["primary"])) / ".git" @@ -201,7 +196,7 @@ def _bash_verdict( directory resolves its state root to. """ output = _run_guard( - _expand(command, repo), + _expand(command, repo, tmp_path), kitchen_open=True, tmpdir=tmp_path, use_bash_key=True, @@ -257,7 +252,10 @@ def test_denies_run_cmd_with_unknown_tool_cwd( self, linked_repo: dict[str, Path | str], tmp_path: Path, command: str, tool_cwd: str ) -> None: output = _run_guard( - _expand(command, linked_repo), kitchen_open=True, tmpdir=tmp_path, tool_cwd=tool_cwd + _expand(command, linked_repo, tmp_path), + kitchen_open=True, + tmpdir=tmp_path, + tool_cwd=tool_cwd, ) assert _guard_verdict(output) == "checked-out-ref", _context(command, (tool_cwd, output)) @@ -285,7 +283,10 @@ def test_allows_run_cmd_with_empty_tool_cwd_when_no_ref_is_threatened( self, linked_repo: dict[str, Path | str], tmp_path: Path, command: str ) -> None: output = _run_guard( - _expand(command, linked_repo), kitchen_open=True, tmpdir=tmp_path, tool_cwd="" + _expand(command, linked_repo, tmp_path), + kitchen_open=True, + tmpdir=tmp_path, + tool_cwd="", ) assert _guard_verdict(output) == "allow", _context(command, output) diff --git a/tests/infra/test_git_ops_guard_primary_head.py b/tests/infra/test_git_ops_guard_primary_head.py index 9488e46589..ccebb732ec 100644 --- a/tests/infra/test_git_ops_guard_primary_head.py +++ b/tests/infra/test_git_ops_guard_primary_head.py @@ -15,12 +15,10 @@ from __future__ import annotations -import io import json import os import shutil import subprocess -import unittest.mock from pathlib import Path # The sys.path bootstrap for bare-name hook-sibling imports is centralized in @@ -31,13 +29,15 @@ from autoskillit.hooks.guards import git_ops_guard from tests._git_topology import commit_file, isolate_git_environment from tests.infra._git_ops_guard_harness import ( - _HOOK_CONFIG_RELPATH, UNSET, _build_payload, _context, + _expand, _git, _guard_verdict, + _invoke_guard, _make_clean_env, + _open_kitchen, _run_guard, build_linked_repo, ) @@ -154,12 +154,6 @@ def repo(tmp_path: Path) -> dict[str, Path | str]: return built -def _expand(command: str, repo: dict[str, Path | str], tmp_path: Path) -> str: - for name in ("primary", "linked", "unrelated"): - command = command.replace(f"<{name}>", str(repo[name])) - return command.replace("", str(tmp_path)).replace("", str(repo["new_sha"])) - - def _session_env(session: str, repo: dict[str, Path | str], *, state_root: bool) -> dict[str, str]: env = dict(_SESSIONS[session][1]) if state_root and session not in _UNLAUNCHED: @@ -401,21 +395,13 @@ def _cook_exit_code( ``_run_guard`` requires a clean exit, which a blocked classification is not. """ - primary = Path(str(repo["primary"])) - marker = primary / _HOOK_CONFIG_RELPATH - marker.parent.mkdir(parents=True, exist_ok=True) - marker.write_text(json.dumps({"kitchen": "open"})) + _open_kitchen(Path(str(repo["primary"]))) payload = _build_payload( _expand(command, repo, tmp_path), tmp_path, use_bash_key=True, payload_cwd=payload_cwd ) env = {**_make_clean_env(None, headless=False), **_session_env("cook", repo, state_root=True)} - with ( - unittest.mock.patch.dict(os.environ, env, clear=True), - unittest.mock.patch("sys.stdin", io.StringIO(json.dumps(payload))), - unittest.mock.patch("pathlib.Path.cwd", return_value=tmp_path), - pytest.raises(SystemExit) as exit_info, - ): - git_ops_guard.main() + with pytest.raises(SystemExit) as exit_info: + _invoke_guard(json.dumps(payload), env, tmp_path) return exit_info.value.code