oficina / equipo Public

fix: never resend a turn after an uncertain outcome

codex agent, operated by paul@keymail.dev pushed by claude agent, operated by paul@keymail.dev ddf6f09e9e7617931371b16a5ebf98acf029d625 parent af30f98
5 files changed, +62 −1
  • internal/equipo/runner_mutations.go +3 −0
  • internal/equipotest/final_lost_test.go +23 −0
  • internal/runner/final_send_test.go +19 −0
  • internal/runner/retry.go +12 −0
  • internal/runner/workflow_turn.go +5 −1
diff --git a/internal/equipo/runner_mutations.go b/internal/equipo/runner_mutations.go
index e728bc56..ce10e74a 100644
--- a/internal/equipo/runner_mutations.go
+++ b/internal/equipo/runner_mutations.go
@@ -245,6 +245,9 @@ func (e *Execution) mutateAt(ctx context.Context, tx *gorm.DB, a runnerAssignmen
if err != nil {
return nil, err
}
+ if op == "lost" && (action.State == team.ActionDispatching || action.State == team.ActionUncertain) {
+ return nil, e.Core.Lost(ctx, q.Scope, q.ActionID, "runner_unknown_outcome")
+ }
if !cleanup && op == "receipts" && e.validators.Receipt != nil {
receipt, err := e.validators.Receipt(ctx, value, q)
if err != nil {
diff --git a/internal/equipotest/final_lost_test.go b/internal/equipotest/final_lost_test.go
new file mode 100644
index 00000000..dcf6ff4d
--- /dev/null
+++ b/internal/equipotest/final_lost_test.go
@@ -0,0 +1,23 @@
+package equipotest
+
+import (
+ "amadan.net/oficina/equipo/internal/equipo/team"
+ "testing"
+)
+
+func TestRunnerLostMarksUnknownAndCharges(t *testing.T) {
+ f := runnerFixtureNew(t, true)
+ id := eventTurn(t, f)
+ q := f.request()
+ q.ActionID = id
+ q.RequestID = "lost"
+ _, err := f.x.Mutate(connectionContext, f.principal, "lost", q)
+ executionMust(t, err)
+ a, err := f.x.Core.GetAction(connectionContext, q.Scope, id)
+ executionMust(t, err)
+ rows, err := f.x.Core.Reservations(connectionContext, q.Scope)
+ executionMust(t, err)
+ if a.State != team.ActionUncertain || len(rows) != 1 || rows[0].Settled != rows[0].Reserved {
+ t.Fatal("lost action was not unknown and fully charged", a.State, rows)
+ }
+}
diff --git a/internal/runner/final_send_test.go b/internal/runner/final_send_test.go
new file mode 100644
index 00000000..5771c3b6
--- /dev/null
+++ b/internal/runner/final_send_test.go
@@ -0,0 +1,19 @@
+package runner
+
+import (
+ "amadan.net/oficina/equipo/internal/runner/paseo"
+ "context"
+ "errors"
+ "testing"
+ "time"
+)
+
+func TestUncertainSendIsNotRetried(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second)
+ defer cancel()
+ sends, lost := 0, 0
+ err := sendAdmittedTurn(ctx, func() error { sends++; return paseo.ErrUncertain }, func(context.Context) error { lost++; return nil })
+ if !errors.Is(err, paseo.ErrUncertain) || sends != 1 || lost != 1 {
+ t.Fatalf("uncertain turn retried or not recorded: sends=%d lost=%d err=%v", sends, lost, err)
+ }
+}
diff --git a/internal/runner/retry.go b/internal/runner/retry.go
index a5879d82..cd857ba3 100644
--- a/internal/runner/retry.go
+++ b/internal/runner/retry.go
@@ -68,3 +68,15 @@ func refusalReason(key string) string {
return "form.invalid"
}
}
+
+// A consumed ticket permits one send. Without proof that no frame was written,
+// even a transport failure leaves the action unknown and fully charged.
+func sendAdmittedTurn(ctx context.Context, send func() error, lost func(context.Context) error) error {
+ err := send()
+ if err == nil {
+ return nil
+ }
+ cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
+ defer cancel()
+ return errors.Join(paseo.ErrUncertain, err, lost(cleanup))
+}
diff --git a/internal/runner/workflow_turn.go b/internal/runner/workflow_turn.go
index 6b375146..87566c59 100644
--- a/internal/runner/workflow_turn.go
+++ b/internal/runner/workflow_turn.go
@@ -168,7 +168,11 @@ drained:
if err := runnerproto.ConsumeTicket(dir, q.RequestID, ticket, time.Now().UTC(), ticket.Incarnation); err != nil {
return err
}
- return retryAuthority(ctx, func() error { return remoteError(w.cfg.Client.Send(ctx, r.Agent, message, q.RequestID)) })
+ return sendAdmittedTurn(ctx, func() error { return w.cfg.Client.Send(ctx, r.Agent, message, q.RequestID) }, func(reportCtx context.Context) error {
+ lost := r.request("turn-lost-" + suffix)
+ lost.ActionID = action
+ return s.api.Call(reportCtx, "lost", lost, nil)
+ })
}
if err = dispatch(); err != nil {
return err