86 lines
3.5 KiB
Python
86 lines
3.5 KiB
Python
"""
|
|
The owner, narrowed to one of his workers — what an MCP connection **bound
|
|
to a worker** is (Settings → AI apps → a connection → *Acts as*), so a
|
|
Claude Code on the Orus MacBook can drive the hub through the MCP server
|
|
without seeing the lab's other profiles.
|
|
|
|
The MCP tools call this backend's own API over loopback (mcpserver/), where a
|
|
caller is the owner. A bound connection's calls add :data:`AS_WORKER_HEADER`
|
|
(the worker's id) and :data:`AS_WORKER_MODE_HEADER` (``read`` for a read-only
|
|
connection, ``run`` for a full one), and the gate (core/auth.py) answers them
|
|
as :func:`worker_caller` — a downgrade of a caller already trusted, never an
|
|
upgrade:
|
|
|
|
* it holds the ``worker.read`` or ``worker.run`` credential scope
|
|
(authz/scopes.py): reading conversations, workers and plan usage, plus
|
|
starting and renaming under ``run``;
|
|
* reads see the conversations of the worker's own profile only
|
|
(users/access.py, ``Caller.profile``);
|
|
* writes stay on that worker: a new run (``start_conversation``) starts
|
|
there, and send / fork / stop / rename / archive only touch conversations
|
|
that live there.
|
|
|
|
It can't schedule anything (Send later is the ``scheduled-conversations``
|
|
plugin's, whose commands need ``plugins.use`` — a scope this credential never
|
|
holds): the owner's own CLI, or a full connection, schedules.
|
|
|
|
A worker's own sessions don't call the hub at all (it pulls them); their
|
|
``ai-agent`` CLI keeps only ``past``, through the outbox
|
|
(conversations/past.py ``outbox_lookup``), narrowed the same way.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import dataclasses
|
|
|
|
from users.caller import OWNER, Caller
|
|
|
|
AS_WORKER_HEADER = "x-aia-as-worker"
|
|
AS_WORKER_MODE_HEADER = "x-aia-as-worker-mode"
|
|
|
|
READ, RUN = "read", "run"
|
|
MODES = (READ, RUN)
|
|
|
|
# The fields a bound connection's rename / archive may set.
|
|
META_FIELDS = frozenset({"id", "title", "titleSource", "archived"})
|
|
|
|
|
|
class Refused(Exception):
|
|
def __init__(self, status: int, detail):
|
|
super().__init__(str(detail))
|
|
self.status = status
|
|
self.detail = detail
|
|
|
|
|
|
def bindable(w: dict | None) -> bool:
|
|
"""A connection can be bound to one of the owner's workers — never to a
|
|
guest's computer."""
|
|
return bool(w) and not w.get("owner")
|
|
|
|
|
|
def worker_caller(deps, wid: str, mode: str | None = READ) -> Caller:
|
|
"""The owner, narrowed to worker ``wid``'s profile, that worker's runs and
|
|
``mode``'s routes. :class:`Refused` when the worker is unknown or a
|
|
guest's."""
|
|
w = deps.worker_store.get((wid or "").strip())
|
|
if not bindable(w):
|
|
raise Refused(403, "unknown worker")
|
|
profile = deps.profile_store.for_runner(w["id"])["id"]
|
|
mode = mode if mode in MODES else READ
|
|
return dataclasses.replace(OWNER, via_worker=w["id"], profile=profile,
|
|
worker_mode=mode).narrowed(f"worker.{mode}")
|
|
|
|
|
|
def pin_worker(deps, caller: Caller, worker: str | None) -> str:
|
|
"""The runner a bound caller's new run goes to: that worker — unset, or
|
|
named by its id or its name. Any other is refused."""
|
|
want = (worker or "").strip()
|
|
if not want or want == caller.via_worker:
|
|
return caller.via_worker
|
|
w = deps.worker_store.get(caller.via_worker) or {}
|
|
if want.lower() == (w.get("name") or "").strip().lower():
|
|
return caller.via_worker
|
|
raise Refused(403, {"message": f"this connection starts runs on "
|
|
f"{w.get('name') or caller.via_worker} only",
|
|
"kind": "worker_elsewhere"})
|