Skip to content
Myr

Let a maintainer declare layers, held-back checks and value setters in visibility.ts with the sdk

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

- packages/sdk/src/visibility.ts: visibility, layer, embargo, personal, sealedChecks, flows.noWriteDown, deciders and layerManifest. Each builder checks its input against protocol's schema at the call (checked) and returns plain data: visibility() encodes to VisibilityPolicy, layerManifest() to LayerManifest. No function rides in the output, and admit is data (greenOnMainRuns, redOnKnownBadAtLeast), not a callback. - Defaults filled by the sdk: readers and writers "deciders", attempts 3 and 10, admit 3 and 1, notice "7 days", env setters "deciders", flows noWriteDown with the deciders releasing, layer "maintainers", canary false (opt-in: it promises a canary line in each sealed file). result, pin and commit are fixed literals ("area-bit", "claim", "salted-sha256-merkle"). defaults (the no-folder policy) is untouched. - The public file names areas, kinds and rules only; check paths, names, clauses and authors live in the layer's manifest (OSR 4.1). - Types: holds takes only LayerKind; sunset's keys only the kinds the layer holds; a manifest check without a clause is a type error; SealedChecksDefinition<Area> keeps the area names, and layerManifest<AreaOf<typeof policy>>(...) refuses an area the held-back checks don't name. - layer() takes an optional audience (default "maintainers"): the wire requires one. embargo() writes audience security, holds ["fix"], a fixed reason; personal() writes audience personal, disclose hidden, readers and writers "deciders" (Members has no owner form; D14: config only). - sunset is per kind but protocol keeps one sunsetDays a layer: the sdk writes the soonest given. - visibility() refuses at the call a held-back suite whose layer is not declared or holds no checks (a Schema filter), so a misspelt layer can't leave a suite that nothing runs. - apps/cli/src/entries/rules.ts names the new exports (outside the territory, as the issue's Sources ask; build.test.ts's parity case needs it). - Tests: packages/sdk/test/visibility.test.ts (12): OSR 4.1's example equals the expected policy and decodes; the same with every default left out; embargo and personal alone (no repo, no held-back checks); plain data through JSON with no symbol keys; the undeclared-layer refusal; an area that is no area name; defaults without visibility; the manifest decodes, knownBad defaults to []; three expectTypeOf cases (area, clause, holds), each seen failing typecheck against a planted bug. - ASSUMPTION: OSR's embargo readers (P.team, P.list) are written as handles, as 601's fixture does; reader groups, reveal and watchGap are not in protocol and not built. - Not wired: nothing reads .myr/visibility.ts yet (packages/rules); the needed change is in the issue's Comments for the integrator.

authored by oscabriel · committed by oscabriel · parent 9031e57 · Browse files · 4 files+659 added−1 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+659 added−1 removed

  • tests:+309 added−0 removed
  • other:+350 added−1 removed

4 files+659 added−1 removed

