android+server: make every view live from the pushed run events

The pipelines list (and every other run-state view) kept the row it had when
the screen was last fetched. Two class-level root causes, fixed here:

- The app subscribed to run events scoped to the one workspace the operator
  configured, so a run executing in another workspace (a lane worktree is its
  own workspace) never delivered a frame: the rows only changed on a manual
  refresh. The app now asks for the blank "any workspace" scope the server
  already treats as any (ALL_WORKSPACES in AppViewModel.subscribeRuns, blank
  default on PipelinesRepository.subscribeRuns). Workspace isolation on the
  server is unchanged: an explicit workspace scope still matches only that
  workspace (TestRunEventWorkspaceIsolation); the new
  TestRunEventAllWorkspacesSubscription pins the blank "any" scope.
- foldRunEvent refused to insert a run the list had never held, returning
  needsResync and leaving the list untouched, so a run started after the
  screen loaded appeared only after a fetch. The pushed frame now carries the
  pipeline identity a row needs (RunEventFrame.PipelineID/Name/Title/Version),
  so the fold inserts the new run at the head from the frame's own identity
  without a fetch and without reordering existing rows; a server that predates
  those fields still falls back to the one resync. Added fields and why each
  is needed: pipeline_id (row's pipeline join), name (row title), title (the
  stored operator-facing title a row shows), version (the row's pinned
  version) - everything a RunSummary row renders, so no per-event fetch and no
  guess from stale cache. The server stamps them on every emitted frame from a
  per-run identity remembered at create/decide/cancel/resume (runner.go
  runIdentity/rememberRunIdentity/stampRunIdentity), first-write-wins through
  the coalescer (mergeRunEventFrame) and on the subscribe snapshot, so every
  transition - created/queued, running, each stage start/finish, terminal -
  carries them. Reconnect still resyncs once then follows pushes; the
  pull-to-refresh path is untouched.

Checks run: go build ./..., go vet ./..., go test ./... (all packages ok),
go test -race -run 'RunEvent|RunEvents|Subscribe' ./internal/protocol/
./internal/server/, and gradle --no-daemon testDebugUnitTest assembleDebug
(558 tests, 0 failures, 0 skipped; BUILD SUCCESSFUL).

