sailfork / t3code-github / .repos/ alchemy-effect/ packages/ prisma/ src/ Deployment.ts

import { Retry } from "@distilled.cloud/prisma";
import {
  type GetServiceDeploymentsResponse,
  getServiceDeployments,
  createServiceDeployment,
} from "@distilled.cloud/prisma/management";
import { deepEqual, isResolved } from "alchemy/Diff";
import * as ProviderLayer from "alchemy/Local/ProviderLayer";
import * as Provider from "alchemy/Provider";
import { Resource } from "alchemy/Resource";
import { sha256Object } from "alchemy/Util/sha256";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Path from "effect/Path";
import * as Redacted from "effect/Redacted";
import * as Stream from "effect/Stream";
import type { App } from "./App.ts";
import {
  destroyDeployment,
  PrismaDeploymentWaitTimeout,
  waitForDeploymentStatus,
} from "./ComputeLifecycle.ts";
import { promoteAppObserved } from "./internal/AppPromotion.ts";
import {
  inspectArtifactFile,
  readArtifactFile,
  type ArtifactFile,
} from "./internal/ArtifactFile.ts";
import { executeArtifactUpload } from "./internal/ArtifactUpload.ts";
import { aggregateCleanupFailure } from "./internal/CleanupFailure.ts";
import { startDeploymentIdempotent } from "./internal/DeploymentActions.ts";
import { ensureDeploymentMembership } from "./internal/DeploymentIdentity.ts";
import { observeDeployment } from "./internal/DeploymentObserve.ts";
import { DEV_TIMESTAMP, attrOrString, devId, devProvider } from "./internal/DevStub.ts";
import type { ObservedDeployment } from "./internal/Observed.ts";
import { PrismaPaginationError } from "./internal/Pagination.ts";
import {
  concreteIdsChanged,
  isInputObject,
  isPrismaDevId,
  resolveAppId,
  unresolvedAppIdOf,
} from "./internal/Refs.ts";
import { tailDeploymentLogs } from "./PrismaLogs.ts";
import type { Providers } from "./Providers.ts";

export class DeploymentConflict extends Data.TaggedError("DeploymentConflict")<{
  readonly message: string;
  readonly cause?: unknown;
}> {}

export class DeploymentInvalidProps extends Data.TaggedError("DeploymentInvalidProps")<{
  readonly message: string;
  readonly cause?: unknown;
}> {}

export class DeploymentOperationFailed extends Data.TaggedError("DeploymentOperationFailed")<{
  readonly message: string;
  readonly cause?: unknown;
}> {}

export class DeploymentUnexpectedResponse extends Data.TaggedError("DeploymentUnexpectedResponse")<{
  readonly message: string;
  readonly cause?: unknown;
}> {}

export const MAX_DEPLOYMENT_ARTIFACT_BYTES = 768 * 1024 * 1024;

