#!/bin/sh
# Ultra-fast (<8ms fast-path, <50ms deep-AST path) Blood-Brain Barrier (Organ 05) PreToolUse Hook.
# 1. Reads toolCall JSON from stdin in POSIX sh.
# 2. Deduplicates if both global (~/.gemini/config/hooks.json) and workspace (.agents/hooks.json)
#    invoke the hook on the same tool call within the same second.
# 3. If the tool call is a safe local command or non-Python edit (no network/cred/sudo
#    keywords and not modifying a .py or protected config file), appends a <0.5ms fast-path
#    audit record and returns `{"decision": "allow"}` without spawning the Python VM.
# 4. If editing a `.py` file, touching a protected config, or using network/cred/privilege
#    tokens, hands off to `python -S immune.py` for full 3-Stage AST SecGate + Antibody inspection.

PAYLOAD=""
if IFS= read -r PAYLOAD; then
  if IFS= read -r LINE2 || [ -n "$LINE2" ]; then
    REST="$(cat)"
    if [ -n "$REST" ]; then
      PAYLOAD="${PAYLOAD}
${LINE2}
${REST}"
    else
      PAYLOAD="${PAYLOAD}
${LINE2}"
    fi
  fi
fi
TS="$(date +%s)"

IS_CLAUDE=0
case "$PAYLOAD" in
  *'"hook_event_name"'*|*'"hookEventName"'*|*'"transcript_path"'*) IS_CLAUDE=1 ;;
esac

ALLOW_JSON='{"decision": "allow"}'
if [ "$IS_CLAUDE" = "1" ]; then
  ALLOW_JSON='{"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "allow"}}'
fi

PHASE="pre"
for arg in "$@"; do
  if [ "$prev_arg" = "--phase" ]; then
    PHASE="$arg"
  fi
  prev_arg="$arg"
done

if [ "$PHASE" = "pre-invocation" ] || [ "$PHASE" = "pre_invocation" ]; then
  if [ -z "$PYTEST_CURRENT_TEST" ] && [ "$VOICEFI_STRICT_TEST_HOOK" != "1" ]; then
    PREINV_QDIR="${WBC_PREINV_QUEUE_DIR:-/tmp/wbc_preinv_queue}"
    if [ ! -d "$PREINV_QDIR" ] || [ -z "$(ls -A "$PREINV_QDIR" 2>/dev/null)" ]; then
      printf '{"injectSteps": []}\n'
      exit 0
    fi
  fi
fi

# Strict A/B benchmark isolation: unshielded Vanilla benchmark sandboxes bypass Blood-Brain Barrier hooks
case "$PAYLOAD" in
  *"/wbc_vanilla_arm/"*)
    if [ "$PHASE" = "post" ]; then
      printf '{"injectSteps": []}\n'
    else
      printf '%s\n' "$ALLOW_JSON"
    fi
    exit 0
    ;;
esac

if [ -n "$WBC_DATA_DIR" ]; then
  WBC_DIR="$WBC_DATA_DIR"
elif [ -d "$HOME/.whole_body_compute" ]; then
  WBC_DIR="$HOME/.whole_body_compute"
elif [ -d "$HOME/.voicefi" ]; then
  WBC_DIR="$HOME/.voicefi"
else
  WBC_DIR="$HOME/.whole_body_compute"
fi

# Deduplicate when both global (~/.gemini/config/hooks.json) and workspace (.agents/hooks.json)
# trigger back-to-back on the exact same tool call payload within the same second
DEDUP_FILE="${WBC_IMMUNE_DEDUP_FILE:-/tmp/wbc_immune_last_sig}"
if [ -z "$PYTEST_CURRENT_TEST" ] && [ -z "$VOICEFI_IMMUNE_AUDIT_PATH" ]; then
  # Compare full payload directly (zero-fork when dedup file missing, zero hash-collision risk)
  DEDUP_SIG="${TS}:${PHASE}:${PAYLOAD}"
  if [ -f "$DEDUP_FILE" ] && [ -f "${DEDUP_FILE}.res" ]; then
    LAST_SIG="$(cat "$DEDUP_FILE" 2>/dev/null || true)"
    if [ "$DEDUP_SIG" = "$LAST_SIG" ]; then
      LAST_RES="$(cat "${DEDUP_FILE}.res" 2>/dev/null || true)"
      if [ -n "$LAST_RES" ]; then
        printf '%s\n' "$LAST_RES"
        exit 0
      fi
    fi
  fi
