Files
gabrielvidalandClaude Opus 5.5 9da9a3d683 fix(workers): a run's worker by its name too — EVOX2 as well as a7e9d2a320e3
The MCP start_conversation and the CLI's --on have always said "a worker
id or name", and browse / ai-agent workers print the name first, but the
hub looked workers up by id only: worker='EVOX2' was a 400 "unknown
worker" and agents guessed their way to the id (the three relays of
2ca176fc, 2026-11-28).

workers/picks.py resolves every pick: local / lab / homelab are the lab,
a worker's id as is, else its name in any case and spacing among the
owner's workers (a guest's own computers for a guest's run). A name two
workers share names none. An unknown pick is a 400 worker_unknown whose
message lists the valid ids and names (and `choices`), so the next try is
the right one. Used by route_new_run (spawns, cron fires, the plugin runs
bridge), the cron routes (a job pinned by name stores the id) and the
runs bridge's worker check (names with spaces pass, "lab" is the lab).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 12:57:24 +01:00

89 lines
3.4 KiB
Python

"""
Which runner a run's ``worker`` pick names — its id, its name, or the lab.
The composer sends a worker's id; an agent types what it reads. The MCP
``start_conversation`` and the CLI's ``--on`` have always said "a worker id
or name", ``browse`` / ``ai-agent workers`` print the name first, and the
hub took the id only — so ``worker='EVOX2'`` was a 400 *unknown worker*,
and the agent guessed its way to ``a7e9d2a320e3`` (2026-11-28, the relays
of 2ca176fc). Every place that takes a pick — a new run
(``runs.service.route_new_run``), a cron job (``cron/routes.py``), a
connection bound to a worker (``workers/bound.py``) — reads it here:
* ``local``, ``lab`` and ``homelab`` (any case) are the lab;
* a worker's id, as is;
* else a worker's name, in any case and spacing — among the owner's own
workers, or a guest's own computers for a guest's run. A name two of them
share names none: the id is then the only way.
An unknown pick is a 400 ``worker_unknown`` whose message lists what would
have worked (``choices``: ``[{id, name}]``), so the next try is the right one.
"""
from __future__ import annotations
from fastapi import HTTPException
from . import guests
# runs.service.LOCAL_RUNNER — the lab, as a run's ``worker`` says it.
LAB = "local"
LAB_NAMES = frozenset({"local", "lab", "homelab"})
def _norm(name: str | None) -> str:
return " ".join((name or "").split()).casefold()
def is_lab(pick: str | None) -> bool:
return _norm(pick) in LAB_NAMES
def _mine(store, guest: str | None) -> list[dict]:
"""The workers a pick may name by name: the owner's own (``guest`` None)
or that guest's computers."""
return [w for w in store.list() if guests.owner_of(w) == guest]
def find(store, pick: str | None, *, guest: str | None = None) -> dict | None:
"""The worker ``pick`` names — by id (any worker: who may run there is
the caller's check), else by name among ``guest``'s own; None when it
names none, or two."""
want = (pick or "").strip()
if not want:
return None
w = store.get(want)
if w:
return w
key = _norm(want)
hits = [w for w in _mine(store, guest) if _norm(w.get("name")) == key]
return hits[0] if len(hits) == 1 else None
def choices(store, guest: str | None = None) -> list[dict]:
"""What a pick may say: the lab (not for a guest) and each worker."""
rows = [] if guest else [{"id": LAB, "name": "lab"}]
return rows + [{"id": w["id"], "name": w.get("name") or w["id"]}
for w in _mine(store, guest)]
def resolve(store, pick: str | None, *, guest: str | None = None) -> str:
"""``pick`` as a runner id: ``""`` for none, :data:`LAB` for the lab, else
the worker's id — or a 400 ``worker_unknown`` naming the valid ones."""
want = (pick or "").strip()
if not want:
return ""
if is_lab(want):
return LAB
w = find(store, want, guest=guest)
if w:
return w["id"]
rows = choices(store, guest)
shared = [r["id"] for r in rows if _norm(r["name"]) == _norm(want)]
listed = ", ".join(f"{r['id']} ({r['name']})" if r["name"] != r["id"] else r["id"]
for r in rows) or "none"
why = (f"{want!r} names {len(shared)} workers ({', '.join(shared)}) — use its id"
if len(shared) > 1 else f"unknown worker: {want!r}")
raise HTTPException(400, {"message": f"{why}. Pick an id or a name: {listed}",
"kind": "worker_unknown", "choices": rows})