fix: return final runner refusals with durable reason codes
7 files changed,
+135
−6
internal/equipo/copy.go+13 −0internal/equipo/final_runner_errors_test.go+36 −0internal/equipo/runner_handlers.go+14 −2internal/runner/final_http_errors_test.go+33 −0internal/runner/http.go+7 −4internal/runner/retry.go+26 −0internal/runner/supervisor.go+6 −0
diff --git a/internal/equipo/copy.go b/internal/equipo/copy.go| index be5fe7e6..f9c17214 100644 |
| --- a/internal/equipo/copy.go |
| +++ b/internal/equipo/copy.go |
| @@ -246,6 +246,19 @@ var runnerDraftCopy = map[string]string{ |
| "task.group.queued": "Queued", |
| // COPY REVIEW REQUIRED: task.group.finished |
| "task.group.finished": "Finished this week", |
| + // COPY REVIEW REQUIRED: runner.budget |
| + "runner.budget": "The task has reached its budget.", |
| + // COPY REVIEW REQUIRED: runner.quota |
| + "runner.quota": "The task has reached its attempt limit.", |
| + // COPY REVIEW REQUIRED: runner.evidence |
| + "runner.evidence": "The evidence does not meet the task requirements.", |
| + // COPY REVIEW REQUIRED: runner.containment |
| + "runner.containment": "Waiting for the previous attempt to stop.", |
| + // COPY REVIEW REQUIRED: runner.replay |
| + "runner.replay": "This action cannot be repeated safely.", |
| + // COPY REVIEW REQUIRED: runner.uncertain |
| + "runner.uncertain": "The action outcome is unknown.", |
| + |
| // COPY REVIEW REQUIRED: task.wait.family |
| "task.wait.family": "Waiting for a different review family.", |
| // COPY REVIEW REQUIRED: task.wait.setup |
diff --git a/internal/equipo/final_runner_errors_test.go b/internal/equipo/final_runner_errors_test.go| new file mode 100644 |
| index 00000000..81c914a2 |
| --- /dev/null |
| +++ b/internal/equipo/final_runner_errors_test.go |
| @@ -0,0 +1,36 @@ |
| +package equipo |
| + |
| +import ( |
| + "amadan.net/oficina/equipo/internal/equipo/team" |
| + "amadan.net/oficina/equipo/internal/runnerproto" |
| + "encoding/json" |
| + "net/http/httptest" |
| + "testing" |
| +) |
| + |
| +func TestRunnerBusinessRefusals(t *testing.T) { |
| + for _, tc := range []struct { |
| + err error |
| + status int |
| + key string |
| + }{ |
| + {team.ErrBudget, 409, "runner.budget"}, {team.ErrQuota, 409, "runner.quota"}, |
| + {team.ErrEvidence, 422, "runner.evidence"}, {team.ErrState, 409, "form.conflict"}, |
| + {team.ErrRevision, 409, "form.conflict"}, {team.ErrConflict, 409, "form.conflict"}, |
| + {team.ErrContainment, 409, "runner.containment"}, {team.ErrReplayUnsafe, 409, "runner.replay"}, |
| + {team.ErrUncertain, 409, "runner.uncertain"}, {team.ErrInvalid, 422, "form.invalid"}, |
| + {ErrUnavailable, 503, "service.unavailable"}, |
| + } { |
| + t.Run(tc.key+tc.err.Error(), func(t *testing.T) { |
| + w := httptest.NewRecorder() |
| + runnerFailure(w, tc.err) |
| + var body runnerproto.ErrorResponse |
| + if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { |
| + t.Fatal(err) |
| + } |
| + if w.Code != tc.status || body.Error != tc.key { |
| + t.Fatalf("refusal = %d %s; want %d %s", w.Code, body.Error, tc.status, tc.key) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/equipo/runner_handlers.go b/internal/equipo/runner_handlers.go| index f1d22a58..3dcca8df 100644 |
| --- a/internal/equipo/runner_handlers.go |
| +++ b/internal/equipo/runner_handlers.go |
| @@ -36,11 +36,23 @@ func runnerFailure(w http.ResponseWriter, err error) { |
| w.Header().Set("Retry-After", "5") |
| case errors.Is(err, ErrEnrolmentPending): |
| status, key = 409, "runner.pending" |
| - case errors.Is(err, ErrInvalid): |
| + case errors.Is(err, ErrInvalid), errors.Is(err, team.ErrInvalid), errors.Is(err, team.ErrSize), errors.Is(err, team.ErrUnknownKind): |
| status, key = 422, "form.invalid" |
| case errors.Is(err, ErrForbidden), errors.Is(err, ErrNotFound), errors.Is(err, team.ErrScope), errors.Is(err, team.ErrFence), errors.Is(err, team.ErrLeaseExpired), errors.Is(err, team.ErrAdmissionExpired), errors.Is(err, team.ErrGrantRevoked), errors.Is(err, team.ErrGrantStale): |
| status, key = 403, "form.forbidden" |
| - case errors.Is(err, ErrConflict): |
| + case errors.Is(err, team.ErrBudget): |
| + status, key = 409, "runner.budget" |
| + case errors.Is(err, team.ErrQuota): |
| + status, key = 409, "runner.quota" |
| + case errors.Is(err, team.ErrEvidence): |
| + status, key = 422, "runner.evidence" |
| + case errors.Is(err, team.ErrContainment): |
| + status, key = 409, "runner.containment" |
| + case errors.Is(err, team.ErrReplayUnsafe): |
| + status, key = 409, "runner.replay" |
| + case errors.Is(err, team.ErrUncertain): |
| + status, key = 409, "runner.uncertain" |
| + case errors.Is(err, ErrConflict), errors.Is(err, team.ErrConflict), errors.Is(err, team.ErrRevision), errors.Is(err, team.ErrState), errors.Is(err, team.ErrDependency), errors.Is(err, team.ErrWorkspace), errors.Is(err, team.ErrReceiptConflict): |
| status, key = 409, "form.conflict" |
| } |
| runnerJSON(w, status, runnerproto.ErrorResponse{Version: 1, Error: key}) |
diff --git a/internal/runner/final_http_errors_test.go b/internal/runner/final_http_errors_test.go| new file mode 100644 |
| index 00000000..95a9a73e |
| --- /dev/null |
| +++ b/internal/runner/final_http_errors_test.go |
| @@ -0,0 +1,33 @@ |
| +package runner |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "io" |
| + "net/http" |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestBudgetRefusalStopsRetry(t *testing.T) { |
| + calls := 0 |
| + c := &HTTPInstance{origin: "https://equipo.example", client: &http.Client{Transport: transportFunc(func(*http.Request) (*http.Response, error) { |
| + calls++ |
| + return &http.Response{StatusCode: 409, Body: io.NopCloser(strings.NewReader(`{"version":1,"error":"runner.budget"}`))}, nil |
| + })}} |
| + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| + defer cancel() |
| + err := retryAuthority(ctx, func() error { return c.post(ctx, "admit", struct{}{}, "", nil) }) |
| + var refusal *RefusalError |
| + if calls != 1 || !errors.As(err, &refusal) || refusal.Reason != "runner.budget" { |
| + t.Fatalf("budget retry lost final reason: calls=%d err=%v", calls, err) |
| + } |
| + s, r, _, _ := cleanupFixture(t) |
| + _ = s.finishAttempt(context.Background(), r, err) |
| + var states []attemptState |
| + wfMust(t, s.journal.readStates(context.Background(), "attempt", &states)) |
| + if len(states) != 1 || states[0].FailureReason != "runner.budget" { |
| + t.Fatal("attempt lost typed budget reason", states) |
| + } |
| +} |
diff --git a/internal/runner/http.go b/internal/runner/http.go| index 53658dfd..e0bb46c6 100644 |
| --- a/internal/runner/http.go |
| +++ b/internal/runner/http.go |
| @@ -49,12 +49,15 @@ func (c *HTTPInstance) post(ctx context.Context, op string, body any, token stri |
| } |
| defer response.Body.Close() |
| if response.StatusCode != http.StatusOK { |
| - if op == "claim" && (response.StatusCode == http.StatusConflict || response.StatusCode == http.StatusServiceUnavailable) { |
| - return ErrClaimRefused |
| + if response.StatusCode == 403 || response.StatusCode == 401 || response.StatusCode == 409 || response.StatusCode == 422 { |
| + var body runnerproto.ErrorResponse |
| + _ = runnerproto.DecodeLimit(io.LimitReader(response.Body, 4096), &body, 4096) |
| + return &RefusalError{Reason: refusalReason(body.Error), Status: response.StatusCode, Claim: op == "claim"} |
| } |
| - if response.StatusCode == http.StatusForbidden || response.StatusCode == http.StatusUnauthorized { |
| - return paseo.ErrIdentity |
| + if op == "claim" && response.StatusCode == http.StatusServiceUnavailable { |
| + return ErrClaimRefused |
| } |
| + |
| if response.StatusCode >= 500 || response.StatusCode == http.StatusTooManyRequests { |
| return ErrTransient |
| } |
diff --git a/internal/runner/retry.go b/internal/runner/retry.go| index 51da63a8..a5879d82 100644 |
| --- a/internal/runner/retry.go |
| +++ b/internal/runner/retry.go |
| @@ -42,3 +42,29 @@ func remoteError(err error) error { |
| } |
| return err |
| } |
| + |
| +// RefusalError is a final instance decision, safe to persist without response text. |
| +type RefusalError struct { |
| + Reason string |
| + Status int |
| + Claim bool |
| +} |
| + |
| +func (e *RefusalError) Error() string { return e.Reason } |
| +func (e *RefusalError) Unwrap() error { |
| + if e.Status == 401 || e.Status == 403 { |
| + return paseo.ErrIdentity |
| + } |
| + if e.Claim { |
| + return ErrClaimRefused |
| + } |
| + return paseo.ErrContract |
| +} |
| +func refusalReason(key string) string { |
| + switch key { |
| + case "runner.budget", "runner.quota", "runner.evidence", "runner.containment", "runner.replay", "runner.uncertain", "form.invalid", "form.conflict", "form.forbidden": |
| + return key |
| + default: |
| + return "form.invalid" |
| + } |
| +} |
diff --git a/internal/runner/supervisor.go b/internal/runner/supervisor.go| index b0bf17ec..692d00b7 100644 |
| --- a/internal/runner/supervisor.go |
| +++ b/internal/runner/supervisor.go |
| @@ -35,6 +35,7 @@ type attemptState struct { |
| Phase string |
| FailurePhase string |
| FailureDetail string |
| + FailureReason string |
| StopError string |
| SocketPath, CapabilityPath string |
| Proof *ContainmentProof |
| @@ -162,6 +163,11 @@ func (s *Supervisor) Run(ctx context.Context, claim RunClaim) error { |
| } |
| func (s *Supervisor) finishAttempt(cleanupCtx context.Context, r *attemptState, err error) error { |
| + var refusal *RefusalError |
| + if errors.As(err, &refusal) { |
| + r.FailureReason = refusal.Reason |
| + } |
| + |
| outcome := "unprocessed" |
| if err != nil { |
| outcome = "rejected" |