The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
fix(agent-payment-x402): release the session reservation when nothing was signed
Notorious D.E.Vd24c97d13c7f
diff --git a/docs/ja-JP/skills/agent-payment-x402/SKILL.md b/docs/ja-JP/skills/agent-payment-x402/SKILL.mdindex bf0e13f03..94af8757e 100644--- a/docs/ja-JP/skills/agent-payment-x402/SKILL.md+++ b/docs/ja-JP/skills/agent-payment-x402/SKILL.md@@ -174,8 +174,7 @@ client.registerPolicy((_x402Version, requirements) =>// The policy sees one challenge at a time, so it cannot enforce a session// total or ask a human anything. Keep the payment-enabled client private and-// route every paid call through this boundary.-const fetchWithPayment = wrapFetchWithPayment(fetch, client);+// route every paid call through the payOnce boundary below.// Supply this from your harness — a real prompt, never a stub that returns true.declare function confirmWithUser(prompt: string): Promise<boolean>;@@ -192,16 +191,30 @@ async function payOnce(url: string, init?: RequestInit): Promise<Response> {throw new Error("Session budget exhausted — blocked");}sessionSpent += MAX_AMOUNT;- if (!sessionApproved) {- sessionApproved = await confirmWithUser(- `Allow paid requests up to ${SESSION_CAP} atomic units this session?`,- );+ // Release the reservation only when no signed payment left this process:+ // a declined or failed prompt, a challenge the policy rejected, or a free+ // response. Once a signed request is sent it may settle, so keep it counted.+ let signedRequestSent = false;+ const trackingFetch: typeof fetch = (input, reqInit) => {+ const req = new Request(input, reqInit);+ if (req.headers.has("PAYMENT-SIGNATURE") || req.headers.has("X-PAYMENT")) {+ signedRequestSent = true;+ }+ return fetch(req);+ };+ try {if (!sessionApproved) {- sessionSpent -= MAX_AMOUNT; // nothing was signed- throw new Error("User declined — no payment attempted");+ sessionApproved = await confirmWithUser(+ `Allow paid requests up to ${SESSION_CAP} atomic units this session?`,+ );+ if (!sessionApproved) {+ throw new Error("User declined — no payment attempted");+ }}+ return await wrapFetchWithPayment(trackingFetch, client)(url, init);+ } finally {+ if (!signedRequestSent) sessionSpent -= MAX_AMOUNT;}- return fetchWithPayment(url, init);}const res = await payOnce("https://api.example.com/data", { method: "GET" });diff --git a/docs/zh-CN/skills/agent-payment-x402/SKILL.md b/docs/zh-CN/skills/agent-payment-x402/SKILL.mdindex e2095d78c..0fe5ca400 100644--- a/docs/zh-CN/skills/agent-payment-x402/SKILL.md+++ b/docs/zh-CN/skills/agent-payment-x402/SKILL.md@@ -174,8 +174,7 @@ client.registerPolicy((_x402Version, requirements) =>// The policy sees one challenge at a time, so it cannot enforce a session// total or ask a human anything. Keep the payment-enabled client private and-// route every paid call through this boundary.-const fetchWithPayment = wrapFetchWithPayment(fetch, client);+// route every paid call through the payOnce boundary below.// Supply this from your harness — a real prompt, never a stub that returns true.declare function confirmWithUser(prompt: string): Promise<boolean>;@@ -192,16 +191,30 @@ async function payOnce(url: string, init?: RequestInit): Promise<Response> {throw new Error("Session budget exhausted — blocked");}sessionSpent += MAX_AMOUNT;- if (!sessionApproved) {- sessionApproved = await confirmWithUser(- `Allow paid requests up to ${SESSION_CAP} atomic units this session?`,- );+ // Release the reservation only when no signed payment left this process:+ // a declined or failed prompt, a challenge the policy rejected, or a free+ // response. Once a signed request is sent it may settle, so keep it counted.+ let signedRequestSent = false;+ const trackingFetch: typeof fetch = (input, reqInit) => {+ const req = new Request(input, reqInit);+ if (req.headers.has("PAYMENT-SIGNATURE") || req.headers.has("X-PAYMENT")) {+ signedRequestSent = true;+ }+ return fetch(req);+ };+ try {if (!sessionApproved) {- sessionSpent -= MAX_AMOUNT; // nothing was signed- throw new Error("User declined — no payment attempted");+ sessionApproved = await confirmWithUser(+ `Allow paid requests up to ${SESSION_CAP} atomic units this session?`,+ );+ if (!sessionApproved) {+ throw new Error("User declined — no payment attempted");+ }}+ return await wrapFetchWithPayment(trackingFetch, client)(url, init);+ } finally {+ if (!signedRequestSent) sessionSpent -= MAX_AMOUNT;}- return fetchWithPayment(url, init);}const res = await payOnce("https://api.example.com/data", { method: "GET" });diff --git a/skills/agent-payment-x402/SKILL.md b/skills/agent-payment-x402/SKILL.mdindex f32efb687..636415848 100644--- a/skills/agent-payment-x402/SKILL.md+++ b/skills/agent-payment-x402/SKILL.md@@ -175,8 +175,7 @@ client.registerPolicy((_x402Version, requirements) =>// The policy sees one challenge at a time, so it cannot enforce a session// total or ask a human anything. Keep the payment-enabled client private and-// route every paid call through this boundary.-const fetchWithPayment = wrapFetchWithPayment(fetch, client);+// route every paid call through the payOnce boundary below.// Supply this from your harness — a real prompt, never a stub that returns true.declare function confirmWithUser(prompt: string): Promise<boolean>;@@ -193,16 +192,30 @@ async function payOnce(url: string, init?: RequestInit): Promise<Response> {throw new Error("Session budget exhausted — blocked");}sessionSpent += MAX_AMOUNT;- if (!sessionApproved) {- sessionApproved = await confirmWithUser(- `Allow paid requests up to ${SESSION_CAP} atomic units this session?`,- );+ // Release the reservation only when no signed payment left this process:+ // a declined or failed prompt, a challenge the policy rejected, or a free+ // response. Once a signed request is sent it may settle, so keep it counted.+ let signedRequestSent = false;+ const trackingFetch: typeof fetch = (input, reqInit) => {+ const req = new Request(input, reqInit);+ if (req.headers.has("PAYMENT-SIGNATURE") || req.headers.has("X-PAYMENT")) {+ signedRequestSent = true;+ }+ return fetch(req);+ };+ try {if (!sessionApproved) {- sessionSpent -= MAX_AMOUNT; // nothing was signed- throw new Error("User declined — no payment attempted");+ sessionApproved = await confirmWithUser(+ `Allow paid requests up to ${SESSION_CAP} atomic units this session?`,+ );+ if (!sessionApproved) {+ throw new Error("User declined — no payment attempted");+ }}+ return await wrapFetchWithPayment(trackingFetch, client)(url, init);+ } finally {+ if (!signedRequestSent) sessionSpent -= MAX_AMOUNT;}- return fetchWithPayment(url, init);}const res = await payOnce("https://api.example.com/data", { method: "GET" });