Files
ai-agent/backend/profiles/runners.py
Gabriel VidalandClaude Opus 5.5 0bbb086c07 feat(providers): cut catalog, presets, accounts, profiles & routing over to the ProviderRegistry (Wave 3A)
Strict plugin-only: no model / account / harness table left in core.

- plugins/providers: an unbound or never-seeded store reads the seed dir's
  provider-*/harness-* manifests (PLUGINS_SEED_DIR, else the plugins
  checkout, else backend/fixtures/typed_plugins) — once seeded, only what is
  installed. Harness↔vendor helpers (vendors_of_harness, login_harness,
  hosted_harnesses, accounts_for_harness, provider_config/secret).
- models/catalog + presets: list_models / is_local / context_window /
  opencode_model / agy_model / pi_provider / key_of / vendor_of / guest pool
  delegate to the registry; DEFAULT_MODEL, FAMILIES… computed (PEP 562).
  openrouter.py reads the provider-openrouter plugin's catalogueUrl and its
  api_key hub secret (OPENROUTER_API_KEY fallback).
- accounts: provider-owned; vendor-native helpers (accounts_for_vendor,
  vendor_of_account, resolve_alias, wire_id) + compat for_harness /
  valid_for / harness_of joined through harness.supports[]. Clean provider
  accounts: keyless evox2 (alias opencode-lab, still sent to the sidecar as
  opencode-lab), openrouter's own account — pi no longer bills `personal`.
- profiles: profile-level ★ per vendor (profile.accounts), per-machine
  overrides on workers[i].accounts; idempotent migration of the harness-keyed
  runner ★ on load (file version 3); default_login takes a vendor or a
  harness; PUT|PATCH /api/profiles/{id} edits accounts. The API keeps
  harness keys beside vendor keys in each runner's accounts/logins.
- runs/cron/scheduled: harness tuples → the registry (caps lean/fork,
  runsOn lab, hosted harnesses); a new run routes model → vendor → the
  profile's ★ for it.
- cli: --harness accepts any active harness (checked against
  /api/providers); MCP drops its static enum.
- tests: golden-tables.json pins the old tables' answers; new
  test_provider_accounts.py (migration, clean accounts, routing by vendor).

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

138 lines
5.7 KiB
Python

