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
@@ -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.
+18
View File
@@ -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,
+86 -3
View File
@@ -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)
}