Files
pipelines/internal/pipeline/overrides.go
T
alan fae1005962 Allow launching runs with whole-run and per-stage model overrides
Add `pipelines run --model PROVIDER/MODEL` and repeatable
`--stage-model STAGE=PROVIDER/MODEL`, resolved with precedence
per-stage > whole-run > the model pinned in the pipeline version.

- A shared pure resolver (`pipeline.ParseModelOverride` /
  `pipeline.EffectiveModels`) owns the precedence and validation: every value
  must be a `provider/model` pair (`stage.ValidModel`), every stage key must
  exist in the pinned graph, an unknown key fails naming the valid keys, and a
  per-stage key that names a non-model stage is refused.
- The server validates against the pinned version before admission (no run row
  is ever inserted for an invalid request), persists `model`/`stage_models` on
  the run and `provider`/`model` on each step, applies the effective pair to a
  clone of the stage definition so the stage and its repair/formatter path share
  it, overlays the returned run graph with the effective pair (presentation
  only), and reuses the recorded overrides on retry.
- The run record echoes the overrides it was launched with and each step records
  the provider/model it actually ran with; the TUI Evidence pane renders that
  effective pair. The pinned pipeline version and `show --version N` are never
  modified.
- CLI help, `docs/cli.md` and the packaged authoring skill references document
  the flags and the precedence. A TUI override picker remains a documented
  follow-up; the CLI is the deliverable.

Tests: pure precedence/validation table tests, store round-trip and
request-key-mismatch plus migration, server admission/application/overlay/retry
with a controlled executor, CLI flag/help/malformed-value tests, and a TUI
evidence test.
2026-10-16 23:33:53 +01:00

98 lines
3.3 KiB
Go

package pipeline
import (
"fmt"
"sort"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
)
// ModelOverride is a provider/model pair chosen for a run.
type ModelOverride struct {
Provider string
Model string
}
// String renders the pair as provider/model.
func (m ModelOverride) String() string { return m.Provider + "/" + m.Model }
// ParseModelOverride parses a provider/model value and validates both sides with
// stage.ValidModel. It is the one parser shared by launch-time resolution and the
// CLI's pre-flight form check, so the two cannot drift.
func ParseModelOverride(value string) (ModelOverride, error) {
provider, model, ok := strings.Cut(strings.TrimSpace(value), "/")
provider, model = strings.TrimSpace(provider), strings.TrimSpace(model)
if !ok || !stage.ValidModel(provider, model) {
return ModelOverride{}, fmt.Errorf("model override %q must be provider/model", value)
}
return ModelOverride{Provider: provider, Model: model}, nil
}
// EffectiveModels resolves the provider/model each model stage will run with for
// one run. Precedence: a per-stage override beats the whole-run override, which
// beats the model pinned in the pipeline version. It is pure and validates the
// overrides against the pinned graph: an unknown stage key fails naming the valid
// keys, a value that is not a provider/model fails, and a per-stage key for a
// non-model stage fails because that stage could never honour it. A whole-run
// override on a graph with no model stage is accepted and inert. The helper is
// shared by the server (authoritative, against the pinned version) and the CLI
// (pre-flight form check), so the precedence rule lives once.
func EffectiveModels(graph protocol.Graph, model string, stageModels map[string]string) (map[string]ModelOverride, error) {
keys := make([]string, 0, len(graph.Stages))
byKey := make(map[string]protocol.Stage, len(graph.Stages))
for _, s := range graph.Stages {
keys = append(keys, s.Key)
byKey[s.Key] = s
}
sort.Strings(keys)
var whole *ModelOverride
if strings.TrimSpace(model) != "" {
parsed, err := ParseModelOverride(model)
if err != nil {
return nil, err
}
whole = &parsed
}
perStage := make(map[string]ModelOverride, len(stageModels))
overrideKeys := make([]string, 0, len(stageModels))
for key := range stageModels {
overrideKeys = append(overrideKeys, key)
}
sort.Strings(overrideKeys)
for _, key := range overrideKeys {
declared, exists := byKey[key]
if !exists {
return nil, fmt.Errorf("unknown stage key %q; valid keys: %s", key, strings.Join(keys, ", "))
}
if stage.EffectiveType(declared.Type) != stage.ModelType {
return nil, fmt.Errorf("stage %q cannot honour a model override; it is not a model stage", key)
}
parsed, err := ParseModelOverride(stageModels[key])
if err != nil {
return nil, fmt.Errorf("stage %q: %w", key, err)
}
perStage[key] = parsed
}
effective := make(map[string]ModelOverride, len(graph.Stages))
for _, s := range graph.Stages {
if stage.EffectiveType(s.Type) != stage.ModelType {
continue
}
if override, ok := perStage[s.Key]; ok {
effective[s.Key] = override
continue
}
if whole != nil {
effective[s.Key] = *whole
continue
}
effective[s.Key] = ModelOverride{Provider: s.Configs["provider"], Model: s.Configs["model"]}
}
return effective, nil
}