Skip to content
Myr

Merge build/604-sdk-visibility: a maintainer declares layers, sealed checks and who may set values in visibility.ts

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by oscabriel · committed by oscabriel · parents f77044d, 70c8512 · Browse files · 4 files+766 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+766 added−7 removed

  • tests:+403 added−0 removed
  • other:+363 added−7 removed

4 files+766 added−7 removed

modified:apps/cli/src/entries/rules.ts+23 added-3 removed
modified:packages/sdk/src/index.ts+27 added-4 removed
added:packages/sdk/src/visibility.ts+313 added
1/**2 * The config folder's `visibility.ts`: a project's layers, its held-back3 * checks, how content flows between audiences, how long a narrowing waits,4 * and who may set write-only env values. And a layer's own manifest5 * (`layer.ts` in the layer repo's config folder), which alone names the6 * held-back checks' paths, each with the clause it enforces.7 *8 * Every builder returns plain data, checked against protocol's schema at the9 * call, so a mistake fails where it is written. The public file names areas,10 * kinds and rules only: what a `sealed` layer discloses.11 */12import {13 Layer,14 LayerManifest,15 SealedChecks,16 VisibilityPolicy,17} from "@repo/protocol/domain/visibility";18import type {19 Audience,20 Disclosure,21 LayerKind,22 Members,23 SealedCheck,24} from "@repo/protocol/domain/visibility";25import { Duration, Predicate, Schema, Struct } from "effect";2627import { checked } from "./checked.ts";28import type { Days } from "./project.ts";2930type VisibilityPolicyEncoded = typeof VisibilityPolicy.Encoded;3132type SealedChecksEncoded = typeof SealedChecks.Encoded;3334/** Who a rule names, as config writes it: `deciders`, or handles without `@`. */35export type MembersInput = typeof Members.Encoded;3637/** The project's deciders, whoever they are at the landed commit. */38export const deciders = "deciders" satisfies MembersInput;3940/**41 * A layer as a builder states it: everything but its name, which is its key42 * in `visibility({ layers })`, and its repo, which the platform makes.43 */44export type LayerDefinition = Omit<typeof Layer.Encoded, "name" | "repo">;4546/** What a layer holds: one kind or more. */47export type LayerHolds = readonly [LayerKind, ...LayerKind[]];4849/** A layer, as `layer()` takes it. */50export interface LayerConfig<Holds extends LayerHolds = LayerHolds> {51 /** Who may read it. Default `"maintainers"`, the deciders' audience. */52 readonly audience?: Audience;53 /** What people outside its audience learn about what it holds. */54 readonly disclose: Disclosure;55 readonly holds: Holds;56 /** Who may clone it. */57 readonly readers?: MembersInput;58 /** Why it exists, in public. */59 readonly reason: string;60 /**61 * How long each kind it holds may stay unreleased: `{ experiment: "30 days" }`.62 * The protocol keeps one sunset a layer, so the layer takes the soonest.63 */64 readonly sunset?: Readonly<Partial<Record<Holds[number], Days>>>;65 /** Who may push to it. */66 readonly writers?: MembersInput;67}6869const LayerFields = Schema.Struct(Struct.omit(Layer.fields, ["name", "repo"]));7071const layerOf = checked(LayerFields);7273// The protocol keeps one sunset a layer: the soonest any kind it holds is due.74const sunsetOf = (sunset: Readonly<Partial<Record<string, Days>>>) => {75 const days = Object.values(sunset)76 .filter(Predicate.isNotUndefined)77 .map((each) => Duration.toDays(each));7879 return days.length === 0 ? {} : { sunsetDays: Math.min(...days) };80};8182/**83 * Declares a layer: a separate repo holding one audience's files, overlaid84 * on main only where that audience may read. Its paths stay in the layer.85 */86export const layer = <const Holds extends LayerHolds>(87 config: LayerConfig<Holds>88): LayerDefinition =>89 layerOf({90 audience: config.audience ?? "maintainers",91 disclose: config.disclose,92 holds: config.holds,93 readers: config.readers ?? deciders,94 reason: config.reason,95 writers: config.writers ?? deciders,96 ...sunsetOf(config.sunset ?? {}),97 });9899/**100 * Declares an embargo: fixes held for the named readers, released within101 * `maxDays` and counted in public. Config only: nothing releases it yet.102 */103export const embargo = (config: {104 readonly disclose: "counted";105 readonly maxDays: number;106 readonly readers: MembersInput;107}): LayerDefinition =>108 layerOf({109 audience: "security",110 disclose: config.disclose,111 holds: ["fix"],112 maxDays: config.maxDays,113 readers: config.readers,114 reason: "Embargoed fixes, released with their advisory",115 writers: deciders,116 });117118/**119 * Declares the personal layer: each person's own agent and tool120 * configuration, hidden, gating nothing. Config only.121 */122export const personal = (): LayerDefinition =>123 layerOf({124 audience: "personal",125 disclose: "hidden",126 holds: ["personal"],127 readers: deciders,128 reason:129 "Each person's own agent and tool configuration, for them and their agents only",130 writers: deciders,131 });132133/**134 * Held-back checks as the public file states them: their areas by name and135 * code glob, never a check's path or name. `Area` is the areas' names, so a136 * layer's manifest can be held to them (`AreaOf`).137 */138export type SealedChecksDefinition<Area extends string = string> = Omit<139 SealedChecksEncoded,140 "areas"141> & { readonly areas: Readonly<Record<Area, string>> };142143const sealedChecksOf = checked(SealedChecks);144145/**146 * Declares the held-back checks: the layer holding them, each area's name147 * and the code it guards, how often an answer may fail them, and when a new148 * check joins the suite. An answer is judged by the suite pinned at its149 * claim; a failure tells it one bit per area, and the suite is committed to150 * in public by a salted SHA-256 Merkle root.151 */152export const sealedChecks = <153 const Areas extends Readonly<Record<string, string>>,154>(config: {155 readonly admit?: {156 readonly greenOnMainRuns?: number;157 readonly redOnKnownBadAtLeast?: number;158 };159 readonly areas: Areas;160 readonly attempts?: {161 readonly perClaim?: number;162 readonly perPersonPerAreaPerWeek?: number;163 };164 readonly canary?: boolean;165 /** The layer that holds the check files. Default `"maintainers"`. */166 readonly layer?: string;167}): SealedChecksDefinition<keyof Areas & string> => ({168 ...sealedChecksOf({169 admit: {170 greenOnMainRuns: config.admit?.greenOnMainRuns ?? 3,171 redOnKnownBadAtLeast: config.admit?.redOnKnownBadAtLeast ?? 1,172 },173 areas: config.areas,174 attempts: {175 perClaim: config.attempts?.perClaim ?? 3,176 perPersonPerAreaPerWeek: config.attempts?.perPersonPerAreaPerWeek ?? 10,177 },178 canary: config.canary ?? false,179 commit: "salted-sha256-merkle",180 layer: config.layer ?? "maintainers",181 pin: "claim",182 result: "area-bit",183 }),184 areas: config.areas,185});186187/** How content moves between audiences, as data. */188export type FlowsDefinition = VisibilityPolicyEncoded["flows"];189190const flowsOf = checked(VisibilityPolicy.fields.flows);191192/** The flow rules. */193export const flows = {194 /**195 * Nothing moves from a layer into a wider audience but a credited196 * release, by the people named. Default release: the deciders.197 */198 noWriteDown: (options?: {199 readonly release?: MembersInput;200 }): FlowsDefinition =>201 flowsOf({ noWriteDown: true, release: options?.release ?? deciders }),202};203204/** A project's `visibility.ts`, as `visibility()` takes it. */205export interface VisibilityConfig<Area extends string = string> {206 /** Held-back checks: `sealedChecks({ areas })`. */207 readonly checks?: SealedChecksDefinition<Area>;208 /** Who may set a write-only env value. Default the deciders. */209 readonly env?: { readonly setters: MembersInput };210 /** Default `flows.noWriteDown()`. */211 readonly flows?: FlowsDefinition;212 /** The project's layers, by name. */213 readonly layers?: Readonly<Record<string, LayerDefinition>>;214 /** How long a change that narrows visibility waits before it lands. Default `"7 days"`. */215 readonly notice?: { readonly narrowing: Days };216}217218/** A visibility file's default export: the policy it states, as data. */219export type VisibilityDefinition<Area extends string = string> = Omit<220 VisibilityPolicyEncoded,221 "checks"222> & { readonly checks?: SealedChecksDefinition<Area> };223224// The held-back checks live in a layer the file declares, one that holds225// checks: a misspelt layer would leave a suite that nothing runs.226const policyOf = checked(227 VisibilityPolicy.check(228 Schema.makeFilter(({ checks, layers }) =>229 checks === undefined ||230 layers.some(231 (each) => each.name === checks.layer && each.holds.includes("checks")232 )233 ? undefined234 : {235 issue: `no declared layer "${checks.layer}" holds checks`,236 path: ["checks", "layer"],237 }238 )239 )240);241242/**243 * Declares a project's visibility: the default export of the config244 * folder's `visibility.ts`. A project without one is Open.245 */246export const visibility = <Area extends string = never>(247 config: VisibilityConfig<Area>248): VisibilityDefinition<Area> => {249 const { checks } = config;250251 const policy: VisibilityPolicyEncoded = {252 env: { setters: config.env?.setters ?? deciders },253 flows: config.flows ?? flows.noWriteDown(),254 layers: Object.entries(config.layers ?? {}).map(([name, each]) => ({255 ...each,256 name,257 })),258 notice: {259 narrowingDays: Duration.toDays(config.notice?.narrowing ?? "7 days"),260 },261 };262263 // Checked whole, so a key the schema doesn't name (a check's path) is264 // dropped. The held-back checks' areas, checked with the rest, come back265 // as the file wrote them, so they keep the names `AreaOf` reads.266 const { checks: suite, ...stated } = policyOf(267 checks === undefined ? policy : { ...policy, checks }268 );269270 return checks === undefined || suite === undefined271 ? stated272 : { ...stated, checks: { ...suite, areas: checks.areas } };273};274275/**276 * The areas a visibility file's (or a `sealedChecks`'s) held-back checks277 * guard, by name: `layerManifest<AreaOf<typeof policy>>(…)` holds each278 * check's area to them.279 */280export type AreaOf<Definition> =281 Definition extends SealedChecksDefinition<infer Area>282 ? Area283 : Definition extends VisibilityDefinition<infer Area>284 ? Area285 : never;286287/** One held-back check as its layer's manifest lists it. */288export type SealedCheckInput<Area extends string = string> = Omit<289 typeof SealedCheck.Encoded,290 "area"291> & { readonly area: Area };292293/** A layer manifest's default export, as data. */294export type LayerManifestDefinition = typeof LayerManifest.Encoded;295296const manifestOf = checked(LayerManifest);297298/**299 * Declares a layer's manifest: the default export of `layer.ts` in the300 * layer repo's config folder. Each held-back check's path, its area, the301 * clause of an ask or a doc it enforces, and its author; and the commits a302 * new check must fail on before it is admitted.303 */304export const layerManifest = <Area extends string>(config: {305 readonly checks: readonly SealedCheckInput<Area>[];306 readonly knownBad?: readonly string[];307 readonly layer: string;308}): LayerManifestDefinition =>309 manifestOf({310 checks: config.checks,311 knownBad: config.knownBad ?? [],312 layer: config.layer,313 });
added:packages/sdk/test/visibility.test.ts+403 added
1import { describe, expect, expectTypeOf, it } from "@effect/vitest";2import {3 LayerManifest,4 VisibilityPolicy,5} from "@repo/protocol/domain/visibility";6import { Schema } from "effect";78import {9 deciders,10 defaults,11 embargo,12 flows,13 layer,14 layerManifest,15 personal,16 sealedChecks,17 visibility,18} from "../src/index.ts";19import type { AreaOf, SealedCheckInput } from "../src/index.ts";2021const decodePolicy = Schema.decodeUnknownSync(VisibilityPolicy);2223const decodeManifest = Schema.decodeUnknownSync(LayerManifest);2425// One held-back check as a layer's manifest lists it.26const exportCheck = {27 area: "export",28 author: "alice",29 clause: {30 quote: "Every export format escapes a title it writes.",31 ref: "export-escapes-titles",32 source: "ask",33 },34 path: "checks/export/escapes-titles.test.ts",35} as const;3637// OSR §4.1's `visibility.ts` as the platform reads it: every default the38// example leaves unsaid is filled, and nothing names a path or a check.39const osrPolicy = {40 checks: {41 admit: { greenOnMainRuns: 3, redOnKnownBadAtLeast: 1 },42 areas: { export: "src/export/**", parser: "src/parser/**" },43 attempts: { perClaim: 3, perPersonPerAreaPerWeek: 10 },44 canary: true,45 commit: "salted-sha256-merkle",46 layer: "maintainers",47 pin: "claim",48 result: "area-bit",49 },50 env: { setters: "deciders" },51 flows: { noWriteDown: true, release: "deciders" },52 layers: [53 {54 audience: "maintainers",55 disclose: "sealed",56 holds: ["checks", "agent-config", "notes", "experiment"],57 name: "maintainers",58 readers: "deciders",59 reason:60 "Held-back checks for parser and export; maintainers' review rubrics",61 sunsetDays: 30,62 writers: "deciders",63 },64 {65 audience: "personal",66 disclose: "hidden",67 holds: ["personal"],68 name: "personal",69 readers: "deciders",70 reason:71 "Each person's own agent and tool configuration, for them and their agents only",72 writers: "deciders",73 },74 {75 audience: "security",76 disclose: "counted",77 holds: ["fix"],78 maxDays: 14,79 name: "security",80 readers: ["alice", "bob"],81 reason: "Embargoed fixes, released with their advisory",82 writers: "deciders",83 },84 ],85 notice: { narrowingDays: 7 },86};8788describe("the config folder's visibility file", () => {89 it("encodes OSR's example to the policy the platform reads", () => {90 const definition = visibility({91 checks: sealedChecks({92 admit: { greenOnMainRuns: 3, redOnKnownBadAtLeast: 1 },93 areas: { export: "src/export/**", parser: "src/parser/**" },94 attempts: { perClaim: 3, perPersonPerAreaPerWeek: 10 },95 canary: true,96 }),97 flows: flows.noWriteDown({ release: deciders }),98 layers: {99 maintainers: layer({100 disclose: "sealed",101 holds: ["checks", "agent-config", "notes", "experiment"],102 readers: deciders,103 reason:104 "Held-back checks for parser and export; maintainers' review rubrics",105 sunset: { experiment: "30 days" },106 writers: deciders,107 }),108 personal: personal(),109 security: embargo({110 disclose: "counted",111 maxDays: 14,112 readers: ["alice", "bob"],113 }),114 },115 notice: { narrowing: "7 days" },116 });117118 expect(definition).toStrictEqual(osrPolicy);119 expect(decodePolicy(definition)).toStrictEqual(osrPolicy);120 });121122 it("fills every default OSR's example spells out", () => {123 const definition = visibility({124 checks: sealedChecks({125 areas: { export: "src/export/**", parser: "src/parser/**" },126 canary: true,127 }),128 layers: {129 maintainers: layer({130 disclose: "sealed",131 holds: ["checks", "agent-config", "notes", "experiment"],132 reason:133 "Held-back checks for parser and export; maintainers' review rubrics",134 sunset: { experiment: "30 days" },135 }),136 personal: personal(),137 security: embargo({138 disclose: "counted",139 maxDays: 14,140 readers: ["alice", "bob"],141 }),142 },143 });144145 expect(definition).toStrictEqual(osrPolicy);146 });147148 it("keeps every value the file states over its default", () => {149 const definition = visibility({150 checks: sealedChecks({151 admit: { greenOnMainRuns: 5, redOnKnownBadAtLeast: 2 },152 areas: { parser: "src/parser/**" },153 attempts: { perClaim: 2, perPersonPerAreaPerWeek: 6 },154 layer: "evidence",155 }),156 env: { setters: ["alice"] },157 flows: flows.noWriteDown({ release: ["bob"] }),158 layers: {159 evidence: layer({160 audience: "team",161 disclose: "declared",162 holds: ["checks", "experiment", "notes"],163 readers: ["alice", "carol"],164 reason: "The team's checks, experiments and notes",165 sunset: { experiment: "2 weeks", notes: "90 days" },166 writers: ["alice"],167 }),168 security: embargo({169 disclose: "counted",170 maxDays: 30,171 readers: ["dave"],172 }),173 },174 notice: { narrowing: "2 weeks" },175 });176177 const expected = {178 checks: {179 admit: { greenOnMainRuns: 5, redOnKnownBadAtLeast: 2 },180 areas: { parser: "src/parser/**" },181 attempts: { perClaim: 2, perPersonPerAreaPerWeek: 6 },182 canary: false,183 commit: "salted-sha256-merkle",184 layer: "evidence",185 pin: "claim",186 result: "area-bit",187 },188 env: { setters: ["alice"] },189 flows: { noWriteDown: true, release: ["bob"] },190 layers: [191 {192 audience: "team",193 disclose: "declared",194 holds: ["checks", "experiment", "notes"],195 name: "evidence",196 readers: ["alice", "carol"],197 reason: "The team's checks, experiments and notes",198 // The protocol keeps one sunset a layer: the soonest stated.199 sunsetDays: 14,200 writers: ["alice"],201 },202 {203 audience: "security",204 disclose: "counted",205 holds: ["fix"],206 maxDays: 30,207 name: "security",208 readers: ["dave"],209 reason: "Embargoed fixes, released with their advisory",210 writers: "deciders",211 },212 ],213 notice: { narrowingDays: 14 },214 };215216 expect(definition).toStrictEqual(expected);217 expect(decodePolicy(definition)).toStrictEqual(expected);218 });219220 it("gives back only what the policy names: a check path stated beside the areas never reaches it", () => {221 const stated = {222 ...sealedChecks({ areas: { export: "src/export/**" } }),223 paths: ["checks/export/escapes-titles.test.ts"],224 };225226 const definition = visibility({227 checks: stated,228 layers: {229 maintainers: layer({230 disclose: "sealed",231 holds: ["checks"],232 reason: "Held-back checks for export",233 }),234 },235 });236237 expect(definition.checks).toStrictEqual(238 sealedChecks({ areas: { export: "src/export/**" } })239 );240 });241242 it("declares an embargo and the personal layer as config alone: no repo, no held-back checks", () => {243 const definition = visibility({244 layers: {245 personal: personal(),246 security: embargo({247 disclose: "counted",248 maxDays: 14,249 readers: ["alice"],250 }),251 },252 });253254 expect(decodePolicy(definition)).toStrictEqual({255 env: { setters: "deciders" },256 flows: { noWriteDown: true, release: "deciders" },257 layers: [258 {259 audience: "personal",260 disclose: "hidden",261 holds: ["personal"],262 name: "personal",263 readers: "deciders",264 reason:265 "Each person's own agent and tool configuration, for them and their agents only",266 writers: "deciders",267 },268 {269 audience: "security",270 disclose: "counted",271 holds: ["fix"],272 maxDays: 14,273 name: "security",274 readers: ["alice"],275 reason: "Embargoed fixes, released with their advisory",276 writers: "deciders",277 },278 ],279 notice: { narrowingDays: 7 },280 });281 });282283 it("writes plain data: JSON carries it whole", () => {284 const definition = visibility({285 checks: sealedChecks({ areas: { export: "src/export/**" } }),286 layers: {287 maintainers: layer({288 disclose: "sealed",289 holds: ["checks"],290 reason: "Held-back checks for export",291 }),292 },293 });294295 // The rules evaluator hands a module's default export back as JSON.296 const carried = JSON.stringify(definition);297298 expect(JSON.parse(carried)).toStrictEqual(definition);299 expect(Object.getOwnPropertySymbols(definition)).toStrictEqual([]);300 });301302 it("fails at the call when the held-back checks' layer is not declared, or holds no checks", () => {303 const checks = sealedChecks({ areas: { export: "src/export/**" } });304305 const notes = layer({306 disclose: "declared",307 holds: ["notes"],308 reason: "Maintainers' notes on asks",309 });310311 expect(() => visibility({ checks, layers: {} })).toThrow(312 /no declared layer "maintainers" holds checks/u313 );314 expect(() =>315 visibility({ checks, layers: { maintainers: notes } })316 ).toThrow(/no declared layer "maintainers" holds checks/u);317 });318319 it("fails at the call with a Schema issue when an area's name is no area name", () => {320 expect(() =>321 sealedChecks({ areas: { "Export Formats": "src/export/**" } })322 ).toThrow(Schema.SchemaError);323 });324325 it("leaves the no-folder defaults without visibility: Open", () => {326 expect(defaults).not.toHaveProperty("visibility");327 });328});329330describe("a layer's manifest", () => {331 it("encodes each held-back check with its area, clause and author", () => {332 const definition = layerManifest({333 checks: [exportCheck],334 knownBad: [],335 layer: "maintainers",336 });337338 const expected = {339 checks: [exportCheck],340 knownBad: [],341 layer: "maintainers",342 };343344 expect(definition).toStrictEqual(expected);345 expect(decodeManifest(definition)).toStrictEqual(expected);346 });347348 it("names no known-bad commit unless told", () => {349 expect(350 layerManifest({ checks: [exportCheck], layer: "maintainers" })351 ).toStrictEqual({352 checks: [exportCheck],353 knownBad: [],354 layer: "maintainers",355 });356 });357});358359// What the maintainer's editor refuses before anything runs.360describe("the builders' types", () => {361 it("holds a manifest's checks to the areas the held-back checks name", () => {362 const checks = sealedChecks({ areas: { export: "src/export/**" } });363364 const policy = visibility({365 checks,366 layers: {367 maintainers: layer({368 disclose: "sealed",369 holds: ["checks"],370 reason: "Held-back checks for export",371 }),372 },373 });374375 expectTypeOf<AreaOf<typeof checks>>().toEqualTypeOf<"export">();376 expectTypeOf<AreaOf<typeof policy>>().toEqualTypeOf<"export">();377378 type Manifest = Parameters<typeof layerManifest<AreaOf<typeof policy>>>[0];379380 expectTypeOf({381 checks: [exportCheck],382 layer: "maintainers",383 } as const).toExtend<Manifest>();384 expectTypeOf({385 checks: [{ ...exportCheck, area: "parser" }],386 layer: "maintainers",387 } as const).not.toExtend<Manifest>();388 });389390 it("requires each held-back check to cite a clause", () => {391 expectTypeOf(exportCheck).toExtend<SealedCheckInput<"export">>();392 expectTypeOf<Omit<typeof exportCheck, "clause">>().not.toExtend<393 SealedCheckInput<"export">394 >();395 });396397 it("takes only the kinds a layer can hold", () => {398 type Holds = Parameters<typeof layer>[0]["holds"];399400 expectTypeOf<readonly ["checks", "notes"]>().toExtend<Holds>();401 expectTypeOf<readonly ["nonsense"]>().not.toExtend<Holds>();402 });403});