export interface DeploymentProps {
  /**
   * App ID or Prisma.App resource that owns this deployment.
   */
  app: string | App;
  /**
   * Port mapping for the deployment. Set `http` to `null` to reset to
   * Foundry's default port (8080); non-null values must be integers from
   * 1 through 65535.
   */
  portMapping?: { http?: number | null };
  /**
   * Create the deployment by reusing the App's currently promoted artifact
   * instead of uploading code. Requires an existing promoted deployment.
   */
  skipCodeUpload?: boolean;
  /**
   * Path to a pre-created artifact file to upload to the pre-signed upload URL.
   */
  artifactPath?: string;
  /**
   * Content type for artifact uploads.
   *
   * @default "application/octet-stream"
   */
  artifactContentType?: string;
  /**
   * Start the deployment after it is created.
   *
   * @default false
   */
  start?: boolean;
  /**
   * Promote the deployment to the App's stable endpoint after start.
   * If `start` is omitted, enabling promotion starts the deployment first.
   *
   * @default false
   */
  promote?: boolean;
  /**
   * Opaque key/value map; when any resolved value changes, a replacement
   * deployment is planned (create-before-delete) even if the artifact is
   * unchanged — the same contract as `AWS.ApiGateway.Deployment.triggers`.
   * Because values here may be secrets, they are folded into the deployment
   * fingerprint as a salted hash and persisted `Redacted` rather than
   * compared as plaintext; plaintext never lands in state.
   *
   * Prisma snapshots environment variables into a deployment when the
   * deployment is created, so changing only an environment variable's value
   * updates the platform's variable record but never reaches the running app.
   * Pass those values here to make such a change take effect.
   *
   * Members may be wrapped in `Redacted.make(secret)`; they are unwrapped only
   * to compute the fingerprint.
   *
   * If the value cannot be resolved while the deploy is being planned —
   * because it reads an attribute of another resource that the same deploy is
   * changing — the diff cannot prove the fingerprint is unchanged, and the
   * engine offers no later opportunity to plan a replacement. The deployment
   * is then replaced conservatively, but only once a fingerprint has already
   * been recorded: adding `triggers` to an existing deployment records its
   * fingerprint through a plain update rather than forcing a replacement.
   *
   * Leaving `triggers` unset tracks nothing, so removing it from a
   * deployment that had it does not trigger a replacement on its own.
   *
   * @example
   * ```typescript
   * const deployment = yield* Prisma.Deployment("web", {
   *   app,
   *   artifactPath: "./dist/app.tar.gz",
   *   triggers: { DATABASE_URL: Redacted.make(databaseUrl) },
   *   start: true,
   *   promote: true,
   * });
   * ```
   */
  triggers?: Record<string, unknown>;
}

export interface Deployment extends Resource<
  "Prisma.Deployment",
  DeploymentProps,
  {
    /**
     * Prisma deployment ID.
     */
    deploymentId: string;
    /**
     * App ID that owns the deployment.
     */
    appId: string;
    /**
     * Foundry version ID returned by the Prisma Management API.
     */
    foundryVersionId: string;
    /**
     * Current deployment status, when observed.
     */
    status: string | undefined;
    /**
     * Preview endpoint domain for the deployment.
     */
    previewDomain: string | null | undefined;
    /**
     * Hash of the artifact bytes uploaded for this deployment, when Alchemy
     * uploaded an artifact.
     */
    artifactHash?: string;
    /**
     * Salted fingerprint of the resolved `triggers` inputs this deployment
     * was created for. Held `Redacted` so the inputs stay out of plaintext
     * state; absent when `triggers` is unset.
     */
    triggersHash?: Redacted.Redacted<string>;
    /**
     * Stable App endpoint domain after promotion.
     */
    appEndpointDomain: string | undefined;
    /**
     * ISO timestamp when the deployment was created, when observed.
     */
    createdAt: string | undefined;
  },
  never,
  Providers
> {}

/**
 * A Prisma deployment owned by an App.
 *
 * This is the low-level resource: it can upload or reuse an artifact, start it,
 * and promote it, but it does not provide `Prisma.Compute`'s preview/stable
 * health checks or automatic rollback. Prefer `Prisma.Compute` for production
 * application deployments.
 *
 * Prisma's create-deployment API currently exposes neither an idempotency key
 * nor a caller-defined natural key. After a crash that loses state immediately
 * after creation, Alchemy deliberately does not adopt the App's latest
 * deployment: doing so could take ownership of an unrelated deployment. When
 * persisted state contains a Foundry version ID, refresh may safely recover the
 * matching deployment.
 *
 * ### Creating a Deployment
 * **Example:** Fork the currently promoted artifact
 * ```typescript
 * const deployment = yield* Prisma.Deployment("web-v2", {
 *   app: app.appId,
 *   skipCodeUpload: true,
 *   start: true,
 *   promote: true,
 * });
 * ```
 *
 * **Example:** Upload a prebuilt artifact
 * ```typescript
 * const deployment = yield* Prisma.Deployment("web-v3", {
 *   app: app.appId,
 *   artifactPath: "./dist/app.tar.gz",
 *   artifactContentType: "application/gzip",
 *   start: true,
 *   promote: true,
 * });
 * ```
 *
 * @resource
 * @product Compute
 */
export const Deployment = Resource<Deployment>("Prisma.Deployment");

