- /api/perf/conversation/{sid}/calls: per-call view cache on (updated, len(data)),
?since= delta with a cursor (client polls since = cursor - 30), marks memo,
(sid, updated) index. 260 calls: ~1 s → 3.7 ms cached, 0.7 ms per empty poll.
- _Memo: max_stale (soft key = summaries_version) + slots; project costs and
the related index use it (151 cost rebuilds x ~10 s in 40 min on prod).
- PerfStore: per-thread read-only connections, so a slow ingest no longer
parks pool threads (14 seen in query) and starves conversation reads.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
416 lines
12 KiB
Python
416 lines
12 KiB
Python
"""The Performance dashboard (perf/): CPU + memory of agent runs and the
|
||
processes they start. CPU is % of one core or CPU seconds; memory in bytes."""
|
||
from __future__ import annotations
|
||
|
||
from typing import Optional
|
||
|
||
from .base import Schema
|
||
from .perf_storage import PerfStorageGrow # noqa: F401 — PerfCalls.storage
|
||
|
||
__all__ = [
|
||
"PerfSettings", "PerfHost", "PerfAgentsNow", "PerfMachine", "PerfProcess",
|
||
"PerfSession", "PerfLeftover", "PerfLive", "PerfMachineSeries", "PerfSeries",
|
||
"PerfOther", "PerfHistory", "PerfHeaviest", "PerfConversationRow",
|
||
"PerfConversations", "PerfCommandExample", "PerfCommandTop", "PerfCommand",
|
||
"PerfCommands", "PerfLiveNow", "PerfConversation", "PerfCommandRun", "PerfSeriesOne",
|
||
"PerfCallProc", "PerfCallPart", "PerfCall", "PerfMark", "PerfCalls",
|
||
"PerfCallFileTop", "PerfCallFileDir", "PerfCallFiles",
|
||
]
|
||
|
||
|
||
class PerfSettings(Schema):
|
||
autoKill: bool
|
||
autoKillHours: int
|
||
keepLabels: list[str]
|
||
|
||
|
||
class PerfHost(Schema):
|
||
cpus: int
|
||
cpu: Optional[float] = None # % of one core, summed (≤ cpus × 100)
|
||
memTotal: Optional[int] = None
|
||
memAvailable: Optional[int] = None
|
||
swapTotal: Optional[int] = None
|
||
swapUsed: Optional[int] = None
|
||
load: Optional[list[float]] = None
|
||
|
||
|
||
class PerfAgentsNow(Schema):
|
||
count: int
|
||
cpu: float
|
||
rss: int
|
||
procs: int
|
||
|
||
|
||
class PerfMachine(Schema):
|
||
id: str
|
||
name: str
|
||
platform: Optional[str] = None
|
||
online: bool
|
||
at: Optional[float] = None
|
||
error: Optional[str] = None
|
||
unsupported: bool = False # its sidecar predates /procs
|
||
host: Optional[PerfHost] = None
|
||
scanMs: Optional[float] = None
|
||
agents: PerfAgentsNow
|
||
owner: Optional[str] = None # a guest's own computer: that guest
|
||
|
||
|
||
class PerfProcess(Schema):
|
||
pid: int
|
||
ppid: int
|
||
depth: int
|
||
label: str
|
||
cmd: str
|
||
kind: Optional[str] = None # "tool" = a Bash-tool shell
|
||
cpu: Optional[float] = None
|
||
cpuSeconds: float
|
||
rss: int
|
||
threads: int
|
||
treeCpuSeconds: float
|
||
treeRss: int
|
||
startedAt: float
|
||
detached: bool = False
|
||
# leftovers only
|
||
leftoverSince: Optional[float] = None
|
||
kept: Optional[bool] = None
|
||
keptByLabel: Optional[bool] = None
|
||
autoKillAt: Optional[float] = None
|
||
|
||
|
||
class PerfSession(Schema):
|
||
runner: str
|
||
sessionId: str
|
||
conversationId: Optional[str] = None
|
||
title: Optional[str] = None
|
||
guest: Optional[str] = None # the guest whose run it is (None: the owner's)
|
||
harness: Optional[str] = None
|
||
source: Optional[str] = None # hub | terminal
|
||
account: Optional[str] = None
|
||
model: Optional[str] = None
|
||
cwd: Optional[str] = None
|
||
rootPid: int
|
||
startedAt: float
|
||
cpu: Optional[float] = None
|
||
cpuSeconds: float
|
||
rss: int
|
||
procs: int
|
||
threads: int
|
||
processes: list[PerfProcess]
|
||
|
||
|
||
class PerfLeftover(Schema):
|
||
runner: str
|
||
sessionId: str
|
||
conversationId: Optional[str] = None
|
||
title: Optional[str] = None
|
||
guest: Optional[str] = None # the guest whose run it is (None: the owner's)
|
||
endedAt: Optional[str] = None
|
||
procs: int
|
||
rss: int
|
||
cpu: Optional[float] = None
|
||
cpuSeconds: float
|
||
processes: list[PerfProcess]
|
||
|
||
|
||
class PerfLive(Schema):
|
||
at: float
|
||
machines: list[PerfMachine]
|
||
sessions: list[PerfSession]
|
||
leftovers: list[PerfLeftover]
|
||
settings: PerfSettings
|
||
|
||
|
||
class PerfMachineSeries(Schema):
|
||
id: str
|
||
name: str
|
||
platform: Optional[str] = None
|
||
memTotal: Optional[int] = None
|
||
cpus: Optional[int] = None
|
||
cpu: list[Optional[float]]
|
||
mem: list[Optional[int]]
|
||
|
||
|
||
class PerfSeries(Schema):
|
||
runner: str
|
||
sessionId: str
|
||
conversationId: Optional[str] = None
|
||
title: Optional[str] = None
|
||
guest: Optional[str] = None # the guest whose run it is (None: the owner's)
|
||
cpu: list[float]
|
||
rss: list[int]
|
||
|
||
|
||
class PerfOther(Schema):
|
||
cpu: list[float]
|
||
rss: list[int]
|
||
|
||
|
||
class PerfHistory(Schema):
|
||
range: str
|
||
step: int
|
||
t: list[float]
|
||
machines: list[PerfMachineSeries]
|
||
series: list[PerfSeries]
|
||
other: PerfOther
|
||
|
||
|
||
class PerfHeaviest(Schema):
|
||
label: str
|
||
cpuSeconds: float
|
||
peakRss: int
|
||
|
||
|
||
class PerfConversationRow(Schema):
|
||
runner: str
|
||
sessionId: str
|
||
conversationId: Optional[str] = None
|
||
title: Optional[str] = None
|
||
guest: Optional[str] = None # the guest whose run it is (None: the owner's)
|
||
harness: Optional[str] = None
|
||
source: Optional[str] = None
|
||
firstSeen: float
|
||
lastSeen: float
|
||
cpuSeconds: float
|
||
peakCpu: Optional[float] = None
|
||
peakRss: int
|
||
peakProcs: int
|
||
runs: int
|
||
live: bool
|
||
heaviest: Optional[PerfHeaviest] = None
|
||
|
||
|
||
class PerfConversations(Schema):
|
||
range: str
|
||
rows: list[PerfConversationRow]
|
||
|
||
|
||
class PerfCommandExample(Schema):
|
||
cmd: Optional[str] = None
|
||
runner: str
|
||
sessionId: str
|
||
conversationId: Optional[str] = None
|
||
title: Optional[str] = None
|
||
guest: Optional[str] = None # the guest whose run it is (None: the owner's)
|
||
cpuSeconds: float
|
||
peakRss: int
|
||
duration: float
|
||
toolUseId: Optional[str] = None # the exact call (call probe), for a link to its card
|
||
|
||
|
||
class PerfCommandTop(Schema):
|
||
sessionId: str
|
||
conversationId: Optional[str] = None
|
||
title: Optional[str] = None
|
||
guest: Optional[str] = None # the guest whose run it is (None: the owner's)
|
||
cpuSeconds: float
|
||
|
||
|
||
class PerfCommand(Schema):
|
||
label: str
|
||
count: int
|
||
conversations: int
|
||
cpuSeconds: float
|
||
maxCpuSeconds: float
|
||
peakRss: int
|
||
avgDuration: float
|
||
lastSeen: float
|
||
example: PerfCommandExample
|
||
top: list[PerfCommandTop]
|
||
guests: list[str] = [] # whose runs, heaviest first (the row's avatars)
|
||
|
||
|
||
class PerfCommands(Schema):
|
||
range: str
|
||
kind: str
|
||
rows: list[PerfCommand]
|
||
|
||
|
||
class PerfLiveNow(Schema):
|
||
cpu: float
|
||
rss: int
|
||
procs: int
|
||
|
||
|
||
class PerfCommandRun(Schema):
|
||
"""One Bash command a conversation ran (it and everything under it)."""
|
||
runner: str
|
||
pid: int
|
||
startedAt: float
|
||
label: str
|
||
cmd: Optional[str] = None
|
||
cpuSeconds: float
|
||
peakRss: int
|
||
duration: float
|
||
# from the call probe: the exact call (its card), its status, how it was measured
|
||
toolUseId: Optional[str] = None
|
||
exitCode: Optional[int] = None
|
||
source: Optional[str] = None # kernel | sampled
|
||
agentType: Optional[str] = None # made by a subagent
|
||
|
||
|
||
class PerfConversation(Schema):
|
||
sessionId: str
|
||
tracked: bool
|
||
live: Optional[PerfLiveNow] = None
|
||
cpuSeconds: float
|
||
peakRss: int
|
||
peakProcs: int
|
||
peakCpu: Optional[float] = None
|
||
runs: int = 0 # agent processes: the run + each resume
|
||
firstSeen: Optional[float] = None
|
||
lastSeen: Optional[float] = None
|
||
heaviest: list[PerfHeaviest]
|
||
commands: list[PerfCommandRun] = []
|
||
processes: list[PerfProcess] = [] # its tree now, while it runs
|
||
leftovers: list[PerfLeftover] = [] # what it left running once ended
|
||
|
||
|
||
class PerfSeriesOne(Schema):
|
||
sessionId: str
|
||
step: int
|
||
t: list[float]
|
||
cpu: list[Optional[float]]
|
||
rss: list[Optional[int]]
|
||
|
||
|
||
# ── the call probe (perf/calls.py): each Bash call, exactly ──────────────────
|
||
|
||
class PerfCallProc(Schema):
|
||
"""One process a call started — exact (``exact``) when the kernel
|
||
reported its exit, sampled otherwise."""
|
||
pid: int
|
||
ppid: Optional[int] = None
|
||
depth: Optional[int] = None
|
||
label: str
|
||
argv: Optional[str] = None
|
||
startedAt: Optional[float] = None
|
||
endedAt: Optional[float] = None
|
||
cpu: Optional[float] = None # it + the children it reaped
|
||
cpuOwn: Optional[float] = None # it alone
|
||
peakRss: Optional[int] = None
|
||
exitCode: Optional[int] = None
|
||
signal: Optional[int] = None
|
||
exact: Optional[bool] = None
|
||
cpuUnseen: Optional[float] = None # sampled: reaped children never seen
|
||
gpuS: Optional[float] = None
|
||
gpuMem: Optional[int] = None
|
||
readBytes: Optional[int] = None
|
||
writeBytes: Optional[int] = None
|
||
|
||
|
||
class PerfCallPart(Schema):
|
||
"""A part of the command line (``a && b | c``) and what it ran."""
|
||
text: str
|
||
program: Optional[str] = None
|
||
op: Optional[str] = None # how it joins the next part
|
||
background: bool = False
|
||
depth: int = 0 # subshells around it
|
||
builtin: bool = False # forked nothing (cd, echo, export…)
|
||
other: Optional[bool] = None # processes no part names
|
||
pids: list[int] = []
|
||
cpu: float = 0
|
||
peakRss: int = 0
|
||
exitCode: Optional[int] = None
|
||
startedAt: Optional[float] = None
|
||
endedAt: Optional[float] = None
|
||
|
||
|
||
class PerfCall(Schema):
|
||
toolUseId: str
|
||
sessionId: Optional[str] = None
|
||
agentId: Optional[str] = None # the subagent that made the call
|
||
agentType: Optional[str] = None
|
||
promptId: Optional[str] = None
|
||
status: Optional[str] = None # running | detached | done | capped | error
|
||
source: str # kernel (exact) | sampled
|
||
background: bool = False
|
||
seen: bool = False # its shell was found
|
||
startedAt: Optional[float] = None
|
||
endedAt: Optional[float] = None
|
||
wall: Optional[float] = None # s
|
||
exitCode: Optional[int] = None
|
||
signal: Optional[int] = None
|
||
cpu: float = 0 # CPU seconds, every process
|
||
peakRss: int = 0
|
||
readBytes: int = 0 # disk
|
||
writeBytes: int = 0
|
||
gpuSeconds: float = 0
|
||
gpuMem: int = 0
|
||
procs: int = 0
|
||
liveProcs: int = 0
|
||
ran: Optional[str] = None # the command as it ran, when a hook rewrote it
|
||
overflow: int = 0
|
||
components: list[PerfCallPart] = []
|
||
processes: list[PerfCallProc] = []
|
||
tool: Optional[str] = None # Bash | Write | Edit | MultiEdit | NotebookEdit
|
||
filePath: Optional[str] = None # a file tool's file
|
||
agent: Optional[str] = None # claude-code | opencode | codex | pi (none: claude-probe's)
|
||
adopted: bool = False # no hook announced it: found under the agent's process
|
||
files: Optional[PerfCallFiles] = None
|
||
command: Optional[str] = None # the call's command (cut at 400 chars)
|
||
# Code Mode: the script (its REPL call's toolUseId) that made the call,
|
||
# and whether agent-probe matched it by time between two running scripts
|
||
script: Optional[str] = None
|
||
scriptGuess: bool = False
|
||
|
||
|
||
class PerfMark(Schema):
|
||
"""A turn or subagent boundary: the plan and the official usage reading
|
||
(account-wide, whole percents) at that moment — recorded by claude-probe,
|
||
derived from the usage history for agent-probe's."""
|
||
event: Optional[str] = None # prompt | stop | subStart | subStop
|
||
promptId: Optional[str] = None
|
||
agentId: Optional[str] = None
|
||
at: Optional[float] = None
|
||
plan: Optional[dict] = None
|
||
usage: Optional[dict] = None
|
||
derived: Optional[bool] = None # from the usage history, not recorded live
|
||
|
||
|
||
class PerfCallFileTop(Schema):
|
||
path: str
|
||
delta: int # allocated bytes, after - before
|
||
size: Optional[int] = None
|
||
op: Optional[str] = None # write | unlink | replace
|
||
created: Optional[bool] = None
|
||
deleted: Optional[bool] = None
|
||
|
||
|
||
class PerfCallFileDir(Schema):
|
||
dir: str
|
||
net: int
|
||
files: int
|
||
|
||
|
||
class PerfCallFiles(Schema):
|
||
"""What the call left on disk — exact: agent-probe's eBPF follows every
|
||
file its processes write, unlink or rename over (a Write / Edit: the
|
||
hook's stat before and after)."""
|
||
written: int
|
||
freed: int
|
||
net: int
|
||
logical: Optional[int] = None
|
||
created: int = 0
|
||
deleted: int = 0
|
||
files: int = 0
|
||
events: Optional[int] = None
|
||
exact: bool = True
|
||
source: Optional[str] = None # hook (a file tool) | kernel
|
||
top: list[PerfCallFileTop] = []
|
||
dirs: list[PerfCallFileDir] = []
|
||
|
||
|
||
class PerfCalls(Schema):
|
||
"""``GET /api/perf/conversation/{sid}/calls[?since=]``: whole, or only the
|
||
calls ingested after ``since`` (``delta``) — merge those by ``toolUseId``.
|
||
Poll with ``since = cursor - 30``."""
|
||
sessionId: str
|
||
calls: list[PerfCall]
|
||
# the newest ingest time this answer saw (``since`` when nothing changed;
|
||
# null: a whole read of a conversation without calls)
|
||
cursor: Optional[float] = None
|
||
delta: bool = False
|
||
marks: list[PerfMark] = []
|
||
# what each call added to its session's scratchpad / worktrees (toolUseId →)
|
||
storage: dict[str, "PerfStorageGrow"] = {}
|