Skip to content
Myr

claim.ts

This file

Path

packages/project/src/claim.ts

395 lines · 9.6 KiB · TypeScript

Last changed by

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

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–4why, one step back
1import { ClaimView } from "@repo/protocol/domain/claim";
2import type { Policy } from "@repo/protocol/domain/policy";3import {4 AskId,

Merge build/1400-followups into mainoscabrielbefore adoption7a90542

line 5why, one step back
5 claimForkName,

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 6–7why, one step back
6 CommitSha,
7 Handle,

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 8why, one step back
8 keyOf,

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–131why, one step back
9 LandedSha,
10 ProjectName,11 RepoName,12} from "@repo/protocol/ids";13import { Person } from "@repo/protocol/principal";14import {15 AskBlocked,16 AskFull,17 CapReached,18 ClaimLapsed,19 Denounced,20 NotClaimed,21 NotVouched,22} from "@repo/protocol/refusal";23import { Array as Arr, Data, DateTime, Option, Result, Schema } from "effect";2425import { ProjectEventInput } from "./events.ts";2627const claimFields = {28 ask: AskId,29 base: CommitSha,30 claimant: Handle,31 fork: RepoName,32 lapsesAt: Schema.DateTimeUtcFromString,33 lastPushAt: Schema.OptionFromOptionalKey(Schema.DateTimeUtcFromString),34 openedAt: Schema.DateTimeUtcFromString,35 warnsAt: Schema.DateTimeUtcFromString,36};3738export const ClaimState = Schema.TaggedUnion({39 Answered: { ...claimFields, landing: LandedSha },40 Lapsed: { ...claimFields, at: Schema.DateTimeUtcFromString },41 Live: claimFields,42 Open: { ask: AskId },43 Released: claimFields,44 Warned: claimFields,45});4647export type ClaimState = typeof ClaimState.Type;4849export type Live = Omit<typeof ClaimState.cases.Live.Type, "_tag">;5051export const ClaimCommand = Schema.TaggedUnion({52 Claim: {53 base: CommitSha,54 blockedBy: Schema.Array(AskId),55 cap: Schema.Int,56 denounced: Schema.Boolean,57 heldByWho: Schema.Int,58 liveCount: Schema.Int,59 project: ProjectName,60 race: Schema.Int,61 vouched: Schema.Boolean,62 who: Person,63 },64 Land: { landing: LandedSha },65 Push: { head: CommitSha, who: Handle },66 Release: { who: Handle },67 Tick: {},68});6970export type ClaimCommand = typeof ClaimCommand.Type;7172export type ClaimEffect = Data.TaggedEnum<{73 Emit: { readonly event: ProjectEventInput };74 MakeFork: { readonly from: RepoName; readonly name: RepoName };75 RevokeAccess: { readonly fork: RepoName };76 SetAlarm: { readonly at: DateTime.Utc };77}>;7879export const ClaimEffect = Data.taggedEnum<ClaimEffect>();8081export type ClaimRefusal =82 | AskBlocked83 | AskFull84 | CapReached85 | ClaimLapsed86 | Denounced87 | NotClaimed88 | NotVouched;8990export interface ClaimStep {91 readonly effects: readonly ClaimEffect[];92 readonly state: ClaimState;93}9495export type ClaimResult = Result.Result<ClaimStep, ClaimRefusal>;9697const stay = (state: ClaimState): ClaimResult =>98 Result.succeed({ effects: [], state });99100const move = (101 state: ClaimState,102 ...effects: readonly ClaimEffect[]103): ClaimResult => Result.succeed({ effects, state });104105const claimOf = ({106 ask,107 base,108 claimant,109 fork,110 lapsesAt,111 lastPushAt,112 openedAt,113 warnsAt,114}: Live): Live => ({115 ask,116 base,117 claimant,118 fork,119 lapsesAt,120 lastPushAt,121 openedAt,122 warnsAt,123});124125const deadlines = (from: DateTime.Utc, policy: Policy["claims"]) => ({126 lapsesAt: DateTime.add(from, { milliseconds: policy.lapseAfterMs }),127 warnsAt: DateTime.add(from, { milliseconds: policy.warnAfterMs }),128});129130type Claim = typeof ClaimCommand.cases.Claim.Type;131

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 132why, one step back
132// `AskFull` comes first, so a full ask never forks.

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 133–164why, one step back
133const grant = (
134 ask: AskId,135 made: Claim,136 now: DateTime.Utc,137 policy: Policy["claims"]138): ClaimResult => {139 if (made.liveCount >= made.race) {140 return Result.fail(new AskFull({ ask }));141 }142143 if (Arr.isReadonlyArrayNonEmpty(made.blockedBy)) {144 return Result.fail(AskBlocked.of(ask, made.blockedBy));145 }146147 if (made.heldByWho >= made.cap) {148 return Result.fail(new CapReached({ cap: made.cap }));149 }150151 if (made.denounced) {152 return Result.fail(new Denounced({}));153 }154155 if (!made.vouched) {156 return Result.fail(new NotVouched({}));157 }158159 const claimant = made.who.handle;160161 const live = ClaimState.cases.Live.make({162 ask,163 base: made.base,164 claimant,

Merge build/1400-followups into mainoscabrielbefore adoption7a90542

line 165why, one step back
165 fork: claimForkName(made.project, ask, claimant),

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–173why, one step back
166 lastPushAt: Option.none(),
167 openedAt: now,168 ...deadlines(now, policy),169 });170171 return move(172 live,173 ClaimEffect.MakeFork({

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 174why, one step back
174 from: RepoName.make(keyOf(made.project)),

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 175–395why, one step back
175 name: live.fork,
176 }),177 ClaimEffect.SetAlarm({ at: live.warnsAt }),178 ClaimEffect.Emit({179 event: ProjectEventInput.Claimed({ ask, at: now, claimant }),180 })181 );182};183184const claim = (185 state: ClaimState,186 made: Claim,187 now: DateTime.Utc,188 policy: Policy["claims"]189): ClaimResult => {190 const holder = (live: Live) =>191 live.claimant === made.who.handle192 ? stay(state)193 : Result.fail(new AskFull({ ask: live.ask }));194195 return ClaimState.match(state, {196 Answered: ({ ask }) => Result.fail(new AskFull({ ask })),197 Lapsed: ({ ask }) => grant(ask, made, now, policy),198 Live: holder,199 Open: ({ ask }) => grant(ask, made, now, policy),200 Released: ({ ask }) => grant(ask, made, now, policy),201 Warned: holder,202 });203};204205const unclaimed = ({ ask }: { readonly ask: AskId }) =>206 Result.fail(NotClaimed.of(ask));207208const push = (209 state: ClaimState,210 { head, who }: typeof ClaimCommand.cases.Push.Type,211 now: DateTime.Utc,212 policy: Policy["claims"]213): ClaimResult => {214 const kept = (live: Live) => {215 if (live.claimant !== who) {216 return Result.fail(NotClaimed.of(live.ask));217 }218219 const pushed = ClaimState.cases.Live.make({220 ...claimOf(live),221 lastPushAt: Option.some(now),222 ...deadlines(now, policy),223 });224225 return move(226 pushed,227 ClaimEffect.SetAlarm({ at: pushed.warnsAt }),228 ClaimEffect.Emit({229 event: ProjectEventInput.Pushed({ ask: live.ask, at: now, head }),230 })231 );232 };233234 return ClaimState.match(state, {235 Answered: unclaimed,236 Lapsed: (lapsed) =>237 lapsed.claimant === who238 ? Result.fail(ClaimLapsed.of(lapsed.ask))239 : unclaimed(lapsed),240 Live: kept,241 Open: unclaimed,242 Released: unclaimed,243 Warned: kept,244 });245};246247const tick = (state: ClaimState, now: DateTime.Utc): ClaimResult => {248 const reached = (deadline: DateTime.Utc) =>249 DateTime.isGreaterThanOrEqualTo(now, deadline);250251 // An alarm that comes late lapses the claim without warning it first.252 const lapse = (live: Live) =>253 move(254 ClaimState.cases.Lapsed.make({ ...claimOf(live), at: now }),255 ClaimEffect.RevokeAccess({ fork: live.fork }),256 ClaimEffect.Emit({257 event: ProjectEventInput.Lapsed({ ask: live.ask, at: now }),258 })259 );260261 const warn = (live: Live) =>262 move(263 ClaimState.cases.Warned.make(claimOf(live)),264 ClaimEffect.SetAlarm({ at: live.lapsesAt }),265 ClaimEffect.Emit({266 event: ProjectEventInput.Warned({ ask: live.ask, at: now }),267 })268 );269270 return ClaimState.match(state, {271 Answered: stay,272 Lapsed: stay,273 Live: (live) => {274 if (reached(live.lapsesAt)) {275 return lapse(live);276 }277278 return reached(live.warnsAt) ? warn(live) : stay(state);279 },280 Open: stay,281 Released: stay,282 Warned: (warned) =>283 reached(warned.lapsesAt) ? lapse(warned) : stay(state),284 });285};286287const release = (288 state: ClaimState,289 who: Handle,290 now: DateTime.Utc291): ClaimResult => {292 const end = (live: Live) =>293 live.claimant === who294 ? move(295 ClaimState.cases.Released.make(claimOf(live)),296 ClaimEffect.RevokeAccess({ fork: live.fork }),297 ClaimEffect.Emit({298 event: ProjectEventInput.Released({ ask: live.ask, at: now }),299 })300 )301 : Result.fail(NotClaimed.of(live.ask));302303 const ended = (claimed: Live) =>304 claimed.claimant === who305 ? stay(state)306 : Result.fail(NotClaimed.of(claimed.ask));307308 return ClaimState.match(state, {309 Answered: ended,310 Lapsed: ended,311 Live: end,312 Open: unclaimed,313 Released: ended,314 Warned: end,315 });316};317318const land = (state: ClaimState, landing: LandedSha): ClaimResult => {319 const answer = (live: Live, ...effects: readonly ClaimEffect[]) =>320 move(321 ClaimState.cases.Answered.make({ ...claimOf(live), landing }),322 ...effects323 );324325 const revoked = (live: Live) =>326 answer(live, ClaimEffect.RevokeAccess({ fork: live.fork }));327328 return ClaimState.match(state, {329 Answered: stay,330 Lapsed: (lapsed) => answer(lapsed),331 Live: revoked,332 Open: stay,333 Released: stay,334 Warned: revoked,335 });336};337338export const transition = (339 state: ClaimState,340 command: ClaimCommand,341 now: DateTime.Utc,342 policy: Policy["claims"]343): ClaimResult =>344 ClaimCommand.match(command, {345 Claim: (made) => claim(state, made, now, policy),346 Land: ({ landing }) => land(state, landing),347 Push: (pushed) => push(state, pushed, now, policy),348 Release: ({ who }) => release(state, who, now),349 Tick: () => tick(state, now),350 });351352export interface ViewContext {353 readonly checkCommand: string;354 readonly project: ProjectName;355 readonly remote: string;356}357358export const toView = (359 state: ClaimState,360 context: ViewContext361): Option.Option<ClaimView> => {362 const view = (claimed: Live, standing: ClaimView["state"]) => {363 const shown = {364 ask: claimed.ask,365 base: claimed.base,366 checkCommand: context.checkCommand,367 claimant: claimed.claimant,368 fork: claimed.fork,369 lapsesAt: claimed.lapsesAt,370 openedAt: claimed.openedAt,371 project: context.project,372 remote: context.remote,373 state: standing,374 warnsAt: claimed.warnsAt,375 };376377 return Option.some(378 ClaimView.make(379 Option.match(claimed.lastPushAt, {380 onNone: () => shown,381 onSome: (lastPushAt) => ({ ...shown, lastPushAt }),382 })383 )384 );385 };386387 return ClaimState.match(state, {388 Answered: (answered) => view(answered, "answered"),389 Lapsed: (lapsed) => view(lapsed, "lapsed"),390 Live: (live) => view(live, "live"),391 Open: () => Option.none(),392 Released: (released) => view(released, "released"),393 Warned: (warned) => view(warned, "warned"),394 });395};