The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import type * as microvms from "@distilled.cloud/aws/lambda-microvms"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 6 | import * as HttpBody from "effect/http/HttpBody"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import type * as HttpClientError from "effect/http/HttpClientError"; |
| 9 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 10 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 11 | import * as Layer from "effect/Layer"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import { BunMicrovm } from "./bun-image.ts"; |
| 14 | import { EffectfulBun } from "./effectful-bun.ts"; |
| 15 | import { EffectfulNode } from "./effectful-node.ts"; |
| 16 | import { ExternalMicrovm } from "./external-image.ts"; |
| 17 | import { NodeMicrovm } from "./node-image.ts"; |
| 18 | import { OpencodeMicrovm } from "./opencode-image.ts"; |
| 19 | |
| 20 | /** |
| 21 | * Lambda host for the MicroVM cold-start benchmark. Exposes a boot/shutdown |
| 22 | * lifecycle so the driver can time each cold start from the OUTSIDE: |
| 23 | * |
| 24 | * - `GET /boot?variant=effectful|external&key=K` launches ONE fresh MicroVM and |
| 25 | * blocks until its in-VM service answers, returning `{ id, readyMs }` where |
| 26 | * `readyMs` = `RunMicrovm` → service-available (time to usable service). It |
| 27 | * does NOT terminate. |
| 28 | * - `GET /shutdown?variant=…&id=ID` terminates the MicroVM. |
| 29 | */ |
| 30 | type Variant = { |
| 31 | readonly run: ( |
| 32 | req: AWS.Lambda.RunMicrovmRequest, |
| 33 | ) => Effect.Effect<microvms.RunMicrovmResponse, microvms.RunMicrovmError>; |
| 34 | readonly get: ( |
| 35 | req: AWS.Lambda.GetMicrovmRequest, |
| 36 | ) => Effect.Effect<microvms.GetMicrovmResponse, microvms.GetMicrovmError>; |
| 37 | readonly auth: ( |
| 38 | req: AWS.Lambda.CreateAuthTokenRequest, |
| 39 | ) => Effect.Effect<microvms.CreateMicrovmAuthTokenResponse, microvms.CreateMicrovmAuthTokenError>; |
| 40 | readonly term: ( |
| 41 | req: AWS.Lambda.TerminateMicrovmRequest, |
| 42 | ) => Effect.Effect<microvms.TerminateMicrovmResponse, microvms.TerminateMicrovmError>; |
| 43 | readonly reachable: ( |
| 44 | endpoint: string, |
| 45 | authToken: AWS.Lambda.MicrovmConnection["authToken"], |
| 46 | ) => Effect.Effect<unknown, HttpClientError.HttpClientError | Error, HttpClient.HttpClient>; |
| 47 | }; |
| 48 | |
| 49 | export default class Orchestrator extends AWS.Lambda.Function<Orchestrator>()( |
| 50 | "MicrovmBenchOrchestrator", |
| 51 | { |
| 52 | main: import.meta.filename, |
| 53 | functionUrl: true, |
| 54 | timeout: Duration.seconds(120), |
| 55 | }, |
| 56 | Effect.gen(function* () { |
| 57 | // Plain-HTTP reachable check shared by every non-effectful (raw) image: |
| 58 | // hit `GET /` with the MicroVM auth headers and read the body. |
| 59 | const rawReachable = (endpoint: string, authToken: AWS.Lambda.MicrovmConnection["authToken"]) => |
| 60 | Effect.gen(function* () { |
| 61 | const client = yield* HttpClient.HttpClient; |
| 62 | const res = yield* client.get(`https://${endpoint}/`, { |
| 63 | headers: AWS.Lambda.microvmAuthHeaders(authToken), |
| 64 | }); |
| 65 | return yield* res.text; |
| 66 | }); |
| 67 | |
| 68 | const effectfulBun: Variant = { |
| 69 | run: yield* AWS.Lambda.RunMicrovm(EffectfulBun), |
| 70 | get: yield* AWS.Lambda.GetMicrovm(EffectfulBun), |
| 71 | auth: yield* AWS.Lambda.CreateAuthToken(EffectfulBun), |
| 72 | term: yield* AWS.Lambda.TerminateMicrovm(EffectfulBun), |
| 73 | reachable: (endpoint, authToken) => |
| 74 | Effect.gen(function* () { |
| 75 | const sandbox = yield* AWS.Lambda.connectMicrovm(EffectfulBun, { |
| 76 | endpoint, |
| 77 | authToken, |
| 78 | }); |
| 79 | return yield* sandbox.hello("bench"); |
| 80 | }), |
| 81 | }; |
| 82 | const effectfulNode: Variant = { |
| 83 | run: yield* AWS.Lambda.RunMicrovm(EffectfulNode), |
| 84 | get: yield* AWS.Lambda.GetMicrovm(EffectfulNode), |
| 85 | auth: yield* AWS.Lambda.CreateAuthToken(EffectfulNode), |
| 86 | term: yield* AWS.Lambda.TerminateMicrovm(EffectfulNode), |
| 87 | reachable: (endpoint, authToken) => |
| 88 | Effect.gen(function* () { |
| 89 | const sandbox = yield* AWS.Lambda.connectMicrovm(EffectfulNode, { |
| 90 | endpoint, |
| 91 | authToken, |
| 92 | }); |
| 93 | return yield* sandbox.hello("bench"); |
| 94 | }), |
| 95 | }; |
| 96 | // Raw baselines: same image-build path + `GET /` reachable as each other, |
| 97 | // differing only by runtime (bun / node) or language (Python external). |
| 98 | const bun: Variant = { |
| 99 | run: yield* AWS.Lambda.RunMicrovm(BunMicrovm), |
| 100 | get: yield* AWS.Lambda.GetMicrovm(BunMicrovm), |
| 101 | auth: yield* AWS.Lambda.CreateAuthToken(BunMicrovm), |
| 102 | term: yield* AWS.Lambda.TerminateMicrovm(BunMicrovm), |
| 103 | reachable: rawReachable, |
| 104 | }; |
| 105 | const node: Variant = { |
| 106 | run: yield* AWS.Lambda.RunMicrovm(NodeMicrovm), |
| 107 | get: yield* AWS.Lambda.GetMicrovm(NodeMicrovm), |
| 108 | auth: yield* AWS.Lambda.CreateAuthToken(NodeMicrovm), |
| 109 | term: yield* AWS.Lambda.TerminateMicrovm(NodeMicrovm), |
| 110 | reachable: rawReachable, |
| 111 | }; |
| 112 | const external: Variant = { |
| 113 | run: yield* AWS.Lambda.RunMicrovm(ExternalMicrovm), |
| 114 | get: yield* AWS.Lambda.GetMicrovm(ExternalMicrovm), |
| 115 | auth: yield* AWS.Lambda.CreateAuthToken(ExternalMicrovm), |
| 116 | term: yield* AWS.Lambda.TerminateMicrovm(ExternalMicrovm), |
| 117 | reachable: rawReachable, |
| 118 | }; |
| 119 | // opencode is "usable" when its health endpoint answers healthy AND a |
| 120 | // session can actually be created — a real write through the app, which |
| 121 | // proves the server is functional after the snapshot resume, not merely |
| 122 | // listening. The server requires basic auth (see |
| 123 | // contexts/microvm-opencode/Dockerfile), stacked on the MicroVM proxy |
| 124 | // auth headers. |
| 125 | const opencode: Variant = { |
| 126 | run: yield* AWS.Lambda.RunMicrovm(OpencodeMicrovm), |
| 127 | get: yield* AWS.Lambda.GetMicrovm(OpencodeMicrovm), |
| 128 | auth: yield* AWS.Lambda.CreateAuthToken(OpencodeMicrovm), |
| 129 | term: yield* AWS.Lambda.TerminateMicrovm(OpencodeMicrovm), |
| 130 | reachable: (endpoint, authToken) => |
| 131 | Effect.gen(function* () { |
| 132 | const client = yield* HttpClient.HttpClient; |
| 133 | const headers = { |
| 134 | ...AWS.Lambda.microvmAuthHeaders(authToken), |
| 135 | // base64("opencode:bench") |
| 136 | authorization: "Basic b3BlbmNvZGU6YmVuY2g=", |
| 137 | }; |
| 138 | const health = yield* client.get(`https://${endpoint}/global/health`, { headers }); |
| 139 | const healthBody = yield* health.text; |
| 140 | if (health.status !== 200 || !healthBody.includes('"healthy":true')) { |
| 141 | return yield* Effect.fail( |
| 142 | new Error(`opencode health ${health.status}: ${healthBody.slice(0, 120)}`), |
| 143 | ); |
| 144 | } |
| 145 | const session = yield* client.post(`https://${endpoint}/session`, { |
| 146 | headers, |
| 147 | body: HttpBody.text("{}", "application/json"), |
| 148 | }); |
| 149 | const sessionBody = yield* session.text; |
| 150 | if (session.status !== 200 || !sessionBody.includes('"id"')) { |
| 151 | return yield* Effect.fail( |
| 152 | new Error(`opencode session ${session.status}: ${sessionBody.slice(0, 120)}`), |
| 153 | ); |
| 154 | } |
| 155 | return sessionBody; |
| 156 | }), |
| 157 | }; |
| 158 | const variants: Record<string, Variant> = { |
| 159 | "effectful-bun": effectfulBun, |
| 160 | "effectful-node": effectfulNode, |
| 161 | bun, |
| 162 | node, |
| 163 | external, |
| 164 | opencode, |
| 165 | }; |
| 166 | const pick = (v: string | null): Variant => variants[v ?? ""] ?? effectfulBun; |
| 167 | |
| 168 | // Run one fresh MicroVM and time RunMicrovm → service-reachable (readyMs). |
| 169 | // Leaves it running for an explicit /shutdown; self-terminates only if boot |
| 170 | // itself fails. |
| 171 | const boot = (v: Variant) => |
| 172 | Effect.gen(function* () { |
| 173 | const start = yield* Effect.sync(() => Date.now()); |
| 174 | const vm = yield* v.run({ |
| 175 | idlePolicy: { |
| 176 | maxIdleDurationSeconds: 900, |
| 177 | suspendedDurationSeconds: 300, |
| 178 | autoResumeEnabled: true, |
| 179 | }, |
| 180 | }); |
| 181 | return yield* Effect.gen(function* () { |
| 182 | yield* v.get({ microvmIdentifier: vm.microvmId }).pipe( |
| 183 | Effect.flatMap((m) => |
| 184 | m.state === "RUNNING" ? Effect.void : Effect.fail(new Error(`microvm ${m.state}`)), |
| 185 | ), |
| 186 | Effect.retry({ |
| 187 | schedule: Schedule.spaced("500 millis"), |
| 188 | times: 180, |
| 189 | }), |
| 190 | ); |
| 191 | const { authToken } = yield* v.auth({ |
| 192 | microvmIdentifier: vm.microvmId, |
| 193 | expirationInMinutes: 5, |
| 194 | allowedPorts: [{ port: 8080 }], |
| 195 | }); |
| 196 | yield* v.reachable(vm.endpoint, authToken).pipe( |
| 197 | Effect.retry({ |
| 198 | schedule: Schedule.exponential("250 millis"), |
| 199 | times: 14, |
| 200 | }), |
| 201 | ); |
| 202 | const readyMs = (yield* Effect.sync(() => Date.now())) - start; |
| 203 | return yield* HttpServerResponse.json({ id: vm.microvmId, readyMs }); |
| 204 | }).pipe( |
| 205 | Effect.onError(() => v.term({ microvmIdentifier: vm.microvmId }).pipe(Effect.ignore)), |
| 206 | Effect.provide(FetchHttpClient.layer), |
| 207 | ); |
| 208 | }); |
| 209 | |
| 210 | return { |
| 211 | fetch: Effect.gen(function* () { |
| 212 | const request = yield* HttpServerRequest; |
| 213 | const url = new URL(request.originalUrl); |
| 214 | const v = pick(url.searchParams.get("variant")); |
| 215 | |
| 216 | if (request.method === "GET" && url.pathname === "/boot") { |
| 217 | return yield* boot(v); |
| 218 | } |
| 219 | if (request.method === "GET" && url.pathname === "/shutdown") { |
| 220 | const id = url.searchParams.get("id")!; |
| 221 | yield* v.term({ microvmIdentifier: id }).pipe(Effect.ignore); |
| 222 | return yield* HttpServerResponse.json({ ok: true }); |
| 223 | } |
| 224 | return HttpServerResponse.text("ok"); |
| 225 | }).pipe(Effect.orDie), |
| 226 | }; |
| 227 | }).pipe( |
| 228 | Effect.provide( |
| 229 | Layer.mergeAll( |
| 230 | AWS.Lambda.RunMicrovmHttp, |
| 231 | AWS.Lambda.GetMicrovmHttp, |
| 232 | AWS.Lambda.CreateAuthTokenHttp, |
| 233 | AWS.Lambda.TerminateMicrovmHttp, |
| 234 | ), |
| 235 | ), |
| 236 | ), |
| 237 | ) {} |