fix(presence): P-085 main_agent Stale-Reset im GC-Block der Drift-Korrektur
main_agent ist ein Hook-Counter (on_pre_gateway_dispatch/on_post_llm_call). Wenn on_post_llm_call nicht zuverlässig feuert, läuft der Counter hoch und bleibt hängen → RED_STATIC obwohl der Agent idle ist. Analog zum chat_profile GC-Reset (P-041/P-041a), aber mit kürzerem Timeout (60s statt 1800s), weil main_agent der häufigste Counter ist. - main_agent in _counter_timestamps aufgenommen (Timestamp-Tracking) - MAIN_AGENT_GC_TIMEOUT_DEFAULT_S = 60.0 (Env: PRESENCE_MAIN_AGENT_GC_TIMEOUT_S) - Stale-Reset im GC-Block: pgrep==0 + Timestamp-Alter > 60s → main_agent=0 - P-039-Ausnahme (GC-Reset), P-063 (nur Reset, nie Inkrement) respektiert - 5 Unit-Tests (Reset, fresh-hook, pgrep-nonzero, timeout-default, F4-Regression)
This commit is contained in:
@@ -1485,6 +1485,11 @@ class PresenceEngine:
|
|||||||
# Default 1800s (30 Min) — ein Turn ohne Activity ist definitiv tot.
|
# Default 1800s (30 Min) — ein Turn ohne Activity ist definitiv tot.
|
||||||
# Überschreibbar via Env-Variable PRESENCE_GC_TIMEOUT_S.
|
# Überschreibbar via Env-Variable PRESENCE_GC_TIMEOUT_S.
|
||||||
GC_TIMEOUT_DEFAULT_S: float = 1800.0
|
GC_TIMEOUT_DEFAULT_S: float = 1800.0
|
||||||
|
# P-085: GC-Timeout für main_agent (Hook-Counter via pre_gateway_dispatch/
|
||||||
|
# post_llm_call). KÜRZER als chat_profile (60s statt 1800s), weil
|
||||||
|
# main_agent der häufigste Counter ist und stale-Zustände schnell
|
||||||
|
# erkannt werden müssen. Überschreibbar via PRESENCE_MAIN_AGENT_GC_TIMEOUT_S.
|
||||||
|
MAIN_AGENT_GC_TIMEOUT_DEFAULT_S: float = 60.0
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -1515,6 +1520,15 @@ class PresenceEngine:
|
|||||||
os.getenv("PRESENCE_GC_TIMEOUT_S", str(self.GC_TIMEOUT_DEFAULT_S))
|
os.getenv("PRESENCE_GC_TIMEOUT_S", str(self.GC_TIMEOUT_DEFAULT_S))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# P-085: main_agent GC-Timeout aus Env-Variable (Default 60s).
|
||||||
|
# Kürzer als chat_profile, weil main_agent der häufigste Counter ist.
|
||||||
|
self._main_agent_gc_timeout_s = float(
|
||||||
|
os.getenv(
|
||||||
|
"PRESENCE_MAIN_AGENT_GC_TIMEOUT_S",
|
||||||
|
str(self.MAIN_AGENT_GC_TIMEOUT_DEFAULT_S),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
# ── Counter-Dict: 5 Zähler (P-043: +manual_task) ──────────────────
|
# ── Counter-Dict: 5 Zähler (P-043: +manual_task) ──────────────────
|
||||||
self.counters: Dict[str, int] = {
|
self.counters: Dict[str, int] = {
|
||||||
"main_agent": 0,
|
"main_agent": 0,
|
||||||
@@ -1534,7 +1548,11 @@ class PresenceEngine:
|
|||||||
# Wird beim Inkrementieren auf time.time() gesetzt, beim
|
# Wird beim Inkrementieren auf time.time() gesetzt, beim
|
||||||
# Dekrementieren auf 0.0 zurückgesetzt. Dient als Guard für
|
# Dekrementieren auf 0.0 zurückgesetzt. Dient als Guard für
|
||||||
# den negativen GC-Check in _drift_correction().
|
# den negativen GC-Check in _drift_correction().
|
||||||
|
# P-085: main_agent ist ebenfalls ein Hook-Counter (via
|
||||||
|
# on_pre_gateway_dispatch/on_post_llm_call) und wird hier
|
||||||
|
# mitgetrackt, damit der Stale-Reset (60s) greifen kann.
|
||||||
self._counter_timestamps: Dict[str, float] = {
|
self._counter_timestamps: Dict[str, float] = {
|
||||||
|
"main_agent": 0.0,
|
||||||
"chat_profile": 0.0,
|
"chat_profile": 0.0,
|
||||||
"background_term": 0.0,
|
"background_term": 0.0,
|
||||||
}
|
}
|
||||||
@@ -1923,6 +1941,44 @@ class PresenceEngine:
|
|||||||
_gc_counter, _gc_value, _gc_age, self._gc_timeout_s,
|
_gc_counter, _gc_value, _gc_age, self._gc_timeout_s,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# ── P-085: main_agent Stale-Reset ────────────────────────────
|
||||||
|
# main_agent ist ein Hook-Counter (via on_pre_gateway_dispatch/
|
||||||
|
# on_post_llm_call). Wenn on_post_llm_call nicht zuverlässig
|
||||||
|
# feuert, läuft der Counter hoch und bleibt hängen → RED_STATIC
|
||||||
|
# obwohl der Agent idle ist.
|
||||||
|
#
|
||||||
|
# P-039: Hook-Counter nie via pgrep resetten — ABER der GC-Reset
|
||||||
|
# ist die dokumentierte Ausnahme (pgrep-leer + timeout). Analog
|
||||||
|
# zum chat_profile GC-Reset (P-041/P-041a), aber mit KÜRZEREM
|
||||||
|
# Timeout (60s statt 1800s), weil main_agent der häufigste
|
||||||
|
# Counter ist und stale-Zustände schnell erkannt werden müssen.
|
||||||
|
#
|
||||||
|
# Guard: NUR resetten wenn pgrep==0 (kein aktiver Hauptagent-
|
||||||
|
# Prozess) UND Timestamp-Alter > 60s (nicht bei frischen Hooks).
|
||||||
|
# P-063: NUR Reset, niemals Auto-Inkrement.
|
||||||
|
_ma_value = self.counters.get("main_agent", 0)
|
||||||
|
if _ma_value > 0 and pgrep_count == 0:
|
||||||
|
_ma_ts = self._counter_timestamps.get("main_agent", 0.0)
|
||||||
|
if _ma_ts > 0.0:
|
||||||
|
_ma_age = time.time() - _ma_ts
|
||||||
|
if _ma_age > self._main_agent_gc_timeout_s:
|
||||||
|
logger.warning(
|
||||||
|
"[presence-v10] Drift-Korrektur: GC-Reset für main_agent — "
|
||||||
|
"Counter=%d, Timestamp-Alter=%.1fs (>%.0fs), "
|
||||||
|
"Grund=pgrep-leer+timeout (P-085)",
|
||||||
|
_ma_value, _ma_age, self._main_agent_gc_timeout_s,
|
||||||
|
)
|
||||||
|
self.counters["main_agent"] = 0
|
||||||
|
self._counter_timestamps["main_agent"] = 0.0
|
||||||
|
self._telemetry_drift_corrections += 1
|
||||||
|
else:
|
||||||
|
logger.debug(
|
||||||
|
"[presence-v10] Drift-Korrektur: GC-Skip für main_agent — "
|
||||||
|
"Counter=%d, Timestamp-Alter=%.1fs (<%.0fs) — "
|
||||||
|
"Race-Schutz aktiv (P-085)",
|
||||||
|
_ma_value, _ma_age, self._main_agent_gc_timeout_s,
|
||||||
|
)
|
||||||
|
|
||||||
# ── P-043: GC für manuelle Tasks (> 60 Min) ──────────────────
|
# ── P-043: GC für manuelle Tasks (> 60 Min) ──────────────────
|
||||||
_MANUAL_TASK_GC_TIMEOUT_S = 3600.0 # 60 Minuten
|
_MANUAL_TASK_GC_TIMEOUT_S = 3600.0 # 60 Minuten
|
||||||
_expired_tasks = []
|
_expired_tasks = []
|
||||||
|
|||||||
@@ -991,3 +991,79 @@ class TestMultipleSubagents:
|
|||||||
calls_after_stops = rest._request.call_count
|
calls_after_stops = rest._request.call_count
|
||||||
# Maximal 2 REST-Calls: P1→P3 + P3→P1
|
# Maximal 2 REST-Calls: P1→P3 + P3→P1
|
||||||
assert calls_after_stops <= calls_after_starts + 2
|
assert calls_after_stops <= calls_after_starts + 2
|
||||||
|
|
||||||
|
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════
|
||||||
|
# P-085: main_agent Stale-Reset Tests
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
class TestMainAgentStaleReset:
|
||||||
|
"""P-085: main_agent Hook-Counter wird bei pgrep==0 + Timeout resettet.
|
||||||
|
|
||||||
|
Analog zum chat_profile GC-Reset (P-041/P-041a), aber mit kürzerem
|
||||||
|
Timeout (60s statt 1800s). Guard: NUR resetten wenn pgrep==0 UND
|
||||||
|
Timestamp-Alter > 60s. P-063: NUR Reset, niemals Auto-Inkrement.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def _make_stale_engine(self, main_agent_value=24, age_s=120.0):
|
||||||
|
"""Erzeuge Engine mit stale main_agent (Timestamp in der Vergangenheit)."""
|
||||||
|
engine = _make_engine()
|
||||||
|
# main_agent via Hook setzen (setzt Timestamp auf time.time())
|
||||||
|
for _ in range(main_agent_value):
|
||||||
|
engine.on_pre_gateway_dispatch()
|
||||||
|
# Timestamp künstlich altern lassen (stale simulieren)
|
||||||
|
engine._counter_timestamps["main_agent"] = time.time() - age_s
|
||||||
|
return engine
|
||||||
|
|
||||||
|
def test_main_agent_stale_reset_when_pgrep_zero_and_timeout(self):
|
||||||
|
"""pgrep==0 + Timestamp-Alter > 60s → main_agent auf 0, Phase GREEN_STATIC."""
|
||||||
|
engine = self._make_stale_engine(main_agent_value=24, age_s=120.0)
|
||||||
|
# pgrep==0 (kein aktiver Hauptagent-Prozess) + Taskliste leer
|
||||||
|
with patch.object(engine, "_count_delegated_subagents", return_value=0), \
|
||||||
|
patch.object(engine, "_count_active_tasks_from_tracker", return_value=0):
|
||||||
|
engine._drift_correction()
|
||||||
|
|
||||||
|
assert engine.counters["main_agent"] == 0
|
||||||
|
assert engine._counter_timestamps["main_agent"] == 0.0
|
||||||
|
# Phase fällt auf GREEN_STATIC (main_agent=0, delegate_subagent=0)
|
||||||
|
assert engine.get_current_phase().name == "GREEN_STATIC"
|
||||||
|
|
||||||
|
def test_main_agent_not_reset_when_fresh_hook(self):
|
||||||
|
"""Frischer Hook (Timestamp-Alter < 60s) → kein Reset."""
|
||||||
|
engine = self._make_stale_engine(main_agent_value=1, age_s=5.0)
|
||||||
|
with patch.object(engine, "_count_delegated_subagents", return_value=0), \
|
||||||
|
patch.object(engine, "_count_active_tasks_from_tracker", return_value=0):
|
||||||
|
engine._drift_correction()
|
||||||
|
|
||||||
|
# Frischer Hook: main_agent bleibt unverändert
|
||||||
|
assert engine.counters["main_agent"] == 1
|
||||||
|
|
||||||
|
def test_main_agent_not_reset_when_pgrep_nonzero(self):
|
||||||
|
"""pgrep > 0 (aktiver Hauptagent-Prozess) → kein Reset trotz Timeout."""
|
||||||
|
engine = self._make_stale_engine(main_agent_value=24, age_s=120.0)
|
||||||
|
with patch.object(engine, "_count_delegated_subagents", return_value=1), \
|
||||||
|
patch.object(engine, "_count_active_tasks_from_tracker", return_value=0):
|
||||||
|
engine._drift_correction()
|
||||||
|
|
||||||
|
# pgrep findet Prozess → main_agent bleibt (kein Reset)
|
||||||
|
assert engine.counters["main_agent"] == 24
|
||||||
|
|
||||||
|
def test_main_agent_gc_timeout_default_is_60s(self):
|
||||||
|
"""MAIN_AGENT_GC_TIMEOUT_DEFAULT_S == 60.0 (kürzer als chat_profile 1800s)."""
|
||||||
|
from plugin.archivar.presence_engine import PresenceEngine
|
||||||
|
assert PresenceEngine.MAIN_AGENT_GC_TIMEOUT_DEFAULT_S == 60.0
|
||||||
|
assert PresenceEngine.MAIN_AGENT_GC_TIMEOUT_DEFAULT_S < PresenceEngine.GC_TIMEOUT_DEFAULT_S
|
||||||
|
|
||||||
|
def test_chat_profile_gc_reset_still_works(self):
|
||||||
|
"""F4: chat_profile GC-Reset (P-041/P-041a) darf nicht brechen."""
|
||||||
|
engine = _make_engine()
|
||||||
|
engine.on_session_start() # chat_profile=1, Timestamp=now
|
||||||
|
# chat_profile-Timestamp altern lassen (über 1800s Default)
|
||||||
|
engine._counter_timestamps["chat_profile"] = time.time() - 2000.0
|
||||||
|
with patch.object(engine, "_count_delegated_subagents", return_value=0), \
|
||||||
|
patch.object(engine, "_count_active_tasks_from_tracker", return_value=0):
|
||||||
|
engine._drift_correction()
|
||||||
|
|
||||||
|
assert engine.counters["chat_profile"] == 0
|
||||||
|
assert engine._counter_timestamps["chat_profile"] == 0.0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user