design: re-place a Design step its runner never got below 39
Final review F5 (Fable 2). A Design dispatch queued for a runner that
reconnected below 39 before receiving it was dropped at send (Encode
refuses it), so the runner never asked to start it, refuseStaleDesign
never ran, and the step stayed queued for good with a Design run
spent.
On Connect below 39 (fenceDesign, with the other protocol fences),
each of that runner's queued, unstarted Design steps is replaced: the
undelivered dispatch voided, a new attempt placed, which waits with
design_runner_old and goes once the runner is back at 39. The
unlaunched attempt's run is taken back (designUnran, never below
none). A step that started is left to its start checks.
Test (RED first):
go test ./internal/equipo/chat/ -run TestReconnectBelow39BeforeDelivery
RED: "after a reconnect at 38: {... Attempt:1 ... RunnerID:RUNNER1}"
GREEN: ok; chat -run 'Connect|Reconnect|Design|Runner|Taste|Forget'
Mutations (each restored):
no fenceDesign call -> the RED above
fence started steps too -> "a started attempt was re-placed"
FAIL
no designUnran in the fence -> "an undelivered attempt counted as
a run: 1" FAIL
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+111
−0
internal/equipo/chat/design_routing.go+12 −0internal/equipo/chat/runner.go+4 −0internal/equipo/chat/runners.go+44 −0internal/equipo/chat/taste_dispatch_test.go+51 −0
diff --git a/internal/equipo/chat/design_routing.go b/internal/equipo/chat/design_routing.go| index cfa1c7d8..9dfe20b0 100644 |
| --- a/internal/equipo/chat/design_routing.go |
| +++ b/internal/equipo/chat/design_routing.go |
| @@ -158,6 +158,18 @@ func (t *txn) designRan(k *taskRecord) error { |
| return t.db.Model(&taskRecord{}).Where("id = ?", k.ID).UpdateColumn("design_runs", gorm.Expr("design_runs + 1")).Error |
| } |
| +// designUnran takes back the count of one Design attempt that was sent |
| +// and never launched (refused at its start, or never delivered): only |
| +// attempts that ran count against DesignRunLimit, so replacing one for |
| +// fresh notes or a runner at 39 spends no run. Never below none (the |
| +// person may have acted since it was counted). |
| +func (t *txn) designUnran(k *taskRecord) error { |
| + if k.DesignRuns > 0 { |
| + k.DesignRuns-- |
| + } |
| + return t.db.Model(&taskRecord{}).Where("id = ? AND design_runs > 0", k.ID).UpdateColumn("design_runs", gorm.Expr("design_runs - 1")).Error |
| +} |
| + |
| // personActed is person (the session's) acting on task id (a message in |
| // its thread, an answer on one of its cards, Run it now): when they are |
| // the task's creator and an owner now, read in this transaction, its |
diff --git a/internal/equipo/chat/runner.go b/internal/equipo/chat/runner.go| index a5600a2b..8fa8aa2b 100644 |
| --- a/internal/equipo/chat/runner.go |
| +++ b/internal/equipo/chat/runner.go |
| @@ -141,6 +141,10 @@ func (s *Store) Connect(ctx context.Context, p RunnerPrincipal, process string, |
| if err := t.fenceResearch(lease); err != nil { |
| return err |
| } |
| + // Nor a Design step it never received, below protocol 39. |
| + if err := t.fenceDesign(lease); err != nil { |
| + return err |
| + } |
| // A primary's detected addresses, for Research's site inventory; |
| // its steps waiting for the inventory are looked at again. |
| if role == wire.RolePrimary { |
diff --git a/internal/equipo/chat/runners.go b/internal/equipo/chat/runners.go| index a81ef832..c032e378 100644 |
| --- a/internal/equipo/chat/runners.go |
| +++ b/internal/equipo/chat/runners.go |
| @@ -1086,6 +1086,50 @@ func (t *txn) refuseStaleDesign(l Lease, st *stepRecord) (string, error) { |
| return "withdrawn", t.saveStep(st, &k) |
| } |
| +// fenceDesign re-places a runner's Design steps it was sent and never |
| +// started, when it connects below protocol 39 (spec 8.7): the dispatch |
| +// is never sent there (Encode refuses it), so the runner would never ask |
| +// to start it and the start check would never run. Each is a new |
| +// attempt, waiting with design_runner_old for a runner at 39; the |
| +// unlaunched attempt is not counted as a Design run. A step that started |
| +// is left to its own checks (Authorize refuses it below 39). |
| +func (t *txn) fenceDesign(l Lease) error { |
| + if l.Protocol >= wire.ProtocolDesignFeedback { |
| + return nil |
| + } |
| + var rows []stepRecord |
| + if err := t.db.Where("runner_id = ? AND specialist = ? AND specialist_id <> '' AND state = ? AND started_at IS NULL AND withdrawn = ?", l.Runner.RunnerID, wire.SpecialistEngineering, wire.StepQueued, false).Find(&rows).Error; err != nil { |
| + return err |
| + } |
| + for i := range rows { |
| + s := &rows[i] |
| + design, err := t.designStep(*s) |
| + if err != nil { |
| + return err |
| + } |
| + if !design { |
| + continue |
| + } |
| + k, err := t.loadTask(s.TaskID) |
| + if err != nil { |
| + return err |
| + } |
| + if wire.FinalStage(k.Stage) || k.CancelRequested { |
| + continue |
| + } |
| + if err := t.designUnran(&k); err != nil { |
| + return err |
| + } |
| + if err := t.replace(&k, s); err != nil { |
| + return err |
| + } |
| + if err := t.saveStep(s, &k); err != nil { |
| + return err |
| + } |
| + } |
| + return nil |
| +} |
| + |
| // waitingPlacements are the placements of a step waiting to be placed, |
| // a runner that can run Design's among them. A Design step the autonomy |
| // limit held goes only once its person acts (releaseDesignHeld). |
diff --git a/internal/equipo/chat/taste_dispatch_test.go b/internal/equipo/chat/taste_dispatch_test.go| index 8aae027f..30c7ea36 100644 |
| --- a/internal/equipo/chat/taste_dispatch_test.go |
| +++ b/internal/equipo/chat/taste_dispatch_test.go |
| @@ -643,3 +643,54 @@ func TestDesignQuestionForgetRevision(t *testing.T) { |
| t.Fatalf("Engineering's question: %+v", asks) |
| } |
| } |
| + |
| +// TestReconnectBelow39BeforeDelivery (final review, F5 / Fable 2): a |
| +// Design step dispatched at 39 that the runner never received (Encode |
| +// refuses the dispatch below 39, so it is never sent) is re-placed when |
| +// the runner reconnects below 39: a new attempt waiting with |
| +// design_runner_old, the old command voided; back at 39 it goes. A |
| +// Design step that already started is left to its start checks. |
| +func TestReconnectBelow39BeforeDelivery(t *testing.T) { |
| + r, l := designReady(t) |
| + r.exec("CREATE TABLE IF NOT EXISTS runners (id text PRIMARY KEY, person text, role text, label text, removing_at datetime, tainted_at datetime, revoked_at datetime)") |
| + r.exec("INSERT INTO runners(id, person, role) VALUES ('RUNNER1', ?, ?)", paul, wire.RolePrimary) |
| + task := r.designTask(l, r.makerSpecialist(), wire.KindChange) |
| + s := r.makerStep(task) |
| + if s.RunnerID != "RUNNER1" || !r.attemptSent(s) || s.StartedAt != nil { |
| + t.Fatalf("placed %+v", s) |
| + } |
| + old := designHello(wire.ToolHealthy, claudeSub) |
| + old.Protocol = wire.ProtocolDesign |
| + if _, err := r.s.Connect(r.ctx, l.Runner, "p2", old, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + got := r.stepNow(s.ID) |
| + if got.Attempt != s.Attempt+1 || got.State != wire.StepQueued || got.Placement != wire.PlacementDesign || got.Reason != reasonDesignProtocol || got.RunnerID != "" || r.attemptSent(got) { |
| + t.Fatalf("after a reconnect at 38: %+v", got) |
| + } |
| + if n := r.count("SELECT count(*) FROM runner_commands WHERE kind = ? AND acked_at IS NULL AND body LIKE ?", wire.FrameDispatch, attemptLike(s.ID, s.Attempt)); n != 0 { |
| + t.Fatalf("the undelivered dispatch is still served: %d", n) |
| + } |
| + if k := r.load(task); k.DesignRuns != 0 { |
| + t.Fatalf("an undelivered attempt counted as a run: %d", k.DesignRuns) |
| + } |
| + cur := designHello(wire.ToolHealthy, claudeSub) |
| + cur.Protocol = wire.ProtocolDesignFeedback |
| + l2, err := r.s.Connect(r.ctx, l.Runner, "p3", cur, "") |
| + r.must(err) |
| + got = r.stepNow(s.ID) |
| + if got.RunnerID != "RUNNER1" || got.Placement != "" || !r.attemptSent(got) || got.DispatchProtocol != wire.ProtocolDesignFeedback { |
| + t.Fatalf("back at 39: %+v", got) |
| + } |
| + // Started: the reconnect leaves it (its next check refuses it). |
| + r.ackDispatches(s.ID) |
| + if refused := r.authorize(l2, got, ""); refused != "" { |
| + t.Fatalf("start %q", refused) |
| + } |
| + if _, err := r.s.Connect(r.ctx, l.Runner, "p4", old, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + if now := r.stepNow(s.ID); now.Attempt != got.Attempt || now.StartedAt == nil { |
| + t.Fatalf("a started attempt was re-placed: %+v", now) |
| + } |
| +} |