Finish cleanup through the normal invocation lifecycle

This commit is contained in:
2026-11-30 13:58:52 +00:00
parent 03c16df524
commit c00c8169e2
4 changed files with 78 additions and 17 deletions

View File

@@ -14,6 +14,7 @@ import (
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
"gitea.maximumdirect.net/eric/narratio/internal/stage"
)
@@ -21,8 +22,8 @@ import (
const cleanupOrchestrationRunID = "20260905T010203Z-a1b2c3d4"
type cleanupPublishEvidenceStage struct {
runs *int
invalidRetention bool
runs *int
blockDeletion bool
}
func (cleanupPublishEvidenceStage) Name() string { return "publish" }
@@ -35,12 +36,15 @@ func (s cleanupPublishEvidenceStage) Run(_ context.Context, env *stage.Env, m *m
if err := os.MkdirAll(workspaceTarget, 0o755); err != nil {
return nil, err
}
if s.invalidRetention {
extract := filepath.Join(workspaceTarget, "extract")
if err := os.MkdirAll(extract, 0755); err != nil {
if s.blockDeletion {
blocked := filepath.Join(workspaceTarget, "locked")
if err := os.MkdirAll(blocked, 0o755); err != nil {
return nil, err
}
if err := os.WriteFile(filepath.Join(extract, "notarius-retention.json"), []byte("invalid"), 0644); err != nil {
if err := os.WriteFile(filepath.Join(blocked, "data"), []byte("keep"), 0o644); err != nil {
return nil, err
}
if err := os.Chmod(blocked, 0o555); err != nil {
return nil, err
}
}
@@ -104,11 +108,14 @@ func TestCleanupStopsAtPublishAndNotifyFailureBoundaries(t *testing.T) {
}
func TestCleanupFailurePreservesPublishAndRetriesWithoutRepublishing(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("filesystem permissions do not constrain root")
}
cfg, remote := cleanupOrchestrationConfig(t)
remote.SeedObject(storage.FakeObject{Key: "sentinel/remote-state", Data: []byte("unchanged")})
publishRuns := 0
plan := BoundedPlan{stages: []stage.Stage{
cleanupPublishEvidenceStage{runs: &publishRuns, invalidRetention: true},
cleanupPublishEvidenceStage{runs: &publishRuns, blockDeletion: true},
resultStage{name: "notify"},
selectedCleanupStage(t),
}}
@@ -134,11 +141,17 @@ func TestCleanupFailurePreservesPublishAndRetriesWithoutRepublishing(t *testing.
}
workspaceTarget := artifacts.SessionRunRootForCampaign(cfg.Pipeline.Workspace.Root, cfg.Session.Campaign, cfg.Session.SessionID, cleanupOrchestrationRunID)
if err := os.Remove(filepath.Join(workspaceTarget, "extract", "notarius-retention.json")); err != nil {
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
if _, err := os.Lstat(filepath.Join(paths.LogsDir, "publish")); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("managed view remained after partial cleanup: %v", err)
}
if err := os.Chmod(filepath.Join(workspaceTarget, "locked"), 0o755); err != nil {
t.Fatal(err)
}
retryDeps := cleanupOrchestrationDependencies()
retryDeps.newRunID = func() (string, error) { return "retry-cleanup", nil }
summary, err := executePlanWithDependencies(
context.Background(), cfg, plan, RunOptions{Env: &Env{ObjectStore: remote}}, cleanupOrchestrationDependencies(),
context.Background(), cfg, plan, RunOptions{Env: &Env{ObjectStore: remote}}, retryDeps,
)
if err != nil {
t.Fatalf("retry cleanup: %v", err)
@@ -149,11 +162,8 @@ func TestCleanupFailurePreservesPublishAndRetriesWithoutRepublishing(t *testing.
if !reflect.DeepEqual(summary.Executed, []string{"cleanup"}) || !reflect.DeepEqual(summary.Skipped, []string{"publish", "notify"}) {
t.Fatalf("retry summary = %#v", summary)
}
if _, statErr := os.Stat(filepath.Join(workspaceTarget, "publish", "logs")); statErr != nil {
t.Fatalf("latest publish diagnostics missing: %v", statErr)
}
if _, statErr := os.Lstat(filepath.Join(workspaceTarget, "extract")); !errors.Is(statErr, os.ErrNotExist) {
t.Fatalf("unprotected extract data remains: %v", statErr)
if _, statErr := os.Lstat(filepath.Dir(workspaceTarget)); !errors.Is(statErr, os.ErrNotExist) {
t.Fatalf("run history remains: %v", statErr)
}
}
@@ -269,3 +279,53 @@ func loadCleanupOrchestrationManifest(t *testing.T, cfg *config.Config) *manifes
}
return loaded
}
func TestCleanupLifecycleReusesCleanStateAndRemovesLaterAttempts(t *testing.T) {
cfg, remote := cleanupOrchestrationConfig(t)
publishRuns := 0
plan := BoundedPlan{stages: []stage.Stage{cleanupPublishEvidenceStage{runs: &publishRuns}, resultStage{name: "notify"}, selectedCleanupStage(t)}}
invoke := func(id string, selected BoundedPlan) *RunSummary {
t.Helper()
deps := cleanupOrchestrationDependencies()
deps.newRunID = func() (string, error) { return id, nil }
summary, err := executePlanWithDependencies(context.Background(), cfg, selected, RunOptions{Env: &Env{ObjectStore: remote}}, deps)
if err != nil || !summary.Complete {
t.Fatalf("invocation %s = %#v, %v", id, summary, err)
}
return summary
}
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
checkClean := func() {
t.Helper()
if _, err := os.Lstat(paths.RunsDir); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("runs directory exists: %v", err)
}
loaded := loadCleanupOrchestrationManifest(t, cfg)
if loaded.Stages["cleanup"] == nil || loaded.Stages["cleanup"].Status != manifest.StatusSucceeded {
t.Fatalf("cleanup record = %#v", loaded.Stages["cleanup"])
}
for _, category := range []string{"logs", "debug"} {
for _, name := range []string{"publish", "notify", "extract"} {
if _, err := os.Lstat(filepath.Join(paths.Root, category, name)); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("managed view exists: %v", err)
}
}
}
}
invoke("first-run", plan)
checkClean()
summary := invoke("reused-run", plan)
if len(summary.Executed) != 0 {
t.Fatalf("reused execution = %#v", summary)
}
checkClean()
if _, err := fileops.BeginAttemptViews(paths.Root, "later-run", "extract"); err != nil {
t.Fatal(err)
}
cleanupOnly := BoundedPlan{stages: []stage.Stage{selectedCleanupStage(t)}}
invoke("cleanup-again", cleanupOnly)
checkClean()
if publishRuns != 1 {
t.Fatalf("publish calls = %d", publishRuns)
}
}

View File

@@ -95,7 +95,6 @@ func (s *LocalStore) ensureLayout(paths SessionPaths) (SessionPaths, error) {
paths.TranscriptsRawDir,
paths.ArtifactsDir,
paths.LogsDir,
paths.RunsDir,
paths.PreviousDir,
paths.PreviousArtifactsDir,
}

View File

@@ -25,7 +25,9 @@ func TestEnsureLayoutCreatesExpectedDirectories(t *testing.T) {
checkDirExists(t, paths.TranscriptsRawDir)
checkDirExists(t, paths.ArtifactsDir)
checkDirExists(t, paths.LogsDir)
checkDirExists(t, paths.RunsDir)
if _, err := os.Lstat(paths.RunsDir); !os.IsNotExist(err) {
t.Fatalf("layout created runs directory: %v", err)
}
checkDirExists(t, paths.PreviousDir)
checkDirExists(t, paths.PreviousArtifactsDir)