Files
ai-agent/backend/perf/conversation.py
gabrielvidalandClaude Opus 5.5 4f05cb5378 feat(perf): rankings and drawer from the call probe only; sampled Bash shells kept a day
Sampled Bash-tool rows labelled a whole call by its first word (mkdir 7,060 s
was an mkdir && npm run build subtree): they no longer rank anywhere, and are
pruned a day after they're last seen (the leftover auto-kill is all that still
reads them). Programs keep the samples of what runs outside Bash calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 22:35:45 +01:00

88 lines
4.2 KiB
Python

"""One conversation's CPU and memory — the header chip and the details
drawer's consumption cards: live now (with its process tree), totals, each
Bash command it ran, what it left running, and its timeline."""
from __future__ import annotations
import math
import time
from .store import SAMPLES_KEEP_S
from .views import _r, leftover_group, live_sessions
SERIES_POINTS = 120
def conversation(deps, sid: str) -> dict:
now_rows = [s for s in live_sessions(deps) if s["sessionId"] == sid]
tot = deps.perf_store.query(
"SELECT SUM(cpu_seconds), MAX(peak_rss), MAX(peak_procs), MAX(peak_cpu), COUNT(*),"
" MIN(first_seen), MAX(last_seen) FROM perf_runs WHERE sid = ?", (sid,))[0]
# each Bash call, as the call probe measured it (calls.py)
exact = deps.perf_store.query(
"SELECT runner, started, label, command, cpu, peak_rss,"
" COALESCE(duration_ms / 1000.0, ended - started, 0), tool_use_id, exit_code, source,"
" agent_type FROM perf_calls WHERE sid = ? ORDER BY cpu DESC LIMIT 20", (sid,))
heavy = deps.perf_store.query(
"SELECT label, SUM(cpu), MAX(peak_rss) FROM perf_call_parts WHERE sid = ?"
" GROUP BY label ORDER BY 2 DESC LIMIT 3", (sid,))
live_now = None
if now_rows:
live_now = {"cpu": sum(s.get("cpu") or 0 for s in now_rows),
"rss": sum(s.get("rss") or 0 for s in now_rows),
"procs": sum(s.get("procs") or 0 for s in now_rows)}
cfg = deps.perf_settings.get()
leftovers = []
for runner, st in deps.perf_poller.live().items():
for g in (st.get("snap") or {}).get("leftovers") or []:
if g["sessionId"] == sid and time.time() - (st.get("at") or 0) < 60:
leftovers.append(leftover_group(deps, runner, g, cfg))
return {
"sessionId": sid, "tracked": tot[0] is not None or bool(exact), "live": live_now,
"cpuSeconds": round(tot[0] or 0, 1), "peakRss": tot[1] or 0,
"peakProcs": tot[2] or 0, "peakCpu": _r(tot[3]), "runs": tot[4] or 0,
"firstSeen": tot[5], "lastSeen": tot[6],
"heaviest": [{"label": l, "cpuSeconds": round(c or 0, 1), "peakRss": r or 0}
for l, c, r in heavy],
"commands": [{"runner": rn, "pid": 0, "startedAt": st or 0, "label": l, "cmd": cmd,
"cpuSeconds": round(c or 0, 2), "peakRss": r or 0,
"duration": round(d or 0, 1), "toolUseId": tid, "exitCode": code,
"source": src, "agentType": at}
for rn, st, l, cmd, c, r, d, tid, code, src, at in exact],
"processes": [p for s in now_rows for p in s.get("processes") or []],
"leftovers": leftovers,
}
def series(deps, sid: str, window: str = "life") -> dict:
"""The conversation's CPU (% of one core) and RSS over time, ≤ ~120
points: its whole tracked life, or the last 10 minutes (``window=10m``,
the live sparklines). Gaps (it wasn't running) are nulls."""
now = time.time()
if window == "10m":
since, until = now - 600, now
else:
span = deps.perf_store.query(
"SELECT MIN(first_seen), MAX(last_seen) FROM perf_runs WHERE sid = ?", (sid,))[0]
if span[0] is None:
return {"sessionId": sid, "step": 0, "t": [], "cpu": [], "rss": []}
since, until = span[0], max(span[1], span[0] + 60)
recent = since >= now - SAMPLES_KEEP_S
floor = 5 if recent else 60
step = max(floor, int(math.ceil((until - since) / SERIES_POINTS / floor)) * floor)
table = "perf_samples" if recent else "perf_minutes"
rows = deps.perf_store.query(
f"SELECT CAST(t / {step} AS INT) * {step}, AVG(cpu), MAX(rss) FROM {table}"
f" WHERE sid = ? AND t >= ? AND t <= ? GROUP BY 1, runner", (sid, since - step, until))
start = int(since // step * step)
n = int((until - start) // step) + 1
cpu: list = [None] * n
rss: list = [None] * n
for b, c, r in rows:
i = int((b - start) // step)
if 0 <= i < n:
cpu[i] = round((cpu[i] or 0) + (c or 0), 1)
rss[i] = (rss[i] or 0) + int(r or 0)
return {"sessionId": sid, "step": step, "t": [start + i * step for i in range(n)],
"cpu": cpu, "rss": rss}