z-zero-mcp-server 1.6.1 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +75 -4
- package/dist/{wdk_backend.d.ts → base_backend.d.ts} +18 -1
- package/dist/{wdk_backend.js → base_backend.js} +117 -36
- package/dist/failure_taxonomy.d.ts +48 -0
- package/dist/failure_taxonomy.js +130 -0
- package/dist/index.js +323 -114
- package/dist/lib/api-base.d.ts +2 -0
- package/dist/lib/api-base.js +51 -0
- package/dist/lib/key-store.js +1 -1
- package/dist/lib/web3-detector.js +3 -3
- package/dist/types.d.ts +2 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -17,7 +17,8 @@ npx z-zero-mcp-server
|
|
|
17
17
|
- ⛽ **Gasless USDC on Base** — auto-detects crypto checkout (EIP-681) and settles as a gasless USDC transfer sponsored by Coinbase Paymaster. The agent holds only USDC — no ETH, no gas UX.
|
|
18
18
|
- 💳 **JIT single-use virtual cards** — amount-locked, 1-hour TTL, burned after a single use. Fiat fallback for the rest of the web.
|
|
19
19
|
- 🧠 **Smart Routing + checkout intelligence** — `get_merchant_hints` serves platform-specific checkout playbooks (Shopify, Etsy, WooCommerce…).
|
|
20
|
-
-
|
|
20
|
+
- ✍️ **Signed intent + signed receipt** — the card is bound to a signed statement of *what it is for*, and every confirmed purchase returns a signed receipt with a diff against that intent. The agent can **prove** a purchase instead of claiming one (`verify_receipt`, public verify page).
|
|
21
|
+
- 🔄 **Structured failure labels** — failed checkouts are labeled with a fixed 14-class `failure_class` (automatically, not only when an agent remembers to report) and stored as evidence for the merchant knowledge base. Facts are promoted into shared hints only after a later outcome or review verifies them.
|
|
21
22
|
|
|
22
23
|
---
|
|
23
24
|
|
|
@@ -146,8 +147,8 @@ Older self-hosted backends without the rotate endpoint keep working — the past
|
|
|
146
147
|
|
|
147
148
|
| Tool | Description |
|
|
148
149
|
|------|-------------|
|
|
149
|
-
| `request_payment_token` | Issue a JIT single-use virtual-card token for a specific amount (1hr TTL) |
|
|
150
|
-
| `execute_payment` | Auto-fill checkout form using a payment token via Playwright |
|
|
150
|
+
| `request_payment_token` | Issue a JIT single-use virtual-card token for a specific amount (1hr TTL). Pass `cart` + `ship_to` and the card is bound to a **signed intent** |
|
|
151
|
+
| `execute_payment` | Auto-fill checkout form using a payment token via Playwright. Returns a **signed receipt** on confirmed payments |
|
|
151
152
|
| `cancel_payment_token` | Cancel an unused token and refund to wallet |
|
|
152
153
|
| `request_human_approval` | Pause and request human confirmation before proceeding |
|
|
153
154
|
|
|
@@ -157,12 +158,82 @@ Older self-hosted backends without the rotate endpoint keep working — the past
|
|
|
157
158
|
|------|-------------|
|
|
158
159
|
| `auto_pay_checkout` | Fully autonomous checkout — auto-detects Web3 or Fiat and completes payment |
|
|
159
160
|
| `get_merchant_hints` | Fetch platform-specific checkout playbook (pre-steps + selectors) from Knowledge Base |
|
|
160
|
-
| `report_checkout_fail` |
|
|
161
|
+
| `report_checkout_fail` | Report a failed checkout with a **structured `failure_class`** (14-class enum) — feeds the self-healing loop |
|
|
162
|
+
| `verify_receipt` | Verify a signed receipt by id — prove a purchase happened instead of claiming it |
|
|
161
163
|
|
|
162
164
|
> 📖 **Note:** Version checking is handled automatically in each API call. No separate tool needed.
|
|
163
165
|
|
|
164
166
|
---
|
|
165
167
|
|
|
168
|
+
## Agent primitives (v1.7.0)
|
|
169
|
+
|
|
170
|
+
Three things an agent can do here that it cannot do with a normal virtual card.
|
|
171
|
+
|
|
172
|
+
### 1. Signed intent — the card knows what it is for
|
|
173
|
+
|
|
174
|
+
Pass the cart when you request a token:
|
|
175
|
+
|
|
176
|
+
```jsonc
|
|
177
|
+
request_payment_token({
|
|
178
|
+
card_alias: "Card_01",
|
|
179
|
+
amount: 44.00,
|
|
180
|
+
merchant: "etsy.com",
|
|
181
|
+
cart: [{ title: "Ceramic mug — matte white", qty: 2, unit_price: 18.50 }],
|
|
182
|
+
ship_to: "12 Nguyen Hue, District 1, Ho Chi Minh City, VN"
|
|
183
|
+
})
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Z-ZERO signs that statement (EIP-191) and binds it to the card. The user gets
|
|
187
|
+
cryptographic proof of **what this card was authorized to buy** — not just how
|
|
188
|
+
much it could spend. The shipping address is stored as a hash, never raw.
|
|
189
|
+
|
|
190
|
+
**Before you request a token, compare the checkout page with what the user actually
|
|
191
|
+
asked for** — same items, same quantity, same variant, same destination. A mismatch
|
|
192
|
+
you catch there costs nothing. After the token, it costs a card.
|
|
193
|
+
|
|
194
|
+
### 2. Signed receipt — prove the purchase, don't claim it
|
|
195
|
+
|
|
196
|
+
On a confirmed payment you get back:
|
|
197
|
+
|
|
198
|
+
```jsonc
|
|
199
|
+
"signed_receipt": {
|
|
200
|
+
"receipt_id": "8ea36791-…",
|
|
201
|
+
"receipt_hash": "0x…",
|
|
202
|
+
"match": { "total": "over", "domain": "ok" },
|
|
203
|
+
"diff": [{ "field": "total", "expected": 44.00, "observed": 46.75 }],
|
|
204
|
+
"verify_url": "https://z-zero.xyz/receipt/8ea36791-…"
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
`diff` is the part that matters: it is what the merchant actually did versus what
|
|
209
|
+
was authorized. Share `verify_url` with the user — the page is public and anyone
|
|
210
|
+
can check it. Verification is three checks: the signature is valid, the signer is
|
|
211
|
+
Z-ZERO, and the fields shown still hash to what was signed (so editing the record
|
|
212
|
+
afterwards is detectable, including by us).
|
|
213
|
+
|
|
214
|
+
**What a valid receipt does and does not prove.** It proves the record is signed by
|
|
215
|
+
Z-ZERO and unaltered. It does not by itself prove the merchant charged what the
|
|
216
|
+
receipt says — most fields start life as the agent's reading of a web page. Every
|
|
217
|
+
receipt therefore carries `provenance` per field: `zzero_issued` (the limit we set),
|
|
218
|
+
`issuer_captured` (confirmed by the card issuer's capture webhook — settlement
|
|
219
|
+
evidence), `agent_reported` (unverified), `human_verified`. Until the capture webhook
|
|
220
|
+
lands, this is a **signed execution receipt**, not settlement proof, and it says so.
|
|
221
|
+
|
|
222
|
+
### 3. Structured failure classes — every failure teaches the network
|
|
223
|
+
|
|
224
|
+
`report_checkout_fail` takes a fixed enum, not free text:
|
|
225
|
+
|
|
226
|
+
`card_declined_issuer` · `card_declined_bin_block` · `avs_mismatch` · `3ds_required` ·
|
|
227
|
+
`bot_detected` · `form_changed` · `price_changed` · `out_of_stock` ·
|
|
228
|
+
`shipping_unsupported` · `login_required` · `timeout` · `outcome_unconfirmed` ·
|
|
229
|
+
`intent_mismatch` · `unknown`
|
|
230
|
+
|
|
231
|
+
Failed runs are also labeled automatically from the browser outcome, so the network
|
|
232
|
+
learns even when nobody remembers to report. Card numbers are redacted at capture —
|
|
233
|
+
they never reach a log, screenshot or DOM dump.
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
166
237
|
## REST API Reference
|
|
167
238
|
|
|
168
239
|
The Z-ZERO backend is hosted at `https://z-zero.xyz`. All endpoints require a `Bearer` token using your Passport Key.
|
|
@@ -2,7 +2,24 @@ import type { CardData } from "./types.js";
|
|
|
2
2
|
export declare function listCardsRemote(): Promise<any>;
|
|
3
3
|
export declare function getBalanceRemote(cardAlias: string): Promise<any>;
|
|
4
4
|
export declare function getDepositAddressesRemote(): Promise<any>;
|
|
5
|
-
export declare function issueTokenRemote(cardAlias: string, amount: number, merchant: string
|
|
5
|
+
export declare function issueTokenRemote(cardAlias: string, amount: number, merchant: string, intent?: {
|
|
6
|
+
cart?: Array<{
|
|
7
|
+
title: string;
|
|
8
|
+
qty: number;
|
|
9
|
+
unit_price?: number;
|
|
10
|
+
url?: string;
|
|
11
|
+
}>;
|
|
12
|
+
subtotal?: number;
|
|
13
|
+
currency?: string;
|
|
14
|
+
ship_to?: string;
|
|
15
|
+
}): Promise<any | null>;
|
|
16
|
+
export declare function postReceiptRemote(payload: {
|
|
17
|
+
token: string;
|
|
18
|
+
checkout_url: string;
|
|
19
|
+
merchant_order_id?: string | null;
|
|
20
|
+
amount_captured?: number;
|
|
21
|
+
card_last4?: string;
|
|
22
|
+
}): Promise<any | null>;
|
|
6
23
|
export declare function resolveTokenRemote(token: string): Promise<CardData | null>;
|
|
7
24
|
export declare function burnTokenRemote(token: string, receipt_id?: string): Promise<boolean>;
|
|
8
25
|
export declare function cancelTokenRemote(token: string): Promise<any>;
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// Z-ZERO
|
|
3
|
-
//
|
|
4
|
-
// Exports IDENTICAL function signatures so index.ts can swap backends without any changes.
|
|
2
|
+
// Z-ZERO Base Backend (formerly wdk_backend.ts — renamed in the 05/08/2026 WDK purge;
|
|
3
|
+
// the Tether-WDK/Tron era it was named for is gone, this has been Base + USDC throughout).
|
|
5
4
|
//
|
|
6
|
-
//
|
|
7
|
-
// -
|
|
8
|
-
// -
|
|
9
|
-
// -
|
|
10
|
-
// - cancelTokenRemote() → triggers on-chain USDC refund back to user's Base wallet
|
|
5
|
+
// - getBalanceRemote() → on-chain USDC balance on Base via /api/wallet/balance
|
|
6
|
+
// - getDepositAddressesRemote() → the agent's Base smart-account address
|
|
7
|
+
// - issueTokenRemote() → sends USDC on Base first, THEN issues the JIT card
|
|
8
|
+
// - cancelTokenRemote() → on-chain USDC refund back to the user's Base wallet
|
|
11
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
10
|
exports.listCardsRemote = listCardsRemote;
|
|
13
11
|
exports.getBalanceRemote = getBalanceRemote;
|
|
14
12
|
exports.getDepositAddressesRemote = getDepositAddressesRemote;
|
|
15
13
|
exports.issueTokenRemote = issueTokenRemote;
|
|
14
|
+
exports.postReceiptRemote = postReceiptRemote;
|
|
16
15
|
exports.resolveTokenRemote = resolveTokenRemote;
|
|
17
16
|
exports.burnTokenRemote = burnTokenRemote;
|
|
18
17
|
exports.cancelTokenRemote = cancelTokenRemote;
|
|
19
18
|
exports.refundUnderspendRemote = refundUnderspendRemote;
|
|
20
19
|
const key_store_js_1 = require("./lib/key-store.js");
|
|
21
|
-
const
|
|
20
|
+
const api_base_js_1 = require("./lib/api-base.js");
|
|
21
|
+
const API_BASE_URL = (0, api_base_js_1.resolveApiBaseUrl)();
|
|
22
22
|
const INTERNAL_SECRET = process.env.Z_ZERO_INTERNAL_SECRET || "";
|
|
23
23
|
// Injected at build time — always reflects the actual running version
|
|
24
24
|
const version_js_1 = require("./version.js");
|
|
@@ -45,10 +45,17 @@ async function apiRequest(endpoint, method = 'GET', body = null) {
|
|
|
45
45
|
"X-MCP-Version": version_js_1.CURRENT_MCP_VERSION,
|
|
46
46
|
},
|
|
47
47
|
body: body ? JSON.stringify(body) : null,
|
|
48
|
+
// Never let fetch follow a redirect while carrying the Passport Key:
|
|
49
|
+
// it would either drop the header (silent 401) or hand the key to
|
|
50
|
+
// whatever host the redirect names. Surface it instead.
|
|
51
|
+
redirect: "manual",
|
|
48
52
|
});
|
|
53
|
+
if (res.status >= 300 && res.status < 400) {
|
|
54
|
+
return redirectError(url, res);
|
|
55
|
+
}
|
|
49
56
|
if (!res.ok) {
|
|
50
57
|
const err = await res.json().catch(() => ({ error: res.statusText }));
|
|
51
|
-
return
|
|
58
|
+
return httpError(res, err);
|
|
52
59
|
}
|
|
53
60
|
return await res.json();
|
|
54
61
|
}
|
|
@@ -56,6 +63,36 @@ async function apiRequest(endpoint, method = 'GET', body = null) {
|
|
|
56
63
|
return { error: "NETWORK_ERROR", message: err.message };
|
|
57
64
|
}
|
|
58
65
|
}
|
|
66
|
+
// Normalise a failed HTTP response into an error object callers can branch on.
|
|
67
|
+
// A 401 becomes AUTH_REQUIRED here — at the source — so no caller can mistake a
|
|
68
|
+
// rejected key for a missing resource. `status` rides along so callers can tell
|
|
69
|
+
// 404 (resource genuinely absent) from 500 (server broke).
|
|
70
|
+
// A redirect on an authenticated call is a configuration bug, not an auth bug.
|
|
71
|
+
// Say so, because the 401 it would otherwise become reads as "your key is dead".
|
|
72
|
+
function redirectError(url, res) {
|
|
73
|
+
const target = res.headers.get("location") || "another host";
|
|
74
|
+
return {
|
|
75
|
+
error: "BASE_URL_REDIRECT",
|
|
76
|
+
status: res.status,
|
|
77
|
+
message: `${url} redirected (${res.status}) to ${target}. The Passport Key was NOT forwarded — ` +
|
|
78
|
+
`a redirected request loses its Authorization header, which shows up as a false 401. ` +
|
|
79
|
+
`Set Z_ZERO_API_BASE_URL to https://z-zero.xyz in your MCP config.`,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function httpError(res, err) {
|
|
83
|
+
if (res.status === 401) {
|
|
84
|
+
return {
|
|
85
|
+
error: "AUTH_REQUIRED",
|
|
86
|
+
status: 401,
|
|
87
|
+
message: err?.message || err?.error || "Passport Key is missing, invalid, or revoked.",
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
error: err?.error || "API_ERROR",
|
|
92
|
+
status: res.status,
|
|
93
|
+
message: err?.message || err?.error || res.statusText,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
59
96
|
async function internalApiRequest(endpoint, method, body) {
|
|
60
97
|
// Auth is the Bearer Passport Key — the server dropped the x-internal-secret
|
|
61
98
|
// gate on /resolve and /burn (redundant to per-user token ownership, Jul 2026).
|
|
@@ -73,10 +110,17 @@ async function internalApiRequest(endpoint, method, body) {
|
|
|
73
110
|
"X-MCP-Version": version_js_1.CURRENT_MCP_VERSION,
|
|
74
111
|
},
|
|
75
112
|
body: body ? JSON.stringify(body) : null,
|
|
113
|
+
// Never let fetch follow a redirect while carrying the Passport Key:
|
|
114
|
+
// it would either drop the header (silent 401) or hand the key to
|
|
115
|
+
// whatever host the redirect names. Surface it instead.
|
|
116
|
+
redirect: "manual",
|
|
76
117
|
});
|
|
118
|
+
if (res.status >= 300 && res.status < 400) {
|
|
119
|
+
return redirectError(url, res);
|
|
120
|
+
}
|
|
77
121
|
if (!res.ok) {
|
|
78
122
|
const err = await res.json().catch(() => ({ error: res.statusText }));
|
|
79
|
-
return
|
|
123
|
+
return httpError(res, err);
|
|
80
124
|
}
|
|
81
125
|
return await res.json();
|
|
82
126
|
}
|
|
@@ -88,24 +132,50 @@ async function internalApiRequest(endpoint, method, body) {
|
|
|
88
132
|
// Core API: Same as custodial (cards, tokens still managed by Dashboard)
|
|
89
133
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
90
134
|
async function listCardsRemote() {
|
|
91
|
-
//
|
|
92
|
-
// For WDK mode, the Dashboard API must read wallet_mode and return on-chain balance.
|
|
93
|
-
// This is handled server-side in the updated /api/tokens/cards route.
|
|
135
|
+
// /api/tokens/cards returns card aliases + the on-chain Base USDC balance.
|
|
94
136
|
return await apiRequest('/api/tokens/cards', 'GET');
|
|
95
137
|
}
|
|
96
138
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
97
139
|
// Balance: On-chain USDC (stablecoin) on Base via Dashboard API
|
|
98
140
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
141
|
+
// Turn a failed /api/wallet/balance call into an honest, actionable failure.
|
|
142
|
+
// Returns null when the call succeeded.
|
|
143
|
+
//
|
|
144
|
+
// Why this exists: this used to collapse EVERY error into "Base wallet not
|
|
145
|
+
// connected", so a user whose Passport Key had been revoked was sent to the
|
|
146
|
+
// wallet page to fix a problem that was about the key. Only a genuine 404 means
|
|
147
|
+
// "no wallet" — everything else must keep its own identity.
|
|
148
|
+
function walletLookupFailure(data) {
|
|
149
|
+
if (!data?.error)
|
|
150
|
+
return null;
|
|
151
|
+
if (data.error === "AUTH_REQUIRED") {
|
|
152
|
+
return { error: "AUTH_REQUIRED", message: data.message };
|
|
153
|
+
}
|
|
154
|
+
if (data.error === "NETWORK_ERROR") {
|
|
104
155
|
return {
|
|
105
|
-
error:
|
|
106
|
-
message:
|
|
156
|
+
error: "NETWORK_ERROR",
|
|
157
|
+
message: `Could not reach ${API_BASE_URL}: ${data.message}`,
|
|
107
158
|
};
|
|
108
159
|
}
|
|
160
|
+
if (data.status === 404) {
|
|
161
|
+
return {
|
|
162
|
+
error: "NO_WALLET",
|
|
163
|
+
message: 'Base wallet not connected. Create one at https://z-zero.xyz/dashboard/agent-wallet',
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
error: data.error,
|
|
168
|
+
status: data.status,
|
|
169
|
+
message: data.message || 'Could not read your Base wallet balance.',
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
async function getBalanceRemote(cardAlias) {
|
|
173
|
+
// Call Dashboard to get the agent's Base wallet balance (Dashboard resolves user from
|
|
174
|
+
// passport key, finds the connected Base wallet, queries on-chain USDC balance)
|
|
175
|
+
const data = await apiRequest('/api/wallet/balance', 'GET');
|
|
176
|
+
const failure = walletLookupFailure(data);
|
|
177
|
+
if (failure)
|
|
178
|
+
return failure;
|
|
109
179
|
return {
|
|
110
180
|
wallet_balance: data.base_usdc_balance ?? data.balance_usdt,
|
|
111
181
|
currency: 'USDC',
|
|
@@ -119,13 +189,10 @@ async function getBalanceRemote(cardAlias) {
|
|
|
119
189
|
// Deposit Address: the agent's Base smart-account address (single chain)
|
|
120
190
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
121
191
|
async function getDepositAddressesRemote() {
|
|
122
|
-
const data = await apiRequest('/api/
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
message: 'Base wallet not connected. Create one at https://z-zero.xyz/dashboard/agent-wallet'
|
|
127
|
-
};
|
|
128
|
-
}
|
|
192
|
+
const data = await apiRequest('/api/wallet/balance', 'GET');
|
|
193
|
+
const failure = walletLookupFailure(data);
|
|
194
|
+
if (failure)
|
|
195
|
+
return failure;
|
|
129
196
|
const balance = data.base_usdc_balance ?? data.balance_usdt;
|
|
130
197
|
return {
|
|
131
198
|
cards: [{ alias: 'base-wallet', balance, currency: 'USDC' }],
|
|
@@ -133,7 +200,7 @@ async function getDepositAddressesRemote() {
|
|
|
133
200
|
base: data.address,
|
|
134
201
|
note: 'Send USDC (or any supported stablecoin) on Base to your wallet. Gasless spending via the Coinbase Paymaster.'
|
|
135
202
|
},
|
|
136
|
-
|
|
203
|
+
base_wallet: {
|
|
137
204
|
address: data.address,
|
|
138
205
|
chain: data.chain || 'base',
|
|
139
206
|
balance_usdt: balance
|
|
@@ -143,7 +210,7 @@ async function getDepositAddressesRemote() {
|
|
|
143
210
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
144
211
|
// Issue Token: On-chain USDC (Base) payment → JIT card
|
|
145
212
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
146
|
-
async function issueTokenRemote(cardAlias, amount, merchant) {
|
|
213
|
+
async function issueTokenRemote(cardAlias, amount, merchant, intent) {
|
|
147
214
|
// Base AA Flow (reversed from custodial):
|
|
148
215
|
// 1. Create Partner card first (reservation)
|
|
149
216
|
// 2. Send USDC on Base from the agent's wallet → system wallet (gasless via Coinbase Paymaster)
|
|
@@ -155,8 +222,10 @@ async function issueTokenRemote(cardAlias, amount, merchant) {
|
|
|
155
222
|
merchant,
|
|
156
223
|
device_fingerprint: `mcp-base-${process.platform}-${process.arch}`,
|
|
157
224
|
network_id: process.env.NETWORK_ID || "base-usdc",
|
|
158
|
-
session_id: `
|
|
159
|
-
|
|
225
|
+
session_id: `base-${Math.random().toString(36).substring(7)}`,
|
|
226
|
+
// Primitive 1: the intent this card is derived from. Server signs it and
|
|
227
|
+
// returns the signed object; older servers simply ignore this field.
|
|
228
|
+
...(intent ? { intent } : {}),
|
|
160
229
|
});
|
|
161
230
|
if (!data)
|
|
162
231
|
return null;
|
|
@@ -171,11 +240,23 @@ async function issueTokenRemote(cardAlias, amount, merchant) {
|
|
|
171
240
|
ttl_seconds: 3600,
|
|
172
241
|
used: false,
|
|
173
242
|
tx_hash: data.tx_hash,
|
|
174
|
-
mode: '
|
|
243
|
+
mode: 'base_usdc',
|
|
244
|
+
intent: data.intent || null, // signed intent (intent_id, hash, signature) if server supports it
|
|
175
245
|
mcp_warning: data._mcp_warning || null, // Relay backend version warning to agent
|
|
176
246
|
};
|
|
177
247
|
}
|
|
178
248
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
249
|
+
// Receipt (Primitive 3): ask the Dashboard to build+sign a receipt for a
|
|
250
|
+
// CONFIRMED payment. Never blocks the payment result — caller treats null as
|
|
251
|
+
// "receipt unavailable" and the purchase is still fine.
|
|
252
|
+
// ──────────────────────────────────────────────────────────────────────────────
|
|
253
|
+
async function postReceiptRemote(payload) {
|
|
254
|
+
const data = await apiRequest('/api/receipts', 'POST', payload);
|
|
255
|
+
if (!data || data.error)
|
|
256
|
+
return null;
|
|
257
|
+
return data;
|
|
258
|
+
}
|
|
259
|
+
// ──────────────────────────────────────────────────────────────────────────────
|
|
179
260
|
// Resolve Token: Same as custodial (returns PAN/CVV for Playwright injection)
|
|
180
261
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
181
262
|
async function resolveTokenRemote(token) {
|
|
@@ -193,6 +274,8 @@ async function resolveTokenRemote(token) {
|
|
|
193
274
|
cvv: data.cvv,
|
|
194
275
|
name: data.name || "Z-ZERO AI AGENT",
|
|
195
276
|
authorized_amount: data.authorized_amount ? Number(data.authorized_amount) : undefined,
|
|
277
|
+
intent_id: data.intent_id || undefined, // Primitive 1 linkage (if server supports it)
|
|
278
|
+
intent_hash: data.intent_hash || undefined,
|
|
196
279
|
};
|
|
197
280
|
}
|
|
198
281
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
@@ -203,7 +286,6 @@ async function burnTokenRemote(token, receipt_id) {
|
|
|
203
286
|
token,
|
|
204
287
|
receipt_id,
|
|
205
288
|
success: true,
|
|
206
|
-
wallet_mode: 'wdk', // Signals Dashboard to refund underspend on-chain
|
|
207
289
|
});
|
|
208
290
|
return !!data && !data.error;
|
|
209
291
|
}
|
|
@@ -213,7 +295,6 @@ async function burnTokenRemote(token, receipt_id) {
|
|
|
213
295
|
async function cancelTokenRemote(token) {
|
|
214
296
|
const data = await apiRequest('/api/tokens/cancel', 'POST', {
|
|
215
297
|
token,
|
|
216
|
-
wallet_mode: 'wdk', // Triggers on-chain USDC refund
|
|
217
298
|
});
|
|
218
299
|
if (data?.error)
|
|
219
300
|
return data;
|
|
@@ -229,5 +310,5 @@ async function cancelTokenRemote(token) {
|
|
|
229
310
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
230
311
|
async function refundUnderspendRemote(token, actualSpent) {
|
|
231
312
|
// console.error (NOT console.log) — stdout is the MCP stdio transport; logging there corrupts the protocol.
|
|
232
|
-
console.error(`[
|
|
313
|
+
console.error(`[Z-ZERO MCP] Token ${token} burned. Actual spent: $${actualSpent}. On-chain refund handled by Dashboard.`);
|
|
233
314
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export declare const TAXONOMY_VERSION = 1;
|
|
2
|
+
/**
|
|
3
|
+
* Fixed failure classes. Each has a detector (how it is recognized) and a
|
|
4
|
+
* remediation (what the loop should try next time). Do NOT free-text new
|
|
5
|
+
* classes — extend the enum and bump TAXONOMY_VERSION instead.
|
|
6
|
+
*/
|
|
7
|
+
export declare const FAILURE_CLASSES: readonly ["card_declined_issuer", "card_declined_bin_block", "avs_mismatch", "3ds_required", "bot_detected", "form_changed", "price_changed", "out_of_stock", "shipping_unsupported", "login_required", "timeout", "outcome_unconfirmed", "intent_mismatch", "unknown"];
|
|
8
|
+
export type FailureClass = (typeof FAILURE_CLASSES)[number];
|
|
9
|
+
/** Where in the checkout flow the failure happened. */
|
|
10
|
+
export declare const CHECKOUT_STEPS: readonly ["preflight", "navigate", "pre_steps", "fill_form", "submit", "confirm"];
|
|
11
|
+
export type CheckoutStep = (typeof CHECKOUT_STEPS)[number];
|
|
12
|
+
/** Structured checkout event — one row per failed (or notable) run. */
|
|
13
|
+
export interface CheckoutEvent {
|
|
14
|
+
url: string;
|
|
15
|
+
failure_class: FailureClass;
|
|
16
|
+
step?: CheckoutStep;
|
|
17
|
+
raw_error_type?: string;
|
|
18
|
+
error_message?: string;
|
|
19
|
+
remediation_tried?: string;
|
|
20
|
+
outcome?: "failed" | "recovered" | "aborted";
|
|
21
|
+
labeled_by: "auto" | "agent" | "human";
|
|
22
|
+
card_bin?: string;
|
|
23
|
+
intent_id?: string;
|
|
24
|
+
evidence_ref?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Mask any 12–19 digit sequence (with optional space/dash separators) and CVV-looking patterns.
|
|
28
|
+
*
|
|
29
|
+
* NFKC first: full-width digits (4242…) read as a card number to a human and
|
|
30
|
+
* to anything downstream, but slip past an ASCII \d regex. Normalising folds
|
|
31
|
+
* them to ASCII so one pattern catches both forms.
|
|
32
|
+
*/
|
|
33
|
+
export declare function redactCardData(text: string): string;
|
|
34
|
+
/** First 6 digits of a PAN — safe to log (BIN is not cardholder data). */
|
|
35
|
+
export declare function extractBin(cardNumber: string | undefined): string | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Map a bridge PaymentOutcome (+ message) to a failure_class + step.
|
|
38
|
+
* Returns null for 'confirmed' (nothing to record as a failure).
|
|
39
|
+
*/
|
|
40
|
+
export declare function classifyBridgeResult(status: string, message?: string): {
|
|
41
|
+
failure_class: FailureClass;
|
|
42
|
+
step: CheckoutStep;
|
|
43
|
+
} | null;
|
|
44
|
+
/**
|
|
45
|
+
* Legacy string → enum map. Old published MCP versions (and old SOP text)
|
|
46
|
+
* send free-text error_type; the backend uses this same map server-side.
|
|
47
|
+
*/
|
|
48
|
+
export declare const LEGACY_ERROR_TYPE_MAP: Record<string, FailureClass>;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ============================================================
|
|
3
|
+
// Z-ZERO Failure Taxonomy — single source of truth (v1)
|
|
4
|
+
//
|
|
5
|
+
// Every checkout failure MUST map to exactly one failure_class below.
|
|
6
|
+
// This enum is the schema for the labeled-event corpus (Primitive 2,
|
|
7
|
+
// docs/zzero_agent_primitives_30_07_26.md): labels cannot be assigned
|
|
8
|
+
// retroactively, so this file exists BEFORE the self-healing loop does.
|
|
9
|
+
//
|
|
10
|
+
// ⚠️ KEEP IN SYNC with z-zero-dashboard/src/lib/failure-taxonomy.ts
|
|
11
|
+
// (two repos, no shared package — bump TAXONOMY_VERSION together).
|
|
12
|
+
// ============================================================
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.LEGACY_ERROR_TYPE_MAP = exports.CHECKOUT_STEPS = exports.FAILURE_CLASSES = exports.TAXONOMY_VERSION = void 0;
|
|
15
|
+
exports.redactCardData = redactCardData;
|
|
16
|
+
exports.extractBin = extractBin;
|
|
17
|
+
exports.classifyBridgeResult = classifyBridgeResult;
|
|
18
|
+
exports.TAXONOMY_VERSION = 1;
|
|
19
|
+
/**
|
|
20
|
+
* Fixed failure classes. Each has a detector (how it is recognized) and a
|
|
21
|
+
* remediation (what the loop should try next time). Do NOT free-text new
|
|
22
|
+
* classes — extend the enum and bump TAXONOMY_VERSION instead.
|
|
23
|
+
*/
|
|
24
|
+
exports.FAILURE_CLASSES = [
|
|
25
|
+
"card_declined_issuer", // issuer/webhook decline → check velocity/MCC/funds
|
|
26
|
+
"card_declined_bin_block", // merchant rejects prepaid/virtual BIN → try different BIN
|
|
27
|
+
"avs_mismatch", // address verification failed → use real billing address
|
|
28
|
+
"3ds_required", // SCA challenge appeared → escalate request_human_approval
|
|
29
|
+
"bot_detected", // captcha / Cloudflare / "unusual activity" → new session, slow down
|
|
30
|
+
"form_changed", // selector not found / DOM changed → re-probe, update hints
|
|
31
|
+
"price_changed", // total differs from what was authorized → request new token/intent
|
|
32
|
+
"out_of_stock", // item unavailable at checkout → back to agent for new intent
|
|
33
|
+
"shipping_unsupported", // merchant can't ship to address → fail early, don't issue card
|
|
34
|
+
"login_required", // checkout requires an account → out of scope, report clearly
|
|
35
|
+
"timeout", // page/flow timed out → check hints whether retry is safe
|
|
36
|
+
"outcome_unconfirmed", // submitted but no confirmation signal → verify before any retry
|
|
37
|
+
"intent_mismatch", // reserved for Primitive 1 (checkout page ≠ signed intent)
|
|
38
|
+
"unknown", // catch-all — target of the weekly human-labeling pass
|
|
39
|
+
];
|
|
40
|
+
/** Where in the checkout flow the failure happened. */
|
|
41
|
+
exports.CHECKOUT_STEPS = [
|
|
42
|
+
"preflight", // before the browser touched the page (amount guard, balance, config)
|
|
43
|
+
"navigate", // opening the checkout URL
|
|
44
|
+
"pre_steps", // walking hints.pre_steps (shipping form, accordion, etc.)
|
|
45
|
+
"fill_form", // injecting card fields
|
|
46
|
+
"submit", // clicking Pay
|
|
47
|
+
"confirm", // waiting for / parsing the confirmation signal
|
|
48
|
+
];
|
|
49
|
+
// ------------------------------------------------------------
|
|
50
|
+
// Redaction — 🔴 redact at capture time, never after.
|
|
51
|
+
// Screenshots/DOM/HAR/messages must never contain PAN or CVV.
|
|
52
|
+
// ------------------------------------------------------------
|
|
53
|
+
/**
|
|
54
|
+
* Mask any 12–19 digit sequence (with optional space/dash separators) and CVV-looking patterns.
|
|
55
|
+
*
|
|
56
|
+
* NFKC first: full-width digits (4242…) read as a card number to a human and
|
|
57
|
+
* to anything downstream, but slip past an ASCII \d regex. Normalising folds
|
|
58
|
+
* them to ASCII so one pattern catches both forms.
|
|
59
|
+
*/
|
|
60
|
+
function redactCardData(text) {
|
|
61
|
+
if (!text)
|
|
62
|
+
return text;
|
|
63
|
+
return text
|
|
64
|
+
.normalize("NFKC")
|
|
65
|
+
// PAN: 12-19 digits, allowing spaces or dashes between groups
|
|
66
|
+
.replace(/\b(?:\d[ -]?){12,19}\b/g, "[PAN_REDACTED]")
|
|
67
|
+
// CVV mentioned next to a 3-4 digit number
|
|
68
|
+
.replace(/\b(cvv|cvc|security code)\b\s*:?\s*\d{3,4}/gi, "$1 [REDACTED]");
|
|
69
|
+
}
|
|
70
|
+
/** First 6 digits of a PAN — safe to log (BIN is not cardholder data). */
|
|
71
|
+
function extractBin(cardNumber) {
|
|
72
|
+
const digits = (cardNumber || "").replace(/\D/g, "");
|
|
73
|
+
return digits.length >= 6 ? digits.slice(0, 6) : undefined;
|
|
74
|
+
}
|
|
75
|
+
// ------------------------------------------------------------
|
|
76
|
+
// Auto-classification of Playwright bridge outcomes.
|
|
77
|
+
// This is what turns today's traffic into labeled data without
|
|
78
|
+
// waiting for the agent to call report_checkout_fail.
|
|
79
|
+
// ------------------------------------------------------------
|
|
80
|
+
const BIN_BLOCK_RE = /prepaid|virtual card|this card type|card type (?:is )?not (?:accepted|supported)/i;
|
|
81
|
+
const AVS_RE = /billing address|address (?:verification|does not match|mismatch)|avs/i;
|
|
82
|
+
const THREEDS_RE = /3d[\s-]?secure|verified by visa|additional (?:verification|authentication)|authentication required/i;
|
|
83
|
+
const BOT_RE = /captcha|unusual activity|cloudflare|are you a robot|access denied|blocked/i;
|
|
84
|
+
/**
|
|
85
|
+
* Map a bridge PaymentOutcome (+ message) to a failure_class + step.
|
|
86
|
+
* Returns null for 'confirmed' (nothing to record as a failure).
|
|
87
|
+
*/
|
|
88
|
+
function classifyBridgeResult(status, message) {
|
|
89
|
+
const msg = message || "";
|
|
90
|
+
switch (status) {
|
|
91
|
+
case "confirmed":
|
|
92
|
+
return null;
|
|
93
|
+
case "declined":
|
|
94
|
+
if (BIN_BLOCK_RE.test(msg))
|
|
95
|
+
return { failure_class: "card_declined_bin_block", step: "confirm" };
|
|
96
|
+
if (AVS_RE.test(msg))
|
|
97
|
+
return { failure_class: "avs_mismatch", step: "confirm" };
|
|
98
|
+
if (THREEDS_RE.test(msg))
|
|
99
|
+
return { failure_class: "3ds_required", step: "confirm" };
|
|
100
|
+
return { failure_class: "card_declined_issuer", step: "confirm" };
|
|
101
|
+
case "unconfirmed":
|
|
102
|
+
return { failure_class: "outcome_unconfirmed", step: "confirm" };
|
|
103
|
+
case "not_submitted":
|
|
104
|
+
return { failure_class: "form_changed", step: "submit" };
|
|
105
|
+
case "no_fields":
|
|
106
|
+
if (BOT_RE.test(msg))
|
|
107
|
+
return { failure_class: "bot_detected", step: "navigate" };
|
|
108
|
+
return { failure_class: "form_changed", step: "fill_form" };
|
|
109
|
+
case "error":
|
|
110
|
+
if (/timeout|timed out/i.test(msg))
|
|
111
|
+
return { failure_class: "timeout", step: "navigate" };
|
|
112
|
+
if (BOT_RE.test(msg))
|
|
113
|
+
return { failure_class: "bot_detected", step: "navigate" };
|
|
114
|
+
return { failure_class: "unknown", step: "navigate" };
|
|
115
|
+
default:
|
|
116
|
+
return { failure_class: "unknown", step: "navigate" };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Legacy string → enum map. Old published MCP versions (and old SOP text)
|
|
121
|
+
* send free-text error_type; the backend uses this same map server-side.
|
|
122
|
+
*/
|
|
123
|
+
exports.LEGACY_ERROR_TYPE_MAP = {
|
|
124
|
+
field_not_found: "form_changed",
|
|
125
|
+
timeout: "timeout",
|
|
126
|
+
bot_blocked: "bot_detected",
|
|
127
|
+
unknown_form: "form_changed",
|
|
128
|
+
price_mismatch: "price_changed",
|
|
129
|
+
other: "unknown",
|
|
130
|
+
};
|