diff --git a/web/graph.css b/web/graph.css index ba8ba6c9..d9f15842 100644 --- a/web/graph.css +++ b/web/graph.css @@ -27,12 +27,25 @@ .graph-content { position: absolute; top: 0; left: 0; will-change: transform; } .graph-edges { position: absolute; top: 0; left: 0; overflow: visible; pointer-events: none; } -.graph-edge-path { stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; } +.graph-edge-path { + stroke-width: 3; + stroke-linecap: round; + stroke-linejoin: round; + /* The canvas ignores the pointer so panning works; a wire takes the pointer + back on its own stroke only, which is what makes hover-highlighting work. */ + pointer-events: stroke; + transition: opacity 120ms ease-out; +} .graph-edge-path.not-executed { stroke-width: 2.5; stroke-dasharray: 14 10; } +/* A system-added route (implicit error fallback): dotted, not merely dashed. */ +.graph-edge-path.fallback { stroke-dasharray: 2 8; stroke-linecap: round; } +.graph-edge-path.hot { stroke-width: 5; } +.graph-edges.has-hot .graph-edge-path:not(.hot) { opacity: 0.3; } .graph-arrowhead path { fill: var(--text); fill: context-stroke; } .graph-node-layer, .graph-label-layer { position: absolute; top: 0; left: 0; } .graph-label-layer { pointer-events: none; } +.graph-edge-label { pointer-events: auto; transition: opacity 120ms ease-out; } .graph-node { position: absolute; @@ -93,6 +106,15 @@ font-size: 11px; white-space: nowrap; } +.graph-edge-label.fallback { border-style: dotted; } +/* A label on a vertical run sits beside it, centred on the wire's midpoint. */ +.graph-edge-label.vertical { transform: translate(-50%, -50%); } +.graph-edge-label.hot { + border-color: var(--accent); + color: var(--color-text); + background: var(--color-surface-2); +} +.graph-label-layer.has-hot .graph-edge-label:not(.hot) { opacity: 0.45; } .graph-controls { position: absolute; top: 8px; right: 8px; z-index: 2; display: flex; gap: 6px; } .graph-control { @@ -141,6 +163,7 @@ border-bottom: 4px solid transparent; } .graph-legend-swatch.not-executed { border-top-style: dashed; } +.graph-legend-swatch.fallback { border-top: 2px dotted var(--muted); } /* The live active stage (R5): a ring around the node plus a soft glow, pulsing while the run executes and static when the user prefers reduced motion. */ diff --git a/web/src/app.js b/web/src/app.js index 410ae738..a156ad6c 100644 --- a/web/src/app.js +++ b/web/src/app.js @@ -25,6 +25,7 @@ import { } from "./runEvents.js"; import { SERVER_URL_STORAGE_KEY, readServerUrl, readWorkspace, writeServerUrl } from "./settings.js"; import { layoutGraph } from "./graph.js"; +import { readGraphExpanded } from "./graphPrefs.js"; import { DEFAULT_ROUTE, parseRoute, serializeRoute } from "./routes.js"; import { EMPTY_PIPELINE_FILTERS, PIPELINE_DETAIL_TABS } from "./pipelines.js"; import { @@ -155,11 +156,13 @@ const state = { // The mounted workflow graph (P6) and whether its panel is expanded. The // handle is dropped on every re-render so its listeners can never leak. pipelineGraph: null, - pipelineGraphExpanded: false, + // Expanded by default; a collapse is remembered (graphPrefs.js). + pipelineGraphExpanded: readGraphExpanded(), // The mounted run-mode graph (R5) on the run detail's Graph tab. Dropped on // every re-render and on a run change, so its wheel/pointer/resize listeners // and its elapsed timer can never leak. runGraph: null, + runGraphExpanded: readGraphExpanded(), versionsExpanded: false, pipelinesLoaded: false, runsLoaded: false, diff --git a/web/src/graph.js b/web/src/graph.js index 55e20c90..9acbc767 100644 --- a/web/src/graph.js +++ b/web/src/graph.js @@ -137,6 +137,9 @@ export function graphEdges(graph) { outcome, target: resolveTarget(outcome, raw), label: edgeLabel(stage, outcome), + // The parser adds `error -> #failure` when a stage declares no error + // route; that edge is the system's, not the author's. + fallback: outcome === "error" && Boolean(stage.fallback_error), }); } } @@ -352,12 +355,30 @@ export const DEFAULT_GRAPH_METRICS = Object.freeze({ nodeHeight: 76, hGap: 60, pad: 24, - laneSpacing: 44, + // A lane only has to fit its 18px outcome chip plus a gap, so 34 keeps a + // dense definition's lanes from stretching the canvas to twice its height. + laneSpacing: 34, terminalGap: 88, trackStep: 40, portSpacing: 20, + // Edge labels are DOM chips over the SVG, so the layout has to reserve room + // for them itself: a wide chip between two adjacent stages would otherwise + // overprint both boxes (the layered-drawing "label dummy" problem). + labelCharWidth: 6.4, + labelPadding: 10, + // The rendered chip is ~24px tall at the 11px label type, so routes that + // share a band must clear that plus a gap. + labelHeight: 24, + labelGap: 8, }); +/** The chip width a layout reserves for an outcome label (deterministic, no DOM). */ +export function labelWidthFor(label, metrics = DEFAULT_GRAPH_METRICS) { + const m = { ...DEFAULT_GRAPH_METRICS, ...(metrics ?? {}) }; + const text = String(label ?? ""); + return Math.max(28, Math.round(text.length * m.labelCharWidth) + 16); +} + /** Centred fan for the k-th of n ports: 0, +s, -s, +2s, -2s, ... (portOffset). */ export function portOffset(index, spacing) { if (index <= 0) return 0; @@ -448,29 +469,46 @@ export function routePoints(edge) { } /** - * Where an edge's outcome label sits: the midpoint of its widest horizontal run - * (its own lane), so distinct outcomes never overprint. Pure and stored on the + * Where an edge's outcome label sits. The preferred spot is the midpoint of the + * widest horizontal run that can actually hold the chip; a side exit with only a + * short stub would otherwise park its label on top of a forward route's label. + * When no horizontal run fits, the longest run wins, and a vertical one is + * flagged so the chip can sit beside the wire in the gap. Pure and stored on the * layout so drawing never recomputes it. */ -export function labelPointFor(edge, points) { - if (points.length < 2) return { ...edge.from }; - if (!edge.bottomExit && !edge.topEntry && !edge.across) { - const first = points[0]; - const last = points[points.length - 1]; - return { x: (first.x + last.x) / 2, y: (first.y + last.y) / 2 }; - } - let best = null; - let bestWidth = -1; +export function labelPointFor(edge, points, labelWidth = 0) { + if (points.length < 2) return { ...edge.from, vertical: false }; + let bestH = null; + let bestHw = -1; + let bestV = null; + let bestVh = -1; for (let i = 1; i < points.length; i += 1) { const a = points[i - 1]; const b = points[i]; - if (a.y === b.y && Math.abs(b.x - a.x) > bestWidth) { - bestWidth = Math.abs(b.x - a.x); - best = [a, b]; + if (a.y === b.y) { + const width = Math.abs(b.x - a.x); + if (width > bestHw) { + bestHw = width; + bestH = [a, b]; + } + } else if (a.x === b.x) { + const height = Math.abs(b.y - a.y); + if (height > bestVh) { + bestVh = height; + bestV = [a, b]; + } } } - if (!best) return { ...points[0] }; - return { x: (best[0].x + best[1].x) / 2, y: best[0].y }; + const fits = (run) => run != null && bestHw >= Math.max(labelWidth, 24); + if (fits(bestH)) { + return { x: (bestH[0].x + bestH[1].x) / 2, y: bestH[0].y, vertical: false }; + } + if (bestV && bestVh >= bestHw) { + return { x: bestV[0].x, y: (bestV[0].y + bestV[1].y) / 2, vertical: true }; + } + if (bestH) return { x: (bestH[0].x + bestH[1].x) / 2, y: bestH[0].y, vertical: false }; + if (bestV) return { x: bestV[0].x, y: (bestV[0].y + bestV[1].y) / 2, vertical: true }; + return { ...points[0], vertical: false }; } /** @@ -495,13 +533,32 @@ export function layoutGraph(graph, metrics = {}) { const stageCount = stages.length; const contentLeft = m.pad + m.hGap; - const stageX = (index) => contentLeft + index * (m.nodeWidth + m.hGap); - const stageRowRight = stageX(stageCount - 1) + m.nodeWidth; const stageBottom = m.pad + m.nodeHeight; const isForward = (edge) => !isTerminal(edge.target) && (rank.get(edge.target) ?? -1) === (rank.get(edge.source) ?? -2) + 1; + // A gap is as wide as its widest straight-route label plus breathing room, so + // a chip never reaches into either neighbouring box. This is the standard + // layered-drawing answer to edge labels: reserve the space up front instead of + // discovering the collision after drawing. + const gapWidths = new Array(stageCount).fill(m.hGap); + for (const edge of edges) { + if (!isForward(edge)) continue; + const targetRank = rank.get(edge.target); + if (targetRank == null) continue; + gapWidths[targetRank] = Math.max( + gapWidths[targetRank], + labelWidthFor(edge.label, m) + 2 * m.labelPadding, + ); + } + const stageX = (index) => { + let x = contentLeft; + for (let i = 1; i <= index; i += 1) x += gapWidths[i] + m.nodeWidth; + return x; + }; + const stageRowRight = stageX(stageCount - 1) + m.nodeWidth; + // Every non-forward edge needs its own cross lane, stacked below the stage row // in stable edge-id order; the terminal row sits below all of them. const laneIds = edges.filter((edge) => !isForward(edge)); @@ -574,6 +631,16 @@ export function layoutGraph(graph, metrics = {}) { const horizontals = segmentAllocator(m.portSpacing); const verticals = segmentAllocator(m.trackStep / 3); + const gapContaining = (x) => { + if (x < contentLeft) return [m.pad, contentLeft]; + for (let gap = 1; gap < stageCount; gap += 1) { + const lo = stageX(gap - 1) + m.nodeWidth; + const hi = stageX(gap); + if (x <= hi) return [lo, hi]; + } + return [stageRowRight, stageRowRight + m.hGap]; + }; + const gapBounds = (gap) => { if (gap <= 0) return [m.pad, contentLeft]; if (gap >= stageCount) { @@ -585,29 +652,42 @@ export function layoutGraph(graph, metrics = {}) { // Forward edges are allocated first: a straight forward route keeps a single // horizontal segment by sharing one y between its source and target ports. - const forwardEdges = edges.filter(isForward); + // Grouping per source (not one global index) lets a stage's own routes fan + // across its side at a spacing that keeps their two chips apart, instead of + // piling every forward edge in the definition into the same 20px band. + const forwardBySource = new Map(); + for (const edge of edges) { + if (!isForward(edge)) continue; + const list = forwardBySource.get(edge.source) ?? []; + list.push(edge); + forwardBySource.set(edge.source, list); + } const forwardY = new Map(); - forwardEdges.forEach((edge, index) => { - const source = byKey.get(edge.source); - const target = byKey.get(edge.target); - if (!source || !target) return; - const y = - source.y + - source.height / 2 - - ((forwardEdges.length - 1) / 2) * m.portSpacing + - index * m.portSpacing; - forwardY.set( - edge.id, - horizontals.allocate( - y, - source.x + source.width, - target.x, - source.y + m.portSpacing / 2, - source.y + source.height - m.portSpacing / 2, - m.portSpacing, - ), - ); - }); + for (const list of forwardBySource.values()) { + const source = byKey.get(list[0].source); + if (!source) continue; + const n = list.length; + const usable = source.height - m.portSpacing; + const spacing = + n > 1 ? Math.min(Math.max(m.labelHeight + m.labelGap, m.portSpacing), usable / (n - 1)) : 0; + const first = source.y + source.height / 2 - ((n - 1) / 2) * spacing; + list.forEach((edge, index) => { + const target = byKey.get(edge.target); + if (!target) return; + const desired = first + index * spacing; + forwardY.set( + edge.id, + horizontals.allocate( + desired, + source.x + source.width, + target.x, + desired, + desired, + Math.max(spacing, m.portSpacing), + ), + ); + }); + } const portMin = (node) => node.y + m.portSpacing / 2; const portMax = (node) => node.y + node.height - m.portSpacing / 2; @@ -741,6 +821,7 @@ export function layoutGraph(graph, metrics = {}) { outcome: edge.outcome, target: edge.target, label: edge.label, + fallback: Boolean(edge.fallback), from: { x: fromX, y: fromY }, to: { x: toX, y: toY }, forward, @@ -758,7 +839,14 @@ export function layoutGraph(graph, metrics = {}) { labelPoint: { x: 0, y: 0 }, }; layout.points = routePoints(layout); - layout.labelPoint = labelPointFor(layout, layout.points); + const width = labelWidthFor(layout.label, m); + layout.labelPoint = labelPointFor(layout, layout.points, width); + if (layout.labelPoint.vertical) { + // Keep a chip on a vertical run inside its own gap, clear of both boxes. + const half = width / 2 + 2; + const [lo, hi] = gapContaining(layout.labelPoint.x); + layout.labelPoint.x = Math.min(Math.max(layout.labelPoint.x, lo + half), Math.max(lo + half, hi - half)); + } return layout; }); @@ -991,18 +1079,21 @@ export function mount(container, graph, options = {}) { defs.append(marker); svg.append(defs); + const pathsById = new Map(); for (const edge of layout.edges) { if (edge.points.length < 2) continue; const executed = edgeIsExecuted(edge, runState); + const kind = edge.fallback ? "fallback" : executed ? "executed" : "not-executed"; const path = svgEl("path", { - class: `graph-edge-path${executed ? " executed" : " not-executed"}`, + class: `graph-edge-path ${kind}`, d: pathFromPoints(edge.points), "data-edge-id": edge.id, "data-outcome": edge.outcome, fill: "none", }); - path.style.stroke = executed ? edgeStrokeColor(edge.outcome) : "var(--muted)"; - if (executed) path.setAttribute("marker-end", `url(#${markerId})`); + path.style.stroke = executed || edge.fallback ? edgeStrokeColor(edge.outcome) : "var(--muted)"; + if (executed || edge.fallback) path.setAttribute("marker-end", `url(#${markerId})`); + pathsById.set(edge.id, path); svg.append(path); } content.append(svg); @@ -1012,16 +1103,41 @@ export function mount(container, graph, options = {}) { // error/blocked/retry outcomes too, and labelling all of them buried the graph // under a wall of chips. A definition (no runState) still labels every route. const labelLayer = el("div", "graph-label-layer"); + const chipsById = new Map(); for (const edge of layout.edges) { if (runState && !edgeIsExecuted(edge, runState)) continue; - const chip = el("span", "graph-edge-label", edge.label); + const chipClass = [ + "graph-edge-label", + edge.fallback ? "fallback" : "", + edge.labelPoint.vertical ? "vertical" : "", + ].filter(Boolean).join(" "); + const chip = el("span", chipClass, edge.label); chip.style.left = `${edge.labelPoint.x}px`; chip.style.top = `${edge.labelPoint.y}px`; chip.setAttribute("data-outcome", edge.outcome); + chip.setAttribute("data-edge-id", edge.id); + chipsById.set(edge.id, chip); labelLayer.append(chip); } content.append(labelLayer); + // Hovering either the wire or its chip lights the pair and dims the rest, so a + // route is traceable through the crossing lanes without following every bend. + function setEdgeHot(id, hot) { + pathsById.get(id)?.classList.toggle("hot", hot); + chipsById.get(id)?.classList.toggle("hot", hot); + svg.classList.toggle("has-hot", hot); + labelLayer.classList.toggle("has-hot", hot); + } + for (const [id, path] of pathsById) { + path.addEventListener("pointerenter", () => setEdgeHot(id, true)); + path.addEventListener("pointerleave", () => setEdgeHot(id, false)); + const chip = chipsById.get(id); + if (!chip) continue; + chip.addEventListener("pointerenter", () => setEdgeHot(id, true)); + chip.addEventListener("pointerleave", () => setEdgeHot(id, false)); + } + // Node layer: real buttons, positioned in graph space. const nodeLayer = el("div", "graph-node-layer"); let elapsedBadge = null; @@ -1080,19 +1196,25 @@ export function mount(container, graph, options = {}) { controls.append(fitButton, expandButton); viewport.append(controls); - // The legend, only under a run (never the definition-only view): a solid, - // arrowed swatch means "executed", a dashed, arrowless one "not executed", - // matching what the graph draws. - if (runState != null) { + // The legend: a solid, arrowed swatch means "executed", a dashed, arrowless + // one "not executed", and a dotted one a system-added fallback (the parser's + // implicit `error -> #failure`). A definition shows only the fallback key. + const legendEntries = runState != null + ? [ + { label: "Executed", kind: "executed" }, + { label: "Not executed", kind: "not-executed" }, + ] + : []; + if (layout.edges.some((edge) => edge.fallback)) { + legendEntries.push({ label: "System fallback", kind: "fallback" }); + } + if (legendEntries.length > 0) { const legend = el("div", "graph-legend"); legend.setAttribute("role", "note"); - legend.setAttribute("aria-label", "Executed and not executed routes"); - for (const entry of [ - { label: "Executed", executed: true }, - { label: "Not executed", executed: false }, - ]) { + legend.setAttribute("aria-label", "Route kinds"); + for (const entry of legendEntries) { const item = el("span", "graph-legend-entry"); - item.append(el("span", `graph-legend-swatch ${entry.executed ? "executed" : "not-executed"}`)); + item.append(el("span", `graph-legend-swatch ${entry.kind}`)); item.append(el("span", "graph-legend-label", entry.label)); legend.append(item); } diff --git a/web/src/graphPrefs.js b/web/src/graphPrefs.js new file mode 100644 index 00000000..7ddc8e34 --- /dev/null +++ b/web/src/graphPrefs.js @@ -0,0 +1,22 @@ +// The workflow graph's expansion preference. The graph is expanded by default; +// collapsing it is a deliberate choice and is remembered across reloads, so the +// operator's next visit opens the graph the way they left it. + +export const GRAPH_EXPANDED_KEY = "pipelines.web.graphExpanded.v1"; + +/** True unless the operator collapsed the graph (a missing value means default). */ +export function readGraphExpanded(storage = globalThis.localStorage) { + try { + return storage?.getItem(GRAPH_EXPANDED_KEY) !== "0"; + } catch { + return true; + } +} + +export function writeGraphExpanded(value, storage = globalThis.localStorage) { + try { + storage?.setItem(GRAPH_EXPANDED_KEY, value ? "1" : "0"); + } catch { + // Storage may be unavailable; the in-memory choice still applies. + } +} diff --git a/web/src/views/pipelines.js b/web/src/views/pipelines.js index e4c9f734..16839777 100644 --- a/web/src/views/pipelines.js +++ b/web/src/views/pipelines.js @@ -24,6 +24,7 @@ import { versionRow, } from "../pipelines.js"; import { PIPELINE_GRAPH_EMPTY, mount as mountGraph } from "../graph.js"; +import { writeGraphExpanded } from "../graphPrefs.js"; export function createPipelinesView(rt) { const { state, repo } = rt; @@ -303,6 +304,7 @@ function renderPipelineDetail() { }, onExpandedChange: (value) => { state.pipelineGraphExpanded = value; + writeGraphExpanded(value); }, }); return pane; diff --git a/web/src/views/runDetail.js b/web/src/views/runDetail.js index d50aea8a..28eaf3ee 100644 --- a/web/src/views/runDetail.js +++ b/web/src/views/runDetail.js @@ -9,6 +9,7 @@ import { liveBadgeState } from "../connection.js"; import { resolveArtifactUrl } from "../serverUrl.js"; import { runStateOf } from "../runs.js"; import { PIPELINE_GRAPH_EMPTY, RUN_GRAPH_MISSING, mount as mountGraph } from "../graph.js"; +import { writeGraphExpanded } from "../graphPrefs.js"; import { RENDERED_RUN_TABS, EMPTY_INPUT_MESSAGE, @@ -174,7 +175,12 @@ function renderRunGraphTab(d) { wrap.append(host); state.runGraph = mountGraph(host, d.graph, { runState: runGraphState(d), + expanded: state.runGraphExpanded, onStageTap: (key) => openStagePane(key, latestVisitIndex(d, key)), + onExpandedChange: (value) => { + state.runGraphExpanded = value; + writeGraphExpanded(value); + }, }); return wrap; } diff --git a/web/test/graph.test.mjs b/web/test/graph.test.mjs index 30ad9ced..8525ce9f 100644 --- a/web/test/graph.test.mjs +++ b/web/test/graph.test.mjs @@ -20,6 +20,7 @@ import { edgeLabel, graphEdges, graphNodeKind, + labelWidthFor, graphDescription, hitTest, layoutGraph, @@ -203,6 +204,50 @@ test("layoutGraph labels sit on the edge's own widest horizontal run", () => { } }); +test("a gap is at least as wide as the widest straight-route label plus padding", () => { + const graph = { + stages: [ + { key: "a", edges: { success: "b", repair_attempt: "b", error: "#" }, fallback_error: true }, + { key: "b", edges: { success: "#success" } }, + ], + }; + const layout = layoutGraph(graph); + const [a, b] = layout.nodes.filter((node) => !node.terminal); + const gap = b.x - (a.x + a.width); + const widest = Math.max(labelWidthFor("success"), labelWidthFor("repair_attempt")); + assert.ok(gap >= widest + 2 * 10, `gap ${gap} fits the ${widest}px chip`); +}); + +test("a source's straight routes fan across its side instead of one shared band", () => { + const graph = { + stages: [ + { key: "a", edges: { success: "b", repair_attempt: "b", error: "#" }, fallback_error: true }, + { key: "b", edges: { success: "#success" } }, + ], + }; + const layout = layoutGraph(graph); + const ys = layout.edges + .filter((edge) => edge.forward && edge.source === "a") + .map((edge) => edge.from.y) + .sort((x, y) => x - y); + assert.equal(ys.length, 2); + assert.ok(ys[1] - ys[0] >= 18, "their chips (18px tall) cannot touch"); +}); + +test("graphEdges marks only the parser's implicit error fallback", () => { + const graph = { + stages: [ + { key: "a", edges: { success: "b", error: "#" }, fallback_error: true }, + { key: "b", edges: { success: "#success", error: "#" }, fallback_error: false }, + ], + }; + const edges = graphEdges(graph); + assert.equal(edges.filter((edge) => edge.fallback).length, 1); + assert.equal(edges.find((edge) => edge.fallback).source, "a"); + const layout = layoutGraph(graph); + assert.equal(layout.edges.filter((edge) => edge.fallback).length, 1); +}); + test("layoutGraph is empty and finite for a graph with no stages", () => { const layout = layoutGraph({ stages: [] }); assert.deepEqual(layout.nodes, []); diff --git a/web/test/graphPrefs.test.mjs b/web/test/graphPrefs.test.mjs new file mode 100644 index 00000000..3268b759 --- /dev/null +++ b/web/test/graphPrefs.test.mjs @@ -0,0 +1,32 @@ +// The graph expansion preference: expanded by default, a collapse remembered. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { GRAPH_EXPANDED_KEY, readGraphExpanded, writeGraphExpanded } from "../src/graphPrefs.js"; + +function fakeStorage(initial = {}) { + const values = new Map(Object.entries(initial)); + return { + getItem: (key) => (values.has(key) ? values.get(key) : null), + setItem: (key, value) => values.set(key, String(value)), + values, + }; +} + +test("the graph is expanded when nothing was stored", () => { + assert.equal(readGraphExpanded(fakeStorage()), true); +}); + +test("a collapse survives and expands again on request", () => { + const storage = fakeStorage(); + writeGraphExpanded(false, storage); + assert.equal(storage.values.get(GRAPH_EXPANDED_KEY), "0"); + assert.equal(readGraphExpanded(storage), false); + writeGraphExpanded(true, storage); + assert.equal(readGraphExpanded(storage), true); +}); + +test("storage failures fall back to the expanded default", () => { + const broken = { getItem() { throw new Error("denied"); }, setItem() { throw new Error("denied"); } }; + assert.equal(readGraphExpanded(broken), true); + assert.doesNotThrow(() => writeGraphExpanded(false, broken)); +});