import * as Context from "effect/Context"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; /** * Per-resource in-memory artifacts shared across a single `Plan.make -> apply` * execution. * * The engine scopes this service by resource `FQN` before invoking lifecycle * handlers, so providers should treat it as a resource-local bag for expensive, * deterministic intermediate results that can be reused across phases. * * Expected usage: * * - `diff` computes an expensive artifact once and stores it * - `create` / `update` reads the same artifact and skips recomputing it * - artifacts are ephemeral and must never be required for correctness on a * later deploy because the bag is reset between runs * * Example: * * ```ts * const artifacts = yield* Artifacts; * const cached = yield* artifacts.get("bundle"); * if (cached) return cached; * * const bundle = yield* prepareBundle(); * yield* artifacts.set("bundle", bundle); * return bundle; * ``` */ export class Artifacts extends Context.Service< Artifacts, { /** * Get an artifact by key from the current resource's bag. */ get(key: string): Effect.Effect; /** * Store an artifact by key in the current resource's bag. */ set(key: string, value: T): Effect.Effect; /** * Delete an artifact by key from the current resource's bag. */ delete(key: string): Effect.Effect; } >()("Artifacts") {} type ArtifactBag = Map; export class ArtifactStore extends Context.Service>()( "Artifacts/Store", ) {} /** * Create a fresh root store for one deploy/test run. */ export const createArtifactStore = (): ArtifactStore["Service"] => new Map(); const getOrCreateBag = (store: Map, fqn: string): ArtifactBag => { const existing = store.get(fqn); if (existing) { return existing; } const bag = new Map(); store.set(fqn, bag); return bag; }; export const makeScopedArtifacts = ( store: Map, fqn: string, ): Artifacts["Service"] => { const bag = getOrCreateBag(store, fqn); return { get: (key: string) => Effect.sync(() => bag.get(key) as T | undefined), set: (key: string, value: T) => Effect.sync(() => { bag.set(key, value); }), delete: (key: string) => Effect.sync(() => { bag.delete(key); }), }; }; export const scopedArtifacts = (fqn: string): Layer.Layer => Layer.effect( Artifacts, ArtifactStore.useSync((store) => makeScopedArtifacts(store, fqn)), ); /** * Run an effect with a fresh artifact root, replacing any existing store. * Use this at top-level entrypoints that intentionally define a new deploy run. */ export const provideFreshArtifactStore = ( effect: Effect.Effect, ): Effect.Effect => effect.pipe(Effect.provideServiceEffect(ArtifactStore, Effect.sync(createArtifactStore))); /** * Ensure an artifact root exists, reusing the ambient store when one is already * present. This lets nested helpers participate in the same run-scoped cache. */ export const ensureArtifactStore = ( effect: Effect.Effect, ): Effect.Effect => Effect.serviceOption(ArtifactStore).pipe( Effect.map(Option.getOrUndefined), Effect.flatMap((existing) => effect.pipe(Effect.provideService(ArtifactStore, existing ?? createArtifactStore())), ), ); export const cached = (id: string) => ( eff: Effect.Effect, ): Effect.Effect => Effect.gen(function* () { const artifacts = yield* Artifacts; const cached = yield* artifacts.get>(id); if (cached !== undefined) { if (Effect.isEffect(cached)) { return yield* cached; } return cached; } const deferred = yield* Deferred.make(); yield* artifacts.set(id, Deferred.await(deferred)); // Settle waiters on every exit, or a failed or interrupted run leaves // them waiting forever. Only a success is cached: a later caller // retries instead of replaying the failure. return yield* eff.pipe( Effect.onExit((exit) => (Exit.isSuccess(exit) ? artifacts.set(id, exit.value) : artifacts.delete(id)).pipe( Effect.andThen(Deferred.done(deferred, exit)), ), ), ); });