Files
pipelines/internal/server/ui_surface.go
T
alan b3188a5c6f 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.
2026-10-16 23:31:16 +01:00

110 lines
3.7 KiB
Go

package server
import (
"fmt"
"path"
"path/filepath"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// UI-surface classification is the single source of truth for the deterministic
// pre-check that lets a stage declaring a `skipped` route short-circuit when the
// run changed no rendered surface. The path/extension lists live only here; the
// skip guard, its reason and the resumed-run seed all read them.
// maxRetainedChangedPaths bounds how many recorded changed paths a runState
// retains for the skip reason. The full count is kept separately, so the reason
// stays informative without unbounded memory.
const maxRetainedChangedPaths = 32
// uiSurfaceDirs are the workspace prefixes whose changes are a user-facing
// surface. Matching is on a path boundary, so "androidx/" is not "android/".
var uiSurfaceDirs = []string{"android", "internal/tui"}
// presentationExtensions are the rendered presentation file types a change can
// touch without living under a surface directory.
var presentationExtensions = map[string]bool{
".html": true,
".css": true,
".js": true,
".ts": true,
".jsx": true,
".tsx": true,
}
// renderedArtifactExtensions are the image types a captured artifact may carry
// when its content type was not recorded or sniffed as an image.
var renderedArtifactExtensions = map[string]bool{
".png": true,
".jpg": true,
".jpeg": true,
".gif": true,
".webp": true,
}
// uiSurfacePath reports whether a run-relative, forward-slash workspace path
// changed a user-facing surface: a file under android/ or internal/tui/, or a
// file with a presentation extension. It is O(len(path)) with no I/O.
func uiSurfacePath(p string) bool {
clean := strings.TrimPrefix(path.Clean(filepath.ToSlash(p)), "/")
if clean == "" || clean == "." {
return false
}
for _, dir := range uiSurfaceDirs {
if clean == dir || strings.HasPrefix(clean, dir+"/") {
return true
}
}
return presentationExtensions[strings.ToLower(path.Ext(clean))]
}
// uiSurfaceArtifact reports whether a captured artifact is a rendered surface:
// an image content type, or an image file extension when no type was recorded.
func uiSurfaceArtifact(a protocol.StageArtifact) bool {
if strings.HasPrefix(strings.ToLower(a.ContentType), "image/") {
return true
}
return renderedArtifactExtensions[strings.ToLower(path.Ext(a.Name))]
}
// recordChanges folds one completed step's observed changes into the run's
// UI-surface decision. It retains a bounded sample of paths; the count is
// unbounded so the reason can state the true total. It is O(changes) with no I/O.
func (s *runState) recordChanges(changes *protocol.StageFileChanges, artifacts []protocol.StageArtifact) {
if changes != nil {
for _, file := range changes.Files {
s.changedCount++
if len(s.changedPaths) < maxRetainedChangedPaths {
s.changedPaths = append(s.changedPaths, file.Path)
}
if !s.uiSurface && uiSurfacePath(file.Path) {
s.uiSurface = true
}
}
}
for _, artifact := range artifacts {
if !s.uiSurface && uiSurfaceArtifact(artifact) {
s.uiSurface = true
}
}
}
// skipReasonLine states, as one bounded line, why the guard skipped a stage: no
// recorded change touched a UI surface. It names the retained checked paths.
func skipReasonLine(state *runState) string {
if state.changedCount == 0 {
return "no UI surface changed (no recorded file changes)"
}
line := fmt.Sprintf("no UI surface changed in %d recorded file change(s): %s", state.changedCount, strings.Join(state.changedPaths, ", "))
if extra := state.changedCount - len(state.changedPaths); extra > 0 {
line = fmt.Sprintf("%s (+%d more)", line, extra)
}
runes := []rune(line)
if len(runes) > protocol.VerdictReasonCap {
return string(runes[:protocol.VerdictReasonCap])
}
return line
}