package pipeline import ( "bytes" "context" "database/sql" "encoding/json" "fmt" "strings" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" "git.a-wan.net/alan/pipelines/internal/summarise" ) func (s *Store) initRuns() error { _, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS pipeline_runs ( id INTEGER PRIMARY KEY, pipeline_id INTEGER NOT NULL, version INTEGER NOT NULL, request_key TEXT NOT NULL UNIQUE, workspace TEXT NOT NULL, allow_tools INTEGER NOT NULL, title TEXT NOT NULL DEFAULT '', title_source TEXT NOT NULL DEFAULT '', title_model TEXT NOT NULL DEFAULT '', model TEXT NOT NULL DEFAULT '', stage_models TEXT NOT NULL DEFAULT '{}', write_paths TEXT NOT NULL, input_json TEXT NOT NULL, output_json TEXT NOT NULL DEFAULT '{}', status TEXT NOT NULL DEFAULT 'queued', outcome TEXT NOT NULL DEFAULT '', current_stage TEXT NOT NULL DEFAULT '', diagnostic TEXT NOT NULL DEFAULT '', created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, finished_at TEXT, FOREIGN KEY(pipeline_id,version) REFERENCES pipeline_versions(pipeline_id,version)); CREATE TABLE IF NOT EXISTS run_steps ( run_id INTEGER NOT NULL REFERENCES pipeline_runs(id), sequence INTEGER NOT NULL, stage_key TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'running', outcome TEXT NOT NULL DEFAULT '', target TEXT NOT NULL DEFAULT '', input_json TEXT NOT NULL, output_json TEXT NOT NULL DEFAULT '{}', diagnostic TEXT NOT NULL DEFAULT '', provider TEXT NOT NULL DEFAULT '', model TEXT NOT NULL DEFAULT '', session_id TEXT NOT NULL DEFAULT '', agent_active INTEGER NOT NULL DEFAULT 0, transcript TEXT NOT NULL DEFAULT '', started_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, finished_at TEXT, PRIMARY KEY(run_id,sequence)); CREATE INDEX IF NOT EXISTS run_latest ON pipeline_runs(id DESC); CREATE INDEX IF NOT EXISTS run_steps_session ON run_steps(session_id); CREATE TABLE IF NOT EXISTS step_notifications ( run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, receipts_json TEXT NOT NULL, PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence)); CREATE TABLE IF NOT EXISTS step_file_changes ( run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, changes_json TEXT NOT NULL, PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence)); CREATE TABLE IF NOT EXISTS step_control ( run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, evidence_json TEXT NOT NULL, PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence)); CREATE TABLE IF NOT EXISTS step_artifacts ( run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, artifacts_json TEXT NOT NULL, PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence)); CREATE TABLE IF NOT EXISTS step_verdicts ( run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, verdict_json TEXT NOT NULL, PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence)); CREATE TABLE IF NOT EXISTS run_session_owners ( run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, backend TEXT NOT NULL, reference TEXT NOT NULL DEFAULT '', PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence)); CREATE TABLE IF NOT EXISTS run_retries ( run_id INTEGER PRIMARY KEY REFERENCES pipeline_runs(id), original_run_id INTEGER NOT NULL REFERENCES pipeline_runs(id));`) if err != nil { return err } if err := s.migrateRunTitles(); err != nil { return err } return s.migrateRunModels() } // migrateRunModels adds the launch-time model-override columns to a database // created before per-run overrides existed. Each column is added only when // missing, so an upgrade is idempotent across repeated Open calls and existing // rows keep their defaults (no model override, no recorded step model). func (s *Store) migrateRunModels() error { for _, column := range [][3]string{ {"pipeline_runs", "model", "TEXT NOT NULL DEFAULT ''"}, {"pipeline_runs", "stage_models", "TEXT NOT NULL DEFAULT '{}'"}, {"run_steps", "provider", "TEXT NOT NULL DEFAULT ''"}, {"run_steps", "model", "TEXT NOT NULL DEFAULT ''"}, } { if err := s.addColumnIfMissing(column[0], column[1], column[2]); err != nil { return err } } return nil } // migrateRunTitles adds the title columns to a database created before run // titles existed. Adding a column with a default leaves every existing row valid // and untouched, so old runs still list. func (s *Store) migrateRunTitles() error { for _, column := range [][2]string{ {"title", "TEXT NOT NULL DEFAULT ''"}, {"title_source", "TEXT NOT NULL DEFAULT ''"}, {"title_model", "TEXT NOT NULL DEFAULT ''"}, } { if err := s.addColumnIfMissing("pipeline_runs", column[0], column[1]); err != nil { return err } } return nil } // addColumnIfMissing adds a column only when the table predates it, making an // upgrade idempotent across repeated Open calls without rewriting history. func (s *Store) addColumnIfMissing(table, column, definition string) error { rows, err := s.db.Query("PRAGMA table_info(" + table + ")") if err != nil { return err } defer rows.Close() for rows.Next() { var cid, notnull, pk int var name, ctype string var dflt sql.NullString if err := rows.Scan(&cid, &name, &ctype, ¬null, &dflt, &pk); err != nil { return err } if name == column { return nil } } if err := rows.Err(); err != nil { return err } _, err = s.db.Exec("ALTER TABLE " + table + " ADD COLUMN " + column + " " + definition) return err } // 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 } // runFrom is the shared pipeline_runs/pipelines join every run-row read builds // on, so the run projections cannot drift. const runFrom = `FROM pipeline_runs r JOIN pipelines p ON p.id=r.pipeline_id` // runStamps formats a run row's created/finished timestamps; reused by runSelect // and searchRuns instead of copied per query. const runStamps = `strftime('%Y-%m-%dT%H:%M:%SZ',r.created_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',r.finished_at),'')` // 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 } const runSelect = `SELECT r.id,r.pipeline_id,p.name,r.version,v.content_hash,r.status,r.outcome,r.current_stage, ` + runStamps + `,r.diagnostic, CASE WHEN r.status='succeeded' THEN r.output_json ELSE COALESCE((SELECT output_json FROM run_steps WHERE run_id=r.id AND status='completed' ORDER BY sequence DESC LIMIT 1),'{}') END, (SELECT COUNT(*) FROM run_steps WHERE run_id=r.id AND status='completed'),r.workspace,r.title,r.model,r.stage_models ` + runFrom + ` JOIN pipeline_versions v ON v.pipeline_id=r.pipeline_id AND v.version=r.version` func scanRun(row interface{ Scan(...any) error }, r *protocol.RunSummary) error { var output, stageModels string if err := row.Scan(&r.ID, &r.PipelineID, &r.Name, &r.Version, &r.ContentHash, &r.Status, &r.Outcome, &r.CurrentStage, &r.CreatedAt, &r.FinishedAt, &r.Diagnostic, &output, &r.CompletedSteps, &r.Workspace, &r.Title, &r.Model, &stageModels); err != nil { return err } if stageModels != "" && stageModels != "{}" { if err := json.Unmarshal([]byte(stageModels), &r.StageModels); err != nil { return err } } var values map[string]any if err := decodeValues(output, &values); err != nil { return err } r.ResultPreview = protocol.OutputPreview(values) return nil } func (s *Store) ListRuns(ctx context.Context) ([]protocol.RunSummary, error) { rows, err := s.db.QueryContext(ctx, runSelect+" ORDER BY r.id DESC") if err != nil { return nil, err } defer rows.Close() items := []protocol.RunSummary{} for rows.Next() { var r protocol.RunSummary if err := scanRun(rows, &r); err != nil { return nil, err } items = append(items, r) } return items, rows.Err() } func (s *Store) RunByKey(ctx context.Context, key string) (int64, error) { var id int64 err := s.db.QueryRowContext(ctx, "SELECT id FROM pipeline_runs WHERE request_key=?", key).Scan(&id) if err == sql.ErrNoRows { return 0, nil } return id, err } func decodeValues(text string, v *map[string]any) error { d := json.NewDecoder(bytes.NewBufferString(text)) d.UseNumber() return d.Decode(v) } func (s *Store) GetRun(ctx context.Context, id int64) (*protocol.RunDetail, error) { tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) if err != nil { return nil, err } defer tx.Rollback() r := &protocol.RunDetail{} if err := scanRun(tx.QueryRowContext(ctx, runSelect+" WHERE r.id=?", id), &r.RunSummary); err != nil { if err == sql.ErrNoRows { return nil, fmt.Errorf("run not found") } return nil, err } var input, output, paths string var graphJSON string if err := tx.QueryRowContext(ctx, "SELECT graph_json FROM pipeline_versions WHERE pipeline_id=? AND version=?", r.PipelineID, r.Version).Scan(&graphJSON); err != nil { return nil, err } if err := json.Unmarshal([]byte(graphJSON), &r.Graph); err != nil { return nil, err } if err := tx.QueryRowContext(ctx, "SELECT workspace,allow_tools,write_paths,input_json,output_json,COALESCE((SELECT original_run_id FROM run_retries WHERE run_id=pipeline_runs.id),0),title_source,title_model FROM pipeline_runs WHERE id=?", id).Scan(&r.Workspace, &r.AllowTools, &paths, &input, &output, &r.RetryOf, &r.TitleSource, &r.TitleModel); err != nil { return nil, err } if err := decodeValues(input, &r.Input); err != nil { return nil, err } if err := decodeValues(output, &r.Output); err != nil { return nil, err } if err := json.Unmarshal([]byte(paths), &r.WritePaths); err != nil { return nil, err } rows, err := tx.QueryContext(ctx, `SELECT sequence,stage_key,status,outcome,target,input_json,output_json,diagnostic,session_id,agent_active,transcript,provider,model, COALESCE((SELECT receipts_json FROM step_notifications n WHERE n.run_id=run_steps.run_id AND n.sequence=run_steps.sequence),'[]'), COALESCE((SELECT changes_json FROM step_file_changes f WHERE f.run_id=run_steps.run_id AND f.sequence=run_steps.sequence),'null'), COALESCE((SELECT artifacts_json FROM step_artifacts a WHERE a.run_id=run_steps.run_id AND a.sequence=run_steps.sequence),'null'), COALESCE((SELECT verdict_json FROM step_verdicts v WHERE v.run_id=run_steps.run_id AND v.sequence=run_steps.sequence),''), strftime('%Y-%m-%dT%H:%M:%SZ',started_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',finished_at),'') FROM run_steps WHERE run_id=? ORDER BY sequence`, id) if err != nil { return nil, err } r.Steps = []protocol.RunStep{} for rows.Next() { var step protocol.RunStep var notifications string var fileChanges, artifacts, storedVerdict string if err := rows.Scan(&step.Sequence, &step.Stage, &step.Status, &step.Outcome, &step.Target, &input, &output, &step.Diagnostic, &step.SessionID, &step.AgentActive, &step.Transcript, &step.Provider, &step.Model, ¬ifications, &fileChanges, &artifacts, &storedVerdict, &step.StartedAt, &step.FinishedAt); err != nil { rows.Close() return nil, err } if err := json.Unmarshal([]byte(notifications), &step.Notifications); err != nil { rows.Close() return nil, err } if err := json.Unmarshal([]byte(fileChanges), &step.FileChanges); err != nil { rows.Close() return nil, err } if artifacts != "" && artifacts != "null" { var evidence stageArtifactsEvidence if err := json.Unmarshal([]byte(artifacts), &evidence); err != nil { rows.Close() return nil, err } step.Artifacts = evidence.Files step.ArtifactsSkipped = evidence.Skipped step.ArtifactsTruncated = evidence.Truncated } if err := decodeValues(input, &step.Input); err != nil { rows.Close() return nil, err } if err := decodeValues(output, &step.Output); err != nil { rows.Close() return nil, err } // The verdict is derived here - the one place that already holds both the // decoded output and the recorded artifacts - using the stored model line // when present and the bounded read-time fallback otherwise, so existing // runs need no backfill. var stored stageVerdictEvidence if storedVerdict != "" { if err := json.Unmarshal([]byte(storedVerdict), &stored); err != nil { rows.Close() return nil, err } } step.Verdict = protocol.NewStageVerdict(step.Outcome, step.Target, step.Output, step.Artifacts, stored.Reason, stored.Source) // Loaded below after the rows cursor closes to preserve SQLite connection use. r.Steps = append(r.Steps, step) } err = rows.Err() rows.Close() if err != nil { return nil, err } for i := range r.Steps { var data string err := tx.QueryRowContext(ctx, "SELECT evidence_json FROM step_control WHERE run_id=? AND sequence=?", id, r.Steps[i].Sequence).Scan(&data) if err == sql.ErrNoRows { continue } if err != nil { return nil, err } if err := json.Unmarshal([]byte(data), &r.Steps[i].Control); err != nil { return nil, err } } if err := tx.Commit(); err != nil { return nil, err } return r, nil } func (s *Store) SaveNotifications(ctx context.Context, id int64, sequence int, receipts []protocol.NotificationReceipt) error { data, err := json.Marshal(receipts) if err != nil { return err } _, err = s.db.ExecContext(ctx, `INSERT INTO step_notifications(run_id,sequence,receipts_json) VALUES(?,?,?) ON CONFLICT(run_id,sequence) DO UPDATE SET receipts_json=excluded.receipts_json`, id, sequence, string(data)) return err } func affected(result sql.Result, err error) error { if err != nil { return err } n, err := result.RowsAffected() if err != nil { return err } if n != 1 { return fmt.Errorf("invalid run state transition") } return nil } func (s *Store) BeginStep(ctx context.Context, id int64, sequence int, key string, input map[string]any) error { encoded, err := json.Marshal(input) if err != nil { return err } tx, err := s.db.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() if err := affected(tx.ExecContext(ctx, "UPDATE pipeline_runs SET status='running',current_stage=? WHERE id=? AND status IN ('queued','running')", key, id)); err != nil { return err } if _, err := tx.ExecContext(ctx, "INSERT INTO run_steps(run_id,sequence,stage_key,input_json) VALUES (?,?,?,?)", id, sequence, key, string(encoded)); err != nil { return err } return tx.Commit() } func (s *Store) RunSession(ctx context.Context, id int64, sequence int, session string, active bool, metadata ...string) error { tx, err := s.db.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() if err = affected(tx.ExecContext(ctx, "UPDATE run_steps SET session_id=?,agent_active=? WHERE run_id=? AND sequence=? AND status='running'", session, active, id, sequence)); err != nil { return err } if len(metadata) > 0 { backend := metadata[0] if backend == "" { backend = "v2" } reference := "" if len(metadata) > 1 { reference = metadata[1] } if _, err = tx.ExecContext(ctx, "INSERT INTO run_session_owners(run_id,sequence,backend,reference) VALUES(?,?,?,?) ON CONFLICT(run_id,sequence) DO UPDATE SET backend=excluded.backend,reference=excluded.reference", id, sequence, backend, reference); err != nil { return err } } return tx.Commit() } type SessionOwner struct { ID string Backend string Directory string Reference string // RunID is the owning run, used to tell an orphaned session (no active run) // from one owned by a run that is active right now. RunID int64 // Active is true while the owning step records the session as agent-active. Active bool } func (s *Store) RunSessionOwner(ctx context.Context, id int64, sequence int) (SessionOwner, error) { var owner SessionOwner err := s.db.QueryRowContext(ctx, `SELECT s.session_id,COALESCE(o.backend,'v2'),r.workspace,COALESCE(o.reference,'') FROM run_steps s JOIN pipeline_runs r ON r.id=s.run_id LEFT JOIN run_session_owners o ON o.run_id=s.run_id AND o.sequence=s.sequence WHERE s.run_id=? AND s.sequence=?`, id, sequence).Scan(&owner.ID, &owner.Backend, &owner.Directory, &owner.Reference) return owner, err } // sessionOwnerScanLimit bounds the ownership scan so a long-lived database with // thousands of historical sessions cannot make one chat_sessions request scan // them all. Rows are read newest-run first, so the bound only ever drops the // oldest owners, never the sessions a client can still be writing. const sessionOwnerScanLimit = 1024 // ActiveSessionOwners reports every currently active agent session, deduped by // session id (newest owning run wins). func (s *Store) ActiveSessionOwners(ctx context.Context) ([]SessionOwner, error) { return s.sessionOwners(ctx, true) } // OwnedSessions reports every session the server ever recorded in run_steps, // deduped by session id (newest owning run wins) and including inactive ones, so // chat_sessions can be restricted to sessions this server created. func (s *Store) OwnedSessions(ctx context.Context) ([]SessionOwner, error) { return s.sessionOwners(ctx, false) } // sessionOwners is the single ownership query both ActiveSessionOwners and // OwnedSessions run, so the SQL is not duplicated. It reads one bounded, // newest-run-first window and drops repeated session ids in favour of the most // recent owning run's directory and active flag. func (s *Store) sessionOwners(ctx context.Context, activeOnly bool) ([]SessionOwner, error) { query := `SELECT s.session_id,COALESCE(o.backend,'v2'),r.workspace,COALESCE(o.reference,''),s.run_id,s.agent_active FROM run_steps s JOIN pipeline_runs r ON r.id=s.run_id LEFT JOIN run_session_owners o ON o.run_id=s.run_id AND o.sequence=s.sequence WHERE s.session_id<>''` if activeOnly { query += " AND s.agent_active=1" } query += " ORDER BY s.run_id DESC LIMIT ?" rows, err := s.db.QueryContext(ctx, query, sessionOwnerScanLimit) if err != nil { return nil, err } defer rows.Close() seen := map[string]struct{}{} var owners []SessionOwner for rows.Next() { var owner SessionOwner var active int if err = rows.Scan(&owner.ID, &owner.Backend, &owner.Directory, &owner.Reference, &owner.RunID, &active); err != nil { return nil, err } owner.Active = active != 0 if _, ok := seen[owner.ID]; ok { continue } seen[owner.ID] = struct{}{} owners = append(owners, owner) } return owners, rows.Err() } // stageArtifactsEvidence is the persisted per-step artifact list plus its // bounded-scan counters; it is kept in a separate table like the other // step-scoped evidence so run_steps needs no schema migration on old databases. type stageArtifactsEvidence struct { Files []protocol.StageArtifact `json:"files"` Skipped int `json:"skipped,omitempty"` Truncated int `json:"truncated,omitempty"` } // stageVerdictEvidence is the generated one-line verdict reason plus its // provenance. Like the other step-scoped evidence it lives in its own table, so // run_steps needs no schema migration and an old database gains the empty table // on Open with no backfill. Only a model-generated line is ever stored; the // read-time fallback is always derived, so existing runs still show a verdict. type stageVerdictEvidence struct { Reason string `json:"reason"` Source string `json:"source"` } func (s *Store) FinishStep(ctx context.Context, id int64, step protocol.RunStep) error { output, err := json.Marshal(step.Output) if err != nil { return err } // Bound the stored transcript without corrupting structured JSON. A large // transcript becomes a valid bounded preview envelope preserving stage_limits // and failure/format diagnostics; the exact Output is saved separately below. step.Transcript = stage.BoundTranscript(step.Transcript, step.SessionID) tx, err := s.db.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() if err := affected(tx.ExecContext(ctx, `UPDATE run_steps SET status=?,outcome=?,target=?,output_json=?,diagnostic=?,transcript=?,provider=?,model=?,finished_at=CURRENT_TIMESTAMP WHERE run_id=? AND sequence=? AND status='running'`, step.Status, step.Outcome, step.Target, string(output), step.Diagnostic, step.Transcript, step.Provider, step.Model, id, step.Sequence)); err != nil { return err } if step.FileChanges != nil { data, err := json.Marshal(step.FileChanges) if err != nil { return err } if _, err = tx.ExecContext(ctx, "INSERT INTO step_file_changes(run_id,sequence,changes_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil { return err } } if step.Control != nil { data, err := json.Marshal(step.Control) if err != nil { return err } if _, err := tx.ExecContext(ctx, "INSERT INTO step_control(run_id,sequence,evidence_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil { return err } } if len(step.Artifacts) > 0 || step.ArtifactsSkipped > 0 || step.ArtifactsTruncated > 0 { data, err := json.Marshal(stageArtifactsEvidence{Files: step.Artifacts, Skipped: step.ArtifactsSkipped, Truncated: step.ArtifactsTruncated}) if err != nil { return err } if _, err := tx.ExecContext(ctx, "INSERT INTO step_artifacts(run_id,sequence,artifacts_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil { return err } } // Persist only a generated model line and its provenance; the read-time // fallback is always derived in GetRun, so old runs stay intact. if step.Verdict != nil && strings.TrimSpace(step.Verdict.Reason) != "" { data, err := json.Marshal(stageVerdictEvidence{Reason: step.Verdict.Reason, Source: step.Verdict.ReasonSource}) if err != nil { return err } if _, err := tx.ExecContext(ctx, "INSERT INTO step_verdicts(run_id,sequence,verdict_json) VALUES(?,?,?) ON CONFLICT(run_id,sequence) DO UPDATE SET verdict_json=excluded.verdict_json", id, step.Sequence, string(data)); err != nil { return err } } return tx.Commit() } // MarkWaiting persists a human stage's pending state in one transaction: the // step becomes waiting with self-contained evidence (its operator instructions // and ordered options) and the run reports the same key it is waiting on. It // leaves finished_at NULL for both and adds no goroutine, timer or new column. func (s *Store) MarkWaiting(ctx context.Context, id int64, sequence int, key string, evidence map[string]any) error { data, err := json.Marshal(evidence) if err != nil { return err } tx, err := s.db.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() if err := affected(tx.ExecContext(ctx, `UPDATE run_steps SET status=?,output_json=? WHERE run_id=? AND sequence=? AND status='running'`, protocol.StepStatusWaiting, string(data), id, sequence)); err != nil { return err } if err := affected(tx.ExecContext(ctx, `UPDATE pipeline_runs SET status=?,current_stage=? WHERE id=? AND status IN ('queued','running')`, protocol.RunStatusWaiting, key, id)); err != nil { return err } return tx.Commit() } // ResolveDecision records an operator's choice for a waiting human stage and // routes the run along that option's edge, all in one short transaction. It // reads the pinned graph and the pending step from persisted rows, so it is // correct after a restart and needs no live goroutine. Validation is idempotent: // a repeated decision for an already-decided visit with the same option returns // applied=false and no error, an unknown option is refused with the valid list, // and a stage that is not waiting is refused with its current state. func (s *Store) ResolveDecision(ctx context.Context, id int64, stageKey, option, feedback string) (protocol.RunStep, bool, error) { tx, err := s.db.BeginTx(ctx, nil) if err != nil { return protocol.RunStep{}, false, err } defer tx.Rollback() var status, currentStage, graphJSON string if err := tx.QueryRowContext(ctx, `SELECT r.status,r.current_stage,v.graph_json FROM pipeline_runs r JOIN pipeline_versions v ON v.pipeline_id=r.pipeline_id AND v.version=r.version WHERE r.id=?`, id).Scan(&status, ¤tStage, &graphJSON); err != nil { if err == sql.ErrNoRows { return protocol.RunStep{}, false, fmt.Errorf("run not found") } return protocol.RunStep{}, false, err } if stageKey == "" { stageKey = currentStage // Prefer the actual waiting step's stage; current_stage is normally the // waiting stage, but this keeps the decision correct if it ever drifts. var waitingKey string if err := tx.QueryRowContext(ctx, `SELECT stage_key FROM run_steps WHERE run_id=? AND status=? ORDER BY sequence DESC LIMIT 1`, id, protocol.StepStatusWaiting).Scan(&waitingKey); err == nil { stageKey = waitingKey } else if err != sql.ErrNoRows { return protocol.RunStep{}, false, err } } var graph protocol.Graph if err := json.Unmarshal([]byte(graphJSON), &graph); err != nil { return protocol.RunStep{}, false, err } var edges map[string]string found := false for _, declared := range graph.Stages { if declared.Key == stageKey { edges = declared.Edges found = true break } } if !found { return protocol.RunStep{}, false, fmt.Errorf("stage %q is not part of this run", stageKey) } options := stage.HumanOptions(edges) valid := false for _, candidate := range options { if candidate == option { valid = true break } } if !valid { return protocol.RunStep{}, false, fmt.Errorf("unknown option %q for stage %s; valid options: %s", option, stageKey, strings.Join(options, ", ")) } var sequence int var stepStatus, stepOutcome string err = tx.QueryRowContext(ctx, `SELECT sequence,status,outcome FROM run_steps WHERE run_id=? AND stage_key=? ORDER BY sequence DESC LIMIT 1`, id, stageKey).Scan(&sequence, &stepStatus, &stepOutcome) if err == sql.ErrNoRows { return protocol.RunStep{}, false, fmt.Errorf("stage %s is not waiting", stageKey) } if err != nil { return protocol.RunStep{}, false, err } if stepStatus != protocol.StepStatusWaiting { if stepStatus == "completed" && stepOutcome == option { return protocol.RunStep{Sequence: sequence, Stage: stageKey, Status: "completed", Outcome: stepOutcome, Target: edges[stepOutcome]}, false, nil } return protocol.RunStep{}, false, fmt.Errorf("stage %s is not waiting (state %s)", stageKey, stepStatus) } if status != protocol.RunStatusWaiting && status != protocol.RunStatusRunning { return protocol.RunStep{}, false, fmt.Errorf("run %d is not waiting (state %s)", id, status) } target := edges[option] // The operator's choice and feedback are the step's evidence. The feedback // stays in the output only - never the step diagnostic - so a deliberate, // successful decision is not presented as a stop or error; the derived // verdict reads the feedback as the step's reason. output := map[string]any{protocol.DecisionOptionKey: option, protocol.DecisionFeedbackKey: feedback} encoded, err := json.Marshal(output) if err != nil { return protocol.RunStep{}, false, err } if err := affected(tx.ExecContext(ctx, `UPDATE run_steps SET status='completed',outcome=?,target=?,output_json=?,finished_at=CURRENT_TIMESTAMP WHERE run_id=? AND sequence=? AND status=?`, option, target, string(encoded), id, sequence, protocol.StepStatusWaiting)); err != nil { return protocol.RunStep{}, false, err } if err := affected(tx.ExecContext(ctx, `UPDATE pipeline_runs SET status='running' WHERE id=? AND status IN (?,?)`, id, protocol.RunStatusWaiting, protocol.RunStatusRunning)); err != nil { return protocol.RunStep{}, false, err } if err := tx.Commit(); err != nil { return protocol.RunStep{}, false, err } return protocol.RunStep{Sequence: sequence, Stage: stageKey, Status: "completed", Outcome: option, Target: target, Output: output}, true, nil } // CancelWaiting cancels a run that is waiting on a person without a live // goroutine. It is a single transaction that marks the run canceled and its // waiting step canceled; a run that is not waiting is reported, not silently // ignored. func (s *Store) CancelWaiting(ctx context.Context, id int64) error { tx, err := s.db.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() res, err := tx.ExecContext(ctx, `UPDATE pipeline_runs SET status='canceled',finished_at=CURRENT_TIMESTAMP WHERE id=? AND status=?`, id, protocol.RunStatusWaiting) if err != nil { return err } n, err := res.RowsAffected() if err != nil { return err } if n != 1 { return fmt.Errorf("run is not active or waiting") } if _, err := tx.ExecContext(ctx, `UPDATE run_steps SET status='canceled',diagnostic='Canceled while waiting for a decision',finished_at=CURRENT_TIMESTAMP WHERE run_id=? AND status=?`, id, protocol.StepStatusWaiting); err != nil { return err } return tx.Commit() } func (s *Store) FinishRun(ctx context.Context, id int64, status, outcome, diagnostic string, output map[string]any) error { data, err := json.Marshal(output) if err != nil { return err } return affected(s.db.ExecContext(ctx, "UPDATE pipeline_runs SET status=?,outcome=?,diagnostic=?,output_json=?,finished_at=CURRENT_TIMESTAMP WHERE id=? AND status IN ('queued','running')", status, outcome, diagnostic, string(data), id)) } // ActiveAgentSessionsForRun returns the distinct active agent session ids that // belong to one run. It is scoped to run_steps.run_id so concurrent runs never // observe each other's sessions: run_steps is keyed by (run_id, sequence), so // this is one indexed lookup, not a scan. func (s *Store) ActiveAgentSessionsForRun(ctx context.Context, runID int64) ([]string, error) { rows, err := s.db.QueryContext(ctx, "SELECT DISTINCT session_id FROM run_steps WHERE run_id=? AND agent_active=1 AND session_id<>''", runID) if err != nil { return nil, err } defer rows.Close() ids := []string{} for rows.Next() { var id string if err := rows.Scan(&id); err != nil { return nil, err } ids = append(ids, id) } return ids, rows.Err() } func (s *Store) RecoverRuns(ctx context.Context) error { tx, err := s.db.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() if _, err := tx.ExecContext(ctx, "UPDATE run_steps SET status='interrupted',agent_active=0,diagnostic='Server stopped; stage was not resumed',finished_at=CURRENT_TIMESTAMP WHERE (status='running' OR agent_active=1) AND status<>?", protocol.StepStatusWaiting); err != nil { return err } if _, err := tx.ExecContext(ctx, "UPDATE pipeline_runs SET status='interrupted',diagnostic='Server stopped; run was not resumed',finished_at=CURRENT_TIMESTAMP WHERE status IN ('queued','running') AND status<>?", protocol.RunStatusWaiting); err != nil { return err } return tx.Commit() }