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:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user