Files
alan c42a19f7d3 Improve workflow UI, retain stage diffs, and add portable pipeline transfers
Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
2026-10-10 20:55:42 +01:00

191 lines
5.6 KiB
Go

package tui
import (
"fmt"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
"github.com/charmbracelet/x/ansi"
)
// Each visit is a separate node, preserving cycles. Only recorded routes are drawn.
func (m Model) runPathGraph() protocol.Graph {
r := m.runDetail
if r == nil {
return protocol.Graph{}
}
graph := protocol.Graph{}
for i, step := range r.Steps {
node := protocol.Stage{Key: m.runVisitKey(i), Edges: map[string]string{}}
if step.Target != "" && step.Outcome != "" {
if protocol.IsTerminal(step.Target) {
node.Edges[step.Outcome] = step.Target
} else if i+1 < len(r.Steps) && r.Steps[i+1].Stage == step.Target {
node.Edges[step.Outcome] = m.runVisitKey(i + 1)
}
}
graph.Stages = append(graph.Stages, node)
}
return graph
}
func (m Model) runVisitKey(index int) string {
step := m.runDetail.Steps[index]
return fmt.Sprintf("%d. %s (%s)", step.Sequence, step.Stage, m.stepProgressStatus(step))
}
func (m Model) runGraphLines(_ int) []string {
graph, selected, appearance := m.runGraphState()
if len(graph.Stages) == 0 {
return []string{"Waiting for the first stage…"}
}
return renderGraphAppearance(graph, selected, appearance)
}
func (m Model) runGraphState() (protocol.Graph, string, *graphAppearance) {
if m.runDetail == nil {
return protocol.Graph{}, "", nil
}
r := m.runDetail
graph := r.Graph
// Older servers may omit the graph. Never substitute a different version.
if len(graph.Stages) == 0 && m.detail != nil && m.detail.ID == r.PipelineID && m.detail.Version == r.Version {
graph = m.detail.Graph
}
index := len(r.Steps) - 1
if m.runStepIndex > 0 && m.runStepIndex <= len(r.Steps) {
index = m.runStepIndex - 1
}
if len(graph.Stages) == 0 {
graph = m.runPathGraph()
selected := ""
if index >= 0 {
selected = graph.Stages[index].Key
}
return graph, selected, nil
}
appearance := &graphAppearance{nodes: map[string]bool{}, routes: map[graphRoute]bool{}, labels: map[string]string{}}
for _, stage := range graph.Stages {
appearance.labels[stage.Key] = stage.Key + " · ○ pending"
}
for _, step := range r.Steps {
appearance.nodes[step.Stage] = true
appearance.labels[step.Stage] = step.Stage + " · " + ansi.Strip(m.progress(m.stepProgressStatus(step)))
if step.Target != "" && step.Outcome != "" {
target := protocol.ResolveTarget(step.Outcome, step.Target)
appearance.routes[graphRoute{step.Stage, step.Outcome, target}] = true
if protocol.IsTerminal(target) {
appearance.nodes[target] = true
}
}
}
selected := r.CurrentStage
if index >= 0 {
selected = r.Steps[index].Stage
}
return graph, selected, appearance
}
func (m Model) runsView(height int) string {
if m.runDetail == nil {
return m.runsDetailsView(height)
}
graphHeight, detailHeight := m.runPaneHeights(height)
top, pan := m.runGraphTop, m.runGraphPan
if !m.runGraphFocus {
top, pan = m.runGraphFollow()
}
graph := ""
if graphHeight > 0 {
if graphHeight <= 2 {
graph = fitBlock([]string{bar("Run graph", "Tab to expand", m.width, false)}, m.width, graphHeight)
} else {
graph = m.runGraphPanel(graphHeight, top, pan)
}
}
details := m.runsDetailsView(detailHeight)
parts := []string{}
if detailHeight > 0 {
parts = append(parts, details)
}
if graphHeight > 0 {
parts = append(parts, graph)
}
return fitBlock(strings.Split(strings.Join(parts, "\n"), "\n"), m.width, height)
}
func (m Model) runPaneHeights(height int) (int, int) {
if m.runGraphFocus {
details := min(2, max(0, height-6))
return height - details, details
}
if height < 8 {
return 0, height
}
lines := m.runGraphLines(max(1, m.width-4))
widest := 0
for _, line := range lines {
widest = max(widest, ansi.StringWidth(line))
}
graphHeight := len(lines) + 5
if graphHeight > height-12 || widest > max(1, m.width-4) {
graphHeight = 2
}
return graphHeight, height - graphHeight
}
func (m Model) runGraphTitle() string {
r := m.runDetail
if r == nil {
return "Run graph"
}
durationLabel := "elapsed"
if r.FinishedAt != "" {
durationLabel = "duration"
}
title := fmt.Sprintf("Run graph · %d recorded visits · %s %s", len(r.Steps), durationLabel, runDuration(r.CreatedAt, r.FinishedAt))
if m.runGraphFocus {
title = "▸ " + title
}
return title
}
func (m Model) runGraphFollow() (int, int) {
graph, selected, appearance := m.runGraphState()
if len(graph.Stages) == 0 {
return 0, 0
}
graphHeight, _ := m.runPaneHeights(max(0, m.height-4))
rows := max(1, graphHeight-5)
layout := layoutGraphAppearance(graph, appearance)
for _, node := range layout.nodes {
if node.key == selected {
return max(0, node.y-rows/2), max(0, node.x-(m.width-4-node.width)/2)
}
}
return 0, 0
}
func (m Model) runGraphPanel(height, top, pan int) string {
width, rows := max(1, m.width-4), max(0, height-5)
lines := m.runGraphLines(width)
widest := 0
for _, line := range lines {
widest = max(widest, ansi.StringWidth(line))
}
pan = max(0, min(pan, max(0, widest-width)))
top = max(0, min(top, max(0, len(lines)-rows)))
leftPad, topPad := max(0, (width-widest)/2), max(0, (rows-len(lines))/2)
body := make([]string, topPad)
for _, line := range lines[top:min(len(lines), top+rows-topPad)] {
body = append(body, strings.Repeat(" ", leftPad)+ansi.Cut(line, pan, pan+width-leftPad))
}
label := m.runGraphTitle()
footer := "Dimmed stages and routes were not executed."
if widest > width || len(lines) > rows {
footer = "h/j/k/l pan · c center · dim = not executed"
}
header := runPaneTitle(strings.TrimPrefix(label, "▸ "), "", width, m.runGraphFocus)
return panel(m.width, height, []string{header, ""}, body, footer, m.runGraphFocus)
}