// Distilled emits the cursor-paginated list operations as plain ops, so
// callers walk `pagination` themselves (see `src/Neon/Project.ts`).
const listAppDeployments = (appId: string) =>
  Effect.gen(function* () {
    const deployments: GetServiceDeploymentsResponse["data"][number][] = [];
    let cursor: string | undefined;
    while (true) {
      const page = yield* getServiceDeployments(
        cursor === undefined
          ? { serviceId: appId, limit: 100 }
          : { serviceId: appId, limit: 100, cursor },
      );
      deployments.push(...page.data);
      const nextCursor = page.pagination.nextCursor;
      if (!page.pagination.hasMore) break;
      if (nextCursor === null) {
        return yield* new PrismaPaginationError({
          message:
            "Invalid Prisma Management API pagination response from getServiceDeployments: hasMore was true without a non-empty nextCursor",
        });
      }
      cursor = nextCursor;
    }
    return deployments;
  });

const findDeployment = (appId: string, foundryVersionId: string | undefined) =>
  foundryVersionId === undefined
    ? Effect.undefined
    : listAppDeployments(appId).pipe(
        Effect.flatMap((deployments) => {
          const matches = deployments.filter(
            (deployment) => deployment.foundryVersionId === foundryVersionId,
          );
          return matches.length > 1
            ? Effect.fail(
                new DeploymentConflict({
                  message: `Prisma returned multiple deployments with Foundry version ID '${foundryVersionId}' for App '${appId}'; refusing an ambiguous recovery match.`,
                }),
              )
            : Effect.succeed(matches[0]);
        }),
      );

const attrsFrom = (
  deployment: ObservedDeployment,
  appId: string,
  extra?: {
    artifactHash?: string;
    triggersHash?: Redacted.Redacted<string>;
    appEndpointDomain?: string;
  },
): Deployment["Attributes"] => ({
  deploymentId: deployment.id,
  appId,
  foundryVersionId: deployment.foundryVersionId,
  status: deployment.status,
  previewDomain: deployment.previewDomain,
  artifactHash: extra?.artifactHash,
  triggersHash: extra?.triggersHash,
  appEndpointDomain: extra?.appEndpointDomain,
  createdAt: deployment.createdAt,
});

export class InvalidArtifactUploadUrlError extends Data.TaggedError(
  "InvalidArtifactUploadUrlError",
)<{
  readonly message: string;
  readonly cause?: unknown;
}> {}

export interface ReadUploadArtifactInput {
  artifact?: string | Uint8Array;
  artifactPath?: string;
  output?: "bytes" | "file";
}

export function readUploadArtifact(
  input: ReadUploadArtifactInput & { readonly output: "file" },
): Effect.Effect<ArtifactFile | undefined, Error, Path.Path>;
export function readUploadArtifact(
  input: ReadUploadArtifactInput & { readonly output?: "bytes" },
): Effect.Effect<Uint8Array | undefined, Error, Path.Path>;
export function readUploadArtifact(
  input: ReadUploadArtifactInput,
): Effect.Effect<Uint8Array | ArtifactFile | undefined, Error, Path.Path>;
export function readUploadArtifact(input: ReadUploadArtifactInput) {
  return readUploadArtifactInternal(input);
}

const readUploadArtifactInternal = Effect.fn(function* (input: ReadUploadArtifactInput) {
  if (input.artifact !== undefined && input.artifactPath !== undefined) {
    return yield* new DeploymentInvalidProps({
      message: "artifact and artifactPath are mutually exclusive.",
    });
  }
  if (input.output === "file" && input.artifact !== undefined) {
    return yield* new DeploymentInvalidProps({
      message: "File-backed artifact output requires artifactPath.",
    });
  }
  if (input.artifactPath !== undefined) {
    const path = yield* Path.Path;
    const resolved = path.resolve(input.artifactPath);
    const artifact = yield* inspectArtifactFile(resolved, MAX_DEPLOYMENT_ARTIFACT_BYTES);
    if (input.output === "file") {
      return artifact;
    }
    return yield* readArtifactFile(artifact);
  }
  if (input.artifact !== undefined) {
    const artifact = input.artifact;
    const bytes = yield* Effect.sync(() =>
      typeof artifact === "string" ? new TextEncoder().encode(artifact) : artifact,
    );
    return yield* validateDeploymentArtifactBytes(bytes);
  }
  return undefined;
});

