Files
ai-agent/backend/runs/sidecar_client.py
gabrielvidalandClaude Opus 5.5 e0d0bf3b51 refactor(approvals): move Approvals to the live approvals plugin
Removes core backend/approvals/ (routes, service, store), its schemas and
tests, Settings → Approvals, the generated client bits and `notify approval`
with cli/skills/ask-approval: the ai-agent-plugins 'approvals' plugin (with
its ask-approval skill) replaces them.

- carryover.approvals(): /data/approvals.json → the plugin's hub-scoped
  approvals.json, once (history and standing rules kept)
- the run-exit watcher now dispatches conversation.finished to live plugins
- /settings/approvals redirects to /settings/plugin/approvals
- guests get the approval guidance line when the plugin is on for them

authz/matrix.golden.txt is left for the merge step (3 approvals routes gone).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 20:02:48 +01:00

246 lines
11 KiB
Python

"""Talking to the runners (host sidecar / paired workers) and the run-exit watcher."""
import datetime
import json
import logging
import time
import urllib.error
import urllib.request
import uuid as uuidlib
from fastapi import HTTPException
import accounts as accounts_mod
import workers as workers_mod
from runs import genprogress
from core import liveruns
from core.config import SIDECAR_TOKEN, SIDECAR_URL
from core.state import AppState
log = logging.getLogger(__name__)
# ── spawn / resume / interrupt `claude -p` sessions via the host sidecar ──────
def _sidecar_call(deps: AppState, path: str, payload: dict | None = None, *,
method: str = "POST", timeout: float = 15,
worker: str | None = None) -> dict:
"""Call a runner (bearer auth) and return its JSON reply: the host sidecar,
or — with ``worker`` — that paired remote worker (workers.py).
Raises 503 when the sidecar isn't configured, 502 when it's unreachable,
and passes a sidecar error through with its own status. The detail is
always an object ``{message, kind, hint?, account?}`` (see the sidecar's
``claude_cli.error_detail``), so the viewer can tell an expired login from
a rate limit from a broken CLI without parsing prose.
A claude run on a worker keeps its ``account`` when the worker runs it
through the hub's Claude proxy (claudeproxy/ — its sidecar knows which);
a worker whose sidecar predates the proxy gets none and takes its own
default login. An opencode run keeps it (the worker's opencode account)."""
if worker:
w = deps.worker_store.get(worker)
if not w:
raise HTTPException(409, {"message": f"unknown worker: {worker!r} "
"(unpaired?)",
"kind": "worker_unknown"})
if payload is not None and (payload.get("harness") or "claude") == "claude":
held = (deps.worker_store.status(worker).get("claudeProxy") or {})
if payload.get("account") not in (held.get("accounts") or []) \
and not _own_login(w, payload.get("account")):
payload = {k: v for k, v in payload.items() if k != "account"}
try:
return workers_mod.sidecar_request(
w["url"], w["token"], path, payload, method=method,
timeout=timeout, label=f"worker {w.get('name') or worker}")
except HTTPException as e:
if isinstance(e.detail, dict) and \
e.detail.get("kind") == "worker_offline":
deps.worker_store.set_status(worker, status="offline",
lastError=e.detail.get("message"))
deps.hub.publish({"type": "workers"})
raise
if not SIDECAR_TOKEN:
raise HTTPException(503, {"message": "sidecar not configured (no "
"SIDECAR_TOKEN)", "kind": "sidecar"})
return workers_mod.sidecar_request(SIDECAR_URL, SIDECAR_TOKEN, path,
payload, method=method, timeout=timeout)
def _own_login(w: dict, account: str | None) -> bool:
"""``guest-<owner>`` on that guest's own computer: its own Claude login
(users/runs.run_on_computer, the sidecar's ``machine_login``) — never
one of the proxy's accounts, and never to be dropped. Dropping it made
the computer take its default account, the proxied ``personal``: 23 of
Akira's runs labelled "his own Claude" billed the owner's plan
(2026-11-06 → 11-10, homelab data/log/2026-11-10-akira-budget-proxy-leak.md)."""
from workers import guests
owner = guests.owner_of(w)
return bool(owner) and account == accounts_mod.guest_account(owner)
def _detail_text(e: HTTPException) -> str:
"""An HTTPException's detail as one line of text (structured or not)."""
d = e.detail
return d.get("message", str(d)) if isinstance(d, dict) else str(d)
_sidecar_detail = workers_mod.error_detail
def _valid_sid(raw: str) -> str:
sid = (raw or "").strip()
try:
uuidlib.UUID(sid)
except ValueError:
raise HTTPException(400, "sessionId must be a valid UUID")
return sid
# ── run-exit detection ────────────────────────────────────────────────────────
# The sidecar knows exactly when a run's process dies (its pidfiles), so the
# viewer doesn't have to guess "finished" from the DONE marker or wait out the
# recency window. A background thread polls the sidecar's live-session list and
# stamps a conversation finished the moment its run vanishes from it.
# Sessions with an in-flight /resume or /interrupt: those deliberately stop or
# replace the run, so the exit watcher must not race the vanish and stamp
# "finished" over the state the endpoint is about to write.
_sidecar_busy: set[str] = set()
def _sidecar_live_sids(deps: AppState, worker: str | None = None) -> set[str] | None:
"""Session ids with a live process behind them, per the runner's
``/sessions`` (which also reaps finished runs' pidfiles as it answers) —
the host sidecar, or the paired ``worker``. None = unconfigured/unreachable
— the caller must treat that as "unknown", never as "nothing is running"."""
sessions = _sidecar_live_sessions(deps, worker)
return None if sessions is None else {s["sessionId"] for s in sessions}
def _sidecar_live_sessions(deps: AppState, worker: str | None = None) -> list[dict] | None:
"""The runner's ``/sessions`` rows (see ``_sidecar_live_sids``)."""
if worker:
w = deps.worker_store.get(worker)
if not w or not deps.worker_store.online(worker):
return None
base, token = w["url"], w["token"]
elif not SIDECAR_TOKEN:
return None
else:
base, token = SIDECAR_URL, SIDECAR_TOKEN
req = urllib.request.Request(
f"{base}/sessions", headers={"Authorization": f"Bearer {token}"})
try:
with urllib.request.urlopen(req, timeout=3) as r:
out = json.loads(r.read().decode())
return [s for s in out.get("sessions", [])
if isinstance(s, dict) and s.get("sessionId")]
except (urllib.error.URLError, OSError, ValueError, KeyError, AttributeError):
return None
def _mark_run_exited(deps: AppState, sid: str) -> None:
"""A sidecar-launched run's process is gone: stamp the conversation
``finished`` (with the exit time) unless an endpoint/skill already moved it
to another state. The transcript's own trailing markers still outrank the
stamp in ``_resolve_state`` (interrupted, DONE+notified, …), and
``exitedAt`` lets it be distrusted if the session later continues outside
the sidecar (see ``_outlived_exit``). A run the closing push already
stamped ``finished`` only gets the exit time: that stamp is trusted once
the process is gone (``_resolve_state``)."""
state = deps.meta_store.get(sid).get("state")
if state not in (None, "running", "finished"):
return
now = datetime.datetime.now(datetime.timezone.utc) \
.strftime("%Y-%m-%dT%H:%M:%SZ")
deps.meta_store.update(sid, {"state": "finished", "exitedAt": now})
deps.hub.publish({"type": "meta", "id": sid})
# Live plugins subscribed to ``conversation.finished`` (e.g. Approvals
# relaying an owner run's report): the owner's installs, best-effort.
try:
from plugins.scheduler import dispatch_event
from plugins.store import OWNER_UID
dispatch_event(deps, "conversation.finished",
{"sessionId": sid, "state": "finished", "exitedAt": now},
owner_uid=OWNER_UID)
except Exception:
log.exception("conversation.finished dispatch for %s", sid)
def _watch_sidecar_runs(deps: AppState, interval: float = 2.0) -> None:
"""Poll the sidecar's live-session list; a session id vanishing from it
means its process exited — flip the conversation to ``finished`` within a
tick instead of after the recency window.
The first successful poll only sets the baseline (a fresh backend can't
tell "exited while we were down" from "never sidecar-launched" — those
stay on the recency fallback). Ids with an in-flight resume/interrupt are
deferred a tick so the endpoint's own stamp wins; if the resume then
failed, the id is still absent next tick and gets flipped then.
One baseline **per runner** (the host sidecar = ``None``, then each paired
worker): an unreachable runner answers None and keeps *its* baseline, so a
Mac dropping off the tailnet never reads as "all its runs exited" — and
never disturbs the lab sidecar's.
While a local-model run is live it polls every ``GEN_RELAY_S`` instead:
the viewer plays the run's llm-server readings on the conversation's clock,
which runs only a second or so behind its transcript."""
prev: dict[str | None, set[str]] = {}
while True:
time.sleep(genprogress.RELAY_S if genprogress.snapshot() else interval)
runners: list[str | None] = [None] + [w["id"]
for w in deps.worker_store.list()]
for runner in runners:
try:
sessions = _sidecar_live_sessions(deps, runner)
if sessions is None:
continue # unknown — keep the baseline, flip nothing
cur = {s["sessionId"] for s in sessions}
# Before any exit stamp below: the state that stamp brings
# back must already see the process gone.
liveruns.update(runner, cur, {
s["sessionId"]: s["held"] for s in sessions
if isinstance(s.get("held"), dict)})
# Local-model runs' live decode (runs/genprogress.py).
genprogress.update(deps, runner, sessions)
# A Code Mode run's start banner, when its launch answer
# didn't carry it (runs/service.py code_mode_seen).
from runs.service import code_mode_seen
code_mode_seen(deps, sessions)
if runner in prev:
for sid in prev[runner] - cur:
if sid in _sidecar_busy:
cur.add(sid) # decide next tick
continue
_mark_run_exited(deps, sid)
prev[runner] = cur
if runner:
deps.worker_store.set_status(runner, running=len(cur))
except Exception:
pass
for gone in [r for r in prev if r not in runners]:
prev.pop(gone, None) # unpaired
liveruns.drop(gone)
# The children whose turn just ended report back to their parent
# (runs/children.py) — after the stamps above, so they read ended.
from runs import children
try:
children.sweep_reports(deps)
except Exception:
children.log.exception("report-back sweep failed")
def _sidecar_get(path: str, timeout: float = 25) -> dict | None:
"""GET from the host sidecar; None when it's unconfigured/unreachable."""
if not SIDECAR_TOKEN:
return None
req = urllib.request.Request(
f"{SIDECAR_URL}{path}",
headers={"Authorization": f"Bearer {SIDECAR_TOKEN}"})
try:
with urllib.request.urlopen(req, timeout=timeout) as r:
return json.loads(r.read().decode())
except (urllib.error.URLError, OSError, ValueError):
return None