inline.ts
This file
Path
packages/drafting/src/draft-runs/inline.ts
105 lines · 3.7 KiB · TypeScript
Last changed by
Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f
History of this fileTake it
Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f
lines 1–105why, one step back1/**2 * `DraftRuns` in process, for tests and the worlds that run without a3 * Workflow engine (the web's dev world, the platform's Node world): `start`4 * runs the round's program (`runDraft`) at once, to its settling, as5 * `@repo/project`'s `AnswerRunsInline` runs an answer's steps. Each step runs6 * once, its failures retried as its retries say, without waiting; nothing7 * is recorded, since nothing replays. A round already under way is not8 * started twice, and a run that dies is logged: the proposal stays9 * drafting, and its proposer may draft it again.10 *11 * The run reaches the project through `Projects`, which is built over the12 * project services that start runs: so the runs take their ports when a13 * round starts (`draftRunsInline`), and `withDraftRunsInline` ties the knot14 * for a `Projects` layer, handing its runs the `Projects` it builds.15 */16import type { RepoHost } from "@repo/artifacts/repo-host";17import { DraftRuns } from "@repo/project/draft-runs";18import type { DraftParams } from "@repo/project/draft-runs";19import { Projects } from "@repo/project/projects";20import { Context, Deferred, Effect, Layer, Ref } from "effect";2122import type { Drafter } from "../drafter.ts";23import { runDraft } from "../run.ts";24import type { DraftSteps } from "../run.ts";2526/** What a round runs on. */27export type InlinePorts = Projects | Drafter | RepoHost;2829/** Steps run once each, in process: a failure retried as its retries say, at once. */30const inlineSteps: DraftSteps = {31 step: (_name, _schema, effect, retry) =>32 Effect.retry(effect, { times: retry?.limit ?? 0 }),33};3435/** One round's key: its project, proposal and round. */36const keyOf = ({ project, proposal, round }: DraftParams) =>37 `${project}/${proposal}/${round}`;3839/** `DraftRuns` that run each round in process, on the ports `portsOf` gives when it starts. */40export const draftRunsInline = (41 portsOf: Effect.Effect<Context.Context<InlinePorts>>42): Layer.Layer<never> =>43 Layer.effect(44 DraftRuns,45 Effect.gen(function* () {46 const running = yield* Ref.make<ReadonlySet<string>>(new Set());4748 const claimRun = (key: string) =>49 Ref.modify(running, (under) =>50 under.has(key) ? [false, under] : [true, new Set(under).add(key)]51 );5253 const endRun = (key: string) =>54 Ref.update(running, (under) => {55 const rest = new Set(under);5657 rest.delete(key);5859 return rest;60 });6162 return {63 start: (params) =>64 Effect.gen(function* () {65 const key = keyOf(params);6667 if (!(yield* claimRun(key))) {68 return;69 }7071 yield* runDraft(params, inlineSteps).pipe(72 Effect.provideContext(yield* portsOf),73 Effect.catchCause((cause) =>74 Effect.logWarning("a draft's run died", cause)75 ),76 Effect.ensuring(endRun(key))77 );78 }),79 };80 })81 );8283/**84 * `projects` with in-process draft runs: every project service it builds85 * starts its rounds through `draftRunsInline`, which reaches the projects86 * through the `Projects` this layer builds.87 */88export const withDraftRunsInline = <E, R>(89 projects: Layer.Layer<Projects, E, R>90): Layer.Layer<Projects, E, R | Drafter | RepoHost> =>91 Layer.effectContext(92 Effect.gen(function* () {93 const ready = yield* Deferred.make<Context.Context<InlinePorts>>();9495 const built = yield* Layer.build(96 projects.pipe(Layer.provide(draftRunsInline(Deferred.await(ready))))97 );9899 const around = yield* Effect.context<Drafter | RepoHost>();100101 yield* Deferred.succeed(ready, Context.merge(around, built));102103 return Context.make(Projects, Context.get(built, Projects));104 })105 );