Clean the complete session run history

This commit is contained in:
2026-11-30 13:56:56 +00:00
parent 5196c67570
commit 03c16df524
8 changed files with 183 additions and 278 deletions

View File

@@ -2,16 +2,19 @@ package stage
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
)
const (
cleanupSkipReason = "cleanup policies are disabled"
cleanupSemanticConfigVersion = 1
cleanupSemanticConfigVersion = 2
)
type cleanupStage struct{}
@@ -42,7 +45,18 @@ func (cleanupStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S
if err := ctx.Err(); err != nil {
return nil, fmt.Errorf("cleanup canceled before %s: %w", target.policy, err)
}
_, err := cleanRunWithRetainedBundle(target.root, target.target, true)
_, 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)
}
}
if err != nil {
return nil, fmt.Errorf("apply %s: %w", target.policy, err)
}
@@ -79,7 +93,7 @@ func (cleanupStage) ValidateResume(ctx context.Context, env *Env, m *manifest.Ma
if err := ctx.Err(); err != nil {
return ResumeValidation{}, fmt.Errorf("validate cleanup resume for %s: %w", target.policy, err)
}
pending, err := cleanRunWithRetainedBundle(target.root, target.target, false)
pending, err := inspectCleanupTarget(target.root, target.target)
if err != nil {
return ResumeValidation{}, err
}
@@ -97,3 +111,63 @@ func normalizedCleanupRoot(root string) string {
}
return filepath.Clean(root)
}
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
}