fix: Phase 3 line-walk fixes (3a-3d)
F3a-01 agent.py: _safe_float() prevents ValueError from non-numeric metric values F3b-01 schedule_runner.py: fix tz-aware minus naive DateTime TypeError F3b-02 schedule_runner.py: cron field divided by 0 returns False instead of crash F3c-01 redis/client.py: mask credentials in REDIS_URL log output F3d-01 main.py: cancel background tasks when primary worker lock is lost F3d-02 main.py: strip whitespace from CORS origins after split Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+26
-14
@@ -83,6 +83,17 @@ def _threshold_for(thresholds: dict, metric: str) -> float:
|
||||
return float(thresholds.get(metric, defaults.get(metric, 80)))
|
||||
|
||||
|
||||
def _safe_float(value, name: str) -> Optional[float]:
|
||||
"""Convert agent-supplied metric value to float, discarding malformed values."""
|
||||
if value is None:
|
||||
return None
|
||||
try:
|
||||
return float(value)
|
||||
except (ValueError, TypeError):
|
||||
logger.warning(f"Agent sent non-numeric {name}: {value!r!s:.50} — skipped")
|
||||
return None
|
||||
|
||||
|
||||
def _detect_alerts(system_info: dict, thresholds: dict) -> list:
|
||||
"""Detect metrics exceeding alert thresholds
|
||||
|
||||
@@ -90,16 +101,16 @@ def _detect_alerts(system_info: dict, thresholds: dict) -> list:
|
||||
E.g. [("cpu", 85.3), ("mem", 92.1)]
|
||||
"""
|
||||
alerts = []
|
||||
cpu = system_info.get("cpu_usage") or system_info.get("cpu")
|
||||
mem = system_info.get("mem_usage") or system_info.get("mem")
|
||||
disk = system_info.get("disk_usage") or system_info.get("disk")
|
||||
cpu = _safe_float(system_info.get("cpu_usage") or system_info.get("cpu"), "cpu")
|
||||
mem = _safe_float(system_info.get("mem_usage") or system_info.get("mem"), "mem")
|
||||
disk = _safe_float(system_info.get("disk_usage") or system_info.get("disk"), "disk")
|
||||
|
||||
if cpu is not None and float(cpu) > _threshold_for(thresholds, "cpu"):
|
||||
alerts.append(("cpu", float(cpu)))
|
||||
if mem is not None and float(mem) > _threshold_for(thresholds, "mem"):
|
||||
alerts.append(("mem", float(mem)))
|
||||
if disk is not None and float(disk) > _threshold_for(thresholds, "disk"):
|
||||
alerts.append(("disk", float(disk)))
|
||||
if cpu is not None and cpu > _threshold_for(thresholds, "cpu"):
|
||||
alerts.append(("cpu", cpu))
|
||||
if mem is not None and mem > _threshold_for(thresholds, "mem"):
|
||||
alerts.append(("mem", mem))
|
||||
if disk is not None and disk > _threshold_for(thresholds, "disk"):
|
||||
alerts.append(("disk", disk))
|
||||
|
||||
return alerts
|
||||
|
||||
@@ -114,11 +125,12 @@ def _detect_recovery(system_info: dict, prev_alerts: set, thresholds: dict) -> l
|
||||
parts = prev.split(":")
|
||||
if len(parts) != 2:
|
||||
continue
|
||||
metric, prev_val = parts[0], float(parts[1])
|
||||
current = system_info.get(f"{metric}_usage") or system_info.get(metric)
|
||||
|
||||
if current is not None and float(current) < _threshold_for(thresholds, metric):
|
||||
recoveries.append((metric, float(current)))
|
||||
metric = parts[0]
|
||||
current = _safe_float(
|
||||
system_info.get(f"{metric}_usage") or system_info.get(metric), metric
|
||||
)
|
||||
if current is not None and current < _threshold_for(thresholds, metric):
|
||||
recoveries.append((metric, current))
|
||||
|
||||
return recoveries
|
||||
|
||||
|
||||
Reference in New Issue
Block a user