oficina / equipo Public

Mac slice F wb fix 2 #3, #4: commits between primaries, Retry pruned

Whole-branch review round 2, findings 3 and 4. A worker's commit
that arrives while there is no primary now waits visibly: the step
takes the existing waiting_runner placement and its line says it is
waiting for the main runner to publish it (work.step.waiting_publish,
pending). When a primary connects, reconcileUnpublished reads every
worker Engineering step with a candidate, no publication and no live
outbound transfer from the durable step records, and opens its
outbound transfer to that primary (a commit after a new primary is
enrolled opens it at once, as before).

Retry of a failed publication whose transfer the daily sweep pruned
rebuilds a fresh outbound transfer from the step record (the worker
keeps the candidate under KeepUntil and answers a second send_pack);
with no primary it waits as above.

Regressions: TestContractCommitBetweenPrimaries,
TestContractPublicationRetryAfterPrune, TestWorkerCommitWaitsForAPrimary.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 31c96803121511d9c15e80a7d8c3033f83f4bdb2 parent 6b8ba04
6 files changed, +210 −10
  • internal/equipo/chat/runner.go +3 −0
  • internal/equipo/chat/tasks.go +17 −6
  • internal/equipo/chat/transfers.go +51 −4
  • internal/equipo/mac_engineering_views_test.go +30 −0
  • internal/equipo/work_pages.go +4 −0
  • internal/thintest/wbfix2_contract_test.go +105 −0
