Open stage sections as their own pane instead of expanding inline

Expanding a stage section rendered its rows stacked at the top of the section: CollapsibleSectionBody passed the section's several field composables straight into AnimatedVisibility, which measures a single child, so all rows were placed at the section origin and only the last (Memory) was visible. Fix the shared body by laying its content out in one full-width Column, and make every stage section open as its own pane rather than growing the stage screen inline.

- PaneState: add the Section pane level (MAX_PANE_DEPTH = 3), a saved sectionKey primitive, and the matching backStep/popTo/encode/decode handling so one Back or peek pops one level and the open section survives rotation and process death.

- StageDetailPane: the stage screen now keeps a compact collapsed list of sections (Artifacts, Output, Input, This run, Stage definition); tapping one opens it as a full-window screen on a narrow window or a panel to the right of the stage pane on a wide one, with its own title, scroll and top spacing. A shared stageSectionSpecs derives the sections for both the stage screen and the section pane, and the artifacts section keeps the shared ArtifactSelectionScaffold.

- PipelinesApp: wire the Section pane into the stack, its peeks and Back. Section content (Definition, Input, Output, Artifacts) and semantics are unchanged; sections start collapsed and the operator's choice persists.

- Tests: cover the section pane level, backStep, popTo and the shared stageSectionSpecs. Build and forced unit tests pass (262 tests, 0 failures); lint shows only the pre-existing themes.xml NewApi errors.

