router

package
v0.31.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

BackendPreference is every backend, in the order a route is picked in when several backends are installed: the CLIs most people have first.

View Source
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

func InferEntry(key string, defaultTier model.Tier) (model.RouteEntry, error)

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

func OnPath(b model.Backend) bool

OnPath reports whether a backend's CLI is on PATH; each backend's binary has the backend's name.

func SetRoute

func SetRoute(path, key string, backend model.Backend, cliModel string) error

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

type Fallback struct {
	Key      string
	CLIModel string
	Fallback string
}

Fallback is one route that can substitute a different CLI model when the installed CLI has drifted from what the route names.

type FormatError

type FormatError struct {
	Path string
	Msg  string
}

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 Offender

type Offender struct {
	TaskID string
	Model  string
}

Offender is one task pointing at a model the router does not know.

type Router

type Router map[string]model.RouteEntry

Router maps a tasks.json `model` string to its route.

func Load

func Load(path string) (Router, error)

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

func Parse(path string, raw []byte) (Router, error)

Parse is Load for a router already in memory — the packaged default, say; path only names it in errors.

func (Router) CLIModelWarnings

func (r Router) CLIModelWarnings(tasks []model.Task) []string

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

func (r Router) Fallbacks() []Fallback

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

func (r Router) ForTier(tier model.Tier, installed func(model.Backend) bool) string

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

func (r Router) Keys() []string

Keys returns the route keys sorted, so every listing and error message is in the same order twice running.

func (Router) MissingModels

func (r Router) MissingModels(tasks []model.Task) []string

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.

func (Router) Validate

func (r Router) Validate(tasks []model.Task) error

Validate fails fast when any task references a model the router lacks.

Returns an *UnroutedError naming every offender. This is AS-06, and it runs before a single subprocess is spawned.

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL