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.
This commit is contained in:
2026-11-11 10:10:40 +00:00
parent 0392f57b08
commit 16643b9f1b
4 changed files with 52 additions and 4 deletions
+21 -3
View File
@@ -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
}
+22
View File
@@ -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)
}
}
+6
View File
@@ -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", "", ""
+3 -1
View File
@@ -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)