oficina / equipo Public

hosted: img delete -ledger is resumable (Task 34 kit, K6)

With -ledger, img delete acts only on an image the run's journal
records (exit 4 otherwise, before any delete). A recorded image a read
shows already gone (not found, or DELETED) is success, journalled
"DELETED microvm-image ARN read-gone" once; a deletion it makes is
journalled "img-delete". Without -ledger nothing changes. The command
comment now says which exit codes are safety refusals (4) and which
are ordinary (1, 2, 5, 6).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev f384ee23ac390faef87ec4b90b900842b36863c1 parent cad3e17
5 files changed, +174 −17
  • cmd/equipo-hosted-image/main.go +12 −5
  • internal/hostedimage/caller_test.go +1 −1
  • internal/hostedimage/delete_test.go +93 −0
  • internal/hostedimage/publish.go +65 −8
  • internal/hostedimage/publish_test.go +3 −3
diff --git a/cmd/equipo-hosted-image/main.go b/cmd/equipo-hosted-image/main.go
index 1c0fba0f..7861de88 100644
--- a/cmd/equipo-hosted-image/main.go
+++ b/cmd/equipo-hosted-image/main.go
@@ -27,8 +27,11 @@
// writes ImageArn and VersionArn ("<image ARN>:<version>") to FILE.
// list
// the prefix's images.
-// delete -arn ARN [-timeout 15m] [-interval 15s]
-// deletes an image of the prefix and waits until it is gone.
+// delete -arn ARN [-ledger DIR] [-timeout 15m] [-interval 15s]
+// deletes an image of the prefix and waits until it is gone. With
+// -ledger (the run's directory) only an image DIR/journal.tsv
+// records, and resumably: one already gone is success; either way
+// "DELETED microvm-image ARN" is journalled once.
// connector-wait -arn ARN [-timeout 20m] [-interval 15s]
// waits for a network connector of the prefix to be ACTIVE.
//
@@ -46,7 +49,10 @@
// Each success prints one JSON object on stdout. Each failure prints one
// JSON line on stderr and exits 2 (an AWS call failed), 3 (usage), 4 (a
// guard refused), 5 (a wait ran out), 6 (a failed state, or a built
-// version that differs from the request) or 1 (anything else).
+// version that differs from the request) or 1 (anything else). Of these
+// only 4 is a safety refusal (equipo-hosted-ops's 99, for a plan's
+// branches); 1, 2, 5 and 6 are ordinary failures a pre-approved fallback
+// may retry; 3 is the plan's own error.
package main
import (
@@ -267,12 +273,13 @@ func parseInvocation(ctx context.Context, args []string) (invocation, error) {
}
case "delete":
arn := fs.String("arn", "", "the image ARN")
+ ledger := fs.String("ledger", "", "the run's ledger directory: delete only a recorded image, accept one already gone, journal the deletion")
wait := waitFlags(fs, 15*time.Minute)
if err := parse(fs, rest[1:]); err != nil {
return invocation{}, err
}
- check = func() error { return hostedimage.CheckDelete(t, *arn, wait()) }
- step = func(p hostedimage.Provider) (any, error) { return hostedimage.Delete(ctx, p, t, *arn, wait()) }
+ check = func() error { return hostedimage.CheckDelete(t, *arn, wait(), *ledger) }
+ step = func(p hostedimage.Provider) (any, error) { return hostedimage.Delete(ctx, p, t, *arn, wait(), *ledger) }
case "connector-wait":
arn := fs.String("arn", "", "the network connector ARN")
wait := waitFlags(fs, 20*time.Minute)
diff --git a/internal/hostedimage/caller_test.go b/internal/hostedimage/caller_test.go
index 4808c793..75f2763c 100644
--- a/internal/hostedimage/caller_test.go
+++ b/internal/hostedimage/caller_test.go
@@ -21,7 +21,7 @@ var ops = map[string]func(tt *testing.T, p Provider, t Target) error{
return err
},
"delete": func(_ *testing.T, p Provider, t Target) error {
- _, err := Delete(context.Background(), p, t, "arn:aws:lambda:"+region+":"+acct+":microvm-image:"+prefix, wait())
+ _, err := Delete(context.Background(), p, t, "arn:aws:lambda:"+region+":"+acct+":microvm-image:"+prefix, wait(), "")
return err
},
"list": func(_ *testing.T, p Provider, t Target) error { _, err := List(context.Background(), p, t); return err },
diff --git a/internal/hostedimage/delete_test.go b/internal/hostedimage/delete_test.go
new file mode 100644
index 00000000..6bbd40b8
--- /dev/null
+++ b/internal/hostedimage/delete_test.go
@@ -0,0 +1,93 @@
+package hostedimage
+
+import (
+ "context"
+ "fmt"
+ "os"
+ "path/filepath"
+ "slices"
+ "testing"
+)
+
+// Task 34 kit, K6 (p34-r1 finding 8): with -ledger, img delete is
+// resumable. A recorded image a read shows already gone (absent, or in
+// state DELETED) is success, journalled DELETED once; a deletion it makes
+// is journalled too. It refuses, as a guard, an image the ledger does not
+// record. Without -ledger nothing changes.
+func TestDeleteResumable(t *testing.T) {
+ ctx := context.Background()
+ mine, other := imageARN(prefix), imageARN(prefix+"-b")
+ ledger := func(t *testing.T, lines ...string) string {
+ dir := t.TempDir()
+ f, err := os.OpenFile(filepath.Join(dir, JournalFile), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, l := range lines {
+ fmt.Fprintf(f, "2026-12-02T10:00:00.000Z\t%s\n", l)
+ }
+ f.Close()
+ return dir
+ }
+ deleted := func(t *testing.T, dir, how string) int {
+ n := 0
+ for _, e := range readJournal(t, dir) {
+ if e == "DELETED microvm-image "+mine+" "+how {
+ n++
+ }
+ }
+ return n
+ }
+
+ // Recorded, already absent: success, journalled once over two runs.
+ dir := ledger(t, "INTENT microvm-image "+prefix+" tok", "REC microvm-image "+mine, "REC microvm-image-version "+mine+":1")
+ for i := 0; i < 2; i++ {
+ p := newFake(t)
+ img, err := Delete(ctx, p, target, mine, wait(), dir)
+ if err != nil || img.ARN != mine || img.State != "DELETED" || slices.Contains(p.calls, "DeleteImage") {
+ t.Fatalf("run %d: an absent recorded image: %+v %v, calls %v", i+1, img, err, p.calls)
+ }
+ }
+ if n := deleted(t, dir, "read-gone"); n != 1 {
+ t.Fatalf("%d DELETED read-gone lines, want 1: %q", n, readJournal(t, dir))
+ }
+ // Recorded, in state DELETED: the same.
+ dir = ledger(t, "REC microvm-image "+mine)
+ p := newFake(t)
+ p.images[mine] = Image{ARN: mine, Name: prefix, State: "DELETED"}
+ if _, err := Delete(ctx, p, target, mine, wait(), dir); err != nil || slices.Contains(p.calls, "DeleteImage") || deleted(t, dir, "read-gone") != 1 {
+ t.Fatalf("a DELETED recorded image: %v, calls %v, journal %q", err, p.calls, readJournal(t, dir))
+ }
+ // Recorded and there: deleted, waited for and journalled.
+ dir = ledger(t, "REC microvm-image "+mine)
+ p = newFake(t)
+ p.images[mine] = Image{ARN: mine, Name: prefix, State: "CREATED"}
+ if _, err := Delete(ctx, p, target, mine, wait(), dir); err != nil || !slices.Contains(p.calls, "DeleteImage") || deleted(t, dir, "img-delete") != 1 {
+ t.Fatalf("a recorded image: %v, calls %v, journal %q", err, p.calls, readJournal(t, dir))
+ }
+ // Not recorded: refused as a guard, before the delete, whether there or not.
+ for _, there := range []bool{true, false} {
+ dir = ledger(t, "REC microvm-image "+mine)
+ p = newFake(t)
+ if there {
+ p.images[other] = Image{ARN: other, Name: prefix + "-b", State: "CREATED"}
+ }
+ _, err := Delete(ctx, p, target, other, wait(), dir)
+ if err == nil || failureOf(t, err).Exit != ExitGuard || slices.Contains(p.calls, "DeleteImage") || len(readJournal(t, dir)) != 1 {
+ t.Fatalf("an image the ledger does not record (there %t): %v, calls %v", there, err, p.calls)
+ }
+ }
+ // A recorded deletion counts: the image was recorded and later deleted.
+ dir = ledger(t, "REC microvm-image "+mine, "DELETED microvm-image "+mine+" img-delete")
+ if _, err := Delete(ctx, newFake(t), target, mine, wait(), dir); err != nil || deleted(t, dir, "read-gone") != 0 {
+ t.Fatalf("an image already recorded deleted: %v, journal %q", err, readJournal(t, dir))
+ }
+ // Without -ledger, an absent image is still the API's not-found.
+ if _, err := Delete(ctx, newFake(t), target, mine, wait(), ""); err == nil || failureOf(t, err).Exit != ExitAPI {
+ t.Fatalf("an absent image without -ledger: %v", err)
+ }
+ // -ledger must be a directory, checked before any call.
+ if err := CheckDelete(target, mine, wait(), filepath.Join(t.TempDir(), "nope")); err == nil || failureOf(t, err).Exit != ExitUsage {
+ t.Fatalf("a missing -ledger directory: %v", err)
+ }
+}
diff --git a/internal/hostedimage/publish.go b/internal/hostedimage/publish.go
index 5e53079a..8cdd5948 100644
--- a/internal/hostedimage/publish.go
+++ b/internal/hostedimage/publish.go
@@ -566,18 +566,46 @@ func sameVersion(v Version, req CreateImage) error {
return nil
}
-// Delete deletes an image of the prefix, then waits until it is gone. The
-// ARN must be in the target's account and region (no call otherwise);
-// the image is read first and refused unless its name carries the
-// prefix.
-func Delete(ctx context.Context, p Provider, t Target, arn string, w Wait) (Image, error) {
- if err := CheckDelete(t, arn, w); err != nil {
+// Delete deletes an image of the prefix and waits until it is gone.
+//
+// With ledger (the run's -ledger directory; Task 34 kit, K6) it acts only
+// on an image the journal records (REC microvm-image ARN), refusing any
+// other as a guard (exit 4) before any delete, and it is resumable: a
+// recorded image a read shows already gone (not found, or DELETED) is
+// success, journalled "DELETED microvm-image ARN read-gone" unless the
+// journal already records its deletion; a deletion it makes and sees
+// through is journalled "DELETED microvm-image ARN img-delete". Without
+// ledger an image already gone is the read's failure, as before.
+func Delete(ctx context.Context, p Provider, t Target, arn string, w Wait, ledger string) (Image, error) {
+ if err := CheckDelete(t, arn, w, ledger); err != nil {
return Image{}, err
}
+ var deletedBefore bool
+ if ledger != "" {
+ recorded, deleted, err := journalImage(ledger, arn)
+ if err != nil {
+ return Image{}, err
+ }
+ if !recorded {
+ return Image{}, guard("image %s is not recorded in %s; delete acts only on the run's recorded images", arn, filepath.Join(ledger, JournalFile))
+ }
+ deletedBefore = deleted
+ }
if err := checkCaller(ctx, p, t); err != nil {
return Image{}, err
}
+ gone := func(how string) (Image, error) {
+ if !deletedBefore {
+ if err := journal(ledger, w.Clock.Now(), "DELETED microvm-image "+arn+" "+how); err != nil {
+ return Image{}, err
+ }
+ }
+ return Image{ARN: arn, State: "DELETED"}, nil
+ }
img, err := p.GetImage(ctx, arn)
+ if ledger != "" && (errors.Is(err, ErrNotFound) || err == nil && img.State == "DELETED") {
+ return gone("read-gone")
+ }
if err != nil {
return Image{}, apiFailure("GetMicrovmImage", err)
}
@@ -605,18 +633,47 @@ func Delete(ctx context.Context, p Provider, t Target, arn string, w Wait) (Imag
}
return false, nil
})
+ if err == nil && ledger != "" {
+ if _, jerr := gone("img-delete"); jerr != nil {
+ return last, jerr
+ }
+ }
return last, err
}
+// journalImage reads the ledger's journal for image: whether a REC records
+// it, and whether a DELETED line already does.
+func journalImage(dir, image string) (recorded, deleted bool, err error) {
+ b, rerr := os.ReadFile(filepath.Join(dir, JournalFile))
+ if rerr != nil {
+ return false, false, other("the journal does not read: %v", rerr)
+ }
+ for _, line := range strings.Split(string(b), "\n") {
+ _, entry, _ := strings.Cut(line, "\t")
+ f := strings.Fields(entry)
+ if len(f) >= 3 && f[1] == "microvm-image" && f[2] == image {
+ recorded = recorded || f[0] == "REC"
+ deleted = deleted || f[0] == "DELETED"
+ }
+ }
+ return recorded, deleted, nil
+}
+
// CheckDelete refuses a delete the tool will not run, before any call:
-// the ARN must be an image in the target's account and region.
-func CheckDelete(t Target, arn string, w Wait) error {
+// the ARN must be an image in the target's account and region, and
+// -ledger, when given, a directory.
+func CheckDelete(t Target, arn string, w Wait, ledger string) error {
if err := t.Check(); err != nil {
return err
}
if _, ok := t.arnIn(arn, "microvm-image"); !ok {
return guard("-arn %q is not an image in account %s, region %s", arn, t.Account, t.Region)
}
+ if ledger != "" {
+ if st, err := os.Stat(ledger); err != nil || !st.IsDir() {
+ return usage("-ledger %s is not a directory", ledger)
+ }
+ }
return w.check()
}
diff --git a/internal/hostedimage/publish_test.go b/internal/hostedimage/publish_test.go
index a4c93ee9..86896c40 100644
--- a/internal/hostedimage/publish_test.go
+++ b/internal/hostedimage/publish_test.go
@@ -409,18 +409,18 @@ func TestDeleteRefusesForeignImage(t *testing.T) {
p := newFake(t)
other := imageARN("someone-else")
p.images[other] = Image{ARN: other, Name: "someone-else", State: "CREATED"}
- if _, err := Delete(context.Background(), p, target, other, request(t).Wait); failureOf(t, err).Exit != ExitGuard || slices.Contains(p.calls, "DeleteImage") {
+ if _, err := Delete(context.Background(), p, target, other, request(t).Wait, ""); failureOf(t, err).Exit != ExitGuard || slices.Contains(p.calls, "DeleteImage") {
t.Fatalf("Delete of a foreign image = %v, calls %v", err, p.calls)
}
p = newFake(t)
foreign := "arn:aws:lambda:" + region + ":444455556666:microvm-image:" + prefix
- if _, err := Delete(context.Background(), p, target, foreign, request(t).Wait); err == nil || len(p.calls) > 1 {
+ if _, err := Delete(context.Background(), p, target, foreign, request(t).Wait, ""); err == nil || len(p.calls) > 1 {
t.Fatalf("Delete in another account = %v, calls %v", err, p.calls)
}
p = newFake(t)
mine := imageARN(prefix)
p.images[mine] = Image{ARN: mine, Name: prefix, State: "CREATED"}
- if _, err := Delete(context.Background(), p, target, mine, request(t).Wait); err != nil || !slices.Contains(p.calls, "DeleteImage") {
+ if _, err := Delete(context.Background(), p, target, mine, request(t).Wait, ""); err != nil || !slices.Contains(p.calls, "DeleteImage") {
t.Fatalf("Delete of the run's image = %v, calls %v", err, p.calls)
}
}