oficina / equipo Public

Mac slice F task 16 fix 3: a cancel before its dispatch is classified

Final review (1 Important): below 37 a Checks cancellation that came
before its dispatch was held for good. The cancel's record named no
specialist and the dispatch left it so, so its report could never be
classified, and a protocol 35 instance never learned the step stopped.

The dispatch now gives such a record its classification, keeping the
cancel flag and the row's state, and the link offers the outbox again
from the start on the same connection once a dispatch comes after a
held report. Engineering's reports stay held for 37.

TestWorkerCancelBeforeDispatchBelow37 (a worker at 35, cancels before a
Checks and an Engineering dispatch, through to delivery); C1 to C3
caught.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 4854d8feb63f7f2dc365506edbc44d6558ca78eb parent 492edc2
4 files changed, +131 −1
  • .rastrillo/budgets.txt +1 −1
  • internal/runner/thin/link.go +24 −0
  • internal/runner/thin/step.go +6 −0
  • internal/runner/thin/worker_rollback_test.go +100 −0

CI cancelled — superseded by a newer push — run details

diff --git a/.rastrillo/budgets.txt b/.rastrillo/budgets.txt
index 247ca84f..cb5939ba 100644
--- a/.rastrillo/budgets.txt
+++ b/.rastrillo/budgets.txt
@@ -38,7 +38,7 @@ time internal/runner/provider 15s Plan 3 CI repair measured 10.346s for the full
perf GET /releases/* skip Verified runner release file downloads, not browser GET screens.
perf GET /install.sh skip Mac runners: the one-line bootstrap a terminal pipes to sh, not a browser screen.
time internal/runner/containment 15s Plan 3 loaded CI measured 10.817s;
-size internal/runner/thin 34465 31428 Protocols 31 to 35, merge credential, publication guards, kept work, scans, Mac pin, paths, bases, endpoint, Mac turns, Mac credentials, worker conns, Engineering, asks, F probes, D1, the fake Mac worker that makes changes, reboot acknowledgements, the rollback hold of Engineering reports; 34465/31428.
+size internal/runner/thin 34495 31528 Protocols 31 to 35, merge credential, publication guards, kept work, scans, Mac pin, paths, bases, endpoint, Mac turns, Mac credentials, worker conns, Engineering, asks, F probes, D1, the fake Mac worker that makes changes, reboot acknowledgements, the rollback hold of Engineering reports, cancels classified on dispatch; 34495/31528.
time internal/thintest 240s Thin contract and page tests, the Assistant's and the models' included, share one harness. Measured 202s on a shared 16 CPU box (2026-11-13); CI runs two lanes.
time internal/runner/mac 45s Real processes killed and proven gone: groups, sessions, the runner-user sweep, turns, restarts; probes end to end. Measured 29.3s locally.
time internal/runner/thin 45s Thin v1 slice 4 review: real git repositories for the merge and squash checks, a stalled fetch, and a 16 MiB candidate streamed through the review diff and the checks pack (about 5s).
diff --git a/internal/runner/thin/link.go b/internal/runner/thin/link.go
index 5c31d25d..66ddc8bf 100644
--- a/internal/runner/thin/link.go
+++ b/internal/runner/thin/link.go
@@ -125,6 +125,10 @@ type Link struct {
// said this runner is (primary at protocol 1).
proto int
role string
+ // heldStep: a step report was held below 37 (holdBelow37) since the
+ // outbox was last offered again; rewind: offer it again from the
+ // start, on this connection (a dispatch may have classified it).
+ heldStep, rewind bool
}
// Spoken is the protocol of the current connection (0 when disconnected)
@@ -486,6 +490,9 @@ func (l *Link) send(ctx context.Context, c *websocket.Conn, f wire.Frame) error
}
if hold, err := l.State.holdBelow37(ctx, f, l.WorkerOnly || role == wire.RoleWorker, proto); hold {
// Nor of its Engineering, which only 37 places (holdBelow37).
+ l.mu.Lock()
+ l.heldStep = true
+ l.mu.Unlock()
if err != nil && l.Logger != nil {
l.Logger.Warn("a step report kept for protocol 37: it could not be classified", "frame", f.ID, "err", sanitize(err))
}
@@ -838,6 +845,15 @@ func (l *Link) read(ctx context.Context, c *websocket.Conn) (time.Duration, erro
if err = l.State.AcceptDispatch(ctx, f.ID, *v); err != nil {
return 0, err
}
+ // A report held below 37 may be classified now (a cancel that
+ // came before this dispatch): offered again.
+ l.mu.Lock()
+ held := l.heldStep
+ l.heldStep, l.rewind = false, l.rewind || held
+ l.mu.Unlock()
+ if held {
+ l.Notify()
+ }
if l.Steps != nil {
l.Steps.Wake()
}
@@ -1130,6 +1146,14 @@ func (l *Link) write(ctx context.Context, c *websocket.Conn) error {
if err := flush(); err != nil {
return err
}
+ l.mu.Lock()
+ if l.rewind {
+ // Held step reports are offered again from the start; a frame
+ // already sent and not acked may go twice, as after a
+ // reconnect.
+ sent, l.rewind = 0, false
+ }
+ l.mu.Unlock()
frames, last, err := l.State.Unacked(ctx, sent)
if err != nil {
return err
diff --git a/internal/runner/thin/step.go b/internal/runner/thin/step.go
index b702a681..aa2dd5d4 100644
--- a/internal/runner/thin/step.go
+++ b/internal/runner/thin/step.go
@@ -99,6 +99,12 @@ func (s *State) AcceptDispatch(ctx context.Context, frameID string, d wire.Dispa
if _, err = t.Exec("INSERT INTO steps(step_id, attempt, task_id, person, body, state, base_sha, created_at) VALUES(?,?,?,?,?,?,?,?) ON CONFLICT DO NOTHING", d.StepID, d.Attempt, d.TaskID, d.Person, string(raw), runRunning, d.BaseSHA, now()); err != nil {
return err
}
+ // But the cancel's own record names no specialist: it takes the
+ // dispatch's (still cancelled, in whatever state), so its report
+ // can be classified below 37 (holdBelow37).
+ if _, err = t.Exec("UPDATE steps SET body=? WHERE step_id=? AND attempt=? AND task_id=? AND cancel=1 AND coalesce(json_extract(body, '$.specialist'), '')=''", string(raw), d.StepID, d.Attempt, d.TaskID); err != nil {
+ return err
+ }
// Its scheduler facts: revision, priority, deadline, a release.
return acceptScheduled(t, d)
})
diff --git a/internal/runner/thin/worker_rollback_test.go b/internal/runner/thin/worker_rollback_test.go
index 08705f02..bc5e95f2 100644
--- a/internal/runner/thin/worker_rollback_test.go
+++ b/internal/runner/thin/worker_rollback_test.go
@@ -2,7 +2,11 @@ package thin
import (
"context"
+ "encoding/json"
+ "io"
+ "log/slog"
"testing"
+ "time"
"amadan.net/oficina/equipo/internal/wire"
)
@@ -92,3 +96,99 @@ func TestWorkerReportsBelow37(t *testing.T) {
}
func heldBelow37(hold bool, _ error) bool { return hold }
+
+// A protocol 35 worker (a rollback) told to cancel a step before the
+// step's dispatch arrives: the cancellation's record names no specialist
+// yet, so its report waits; the dispatch that follows says Checks, the
+// record takes that (still cancelled, never run), and the waiting report
+// goes on the same connection. Cancelled before an Engineering dispatch,
+// the report still waits for 37.
+func TestWorkerCancelBeforeDispatchBelow37(t *testing.T) {
+ inst := newFakeInstance(t)
+ ctx, cancel := context.WithCancel(context.Background())
+ t.Cleanup(cancel)
+ st, err := OpenState(ctx, t.TempDir())
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { st.Close() })
+ link := &Link{Logger: slog.New(slog.NewTextHandler(io.Discard, nil)), Origin: inst.url, Token: "token", State: st, Role: wire.RoleWorker, WorkerOnly: true, Hello: func() wire.Hello {
+ return wire.Hello{Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test", "claude": "2.1.312"}}
+ }}
+ ran := make(chan string, 4)
+ steps := &StepEngine{State: st, Notify: link.Notify, Exec: func(_ context.Context, r stepRow, _ func(wire.StepReport)) wire.StepReport {
+ ran <- r.StepID
+ return wire.StepReport{TaskID: r.TaskID, StepID: r.StepID, Attempt: r.Attempt, State: wire.StepDone, Termination: true, Findings: []wire.Finding{}}
+ }}
+ link.Steps = steps
+ if err := steps.Start(ctx); err != nil {
+ t.Fatal(err)
+ }
+ done := make(chan error, 1)
+ go func() { done <- link.Run(ctx) }()
+ t.Cleanup(func() { cancel(); <-done; steps.Wait() })
+ c := <-inst.conns
+ read(t, c, wire.FrameHello)
+ send := func(id, kind string, body any) {
+ t.Helper()
+ f, err := wire.NewFrame(id, kind, body)
+ if err != nil {
+ t.Fatal(err)
+ }
+ raw, _ := json.Marshal(f)
+ write(t, c, raw)
+ }
+ send("i-welcome", wire.FrameWelcome, wire.Welcome{Generation: 1, RunnerID: "RUNNER2", Protocol: wire.Protocol35, Role: wire.RoleWorker})
+ reports := make(chan wire.StepReport, 8)
+ go func() {
+ for {
+ _, raw, err := c.Read(context.Background())
+ if err != nil {
+ return
+ }
+ if f, body, err := wire.ParseFrame(raw); err == nil && f.Type == wire.FrameStep {
+ reports <- *body.(*wire.StepReport)
+ }
+ }
+ }()
+ // none: no report of step (any, for "") goes within a moment. A
+ // report already sent and not acked may go again (the fake instance
+ // never acks), as after a reconnect.
+ none := func(what, step string) {
+ t.Helper()
+ deadline := time.After(1500 * time.Millisecond)
+ for {
+ select {
+ case r := <-reports:
+ if step == "" || r.StepID == step {
+ t.Fatalf("%s: sent at 35 %+v", what, r)
+ }
+ case <-deadline:
+ return
+ }
+ }
+ }
+ dispatch := func(id, specialist string) {
+ send("i-d-"+id, wire.FrameDispatch, wire.DispatchCommand{Person: "p@example.com", TaskID: "tk" + id, StepID: "s" + id, Attempt: 1, Specialist: specialist, Repo: "amadan.net/woodstar/panes", Branch: "equipo/tk" + id + "-panes", BaseSHA: "1111111111111111111111111111111111111111", Findings: []wire.Finding{}})
+ }
+ for _, id := range []string{"c", "e"} {
+ send("i-c-"+id, wire.FrameCancelStep, wire.CancelStepCommand{Person: "p@example.com", TaskID: "tk" + id, StepID: "s" + id, Attempt: 1})
+ }
+ none("a cancellation of a step not yet dispatched", "")
+ dispatch("c", wire.SpecialistChecks)
+ select {
+ case r := <-reports:
+ if r.StepID != "sc" || r.State != wire.StepCancelled || !r.Termination {
+ t.Fatalf("the checks step's report %+v", r)
+ }
+ case <-time.After(10 * time.Second):
+ t.Fatal("the cancelled checks step's report never went at 35")
+ }
+ dispatch("e", wire.SpecialistEngineering)
+ none("the cancelled Engineering step's report", "se")
+ select {
+ case id := <-ran:
+ t.Fatalf("a cancelled step ran: %s", id)
+ default:
+ }
+}