Skip to content
Myr

handlers.ts

This file

Path

apps/web/src/dev/handlers.ts

373 lines · 12.7 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

line 1why, one step back
1import { RepoHost } from "@repo/artifacts/repo-host";

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 2why, one step back
2import { EvidenceStore, LogRef } from "@repo/checks/evidence-store";

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

lines 3–13why, one step back
3import {
4 codeProjectOf,5 commitOf,6 fileOf,7 logOf,8 pathsOf,9 refsOf,10 treeOf,11 whyOf,12} from "@repo/project/code";13import type { CodeProject } from "@repo/project/code";

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 14why, one step back
14import { landedProjectOf, withDescriptions } from "@repo/project/description";

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 15–17why, one step back
15import { FrontPage } from "@repo/project/front-page";
16import type { ProjectApi } from "@repo/project/project";17import { Projects } from "@repo/project/projects";

Merge build/1102 into mainoscabrielbefore adoption084383d

line 18why, one step back
18import { withStandingPosture } from "@repo/project/standing-posture";

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 19–32why, one step back
19import { Contracts, EnvSchemaOf } from "@repo/protocol/contracts";
20import type { AnswerView } from "@repo/protocol/domain/answer";21import type { ClaimView } from "@repo/protocol/domain/claim";22import type { Decision, Today } from "@repo/protocol/domain/decision";23import { sameHandle } from "@repo/protocol/ids";24import type { ProjectName, RepoName } from "@repo/protocol/ids";25import { CurrentPrincipal, Principal } from "@repo/protocol/principal";26import type { Person } from "@repo/protocol/principal";27import { NotDecider } from "@repo/protocol/refusal";28import { Array, DateTime, Effect, Number, Option, Order, Stream } from "effect";29import type { Layer } from "effect";30import { HttpServerRequest } from "effect/http";31import type { Rpc, RpcGroup } from "effect/rpc";32

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

lines 33–34why, one step back
33const LOG_PAGE = 30;
34

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 35–38why, one step back
35const EVENT_PAGE = 100;
3637const caller = Effect.service(CurrentPrincipal);38

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

line 39why, one step back
39// `Authenticated` lets an anonymous call reach only a `public` contract, so anyone else here is a defect.

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 40–120why, one step back
40const person = Effect.flatMap(caller, (principal) =>
41 Principal.match(principal, {42 Anonymous: () =>43 Effect.die("a contract for signed-in people was called anonymously"),44 Person: (named) => Effect.succeed<Person>(named),45 })46);4748const gateway = Effect.map(49 Effect.serviceOption(HttpServerRequest.HttpServerRequest),50 (request) =>51 Option.getOrElse(52 Option.flatMap(request, (incoming) =>53 Option.map(HttpServerRequest.toURL(incoming), ({ origin }) => origin)54 ),55 () => "http://localhost"56 )57);5859const remoteOf = (repo: RepoName | ProjectName) =>60 Effect.map(gateway, (origin) => `${origin}/${repo}.git`);6162const atGateway = (claim: ClaimView) =>63 Effect.map(remoteOf(claim.fork), (remote): ClaimView => ({64 ...claim,65 remote,66 }));6768const statusAtGateway = (69 who: Person,70 status: {71 readonly answers: readonly AnswerView[];72 readonly claims: readonly ClaimView[];73 }74) =>75 Effect.map(Effect.forEach(status.claims, atGateway), (claims) => ({76 answers: status.answers,77 claims,78 handle: who.handle,79 }));8081const byExpiry: Order.Order<Decision> = Order.combineAll([82 Order.mapInput(DateTime.Order, ({ expiresAt }: Decision) => expiresAt),83 Order.mapInput(DateTime.Order, ({ openedAt }: Decision) => openedAt),84 Order.mapInput(Order.String, ({ project }: Decision) => project),85 Order.mapInput(Order.String, ({ id }: Decision) => id),86]);8788const acrossProjects = (89 decider: Person,90 pages: readonly Today[]91): Effect.Effect<Today, NotDecider> => {92 const sumOf = (pick: (page: Today) => number) =>93 Number.sumAll(pages.map(pick));9495 return Array.isReadonlyArrayNonEmpty(pages)96 ? Effect.succeed({97 budget: {98 finishes: sumOf(({ budget }) => budget.finishes),99 total: sumOf(({ budget }) => budget.total),100 used: sumOf(({ budget }) => budget.used),101 },102 decider: decider.handle,103 decisions: Array.sort(104 pages.flatMap(({ decisions }) => decisions),105 byExpiry106 ),107 expiringSoon: pages.flatMap(({ expiringSoon }) => expiringSoon),108 newcomers: Array.dedupeWith(109 pages.flatMap(({ newcomers }) => newcomers),110 sameHandle111 ),112 overflow: sumOf(({ overflow }) => overflow),113 unattended: pages.flatMap(({ unattended }) => unattended),114 })115 : Effect.fail(new NotDecider({}));116};117118export const devHandlers: Layer.Layer<119 Rpc.ToHandler<RpcGroup.Rpcs<typeof Contracts>>,120 never,

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

