Problem
AI content rating in telescopetest-io runs using ctx.waitUntil() which has a 30-second timeout. If AI models take longer, the rating fails silently and tests stay stuck as UNKNOWN/IN_PROGRESS.
Protecting each asset in R2 (accessed through the endpoint api/tests/{testId}/{filename}) puts a lot of strain on D1 (to fetch the content_rating of one test_id multiple times). This sometimes results in 500 errors (Filmstrip and other assets throw 500s errors intermittently #221 ), and we need a more permanent solution than Telescopetest-io: Fix 500 errors bug from per-asset content_rating check #224 .
Problem 1 Solution
Use Cloudflare Workflows to run AI rating without timeout limits.
What to Change
Create new workflow file src/workflows/ai-content-rating.ts
4 steps: mark in-progress → fetch R2 files → run AI → update DB
Update wrangler.jsonc
Add workflow binding for dev/staging/prod
Update src/pages/api/upload.ts
Remove ctx.waitUntil() block
Replace with env.AI_RATING_WORKFLOW.create({ params: { testId, url } })
Update worker-configuration.d.ts
Add AI_RATING_WORKFLOW: Workflow to Env interface
No changes needed: Database schema, Repository functions, Detail page, Rating API endpoint, Frontend polling logic
How It Works
User uploads → API saves test → triggers workflow → returns immediately
Workflow runs in background (no timeout)
Frontend polls /api/tests/[testId]/rating until done
When workflow finishes, frontend detects rating changed
Problem 2 Solution
Ideal implementation we should serve test results directly from a public R2 bucket though CDN rather than using a worker for loading them from R2.
Which means we do need to implement a private upload bucket where assets go before they are rated (if AI rating is enabled).
Might be a good task to implement using those workflows.
Most docs suggest standalone custom domain, but I can imagine we could map a subpath of the main domain to that bucket.
If we set up separate buckets, we'd be able to do Telescopetest-io: consider scheduling clean up for 'UNSAFE' R2 files #216 much more easily
Problem
ctx.waitUntil()which has a 30-second timeout. If AI models take longer, the rating fails silently and tests stay stuck as UNKNOWN/IN_PROGRESS.content_ratingcheck #224.Problem 1 Solution
Use Cloudflare Workflows to run AI rating without timeout limits.
What to Change
src/workflows/ai-content-rating.tswrangler.jsoncsrc/pages/api/upload.tsctx.waitUntil()blockenv.AI_RATING_WORKFLOW.create({ params: { testId, url } })worker-configuration.d.tsAI_RATING_WORKFLOW: Workflowto Env interfaceHow It Works
/api/tests/[testId]/ratinguntil doneProblem 2 Solution