mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 07:20:45 +00:00
fix: tighten 402 billing detection regexes (openclaw#13827) thanks @0xRaini
This commit is contained in:
35
src/agents/live-auth-keys.test.ts
Normal file
35
src/agents/live-auth-keys.test.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isAnthropicBillingError } from "./live-auth-keys.js";
|
||||
|
||||
describe("isAnthropicBillingError", () => {
|
||||
it("does not false-positive on plain 'a 402' prose", () => {
|
||||
const samples = [
|
||||
"Use a 402 stainless bolt",
|
||||
"Book a 402 room",
|
||||
"There is a 402 near me",
|
||||
"The building at 402 Main Street",
|
||||
];
|
||||
|
||||
for (const sample of samples) {
|
||||
expect(isAnthropicBillingError(sample)).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
it("matches real 402 billing payload contexts including JSON keys", () => {
|
||||
const samples = [
|
||||
"HTTP 402 Payment Required",
|
||||
"status: 402",
|
||||
"error code 402",
|
||||
'{"status":402,"type":"error"}',
|
||||
'{"code":402,"message":"payment required"}',
|
||||
'{"error":{"code":402,"message":"billing hard limit reached"}}',
|
||||
"got a 402 from the API",
|
||||
"returned 402",
|
||||
"received a 402 response",
|
||||
];
|
||||
|
||||
for (const sample of samples) {
|
||||
expect(isAnthropicBillingError(sample)).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -91,7 +91,7 @@ export function isAnthropicBillingError(message: string): boolean {
|
||||
return true;
|
||||
}
|
||||
if (
|
||||
/(?:status|code|http|error)\s*[:=]?\s*402\b|(?:got|returned|received|a)\s+(?:a\s+)?402\b|^\s*402\s+payment/i.test(
|
||||
/["']?(?:status|code)["']?\s*[:=]\s*402\b|\bhttp\s*402\b|\berror(?:\s+code)?\s*[:=]?\s*402\b|\b(?:got|returned|received)\s+(?:a\s+)?402\b|^\s*402\s+payment/i.test(
|
||||
lower,
|
||||
)
|
||||
) {
|
||||
|
||||
@@ -38,6 +38,9 @@ describe("isBillingErrorMessage", () => {
|
||||
"processed 402 records",
|
||||
"402 items found in the database",
|
||||
"port 402 is open",
|
||||
"Use a 402 stainless bolt",
|
||||
"Book a 402 room",
|
||||
"There is a 402 near me",
|
||||
];
|
||||
for (const sample of falsePositives) {
|
||||
expect(isBillingErrorMessage(sample)).toBe(false);
|
||||
@@ -53,6 +56,9 @@ describe("isBillingErrorMessage", () => {
|
||||
"got a 402 from the API",
|
||||
"returned 402",
|
||||
"received a 402 response",
|
||||
'{"status":402,"type":"error"}',
|
||||
'{"code":402,"message":"payment required"}',
|
||||
'{"error":{"code":402,"message":"billing hard limit reached"}}',
|
||||
];
|
||||
for (const sample of realErrors) {
|
||||
expect(isBillingErrorMessage(sample)).toBe(true);
|
||||
|
||||
@@ -535,7 +535,7 @@ const ERROR_PATTERNS = {
|
||||
overloaded: [/overloaded_error|"type"\s*:\s*"overloaded_error"/i, "overloaded"],
|
||||
timeout: ["timeout", "timed out", "deadline exceeded", "context deadline exceeded"],
|
||||
billing: [
|
||||
/(?:status|code|http|error)\s*[:=]?\s*402\b|(?:got|returned|received|a)\s+(?:a\s+)?402\b|^\s*402\s+payment/i,
|
||||
/["']?(?:status|code)["']?\s*[:=]\s*402\b|\bhttp\s*402\b|\berror(?:\s+code)?\s*[:=]?\s*402\b|\b(?:got|returned|received)\s+(?:a\s+)?402\b|^\s*402\s+payment/i,
|
||||
"payment required",
|
||||
"insufficient credits",
|
||||
"credit balance",
|
||||
|
||||
Reference in New Issue
Block a user