Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions docs/SWARM-DELEGATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,29 @@ carrying a name; nothing resolves, routes, or activates a route at runtime.
bytes exactly, so peers on the older vocabulary are unaffected. The wire carries the name;
nothing on the wire resolves it.

## Runtime prerequisite (router helpers, D02 not yet active)

The router side now has the primitives the later activation slice will build on, but nothing calls
them outside the router's own tests:

- **`Router.stream(..., _exact="provider:model")`** (private, optional): pins the call to the one
configured route the ref names. The chain is that route alone — no fallback to any other route,
in this role or another — while the ordinary credential rotation, cooldowns, circuit breaker and
spend guard of this Router all still apply. A bad ref raises `ValueError` before anything is
streamed.
- **`Router.provider_for(ref, role="delegate")`**: a `RoleProvider` for one route on this same
Router (one shared cooldown, breaker and spend state; default `provider()` is unchanged). It
validates the ref up front — shape, known provider, and the provider's `roles` table permitting
`role` — and raises `ValueError` before any credential is read.
- Helpers (`_exhausted`, `_chain_cooldowns`, `_refused`) now scope their wait hints and refusals to
the chain the call actually walked, so a pinned failure speaks only for its route.

## What is deliberately NOT built (later slices)

- No runtime activation: no code path reads `route_config.py` yet; `Delegator`, the router, and
the child loop ignore the `route` field.
- No provider/catalog validation of a ref, no credential handling, no `profile_routes`.
- No resolution or fallback behavior for a failed route.
- No runtime activation: no code path reads `route_config.py` yet, and delegation itself does not
use the router helpers above; `Delegator` and the child loop ignore the `route` field.
- No resolution, fallback, or profile_routes behavior for a failed route; `provider_for` is not
called by any delegation path.

D02 is incomplete until that runtime slice lands. Until then, setting `delegation.routes` in a
config file parses (see `tests/agents/test_route_config.py`) and does nothing else.
Expand Down
67 changes: 52 additions & 15 deletions src/exo/core/models/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@ def _allows(self, provider: str, role: str) -> bool:
allowed = (self.providers.get(provider) or {}).get("roles")
return not allowed or role in allowed

def pinned(self, ref: str, role: str = "delegate") -> Route:
"""The one route ``ref`` names, checked against the configured providers and the role's limits.
Raises ``ValueError`` for a bad shape, an unknown provider, or a provider that may not serve ``role``."""
if not ref or ref.startswith("-") or any(c.isspace() for c in ref):
raise ValueError(f"bad model ref {ref!r}")
route = Route.parse(ref)
if route.provider not in self.providers and route.provider not in self.instances:
raise ValueError(f"unknown provider {route.provider!r} in {ref!r}")
if not self._allows(route.provider, role):
raise ValueError(f"provider {route.provider!r} cannot serve role {role!r}")
return route

def _chain_for(self, role: str, ref: str | None = None) -> list[Route]:
"""The chain for ``role``, or only the route ``ref`` names: an exact-route call never falls back."""
return self.chain(role) if ref is None else [self.pinned(ref, role)]

def tier_route(self, tier: str) -> Route | None:
"""First route of ``tier`` (then its fallbacks) whose provider has credentials, else None."""
return next((r for r in self.chain(tier) if self.credentials(r.provider)), None)
Expand Down Expand Up @@ -413,16 +429,23 @@ def price(self, route: Route, usage: Usage | None) -> Usage | None:

# --- streaming -----------------------------------------------------------
async def stream(self, role: str, messages: list[Message], tools: list[ToolSpec],
**opts: Any) -> AsyncIterator[ProviderEvent]:
*, _exact: str | None = None, **opts: Any) -> AsyncIterator[ProviderEvent]:
"""Stream from the first available route of ``role``, rotating sibling credentials
then falling back to later routes until output has started. Ends with exactly one
``done`` or ``error``."""
``done`` or ``error``.

