fix: keep proxy usage recording and billing consistent#345
Merged
greatest0fallt1me merged 2 commits intoMay 27, 2026
Merged
Conversation
|
@samueloyibodevv Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR fixes the
/v1/callproxy metering flow so usage recording and billing deduction stay consistent instead of diverging under partial failure. It anchors recordable proxy charging to the request ID, ensures a successful recordable call yields one matching usage event and one deduction or neither, and logs reconciliation paths explicitly when follow-up repair may be needed.Related Issue
Closes #322
Changes
⚙️ Proxy Usage/Billing Consistency
src/routes/proxyRoutes.tsrequestIdwhen the billing backend supports it.💳 Billing Surface
[MODIFY]
src/types/gateway.tsAdded requestId-anchored usage charge request/result types.
Extended the billing interface with optional
chargeUsage(...)support for consistent proxy charging.[MODIFY]
src/services/billingService.tsAdded requestId-aware
chargeUsage(...)support to the in-memory/mock billing implementation.Added deterministic failure injection helpers for integration tests.
Preserved idempotent behavior when the same request is charged more than once.
[MODIFY]
src/services/billing.tsSurfaced richer billing deduction outcome metadata, including whether reconciliation is required.
Preserved the durable usage-event anchor behavior for requestId-backed billing flows.
🔐 Auth and Header Handling
src/middleware/gatewayApiKeyAuth.tsx-api-keyextraction over bearer auth when both are present so proxy auth and header-safety scenarios behave consistently.🧪 Tests
[MODIFY]
src/__tests__/proxy.integration.test.tsAdded coverage for anchored charging failure leaving both usage and balance untouched.
Updated proxy response assertions to match the standardized error payload shape.
Kept request/response proxy safety coverage aligned with current transport behavior.
[MODIFY]
src/__tests__/usageMetering.test.tsAdded coverage for anchored charging failure yielding neither usage nor deduction.
Added coverage for the reconciliation log path when billing succeeds but the in-memory usage view write fails.
📚 Documentation
README.mdVerification Results
How to Test
Screenshots
✅ Proxy consistency tests pass
A screenshot of:
npm test -- src/__tests__/proxy.integration.test.ts src/__tests__/usageMetering.test.ts