fix: DetectionTimer pgrep als Primärsignal — gateway_state unbrauchbar fuer Subagenten-Erkennung
This commit is contained in:
@@ -704,66 +704,55 @@ class DetectionTimer:
|
||||
def check(self) -> bool:
|
||||
"""Check if background agents are active.
|
||||
|
||||
pgrep is the PRIMARY signal — always executed, regardless of gateway_state.
|
||||
gateway_state is SECONDARY — read only for diagnostic logging, never for decisions.
|
||||
|
||||
Returns:
|
||||
True if active_agents > 1 (background agent active),
|
||||
True if pgrep finds other hermes chat processes (self-excluded),
|
||||
False otherwise.
|
||||
"""
|
||||
self._check_count += 1
|
||||
self._increment_telemetry()
|
||||
|
||||
result = False
|
||||
source = "none"
|
||||
pgrep_pids = -1 # -1 = not yet run, >=0 = count, stays -1 on error
|
||||
|
||||
# Try gateway_state.json first
|
||||
# ── PRIMARY: pgrep — ALWAYS run ──────────────────────────────────
|
||||
try:
|
||||
own_pid = os.getpid()
|
||||
proc = subprocess.run(
|
||||
["pgrep", "-f", "hermes chat --profile"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=5,
|
||||
)
|
||||
# Parse PIDs and self-exclude
|
||||
raw_lines = [l.strip() for l in proc.stdout.strip().split("\n") if l.strip()]
|
||||
all_pids = [int(l) for l in raw_lines if l.isdigit()]
|
||||
pids = [p for p in all_pids if p != own_pid]
|
||||
pgrep_pids = len(pids)
|
||||
result = pgrep_pids > 0
|
||||
except (subprocess.TimeoutExpired, FileNotFoundError, OSError, ValueError) as e:
|
||||
logger.debug("[detection] pgrep failed: %s", e)
|
||||
# pgrep_pids stays -1 (error sentinel)
|
||||
|
||||
# ── SECONDARY: gateway_state — diagnostic only ────────────────────
|
||||
gateway_active = 0
|
||||
try:
|
||||
if os.path.exists(self._gateway_state_path):
|
||||
with open(self._gateway_state_path, 'r') as f:
|
||||
state = json.load(f)
|
||||
active = state.get("active_agents", 0)
|
||||
result = active > 1
|
||||
source = "gateway_state"
|
||||
logger.info(
|
||||
"[detection] check #%d: gateway_state=%d, result=%s",
|
||||
self._check_count, active, "active" if result else "inactive",
|
||||
)
|
||||
gateway_active = state.get("active_agents", 0)
|
||||
except (json.JSONDecodeError, IOError, OSError) as e:
|
||||
logger.debug("[detection] gateway_state.json read failed: %s", e)
|
||||
|
||||
# Fallback: pgrep (only if gateway_state didn't produce a result)
|
||||
if source == "none":
|
||||
try:
|
||||
own_pid = os.getpid()
|
||||
proc = subprocess.run(
|
||||
["pgrep", "-f", "hermes chat --profile"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=5,
|
||||
)
|
||||
# Parse PIDs and self-exclude
|
||||
raw_lines = [l.strip() for l in proc.stdout.strip().split("\n") if l.strip()]
|
||||
all_pids = [int(l) for l in raw_lines if l.isdigit()]
|
||||
pids = [p for p in all_pids if p != own_pid]
|
||||
result = len(pids) > 0
|
||||
source = "pgrep"
|
||||
logger.info(
|
||||
"[detection] check #%d: pgrep=%d pids (self-excluded), result=%s",
|
||||
self._check_count, len(pids), "active" if result else "inactive",
|
||||
)
|
||||
except (subprocess.TimeoutExpired, FileNotFoundError, OSError, ValueError) as e:
|
||||
logger.debug("[detection] pgrep fallback failed: %s", e)
|
||||
result = False
|
||||
source = "pgrep_error"
|
||||
logger.info(
|
||||
"[detection] check #%d: detection unavailable (pgrep failed)",
|
||||
self._check_count,
|
||||
)
|
||||
|
||||
# If no source produced a result, log inactive
|
||||
if source == "none":
|
||||
logger.info(
|
||||
"[detection] check #%d: no background activity detected",
|
||||
self._check_count,
|
||||
)
|
||||
# ── Log: both signals, result ─────────────────────────────────────
|
||||
pgrep_str = str(pgrep_pids) if pgrep_pids >= 0 else "error"
|
||||
logger.info(
|
||||
"[detection] check #%d: pgrep=%s, gateway_state=%d, result=%s",
|
||||
self._check_count, pgrep_str, gateway_active,
|
||||
"active" if result else "inactive",
|
||||
)
|
||||
|
||||
# Notify on change (P1-2: callback fires from check() itself)
|
||||
if result != self._last_result and self._on_change is not None:
|
||||
|
||||
Reference in New Issue
Block a user