Skip to content

feat: wallet ask with bounded router x402 payments - #4

Merged
eminogrande merged 4 commits into
mainfrom
feat/wallet-ask-20261018
Oct 18, 2026
Merged

eminogrande merged 4 commits into
mainfrom
feat/wallet-ask-20261018

Conversation

@eminogrande

Copy link
Copy Markdown
Contributor

What changed

  • Add wallet ask with wallet_key, prompt, positive USDC max_amount, required caller-owned request_id, and optional quality: fast|smart. It POSTs {model,messages,stream:false} to the fixed router endpoint through the existing wallet pay transport and returns {ok,answer,cost,model,receipt,request_id} (src/mcp-wallet-tool.mjs:27-29,125-153,724-746).
  • Skip pay's dry-run only for ask: the router generates before returning its exact invoice, so preflight would generate a second invoice (src/mcp-wallet-tool.mjs:652-665,735-739). No new signing path or dependencies.
  • Fix the reviewer-found retry trap: over-cap means “send again with a NEW request_id and a higher max_amount” in both the returned error and tool instructions (src/mcp-wallet-tool.mjs:80, src/mcp-instructions.mjs:24). Keep the durable failed operation and immutable cap binding. This is simpler and safer than weakening general payment idempotency; only a positively unpaid cap rejection gets the new-ID advice.
  • Require the caller to know its ID before any spending, including if the response is lost (src/mcp-wallet-tool.mjs:730-733). Same-ID replay uses the durable existing journal, including across restart.
  • Allow the fixed router request 190 seconds because its invoice comes after generation. On a paid operation with a missing response body, same-ID replay retrieves the retained answer using only the original private Idempotency-Key, never another payment signature (src/agent-kernel-runtime.mjs:102,1055-1070). A recovery 402 is never paid.
  • Update README/CHANGELOG and add offline integration, mutation checks, and GitHub-hosted Node 22 CI. Production code changes remain in the existing wrapper/runtime.
sequenceDiagram
  participant Agent
  participant Wallet
  participant Router
  Agent->>Wallet: ask(prompt, max_amount, request_id)
  Wallet->>Router: POST stream:false + Idempotency-Key
  Router-->>Wallet: 402 exact invoice after generation
  alt invoice exceeds max_amount
    Wallet-->>Agent: Unpaid; NEW request_id + higher max_amount
  else within cap
    Wallet->>Router: Same POST + PAYMENT-SIGNATURE
    Router-->>Wallet: 200 answer + settlement receipt
    Wallet-->>Agent: answer, cost, model, receipt
  end
  Agent->>Wallet: Same request_id, unchanged arguments
  Wallet-->>Agent: Durable result, no second payment
Loading

Verification

  • GitHub-hosted Ubuntu / Node 22 (the service's Docker runtime), fresh checkout + npm ci --ignore-scripts --no-audit --no-fund.
  • npm run check: PASS, syntax and import closure (57 files, 55 src modules).
  • npm run check:ask: PASS three consecutive runs. Real wallet action, x402 parsing/signing and SQLite journal; explicitly fake wallet-credential adapter, RPC, Privy signer, Kernel account and router. No usable credentials or network.
  • Covered: 402 → one signed 200; exact cost/answer/receipt; no preflight; same-ID/concurrent replay and restart; changed-prompt conflict; cap refuses before signing; durable failed over-cap replay; same-ID cap change refuses; NEW-ID higher-cap retry succeeds; both qualities; lost paid response recovery without signing; recovery 402 cannot pay; malformed answer retains receipt; validation reports all missing fields.
  • npm run check:ask:regression: PASS. Reintroducing old over-cap advice, removing the cap, and removing answer recovery each fail an assertion; byte-restored candidate passes.
  • Both configured models (gemini-3.5-flash-lite, claude-sonnet-5-5) confirmed by read-only live GET https://router.paymentrequired.com/v1/models on 2026-10-18.
  • First passing cloud run (runtime/test bytes unchanged by the later README-only commit): https://github.com/nuri-com/paymentrequired-wallet/actions/runs/39132180927. Final-head PR checks run separately.

Scope / remaining proof

No real money moved, no live paid inference, no deployment, no merge. Live settlement interoperability is not claimed; it reuses the existing transport. Node 24/npm's stricter install rejected pre-existing missing esbuild optional-platform lock entries, so CI deliberately uses the service's supported Node 22 runtime, which installs cleanly.

@eminogrande
eminogrande merged commit 660650f into main Oct 18, 2026
2 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