posture.ts
This file
Path
packages/visibility/src/posture.ts
216 lines · 6.4 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
lines 1–193why, one step back1import type {2 Layer,3 LayerKind,4 VisibilityPolicy,5} from "@repo/protocol/domain/visibility";6import type { Posture } from "@repo/protocol/posture";7import { Array, Option, Order } from "effect";89/**10 * Licences on the OSI's list that the platform knows by SPDX id, without11 * their `-only`, `-or-later` or `+` suffix. A licence off this list is read12 * as not OSI-approved: the label errs towards closed, never towards open.13 */14const OSI: ReadonlySet<string> = new Set([15 "agpl-3.0",16 "apache-2.0",17 "bsd-2-clause",18 "bsd-3-clause",19 "gpl-2.0",20 "gpl-3.0",21 "isc",22 "lgpl-2.1",23 "lgpl-3.0",24 "mit",25 "mpl-2.0",26]);2728const isOsi = (id: string) =>29 OSI.has(30 id31 .toLowerCase()32 .replace(/\+$/u, "")33 .replace(/-(?:only|or-later)$/u, "")34 );3536/**37 * Exceptions on SPDX's list that the platform knows to add permissions only38 * (linking, runtime, syscall and build-tool exceptions). Any other addition39 * after `WITH` may take a freedom away (`Commons-Clause`, which SPDX does not40 * list as an exception, forbids selling the code), so it closes the licence:41 * the same lean towards closed as the licence list.42 */43const PERMISSIVE_EXCEPTIONS: ReadonlySet<string> = new Set([44 "autoconf-exception-3.0",45 "bison-exception-2.2",46 "classpath-exception-2.0",47 "font-exception-2.0",48 "gcc-exception-2.0",49 "gcc-exception-3.1",50 "gpl-3.0-linking-exception",51 "lgpl-3.0-linking-exception",52 "libtool-exception",53 "linux-syscall-note",54 "llvm-exception",55 "ocaml-lgpl-linking-exception",56 "qt-gpl-exception-1.0",57 "qt-lgpl-exception-1.1",58 "universal-foss-exception-1.0",59 "wxwindows-exception-3.1",60]);6162// An open licence and the token after the expression read so far.63type Step = Option.Option<readonly [open: boolean, next: number]>;6465const OPERATORS: ReadonlySet<string> = new Set(["(", ")", "AND", "OR", "WITH"]);6667/**68 * Whether an SPDX licence expression leaves the code open: a choice (`OR`)69 * is open when any side is, a conjunction (`AND`) when every side is, and70 * an exception (`WITH`) only when it is one known to add permissions. An71 * expression that does not parse is not open.72 */73const isOpen = (expression: string): boolean => {74 const tokens = expression.match(/[()]|[^\s()]+/gu) ?? [];75 const word = (at: number) => tokens[at]?.toUpperCase() ?? ")";76 const isId = (at: number) => at < tokens.length && !OPERATORS.has(word(at));7778 const joined =79 (80 operand: (at: number) => Step,81 operator: string,82 combine: (left: boolean, right: boolean) => boolean83 ) =>84 (at: number): Step => {85 let step = operand(at);8687 while (Option.isSome(step) && word(step.value[1]) === operator) {88 const [left, next] = step.value;8990 step = Option.map(91 operand(next + 1),92 ([right, after]) => [combine(left, right), after] as const93 );94 }9596 return step;97 };9899 // One licence: an id, maybe `WITH` an exception, or `inner` parenthesised.100 const atom =101 (inner: (at: number) => Step) =>102 (at: number): Step => {103 if (tokens[at] === "(") {104 return Option.filter(105 inner(at + 1),106 ([, next]) => tokens[next] === ")"107 ).pipe(Option.map(([open, next]) => [open, next + 1] as const));108 }109110 if (!isId(at)) {111 return Option.none();112 }113114 const open = isOsi(tokens[at] ?? "");115116 if (word(at + 1) !== "WITH") {117 return Option.some([open, at + 1]);118 }119120 return isId(at + 2)121 ? Option.some([122 open &&123 PERMISSIVE_EXCEPTIONS.has(tokens[at + 2]?.toLowerCase() ?? ""),124 at + 3,125 ])126 : Option.none();127 };128129 // Choices (`OR`) of conjunctions (`AND`) of licences, from `from`.130 const anyOf = (from: number): Step =>131 joined(132 joined(atom(anyOf), "AND", (left, right) => left && right),133 "OR",134 (left, right) => left || right135 )(from);136137 return Option.exists(138 anyOf(0),139 ([open, next]) => open && next === tokens.length140 );141};142143// Kinds whose files would ship: a layer holding one keeps shipped code closed.144const SHIPPED: ReadonlySet<LayerKind> = new Set(["fix", "source"]);145146const narrowing = (layer: Layer) =>147 layer.audience !== "personal" && layer.audience !== "public";148149const ships = (layer: Layer) => layer.holds.some((kind) => SHIPPED.has(kind));150151const sorted = (names: readonly string[]) =>152 Array.dedupe(Array.sort(names, Order.String)).join(", ");153154const days = (count: number) => (count === 1 ? "1 day" : `${count} days`);155156const labelled = (posture: Posture, label: string) => ({ label, posture });157158const fromLayers = (visibility: VisibilityPolicy) => {159 const layers = visibility.layers.filter(narrowing);160 const shipping = layers.filter(ships);161162 // A layer of shipped files with no limit on its wait never promises to open.163 const closed = shipping.filter((layer) => layer.maxDays === undefined);164 const waits = shipping.flatMap((layer) => layer.maxDays ?? []);165166 if (Array.isReadonlyArrayNonEmpty(closed)) {167 return labelled(168 "openCore",169 `Open core · closed: ${sorted(closed.map((layer) => layer.name))}`170 );171 }172173 if (Array.isReadonlyArrayNonEmpty(waits)) {174 return labelled(175 "openOnSchedule",176 `Open on schedule · embargo ≤ ${days(Math.max(...waits))}`177 );178 }179180 if (layers.length === 0) {181 return labelled("open", "Open");182 }183184 const areas = Object.keys(visibility.checks?.areas ?? {});185186 return labelled(187 "openWithSealedEvidence",188 areas.length === 0189 ? "Open with sealed evidence"190 : `Open with sealed evidence · sealed checks in ${sorted(areas)}`191 );192};193Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
line 194why, one step back194/** `licence` is the SPDX id or expression from the landed `LICENSE` or `package.json`; none found leaves the layers to decide. */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 195–213why, one step back195export const postureOf = (input: {196 readonly visibility: Option.Option<VisibilityPolicy>;197 readonly licence: Option.Option<string>;198}): { readonly posture: Posture; readonly label: string } =>199 Option.exists(input.licence, (licence) => !isOpen(licence))200 ? labelled("sourceAvailable", "Source available")201 : Option.match(input.visibility, {202 onNone: () => labelled("open", "Open"),203 onSome: fromLayers,204 });205206const FURTHER_FROM_OPEN: readonly Posture[] = [207 "open",208 "openWithSealedEvidence",209 "openOnSchedule",210 "openCore",211 "sourceAvailable",212];213Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
line 214why, one step back214/** Narrowing is a `PostureChange` decision with a notice period; widening is a release. */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 215–216why, one step back215export const narrows = (from: Posture, to: Posture): boolean =>216 FURTHER_FROM_OPEN.indexOf(to) > FURTHER_FROM_OPEN.indexOf(from);