The best way to control Claude Code, Codex and more from any computer or phone

main
ts 105 lines 3.98 KB
Raw
1 /** @effect-diagnostics anyUnknownInErrorContext:off */
2
3 /**
4 * INTERNAL — the "image source changed" trigger stream shared by the floci
5 * dev providers that rebuild a Docker image on file change
6 * ([ECS FlociTaskProvider](../ECS/FlociTaskProvider.ts),
7 * [ECS FlociServiceProvider](../ECS/FlociServiceProvider.ts),
8 * [Lambda FlociMicrovmImageProvider](../Lambda/FlociMicrovmImageProvider.ts)).
9 * NOT exported from any service `index.ts`.
10 */
11
12 import { isInlineDockerfile } from "@alchemy.run/docker/Dockerfile";
13 import * as Bundle from "alchemy/Bundle/Bundle";
14 import * as Effect from "effect/Effect";
15 import * as FileSystem from "effect/FileSystem";
16 import * as Path from "effect/Path";
17 import * as Stream from "effect/Stream";
18 import {
19 imageSourceKind,
20 makeBunBootstrap,
21 makeImageSource,
22 type BundledImageSource,
23 type ImageSourceLike,
24 } from "../ECR/ImageSource.ts";
25
26 /**
27 * A stream of "the image source changed" triggers for a props bag:
28 *
29 * - `main` — a `Bundle.watch` over the exact module graph the deploy
30 * bundles (rebuild successes are the trigger; rebuild errors are logged
31 * and swallowed);
32 * - `context` — a debounced recursive `fs.watch` of the build context (and
33 * the Dockerfile's directory when it lives outside the context);
34 * - `image` (registry ref) / inline-dockerfile-only — nothing local to
35 * watch: an empty stream.
36 */
37 export const imageSourceTrigger = Effect.fn(function* (options: {
38 /** Logical id, for log prefixes. */
39 id: string;
40 source: ImageSourceLike;
41 isExternal: boolean | undefined;
42 }) {
43 const { source } = options;
44 const kind = imageSourceKind(source);
45
46 if (kind === "main") {
47 const imageSource = yield* makeImageSource;
48 const plan = yield* imageSource.watchMain({
49 source: source as BundledImageSource,
50 isExternal: options.isExternal,
51 bootstrap: makeBunBootstrap(source.handler ?? "default"),
52 });
53 // Rolldown's first build runs when the watcher starts, before any file
54 // changed; the deploy already built that exact graph. Only a build that
55 // follows a change (`Start`) is a trigger — otherwise the rerun would
56 // re-register the image and reap the revision the engine just handed to
57 // dependents.
58 let changed = false;
59 return Bundle.watch(plan.inputOptions, plan.outputOptions, plan.extra).pipe(
60 Stream.tap((event) =>
61 event._tag === "Error"
62 ? Effect.logWarning(`rebuild failed: ${event.error.message}`)
63 : Effect.void,
64 ),
65 Stream.filter((event) => {
66 if (event._tag === "Start") {
67 changed = true;
68 return false;
69 }
70 return event._tag === "Success" && changed;
71 }),
72 Stream.debounce("200 millis"),
73 Stream.map(() => undefined),
74 ) as Stream.Stream<void>;
75 }
76
77 if (kind === "context") {
78 if (source.context === undefined) {
79 // Inline-dockerfile-only builds have no local files to watch — the
80 // content is a prop, and prop changes flow through the dev diff.
81 return Stream.empty as Stream.Stream<void>;
82 }
83 const fs = yield* FileSystem.FileSystem;
84 const path = yield* Path.Path;
85 const context = path.resolve(source.context);
86 const streams: Stream.Stream<unknown, any>[] = [fs.watch(context, { recursive: true })];
87 // A path Dockerfile living OUTSIDE the context dir is watched too.
88 if (source.dockerfile !== undefined && !isInlineDockerfile(source.dockerfile)) {
89 const dockerfile = path.resolve(source.dockerfile);
90 if (!dockerfile.startsWith(`${context}/`)) {
91 streams.push(fs.watch(path.dirname(dockerfile)));
92 }
93 }
94 return Stream.mergeAll(streams, { concurrency: streams.length }).pipe(
95 Stream.debounce("300 millis"),
96 Stream.map(() => undefined),
97 Stream.catchCause((cause) =>
98 Stream.fromEffect(Effect.logWarning(`context watch failed`, cause)).pipe(Stream.drain),
99 ),
100 ) as Stream.Stream<void>;
101 }
102
103 // Registry `image` refs have nothing local to watch.
104 return Stream.empty as Stream.Stream<void>;
105 });