#!/bin/bash # Delegation Watchdog — prüft auf hängengebliebene oder verschwundene Subagenten. # Wird via cronjob alle 5 Minuten aufgerufen. # Leere Ausgabe = still (kein Alarm). Nicht-leere Ausgabe = Alarm an team/team. # # Das Timeout ist PRO AUFTRAG in delegation_tracker.json hinterlegt — nicht pro Profil. # team schätzt bei jeder Delegation: "Dieser Review braucht 10 Min, jene Implementierung 20." # Fehlt der Tracker-Eintrag (z.B. extern gestarteter Prozess), Fallback: 15 min Default. set -euo pipefail TRACKER="${HOME}/.hermes/delegation_tracker.json" DEFAULT_TIMEOUT=900 # 15 min Fallback wenn kein Tracker-Eintrag existiert now_epoch=$(date +%s) # ── Aktive hermes chat --profile Prozesse finden ────────────────────── pids=$(pgrep -f "hermes.*chat.*--profile" 2>/dev/null || true) alerts="" # ── 1. Hängengebliebene Prozesse (läuft länger als Timeout) ─────────── for pid in $pids; do elapsed=$(ps -p "$pid" -o etimes= 2>/dev/null | tr -d ' ' || true) if [ -z "$elapsed" ]; then continue fi cmd=$(ps -p "$pid" -o args= 2>/dev/null || true) profile=$(echo "$cmd" | grep -oP '(?<=--profile )\S+' | head -1 || true) [ -z "$profile" ] && profile="unknown" # -- SICHERHEITS-CHECK: Broker-Kinder erkennen -- # Prüfe PPID-Kette auf 'connector.py --profile' is_broker_child=$(python3 -c " import subprocess, os def get_ppid_chain(pid): chain = [] try: while pid > 1: with open(f'/proc/{pid}/status') as f: for line in f: if line.startswith('PPid:'): pid = int(line.split()[1]) chain.append(pid) break # Get cmdline for this parent with open(f'/proc/{pid}/cmdline') as f: if 'connector.py --profile' in f.read().replace('\x00', ' '): return True except: pass return False print(get_ppid_chain($pid)) " 2>/dev/null || true) # Timeout aus Tracker-Datei holen (pro Auftrag) threshold="" task_desc="" if [ -f "$TRACKER" ]; then tracker_info=$(python3 -c " import json, sys try: with open('$TRACKER') as f: entries = json.load(f) for e in entries: if e.get('pid') == $pid and e.get('status') == 'running': print(e.get('timeout_sec', 900)) print('|||') print(e.get('task', '')) sys.exit(0) except: sys.exit(1) " 2>/dev/null || true) if [ -n "$tracker_info" ]; then threshold=$(echo "$tracker_info" | head -1) task_desc=$(echo "$tracker_info" | tail -1 | sed 's/^|||$//') fi fi # -- ENFORCEMENT -- if [ "$is_broker_child" = "True" ]; then # Broker-Kinder niemals in Timeout-Prüfung, nur melden wenn kein Tracker if [ -z "$threshold" ]; then alerts="${alerts}ℹ️ Broker-Kind (PID ${pid}, ${profile}) läuft ohne Tracker-Eintrag (Sicherheits-Ignore)\n" fi continue fi if [ -z "$threshold" ]; then # Treffer OHNE Tracker-Eintrag nur melden, NIEMALS in Timeout-Prüfung alerts="${alerts}ℹ️ Prozess (PID ${pid}, ${profile}) läuft ohne Tracker-Eintrag (Sicherheits-Ignore)\n" continue fi if [ "$elapsed" -gt "$threshold" ]; then minutes=$((elapsed / 60)) threshold_min=$((threshold / 60)) alerts="${alerts}⚠️ ${profile} läuft seit ${minutes} min — Timeout war ${threshold_min} min (PID ${pid})\n Aufgabe: ${task_desc}\n → Nachhaken oder neu delegieren?\n" fi done # ── 2. Verschwundene Prozesse (Absturz / Gateway-Neustart) ──────────── if [ -f "$TRACKER" ]; then vanished=$(python3 -c " import json, os, sys, time try: with open('$TRACKER') as f: entries = json.load(f) now = int(time.time()) for e in entries: if e.get('status') != 'running': continue pid = e.get('pid', 0) if pid <= 0: continue try: os.kill(pid, 0) except ProcessLookupError: profile = e.get('agent', 'unknown') task = e.get('task', 'unbekannt') timeout_sec = e.get('timeout_sec', 600) age_min = (now - e.get('start_epoch', now)) // 60 expected_min = timeout_sec // 60 print(f'🚨 {profile} ist verschwunden ohne Abschluss-Benachrichtigung') print(f' Aufgabe: {task}') print(f' War seit {age_min} min aktiv (erwartet: {expected_min} min, PID {pid})') print(f' Wahrscheinlich: Absturz oder Gateway-Neustart. Neu delegieren?') print() except PermissionError: pass except Exception: pass " 2>/dev/null || true) if [ -n "$vanished" ]; then alerts="${alerts}${vanished}" fi fi # ── Alarm ausgeben (oder still bleiben) ──────────────────────────────── if [ -n "$alerts" ]; then echo -e "🔍 Delegation-Watchdog — Aufträge benötigen Aufmerksamkeit:\n" echo -e "$alerts" echo "team: Bitte prüfen — Prozess am Leben? Abgebrochen? Nachhaken oder neu delegieren." fi # Keine Alerts → leere Ausgabe → still (keine Nachricht)