diff --git a/internal/equipo/chat/runner.go b/internal/equipo/chat/runner.go
index 1283d373..6590c776 100644
--- a/internal/equipo/chat/runner.go
+++ b/internal/equipo/chat/runner.go
@@ -99,6 +99,9 @@ func (s *Store) Connect(ctx context.Context, p RunnerPrincipal, process string,
if err := t.reconcileOutbound(p.Person, p.RunnerID); err != nil {
return err
}
+ if err := t.reconcileUnpublished(p.Person, p.RunnerID); 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/tasks.go b/internal/equipo/chat/tasks.go
index 56905c1e..d4e14b48 100644
--- a/internal/equipo/chat/tasks.go
+++ b/internal/equipo/chat/tasks.go
@@ -1801,7 +1801,7 @@ func (t *txn) stepReport(l Lease, v *wire.StepReport) error {
return nil
}
if role != wire.RolePrimary && s.Specialist == wire.SpecialistEngineering && s.CandidateSHA != "" && v.Phase == phaseCommitted {
- if err := t.candidateCommitted(k, s); err != nil {
+ if err := t.candidateCommitted(k, &s); err != nil {
return err
}
}
@@ -2213,17 +2213,28 @@ func (t *txn) retryStepOf(k *taskRecord, person, step string, coordinator bool)
// A worker's commit the primary could not push: publish the
// recorded commit again, without running Engineering.
var tr transferRecord
- if err := t.db.Where("step_id = ? AND attempt = ? AND direction = ?", st.ID, st.Attempt, wire.TransferOutbound).Order("created_at DESC").First(&tr).Error; err != nil {
- return 0, ErrNotRetryable
+ err := t.db.Where("step_id = ? AND attempt = ? AND direction = ?", st.ID, st.Attempt, wire.TransferOutbound).Order("created_at DESC").First(&tr).Error
+ if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
+ return 0, err
}
+ pruned := err != nil
st.Reason = ""
primary, err := t.s.authority.PrimaryRunner(t.db, st.Person)
if err != nil {
return 0, err
}
- if primary != "" && tr.ToRunner != primary {
- // The commit went to a primary that has been replaced: the
- // worker sends it again, to this one, which then publishes it.
+ if pruned && primary == "" {
+ // Its transfer is gone and no primary is there: it waits for
+ // one, which asks the worker again (reconcileUnpublished).
+ now := t.now()
+ st.Publication, st.Placement, st.WaitingSince = "", wire.PlacementWaiting, &now
+ if err := t.saveStep(&st, k); err != nil {
+ return 0, err
+ }
+ } else if pruned || primary != "" && tr.ToRunner != primary {
+ // Its transfer was pruned (the worker keeps its candidate
+ // under KeepUntil), or went to a primary since replaced: the
+ // worker sends it again, to this one, which publishes it.
st.Publication = ""
if err := t.saveStep(&st, k); err != nil {
return 0, err
diff --git a/internal/equipo/chat/transfers.go b/internal/equipo/chat/transfers.go
index f6f1b078..56db5065 100644
--- a/internal/equipo/chat/transfers.go
+++ b/internal/equipo/chat/transfers.go
@@ -429,20 +429,67 @@ func (t *txn) authorizePublication(l Lease, stepID string, attempt int64, public
// candidateCommitted opens the outbound transfer for a worker's commit
// when the instance accepts its committed report, for exactly that
-// candidate.
-func (t *txn) candidateCommitted(k taskRecord, s stepRecord) error {
+// candidate. With no primary to publish to, the step waits for one
+// (waiting_runner, the step's line says so) and reconcileUnpublished
+// asks for it when a primary connects.
+func (t *txn) candidateCommitted(k taskRecord, s *stepRecord) error {
primary, err := t.s.authority.PrimaryRunner(t.db, s.Person)
- if err != nil || primary == "" || primary == s.RunnerID {
+ if err != nil || primary == s.RunnerID {
return err
}
+ if primary == "" {
+ now := t.now()
+ s.Placement, s.WaitingSince = wire.PlacementWaiting, &now
+ return nil
+ }
var open int64
if err := t.db.Model(&transferRecord{}).Where("step_id = ? AND attempt = ? AND direction = ?", s.ID, s.Attempt, wire.TransferOutbound).Count(&open).Error; err != nil || open > 0 {
return err
}
- _, err = t.openTransfer(k, s, s.RunnerID, primary, wire.TransferHeader{Direction: wire.TransferOutbound, BaseSHA: s.BaseSHA, CandidateSHA: s.CandidateSHA})
+ _, err = t.openTransfer(k, *s, s.RunnerID, primary, wire.TransferHeader{Direction: wire.TransferOutbound, BaseSHA: s.BaseSHA, CandidateSHA: s.CandidateSHA})
return err
}
+// reconcileUnpublished runs when the person's primary connects: every
+// worker Engineering commit with no publication and no live outbound
+// transfer (it came while there was no primary, or its transfer was
+// pruned) is asked for again, from the step's durable record, for this
+// primary.
+func (t *txn) reconcileUnpublished(person, primary string) 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 specialist = ? AND on_worker = ? AND state IN ? AND coalesce(candidate_sha, '') <> '' AND pushed = ? AND coalesce(publication, '') = '' AND withdrawn = ? AND retired = ?", person, wire.SpecialistEngineering, true, []string{wire.StepRunning, wire.StepDone}, false, false, false).Find(&rows).Error; err != nil {
+ return err
+ }
+ for i := range rows {
+ s := rows[i]
+ var live int64
+ if err := t.db.Model(&transferRecord{}).Where("step_id = ? AND attempt = ? AND direction = ? AND state <> ?", s.ID, s.Attempt, wire.TransferOutbound, transferClosed).Count(&live).Error; err != nil || live > 0 {
+ if err != nil {
+ return err
+ }
+ continue
+ }
+ k, err := t.loadTask(s.TaskID)
+ if err != nil {
+ return err
+ }
+ if k.MoveProject != "" || k.CancelRequested || wire.FinalStage(k.Stage) || s.RunnerID == primary {
+ continue
+ }
+ s.Placement, s.WaitingSince = "", nil
+ if err := t.saveStep(&s, &k); err != nil {
+ return err
+ }
+ if _, err := t.openTransfer(k, s, s.RunnerID, primary, wire.TransferHeader{Direction: wire.TransferOutbound, BaseSHA: s.BaseSHA, CandidateSHA: s.CandidateSHA}); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
// redirectOutbound supersedes tr, an outbound transfer to a primary
// since replaced: it is closed, and the worker is asked for its retained
// candidate again, to primary (it answers a second send_pack from its
diff --git a/internal/equipo/mac_engineering_views_test.go b/internal/equipo/mac_engineering_views_test.go
index 17e9bc1f..8a82b740 100644
--- a/internal/equipo/mac_engineering_views_test.go
+++ b/internal/equipo/mac_engineering_views_test.go
@@ -230,3 +230,33 @@ func TestMachinesTaintWithHold(t *testing.T) {
t.Fatal("held alone")
}
}
+
+// Whole-branch fix 2, #3: a Mac's commit made while no primary is there
+// to publish it says it is waiting for one.
+func TestWorkerCommitWaitsForAPrimary(t *testing.T) {
+ task := wire.Task{ID: "t1", Stage: wire.StageInProgress, Steps: []wire.Step{{ID: "s1", Specialist: wire.SpecialistEngineering, State: wire.StepDone, Attempt: 1, MadeOn: "Tito Studio", XcodeAccess: wire.XcodeAccessSandboxed, Placement: wire.PlacementWaiting, CandidateSHA: strings.Repeat("c", 40)}}}
+ v := taskLineOf(task, "a@example.com")
+ if want := copyText("work.step.waiting_publish"); want == "" || !strings.Contains(v.Steps[0].Line, want) {
+ t.Fatalf("line %q", v.Steps[0].Line)
+ }
+ task.Steps[0].Placement = ""
+ if v := taskLineOf(task, "a@example.com"); strings.Contains(v.Steps[0].Line, copyText("work.step.waiting_publish")) {
+ t.Fatalf("line %q", v.Steps[0].Line)
+ }
+}
+
+// Whole-branch fix 2, #5: a Mac not ready because P2 is not proven
+// says that its changes cannot be scanned until it is.
+func TestMachinesSayP2Decides(t *testing.T) {
+ for _, st := range []wire.EngineeringStatus{{State: wire.EngineeringProbesFailed, Reason: "P2"}, {State: wire.EngineeringProbesPending, Reason: "F4 P2"}} {
+ hello := wire.Hello{Platform: &wire.Platform{OS: "darwin", Arch: "arm64"}, Capabilities: &wire.Capabilities{Tags: []string{wire.TagMacOS}, Engineering: &st}}
+ r := reportOf(hello)
+ if want := copyText("runner.engineering.needs_p2"); want == "" || !strings.Contains(r.Engineering, want) || !strings.HasPrefix(r.Engineering, copyText("runner.engineering."+st.State)) {
+ t.Fatalf("%+v shown as %q", st, r.Engineering)
+ }
+ }
+ hello := wire.Hello{Platform: &wire.Platform{OS: "darwin", Arch: "arm64"}, Capabilities: &wire.Capabilities{Tags: []string{wire.TagMacOS}, Engineering: &wire.EngineeringStatus{State: wire.EngineeringProbesFailed, Reason: "F2"}}}
+ if r := reportOf(hello); strings.Contains(r.Engineering, "P2") || r.Engineering != copyText("runner.engineering.probes_failed") {
+ t.Fatalf("F2 shown as %q", r.Engineering)
+ }
+}
diff --git a/internal/equipo/work_pages.go b/internal/equipo/work_pages.go
index 722b435d..044f46e7 100644
--- a/internal/equipo/work_pages.go
+++ b/internal/equipo/work_pages.go
@@ -176,6 +176,10 @@ func taskLineOf(t wire.Task, viewer string) taskLine {
if s.XcodeAccess != wire.XcodeAccessSandboxed {
l.Result = copyText("work.step.no_xcode")
}
+ if s.Specialist == wire.SpecialistEngineering && s.Placement == wire.PlacementWaiting && s.CandidateSHA != "" {
+ // Its commit waits for a primary to publish it.
+ l.Line += " · " + copyText("work.step.waiting_publish")
+ }
}
if s.Specialist == wire.SpecialistEngineering && s.State == wire.StepQueued && s.Placement == wire.PlacementBase && s.RunnerLabel != "" {
// The main runner resolves the Mac's starting point first.
diff --git a/internal/thintest/wbfix2_contract_test.go b/internal/thintest/wbfix2_contract_test.go
new file mode 100644
index 00000000..ba144c19
--- /dev/null
+++ b/internal/thintest/wbfix2_contract_test.go
@@ -0,0 +1,105 @@
+package thintest
+
+import (
+ "testing"
+ "time"
+
+ "amadan.net/oficina/equipo/internal/wire"
+)
+
+// Whole-branch fix 2, #3: a worker's commit that arrives while there is
+// no primary waits, visibly (waiting_runner), for one; when a primary
+// connects it is asked for, from the step's durable record, and
+// published.
+func TestContractCommitBetweenPrimaries(t *testing.T) {
+ r := newMacRig(t)
+ w := r.newMacEngineer("Mac A", wire.EngineeringReady)
+ base, cand := sha(1), sha(0xc3)
+ task, d := r.macDispatch(w, "iOS between primaries", base)
+ if !w.lease(d.StepID, d.Attempt) {
+ t.Fatal("start refused")
+ }
+ r.remove(r.primary.fr.Welcome.RunnerID, false)
+ w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, CandidateSHA: cand, Phase: wire.OutcomeCommitted, OutcomePhase: wire.OutcomeCommitted, Findings: []wire.Finding{}})
+ w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeCommitted))
+ w.none(wire.FrameSendPack, 50*time.Millisecond)
+ if s := r.engineeringStep(task); s.State != wire.StepDone || s.Pushed || s.Placement != wire.PlacementWaiting {
+ t.Fatalf("with no primary %+v", s)
+ }
+ token := r.inst.EnrolRunner(t).Token
+ h := primaryHello(0)
+ h.Protocol, h.Label = wire.ProtocolMacEngineering, "docs-t2"
+ h.Catalog[0].NeedsMacOS = true
+ np, err := DialRunnerHello(r.ctx, r.inst.URL, token, h)
+ if err != nil || np.Welcome.Role != wire.RolePrimary {
+ t.Fatalf("replacement %+v %v", np, err)
+ }
+ t.Cleanup(np.Close)
+ b := &peer{t: t, r: r.toolRig, fr: np}
+ _, body := w.expect(wire.FrameSendPack)
+ tr := body.(*wire.SendPackCommand).Transfer
+ if tr.Direction != wire.TransferOutbound || tr.CandidateSHA != cand || tr.BaseSHA != base || tr.Attempt != d.Attempt {
+ t.Fatalf("outbound %+v", tr)
+ }
+ if to, _ := r.transferTo(tr.ID); to != np.Welcome.RunnerID {
+ t.Fatalf("the transfer goes to %s", to)
+ }
+ w.send(wire.FrameBlob, chunk(tr, []byte("PACK commit")))
+ b.expect(wire.FrameBlob)
+ _, body = b.expect(wire.FramePublish)
+ pub := body.(*wire.PublishCommand)
+ if !b.leasePublication(d.StepID, d.Attempt, pub.Publication) {
+ t.Fatal("B's publication refused")
+ }
+ b.send(wire.FramePublished, publishedWith(pub, base, swipeDiff))
+ if s := r.engineeringStep(task); !s.Pushed || s.Publication != wire.PublicationPublished || s.Placement != "" {
+ t.Fatalf("after B published %+v", s)
+ }
+}
+
+// Whole-branch fix 2, #4: a failed publication can be retried after the
+// daily sweep has pruned its transfer: Retry asks the worker again, from
+// the step's record, and the primary publishes.
+func TestContractPublicationRetryAfterPrune(t *testing.T) {
+ r := newMacRig(t)
+ w := r.newMacEngineer("Mac A", wire.EngineeringReady)
+ base, cand := sha(1), sha(0xc4)
+ task, d := r.macDispatch(w, "iOS retry after prune", base)
+ pub := r.committedAs(w, d, cand, wire.OutcomeCommitted, true)
+ if !r.primary.leasePublication(d.StepID, d.Attempt, pub.Publication) {
+ t.Fatal("publication refused")
+ }
+ r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: pub.Publication, TaskID: task, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: cand, Reason: "push_failed"})
+ if s := r.engineeringStep(task); s.Publication != wire.PublicationFailed {
+ t.Fatalf("push failed %+v", s)
+ }
+ // The next day's sweep.
+ chat := r.inst.Store.Chat()
+ chat.SetClock(func() time.Time { return time.Now().UTC().Add(25 * time.Hour) })
+ if err := chat.Sweep(r.ctx); err != nil {
+ t.Fatal(err)
+ }
+ chat.SetClock(func() time.Time { return time.Now().UTC() })
+ var n int
+ if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_transfers WHERE step_id = ?", d.StepID).Scan(&n); err != nil || n != 0 {
+ t.Fatalf("%d transfers left %v", n, err)
+ }
+ if _, err := r.client.RetryTask(r.ctx, task); err != nil {
+ t.Fatalf("Retry after the prune: %v", err)
+ }
+ _, body := w.expect(wire.FrameSendPack)
+ tr := body.(*wire.SendPackCommand).Transfer
+ if tr.Direction != wire.TransferOutbound || tr.CandidateSHA != cand || tr.BaseSHA != base || tr.Attempt != d.Attempt {
+ t.Fatalf("outbound %+v", tr)
+ }
+ w.send(wire.FrameBlob, chunk(tr, []byte("PACK commit")))
+ r.fromPrimary(wire.FrameBlob)
+ again := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand)
+ if again.Publication == pub.Publication || again.Transfer != tr.ID {
+ t.Fatalf("republish %+v", again)
+ }
+ r.primary.send(wire.FramePublished, publishedWith(again, base, swipeDiff))
+ if s := r.engineeringStep(task); !s.Pushed || s.Publication != wire.PublicationPublished {
+ t.Fatalf("after Retry %+v", s)
+ }
+}