carlos / platform Public

moves: offline run takes Ctrl-C before any POST, never claims an open

Astra branch review round 1, findings 2 (P1) and 3 (P2).

Ctrl-Cs are counted (offlinerun.Interrupts) as they arrive, and taken
after every status read and again before any POST, so a first Ctrl-C
pending while a read answers landed/next=open becomes an abort, never an
open. The rate limit's back-off and the status reads end at a Ctrl-C (a
POST is never cut short, so its outcome is never made unknown by one); a
POST abandoned in the back-off was taken by no door and is not counted.
A Ctrl-C before run has sent anything stops it there (exit 4, new OR70,
pending in strings-fix.json).

An open whose reply never came may or may not have committed: a Ctrl-C
then still never aborts, but says OR29 and the status command instead of
OR40's "already served".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 5896a9e0c3e6c4d0213f7b6dc74e3f0891bc4113 parent 5b6e62c
10 files changed, +639 −90
  • approved_moves_copy_test.go +3 −0
  • docs/drafts/offline-move-run/copy-review/strings-fix.json +10 −0
  • docs/superpowers/plans/2026-10-06-moves-phase1-copy.md +8 −0
  • internal/moves/offlinerun/run.go +203 −24
  • internal/moves/offlinerun/run_test.go +143 −26
  • move.go +6 −4
  • move_offline_run.go +133 −31
  • move_offline_run_test.go +103 −3
  • move_test.go +29 −1
  • offline_move_e2e_test.go +1 −1
