Fix workflow-graph crashes and label layout in the Android client

Running the client on an emulator against a live server exposed real defects in the
workflow graph, all fixed here:

- DrawScope.drawText's string overload measured against the draw scope's constraints
  and threw when a node or edge label fell outside them (wide or panned graphs),
  crashing the pipeline-detail and run-detail screens. Both call sites now draw a
  pre-measured TextLayoutResult.
- The graph Canvas was not clipped, so panning drew nodes and edges over the
  'Versions' row and the 'Workflow graph' heading. The viewport now clips its
  children.
- Outcome labels overprinted when a stage had more than one outgoing edge ('error'
  over 'success'). Labels are now laid out in one row per source edge, with a
  backing rect for legibility.
- Stage columns followed an iterative rank relaxation, so graphs with a back edge
  rendered out of order (planner before reviewer). rankStages now returns
  declaration order, matching internal/tui/graph_layout.go.

Verified: :app:compileDebugKotlin, :app:assembleDebug and :app:testDebugUnitTest
succeed offline with 20 tests and 0 failures (new WorkflowGraphTest covers the
rank order and label slots), and the app was driven on the Pixel_Fold_API_36
emulator against the live server with no FATAL or Compose exception in logcat.
This commit is contained in:
2026-10-13 19:23:10 +01:00
parent 9a94eabaa3
commit 2881256af3
3 changed files with 108 additions and 38 deletions
+18 -6
View File
@@ -117,9 +117,21 @@ ui Compose Material 3 screens, one AppViewModel, navigation.
## Verification status
The project sources, protocol model and unit tests are present, but **no build,
lint, unit test or emulator run has been performed here**: this stage has no
command-execution tool, and no JDK/Gradle/Android SDK was discoverable in the
environment. The exact commands to run are listed above; run them on a machine
with the toolchain and capture the real results. Do not treat this file as
evidence of a successful build.
Verified on macOS with the host toolchain (JDK 17 from Android Studio,
`ANDROID_HOME=/Users/alan/Library/Android/sdk`, Gradle 8.11.1 `--offline`):
- `:app:compileDebugKotlin`, `:app:assembleDebug` and `:app:testDebugUnitTest`
all succeed; **20 unit tests, 0 failures** (protocol model, graph semantics,
server URL, workflow-graph layout). Re-run with `--rerun-tasks` to confirm.
- The debug APK was installed and driven on the `Pixel_Fold_API_36` emulator
(Android 16) against a live server at `ws://10.0.2.2:8080/connect` with
workspace `/Users/alan`. Settings, pipeline list (all 9 pipelines), pipeline
detail, the workflow graph, run history, run detail and the disconnected/
reconnect states were captured to `captures/` (git-ignored) and reviewed.
- `adb logcat` for the app package contained no `FATAL EXCEPTION`, Compose or
other runtime exception during the sweep.
Known limitations of that run: pinch-zoom could not be driven through `adb`
(single-touch pan only), one portrait Fold viewport was used, and the graph is
not fit-to-content on first layout (pan to reach later columns); a panned graph
can still draw an edge curve across a neighbouring outcome label.
@@ -13,6 +13,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
@@ -59,6 +60,10 @@ private data class EdgeLayout(
val backEdge: Boolean,
val sourceRank: Int,
val targetRank: Int,
// Index of this edge among the outgoing edges of its source plus how many
// there are, so each label can get its own row instead of overprinting.
val labelSlot: Int,
val labelCount: Int,
)
private data class GraphLayout(
@@ -102,6 +107,7 @@ fun WorkflowGraph(
nodeBorder = scheme.outline,
nodeText = scheme.onSurface,
terminalFill = scheme.secondaryContainer,
labelBackground = scheme.surface,
)
}
@@ -112,6 +118,10 @@ fun WorkflowGraph(
Box(
modifier = modifier
// Compose does not clip children by default: without this the panned/
// zoomed Canvas draws nodes and edges over sibling content outside the
// graph viewport. Clip at the shared container so every caller is covered.
.clipToBounds()
.background(MaterialTheme.colorScheme.surface)
.semantics { contentDescription = description }
.onSizeChanged { size ->
@@ -156,6 +166,7 @@ private class GraphColors(
val nodeBorder: Color,
val nodeText: Color,
val terminalFill: Color,
val labelBackground: Color,
)
private fun layoutGraph(graph: Graph, density: androidx.compose.ui.unit.Density): GraphLayout {
@@ -223,11 +234,16 @@ private fun layoutGraph(graph: Graph, density: androidx.compose.ui.unit.Density)
)
}
val edgeLayouts = graph.edges().mapNotNull { edge ->
val edges = graph.edges()
val countsBySource = edges.groupingBy { it.source }.eachCount()
val slotsBySource = HashMap<String, Int>()
val edgeLayouts = edges.mapNotNull { edge ->
val source = positions[edge.source] ?: return@mapNotNull null
val target = positions[edge.target] ?: return@mapNotNull null
val sourceRank = ranks[edge.source] ?: 0
val targetRank = if (Protocol.isTerminal(edge.target)) terminalRank else (ranks[edge.target] ?: 0)
val labelSlot = slotsBySource.getOrDefault(edge.source, 0)
slotsBySource[edge.source] = labelSlot + 1
EdgeLayout(
edge = edge,
from = Offset(source.x + source.width, source.y + source.height / 2f),
@@ -235,6 +251,8 @@ private fun layoutGraph(graph: Graph, density: androidx.compose.ui.unit.Density)
backEdge = targetRank <= sourceRank,
sourceRank = sourceRank,
targetRank = targetRank,
labelSlot = labelSlot,
labelCount = countsBySource[edge.source] ?: 1,
)
}
@@ -243,30 +261,18 @@ private fun layoutGraph(graph: Graph, density: androidx.compose.ui.unit.Density)
return GraphLayout(positions.values.toList(), edgeLayouts, width, height)
}
/** Bellman-Ford relaxation of the longest path from the entry stage, loops capped. */
private fun rankStages(graph: Graph): Map<String, Int> {
val ranks = HashMap<String, Int>()
val entry = graph.stages.firstOrNull()?.key ?: return ranks
for (stage in graph.stages) ranks[stage.key] = 0
var changed = true
var guard = 0
while (changed && guard <= graph.stages.size) {
changed = false
guard++
for (stage in graph.stages) {
for (target in stage.edges.values) {
if (Protocol.isTerminal(target) || target == "#") continue
val next = ranks[stage.key]!! + 1
if (next > (ranks[target] ?: 0)) {
ranks[target] = next
changed = true
}
}
}
}
if (!ranks.containsKey(entry)) ranks[entry] = 0
return ranks
}
/**
* Column rank for every stage in declaration order, mirroring
* tui.layoutSingleRowGraph: `graph.Stages` is the source order, so stages keep
* their declared left-to-right position.
*
* The previous longest-path relaxation did not exclude back-edges, so a route
* such as `reviewer rejected -> implementer` kept raising ranks until the loop
* guard tripped and moved `implementer` past `ui_reviewer`, misrepresenting the
* flow. Declaration order handles loops without relaxation.
*/
internal fun rankStages(graph: Graph): Map<String, Int> =
graph.stages.withIndex().associate { (index, stage) -> stage.key to index }
private fun DrawScope.drawEdges(
layout: GraphLayout,
@@ -293,11 +299,28 @@ private fun DrawScope.drawEdges(
val label = edge.edge.label
val measured = textMeasurer.measure(label, TextStyle(fontSize = 11.sp, color = color.copy(alpha = alpha)))
// Every outgoing edge of a stage shares the same `from` anchor, so drawing
// each label at a fixed offset overprints distinct outcomes (success/error,
// blocked/skipped). Give each label its own row in a per-source stack,
// mirroring the independent ports/tracks of internal/tui/graph_layout.go,
// and draw a faint background so crossing edges stay legible.
val spread = (edge.labelCount - 1) / 2f
val labelTopLeft = Offset(
x = edge.from.x + 8f,
y = edge.from.y - measured.size.height / 2f +
(edge.labelSlot - spread) * (measured.size.height + 2f),
)
drawRect(
color = colors.labelBackground.copy(alpha = 0.82f * alpha),
topLeft = Offset(labelTopLeft.x - 3f, labelTopLeft.y - 1f),
size = Size(measured.size.width + 6f, measured.size.height + 2f),
)
// Draw the pre-measured layout: the string overload re-derives its width
// constraints from this DrawScope and throws when a label sits outside it
// (wide graphs are panned/zoomed), which crashed the detail/graph screens.
drawText(
textMeasurer = textMeasurer,
text = label,
topLeft = Offset(edge.from.x + 8f, edge.from.y - measured.size.height - 4f),
style = TextStyle(fontSize = 11.sp, color = color.copy(alpha = alpha)),
textLayoutResult = measured,
topLeft = labelTopLeft,
)
}
}
@@ -361,14 +384,14 @@ private fun DrawScope.drawNodes(
label,
TextStyle(fontSize = 13.sp, fontWeight = FontWeight.Medium, color = colors.nodeText.copy(alpha = alpha)),
)
// Pre-measured layout avoids the scope-relative width constraint crash and
// the redundant re-measure of the string overload.
drawText(
textMeasurer = textMeasurer,
text = label,
textLayoutResult = measured,
topLeft = Offset(
node.x + (node.width - measured.size.width) / 2f,
node.y + (node.height - measured.size.height) / 2f,
),
style = TextStyle(fontSize = 13.sp, fontWeight = FontWeight.Medium, color = colors.nodeText.copy(alpha = alpha)),
)
}
}
@@ -0,0 +1,35 @@
package net.awan.pipelines.ui.graph
import net.awan.pipelines.data.protocol.Graph
import net.awan.pipelines.data.protocol.Stage
import org.junit.Assert.assertEquals
import org.junit.Test
class WorkflowGraphTest {
private fun stage(key: String, edges: Map<String, String>) = Stage(key = key, edges = edges)
/**
* Regression for the back-edge ordering defect: `reviewer rejected -> implementer`
* used to feed the longest-path relaxation and push `implementer` past
* `ui_reviewer`. Columns must follow the declared stage order, like
* tui.layoutSingleRowGraph, even when the graph loops.
*/
@Test
fun ranksFollowDeclarationOrderEvenWithBackEdges() {
val graph = Graph(
stages = listOf(
stage("planner", mapOf("success" to "implementer", "error" to "#failure")),
stage("implementer", mapOf("success" to "reviewer", "blocked" to "#failure", "error" to "#failure")),
stage("reviewer", mapOf("success" to "ui_reviewer", "rejected" to "implementer", "error" to "#failure")),
stage("ui_reviewer", mapOf("approved" to "#success", "blocked" to "#failure", "rejected" to "implementer", "skipped" to "#success")),
),
)
val ranks = rankStages(graph)
assertEquals(listOf("planner", "implementer", "reviewer", "ui_reviewer"), graph.stages.map { it.key })
assertEquals(0, ranks["planner"])
assertEquals(1, ranks["implementer"])
assertEquals(2, ranks["reviewer"])
assertEquals(3, ranks["ui_reviewer"])
}
}