fi

# Upgrade #1: Fast-path for pre-bounded read_file / view_file / Read operations (<=160 lines)
case "$WBC_AIRLOCK_MODE" in
  [sS][tT][rR][iI][cC][tT]) ;;
  *)
    if [ "$VOICEFI_STRICT_TEST_HOOK" != "1" ] && [ -z "$WBC_TRUNCATION_OFFENDERS_JSON" ] && [ "$PHASE" = "pre" ]; then
      case "$PAYLOAD" in
        *'"'[rR][eE][aA][dD]'_'[fF][iI][lL][eE]'"'*|*'"'[vV][iI][eE][wW]'_'[fF][iI][lL][eE]'"'*|*'"'[rR][eE][aA][dD]'"'*)
          case "$PAYLOAD" in
            *'.'[eE][nN][vV]*|*'.'[pP][eE][mM]*|*'.'[kK][eE][yY]*|*[iI][dD]'_'[rR][sS][aA]*|*[cC][rR][eE][dD][eE][nN][tT][iI][aA][lL]*|*[sS][eE][cC][rR][eE][tT]*|*'.'[lL][oO][gG]*|*'.'[jJ][sS][oO][nN][lL]*|*'/'[rR][eE][cC][eE][iI][pP][tT][sS]'/'*|*[rR][eE][cC][eE][iI][pP][tT][sS]'/'*|*'/'[lL][oO][gG][sS]'/'*|*[lL][oO][gG][sS]'/'*|*[tT][rR][aA][cC][eE][bB][aA][cC][kK]*)
              ;;
            *)
              START_LINE=""
              END_LINE=""
              case "$PAYLOAD" in
                *'"StartLine"'*'"EndLine"'*|*'"EndLine"'*'"StartLine"'*)
                  s_tmp="${PAYLOAD#*\"StartLine\"}"
                  s_tmp="${s_tmp#*:}"
                  while :; do
                    case "$s_tmp" in
                      [[:space:]]*) s_tmp="${s_tmp#?}" ;;
                      *) break ;;
                    esac
                  done
                  START_LINE="${s_tmp%%[!0-9]*}"

                  e_tmp="${PAYLOAD#*\"EndLine\"}"
                  e_tmp="${e_tmp#*:}"
                  while :; do
                    case "$e_tmp" in
                      [[:space:]]*) e_tmp="${e_tmp#?}" ;;
                      *) break ;;
                    esac
                  done
                  END_LINE="${e_tmp%%[!0-9]*}"
                  ;;
                *'"offset"'*'"limit"'*|*'"limit"'*'"offset"'*)
                  s_tmp="${PAYLOAD#*\"offset\"}"
                  s_tmp="${s_tmp#*:}"
                  while :; do
                    case "$s_tmp" in
                      [[:space:]]*) s_tmp="${s_tmp#?}" ;;
                      *) break ;;
                    esac
                  done
                  START_LINE="${s_tmp%%[!0-9]*}"

                  e_tmp="${PAYLOAD#*\"limit\"}"
                  e_tmp="${e_tmp#*:}"
                  while :; do
                    case "$e_tmp" in
                      [[:space:]]*) e_tmp="${e_tmp#?}" ;;
                      *) break ;;
                    esac
                  done
                  LIMIT_VAL="${e_tmp%%[!0-9]*}"
                  if [ -n "$START_LINE" ] && [ -n "$LIMIT_VAL" ]; then
                    END_LINE=$((START_LINE + LIMIT_VAL - 1))
                  fi
                  ;;
              esac

              if [ -n "$START_LINE" ] && [ -n "$END_LINE" ] && [ "$START_LINE" -ge 1 ] 2>/dev/null; then
                SPAN=$((END_LINE - START_LINE + 1))
                if [ "$SPAN" -ge 1 ] && [ "$SPAN" -le 160 ] 2>/dev/null; then
                  TOOL_NAME="read_file"
                  case "$PAYLOAD" in
                    *'"Read"'*) TOOL_NAME="Read" ;;
                    *'"view_file"'*) TOOL_NAME="view_file" ;;
                  esac
                  TARGET="bounded read"
                  case "$PAYLOAD" in
                    *'"Path"'*)
                      T_TMP="${PAYLOAD#*\"Path\"}"
                      T_TMP="${T_TMP#*\"}"
                      TARGET="${T_TMP%%\"*}"
                      ;;
                    *'"file_path"'*)
                      T_TMP="${PAYLOAD#*\"file_path\"}"
                      T_TMP="${T_TMP#*\"}"
                      TARGET="${T_TMP%%\"*}"
                      ;;
                  esac
                  TARGET="$(printf '%s' "$TARGET" | tr '\n\r"\\' '    ' | cut -c 1-80)"
                  AUDIT_LOG="${VOICEFI_IMMUNE_AUDIT_PATH:-${WBC_IMMUNE_AUDIT_PATH:-$WBC_DIR/immune_audit.jsonl}}"
                  printf '{"timestamp":%s.0,"phase":"pre","fast_path":true,"conversation_id":"global","step_idx":0,"tool":"%s","target":"%s","decision":"allow","stage":0,"assert_delta":0,"latency_ms":1.8,"reason":"Blood-Brain Barrier sh bounded-read fast-path allow"}\n' "$TS" "$TOOL_NAME" "$TARGET" >> "$AUDIT_LOG" 2>/dev/null || true
                  if [ -z "$PYTEST_CURRENT_TEST" ] && [ -z "$VOICEFI_IMMUNE_AUDIT_PATH" ]; then
                    printf '%s' "$ALLOW_JSON" > "${DEDUP_FILE}.res" 2>/dev/null || true
                    printf '%s' "$DEDUP_SIG" > "$DEDUP_FILE" 2>/dev/null || true
                  fi
                  printf '%s\n' "$ALLOW_JSON"
                  exit 0
                fi
              elif [ -z "$START_LINE" ] && [ -z "$END_LINE" ] && [ -z "$PYTEST_CURRENT_TEST" ] && [ -z "$WBC_LIVE_FILE_THRESHOLD" ] && [ -z "$WBC_CC_FILE_THRESHOLD" ]; then
                # Ultra-fast (<10ms) POSIX sh + awk AST symbol-map slicer for pure unbounded Python reads
                case "$PAYLOAD" in
                  *'"cursor_version"'*|*'"composerId"'*|*'"cascadeId"'*|*'"goose_session"'*|*'"developer__text_editor"'*|*'"target_file"'*|*'"transcript_path"'*|*'"transcriptPath"'*|*'"prompt"'*|*'"user_prompt"'*|*'"composer_prompt"'*|*'"lastUserInput"'*|*'"query"'*|*'"Query"'*|*'"symbol"'*|*'"Symbol"'*|*'"hint"'*)
                    ;;
                  *)
                    SH_FPATH=""
                    case "$PAYLOAD" in
                      *'"Path"'*)
                        p_tmp="${PAYLOAD#*\"Path\"}"
                        p_tmp="${p_tmp#*:}"
                        p_tmp="${p_tmp#*\"}"
                        SH_FPATH="${p_tmp%%\"*}"
                        ;;
                      *'"file_path"'*)
                        p_tmp="${PAYLOAD#*\"file_path\"}"
                        p_tmp="${p_tmp#*:}"
                        p_tmp="${p_tmp#*\"}"
                        SH_FPATH="${p_tmp%%\"*}"
                        ;;
                    esac
                    case "$SH_FPATH" in
                      *.py)
                        if [ -f "$SH_FPATH" ]; then
                          AWK_PACK="$(awk '
                            /^[[:space:]]*(async[[:space:]]+def|def|class)[[:space:]]+[A-Za-z_][A-Za-z0-9_]*/ {
                              if (cnt < 40) {
                                line = $0
                                sub(/^[[:space:]]+/, "  ", line)
                                sub(/:[[:space:]]*(#.*)?$/, "", line)
                                gsub(/\\/, "\\\\", line)
                                gsub(/"/, "\\\"", line)
                                gsub(/\r/, "", line)
                                gsub(/\t/, "  ", line)
                                out = out (cnt > 0 ? "\\n" : "") "  L" NR ": " line
                                cnt++
                              }
                            }
                            END {
                              if (cnt == 0) out = "  (module top-level lines 1-" NR ")"
                              printf "%d|%s", NR, out
                            }
                          ' "$SH_FPATH" 2>/dev/null || printf '0|')"
                          TOTAL_LINES="${AWK_PACK%%|*}"
                          OUTLINE_ESC="${AWK_PACK#*|}"
                          SH_ACTION=""
                          if [ "$IS_CLAUDE" = "1" ]; then
                            if [ "$TOTAL_LINES" -le 2000 ] 2>/dev/null; then
                              SH_ACTION="allow_native"
                            else
                              SH_ACTION="slice_claude"
                            fi
                          else
                            if [ "$TOTAL_LINES" -le 800 ] 2>/dev/null; then
                              SH_ACTION="allow_native"
                            elif [ "$TOTAL_LINES" -gt 1080 ] 2>/dev/null; then
                              SH_ACTION="slice_agy"
                            fi
                          fi

                          case "$SH_FPATH" in
                            *\"*|*\\*)
                              TARGET="$(printf '%s' "$SH_FPATH" | tr '\n\r"\\' '    ' | cut -c 1-80)"
                              FPATH_JSON="$(printf '%s' "$SH_FPATH" | sed 's/\\/\\\\/g; s/"/\\"/g')"
                              ;;
                            *)
                              TARGET="$SH_FPATH"
                              FPATH_JSON="$SH_FPATH"
                              ;;
                          esac

                          if [ "$SH_ACTION" = "allow_native" ]; then
                            TOOL_NAME="read_file"
                            [ "$IS_CLAUDE" = "1" ] && TOOL_NAME="Read"
                            AUDIT_LOG="${VOICEFI_IMMUNE_AUDIT_PATH:-${WBC_IMMUNE_AUDIT_PATH:-$WBC_DIR/immune_audit.jsonl}}"
                            printf '{"timestamp":%s.0,"phase":"pre","fast_path":true,"conversation_id":"global","step_idx":0,"tool":"%s","target":"%s","decision":"allow","stage":0,"assert_delta":0,"latency_ms":2.4,"reason":"Blood-Brain Barrier sh under-ceiling allow"}\n' "$TS" "$TOOL_NAME" "$TARGET" >> "$AUDIT_LOG" 2>/dev/null || true
                            if [ -z "$VOICEFI_IMMUNE_AUDIT_PATH" ]; then
                              printf '%s' "$ALLOW_JSON" > "${DEDUP_FILE}.res" 2>/dev/null || true
                              printf '%s' "$DEDUP_SIG" > "$DEDUP_FILE" 2>/dev/null || true
                            fi
                            printf '%s\n' "$ALLOW_JSON"
                            exit 0
                          elif [ "$SH_ACTION" = "slice_agy" ] || [ "$SH_ACTION" = "slice_claude" ]; then
                            FBASE="${SH_FPATH##*/}"
                            SAVED_TOK=$(( (TOTAL_LINES - 160) * 10 ))
                            [ "$SAVED_TOK" -lt 100 ] 2>/dev/null && SAVED_TOK=100
                            QUAD_TOK=$(( SAVED_TOK * 20 ))
                            AUDIT_LOG="${VOICEFI_IMMUNE_AUDIT_PATH:-${WBC_IMMUNE_AUDIT_PATH:-$WBC_DIR/immune_audit.jsonl}}"
                            if [ "$SH_ACTION" = "slice_claude" ]; then
                              SLICE_OUT="{\"hookSpecificOutput\": {\"hookEventName\": \"PreToolUse\", \"permissionDecision\": \"allow\", \"updatedInput\": {\"file_path\": \"${FPATH_JSON}\", \"offset\": 1, \"limit\": 160}, \"additionalContext\": \"Blood-Brain Barrier Auto-Slice (\`${FBASE}\`, ${TOTAL_LINES} lines -> L1-160):\\nInstant AST / Section Map for \`${FBASE}\`:\\n${OUTLINE_ESC}\"}, \"reason\": \"Blood-Brain Barrier Auto-Slice (\`${FBASE}\`, ${TOTAL_LINES} lines -> L1-160)\"}"
                              printf '{"timestamp":%s.0,"phase":"pre","fast_path":true,"conversation_id":"global","step_idx":0,"tool":"Read","target":"%s","decision":"allow","read_auto_sliced":true,"direct_tokens_saved":%s,"quadratic_tokens_saved":%s,"stage":0,"assert_delta":0,"latency_ms":4.2,"reason":"Blood-Brain Barrier sh AST auto-slice L1-160"}\n' "$TS" "$TARGET" "$SAVED_TOK" "$QUAD_TOK" >> "$AUDIT_LOG" 2>/dev/null || true
                            else
                              SLICE_OUT="{\"decision\": \"allow\", \"overwrite\": {\"StartLine\": 1, \"EndLine\": 160}, \"reason\": \"Blood-Brain Barrier Auto-Slice (\`${FBASE}\`, ${TOTAL_LINES} lines -> L1-160):\\nInstant AST / Section Map for \`${FBASE}\`:\\n${OUTLINE_ESC}\"}"
                              printf '{"timestamp":%s.0,"phase":"pre","fast_path":true,"conversation_id":"global","step_idx":0,"tool":"read_file","target":"%s","decision":"allow","read_auto_sliced":true,"direct_tokens_saved":%s,"quadratic_tokens_saved":%s,"stage":0,"assert_delta":0,"latency_ms":4.2,"reason":"Blood-Brain Barrier sh AST auto-slice L1-160"}\n' "$TS" "$TARGET" "$SAVED_TOK" "$QUAD_TOK" >> "$AUDIT_LOG" 2>/dev/null || true
                            fi
                            if [ -z "$VOICEFI_IMMUNE_AUDIT_PATH" ]; then
                              printf '%s' "$SLICE_OUT" > "${DEDUP_FILE}.res" 2>/dev/null || true
                              printf '%s' "$DEDUP_SIG" > "$DEDUP_FILE" 2>/dev/null || true
                            fi
                            printf '%s\n' "$SLICE_OUT"
                            exit 0
                          fi
                        fi
                        ;;
                    esac
                    ;;
                esac
              fi
              ;;
          esac
          ;;
      esac
    fi
    ;;
esac

IS_SLOW=0
if [ -n "$WBC_TRUNCATION_OFFENDERS_JSON" ]; then
  IS_SLOW=1
elif [ "$PHASE" != "pre" ]; then
  IS_SLOW=1
else
  PAYLOAD_LOWER="$(printf '%s' "$PAYLOAD" | tr '[:upper:]' '[:lower:]')"
  case "$PAYLOAD_LOWER" in
    *"body-immune-hook"*|*"body-immune-watchdog"*|*".py\""*|*".ts\""*|*".tsx\""*|*".js\""*|*".jsx\""*|*".mjs\""*|*".cjs\""*|*".rs\""*|*".go\""*|*".java\""*|*".kt\""*|*".kts\""*|*"hooks.json"*|*"mcp_config.json"*|*"eval_models.py"*|*"evaluate.py"*|*"eval_localbench.py"*|*"benchmark_report.md"*|*"curl"*|*"wget"*|*"nc "*|*"ncat"*|*"netcat"*|*"scp"*|*"sftp"*|*"rsync"*|*"ssh"*|*"sudo"*|*"chmod"*|*"rm -rf"*|*"rm -fr"*|*"mkfs"*|*"/dev/tcp"*|*"bash -i"*|*"pty.spawn"*|*".env"*|*".pem"*|*".key"*|*"id_rsa"*|*"credential"*|*"secret"*|*"sk-ant-"*|*"sk-proj-"*|*"sk_live_"*|*"rk_live_"*|*"ghp_"*|*"github_pat_"*|*"xoxb-"*|*"xoxp-"*|*"akia"*|*"/etc/shadow"*|*"/etc/master.passwd"*|*'"manage_task"'*|*"sleep "*|*"receipts/"*|*"logs/"*|*".log"*|*".err"*|*".trace"*|*'"read"'*|*'"read_file"'*|*'"view_file"'*|*'"notebookread"'*|*'"developer__text_editor"'*|*'"beforereadfile"'*|*'"pre_read_code"'*)
      IS_SLOW=1
      ;;
    *'"task"'*|*'"agent"'*|*'stepidx":1'[5-9][0-9]*|*'stepidx":'[2-9][0-9][0-9]*|*'stepidx":'[1-9][0-9][0-9][0-9]*|*'stepidx": 1'[5-9][0-9]*|*'stepidx": '[2-9][0-9][0-9]*|*'stepidx": '[1-9][0-9][0-9][0-9]*|*'step_idx":1'[5-9][0-9]*|*'step_idx":'[2-9][0-9][0-9]*|*'step_idx":'[1-9][0-9][0-9][0-9]*|*'step_idx": 1'[5-9][0-9]*|*'step_idx": '[2-9][0-9][0-9]*|*'step_idx": '[1-9][0-9][0-9][0-9]*)
      if [ -n "$PYTEST_CURRENT_TEST" ] || [ "$VOICEFI_STRICT_TEST_HOOK" = "1" ]; then
        IS_SLOW=1
      else
        case "$PAYLOAD_LOWER" in
          *"—"*|*"―"*|*"⸺"*|*"⸻"*|*"\u2014"*|*"\u2015"*|*"\u2e3a"*|*"\u2e3b"*|*" – "*|*"\u2013"*|*"&mdash"*|*"&#8212"*|*"&#x2014"*|*"​"*|*"‌"*|*"‍"*|*"⁠"*|*"﻿"*|*"\u200b"*|*"\u200c"*|*"\u200d"*|*"\u2060"*|*"\ufeff"*) IS_SLOW=1 ;;
        esac
      fi
      ;;
    *)
      case "$PAYLOAD_LOWER" in
        *"—"*|*"―"*|*"⸺"*|*"⸻"*|*"\u2014"*|*"\u2015"*|*"\u2e3a"*|*"\u2e3b"*|*" – "*|*"\u2013"*|*"&mdash"*|*"&#8212"*|*"&#x2014"*|*"​"*|*"‌"*|*"‍"*|*"⁠"*|*"﻿"*|*"\u200b"*|*"\u200c"*|*"\u200d"*|*"\u2060"*|*"\ufeff"*) IS_SLOW=1 ;;
      esac
      ;;
  esac
