#!/usr/bin/env bash
# revagent — black-box program logic analysis agent (pi + RE toolchain).
#
# Wraps the pi coding agent with an RE-specialist system prompt, the revagent
# tool extension, and format-specific skills. Any extra args/flags pass through
# to pi unchanged (e.g. --model, --print, -c).
set -euo pipefail

AGENT_HOME="${REVAGENT_HOME:-$HOME/.pi/agent/revagent}"
SYS="$AGENT_HOME/system.md"
EXT="$AGENT_HOME/extensions/index.ts"
SKILLS="$AGENT_HOME/skills"

if ! command -v pi >/dev/null 2>&1; then
  echo "error: 'pi' not found on PATH. Install the pi coding agent first." >&2
  exit 1
fi
if [ ! -f "$SYS" ]; then
  echo "error: revagent not deployed at $AGENT_HOME. Run install.sh first." >&2
  exit 1
fi

args=( --append-system-prompt "$SYS" -e "$EXT" )
# Load only the unified skill explicitly so every harness exposes the same single
# entry even if stale format-specific directories exist from an older release.
UNIFIED_SKILL="$SKILLS/analyze-blackbox-artifact"
[ -f "$UNIFIED_SKILL/SKILL.md" ] && args+=( --skill "$UNIFIED_SKILL" )

# Helpful default session name when none was provided.
case " $* " in *" -n "*|*" --name "*) ;; *) args+=( -n "revagent" ) ;; esac

exec pi "${args[@]}" "$@"
