From d4899bee1a2132aa0871294a1550743d2646b362 Mon Sep 17 00:00:00 2001 From: 0xRaini <0xRaini@users.noreply.github.com> Date: Wed, 11 Feb 2026 20:02:34 +0800 Subject: [PATCH] address review: expand 402 regex, fix live-auth-keys, add tests - Add verb patterns (got/returned/received) to catch real billing errors - Fix same broad `402` match in live-auth-keys.ts (reviewer point #3) - Add false-positive tests for plain numeric '402' (point #1) - Add true-positive tests for 'got a 402 from the API' etc. (point #2) --- src/agents/live-auth-keys.ts | 6 +++++- .../pi-embedded-helpers.isbillingerrormessage.test.ts | 6 ++++++ src/agents/pi-embedded-helpers/errors.ts | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/agents/live-auth-keys.ts b/src/agents/live-auth-keys.ts index 8266d4a1b52..50ddd64bcf3 100644 --- a/src/agents/live-auth-keys.ts +++ b/src/agents/live-auth-keys.ts @@ -90,7 +90,11 @@ export function isAnthropicBillingError(message: string): boolean { if (lower.includes("billing") && lower.includes("disabled")) { return true; } - if (lower.includes("402")) { + if ( + /(?:status|code|http|error)\s*[:=]?\s*402\b|(?:got|returned|received|a)\s+(?:a\s+)?402\b|^\s*402\s+payment/i.test( + lower, + ) + ) { return true; } return false; diff --git a/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts b/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts index 00c6d4910f8..5529be9c4a2 100644 --- a/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts +++ b/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts @@ -35,6 +35,9 @@ describe("isBillingErrorMessage", () => { "Room 402 is available", "Error code 403 was returned, not 402-related", "The building at 402 Main Street", + "processed 402 records", + "402 items found in the database", + "port 402 is open", ]; for (const sample of falsePositives) { expect(isBillingErrorMessage(sample)).toBe(false); @@ -47,6 +50,9 @@ describe("isBillingErrorMessage", () => { "error code 402", "http 402", "status=402 payment required", + "got a 402 from the API", + "returned 402", + "received a 402 response", ]; for (const sample of realErrors) { expect(isBillingErrorMessage(sample)).toBe(true); diff --git a/src/agents/pi-embedded-helpers/errors.ts b/src/agents/pi-embedded-helpers/errors.ts index 3ca9261bf49..99d230ed307 100644 --- a/src/agents/pi-embedded-helpers/errors.ts +++ b/src/agents/pi-embedded-helpers/errors.ts @@ -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|^\s*402\s+payment/i, + /(?:status|code|http|error)\s*[:=]?\s*402\b|(?:got|returned|received|a)\s+(?:a\s+)?402\b|^\s*402\s+payment/i, "payment required", "insufficient credits", "credit balance",