Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(risk-ack): gate can never brick a money feature — degrade to confirm() on any failure

nrpo6832f4d2e2d1
diff --git a/docs/risk-acknowledgment.md b/docs/risk-acknowledgment.md
index 34a6d05ab..acea75499 100644
--- a/docs/risk-acknowledgment.md
+++ b/docs/risk-acknowledgment.md
@@ -31,9 +31,17 @@ simply doesn't run; everything else on the platform keeps working.
- **Devnet stays friction-free.** Surfaces that know their network only gate
when it isn't `devnet`. Simulation modes (e.g. Oracle arm's simulate mode)
are never gated.
-- **Fails closed.** If the gate can't render (no DOM) or the user never
+- **Fails closed on "no", degrades gracefully on breakage.** If the user never
accepts, `ensureRiskAck()` resolves `false` and the caller must abort the
- money action.
+ money action. But the gate machinery itself can never brick a feature:
+ `ensureRiskAck()` **never rejects**. If `/risk-ack.js` fails to load (broken
+ deploy, blocked request) or the `<dialog>` can't open (unsupported browser),
+ the gate degrades to a native `confirm()` carrying the same core
+ acknowledgment text — the user is still asked, the feature still works. The
+ x402 embed loads the gate lazily for the same reason: a gate failure must
+ never take the payment modal down with it. A failed `POST` to the recording
+ endpoint never blocks an accepted state. Verified in-browser: blocked module,
+ throwing `showModal`, and a 500ing endpoint all leave every feature usable.
## Wiring a new money surface
diff --git a/public/risk-ack.js b/public/risk-ack.js
index e0c48f646..b28e2ba26 100644
--- a/public/risk-ack.js
+++ b/public/risk-ack.js
@@ -276,6 +276,11 @@ export function ensureRiskAck({ context = 'real-funds' } = {}) {
try {
_openDialog({ context, resolve });
} catch {
+ // A half-built dialog may have landed in the DOM before the failure.
+ try {
+ document.querySelectorAll('dialog.risk-ack').forEach((d) => d.remove());
+ document.body.style.overflow = '';
+ } catch { /* detached document — nothing to clean */ }
_inFlight = null;
resolve(fallbackConfirmAck({ context }));
}
diff --git a/public/x402.js b/public/x402.js
index b3d6ec537..38482b78f 100644
--- a/public/x402.js
+++ b/public/x402.js
@@ -34,9 +34,36 @@
const VERSION = '0.1.0';
// Real-funds gate: before the first payment, the user must accept the three.ws
-// Risk Disclosure. Resolved relative to this module's URL, so merchant-site
-// embeds load it from the three.ws origin.
-import { ensureRiskAck } from './risk-ack.js';
+// Risk Disclosure (three.ws/legal/risk). Loaded lazily and failure-tolerant on
+// purpose — this modal is a drop-in embed on merchant sites, and a payment
+// must never brick because the gate module 404'd or was blocked. If risk-ack.js
+// can't load, degrade to a native confirm() with the same core wording,
+// remembered for the page session.
+let _riskAckSessionOk = false;
+
+async function ensureRiskAckSafe(context) {
+ try {
+ // Resolved relative to this module's URL, so merchant-site embeds load it
+ // from the three.ws origin, not the host page's.
+ const m = await import(new URL('./risk-ack.js', import.meta.url).href);
+ return await m.ensureRiskAck({ context });
+ } catch (err) {
+ console.error('[x402] risk-ack unavailable, degrading to confirm()', err);
+ if (_riskAckSessionOk) return true;
+ try {
+ _riskAckSessionOk = globalThis.confirm?.(
+ 'Real funds — risk acknowledgment\n\n' +
+ 'three.ws is experimental software. Losses can be total, fast, and irreversible; ' +
+ 'nothing here is financial advice; and three.ws is not responsible for any losses. ' +
+ 'Full text: three.ws/legal/risk\n\n' +
+ 'Press OK to accept that you use real funds entirely at your own risk, or Cancel to stop.',
+ ) === true;
+ } catch {
+ _riskAckSessionOk = false;
+ }
+ return _riskAckSessionOk;
+ }
+}
// SIWX ("Sign-In-With-X" / CAIP-122) lets a wallet that has already paid for
// an endpoint re-enter it by signing a challenge instead of paying again. The
@@ -1364,7 +1391,7 @@ class CheckoutModal {
}
async runSolana(accept) {
- if (!(await ensureRiskAck({ context: 'x402-pay' }))) { this.close('cancelled'); return; }
+ if (!(await ensureRiskAckSafe('x402-pay'))) { this.close('cancelled'); return; }
this.accept = accept;
this.setPrice(accept);
const provider = detectSolanaProvider();
@@ -1434,7 +1461,7 @@ class CheckoutModal {
}
async runEvm(accept) {
- if (!(await ensureRiskAck({ context: 'x402-pay' }))) { this.close('cancelled'); return; }
+ if (!(await ensureRiskAckSafe('x402-pay'))) { this.close('cancelled'); return; }
this.accept = accept;
this.setPrice(accept);
this.renderProgress('connect', { text: 'Opening browser wallet…' });
diff --git a/src/shared/risk-ack.js b/src/shared/risk-ack.js
index ede24600f..d22ec46f1 100644
--- a/src/shared/risk-ack.js
+++ b/src/shared/risk-ack.js
@@ -11,6 +11,13 @@
*
* import { ensureRiskAck } from '../shared/risk-ack.js';
* if (!(await ensureRiskAck({ context: 'trade' }))) return; // user declined
+ *
+ * Failure policy: this wrapper NEVER rejects. Money features must not brick
+ * because the acknowledgment machinery had a bad day. If /risk-ack.js cannot
+ * be loaded (broken deploy, blocked request), the gate degrades to a native
+ * confirm() carrying the core acceptance text — the user is still asked, the
+ * feature still works. The degraded acceptance is remembered only for this
+ * page session (no version constant to trust without the module).
*/
function _mod() {
@@ -23,20 +30,56 @@ function _mod() {
return import(/* @vite-ignore */ spec);
}
+// Same core wording as RISK_ACK_CONFIRM_TEXT in public/risk-ack.js — inlined
+// because this path only runs when that module failed to load.
+const DEGRADED_CONFIRM_TEXT =
+ 'Real funds — risk acknowledgment\n\n' +
+ 'three.ws is experimental software. Losses can be total, fast, and irreversible; ' +
+ 'autonomous features can trade and pay on your behalf without asking again; nothing here is financial advice; ' +
+ 'and three.ws is not responsible for any losses. Full text: three.ws/legal/risk\n\n' +
+ 'Press OK to accept that you use real funds entirely at your own risk, or Cancel to stop.';
+
+let _degradedSessionAck = false;
+
+function _degradedConfirm() {
+ if (_degradedSessionAck) return true;
+ try {
+ _degradedSessionAck = globalThis.confirm?.(DEGRADED_CONFIRM_TEXT) === true;
+ } catch {
+ _degradedSessionAck = false;
+ }
+ return _degradedSessionAck;
+}
+
/**
* Ensure the user has accepted the current Risk Disclosure — shows the
* acknowledgment dialog if not. Resolves true when accepted, false when
- * declined; the caller must abort the money action on false.
+ * declined; the caller must abort the money action on false. Never rejects.
* @param {{context?: string}} [opts]
* @returns {Promise<boolean>}
*/
export async function ensureRiskAck(opts) {
- const m = await _mod();
- return m.ensureRiskAck(opts);
+ let m;
+ try {
+ m = await _mod();
+ } catch (err) {
+ console.error('[risk-ack] module failed to load, degrading to confirm()', err);
+ return _degradedConfirm();
+ }
+ try {
+ return await m.ensureRiskAck(opts);
+ } catch (err) {
+ console.error('[risk-ack] gate failed, degrading to confirm()', err);
+ return _degradedConfirm();
+ }
}
-/** @returns {Promise<boolean>} whether the current disclosure version is already accepted */
+/** @returns {Promise<boolean>} whether the current disclosure version is already accepted. Never rejects. */
export async function hasRiskAck() {
- const m = await _mod();
- return m.hasRiskAck();
+ try {
+ const m = await _mod();
+ return m.hasRiskAck();
+ } catch {
+ return _degradedSessionAck;
+ }
}
diff --git a/tests/risk-ack.test.js b/tests/risk-ack.test.js
index c9abb524a..c2f31e3d5 100644
--- a/tests/risk-ack.test.js
+++ b/tests/risk-ack.test.js
@@ -9,6 +9,7 @@ import {
isAckCurrent,
hasRiskAck,
ensureRiskAck,
+ fallbackConfirmAck,
RISK_ACK_VERSION,
RISK_ACK_STORAGE_KEY,
} from '../public/risk-ack.js';
@@ -125,3 +126,48 @@ describe('hasRiskAck / ensureRiskAck against storage', () => {
await expect(ensureRiskAck({ context: 'trade' })).resolves.toBe(false);
});
});
+
+describe('fallbackConfirmAck — the degraded path when the dialog cannot render', () => {
+ let originalStorage;
+ let originalConfirm;
+
+ beforeEach(() => {
+ originalStorage = globalThis.localStorage;
+ originalConfirm = globalThis.confirm;
+ });
+
+ afterEach(() => {
+ if (originalStorage === undefined) delete globalThis.localStorage;
+ else globalThis.localStorage = originalStorage;
+ if (originalConfirm === undefined) delete globalThis.confirm;
+ else globalThis.confirm = originalConfirm;
+ });
+
+ it('returns false when confirm() is unavailable — degraded gate still fails closed', () => {
+ delete globalThis.confirm;
+ globalThis.localStorage = fakeStorage();
+ expect(fallbackConfirmAck({ context: 'trade' })).toBe(false);
+ });
+
+ it('accepting via confirm() persists a current, versioned record', () => {
+ globalThis.localStorage = fakeStorage();
+ globalThis.confirm = () => true;
+ expect(fallbackConfirmAck({ context: 'swap' })).toBe(true);
+ const rec = parseAckRecord(globalThis.localStorage.getItem(RISK_ACK_STORAGE_KEY));
+ expect(isAckCurrent(rec)).toBe(true);
+ expect(rec.context).toBe('swap');
+ });
+
+ it('cancelling confirm() returns false and stores nothing', () => {
+ globalThis.localStorage = fakeStorage();
+ globalThis.confirm = () => false;
+ expect(fallbackConfirmAck({ context: 'trade' })).toBe(false);
+ expect(globalThis.localStorage.getItem(RISK_ACK_STORAGE_KEY)).toBeNull();
+ });
+
+ it('a throwing confirm() cannot break the caller', () => {
+ globalThis.localStorage = fakeStorage();
+ globalThis.confirm = () => { throw new Error('blocked'); };
+ expect(fallbackConfirmAck({ context: 'trade' })).toBe(false);
+ });
+});