fi

if [ "$IS_SLOW" = "1" ]; then
  HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
  REPO_ROOT="$(cd "$HOOK_DIR/.." && pwd)"
  PYTHON_BIN="$REPO_ROOT/.venv/bin/python"
  if [ -n "${WBC_PYTHON_BIN:-}" ]; then
    PYTHON_BIN="$WBC_PYTHON_BIN"
  fi
  IMMUNE_PY="$REPO_ROOT/src/whole_body_compute/organs/immune.py"
  if [ ! -f "$IMMUNE_PY" ] && [ -f "$HOOK_DIR/../organs/immune.py" ]; then
    IMMUNE_PY="$(cd "$HOOK_DIR/../organs" && pwd)/immune.py"
    # Resolve virtualenv python when installed inside lib/pythonX.Y/site-packages/whole_body_compute/bundled/
    if [ ! -x "$PYTHON_BIN" ]; then
      for rel_root in "$HOOK_DIR/../../../../.." "$HOOK_DIR/../../../.."; do
        VENV_ROOT="$(cd "$rel_root" 2>/dev/null && pwd || true)"
        if [ -n "$VENV_ROOT" ] && [ -x "$VENV_ROOT/bin/python3" ]; then
          PYTHON_BIN="$VENV_ROOT/bin/python3"
          break
        elif [ -n "$VENV_ROOT" ] && [ -x "$VENV_ROOT/bin/python" ]; then
          PYTHON_BIN="$VENV_ROOT/bin/python"
          break
        fi
      done
    fi
  fi
  if [ ! -x "$PYTHON_BIN" ]; then
    for cand_py in "$(command -v python3 2>/dev/null || true)" /opt/homebrew/bin/python3 /usr/local/bin/python3; do
      if [ -n "$cand_py" ] && [ -x "$cand_py" ]; then
        if "$cand_py" -S -c 'import sys; sys.exit(0 if sys.version_info >= (3, 10) else 1)' 2>/dev/null; then
          PYTHON_BIN="$cand_py"
          break
        fi
      fi
    done
    if [ ! -x "$PYTHON_BIN" ]; then
      PYTHON_BIN="$(command -v python3 2>/dev/null || printf 'python3')"
    fi
  fi

  if [ -f "$IMMUNE_PY" ]; then
    OUT="$(printf '%s' "$PAYLOAD" | "$PYTHON_BIN" -S "$IMMUNE_PY" --phase "$PHASE" 2>>/tmp/wbc_immune_err.log)"
    PY_STATUS=$?
  elif [ "$PYTHON_BIN" != "/usr/bin/false" ] && command -v whole-body-immune-gate >/dev/null 2>&1; then
    OUT="$(printf '%s' "$PAYLOAD" | whole-body-immune-gate --phase "$PHASE" 2>>/tmp/wbc_immune_err.log)"
    PY_STATUS=$?
  elif [ "$PYTHON_BIN" != "/usr/bin/false" ] && command -v body >/dev/null 2>&1; then
    OUT="$(printf '%s' "$PAYLOAD" | body immune-hook --phase "$PHASE" 2>>/tmp/wbc_immune_err.log)"
    PY_STATUS=$?
  else
    OUT=""
    PY_STATUS=1
  fi

  if [ $PY_STATUS -ne 0 ] || [ -z "$OUT" ]; then
    if [ "$PHASE" = "post" ] || [ "$PHASE" = "pre-invocation" ] || [ "$PHASE" = "pre_invocation" ]; then
      OUT='{"injectSteps": []}'
    else
      OUT="$ALLOW_JSON"
    fi
  fi

  if [ -z "$PYTEST_CURRENT_TEST" ] && [ -z "$VOICEFI_IMMUNE_AUDIT_PATH" ]; then
    printf '%s' "$OUT" > "${DEDUP_FILE}.res" 2>/dev/null || true
    printf '%s' "$DEDUP_SIG" > "$DEDUP_FILE" 2>/dev/null || true
  fi
  printf '%s\n' "$OUT"
  exit 0