diff --git a/approved_moves_copy_test.go b/approved_moves_copy_test.go
index 71c669fe6..18c03531e 100644
--- a/approved_moves_copy_test.go
+++ b/approved_moves_copy_test.go
@@ -127,6 +127,9 @@ var movesCopyIDs = []string{
// strings-task8.json beside strings.json). OR66 replaced MV-O181.
"OR50", "OR51", "OR53", "OR54", "OR55", "OR56", "OR57", "OR58", "OR59",
"OR60", "OR61", "OR62", "OR63", "OR65", "OR66",
+ // The fixes for Astra's branch review, round 1 (deck
+ // strings-fix.json beside strings.json; pending).
+ "OR70",
}
// copyRow is one row of the record. The File cell is the file the sentence is
diff --git a/docs/drafts/offline-move-run/copy-review/strings-fix.json b/docs/drafts/offline-move-run/copy-review/strings-fix.json
new file mode 100644
index 000000000..4a828410b
--- /dev/null
+++ b/docs/drafts/offline-move-run/copy-review/strings-fix.json
@@ -0,0 +1,10 @@
+[
+ {
+ "id": "OR70",
+ "section": "Ctrl-C",
+ "label": "Ctrl-C before anything was sent",
+ "text": "Stopped. Nothing was sent.",
+ "context": "carlos move offline run, a Ctrl-C before run has sent anything: during its first status read, or while a rate limit holds up its first stage (no door took it). No move was staged. Exit 4. Astra branch review round 1, finding 2.",
+ "status": "pending"
+ }
+]
diff --git a/docs/superpowers/plans/2026-10-06-moves-phase1-copy.md b/docs/superpowers/plans/2026-10-06-moves-phase1-copy.md
index 6fad467ce..9b1f0654a 100644
--- a/docs/superpowers/plans/2026-10-06-moves-phase1-copy.md
+++ b/docs/superpowers/plans/2026-10-06-moves-phase1-copy.md
@@ -811,3 +811,11 @@ Task 8 of the offline move run plan (deck `docs/drafts/offline-move-run/copy-rev
| OR63 | docs/site/cli.md | `status` leads with the state, then shows the move, how long the instance has been offline when that applies, how long it has been in this step, and the one command forward and the one back: | approved |
| OR65 | docs/site/cli.md | While the console waits on a box, a `waiting` row says what for, and the forward command ends with “when ready”. Under the block, `status` lists each item the move checks and each box’s report. | approved |
| OR66 | move.go | carlos move offline needs run, stage, start, seal, flip, open, finish, abort or status. | approved |
+
+## Offline move run: the branch review's fixes (Astra round 1)
+
+Sentences the fixes for Astra's whole-branch review of `offline-move-run` (round 1) need. Pending: deck `docs/drafts/offline-move-run/copy-review/strings-fix.json`, for Luna under Paul's 2026-10-25 delegation. The code is written against the deck's text; an approval that changes a row is applied verbatim.
+
+| id | file | text | status |
+|---|---|---|---|
+| OR70 | move_offline_run.go | Stopped. Nothing was sent. | pending |
diff --git a/internal/moves/offlinerun/run.go b/internal/moves/offlinerun/run.go
index d2e82d447..b0b400c67 100644
--- a/internal/moves/offlinerun/run.go
+++ b/internal/moves/offlinerun/run.go
@@ -2,6 +2,7 @@ package offlinerun
import (
"context"
+ "sync"
"time"
)
@@ -16,6 +17,10 @@ const DefaultPoll = 2 * time.Second
// second before open, the first after it). The move itself goes on.
const ReasonStoppedWatching = "stopped-watching"
+// ReasonNothingSent ends a run stopped with Ctrl-C before it sent anything
+// (exit 4): no move was staged.
+const ReasonNothingSent = "nothing-sent"
+
// Answer is how a door answered one POST.
type Answer struct {
// Accepted is a 2xx.
@@ -26,6 +31,10 @@ type Answer struct {
// not have happened, and the next status read says which. Neither case
// is ever sent again blindly.
Refused bool
+ // NotSent: no door took the POST. A rate limit answered it (before
+ // any handler) and the run was interrupted during the back-off, so the
+ // Doors gave up; nothing was written. Step undoes its count.
+ NotSent bool
// Sentence is the console's own sentence for a refusal, when the
// Doors can carry it; "" otherwise.
Sentence string
@@ -62,11 +71,108 @@ const (
// InterruptStop is the second Ctrl-C before open: the run stops
// watching and the move goes on as it stands.
InterruptStop
- // InterruptAfterOpen is a Ctrl-C once the move may be open: nothing
- // is undone, the run stops watching.
+ // InterruptAfterOpen is a Ctrl-C once the move was read open, or its
+ // open was accepted: nothing is undone, the run stops watching.
InterruptAfterOpen
+ // InterruptOpenUncertain is a Ctrl-C after an open POST got no
+ // answer: it may have committed, so nothing is undone, and it may
+ // not have, so the run does not say the instance is served. It stops
+ // watching.
+ InterruptOpenUncertain
+ // InterruptNothingSent is a Ctrl-C before the run sent anything: it
+ // stops, and no move was staged.
+ InterruptNothingSent
)
+// Interrupts counts Ctrl-Cs as they arrive. Add is safe from any
+// goroutine (the signal source's); the run takes them after each status
+// read, before any POST and between ticks, and a wait or read the Doors
+// make on a context from Bind ends when one arrives. A nil *Interrupts has
+// none.
+type Interrupts struct {
+ mu sync.Mutex
+ n int
+ wake chan struct{}
+ next int
+ cancels map[int]context.CancelFunc
+}
+
+// NewInterrupts is an empty count.
+func NewInterrupts() *Interrupts {
+ return &Interrupts{wake: make(chan struct{}, 1), cancels: map[int]context.CancelFunc{}}
+}
+
+// Add is one Ctrl-C.
+func (i *Interrupts) Add() {
+ i.mu.Lock()
+ i.n++
+ for _, cancel := range i.cancels {
+ cancel()
+ }
+ i.mu.Unlock()
+ select {
+ case i.wake <- struct{}{}:
+ default:
+ }
+}
+
+// Bind is ctx, ended also by a Ctrl-C (at once when one is pending): for a
+// wait, or a status read, the Doors make. A POST is never sent on it, so a
+// Ctrl-C never makes a write's outcome unknown. release frees it.
+func (i *Interrupts) Bind(ctx context.Context) (context.Context, func()) {
+ c, cancel := context.WithCancel(ctx)
+ if i == nil {
+ return c, cancel
+ }
+ i.mu.Lock()
+ defer i.mu.Unlock()
+ if i.n > 0 {
+ cancel()
+ return c, cancel
+ }
+ id := i.next
+ i.next++
+ i.cancels[id] = cancel
+ return c, func() {
+ i.mu.Lock()
+ delete(i.cancels, id)
+ i.mu.Unlock()
+ cancel()
+ }
+}
+
+// Wake is signalled after an Add (a hint: the count is the truth).
+func (i *Interrupts) Wake() <-chan struct{} {
+ if i == nil {
+ return nil
+ }
+ return i.wake
+}
+
+// pending says a Ctrl-C has not been taken yet.
+func (i *Interrupts) pending() bool {
+ if i == nil {
+ return false
+ }
+ i.mu.Lock()
+ defer i.mu.Unlock()
+ return i.n > 0
+}
+
+// take takes one Ctrl-C, false when none is pending.
+func (i *Interrupts) take() bool {
+ if i == nil {
+ return false
+ }
+ i.mu.Lock()
+ defer i.mu.Unlock()
+ if i.n == 0 {
+ return false
+ }
+ i.n--
+ return true
+}
+
// Watcher sees the run: each view read, each action before it is performed
// and its result after, and each Ctrl-C.
type Watcher interface {
@@ -77,39 +183,67 @@ type Watcher interface {
}
// Runner runs one move. Clock is the terminal's monotonic clock; After is
-// the poll timer (time.After); both are seams for tests.
+// the poll timer (time.After); both are seams for tests. Interrupts are the
+// operator's Ctrl-Cs (nil for none).
type Runner struct {
- Doors Doors
- Probe func(ctx context.Context) int
- Policy Policy
- Clock func() time.Duration
- After func(d time.Duration) <-chan time.Time
- Poll time.Duration
- Watch Watcher
+ Doors Doors
+ Probe func(ctx context.Context) int
+ Policy Policy
+ Clock func() time.Duration
+ After func(d time.Duration) <-chan time.Time
+ Poll time.Duration
+ Watch Watcher
+ Interrupts *Interrupts
- // openSent: an open POST was accepted, or got no answer (it may have
- // committed). From then on a Ctrl-C never aborts.
- openSent bool
- stops int
+ // openAccepted: an open POST was accepted. openUncertain: one got no
+ // answer (it may have committed). From either on a Ctrl-C never
+ // aborts.
+ openAccepted, openUncertain bool
+ stops int
}
// Step reads status once, decides, and performs at most one action. done
// is set when the run ends; err when the status could not be read (nothing
-// was decided or sent).
+// was decided or sent). A Ctrl-C that arrived by the end of the read is
+// taken before anything is decided from it, and one that arrives before a
+// POST goes out is taken and the view decided again (finding 2 of the
+// branch review): a pending first Ctrl-C becomes an abort, never an open.
func (r *Runner) Step(ctx context.Context, m Memory) (Memory, *Done, error) {
v, err := r.Doors.Status(ctx)
+ var read *View
+ if err == nil {
+ read = &v
+ }
+ var stop *Done
+ if m, stop = r.takeInterrupts(m, read); stop != nil {
+ return m, stop, nil
+ }
if err != nil {
return m, nil, err
}
r.Watch.Saw(v)
var a Action
- a, m = Decide(v, r.Policy, m, r.Clock())
+ for {
+ var next Memory
+ a, next = Decide(v, r.Policy, m, r.Clock())
+ if _, post := a.(Post); post && r.Interrupts.pending() {
+ if m, stop = r.takeInterrupts(m, read); stop != nil {
+ return m, stop, nil
+ }
+ continue
+ }
+ m = next
+ break
+ }
switch a := a.(type) {
case Done:
return m, &a, nil
case Post:
r.Watch.Acting(a, m)
ans := r.Doors.Post(ctx, a.Verb, m.Bound())
+ if ans.NotSent {
+ m = m.notSent(a.Verb)
+ }
if !ans.Accepted && ans.Refused {
m = m.Refused(ans.Sentence)
}
@@ -119,8 +253,10 @@ func (r *Runner) Step(ctx context.Context, m Memory) (Memory, *Done, error) {
// staged their own before that read.
m = m.bindTo(ans.ID)
}
- if a.Verb == VerbOpen && (ans.Accepted || !ans.Refused) {
- r.openSent = true
+ if a.Verb == VerbOpen && ans.Accepted {
+ r.openAccepted = true
+ } else if a.Verb == VerbOpen && !ans.Refused && !ans.NotSent {
+ r.openUncertain = true
}
r.Watch.Acted(a, Result{Answer: ans}, m)
case Nudge:
@@ -139,8 +275,9 @@ func (r *Runner) Step(ctx context.Context, m Memory) (Memory, *Done, error) {
// Run steps until the run ends, sleeping Poll between ticks. A status read
// that fails before any view was read ends it with that error; later ones
// are ridden out (the move is under way, and only a view decides). A
-// receive on interrupts is a Ctrl-C.
-func (r *Runner) Run(ctx context.Context, m Memory, interrupts <-chan struct{}) (Done, Memory, error) {
+// Ctrl-C (Interrupts) is taken as soon as it arrives between ticks, and
+// otherwise by Step.
+func (r *Runner) Run(ctx context.Context, m Memory) (Done, Memory, error) {
read := false
for {
next, done, err := r.Step(ctx, m)
@@ -157,9 +294,9 @@ func (r *Runner) Run(ctx context.Context, m Memory, interrupts <-chan struct{})
select {
case <-ctx.Done():
return Done{}, m, ctx.Err()
- case <-interrupts:
+ case <-r.Interrupts.Wake():
var stop *Done
- if m, stop = r.interrupt(m); stop != nil {
+ if m, stop = r.takeInterrupts(m, nil); stop != nil {
return *stop, m, nil
}
case <-r.After(r.Poll):
@@ -167,14 +304,43 @@ func (r *Runner) Run(ctx context.Context, m Memory, interrupts <-chan struct{})
}
}
+// takeInterrupts takes every pending Ctrl-C, in order, until one ends the
+// run. v is the view just read, if any: it says whether the move is open
+// before Decide has folded it in.
+func (r *Runner) takeInterrupts(m Memory, v *View) (Memory, *Done) {
+ for r.Interrupts.take() {
+ var stop *Done
+ if m, stop = r.interrupt(m, v); stop != nil {
+ return m, stop
+ }
+ }
+ return m, nil
+}
+
// interrupt is one Ctrl-C (plan §7, decision 3).
-func (r *Runner) interrupt(m Memory) (Memory, *Done) {
+func (r *Runner) interrupt(m Memory, v *View) (Memory, *Done) {
r.stops++
stop := &Done{Exit: ExitStopped, Reason: ReasonStoppedWatching}
+ opened := m.Opened() || (v != nil && v.ID != "" && v.ID == m.Bound() && (v.State == StateOpen || v.State == StateFinishing))
switch {
- case m.Opened() || r.openSent:
+ case m.Bound() == "" && !m.Staged():
+ r.Watch.Interrupted(InterruptNothingSent, m)
+ return m, &Done{Exit: ExitStopped, Reason: ReasonNothingSent}
+ case m.Bound() == "" && m.refused:
+ // The first stage was refused: nothing was written, and the next
+ // Decide would end the run on it. End it now.
+ return m, &Done{Exit: ExitRefused, Reason: m.refusal}
+ case m.Bound() == "":
+ // The first stage got no answer: a move may exist, and the run
+ // cannot tell whose. It stops watching (status says).
+ r.Watch.Interrupted(InterruptStop, m)
+ return m, stop
+ case opened || r.openAccepted:
r.Watch.Interrupted(InterruptAfterOpen, m)
return m, stop
+ case r.openUncertain:
+ r.Watch.Interrupted(InterruptOpenUncertain, m)
+ return m, stop
case r.stops == 1:
m = m.Abort(ReasonInterrupted)
r.Watch.Interrupted(InterruptAbort, m)
@@ -187,5 +353,18 @@ func (r *Runner) interrupt(m Memory) (Memory, *Done) {
// Opened says the run read the move open (or finishing): nothing aborts it.
func (m Memory) Opened() bool { return m.opened }
+// Staged says the run has a stage POST of its own out (one a door took, or
+// may have).
+func (m Memory) Staged() bool { return m.stagePosts > 0 }
+
+// notSent undoes the count of a POST no door took (Answer.NotSent).
+func (m Memory) notSent(verb string) Memory {
+ if verb == VerbStage && m.stagePosts > 0 {
+ m.stagePosts--
+ m.stageSent = false
+ }
+ return m
+}
+
// QuietFor is how long the view has gone without a change, at now.
func (m Memory) QuietFor(now time.Duration) time.Duration { return now - m.lastChange }
diff --git a/internal/moves/offlinerun/run_test.go b/internal/moves/offlinerun/run_test.go
index 7778ff408..470002d79 100644
--- a/internal/moves/offlinerun/run_test.go
+++ b/internal/moves/offlinerun/run_test.go
@@ -18,12 +18,18 @@ type fakeDoors struct {
reads int
answer func(verb string) Answer
log []string
+ // during, when set, runs inside status read n (0-based): a Ctrl-C
+ // that lands while the read is on the wire.
+ during func(n int)
}
func (f *fakeDoors) Status(context.Context) (View, error) {
n := f.reads
f.reads++
f.log = append(f.log, "status")
+ if f.during != nil {
+ f.during(n)
+ }
if f.errs[n] {
return View{}, errors.New("no answer")
}
@@ -87,7 +93,7 @@ func TestRunNoAnswerIsFollowedByAStatusRead(t *testing.T) {
},
}
r, _ := testRunner(d, func() int { return 200 })
- done, _, err := r.Run(context.Background(), NewMemory(tID), nil)
+ done, _, err := r.Run(context.Background(), NewMemory(tID))
if err != nil {
t.Fatal(err)
}
@@ -106,7 +112,7 @@ func TestRunRefusedFirstStageExits5(t *testing.T) {
answer: func(string) Answer { return Answer{Refused: true, Sentence: "another move is in flight"} },
}
r, _ := testRunner(d, func() int { return 200 })
- done, _, err := r.Run(context.Background(), NewMemory(""), nil)
+ done, _, err := r.Run(context.Background(), NewMemory(""))
if err != nil {
t.Fatal(err)
}
@@ -122,7 +128,7 @@ func TestRunStatusErrors(t *testing.T) {
// Before any view: the error ends the run, nothing sent.
d := &fakeDoors{views: []View{none(nil)}, errs: map[int]bool{0: true}}
r, _ := testRunner(d, func() int { return 200 })
- if _, _, err := r.Run(context.Background(), NewMemory(""), nil); err == nil {
+ if _, _, err := r.Run(context.Background(), NewMemory("")); err == nil {
t.Fatal("a first status read that failed did not end the run")
}
if len(d.log) != 1 {
@@ -134,7 +140,7 @@ func TestRunStatusErrors(t *testing.T) {
errs: map[int]bool{1: true, 2: true},
}
r, _ = testRunner(d, func() int { return 200 })
- done, _, err := r.Run(context.Background(), offline(StateSealing, 0), nil)
+ done, _, err := r.Run(context.Background(), offline(StateSealing, 0))
if err != nil || done.Exit != ExitDone {
t.Fatalf("done %#v, err %v", done, err)
}
@@ -149,19 +155,19 @@ func TestRunCtrlC(t *testing.T) {
none(&Outcome{ID: tID, Outcome: OutcomeAborted}),
}}
r, w := testRunner(d, func() int { return 200 })
- ints := make(chan struct{}, 1)
- ints <- struct{}{}
- // After lets the interrupt win the first select: it never fires.
+ r.Interrupts = NewInterrupts()
+ // The Ctrl-C lands during the first sleep: After never fires.
after := r.After
first := true
r.After = func(dd time.Duration) <-chan time.Time {
if first {
first = false
+ r.Interrupts.Add()
return nil
}
return after(dd)
}
- done, _, err := r.Run(context.Background(), offline(StateSealing, 0), ints)
+ done, _, err := r.Run(context.Background(), offline(StateSealing, 0))
if err != nil || done.Exit != ExitAborted {
t.Fatalf("done %#v, err %v", done, err)
}
@@ -175,11 +181,16 @@ func TestRunCtrlC(t *testing.T) {
// The second stops watching: exit 4, nothing more sent.
d = &fakeDoors{views: []View{tv(StateSealing)}, answer: func(string) Answer { return Answer{} }}
r, w = testRunner(d, func() int { return 200 })
- ints = make(chan struct{}, 2)
- ints <- struct{}{}
- ints <- struct{}{}
- r.After = func(time.Duration) <-chan time.Time { return nil }
- done, _, _ = r.Run(context.Background(), offline(StateSealing, 0), ints)
+ r.Interrupts = NewInterrupts()
+ sleeps := 0
+ r.After = func(time.Duration) <-chan time.Time {
+ sleeps++
+ if sleeps <= 2 {
+ r.Interrupts.Add()
+ }
+ return nil
+ }
+ done, _, _ = r.Run(context.Background(), offline(StateSealing, 0))
if done != (Done{Exit: ExitStopped, Reason: ReasonStoppedWatching}) {
t.Fatalf("done %#v", done)
}
@@ -190,20 +201,15 @@ func TestRunCtrlC(t *testing.T) {
t.Fatalf("calls %s", got)
}
- // Once open was sent, a Ctrl-C undoes nothing.
- d = &fakeDoors{views: []View{withNext(tv(StateLanded), VerbOpen), tv(StateLanded)}, answer: func(string) Answer { return Answer{} }}
+ // Once open was accepted, a Ctrl-C undoes nothing.
+ d = &fakeDoors{views: []View{withNext(tv(StateLanded), VerbOpen), tv(StateLanded)}}
r, w = testRunner(d, func() int { return 200 })
- ints = make(chan struct{}, 1)
- calls := 0
+ r.Interrupts = NewInterrupts()
r.After = func(time.Duration) <-chan time.Time {
- calls++
- if calls == 1 {
- ints <- struct{}{}
- return nil
- }
+ r.Interrupts.Add()
return nil
}
- done, _, _ = r.Run(context.Background(), offline(StateLanded, 0), ints)
+ done, _, _ = r.Run(context.Background(), offline(StateLanded, 0))
if done != (Done{Exit: ExitStopped, Reason: ReasonStoppedWatching}) {
t.Fatalf("done %#v", done)
}
@@ -217,12 +223,123 @@ func TestRunCtrlC(t *testing.T) {
}
}
+// TestRunCtrlCOpenUncertain: an open whose answer never came may have
+// committed, so a Ctrl-C then never aborts; and since it may not have, the
+// run does not say the instance is served (finding 3): InterruptOpenUncertain.
+func TestRunCtrlCOpenUncertain(t *testing.T) {
+ d := &fakeDoors{views: []View{withNext(tv(StateLanded), VerbOpen), tv(StateLanded)}, answer: func(string) Answer { return Answer{} }}
+ r, w := testRunner(d, func() int { return 200 })
+ r.Interrupts = NewInterrupts()
+ r.After = func(time.Duration) <-chan time.Time {
+ r.Interrupts.Add()
+ return nil
+ }
+ done, _, _ := r.Run(context.Background(), offline(StateLanded, 0))
+ if done != (Done{Exit: ExitStopped, Reason: ReasonStoppedWatching}) {
+ t.Fatalf("done %#v", done)
+ }
+ if !reflect.DeepEqual(w.interrupts, []Interrupt{InterruptOpenUncertain}) {
+ t.Fatalf("interrupts %v", w.interrupts)
+ }
+ if got := strings.Join(d.log, ","); got != "status,open "+tID {
+ t.Fatalf("calls %s", got)
+ }
+}
+
+// TestRunCtrlCDuringStatusRead (finding 2): a first Ctrl-C that lands while
+// a status read is on the wire is taken before anything is decided from
+// that read. A read that answers landed with next open becomes an abort,
+// never an open.
+func TestRunCtrlCDuringStatusRead(t *testing.T) {
+ d := &fakeDoors{views: []View{
+ withNext(tv(StateLanded), VerbOpen),
+ withNext(tv(StateReverting), VerbFinish),
+ none(&Outcome{ID: tID, Outcome: OutcomeAborted}),
+ }}
+ r, w := testRunner(d, func() int { return 200 })
+ r.Interrupts = NewInterrupts()
+ d.during = func(n int) {
+ if n == 0 {
+ r.Interrupts.Add()
+ }
+ }
+ done, _, err := r.Run(context.Background(), offline(StateLanded, 0))
+ if err != nil || done.Exit != ExitAborted {
+ t.Fatalf("done %#v, err %v", done, err)
+ }
+ if got := strings.Join(d.log, ","); got != "status,abort "+tID+",status,finish "+tID+",status" {
+ t.Fatalf("calls %s", got)
+ }
+ if !reflect.DeepEqual(w.interrupts, []Interrupt{InterruptAbort}) {
+ t.Fatalf("interrupts %v", w.interrupts)
+ }
+ // A read that failed is no different: the Ctrl-C is taken, and the
+ // abort goes out after the next read.
+ d = &fakeDoors{views: []View{withNext(tv(StateLanded), VerbOpen)}, errs: map[int]bool{0: true}}
+ r, _ = testRunner(d, func() int { return 200 })
+ r.Interrupts = NewInterrupts()
+ d.during = func(n int) {
+ if n == 0 {
+ r.Interrupts.Add()
+ }
+ }
+ m, stop, err := r.Step(context.Background(), offline(StateLanded, 0))
+ if stop != nil || err == nil {
+ t.Fatalf("step: stop %#v, err %v", stop, err)
+ }
+ if _, stop, err = r.Step(context.Background(), m); stop != nil || err != nil {
+ t.Fatalf("stop %#v, err %v", stop, err)
+ }
+ if got := strings.Join(d.log, ","); got != "status,status,abort "+tID {
+ t.Fatalf("calls %s", got)
+ }
+}
+
+// TestRunCtrlCBeforeAnythingSent: a Ctrl-C during the first status read,
+// before the run has sent anything, ends it there: exit 4, nothing sent.
+func TestRunCtrlCBeforeAnythingSent(t *testing.T) {
+ d := &fakeDoors{views: []View{none(nil)}}
+ r, w := testRunner(d, func() int { return 200 })
+ r.Interrupts = NewInterrupts()
+ d.during = func(int) { r.Interrupts.Add() }
+ done, _, err := r.Run(context.Background(), NewMemory(""))
+ if err != nil || done != (Done{Exit: ExitStopped, Reason: ReasonNothingSent}) {
+ t.Fatalf("done %#v, err %v", done, err)
+ }
+ if got := strings.Join(d.log, ","); got != "status" {
+ t.Fatalf("calls %s", got)
+ }
+ if !reflect.DeepEqual(w.interrupts, []Interrupt{InterruptNothingSent}) {
+ t.Fatalf("interrupts %v", w.interrupts)
+ }
+}
+
+// TestRunNotSentPostIsNotCounted: a POST the doors gave up on before any
+// door took it (interrupted in a rate limit's back-off) is not a stage the
+// run made: a Ctrl-C then ends a run that has sent nothing.
+func TestRunNotSentPostIsNotCounted(t *testing.T) {
+ d := &fakeDoors{views: []View{none(nil)}}
+ r, w := testRunner(d, func() int { return 200 })
+ r.Interrupts = NewInterrupts()
+ d.answer = func(string) Answer {
+ r.Interrupts.Add()
+ return Answer{NotSent: true}
+ }
+ done, m, err := r.Run(context.Background(), NewMemory(""))
+ if err != nil || done != (Done{Exit: ExitStopped, Reason: ReasonNothingSent}) || m.Staged() {
+ t.Fatalf("done %#v, staged %v, err %v", done, m.Staged(), err)
+ }
+ if !reflect.DeepEqual(w.interrupts, []Interrupt{InterruptNothingSent}) {
+ t.Fatalf("interrupts %v", w.interrupts)
+ }
+}
+
func TestRunProbeThenFinish(t *testing.T) {
open := withNext(tv(StateOpen), VerbFinish)
d := &fakeDoors{views: []View{open, open, open, none(&Outcome{ID: tID, Outcome: OutcomeDone})}}
codes := []int{0, 200}
r, _ := testRunner(d, func() int { c := codes[0]; codes = codes[1:]; return c })
- done, _, err := r.Run(context.Background(), offline(StateLanded, 0), nil)
+ done, _, err := r.Run(context.Background(), offline(StateLanded, 0))
if err != nil || done.Exit != ExitDone {
t.Fatalf("done %#v, err %v", done, err)
}
@@ -245,7 +362,7 @@ func TestRunBindsTheMoveItsStageMade(t *testing.T) {
},
}
r, _ := testRunner(d, func() int { return 200 })
- done, m, err := r.Run(context.Background(), NewMemory(""), nil)
+ done, m, err := r.Run(context.Background(), NewMemory(""))
if err != nil {
t.Fatal(err)
}
@@ -277,7 +394,7 @@ func TestRunFirstStageUncertainNeverAdopts(t *testing.T) {
t.Run(name, func(t *testing.T) {
d := &fakeDoors{views: []View{none(nil), theirs}, answer: func(string) Answer { return c.ans }}
r, _ := testRunner(d, func() int { return 200 })
- done, m, err := r.Run(context.Background(), NewMemory(""), nil)
+ done, m, err := r.Run(context.Background(), NewMemory(""))
if err != nil {
t.Fatal(err)
}
diff --git a/move.go b/move.go
index beeefc2db..5b20254e4 100644
--- a/move.go
+++ b/move.go
@@ -163,10 +163,12 @@ type moveDeps struct {
// offline run's seams (move_offline_run.go), each its real one when
// nil: the wall clock (time.Now, also the monotonic one), the poll
// timer (time.After), the probe (an HTTPS GET) and the Ctrl-C source.
- now func() time.Time
- after func(time.Duration) <-chan time.Time
- probe func(ctx context.Context, url string) int
- interrupts func() (<-chan struct{}, func())
+ now func() time.Time
+ after func(time.Duration) <-chan time.Time
+ probe func(ctx context.Context, url string) int
+ // interrupts starts the Ctrl-C source: it calls add once per Ctrl-C,
+ // from any goroutine, until stop.
+ interrupts func(add func()) (stop func())
}
type moveArgs struct {
diff --git a/move_offline_run.go b/move_offline_run.go
index d498fbf57..fc8c951c5 100644
--- a/move_offline_run.go
+++ b/move_offline_run.go
@@ -119,24 +119,26 @@ func (m *mover) offlineRun(ctx context.Context, a moveArgs, d moveDeps) int {
path = "/" + path
}
+ ints := offlinerun.NewInterrupts()
w := &offlineRun{m: m, said: map[string]bool{}}
- doors := &runDoors{w: w, region: a.to, code: -1}
+ doors := &runDoors{w: w, region: a.to, code: -1, ints: ints}
start := now()
watch := &runWatcher{w: w, doors: doors, now: now, policy: policy,
resume: a.resume, bound: a.resume, url: "https://" + m.host + path}
watch.clock = func() time.Duration { return now().Sub(start) }
r := &offlinerun.Runner{
- Doors: doors,
- Probe: func(ctx context.Context) int { return probe(ctx, watch.url) },
- Policy: policy,
- Clock: watch.clock,
- After: after,
- Poll: offlinerun.DefaultPoll,
- Watch: watch,
- }
- ints, stop := interrupts()
+ Doors: doors,
+ Probe: func(ctx context.Context) int { return probe(ctx, watch.url) },
+ Policy: policy,
+ Clock: watch.clock,
+ After: after,
+ Poll: offlinerun.DefaultPoll,
+ Watch: watch,
+ Interrupts: ints,
+ }
+ stop := interrupts(ints.Add)
defer stop()
- done, _, err := r.Run(ctx, offlinerun.NewMemory(a.resume), ints)
+ done, _, err := r.Run(ctx, offlinerun.NewMemory(a.resume))
if err != nil {
if doors.code >= 0 {
return doors.code // the first status read said why
@@ -147,28 +149,24 @@ func (m *mover) offlineRun(ctx context.Context, a moveArgs, d moveDeps) int {
return watch.end(done)
}
-// ctrlC is the real Ctrl-C source: each SIGINT is one receive. A hangup
-// (the SSH session dropped) is ignored, so the move goes on (unix only).
-func ctrlC() (<-chan struct{}, func()) {
+// ctrlC is the real Ctrl-C source: each SIGINT is one add. A hangup (the
+// SSH session dropped) is ignored, so the move goes on (unix only).
+func ctrlC(add func()) func() {
ignoreHangup()
sig := make(chan os.Signal, 2)
signal.Notify(sig, os.Interrupt)
- out := make(chan struct{}, 2)
quit := make(chan struct{})
go func() {
for {
select {
case <-sig:
- select {
- case out <- struct{}{}:
- default:
- }
+ add()
case <-quit:
return
}
}
}()
- return out, func() {
+ return func() {
signal.Stop(sig)
close(quit)
}
@@ -200,27 +198,96 @@ var errRunStatus = errors.New("no status view")
// said by saySpoken; Answer.Sentence stays empty).
type runDoors struct {
w *offlineRun
- region string // the region stage and start are sent: --to, then the bound move's
- seen bool // a status view was read
- code int // the exit code the first status read ended with, -1 for none
- last offlineReply // the latest status view
- reply moveReply // the latest POST's reply
+ ints *offlinerun.Interrupts // the run's Ctrl-Cs: they end a wait or a status read
+ region string // the region stage and start are sent: --to, then the bound move's
+ seen bool // a status view was read
+ code int // the exit code the first status read ended with, -1 for none
+ last offlineReply // the latest status view
+ reply moveReply // the latest POST's reply
+}
+
+// wait is one interval between calls (the mover's, 10 s once limited),
+// false when ctx ended or a Ctrl-C came first (finding 2 of the branch
+// review: a back-off never holds a Ctrl-C up).
+func (d *runDoors) wait(ctx context.Context) bool {
+ wctx, release := d.ints.Bind(ctx)
+ defer release()
+ t := time.NewTimer(d.w.m.interval())
+ defer t.Stop()
+ select {
+ case <-wctx.Done():
+ return false
+ case <-t.C:
+ return true
+ }
+}
+
+// get is one status read, which a Ctrl-C ends (the run then takes it).
+// interrupted says that is what ended it.
+func (d *runDoors) get(ctx context.Context) (r moveReply, interrupted bool) {
+ rctx, release := d.ints.Bind(ctx)
+ defer release()
+ r = d.w.m.roundTrip(rctx, http.MethodGet, d.w.m.path, nil)
+ return r, r.failed && rctx.Err() != nil && ctx.Err() == nil
+}
+
+// firstRead is the first status read: as the hand verbs' getReply, it
+// rides out a rate limit and a few failures and says why it gave up (code
+// >= 0), but a Ctrl-C ends it, and its waits, at once (interrupted).
+func (d *runDoors) firstRead(ctx context.Context) (r moveReply, code int, interrupted bool) {
+ m := d.w.m
+ failures := 0
+ for {
+ r, interrupted = d.get(ctx)
+ if interrupted {
+ return moveReply{}, -1, true
+ }
+ ok := !r.failed && r.moved == ""
+ switch {
+ case ok && r.status == http.StatusOK:
+ return r, -1, false
+ case r.moved != "" || r.stopped:
+ m.sayReply(r)
+ return moveReply{}, moveExitError, false
+ case ok && r.status == http.StatusTooManyRequests:
+ m.limit()
+ case ok && r.status >= 400 && r.status < 500:
+ m.sayReply(r)
+ return moveReply{}, moveExitRefused, false
+ default:
+ failures++
+ if failures >= moveTransient {
+ m.sayReply(r)
+ return moveReply{}, moveExitError, false
+ }
+ }
+ if !d.wait(ctx) {
+ if ctx.Err() != nil {
+ moveSay(m.out, moveStopped)
+ return moveReply{}, moveExitError, false
+ }
+ return moveReply{}, -1, true
+ }
+ }
}
func (d *runDoors) Status(ctx context.Context) (offlinerun.View, error) {
var r moveReply
if !d.seen {
// The first read rides out a rate limit and a few failures, and
- // says why it gave up (the hand verbs' getReply).
+ // says why it gave up.
var code int
- if r, code = d.w.m.getReply(ctx, d.w.m.path); code >= 0 {
+ var interrupted bool
+ if r, code, interrupted = d.firstRead(ctx); interrupted {
+ return offlinerun.View{}, errRunStatus
+ } else if code >= 0 {
d.code = code
return offlinerun.View{}, errRunStatus
}
} else {
// Later ones are ridden out by the loop, silently: the move is
// under way and only a view decides anything.
- if r = d.w.m.roundTrip(ctx, http.MethodGet, d.w.m.path, nil); r.failed || r.moved != "" || r.status != http.StatusOK {
+ if r, _ = d.get(ctx); r.failed || r.moved != "" || r.status != http.StatusOK {
return offlinerun.View{}, errRunStatus
}
}
@@ -262,17 +329,45 @@ func (d *runDoors) body(verb, id string) map[string]any {
return b
}
+// send sends one POST, again after the back-off while the rate limit
+// answers 429 (it answers before any door, so nothing was done). The POST
+// itself is never cut short by a Ctrl-C, so its outcome is never made
+// unknown by one; the back-off is. sent is false when no door took it.
+func (d *runDoors) send(ctx context.Context, tail string, body any) (r moveReply, sent bool) {
+ m := d.w.m
+ for {
+ r = m.roundTrip(ctx, http.MethodPost, m.path+tail, body)
+ if r.failed || r.moved != "" || r.status != http.StatusTooManyRequests {
+ return r, true
+ }
+ m.limit()
+ if !d.wait(ctx) {
+ return r, false
+ }
+ }
+}
+
func (d *runDoors) Post(ctx context.Context, verb, id string) offlinerun.Answer {
tail := "/" + verb
if verb == offlinerun.VerbStart {
tail = "" // start is the offline door itself
}
b := d.body(verb, id)
- return d.answer(d.w.m.send(ctx, http.MethodPost, tail, b), b["new"] == true)
+ r, sent := d.send(ctx, tail, b)
+ if !sent {
+ d.reply = moveReply{}
+ return offlinerun.Answer{NotSent: true}
+ }
+ return d.answer(r, b["new"] == true)
}
func (d *runDoors) Nudge(ctx context.Context, id string) offlinerun.Answer {
- return d.answer(d.w.m.send(ctx, http.MethodPost, "/nudge", d.body("nudge", id)), false)
+ r, sent := d.send(ctx, "/nudge", d.body("nudge", id))
+ if !sent {
+ d.reply = moveReply{}
+ return offlinerun.Answer{NotSent: true}
+ }
+ return d.answer(r, false)
}
// answer reads a POST's reply. A 2xx is accepted (and its view teaches the
@@ -488,6 +583,13 @@ func (rw *runWatcher) Interrupted(i offlinerun.Interrupt, _ offlinerun.Memory) {
sayRunCommand(rw.out(), "status", host)
case offlinerun.InterruptAfterOpen:
moveSay(rw.out(), fmt.Sprintf("%s is already served from %s, so the move is not undone. Stopped watching.", host, rw.to(v)))
+ case offlinerun.InterruptOpenUncertain:
+ // The open got no answer: it may or may not be served yet, so
+ // OR40's "already served" would be a guess. status says.
+ moveSay(rw.out(), "Stopped watching. The move is still running. To see where it is, run:")
+ sayRunCommand(rw.out(), "status", host)
+ case offlinerun.InterruptNothingSent:
+ moveSay(rw.out(), "Stopped. Nothing was sent.")
}
}
@@ -543,7 +645,7 @@ func (rw *runWatcher) end(d offlinerun.Done) int {
sayRunCommand(rw.out(), "finish", host)
case offlinerun.ReasonUnexpectedState:
rw.w.headline(r)
- case offlinerun.ReasonStoppedWatching:
+ case offlinerun.ReasonStoppedWatching, offlinerun.ReasonNothingSent:
// said at the Ctrl-C
}
return d.Exit
diff --git a/move_offline_run_test.go b/move_offline_run_test.go
index 4a2126e53..054236179 100644
--- a/move_offline_run_test.go
+++ b/move_offline_run_test.go
@@ -38,6 +38,16 @@ type offlineSim struct {
// beforePost, when set, runs as a POST arrives, before the door acts:
// a second operator's hand between the run's status read and its POST.
beforePost func(s *offlineSim, verb string)
+ // onGet, when set, runs as a status read is answered: a Ctrl-C that
+ // lands while the read is on the wire, say.
+ onGet func(s *offlineSim)
+ // limitGets is how many status reads the rate limit answers 429
+ // first; limit how many POSTs of a door. onLimit runs at each.
+ limitGets int
+ limit map[string]int
+ onLimit func()
+ // onDrop runs as a reply is thrown away (drop).
+ onDrop func()
}
const simEpoch = int64(1794724320) // 2026-11-15T06:32:00Z
@@ -246,6 +256,9 @@ type runSeams struct {
probe func() int
// ctrlC, when set, says at which polls (1-based) a Ctrl-C arrives.
ctrlC func(poll int) bool
+ // press, when set, is filled with the Ctrl-C itself, for a test's
+ // console hooks to press at any point.
+ press *func()
}
// runOfflineRun runs `carlos move offline run` against c on a fake clock
@@ -256,7 +269,7 @@ func runOfflineRun(t *testing.T, c *fakeOfflineConsole, seams runSeams, args ...
t.Cleanup(ts.Close)
var out bytes.Buffer
clock := time.Date(2026, 11, 15, 6, 12, 0, 0, time.UTC)
- ints := make(chan struct{}, 4)
+ var add func()
polls := 0
if seams.probe == nil {
seams.probe = func() int { return 200 }
@@ -279,7 +292,7 @@ func runOfflineRun(t *testing.T, c *fakeOfflineConsole, seams runSeams, args ...
if seams.ctrlC != nil && seams.ctrlC(polls) {
// The Ctrl-C lands during this poll's sleep: the
// timer never fires, so the run takes the Ctrl-C.
- ints <- struct{}{}
+ add()
return nil
}
fired := make(chan time.Time, 1)
@@ -292,7 +305,13 @@ func runOfflineRun(t *testing.T, c *fakeOfflineConsole, seams runSeams, args ...
}
return seams.probe()
},
- interrupts: func() (<-chan struct{}, func()) { return ints, func() {} },
+ interrupts: func(a func()) func() {
+ add = a
+ if seams.press != nil {
+ *seams.press = a
+ }
+ return func() {}
+ },
}
args = append([]string{"offline", "run"}, args...)
args = append(args, "--host", oHost, "--account", "bab", "--app", "shop")
@@ -785,3 +804,84 @@ func TestMoveOfflineRunLostFirstStageReply(t *testing.T) {
t.Fatalf("output:\n%s\nwant:\n%s", out, want)
}
}
+
+// TestMoveOfflineRunCtrlCDuringStatusRead (finding 2): a Ctrl-C that lands
+// while the read is on the wire, and that read says landed with next open:
+// the run aborts, and open is never sent.
+func TestMoveOfflineRunCtrlCDuringStatusRead(t *testing.T) {
+ var press func()
+ sim := &offlineSim{onGet: func(s *offlineSim) {
+ if s.state == "offline-landed" {
+ s.onGet = nil
+ press()
+ }
+ }}
+ c := &fakeOfflineConsole{sim: sim}
+ code, out := runOfflineRun(t, c, runSeams{press: &press}, "--to", "uk")
+ if code != 3 {
+ t.Fatalf("exit %d:\n%s", code, out)
+ }
+ if got := doorsOf(c.postsSent()); got != "stage,start,seal,flip,abort,finish" {
+ t.Fatalf("doors %s:\n%s", got, out)
+ }
+ wantLines(t, out, "Moving acme.shop.example.com back to ie. Press Ctrl-C again to stop watching.", orBack)
+}
+
+// TestMoveOfflineRunCtrlCDuringRateLimit (finding 2): the rate limit's
+// back-off ends at a Ctrl-C. On a POST: no door took it, so nothing was
+// written; the first Ctrl-C aborts. On the first status read, a Ctrl-C as
+// the limit answers ends the read itself: nothing was sent at all, and the
+// run stops there.
+func TestMoveOfflineRunCtrlCDuringRateLimit(t *testing.T) {
+ var press func()
+ sim := &offlineSim{limit: map[string]int{"seal": 1}, onLimit: func() { press() }}
+ c := &fakeOfflineConsole{sim: sim}
+ code, out := runOfflineRun(t, c, runSeams{press: &press}, "--to", "uk")
+ if code != 3 {
+ t.Fatalf("exit %d:\n%s", code, out)
+ }
+ if n := c.posted("/seal"); n != 0 {
+ t.Fatalf("a door took %d seals:\n%s", n, out)
+ }
+ if got := doorsOf(c.postsSent()); got != "stage,start,seal,abort,finish" {
+ t.Fatalf("doors %s:\n%s", got, out)
+ }
+ wantLines(t, out, "Moving acme.shop.example.com back to ie. Press Ctrl-C again to stop watching.", orBack)
+
+ sim = &offlineSim{limitGets: 1, onLimit: func() { press() }}
+ c = &fakeOfflineConsole{sim: sim}
+ code, out = runOfflineRun(t, c, runSeams{press: &press}, "--to", "uk")
+ if code != 4 {
+ t.Fatalf("exit %d:\n%s", code, out)
+ }
+ if posts := c.postsSent(); len(posts) != 0 {
+ t.Fatalf("posted %q", posts)
+ }
+ if out != "Stopped. Nothing was sent.\n" {
+ t.Fatalf("output:\n%s", out)
+ }
+}
+
+// TestMoveOfflineRunCtrlCAfterUncertainOpen (finding 3): an open whose
+// reply was lost, then Ctrl-C. Nothing is undone (it may have committed),
+// and the run does not say the instance is served (it may not have): OR29
+// and the status command.
+func TestMoveOfflineRunCtrlCAfterUncertainOpen(t *testing.T) {
+ var press func()
+ c := &fakeOfflineConsole{sim: &offlineSim{drop: "open", onDrop: func() { press() }}}
+ code, out := runOfflineRun(t, c, runSeams{press: &press}, "--to", "uk")
+ if code != 4 {
+ t.Fatalf("exit %d:\n%s", code, out)
+ }
+ if got := doorsOf(c.postsSent()); got != "stage,start,seal,flip,open" {
+ t.Fatalf("doors %s:\n%s", got, out)
+ }
+ wantLines(t, out,
+ "open sent, no answer, checking where the move stands",
+ "Stopped watching. The move is still running. To see where it is, run:",
+ " carlos move offline status --host acme.shop.example.com",
+ )
+ if strings.Contains(out, "already served") {
+ t.Fatalf("said served after an open with no answer:\n%s", out)
+ }
+}
diff --git a/move_test.go b/move_test.go
index 9038adb5e..1bf3c89c9 100644
--- a/move_test.go
+++ b/move_test.go
@@ -1146,8 +1146,20 @@ func (c *fakeOfflineConsole) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
if r.Method == http.MethodGet && r.URL.Path == tOfflinePth && c.sim != nil {
+ if c.sim.limitGets > 0 {
+ c.sim.limitGets--
+ if c.sim.onLimit != nil {
+ c.sim.onLimit()
+ }
+ write(http.StatusTooManyRequests, frame{"error": "too many requests"})
+ return
+ }
c.i++
- write(http.StatusOK, c.sim.get())
+ v := c.sim.get()
+ if c.sim.onGet != nil {
+ c.sim.onGet(c.sim)
+ }
+ write(http.StatusOK, v)
return
}
if r.Method == http.MethodGet && r.URL.Path == tOfflinePth {
@@ -1186,8 +1198,24 @@ func (c *fakeOfflineConsole) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
+ verb := strings.TrimPrefix(tail, "/")
+ if tail == "" {
+ verb = "start"
+ }
+ if c.sim.limit[verb] > 0 {
+ // The rate limit answers before any door: nothing is done.
+ c.sim.limit[verb]--
+ if c.sim.onLimit != nil {
+ c.sim.onLimit()
+ }
+ write(http.StatusTooManyRequests, frame{"error": "too many requests"})
+ return
+ }
c.n[tail]++
code, v, drop := c.sim.post(tail, createOnly)
+ if drop && c.sim.onDrop != nil {
+ c.sim.onDrop()
+ }
if drop {
// The write committed; the reply is lost on the way back.
if hj, ok := w.(http.Hijacker); ok {
diff --git a/offline_move_e2e_test.go b/offline_move_e2e_test.go
index ecf3a2fb2..d0a673708 100644
--- a/offline_move_e2e_test.go
+++ b/offline_move_e2e_test.go
@@ -660,7 +660,7 @@ func (e *offlineE2E) runOffline(t *testing.T, boxes *offlineBoxScript, args ...s
}
return 200
},
- interrupts: func() (<-chan struct{}, func()) { return nil, func() {} },
+ interrupts: func(func()) func() { return func() {} },
}
args = append(append([]string{"offline", "run", "--to", "uk"}, args...), "--host", e.host, "--account", release.OpsAccount, "--app", "shop")
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)