324 lines
13 KiB
Go
324 lines
13 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// 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),'')`
|
|
|
|
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,r.notify_mode,r.cancel_requested,r.cleanup_unconfirmed,r.output_format,
|
|
CASE WHEN r.status='waiting' THEN COALESCE((SELECT sequence FROM session_export_gates WHERE run_id=r.id),0) ELSE 0 END,
|
|
EXISTS(SELECT 1 FROM run_schedule_origin WHERE run_id=r.id),
|
|
COALESCE((SELECT schedule_id FROM run_schedule_origin WHERE run_id=r.id),0),
|
|
COALESCE((SELECT name FROM run_schedule_origin WHERE run_id=r.id),'')
|
|
` + 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, &r.NotifyMode, &r.CancelRequested, &r.CleanupUnconfirmed, &r.OutputFormat, &r.ExportPendingSequence, &r.Scheduled, &r.ScheduleID, &r.ScheduleName); 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) {
|
|
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer tx.Rollback()
|
|
rows, err := tx.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)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := loadRunGraphEvidence(ctx, tx, items); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
// One projection query per bounded batch, not a GetRun per row. Window ranks
|
|
// collapse arbitrarily long loops while preserving every distinct taken route
|
|
// and the last status of each stage. Only the five public graph facts are read.
|
|
type runGraphQuerier interface {
|
|
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
|
|
}
|
|
|
|
func loadRunGraphEvidence(ctx context.Context, tx runGraphQuerier, runs []protocol.RunSummary) error {
|
|
const batchSize = 100
|
|
for start := 0; start < len(runs); start += batchSize {
|
|
end := min(start+batchSize, len(runs))
|
|
args := make([]any, 0, end-start)
|
|
byID := make(map[int64]*protocol.RunGraphEvidence, end-start)
|
|
for i := start; i < end; i++ {
|
|
evidence := &protocol.RunGraphEvidence{Complete: true, Visits: []protocol.RunGraphVisit{}}
|
|
runs[i].GraphEvidence = evidence
|
|
byID[runs[i].ID] = evidence
|
|
args = append(args, runs[i].ID)
|
|
}
|
|
rows, err := tx.QueryContext(ctx, `WITH visits AS (
|
|
SELECT run_id,sequence,stage_key,status,outcome,target,
|
|
ROW_NUMBER() OVER (PARTITION BY run_id,stage_key ORDER BY sequence DESC) AS stage_rank,
|
|
ROW_NUMBER() OVER (PARTITION BY run_id,stage_key,outcome,target ORDER BY sequence DESC) AS route_rank
|
|
FROM run_steps WHERE run_id IN (`+strings.TrimSuffix(strings.Repeat("?,", len(args)), ",")+`))
|
|
SELECT run_id,sequence,stage_key,status,outcome,target FROM visits
|
|
WHERE stage_rank=1 OR (route_rank=1 AND outcome<>'' AND target<>'') ORDER BY run_id,sequence`, args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for rows.Next() {
|
|
var id int64
|
|
var visit protocol.RunGraphVisit
|
|
if err := rows.Scan(&id, &visit.Sequence, &visit.Stage, &visit.Status, &visit.Outcome, &visit.Target); err != nil {
|
|
rows.Close()
|
|
return err
|
|
}
|
|
byID[id].Visits = append(byID[id].Visits, visit)
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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),COALESCE((SELECT mode FROM retry_checkpoints WHERE run_id=pipeline_runs.id),CASE WHEN EXISTS(SELECT 1 FROM run_retries WHERE run_id=pipeline_runs.id) THEN 'restart' ELSE '' END),COALESCE((SELECT source_run_id FROM retry_checkpoints WHERE run_id=pipeline_runs.id),0),COALESCE((SELECT source_version FROM retry_checkpoints WHERE run_id=pipeline_runs.id),0),COALESCE((SELECT source_hash FROM retry_checkpoints WHERE run_id=pipeline_runs.id),''),title_source,title_model FROM pipeline_runs WHERE id=?", id).Scan(&r.Workspace, &r.AllowTools, &paths, &input, &output, &r.RetryOf, &r.RetryMode, &r.RetrySource, &r.RetrySourceVersion, &r.RetrySourceHash, &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),''),
|
|
COALESCE((SELECT export_json FROM step_session_exports x WHERE x.run_id=run_steps.run_id AND x.sequence=run_steps.sequence),''),
|
|
COALESCE((SELECT usage_json FROM step_usage u WHERE u.run_id=run_steps.run_id AND u.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, sessionExport, storedUsage 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, &sessionExport, &storedUsage, &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)
|
|
if sessionExport != "" {
|
|
if err := json.Unmarshal([]byte(sessionExport), &step.SessionExport); err != nil {
|
|
rows.Close()
|
|
return nil, err
|
|
}
|
|
}
|
|
if storedUsage != "" {
|
|
if err := json.Unmarshal([]byte(storedUsage), &step.Usage); err != nil {
|
|
rows.Close()
|
|
return nil, err
|
|
}
|
|
}
|
|
// 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 recovery protocol.SessionExportRecovery
|
|
jobErr := tx.QueryRowContext(ctx, "SELECT status,attempts,next_at,diagnostic FROM session_export_jobs WHERE run_id=? AND sequence=?", id, r.Steps[i].Sequence).Scan(&recovery.Status, &recovery.Attempts, &recovery.NextAt, &recovery.Diagnostic)
|
|
if jobErr == nil {
|
|
r.Steps[i].SessionExportRecovery = &recovery
|
|
} else if jobErr != sql.ErrNoRows {
|
|
return nil, jobErr
|
|
}
|
|
repairs, err := tx.QueryContext(ctx, "SELECT export_json FROM session_export_repairs WHERE run_id=? AND sequence=? ORDER BY id", id, r.Steps[i].Sequence)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for repairs.Next() {
|
|
var body string
|
|
var evidence protocol.SessionExport
|
|
if err := repairs.Scan(&body); err != nil {
|
|
repairs.Close()
|
|
return nil, err
|
|
}
|
|
if err := json.Unmarshal([]byte(body), &evidence); err != nil {
|
|
repairs.Close()
|
|
return nil, err
|
|
}
|
|
r.Steps[i].SessionExportRepairs = append(r.Steps[i].SessionExportRepairs, &evidence)
|
|
}
|
|
readErr := repairs.Err()
|
|
repairs.Close()
|
|
if readErr != nil {
|
|
return nil, readErr
|
|
}
|
|
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
|
|
}
|
|
}
|
|
// The run total is one pass over the steps already loaded above: it adds no
|
|
// query and no transcript parse.
|
|
r.Usage = protocol.TotalUsage(r.Steps)
|
|
alerts, err := tx.QueryContext(ctx, `SELECT sequence,event,channel,status,attempts,diagnostic,message_id FROM run_notifications WHERE run_id=? ORDER BY sequence,event,channel`, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for alerts.Next() {
|
|
var receipt protocol.RunNotificationReceipt
|
|
if err := alerts.Scan(&receipt.Sequence, &receipt.Event, &receipt.Channel, &receipt.Status, &receipt.Attempts, &receipt.Diagnostic, &receipt.MessageID); err != nil {
|
|
alerts.Close()
|
|
return nil, err
|
|
}
|
|
r.Notifications = append(r.Notifications, receipt)
|
|
}
|
|
err = alerts.Err()
|
|
alerts.Close()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return nil, err
|
|
}
|
|
return r, nil
|
|
}
|