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.
|
||||
# Überschreibbar via Env-Variable PRESENCE_GC_TIMEOUT_S.
|
||||
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__(
|
||||
self,
|
||||
@@ -1515,6 +1520,15 @@ class PresenceEngine:
|
||||
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) ──────────────────
|
||||
self.counters: Dict[str, int] = {
|
||||
"main_agent": 0,
|
||||
@@ -1534,7 +1548,11 @@ class PresenceEngine:
|
||||
# Wird beim Inkrementieren auf time.time() gesetzt, beim
|
||||
# Dekrementieren auf 0.0 zurückgesetzt. Dient als Guard für
|
||||
# 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] = {
|
||||
"main_agent": 0.0,
|
||||
"chat_profile": 0.0,
|
||||
"background_term": 0.0,
|
||||
}
|
||||
@@ -1923,6 +1941,44 @@ class PresenceEngine:
|
||||
_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) ──────────────────
|
||||
_MANUAL_TASK_GC_TIMEOUT_S = 3600.0 # 60 Minuten
|
||||
_expired_tasks = []
|
||||
|
||||
Reference in New Issue
Block a user