feat(p1-2): DetectionTimer erkennt Plugin-State-Datei (background_tasks > 0 → active)
This commit is contained in:
@@ -1054,8 +1054,9 @@ class DetectionTimer:
|
||||
"""Detects whether background agents are active.
|
||||
|
||||
P1b: gateway_state.json is PRIMARY — active_agents > 1 means active.
|
||||
pgrep is SECONDARY/CONFIRM — used as fallback when gateway_state is
|
||||
unavailable or returns 0/1 (ambiguous).
|
||||
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).
|
||||
|
||||
pgrep pattern: 'hermes_cli|hermes chat' (catches subagents AND gateway
|
||||
subprocesses). Self-pid exclude is preserved.
|
||||
@@ -1068,6 +1069,7 @@ class DetectionTimer:
|
||||
gateway_state_path: str = "",
|
||||
detection_interval: float = 10.0,
|
||||
on_change: Optional[Callable[[bool], None]] = None,
|
||||
plugin_state_path: str = "",
|
||||
):
|
||||
"""Initialize DetectionTimer.
|
||||
|
||||
@@ -1075,6 +1077,8 @@ class DetectionTimer:
|
||||
gateway_state_path: Path to gateway_state.json.
|
||||
detection_interval: Check interval in seconds (default 10).
|
||||
on_change: Callback(is_active) when detection state changes.
|
||||
plugin_state_path: P1-2: Path to plugin state file. Defaults to
|
||||
~/.hermes/plugin_state/hermes_rocket_plugin.json.
|
||||
"""
|
||||
if not gateway_state_path:
|
||||
gateway_state_path = os.environ.get(
|
||||
@@ -1082,6 +1086,12 @@ class DetectionTimer:
|
||||
os.path.expanduser("~/.hermes/gateway_state.json"),
|
||||
)
|
||||
self._gateway_state_path = gateway_state_path
|
||||
|
||||
# P1-2: Plugin-State-Datei für Gateway-interne Subagenten
|
||||
if not plugin_state_path:
|
||||
plugin_state_path = _get_plugin_state_path()
|
||||
self._plugin_state_path = plugin_state_path
|
||||
|
||||
self._detection_interval = detection_interval
|
||||
self._on_change = on_change
|
||||
self._thread: Optional[threading.Thread] = None
|
||||
@@ -1118,8 +1128,9 @@ class DetectionTimer:
|
||||
"""Check if background agents are active.
|
||||
|
||||
P1b: gateway_state.json is PRIMARY — active_agents > 1 means active.
|
||||
pgrep is SECONDARY/CONFIRM — used as fallback when gateway_state is
|
||||
unavailable or returns 0/1 (ambiguous).
|
||||
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).
|
||||
|
||||
pgrep pattern expanded from 'hermes chat --profile' to
|
||||
'hermes_cli|hermes chat' to catch subagents AND gateway subprocesses.
|
||||
@@ -1135,6 +1146,9 @@ class DetectionTimer:
|
||||
pgrep_pids = -1 # -1 = not yet run, >=0 = count, stays -1 on error
|
||||
gateway_active = 0
|
||||
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:
|
||||
@@ -1145,12 +1159,22 @@ class DetectionTimer:
|
||||
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 pgrep
|
||||
# Fall through to plugin_state
|
||||
|
||||
# ── SECONDARY: pgrep — run when gateway_state didn't decide ───────
|
||||
# ── P1-2: SECONDARY: plugin_state.json ─────────────────────────────
|
||||
if 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:
|
||||
result = True
|
||||
plugin_used = True
|
||||
detection_source = "plugin_state"
|
||||
|
||||
# ── TERTIARY: pgrep — run when neither gateway nor plugin decided ──
|
||||
if not gateway_used and not plugin_used:
|
||||
try:
|
||||
own_pid = os.getpid()
|
||||
# P1b: Expanded pattern catches subagents AND gateway subprocesses
|
||||
@@ -1166,20 +1190,31 @@ class DetectionTimer:
|
||||
pids = [p for p in all_pids if p != own_pid]
|
||||
pgrep_pids = len(pids)
|
||||
result = pgrep_pids > 0
|
||||
if result:
|
||||
detection_source = "pgrep"
|
||||
except (subprocess.TimeoutExpired, FileNotFoundError, OSError, ValueError) as e:
|
||||
logger.debug("[detection] pgrep failed: %s", e)
|
||||
# pgrep_pids stays -1 (error sentinel)
|
||||
|
||||
# ── Log: both signals, result ─────────────────────────────────────
|
||||
# ── Log: all signals, result, source ───────────────────────────────
|
||||
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), pgrep=%s, result=%s",
|
||||
"[detection] check #%d: gateway_state=%d (primary=%s), "
|
||||
"plugin_state=%s (bg_tasks=%s), pgrep=%s, "
|
||||
"result=%s, source=%s",
|
||||
self._check_count, gateway_active,
|
||||
"yes" if gateway_used else "no",
|
||||
"yes" if plugin_used else "no",
|
||||
plugin_str,
|
||||
pgrep_str,
|
||||
"active" if result else "inactive",
|
||||
detection_source,
|
||||
)
|
||||
|
||||
# P1-2: Set telemetry source
|
||||
self._set_detection_source_telemetry(detection_source)
|
||||
|
||||
# Notify on change (P1-2: callback fires from check() itself)
|
||||
if result != self._last_result and self._on_change is not None:
|
||||
try:
|
||||
@@ -1227,3 +1262,15 @@ class DetectionTimer:
|
||||
routing_telemetry.heartbeat_detection += 1
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _set_detection_source_telemetry(self, source: str) -> None:
|
||||
"""P1-2: Set the heartbeat_detection_source telemetry field.
|
||||
|
||||
Args:
|
||||
source: One of 'gateway', 'plugin_state', 'pgrep', 'none'.
|
||||
"""
|
||||
try:
|
||||
from archivar.routing_telemetry import routing_telemetry
|
||||
routing_telemetry.heartbeat_detection_source = source
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user