diff --git a/android/app/src/main/java/net/awan/pipelines/data/PipelinesRepository.kt b/android/app/src/main/java/net/awan/pipelines/data/PipelinesRepository.kt index 77189e59..284f038e 100644 --- a/android/app/src/main/java/net/awan/pipelines/data/PipelinesRepository.kt +++ b/android/app/src/main/java/net/awan/pipelines/data/PipelinesRepository.kt @@ -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 = emptyList()): String = + suspend fun subscribeRuns(workspace: String = "", runIds: List = emptyList()): String = client.sendOnly( Message( type = Protocol.SUBSCRIBE, 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 bf656260..4817181b 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 @@ -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 = "", diff --git a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt index 86c40f57..80f01734 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt @@ -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 diff --git a/android/app/src/main/java/net/awan/pipelines/ui/RunEvents.kt b/android/app/src/main/java/net/awan/pipelines/ui/RunEvents.kt index ffaa7c48..ecf6a66c 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/RunEvents.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/RunEvents.kt @@ -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, 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, 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, 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 diff --git a/android/app/src/test/java/net/awan/pipelines/ui/RunEventsTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/RunEventsTest.kt index 422e91d3..253daa7f 100644 --- a/android/app/src/test/java/net/awan/pipelines/ui/RunEventsTest.kt +++ b/android/app/src/test/java/net/awan/pipelines/ui/RunEventsTest.kt @@ -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)) diff --git a/android/app/src/test/java/net/awan/pipelines/ui/screens/PipelineLastRunTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/screens/PipelineLastRunTest.kt index e0a84f6a..676d8e2b 100644 --- a/android/app/src/test/java/net/awan/pipelines/ui/screens/PipelineLastRunTest.kt +++ b/android/app/src/test/java/net/awan/pipelines/ui/screens/PipelineLastRunTest.kt @@ -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()) + } } diff --git a/internal/protocol/runevents.go b/internal/protocol/runevents.go index ab1796ff..f3b424fd 100644 --- a/internal/protocol/runevents.go +++ b/internal/protocol/runevents.go @@ -18,9 +18,20 @@ const ( // stage, completed-step count and timestamps, plus at most one changed step. // Every field is optional so an older peer ignores what it does not know and a // client can fold the frame into its cached run. +// +// PipelineID, Name, Title and Version are the pipeline identity and stored title +// a list row shows, so a client that has never held the run can insert its row +// from the frame instead of re-reading the whole list. They repeat on every +// frame (pipeline identity never changes mid-run). type RunEventFrame struct { - RunID int64 `json:"run_id"` - Workspace string `json:"workspace,omitempty"` + RunID int64 `json:"run_id"` + Workspace string `json:"workspace,omitempty"` + // PipelineID, Name, Title and Version identify the run's pipeline and its + // operator-facing title, so a new run's row can be built without a fetch. + PipelineID int64 `json:"pipeline_id,omitempty"` + Name string `json:"name,omitempty"` + Title string `json:"title,omitempty"` + Version int `json:"version,omitempty"` Status string `json:"status,omitempty"` Outcome string `json:"outcome,omitempty"` CurrentStage string `json:"current_stage,omitempty"` diff --git a/internal/protocol/runevents_test.go b/internal/protocol/runevents_test.go index 812d9786..2c9cd480 100644 --- a/internal/protocol/runevents_test.go +++ b/internal/protocol/runevents_test.go @@ -8,10 +8,12 @@ import ( ) // TestRunEventGoldenJSON pins the exact wire shape of the pushed run_event frame -// so a JSON tag change cannot silently alter what a client parses. +// so a JSON tag change cannot silently alter what a client parses. The pipeline +// identity fields let a client build a brand-new run's row from the frame. func TestRunEventGoldenJSON(t *testing.T) { frame := RunEventFrame{ - RunID: 7, Workspace: "/work", Status: RunStatusRunning, CurrentStage: "build", + RunID: 7, Workspace: "/work", PipelineID: 4, Name: "release", Title: "Ship it", Version: 3, + Status: RunStatusRunning, CurrentStage: "build", CompletedSteps: 1, CreatedAt: "2026-01-02T03:04:05Z", Artifacts: 2, Step: &RunStepDelta{Sequence: 2, Stage: "build", Status: "completed", Outcome: "success", Target: SuccessEnd, Artifacts: 2}, } @@ -19,7 +21,7 @@ func TestRunEventGoldenJSON(t *testing.T) { if err != nil { t.Fatal(err) } - want := `{"run_id":7,"workspace":"/work","status":"running","current_stage":"build","completed_steps":1,"created_at":"2026-01-02T03:04:05Z","artifacts":2,"step":{"sequence":2,"stage":"build","status":"completed","outcome":"success","target":"#success","artifacts":2}}` + want := `{"run_id":7,"workspace":"/work","pipeline_id":4,"name":"release","title":"Ship it","version":3,"status":"running","current_stage":"build","completed_steps":1,"created_at":"2026-01-02T03:04:05Z","artifacts":2,"step":{"sequence":2,"stage":"build","status":"completed","outcome":"success","target":"#success","artifacts":2}}` if string(encoded) != want { t.Fatalf("golden mismatch\n got %s\nwant %s", encoded, want) } @@ -32,6 +34,21 @@ func TestRunEventGoldenJSON(t *testing.T) { } } +// TestRunEventOmitsBlankIdentity proves a frame without identity (a run with no +// remembered identity) omits the fields entirely, so an older peer sees exactly +// the previous shape and a client falls back to its resync. +func TestRunEventOmitsBlankIdentity(t *testing.T) { + encoded, err := json.Marshal(RunEventFrame{RunID: 3, Workspace: "/w", Status: RunStatusQueued}) + if err != nil { + t.Fatal(err) + } + for _, absent := range []string{"pipeline_id", "name", "title", "version"} { + if strings.Contains(string(encoded), absent) { + t.Fatalf("blank %q must be omitted: %s", absent, encoded) + } + } +} + // TestRunEventEnvelopeRoundTrip proves the new optional fields survive the shared // Message envelope and that an unused payload is omitted from the wire. func TestRunEventEnvelopeRoundTrip(t *testing.T) { diff --git a/internal/server/run_event_websocket_test.go b/internal/server/run_event_websocket_test.go index 008320b4..b6ba611e 100644 --- a/internal/server/run_event_websocket_test.go +++ b/internal/server/run_event_websocket_test.go @@ -161,6 +161,13 @@ func TestRunEventWebSocketPushesTransitions(t *testing.T) { if stepStarted == nil || stepStarted.RunID != runID || stepStarted.Status != protocol.RunStatusRunning || stepStarted.CurrentStage != "one" || stepStarted.Step.Sequence != 1 || stepStarted.Step.Stage != "one" { t.Fatalf("step-started frame = %+v", stepStarted) } + // Every frame carries the run's pipeline identity and stored title, so a + // client that has never held the run can build its list row from the frame. + for name, frame := range map[string]*protocol.RunEventFrame{"created": created, "step-started": stepStarted} { + if frame.PipelineID != ids[0] || frame.Name == "" || frame.Title == "" || frame.Version != 1 { + t.Fatalf("%s frame identity = %+v, want pipeline %d with name/title and version 1", name, frame, ids[0]) + } + } if stepFinished == nil || stepFinished.RunID != runID || stepFinished.Step.Status != "completed" || stepFinished.Step.Outcome != "success" || stepFinished.Step.Target != protocol.SuccessEnd || stepFinished.CompletedSteps != 1 { t.Fatalf("step-finished frame = %+v", stepFinished) } @@ -195,11 +202,27 @@ func TestRunEventSubscribeSendsSnapshot(t *testing.T) { if first.RunEvent.RunID != run.ID || first.RunEvent.Status != "succeeded" || first.RunEvent.CompletedSteps != 1 || first.RunEvent.FinishedAt == "" { t.Fatalf("snapshot frame = %+v", first.RunEvent) } + // A snapshot for a run the client never saw also carries the identity its row + // needs, so a fresh subscribe renders it without a second read. + if first.RunEvent.PipelineID != ids[0] || first.RunEvent.Name == "" || first.RunEvent.Version != 1 { + t.Fatalf("snapshot identity = %+v, want pipeline %d version 1", first.RunEvent, ids[0]) + } if ack := client.read(); ack.Type != protocol.Subscribe || ack.ID != "snap" { t.Fatalf("subscribe ack = %+v", ack) } } +// TestMergeRunEventFrameKeepsIdentity proves a coalesced frame keeps the run's +// identity from an earlier transition when a later delta omits it, so the merged +// frame a client actually receives still carries what its row needs. +func TestMergeRunEventFrameKeepsIdentity(t *testing.T) { + into := &protocol.RunEventFrame{RunID: 1, PipelineID: 4, Name: "release", Title: "Ship it", Version: 3, Status: protocol.RunStatusQueued} + mergeRunEventFrame(into, &protocol.RunEventFrame{RunID: 1, Status: protocol.RunStatusRunning, CurrentStage: "build"}) + if into.PipelineID != 4 || into.Name != "release" || into.Title != "Ship it" || into.Version != 3 || into.Status != protocol.RunStatusRunning { + t.Fatalf("merged identity lost: %+v", into) + } +} + // assertNoRunEvent reads for d, ignoring responses, and fails if any run_event // frame arrives. func assertNoRunEvent(t *testing.T, client *runEventClient, d time.Duration) { @@ -288,6 +311,49 @@ func TestRunEventWorkspaceIsolation(t *testing.T) { assertNoRunEvent(t, client, 400*time.Millisecond) } +// TestRunEventAllWorkspacesSubscription proves the blank ("any workspace") +// subscribe scope — the one the app now asks for because its runs list is +// server-wide — delivers a run from every workspace, while an explicit +// workspace scope stays isolated (TestRunEventWorkspaceIsolation). It is what +// lets the app see a run executing in a lane worktree, a different workspace +// from the one the operator configured. +func TestRunEventAllWorkspacesSubscription(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + _, handler, ids := eventStore(t, ctx, "one") + handler.runner.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) { + return stage.Result{Outcome: "success", Output: stage.Values{}}, nil + } + first := eventWorkspace(t) + second := eventWorkspace(t) + client := dialRunEvents(t, ctx, handler) + // The blank workspace is the "every workspace" scope; no run_ids narrows it. + client.send(protocol.Message{Type: protocol.Subscribe, ID: "all", Workspace: ""}) + if ack := client.read(); ack.Type != protocol.Subscribe || ack.ID != "all" { + t.Fatalf("subscribe ack = %+v", ack) + } + // Subscribe first, then start a run in each workspace, so both are delivered + // as live deltas rather than only in the snapshot. + client.send(protocol.Message{Type: protocol.RunPipeline, ID: "a", PipelineID: ids[0], Version: 1, RequestKey: "all-ws-a", Workspace: first, Input: map[string]any{"prompt": "test"}}) + client.send(protocol.Message{Type: protocol.RunPipeline, ID: "b", PipelineID: ids[0], Version: 1, RequestKey: "all-ws-b", Workspace: second, Input: map[string]any{"prompt": "test"}}) + + terminals := map[string]*protocol.RunEventFrame{} + deadline := time.Now().Add(8 * time.Second) + for len(terminals) < 2 && time.Now().Before(deadline) { + message := client.read() + if message.Type != protocol.RunEvent || message.RunEvent == nil { + continue + } + frame := message.RunEvent + if frame.Status == "succeeded" || frame.Status == "failed" || frame.Status == "canceled" { + terminals[frame.Workspace] = frame + } + } + if terminals[first] == nil || terminals[second] == nil { + t.Fatalf("blank-scope subscription missed a workspace: got %v", terminals) + } +} + // TestRunEventCoalescingBoundsFrameCount records how many run_event frames a // typical multi-stage run produces and asserts the count stays bounded: rapid // transitions coalesce, and the terminal frame is never dropped. diff --git a/internal/server/runevents.go b/internal/server/runevents.go index e767d4fa..b17ef92c 100644 --- a/internal/server/runevents.go +++ b/internal/server/runevents.go @@ -353,6 +353,20 @@ func mergeRunEventFrame(into, in *protocol.RunEventFrame) { if in.Workspace != "" { into.Workspace = in.Workspace } + // Identity is fixed for a run's life, so the first frame that carries it wins + // and a later delta never blanks it. + if into.PipelineID == 0 { + into.PipelineID = in.PipelineID + } + if into.Name == "" { + into.Name = in.Name + } + if into.Title == "" { + into.Title = in.Title + } + if into.Version == 0 { + into.Version = in.Version + } if in.Status != "" { into.Status = in.Status } @@ -410,6 +424,10 @@ func handleRunEvents(request protocol.Message, events *runEventHub, runs *runner sub.send(protocol.RunEventFrame{ RunID: summary.ID, Workspace: summary.Workspace, + PipelineID: summary.PipelineID, + Name: summary.Name, + Title: summary.Title, + Version: summary.Version, Status: summary.Status, Outcome: summary.Outcome, CurrentStage: summary.CurrentStage, diff --git a/internal/server/runner.go b/internal/server/runner.go index ef464b73..6afe105a 100644 --- a/internal/server/runner.go +++ b/internal/server/runner.go @@ -47,6 +47,13 @@ type runner struct { // events pushes run state deltas to subscribed clients. When nil (tests that // build a runner directly) emits are dropped, so nothing else changes. events *runEventHub + // identMu guards idents: the pipeline identity and stored title (see + // runIdentity) that every emitted frame of a run carries, so a client with no + // cached row renders a brand-new run without re-reading the list. Populated + // where a run is created, decided or canceled; a run with no remembered + // identity keeps blank identity fields and the client resyncs once instead. + identMu sync.Mutex + idents map[int64]runIdentity // exportSession writes a finished stage's session into its own artifacts // directory. It defaults to stage.ExportSession; a failure is best-effort and // never changes the stage's status, outcome, target or result validation. @@ -82,7 +89,7 @@ func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) { return nil, err } root := filepath.Join(s.Directory(), "artifacts") - return &runner{ctx: ctx, store: s, artifactsRoot: root, active: map[int64]activeRun{}, attentionSent: map[int64]map[string]bool{}, exportSession: stage.ExportSession}, nil + return &runner{ctx: ctx, store: s, artifactsRoot: root, active: map[int64]activeRun{}, idents: map[int64]runIdentity{}, attentionSent: map[int64]map[string]bool{}, exportSession: stage.ExportSession}, nil } func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.RunDetail, error) { @@ -252,6 +259,18 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru if err != nil { return nil, err } + // The run row now exists: read it once for its resolved identity (pipeline + // id/name, the store-derived title, the pinned version), so the queued frame + // and every later frame carry what a client needs to render a brand-new run + // without re-reading the list. A failed read only costs that seed: the run is + // still launched below and its frames carry no identity, so the client falls + // back to its one resync. + detail, readErr := r.store.GetRun(r.ctx, id) + if readErr != nil { + detail = nil + } else if detail != nil { + r.rememberRunIdentity(id, identityOfRunSummary(detail.RunSummary)) + } if created { // Emit the created delta before launching: the queued frame reaches // subscribers before the running/step-started transition, so a client @@ -266,12 +285,14 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru r.run(ctx, id, d, workspace, request) }) } - detail, err := r.store.GetRun(r.ctx, id) if detail != nil { _, detail.Executing = r.active[id] r.overlayEffectiveModels(detail) } - return detail, err + if readErr != nil { + return nil, readErr + } + return detail, nil } // launch registers an active run's cancellation seam and workspace, adds it to @@ -406,6 +427,7 @@ func (r *runner) cancel(id int64) error { r.forgetAttention(id) if r.events != nil { if detail, err := r.store.GetRun(r.ctx, id); err == nil && detail != nil { + r.rememberRunIdentity(id, identityOfRunSummary(detail.RunSummary)) r.emitRun(protocol.RunEventFrame{ RunID: id, Workspace: detail.Workspace, @@ -450,6 +472,9 @@ func (r *runner) decide(request protocol.Message) (*protocol.RunDetail, error) { return nil, err } if applied { + // Re-seed the identity so the resumed frames (this one and the resumed + // loop's) carry it even after a restart, when no creation was observed. + r.rememberRunIdentity(detail.ID, identityOfRunSummary(detail.RunSummary)) for i := range detail.Steps { if detail.Steps[i].Sequence == step.Sequence && detail.Steps[i].Stage == step.Stage { detail.Steps[i] = step @@ -518,6 +543,9 @@ func terminalFinish(target, stageKey, outcome, diagnostic string) (status, final // shared loop from the decision's target; no full-table scan and no extra // per-step query are introduced. func (r *runner) resume(ctx context.Context, id int64, d *protocol.RunDetail, workspace string, step protocol.RunStep) { + // A resumed run's frames carry the same identity as its creation, so re-seed + // it here too: resume is also reachable from a store read after a restart. + r.rememberRunIdentity(id, identityOfRunSummary(d.RunSummary)) // The decision may route straight to a terminal, which has no stage to run. if status, outcome, diagnostic, terminal := terminalFinish(step.Target, step.Stage, step.Outcome, step.Diagnostic); terminal { r.finishRunState(id, d.Name, workspace, status, outcome, diagnostic, completedSteps(d.Steps), step.Sequence, step.Stage, stage.Values(step.Output)) @@ -615,6 +643,60 @@ func (s *runState) publishServerChangedFiles() { s.memory["input"] = entry } +// runIdentity is the pipeline identity and stored title a run's pushed frames +// carry (RunEventFrame.PipelineID/Name/Title/Version). It is remembered where a +// run is created, decided or canceled and stamped onto every frame that leaves +// emitRun, so a client that has never seen the run can build its list row from +// the frame without re-reading the whole list. +type runIdentity struct { + pipelineID int64 + name string + title string + version int +} + +// identityOfRunSummary is the one projection of a run row onto the identity its +// pushed frames carry, so the frame and the list row cannot disagree. +func identityOfRunSummary(s protocol.RunSummary) runIdentity { + return runIdentity{pipelineID: s.PipelineID, name: s.Name, title: s.Title, version: s.Version} +} + +// rememberRunIdentity records the identity every later frame of run id carries. +// It is safe from any goroutine and takes its own lock (never r.mu), so it is +// callable from a path that already holds r.mu. +func (r *runner) rememberRunIdentity(id int64, ident runIdentity) { + if r.idents == nil { + return + } + r.identMu.Lock() + r.idents[id] = ident + r.identMu.Unlock() +} + +// stampRunIdentity fills a frame's blank identity fields from the run's +// remembered identity, leaving any the emit site set untouched. A run with no +// remembered identity keeps them blank, so the client falls back to its resync. +func (r *runner) stampRunIdentity(frame *protocol.RunEventFrame) { + r.identMu.Lock() + ident, ok := r.idents[frame.RunID] + r.identMu.Unlock() + if !ok { + return + } + if frame.PipelineID == 0 { + frame.PipelineID = ident.pipelineID + } + if frame.Name == "" { + frame.Name = ident.name + } + if frame.Title == "" { + frame.Title = ident.title + } + if frame.Version == 0 { + frame.Version = ident.version + } +} + // emitRun pushes one run delta to the subscribed-client hub when one is wired, // and does nothing otherwise (a runner built directly in a test has no hub). // terminal marks a final transition, which the hub flushes without coalescing. @@ -622,6 +704,7 @@ func (r *runner) emitRun(frame protocol.RunEventFrame, terminal bool) { if r.events == nil { return } + r.stampRunIdentity(&frame) r.events.emit(frame, terminal) }