The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 5 | import * as HttpBody from "effect/http/HttpBody"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 8 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import type * as Redacted from "effect/Redacted"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import { BunMicrovm } from "./bun-image.ts"; |
| 13 | import { EffectfulBun } from "./effectful-bun.ts"; |
| 14 | import { EffectfulNode } from "./effectful-node.ts"; |
| 15 | import { ExternalMicrovm } from "./external-image.ts"; |
| 16 | import { NodeMicrovm } from "./node-image.ts"; |
| 17 | import { OpencodeMicrovm } from "./opencode-image.ts"; |
| 18 | |
| 19 | /** |
| 20 | * Cloudflare Worker host for the MicroVM cold-start benchmark — the cross-cloud |
| 21 | * analog of the Lambda {@link import("./orchestrator.ts")}. Same boot/shutdown |
| 22 | * lifecycle and `readyMs` (time to usable service), but driven from a Worker: |
| 23 | * binding the AWS MicroVM ops provisions an IAM User + AccessKey + assume-role |
| 24 | * Role once per worker (see `MicrovmBinding.ts`), measuring the Worker → MicroVM |
| 25 | * cold-start path. |
| 26 | */ |
| 27 | type Variant = { |
| 28 | readonly run: ( |
| 29 | req: AWS.Lambda.RunMicrovmRequest, |
| 30 | ) => Effect.Effect<{ microvmId: string; endpoint: string }, any>; |
| 31 | readonly get: (req: { microvmIdentifier: string }) => Effect.Effect<{ state: string }, any>; |
| 32 | readonly auth: (req: { |
| 33 | microvmIdentifier: string; |
| 34 | expirationInMinutes: number; |
| 35 | allowedPorts: { port: number }[]; |
| 36 | }) => Effect.Effect< |
| 37 | { |
| 38 | authToken: Record<string, string | Redacted.Redacted<string> | undefined>; |
| 39 | }, |
| 40 | any |
| 41 | >; |
| 42 | readonly term: (req: { microvmIdentifier: string }) => Effect.Effect<unknown, any>; |
| 43 | readonly reachable: ( |
| 44 | endpoint: string, |
| 45 | authToken: Record<string, string | Redacted.Redacted<string> | undefined>, |
| 46 | ) => Effect.Effect<unknown, any, any>; |
| 47 | }; |
| 48 | |
| 49 | export default Cloudflare.Worker( |
| 50 | "MicrovmBenchWorker", |
| 51 | { main: import.meta.filename }, |
| 52 | Effect.gen(function* () { |
| 53 | const rawReachable = ( |
| 54 | endpoint: string, |
| 55 | authToken: Record<string, string | Redacted.Redacted<string> | undefined>, |
| 56 | ) => |
| 57 | Effect.gen(function* () { |
| 58 | const client = yield* HttpClient.HttpClient; |
| 59 | const res = yield* client.get(`https://${endpoint}/`, { |
| 60 | headers: AWS.Lambda.microvmAuthHeaders(authToken), |
| 61 | }); |
| 62 | return yield* res.text; |
| 63 | }); |
| 64 | |
| 65 | const effectfulBun: Variant = { |
| 66 | run: yield* AWS.Lambda.RunMicrovm(EffectfulBun), |
| 67 | get: yield* AWS.Lambda.GetMicrovm(EffectfulBun), |
| 68 | auth: yield* AWS.Lambda.CreateAuthToken(EffectfulBun), |
| 69 | term: yield* AWS.Lambda.TerminateMicrovm(EffectfulBun), |
| 70 | reachable: (endpoint, authToken) => |
| 71 | Effect.gen(function* () { |
| 72 | const sandbox = yield* AWS.Lambda.connectMicrovm(EffectfulBun, { |
| 73 | endpoint, |
| 74 | authToken, |
| 75 | }); |
| 76 | return yield* sandbox.hello("bench"); |
| 77 | }), |
| 78 | }; |
| 79 | const effectfulNode: Variant = { |
| 80 | run: yield* AWS.Lambda.RunMicrovm(EffectfulNode), |
| 81 | get: yield* AWS.Lambda.GetMicrovm(EffectfulNode), |
| 82 | auth: yield* AWS.Lambda.CreateAuthToken(EffectfulNode), |
| 83 | term: yield* AWS.Lambda.TerminateMicrovm(EffectfulNode), |
| 84 | reachable: (endpoint, authToken) => |
| 85 | Effect.gen(function* () { |
| 86 | const sandbox = yield* AWS.Lambda.connectMicrovm(EffectfulNode, { |
| 87 | endpoint, |
| 88 | authToken, |
| 89 | }); |
| 90 | return yield* sandbox.hello("bench"); |
| 91 | }), |
| 92 | }; |
| 93 | const bun: Variant = { |
| 94 | run: yield* AWS.Lambda.RunMicrovm(BunMicrovm), |
| 95 | get: yield* AWS.Lambda.GetMicrovm(BunMicrovm), |
| 96 | auth: yield* AWS.Lambda.CreateAuthToken(BunMicrovm), |
| 97 | term: yield* AWS.Lambda.TerminateMicrovm(BunMicrovm), |
| 98 | reachable: rawReachable, |
| 99 | }; |
| 100 | const node: Variant = { |
| 101 | run: yield* AWS.Lambda.RunMicrovm(NodeMicrovm), |
| 102 | get: yield* AWS.Lambda.GetMicrovm(NodeMicrovm), |
| 103 | auth: yield* AWS.Lambda.CreateAuthToken(NodeMicrovm), |
| 104 | term: yield* AWS.Lambda.TerminateMicrovm(NodeMicrovm), |
| 105 | reachable: rawReachable, |
| 106 | }; |
| 107 | const external: Variant = { |
| 108 | run: yield* AWS.Lambda.RunMicrovm(ExternalMicrovm), |
| 109 | get: yield* AWS.Lambda.GetMicrovm(ExternalMicrovm), |
| 110 | auth: yield* AWS.Lambda.CreateAuthToken(ExternalMicrovm), |
| 111 | term: yield* AWS.Lambda.TerminateMicrovm(ExternalMicrovm), |
| 112 | reachable: rawReachable, |
| 113 | }; |
| 114 | // opencode is "usable" when its health endpoint answers healthy AND a |
| 115 | // session can actually be created — a real write through the app, which |
| 116 | // proves the server is functional after the snapshot resume, not merely |
| 117 | // listening. The server requires basic auth (see |
| 118 | // contexts/microvm-opencode/Dockerfile), stacked on the MicroVM proxy |
| 119 | // auth headers. |
| 120 | const opencode: Variant = { |
| 121 | run: yield* AWS.Lambda.RunMicrovm(OpencodeMicrovm), |
| 122 | get: yield* AWS.Lambda.GetMicrovm(OpencodeMicrovm), |
| 123 | auth: yield* AWS.Lambda.CreateAuthToken(OpencodeMicrovm), |
| 124 | term: yield* AWS.Lambda.TerminateMicrovm(OpencodeMicrovm), |
| 125 | reachable: (endpoint, authToken) => |
| 126 | Effect.gen(function* () { |
| 127 | const client = yield* HttpClient.HttpClient; |
| 128 | const headers = { |
| 129 | ...AWS.Lambda.microvmAuthHeaders(authToken), |
| 130 | // base64("opencode:bench") |
| 131 | authorization: "Basic b3BlbmNvZGU6YmVuY2g=", |
| 132 | }; |
| 133 | const health = yield* client.get(`https://${endpoint}/global/health`, { headers }); |
| 134 | const healthBody = yield* health.text; |
| 135 | if (health.status !== 200 || !healthBody.includes('"healthy":true')) { |
| 136 | return yield* Effect.fail( |
| 137 | new Error(`opencode health ${health.status}: ${healthBody.slice(0, 120)}`), |
| 138 | ); |
| 139 | } |
| 140 | const session = yield* client.post(`https://${endpoint}/session`, { |
| 141 | headers, |
| 142 | body: HttpBody.text("{}", "application/json"), |
| 143 | }); |
| 144 | const sessionBody = yield* session.text; |
| 145 | if (session.status !== 200 || !sessionBody.includes('"id"')) { |
| 146 | return yield* Effect.fail( |
| 147 | new Error(`opencode session ${session.status}: ${sessionBody.slice(0, 120)}`), |
| 148 | ); |
| 149 | } |
| 150 | return sessionBody; |
| 151 | }), |
| 152 | }; |
| 153 | const variants: Record<string, Variant> = { |
| 154 | "effectful-bun": effectfulBun, |
| 155 | "effectful-node": effectfulNode, |
| 156 | bun, |
| 157 | node, |
| 158 | external, |
| 159 | opencode, |
| 160 | }; |
| 161 | const pick = (v: string | null): Variant => variants[v ?? ""] ?? effectfulBun; |
| 162 | |
| 163 | const boot = (v: Variant) => |
| 164 | Effect.gen(function* () { |
| 165 | const start = yield* Effect.sync(() => Date.now()); |
| 166 | const vm = yield* v.run({ |
| 167 | idlePolicy: { |
| 168 | maxIdleDurationSeconds: 900, |
| 169 | suspendedDurationSeconds: 300, |
| 170 | autoResumeEnabled: true, |
| 171 | }, |
| 172 | }); |
| 173 | return yield* Effect.gen(function* () { |
| 174 | yield* v.get({ microvmIdentifier: vm.microvmId }).pipe( |
| 175 | Effect.flatMap((m) => |
| 176 | m.state === "RUNNING" ? Effect.void : Effect.fail(new Error(`microvm ${m.state}`)), |
| 177 | ), |
| 178 | Effect.retry({ |
| 179 | schedule: Schedule.spaced("500 millis"), |
| 180 | times: 180, |
| 181 | }), |
| 182 | ); |
| 183 | const { authToken } = yield* v.auth({ |
| 184 | microvmIdentifier: vm.microvmId, |
| 185 | expirationInMinutes: 5, |
| 186 | allowedPorts: [{ port: 8080 }], |
| 187 | }); |
| 188 | yield* v.reachable(vm.endpoint, authToken).pipe( |
| 189 | Effect.retry({ |
| 190 | schedule: Schedule.exponential("250 millis"), |
| 191 | times: 14, |
| 192 | }), |
| 193 | ); |
| 194 | const readyMs = (yield* Effect.sync(() => Date.now())) - start; |
| 195 | return yield* HttpServerResponse.json({ id: vm.microvmId, readyMs }); |
| 196 | }).pipe( |
| 197 | Effect.onError(() => v.term({ microvmIdentifier: vm.microvmId }).pipe(Effect.ignore)), |
| 198 | Effect.provide(FetchHttpClient.layer), |
| 199 | ); |
| 200 | }); |
| 201 | |
| 202 | return { |
| 203 | fetch: Effect.gen(function* () { |
| 204 | const request = yield* HttpServerRequest; |
| 205 | const url = new URL(request.url, "http://microvm"); |
| 206 | const v = pick(url.searchParams.get("variant")); |
| 207 | |
| 208 | if (url.pathname === "/boot") { |
| 209 | return yield* boot(v); |
| 210 | } |
| 211 | if (url.pathname === "/shutdown") { |
| 212 | const id = url.searchParams.get("id")!; |
| 213 | yield* v.term({ microvmIdentifier: id }).pipe(Effect.ignore); |
| 214 | return yield* HttpServerResponse.json({ ok: true }); |
| 215 | } |
| 216 | return HttpServerResponse.text("ok"); |
| 217 | }).pipe(Effect.orDie), |
| 218 | }; |
| 219 | }).pipe( |
| 220 | Effect.provide( |
| 221 | Layer.mergeAll( |
| 222 | AWS.Lambda.RunMicrovmHttp, |
| 223 | AWS.Lambda.GetMicrovmHttp, |
| 224 | AWS.Lambda.CreateAuthTokenHttp, |
| 225 | AWS.Lambda.TerminateMicrovmHttp, |
| 226 | ), |
| 227 | ), |
| 228 | ), |
| 229 | ); |