Files
gabrielvidalandClaude Opus 5.5 aee457cdbf Merge origin/main into hub-mcp-plugin
main moved cron jobs into the cron-jobs plugin (#37) and retired the core
Send later doors for the scheduled-conversations plugin's commands (#36),
both touching the core MCP files this branch moves into the hub-mcp plugin.

- The MCP stays out of core: backend/mcpserver/, cli/mcp_tools.py,
  cli/mcp_server.py, their tests and business/aiApps/ stay deleted (main's
  edits to them — no schedule group, no cron / schedule tools — are the
  plugin's to apply).
- Cron and Send later follow main: backend/cron/routes.py gone (its
  by-name worker pick is runs.start's at fire time now), no ai-agent
  later / scheduled / unschedule — so a worker's narrowed CLI drops them
  from its command lists (workers/cli.py) and schedules nothing; Full
  reaches the plugin's commands.
- authz: neither runs.schedule nor mcp.connect.
- Settings: neither Scheduled jobs nor AI apps in the registry; both old
  addresses redirect to their plugin's page.
- Regenerated openapi.json, src/generated, rolesCatalog.json,
  matrix.golden.txt and telemetry features.json.
- cli/test_library_surface.py: parse_when left with `ai-agent later`.

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

86 lines
3.6 KiB
Python

"""
The owner, narrowed to one of his workers — what a worker's own ``ai-agent``
CLI runs as under its *Read* and *Read + run here* switch (workers/cli.py:
the hub runs the command a session there dropped in its outbox, with these
headers, its mode the worker's *ai-agent CLI* switch). *Full* is not
narrowed: that is the owner's own CLI.
The CLI calls this backend's own API over loopback, where a caller is the
owner. A worker's calls add :data:`AS_WORKER_HEADER` (the worker's id) and
:data:`AS_WORKER_MODE_HEADER` (``read`` | ``run``), 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 (``new``) 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 worker on the *Full* switch, schedules.
Until 2026-11-28 this was also what an MCP connection **bound to a worker**
was (Settings → AI apps → *Acts as*). The MCP server is the ``hub-mcp``
plugin now and has no such binding: a worker drives the hub with the CLI, and
the bound connections were revoked when their data was carried over
(plugins/carryover.py ``hub_mcp``) — never left as plain owner connections.
"""
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:
"""One of the owner's own workers — never 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 (workers/picks.py). Any other is refused."""
from . import picks
want = (worker or "").strip()
if not want or (picks.find(deps.worker_store, want) or {}).get("id") == caller.via_worker:
return caller.via_worker
w = deps.worker_store.get(caller.via_worker) or {}
name = w.get("name") or caller.via_worker
raise Refused(403, {"message": f"a run started from {name} goes to {name} only — "
f"leave the worker unset, or name it",
"kind": "worker_elsewhere"})