fix: release infrastructure failures and retry exact report posts
3 files changed,
+73
−4
internal/runner/final_infrastructure_test.go+55 −0internal/runner/http.go+7 −1internal/runner/supervisor.go+11 −3
diff --git a/internal/runner/final_infrastructure_test.go b/internal/runner/final_infrastructure_test.go| new file mode 100644 |
| index 00000000..e86854c6 |
| --- /dev/null |
| +++ b/internal/runner/final_infrastructure_test.go |
| @@ -0,0 +1,55 @@ |
| +package runner |
| + |
| +import ( |
| + "amadan.net/oficina/equipo/internal/equipo/team" |
| + "amadan.net/oficina/equipo/internal/runnerproto" |
| + "context" |
| + "errors" |
| + "io" |
| + "net/http" |
| + "slices" |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestInfrastructureFailureReleasesAttempt(t *testing.T) { |
| + for _, cause := range []error{context.Canceled, context.DeadlineExceeded, ErrTransient, team.ErrEvidence} { |
| + t.Run(cause.Error(), func(t *testing.T) { |
| + s, r, _, order := cleanupFixture(t) |
| + _ = s.finishAttempt(context.Background(), r, cause) |
| + if errors.Is(cause, team.ErrEvidence) { |
| + if !slices.Contains(*order, "complete") { |
| + t.Fatal("task outcome not failed", *order) |
| + } |
| + } else if slices.Contains(*order, "complete") || !slices.Contains(*order, "release") { |
| + t.Fatal("infrastructure permanently failed task", *order) |
| + } |
| + }) |
| + } |
| +} |
| +func TestReportPostsRetryExactRequest(t *testing.T) { |
| + for _, op := range []string{"evidence", "artifacts", "freeze", "complete", "questions", "prepare", "publication_ready", "publication_authorize"} { |
| + t.Run(op, func(t *testing.T) { |
| + calls := 0 |
| + var first string |
| + c := &HTTPInstance{origin: "https://equipo.example", bearer: runnerproto.RunnerCredential{Token: "test", ExpiresAt: time.Now().Add(time.Hour)}, client: &http.Client{Transport: transportFunc(func(r *http.Request) (*http.Response, error) { |
| + raw, _ := io.ReadAll(r.Body) |
| + calls++ |
| + if calls == 1 { |
| + first = string(raw) |
| + return nil, io.ErrUnexpectedEOF |
| + } |
| + if string(raw) != first { |
| + t.Error("retry changed request") |
| + } |
| + return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`{"version":1,"result":null}`))}, nil |
| + })}} |
| + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| + defer cancel() |
| + if err := c.Call(ctx, op, runnerproto.RunnerRequest{Version: 1, RequestID: "exact"}, nil); err != nil || calls != 2 { |
| + t.Fatal("post was not retried", calls, err) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/runner/http.go b/internal/runner/http.go| index e0bb46c6..6d4b5afd 100644 |
| --- a/internal/runner/http.go |
| +++ b/internal/runner/http.go |
| @@ -95,7 +95,13 @@ func (c *HTTPInstance) Call(ctx context.Context, op string, q runnerproto.Runner |
| if err != nil { |
| return err |
| } |
| - return c.post(ctx, op, q, token, out) |
| + call := func() error { return c.post(ctx, op, q, token, out) } |
| + switch op { |
| + case "evidence", "artifacts", "freeze", "complete", "questions", "prepare", "publication_ready", "publication_authorize", "receipts", "lost", "release", "stop", "contained", "events", "question_resolved", "conclude": |
| + return retryAuthority(ctx, call) |
| + default: |
| + return call() |
| + } |
| } |
| func (c *HTTPInstance) Poll(ctx context.Context) ([]runnerproto.Assignment, error) { |
| token, err := c.token("", false) |
diff --git a/internal/runner/supervisor.go b/internal/runner/supervisor.go| index b0e0917f..37beba69 100644 |
| --- a/internal/runner/supervisor.go |
| +++ b/internal/runner/supervisor.go |
| @@ -177,9 +177,17 @@ func (s *Supervisor) finishAttempt(cleanupCtx context.Context, r *attemptState, |
| } |
| bookkeepingErr := s.journal.finishTools(cleanupCtx, r.Claim, outcome) |
| if err != nil { |
| - q := r.request("failed") |
| - q.Result = &team.AttemptResult{Outcome: team.OutcomeFailure} |
| - err = errors.Join(err, s.api.Call(cleanupCtx, "complete", q, nil)) |
| + var refusal *RefusalError |
| + taskFailure := errors.Is(err, team.ErrEvidence) || (errors.As(err, &refusal) && refusal.Reason == "runner.evidence") |
| + if taskFailure { |
| + q := r.request("failed") |
| + q.Result = &team.AttemptResult{Outcome: team.OutcomeFailure} |
| + err = errors.Join(err, s.api.Call(cleanupCtx, "complete", q, nil)) |
| + } else { |
| + q := r.request("release") |
| + q.Reason = team.WaitRunnerUnavailable |
| + err = errors.Join(err, s.api.Call(cleanupCtx, "release", q, nil)) |
| + } |
| } |
| _, cleanupErr := s.cleanup(cleanupCtx, r) |
| if err == nil && cleanupErr == nil && r.Claim.Assignment.StepKey == "publish" { |