Skip to content
Myr

custody.ts

This file

Path

apps/platform/src/adapters/custody.ts

359 lines · 11.1 KiB · TypeScript

Last changed by

Merge observability into mainoscabrielbefore adoption1985111

History of this file

Take 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–13why, one step back
1import type { RepoName } from "@repo/artifacts/repo-host";
2import { Custody } from "@repo/git/custody";3import type { Command } from "@repo/git/custody";4import type { ProjectApi } from "@repo/project/project";5import { Projects } from "@repo/project/projects";6import type { HarnessAudit } from "@repo/protocol/audit";7import { RequestId, sameHandle } from "@repo/protocol/ids";8import type { AskId, CommitSha, Handle, ProjectName } from "@repo/protocol/ids";9import { Principal } from "@repo/protocol/principal";10import type { Person } from "@repo/protocol/principal";11import { ClaimLapsed, DirectPush, NotClaimed } from "@repo/protocol/refusal";12import type { Refusal } from "@repo/protocol/refusal";13import { receiptLine } from "@repo/protocol/render";

Merge observability into mainoscabrielbefore adoption1985111

lines 14–15why, one step back
14import { annotateInvocation, refusedWith } from "@repo/telemetry/spans";
15import type { InvocationIds } 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 16–24why, one step back
16import { Array, Effect, Option, Result } from "effect";
1718import type { LayerTarget } from "../layer-gate.ts";19import { RepoKind } from "../repo-names.ts";2021const mainRef = "refs/heads/main";2223export type Notice = (line: string) => Effect.Effect<void>;24

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

line 25why, one step back
25// A request id holds 64 characters, so the head comes first and is never cut.

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 26–33why, one step back
26export const pushRequestId = (pusher: Handle, head: CommitSha) =>
27 RequestId.make(`push-${head}-${pusher.toLowerCase()}`.slice(0, 64));2829const pushEventId = (repo: RepoName, head: CommitSha) => `push-${repo}-${head}`;3031const directPushes = (commands: readonly Command[]) =>32 commands.map(({ ref }) => new DirectPush({ ref }));33

Merge build/1103 into mainoscabrielbefore adoption9c64f9b

lines 34–35why, one step back
34const branchPrefix = "refs/heads/";
35

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

line 36why, one step back
36// Every ref but a branch is the platform's (notes, merge refs) or no one's (tags).

Merge build/1103 into mainoscabrielbefore adoption9c64f9b

lines 37–41why, one step back
37const branchesOnly = (commands: readonly Command[]): readonly Refusal[] =>
38 commands.flatMap(({ ref }) =>39 ref.startsWith(branchPrefix) ? [] : [new DirectPush({ ref })]40 );41

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 42–67why, one step back
42const mainTip = (commands: readonly Command[]) =>
43 Option.flatMap(44 Array.findFirst(commands, ({ ref }) => ref === mainRef),45 (command) =>46 Option.map(command.to, (head) => ({ from: command.from, head }))47 );4849const against = (50 projects: Projects["Service"],51 project: ProjectName,52 judge: (api: ProjectApi) => Effect.Effect<readonly Refusal[]>53): Effect.Effect<readonly Refusal[]> =>54 projects.get(project).pipe(55 Effect.flatMap(judge),56 Effect.catchTag("UnknownProject", (refused) => Effect.succeed([refused]))57 );5859const custodyOf = (60 admit: (commands: readonly Command[]) => readonly Refusal[],61 review: Custody["Service"]["review"]62) =>63 Custody.of({64 admit: (_repo, commands) => Effect.succeed(admit(commands)),65 review,66 });67

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

