Make executed vs not-executed unmistakable in the run graph

Carry the two states structurally instead of by fading, so the difference
survives a screenshot, a dim screen and a colour-blind reader.

- Executed edges: solid full-colour stroke plus an arrowhead.
- Not-executed edges: dashed muted stroke (INACTIVE_DASH_EFFECT), no arrowhead.
- Executed nodes: keep their fill and normal border.
- Not-executed nodes: no fill, dashed muted border and a muted but legible label.
- Add a one-line legend (GRAPH_LEGEND / GraphLegendEntry / LegendSwatch) pinned
  TopStart, rendered only when a run is selected.
- Replace the private EdgeLayout.isExecuted/inline visited check with the shared
  edgeIsExecuted/stageIsExecuted predicates and remove the alpha-based dimming.
- Update the KDoc and the run Graph tab help text ("solid ... dashed and outlined").

Tests: executed/inactive predicates over the definition view and a RunGraphState,
the dash pattern as a positive even-length rhythm, and the legend naming both
states with executed first.
This commit is contained in:
2026-10-16 07:04:48 +01:00
parent 54af4535d7
commit 9c47527def
4 changed files with 340 additions and 48 deletions
@@ -5,6 +5,7 @@ import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.gestures.detectTransformGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
@@ -22,6 +23,7 @@ import androidx.compose.material3.ColorScheme
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
@@ -39,6 +41,7 @@ import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Stroke
@@ -137,8 +140,13 @@ internal data class ZoomResult(val offset: Offset, val scale: Float)
* Node/edge/outcome semantics come from [GraphSemantics] and [edges], mirroring
* internal/tui/graph.go: content-sized stage nodes, labelled outcome edges with
* the `error*` fallback marker, `#success`/`#failure` terminals and loop routes.
* When [runState] is supplied, executed stages/routes are highlighted and the
* rest dimmed; when it is null the full definition is shown.
* When [runState] is supplied, an executed stage/route is drawn solid and
* full-colour with an arrowhead (nodes filled with a normal border) while a
* not-executed one is dashed and arrowless (nodes hollow with a muted, legible
* label), so the two states differ in shape, fill and border style rather than
* by a faded tint and survive a screenshot, a dim screen and a colour-blind
* reader; a one-line legend names both states. When [runState] is null the full
* definition is shown, unchanged.
*
* A tap that lands inside a node (stage or terminal) calls [onStageTap] with the
* node key; tapping empty space does nothing. Pan/zoom keep working because the
@@ -302,6 +310,33 @@ fun WorkflowGraph(
)
}
}
// A compact one-line legend, only under a run (never the definition-only
// view): a solid, arrowed swatch means "executed", a dashed, arrowless
// one means "not executed", matching what the graph draws. It is pinned
// to the top-start (opposite the Fit/expand controls) so it stays visible
// with the top of the panel on a short/narrow window, and its surface
// background keeps it legible wherever the panned/zoomed graph sits.
if (runState != null) {
Row(
modifier = Modifier
.align(Alignment.TopStart)
.padding(4.dp)
.background(MaterialTheme.colorScheme.surface.copy(alpha = 0.85f))
.padding(horizontal = 6.dp, vertical = 3.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(10.dp),
) {
GRAPH_LEGEND.forEach { entry ->
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(4.dp),
) {
LegendSwatch(entry.executed, colors)
Text(entry.label, style = MaterialTheme.typography.labelSmall)
}
}
}
}
Row(
modifier = Modifier.align(Alignment.TopEnd).padding(4.dp),
) {
@@ -339,6 +374,41 @@ fun WorkflowGraph(
}
}
/**
* One legend entry: the [label] a reader sees and whether it names the executed
* state. Pure data so the legend's content is asserted in a plain JVM test.
*/
internal data class GraphLegendEntry(val label: String, val executed: Boolean)
/** The two execution states the legend names, executed first. */
internal val GRAPH_LEGEND: List<GraphLegendEntry> = listOf(
GraphLegendEntry("Executed", executed = true),
GraphLegendEntry("Not executed", executed = false),
)
/**
* A small line swatch for one legend entry, drawn with the same stroke style the
* graph uses: a solid, arrowed line when [executed], a dashed, arrowless line
* otherwise.
*/
@Composable
private fun LegendSwatch(executed: Boolean, colors: GraphColors) {
Canvas(Modifier.size(LEGEND_SWATCH_WIDTH, LEGEND_SWATCH_HEIGHT)) {
val y = size.height / 2f
val from = Offset(0f, y)
val to = Offset(size.width, y)
val color = if (executed) colors.nodeText else colors.labelText
drawLine(
color = color,
start = from,
end = to,
strokeWidth = EDGE_STROKE,
pathEffect = if (executed) null else INACTIVE_DASH_EFFECT,
)
if (executed) drawArrowHead(listOf(from, to), color)
}
}
internal class GraphColors(
val success: Color,
val error: Color,
@@ -992,9 +1062,6 @@ private fun DrawScope.drawEdgePaths(
runState: RunGraphState?,
) {
for (edge in layout.edges) {
val executed = edge.isExecuted(runState)
val color = outcomeColor(edge.edge.outcome, colors)
val alpha = if (executed) 1f else 0.25f
// Routing already happened once in layoutGraph; drawing only replays the
// stored polyline, so there is no per-frame recompute.
val points = if (edge.points.isEmpty()) routePoints(edge) else edge.points
@@ -1004,8 +1071,21 @@ private fun DrawScope.drawEdgePaths(
for (i in 1 until points.size) {
path.lineTo(points[i].x, points[i].y)
}
drawPath(path, color = color.copy(alpha = alpha), style = Stroke(width = 3f))
drawArrowHead(points, color.copy(alpha = alpha))
// Executed = solid full-colour stroke plus an arrowhead; not executed =
// dashed muted stroke with no arrowhead. The difference is carried by line
// style and arrowhead presence, never by opacity alone, so it survives a
// screenshot, a dim screen and a colour-blind reader.
if (edgeIsExecuted(edge.edge, runState)) {
val color = outcomeColor(edge.edge.outcome, colors)
drawPath(path, color = color, style = Stroke(width = EDGE_STROKE))
drawArrowHead(points, color)
} else {
drawPath(
path,
color = colors.labelText,
style = Stroke(width = EDGE_STROKE, pathEffect = INACTIVE_DASH_EFFECT),
)
}
}
}
@@ -1013,10 +1093,10 @@ private fun DrawScope.drawEdgePaths(
* Draws every outcome label in a second pass, after all curves, so a
* neighbouring edge can never be drawn across a label. Each label sits on its
* own edge's stored [EdgeLayout.labelPoint] (the edge's own lane/track), so
* distinct outcomes never overprint. Executed edges use a legible on-surface
* variant of their outcome colour (see [outcomeLabelColor]); dimmed edges use a
* muted colour on a near-opaque background instead of inheriting the curve's
* 0.25 alpha, which left the text unreadable.
* distinct outcomes never overprint. An executed edge's label uses its legible
* outcome colour (see [outcomeColor]); a not-executed edge's label uses a muted
* colour on a near-opaque background, matching its dashed stroke rather than
* inheriting a faded curve alpha, which left the text unreadable.
*/
private fun DrawScope.drawEdgeLabels(
layout: GraphLayout,
@@ -1025,7 +1105,7 @@ private fun DrawScope.drawEdgeLabels(
textMeasurer: TextMeasurer,
) {
for (edge in layout.edges) {
val executed = edge.isExecuted(runState)
val executed = edgeIsExecuted(edge.edge, runState)
val textColor = if (executed) outcomeColor(edge.edge.outcome, colors) else colors.labelText
val measured = textMeasurer.measure(edge.edge.label, TextStyle(fontSize = 11.sp, color = textColor))
val labelTopLeft = Offset(
@@ -1085,42 +1165,61 @@ private fun DrawScope.drawNodes(
textMeasurer: TextMeasurer,
) {
for (node in layout.nodes) {
val executed = runState == null || node.key in runState.visitedStages
val executed = stageIsExecuted(node.key, runState)
val selected = node.key == selectedStage
val alpha = if (executed) 1f else 0.3f
val kind = graphNodeKind(node.terminal, node.key, stageTypeByKey[node.key])
val fill = when {
kind == GraphNodeKind.TerminalFailure -> colors.terminalErrorFill
kind == GraphNodeKind.TerminalSuccess -> colors.terminalFill
selected -> colors.nodeFillActive
kind == GraphNodeKind.Ntfy -> colors.nodeNtfyFill
kind == GraphNodeKind.Custom -> colors.nodeCustomFill
else -> colors.nodeFill
}.copy(alpha = alpha)
val border = when (kind) {
GraphNodeKind.TerminalFailure -> colors.error
GraphNodeKind.TerminalSuccess -> colors.success
else -> if (selected) colors.nodeBorderActive else colors.nodeBorder
}.copy(alpha = alpha)
val textColor = if (!node.terminal && selected) colors.nodeTextActive else colors.nodeText
val topLeft = Offset(node.x, node.y)
drawRoundRect(
color = fill,
topLeft = topLeft,
size = Size(node.width, node.height),
cornerRadius = androidx.compose.ui.geometry.CornerRadius(14f, 14f),
)
drawRoundRect(
color = border,
topLeft = topLeft,
size = Size(node.width, node.height),
cornerRadius = androidx.compose.ui.geometry.CornerRadius(14f, 14f),
style = Stroke(width = if (selected && !node.terminal) 4f else 2f),
)
val corner = androidx.compose.ui.geometry.CornerRadius(14f, 14f)
val borderWidth = if (selected && !node.terminal) 4f else 2f
val textColor: Color
if (executed) {
// Filled with a normal border, exactly as the definition-only view.
val fill = when {
kind == GraphNodeKind.TerminalFailure -> colors.terminalErrorFill
kind == GraphNodeKind.TerminalSuccess -> colors.terminalFill
selected -> colors.nodeFillActive
kind == GraphNodeKind.Ntfy -> colors.nodeNtfyFill
kind == GraphNodeKind.Custom -> colors.nodeCustomFill
else -> colors.nodeFill
}
val border = when (kind) {
GraphNodeKind.TerminalFailure -> colors.error
GraphNodeKind.TerminalSuccess -> colors.success
else -> if (selected) colors.nodeBorderActive else colors.nodeBorder
}
drawRoundRect(
color = fill,
topLeft = topLeft,
size = Size(node.width, node.height),
cornerRadius = corner,
)
drawRoundRect(
color = border,
topLeft = topLeft,
size = Size(node.width, node.height),
cornerRadius = corner,
style = Stroke(width = borderWidth),
)
textColor = if (!node.terminal && selected) colors.nodeTextActive else colors.nodeText
} else {
// Hollow and outlined: no fill (the surface shows through), a dashed
// muted border and a muted but legible label, so "not executed" is a
// shape and border-style difference rather than a faded node. A
// selected not-executed node keeps the stronger border colour.
val border = if (selected && !node.terminal) colors.nodeBorderActive else colors.labelText
drawRoundRect(
color = border,
topLeft = topLeft,
size = Size(node.width, node.height),
cornerRadius = corner,
style = Stroke(width = borderWidth, pathEffect = INACTIVE_DASH_EFFECT),
)
textColor = colors.labelText
}
val label = "[" + node.label + "]"
val measured = textMeasurer.measure(
label,
TextStyle(fontSize = 13.sp, fontWeight = FontWeight.Medium, color = textColor.copy(alpha = alpha)),
TextStyle(fontSize = 13.sp, fontWeight = FontWeight.Medium, color = textColor),
)
// Pre-measured layout avoids the scope-relative width constraint crash and
// the redundant re-measure of the string overload.
@@ -1148,8 +1247,20 @@ internal fun outcomeColor(outcome: String, colors: GraphColors): Color = when (o
else -> colors.palette[GraphPalette.outcomeColorIndex(outcome)]
}
/** True when [runState] is null (definition view) or recorded this edge's route. */
private fun EdgeLayout.isExecuted(runState: RunGraphState?): Boolean =
/**
* True when no run is selected (the definition-only view) or [key] was visited
* by the selected run. Pure, so graph drawing and the unit tests share one
* definition of "executed".
*/
internal fun stageIsExecuted(key: String, runState: RunGraphState?): Boolean =
runState == null || key in runState.visitedStages
/**
* True when no run is selected (the definition-only view) or the selected run
* recorded [edge]'s route. Pure, so graph drawing and the unit tests share one
* definition of "executed".
*/
internal fun edgeIsExecuted(edge: GraphEdge, runState: RunGraphState?): Boolean =
runState == null || GraphRoute(edge.source, edge.outcome, edge.target) in runState.takenRoutes
/** A screen-reader description so meaning is never conveyed by colour alone. */
@@ -1157,7 +1268,7 @@ private fun graphDescription(graph: Graph, runState: RunGraphState?): String {
if (graph.stages.isEmpty()) return "Empty workflow graph"
val builder = StringBuilder("Workflow graph with ${graph.stages.size} stages. ")
for (stage in graph.stages) {
val executed = runState == null || stage.key in runState.visitedStages
val executed = stageIsExecuted(stage.key, runState)
builder.append("Stage ${stage.key}")
if (runState != null) builder.append(if (executed) ", executed" else ", not executed")
builder.append(". ")
@@ -1178,6 +1289,25 @@ private const val MAX_SCALE = 3f
private val FIT_PADDING = 16.dp
private val MIN_VISIBLE = 48.dp
/**
* Line-style tokens shared by the graph and its legend. An executed stroke is
* solid; a not-executed stroke is dashed with [INACTIVE_DASH_INTERVALS] on/off
* pixels, so the two states differ by shape, not by a faded tint.
*/
private const val EDGE_STROKE = 3f
/** The dash pattern (on/off, px) for every not-executed stroke; pure data. */
internal val INACTIVE_DASH_INTERVALS = floatArrayOf(14f, 10f)
/** The dashed effect for not-executed strokes, built once, never per frame. */
private val INACTIVE_DASH_EFFECT: PathEffect by lazy {
PathEffect.dashPathEffect(INACTIVE_DASH_INTERVALS)
}
/** Legend swatch geometry: a compact line, not a graph-layout token. */
private val LEGEND_SWATCH_WIDTH = 22.dp
private val LEGEND_SWATCH_HEIGHT = 10.dp
/**
* Collapsed graph-panel height, shared by both detail screens (one source for
* the former 360.dp / 340.dp literals). ~58% of the window height so the graph
@@ -385,7 +385,7 @@ private fun RunGraphTab(
Text("This server did not include the run graph.", style = text.meta)
} else {
Text(
"Executed stages and routes are highlighted; the rest are dimmed. Pinch to zoom, drag to pan, tap a stage for details.",
"Executed stages and routes are solid; not-executed ones are dashed and outlined. Pinch to zoom, drag to pan, tap a stage for details.",
style = text.meta,
)
WorkflowGraph(
@@ -4,9 +4,13 @@ import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.unit.Density
import net.awan.pipelines.data.protocol.Graph
import net.awan.pipelines.data.protocol.GraphRoute
import net.awan.pipelines.data.protocol.Protocol
import net.awan.pipelines.data.protocol.RunGraphState
import net.awan.pipelines.data.protocol.Stage
import net.awan.pipelines.data.protocol.edges
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
@@ -484,6 +488,60 @@ class WorkflowGraphTest {
}
}
/**
* The executed/inactive predicates are the one definition of the two states:
* the definition-only view (no run) is entirely executed, and under a run
* only the visited stages and recorded routes are.
*/
@Test
fun executedPredicatesSeparateDefinitionViewFromaRun() {
val graph = sampleGraph()
val taken = graph.edges().first { it.source == "planner" && it.outcome == "success" }
val notTaken = graph.edges().first { it.source == "planner" && it.outcome == "error" }
// No run selected: every stage and route is executed (definition view,
// unchanged today).
assertTrue(stageIsExecuted("planner", null))
assertTrue(stageIsExecuted("ui_reviewer", null))
assertTrue(edgeIsExecuted(taken, null))
val runState = RunGraphState(
visitedStages = setOf("planner", "implementer"),
takenRoutes = setOf(GraphRoute("planner", "success", "implementer")),
currentStage = "implementer",
)
assertTrue(stageIsExecuted("planner", runState))
assertTrue(stageIsExecuted("implementer", runState))
assertFalse(stageIsExecuted("reviewer", runState))
assertFalse(stageIsExecuted("#success", runState))
assertTrue(edgeIsExecuted(taken, runState))
assertFalse(edgeIsExecuted(notTaken, runState))
}
/**
* The not-executed dash pattern is a real on/off rhythm, not a faded tint: a
* positive, even-length interval list so a dashed stroke reads differently
* from the solid executed one.
*/
@Test
fun inactiveDashPatternIsARealDashRhythm() {
assertTrue(INACTIVE_DASH_INTERVALS.isNotEmpty())
assertEquals(0, INACTIVE_DASH_INTERVALS.size % 2)
assertTrue(INACTIVE_DASH_INTERVALS.all { it > 0f && it.isFinite() })
}
/**
* The one-line legend names both states, executed first, with exactly one
* entry flagged executed so the two swatches cannot both claim solid.
*/
@Test
fun legendNamesBothStatesWithExecutedFirst() {
assertEquals(listOf("Executed", "Not executed"), GRAPH_LEGEND.map { it.label })
assertTrue(GRAPH_LEGEND.first().executed)
assertFalse(GRAPH_LEGEND.last().executed)
assertEquals(1, GRAPH_LEGEND.count { it.executed })
}
private fun assertNoSharedRuns(layout: GraphLayout) {
val edges = layout.edges
for (i in edges.indices) {