ratchets.ts
This file
Path
packages/project/src/ratchets.ts
104 lines · 2.8 KiB · TypeScript
Last changed by
Merge build/comments-project into mainoscabrielbefore adoption49efd2a
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–32why, one step back1import { sameRatchet } from "@repo/checks/kinds/ratchet";2import type {3 RaiseRatchet,4 RatchetCount,5 RatchetKey,6} from "@repo/checks/kinds/ratchet";7import type { Choice } from "@repo/protocol/domain/decision";8import type { LandedSha, RatchetName } from "@repo/protocol/ids";9import { Array, Option } from "effect";1011export interface RatchetBaseline {12 readonly ratchet: RatchetName;13 readonly territory: string;14 readonly count: number;15 readonly setAt: LandedSha;16}1718export interface Lowered {19 readonly ratchet: RatchetName;20 readonly territory: string;21 readonly before: number;22 readonly after: number;23}2425export interface BaselinesAfterLanding {26 readonly baselines: readonly RatchetBaseline[];27 readonly lowered: readonly Lowered[];28}2930const baselineOf = (baselines: readonly RatchetBaseline[], key: RatchetKey) =>31 Array.findFirst(baselines, sameRatchet(key));32Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 33why, one step back33/** A landing lowers baselines or sets new ones, never raises one: only an accepted `RaiseRatchet` does. */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 34–78why, one step back34export const afterLanding = (35 baselines: readonly RatchetBaseline[],36 measured: readonly RatchetCount[],37 landed: LandedSha38): BaselinesAfterLanding => {39 const lowered = baselines.flatMap((held) =>40 Array.findFirst(measured, sameRatchet(held)).pipe(41 Option.filter(({ count }) => count < held.count),42 Option.map(({ count }): Lowered => ({43 after: count,44 before: held.count,45 ratchet: held.ratchet,46 territory: held.territory,47 })),48 Option.toArray49 )50 );5152 const kept = baselines.map((held) =>53 Option.match(Array.findFirst(lowered, sameRatchet(held)), {54 onNone: () => held,55 onSome: ({ after }) => ({ ...held, count: after, setAt: landed }),56 })57 );5859 const first = Array.dedupeWith(60 measured.filter((count) => Option.isNone(baselineOf(baselines, count))),61 (one, other) => sameRatchet(one)(other)62 ).map(({ count, ratchet, territory }) => ({63 count,64 ratchet,65 setAt: landed,66 territory,67 }));6869 return { baselines: [...kept, ...first], lowered };70};7172const raisesOn = {73 accept: true,74 adopt: true,75 decline: false,76 revert: false,77} as const satisfies Record<Choice, boolean>;78Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 79why, one step back79/** An accepted raise never lowers a baseline another accepted raise already took past 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 80–104why, one step back80export const decideRaise = (81 baselines: readonly RatchetBaseline[],82 subject: RaiseRatchet,83 choice: Choice,84 landed: LandedSha85): readonly RatchetBaseline[] => {86 if (!raisesOn[choice]) {87 return baselines;88 }8990 const raised = {91 count: subject.after,92 ratchet: subject.ratchet,93 setAt: landed,94 territory: subject.territory,95 };9697 return Option.match(baselineOf(baselines, subject), {98 onNone: () => [...baselines, raised],99 onSome: (held) =>100 held.count >= subject.after101 ? baselines102 : baselines.map((row) => (sameRatchet(subject)(row) ? raised : row)),103 });104};