Push run state to subscribed websocket clients
Add a server-pushed run_event frame so a client no longer has to poll once a second to notice run changes. - protocol: RunEventFrame/RunStepDelta compact delta (run id, workspace, status, outcome, current stage, completed-step count, timestamps and an optional single step delta), plus subscribe/unsubscribe/run_event types and the optional RunEvent/RunIDs message fields. All fields optional so older peers ignore them. - server: runEventHub with per-connection subscriptions scoped to a workspace (and optional explicit run ids), one bounded writer goroutine per subscription, slow-subscriber drop, per-client subscription cap and a ~220ms per-run coalescer that flushes pending deltas before a terminal frame. - runner: emit deltas on created/queued, queued->running and step start, human waiting, step finish (outcome, target, diagnostic, artifacts and completed count), terminal finish, cancel of a waiting run and decide/resume; emitRun is a no-op without a hub. - websocket: subscribe seeds current state before the ack, unsubscribe and connection teardown drop subscriptions, and pushed frames reuse the connection write closure. - tests: protocol golden-JSON wire shape plus websocket coverage for the started-run frame sequence, subscribe current-state snapshot, silent unsubscribed/never-subscribed clients, workspace isolation and a bounded coalescing frame count. - docs: note subscribe/unsubscribe/run_event in docs/cli.md; TUI and Android still poll in this release.
This commit is contained in:
+127
-6
@@ -43,7 +43,10 @@ type runner struct {
|
||||
// verdict generates the bounded one-line reason for a completed stage visit
|
||||
// from its output map. When nil the store derives the reason at read time with
|
||||
// no model call. It is best-effort: any failure must never fail the stage.
|
||||
verdict func(context.Context, map[string]any) string
|
||||
verdict func(context.Context, map[string]any) string
|
||||
// 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
|
||||
wg sync.WaitGroup
|
||||
schedulerWG sync.WaitGroup
|
||||
}
|
||||
@@ -241,6 +244,15 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
|
||||
return nil, err
|
||||
}
|
||||
if created {
|
||||
// Emit the created delta before launching: the queued frame reaches
|
||||
// subscribers before the running/step-started transition, so a client
|
||||
// sees the run appear and then advance.
|
||||
r.emitRun(protocol.RunEventFrame{
|
||||
RunID: id,
|
||||
Workspace: workspace,
|
||||
Status: protocol.RunStatusQueued,
|
||||
CreatedAt: time.Now().UTC().Format(time.RFC3339),
|
||||
}, false)
|
||||
r.launch(id, workspace, func(ctx context.Context) {
|
||||
r.run(ctx, id, d, workspace, request)
|
||||
})
|
||||
@@ -376,7 +388,25 @@ func (r *runner) cancel(id int64) error {
|
||||
}
|
||||
// A run that is waiting on a person has no live goroutine, so cancel it at
|
||||
// the persistence layer instead. This keeps cancel working across a restart.
|
||||
return r.store.CancelWaiting(r.ctx, id)
|
||||
// The single follow-up read seeds the terminal delta; cancel is a cold
|
||||
// operator action, not a per-transition hot path.
|
||||
if err := r.store.CancelWaiting(r.ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if r.events != nil {
|
||||
if detail, err := r.store.GetRun(r.ctx, id); err == nil && detail != nil {
|
||||
r.emitRun(protocol.RunEventFrame{
|
||||
RunID: id,
|
||||
Workspace: detail.Workspace,
|
||||
Status: detail.Status,
|
||||
Outcome: detail.Outcome,
|
||||
CurrentStage: detail.CurrentStage,
|
||||
CompletedSteps: detail.CompletedSteps,
|
||||
FinishedAt: detail.FinishedAt,
|
||||
}, true)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// decide records an operator's choice for a waiting human stage and resumes the
|
||||
@@ -415,6 +445,23 @@ func (r *runner) decide(request protocol.Message) (*protocol.RunDetail, error) {
|
||||
break
|
||||
}
|
||||
}
|
||||
// The decision closes the waiting step and returns the run to running;
|
||||
// report that before the resumed loop emits its own deltas.
|
||||
r.emitRun(protocol.RunEventFrame{
|
||||
RunID: detail.ID,
|
||||
Workspace: detail.Workspace,
|
||||
Status: protocol.RunStatusRunning,
|
||||
CurrentStage: step.Stage,
|
||||
CompletedSteps: completedSteps(detail.Steps),
|
||||
Step: &protocol.RunStepDelta{
|
||||
Sequence: step.Sequence,
|
||||
Stage: step.Stage,
|
||||
Status: step.Status,
|
||||
Outcome: step.Outcome,
|
||||
Target: step.Target,
|
||||
Diagnostic: step.Diagnostic,
|
||||
},
|
||||
}, false)
|
||||
r.launch(detail.ID, detail.Workspace, func(ctx context.Context) {
|
||||
r.resume(ctx, detail.ID, detail, detail.Workspace, step)
|
||||
})
|
||||
@@ -426,7 +473,7 @@ func (r *runner) decide(request protocol.Message) (*protocol.RunDetail, error) {
|
||||
func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail, workspace string, request protocol.Message) {
|
||||
original, copyErr := copyValues(stage.Values(request.Input))
|
||||
if copyErr != nil {
|
||||
r.finishRunState(id, "failed", "error", copyErr.Error(), stage.Values(request.Input))
|
||||
r.finishRunState(id, workspace, "failed", "error", copyErr.Error(), 0, "", stage.Values(request.Input))
|
||||
return
|
||||
}
|
||||
input := stage.Values(request.Input)
|
||||
@@ -462,7 +509,7 @@ func terminalFinish(target, stageKey, outcome, diagnostic string) (status, final
|
||||
func (r *runner) resume(ctx context.Context, id int64, d *protocol.RunDetail, workspace string, step protocol.RunStep) {
|
||||
// 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, status, outcome, diagnostic, stage.Values(step.Output))
|
||||
r.finishRunState(id, workspace, status, outcome, diagnostic, completedSteps(d.Steps), step.Stage, stage.Values(step.Output))
|
||||
return
|
||||
}
|
||||
state := rebuildRunState(d, step)
|
||||
@@ -478,6 +525,9 @@ type runState struct {
|
||||
visits map[string]int
|
||||
memory map[string]memoryValue
|
||||
input stage.Values
|
||||
// completed counts the run's finished steps, so a delta can report the
|
||||
// running total without re-reading the store.
|
||||
completed int
|
||||
}
|
||||
|
||||
func newRunState(key string, input stage.Values) *runState {
|
||||
@@ -498,22 +548,54 @@ func rebuildRunState(detail *protocol.RunDetail, decided protocol.RunStep) *runS
|
||||
if values, err := copyValues(stage.Values(step.Output)); err == nil {
|
||||
state.memory[step.Stage] = memoryValue{output: values, sequence: step.Sequence}
|
||||
}
|
||||
state.completed++
|
||||
}
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
func (r *runner) finishRunState(id int64, status, outcome, diagnostic string, input stage.Values) {
|
||||
// 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.
|
||||
func (r *runner) emitRun(frame protocol.RunEventFrame, terminal bool) {
|
||||
if r.events == nil {
|
||||
return
|
||||
}
|
||||
r.events.emit(frame, terminal)
|
||||
}
|
||||
|
||||
func (r *runner) finishRunState(id int64, workspace, status, outcome, diagnostic string, completed int, currentStage string, input stage.Values) {
|
||||
c, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := r.store.FinishRun(c, id, status, outcome, diagnostic, input); err != nil {
|
||||
log.Printf("finish run %d: %v", id, err)
|
||||
}
|
||||
r.emitRun(protocol.RunEventFrame{
|
||||
RunID: id,
|
||||
Workspace: workspace,
|
||||
Status: status,
|
||||
Outcome: outcome,
|
||||
CurrentStage: currentStage,
|
||||
CompletedSteps: completed,
|
||||
FinishedAt: time.Now().UTC().Format(time.RFC3339),
|
||||
}, true)
|
||||
}
|
||||
|
||||
// completedSteps counts the completed steps of a run detail; resume uses it to
|
||||
// seed the terminal delta without another store read.
|
||||
func completedSteps(steps []protocol.RunStep) int {
|
||||
n := 0
|
||||
for _, step := range steps {
|
||||
if step.Status == "completed" {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, name, workspace string, request protocol.Message, state *runState) {
|
||||
finish := func(status, outcome, diagnostic string) {
|
||||
r.finishRunState(id, status, outcome, diagnostic, state.input)
|
||||
r.finishRunState(id, workspace, status, outcome, diagnostic, state.completed, state.key, state.input)
|
||||
}
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
@@ -604,6 +686,15 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
finish("failed", "error", err.Error())
|
||||
return
|
||||
}
|
||||
// queued -> running: report the new current stage and the step start.
|
||||
r.emitRun(protocol.RunEventFrame{
|
||||
RunID: id,
|
||||
Workspace: workspace,
|
||||
Status: protocol.RunStatusRunning,
|
||||
CurrentStage: key,
|
||||
CompletedSteps: state.completed,
|
||||
Step: &protocol.RunStepDelta{Sequence: sequence, Stage: key, Status: protocol.StepStatusRunning},
|
||||
}, false)
|
||||
if isHuman {
|
||||
// A human step is O(1): one BeginStep plus one MarkWaiting, no
|
||||
// filediff capture, no artifacts directory and no workspace mutation.
|
||||
@@ -617,6 +708,16 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
return
|
||||
}
|
||||
waitStep := protocol.RunStep{Sequence: sequence, Stage: key, Status: protocol.StepStatusWaiting, Output: evidence}
|
||||
// A human step parks the run without a live goroutine; the delta is
|
||||
// coalesced by the hub's timer, which keeps running after this returns.
|
||||
r.emitRun(protocol.RunEventFrame{
|
||||
RunID: id,
|
||||
Workspace: workspace,
|
||||
Status: protocol.RunStatusWaiting,
|
||||
CurrentStage: key,
|
||||
CompletedSteps: state.completed,
|
||||
Step: &protocol.RunStepDelta{Sequence: sequence, Stage: key, Status: protocol.StepStatusWaiting},
|
||||
}, false)
|
||||
r.notifyStep(id, name, workspace, selected.Notify, waitStep)
|
||||
return
|
||||
}
|
||||
@@ -799,7 +900,27 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
return
|
||||
}
|
||||
state.memory[key] = memoryValue{output: values, sequence: sequence}
|
||||
state.completed++
|
||||
}
|
||||
// Step finished: report its outcome, routing target, diagnostic and the
|
||||
// artifacts recorded for the visit, plus the run's running step count.
|
||||
r.emitRun(protocol.RunEventFrame{
|
||||
RunID: id,
|
||||
Workspace: workspace,
|
||||
Status: protocol.RunStatusRunning,
|
||||
CurrentStage: key,
|
||||
CompletedSteps: state.completed,
|
||||
Artifacts: len(step.Artifacts),
|
||||
Step: &protocol.RunStepDelta{
|
||||
Sequence: step.Sequence,
|
||||
Stage: step.Stage,
|
||||
Status: step.Status,
|
||||
Outcome: step.Outcome,
|
||||
Target: step.Target,
|
||||
Diagnostic: step.Diagnostic,
|
||||
Artifacts: len(step.Artifacts),
|
||||
},
|
||||
}, false)
|
||||
r.notifyStep(id, name, workspace, selected.Notify, step)
|
||||
state.input = stage.Values(step.Output)
|
||||
if useAgent || errors.Is(err, stage.ErrStageCleanup) {
|
||||
|
||||
Reference in New Issue
Block a user