Skip to content
Myr

visibility.ts

This file

Path

packages/sdk/src/visibility.ts

267 lines · 8.6 KiB · TypeScript

Last changed by

Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

lines 1–2why, one step back
1// The public file names areas, kinds and rules only: what a `sealed` layer
2// discloses. Only a layer's own manifest names the held-back checks' paths.

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 3–33why, one step back
3import {
4 Layer,5 LayerManifest,6 SealedChecks,7 VisibilityPolicy,8} from "@repo/protocol/domain/visibility";9import type {10 Audience,11 Disclosure,12 LayerKind,13 Members,14 SealedCheck,15} from "@repo/protocol/domain/visibility";16import { Duration, Predicate, Schema, Struct } from "effect";1718import { checked } from "./checked.ts";19import type { Days } from "./project.ts";2021type VisibilityPolicyEncoded = typeof VisibilityPolicy.Encoded;2223type SealedChecksEncoded = typeof SealedChecks.Encoded;2425export type MembersInput = typeof Members.Encoded;2627export const deciders = "deciders" satisfies MembersInput;2829export type LayerDefinition = Omit<typeof Layer.Encoded, "name" | "repo">;3031export type LayerHolds = readonly [LayerKind, ...LayerKind[]];3233export interface LayerConfig<Holds extends LayerHolds = LayerHolds> {

Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571

line 34why, one step back
34 /** Who may read it. Default `"maintainers"`, the maintainers' audience. */

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 35–61why, one step back
35 readonly audience?: Audience;
36 /** What people outside its audience learn about what it holds. */37 readonly disclose: Disclosure;38 readonly holds: Holds;39 readonly readers?: MembersInput;40 readonly reason: string;41 /**42 * How long each kind it holds may stay unreleased: `{ experiment: "30 days" }`.43 * The protocol keeps one sunset a layer, so the layer takes the soonest.44 */45 readonly sunset?: Readonly<Partial<Record<Holds[number], Days>>>;46 readonly writers?: MembersInput;47}4849const LayerFields = Schema.Struct(Struct.omit(Layer.fields, ["name", "repo"]));5051const layerOf = checked(LayerFields);5253// The protocol keeps one sunset a layer: the soonest any kind it holds is due.54const sunsetOf = (sunset: Readonly<Partial<Record<string, Days>>>) => {55 const days = Object.values(sunset)56 .filter(Predicate.isNotUndefined)57 .map((each) => Duration.toDays(each));5859 return days.length === 0 ? {} : { sunsetDays: Math.min(...days) };60};61

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

