Documentation
¶
Overview ¶
Package router resolves a task's `model` string to the CLI and model that will actually run it.
Ported from `orchestrator/router.py`. The contract it enforces (FR-D-6, AS-06) is the last thing standing between a typo in tasks.json and a subprocess spawned against the wrong provider: every `task.model` MUST be a key in model_router.yaml, checked before anything is dispatched.
Two design points carried over deliberately:
- The router absorbs drift; tasks.json is never edited to match it. A model the router does not know is fixed by adding a route, not by rewriting the task. `AddMissing` exists for exactly that.
- `is_premium` is recomputed from `tier` rather than trusted from the file. The YAML flag is a denormalised cache and config authors forget it; the tier is the truth.
Index ¶
- Variables
- func AddMissing(path string, keys []string, defaultTier model.Tier) (added, skipped []string, err error)
- func CLIModelWarning(key string, e model.RouteEntry) string
- func InferEntry(key string, defaultTier model.Tier) (model.RouteEntry, error)
- func OnPath(b model.Backend) bool
- func SetRoute(path, key string, backend model.Backend, cliModel string) error
- type Fallback
- type FormatError
- type Offender
- type Router
- func (r Router) CLIModelWarnings(tasks []model.Task) []string
- func (r Router) Fallbacks() []Fallback
- func (r Router) ForTier(tier model.Tier, installed func(model.Backend) bool) string
- func (r Router) Keys() []string
- func (r Router) MissingModels(tasks []model.Task) []string
- func (r Router) Validate(tasks []model.Task) error
- type UnroutedError
Constants ¶
This section is empty.
Variables ¶
var BackendPreference = []model.Backend{ model.BackendClaude, model.BackendCodex, model.BackendGemini, model.BackendOpencode, model.BackendAgy, model.BackendMuse, }
BackendPreference is every backend, in the order a route is picked in when several backends are installed: the CLIs most people have first.
var ErrCannotInfer = errors.New("cannot infer a route")
ErrCannotInfer means a route key does not follow the `<backend>/<model>` convention, so nothing can be guessed from it.
Functions ¶
func AddMissing ¶
func AddMissing(path string, keys []string, defaultTier model.Tier) (added, skipped []string, err error)
AddMissing appends inferred routes for every model in `keys` the file does not already have, and returns the keys it added plus the ones it could not infer.
It APPENDS raw YAML rather than re-dumping the parsed mapping. A round trip through a YAML encoder would drop the header comments that explain the file and reorder the hand-grouped entries — the file is read by people, and rewriting it is a worse outcome than the duplication of formatting rows by hand here.
Idempotent: a key already present is skipped, so running it twice adds nothing the second time.
func CLIModelWarning ¶
func CLIModelWarning(key string, e model.RouteEntry) string
CLIModelWarning describes a route whose cli_model its backend's CLI will not take, or returns "" when it will or the backend is not checked.
A warning and not an error: the rule is orch's knowledge, not the CLI's. No network call either — preflight stays offline.
func InferEntry ¶
InferEntry guesses a route from a `<backend>/<cli_model>` key.
`backend` and `cli_model` fall out of the key, the latter mapped to an id the CLI accepts where orch knows the backend's ids (`claude/sonnet-5` → `claude-sonnet-5`). A model it cannot map is kept as written; callers show CLIModelWarning for it. `tier` does not: it is a business decision about cost and capability that no model name reveals, so it defaults to standard — non-premium, and therefore not tripping the semi gate. Whatever writes these must tell the operator to review the tiers.
Returns ErrCannotInfer for a bare model name or an unknown backend prefix (`openai/...`). Those need a person, not a guess: writing a wrong route is worse than writing none, because it dispatches somewhere.
func OnPath ¶
OnPath reports whether a backend's CLI is on PATH; each backend's binary has the backend's name.
func SetRoute ¶
SetRoute points an existing route at another backend and CLI model: what `orch router suggest --apply` writes (#457).
Like AddMissing it edits the text rather than re-dumping the mapping, so the comments, the order and every other field survive: only the two values change, each on its own line, a trailing comment kept. A flow-style entry (`key: {backend: …}`) is refused rather than reformatted. The result is parsed again before it is written, so a value that would not read back leaves the file as it was.
Types ¶
type Fallback ¶
Fallback is one route that can substitute a different CLI model when the installed CLI has drifted from what the route names.
type FormatError ¶
FormatError is a model_router.yaml that does not describe routes: a bad backend, an unknown tier, a missing cli_model, a dangling escalation.
Separate from a missing route because the fix is different — this is a file to correct, not an entry to add.
func (*FormatError) Error ¶
func (e *FormatError) Error() string
type Router ¶
type Router map[string]model.RouteEntry
Router maps a tasks.json `model` string to its route.
func Load ¶
Load reads model_router.yaml and validates every row.
A missing file is an error here, unlike config.yaml: a project with tasks and no router cannot dispatch anything, and saying so at load time beats failing per-task later.
func Parse ¶
Parse is Load for a router already in memory — the packaged default, say; path only names it in errors.
func (Router) CLIModelWarnings ¶
CLIModelWarnings returns CLIModelWarning for every route the tasks use, once per route, sorted by key. Routes no task uses are not reported.
func (Router) Fallbacks ¶
Fallbacks lists the routes carrying a fallback_cli_model, sorted by key.
The caller announces these at startup. Note what this is NOT: FR-D-7 originally specified one WARN per substitution at startup, and Sprint C removed that — the announcement is informational, because the actual swap happens later in the reap loop when a dispatch fails with a version-drift error, and a WARN per route on every start spammed the console. Python prints one INFO summary line, or one INFO per route under -v. Reintroducing the WARN here would undo a deliberate decision, so this returns the data and leaves the level to the caller.
func (Router) ForTier ¶
ForTier is the route key of tier whose backend installed accepts — by BackendPreference, then by key — or "" when no installed backend has a route of that tier (#377).
func (Router) Keys ¶
Keys returns the route keys sorted, so every listing and error message is in the same order twice running.
func (Router) MissingModels ¶
MissingModels returns the distinct models tasks reference and the router lacks, sorted.
Distinct, not per-task: an UnroutedError prints a line per task, but twenty tasks usually share two missing models, and the thing being added is a model.
type UnroutedError ¶
type UnroutedError struct {
Offenders []Offender
}
UnroutedError lists every task whose model has no route.
Every offender, not the first: an operator fixing them one error message at a time would run orch once per typo. The list is sorted by task id so the output is stable enough to diff.
func (*UnroutedError) Error ¶
func (e *UnroutedError) Error() string