web: clearer workflow graph labels, routes and kinds

- Gaps between stages are sized from the widest straight-route label, so a chip
  never reaches into either box; a source's forward routes fan across its side
  instead of sharing a 20px band.
- A label prefers the widest horizontal run that can hold it; when only a stub
  exists the chip moves onto the edge's longest run and sits beside a vertical
  one, keeping clear of both the box and the neighbouring label.
- Hovering a wire or its chip highlights the route and dims the rest.
- System-added routes (the parser's implicit error fallback) draw dotted with a
  legend entry, distinct from declared routes and executed state.
- Lanes are 34px apart, so a dense definition no longer doubles the canvas height.
- The graph opens expanded by default and remembers a collapse (graphPrefs.js).
This commit is contained in:
2026-10-19 22:23:58 +01:00
parent 1eaedf5c53
commit b00b434c5d
8 changed files with 313 additions and 58 deletions
+4 -1
View File
@@ -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,
+178 -56
View File
@@ -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);
}
+22
View File
@@ -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.
}
}
+2
View File
@@ -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;
+6
View File
@@ -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;
}