project-shell.ts
This file
Path
apps/platform/src/project-shell.ts
172 lines · 5.0 KiB · TypeScript
Last changed by
Merge observability into mainoscabrielbefore adoption1985111
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–7why, one step back1import { EvidenceStore } from "@repo/checks/evidence-store";2import { projectService } from "@repo/project/project";3import type { ProjectApi, ProjectPorts } from "@repo/project/project";4import { ProjectStore } from "@repo/project/project-store";5import { ProjectStoreSqlite } from "@repo/project/project-store/sqlite";6import type { ProjectName } from "@repo/protocol/ids";7import { UnknownProject } from "@repo/protocol/refusal";Merge observability into mainoscabrielbefore adoption1985111
line 8why, one step back8import { withInvocation } from "@repo/telemetry/spans";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 9–11why, one step back9import {10 Context,11 Effect,Merge observability into mainoscabrielbefore adoption1985111
line 12why, one step back12 ErrorReporter,Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 13–142why, one step back13 Exit,14 Layer,15 Option,16 Ref,17 Schema,18 SynchronizedRef,19} from "effect";20import type { Scope } from "effect";21import type { SqlClient } from "effect/sql/SqlClient";2223import { CallRequest, ProjectCalls } from "./project-calls.ts";24import type { ArgsOf, ExitEncoded, ProjectMethod } from "./project-calls.ts";2526export interface ProjectShell {27 readonly call: (28 request: typeof CallRequest.Encoded29 ) => Effect.Effect<ExitEncoded>;30 readonly alarm: () => Effect.Effect<void>;31}3233type Methods = {34 readonly [M in ProjectMethod]: (35 ...args: Readonly<ArgsOf<M>>36 ) => Effect.Effect<37 Effect.Success<ReturnType<ProjectApi[M]>>,38 Effect.Error<ReturnType<ProjectApi[M]>>,39 EvidenceStore40 >;41};4243const nameless = Effect.die(44 "the Project DO has no name: the Worker reaches it by `idFromName`"45);4647export const projectShell = (input: {48 readonly name: ProjectName | undefined;49 readonly sql: Layer.Layer<SqlClient>;50 readonly ports: Layer.Layer<51 Exclude<ProjectPorts, ProjectStore> | EvidenceStore52 >;53}): Effect.Effect<ProjectShell, never, Scope.Scope> =>54 Effect.gen(function* () {55 const context = yield* Layer.build(56 Layer.merge(57 // Fresh: one store per shell, never one an enclosing layer made.58 Layer.fresh(ProjectStoreSqlite.pipe(Layer.provide(input.sql))),59 input.ports60 )61 );6263 const store = Context.get(context, ProjectStore);6465 const withEvidence = Effect.provideService(66 EvidenceStore,67 Context.get(context, EvidenceStore)68 );6970 const made = yield* Ref.make(false);71 const built = yield* SynchronizedRef.make(Option.none<Methods>());7273 const isMade = Effect.gen(function* () {74 if (yield* Ref.get(made)) {75 return true;76 }7778 const { project } = yield* store.load();7980 if (Option.isSome(project)) {81 yield* Ref.set(made, true);82 }8384 return Option.isSome(project);85 });8687 const nameOf = Effect.gen(function* () {88 if (input.name !== undefined) {89 return Option.some(input.name);90 }9192 const { project } = yield* store.load();9394 return Option.map(project, (row) => row.project);95 });9697 const service = SynchronizedRef.modifyEffect(built, (current) =>98 Option.match(current, {99 onNone: () =>100 Effect.gen(function* () {101 const name = yield* nameOf;102103 if (Option.isNone(name)) {104 return yield* nameless;105 }106107 const api: Methods = yield* projectService(name.value).pipe(108 Effect.provideContext(context)109 );110111 return [api, Option.some(api)] as const;112 }),113 onSome: (api) => Effect.succeed([api, current] as const),114 })115 );116117 const serve = <M extends ProjectMethod>(118 method: M,119 request: typeof CallRequest.Encoded & { readonly method: M }120 ) =>121 Effect.gen(function* () {122 const call = ProjectCalls[method];123124 const args = yield* Schema.decodeUnknownEffect(call.payload)(125 call.caller ? [request.caller, ...request.payload] : request.payload126 ).pipe(Effect.orDie);127128 if (call.refusesUnmade && !(yield* isMade)) {129 const name = yield* nameOf;130131 if (Option.isNone(name)) {132 return yield* nameless;133 }134135 return yield* Schema.encodeEffect(call.exit)(136 Exit.fail(new UnknownProject({ project: name.value }))137 ).pipe(Effect.orDie);138 }139140 const api = yield* service;141 const outcome = yield* Effect.exit(withEvidence(api[method](...args)));142Merge observability into mainoscabrielbefore adoption1985111
lines 143–148why, one step back143 // The reply carries a defect without its stack, and the caller reports it no144 // more: it is reported here, where the stack is.145 if (Exit.isFailure(outcome)) {146 yield* ErrorReporter.report(outcome.cause);147 }148Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 149–151why, one step back149 return yield* Schema.encodeUnknownEffect(call.exit)(outcome).pipe(150 Effect.orDie151 );Merge observability into mainoscabrielbefore adoption1985111
line 152why, one step back152 }).pipe(Effect.withSpan(`Project.${method}`));Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 153why, one step back153Merge observability into mainoscabrielbefore adoption1985111
lines 154–157why, one step back154 const named = withInvocation(155 input.name === undefined ? {} : { project: input.name }156 );157Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 158why, one step back158 return {Merge observability into mainoscabrielbefore adoption1985111
lines 159–165why, one step back159 alarm: Effect.fn("Project.alarm")(function* () {160 if (yield* isMade) {161 yield* Effect.flatMap(service, (api) =>162 withEvidence(api.onAlarm())163 ).pipe(named);164 }165 }),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 166–168why, one step back166 call: (request) =>167 Effect.flatMap(168 Schema.decodeEffect(CallRequest)(request).pipe(Effect.orDie),Merge observability into mainoscabrielbefore adoption1985111
line 169why, one step back169 ({ method }) => named(serve(method, request))Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 170–172why, one step back170 ),171 };172 });