From 4eba2334cf9b5e1bde3cea17024cbb8c2e3b6a2e Mon Sep 17 00:00:00 2001 From: 0xRaini <0xRaini@users.noreply.github.com> Date: Wed, 11 Feb 2026 10:42:41 +0800 Subject: [PATCH] fix(agents): narrow billing error 402 regex to avoid false positives on issue IDs The broad /\b402\b/ regex in ERROR_PATTERNS.billing matched any text containing '402' (e.g., 'CHE-402', '#402', 'Room 402'), causing sanitizeUserFacingText() to replace valid agent responses with a billing error message. Replace with a context-aware pattern that only matches HTTP 402 error contexts (e.g., 'status: 402', 'HTTP 402', 'error code 402'). Fixes #13822 --- ...dded-helpers.isbillingerrormessage.test.ts | 25 +++++++++++++++++++ src/agents/pi-embedded-helpers/errors.ts | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts b/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts index ed23f93d772..00c6d4910f8 100644 --- a/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts +++ b/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts @@ -27,4 +27,29 @@ describe("isBillingErrorMessage", () => { expect(isBillingErrorMessage("invalid api key")).toBe(false); expect(isBillingErrorMessage("context length exceeded")).toBe(false); }); + it("does not false-positive on issue IDs or text containing 402", () => { + const falsePositives = [ + "Fixed issue CHE-402 in the latest release", + "See ticket #402 for details", + "ISSUE-402 has been resolved", + "Room 402 is available", + "Error code 403 was returned, not 402-related", + "The building at 402 Main Street", + ]; + for (const sample of falsePositives) { + expect(isBillingErrorMessage(sample)).toBe(false); + } + }); + it("still matches real HTTP 402 billing errors", () => { + const realErrors = [ + "HTTP 402 Payment Required", + "status: 402", + "error code 402", + "http 402", + "status=402 payment required", + ]; + 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 12461074fa6..3ca9261bf49 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: [ - /\b402\b/, + /(?:status|code|http|error)\s*[:=]?\s*402\b|^\s*402\s+payment/i, "payment required", "insufficient credits", "credit balance",