@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 06:00 UTC

main
ts 233 lines 9.9 KB
Raw
1 /** @effect-diagnostics anyUnknownInErrorContext:off */
2
3 /**
4 * The `alchemy dev` provider for `AWS.Lambda.MicrovmImage`: builds the image
5 * as a plain LOCAL `docker build` and hands the emulator a pre-built
6 * reference, so an image reload costs what a `docker build` costs — with
7 * real BuildKit layer caching against the user's actual sources.
8 *
9 * The live flow (zip the context → upload to S3 → floci downloads, extracts
10 * to a fresh temp dir, and docker-builds server-side) is faithful to the
11 * real AWS API but hostile to dev: the fresh extract defeated layer caching
12 * and every content edit cost a full image build (~60s measured). Here the
13 * flow is:
14 *
15 * 1. bundle (`main`) or use the user's context/Dockerfile as-is;
16 * 2. `docker build -t alchemy-dev/microvm-<name>:<contentHash>` on the
17 * HOST daemon — unchanged layers cache, so a well-ordered Dockerfile
18 * only rebuilds its tail (the alchemy-generated Dockerfile installs the
19 * runtime BEFORE copying the bundle for exactly this reason);
20 * 3. delegate to the LIVE reconcile with
21 * `codeArtifact: { uri: "docker://<tag>" }` — the alchemy floci fork
22 * (≥ the docker-artifact build) short-circuits its build for that form
23 * and marks the version ACTIVE immediately.
24 *
25 * `RunMicrovm` defaults to the latest ACTIVE version, so every VM booted
26 * after a rebuild runs the new image; running VMs are deliberately NOT
27 * terminated — MicroVM sessions are stateful, and killing a user's live
28 * shell/sandbox on every source edit would be worse than serving the old
29 * code until it re-runs.
30 *
31 * Built on the shared dev-watch skeleton
32 * ([DevWatchProvider](../Local/DevWatchProvider.ts)); the watch trigger is
33 * the shared [ImageSourceTrigger](../Local/ImageSourceTrigger.ts) (`main` →
34 * rolldown module-graph watch, `context` → fs-watch incl. an
35 * outside-context Dockerfile).
36 */
37
38 import { Docker, DockerLive } from "@alchemy.run/docker/Docker";
39 import { isInlineDockerfile } from "@alchemy.run/docker/Dockerfile";
40 import { AlchemyContext } from "alchemy/AlchemyContext";
41 import * as Artifacts from "alchemy/Artifacts";
42 import { getStableContextDir } from "alchemy/Bundle/TempRoot";
43 import { hashDirectory } from "alchemy/Command/Memo";
44 import { sha256 } from "alchemy/Util/sha256";
45 import * as Effect from "effect/Effect";
46 import * as FileSystem from "effect/FileSystem";
47 import * as Path from "effect/Path";
48 import * as Stream from "effect/Stream";
49 import type { ImageSourceLike } from "../ECR/ImageSource.ts";
50 import { flociProvidersUrl, makeDevWatchProvider } from "../Local/DevWatchProvider.ts";
51 import { imageSourceTrigger } from "../Local/ImageSourceTrigger.ts";
52 import {
53 buildMicrovmDockerfile,
54 bundleMicrovmProgram,
55 DEFAULT_MICROVM_PORT,
56 MICROVM_BASE_DOCKER_IMAGE,
57 } from "./MicrovmBundle.ts";
58 import { MicrovmImage, type MicrovmImageProps } from "./MicrovmImage.ts";
59 import { MicrovmImageProvider } from "./MicrovmProvider.ts";
60
61 /**
62 * The AWS-managed MicroVM base is not publicly pullable; the floci fork
63 * rewrites `FROM public.ecr.aws/lambda/microvms:*` to this Amazon Linux
64 * 2023 mirror in its own builds, and host-side dev builds must match so
65 * both paths produce the same image.
66 */
67 const LOCAL_MICROVM_BASE_IMAGE =
68 process.env.FLOCI_MICROVM_BASE_IMAGE || "public.ecr.aws/amazonlinux/amazonlinux:2023";
69
70 const rewriteBaseImage = (dockerfile: string): string =>
71 dockerfile
72 .split("\n")
73 .map((line) => {
74 const trimmed = line.trimStart();
75 if (!/^from\s/i.test(trimmed)) return line;
76 const rest = trimmed.slice(5).trimStart();
77 if (!rest.startsWith(MICROVM_BASE_DOCKER_IMAGE.split(":")[0])) {
78 return line;
79 }
80 const space = rest.indexOf(" ");
81 const suffix = space >= 0 ? rest.slice(space) : "";
82 return `FROM ${LOCAL_MICROVM_BASE_IMAGE}${suffix}`;
83 })
84 .join("\n");
85
86 /**
87 * Build the image on the host daemon and return the content-addressed
88 * `docker://` artifact reference for it. The tag embeds the content hash,
89 * so an unchanged build is a pure cache hit AND the live reconcile's
90 * artifact-hash comparison (`sha256(uri:propsId)`) sees content changes.
91 */
92 const buildLocalImage = Effect.fn(function* (id: string, news: MicrovmImageProps) {
93 const docker = yield* Docker;
94 const fs = yield* FileSystem.FileSystem;
95 const path = yield* Path.Path;
96 const { dotAlchemy } = yield* AlchemyContext;
97 const runtime = news.runtime ?? "node";
98 const port = news.port ?? DEFAULT_MICROVM_PORT;
99
100 if (news.main) {
101 const { files, hash: bundleHash } = yield* bundleMicrovmProgram({
102 main: news.main,
103 runtime,
104 isExternal: news.isExternal ?? false,
105 external: news.external,
106 port,
107 build: news.build,
108 });
109 const userDockerfile =
110 news.dockerfile !== undefined && isInlineDockerfile(news.dockerfile)
111 ? (news.dockerfile.content as string)
112 : news.dockerfile;
113 const dockerfile = rewriteBaseImage(
114 buildMicrovmDockerfile(userDockerfile, runtime, port, {
115 sourceMaps: files.some((f) => f.path.endsWith(".map")),
116 }),
117 );
118 const contentHash = yield* sha256(`${bundleHash}:${dockerfile}`);
119 const context = yield* getStableContextDir(process.cwd(), dotAlchemy, `${id}-microvm`);
120 yield* docker.materialize({
121 context,
122 dockerfile,
123 files: files.map((f) => ({ path: f.path, content: f.content })),
124 });
125 const tag = `alchemy-dev/microvm-${id.toLowerCase()}:${contentHash.slice(0, 16)}`;
126 yield* docker.image.build({ context, tag });
127 return tag;
128 }
129
130 if (news.context) {
131 const context = yield* fs.realPath(news.context);
132 const dockerfilePath =
133 news.dockerfile !== undefined && !isInlineDockerfile(news.dockerfile)
134 ? yield* fs.realPath(news.dockerfile)
135 : path.join(context, "Dockerfile");
136 const original = yield* fs.readFileString(dockerfilePath);
137 const rewritten = rewriteBaseImage(original);
138 const contextHash = yield* hashDirectory({ cwd: context });
139 const contentHash = yield* sha256(`${contextHash}:${rewritten}`);
140 // The rewritten Dockerfile lives OUTSIDE the user's context (never
141 // touch their files); `docker build -f` accepts that.
142 const staging = yield* getStableContextDir(process.cwd(), dotAlchemy, `${id}-microvm`);
143 const rewrittenPath = path.join(staging, "Dockerfile");
144 yield* fs.writeFileString(rewrittenPath, rewritten);
145 const tag = `alchemy-dev/microvm-${id.toLowerCase()}:${contentHash.slice(0, 16)}`;
146 yield* docker.image.build({ context, file: rewrittenPath, tag });
147 return tag;
148 }
149
150 return undefined;
151 });
152
153 export const FlociMicrovmImageProvider = () =>
154 makeDevWatchProvider<MicrovmImage, MicrovmImageProps, MicrovmImage["Attributes"]>(
155 MicrovmImage,
156 flociProvidersUrl(),
157 {
158 liveProvider: () => MicrovmImageProvider(),
159 services: DockerLive,
160 // The restart surface of the watch loop: everything that changes WHAT
161 // is built (props flow through the reconcile anyway).
162 watchConfigOf: (news, attrs) => {
163 const source = news as ImageSourceLike;
164 return {
165 name: attrs.name,
166 main: source.main,
167 context: source.context,
168 dockerfile: source.dockerfile,
169 codeArtifactUri: news.codeArtifact?.uri,
170 runtime: news.runtime,
171 port: news.port,
172 isExternal: news.isExternal,
173 external: news.external,
174 build: news.build,
175 };
176 },
177 // Mirrors the live diff: the image name is the identity.
178 replaceOn: ({ id: _id, olds, news }) =>
179 Effect.sync(() =>
180 (olds.name ?? null) !== (news.name ?? null) ? { action: "replace" as const } : undefined,
181 ),
182 // Build locally, delegate with a pre-built reference. A user-supplied
183 // `codeArtifact.uri` passes through untouched (already pre-built).
184 transformReconcileNews: ({ id, news }) =>
185 Effect.gen(function* () {
186 const tag = yield* buildLocalImage(id, news);
187 if (tag === undefined) return news;
188 // The live reconcile memoizes its resolved artifact per id for the
189 // run (`Artifacts.cached`) — but the sidecar's artifact store lives
190 // for the whole dev session, so a rebuilt docker:// reference would
191 // read back the FIRST reconcile's artifact and no-op forever. Evict
192 // so the reconcile resolves the fresh reference.
193 yield* (yield* Artifacts.Artifacts).delete(`microvm-image-content:${id}`);
194 return {
195 ...news,
196 main: undefined,
197 context: undefined,
198 dockerfile: undefined,
199 codeArtifact: { uri: `docker://${tag}` },
200 } satisfies MicrovmImageProps;
201 }),
202 startWatch: (ctx) =>
203 Effect.gen(function* () {
204 const trigger = yield* imageSourceTrigger({
205 id: ctx.id,
206 source: ctx.news as ImageSourceLike,
207 isExternal: ctx.news.isExternal,
208 });
209 yield* trigger.pipe(
210 // The re-run builds (cached) and re-reconciles; floci marks the
211 // new version ACTIVE immediately, and the next RunMicrovm boots
212 // it. Running VMs keep their session (deliberately not killed).
213 Stream.runForEach(() =>
214 Effect.gen(function* () {
215 const startedAt = Date.now();
216 yield* Effect.logInfo(`microvm source changed — rebuilding`);
217 const previous = yield* ctx.currentAttrs;
218 const attrs = yield* ctx.rerunReconcile;
219 if (attrs.codeArtifact?.hash !== previous.codeArtifact?.hash) {
220 yield* Effect.logInfo(
221 `microvm image rebuilt (v${attrs.latestActiveImageVersion}) in ${Date.now() - startedAt}ms`,
222 );
223 }
224 }).pipe(
225 Effect.catchCause((cause) =>
226 Effect.logWarning(`microvm image rebuild failed`, cause),
227 ),
228 ),
229 ),
230 );
231 }),
232 },
233 );