felix-mcp 2.0.123 → 2.0.129

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.
@@ -37,7 +37,7 @@ export const RELAY_TRUSTED_SOLVERS = [
37
37
  const ORDER_DEADLINE_MARGIN_S = 60;
38
38
  // Relay's v1 order schema (Felix: retail_relay_funding_adapter._RELAY_ORDER_TYPES).
39
39
  // The order id is the EIP-712 struct hash of the order, with no domain.
40
- const RELAY_ORDER_TYPES = {
40
+ export const RELAY_ORDER_TYPES = {
41
41
  Order: [
42
42
  { name: "version", type: "string" }, { name: "solverChainId", type: "string" },
43
43
  { name: "solver", type: "address" }, { name: "salt", type: "uint256" },
@@ -274,6 +274,31 @@ function hexBytes(value, name) {
274
274
  hostile(`${name} is not hex bytes`);
275
275
  return value.toLowerCase();
276
276
  }
277
+ const UINT256_MAX = (1n << 256n) - 1n;
278
+ const SALT_DECIMAL = /^(0|[1-9][0-9]{0,77})$/;
279
+ const SALT_HEX = /^0x[0-9a-fA-F]{1,64}$/;
280
+ /**
281
+ * RELAY_ORDER_SALT_2026_09_26: the order's `salt` is a full-width uint256 and
282
+ * real Relay orders carry it as 0x hex (32 bytes); the monetary `units` reader
283
+ * is deliberately bounded far below that and stays unchanged. Accept exactly a
284
+ * canonical decimal (no sign, fraction, whitespace or leading zeros) or 0x hex
285
+ * of at most 32 bytes, as a string, or a safe non-negative JS integer; reject
286
+ * anything above 2^256-1.
287
+ */
288
+ export function orderSalt(value, name = "order.salt") {
289
+ if (typeof value === "number") {
290
+ if (Number.isSafeInteger(value) && value >= 0)
291
+ return BigInt(value);
292
+ hostile(`${name} is not a safe integer`);
293
+ }
294
+ if (typeof value !== "string" || value.length > 78 || !(SALT_DECIMAL.test(value) || SALT_HEX.test(value))) {
295
+ hostile(`${name} is not a uint256`);
296
+ }
297
+ const salt = BigInt(value);
298
+ if (salt > UINT256_MAX)
299
+ hostile(`${name} overflows uint256`);
300
+ return salt;
301
+ }
277
302
  function orderUint(value, name) {
278
303
  if (typeof value === "number" && Number.isSafeInteger(value) && value >= 0)
279
304
  return BigInt(value);
@@ -285,7 +310,7 @@ function text(value, name) {
285
310
  return value;
286
311
  }
287
312
  /** The order as typed EIP-712 data, every field present, every type exact. */
288
- function relayOrderMessage(order) {
313
+ export function relayOrderMessage(order) {
289
314
  const o = exactKeys(order, ["version", "solverChainId", "solver", "salt", "inputs", "output", "fees"], "order");
290
315
  if (!Array.isArray(o.inputs) || o.inputs.length !== 1)
291
316
  hostile("order must have exactly one input");
@@ -302,7 +327,7 @@ function relayOrderMessage(order) {
302
327
  version: text(o.version, "order.version"),
303
328
  solverChainId: text(o.solverChainId, "order.solverChainId"),
304
329
  solver: getAddress(lower(o.solver, "order.solver")),
305
- salt: orderUint(o.salt, "order.salt"),
330
+ salt: orderSalt(o.salt, "order.salt"),
306
331
  inputs: [{
307
332
  payment: {
308
333
  chainId: text(payment.chainId, "payment.chainId"), currency: hexBytes(payment.currency, "payment.currency"),
@@ -0,0 +1,209 @@
1
+ // SAFE_NONCE_INVALIDATION_2026_09_28 (A3a/B5): dependency-free guidance the Arc
2
+ // drivers attach to their answers: the exact invalidate_safe_nonce call that
3
+ // releases a reserved Arc home Safe nonce, and the caller's own open journey
4
+ // named by a Felix *_in_progress refusal. Nothing here signs or sends; the
5
+ // driver that acts on it is src/arc-safe-invalidation.ts.
6
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
7
+ function isRecord(value) {
8
+ return typeof value === "object" && value !== null && !Array.isArray(value);
9
+ }
10
+ export const SAFE_INVALIDATION_TOOL = "invalidate_safe_nonce";
11
+ export const SAFE_INVALIDATION_ROUTE_PREFIX = "/v1/arc-home/safe-invalidations";
12
+ export const INVALIDATION_WRITERS = ["withdrawal_source", "hypercore", "polygon"];
13
+ /** The engine's refusal naming the operation that holds the Safe's next nonce. */
14
+ export const SAFE_NONCE_INVALIDATION_REQUIRED = "arc_safe_nonce_invalidation_required";
15
+ /** The engine's older refusal: the nonce is held, the holder is not named. */
16
+ export const SAFE_NONCE_RESERVED = "arc_safe_nonce_reserved";
17
+ /** The engine's observe-only next_action for an issued, unexecuted Safe payload. */
18
+ export const ENGINE_OBSERVE_NEXT_ACTION = "reconcile_or_invalidate_nonce";
19
+ export const INVALIDATION_EFFECT = "Signs one no-op Safe transaction (a call to the owner's own address with value 0 and no data) at the "
20
+ + "reserved Safe nonce. It moves no funds. Once it executes, the unexecuted original at that nonce can never "
21
+ + "execute, the Safe is free for new Felix operations, and Felix closes the original operation.";
22
+ export function isInvalidationWriter(value) {
23
+ return typeof value === "string" && INVALIDATION_WRITERS.includes(value);
24
+ }
25
+ /** The exact invalidate_safe_nonce call that releases one operation's reserved Safe nonce. */
26
+ export function invalidationSuggestion(writer, operationId, nonce, reason, invalidationId) {
27
+ const id = String(operationId).toLowerCase();
28
+ const child = typeof invalidationId === "string" && UUID.test(invalidationId.toLowerCase()) ? invalidationId.toLowerCase() : null;
29
+ return {
30
+ tool: SAFE_INVALIDATION_TOOL,
31
+ arguments: {
32
+ original_writer: writer, original_operation_id: id,
33
+ ...(typeof nonce === "number" && Number.isSafeInteger(nonce) && nonce >= 0 ? { expected_safe_nonce: nonce } : {}),
34
+ ...(child ? { invalidation_id: child } : {}),
35
+ },
36
+ reason: reason ?? "This operation's Safe transaction was issued but not executed, so its Safe nonce stays "
37
+ + "reserved and blocks every later Felix Safe move.",
38
+ effect: INVALIDATION_EFFECT,
39
+ };
40
+ }
41
+ /** The error record of a Felix refusal (top-level or FastAPI detail). */
42
+ export function errorRecord(response) {
43
+ if (isRecord(response.error))
44
+ return response.error;
45
+ const detail = isRecord(response.detail) ? response.detail.error : undefined;
46
+ return isRecord(detail) ? detail : {};
47
+ }
48
+ /** The stable machine reason of a Felix refusal (error.reason, else error.code). */
49
+ export function reasonOf(response) {
50
+ const error = errorRecord(response);
51
+ return String(error.reason || error.code || "");
52
+ }
53
+ /** The engine's next_action values on 409 arc_safe_nonce_invalidation_required (arc_safe_nonce_reservations.invalidation_details). */
54
+ export const ENGINE_NONCE_NEXT_ACTION = {
55
+ invalidate: "invalidate_safe_nonce",
56
+ reconcileBlocker: "reconcile_blocking_operation",
57
+ completeInvalidation: "complete_invalidation",
58
+ contactSupport: "contact_support",
59
+ };
60
+ /** The tool that resumes or reconciles each blocking writer's own journey. */
61
+ const BLOCKER_TOOL = {
62
+ withdrawal_source: "reconcile_arc_home_operation (operation_id = blocking_operation.operation_id)",
63
+ withdrawal_mint: "reconcile_arc_home_operation (operation_id = blocking_operation.operation_id)",
64
+ hypercore: "fund_hypercore_from_arc (with the idempotency_key that started it)",
65
+ polygon: "fund_polymarket_from_arc (with the idempotency_key that started it)",
66
+ };
67
+ const NOT_AN_INVALIDATION = "Do NOT call invalidate_safe_nonce for it: its Safe transaction may still execute, and a "
68
+ + "no-op at the same nonce would race and cancel a live operation.";
69
+ /**
70
+ * Guidance for a Felix refusal caused by a reserved Safe nonce, or null for any
71
+ * other refusal. The engine's 409 arc_safe_nonce_invalidation_required names
72
+ * the blocking operation (blocking_writer / blocking_operation_id, aliases
73
+ * original_*) and its OWN next_action, which is honoured exactly:
74
+ * * invalidate_safe_nonce -- the exact invalidate_safe_nonce call;
75
+ * * reconcile_blocking_operation -- the blocker may still send its SafeTx:
76
+ * reconcile it with its own tool, never invalidate it;
77
+ * * complete_invalidation -- an invalidation already exists for the nonce:
78
+ * the invalidate_safe_nonce call that completes THAT child (invalidation_id);
79
+ * * contact_support -- no client exit (orphan or unsupported writer).
80
+ * Without an engine next_action (an older engine, or the older 503
81
+ * arc_safe_nonce_reserved) no invalidation is suggested: the blocker is
82
+ * reconciled first, and its own tool names invalidate_safe_nonce only once its
83
+ * SafeTx can no longer execute.
84
+ */
85
+ export function safeNonceRefusalGuidance(response) {
86
+ if (!isRecord(response))
87
+ return null;
88
+ const reason = reasonOf(response);
89
+ if (reason !== SAFE_NONCE_INVALIDATION_REQUIRED && reason !== SAFE_NONCE_RESERVED)
90
+ return null;
91
+ const error = errorRecord(response);
92
+ const writerRaw = error.blocking_writer ?? error.original_writer;
93
+ const writer = typeof writerRaw === "string" ? writerRaw : null;
94
+ const rawId = error.blocking_operation_id ?? error.original_operation_id;
95
+ const id = typeof rawId === "string" && UUID.test(rawId.toLowerCase()) ? rawId.toLowerCase() : null;
96
+ const nonce = typeof error.safe_nonce === "number" && Number.isSafeInteger(error.safe_nonce) && error.safe_nonce >= 0
97
+ ? error.safe_nonce : null;
98
+ const rawChild = typeof error.invalidation_id === "string" ? error.invalidation_id.toLowerCase() : "";
99
+ const invalidationId = UUID.test(rawChild) ? rawChild : null;
100
+ const engineNext = reason === SAFE_NONCE_INVALIDATION_REQUIRED && typeof error.next_action === "string"
101
+ ? error.next_action : null;
102
+ const blocking = {
103
+ writer, operation_id: id,
104
+ phase: typeof error.blocking_phase === "string" ? error.blocking_phase : null,
105
+ safe_nonce: nonce,
106
+ safe_tx_hash: typeof error.blocking_safe_tx_hash === "string" ? error.blocking_safe_tx_hash : null,
107
+ };
108
+ const reconcileFirst = (hint) => ({
109
+ next_action: ENGINE_NONCE_NEXT_ACTION.reconcileBlocker,
110
+ blocking_operation: blocking,
111
+ ...(writer && BLOCKER_TOOL[writer] ? { resume_tool: BLOCKER_TOOL[writer] } : {}),
112
+ safe_nonce_hint: hint,
113
+ });
114
+ if (engineNext === ENGINE_NONCE_NEXT_ACTION.invalidate) {
115
+ if (isInvalidationWriter(writer) && id) {
116
+ return {
117
+ next_action: SAFE_INVALIDATION_TOOL, blocking_operation: blocking,
118
+ safe_nonce_invalidation: invalidationSuggestion(writer, id, nonce, "Felix refused because an earlier operation's issued Safe transaction holds the Safe's next nonce and "
119
+ + "can no longer be sent by Felix."),
120
+ };
121
+ }
122
+ return { next_action: ENGINE_NONCE_NEXT_ACTION.contactSupport, blocking_operation: blocking,
123
+ safe_nonce_hint: "Felix asks for an invalidation of an operation this client cannot name; contact support." };
124
+ }
125
+ if (engineNext === ENGINE_NONCE_NEXT_ACTION.reconcileBlocker) {
126
+ return reconcileFirst("The Safe nonce is held by an operation that may still execute its Safe transaction. "
127
+ + `Reconcile or resume that operation with its own tool; it settles from its own proof. ${NOT_AN_INVALIDATION}`);
128
+ }
129
+ if (engineNext === ENGINE_NONCE_NEXT_ACTION.completeInvalidation) {
130
+ const suggestion = isInvalidationWriter(writer) && id && invalidationId
131
+ ? invalidationSuggestion(writer, id, nonce, "Felix already holds an invalidation for this Safe nonce; complete "
132
+ + "THAT invalidation (invalidation_id). A new one cannot be prepared for the same nonce.", invalidationId)
133
+ : null;
134
+ return {
135
+ next_action: ENGINE_NONCE_NEXT_ACTION.completeInvalidation, blocking_operation: blocking,
136
+ invalidation_id: invalidationId,
137
+ ...(suggestion ? { safe_nonce_invalidation: suggestion }
138
+ : { safe_nonce_hint: "An invalidation already exists for this Safe nonce, but this client cannot name it "
139
+ + "completely; contact support with the invalidation id." }),
140
+ };
141
+ }
142
+ if (engineNext === ENGINE_NONCE_NEXT_ACTION.contactSupport) {
143
+ return {
144
+ next_action: ENGINE_NONCE_NEXT_ACTION.contactSupport, blocking_operation: blocking,
145
+ safe_nonce_hint: "The Safe nonce is held by a reservation this client has no exit for (no operation can be "
146
+ + "named, or its writer cannot be invalidated by the owner here). Contact support; nothing was signed.",
147
+ };
148
+ }
149
+ if (engineNext !== null) {
150
+ // An engine next_action this client does not know: passed through, never widened to an invalidation.
151
+ return { next_action: engineNext, blocking_operation: blocking };
152
+ }
153
+ return reconcileFirst(id
154
+ ? "The Safe nonce is held by the operation named here. Reconcile or resume it with its own tool first; it names "
155
+ + `invalidate_safe_nonce only once its Safe transaction can no longer execute. ${NOT_AN_INVALIDATION}`
156
+ : "The Safe nonce is held by an earlier Felix operation that was not named. Resume your last unfinished Arc "
157
+ + "withdrawal, HyperCore funding or Polymarket funding with its own tool and idempotency key; it names "
158
+ + "invalidate_safe_nonce (with exact arguments) only once its Safe transaction can no longer execute.");
159
+ }
160
+ /**
161
+ * The caller's own open journey id from a Felix *_in_progress refusal (engine
162
+ * B5), or null. The returns (relay / HyperCore / PM) name it open_journey_id;
163
+ * the HyperCore and Polygon fundings' one-open refusals name it
164
+ * open_funding_id (engine A3b). Either field is read.
165
+ */
166
+ export function openJourneyId(response) {
167
+ if (!isRecord(response))
168
+ return null;
169
+ const error = errorRecord(response);
170
+ const reason = reasonOf(response);
171
+ const raw = typeof error.open_journey_id === "string" ? error.open_journey_id
172
+ : typeof error.open_funding_id === "string" ? error.open_funding_id : "";
173
+ const id = raw.toLowerCase();
174
+ return reason.endsWith("_in_progress") && UUID.test(id) ? id : null;
175
+ }
176
+ /**
177
+ * One passive read of the account's open journey that blocked a new prepare.
178
+ * Never a reconcile, never a signature: it only reports the open journey's
179
+ * phase and the next step, including the invalidation call when Felix is
180
+ * observing an issued, unexecuted Safe payload.
181
+ */
182
+ export async function describeOpenJourney(call, config) {
183
+ const open = { id: config.id, read_path: config.readPath, phase: null, server_next_action: null };
184
+ let view = null;
185
+ try {
186
+ const answer = await call("GET", `${config.readPath}?account_id=${encodeURIComponent(config.accountId)}`, undefined, { retryOnError: true });
187
+ if (isRecord(answer) && answer.ok !== false && String(answer.id || "").toLowerCase() === config.id)
188
+ view = answer;
189
+ }
190
+ catch {
191
+ view = null;
192
+ }
193
+ if (view) {
194
+ open.phase = typeof view.phase === "string" ? view.phase : null;
195
+ open.server_next_action = typeof view.next_action === "string" ? view.next_action : null;
196
+ }
197
+ const observing = view !== null && config.writer !== undefined && view.phase !== "source_not_executed"
198
+ && (view.next_action === ENGINE_OBSERVE_NEXT_ACTION || view.phase === "expired" || view.phase === "cancelled");
199
+ return {
200
+ open_journey: open,
201
+ next_action: observing ? SAFE_INVALIDATION_TOOL : "resume_open_journey",
202
+ ...(observing ? { safe_nonce_invalidation: invalidationSuggestion(config.writer, config.id) } : {}),
203
+ open_journey_hint: observing
204
+ ? "Felix is observing this account's open journey for an execution of an issued Safe transaction that "
205
+ + "was never completed. Invalidate its Safe nonce to close it; nothing new is prepared meanwhile."
206
+ : `This account already has an open journey. Resume it with ${config.resumeTool} and the idempotency_key `
207
+ + "that started it; do not start another one with a new key.",
208
+ };
209
+ }