line 62why, one step back
62/** Declares a layer: a separate repo holding one audience's files. Its paths stay in the layer. */

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 63–117why, one step back
63export const layer = <const Holds extends LayerHolds>(
64 config: LayerConfig<Holds>65): LayerDefinition =>66 layerOf({67 audience: config.audience ?? "maintainers",68 disclose: config.disclose,69 holds: config.holds,70 readers: config.readers ?? deciders,71 reason: config.reason,72 writers: config.writers ?? deciders,73 ...sunsetOf(config.sunset ?? {}),74 });7576/**77 * Declares an embargo: fixes held for the named readers, released within78 * `maxDays` and counted in public. Config only: nothing releases it yet.79 */80export const embargo = (config: {81 readonly disclose: "counted";82 readonly maxDays: number;83 readonly readers: MembersInput;84}): LayerDefinition =>85 layerOf({86 audience: "security",87 disclose: config.disclose,88 holds: ["fix"],89 maxDays: config.maxDays,90 readers: config.readers,91 reason: "Embargoed fixes, released with their advisory",92 writers: deciders,93 });9495/**96 * Declares the personal layer: each person's own agent and tool97 * configuration, hidden, gating nothing. Config only.98 */99export const personal = (): LayerDefinition =>100 layerOf({101 audience: "personal",102 disclose: "hidden",103 holds: ["personal"],104 readers: deciders,105 reason:106 "Each person's own agent and tool configuration, for them and their agents only",107 writers: deciders,108 });109110export type SealedChecksDefinition<Area extends string = string> = Omit<111 SealedChecksEncoded,112 "areas"113> & { readonly areas: Readonly<Record<Area, string>> };114115const sealedChecksOf = checked(SealedChecks);116117/**

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

lines 118–120why, one step back
118 * Declares the held-back checks. An answer is judged by the suite pinned at
119 * its claim; a failure tells it one bit per area, and the suite is committed120 * to in public by a salted SHA-256 Merkle root.

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 121–163why, one step back
121 */
122export const sealedChecks = <123 const Areas extends Readonly<Record<string, string>>,124>(config: {125 readonly admit?: {126 readonly greenOnMainRuns?: number;127 readonly redOnKnownBadAtLeast?: number;128 };129 readonly areas: Areas;130 readonly attempts?: {131 readonly perClaim?: number;132 readonly perPersonPerAreaPerWeek?: number;133 };134 readonly canary?: boolean;135 /** The layer that holds the check files. Default `"maintainers"`. */136 readonly layer?: string;137}): SealedChecksDefinition<keyof Areas & string> => ({138 ...sealedChecksOf({139 admit: {140 greenOnMainRuns: config.admit?.greenOnMainRuns ?? 3,141 redOnKnownBadAtLeast: config.admit?.redOnKnownBadAtLeast ?? 1,142 },143 areas: config.areas,144 attempts: {145 perClaim: config.attempts?.perClaim ?? 3,146 perPersonPerAreaPerWeek: config.attempts?.perPersonPerAreaPerWeek ?? 10,147 },148 canary: config.canary ?? false,149 commit: "salted-sha256-merkle",150 layer: config.layer ?? "maintainers",151 pin: "claim",152 result: "area-bit",153 }),154 areas: config.areas,155});156157export type FlowsDefinition = VisibilityPolicyEncoded["flows"];158159const flowsOf = checked(VisibilityPolicy.fields.flows);160161export const flows = {162 /**163 * Nothing moves from a layer into a wider audience but a credited

Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571

line 164why, one step back
164 * release, by the people named. Default release: the maintainers.

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 165–173why, one step back
165 */
166 noWriteDown: (options?: {167 readonly release?: MembersInput;168 }): FlowsDefinition =>169 flowsOf({ noWriteDown: true, release: options?.release ?? deciders }),170};171172export interface VisibilityConfig<Area extends string = string> {173 readonly checks?: SealedChecksDefinition<Area>;

Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571

line 174why, one step back
174 /** Who may set a write-only env value. Default the maintainers. */

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–205why, one step back
175 readonly env?: { readonly setters: MembersInput };
176 /** Default `flows.noWriteDown()`. */177 readonly flows?: FlowsDefinition;178 readonly layers?: Readonly<Record<string, LayerDefinition>>;179 /** How long a change that narrows visibility waits before it lands. Default `"7 days"`. */180 readonly notice?: { readonly narrowing: Days };181}182183export type VisibilityDefinition<Area extends string = string> = Omit<184 VisibilityPolicyEncoded,185 "checks"186> & { readonly checks?: SealedChecksDefinition<Area> };187188// The held-back checks live in a layer the file declares, one that holds189// checks: a misspelt layer would leave a suite that nothing runs.190const policyOf = checked(191 VisibilityPolicy.check(192 Schema.makeFilter(({ checks, layers }) =>193 checks === undefined ||194 layers.some(195 (each) => each.name === checks.layer && each.holds.includes("checks")196 )197 ? undefined198 : {199 issue: `no declared layer "${checks.layer}" holds checks`,200 path: ["checks", "layer"],201 }202 )203 )204);205

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

line 206why, one step back
206/** Declares a project's visibility: the default export of `visibility.ts`. A project without one is Open. */

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 207–256why, one step back
207export const visibility = <Area extends string = never>(
208 config: VisibilityConfig<Area>209): VisibilityDefinition<Area> => {210 const { checks } = config;211212 const policy: VisibilityPolicyEncoded = {213 env: { setters: config.env?.setters ?? deciders },214 flows: config.flows ?? flows.noWriteDown(),215 layers: Object.entries(config.layers ?? {}).map(([name, each]) => ({216 ...each,217 name,218 })),219 notice: {220 narrowingDays: Duration.toDays(config.notice?.narrowing ?? "7 days"),221 },222 };223224 // Checked whole, so a key the schema doesn't name (a check's path) is225 // dropped. The held-back checks' areas, checked with the rest, come back226 // as the file wrote them, so they keep the names `AreaOf` reads.227 const { checks: suite, ...stated } = policyOf(228 checks === undefined ? policy : { ...policy, checks }229 );230231 return checks === undefined || suite === undefined232 ? stated233 : { ...stated, checks: { ...suite, areas: checks.areas } };234};235236/**237 * The areas a visibility file's (or a `sealedChecks`'s) held-back checks238 * guard, by name: `layerManifest<AreaOf<typeof policy>>(…)` holds each239 * check's area to them.240 */241export type AreaOf<Definition> =242 Definition extends SealedChecksDefinition<infer Area>243 ? Area244 : Definition extends VisibilityDefinition<infer Area>245 ? Area246 : never;247248export type SealedCheckInput<Area extends string = string> = Omit<249 typeof SealedCheck.Encoded,250 "area"251> & { readonly area: Area };252253export type LayerManifestDefinition = typeof LayerManifest.Encoded;254255const manifestOf = checked(LayerManifest);256

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

line 257why, one step back
257/** Declares a layer's manifest: the default export of `layer.ts` in the layer repo's config folder. */

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 258–267why, one step back
258export const layerManifest = <Area extends string>(config: {
259 readonly checks: readonly SealedCheckInput<Area>[];260 readonly knownBad?: readonly string[];261 readonly layer: string;262}): LayerManifestDefinition =>263 manifestOf({264 checks: config.checks,265 knownBad: config.knownBad ?? [],266 layer: config.layer,267 });