``_exact`` (private, D02 prerequisite): pin this call to the one route named
``"provider:model"``. No other configured route is tried, in this role or another. The
route keeps the ordinary credential rotation, cooldowns, circuit breaker and spend guard.
An invalid ref raises ``ValueError`` before anything is streamed."""
if _exact is not None:
self.pinned(_exact, role) # raises before anything is streamed; also limits the chain below
tried: list[str] = []
attempts: list[dict[str, Any]] = [] # every try, for the model.call record (D-025)
last: ProviderEvent | None = None
failed_credentials: set[str] = set()
attempted_credentials: set[tuple[str, str]] = set()
chain = self.chain(role)
chain = self._chain_for(role, _exact)
# Breaker state is cheap; resolve credentials only when reaching a route, off the event loop.
skip = {r.ref for r in chain if self.route_open(r.ref, self.clock())}
pending_routes, i = chain, 0
Expand Down Expand Up @@ -519,7 +542,7 @@ async def stream(self, role: str, messages: list[Message], tools: list[ToolSpec]
yield ProviderEvent(kind="notice", text=f"{route.ref} failed repeatedly; skipping it for {cs / 60:.0f} min")
if attempts and attempts[-1]["outcome"] == "fallback":
attempts[-1]["outcome"] = "error" # normal routes and the optional last resort are exhausted
end = self._exhausted(role, tried, last)
end = self._exhausted(role, tried, last, _chain=chain)
end.attempts = attempts
yield end

Expand Down Expand Up @@ -591,29 +614,34 @@ def note(outcome: str, err: str = "") -> None:
ev.message.meta.setdefault("model", route.ref)
yield ev

def _exhausted(self, role: str, tried: list[str], last: ProviderEvent | None) -> ProviderEvent:
def _exhausted(self, role: str, tried: list[str], last: ProviderEvent | None,
*, _chain: list[Route] | None = None) -> ProviderEvent:
"""The closing event. ``_chain`` is the chain this call actually walked (exact route: one route), so the
wait hint and the refusal speak only for that route."""
chain = self.chain(role) if _chain is None else _chain
now = self.clock()
waits = [until - now for until, _, _ in self._chain_cooldowns(role, now)]
waits = [until - now for until, _, _ in self._chain_cooldowns(role, now, _chain=chain)]
wait = min(waits) if waits else None
if not self.chain(role):
if not chain:
return error_event(f"no model configured for role {role!r}; use /providers and choose a default model",
None, retryable=False)
if not tried and not waits:
return error_event(f"no credentials for role {role!r}: set the provider key_env variables, "
"or use /providers to add a provider and choose its model",
None, retryable=False)
if refused := self._refused(role, tried, last, now): # no retry now, say what to fix; retry_after: it reopens
if refused := self._refused(role, tried, last, now, _chain=chain): # no retry now, say what to fix
return error_event(refused[1], refused[0], retryable=False, retry_after=wait)
why = f"; last error: {last.error}" if last is not None else ""
hint = f"; next route reopens in {wait:.0f}s" if wait else ""
return error_event(f"all routes for {role!r} failed or are cooling down{why}{hint}",
last.status if last is not None else 429, retryable=True, retry_after=wait)

def _chain_cooldowns(self, role: str, now: float) -> list[tuple[float, str, Cooldown]]:
def _chain_cooldowns(self, role: str, now: float, *,
_chain: list[Route] | None = None) -> list[tuple[float, str, Cooldown]]:
"""Live cooldowns that bear on ``role``: a credential of a chain provider, or a credential|model cooldown on a
model of the chain. A gate side call (D-081) cools a model this role never asks, and another provider's
cooldown says nothing about this role's routes, so neither moves the wait hint or the refusal."""
routes = {(r.provider, r.model) for r in self.chain(role)}
routes = {(r.provider, r.model) for r in (self.chain(role) if _chain is None else _chain)}
out: list[tuple[float, str, Cooldown]] = []
for key, cd in self.cooldowns.items():
cred, scoped, model = key.partition("|")
Expand All @@ -623,12 +651,12 @@ def _chain_cooldowns(self, role: str, now: float) -> list[tuple[float, str, Cool
return out

def _refused(self, role: str, tried: list[str], last: ProviderEvent | None,
now: float) -> tuple[int | None, str] | None:
now: float, *, _chain: list[Route] | None = None) -> tuple[int | None, str] | None:
"""Status and message when waiting cannot help: the provider refused the key or login (401, 403), or the key is
over its spend limit (402) which a new key does not fix; on a later call too, while that credential, or a model
of the chain on it, reopens first; or a local server is not running."""
if last is None: # nothing ran: the first of this chain's cooldowns to reopen holds the cause
cooling = self._chain_cooldowns(role, now)
cooling = self._chain_cooldowns(role, now, _chain=_chain)
_, name, cd = min(cooling, key=lambda c: c[0], default=(0.0, "", Cooldown()))
status, err = cd.status, cd.error or f"HTTP {cd.status}"
else:
Expand All @@ -650,6 +678,14 @@ def _refused(self, role: str, tried: list[str], last: ProviderEvent | None,
def provider(self, role: str = "main") -> RoleProvider:
return RoleProvider(self, role)

def provider_for(self, ref: str, role: str = "delegate") -> RoleProvider:
"""A Provider for the one route ``ref`` names (D02 prerequisite). Raises ``ValueError`` before any
credential is read, for a bad shape, a provider that is not configured, or a provider whose ``roles``
table may not serve ``role``. Uses this same Router (one cooldown, breaker and spend state) with
``role`` as the stream role, so it never falls back to another route."""
self.pinned(ref, role)
return RoleProvider(self, role, exact=ref)

def status(self) -> list[dict[str, Any]]:
"""Routes per role with availability, for ``models.list``."""
now, out = self.clock(), []
Expand Down Expand Up @@ -684,13 +720,14 @@ def resolved_roles(self) -> dict[str, list[str]]:
class RoleProvider:
"""A Router bound to one role; satisfies the ``Provider`` protocol for the loop."""

def __init__(self, router: Router, role: str) -> None:
def __init__(self, router: Router, role: str, *, exact: str | None = None) -> None:
self.router = router
self.role = role
self.name = f"router:{role}"
self.exact = exact
self.name = f"router:{role}" if exact is None else f"router:{role}:{exact}"

def stream(self, messages: list[Message], tools: list[ToolSpec], **opts: Any) -> AsyncIterator[ProviderEvent]:
return self.router.stream(self.role, messages, tools, **opts)
return self.router.stream(self.role, messages, tools, _exact=self.exact, **opts)


class ModelError(RuntimeError):
Expand Down
Loading
Loading