# Build executor (Milestone 2): Containers + Queue + R2

> Product path for Deploy / Retry (default): see [DEVSTACK-HOSTING.md](./DEVSTACK-HOSTING.md). User Cloudflare is optional.

Status 2026-10-22: code complete and tested locally; **not provisioned** (see Blocked).

## Flow
1. `POST /api/projects/import` (or `POST /api/projects/:id/builds`) → `startContainerBuild`: daily cap check, commit pinned from the branch, `build_jobs` row (`executor = cf-container`), `deployments` row (`target = devstack-sites`), message on `BUILD_QUEUE`.
2. Queue consumer (`custom-worker.ts` `queue()`) → `runContainerJob`: `claimWithCap` (max 2 running, conditional UPDATE) → container `build-<jobId>` `POST /build` → NDJSON phases (`installing` → `building` → `uploading`) and log chunks → `createLogStream` (line-buffered, redacted) → `build_events` + `logs/<jobId>.txt` in R2.
3. `GET /artifact` (tar of the output dir) → `artifacts/<jobId>.tar` → files → `SITES` bucket `sites/<deploymentId>/…` → hostname aliases `hosts/<label>.json`.
4. Production builds also move `<slug>.devstack.bid` (promote). Previews get `<slug>-<shortid>.devstack.bid`.
5. Cancel: `requestCancel` → heartbeat watchdog (10 s) → container `/cancel` (process-group SIGTERM, then SIGKILL) + AbortController on the `/build` fetch so a hung stream cannot pin the job. 15-minute outer hard limit → `timed_out` (Thai user message; quota excluded). Inside the container: install ≤8 min, build ≤12 min, and a 4-minute no-log stall kill for OpenNext/esbuild/workerd hangs. **OpenNext:** if `package.json` has Next **16.4.x** and `@opennextjs/cloudflare` **&lt;1.20.12**, the build workspace bumps the adapter (16.4.5+ → **1.20.16**, 16.4.1–4 → **1.20.15**); after `opennextjs-cloudflare` build, the server fails if `preview-props.json` is not inlined; after `wrangler deploy --dry-run`, **`wrangler dev --local`** runs a **GET /** smoke (timeout, fail on 5xx). Cron sweeper unchanged (building phase heartbeat ≤14 min).

## Caps
`MAX_CONCURRENT_BUILDS = 2`, `BUILD_TIMEOUT_MS = 15 min`, `DAILY_BUILDS_PER_USER = 10` (`src/lib/build/caps.ts`), container `max_instances: 2`, `instance_type: standard-3` (2 vCPU / 8 GiB — OpenNext needs more than `standard-1`), queue `max_concurrency: 2`.

## Security
- Container image (`container/Dockerfile`): pinned digests, non-root `node` user, tini, no secrets/ARGs.
- GitHub token: only in the `/build` request body → git `http.extraheader` via `GIT_CONFIG_*` env (never argv, never the build env, never logged). Public repos build without a token. The user OAuth token is used (no GitHub App yet), so it is not short-lived or repo-scoped: see Next.
- Commands run without a shell (argv split, metacharacters rejected). Dangerous env keys (`PATH`, `NODE_OPTIONS`, `LD_PRELOAD`, …) are dropped.
- Egress allowlist on the container: GitHub + npm/yarn/bun registries + nodejs.org.
- Logs: `sanitizeLog` everywhere (verbatim, URL-encoded and base64 forms, longest first, GitHub token patterns); warning when a secret is shorter than 6 chars.
- User sites are served by a separate Worker (`sites-worker/`) with no D1 and no platform secrets; static files only; `nosniff`; no cookies. A hostname belongs to the first project that claims it.
- Recommendation: move user sites to a dedicated registrable domain (for example `devstack.app`) so user JavaScript is not same-site with `devstack.bid`.

## Blocked (needs the owner / infrastructure)
- **Container image**: built and verified locally (see Verified). Pushing it happens only as part of `npm run deploy:release`, which needs a GO.
- **Queue / R2 buckets** not created yet: `wrangler queues create devstack-build-jobs`, `wrangler r2 bucket create devstack-build-artifacts`, `wrangler r2 bucket create devstack-sites`.
- **Wildcard DNS + route** (the API token has no Zone DNS permission):
  - DNS: `AAAA  *  100::  proxied` in zone `devstack.bid` (apex, `www` and all other records unchanged).
  - Worker route: `*.devstack.bid/*` → Worker `devstack-sites` (deploy with `wrangler deploy -c sites-worker/wrangler.jsonc`). The zone already has a route `*.devstack.bid` with no Worker; it must be replaced by (or narrowed to) the route above.
  - Then set `SITES_DNS_READY=1`.

## Deploy configs (hotfix vs full release)
- `wrangler.jsonc` (base, **hotfix**): no Containers, Durable Object, Queue or build R2. `npm run deploy:hotfix` (same as `npm run deploy`) never needs Docker. The executor reports the missing bindings (`missingInfra`) and builds stay queued/blocked.
- `release/build-bindings.jsonc`: the build-executor bindings only. `npm run cf:release-config` merges them over the base into `wrangler.release.jsonc` (generated, committed; `tests/release-config.test.ts` fails if it is stale).
- `npm run deploy:release` = regenerate + `opennextjs-cloudflare deploy -c wrangler.release.jsonc`. Needs Docker plus the Queue and buckets above.
- After the first release deploy (DO migration `v1-build-container` applied), prefer `deploy:release` for later deploys. A hotfix deploy keeps `BuildContainer` exported, so it does not delete the class, but it removes the bindings until the next release deploy.

## Verified
- Docker 26.1.5 on the box (dockerd started by hand, no systemd). `docker build --network host -t devstack-builder:local container/` (the box sandbox only gives apt network on the host network). The real container (runs as `node`, uid 1000) built `sveltejs/template@5b3da65` through `POST /build` → `{"t":"result","ok":true}`, and `GET /artifact` returned a tar with `index.html`, `global.css`, `favicon.png` and `build/bundle.{js,css}`.
- Dry runs: `wrangler deploy --dry-run` passes with no Docker (base); `wrangler deploy -c wrangler.release.jsonc --dry-run` builds the image and lists BUILD_CONTAINER, BUILD_QUEUE, BUILD_ARTIFACTS and SITES.
- Unit tests: `tests/build-executor.test.ts`, `tests/hosting.test.ts`, `tests/security-review.test.ts`.
- Real end-to-end without Docker: `E2E_BUILD=1 npx vitest run tests/build-executor.test.ts` runs `container/build-server.mjs` locally, clones `sveltejs/template@5b3da65`, `npm install`, `npm run build`, and the executor publishes `public/` and serves it by hostname.

## Preview environment variables never hold production secrets

Preview builds (webhook pushes to non-production branches, PRs, and AI chat commits on `devstack/ai`) run the
project's own code: at build time (Next.js SSG / `next build`, Vite plugins, PostCSS/Tailwind/Babel configs) and
again at runtime in the preview Worker. Making build configs read-only for the AI (`READ_ONLY` in
`src/lib/aisite/repo-turn.ts`) lowers the risk but cannot remove it, because application code itself executes.
The safeguard is the environment boundary: **never put production secrets in the `preview` environment**. Use
separate, low-privilege test keys for preview, and keep production keys only in the `production` environment.
