Skip fallback needs complete, empty evidence; capture lists over-budget changes

The server skip fallback for stages with a skipped edge now applies only
when every earlier visit recorded a complete file-change capture with no
changed files and no image artifact. Any change (no repo-specific UI path
or extension list), any partial/unavailable/missing capture, a human
decision or a checkpoint-retry prefix runs the model. auto_skip=false opts
a stage out. The fallback verdict names the server fallback.

Capture compares over-budget text by hash and, in Git, lists paths it
could not compare when their git status changes, marked no_patch, so a
large workspace no longer records files: [] for a real edit. Run detail
gains a warnings list for partial/unavailable captures, also printed by
inspect on stderr.
This commit is contained in:
2026-11-04 12:56:40 +00:00
parent fa6c0864db
commit f16d61cdfa
14 changed files with 660 additions and 188 deletions
+154 -72
View File
@@ -2,12 +2,15 @@ package server
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
"git.a-wan.net/alan/pipelines/internal/filediff"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
@@ -30,6 +33,12 @@ func uiReviewerSource(withSkip bool) string {
// then returns the persisted detail, the visited keys and the number of verdict
// (model) calls the runner made.
func runUISkip(t *testing.T, source string, files map[string]string) (*protocol.RunDetail, []string, int) {
t.Helper()
return runUISkipIn(t, source, files, t.TempDir())
}
// runUISkipIn is runUISkip against a prepared workspace directory.
func runUISkipIn(t *testing.T, source string, files map[string]string, dir string) (*protocol.RunDetail, []string, int) {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@@ -50,7 +59,6 @@ func runUISkip(t *testing.T, source string, files map[string]string) (*protocol.
t.Fatal(err)
}
defer r.wg.Wait()
dir := t.TempDir()
var mu sync.Mutex
var visited []string
verdictCalls := 0
@@ -106,37 +114,28 @@ func executed(visited []string, key string) bool {
return false
}
// TestUISkipShortCircuitsWhenNoSurfaceChanged proves a non-UI change skips the
// ui_reviewer stage with no model call and routes along its declared skip edge.
func TestUISkipShortCircuitsWhenNoSurfaceChanged(t *testing.T) {
detail, visited, verdictCalls := runUISkip(t, uiReviewerSource(true), map[string]string{"implement": "internal/server/fake.go"})
if executed(visited, "ui_reviewer") {
t.Fatalf("ui_reviewer executed a model call; visited=%v", visited)
// assertModelRan fails unless ui_reviewer executed its model and routed through
// its own outcome rather than the server fallback.
func assertModelRan(t *testing.T, detail *protocol.RunDetail, visited []string, verdictCalls int) {
t.Helper()
if !executed(visited, "ui_reviewer") {
t.Fatalf("ui_reviewer was auto-skipped; visited=%v steps=%+v", visited, detail.Steps)
}
if verdictCalls != 1 {
t.Fatalf("verdict (model) calls = %d, want only implement's 1", verdictCalls)
if verdictCalls != 2 {
t.Fatalf("verdict (model) calls = %d, want 2 for both executed stages", verdictCalls)
}
if detail.Status != "succeeded" {
t.Fatalf("run status = %q, want succeeded: %+v", detail.Status, detail.RunSummary)
}
if len(detail.Steps) != 2 {
t.Fatalf("steps = %d, want 2: %+v", len(detail.Steps), detail.Steps)
}
step := detail.Steps[1]
if step.Stage != "ui_reviewer" || step.Status != "completed" || step.Outcome != "skipped" || step.Target != protocol.SuccessEnd {
t.Fatalf("skip step wrong: %+v", step)
}
if step.FileChanges != nil && len(step.FileChanges.Files) != 0 {
t.Fatalf("skip step recorded file changes: %+v", step.FileChanges)
}
reason, _ := step.Output["summary"].(string)
if !strings.Contains(reason, "internal/server/fake.go") {
t.Fatalf("skip reason does not name the checked path: %q", reason)
step := detail.Steps[len(detail.Steps)-1]
if step.Stage != "ui_reviewer" || step.Outcome != "approved" || step.Target != protocol.SuccessEnd {
t.Fatalf("executed step did not route through approved: %+v", step)
}
}
// TestUISkipZeroChangesStillSkips proves the guard also short-circuits when the
// run recorded no file changes at all, with a reason that says so.
// TestUISkipZeroChangesStillSkips proves the fallback short-circuits when every
// visit recorded a complete capture with no file changes, and that the verdict
// says it was a server fallback with no model run.
func TestUISkipZeroChangesStillSkips(t *testing.T) {
detail, visited, verdictCalls := runUISkip(t, uiReviewerSource(true), nil)
if executed(visited, "ui_reviewer") {
@@ -148,48 +147,127 @@ func TestUISkipZeroChangesStillSkips(t *testing.T) {
if detail.Status != "succeeded" || len(detail.Steps) != 2 {
t.Fatalf("run = %q steps=%d, want succeeded/2", detail.Status, len(detail.Steps))
}
if first := detail.Steps[0].FileChanges; first == nil || first.Status != "complete" {
t.Fatalf("implement capture = %+v, want complete", first)
}
step := detail.Steps[1]
if step.Outcome != "skipped" {
t.Fatalf("step outcome = %q, want skipped: %+v", step.Outcome, step)
if step.Stage != "ui_reviewer" || step.Status != "completed" || step.Outcome != "skipped" || step.Target != protocol.SuccessEnd {
t.Fatalf("skip step wrong: %+v", step)
}
if step.FileChanges != nil {
t.Fatalf("skip step recorded a capture: %+v", step.FileChanges)
}
if step.Verdict == nil || step.Verdict.ReasonSource != protocol.VerdictReasonFallback || !strings.HasPrefix(step.Verdict.Reason, serverSkipReasonPrefix) {
t.Fatalf("skip verdict does not name the server fallback: %+v", step.Verdict)
}
reason, _ := step.Output["summary"].(string)
if !strings.Contains(reason, "no recorded file changes") {
if !strings.Contains(reason, "recorded no file changes") {
t.Fatalf("zero-change reason does not state that: %q", reason)
}
if len(detail.Warnings) != 0 {
t.Fatalf("complete run carries warnings: %v", detail.Warnings)
}
}
// TestUISkipRunsStageOnSurfaceChange proves a run that touched a UI surface
// still executes the stage and routes through its normal outcome.
func TestUISkipRunsStageOnSurfaceChange(t *testing.T) {
// TestUISkipRunsModelOnAnyRecordedChange proves no path is classified as "not
// UI": a backend file, a Flutter .dart file, a Kotlin file, a Go TUI file and a
// web .tsx file all make the stage run its model. The fallback never encodes a
// repository layout.
func TestUISkipRunsModelOnAnyRecordedChange(t *testing.T) {
cases := map[string]string{
"android": "android/app/src/main/kotlin/Main.kt",
"internal tui": "internal/tui/view.go",
"presentation": "web/app.tsx",
"go backend": "internal/server/fake.go",
"flutter dart": "app/lib/features/home/home_page.dart",
"kotlin": "android/app/src/main/kotlin/Main.kt",
"go tui": "internal/tui/view.go",
"web tsx": "web/app.tsx",
"docs": "docs/readme.md",
}
for name, path := range cases {
t.Run(name, func(t *testing.T) {
detail, visited, verdictCalls := runUISkip(t, uiReviewerSource(true), map[string]string{"implement": path})
if !executed(visited, "ui_reviewer") {
t.Fatalf("ui_reviewer did not execute on %s; visited=%v", path, visited)
}
if verdictCalls != 2 {
t.Fatalf("verdict (model) calls = %d, want 2 for both executed stages", verdictCalls)
}
if detail.Status != "succeeded" {
t.Fatalf("run status = %q, want succeeded: %+v", detail.Status, detail.RunSummary)
}
step := detail.Steps[len(detail.Steps)-1]
if step.Stage != "ui_reviewer" || step.Outcome != "approved" || step.Target != protocol.SuccessEnd {
t.Fatalf("executed step did not route through approved: %+v", step)
}
assertModelRan(t, detail, visited, verdictCalls)
})
}
}
// TestUISkipPartialCaptureNeverSkips proves a visit whose capture was partial
// (here a protected .env file forces skipped>0) blocks the fallback even when
// it recorded no file changes, and that inspect carries a run-level warning.
func TestUISkipPartialCaptureNeverSkips(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, ".env"), []byte("A=1\n"), 0o600); err != nil {
t.Fatal(err)
}
detail, visited, verdictCalls := runUISkipIn(t, uiReviewerSource(true), nil, dir)
if first := detail.Steps[0].FileChanges; first == nil || first.Status != "partial" || len(first.Files) != 0 {
t.Fatalf("fixture capture = %+v, want partial with no files", first)
}
assertModelRan(t, detail, visited, verdictCalls)
if len(detail.Warnings) != 1 || !strings.Contains(detail.Warnings[0], "implement #1 (partial)") {
t.Fatalf("partial capture warning missing: %v", detail.Warnings)
}
}
// TestUISkipOverBudgetGitWorkspaceRunsModel is the run-236 regression: in a Git
// workspace whose text exceeds the snapshot budget, a stage edits one .tsx file
// sorted after the budget is exhausted. The capture must list that file
// (without a patch) and the next stage, which declares `skipped`, must run its
// model rather than be auto-routed with "no recorded file changes".
func TestUISkipOverBudgetGitWorkspaceRunsModel(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git unavailable")
}
restore := filediff.SetSnapshotBudgetForTesting(256)
defer restore()
dir := t.TempDir()
git := func(args ...string) {
t.Helper()
cmd := exec.Command("git", append([]string{"-C", dir, "-c", "user.email=t@example.invalid", "-c", "user.name=t", "-c", "commit.gpgsign=false"}, args...)...)
if data, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("%v: %v %s", args, err, data)
}
}
git("init", "-q")
target := "web/src/pages/inbox/PortfolioIntelligenceInbox.test.tsx"
if err := os.MkdirAll(filepath.Join(dir, filepath.Dir(target)), 0o700); err != nil {
t.Fatal(err)
}
for i := 0; i < 40; i++ {
if err := os.WriteFile(filepath.Join(dir, fmt.Sprintf("a-%02d.txt", i)), []byte(strings.Repeat("filler line\n", 4)), 0o600); err != nil {
t.Fatal(err)
}
}
if err := os.WriteFile(filepath.Join(dir, filepath.FromSlash(target)), []byte(strings.Repeat("original test line\n", 8)), 0o600); err != nil {
t.Fatal(err)
}
git("add", ".")
git("commit", "-qm", "fixture")
detail, visited, verdictCalls := runUISkipIn(t, uiReviewerSource(true), map[string]string{"implement": target}, dir)
capture := detail.Steps[0].FileChanges
if capture == nil || len(capture.Files) != 1 || capture.Files[0].Path != target || !capture.Files[0].NoPatch {
t.Fatalf("over-budget capture did not list the edited file: %+v", capture)
}
assertModelRan(t, detail, visited, verdictCalls)
if len(detail.Warnings) == 0 {
t.Fatal("partial capture produced no run-level warning")
}
}
// TestUISkipOptOut proves auto_skip=false makes a `skipped` stage run its model
// even on complete, empty evidence.
func TestUISkipOptOut(t *testing.T) {
source := uiReviewerSource(true)
at := strings.LastIndex(source, "timeout=1h\n")
source = source[:at] + "timeout=1h\nauto_skip=false\n" + source[at+len("timeout=1h\n"):]
detail, visited, verdictCalls := runUISkip(t, source, nil)
assertModelRan(t, detail, visited, verdictCalls)
}
// TestUISkipNeedsDeclaredEdge proves the guard never invents a skip for a stage
// whose definition declares no `skipped` route: it always executes.
// whose definition declares no `skipped` route: it always executes, even on
// complete, empty evidence.
func TestUISkipNeedsDeclaredEdge(t *testing.T) {
detail, visited, verdictCalls := runUISkip(t, uiReviewerSource(false), map[string]string{"implement": "internal/server/fake.go"})
detail, visited, verdictCalls := runUISkip(t, uiReviewerSource(false), nil)
if !executed(visited, "ui_reviewer") {
t.Fatalf("ui_reviewer without a skipped edge did not execute; visited=%v", visited)
}
@@ -205,33 +283,37 @@ func TestUISkipNeedsDeclaredEdge(t *testing.T) {
}
}
func TestUISurfacePathClassification(t *testing.T) {
cases := []struct {
path string
want bool
}{
{"android/app/src/main/kotlin/Main.kt", true},
{"android/README.md", true},
{"internal/tui/view.go", true},
{"internal/tui", true},
{"web/index.html", true},
{"web/app.css", true},
{"web/app.js", true},
{"web/app.ts", true},
{"web/app.jsx", true},
{"web/app.tsx", true},
{"web/Index.HTML", true},
{"internal/server/fake.go", false},
{"androidx/foo.go", false},
{"cmd/pipelines/main.go", false},
{"docs/readme.md", false},
{"README.md", false},
// TestUISkipPersistedEvidence proves the resume/retry seed: a stored server
// fallback step neither adds nor breaks evidence, while a stored visit with no
// capture (a human decision, a crash, a historical row) or a partial one makes
// the evidence incomplete.
func TestUISkipPersistedEvidence(t *testing.T) {
complete := &protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{}}
reason := skipReasonLine()
serverSkip := protocol.RunStep{Stage: "ui", Outcome: "skipped", Verdict: &protocol.StageVerdict{Reason: reason, ReasonSource: protocol.VerdictReasonFallback}}
s := &runState{}
s.recordPersistedStep(protocol.RunStep{Stage: "implement", FileChanges: complete})
s.recordPersistedStep(serverSkip)
if !autoSkipEligible(nil, s) {
t.Fatalf("complete evidence plus a server skip is not eligible: %+v", s)
}
for _, tc := range cases {
if got := uiSurfacePath(tc.path); got != tc.want {
t.Errorf("uiSurfacePath(%q) = %v, want %v", tc.path, got, tc.want)
legacy := serverSkip
legacy.Verdict = &protocol.StageVerdict{Reason: "no UI surface changed (no recorded file changes)", ReasonSource: protocol.VerdictReasonFallback}
for name, step := range map[string]protocol.RunStep{
"no capture": {Stage: "gate", Outcome: "approve"},
"partial": {Stage: "implement", FileChanges: &protocol.StageFileChanges{Status: "partial", Files: []protocol.FileChange{}}},
"unavailable": {Stage: "implement", FileChanges: &protocol.StageFileChanges{Status: "unavailable"}},
"legacy skip row": legacy,
} {
s := &runState{}
s.recordPersistedStep(step)
if autoSkipEligible(nil, s) {
t.Errorf("%s: incomplete evidence is eligible for the fallback", name)
}
}
if autoSkipEligible(map[string]string{"auto_skip": "false"}, &runState{}) {
t.Error("auto_skip=false did not opt out")
}
}
func TestUISurfaceArtifactClassification(t *testing.T) {