Skip to content

fix(preauth): don't reuse a pre-auth that overpays this request - #514

Merged
VickyXAI merged 1 commit into
mainfrom
fix/preauth-no-oversign
Oct 25, 2026
Merged

VickyXAI merged 1 commit into
mainfrom
fix/preauth-no-oversign

Conversation

@VickyXAI

@VickyXAI VickyXAI commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

payment-preauth.ts reused a cached pre-auth whenever need <= cover. exact settles the whole signed amount, so a smaller request reusing a pre-auth from a bigger one paid the bigger price. In prod 2026-10-21..24 this showed up as 259 over-signed settlements; one $0.001 glm-5.3-flash call paid $0.009782.

The gateway now refuses an exact payment signed more than 3% above the price (BlockRunAI/blockrun#874, live as blockrun-web-01201-tn6, code PAYMENT_OVERSIGNED). This client already handles that 402 by re-fetching a fresh challenge, so the user is no longer overcharged. Each such reuse is still a wasted round trip, though.

Fix

Reuse only when need <= cover <= need × 1.03, the gateway's band.

Tests

payment-preauth.test.ts: a shrunk request skips pre-auth (clean unpaid request, then paid retry); reuse within 3% above still happens. tsc, eslint and the suite pass, except test/upstream-proxy.test.ts, which fails to load (undici bind of undefined) identically on main in this environment.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Cached pre-authorizations are now reused only when they cover the request estimate and exceed it by no more than 3%.
    • When a cached authorization does not meet this limit, the request proceeds through the unpaid-then-paid flow instead of sending the cached payment.
  • Tests
    • Added coverage for estimates below a cached authorization and for authorizations within the 3% limit.

exact settles the whole signed amount, so reusing a pre-auth bought for a
bigger request charged the user the bigger price (prod 2026-10-21..24: a
$0.001 call paid $0.0098). The gateway now refuses an exact payment signed
more than 3% above the price (blockrun#874, PAYMENT_OVERSIGNED), so reusing one
only wastes a round trip. Reuse a cached pre-auth only when it covers the
request and exceeds its estimate by at most 3%.
@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 998a4b0 and b230a7e.

Files selected for processing: 2
  • src/payment-preauth.test.ts
  • src/payment-preauth.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Walkthrough

Cached pre-authorization reuse now requires the cached amount to cover the request estimate and stay within 3% above it. Tests cover a large estimate decrease and reuse at the 3% limit.

Changes: Pre-authorization reuse

Layer / File(s) Summary
Apply and verify the reuse limit
src/payment-preauth.ts, src/payment-preauth.test.ts
The reuse check skips cached payments when the amount exceeds the request estimate by more than 3%. Tests cover skipped reuse after a large estimate decrease and reuse at the 3% limit.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

  • BlockRunAI/ClawRouter issue 511: The change prevents cached pre-authorization reuse when it can overcharge a smaller request, aligning with the issue’s focus on quote-versus-settled price accuracy.

Suggested reviewers: killerqueen-z

Merge Risk: Minimal · up to b230a

No actionable issue remains on the supplied evidence; the change appears mergeable after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(preauth): don't reuse a pre-auth that overpays this request" matches the main change. The diff limits cached pre-auth reuse to cases where the cached amount covers the estimate and exce…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix(preauth): don't reuse a pre-auth that overpays this request" matches the main change. The diff limits cached pre-auth reuse to cases where the cached amount covers the estimate and exceeds it by no more than 3%. The tests cover the shrunk-request fallback and the 3% reuse band. The title is specific and uses the conventional fix(scope) format.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@VickyXAI
VickyXAI merged commit a15ba6d into main Oct 25, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant