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)) } } } }