#!/usr/bin/env python3
"""Decision core for agent-guard.sh (see that file for the hook contract).

Reads the PreToolUse payload JSON on stdin and prints exactly one decision:
  BLOCK_ATTRIB | BLOCK_FORCE | BLOCK_NOVERIFY<TAB>reason | BLOCK_READONLY<TAB>reason |
  BLOCK_UNATTENDED<TAB>reason | BLOCK_PARSE | ASK<TAB>hook-json |
  DENY<TAB>reason | NOTE<TAB>hook-json | OK

Never executes the command. shlex.split only tokenizes shell-like text; a
`$(...)` in the command is a literal token, never a subshell. The processes it
does start are read-only `git rev-parse` / `git diff --name-only` calls on a
path, with argv and no shell.

Attended (MULTI_AGENT_UNATTENDED unset): the attribution (argv and -F
message files), force-push and hook-skipping rules, plus the confirmations in
_guard_rules.ask_reason, and any error prints OK (fail-open) so a guard bug cannot break a
legitimate tool call. Unattended: unattended_policy.py is consulted as well,
and an error prints BLOCK_PARSE (fail-closed). The user's own rules
(prefs.global.guardRules, _user_guard_rules) are applied last and can only
make the verdict stricter; a warn rule's note is NOTE.
"""
import hashlib
import json
import os
import re
import shlex
import signal
import sys
import unicodedata

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import _guard_rules  # noqa: E402
import _user_guard_rules  # noqa: E402

# An internal deadline well under the hook timeout: if the guard cannot decide
# in this long, it prints the blocking verdict when unattended (fail-closed) and
# the allow verdict when attended (a guard bug must not break legitimate work).
# MA_GUARD_DEADLINE_SECONDS may lower it, never raise it: a longer deadline
# would let the hook timeout decide instead of the guard.
DEADLINE_SECONDS = 5


def deadline_seconds(env=None) -> float:
    env = os.environ if env is None else env
    try:
        v = float(env.get("MA_GUARD_DEADLINE_SECONDS", ""))
    except ValueError:
        return DEADLINE_SECONDS
    return v if 0 < v < DEADLINE_SECONDS else DEADLINE_SECONDS

# --- Rule data (extend here; this is the hookify-style rule surface) ----------
PROTECTED = {"main", "master", "develop"}
REMOTES = {"origin", "upstream"}
ATTRIBUTION = re.compile(
    r"co-authored-by:\s*(claude|anthropic|opus|sonnet|haiku|fable)"
    r"|co-authored-by:[^\n]*\bcopilot\b"
    r"|noreply@anthropic\.com"
    r"|generated with\s+.*claude"
    r"|\U0001F916\s*generated"
    r"|claude code <",
    re.IGNORECASE,
)


def _strip_set(ranges):
    out = set()
    for spec in ranges:
        first, _, last = str(spec).partition("-")
        try:
            lo, hi = int(first, 16), int(last or first, 16)
        except ValueError:
            continue
        out.update(chr(cp) for cp in range(lo, hi + 1))
    return out


def _load_confusables():
    here = os.path.dirname(os.path.abspath(__file__))
    try:
        with open(os.path.join(here, "..", "lib", "confusables.json"), encoding="utf-8") as fh:
            table = json.load(fh)
        return _strip_set(table.get("stripRanges", [])), dict(table.get("map", {}))
    except (OSError, ValueError):
        return set(), {}


_STRIP, _MAP = _load_confusables()


def normalize_for_match(text: str) -> str:
    """NFKC, Default_Ignorable_Code_Point removed, look-alikes mapped to ASCII.

    The same fold lib/normalize-text.mjs applies, from the same table. Rules
    that match text content run on the raw command and on this copy, so a
    trailer written with a zero-width space or a Cyrillic letter still matches.
    The table's map also carries the compatibility letters newer than this
    Python's unicodedata, which NFKC here would leave alone. Without the table
    only NFKC applies.
    """
    return "".join(
        _MAP.get(ch, ch) for ch in unicodedata.normalize("NFKC", text) if ch not in _STRIP
    )


# Force flags, and only force flags: `--force`, `--force-with-lease[=ref]`,
# `--force-if-includes`, and a single-dash cluster that contains `f` (`-f`,
# `-fq`, `-qf`). A long flag that merely contains an f (`--ff-only`,
# `--follow-tags`, `--file=`) rewrites nothing and passes: blocking safe
# commands trains people to work around the hook.
FORCE = re.compile(
    r"(^|\s)("
    r"--force(-with-lease|-if-includes)?(=\S+)?"  # --force, --force-with-lease[=ref], --force-if-includes
    r"|-[A-Za-z]*f[A-Za-z]*"  # -f and short clusters containing f
    r")(\s|$)"
)


# A refspec whose destination starts with `+` is a force-push without any flag:
# `git push origin +main`, `git push origin HEAD:+main`.
FORCED_REFSPEC = re.compile(r"(^|\s|:)\+\S")