line 68why, one step back
68// A project never made has no first pusher: custody fails closed.

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 69–164why, one step back
69const canonical = (
70 projects: Projects["Service"],71 project: ProjectName,72 pusher: Person73) =>74 Effect.gen(function* () {75 const first = yield* projects.get(project).pipe(76 Effect.flatMap((api) => api.firstPusher()),77 Effect.catchTag("UnknownProject", () => Effect.succeedNone)78 );7980 const isFirst = Option.exists(first, (initializer) =>81 sameHandle(initializer, pusher.handle)82 );8384 return custodyOf(85 (commands) =>86 commands.flatMap(({ ref, to }) =>87 isFirst && ref === mainRef && Option.isSome(to)88 ? []89 : [new DirectPush({ ref })]90 ),91 // Only a first push was admitted: it becomes the landed SHA, once.92 (_repo, commands) =>93 Option.match(mainTip(commands), {94 onNone: () => Effect.succeed(directPushes(commands)),95 onSome: ({ head }) =>96 against(projects, project, (api) =>97 api.recordFirstPush(pusher, head).pipe(98 Effect.as([]),99 Effect.catchTag("DirectPush", (refused) =>100 Effect.succeed([refused])101 )102 )103 ),104 })105 );106 });107108const claimOn = (api: ProjectApi, person: Person, ask: AskId) =>109 Effect.map(api.status(person), ({ claims }) =>110 Array.findFirst(claims, (claim) => claim.ask === ask)111 );112113const holds = (api: ProjectApi, person: Person, ask: AskId) =>114 Effect.map(claimOn(api, person, ask), (claim) =>115 Option.match(claim, {116 onNone: () => [NotClaimed.of(ask)],117 onSome: ({ state }) => {118 if (state === "live" || state === "warned") {119 return [];120 }121122 return state === "lapsed"123 ? [ClaimLapsed.of(ask)]124 : [NotClaimed.of(ask)];125 },126 })127 );128129const offer = (input: {130 readonly api: ProjectApi;131 readonly ask: AskId;132 readonly audit: Option.Option<HarnessAudit>;133 readonly from: Option.Option<CommitSha>;134 readonly head: CommitSha;135 readonly notice: Notice;136 readonly person: Person;137 readonly repo: RepoName;138}) =>139 Effect.gen(function* () {140 const { api, ask, head, person, repo } = input;141142 const refusals = yield* api.recordPush({143 after: head,144 before: input.from,145 eventId: pushEventId(repo, head),146 pusher: person,147 ref: mainRef,148 repo,149 ...Option.match(input.audit, {150 onNone: () => ({}),151 onSome: (audit) => ({ audit }),152 }),153 });154155 if (Array.isReadonlyArrayNonEmpty(refusals)) {156 return refusals;157 }158159 const claim = yield* claimOn(api, person, ask);160161 if (Option.isNone(claim)) {162 return [NotClaimed.of(ask)];163 }164

Merge observability into mainoscabrielbefore adoption1985111

lines 165–168why, one step back
165 const requestId = pushRequestId(person.handle, head);
166167 yield* annotateInvocation({ requestId });168

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 169why, one step back
169 const submitted = yield* Effect.result(

Merge observability into mainoscabrielbefore adoption1985111

line 170why, one step back
170 api.submitAnswer(person, { ask, base: claim.value.base, head, requestId })

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 171–192why, one step back
171 );
172173 if (Result.isFailure(submitted)) {174 return [submitted.failure];175 }176177 return yield* Result.match(receiptLine(submitted.success), {178 onFailure: Effect.succeed,179 onSuccess: (line) => Effect.as(input.notice(line), []),180 });181 });182183const claimFork = (184 projects: Projects["Service"],185 fork: Extract<RepoKind, { readonly _tag: "ClaimFork" }>,186 pusher: Person,187 notice: Notice,188 audit: Option.Option<HarnessAudit>189) =>190 Effect.gen(function* () {191 const isClaimant = sameHandle(pusher.handle, fork.handle);192

Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571

line 193why, one step back
193 // A maintainer finishes an answer by pushing onto its fork.

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 194–202why, one step back
194 const refused = yield* against(projects, fork.project, (api) =>
195 isClaimant196 ? holds(api, pusher, fork.ask)197 : Effect.map(Effect.isSuccess(api.today(pusher)), (decides) =>198 decides ? [] : [NotClaimed.of(fork.ask)]199 )200 );201202 return custodyOf(

Merge build/1103 into mainoscabrielbefore adoption9c64f9b

line 203why, one step back
203 (commands) => [...branchesOnly(commands), ...refused],

Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571

line 204why, one step back
204 // A maintainer's commits are judged by `<cli> finish`, not by the push.

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 205–242why, one step back
205 (repo, commands) =>
206 Option.match(207 Option.filter(mainTip(commands), () => isClaimant),208 {209 onNone: () => Effect.succeed([]),210 onSome: ({ from, head }) =>211 against(projects, fork.project, (api) =>212 offer({213 api,214 ask: fork.ask,215 audit,216 from,217 head,218 notice,219 person: pusher,220 repo,221 })222 ),223 }224 )225 );226 });227228type PushAnnotations = HarnessAudit & {229 readonly pusher: string;230 readonly target: RepoKind["_tag"];231};232233const annotationsOf = (234 pusher: Person,235 kind: RepoKind,236 audit: Option.Option<HarnessAudit>237): PushAnnotations => ({238 ...Option.getOrElse(audit, (): HarnessAudit => ({})),239 pusher: pusher.handle,240 target: kind._tag,241});242

Merge observability into mainoscabrielbefore adoption1985111

lines 243–262why, one step back
243// What Issues shows beside a push's failure: the project and ask it reached,
244// the tip it carried to main and the tags it was refused with.245const targetIds = (kind: RepoKind): InvocationIds =>246 RepoKind.$match(kind, {247 Canonical: ({ project }) => ({ project }),248 ClaimFork: ({ ask, project }) => ({ ask, project }),249 Unknown: () => ({}),250 WorkingFork: ({ project }) => ({ project }),251 });252253const judged =254 (commands: readonly Command[]) => (refusals: readonly Refusal[]) =>255 annotateInvocation({256 ...Option.match(mainTip(commands), {257 onNone: () => ({}),258 onSome: ({ head }) => ({ sha: head }),259 }),260 ...refusedWith(refusals),261 });262

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

line 263why, one step back
263// `admit` stays synchronous: the gateway's body locks the moment the fiber yields.

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 264–266why, one step back
264const annotated = (custody: Custody["Service"], annotations: PushAnnotations) =>
265 Custody.of({266 admit: (repo, commands) =>

Merge observability into mainoscabrielbefore adoption1985111

lines 267–269why, one step back
267 custody
268 .admit(repo, commands)269 .pipe(Effect.tap(judged(commands)), Effect.annotateLogs(annotations)),

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 270–273why, one step back
270 review: (repo, commands) =>
271 custody272 .review(repo, commands)273 .pipe(

Merge observability into mainoscabrielbefore adoption1985111

line 274why, one step back
274 Effect.tap(judged(commands)),

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 275–279why, one step back
275 Effect.annotateLogs(annotations),
276 Effect.withSpan("custody.review", { attributes: annotations })277 ),278 });279

Merge build/1103 into mainoscabrielbefore adoption9c64f9b

line 280why, one step back
280const ownBranches = custodyOf(branchesOnly, () => Effect.succeed([]));

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 281–285why, one step back
281
282export const refuseAll = custodyOf(directPushes, (_repo, commands) =>283 Effect.succeed(directPushes(commands))284);285

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

line 286why, one step back
286// `audit` is metadata only: who pushes is the token's person, never the username.

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 287–300why, one step back
287export const custodyFor = (
288 pusher: Principal,289 kind: RepoKind,290 notice: Notice = () => Effect.void,291 audit: Option.Option<HarnessAudit> = Option.none()292): Effect.Effect<Custody["Service"], never, Projects> =>293 Effect.gen(function* () {294 if (!Principal.guards.Person(pusher)) {295 return refuseAll;296 }297298 const projects = yield* Projects;299 const annotations = annotationsOf(pusher, kind, audit);300

Merge observability into mainoscabrielbefore adoption1985111

line 301why, one step back
301 yield* annotateInvocation(targetIds(kind));

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 302–312why, one step back
302 yield* Effect.logInfo("a push reached custody").pipe(
303 Effect.annotateLogs(annotations)304 );305306 const custody = yield* RepoKind.$match(kind, {307 Canonical: ({ project }) => canonical(projects, project, pusher),308 ClaimFork: (fork) => claimFork(projects, fork, pusher, notice, audit),309 // The platform makes no repo by any other name; nothing pushes there.310 Unknown: () => Effect.succeed(refuseAll),311 // A person's working fork: only its owner pushes; their `land` judges it.312 WorkingFork: ({ handle }) =>

Merge build/1103 into mainoscabrielbefore adoption9c64f9b

lines 313–315why, one step back
313 Effect.succeed(
314 sameHandle(pusher.handle, handle) ? ownBranches : refuseAll315 ),

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 316–320why, one step back
316 });
317318 return annotated(custody, annotations);319 });320

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

line 321why, one step back
321// The gateway lets only the layer's writers push here, so every ref goes through.

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 322–359why, one step back
322export const layerCustodyFor = (
323 pusher: Person,324 target: LayerTarget325): Effect.Effect<Custody["Service"], never, Projects> =>326 Effect.map(Projects, (projects) =>327 custodyOf(328 () => [],329 (_repo, commands) =>330 Option.match(mainTip(commands), {331 onNone: () => Effect.succeed([]),332 onSome: ({ head }) =>333 against(projects, target.project, (api) =>334 Effect.as(335 api.recordLayerPush(pusher, { head, layer: target.layer }),336 []337 )338 ),339 })340 )341 );342343export const layerRead = (344 reader: Person,345 target: LayerTarget,346 sha: Option.Option<CommitSha>347): Effect.Effect<void, never, Projects> =>348 Projects.use((projects) => projects.get(target.project)).pipe(349 Effect.flatMap((api) =>350 api.recordLayerRead(reader, {351 layer: target.layer,352 ...Option.match(sha, {353 onNone: () => ({}),354 onSome: (read) => ({ sha: read }),355 }),356 })357 ),358 Effect.catchTag("UnknownProject", () => Effect.void)359 );