Skip to content
Myr

ledger.ts

This file

Path

packages/project/src/visibility/ledger.ts

223 lines · 6.4 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–85why, one step back
1import { DecisionSubject } from "@repo/protocol/domain/decision";
2import type { Decision } from "@repo/protocol/domain/decision";3import { LedgerEntry } from "@repo/protocol/domain/visibility";4import type {5 Ledger,6 VisibilityPolicy,7} from "@repo/protocol/domain/visibility";8import type { ProjectName } from "@repo/protocol/ids";9import type { Posture } from "@repo/protocol/posture";10import { ledgerOf } from "@repo/visibility/ledger";11import { postureOf } from "@repo/visibility/posture";12import { Array as Arr, DateTime, Option } from "effect";1314import { ProjectEventInput } from "../events.ts";15import type { ProjectSnapshot, ProjectWrites } from "../project-store.ts";16import type { LayerRow, LedgerRow } from "../project-store/rows.ts";17import { policyIn } from "../visibility-port.ts";1819const WORDS = {20 open: "Open",21 openCore: "Open core",22 openOnSchedule: "Open on schedule",23 openWithSealedEvidence: "Open with sealed evidence",24 sourceAvailable: "Source available",25} as const satisfies Record<Posture, string>;2627export const nextSeq = (rows: readonly LedgerRow[]): number =>28 Math.max(0, ...rows.map(({ seq }) => seq)) + 1;2930export const appended = (entries: readonly LedgerEntry[]): ProjectWrites =>31 entries.length === 032 ? {}33 : {34 events: entries.map((entry) =>35 ProjectEventInput.LedgerAppended({ at: entry.at, entry })36 ),37 ledger: entries,38 };3940export const visibilityIn = (41 snapshot: ProjectSnapshot42): Option.Option<VisibilityPolicy> =>43 Option.flatMap(policyIn(snapshot), (policy) =>44 Option.fromUndefinedOr(policy.visibility)45 );4647export const declaredBy = (visibility: Option.Option<VisibilityPolicy>) =>48 postureOf({ licence: Option.none(), visibility });4950type PostureChange = Extract<51 DecisionSubject,52 { readonly _tag: "PostureChange" }53>;5455interface Asked {56 readonly decision: Decision;57 readonly subject: PostureChange;58}5960const askedOf = (decision: Decision): Option.Option<Asked> =>61 DecisionSubject.guards.PostureChange(decision.subject)62 ? Option.some({ decision, subject: decision.subject })63 : Option.none();6465const takesEffect = (decision: Decision, noticeDays: number) =>66 Option.map(67 Option.filter(68 Option.fromUndefinedOr(decision.decided),69 ({ choice }) => choice === "accept"70 ),71 ({ at }) =>72 DateTime.max(at, DateTime.add(decision.openedAt, { days: noticeDays }))73 );7475export interface Standing {76 readonly label: string;77 readonly posture: Posture;78 readonly declared: Posture;79 readonly changed: Option.Option<{80 readonly at: DateTime.Utc;81 readonly decision: Decision;82 readonly subject: PostureChange;83 }>;84}85

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 86why, one step back
86/** Only the latest posture decision counts: a config that moved on from its `to` without a decision widened, and stands. */

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 87–133why, one step back
87export const standing = (input: {
88 readonly decisions: readonly Decision[];89 readonly now: DateTime.Utc;90 readonly visibility: Option.Option<VisibilityPolicy>;91}): Standing => {92 const declared = declaredBy(input.visibility);9394 const asked = Option.filter(95 Arr.last(96 input.decisions.flatMap((decision) => Option.toArray(askedOf(decision)))97 ),98 ({ subject }) => subject.to === declared.posture99 );100101 if (Option.isNone(asked)) {102 return {103 changed: Option.none(),104 declared: declared.posture,105 ...declared,106 };107 }108109 const noticeDays = Option.match(input.visibility, {110 onNone: () => 0,111 onSome: ({ notice }) => notice.narrowingDays,112 });113114 const effective = Option.filter(115 takesEffect(asked.value.decision, noticeDays),116 (at) => DateTime.isLessThanOrEqualTo(at, input.now)117 );118119 return Option.match(effective, {120 onNone: (): Standing => ({121 changed: Option.none(),122 declared: declared.posture,123 label: WORDS[asked.value.subject.from],124 posture: asked.value.subject.from,125 }),126 onSome: (at): Standing => ({127 changed: Option.some({ at, ...asked.value }),128 declared: declared.posture,129 ...declared,130 }),131 });132};133

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 134–154why, one step back
134export const nextMove = (input: {
135 readonly decisions: readonly Decision[];136 readonly now: DateTime.Utc;137 readonly visibility: Option.Option<VisibilityPolicy>;138}): Option.Option<DateTime.Utc> => {139 const declared = declaredBy(input.visibility);140141 const noticeDays = Option.match(input.visibility, {142 onNone: () => 0,143 onSome: ({ notice }) => notice.narrowingDays,144 });145146 return Arr.last(147 input.decisions.flatMap((decision) => Option.toArray(askedOf(decision)))148 ).pipe(149 Option.filter(({ subject }) => subject.to === declared.posture),150 Option.flatMap(({ decision }) => takesEffect(decision, noticeDays)),151 Option.filter((at) => DateTime.isGreaterThan(at, input.now))152 );153};154

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 155why, one step back
155/** Not while a narrowing from Open waits on its decision: nothing was sealed before. */

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 156–198why, one step back
156export const sealedInEffect = (posture: Standing) =>
157 posture.posture !== "open" || posture.declared === "open";158159export const postureCaughtUp = (160 snapshot: ProjectSnapshot,161 posture: Standing,162 seq: number163): readonly LedgerEntry[] =>164 Option.match(posture.changed, {165 onNone: () => [],166 onSome: ({ at, decision, subject }) =>167 snapshot.ledger.some(168 (row) =>169 LedgerEntry.guards.PostureChanged(row) && row.decision === decision.id170 )171 ? []172 : [173 LedgerEntry.cases.PostureChanged.make({174 at,175 decision: decision.id,176 from: subject.from,177 seq,178 to: subject.to,179 }),180 ],181 });182183const withRepos = (184 visibility: VisibilityPolicy,185 rows: readonly LayerRow[]186): VisibilityPolicy => ({187 ...visibility,188 layers: visibility.layers.map((layer) =>189 Option.match(190 Arr.findFirst(rows, ({ name }) => name === layer.name),191 {192 onNone: () => layer,193 onSome: ({ repo }) => ({ ...layer, repo }),194 }195 )196 ),197});198

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 199why, one step back
199/** The layer read log is the layers' readers' own, never in the public ledger. */

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 200–223why, one step back
200export const ledgerFor = (
201 project: ProjectName,202 snapshot: ProjectSnapshot,203 now: DateTime.Utc204): Ledger => {205 const visibility = visibilityIn(snapshot);206207 const ledger = ledgerOf({208 licence: Option.none(),209 project,210 rows: snapshot.ledger,211 visibility: Option.map(visibility, (declared) =>212 withRepos(declared, snapshot.layers)213 ),214 });215216 const { label, posture } = standing({217 decisions: snapshot.decisions,218 now,219 visibility,220 });221222 return { ...ledger, posture, postureLabel: label };223};