From 0700f4024e7fb2193354dec3f2a8d32e9c24dd0e Mon Sep 17 00:00:00 2001 From: "Bernd (Hermes)" Date: Sat, 15 Aug 2026 10:04:51 +0200 Subject: [PATCH] fix(P-074): Crash-Forensik Pfad-Mismatch + SIGKILL-Klassifikation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit P-074a (Pfad-Mismatch): - GATEWAY_STATE_PATH korrigiert: HERMES_HOME/gateway_state.json (Core schreibt via status.py _get_pid_path().with_name(), nicht gateway/gateway_state.json) - _resolve_path() Helper: primärer Pfad + Fallback-Lage, graceful skip wenn Datei fehlt (Quelle als 'nicht verfügbar', kein Fehler) - Quelle 3 (restart_loop.json) + Quelle 4 (gateway_state.json) nutzen jetzt dynamische Pfad-Auflösung statt hardcoded P-074b (SIGKILL-Klassifikation): - classify_crash_source() erkennt systemd 'status=9/KILL', 'code=killed', 'signal=KILL', 'SIGKILL' → neue Kategorie 'external (SIGKILL)' wenn kein Traceback vorliegt (kill -9 / OOM erzeugt keinen Python-Traceback) - Core-Pfad-Erkennung erweitert: hermes-agent/ (Bindestrich, realer Install-Pfad) zusätzlich zu hermes_agent/ (Unterstrich) — vorher wurde 'core' nie erkannt - Test-Mode um SIGKILL-Szenario erweitert Verifiziert: Test-Mode liefert alle 6 Datenpunkte, Integrationstest S1/S2/S3 ALL_PASS, SIGKILL-Klassifikation liefert 'external (SIGKILL)' --- plugin/crash_forensics.py | 96 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 7 deletions(-) diff --git a/plugin/crash_forensics.py b/plugin/crash_forensics.py index 5cdf97d..a94aa59 100644 --- a/plugin/crash_forensics.py +++ b/plugin/crash_forensics.py @@ -33,9 +33,20 @@ logger = logging.getLogger("hermes-rocketchat.crash_forensics") HERMES_HOME = Path(os.environ.get("HERMES_HOME", os.path.expanduser("~/.hermes"))) HEARTBEAT_PATH = HERMES_HOME / "state" / "gateway.heartbeat" RESTART_LOOP_PATH = HERMES_HOME / "gateway" / "restart_loop.json" -GATEWAY_STATE_PATH = HERMES_HOME / "gateway" / "gateway_state.json" +GATEWAY_STATE_PATH = HERMES_HOME / "gateway_state.json" TAKEOVER_MARKER_PATH = HERMES_HOME / ".gateway-takeover.json" +# P-074a: Historische Pfad-Varianten als Fallback. Der Core schreibt: +# - gateway_state.json → HERMES_HOME/gateway_state.json +# (status.py:_get_pid_path().with_name("gateway_state.json"), _get_pid_path +# = HERMES_HOME/gateway.pid → with_name = HERMES_HOME/gateway_state.json) +# - restart_loop.json → HERMES_HOME/gateway/restart_loop.json +# (restart_loop_guard.py:_state_path = get_hermes_home()/"gateway"/...) +# Die Fallback-Lagen decken ältere/abweichende Core-Versionen ab, damit die +# 4-Quellen-Korrelation nicht an einem Pfad-Mismatch scheitert. +RESTART_LOOP_PATH_FALLBACK = HERMES_HOME / "restart_loop.json" +GATEWAY_STATE_PATH_FALLBACK = HERMES_HOME / "gateway" / "gateway_state.json" + # ── Konstanten ─────────────────────────────────────────────────────────── HEARTBEAT_MAX_AGE_S = 3600 # 1h — ältere Heartbeats sind veraltete Leichen RESTART_LOOP_WINDOW_S = 60 # 60s — Boot-Timestamp innerhalb dieses Fensters = Crash @@ -104,6 +115,25 @@ def _read_json(path: Path) -> Optional[Dict[str, Any]]: return None +def _resolve_path(primary: Path, fallback: Path) -> Optional[Path]: + """P-074a: Primärpfad auflösen, bei Nicht-Existenz auf Fallback ausweichen. + + Gibt den ersten existierenden Pfad zurück, sonst None. Verhindert, dass + die 4-Quellen-Korrelation an einem Pfad-Mismatch (Core schreibt an anderer + Stelle als das Plugin liest) scheitert. Quelle wird bei fehlender Datei + als 'nicht verfügbar' gewertet — kein Fehler, kein False-Positive. + """ + if primary.exists(): + return primary + if fallback.exists(): + logger.debug( + "crash_forensics: primary path %s missing — using fallback %s", + primary, fallback, + ) + return fallback + return None + + def _read_heartbeat() -> Optional[Dict[str, Any]]: """Heartbeat-Datei lesen und validieren. @@ -269,17 +299,46 @@ def classify_crash_source(journal_entries: List[str]) -> str: """Klassifiziere Crash-Quelle aus Journal-Einträgen. Returns: - "plugin" — Traceback enthält plugin/ Pfade - "core" — Traceback enthält hermes_agent/ Pfade - "unknown" — keine Traceback-Informationen gefunden + "plugin" — Traceback enthält plugin/ Pfade + "core" — Traceback enthält hermes_agent/ Pfade + "mixed" — Traceback enthält beide Pfade + "external (SIGKILL)" — systemd meldet status=9/KILL oder code=killed + OHNE Python-Traceback (kill -9, OOM-Killer) + "unknown" — keine Crash-Informationen gefunden """ plugin_hits = 0 core_hits = 0 + sigkill_hits = 0 for line in journal_entries: if "plugin/" in line or "/plugin/" in line: plugin_hits += 1 - if "hermes_agent/" in line or "/hermes_agent/" in line: + # Core-Pfad: hermes-agent/ (Bindestrich, realer Install-Pfad) ODER + # hermes_agent/ (Unterstrich, historische/importierte Variante). + if ( + "hermes_agent/" in line + or "/hermes_agent/" in line + or "hermes-agent/" in line + or "/hermes-agent/" in line + ): core_hits += 1 + # P-074b: SIGKILL-Signatur — systemd beendet den Prozess hart, es gibt + # KEINEN Python-Traceback. Muster: 'status=9/KILL', 'code=killed', + # 'signal=KILL', 'SIGKILL'. Diese Zeilen sind ein eigenständiges + # Crash-Signal, kein Plugin-vs-Core-Crash. + low = line.lower() + if ( + "status=9" in low + or "code=killed" in low + or "signal=kill" in low + or "sigkill" in low + ): + sigkill_hits += 1 + + # P-074b: SIGKILL hat Vorrang, wenn kein Traceback vorliegt. Ein externer + # Kill (kill -9 / OOM) ist weder Plugin- noch Core-Crash — eigene Kategorie. + if sigkill_hits > 0 and plugin_hits == 0 and core_hits == 0: + return "external (SIGKILL)" + if plugin_hits > core_hits: return "plugin" elif core_hits > plugin_hits: @@ -533,7 +592,10 @@ async def check_for_predecessor_crash() -> Optional[str]: journal_summary = " | ".join(crash_lines[-3:])[:200] # ── Quelle 3: restart_loop.json ───────────────────────────────── - restart_loop = _read_json(RESTART_LOOP_PATH) + # P-074a: Pfad dynamisch auflösen (primär + Fallback). Fehlt die Datei + # auf beiden Lagen, ist die Quelle 'nicht verfügbar' — kein Fehler. + restart_loop_path = _resolve_path(RESTART_LOOP_PATH, RESTART_LOOP_PATH_FALLBACK) + restart_loop = _read_json(restart_loop_path) if restart_loop_path else None restart_loop_crash = False if restart_loop: restart_loop_crash = _parse_restart_loop_recent(restart_loop) @@ -544,9 +606,16 @@ async def check_for_predecessor_crash() -> Optional[str]: "crash_forensics: restart_loop.json confirms crash — " "last_boot=%.0fs ago", time.time() - last_boot, ) + else: + logger.debug( + "crash_forensics: restart_loop.json not found at %s or %s — " + "source 3 unavailable", RESTART_LOOP_PATH, RESTART_LOOP_PATH_FALLBACK, + ) # ── Quelle 4: gateway_state.json ───────────────────────────────── - gateway_state = _read_json(GATEWAY_STATE_PATH) + # P-074a: Pfad dynamisch auflösen (primär + Fallback). + gateway_state_path = _resolve_path(GATEWAY_STATE_PATH, GATEWAY_STATE_PATH_FALLBACK) + gateway_state = _read_json(gateway_state_path) if gateway_state_path else None gateway_state_stale = False if gateway_state and heartbeat: updated_at = gateway_state.get("updated_at", 0) @@ -649,6 +718,19 @@ async def run_post_crash_forensic_test() -> str: counter_drift = reconstruct_counter_drift(fake_gateway_state) state_loss = _analyze_state_loss(fake_gateway_state) + # P-074b: SIGKILL-Szenario — reiner externer Kill ohne Traceback. + # Verifiziert, dass classify_crash_source() bei systemd 'status=9/KILL' + # eine Aussage liefert statt 'unknown'. + sigkill_journal = [ + "systemd[1]: hermes-gateway.service: Main process exited, code=killed, status=9/KILL", + "systemd[1]: hermes-gateway.service: Failed with result 'signal'.", + ] + sigkill_source = classify_crash_source(sigkill_journal) + logger.info( + "crash_forensics: P-074b SIGKILL classification check — " + "classify_crash_source(sigkill_journal)=%r", sigkill_source, + ) + report = format_crash_forensic_report( crash_source=crash_source, blast_radius=blast_radius,