# git's own options, which sit between `git` and the subcommand:
# `git -C /tmp/wt commit`, `git -c user.name=x commit`, `git --no-pager push`.
GIT_VALUE_OPTIONS = {"-C", "-c", "--git-dir", "--work-tree", "--exec-path", "--namespace"}
GIT_FLAG_OPTIONS = {
    "--no-pager",
    "--paginate",
    "-p",
    "-P",
    "--bare",
    "--literal-pathspecs",
    "--glob-pathspecs",
    "--noglob-pathspecs",
    "--icase-pathspecs",
    "--no-replace-objects",
    "--no-optional-locks",
}


# Shell separators that end one command and start the next. Used to attribute a
# flag to the command it actually belongs to.
SEPARATORS = re.compile(r"&&|\|\||;|\||\n")


# shlex grows each token one character at a time, which is quadratic in the
# token's length: a megabyte argument takes longer than the deadline. A segment
# longer than this is split on whitespace with its quotes and backslashes
# removed instead, which keeps every word the segment names in order.
SHLEX_MAX_CHARS = 16384


def coarse_tokens(cmd: str) -> list:
    return [re.sub(r"[\"'\\]", "", t) for t in cmd.split()]


def tokenize(cmd: str) -> list:
    """shlex tokens, or whitespace tokens when shlex cannot parse (an unbalanced
    quote) or the segment is too long for it. Never returns None: a guard has to
    reason about a segment it cannot parse cleanly, not skip it."""
    if len(cmd) > SHLEX_MAX_CHARS:
        return coarse_tokens(cmd)
    try:
        return shlex.split(cmd)
    except Exception:
        return cmd.split()


def git_subcommand(toks: list) -> str:
    """The git subcommand in a token list, skipping git's global options.

    `git -C <dir> commit` and `git -c k=v push` are the same commands as
    `git commit` and `git push`; matching `git\s+commit` saw neither.
    Returns "" when the tokens do not invoke git.
    """
    for i, t in enumerate(toks):
        if os.path.basename(t).lower() != "git":
            continue
        j = i + 1
        while j < len(toks):
            opt = toks[j]
            if opt in GIT_VALUE_OPTIONS:
                j += 2
            elif opt.startswith("-"):
                j += 1
            else:
                return opt.lower()
        return ""
    return ""


def segments(cmd: str) -> list:
    """The sub-commands of a shell line, paired with their tokens.

    A flag must be attributed to its own command. Scanning the whole string made
    `git commit -f ... && git push origin main` read as a force-push, because the
    `-f` belonged to the commit. Splitting on shell separators first keeps each
    command's flags with that command.

    Deliberately coarse: a separator inside a quoted string splits too, which can
    only ever produce MORE segments to inspect, never fewer - the safe direction
    for a guard.
    """
    return [(seg, tokenize(seg)) for seg in SEPARATORS.split(cmd)]


# `git push` options that take the NEXT token as their value. Without this list
# `git push -f -o ci.skip origin` reads `ci.skip` as the repository and
# `origin` as a branch name, and the bare-push check never runs.
PUSH_VALUE_OPTIONS = {"-o", "--push-option", "--repo", "--receive-pack", "--exec"}
# Pushes that rewrite or remove every ref, protected ones included.
PUSH_ALL_REFS = {"--all", "--mirror", "--branches"}
PUSH_DELETE = {"-d", "--delete", "--prune"}
# Pushes that can remove or rewrite a protected ref with no force flag: a
# mirror (force + prune over every ref), a prune, a delete, and a refspec with
# an empty source (`:main`), which deletes its destination.
PUSH_GATE = re.compile(r"(^|\s)(-d|--delete|--mirror|--prune)(\s|=|$)|(^|\s)\+?:\S")
# A destination that names the branch the checkout is on.
CURRENT_BRANCH_REFS = {"HEAD", "@"}
SHORT_FORCE = re.compile(r"^-[A-Za-z]*f[A-Za-z]*$")


SHELLS = {"sh", "bash", "zsh", "dash", "ksh", "fish"}
MAX_DEPTH = 3


def flatten(cmd: str) -> str:
    """cmd with every unquoted `(`, `)` and backtick turned into a separator,
    so a command inside a subshell or a substitution is a segment of its own.
    Like the separator split, this can only add segments."""
    out, q, i = [], None, 0
    while i < len(cmd):
        c = cmd[i]
        if q == "'":
            q = None if c == "'" else q
        elif c == "\\" and i + 1 < len(cmd):
            out.append(cmd[i : i + 2])
            i += 2
            continue
        elif q == '"':
            q = None if c == '"' else q
        elif c in ("'", '"'):
            q = c
        elif c in "()`":
            out.append(" ; ")
            i += 1
            continue
        out.append(c)
        i += 1
    return "".join(out)


