100 lines
4.1 KiB
Python
100 lines
4.1 KiB
Python
"""What a claude run is launched with on top of its prompt: an appended system
|
|
prompt and subagent definitions (``claude --agents``).
|
|
|
|
* every run — the ``clarifier`` (runs/clarifier.py), which asks the user a
|
|
form so the main thread doesn't carry it;
|
|
* a lab run (not a guest's) — the homelab agents marked ``portable: true``
|
|
(workers/labsync.py), whatever the run's cwd. A worker gets those as files
|
|
through the lab sync instead, so its runs carry nothing extra.
|
|
|
|
The sidecar caps the ``--agents`` payload; an older one at 5 agents / 16 000
|
|
chars, a newer one says its own limits in ``/health`` (``agentsMax``). The
|
|
clarifier comes first; a portable agent that doesn't fit the rest of the
|
|
budget is left out of that run.
|
|
|
|
(Work runs used to get a Haiku ``work-context`` agent here, rendered from the
|
|
profile's resources block. Since 2026-11-05 it lives on the Orus MacBook
|
|
itself — an opt-in ``~/.claude/agents/work-context.md`` — and the hub injects
|
|
nothing work-specific.)
|
|
|
|
It also refuses a definition key it doesn't know (400, the whole run). One
|
|
from 2026-11-04 on lists the keys it takes in ``/health`` (``agentKeys``, the
|
|
lab's read here, a worker's from its last probe); a definition sent to one
|
|
that doesn't list them loses ``omitClaudeMd`` / ``effort`` — the clarifier as
|
|
it was — rather than the run failing until that worker updates.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import time
|
|
|
|
from plugins import agents as plugin_agents
|
|
from workers import labsync
|
|
|
|
OLD_MAX = {"count": 5, "chars": 16000}
|
|
# The definition keys every sidecar takes (one without ``agentKeys`` in /health).
|
|
OLD_KEYS = frozenset({"description", "prompt", "model", "tools", "disallowedTools"})
|
|
_health: list = [0.0, None] # [fetched at, {"budget", "keys"}]
|
|
|
|
|
|
def _lab_health() -> dict:
|
|
"""The host sidecar's ``agentsMax`` and ``agentKeys`` (cached a minute)."""
|
|
from runs.sidecar_client import _sidecar_get
|
|
if time.time() - _health[0] > 60 or _health[1] is None:
|
|
h = _sidecar_get("/health", timeout=3) or {}
|
|
m = h.get("agentsMax")
|
|
ok = isinstance(m, dict) and all(isinstance(m.get(k), int) for k in OLD_MAX)
|
|
_health[:] = [time.time(), {"budget": {k: m[k] for k in OLD_MAX} if ok else dict(OLD_MAX),
|
|
"keys": _keys(h.get("agentKeys"))}]
|
|
return _health[1]
|
|
|
|
|
|
def _lab_budget() -> dict:
|
|
return _lab_health()["budget"]
|
|
|
|
|
|
def _keys(raw) -> frozenset:
|
|
"""A sidecar's ``agentKeys`` — the old set when it says none."""
|
|
if isinstance(raw, list) and all(isinstance(k, str) for k in raw):
|
|
return frozenset(raw) | OLD_KEYS
|
|
return OLD_KEYS
|
|
|
|
|
|
def _accepted(agents: dict, keys: frozenset) -> dict:
|
|
"""Each definition without the keys the target sidecar would refuse."""
|
|
return {name: {k: v for k, v in d.items() if k in keys} for name, d in agents.items()}
|
|
|
|
|
|
def _fit(agents: dict, extra: dict, budget: dict) -> dict:
|
|
"""``agents`` plus as many of ``extra`` (in order) as the budget holds."""
|
|
out = dict(agents)
|
|
for name, d in extra.items():
|
|
if name in out:
|
|
continue
|
|
trial = {**out, name: d}
|
|
if len(trial) > budget["count"] or len(json.dumps(trial)) > budget["chars"]:
|
|
continue
|
|
out = trial
|
|
return out
|
|
|
|
|
|
def run_extras(deps, account: str | None, worker: str | None,
|
|
cwd: str | None, *, scoped: bool = False,
|
|
person: str | None = None) -> dict:
|
|
"""``{appendSystemPrompt?, agents?}`` for a claude run (``{}`` = none).
|
|
``scoped``: a guest's run (gets active plugin subagents only, e.g. the
|
|
``clarifier`` plugin). ``person``: whom the run is for (``runAs``) — their
|
|
own plugin preferences on top of the operator's defaults."""
|
|
out: dict = {}
|
|
agents = dict(plugin_agents.agent_definitions_for(deps, person))
|
|
if not worker and not scoped:
|
|
portable = labsync.agent_definitions()
|
|
if portable:
|
|
agents = _fit(agents, portable, _lab_budget())
|
|
if agents:
|
|
keys = (_keys(deps.worker_store.status(worker).get("agentKeys")) if worker
|
|
else _lab_health()["keys"])
|
|
out["agents"] = _accepted(agents, keys)
|
|
return out
|