Skip to content

Telescopetest-io: Use Cloudflare workflows to remove AI content rating timeout and bottleneck in serving 'SAFE' assets #209

@Judyzc

Description

@Judyzc

Problem

  1. 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.
  2. 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

  1. Create new workflow file src/workflows/ai-content-rating.ts
    • 4 steps: mark in-progress → fetch R2 files → run AI → update DB
  2. Update wrangler.jsonc
    • Add workflow binding for dev/staging/prod
  3. Update src/pages/api/upload.ts
    • Remove ctx.waitUntil() block
    • Replace with env.AI_RATING_WORKFLOW.create({ params: { testId, url } })
  4. 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

  1. User uploads → API saves test → triggers workflow → returns immediately
  2. Workflow runs in background (no timeout)
  3. Frontend polls /api/tests/[testId]/rating until done
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    telescopetest-ioticketThis label indicates that internal ticket was created to track it.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions