feat(P-041): GC for hook-set counters — pgrep-negativ + 30s timestamp guard
This commit is contained in:
@@ -690,6 +690,15 @@ class PresenceEngine:
|
||||
"background_term": 0,
|
||||
}
|
||||
|
||||
# ── P-041: Timestamps für Hook-gesetzte Counter (GC) ─────────────
|
||||
# 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().
|
||||
self._counter_timestamps: Dict[str, float] = {
|
||||
"chat_profile": 0.0,
|
||||
"background_term": 0.0,
|
||||
}
|
||||
|
||||
# ── State-Maschine ────────────────────────────────────────────────
|
||||
self._status: str = "ONLINE" # ONLINE | BUSY | AWAY (Kompatibilität)
|
||||
self._transition_lock: threading.Lock = threading.Lock()
|
||||
@@ -857,6 +866,50 @@ class PresenceEngine:
|
||||
delegate_counter, real_delegated,
|
||||
)
|
||||
|
||||
# ── P-041: 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 > 30s → Reset auf 0.
|
||||
# 30s Timestamp-Guard verhindert premature Reset bei Race
|
||||
# zwischen Prozess-Start und Hook-Feuer.
|
||||
_GC_TIMEOUT_S = 30.0
|
||||
for _gc_counter in ("chat_profile", "background_term"):
|
||||
_gc_value = self.counters.get(_gc_counter, 0)
|
||||
if _gc_value <= 0:
|
||||
continue
|
||||
_gc_ts = self._counter_timestamps.get(_gc_counter, 0.0)
|
||||
if _gc_ts <= 0.0:
|
||||
# Kein Timestamp — defensiv: nichts tun
|
||||
continue
|
||||
_gc_age = time.time() - _gc_ts
|
||||
if _gc_age > _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)",
|
||||
_gc_counter, _gc_value, _gc_age, _GC_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) — "
|
||||
"Race-Schutz aktiv (P-041)",
|
||||
_gc_counter, _gc_value, _gc_age, _GC_TIMEOUT_S,
|
||||
)
|
||||
|
||||
# ── Phasen-Übergang prüfen nach GC-Reset ─────────────────────
|
||||
# Falls GC einen Counter auf 0 gesetzt hat, kann sich die Phase
|
||||
# geändert haben.
|
||||
_gc_old_phase = self._mediator.current_phase
|
||||
_gc_new_phase = resolve_phase(self.counters)
|
||||
self._update_internal_status(_gc_new_phase)
|
||||
if _gc_old_phase != _gc_new_phase:
|
||||
self._mediator.on_phase_transition(_gc_old_phase, _gc_new_phase)
|
||||
|
||||
@staticmethod
|
||||
def _count_delegated_subagents() -> int:
|
||||
"""Zähle DELEGIERTE Subagent-Prozesse via pgrep (P-008, P-009 safe).
|
||||
@@ -1015,6 +1068,15 @@ class PresenceEngine:
|
||||
new_value = 0
|
||||
self.counters[counter_name] = new_value
|
||||
|
||||
# P-041: Timestamp für Hook-gesetzte Counter tracken (GC-Guard).
|
||||
# Beim Inkrementieren: time.time() setzen.
|
||||
# Beim Dekrementieren auf 0: Timestamp löschen.
|
||||
if counter_name in self._counter_timestamps:
|
||||
if new_value > old_value:
|
||||
self._counter_timestamps[counter_name] = time.time()
|
||||
elif new_value == 0:
|
||||
self._counter_timestamps[counter_name] = 0.0
|
||||
|
||||
# P-039: Reconciliation-Schutz — Timestamp der letzten echten
|
||||
# Counter-Mutation merken (nicht bei Clamp auf 0).
|
||||
if new_value != old_value:
|
||||
|
||||
Reference in New Issue
Block a user