Files
ai-agent/backend/perf/scope.py
gabrielvidalandClaude Opus 5.5 0af29c2d3b feat(perf): every perf row says whose it is; ?account= takes the guest pick
guest on each run, leftover, history band, conversation row, command
example / top entry, writer and storage conversation (the guest who started
it, else the guest whose computer it ran on); guests on a command and a
disk item; owner on a guest's computer. guest:<uid> items narrow the Guests
profile to those guests (the Homelab stays, other guests' computers go).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 16:53:16 +01:00

140 lines
6.2 KiB
Python

"""Which profile a perf row belongs to — the Personal / Work / Guests / All
switch of the Performance and Storage dashboards (``?account=profile:<id>``,
the value the other dashboards send; unset / ``all`` / anything without a
``profile:`` item = every profile).
Two rules (Gabriel's picks, form performance-profile-switch_7q5168):
* a **run** — its live process tree, its leftovers, its band in the history,
its commands, the disk items it made — is its *conversation's* profile,
the rule Activity and the lists use (``ProfileStore.of_meta``): a guest's
run on the Homelab is Guests', a pinned ``meta.profile`` wins. A session
the hub keeps no metadata for (a terminal ``claude`` on a Mac) is its
machine's, and so is a disk item no session claims.
* a **machine** — its card, its whole-box CPU / RAM line, its filesystems —
shows under the profile that lists it as a runner; a guest's own computer
under Guests, and the Guests view also shows the Homelab, where the hub
runs guests' conversations. The views also keep a machine that hosts a run
of the scope (a Work conversation pinned on the lab).
``guest:<uid>`` items (the Guests profile's chips, shared with the Guests
home) narrow the Guests profile to those guests — their runs and their own
computers. Every row also says whose it is (``guest``, :meth:`Scope.guest`):
the dashboards' guest pills, in the Guests view and in All.
"""
from __future__ import annotations
import accounts as accounts_mod
from profiles.store import GUESTS, guest_owner
from .poller import LAB
def wanted(param: str | None) -> set[str] | None:
"""The profile ids an ``?account=`` value asks for — None = every one."""
items = [x.strip() for x in (param or "").lower().split(",")]
ids = {x.split(":", 1)[1] for x in items if x.startswith("profile:") and len(x) > 8}
return ids or None
def picked_guests(param: str | None) -> set[str]:
"""The guests a ``guest:<uid>`` item narrows the Guests profile to."""
items = [x.strip() for x in (param or "").split(",")]
return {x.split(":", 1)[1] for x in items if x.lower().startswith("guest:") and len(x) > 6}
class Scope:
"""The predicates a view filters with, memoized for one request."""
def __init__(self, deps, param: str | None = None):
self.deps = deps
self.wants = wanted(param)
# the guest pick narrows the Guests profile only
self.only = picked_guests(param) if self.wants and GUESTS in self.wants else set()
self._machines: dict[str, str] = {}
self._convs: dict[tuple[str, str], bool] = {}
self._guests: dict[tuple[str, str], str | None] = {}
@property
def all(self) -> bool:
return self.wants is None
def _owner(self, runner: str) -> str | None:
"""The guest whose own computer ``runner`` is (None: the owner's)."""
if runner == LAB:
return None
try:
w = self.deps.worker_store.get(runner) or {}
except Exception: # noqa: BLE001 — a store hiccup scopes it as the owner's
w = {}
return w.get("owner") or None
def machine_profile(self, runner: str) -> str:
if runner not in self._machines:
self._machines[runner] = self.deps.profile_store.resolve(
None if runner == LAB else runner, None, None, self._owner(runner))
return self._machines[runner]
def _picked(self, profile: str, guest: str | None) -> bool:
"""Past the guest pick: anything not the Guests', or a picked guest's."""
return profile != GUESTS or not self.only or guest in self.only
def owns(self, runner: str) -> bool:
"""The machine is the scoped profile's own (a guest's computer: and
its guest is picked, when the pick narrows Guests)."""
if self.all:
return True
p = self.machine_profile(runner)
return p in self.wants and self._picked(p, self._owner(runner))
def machine(self, runner: str) -> bool:
"""The machine shows in the scope: its own, or the Homelab under
Guests (the hub runs guests' conversations there)."""
if self.owns(runner):
return True
return runner == LAB and GUESTS in self.wants and self.deps.profile_store.guests_enabled()
def guest(self, runner: str, sid: str | None) -> str | None:
"""Whose run it is: the guest who started it (or whose own login it
billed), else the guest whose computer it ran on — None = the owner's."""
key = (runner, sid or "")
if key not in self._guests:
m = (self.deps.meta_store.peek(sid) or {}) if sid else {}
self._guests[key] = (guest_owner(m, accounts_mod.resolve(None, m)) if m else None) \
or self._owner(runner)
return self._guests[key]
def conv_profile(self, runner: str, sid: str) -> str:
m = self.deps.meta_store.peek(sid) or {}
if not m:
return self.machine_profile(runner)
account = accounts_mod.resolve(None, m)
# a hub conversation names its worker; one with metadata but none
# (a lab run, a tagged terminal session) is where the probe saw it
where = m.get("worker") or m.get("workerSource") or (None if runner == LAB else runner)
return self.deps.profile_store.resolve(
where, account, m.get("profile"),
guest_owner(m, account) or self._owner(runner))
def conv(self, runner: str, sid: str | None) -> bool:
"""The run (``sid`` on ``runner``) is the scope's — no session: the
machine's own profile decides."""
if self.all:
return True
if not sid:
return self.owns(runner)
key = (runner, sid)
if key not in self._convs:
p = self.conv_profile(runner, sid)
self._convs[key] = p in self.wants and self._picked(p, self.guest(runner, sid))
return self._convs[key]
def item(self, runner: str, sids) -> bool:
"""A disk item: any of its sessions is the scope's (none: the
machine's own profile decides)."""
sids = [s for s in sids if s]
if not sids:
return self.conv(runner, None)
return any(self.conv(runner, s) for s in sids)