#!/usr/bin/env bash
set -euo pipefail

if [[ $# -lt 1 ]]; then
  cat >&2 <<'USAGE'
Usage:
  jarvis-test-analyze <repo-path> [logfile-or-command-or-task]

Examples:
  jarvis-test-analyze /home/agent/projects/FamilyDashboard /tmp/vitest.log
  jarvis-test-analyze . "npm test -- --runInBand"
USAGE
  exit 2
fi

REPO="$1"
INPUT="${2:-Analyze recent test/build/lint failures in this repo. Do not modify files.}"
cd "$REPO"

if [[ -f "$INPUT" ]]; then
  LOG="$(tail -c 40000 "$INPUT")"
  SOURCE="logfile: $INPUT (last 40k chars)"
else
  SOURCE="task/command/context: $INPUT"
  LOG=""
fi

STATUS="$(git status --short 2>/dev/null || true)"

PROMPT=$(cat <<EOF
Du bist der JARVIS Test-/Build-Log-Analyst (read-only, GPT 5.3 Codex-Spark).

Repo: $REPO
Quelle: $SOURCE

Regeln:
- Keine Dateien ändern, keine Commits, kein Push, kein Deploy.
- Keine Secrets ausgeben.
- Analysiere Fehlerursachen kompakt auf Deutsch.
- Gruppiere ähnliche Fehler, nenne wahrscheinlichste Root Cause und minimalen Fixvorschlag.
- GPT 5.5/JARVIS entscheidet final und verifiziert.

Git status:
$STATUS

Log/Context:
$LOG

Wenn keine Logdaten vorhanden sind, inspiziere nur gezielt Projektdateien/Testkonfiguration, keine grossen Dumps.
EOF
)

hermes --profile code-review chat --toolsets terminal,file --max-turns 40 -q "$PROMPT"
