carlos / platform Public

host: offline run's Step over the two boxes, woken only by the serial

offlinerun.Runner's Step drives the two-box world through the real
doors from nothing to done, the boxes' passes started only by serial
bumps. A box missing any step's bump stands still until the nudge; with
no nudge a missed stop waits out its budget and aborts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev cbff4f16a2cdd68128d8a169877074e4701a1794 parent 189e4db
1 file changed, +450 −2
  • internal/host/movesoffline_e2e_test.go +450 −2
diff --git a/internal/host/movesoffline_e2e_test.go b/internal/host/movesoffline_e2e_test.go
index 6491aaf69..b4ea4e737 100644
--- a/internal/host/movesoffline_e2e_test.go
+++ b/internal/host/movesoffline_e2e_test.go
@@ -36,6 +36,7 @@ import (
"github.com/carlosframework/platform/internal/lease"
"github.com/carlosframework/platform/internal/lease/leasetest"
"github.com/carlosframework/platform/internal/moves"
+ "github.com/carlosframework/platform/internal/moves/offlinerun"
"github.com/carlosframework/platform/internal/provision"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
@@ -202,7 +203,9 @@ type e2eWorld struct {
rs *recordingStore
base string // the doors' base URL
token string
- ieEB *activator.ExecBackend // ie-box's real lease take and restore
+ // mint is one more CLI token for the operator, as another login.
+ mint func() string
+ ieEB *activator.ExecBackend // ie-box's real lease take and restore
id string
recBefore map[string]any
@@ -354,6 +357,22 @@ func newE2EWorld(t *testing.T, opts landOpts) *e2eWorld {
lw.ukEB = &activator.ExecBackend{ServedPools: testSharedPoolEvidence, DataDir: lw.uk.dd, OptDir: lw.uk.opt, Region: "uk"}
lw.arm(lw.uk)
w := &e2eWorld{landWorld: lw, t: t, rs: rs, base: ts.URL + "/api/cli/apps/" + wd.acct + "/app/instances/" + offHost + "/move/offline", token: token}
+ w.mint = func() string {
+ t.Helper()
+ at := time.Now()
+ userCode, deviceCode, err := cst.NewCLIAuth("ops@laptop", "127.0.0.1", at)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := cst.ApproveCLIAuth(userCode, e2eAddr, at); err != nil {
+ t.Fatal(err)
+ }
+ _, token, _, err := cst.PollCLIAuth(deviceCode, at)
+ if err != nil || token == "" {
+ t.Fatalf("no CLI token: %v", err)
+ }
+ return token
+ }
if opts.poolBox {
w.pool = sw.box("uk-pool", "uk", map[string]string{"CARLOS_HIBERNATE_BUCKET": sealUK, "CARLOS_SECRETS_KEY": keyB64,
"CARLOS_HIBERNATE_REGION": sealUKRegion, "CARLOS_RELEASE_PUBKEY": pubB64, "CARLOS_POOLS": "cohort1"})
@@ -545,12 +564,18 @@ func (w *e2eWorld) door(verb, body string) (int, string) {
}
func (w *e2eWorld) do(method, u, body string) (int, string) {
+ w.t.Helper()
+ return w.doAs(w.token, method, u, body)
+}
+
+// doAs is do with the CLI token given.
+func (w *e2eWorld) doAs(token, method, u, body string) (int, string) {
w.t.Helper()
req, err := http.NewRequest(method, u, strings.NewReader(body))
if err != nil {
w.t.Fatal(err)
}
- req.Header.Set("Authorization", "Bearer "+w.token)
+ req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
@@ -1925,3 +1950,426 @@ func offlineArmedReleaser(t *testing.T) {
t.Fatal("a release that changed nothing touched the marker")
}
}
+
+// ---- carlos move offline run over the two boxes (plan 2026-11-15-offline-move-run, Task 7) ----
+
+// e2eRunPost is one POST a run or a hand made, with the record either side
+// of it.
+type e2eRunPost struct {
+ hand bool
+ verb, id string
+ code int
+ lost bool // the reply was lost after the door answered
+ before, after e2eRecSnap
+}
+
+// e2eRecSnap is the part of the record a POST is judged by.
+type e2eRecSnap struct {
+ id, state string
+ opened bool
+ raw string // the record's bytes: a write changes them
+ last *provision.MoveOutcome
+}
+
+func (s e2eRecSnap) String() string { return s.id + "@" + s.state }
+
+func (w *e2eWorld) snap() e2eRecSnap {
+ w.t.Helper()
+ rec := w.wd.record()
+ s := e2eRecSnap{state: "none", raw: string(w.recordRaw()), last: rec.LastMove}
+ if rec.Move != nil {
+ s.id, s.state, s.opened = rec.Move.ID, rec.Move.State, rec.Move.OpenedAt != nil
+ }
+ return s
+}
+
+// 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
+// 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 {
+ w *e2eWorld
+ tokens map[bool]*e2eToken // the run's and the hand's
+ posts []e2eRunPost
+ lose func(verb string) bool
+ accepted func(verb string) // called after a POST the door accepted
+}
+
+// e2eToken is a CLI login's token and the requests it has made. The
+// console allows a token 60 instance requests a minute; the run makes at
+// most two a tick, 2 s apart (one status read, one POST), but the test's
+// ticks take no wall time, so a token is swapped for a fresh login's every
+// 50 requests, as the minute passing would.
+type e2eToken struct {
+ token string
+ n int
+}
+
+func (d *e2eRunDoors) do(hand bool, method, u, body string) (int, string) {
+ d.w.t.Helper()
+ if d.tokens == nil {
+ d.tokens = map[bool]*e2eToken{}
+ }
+ tk := d.tokens[hand]
+ if tk == nil || tk.n >= 50 {
+ tk = &e2eToken{token: d.w.mint()}
+ d.tokens[hand] = tk
+ }
+ tk.n++
+ return d.w.doAs(tk.token, method, u, body)
+}
+
+func (d *e2eRunDoors) Status(context.Context) (offlinerun.View, error) {
+ code, out := d.do(false, http.MethodGet, d.w.base, "")
+ if code != http.StatusOK {
+ return offlinerun.View{}, fmt.Errorf("status = %d %s", code, out)
+ }
+ var v offlinerun.View
+ if err := json.Unmarshal([]byte(out), &v); err != nil {
+ return offlinerun.View{}, err
+ }
+ return v, nil
+}
+
+// post sends one door, by the run (hand false) or by a second operator.
+func (d *e2eRunDoors) post(hand bool, verb, id string) (int, string) {
+ body := map[string]string{}
+ if id != "" {
+ body["id"] = id
+ }
+ if verb == offlinerun.VerbStage || verb == offlinerun.VerbStart {
+ body["region"] = "uk"
+ }
+ raw, _ := json.Marshal(body)
+ u := d.w.base
+ if verb != offlinerun.VerbStart {
+ u += "/" + verb
+ }
+ p := e2eRunPost{hand: hand, verb: verb, id: id, before: d.w.snap()}
+ code, out := d.do(hand, http.MethodPost, u, string(raw))
+ p.code, p.after = code, d.w.snap()
+ p.lost = !hand && d.lose != nil && d.lose(verb)
+ d.posts = append(d.posts, p)
+ if p.code >= 200 && p.code <= 299 && d.accepted != nil {
+ d.accepted(verb)
+ }
+ return code, out
+}
+
+func (d *e2eRunDoors) answer(code int) 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}
+ case code >= 400 && code <= 499:
+ return offlinerun.Answer{Refused: true}
+ }
+ return offlinerun.Answer{}
+}
+
+func (d *e2eRunDoors) Post(_ context.Context, verb, id string) offlinerun.Answer {
+ code, _ := d.post(false, verb, id)
+ return d.answer(code)
+}
+
+func (d *e2eRunDoors) Nudge(_ context.Context, id string) offlinerun.Answer {
+ code, _ := d.post(false, "nudge", id)
+ return d.answer(code)
+}
+
+// runVerbs lists the run's POSTs as "<verb>" in order.
+func (d *e2eRunDoors) runVerbs() []string {
+ var out []string
+ for _, p := range d.posts {
+ if !p.hand {
+ out = append(out, p.verb)
+ }
+ }
+ return out
+}
+
+// e2eQuiet is a Watcher that watches nothing.
+type e2eQuiet struct{}
+
+func (e2eQuiet) Saw(offlinerun.View) {}
+func (e2eQuiet) Acting(offlinerun.Action, offlinerun.Memory) {}
+func (e2eQuiet) Acted(offlinerun.Action, offlinerun.Result, offlinerun.Memory) {}
+func (e2eQuiet) Interrupted(offlinerun.Interrupt, offlinerun.Memory) {}
+
+// e2eSerialOnly is the boxes woken by the serial and by nothing else: each
+// box's fastTick (the real serial rule) with the agent's pass set behind
+// it, as e2eLoops; no re-assert loop and no timed pass. deaf, when set, is
+// asked before each pass the serial earned: true and the box misses that
+// wake (fastTick has taken the serial; the pass does not run), as a box
+// whose read of the bump was lost.
+type e2eSerialOnly struct {
+ w *e2eWorld
+ fast map[*offBox]*fastDeps
+ tickAt time.Time
+ deaf func(b *offBox) bool
+ passes int
+ missed int
+ rendered string // the move whose source config was rendered
+}
+
+func (w *e2eWorld) serialOnly(deaf func(b *offBox) bool) *e2eSerialOnly {
+ s := &e2eSerialOnly{w: w, fast: map[*offBox]*fastDeps{}, tickAt: time.Now(), deaf: deaf}
+ for _, b := range w.boxes() {
+ s.fast[b] = &fastDeps{reg: restartTestRegistry(w.t), store: w.rs, fullPass: func(context.Context) {
+ if s.deaf != nil && s.deaf(b) {
+ s.missed++
+ return
+ }
+ s.passes++
+ w.tickAt(b, time.Now())
+ }}
+ }
+ s.round() // the boot read seeds each box's serial
+ return s
+}
+
+// round is one fastTick on each box, past the clamp; it reports whether a
+// box took a serial move (ran its pass or missed it).
+func (s *e2eSerialOnly) round() bool {
+ before := s.passes + s.missed
+ s.tickAt = s.tickAt.Add(fastFullClamp + time.Second)
+ for _, b := range s.w.boxes() {
+ fastTick(context.Background(), s.fast[b], s.tickAt)
+ }
+ return s.passes+s.missed > before
+}
+
+// settle runs rounds until no box takes a serial move (a pass can bump the
+// serial again), then renders the source's config once its row is held for
+// a move, standing in for config sync's render at rest as begin does.
+func (s *e2eSerialOnly) settle() {
+ t := s.w.t
+ t.Helper()
+ for rounds := 0; s.round(); rounds++ {
+ if rounds > 6 {
+ t.Fatal("the serial kept moving after six rounds of passes")
+ }
+ }
+ rec := s.w.wd.record()
+ if mv := rec.Move; mv != nil && mv.ID != s.rendered && mv.State == provision.OfflineStopping {
+ if rt := s.w.ie.row(offHost); rt != nil && rt.HoldOp == mv.ID {
+ s.w.id, s.rendered = mv.ID, mv.ID
+ s.w.renderSourceConfig()
+ }
+ }
+}
+
+// e2eRun drives one offlinerun.Runner over the world, one Step a tick, the
+// boxes woken between ticks only by the serial; between ticks also runs
+// between (a second operator, say). The run's clock is virtual, two seconds
+// a tick. It answers how the run ended.
+type e2eRun struct {
+ w *e2eWorld
+ doors *e2eRunDoors
+ boxes *e2eSerialOnly
+ policy offlinerun.Policy
+ now time.Duration
+ between func()
+ ticks int
+}
+
+func (r *e2eRun) probe(context.Context) int {
+ // The edge serves the target from its registry: a ready row, not held.
+ if rt := r.w.uk.row(offHost); rt != nil && rt.Status == "ready" && rt.HoldOp == "" {
+ return http.StatusOK
+ }
+ return http.StatusServiceUnavailable
+}
+
+func (r *e2eRun) run(maxTicks int) offlinerun.Done {
+ t := r.w.t
+ t.Helper()
+ runner := &offlinerun.Runner{Doors: r.doors, Probe: r.probe, Policy: r.policy,
+ Clock: func() time.Duration { return r.now }, Poll: offlinerun.DefaultPoll, Watch: e2eQuiet{}}
+ m := offlinerun.NewMemory("")
+ for ; r.ticks < maxTicks; r.ticks++ {
+ next, done, err := runner.Step(context.Background(), m)
+ if err != nil {
+ t.Fatalf("tick %d: %v", r.ticks, err)
+ }
+ m = next
+ if done != nil {
+ return *done
+ }
+ r.now += offlinerun.DefaultPoll
+ r.boxes.settle()
+ if r.between != nil {
+ r.between()
+ r.boxes.settle()
+ }
+ }
+ t.Fatalf("the run did not end in %d ticks; the record is %s; POSTs %v", maxTicks, r.w.snap(), r.doors.runVerbs())
+ return offlinerun.Done{}
+}
+
+// newRun is a run over w, before anything is posted: the source's timers
+// are kept for requireTarget.
+func (w *e2eWorld) newRun(deaf func(b *offBox) bool) *e2eRun {
+ w.t.Helper()
+ timers, err := w.ie.reg.TimersFor(offHost)
+ if err != nil {
+ w.t.Fatal(err)
+ }
+ w.srcTimers = map[string]registry.Timer{}
+ for _, tm := range timers {
+ w.srcTimers[tm.Name] = tm
+ }
+ return &e2eRun{w: w, doors: &e2eRunDoors{w: w}, boxes: w.serialOnly(deaf), policy: offlinerun.DefaultPolicy()}
+}
+
+// e2eRunHappy is the run's POSTs on a move nothing goes wrong with. The
+// nudge between the two finishes is not a fault: on the first finish's bump
+// the source's pass reconciles before its moves pass reads the record at
+// offline-finishing, so its row is still there and it reports gone only on
+// its next pass (offlineSourceGone), which only the nudge's bump starts.
+const e2eRunHappy = "stage,start,seal,flip,open,finish,nudge,finish"
+
+// TestOfflineMoveRunE2E is the two-box witness for carlos move offline
+// run's loop: offlinerun.Runner's Step, one a tick, over the console's real
+// doors, and both boxes' production passes woken only by the serial (the
+// doors' bumps and the boxes' own), nothing else. The move goes from
+// nothing through a stage round to done and the target holds the source's
+// instance. Then a box that misses one wake: the move stands still until
+// the run's nudge, whose bump alone gets it going, at every step a box
+// does. Without the nudge the same miss stalls the stop until its budget
+// aborts the move.
+func TestOfflineMoveRunE2E(t *testing.T) {
+ t.Run("done by Step, woken by the serial", func(t *testing.T) {
+ w := newE2EWorld(t, landOpts{})
+ r := w.newRun(nil)
+ done := r.run(200)
+ if done.Exit != offlinerun.ExitDone || done.Reason != offlinerun.ReasonDone {
+ t.Fatalf("the run ended %+v; POSTs %v", done, r.doors.runVerbs())
+ }
+ if got := strings.Join(r.doors.runVerbs(), ","); got != e2eRunHappy {
+ t.Fatalf("POSTs %s, want %s", got, e2eRunHappy)
+ }
+ rec := w.wd.record()
+ if rec.Move != nil || rec.LastMove == nil || rec.LastMove.Outcome != "done" || rec.LastMove.ID != w.id || rec.Region != "uk" {
+ t.Fatalf("after the run: region %q, move %+v, last %+v", rec.Region, rec.Move, rec.LastMove)
+ }
+ for _, p := range r.doors.posts[1:] {
+ if p.id != w.id {
+ t.Fatalf("%s named %q, the move is %s", p.verb, p.id, w.id)
+ }
+ }
+ if r.boxes.passes == 0 || r.boxes.missed != 0 {
+ t.Fatalf("passes %d, missed %d", r.boxes.passes, r.boxes.missed)
+ }
+ w.requireTarget()
+ w.requireTombstoneLifted()
+ if rt := w.ie.row(offHost); rt != nil {
+ t.Fatalf("ie-box still has a row after finish: %+v", rt)
+ }
+ t.Logf("%d ticks, %d serial passes", r.ticks, r.boxes.passes)
+ })
+ // The box that acts on each accepted door's bump, made to miss it.
+ for _, c := range []struct {
+ verb, box string
+ }{
+ {offlinerun.VerbStage, "ie-box"}, // the stager's round
+ {offlinerun.VerbStart, "ie-box"}, // the source's stop
+ {offlinerun.VerbSeal, "ie-box"}, // the sealer
+ {offlinerun.VerbFlip, "uk-box"}, // the target's landing
+ {offlinerun.VerbOpen, "uk-box"}, // the target's open
+ {offlinerun.VerbFinish, "ie-box"}, // the source's park ends (the first finish)
+ } {
+ t.Run(c.box+" misses the "+c.verb+" bump, the nudge wakes it", func(t *testing.T) {
+ w := newE2EWorld(t, landOpts{})
+ armed := false
+ r := w.newRun(func(b *offBox) bool {
+ if armed && b.name == c.box {
+ armed = false
+ return true
+ }
+ return false
+ })
+ once := false
+ r.doors.accepted = func(verb string) {
+ if verb == c.verb && !once {
+ armed, once = true, true
+ }
+ }
+ done := r.run(300)
+ if done.Exit != offlinerun.ExitDone {
+ t.Fatalf("the run ended %+v; POSTs %v", done, r.doors.runVerbs())
+ }
+ if r.boxes.missed != 1 {
+ t.Fatalf("%s missed %d wakes, want 1", c.box, r.boxes.missed)
+ }
+ // Between the missed wake and the nudge the record stood still:
+ // every POST is the run's own, and the first after the missed
+ // door is the nudge, with the record as the door left it.
+ var door e2eRunPost
+ var after []e2eRunPost
+ for i, p := range r.doors.posts {
+ if p.verb == c.verb && p.code == http.StatusAccepted {
+ door, after = p, r.doors.posts[i+1:]
+ break
+ }
+ }
+ if len(after) == 0 || after[0].verb != "nudge" {
+ t.Fatalf("after %s the run posted %v, want a nudge first", c.verb, r.doors.runVerbs())
+ }
+ if after[0].before.raw != door.after.raw {
+ t.Fatalf("the record moved between %s and the nudge: %s, then %s", c.verb, door.after, after[0].before)
+ }
+ if after[0].before.raw != after[0].after.raw {
+ t.Fatal("the nudge wrote the record")
+ }
+ got := strings.Join(r.doors.runVerbs(), ",")
+ want := strings.Replace(e2eRunHappy, c.verb, c.verb+",nudge", 1)
+ if got != want {
+ t.Fatalf("POSTs %s, want %s", got, want)
+ }
+ w.requireTarget()
+ })
+ }
+ // 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.
+ t.Run("no nudge: the missed stop stalls until the budget aborts", func(t *testing.T) {
+ w := newE2EWorld(t, landOpts{})
+ armed := false
+ r := w.newRun(func(b *offBox) bool {
+ if armed && b.name == "ie-box" {
+ armed = false
+ return true
+ }
+ return false
+ })
+ r.policy.NudgeAfter, r.policy.NudgeEvery = time.Hour, time.Hour
+ r.doors.accepted = func(verb string) {
+ if verb == offlinerun.VerbStart {
+ armed = true
+ }
+ }
+ done := r.run(300)
+ if done.Exit != offlinerun.ExitAborted {
+ t.Fatalf("the run ended %+v; POSTs %v", done, r.doors.runVerbs())
+ }
+ if got := strings.Join(r.doors.runVerbs(), ","); got != "stage,start,abort,finish" {
+ t.Fatalf("POSTs %s", got)
+ }
+ if r.now < offlinerun.DefaultStop {
+ t.Fatalf("aborted after %s, before the stop budget", r.now)
+ }
+ rec := w.wd.record()
+ if rec.Move != nil || rec.LastMove == nil || rec.LastMove.Outcome != "aborted" || rec.Region != "" && rec.Region != "ie" {
+ t.Fatalf("after the run: region %q, move %+v, last %+v", rec.Region, rec.Move, rec.LastMove)
+ }
+ w.tickAll()
+ if !w.ie.lists(offHost) {
+ t.Fatal("ie-box does not serve the host after the abort")
+ }
+ })
+}