def _nested_payload(toks: list):
    """The command line a segment hands to another shell: the `-c` argument
    of sh/bash/zsh (after any env assignments or wrappers), or the words of
    `eval`. None when the segment runs no nested line."""
    i = 0
    while i < len(toks) and (re.match(r"^[A-Za-z_][A-Za-z0-9_]*=", toks[i]) or os.path.basename(toks[i]) in ("env", "nohup", "command", "builtin", "exec", "nice", "time")):
        i += 1
    if i >= len(toks):
        return None
    base = os.path.basename(toks[i]).lower()
    if base == "eval":
        return " ".join(toks[i + 1 :])
    if base not in SHELLS:
        return None
    has_c = False
    for t in toks[i + 1 :]:
        if t.startswith("-") and not t.startswith("--"):
            has_c = has_c or "c" in t[1:]
            continue
        if t.startswith("--"):
            continue
        return t if has_c else None
    return None


def _attributed(text: str) -> bool:
    return bool(ATTRIBUTION.search(text) or ATTRIBUTION.search(normalize_for_match(text)))


# The directory the hook payload names as the session's cwd; message files a
# command names relative to it are read from there.
PAYLOAD_CWD = {"dir": None}
# The hook payload's session id: a commit inside the run this session drives
# is confirmed by the user.
PAYLOAD_SESSION = {"id": None}


def _command_rules(cmd: str) -> str:
    """Rules 1b, 3, 4 and 5, judged on the line's simple commands with heredoc
    bodies removed and only where the program is the command word
    (_guard_rules.command_segments / command_word). A `cd` moves later
    segments into another directory."""
    pending_ask = None
    cwd = None
    in_run = None
    for seg, toks in _guard_rules.command_segments(cmd):
        if toks and toks[0] in ("{", "}"):
            toks = toks[1:]
        if toks and toks[0] in ("cd", "pushd"):
            cwd = _cd_target(toks, cwd)
            continue
        here = cwd or PAYLOAD_CWD["dir"]
        # Rule 1b: the commit message given as a file (-F / --file).
        for path in _guard_rules.message_files(toks):
            if _attributed(_guard_rules.read_bounded(path, here)):
                return "BLOCK_ATTRIB"
        # Rule 5: history writes in a repository listed as read-only.
        reason = _guard_rules.read_only_git_reason(toks, here)
        if reason:
            return "BLOCK_READONLY\t" + reason
        # Rule 3: skipping the repository's git hooks.
        reason = _guard_rules.no_verify(toks)
        if reason:
            return "BLOCK_NOVERIFY\t" + reason
        # Rule 1c: attribution in a pull request or issue the gh CLI publishes.
        gh_body = _guard_rules.gh_text(toks, here)
        if gh_body and "$(" in gh_body:
            gh_body = "\n".join([gh_body] + _guard_rules.strip_heredoc_bodies(cmd)[1])
        if _attributed(gh_body):
            return "BLOCK_ATTRIB"
        # Rule 9: inside the run this session drives, a published body keeps
        # real line breaks and no HTML entities.
        _, argv = _guard_rules.command_word(toks)
        bodies = _guard_rules.payload_files(argv)
        piped = _guard_rules.stdin_payload(argv) and bool(_guard_rules.strip_heredoc_bodies(cmd)[1])
        if gh_body or bodies or piped:
            if in_run is None:
                in_run = bool(_guard_rules.driving_run(PAYLOAD_SESSION["id"]))
            if in_run:
                jira = bool(_guard_rules.JIRA_API.search(seg))
                reason = _guard_rules.body_text_reason(gh_body) if gh_body else None
                for path in bodies:
                    reason = reason or _guard_rules.payload_reason(path, here, entities=jira)
                if piped:
                    for body in _guard_rules.strip_heredoc_bodies(cmd)[1]:
                        reason = reason or _guard_rules.payload_reason(None, here, entities=jira, raw=body)
                if reason:
                    return "DENY\t" + reason
        # Rule 8: a pull request PUT that would wipe reviewers or refs.
        reason = _guard_rules.pr_update_reason(toks, seg, here)
        if reason:
            return "DENY\t" + reason
        # Rule 6: a token on the vercel command line.
        reason = _guard_rules.vercel_token_reason(toks)
        if reason:
            return "DENY\t" + reason
        # Rule 4: writes a person confirms (issue closure, Jira transition, clean-up).
        ask = _guard_rules.ask_reason(toks, seg, here)
        if ask and not pending_ask:
            pending_ask = "ASK\t" + ask
    return pending_ask or "OK"


