#12273: fix: prevent billing error false positive on bare '402' in chat content
agents
stale
Cluster:
Error Handling Improvements
## Summary
- `ERROR_PATTERNS.billing` included `/\b402\b/` which matches any text containing the bare number '402'
- Since `sanitizeUserFacingText()` runs on all outbound assistant text, normal chat content mentioning '402' (e.g. discussing x402 protocol, HTTP status codes) triggers a spurious billing warning
- Replace `/\b402\b/` with `/\b402\s+payment/i` so the pattern only fires on actual '402 Payment Required' error responses
- The existing `"payment required"` string pattern already covers the full error message
Closes #12192
## Test plan
- [x] Existing tests for `isBillingErrorMessage` still pass (positive matches: "HTTP 402 Payment Required", "insufficient credits", etc.)
- [x] New test verifies no false positive on chat content mentioning '402'
- [x] `sanitizeUserFacingText` regression tests pass (10 tests)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR narrows the billing-error detection regex in `src/agents/pi-embedded-helpers/errors.ts` to avoid false positives when assistant output contains the bare number `402` (since `sanitizeUserFacingText()` runs over normal assistant chat content).
It updates `ERROR_PATTERNS.billing` from `/\b402\b/` to `/\b402\s+payment/i`, relying on explicit “Payment Required” wording (and the existing `"payment required"` string pattern) to identify true billing failures. A new unit test in `src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts` asserts that general chat text mentioning `402` no longer triggers billing warnings.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- Change is narrowly scoped (one regex tweak) with a matching unit test addition to prevent regression; it reduces known false positives without affecting other billing indicators (e.g., "payment required", "insufficient credits"). No behavioral changes outside billing-error classification paths were introduced.
- No files require special attention
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#19271: fix: remove false-positive billing text rewrite in sanitizeUserFaci...
by MisterGuy420 · 2026-02-17
82.5%
#15109: fix: distinguish transient API errors from billing errors
by jwchmodx · 2026-02-13
81.6%
#16962: fix: make auth error detection contextual to prevent false positives
by StressTestor · 2026-02-15
79.9%
#20262: fix: detect Venice 402 insufficient USD/Diem balance as billing fai...
by ZPTDclaw · 2026-02-18
79.8%
#13467: fix(errors): prevent billing false positive in sanitizeUserFacingText
by lailoo · 2026-02-10
78.8%
#9173: Fix: Improve error messaging for API rate limits and billing errors
by vishaltandale00 · 2026-02-04
76.7%
#16307: fix: surface billing/auth FailoverErrors as user-friendly messages
by petter-b · 2026-02-14
75.2%
#23520: fix: trigger failover on Anthropic insufficient_quota (HTTP 400) (#...
by dissaozw · 2026-02-22
74.0%
#14290: feat: x402 payment info parsing and configurable billing recovery [AI]
by stubbi · 2026-02-11
72.9%
#8661: fix: display rate limit errors correctly instead of as context over...
by dbottme · 2026-02-04
72.6%