escapes.ts
This file
Path
packages/gate/src/escapes.ts
101 lines · 3.0 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
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
line 1why, one step back1/**Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
lines 2–3why, one step back2 * A pure function beside the stages, not a stage: a waiting answer is not3 * refused, so the caller (the Project DO) opens the decisions.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 4–101why, one step back4 */5import type { Ask } from "@repo/protocol/domain/ask";6import { DecisionSubject } from "@repo/protocol/domain/decision";7import type { Policy } from "@repo/protocol/domain/policy";8import { RatchetName } from "@repo/protocol/ids";9import type { RepoPath } from "@repo/protocol/ids";10import { Option } from "effect";1112import type { PushFacts } from "./facts.ts";13import { matchAny } from "./glob.ts";14import { markersOf, scanMarkers } from "./markers.ts";15import type { MarkerHit } from "./markers.ts";1617export type Waiver = Extract<DecisionSubject, { readonly _tag: "Waiver" }>;1819export interface RaiseRequest {20 readonly ratchet: RatchetName;21 readonly territory: string;22 readonly reason: string;23}2425export const escapeRatchet = (territory: string): RatchetName =>26 RatchetName.make(27 `escapes-${28 territory29 .toLowerCase()30 .split(/[^a-z0-9]+/u)31 .filter((word) => word !== "")32 .join("-") || "root"33 }`34 );3536interface Escape extends MarkerHit {37 readonly given: string;38}3940const escapesIn = (41 lines: PushFacts["addedLines"],42 markers: readonly string[]43): readonly Escape[] =>44 scanMarkers(lines, markers).flatMap((hit) =>45 Option.match(hit.reason, {46 onNone: () => [],47 onSome: (given) => [{ ...hit, given }],48 })49 );5051export const territoryOf = (policy: Policy, ask: Ask, path: RepoPath) =>52 policy.modules.find((module) => matchAny(module.paths, path))?.module ??53 ask.territory.module;5455export const escapeDecisions = (facts: PushFacts): readonly Waiver[] =>56 Option.match(facts.ask, {57 onNone: () => [],58 onSome: (ask) => {59 const markers = markersOf(facts.policy);60 const added = escapesIn(facts.addedLines, markers);61 const removed = escapesIn(facts.removedLines ?? new Map(), markers);6263 const territory = (hit: Escape) =>64 territoryOf(facts.policy, ask, hit.path);6566 const raises = (name: string) => {67 const ratchet = escapeRatchet(name);68 const baseline = facts.ratchetBaselines?.get(ratchet) ?? 0;69 const onMain = facts.escapeCounts?.get(ratchet) ?? baseline;7071 const moved = (hits: readonly Escape[]) =>72 hits.filter((hit) => territory(hit) === name).length;7374 return onMain + moved(added) - moved(removed) > baseline;75 };7677 return added78 .filter((hit) => raises(territory(hit)))79 .map(({ given, line, marker, path }) =>80 DecisionSubject.cases.Waiver.make({81 ask: ask.id,82 at: { line, path },83 marker,84 reason: given,85 })86 );87 },88 });8990export const explainedRaises = (91 policy: Policy,92 ask: Ask,93 waivers: readonly Waiver[]94): readonly RaiseRequest[] =>95 waivers.flatMap((waiver) =>96 policy.ratchets.map(({ name: ratchet }) => ({97 ratchet,98 reason: waiver.reason,99 territory: territoryOf(policy, ask, waiver.at.path),100 }))101 );