"""
Which logins a runner holds — the machine side of Profiles → Workers →
Accounts.
* **The lab** (``"local"``) holds every account whose login lives on the hub
— each Claude account is a config dir on this box (``~/.claude``,
``~/.claude-work``), the OpenRouter key is the hub's — every keyless one
(the EVOX2's local models) and the hosted logins its own sidecar declares
(the opencode / agy ones: ``SIDECAR_OPENCODE_ACCOUNTS`` /
``SIDECAR_AGY_ACCOUNTS``, as the login-health monitor last read them). A
guest's own login (``guest-<uid>``) is a lab login too, never listed.
* **A paired worker** holds the Claude logins it bills (proxied or its own)
and the opencode / agy accounts its ``/health`` declares.
Logins are keyed by **vendor** (``{anthropic: [...], openai: [...], google:
[...], evox2: [...], openrouter: [...]}``, :func:`logins`); the harness-keyed
view older callers read (``{claude: [...], opencode: [...], agy: [...],
pi: [...]}``) is :func:`harness_logins`. Which of a runner's logins a new
prompt bills is the profile's ★ for the vendor (``ProfileStore.default_login``).
"""
from __future__ import annotations
import accounts as accounts_mod
from profiles.store import LOCAL, by_vendor, lab_logins_static, worker_logins_by_harness
def _reg():
from plugins import providers
return providers.current()
def is_lab(runner: str | None) -> bool:
return not runner or runner == LOCAL
def _host(deps) -> set[str]:
health = getattr(deps, "account_health", None)
try:
return set(health.host_accounts()) if health else set()
except Exception:
return set()
def lab_harness_logins(deps) -> dict[str, list[str]]:
"""The lab's logins per harness: every account a non-hosted harness runs
(claude: the config dirs; pi: the EVOX2 and the OpenRouter key), and of a
hosted harness (opencode, agy) the ones its sidecar declares."""
reg = _reg()
host = _host(deps)
hosted = set(reg.hosted_harnesses())
return {h: [a["id"] for a in reg.accounts_for_harness(h)
if h not in hosted or a["id"] in host]
for h in reg.harness_ids()}
def lab_logins(deps) -> dict[str, list[str]]:
"""The lab's logins per vendor: what it holds before its sidecar answers
(:func:`lab_logins_static`) plus the hosted logins it declares."""
out = {v: list(a) for v, a in lab_logins_static().items()}
for v, accts in by_vendor(lab_harness_logins(deps)).items():
for a in accts:
if a not in out.setdefault(v, []):
out[v].append(a)
return out
def harness_logins(deps, runner: str | None) -> dict[str, list[str]]:
"""``{harness: [account ids]}`` the runner can run — the compat view
(empty lists for a runner nobody paired; pi runs on the lab only)."""
if is_lab(runner):
return lab_harness_logins(deps)
w = deps.worker_store.get(runner)
out = worker_logins_by_harness(w) if w else {}
return {h: list(out.get(h) or []) for h in _reg().harness_ids()}
def logins(deps, runner: str | None) -> dict[str, list[str]]:
"""``{vendor: [account ids]}`` the runner holds."""
if is_lab(runner):
return lab_logins(deps)
w = deps.worker_store.get(runner)
return by_vendor(worker_logins_by_harness(w)) if w else {}
def holds(deps, runner: str | None, account: str | None, harness: str | None = None) -> bool:
"""Whether ``runner`` can run ``account`` — on ``harness`` when given
(the account must be one the harness runs, and a hosted harness's login
one the machine declares for it)."""
a = accounts_mod.valid_for(account, harness) if harness else accounts_mod.valid(account)
if not a:
return False
guest = accounts_mod.guest_of(a)
if guest and (harness or "claude") == "claude":
if is_lab(runner):
return True
# A guest's own computer runs their own Claude on its own login
# (users/runs.run_on_computer, sidecar claude_accounts.machine_login).
from workers import guests
return guests.owner_of(deps.worker_store.get(runner)) == guest
if harness:
return a in harness_logins(deps, runner).get(harness, [])
return a in logins(deps, runner).get(accounts_mod.vendor_of_account(a) or "", [])
def runner_name(deps, runner: str | None) -> str:
if is_lab(runner):
return "the Homelab"
return (deps.worker_store.get(runner) or {}).get("name") or str(runner)
def profiles_payload(deps) -> list[dict]:
"""``/api/profiles``: the stored profiles, each runner entry with the
logins its machine holds right now (``logins`` — computed, never
stored)."""
return [with_computed(deps, p) for p in deps.profile_store.list()]
def with_computed(deps, p: dict) -> dict:
"""A profile as the API answers it: each runner entry with the logins its
machine holds (``logins``), the Guests profile with its ``members`` (the
guests' ids, Settings → Users) — computed, never stored."""
store = deps.profile_store
for e in p["workers"]:
by_h = harness_logins(deps, e["id"])
by_v = logins(deps, e["id"])
# vendor keys and (compat) harness keys side by side — they never collide
e["logins"] = {**by_h, **by_v}
e["overrides"] = dict(e.get("accounts") or {})
eff = store.effective_accounts(e["id"]) if not p.get("kind") else {}
stars = {h: store.default_login(e["id"], h, held) for h, held in by_h.items() if held}
e["accounts"] = {**{h: a for h, a in stars.items() if a}, **eff}
if p.get("kind") == "guests":
users = getattr(deps, "users", None)
p["members"] = sorted(u["id"] for u in (users.all() if users else [])
if u.get("id"))
return p