From bc4878f2d72b93e46da8b83fb64bd5f1001a1a60 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Mon, 30 Nov 2026 03:59:19 +0000 Subject: [PATCH] Limit cleanup checks to owned diagnostic views --- docs/roadmap/remediation.md | 7 ++++++- internal/fileops/attempt_views.go | 16 ++-------------- internal/fileops/attempt_views_test.go | 2 +- internal/stage/cleanup_retained_bundle.go | 6 +++++- internal/stage/cleanup_test.go | 14 ++++++++++++++ 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index d843d28..0a05159 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -57,7 +57,12 @@ before promotion, as the existing staging test already requires. **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. diff --git a/internal/fileops/attempt_views.go b/internal/fileops/attempt_views.go index 44815cf..fc140b9 100644 --- a/internal/fileops/attempt_views.go +++ b/internal/fileops/attempt_views.go @@ -140,7 +140,7 @@ func existingManagedView(parent *os.Root, name, category string) (*string, error // ManagedAttemptTargets returns run-relative diagnostic directories selected by // 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 { return nil, fmt.Errorf("run identity: %w", err) } @@ -165,19 +165,7 @@ func ManagedAttemptTargets(sessionRoot, runID string) ([]string, error) { if err != nil { return nil, err } - dir, err := parent.Open(".") - 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() + for _, name := range names { if err := pathsafe.ValidateOpaqueSegment(name); err != nil { _ = parent.Close() return nil, fmt.Errorf("invalid managed %s view %q: %w", category, name, err) diff --git a/internal/fileops/attempt_views_test.go b/internal/fileops/attempt_views_test.go index 0a23764..e435d7d 100644 --- a/internal/fileops/attempt_views_test.go +++ b/internal/fileops/attempt_views_test.go @@ -109,7 +109,7 @@ func TestManagedAttemptTargetsRejectForeignLinks(t *testing.T) { if err := os.Symlink("../../outside", filepath.Join(root, "logs", "polish")); err != nil { 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") } } diff --git a/internal/stage/cleanup_retained_bundle.go b/internal/stage/cleanup_retained_bundle.go index 7d59efd..cadd917 100644 --- a/internal/stage/cleanup_retained_bundle.go +++ b/internal/stage/cleanup_retained_bundle.go @@ -34,7 +34,11 @@ func cleanRunWithRetainedBundle(workspaceRoot, runRoot string, remove bool) (boo return false, fmt.Errorf("invalid run cleanup directory %q", runRoot) } 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 { return false, fmt.Errorf("resolve managed diagnostic targets: %w", err) } diff --git a/internal/stage/cleanup_test.go b/internal/stage/cleanup_test.go index 62d732d..c23aa46 100644 --- a/internal/stage/cleanup_test.go +++ b/internal/stage/cleanup_test.go @@ -49,6 +49,13 @@ func TestCleanupPreservesLatestViewsAcrossRunsAndRemovesOldDiagnostics(t *testin writeStageTestFile(t, kept, "current") writeStageTestFile(t, old, "old") 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 { t.Fatal(err) } @@ -78,6 +85,13 @@ func TestCleanupPreservesLatestViewsAcrossRunsAndRemovesOldDiagnostics(t *testin if _, err := os.Stat(filepath.Join(session.Root, "logs", "merge")); err != nil { 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) {