Files

240 lines
7.8 KiB
Go

package server
import (
"context"
"fmt"
"path/filepath"
"sort"
"strconv"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
type storageCandidate struct {
item protocol.StorageCleanupItem
timestamp time.Time
}
func (m *storageManager) preview(ctx context.Context, policy protocol.StoragePolicy) (*protocol.StorageCleanupResult, error) {
if err := policy.Validate(); err != nil {
return nil, err
}
inventory, err := m.inventory(ctx)
if err != nil {
return nil, err
}
result := &protocol.StorageCleanupResult{Policy: policy, TotalBytes: inventory.TotalBytes, TargetBytes: policy.MaxBytes, Items: []protocol.StorageCleanupItem{}, Errors: append([]string{}, inventory.Errors...)}
runs, err := m.store.ListRuns(ctx)
if err != nil {
return nil, err
}
recordProtected := func(item protocol.StorageCleanupItem) {
result.ProtectedCount++
if result.ProtectedCount <= 200 {
result.Items = append(result.Items, item)
}
if result.ProtectedCount == 201 {
result.Errors = append(result.Errors, "Only the first 200 protected items are shown; the protected count includes all items.")
}
}
var candidates []storageCandidate
artifactWalker := storageWalker{ctx: ctx, seen: map[string]bool{}, inodes: map[[2]uint64]bool{}}
for _, run := range runs {
if err := ctx.Err(); err != nil {
return nil, err
}
_, reason, err := m.store.PreviewRunDeletion(ctx, run.ID, m.runner.artifactsRoot)
if err != nil {
return nil, err
}
item := protocol.StorageCleanupItem{Kind: "run", ID: strconv.FormatInt(run.ID, 10), Path: filepath.Join(m.runner.artifactsRoot, strconv.FormatInt(run.ID, 10)), Protected: reason != "", Reason: reason}
if reason != "" {
recordProtected(item)
continue
}
measured := storageCategory{}
artifactWalker.measure(item.Path, &measured)
bytes, partial := measured.Bytes, measured.Partial
var problems []string
if measured.Error != "" {
problems = append(problems, measured.Error)
}
if partial {
item.Protected = true
item.Reason = "artifact measurement incomplete"
recordProtected(item)
result.Errors = append(result.Errors, problems...)
continue
}
item.Bytes = bytes
stamp := run.FinishedAt
if stamp == "" {
stamp = run.CreatedAt
}
candidates = append(candidates, storageCandidate{item: item, timestamp: storageTime(stamp)})
}
if policy.IncludeWorktrees {
for _, tree := range inventory.Worktrees {
item := protocol.StorageCleanupItem{Kind: "worktree", ID: canonicalStoragePath(tree.Path), Path: tree.Path, Bytes: tree.Bytes, Protected: !tree.Eligible, Reason: tree.Reason}
if !tree.Eligible {
recordProtected(item)
continue
}
candidates = append(candidates, storageCandidate{item: item, timestamp: tree.ModifiedAt})
}
}
sort.Slice(candidates, func(i, j int) bool { return candidates[i].timestamp.Before(candidates[j].timestamp) })
remaining := inventory.TotalBytes
cutoff := time.Now().Add(-time.Duration(policy.MaxAgeDays) * 24 * time.Hour)
for _, candidate := range candidates {
expired := policy.MaxAgeDays > 0 && !candidate.timestamp.IsZero() && candidate.timestamp.Before(cutoff)
over := policy.MaxBytes > 0 && remaining > policy.MaxBytes
if !expired && !over {
continue
}
if result.CandidateCount >= 100 {
result.Errors = append(result.Errors, "Cleanup is limited to 100 items per pass; preview again for remaining items.")
break
}
item := candidate.item
if expired {
item.Reason = "older than retention age"
} else {
item.Reason = "oldest eligible data above storage target"
}
result.Items = append(result.Items, item)
result.CandidateCount++
result.ReclaimableBytes += item.Bytes
remaining = max(int64(0), remaining-item.Bytes)
}
result.TargetReached = policy.MaxBytes == 0 || remaining <= policy.MaxBytes
if inventory.Partial {
result.Errors = append(result.Errors, "Storage measurement is partial; the total and target result are lower bounds.")
result.TargetReached = false
}
return result, nil
}
func storageTime(value string) time.Time {
for _, layout := range []string{time.RFC3339Nano, "2006-01-02 15:04:05"} {
if t, err := time.Parse(layout, value); err == nil {
return t
}
}
return time.Time{}
}
func (m *storageManager) apply(ctx context.Context, preview protocol.StorageCleanupResult) (*protocol.StorageCleanupResult, error) {
result := preview
result.PreviewToken = ""
result.Applied = true
before, err := m.inventory(ctx)
if err != nil {
return nil, err
}
var ids []int64
for _, item := range preview.Items {
if item.Protected || item.Kind != "run" {
continue
}
id, err := strconv.ParseInt(item.ID, 10, 64)
if err != nil {
return nil, err
}
ids = append(ids, id)
}
if len(ids) > 0 {
deleted, err := m.runner.deleteRunsContext(ctx, ids, true)
if err != nil {
return nil, err
}
result.RemovedCount += len(deleted.Deleted)
removed := map[string]bool{}
for _, id := range deleted.Deleted {
removed[strconv.FormatInt(id, 10)] = true
}
for _, item := range preview.Items {
if item.Kind == "run" && removed[item.ID] {
result.RemovedBytes += item.Bytes
}
}
result.RemovedBytes = max(int64(0), result.RemovedBytes-deleted.RemainingFileBytes)
if deleted.RemainingFileBytes > 0 {
result.Errors = append(result.Errors, "Some staged files remain after run deletion; their bytes are not counted as removed.")
}
for _, refused := range deleted.Refused {
result.Errors = append(result.Errors, fmt.Sprintf("Run %d retained: %s", refused.RunID, refused.Reason))
}
}
for _, item := range preview.Items {
if item.Protected || item.Kind != "worktree" {
continue
}
if err := ctx.Err(); err != nil {
result.Errors = append(result.Errors, err.Error())
break
}
// Admission and removal are serialized against the runner. Chat creation
// takes the workspace gate in the websocket dispatcher before recording paths.
if !m.workspaceMu.TryLock() {
result.Errors = append(result.Errors, "Worktree retained: chat workspace admission is busy")
continue
}
if !m.runner.mu.TryLock() {
m.workspaceMu.Unlock()
result.Errors = append(result.Errors, "Worktree retained: run workspace admission is busy")
continue
}
removalCtx, cancelRemoval := context.WithTimeout(ctx, 3*time.Second)
config, err := m.inventoryConfig(removalCtx)
if err == nil {
for _, active := range m.runner.active {
config.ProtectedWorkspaces = append(config.ProtectedWorkspaces, active.workspace)
}
var tree storageWorktree
found := false
for _, known := range before.Worktrees {
if canonicalStoragePath(known.Path) == canonicalStoragePath(item.Path) {
tree = known
found = true
break
}
}
if !found {
err = fmt.Errorf("worktree is no longer inventoried")
} else {
err = removeStorageWorktree(removalCtx, tree, config.ProtectedWorkspaces)
}
}
cancelRemoval()
m.runner.mu.Unlock()
m.workspaceMu.Unlock()
if err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("Worktree %s retained: %v", item.Path, err))
} else {
result.RemovedCount++
result.RemovedBytes += treeBytes(before.Worktrees, item.Path)
}
}
after, err := m.inventory(ctx)
if err != nil {
result.Errors = append(result.Errors, err.Error())
result.TargetReached = false
return &result, nil
}
result.TotalBytes = after.TotalBytes
// Sum only successfully removed measured file attribution. A partial
// post-scan or concurrent writes must never fabricate a reclaimed total.
result.TargetReached = !after.Partial && (result.Policy.MaxBytes == 0 || after.TotalBytes <= result.Policy.MaxBytes)
result.Errors = append(result.Errors, after.Errors...)
return &result, nil
}
func treeBytes(trees []storageWorktree, path string) int64 {
for _, tree := range trees {
if canonicalStoragePath(tree.Path) == canonicalStoragePath(path) {
return tree.Bytes
}
}
return 0
}