def decide(cmd: str, depth: int = 0) -> str:
    low = cmd.lower()
    segs = segments(flatten(cmd))

    # Rule 1: AI attribution inside a git commit, read from what the commit
    # records (_guard_rules.commit_text), never from a neighbouring command.
    commits = re.search(r"git\s+commit", low) or any(
        git_subcommand(toks) == "commit" for _, toks in segs
    )
    if commits and _attributed(_guard_rules.commit_text(cmd, PAYLOAD_CWD["dir"])):
        return "BLOCK_ATTRIB"
    if commits and _guard_rules.ai_generated(cmd, PAYLOAD_CWD["dir"]):
        return "BLOCK_ATTRIB"
    # Rule 7: a stored secret printed with nothing capturing it.
    reason = _guard_rules.secret_echo_reason(cmd) or _guard_rules.encoded_secret_read_reason(cmd)
    if reason:
        return "DENY\t" + reason
    pending_ask = None
    if commits and _guard_rules.CLOSING.search(_guard_rules.message_text(cmd, PAYLOAD_CWD["dir"])):
        pending_ask = (
            "ASK\tthe commit message carries a closing keyword (Closes/Fixes/Resolves #N), which closes "
            "the issue when the commit lands on the default branch; use Ref: #N, or confirm the closure"
        )
    commit_dirs = _commit_dirs(cmd)
    driving = _guard_rules.driving_run(PAYLOAD_SESSION["id"]) if commit_dirs else None
    if driving and not any(_owned_by(driving, d) for d in commit_dirs):
        driving = None
    if driving:
        reason = _guard_rules.commit_subject_reason(
            _guard_rules.message_text(cmd, PAYLOAD_CWD["dir"]), PAYLOAD_CWD["dir"]
        )
        if reason:
            return "DENY\t" + reason
    if driving and not pending_ask:
        reason = _guard_rules.run_commit_reason(driving)
        if reason:
            pending_ask = "ASK\t" + reason

    # Rule 2: force-push to a protected branch. Evaluate each push segment on its
    # own so a neighbouring command's flags cannot implicate it. A `cd` earlier
    # on the line moves the push into another checkout, whose branch is the one
    # that counts.
    cwd = None
    for seg, toks in segs:
        if toks and toks[0] in ("{", "}"):
            toks = toks[1:]
        if toks and toks[0] in ("cd", "pushd"):
            cwd = _cd_target(toks, cwd)
            continue
        payload = _nested_payload(toks)
        if payload and depth < MAX_DEPTH:
            verdict = decide(payload, depth + 1)
            if verdict.startswith("ASK"):
                pending_ask = pending_ask or verdict
            elif verdict != "OK":
                return verdict
            continue
        if git_subcommand(toks) != "push" and not re.search(r"git\s+push", seg.lower()):
            continue
        verdict = _decide_push(seg, toks, cwd)
        if verdict != "OK":
            return verdict

    if not pending_ask:
        reason = _guard_rules.fork_bomb_reason(cmd) or _guard_rules.fork_bomb_reason(
            normalize_for_match(cmd)
        )
        if reason:
            pending_ask = "ASK\t" + reason

    verdict = _command_rules(cmd)
    if verdict.startswith("ASK"):
        return pending_ask or verdict
    if verdict != "OK":
        return verdict
    return pending_ask or "OK"


def _cd_target(toks: list, cwd):
    """The directory a `cd` moves to, or "" when it cannot be read literally
    (`cd`, `cd -`, `cd ~`, `cd "$X"`): a push after it is on an unknown branch.
    Once unknown, a relative cd stays unknown; only an absolute one resolves."""
    args = [t for t in toks[1:] if not t.startswith("-")]
    if not args or any(ch in args[0] for ch in "$`~*?"):
        return ""
    if os.path.isabs(args[0]):
        return os.path.normpath(args[0])
    if cwd == "":
        return ""
    base = cwd if cwd else os.getcwd()
    return os.path.normpath(os.path.join(base, args[0]))


def _commit_dirs(cmd: str) -> list:
    """The directory each `git commit` on the line runs in: a `-C <dir>`, else
    the directory a preceding `cd` moved to, else the payload cwd. "" stands
    for a directory that cannot be read literally."""
    dirs = []
    cwd = None
    for _, toks in _guard_rules.command_segments(cmd):
        if toks and toks[0] in ("{", "}"):
            toks = toks[1:]
        if toks and toks[0] in ("cd", "pushd"):
            cwd = _cd_target(toks, cwd)
            continue
        if git_subcommand(toks) != "commit":
            continue
        here = cwd if cwd is not None else (PAYLOAD_CWD["dir"] or "")
        if "-C" in toks and toks.index("-C") + 1 < len(toks):
            target = toks[toks.index("-C") + 1]
            here = target if os.path.isabs(target) else os.path.join(here or os.getcwd(), target)
        dirs.append(here)
    return dirs


def _owned_by(doc: dict, directory: str) -> bool:
    """Whether a commit in `directory` belongs to the run: the directory's
    repository is the run's worktree, its project root or one of its multi-repo
    entries. A directory that cannot be read counts as the run's, so the
    confirmation is never skipped on a guess."""
    if not directory:
        return True
    try:
        import subprocess

        out = subprocess.run(
            ["git", "-C", directory, "rev-parse", "--show-toplevel"],
            capture_output=True,
            text=True,
            timeout=3,
        )
        if out.returncode != 0:
            return True
        top = os.path.realpath(out.stdout.strip())
    except Exception:
        return True
    paths = [doc.get("worktreePath"), doc.get("projectRoot")]
    for p in doc.get("projects") if isinstance(doc.get("projects"), list) else []:
        if isinstance(p, dict):
            paths += [p.get("worktreePath"), p.get("projectRoot")]
    return any(isinstance(p, str) and p and os.path.realpath(p) == top for p in paths)


