Skip to content

[P4] Phase 6.7 Cloudflare Sandbox Integration #80

@frankbria

Description

@frankbria

Summary

Integrate Cloudflare as a first-class sandbox backend for Ralph. Cloudflare offers edge compute and container capabilities that can provide globally distributed, low-latency sandbox execution.

Problem Statement

Users may prefer Cloudflare over other sandbox options when they:

  • Already use Cloudflare infrastructure
  • Need edge/global distribution for latency reasons
  • Want integration with Cloudflare's security features
  • Prefer Cloudflare's pricing model
  • Need to stay within Cloudflare's ecosystem for compliance

Cloudflare Compute Options

Cloudflare Containers (Primary Target)

  • Container-based execution
  • Longer-running workloads than Workers
  • Full Linux environment
  • Suitable for Ralph's autonomous execution

Cloudflare Workers (Limited Applicability)

  • V8 isolate-based serverless
  • Short execution limits (may be too restrictive)
  • Could be used for lightweight tasks

Workers for Platforms

  • Multi-tenant execution
  • Could enable "Ralph as a Service" scenarios

Proposed CLI Interface

# Basic Cloudflare sandbox execution
ralph --sandbox cloudflare

# Specify Cloudflare product
ralph --sandbox cloudflare --cf-product containers
ralph --sandbox cloudflare --cf-product workers  # Limited use cases

# Account configuration
ralph --sandbox cloudflare --cf-account-id "abc123"
ralph --sandbox cloudflare --cf-api-token "$CF_API_TOKEN"

# Region/location preferences
ralph --sandbox cloudflare --cf-region "us-east"
ralph --sandbox cloudflare --cf-colo "EWR"  # Specific datacenter

# Resource configuration
ralph --sandbox cloudflare --memory 2g
ralph --sandbox cloudflare --timeout 3600

# Networking
ralph --sandbox cloudflare --cf-network "my-network"  # Cloudflare network

Key Design Questions

  1. Which Cloudflare Product?

    • Containers most likely for full Ralph execution
    • Workers for specific lightweight tasks?
    • How to handle product evolution (CF adds new compute options)?
  2. Authentication

    • API token management
    • Account ID configuration
    • Scoped tokens for security
  3. Container Image

    • Use Ralph's official Docker image?
    • Push to Cloudflare's registry?
    • Or pull from Docker Hub/GHCR?
  4. Networking

    • Cloudflare's network model
    • Access to Claude API from edge
    • Tunnels for accessing user's services?
  5. Persistence

    • Cloudflare containers are ephemeral
    • R2 for artifact storage?
    • Durable Objects for state?
  6. Cost Model

    • Cloudflare has different pricing than E2B
    • CPU-time based? Request-based?
    • Budget controls needed
  7. Edge Distribution

    • Run near user for latency?
    • Run near Claude API for API latency?
    • Let Cloudflare decide?

Cloudflare API Overview

# Conceptual - actual API may differ
# Cloudflare Containers API
curl -X POST "https://api.cloudflare.com/client/v4/accounts/{account_id}/containers" \
  -H "Authorization: Bearer $CF_API_TOKEN" \
  -d '{"image": "ralph:latest", "resources": {"memory": "2g"}}'

# Get container status
curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/containers/{id}"

# Stream logs
curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/containers/{id}/logs"

Implementation Approach

  1. Create lib/sandbox/cloudflare.sh implementing sandbox interface
  2. Implement Cloudflare API client for containers
  3. Handle container image deployment
  4. Implement file sync (upload to container, download artifacts)
  5. Integrate with ralph-monitor for log streaming
  6. Add cost tracking integration
  7. Tests with mocked Cloudflare API

Cloudflare-Specific Considerations

  • Edge-first: Cloudflare's model is edge compute
  • Ephemeral: Containers don't persist by default
  • Global: Can run in multiple locations
  • Integrated Security: WAF, DDoS protection built-in
  • R2 Storage: Natural artifact storage option
  • Evolving Platform: Cloudflare compute is rapidly evolving

Acceptance Criteria

  • --sandbox cloudflare flag launches Ralph in Cloudflare container
  • Cloudflare authentication (API token, account ID)
  • Container creation and lifecycle management
  • File upload to container, artifact download
  • Log streaming to ralph-monitor
  • Cost tracking and budget limits
  • Region/location preferences
  • Graceful cleanup on exit
  • Tests for Cloudflare sandbox execution

Dependencies

Related

Notes

Cloudflare's compute offerings are evolving rapidly. This issue should be reviewed against current Cloudflare capabilities when implementation begins. The Containers product specifically should be validated as GA and suitable for this use case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions