From 564630e027256eb2d3d55fcff3f21d88b9bc72d1 Mon Sep 17 00:00:00 2001 From: 1bcMax Date: Fri, 13 Nov 2026 23:28:06 +0000 Subject: [PATCH 1/2] fix(modal): accept sandbox lifetimes up to 24 h and refuse quotes above the expected price The gateways accept create timeouts from 10 to 86400 s (probed 2026-11-13: 86400 -> 402 quote, 86401 -> 400 on sol.blockrun.ai and blockrun.ai), but ModalCreate refused anything above 300 s and told the model the gateway would 400 it. - Lift the client cap to 86400 s (min 10, integer, cpu >= 0.125); default 300. - Tool text explains timeout is the BILLED lifetime: flat only at <= 300 s, <= 1 CPU and <= 1024 MB; otherwise hourly x the full requested lifetime, upfront, never below flat, no refund on early terminate. - modalCreatePricing mirrors getModalCreatePricing: the lifetime now decides flat vs hourly, and CPU sandboxes bill $0.25/h (a 24 h CPU create was priced $0.01 against a $6.00 quote). - postWithPayment takes the expected list price and refuses to sign a 402 quote above list + Base fee (or an unreadable one); all Modal calls pass it. ModalCreate reserves that same ceiling. --- src/tools/modal.ts | 241 ++++++++++++++++++++++++++++--------- test/local.mjs | 19 +++ test/reservation.local.mjs | 147 ++++++++++++++++++++++ 3 files changed, 353 insertions(+), 54 deletions(-) diff --git a/src/tools/modal.ts b/src/tools/modal.ts index 2b96cfa2..d321b8de 100644 --- a/src/tools/modal.ts +++ b/src/tools/modal.ts @@ -4,15 +4,20 @@ * status, terminate}. See https://modal.com/docs/guide/sandboxes for the * underlying primitives. * - * Pricing (per-call, USDC): - * create at up to 1 CPU and 1024 MB: $0.01 (CPU) / $0.05 (T4) / $0.08 (L4) / $0.10 (A10G) / $0.20 (A100) / $0.40 (H100) - * create above that: hourly rate x lifetime, never below the flat rate: - * $4 (T4) / $4.50 (L4) / $5 (A10G) / $7 (A100) / $9 (H100) per hour + * Pricing (per-call, USDC; the Base gateway adds a $0.001 transaction fee): + * create with timeout <= 300s, cpu <= 1 and memory <= 1024 MB (flat): + * $0.01 (CPU) / $0.05 (T4) / $0.08 (L4) / $0.10 (A10G) / $0.20 (A100) / $0.40 (H100) + * any other create: hourly rate x the full requested lifetime, upfront, never + * below the flat rate, no refund on early terminate: + * $0.25 (CPU) / $4 (T4) / $4.50 (L4) / $5 (A10G) / $7 (A100) / $9 (H100) per hour * exec: $0.001 * status: $0.001 * terminate: $0.001 * - * Gateway constraints (probed 2026-05-02): + * Gateway constraints (probed 2026-05-02; lifetime re-probed 2026-11-13): + * - sandbox lifetime (create `timeout`): 10..86400 s (24 h). 86401 -> 400 on + * sol.blockrun.ai and blockrun.ai; 86400 -> 402 with a quote. + * - cpu >= 0.125 (Modal's own default; below that the Solana gateway 400s). * - image is fixed at python:3.11 — no custom containers yet. * - command is execve-style (string[]), not a shell string. We accept a * plain string from the LLM and auto-wrap to ["sh","-c", string]. @@ -45,6 +50,7 @@ import { walletReservation, AMBIGUOUS_GRACE_MS, type ReservationToken } from '.. import { recordUsage } from '../stats/tracker.js'; import { logger } from '../logger.js'; import { noChargeNote } from '../payments/billing-copy.js'; +import { GATEWAY_TRANSACTION_FEE_USD } from '../gateway-models.js'; // ─── Pricing table (probed from /.well-known/x402 + 402 responses) ───────── const CREATE_PRICE_USD: Record = { @@ -55,9 +61,14 @@ const CREATE_PRICE_USD: Record = { A100: 0.20, H100: 0.40, }; -// The gateway bills a larger sandbox by the hour for its whole lifetime. Keep in -// step with getModalCreatePricing in the gateway's src/lib/modal.ts. -const GPU_HOURLY_PRICE_USD: Record = { +// The gateway bills every create outside the flat tier by the hour for its whole +// requested lifetime. Keep in step with getModalCreatePricing in the gateway's +// src/lib/modal.ts (sol, arc and nano carry these rates; the Base gateway's are +// lower, so these never under-reserve there). The quote guard in +// postWithPayment refuses to sign anything above this table, so a gateway price +// rise surfaces as a clear refusal rather than a silent over-charge. +const HOURLY_PRICE_USD: Record = { + cpu: 0.25, T4: 4.00, L4: 4.50, A10G: 5.00, @@ -66,25 +77,75 @@ const GPU_HOURLY_PRICE_USD: Record = { }; const FLAT_RATE_MAX_CPU = 1; const FLAT_RATE_MAX_MEMORY_MB = 1024; +const FLAT_RATE_MAX_SECONDS = 300; const DEFAULT_TIMEOUT_SECONDS = 300; +/** Gateway bounds on create `timeout` (sol and base probed 2026-11-13; arc and nano share the schema). */ +export const MODAL_MIN_TIMEOUT_SECONDS = 10; +export const MODAL_MAX_TIMEOUT_SECONDS = 86_400; +export const MODAL_MIN_CPU = 0.125; +/** + * Per-call transaction fee the Base gateway adds to every quote (the Solana + * gateway charges none). The quote guard allows it on top of the list price. + */ +const QUOTE_FEE_ALLOWANCE_USD = GATEWAY_TRANSACTION_FEE_USD; + +export interface ModalCreatePricing { + priceUsd: number; + model: 'flat' | 'hourly'; + /** Set when model === 'hourly'. */ + hourlyRateUsd?: number; +} /** - * What the gateway will ask for this create. The reservation and the confirm - * dialog both read it, so it has to see size as well as GPU type: an 8-core T4 - * is $0.33, not the $0.05 a tier lookup says. Lifetime is capped at 300s here, - * so the CPU tier is always flat. + * What the gateway will ask for this create — mirrors getModalCreatePricing. + * Flat only when the lifetime is <= 300 s AND the size is <= 1 CPU / 1024 MB; + * anything else is hourly x the full requested lifetime, floored at the flat + * rate. The reservation, the quote ceiling and the confirm dialog all read it. */ +export function modalCreatePricing( + tier: string, + cpu?: number, + memory?: number, + timeoutSeconds: number = DEFAULT_TIMEOUT_SECONDS, +): ModalCreatePricing { + const flat = CREATE_PRICE_USD[tier]; + const flatEligible = + timeoutSeconds <= FLAT_RATE_MAX_SECONDS && + (cpu ?? 1) <= FLAT_RATE_MAX_CPU && + (memory ?? 512) <= FLAT_RATE_MAX_MEMORY_MB; + if (!flatEligible) { + const hourlyRateUsd = HOURLY_PRICE_USD[tier]; + const hourlyPrice = hourlyRateUsd * (timeoutSeconds / 3600); + if (hourlyPrice > flat) return { priceUsd: hourlyPrice, model: 'hourly', hourlyRateUsd }; + } + return { priceUsd: flat, model: 'flat' }; +} + export function modalCreatePriceUsd( tier: string, cpu?: number, memory?: number, timeoutSeconds: number = DEFAULT_TIMEOUT_SECONDS, ): number { - const flat = CREATE_PRICE_USD[tier]; - const hourly = GPU_HOURLY_PRICE_USD[tier]; - const small = (cpu ?? 1) <= FLAT_RATE_MAX_CPU && (memory ?? 512) <= FLAT_RATE_MAX_MEMORY_MB; - if (small || hourly === undefined) return flat; - return Math.max(flat, hourly * (timeoutSeconds / 3600)); + return modalCreatePricing(tier, cpu, memory, timeoutSeconds).priceUsd; +} + +/** + * The most this client will sign for a call listed at `listPriceUsd`: the list + * price plus the Base transaction fee, rounded UP to USDC's 6 decimals, plus + * one atomic unit for the gateway's own round-up of an hourly quote. Returned + * in atomic units (1 USDC = 1_000_000). + */ +export function maxSignableAtomic(listPriceUsd: number): bigint { + // Round the micro-dollar figure to 3 places first so float noise + // (0.1 + 0.001 = 0.10100000000000001) does not ceil an exact figure up a unit. + const micro = Math.ceil(Number(((listPriceUsd + QUOTE_FEE_ALLOWANCE_USD) * 1e6).toFixed(3))); + return BigInt(micro) + 1n; +} + +/** The ceiling as dollars — what the wallet reservation holds. */ +export function maxSignableUsd(listPriceUsd: number): number { + return Number(maxSignableAtomic(listPriceUsd)) / 1e6; } const EXEC_PRICE_USD = 0.001; @@ -151,6 +212,9 @@ async function signPayment( const paymentHeader = await extractPaymentReq(response); if (!paymentHeader) return null; + // maxTimeoutSeconds below is the x402 authorization validity window the 402 + // quotes (300 s), NOT the sandbox lifetime: a 24 h create still settles + // within seconds of signing, so it needs no change for long lifetimes. if (chain === 'solana') { const wallet = await getOrCreateSolanaWallet(); const paymentRequired = parsePaymentRequired(paymentHeader); @@ -211,6 +275,29 @@ async function extractPaymentReq(response: Response): Promise { return header; } +/** + * The amount (atomic USDC) a 402 asks for, picked the same way signPayment + * picks its option. Null when the 402 carries no readable quote. + */ +async function quotedAtomic(response: Response, chain: Chain): Promise { + try { + const header = await extractPaymentReq(response.clone()); + if (!header) return null; + const details = extractPaymentDetails( + parsePaymentRequired(header), + chain === 'solana' ? SOLANA_NETWORK : undefined, + ); + const amount = String(details.amount ?? ''); + return /^\d+$/.test(amount) ? BigInt(amount) : null; + } catch { + return null; + } +} + +function atomicToUsd(n: bigint): string { + return `$${(Number(n) / 1e6).toFixed(6)}`; +} + /** * Generic POST-with-x402-retry helper used by all four Modal endpoints. The * first POST gets a 402 with payment requirements; we sign and retry once @@ -226,6 +313,13 @@ async function extractPaymentReq(response: Response): Promise { * nothing was spent. Failures that provably never left this machine (signal * already aborted before dispatch, DNS / connection-refused) are NOT * ambiguous and release normally. + * + * `maxPriceUsd` is the list price this client expects for the call. When set, + * the 402's quote is checked BEFORE anything is signed: above + * maxSignableAtomic(maxPriceUsd), or unreadable, and nothing is signed or + * sent — the call returns 402 with `quote_exceeds_expected`. Without it a + * gateway quoting a long-lifetime create at $216 would be signed blind while + * the reservation held a fraction of that. */ export async function postWithPayment( endpoint: string, @@ -234,6 +328,7 @@ export async function postWithPayment( abortSignal: AbortSignal, timeoutMs: number, reservation?: ReservationToken | null, + maxPriceUsd?: number, ): Promise<{ ok: boolean; status: number; body: Record; raw: string }> { const chain = loadChain(); const headers: Record = { @@ -252,6 +347,18 @@ export async function postWithPayment( let response = await fetch(endpoint, { method: 'POST', signal: ctrl.signal, headers, body: payload }); if (response.status === 402 && walletMayPay(headers)) { + if (maxPriceUsd !== undefined) { + const quoted = await quotedAtomic(response, chain); + const ceiling = maxSignableAtomic(maxPriceUsd); + if (quoted === null || quoted > ceiling) { + const error = quoted === null + ? 'quote_exceeds_expected: the 402 carried no readable amount, so nothing was signed' + : `quote_exceeds_expected: the gateway asked ${atomicToUsd(quoted)}, above the ` + + `${atomicToUsd(ceiling)} this client expected, so nothing was signed`; + logger.warn(`[franklin] Modal ${endpoint}: ${error}`); + return { ok: false, status: 402, body: { error }, raw: '' }; + } + } const paymentHeaders = await paymentSigner(response, chain, endpoint, resourceDescription); if (!paymentHeaders) { return { ok: false, status: 402, body: { error: 'payment signing failed' }, raw: '' }; @@ -385,23 +492,26 @@ export const modalCreateCapability: CapabilityHandler = { name: 'ModalCreate', description: 'Create a Modal Python 3.11 sandbox (CPU or GPU) via the BlockRun gateway. ' + - 'Returns a sandbox_id you pass to ModalExec. Charged once per create. Up to 1 CPU and ' + - '1024 MB: CPU $0.01, T4 $0.05, L4 $0.08, A10G $0.10, A100 $0.20, H100 $0.40. Larger GPU ' + - 'sandboxes pay hourly for the lifetime, at least the same: T4 $4, L4 $4.50, A10G $5, A100 $7, H100 $9 per hour. ' + - 'IMPORTANT — current limitations (BlockRun gateway is in early-access for sandboxes):\n' + - ' - sandbox lifetime: 5 minutes MAX (gateway hard-cap, regardless of GPU tier)\n' + - ' - per ModalExec call: 60 seconds MAX wall-clock\n' + + 'Returns a sandbox_id you pass to ModalExec. Charged ONCE, UPFRONT, at create.\n' + + 'BILLING — `timeout` is the BILLED lifetime, not an idle timeout:\n' + + ' - timeout <= 300 s AND cpu <= 1 AND memory <= 1024 MB: flat rate. ' + + 'CPU $0.01, T4 $0.05, L4 $0.08, A10G $0.10, A100 $0.20, H100 $0.40.\n' + + ' - Anything else (timeout > 300 s, OR cpu > 1, OR memory > 1024 MB): hourly rate x the FULL ' + + 'requested timeout, charged upfront, never below the flat rate, NO refund when you terminate early. ' + + 'Per hour: CPU $0.25, T4 $4, L4 $4.50, A10G $5, A100 $7, H100 $9.\n' + + ' - Examples: {timeout: 600, gpu: "A100"} = $1.17; {timeout: 1200, gpu: "H100"} = $3.00; ' + + '{timeout: 86400, gpu: "H100"} = $216 even if the job finishes in a minute.\n' + + ' - Ask for the time the work needs, not a safe-looking ceiling. Keep timeout <= 300 with ' + + 'default size for short tasks.\n' + + 'Limits:\n' + + ' - sandbox lifetime: 10 s to 86400 s (24 h); default 300 s\n' + + ' - per ModalExec call: 60 seconds MAX wall-clock (use fire-and-poll below)\n' + ' - Python 3.11 only, no custom images yet\n' + - ' - 1 vCPU, 1 GiB RAM defaults\n' + - ' - GPU access is preview-tier (officially "coming later" in docs)\n' + + ' - 1 vCPU, 512 MB RAM defaults\n' + ' - No setup-time provisioning — every sandbox starts empty\n' + - 'These limits make this tool suitable for: GPU benchmarks (nvidia-smi, matmul), ' + - 'small model inference (≤3B params if weights pre-cached), CUDA kernel validation, ' + - 'short ad-hoc Python tasks. NOT suitable for: full LoRA / fine-tuning runs, ' + - 'pip install + model download + training (pip alone burns 1-2 min of the 5-min budget). ' + - 'Custom images + longer lifetime + GPU production tier are documented as "coming later" ' + - 'by BlockRun — for serious ML workloads tell the user to use Modal directly until then. ' + - 'Always call ModalTerminate when done. ' + + 'Good for: GPU benchmarks, CUDA kernel validation, model inference, ad-hoc Python, and ' + + 'longer jobs (pip install + model download + training) when the user accepts the hourly cost. ' + + 'Always call ModalTerminate when done (it stops the sandbox; it does not refund the lifetime). ' + 'Long-running command pattern: each ModalExec call is itself capped at 60s wall-clock. ' + 'For work that takes >60s (pip install, model download, training), use the ' + 'fire-and-poll pattern: ModalExec(["sh","-c","nohup > /workspace/log 2>&1 &"]) ' + @@ -410,7 +520,13 @@ export const modalCreateCapability: CapabilityHandler = { type: 'object', properties: { gpu: { type: 'string', description: 'GPU tier. One of T4, L4, A10G, A100, H100. Omit for CPU-only ($0.01).' }, - timeout: { type: 'number', description: 'Lifetime cap in seconds. Default + Max = 300 (5 min). Gateway rejects values > 300 with HTTP 400.' }, + timeout: { + type: 'number', + description: + 'BILLED sandbox lifetime in seconds: charged upfront for the whole value, never refunded. ' + + 'Default 300, min 10, max 86400 (24 h). <= 300 (with cpu <= 1, memory <= 1024) is the flat rate; ' + + '> 300 bills hourly for the full value (e.g. 600 s on A100 = $1.17, 3600 s CPU = $0.25, 86400 s H100 = $216).', + }, cpu: { type: 'number', description: 'Number of CPU cores. Default 1, min 0.125, max 1 for CPU sandboxes and 8 with a GPU. Above 1 bills hourly.' }, memory: { type: 'number', description: 'Memory MB. Default 512, max 1024 for CPU sandboxes and 32768 with a GPU. Above 1024 bills hourly.' }, }, @@ -456,42 +572,51 @@ export const modalCreateCapability: CapabilityHandler = { return { output: `Error: ${c.error}`, isError: true }; } } - // Gateway hard-caps sandbox lifetime at 300s. Cap client-side so we - // surface a clear error instead of letting the user pay $0.20 for a - // create that 400s on the wire. - const CREATE_TIMEOUT_MAX = 300; - if (typeof timeoutCoerced === 'number' && timeoutCoerced > CREATE_TIMEOUT_MAX) { + // Mirror the gateway's create schema (integer timeout in 10..86400, cpu + // >= 0.125) so a bad value fails here with a clear message instead of a + // bare 400 from the gateway. + const timeoutSec = typeof timeoutCoerced === 'number' ? timeoutCoerced : DEFAULT_TIMEOUT_SECONDS; + if ( + !Number.isInteger(timeoutSec) || + timeoutSec < MODAL_MIN_TIMEOUT_SECONDS || + timeoutSec > MODAL_MAX_TIMEOUT_SECONDS + ) { return { output: - `Error: timeout ${timeoutCoerced}s exceeds gateway max of ${CREATE_TIMEOUT_MAX}s. ` + - `BlockRun caps Modal sandbox lifetime at 5 minutes regardless of GPU tier. ` + - `For longer workloads, the work must be split across multiple sandboxes ` + - `(checkpoint + reload) or you need to ask BlockRun to lift this cap.`, + `Error: timeout must be a whole number of seconds from ${MODAL_MIN_TIMEOUT_SECONDS} to ` + + `${MODAL_MAX_TIMEOUT_SECONDS} (24 h), got ${timeoutSec}. Remember it is the billed lifetime: ` + + `above 300 s it is charged hourly for the full value, upfront, with no refund.`, isError: true, }; } + if (typeof cpuCoerced === 'number' && cpuCoerced < MODAL_MIN_CPU) { + return { output: `Error: cpu must be at least ${MODAL_MIN_CPU}, got ${cpuCoerced}.`, isError: true }; + } - const price = modalCreatePriceUsd( + const pricing = modalCreatePricing( tier, typeof cpuCoerced === 'number' ? cpuCoerced : undefined, typeof memoryCoerced === 'number' ? memoryCoerced : undefined, - typeof timeoutCoerced === 'number' ? timeoutCoerced : undefined, + timeoutSec, ); + const price = pricing.priceUsd; + const billingLine = pricing.model === 'hourly' + ? `hourly: ${fmtUsd(pricing.hourlyRateUsd ?? 0)}/h x ${(timeoutSec / 3600).toFixed(2)} h, upfront, no refund on early terminate` + : 'flat (timeout <= 300 s, cpu <= 1, memory <= 1024 MB)'; // ── AskUser cost preview (skipped if env auto-approve or non-UI mode) ── const autoApprove = process.env.FRANKLIN_MEDIA_AUTO_APPROVE_ALL === '1'; if (ctx.onAskUser && !autoApprove) { - const timeoutSec = raw.timeout ?? 300; const lines = [ `Create Modal sandbox?`, ``, ` Tier: ${tier === 'cpu' ? 'CPU only' : `GPU ${tier}`}`, ` Image: python:3.11`, - ` Timeout: ${timeoutSec}s (${(timeoutSec / 60).toFixed(1)} min)`, - ...(raw.cpu ? [` CPU cores: ${raw.cpu}`] : []), - ...(raw.memory ? [` Memory: ${raw.memory} MB`] : []), + ` Timeout: ${timeoutSec}s (${(timeoutSec / 60).toFixed(1)} min) — billed lifetime`, + ...(typeof cpuCoerced === 'number' ? [` CPU cores: ${cpuCoerced}`] : []), + ...(typeof memoryCoerced === 'number' ? [` Memory: ${memoryCoerced} MB`] : []), ``, - `Create cost: ${fmtUsd(price)} (one-time)`, + `Create cost: ${fmtUsd(price)} (one-time, ${billingLine})`, `Each exec: ${fmtUsd(EXEC_PRICE_USD)}`, `Terminate: ${fmtUsd(TERMINATE_PRICE_USD)}`, ]; @@ -507,9 +632,11 @@ export const modalCreateCapability: CapabilityHandler = { } // Wallet reservation — block over-spend if other in-flight calls hold balance. + // Hold the most we will sign (list price + Base fee), not the list price: + // the quote guard in postWithPayment refuses anything above that ceiling. let reservation: ReservationToken | null = null; try { - reservation = await walletReservation.hold(price); + reservation = await walletReservation.hold(maxSignableUsd(price)); if (!reservation) { return { output: @@ -535,6 +662,7 @@ export const modalCreateCapability: CapabilityHandler = { ctx.abortSignal, 90_000, // 90s — sandbox cold-start can be slow on fresh GPU pulls reservation, + price, ); const latencyMs = Date.now() - callStartedAt; @@ -569,7 +697,7 @@ export const modalCreateCapability: CapabilityHandler = { id: sandboxId, gpu: tier, createdAt: Date.now(), - timeoutSeconds: raw.timeout ?? 300, + timeoutSeconds: timeoutSec, }); // Stats — surface Modal usage in `franklin insights` like other paid tools. @@ -582,8 +710,8 @@ export const modalCreateCapability: CapabilityHandler = { `Sandbox created\n` + `- id: \`${sandboxId}\`\n` + `- tier: ${tier === 'cpu' ? 'CPU only' : `GPU ${tier}`}\n` + - `- timeout: ${raw.timeout ?? 300}s\n` + - `- charged: ${fmtUsd(price)}\n\n` + + `- timeout: ${timeoutSec}s (billed lifetime; terminating early refunds nothing)\n` + + `- charged: ${fmtUsd(price)} (${billingLine})\n\n` + `Next: ModalExec({ sandbox_id: "${sandboxId}", command: ["python","-c","print(1)"] })`, }; } finally { @@ -617,7 +745,7 @@ export const modalExecCapability: CapabilityHandler = { ' exec2: ["sh","-c","tail -50 /workspace/log"] (poll progress, <1s) ' + ' exec3: ["sh","-c","kill -0 $(cat /workspace/pid) 2>/dev/null && echo RUN || echo DONE"] (check live) ' + 'This decouples actual work duration from the per-exec 60s ceiling, but the sandbox ' + - 'itself still dies at 300s wall-clock — total useful work fits in ~5 minutes.', + 'itself still dies when its create `timeout` (billed lifetime, default 300 s, max 24 h) runs out.', input_schema: { type: 'object', properties: { @@ -685,6 +813,7 @@ export const modalExecCapability: CapabilityHandler = { ctx.abortSignal, Math.max(30_000, ((coercedTimeout ?? 300) + 30) * 1000), reservation, + EXEC_PRICE_USD, ); const latencyMs = Date.now() - callStartedAt; @@ -774,6 +903,7 @@ export const modalStatusCapability: CapabilityHandler = { ctx.abortSignal, 30_000, reservation, + STATUS_PRICE_USD, ); const latencyMs = Date.now() - callStartedAt; @@ -829,6 +959,7 @@ export const modalTerminateCapability: CapabilityHandler = { ctx.abortSignal, 30_000, reservation, + TERMINATE_PRICE_USD, ); const latencyMs = Date.now() - callStartedAt; @@ -887,6 +1018,8 @@ export async function terminateAllSessionSandboxes(opts: { abortSignal?: AbortSi 'Franklin Modal sandbox cleanup', ctrl.signal, 20_000, + null, + TERMINATE_PRICE_USD, ); if (res.ok) succeeded++; else failed.push({ id, error: String(res.body.error ?? res.raw.slice(0, 200)) }); diff --git a/test/local.mjs b/test/local.mjs index 17422c60..e1575486 100644 --- a/test/local.mjs +++ b/test/local.mjs @@ -13093,3 +13093,22 @@ test('modalCreatePriceUsd prices size as the gateway does, so the reservation is // Large and short: the flat rate is the floor. assert.equal(modalCreatePriceUsd('T4', 8, 32768, 10), 0.05); }); + +test('modalCreatePriceUsd bills a lifetime above 300 s hourly at any size, CPU included', async () => { + const { modalCreatePriceUsd, modalCreatePricing, MODAL_MAX_TIMEOUT_SECONDS } = await import('../dist/tools/modal.js'); + const close = (a, b) => Math.abs(a - b) < 1e-9; + assert.equal(MODAL_MAX_TIMEOUT_SECONDS, 86400, 'gateway max probed 2026-11-13: 86400 -> 402, 86401 -> 400'); + assert.equal(modalCreatePriceUsd('cpu', undefined, undefined, 300), 0.01, '300 s is still flat'); + assert.ok(close(modalCreatePriceUsd('cpu', undefined, undefined, 301), 0.25 * 301 / 3600), '301 s is hourly'); + assert.ok(close(modalCreatePriceUsd('cpu', undefined, undefined, 3600), 0.25)); + assert.ok(close(modalCreatePriceUsd('cpu', undefined, undefined, 86400), 6), 'sol quotes 6000000 for a 24 h CPU create'); + assert.ok(close(modalCreatePriceUsd('A100', undefined, undefined, 600), 7 / 6)); + assert.ok(close(modalCreatePriceUsd('H100', undefined, undefined, 1200), 3)); + assert.ok(close(modalCreatePriceUsd('H100', undefined, undefined, 86400), 216)); + const h = modalCreatePricing('H100', undefined, undefined, 301); + assert.equal(h.model, 'hourly'); + assert.equal(h.hourlyRateUsd, 9); + assert.ok(close(h.priceUsd, 9 * 301 / 3600)); + // Hourly-eligible (8 cores) but below the flat floor: the floor wins and the model says flat. + assert.deepEqual(modalCreatePricing('T4', 8, undefined, 10), { priceUsd: 0.05, model: 'flat' }); +}); diff --git a/test/reservation.local.mjs b/test/reservation.local.mjs index 5c2a63fd..f447624f 100644 --- a/test/reservation.local.mjs +++ b/test/reservation.local.mjs @@ -257,3 +257,150 @@ test('an untrustworthy read does not prune ambiguous holds', async () => { assert.equal(walletReservation.snapshot().ambiguousCount, 1, 'an ambiguous hold must survive a read that could not see it'); }); + +// ─── Quote ceiling: never sign more than the client expected ────────────── +// Older clients reserved the list price and then signed whatever the 402 +// asked. With create lifetimes up to 24 h a quote can be $216, so the client +// checks the quote against its own price before it signs anything. + +const { modalCreateCapability, maxSignableAtomic } = await import('../dist/tools/modal.js'); + +function res402Quote(amount) { + return new Response(JSON.stringify({ + x402Version: 2, + accepts: [{ scheme: 'exact', network: 'eip155:8453', amount, payTo: '0xabc', asset: '0xusdc', maxTimeoutSeconds: 300 }], + }), { status: 402, headers: { 'content-type': 'application/json' } }); +} + +/** Stub: 402 quoting `amount`, then 200 for the paid retry. Records the bodies sent. */ +function stubQuote(amount, paidBody = { sandbox_id: 'sb_q' }) { + const sent = []; + globalThis.fetch = async (_url, init) => { + sent.push({ body: init.body ? JSON.parse(init.body) : null, paid: !!init.headers?.['PAYMENT-SIGNATURE'] }); + if (sent.length === 1) return res402Quote(amount); + return new Response(JSON.stringify(paidBody), { status: 200 }); + }; + let signed = 0; + _setPaymentSignerForTests(async () => { signed++; return { 'PAYMENT-SIGNATURE': 'sig' }; }); + return { sent, signedCount: () => signed }; +} + +test('maxSignableAtomic = list + Base fee, rounded up, + 1 unit', () => { + assert.equal(maxSignableAtomic(0.01), 11_001n); + assert.equal(maxSignableAtomic(0.10), 101_001n, 'float noise must not add a unit'); + assert.equal(maxSignableAtomic(7 * 600 / 3600), 1_167_668n); + assert.equal(maxSignableAtomic(216), 216_001_001n); +}); + +test('quote above the expected price -> nothing signed, nothing sent, 402 quote_exceeds_expected', async () => { + walletReservation._resetForTests(fixed(1000)); + const { sent, signedCount } = stubQuote('216000000'); // a 24h H100 quote + const r = await postWithPayment('https://gateway.invalid/modal/create', {}, 't', new AbortController().signal, 1000, null, 0.01); + assert.equal(r.ok, false); + assert.equal(r.status, 402); + assert.match(String(r.body.error), /quote_exceeds_expected.*\$216\.000000.*\$0\.011001/); + assert.equal(signedCount(), 0, 'signer must not run'); + assert.equal(sent.length, 1, 'no paid request dispatched'); +}); + +test('quote one unit over the ceiling is refused; at the ceiling it is signed', async () => { + walletReservation._resetForTests(fixed(1000)); + let q = stubQuote('11002'); + let r = await postWithPayment('https://gateway.invalid/x', {}, 't', new AbortController().signal, 1000, null, 0.01); + assert.equal(r.ok, false); + assert.equal(q.signedCount(), 0); + + q = stubQuote('11001'); + r = await postWithPayment('https://gateway.invalid/x', {}, 't', new AbortController().signal, 1000, null, 0.01); + assert.equal(r.ok, true); + assert.equal(q.signedCount(), 1); +}); + +test('live gateway quotes (2026-11-13) pass the ceiling of the client price', async () => { + const { modalCreatePriceUsd } = await import('../dist/tools/modal.js'); + // [tier, timeout, quoted atomic] probed unpaid on sol.blockrun.ai and blockrun.ai. + const live = [ + ['cpu', 300, '10000'], // sol flat + ['cpu', 300, '11000'], // base flat + $0.001 fee + ['cpu', 86400, '6000000'], // sol 24h CPU + ['cpu', 86400, '2401000'], // base 24h CPU (lower rate) + ['A100', 600, '1166667'], // sol hourly, gateway rounds up + ['A100', 600, '667667'], // base hourly + ]; + for (const [tier, timeout, amount] of live) { + walletReservation._resetForTests(fixed(1000)); + const q = stubQuote(amount); + const price = modalCreatePriceUsd(tier, undefined, undefined, timeout); + const r = await postWithPayment('https://gateway.invalid/x', {}, 't', new AbortController().signal, 1000, null, price); + assert.equal(r.ok, true, `${tier} ${timeout}s quote ${amount} must be signed (price ${price})`); + assert.equal(q.signedCount(), 1); + } +}); + +test('an unreadable 402 is refused when a ceiling is set', async () => { + walletReservation._resetForTests(fixed(1000)); + let signed = 0; + _setPaymentSignerForTests(async () => { signed++; return { 'PAYMENT-SIGNATURE': 'sig' }; }); + stubFetch(async () => new Response('{}', { status: 200 })); + const r = await postWithPayment('https://gateway.invalid/x', {}, 't', new AbortController().signal, 1000, null, 0.01); + assert.equal(r.ok, false); + assert.match(String(r.body.error), /no readable amount/); + assert.equal(signed, 0); +}); + +const createCtx = () => ({ abortSignal: new AbortController().signal, workingDir: process.cwd() }); + +test('ModalCreate accepts a 24h lifetime and signs the hourly quote', async () => { + walletReservation._resetForTests(fixed(1000)); + const q = stubQuote('216000000'); + const out = await modalCreateCapability.execute({ gpu: 'H100', timeout: 86400 }, createCtx()); + assert.equal(out.isError, undefined, out.output); + assert.equal(q.sent[0].body.timeout, 86400, 'timeout reaches the gateway unclamped'); + assert.equal(q.signedCount(), 1); + assert.match(out.output, /charged: \$216\.00 \(hourly: \$9\.00\/h x 24\.00 h/); +}); + +test('ModalCreate refuses a quote above its own price for the request', async () => { + walletReservation._resetForTests(fixed(1000)); + // Client prices a default 300 s CPU create at $0.01; a gateway asking $6 is refused. + const q = stubQuote('6000000'); + const out = await modalCreateCapability.execute({}, createCtx()); + assert.equal(out.isError, true); + assert.match(out.output, /ModalCreate failed \(402\): quote_exceeds_expected/); + assert.equal(q.signedCount(), 0); + assert.equal(q.sent.length, 1); +}); + +test('ModalCreate reserves the ceiling of the hourly price, not the flat rate', async () => { + // Balance covers the flat rate many times over but not 1h of A100. + walletReservation._resetForTests(fixed(5)); + const q = stubQuote('7000000'); + const out = await modalCreateCapability.execute({ gpu: 'A100', timeout: 3600 }, createCtx()); + assert.equal(out.isError, true); + assert.match(out.output, /Insufficient .* ~\$7\.00/); + assert.equal(q.sent.length, 0, 'nothing leaves the machine'); +}); + +test('ModalCreate rejects lifetimes outside the gateway range before any request', async () => { + walletReservation._resetForTests(fixed(1000)); + for (const timeout of [86401, 9, 600.5]) { + const q = stubQuote('1'); + const out = await modalCreateCapability.execute({ timeout }, createCtx()); + assert.equal(out.isError, true, `timeout ${timeout}`); + assert.match(out.output, /from 10 to 86400/); + assert.equal(q.sent.length, 0); + } + const q = stubQuote('1'); + const out = await modalCreateCapability.execute({ cpu: 0.1 }, createCtx()); + assert.match(out.output, /cpu must be at least 0\.125/); + assert.equal(q.sent.length, 0); +}); + +test('ModalCreate holds list price + fee allowance, so a balance at exactly the list price is refused', async () => { + walletReservation._resetForTests(fixed(7.0005)); // covers $7.00, not the $7.001001 ceiling + const q = stubQuote('7001000'); + const out = await modalCreateCapability.execute({ gpu: 'A100', timeout: 3600 }, createCtx()); + assert.equal(out.isError, true); + assert.match(out.output, /Insufficient/); + assert.equal(q.sent.length, 0); +}); From 18d80bf72a1acad8e220d7e28bf3ef182ce1e433 Mon Sep 17 00:00:00 2001 From: 1bcMax Date: Fri, 13 Nov 2026 23:28:06 +0000 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20release=20v3.49.28=20=E2=80=94=20M?= =?UTF-8?q?odal=20sandboxes=20up=20to=2024=20h,=20no=20signing=20above=20t?= =?UTF-8?q?he=20expected=20price?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 23 +++++++++++++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15612a13..e53bb744 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,28 @@ # Changelog +## Franklin Agent 3.49.28 — Modal sandboxes can live up to 24 hours, and Franklin never signs more than it expected + +Upgrade if you use Modal sandboxes. + +- **Lifetimes up to 24 h.** ModalCreate refused any `timeout` above 300 s and told + the model the gateway would reject it. The gateways have accepted up to 86400 s + (24 h) for a while (probed 2026-11-13: 86400 is quoted, 86401 is a 400 on + sol.blockrun.ai and blockrun.ai). The client now accepts 10 to 86400 s and + still defaults to 300 s. +- **`timeout` is explained as the billed lifetime.** A create with timeout <= 300 s, + cpu <= 1 and memory <= 1024 MB pays the flat rate. Anything else pays the hourly + rate for the full requested lifetime, upfront, never below the flat rate, with + no refund on early terminate: 600 s on an A100 is $1.17, 24 h on an H100 is $216. + The tool text, the confirm dialog and the result now all say so. +- **Correct hourly price for CPU sandboxes.** A CPU create above 300 s is billed at + $0.25/h. Franklin priced it at the flat $0.01, so a 24 h CPU sandbox would have + reserved $0.01 against a $6.00 charge. +- **No signing above the expected price.** Before it signs, every Modal call now + checks the 402 quote against the price Franklin computed for the request (plus + the Base gateway's $0.001 transaction fee). A higher or unreadable quote is + refused with `quote_exceeds_expected` and nothing is signed or sent. The wallet + reservation holds that same ceiling. + ## Franklin Agent 3.49.27 — Solana payments sign on the gateway's quoted blockhash Requires `@blockrun/llm` 3.26.5 (was 3.19.1 in the lockfile). When a Solana 402 diff --git a/package-lock.json b/package-lock.json index 78f5dfa3..7f4ee415 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@blockrun/franklin", - "version": "3.49.27", + "version": "3.49.28", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@blockrun/franklin", - "version": "3.49.27", + "version": "3.49.28", "license": "Apache-2.0", "workspaces": [ "apps/desktop" diff --git a/package.json b/package.json index 4a43eb2b..d7201a9e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@blockrun/franklin", - "version": "3.49.27", + "version": "3.49.28", "description": "Franklin Agent — The AI agent with a wallet. Spends USDC autonomously to get real work done. Pay per action, no subscriptions.", "type": "module", "exports": {