Android only (7 files); no dependency added, no server/Go/installer/Docker change, no run started from the app. APK published to the run artifacts directory and documented in android/README.md.
This commit is contained in:
2026-10-16 17:12:13 +01:00
parent c700b28e6a
commit 12976ba372
7 changed files with 700 additions and 231 deletions
@@ -4,26 +4,26 @@ import androidx.compose.runtime.saveable.Saver
/**
* Which pane of the sliding stack is showing: the list (level 0), the selected
* item's detail (level 1) and, when a stage/node is open, the stage pane
* (level 2). On a wide window the panes stack left to right and share a fixed
* peek budget; on a narrow window only the topmost pane is visible and it fills
* the window.
* item's detail (level 1), the stage/node pane (level 2) and, when a stage
* section is opened, that section's pane (level 3). On a wide window the panes
* stack left to right and share a fixed peek budget; on a narrow window only the
* topmost pane is visible and it fills the window.
*/
enum class MasterPane { List, Detail, Stage }
enum class MasterPane { List, Detail, Stage, Section }
/**
* The declared bound on the stack: list (0), detail (1), stage (2). The stack can
* never grow deeper than this; selecting a different stage or list item replaces
* the rightmost pane instead of pushing a new level.
* The declared bound on the stack: list (0), detail (1), stage (2), section (3).
* The stack can never grow deeper than this; selecting a different stage, list
* item or section replaces the rightmost pane instead of pushing a new level.
*/
const val MAX_PANE_DEPTH = 2
const val MAX_PANE_DEPTH = 3
/**
* The saved pane-stack selection: which pane is rightmost, which item (0 = none),
* its pipeline/run version, which detail section (page) is showing, and which
* stage/visit the stage pane is on. Every field is a primitive, so the state
* round-trips through a Bundle cheaply and survives rotation, configuration
* change and process death.
* its pipeline/run version, which detail section (page) is showing, which
* stage/visit the stage pane is on and, when a stage section is open, its key.
* Every field is a primitive, so the state round-trips through a Bundle cheaply
* and survives rotation, configuration change and process death.
*
* Geometry (peek width, insets, measured pane widths) is deliberately *not* part
* of this model; it is derived from layout so nothing is hardcoded here.
@@ -35,13 +35,15 @@ data class PaneSelection(
val sectionIndex: Int = 0,
val stageKey: String? = null,
val stageVisit: Int = 0,
val sectionKey: String? = null,
) {
/** How many panes are stacked: 0 list, 1 detail, 2 stage. */
/** How many panes are stacked: 0 list, 1 detail, 2 stage, 3 section. */
val depth: Int
get() = when (pane) {
MasterPane.List -> 0
MasterPane.Detail -> 1
MasterPane.Stage -> 2
MasterPane.Section -> 3
}
}
@@ -55,29 +57,39 @@ sealed interface BackResult {
}
/**
* Exactly one pane level of Back. Inside the stage pane it steps back to the
* detail; inside the detail it steps back to the list; at the list it leaves the
* app. Sections are navigated only by their own tab row/pager, never by Back, so
* the peek tap and the Back button/gesture always pop the same single level.
* Exactly one pane level of Back. Inside a stage section it steps back to the
* stage pane; inside the stage pane it steps back to the detail; inside the
* detail it steps back to the list; at the list it leaves the app. The peek tap
* and the Back button/gesture therefore always pop the same single level.
*/
fun backStep(selection: PaneSelection): BackResult = when (selection.pane) {
MasterPane.List -> BackResult.Exit
MasterPane.Detail -> BackResult.Show(PaneSelection(pane = MasterPane.List))
MasterPane.Stage -> BackResult.Show(
selection.copy(pane = MasterPane.Detail, stageKey = null, stageVisit = 0),
selection.copy(pane = MasterPane.Detail, stageKey = null, stageVisit = 0, sectionKey = null),
)
MasterPane.Section -> BackResult.Show(
selection.copy(pane = MasterPane.Stage, sectionKey = null),
)
}
/**
* The selection after tapping the peek of the pane at [level]: every pane to its
* right is closed. Level 0 (the list) shows a bare list; level 1 shows the same
* item's detail with no stage; a [level] at or beyond the current depth is a
* no-op (there is nothing to its right). Peeks exist only on a wide window;
* narrow windows use Back/gesture for the same one-level step.
* item's detail with no stage; level 2 shows the same item's stage with no
* section; a [level] at or beyond the current depth is a no-op (there is nothing
* to its right). Peeks exist only on a wide window; narrow windows use
* Back/gesture for the same one-level step.
*/
fun popTo(selection: PaneSelection, level: Int): PaneSelection = when {
level <= 0 -> PaneSelection()
level == 1 -> selection.copy(pane = MasterPane.Detail, stageKey = null, stageVisit = 0)
level == 1 -> selection.copy(
pane = MasterPane.Detail,
stageKey = null,
stageVisit = 0,
sectionKey = null,
)
level == 2 -> selection.copy(pane = MasterPane.Stage, sectionKey = null)
else -> selection
}
@@ -92,6 +104,7 @@ internal fun encodePaneSelection(selection: PaneSelection): List<Any> = listOf(
selection.sectionIndex,
selection.stageKey ?: "",
selection.stageVisit,
selection.sectionKey ?: "",
)
/** Restores a [PaneSelection] encoded by [encodePaneSelection]. */
@@ -102,6 +115,7 @@ internal fun decodePaneSelection(encoded: List<Any>): PaneSelection = PaneSelect
sectionIndex = encoded[3] as Int,
stageKey = (encoded[4] as String).takeIf { it.isNotEmpty() },
stageVisit = encoded[5] as Int,
sectionKey = (encoded[6] as String).takeIf { it.isNotEmpty() },
)
/**
@@ -220,7 +220,13 @@ private fun PipelineDestination(vm: AppViewModel, wide: Boolean, onOpenRun: (Lon
depth = depth,
wide = wide,
onPopTo = { level -> selection = popTo(selection, level) },
backContentDescription = { level -> if (level == 0) "Back to pipelines" else "Back to pipeline" },
backContentDescription = { level ->
when (level) {
0 -> "Back to pipelines"
1 -> "Back to pipeline"
else -> "Back to stage"
}
},
pane = { level, covered, paneModifier ->
when (level) {
0 -> PipelineListScreen(
@@ -263,6 +269,33 @@ private fun PipelineDestination(vm: AppViewModel, wide: Boolean, onOpenRun: (Lon
pipelineId = itemId,
version = version,
stageKey = key,
openSection = null,
onOpenSection = { sectionKey ->
selection = selection.copy(
pane = MasterPane.Section,
sectionKey = sectionKey,
)
},
onBack = onBack,
wide = wide,
modifier = paneModifier,
covered = covered,
)
}
3 -> rememberPaneContent(
selection.sectionKey?.let {
Triple(selection.itemId, it, selection.stageKey)
},
)?.let { (itemId, sectionKey, key) ->
// A closing section pane keeps the identity that opened it, so
// it renders its content until it is fully off-screen.
PipelineStagePane(
vm = vm,
pipelineId = itemId,
version = selection.itemVersion,
stageKey = key.orEmpty(),
openSection = sectionKey,
onOpenSection = {},
onBack = onBack,
wide = wide,
modifier = paneModifier,
@@ -298,7 +331,13 @@ private fun RunsDestination(
depth = depth,
wide = wide,
onPopTo = { level -> onSelectionChange(popTo(selection, level)) },
backContentDescription = { level -> if (level == 0) "Back to runs" else "Back to run" },
backContentDescription = { level ->
when (level) {
0 -> "Back to runs"
1 -> "Back to run"
else -> "Back to stage"
}
},
pane = { level, covered, paneModifier ->
when (level) {
0 -> RunHistoryScreen(
@@ -337,6 +376,34 @@ private fun RunsDestination(
runId = itemId,
stageKey = key,
visitIndex = visit,
openSection = null,
onOpenSection = { sectionKey ->
onSelectionChange(
selection.copy(pane = MasterPane.Section, sectionKey = sectionKey),
)
},
onVisitChange = { onSelectionChange(selection.copy(stageVisit = it)) },
onBack = onBack,
wide = wide,
modifier = paneModifier,
covered = covered,
)
}
3 -> rememberPaneContent(
selection.sectionKey?.let {
Triple(selection.itemId, it, selection.stageKey)
},
)?.let { (itemId, sectionKey, key) ->
// A closing section pane keeps the identity that opened it, so
// it renders its content until it is fully off-screen.
RunStagePane(
vm = vm,
runId = itemId,
stageKey = key.orEmpty(),
visitIndex = selection.stageVisit,
openSection = sectionKey,
onOpenSection = {},
onVisitChange = { onSelectionChange(selection.copy(stageVisit = it)) },
onBack = onBack,
wide = wide,
modifier = paneModifier,
@@ -115,6 +115,13 @@ fun SectionHeader(
* section does no work and the resting layout is the collapsed header alone.
* With animations disabled Compose maps the platform animator duration scale
* onto the specs, so the body is at its final state at once.
*
* [content] is laid out in a single full-width [Column]: [AnimatedVisibility]
* measures *one* child, so several composables emitted straight into the body
* would be stacked on top of each other at the section's top instead of under
* their header (the "renders oddly at the top of the section" defect). The
* wrapper gives the transition exactly one child and lets a section hold any
* number of rows.
*/
@Composable
fun CollapsibleSectionBody(
@@ -130,7 +137,7 @@ fun CollapsibleSectionBody(
fadeOut(animationSpec = MotionQuick.spec()),
modifier = modifier,
) {
content()
Column(Modifier.fillMaxWidth()) { content() }
}
}
@@ -17,6 +17,7 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.PrimaryScrollableTabRow
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Tab
import androidx.compose.material3.Text
@@ -24,10 +25,7 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.heading
import androidx.compose.ui.semantics.semantics
@@ -42,14 +40,11 @@ import net.awan.pipelines.data.protocol.Stage
import net.awan.pipelines.data.protocol.edges
import net.awan.pipelines.ui.AppViewModel
import net.awan.pipelines.ui.components.CodeBlock
import net.awan.pipelines.ui.components.CollapsibleSectionBody
import net.awan.pipelines.ui.components.DetailTabBody
import net.awan.pipelines.ui.components.KeyValueRow
import net.awan.pipelines.ui.components.PaneIdentity
import net.awan.pipelines.ui.components.SectionExpansionState
import net.awan.pipelines.ui.components.SectionHeader
import net.awan.pipelines.ui.components.paneSafeArea
import net.awan.pipelines.ui.components.rememberSectionExpansion
import net.awan.pipelines.ui.displayMap
import net.awan.pipelines.ui.fileChangeLine
import net.awan.pipelines.ui.fileChangesSummary
@@ -184,24 +179,16 @@ internal const val STAGE_DEFINITION_TITLE = "Stage definition"
@Composable
internal fun paneContainerColor() = MaterialTheme.colorScheme.surfaceContainer
/**
* Whether the stage-definition section starts expanded. It is the only content of
* a pane opened without a run context (a pipeline's own stage definition, see
* [PipelineStagePane]), and starts collapsed in a run pane, where the run's
* results lead and the definition sits at the bottom. An operator's own
* expand/collapse choice is still remembered on top of this default.
*/
internal fun definitionStartsExpanded(runContext: Boolean): Boolean = !runContext
/**
* One run-side result section of the stage pane. The order is the operator's
* stated preference: the artifacts gallery first, then the output the stage
* produced for that visit, then the input it received.
* produced for that visit, then the input it received. [title] is the section's
* own visible name (its pane's title and its row on the stage screen).
*/
internal enum class StageResultSection(val key: String) {
Artifacts("artifacts"),
Output("output"),
Input("input"),
internal enum class StageResultSection(val key: String, val title: String) {
Artifacts("artifacts", "Artifacts"),
Output("output", "Output"),
Input("input", "Input"),
}
/** The run-side result sections, in display order (the artifacts gallery first). */
@@ -211,6 +198,74 @@ internal val STAGE_RESULT_SECTIONS: List<StageResultSection> = listOf(
StageResultSection.Input,
)
/** The evidence section key/title ("This run" holds the remaining run evidence). */
internal const val STAGE_EVIDENCE_KEY = "run"
internal const val STAGE_EVIDENCE_TITLE = "This run"
/**
* One section of the stage screen: its stable [key] (what the pane selection and
* the section pane address it by), its own [title], the [fields] it shows when it
* is a plain field section, and [artifacts] when it is the per-visit gallery
* (which renders the shared [ArtifactsSection] instead of [fields]).
*/
internal data class StageSectionSpec(
val key: String,
val title: String,
val fields: List<StageField> = emptyList(),
val artifacts: Boolean = false,
)
/**
* The ordered sections of the stage screen for one stage and visit: a run pane
* leads with the visit's Artifacts gallery, then Output, Input and the remaining
* evidence (`This run`); the stage's own definition is the last section in both
* contexts and the only section when there is no run. Blank field sections are
* skipped; the artifacts section always shows for a reached visit, even when the
* visit recorded none, so the operator always finds the gallery. Pure, so the
* compact stage screen's list and the section pane's content are derived from
* exactly the same spec.
*/
internal fun stageSectionSpecs(
stageKey: String,
graph: Graph,
runSteps: List<RunStep>,
visitIndex: Int,
runContext: Boolean,
): List<StageSectionSpec> {
val specs = ArrayList<StageSectionSpec>()
val stage = graph.stages.firstOrNull { it.key == stageKey }
val definition = stage?.let(::stageDefinitionFields) ?: terminalFields(graph, stageKey)
val selectedStep = runSteps.getOrNull(clampVisitIndex(visitIndex, runSteps.size))
if (runContext && selectedStep != null) {
val runFields = runStepFields(selectedStep)
STAGE_RESULT_SECTIONS.forEach { section ->
when (section) {
StageResultSection.Artifacts -> specs += StageSectionSpec(
key = section.key,
title = section.title,
artifacts = true,
)
StageResultSection.Output -> {
val fields = runFields.filter { it.label == "Output" }
if (fields.isNotEmpty()) specs += StageSectionSpec(section.key, section.title, fields)
}
StageResultSection.Input -> {
val fields = runFields.filter { it.label == "Input" }
if (fields.isNotEmpty()) specs += StageSectionSpec(section.key, section.title, fields)
}
}
}
val evidence = evidenceFields(runFields)
if (evidence.isNotEmpty()) {
specs += StageSectionSpec(STAGE_EVIDENCE_KEY, STAGE_EVIDENCE_TITLE, evidence)
}
}
if (definition.isNotEmpty()) {
specs += StageSectionSpec(STAGE_DEFINITION_KEY, STAGE_DEFINITION_TITLE, definition)
}
return specs
}
/**
* The pure run view of one [RunStep] for the selected stage: status, outcome,
* duration, visit against the control's max_visits, target, session id,
@@ -300,17 +355,19 @@ internal fun clampVisitIndex(index: Int, visitCount: Int): Int =
internal fun showVisitTabs(visitCount: Int): Boolean = visitCount > 1
/**
* The stage pane's content: opened as the rightmost level of the sliding stack
* from a timeline entry or a graph node, exactly like the list and detail panes,
* so there is no popup anywhere. It carries its own Scaffold/[TopAppBar] with a
* back arrow that pops one level (the same step as the peek tap and system Back).
* The stage screen: the pane shown when no section is open. It carries its own
* Scaffold/[TopAppBar] with a back arrow that pops one level (the same step as
* the peek tap and system Back), leads with the run-side headline, and keeps a
* **compact list of the stage's sections**: tapping a section opens it as its own
* pane ([StageSectionPane]) rather than growing this screen inline.
*
* [runSteps] is every recorded visit of the stage in order; when there is more
* than one the pane shows a visit tab row (visit 1..N) and starts on
* [initialVisitIndex] (the tapped timeline entry, or the most recent visit when
* opened from the graph). A stage the run never reached has no run section; the
* identity heading and type always show, so a freshly opened pane never reads as
* empty.
* than one the pane shows a visit tab row (visit 1..N) and reports the chosen
* visit through [onVisitIndexChange]. The selection is owned by the caller (part
* of the saved pane selection), so this screen and the section pane always agree
* and it survives rotation. A stage the run never reached lists only its
* definition; the identity heading and type always show, so a freshly opened
* pane never reads as empty.
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
@@ -318,9 +375,9 @@ fun StageDetailPane(
stageKey: String,
graph: Graph,
runSteps: List<RunStep> = emptyList(),
initialVisitIndex: Int = 0,
configuredServerUrl: String = "",
fetchArtifactBytes: suspend (String) -> ByteArray? = { null },
visitIndex: Int = 0,
onVisitIndexChange: (Int) -> Unit = {},
onOpenSection: (String) -> Unit = {},
onBack: () -> Unit,
wide: Boolean = false,
modifier: Modifier = Modifier,
@@ -338,32 +395,10 @@ fun StageDetailPane(
)
return
}
// The selected visit is owned here so the selection's copy/share actions in
// the top bar and the body's per-visit artifacts section read the same step.
// Reset when the stage changes; saved across configuration.
var visitIndex by rememberSaveable(stageKey) {
mutableIntStateOf(clampVisitIndex(initialVisitIndex, runSteps.size))
}
val clampedVisit = clampVisitIndex(visitIndex, runSteps.size)
val selectedStep = runSteps.getOrNull(clampedVisit)
val visitArtifacts = remember(selectedStep) { selectedStep?.artifacts.orEmpty() }
// The artifact selection for this visit: a long-press enters it, a tap
// toggles. Keyed on the visit, so switching visits leaves selection mode; it
// restores fully (or not at all) after rotation.
val selectionState = rememberArtifactSelection(stageKey, clampedVisit)
// The shared selection host owns the selection, the contextual bar, the bulk
// copy/share actions and Back-leaves-selection-first; the two artifact
// surfaces go through it so they can never drift.
ArtifactSelectionScaffold(
selectionState = selectionState,
candidates = visitArtifacts,
configuredServerUrl = configuredServerUrl,
fetchArtifactBytes = fetchArtifactBytes,
Scaffold(
modifier = modifier,
containerColor = paneContainerColor(),
contentWindowInsets = paneSafeArea(),
topBar = {
TopAppBar(
title = { Text(GraphSemantics.nodeLabel(stageKey)) },
@@ -376,68 +411,47 @@ fun StageDetailPane(
colors = TopAppBarDefaults.topAppBarColors(containerColor = paneContainerColor()),
)
},
) { selection, onSelectionChange, padding ->
) { padding ->
StageDetailBody(
stageKey = stageKey,
graph = graph,
runSteps = runSteps,
selectedStep = selectedStep,
visitIndex = clampedVisit,
onVisitIndexChange = { visitIndex = it },
configuredServerUrl = configuredServerUrl,
fetchArtifactBytes = fetchArtifactBytes,
visitIndex = visitIndex,
onVisitIndexChange = onVisitIndexChange,
onOpenSection = onOpenSection,
wide = wide,
runContext = runContext,
selection = selection,
onSelectionChange = onSelectionChange,
modifier = Modifier.padding(padding),
)
}
}
/** The stage detail's content, wrapped by [StageDetailPane]. */
@OptIn(ExperimentalMaterial3Api::class)
/** The stage screen's content, wrapped by [StageDetailPane]. */
@Composable
private fun StageDetailBody(
stageKey: String,
graph: Graph,
runSteps: List<RunStep>,
selectedStep: RunStep?,
visitIndex: Int,
onVisitIndexChange: (Int) -> Unit,
configuredServerUrl: String,
fetchArtifactBytes: suspend (String) -> ByteArray?,
onOpenSection: (String) -> Unit,
wide: Boolean,
runContext: Boolean,
selection: ArtifactSelection,
onSelectionChange: (ArtifactSelection) -> Unit,
modifier: Modifier = Modifier,
) {
val text = pipelinesText()
val stage = graph.stages.firstOrNull { it.key == stageKey }
val definition = remember(stage, graph, stageKey) {
stage?.let(::stageDefinitionFields) ?: terminalFields(graph, stageKey)
}
val typeLabel = if (stage != null) effectiveStageType(stage.type) else "terminal"
val clampedVisit = clampVisitIndex(visitIndex, runSteps.size)
// Derived once per selected step, not per recomposition. Only the run-side
// facts lead the pane; the stage's own configs stay in the definition section.
val selectedStep = runSteps.getOrNull(clampedVisit)
// The run-side headline: always visible, never behind a section, so a freshly
// opened screen never reads as empty.
val runFields = remember(selectedStep) { selectedStep?.let(::runStepFields).orEmpty() }
val headline = headlineFields(runFields)
val inputFields = remember(runFields) { runFields.filter { it.label == "Input" } }
val outputFields = remember(runFields) { runFields.filter { it.label == "Output" } }
val evidence = evidenceFields(runFields)
// The stage definition is the only content of a pane without a run context,
// so it starts expanded there and collapsed at the bottom of a run pane.
val expansion = rememberSectionExpansion(
stageKey,
initiallyExpanded = if (definitionStartsExpanded(runContext)) {
setOf(STAGE_DEFINITION_KEY)
} else {
emptySet()
},
)
val sections = remember(stageKey, graph, runSteps, clampedVisit, runContext) {
stageSectionSpecs(stageKey, graph, runSteps, clampedVisit, runContext)
}
DetailTabBody(wide, modifier) { columnModifier ->
Column(
@@ -452,106 +466,235 @@ private fun StageDetailBody(
modifier = Modifier.semantics { heading() },
)
StageTypeChip(typeLabel)
if (showVisitTabs(runSteps.size)) {
PrimaryScrollableTabRow(
selectedTabIndex = clampedVisit,
edgePadding = 0.dp,
containerColor = paneContainerColor(),
modifier = Modifier.fillMaxWidth().padding(top = SPACING_S),
) {
runSteps.forEachIndexed { index, _ ->
Tab(
selected = index == clampedVisit,
onClick = { onVisitIndexChange(index) },
text = { Text("visit ${index + 1}", maxLines = 1) },
)
}
}
}
// Identity and result: always visible, never behind a collapse, so a
// freshly opened pane never reads as empty.
StageVisitTabs(runSteps.size, clampedVisit, onVisitIndexChange)
headline.forEach { field -> StageFieldRow(field) }
if (headline.isEmpty() && selectedStep == null && stage == null && definition.isEmpty()) {
if (headline.isEmpty() && selectedStep == null && stage == null && sections.isEmpty()) {
Text("No details recorded for this node.", style = text.meta)
}
// A run pane leads with the run's results: the artifacts gallery
// first, then the output and the input, then the remaining evidence.
if (runContext && selectedStep != null) {
STAGE_RESULT_SECTIONS.forEach { section ->
when (section) {
StageResultSection.Artifacts -> StageSection(expansion, section.key) {
ArtifactsSection(
step = selectedStep,
configuredServerUrl = configuredServerUrl,
fetchArtifactBytes = fetchArtifactBytes,
selection = selection,
onSelectionChange = onSelectionChange,
)
}
StageResultSection.Output -> if (outputFields.isNotEmpty()) {
StageSection(expansion, section.key) {
outputFields.forEach { StageFieldRow(it) }
}
}
StageResultSection.Input -> if (inputFields.isNotEmpty()) {
StageSection(expansion, section.key) {
inputFields.forEach { StageFieldRow(it) }
}
}
}
}
if (evidence.isNotEmpty()) {
StageSection(expansion, "run", "This run") {
evidence.forEach { field -> StageFieldRow(field) }
}
}
}
// The stage's own definition sits in one clearly named section: it is
// the pane's only content without a run context (so it starts
// expanded) and the collapsed last section of a run pane.
if (definition.isNotEmpty()) {
StageSection(expansion, STAGE_DEFINITION_KEY, STAGE_DEFINITION_TITLE) {
definition.forEach { field -> StageFieldRow(field) }
}
// The compact list of sections: each row is the section's own header,
// and tapping it opens that section as its own pane instead of growing
// this screen inline (which stacked a section's several rows at the
// top of its header).
sections.forEach { section ->
SectionHeader(
title = section.title,
expanded = false,
onToggle = { onOpenSection(section.key) },
containerColor = paneContainerColor(),
)
}
}
}
}
/**
* One collapsible section of the stage pane: its header toggles [expansion]
* under [key] and its body animates open/closed with the shared
* [CollapsibleSectionBody], so the content and the chevron move together. The
* title defaults to the capitalised key (e.g. `artifacts` → `Artifacts`), so the
* pane names every section the same way.
* The shared visit tab row: rendered only when [visitCount] is more than one, so
* a single-visit stage never shows a pointless one-tab row. Both the stage screen
* and a run-section pane use it, so a visit always reads the same way.
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun StageSection(
expansion: SectionExpansionState,
key: String,
title: String = key.capitalized(),
content: @Composable () -> Unit,
internal fun StageVisitTabs(
visitCount: Int,
selectedIndex: Int,
onSelect: (Int) -> Unit,
modifier: Modifier = Modifier,
) {
val expanded = expansion.isExpanded(key)
SectionHeader(
title,
expanded,
{ expansion.toggle(key) },
if (!showVisitTabs(visitCount)) return
PrimaryScrollableTabRow(
selectedTabIndex = selectedIndex,
edgePadding = 0.dp,
containerColor = paneContainerColor(),
)
CollapsibleSectionBody(expanded) { content() }
modifier = modifier.fillMaxWidth().padding(top = SPACING_S),
) {
repeat(visitCount) { index ->
Tab(
selected = index == selectedIndex,
onClick = { onSelect(index) },
text = { Text("visit ${index + 1}", maxLines = 1) },
)
}
}
}
/** The stage pane hosted directly by the run destination, reading the run detail. */
/**
* One stage section opened as its own pane: its own Scaffold/[TopAppBar] titled
* with the section's own name, its own scroll and its own top spacing, so the
* content always sits cleanly under its header. It is the section level of the
* sliding stack (a full-window screen over the stage on a narrow window, a panel
* to the stage pane's right on a wide one); Back — the app bar, system Back or
* the peek — pops exactly one level, back to the stage.
*
* The artifacts section keeps the shared [ArtifactSelectionScaffold], so its
* gallery retains long-press selection, the contextual bar and
* Back-leaves-selection-first; every other section is plain fields. The visited
* section is derived from the same [stageSectionSpecs] the stage screen lists, so
* the two can never disagree.
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun StageSectionPane(
stageKey: String,
sectionKey: String,
graph: Graph,
runSteps: List<RunStep> = emptyList(),
visitIndex: Int = 0,
onVisitIndexChange: (Int) -> Unit = {},
configuredServerUrl: String = "",
fetchArtifactBytes: suspend (String) -> ByteArray? = { null },
onBack: () -> Unit,
wide: Boolean = false,
modifier: Modifier = Modifier,
covered: Boolean = false,
runContext: Boolean = false,
) {
val clampedVisit = clampVisitIndex(visitIndex, runSteps.size)
val selectedStep = runSteps.getOrNull(clampedVisit)
val spec = remember(stageKey, graph, runSteps, clampedVisit, runContext, sectionKey) {
stageSectionSpecs(stageKey, graph, runSteps, clampedVisit, runContext)
.firstOrNull { it.key == sectionKey }
}
val title = spec?.title ?: sectionKey
if (covered) {
PaneIdentity(
title = title,
subtitle = GraphSemantics.nodeLabel(stageKey),
modifier = modifier.fillMaxSize(),
)
return
}
val bar: @Composable () -> Unit = {
TopAppBar(
title = { Text(title) },
navigationIcon = {
IconButton(onClick = onBack) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
}
},
windowInsets = paneSafeArea().only(WindowInsetsSides.Top + WindowInsetsSides.Horizontal),
colors = TopAppBarDefaults.topAppBarColors(containerColor = paneContainerColor()),
)
}
if (spec?.artifacts == true) {
// The gallery keeps the one selection host (long-press selection, the
// contextual bar and Back-leaves-selection-first) it had inline.
val selectionState = rememberArtifactSelection(stageKey, clampedVisit)
ArtifactSelectionScaffold(
selectionState = selectionState,
candidates = selectedStep?.artifacts.orEmpty(),
configuredServerUrl = configuredServerUrl,
fetchArtifactBytes = fetchArtifactBytes,
modifier = modifier,
containerColor = paneContainerColor(),
topBar = bar,
) { selection, onSelectionChange, padding ->
StageSectionBody(
stageKey = stageKey,
spec = spec,
step = selectedStep,
runSteps = runSteps,
visitIndex = clampedVisit,
onVisitIndexChange = onVisitIndexChange,
configuredServerUrl = configuredServerUrl,
fetchArtifactBytes = fetchArtifactBytes,
selection = selection,
onSelectionChange = onSelectionChange,
wide = wide,
modifier = Modifier.padding(padding),
)
}
} else {
Scaffold(
modifier = modifier,
containerColor = paneContainerColor(),
contentWindowInsets = paneSafeArea(),
topBar = bar,
) { padding ->
StageSectionBody(
stageKey = stageKey,
spec = spec,
step = selectedStep,
runSteps = runSteps,
visitIndex = clampedVisit,
onVisitIndexChange = onVisitIndexChange,
configuredServerUrl = configuredServerUrl,
fetchArtifactBytes = fetchArtifactBytes,
selection = ArtifactSelection(),
onSelectionChange = {},
wide = wide,
modifier = Modifier.padding(padding),
)
}
}
}
/**
* One section's content: its own single scroll, a context line naming the stage,
* its own top spacing, and — for a visit's result section — the visit tab row,
* so the operator can switch visits in place. The stage definition skips the tab
* row (it does not change per visit). Plain sections render their rows; the
* artifacts section renders the shared [ArtifactsSection] gallery.
*/
@Composable
private fun StageSectionBody(
stageKey: String,
spec: StageSectionSpec?,
step: RunStep?,
runSteps: List<RunStep>,
visitIndex: Int,
onVisitIndexChange: (Int) -> Unit,
configuredServerUrl: String,
fetchArtifactBytes: suspend (String) -> ByteArray?,
selection: ArtifactSelection,
onSelectionChange: (ArtifactSelection) -> Unit,
wide: Boolean,
modifier: Modifier = Modifier,
) {
val text = pipelinesText()
DetailTabBody(wide, modifier) { columnModifier ->
Column(
modifier = columnModifier
.verticalScroll(rememberScrollState())
.padding(horizontal = SPACING_L, vertical = SPACING_L),
verticalArrangement = Arrangement.spacedBy(SPACING_XS),
) {
Text(GraphSemantics.nodeLabel(stageKey), style = text.meta)
if (spec != null && spec.key != STAGE_DEFINITION_KEY) {
StageVisitTabs(runSteps.size, visitIndex, onVisitIndexChange)
}
when {
spec == null -> Text("This section is not recorded for this stage.", style = text.meta)
spec.artifacts -> if (step != null) {
ArtifactsSection(
step = step,
configuredServerUrl = configuredServerUrl,
fetchArtifactBytes = fetchArtifactBytes,
selection = selection,
onSelectionChange = onSelectionChange,
)
}
else -> spec.fields.forEach { field -> StageFieldRow(field) }
}
}
}
}
/**
* The run-side stage level: with no [openSection] it is the compact stage screen
* ([StageDetailPane]); with one it is that section's pane ([StageSectionPane]).
* Both read the same run detail and derive the stage's visits the same way, so
* the two levels can never disagree and no state is read twice.
*/
@Composable
fun RunStagePane(
vm: AppViewModel,
runId: Long,
stageKey: String,
visitIndex: Int,
openSection: String?,
onOpenSection: (String) -> Unit,
onVisitChange: (Int) -> Unit,
onBack: () -> Unit,
wide: Boolean,
modifier: Modifier = Modifier,
@@ -563,28 +706,55 @@ fun RunStagePane(
val steps = run?.steps.orEmpty()
// One aggregation per step-list change; the stage's visits are read from it.
val timeline = remember(steps) { runTimeline(steps) }
StageDetailPane(
stageKey = stageKey,
graph = run?.graph ?: Graph(),
runSteps = timeline.visitsByStage[stageKey].orEmpty(),
initialVisitIndex = visitIndex,
configuredServerUrl = configuredServerUrl,
fetchArtifactBytes = vm::fetchArtifactBytes,
onBack = onBack,
wide = wide,
modifier = modifier,
covered = covered,
runContext = true,
)
val runSteps = remember(timeline, stageKey) { timeline.visitsByStage[stageKey].orEmpty() }
val graph = run?.graph ?: Graph()
if (openSection == null) {
StageDetailPane(
stageKey = stageKey,
graph = graph,
runSteps = runSteps,
visitIndex = visitIndex,
onVisitIndexChange = onVisitChange,
onOpenSection = onOpenSection,
onBack = onBack,
wide = wide,
modifier = modifier,
covered = covered,
runContext = true,
)
} else {
StageSectionPane(
stageKey = stageKey,
sectionKey = openSection,
graph = graph,
runSteps = runSteps,
visitIndex = visitIndex,
onVisitIndexChange = onVisitChange,
configuredServerUrl = configuredServerUrl,
fetchArtifactBytes = vm::fetchArtifactBytes,
onBack = onBack,
wide = wide,
modifier = modifier,
covered = covered,
runContext = true,
)
}
}
/** The stage pane hosted by the pipeline destination; a definition has no run. */
/**
* The pipeline-side stage level: with no [openSection] it is the stage screen
* (a definition has no run, so it lists just that section); with one it is the
* definition section's pane. The two share the stage screen and the section pane
* with the run side.
*/
@Composable
fun PipelineStagePane(
vm: AppViewModel,
pipelineId: Long,
version: Int,
stageKey: String,
openSection: String?,
onOpenSection: (String) -> Unit,
onBack: () -> Unit,
wide: Boolean,
modifier: Modifier = Modifier,
@@ -592,14 +762,28 @@ fun PipelineStagePane(
) {
val state by vm.pipelineDetail.collectAsStateWithLifecycle()
val detail = state.data?.takeIf { it.id == pipelineId && it.version == version }
StageDetailPane(
stageKey = stageKey,
graph = detail?.graph ?: Graph(),
onBack = onBack,
wide = wide,
modifier = modifier,
covered = covered,
)
val graph = detail?.graph ?: Graph()
if (openSection == null) {
StageDetailPane(
stageKey = stageKey,
graph = graph,
onOpenSection = onOpenSection,
onBack = onBack,
wide = wide,
modifier = modifier,
covered = covered,
)
} else {
StageSectionPane(
stageKey = stageKey,
sectionKey = openSection,
graph = graph,
onBack = onBack,
wide = wide,
modifier = modifier,
covered = covered,
)
}
}
/** One field as either the shared code card (blocks) or a key/value row. */
@@ -627,7 +811,3 @@ internal fun StageTypeChip(label: String) {
)
}
}
/** A section key as its visible title ("definition" → "Definition"). */
private fun String.capitalized(): String =
replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }
@@ -44,11 +44,38 @@ class PaneStateTest {
assertEquals(1, result.selection.depth)
}
@Test
fun backInsideSectionReturnsToTheStageKeepingTheStage() {
val section = PaneSelection(
pane = MasterPane.Section,
itemId = 7L,
itemVersion = 3,
sectionIndex = 1,
stageKey = "planner",
stageVisit = 2,
sectionKey = "artifacts",
)
val result = backStep(section) as BackResult.Show
// Exactly one level: section -> stage, keeping the stage and its visit,
// clearing only the open section.
assertEquals(MasterPane.Stage, result.selection.pane)
assertEquals(7L, result.selection.itemId)
assertEquals(3, result.selection.itemVersion)
assertEquals("planner", result.selection.stageKey)
assertEquals(2, result.selection.stageVisit)
assertEquals(null, result.selection.sectionKey)
assertEquals(2, result.selection.depth)
}
@Test
fun depthIsBoundedByTheDeclaredMaximum() {
assertEquals(0, PaneSelection(MasterPane.List).depth)
assertEquals(1, PaneSelection(MasterPane.Detail, itemId = 1L).depth)
assertEquals(2, PaneSelection(MasterPane.Stage, itemId = 1L, stageKey = "s").depth)
assertEquals(
3,
PaneSelection(MasterPane.Section, itemId = 1L, stageKey = "s", sectionKey = "artifacts").depth,
)
// Every pane's depth is within the bound; the stack can never grow deeper.
MasterPane.entries.forEach { pane ->
assertTrue(PaneSelection(pane = pane).depth <= MAX_PANE_DEPTH)
@@ -65,18 +92,26 @@ class PaneStateTest {
stageKey = "s",
stageVisit = 3,
)
val section = stage.copy(pane = MasterPane.Section, sectionKey = "artifacts")
// Level 0 (list): everything closes.
assertEquals(PaneSelection(), popTo(stage, 0))
// Level 1 (detail): only the stage closes, the item is kept.
val detail = popTo(stage, 1)
assertEquals(PaneSelection(), popTo(section, 0))
// Level 1 (detail): the stage and its section close, the item is kept.
val detail = popTo(section, 1)
assertEquals(MasterPane.Detail, detail.pane)
assertEquals(9L, detail.itemId)
assertEquals(2, detail.itemVersion)
assertEquals(1, detail.sectionIndex)
assertEquals(null, detail.stageKey)
assertEquals(null, detail.sectionKey)
// Level 2 (stage): only the section closes, the stage is kept.
val stageAgain = popTo(section, 2)
assertEquals(MasterPane.Stage, stageAgain.pane)
assertEquals("s", stageAgain.stageKey)
assertEquals(null, stageAgain.sectionKey)
// A level at or beyond the current depth is a no-op.
assertEquals(stage, popTo(stage, 2))
assertEquals(stage, popTo(stage, 5))
assertEquals(section, popTo(section, 3))
assertEquals(section, popTo(section, 5))
assertEquals(PaneSelection(MasterPane.Detail, 4L), popTo(PaneSelection(MasterPane.Detail, 4L), 3))
}
@@ -95,6 +130,15 @@ class PaneStateTest {
stageKey = "planner",
stageVisit = 3,
),
PaneSelection(
pane = MasterPane.Section,
itemId = 8L,
itemVersion = 2,
sectionIndex = 4,
stageKey = "planner",
stageVisit = 3,
sectionKey = "artifacts",
),
)
cases.forEach { selection ->
val saved = with(PaneSelectionSaver) { scope.save(selection) }
@@ -105,7 +149,7 @@ class PaneStateTest {
@Test
fun encodedPayloadIsPrimitivesOnly() {
// Process-death restoration stays small: only Strings, ints and a long,
// with a null stage key encoded as an empty string.
// with a null stage or section key encoded as an empty string.
val encoded = encodePaneSelection(
PaneSelection(
pane = MasterPane.Stage,
@@ -116,14 +160,25 @@ class PaneStateTest {
stageVisit = 4,
),
)
assertEquals(listOf("Stage", 9L, 2, 1, "planner", 4), encoded)
assertEquals(listOf("Stage", 9L, 2, 1, "planner", 4, ""), encoded)
assertEquals(
PaneSelection(MasterPane.Stage, 9L, 2, 1, "planner", 4),
decodePaneSelection(encoded),
)
assertEquals(
listOf("Detail", 9L, 2, 1, "", 0),
listOf("Detail", 9L, 2, 1, "", 0, ""),
encodePaneSelection(PaneSelection(MasterPane.Detail, 9L, 2, 1)),
)
val section = PaneSelection(
pane = MasterPane.Section,
itemId = 9L,
itemVersion = 2,
sectionIndex = 1,
stageKey = "planner",
stageVisit = 4,
sectionKey = "artifacts",
)
assertEquals(listOf("Section", 9L, 2, 1, "planner", 4, "artifacts"), encodePaneSelection(section))
assertEquals(section, decodePaneSelection(encodePaneSelection(section)))
}
}
@@ -195,11 +195,49 @@ class StageDetailSheetTest {
}
@Test
fun theDefinitionStartsExpandedOnlyWithoutARunContext() {
// A pipeline's stage definition is the pane's only content, so it starts
// expanded; a run pane leads with results and collapses the definition.
assertTrue(definitionStartsExpanded(runContext = false))
assertFalse(definitionStartsExpanded(runContext = true))
fun theStageScreenListsItsSectionsInOrderAndTheDefinitionLast() {
val stage = Stage(key = "implementer", type = "model", prompt = "do", output = "done")
val graph = Graph(stages = listOf(stage))
val step = RunStep(
status = "succeeded",
outcome = "success",
target = "reviewer",
input = mapOf("prompt" to JsonPrimitive("do it")),
output = mapOf("result" to JsonPrimitive("done")),
diagnostic = "why",
)
val specs = stageSectionSpecs("implementer", graph, listOf(step), visitIndex = 0, runContext = true)
// The run's results lead (artifacts first) and the stage's own definition
// is the last section, named with its own visible title.
assertEquals(listOf("artifacts", "output", "input", "run", "definition"), specs.map { it.key })
assertEquals(
listOf("Artifacts", "Output", "Input", "This run", "Stage definition"),
specs.map { it.title },
)
// The artifacts section is the gallery (no fields); the definition keeps
// the stage's own fields.
assertTrue(specs.first().artifacts)
assertTrue(specs.first().fields.isEmpty())
assertTrue(specs.first { it.key == "definition" }.fields.isNotEmpty())
}
@Test
fun aDefinitionOnlyStageListsJustItsDefinition() {
val graph = Graph(stages = listOf(Stage(key = "planner", type = "model", prompt = "do")))
// No run context (a pipeline stage): only the definition section.
val specs = stageSectionSpecs("planner", graph, emptyList(), visitIndex = 0, runContext = false)
assertEquals(listOf("definition"), specs.map { it.key })
}
@Test
fun aVisitThatRecordedNoOutputOrInputListsArtifactsAndTheDefinition() {
val graph = Graph(stages = listOf(Stage(key = "s", type = "model")))
val step = RunStep(status = "succeeded")
val specs = stageSectionSpecs("s", graph, listOf(step), visitIndex = 0, runContext = true)
// The artifacts gallery always shows for a reached visit; empty
// output/input/evidence sections are omitted rather than listed blank.
assertEquals(listOf("artifacts", "definition"), specs.map { it.key })
assertTrue(specs.none { !it.artifacts && it.fields.isEmpty() })
}
@Test