#!/usr/bin/env bash set -euo pipefail ROOT="$HOME/Desktop/AI_Brain" BRAIN="$ROOT/sub_brains/hypothalamus" LOG="$ROOT/logs/hypothalamus.log" say(){ echo "🔧 $1" | tee -a "$LOG"; } say "Hypothalamus scanning logs with heal_map.json…" MAP="$BRAIN/heal_map.json" ERRSCAN="$ROOT/logs/error_scan.tmp" grep -H -i -E "error|❌|traceback|no such file|command not found|failed|refused" "$ROOT/logs/"*.log 2>/dev/null > "$ERRSCAN" || true # helper to lookup layer get_layer() { local line="$1" while read -r key rest; do # skip if not keyword list [[ "$key" == "{" || "$key" == "}" ]] && continue layer=$(echo "$key" | sed 's/"//g' | sed 's/://') words=$(jq -r --arg l "$layer" '.[$l][]?' "$MAP") for w in $words; do if echo "$line" | grep -qi "$w"; then echo "$layer"; return; fi done done < <(jq -c 'to_entries[] | "\(.key): \(.value)"' "$MAP") } while read -r line; do [[ -z "$line" ]] && continue LAYER=$(get_layer "$line" || true) case "$LAYER" in bios) bash "$BRAIN/heal_bios.sh" ;; os) bash "$BRAIN/heal_os.sh" ;; apps) bash "$BRAIN/heal_apps.sh" ;; files) bash "$BRAIN/heal_files.sh" ;; network) bash "$BRAIN/heal_network.sh" ;; *) echo "ℹ️ No match for: $line" | tee -a "$LOG" ;; esac done < "$ERRSCAN" say "Healing pass complete."