package pipeline import ( "context" "database/sql" "encoding/json" "fmt" "strings" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/summarise" ) // RunOverrides are the launch-time model overrides recorded for a run: // Model applies to every model stage and StageModels overrides individual model // stages by key (per-stage wins over whole-run). They are recorded so the run // stays reproducible without touching the pinned pipeline version. type RunOverrides struct { Model string StageModels map[string]string } // CreateRun inserts a run and derives its title from the prompt. It is the entry // point for callers with no generated title; the derived fallback is always // non-empty and never calls a model. func (s *Store) CreateRun(ctx context.Context, d *protocol.PipelineDetail, key, workspace string, tools bool, paths []string, input map[string]any, retryOf ...int64) (int64, bool, error) { return s.CreateRunTitled(ctx, d, key, workspace, tools, paths, input, RunOverrides{}, summarise.Title{}, retryOf...) } // CreateRunTitled inserts a run with the caller's title and launch-time model // overrides. A non-empty title (a model generation) is stored verbatim; an empty // one falls back to the prompt-derived title, so a run never stores an empty // title. The overrides are compared in the request-key idempotency check, so a // replay with different overrides is refused as a different run. func (s *Store) CreateRunTitled(ctx context.Context, d *protocol.PipelineDetail, key, workspace string, tools bool, paths []string, input map[string]any, overrides RunOverrides, title summarise.Title, retryOf ...int64) (int64, bool, error) { var parent int64 if len(retryOf) > 0 { parent = retryOf[0] } if parent < 0 { return 0, false, fmt.Errorf("invalid retry parent") } title = resolveRunTitle(title, input) encoded, err := json.Marshal(input) if err != nil { return 0, false, err } writes, err := json.Marshal(paths) if err != nil { return 0, false, err } stageModels := overrides.StageModels if stageModels == nil { stageModels = map[string]string{} } encodedModels, err := json.Marshal(stageModels) if err != nil { return 0, false, err } s.mu.Lock() defer s.mu.Unlock() var id, pid int64 var version int var oldInput, oldWorkspace, oldPaths, oldModel, oldStageModels string var oldTools bool err = s.db.QueryRowContext(ctx, "SELECT id,pipeline_id,version,input_json,workspace,write_paths,allow_tools,model,stage_models FROM pipeline_runs WHERE request_key=?", key).Scan(&id, &pid, &version, &oldInput, &oldWorkspace, &oldPaths, &oldTools, &oldModel, &oldStageModels) if err == nil { var oldParent int64 if err := s.db.QueryRowContext(ctx, "SELECT COALESCE((SELECT original_run_id FROM run_retries WHERE run_id=?),0)", id).Scan(&oldParent); err != nil { return 0, false, err } if pid != d.ID || version != d.Version || oldInput != string(encoded) || oldWorkspace != workspace || oldPaths != string(writes) || oldTools != tools || oldParent != parent || oldModel != overrides.Model || oldStageModels != string(encodedModels) { return 0, false, fmt.Errorf("request key already used for a different run") } return id, false, nil } if err != sql.ErrNoRows { return 0, false, err } tx, err := s.db.BeginTx(ctx, nil) if err != nil { return 0, false, err } defer tx.Rollback() result, err := tx.ExecContext(ctx, "INSERT INTO pipeline_runs(pipeline_id,version,request_key,workspace,allow_tools,write_paths,input_json,title,title_source,title_model,model,stage_models) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", d.ID, d.Version, key, workspace, tools, string(writes), string(encoded), title.Text, title.Source, title.Model, overrides.Model, string(encodedModels)) if err != nil { return 0, false, err } id, err = result.LastInsertId() if err != nil { return 0, false, err } if parent > 0 { if _, err := tx.ExecContext(ctx, "INSERT INTO run_retries(run_id,original_run_id) VALUES(?,?)", id, parent); err != nil { return 0, false, err } } if err := tx.Commit(); err != nil { return 0, false, err } return id, true, nil } // resolveRunTitle guarantees a stored run title is never empty: a title supplied // by the caller (a model generation) is kept verbatim, and an absent one is // derived from the prompt's first line. The fallback is server-owned and never // calls a model, so it can never block or fail run creation. func resolveRunTitle(title summarise.Title, input map[string]any) summarise.Title { if strings.TrimSpace(title.Text) != "" { return title } prompt, _ := input["prompt"].(string) return summarise.Title{Text: summarise.DeriveTitle(prompt), Source: summarise.SourceFallback} } // SetRunTitle replaces a run's operator-facing title and its provenance without // touching the run's input, steps or execution. It is how an operator renames a // run after the fact. func (s *Store) SetRunTitle(ctx context.Context, id int64, title summarise.Title) error { result, err := s.db.ExecContext(ctx, "UPDATE pipeline_runs SET title=?,title_source=?,title_model=? WHERE id=?", title.Text, title.Source, title.Model, id) if err != nil { return err } n, err := result.RowsAffected() if err != nil { return err } if n != 1 { return fmt.Errorf("run not found") } return nil }