Multiplayer agent harness for work.
Add E2B as a selectable agent-computer backend (#2370)
Joshua France5a1b948445bc
diff --git a/adrs/e2b-agent-computer.md b/adrs/e2b-agent-computer.mdnew file mode 100644index 000000000..ed81d2703--- /dev/null+++ b/adrs/e2b-agent-computer.md@@ -0,0 +1,27 @@+# E2B as a selectable agent-computer backend++Drew from E2B here. This PR lets an operator pick E2B for agent computers with `"sandbox.backend": "e2b"`, the same way #922 did for Agent37.++Core already has the E2B backend (`SANDBOX_BACKEND=e2b`). The CLI was the missing piece: its config rejects `"sandbox.backend": "e2b"`, so the only way in is setting `env.core.SANDBOX_BACKEND` by hand. The image from `npm run build:e2b-template` is also thin, with no gh, AWS CLI, Claude Code or Codex.++So E2B now publishes two public templates in all three regions (US, EU, APAC), by bare name:++- `qm-sandbox`, the per-scope agent computer. Same tools as your sandbox base image (`fly/Dockerfile`), 8 vCPU and 8 GiB, runs as `user` in `/home/user/workspace`.+- `qm-core`, core on E2B for small teams and trials. `e2b sandbox create qm` starts it with a setup page. It does not replace the Fly or AWS targets.++What the PR adds++- `"sandbox.backend": "e2b"` in the CLI, wired like agent37: type, allowlist on docker, fly and aws, validation message, forwarding `SANDBOX_BACKEND` to core, and the stray Fly settings check. One test, modeled on the agent37 one. The CLI suite passes on Node 24 (624/624).+- `docs/e2b-template.md` now starts with `qm-sandbox:v1.0.0` and the one-hour plan setting.++Backend and default changes++Live use of E2B as the only cloud showed gaps the CLI change alone leaves. Each change and why:++- `DEPLOY_PROVIDER=e2b`: published apps run in their own E2B sandbox, paused when idle and resumed by the next request, so an E2B-only deployment needs no second cloud for apps. It reuses `E2B_API_KEY` and `E2B_TEMPLATE_ID` and the shared app launcher.+- Making an app public is refused (409) unless an apps domain and gate secret are set, on both the share route and the agent's apps tool. Before, it reported "anyone with the link" while signed-out visitors still got 401. `e2b.app` joins the shared domains refused as an apps domain.+- The E2B profile lists gh, aws and x-api as installed only when `E2B_TEMPLATE_ID` is set, since E2B's stock image has none of them. The template now copies in `x-api`.++One thing operators hit: core asks E2B for a 61 minute timeout, so plans capped at one hour reject every create. `E2B_MAX_LIFETIME_SEC=3600` fixes it, and the docs say so.++If you would rather take only this file and do the code yourselves, say so and I will trim the PR.diff --git a/cli/src/config.ts b/cli/src/config.tsindex 41a35d30e..e53775179 100644--- a/cli/src/config.ts+++ b/cli/src/config.ts@@ -46,7 +46,7 @@ export interface PluginEntry {}export interface SandboxConfig {- backend?: "local" | "sprites" | "aws" | "agent37" | "superserve";+ backend?: "local" | "sprites" | "aws" | "agent37" | "superserve" | "e2b";app?: string;image?: string;baseImage?: string;@@ -236,7 +236,7 @@ export function sandboxCoreEnv(sb.backend === "superserve" || effectiveSandboxBackend(config) === "superserve"? effectiveSandboxBackend(config): sb.backend;- if (backend === "agent37" || backend === "superserve") {+ if (backend === "agent37" || backend === "superserve" || backend === "e2b") {env.SANDBOX_BACKEND = backend;return { env, missingSecrets };}@@ -1512,10 +1512,11 @@ function validateSandbox(raw: unknown, path: string, target: Target): SandboxCono["backend"] !== "sprites" &&o["backend"] !== "aws" &&o["backend"] !== "agent37" &&- o["backend"] !== "superserve"+ o["backend"] !== "superserve" &&+ o["backend"] !== "e2b") {throw new CliError(- `${path}: "sandbox.backend" must be "local" (Docker containers on the deployment host), "sprites" (Fly Sprites), "aws" (Lambda MicroVM sandboxes), "agent37", or "superserve" (Superserve sandboxes)`,+ `${path}: "sandbox.backend" must be "local" (Docker containers on the deployment host), "sprites" (Fly Sprites), "aws" (Lambda MicroVM sandboxes), "agent37", "superserve" (Superserve sandboxes), or "e2b" (E2B sandboxes)`,);}out.backend = o["backend"];@@ -1579,7 +1580,7 @@ function validateSandbox(raw: unknown, path: string, target: Target): SandboxCon);}}- if (out.backend === "agent37" || out.backend === "superserve") {+ if (out.backend === "agent37" || out.backend === "superserve" || out.backend === "e2b") {const stray = (["app", "image", "baseImage", "env", "secretEnv"] as const).filter((key) => out[key] !== undefined);if (stray.length) {throw new CliError(diff --git a/cli/src/providers.ts b/cli/src/providers.tsindex 7676dd4db..e70a0f62c 100644--- a/cli/src/providers.ts+++ b/cli/src/providers.ts@@ -5,7 +5,7 @@ export type Target = (typeof HOSTING_PROVIDER_IDS)[number];export const isTarget = (value: unknown): value is Target =>typeof value === "string" && (HOSTING_PROVIDER_IDS as readonly string[]).includes(value);-export type SandboxBackendId = "local" | "sprites" | "aws" | "agent37" | "superserve";+export type SandboxBackendId = "local" | "sprites" | "aws" | "agent37" | "superserve" | "e2b";export interface SandboxBackendPolicy {/** Sandbox backends this hosting target can run. */@@ -16,9 +16,9 @@ export interface SandboxBackendPolicy {/** Keyed by hosting target so adding a target forces a sandbox-backend decision. */export const SANDBOX_BACKEND_POLICY: Record<Target, SandboxBackendPolicy> = {- docker: { allowed: ["local", "sprites", "agent37", "superserve"], requireExplicit: false },- fly: { allowed: ["sprites", "agent37", "superserve"], requireExplicit: false },- aws: { allowed: ["sprites", "aws", "agent37", "superserve"], requireExplicit: true },+ docker: { allowed: ["local", "sprites", "agent37", "superserve", "e2b"], requireExplicit: false },+ fly: { allowed: ["sprites", "agent37", "superserve", "e2b"], requireExplicit: false },+ aws: { allowed: ["sprites", "aws", "agent37", "superserve", "e2b"], requireExplicit: true },};export const targetsAllowingSandboxBackend = (backend: SandboxBackendId): Target[] =>diff --git a/cli/test/config.test.ts b/cli/test/config.test.tsindex ea49f08ed..1286ac466 100644--- a/cli/test/config.test.ts+++ b/cli/test/config.test.ts@@ -895,11 +895,11 @@ test("sandbox shape errors: object, app non-empty string, env string-map, secret{ sandbox: { secretEnv: ["1BAD"] }, rx: /not a valid env var name/ },{sandbox: { backend: "k8s", app: "acme-sandboxes" },- rx: /"sandbox.backend" must be "local".*"sprites".*"aws".*"agent37".*or "superserve"/,+ rx: /"sandbox.backend" must be "local".*"sprites".*"aws".*"agent37".*"superserve".*or "e2b"/,},{sandbox: { backend: "fly", app: "acme-sandboxes" },- rx: /"sandbox.backend" must be "local".*"sprites".*"aws".*"agent37".*or "superserve"/,+ rx: /"sandbox.backend" must be "local".*"sprites".*"aws".*"agent37".*"superserve".*or "e2b"/,},{sandbox: { backend: "aws", app: "acme-sandboxes" },@@ -947,6 +947,21 @@ test("agent37 is a deployment backend on every target and rejects unused Fly set});});+test("e2b is a deployment backend on every target and rejects unused Fly settings", () => {+ for (const target of ["docker", "fly"] as const) {+ withConfig({ target, sandbox: { backend: "e2b" } }, ({ path }) => {+ const { config } = loadConfigAt(path);+ assert.deepEqual(sandboxCoreEnv(config), {+ env: { SANDBOX_BACKEND: "e2b" },+ missingSecrets: [],+ });+ });+ }+ withConfig({ sandbox: { backend: "e2b", app: "unused" } }, ({ path }) => {+ assert.throws(() => loadConfigAt(path), /"sandbox.backend": "e2b" ignores "sandbox.app"/);+ });+});+test("superserve backend requires the agent template in env.core", () => {withConfig({ sandbox: { backend: "superserve" } }, ({ path }) => {assert.throws(() => loadConfigAt(path), /superserve sandbox backend requires env.core.SUPERSERVE_TEMPLATE/);diff --git a/deploy/e2b/e2b.Dockerfile b/deploy/e2b/e2b.Dockerfileindex a00641b3d..a90de85a0 100644--- a/deploy/e2b/e2b.Dockerfile+++ b/deploy/e2b/e2b.Dockerfile@@ -8,6 +8,21 @@ RUN apt-get update && apt-get install -y --no-install-recommends \&& rm -rf /var/lib/apt/lists/* \&& node --version && npm --version+RUN npm install -g --allow-scripts=@anthropic-ai/claude-code \+ @anthropic-ai/[email protected] \+ @openai/[email protected] \+ && claude --version \+ && codex --version++ARG GH_VERSION=2.93.0+ARG GH_SHA256=02d1290eba130e0b896f3709ffff22e1c75a51475ddb70476a85abc6b5807af0+RUN curl -fsSL "https://github.com/cli/cli/releases/download/v${GH_VERSION}/gh_${GH_VERSION}_linux_amd64.tar.gz" -o /tmp/gh.tgz \+ && echo "${GH_SHA256} /tmp/gh.tgz" | sha256sum -c - \+ && tar xzf /tmp/gh.tgz -C /tmp \+ && install "/tmp/gh_${GH_VERSION}_linux_amd64/bin/gh" /usr/local/bin/gh \+ && rm -rf /tmp/gh* \+ && gh --version+ARG AWSCLI_VERSION=2.34.54ARG AWSCLI_SHA256=de278754dec97e0f6e9b4e8167d4bd1a27004c3e56e9c2da10002597f76ca35aRUN curl -fsSL "https://awscli.amazonaws.com/awscli-exe-linux-x86_64-${AWSCLI_VERSION}.zip" -o /tmp/awscliv2.zip \@@ -17,5 +32,15 @@ RUN curl -fsSL "https://awscli.amazonaws.com/awscli-exe-linux-x86_64-${AWSCLI_VE&& rm -rf /tmp/aws /tmp/awscliv2.zip \&& aws --version+RUN python3 -m venv /opt/agent-venv \+ && /opt/agent-venv/bin/pip install --no-cache-dir --upgrade pip+ENV PATH="/opt/agent-venv/bin:${PATH}"+ENV VIRTUAL_ENV=/opt/agent-venv++COPY fly/tools/x-api /usr/local/bin/x-api+RUN chmod +x /usr/local/bin/x-api+RUN id -u user >/dev/null 2>&1 || useradd --create-home --user-group --shell /bin/bash user-RUN mkdir -p /home/user/workspace && chown -R user:user /home/user+RUN chown -R user:user /opt/agent-venv \+ && mkdir -p /home/user/workspace /home/user/.config /home/user/.cache /home/user/.local/bin \+ && chown -R user:user /home/userdiff --git a/docs/e2b-template.md b/docs/e2b-template.mdindex f8b3595eb..5c6cc20cc 100644--- a/docs/e2b-template.md+++ b/docs/e2b-template.md@@ -1,5 +1,34 @@# E2B sandbox template+## Public template++E2B publishes a ready agent-computer template, `qm-sandbox`. It has the same tools as+the sandbox base image (`fly/Dockerfile`): Claude Code, Codex, GitHub CLI, AWS CLI v2, the+Python venv at `/opt/agent-venv` and `x-api`. Each sandbox gets 8 vCPU and 8 GiB where the+plan allows.++```jsonc+"sandbox": { "backend": "e2b" },+"env": { "core": { "E2B_TEMPLATE_ID": "qm-sandbox:v1.0.0" } }+```++Set `E2B_API_KEY` with `qm secrets set`. Pin a version tag, so a new release does not+change the image of new scopes.++With `E2B_TEMPLATE_ID` set, the agent computer's profile lists gh, aws and `x-api` as+installed. Without it, core uses E2B's stock `base` image and lists them as missing.++On E2B plans capped at one hour, also set `E2B_MAX_LIFETIME_SEC=3600` in `env.core`.+Core otherwise asks for a 61 minute sandbox timeout, and those plans reject every create+with `400: Timeout cannot be greater than 1 hours`. The setting also caps a command at 59+minutes.++To run core on E2B as well, `e2b sandbox create qm` starts it from the `qm-core` template+with a setup page for the E2B key, org, admin and model keys. See+[E2B's docs](https://e2b.dev/docs) for the CLI.++## Build your own+E2B stopped accepting V1 template builds (`e2b template build` against a bareDockerfile with an access token) on 2026-08-01. `deploy/e2b/e2b.Dockerfile` is stillthe single source for the sandbox image, but it is now fed to the Template SDK, which@@ -45,3 +74,62 @@ Rebuilding from unchanged inputs uses E2B's build cache. Existing sandboxes keeptemplate they were created from; a paused scope sandbox only picks up a new templateafter it is destroyed or restored from a recovery snapshot, which is itself a templatederived from the old image.++## Egress++E2B can filter a sandbox's outbound traffic itself (`network.allowOut` / `denyOut`), but+those rules cannot carry QM's egress policy, so QM keeps its own proxy as the enforcement+point and uses E2B's rules only to make that proxy unavoidable:++- QM policies are deny lists over open egress as well as allow lists. E2B accepts host+ names only in `allowOut`; `denyOut` takes IPs and CIDR ranges only, and allow rules+ always win over deny rules, so "allow `github.com` but not `gist.github.com`" has no+ E2B form.+- E2B matches host names only on ports 80 and 443 (HTTP `Host` and TLS SNI). Other+ ports, such as SSH to a git host, fall back to IP rules.+- QM resolves the policy per turn and per speaker and signs it into the turn's egress+ token. E2B rules are per sandbox, and one scope's computer serves concurrent turns from+ different people.+- The proxy also writes the egress audit log and checks the turn token; E2B's firewall+ does neither.++Set `E2B_EGRESS_PROXY_URL` to a QM egress proxy reachable on port 443, for example+`deploy/egress-proxy-pub` (TLS on 443 in front of the proxy's port 48080). Core then+creates every agent computer with `allowOut: [<proxy host>]` and `denyOut: ["0.0.0.0/0"]`+and points the computer's proxy settings at it with the turn's token. Without it, core+warns at boot and computers have open egress.++The proxy cannot run inside a core that is itself an E2B sandbox: E2B's public URL edge+forwards HTTP but answers `CONNECT` with 405, so sibling sandboxes cannot use it as a+forward proxy. Run it where it gets a real TCP listener on 443, and give it the same+`CORE_SIGNING_SECRET` as core so it can verify turn tokens.++See [Internet access](https://e2b.dev/docs/network/internet-access).++## Published apps++`DEPLOY_PROVIDER=e2b` runs each published app in its own E2B sandbox, created from+`E2B_TEMPLATE_ID` so an app gets the same machine as the agent that built it. It uses+the same `E2B_API_KEY`.++- The sandbox is created with `lifecycle: { onTimeout: "pause", autoResume: true }`. It+ pauses ten minutes after it starts or resumes, and the next request to its URL resumes+ it with memory intact, so the app process keeps running across pauses. E2B keeps paused+ sandboxes until they are killed.+- Its URL is `https://<port>-<sandbox>.<e2b domain>`, created with+ `network.allowPublicTraffic: false`. Only core holds the sandbox's traffic access token+ and sends it as `e2b-traffic-access-token` when it proxies a request, so viewers still+ go through QM's sign-in and sharing at `/d/<app>/` or the apps domain.+- A new version replaces `/home/user/app` in the running sandbox and restarts the+ entrypoint. `/home/user/data` (`$DATA_DIR`) survives redeploys and pauses; deleting the+ app kills the sandbox and its data.+- An always-on app is created with a 24 hour timeout. E2B still pauses it when that+ window ends, and the next request resumes it.+- Apps served at `/d/<app>/` always require sign-in, so "anyone with the link" sharing+ needs an apps domain (`DEPLOY_APPS_DOMAIN` plus `AWS_DEPLOY_GATE_SECRET`) with a+ wildcard DNS record routed to the portal. `e2b.app` cannot carry per-app subdomains, so+ a core running inside an E2B sandbox needs a domain you control in front of it. Without+ one, a request to make an app public is refused.++See [Auto-resume](https://e2b.dev/docs/sandbox/auto-resume) and+[Restricting public access](https://e2b.dev/docs/network/restrict-public-access).diff --git a/scripts/e2b-build-template.ts b/scripts/e2b-build-template.tsindex 19a415f6c..203f9dc24 100644--- a/scripts/e2b-build-template.ts+++ b/scripts/e2b-build-template.ts@@ -1,6 +1,7 @@import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";+import { fileURLToPath } from "node:url";import { Template, defaultBuildLogger, type CopyItem } from "e2b";import { loadDeploymentLayer } from "../src/deployment/load-layer.ts";@@ -18,7 +19,8 @@ const memoryMB = positiveInt("E2B_TEMPLATE_MEMORY_MB");const resources = { ...(cpuCount ? { cpuCount } : {}), ...(memoryMB ? { memoryMB } : {}) };const dockerfile = readFileSync(new URL("../deploy/e2b/e2b.Dockerfile", import.meta.url), "utf8");-const base = await Template.build(Template().fromDockerfile(dockerfile), baseName, {+const repoRoot = fileURLToPath(new URL("..", import.meta.url));+const base = await Template.build(Template({ fileContextPath: repoRoot }).fromDockerfile(dockerfile), baseName, {...resources,onBuildLogs: defaultBuildLogger(),});diff --git a/src/api/routes/deployments.ts b/src/api/routes/deployments.tsindex 1e80adc84..02ad38f85 100644--- a/src/api/routes/deployments.ts+++ b/src/api/routes/deployments.ts@@ -1,4 +1,5 @@import { EXTERNAL_APP_SHARING_OFF, externalAppSharingAllowed } from "../../feature-flags.ts";+import { PublicLinksUnavailableError } from "../../deploy/deploy-service.ts";import { SocksProxyAgent } from "socks-proxy-agent";import { request as httpRequest } from "node:http";import { request as httpsRequest } from "node:https";@@ -1630,6 +1631,9 @@ export async function shareDeployment(ctx: ApiCtx): Promise<void> {} else if (msg === EXTERNAL_APP_SHARING_OFF) {status = 403;error = "external_sharing_disabled";+ } else if (e instanceof PublicLinksUnavailableError) {+ status = 409;+ error = "public_links_unavailable";}return sendJson(res, status, { error, message: msg });}diff --git a/src/config.ts b/src/config.tsindex 12b42a7ae..11d90ba19 100644--- a/src/config.ts+++ b/src/config.ts@@ -78,7 +78,7 @@ export interface Config {legacySandboxScopeDefaults?: LegacySandboxScopeDefaults;sandboxBackend: SandboxBackendName;sandboxSecondaryBackend?: SandboxBackendName;- deployProvider: "docker" | "aws" | "fly" | "porter";+ deployProvider: "docker" | "aws" | "fly" | "porter" | "e2b";egressServiceHosts?: string[];brandingDefault?: OrgBranding;modelId?: string;@@ -814,6 +814,7 @@ const SHARED_PLATFORM_SUFFIXES = ["github.io","pages.dev","workers.dev",+ "e2b.app",];function sharedPlatformSuffixOf(domain: string): string | undefined {@@ -1414,10 +1415,11 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env): Config {deployProvider !== "aws" &&deployProvider !== "docker" &&deployProvider !== "fly" &&- deployProvider !== "porter"+ deployProvider !== "porter" &&+ deployProvider !== "e2b") {throw new Error(- `DEPLOY_PROVIDER=${JSON.stringify(deployProvider)} is not recognized (expected aws, docker, fly, or porter)`,+ `DEPLOY_PROVIDER=${JSON.stringify(deployProvider)} is not recognized (expected aws, docker, fly, porter, or e2b)`,);}let runStore: "memory" | "postgres" = env.SESSION_STORE === "postgres" ? "postgres" : "memory";diff --git a/src/deploy/aws-deploy-provider.ts b/src/deploy/aws-deploy-provider.tsindex 76896df24..01b87d0a3 100644--- a/src/deploy/aws-deploy-provider.ts+++ b/src/deploy/aws-deploy-provider.ts@@ -9,14 +9,16 @@ import { createMemoryMap, type DurableMap } from "../persistence/durable-map.ts"import { createNoopAdvisoryLock, type AdvisoryLock } from "../persistence/advisory-lock.ts";import { createKeyedQueue, sleep } from "../util/async.ts";import { shq } from "../util/shell.ts";-import { swallow } from "../util/errors.ts";+import { execFailure, swallow } from "../util/errors.ts";import {- APP_PID_PATH,+ appEnv,appGone,appLaunch,appLogs,appReporter,+ exportEnv,listeningOn,+ STOP_APP,waitAppReady,type AppErrorSink,} from "./shared-deploy-provider.ts";@@ -439,7 +441,7 @@ export function createAwsDeployProvider(opts: AwsDeployProviderOptions): DeployP"git clean -ffd -e .qm-ready",].join(" && ");const r = await execRaw(id, endpoint, script, 180);- if (r.code !== 0) throw new Error(`aws deploy git checkout failed: ${r.stderr.slice(0, 300)}`);+ if (r.code !== 0) throw execFailure("aws deploy git checkout", r);}async function writeFiles(@@ -452,16 +454,11 @@ export function createAwsDeployProvider(opts: AwsDeployProviderOptions): DeployP}async function startApp(d: Deployment, id: string, endpoint: string, version: DeploymentVersion): Promise<void> {- const envExports = Object.entries(version.env ?? {})- .filter(([k]) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(k))- .map(([k, v]) => `export ${k}=${shq(v)}`)- .join("; ");+ const envExports = exportEnv(appEnv(version.env));const inner = `cd ${shq(APP_DIR)}; ${version.entrypoint}`;const script = [listeningOn(appPort),- `old=$(cat ${shq(APP_PID_PATH)} 2>/dev/null || true)`,- `if [ -n "$old" ]; then kill -TERM "-$old" 2>/dev/null || true; sleep 0.5; kill -KILL "-$old" 2>/dev/null || true; fi`,- `rm -f ${shq(APP_PID_PATH)}`,+ STOP_APP,`for i in $(seq 1 ${APP_PORT_RELEASE_TICKS}); do listening || break; sleep 0.25; done`,`listening && exit ${EXIT_APP_PORT_HELD}`,...(s3 ? [`mkdir -p ${shq(DATA_DIR)}`] : []),@@ -477,7 +474,7 @@ export function createAwsDeployProvider(opts: AwsDeployProviderOptions): DeployP`port ${appPort} is still held by a process this body cannot account for, so the new version was not started`,);}- if (r.code !== 0) throw new Error(`aws deploy app start failed: ${r.stderr.slice(0, 300)}`);+ if (r.code !== 0) throw execFailure("aws deploy app start", r);await waitAppReady((script, timeoutSec) => execRaw(id, endpoint, script, timeoutSec), {appPort,windowSec: APP_READY_WINDOW_SEC,diff --git a/src/deploy/deploy-service.ts b/src/deploy/deploy-service.tsindex 937e2a648..9a91f6e88 100644--- a/src/deploy/deploy-service.ts+++ b/src/deploy/deploy-service.ts@@ -115,9 +115,20 @@ export interface DeploymentGrantee {permission: Permission;}+const PUBLIC_LINKS_UNAVAILABLE =+ "This instance serves apps at /d/<app>/ on its own domain, which always requires sign-in, so a public link would still ask visitors to sign in. Public links need an apps domain: set DEPLOY_APPS_DOMAIN to a domain with a wildcard DNS record routed to the portal, and AWS_DEPLOY_GATE_SECRET.";++export class PublicLinksUnavailableError extends Error {+ constructor() {+ super(PUBLIC_LINKS_UNAVAILABLE);+ this.name = "PublicLinksUnavailableError";+ }+}+export interface DeployServiceDeps {deliveries?: DeliveryStore;deployAppsDomain?: string;+ publicLinksAvailable?: boolean;publicWebUrl?: string;deployStore: DeployStore;provider: DeployProvider;@@ -533,6 +544,7 @@ export function createDeployService(deps: DeployServiceDeps): DeployService {throw new Error(`only the owner can change who can reach "${d.name ?? d.id}"`);}if (isPublic && !(await externalSharingAllowed(d.ownerScopeId))) throw new Error(EXTERNAL_APP_SHARING_OFF);+ if (isPublic && deps.publicLinksAvailable === false) throw new PublicLinksUnavailableError();await deps.deployStore.setPublic(d.id, isPublic);deps.auditLog.record({at: Date.now(),diff --git a/src/deploy/e2b-deploy-provider.ts b/src/deploy/e2b-deploy-provider.tsnew file mode 100644index 000000000..046a2e3ab--- /dev/null+++ b/src/deploy/e2b-deploy-provider.ts@@ -0,0 +1,210 @@+import { randomUUID } from "node:crypto";+import { LRUCache } from "lru-cache";+import type { Deployment, DeploymentVersion } from "./deploy-store.ts";+import type { DeployEndpoint, DeployProvider } from "./deploy-provider.ts";+import {+ appEnv,+ appLaunch,+ appLogs,+ appReporter,+ bundleEntries,+ exportEnv,+ extractBundle,+ STOP_APP,+ waitAppReady,+ type AppErrorSink,+} from "./shared-deploy-provider.ts";+import { makeTar } from "../sandbox/tar.ts";+import { E2bSandboxGoneError, runE2bScript, type E2bClient, type E2bSession } from "../sandbox/e2b-client.ts";+import { createMemoryMap, type DurableMap } from "../persistence/durable-map.ts";+import { createNoopAdvisoryLock, type AdvisoryLock } from "../persistence/advisory-lock.ts";+import { createKeyedQueue } from "../util/async.ts";+import { shq } from "../util/shell.ts";+import { execFailure, swallow } from "../util/errors.ts";++const HOME_DIR = "/home/user";+const APP_DIR = `${HOME_DIR}/app`;+const DATA_DIR = `${HOME_DIR}/data`;+const APP_PORT_DEFAULT = 8080;+const ENDPOINT_PORT = 443;+const READY_WINDOW_SEC_DEFAULT = 60;+export const E2B_DEPLOY_IDLE_TIMEOUT_SEC = 600;+const ALWAYS_ON_TIMEOUT_MS = 24 * 3600_000;+const RESOLVE_CACHE_MS_DEFAULT = 15_000;+const RESOLVE_CACHE_MAX = 500;+const DEPLOY_TAG = "qm-deploy";+const TRAFFIC_TOKEN_HEADER = "e2b-traffic-access-token";++export interface StoredE2bDeployBody {+ deploymentId: string;+ sandboxId: string;+ host: string;+ trafficAccessToken?: string;+ createdAtMs: number;+}++export interface E2bDeployProviderOptions {+ client: E2bClient;+ namePrefix?: string;+ appPort?: number;+ readyWindowSec?: number;+ idleTimeoutSec?: number;+ resolveCacheMs?: number;+ store?: DurableMap<StoredE2bDeployBody>;+ advisoryLock?: AdvisoryLock;+ onError?: AppErrorSink;+}++export function createE2bDeployProvider(opts: E2bDeployProviderOptions): DeployProvider {+ const client = opts.client;+ const prefix = opts.namePrefix ?? "qm";+ const appPort = opts.appPort ?? APP_PORT_DEFAULT;+ const readyWindowSec = opts.readyWindowSec ?? READY_WINDOW_SEC_DEFAULT;+ const idleTimeoutMs = (opts.idleTimeoutSec ?? E2B_DEPLOY_IDLE_TIMEOUT_SEC) * 1000;+ const resolveCacheMs = opts.resolveCacheMs ?? RESOLVE_CACHE_MS_DEFAULT;+ const store = opts.store ?? createMemoryMap<StoredE2bDeployBody>();+ const advisoryLock = opts.advisoryLock ?? createNoopAdvisoryLock();+ const queue = createKeyedQueue<string>();+ const resolveCache = new LRUCache<string, DeployEndpoint>({+ max: RESOLVE_CACHE_MAX,+ ttl: Math.max(1, resolveCacheMs),+ });++ const serialized = <T>(d: Deployment, fn: () => Promise<T>): Promise<T> =>+ queue(d.id, () => advisoryLock.withLock(`e2b-deploy:${d.id}`, fn));++ const endpointOf = (body: Pick<StoredE2bDeployBody, "host" | "trafficAccessToken">): DeployEndpoint => ({+ host: body.host,+ port: ENDPOINT_PORT,+ tls: true,+ ...(body.trafficAccessToken ? { proxyHeaders: { [TRAFFIC_TOKEN_HEADER]: body.trafficAccessToken } } : {}),+ });++ async function unpackTree(session: E2bSession, guestDir: string, dir: string): Promise<void> {+ const entries = await bundleEntries(dir);+ if (!entries.length) return;+ const tar = `${HOME_DIR}/.qm-bundle-${randomUUID()}.tar`;+ await session.writeFileBytes(tar, await makeTar(entries));+ const r = await runE2bScript(session, extractBundle(tar, guestDir), 120);+ if (r.code !== 0) throw execFailure(`e2b deploy: unpacking into ${guestDir}`, r);+ }++ async function install(d: Deployment, session: E2bSession, version: DeploymentVersion): Promise<void> {+ const stopped = await runE2bScript(+ session,+ `${STOP_APP}; rm -rf ${shq(APP_DIR)}; mkdir -p ${shq(APP_DIR)} ${shq(DATA_DIR)}`,+ 60,+ );+ if (stopped.code !== 0) throw execFailure("e2b deploy: stopping the previous version", stopped);+ await unpackTree(session, APP_DIR, version.snapshotDir);+ if (version.homeDir) await unpackTree(session, HOME_DIR, version.homeDir);+ const env = appEnv(version.env, { HOME: HOME_DIR, PORT: String(appPort), DATA_DIR });+ const inner = `${exportEnv(env)}; cd ${shq(APP_DIR)}; ${version.entrypoint}`;+ const started = await runE2bScript(session, appLaunch(inner, appPort), 60);+ if (started.code !== 0) throw execFailure("e2b deploy: app start", started);+ await waitAppReady((script, timeoutSec) => runE2bScript(session, script, timeoutSec), {+ appPort,+ windowSec: readyWindowSec,+ report: appReporter(opts.onError, d, version.env),+ });+ }++ const forget = (d: Deployment): Promise<void> =>+ store.delete(d.id).catch((err) => swallow("e2b-deploy: drop dead body pointer", err));++ async function connectStored(d: Deployment): Promise<{ stored: StoredE2bDeployBody; session: E2bSession } | null> {+ const stored = await store.get(d.id);+ if (!stored) return null;+ try {+ return { stored, session: await client.connect(stored.sandboxId) };+ } catch (e) {+ if (!(e instanceof E2bSandboxGoneError)) throw e;+ await forget(d);+ return null;+ }+ }++ async function storedAlive(d: Deployment): Promise<StoredE2bDeployBody | null> {+ const stored = await store.get(d.id);+ if (!stored || !client.info) return stored;+ try {+ await client.info(stored.sandboxId);+ return stored;+ } catch (e) {+ if (!(e instanceof E2bSandboxGoneError)) throw e;+ await forget(d);+ return null;+ }+ }++ async function killStrays(d: Deployment, keep?: string): Promise<void> {+ for (const s of await client.list({ [DEPLOY_TAG]: d.id })) if (s.sandboxId !== keep) await client.kill(s.sandboxId);+ }++ return {+ profile: { managedScaleToZero: true, dataDir: DATA_DIR },++ apply: (d, version) =>+ serialized(d, async () => {+ resolveCache.delete(d.id);+ const existing = await connectStored(d);+ if (existing) {+ await install(d, existing.session, version);+ await killStrays(d, existing.stored.sandboxId);+ return endpointOf(existing.stored);+ }+ await killStrays(d);+ const session = await client.create({+ metadata: { name: `${prefix}-app-${d.id.slice(0, 12).toLowerCase()}`, [DEPLOY_TAG]: d.id },+ timeoutMs: d.alwaysOn ? ALWAYS_ON_TIMEOUT_MS : idleTimeoutMs,+ autoPause: true,+ autoResume: true,+ privateTraffic: true,+ });+ try {+ await install(d, session, version);+ const body: StoredE2bDeployBody = {+ deploymentId: d.id,+ sandboxId: session.sandboxId,+ host: session.host(appPort),+ ...(session.trafficAccessToken ? { trafficAccessToken: session.trafficAccessToken } : {}),+ createdAtMs: Date.now(),+ };+ await store.put(d.id, body);+ return endpointOf(body);+ } catch (e) {+ await session.kill().catch((err) => swallow("e2b-deploy: abandon failed body", err));+ throw e;+ }+ }),++ async setAlwaysOn(d, alwaysOn): Promise<void> {+ if (alwaysOn) await (await connectStored(d))?.session.keepAlive(ALWAYS_ON_TIMEOUT_MS);+ },++ async resolveEndpoint(d): Promise<DeployEndpoint | null> {+ const cached = resolveCache.get(d.id);+ if (cached) return cached;+ const stored = await storedAlive(d);+ if (!stored) return null;+ const endpoint = endpointOf(stored);+ if (resolveCacheMs > 0) resolveCache.set(d.id, endpoint);+ return endpoint;+ },++ async logs(d, logOpts): Promise<string | null> {+ const live = await connectStored(d);+ if (!live) return null;+ return appLogs((script, timeoutSec) => runE2bScript(live.session, script, timeoutSec), logOpts.tailLines);+ },++ destroy: (d) =>+ serialized(d, async () => {+ resolveCache.delete(d.id);+ const stored = await store.get(d.id);+ if (stored) await client.kill(stored.sandboxId);+ await killStrays(d);+ await forget(d);+ }),+ };+}diff --git a/src/deploy/porter-deploy-provider.ts b/src/deploy/porter-deploy-provider.tsindex fe3adb235..7581868c8 100644--- a/src/deploy/porter-deploy-provider.ts+++ b/src/deploy/porter-deploy-provider.ts@@ -3,8 +3,18 @@ import { LRUCache } from "lru-cache";import { NotFoundError } from "porter-sandbox";import type { Deployment, DeploymentVersion } from "./deploy-store.ts";import type { DeployEndpoint, DeployProvider } from "./deploy-provider.ts";-import { appGone, appLaunch, appLogs, appReporter, waitAppReady, type AppErrorSink } from "./shared-deploy-provider.ts";-import { normalizeRelPath, posixJoin, readTree } from "./deploy-fs.ts";+import {+ appEnv as declaredAppEnv,+ appGone,+ appLaunch,+ appLogs,+ appReporter,+ bundleEntries,+ extractBundle,+ waitAppReady,+ type AppErrorSink,+} from "./shared-deploy-provider.ts";+import { posixJoin } from "./deploy-fs.ts";import { makeTar } from "../sandbox/tar.ts";import {createPorterClient,@@ -22,7 +32,7 @@ import { createMemoryMap, type DurableMap } from "../persistence/durable-map.ts"import { createNoopAdvisoryLock, type AdvisoryLock } from "../persistence/advisory-lock.ts";import { createKeyedQueue } from "../util/async.ts";import { shq } from "../util/shell.ts";-import { errMessage, swallow, swallowAs } from "../util/errors.ts";+import { errMessage, execFailure, swallow, swallowAs } from "../util/errors.ts";const APP_DIR = "/app";const HOME_DIR = "/root";@@ -39,7 +49,6 @@ const RESOLVE_CACHE_MAX = 500;const KIND_TAG = "qm-kind";const DEPLOY_TAG = "qm-deploy";const DEFAULT_RUNNER_IMAGE = "ghcr.io/porter-dev/qm-app-runner:latest";-const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;export interface StoredPorterDeployBody {deploymentId: string;@@ -112,14 +121,12 @@ export function createPorterDeployProvider(opts: PorterDeployProviderOptions): D}function appEnv(version: DeploymentVersion): Record<string, string> {- const declared = Object.fromEntries(Object.entries(version.env ?? {}).filter(([k]) => ENV_NAME.test(k)));- return { ...declared, HOME: HOME_DIR, PORT: String(appPort), DATA_DIR };+ return declaredAppEnv(version.env, { HOME: HOME_DIR, PORT: String(appPort), DATA_DIR });}async function unpackTree(sandboxId: string, volumeId: string, guestDir: string, dir: string): Promise<void> {- const files = await readTree(dir, { tolerateMissing: true });- if (!files.length) return;- const entries = files.map((f) => ({ path: normalizeRelPath(f.path), data: f.data }));+ const entries = await bundleEntries(dir);+ if (!entries.length) return;const bundle = `${BUNDLE_DIR}/${randomUUID()}.tar`;const uploaded = await client.volumes.raw.writeFile(volumeId, await makeTar(entries), { path: `/${bundle}` }, { timeoutMs: BUNDLE_UPLOAD_TIMEOUT_MS })@@ -131,12 +138,10 @@ export function createPorterDeployProvider(opts: PorterDeployProviderOptions): Dconst guestBundle = posixJoin(DATA_DIR, bundle);const r = await execRaw(sandboxId,- `mkdir -p ${shq(guestDir)} && tar -xmf ${shq(guestBundle)} -C ${shq(guestDir)}; rc=$?; ` +- `rm -f ${shq(guestBundle)}; rmdir ${shq(posixJoin(DATA_DIR, BUNDLE_DIR))} 2>/dev/null; exit $rc`,+ extractBundle(guestBundle, guestDir, `rmdir ${shq(posixJoin(DATA_DIR, BUNDLE_DIR))} 2>/dev/null`),BUNDLE_EXTRACT_TIMEOUT_SEC,);- if (r.code !== 0)- throw new Error(`porter deploy: unpacking into ${guestDir} failed: ${(r.stderr || r.stdout).slice(0, 300)}`);+ if (r.code !== 0) throw execFailure(`porter deploy: unpacking into ${guestDir}`, r);}async function materialize(sandboxId: string, volumeId: string, version: DeploymentVersion): Promise<void> {@@ -152,7 +157,7 @@ export function createPorterDeployProvider(opts: PorterDeployProviderOptions): DappLaunch(inner, appPort),].join("; ");const r = await execRaw(sandboxId, script, APP_START_EXEC_TIMEOUT_SEC);- if (r.code !== 0) throw new Error(`porter deploy app start failed: ${r.stderr.slice(0, 300)}`);+ if (r.code !== 0) throw execFailure("porter deploy app start", r);await waitAppReady((script, timeoutSec) => execRaw(sandboxId, script, timeoutSec), {appPort,windowSec: readyWindowSec,diff --git a/src/deploy/shared-deploy-provider.ts b/src/deploy/shared-deploy-provider.tsindex e3edb1105..64316a32c 100644--- a/src/deploy/shared-deploy-provider.ts+++ b/src/deploy/shared-deploy-provider.ts@@ -5,6 +5,7 @@ import { errMessage, swallow } from "../util/errors.ts";import { redactSecrets } from "../harness/redact-secrets.ts";import { createSecretValueMasker } from "../security/secret-masking.ts";import type { Deployment } from "./deploy-store.ts";+import { normalizeRelPath, readTree } from "./deploy-fs.ts";const EXIT_APP_SERVER_ERROR = 96;const EXIT_APP_EXITED = 98;@@ -16,9 +17,37 @@ const READY_EXEC_GRACE_SEC = 15;const APP_PROBE_TIMEOUT_SEC = 5;const EXITED_MARK = "qm: entrypoint exited";const APP_CHECK_CACHE_MS = 15_000;-export const APP_PID_PATH = "/tmp/qm-app.pid";+const APP_PID_PATH = "/tmp/qm-app.pid";const APP_LOG_PATH = "/tmp/qm-app.log";+const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;++export function appEnv(+ declared: Record<string, string> | undefined,+ platform: Record<string, string> = {},+): Record<string, string> {+ return { ...Object.fromEntries(Object.entries(declared ?? {}).filter(([k]) => ENV_NAME.test(k))), ...platform };+}++export const exportEnv = (env: Record<string, string>): string =>+ Object.entries(env)+ .map(([k, v]) => `export ${k}=${shq(v)}`)+ .join("; ");++export const STOP_APP = [+ `old=$(cat ${shq(APP_PID_PATH)} 2>/dev/null || true)`,+ `if [ -n "$old" ]; then kill -TERM "-$old" 2>/dev/null || true; sleep 0.5; kill -KILL "-$old" 2>/dev/null || true; fi`,+ `rm -f ${shq(APP_PID_PATH)}`,+].join("; ");++export async function bundleEntries(dir: string): Promise<Array<{ path: string; data: Uint8Array }>> {+ const files = await readTree(dir, { tolerateMissing: true });+ return files.map((f) => ({ path: normalizeRelPath(f.path), data: f.data }));+}++export const extractBundle = (bundle: string, guestDir: string, cleanup = ""): string =>+ `mkdir -p ${shq(guestDir)} && tar -xmf ${shq(bundle)} -C ${shq(guestDir)}; rc=$?; rm -f ${shq(bundle)}; ${cleanup ? `${cleanup}; ` : ""}exit $rc`;+export type Exec = (script: string, timeoutSec: number) => Promise<Pick<ExecResult, "stdout" | "stderr" | "code">>;export type AppErrorSink = (diff --git a/src/deployment/secret-schema.ts b/src/deployment/secret-schema.tsindex 18b4819a8..fa80a495a 100644--- a/src/deployment/secret-schema.ts+++ b/src/deployment/secret-schema.ts@@ -13,6 +13,7 @@ type SecretGate =| "superserve"| "do-managed-agents"| "porter-deploy"+ | "e2b-deploy"| "fly-shared-deploy"| "fly-deploy"| "aws-deploy-gate"@@ -45,7 +46,7 @@ export const CORE_SECRET_SPECS: readonly RuntimeSecretSpec[] = [{ name: "SMOLMACHINES_TOKEN", requiredWhen: "smolmachines" },{ name: "AGENT37_API_KEY", requiredWhen: "agent37" },{ name: "SUPERSERVE_API_KEY", requiredWhen: "superserve" },- { name: "E2B_API_KEY", requiredWhen: "e2b" },+ { name: "E2B_API_KEY", requiredWhen: ["e2b", "e2b-deploy"] },{ name: "DO_AGENTS_API_TOKEN", requiredWhen: "do-managed-agents" },{ name: "MODAL_TOKEN_ID", requiredWhen: "modal" },{ name: "MODAL_TOKEN_SECRET", requiredWhen: "modal" },@@ -75,6 +76,7 @@ const GATE_PREDICATES: Readonly<Record<SecretGate, (env: NodeJS.ProcessEnv) => bsuperserve: (env) => sandboxBackendSelected(env, "superserve"),"do-managed-agents": (env) => sandboxBackendSelected(env, "do-managed-agents"),"porter-deploy": (env) => env.DEPLOY_PROVIDER === "porter",+ "e2b-deploy": (env) => env.DEPLOY_PROVIDER === "e2b","fly-shared-deploy": (env) => env.DEPLOY_PROVIDER === "fly" && Boolean(env.FLY_DEPLOY_SHARED_APP_NAME?.trim()),"fly-deploy": (env) => env.DEPLOY_PROVIDER === "fly","aws-deploy-gate": (env) => Boolean(env.AWS_DEPLOY_APPS_DOMAIN || env.DEPLOY_APPS_DOMAIN),diff --git a/src/sandbox/e2b-client.ts b/src/sandbox/e2b-client.tsindex 23f026b77..8662ac8b8 100644--- a/src/sandbox/e2b-client.ts+++ b/src/sandbox/e2b-client.ts@@ -1,3 +1,6 @@+import type { ExecResult } from "./sandbox.ts";+import { shq } from "../util/shell.ts";+export interface E2bCommandResult {stdout: string;stderr: string;@@ -19,6 +22,8 @@ export interface E2bMetrics {export interface E2bSession {readonly sandboxId: string;+ readonly trafficAccessToken?: string;+ host(port: number): string;runCommand(command: string, opts?: E2bRunOpts): Promise<E2bCommandResult>;readFileBytes(absPath: string): Promise<Uint8Array | null>;@@ -64,6 +69,8 @@ interface E2bCreateOpts {metadata: Record<string, string>;timeoutMs?: number;autoPause?: boolean;+ autoResume?: boolean;+ privateTraffic?: boolean;fromSnapshot?: string;}@@ -113,6 +120,13 @@ const DEFAULT_TTL_MS = 15 * 60_000;const DEFAULT_MAX_COMMAND_MS = 3600_000;const E2B_PRO_MAX_LIFETIME_MS = 24 * 3600_000;export const E2B_EXEC_MARGIN_MS = 60_000;++export async function runE2bScript(session: E2bSession, script: string, timeoutSec: number): Promise<ExecResult> {+ const r = await session.runCommand(`timeout ${timeoutSec} sh -c ${shq(script)}`, {+ timeoutMs: timeoutSec * 1000 + 30_000,+ });+ return { stdout: r.stdout, stderr: r.stderr, code: r.exitCode, timedOut: r.exitCode === 124 };+}const ALL_TRAFFIC = "0.0.0.0/0";const HOSTNAME_MATCHED_PORTS: Record<string, string> = { "http:": "80", "https:": "443" };@@ -204,6 +218,8 @@ export function createSdkE2bClient(opts: SdkE2bClientOptions): E2bClient {};return {sandboxId: sbx.sandboxId,+ ...(sbx.trafficAccessToken ? { trafficAccessToken: sbx.trafficAccessToken } : {}),+ host: (port) => sbx.getHost(port),async runCommand(command, runOpts): Promise<E2bCommandResult> {const timeoutMs = runOpts?.timeoutMs ?? maxCommandMs;await cover(timeoutMs);@@ -303,8 +319,13 @@ export function createSdkE2bClient(opts: SdkE2bClientOptions): E2bClient {...common,timeoutMs,metadata: createOpts.metadata,- lifecycle: { onTimeout: createOpts.autoPause ? "pause" : "kill", autoResume: false },- ...(network ? { network } : {}),+ lifecycle: {+ onTimeout: createOpts.autoPause ? "pause" : "kill",+ autoResume: !!(createOpts.autoPause && createOpts.autoResume),+ },+ ...(network || createOpts.privateTraffic+ ? { network: { ...network, ...(createOpts.privateTraffic ? { allowPublicTraffic: false } : {}) } }+ : {}),});return wrap(lib, sbx, timeoutMs);},diff --git a/src/sandbox/e2b-sandbox.ts b/src/sandbox/e2b-sandbox.tsindex 545fcba35..60a14e614 100644--- a/src/sandbox/e2b-sandbox.ts+++ b/src/sandbox/e2b-sandbox.ts@@ -34,6 +34,7 @@ import {type E2bClient,type E2bSandboxInfo,type E2bSession,+ runE2bScript,} from "./e2b-client.ts";import {createHomeSnapshotOps,@@ -75,6 +76,8 @@ export interface StoredE2bSandbox {homeDirty?: boolean;}+export const E2B_TEMPLATE_TOOLS = ["wget", "unzip", "gh", "aws", "x-api"];+export interface E2bSandboxOptions extends BlobStagingOptions {advisoryLock?: AdvisoryLock;client: E2bClient;@@ -294,12 +297,7 @@ export function createE2bSandbox(workspace: WorkspaceStore, opts: E2bSandboxOpti}async function execRaw(name: string, script: string, timeoutSec: number): Promise<ExecResult> {- return withSession(name, async (session) => {- const r = await session.runCommand(`timeout ${timeoutSec} sh -c ${shq(script)}`, {- timeoutMs: timeoutSec * 1000 + 30_000,- });- return { stdout: r.stdout, stderr: r.stderr, code: r.exitCode, timedOut: r.exitCode === 124 };- });+ return withSession(name, (session) => runE2bScript(session, script, timeoutSec));}const profile: AgentComputerProfile = {diff --git a/src/util/errors.ts b/src/util/errors.tsindex 379166e93..47092586e 100644--- a/src/util/errors.ts+++ b/src/util/errors.ts@@ -68,3 +68,13 @@ export async function withCleanup<T>(work: () => Promise<T>, cleanup: () => Promawait cleanup();return result;}++const FAILURE_OUTPUT_CHARS = 300;++export function execFailure(what: string, r: { stdout: string; stderr: string; code: number }): Error {+ const tail = (out: string): string => out.trim().slice(-FAILURE_OUTPUT_CHARS);+ const parts = [`${what} failed (exit ${r.code}${r.code === 124 ? ", timed out" : ""})`];+ if (r.stderr.trim()) parts.push(`stderr: ${tail(r.stderr)}`);+ if (r.stdout.trim()) parts.push(`stdout: ${tail(r.stdout)}`);+ return new Error(parts.join("; "));+}diff --git a/src/wiring.ts b/src/wiring.tsindex e80ef056c..8e06376b6 100644--- a/src/wiring.ts+++ b/src/wiring.ts@@ -194,6 +194,11 @@ import { createDockerDeployProvider } from "./deploy/docker-deploy-provider.ts";import { createAwsDeployProvider, type StoredDeployBody } from "./deploy/aws-deploy-provider.ts";import { createFlyDeployProvider, type FlyMachineConfig } from "./deploy/fly-deploy-provider.ts";import { createPorterDeployProvider, type StoredPorterDeployBody } from "./deploy/porter-deploy-provider.ts";+import {+ createE2bDeployProvider,+ E2B_DEPLOY_IDLE_TIMEOUT_SEC,+ type StoredE2bDeployBody,+} from "./deploy/e2b-deploy-provider.ts";import type { DeployProvider } from "./deploy/deploy-provider.ts";import { createDeployService } from "./deploy/deploy-service.ts";import {@@ -239,8 +244,8 @@ import {type StoredSuperserveSandbox,} from "./sandbox/superserve-sandbox.ts";import { createSdkSuperserveClient } from "./sandbox/superserve-client.ts";-import { createE2bSandbox, type StoredE2bSandbox } from "./sandbox/e2b-sandbox.ts";-import { createSdkE2bClient } from "./sandbox/e2b-client.ts";+import { createE2bSandbox, E2B_TEMPLATE_TOOLS, type StoredE2bSandbox } from "./sandbox/e2b-sandbox.ts";+import { createSdkE2bClient, type E2bClient } from "./sandbox/e2b-client.ts";import {createManagedAgentsSandbox,QM_TEMPLATE_TOOLS,@@ -929,19 +934,25 @@ export function buildApp(const e2bBodies = artifactMap<StoredE2bSandbox>("e2b_sandbox_bodies");const modalBodies = artifactMap<StoredModalSandbox>("modal_sandbox_bodies");const awsBodies = artifactMap<StoredMicrovm>("aws_sandbox_bodies");+ const e2bClient = (use: "computers" | "apps"): E2bClient => {+ const e2b = config.e2bSandbox;+ if (!e2b.apiKey)+ throw new Error(`${use === "apps" ? "DEPLOY_PROVIDER" : "SANDBOX_BACKEND"}=e2b requires E2B_API_KEY`);+ const ttlSec = use === "apps" ? E2B_DEPLOY_IDLE_TIMEOUT_SEC : e2b.sandboxTtlSec;+ return createSdkE2bClient({+ apiKey: e2b.apiKey,+ ...(e2b.templateId ? { templateId: e2b.templateId } : {}),+ ...(ttlSec ? { sandboxTtlMs: ttlSec * 1000 } : {}),+ ...(e2b.maxLifetimeSec ? { maxLifetimeMs: e2b.maxLifetimeSec * 1000 } : {}),+ ...(e2b.proxy ? { proxy: e2b.proxy } : {}),+ ...(use === "computers" && e2b.egressProxyUrl ? { egressProxyUrl: e2b.egressProxyUrl } : {}),+ });+ };const buildE2b = (): Sandbox => {const e2b = config.e2bSandbox;- if (!e2b.apiKey) throw new Error("SANDBOX_BACKEND=e2b requires E2B_API_KEY");return createE2bSandbox(workspace, {advisoryLock,- client: createSdkE2bClient({- apiKey: e2b.apiKey,- ...(e2b.templateId ? { templateId: e2b.templateId } : {}),- ...(e2b.sandboxTtlSec ? { sandboxTtlMs: e2b.sandboxTtlSec * 1000 } : {}),- ...(e2b.maxLifetimeSec ? { maxLifetimeMs: e2b.maxLifetimeSec * 1000 } : {}),- ...(e2b.proxy ? { proxy: e2b.proxy } : {}),- ...(e2b.egressProxyUrl ? { egressProxyUrl: e2b.egressProxyUrl } : {}),- }),+ client: e2bClient("computers"),...(e2b.namePrefix ? { namePrefix: e2b.namePrefix } : {}),...(e2b.defaultTimeoutSec ? { defaultTimeoutSec: e2b.defaultTimeoutSec } : {}),keepWarmSec: Math.ceil(config.backgroundJobTtlMaxMs / 1000),@@ -950,7 +961,7 @@ export function buildApp(? { nativeSnapshotIntervalMs: e2b.nativeSnapshotIntervalSec * 1000 }: {}),...(e2b.egressProxyUrl ? { egressProxyUrl: e2b.egressProxyUrl } : {}),- extraTools: deploymentLayer.advertisedTools,+ extraTools: [...(e2b.templateId ? E2B_TEMPLATE_TOOLS : []), ...deploymentLayer.advertisedTools],credentialPaths: deploymentLayer.credentialPaths,layerToolFiles: () => deploymentLayer.installFiles,blobTransfer,@@ -1742,6 +1753,16 @@ export function buildApp(store: artifactMap<StoredPorterDeployBody>("porter_deploy_bodies"),onError: sandboxOnError,}),+ e2b: () => {+ const e2b = config.e2bSandbox;+ return createE2bDeployProvider({+ client: e2bClient("apps"),+ ...(e2b.namePrefix ? { namePrefix: e2b.namePrefix } : {}),+ advisoryLock,+ store: artifactMap<StoredE2bDeployBody>("e2b_deploy_bodies"),+ onError: sandboxOnError,+ });+ },};if (config.deployProvider === "fly" &&@@ -1797,6 +1818,7 @@ export function buildApp(const deployService = createDeployService({deliveries,deployAppsDomain: config.awsDeploy.appsDomain,+ publicLinksAvailable: !!(config.awsDeploy.appsDomain && config.awsDeploy.gateSecret),publicWebUrl: config.publicWebUrl,appPublished: productAnalytics.appPublished,deployStore,diff --git a/test/config.test.ts b/test/config.test.tsindex fb0ff8b34..5a52d2434 100644--- a/test/config.test.ts+++ b/test/config.test.ts@@ -608,6 +608,11 @@ test("DEPLOY_PROVIDER=porter selects the Porter deploy provider and reads its en});});+test("DEPLOY_PROVIDER=e2b selects E2B publishing and needs the E2B key", () => {+ assert.equal(loadConfig({ DEPLOY_PROVIDER: "e2b", E2B_API_KEY: "e2b-key" }).deployProvider, "e2b");+ assert.throws(() => loadConfig({ DEPLOY_PROVIDER: "e2b" }), /missing or insecure required core secrets: E2B_API_KEY/);+});+test("the deploy runner image falls back to the sandbox image", () => {const config = loadConfig({DEPLOY_PROVIDER: "porter",@@ -736,6 +741,7 @@ test("the active provider's own apps domain reaches the gate when DEPLOY_APPS_DOtest("DEPLOY_APPS_DOMAIN refuses shared platform domains that cannot carry per-app subdomains", () => {assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "myapp.onporter.run" }), /shared platform domain/);assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "myapp.fly.dev" }), /shared platform domain/);+ assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "8081-abc.e2b.app" }), /shared platform domain/);assert.equal(loadConfig({ DEPLOY_APPS_DOMAIN: "apps.example.com", AWS_DEPLOY_GATE_SECRET: "0123456789abcdef0123456789abcdef" }).deployAppsDomain,diff --git a/test/e2b-deploy-provider.test.ts b/test/e2b-deploy-provider.test.tsnew file mode 100644index 000000000..af2602390--- /dev/null+++ b/test/e2b-deploy-provider.test.ts@@ -0,0 +1,142 @@+import { test, afterEach } from "node:test";+import assert from "node:assert/strict";+import { spawnSync } from "node:child_process";+import { mkdtempSync, writeFileSync } from "node:fs";+import { createServer } from "node:net";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { createE2bDeployProvider, type StoredE2bDeployBody } from "../src/deploy/e2b-deploy-provider.ts";+import { createMemoryMap, type DurableMap } from "../src/persistence/durable-map.ts";+import type { Deployment, DeploymentVersion } from "../src/deploy/deploy-store.ts";+import type { DeployProvider } from "../src/deploy/deploy-provider.ts";+import { scopeId } from "../src/types.ts";+import { installFakeE2b, type FakeE2b } from "./support/fake-e2b.ts";++const SERVER = [+ "const fs = require('fs');",+ "require('http').createServer((q, s) => {",+ " s.setHeader('connection', 'close');",+ " if (q.url === '/write') { fs.writeFileSync(process.env.DATA_DIR + '/note.txt', 'kept'); return s.end('ok'); }",+ " if (q.url === '/data') return s.end(fs.readFileSync(process.env.DATA_DIR + '/note.txt', 'utf8'));",+ " s.end('v' + process.env.APP_VERSION);",+ "}).listen(process.env.PORT);",+].join(" ");++let fake: FakeE2b | undefined;+let appPort = 0;++afterEach(() => {+ if (appPort) spawnSync("sh", ["-c", `lsof -ti tcp:${appPort} -sTCP:LISTEN | xargs kill 2>/dev/null; true`]);+ fake?.cleanup();+ fake = undefined;+});++const freePort = (): Promise<number> =>+ new Promise((resolve) => {+ const s = createServer();+ s.listen(0, () => {+ const port = (s.address() as { port: number }).port;+ s.close(() => resolve(port));+ });+ });++const deployment = (id: string, extra: Partial<Deployment> = {}): Deployment => ({+ id,+ ownerScopeId: scopeId("personal", "tester"),+ createdBy: "tester",+ currentVersion: 1,+ status: "running",+ endpoint: null,+ versions: [],+ ...extra,+});++function version(appVersion: string, entrypoint = "node server.js"): DeploymentVersion {+ const snapshotDir = mkdtempSync(join(tmpdir(), "e2b-deploy-snap-"));+ writeFileSync(join(snapshotDir, "server.js"), SERVER);+ return { version: 1, createdAt: Date.now(), entrypoint, snapshotDir, env: { APP_VERSION: appVersion } };+}++async function make(): Promise<{ provider: DeployProvider; store: DurableMap<StoredE2bDeployBody>; fake: FakeE2b }> {+ appPort = await freePort();+ fake = installFakeE2b({ info: true });+ const store = createMemoryMap<StoredE2bDeployBody>();+ const provider = createE2bDeployProvider({+ client: fake.client,+ namePrefix: "qmt",+ appPort,+ readyWindowSec: 10,+ idleTimeoutSec: 300,+ resolveCacheMs: 0,+ store,+ });+ return { provider, store, fake };+}++const get = async (path: string): Promise<string> => (await fetch(`http://127.0.0.1:${appPort}${path}`)).text();++test("e2b deploy runs each app in its own auto-pausing, auto-resuming private sandbox", async () => {+ const { provider, fake } = await make();+ const d = deployment("dep-aaaaaaaaaaaa");+ const endpoint = await provider.apply(d, version("1"));+ const name = "qmt-app-dep-aaaaaaaa";+ const sandboxId = fake.current(name)!.sandboxId;+ assert.deepEqual(endpoint, {+ host: `${appPort}-${sandboxId}.e2b.test`,+ port: 443,+ tls: true,+ proxyHeaders: { "e2b-traffic-access-token": `traffic-${sandboxId}` },+ });+ assert.deepEqual(fake.createOptions(name), {+ autoPause: true,+ autoResume: true,+ privateTraffic: true,+ timeoutMs: 300_000,+ });+ assert.equal(fake.current(name)!.metadata["qm-deploy"], d.id);+ assert.equal(await get("/"), "v1");+ assert.equal(provider.profile.managedScaleToZero, true);+});++test("e2b deploy updates in place, keeping the sandbox and its data directory", async () => {+ const { provider, fake } = await make();+ const d = deployment("dep-bbbbbbbbbbbb");+ const first = await provider.apply(d, version("1"));+ assert.equal(await get("/write"), "ok");+ const second = await provider.apply(d, version("2"));+ assert.deepEqual(second, first);+ assert.equal(fake.createdCount("qmt-app-dep-bbbbbbbb"), 1);+ assert.equal(await get("/"), "v2");+ assert.equal(await get("/data"), "kept");+});++test("e2b deploy resolves a paused app without waking it and forgets a killed one", async () => {+ const { provider, store, fake } = await make();+ const d = deployment("dep-cccccccccccc");+ const endpoint = await provider.apply(d, version("1"));+ const name = "qmt-app-dep-cccccccc";+ fake.pause(name);+ assert.deepEqual(await provider.resolveEndpoint!(d, version("1")), endpoint);+ assert.equal(fake.current(name)!.state, "paused");+ await fake.client.kill(fake.current(name)!.sandboxId);+ assert.equal(await provider.resolveEndpoint!(d, version("1")), null);+ assert.equal(await store.get(d.id), null);+});++test("e2b deploy kills the sandbox when the app never listens", async () => {+ const { provider, fake } = await make();+ const d = deployment("dep-dddddddddddd");+ await assert.rejects(provider.apply(d, version("1", "echo boom; exit 3")), /exited without binding port[\s\S]*boom/);+ assert.equal(fake.current("qmt-app-dep-dddddddd"), null);+});++test("e2b deploy keeps always-on apps up for the longest timeout and destroys cleanly", async () => {+ const { provider, store, fake } = await make();+ const d = deployment("dep-eeeeeeeeeeee", { alwaysOn: true });+ await provider.apply(d, version("1"));+ const name = "qmt-app-dep-eeeeeeee";+ assert.equal(fake.createOptions(name).timeoutMs, 24 * 3600_000);+ await provider.destroy(d);+ assert.equal(fake.current(name), null);+ assert.equal(await store.get(d.id), null);+});diff --git a/test/secret-schema-drift.test.ts b/test/secret-schema-drift.test.tsindex aa6ad6e23..46aa7f40c 100644--- a/test/secret-schema-drift.test.ts+++ b/test/secret-schema-drift.test.ts@@ -149,6 +149,12 @@ test("both porter roles share PORTER_DEPLOY_API_TOKEN", () => {);});+test("E2B sandboxes and E2B publishing share E2B_API_KEY", () => {+ assert.deepEqual(validateCoreSecretEnv({ DEPLOY_PROVIDER: "e2b" } as NodeJS.ProcessEnv), ["E2B_API_KEY"]);+ assert.deepEqual(validateCoreSecretEnv({ SANDBOX_BACKEND: "e2b" } as NodeJS.ProcessEnv), ["E2B_API_KEY"]);+ assert.deepEqual(validateCoreSecretEnv({ DEPLOY_PROVIDER: "e2b", E2B_API_KEY: "k" } as NodeJS.ProcessEnv), []);+});+test("shared Fly publishing validates its private peer secret", () => {const env = { DEPLOY_PROVIDER: "fly", FLY_DEPLOY_API_TOKEN: "token", FLY_DEPLOY_SHARED_APP_NAME: "acme-apps" };assert.deepEqual(validateCoreSecretEnv(env), ["FLY_DEPLOY_WIREGUARD_PEERS"]);diff --git a/test/share-deployment.test.ts b/test/share-deployment.test.tsindex a67d9b86e..dacc29809 100644--- a/test/share-deployment.test.ts+++ b/test/share-deployment.test.ts@@ -36,6 +36,7 @@ type Dir = { resolve: (orgId: string, q: string) => Promise<RecipientResolution>function makeDeploy(canManageEmail?: (email: string) => Promise<boolean>,externalSharing = true,+ publicLinksAvailable = true,): { deploy: DeployService; acl: AclStore } {const acl: AclStore = createAclStore();const deploy = createDeployService({@@ -49,6 +50,7 @@ function makeDeploy(acl,...(canManageEmail ? { canManageEmail } : {}),externalSharingAllowed: async () => externalSharing,+ publicLinksAvailable,deployDir: mkdtempSync(join(tmpdir(), "share-api-")),});return { deploy, acl };@@ -84,7 +86,14 @@ function callShare(out.body = d ? JSON.parse(d) : undefined;},};- const ctx = { res, app, deps: { featureFlags }, params: { id }, body, capability } as unknown as ApiCtx;+ const ctx = {+ res,+ app,+ deps: { featureFlags },+ params: { id },+ body,+ capability,+ } as unknown as ApiCtx;return shareDeployment(ctx).then(() => out);}@@ -566,6 +575,27 @@ test("deployment public access is explicit, owner-only, and reversible", async (assert.equal((await app.getDeployment("public-toggle"))?.public, undefined);});+test("without an apps domain, a public link is refused on the share route and the agent's apps tool", async () => {+ const { deploy } = makeDeploy(undefined, true, false);+ const app = createApp({ deploy, identity: createIdentityService() } as unknown as Parameters<typeof createApp>[0]);+ await app.deploy({+ ownerScopeId: scopeId("personal", "U1"),+ createdBy: "U1",+ entrypoint: "x",+ files: [],+ name: "path-only",+ });+ const refused = await callShare(app, cap("U1"), "path-only", { public: true });+ assert.equal(refused.status, 409);+ assert.equal(refused.body.error, "public_links_unavailable");+ await assert.rejects(+ () => toolCtx(deploy).setDeploymentPublic("path-only", true),+ /Public links need an apps domain/,+ );+ assert.equal((await app.getDeployment("path-only"))?.public, undefined);+ assert.equal((await callShare(app, cap("U1"), "path-only", { public: false })).status, 200);+});+test("with external app sharing off, public links and outside emails are refused while org sharing still works", async () => {const { deploy } = makeDeploy(async (email) => email.endsWith("@acme.test"), false);const app = createApp({ deploy, identity: createIdentityService() } as unknown as Parameters<typeof createApp>[0]);diff --git a/test/support/fake-e2b.ts b/test/support/fake-e2b.tsindex 7667b0bab..4c0d5850a 100644--- a/test/support/fake-e2b.ts+++ b/test/support/fake-e2b.ts@@ -22,6 +22,9 @@ interface FakeRecord {fromSnapshot?: string;timeoutMs: number[];loseNextCommand: boolean;+ autoResume: boolean;+ privateTraffic: boolean;+ createTimeoutMs?: number;}export interface FakeE2b {@@ -34,6 +37,7 @@ export interface FakeE2b {expirePaused(): void;loseNextCommand(name: string): void;timeouts(name: string): number[];+ createOptions(name: string): { autoPause: boolean; autoResume: boolean; privateTraffic: boolean; timeoutMs?: number };snapshots(): string[];restoredFrom(name: string): string | undefined;metrics: E2bMetrics | null;@@ -41,7 +45,7 @@ export interface FakeE2b {cleanup(): void;}-export function installFakeE2b(): FakeE2b {+export function installFakeE2b(fakeOpts: { info?: boolean } = {}): FakeE2b {const root = mkdtempSync(join(tmpdir(), "fake-e2b-"));const records = new Map<string, FakeRecord>();const snapshots = new Map<string, string>();@@ -106,6 +110,16 @@ export function installFakeE2b(): FakeE2b {if (r) r.loseNextCommand = true;},timeouts: (name) => [...(byName(name)?.timeoutMs ?? [])],+ createOptions: (name) => {+ const r = byName(name);+ if (!r) throw new Error(`fake-e2b: no sandbox named ${name}`);+ return {+ autoPause: r.autoPause,+ autoResume: r.autoResume,+ privateTraffic: r.privateTraffic,+ ...(r.createTimeoutMs ? { timeoutMs: r.createTimeoutMs } : {}),+ };+ },snapshots: () => [...snapshots.keys()],restoredFrom: (name) => byName(name)?.fromSnapshot,execScripts: () => [...execScripts],@@ -114,6 +128,8 @@ export function installFakeE2b(): FakeE2b {const session = (r: FakeRecord): E2bSession => ({sandboxId: r.sandboxId,+ ...(r.privateTraffic ? { trafficAccessToken: `traffic-${r.sandboxId}` } : {}),+ host: (port) => `${port}-${r.sandboxId}.e2b.test`,async runCommand(command, opts): Promise<E2bCommandResult> {alive(r);if (opts?.timeoutMs) r.timeoutMs.push(opts.timeoutMs);@@ -174,7 +190,17 @@ export function installFakeE2b(): FakeE2b {},});+ const info: Pick<E2bClient, "info"> = fakeOpts.info+ ? {+ async info(sandboxId) {+ const r = records.get(sandboxId);+ if (!r || r.expired) throw new E2bSandboxGoneError(sandboxId, "sandbox was not found");+ return { state: r.state, expiresAtMs: Date.now() + 60_000, onTimeout: r.autoPause ? "pause" : "kill" };+ },+ }+ : {};fake.client = {+ ...info,async create(opts): Promise<E2bSession> {const id = `sbx-${nextId++}`;const r: FakeRecord = {@@ -186,7 +212,10 @@ export function installFakeE2b(): FakeE2b {home: join(root, id),createdAt: ++clock,timeoutMs: [],+ ...(opts.timeoutMs ? { createTimeoutMs: opts.timeoutMs } : {}),loseNextCommand: false,+ autoResume: opts.autoResume ?? false,+ privateTraffic: opts.privateTraffic ?? false,...(opts.fromSnapshot ? { fromSnapshot: opts.fromSnapshot } : {}),};if (opts.fromSnapshot) {