Add run size_bytes and delete_runs to free disk space

Every run summary carries size_bytes (artifact tree, server-created Pi
session directories and an estimate of the run's database rows), cached in
pipeline_runs.size_bytes, invalidated by triggers, measured when a run
finishes and backfilled in the background. delete_runs removes terminal
runs with every scoped row and server-owned file, refuses active ones with
a reason, and pushes runs_removed to subscribers. pipelines runs rm is the
CLI form; retention apply now shares the complete row deletion.
This commit is contained in:
2026-10-31 22:11:14 +00:00
parent 62b56e66d0
commit 31e89e872b
20 changed files with 1421 additions and 12 deletions
+4
View File
@@ -158,6 +158,7 @@ func (a App) flags(command string) (*flag.FlagSet, *options) {
f.DurationVar(&o.waitTimeout, "wait-timeout", 0, "Client limit for --wait after the run is admitted (0 waits until terminal); separate from per-request --timeout")
case "runs":
f.StringVar(&o.name, "pipeline", "", "Filter by exact pipeline name or ID")
f.BoolVar(&o.yes, "yes", false, "Confirm `runs rm` without an interactive prompt")
case "search":
f.StringVar(&o.kind, "type", "", "Narrow to one kind: "+strings.Join(protocol.SearchKinds, ", "))
f.IntVar(&o.limit, "limit", 0, "Maximum results (default 50, max 200)")
@@ -751,6 +752,9 @@ func (a App) Run(ctx context.Context, args []string) error {
}
return nil
case "list", "templates", "runs", "ping", "connection":
if command == "runs" && len(pos) > 0 && pos[0] == "rm" {
return a.removeRuns(o, pos[1:], call)
}
if len(pos) > 0 {
return fmt.Errorf("%s takes no positional arguments", command)
}
+2 -1
View File
@@ -19,7 +19,7 @@ var usage = map[string]string{
"templates": "templates [--json]",
"template": "template <key> [--source | --json]",
"run": "run [name|id] [prompt] [--title TITLE] [--model PROVIDER/MODEL] [--stage-model STAGE=PROVIDER/MODEL ...] [--json [--wait [--wait-timeout DURATION]]]",
"runs": "runs [--pipeline NAME] [--json]",
"runs": "runs [--pipeline NAME] [--json] | runs rm <run-id> [run-id ...] [--yes] [--json]",
"inspect": "inspect <run-id> [--json]",
"wait": "wait <run-id> [run-id ...] [--timeout DURATION] [--json]",
"cancel": "cancel <run-id> [--json]",
@@ -52,6 +52,7 @@ var notes = map[string]string{
"submit": "Confirm publication at the prompt, or use --yes for explicit automation; schedules activate on acceptance. Managed drafts provide the name and base-version guard. --force never bypasses validation.",
"run": "Runs in your current directory after action confirmation (--yes skips it). Select a pipeline and compose a multiline task with Ctrl+S. Stage tools are enabled automatically; no file list or typed RUN confirmation. Cancellation does not undo edits. --title sets an explicit run title; without it the title is derived from the prompt. --model PROVIDER/MODEL overrides the model for every stage of that run; --stage-model STAGE=PROVIDER/MODEL overrides one stage and is repeatable, with a repeated key last-wins. Precedence is per-stage > whole-run > the model pinned in the pipeline version; overrides apply to that run only and never change the pinned definition. An unknown stage key or a malformed provider/model is refused before the run starts, naming the valid stage keys. In JSON mode --wait polls until terminal; --wait-timeout bounds only the client wait after admission and is independent of per-request --timeout. A reached wait limit leaves the run active: inspect read-only with `inspect <run-id> --json`, or reattach with --wait reusing the original --request-key (never an automatic replay or cancel).",
"retry": "Resume a failed stage by default, restoring the completed prefix handoff; use --mode=restart to start at entry. --stage selects a recorded stage (use --visit for repeated stages). --input-file accepts a bounded JSON object, merged with replacement at the selected stage; '-' reads stdin. Original history and pinned identity are retained; --version selects an explicit published version of the same pipeline only. --wait-timeout leaves an active retry for later read-only inspection or reattachment with the original --request-key, rather than starting a duplicate retry.",
"runs": "Lists runs newest first; --json includes each run's size_bytes (bytes on disk: artifacts, session exports, server-created Pi session directories and an estimate of its database rows; 0 until first measured). `runs rm <id...>` permanently deletes finished runs (succeeded, failed, canceled, interrupted) with every database row and server-owned file for them, after confirmation (--yes for scripts). Active runs, runs an unfinished retry depends on and unknown ids are refused individually with a reason, and the command then exits 1. User workspaces are never touched. Database pages are reused by SQLite; `retention apply` additionally VACUUMs to shrink the file.",
"title": "Rename a run's operator-facing title after the fact without touching its input, steps or execution. The title is collapsed to a single bounded line and recorded as explicitly set. `pipelines runs` and `pipelines inspect` then show it.",
"wait": "Block until each named run reaches a terminal status - succeeded, failed, canceled or interrupted, and also through a run waiting on a person - then print one line per run with its id, status and outcome, its stage and how long it took, and exit 0 even for a run that finished unsuccessfully. Several ids wait together in one command. The exit is non-zero only for a client-side failure: an unknown run id, a connection that could not be established, or --timeout expiring while a run is still active; a timeout says so plainly, never cancels, retries or otherwise touches a run, and is never reported as its outcome. Read-only: it needs no workspace, is not refused by the workspace guard and writes nothing. --json prints the final run envelope instead of the line (one envelope for one id, an array for several).",
"decide": "Choose an option for a run waiting on a human stage. The run must be `waiting`; the option must be one of that stage's non-error edges. The stage is inferred from the run's current stage unless named. The choice and optional --feedback are recorded as the step's evidence (visible in inspect and the TUI) and the run resumes along the chosen edge. An unknown option is refused with the valid list; a repeated decision for an already-decided visit does not double-apply. Works across a server restart because the wait is persisted.",
+70
View File
@@ -0,0 +1,70 @@
package cli
import (
"fmt"
"strconv"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// removeRuns implements `pipelines runs rm <id...>`: one delete_runs request
// for every named run. The server refuses active and unknown runs individually;
// any refusal makes the command exit non-zero after reporting the result.
func (a App) removeRuns(o *options, args []string, call callFunc) error {
if len(args) == 0 {
return fmt.Errorf("runs rm requires at least one run ID; use pipelines help runs")
}
ids := make([]int64, 0, len(args))
for _, value := range args {
id, err := strconv.ParseInt(value, 10, 64)
if err != nil || id <= 0 {
return fmt.Errorf("run ID must be positive: %q", value)
}
ids = append(ids, id)
}
if err := a.confirmAction(fmt.Sprintf("Permanently delete %d run(s) with their artifacts and history?", len(ids)), o.yes); err != nil {
return err
}
r, err := call(protocol.Message{Type: protocol.DeleteRuns, RunIDs: ids})
if err != nil {
return err
}
if r.RunsDeleted == nil {
return fmt.Errorf("server did not return a deletion result; update pipelines-server")
}
result := r.RunsDeleted
if o.json {
if err := a.output(result); err != nil {
return err
}
} else {
for _, id := range result.Deleted {
fmt.Fprintf(a.Out, "deleted\t%d\n", id)
}
for _, refusal := range result.Refused {
fmt.Fprintf(a.Out, "refused\t%d\t%s\n", refusal.RunID, refusal.Reason)
}
fmt.Fprintf(a.Out, "Freed %s\n", formatSize(result.FreedBytes))
}
if len(result.Refused) > 0 {
return &ExitError{Code: 1}
}
return nil
}
// formatSize renders a byte count with a binary unit for human output.
func formatSize(bytes int64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
value, suffix := float64(bytes), "B"
for _, next := range []string{"KiB", "MiB", "GiB", "TiB"} {
if value < unit {
break
}
value /= unit
suffix = next
}
return fmt.Sprintf("%.1f %s", value, suffix)
}
+60
View File
@@ -0,0 +1,60 @@
package cli
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/wstest"
)
func TestCLIRunsRmSendsDeleteRuns(t *testing.T) {
var got protocol.Message
endpoint := wstest.Server(t, func(request protocol.Message) protocol.Message {
got = request
return protocol.Message{Type: protocol.RunsDeleted, RunsDeleted: &protocol.RunsDeletedResult{
Deleted: []int64{3},
Refused: []protocol.RunDeleteRefusal{{RunID: 4, Reason: "run is running; cancel it or wait for it to finish"}},
FreedBytes: 2048,
}}
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var out, errOut bytes.Buffer
app := App{Out: &out, Err: &errOut, In: strings.NewReader("")}
err := app.Run(ctx, []string{"--url", endpoint, "runs", "rm", "3", "4", "--yes", "--json"})
var exit *ExitError
if !errors.As(err, &exit) || exit.Code != 1 {
t.Fatalf("err = %v, want exit 1 for a refusal", err)
}
if got.Type != protocol.DeleteRuns || fmt.Sprint(got.RunIDs) != "[3 4]" {
t.Fatalf("wire request = %+v", got)
}
var decoded protocol.RunsDeletedResult
if err := json.Unmarshal(out.Bytes(), &decoded); err != nil {
t.Fatalf("stdout is not JSON: %v\n%s", err, out.String())
}
if fmt.Sprint(decoded.Deleted) != "[3]" || decoded.FreedBytes != 2048 || len(decoded.Refused) != 1 {
t.Fatalf("decoded = %+v", decoded)
}
// Without --yes a declined prompt sends nothing.
got = protocol.Message{}
declined := App{Out: &bytes.Buffer{}, Err: &bytes.Buffer{}, In: strings.NewReader("n\n")}
if err := declined.Run(ctx, []string{"--url", endpoint, "runs", "rm", "3"}); err == nil {
t.Fatal("declined deletion succeeded")
}
if got.Type != "" {
t.Fatalf("declined deletion sent %+v", got)
}
if err := declined.Run(ctx, []string{"--url", endpoint, "runs", "rm", "x", "--yes"}); err == nil {
t.Fatal("non-numeric id accepted")
}
}