Packaged templates and the packaged authoring skill references were checked:
nothing there describes the run_event field list or the client's subscription
scope, so neither needs updating (references/notifications.md only says the
attention surface is derived from the pushed run events, which is unchanged).
Docs are a shared file owned by a single writer and were not edited:
docs/cli.md's description of the pushed `run_event` frame still enumerates
only the older fields (status, outcome, current stage, completed-step count,
timestamps, optional step) and is now incomplete - it should mention the
pipeline identity fields; docs/ntfy.md remains accurate.
This commit is contained in:
2026-10-17 20:20:05 +01:00
parent 50af9dbc62
commit 5e2aece1be
11 changed files with 433 additions and 51 deletions
@@ -126,18 +126,20 @@ class PipelinesRepository(
client.call(Message(type = Protocol.GET_RUN_EVIDENCE, runId = id)).evidence ?: emptyList()
/**
* Subscribes this connection to the workspace's run-state pushes. The server
* first sends the current state of every matching run as `run_event` frames
* (which arrive on [events]), then a compact delta on each transition, so a
* reconnecting client re-syncs before it applies deltas. [runIds] narrows the
* watch; an empty list watches the whole workspace.
* Subscribes this connection to run-state pushes. The server first sends the
* current state of every matching run as `run_event` frames (which arrive on
* [events]), then a compact delta on each transition, so a reconnecting client
* re-syncs before it applies deltas. [workspace] scopes the watch; the default
* blank scope watches every workspace, which is what the runs list displays
* (the work an operator watches can run in any workspace). [runIds] narrows
* the watch further; an empty list watches the whole scope.
*
* Returns the request id and does not wait for the ack: a server that never
* acks must not block the send lock, and the pushes themselves confirm the
* watch (see `AppViewModel.observeRunEvents`). Any ack or error reply lands on
* [events] with this id.
*/
suspend fun subscribeRuns(workspace: String, runIds: List<Long> = emptyList()): String =
suspend fun subscribeRuns(workspace: String = "", runIds: List<Long> = emptyList()): String =
client.sendOnly(
Message(
type = Protocol.SUBSCRIBE,
@@ -399,6 +399,14 @@ data class RunDetail(
data class RunEventFrame(
@SerialName("run_id") val runId: Long = 0,
val workspace: String = "",
// The run's pipeline identity and stored title, repeated on every frame so a
// client that has never held the run can build its list row from the frame
// (the fold's new-run case) rather than re-reading the whole list. Blank on a
// server that predates them, which makes the fold fall back to a resync.
@SerialName("pipeline_id") val pipelineId: Long = 0,
val name: String = "",
val title: String = "",
val version: Int = 0,
val status: String = "",
val outcome: String = "",
@SerialName("current_stage") val currentStage: String = "",
@@ -166,12 +166,13 @@ class AppViewModel(
.distinctUntilChanged()
.stateIn(viewModelScope, SharingStarted.Eagerly, chatStore.value.active != null)
// The live run subscription: the workspace currently watched, the job that
// established it, and the coalescing jobs that re-sync on a push. One
// subscription per connection; a reconnect (which clears this watch, not the
// saved workspace) re-subscribes, and every (re)subscribe re-reads the list so
// the gap re-syncs from the server's current state. No timer is involved:
// every refresh here is driven by a push, a (re)connect or an operator action.
// The live run subscription: the scope currently watched (always
// [ALL_WORKSPACES] while subscribed, null when not), the job that established
// it, and the coalescing jobs that re-sync on a push. One subscription per
// connection; a reconnect (which clears this watch, not the saved workspace)
// re-subscribes, and every (re)subscribe re-reads the list so the gap re-syncs
// from the server's current state. No timer is involved: every refresh here is
// driven by a push, a (re)connect or an operator action.
private var subscribedWorkspace: String? = null
private var subscriptionJob: Job? = null
// The id of the last subscribe we sent, so its optional ack or error reply
@@ -250,19 +251,22 @@ class AppViewModel(
connected = isConnected
}
}
// The run stream is live only while connected to the right workspace: the
// pair (connected, workspace) drives a single subscribe. A reconnect drops
// the socket, so the pair emits (false, workspace) — which clears the watch
// — then (true, workspace), which re-subscribes; a subscribe re-reads the
// list first so the gap is re-synced before any later delta applies.
// The run stream watches every workspace the client displays. The runs
// list — and so the pipelines list's last-run line and every run summary
// card — is server-wide (list_runs is not workspace-scoped), while the
// work an operator watches can run in any workspace (a lane worktree is
// its own workspace). A watch scoped to the one configured workspace
// never receives those runs, which is exactly the stale row this fixes,
// so the client asks for the blank "any workspace" scope the server
// already honours. A reconnect drops the socket, so this emits false
// (which clears the watch) then true, which re-subscribes; a subscribe
// re-reads the list first so the gap is re-synced before any later delta
// applies.
viewModelScope.launch {
combine(connectionState, settings.workspace) { state, workspace ->
(state is ConnectionState.Connected) to workspace
}
connectionState
.map { it is ConnectionState.Connected }
.distinctUntilChanged()
.collect { (live, workspace) ->
if (live && workspace.isNotBlank()) subscribeRuns(workspace) else streamDown()
}
.collect { live -> if (live) subscribeRuns() else streamDown() }
}
}
@@ -339,19 +343,22 @@ class AppViewModel(
}
/**
* Establishes the one watch over [workspace]. A workspace switch drops the
* previous watch first (the server caps subscriptions per connection), and
* the watch is confirmed live by its ack, its first push or the read that
* follows (see [observeRunEvents]) rather than assumed: if the server cannot
* subscribe, the stream reads as not live instead of silently stale.
* Establishes the one watch over every workspace the client displays (the
* blank scope, which the server treats as "any"): the runs list is
* server-wide, so the client asks for the runs it shows rather than the one
* workspace the operator configured. A reconnect (which clears this watch)
* re-subscribes, and the watch is confirmed live by its ack, its first push
* or the read that follows (see [observeRunEvents]) rather than assumed: if
* the server cannot subscribe, the stream reads as not live instead of
* silently stale.
*/
private fun subscribeRuns(workspace: String) {
if (subscribedWorkspace == workspace) return
private fun subscribeRuns() {
if (subscribedWorkspace == ALL_WORKSPACES) return
subscriptionJob?.cancel()
_runStream.value = RunStreamState.Connecting
subscriptionJob = viewModelScope.launch {
try {
// Drop any previous watch so a workspace switch never stacks
// Drop any previous watch so a reconnect never stacks
// subscriptions (the server caps them per connection). A failed
// drop must not stop the new subscribe, but it is not swallowed:
// the badge stays not-live until a push confirms the new watch.
@@ -363,8 +370,8 @@ class AppViewModel(
}
// Mark the watch before sending so a push that races the send is
// still attributed to this subscription.
subscribedWorkspace = workspace
subscriptionId = repository.subscribeRuns(workspace)
subscribedWorkspace = ALL_WORKSPACES
subscriptionId = repository.subscribeRuns()
// The server's subscribe snapshot is not guaranteed complete (the
// running server's can be partial), so take an authoritative read
// of the watched state once per (re)connect; later pushes then fold
@@ -1107,6 +1114,15 @@ class AppViewModel(
companion object {
private const val CONNECT_TIMEOUT_MILLIS = 20_000L
/**
* The run watch scope for every workspace the client displays. The server
* treats a blank subscribe workspace as "any", matching the server-wide
* runs list rather than the one workspace the operator configured: work
* the operator watches can run in any workspace (a lane worktree is its
* own), so a workspace-scoped watch would never see it.
*/
private const val ALL_WORKSPACES = ""
/** Bounded backoff retries for a push-driven list/detail read that failed. */
private const val RESYNC_RETRY_LIMIT = 3
private const val RESYNC_RETRY_BASE_MILLIS = 500L
@@ -13,10 +13,12 @@ import net.awan.pipelines.data.protocol.RunSummary
* count) is advanced here from the pushes.
*
* The fold is deliberately monotonic so an out-of-order or duplicated delta can
* never move a run or a step backwards, and a delta that names a run the list
* does not hold reports [RunListFold.needsResync] rather than inventing a row:
* a brand-new run's summary (name, pipeline id, version) is not in the compact
* delta, so the caller re-reads the server's current list once instead.
* never move a run or a step backwards. A delta that names a run the list does
* not hold is inserted as a new row from the frame's own identity (pipeline id,
* name, title, version), so a run started after the screen loaded appears
* without a fetch; only a frame with no such identity (an older server) reports
* [RunListFold.needsResync], and the caller re-reads the server's current list
* once instead.
*/
/** Run statuses from which a run never transitions again. */
@@ -62,15 +64,26 @@ internal fun stepLifecycleRank(status: String): Int = when (status.trim().lowerc
/**
* The outcome of folding one frame into the runs list: the list with the matching
* run updated in place (same order, so a read never reshuffles), or
* [needsResync] when the frame names a run the list does not hold.
* [needsResync] when the frame names a run the list does not hold and carries no
* identity to build its row from.
*/
internal data class RunListFold(val runs: List<RunSummary>, val needsResync: Boolean)
/** Folds one pushed frame into the runs list, in place and without reordering. */
/**
* Folds one pushed frame into the runs list, in place and without reordering.
* A run the list has never held is inserted as a new row at the head (newest
* first, matching the server's own order) when the frame carries its pipeline
* identity, so a run started after the screen loaded appears while the operator
* watches without a fetch; a frame with no identity (an older server) still
* reports [RunListFold.needsResync] so the caller re-reads once instead.
*/
internal fun foldRunEvent(runs: List<RunSummary>, frame: RunEventFrame): RunListFold {
if (frame.runId <= 0) return RunListFold(runs, needsResync = false)
val index = runs.indexOfFirst { it.id == frame.runId }
if (index < 0) return RunListFold(runs, needsResync = true)
if (index < 0) {
val added = frame.newRunSummary() ?: return RunListFold(runs, needsResync = true)
return RunListFold(listOf(added) + runs, needsResync = false)
}
val current = runs[index]
val updated = current.applying(frame)
if (updated == current) return RunListFold(runs, needsResync = false)
@@ -79,6 +92,30 @@ internal fun foldRunEvent(runs: List<RunSummary>, frame: RunEventFrame): RunList
return RunListFold(next, needsResync = false)
}
/**
* The row a frame for a run the list has never held describes, or null when the
* frame lacks the pipeline identity a row needs (an older server), in which case
* the caller resyncs instead of inventing an incomplete row. The frame already
* carries everything the row shows, so no field is guessed from cached state.
*/
internal fun RunEventFrame.newRunSummary(): RunSummary? {
if (pipelineId <= 0 || name.isBlank()) return null
return RunSummary(
id = runId,
pipelineId = pipelineId,
name = name,
title = title.ifBlank { null },
version = version,
status = status,
executing = executingFor(status, true),
outcome = outcome,
currentStage = currentStage,
createdAt = createdAt,
finishedAt = finishedAt,
completedSteps = completedSteps,
)
}
/**
* Folds one pushed frame into a cached run detail: the run's own status/outcome/
* stage/timestamps plus at most one step, matched by sequence so a duplicate or
@@ -75,13 +75,23 @@ class RunEventsTest {
completedSteps: Int = 0,
finishedAt: String = "",
step: RunStepDelta? = null,
pipelineId: Long = 0,
name: String = "",
title: String = "",
version: Int = 0,
createdAt: String = "",
) = RunEventFrame(
runId = runId,
workspace = "/work",
pipelineId = pipelineId,
name = name,
title = title,
version = version,
status = status,
outcome = outcome,
currentStage = currentStage,
completedSteps = completedSteps,
createdAt = createdAt,
finishedAt = finishedAt,
step = step,
)
@@ -107,15 +117,68 @@ class RunEventsTest {
}
@Test
fun aCreatedRunNotInTheListAsksForAResyncInsteadOfInventingARow() {
fun aNewRunAppearsFromItsFrameWithoutAFetch() {
val runs = listOf(summary(1, status = "succeeded", executing = false))
// The server tags the frame with the pipeline identity a row needs, so a
// run started after the screen loaded is inserted rather than resynced.
val fold = foldRunEvent(
runs,
frame(
runId = 9,
status = "queued",
pipelineId = 4,
name = "pipeline",
title = "a fresh run",
version = 3,
createdAt = "2026-10-17T09:30:00Z",
),
)
assertFalse(fold.needsResync)
// The existing rows keep their relative order; the new run is added first
// (the server's own newest-first order), never spliced between them.
assertEquals(listOf(9L, 1L), fold.runs.map { it.id })
val added = fold.runs.first()
assertEquals(4L, added.pipelineId)
assertEquals("pipeline", added.name)
assertEquals("a fresh run", added.title)
assertEquals(3, added.version)
assertEquals("queued", added.status)
assertTrue(added.executing)
assertEquals("2026-10-17T09:30:00Z", added.createdAt)
}
@Test
fun aFrameForAnUnknownRunWithoutIdentityStillAsksForAResync() {
val runs = listOf(summary(1))
// A brand-new run's compact delta carries no name/pipeline/version, so the
// list cannot render it and must re-read the server's current state.
// A server that predates the identity fields carries none: the list cannot
// render the row and must re-read the server's current state once.
val fold = foldRunEvent(runs, frame(runId = 9, status = "queued"))
assertTrue(fold.needsResync)
assertSame(runs, fold.runs)
}
@Test
fun anInsertedRunThenAdvancesInPlace() {
// A run inserted from its created frame must later update in place, so a
// live run shows running and then succeeded without being re-inserted.
val created = foldRunEvent(
emptyList(),
frame(runId = 9, status = "queued", pipelineId = 4, name = "pipeline", version = 1, createdAt = "2026-10-17T09:30:00Z"),
).runs
val running = foldRunEvent(created, frame(runId = 9, status = "running", currentStage = "build", completedSteps = 1))
assertFalse(running.needsResync)
assertEquals(1, running.runs.size)
assertEquals("running", running.runs[0].status)
assertEquals("build", running.runs[0].currentStage)
val done = foldRunEvent(
running.runs,
frame(runId = 9, status = "succeeded", outcome = "success", completedSteps = 1, finishedAt = "2026-10-17T09:31:00Z"),
)
assertEquals(1, done.runs.size)
assertEquals("succeeded", done.runs[0].status)
assertFalse(done.runs[0].executing)
}
@Test
fun aDeltaForAnUnknownRunAlsoAsksForAResync() {
val runs = listOf(summary(1, status = "succeeded", executing = false))
@@ -1,8 +1,10 @@
package net.awan.pipelines.ui.screens
import java.time.OffsetDateTime
import net.awan.pipelines.data.protocol.RunEventFrame
import net.awan.pipelines.data.protocol.RunSummary
import net.awan.pipelines.ui.components.RunState
import net.awan.pipelines.ui.foldRunEvent
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
@@ -142,4 +144,63 @@ class PipelineLastRunTest {
fun noRunsWordingIsTheExplicitConstant() {
assertEquals("no runs recorded", NO_RUNS_RECORDED)
}
private fun eventFrame(
runId: Long,
status: String,
createdAt: String = "",
finishedAt: String = "",
pipelineId: Long = 0,
name: String = "",
version: Int = 0,
) = RunEventFrame(
runId = runId,
pipelineId = pipelineId,
name = name,
version = version,
status = status,
createdAt = createdAt,
finishedAt = finishedAt,
)
@Test
fun thePipelinesLastRunLineFollowsARunFromRunningToSucceeded() {
val runs = listOf(
run(42, 10, "2026-10-16T11:57:30Z", finishedAt = "", status = "running", executing = true),
)
// A pushed terminal frame settles the run in the folded list; the pipeline
// row's badge — the same join the pipelines list renders — follows without
// a refresh.
val folded = foldRunEvent(
runs,
eventFrame(runId = 42, status = "succeeded", finishedAt = "2026-10-16T11:59:30Z"),
).runs
val badge = pipelineRunBadges(folded, now).getValue(10L)
assertEquals(RunState.Succeeded, badge.state)
assertEquals(42L, badge.runId)
assertEquals("2m ago · 2m 0s · run 42", badge.metaLine())
}
@Test
fun aNewRunAppearsOnThePipelinesLastRunLineFromItsFrame() {
// The pipeline's only recorded run had already finished...
val runs = listOf(run(41, 10, "2026-10-16T10:00:00Z", finishedAt = "2026-10-16T10:01:00Z"))
// ...and a run started after the screen loaded arrives only as a push. It
// must become the pipeline's last-run line with no fetch.
val folded = foldRunEvent(
runs,
eventFrame(
runId = 42,
status = "running",
createdAt = "2026-10-16T11:58:00Z",
pipelineId = 10,
name = "release",
version = 2,
),
).runs
val badge = pipelineRunBadges(folded, now).getValue(10L)
assertEquals(42L, badge.runId)
assertEquals(RunState.Running, badge.state)
assertEquals("2m ago · 2m 0s · run 42", badge.metaLine())
}
}