diff --git a/README.md b/README.md index f70935b1..72b0b0ec 100644 --- a/README.md +++ b/README.md @@ -225,7 +225,9 @@ CLI: `pipelines status [--json]`. Candidate URLs are not verified reachable; publishing endpoints and credentials are not disclosed. Retry is offered only after the failed run and its agent have fully stopped. Graphs use content-sized boxes/columns and tightly spaced branch lanes while -retaining every stage and labeled box-and-arrow route. +retaining every stage and labeled box-and-arrow route. Incoming route IDs (left) +and outgoing outcomes (right) share rows to reduce box height. Both pipeline and +run views center the graph horizontally and vertically when it fits. The run inspector shows the entire pinned graph above the details, dimming stages and routes not executed. **g** focuses the graph; **hjkl** or arrows pan it. The path and elapsed duration update while running. **a** opens the active/latest diff --git a/docs/cli.md b/docs/cli.md index 4eb893f3..75f4a38c 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -214,7 +214,12 @@ Graph boxes are sized to their contents rather than the longest label anywhere in the graph. Fork columns share only their own required width, route tracks are adjacent, and branch spacing follows stacked boxes rather than tall trunk/merge nodes. All stages, outcome labels, ports, and terminal arrows remain visible in -the full pannable diagram. +the full pannable diagram. Incoming route IDs are shown on the left and outgoing +outcome labels on the right of shared rows, without separate input/output +sections or divider rows. This reduces height while allowing enough width to +keep both labels readable. Failure-bound error exits remain at the bottom. +Pipeline and run graph panes both center horizontally and vertically when the +diagram fits, and clamp scroll/pan offsets on resize or graph changes. Press `a` to select the active/latest retained OpenCode agent session and open its tool evidence. Session ID is available during execution; tool transcripts appear diff --git a/internal/tui/graph.go b/internal/tui/graph.go index f24b2f87..a8908e5e 100644 --- a/internal/tui/graph.go +++ b/internal/tui/graph.go @@ -240,18 +240,6 @@ func renderGraphAppearance(graph protocol.Graph, selected string, appearance *gr put(x, row, vertical, color, active) put(x+w-1, row, vertical, color, active) } - if !protocol.IsTerminal(node.key) { - text(x+1, y+2, strings.Repeat("─", w-2), color, false) - incoming := 0 - for _, edge := range layout.edges { - if edge.target == node.key { - incoming++ - } - } - if incoming > 0 { - text(x+1, y+3+incoming, strings.Repeat("─", w-2), color, false) - } - } text(x, y+h-1, bottom, color, active) } line := func(p graphPoint, r rune, color lipgloss.Color) { @@ -319,10 +307,11 @@ func renderGraphAppearance(graph protocol.Graph, selected string, appearance *gr } else { put(end.x, end.y, '▶', color, bold) target := nodes[edge.target] - text(target.x+2, edge.to, fmt.Sprintf("in [%d]", edge.id), color, bold) + text(target.x+2, edge.to, fmt.Sprintf("[%d]", edge.id), color, bold) } source := nodes[edge.source] - text(source.x+2, edge.from, fmt.Sprintf("%s [%d]", edge.label, edge.id), color, bold) + label := fmt.Sprintf("%s [%d]", edge.label, edge.id) + text(source.x+source.width-2-len(label), edge.from, label, color, bold) } result := make([]string, layout.height) for row, cells := range canvas { diff --git a/internal/tui/graph_center_test.go b/internal/tui/graph_center_test.go new file mode 100644 index 00000000..8a62da7f --- /dev/null +++ b/internal/tui/graph_center_test.go @@ -0,0 +1,55 @@ +package tui + +import ( + "context" + "strings" + "testing" + + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "github.com/charmbracelet/x/ansi" +) + +func assertCenteredGraphBody(t *testing.T, view string, graph []string, width, height int) { + t.Helper() + widest := 0 + for _, line := range graph { + widest = max(widest, ansi.StringWidth(line)) + } + inner, rows := width-4, height-5 + left, top := max(0, (inner-widest)/2), max(0, (rows-len(graph))/2) + actual := strings.Split(ansi.Strip(view), "\n") + for i, line := range graph { + want := " " + strings.Repeat(" ", left) + ansi.Strip(line) + if strings.TrimRight(actual[3+top+i], " ") != strings.TrimRight(want, " ") { + t.Fatalf("graph not centered at row %d:\nactual %q\nwant %q", i, actual[3+top+i], want) + } + } +} + +func TestPipelineAndRunGraphsCenterWhenTheyFit(t *testing.T) { + graph, err := pipeline.Parse(pipeline.Template([]string{"worker"}, true)) + if err != nil { + t.Fatal(err) + } + m := New(context.Background(), "") + m.width, m.height = 160, 60 + m.detail = &protocol.PipelineDetail{Graph: graph} + m.keys.detail = true + // Stale panning/scroll values from a larger graph must not prevent centering. + m.pipelineTop, m.graphPan = 99, 99 + for _, focused := range []bool{false, true} { + m.stageFocus = focused + assertCenteredGraphBody(t, m.graphPanel(m.width, 40), m.graphLines(), m.width, 40) + } + m.runDetail = &protocol.RunDetail{Graph: graph} + assertCenteredGraphBody(t, m.runGraphPanel(40, 99, 99), m.runGraphLines(m.width-4), m.width, 40) +} + +func TestGraphViewportKeepsOversizedDiagramPannable(t *testing.T) { + lines := []string{"0123456789abcdefghij", "ABCDEFGHIJKLMNOPQRST"} + view := strings.Split(ansi.Strip(graphViewport("Graph", lines, 14, 7, 99, 99, true, "pan")), "\n") + if strings.TrimSpace(view[3]) != "abcdefghij" || strings.TrimSpace(view[4]) != "KLMNOPQRST" { + t.Fatal("oversized graph should clamp pan without centering/cropping the wrong region", view) + } +} diff --git a/internal/tui/graph_compact_test.go b/internal/tui/graph_compact_test.go index 824fb5d6..bad11704 100644 --- a/internal/tui/graph_compact_test.go +++ b/internal/tui/graph_compact_test.go @@ -18,11 +18,12 @@ func TestCompactGraphDimensions(t *testing.T) { for name, graph := range map[string]protocol.Graph{"linear": linear, "nested": nestedForkGraph(t)} { layout := layoutGraph(graph) t.Logf("%s: %d columns × %d rows", name, layout.width, layout.height) - // Measured before compaction: linear 175×15; nested forks 147×48. - if name == "linear" && (layout.width > 100 || layout.height > 15) { + // Before compaction: linear 175×15; nested forks 147×48. Sharing input + // and outcome rows trades a little width for substantially less height. + if name == "linear" && (layout.width > 108 || layout.height > 12) { t.Fatal("linear graph grew", layout.width, layout.height) } - if name == "nested" && (layout.width > 118 || layout.height > 41) { + if name == "nested" && (layout.width > 134 || layout.height > 32) { t.Fatal("fork graph grew", layout.width, layout.height) } assertForkRouteClearance(t, layout) @@ -36,6 +37,17 @@ func TestCompactGraphDimensions(t *testing.T) { if !strings.Contains(view, fmt.Sprintf("%s [%d]", edge.label, edge.id)) { t.Fatal("compaction lost outcome", edge) } + if !edge.topEntry { + for _, node := range layout.nodes { + if node.key == edge.target { + line := renderGraph(graph)[edge.to] + input := ansi.Strip(ansi.Cut(line, node.x+2, node.x+2+len(fmt.Sprintf("[%d]", edge.id)))) + if input != fmt.Sprintf("[%d]", edge.id) { + t.Fatal("paired outcome overwrote input ID", edge, input) + } + } + } + } } } } diff --git a/internal/tui/graph_forks.go b/internal/tui/graph_forks.go index a01f513d..67dabca2 100644 --- a/internal/tui/graph_forks.go +++ b/internal/tui/graph_forks.go @@ -77,11 +77,6 @@ func layoutForkGraph(graph protocol.Graph, columns map[string]int, order []strin for _, node := range r.nodes { old[node.key] = node } - incoming, outgoing := map[string]int{}, map[string]int{} - for _, edge := range r.edges { - incoming[edge.target]++ - outgoing[edge.source]++ - } // A deterministic spanning tree gives sibling branches disjoint leaf ranges. // Shared descendants are subsequently centered over their incoming branches. tree := map[string][]string{} @@ -168,10 +163,6 @@ func layoutForkGraph(graph protocol.Graph, columns map[string]int, order []strin } for i := 0; i < len(graph.Stages); i++ { node := &r.nodes[i] - node.height = incoming[node.key] + outgoing[node.key] + 4 - if incoming[node.key] > 0 { - node.height++ - } // Only stacked branches set lane spacing; a tall trunk/merge must not // inflate the spacing between every leaf in the graph. if columnCounts[columns[node.key]] > 1 { @@ -234,14 +225,10 @@ func layoutForkGraph(graph protocol.Graph, columns map[string]int, order []strin sort.SliceStable(outputIDs, func(i, j int) bool { return targetRow(outputIDs[i]) < targetRow(outputIDs[j]) }) node := nodes[stage.Key] for row, id := range inputIDs { - r.edges[id].to = node.y + 3 + row - } - divider := 0 - if len(inputIDs) > 0 { - divider = 1 + r.edges[id].to = node.y + 2 + row } for row, id := range outputIDs { - r.edges[id].from = node.y + 3 + len(inputIDs) + divider + row + r.edges[id].from = node.y + 2 + row } for i, edge := range r.edges { if edge.source == stage.Key && edge.bottomExit { diff --git a/internal/tui/graph_layout.go b/internal/tui/graph_layout.go index a6ddad8b..e03a6df6 100644 --- a/internal/tui/graph_layout.go +++ b/internal/tui/graph_layout.go @@ -37,7 +37,7 @@ func layoutSingleRowGraph(graph protocol.Graph) graphLayout { } widths[e.source] = max(widths[e.source], len(fmt.Sprintf("%s [%d]", e.label, e.id))+4) if !protocol.IsTerminal(e.target) { - widths[e.target] = max(widths[e.target], len(fmt.Sprintf("in [%d]", e.id))+4) + widths[e.target] = max(widths[e.target], len(fmt.Sprintf("[%d]", e.id))+4) } if !protocol.IsTerminal(e.target) && index[e.target] != index[e.source]+1 { arcs++ @@ -47,12 +47,29 @@ func layoutSingleRowGraph(graph protocol.Graph) graphLayout { gapStarts := make([]int, count+1) x, mainY, height := 0, arcs, 4 for i, s := range graph.Stages { + inputs, outputs, bottom := 0, 0, 0 + for _, id := range incoming[s.Key] { + inputs = max(inputs, len(fmt.Sprintf("[%d]", r.edges[id].id))) + } + sideOutputs := 0 + for _, id := range outgoing[s.Key] { + e := r.edges[id] + if e.bottomExit { + bottom = 1 + if len(incoming[s.Key]) > 0 { + outputs = max(outputs, len(fmt.Sprintf("%s [%d]", e.label, e.id))) + } + continue + } + sideOutputs++ + outputs = max(outputs, len(fmt.Sprintf("%s [%d]", e.label, e.id))) + } + if inputs > 0 && outputs > 0 { + widths[s.Key] = max(widths[s.Key], inputs+outputs+5) + } gapStarts[i] = x x += gapCount[i] + 3 - h := len(incoming[s.Key]) + len(outgoing[s.Key]) + 4 - if len(incoming[s.Key]) > 0 { - h++ - } + h := max(len(incoming[s.Key]), sideOutputs+bottom) + 3 height = max(height, h) r.nodes = append(r.nodes, graphNode{key: s.Key, x: x, y: mainY, width: widths[s.Key], height: h}) r.positions[s.Key] = mainY @@ -61,15 +78,12 @@ func layoutSingleRowGraph(graph protocol.Graph) graphLayout { gapStarts[count] = x x += gapCount[count] + 3 r.width = x - for i := range r.nodes { - r.nodes[i].height = height - } nodes := map[string]graphNode{} for _, n := range r.nodes { nodes[n.key] = n } - // Inputs occupy the first section; outcomes follow a divider. Within each - // section, upper arcs precede adjacent routes and downward terminal routes. + // Incoming IDs and outgoing labels occupy opposite sides of shared rows. + // Upper arcs precede adjacent routes; failure exits get their own bottom row. type port struct { edge int input bool @@ -99,19 +113,19 @@ func layoutSingleRowGraph(graph protocol.Graph) graphLayout { ports = append(ports, port{id, false, rank}) } sort.SliceStable(ports, func(i, j int) bool { return ports[i].rank < ports[j].rank }) - for row, p := range ports { + inputRow, outputRow := 0, 0 + for _, p := range ports { if p.input { - r.edges[p.edge].to = mainY + 3 + row + r.edges[p.edge].to = mainY + 2 + inputRow + inputRow++ } else { - r.edges[p.edge].from = mainY + 3 + row - if len(incoming[stage.Key]) > 0 { - r.edges[p.edge].from++ - } + r.edges[p.edge].from = mainY + 2 + outputRow + outputRow++ } } for _, id := range outgoing[stage.Key] { if r.edges[id].bottomExit { - r.edges[id].from = mainY + height - 2 + r.edges[id].from = mainY + nodes[stage.Key].height - 2 } } } diff --git a/internal/tui/graph_test.go b/internal/tui/graph_test.go index e45f39b9..6121fdc0 100644 --- a/internal/tui/graph_test.go +++ b/internal/tui/graph_test.go @@ -110,7 +110,7 @@ func TestAdjacentFlowStaysBetweenBoxes(t *testing.T) { if edge.outcome == "success" && !edge.topEntry { from, to := nodes[edge.source], nodes[edge.target] for _, point := range edge.path { - if point.x < from.x+from.width-1 || point.x > to.x || point.y < from.y+3 || point.y >= from.y+from.height-1 { + if point.x < from.x+from.width-1 || point.x > to.x || point.y < from.y+2 || point.y >= from.y+from.height-1 { t.Fatalf("normal flow leaves the gap: %+v", edge) } } @@ -191,18 +191,15 @@ func TestOrderedPortsAndBottomFailureExits(t *testing.T) { if incoming == 0 { continue } - divider := node.y + 3 + incoming + title := node.y + 1 for _, edge := range layout.edges { - if edge.target == node.key && edge.to >= divider { - t.Fatal("inputs must precede the section divider") + if edge.target == node.key && (edge.to <= title || edge.to >= node.y+node.height-1) { + t.Fatal("inputs must stay within compact port rows") } - if edge.source == node.key && edge.from <= divider { - t.Fatal("outcomes must follow the section divider") + if edge.source == node.key && edge.from <= title { + t.Fatal("outcomes must follow the title") } } - if !strings.Contains(renderGraph(graph)[divider], strings.Repeat("─", node.width-2)) { - t.Fatal("missing input/outcome separator") - } } view := renderGraph(graph) for row := 0; row < nodes["planner"].y+nodes["planner"].height; row++ { diff --git a/internal/tui/highlights_test.go b/internal/tui/highlights_test.go index b4023509..928a81c2 100644 --- a/internal/tui/highlights_test.go +++ b/internal/tui/highlights_test.go @@ -181,7 +181,7 @@ func TestEdgesAreColoredIncludingInjectedErrors(t *testing.T) { if edge.outcome == "success" && color != 2 { t.Fatal("success outcomes must use terminal green") } - port, row := fmt.Sprintf("in [%d]", edge.id), edge.to + port, row := fmt.Sprintf("[%d]", edge.id), edge.to if edge.topEntry { port = fmt.Sprintf("[%d]", edge.id) row-- diff --git a/internal/tui/panels.go b/internal/tui/panels.go index 435f050c..7258ad8b 100644 --- a/internal/tui/panels.go +++ b/internal/tui/panels.go @@ -191,14 +191,16 @@ func graphViewport(label string, lines []string, width, height, scrollTop, pan i rows := max(0, height-5) top := max(0, min(scrollTop, max(0, len(lines)-rows))) end := min(len(lines), top+rows) - body := make([]string, 0, end-top) - for _, line := range lines[top:end] { - body = append(body, ansi.Cut(line, pan, pan+inner)) - } graphWidth := 0 for _, line := range lines { graphWidth = max(graphWidth, ansi.StringWidth(line)) } + pan = max(0, min(pan, max(0, graphWidth-inner))) + leftPad, topPad := max(0, (inner-graphWidth)/2), max(0, (rows-len(lines))/2) + body := make([]string, topPad) + for _, line := range lines[top:end] { + body = append(body, strings.Repeat(" ", leftPad)+ansi.Cut(line, pan, pan+inner-leftPad)) + } if pan > 0 { label = "← " + label }