@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, 04:45 UTC

main
ts 486 lines 18.1 KB
Raw
1 import type * as Containers from "@distilled.cloud/cloudflare/containers";
2 import { deepEqual } from "alchemy/Diff";
3 import * as Output from "alchemy/Output";
4 import { normalizeNulls } from "alchemy/Util/stable";
5 import * as Data from "effect/Data";
6 import * as Duration from "effect/Duration";
7 import * as Effect from "effect/Effect";
8 import type {
9 AnyContainerApplicationProps,
10 ContainerApplication,
11 DevContainerImage,
12 DurableObjectApplicationSettings,
13 DurableObjectContainerProps,
14 } from "./ContainerApplication.ts";
15 import type { DurableObjectApplication } from "./DurableObjectApplication.ts";
16
17 export class ContainerConfigurationError extends Data.TaggedError("ContainerConfigurationError")<{
18 readonly message: string;
19 }> {}
20
21 export class ContainerImagePreparationError extends Data.TaggedError(
22 "ContainerImagePreparationError",
23 )<{
24 readonly image: string;
25 readonly status: "pending" | "error";
26 readonly message: string;
27 }> {}
28
29 /**
30 * Two Containers bound to the same Durable Object class declare different
31 * values for one application-wide setting. The class has one container
32 * application, so the declarations must agree (or only one may declare it).
33 */
34 export class ContainerApplicationSettingsConflict extends Data.TaggedError(
35 "ContainerApplicationSettingsConflict",
36 )<{
37 readonly className: string;
38 readonly setting: keyof DurableObjectApplicationSettings;
39 readonly containers: ReadonlyArray<string>;
40 readonly message: string;
41 }> {}
42
43 /**
44 * The Containers bound to one Durable Object class cannot be merged into the
45 * class's single container entry: two images share a key, the class mixes a
46 * fleet Container with Durable Object-managed ones, or it exceeds Cloudflare's
47 * image limit.
48 */
49 export class DurableObjectContainerConflict extends Data.TaggedError(
50 "DurableObjectContainerConflict",
51 )<{
52 readonly className: string;
53 readonly message: string;
54 }> {}
55
56 /**
57 * A Durable Object-managed Container was bound on an async Worker outside a
58 * Durable Object binding, or a Durable Object binding lists a Container it
59 * cannot attach. Durable Object-managed Containers attach to a class through
60 * `Cloudflare.DurableObject(className, { containers })`.
61 */
62 export class DurableObjectContainerBindingError extends Data.TaggedError(
63 "DurableObjectContainerBindingError",
64 )<{
65 readonly bindingName: string;
66 readonly message: string;
67 }> {}
68
69 /**
70 * An async `Cloudflare.DurableObject(className, { containers })` binding
71 * attaches Containers to a class the same Worker implements as an
72 * Effect-native Durable Object. Effect-native classes `yield*` their
73 * Containers instead.
74 */
75 export class DurableObjectContainersOnEffectNativeClass extends Data.TaggedError(
76 "DurableObjectContainersOnEffectNativeClass",
77 )<{
78 readonly className: string;
79 readonly message: string;
80 }> {}
81
82 /**
83 * Fail when an async binding's `containers` target a class the Worker
84 * implements as an Effect-native Durable Object.
85 */
86 export const assertBindingContainersOnAsyncClasses = (
87 rows: ReadonlyArray<{ readonly className: string; readonly attachedBy?: "binding" }>,
88 effectNativeClasses: Iterable<string>,
89 ) => {
90 const effectNative = new Set(effectNativeClasses);
91 const row = rows.find((row) => row.attachedBy === "binding" && effectNative.has(row.className));
92 return row === undefined
93 ? Effect.void
94 : Effect.fail(
95 new DurableObjectContainersOnEffectNativeClass({
96 className: row.className,
97 message: `Durable Object class '${row.className}' is an Effect-native Durable Object, so it cannot take containers from Cloudflare.DurableObject("${row.className}", { containers }). Bind each Container inside the Durable Object with \`yield*\` instead, and remove the containers prop.`,
98 }),
99 );
100 };
101
102 /** Cloudflare's limits for the `images` map of a Durable Object class. */
103 const MAX_NAMED_IMAGES = 100;
104 const MAX_IMAGE_NAME_LENGTH = 128;
105
106 /**
107 * Fleet-scheduled settings that have no meaning when the Durable Object picks
108 * the image and instance size at `start()` time.
109 */
110 const FLEET_ONLY_PROPS = [
111 "name",
112 "className",
113 "main",
114 "instanceType",
115 "instances",
116 "maxInstances",
117 "vcpu",
118 "memory",
119 "memoryMib",
120 "disk",
121 "env",
122 "environmentVariables",
123 "secrets",
124 "labels",
125 "network",
126 "command",
127 "entrypoint",
128 "dns",
129 "ports",
130 "checks",
131 "constraints",
132 "affinities",
133 "rollout",
134 "registryId",
135 "sshPublicKeyIds",
136 ] as const satisfies ReadonlyArray<keyof AnyContainerApplicationProps>;
137
138 export const isDurableObjectContainer = (
139 props: AnyContainerApplicationProps,
140 ): props is DurableObjectContainerProps => props.schedulingPolicy === "durable_object";
141
142 const invalid = (message: string) => Effect.fail(new ContainerConfigurationError({ message }));
143
144 /** Validate a bounded preparation deadline before publishing any images. */
145 export const imagePreparationTimeoutMillis = (timeout: Duration.Input = "30 minutes") =>
146 Effect.try({
147 try: () => {
148 const millis = Duration.toMillis(timeout);
149 if (!Number.isFinite(millis) || millis <= 0) throw new Error("Invalid timeout");
150 return millis;
151 },
152 catch: () =>
153 new ContainerConfigurationError({
154 message: "imagePreparationTimeout must be a finite, positive duration.",
155 }),
156 });
157
158 /**
159 * Validate container props before publishing images or touching the hosting
160 * Worker. Reject switches between fleet scheduling and Durable Object scheduling.
161 */
162 export const validateContainerConfiguration = Effect.fn(function* (
163 props: AnyContainerApplicationProps,
164 deployedPolicy?: string,
165 ) {
166 const policy = props.schedulingPolicy ?? "default";
167 if (
168 deployedPolicy !== undefined &&
169 (deployedPolicy === "durable_object") !== (policy === "durable_object")
170 ) {
171 return yield* invalid(
172 "A container cannot switch between fleet and Durable Object scheduling in place. Declare a new container application and Durable Object class, then move traffic to the new namespace. Existing Durable Object storage is not transferred.",
173 );
174 }
175
176 // `images` was removed: one Container is one image. Untyped or
177 // Effect-produced props can still carry it.
178 if ((props as { images?: unknown }).images !== undefined) {
179 return yield* invalid(
180 "Container property 'images' was removed. Declare one Cloudflare.Container per image with schedulingPolicy: 'durable_object' and bind each one to the Durable Object; the image key is the Container's id.",
181 );
182 }
183
184 if (!isDurableObjectContainer(props)) return;
185
186 yield* imagePreparationTimeoutMillis(props.imagePreparationTimeout);
187 if (
188 props.image !== undefined &&
189 (props.context !== undefined || props.dockerfile !== undefined)
190 ) {
191 return yield* invalid(
192 "A Durable Object-managed container declares one image source: `image`, or `context`/`dockerfile`.",
193 );
194 }
195
196 // The union type already forbids these, but untyped or Effect-produced
197 // props can still carry them.
198 const loose: AnyContainerApplicationProps = props;
199 const unsupported = FLEET_ONLY_PROPS.find((key) => loose[key] !== undefined);
200 if (unsupported !== undefined) {
201 return yield* invalid(
202 `Container property '${unsupported}' is not supported with schedulingPolicy: 'durable_object'. The application is named and owned by the Durable Object class: attach the Container with Cloudflare.DurableObject(className, { containers }) on an async Worker, or yield it inside an Effect-native Durable Object. Pass per-instance settings to start().`,
203 );
204 }
205 });
206
207 /** Whether a Durable Object-managed Container declares an image to publish. */
208 export const hasImageSource = (props: DurableObjectContainerProps) =>
209 props.image !== undefined || props.context !== undefined || props.dockerfile !== undefined;
210
211 /** Read the scheduling policy off a declared Container resource's props. */
212 export const isDurableObjectContainerResource = (container: ContainerApplication) => {
213 const props = container.Props as { schedulingPolicy?: unknown } | undefined;
214 return props?.schedulingPolicy === "durable_object";
215 };
216
217 /** The application-wide settings a Container contributes to its Durable Object class. */
218 export const containerApplicationSettings = (container: ContainerApplication) => {
219 const props = container.Props as DurableObjectApplicationSettings | undefined;
220 return {
221 ssh: props?.ssh,
222 authorizedKeys: props?.authorizedKeys,
223 observability: props?.observability,
224 };
225 };
226
227 const SETTING_KEYS = ["ssh", "authorizedKeys", "observability"] as const;
228
229 /**
230 * Merge the application-wide settings every bound Container declares. A
231 * setting declared by several Containers must have the same value.
232 */
233 export const mergeApplicationSettings = (
234 className: string,
235 contributions: ReadonlyArray<{
236 readonly id: string;
237 readonly settings: DurableObjectApplicationSettings | undefined;
238 }>,
239 ) =>
240 Effect.gen(function* () {
241 const merged: DurableObjectApplicationSettings = {};
242 for (const setting of SETTING_KEYS) {
243 const declared = contributions.flatMap(({ id, settings }) => {
244 const value = normalizeNulls(settings?.[setting]);
245 return value === undefined ? [] : [{ id, value }];
246 });
247 const [first] = declared;
248 if (first === undefined) continue;
249 const conflicting = declared.find(({ value }) => !deepEqual(value, first.value));
250 if (conflicting !== undefined) {
251 return yield* new ContainerApplicationSettingsConflict({
252 className,
253 setting,
254 containers: declared.map(({ id }) => id),
255 message: `Containers '${first.id}' and '${conflicting.id}' declare different '${setting}' for Durable Object class '${className}'. A class has one container application, so its Containers must agree on application-wide settings; declare '${setting}' on one Container or give them the same value.`,
256 });
257 }
258 Object.assign(merged, { [setting]: first.value });
259 }
260 return merged;
261 });
262
263 /**
264 * One row of a Worker's `containers` binding data: the container a Durable
265 * Object class is backed by.
266 */
267 export interface WorkerContainerRow {
268 readonly className: string;
269 /** Set when an async `Cloudflare.DurableObject(name, { containers })` binding contributed the row. */
270 readonly attachedBy?: "binding";
271 /** Application name, set for a Durable Object-managed class. */
272 readonly name?: string;
273 /** Prepared images keyed by Container id (Durable Object-managed). */
274 readonly images?: Record<string, string>;
275 /** Local image sources keyed by Container id (Durable Object-managed). */
276 readonly devImages?: Record<string, DevContainerImage>;
277 /** The fleet container's local image. */
278 readonly dev: DevContainerImage | undefined;
279 readonly hash: string | undefined;
280 }
281
282 const isDurableObjectRow = (row: Pick<WorkerContainerRow, "name" | "images" | "devImages">) =>
283 row.name !== undefined || row.images !== undefined || row.devImages !== undefined;
284
285 const mergeImageMaps = <V>(
286 className: string,
287 label: string,
288 maps: ReadonlyArray<Record<string, V> | undefined>,
289 ) =>
290 Effect.gen(function* () {
291 if (maps.every((map) => map === undefined)) return undefined;
292 const merged: Record<string, V> = {};
293 for (const map of maps) {
294 for (const [key, value] of Object.entries(map ?? {})) {
295 if (key in merged && !deepEqual(merged[key], value)) {
296 return yield* new DurableObjectContainerConflict({
297 className,
298 message: `Two Containers bound to Durable Object class '${className}' publish different ${label} under the key '${key}'. The image key is the Container's id, so give each Container a distinct id.`,
299 });
300 }
301 merged[key] = value;
302 }
303 }
304 return merged;
305 });
306
307 /**
308 * Merge a Worker's `containers` rows into one entry per Durable Object class.
309 * A Durable Object-managed class unions the images of every bound Container;
310 * a fleet class is backed by exactly one Container.
311 */
312 export const mergeWorkerContainers = <
313 Row extends Partial<WorkerContainerRow> & { className: string },
314 >(
315 rows: ReadonlyArray<Row>,
316 ) =>
317 Effect.gen(function* () {
318 const byClass = new Map<string, Row[]>();
319 for (const row of rows) {
320 const existing = byClass.get(row.className);
321 if (existing) existing.push(row);
322 else byClass.set(row.className, [row]);
323 }
324 const merged: Row[] = [];
325 for (const [className, classRows] of byClass) {
326 const [first, ...rest] = classRows as [Row, ...Row[]];
327 const managed = classRows.filter(isDurableObjectRow);
328 if (managed.length === 0) {
329 // Fleet: rows for the same Container collapse; distinct ones conflict.
330 if (rest.some((row) => !deepEqual(row, first))) {
331 return yield* new DurableObjectContainerConflict({
332 className,
333 message: `Durable Object class '${className}' is bound to more than one fleet Container. Only Durable Object-managed Containers (schedulingPolicy: 'durable_object') can share a class.`,
334 });
335 }
336 merged.push(first);
337 continue;
338 }
339 if (managed.length !== classRows.length) {
340 return yield* new DurableObjectContainerConflict({
341 className,
342 message: `Durable Object class '${className}' mixes a fleet Container with Durable Object-managed Containers. Every Container bound to one class must use schedulingPolicy: 'durable_object'.`,
343 });
344 }
345 const names = [
346 ...new Set(managed.flatMap((row) => (row.name === undefined ? [] : [row.name]))),
347 ];
348 if (names.length > 1) {
349 return yield* new DurableObjectContainerConflict({
350 className,
351 message: `Durable Object class '${className}' resolves to more than one container application (${names.join(", ")}).`,
352 });
353 }
354 const images = yield* mergeImageMaps(
355 className,
356 "images",
357 managed.map((row) => row.images),
358 );
359 const devImages = yield* mergeImageMaps(
360 className,
361 "local images",
362 managed.map((row) => row.devImages),
363 );
364 const keys = Object.keys(images ?? devImages ?? {});
365 if (keys.length > MAX_NAMED_IMAGES) {
366 return yield* new DurableObjectContainerConflict({
367 className,
368 message: `Durable Object class '${className}' is bound to ${keys.length} container images; Cloudflare allows at most ${MAX_NAMED_IMAGES}.`,
369 });
370 }
371 const badKey = keys.find((key) => key.length === 0 || key.length > MAX_IMAGE_NAME_LENGTH);
372 if (badKey !== undefined) {
373 return yield* new DurableObjectContainerConflict({
374 className,
375 message: `Container id '${badKey}' is used as an image key and must contain between 1 and ${MAX_IMAGE_NAME_LENGTH} characters.`,
376 });
377 }
378 const hashes = managed.flatMap((row) => (row.hash === undefined ? [] : [row.hash])).sort();
379 merged.push({
380 ...first,
381 name: names[0],
382 images,
383 devImages,
384 dev: undefined,
385 hash: hashes.length === 0 ? undefined : hashes.join(","),
386 });
387 }
388 return merged;
389 });
390
391 /**
392 * The `containers` entry a fleet Container contributes to the Durable Object
393 * class it backs.
394 */
395 export const workerContainerBinding = (className: string, application: ContainerApplication) => ({
396 className,
397 dev: application.dev,
398 hash: application.hash.pipe(Output.map((hash) => hash?.image)),
399 });
400
401 /**
402 * The `containers` entry a Durable Object-managed Container contributes to
403 * its class: the class's application name plus this Container's one image,
404 * keyed by the Container's id. The Worker merges every entry of the class.
405 */
406 export const durableObjectContainerBinding = (
407 className: string,
408 application: DurableObjectApplication,
409 container: ContainerApplication,
410 ) => {
411 const key = container.LogicalId;
412 return {
413 className,
414 name: application.applicationName,
415 images: container.image.pipe(
416 Output.map((image): Record<string, string> => (image === undefined ? {} : { [key]: image })),
417 ),
418 devImages: container.dev.pipe(
419 Output.map((dev): Record<string, DevContainerImage> =>
420 dev === undefined ? {} : { [key]: dev },
421 ),
422 ),
423 dev: undefined,
424 hash: container.hash.pipe(Output.map((hash) => hash?.image)),
425 };
426 };
427
428 /**
429 * Converge declared settings and reset settings removed from a prior declaration.
430 * Omitted settings on new or adopted applications remain unmanaged.
431 */
432 export const durableObjectSettingsPatch = (
433 desired: DurableObjectApplicationSettings,
434 previous: DurableObjectApplicationSettings | undefined,
435 observed: Pick<Containers.GetContainerApplicationResponse, "configuration" | "observability">,
436 ): Pick<Containers.UpdateContainerApplicationRequest, "configuration" | "observability"> => {
437 const configuration: Containers.DurableObjectContainerConfiguration = {
438 // Cloudflare names the SSH setting after the CLI that connects to it.
439 ...(desired.ssh !== undefined
440 ? { wranglerSsh: desired.ssh }
441 : previous?.ssh !== undefined
442 ? { wranglerSsh: { enabled: false } }
443 : {}),
444 ...(desired.authorizedKeys !== undefined
445 ? { authorizedKeys: desired.authorizedKeys }
446 : previous?.authorizedKeys !== undefined
447 ? { authorizedKeys: [] }
448 : {}),
449 };
450 const observability =
451 desired.observability ??
452 (previous?.observability !== undefined ? { logs: { enabled: false } } : undefined);
453 const configurationChanged = Object.entries(configuration).some(
454 ([key, value]) =>
455 !deepEqual(
456 value,
457 normalizeNulls(observed.configuration?.[key as keyof typeof configuration]),
458 ),
459 );
460 return {
461 ...(configurationChanged ? { configuration } : {}),
462 ...(observability !== undefined &&
463 !deepEqual(observability, normalizeNulls(observed.observability))
464 ? { observability }
465 : {}),
466 };
467 };
468
469 /** Common attributes before a Durable Object-managed application exists remotely. */
470 export const durableObjectPlaceholder = (
471 identity: Pick<
472 ContainerApplication["Attributes"],
473 "applicationId" | "applicationName" | "accountId" | "createdAt" | "observability"
474 >,
475 ): ContainerApplication["Attributes"] => ({
476 ...identity,
477 schedulingPolicy: "durable_object",
478 instances: undefined,
479 maxInstances: undefined,
480 constraints: undefined,
481 affinities: undefined,
482 configuration: {},
483 durableObjects: undefined,
484 version: undefined,
485 dev: undefined,
486 });