line 121why, one step back
121 Projects | FrontPage | EvidenceStore | RepoHost

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 122–126why, one step back
122> = Contracts.toLayer(
123 Effect.gen(function* () {124 const projects = yield* Projects;125 const frontPage = yield* FrontPage;126 const evidence = yield* EvidenceStore;

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

line 127why, one step back
127 const host = yield* RepoHost;

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 128–133why, one step back
128
129 const on = <A, E>(130 project: ProjectName,131 call: (api: ProjectApi) => Effect.Effect<A, E>132 ) => Effect.flatMap(projects.get(project), call);133

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

lines 134–143why, one step back
134 const code = <A, E>(
135 project: ProjectName,136 read: (of: CodeProject) => Effect.Effect<A, E, RepoHost>137 ) =>138 on(project, (api) =>139 read(codeProjectOf(api, project)).pipe(140 Effect.provideService(RepoHost, host)141 )142 );143

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 144–178why, one step back
144 const everywhere = <A, E>(
145 call: (api: ProjectApi) => Effect.Effect<Option.Option<A>, E>146 ) =>147 Effect.gen(function* () {148 const listed = yield* frontPage.projects();149150 const each = yield* Effect.forEach(151 Array.sort(152 Array.dedupe(listed.map(({ project }) => project)),153 Order.String154 ),155 (project) =>156 Effect.flatMap(157 Effect.option(projects.get(project)),158 Option.match({ onNone: () => Effect.succeedNone, onSome: call })159 )160 );161162 return Array.getSomes(each);163 });164165 return {166 claim: ({ ask, project }) =>167 Effect.gen(function* () {168 const who = yield* person;169 const { claim } = yield* on(project, (api) => api.claim(who, ask));170171 return { claim: yield* atGateway(claim) };172 }),173 credits: ({ handle }) =>174 Effect.map(frontPage.credits(handle), (credits) => ({ credits })),175 decide: ({ project, ...input }) =>176 Effect.flatMap(person, (who) =>177 on(project, (api) => api.decide(who, input))178 ),

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

lines 179–182why, one step back
179 draftAsk: ({ project, ...input }) =>
180 Effect.flatMap(person, (who) =>181 on(project, (api) => api.draftAsk(who, input))182 ),

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 183–244why, one step back
183 evidence: ({ project, ref }) =>
184 Effect.flatMap(person, (who) =>185 on(project, (api) =>186 api187 .evidence(who, LogRef.make(ref))188 .pipe(Effect.provideService(EvidenceStore, evidence))189 )190 ),191 // Keyed by the class: lint keeps "shape" out of code names.192 [EnvSchemaOf._tag]: ({ project }) =>193 Effect.flatMap(caller, (who) =>194 on(project, (api) => api[EnvSchemaOf._tag](who))195 ),196 finish: ({ project, ...input }) =>197 Effect.flatMap(person, (who) =>198 on(project, (api) => api.finish(who, input))199 ),200 flagAsk: ({ project, ...input }) =>201 Effect.flatMap(person, (who) =>202 on(project, (api) => api.flagAsk(who, input))203 ),204 fork: ({ project }) =>205 Effect.gen(function* () {206 const who = yield* person;207 const { fork } = yield* on(project, (api) => api.fork(who));208209 return { fork, remote: yield* remoteOf(fork) };210 }),211 frontPage: (filter) =>212 Effect.map(frontPage.entries(filter), (entries) => ({ entries })),213 initProject: ({ project, ...input }) =>214 Effect.gen(function* () {215 const who = yield* person;216217 // A project an earlier call made but could not start is this218 // call's to start; one already started refuses it itself.219 const api = yield* projects220 .create(project)221 .pipe(222 Effect.catchTag("ProjectExists", () =>223 Effect.orDie(projects.get(project))224 )225 );226227 const made = yield* api.initProject(who, input);228229 return { ...made, remote: yield* remoteOf(made.project) };230 }),231 land: ({ project, ...input }) =>232 Effect.flatMap(person, (who) =>233 on(project, (api) => api.land(who, input))234 ),235 landing: ({ project, sha }) =>236 Effect.flatMap(caller, (who) =>237 on(project, (api) => api.landing(who, sha))238 ),239 ledger: ({ project }) =>240 Effect.flatMap(caller, (who) => on(project, (api) => api.ledger(who))),241 listAsks: ({ project }) =>242 Effect.flatMap(caller, (who) =>243 on(project, (api) => api.listAsks(who))244 ),

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

lines 245–246why, one step back
245 listPaths: ({ at, project }) =>
246 code(project, (of) => pathsOf(of, Option.fromUndefinedOr(at))),

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 247why, one step back
247 listProjects: () =>

Merge build/1102 into mainoscabrielbefore adoption084383d

lines 248–250why, one step back
248 frontPage.projects().pipe(
249 Effect.flatMap(withStandingPosture),250 Effect.provideService(Projects, projects),

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 251–258why, one step back
251 Effect.flatMap((listed) =>
252 withDescriptions(listed, (project) =>253 Effect.map(projects.get(project), (api) =>254 landedProjectOf(api, project)255 )256 )257 ),258 Effect.provideService(RepoHost, host),

Merge build/1102 into mainoscabrielbefore adoption084383d

lines 259–260why, one step back
259 Effect.map((listed) => ({ projects: listed }))
260 ),

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

lines 261–270why, one step back
261 listRefs: ({ project }) => code(project, refsOf),
262 log: ({ at, from = 0, limit = LOG_PAGE, path, project }) =>263 code(project, (of) =>264 logOf(of, {265 at: Option.fromUndefinedOr(at),266 from,267 limit,268 path: Option.fromUndefinedOr(path),269 })270 ),

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 271–274why, one step back
271 openAsk: ({ project, ...input }) =>
272 Effect.flatMap(person, (who) =>273 on(project, (api) => api.openAsk(who, input))274 ),

Name the ask an opening opened, say why a policy has not evaluated, and give code history a durable storeoscabrielbefore adoption51f046c

line 275why, one step back
275 projectPolicy: ({ project }) => on(project, (api) => api.policy()),

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 276–279why, one step back
276 proposeAsk: ({ project, ...input }) =>
277 Effect.flatMap(person, (who) =>278 on(project, (api) => api.proposeAsk(who, input))279 ),

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

lines 280–285why, one step back
280 readFile: ({ at, path, project }) =>
281 code(project, (of) => fileOf(of, Option.fromUndefinedOr(at), path)),282 readTree: ({ at, path, project }) =>283 code(project, (of) =>284 treeOf(of, Option.fromUndefinedOr(at), Option.fromUndefinedOr(path))285 ),

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 286–288why, one step back
286 // Read as nobody: the list ignores the caller, so every caller's read is the same.
287 recentLandings: () =>288 Effect.map(frontPage.recentLandings(), (landings) => ({ landings })),

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 289–300why, one step back
289 release: ({ ask, project }) =>
290 Effect.flatMap(person, (who) =>291 on(project, (api) => api.release(who, ask))292 ),293 setEnv: ({ project, ...input }) =>294 Effect.flatMap(person, (who) =>295 on(project, (api) => api.setEnv(who, input))296 ),297 showAsk: ({ ask, project }) =>298 Effect.flatMap(caller, (who) =>299 on(project, (api) => api.showAsk(who, ask))300 ),

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 301–302why, one step back
301 showCommit: ({ at, project, sha }) =>
302 code(project, (of) => commitOf(of, sha, Option.fromUndefinedOr(at))),

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

lines 303–306why, one step back
303 showProposal: ({ project, proposal }) =>
304 Effect.flatMap(caller, (who) =>305 on(project, (api) => api.showProposal(who, proposal))306 ),

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 307–368why, one step back
307 status: ({ project }) =>
308 Effect.gen(function* () {309 const who = yield* person;310311 if (project !== undefined) {312 return yield* statusAtGateway(313 who,314 yield* on(project, (api) => api.status(who))315 );316 }317318 const each = yield* everywhere((api) =>319 Effect.asSome(api.status(who))320 );321322 return yield* statusAtGateway(who, {323 answers: each.flatMap(({ answers }) => answers),324 claims: each.flatMap(({ claims }) => claims),325 });326 }),327 submitAnswer: ({ project, ...input }) =>328 Effect.flatMap(person, (who) =>329 on(project, (api) => api.submitAnswer(who, input))330 ),331 today: ({ project }) =>332 Effect.gen(function* () {333 const who = yield* person;334335 if (project !== undefined) {336 return yield* on(project, (api) => api.today(who));337 }338339 const pages = yield* everywhere((api) =>340 api.today(who).pipe(341 Effect.asSome,342 Effect.catchTag("NotDecider", () => Effect.succeedNone)343 )344 );345346 return yield* acrossProjects(who, pages);347 }),348 verdict: ({ project, ...input }) =>349 Effect.flatMap(caller, (who) =>350 on(project, (api) => api.verdict(who, input))351 ),352 vouch: ({ project, ...input }) =>353 Effect.flatMap(person, (who) =>354 on(project, (api) => api.vouch(who, input))355 ),356 watch: ({ project, since = 0 }) =>357 Stream.unwrap(358 Effect.map(projects.get(project), (api) =>359 Stream.paginate(since, (after) =>360 Effect.map(api.events(after, EVENT_PAGE), (rows) => [361 rows.map(({ event }) => event),362 rows.length < EVENT_PAGE363 ? Option.none()364 : Option.fromUndefinedOr(rows.at(-1)?.seq),365 ])366 )367 )368 ),

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

lines 369–370why, one step back
369 why: ({ at, path, project }) =>
370 code(project, (of) => whyOf(of, Option.fromUndefinedOr(at), path)),

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 371–373why, one step back
371 };
372 })373);