oficina / equipo Public

Mac slice F wb fix 1 #4: publish only by the transfer's own primary

Whole-branch review round 1, finding 4. publish now requires
tr.ToRunner to be the current primary. A pack that went to a primary
since replaced is redirected: its transfer is closed as superseded
(chunks voided) and the worker is asked for its retained candidate
again, to the current primary (task 12 answers a second send_pack).
When the primary connects, reconcileOutbound does the same for an
outbound transfer still open to another runner, or one that completed
with no publication because no primary was there. A transfer whose
publication was already given to the old primary is left to
reassignPublications (failed, Retry re-sends). Where the worker
cannot be asked the publication fails as push_failed, with Retry.

Regressions: TestContractPublicationPrimaryReplacedMidTransfer (open
and complete), TestContractPublicationNeedsItsTransfersPrimary.
Mutations: no guard, no reconcile, the old transfer left open,
complete transfers skipped; each fails a named test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev e8476a5579a83ed209801babd2951c0f3748a806 parent b761c03
3 files changed, +213 −0
  • internal/equipo/chat/runner.go +3 −0
  • internal/equipo/chat/transfers.go +65 −0
  • internal/thintest/wbfix1_publication_contract_test.go +145 −0
diff --git a/internal/equipo/chat/runner.go b/internal/equipo/chat/runner.go
index 214681fc..1283d373 100644
--- a/internal/equipo/chat/runner.go
+++ b/internal/equipo/chat/runner.go
@@ -96,6 +96,9 @@ func (s *Store) Connect(ctx context.Context, p RunnerPrincipal, process string,
if err := t.reassignPublications(p.Person, p.RunnerID); err != nil {
return err
}
+ if err := t.reconcileOutbound(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/transfers.go b/internal/equipo/chat/transfers.go
index d90a6f33..fd7adf7e 100644
--- a/internal/equipo/chat/transfers.go
+++ b/internal/equipo/chat/transfers.go
@@ -250,6 +250,11 @@ func (t *txn) publish(k *taskRecord, s *stepRecord, tr transferRecord) error {
if err != nil || primary == "" {
return err
}
+ if tr.ToRunner != primary {
+ // The pack went to a primary since replaced: this one has none of
+ // it, and that one publishes nothing now.
+ return t.redirectOutbound(k, s, tr, primary)
+ }
s.Publication, s.PublicationID, s.PublicationRunner = wire.PublicationPending, newID("pb_"), primary
if err := t.saveStep(s, k); err != nil {
return err
@@ -411,6 +416,66 @@ func (t *txn) candidateCommitted(k taskRecord, s stepRecord) error {
return err
}
+// 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
+// store). An attempt that no longer publishes only has it closed. When
+// the worker cannot be asked (primary is that worker), the publication
+// fails as an unpushed commit, which Retry sends again.
+func (t *txn) redirectOutbound(k *taskRecord, s *stepRecord, tr transferRecord, primary string) error {
+ if err := t.dropChunks(tr); err != nil {
+ return err
+ }
+ if err := t.db.Model(&transferRecord{}).Where("id = ?", tr.ID).Update("state", transferClosed).Error; err != nil {
+ return err
+ }
+ if k.MoveProject != "" || s.Retired || s.Withdrawn || k.CancelRequested || wire.FinalStage(k.Stage) || s.Attempt != tr.Attempt || s.CandidateSHA != tr.CandidateSHA || s.Pushed || wire.StepTerminal(s.State) && s.State != wire.StepDone {
+ return nil
+ }
+ if primary == s.RunnerID {
+ s.Publication, s.Reason = wire.PublicationFailed, "push_failed"
+ if err := t.saveStep(s, k); err != nil {
+ return err
+ }
+ return t.advance(k)
+ }
+ _, err := t.openTransfer(*k, *s, s.RunnerID, primary, wire.TransferHeader{Direction: wire.TransferOutbound, BaseSHA: tr.BaseSHA, CandidateSHA: tr.CandidateSHA})
+ return err
+}
+
+// reconcileOutbound runs when the person's primary connects: an
+// outbound transfer still open to another runner (a primary since
+// replaced), or one that completed with no publication (no primary was
+// there to publish it), is redirected to this primary.
+func (t *txn) reconcileOutbound(person, primary string) error {
+ if current, err := t.s.authority.PrimaryRunner(t.db, person); err != nil || current != primary {
+ return err
+ }
+ var trs []transferRecord
+ if err := t.db.Where("person = ? AND direction = ? AND state IN ? AND to_runner <> ?", person, wire.TransferOutbound, []string{transferOpen, transferComplete}, primary).Find(&trs).Error; err != nil {
+ return err
+ }
+ for _, tr := range trs {
+ var s stepRecord
+ if err := t.db.Where("id = ?", tr.StepID).First(&s).Error; err != nil {
+ return err
+ }
+ if tr.State == transferComplete && s.Attempt == tr.Attempt && s.Publication != "" {
+ // Its publication was given to the old primary:
+ // reassignPublications fails it, and Retry sends it again.
+ continue
+ }
+ k, err := t.loadTask(s.TaskID)
+ if err != nil {
+ return err
+ }
+ if err := t.redirectOutbound(&k, &s, tr, primary); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
// reassignPublications runs when the person's primary connects: a
// pending publication given to another runner (a primary since replaced)
// can no longer be answered by anyone, so it fails as an unpushed commit
diff --git a/internal/thintest/wbfix1_publication_contract_test.go b/internal/thintest/wbfix1_publication_contract_test.go
new file mode 100644
index 00000000..8fad4a6f
--- /dev/null
+++ b/internal/thintest/wbfix1_publication_contract_test.go
@@ -0,0 +1,145 @@
+package thintest
+
+import (
+ "crypto/sha256"
+ "encoding/base64"
+ "encoding/hex"
+ "testing"
+ "time"
+
+ "amadan.net/oficina/equipo/internal/wire"
+)
+
+// Whole-branch fix 1, #4: a worker's commit is published only by the
+// primary its pack went to. A primary replaced while the outbound
+// transfer is open (or once it completed with no primary to publish)
+// has that transfer closed as superseded, and the worker is asked for
+// its retained candidate again, to the new primary.
+
+// chunksOf is pack split into n chunks of h.
+func chunksOf(h wire.TransferHeader, pack []byte, n int) []wire.Blob {
+ sum := sha256.Sum256(pack)
+ var out []wire.Blob
+ for i := range n {
+ part := pack[i*len(pack)/n : (i+1)*len(pack)/n]
+ out = append(out, wire.Blob{Transfer: h, Index: i, Total: n, SHA256: hex.EncodeToString(sum[:]), Data: base64.StdEncoding.EncodeToString(part)})
+ }
+ return out
+}
+
+func (r *runnersRig) transferTo(id string) (to, state string) {
+ r.t.Helper()
+ if err := r.inst.DB.Writer().QueryRow("SELECT to_runner, state FROM runner_transfers WHERE id = ?", id).Scan(&to, &state); err != nil {
+ r.t.Fatal(err)
+ }
+ return to, state
+}
+
+func TestContractPublicationPrimaryReplacedMidTransfer(t *testing.T) {
+ for _, when := range []string{"open", "complete"} {
+ t.Run(when, func(t *testing.T) {
+ r := newMacRig(t)
+ w := r.newMacEngineer("Mac A", wire.EngineeringReady)
+ base, cand := sha(1), sha(0xa4)
+ task, d := r.macDispatch(w, "iOS primary replaced", base)
+ if !w.lease(d.StepID, d.Attempt) {
+ t.Fatal("start refused")
+ }
+ 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{}})
+ _, body := w.expect(wire.FrameSendPack)
+ old := body.(*wire.SendPackCommand).Transfer
+ a := r.primary.fr.Welcome.RunnerID
+ chunks := chunksOf(old, []byte("PACK commit, two chunks"), 2)
+ w.send(wire.FrameBlob, chunks[0])
+ r.remove(a, false)
+ if when == "complete" {
+ // The last chunk lands with no primary to publish it.
+ w.send(wire.FrameBlob, chunks[1])
+ }
+ 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}
+ bID := np.Welcome.RunnerID
+ // The open transfer to A is superseded; the worker is asked again,
+ // for B.
+ if to, state := r.transferTo(old.ID); to != a || state != "closed" {
+ t.Fatalf("A's transfer: to %s, %s", to, state)
+ }
+ _, body = w.expect(wire.FrameSendPack)
+ again := body.(*wire.SendPackCommand).Transfer
+ if again.ID == old.ID || again.Direction != wire.TransferOutbound || again.CandidateSHA != cand || again.BaseSHA != base || again.Attempt != d.Attempt {
+ t.Fatalf("outbound again %+v", again)
+ }
+ if to, state := r.transferTo(again.ID); to != bID || state != "open" {
+ t.Fatalf("the new transfer: to %s, %s", to, state)
+ }
+ if when == "open" {
+ // A's last chunk now has nowhere to go.
+ w.send(wire.FrameBlob, chunks[1])
+ b.none(wire.FramePublish, 50*time.Millisecond)
+ }
+ w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeCommitted))
+ w.send(wire.FrameBlob, chunk(again, []byte("PACK commit")))
+ b.expect(wire.FrameBlob)
+ _, body = b.expect(wire.FramePublish)
+ pub := body.(*wire.PublishCommand)
+ if pub.Transfer != again.ID || pub.CandidateSHA != cand {
+ t.Fatalf("B's publication %+v", pub)
+ }
+ if !b.leasePublication(d.StepID, d.Attempt, pub.Publication) {
+ t.Fatal("B's own publication refused")
+ }
+ b.send(wire.FramePublished, publishedWith(pub, base, swipeDiff))
+ if s := r.task(task).Steps[0]; !s.Pushed || s.Publication != wire.PublicationPublished {
+ t.Fatalf("after B published %+v", s)
+ }
+ })
+ }
+}
+
+// A transfer that completes while it names a primary since replaced
+// (the new one enrolled, not yet connected) is never published by the
+// new primary: the worker is asked again, for it.
+func TestContractPublicationNeedsItsTransfersPrimary(t *testing.T) {
+ r := newMacRig(t)
+ w := r.newMacEngineer("Mac A", wire.EngineeringReady)
+ base, cand := sha(1), sha(0xa5)
+ task, d := r.macDispatch(w, "iOS primary enrolled", base)
+ if !w.lease(d.StepID, d.Attempt) {
+ t.Fatal("start refused")
+ }
+ 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{}})
+ _, body := w.expect(wire.FrameSendPack)
+ old := body.(*wire.SendPackCommand).Transfer
+ a := r.primary.fr.Welcome.RunnerID
+ r.remove(a, false)
+ token := r.inst.EnrolRunner(t).Token
+ w.send(wire.FrameBlob, chunk(old, []byte("PACK commit")))
+ if n := r.mailboxHas(a, `"publication"`); n != 0 {
+ t.Fatalf("%d publications for the replaced primary", n)
+ }
+ 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 {
+ t.Fatal(err)
+ }
+ t.Cleanup(np.Close)
+ b := &peer{t: t, r: r.toolRig, fr: np}
+ b.none(wire.FramePublish, 50*time.Millisecond)
+ _, body = w.expect(wire.FrameSendPack)
+ if again := body.(*wire.SendPackCommand).Transfer; again.ID == old.ID {
+ t.Fatalf("not asked again %+v", again)
+ }
+ if s := r.task(task).Steps[0]; s.Pushed || s.Publication == wire.PublicationPending {
+ t.Fatalf("published without its pack %+v", s)
+ }
+}