Files
pipelines/internal/pipeline/chat_shown_run_map.go
T
alan 7872e57d40 Run card mini-map and stage strip; graph edge hover highlight
The show_run card carries the run's graph and per-stage progress (bounded by
the graph, not the run length) and draws a still mini-map with the run graph
plus a stage strip with status, visits and time. Hovering or tapping an edge
in the pipeline and run graphs highlights it, its label and endpoints, with a
tooltip naming from -> to, the outcome and, for runs, how often it was taken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:56:51 +00:00

98 lines
3.3 KiB
Go

package pipeline
import (
"context"
"encoding/json"
"sort"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// RunShownMap returns the graph and per-stage progress a chat's run card
// draws (protocol.ChatShownRunMap). It is nil, without an error, when the
// graph has more than protocol.ChatShownRunMapStages stages. Only routing and
// lifecycle facts are read: no step inputs, outputs or transcripts.
func (s *Store) RunShownMap(ctx context.Context, id int64) (*protocol.ChatShownRunMap, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
var graphJSON string
if err := tx.QueryRowContext(ctx, `SELECT 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(&graphJSON); err != nil {
return nil, err
}
var graph protocol.Graph
if err := json.Unmarshal([]byte(graphJSON), &graph); err != nil {
return nil, err
}
if len(graph.Stages) > protocol.ChatShownRunMapStages {
return nil, nil
}
rows, err := tx.QueryContext(ctx, `SELECT sequence,stage_key,status,outcome,target,
COALESCE(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
}
defer rows.Close()
result := &protocol.ChatShownRunMap{Stages: []protocol.ChatShownRunStage{}, Visits: []protocol.RunGraphVisit{}}
index := map[string]int{}
for _, stage := range graph.Stages {
if _, seen := index[stage.Key]; seen || stage.Key == "" {
continue
}
index[stage.Key] = len(result.Stages)
result.Stages = append(result.Stages, protocol.ChatShownRunStage{
Key: stage.Key, Type: stage.Type, Edges: stage.Edges, FallbackError: stage.FallbackError,
})
}
latestStage := map[string]protocol.RunGraphVisit{}
latestRoute := map[[3]string]protocol.RunGraphVisit{}
for rows.Next() {
var visit protocol.RunGraphVisit
var started, finished string
if err := rows.Scan(&visit.Sequence, &visit.Stage, &visit.Status, &visit.Outcome, &visit.Target, &started, &finished); err != nil {
return nil, err
}
latestStage[visit.Stage] = visit
if visit.Outcome != "" && visit.Target != "" {
latestRoute[[3]string{visit.Stage, visit.Outcome, visit.Target}] = visit
}
i, declared := index[visit.Stage]
if !declared {
continue
}
stage := &result.Stages[i]
stage.Visits++
stage.Status = visit.Status
stage.RunningSince = 0
start, startErr := time.Parse(time.RFC3339, started)
end, endErr := time.Parse(time.RFC3339, finished)
switch {
case startErr == nil && endErr == nil && !end.Before(start):
stage.Seconds += int64(end.Sub(start) / time.Second)
case startErr == nil && finished == "" && visit.Status == "running":
stage.RunningSince = start.UnixMilli()
}
}
if err := rows.Err(); err != nil {
return nil, err
}
seen := map[int]bool{}
for _, visit := range latestStage {
seen[visit.Sequence] = true
result.Visits = append(result.Visits, visit)
}
for _, visit := range latestRoute {
if !seen[visit.Sequence] {
seen[visit.Sequence] = true
result.Visits = append(result.Visits, visit)
}
}
sort.Slice(result.Visits, func(a, b int) bool { return result.Visits[a].Sequence < result.Visits[b].Sequence })
return result, nil
}