Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 60 additions & 13 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
# Use Ubuntu 22.04 LTS as base image
# Local development image for Turborepo.
#
# The pinned versions below match the factory image the agents run on
# (apps/factory/agent/lib/factory-image.ts) and the CI setup actions
# (.github/actions/setup-*). `apps/factory` has a test that fails when
# this file drifts from that specification, so update them together.
FROM mcr.microsoft.com/vscode/devcontainers/base:ubuntu-22.04

# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive
ENV RUSTUP_HOME=/usr/local/rustup
ENV CARGO_HOME=/usr/local/cargo
ENV PATH=/usr/local/cargo/bin:$PATH
ENV RUST_VERSION=nightly-2025-06-20
ENV RUST_VERSION=nightly-2026-05-22
ENV NODE_MAJOR=24
ENV PNPM_VERSION=10.28.0
ENV PROTOC_VERSION=26.1
ENV ZIG_VERSION=0.15.2
ENV ZIG_GLOBAL_CACHE_DIR=/usr/local/zig-cache

# Install system dependencies
RUN apt-get update && apt-get install -y \
# Build essentials
build-essential \
pkg-config \
# LLD linker (required on Linux per CONTRIBUTING.md)
# LLD linker (.cargo/config.toml links with -fuse-ld=lld on Linux)
lld \
# Protocol Buffers compiler
protobuf-compiler \
# Cap'n Proto
capnproto \
libcapnp-dev \
Expand All @@ -29,27 +37,64 @@ RUN apt-get update && apt-get install -y \
curl \
wget \
git \
unzip \
xz-utils \
ca-certificates \
gnupg \
lsb-release \
&& rm -rf /var/lib/apt/lists/*
&& rm -rf /var/lib/apt/lists/* \
&& (command -v ld.lld > /dev/null \
|| ln -sf "$(command -v lld)" /usr/local/bin/ld.lld)

# Install Node.js v22 (using NodeSource repository for exact version control)
RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
# Install Node.js (using NodeSource repository for exact version control)
RUN curl -fsSL "https://deb.nodesource.com/setup_${NODE_MAJOR}.x" | bash - \
&& apt-get install -y nodejs \
&& rm -rf /var/lib/apt/lists/*

# Install pnpm globally (specific version as per package.json)
RUN npm install -g pnpm@8.14.0
# Install pnpm globally (the version in the root package.json)
RUN npm install -g "pnpm@${PNPM_VERSION}"

# Install protoc, pinned to the version CI installs
RUN arch="$(uname -m)" \
&& case "$arch" in \
x86_64 | amd64) asset="x86_64" ;; \
aarch64 | arm64) asset="aarch_64" ;; \
*) echo "unsupported architecture: $arch" >&2; exit 1 ;; \
esac \
&& curl --fail --show-error --silent --location --output /tmp/protoc.zip \
"https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-${asset}.zip" \
&& unzip -o -q /tmp/protoc.zip -d /usr/local 'bin/protoc' 'include/*' \
&& chmod 0755 /usr/local/bin/protoc \
&& rm /tmp/protoc.zip

# Install Rust with the specific nightly version and components
# Install Zig, required to build libghostty-vt for the terminal UI
RUN arch="$(uname -m)" \
&& case "$arch" in \
x86_64 | amd64) zig_arch="x86_64" ;; \
aarch64 | arm64) zig_arch="aarch64" ;; \
*) echo "unsupported architecture: $arch" >&2; exit 1 ;; \
esac \
&& curl --fail --show-error --silent --location --output /tmp/zig.tar.xz \
"https://ziglang.org/download/${ZIG_VERSION}/zig-${zig_arch}-linux-${ZIG_VERSION}.tar.xz" \
&& mkdir -p /usr/local/zig \
&& tar -xJf /tmp/zig.tar.xz -C /usr/local/zig --strip-components 1 \
&& ln -sf /usr/local/zig/zig /usr/local/bin/zig \
&& mkdir -p "$ZIG_GLOBAL_CACHE_DIR" \
&& chmod -R a+rwX "$ZIG_GLOBAL_CACHE_DIR" \
&& rm /tmp/zig.tar.xz

# Install Rust with the repository toolchain and components
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- \
--default-toolchain ${RUST_VERSION} \
--profile minimal \
--component rustfmt,clippy \
-y \
&& chmod -R a+w $RUSTUP_HOME $CARGO_HOME

# Install the performance tooling the agent skills reach for
RUN cargo install --locked hyperfine cargo-bloat twiggy \
&& chmod -R a+w $CARGO_HOME

# Create cache directories for better performance
RUN mkdir -p /home/vscode/.cache /usr/local/cargo \
&& chown -R vscode:vscode /home/vscode/.cache /usr/local/cargo
Expand All @@ -65,8 +110,10 @@ RUN node --version \
&& cargo --version \
&& protoc --version \
&& capnp --version \
&& zig version \
&& jq --version \
&& zstd --version \
&& which lld
&& hyperfine --version \
&& which ld.lld

WORKDIR /workspace
WORKDIR /workspace
73 changes: 73 additions & 0 deletions apps/factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,79 @@

The operator page and its API routes rely on Vercel Deployment Protection for access control. Keep Deployment Protection enabled for every deployed environment that exposes them.

## Factory image

Every agent in this app runs against the same sandbox base layer, the
factory image: a Turborepo checkout plus everything `cargo build` and
`pnpm test` need. `agent/lib/factory-image.ts` is the single source of
truth for it — pinned versions, the shell that installs them, and the
fingerprint that decides when a rebuild is required. It installs the
system build toolchain (`build-essential`, `pkg-config`, `lld`, OpenSSL
headers, `jq`, `zstd`), Cap'n Proto, `protoc`, Zig, Node.js, pnpm, the
`rust-toolchain.toml` nightly with `rustfmt` and `clippy`, the workspace
`node_modules`, a warm Cargo registry, and the `hyperfine`,
`cargo-bloat`, and `twiggy` tools the performance skill reaches for. The
last phase verifies each tool is present and writes a version manifest.

`tests/factory-image.test.mjs` fails when those pins drift from
`rust-toolchain.toml`, the root `package.json`, the CI setup actions, or
`.devcontainer/Dockerfile`, so the local dev container and the agents'
sandbox stay on one toolchain.

### Rebuilding on every merge

A push to `main` reaches `POST /api/github/push`, which verifies the
GitHub HMAC signature and starts the `factory-image` workflow. The
workflow creates a build sandbox, detaches the provisioning script inside
it, polls the markers the script writes, snapshots the result, and
publishes the snapshot id as the current image. No GitHub Actions job is
involved. When a published image already exists for the same toolchain
the build boots from it, so a merge build only has to fast-forward the
checkout, refresh dependencies, and recompile.

Rapid merges are resolved in the ledger rather than by racing: claiming a
build cancels every build still in flight, marks it superseded, stops its
workflow run, and deletes its sandbox. Each step re-reads the ledger
before doing work and a build that has lost can neither report progress
nor publish, so only the newest revision on `main` is ever published.
`tests/factory-image-ledger.test.mjs` covers those transitions.

Configure it with:

- A private Vercel Blob store (the ledger lives beside the run
registry).
- `FACTORY_IMAGE_WEBHOOK_SECRET` — the webhook secret. Falls back to
`GITHUB_WEBHOOK_SECRET`, the secret the Eve GitHub channel already
verifies with, when the same GitHub App also subscribes to `push`.
- A webhook delivering `push` to `https://<deployment>/api/github/push`.
Deployment Protection covers that path, so append the automation
bypass token as a query parameter
(`?x-vercel-protection-bypass=<secret>`); the HMAC signature is what
authenticates the delivery. Other events are acknowledged and ignored.

The operator page shows the published image, the toolchain fingerprint,
and recent builds, and can start a build for the current `main` head with
`POST /api/factory-image`.

### How the image is consumed

`agent/sandbox.ts` builds the Eve sandbox template from the same phases.
Eve freezes `revalidationKey` at build time, so the template rotates when
the toolchain fingerprint changes or a newer image is published, and
boots from the published snapshot when one matches. Each session then
fast-forwards its checkout to the current `main`. Harness sessions do the
same through `sandboxConfig` in `agent/lib/harness-agent.ts`, and fall
back to a shallow clone on a stock runtime when no image matches this
deployment's toolchain.

A toolchain change provisions the template from scratch during the next
Vercel build, because Eve prewarms sandbox templates there. Measured
against `vercel/eve:latest`, every phase through verification takes about
two minutes, and the phases that compile Rust are wrapped in timeouts so
one bad upstream release cannot hold a deployment build open. Only the
merge webhook asks for the warm `cargo build`, which runs off the
deployment path inside the build sandbox.

## Harness maintenance

Manual daily-example maintenance runs with `HarnessAgent`. Operators can choose Claude Code, Codex, or OpenCode; each runs in an isolated Vercel Sandbox. Performance runs and scheduled Eve automation are unchanged.
Expand Down
84 changes: 84 additions & 0 deletions apps/factory/agent/lib/factory-image-handoff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Build-time handoff of the published factory image to the Eve sandbox
* backend.
*
* Eve resolves `revalidationKey` asynchronously while it compiles, then
* asks the sandbox definition for its `backend` synchronously when it
* prewarms the template — possibly from a second process in the same
* build. Reading the pointer needs `await`, so the async hook records it
* here and the synchronous factory picks it up.
*
* This is only ever an optimization. Without the file the template is
* built from the base image instead of the published snapshot: slower,
* identical result, because every provisioning phase is idempotent. The
* template's identity comes from `revalidationKey`, never from this file.
*/

import { readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";

export interface FactoryImageHandoff {
readonly commit: string;
readonly snapshotId: string;
}

interface HandoffFile {
readonly commit?: string;
readonly fingerprint: string;
readonly snapshotId?: string;
}

const HANDOFF_PATH = path.join(tmpdir(), "turborepo-factory-image-base.json");

let cached: HandoffFile | null | undefined;

export function writeFactoryImageHandoff(handoff: {
readonly commit?: string;
readonly fingerprint: string;
readonly snapshotId?: string;
}): void {
cached = handoff;
try {
writeFileSync(HANDOFF_PATH, JSON.stringify(handoff), "utf8");
} catch (error) {
console.warn("Could not record the factory image base snapshot.", error);
}
}

/**
* Base snapshot recorded for the current toolchain, or `null` when there
* is none (or when the recorded one belongs to a different toolchain).
*/
export function readFactoryImageHandoff(
fingerprint: string
): FactoryImageHandoff | null {
const file = cached === undefined ? load() : cached;
if (
file === null ||
file.fingerprint !== fingerprint ||
file.snapshotId === undefined ||
file.commit === undefined
) {
return null;
}
return { commit: file.commit, snapshotId: file.snapshotId };
}

function load(): HandoffFile | null {
try {
const value: unknown = JSON.parse(readFileSync(HANDOFF_PATH, "utf8"));
if (
typeof value === "object" &&
value !== null &&
typeof (value as HandoffFile).fingerprint === "string"
) {
cached = value as HandoffFile;
return cached;
}
} catch {
// No handoff was recorded in this build; provision from the base image.
}
cached = null;
return null;
}
Loading
Loading