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.
This commit is contained in:
2026-10-16 23:33:53 +01:00
parent 80232a4ffe
commit fae1005962
19 changed files with 912 additions and 31 deletions
+76 -5
View File
@@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
@@ -43,6 +44,41 @@ type options struct {
json, source, noOpen, yes, allow, createOnly, force, wait bool
version, expected, limit int
timeout, waitTimeout time.Duration
stageModels stageModelFlags
}
// stageModelFlags collects the repeatable --stage-model STAGE=PROVIDER/MODEL
// values. A repeated key is last-wins. It is a flag.Value so the repeatable flag
// needs no dependency (Go stdlib has no slice flag).
type stageModelFlags map[string]string
// String renders the collected overrides for flag's defaults display.
func (s stageModelFlags) String() string {
keys := make([]string, 0, len(s))
for key := range s {
keys = append(keys, key)
}
sort.Strings(keys)
parts := make([]string, 0, len(keys))
for _, key := range keys {
parts = append(parts, key+"="+s[key])
}
return strings.Join(parts, ",")
}
// Set parses one STAGE=PROVIDER/MODEL value and validates the provider/model with
// the shared parser, so a malformed value fails before any run is created.
func (s stageModelFlags) Set(value string) error {
key, model, ok := strings.Cut(value, "=")
key, model = strings.TrimSpace(key), strings.TrimSpace(model)
if !ok || key == "" || model == "" {
return fmt.Errorf("--stage-model requires STAGE=PROVIDER/MODEL")
}
if _, err := pipeline.ParseModelOverride(model); err != nil {
return fmt.Errorf("--stage-model %s: %w", key, err)
}
s[key] = model
return nil
}
func (a App) flags(command string) (*flag.FlagSet, *options) {
@@ -98,6 +134,9 @@ func (a App) flags(command string) (*flag.FlagSet, *options) {
f.StringVar(&o.paths, "write-paths", "", "Optional narrower edit scope; default is all files in the run directory")
f.StringVar(&o.key, "request-key", "", "Idempotency key (default: generated unique key)")
f.IntVar(&o.version, "version", 0, "Version to run (default: latest)")
f.StringVar(&o.model, "model", "", "Override the model for every stage of this run (PROVIDER/MODEL)")
o.stageModels = stageModelFlags{}
f.Var(o.stageModels, "stage-model", "Override one stage's model; repeatable (STAGE=PROVIDER/MODEL); beats --model")
f.BoolVar(&o.wait, "wait", false, "In JSON mode, wait for a terminal run state")
f.DurationVar(&o.waitTimeout, "wait-timeout", 0, "Client limit for --wait after the run is admitted (0 waits until terminal); separate from per-request --timeout")
case "runs":
@@ -278,10 +317,11 @@ func (a App) Run(ctx context.Context, args []string) error {
if len(pos) != 0 {
return fmt.Errorf("provider-check takes no positional arguments")
}
provider, model, ok := strings.Cut(o.model, "/")
if !ok || !stage.ValidModel(provider, model) {
override, err := pipeline.ParseModelOverride(o.model)
if err != nil {
return fmt.Errorf("--model requires provider/model")
}
provider, model := override.Provider, override.Model
if err := a.confirmAction("Validate "+o.model+" with one small tool-free generation (provider charges may apply)?", o.yes); err != nil {
return err
}
@@ -1117,6 +1157,14 @@ func (a App) run(ctx context.Context, pos []string, o *options, call callFunc, r
paths = append(paths, p)
}
}
// Pre-flight form validation of the whole-run override with the same parser
// the server uses; stage-key existence stays server-authoritative. A malformed
// value fails here, before any confirmation or run request.
if o.model != "" {
if _, err := pipeline.ParseModelOverride(o.model); err != nil {
return err
}
}
if o.yes && (o.name == "" || strings.TrimSpace(o.prompt) == "") {
return fmt.Errorf("--yes requires a pipeline and nonempty prompt")
}
@@ -1131,7 +1179,7 @@ func (a App) run(ctx context.Context, pos []string, o *options, call callFunc, r
if o.wait || o.key != "" || o.version > 0 {
return fmt.Errorf("--wait, --request-key and --version require --json")
}
_, err := tea.NewProgram(tui.NewRun(ctx, o.url, tui.RunOptions{Pipeline: o.name, Prompt: o.prompt, Title: o.title, Workspace: workspace, Yes: o.yes, AllowTools: o.allow, WritePaths: paths}), tea.WithContext(ctx), tea.WithAltScreen()).Run()
_, err := tea.NewProgram(tui.NewRun(ctx, o.url, tui.RunOptions{Pipeline: o.name, Prompt: o.prompt, Title: o.title, Workspace: workspace, Yes: o.yes, AllowTools: o.allow, WritePaths: paths, Model: o.model, StageModels: o.stageModels}), tea.WithContext(ctx), tea.WithAltScreen()).Run()
return err
}
if o.name == "" || strings.TrimSpace(o.prompt) == "" {
@@ -1145,20 +1193,43 @@ func (a App) run(ctx context.Context, pos []string, o *options, call callFunc, r
if version == 0 {
version = p.Version
}
if err := a.confirmAction(fmt.Sprintf("Run %s v%d in %s? This may edit files, execute commands and publish notifications.", p.Name, version, workspace), o.yes); err != nil {
confirmation := fmt.Sprintf("Run %s v%d in %s? This may edit files, execute commands and publish notifications.", p.Name, version, workspace) + o.modelSummary()
if err := a.confirmAction(confirmation, o.yes); err != nil {
return err
}
key := o.key
if key == "" {
key = fmt.Sprintf("cli-%d-%d", os.Getpid(), time.Now().UnixNano())
}
r, err := call(protocol.Message{Type: protocol.RunPipeline, PipelineID: p.ID, Version: version, RequestKey: key, Workspace: workspace, Title: o.title, Input: map[string]any{"prompt": o.prompt}, AllowTools: o.allow, WritePaths: paths})
r, err := call(protocol.Message{Type: protocol.RunPipeline, PipelineID: p.ID, Version: version, RequestKey: key, Workspace: workspace, Title: o.title, Input: map[string]any{"prompt": o.prompt}, AllowTools: o.allow, WritePaths: paths, Model: o.model, StageModels: o.stageModels})
if err != nil {
return err
}
return a.finishRun(ctx, r, o, call)
}
// modelSummary describes the launch-time model overrides for the action
// confirmation, so a model mutation is visible before approval. It is empty when
// no override was supplied.
func (o *options) modelSummary() string {
if o.model == "" && len(o.stageModels) == 0 {
return ""
}
parts := []string{}
if o.model != "" {
parts = append(parts, "all stages: "+o.model)
}
keys := make([]string, 0, len(o.stageModels))
for key := range o.stageModels {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
parts = append(parts, key+": "+o.stageModels[key])
}
return " Model overrides — " + strings.Join(parts, "; ") + "."
}
func (a App) finishRun(ctx context.Context, r protocol.Message, o *options, call callFunc) error {
var err error
if o.wait {