Render run list miniatures from compact per-stage route evidence

This commit is contained in:
2026-10-20 18:21:24 +01:00
parent 855de19a35
commit 34939edd0e
14 changed files with 501 additions and 40 deletions
@@ -0,0 +1,121 @@
package pipeline
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"path/filepath"
"strings"
"testing"
)
type graphQueryCounter struct {
*sql.DB
calls int
}
func (q *graphQueryCounter) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
q.calls++
if len(args) > 100 {
return nil, fmt.Errorf("unbounded batch: %d", len(args))
}
for _, private := range []string{"input_json", "output_json", "transcript", "diagnostic", "session_id"} {
if strings.Contains(query, private) {
return nil, fmt.Errorf("private projection: %s", private)
}
}
return q.DB.QueryContext(ctx, query, args...)
}
func TestRunGraphEvidenceBatchesCompactsAndReopens(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "evidence.db")
store, err := Open(path)
if err != nil {
t.Fatal(err)
}
defer func() { store.Close() }()
if _, err := store.Submit(ctx, "graph", Template([]string{"a", "b", "c", "never"}, true)); err != nil {
t.Fatal(err)
}
pipelines, _ := store.List(ctx)
definition, _ := store.Get(ctx, pipelines[0].ID, 1)
var first int64
// Cross two batch boundaries, retaining the complete empty evidence of queued
// runs. A single connection also catches accidental nested detail reads.
store.db.SetMaxOpenConns(1)
for i := 0; i < 205; i++ {
id, _, err := store.CreateRun(ctx, definition, fmt.Sprintf("graph-%d", i), "workspace", false, nil, nil)
if err != nil {
t.Fatal(err)
}
if i == 0 {
first = id
}
}
tx, err := store.db.BeginTx(ctx, nil)
if err != nil {
t.Fatal(err)
}
for sequence := 1; sequence <= 200; sequence++ {
stage, status, outcome, target := "a", "completed", "success", "b"
if sequence == 198 {
stage, target = "b", "c"
}
if sequence == 199 {
stage, outcome, target = "c", "retry", "a"
}
if sequence == 200 {
status, outcome, target = "failed", "", ""
}
_, err := tx.ExecContext(ctx, `INSERT INTO run_steps(run_id,sequence,stage_key,status,outcome,target,input_json,output_json,diagnostic,transcript) VALUES(?,?,?,?,?,?,'{"secret":"PRIVATE-INPUT"}','{}','PRIVATE-DIAGNOSTIC','PRIVATE-TRANSCRIPT')`, first, sequence, stage, status, outcome, target)
if err != nil {
t.Fatal(err)
}
}
if err := tx.Commit(); err != nil {
t.Fatal(err)
}
// No new table or column migration is required: projection works after an
// existing database is closed and reopened, including historical rows.
if err := store.Close(); err != nil {
t.Fatal(err)
}
store, err = Open(path)
if err != nil {
t.Fatal(err)
}
runs, err := store.ListRuns(ctx)
if err != nil {
t.Fatal(err)
}
if len(runs) != 205 {
t.Fatal(len(runs))
}
counter := &graphQueryCounter{DB: store.db}
if err := loadRunGraphEvidence(ctx, counter, runs); err != nil {
t.Fatal(err)
}
if counter.calls != 3 {
t.Fatalf("graph projection made %d queries for 205 runs; want 3, not N+1", counter.calls)
}
for _, run := range runs {
if run.GraphEvidence == nil || !run.GraphEvidence.Complete {
t.Fatalf("missing complete evidence: %+v", run)
}
if run.ID != first && len(run.GraphEvidence.Visits) != 0 {
t.Fatal("fabricated queued visit")
}
if run.ID == first {
visits := run.GraphEvidence.Visits
if len(visits) != 4 || visits[0].Sequence != 197 || visits[3].Sequence != 200 || visits[3].Status != "failed" {
t.Fatalf("lost latest stage or loop routes: %+v", visits)
}
encoded, _ := json.Marshal(run.GraphEvidence)
if strings.Contains(string(encoded), "PRIVATE") || strings.Contains(string(encoded), "input") || strings.Contains(string(encoded), "diagnostic") {
t.Fatal(string(encoded))
}
}
}
}
+67 -2
View File
@@ -6,6 +6,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
@@ -43,7 +44,12 @@ func scanRun(row interface{ Scan(...any) error }, r *protocol.RunSummary) error
}
func (s *Store) ListRuns(ctx context.Context) ([]protocol.RunSummary, error) {
rows, err := s.db.QueryContext(ctx, runSelect+" ORDER BY r.id DESC")
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
}
@@ -56,7 +62,66 @@ func (s *Store) ListRuns(ctx context.Context) ([]protocol.RunSummary, error) {
}
items = append(items, r)
}
return items, rows.Err()
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) {