commitment.ts
This file
Path
packages/visibility/src/commitment.ts
155 lines · 4.7 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
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–81why, one step back1import type { HeldBackResult } from "@repo/protocol/domain/answer";2import type { AreaName, BlobSha, RepoPath } from "@repo/protocol/ids";3import { SuiteRoot } from "@repo/protocol/ids";4import { Array, Effect, Option, Order } from "effect";56export interface SuiteFile {7 readonly path: RepoPath;8 readonly area: AreaName;9 readonly blob: BlobSha;10}1112const LEAF_SEPARATOR = Uint8Array.of(0x00);1314const NODE = Uint8Array.of(0x01);1516const AREA = Uint8Array.of(0x02);1718const utf8 = (text: string) => new TextEncoder().encode(text);1920const sha256 = (...parts: readonly Uint8Array[]) =>21 Effect.promise(() =>22 crypto.subtle.digest(23 "SHA-256",24 Uint8Array.from(parts.flatMap((part) => [...part]))25 )26 ).pipe(Effect.map((digest) => new Uint8Array(digest)));2728const rootOf = (digest: Uint8Array) =>29 SuiteRoot.make(30 `sha256:${[...digest].map((byte) => byte.toString(16).padStart(2, "0")).join("")}`31 );3233/**34 * The Merkle root of the nodes in order: pairs hashed as `H(0x01 ‖ left ‖35 * right)`, an odd one out promoted unchanged, level by level; one node is36 * its own root, and none is the hash of nothing.37 */38const merkle = (level: readonly Uint8Array[]): Effect.Effect<Uint8Array> =>39 Array.match(level, {40 onEmpty: () => sha256(),41 onNonEmpty: ([first, ...rest]) =>42 rest.length === 043 ? Effect.succeed(first)44 : Effect.forEach(Array.chunksOf(level, 2), ([left, right]) =>45 right === undefined46 ? Effect.succeed(left)47 : sha256(NODE, left, right)48 ).pipe(Effect.flatMap(merkle)),49 });5051const byPath = Order.combine(52 Order.mapInput(Order.String, (file: SuiteFile) => file.path),53 Order.mapInput(Order.String, (file: SuiteFile) => file.blob)54);5556/** A file's leaf: `H(salt ‖ 0x00 ‖ path ‖ 0x00 ‖ blob id)`, the id as its 40 hex. */57const leaf = (salt: Uint8Array, file: SuiteFile) =>58 sha256(59 salt,60 LEAF_SEPARATOR,61 utf8(file.path),62 LEAF_SEPARATOR,63 utf8(file.blob)64 );6566const roots = (salt: Uint8Array, files: readonly SuiteFile[]) =>67 Effect.forEach(68 Array.sort(new Set(files.map((file) => file.area)), Order.String),69 (area) =>70 Effect.forEach(71 Array.sort(72 files.filter((file) => file.area === area),73 byPath74 ),75 (file) => leaf(salt, file)76 ).pipe(77 Effect.flatMap(merkle),78 Effect.map((root) => [area, root] as const)79 )80 );81Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
line 82why, one step back82/** Without the salt, nobody can test a guess at a check against 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 83–92why, one step back83export const areaRoots = (84 salt: Uint8Array,85 files: readonly SuiteFile[]86): Effect.Effect<ReadonlyMap<AreaName, SuiteRoot>> =>87 roots(salt, files).pipe(88 Effect.map(89 (areas) => new Map(areas.map(([area, root]) => [area, rootOf(root)]))90 )91 );92Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
line 93why, one step back93/** `H(0x02 ‖ area ‖ 0x00 ‖ area root)` per area, sorted: the public sees the suite changed and learns nothing of what it holds. */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 94–155why, one step back94export const suiteRoot = (95 salt: Uint8Array,96 files: readonly SuiteFile[]97): Effect.Effect<SuiteRoot> =>98 roots(salt, files).pipe(99 Effect.flatMap((areas) =>100 Effect.forEach(areas, ([area, root]) =>101 sha256(AREA, utf8(area), LEAF_SEPARATOR, root)102 )103 ),104 Effect.flatMap(merkle),105 Effect.map(rootOf)106 );107108const byArea = Order.combine(109 Order.mapInput(Order.String, (result: HeldBackResult) => result.area),110 Order.combine(111 Order.mapInput(Order.String, (result: HeldBackResult) => result.suite),112 Order.combine(113 Order.mapInput(Order.Number, (result: HeldBackResult) => result.passed),114 Order.mapInput(Order.Number, (result: HeldBackResult) => result.total)115 )116 )117);118119export const heldBackValue = (input: {120 readonly results: readonly HeldBackResult[];121 readonly areas: readonly AreaName[];122}): Option.Option<string> => {123 const ran = new Set<AreaName>(input.results.map((result) => result.area));124125 const lines = [126 ...Array.sort(input.results, byArea).map(127 ({ area, passed, suite, total }) =>128 [area, `${area} ${passed}/${total} @ ${suite}`] as const129 ),130 ...input.areas131 .filter((area) => !ran.has(area))132 .map((area) => [area, `${area} n/a`] as const),133 ];134135 // The sort is stable, so one area's several results keep `byArea`'s order.136 return ran.size === 0137 ? Option.none()138 : Option.some(139 Array.dedupe(140 Array.sort(141 lines,142 Order.mapInput(143 Order.String,144 ([area]: (typeof lines)[number]) => area145 )146 ).map(([, line]) => line)147 ).join("; ")148 );149};150151export const heldBackTrailer = (input: {152 readonly results: readonly HeldBackResult[];153 readonly areas: readonly AreaName[];154}): Option.Option<string> =>155 Option.map(heldBackValue(input), (value) => `Held-back: ${value}`);