diff --git a/docs/design/05-prisma-cloud/alchemy-lowering.md b/docs/design/05-prisma-cloud/alchemy-lowering.md index 72ad47943..441266007 100644 --- a/docs/design/05-prisma-cloud/alchemy-lowering.md +++ b/docs/design/05-prisma-cloud/alchemy-lowering.md @@ -67,9 +67,9 @@ behind them. | `Prisma.Project` | Project | name | projectId | **one per Prisma Composer application**; resolved by the CLI before Alchemy runs, so no lowering yields one | | `Prisma.Database` | Database | project, name?, region, branchId?, logicalId? | databaseId, connection strings | one per Module-provisioned postgres resource; never the project default; a branch-attached database is created with `branchId` and no display name (upstream refuses the combination — see [deploying.md](../../guides/deploying.md)) | | `Prisma.Connection` | database connection info | database, name | connectionId, directConnectionString | Composer binds the DIRECT string explicitly; upstream's `databaseUrl` is pooled-first | -| `Prisma.App` | App | project, displayName, regionId, branchId?, logicalId? | appId, appEndpointDomain | `branchId` targets the Branch of an environment other than `production`; omitted, upstream attaches the App to the project's default (production) Branch. `appEndpointDomain` is available at provision — that is what a service's own origin is read from | +| `Prisma.App` | App | project, displayName, regionId, branchId?, logicalId? | appId, appEndpointDomain | `branchId` targets the Branch of an environment other than `production`; omitted, upstream attaches the App to the project's default (production) Branch. `appEndpointDomain` is available at provision — that is what a service's own origin and its published `url` are read from | | `Prisma.EnvironmentVariable` | ConfigVariable | project, class, key, value (Redacted), branchId? | environmentVariableId | production-class with no `branchId` for `production`; preview-class with `branchId` for any other environment. Values are write-only, so upstream re-applies the desired one on every deploy | -| `Prisma.Deployment` | Deployment (ComputeVersion) + Promotion | app, artifactPath, artifactContentType, portMapping, triggers, start, promote | deploymentId, appEndpointDomain | provider reconcile: create → upload tar.gz → start → poll until running → promote; `appEndpointDomain` read **post-promote** (create-time domain is a placeholder — PRO-200). It is replaced, not updated, when its artifact fingerprint or its `triggers` fingerprint moves | +| `Prisma.Deployment` | Deployment (ComputeVersion) + Promotion | app, artifactPath, artifactContentType, portMapping, triggers, start, promote | deploymentId, appEndpointDomain | provider reconcile: create → upload tar.gz → start → poll until running → promote; `appEndpointDomain` read **post-promote**; it is the same domain the App reports from its create. It is replaced, not updated, when its artifact fingerprint or its `triggers` fingerprint moves | | `Prisma.Bucket` | Bucket | project, name, branchId?, logicalId? | bucketId, name | one per bucket resource; `Prisma.BucketAccessKey` mints its read-write S3 credentials, which become the node's outputs | `logicalId` is set on every deploy except branchless local dev; [§ Platform identity](#platform-identity-logicalid) says what it carries. @@ -178,14 +178,7 @@ logic is untouched. Management API; the table above is that mapping. One resource maps to one PDP entity except `Deployment`, which spans version-create + upload + start + promote (and therefore owns the env-snapshot moment). -- **PDP → ours**: `foundryVersionId`, `Promotion`, and Foundry's version record - have no resource of ours; they are internal to the `Deployment` provider's - behavior or unmodeled. **Branch** likewise has no Alchemy resource — it is - resolved and its lifecycle managed by the CLI's container-resolution - client, outside the Alchemy graph entirely (see - [§ Environments and container resolution](#environments-and-container-resolution)). - `serviceEndpointDomain` surfaces as `App.appEndpointDomain` (before the first - deploy) and `Deployment.appEndpointDomain` (post-promote). +- **PDP → ours**: `foundryVersionId`, `Promotion`, and Foundry's version record have no resource of ours; they are internal to the `Deployment` provider's behavior or unmodeled. **Branch** likewise has no Alchemy resource — it is resolved and its lifecycle managed by the CLI's container-resolution client, outside the Alchemy graph entirely (see [§ Environments and container resolution](#environments-and-container-resolution)). `serviceEndpointDomain` surfaces as `App.appEndpointDomain` and `Deployment.appEndpointDomain`; both are the same domain. ## The lowering graphs @@ -215,8 +208,9 @@ flowchart TB Ss[App shop.storefront] --> Ds[Deployment shop.storefront.deployment] EVa -- resource ref --> Da EVs -- resource ref --> Ds - Da -- appEndpointDomain --> EVu["EnvironmentVariable(COMPOSER_SHOP_STOREFRONT_AUTH_URL)"] + Sa -- appEndpointDomain --> EVu["EnvironmentVariable(COMPOSER_SHOP_STOREFRONT_AUTH_URL)"] EVu -- resource ref --> Ds + Da -- "serviceId (after promote)" --> Ds end ``` @@ -227,12 +221,9 @@ How the pieces map: - **Each service** lowers to an `App → Deployment` chain plus its own `Database → Connection`, whose url is written as that service's **explicitly named** variable — the same `serialize` path as any other config value. -- **The connection** lowers to two edges: the producer's endpoint domain flows - into a named `EnvironmentVariable`, and that **whole resource flows into the - consumer's `Deployment`** through its `app` prop. -- Every `EnvironmentVariable` a Deployment boots with is threaded into its - `app` — database URLs and connection URLs alike — so the deployment - depends on its config being written first. +- **The connection** lowers to two edges: the producer's App's endpoint domain flows into a named `EnvironmentVariable`, and that **whole resource flows into the consumer's `Deployment`** through its `app` prop. The domain comes from the App, not the Deployment: the Deployment is promoted on every deploy, so its domain is unknown while planning, and a consumer's deployment, whose triggers hold the value, would be replaced on every deploy. Because the App's domain is known, an unchanged consumer keeps its deployment when its producer is redeployed. +- Every `EnvironmentVariable` a Deployment boots with is threaded into its `app` — database URLs and connection URLs alike — so the deployment depends on its config being written first. +- Every output of a consumer's producers is threaded into its Deployment's `app` too, so its deployment waits for its producers' promotion: a Compute producer's `serviceId` comes from its Deployment and resolves only after the promotion. The wait is on `app`, not in `triggers`, and upstream treats an unresolved `app` as unchanged, so an unchanged consumer is not replaced. - The Deployment's `portMapping.http` rides the same seam: `serialize` resolves the service's `port` param from the typed Config and surfaces it in its outputs, and `deploy` routes the platform to it — so the routed port and the @@ -247,25 +238,11 @@ deployment-create, so new and replacement deployments boot with the completed configuration updates. Without it the two race — the failure documented as PRO-211 in `gotchas.md`. -**Why the edge rides `app`.** Upstream's `Prisma.Deployment` has no -`environment` prop (Composer's deleted one did). Alchemy derives its dependency -graph from the resource references a prop's *value* is built from, so the -descriptor builds `app` as an Output over the app id AND every whole variable -resource, resolving to the app id itself: the graph gains the edges. A persisted -variable ID can resolve before its pending value update finishes; the whole -resource keeps that update as a dependency. The edge cannot ride -`artifactPath` (or any of upstream's other replacement-block props): the diff -reads that block as one unit and gives no opinion the moment any member is -unresolved — and a brand-new variable's reference IS unresolved at plan time — -which would skip the artifact comparison and silently drop a code change. -`compute/deployment-edge.ts` records the full argument and its test drives -upstream's real diff. +**Why the edge rides `app`.** Upstream's `Prisma.Deployment` has no `environment` prop (Composer's deleted one did). Alchemy derives its dependency graph from the resource references a prop's *value* is built from, so the descriptor builds `app` as an Output over the app id, every whole variable resource and every output of the service's producers, resolving to the app id itself: the graph gains the edges. A persisted variable ID can resolve before its pending value update finishes; the whole resource keeps that update as a dependency. The edge cannot ride `artifactPath` (or any of upstream's other replacement-block props): the diff reads that block as one unit and gives no opinion the moment any member is unresolved — and a brand-new variable's reference IS unresolved at plan time — which would skip the artifact comparison and silently drop a code change. [ADR-0048](../90-decisions/ADR-0048-prisma-cloud-resources-come-from-the-upstream-alchemy-provider.md) records the full argument, and `compute/__tests__/deployment-edge.test.ts` drives upstream's real diff. **Change propagation is wired through `Prisma.Deployment.triggers`.** The platform freezes a deployment's environment at create, so a *value* change (a rotated URL) reaches a running service only through a new deployment. The compute descriptor declares one trigger member per environment row, carrying the same `Redacted` value the row writes; upstream folds the resolved members into a salted fingerprint inside its own encrypted state and plans a replacement exactly when one moved (and conservatively when a member cannot be resolved at plan time because the producing resource is itself changing). Unchanged service → same fingerprint → reuse; changed value or artifact → replace. Because upstream resolves the actual values, a value re-issued under a stable resource identity (a connection rotated in place, a re-minted service key) moves the fingerprint too. No plaintext lands in state — the members are `Redacted` in props and only the salted hash is persisted. A platform variable a row merely POINTS at (operator-owned; Composer never reads its value) contributes a `:updatedAt` member instead, read at preflight and handed to the lowering in the extension's preflight payload, so an out-of-band rotation still ships a new deployment. A service's input document is not an environment row and has no trigger member: it travels in the service's artifact, in the `inputDocument` field of `compute.bootstrap.json` ([ADR-0065](../90-decisions/ADR-0065-a-compute-services-input-document-travels-in-its-artifact.md)), so a changed document changes the artifact's bytes and its content-addressed `artifactPath`, which replaces the deployment. A platform variable the document names is still a `:updatedAt` member. -The framework's core constructs these edges when lowering a connection (the -`serialize` env-var records thread into `deploy` through the service SPI); no pack -author and no app author ever hand-wires them. +The compute descriptor constructs these edges (the `serialize` env-var records thread into `deploy` through the service SPI, and the producers' outputs come from the lowering context); no app author ever hand-wires them. ## Related diff --git a/docs/design/05-prisma-cloud/pdp-data-model.md b/docs/design/05-prisma-cloud/pdp-data-model.md index b324f2487..ff701ebe5 100644 --- a/docs/design/05-prisma-cloud/pdp-data-model.md +++ b/docs/design/05-prisma-cloud/pdp-data-model.md @@ -85,7 +85,7 @@ This is the timing model Prisma Composer's graph must respect: | any time | ConfigVariable rows are created/updated/deleted via the environment-variables API — **rows only; no effect on any existing version** | `routes/v1/environment-variables.ts` | | **version create** | `materializeBranchEnvVars(app.branchId)` resolves the complete map — production branch → production templates; preview branch → preview templates merged with that branch's overrides (override wins) — decrypts it, and hands the **plain map to Foundry with the version**. The env is **frozen into the version** | `routes/v1/versions.ts` → `resolveDeployEnvVars.ts` → `compute-config/materializeBranchEnvVars.ts` | | version start | boots the version; **no env re-resolution** | `routes/v1/version-handlers.ts` | -| promote | endpoint routed to the version; `serviceEndpointDomain` persisted. The domain returned at *create* time is a placeholder region — only the post-promote read is trustworthy (PRO-200) | `interactors/compute/service.ts` | +| promote | endpoint routed to the version; `serviceEndpointDomain` persisted. The domain returned at *create* names the domain the service will serve on | `interactors/compute/service.ts` | Consequences Prisma Composer designs around: diff --git a/docs/design/10-domains/core-model.md b/docs/design/10-domains/core-model.md index e101fe6da..9927d8456 100644 --- a/docs/design/10-domains/core-model.md +++ b/docs/design/10-domains/core-model.md @@ -670,7 +670,7 @@ type Outputs = Readonly> // `url` is present only when the descriptor declares the address publicly // reachable — a connection string is never a `url` (no core-level rule is safe: // `url` on compute is an endpoint, on postgres it would be a DSN). A descriptor -// constructing one holds `deployment.appEndpointDomain` — an Output, not a T, +// constructing one holds the App's `appEndpointDomain` — an Output, not a T, // because the stack effect runs before Alchemy applies — so construction sites // traffic in `Input` (LoweredResult.entities above); apply // resolves them before any reader sees them. @@ -766,13 +766,7 @@ module-provisioned **resource** lowers exactly once via its extension's nodes are edges only and never lower. Then for each service: 1. `provision` — the service now has identity (its App). -2. core **builds the typed `Config`** — each input's declared params matched by - name to its producer's `outputs` through the one `dependency` edge: - whatever the module wired in — a resource's outputs (shared by every consumer - wired to it) or a producer service's deploy outputs (the producer, earlier in - topo order, is already fully deployed — its URL is real, not the create-time - placeholder) — plus service-param defaults. Leaf values are provisioning refs, - not strings. +2. core **builds the typed `Config`** — each input's declared params matched by name to its producer's `outputs` through the one `dependency` edge: whatever the module wired in — a resource's outputs (shared by every consumer wired to it) or a producer service's deploy outputs — plus service-param defaults. Leaf values are provisioning refs, not strings. Core lowers a producer before its consumers, so a consumer's hooks receive the producer's outputs; how a target makes the consumer's deployment wait for its producers is the target's ([Prisma Cloud](../05-prisma-cloud/alchemy-lowering.md#the-lowering-graphs)). 3. `serialize(config)` — the pack encodes that typed Config into the service's runtime environment (Prisma Cloud: one env write per leaf, keyed by the pack's own naming from the address, value = the ref; the input document @@ -784,15 +778,7 @@ nodes are edges only and never lower. Then for each service: 5. `deploy(artifact)` — the first version snapshots an environment that is already complete. -**How the ordering is actually enforced:** our walk only *assembles* Alchemy -resource descriptions — Alchemy executes them in dependency order and runs -unordered resources concurrently; declaration order is never consulted. So core -realizes the sequence as **dependency edges**: most arise naturally from value flow -(the env var consumes the project id and the producer's URL), and the one that -doesn't — deploy-after-serialize — exists because the service descriptor builds -the `Deployment`'s props out of the environment records it boots with, which is -PDP's own dataflow restored (the deployment-create call literally contains the -materialized env map). See the lowering graphs in +**How the ordering is actually enforced:** our walk only *assembles* Alchemy resource descriptions — Alchemy executes them in dependency order and runs unordered resources concurrently; declaration order is never consulted. So core realizes the sequence as **dependency edges**: most arise naturally from value flow (the env var consumes the project id and the producer's URL), and two do not. Deploy-after-serialize exists because the service descriptor builds the `Deployment`'s props out of the environment records it boots with, which is PDP's own dataflow restored (the deployment-create call literally contains the materialized env map). Deploy-after-producers exists because the service descriptor also builds them out of its producers' outputs, so a consumer's deployment is created after its producers go live. See the lowering graphs in [`../05-prisma-cloud/alchemy-lowering.md`](../05-prisma-cloud/alchemy-lowering.md). This is what makes the fresh-deploy config race (PRO-211) structurally impossible on every target — the edge's **ordering** job. Its second job, **propagating** @@ -1098,7 +1084,7 @@ export const prismaCloud = (opts: PrismaCloudOptions = {}): ExtensionDescriptor const svc = yield* Prisma.App(address, { project: projectIdOf(application), displayName: address, regionId: o.region ?? "us-east-1", logicalId: address, }) - return { serviceId: svc.appId, projectId: projectIdOf(application) } // : ComputeProvisioned + return { serviceId: svc.appId, projectId: projectIdOf(application), endpointDomain: svc.appEndpointDomain } // : ComputeProvisioned }), // Encode the typed Config into the runtime environment — one env var per @@ -1133,22 +1119,30 @@ export const prismaCloud = (opts: PrismaCloudOptions = {}): ExtensionDescriptor Effect.try(() => Prisma.packageComputeArtifact({ id, bundleDir: assembled.dir, appEntry: assembled.entry, address, inputDocument: serialized.input && JSON.parse(serialized.input.value) })), // create → upload → start → promote. The app id is read through - // serialize's env-var records, so the deployment depends on them — the - // ordering edge that kills PRO-211. Returns a LoweredResult: `url` IS + // serialize's env-var records and every output of its producers, so the + // deployment is created after its rows are written (PRO-211) and after + // its producers are promoted. Returns a LoweredResult: `url` IS // published here — a Compute service's deployed URL is a public endpoint, - // and this descriptor is the only party that knows it. Both fields are - // still Output refs until apply. - deploy: ({ address }, provisioned, artifact, serialized) => + // and this descriptor is the only party that knows it. It is the App's + // domain, not the deployment's: the deployment is promoted on every + // deploy, so its domain is unknown while planning, and every service + // reading the URL would get a new deployment. Both fields are still + // Output refs until apply. + deploy: ({ id, address, graph, lowered }, provisioned, artifact, serialized) => Effect.gen(function* () { + const producerOutputs = graph.edges + .filter((edge) => edge.kind === "dependency" && edge.to === id) + .flatMap((edge) => Object.values(lowered.get(edge.from) ?? {})) + .filter(Output.isOutput) const deployment = yield* Prisma.Deployment(`${address}.deployment`, { - app: appAfterEnvironment(provisioned.serviceId, serialized.environment), + app: appAfter(provisioned.serviceId, serialized.environment, producerOutputs), artifactPath: artifact.path, artifactContentType: "application/gzip", portMapping: { http: serialized.port }, start: true, promote: true, }) return { - outputs: { url: deployment.appEndpointDomain, projectId: provisioned.projectId }, - entities: [{ kind: "compute-service", id: provisioned.serviceId, url: deployment.appEndpointDomain }], + outputs: { url: provisioned.endpointDomain, projectId: provisioned.projectId, serviceId: deployment.appId }, + entities: [{ kind: "compute-service", id: provisioned.serviceId, url: provisioned.endpointDomain }], } }), }, diff --git a/docs/design/90-decisions/ADR-0048-prisma-cloud-resources-come-from-the-upstream-alchemy-provider.md b/docs/design/90-decisions/ADR-0048-prisma-cloud-resources-come-from-the-upstream-alchemy-provider.md index 0af87767b..4ba87166f 100644 --- a/docs/design/90-decisions/ADR-0048-prisma-cloud-resources-come-from-the-upstream-alchemy-provider.md +++ b/docs/design/90-decisions/ADR-0048-prisma-cloud-resources-come-from-the-upstream-alchemy-provider.md @@ -4,6 +4,7 @@ Amended by [ADR-0057](ADR-0057-a-nodes-address-is-its-full-path-from-the-app-nam Amended 2026-10-30: from Alchemy 2.0.0-beta.84 the provider is its own package, `@alchemy.run/prisma`, versioned in lockstep with `alchemy`; before that it was the `alchemy/Prisma` module of the `alchemy` package. Only `@prisma/composer-prisma-cloud` and the Prisma Cloud internals it bundles depend on it; `@prisma/composer` and `@prisma/composer-cli` depend on `alchemy` alone. The move kept every resource type id (`Prisma.*`), so deploy state carries over unchanged. The decision below is unchanged. +Amended 2026-11-20: a compute service's published `url` is its App's `appEndpointDomain`, not its Deployment's: the Deployment is updated on every deploy, so its domain is unknown while planning, and every consumer's deployment, whose `triggers` hold the url, would be replaced on every deploy. A consumer's deployment waits for its producers' promotion through the deployment's `app` prop, which also carries every output of the nodes it depends on (a compute producer's `serviceId` resolves only after its promotion), never through `triggers`, so an unchanged consumer is reused. ## Decision @@ -31,7 +32,7 @@ and a lowered compute service is upstream resources wired by Composer's descript const app = Prisma.App(`${id}-svc`, { project, regionId, branchId }); const vars = records.map((r) => Prisma.EnvironmentVariable(...)); const deployment = Prisma.Deployment(`${id}-deploy`, { - app: dependsOnEnvironment(app, vars), // see "The ordering edge" below + app: appAfter(app, vars, producerOutputs), // see "The ordering edge" below artifactPath, // Composer's own tar.gz — never built by Alchemy start: true, promote: true, diff --git a/docs/design/90-decisions/README.md b/docs/design/90-decisions/README.md index 68b7fbef4..a24715f93 100644 --- a/docs/design/90-decisions/README.md +++ b/docs/design/90-decisions/README.md @@ -69,7 +69,7 @@ _Earlier drafts (ADR-0001, ADR-0002) were retired as the high-level design settl - [ADR-0045](ADR-0045-deploy-state-lives-behind-the-platform-state-api.md) — Deploy state lives behind the platform state API (the Management API implements Alchemy's stock `HttpStateApi` wire contract per Branch; composer's state layer is Alchemy's stock HTTP client), and deploys hold a server-side per-`(stack, stage)` lease (TTL 60s, heartbeated, released on exit; contention fails fast naming the holder; state operations without a live lease fail 409). Supersedes ADR-0010 (lock → lease) and the storage half of ADR-0034 (Branch scoping and lifetime stand; the visible per-stage database is gone); closes ADR-0012 as obsolete. No migration: legacy stages are refused until destroyed or deleted. - [ADR-0046](ADR-0046-the-orm-facade-is-a-peer-dependency.md) — `@prisma/composer-prisma-cloud` takes the Prisma ORM postgres facade (`@prisma/orm-postgres`) as a **peer** dependency at one exact version, not a regular dependency: Composer registers an extension pack against the application's copy of the target, and two copies of a shell in one tree means two codec/operation registries and two class identities — a value from one is rejected by the other, silently. As a peer, that combination fails at install instead. Every `@prisma/orm-*` spec in the workspace is one exact version and all name the same one (`scripts/lint-orm-pins.mjs`). `@prisma/orm-toolchain`, which Composer drives rather than extends, stays a regular dependency. Replaces ADR-0022's consequence bullet on how the ORM is installed. - [ADR-0047](ADR-0047-compute-assembly-preserves-safe-runtime-topology.md) — Compute assembly traces runtime files from the author-declared Node entry without rebundling app code, preserves only symlinks whose resolved targets remain inside the staged bundle, and installs narrowly runtime-gated bootstrap compatibility when a framework needs Node semantics. Each local mechanism is removed once the upstream Alchemy Compute provider owns the equivalent guarantee. Supersedes ADR-0005's blanket ban on symlinks and its assumption that directory output is already self-contained. -- [ADR-0048](ADR-0048-prisma-cloud-resources-come-from-the-upstream-alchemy-provider.md) — The six Management-API resource families (project, database, connection, app, deployment, environment variable) are the upstream Prisma provider's classes (`@alchemy.run/prisma`), registered in Composer's `PrismaComposer` collection; Composer defines resources only where the upstream provider has no support yet (buckets) or no Management API exists behind them. Compute binds the low-level App/Deployment/EnvironmentVariable trio (the `COMPOSER_*_ORIGIN` self-edge and ADR-0005 rule out composite `Compute`); the env→deployment ordering edge rides the deployment's `app` prop, and a deployment is replaced exactly when its artifact or environment fingerprint changes — unchanged services are reused. Legacy rows in the platform state API (retired type-ids and shapes) migrate on read — the older retired SQL state stores stay destroy-only under ADR-0045 — branch-stage databases take generated physical names, and the platform's `DATABASE_URL`, which the platform writes only for a project's default database, is left to the platform: Composer creates no default database and neither writes nor tracks the variable. *(Amended by ADR-0057: resource IDs are the node's full address, or the address plus `.` and a role, each declaring its previous ID with `renamedFrom`. Amended 2026-10-30: the provider is the `@alchemy.run/prisma` package from Alchemy beta.84 (formerly the `alchemy/Prisma` module); resource type ids are unchanged.)* +- [ADR-0048](ADR-0048-prisma-cloud-resources-come-from-the-upstream-alchemy-provider.md) — The six Management-API resource families (project, database, connection, app, deployment, environment variable) are the upstream Prisma provider's classes (`@alchemy.run/prisma`), registered in Composer's `PrismaComposer` collection; Composer defines resources only where the upstream provider has no support yet (buckets) or no Management API exists behind them. Compute binds the low-level App/Deployment/EnvironmentVariable trio (the `COMPOSER_*_ORIGIN` self-edge and ADR-0005 rule out composite `Compute`); the env→deployment ordering edge rides the deployment's `app` prop, and a deployment is replaced exactly when its artifact or environment fingerprint changes — unchanged services are reused. Legacy rows in the platform state API (retired type-ids and shapes) migrate on read — the older retired SQL state stores stay destroy-only under ADR-0045 — branch-stage databases take generated physical names, and the platform's `DATABASE_URL`, which the platform writes only for a project's default database, is left to the platform: Composer creates no default database and neither writes nor tracks the variable. *(Amended by ADR-0057: resource IDs are the node's full address, or the address plus `.` and a role, each declaring its previous ID with `renamedFrom`. Amended 2026-10-30: the provider is the `@alchemy.run/prisma` package from Alchemy beta.84 (formerly the `alchemy/Prisma` module); resource type ids are unchanged. Amended 2026-11-20: a compute service's published `url` is its App's endpoint domain, and a consumer's deployment waits for its producers' promotion through the deployment's `app` prop, never through `triggers`.)* - [ADR-0049](ADR-0049-composers-configuration-is-the-composer-section-of-prisma-config.md) — Composer's configuration is the `composer` section of `prisma.config.ts`, loaded by the Prisma CLI engine: `extensions`, `state`, and optional `entry`, `config` and `secrets` adapter lists. The validator checks only what identifies each value, passes the file's own objects through, never calls an adapter, rejects unknown keys, takes the nearest declaring file's section whole, and returns `{ value, file }`; the programmatic operations take the same input. The old config file is refused, not migrated. Only the first process runs adapters; the second process imports the file for its descriptors and reads what Load read from the app configuration file. *(Amended by ADR-0066: the child-process reason for one declaring file now holds for `destroy` and `prisma dev`; the decision stands.)* *(Amended by ADR-0067: the dev worker thread imports the config; no dev stack file.)* *(Amended by ADR-0068: `destroy` has no second process; no command hands the config to a second process.)* - [ADR-0050](ADR-0050-composer-runs-as-prisma-deploy-and-prisma-dev.md) — Composer has no binary of its own: the `prisma` CLI mounts exactly two Composer commands, `prisma deploy` and `prisma dev`, mounted at the root as bare verbs on purpose rather than under a noun. `destroy` and `log` are operations on `@prisma/composer/control`, called from a script; a teardown script reads `PRISMA_SERVICE_TOKEN` and `PRISMA_WORKSPACE_ID`. Where teardown and logs belong in the `prisma` command tree is not decided; until it is, agents and users need a script for both. A `destroy` command was rejected: the CLI's destructive verb is `delete`, and no Composer command takes `--production`. Retires the `prisma-composer` binary; a CI lint keeps its name out of user-facing files. *(Amended by ADR-0053: the family also ships `dev url`, `dev stop` and `emulator list`, and `prisma dev` gains `--detach`. Amended 2026-10-23: neither command takes a positional entry; both take `--entry` over the config's `entry`. Amended 2026-10-27: the family also ships `deploy list`.)* *(Amended by ADR-0062: the flag is `--environment`, defaulting per command; `destroy` targets an environment by name.)* - [ADR-0051](ADR-0051-a-nodes-address-is-its-logical-id-on-the-platform.md) — A node's address is its identity on Prisma Cloud: Composer writes it, byte for byte, as the `logicalId` of the node's one platform row (Project, App, Database, Bucket) and as the node's `logicalId` in the application topology, so the platform joins topology nodes to rows, and one node across branches, by string equality. A provision ID defaults to the node's name, so changing a name changes identity. The value is never an Alchemy resource ID, a display name or a platform `id`; App, Database and Bucket rows get it as a prop on the upstream resource that creates them, and the Project from container resolution; no separate resource writes it. Changing an address changes the entity's identity; changing a display name changes nothing. *(Amended 2026-10-16: on the app's own Project and Branch, the row holding a node's `logicalId` is that node's row, so a deploy resuming an interrupted create uses it. Superseded by ADR-0057: addresses start with the app name, and resource IDs, env keys and `serviceOrigin()` are built from them; the identity rules here are carried forward.)* diff --git a/packages/1-prisma-cloud/0-lowering/lowering/src/compute/__tests__/deployment-edge.test.ts b/packages/1-prisma-cloud/0-lowering/lowering/src/compute/__tests__/deployment-edge.test.ts index f8ef9854f..c5dfe6b57 100644 --- a/packages/1-prisma-cloud/0-lowering/lowering/src/compute/__tests__/deployment-edge.test.ts +++ b/packages/1-prisma-cloud/0-lowering/lowering/src/compute/__tests__/deployment-edge.test.ts @@ -21,7 +21,7 @@ import { sha256, sha256Object } from 'alchemy/Util/sha256'; import * as Effect from 'effect/Effect'; import * as Layer from 'effect/Layer'; import * as Redacted from 'effect/Redacted'; -import { appAfterEnvironment } from '../deployment-edge.ts'; +import { appAfter } from '../deployment-edge.ts'; /** A stack the resource constructors register into; nothing ever applies it. */ const stack = { name: 'shop', stage: 'prod', resources: {}, bindings: {}, actions: {} }; @@ -66,7 +66,7 @@ afterAll(() => { /** The deploy hook's props, built by the same helper the descriptor uses. */ const deploymentProps = (triggers?: Record) => ({ - app: appAfterEnvironment(app.appId, environment), + app: appAfter(app.appId, environment, []), artifactPath, artifactContentType: 'application/gzip', portMapping: { http: 8080 }, @@ -158,7 +158,7 @@ const diffAgainst = async ( ); }; -describe('appAfterEnvironment — the edge Alchemy actually plans on', () => { +describe('appAfter — the edge Alchemy actually plans on', () => { test('every variable AND the app are upstream of the deployment', () => { const upstream = Output.upstreamAny(deploymentProps()); expect(Object.keys(upstream).sort()).toEqual( @@ -168,7 +168,7 @@ describe('appAfterEnvironment — the edge Alchemy actually plans on', () => { test('the combined expression still resolves to the app id — not a variable id or tuple', () => { const resolved = Effect.runSync( - Output.evaluate(appAfterEnvironment(app.appId, environment), { + Output.evaluate(appAfter(app.appId, environment, []), { 'auth-svc': { appId: 'app-123' }, 'COMPOSER_AUTH_PORT-var': { environmentVariableId: 'var-1' }, 'COMPOSER_AUTH_DB_URL-var': { environmentVariableId: 'var-2' }, @@ -178,11 +178,33 @@ describe('appAfterEnvironment — the edge Alchemy actually plans on', () => { }); test('a service with no variables passes the app id straight through', () => { - expect(Object.keys(Output.upstreamAny({ app: appAfterEnvironment(app.appId, []) }))).toEqual([ + expect(Object.keys(Output.upstreamAny({ app: appAfter(app.appId, [], []) }))).toEqual([ 'auth-svc', ]); }); + test("a producer's outputs are upstream of the deployment, and it still resolves to the app id", () => { + const producer = registered( + Prisma.App('producer-svc', { + project: 'proj-1', + displayName: 'producer', + regionId: 'us-east-1', + }), + ); + const edge = appAfter(app.appId, [], [producer.appId]); + + expect(Object.keys(Output.upstreamAny({ app: edge })).sort()).toEqual( + ['auth-svc', 'producer-svc'].sort(), + ); + const resolved = Effect.runSync( + Output.evaluate(edge, { + 'auth-svc': { appId: 'app-123' }, + 'producer-svc': { appId: 'app-456' }, + }) as Effect.Effect, + ); + expect(resolved).toBe('app-123'); + }); + test('the artifact props are plain values, never expressions', () => { const props = deploymentProps(); expect(Output.isOutput(props.artifactPath)).toBe(false); diff --git a/packages/1-prisma-cloud/0-lowering/lowering/src/compute/__tests__/deployment-environment-lifecycle.test.ts b/packages/1-prisma-cloud/0-lowering/lowering/src/compute/__tests__/deployment-environment-lifecycle.test.ts index 8cb310ea9..a824fe4b9 100644 --- a/packages/1-prisma-cloud/0-lowering/lowering/src/compute/__tests__/deployment-environment-lifecycle.test.ts +++ b/packages/1-prisma-cloud/0-lowering/lowering/src/compute/__tests__/deployment-environment-lifecycle.test.ts @@ -8,9 +8,9 @@ import * as Exit from 'effect/Exit'; import * as Fiber from 'effect/Fiber'; import * as Layer from 'effect/Layer'; import * as Redacted from 'effect/Redacted'; -import { data, fakeManagementApi, page } from '../../__tests__/fake-management-api.ts'; +import { data, fakeManagementApi, page } from '../../fake-management-api.ts'; import { runScratch, scratchStack } from '../../scratch-stack.ts'; -import { appAfterEnvironment } from '../deployment-edge.ts'; +import { appAfter } from '../deployment-edge.ts'; function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null; @@ -212,7 +212,7 @@ test.each(scenarios)( ); } const deployment = yield* Prisma.Deployment('deployment', { - app: appAfterEnvironment(Output.asOutput('app-1'), environment), + app: appAfter(Output.asOutput('app-1'), environment, []), skipCodeUpload: true, start: false, // Environment values alone must replace the deployment. The drift case diff --git a/packages/1-prisma-cloud/0-lowering/lowering/src/compute/deployment-edge.ts b/packages/1-prisma-cloud/0-lowering/lowering/src/compute/deployment-edge.ts index 563ed6940..9f8b08011 100644 --- a/packages/1-prisma-cloud/0-lowering/lowering/src/compute/deployment-edge.ts +++ b/packages/1-prisma-cloud/0-lowering/lowering/src/compute/deployment-edge.ts @@ -1,32 +1,24 @@ /** - * Orders a deployment AFTER the environment rows it boots with: the platform - * materializes rows into a deployment at create time and never re-reads them - * (PRO-211). Alchemy schedules only on resource references inside prop - * values, and upstream's `Prisma.Deployment` has no environment prop, so the - * edge rides `app`: whole variable resources preserve their update edges; - * stable variable IDs alone are folded away during planning. The platform - * still receives the app id. `app` is the ONLY safe prop — upstream's diff - * treats `{portMapping, skipCodeUpload, artifactPath, artifactContentType}` - * as one block and returns "no opinion" if any is unresolved (a brand-new - * variable always is), which would silently skip the artifact comparison. - * Ordering only: shipping a CHANGED value is `Deployment.triggers`' job (the - * compute descriptor declares one member per environment row). + * A deployment's `app` prop, ordered after its environment rows and after the + * producer outputs the caller passes. The edges ride `app` because upstream + * treats an unresolved `app` as unchanged, while an unresolved artifact prop + * would skip the artifact comparison. */ import type { EnvironmentVariable } from '@alchemy.run/prisma'; import * as Output from 'alchemy/Output'; -export const appAfterEnvironment = ( +export const appAfter = ( app: Output.Output, environment: readonly EnvironmentVariable[], -): Output.Output => - environment.length === 0 + producerOutputs: readonly Output.Output[], +): Output.Output => { + const waits = [...environment.map((variable) => Output.of(variable)), ...producerOutputs]; + return waits.length === 0 ? app : // The app id is inside the combined expression as well as returned from // it: Alchemy's dependency walker looks only at what an expression is // built FROM, never inside the function, so an app referenced only by // the closure would leave the deployment with no edge to its own app. - Output.flatMap( - Output.all(app, ...environment.map((variable) => Output.of(variable))), - () => app, - ); + Output.flatMap(Output.all(app, ...waits), () => app); +}; diff --git a/packages/1-prisma-cloud/0-lowering/lowering/src/exports/scratch-stack.ts b/packages/1-prisma-cloud/0-lowering/lowering/src/exports/scratch-stack.ts index 69c23bb1d..3a327a985 100644 --- a/packages/1-prisma-cloud/0-lowering/lowering/src/exports/scratch-stack.ts +++ b/packages/1-prisma-cloud/0-lowering/lowering/src/exports/scratch-stack.ts @@ -1 +1,2 @@ +export { data, fakeManagementApi, page } from '../fake-management-api.ts'; export { runScratch, type ScratchStackOptions, scratchStack } from '../scratch-stack.ts'; diff --git a/packages/1-prisma-cloud/0-lowering/lowering/src/__tests__/fake-management-api.ts b/packages/1-prisma-cloud/0-lowering/lowering/src/fake-management-api.ts similarity index 100% rename from packages/1-prisma-cloud/0-lowering/lowering/src/__tests__/fake-management-api.ts rename to packages/1-prisma-cloud/0-lowering/lowering/src/fake-management-api.ts diff --git a/packages/1-prisma-cloud/0-lowering/lowering/src/state/__tests__/legacy-resources.test.ts b/packages/1-prisma-cloud/0-lowering/lowering/src/state/__tests__/legacy-resources.test.ts index 14d064529..b24bdd549 100644 --- a/packages/1-prisma-cloud/0-lowering/lowering/src/state/__tests__/legacy-resources.test.ts +++ b/packages/1-prisma-cloud/0-lowering/lowering/src/state/__tests__/legacy-resources.test.ts @@ -32,7 +32,7 @@ import * as ConfigProvider from 'effect/ConfigProvider'; import * as Effect from 'effect/Effect'; import * as Layer from 'effect/Layer'; import * as Redacted from 'effect/Redacted'; -import { data, fakeManagementApi, page } from '../../__tests__/fake-management-api.ts'; +import { data, fakeManagementApi, page } from '../../fake-management-api.ts'; import { stateLayerAgainst } from '../layer.ts'; import { migrateLegacyResourceState } from '../legacy-resources.ts'; import { FakeStateApi } from './fake-state-api.ts'; diff --git a/packages/1-prisma-cloud/1-extensions/target/src/__tests__/compute-redeploy-apply.test.ts b/packages/1-prisma-cloud/1-extensions/target/src/__tests__/compute-redeploy-apply.test.ts new file mode 100644 index 000000000..369863e75 --- /dev/null +++ b/packages/1-prisma-cloud/1-extensions/target/src/__tests__/compute-redeploy-apply.test.ts @@ -0,0 +1,342 @@ +// A producer and a consumer that reads its url, deployed twice under the real Alchemy apply engine and the real `Prisma.App`, `Prisma.Deployment` and `Prisma.EnvironmentVariable` providers, against a fake Management API. +import { expect, test } from 'bun:test'; +import * as path from 'node:path'; +import * as Prisma from '@alchemy.run/prisma'; +import { dependency, string } from '@internal/core'; +import type { LowerContext } from '@internal/core/deploy'; +import { + data, + fakeManagementApi, + page, + runScratch, + scratchStack, +} from '@internal/lowering/scratch-stack'; +import * as Provider from 'alchemy/Provider'; +import * as Deferred from 'effect/Deferred'; +import * as Duration from 'effect/Duration'; +import * as Effect from 'effect/Effect'; +import * as Layer from 'effect/Layer'; +import { compute } from '../compute.ts'; +import { computeDescriptor } from '../descriptors/compute.ts'; + +const PROJECT_ID = 'proj_1'; +const BRANCH_ID = 'br_default'; +const CREATED_AT = '2026-11-20T00:00:00.000Z'; + +interface FakePlatform { + readonly services: Map>; + readonly deployments: Map>; + // A deleted deployment stays listed. + readonly created: unknown[]; + readonly variables: Map>; + readonly values: Map; + readonly events: string[]; + // While set, the producer's deployment create waits until the consumer's arrives, so a consumer that does not wait for its producer is created first. + authCreateWaitsForWeb: Deferred.Deferred | undefined; +} + +function record(body: unknown): Record { + return typeof body === 'object' && body !== null ? { ...body } : {}; +} + +function respond(platform: FakePlatform, method: string, url: URL, body: unknown): Response { + const path = url.pathname; + const sent = record(body); + + if (method === 'PUT' && path.startsWith('/upload/')) return new Response(null); + + if (method === 'GET' && path === `/v1/projects/${PROJECT_ID}/branches`) { + return page([ + { + id: BRANCH_ID, + type: 'branch', + url: `https://api.prisma.test/v1/branches/${BRANCH_ID}`, + gitName: 'main', + isDefault: true, + role: 'production', + createdAt: CREATED_AT, + updatedAt: CREATED_AT, + project: { + id: PROJECT_ID, + url: `https://api.prisma.test/v1/projects/${PROJECT_ID}`, + name: 'shop', + }, + }, + ]); + } + if (method === 'GET' && path === '/v1/services') { + const logicalId = url.searchParams.get('logicalId'); + return page( + [...platform.services.values()].filter( + (service) => logicalId === null || service['logicalId'] === logicalId, + ), + ); + } + if (method === 'POST' && path === '/v1/services') { + const id = `svc_${platform.services.size + 1}`; + const service = { + id, + type: 'app', + url: `https://api.prisma.test/v1/services/${id}`, + name: sent['displayName'], + region: { id: 'eu-west-3', name: 'Paris' }, + projectId: sent['projectId'], + branchId: sent['branchId'] ?? BRANCH_ID, + latestDeploymentId: null, + appEndpointDomain: `https://${id}.cdg.prisma.build`, + createdAt: CREATED_AT, + logicalId: sent['logicalId'] ?? null, + }; + platform.services.set(id, service); + return data(service); + } + const serviceMatch = path.match(/^\/v1\/services\/([^/]+)(\/[a-z]+)?$/); + const service = serviceMatch === null ? undefined : platform.services.get(serviceMatch[1] ?? ''); + if (serviceMatch !== null && service !== undefined) { + const action = serviceMatch[2]; + if (method === 'GET' && action === undefined) return data(service); + if (method === 'GET' && action === '/deployments') { + return page( + [...platform.deployments.values()].filter((d) => d['serviceId'] === service['id']), + ); + } + if (method === 'POST' && action === '/deployments') { + platform.created.push(service['id']); + platform.events.push(`create:${service['logicalId']}`); + const id = `dep_${platform.created.length}`; + const deployment = { + id, + type: 'deployment', + url: `https://api.prisma.test/v1/deployments/${id}`, + serviceId: service['id'], + foundryVersionId: `ver_${platform.created.length}`, + status: 'new', + previewDomain: null, + createdAt: CREATED_AT, + }; + platform.deployments.set(id, deployment); + return data({ ...deployment, uploadUrl: `https://api.prisma.test/upload/${id}` }); + } + if (method === 'POST' && action === '/promote') { + service['latestDeploymentId'] = sent['deploymentId']; + platform.events.push(`promote:${service['logicalId']}`); + return data({ appEndpointDomain: service['appEndpointDomain'], reassignedDomains: 0 }); + } + } + const deploymentMatch = path.match(/^\/v1\/deployments\/([^/]+)(\/start|\/stop)?$/); + const deployment = + deploymentMatch === null ? undefined : platform.deployments.get(deploymentMatch[1] ?? ''); + if (deployment !== undefined) { + if (method === 'GET' && deploymentMatch?.[2] === undefined) return data(deployment); + if (method === 'POST' && deploymentMatch?.[2] === '/start') { + deployment['status'] = 'running'; + deployment['previewDomain'] = `https://${deployment['id']}.preview.prisma.build`; + return data({ previewDomain: deployment['previewDomain'] }); + } + if (method === 'POST' && deploymentMatch?.[2] === '/stop') { + deployment['status'] = 'stopped'; + return data(deployment); + } + if (method === 'DELETE') { + platform.deployments.delete(String(deployment['id'])); + return data(deployment); + } + } + + if (method === 'GET' && path === '/v1/environment-variables') { + const key = url.searchParams.get('key'); + return page([...platform.variables.values()].filter((v) => key === null || v['key'] === key)); + } + if (method === 'POST' && path === '/v1/environment-variables') { + const id = `var_${platform.variables.size + 1}`; + const variable = { + id, + type: 'environment-variable', + url: `https://api.prisma.test/v1/environment-variables/${id}`, + projectId: sent['projectId'], + branchId: null, + class: sent['class'], + key: sent['key'], + valueKid: 'kid_1', + isManagedBySystem: false, + createdAt: CREATED_AT, + updatedAt: CREATED_AT, + }; + platform.variables.set(id, variable); + platform.values.set(String(sent['key']), sent['value']); + return data(variable); + } + const variable = platform.variables.get( + path.match(/^\/v1\/environment-variables\/([^/]+)$/)?.[1] ?? '', + ); + if (variable !== undefined && method === 'GET') return data(variable); + if (variable !== undefined && method === 'PATCH') { + platform.values.set(String(variable['key']), sent['value']); + return data(variable); + } + + return Response.json( + { error: { code: 'unhandled', message: `${method} ${path}` } }, + { status: 400 }, + ); +} + +function fakePlatform() { + const platform: FakePlatform = { + services: new Map(), + deployments: new Map(), + created: [], + variables: new Map(), + values: new Map(), + events: [], + authCreateWaitsForWeb: undefined, + }; + const api = fakeManagementApi(({ method, path, body }) => + Effect.gen(function* () { + const url = new URL(path, 'https://api.prisma.test'); + const creating = + method === 'POST' ? url.pathname.match(/^\/v1\/services\/([^/]+)\/deployments$/) : null; + const address = platform.services.get(creating?.[1] ?? '')?.['logicalId']; + const waitForWeb = platform.authCreateWaitsForWeb; + if (waitForWeb !== undefined && address === 'shop.web') { + yield* Deferred.succeed(waitForWeb, undefined); + } + if (waitForWeb !== undefined && address === 'shop.auth') { + yield* Deferred.await(waitForWeb).pipe(Effect.timeoutOption(Duration.millis(500))); + } + return respond(platform, method, url, body); + }), + ); + const providers = Layer.effect( + Prisma.Providers, + Provider.collection([Prisma.App, Prisma.Deployment, Prisma.EnvironmentVariable]), + ).pipe( + Layer.provideMerge( + Layer.mergeAll( + Prisma.AppProvider(), + Prisma.DeploymentProvider(), + Prisma.EnvironmentVariableProvider(), + ), + ), + Layer.provideMerge(api), + ); + const createdFor = (address: string) => { + const service = [...platform.services.values()].find((s) => s['logicalId'] === address); + return platform.created.filter((serviceId) => serviceId === service?.['id']); + }; + return { + platform, + createdFor, + stack: scratchStack({ providers, dev: false }, 'compute-redeploy'), + }; +} + +const build = { + extension: '@prisma/composer/node', + type: 'node', + module: 'file:///test/service.ts', + entry: 'server.js', +}; +const auth = compute({ name: 'auth', deps: {}, build }); +const web = compute({ + name: 'web', + deps: { + auth: dependency({ + type: 'rpc', + connection: { params: { url: string() }, hydrate: (v) => v }, + }), + }, + build, +}); + +const descriptor = computeDescriptor(() => ({ workspaceId: 'ws_1', providerParams: new Map() })); +const graph = { + root: { id: 'shop' }, + inputBindings: [], + edges: [{ from: 'shop.auth', to: 'shop.web', input: 'auth', kind: 'dependency' }], +}; +const application = { + projectId: PROJECT_ID, + branchId: undefined, + defaultBranchId: BRANCH_ID, + branchless: false, +}; +// The fake platform never unpacks an artifact, so any file serves as one. +const artifact = { path: import.meta.path, sha256: 'sha-artifact' }; +const changedArtifact = { path: path.join(import.meta.dir, '..', 'compute.ts'), sha256: 'sha-2' }; + +function program(authArtifact = artifact, webArtifact = artifact) { + return Effect.gen(function* () { + const lowered = new Map>>(); + const ctxOf = (address: string, node: unknown) => + ({ id: address, address, node, graph, application, lowered }) as unknown as LowerContext; + const authCtx = ctxOf('shop.auth', auth); + const webCtx = ctxOf('shop.web', web); + const authProvisioned = yield* descriptor.provision(authCtx); + const webProvisioned = yield* descriptor.provision(webCtx); + const authSerialized = yield* descriptor.serialize(authCtx, authProvisioned, { + service: { port: 3000 }, + inputs: {}, + }); + const deployed = yield* descriptor.deploy( + authCtx, + authProvisioned, + authArtifact, + authSerialized, + ); + lowered.set('shop.auth', deployed.outputs); + const webSerialized = yield* descriptor.serialize(webCtx, webProvisioned, { + service: { port: 3000 }, + inputs: { auth: { url: deployed.outputs['url'] } }, + }); + yield* descriptor.deploy(webCtx, webProvisioned, webArtifact, webSerialized); + }) as Effect.Effect; +} + +test("an unchanged deploy keeps the deployment of a consumer of the producer's url", async () => { + const { platform, createdFor, stack } = fakePlatform(); + + await runScratch(stack.deploy(program())); + expect(createdFor('shop.auth')).toHaveLength(1); + expect(createdFor('shop.web')).toHaveLength(1); + expect(platform.values.get('COMPOSER_SHOP_WEB_AUTH_URL')).toBe('https://svc_1.cdg.prisma.build'); + + await runScratch(stack.deploy(program())); + expect(createdFor('shop.auth')).toHaveLength(1); + expect(createdFor('shop.web')).toHaveLength(1); +}); + +test("a new artifact for the producer replaces its deployment and keeps the consumer's", async () => { + const { createdFor, stack } = fakePlatform(); + + await runScratch(stack.deploy(program())); + await runScratch(stack.deploy(program(changedArtifact))); + + expect(createdFor('shop.auth')).toHaveLength(2); + expect(createdFor('shop.web')).toHaveLength(1); +}); + +test("on a first deploy, the producer is promoted before the consumer's deployment is created", async () => { + const { platform, stack } = fakePlatform(); + platform.authCreateWaitsForWeb = Deferred.makeUnsafe(); + + await runScratch(stack.deploy(program())); + + expect( + platform.events.filter((event) => event === 'promote:shop.auth' || event === 'create:shop.web'), + ).toEqual(['promote:shop.auth', 'create:shop.web']); +}); + +test("when both artifacts change, the producer is promoted before the consumer's new deployment is created", async () => { + const { platform, createdFor, stack } = fakePlatform(); + await runScratch(stack.deploy(program())); + platform.events.length = 0; + platform.authCreateWaitsForWeb = Deferred.makeUnsafe(); + + await runScratch(stack.deploy(program(changedArtifact, changedArtifact))); + + expect(createdFor('shop.web')).toHaveLength(2); + expect( + platform.events.filter((event) => event === 'promote:shop.auth' || event === 'create:shop.web'), + ).toEqual(['promote:shop.auth', 'create:shop.web']); +}); diff --git a/packages/1-prisma-cloud/1-extensions/target/src/__tests__/control-lowering.test.ts b/packages/1-prisma-cloud/1-extensions/target/src/__tests__/control-lowering.test.ts index 7a595433e..c4ccb57b1 100644 --- a/packages/1-prisma-cloud/1-extensions/target/src/__tests__/control-lowering.test.ts +++ b/packages/1-prisma-cloud/1-extensions/target/src/__tests__/control-lowering.test.ts @@ -1692,10 +1692,15 @@ describe("prismaCloud().nodes['compute'] — the service descriptor", () => { const ctx = { id: 'shop.auth', address: 'shop.auth', - graph: { root: { id: 'shop' } }, + graph: { root: { id: 'shop' }, edges: [] }, + lowered: new Map(), opts: {}, } as unknown as LowerContext; - const provisioned = { serviceId: 'auth-svc#cloud-id', projectId: 'shop-project#cloud-id' }; + const provisioned = { + serviceId: 'auth-svc#cloud-id', + projectId: 'shop-project#cloud-id', + endpointDomain: 'https://auth-svc.example', + }; const artifact = { path: '/tmp/auth.tar.gz', sha256: 'sha-auth' }; const serialized = { environment: [ @@ -1740,7 +1745,7 @@ describe("prismaCloud().nodes['compute'] — the service descriptor", () => { ], ]); expect(result.outputs).toEqual({ - url: 'https://shop.auth.deployment.example', + url: 'https://auth-svc.example', projectId: 'shop-project#cloud-id', serviceId: 'deployment-app-of:shop.auth.deployment', }); @@ -1750,7 +1755,7 @@ describe("prismaCloud().nodes['compute'] — the service descriptor", () => { { kind: 'compute-service', id: 'auth-svc#cloud-id', - url: 'https://shop.auth.deployment.example', + url: 'https://auth-svc.example', }, ]); }); @@ -1760,10 +1765,15 @@ describe("prismaCloud().nodes['compute'] — the service descriptor", () => { const ctx = { id: 'shop.auth', address: 'shop.auth', - graph: { root: { id: 'shop' } }, + graph: { root: { id: 'shop' }, edges: [] }, + lowered: new Map(), opts: {}, } as unknown as LowerContext; - const provisioned = { serviceId: 'auth-svc#cloud-id', projectId: 'shop-project#cloud-id' }; + const provisioned = { + serviceId: 'auth-svc#cloud-id', + projectId: 'shop-project#cloud-id', + endpointDomain: 'https://auth-svc.example', + }; const artifact = { path: '/tmp/auth.tar.gz', sha256: 'sha-auth' }; const serialized = { environment: [], @@ -1785,7 +1795,7 @@ describe("prismaCloud().nodes['compute'] — the service descriptor", () => { { kind: 'compute-service', id: 'auth-svc#cloud-id', - url: 'https://shop.auth.deployment.example', + url: 'https://auth-svc.example', details: { input: '{"stripeKey":$platform STRIPE_SECRET_KEY}', absent: 'greeting → ENV_GREETING\nregion → ENV_REGION', @@ -1799,10 +1809,15 @@ describe("prismaCloud().nodes['compute'] — the service descriptor", () => { const ctx = { id: 'shop.mailer', address: 'shop.mailer', - graph: { root: { id: 'shop' } }, + graph: { root: { id: 'shop' }, edges: [] }, + lowered: new Map(), opts: {}, } as unknown as LowerContext; - const provisioned = { serviceId: 'mailer-svc#cloud-id', projectId: 'shop-project#cloud-id' }; + const provisioned = { + serviceId: 'mailer-svc#cloud-id', + projectId: 'shop-project#cloud-id', + endpointDomain: 'https://mailer-svc.example', + }; const artifact = { path: '/tmp/mailer.tar.gz', sha256: 'sha-mailer' }; const serialized = { environment: [], @@ -1827,7 +1842,7 @@ describe("prismaCloud().nodes['compute'] — the service descriptor", () => { { kind: 'compute-service', id: 'mailer-svc#cloud-id', - url: 'https://shop.mailer.deployment.example', + url: 'https://mailer-svc.example', details: { input: '{"port":$platform SMTP_PORT (number),"from":$platform EMAIL_FROM}' }, }, ]); @@ -1838,10 +1853,15 @@ describe("prismaCloud().nodes['compute'] — the service descriptor", () => { const ctx = { id: 'shop.auth', address: 'shop.auth', - graph: { root: { id: 'shop' } }, + graph: { root: { id: 'shop' }, edges: [] }, + lowered: new Map(), opts: {}, } as unknown as LowerContext; - const provisioned = { serviceId: 'auth-svc#cloud-id', projectId: 'shop-project#cloud-id' }; + const provisioned = { + serviceId: 'auth-svc#cloud-id', + projectId: 'shop-project#cloud-id', + endpointDomain: 'https://auth-svc.example', + }; const artifact = { path: '/tmp/auth.tar.gz', sha256: 'sha-auth' }; const serialized = { environment: [], @@ -1863,7 +1883,7 @@ describe("prismaCloud().nodes['compute'] — the service descriptor", () => { { kind: 'compute-service', id: 'auth-svc#cloud-id', - url: 'https://shop.auth.deployment.example', + url: 'https://auth-svc.example', details: { input: '{"stripeKey":$platform STRIPE_SECRET_KEY}', }, @@ -1985,10 +2005,15 @@ describe("prismaCloud().nodes['s3-store'] — the service descriptor with extend const ctx = { id: 'shop.store', address: 'shop.store', - graph: { root: { id: 'shop' } }, + graph: { root: { id: 'shop' }, edges: [] }, + lowered: new Map(), opts: {}, } as unknown as LowerContext; - const provisioned = { serviceId: 'store-svc#cloud-id', projectId: 'shop-project#cloud-id' }; + const provisioned = { + serviceId: 'store-svc#cloud-id', + projectId: 'shop-project#cloud-id', + endpointDomain: 'https://store-svc.example', + }; const artifact = { path: '/tmp/store.tar.gz', sha256: 'sha-store' }; const serialized = { environment: [{ id: 'STORE_PORT-var#cloud-id', key: 'STORE_PORT' }], @@ -2006,7 +2031,7 @@ describe("prismaCloud().nodes['s3-store'] — the service descriptor with extend // The four S3Config fields a consumer's s3() slot resolves by name, plus url. expect(result.outputs).toEqual({ - url: 'https://shop.store.deployment.example', + url: 'https://store-svc.example', projectId: 'shop-project#cloud-id', serviceId: 'deployment-app-of:shop.store.deployment', bucket: 'streams', @@ -2021,7 +2046,7 @@ describe("prismaCloud().nodes['s3-store'] — the service descriptor with extend { kind: 'compute-service', id: 'store-svc#cloud-id', - url: 'https://shop.store.deployment.example', + url: 'https://store-svc.example', }, ]); }); @@ -3129,18 +3154,28 @@ describe('resource IDs are built from the full address, and each declares the ID recorded.renamePolicy.filter(([r]) => r === resource).at(-1)?.[1]; const idsSince = (rows: ReadonlyArray<[string, unknown]>, before: number) => rows.slice(before).map(([id]) => id); - const shopGraph = { root: { id: 'shop' } }; + const shopGraph = { root: { id: 'shop' }, edges: [] }; test('compute: the App and Deployment of a nested service', () => { const target = prismaCloud({ workspaceId: 'ws_1' }); - const ctx = { id: 'shop.auth.api', address: 'shop.auth.api', graph: shopGraph, application }; + const ctx = { + id: 'shop.auth.api', + address: 'shop.auth.api', + graph: shopGraph, + application, + lowered: new Map(), + }; const before = { svc: recorded.svc.length, deploy: recorded.deploy.length }; run(serviceDescriptorOf(target, 'compute').provision(ctx as unknown as LowerContext)); run( serviceDescriptorOf(target, 'compute').deploy( ctx as unknown as LowerContext, - { serviceId: 'shop.auth.api#cloud-id', projectId: 'shop-project#cloud-id' }, + { + serviceId: 'shop.auth.api#cloud-id', + projectId: 'shop-project#cloud-id', + endpointDomain: 'https://shop.auth.api.example', + }, { path: '/tmp/api.tar.gz', sha256: 'sha-api' }, { environment: [], triggers: {}, platformVariables: [], port: 3000 }, ), @@ -3982,6 +4017,16 @@ describe("a compute service's input document travels in its artifact", () => { ]); }); + test("reserve publishes the App's endpoint domain as the service's url", async () => { + const { reserve } = serviceDescriptorOf(prismaCloud({ workspaceId: 'ws_1' }), 'compute'); + if (reserve === undefined) throw new Error('compute declares reserve'); + + const result = run(reserve(contextFor('Shop'), provisioned, serialize('Shop'))); + + expect(result.outputs['url']).toBe('https://shop.web.example'); + expect(result.entities[0]).toHaveProperty('url', 'https://shop.web.example'); + }); + test('reserve writes the input document as a preview variable on the branch of an environment that has one, as serialize does for its variables', async () => { const { reserve } = serviceDescriptorOf(prismaCloud({ workspaceId: 'ws_1' }), 'compute'); if (reserve === undefined) throw new Error('compute declares reserve'); diff --git a/packages/1-prisma-cloud/1-extensions/target/src/control/extension.ts b/packages/1-prisma-cloud/1-extensions/target/src/control/extension.ts index 6b8851ff6..36699d6d4 100644 --- a/packages/1-prisma-cloud/1-extensions/target/src/control/extension.ts +++ b/packages/1-prisma-cloud/1-extensions/target/src/control/extension.ts @@ -24,7 +24,7 @@ import { prismaCloudContainerOf, } from '../container.ts'; import { bucketDescriptor } from '../descriptors/bucket.ts'; -import { computeDescriptor } from '../descriptors/compute.ts'; +import { computeDescriptor, endpointDomainOf } from '../descriptors/compute.ts'; import { postgresDescriptor } from '../descriptors/orm-postgres.ts'; import { platformVariableDescriptor } from '../descriptors/platform-variable.ts'; import { publicUrlDescriptor } from '../descriptors/public-url.ts'; @@ -153,26 +153,6 @@ const streamsApiKeyValue: ProviderParam['value'] = (refs) => { }); }; -/** - * Origin's deploy-side value function (service-derived): the provisioned - * service's own `endpointDomain`, verbatim — `https://…`, no trailing slash, - * no normalization. Every compute service gets this row, exposing or not. - * The undefined guard is a deploy-time invariant check, not a policy: the - * Management API always reports an endpoint domain post-PRO-200, so a missing - * one means the platform predates the fix — fail the deploy loudly rather - * than write a row origin() would trust. The raw string is JSON-encoded by - * the descriptor's generic loop, like every other reserved provider param. - */ -const selfOriginValue: ServiceProviderParam['valueForService'] = (provisioned, address) => - Output.map(provisioned.endpointDomain, (v) => { - if (v === undefined) { - throw new Error( - `the App for "${address}" reported no endpoint domain at provision — cannot resolve the service's own origin (Management API predates the PRO-200 fix?)`, - ); - } - return v; - }); - /** The user-facing state descriptor: `state: prismaState()` in the `composer` section of `prisma.config.ts` (ADR-0017). */ export const prismaState = (): StateDescriptor => ({ extension: PRISMA_CLOUD_EXTENSION_ID, @@ -249,7 +229,8 @@ const PROVIDER_PARAM_VALUES: ReadonlyMap = new Map< >([ [RPC_PEER_KEY, { value: rpcAcceptedKeysValue }], [STREAMS_API_KEY, { value: streamsApiKeyValue }], - [SELF_ORIGIN, { valueForService: selfOriginValue }], + // The service's own App domain, verbatim, written for every compute service whether it exposes anything or not. + [SELF_ORIGIN, { valueForService: endpointDomainOf }], ]); /** diff --git a/packages/1-prisma-cloud/1-extensions/target/src/descriptors/compute.ts b/packages/1-prisma-cloud/1-extensions/target/src/descriptors/compute.ts index 4c086e563..81fee7110 100644 --- a/packages/1-prisma-cloud/1-extensions/target/src/descriptors/compute.ts +++ b/packages/1-prisma-cloud/1-extensions/target/src/descriptors/compute.ts @@ -3,11 +3,7 @@ import * as Prisma from '@alchemy.run/prisma'; import type { ServiceLowering } from '@internal/core/deploy'; import { describeDocument, type PlatformMarkerUse } from '@internal/input-document'; -import { - ARTIFACT_CONTENT_TYPE, - appAfterEnvironment, - packageComputeArtifact, -} from '@internal/lowering'; +import { ARTIFACT_CONTENT_TYPE, appAfter, packageComputeArtifact } from '@internal/lowering'; import * as Output from 'alchemy/Output'; import * as Effect from 'effect/Effect'; import * as Redacted from 'effect/Redacted'; @@ -32,6 +28,7 @@ import { logicalIdProp, projectIdOf, type ResolvedCloudOptions, + type ServiceProvisionedAttributes, validatePlatformName, variableClassAndBranch, } from './shared.ts'; @@ -53,10 +50,7 @@ function declaresNoListening(node: unknown): boolean { export interface ComputeProvisioned { readonly serviceId: Output.Output; readonly projectId: string; - /** The platform-assigned public origin domain — resolves to `undefined` only - * in the narrow provider-response gap the origin param's deploy-side value - * function enforces away (control.ts's `selfOriginValue`): every other - * reader may treat it as present. */ + /** The platform-assigned public origin domain. Read it through `endpointDomainOf`, which fails the deploy when the platform reported none. */ readonly endpointDomain: Output.Output; } @@ -111,13 +105,25 @@ function originOf( `address — known services: ${known}.`, ); } - return Output.map(provisioned.endpointDomain, (v) => { - if (v === undefined) { + return endpointDomainOf( + provisioned, + target, + `cannot resolve serviceOrigin("${target}") for "${consumer}"`, + ); +} + +export function endpointDomainOf( + provisioned: ServiceProvisionedAttributes, + address: string, + consequence?: string, +): Output.Output { + return Output.map(provisioned.endpointDomain, (domain) => { + if (domain === undefined) { throw new Error( - `the App for "${target}" reported no endpoint domain — cannot resolve serviceOrigin("${target}") for "${consumer}".`, + `the App for "${address}" reported no endpoint domain${consequence === undefined ? '' : `: ${consequence}`}.`, ); } - return v; + return domain; }); } @@ -352,8 +358,13 @@ export function computeDescriptor( }), ), - deploy: ({ address, graph, preflight }, provisioned, artifact, serialized) => + deploy: ({ id, address, graph, lowered, preflight }, provisioned, artifact, serialized) => Effect.gen(function* () { + // Only to order this deployment after its producers are live; their values reach it through the rows and triggers. + const producerOutputs = graph.edges + .filter((edge) => edge.kind === 'dependency' && edge.to === id) + .flatMap((edge) => Object.values(lowered.get(edge.from) ?? {})) + .filter(Output.isOutput); const { platformVariableVersion } = preflightView(preflight); // Env keys never contain ':', so a `:updatedAt` member cannot // collide with a row's own trigger member. The key name is historical: @@ -370,10 +381,9 @@ export function computeDescriptor( }; const app = graph.root.id; const deployment = yield* Prisma.Deployment(resourceId(address, 'deployment'), { - // `app` carries the ordering edge on serialize's variable writes as - // well as the app id — see `appAfterEnvironment` for why it is the - // only prop that can (PRO-211). - app: appAfterEnvironment(provisioned.serviceId, serialized.environment), + // `app` carries the ordering edges as well as the app id — see + // `appAfter` for why it is the only prop that can. + app: appAfter(provisioned.serviceId, serialized.environment, producerOutputs), artifactPath: artifact.path, // The artifact IS a gzipped tar (see @internal/lowering's packager); // upstream sends this as the upload's Content-Type and folds it into @@ -394,22 +404,20 @@ export function computeDescriptor( start: true, promote: true, }).pipe(fromLegacyId(legacyNodeResourceId(app, address, 'deploy'))); - // `url` IS published here: a Compute service's deployed URL is a - // public endpoint, and this descriptor is the only party that knows - // that. Both fields are still unresolved Output references at this - // point — apply resolves them before the report's runner sees them. - // + const url = endpointDomainOf(provisioned, address); return { outputs: { - url: deployment.appEndpointDomain, + // The App's domain, not the deployment's: the deployment's is unknown while planning, so every consumer of this url would get a new deployment. + url, projectId: provisioned.projectId, + // From the deployment, so it resolves once this service is live: a custom domain binds to the live version, and its consumers' deployments wait for it. serviceId: deployment.appId, }, entities: [ { kind: 'compute-service', id: provisioned.serviceId, - url: deployment.appEndpointDomain, + url, ...inputDetails(serialized), }, ], @@ -418,10 +426,8 @@ export function computeDescriptor( // ADR-0056: a service that runs its build adapter's dev server under // `prisma dev` has no artifact. What `deploy` returns, with the - // `Deployment` removed: the URL comes from the App, which locally is the - // same `http://localhost:` the Deployment would report, so - // dependents' rows do not change between a served and an assembled run. - // With no artifact to carry it, the input document is a variable. + // `Deployment` removed. With no artifact to carry it, the input document + // is a variable. reserve: ({ address, application, graph }, provisioned, serialized) => Effect.gen(function* () { const { input } = serialized; @@ -433,14 +439,7 @@ export function computeDescriptor( ...variableClassAndBranch(application), }).pipe(asOwnRow, fromLegacyId(legacyEnvVarId(legacyInputKey(graph.root.id, address)))); } - const url = Output.map(provisioned.endpointDomain, (domain) => { - if (domain === undefined) { - throw new Error( - 'the App reported no endpoint domain for a service without a deployment.', - ); - } - return domain; - }); + const url = endpointDomainOf(provisioned, address); return { outputs: { url, projectId: provisioned.projectId, serviceId: provisioned.serviceId }, entities: [ diff --git a/packages/1-prisma-cloud/1-extensions/target/src/origin-key.ts b/packages/1-prisma-cloud/1-extensions/target/src/origin-key.ts index 8a35f737b..4c4d32e55 100644 --- a/packages/1-prisma-cloud/1-extensions/target/src/origin-key.ts +++ b/packages/1-prisma-cloud/1-extensions/target/src/origin-key.ts @@ -2,7 +2,7 @@ * The service's own origin as a reserved provider param (ADR-0031): the ONE * brand and the ONE entry — shared by control.ts (which registers the * deploy-side value function that resolves the provisioned service's - * `endpointDomain` — see its `selfOriginValue`) and compute.ts (which + * `endpointDomain` through `endpointDomainOf`) and compute.ts (which * validates and stashes the row at boot through the generic * `stashProviderParams` loop), so writer and reader cannot drift. *