modified:apps/cli/src/entries/rules.ts+20 added-1 removed
modified:packages/sdk/src/index.ts+22 added
added:packages/sdk/src/visibility.ts+308 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 /** How long each kind it holds may stay unreleased: `{ experiment: "30 days" }`. */61 readonly sunset?: Readonly<Partial<Record<Holds[number], Days>>>;62 /** Who may push to it. */63 readonly writers?: MembersInput;64}6566const LayerFields = Schema.Struct(Struct.omit(Layer.fields, ["name", "repo"]));6768const layerOf = checked(LayerFields);6970// The protocol keeps one sunset a layer: the soonest any kind it holds is due.71const sunsetOf = (sunset: Readonly<Partial<Record<string, Days>>>) => {72 const days = Object.values(sunset)73 .filter(Predicate.isNotUndefined)74 .map((each) => Duration.toDays(each));7576 return days.length === 0 ? {} : { sunsetDays: Math.min(...days) };77};7879/**80 * Declares a layer: a separate repo holding one audience's files, overlaid81 * on main only where that audience may read. Its paths stay in the layer.82 */83export const layer = <const Holds extends LayerHolds>(84 config: LayerConfig<Holds>85): LayerDefinition =>86 layerOf({87 audience: config.audience ?? "maintainers",88 disclose: config.disclose,89 holds: config.holds,90 readers: config.readers ?? deciders,91 reason: config.reason,92 writers: config.writers ?? deciders,93 ...sunsetOf(config.sunset ?? {}),94 });9596/**97 * Declares an embargo: fixes held for the named readers, released within98 * `maxDays` and counted in public. Config only: nothing releases it yet.99 */100export const embargo = (config: {101 readonly disclose: "counted";102 readonly maxDays: number;103 readonly readers: MembersInput;104}): LayerDefinition =>105 layerOf({106 audience: "security",107 disclose: config.disclose,108 holds: ["fix"],109 maxDays: config.maxDays,110 readers: config.readers,111 reason: "Embargoed fixes, released with their advisory",112 writers: deciders,113 });114115/**116 * Declares the personal layer: each person's own agent and tool117 * configuration, hidden, gating nothing. Config only.118 */119export const personal = (): LayerDefinition =>120 layerOf({121 audience: "personal",122 disclose: "hidden",123 holds: ["personal"],124 readers: deciders,125 reason:126 "Each person's own agent and tool configuration, for them and their agents only",127 writers: deciders,128 });129130/**131 * Held-back checks as the public file states them: their areas by name and132 * code glob, never a check's path or name. `Area` is the areas' names, so a133 * layer's manifest can be held to them (`AreaOf`).134 */135export type SealedChecksDefinition<Area extends string = string> = Omit<136 SealedChecksEncoded,137 "areas"138> & { readonly areas: Readonly<Record<Area, string>> };139140const sealedChecksOf = checked(SealedChecks);141142/**143 * Declares the held-back checks: the layer holding them, each area's name144 * and the code it guards, how often an answer may fail them, and when a new145 * check joins the suite. An answer is judged by the suite pinned at its146 * claim; a failure tells it one bit per area, and the suite is committed to147 * in public by a salted SHA-256 Merkle root.148 */149export const sealedChecks = <150 const Areas extends Readonly<Record<string, string>>,151>(config: {152 readonly admit?: {153 readonly greenOnMainRuns?: number;154 readonly redOnKnownBadAtLeast?: number;155 };156 readonly areas: Areas;157 readonly attempts?: {158 readonly perClaim?: number;159 readonly perPersonPerAreaPerWeek?: number;160 };161 readonly canary?: boolean;162 /** The layer that holds the check files. Default `"maintainers"`. */163 readonly layer?: string;164}): SealedChecksDefinition<keyof Areas & string> => ({165 ...sealedChecksOf({166 admit: {167 greenOnMainRuns: config.admit?.greenOnMainRuns ?? 3,168 redOnKnownBadAtLeast: config.admit?.redOnKnownBadAtLeast ?? 1,169 },170 areas: config.areas,171 attempts: {172 perClaim: config.attempts?.perClaim ?? 3,173 perPersonPerAreaPerWeek: config.attempts?.perPersonPerAreaPerWeek ?? 10,174 },175 canary: config.canary ?? false,176 commit: "salted-sha256-merkle",177 layer: config.layer ?? "maintainers",178 pin: "claim",179 result: "area-bit",180 }),181 areas: config.areas,182});183184/** How content moves between audiences, as data. */185export type FlowsDefinition = VisibilityPolicyEncoded["flows"];186187const flowsOf = checked(VisibilityPolicy.fields.flows);188189/** The flow rules. */190export const flows = {191 /**192 * Nothing moves from a layer into a wider audience but a credited193 * release, by the people named. Default release: the deciders.194 */195 noWriteDown: (options?: {196 readonly release?: MembersInput;197 }): FlowsDefinition =>198 flowsOf({ noWriteDown: true, release: options?.release ?? deciders }),199};200201/** A project's `visibility.ts`, as `visibility()` takes it. */202export interface VisibilityConfig<Area extends string = string> {203 /** Held-back checks: `sealedChecks({ areas })`. */204 readonly checks?: SealedChecksDefinition<Area>;205 /** Who may set a write-only env value. Default the deciders. */206 readonly env?: { readonly setters: MembersInput };207 /** Default `flows.noWriteDown()`. */208 readonly flows?: FlowsDefinition;209 /** The project's layers, by name. */210 readonly layers?: Readonly<Record<string, LayerDefinition>>;211 /** How long a change that narrows visibility waits before it lands. Default `"7 days"`. */212 readonly notice?: { readonly narrowing: Days };213}214215/** A visibility file's default export: the policy it states, as data. */216export type VisibilityDefinition<Area extends string = string> = Omit<217 VisibilityPolicyEncoded,218 "checks"219> & { readonly checks?: SealedChecksDefinition<Area> };220221// The held-back checks live in a layer the file declares, one that holds222// checks: a misspelt layer would leave a suite that nothing runs.223const policyOf = checked(224 VisibilityPolicy.check(225 Schema.makeFilter(({ checks, layers }) =>226 checks === undefined ||227 layers.some(228 (each) => each.name === checks.layer && each.holds.includes("checks")229 )230 ? undefined231 : {232 issue: `no declared layer "${checks.layer}" holds checks`,233 path: ["checks", "layer"],234 }235 )236 )237);238239/**240 * Declares a project's visibility: the default export of the config241 * folder's `visibility.ts`. A project without one is Open.242 */243export const visibility = <Area extends string = never>(244 config: VisibilityConfig<Area>245): VisibilityDefinition<Area> => {246 const { checks } = config;247248 const policy: VisibilityPolicyEncoded = {249 env: { setters: config.env?.setters ?? deciders },250 flows: config.flows ?? flows.noWriteDown(),251 layers: Object.entries(config.layers ?? {}).map(([name, each]) => ({252 ...each,253 name,254 })),255 notice: {256 narrowingDays: Duration.toDays(config.notice?.narrowing ?? "7 days"),257 },258 };259260 // Checked whole, then the held-back checks given back as stated, so261 // their areas keep the names `AreaOf` reads.262 const stated = Struct.omit(263 policyOf(checks === undefined ? policy : { ...policy, checks }),264 ["checks"]265 );266267 return checks === undefined ? stated : { ...stated, checks };268};269270/**271 * The areas a visibility file's (or a `sealedChecks`'s) held-back checks272 * guard, by name: `layerManifest<AreaOf<typeof policy>>(…)` holds each273 * check's area to them.274 */275export type AreaOf<Definition> =276 Definition extends SealedChecksDefinition<infer Area>277 ? Area278 : Definition extends VisibilityDefinition<infer Area>279 ? Area280 : never;281282/** One held-back check as its layer's manifest lists it. */283export type SealedCheckInput<Area extends string = string> = Omit<284 typeof SealedCheck.Encoded,285 "area"286> & { readonly area: Area };287288/** A layer manifest's default export, as data. */289export type LayerManifestDefinition = typeof LayerManifest.Encoded;290291const manifestOf = checked(LayerManifest);292293/**294 * Declares a layer's manifest: the default export of `layer.ts` in the295 * layer repo's config folder. Each held-back check's path, its area, the296 * clause of an ask or a doc it enforces, and its author; and the commits a297 * new check must fail on before it is admitted.298 */299export const layerManifest = <Area extends string>(config: {300 readonly checks: readonly SealedCheckInput<Area>[];301 readonly knownBad?: readonly string[];302 readonly layer: string;303}): LayerManifestDefinition =>304 manifestOf({305 checks: config.checks,306 knownBad: config.knownBad ?? [],307 layer: config.layer,308 });
added:packages/sdk/test/visibility.test.ts+309 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("declares an embargo and the personal layer as config alone: no repo, no held-back checks", () => {149 const definition = visibility({150 layers: {151 personal: personal(),152 security: embargo({153 disclose: "counted",154 maxDays: 14,155 readers: ["alice"],156 }),157 },158 });159160 expect(decodePolicy(definition)).toStrictEqual({161 env: { setters: "deciders" },162 flows: { noWriteDown: true, release: "deciders" },163 layers: [164 {165 audience: "personal",166 disclose: "hidden",167 holds: ["personal"],168 name: "personal",169 readers: "deciders",170 reason:171 "Each person's own agent and tool configuration, for them and their agents only",172 writers: "deciders",173 },174 {175 audience: "security",176 disclose: "counted",177 holds: ["fix"],178 maxDays: 14,179 name: "security",180 readers: ["alice"],181 reason: "Embargoed fixes, released with their advisory",182 writers: "deciders",183 },184 ],185 notice: { narrowingDays: 7 },186 });187 });188189 it("writes plain data: JSON carries it whole", () => {190 const definition = visibility({191 checks: sealedChecks({ areas: { export: "src/export/**" } }),192 layers: {193 maintainers: layer({194 disclose: "sealed",195 holds: ["checks"],196 reason: "Held-back checks for export",197 }),198 },199 });200201 // The rules evaluator hands a module's default export back as JSON.202 const carried = JSON.stringify(definition);203204 expect(JSON.parse(carried)).toStrictEqual(definition);205 expect(Object.getOwnPropertySymbols(definition)).toStrictEqual([]);206 });207208 it("fails at the call when the held-back checks' layer is not declared, or holds no checks", () => {209 const checks = sealedChecks({ areas: { export: "src/export/**" } });210211 const notes = layer({212 disclose: "declared",213 holds: ["notes"],214 reason: "Maintainers' notes on asks",215 });216217 expect(() => visibility({ checks, layers: {} })).toThrow(218 /no declared layer "maintainers" holds checks/u219 );220 expect(() =>221 visibility({ checks, layers: { maintainers: notes } })222 ).toThrow(/no declared layer "maintainers" holds checks/u);223 });224225 it("fails at the call with a Schema issue when an area's name is no area name", () => {226 expect(() =>227 sealedChecks({ areas: { "Export Formats": "src/export/**" } })228 ).toThrow(Schema.SchemaError);229 });230231 it("leaves the no-folder defaults without visibility: Open", () => {232 expect(defaults).not.toHaveProperty("visibility");233 });234});235236describe("a layer's manifest", () => {237 it("encodes each held-back check with its area, clause and author", () => {238 const definition = layerManifest({239 checks: [exportCheck],240 knownBad: [],241 layer: "maintainers",242 });243244 const expected = {245 checks: [exportCheck],246 knownBad: [],247 layer: "maintainers",248 };249250 expect(definition).toStrictEqual(expected);251 expect(decodeManifest(definition)).toStrictEqual(expected);252 });253254 it("names no known-bad commit unless told", () => {255 expect(256 layerManifest({ checks: [exportCheck], layer: "maintainers" })257 ).toStrictEqual({258 checks: [exportCheck],259 knownBad: [],260 layer: "maintainers",261 });262 });263});264265// What the maintainer's editor refuses before anything runs.266describe("the builders' types", () => {267 it("holds a manifest's checks to the areas the held-back checks name", () => {268 const checks = sealedChecks({ areas: { export: "src/export/**" } });269270 const policy = visibility({271 checks,272 layers: {273 maintainers: layer({274 disclose: "sealed",275 holds: ["checks"],276 reason: "Held-back checks for export",277 }),278 },279 });280281 expectTypeOf<AreaOf<typeof checks>>().toEqualTypeOf<"export">();282 expectTypeOf<AreaOf<typeof policy>>().toEqualTypeOf<"export">();283284 type Manifest = Parameters<typeof layerManifest<AreaOf<typeof policy>>>[0];285286 expectTypeOf({287 checks: [exportCheck],288 layer: "maintainers",289 } as const).toExtend<Manifest>();290 expectTypeOf({291 checks: [{ ...exportCheck, area: "parser" }],292 layer: "maintainers",293 } as const).not.toExtend<Manifest>();294 });295296 it("requires each held-back check to cite a clause", () => {297 expectTypeOf(exportCheck).toExtend<SealedCheckInput<"export">>();298 expectTypeOf<Omit<typeof exportCheck, "clause">>().not.toExtend<299 SealedCheckInput<"export">300 >();301 });302303 it("takes only the kinds a layer can hold", () => {304 type Holds = Parameters<typeof layer>[0]["holds"];305306 expectTypeOf<readonly ["checks", "notes"]>().toExtend<Holds>();307 expectTypeOf<readonly ["nonsense"]>().not.toExtend<Holds>();308 });309});