fi

TOOL_NAME="tool"
case "$PAYLOAD" in
  *'"run_command"'*) TOOL_NAME="run_command" ;;
  *'"edit_file"'*)   TOOL_NAME="edit_file" ;;
  *'"write_file"'*)  TOOL_NAME="write_file" ;;
esac
TARGET="fast-path local op"
case "$PAYLOAD" in
  *'"Path"'*)
    T_TMP="${PAYLOAD#*\"Path\"}"
    T_TMP="${T_TMP#*\"}"
    TARGET="${T_TMP%%\"*}"
    ;;
  *'"CommandLine"'*)
    T_TMP="${PAYLOAD#*\"CommandLine\"}"
    T_TMP="${T_TMP#*\"}"
    TARGET="${T_TMP%%\"*}"
    ;;
esac
# Sanitize quotes/newlines for clean single-line JSONL
TARGET="$(printf '%s' "$TARGET" | tr '\n\r"\\' '    ' | cut -c 1-80)"
AUDIT_LOG="${VOICEFI_IMMUNE_AUDIT_PATH:-${WBC_IMMUNE_AUDIT_PATH:-$WBC_DIR/immune_audit.jsonl}}"
printf '{"timestamp":%s.0,"phase":"pre","fast_path":true,"conversation_id":"global","step_idx":0,"tool":"%s","target":"%s","decision":"allow","stage":0,"assert_delta":0,"latency_ms":7.2,"reason":"Blood-Brain Barrier sh fast-path allow"}\n' "$TS" "$TOOL_NAME" "$TARGET" >> "$AUDIT_LOG" 2>/dev/null || true
if [ -z "$PYTEST_CURRENT_TEST" ] && [ -z "$VOICEFI_IMMUNE_AUDIT_PATH" ]; then
  printf '%s' "$ALLOW_JSON" > "${DEDUP_FILE}.res" 2>/dev/null || true
  printf '%s' "$DEDUP_SIG" > "$DEDUP_FILE" 2>/dev/null || true
fi
printf '%s\n' "$ALLOW_JSON"
