Skip to content
Myr

kinds.ts

This file

Path

packages/checks/src/kinds.ts

162 lines · 4.9 KiB · TypeScript

Last changed by

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

History of this file

Take 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–134why, one step back
1import { CheckSpec } from "@repo/protocol/domain/check";
2import type { RatchetSpec } from "@repo/protocol/domain/check";3import { Command } from "@repo/protocol/domain/common";4import type { CheckKind } from "@repo/protocol/domain/kind";5import type { RunBlock } from "@repo/protocol/domain/run";6import { CheckVersion, LaneName } from "@repo/protocol/ids";7import { Option, Predicate } from "effect";8import type { Schema } from "effect";910export const PATH_PLACEHOLDER = "{path}";1112export const checkKindOf = (spec: CheckSpec): CheckKind =>13 CheckSpec.match(spec, {14 LintToZero: (): CheckKind => "lintToZero",15 Migrate: (): CheckKind => "migrate",16 Ratchet: (): CheckKind => "ratchet",17 RedThenGreen: (): CheckKind => "redThenGreen",18 SymbolAbsent: (): CheckKind => "symbolAbsent",19 });2021const shellWord = (path: string) =>22 /^[\w./@+-]+$/u.test(path) ? path : `'${path.replaceAll("'", `'\\''`)}'`;2324const declared = (25 ratchets: readonly RatchetSpec[],26 name: string27): Option.Option<RatchetSpec> =>28 Option.fromNullishOr(ratchets.find((ratchet) => ratchet.name === name));2930export const commandFor = (31 spec: CheckSpec,32 run: RunBlock,33 ratchets: readonly RatchetSpec[] = []34): Option.Option<{ readonly command: Command; readonly lane: LaneName }> =>35 CheckSpec.match(spec, {36 LintToZero: ({ command }) => Option.some(command),37 Migrate: ({ count }) => Option.some(count),38 Ratchet: ({ ratchet }) =>39 Option.map(declared(ratchets, ratchet), ({ measure }) => measure.command),40 RedThenGreen: ({ path }) =>41 run.test.includes(PATH_PLACEHOLDER)42 ? Option.some(43 Command.make(run.test.replaceAll(PATH_PLACEHOLDER, shellWord(path)))44 )45 : Option.none(),46 SymbolAbsent: ({ count }) => Option.some(count),47 }).pipe(48 Option.map((command) => ({49 command,50 lane: LaneName.make(checkKindOf(spec)),51 }))52 );5354const isJsonArray = (value: Schema.Json): value is Schema.JsonArray =>55 Array.isArray(value);5657const isJsonObject = (value: Schema.Json): value is Schema.JsonObject =>58 Predicate.isObject(value);5960// JSON with every object's keys sorted, so equal data hashes equally.61const canonicalJson = (value: Schema.Json): string => {62 if (isJsonArray(value)) {63 return `[${value.map(canonicalJson).join(",")}]`;64 }6566 if (isJsonObject(value)) {67 const entries = Object.keys(value)68 .toSorted()69 .flatMap((key) => {70 const field = value[key];7172 return field === undefined73 ? []74 : [`${JSON.stringify(key)}:${canonicalJson(field)}`];75 });7677 return `{${entries.join(",")}}`;78 }7980 return JSON.stringify(value);81};8283// UTF-8 bytes of a string, without the DOM's TextEncoder: percent-encoding84// writes each byte that isn't a plain ASCII character as `%XX`. The text is85// JSON.stringify's, which never holds a lone surrogate.86const utf8 = (text: string): readonly number[] => {87 const encoded = encodeURIComponent(text);88 const bytes: number[] = [];8990 for (let at = 0; at < encoded.length; at += 1) {91 if (encoded[at] === "%") {92 bytes.push(Number.parseInt(encoded.slice(at + 1, at + 3), 16));93 at += 2;94 } else {95 bytes.push(encoded.codePointAt(at) ?? 0);96 }97 }9899 return bytes;100};101102const FNV_OFFSET = 0xcb_f2_9c_e4_84_22_23_25n;103104const FNV_PRIME = 0x1_00_00_00_01_b3n;105106const TWO_TO_64 = 2n ** 64n;107108// `hash ^ byte`: only the low byte changes, so XOR it bit by bit. Plain109// arithmetic, since the lint config refuses bitwise operators.110const xorLowByte = (hash: bigint, byte: number): bigint => {111 const low = Number(hash % 256n);112 let mixed = 0;113114 for (let bit = 1; bit < 256; bit *= 2) {115 if (Math.floor(low / bit) % 2 !== Math.floor(byte / bit) % 2) {116 mixed += bit;117 }118 }119120 return hash - BigInt(low) + BigInt(mixed);121};122123// FNV-1a, 64-bit, as 16 hex digits. A cache key, not a commitment, so it124// needs no WebCrypto and stays synchronous.125const fnv1a64 = (text: string) => {126 let hash = FNV_OFFSET;127128 for (const byte of utf8(text)) {129 hash = (xorLowByte(hash, byte) * FNV_PRIME) % TWO_TO_64;130 }131132 return hash.toString(16).padStart(16, "0");133};134

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

lines 135–136why, one step back
135// The run hash covers everything the run executes and nothing else; `doctor`
136// judges nothing, so it is left out.

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 137–162why, one step back
137export const checkVersion = (
138 spec: CheckSpec,139 sdkVersion: string,140 run: RunBlock,141 ratchets: readonly RatchetSpec[] = []142): CheckVersion => {143 const runs: Schema.Json = {144 check: spec,145 checks: run.checks,146 command: Option.getOrNull(147 Option.map(commandFor(spec, run, ratchets), ({ command }) => command)148 ),149 image: run.image ?? null,150 measure: CheckSpec.guards.Ratchet(spec)151 ? Option.getOrNull(152 Option.map(declared(ratchets, spec.ratchet), ({ measure }) => measure)153 )154 : null,155 setup: run.setup ?? null,156 setupEgress: run.setupEgress ?? null,157 };158159 return CheckVersion.make(160 `${checkKindOf(spec)}@${sdkVersion}+${fnv1a64(canonicalJson(runs))}`161 );162};