Add x402 session settlement timing configuration#126
Conversation
…eadline The chat/completions/ohttp routes use the x402 "upto" draw-down scheme, where a single signed Permit2 authorization backs many requests. The routes did not set maxTimeoutSeconds (server default 300s), while the reaper settled sessions only after 100s of idle — so a session busy for more than ~200s was settled after its authorization deadline and the on-chain settle reverted, silently dropping the tab. - Advertise an explicit 900s authorization window (max_timeout_seconds) on all three upto routes. - Enable wait_for_settlement on the facilitator client so the reaper confirms the on-chain result before marking a session settled instead of trusting the 202 enqueue acknowledgement. - Force-settle sessions 180s before their deadline (settlement_safety_margin), leaving room for on-chain confirmation. New timing knobs and the invariant (idle < safety_margin < max_timeout, safety_margin > poll_timeout) live in definitions.py so the relationship can't silently regress. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HdeaNhmieATKBRgpVtPG89
|
The This PR uses new TODO before merge: once OpenGradient/x402#1 is merged and released as a new Generated by Claude Code |
Locks in the ordering between the settlement timing knobs so a future edit to
definitions.py can't silently reintroduce the "tab settled after its Permit2
deadline" drop:
poll_timeout < safety_margin < max_timeout
idle_timeout < safety_margin
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HdeaNhmieATKBRgpVtPG89
Summary
Adds configuration constants and middleware setup for x402 "upto" session settlement timing, ensuring that accumulated payment tabs are settled safely within the signed Permit2 authorization deadline.
Key Changes
New configuration constants in
definitions.py:UPTO_SESSION_MAX_TIMEOUT_SECONDS(900s): Validity window signed into the Permit2 authorizationUPTO_SETTLEMENT_SAFETY_MARGIN_SECONDS(180s): Force-settle buffer before authorization deadlineUPTO_SESSION_IDLE_TIMEOUT_SECONDS(100s): Idle timeout for session settlementSETTLEMENT_POLL_INTERVAL_SECONDS(2.0s): Poll interval for deferred settlement statusSETTLEMENT_POLL_TIMEOUT_SECONDS(120.0s): Timeout for settlement confirmation pollingUpdated
__main__.py:HTTPFacilitatorClientSyncwithwait_for_settlement=Trueand polling parameters to handle asynchronous settlement (202 + job id responses)max_timeout_secondsto all three x402 session configurations (OPG, Completions, Chat)session_idle_timeoutvalue (100) withUPTO_SESSION_IDLE_TIMEOUT_SECONDSconstantsettlement_safety_marginparameter to x402 middleware initializationImplementation Details
The changes enforce the critical invariant documented in
definitions.py:This ensures that:
The facilitator client now polls for settlement job completion rather than assuming immediate success, providing visibility into on-chain settlement results.
https://claude.ai/code/session_01HdeaNhmieATKBRgpVtPG89