export const validateDeploymentArtifactBytes = (
  artifact: Uint8Array,
  maxBytes = MAX_DEPLOYMENT_ARTIFACT_BYTES,
) =>
  !Number.isSafeInteger(maxBytes) || maxBytes <= 0
    ? Effect.fail(
        new DeploymentInvalidProps({
          message: "Artifact maxBytes must be a positive safe integer.",
        }),
      )
    : maxBytes > MAX_DEPLOYMENT_ARTIFACT_BYTES
      ? Effect.fail(
          new DeploymentInvalidProps({
            message: `Artifact maxBytes must not exceed the hard limit of ${MAX_DEPLOYMENT_ARTIFACT_BYTES}.`,
          }),
        )
      : artifact.byteLength === 0
        ? Effect.fail(
            new DeploymentInvalidProps({
              message: "Prisma deployment artifact must be non-empty.",
            }),
          )
        : artifact.byteLength > maxBytes
          ? Effect.fail(
              new DeploymentInvalidProps({
                message: `Prisma deployment artifact exceeds the ${maxBytes} byte upload safety limit.`,
              }),
            )
          : Effect.succeed(artifact);

const artifactHashOf = Effect.fn(function* (props: DeploymentProps) {
  const artifact = yield* readUploadArtifact({
    artifactPath: props.artifactPath,
    output: "file",
  });
  if (artifact === undefined) return undefined;
  return yield* sha256Object({
    artifact: artifact.sha256,
    contentType: props.artifactContentType ?? "application/octet-stream",
  });
});

/**
 * Domain separator so a `triggers` fingerprint is never the bare SHA-256 of
 * a secret value, which would otherwise be comparable against a precomputed
 * digest of a guessed value.
 */
const TRIGGERS_HASH_SALT = "alchemy/Prisma.Deployment/triggers/v1";

const unwrapRedacted = (value: unknown): unknown => {
  if (Redacted.isRedacted(value)) return value.pipe(Redacted.value, unwrapRedacted);
  if (Array.isArray(value)) return value.map(unwrapRedacted);
  if (value !== null && typeof value === "object") {
    return Object.fromEntries(
      Object.entries(value).map(([key, item]) => [key, unwrapRedacted(item)]),
    );
  }
  return value;
};

const triggersHashOf = (triggers: unknown) =>
  sha256Object({
    salt: TRIGGERS_HASH_SALT,
    triggers: unwrapRedacted(triggers) ?? null,
  });

const persistedTriggersHash = (value: Redacted.Redacted<string> | string | undefined) =>
  Redacted.isRedacted(value) ? Redacted.value(value) : value;

/**
 * Whether the declared `triggers` inputs differ from the fingerprint the
 * running deployment was created for.
 *
 * Only `diff` can plan a replacement — `reconcile` has no way to ask for one —
 * so an input that is still unresolved while planning is treated as changed.
 * That is only reached when another resource in the same deploy owns the
 * value and is itself changing, in which case the value most likely changed
 * too; the cost of being wrong is one extra create-before-delete replacement.
 * Without a recorded fingerprint there is nothing to compare against, so the
 * first deploy that declares `triggers` records it through the engine's
 * default update instead of replacing the deployment.
 */
const triggersChanged = Effect.fn(function* (
  triggers: unknown,
  persisted: Redacted.Redacted<string> | string | undefined,
) {
  const recorded = persistedTriggersHash(persisted);
  if (triggers === undefined || recorded === undefined) return false;
  if (!isResolved({ triggers })) return true;
  return (yield* triggersHashOf(triggers)) !== recorded;
});

