Files
pipelines/internal/pipeline/runs_create.go
T

185 lines
7.6 KiB
Go

package pipeline
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"strings"
"time"
"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 {
Schedule *ScheduleAdmission
OutputFormat string
Model string
StageModels map[string]string
// NotifyMode is auto, on or off for this run. Empty means auto.
NotifyMode 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) {
if err := protocol.ValidateOutputFormat(overrides.OutputFormat); err != nil {
return 0, false, err
}
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, oldNotify, oldFormat string
var oldTools bool
err = s.db.QueryRowContext(ctx, "SELECT id,pipeline_id,version,input_json,workspace,write_paths,allow_tools,model,stage_models,notify_mode,output_format FROM pipeline_runs WHERE request_key=?", key).Scan(&id, &pid, &version, &oldInput, &oldWorkspace, &oldPaths, &oldTools, &oldModel, &oldStageModels, &oldNotify, &oldFormat)
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) || oldNotify != notifyMode(overrides.NotifyMode) || oldFormat != overrides.OutputFormat {
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()
if origin := overrides.Schedule; origin != nil {
// Acquire the SQLite writer lock while checking the original saved
// revision and occurrence. Edits/deletes between claim and admission
// invalidate admission, including while a title was being generated.
result, err := tx.ExecContext(ctx, `UPDATE saved_schedule_events SET status='admitted' WHERE schedule_id=? AND due=? AND revision=? AND status='claimed' AND run_id IS NULL AND EXISTS(SELECT 1 FROM saved_schedules WHERE id=? AND revision=? AND enabled=1 AND deleted=0)`, origin.ID, origin.Due.UTC().Format(time.RFC3339Nano), origin.Revision, origin.ID, origin.Revision)
if err != nil {
return 0, false, err
}
n, err := result.RowsAffected()
if err != nil {
return 0, false, err
}
if n != 1 {
return 0, false, fmt.Errorf("schedule changed or occurrence already admitted")
}
}
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,notify_mode,output_format) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", d.ID, d.Version, key, workspace, tools, string(writes), string(encoded), title.Text, title.Source, title.Model, overrides.Model, string(encodedModels), notifyMode(overrides.NotifyMode), overrides.OutputFormat)
if err != nil {
return 0, false, err
}
id, err = result.LastInsertId()
if err != nil {
return 0, false, err
}
if origin := overrides.Schedule; origin != nil {
if _, err = tx.ExecContext(ctx, `INSERT INTO run_schedule_origin(run_id,schedule_id,name) VALUES(?,?,?)`, id, origin.ID, origin.Name); err != nil {
return 0, false, err
}
if _, err = tx.ExecContext(ctx, `UPDATE saved_schedule_events SET run_id=? WHERE schedule_id=? AND due=?`, id, origin.ID, origin.Due.UTC().Format(time.RFC3339Nano)); 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
}
func notifyMode(value string) string {
switch strings.ToLower(strings.TrimSpace(value)) {
case "on", "off":
return strings.ToLower(strings.TrimSpace(value))
default:
return "auto"
}
}
// RunNotifyMode returns the stored per-run notification override (auto, on or
// off). It lets a restarted server honor an existing run's silence.
func (s *Store) RunNotifyMode(ctx context.Context, id int64) (string, error) {
var mode string
err := s.db.QueryRowContext(ctx, "SELECT notify_mode FROM pipeline_runs WHERE id=?", id).Scan(&mode)
if err != nil {
return "", err
}
return notifyMode(mode), 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
}