From dd98ff7d7c3734e06f02add5834abc018ecbc763 Mon Sep 17 00:00:00 2001 From: "nekomimi.pet" Date: Sun, 25 Oct 2026 11:19:35 -0400 Subject: [PATCH] storage: a cancelled cleanup checks for cancellation before authorising deletes, so it no longer removes files after the user cancelled --- cli/internal/storage/maintenance.go | 4 ++++ cli/internal/storage/maintenance_test.go | 20 ++++++++++++++++---- robot-docs/migrations.md | 9 ++++++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/cli/internal/storage/maintenance.go b/cli/internal/storage/maintenance.go index 239e8797b..137b1abcc 100644 --- a/cli/internal/storage/maintenance.go +++ b/cli/internal/storage/maintenance.go @@ -90,6 +90,10 @@ func (m *maintenance) failure(ctx context.Context, err error) error { func (m *maintenance) apply(ctx context.Context, vacuum bool, fresh daemon.StorageReport) (CleanupResult, error) { var result CleanupResult + if err := ctx.Err(); err != nil { + m.close() + return result, err + } err := json.NewEncoder(m.input).Encode(struct { Apply bool `json:"apply"` Vacuum bool `json:"vacuum"` diff --git a/cli/internal/storage/maintenance_test.go b/cli/internal/storage/maintenance_test.go index 50e71e689..8ec334eb5 100644 --- a/cli/internal/storage/maintenance_test.go +++ b/cli/internal/storage/maintenance_test.go @@ -7,13 +7,14 @@ import ( "context" "errors" "os" + "os/exec" "path/filepath" "strings" "testing" ) func TestMaintenanceOwnershipAndRelease(t *testing.T) { - for _, ending := range []string{"success", "cancel", "helper killed", "input EOF", "replacement", "vacuum failure", "deletion failure", "no free pages"} { + for _, ending := range []string{"success", "cancel", "cancel before helper kill", "helper killed", "input EOF", "replacement", "vacuum failure", "deletion failure", "no free pages"} { t.Run(ending, func(t *testing.T) { if ending == "deletion failure" && os.Geteuid() == 0 { t.Skip("root can unlink files in a directory without write permission") @@ -29,7 +30,18 @@ func TestMaintenanceOwnershipAndRelease(t *testing.T) { } ctx, cancel := context.WithCancel(t.Context()) defer cancel() - helper, err := testService(home).startMaintenance(ctx, []daemon.StorageFile{{Path: path, Bytes: 4}}) + service := testService(home) + if ending == "cancel before helper kill" { + service.Command = func(ctx context.Context, args ...string) (*exec.Cmd, error) { + command, err := testCommand(ctx, args...) + if err == nil { + // A busy OS may not run CommandContext's kill before apply. + command.Cancel = func() error { return nil } + } + return command, err + } + } + helper, err := service.startMaintenance(ctx, []daemon.StorageFile{{Path: path, Bytes: 4}}) if err != nil { t.Fatal(err) } @@ -43,7 +55,7 @@ func TestMaintenanceOwnershipAndRelease(t *testing.T) { switch ending { case "success": _, err = helper.apply(ctx, false, daemon.StorageReport{}) - case "cancel": + case "cancel", "cancel before helper kill": cancel() _, err = helper.apply(ctx, false, daemon.StorageReport{}) if !errors.Is(err, context.Canceled) { @@ -84,7 +96,7 @@ func TestMaintenanceOwnershipAndRelease(t *testing.T) { t.Fatalf("unexpected vacuum result: %+v", result) } } - wantFailure := ending == "cancel" || ending == "helper killed" || ending == "replacement" || ending == "vacuum failure" || ending == "deletion failure" + wantFailure := ending == "cancel" || ending == "cancel before helper kill" || ending == "helper killed" || ending == "replacement" || ending == "vacuum failure" || ending == "deletion failure" if (err != nil) != wantFailure { t.Fatalf("%s: %v", ending, err) } diff --git a/robot-docs/migrations.md b/robot-docs/migrations.md index 42d0811c6..460affdce 100644 --- a/robot-docs/migrations.md +++ b/robot-docs/migrations.md @@ -35,9 +35,12 @@ before it revalidates candidates. Its Gleam helper holds ownership through file deletion and vacuum. Daemon startup refuses while maintenance owns the home, before it touches application storage. Neither owner commits the lock transaction, changes its journal mode, or replaces the lock file. Cleanup checks health for -diagnostics; only exclusive ownership permits offline mutation. CLI cancellation -or death stops the helper and releases ownership. Completed deletions remain -deleted if later deletion or vacuum fails. +diagnostics; only exclusive ownership permits offline mutation. The CLI checks +cancellation before sending authorization; it does not rely on asynchronous +helper termination to prevent an already-cancelled cleanup from starting. +Cancellation or death stops the helper and releases ownership. Deletions already +completed after authorization remain deleted if cancellation, later deletion, +or vacuum fails. The client invokes `albedo-daemon storage inspect ` and `albedo-daemon storage maintain ` through the same local executable -- knotmirror 0.0.1