oficina / equipo Public

Mac slice F wb fix 1 #3: hold worker Engineering reports below 37

Whole-branch review round 1, finding 3. A worker's Engineering step
report on a connection below protocol 37 is refused whole, before
any mutation, with the new chat.ErrHeld: the stream logs it, keeps
the connection and does not ack it, and its transaction (inbox row
included) rolls back. The worker keeps it in its outbox, as task 16
has workers hold such reports below 37, and it counts once the
worker is back at 37: the outcome (step_timeout) survives.

TestContractSliceFFieldsBelow37 now asserts the worker's reports at
36 are unacked and change nothing (no candidate, no outbound
transfer, no send_pack), then replays them at 37.
Mutations: no gate; ErrHeld acked; the role check dropped (primary
held too); each fails a named test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev b761c03aa4520ae53c5f404c35f9a55230dddedc parent 71cae52
5 files changed, +117 −26
  • internal/equipo/chat/store.go +5 −0
  • internal/equipo/chat/stream.go +4 −0
  • internal/equipo/chat/tasks.go +11 −0
  • internal/thintest/mac_publication_contract_test.go +53 −26
  • internal/thintest/wbfix1_helpers_test.go +44 −0
diff --git a/internal/equipo/chat/store.go b/internal/equipo/chat/store.go
index d178c493..f1d2bdab 100644
--- a/internal/equipo/chat/store.go
+++ b/internal/equipo/chat/store.go
@@ -42,6 +42,11 @@ var (
// from a runner that does not own what it names. The frame is acked
// (it will never succeed) and logged; the connection stays.
ErrRefused = errors.New("chat: refused for this runner")
+ // ErrHeld refuses a runner frame for now, without effect and unacked:
+ // the runner keeps it in its outbox and sends it again on a later
+ // connection (a worker's Engineering report below protocol 37). The
+ // connection stays.
+ ErrHeld = errors.New("chat: held until the runner reconnects at a newer protocol")
)
// RunnerPrincipal is an authenticated runner credential.
diff --git a/internal/equipo/chat/stream.go b/internal/equipo/chat/stream.go
index 5028a90e..5d57765d 100644
--- a/internal/equipo/chat/stream.go
+++ b/internal/equipo/chat/stream.go
@@ -220,6 +220,10 @@ func (rc *runnerConn) read(ctx context.Context) websocket.StatusCode {
return websocket.StatusPolicyViolation
}
switch {
+ case errors.Is(err, ErrHeld):
+ // Not applied and not acked: the runner sends it again later.
+ rc.log("runner frame held", err)
+ continue
case errors.Is(err, ErrRefused):
rc.log("runner frame refused", err)
case errors.Is(err, ErrSuperseded):
diff --git a/internal/equipo/chat/tasks.go b/internal/equipo/chat/tasks.go
index 43e55970..56905c1e 100644
--- a/internal/equipo/chat/tasks.go
+++ b/internal/equipo/chat/tasks.go
@@ -1422,6 +1422,17 @@ func (t *txn) stepReport(l Lease, v *wire.StepReport) error {
if s.RunnerID != l.Runner.RunnerID {
return ErrRefused
}
+ if s.Specialist == wire.SpecialistEngineering && l.Protocol < wire.ProtocolMacEngineering {
+ // A worker's Engineering report below 37 would lose what only 37
+ // carries (its outcome, its kept session): the whole transition
+ // is refused before anything changes, and the report stays in
+ // the worker's outbox until it is back at 37.
+ if role, err := t.s.authority.RunnerRole(t.db, l.Runner.RunnerID); err != nil {
+ return err
+ } else if role != wire.RolePrimary {
+ return fmt.Errorf("%w: a worker's Engineering report below protocol %d", ErrHeld, wire.ProtocolMacEngineering)
+ }
+ }
advanced := false
switch {
case v.Attempt == s.Attempt:
diff --git a/internal/thintest/mac_publication_contract_test.go b/internal/thintest/mac_publication_contract_test.go
index 92416516..561a7175 100644
--- a/internal/thintest/mac_publication_contract_test.go
+++ b/internal/thintest/mac_publication_contract_test.go
@@ -332,9 +332,12 @@ func TestContractPublicationBoundToItsPrimary(t *testing.T) {
r.shown(task, base, cand, swipeDiff)
}
-// Slice F fields read from a runner count only at protocol 37: a worker
-// that reconnected below it cannot set the outcome, and a primary below
-// it cannot set change files, a diff collection or a publication's diffs.
+// Slice F fields read from a runner count only at protocol 37. A worker
+// that reconnected below it has its Engineering reports refused whole,
+// before anything changes, and unacked: it keeps them in its outbox
+// (task 16 holds them there below 37) and they count once it is back at
+// 37. A primary below 37 cannot set change files, a diff collection or a
+// publication's diffs.
func TestContractSliceFFieldsBelow37(t *testing.T) {
for _, protocol := range []int{36, wire.ProtocolMacEngineering} {
t.Run(itoa(protocol), func(t *testing.T) {
@@ -347,30 +350,54 @@ func TestContractSliceFFieldsBelow37(t *testing.T) {
}
// The worker comes back at protocol.
c := r.creds[w.fr]
- wh := macEngineerHello(c[1], wire.EngineeringReady, protocol, false)
- wh.Generation = w.fr.Welcome.Generation
- w.fr.Close()
- fr, err := DialRunnerHello(r.ctx, r.inst.URL, c[0], wh)
- if err != nil || fr.Welcome.Protocol != protocol {
- t.Fatalf("worker at %d: %+v %v", protocol, fr, err)
+ reconnect := func(protocol int) *peer {
+ wh := macEngineerHello(c[1], wire.EngineeringReady, protocol, false)
+ wh.Generation = w.fr.Welcome.Generation
+ w.fr.Close()
+ fr, err := DialRunnerHello(r.ctx, r.inst.URL, c[0], wh)
+ if err != nil || fr.Welcome.Protocol != protocol {
+ t.Fatalf("worker at %d: %+v %v", protocol, fr, err)
+ }
+ t.Cleanup(fr.Close)
+ r.creds[fr] = c
+ return &peer{t: t, r: r.toolRig, fr: fr}
}
- t.Cleanup(fr.Close)
- r.creds[fr] = c
- w = &peer{t: t, r: r.toolRig, fr: fr}
+ w = reconnect(protocol)
// Raw frames carrying the slice F keys, whatever Encode would do.
- w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, CandidateSHA: cand, Phase: wire.OutcomeCommitted, OutcomePhase: wire.OutcomeUnfinished, XcodeAccess: wire.XcodeAccessSandboxed, SessionID: "sess-1", Findings: []wire.Finding{}})
+ committed := wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, CandidateSHA: cand, Phase: wire.OutcomeCommitted, OutcomePhase: wire.OutcomeUnfinished, XcodeAccess: wire.XcodeAccessSandboxed, SessionID: "sess-1", Findings: []wire.Finding{}}
+ done := workerDone(d, cand, wire.OutcomeUnfinished)
+ if protocol < wire.ProtocolMacEngineering {
+ for i, rep := range []wire.StepReport{committed, done} {
+ if w.sendHeld("held-"+itoa(i), wire.FrameStep, rep) {
+ t.Fatalf("at %d the worker's Engineering report %d was acked", protocol, i)
+ }
+ }
+ if s := r.engineeringStep(task); s.CandidateSHA != "" || wire.StepTerminal(s.State) || s.Terminated {
+ t.Fatalf("at %d the step changed: %+v", protocol, s)
+ }
+ var n int
+ if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_transfers WHERE step_id = ? AND direction = ?", d.StepID, wire.TransferOutbound).Scan(&n); err != nil || n != 0 {
+ t.Fatalf("at %d an outbound transfer opened: %d %v", protocol, n, err)
+ }
+ w.none(wire.FrameSendPack, 50*time.Millisecond)
+ // Back at 37, its outbox sends them again.
+ w = reconnect(wire.ProtocolMacEngineering)
+ w.sendAs("held-0", wire.FrameStep, committed)
+ } else {
+ w.send(wire.FrameStep, committed)
+ }
_, body := w.expect(wire.FrameSendPack)
h := body.(*wire.SendPackCommand).Transfer
- w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeUnfinished))
+ if protocol < wire.ProtocolMacEngineering {
+ w.sendAs("held-1", wire.FrameStep, done)
+ } else {
+ w.send(wire.FrameStep, done)
+ }
w.send(wire.FrameBlob, chunk(h, []byte("PACK commit")))
r.fromPrimary(wire.FrameBlob)
pub := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand)
- want := ""
- if protocol >= wire.ProtocolMacEngineering {
- want = wire.OutcomeUnfinished
- }
- if pub.OutcomePhase != want {
- t.Fatalf("at %d the outcome is %q, want %q", protocol, pub.OutcomePhase, want)
+ if pub.OutcomePhase != wire.OutcomeUnfinished {
+ t.Fatalf("at %d the outcome is %q", protocol, pub.OutcomePhase)
}
// The primary comes back at protocol.
ph := primaryHello(r.primary.fr.Welcome.Generation)
@@ -387,17 +414,17 @@ func TestContractSliceFFieldsBelow37(t *testing.T) {
rows := r.diffRows(d.StepID)
r.primary.send(wire.FramePublished, publishedWith(pub, base, swipeDiff))
s := r.task(task).Steps[0]
- if !s.Pushed {
- t.Fatalf("at %d not published: %+v", protocol, s)
+ if !s.Pushed || s.Reason != "step_timeout" {
+ t.Fatalf("at %d not published as unfinished: %+v", protocol, s)
}
if protocol < wire.ProtocolMacEngineering {
- if rows != 0 || s.ChangeFiles != nil || s.Reason == "step_timeout" {
- t.Fatalf("at %d slice F fields counted: %d rows, files %+v, reason %q", protocol, rows, s.ChangeFiles, s.Reason)
+ if rows != 0 || s.ChangeFiles != nil {
+ t.Fatalf("at %d slice F fields counted: %d rows, files %+v", protocol, rows, s.ChangeFiles)
}
return
}
- if rows != 1 || s.Reason != "step_timeout" {
- t.Fatalf("at 37: %d rows, reason %q", rows, s.Reason)
+ if rows != 1 {
+ t.Fatalf("at 37: %d rows", rows)
}
r.shown(task, base, cand, swipeDiff)
})
diff --git a/internal/thintest/wbfix1_helpers_test.go b/internal/thintest/wbfix1_helpers_test.go
new file mode 100644
index 00000000..0165b085
--- /dev/null
+++ b/internal/thintest/wbfix1_helpers_test.go
@@ -0,0 +1,44 @@
+package thintest
+
+import "amadan.net/oficina/equipo/internal/wire"
+
+// Whole-branch fix 1 helpers.
+
+// sendHeld sends a durable frame and then a lease round trip (frames are
+// applied in order), and reports whether the frame was acked meanwhile.
+// Other frames that arrive are held as send holds them.
+func (p *peer) sendHeld(id, kind string, body any) (acked bool) {
+ p.t.Helper()
+ if err := p.fr.Send(p.r.ctx, id, kind, body); err != nil {
+ p.t.Fatal(err)
+ }
+ lease := p.id("p-lease")
+ if err := p.fr.Send(p.r.ctx, lease, wire.FrameLease, wire.LeaseCheck{Generation: p.fr.Welcome.Generation}); err != nil {
+ p.t.Fatal(err)
+ }
+ for {
+ f, body, err := p.fr.Next(p.r.ctx)
+ if err != nil {
+ p.t.Fatalf("waiting for the lease after %s: %v", id, err)
+ }
+ if a, ok := body.(*wire.Ack); ok {
+ switch a.ID {
+ case id:
+ acked = true
+ case lease:
+ return acked
+ }
+ continue
+ }
+ p.held = append(p.held, heldFrame{f, body, p.fr.Received[len(p.fr.Received)-1]})
+ }
+}
+
+// sendAs sends a durable frame under id (a replay from the runner's
+// outbox) and waits for its ack.
+func (p *peer) sendAs(id, kind string, body any) {
+ p.t.Helper()
+ if acked := p.sendHeld(id, kind, body); !acked {
+ p.t.Fatalf("%s %s not acked", kind, id)
+ }
+}