From 16643b9f1b3ae8e6e5c53416244db5817efa224e Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Wed, 11 Nov 2026 10:10:40 +0000 Subject: [PATCH] Stop a run when the model provider refuses for balance or quota Ten runs each burned five visits in seconds on "OpenCode agent reported an execution error": the provider answered HTTP 402 provider.quota and the classifier read neither field. That refusal is now its own error with fixed text, is not retried inside the stage, and ends the run instead of following the error edge. --- internal/opencode/format.go | 24 +++++++++++++++++++++--- internal/opencode/format_test.go | 22 ++++++++++++++++++++++ internal/server/runner.go | 6 ++++++ internal/stage/model_run.go | 4 +++- 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/internal/opencode/format.go b/internal/opencode/format.go index 3d36e51f..af5f4b39 100644 --- a/internal/opencode/format.go +++ b/internal/opencode/format.go @@ -3,6 +3,7 @@ package opencode import ( "context" "encoding/json" + "errors" "fmt" "strings" ) @@ -59,20 +60,37 @@ func (c *Connection) supportsFormat(ctx context.Context, format json.RawMessage) return true, nil } +// ErrProviderQuota is a provider refusal for the account's balance or quota. +// Its text is fixed: no provider message, link or credential is copied. +var ErrProviderQuota = errors.New("the model provider refused the request: the account's balance or quota is used up; top up the provider account or choose another model for the stage") + // ExecutionError exposes a safe classification, not arbitrary provider messages, // prompts or credentials. Full redacted responses belong in failure evidence. func ExecutionError(data json.RawMessage) error { var failure struct { - Name string `json:"name"` - Tag string `json:"_tag"` - Data struct { + Name string `json:"name"` + Tag string `json:"_tag"` + Type string `json:"type"` + Status int `json:"status"` + Data struct { StatusCode int `json:"statusCode"` } `json:"data"` } if json.Unmarshal(data, &failure) != nil { return fmt.Errorf("OpenCode agent reported an execution error") } + if failure.Data.StatusCode == 0 { + failure.Data.StatusCode = failure.Status + } + // The provider refused for the account, not for this request: another + // attempt cannot succeed, so the caller must not retry or loop. + if failure.Type == "provider.quota" || failure.Data.StatusCode == 402 { + return fmt.Errorf("%w (HTTP %d)", ErrProviderQuota, failure.Data.StatusCode) + } name := failure.Name + if name == "" { + name = failure.Type + } if name == "" { name = failure.Tag } diff --git a/internal/opencode/format_test.go b/internal/opencode/format_test.go index c8cdab69..a7df9597 100644 --- a/internal/opencode/format_test.go +++ b/internal/opencode/format_test.go @@ -3,6 +3,7 @@ package opencode import ( "context" "encoding/json" + "errors" "net/http" "net/http/httptest" "strings" @@ -113,3 +114,24 @@ func TestExecutionErrorReportsSanitizedNameAndStatusWithoutBody(t *testing.T) { t.Fatalf("known safe name changed: %v", err) } } + +// A provider that refuses for the account (no balance, quota used up) is its +// own error with fixed text: the provider's message and billing link are not +// copied, and callers can tell it must not be retried. +func TestExecutionErrorClassifiesProviderQuota(t *testing.T) { + for _, body := range []string{ + `{"message":"Insufficient balance. Top up at https://example.test/billing?credits=1.","status":402,"type":"provider.quota"}`, + `{"name":"APIError","data":{"statusCode":402,"message":"pay"}}`, + } { + err := ExecutionError(json.RawMessage(body)) + if !errors.Is(err, ErrProviderQuota) { + t.Fatalf("%s: err = %v", body, err) + } + if text := err.Error(); strings.Contains(text, "example.test") || strings.Contains(text, "Insufficient") || !strings.Contains(text, "HTTP 402") { + t.Fatalf("diagnostic = %q", text) + } + } + if err := ExecutionError(json.RawMessage(`{"name":"APIError","data":{"statusCode":500}}`)); errors.Is(err, ErrProviderQuota) { + t.Fatalf("a server error is not a quota refusal: %v", err) + } +} diff --git a/internal/server/runner.go b/internal/server/runner.go index 59ae74be..8a179193 100644 --- a/internal/server/runner.go +++ b/internal/server/runner.go @@ -16,6 +16,7 @@ import ( "git.a-wan.net/alan/pipelines/internal/filediff" "git.a-wan.net/alan/pipelines/internal/harness" + "git.a-wan.net/alan/pipelines/internal/opencode" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" @@ -2106,6 +2107,11 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na step.Transcript = stage.RetainSession(step.Transcript, stageSession) } step.Target = def.Edges[step.Outcome] + if errors.Is(err, opencode.ErrProviderQuota) { + // Another visit would be refused the same way: stop the run on the + // provider's refusal instead of following the error edge again. + step.Target = protocol.FailureEnd + } if errors.Is(err, stage.ErrStageCleanup) { // Retain the partial evidence without pretending that the agent stopped. step.Status, step.Outcome, step.Target = "running", "", "" diff --git a/internal/stage/model_run.go b/internal/stage/model_run.go index 8839b4c7..92ab0a07 100644 --- a/internal/stage/model_run.go +++ b/internal/stage/model_run.go @@ -5,6 +5,8 @@ import ( "errors" "fmt" "time" + + "git.a-wan.net/alan/pipelines/internal/opencode" ) func (m ModelStage) Run(ctx context.Context, def Definition, input Values) (Result, error) { @@ -99,7 +101,7 @@ func (m ModelStage) Run(ctx context.Context, def Definition, input Values) (Resu } return Result{}, ctx.Err() } - if errors.Is(err, ErrBudgetExceeded) || errors.Is(err, ErrBudgetAccounting) || errors.Is(err, ErrStageCleanup) { + if errors.Is(err, ErrBudgetExceeded) || errors.Is(err, ErrBudgetAccounting) || errors.Is(err, ErrStageCleanup) || errors.Is(err, opencode.ErrProviderQuota) { break } replayPostAdmission = safeToReplay(def) && replayablePostAdmission(err)