host: seeded simulation of offline run over the two boxes (Task 7)
Fifty seeds (OFFLINE_RUN_SIM_SEEDS for more) of Step over the two-box world with lost replies, missed serial wakes, hand doors, take-overs and short budgets. Each seed checks the run names only its own move, one writer per step, no abort after open, and a cleared or named end. Plus a witness that a take-over before the first read stops the run.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+249
−8
internal/host/movesoffline_e2e_test.go+249 −8
diff --git a/internal/host/movesoffline_e2e_test.go b/internal/host/movesoffline_e2e_test.go| index b4ea4e737..c448834a0 100644 |
| --- a/internal/host/movesoffline_e2e_test.go |
| +++ b/internal/host/movesoffline_e2e_test.go |
| @@ -13,6 +13,7 @@ import ( |
| "go/parser" |
| "go/token" |
| "io" |
| + "math/rand/v2" |
| "net/http" |
| "net/http/httptest" |
| "os" |
| @@ -20,6 +21,7 @@ import ( |
| "reflect" |
| "slices" |
| "sort" |
| + "strconv" |
| "strings" |
| "sync" |
| "testing" |
| @@ -1985,8 +1987,8 @@ func (w *e2eWorld) snap() e2eRecSnap { |
| // e2eRunDoors are offlinerun.Doors over the console's real HTTP doors, as |
| // the CLI's runDoors send them: the bound move's id in the body, the region |
| -// on stage and start; a 2xx accepted, a 4xx refused with the console's |
| -// sentence, anything else no answer. lose, when set, throws a reply away |
| +// on stage and start; a 2xx accepted with the move its view names, a 4xx |
| +// refused, anything else no answer. lose, when set, throws a reply away |
| // after the door answered it (the write, if any, has committed). Every |
| // POST is kept with the record either side of it. |
| type e2eRunDoors struct { |
| @@ -2058,13 +2060,15 @@ func (d *e2eRunDoors) post(hand bool, verb, id string) (int, string) { |
| return code, out |
| } |
| -func (d *e2eRunDoors) answer(code int) offlinerun.Answer { |
| +func (d *e2eRunDoors) answer(code int, out string) offlinerun.Answer { |
| if p := d.posts[len(d.posts)-1]; p.lost { |
| return offlinerun.Answer{} |
| } |
| switch { |
| case code >= 200 && code <= 299: |
| - return offlinerun.Answer{Accepted: true} |
| + var v offlinerun.View |
| + _ = json.Unmarshal([]byte(out), &v) |
| + return offlinerun.Answer{Accepted: true, ID: v.ID} |
| case code >= 400 && code <= 499: |
| return offlinerun.Answer{Refused: true} |
| } |
| @@ -2072,13 +2076,11 @@ func (d *e2eRunDoors) answer(code int) offlinerun.Answer { |
| } |
| func (d *e2eRunDoors) Post(_ context.Context, verb, id string) offlinerun.Answer { |
| - code, _ := d.post(false, verb, id) |
| - return d.answer(code) |
| + return d.answer(d.post(false, verb, id)) |
| } |
| func (d *e2eRunDoors) Nudge(_ context.Context, id string) offlinerun.Answer { |
| - code, _ := d.post(false, "nudge", id) |
| - return d.answer(code) |
| + return d.answer(d.post(false, "nudge", id)) |
| } |
| // runVerbs lists the run's POSTs as "<verb>" in order. |
| @@ -2334,6 +2336,38 @@ func TestOfflineMoveRunE2E(t *testing.T) { |
| w.requireTarget() |
| }) |
| } |
| + // A second operator takes the host over between the run's stage and its |
| + // next read: aborts the run's move, finishes it once the boxes have |
| + // reverted, and stages their own. The run is bound to the move its |
| + // stage made, so it stops and sends nothing for theirs. |
| + t.Run("a second operator takes the host over before the run's first read", func(t *testing.T) { |
| + w := newE2EWorld(t, landOpts{}) |
| + r := w.newRun(nil) |
| + taken := false |
| + r.between = func() { |
| + if taken { |
| + return |
| + } |
| + taken = true |
| + for _, verb := range []string{offlinerun.VerbAbort, offlinerun.VerbFinish, offlinerun.VerbStage} { |
| + if code, out := r.doors.post(true, verb, ""); code != http.StatusAccepted { |
| + t.Fatalf("hand %s = %d %s", verb, code, out) |
| + } |
| + r.boxes.settle() |
| + } |
| + } |
| + done := r.run(50) |
| + if done.Exit != offlinerun.ExitStopped || done.Reason != offlinerun.ReasonOtherMove { |
| + t.Fatalf("the run ended %+v; POSTs %v", done, r.doors.runVerbs()) |
| + } |
| + if got := strings.Join(r.doors.runVerbs(), ","); got != "stage" { |
| + t.Fatalf("the run posted %s", got) |
| + } |
| + mine, theirs := r.doors.posts[0].after.id, w.snap().id |
| + if mine == "" || theirs == "" || mine == theirs { |
| + t.Fatalf("the run's move %q, the record's %q", mine, theirs) |
| + } |
| + }) |
| // Without the nudge (NudgeAfter past every budget) the same miss stalls |
| // the stop: nothing moves until the stop's budget aborts the move, and |
| // the abort's bump reverts it. |
| @@ -2373,3 +2407,210 @@ func TestOfflineMoveRunE2E(t *testing.T) { |
| } |
| }) |
| } |
| + |
| +// TestOfflineMoveRunSimulation is Task 7's seeded simulation: the run's |
| +// Step over the two-box world as TestOfflineMoveRunE2E drives it, with, at |
| +// points each seed picks, replies lost after the door answered (the write, |
| +// if any, committed), serial wakes a box misses, and a second operator |
| +// posting any door by hand between ticks (no id, as the hand verbs send) or |
| +// taking the host over (abort, finish, a stage of their own). Half the seeds |
| +// run on short budgets, so the run's own aborts land at random points too. |
| +// Every seed must keep: |
| +// - the run's POSTs name its own move: the first is the stage that made |
| +// it, every later one carries its id, never another move's; |
| +// - one writer per step: no record transition is written twice; |
| +// - no abort from the run once the move was open, and a nudge never |
| +// writes the record; |
| +// - the run ends cleared, done or aborted as last says (exit 0 or 3), or |
| +// stopped (exit 4) with the record still carrying a move in a named |
| +// state. |
| +// |
| +// CI runs offlineRunSimSeeds seeds; OFFLINE_RUN_SIM_SEEDS=<n> runs n. |
| +func TestOfflineMoveRunSimulation(t *testing.T) { |
| + seeds := offlineRunSimSeeds |
| + if v := os.Getenv("OFFLINE_RUN_SIM_SEEDS"); v != "" { |
| + n, err := strconv.Atoi(v) |
| + if err != nil || n < 1 { |
| + t.Fatalf("OFFLINE_RUN_SIM_SEEDS=%q", v) |
| + } |
| + seeds = n |
| + } |
| + var all offlineRunSimTally |
| + for seed := 1; seed <= seeds; seed++ { |
| + t.Run(fmt.Sprintf("seed %d", seed), func(t *testing.T) { |
| + all.add(offlineRunSim(t, uint64(seed))) |
| + }) |
| + } |
| + t.Logf("%d seeds: %+v", seeds, all) |
| + // The faults happened: a simulation that injected none proves nothing. |
| + if !t.Failed() && seeds >= offlineRunSimSeeds && |
| + (all.done == 0 || all.aborted == 0 || all.stopped == 0 || all.runAborts == 0 || all.lost == 0 || all.missed == 0 || all.handWrites == 0) { |
| + t.Fatalf("the seeds did not exercise every fault and ending: %+v", all) |
| + } |
| +} |
| + |
| +// offlineRunSimSeeds is how many seeds CI runs. |
| +const offlineRunSimSeeds = 50 |
| + |
| +// The simulation's odds: a reply lost, a serial wake missed, and a hand |
| +// POST between two ticks. |
| +const ( |
| + simLoseReply = 0.15 |
| + simMissWake = 0.2 |
| + simHandDoor = 0.04 |
| + simTakeOver = 0.005 |
| +) |
| + |
| +type offlineRunSimTally struct { |
| + done, aborted, stopped int |
| + lost, missed, handPosts int |
| + handWrites, nudges, ticks int |
| + runAborts int |
| +} |
| + |
| +func (a *offlineRunSimTally) add(b offlineRunSimTally) { |
| + a.done += b.done |
| + a.aborted += b.aborted |
| + a.stopped += b.stopped |
| + a.lost += b.lost |
| + a.missed += b.missed |
| + a.handPosts += b.handPosts |
| + a.handWrites += b.handWrites |
| + a.nudges += b.nudges |
| + a.ticks += b.ticks |
| + a.runAborts += b.runAborts |
| +} |
| + |
| +func offlineRunSim(t *testing.T, seed uint64) offlineRunSimTally { |
| + rng := rand.New(rand.NewPCG(seed, 0x0ff11e)) |
| + w := newE2EWorld(t, landOpts{}) |
| + r := w.newRun(func(*offBox) bool { return rng.Float64() < simMissWake }) |
| + r.doors.lose = func(string) bool { return rng.Float64() < simLoseReply } |
| + hands := []string{offlinerun.VerbStage, offlinerun.VerbStart, offlinerun.VerbSeal, offlinerun.VerbFlip, |
| + offlinerun.VerbOpen, offlinerun.VerbFinish, offlinerun.VerbAbort} |
| + r.between = func() { |
| + switch x := rng.Float64(); { |
| + case x < simTakeOver: |
| + // A second operator takes the host over: aborts the move, |
| + // finishes it once the boxes have reverted, and stages a move |
| + // of their own, all before the run's next read. |
| + for _, verb := range []string{offlinerun.VerbAbort, offlinerun.VerbFinish, offlinerun.VerbStage} { |
| + r.doors.post(true, verb, "") |
| + r.boxes.settle() |
| + } |
| + case x < simTakeOver+simHandDoor: |
| + r.doors.post(true, hands[rng.IntN(len(hands))], "") |
| + } |
| + } |
| + // Half the seeds run on budgets short enough for the run's own aborts |
| + // (a step or the time offline over its limit) to land at random points. |
| + if rng.IntN(2) == 0 { |
| + d := func() time.Duration { return time.Duration(10+rng.IntN(110)) * time.Second } |
| + r.policy.Stop, r.policy.Seal, r.policy.Landing, r.policy.OpenCheck = d(), d(), d(), d() |
| + r.policy.Offline = d() + d() |
| + } |
| + done := r.run(3000) |
| + |
| + var tally offlineRunSimTally |
| + tally.missed, tally.ticks = r.boxes.missed, r.ticks |
| + var trail []string |
| + for _, p := range r.doors.posts { |
| + who := "run" |
| + if p.hand { |
| + who = "hand" |
| + tally.handPosts++ |
| + if p.before.raw != p.after.raw { |
| + tally.handWrites++ |
| + } |
| + } |
| + if p.lost { |
| + tally.lost++ |
| + } |
| + if p.verb == "nudge" && !p.hand { |
| + tally.nudges++ |
| + } |
| + trail = append(trail, fmt.Sprintf("%s %s %s %d %s->%s", who, p.verb, p.id, p.code, p.before, p.after)) |
| + } |
| + fail := func(format string, args ...any) { |
| + t.Helper() |
| + t.Fatalf("seed %d: %s\nended %+v after %d ticks\n%s", seed, fmt.Sprintf(format, args...), done, r.ticks, strings.Join(trail, "\n")) |
| + } |
| + |
| + // The run's own move: its first POST is the stage that made it. |
| + var mine string |
| + opened := map[string]bool{} |
| + written := map[string]bool{} |
| + for i, p := range r.doors.posts { |
| + if !p.hand && mine == "" { |
| + if i != 0 || p.verb != offlinerun.VerbStage || p.id != "" || p.before.state != "none" || p.after.id == "" { |
| + fail("the run's first POST is %s %q on %s", p.verb, p.id, p.before) |
| + } |
| + mine = p.after.id |
| + } else if !p.hand && p.id != mine { |
| + fail("the run posted %s naming %q; its move is %s", p.verb, p.id, mine) |
| + } |
| + if !p.hand && p.verb == offlinerun.VerbAbort { |
| + tally.runAborts++ |
| + if opened[p.before.id] || p.before.opened { |
| + fail("the run posted abort on %s, which was open", p.before) |
| + } |
| + } |
| + if p.before.raw != p.after.raw { |
| + if p.verb == "nudge" { |
| + fail("a nudge wrote the record: %s -> %s", p.before, p.after) |
| + } |
| + // One writer per step: a transition is written once. A |
| + // restage (staging to staging) is the one repeatable write. |
| + if step := p.before.String() + "->" + p.after.String(); p.before.state != p.after.state || p.before.id != p.after.id { |
| + if written[step] { |
| + fail("%s was written twice", step) |
| + } |
| + written[step] = true |
| + } |
| + } |
| + for _, s := range []e2eRecSnap{p.before, p.after} { |
| + if s.opened || s.state == provision.OfflineOpen || s.state == provision.OfflineFinishing { |
| + opened[s.id] = true |
| + } |
| + } |
| + } |
| + |
| + end := w.snap() |
| + switch done.Exit { |
| + case offlinerun.ExitDone: |
| + tally.done++ |
| + if done.Reason != offlinerun.ReasonDone || end.state != "none" || end.last == nil || end.last.ID != mine || end.last.Outcome != "done" { |
| + fail("exit 0 (%s) with the record %s, last %+v", done.Reason, end, end.last) |
| + } |
| + r.boxes.settle() |
| + if rt := w.uk.row(offHost); rt == nil || rt.Status != "ready" || rt.HoldOp != "" { |
| + fail("done, but uk-box's row is %+v", rt) |
| + } |
| + case offlinerun.ExitAborted: |
| + tally.aborted++ |
| + if done.Reason != offlinerun.ReasonAborted || end.state != "none" || end.last == nil || end.last.ID != mine || end.last.Outcome != "aborted" { |
| + fail("exit 3 (%s) with the record %s, last %+v", done.Reason, end, end.last) |
| + } |
| + if opened[mine] { |
| + fail("aborted after open") |
| + } |
| + // The edges loop's pass on the release the revert made. |
| + r.boxes.settle() |
| + w.publish(w.ie) |
| + if rt := w.ie.row(offHost); rt == nil || rt.HoldOp != "" || !w.ie.lists(offHost) { |
| + fail("aborted, but ie-box's row is %+v (listed %v)", rt, w.ie.lists(offHost)) |
| + } |
| + case offlinerun.ExitStopped: |
| + tally.stopped++ |
| + switch { |
| + case done.Reason == offlinerun.ReasonOtherMove && end.id != "" && end.id != mine: |
| + case done.Reason == offlinerun.ReasonProbeFailed && end.id == mine && end.state == provision.OfflineOpen: |
| + default: |
| + fail("exit 4 (%s) with the record %s", done.Reason, end) |
| + } |
| + default: |
| + fail("exit %d", done.Exit) |
| + } |
| + t.Logf("seed %d: exit %d %s in %d ticks; run %s", seed, done.Exit, done.Reason, r.ticks, strings.Join(r.doors.runVerbs(), ",")) |
| + return tally |
| +} |