2026-09-05 15:15:28 +00:00
|
|
|
package stage
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-11-30 13:56:56 +00:00
|
|
|
"errors"
|
2026-09-05 15:15:28 +00:00
|
|
|
"fmt"
|
2026-11-30 13:56:56 +00:00
|
|
|
"os"
|
2026-09-05 15:15:28 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-11-30 13:56:56 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
2026-09-05 15:15:28 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
cleanupSkipReason = "cleanup policies are disabled"
|
2026-11-30 13:56:56 +00:00
|
|
|
cleanupSemanticConfigVersion = 2
|
2026-09-05 15:15:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type cleanupStage struct{}
|
|
|
|
|
|
|
|
|
|
type cleanupSemanticConfig struct {
|
2026-11-30 01:41:12 +00:00
|
|
|
DeleteWorkspace bool `json:"delete_workspace"`
|
|
|
|
|
WorkspaceRoot string `json:"workspace_root"`
|
2026-09-05 15:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cleanupStage) Name() string { return "cleanup" }
|
|
|
|
|
|
2026-09-05 16:34:59 +00:00
|
|
|
func (cleanupStage) RequiresRunWorkspace() bool { return false }
|
|
|
|
|
|
2026-09-05 15:15:28 +00:00
|
|
|
func (cleanupStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*StageResult, error) {
|
|
|
|
|
plan, err := buildCleanupPlan(env, m)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if !plan.enabled {
|
|
|
|
|
return &StageResult{
|
|
|
|
|
Disposition: StageDispositionSkipped,
|
|
|
|
|
SkipReason: cleanupSkipReason,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
completedPolicies := make([]string, 0, len(plan.targets))
|
|
|
|
|
for _, target := range plan.targets {
|
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("cleanup canceled before %s: %w", target.policy, err)
|
|
|
|
|
}
|
2026-11-30 13:56:56 +00:00
|
|
|
_, err := inspectCleanupTarget(target.root, target.target)
|
|
|
|
|
if err == nil {
|
|
|
|
|
err = fileops.RemoveManagedAttemptViews(filepath.Dir(target.target), cleanupManagedNames())
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
err = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
|
|
|
|
if err = ctx.Err(); err == nil {
|
|
|
|
|
err = fileops.RemoveAllUnderRoot(target.root, target.target)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-30 01:02:01 +00:00
|
|
|
if err != nil {
|
2026-09-05 15:15:28 +00:00
|
|
|
return nil, fmt.Errorf("apply %s: %w", target.policy, err)
|
|
|
|
|
}
|
|
|
|
|
completedPolicies = append(completedPolicies, target.policy)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &StageResult{Metadata: map[string]any{
|
|
|
|
|
"stage": "cleanup",
|
|
|
|
|
"published_run_id": plan.publishedRunID,
|
|
|
|
|
"completed_policies": completedPolicies,
|
|
|
|
|
}}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cleanupStage) SemanticConfigFingerprint(env *Env) (manifest.SemanticConfigFingerprint, error) {
|
|
|
|
|
if env == nil || env.Config == nil || env.Config.Pipeline == nil {
|
|
|
|
|
return manifest.SemanticConfigFingerprint{}, fmt.Errorf("resolved pipeline configuration is required")
|
|
|
|
|
}
|
|
|
|
|
pipeline := env.Config.Pipeline
|
|
|
|
|
return FingerprintSemanticConfig(cleanupSemanticConfigVersion, cleanupSemanticConfig{
|
2026-11-30 01:41:12 +00:00
|
|
|
DeleteWorkspace: pipeline.Workspace.CleanupAfterPublish,
|
|
|
|
|
WorkspaceRoot: normalizedCleanupRoot(pipeline.Workspace.Root),
|
2026-09-05 15:15:28 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cleanupStage) ValidateResume(ctx context.Context, env *Env, m *manifest.Manifest) (ResumeValidation, error) {
|
|
|
|
|
plan, err := buildCleanupPlan(env, m)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ResumeValidation{}, err
|
|
|
|
|
}
|
|
|
|
|
if !plan.enabled {
|
|
|
|
|
return NonResumable(cleanupSkipReason), nil
|
|
|
|
|
}
|
|
|
|
|
for _, target := range plan.targets {
|
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
|
return ResumeValidation{}, fmt.Errorf("validate cleanup resume for %s: %w", target.policy, err)
|
|
|
|
|
}
|
2026-11-30 13:56:56 +00:00
|
|
|
pending, err := inspectCleanupTarget(target.root, target.target)
|
2026-11-30 01:41:12 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return ResumeValidation{}, err
|
2026-11-30 01:02:01 +00:00
|
|
|
}
|
2026-11-30 01:41:12 +00:00
|
|
|
if pending {
|
2026-09-05 15:15:28 +00:00
|
|
|
return NonResumable(fmt.Sprintf("cleanup target for %s exists", target.policy)), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Resumable(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func normalizedCleanupRoot(root string) string {
|
|
|
|
|
root = strings.TrimSpace(root)
|
|
|
|
|
if root == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return filepath.Clean(root)
|
|
|
|
|
}
|
2026-11-30 13:56:56 +00:00
|
|
|
|
|
|
|
|
func cleanupManagedNames() []string {
|
|
|
|
|
names := []string{"restore"}
|
|
|
|
|
for _, item := range All() {
|
|
|
|
|
names = append(names, item.Name())
|
|
|
|
|
}
|
|
|
|
|
return names
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// inspectCleanupTarget checks the session path and runs directory without creating
|
|
|
|
|
// anything, then inspects managed views independently of their targets.
|
|
|
|
|
func inspectCleanupTarget(workspaceRoot, runsDir string) (bool, error) {
|
|
|
|
|
workspace, err := filepath.Abs(workspaceRoot)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
session, err := filepath.Abs(filepath.Dir(runsDir))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
rel, err := filepath.Rel(workspace, session)
|
|
|
|
|
if err != nil || rel == "." || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
|
|
|
|
return false, fmt.Errorf("cleanup session must be below workspace root")
|
|
|
|
|
}
|
|
|
|
|
root, err := os.OpenRoot(workspace)
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
defer root.Close()
|
|
|
|
|
current := ""
|
|
|
|
|
for _, component := range strings.Split(rel, string(filepath.Separator)) {
|
|
|
|
|
current = filepath.Join(current, component)
|
|
|
|
|
info, err := root.Lstat(current)
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
|
|
|
|
return false, fmt.Errorf("unsafe cleanup session directory %q", current)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
info, err := root.Lstat(filepath.Join(rel, "runs"))
|
|
|
|
|
runsExist := err == nil
|
|
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
if runsExist && (!info.IsDir() || info.Mode()&os.ModeSymlink != 0) {
|
|
|
|
|
return false, fmt.Errorf("unsafe run cleanup directory %q", runsDir)
|
|
|
|
|
}
|
|
|
|
|
viewsExist, err := fileops.InspectManagedAttemptViews(session, cleanupManagedNames())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
return runsExist || viewsExist, nil
|
|
|
|
|
}
|