oficina / equipo Public

isolatedjob: let the contract wait for a new VM to appear

AWS GetMicrovm is eventually consistent, so a VM RunMicrovm just
created can read as not found for a while (final review A3). The
Harness gains Visibility, a bounded wait for a new VM to appear that is
separate from state waits, and CallTimeout, a deadline on every Runner
call the contract makes, List in listUntil included (deferred Task 6
minor). The live settle helper bounds each Get and tolerates not found
for 60 s unless it waits for an end.

TestFakeContractLateVisibility runs the contract over a runner that
misses every new VM once and checks every Get and List has a deadline.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev d94d74546832c6d228843bfcea918f6d9688ea87 parent ab9e96b
4 files changed, +246 −39
  • docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md +24 −0
  • internal/isolatedjob/awsjob/live_test.go +27 −9
  • internal/isolatedjob/fake_test.go +77 −0
  • internal/isolatedjob/isolatedjobtest/contract.go +118 −30
diff --git a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
index 35d920b9..7b77a3f9 100644
--- a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
+++ b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
@@ -1013,3 +1013,27 @@ Findings from the final whole-branch review (final-A, B1, B2, C), fixed under ru
| Mutation | Command | Observed failure | Restored |
| --- | --- | --- | --- |
| Early step skips pending stops again (`pending = false && ...`) | `go test ./internal/hostedctl -run 'TestDeadlineStopBeforeSlowReplay$' -count=1` | `reaper_test.go:405: pending true: provider stops while the replay's launch call hung = [vm-0001], want [vm-0001 vm-0001]` | yes, passes |
+
+## Task 17 pre-flight
+
+Offline preparation for the live check; no AWS call was made. The live pieces compile under
+`hostedlive` and were not run.
+
+### A3: the contract waits for a new VM to appear
+
+- `isolatedjobtest.Harness` gains `Visibility` (how long Get may answer `ErrNotFound` for a VM Launch
+ just returned; zero for the fake, 60 s live) and `CallTimeout` (each Runner call's bound, 30 s by
+ default). The contract's `launch` waits for the VM to appear before any check reads it; this wait
+ is separate from Settle's state waits. Every Runner call the contract makes, `listUntil`'s Lists
+ included, now carries a deadline (closes the deferred Task 6 minor).
+- The live `settle` helper bounds each Get and reads `ErrNotFound` again during the first 60 s,
+ unless it waits for a terminal state (gone is then an answer).
+- `TestFakeContractLateVisibility`: the contract over a runner whose Get misses every new VM once,
+ and which counts Get and List calls without a deadline.
+- Runs: `go test ./internal/isolatedjob/... -count=1` ok; `go vet -tags hostedlive
+ ./internal/isolatedjob/...`; `make build vet`; the Task 7 guard passes.
+
+| Mutation | Command | Observed failure | Restored |
+| --- | --- | --- | --- |
+| Remove the visibility wait (`appear` not called in the contract's `launch`) | `go test ./internal/isolatedjob -run 'TestFakeContract' -count=1` | `contract.go:151: Get(vm-0001) = isolatedjob: not found: vm-0001 is not visible yet` | yes, passes |
+| Drop the per-call deadline (`call` returns `context.WithCancel`) | same | `fake_test.go:136: contract made 19 Get or List calls without a deadline (0 with one), want none without` | yes, passes |
diff --git a/internal/isolatedjob/awsjob/live_test.go b/internal/isolatedjob/awsjob/live_test.go
index abb688d4..2e0ae2de 100644
--- a/internal/isolatedjob/awsjob/live_test.go
+++ b/internal/isolatedjob/awsjob/live_test.go
@@ -80,21 +80,39 @@ func TestLiveContract(t *testing.T) {
Runner: r,
Image: lc.Image,
Settle: func(ctx context.Context, ref isolatedjob.Ref, want func(isolatedjob.State) bool) (isolatedjob.Job, error) {
- return settle(ctx, r, ref, want, 5*time.Minute)
+ return settle(ctx, r, ref, want, 5*time.Minute, liveVisibility)
},
// GetMicrovm's state is eventually consistent (doc.go, States), and
- // ListMicrovms is a separate read that can trail it. 30 s is a
- // guess for Task 17 to replace with what it measures.
- ListLag: 30 * time.Second,
+ // ListMicrovms is a separate read that can trail it. 30 s and 60 s
+ // are guesses for Task 17 to replace with what it measures.
+ ListLag: 30 * time.Second,
+ Visibility: liveVisibility,
+ CallTimeout: liveCallTimeout,
})
}
-// settle polls Get every 5 s until want holds or bound passes.
-func settle(ctx context.Context, r *Runner, ref isolatedjob.Ref, want func(isolatedjob.State) bool, bound time.Duration) (isolatedjob.Job, error) {
- deadline := time.Now().Add(bound)
+const (
+ // liveVisibility is how long a VM RunMicrovm just created may read as
+ // not found (final review A3).
+ liveVisibility = 60 * time.Second
+ // liveCallTimeout bounds each provider call of a live run.
+ liveCallTimeout = 30 * time.Second
+)
+
+// settle polls Get every 5 s until want holds or bound passes. Each Get is
+// bounded by liveCallTimeout. ErrNotFound is read again for the first
+// visible of the wait, as a new VM may not show yet, unless want accepts a
+// terminal state: to a caller waiting for the VM to end, gone is an answer.
+func settle(ctx context.Context, r *Runner, ref isolatedjob.Ref, want func(isolatedjob.State) bool, bound, visible time.Duration) (isolatedjob.Job, error) {
+ start := time.Now()
+ deadline := start.Add(bound)
+ waitsForEnd := want(isolatedjob.StateStopped) || want(isolatedjob.StateFailed)
for {
- job, err := r.Get(ctx, ref)
- if err != nil && !errors.Is(err, isolatedjob.ErrThrottled) {
+ callCtx, cancel := context.WithTimeout(ctx, liveCallTimeout)
+ job, err := r.Get(callCtx, ref)
+ cancel()
+ notYet := errors.Is(err, isolatedjob.ErrNotFound) && !waitsForEnd && time.Since(start) < visible
+ if err != nil && !errors.Is(err, isolatedjob.ErrThrottled) && !notYet {
return job, err
}
if err == nil && want(job.State) {
diff --git a/internal/isolatedjob/fake_test.go b/internal/isolatedjob/fake_test.go
index 09436056..47c45f39 100644
--- a/internal/isolatedjob/fake_test.go
+++ b/internal/isolatedjob/fake_test.go
@@ -60,6 +60,83 @@ func TestFakeContract(t *testing.T) {
isolatedjobtest.Contract(t, isolatedjobtest.Harness{Runner: f, Image: fakeDigest, Settle: settleFake(f)})
}
+// lateVisible is the Fake with reads that trail launches: Get answers
+// ErrNotFound once for each VM Launch returns, as AWS GetMicrovm can just
+// after the VM is created, its reads being eventually consistent. It also
+// counts Get and List calls whose context has no deadline.
+type lateVisible struct {
+ *isolatedjob.Fake
+ mu sync.Mutex
+ seen map[isolatedjob.Ref]bool
+ hidden map[isolatedjob.Ref]bool
+ misses int
+ unbounded int
+ boundedOps int
+}
+
+func (l *lateVisible) note(ctx context.Context) {
+ _, ok := ctx.Deadline()
+ l.mu.Lock()
+ defer l.mu.Unlock()
+ if ok {
+ l.boundedOps++
+ } else {
+ l.unbounded++
+ }
+}
+
+func (l *lateVisible) List(ctx context.Context) ([]isolatedjob.Job, error) {
+ l.note(ctx)
+ return l.Fake.List(ctx)
+}
+
+func (l *lateVisible) Launch(ctx context.Context, s isolatedjob.LaunchSpec) (isolatedjob.Ref, error) {
+ ref, err := l.Fake.Launch(ctx, s)
+ if err == nil {
+ l.mu.Lock()
+ if !l.seen[ref] {
+ l.seen[ref], l.hidden[ref] = true, true
+ }
+ l.mu.Unlock()
+ }
+ return ref, err
+}
+
+func (l *lateVisible) Get(ctx context.Context, ref isolatedjob.Ref) (isolatedjob.Job, error) {
+ l.note(ctx)
+ l.mu.Lock()
+ hide := l.hidden[ref]
+ if hide {
+ delete(l.hidden, ref)
+ l.misses++
+ }
+ l.mu.Unlock()
+ if hide {
+ return isolatedjob.Job{}, fmt.Errorf("%w: %s is not visible yet", isolatedjob.ErrNotFound, ref)
+ }
+ return l.Fake.Get(ctx, ref)
+}
+
+// TestFakeContractLateVisibility runs the contract against a runner whose
+// Get misses every new VM once (Task 17, final review A3): the contract
+// waits for the VM to appear, within Harness.Visibility, before it reads it.
+// Every Get and List the contract makes carries a deadline, so a stalled
+// provider read fails a live run rather than hanging it.
+func TestFakeContractLateVisibility(t *testing.T) {
+ f, _ := newFake(t)
+ lv := &lateVisible{Fake: f, seen: map[isolatedjob.Ref]bool{}, hidden: map[isolatedjob.Ref]bool{}}
+ isolatedjobtest.Contract(t, isolatedjobtest.Harness{Runner: lv, Image: fakeDigest, Settle: settleFake(f),
+ Visibility: 200 * time.Millisecond})
+ lv.mu.Lock()
+ defer lv.mu.Unlock()
+ if lv.misses == 0 {
+ t.Fatal("no Get answered ErrNotFound for a new VM; the test checked nothing")
+ }
+ if lv.unbounded != 0 || lv.boundedOps == 0 {
+ t.Fatalf("contract made %d Get or List calls without a deadline (%d with one), want none without", lv.unbounded, lv.boundedOps)
+ }
+}
+
func mustLaunch(t *testing.T, f *isolatedjob.Fake, s isolatedjob.LaunchSpec) isolatedjob.Ref {
t.Helper()
ref, err := f.Launch(context.Background(), s)
diff --git a/internal/isolatedjob/isolatedjobtest/contract.go b/internal/isolatedjob/isolatedjobtest/contract.go
index c39457a6..949419b3 100644
--- a/internal/isolatedjob/isolatedjobtest/contract.go
+++ b/internal/isolatedjob/isolatedjobtest/contract.go
@@ -21,21 +21,81 @@ type Harness struct {
// Settle waits until the VM's state satisfies want and returns the job
// as last read. It returns an error when the VM cannot be read (which
// may wrap isolatedjob.ErrNotFound) or when the state does not arrive in
- // the implementation's own time bound.
+ // the implementation's own time bound, and it bounds each read it makes.
Settle func(ctx context.Context, ref isolatedjob.Ref, want func(isolatedjob.State) bool) (isolatedjob.Job, error)
// ListLag is how long List may take to show a VM that Get already shows
// as running. Zero means at once, as for the fake.
ListLag time.Duration
+ // Visibility is how long Get may answer ErrNotFound for a VM that
+ // Launch has just returned, as a provider whose reads are eventually
+ // consistent does. It bounds only the wait for a new VM to appear, never
+ // a wait for a state. Zero means at once, as for the fake.
+ Visibility time.Duration
+ // CallTimeout bounds each Runner call the contract makes, so a stalled
+ // request fails the run instead of hanging it. Zero means
+ // DefaultCallTimeout.
+ CallTimeout time.Duration
+}
+
+// DefaultCallTimeout bounds each Runner call when the Harness sets no
+// CallTimeout.
+const DefaultCallTimeout = 30 * time.Second
+
+// call returns the context for one Runner call, bounded by CallTimeout.
+func (h Harness) call() (context.Context, context.CancelFunc) {
+ d := h.CallTimeout
+ if d <= 0 {
+ d = DefaultCallTimeout
+ }
+ return context.WithTimeout(context.Background(), d)
+}
+
+func (h Harness) launch(s isolatedjob.LaunchSpec) (isolatedjob.Ref, error) {
+ ctx, cancel := h.call()
+ defer cancel()
+ return h.Runner.Launch(ctx, s)
+}
+
+func (h Harness) get(ref isolatedjob.Ref) (isolatedjob.Job, error) {
+ ctx, cancel := h.call()
+ defer cancel()
+ return h.Runner.Get(ctx, ref)
+}
+
+func (h Harness) list() ([]isolatedjob.Job, error) {
+ ctx, cancel := h.call()
+ defer cancel()
+ return h.Runner.List(ctx)
+}
+
+func (h Harness) stop(ref isolatedjob.Ref, reason string) error {
+ ctx, cancel := h.call()
+ defer cancel()
+ return h.Runner.Stop(ctx, ref, reason)
+}
+
+func (h Harness) suspend(ref isolatedjob.Ref) error {
+ ctx, cancel := h.call()
+ defer cancel()
+ return h.Runner.Suspend(ctx, ref)
+}
+
+func (h Harness) resume(ref isolatedjob.Ref) error {
+ ctx, cancel := h.call()
+ defer cancel()
+ return h.Runner.Resume(ctx, ref)
}
// contractLifetime keeps VMs launched by a live run short.
const contractLifetime = 15 * time.Minute
// Contract checks the behaviour every Runner must share. It uses only the
-// Runner interface, h.Settle and h.ListLag, so it runs against the fake and, behind the
-// hostedlive build tag, against a real provider. Each subtest stops the VMs it
-// launched. Attempt IDs carry a random part, so a second run against a
-// provider that remembers client tokens does not get the first run's VMs.
+// Runner interface and the Harness, so it runs against the fake and, behind
+// the hostedlive build tag, against a real provider. Each subtest stops the
+// VMs it launched. Attempt IDs carry a random part, so a second run against
+// a provider that remembers client tokens does not get the first run's VMs.
+// Every VM the contract launches must show in Get within h.Visibility
+// before any check reads it.
func Contract(t *testing.T, h Harness) {
t.Helper()
run := randomHex(t, 6)
@@ -55,15 +115,16 @@ func Contract(t *testing.T, h Harness) {
}
launch := func(t *testing.T, s isolatedjob.LaunchSpec) isolatedjob.Ref {
t.Helper()
- ref, err := h.Runner.Launch(context.Background(), s)
+ ref, err := h.launch(s)
if err != nil {
t.Fatalf("Launch(%s) = %v, want a ref", s.AttemptID, err)
}
t.Cleanup(func() {
- if err := h.Runner.Stop(context.Background(), ref, "contract cleanup"); err != nil {
+ if err := h.stop(ref, "contract cleanup"); err != nil {
t.Errorf("cleanup Stop(%s) = %v", ref, err)
}
})
+ appear(t, h, ref)
return ref
}
settle := func(t *testing.T, ref isolatedjob.Ref, want isolatedjob.State) isolatedjob.Job {
@@ -78,14 +139,14 @@ func Contract(t *testing.T, h Harness) {
t.Run("LaunchIdempotentOnAttempt", func(t *testing.T) {
s := spec()
first := launch(t, s)
- again, err := h.Runner.Launch(context.Background(), s)
+ again, err := h.launch(s)
if err != nil {
t.Fatalf("second Launch(%s) = %v, want the first ref", s.AttemptID, err)
}
if again != first {
t.Fatalf("second Launch(%s) = %s, want %s: one attempt must be one VM", s.AttemptID, again, first)
}
- job, err := h.Runner.Get(context.Background(), first)
+ job, err := h.get(first)
if err != nil {
t.Fatalf("Get(%s) = %v", first, err)
}
@@ -99,7 +160,7 @@ func Contract(t *testing.T, h Harness) {
first := launch(t, s)
changed := s
changed.Bootstrap = []byte("another-bootstrap")
- ref, err := h.Runner.Launch(context.Background(), changed)
+ ref, err := h.launch(changed)
if !errors.Is(err, isolatedjob.ErrConflict) {
t.Fatalf("Launch with the same attempt and a new bootstrap = (%q, %v), want ErrConflict", ref, err)
}
@@ -111,7 +172,7 @@ func Contract(t *testing.T, h Harness) {
t.Run("StopTwice", func(t *testing.T) {
ref := launch(t, spec())
for i := 1; i <= 2; i++ {
- if err := h.Runner.Stop(context.Background(), ref, "contract stop twice"); err != nil {
+ if err := h.stop(ref, "contract stop twice"); err != nil {
t.Fatalf("Stop %d of %s = %v, want nil", i, ref, err)
}
}
@@ -119,21 +180,21 @@ func Contract(t *testing.T, h Harness) {
t.Run("StopUnknownRef", func(t *testing.T) {
ref := isolatedjob.Ref("contract-never-launched-" + run)
- if err := h.Runner.Stop(context.Background(), ref, "contract unknown ref"); err != nil {
+ if err := h.stop(ref, "contract unknown ref"); err != nil {
t.Fatalf("Stop(%s) of a ref the provider never held = %v, want nil", ref, err)
}
})
t.Run("GetAfterStop", func(t *testing.T) {
ref := launch(t, spec())
- if err := h.Runner.Stop(context.Background(), ref, "contract get after stop"); err != nil {
+ if err := h.stop(ref, "contract get after stop"); err != nil {
t.Fatalf("Stop(%s) = %v", ref, err)
}
_, err := h.Settle(context.Background(), ref, isolatedjob.State.Terminal)
if err != nil && !errors.Is(err, isolatedjob.ErrNotFound) {
t.Fatalf("Settle(%s, terminal) = %v, want a terminal state or ErrNotFound", ref, err)
}
- job, err := h.Runner.Get(context.Background(), ref)
+ job, err := h.get(ref)
switch {
case errors.Is(err, isolatedjob.ErrNotFound):
case err != nil:
@@ -146,11 +207,11 @@ func Contract(t *testing.T, h Harness) {
t.Run("SuspendResumeRoundTrip", func(t *testing.T) {
ref := launch(t, spec())
settle(t, ref, isolatedjob.StateRunning)
- if err := h.Runner.Suspend(context.Background(), ref); err != nil {
+ if err := h.suspend(ref); err != nil {
t.Fatalf("Suspend(%s) = %v", ref, err)
}
settle(t, ref, isolatedjob.StateSuspended)
- if err := h.Runner.Resume(context.Background(), ref); err != nil {
+ if err := h.resume(ref); err != nil {
t.Fatalf("Resume(%s) = %v", ref, err)
}
settle(t, ref, isolatedjob.StateRunning)
@@ -159,10 +220,10 @@ func Contract(t *testing.T, h Harness) {
t.Run("ResumeRunningIsNoop", func(t *testing.T) {
ref := launch(t, spec())
settle(t, ref, isolatedjob.StateRunning)
- if err := h.Runner.Resume(context.Background(), ref); err != nil {
+ if err := h.resume(ref); err != nil {
t.Fatalf("Resume(%s) of a running VM = %v, want nil", ref, err)
}
- job, err := h.Runner.Get(context.Background(), ref)
+ job, err := h.get(ref)
if err != nil {
t.Fatalf("Get(%s) = %v", ref, err)
}
@@ -176,20 +237,20 @@ func Contract(t *testing.T, h Harness) {
// that always comes back empty cannot pass the check below.
known := launch(t, spec())
settle(t, known, isolatedjob.StateRunning)
- before := listUntil(t, h.Runner, known, h.ListLag)
+ before := listUntil(t, h, known)
s := spec()
s.Bootstrap = make([]byte, h.Runner.Limits().MaxBootstrapBytes+1)
for i := range s.Bootstrap {
s.Bootstrap[i] = 'x'
}
- ref, err := h.Runner.Launch(context.Background(), s)
+ ref, err := h.launch(s)
if ref != "" {
- t.Cleanup(func() { _ = h.Runner.Stop(context.Background(), ref, "contract cleanup") })
+ t.Cleanup(func() { _ = h.stop(ref, "contract cleanup") })
}
if !errors.Is(err, isolatedjob.ErrBootstrapTooLarge) {
t.Fatalf("Launch with a %d-byte bootstrap = (%q, %v), want ErrBootstrapTooLarge", len(s.Bootstrap), ref, err)
}
- after := listRefs(t, h.Runner)
+ after := listRefs(t, h)
if !slices.Contains(after, known) {
t.Fatalf("List after a refused launch = %v, want it still to hold running VM %s", after, known)
}
@@ -211,9 +272,34 @@ func Contract(t *testing.T, h Harness) {
})
}
-func listRefs(t *testing.T, r isolatedjob.Runner) []isolatedjob.Ref {
+// appear reads ref with Get until the provider shows it, for at most
+// h.Visibility after the first read. Only ErrNotFound and ErrThrottled are
+// read again; any other error, or ref still missing when the time is up,
+// fails the test. Zero Visibility is one read. Reads are spaced a
+// twentieth of Visibility apart, at most a second, so a short window in a
+// test costs little.
+func appear(t *testing.T, h Harness, ref isolatedjob.Ref) {
+ t.Helper()
+ deadline := time.Now().Add(h.Visibility)
+ gap := min(time.Second, max(h.Visibility/20, time.Millisecond))
+ for {
+ _, err := h.get(ref)
+ if err == nil {
+ return
+ }
+ if !errors.Is(err, isolatedjob.ErrNotFound) && !errors.Is(err, isolatedjob.ErrThrottled) {
+ t.Fatalf("Get(%s) of a VM Launch just returned = %v", ref, err)
+ }
+ if !time.Now().Before(deadline) {
+ t.Fatalf("Get(%s) = %v for %v after Launch returned it, want the VM to appear", ref, err, h.Visibility)
+ }
+ time.Sleep(min(gap, time.Until(deadline)))
+ }
+}
+
+func listRefs(t *testing.T, h Harness) []isolatedjob.Ref {
t.Helper()
- jobs, err := r.List(context.Background())
+ jobs, err := h.list()
if err != nil {
t.Fatalf("List = %v", err)
}
@@ -224,13 +310,15 @@ func listRefs(t *testing.T, r isolatedjob.Runner) []isolatedjob.Ref {
return refs
}
-// listUntil lists until the result holds a live job for ref, for at most lag,
-// and returns the refs of that list. It fails the test when ref never shows.
-func listUntil(t *testing.T, r isolatedjob.Runner, ref isolatedjob.Ref, lag time.Duration) []isolatedjob.Ref {
+// listUntil lists until the result holds a live job for ref, for at most
+// h.ListLag, and returns the refs of that list. It fails the test when ref
+// never shows. Each List is bounded by h.CallTimeout, so a stalled request
+// overruns ListLag by at most that; zero ListLag is one List.
+func listUntil(t *testing.T, h Harness, ref isolatedjob.Ref) []isolatedjob.Ref {
t.Helper()
- deadline := time.Now().Add(lag)
+ deadline := time.Now().Add(h.ListLag)
for {
- jobs, err := r.List(context.Background())
+ jobs, err := h.list()
if err != nil {
t.Fatalf("List = %v", err)
}
@@ -244,7 +332,7 @@ func listUntil(t *testing.T, r isolatedjob.Runner, ref isolatedjob.Ref, lag time
return refs
}
if !time.Now().Before(deadline) {
- t.Fatalf("List = %v after %v, want it to hold running VM %s", refs, lag, ref)
+ t.Fatalf("List = %v after %v, want it to hold running VM %s", refs, h.ListLag, ref)
}
time.Sleep(min(time.Second, time.Until(deadline)))
}