def _branch_of(directory: str) -> str:
    """The branch checked out in `directory`, read with the same read-only
    `git rev-parse` the wrapper runs for the cwd. "" when it cannot be read."""
    if not directory:
        return ""
    try:
        import subprocess

        out = subprocess.run(
            ["git", "-C", directory, "rev-parse", "--abbrev-ref", "HEAD"],
            capture_output=True,
            text=True,
            timeout=3,
        )
        return out.stdout.strip() if out.returncode == 0 else ""
    except Exception:
        return ""


def _parse_push(toks: list):
    """(git_dir, options, positionals) for a tokenized `git ... push ...`.

    git_dir is the directory named by `-C` / `--work-tree` / `--git-dir`, or
    None. Values of options that take one are consumed, never read as a
    repository or refspec.
    """
    git_dir, i = None, 0
    while i < len(toks) and os.path.basename(toks[i]).lower() != "git":
        i += 1
    i += 1
    while i < len(toks):
        t = toks[i]
        if t in ("-C", "--work-tree", "--git-dir"):
            if i + 1 < len(toks):
                val = toks[i + 1]
                if t == "--git-dir":
                    val = os.path.dirname(val.rstrip("/")) or "."
                git_dir = val if git_dir is None or os.path.isabs(val) else os.path.join(git_dir, val)
            i += 2
            continue
        if t.startswith("--work-tree=") or t.startswith("--git-dir="):
            key, _, val = t.partition("=")
            if key == "--git-dir":
                val = os.path.dirname(val.rstrip("/")) or "."
            git_dir = val
            i += 1
            continue
        if t in GIT_VALUE_OPTIONS:
            i += 2
            continue
        if t.startswith("-"):
            i += 1
            continue
        break
    # toks[i] is the subcommand
    opts, positionals = [], []
    j = i + 1
    while j < len(toks):
        t = toks[j]
        if t in PUSH_VALUE_OPTIONS:
            opts.append((t, toks[j + 1] if j + 1 < len(toks) else ""))
            j += 2
            continue
        if t.startswith("-"):
            key, _, val = t.partition("=")
            opts.append((key, val))
        else:
            positionals.append(t)
        j += 1
    return git_dir, opts, positionals


def _ref_target(arg: str) -> tuple:
    """(forced, branch) for one refspec.

    The destination is the part after the last `:`; a leading `+` on it is a
    force-push; `refs/heads/main` names the same branch as `main`. A refspec
    with no `:` pushes to the branch of the same name.
    """
    dst = arg.rsplit(":", 1)[-1]
    forced = dst.startswith("+") or arg.startswith("+")
    name = dst.lstrip("+")
    if name.startswith("refs/heads/"):
        name = name[len("refs/heads/"):]
    return forced, name


def _decide_push(cmd: str, toks=None, cwd=None) -> str:
    """Verdict for a single `git push ...` command.

    `git push [options] [<repository> [<refspec>...]]`: the first positional is
    always the repository, whatever it is called; everything after it is a
    refspec. A refspec of HEAD or @, or none at all, targets the branch that is
    checked out in the directory the push runs in.
    """
    if toks is None:
        toks = tokenize(cmd)
    force_flag = bool(FORCE.search(cmd))
    if not force_flag and not FORCED_REFSPEC.search(cmd) and not PUSH_GATE.search(cmd):
        return "OK"
    if len(cmd) <= SHLEX_MAX_CHARS:
        try:
            shlex.split(cmd)
        except Exception:
            # A force-push we can't tokenize -> can't prove it's safe -> block.
            return "BLOCK_FORCE"
    git_dir, opts, positionals = _parse_push(toks)
    keys = {k for k, _ in opts}
    force_flag = any(
        k in ("--force", "--force-with-lease", "--force-if-includes") or SHORT_FORCE.match(k) for k in keys
    )
    repo_opt = any(k == "--repo" for k in keys)
    refspecs = positionals if repo_opt else positionals[1:]
    if "--mirror" in keys:
        return "BLOCK_FORCE"
    deleting = bool(keys & PUSH_DELETE) or any(r.lstrip("+").startswith(":") for r in refspecs)

    if git_dir is not None:
        base = cwd if cwd else os.getcwd()
        current = _branch_of(git_dir if os.path.isabs(git_dir) else os.path.join(base, git_dir))
    elif cwd is not None:
        current = _branch_of(cwd)
    else:
        current = os.environ.get("CUR_BRANCH", "")

    targets = []
    for ref in refspecs:
        forced, name = _ref_target(ref)
        if name in CURRENT_BRANCH_REFS:
            name = current or "HEAD"
        targets.append((forced, name))

    if any(forced and name in PROTECTED for forced, name in targets):
        return "BLOCK_FORCE"
    if not (force_flag or deleting):
        return "OK"
    if keys & PUSH_ALL_REFS:
        return "BLOCK_FORCE"
    if any(name in PROTECTED for _, name in targets):
        return "BLOCK_FORCE"
    if "--prune" in keys and (not targets or any("*" in name for _, name in targets)):
        return "BLOCK_FORCE"
    unresolved = any(name == "HEAD" for _, name in targets)
    if force_flag and (not targets or unresolved):
        # The target is the current branch. Block if it's protected OR
        # undeterminable (fail-closed for a data-loss guard).
        if current in PROTECTED or current in ("", "HEAD"):
            return "BLOCK_FORCE"
    return "OK"


