Skip to content
Myr

Hold a verdict note's push to the notes ref alone, and retry a busy fork, with a test each

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

Review of W4 407 against its issue (Spec) and AGENTS.md and spec §7 (Standards). - Test added, apps/platform/test/notes.test.ts: "pushes the notes ref alone, never forced: a compare-and-swap from the tip it listed". It records each push `writeVerdictNote` makes and expects exactly one update, on the notes ref, from the tip it listed (`None` for the first note). It holds the Done-when's "CAS on the notes ref only", which no test held before. It goes red by assertion when a branch is planted in the push. - Test added: "a fork busy with another push is asked again, and the note is written". It drives `runAnswer` over the W3 memory world, with the fork's first notes push failing `HostBusy`. It expects two pushes and the note equal to the project's verdict. It goes red by assertion with `Effect.retry(noteRetry)` removed. The run races `Effect.forever(TestClock.adjust(...))`, so the backoff passes on the TestClock, and the test fails rather than hangs when the retry is gone. - notes.ts: the git blob framing (`blob <n>\0` + bytes) was built twice, once for the blob id and once for the loose object, each time by spreading the bytes into a number array. Both now use one `framed` helper. - Not changed, recorded in the issue's Comments for 499: notes.ts repeats `@repo/git/notes`' `notesAt`, `deflate` and verdict encoding, and reads the notes tree twice per write. `noteCommit` keeping the earlier blobs would remove both, and it fixes the isogit Lander's canonical note over `/binding` (217 #7). That change belongs to packages/git, outside 407's territory. - bun run fix && bun run verify green: platform tests 257; bytes platform 1,516,272 of 1,665,000.

authored by oscabriel · committed by oscabriel · parent ad9885e · Browse files · 2 files+106 added−22 removed
This commit

State

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

Diff stats

2 files+106 added−22 removed

  • tests:+93 added−7 removed
  • other:+13 added−15 removed

2 files+106 added−22 removed

modified:apps/platform/src/workflows/notes.ts+13 added-15 removed
46 unmodified lines
47 Effect.map(Effect.orDie(encodeVerdict(verdict)), (json) =>48 encoder.encode(`${json}\n`)49 );5051/** A blob holding `bytes` as git frames it: its header, then its content. */52const framed = (bytes: Uint8Array) => {53 const header = encoder.encode(`blob ${bytes.byteLength}\0`);54 const raw = new Uint8Array(header.byteLength + bytes.byteLength);5556 raw.set(header);57 raw.set(bytes, header.byteLength);5859 return raw;60};6162/** The id git gives a blob holding `bytes`. */63const blobId = (bytes: Uint8Array) =>64 Effect.map(54 Effect.promise(() =>55 crypto.subtle.digest(56 "SHA-1",57 Uint8Array.from([58 ...encoder.encode(`blob ${bytes.byteLength}\0`),59 ...bytes,60 ])61 )62 ),65 Effect.promise(() => crypto.subtle.digest("SHA-1", framed(bytes))),66 (digest) =>67 [...new Uint8Array(digest)]68 .map((byte) => byte.toString(16).padStart(2, "0"))69 .join("")
32 unmodified lines
102 );103104 const dir = `${source.gitdir}/objects/${blob.slice(0, 2)}`;105103 const loose = yield* deflate(104 Uint8Array.from([105 ...encoder.encode(`blob ${bytes.byteLength}\0`),106 ...bytes,107 ])108 );106 const loose = yield* deflate(framed(bytes));107108 // Another note's blob may have made the folder already.109 yield* Effect.ignore(110 Effect.tryPromise(() => source.fs.promises.mkdir(dir))
107 unmodified lines
modified:apps/platform/test/notes.test.ts+93 added-7 removed
6 unmodified lines
7 RepoHost,8 RepoName,9 RepoNotFound,10} from "@repo/artifacts/repo-host";11import type { RefUpdate } from "@repo/artifacts/repo-host";12import { RepoHostLocal } from "@repo/artifacts/repo-host/local";13import { memoryFs } from "@repo/artifacts/repo-host/memory-fs";14import { pushRefs } from "@repo/artifacts/repo-host/smart-http";15import { Brand } from "@repo/brand";
27 unmodified lines
43 Schema,44} from "effect";45import type { Scope } from "effect";46import { HttpServer, HttpServerRequest, HttpServerResponse } from "effect/http";47import { TestClock } from "effect/testing";4849import { WorkerFetch } from "../src/adapters/fetch.ts";50import { runAnswer } from "../src/workflows/answer/program.ts";51import { notesRef, writeVerdictNote } from "../src/workflows/notes.ts";
224 unmodified lines
276 }).pipe(Effect.provide(WorkerFetch))277 ),278 });279280/** The disk's store, keeping the updates of every push it takes, in order. */281const recordingPushes = (disk: DiskWorld) =>282 Effect.map(Ref.make<readonly (readonly RefUpdate[])[]>([]), (pushed) => ({283 host: RepoHost.of({284 ...disk.host,285 push: (name, source, updates) =>286 Effect.andThen(287 Ref.update(pushed, (all) => [...all, updates]),288 disk.host.push(name, source, updates)289 ),290 }),291 pushed: Ref.get(pushed),292 }));293294/** Where the fork's notes ref stands, through the port. */295const notesTip = (host: RepoHost["Service"]) =>296 Effect.map(host.refs(fork).pipe(Effect.orDie), (refs) =>297 Option.fromNullishOr(refs.get(notesRef))
107 unmodified lines
405 30_000406 );407408 it.live(409 "pushes the notes ref alone, never forced: a compare-and-swap from the tip it listed",410 () =>411 Effect.gen(function* () {412 const disk = yield* diskWorld;413414 const { first, second } = yield* forkWithTwoHeads(disk.host);415416 const recording = yield* recordingPushes(disk);417418 const noted = (head: CommitSha) =>419 writeVerdictNote({ fork, head, verdict: refusedOn(head) }).pipe(420 Effect.provideService(RepoHost, recording.host)421 );422423 const one = yield* noted(first);424 const two = yield* noted(second);425426 expect(yield* recording.pushed).toStrictEqual([427 [{ from: Option.none(), ref: notesRef, to: one.notesTip }],428 [429 {430 from: Option.some(one.notesTip),431 ref: notesRef,432 to: two.notesTip,433 },434 ],435 ]);436 }).pipe(Effect.provide(NodeServices.layer)),437 30_000438 );439440 it.live(441 "a notes ref that moved under the write is read again, and the note goes on top of the move",442 () =>443 Effect.gen(function* () {444 const disk = yield* diskWorld;
127 unmodified lines
572 });573574/**575 * The world's store, each push to the fork's notes ref counted; with528 * `refuse`, every such push fails as a store that lost the fork would.576 * `refuse`, every such push fails as a store that lost the fork would; with577 * `busy`, that many first ones find the fork busy with another push.578 */530const countingNotes = (options: { readonly refuse?: boolean } = {}) =>579const countingNotes = (580 options: { readonly busy?: number; readonly refuse?: boolean } = {}581) =>582 Effect.gen(function* () {583 const host = yield* RepoHost;584 const pushes = yield* Ref.make(0);585586 const counted = RepoHost.of({587 ...host,588 push: (name, source, updates) =>589 name === bobFork && updates.some(({ ref }) => ref === notesRef)539 ? Effect.andThen(540 Ref.update(pushes, (n) => n + 1),541 options.refuse === true542 ? Effect.fail(new RepoNotFound({ name }))543 : host.push(name, source, updates)590 ? Effect.flatMap(591 Ref.getAndUpdate(pushes, (n) => n + 1),592 (before): ReturnType<typeof host.push> => {593 if (options.refuse === true) {594 return Effect.fail(new RepoNotFound({ name }));595 }596597 return before < (options.busy ?? 0)598 ? Effect.fail(new HostBusy({ name }))599 : host.push(name, source, updates);600 }601 )602 : host.push(name, source, updates),603 });604
134 unmodified lines
739 }).pipe(Effect.provide(world))740 );741742 it.effect(743 "a fork busy with another push is asked again, and the note is written",744 () =>745 Effect.gen(function* () {746 const answer = yield* admitted();747748 yield* unitFails(answer);749750 const { counted, pushes } = yield* countingNotes({ busy: 1 });751752 // The step waits out its backoff on the TestClock: move the clock753 // on for as long as the run goes.754 const outcome = yield* Effect.race(755 runOver(answer, yield* freshSteps, counted),756 Effect.forever(TestClock.adjust("250 millis"))757 );758759 const noted = yield* noteOnBobsFork(answer.head);760761 expect(Outcome.guards.Refused(outcome)).toBeTruthy();762 expect(yield* pushes).toBe(2);763 expect(yield* noteIn(noted)).toStrictEqual(764 Option.some(765 yield* encoded(Option.getOrThrow(yield* verdictOf(answer.api)))766 )767 );768 }).pipe(Effect.provide(world))769 );770771 it.effect(772 "a note the fork refuses leaves the run's outcome as it was, in the run and in its replay",773 () =>774 Effect.gen(function* () {775 const answer = yield* admitted();
15 unmodified lines