P2 Fix: Presence-Engine Detection — Option D (PID-Sentinel + Semantik-Korrektur)
Root Cause: Gateway erkannte sich selbst nicht als aktiv im Idle-Zustand. - gateway_state.json: active_agents=0 im Idle, Schwellwert >1 griff nie - plugin_state.json: background_tasks=0 im Idle → inactive - pgrep: Pattern 'hermes_cli|hermes chat' matchte 'gateway run' nicht Fix (Rainer approved, Option D): 1. PID-Sentinel (neues PRIMARY Signal) - _check_gateway_pid_alive() prüft /proc/<ppid> auf Existenz - detection_source='gateway_pid' wenn PID-Sentinel entscheidet 2. gateway_state.json Schwelle korrigiert - >1 → >0 (active_agents=1 reicht jetzt) 3. pgrep-Pattern erweitert - 'hermes_cli|hermes chat' → 'hermes_cli|hermes chat|gateway run' 4. Semantik: Idle ≠ Abwesend - PID-Sentinel alive + keine Subagenten → Status 'online' (grün) - 'inactive' (gelb) nur wenn Gateway-Prozess nicht gefunden Tests: 94 passed (86 existing + 8 new T1-T8) - T1: PID-Sentinel erkennt lebenden Gateway - T2: PID-Sentinel erkennt toten Gateway - T3: gateway_state Schwelle >0 - T4: pgrep-Pattern matcht 'gateway run' - T5: Idle-Puls läuft weiter wenn Gateway alive (zentraler Test) - T6: Subagent-Aktivität pulsiert gelb - T7: Kein False-Positive wenn Gateway tot - T8: detection_source='gateway_pid'
This commit is contained in:
@@ -1061,13 +1061,17 @@ class PulseController:
|
||||
class DetectionTimer:
|
||||
"""Detects whether background agents are active.
|
||||
|
||||
P1b: gateway_state.json is PRIMARY — active_agents > 1 means active.
|
||||
P1-2: plugin_state.json is SECONDARY — background_tasks > 0 means active.
|
||||
pgrep is TERTIARY/CONFIRM — used as fallback when gateway_state and
|
||||
plugin_state are unavailable or return 0/1 (ambiguous).
|
||||
P2 Fix Option D: PID-Sentinel ist PRIMARY — prüft ob Gateway-Prozess lebt.
|
||||
gateway_state.json ist SECONDARY — active_agents > 0 bedeutet Subagenten aktiv.
|
||||
plugin_state.json ist TERTIARY — background_tasks > 0 bedeutet aktiv.
|
||||
pgrep ist QUATERNARY/FALLBACK — 'hermes_cli|hermes chat|gateway run'.
|
||||
|
||||
pgrep pattern: 'hermes_cli|hermes chat' (catches subagents AND gateway
|
||||
subprocesses). Self-pid exclude is preserved.
|
||||
pgrep pattern: 'hermes_cli|hermes chat|gateway run' (fängt Subagenten
|
||||
UND Gateway-Prozess). Self-pid exclude bleibt erhalten.
|
||||
|
||||
Semantik: Idle ≠ Abwesend. Wenn PID-Sentinel 'alive' liefert UND keine
|
||||
Subagenten aktiv → Status 'online' (grün), nicht 'inactive'.
|
||||
'inactive' (gelb) bedeutet NUR: Gateway-Prozess nicht gefunden.
|
||||
|
||||
Calls on_change callback when detection state changes.
|
||||
"""
|
||||
@@ -1132,17 +1136,37 @@ class DetectionTimer:
|
||||
self._thread = None
|
||||
logger.info("[detection] DetectionTimer stopped")
|
||||
|
||||
def _check_gateway_pid_alive(self) -> bool:
|
||||
"""P2 Fix Option D: PID-Sentinel — prüft ob der Gateway-Prozess lebt.
|
||||
|
||||
Verwendet os.getppid() um den Parent-PID (Gateway-Prozess) zu ermitteln
|
||||
und prüft ob /proc/<ppid> existiert. Dies ist das zuverlässigste Signal
|
||||
für die Gateway-Präsenz, da es keine Datei-I/O oder Prozess-Suche benötigt.
|
||||
|
||||
Returns:
|
||||
True wenn der Gateway-Parent-Prozess existiert, False sonst.
|
||||
"""
|
||||
try:
|
||||
ppid = os.getppid()
|
||||
# /proc/<ppid> existiert nur wenn der Prozess lebt
|
||||
return os.path.exists(f"/proc/{ppid}")
|
||||
except (OSError, ValueError):
|
||||
return False
|
||||
|
||||
def check(self) -> bool:
|
||||
"""Check if background agents are active.
|
||||
|
||||
P1b: gateway_state.json is PRIMARY — active_agents > 1 means active.
|
||||
P1-2: plugin_state.json is SECONDARY — background_tasks > 0 means active.
|
||||
pgrep is TERTIARY/CONFIRM — used as fallback when gateway_state and
|
||||
plugin_state are unavailable or return 0/1 (ambiguous).
|
||||
P2 Fix Option D: PID-Sentinel ist PRIMARY — prüft ob Gateway-Prozess lebt.
|
||||
gateway_state.json ist SECONDARY — active_agents > 0 bedeutet Subagenten aktiv.
|
||||
plugin_state.json ist TERTIARY — background_tasks > 0 bedeutet aktiv.
|
||||
pgrep ist QUATERNARY/FALLBACK — 'hermes_cli|hermes chat|gateway run'.
|
||||
|
||||
pgrep pattern expanded from 'hermes chat --profile' to
|
||||
'hermes_cli|hermes chat' to catch subagents AND gateway subprocesses.
|
||||
Self-pid exclude is preserved.
|
||||
pgrep pattern: 'hermes_cli|hermes chat|gateway run' (fängt Subagenten
|
||||
UND Gateway-Prozess). Self-pid exclude bleibt erhalten.
|
||||
|
||||
Semantik: Idle ≠ Abwesend. Wenn PID-Sentinel 'alive' liefert UND keine
|
||||
Subagenten aktiv → Status 'online' (grün), nicht 'inactive'.
|
||||
'inactive' (gelb) bedeutet NUR: Gateway-Prozess nicht gefunden.
|
||||
|
||||
Returns:
|
||||
True if background agents are detected, False otherwise.
|
||||
@@ -1153,27 +1177,36 @@ class DetectionTimer:
|
||||
result = False
|
||||
pgrep_pids = -1 # -1 = not yet run, >=0 = count, stays -1 on error
|
||||
gateway_active = 0
|
||||
pid_used = False # P2: True if PID-Sentinel was the deciding signal
|
||||
gateway_used = False # True if gateway_state was the deciding signal
|
||||
plugin_used = False # P1-2: True if plugin_state was the deciding signal
|
||||
plugin_bg_tasks = -1 # P1-2: -1 = not read, >=0 = count
|
||||
detection_source = "none" # P1-2: which signal decided
|
||||
|
||||
# ── PRIMARY: gateway_state.json ────────────────────────────────────
|
||||
try:
|
||||
if os.path.exists(self._gateway_state_path):
|
||||
with open(self._gateway_state_path, 'r') as f:
|
||||
state = json.load(f)
|
||||
gateway_active = state.get("active_agents", 0)
|
||||
if gateway_active > 1:
|
||||
result = True
|
||||
gateway_used = True
|
||||
detection_source = "gateway"
|
||||
except (json.JSONDecodeError, IOError, OSError) as e:
|
||||
logger.debug("[detection] gateway_state.json read failed: %s", e)
|
||||
# Fall through to plugin_state
|
||||
# ── P2 PRIMARY: PID-Sentinel — prüft ob Gateway-Prozess lebt ────────
|
||||
if self._check_gateway_pid_alive():
|
||||
result = True
|
||||
pid_used = True
|
||||
detection_source = "gateway_pid"
|
||||
|
||||
# ── P1-2: SECONDARY: plugin_state.json ─────────────────────────────
|
||||
if not gateway_used:
|
||||
# ── P2 SECONDARY: gateway_state.json (Schwelle >0, war >1) ──────────
|
||||
if not pid_used:
|
||||
try:
|
||||
if os.path.exists(self._gateway_state_path):
|
||||
with open(self._gateway_state_path, 'r') as f:
|
||||
state = json.load(f)
|
||||
gateway_active = state.get("active_agents", 0)
|
||||
# P2 Fix: Schwelle >0 statt >1
|
||||
if gateway_active > 0:
|
||||
result = True
|
||||
gateway_used = True
|
||||
detection_source = "gateway"
|
||||
except (json.JSONDecodeError, IOError, OSError) as e:
|
||||
logger.debug("[detection] gateway_state.json read failed: %s", e)
|
||||
# Fall through to plugin_state
|
||||
|
||||
# ── P1-2: TERTIARY: plugin_state.json ────────────────────────────────
|
||||
if not pid_used and not gateway_used:
|
||||
plugin_state = _read_plugin_state(self._plugin_state_path)
|
||||
plugin_bg_tasks = plugin_state.get("background_tasks", 0)
|
||||
if plugin_bg_tasks > 0:
|
||||
@@ -1181,13 +1214,13 @@ class DetectionTimer:
|
||||
plugin_used = True
|
||||
detection_source = "plugin_state"
|
||||
|
||||
# ── TERTIARY: pgrep — run when neither gateway nor plugin decided ──
|
||||
if not gateway_used and not plugin_used:
|
||||
# ── P2 QUATERNARY: pgrep — erweitertes Pattern 'gateway run' ────────
|
||||
if not pid_used and not gateway_used and not plugin_used:
|
||||
try:
|
||||
own_pid = os.getpid()
|
||||
# P1b: Expanded pattern catches subagents AND gateway subprocesses
|
||||
# P2 Fix: Pattern um 'gateway run' erweitert
|
||||
proc = subprocess.run(
|
||||
["pgrep", "-f", "hermes_cli|hermes chat"],
|
||||
["pgrep", "-f", "hermes_cli|hermes chat|gateway run"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=5,
|
||||
@@ -1208,10 +1241,12 @@ class DetectionTimer:
|
||||
pgrep_str = str(pgrep_pids) if pgrep_pids >= 0 else "error"
|
||||
plugin_str = str(plugin_bg_tasks) if plugin_bg_tasks >= 0 else "n/a"
|
||||
logger.info(
|
||||
"[detection] check #%d: gateway_state=%d (primary=%s), "
|
||||
"[detection] check #%d: pid_sentinel=%s, gateway_state=%d (primary=%s), "
|
||||
"plugin_state=%s (bg_tasks=%s), pgrep=%s, "
|
||||
"result=%s, source=%s",
|
||||
self._check_count, gateway_active,
|
||||
self._check_count,
|
||||
"alive" if pid_used else "dead",
|
||||
gateway_active,
|
||||
"yes" if gateway_used else "no",
|
||||
"yes" if plugin_used else "no",
|
||||
plugin_str,
|
||||
|
||||
Reference in New Issue
Block a user