FALLBACK_SPLIT = re.compile(r"&&|\|\||[;|\n()`]|\\n")
FALLBACK_FORCE_WORDS = {"-d", "--force", "--force-with-lease", "--force-if-includes", "--mirror", "--delete", "--prune"}
FALLBACK_TARGET_WORDS = {"head", "@", "--all", "--mirror", "--branches"}


def _fallback_forced(tok: str) -> bool:
    key = tok.split("=", 1)[0]
    if key in FALLBACK_FORCE_WORDS or tok.startswith("+") or ":+" in tok or tok.startswith(":"):
        return True
    return tok.startswith("-") and not tok.startswith("--") and tok[1:].isalpha() and "f" in tok


def _fallback_protected(tok: str) -> bool:
    name = tok.rsplit(":", 1)[-1].lstrip("+")
    if name.startswith("refs/heads/"):
        name = name[len("refs/heads/"):]
    return name in PROTECTED or tok in FALLBACK_TARGET_WORDS


def fallback_verdict(text: str) -> str:
    """The always-on rules as a linear scan, for input the tokenizer could not
    finish in time. Each piece between shell separators is split on whitespace
    with its quotes and backslashes removed, and no regex that can backtrack
    runs over it. A push piece with a force indicator is blocked when it names a
    protected branch, HEAD / @ or every ref, or when the checkout's own branch
    is protected or unknown; a line with a git commit is blocked when the
    attribution pattern matches the raw or the folded text. Stricter than
    decide() by design: it only runs when decide() could not."""
    try:
        folded = normalize_for_match(text)
    except Exception:
        folded = text
    current = os.environ.get("CUR_BRANCH", "")
    commits = False
    for t in (text, folded):
        for piece in FALLBACK_SPLIT.split(t.lower()):
            toks = coarse_tokens(piece)
            if not any(os.path.basename(x) == "git" for x in toks):
                continue
            words = set(toks)
            if "commit" in words:
                commits = True
            if "push" not in words or not any(_fallback_forced(x) for x in toks):
                continue
            if any(_fallback_protected(x) for x in toks) or current in PROTECTED or current in ("", "HEAD"):
                return "BLOCK_FORCE"
    if commits and (ATTRIBUTION.search(text) or ATTRIBUTION.search(folded)):
        return "BLOCK_ATTRIB"
    return "OK"


FILE_TOOLS = {"Edit", "Write", "NotebookEdit", "MultiEdit"}
PRODUCT_TREE_OVERLAY = "product-tree-overlay.jsonl"


def product_tree_overlay_verdict(ti: dict, cwd: str = "") -> str:
    """DENY for a file tool aimed at the product-tree overlay, attended and
    unattended: its rows are the user's agree or reject decisions, written
    only through product-tree.mjs. The name is compared casefolded, both as
    given and after resolving symlinks against the payload's cwd, so a case
    variant on a case-insensitive volume or a link to the overlay is refused
    too."""
    path = ti.get("file_path") or ti.get("notebook_path") if isinstance(ti, dict) else None
    if not isinstance(path, str) or not path:
        return "OK"
    names = [os.path.basename(os.path.normpath(path))]
    try:
        names.append(os.path.basename(os.path.realpath(os.path.join(cwd or os.getcwd(), path))))
    except (OSError, ValueError):
        pass
    if any(n.casefold() == PRODUCT_TREE_OVERLAY for n in names):
        return "DENY\tonly product-tree.mjs propose writes the overlay; a decision is the user's"
    return "OK"


# Permission modes in which a hook's "ask" has no documented prompt to raise.
# The hooks reference documents "ask" as forcing a prompt (and states it for
# auto mode); it says nothing for bypassPermissions or dontAsk, where the
# permission prompt is switched off. There the guard denies with the reason
# and names the channel the user's answer travels through: the model asks,
# confirm-command.mjs records a yes for that exact command (sha256, ten
# minutes, one use), and the rerun finds it. Attended only: unattended, the
# record is never read.
NO_PROMPT_MODES = {"bypassPermissions", "dontAsk"}


