Limit cleanup checks to owned diagnostic views

This commit is contained in:
2026-11-30 03:59:19 +00:00
parent b6492ab4e8
commit bc4878f2d7
5 changed files with 28 additions and 17 deletions

View File

@@ -57,7 +57,12 @@ before promotion, as the existing staging test already requires.
**Priority:** P2 **Priority:** P2
**Status:** Pending **Status:** Complete
Cleanup inspects only canonical stage and restore views. Unrelated files,
directories, and links remain untouched; conflicts at owned views still fail.
Verified cleanup/resume/rotation and file-operation tests with
`go test ./internal/fileops ./internal/stage`.
**Stages:** 15 and 20; requirement-strengthening review in 21–23. **Stages:** 15 and 20; requirement-strengthening review in 21–23.

View File

@@ -140,7 +140,7 @@ func existingManagedView(parent *os.Root, name, category string) (*string, error
// ManagedAttemptTargets returns run-relative diagnostic directories selected by // ManagedAttemptTargets returns run-relative diagnostic directories selected by
// the session's managed navigation links. It never follows link targets. // the session's managed navigation links. It never follows link targets.
func ManagedAttemptTargets(sessionRoot, runID string) ([]string, error) { func ManagedAttemptTargets(sessionRoot, runID string, names []string) ([]string, error) {
if err := pathsafe.ValidateOpaqueSegment(runID); err != nil { if err := pathsafe.ValidateOpaqueSegment(runID); err != nil {
return nil, fmt.Errorf("run identity: %w", err) return nil, fmt.Errorf("run identity: %w", err)
} }
@@ -165,19 +165,7 @@ func ManagedAttemptTargets(sessionRoot, runID string) ([]string, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
dir, err := parent.Open(".") for _, name := range names {
if err != nil {
_ = parent.Close()
return nil, err
}
entries, err := dir.ReadDir(-1)
_ = dir.Close()
if err != nil {
_ = parent.Close()
return nil, err
}
for _, entry := range entries {
name := entry.Name()
if err := pathsafe.ValidateOpaqueSegment(name); err != nil { if err := pathsafe.ValidateOpaqueSegment(name); err != nil {
_ = parent.Close() _ = parent.Close()
return nil, fmt.Errorf("invalid managed %s view %q: %w", category, name, err) return nil, fmt.Errorf("invalid managed %s view %q: %w", category, name, err)

View File

@@ -109,7 +109,7 @@ func TestManagedAttemptTargetsRejectForeignLinks(t *testing.T) {
if err := os.Symlink("../../outside", filepath.Join(root, "logs", "polish")); err != nil { if err := os.Symlink("../../outside", filepath.Join(root, "logs", "polish")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if _, err := ManagedAttemptTargets(root, "run-one"); err == nil { if _, err := ManagedAttemptTargets(root, "run-one", []string{"polish"}); err == nil {
t.Fatal("accepted foreign diagnostic target") t.Fatal("accepted foreign diagnostic target")
} }
} }

View File

@@ -34,7 +34,11 @@ func cleanRunWithRetainedBundle(workspaceRoot, runRoot string, remove bool) (boo
return false, fmt.Errorf("invalid run cleanup directory %q", runRoot) return false, fmt.Errorf("invalid run cleanup directory %q", runRoot)
} }
sessionRoot := filepath.Dir(runsDir) sessionRoot := filepath.Dir(runsDir)
targets, err := fileops.ManagedAttemptTargets(sessionRoot, filepath.Base(runRoot)) names := []string{"restore"}
for _, stage := range All() {
names = append(names, stage.Name())
}
targets, err := fileops.ManagedAttemptTargets(sessionRoot, filepath.Base(runRoot), names)
if err != nil { if err != nil {
return false, fmt.Errorf("resolve managed diagnostic targets: %w", err) return false, fmt.Errorf("resolve managed diagnostic targets: %w", err)
} }

View File

@@ -49,6 +49,13 @@ func TestCleanupPreservesLatestViewsAcrossRunsAndRemovesOldDiagnostics(t *testin
writeStageTestFile(t, kept, "current") writeStageTestFile(t, kept, "current")
writeStageTestFile(t, old, "old") writeStageTestFile(t, old, "old")
writeStageTestFile(t, filepath.Join(root, "polish", "outputs", "work.json"), "work") writeStageTestFile(t, filepath.Join(root, "polish", "outputs", "work.json"), "work")
for _, category := range []string{"logs", "debug"} {
writeStageTestFile(t, filepath.Join(session.Root, category, "README.txt"), "operator notes")
writeStageTestFile(t, filepath.Join(session.Root, category, "notes", "keep.txt"), "keep")
if err := os.Symlink("../../outside", filepath.Join(session.Root, category, "operator-link")); err != nil {
t.Fatal(err)
}
}
if _, err := (cleanupStage{}).Run(context.Background(), env, m); err != nil { if _, err := (cleanupStage{}).Run(context.Background(), env, m); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -78,6 +85,13 @@ func TestCleanupPreservesLatestViewsAcrossRunsAndRemovesOldDiagnostics(t *testin
if _, err := os.Stat(filepath.Join(session.Root, "logs", "merge")); err != nil { if _, err := os.Stat(filepath.Join(session.Root, "logs", "merge")); err != nil {
t.Fatalf("newer merge view: %v", err) t.Fatalf("newer merge view: %v", err)
} }
for _, category := range []string{"logs", "debug"} {
for _, name := range []string{"README.txt", "notes/keep.txt", "operator-link"} {
if _, err := os.Lstat(filepath.Join(session.Root, category, name)); err != nil {
t.Fatalf("unrelated content removed: %s/%s: %v", category, name, err)
}
}
}
} }
func TestCleanupStageRemovesOnlySelectedPublishedRunTarget(t *testing.T) { func TestCleanupStageRemovesOnlySelectedPublishedRunTarget(t *testing.T) {