isolatedjob: fake resume reports suspended until the next tick
Controller ruling: the fake mirrors AWS, which keeps a VM SUSPENDED while its /resume hook runs. Resume is accepted at once, Get shows suspended until the next Tick moves the VM to running, a second Resume in that window changes nothing, and Stop in it drops the pending resume.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+55
−6
docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md+2 −1internal/isolatedjob/fake.go+17 −5internal/isolatedjob/fake_test.go+36 −0
diff --git a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md| index b047ac8b..a6a639e2 100644 |
| --- a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md |
| +++ b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md |
| @@ -103,7 +103,7 @@ Every guard now has a mutation. `SystemClock` takes its wall and elapsed sources |
| ## Task 6 |
| -`isolatedjob.Fake` (an in-memory `Runner` and `NetworkAuditor`, limits from `SpikeLimits()`) and `isolatedjobtest` (`FakeClock`, `Harness`, `Contract`). Faults are one-shot and queue in order per op. `Tick` steps starting, suspending and stopping on, then the backstop stops VMs whose lifetime since launch (suspended time included while `SuspendUsesLifetime`) has reached their `MaxLifetime`. Resume moves a suspended VM straight to running; Resume of a running VM is a no-op. |
| +`isolatedjob.Fake` (an in-memory `Runner` and `NetworkAuditor`, limits from `SpikeLimits()`) and `isolatedjobtest` (`FakeClock`, `Harness`, `Contract`). Faults are one-shot and queue in order per op. `Tick` steps starting, suspending and stopping on, then the backstop stops VMs whose lifetime since launch (suspended time included while `SuspendUsesLifetime`) has reached their `MaxLifetime`. Resume of a suspended VM is accepted but the VM reports `suspended` until the next `Tick` moves it to running, as AWS keeps SUSPENDED while its /resume hook runs (controller ruling after the first commit); a second Resume in that window is accepted and changes nothing, Stop in it works as usual, and Resume of a running VM is a no-op. |
| | Mutation | Command | Observed failure | Restored | |
| | --- | --- | --- | --- | |
| @@ -115,3 +115,4 @@ Every guard now has a mutation. `SystemClock` takes its wall and elapsed sources |
| | unknown state ignored (`case FaultUnknownState + "x":` in Get) | `go test ./internal/isolatedjob -run 'TestFakeFaults' -count=1` | `fake_test.go:166: Get(vm-0001).State = running, want unknown` | yes, passes | |
| | age unknown ignored (`case FaultAgeUnknown + "x":` in Get) | `go test ./internal/isolatedjob -run 'TestFakeFaults' -count=1` | `fake_test.go:175: faulted Get = {... AgeKnown:true}, want AgeKnown false and still running` | yes, passes | |
| | faults not used up (`f.faults[op] = q[0:]`) | `go test ./internal/isolatedjob -run 'TestFakeFaults' -count=1` | `fake_test.go:121: Launch(a1) = isolatedjob: throttled: fake launch` (and every other row) | yes, passes | |
| +| resume straight to running (`vm.state = StateRunning` in place of `vm.resuming = true` in Resume) | `go test ./internal/isolatedjob -run 'TestFakeLifecycle' -count=1` | `fake_test.go:312: Get(vm-0001).State = running, want suspended` in `TestFakeLifecycle/resume_reports_suspended_until_the_next_tick` | yes, passes | |
diff --git a/internal/isolatedjob/fake.go b/internal/isolatedjob/fake.go| index 4e738a93..5e0c068a 100644 |
| --- a/internal/isolatedjob/fake.go |
| +++ b/internal/isolatedjob/fake.go |
| @@ -62,9 +62,12 @@ func SpikeLimits() Limits { |
| // |
| // Lifecycle. Launch creates a VM in starting. Tick moves every VM one step: |
| // starting to running, suspending to suspended, stopping to stopped. Suspend |
| -// needs a running VM and moves it to suspending. Resume moves a suspended VM |
| -// straight to running (AWS has no resuming state) and does nothing to a |
| -// running one. Stop moves any live VM to stopping and does nothing to a |
| +// needs a running VM and moves it to suspending. Resume of a suspended VM is |
| +// accepted at once but the VM is still reported suspended until the next |
| +// Tick moves it to running, as AWS keeps a VM SUSPENDED while its /resume |
| +// hook runs; a second Resume in that window is accepted and changes nothing. |
| +// Resume of a running VM does nothing. Stop moves any live VM to stopping, |
| +// dropping a pending resume, and does nothing to a |
| // stopped VM or a ref it never held. Other calls on a VM in the wrong state |
| // return ErrConflict; calls on an unknown ref return ErrNotFound. Stopped VMs |
| // stay visible to Get and List. |
| @@ -113,6 +116,8 @@ type fakeVM struct { |
| paused time.Duration // finished suspensions |
| pausedSince time.Duration // Mono when the current suspension began |
| isPaused bool |
| + |
| + resuming bool // Resume accepted; the next Tick moves suspended to running |
| } |
| // NewFake returns a Fake reading time from c and reporting limits l. Pass |
| @@ -297,7 +302,9 @@ func (f *Fake) Suspend(ctx context.Context, ref Ref) error { |
| return finish(fl, OpSuspend, err) |
| } |
| -// Resume moves a suspended VM to running. A running VM is left alone. |
| +// Resume asks a suspended VM to resume; it reports suspended until the next |
| +// Tick. Suspended time ends when the resume is accepted. A running VM, or one |
| +// whose resume is already pending, is left alone. |
| func (f *Fake) Resume(ctx context.Context, ref Ref) error { |
| fl, err := f.begin(ctx, OpResume) |
| if err != nil { |
| @@ -312,7 +319,7 @@ func (f *Fake) Resume(ctx context.Context, ref Ref) error { |
| case vm.state != StateSuspended: |
| err = fmt.Errorf("%w: resume %s while %s", ErrConflict, ref, vm.state) |
| default: |
| - vm.state = StateRunning |
| + vm.resuming = true |
| vm.unpause(f.clock.Mono()) |
| } |
| return finish(fl, OpResume, err) |
| @@ -349,6 +356,7 @@ func (f *Fake) Stop(ctx context.Context, ref Ref, reason string) error { |
| func (f *Fake) stopLocked(vm *fakeVM, reason string) { |
| vm.state = StateStopping |
| vm.reason = reason |
| + vm.resuming = false |
| vm.unpause(f.clock.Mono()) |
| } |
| @@ -389,6 +397,10 @@ func (f *Fake) Tick() { |
| vm.state = StateRunning |
| case StateSuspending: |
| vm.state = StateSuspended |
| + case StateSuspended: |
| + if vm.resuming { |
| + vm.state, vm.resuming = StateRunning, false |
| + } |
| case StateStopping: |
| vm.state = StateStopped |
| } |
diff --git a/internal/isolatedjob/fake_test.go b/internal/isolatedjob/fake_test.go| index 0ebf79e2..d57dfb71 100644 |
| --- a/internal/isolatedjob/fake_test.go |
| +++ b/internal/isolatedjob/fake_test.go |
| @@ -271,6 +271,7 @@ func TestFakeLifecycle(t *testing.T) { |
| if err := f.Resume(ctx, ref); err != nil { |
| t.Fatal(err) |
| } |
| + f.Tick() |
| wantState(t, f, ref, isolatedjob.StateRunning) |
| if err := f.Stop(ctx, ref, "done"); err != nil { |
| t.Fatal(err) |
| @@ -292,6 +293,41 @@ func TestFakeLifecycle(t *testing.T) { |
| t.Fatalf("Get of an unknown ref = %v, want ErrNotFound", err) |
| } |
| }) |
| + t.Run("resume reports suspended until the next tick", func(t *testing.T) { |
| + f, _ := newFake(t) |
| + suspended := func(attempt string) isolatedjob.Ref { |
| + ref := running(t, f, fakeSpec(attempt)) |
| + if err := f.Suspend(ctx, ref); err != nil { |
| + t.Fatal(err) |
| + } |
| + f.Tick() |
| + wantState(t, f, ref, isolatedjob.StateSuspended) |
| + return ref |
| + } |
| + ref := suspended("a1") |
| + for i := 1; i <= 2; i++ { |
| + if err := f.Resume(ctx, ref); err != nil { |
| + t.Fatalf("Resume %d while the resume is pending = %v, want nil", i, err) |
| + } |
| + wantState(t, f, ref, isolatedjob.StateSuspended) |
| + } |
| + f.Tick() |
| + wantState(t, f, ref, isolatedjob.StateRunning) |
| + f.Tick() |
| + wantState(t, f, ref, isolatedjob.StateRunning) |
| + |
| + // Stop while the resume is pending stops the VM; the resume is dropped. |
| + other := suspended("a2") |
| + if err := f.Resume(ctx, other); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := f.Stop(ctx, other, "stop mid-resume"); err != nil { |
| + t.Fatal(err) |
| + } |
| + wantState(t, f, other, isolatedjob.StateStopping) |
| + f.Tick() |
| + wantState(t, f, other, isolatedjob.StateStopped) |
| + }) |
| t.Run("launch replays after stop and refuses bad specs", func(t *testing.T) { |
| f, _ := newFake(t) |
| ref := mustLaunch(t, f, fakeSpec("a1")) |