def confirmation_key(data: dict) -> str:
    """What a recorded confirmation names: the exact command for Bash; for any
    other tool its name, the target path when there is one, and the sha256 of
    its input as sorted compact JSON, so a yes covers this exact call only."""
    tool = data.get("tool_name")
    ti = data.get("tool_input") or {}
    if tool == "Bash" or not isinstance(tool, str) or not tool:
        cmd = ti.get("command", "") if isinstance(ti, dict) else ""
        return cmd if isinstance(cmd, str) else ""
    try:
        canonical = json.dumps(ti, sort_keys=True, ensure_ascii=False, separators=(",", ":"))
    except (TypeError, ValueError):
        return ""
    digest = hashlib.sha256(canonical.encode("utf-8")).hexdigest()
    path = (ti.get("file_path") or ti.get("notebook_path")) if isinstance(ti, dict) else None
    parts = [tool]
    if isinstance(path, str) and path and not any(c in path for c in "\r\n"):
        parts.append(path)
    parts.append("sha256:" + digest)
    return " ".join(parts)


def ask_verdict(reason: str, data: dict, strict: bool) -> str:
    """ASK<TAB>json for a prompt, DENY<TAB>reason where no prompt can be
    raised, BLOCK_UNATTENDED<TAB>reason when nobody is there to answer."""
    if strict:
        return "BLOCK_UNATTENDED\t" + reason
    if data.get("permission_mode") in NO_PROMPT_MODES:
        # With prompts switched off the native ask cannot be raised, so the
        # recorded confirmation stands in for it. In every other mode the
        # prompt is the confirmation and a record must not skip it.
        key = confirmation_key(data)
        if _guard_rules.confirmed(key):
            return "OK"
        if data.get("tool_name") == "Bash" or not key:
            what, again = "'<the exact command>'", "run the command again"
        else:
            what, again = shlex.quote(key), f"make the same {data.get('tool_name')} call again"
        return (
            "DENY\t" + reason + " (permission prompts are off in this mode: ask the user with AskUserQuestion; on yes, "
            f"record it with node \"$HOME/.claude/scripts/confirm-command.mjs\" --command {what} "
            f"and {again}, unchanged, within 10 minutes)"
        )
    out = {
        "hookSpecificOutput": {
            "hookEventName": "PreToolUse",
            "permissionDecision": "ask",
            "permissionDecisionReason": "agent-guard: " + reason,
        }
    }
    return "ASK\t" + json.dumps(out)


def unattended() -> bool:
    """MULTI_AGENT_UNATTENDED=1 in the hook's own environment - the launcher's,
    never anything the run wrote (lib/unattended.sh ma_unattended)."""
    return os.environ.get("MULTI_AGENT_UNATTENDED", "") == "1"


def _arm_deadline(on_deadline) -> None:
    """Run on_deadline (which must end the process) once deadline_seconds()
    have passed. Where SIGALRM is unavailable the call simply runs to the end."""

    def _fire(_signum, _frame):
        try:
            signal.setitimer(signal.ITIMER_REAL, 0)
        except (ValueError, AttributeError, OSError):
            pass
        on_deadline()

    try:
        signal.signal(signal.SIGALRM, _fire)
        signal.setitimer(signal.ITIMER_REAL, deadline_seconds())
    except (ValueError, AttributeError, OSError):
        pass


def _exit_with(line: str) -> None:
    # os._exit skips the stdio flush, so the line is flushed first or the
    # caller reads nothing.
    if line:
        print(line, flush=True)
    os._exit(0)


def git_subcommand_mode() -> None:
    """`agent-guard.py --git-subcommand`: read a Bash payload (or a raw command
    on argv) and print each git subcommand it runs, one per line, using the same
    tokenizer the guard uses. pre-commit-check.sh reads this to detect a commit
    without a regex. Never executes anything. At the deadline it prints
    `commit`, so the caller scans rather than skipping a commit it could not
    rule out."""
    _arm_deadline(lambda: _exit_with("commit"))
    if len(sys.argv) > 2:
        cmd = sys.argv[2]
    else:
        try:
            data = json.load(sys.stdin)
            cmd = (data.get("tool_input") or {}).get("command", "")
        except Exception:
            cmd = ""
    if not isinstance(cmd, str) or not cmd:
        return
    try:
        sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
        import unattended_policy

        for sub in unattended_policy.git_subcommands(cmd):
            print(sub)
    except Exception:
        # Fall back to the git subcommand of the whole token list; a caller that
        # gets nothing scans anyway, so silence here only costs an extra scan.
        subs = git_subcommand(tokenize(cmd))
        if subs:
            print(subs)


def commit_scope_mode() -> None:
    """`agent-guard.py --commit-scope`: read a Bash payload and print what its
    commit takes beyond the index (unattended_policy.git_commit_scope), one
    item per line. Never executes anything. At the deadline or on an error it
    prints `ADD_ALL` and `COMMIT_ALL`, so the caller scans the widest set rather
    than missing a path the commit could include."""
    widest = "ADD_ALL\nCOMMIT_ALL"
    _arm_deadline(lambda: _exit_with(widest))
    try:
        data = json.load(sys.stdin)
        cmd = (data.get("tool_input") or {}).get("command", "")
    except Exception:
        cmd = ""
    if not isinstance(cmd, str) or not cmd:
        return
    try:
        sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
        import unattended_policy

        items = unattended_policy.git_commit_scope(cmd)
    except Exception:
        print(widest)
        return
    for item in items:
        print(item)


def commit_dir_mode() -> None:
    """`agent-guard.py --commit-dir`: read a Bash payload and print the
    directory the first commit-writing git command on the line runs in, after
    any `cd` / `git -C`, resolved from the payload's cwd. Prints nothing when
    there is no commit, the directory cannot be read literally, or the deadline
    passes, so the caller keeps its own cwd. Never executes anything."""
    _arm_deadline(lambda: _exit_with(""))
    try:
        data = json.load(sys.stdin)
        cmd = (data.get("tool_input") or {}).get("command", "")
        base = data.get("cwd") or os.getcwd()
    except Exception:
        return
    if not isinstance(cmd, str) or not cmd:
        return
    try:
        sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
        import unattended_policy

        dirs = unattended_policy.git_commit_dirs(cmd, base)
    except Exception:
        return
    if dirs and dirs[0] and os.path.isdir(dirs[0]):
        print(dirs[0])


def main() -> None:
    if len(sys.argv) > 1 and sys.argv[1] == "--git-subcommand":
        git_subcommand_mode()
        return
    if len(sys.argv) > 1 and sys.argv[1] == "--commit-scope":
        commit_scope_mode()
        return
    if len(sys.argv) > 1 and sys.argv[1] == "--commit-dir":
        commit_dir_mode()
        return
    strict = unattended()
    seen = {"text": "", "raw": True}

    def _unjudged(default: str) -> str:
        # The always-on rules still hold when the full decision could not be
        # reached: a linear scan of whatever text is at hand decides them.
        text = seen["text"]
        if seen.get("raw"):
            try:
                cmd = (json.loads(text).get("tool_input") or {}).get("command", "")
                text = cmd if isinstance(cmd, str) else text
            except Exception:
                pass
        try:
            verdict = fallback_verdict(text)
        except Exception:
            verdict = "OK"
        return verdict if verdict != "OK" else default

    try:
        seen["text"] = sys.stdin.read()
    except Exception:
        seen["text"] = ""
    # Armed once the payload is in hand, so the fallback always has the text.
    _arm_deadline(
        lambda: _exit_with(_unjudged("BLOCK_UNATTENDED\tthe guard hit its internal deadline" if strict else "OK"))
    )
    try:
        data = json.loads(seen["text"])
        cmd = (data.get("tool_input") or {}).get("command", "")
        if isinstance(data.get("cwd"), str) and data.get("cwd"):
            PAYLOAD_CWD["dir"] = data["cwd"]
        if isinstance(data.get("session_id"), str) and data.get("session_id"):
            PAYLOAD_SESSION["id"] = data["session_id"]
        if isinstance(cmd, str):
            seen["text"], seen["raw"] = cmd, False
        verdict = decide(cmd) if isinstance(cmd, str) and cmd else "OK"
        if verdict == "OK" and data.get("tool_name") in FILE_TOOLS:
            ti = data.get("tool_input") or {}
            reason = _guard_rules.read_only_file_reason(
                ti.get("file_path") or ti.get("notebook_path") or "", PAYLOAD_CWD["dir"]
            )
            if reason:
                verdict = "BLOCK_READONLY\t" + reason
        if verdict == "OK" and data.get("tool_name") in FILE_TOOLS:
            verdict = product_tree_overlay_verdict(data.get("tool_input") or {}, PAYLOAD_CWD["dir"])
        if (
            verdict == "OK"
            and isinstance(cmd, str)
            and cmd
            and _guard_rules.read_only_agent(data.get("agent_type"))
        ):
            effect = _guard_rules.subagent_side_effect(cmd)
            if effect:
                verdict = (
                    "DENY\tthe " + str(data.get("agent_type")) + " subagent is read-only and " + effect
                    + "; it returns findings, and the orchestrator acts on them"
                )
        verdict, user_rule = _user_guard_rules.before_ask(verdict, data, strict, normalize_for_match)
        if verdict.startswith("ASK"):
            verdict = ask_verdict(verdict.split("\t", 1)[1], data, strict)
        if verdict == "OK" and strict:
            sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
            import unattended_policy

            reason = unattended_policy.check(data)
            if reason:
                verdict = "BLOCK_UNATTENDED\t" + reason.replace("\n", " ")
        verdict = _user_guard_rules.after_policy(verdict, user_rule, strict)
        print(verdict)
    except Exception:
        # Attended: a guard bug must not break a legitimate tool call.
        # Unattended: nobody is there to notice a gap, so an input the guard
        # cannot judge is refused.
        print(_unjudged("BLOCK_PARSE" if strict else "OK"))


if __name__ == "__main__":
    main()
