perf/ polls every runner's /procs (lab 5 s, online workers 15 s) into its own
perf.db: 5-s samples (48 h), 1-min rollups (30 d), per-run totals and
per-process rows (Bash-tool commands kept forever). /api/perf/{live,history,
conversations,commands,conversation/{sid}} read it; /api/perf/kill and the
auto-kill stop leftovers of ended conversations (24 h after first seen, never
kept ones or keepLabels like opencode serve); /api/perf/settings. Owner only.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
"""The Performance dashboard's settings: auto-kill of leftover processes.
|
|
|
|
A **leftover** is a process an agent run started that is still alive after
|
|
the run ended (a forgotten ``vite``, ``python -m http.server``…). With
|
|
``autoKill`` on, one that has been a leftover for ``autoKillHours`` — counted
|
|
from the first poll that saw it as one, so a fresh install never kills on
|
|
sight — is stopped, unless it is *kept* (its row's Keep) or its label starts
|
|
with one of ``keepLabels``: shared daemons that merely inherited a session's
|
|
environment (``opencode serve`` is started by whichever opencode run comes
|
|
first and serves every later one)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import threading
|
|
|
|
DEFAULTS = {
|
|
"autoKill": True,
|
|
"autoKillHours": 24,
|
|
"keepLabels": ["opencode", "ask-form-relay", "ask-form serve"],
|
|
}
|
|
|
|
|
|
def valid_settings(raw: dict) -> dict:
|
|
out = dict(DEFAULTS)
|
|
if isinstance(raw.get("autoKill"), bool):
|
|
out["autoKill"] = raw["autoKill"]
|
|
try:
|
|
out["autoKillHours"] = max(1, min(24 * 30, int(raw.get("autoKillHours", out["autoKillHours"]))))
|
|
except (TypeError, ValueError):
|
|
pass
|
|
if isinstance(raw.get("keepLabels"), list):
|
|
out["keepLabels"] = sorted({str(x).strip()[:60] for x in raw["keepLabels"]
|
|
if str(x).strip()})[:50]
|
|
return out
|
|
|
|
|
|
def kept_by_label(label: str, keep: list[str]) -> bool:
|
|
return any(label == k or label.startswith(k + " ") or label.startswith(k + " (")
|
|
for k in keep)
|
|
|
|
|
|
class PerfSettings:
|
|
"""One small JSON file, rewritten atomically (temp file + rename)."""
|
|
|
|
def __init__(self, path: str):
|
|
self.path = pathlib.Path(path)
|
|
self._lock = threading.Lock()
|
|
try:
|
|
self._cfg = valid_settings(json.loads(self.path.read_text(encoding="utf-8")))
|
|
except (OSError, ValueError):
|
|
self._cfg = dict(DEFAULTS)
|
|
|
|
def get(self) -> dict:
|
|
with self._lock:
|
|
return dict(self._cfg)
|
|
|
|
def set(self, raw: dict) -> dict:
|
|
cfg = valid_settings({**self.get(), **(raw or {})})
|
|
with self._lock:
|
|
tmp = self.path.with_suffix(".tmp")
|
|
tmp.write_text(json.dumps(cfg, indent=2), encoding="utf-8")
|
|
os.replace(tmp, self.path)
|
|
self._cfg = cfg
|
|
return dict(cfg)
|