kind.ts
This file
Path
packages/sdk/src/kind.ts
234 lines · 7.5 KiB · TypeScript
Last changed by
Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571
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–60why, one step back1import type { Brief } from "@repo/protocol/domain/ask";2import { AskKind } from "@repo/protocol/domain/kind";3import type {4 BriefField,5 CheckKind,6 ClaimRule,7} from "@repo/protocol/domain/kind";8import type { AskInput } from "@repo/protocol/domain/policy";9import { Territory } from "@repo/protocol/domain/territory";10import { Schema, Struct } from "effect";1112import { check } from "./check.ts";13import type { CheckSpecInput } from "./check.ts";14import { checked } from "./checked.ts";15import { withFunctions } from "./functions.ts";16import type { KindFunctions, OpensAlone, WithFunctions } from "./functions.ts";17import type { TerritoryInput } from "./territory.ts";1819export type AskKindInput = typeof AskKind.Encoded;2021type AskInputEncoded = typeof AskInput.Encoded;2223export type AskStated = Omit<AskInputEncoded, "askedBy" | "file" | "id">;2425export type AskDefinition = AskStated & WithFunctions<KindFunctions>;2627export type AskFields = typeof Brief.Encoded &28 Partial<Pick<AskInputEncoded, "blockedBy">> &29 Pick<AskInputEncoded, "mentor" | "parent" | "reserve">;3031export type TerritoryMode = "derived" | "stated";3233type TerritoryField<Mode extends TerritoryMode> = Mode extends "derived"34 ? { readonly territory?: TerritoryInput }35 : { readonly territory: TerritoryInput };3637export type ArgFields = Readonly<38 Record<PropertyKey, Schema.Constraint & { readonly DecodingServices: never }>39>;4041export type AskArgs<42 Args extends ArgFields,43 Mode extends TerritoryMode = "stated",44> = Schema.Struct.Encoded<Args> & AskFields & TerritoryField<Mode>;4546export type DeriveTerritory<Args extends ArgFields> = (47 args: Schema.Struct.Type<Args>48) => TerritoryInput;4950export interface KindDefinition<51 Args extends ArgFields,52 K extends CheckKind = CheckKind,53> {54 readonly args: Args;55 /** Builds an ask's check from its args; it must be of `checkKind`. */56 readonly check: (57 args: Schema.Struct.Type<Args>58 ) => CheckSpecInput<Capitalize<K>>;59 readonly checkKind: K;60 readonly claimers?: typeof ClaimRule.Encoded;Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571
line 61why, one step back61 /** Whether a maintainer's finish is never offered, optional, or required before landing. */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 62–65why, one step back62 readonly finish?: AskKindInput["finish"];63 readonly name: string;64 /** Answers can't be undone by a later answer: judge changes, dependency changes. */65 readonly oneWay?: boolean;Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571
line 66why, one step back66 /** Whether a proposal for an ask of this kind opens without a maintainer. */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 67–83why, one step back67 readonly opensAlone?: OpensAlone;68 /** How many claims an ask takes at once; 1 is exclusive. */69 readonly race?: number;70 readonly requires?: readonly BriefField[];71 /** How strong an ask's check must be: failing on main by an assertion, or also failing a null answer. */72 readonly strength?: AskKindInput["strength"];73 readonly territory?: DeriveTerritory<Args>;74}7576export interface DeclaredKind extends WithFunctions<KindFunctions> {77 readonly askKind: AskKindInput;78}7980export interface KindHandle<81 Args extends ArgFields,82 Mode extends TerritoryMode = "stated",83> extends DeclaredKind {Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
line 84why, one step back84 /** An ask of this kind; the args are checked at the call, so a malformed ask fails with a Schema issue. */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 85–105why, one step back85 readonly ask: (input: AskArgs<Args, Mode>) => AskDefinition;86}8788const askKindOf = checked(AskKind);8990const territoryOf = Schema.decodeUnknownSync(Territory);9192const encodeTerritory = Schema.encodeSync(Territory);9394export interface KindBuilder {95 <const Args extends ArgFields, const K extends CheckKind>(96 definition: KindDefinition<Args, K> & {97 readonly territory: DeriveTerritory<Args>;98 }99 ): KindHandle<Args, "derived">;100 <const Args extends ArgFields, const K extends CheckKind>(101 definition: KindDefinition<Args, K>102 ): KindHandle<Args>;103}104105/**Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
lines 106–108why, one step back106 * Declares an ask kind, checked at the call: `kind({ name: "fixBug", args: {107 * repro: Schema.NonEmptyString }, checkKind: "redThenGreen", check: ({ repro })108 * => check.redThenGreen(repro) })`.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 109–187why, one step back109 */110export const kind: KindBuilder = <111 const Args extends ArgFields,112 const K extends CheckKind,113>(114 definition: KindDefinition<Args, K>115): KindHandle<Args, TerritoryMode> => {116 const decodeArgs = Schema.decodeSync(Schema.Struct(definition.args));117 const functions: KindFunctions = { opensAlone: definition.opensAlone };118119 const ask = (input: AskArgs<Args, TerritoryMode>): AskDefinition => {120 const args = decodeArgs(input);121 const fields: AskFields & { readonly territory?: TerritoryInput } = input;122123 const stated: AskStated = {124 ...Struct.pick(fields, ["mentor", "parent", "reserve"]),125 blockedBy: fields.blockedBy ?? [],126 brief: Struct.pick(fields, [127 "context",128 "current",129 "outOfScope",130 "terms",131 "want",132 ]),133 check: definition.check(args),134 kind: definition.name,135 // A territory the ask leaves out comes from its kind; with neither,136 // this fails at the call, as malformed args do.137 territory: encodeTerritory(138 territoryOf(fields.territory ?? definition.territory?.(args))139 ),140 };141142 return withFunctions(stated, functions);143 };144145 return withFunctions(146 {147 ask,148 askKind: askKindOf({149 checkKind: definition.checkKind,150 claimers: definition.claimers ?? "vouched",151 finish: definition.finish ?? "optional",152 name: definition.name,153 oneWay: definition.oneWay ?? false,154 race: definition.race ?? 1,155 requires: definition.requires ?? ["want", "check", "territory"],156 strength: definition.strength ?? "assertion",157 }),158 },159 functions160 );161};162163export interface AskOrigin {164 readonly askedBy: string;165 readonly file: string;166}167168const askSuffix = ".ask.ts";169170const askIdOf = (file: string): string => {171 const name = file.slice(file.lastIndexOf("/") + 1);172173 return name.endsWith(askSuffix) ? name.slice(0, -askSuffix.length) : name;174};175176export const toAskInput = (177 definition: AskDefinition,178 origin: AskOrigin179): AskInputEncoded => ({180 ...definition,181 askedBy: origin.askedBy,182 file: origin.file,183 id: askIdOf(origin.file),184});185186const path = { path: Schema.NonEmptyString };187Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
line 188why, one step back188/** The ask kinds a project gets without declaring any; list the ones it uses in `project({ kinds })`. */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 189why, one step back189export const kinds = {Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571
line 190why, one step back190 /** Changes what judges answers (a check, lint or CI file): one-way, and a maintainer finishes every answer. */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 191–198why, one step back191 changeJudge: kind({192 args: path,193 check: (args) => check.redThenGreen(args.path),194 checkKind: "redThenGreen",195 finish: "required",196 name: "changeJudge",197 oneWay: true,198 }),Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
line 199why, one step back199 /** A sentence and a check file that fails on main by an assertion; an answer must pass 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 200–205why, one step back200 fix: kind({201 args: path,202 check: (args) => check.redThenGreen(args.path),203 checkKind: "redThenGreen",204 name: "fix",205 }),Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
line 206why, one step back206 /** A lint rule's count in the territory goes to zero. Never finished. */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–213why, one step back207 lintToZero: kind({208 args: { command: Schema.NonEmptyString, rule: Schema.NonEmptyString },209 check: (args) => check.lintToZero(args.rule, args.command),210 checkKind: "lintToZero",211 finish: "never",212 name: "lintToZero",213 }),Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
line 214why, one step back214 /** Every use of one name moves to another, package by package. Never finished. */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–234why, one step back215 migrate: kind({216 args: {217 count: Schema.NonEmptyString,218 from: Schema.NonEmptyString,219 to: Schema.optionalKey(Schema.NonEmptyString),220 },221 check: (args) => check.migrate(args),222 checkKind: "migrate",223 finish: "never",224 name: "migrate",225 }),226 /** A named ratchet goes down. Never finished. */227 ratchetDown: kind({228 args: { ratchet: Schema.NonEmptyString },229 check: (args) => check.ratchet.down(args.ratchet),230 checkKind: "ratchet",231 finish: "never",232 name: "ratchetDown",233 }),234};