Files
alan 14175eba70 retention: explicit run-history and artifact retention
Add `pipelines retention report` (strictly read-only) and
`pipelines retention apply` (explicit, approval-gated removal) with an age
and/or count policy, protected-run rules, atomic single-transaction removal
and honest database space reclaim. Nothing prunes automatically.
2026-10-17 15:45:46 +01:00

180 lines
5.9 KiB
Go

package cli
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/pipeline"
)
// seedRetentionStore creates a scratch database with a definition and the given
// number of runs, plus one artifact under the first run. It returns the database
// path and the directory that holds the default artifacts root.
func seedRetentionStore(t *testing.T, runs int) (db, dir string) {
t.Helper()
dir = t.TempDir()
db = filepath.Join(dir, "pipelines.db")
ctx := context.Background()
store, err := pipeline.Open(db)
if err != nil {
t.Fatal(err)
}
if _, err := store.Submit(ctx, "cli-retention", pipeline.Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
items, err := store.List(ctx)
if err != nil {
t.Fatal(err)
}
definition, err := store.Get(ctx, items[0].ID, 1)
if err != nil {
t.Fatal(err)
}
for i := 0; i < runs; i++ {
id, _, err := store.CreateRun(ctx, definition, fmt.Sprintf("run-%d", i), "ws", false, nil, map[string]any{"prompt": "task"})
if err != nil {
t.Fatal(err)
}
if err := store.FinishRun(ctx, id, "succeeded", "success", "", map[string]any{}); err != nil {
t.Fatal(err)
}
}
if err := store.Close(); err != nil {
t.Fatal(err)
}
artifact := filepath.Join(dir, "artifacts", "1", "1", "report.bin")
if err := os.MkdirAll(filepath.Dir(artifact), 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(artifact, []byte("artifact bytes"), 0600); err != nil {
t.Fatal(err)
}
return db, dir
}
func runCount(t *testing.T, db string) int {
t.Helper()
store, err := pipeline.OpenReadOnly(db)
if err != nil {
t.Fatal(err)
}
defer store.Close()
runs, err := store.ListRuns(context.Background())
if err != nil {
t.Fatal(err)
}
return len(runs)
}
func newRetentionApp() (App, *bytes.Buffer) {
var out bytes.Buffer
return App{Out: &out, Err: &out, In: strings.NewReader("")}, &out
}
func TestRetentionReportIsReadOnlyAndLocal(t *testing.T) {
ctx := context.Background()
db, dir := seedRetentionStore(t, 3)
app, out := newRetentionApp()
// --url not-a-server proves the command never dials a server.
if err := app.Run(ctx, []string{"retention", "report", "--db", db, "--keep", "1", "--json", "--url", "not-a-server"}); err != nil {
t.Fatal(err)
}
var report pipeline.RetentionReport
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
t.Fatalf("report is not valid JSON: %v\n%s", err, out.String())
}
if report.Applied {
t.Fatal("a report must never be applied")
}
if len(report.WouldRemove) != 2 || report.TotalRuns != 3 {
t.Fatalf("report selection wrong: %+v", report)
}
if report.ArtifactsRoot != filepath.Join(dir, "artifacts") {
t.Fatalf("default artifacts root = %q, want artifacts/ beside the db", report.ArtifactsRoot)
}
if report.ReclaimableBytes < int64(len("artifact bytes")) {
t.Fatalf("reclaimable bytes must include the run artifacts: %d", report.ReclaimableBytes)
}
if runCount(t, db) != 3 {
t.Fatal("report must not change the run history")
}
if _, err := os.Stat(filepath.Join(dir, "artifacts", "1", "1", "report.bin")); err != nil {
t.Fatalf("report must not touch artifacts: %v", err)
}
}
func TestRetentionApplyRequiresApprovalAndRemovesRuns(t *testing.T) {
ctx := context.Background()
db, dir := seedRetentionStore(t, 3)
app, out := newRetentionApp()
// Without --yes and with no interactive input, apply refuses and changes nothing.
if err := app.Run(ctx, []string{"retention", "apply", "--db", db, "--keep", "1", "--url", "not-a-server"}); err == nil {
t.Fatal("apply must refuse without --yes or interactive approval")
}
if runCount(t, db) != 3 {
t.Fatal("refused apply changed the run history")
}
if _, err := os.Stat(filepath.Join(dir, "artifacts", "1", "1", "report.bin")); err != nil {
t.Fatalf("refused apply touched artifacts: %v", err)
}
out.Reset()
if err := app.Run(ctx, []string{"retention", "apply", "--db", db, "--keep", "1", "--yes", "--json", "--url", "not-a-server"}); err != nil {
t.Fatal(err)
}
var result pipeline.RetentionApplyResult
if err := json.Unmarshal(out.Bytes(), &result); err != nil {
t.Fatalf("apply is not valid JSON: %v\n%s", err, out.String())
}
if !result.Applied || result.RemovedRunCount != 2 || !result.Vacuumed {
t.Fatalf("apply did not remove and reclaim: %+v", result)
}
if runCount(t, db) != 1 {
t.Fatalf("apply removed the wrong number of runs: %d", runCount(t, db))
}
if _, err := os.Stat(filepath.Join(dir, "artifacts", "1")); !os.IsNotExist(err) {
t.Fatalf("removed run's artifacts must be gone: %v", err)
}
}
func TestRetentionValidationAndMissingDatabase(t *testing.T) {
ctx := context.Background()
db, dir := seedRetentionStore(t, 1)
app, _ := newRetentionApp()
for _, args := range [][]string{
{"retention", "report", "--db", db, "--url", "not-a-server"}, // no policy
{"retention", "report", "--db", db, "--keep=-1", "--url", "not-a-server"}, // negative count
{"retention", "report", "--db", db, "--older-than", "0h", "--url", "not-a-server"},
{"retention", "frobnicate", "--db", db, "--keep", "1", "--url", "not-a-server"}, // unknown subcommand
} {
if err := app.Run(ctx, args); err == nil {
t.Fatalf("%v was accepted", args)
}
}
missing := filepath.Join(dir, "missing.db")
if err := app.Run(ctx, []string{"retention", "report", "--db", missing, "--keep", "1", "--url", "not-a-server"}); err == nil {
t.Fatal("a missing database must be an error")
}
if _, err := os.Stat(missing); !os.IsNotExist(err) {
t.Fatal("retention must not create a database")
}
}
func TestRetentionHelp(t *testing.T) {
app, out := newRetentionApp()
if err := app.Run(context.Background(), []string{"help", "retention"}); err != nil {
t.Fatal(err)
}
for _, want := range []string{"retention report|apply", "--older-than", "--keep", "--artifacts"} {
if !strings.Contains(out.String(), want) {
t.Fatalf("help retention missing %q:\n%s", want, out.String())
}
}
}