Skip to content
Myr

notes.ts

This file

Path

apps/platform/src/workflows/notes.ts

178 lines · 4.9 KiB · TypeScript

Last changed by

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

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–58why, one step back
1import { HostBusy, RefOutcome, RepoHost } from "@repo/artifacts/repo-host";
2import type {3 ObjectId,4 RepoName,5 RepoNotFound,6 TreeEntry,7} from "@repo/artifacts/repo-host";8import { noteCommit, notesRef } from "@repo/git/notes";9import type { ScratchSource } from "@repo/git/notes";10import { Verdict } from "@repo/protocol/domain/answer";11import type { CommitSha } from "@repo/protocol/ids";12import { DateTime, Effect, Option, Schema } from "effect";1314export { notesRef } from "@repo/git/notes";1516export interface NoteWritten {17 readonly notesTip: CommitSha;18 readonly changed: boolean;19}2021const encoder = new TextEncoder();2223const encodeVerdict = Schema.encodeEffect(Schema.fromJsonString(Verdict));2425const noteBytes = (verdict: Verdict) =>26 Effect.map(Effect.orDie(encodeVerdict(verdict)), (json) =>27 encoder.encode(`${json}\n`)28 );2930const framed = (bytes: Uint8Array) => {31 const header = encoder.encode(`blob ${bytes.byteLength}\0`);32 const raw = new Uint8Array(header.byteLength + bytes.byteLength);3334 raw.set(header);35 raw.set(bytes, header.byteLength);3637 return raw;38};3940const blobId = (bytes: Uint8Array) =>41 Effect.map(42 Effect.promise(() => crypto.subtle.digest("SHA-1", framed(bytes))),43 (digest) =>44 [...new Uint8Array(digest)]45 .map((byte) => byte.toString(16).padStart(2, "0"))46 .join("")47 );4849const deflate = (bytes: Uint8Array) =>50 Effect.map(51 Effect.promise(() =>52 new Response(53 new Blob([bytes]).stream().pipeThrough(new CompressionStream("deflate"))54 ).arrayBuffer()55 ),56 (buffer) => new Uint8Array(buffer)57 );58

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

lines 59–61why, one step back
59// A push over smart HTTP sends every object the new notes tree names that it cannot
60// tell the remote has, and the source holds no earlier notes commit, so it must hold61// their blobs.

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–178why, one step back
62const keepEarlierNote = (
63 fork: RepoName,64 source: ScratchSource,65 blob: ObjectId66) =>67 Effect.gen(function* () {68 const bytes = yield* RepoHost.use((host) => host.readBlob(fork, blob)).pipe(69 Effect.flatMap(70 Option.match({71 onNone: () => Effect.die(`${fork} has no note blob ${blob}`),72 onSome: Effect.succeed,73 })74 )75 );7677 const dir = `${source.gitdir}/objects/${blob.slice(0, 2)}`;7879 const loose = yield* deflate(framed(bytes));8081 // Another note's blob may have made the folder already.82 yield* Effect.ignore(83 Effect.tryPromise(() => source.fs.promises.mkdir(dir))84 );85 yield* Effect.promise(() =>86 source.fs.promises.writeFile(`${dir}/${blob.slice(2)}`, loose)87 );88 });8990const notesAt = (fork: RepoName, tip: Option.Option<CommitSha>) =>91 Option.match(tip, {92 onNone: () => Effect.succeed<readonly TreeEntry[]>([]),93 onSome: (sha) =>94 Effect.gen(function* () {95 const host = yield* RepoHost;96 const commit = yield* host.readCommit(fork, sha);9798 if (Option.isNone(commit)) {99 return yield* Effect.die(`${fork} has no notes commit ${sha}`);100 }101102 const tree = yield* host.readTree(fork, commit.value.tree);103104 if (Option.isNone(tree)) {105 return yield* Effect.die(`${fork} has no tree ${commit.value.tree}`);106 }107108 return tree.value;109 }),110 });111112interface NoteInput {113 readonly fork: RepoName;114 readonly head: CommitSha;115 readonly verdict: Verdict;116}117118const once = (input: NoteInput) =>119 Effect.gen(function* () {120 const host = yield* RepoHost;121122 const tip = Option.fromNullishOr(123 (yield* host.refs(input.fork)).get(notesRef)124 );125126 const earlier = yield* notesAt(input.fork, tip);127 const note = yield* blobId(yield* noteBytes(input.verdict));128129 if (130 Option.isSome(tip) &&131 earlier.some(({ name, sha }) => name === input.head && sha === note)132 ) {133 return Option.some<NoteWritten>({ changed: false, notesTip: tip.value });134 }135136 const { commit, source } = yield* noteCommit({137 notesParent: tip,138 now: yield* DateTime.now,139 repo: input.fork,140 target: input.head,141 verdict: input.verdict,142 });143144 yield* Effect.forEach(145 earlier.filter(146 ({ name, type }) => name !== input.head && type === "blob"147 ),148 ({ sha }) => keepEarlierNote(input.fork, source, sha),149 { discard: true }150 );151152 const [outcome] = yield* host.push(input.fork, source, [153 { from: tip, ref: notesRef, to: commit },154 ]);155156 return outcome !== undefined && RefOutcome.$is("Updated")(outcome)157 ? Option.some<NoteWritten>({ changed: true, notesTip: commit })158 : Option.none();159 });160161export const writeVerdictNote = (162 input: NoteInput163): Effect.Effect<NoteWritten, RepoNotFound | HostBusy, RepoHost> =>164 Effect.gen(function* () {165 const first = yield* once(input);166167 if (Option.isSome(first)) {168 return first.value;169 }170171 const second = yield* once(input);172173 if (Option.isSome(second)) {174 return second.value;175 }176177 return yield* new HostBusy({ name: input.fork });178 });