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(embed-doctor): bound every post-boot step on one shared deadline

nirholasd2c70d2378f8
diff --git a/api/_lib/embed-doctor.js b/api/_lib/embed-doctor.js
index ceb3089a8..89ebec305 100644
--- a/api/_lib/embed-doctor.js
+++ b/api/_lib/embed-doctor.js
@@ -886,9 +886,10 @@ async function waitForEmbed(page, selector, budgetMs) {
*
* Returns null when the question could not be answered, never a guess.
*/
-async function measurePainted(page, selector) {
+async function measurePainted(page, selector, budgetMs) {
+ const deadline = Date.now() + budgetMs;
try {
- const box = await page.evaluate((sel) => {
+ const box = await withDeadline(page.evaluate((sel) => {
const el = document.querySelector(sel);
if (!el) return null;
const r = el.getBoundingClientRect();
@@ -901,10 +902,15 @@ async function measurePainted(page, selector) {
const h = Math.min(r.bottom, window.innerHeight) - y;
if (w < 4 || h < 4) return null;
return { x, y, width: w, height: h };
- }, selector);
+ }, selector), Math.max(1000, deadline - Date.now()), null);
if (!box) return null;
- const png = await page.screenshot({ type: 'png', clip: box });
+ const png = await withDeadline(
+ page.screenshot({ type: 'png', clip: box }),
+ Math.max(1000, deadline - Date.now()),
+ null,
+ );
+ if (!png) return null;
const { default: sharp } = await import('sharp');
const stats = await sharp(png).stats();
// Three channels of near-zero deviation means every pixel is the same
@@ -985,21 +991,28 @@ function instrument(page) {
return { network, console: consoleMsgs, pageErrors };
}
-/** Everything a diagnosis needs from a booted page, once it has settled. */
-async function harvest(page, { recorders, response, bootMs, platformOrigin, screenshot }) {
- // A page that never yields its main thread (or one the watchdog closed) makes
- // the probe reject. That is "we could not look", not "there is nothing there",
- // and the difference has to survive into the report: honesty rule 1.
- let probeFailed = false;
- const probe = await page.evaluate(inPageProbe, EMBED_SELECTOR, SOURCE_ATTRIBUTES).catch(() => {
- probeFailed = true;
- return {
- scripts: [],
- element: null,
- webgl: { available: null, renderer: null },
- title: '',
- };
- });
+/** Everything a diagnosis needs from a booted page, once it has settled.
+ * `deadlineAt` is the wall-clock instant every remaining step shares, so a
+ * slow page cannot spend the whole request in one of them. */
+async function harvest(page, { recorders, response, bootMs, platformOrigin, screenshot, deadlineAt }) {
+ const left = (max) => Math.min(max, Math.max(1000, deadlineAt - Date.now()));
+
+ // A page that never yields its main thread never runs the probe, and
+ // `page.evaluate` has no deadline of its own. Giving up on it is "we could
+ // not look", not "there is nothing there", and the difference has to survive
+ // into the report: honesty rule 1.
+ const probed = await withDeadline(
+ page.evaluate(inPageProbe, EMBED_SELECTOR, SOURCE_ATTRIBUTES),
+ left(PROBE_MAX_MS),
+ null,
+ );
+ const probeFailed = probed === null;
+ const probe = probed || {
+ scripts: [],
+ element: null,
+ webgl: { available: null, renderer: null },
+ title: '',
+ };
const headers = (() => {
try {
@@ -1011,18 +1024,24 @@ async function harvest(page, { recorders, response, bootMs, platformOrigin, scre
const finalUrl = page.url();
const agentId = probe.element?.attributes?.['agent-id'] || null;
- const agent = agentId ? await resolveAgent(agentId, { platformOrigin }) : null;
+ const agent = agentId
+ ? await resolveAgent(agentId, { platformOrigin, timeoutMs: left(AGENT_LOOKUP_MAX_MS) })
+ : null;
if (probe.element?.canvas?.present) {
- const painted = await measurePainted(page, EMBED_SELECTOR);
+ const painted = await measurePainted(page, EMBED_SELECTOR, left(PAINT_MAX_MS));
probe.element.canvas.blank = painted === null ? null : !painted;
}
+ // A screenshot of a busy WebGL page is the single slowest call here, and it
+ // is the least load-bearing: the report is complete without it.
let shot = null;
if (screenshot) {
- shot = await page
- .screenshot({ type: 'jpeg', quality: 62, encoding: 'base64', fullPage: false })
- .catch(() => null);
+ shot = await withDeadline(
+ page.screenshot({ type: 'jpeg', quality: 62, encoding: 'base64', fullPage: false }),
+ left(SCREENSHOT_MAX_MS),
+ null,
+ );
}
return {
@@ -1046,16 +1065,57 @@ async function harvest(page, { recorders, response, bootMs, platformOrigin, scre
};
}
+// ── Deadlines ─────────────────────────────────────────────────────────────────
+// The caller's `budgetMs` bounds how long the embed is given to paint, and
+// navigation has its own timeout, but every remaining page call (the probe, the
+// paint measurement, the screenshot) had none. On a page that keeps chromium
+// busy those calls dominate: inspecting the three.ws home page took 260 s for a
+// 10 s budget, four times the handler's declared 60 s ceiling, holding a request
+// and a browser open the whole time. So the whole inspection now shares one
+// wall-clock deadline, each step draws from what is left of it, and none of them
+// can be the reason a request outlives the handler.
+
+/** Navigation gets its own timeout; a page that will not load is not worth the
+ * rest of the budget. */
+const NAV_TIMEOUT_MS = 25000;
+/** Per-step ceilings, each also clamped to whatever remains of the deadline. */
+const PROBE_MAX_MS = 8000;
+const AGENT_LOOKUP_MAX_MS = 6000;
+const PAINT_MAX_MS = 8000;
+const SCREENSHOT_MAX_MS = 8000;
+/** What the harvest steps above may add on top of navigation and the boot
+ * budget, and the hard ceiling on one page's total lifetime. The ceiling is the
+ * number that matters: it keeps the worst case inside the handler's
+ * `maxDuration = 60`, whatever budget the caller asked for. */
+const HARVEST_GRACE_MS = 15000;
+const MAX_PAGE_LIFETIME_MS = 50000;
+
+function pageDeadline(budgetMs) {
+ return Math.min(MAX_PAGE_LIFETIME_MS, NAV_TIMEOUT_MS + budgetMs + HARVEST_GRACE_MS);
+}
+
/**
- * Close the page once the whole inspection has outstayed its welcome.
- *
- * Every individual step is already bounded (navigation, boot budget, agent
- * lookup), but the in-page probe is not: a page that pegs its main thread never
- * runs it, and `page.evaluate` has no deadline of its own. Without this, one
- * such page holds a request and a chromium instance open indefinitely. Closing
- * the page rejects whatever is pending, and every caller of a page API here
- * already treats a rejection as "unknown", so the request unwinds into an
- * honest inconclusive report instead of hanging.
+ * Bound a page call that has no timeout of its own, resolving to `fallback`
+ * when the deadline passes or the call rejects. The abandoned call is cancelled
+ * for real when the caller closes the page in its `finally`, so nothing is left
+ * running behind the response.
+ */
+function withDeadline(promise, ms, fallback) {
+ let timer = null;
+ const timeout = new Promise((resolve) => {
+ timer = setTimeout(() => resolve(fallback), ms);
+ if (typeof timer.unref === 'function') timer.unref();
+ });
+ return Promise.race([Promise.resolve(promise).catch(() => fallback), timeout]).finally(() => {
+ if (timer) clearTimeout(timer);
+ });
+}
+
+/**
+ * Last-resort backstop: close the page once its lifetime is spent, so a call
+ * nothing else bounded cannot hold a chromium instance open. Every page API used
+ * here treats a rejection as "unknown", so the request unwinds into an honest
+ * inconclusive report rather than hanging.
*/
function pageWatchdog(page, ms) {
const state = { firedAt: null };
@@ -1068,12 +1128,6 @@ function pageWatchdog(page, ms) {
return state;
}
-/** How long past the caller's boot budget the probe, screenshot and agent
- * lookup are allowed before the watchdog closes the page. Sized from the two
- * bounded calls inside `harvest` (a 6 s agent lookup and a screenshot) with
- * room to spare, and still well inside the handler's 60 s ceiling. */
-const HARVEST_GRACE_MS = 20000;
-
/**
* Diagnose a live URL.
*
@@ -1096,7 +1150,11 @@ export async function collectFromUrl({
const browser = await getBrowser();
const page = await browser.newPage();
const target = { kind: 'url', url };
- const watchdog = pageWatchdog(page, budgetMs + HARVEST_GRACE_MS);
+ const lifetimeMs = pageDeadline(budgetMs);
+ const deadlineAt = Date.now() + lifetimeMs;
+ // The backstop fires a beat after the shared deadline, so the harvest steps
+ // get their full allowance and only a call that ignored it loses its page.
+ const watchdog = pageWatchdog(page, lifetimeMs + 2000);
try {
await page.setUserAgent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36 three.ws-EmbedDoctor/1.0 (+https://three.ws/embed-doctor)',
@@ -1104,7 +1162,10 @@ export async function collectFromUrl({
const recorders = instrument(page);
let response = null;
try {
- response = await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 25000 });
+ response = await page.goto(url, {
+ waitUntil: 'domcontentloaded',
+ timeout: NAV_TIMEOUT_MS,
+ });
} catch (err) {
return {
target,
diff --git a/api/_mcp/tools/trader.js b/api/_mcp/tools/trader.js
index 7b1de9988..ac8edda9c 100644
--- a/api/_mcp/tools/trader.js
+++ b/api/_mcp/tools/trader.js
@@ -1,21 +1,25 @@
-// Trader MCP tools — leaderboard discovery + copy-trading for autonomous agents.
+// Trader MCP tools: leaderboard discovery + copy-trading for autonomous agents.
//
// Four tools that close the autonomous copy-trading loop:
//
-// trader_leaderboard — public. Top agents ranked by composite TraderScore.
-// Each row carries win rate, realized P&L, ROI, and
-// a recommendation on whether to copy them.
+// trader_leaderboard unscoped. Top agents ranked by composite TraderScore.
+// Each row carries win rate, realized P&L, ROI, and
+// a recommendation on whether to copy them.
//
-// trader_profile — public. Full track record for one agent: score,
-// all headline metrics, and the 10 most recent trades.
+// trader_profile unscoped. Full track record for one agent: score,
+// all headline metrics, and the 10 most recent trades.
//
-// copy_subscribe — auth-gated (agents:write). Set up copy-trading: mirror
-// a leader's future entries to your own wallet with your
-// own sizing and risk caps. Non-custodial — we never
-// touch keys.
+// copy_subscribe auth-gated (agents:write). Set up copy-trading: mirror
+// a leader's future entries to your own wallet with your
+// own sizing and risk caps. Non-custodial: we never
+// touch keys.
//
-// copy_status — auth-gated (agents:read). Check the caller's active
-// copy subscriptions and their execution counts.
+// copy_status auth-gated (agents:read). Check the caller's active
+// copy subscriptions and their execution counts.
+//
+// "unscoped" means no OAuth scope is required, not free: /api/mcp still gates
+// every tool here behind an OAuth token or an x402 payment (only
+// getting_started is exempt), so an anonymous caller gets a 402, never data.
import { sql, isDbUnavailableError } from '../../_lib/db.js';
import { limits } from '../../_lib/rate-limit.js';
@@ -118,7 +122,7 @@ export const toolDefs = [
title: 'Top pump.fun traders',
annotations: LIVE,
description:
- "Get the top pump.fun traders on three.ws ranked by composite TraderScore (win rate + P&L + ROI + drawdown). Each row includes score (0–100), verified badge, closed trade count, win rate, realized P&L in SOL and USD, ROI %, max drawdown, and a 'recommendation' field: 'copy' = strong candidate, 'watch' = emerging, 'skip' = unproven. Use trader_profile to get full details before copying. Sort by 'score' (default), 'pnl', 'winrate', or 'roi'. Window: '24h', '7d', '30d' (default), or 'all'.",
+ "Get the top pump.fun traders on three.ws ranked by composite TraderScore (win rate + P&L + ROI + drawdown). Each row includes score (0-100), verified badge, closed trade count, win rate, realized P&L in SOL and USD, ROI %, max drawdown, and a 'recommendation' field: 'copy' = strong candidate, 'watch' = emerging, 'skip' = unproven. Use trader_profile to get full details before copying. Sort by 'score' (default), 'pnl', 'winrate', or 'roi'. Window: '24h', '7d', '30d' (default), or 'all'.",
inputSchema: {
type: 'object',
properties: {
@@ -189,7 +193,7 @@ export const toolDefs = [
},
async handler(args, auth) {
const agentId = (args?.agent_id || '').trim();
- if (!isUuid(agentId)) return mcpErr('Invalid agent_id — must be a UUID (get it from trader_leaderboard).');
+ if (!isUuid(agentId)) return mcpErr('Invalid agent_id: must be a UUID (get it from trader_leaderboard).');
const network = NETWORKS.has(args?.network) ? args.network : 'mainnet';
const window = WINDOWS.has(args?.window) ? args.window : 'all';
@@ -288,7 +292,7 @@ export const toolDefs = [
scope: 'agents:write',
annotations: WRITE,
description:
- "Set up non-custodial copy-trading: mirror a leader agent's future pump.fun entries into your own wallet with your own sizing and risk caps. You supply your wallet address and the sizing rules; we never hold keys. The fan-out cron checks for new leader entries and generates sized intents you act on from /dashboard/copy. Provide leader_agent_id (from trader_leaderboard) and your Solana wallet address. Sizing rules: 'fixed' = exact SOL per trade, 'multiplier' = N× leader size, 'pct_balance' = % of your balance. Always set a per_trade_cap_sol and daily_budget_sol to limit exposure.",
+ "Set up non-custodial copy-trading: mirror a leader agent's future pump.fun entries into your own wallet with your own sizing and risk caps. You supply your wallet address and the sizing rules; we never hold keys. The fan-out cron checks for new leader entries and generates sized intents you act on from /dashboard/copy. Provide leader_agent_id (from trader_leaderboard) and your Solana wallet address. Sizing rules: 'fixed' = exact SOL per trade, 'multiplier' = N× leader size, 'pct_balance' = % of your balance. Always set a per_trade_cap_sol and daily_budget_sol to limit exposure. Calling this again for the same leader and network updates that subscription in place: sizing, caps and perf_fee_bps are replaced by what you send, so anything you omit falls back to its default, while the Oracle-score floor, market-cap band and Telegram route are kept as they were. Clear those three from /dashboard/copy.",
inputSchema: {
type: 'object',
properties: {
@@ -316,10 +320,10 @@ export const toolDefs = [
if (!auth.userId) return mcpErr('Sign in to a three.ws account to set up copy-trading.');
const leaderId = (args?.leader_agent_id || '').trim();
- if (!isUuid(leaderId)) return mcpErr('Invalid leader_agent_id — must be a UUID from trader_leaderboard.');
+ if (!isUuid(leaderId)) return mcpErr('Invalid leader_agent_id: must be a UUID from trader_leaderboard.');
const wallet = (args?.copier_wallet || '').trim();
- if (!BASE58_RE.test(wallet)) return mcpErr('Invalid copier_wallet — must be a base58 Solana address.');
+ if (!BASE58_RE.test(wallet)) return mcpErr('Invalid copier_wallet: must be a base58 Solana address.');
const network = NETWORKS.has(args?.network) ? args.network : 'mainnet';
diff --git a/api/launchpad/publish.js b/api/launchpad/publish.js
index 17c4b2f49..fffa82b90 100644
--- a/api/launchpad/publish.js
+++ b/api/launchpad/publish.js
@@ -1,7 +1,7 @@
// POST /api/launchpad/publish
//
// Persists a Launchpad Studio configuration to launchpad_pages so it can be
-// served at /p/<slug> and edited later. Anonymous publish is allowed — the
+// served at /p/<slug> and edited later. Anonymous publish is allowed: the
// studio is the "Wix of 3D avatars" surface and must work for drive-by
// creators with no account.
//
diff --git a/vite.config.js b/vite.config.js
index fa3e1c6f3..cc55080b1 100644
--- a/vite.config.js
+++ b/vite.config.js
@@ -12,6 +12,22 @@ import {
import { extname, basename, relative, sep } from 'path';
import { VitePWA } from 'vite-plugin-pwa';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
+import { rewriteHead } from './server/seo-head.mjs';
+
+// Dev parity for production's per-request <head> rewrite (server/seo-head.mjs).
+// Shared-shell routes serve one template file for hundreds of paths (/docs/* and
+// /tutorials/*), so in dev every one of them presented the shell's own title,
+// description and canonical. Production rewrites that head from data/pages.json
+// before the response leaves Cloud Run; without this the SEO of a docs page can
+// only be checked after a deploy. Same module, same rules: a page whose shell
+// already owns its canonical is untouched, and any error serves the plain shell.
+function rewriteSeoHead(pathname, html) {
+ try {
+ return rewriteHead(pathname, html) || html;
+ } catch {
+ return html;
+ }
+}
// The build emits two targets controlled by the TARGET env var:
//
@@ -2047,7 +2063,7 @@ support: resolve(__dirname, 'pages/support.html'),
const fileUrl = '/' + rel;
const transformed = await server.transformIndexHtml(fileUrl, html);
res.setHeader('Content-Type', 'text/html; charset=utf-8');
- res.end(transformed);
+ res.end(rewriteSeoHead(path, transformed));
} catch {
next();
}