diff --git a/android/README.md b/android/README.md index f4b61055..0119fda0 100644 --- a/android/README.md +++ b/android/README.md @@ -3717,3 +3717,62 @@ visual review owns the device); no server that actually sends `title` was available, so the titled layout is exercised by the unit tests and the fallback path only. +## Stage verdict atop the stage pane, with clickable artifacts (16 Oct 2026) + +A completed run visit now leads its stage pane with the server's derived verdict: +the chosen option as a badge, the concise reason in ordinary body text, and the +recorded artifacts the reason names as inline, tappable references plus the same +references as compact chips. `android/` only; nothing in the Pipelines server, its +Go source, the installer or the Docker files was touched, no run was started from +the app, and no device command was run (build + unit tests only). + +- **Protocol.** `data/protocol/Message.kt` gains `StageVerdict` and an optional + `verdict` on `RunStep`, mirroring `internal/protocol/verdict.go` + (`{option, reason, reason_source, artifacts}` under the `verdict` wire name). It + is nullable and defaults to absent, so a running step or a pre-verdict server + parses cleanly to null. +- **One block.** New `ui/graph/StageVerdict.kt` holds the pure helpers and the + composable. `StageVerdictBlock` is rendered at the top of the run pane, below the + visit tabs and above the facts, only when the server reported a verdict; a null + verdict (or one with neither option nor reason) draws nothing — never an empty + heading or placeholder. +- **Badge.** The option is a `ColoredChip` in the *stage outcome's* existing colour + (`outcomeColor(step.outcome)`, the graph's palette the Timeline chips already + use), so the badge agrees with the graph rather than a new colour vocabulary. +- **Reason.** Rendered with `pipelinesText().body`, so it is readable plain + language, never a monospace JSON dump, and it wraps with no `maxLines` clip. +- **References.** `referencedVerdictArtifacts` resolves the verdict's referenced + names to the visit's recorded artifacts (recorded order, deduplicated, an + unrecorded/invented name dropped). `verdictReasonSegments` splits the reason + around those names — by full recorded name or basename, longest match first — and + the segments become inline `LinkAnnotation.Clickable` references coloured + `primary` with an underline. An image/video reference opens the shared + `ArtifactViewer`; anything else goes through the existing `openArtifact` action. + The same references are `FlowRow` chips (`VerdictArtifactChip`) with their type + glyph, so they can be tapped without hunting in the sentence. +- **Geometry.** Only existing tokens (`SPACING_M`/`SPACING_S`/`SPACING_XS`, + `ICON_STATUS_DP`); no new `dp`/`sp` literal and no new dependency. + +Evidence (macOS, JDK 17 from Homebrew `openjdk@17`, Gradle 8.11.1 from the wrapper +cache, `ANDROID_HOME=/Users/alan/Library/Android/sdk`, `--no-daemon`): + +- `:app:testDebugUnitTest` → `BUILD SUCCESSFUL`; the result XMLs aggregate to + **282 tests, 0 failures, 0 errors, 0 skipped**. New `StageVerdictTest` (5 cases: + split around every referenced name, basename for a slashed path, longest match + wins, a reason with no references, and name→recorded-artifact resolution) and + `ProtocolModelTest#runStepVerdictParsesWithItsReasonSourceAndReferencedArtifacts`. +- `:app:assembleDebug` → `app-debug.apk` **18,270,824 bytes**, sha256 + `85847c6fe32498f38464741ca1b8f592dbbd7a9e1f34dd649121e723836b0903`. +- **APK publish.** `app-debug.apk` copied to + `~/.local/share/pipelines/data/artifacts/167/1/pipelines-debug-wip-8b0d7b1.apk` + (`8b0d7b1` = short `HEAD`); `shasum -a 256` matched. The LAN URL + `http://10.0.0.249:8080/artifacts/167/1/pipelines-debug-wip-8b0d7b1.apk` returned + `HTTP/1.1 200 OK`, `Content-Type: application/vnd.android.package-archive`, + `Content-Length: 18270824`. + +Not verified in this pass: device captures of the stage pane at a narrow and a wide +window (the visual review owns the device, per the brief's device policy), so the +verdict layout is exercised by the unit tests and the compile only. No server that +actually sends `verdict` was available to the implementer, so the block's live +rendering against real data is deferred to visual review. + diff --git a/android/app/src/main/java/net/awan/pipelines/data/protocol/Message.kt b/android/app/src/main/java/net/awan/pipelines/data/protocol/Message.kt index 53a9f24e..8d03f6a8 100644 --- a/android/app/src/main/java/net/awan/pipelines/data/protocol/Message.kt +++ b/android/app/src/main/java/net/awan/pipelines/data/protocol/Message.kt @@ -213,6 +213,22 @@ data class StageArtifact( val url: String = "", ) +/** + * The plain-language outcome of one completed stage visit, derived server-side + * and mirrored from `internal/protocol/verdict.go` StageVerdict: the chosen + * option (the outcome and the target it routed to), a bounded one-line reason + * with its provenance ([reasonSource]), and the names of the recorded artifacts + * the reason refers to. A null on a step means the server reported no verdict + * (for example a running visit), so the pane shows no block at all. + */ +@Serializable +data class StageVerdict( + val option: String = "", + val reason: String = "", + @SerialName("reason_source") val reasonSource: String = "", + val artifacts: List = emptyList(), +) + @Serializable data class NotificationReceipt( val destination: String = "", @@ -265,6 +281,9 @@ data class RunStep( val artifacts: List? = null, @SerialName("artifacts_skipped") val artifactsSkipped: Int = 0, @SerialName("artifacts_truncated") val artifactsTruncated: Int = 0, + // The server's derived plain-language verdict; absent (null) on a running step + // or a pre-verdict server, so the pane shows no block. + val verdict: StageVerdict? = null, val sequence: Int = 0, val stage: String = "", val status: String = "", diff --git a/android/app/src/main/java/net/awan/pipelines/ui/graph/StageDetailPane.kt b/android/app/src/main/java/net/awan/pipelines/ui/graph/StageDetailPane.kt index 5c37aa28..f348510c 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/graph/StageDetailPane.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/graph/StageDetailPane.kt @@ -491,6 +491,20 @@ private fun StageDetailBody( } } + // The verdict leads the run pane, above the facts: the chosen option, + // the concise reason and the artifacts it names. Absent when the server + // reported none, so no empty heading is ever drawn. + if (runContext && selectedStep != null) { + StageVerdictBlock( + verdict = selectedStep.verdict, + outcome = selectedStep.outcome, + status = selectedStep.status, + artifacts = selectedStep.artifacts.orEmpty(), + configuredServerUrl = configuredServerUrl, + fetchArtifactBytes = fetchArtifactBytes, + ) + } + // Identity and result: always visible, never behind a collapse, so a // freshly opened pane never reads as empty. headline.forEach { field -> StageFieldRow(field) } diff --git a/android/app/src/main/java/net/awan/pipelines/ui/graph/StageVerdict.kt b/android/app/src/main/java/net/awan/pipelines/ui/graph/StageVerdict.kt new file mode 100644 index 00000000..c896d04f --- /dev/null +++ b/android/app/src/main/java/net/awan/pipelines/ui/graph/StageVerdict.kt @@ -0,0 +1,279 @@ +package net.awan.pipelines.ui.graph + +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.ExperimentalLayoutApi +import androidx.compose.foundation.layout.FlowRow +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.text.LinkAnnotation +import androidx.compose.ui.text.SpanStyle +import androidx.compose.ui.text.TextLinkStyles +import androidx.compose.ui.text.buildAnnotatedString +import androidx.compose.ui.text.style.TextDecoration +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.text.withLink +import net.awan.pipelines.data.protocol.StageArtifact +import net.awan.pipelines.data.protocol.StageVerdict +import net.awan.pipelines.ui.components.ColoredChip +import net.awan.pipelines.ui.components.runStateIcon +import net.awan.pipelines.ui.components.runStateOf +import net.awan.pipelines.ui.theme.ICON_STATUS_DP +import net.awan.pipelines.ui.theme.SPACING_M +import net.awan.pipelines.ui.theme.SPACING_S +import net.awan.pipelines.ui.theme.SPACING_XS +import net.awan.pipelines.ui.theme.pipelinesText + +/** + * One piece of a verdict's reason: literal [text], or a reference to a recorded + * [artifact] the reason names. A null [artifact] is plain text. + */ +internal data class VerdictReasonSegment(val text: String, val artifact: StageArtifact? = null) + +/** + * The recorded artifacts a [verdict] refers to: its own referenced names resolved + * to the visit's recorded [artifacts], in recorded order and deduplicated. A name + * the server never recorded is dropped, so only real files are ever offered, and + * no verdict or no referenced names yields nothing. + */ +internal fun referencedVerdictArtifacts( + verdict: StageVerdict?, + artifacts: List, +): List { + val names = verdict?.artifacts.orEmpty() + if (names.isEmpty()) return emptyList() + val wanted = names.toHashSet() + val seen = HashSet() + val referenced = ArrayList() + for (artifact in artifacts) { + if (artifact.name.isEmpty() || !seen.add(artifact.name)) continue + if (artifact.name in wanted) referenced += artifact + } + return referenced +} + +/** + * Splits [reason] into literal runs and references to the recorded [references] + * it names, by an artifact's full recorded name or its basename (mirroring the + * server's own reference matching). At the same index a longer token wins over a + * shorter one, so a full slashed path is never split inside a longer match. With + * nothing to reference the whole reason is one literal segment; an empty reason + * yields no segments. + */ +internal fun verdictReasonSegments( + reason: String, + references: List, +): List { + if (reason.isEmpty()) return emptyList() + if (references.isEmpty()) return listOf(VerdictReasonSegment(reason)) + val tokens = ArrayList>() + references.forEach { artifact -> + tokens += artifact.name to artifact + val base = artifactBaseName(artifact.name) + if (base.isNotEmpty() && base != artifact.name) tokens += base to artifact + } + // Longest token first (stable), so a full name wins over its basename at the + // same index; a bare basename still resolves to the artifact named exactly so. + tokens.sortByDescending { it.first.length } + val segments = ArrayList() + val literal = StringBuilder() + var index = 0 + while (index < reason.length) { + val match = tokens.firstOrNull { reason.startsWith(it.first, index) } + if (match == null) { + literal.append(reason[index]) + index++ + } else { + if (literal.isNotEmpty()) { + segments += VerdictReasonSegment(literal.toString()) + literal.clear() + } + segments += VerdictReasonSegment(match.first, match.second) + index += match.first.length + } + } + if (literal.isNotEmpty()) segments += VerdictReasonSegment(literal.toString()) + return segments +} + +/** The last path segment of a recorded artifact name (`shots/x.png` -> `x.png`). */ +private fun artifactBaseName(name: String): String = + name.substringAfterLast('/').substringAfterLast('\\') + +/** + * The stage pane's Verdict block: the completed visit's chosen option as a badge + * in the outcome's existing colour, the concise reason as ordinary body text (never + * a monospace JSON dump, and it wraps rather than clipping), and the recorded + * artifacts the reason names as inline, tappable links plus the same references as + * compact chips. An image or video reference opens the shared full-screen viewer; + * anything else goes through the existing external-open action, so the block reuses + * the gallery's behaviour instead of re-deriving it. + * + * It renders nothing at all when the server reported no verdict ([verdict] null) or + * the verdict carries neither an option nor a reason, so there is never an empty + * heading or placeholder. + */ +@OptIn(ExperimentalLayoutApi::class) +@Composable +internal fun StageVerdictBlock( + verdict: StageVerdict?, + outcome: String, + status: String, + artifacts: List, + configuredServerUrl: String, + fetchArtifactBytes: suspend (String) -> ByteArray?, + modifier: Modifier = Modifier, +) { + if (verdict == null) return + val option = verdict.option + val reason = verdict.reason + if (option.isBlank() && reason.isBlank()) return + + val text = pipelinesText() + val context = LocalContext.current + val scheme = MaterialTheme.colorScheme + val linkColor = scheme.primary + val colors = remember(scheme) { graphColors(scheme) } + val tint = outcomeColor(outcome.ifBlank { status }, colors) + val references = remember(verdict, artifacts) { referencedVerdictArtifacts(verdict, artifacts) } + val segments = remember(reason, references) { verdictReasonSegments(reason, references) } + var viewerArtifact by remember { mutableStateOf(null) } + + // The one tap behaviour the inline links and the chips share: media opens the + // viewer, everything else the existing external-open action. + val open: (StageArtifact) -> Unit = { artifact -> + val resolved = resolvedArtifactUrl(artifact, configuredServerUrl) + val media = artifactKind(artifact.contentType, artifact.name).let { + it == ArtifactKind.Image || it == ArtifactKind.Video + } + if (resolved != null && media) viewerArtifact = artifact else openArtifact(context, resolved) + } + + Surface( + modifier = modifier.fillMaxWidth(), + color = MaterialTheme.colorScheme.surfaceContainerHigh, + shape = MaterialTheme.shapes.medium, + ) { + Column( + modifier = Modifier.padding(SPACING_M), + verticalArrangement = Arrangement.spacedBy(SPACING_S), + ) { + FlowRow( + horizontalArrangement = Arrangement.spacedBy(SPACING_S), + verticalArrangement = Arrangement.spacedBy(SPACING_XS), + ) { + Text("Verdict", style = text.sectionHeader) + if (option.isNotBlank()) { + ColoredChip( + label = option, + color = tint, + icon = runStateIcon(runStateOf(outcome.ifBlank { status })), + ) + } + } + if (reason.isNotBlank()) { + // Rebuilt only when the segments, link colour or configured server + // change, so the tap targets never capture a stale server. + val annotated = remember(segments, linkColor, configuredServerUrl) { + buildAnnotatedString { + segments.forEach { segment -> + val artifact = segment.artifact + if (artifact == null) { + append(segment.text) + } else { + withLink( + LinkAnnotation.Clickable( + tag = "artifact:${artifact.url}", + styles = TextLinkStyles( + style = SpanStyle( + color = linkColor, + textDecoration = TextDecoration.Underline, + ), + ), + linkInteractionListener = { open(artifact) }, + ), + ) { append(segment.text) } + } + } + } + } + Text( + text = annotated, + style = text.body, + modifier = Modifier.fillMaxWidth(), + ) + } + if (references.isNotEmpty()) { + FlowRow( + horizontalArrangement = Arrangement.spacedBy(SPACING_S), + verticalArrangement = Arrangement.spacedBy(SPACING_XS), + ) { + references.forEach { artifact -> + VerdictArtifactChip(artifact = artifact, onClick = { open(artifact) }) + } + } + } + } + } + + val viewer = viewerArtifact + val viewerUrl = viewer?.let { resolvedArtifactUrl(it, configuredServerUrl) } + if (viewer != null && viewerUrl != null) { + ArtifactViewer( + url = viewerUrl, + name = viewer.name, + fetchBytes = fetchArtifactBytes, + onDismiss = { viewerArtifact = null }, + video = artifactKind(viewer.contentType, viewer.name) == ArtifactKind.Video, + ) + } +} + +/** A compact, tappable chip naming one referenced artifact, with its type glyph. */ +@Composable +private fun VerdictArtifactChip(artifact: StageArtifact, onClick: () -> Unit) { + val text = pipelinesText() + val kind = remember(artifact.contentType, artifact.name) { + artifactKind(artifact.contentType, artifact.name) + } + Surface( + shape = RoundedCornerShape(50), + color = MaterialTheme.colorScheme.surfaceContainerHighest, + modifier = Modifier.clickable(onClick = onClick), + ) { + Row( + modifier = Modifier.padding(horizontal = SPACING_S, vertical = SPACING_XS), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(SPACING_XS), + ) { + Icon( + imageVector = artifactIcon(kind), + contentDescription = null, + tint = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.size(ICON_STATUS_DP), + ) + Text( + text = artifact.name, + style = text.label, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) + } + } +} diff --git a/android/app/src/test/java/net/awan/pipelines/data/protocol/ProtocolModelTest.kt b/android/app/src/test/java/net/awan/pipelines/data/protocol/ProtocolModelTest.kt index cce06fd4..a0a6ea8e 100644 --- a/android/app/src/test/java/net/awan/pipelines/data/protocol/ProtocolModelTest.kt +++ b/android/app/src/test/java/net/awan/pipelines/data/protocol/ProtocolModelTest.kt @@ -137,6 +137,48 @@ class ProtocolModelTest { assertEquals(0, without.artifactsTruncated) } + @Test + fun runStepVerdictParsesWithItsReasonSourceAndReferencedArtifacts() { + val json = """ + { + "type": "run", + "run_detail": { + "id": 167, + "steps": [ + { + "sequence": 1, + "stage": "ui_reviewer", + "status": "succeeded", + "outcome": "approved", + "target": "#success", + "verdict": { + "option": "approved → success", + "reason": "Layout matches; see review.png", + "reason_source": "model", + "artifacts": ["review.png"] + } + }, + {"sequence": 2, "stage": "implementer", "status": "running", "outcome": "", "target": ""} + ] + } + } + """.trimIndent() + val run = PipelinesJson.decodeFromString(Message.serializer(), json).runDetail!! + val verdict = run.steps.first().verdict!! + assertEquals("approved → success", verdict.option) + assertEquals("Layout matches; see review.png", verdict.reason) + assertEquals("model", verdict.reasonSource) + assertEquals(listOf("review.png"), verdict.artifacts) + // A running step with no outcome/target carries no verdict, so the pane + // shows no block; a verdict with no artifacts defaults to an empty list. + assertNull(run.steps[1].verdict) + val noArtifacts = PipelinesJson.decodeFromString( + Message.serializer(), + """{"type":"run","run_detail":{"id":1,"steps":[{"sequence":1,"stage":"a","verdict":{"option":"done → success"}}]}}""", + ).runDetail!!.steps.single().verdict!! + assertEquals(emptyList(), noArtifacts.artifacts) + } + @Test fun pipelineDetailPopulatesInheritedAndWireNameFields() { val json = """ diff --git a/android/app/src/test/java/net/awan/pipelines/ui/graph/StageVerdictTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/graph/StageVerdictTest.kt new file mode 100644 index 00000000..479b42db --- /dev/null +++ b/android/app/src/test/java/net/awan/pipelines/ui/graph/StageVerdictTest.kt @@ -0,0 +1,99 @@ +package net.awan.pipelines.ui.graph + +import net.awan.pipelines.data.protocol.StageArtifact +import net.awan.pipelines.data.protocol.StageVerdict +import org.junit.Assert.assertEquals +import org.junit.Test + +class StageVerdictTest { + + private fun artifact(name: String, contentType: String = "image/png") = StageArtifact( + name = name, + sizeBytes = 1, + contentType = contentType, + url = "/artifacts/167/1/$name", + ) + + @Test + fun segmentsSplitTheReasonAroundEveryReferencedName() { + val png = artifact("review.png") + val txt = artifact("report.txt", contentType = "text/plain") + assertEquals( + listOf( + VerdictReasonSegment("Approved: "), + VerdictReasonSegment("review.png", png), + VerdictReasonSegment(" shows the fix; "), + VerdictReasonSegment("report.txt", txt), + VerdictReasonSegment(" lists the checks."), + ), + verdictReasonSegments( + "Approved: review.png shows the fix; report.txt lists the checks.", + listOf(png, txt), + ), + ) + } + + @Test + fun segmentsMatchABasenameForASlashedPath() { + val nested = artifact("shots/wide.png") + assertEquals( + listOf( + VerdictReasonSegment("See "), + VerdictReasonSegment("wide.png", nested), + VerdictReasonSegment(" for the wide layout."), + ), + verdictReasonSegments("See wide.png for the wide layout.", listOf(nested)), + ) + } + + @Test + fun aLongerNameWinsOverItsBasenameAtTheSameIndex() { + val bare = artifact("wide.png") + val nested = artifact("shots/wide.png") + // The full slashed path matches whole, not just its basename, so the + // reference resolves to the artifact actually named. + assertEquals( + listOf( + VerdictReasonSegment("shots/wide.png", nested), + VerdictReasonSegment(" is the shot"), + ), + verdictReasonSegments("shots/wide.png is the shot", listOf(bare, nested)), + ) + // A bare basename still resolves to the artifact named exactly so. + assertEquals( + listOf( + VerdictReasonSegment("wide.png", bare), + VerdictReasonSegment(" was captured"), + ), + verdictReasonSegments("wide.png was captured", listOf(bare, nested)), + ) + } + + @Test + fun aReasonWithoutReferencesIsOneLiteralSegment() { + assertEquals( + listOf(VerdictReasonSegment("nothing referenced here")), + verdictReasonSegments("nothing referenced here", listOf(artifact("x.png"))), + ) + assertEquals( + listOf(VerdictReasonSegment("no recorded artifact is named")), + verdictReasonSegments("no recorded artifact is named", emptyList()), + ) + assertEquals(emptyList(), verdictReasonSegments("", listOf(artifact("x.png")))) + } + + @Test + fun referencedArtifactsResolveNamesToRecordedFilesInRecordedOrder() { + val a = artifact("a.png") + val b = artifact("b.png", contentType = "text/plain") + val verdict = StageVerdict( + option = "approved → success", + reason = "b.png then a.png", + artifacts = listOf("b.png", "a.png", "invented.png"), + ) + // Recorded order, the invented name dropped, only real recorded files. + assertEquals(listOf(a, b), referencedVerdictArtifacts(verdict, listOf(a, b))) + assertEquals(emptyList(), referencedVerdictArtifacts(null, listOf(a))) + assertEquals(emptyList(), referencedVerdictArtifacts(StageVerdict(), listOf(a))) + } +}