export const uploadArtifact = (
  uploadUrl: string,
  artifact: Uint8Array | ArtifactFile,
  contentType: string,
) =>
  Effect.gen(function* () {
    if (artifact instanceof Uint8Array) {
      yield* validateDeploymentArtifactBytes(artifact);
    } else if (artifact.size <= 0 || artifact.size > MAX_DEPLOYMENT_ARTIFACT_BYTES) {
      return yield* new DeploymentInvalidProps({
        message: `Prisma deployment artifact exceeds the ${MAX_DEPLOYMENT_ARTIFACT_BYTES} byte upload safety limit.`,
      });
    }
    yield* Effect.try({
      try: () => {
        const parsed = new URL(uploadUrl);
        if (
          parsed.protocol !== "https:" ||
          parsed.username.length > 0 ||
          parsed.password.length > 0
        ) {
          throw new Error("invalid upload URL");
        }
      },
      catch: (cause) =>
        new InvalidArtifactUploadUrlError({
          message: "Prisma artifact upload URL must be credential-free HTTPS.",
          cause,
        }),
    });
    yield* executeArtifactUpload(uploadUrl, artifact, contentType);
  });

const ProviderLive = () =>
  Provider.effect(
    Deployment,
    Effect.gen(function* () {
      return {
        stables: ["deploymentId"],
        // App deletion cascades deployments. AppProvider is the single nuke
        // enumerator so a deployment is never deleted twice during teardown.
        list: () => Effect.succeed([]),
        diff: Effect.fn(function* ({ olds, news, output }) {
          if (!isInputObject(news)) return undefined;
          if (isPrismaDevId(output?.deploymentId)) {
            return { action: "update" } as const;
          }
          // `deploymentId` is stable and Foundry's failed status is terminal.
          // Route drift recovery through the engine's create-before-delete
          // replacement lifecycle instead of trying to restart the failed
          // generation or changing a stable ID during an update.
          if (output?.status === "failed") {
            return { action: "replace" } as const;
          }
          // Checked before the resolved-content early return below: a
          // deferred diff falls back to an update, which reuses the running
          // deployment and would never apply the changed inputs.
          if (yield* triggersChanged(news.triggers, output?.triggersHash)) {
            return { action: "replace" } as const;
          }
          const replacementContent = {
            portMapping: news.portMapping,
            skipCodeUpload: news.skipCodeUpload,
            artifactPath: news.artifactPath,
            artifactContentType: news.artifactContentType,
          };
          if (!isResolved(replacementContent)) return undefined;
          const resolvedReplacementContent = replacementContent as Pick<
            DeploymentProps,
            "portMapping" | "skipCodeUpload" | "artifactPath" | "artifactContentType"
          >;
          const oldAppId = output?.appId ?? unresolvedAppIdOf(olds.app);
          const newAppId = isResolved(news.app) ? unresolvedAppIdOf(news.app) : undefined;
          const oldArtifactHash = output?.artifactHash;
          const newArtifactHash = yield* artifactHashOf({
            app: olds.app,
            ...resolvedReplacementContent,
          });
          const appChanged = concreteIdsChanged(oldAppId, newAppId);
          if (
            appChanged ||
            !deepEqual(resolvedReplacementContent.portMapping ?? {}, olds.portMapping ?? {}) ||
            (resolvedReplacementContent.skipCodeUpload ?? false) !==
              (olds.skipCodeUpload ?? false) ||
            resolvedReplacementContent.artifactPath !== olds.artifactPath ||
            resolvedReplacementContent.artifactContentType !== olds.artifactContentType ||
            (newArtifactHash !== undefined && newArtifactHash !== oldArtifactHash)
          ) {
            return { action: "replace" } as const;
          }
          const updateProps = {
            start: news.start,
            promote: news.promote,
          };
          if (!isResolved(updateProps)) return undefined;
          const resolvedUpdateProps = updateProps as Pick<DeploymentProps, "start" | "promote">;
          if ((resolvedUpdateProps.start ?? false) || (resolvedUpdateProps.promote ?? false)) {
            // Reconcile asserted lifecycle state on every deploy so external
            // stops and App routing drift are repaired.
            return { action: "update" } as const;
          }
          if (
            (resolvedUpdateProps.start ?? false) !== (olds.start ?? false) ||
            (resolvedUpdateProps.promote ?? false) !== (olds.promote ?? false)
          ) {
            return { action: "update" } as const;
          }
          return undefined;
        }),
        read: Effect.fn(function* ({ output, olds }) {
          if (isPrismaDevId(output?.deploymentId)) return undefined;
          const appId =
            output?.appId && !isPrismaDevId(output.appId)
              ? output.appId
              : yield* resolveAppId(olds.app);
          const savedDeployment = output?.deploymentId
            ? yield* observeDeployment(output.deploymentId).pipe(
                Effect.catchTag("NotFound", () => Effect.undefined),
              )
            : undefined;
          const listed = savedDeployment
            ? undefined
            : yield* findDeployment(appId, output?.foundryVersionId);
          const deployment =
            savedDeployment ?? (listed ? yield* observeDeployment(listed.id) : undefined);
          if (savedDeployment) {
            yield* ensureDeploymentMembership(appId, savedDeployment);
          }
          return deployment ? attrsFrom(deployment, appId, output) : undefined;
        }),
        reconcile: Effect.fn(function* ({ news, output }) {
          if (
            news.portMapping?.http !== undefined &&
            news.portMapping.http !== null &&
            (!Number.isInteger(news.portMapping.http) ||
              news.portMapping.http < 1 ||
              news.portMapping.http > 65_535)
          ) {
            return yield* new DeploymentInvalidProps({
              message: "portMapping.http must be an integer between 1 and 65535.",
            });
          }
          if (!(news.skipCodeUpload ?? false) && news.artifactPath === undefined) {
            return yield* new DeploymentInvalidProps({
              message: "Prisma.Deployment requires artifactPath or skipCodeUpload: true.",
            });
          }
          if ((news.skipCodeUpload ?? false) && news.artifactPath !== undefined) {
            return yield* new DeploymentInvalidProps({
              message: "skipCodeUpload cannot be combined with artifactPath.",
            });
          }
          if ((news.promote ?? false) && news.start === false) {
            return yield* new DeploymentInvalidProps({
              message: "promote cannot be combined with start: false.",
            });
          }
          const artifact = yield* readUploadArtifact({
            artifactPath: news.artifactPath,
            output: "file",
          });
          const artifactHash =
            artifact === undefined
              ? output?.artifactHash
              : yield* sha256Object({
                  artifact: artifact.sha256,
                  contentType: news.artifactContentType ?? "application/octet-stream",
                });
          const triggersHash =
            news.triggers === undefined
              ? undefined
              : Redacted.make(yield* triggersHashOf(news.triggers));
          const appId = yield* resolveAppId(news.app);
          const deploymentId = isPrismaDevId(output?.deploymentId)
            ? undefined
            : output?.deploymentId;
          let deployment: ObservedDeployment | undefined = deploymentId
            ? yield* observeDeployment(deploymentId).pipe(
                Effect.catchTag("NotFound", () => Effect.undefined),
              )
            : undefined;
          if (deployment) {
            yield* ensureDeploymentMembership(appId, deployment);
            if (deployment.status === "failed") {
              return yield* new DeploymentOperationFailed({
                message: `Prisma deployment '${deployment.id}' is in terminal status 'failed' and must be replaced before reconcile; refusing to restart it or change the stable deploymentId during an update.`,
              });
            }
          }
          let createdDeploymentId: string | undefined;
          const cleanupCreatedDeploymentOnFailure = (failedDeploymentId: string, error: unknown) =>
            createdDeploymentId === failedDeploymentId
              ? destroyDeployment(failedDeploymentId).pipe(
                  Effect.mapError((cleanupError) =>
                    aggregateCleanupFailure(
                      "deployment",
                      failedDeploymentId,
                      `/v1/deployments/${failedDeploymentId}`,
                      error,
                      cleanupError,
                    ),
                  ),
                  Effect.andThen(() => Effect.fail(error)),
                )
              : Effect.fail(error);

          if (!deployment) {
            const created = yield* createServiceDeployment({
              serviceId: appId,
              ...(news.portMapping === undefined ? {} : { portMapping: news.portMapping }),
              ...(news.skipCodeUpload === undefined ? {} : { skipCodeUpload: news.skipCodeUpload }),
            }).pipe(
              // A replayed create would make a second deployment; the retry
              // policy cannot see the request, so opt out explicitly.
              Retry.none,
              Effect.map((response) => response.data),
            );
            createdDeploymentId = created.id;
            if (artifact !== undefined && !created.uploadUrl) {
              return yield* cleanupCreatedDeploymentOnFailure(
                created.id,
                new Error("Prisma deployment creation did not return an upload URL."),
              );
            }
            if (created.uploadUrl && artifact !== undefined) {
              yield* uploadArtifact(
                created.uploadUrl,
                artifact,
                news.artifactContentType ?? "application/octet-stream",
              ).pipe(Effect.catch((error) => cleanupCreatedDeploymentOnFailure(created.id, error)));
            }
            deployment = yield* observeDeployment(created.id).pipe(
              Effect.catchTag("NotFound", () =>
                Effect.succeed({
                  id: created.id,
                  foundryVersionId: created.foundryVersionId,
                  status: "new",
                  previewDomain: null,
                  createdAt: undefined,
                }),
              ),
              Effect.catch((error) => cleanupCreatedDeploymentOnFailure(created.id, error)),
            );
          }
          if (!deployment) {
            return yield* new DeploymentUnexpectedResponse({
              message: "Prisma deployment could not be resolved after creation.",
            });
          }

          let appEndpointDomain = output?.appEndpointDomain;
          const shouldStart = news.start ?? news.promote ?? false;
          if (shouldStart) {
            const currentDeployment = deployment;
            const currentDeploymentId = currentDeployment.id;
            deployment = yield* Effect.gen(function* () {
              if (
                currentDeployment.status !== "running" &&
                currentDeployment.status !== "provisioning"
              ) {
                const started = yield* startDeploymentIdempotent(currentDeployment.id);
                if (started) {
                  return yield* waitForDeploymentStatus(currentDeployment.id, "running").pipe(
                    Effect.map((running) => ({
                      ...running,
                      previewDomain: started.previewDomain,
                    })),
                  );
                }
              }
              return yield* waitForDeploymentStatus(currentDeployment.id, "running");
            }).pipe(
              Effect.catch((error) =>
                cleanupCreatedDeploymentOnFailure(currentDeploymentId, error),
              ),
            );
          }
          if (news.promote ?? false) {
            // Promotion is deliberately replayed even when the control-plane
            // record already names this deployment. The endpoint operation also
            // repairs provider routing and custom-domain assignment drift.
            const promoted = yield* promoteAppObserved(appId, deployment.id);
            appEndpointDomain = promoted.appEndpointDomain;
          }

          return attrsFrom(deployment, appId, {
            artifactHash,
            triggersHash,
            appEndpointDomain,
          });
        }),
        delete: Effect.fn(function* ({ output }) {
          if (isPrismaDevId(output.deploymentId)) return;
          const deployment = yield* observeDeployment(output.deploymentId).pipe(
            Effect.catchTag("NotFound", () => Effect.undefined),
          );
          if (!deployment) return;
          yield* ensureDeploymentMembership(output.appId, deployment);
          yield* destroyDeployment(output.deploymentId).pipe(
            // The stop was requested; it finishes once open connections close.
            Effect.catchIf(
              (error) => error instanceof PrismaDeploymentWaitTimeout,
              (error) =>
                Effect.fail(
                  new Provider.DeleteInProgress({
                    message: `${error.message}. Prisma Compute drains open connections before it stops a deployment, and only a stopped deployment can be deleted.`,
                  }),
                ),
            ),
          );
        }),
        tail: ({ output }) =>
          output.deploymentId ? tailDeploymentLogs(output.deploymentId) : Stream.empty,
      };
    }),
  );

const ProviderLocal = () =>
  devProvider(Deployment, ["deploymentId"], ({ id, news }) => ({
    deploymentId: devId("deployment", id),
    appId: attrOrString(news.app, "appId"),
    foundryVersionId: devId("foundry-version", id),
    status: "new",
    previewDomain: undefined,
    artifactHash: undefined,
    triggersHash: undefined,
    appEndpointDomain: undefined,
    createdAt: DEV_TIMESTAMP,
  }));

export const DeploymentProvider = () =>
  ProviderLayer.dual(Deployment, {
    local: () => ProviderLocal(),
    live: () => ProviderLive(),
  });