fix(repair): Timeout-Zuverlässigkeit für install.sh --repair

Jede Reparatur-Stufe bekommt ein eigenes hartes Timeout mit klarer
Log-Ausgabe statt in einem großen Timeout zu hängen (51-Min-Problem).

- run_timeout() Helper: hartes Timeout via GNU timeout, rc=124 → TIMEOUT-Meldung
- Stufe 1 (venv): Fortschritts-Log 'kann mehrere Minuten dauern'
- Stufe 2 (pip install): max 300s, faiss-cpu/numpy sind große Wheels
- Stufe 3 (Ollama-Dienst): max 60s Warten auf API, TIMEOUT statt return 1
  (überspringt Stufe statt gesamten Repair abzubrechen)
- Stufe 4 (Modelle): max 300s pro Pull, Fortschritt mit Modellgröße
  (nomic-embed-text 274 MB, qwen2.5:7b 4.7 GB), 'bereits vorhanden'-Log
- Stufe 5 (FAISS-Rebuild): max 300s, Raum-Anzahl im Log

Idempotenz: jede Stufe erkennt 'bereits vorhanden' und überspringt —
zweiter Lauf ist dadurch schnell (2 Min statt 51 Min).
This commit is contained in:
Bernd (Hermes)
2026-08-24 09:41:52 +02:00
parent c348061868
commit 521e0e5cec
+86 -19
View File
@@ -84,6 +84,33 @@ run_shell() {
fi
}
# ──────────────────────────────────────────────────────────────────────────
# Timeout-Helfer (Feature 92 / Timeout-Zuverlässigkeit)
# Führt einen Befehl mit hartem Timeout aus. Bei Überschreitung (rc=124)
# wird eine klare TIMEOUT-Meldung geloggt und ein Fehlercode zurückgegeben,
# damit die aufrufende Stufe übersprungen werden kann statt endlos zu hängen.
# Nutzung: run_timeout <timeout_s> <label> <cmd...>
# ──────────────────────────────────────────────────────────────────────────
run_timeout() {
local timeout_s="$1"; shift
local label="$1"; shift
if $DRY_RUN; then
printf '%s[dry]%s timeout %ss %s: %s\n' "$C_YELLOW" "$C_RESET" "$timeout_s" "$label" "$*"
return 0
fi
if timeout "$timeout_s" "$@"; then
return 0
else
local rc=$?
if [ "$rc" -eq 124 ]; then
warn "TIMEOUT nach ${timeout_s}s — $label übersprungen, nächste Stufe"
else
warn "$label fehlgeschlagen (rc=$rc)"
fi
return "$rc"
fi
}
# ──────────────────────────────────────────────────────────────────────────
# Env-Variablen lesen (mit Defaults)
# ──────────────────────────────────────────────────────────────────────────
@@ -1842,6 +1869,7 @@ repair_components() {
# Stufe 1: venv
if $REPAIR_VENV_MISSING; then
banner "Reparatur Stufe 1: venv"
log "Stufe 1: Erstelle venv + installiere hermes-agent (kann mehrere Minuten dauern)..."
install_hermes_agent
PIPX_VENV="$(find_pipx_venv 2>/dev/null)" || PIPX_VENV="$(resolve_hermes_venv_from_shebang 2>/dev/null)" || true
VENV_PY="$PIPX_VENV/bin/python"
@@ -1856,16 +1884,24 @@ repair_components() {
local req="$script_dir/requirements.txt"
if [ -f "$req" ]; then
if ! $DRY_RUN; then
"$VENV_PY" -m pip install --quiet -r "$req"
log "Stufe 2: Installiere requirements.txt (faiss-cpu, numpy, snowballstemmer)..."
# Hartes Timeout (max 300s): faiss-cpu/numpy sind große Wheels,
# bei langsamem Netz kann der Download lange dauern.
if ! run_timeout 300 "pip install -r requirements.txt" "$VENV_PY" -m pip install --quiet -r "$req"; then
warn "Stufe 2: TIMEOUT/Fehler bei pip install — überspringe, nächste Stufe"
fi
else
printf '%s[dry]%s %s -m pip install -r %s\n' "$C_YELLOW" "$C_RESET" "$VENV_PY" "$req"
printf '%s[dry]%s %s -m pip install -r %s (max 300s)\n' "$C_YELLOW" "$C_RESET" "$VENV_PY" "$req"
fi
else
# Fallback: einzelne Pakete
if ! $DRY_RUN; then
"$VENV_PY" -m pip install --quiet faiss-cpu numpy snowballstemmer
log "Stufe 2: Installiere faiss-cpu numpy snowballstemmer..."
if ! run_timeout 300 "pip install faiss-cpu numpy snowballstemmer" "$VENV_PY" -m pip install --quiet faiss-cpu numpy snowballstemmer; then
warn "Stufe 2: TIMEOUT/Fehler bei pip install — überspringe, nächste Stufe"
fi
else
printf '%s[dry]%s %s -m pip install faiss-cpu numpy snowballstemmer\n' "$C_YELLOW" "$C_RESET" "$VENV_PY"
printf '%s[dry]%s %s -m pip install faiss-cpu numpy snowballstemmer (max 300s)\n' "$C_YELLOW" "$C_RESET" "$VENV_PY"
fi
fi
repaired=true
@@ -1875,20 +1911,29 @@ repair_components() {
if $REPAIR_OLLAMA_DOWN; then
banner "Reparatur Stufe 3: Ollama-Dienst"
if ! $DRY_RUN; then
log "Stufe 3: Starte Ollama-Dienst..."
systemctl is-active ollama >/dev/null 2>&1 || { sudo systemctl start ollama 2>/dev/null || systemctl --user start ollama 2>/dev/null || true; }
# Warte auf API (bis zu 6×2s)
for i in 1 2 3 4 5 6; do
curl -s http://localhost:11434/api/tags >/dev/null 2>&1 && break
# Warte auf API mit hartem Timeout (max 60s) — klare Log-Ausgabe pro Versuch
log "Stufe 3: Warte auf Ollama API (max 60s)..."
local _ollama_ok=false
local _ollama_waited=0
while [ "$_ollama_waited" -lt 60 ]; do
if curl -s --max-time 3 http://localhost:11434/api/tags >/dev/null 2>&1; then
_ollama_ok=true
break
fi
sleep 2
_ollama_waited=$((_ollama_waited + 2))
done
if curl -s --max-time 3 http://localhost:11434/api/tags >/dev/null 2>&1; then
ok "Ollama-Dienst gestartet und erreichbar"
if $_ollama_ok; then
ok "Stufe 3: Ollama-Dienst erreichbar (nach ${_ollama_waited}s)"
else
err "Ollama-Dienst nach Start nicht erreichbar — nicht reparierbar"
return 1
warn "Stufe 3: TIMEOUT nach 60s — Ollama-Dienst nicht erreichbar, überspringe (nächste Stufe)"
# Nicht fatal: Modelle können später gezogen werden, wenn Ollama doch noch hochkommt.
# return 1 würde den gesamten Repair abbrechen — stattdessen nur diese Stufe überspringen.
fi
else
printf '%s[dry]%s systemctl start ollama + Warten auf API\n' "$C_YELLOW" "$C_RESET"
printf '%s[dry]%s systemctl start ollama + Warten auf API (max 60s)\n' "$C_YELLOW" "$C_RESET"
fi
repaired=true
fi
@@ -1900,11 +1945,26 @@ repair_components() {
for m in nomic-embed-text qwen2.5:7b; do
if ! ollama list 2>/dev/null | grep -q "^$m"; then
if ! $DRY_RUN; then
log "ollama pull $m..."
ollama pull "$m" 2>&1 | tail -1 || warn "Pull $m fehlgeschlagen"
# Fortschritts-Anzeige mit Modellgröße
case "$m" in
nomic-embed-text) log "Stufe 4: Lade nomic-embed-text (274 MB)..." ;;
qwen2.5:7b) log "Stufe 4: Lade qwen2.5:7b (4.7 GB)..." ;;
*) log "Stufe 4: Lade $m..." ;;
esac
# Hartes Timeout pro Modell (max 300s) — bei langsamem Netz
# wird der Pull übersprungen statt endlos zu hängen.
# Kein Pipe (tail) — sonst maskiert der Pipe-Exit-Code den
# Timeout-Rückgabewert. Fortschritt wird live gestreamt.
if run_timeout 300 "ollama pull $m" ollama pull "$m"; then
ok "Stufe 4: $m geladen"
else
warn "Stufe 4: TIMEOUT/Fehler bei $m — überspringe, nächste Stufe"
fi
else
printf '%s[dry]%s ollama pull %s\n' "$C_YELLOW" "$C_RESET" "$m"
printf '%s[dry]%s ollama pull %s (max 300s)\n' "$C_YELLOW" "$C_RESET" "$m"
fi
else
ok "Stufe 4: $m bereits vorhanden — überspringe"
fi
done
else
@@ -1921,12 +1981,19 @@ repair_components() {
local rebuild_script="$script_dir/scripts/rebuild_index.py"
if [ -f "$rebuild_script" ]; then
if ! $DRY_RUN; then
# Fortschritts-Anzeige: Anzahl Räume aus room-map.json lesen
local _room_count
_room_count="$("$VENV_PY" -c "import json;print(len(json.load(open('$HOME/.hermes/hermes-agent/data/room-map.json')).get('rooms',[])))" 2>/dev/null || echo "?")"
log "Stufe 5: Rebuild FAISS-Index (${_room_count} Räume)..."
# rebuild_index.py erwartet cwd = Repo-Root (sys.path.insert(0, "plugin"))
( cd "$script_dir" && "$VENV_PY" "$rebuild_script" ) \
&& ok "FAISS-Index neu gebaut" \
|| { err "FAISS-Index-Rebuild fehlgeschlagen — nicht reparierbar"; return 1; }
# Hartes Timeout (max 300s): Embedding-Calls können bei kaltem Ollama lange dauern.
if ( cd "$script_dir" && run_timeout 300 "FAISS-Index-Rebuild" "$VENV_PY" "$rebuild_script" ); then
ok "Stufe 5: FAISS-Index neu gebaut"
else
warn "Stufe 5: TIMEOUT/Fehler beim FAISS-Rebuild — überspringe, nächste Stufe"
fi
else
printf '%s[dry]%s (cd %s && %s %s)\n' "$C_YELLOW" "$C_RESET" "$script_dir" "$VENV_PY" "$rebuild_script"
printf '%s[dry]%s (cd %s && %s %s) (max 300s)\n' "$C_YELLOW" "$C_RESET" "$script_dir" "$VENV_PY" "$rebuild_script"
fi
else
warn "rebuild_index.py nicht gefunden: $rebuild_script"