fix(presence): P-093 chat_profile stale GC-Reset mit 60s-Timeout
chat_profile Counter blieb nach Turn-Ende hängen (on_session_finalize feuert nicht zuverlässig) → Engine pulsierte endlos in GREEN_PULSE. Der alte GC-Timeout (1800s, P-041a) war zu lang: Counter blieb bis zu 50 Min auf 1. Analog zu P-089 (background_term) bekommt chat_profile jetzt einen Stale-Reset: Counter > 0 + pgrep==0 + (kein Timestamp ODER Timestamp > STALE_RESET_TIMEOUT_S=60s) → Reset auf 0. main_agent bleibt ausgeschlossen (Rainers Veto — eigener Block mit eigenem Timeout). P-063-konform: nur Reset, kein Auto-Inkrement.
This commit is contained in:
@@ -1543,6 +1543,12 @@ class PresenceEngine:
|
||||
# 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
|
||||
# P-093: Stale-Reset-Timeout für chat_profile + background_term.
|
||||
# Kürzer als der alte 1800s-GC-Timeout (P-041a): ein Hook-Counter,
|
||||
# dessen Timestamp > 60s alt ist und bei dem pgrep==0 gilt, ist
|
||||
# definitiv stale — on_session_finalize / on_background_process_stop
|
||||
# hat nicht zuverlässig gefeuert. Analog zu P-089 (kein Timestamp).
|
||||
STALE_RESET_TIMEOUT_S: float = 60.0
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -1954,19 +1960,23 @@ class PresenceEngine:
|
||||
delegate_counter, real_delegated, pgrep_count,
|
||||
)
|
||||
|
||||
# ── P-041 / P-041a: GC für Hook-gesetzte Counter ──────────────
|
||||
# ── P-041 / P-041a / P-093: GC für Hook-gesetzte Counter ──────
|
||||
# chat_profile + background_term werden via Hooks gesetzt und
|
||||
# sind NICHT pgrep-detektierbar. Wenn der Hook complete() nicht
|
||||
# feuert (Crash, SIGTERM), bleibt der Counter hängen.
|
||||
# GC-Check: Counter > 0 + Timestamp > GC-Timeout → Reset auf 0.
|
||||
# feuert (Crash, SIGTERM, on_session_finalize unzuverlässig),
|
||||
# bleibt der Counter hängen.
|
||||
# GC-Check: Counter > 0 + pgrep==0 + (kein Timestamp ODER
|
||||
# Timestamp > STALE_RESET_TIMEOUT_S) → Reset auf 0.
|
||||
#
|
||||
# P-041a: Der Timeout ist jetzt variable (self._gc_timeout_s),
|
||||
# Default 1800s (30 Min) via PRESENCE_GC_TIMEOUT_S Env-Variable.
|
||||
# Der Progress-Refresh (refresh_chat_profile_timestamp()) setzt
|
||||
# den Timestamp bei jeder '⏳ Working'-Meldung neu → GC-Timer
|
||||
# startet neu. Der 1800s-Cap ist die Notbremse für echte Crashes.
|
||||
# P-093: STALE_RESET_TIMEOUT_S = 60s (statt 1800s). Ein
|
||||
# Hook-Counter, dessen Timestamp > 60s alt ist und bei dem
|
||||
# pgrep==0 gilt, ist definitiv stale — on_session_finalize /
|
||||
# on_background_process_stop hat nicht zuverlässig gefeuert.
|
||||
# Der 1800s-Cap (P-041a) war zu lang: der Counter blieb bis zu
|
||||
# 50 Min hängen und die Engine pulsierte endlos in GREEN_PULSE.
|
||||
# P-089: Kein Timestamp = ebenfalls stale (analog).
|
||||
# P-039: chat_profile + background_term via Hooks nie via pgrep
|
||||
# resetten — nur via GC (dieser Block).
|
||||
# resetten — nur via GC (dieser Block, mit pgrep==0 Guard).
|
||||
for _gc_counter in ("chat_profile", "background_term"):
|
||||
_gc_value = self.counters.get(_gc_counter, 0)
|
||||
if _gc_value <= 0:
|
||||
@@ -1999,22 +2009,37 @@ class PresenceEngine:
|
||||
)
|
||||
continue
|
||||
_gc_age = time.time() - _gc_ts
|
||||
if _gc_age > self._gc_timeout_s:
|
||||
logger.warning(
|
||||
"[presence-v10] Drift-Korrektur: GC-Reset für %s — "
|
||||
"Counter=%d, Timestamp-Alter=%.1fs (>%.0fs), "
|
||||
"Grund=pgrep-leer+timeout (P-041/P-041a)",
|
||||
_gc_counter, _gc_value, _gc_age, self._gc_timeout_s,
|
||||
)
|
||||
self.counters[_gc_counter] = 0
|
||||
self._counter_timestamps[_gc_counter] = 0.0
|
||||
self._telemetry_drift_corrections += 1
|
||||
if _gc_age > self.STALE_RESET_TIMEOUT_S:
|
||||
# P-093: Kürzerer Stale-Timeout (60s statt 1800s).
|
||||
# on_session_finalize / on_background_process_stop feuert
|
||||
# nicht zuverlässig → Counter bleibt hängen. Timestamp
|
||||
# > 60s + pgrep==0 = definitiv stale.
|
||||
if pgrep_count == 0:
|
||||
logger.warning(
|
||||
"[presence-v10] Drift-Korrektur: GC-Reset für %s — "
|
||||
"Counter=%d, Timestamp-Alter=%.1fs (>%.0fs), "
|
||||
"pgrep=0 → stale (P-093)",
|
||||
_gc_counter, _gc_value, _gc_age,
|
||||
self.STALE_RESET_TIMEOUT_S,
|
||||
)
|
||||
self.counters[_gc_counter] = 0
|
||||
self._counter_timestamps[_gc_counter] = 0.0
|
||||
self._telemetry_drift_corrections += 1
|
||||
else:
|
||||
logger.debug(
|
||||
"[presence-v10] Drift-Korrektur: GC-Skip für %s — "
|
||||
"Counter=%d, Timestamp-Alter=%.1fs (>%.0fs), aber "
|
||||
"pgrep=%d → Race-Schutz aktiv (P-093)",
|
||||
_gc_counter, _gc_value, _gc_age,
|
||||
self.STALE_RESET_TIMEOUT_S, pgrep_count,
|
||||
)
|
||||
else:
|
||||
logger.debug(
|
||||
"[presence-v10] Drift-Korrektur: GC-Skip für %s — "
|
||||
"Counter=%d, Timestamp-Alter=%.1fs (<%.0fs) — "
|
||||
"Race-Schutz aktiv (P-041/P-041a)",
|
||||
_gc_counter, _gc_value, _gc_age, self._gc_timeout_s,
|
||||
"Race-Schutz aktiv (P-093)",
|
||||
_gc_counter, _gc_value, _gc_age,
|
||||
self.STALE_RESET_TIMEOUT_S,
|
||||
)
|
||||
|
||||
# ── P-085: main_agent Stale-Reset ────────────────────────────
|
||||
|
||||
@@ -12,8 +12,8 @@ Counter resetten. Kein Timestamp = Counter ist definitiv stale.
|
||||
Testfälle:
|
||||
1. background_term=1, kein Timestamp, pgrep=0 → GC-Reset auf 0
|
||||
2. background_term=1, kein Timestamp, pgrep=1 → KEIN Reset (Race-Schutz)
|
||||
3. background_term=1, Timestamp < 1800s, pgrep=0 → KEIN Reset (zu frisch)
|
||||
4. background_term=1, Timestamp > 1800s, pgrep=0 → GC-Reset (existierender Fall)
|
||||
3. background_term=1, Timestamp < 60s, pgrep=0 → KEIN Reset (zu frisch)
|
||||
4. background_term=1, Timestamp > 60s, pgrep=0 → GC-Reset (existierender Fall)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -86,7 +86,7 @@ class TestP089BackgroundTermGCNoTimestamp:
|
||||
)
|
||||
|
||||
def test_no_reset_when_fresh_timestamp(self):
|
||||
"""Fall 3: background_term=1, Timestamp < 1800s, pgrep=0 → KEIN Reset (zu frisch)."""
|
||||
"""Fall 3: background_term=1, Timestamp < 60s, pgrep=0 → KEIN Reset (zu frisch)."""
|
||||
engine = _make_engine()
|
||||
# Via Hook setzen (setzt Timestamp auf time.time())
|
||||
engine.on_pre_tool_call(
|
||||
@@ -105,14 +105,14 @@ class TestP089BackgroundTermGCNoTimestamp:
|
||||
)
|
||||
|
||||
def test_reset_when_stale_timestamp(self):
|
||||
"""Fall 4: background_term=1, Timestamp > 1800s, pgrep=0 → GC-Reset (existierender Fall)."""
|
||||
"""Fall 4: background_term=1, Timestamp > 60s, pgrep=0 → GC-Reset (existierender Fall)."""
|
||||
engine = _make_engine()
|
||||
engine.on_pre_tool_call(
|
||||
tool_name="terminal",
|
||||
args={"background": True, "command": "sleep 10"},
|
||||
)
|
||||
assert engine.counters["background_term"] == 1
|
||||
# Timestamp künstlich altern lassen (> 1800s Default)
|
||||
# Timestamp künstlich altern lassen (> 60s STALE_RESET_TIMEOUT_S)
|
||||
engine._counter_timestamps["background_term"] = time.time() - 2000.0
|
||||
|
||||
with patch.object(engine, "_count_delegated_subagents", return_value=0), \
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
"""
|
||||
P-093: chat_profile Stale-Counter GC-Reset mit 60s-Timeout.
|
||||
|
||||
Root Cause (bewiesen durch Logs): on_session_start setzt chat_profile=1
|
||||
(+ Timestamp), aber on_session_finalize feuert nicht zuverlässig → Counter
|
||||
bleibt hängen. Der alte GC-Timeout (1800s, P-041a) war zu lang: der Counter
|
||||
blieb bis zu 50 Min auf 1 und die Engine pulsierte endlos in GREEN_PULSE.
|
||||
|
||||
Fix (analog zu P-089 für background_term): Wenn chat_profile > 0 UND
|
||||
pgrep==0 UND (kein Timestamp ODER Timestamp > STALE_RESET_TIMEOUT_S=60s)
|
||||
→ Reset auf 0. main_agent bleibt ausgeschlossen (Rainers Veto — eigener
|
||||
Block mit eigenem Timeout).
|
||||
|
||||
Testfälle:
|
||||
1. chat_profile=1, Timestamp > 60s, pgrep=0 → GC-Reset auf 0 (der Bug)
|
||||
2. chat_profile=1, Timestamp > 60s, pgrep=1 → KEIN Reset (Race-Schutz)
|
||||
3. chat_profile=1, Timestamp < 60s (frisch), pgrep=0 → KEIN Reset
|
||||
4. background_term=1, Timestamp > 60s, pgrep=0 → GC-Reset (analog P-089)
|
||||
5. main_agent=1, Timestamp > 60s, pgrep=0 → KEIN Reset durch diesen Block
|
||||
(main_agent hat eigenen Block mit eigenem Timeout — Rainers Veto)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
||||
|
||||
|
||||
def _make_mock_rest():
|
||||
rest = MagicMock()
|
||||
rest.auth_token = "test-token"
|
||||
rest.user_id = "test-user"
|
||||
rest._request = MagicMock()
|
||||
return rest
|
||||
|
||||
|
||||
def _make_engine(rest=None, enabled=True, away_grace_s=0.1):
|
||||
from plugin.archivar.presence_engine import PresenceEngine
|
||||
if rest is None:
|
||||
rest = _make_mock_rest()
|
||||
return PresenceEngine(rest=rest, enabled=enabled, away_grace_s=away_grace_s)
|
||||
|
||||
|
||||
class TestP093ChatProfileStaleReset:
|
||||
"""P-093: chat_profile GC-Reset bei Timestamp > 60s + pgrep==0."""
|
||||
|
||||
def test_stale_reset_timeout_default_is_60s(self):
|
||||
"""STALE_RESET_TIMEOUT_S == 60.0 (kürzer als der alte 1800s-Cap)."""
|
||||
from plugin.archivar.presence_engine import PresenceEngine
|
||||
assert PresenceEngine.STALE_RESET_TIMEOUT_S == 60.0
|
||||
assert PresenceEngine.STALE_RESET_TIMEOUT_S < PresenceEngine.GC_TIMEOUT_DEFAULT_S
|
||||
|
||||
def test_chat_profile_reset_when_timestamp_over_60s_and_pgrep_zero(self):
|
||||
"""Fall 1 (der Bug): chat_profile=1, Timestamp > 60s, pgrep=0 → Reset.
|
||||
|
||||
Simuliert den 50-Min-Hänger: on_session_start setzt den Counter,
|
||||
on_session_finalize feuert nie. Timestamp ist > 60s alt, pgrep==0.
|
||||
"""
|
||||
engine = _make_engine()
|
||||
engine.on_session_start() # chat_profile=1, Timestamp=now
|
||||
assert engine.counters["chat_profile"] == 1
|
||||
assert engine._counter_timestamps["chat_profile"] > 0.0
|
||||
# Timestamp künstlich altern lassen (> 60s, aber < 1800s)
|
||||
engine._counter_timestamps["chat_profile"] = time.time() - 120.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, (
|
||||
"P-093 FAIL: chat_profile mit Timestamp > 60s + pgrep=0 muss auf 0 "
|
||||
f"resettet werden, blieb aber auf {engine.counters['chat_profile']}"
|
||||
)
|
||||
assert engine._counter_timestamps["chat_profile"] == 0.0
|
||||
# Phase fällt automatisch auf GREEN_STATIC → Puls stoppt
|
||||
assert engine.get_current_phase().name == "GREEN_STATIC"
|
||||
|
||||
def test_chat_profile_not_reset_when_pgrep_nonzero(self):
|
||||
"""Fall 2: chat_profile=1, Timestamp > 60s, pgrep=1 → KEIN Reset."""
|
||||
engine = _make_engine()
|
||||
engine.on_session_start()
|
||||
engine._counter_timestamps["chat_profile"] = time.time() - 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()
|
||||
|
||||
assert engine.counters["chat_profile"] == 1, (
|
||||
"P-093 FAIL: chat_profile mit pgrep=1 darf NICHT resettet werden "
|
||||
"(Race-Schutz)"
|
||||
)
|
||||
|
||||
def test_chat_profile_not_reset_when_fresh_timestamp(self):
|
||||
"""Fall 3: chat_profile=1, Timestamp < 60s (frisch), pgrep=0 → KEIN Reset."""
|
||||
engine = _make_engine()
|
||||
engine.on_session_start() # Timestamp = now (frisch, ~0s alt)
|
||||
|
||||
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"] == 1, (
|
||||
"P-093 FAIL: frischer Timestamp (< 60s) darf NICHT resettet werden"
|
||||
)
|
||||
|
||||
def test_background_term_reset_when_timestamp_over_60s(self):
|
||||
"""Fall 4: background_term=1, Timestamp > 60s, pgrep=0 → Reset (analog P-089)."""
|
||||
engine = _make_engine()
|
||||
engine.on_pre_tool_call(
|
||||
tool_name="terminal",
|
||||
args={"background": True, "command": "sleep 10"},
|
||||
)
|
||||
assert engine.counters["background_term"] == 1
|
||||
engine._counter_timestamps["background_term"] = time.time() - 120.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["background_term"] == 0, (
|
||||
"P-093 FAIL: background_term mit Timestamp > 60s + pgrep=0 muss "
|
||||
"resettet werden"
|
||||
)
|
||||
|
||||
def test_main_agent_not_reset_by_chat_profile_block(self):
|
||||
"""Fall 5: main_agent bleibt von diesem Block unberührt (Rainers Veto).
|
||||
|
||||
main_agent hat einen eigenen GC-Block mit eigenem Timeout
|
||||
(MAIN_AGENT_GC_TIMEOUT_DEFAULT_S=60s). Der chat_profile/background_term
|
||||
Block darf main_agent NICHT anfassen.
|
||||
"""
|
||||
engine = _make_engine()
|
||||
engine.on_pre_gateway_dispatch() # main_agent=1, Timestamp=now
|
||||
assert engine.counters["main_agent"] == 1
|
||||
# main_agent-Timestamp frisch lassen (< 60s) → eigener Block greift nicht
|
||||
# chat_profile/background_term sind 0 → dieser Block greift nicht
|
||||
|
||||
with patch.object(engine, "_count_delegated_subagents", return_value=0), \
|
||||
patch.object(engine, "_count_active_tasks_from_tracker", return_value=0):
|
||||
engine._drift_correction()
|
||||
|
||||
# main_agent bleibt 1 (frischer Hook, eigener Block greift nicht)
|
||||
assert engine.counters["main_agent"] == 1, (
|
||||
"P-093 FAIL: main_agent darf vom chat_profile/background_term-Block "
|
||||
"nicht angefasst werden (Rainers Veto)"
|
||||
)
|
||||
Reference in New Issue
Block a user