Files
pipelines/internal/filediff/policy.go
T
alan 87016ef5dc Engine: check stages, harness-agnostic delivery, read-only enforcement, complete captures
- New `type=check` stage: the server runs a `commands` block (sh -c, per
  command_timeout, workspace env) and routes on pass/fail; per-command status,
  exit code, duration and bounded output are recorded and the failing output is
  the handoff. With delivery=true it is the landing gate (commit leftovers,
  merge moved main, checks in a fresh candidate worktree, fast-forward main).
  Parser/registry, lint (silent guards), format docs, CLI inspect, app graph and
  run detail rendering.
- delivery=true is accepted on every harness (and on check stages).
- permissions.shell on claude stages maps to Bash(cmd) / Bash(prefix:*) rules
  instead of a bare Bash allow; stages without edit that change the workspace
  fail the visit with the changed files listed (allow_workspace_changes=true
  opts out; artifacts, public and harness state dirs are ignored).
- File-change capture: symlinks compared by target, untouched protected files no
  longer make captures partial, untracked caches/dependency/build output left
  out, lockfiles listed without patch, huge files compared by metadata, raised
  patch/line limits. Outcome repair follow-ups are logged and counted on the
  step (control.outcome_repairs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:52:16 +00:00

118 lines
4.4 KiB
Go

package filediff
import (
"crypto/sha256"
"fmt"
"io/fs"
"path"
"strings"
)
// Policy no-patch reasons: the change is listed, completely, without a patch
// by design rather than because a capture limit ran out, so it never makes a
// capture partial.
const (
reasonSymlink = "symlink" // a symbolic link; its target is compared
reasonLarge = "large" // over the hash bound; compared by size, mtime and mode
reasonGenerated = "generated" // a lockfile, minified bundle or source map
)
// cacheDirs are tool caches, virtual environments and dependency trees. Their
// untracked files are left out of the inventory entirely (never "skipped"):
// they are regenerated by tools, not authored by a stage. Tracked files below
// them are still compared, without a patch.
var cacheDirs = map[string]bool{
"node_modules": true, "bower_components": true, ".git": true,
"__pycache__": true, ".venv": true, "venv": true, ".tox": true, ".nox": true,
".mypy_cache": true, ".pytest_cache": true, ".ruff_cache": true, ".hypothesis": true, ".eggs": true,
".next": true, ".nuxt": true, ".svelte-kit": true, ".turbo": true, ".parcel-cache": true, ".pnpm-store": true,
".dart_tool": true, ".gradle": true, ".terraform": true,
}
// buildDirs are conventional build output directories. Untracked files below
// one are left out only when Git tracks nothing there, so a new source file
// in a real `build/` source directory is still captured.
var buildDirs = map[string]bool{"build": true, "dist": true, "out": true, "target": true, "coverage": true}
// generatedNames are lockfiles: tool output whose diff is noise.
var generatedNames = map[string]bool{
"package-lock.json": true, "npm-shrinkwrap.json": true, "yarn.lock": true, "pnpm-lock.yaml": true,
"bun.lockb": true, "cargo.lock": true, "go.sum": true, "poetry.lock": true, "uv.lock": true,
"pipfile.lock": true, "gemfile.lock": true, "composer.lock": true, "pubspec.lock": true,
"flake.lock": true, "mix.lock": true, "podfile.lock": true, "packages.lock.json": true,
}
// inCacheDir reports whether any directory component of name is a cache dir.
func inCacheDir(name string) bool {
parts := strings.Split(name, "/")
for _, part := range parts[:len(parts)-1] {
if cacheDirs[part] {
return true
}
}
return false
}
// generatedFile reports a file whose diff is tool output: a lockfile, a
// minified bundle, a source map, compiled Python, or anything below a cache
// directory.
func generatedFile(name string) bool {
base := strings.ToLower(path.Base(name))
if generatedNames[base] || strings.HasSuffix(base, ".min.js") || strings.HasSuffix(base, ".min.css") ||
strings.HasSuffix(base, ".map") || strings.HasSuffix(base, ".pyc") {
return true
}
return inCacheDir(name)
}
// excludedUntracked reports an untracked path that is tool output rather than
// stage work: below a cache directory, compiled Python, or below a build
// directory where Git tracks nothing (trackedDirs holds every directory that
// contains a tracked file).
func excludedUntracked(name string, trackedDirs map[string]bool) bool {
if inCacheDir(name) || strings.HasSuffix(name, ".pyc") {
return true
}
parts := strings.Split(name, "/")
for i, part := range parts[:len(parts)-1] {
if buildDirs[part] && !trackedDirs[strings.Join(parts[:i+1], "/")] {
return true
}
}
return false
}
// trackedDirectories returns every ancestor directory of the tracked names.
func trackedDirectories(names []string) map[string]bool {
dirs := map[string]bool{}
for _, name := range names {
for dir := path.Dir(name); dir != "." && dir != "/" && !dirs[dir]; dir = path.Dir(dir) {
dirs[dir] = true
}
}
return dirs
}
// policyReason is the policy no-patch reason of a changed path, preferring
// the stage-end side (a file that became a symlink is a symlink change).
func policyReason(a file, aOK bool, b file, bOK bool) string {
if bOK && b.reason != "" {
return b.reason
}
if aOK && a.reason != "" {
return a.reason
}
return ""
}
// metaHash fingerprints a file by its metadata alone, without reading it: a
// protected (secret) file is never opened, and a huge file is not hashed.
func metaHash(info fs.FileInfo) [32]byte {
return sha256.Sum256(fmt.Appendf(nil, "meta\x00%d\x00%d\x00%o", info.Size(), info.ModTime().UnixNano(), info.Mode()))
}
// linkHash fingerprints a symbolic link by its target.
func linkHash(target string) [32]byte {
return sha256.Sum256([]byte("symlink\x00" + target))
}