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.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
feat(uploads): hourly sweeper deletes anonymous R2 uploads that exceed their prefix size ceiling
nirholas5803b457925a
diff --git a/CLAUDE.md b/CLAUDE.mdindex 39d86f002..45002be5c 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -180,7 +180,7 @@ Production deploys need owner approval (gate 2) unless the current instruction i- Frontend: vanilla JS modules + Vite (`npm run dev`, port 3000).- 3D: Three.js with glTF/GLB.- Backend touchpoints: serverless-style handlers in `api/`, workers in `workers/`.-- **Production runs on Google Cloud Run, NOT Vercel** (migrated 2026-07-07 after Vercel disabled the deployment). One container ([server/index.mjs](server/index.mjs)) serves the static frontend, the vercel.json route table, and all `api/**` handlers; the crons (150 as of 2026-10-24, see vercel.json) run on Cloud Scheduler. `vercel.json` is a LIVE config file: `server/index.mjs` reads its `routes` array on boot (split at the `{handle:"filesystem"}` marker into pre- and post-filesystem phases), and `scripts/create-gcp-scheduler.mjs` reads its `crons` array to sync Cloud Scheduler jobs. The server itself never reads `crons`. Never delete `vercel.json` as a leftover. GCP builds/deploys must pin the `three-ws-build@` (build) and `three-ws@` (runtime) service accounts; the project's default compute SA was deleted. Deploys: see the "Deploy runbook" section above.+- **Production runs on Google Cloud Run, NOT Vercel** (migrated 2026-07-07 after Vercel disabled the deployment). One container ([server/index.mjs](server/index.mjs)) serves the static frontend, the vercel.json route table, and all `api/**` handlers; the crons (151 as of 2026-10-25, see vercel.json) run on Cloud Scheduler. `vercel.json` is a LIVE config file: `server/index.mjs` reads its `routes` array on boot (split at the `{handle:"filesystem"}` marker into pre- and post-filesystem phases), and `scripts/create-gcp-scheduler.mjs` reads its `crons` array to sync Cloud Scheduler jobs. The server itself never reads `crons`. Never delete `vercel.json` as a leftover. GCP builds/deploys must pin the `three-ws-build@` (build) and `three-ws@` (runtime) service accounts; the project's default compute SA was deleted. Deploys: see the "Deploy runbook" section above.- **Env-var trap:** `vercel env pull` returns EMPTY for secret-type vars. Never trust a Vercel env export as complete. Production env lives on the Cloud Run service (`gcloud run services describe/update three-ws-api --region us-central1`).- Solana/agent SDKs in `sdk/`, `solana-agent-sdk/`, `agent-payments-sdk/`.- Real APIs in use: Pump.fun feed, Solana RPC, OpenAI/Anthropic via worker proxies. Never mock these.diff --git a/README.md b/README.mdindex e8ba6d762..a52e861f1 100644--- a/README.md+++ b/README.md@@ -9996,7 +9996,7 @@ The full OpenAPI 3.1 spec is available at `/openapi.json`. The key API surface iCron schedules are declared in `vercel.json` (still the live cron/route config the server reads) and executed in production by **Google Cloud Scheduler**, which calls each endpoint on its schedule. All cron endpoints are fail-closed — a missing auth token aborts with an error rather than silently skipping (see [Security Hardening](#security-hardening)).-The crons in `vercel.json` (145 entries on 2026-10-23) are routed through a single dynamic handler at [`api/cron/[name].js`](api/cron/[name].js); the `name` segment selects the handler function. Scheduler jobs are provisioned from the `vercel.json` cron list via [scripts/create-gcp-scheduler.mjs](scripts/create-gcp-scheduler.mjs). The table below is a selection of the notable jobs, each schedule quoted verbatim from `vercel.json`; that file is the complete list.+The crons in `vercel.json` (151 entries on 2026-10-25) are routed through a single dynamic handler at [`api/cron/[name].js`](api/cron/[name].js); the `name` segment selects the handler function. Scheduler jobs are provisioned from the `vercel.json` cron list via [scripts/create-gcp-scheduler.mjs](scripts/create-gcp-scheduler.mjs). The table below is a selection of the notable jobs, each schedule quoted verbatim from `vercel.json`; that file is the complete list.| Schedule | Endpoint | Purpose || -------------------- | --------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |diff --git a/api/_lib/anon-upload-limits.js b/api/_lib/anon-upload-limits.jsnew file mode 100644index 000000000..633ed6e44--- /dev/null+++ b/api/_lib/anon-upload-limits.js@@ -0,0 +1,18 @@+// Per-prefix maximum object size for the anonymous presigned PUT handlers.+//+// presignUpload() cannot sign ContentLength (it would add content-length to the+// signed headers and break the browser CORS preflight), so the size a caller+// declares is only checked at presign time. api/cron/anon-upload-sweep.js+// enforces these ceilings after the fact. The handlers import their ceiling from+// here so the presign check and the sweeper can never disagree.+//+// print/uploads/ mirrors MAX_INPUT_BYTES in api/_lib/print/mesh-io.js (a test+// pins the two together; mesh-io is too heavy to import into a sweeper).++export const ANON_UPLOAD_LIMITS = Object.freeze({+ 'capture/uploads/': 512 * 1024 * 1024,+ 'scene/shares/': 200 * 1024 * 1024,+ 'motion-swap/uploads/': 256 * 1024 * 1024,+ 'forge/uploads/': 8 * 1024 * 1024,+ 'print/uploads/': 100 * 1024 * 1024,+});diff --git a/api/_lib/anon-upload-sweep.js b/api/_lib/anon-upload-sweep.jsnew file mode 100644index 000000000..01f25977d--- /dev/null+++ b/api/_lib/anon-upload-sweep.js@@ -0,0 +1,70 @@+// Enforces the declared size ceiling on anonymous presigned PUT uploads.+//+// A presigned PUT does not bind Content-Length, so a caller can declare 1 MB at+// presign time and PUT far more. This walks each anonymous prefix, confirms any+// object over its ceiling with a HEAD, and deletes it.+//+// Presigned URLs live 300s, so an object can only appear within minutes of its+// presign; the sweep therefore only needs to look at recently modified keys+// (LOOKBACK_MS), which keeps a very large prefix cheap to judge.++import { ANON_UPLOAD_LIMITS } from './anon-upload-limits.js';++export const LOOKBACK_MS = 26 * 60 * 60 * 1000;+export const MAX_PAGES_PER_PREFIX = 100;++/**+ * @param {{ listObjectsPage: Function, headObject: Function, deleteObject: Function }} storage+ * @param {{ now?: number, limits?: Record<string, number>, lookbackMs?: number, maxPages?: number }} [opts]+ */+export async function sweepAnonUploads(storage, opts = {}) {+ const now = opts.now ?? Date.now();+ const limits = opts.limits ?? ANON_UPLOAD_LIMITS;+ const lookbackMs = opts.lookbackMs ?? LOOKBACK_MS;+ const maxPages = opts.maxPages ?? MAX_PAGES_PER_PREFIX;+ const summary = { scanned: 0, oversized: 0, deleted: 0, errors: 0, truncated: [], prefixes: {} };++ for (const [prefix, max] of Object.entries(limits)) {+ const stat = { scanned: 0, deleted: 0 };+ summary.prefixes[prefix] = stat;+ let token;+ let pages = 0;+ do {+ let page;+ try {+ page = await storage.listObjectsPage({ prefix, continuationToken: token });+ } catch (err) {+ summary.errors += 1;+ console.warn('[anon-upload-sweep] list failed', { prefix, error: err?.message });+ break;+ }+ pages += 1;+ for (const obj of page.objects) {+ stat.scanned += 1;+ summary.scanned += 1;+ const modified = obj.lastModified ? new Date(obj.lastModified).getTime() : now;+ if (now - modified > lookbackMs) continue;+ if (!(obj.size > max)) continue;+ try {+ const head = await storage.headObject(obj.key);+ const size = Number(head?.ContentLength);+ if (!head || !(size > max)) continue;+ summary.oversized += 1;+ await storage.deleteObject(obj.key);+ stat.deleted += 1;+ summary.deleted += 1;+ console.warn('[anon-upload-sweep] deleted oversized upload', { key: obj.key, size, max });+ } catch (err) {+ summary.errors += 1;+ console.warn('[anon-upload-sweep] check failed', { key: obj.key, error: err?.message });+ }+ }+ token = page.nextToken || undefined;+ if (token && pages >= maxPages) {+ summary.truncated.push(prefix);+ break;+ }+ } while (token);+ }+ return summary;+}diff --git a/api/_lib/r2.js b/api/_lib/r2.jsindex 4b5d118d8..eabec8ddb 100644--- a/api/_lib/r2.js+++ b/api/_lib/r2.js@@ -207,6 +207,27 @@ export async function headObject(key) {}}+// One page of a prefix listing (key, size, last-modified). Used by sweepers that+// must walk a prefix; callers loop on `nextToken` until it is null.+export async function listObjectsPage({ prefix, continuationToken, maxKeys = 1000 }) {+ const out = await r2.send(+ new ListObjectsV2Command({+ Bucket: env.S3_BUCKET,+ Prefix: prefix,+ ContinuationToken: continuationToken,+ MaxKeys: maxKeys,+ }),+ );+ return {+ objects: (out.Contents || []).map((o) => ({+ key: o.Key,+ size: o.Size ?? 0,+ lastModified: o.LastModified ?? null,+ })),+ nextToken: out.IsTruncated ? out.NextContinuationToken || null : null,+ };+}+export async function deleteObject(key) {if (isAbsoluteUrl(key)) return; // externally-hosted asset — nothing in our bucket to deleteawait r2.send(new DeleteObjectCommand({ Bucket: env.S3_BUCKET, Key: key }));diff --git a/api/cron/anon-upload-sweep.js b/api/cron/anon-upload-sweep.jsnew file mode 100644index 000000000..53ea96d05--- /dev/null+++ b/api/cron/anon-upload-sweep.js@@ -0,0 +1,20 @@+// @ts-check+// GET /api/cron/anon-upload-sweep: enforces the declared size on anonymous+// presigned PUT uploads. presignUpload() cannot sign ContentLength (it breaks the+// CORS preflight), so this hourly sweep lists each anonymous prefix, HEAD-checks+// anything over its ceiling (api/_lib/anon-upload-limits.js) and deletes it.++import { json, method, wrapCron } from '../_lib/http.js';+import { requireCron } from '../_lib/cron-auth.js';+import { listObjectsPage, headObject, deleteObject, objectStorageConfigured } from '../_lib/r2.js';+import { sweepAnonUploads } from '../_lib/anon-upload-sweep.js';++export default wrapCron(async (req, res) => {+ if (!method(req, res, ['GET', 'POST'])) return;+ if (!requireCron(req, res)) return;+ if (!objectStorageConfigured()) {+ return json(res, 200, { ok: true, skipped: 'storage_unconfigured' });+ }+ const summary = await sweepAnonUploads({ listObjectsPage, headObject, deleteObject });+ return json(res, 200, { ok: true, ...summary });+});diff --git a/api/forge-upload.js b/api/forge-upload.jsindex 79c3ebae3..2bb4979ec 100644--- a/api/forge-upload.js+++ b/api/forge-upload.js@@ -23,6 +23,7 @@* bucket is refusing us, and the user should come back shortly).*/+import { ANON_UPLOAD_LIMITS } from './_lib/anon-upload-limits.js';import { randomUUID } from 'node:crypto';import { cors, json, method, readJson, wrap, rateLimited } from './_lib/http.js';import { limits, clientIp } from './_lib/rate-limit.js';@@ -36,7 +37,7 @@ const CONTENT_TYPE_EXT = Object.freeze({'image/webp': 'webp',});-const MAX_IMAGE_BYTES = 8 * 1024 * 1024; // matches forge-store's preview copy cap+const MAX_IMAGE_BYTES = ANON_UPLOAD_LIMITS['forge/uploads/']; // matches forge-store's preview copy capfunction clientKeyFrom(req) {const raw = req.headers['x-forge-client'];diff --git a/api/motion-swap.js b/api/motion-swap.jsindex 3eee00c32..7c2813300 100644--- a/api/motion-swap.js+++ b/api/motion-swap.js@@ -26,12 +26,13 @@ import { randomUUID } from 'node:crypto';import { cors, json, method, readJson, wrap, rateLimited } from './_lib/http.js';import { limits, clientIp } from './_lib/rate-limit.js';import { presignUpload, publicUrl } from './_lib/r2.js';+import { ANON_UPLOAD_LIMITS } from './_lib/anon-upload-limits.js';import { createRegenProvider } from './_providers/gcp.js';// Provider job ids are base64url JSON envelopes (mode + task id + worker base// URL — see packJobId in _providers/gcp.js), so they run several hundred chars.const JOB_ID_RE = /^[A-Za-z0-9_-]{20,600}$/;-const MAX_VIDEO_BYTES = 256 * 1024 * 1024;+const MAX_VIDEO_BYTES = ANON_UPLOAD_LIMITS['motion-swap/uploads/'];const MAX_SECONDS = 90;const VIDEO_CONTENT_TYPE_EXT = Object.freeze({diff --git a/api/scene-glb-upload.js b/api/scene-glb-upload.jsindex 72137dd47..db04d3a00 100644--- a/api/scene-glb-upload.js+++ b/api/scene-glb-upload.js@@ -26,6 +26,7 @@ import { randomUUID } from 'node:crypto';import { cors, json, method, readJson, wrap, rateLimited } from './_lib/http.js';import { limits, clientIp } from './_lib/rate-limit.js';import { presignUpload, publicUrl } from './_lib/r2.js';+import { ANON_UPLOAD_LIMITS } from './_lib/anon-upload-limits.js';// Accepted export types → file extension for the storage key. Binary glTF is// the only format Scene Studio's Share action produces; the vendored editor's@@ -37,7 +38,7 @@ const CONTENT_TYPE_EXT = Object.freeze({// A composed scene (multiple imported avatars/props + baked textures) can run// well past a single reference image — cap comfortably above what the editor// can hold in memory client-side without choking a low-end device first.-const MAX_GLB_BYTES = 200 * 1024 * 1024;+const MAX_GLB_BYTES = ANON_UPLOAD_LIMITS['scene/shares/'];function storageConfigured() {return Boolean(diff --git a/api/scene-upload.js b/api/scene-upload.jsindex 27763dde1..865e928f5 100644--- a/api/scene-upload.js+++ b/api/scene-upload.js@@ -19,6 +19,7 @@ import { randomUUID } from 'node:crypto';import { cors, json, method, readJson, wrap, rateLimited } from './_lib/http.js';import { limits, clientIp } from './_lib/rate-limit.js';import { presignUpload, publicUrl } from './_lib/r2.js';+import { ANON_UPLOAD_LIMITS } from './_lib/anon-upload-limits.js';import { hashClient } from './_lib/forge-store.js';// Accepted source-video types → file extension for the storage key.@@ -30,7 +31,7 @@ const CONTENT_TYPE_EXT = Object.freeze({// A walkthrough clip is comfortably under this; anything larger is almost// certainly the wrong input for a streaming reconstruction.-const MAX_VIDEO_BYTES = 512 * 1024 * 1024;+const MAX_VIDEO_BYTES = ANON_UPLOAD_LIMITS['capture/uploads/'];function storageConfigured() {return Boolean(diff --git a/docs/build.md b/docs/build.mdindex 3634eb6c3..660558b12 100644--- a/docs/build.md+++ b/docs/build.md@@ -47,7 +47,7 @@ front-end, the `vercel.json` route table, and every `api/**` handler from sourceprovisioned from the `crons` array in `vercel.json` by[scripts/create-gcp-scheduler.mjs](../scripts/create-gcp-scheduler.mjs); thatarray is the count, so read it there rather than trusting a number quoted here-(145 entries on 2026-10-23). There is **no GitHub Actions CI**. Full runbook: [docs/ops/gcp-production.md](./ops/gcp-production.md).+(151 entries on 2026-10-25). There is **no GitHub Actions CI**. Full runbook: [docs/ops/gcp-production.md](./ops/gcp-production.md).`.npmrc` sets `legacy-peer-deps=true` (npm never auto-installs peers — thereason `audit:deploy` checks the peer tree), and `engines.node` pins Nodediff --git a/docs/ops/cron-auth.md b/docs/ops/cron-auth.mdindex de94704eb..7007a8299 100644--- a/docs/ops/cron-auth.md+++ b/docs/ops/cron-auth.md@@ -138,3 +138,14 @@ every job. The edge keeps accepting it, because it never stopped.- [gcp-production.md](gcp-production.md) for the service, env, and deploy story- `npm run check:cron-drift` (declared vs live jobs) and`npm run audit:cron-liveness` (jobs that exist but never fire)++## Anonymous upload size enforcement++Anonymous presigned PUTs (`capture/uploads/`, `scene/shares/`,+`motion-swap/uploads/`, `forge/uploads/`, `print/uploads/`) cannot sign+`ContentLength` without breaking the browser CORS preflight, so the declared+size is only checked at presign time. `/api/cron/anon-upload-sweep` (hourly,+`api/cron/anon-upload-sweep.js`, logic in `api/_lib/anon-upload-sweep.js`)+lists each prefix, HEAD-confirms objects over their ceiling and deletes them.+Ceilings live in `api/_lib/anon-upload-limits.js`, shared with the presign+handlers. Only objects modified in the last 26 hours are judged.diff --git a/tests/anon-upload-sweep.test.js b/tests/anon-upload-sweep.test.jsnew file mode 100644index 000000000..dec73db77--- /dev/null+++ b/tests/anon-upload-sweep.test.js@@ -0,0 +1,129 @@+import { describe, it, expect } from 'vitest';+import { readFileSync } from 'node:fs';+import { sweepAnonUploads } from '../api/_lib/anon-upload-sweep.js';+import { ANON_UPLOAD_LIMITS } from '../api/_lib/anon-upload-limits.js';++const NOW = Date.parse('2026-10-25T12:00:00Z');+const MB = 1024 * 1024;++// In-memory bucket implementing the same surface as api/_lib/r2.js.+function bucket(objects, pageSize = 2) {+ const store = new Map(objects.map((o) => [o.key, { lastModified: new Date(NOW - 60_000), ...o }]));+ const deleted = [];+ const heads = [];+ return {+ deleted,+ heads,+ store,+ async listObjectsPage({ prefix, continuationToken }) {+ const all = [...store.values()]+ .filter((o) => o.key.startsWith(prefix) && (!continuationToken || o.key > continuationToken))+ .sort((x, y) => (x.key < y.key ? -1 : 1));+ const slice = all.slice(0, pageSize);+ const next = all.length > pageSize ? slice[slice.length - 1].key : null;+ return { objects: slice, nextToken: next };+ },+ async headObject(key) {+ heads.push(key);+ const o = store.get(key);+ return o ? { ContentLength: o.headSize ?? o.size } : null;+ },+ async deleteObject(key) {+ deleted.push(key);+ store.delete(key);+ },+ };+}++describe('sweepAnonUploads', () => {+ it('deletes only objects above their prefix ceiling, across pages', async () => {+ const b = bucket([+ { key: 'forge/uploads/a/ok.png', size: 8 * MB },+ { key: 'forge/uploads/a/big1.png', size: 8 * MB + 1 },+ { key: 'forge/uploads/a/big2.png', size: 90 * MB },+ { key: 'scene/shares/ok.glb', size: 150 * MB },+ { key: 'scene/shares/big.glb', size: 201 * MB },+ { key: 'other/prefix/huge.bin', size: 900 * MB },+ ]);+ const out = await sweepAnonUploads(b, { now: NOW });+ expect(b.deleted.sort()).toEqual([+ 'forge/uploads/a/big1.png',+ 'forge/uploads/a/big2.png',+ 'scene/shares/big.glb',+ ]);+ expect(out.deleted).toBe(3);+ expect(out.errors).toBe(0);+ });++ it('confirms with HEAD and keeps an object whose real size is within the ceiling', async () => {+ const b = bucket([{ key: 'motion-swap/uploads/x.mp4', size: 300 * MB, headSize: 10 * MB }]);+ await sweepAnonUploads(b, { now: NOW });+ expect(b.heads).toEqual(['motion-swap/uploads/x.mp4']);+ expect(b.deleted).toEqual([]);+ });++ it('does not HEAD or delete objects within the ceiling', async () => {+ const b = bucket([{ key: 'print/uploads/a/m.glb', size: 100 * MB }]);+ await sweepAnonUploads(b, { now: NOW });+ expect(b.heads).toEqual([]);+ });++ it('skips objects older than the lookback window', async () => {+ const b = bucket([+ { key: 'forge/uploads/a/old.png', size: 50 * MB, lastModified: new Date(NOW - 48 * 3600_000) },+ ]);+ await sweepAnonUploads(b, { now: NOW });+ expect(b.deleted).toEqual([]);+ });++ it('survives a failing delete and keeps sweeping', async () => {+ const b = bucket([+ { key: 'forge/uploads/a/1.png', size: 20 * MB },+ { key: 'forge/uploads/a/2.png', size: 20 * MB },+ ]);+ const real = b.deleteObject;+ b.deleteObject = async (k) => {+ if (k.endsWith('1.png')) throw new Error('boom');+ return real(k);+ };+ const out = await sweepAnonUploads(b, { now: NOW });+ expect(out.errors).toBe(1);+ expect(b.deleted).toEqual(['forge/uploads/a/2.png']);+ });++ it('reports a prefix it stopped early on', async () => {+ const b = bucket(+ Array.from({ length: 6 }, (_, i) => ({ key: `forge/uploads/a/${i}.png`, size: 1 })),+ 1,+ );+ const out = await sweepAnonUploads(b, { now: NOW, maxPages: 2 });+ expect(out.truncated).toEqual(['forge/uploads/']);+ });+});++describe('anonymous upload ceilings', () => {+ it('print ceiling matches the analyzer input cap', () => {+ const src = readFileSync(new URL('../api/_lib/print/mesh-io.js', import.meta.url), 'utf8');+ const m = src.match(/MAX_INPUT_BYTES = (\d+) \* 1024 \* 1024/);+ expect(Number(m[1]) * 1024 * 1024).toBe(ANON_UPLOAD_LIMITS['print/uploads/']);+ });++ it('every prefix the presign handlers write is covered', () => {+ const files = {+ 'api/scene-upload.js': 'capture/uploads/',+ 'api/scene-glb-upload.js': 'scene/shares/',+ 'api/motion-swap.js': 'motion-swap/uploads/',+ 'api/forge-upload.js': 'forge/uploads/',+ 'api/print/upload.js': 'print/uploads/',+ };+ for (const [f, prefix] of Object.entries(files)) {+ expect(readFileSync(new URL('../' + f, import.meta.url), 'utf8')).toContain('`' + prefix);+ expect(ANON_UPLOAD_LIMITS[prefix]).toBeGreaterThan(0);+ }+ });++ it('the sweep cron is declared in vercel.json', () => {+ const { crons } = JSON.parse(readFileSync(new URL('../vercel.json', import.meta.url), 'utf8'));+ expect(crons.some((c) => c.path === '/api/cron/anon-upload-sweep')).toBe(true);+ });+});diff --git a/vercel.json b/vercel.jsonindex 74787166a..244da3d03 100644--- a/vercel.json+++ b/vercel.json@@ -4262,6 +4262,10 @@"src": "/api/oracle/test-alert","dest": "/api/oracle/test-alert.js"},+ {+ "src": "/api/oracle/telegram-chat",+ "dest": "/api/oracle/telegram-chat.js"+ },{"src": "/api/social/sentiment","dest": "/api/social/sentiment.js"@@ -7210,6 +7214,10 @@"path": "/api/cron/gcp-burn-report","schedule": "0 14 * * *"},+ {+ "path": "/api/cron/anon-upload-sweep",+ "schedule": "17 * * * *"+ },{"path": "/api/cron/auto-rig-sweep","schedule": "*/5 * * * *"