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) {
+9 -4
View File
@@ -15,7 +15,8 @@ const (
// RunEventFrame is the server-pushed run_event delta. It carries only the values
// that changed, never the whole run: the run's identity, status, outcome, current
// stage, completed-step count and timestamps, plus at most one changed step.
// stage, completed-step count and timestamps, plus the latest changed step and
// compact routing facts for all changed visits preserved during coalescing.
// Every field is optional so an older peer ignores what it does not know and a
// client can fold the frame into its cached run.
//
@@ -24,9 +25,13 @@ const (
// from the frame instead of re-reading the whole list. They repeat on every
// frame (pipeline identity never changes mid-run).
type RunEventFrame struct {
NotifyMode string `json:"notify_mode,omitempty"`
RunID int64 `json:"run_id"`
Workspace string `json:"workspace,omitempty"`
// GraphEvidence seeds a complete compact snapshot; GraphVisits preserves
// changed visits even when Step is replaced during frame coalescing.
GraphEvidence *RunGraphEvidence `json:"graph_evidence,omitempty"`
GraphVisits []RunGraphVisit `json:"graph_visits,omitempty"`
NotifyMode string `json:"notify_mode,omitempty"`
RunID int64 `json:"run_id"`
Workspace string `json:"workspace,omitempty"`
// PipelineID, Name, Title and Version identify the run's pipeline and its
// operator-facing title, so a new run's row can be built without a fetch.
PipelineID int64 `json:"pipeline_id,omitempty"`
+21 -4
View File
@@ -134,10 +134,11 @@ type SessionExportRecovery struct {
}
type RunSummary struct {
NotifyMode string `json:"notify_mode,omitempty"`
ID int64 `json:"id"`
PipelineID int64 `json:"pipeline_id"`
Name string `json:"name"`
GraphEvidence *RunGraphEvidence `json:"graph_evidence,omitempty"`
NotifyMode string `json:"notify_mode,omitempty"`
ID int64 `json:"id"`
PipelineID int64 `json:"pipeline_id"`
Name string `json:"name"`
// Title is the operator-facing, bounded single-line description of what the
// run is for, generated from its prompt. It is never empty for a new run;
// historical rows predating the title column may omit it.
@@ -165,6 +166,22 @@ type RunSummary struct {
OutputFormat string `json:"output_format,omitempty"`
}
// RunGraphVisit contains only routing and lifecycle facts, never private step
// inputs, outputs, diagnostics, sessions or transcripts. Repeated loop visits
// are compacted to the latest stage visit and the latest visit of each route.
type RunGraphVisit struct {
Sequence int `json:"sequence"`
Stage string `json:"stage"`
Status string `json:"status"`
Outcome string `json:"outcome"`
Target string `json:"target"`
}
type RunGraphEvidence struct {
Complete bool `json:"complete"`
Visits []RunGraphVisit `json:"visits"`
}
// Label is the operator-facing name of a run: the generated Title when the
// server stored one, otherwise the pipeline Name (historical rows predate
// titles). It is the single definition shared by the CLI and TUI.
+79
View File
@@ -0,0 +1,79 @@
package server
import (
"context"
"encoding/json"
"git.a-wan.net/alan/pipelines/internal/protocol"
"strings"
"testing"
"time"
)
func TestGraphEvidenceSubscribeSnapshot(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
store, handler, ids := eventStore(t, ctx, "a", "b", "c")
definition, err := store.Get(ctx, ids[0], 1)
if err != nil {
t.Fatal(err)
}
id, _, err := store.CreateRun(ctx, definition, "graph-snapshot", "workspace", false, nil, nil)
if err != nil {
t.Fatal(err)
}
for index, key := range []string{"a", "b", "c"} {
sequence := index + 1
if err := store.BeginStep(ctx, id, sequence, key, map[string]any{"prompt": "PRIVATE-PROMPT"}); err != nil {
t.Fatal(err)
}
status, outcome, target := "completed", "success", "b"
if key == "b" {
target = "c"
}
if key == "c" {
status, outcome, target = "failed", "", ""
}
if err := store.FinishStep(ctx, id, protocol.RunStep{Sequence: sequence, Status: status, Outcome: outcome, Target: target, Transcript: "PRIVATE-TRANSCRIPT"}); err != nil {
t.Fatal(err)
}
}
hub := newRunEventHub()
defer hub.unsubscribeClient("snapshot-test")
var messages []protocol.Message
if !handleRunEvents(protocol.Message{Type: protocol.Subscribe}, hub, handler.runner, "snapshot-test", func(message protocol.Message) error { messages = append(messages, message); return nil }) {
t.Fatal("unhandled subscribe")
}
if len(messages) != 2 || messages[0].RunEvent == nil || messages[1].Type != protocol.Subscribe {
t.Fatalf("snapshot/ack: %+v", messages)
}
evidence := messages[0].RunEvent.GraphEvidence
if evidence == nil || !evidence.Complete || len(evidence.Visits) != 3 || evidence.Visits[2].Status != "failed" {
t.Fatalf("missing persisted graph evidence: %+v", evidence)
}
encoded, _ := json.Marshal(messages[0])
if strings.Contains(string(encoded), "PRIVATE") {
t.Fatal(string(encoded))
}
}
func TestCoalescedGraphVisitsRetainEveryStage(t *testing.T) {
hub := newRunEventHub()
hub.interval = time.Hour
sub := hub.subscribe("graph-test", "", nil, func(protocol.Message) error { return nil })
defer sub.close()
// Hold subsequent frames in the coalescer without relying on wall-clock races.
hub.emit(protocol.RunEventFrame{RunID: 1, Status: "running"}, false)
for sequence, stage := range []string{"a", "b", "c"} {
hub.emit(protocol.RunEventFrame{RunID: 1, Step: &protocol.RunStepDelta{Sequence: sequence + 1, Stage: stage, Status: "completed", Outcome: "success", Target: "next", Diagnostic: "PRIVATE"}}, false)
}
hub.mu.Lock()
pending := hub.runs[1].pending
hub.stopTimerLocked(hub.runs[1])
hub.mu.Unlock()
if pending == nil || len(pending.GraphVisits) != 3 || pending.Step.Stage != "c" {
t.Fatalf("coalescing lost evidence: %+v", pending)
}
if pending.GraphVisits[0].Stage != "a" || pending.GraphVisits[1].Stage != "b" {
t.Fatal(pending.GraphVisits)
}
}
+5
View File
@@ -173,6 +173,9 @@ func (h *runEventHub) remove(sub *runSubscription) {
// A terminal transition flushes any pending delta first, then the terminal frame
// immediately, so a client always observes the final state as its own frame.
func (h *runEventHub) emit(frame protocol.RunEventFrame, terminal bool) {
if step := frame.Step; step != nil {
frame.GraphVisits = append(frame.GraphVisits, protocol.RunGraphVisit{Sequence: step.Sequence, Stage: step.Stage, Status: step.Status, Outcome: step.Outcome, Target: step.Target})
}
if h == nil || frame.RunID <= 0 {
return
}
@@ -470,6 +473,7 @@ func mergeRunEventFrame(into, in *protocol.RunEventFrame) {
if in.Step != nil {
into.Step = in.Step
}
into.GraphVisits = append(into.GraphVisits, in.GraphVisits...)
}
// handleRunEvents dispatches one subscribe/unsubscribe request and reports
@@ -505,6 +509,7 @@ func handleRunEvents(request protocol.Message, events *runEventHub, runs *runner
continue
}
frame := protocol.RunEventFrame{
GraphEvidence: summary.GraphEvidence,
NotifyMode: summary.NotifyMode,
RunID: summary.ID,
Workspace: summary.Workspace,