#!/usr/bin/env bash
# unattended.sh  -  shell twin of unattended.mjs; the same two questions.
#
# ma_unattended: MULTI_AGENT_UNATTENDED=1, set by the launcher (the autopilot
# runner, or an operator), never read from agent-state.json. It decides
# security enforcement: hooks, the pre-commit gate-ledger requirement, and
# agent-guard's unattended policy.
#
# ma_gates_active <state-file>: unattended, or the state says
# `"autopilot": true` (a terminal /multi-agent:autopilot run). It decides
# whether the anti-fabrication quality gates run. A run with the variable set
# cannot switch them off by writing autopilot=false.
#
# Usage:
#   . "$(dirname "${BASH_SOURCE[0]}")/unattended.sh"
#   if ma_unattended; then ...; fi
#   if ma_gates_active "$STATE_FILE"; then ...; fi

ma_unattended() {
  [ "${MULTI_AGENT_UNATTENDED:-}" = "1" ]
}

ma_gates_active() {
  ma_unattended && return 0
  local state="${1:-}"
  [ -n "$state" ] && [ -r "$state" ] || return 1
  if command -v jq >/dev/null 2>&1; then
    [ "$(jq -r '.autopilot == true' "$state" 2>/dev/null)" = "true" ]
  else
    grep -Eq '"autopilot"[[:space:]]*:[[:space:]]*true' "$state" 2>/dev/null
  fi
}
