Mac slice F task 3 fix 1: re-prepare on a new primary, keep Mac need
Astra round 1 on task 3. A primary that connects as a replacement, or below protocol 37, invalidates each base preparation it cannot answer: the attempt moves on, so a report for the invalidated request (or from the old primary) is refused and logged, and the new attempt gets a fresh prepare_base or waits waiting_mac_update naming the primary. A task once pinned to a Mac worker, or with a round published for one, keeps its Mac need for Engineering whatever the catalog says later, so Recover and Retry never send it to Linux. Tasks never on a Mac keep mac-placement-wait's behaviour. Finding 3 (protocol 1 exception) declined by the controller. Budgets re-measured on HEAD: chat 38656/12096, thintest 38258.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+225
−8
.rastrillo/budgets.txt+2 −2internal/equipo/chat/base_prepare.go+42 −0internal/equipo/chat/runner.go+3 −0internal/equipo/chat/runners.go+23 −6internal/thintest/mac_engineering_contract_test.go+155 −0
Intent
diff --git a/.rastrillo/budgets.txt b/.rastrillo/budgets.txt| index d5286bdf..8fc94803 100644 |
| --- a/.rastrillo/budgets.txt |
| +++ b/.rastrillo/budgets.txt |
| @@ -56,7 +56,7 @@ perf GET /api/v1/tasks/{id} skip Thin v1 person API: bearer-only JSON for device |
| perf GET /api/v1/assistant/snapshot skip Assistant person API: bearer-only JSON for Base's chat, not a browser screen. |
| perf GET /api/v1/assistant/messages skip Assistant person API: bearer-only JSON for Base's chat, not a browser screen. |
| perf GET /api/v1/assistant/events skip Assistant event stream (Server-Sent Events), bearer-only and long-lived; not a screen. |
| -size internal/equipo/chat 38586 12096 File facts/diffs, previews, merge authority, unfinished work, CI feedback, notification receipts, Assistant files, places, waiting cards, Mac waits, Mac Engineering; measured 38586/12096. |
| +size internal/equipo/chat 38656 12096 File facts/diffs, previews, merge authority, unfinished work, CI feedback, notification receipts, Assistant files, places, waiting cards, Mac waits, Mac Engineering; measured 38656/12096. |
| perf GET /api/v1/questions/{id} skip Thin v1 person API: bearer-only JSON for devices; the browser reads questions on /tasks/{id}. |
| perf GET /api/v1/steps/{id}/log skip Thin v1 person API: bearer-only JSON for devices; the browser shows the log tail on /tasks/{id}. |
| perf GET /api/v1/connections/preference skip Connections slice 1 person API: bearer-only JSON for devices; the browser shows the preference on /conversation/connections. |
| @@ -71,7 +71,7 @@ perf GET /tasks/{id}/files/{file} skip Research kept images: the bytes of one im |
| perf GET /api/v1/runners skip Mac runners person API: bearer-only JSON for devices; the browser shows runners in the Team section. |
| time cmd/equipo-runner 60s TestReleaseRunnerHasNoFakeWorker builds the release runner and the thin package under testbuild (about 12s locally). |
| size internal/headless - 11977 Grant scenarios (#48); sign-in email at 1280 and 390; Assistant files end to end; the restart wait; the live site waits for a place; a card answer settles at the bottom; measured 11977 test lines. |
| -size internal/thintest - 38103 Protocol 31 contracts, 35 diffs, stuck tasks, no-access merges, held retries, unfinished work, free rounds, previews, Assistant files, places, Mac waits, Mac Engineering; measured 38103. |
| +size internal/thintest - 38258 Protocol 31 contracts, 35 diffs, stuck tasks, no-access merges, held retries, unfinished work, free rounds, previews, Assistant files, places, Mac waits, Mac Engineering; measured 38258. |
| perf GET /api/v1/projects/{id}/merge-policy skip Merge authority person API: bearer-only JSON for devices; the browser reads the policy on /projects/{id}. |
| perf GET /api/v1/usage skip Thin v1 person API: bearer-only JSON for devices; the browser reads usage through /usage. |
diff --git a/internal/equipo/chat/base_prepare.go b/internal/equipo/chat/base_prepare.go| index 3b0580bd..3e738f97 100644 |
| --- a/internal/equipo/chat/base_prepare.go |
| +++ b/internal/equipo/chat/base_prepare.go |
| @@ -175,3 +175,45 @@ func helloEngineering(raw string) string { |
| func awaitingPublication(s *stepRecord) bool { |
| return s.Specialist == wire.SpecialistEngineering && s.State == wire.StepDone && !s.Pushed |
| } |
| + |
| +// reprepareBases runs when the person's primary connects: a replacement, |
| +// or the same runner at another protocol. A base preparation it cannot |
| +// answer is invalidated: one asked of another runner (a primary replaced |
| +// after it acked, rebind moving only unacked commands), or any while it |
| +// speaks below protocol 37. The attempt is replaced, so a report for the |
| +// invalidated request names an attempt that is not current and is |
| +// refused; the next is placed again: a fresh prepare_base to this |
| +// primary, or waiting_mac_update naming it. |
| +func (t *txn) reprepareBases(person, primary string, protocol int) error { |
| + if current, err := t.s.authority.PrimaryRunner(t.db, person); err != nil || current != primary { |
| + return err |
| + } |
| + var rows []stepRecord |
| + if err := t.db.Where("person = ? AND placement = ? AND state = ?", person, wire.PlacementBase, wire.StepQueued).Find(&rows).Error; err != nil { |
| + return err |
| + } |
| + for i := range rows { |
| + s := rows[i] |
| + var mine int64 |
| + if err := t.db.Model(&commandRecord{}).Where("runner_id = ? AND kind = ? AND body LIKE ?", primary, wire.FramePrepareBase, attemptLike(s.ID, s.Attempt)).Count(&mine).Error; err != nil { |
| + return err |
| + } |
| + if mine > 0 && protocol >= wire.ProtocolMacEngineering { |
| + continue |
| + } |
| + k, err := t.loadTask(s.TaskID) |
| + if err != nil { |
| + return err |
| + } |
| + if wire.FinalStage(k.Stage) || k.CancelRequested || k.MoveProject != "" || s.Retired { |
| + continue |
| + } |
| + if err := t.replace(&k, &s); err != nil { |
| + return err |
| + } |
| + if err := t.saveStep(&s, &k); err != nil { |
| + return err |
| + } |
| + } |
| + return nil |
| +} |
diff --git a/internal/equipo/chat/runner.go b/internal/equipo/chat/runner.go| index 1f2b03b0..bf19bd48 100644 |
| --- a/internal/equipo/chat/runner.go |
| +++ b/internal/equipo/chat/runner.go |
| @@ -85,6 +85,9 @@ func (s *Store) Connect(ctx context.Context, p RunnerPrincipal, process string, |
| if err := t.rebind(p); err != nil { |
| return err |
| } |
| + if err := t.reprepareBases(p.Person, p.RunnerID, lease.Protocol); err != nil { |
| + return err |
| + } |
| } |
| // The Assistant: nor its turns, below protocol 19. |
| if err := t.fenceLanes(lease); err != nil { |
diff --git a/internal/equipo/chat/runners.go b/internal/equipo/chat/runners.go| index 1361dda2..2417b407 100644 |
| --- a/internal/equipo/chat/runners.go |
| +++ b/internal/equipo/chat/runners.go |
| @@ -407,19 +407,40 @@ func (t *txn) checksNeeds(k taskRecord, s stepRecord) ([]string, error) { |
| // Xcode (and the simulator) when the repository's gate needs Xcode, as |
| // the primary's index of its default branch says (the base, not anything |
| // the change wrote), and claude to make the change there (slice F). |
| -// Anything else needs nothing. |
| +// A task whose Engineering was once on a Mac keeps that need whatever the |
| +// catalog says later (onMac): its rounds never move to Linux. Anything |
| +// else needs nothing. |
| func (t *txn) engineeringNeeds(k taskRecord, person string) ([]string, error) { |
| info, err := t.repoInfo(k, person) |
| if err != nil { |
| return nil, err |
| } |
| - needs := macNeeds(info.NeedsMacOS, info) |
| + mac := info.NeedsMacOS |
| + if !mac { |
| + if mac, err = t.onMac(k); err != nil { |
| + return nil, err |
| + } |
| + } |
| + needs := macNeeds(mac, info) |
| if len(needs) > 0 { |
| needs = append(needs, wire.TagClaude) |
| } |
| return needs, nil |
| } |
| +// onMac reports whether the task's Engineering was ever on a Mac: pinned |
| +// to a worker (removed or not), or a round's commit published for one. |
| +func (t *txn) onMac(k taskRecord) (bool, error) { |
| + var n int64 |
| + if k.EngineeringRunner != "" { |
| + if err := t.db.Raw(`SELECT count(*) FROM runners WHERE id = ? AND role = ?`, k.EngineeringRunner, wire.RoleWorker).Scan(&n).Error; err != nil || n > 0 { |
| + return n > 0, err |
| + } |
| + } |
| + err := t.db.Model(&stepRecord{}).Where("task_id = ? AND specialist = ? AND coalesce(publication, '') <> ''", k.ID, wire.SpecialistEngineering).Count(&n).Error |
| + return n > 0, err |
| +} |
| + |
| // macTags are needs without claude: what makes a runner a Mac for them. |
| func macTags(needs []string) []string { |
| return slices.DeleteFunc(slices.Clone(needs), func(t string) bool { return t == wire.TagClaude }) |
| @@ -684,10 +705,6 @@ func (t *txn) engineeringPlace(k taskRecord, s stepRecord, primary string) (engi |
| } |
| return engineeringPlacement{placement: wire.PlacementMacEngineering, needs: mac}, nil |
| } |
| - if len(needs) == 0 { |
| - // Pinned to a worker: what it needs to make a change at all. |
| - needs = []string{wire.TagClaude} |
| - } |
| mac := macTags(needs) |
| if prim == nil { |
| return engineeringPlacement{placement: wire.PlacementWaiting, needs: needs}, nil |
diff --git a/internal/thintest/mac_engineering_contract_test.go b/internal/thintest/mac_engineering_contract_test.go| index f9710e49..925c26df 100644 |
| --- a/internal/thintest/mac_engineering_contract_test.go |
| +++ b/internal/thintest/mac_engineering_contract_test.go |
| @@ -654,6 +654,11 @@ func TestContractMacEngineeringPinnedRemoved(t *testing.T) { |
| if _, err := r.client.RecoverTask(r.ctx, task); err != nil { |
| t.Fatal(err) |
| } |
| + // The primary's catalog no longer names the repository: the task was |
| + // made on a Mac, so it still needs one. |
| + h := primaryHello(0) |
| + h.Protocol, h.Catalog = wire.ProtocolMacEngineering, []wire.RepoInfo{} |
| + r.primary.heartbeat(h) |
| if acc, err := r.client.RetryTask(r.ctx, task); err != nil || acc.Attempt != 2 { |
| t.Fatalf("retry %+v %v", acc, err) |
| } |
| @@ -929,3 +934,153 @@ func TestContractMacMadeNeverAutoMerges(t *testing.T) { |
| t.Fatalf("the person's merge %+v", m) |
| } |
| } |
| + |
| +// A primary replaced after it acked prepare_base: the preparation it can |
| +// no longer answer is invalidated (the attempt moves on) and asked of the |
| +// replacement, which must speak 37 (until then waiting_mac_update naming |
| +// it). The old primary's report, and a report for the invalidated |
| +// request, are refused and logged. |
| +func TestContractPrepareBaseAfterPrimaryReplaced(t *testing.T) { |
| + r := newMacRig(t) |
| + w := r.newMacEngineer("Tito Studio", wire.EngineeringReady) |
| + // Work the old primary may have started (a Linux change), so its |
| + // removal waits for that proof and it stays connected, removing. |
| + plain := primaryHello(0) |
| + plain.Protocol = wire.ProtocolMacEngineering |
| + r.primary.heartbeat(plain) |
| + started := r.macTask("Linux change") |
| + d0 := r.fromPrimary(wire.FrameDispatch).(*wire.DispatchCommand) |
| + if d0.TaskID != started || !r.primary.lease(d0.StepID, d0.Attempt) { |
| + t.Fatalf("plain engineering %+v", d0) |
| + } |
| + mac := primaryHello(0) |
| + mac.Protocol = wire.ProtocolMacEngineering |
| + mac.Catalog[0].NeedsMacOS = true |
| + r.primary.heartbeat(mac) |
| + task := r.macTask("iOS replaced primary") |
| + pb := r.prepared(task) |
| + old := r.primary.fr.Welcome.RunnerID |
| + r.ackedBy(old, wire.FramePrepareBase) |
| + r.remove(old, false) |
| + if r.revokedAt(old) { |
| + t.Fatal("revoked before its proof") |
| + } |
| + token := r.inst.EnrolRunner(t).Token |
| + h := primaryHello(0) |
| + h.Protocol, h.Label = wire.ProtocolChangeDiffs, "docs-t2" |
| + h.Catalog[0].NeedsMacOS = true |
| + p35, err := DialRunnerHello(r.ctx, r.inst.URL, token, h) |
| + if err != nil || p35.Welcome.Role != wire.RolePrimary { |
| + t.Fatalf("replacement %+v %v", p35, err) |
| + } |
| + s := r.engineeringStep(task) |
| + if s.Attempt != 2 || s.State != wire.StepQueued || s.Placement != wire.PlacementMacUpdate || s.RunnerLabel != "docs-t2" || r.needsYou(task) != wire.NeedsMacUpdate { |
| + t.Fatalf("with a replacement below 37: %+v needs %q", s, r.needsYou(task)) |
| + } |
| + if n := r.unacked(p35.Welcome.RunnerID, wire.FramePrepareBase); n != 0 { |
| + t.Fatalf("%d prepare_base for a primary below 37", n) |
| + } |
| + // The old primary's late answer, on its own connection. |
| + r.primary.send(wire.FrameBase, wire.BaseReport{TaskID: task, StepID: pb.StepID, Attempt: 1, BaseSHA: sha(1)}) |
| + if s := r.engineeringStep(task); s.BaseSHA != "" || s.Attempt != 2 { |
| + t.Fatalf("the old primary's base bound: %+v", s) |
| + } |
| + if !strings.Contains(r.inst.Logs(), "base_refused: not the primary") { |
| + t.Fatal("the old primary's report not logged") |
| + } |
| + // The replacement updated: asked afresh, for the new attempt. |
| + p35.Close() |
| + h.Protocol = wire.ProtocolMacEngineering |
| + p37, err := DialRunnerHello(r.ctx, r.inst.URL, token, h) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(p37.Close) |
| + np := &peer{t: t, r: r.toolRig, fr: p37} |
| + _, body := np.expect(wire.FramePrepareBase) |
| + pb2 := body.(*wire.PrepareBaseCommand) |
| + if pb2.StepID != pb.StepID || pb2.Attempt != 2 || !pb2.Valid() { |
| + t.Fatalf("fresh prepare_base %+v", pb2) |
| + } |
| + // A report for the invalidated request. |
| + np.send(wire.FrameBase, wire.BaseReport{TaskID: task, StepID: pb.StepID, Attempt: 1, BaseSHA: sha(1)}) |
| + if s := r.engineeringStep(task); s.BaseSHA != "" || s.Placement != wire.PlacementBase { |
| + t.Fatalf("the invalidated request bound: %+v", s) |
| + } |
| + if !strings.Contains(r.inst.Logs(), "base_refused: attempt not current") { |
| + t.Fatal("the invalidated request's report not logged") |
| + } |
| + np.send(wire.FrameBase, wire.BaseReport{TaskID: task, StepID: pb2.StepID, Attempt: 2, BaseSHA: sha(2)}) |
| + _, body = w.expect(wire.FrameDispatch) |
| + if d := body.(*wire.DispatchCommand); d.Attempt != 2 || d.BaseSHA != sha(2) { |
| + t.Fatalf("dispatch %+v", d) |
| + } |
| +} |
| + |
| +// ackedBy waits until runner's commands of kind are all acked. |
| +func (r *runnersRig) ackedBy(runner, kind string) { |
| + r.t.Helper() |
| + deadline := time.Now().Add(5 * time.Second) |
| + for r.unacked(runner, kind) != 0 { |
| + if time.Now().After(deadline) { |
| + r.t.Fatalf("%s not acked by %s", kind, runner) |
| + } |
| + time.Sleep(20 * time.Millisecond) |
| + } |
| +} |
| + |
| +func (r *runnersRig) unacked(runner, kind string) int { |
| + r.t.Helper() |
| + var n int |
| + if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE runner_id = ? AND kind = ? AND acked_at IS NULL", runner, kind).Scan(&n); err != nil { |
| + r.t.Fatal(err) |
| + } |
| + return n |
| +} |
| + |
| +// The same primary back below protocol 37 after it acked prepare_base |
| +// cannot answer it: invalidated, and the attempt waits for its update. |
| +func TestContractPrepareBasePrimaryDowngraded(t *testing.T) { |
| + r := newMacRig(t) |
| + r.newMacEngineer("Tito Studio", wire.EngineeringReady) |
| + task := r.macTask("iOS downgraded primary") |
| + r.prepared(task) |
| + r.ackedBy(r.primary.fr.Welcome.RunnerID, wire.FramePrepareBase) |
| + r.primary.fr.Close() |
| + h := primaryHello(0) |
| + h.Protocol = wire.ProtocolChangeDiffs |
| + h.Catalog[0].NeedsMacOS = true |
| + p, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, h) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(p.Close) |
| + if s := r.engineeringStep(task); s.Attempt != 2 || s.Placement != wire.PlacementMacUpdate || s.RunnerLabel != "docs-t1" || r.needsYou(task) != wire.NeedsMacUpdate { |
| + t.Fatalf("after the downgrade: %+v", s) |
| + } |
| +} |
| + |
| +// Pinned to a Mac that is removed before anything it made was published: |
| +// the task still needs a Mac when the catalog no longer says so. |
| +func TestContractMacEngineeringPinnedBeforeCommit(t *testing.T) { |
| + r := newMacRig(t) |
| + a := r.newMacEngineer("Mac A", wire.EngineeringReady) |
| + task, d := r.macDispatch(a, "iOS early removal", sha(1)) |
| + if !a.lease(d.StepID, d.Attempt) { |
| + t.Fatal("start refused") |
| + } |
| + r.remove(a.fr.Welcome.RunnerID, true) |
| + if _, err := r.client.RecoverTask(r.ctx, task); err != nil { |
| + t.Fatal(err) |
| + } |
| + h := primaryHello(0) |
| + h.Protocol, h.Catalog = wire.ProtocolMacEngineering, []wire.RepoInfo{} |
| + r.primary.heartbeat(h) |
| + if acc, err := r.client.RetryTask(r.ctx, task); err != nil || acc.Attempt != 2 { |
| + t.Fatalf("retry %+v %v", acc, err) |
| + } |
| + if s := r.engineeringStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMac || s.RunnerID != "" { |
| + t.Fatalf("after recovery: %+v", s) |
| + } |
| + r.primary.none(wire.FrameDispatch, 100*time.Millisecond) |
| +} |