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

@@ -196,7 +196,7 @@ owned views, and its reuse decision observes all remaining cleanup work.
## Stage 4: Complete The Invocation And Retry Lifecycle ## Stage 4: Complete The Invocation And Retry Lifecycle
Status: Pending. Status: Complete.
Read `internal/artifacts/local.go`, `local_test.go`, `paths_model_test.go`, Read `internal/artifacts/local.go`, `local_test.go`, `paths_model_test.go`,
`internal/app/runner.go`, `runner_runtime.go`, `runner_stage_execution.go`, `internal/app/runner.go`, `runner_runtime.go`, `runner_stage_execution.go`,

View File

@@ -14,6 +14,7 @@ import (
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage" "gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts" "gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config" "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/manifest"
"gitea.maximumdirect.net/eric/narratio/internal/stage" "gitea.maximumdirect.net/eric/narratio/internal/stage"
) )
@@ -22,7 +23,7 @@ const cleanupOrchestrationRunID = "20260905T010203Z-a1b2c3d4"
type cleanupPublishEvidenceStage struct { type cleanupPublishEvidenceStage struct {
runs *int runs *int
invalidRetention bool blockDeletion bool
} }
func (cleanupPublishEvidenceStage) Name() string { return "publish" } 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 { if err := os.MkdirAll(workspaceTarget, 0o755); err != nil {
return nil, err return nil, err
} }
if s.invalidRetention { if s.blockDeletion {
extract := filepath.Join(workspaceTarget, "extract") blocked := filepath.Join(workspaceTarget, "locked")
if err := os.MkdirAll(extract, 0755); err != nil { if err := os.MkdirAll(blocked, 0o755); err != nil {
return nil, err 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 return nil, err
} }
} }
@@ -104,11 +108,14 @@ func TestCleanupStopsAtPublishAndNotifyFailureBoundaries(t *testing.T) {
} }
func TestCleanupFailurePreservesPublishAndRetriesWithoutRepublishing(t *testing.T) { func TestCleanupFailurePreservesPublishAndRetriesWithoutRepublishing(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("filesystem permissions do not constrain root")
}
cfg, remote := cleanupOrchestrationConfig(t) cfg, remote := cleanupOrchestrationConfig(t)
remote.SeedObject(storage.FakeObject{Key: "sentinel/remote-state", Data: []byte("unchanged")}) remote.SeedObject(storage.FakeObject{Key: "sentinel/remote-state", Data: []byte("unchanged")})
publishRuns := 0 publishRuns := 0
plan := BoundedPlan{stages: []stage.Stage{ plan := BoundedPlan{stages: []stage.Stage{
cleanupPublishEvidenceStage{runs: &publishRuns, invalidRetention: true}, cleanupPublishEvidenceStage{runs: &publishRuns, blockDeletion: true},
resultStage{name: "notify"}, resultStage{name: "notify"},
selectedCleanupStage(t), selectedCleanupStage(t),
}} }}
@@ -134,11 +141,17 @@ func TestCleanupFailurePreservesPublishAndRetriesWithoutRepublishing(t *testing.
} }
workspaceTarget := artifacts.SessionRunRootForCampaign(cfg.Pipeline.Workspace.Root, cfg.Session.Campaign, cfg.Session.SessionID, cleanupOrchestrationRunID) 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) t.Fatal(err)
} }
retryDeps := cleanupOrchestrationDependencies()
retryDeps.newRunID = func() (string, error) { return "retry-cleanup", nil }
summary, err := executePlanWithDependencies( 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 { if err != nil {
t.Fatalf("retry cleanup: %v", err) 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"}) { if !reflect.DeepEqual(summary.Executed, []string{"cleanup"}) || !reflect.DeepEqual(summary.Skipped, []string{"publish", "notify"}) {
t.Fatalf("retry summary = %#v", summary) t.Fatalf("retry summary = %#v", summary)
} }
if _, statErr := os.Stat(filepath.Join(workspaceTarget, "publish", "logs")); statErr != nil { if _, statErr := os.Lstat(filepath.Dir(workspaceTarget)); !errors.Is(statErr, os.ErrNotExist) {
t.Fatalf("latest publish diagnostics missing: %v", statErr) t.Fatalf("run history remains: %v", statErr)
}
if _, statErr := os.Lstat(filepath.Join(workspaceTarget, "extract")); !errors.Is(statErr, os.ErrNotExist) {
t.Fatalf("unprotected extract data remains: %v", statErr)
} }
} }
@@ -269,3 +279,53 @@ func loadCleanupOrchestrationManifest(t *testing.T, cfg *config.Config) *manifes
} }
return loaded 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.TranscriptsRawDir,
paths.ArtifactsDir, paths.ArtifactsDir,
paths.LogsDir, paths.LogsDir,
paths.RunsDir,
paths.PreviousDir, paths.PreviousDir,
paths.PreviousArtifactsDir, paths.PreviousArtifactsDir,
} }

View File

@@ -25,7 +25,9 @@ func TestEnsureLayoutCreatesExpectedDirectories(t *testing.T) {
checkDirExists(t, paths.TranscriptsRawDir) checkDirExists(t, paths.TranscriptsRawDir)
checkDirExists(t, paths.ArtifactsDir) checkDirExists(t, paths.ArtifactsDir)
checkDirExists(t, paths.LogsDir) 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.PreviousDir)
checkDirExists(t, paths.PreviousArtifactsDir) checkDirExists(t, paths.PreviousArtifactsDir)