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

main
ts 1,898 lines 76.1 KB
Raw
1 import { hashDockerBuildInputs } from "@alchemy.run/docker/BuildHash";
2 import { Docker } from "@alchemy.run/docker/Docker";
3 import { isInlineDockerfile } from "@alchemy.run/docker/Dockerfile";
4 import { repositoryFromImageRef } from "@alchemy.run/docker/Registry";
5 import * as Containers from "@distilled.cloud/cloudflare/containers";
6 import { Unowned, Unresolved, UnresolvedIdentity } from "alchemy/AdoptPolicy";
7 import { AlchemyContext } from "alchemy/AlchemyContext";
8 import { getStableContextDir } from "alchemy/Bundle/TempRoot";
9 import { deepEqual, isResolved } from "alchemy/Diff";
10 import * as Provider from "alchemy/Provider";
11 import type { ScopedPlanStatusSession } from "alchemy/Report";
12 import { type ResourceBinding } from "alchemy/Resource";
13 import { sha256Object } from "alchemy/Util/sha256";
14 import { normalizeNulls } from "alchemy/Util/stable";
15 import * as Data from "effect/Data";
16 import * as Effect from "effect/Effect";
17 import * as Exit from "effect/Exit";
18 import * as FileSystem from "effect/FileSystem";
19 import * as HttpClient from "effect/http/HttpClient";
20 import * as HttpClientRequest from "effect/http/HttpClientRequest";
21 import * as Path from "effect/Path";
22 import * as Redacted from "effect/Redacted";
23 import * as Schedule from "effect/Schedule";
24 import * as Schema from "effect/Schema";
25 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
26 import { isLiveId } from "../LocalRuntime.ts";
27 import { CloudflareLogs, type TelemetryFilter } from "../Logs.ts";
28 import type {
29 AnyContainerApplicationProps,
30 ContainerApplication,
31 DurableObjectContainerProps,
32 } from "./ContainerApplication.ts";
33 import {
34 buildFinalDockerfile,
35 bundleContainerProgram,
36 containerEnvPreamble,
37 createContainerApplicationName,
38 makeContainerEnv,
39 materializeInlineDockerfileContext,
40 validateContainerImageProps,
41 } from "./ContainerBundle.ts";
42 import {
43 ContainerConfigurationError,
44 durableObjectPlaceholder,
45 hasImageSource,
46 isDurableObjectContainer,
47 validateContainerConfiguration,
48 } from "./ContainerConfiguration.ts";
49 import { waitForContainerImage } from "./ContainerImagePreparation.ts";
50 import { ContainerPlatform } from "./ContainerPlatform.ts";
51 import { retryContainerBuild, retryContainerPublication } from "./ContainerPublication.ts";
52 import { retryNamespacePropagation } from "./DurableObjectApplication.ts";
53
54 export class ContainerProviderConflict extends Data.TaggedError("ContainerProviderConflict")<{
55 readonly message: string;
56 readonly cause?: unknown;
57 }> {}
58
59 export class ContainerProviderOperationFailed extends Data.TaggedError(
60 "ContainerProviderOperationFailed",
61 )<{
62 readonly message: string;
63 readonly cause?: unknown;
64 }> {}
65
66 export class ContainerProviderUnexpectedResponse extends Data.TaggedError(
67 "ContainerProviderUnexpectedResponse",
68 )<{
69 readonly message: string;
70 readonly cause?: unknown;
71 }> {}
72
73 /**
74 * The image source resolved from a {@link ContainerApplicationProps}. Selects
75 * one of three strategies used by `buildAndPushImage`:
76 *
77 * - `effectful` — bundle an Effect-native `main` and build a generated image.
78 * - `external` — build a user-supplied Dockerfile against a context directory.
79 * - `remote` — pull a pre-built remote image and re-push it to Cloudflare.
80 * - `prepushed` — the image already lives in the target registry; use the
81 * reference as-is with no docker pull/build/push at all.
82 */
83 type ImageBuild =
84 | {
85 readonly kind: "effectful";
86 readonly files: ReadonlyArray<{ path: string; content: Uint8Array }>;
87 }
88 | {
89 readonly kind: "external";
90 readonly context: string;
91 readonly dockerfile: string;
92 readonly buildArgs?: Record<string, string>;
93 readonly buildSecrets?: Record<string, Redacted.Redacted<string>>;
94 }
95 | {
96 readonly kind: "remote";
97 readonly image: string;
98 }
99 | {
100 readonly kind: "prepushed";
101 readonly image: string;
102 };
103
104 /**
105 * Whether an image reference already points at the target registry host —
106 * e.g. `registry.cloudflare.com/<accountId>/repo@sha256:...` pushed by CI.
107 * Such references are deployed as-is; there is nothing to pull or push.
108 */
109 const isTargetRegistryRef = (image: string, registryId: string) =>
110 image.startsWith(`${registryId}/`);
111
112 /**
113 * Insert the account namespace into a Cloudflare-registry reference that
114 * omits it (`registry.cloudflare.com/app:tag` →
115 * `registry.cloudflare.com/<accountId>/app:tag`), mirroring wrangler's
116 * `resolveImageName`. Custom registries are left untouched — the account
117 * namespace rule is specific to Cloudflare's managed registry.
118 */
119 const normalizePrepushedRef = (image: string, registryId: string, accountId: string) => {
120 if (registryId !== "registry.cloudflare.com") return image;
121 const rest = image.slice(registryId.length + 1);
122 const first = rest.split("/")[0];
123 return first !== undefined && /^[a-f0-9]{32}$/.test(first)
124 ? image
125 : `${registryId}/${accountId}/${rest}`;
126 };
127
128 const RegistryDigest = Schema.String.pipe(
129 Schema.check(Schema.isPattern(/^[a-z0-9]+:[a-f0-9]{64}$/i)),
130 );
131 const isRegistryDigest = Schema.is(RegistryDigest);
132
133 class ContainerRegistryError extends Schema.TaggedError<ContainerRegistryError>()(
134 "ContainerRegistryError",
135 {
136 reason: Schema.Literals([
137 "CredentialsMissingUsername",
138 "ImageOutsideRegistry",
139 "InvalidImageReference",
140 "ImageNotFound",
141 "ManifestRequestFailed",
142 "InvalidManifestDigest",
143 ]),
144 message: Schema.String,
145 imageRef: Schema.optional(Schema.String),
146 cause: Schema.optional(Schema.Defect({ includeStack: true })),
147 },
148 ) {}
149
150 /** The explicit application name. Durable Object-managed Containers have none. */
151 const nameOf = (props: AnyContainerApplicationProps | undefined) => props?.name;
152
153 const digestFromImageRef = (imageRef: string) => {
154 const separator = imageRef.lastIndexOf("@");
155 if (separator === -1) return undefined;
156 const digest = imageRef.slice(separator + 1);
157 return isRegistryDigest(digest) ? digest : undefined;
158 };
159
160 export const LiveContainerProvider = () =>
161 Provider.effect(
162 ContainerPlatform,
163 Effect.gen(function* () {
164 const { dotAlchemy } = yield* AlchemyContext;
165 const fs = yield* FileSystem.FileSystem;
166 const path = yield* Path.Path;
167 const docker = yield* Docker;
168 const http = yield* HttpClient.HttpClient;
169
170 const telemetry = yield* CloudflareLogs;
171
172 const createApplicationName = createContainerApplicationName;
173
174 const findApplicationByName = Effect.fn(function* (name: string) {
175 const { accountId } = yield* yield* CloudflareEnvironment;
176
177 return yield* Containers.listContainerApplications({ accountId }).pipe(
178 Effect.map((apps) => apps.find((app) => app.name === name)),
179 );
180 });
181
182 const findApplicationByNamespace = Effect.fn(function* (namespaceId: string) {
183 const { accountId } = yield* yield* CloudflareEnvironment;
184
185 return yield* Containers.listContainerApplications({ accountId }).pipe(
186 Effect.map((apps) => apps.find((app) => app.durableObjects?.namespaceId === namespaceId)),
187 );
188 });
189
190 // After deleting an application by id, Cloudflare's account-scoped
191 // `list` endpoint stays eventually-consistent for a short window and can
192 // keep returning the now-deleted row. A subsequent recreate that
193 // re-discovers the application by name (`createApplication`) would then
194 // "adopt" that stale row and try to UPDATE it — which fails permanently
195 // with `ContainerApplicationNotFound` (the app is really gone) and
196 // exhausts the readiness retry. Block until the deleted id no longer
197 // appears under that name (or a different id has taken the name, i.e. a
198 // concurrent recreate) before proceeding. Bounded by the readiness
199 // schedule; if it never clears we fall through and let create handle it.
200 const waitForApplicationDeleted = (name: string, deletedId: string) =>
201 findApplicationByName(name).pipe(
202 Effect.repeat({
203 schedule: containerApplicationReadinessSchedule,
204 until: (app) => app?.id !== deletedId,
205 }),
206 Effect.asVoid,
207 );
208
209 const desiredConfiguration = (
210 props: AnyContainerApplicationProps,
211 env: Record<string, string | Redacted.Redacted<string>>,
212 imageRef: string,
213 ) =>
214 normalizeNulls({
215 image: imageRef,
216 // Default to wrangler's instance type ("lite") so containers schedule
217 // the same way out of the box. `instance_type` is mutually exclusive
218 // with explicit vcpu/memory/disk, so only default it when none are
219 // set. ("dev" is wrangler's deprecated alias for "lite".)
220 instanceType:
221 props.instanceType ??
222 (props.vcpu === undefined &&
223 props.memory === undefined &&
224 props.memoryMib === undefined &&
225 props.disk === undefined
226 ? "lite"
227 : undefined),
228 observability: props.observability,
229 sshPublicKeyIds: props.sshPublicKeyIds,
230 secrets: props.secrets,
231 vcpu: props.vcpu,
232 memory: props.memory,
233 memoryMib: props.memoryMib,
234 disk: props.disk,
235 environmentVariables: Object.entries(env).map(([name, value]) => ({
236 name,
237 value: Redacted.isRedacted(value) ? Redacted.value(value) : value,
238 })),
239 labels: props.labels,
240 network: props.network,
241 command: props.command,
242 entrypoint: props.entrypoint,
243 dns: props.dns,
244 ports: props.ports,
245 checks: props.checks,
246 }) as ContainerApplication.Configuration;
247
248 // Scaling/placement defaults mirror wrangler's container defaults
249 // (`wrangler-dist/cli.js`) so an Alchemy container behaves like a
250 // `wrangler deploy`d one without extra config:
251 // - max_instances: 20 (`container.max_instances ?? 20`)
252 // - instances: 0 (wrangler forces 0 whenever
253 // max_instances is set, which we always
254 // do — pure scale-from-zero)
255 // - scheduling_policy: "default"
256 // (wrangler also defaults `constraints.tiers` to `[1, 2]`, but the
257 // distilled SDK models constraints as singular `tier`, not the `tiers`
258 // array, so we leave constraints untouched — it's a minor placement hint
259 // next to the scaling defaults.)
260 // A maxInstances default of 1 (the previous value) silently serialised
261 // every Durable Object instance through a single container slot, which is
262 // the dominant cause of "containers are slow under load".
263 const scalingDefaults = (props: AnyContainerApplicationProps) => ({
264 instances: props.instances ?? 0,
265 maxInstances: props.maxInstances ?? 20,
266 schedulingPolicy: props.schedulingPolicy ?? "default",
267 constraints: props.constraints ?? {},
268 });
269
270 const applicationConfigurationHash = Effect.fn("applicationConfigurationHash")(function* (
271 scaling: ReturnType<typeof scalingDefaults>,
272 affinities: ContainerApplication.Affinities | undefined,
273 configuration: ContainerApplication.Configuration,
274 ) {
275 return yield* sha256Object({
276 scaling,
277 affinities: normalizeNulls(affinities),
278 configuration,
279 });
280 });
281
282 const registryCredentials = Effect.fn("registryCredentials")(function* (
283 props: AnyContainerApplicationProps,
284 permissions: Array<"pull" | "push">,
285 ) {
286 const { accountId } = yield* yield* CloudflareEnvironment;
287 const registryId = props.registryId ?? "registry.cloudflare.com";
288 const credentials = yield* Containers.createContainerRegistryCredentials({
289 accountId,
290 registryId,
291 permissions,
292 expirationMinutes: 60,
293 });
294 const username = credentials.username ?? credentials.user;
295 if (!username) {
296 return yield* ContainerRegistryError.make({
297 reason: "CredentialsMissingUsername",
298 message: `Cloudflare registry ${registryId} did not return a username`,
299 });
300 }
301 return {
302 server: registryId,
303 username,
304 password: credentials.password,
305 };
306 });
307
308 const resolveRegistryDigest = Effect.fn("resolveRegistryDigest")(function* (
309 imageRef: string,
310 credentials: {
311 server: string;
312 username: string;
313 password: string | Redacted.Redacted<string>;
314 },
315 ) {
316 const embeddedDigest = digestFromImageRef(imageRef);
317 if (embeddedDigest !== undefined) return embeddedDigest;
318
319 const registryHost = credentials.server.replace(/^https?:\/\//, "").replace(/\/$/, "");
320 if (!imageRef.startsWith(`${registryHost}/`)) {
321 return yield* ContainerRegistryError.make({
322 reason: "ImageOutsideRegistry",
323 message: `Cannot resolve an image outside registry ${registryHost}`,
324 imageRef,
325 });
326 }
327 const repositoryAndTag = imageRef.slice(registryHost.length + 1);
328 const tagSeparator = repositoryAndTag.lastIndexOf(":");
329 if (tagSeparator <= repositoryAndTag.lastIndexOf("/")) {
330 return yield* ContainerRegistryError.make({
331 reason: "InvalidImageReference",
332 message: "Container image reference has no tag or digest",
333 imageRef,
334 });
335 }
336 const repository = repositoryAndTag.slice(0, tagSeparator);
337 const tag = repositoryAndTag.slice(tagSeparator + 1);
338 const manifestUrl = `https://${registryHost}/v2/${repository
339 .split("/")
340 .map(encodeURIComponent)
341 .join("/")}/manifests/${encodeURIComponent(tag)}`;
342 const request = HttpClientRequest.head(manifestUrl).pipe(
343 HttpClientRequest.basicAuth(credentials.username, credentials.password),
344 HttpClientRequest.setHeader(
345 "Accept",
346 "application/vnd.oci.image.index.v1+json, application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.docker.distribution.manifest.v2+json",
347 ),
348 );
349 const response = yield* http.execute(request).pipe(
350 Effect.mapError((cause) =>
351 ContainerRegistryError.make({
352 reason: "ManifestRequestFailed",
353 message: "Failed to resolve the container registry digest",
354 imageRef,
355 cause,
356 }),
357 ),
358 );
359 if (response.status === 404) {
360 return yield* ContainerRegistryError.make({
361 reason: "ImageNotFound",
362 message: "Container image is not published",
363 imageRef,
364 });
365 }
366 if (response.status < 200 || response.status >= 300) {
367 return yield* ContainerRegistryError.make({
368 reason: "ManifestRequestFailed",
369 message: `Container registry returned HTTP ${response.status}`,
370 imageRef,
371 });
372 }
373 return yield* Schema.decodeEffect(RegistryDigest)(
374 response.headers["docker-content-digest"],
375 ).pipe(
376 Effect.mapError((cause) =>
377 ContainerRegistryError.make({
378 reason: "InvalidManifestDigest",
379 message: "Registry response did not include a valid digest",
380 imageRef,
381 cause,
382 }),
383 ),
384 );
385 });
386
387 const resolvePublishedImageRef = Effect.fn("resolvePublishedImageRef")(function* (
388 props: AnyContainerApplicationProps,
389 imageRef: string,
390 ) {
391 let digest = digestFromImageRef(imageRef);
392 if (digest === undefined) {
393 const credentials = yield* registryCredentials(props, ["pull"]);
394 digest = yield* resolveRegistryDigest(imageRef, credentials);
395 }
396 return {
397 imageRef: `${repositoryFromImageRef(imageRef)}@${digest}`,
398 digest,
399 };
400 });
401
402 const computeImage = Effect.fn(function* (
403 id: string,
404 props: AnyContainerApplicationProps,
405 env: Record<string, string | Redacted.Redacted<string>>,
406 ) {
407 const { accountId } = yield* yield* CloudflareEnvironment;
408 const name = yield* createApplicationName(id, props.name);
409 const registryId = props.registryId ?? "registry.cloudflare.com";
410 const repositoryName = (props.publish?.repository ?? name).toLowerCase();
411 const makeRef = (imageHash: string) =>
412 `${registryId}/${accountId}/${repositoryName}:${imageHash}`;
413
414 yield* validateContainerImageProps(id, props);
415
416 // Preserve serialized imageName keys so existing publication hashes remain valid.
417 // Variant 1 — Effect-native program. Bundle `main` and build a
418 // generated Dockerfile around it; the environment preamble comes
419 // from `image` / inline `dockerfile` (default: the runtime base).
420 if (props.main) {
421 const runtime = props.runtime ?? "bun";
422 const { files, hash: bundleHash } = yield* bundleContainerProgram({
423 id,
424 main: props.main,
425 runtime,
426 handler: props.handler,
427 isExternal: props.isExternal,
428 external: props.external,
429 build: props.build,
430 });
431 const finalDockerfile = buildFinalDockerfile(
432 yield* containerEnvPreamble(props),
433 runtime,
434 props.external,
435 props.autoInstallExternals,
436 );
437 const imageHash = (yield* sha256Object({
438 bundleHash,
439 dockerfile: finalDockerfile,
440 imageName: props.publish?.repository?.toLowerCase(),
441 })).slice(0, 16);
442 // The dev image is the deterministic build-context directory that
443 // `buildAndPushImage` materializes into (and that the local provider
444 // regenerates on the next `alchemy dev`). We persist the path here so
445 // a dev run after a live deploy has an image to `docker build` — the
446 // live deploy pushes to Cloudflare's registry, which the local
447 // `workerd` runtime can't pull. See `prepareContainerBuildContext`.
448 const contextDir = yield* getStableContextDir(
449 process.cwd(),
450 dotAlchemy,
451 `${id}-container`,
452 );
453 return {
454 build: { kind: "effectful" as const, files },
455 imageRef: makeRef(imageHash),
456 imageHash,
457 dev: {
458 context: path.relative(process.cwd(), contextDir),
459 dockerfile: "Dockerfile",
460 env,
461 },
462 };
463 }
464
465 // Variant 2 — pre-built remote image. The image reference is the
466 // identity; we pull and re-push it without building anything.
467 if (props.image) {
468 const imageHash = (yield* sha256Object({
469 image: props.image,
470 imageName: props.publish?.repository?.toLowerCase(),
471 })).slice(0, 16);
472 // Already in the target registry (e.g. pushed by CI as a digest
473 // reference) — deploy the reference as-is and skip the docker
474 // pull/tag/push round-trip entirely.
475 if (isTargetRegistryRef(props.image, registryId)) {
476 const prepushedRef = normalizePrepushedRef(props.image, registryId, accountId);
477 return {
478 build: { kind: "prepushed" as const, image: prepushedRef },
479 imageRef: prepushedRef,
480 imageHash,
481 // The local runtime pulls this image directly (no build
482 // context); pulling from the Cloudflare registry requires a
483 // local `docker login`.
484 dev: { imageUri: prepushedRef, env },
485 };
486 }
487 return {
488 build: { kind: "remote" as const, image: props.image },
489 imageRef: makeRef(imageHash),
490 imageHash,
491 // The local runtime pulls this image directly (no build context).
492 dev: { imageUri: props.image, env },
493 };
494 }
495
496 // Variant 3a — inline Dockerfile content (`Dockerfile.inline`), no
497 // build context. Materialize the content into a stable generated
498 // context directory and build that.
499 if (props.dockerfile !== undefined && isInlineDockerfile(props.dockerfile)) {
500 const content = props.dockerfile.content;
501 if (typeof content !== "string") {
502 return yield* Effect.die(
503 new Error(
504 "Inline `dockerfile` content is an unresolved Output at image-build time — its dependencies have not resolved yet (e.g. during precreate of a circular binding). Break the cycle or inline the resolved value.",
505 ),
506 );
507 }
508 const { context, dockerfile } = yield* materializeInlineDockerfileContext(id, content);
509 const imageHash = (yield* sha256Object({
510 dockerfile: content,
511 // Absent rather than `{}` when unset, so existing hashes hold.
512 buildArgs: props.build?.args,
513 imageName: props.publish?.repository?.toLowerCase(),
514 })).slice(0, 16);
515 return {
516 build: {
517 kind: "external" as const,
518 context,
519 dockerfile,
520 buildArgs: props.build?.args,
521 buildSecrets: props.build?.secrets,
522 },
523 imageRef: makeRef(imageHash),
524 imageHash,
525 // The local runtime builds the same materialized context.
526 dev: {
527 context: path.relative(process.cwd(), context),
528 dockerfile: "Dockerfile",
529 buildArgs: props.build?.args,
530 buildSecrets: props.build?.secrets,
531 env,
532 },
533 };
534 }
535
536 // Variant 3b — user-supplied Dockerfile path + build context
537 // directory.
538 const context = yield* fs.realPath(props.context ?? ".");
539 const dockerfile = props.dockerfile
540 ? yield* fs.realPath(props.dockerfile)
541 : path.join(context, "Dockerfile");
542 // Hash what Docker can consume, including gitignored files and file
543 // modes, but excluding ancestor lockfiles outside the build context.
544 const contextHash = yield* hashDockerBuildInputs(
545 { context, dockerfile, platform: publicationPlatform, buildArgs: props.build?.args },
546 "effective",
547 );
548 const imageHash = (yield* sha256Object({
549 contextHash,
550 imageName: props.publish?.repository?.toLowerCase(),
551 })).slice(0, 16);
552 return {
553 build: {
554 kind: "external" as const,
555 context,
556 dockerfile,
557 buildArgs: props.build?.args,
558 buildSecrets: props.build?.secrets,
559 },
560 imageRef: makeRef(imageHash),
561 imageHash,
562 // The local runtime builds the user's Dockerfile against the same
563 // (already real-path'd) context directory.
564 dev: {
565 context: path.relative(process.cwd(), context),
566 dockerfile: path.relative(context, dockerfile),
567 buildArgs: props.build?.args,
568 buildSecrets: props.build?.secrets,
569 env,
570 },
571 };
572 });
573
574 const publicationPlatform = "linux/amd64";
575 const publishImage = Effect.fn("publishImage")(function* (
576 id: string,
577 props: AnyContainerApplicationProps,
578 build: ImageBuild,
579 imageRef: string,
580 // The full session, not just `note`: `Docker.image.build` streams the
581 // builder's output through it as `kind: "output"` notes.
582 session?: ScopedPlanStatusSession,
583 reuseRemotePublication = false,
584 ) {
585 const platform = publicationPlatform;
586
587 if (build.kind === "prepushed") {
588 // The reference already lives in the target registry — nothing to
589 // pull, build, or push.
590 yield* Effect.logInfo(`Cloudflare Container image: using pre-pushed ${imageRef}`);
591 return yield* resolvePublishedImageRef(props, imageRef);
592 }
593
594 const credentials = yield* registryCredentials(props, ["pull", "push"]);
595
596 if (build.kind !== "remote" || reuseRemotePublication) {
597 const digest = yield* resolveRegistryDigest(imageRef, credentials).pipe(
598 Effect.catchTag("ContainerRegistryError", (error) =>
599 error.reason === "ImageNotFound" ? Effect.undefined : Effect.fail(error),
600 ),
601 );
602 if (digest !== undefined) {
603 const published = `${repositoryFromImageRef(imageRef)}@${digest}`;
604 yield* Effect.logInfo(`Cloudflare Container image: registry cache hit ${published}`);
605 if (session) {
606 yield* session.note(`Reusing registry container image ${published}.`);
607 }
608 return { imageRef: published, digest };
609 }
610 }
611 const cacheRef = `${repositoryFromImageRef(imageRef)}:buildcache`;
612 const cacheOptions = {
613 "cache-from": [`type=registry,ref=${cacheRef}`],
614 "cache-to": ["type=inline"],
615 };
616
617 if (build.kind === "remote") {
618 // Pull the pre-built image and re-tag it to the Cloudflare registry
619 // reference; nothing is built locally.
620 yield* Effect.logInfo(`Cloudflare Container image: pulling ${build.image}`);
621 if (session) {
622 yield* session.note(`Pulling container image ${build.image}...`);
623 }
624 // Pull every time so a mutable tag publishes the newest image. An
625 // image built on this machine or `docker load`ed has no registry
626 // to pull from, so when the pull fails and the image is present
627 // locally, publish the local copy.
628 const usedLocalImage = yield* docker.image
629 .pull(build.image, platform, undefined, session)
630 .pipe(
631 Effect.as(false),
632 Effect.catch((pullError) =>
633 docker.image.inspect(build.image).pipe(
634 Effect.flatMap((local) =>
635 local
636 ? Effect.logInfo(
637 `Cloudflare Container image: ${build.image} could not be pulled, publishing the local image`,
638 ).pipe(Effect.as(true))
639 : Effect.fail(pullError),
640 ),
641 Effect.mapError(
642 () =>
643 new ContainerProviderOperationFailed({
644 message:
645 `Container image "${build.image}" could not be pulled and is not in the local Docker. ` +
646 `If it is in a registry, check the name and tag, and run \`docker login\` for a private registry. ` +
647 `If it is built on this machine, build or load it before deploying: ` +
648 `\`docker build --platform ${platform} -t ${build.image} .\` or \`docker load\`.`,
649 cause: pullError,
650 }),
651 ),
652 ),
653 ),
654 );
655 yield* docker.image.tag(build.image, imageRef);
656 yield* Effect.logInfo(`Cloudflare Container image: pushing ${imageRef}`);
657 if (session) {
658 yield* session.note(`Pushing container image ${imageRef}...`);
659 }
660 // Push the same platform that was pulled. A containerd image store
661 // may also hold a host-architecture variant under this tag.
662 yield* docker.image.push(imageRef, credentials, platform, undefined, session).pipe(
663 retryContainerPublication,
664 Effect.mapError((pushError) =>
665 usedLocalImage
666 ? new ContainerProviderOperationFailed({
667 message:
668 `Could not publish the local image "${build.image}" as ${platform}, the platform Cloudflare runs. ` +
669 `Rebuild it for that platform: \`docker build --platform ${platform} -t ${build.image} .\`.`,
670 cause: pushError,
671 })
672 : pushError,
673 ),
674 );
675 } else if (build.kind === "external") {
676 // Build the user's Dockerfile directly against their context dir so
677 // relative `COPY`/`ADD` paths resolve as the author intended.
678 yield* Effect.logInfo(`Cloudflare Container image: building ${imageRef}`);
679 if (session) {
680 yield* session.note(`Building container image ${imageRef}...`);
681 }
682 yield* docker.image
683 .build(
684 {
685 ...cacheOptions,
686 tag: [imageRef, cacheRef],
687 context: build.context,
688 platform,
689 file: build.dockerfile,
690 "build-arg": build.buildArgs,
691 secrets: build.buildSecrets,
692 },
693 session,
694 credentials,
695 )
696 .pipe(retryContainerBuild);
697 } else {
698 // Effect-native program: materialize the generated Dockerfile and
699 // bundled chunks into a stable staging dir, then build.
700 yield* Effect.logInfo(`Cloudflare Container image: building ${imageRef}`);
701 if (session) {
702 yield* session.note(`Building container image ${imageRef}...`);
703 }
704 const runtime = props.runtime ?? "bun";
705 const contextDir = yield* getStableContextDir(
706 process.cwd(),
707 dotAlchemy,
708 `${id}-container`,
709 );
710 const finalDockerfile = buildFinalDockerfile(
711 yield* containerEnvPreamble(props),
712 runtime,
713 props.external,
714 props.autoInstallExternals,
715 );
716 yield* docker.materialize({
717 context: contextDir,
718 dockerfile: finalDockerfile,
719 files: build.files.map((f, i) => ({
720 path: i === 0 ? "index.mjs" : f.path,
721 content: f.content,
722 })),
723 });
724 yield* docker.image
725 .build(
726 {
727 ...cacheOptions,
728 tag: [imageRef, cacheRef],
729 context: contextDir,
730 platform,
731 },
732 session,
733 credentials,
734 )
735 .pipe(retryContainerBuild);
736 }
737
738 // Resolve the pushed manifest digest from the registry itself rather
739 // than scraping `docker push` output: one mechanism for every image
740 // source (local build, remote re-push, pre-pushed tag), and the
741 // registry is authoritative for what the application will pull.
742 const digest = yield* resolveRegistryDigest(imageRef, credentials);
743 return {
744 imageRef: `${repositoryFromImageRef(imageRef)}@${digest}`,
745 digest,
746 };
747 });
748
749 // Applications with identical image inputs can share one immutable
750 // registry reference. Keep publication state within this provider instance.
751 const publications = new Map<string, ReturnType<typeof publishImage>>();
752 const buildAndPushImage = Effect.fn("buildAndPushImage")(function* (
753 id: string,
754 props: AnyContainerApplicationProps,
755 build: ImageBuild,
756 imageRef: string,
757 imageHash: string,
758 previousImageRef: string | undefined,
759 session?: ScopedPlanStatusSession,
760 reuseRemotePublication = false,
761 ) {
762 const { accountId } = yield* yield* CloudflareEnvironment;
763 const key = JSON.stringify([
764 accountId,
765 props.registryId ?? "registry.cloudflare.com",
766 props.publish?.repository,
767 publicationPlatform,
768 build.kind,
769 reuseRemotePublication,
770 imageHash,
771 ]);
772 const candidate: ReturnType<typeof publishImage> = yield* Effect.cached(
773 publishImage(id, props, build, imageRef, session, reuseRemotePublication).pipe(
774 Effect.onExit((exit) =>
775 Exit.isFailure(exit)
776 ? Effect.sync(() => {
777 // A failed or interrupted publisher must not poison a
778 // later attempt, or remove an entry that replaced it.
779 if (publications.get(key) === candidate) {
780 publications.delete(key);
781 }
782 })
783 : Effect.void,
784 ),
785 ),
786 );
787 const publication = yield* Effect.sync(() => {
788 // Allocate atomically: concurrent callers execute only the winning
789 // candidate, sharing both its in-flight work and successful result.
790 const existing = publications.get(key);
791 if (existing) return existing;
792 publications.set(key, candidate);
793 return candidate;
794 });
795 const published = yield* publication;
796 return {
797 ...published,
798 // Previous image identity belongs to each application, not the
799 // shared image publication. Resolve it separately for every caller.
800 previousDigest:
801 previousImageRef === undefined
802 ? undefined
803 : (yield* resolvePublishedImageRef(props, previousImageRef)).digest,
804 };
805 });
806
807 const resolveDeploymentImage = Effect.fn(function* ({
808 id,
809 news,
810 existing,
811 build,
812 imageRef,
813 imageHash,
814 session,
815 }: {
816 id: string;
817 news: AnyContainerApplicationProps;
818 existing: ContainerApplication["Attributes"];
819 build: ImageBuild;
820 imageRef: string;
821 imageHash: string;
822 session: ScopedPlanStatusSession;
823 }) {
824 yield* validateContainerConfiguration(news, existing.schedulingPolicy);
825 const existingImage = existing.configuration.image;
826 if (!existingImage) {
827 return yield* new ContainerConfigurationError({
828 message: `Container application '${existing.applicationName}' has no deployment image.`,
829 });
830 }
831 let deploymentImageRef = existingImage;
832 let imageDigest = existing.hash?.digest;
833 if (imageHash !== existing.hash?.image) {
834 const published = yield* buildAndPushImage(
835 id,
836 news,
837 build,
838 imageRef,
839 imageHash,
840 existing.hash?.digest === undefined ? existingImage : undefined,
841 session,
842 );
843 const existingDigest = existing.hash?.digest ?? published.previousDigest;
844 deploymentImageRef =
845 published.digest === existingDigest && news.publish?.repository === undefined
846 ? existingImage
847 : published.imageRef;
848 imageDigest = published.digest;
849 }
850 return { deploymentImageRef, imageDigest };
851 });
852
853 // ---------------------------------------------------------------
854 // Durable Object-managed Containers (schedulingPolicy:
855 // "durable_object"). A Container publishes one image; the application
856 // belongs to the Durable Object class (see DurableObjectApplication).
857 // ---------------------------------------------------------------
858
859 /**
860 * Cloudflare must prepare an image before a Worker upload may reference
861 * it. Preparation is per image reference, so remember the ones that are
862 * already done for the lifetime of this provider.
863 */
864 const preparedImages = new Set<string>();
865 const waitForImagePrepared = Effect.fn(function* (
866 image: string,
867 name: string,
868 timeout: DurableObjectContainerProps["imagePreparationTimeout"],
869 session: ScopedPlanStatusSession,
870 ) {
871 if (preparedImages.has(image)) return;
872 const { accountId } = yield* yield* CloudflareEnvironment;
873 yield* waitForContainerImage({
874 image,
875 name,
876 timeout,
877 session,
878 prepare: Containers.prepareContainerImage({ accountId, image }),
879 });
880 preparedImages.add(image);
881 });
882
883 /** The build inputs and content hash of a Durable Object Container's image. */
884 const computeDurableObjectImage = (id: string, props: DurableObjectContainerProps) =>
885 hasImageSource(props) ? computeImage(id, props, {}) : Effect.undefined;
886
887 /**
888 * Publish the Container's image to Cloudflare's registry and wait until
889 * it is prepared. An unchanged content hash keeps the previously
890 * published reference.
891 */
892 const reconcileDurableObjectContainer = Effect.fn(function* ({
893 id,
894 news,
895 name,
896 output,
897 session,
898 }: {
899 id: string;
900 news: DurableObjectContainerProps;
901 name: string;
902 output: ContainerApplication["Attributes"] | undefined;
903 session: ScopedPlanStatusSession;
904 }) {
905 const { accountId } = yield* yield* CloudflareEnvironment;
906 const attributes = durableObjectPlaceholder({
907 // The application is owned by the Durable Object class.
908 applicationId: "",
909 applicationName: name,
910 accountId,
911 createdAt: output?.createdAt ?? "",
912 observability: news.observability,
913 });
914 const image = yield* computeDurableObjectImage(id, news);
915 if (image === undefined) return attributes;
916
917 let imageRef: string;
918 let digest: string | undefined;
919 if (output?.image !== undefined && output.hash?.image === image.imageHash) {
920 imageRef = output.image;
921 digest = output.hash.digest;
922 } else {
923 const published = yield* buildAndPushImage(
924 id,
925 news,
926 image.build,
927 image.imageRef,
928 image.imageHash,
929 undefined,
930 session,
931 // The registry tag keyed by image inputs is the durable publication checkpoint.
932 // Reuse it after an interrupted preparation, including mirrored images.
933 true,
934 );
935 imageRef = published.imageRef;
936 digest = published.digest;
937 }
938 yield* waitForImagePrepared(imageRef, id, news.imagePreparationTimeout, session);
939 return {
940 ...attributes,
941 image: imageRef,
942 dev: image.dev,
943 hash: { image: image.imageHash, digest },
944 } satisfies ContainerApplication["Attributes"];
945 });
946
947 const diffDurableObjectContainer = Effect.fn(function* ({
948 id,
949 news,
950 output,
951 }: {
952 id: string;
953 news: DurableObjectContainerProps;
954 output: ContainerApplication["Attributes"] | undefined;
955 }) {
956 if (output === undefined) return undefined;
957 // State written before one Container meant one image recorded the
958 // Container as the application itself. The application now belongs to
959 // the Durable Object class; drop the identity so deleting this
960 // Container never deletes the class's application.
961 if (output.applicationId !== "") {
962 return { action: "update" as const, stables: ["accountId"] };
963 }
964 // Image content (a Dockerfile, a context file) can change without any
965 // prop changing, so compare content hashes as well.
966 const image = yield* computeDurableObjectImage(id, news);
967 if (
968 image?.imageHash !== output.hash?.image ||
969 (image !== undefined && output.image === undefined) ||
970 !deepEqual(image?.dev, output.dev)
971 ) {
972 return { action: "update" } as const;
973 }
974 return undefined;
975 });
976
977 const maybeCreateRollout = Effect.fn(function* ({
978 applicationId,
979 configuration,
980 rollout,
981 }: {
982 applicationId: string;
983 configuration: ContainerApplication.Configuration;
984 rollout: ContainerApplication.Rollout | undefined;
985 }) {
986 const { accountId } = yield* yield* CloudflareEnvironment;
987
988 const strategy = rollout?.strategy ?? "immediate";
989 const stepPercentage = strategy === "immediate" ? 100 : (rollout?.stepPercentage ?? 25);
990
991 yield* retryForContainerApplicationReadiness(
992 "rollout",
993 applicationId,
994 Containers.createContainerApplicationRollout({
995 accountId,
996 applicationId,
997 description: strategy === "immediate" ? "Immediate update" : "Progressive update",
998 strategy: "rolling",
999 kind: rollout?.kind ?? "full_auto",
1000 stepPercentage,
1001 targetConfiguration: configuration,
1002 }),
1003 );
1004 });
1005
1006 const createApplication = Effect.fn(function* ({
1007 id,
1008 news,
1009 bindings,
1010 name,
1011 configuration,
1012 durableObjects,
1013 session,
1014 }: {
1015 id: string;
1016 news: AnyContainerApplicationProps;
1017 bindings: ResourceBinding<ContainerApplication["Binding"]>[];
1018 name: string;
1019 configuration: ContainerApplication.Configuration;
1020 durableObjects:
1021 | {
1022 namespaceId: string;
1023 }
1024 | undefined;
1025 session: ScopedPlanStatusSession;
1026 }) {
1027 const { accountId } = yield* yield* CloudflareEnvironment;
1028
1029 const describeError = (error: unknown) => {
1030 if (error instanceof Error) {
1031 return JSON.stringify(
1032 Object.fromEntries(
1033 Object.getOwnPropertyNames(error).map((key) => [
1034 key,
1035 (error as unknown as Record<string, unknown>)[key],
1036 ]),
1037 ),
1038 null,
1039 2,
1040 );
1041 }
1042 return String(error);
1043 };
1044
1045 // Engine has cleared us via `read` or `observe` (foreign-named
1046 // applications are surfaced as `Unowned`). Re-fetch the existing application to fold
1047 // it into the upsert path.
1048 const existingByName = yield* findApplicationByName(name);
1049
1050 if (existingByName) {
1051 yield* Effect.logInfo(
1052 `Cloudflare Container create: adopting existing application ${name}`,
1053 );
1054 return yield* upsertApplication({
1055 id,
1056 news,
1057 bindings,
1058 existing: toAttributes(existingByName),
1059 durableObjects,
1060 session,
1061 });
1062 }
1063
1064 yield* Effect.logInfo(`Cloudflare Container create: creating application ${name}`);
1065 yield* session.note(`Creating container application ${name}...`);
1066 const adoptExistingByName = Effect.gen(function* () {
1067 yield* Effect.logInfo(
1068 `Cloudflare Container create: application ${name} already exists, adopting`,
1069 );
1070 const existing = yield* findApplicationByName(name);
1071 if (!existing) {
1072 return yield* new ContainerProviderUnexpectedResponse({
1073 message: `Container application "${name}" already exists but could not be found for adoption.`,
1074 });
1075 }
1076 return yield* upsertApplication({
1077 id,
1078 news,
1079 bindings,
1080 existing: toAttributes(existing),
1081 durableObjects,
1082 session,
1083 });
1084 });
1085
1086 const application = yield* retryNamespacePropagation(
1087 Containers.createContainerApplication({
1088 accountId,
1089 name,
1090 ...scalingDefaults(news),
1091 affinities: news.affinities,
1092 configuration,
1093 durableObjects,
1094 }),
1095 ).pipe(
1096 Effect.catchTag("DurableObjectAlreadyHasApplication", () =>
1097 durableObjects
1098 ? Effect.gen(function* () {
1099 const existing = yield* findApplicationByNamespace(durableObjects.namespaceId);
1100 const recovery = resolveDurableObjectApplicationRecovery({
1101 namespaceId: durableObjects.namespaceId,
1102 expectedName: name,
1103 existingName: existing?.name,
1104 });
1105 if (!recovery.canAdopt) {
1106 return yield* new ContainerProviderConflict({ message: recovery.message });
1107 }
1108 if (!existing) {
1109 return yield* new ContainerProviderUnexpectedResponse({
1110 message: `Container application for Durable Object namespace "${durableObjects.namespaceId}" already exists but could not be found for adoption.`,
1111 });
1112 }
1113 return yield* upsertApplication({
1114 id,
1115 news,
1116 bindings,
1117 existing: toAttributes(existing),
1118 durableObjects,
1119 session,
1120 });
1121 })
1122 : Effect.fail(
1123 new ContainerProviderConflict({
1124 message:
1125 "Durable Object namespace already has a container application. Set AdoptPolicy to adopt it.",
1126 }),
1127 ),
1128 ),
1129 Effect.catchIf(
1130 (e) => "message" in (e as any) && String((e as any).message).includes("already exists"),
1131 () => adoptExistingByName,
1132 ),
1133 Effect.tapError((error) =>
1134 Effect.logError(`Cloudflare Container create error: ${describeError(error)}`),
1135 ),
1136 );
1137
1138 return "applicationId" in application ? application : toAttributes(application);
1139 });
1140
1141 const upsertApplication = Effect.fn(function* ({
1142 id,
1143 news,
1144 bindings,
1145 existing,
1146 durableObjects,
1147 session,
1148 }: {
1149 id: string;
1150 news: AnyContainerApplicationProps;
1151 bindings: ResourceBinding<ContainerApplication["Binding"]>[];
1152 existing: ContainerApplication["Attributes"];
1153 // The DO attachment to (re)create with if the "existing" application
1154 // turns out to be gone. Threaded through so the update→create fallback
1155 // below preserves the binding.
1156 durableObjects: { namespaceId: string } | undefined;
1157 session: ScopedPlanStatusSession;
1158 }) {
1159 const { accountId } = yield* yield* CloudflareEnvironment;
1160
1161 yield* Effect.logInfo(`Cloudflare Container update: preparing ${existing.applicationName}`);
1162 const env = makeContainerEnv(news, accountId, bindings);
1163 const { build, imageRef, imageHash, dev } = yield* computeImage(id, news, env);
1164 const { deploymentImageRef, imageDigest } = yield* resolveDeploymentImage({
1165 id,
1166 news,
1167 existing,
1168 build,
1169 imageRef,
1170 imageHash,
1171 session,
1172 });
1173 const configuration = desiredConfiguration(news, env, deploymentImageRef);
1174 const scaling = scalingDefaults(news);
1175 const configurationHash = yield* applicationConfigurationHash(
1176 scaling,
1177 news.affinities,
1178 configuration,
1179 );
1180 if (existing.hash?.configuration === configurationHash) {
1181 yield* Effect.logInfo(
1182 `Cloudflare Container update: ${existing.applicationName} has no effective changes`,
1183 );
1184 yield* session.note(`Container application ${existing.applicationName} is unchanged.`);
1185 // `existing` is the observed application; keep its configuration,
1186 // which `read` reports with Cloudflare's defaults filled in.
1187 return {
1188 ...existing,
1189 hash: {
1190 image: imageHash,
1191 digest: imageDigest,
1192 configuration: configurationHash,
1193 },
1194 dev,
1195 };
1196 }
1197
1198 yield* session.note(`Updating container application ${existing.applicationName}...`);
1199 const application = yield* retryForContainerApplicationReadiness(
1200 "update",
1201 existing.applicationId,
1202 Containers.updateContainerApplication({
1203 accountId,
1204 applicationId: existing.applicationId,
1205 ...scaling,
1206 affinities: news.affinities,
1207 configuration,
1208 }),
1209 ).pipe(
1210 // The "existing" application was observed from an eventually-
1211 // consistent list/get but is actually gone — e.g. a stale row that
1212 // lingered after a replacement/DO-recreate delete, surfaced by
1213 // either the by-name or by-namespace lookup. Updating a ghost
1214 // exhausts the readiness window and then fails permanently with
1215 // `ContainerApplicationNotFound`. Instead, create it fresh so
1216 // reconcile converges regardless of the stale observation. By the
1217 // time the bounded readiness retry has elapsed, the deleted row has
1218 // fallen out of the eventually-consistent views, so this create
1219 // does not re-collide.
1220 Effect.catchTag("ContainerApplicationNotFound", () =>
1221 Effect.gen(function* () {
1222 yield* Effect.logInfo(
1223 `Cloudflare Container update: ${existing.applicationName} no longer exists, creating fresh`,
1224 );
1225 return yield* retryNamespacePropagation(
1226 Containers.createContainerApplication({
1227 accountId,
1228 name: existing.applicationName,
1229 ...scaling,
1230 affinities: news.affinities,
1231 configuration,
1232 durableObjects,
1233 }),
1234 );
1235 }),
1236 ),
1237 );
1238 const updated = toAttributes(application);
1239 if (!deepEqual(existing.configuration, configuration)) {
1240 yield* Effect.logInfo(
1241 `Cloudflare Container update: creating rollout for ${updated.applicationName}`,
1242 );
1243 yield* maybeCreateRollout({
1244 applicationId: updated.applicationId,
1245 configuration,
1246 rollout: news.rollout,
1247 });
1248 yield* session.note(`Waiting for the rollout of ${updated.applicationName} to land...`);
1249 }
1250 // Cloudflare keeps reporting the previous version and configuration
1251 // until the rollout lands, then reports the configuration with its
1252 // defaults filled in. Save the application as `read` will see it.
1253 const observed = yield* retryForContainerApplicationReadiness(
1254 "read",
1255 updated.applicationId,
1256 Containers.getContainerApplication({ accountId, applicationId: updated.applicationId }),
1257 ).pipe(
1258 Effect.repeat({
1259 schedule: Schedule.spaced("2 seconds"),
1260 until: (app) => !hasActiveRollout(app),
1261 times: 30,
1262 }),
1263 );
1264 if (hasActiveRollout(observed)) {
1265 yield* Effect.logInfo(
1266 `Cloudflare Container update: rollout of ${updated.applicationName} is still in progress`,
1267 );
1268 yield* session.note(
1269 `Rollout of ${updated.applicationName} is still in progress; saved the application as it was before the rollout.`,
1270 );
1271 }
1272 return {
1273 ...toAttributes(observed),
1274 hash: {
1275 image: imageHash,
1276 digest: imageDigest,
1277 configuration: configurationHash,
1278 },
1279 dev,
1280 };
1281 });
1282
1283 const getDurableObjects = (bindings: ResourceBinding<ContainerApplication["Binding"]>[]) => {
1284 // A stale Worker namespace map can resolve a binding to an object
1285 // without an id. It does not request removing the live attachment.
1286 const dos = bindings.flatMap((b) =>
1287 b.data.durableObjects?.namespaceId ? [b.data.durableObjects] : [],
1288 );
1289 // A single DO namespace may appear in multiple bindings (e.g. when
1290 // a Container is referenced by several resources). Dedupe by namespaceId.
1291 const uniqueDos = dos.filter(
1292 (d, i, arr) => arr.findIndex((other) => other.namespaceId === d.namespaceId) === i,
1293 );
1294 if (uniqueDos.length === 0) {
1295 return Effect.undefined;
1296 }
1297 if (uniqueDos.length === 1) {
1298 return Effect.succeed(uniqueDos[0]);
1299 }
1300 return Effect.die(
1301 new Error(
1302 `A Container can only be bound to one Durable Object namespace. Found ${uniqueDos.length} unique namespaces in bindings: ${uniqueDos.map((d) => d.namespaceId).join(", ")}`,
1303 ),
1304 );
1305 };
1306
1307 return ContainerPlatform.Provider.of({
1308 stables: ["accountId", "applicationId"],
1309 diff: Effect.fn(function* ({ id, olds = {}, news = {}, output, newBindings, oldBindings }) {
1310 if (!isResolved(news)) return;
1311 yield* validateContainerConfiguration(news, output?.schedulingPolicy);
1312 if (!isResolved(newBindings)) return;
1313 const { accountId } = yield* yield* CloudflareEnvironment;
1314
1315 const oldName =
1316 output?.applicationName ?? (yield* createApplicationName(id, nameOf(olds)));
1317 // Auto-generated names are engine-owned: the deployed name stays
1318 // authoritative even if the generator would name this id differently
1319 // today. Only an explicit user-provided name can force a replace.
1320 const name = nameOf(news) ?? oldName;
1321
1322 if (isDurableObjectContainer(news)) {
1323 return yield* diffDurableObjectContainer({ id, news, output });
1324 }
1325
1326 if ((output?.accountId ?? accountId) !== accountId || name !== oldName) {
1327 return { action: "replace" } as const;
1328 }
1329
1330 const hasDurableObjects = (yield* getDurableObjects(newBindings)) !== undefined;
1331 const hasUnresolvedAttachment =
1332 !hasDurableObjects &&
1333 newBindings.some((binding) => binding.data.durableObjects !== undefined);
1334 // An adopted application whose first reconcile failed has its
1335 // attachment in `output` but no bindings recorded yet. The observed
1336 // attachment may only prevent a replace, never cause one: a replace
1337 // of a named application re-adopts it by name and the old
1338 // generation's delete then removes it. Reconcile detaches in place.
1339 const hadDurableObjects =
1340 (yield* getDurableObjects(oldBindings)) !== undefined ||
1341 (hasDurableObjects && output?.durableObjects !== undefined);
1342 if (!hasUnresolvedAttachment && hasDurableObjects !== hadDurableObjects) {
1343 return { action: "replace" } as const;
1344 }
1345
1346 if (!output) {
1347 return undefined;
1348 }
1349
1350 // A `dev:` applicationId means the resource only exists locally and
1351 // the real application has never been created. Promote it by forcing
1352 // an update so reconcile creates the live application.
1353 if (!isLiveId(output.applicationId)) {
1354 // Override stables to only include the accountId because the applicationId is going to change.
1355 return { action: "update", stables: ["accountId"] } as const;
1356 }
1357
1358 const application = yield* Containers.getContainerApplication({
1359 accountId: output.accountId,
1360 applicationId: output.applicationId,
1361 }).pipe(Effect.catchTag("ContainerApplicationNotFound", () => Effect.undefined));
1362 if (
1363 application &&
1364 (application.id !== output.applicationId ||
1365 application.name !== output.applicationName ||
1366 application.accountId !== output.accountId)
1367 ) {
1368 return { action: "replace" } as const;
1369 }
1370
1371 // Observed attached, desired detached, and no recorded attachment to
1372 // replace on: reconcile detaches in place under a new applicationId.
1373 if (
1374 application?.durableObjects?.namespaceId &&
1375 !hasDurableObjects &&
1376 !hasUnresolvedAttachment
1377 ) {
1378 return { action: "update", stables: ["accountId"] } as const;
1379 }
1380
1381 const { imageHash, dev } = yield* computeImage(
1382 id,
1383 news,
1384 makeContainerEnv(news, accountId, newBindings),
1385 );
1386 if (imageHash !== output.hash?.image || !deepEqual(dev, output.dev)) {
1387 return { action: "update" } as const;
1388 }
1389 }),
1390 observe: Effect.fn(function* ({ id, news = {}, previous }) {
1391 // Precreate creates — or adopts and updates — the application
1392 // named here; ownership follows `read`.
1393 if (!isResolved(nameOf(news))) return Unresolved;
1394 const name = yield* createApplicationName(id, nameOf(news));
1395 const existing = yield* findApplicationByName(name);
1396 if (existing === undefined) return undefined;
1397 const observed = toAttributes(existing);
1398 return isOwnedApplication(nameOf(news), observed, previous)
1399 ? observed
1400 : Unowned(observed);
1401 }),
1402 precreate: Effect.fn(function* ({ id, news = {}, session }) {
1403 if (!isResolved(nameOf(news))) {
1404 return yield* new UnresolvedIdentity({
1405 message:
1406 `Container(${id}): its name depends on a resource in this ` +
1407 "Container's dependency cycle, so precreate cannot name the application. " +
1408 "Use a literal name, or one from outside the cycle.",
1409 resourceType: "Cloudflare.Container",
1410 logicalId: id,
1411 });
1412 }
1413 yield* validateContainerConfiguration(news);
1414 const name = yield* createApplicationName(id, nameOf(news));
1415 yield* Effect.logInfo(`Cloudflare Container precreate: starting ${name}`);
1416
1417 const { accountId } = yield* yield* CloudflareEnvironment;
1418 if (isDurableObjectContainer(news)) {
1419 return yield* reconcileDurableObjectContainer({
1420 id,
1421 news,
1422 name,
1423 output: undefined,
1424 session,
1425 });
1426 }
1427 const env = makeContainerEnv(news, accountId);
1428 const { build, imageRef, imageHash, dev } = yield* computeImage(id, news, env);
1429 const published = yield* buildAndPushImage(
1430 id,
1431 news,
1432 build,
1433 imageRef,
1434 imageHash,
1435 undefined,
1436 session,
1437 );
1438 const configuration = desiredConfiguration(news, env, published.imageRef);
1439 const configurationHash = yield* applicationConfigurationHash(
1440 scalingDefaults(news),
1441 news.affinities,
1442 configuration,
1443 );
1444
1445 // Precreate intentionally omits the Durable Object attachment so the
1446 // worker can bind to this application id and break the circular
1447 // dependency. The final create step recreates the application with the
1448 // resolved namespace when needed.
1449 const result = yield* createApplication({
1450 id,
1451 news,
1452 // Precreate runs before the engine resolves bindings (that is what
1453 // breaks the worker <-> container cycle), so binding-injected env
1454 // lands on the following reconcile.
1455 bindings: [],
1456 name,
1457 configuration,
1458 durableObjects: undefined,
1459 session: {
1460 ...session,
1461 note: (message) => session.note(message.replace("Creating", "Pre-creating")),
1462 },
1463 });
1464 return {
1465 ...("applicationId" in result ? result : toAttributes(result)),
1466 hash: {
1467 image: imageHash,
1468 digest: published.digest,
1469 configuration: configurationHash,
1470 },
1471 dev,
1472 };
1473 }),
1474 reconcile: Effect.fn(function* ({ id, news = {}, bindings, output, session }) {
1475 yield* validateContainerConfiguration(news, output?.schedulingPolicy);
1476 // Prefer the deployed name: regenerating would target a different
1477 // resource if the generator's output for this id ever drifts.
1478 const name = output?.applicationName ?? (yield* createApplicationName(id, nameOf(news)));
1479 yield* Effect.logInfo(`Cloudflare Container reconcile: starting ${name}`);
1480 if (isDurableObjectContainer(news)) {
1481 return yield* reconcileDurableObjectContainer({ id, news, name, output, session });
1482 }
1483 const durableObjects = yield* getDurableObjects(bindings);
1484 const hasUnresolvedAttachment =
1485 durableObjects === undefined &&
1486 bindings.some((binding) => binding.data.durableObjects !== undefined);
1487 const { accountId } = yield* yield* CloudflareEnvironment;
1488 const env = makeContainerEnv(news, accountId, bindings);
1489
1490 // Observe — re-fetch the cached application to confirm it still
1491 // exists. Cloudflare reports a deleted container application as
1492 // `ContainerApplicationNotFound`; we fall back to a name lookup
1493 // so we can recover from out-of-band deletes or partial state
1494 // persistence failures.
1495 let existing: ContainerApplication["Attributes"] | undefined;
1496 // A `dev:` applicationId never exists on Cloudflare — skip the
1497 // cached-id fetch and fall through to the name lookup / create path
1498 // so we promote the local resource to a real application.
1499 if (output?.applicationId && isLiveId(output.applicationId)) {
1500 existing = yield* Containers.getContainerApplication({
1501 accountId: output.accountId,
1502 applicationId: output.applicationId,
1503 }).pipe(
1504 Effect.map((app) => ({
1505 ...toAttributes(app),
1506 hash: output.hash,
1507 })),
1508 Effect.catchTag("ContainerApplicationNotFound", () => Effect.undefined),
1509 );
1510 }
1511 if (!existing) {
1512 const found = yield* findApplicationByName(name);
1513 if (found) {
1514 existing = {
1515 ...toAttributes(found),
1516 hash: output?.hash,
1517 };
1518 }
1519 }
1520
1521 // Only use cached attachment data after confirming the application
1522 // is missing. An observed live attachment outranks stale state.
1523 const recordedDurableObjects = existing
1524 ? existing.durableObjects
1525 : output?.accountId === accountId && isLiveId(output.applicationId)
1526 ? output.durableObjects
1527 : undefined;
1528 const durableObjectsForRecovery =
1529 durableObjects ??
1530 (hasUnresolvedAttachment && recordedDurableObjects?.namespaceId
1531 ? recordedDurableObjects
1532 : undefined);
1533 // An observed detached application being re-attached reconciles
1534 // unattached, like precreate: the Worker publishes the namespace in
1535 // the same apply and the convergence pass attaches it. A missing
1536 // application is never created without its attachment.
1537 if (hasUnresolvedAttachment && durableObjectsForRecovery === undefined && !existing) {
1538 return yield* new ContainerProviderOperationFailed({
1539 message: `Container application "${name}" has an unresolved Durable Object namespace and no recorded attachment. Reconcile its Worker first.`,
1540 });
1541 }
1542 const { build, imageRef, imageHash, dev } = yield* computeImage(id, news, env);
1543
1544 // Special case: precreate produced an application without the
1545 // durable object attachment, but the real reconcile now has one.
1546 // The DO attachment is immutable, so we delete
1547 // and recreate. Adoption-by-namespace is preferred when an app
1548 // already owns the namespace.
1549 // An unresolved declaration is not an intentional removal.
1550 if (
1551 existing &&
1552 !hasUnresolvedAttachment &&
1553 !deepEqual(existing.durableObjects, durableObjects)
1554 ) {
1555 if (durableObjects) {
1556 const owner = yield* findApplicationByNamespace(durableObjects.namespaceId);
1557 const recovery = resolveDurableObjectApplicationRecovery({
1558 namespaceId: durableObjects.namespaceId,
1559 expectedName: name,
1560 existingName: owner?.name,
1561 });
1562 if (recovery.canAdopt) {
1563 if (!owner) {
1564 return yield* new ContainerProviderUnexpectedResponse({
1565 message: `Container application for Durable Object namespace "${durableObjects.namespaceId}" already exists but could not be found for adoption.`,
1566 });
1567 }
1568 return yield* upsertApplication({
1569 id,
1570 news,
1571 bindings,
1572 existing: toAttributes(owner),
1573 durableObjects,
1574 session,
1575 });
1576 }
1577 }
1578 const { deploymentImageRef, imageDigest } = yield* resolveDeploymentImage({
1579 id,
1580 news,
1581 existing,
1582 build,
1583 imageRef,
1584 imageHash,
1585 session,
1586 });
1587 const configuration = desiredConfiguration(news, env, deploymentImageRef);
1588 const configurationHash = yield* applicationConfigurationHash(
1589 scalingDefaults(news),
1590 news.affinities,
1591 configuration,
1592 );
1593 yield* Effect.logInfo(
1594 `Cloudflare Container reconcile: recreating ${name} to attach durable object binding`,
1595 );
1596 yield* session.note(
1597 `Recreating container application ${name} with durable object binding...`,
1598 );
1599 yield* Containers.deleteContainerApplication({
1600 accountId: existing.accountId,
1601 applicationId: existing.applicationId,
1602 }).pipe(Effect.catchTag("ContainerApplicationNotFound", () => Effect.void));
1603 // Wait out the eventually-consistent `list` so the recreate below
1604 // doesn't re-adopt the just-deleted application and then try to
1605 // update a now-gone id (see `waitForApplicationDeleted`).
1606 yield* waitForApplicationDeleted(name, existing.applicationId);
1607 const result = yield* createApplication({
1608 id,
1609 news,
1610 bindings,
1611 name,
1612 configuration,
1613 durableObjects,
1614 session,
1615 });
1616 return {
1617 ...("applicationId" in result ? result : toAttributes(result)),
1618 hash: {
1619 image: imageHash,
1620 digest: imageDigest,
1621 configuration: configurationHash,
1622 },
1623 dev,
1624 };
1625 }
1626
1627 // Sync — application exists with correct DO attachment. Apply
1628 // the desired configuration (image + scheduling + secrets, etc.)
1629 // through the upsert path, which builds and pushes the image
1630 // only when the hash changed and creates a rollout if the
1631 // configuration drifted.
1632 if (existing) {
1633 return yield* upsertApplication({
1634 id,
1635 news,
1636 bindings,
1637 existing,
1638 // Keep the live attachment through the ghost-recreate fallback
1639 // when the desired value is unresolved.
1640 durableObjects: durableObjectsForRecovery,
1641 session,
1642 });
1643 }
1644
1645 // Ensure — no application exists. Build and push the image,
1646 // then create. `createApplication` itself tolerates concurrent
1647 // creates by adopting an existing application with the same
1648 // name or namespace.
1649 const published = yield* buildAndPushImage(
1650 id,
1651 news,
1652 build,
1653 imageRef,
1654 imageHash,
1655 undefined,
1656 session,
1657 );
1658 const configuration = desiredConfiguration(news, env, published.imageRef);
1659 const configurationHash = yield* applicationConfigurationHash(
1660 scalingDefaults(news),
1661 news.affinities,
1662 configuration,
1663 );
1664 const result = yield* createApplication({
1665 id,
1666 news,
1667 bindings,
1668 name,
1669 configuration,
1670 durableObjects: durableObjectsForRecovery,
1671 session,
1672 });
1673 return {
1674 ...("applicationId" in result ? result : toAttributes(result)),
1675 hash: {
1676 image: imageHash,
1677 digest: published.digest,
1678 configuration: configurationHash,
1679 },
1680 dev,
1681 };
1682 }),
1683 delete: Effect.fn(function* ({ output }) {
1684 // A `dev:` applicationId only exists locally — there is no live
1685 // application to delete on Cloudflare.
1686 if (!output.applicationId || !isLiveId(output.applicationId)) return;
1687 yield* Effect.logInfo(`Cloudflare Container delete: deleting ${output.applicationName}`);
1688 yield* Containers.deleteContainerApplication({
1689 accountId: output.accountId,
1690 applicationId: output.applicationId,
1691 }).pipe(Effect.catchTag("ContainerApplicationNotFound", () => Effect.void));
1692 }),
1693 read: Effect.fn(function* ({ id, olds, output, previous }) {
1694 const readByName = (name: string) =>
1695 Effect.gen(function* () {
1696 yield* Effect.logInfo(`Cloudflare Container read: looking up ${name}`);
1697 const existing = yield* findApplicationByName(name);
1698 if (!existing) {
1699 yield* Effect.logInfo(`Cloudflare Container read: ${name} not found`);
1700 return undefined;
1701 }
1702 return {
1703 ...toAttributes(existing),
1704 hash: output?.hash,
1705 // The dev image is a local build-context reference that the
1706 // API can't return — preserve the persisted one so a refresh
1707 // doesn't wipe it (which would break a later `alchemy dev`).
1708 dev: output?.dev,
1709 image: output?.image,
1710 };
1711 });
1712
1713 let attrs: ContainerApplication["Attributes"] | undefined;
1714 // A `dev:` applicationId never exists on Cloudflare — look the
1715 // application up by its (deterministic) name instead of hitting the
1716 // API with a fake id.
1717 if (output?.applicationId && !isLiveId(output.applicationId)) {
1718 return yield* readByName(output.applicationName);
1719 }
1720 if (output?.applicationId) {
1721 yield* Effect.logInfo(`Cloudflare Container read: checking ${output.applicationName}`);
1722 attrs = yield* Containers.getContainerApplication({
1723 accountId: output.accountId,
1724 applicationId: output.applicationId,
1725 }).pipe(
1726 Effect.map((app) => ({
1727 ...toAttributes(app),
1728 hash: output.hash,
1729 dev: output.dev,
1730 image: output.image,
1731 })),
1732 Effect.catchTag("ContainerApplicationNotFound", () =>
1733 readByName(output.applicationName),
1734 ),
1735 );
1736 // If we matched by id from prior state, treat as owned.
1737 return attrs;
1738 }
1739
1740 const name = yield* createApplicationName(id, nameOf(olds));
1741 attrs = yield* readByName(name);
1742 if (!attrs) return undefined;
1743 return isOwnedApplication(nameOf(olds), attrs, previous) ? attrs : Unowned(attrs);
1744 }),
1745 list: () =>
1746 Effect.gen(function* () {
1747 const { accountId } = yield* yield* CloudflareEnvironment;
1748 // Account-scoped collection. `listContainerApplications` returns
1749 // the full application objects in one (non-paginated) response, so
1750 // each item already carries the complete `read` attributes shape —
1751 // no per-item hydration is required.
1752 return yield* Containers.listContainerApplications({
1753 accountId,
1754 }).pipe(
1755 Effect.map((apps) => apps.map((app) => toAttributes(app))),
1756 // Accounts without the containers product reject the route; treat
1757 // a non-entitled account as an empty collection rather than an
1758 // error.
1759 Effect.catchTag("InvalidRoute", () => Effect.succeed([])),
1760 );
1761 }),
1762 tail: ({ output }) =>
1763 telemetry.tailStream({
1764 accountId: output.accountId,
1765 filters: containerFilters(output.applicationId),
1766 }),
1767 logs: ({ output, options }) =>
1768 telemetry.queryLogs({
1769 accountId: output.accountId,
1770 filters: containerFilters(output.applicationId),
1771 options,
1772 }),
1773 });
1774 }),
1775 );
1776
1777 const containerFilters = (applicationId: string): TelemetryFilter[] => [
1778 {
1779 key: "$metadata.type",
1780 operation: "eq",
1781 type: "string",
1782 value: "cf-container",
1783 },
1784 {
1785 key: "$metadata.service",
1786 operation: "eq",
1787 type: "string",
1788 value: applicationId,
1789 },
1790 ];
1791
1792 const resolveDurableObjectApplicationRecovery = ({
1793 namespaceId,
1794 expectedName,
1795 existingName,
1796 }: {
1797 namespaceId: string;
1798 expectedName: string;
1799 existingName: string | undefined;
1800 }) => {
1801 if (!existingName) {
1802 return {
1803 canAdopt: false as const,
1804 message: `Container application for Durable Object namespace "${namespaceId}" already exists but could not be found for adoption.`,
1805 };
1806 }
1807 if (existingName !== expectedName) {
1808 return {
1809 canAdopt: false as const,
1810 message: `Existing container application "${existingName}" is already attached to Durable Object namespace "${namespaceId}". Use that application name to adopt it.`,
1811 };
1812 }
1813 return {
1814 canAdopt: true as const,
1815 };
1816 };
1817
1818 // Cap each delay at 3s so the readiness window is ~30s over 10 attempts; an
1819 // uncapped `Schedule.exponential(150)` reaches a ~76s single delay by the 10th
1820 // retry (~150s total), which both blows test budgets and needlessly stalls the
1821 // update→create fallback when the target is genuinely gone.
1822 const containerApplicationReadinessSchedule = Schedule.max([
1823 Schedule.min([Schedule.exponential(150), Schedule.spaced("3 seconds")]),
1824 Schedule.recurs(10),
1825 ]);
1826
1827 const isContainerApplicationNotFound = (
1828 error: unknown,
1829 ): error is Containers.ContainerApplicationNotFound =>
1830 typeof error === "object" &&
1831 error !== null &&
1832 "_tag" in error &&
1833 error._tag === "ContainerApplicationNotFound";
1834
1835 export const retryForContainerApplicationReadiness = <A, E, R>(
1836 operation: string,
1837 applicationId: string,
1838 effect: Effect.Effect<A, E, R>,
1839 ) =>
1840 effect.pipe(
1841 Effect.tapError((error) =>
1842 isContainerApplicationNotFound(error)
1843 ? Effect.logDebug(
1844 `Cloudflare Container ${operation}: application ${applicationId} not found yet, retrying`,
1845 )
1846 : Effect.void,
1847 ),
1848 Effect.retry({
1849 while: isContainerApplicationNotFound,
1850 schedule: containerApplicationReadinessSchedule,
1851 }),
1852 );
1853
1854 /**
1855 * Whether an application found by name is ours. A generated name identifies
1856 * this instance by its random suffix. An explicit name alone does not, unless
1857 * it is the application a replacement is replacing.
1858 */
1859 const isOwnedApplication = (
1860 explicitName: unknown,
1861 observed: { applicationId: string },
1862 previous: { applicationId: string } | undefined,
1863 ) => explicitName === undefined || observed.applicationId === previous?.applicationId;
1864
1865 // Durable Object applications have no rollouts.
1866 const hasActiveRollout = (application: Containers.GetContainerApplicationResponse) =>
1867 "activeRolloutId" in application && !!application.activeRolloutId;
1868
1869 const toAttributes = (
1870 application: Containers.CreateContainerApplicationResponse,
1871 ): ContainerApplication["Attributes"] => {
1872 // Durable Object applications have no scaling, affinities or versions.
1873 const scheduled = "version" in application ? application : undefined;
1874 return {
1875 applicationId: application.id,
1876 applicationName: application.name,
1877 accountId: application.accountId,
1878 schedulingPolicy: application.schedulingPolicy,
1879 instances: scheduled?.instances ?? undefined,
1880 maxInstances: scheduled?.maxInstances ?? undefined,
1881 constraints: normalizeNulls(
1882 application.constraints as ContainerApplication.Constraints | undefined,
1883 ),
1884 affinities: normalizeNulls(
1885 scheduled?.affinities as ContainerApplication.Affinities | undefined,
1886 ),
1887 configuration: normalizeNulls(
1888 application.configuration,
1889 ) as Partial<ContainerApplication.Configuration>,
1890 observability: normalizeNulls(application.observability ?? undefined),
1891 durableObjects: normalizeNulls(application.durableObjects) as
1892 | { namespaceId: string }
1893 | undefined,
1894 createdAt: application.createdAt,
1895 version: scheduled?.version ?? undefined,
1896 dev: undefined,
1897 };
1898 };