Repository navigation
fix(spend): let settlement evidence beat a skip header; book BlockrunClient costs per call - #92
Merged
Merged
Conversation
…Client costs per call Follow-ups from the review of #89: - settlementSkipReason(): a response that names a skipped settlement but also proves it settled (successful PAYMENT-RESPONSE with a transaction or a positive charge, or an X-Payment-Receipt hash) is not skipped; the conflict goes to stderr. blockrun.ai's free-fallback receipt has an empty transaction and no amount (chat/completions/route.ts), so today's skips still book $0. - Header values false / 0 / no / none are not a skip reason. - BlockrunClient: signFrom402() returns the signed cost with the payload and recordSpending() books that, replacing the per-client pendingCostUsd that a skipped call could zero under a concurrent call. Tests: four booked shapes and one skipped shape (free-fallback as sent) across every EVM site, a receipt-wins shape on every Solana site, predicate and stderr tests, and a concurrent BlockrunClient test. 87 fail on main.
VickyXAI
force-pushed
the
fix/settlement-skip-hardening
branch
from
October 27, 2026 06:08
0270b22 to
ee03ad4
Compare
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.
Follow-ups from the review of #89 (merged).
Problem
settlementSkipReason()treated anyX-Settlement-Skipped/X-Payment-Waivedas "nothing moved", even on a response whose receipt proves a settlement (a transaction, orextra.chargedAmount> 0 on batch).getSpending()andcostUsdwould then report $0 for money that moved. The gateways never send both today, but nothing pinned that.X-Settlement-Skipped: false(or0,no,none) was read as a skip reason.BlockrunClientkept onependingCostUsdper client. With two calls in flight on one client, a skipped answer to one zeroed the other's pending cost, and that real charge was never booked.Change
src/receipt.ts: a skip reason is ignored when the response carries settlement evidence: a successfulPAYMENT-RESPONSEwith a non-emptytransactionor a positive charge, or anX-Payment-Receipthash. The conflict is logged to stderr.false/0/no/noneare never a reason. A failed receipt (success: false) is not evidence.PAYMENT-RESPONSE{ success: true, transaction: "" }with noamountand noX-Payment-Receipt(blockrunsrc/app/api/v1/chat/completions/route.ts~4742–4777). The pm empty-result and rpc unexecuted routes return before settling. So every skip the gateways send today still books $0, and that real free-fallback shape is now a test case.src/blockrun.ts:signFrom402()returns{ paymentPayload, costUsd }, and all four booking sites pass that call's own cost torecordSpending().pendingCostUsdis gone.Tests
settlement-skipped-booking.test.ts:false/0; skip + receipt with a transaction; skip + receipt withchargedAmount; skip +X-Payment-Receipt.nonefalls through toX-Payment-Waived.BlockrunClientcalls (one skipped, one booked) book exactly one quote.settlement-skipped-solana.test.ts: receipt-wins shape on all 7 Solana sites.npm test: 1065 passed, 12 skipped. Build, lint and typecheck are clean.Note: #91 also edits CHANGELOG
[Unreleased], so whichever merges second needs a one-line rebase.