server: skip a stage that declares a skipped edge when no UI surface changed

A stage declaring a `skipped` outcome is short-circuited with no model call
when the run's recorded file changes and artifacts touch no UI surface
(android/, internal/tui/, a presentation extension or an image artifact).
The step is recorded completed/skipped along its declared edge with a
bounded one-line reason, so a backend-only run no longer pays for a
ui_reviewer generation that can only answer ui_applicable=false.
This commit is contained in:
2026-10-16 23:31:16 +01:00
parent 0937327e8c
commit b3188a5c6f
3 changed files with 460 additions and 16 deletions
+71 -16
View File
@@ -439,6 +439,13 @@ type runState struct {
visits map[string]int
memory map[string]memoryValue
input stage.Values
// changedPaths retains a bounded sample of the run's recorded changed paths
// and changedCount the full total, so the skip guard reads only in-memory
// evidence. uiSurface is latched once any recorded change touched a UI
// surface. Both are folded in by recordChanges (ui_surface.go).
changedPaths []string
changedCount int
uiSurface bool
}
func newRunState(key string, input stage.Values) *runState {
@@ -460,6 +467,9 @@ func rebuildRunState(detail *protocol.RunDetail, decided protocol.RunStep) *runS
state.memory[step.Stage] = memoryValue{output: values, sequence: step.Sequence}
}
}
// Seed the UI-surface accumulator from the persisted evidence so a
// resumed run reaches the same skip decision with no extra query.
state.recordChanges(step.FileChanges, step.Artifacts)
}
return state
}
@@ -472,6 +482,32 @@ func (r *runner) finishRunState(id int64, status, outcome, diagnostic string, in
}
}
// finishStage persists one step, refreshes the shared memory for a completed
// step, notifies and classifies the step's target. Both the executed path and
// the skipped path use it, so persistence, notification and terminal routing
// cannot drift between them. A persist or memory-copy failure is returned for
// the caller to fail the run; the caller still owns the model-specific
// verdict, quiescence and cancellation handling around it.
func (r *runner) finishStage(id int64, name, workspace string, selected protocol.Stage, state *runState, step protocol.RunStep) (status, outcome, diagnostic string, terminal bool, err error) {
save, cancel := context.WithTimeout(context.Background(), 10*time.Second)
persistErr := r.store.FinishStep(save, id, step)
cancel()
if persistErr != nil {
return "", "", "", false, persistErr
}
if step.Status == "completed" {
values, copyErr := copyValues(stage.Values(step.Output))
if copyErr != nil {
return "", "", "", false, copyErr
}
state.memory[step.Stage] = memoryValue{output: values, sequence: step.Sequence}
}
r.notifyStep(id, name, workspace, selected.Notify, step)
state.input = stage.Values(step.Output)
status, outcome, diagnostic, terminal = terminalFinish(step.Target, step.Stage, step.Outcome, step.Diagnostic)
return status, outcome, diagnostic, terminal, nil
}
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)
@@ -557,6 +593,34 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
r.notifyStep(id, name, workspace, selected.Notify, waitStep)
return
}
// A stage that declares a `skipped` route is short-circuited when the run
// has recorded no UI surface: the stage would answer exactly that, so skip
// the model call and its capture work, record the step as completed and
// route along the declared edge. Only a declared route is honoured, and no
// other stage or outcome routing changes.
if _, declaresSkip := def.Edges["skipped"]; declaresSkip && !state.uiSurface {
reason := skipReasonLine(state)
step := protocol.RunStep{
Sequence: sequence,
Stage: key,
Status: "completed",
Outcome: "skipped",
Output: stage.Values{"summary": reason},
Target: def.Edges["skipped"],
Control: &protocol.ControlEvidence{Visit: state.visits[key], MaxVisits: maxVisits, Memory: reads},
}
status, outcome, diagnostic, terminal, finishErr := r.finishStage(id, name, workspace, selected, state, step)
if finishErr != nil {
finish("failed", "error", finishErr.Error())
return
}
if terminal {
finish(status, outcome, diagnostic)
return
}
state.key = step.Target
continue
}
var tools []string
explicitTools := false
var toolErr error
@@ -715,23 +779,14 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
}
reasonCancel()
}
save, cancel := context.WithTimeout(context.Background(), 10*time.Second)
persistErr := r.store.FinishStep(save, id, step)
cancel()
if persistErr != nil {
finish("failed", "error", persistErr.Error())
status, outcome, diagnostic, terminal, finishErr := r.finishStage(id, name, workspace, selected, state, step)
if finishErr != nil {
finish("failed", "error", finishErr.Error())
return
}
if step.Status == "completed" {
values, err := copyValues(stage.Values(step.Output))
if err != nil {
finish("failed", "error", err.Error())
return
}
state.memory[key] = memoryValue{output: values, sequence: sequence}
}
r.notifyStep(id, name, workspace, selected.Notify, step)
state.input = stage.Values(step.Output)
// Advance the in-memory UI-surface evidence only after the step is
// persisted, so the skip guard agrees with what the store holds.
state.recordChanges(step.FileChanges, step.Artifacts)
if useAgent || errors.Is(err, stage.ErrStageCleanup) {
sessions, checkErr := r.store.ActiveAgentSessionsForRun(context.Background(), id)
if checkErr != nil || len(sessions) > 0 {
@@ -743,7 +798,7 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
finish("canceled", "", ctx.Err().Error())
return
}
if status, outcome, diagnostic, terminal := terminalFinish(step.Target, step.Stage, step.Outcome, step.Diagnostic); terminal {
if terminal {
finish(status, outcome, diagnostic)
return
}