Skip to content
This repository was archived by the owner on Nov 6, 2026. It is now read-only.
This repository was archived by the owner on Nov 6, 2026. It is now read-only.

[factory] EnvironmentProvider port + Cloudflare ephemeral-environment provider (Workers for Platforms + Containers); K8s/Daytona optional #142

Closed

Description

@khaliqgant

Part of Epic: Factory Verification Environments (#141). This is the core provisioning primitive.

Current implementation status (updated 2026-08-17)

The substrate-agnostic port and Kubernetes provider have landed; the Cloudflare provider and its required real-environment proof have not.

  • Landed on origin/main @ 4f619e9: src/ports/environment.ts:72-97 defines Environment, ProvisionEnvironmentSpec, and the four-method EnvironmentProvider seam. src/environments/kubernetes-provider.ts:107 implements that seam, with provision / status / endpoints / destroy at :136 / :270 / :286 / :312 and its reaper at :546.
  • Not landed: origin/main has no src/environments/cloudflare-provider.ts. Its only cloudflare occurrence under src/ is an incidental target literal in src/environments/verification-stack-deployer.test.ts:275, not a provider.
  • Candidate preserved in PR 142: EnvironmentProvider port + Cloudflare ephemeral-environment provider (Workers for Platforms + Containers); K8s/Daytona optional #194: the branch contains the Cloudflare implementation, but the protected real-Cloudflare job stopped at its first credential guard because CLOUDFLARE_API_TOKEN was absent. The real provision → reachable → destroy → reap path therefore never executed; the passing miniflare path is not the required end-to-end proof.
  • Remaining work tracked here: decide/refresh the Cloudflare design against the current platform, implement or salvage the provider behind the landed port, provision the protected test credential, and produce the real dispatch-namespace/Container lifecycle evidence required below. Do not count the already-landed port or Kubernetes provider as remaining work.

The active factory-cloud hosting implementation is a fixed singleton Factory Container plus Durable Objects. It neither contains nor consumes this per-run dispatch-namespace provider, so it does not complete this issue.

Why

The Factory needs to create and destroy isolated environments on demand to verify features. Follow the repo's existing port + provider seam (src/ports/fleet.ts with RelayFleetClient/InternalFleetClient implementations) so the environment substrate is swappable and testable. The primary provider is Cloudflare (the org's substrate — see #146); K8s/Daytona are optional providers behind the same port for stacks that can't run on Cloudflare.

What

Add an EnvironmentProvider port (src/ports/environment.ts) and a Cloudflare implementation (src/environments/cloudflare-provider.ts):

  • provision(spec): Promise<Environment> — create a Workers for Platforms dispatch namespace for the environment, wire per-env bindings/secrets, and (per the stack descriptor Verification-stack descriptor contract + deployer: repos declare their full stack, Factory brings it up #143) deploy the environment's Workers and Cloudflare Containers into it.
  • Environment carries { id, dispatchNamespace, endpoints, bindings, status, createdAt, ttl }.
  • status(id) — readiness of the namespace's Workers/Containers.
  • endpoints(id) — resolve the environment's reachable URLs (Worker preview URL / Container ingress) so callers (E2E driver, load harness) can hit them.
  • destroy(id) — delete the dispatch namespace + Container instances; idempotent.
  • TTL reaper — mirror src/orchestrator/reaper.ts (identity-checked) to delete environments past TTL or whose owning run died, so namespaces/Containers can't leak.
  • Config schema entry (mirror src/config/schema.ts) for the Cloudflare account/API-token (read via a Resource.*-style secret, not process.env), default limits, TTL.
  • Keep the Cloudflare API/wrangler dependency isolated behind the provider (the port stays substrate-agnostic). Leave a seam for an optional KubernetesEnvironmentProvider / DaytonaEnvironmentProvider implementing the same port later.
  • Export from src/index.ts.

Acceptance

  • EnvironmentProvider port with a Cloudflare provider; unit tests with a faked Cloudflare API/miniflare.
  • Provision creates a dispatch namespace + per-env bindings; destroy is idempotent (namespace + Containers gone).
  • Reaper deletes past-TTL / orphaned environments.
  • Exported from src/index.ts; config schema covers account/token + limits + TTL; port leaves room for K8s/Daytona providers.

✅ End-to-end verification — Factory success criteria (REQUIRED)

"Compiles" is not done. Run the provider against a REAL Cloudflare dispatch namespace and observe an environment come up, be reachable, and get reaped. CI uses miniflare for the fast path and a real dispatch namespace for the E2E.

Run it

npm run build
npm test                                   # unit (faked Cloudflare API / miniflare)

# E2E against a real dispatch namespace (creds from factory-test-infra)
CLOUDFLARE_API_TOKEN=... npx tsx test/e2e/environment-provider.e2e.ts

The E2E must (against real Cloudflare):

  1. provision() → assert the dispatch namespace exists, per-env bindings are set, and status() reports ready.
  2. Deploy a trivial Worker (or Container) into it and assert endpoints() resolves to a reachable URL (curl returns 200).
  3. destroy() → assert the namespace + Container instances are gone.
  4. Provision with a short TTL, run the reaper, assert the environment is deleted without an explicit destroy (proves leaks are cleaned).

Deliverable

  • test/e2e/environment-provider.e2e.ts running provision→reachable→destroy→reap against a real dispatch namespace in CI, one command.
  • Fails if provisioning, endpoint resolution, or reaping is broken (no false green).

Anchor files

  • src/ports/fleet.ts (port pattern) → new src/ports/environment.ts
  • src/fleet/relay-fleet-client.ts (provider-impl pattern) → new src/environments/cloudflare-provider.ts (+ later optional kubernetes-provider.ts / daytona-provider.ts)
  • src/orchestrator/reaper.ts (reaper pattern), src/config/schema.ts, src/index.ts
  • Cloudflare/miniflare posture precedent: relaycast-cloud, cloud's miniflare-sage + test:integration

Activity

  1. added
    factory-ready
    on Jul 20, 2026
  2. changed the title [-]EnvironmentProvider port + Kubernetes ephemeral-environment provider[/-] [+]EnvironmentProvider port + Cloudflare ephemeral-environment provider (Workers for Platforms + Containers); K8s/Daytona optional[/+] on Jul 20, 2026
  3. added
    factory:in-progress
    on Jul 21, 2026
  4. khaliqgant commented on Jul 21, 2026

    @khaliqgant
    MemberAuthor

    Factory human input request

    Agent: ar-142-babysit-factory
    Issue: 142
    Question: Live GitHub shows PR #182 belongs to issue #146 and branch factory/142-agentworkforce-factory-5a9a545c has no PR; should I create a new PR from this branch for issue #142, or which existing PR should I babysit?

  5. agent-relay-code commented on Jul 22, 2026

    @agent-relay-code
    Contributor

    Factory human input request

    Agent: ar-142-babysit-factory
    Issue: 142
    Question: Can a repository or environment owner add the protected CLOUDFLARE_API_TOKEN (and CLOUDFLARE_ACCOUNT_ID if needed) to the factory-test-infra GitHub environment, then rerun the failed Real dispatch namespace and Container job for PR #194 at head 987427a and confirm it passes?

  6. changed the title [-]EnvironmentProvider port + Cloudflare ephemeral-environment provider (Workers for Platforms + Containers); K8s/Daytona optional[/-] [+][factory] EnvironmentProvider port + Cloudflare ephemeral-environment provider (Workers for Platforms + Containers); K8s/Daytona optional[/+] on Aug 7, 2026
  7. khaliqgant commented on Aug 20, 2026

    @khaliqgant
    MemberAuthor

    Factory observed pull request #166 merge and completed this issue.

  8. removed
    factory:in-progress
    on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    factory-ready

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions