#!/usr/bin/env bash
# agent-guard.sh  -  PreToolUse guard for the multi-agent pipeline.
#
# Turns prompt-level rules into deterministic, OS-enforced gates:
#   1. No AI/assistant attribution in git commit messages (Co-Authored-By: Claude
#      or Copilot, "Generated with Claude Code", "Generated by AI", robot emoji,
#      Anthropic no-reply address), on the command line or in a `-F` file.
#   2. No force-push to a protected branch (main / master / develop), whichever
#      remote, push options or `git -C` directory the push names.
#   2b. No skipping the repository's git hooks (commit / push / merge with
#      --no-verify, commit -n, -c core.hooksPath=...).
#   2c. No literal credential on a Bash command line (argv-secret-scan.mjs).
#   2e. No Edit / Write, git commit or git push inside a repository listed in
#      prefs.global.readOnlyRepos (a reference checkout).
#   2d. A person confirms (permissionDecision "ask") closing a GitHub issue, a
#      PR body with Closes/Fixes/Resolves #N and a Jira transition POST. Where
#      the permission mode raises no prompt (bypassPermissions, dontAsk) the
#      call is denied with the reason instead; unattended it is blocked.
#   2f. The user's own rules in prefs.global.guardRules (warn, ask, block),
#      applied after every built-in rule and only ever stricter
#      (_user_guard_rules.py); unattended, ask is block.
#   3. With MULTI_AGENT_UNATTENDED=1 in the hook's environment only: no outward
#      writes (push, PR, issue, tracker publishers, mutating API calls), no
#      Keychain reads, no fetch outside the network allowlist, no package
#      installs or manifest edits, no writes into protected paths
#      (scripts/unattended_policy.py, schemas/unattended-policy.json).
#
# Contract (Claude Code PreToolUse hook, matcher Bash and Edit|Write|NotebookEdit):
#   - Reads the tool-call JSON on stdin: {"tool_name":..., "tool_input":{...}, "cwd":...}.
#   - Exit 2  -> BLOCK the tool call (reason on stderr, shown to the model).
#   - Exit 0  -> allow, or, with hook JSON on stdout, ask the user to confirm.
#
# Safety design:
#   - The command string is ONLY parsed/pattern-matched, NEVER executed or eval'd
#     (the decision core is agent-guard.py; shlex tokenizes without running).
#   - Attended: any internal error (bad JSON, missing python3/helper, empty
#     input) -> exit 0. A guard bug must never break a legitimate tool call.
#   - Unattended: the same errors -> exit 2. Nobody is watching to notice a gap.
#   - No network, no file writes, no secret values printed.
#   - The execs are read-only `git rev-parse` / `git diff --name-only` calls.

set -u

HERE="$(cd "$(dirname "$0")" 2>/dev/null && pwd || true)"
HELPER="$HERE/agent-guard.py"

STRICT=0
[ "${MULTI_AGENT_UNATTENDED:-}" = "1" ] && STRICT=1

refuse_unjudged() {
  if [ "$STRICT" = "1" ]; then
    echo "BLOCKED by agent-guard (unattended): $1, so this tool call cannot be judged and is refused." >&2
    exit 2
  fi
  exit 0
}

[ -f "$HELPER" ] || refuse_unjudged "agent-guard.py is missing"
command -v python3 >/dev/null 2>&1 || refuse_unjudged "python3 is not on PATH"

PAYLOAD="$(cat 2>/dev/null || true)"
[ -z "$PAYLOAD" ] && refuse_unjudged "the hook received no payload"

CUR_BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)"

DECISION="$(printf '%s' "$PAYLOAD" | CUR_BRANCH="$CUR_BRANCH" python3 "$HELPER" 2>/dev/null || true)"

# A literal credential on a Bash command line is refused before anything else
# is said about the call (argv-secret-scan.mjs, the outbound gate's credential
# shapes). Only rule names are printed, never the value.
case "$DECISION" in
  OK|ASK*|DENY*|NOTE*)
    case "$PAYLOAD" in
      *'"tool_name":"Bash"'*|*'"tool_name": "Bash"'*)
        if command -v node >/dev/null 2>&1 && [ -f "$HERE/argv-secret-scan.mjs" ]; then
          RULES="$(printf '%s' "$PAYLOAD" | node "$HERE/argv-secret-scan.mjs" 2>/dev/null)"
          if [ "$?" = "7" ]; then
            echo "BLOCKED by agent-guard: the command line carries a literal credential ($RULES)." >&2
            echo "Read it from the credential store or an environment variable instead; a value typed" >&2
            echo "into argv lands in the transcript, the process table and every log that echoes it." >&2
            exit 2
          fi
        elif [ "$STRICT" = "1" ]; then
          refuse_unjudged "node or argv-secret-scan.mjs is unavailable for the credential scan"
        fi ;;
    esac ;;
esac

case "$DECISION" in
  BLOCK_ATTRIB)
    echo "BLOCKED by agent-guard: the commit message carries AI/assistant attribution." >&2
    echo "Remove any 'Co-Authored-By: Claude', 'Generated with Claude Code', robot-emoji, or" >&2
    echo "anthropic no-reply trailer. Commits are authored solely as the user's git identity." >&2
    exit 2 ;;
  BLOCK_FORCE)
    echo "BLOCKED by agent-guard: force-push to a protected branch (main/master/develop) is not allowed." >&2
    echo "Rewriting shared history is a data-loss risk. Push a normal commit, or force-push a feature branch." >&2
    exit 2 ;;
  BLOCK_READONLY*)
    echo "BLOCKED by agent-guard: ${DECISION#BLOCK_READONLY?}." >&2
    echo "Read it, cite it and port the change to the repository you are working in." >&2
    exit 2 ;;
  BLOCK_NOVERIFY*)
    echo "BLOCKED by agent-guard: ${DECISION#BLOCK_NOVERIFY?}." >&2
    echo "The repository's own hooks (lint, tests, secret checks) are part of the commit contract." >&2
    echo "Fix what they report and commit without skipping them." >&2
    exit 2 ;;
  ASK*)
    # The hook JSON the host reads: permissionDecision "ask" forces a prompt.
    printf '%s\n' "${DECISION#ASK?}"
    exit 0 ;;
  DENY*)
    echo "BLOCKED by agent-guard: ${DECISION#DENY?}." >&2
    exit 2 ;;
  NOTE*)
    # A user guard rule with action warn: the call proceeds with a note.
    printf '%s\n' "${DECISION#NOTE?}"
    exit 0 ;;
  BLOCK_UNATTENDED*)
    echo "BLOCKED by agent-guard (unattended policy): ${DECISION#BLOCK_UNATTENDED?}." >&2
    echo "This run has nobody watching it. Outward writes go through pr-request.json and the runner;" >&2
    echo "see multi-agent-refs/features/unattended-security.md. Record what is needed and continue." >&2
    exit 2 ;;
  BLOCK_PARSE)
    refuse_unjudged "the guard could not parse the tool call" ;;
  OK)
    exit 0 ;;
  *)
    refuse_unjudged "the guard returned no decision" ;;
esac
