Files
loki/scripts/test
CatoandClaude Opus 5 b71941be63 feat(providers): one-tap switch to a provider that still has quota (#757)
* feat(providers): one-tap switch to a provider that still has quota

When the agent building a project hits a rate limit, the way out already
existed — four other agent CLIs, most with untouched quota. Taking it was
the problem. The only chooser lived in the Terminal right rail, it offered
every agent in a fixed order whether or not the connected builder had it
installed, and taking it swapped the live PTY without recording the choice,
so the next dispatch went straight back to the agent that had just run out.

One ranking, three surfaces:

- src/lib/provider-switch.ts — the whole decision as pure functions. Ranks
  the operator's order, filters on two pieces of OBSERVED evidence (the
  builder's installed-agent report, and recent runs that died with capacity
  language), and holds the three-state rule provider_quota is built on: an
  agent nothing was reported about is unknown, and unknown reads as usable.
  Nothing is hidden — a provider that cannot answer still appears, disabled,
  saying why. 18 inline checks in scripts/test/provider-switch.ts.
- GET /api/providers — that ranking, once, for every surface. Accepts either
  the user_projects id or the entity id, because the Feedback row holds one
  and Control/Terminal hold the other.
- ProviderSwitch — "Try <next>" plus a menu of the rest, now the PRIMARY
  control on a Needs-you / Failed feedback row (Retry steps down beside it),
  in the Control inbox, and in the Terminal rail.

And it sticks: POST /api/feedback/<id>/dispatch takes an `agent`, records it
as the project's agentPref, then dispatches on it — no scavenger hunt through
project settings.

The order is the operator's, not the fleet's: user_preferences.agent_order
(migration 0072, nullable and additive) with a reorder list in Settings →
Agent. Null means "never stated", and the fleet default applies.

Two SSOT repairs the work ran into:
- AGENT_FALLBACK_ORDER was defined TWICE (agent-registry.ts and
  agent-resolution.ts) — the server reroute and the client reroute each read
  their own copy. agent-registry now re-exports the client-safe leaf.
- QUOTA_ALTERNATIVE_AGENTS is derived from that order instead of being a
  second hand-written list of the same five ids.
- TerminalRunView.alternatives is gone; quotaDeath stays as the flag that
  asks the shared chooser.

provider_quota is deliberately NOT an input: it meters the ai-kit inference
chain (groq, openrouter) behind Loki's own chat, not the agent CLIs. The
agent-CLI equivalent is the run evidence above — observed, never probed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(providers): never offer the agent the project would actually run on

Self-review caught a gap in the chooser's exclusion rule. It read "current"
straight off `user_projects.agent_pref`, so a project that has never had a
preference set reported no current agent at all — and dispatch does not work
that way. Such a project still runs, on DEFAULT_ADAPTER_ID, which means the
escape offered from a Claude Code rate limit could be Claude Code.

currentProviderFor() answers the question the exclusion is actually asking —
which agent just died — in the order the evidence deserves:

  1. what this project LAST RAN on (freshest run inside the window). A stored
     preference can be stale by one switch; a run is what happened.
  2. else its stored preference.
  3. else the fleet default, because that is what the next dispatch picks.

A run on an agent the chooser cannot offer (openclaw) is not "current" — it
falls through rather than excluding nothing.

Three more checks in scripts/test/provider-switch.ts, 21 total.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 00:59:08 -07:00
..
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00