Skip to content
Myr

Merge build/1102 into main

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

authored by oscabriel · committed by oscabriel · parents 42ea9b7, a9774ec · Browse files · 16 files+639 added−29 removed
This commit

State

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

Diff stats

16 files+639 added−29 removed

  • tests:+519 added−6 removed
  • other:+120 added−23 removed

16 files+639 added−29 removed

modified:apps/platform/src/handlers.ts+10 added-4 removed
1 unmodified line
2 * The contracts' handlers: routing, the rate limit and the fan-out, nothing3 * more. A contract that names a project calls4 * that project's `ProjectApi` (its Durable Object, through `Projects`) as5 * the caller `Authenticated` named; the cross-project reads (`listProjects`,6 * `frontPage`, `credits`) come from the front-page projection; `status` and7 * `today` without a project ask every project the projection lists and8 * merge what each says. `claim`, `proposeAsk` and `flagAsk` spend the6 * `frontPage`, `credits`) come from the front-page projection, each listed7 * project's posture read from its own ledger (`withStandingPosture`);8 * `status` and `today` without a project ask every project the projection9 * lists and merge what each says. `claim`, `proposeAsk` and `flagAsk` spend the10 * caller's rate limit first, so a call past it reaches no project; a11 * proposal with a draft, and `draftAsk`, spend the draft limit too.12 * `evidence` asks the project who may read a lane's log, which it reads13 * from the stage's `EvidenceStore`. The code reads (`listRefs`, `readTree`,
22 unmodified lines
36import { FrontPage } from "@repo/project/front-page";37import { GatewayUrl } from "@repo/project/project";38import type { ProjectApi } from "@repo/project/project";39import { Projects } from "@repo/project/projects";40import { withStandingPosture } from "@repo/project/standing-posture";41import { Contracts, EnvSchemaOf } from "@repo/protocol/contracts";42import type { AnswerView } from "@repo/protocol/domain/answer";43import type { ClaimView } from "@repo/protocol/domain/claim";44import type { Decision, Today } from "@repo/protocol/domain/decision";
279 unmodified lines
324 ),325 listPaths: ({ at, project }) =>326 code(project, (of) => pathsOf(of, Option.fromUndefinedOr(at))),327 listProjects: () =>326 Effect.map(frontPage.projects(), (listed) => ({ projects: listed })),328 frontPage.projects().pipe(329 Effect.flatMap(withStandingPosture),330 Effect.provideService(Projects, projects),331 Effect.map((listed) => ({ projects: listed }))332 ),333 listRefs: ({ project }) => code(project, refsOf),334 log: ({ at, from = 0, limit = LOG_PAGE, path, project }) =>335 code(project, (of) =>336 logOf(of, {
109 unmodified lines
modified:apps/platform/src/home.ts+14 added-5 removed
6 unmodified lines
7 *8 * Every URL is built from the request's origin, the one the client reached:9 * the platform is also the git remote, the MCP base and the CLI's URL, so10 * the page names them as the reader will type them. The projects are the11 * front page's, and nothing else: no layer and no fork is listed. Every12 * value that reaches the HTML is escaped. Not a redirect: the web app is a13 * link, so the host stays discoverable at its own address.11 * front page's, and nothing else: no layer and no fork is listed; each12 * one's posture is the one its own ledger stands at, as `listProjects`13 * answers it (`withStandingPosture`), so `/` and the web's `/llms.txt`14 * stay one text. Every value that reaches the HTML is escaped. Not a15 * redirect: the web app is a link, so the host stays discoverable at its16 * own address.17 */18import { commandDocs } from "@repo/agent-kit/command-doc";19import { frontLlms, MCP_PROTOCOL } from "@repo/agent-kit/host-doc";20import { Brand } from "@repo/brand";21import { FrontPage } from "@repo/project/front-page";22import type { Projects } from "@repo/project/projects";23import { withStandingPosture } from "@repo/project/standing-posture";24import { Contracts } from "@repo/protocol/contracts";25import type { ProjectListing } from "@repo/protocol/domain/front-page";26import { Effect, Option } from "effect";27
139 unmodified lines
167168/** `/` for `request`: Markdown for an agent that asks for it, else the page. */169export const homeResponse = (170 request: Request166): Effect.Effect<Response, never, FrontPage | HostInfo> =>171): Effect.Effect<Response, never, FrontPage | HostInfo | Projects> =>172 Effect.gen(function* () {173 const { draftModel, webUrl } = yield* HostInfo;174169 const projects = yield* FrontPage.use((frontPage) => frontPage.projects());175 const projects = yield* FrontPage.use((frontPage) =>176 frontPage.projects()177 ).pipe(Effect.flatMap(withStandingPosture));178179 const { origin } = new URL(request.url);180181 if (wantsMarkdown(request)) {182 return new Response(
30 unmodified lines
modified:apps/platform/src/surfaces.ts+2 added-1 removed
366 unmodified lines
367/** `/`: what this host serves, for a browser and for an agent (`home.ts`). */368const home = HttpRouter.use((router) =>369 Effect.gen(function* () {370 const services = Context.make(FrontPage, yield* FrontPage).pipe(371 Context.add(HostInfo, yield* HostInfo)371 Context.add(HostInfo, yield* HostInfo),372 Context.add(Projects, yield* Projects)373 );374375 yield* router.add(376 "GET",
111 unmodified lines
added:apps/platform/test/visibility/posture-listing.test.ts+117 added
1/**2 * The project list's posture over the platform's Node world (the core3 * loop's world) with the visibility cartridge's project state composed4 * into every Project DO, as `ledger-mcp.test.ts` composes it. datekit's5 * first push carries a `visibility.ts` that declares sealed checks: a first6 * config stands at once and writes no `PostureChanged` row, so the7 * projection still lists datekit as `open`, the row D1 holds on demo. The8 * list reads datekit's posture from datekit's own ledger, so the badge and9 * the ledger say the same.10 */11import { describe, expect, it } from "@effect/vitest";12import { Fetch } from "@repo/artifacts/fetch";13import { RepoHost } from "@repo/artifacts/repo-host";14import { RepoHostMemory } from "@repo/artifacts/repo-host/memory";15import { Brand } from "@repo/brand";16import { FrontPageMemory } from "@repo/project/front-page/memory";17import { ProjectName } from "@repo/protocol/ids";18import { fakeArtifactsBinding } from "@repo/testkit/fake-artifacts-binding";19import { untouchable } from "@repo/testkit/untouchable";20import { Effect, Layer } from "effect";2122import { decodeEnv } from "../../src/platform.ts";23import { stateLive } from "../../src/visibility/state.ts";24import { fakeEnv } from "../fake-env.ts";25import { coreLoopWorld } from "../world.ts";2627const project = ProjectName.make("datekit");2829const sdk = `${Brand.cliPackage}/rules`;3031/** datekit as dora first pushes it, its visibility config declaring sealed checks. */32const start = {33 [`${Brand.configDir}/project.ts`]: [34 `import { kinds, project } from "${sdk}";`,35 ``,36 `export default project({`,37 ` kinds: [kinds.fix],`,38 ` run: { checks: {}, test: "bun test {path}" },`,39 `});`,40 ``,41 ].join("\n"),42 [`${Brand.configDir}/visibility.ts`]: [43 `import { flows, layer, sealedChecks, visibility } from "${sdk}";`,44 ``,45 `export default visibility({`,46 ` checks: sealedChecks({`,47 ` areas: { export: "src/format.ts", parser: "src/parse.ts" },`,48 ` }),`,49 ` flows: flows.noWriteDown(),`,50 ` layers: {`,51 ` maintainers: layer({`,52 ` disclose: "sealed",`,53 ` holds: ["checks", "notes"],`,54 ` reason: "Held-back checks for export and parser",`,55 ` }),`,56 ` },`,57 `});`,58 ``,59 ].join("\n"),60 "package.json": '{ "name": "datekit", "private": true }\n',61 "src/parse.ts": "export const parse = (iso: string) => 0;\n",62};6364/** A `Fetch` that nothing here may call. */65const noFetch = Layer.succeed(Fetch, untouchable<Fetch["Service"]>("Fetch"));6667/** The cartridge's project state for the world's stage, over a private namespace of its own. */68const cartridge = Layer.unwrap(69 Effect.gen(function* () {70 const host = yield* Effect.provide(71 Effect.service(RepoHost),72 RepoHostMemory73 );7475 const config = yield* decodeEnv({76 ...fakeEnv(),77 ARTIFACTS_PRIVATE: fakeArtifactsBinding(host, {78 remoteBase: "https://artifacts.test",79 }),80 }).pipe(Effect.orDie);8182 return stateLive(config).pipe(Layer.provide(noFetch));83 })84);8586describe("the project list, with the visibility cartridge composed", () => {87 it.live(88 "lists datekit at the posture its ledger stands at, though the projection says open",89 () =>90 Effect.scoped(91 Effect.gen(function* () {92 const world = yield* coreLoopWorld({93 frontPage: FrontPageMemory.layer({94 credits: [],95 entries: [],96 projects: [{ openAsks: 0, posture: "open", project }],97 }),98 });99100 const dora = world.rpc("dora");101 const made = yield* dora.initProject({ project });102 const home = yield* world.git("dora").clone(made.remote);103104 yield* world.git("dora").commit(home, "Start datekit", start);105 yield* world.git("dora").push(home);106107 const anyone = world.rpc("anonymous");108 const { posture } = yield* anyone.ledger({ project });109 const { projects } = yield* anyone.listProjects({});110111 expect(posture).toBe("openWithSealedEvidence");112 expect(projects).toStrictEqual([{ openAsks: 0, posture, project }]);113 }).pipe(Effect.provide(cartridge))114 ),115 30_000116 );117});
modified:apps/platform/test/front-page.test.ts+3 added-2 removed
261 unmodified lines
262 });263264describe("the project list and credits over the platform's /rpc", () => {265 it.effect(266 "after a landing, lists the project and its people's credits from D1 without calling a project",266 "after a landing, lists the project from D1, asking it only its posture, and its people's credits without calling a project",267 () =>268 Effect.gen(function* () {269 yield* advanceTo(noon);270
19 unmodified lines
290 credits: [291 { area: "parser", count: 1, project, role: "Signed-off-by" },292 ],293 },294 calls: [],294 // The list's one read: datekit's posture, from its ledger.295 calls: [`get ${project}`],296 listed: {297 projects: [298 { landedAt: at(noon), openAsks: 1, posture: "open", project },299 ],
5 unmodified lines
modified:apps/platform/test/home.test.ts+86 added-4 removed
8 unmodified lines
9import { commandDocs } from "@repo/agent-kit/command-doc";10import { frontLlms } from "@repo/agent-kit/host-doc";11import { Brand } from "@repo/brand";12import { FrontPageMemory } from "@repo/project/front-page/memory";13import type { ProjectApi } from "@repo/project/project";14import { Projects } from "@repo/project/projects";15import { Contracts } from "@repo/protocol/contracts";16import type { ProjectListing } from "@repo/protocol/domain/front-page";17import { ProjectName } from "@repo/protocol/ids";18import { UnknownProject } from "@repo/protocol/refusal";19import { untouchable } from "@repo/testkit/untouchable";20import { Effect, Layer, Option } from "effect";2122import { MemorySessions } from "../src/sessions/memory.ts";23import { HostInfo } from "../src/surfaces.ts";
9 unmodified lines
33 project: ProjectName.make("burrow"),34 },35];3633/** The platform listing `listed`, on a host whose web app is `web`. */37/**38 * The platform's projects where datekit's ledger stands sealed: its first39 * config declared sealed checks, which writes no `PostureChanged` row, so40 * the projection still lists it `open`. burrow was never made here.41 */42const datekitSealed = Layer.succeed(43 Projects,44 Projects.of({45 create: () => Effect.die("the home page makes no project"),46 get: (project) =>47 project === "datekit"48 ? Effect.succeed(49 untouchable<ProjectApi>("ProjectApi", {50 ledger: () =>51 Effect.succeed({52 entries: [],53 layers: [],54 posture: "openWithSealedEvidence",55 postureLabel: "Open with sealed evidence",56 project,57 }),58 })59 )60 : Effect.fail(new UnknownProject({ project })),61 })62);6364/** The platform's projects before any is made: each listed one keeps its listed posture. */65const noneMade = Layer.succeed(66 Projects,67 Projects.of({68 create: () => Effect.die("the home page makes no project"),69 get: (project) => Effect.fail(new UnknownProject({ project })),70 })71);7273/**74 * The platform listing `listed`, on a host whose web app is `web`, with75 * `made` for its projects.76 */77const platform = (78 listed: readonly ProjectListing[],36 web: Option.Option<string> = Option.some(webUrl)79 web: Option.Option<string> = Option.some(webUrl),80 made: Layer.Layer<Projects> = noneMade81) =>82 served(83 Layer.mergeAll(84 made,85 noProjects,86 MemorySessions.layer({}),87 FrontPageMemory.layer({ credits: [], entries: [], projects: listed }),88 Layer.succeed(
6 unmodified lines
95/** `/` as `headers` ask for it: its status, type and text. */96const home = (97 listed: readonly ProjectListing[],98 init: RequestInit = {},54 web?: Option.Option<string>99 web?: Option.Option<string>,100 made?: Layer.Layer<Projects>101) =>102 Effect.gen(function* () {57 const { fetch } = yield* platform(listed, web);103 const { fetch } = yield* platform(listed, web, made);104 const response = yield* fetch(new Request(`${origin}/`, init));105106 return {107 status: response.status,
98 unmodified lines
206 })207 );208209 it.effect(210 "gives each project the posture its own ledger stands at, as the web's /llms.txt does",211 () =>212 Effect.gen(function* () {213 const { text } = yield* home(214 projects,215 { headers: { accept: "text/markdown" } },216 undefined,217 datekitSealed218 );219220 expect(text).toBe(221 frontLlms({222 commands: commandDocs(Contracts),223 platformUrl: origin,224 projects: [225 {226 openAsks: 3,227 posture: "openWithSealedEvidence",228 project: ProjectName.make("datekit"),229 },230 {231 openAsks: 1,232 posture: "openWithSealedEvidence",233 project: ProjectName.make("burrow"),234 },235 ],236 webUrl,237 })238 );239 expect(text).toContain(240 `- [datekit](${webUrl}/datekit/llms.txt): Open with sealed evidence, 3 open asks.`241 );242 })243 );244245 it.effect(246 "gives an agent the platform's own Markdown on a host with no web app",247 () =>248 Effect.gen(function* () {249 const { text, type } = yield* home(
64 unmodified lines
added:apps/platform/test/posture-listing.test.ts+94 added
1/**2 * `listProjects` as the platform's handlers answer it: the projection lists3 * each project, and each listed project's posture is the one its own4 * ledger stands at now, so the badge and the ledger say the same. The5 * projection here lists datekit as `open`, the row D1 holds for a project6 * whose first config declared sealed checks (that config writes no7 * `PostureChanged` row); datekit's ledger stands sealed. A project the8 * platform never made keeps the posture the projection listed.9 */10import { describe, expect, it } from "@effect/vitest";11import { RepoHost } from "@repo/artifacts/repo-host";12import { EvidenceStoreMemory } from "@repo/checks/evidence-store/memory";13import { FrontPageMemory } from "@repo/project/front-page/memory";14import type { ProjectApi } from "@repo/project/project";15import { Projects } from "@repo/project/projects";16import { Contracts } from "@repo/protocol/contracts";17import { ProjectName } from "@repo/protocol/ids";18import {19 Anonymous,20 Authenticated,21 CurrentPrincipal,22} from "@repo/protocol/principal";23import { UnknownProject } from "@repo/protocol/refusal";24import { untouchable } from "@repo/testkit/untouchable";25import { Effect, Layer } from "effect";26import { RpcTest } from "effect/rpc";2728import { MemoryRateLimits, WorkerCaps } from "../src/adapters/rate-limits.ts";29import { Handlers } from "../src/handlers.ts";3031const datekit = ProjectName.make("datekit");3233const ghost = ProjectName.make("ghost");3435/** datekit's project, whose only answered read is its ledger, standing sealed. */36const datekitApi = untouchable<ProjectApi>("ProjectApi", {37 ledger: () =>38 Effect.succeed({39 entries: [],40 layers: [],41 posture: "openWithSealedEvidence",42 postureLabel: "Open with sealed evidence",43 project: datekit,44 }),45});4647const platform = Handlers.pipe(48 Layer.provide([49 Layer.succeed(50 Projects,51 Projects.of({52 create: () => Effect.die("a listing makes no project"),53 get: (project) =>54 project === datekit55 ? Effect.succeed(datekitApi)56 : Effect.fail(new UnknownProject({ project })),57 })58 ),59 FrontPageMemory.layer({60 credits: [],61 entries: [],62 projects: [63 { openAsks: 3, posture: "open", project: datekit },64 { openAsks: 0, posture: "openCore", project: ghost },65 ],66 }),67 MemoryRateLimits(WorkerCaps),68 EvidenceStoreMemory,69 Layer.succeed(RepoHost, untouchable<RepoHost["Service"]>("RepoHost")),70 ])71);7273const anonymous = Layer.succeed(Authenticated, (effect) =>74 Effect.provideService(effect, CurrentPrincipal, Anonymous.make({}))75);7677describe("the platform's project list", () => {78 it.effect("gives each project the posture its ledger stands at", () =>79 Effect.gen(function* () {80 const anyone = yield* RpcTest.makeClient(Contracts);8182 expect(yield* anyone.listProjects({})).toStrictEqual({83 projects: [84 {85 openAsks: 3,86 posture: "openWithSealedEvidence",87 project: datekit,88 },89 { openAsks: 0, posture: "openCore", project: ghost },90 ],91 });92 }).pipe(Effect.provide(Layer.mergeAll(platform, anonymous)))93 );94});
modified:apps/web/src/dev/front-page.ts+5 added-3 removed
5 unmodified lines
6 * claim marks it claimed until it lapses or is released. An entry is what7 * the anonymous viewer sees: takeable when no live claim holds it, nothing8 * it waits on is still on the page, and it is held for nobody; newest first.9 * A project is listed from its first event, `open` until a `PostureChanged`10 * ledger entry moves it, with when it last landed (the `projects`11 * table). A person's credits are each project's own count, joined12 * (`creditsFor`): the rule D1's `credits` table is fed by, read at the source.10 * ledger entry moves it, with when it last landed (the `projects` table);11 * `listProjects` reads each one's standing posture from its own ledger12 * (`withStandingPosture`) and keeps this one only as the fallback. A13 * person's credits are each project's own count, joined (`creditsFor`): the14 * rule D1's `credits` table is fed by, read at the source.15 */16import { FrontPage } from "@repo/project/front-page";17import type { FrontPageFilter } from "@repo/project/front-page";18import type { ProjectedEvent } from "@repo/project/projection";
212 unmodified lines
modified:apps/web/src/dev/handlers.ts+6 added-1 removed
26 unmodified lines
27import type { CodeProject } from "@repo/project/code";28import { FrontPage } from "@repo/project/front-page";29import type { ProjectApi } from "@repo/project/project";30import { Projects } from "@repo/project/projects";31import { withStandingPosture } from "@repo/project/standing-posture";32import { Contracts, EnvSchemaOf } from "@repo/protocol/contracts";33import type { AnswerView } from "@repo/protocol/domain/answer";34import type { ClaimView } from "@repo/protocol/domain/claim";35import type { Decision, Today } from "@repo/protocol/domain/decision";
242 unmodified lines
278 ),279 listPaths: ({ at, project }) =>280 code(project, (of) => pathsOf(of, Option.fromUndefinedOr(at))),281 listProjects: () =>281 Effect.map(frontPage.projects(), (listed) => ({ projects: listed })),282 frontPage.projects().pipe(283 Effect.flatMap(withStandingPosture),284 Effect.provideService(Projects, projects),285 Effect.map((listed) => ({ projects: listed }))286 ),287 listRefs: ({ project }) => code(project, refsOf),288 log: ({ at, from = 0, limit = LOG_PAGE, path, project }) =>289 code(project, (of) =>290 logOf(of, {
107 unmodified lines
modified:apps/web/src/routes/$project/index.tsx+4 added-3 removed
modified:apps/web/test/dev-backend.test.ts+32 added
169 unmodified lines
170 };171 })172);173174/** The project list beside each project's ledger, as anyone reads them. */175const listedWithLedgers = inFreshWorld((as) =>176 Effect.gen(function* () {177 const anyone = yield* clientOf(as());178 const { projects } = yield* anyone.listProjects({});179180 const ledgers = yield* Effect.forEach(PROJECTS, (project) =>181 anyone.ledger({ project })182 );183184 return { ledgers, projects };185 })186);187188/**189 * Each test seeds whole worlds, the first one cold, while turbo runs every190 * package's tests at once: about 0.5 s alone, near vitest's 5 s default191 * under that load once burrow's migrate opens as a family of eight asks.
46 unmodified lines
238 expect(landedAt).toHaveLength(1);239 })240 );241242 it.live("lists each project at the posture its own ledger gives", () =>243 Effect.gen(function* () {244 const { ledgers, projects } = yield* listedWithLedgers;245246 expect(247 projects.map(({ posture, project }) => [project, posture])248 ).toStrictEqual(249 ledgers.map(({ posture, project }) => [project, posture])250 );251 // The dev world composes no visibility cartridge, so every ledger,252 // and the list with it, reads Open.253 expect(ledgers.map(({ posture }) => posture)).toStrictEqual([254 "open",255 "open",256 ]);257 })258 );259260 it.live("lands bram's answer to datekit with its credit trailers", () =>261 Effect.gen(function* () {262 const { landing, sampled } = yield* landedAnswer;263
133 unmodified lines
modified:packages/project/src/projection-d1/projection.ts+6 added-3 removed
3 unmodified lines
4 * Project DO stays the system of record; this is a read model it feeds5 * through its outbox, and nothing else writes it.6 *7 * - A project is listed from its first event on (no event says a project8 * was made), `open` until a `PostureChanged` ledger entry moves it. Its9 * open asks are counted again from the front page whenever an ask opens10 * or closes, and a landing stamps when it last landed.8 * was made), `open` until a `PostureChanged` ledger entry moves it: the9 * fallback for a list, which reads each project's standing posture from10 * its own ledger (`withStandingPosture`), since a first config, a11 * widening and a narrowing taking effect by the clock write no such12 * entry. Its open asks are counted again from the front page whenever an13 * ask opens or closes, and a landing stamps when it last landed.14 * - A landing credits each person it names once per role, in its ask's15 * area: the module of the ask's territory, read from the ask's front16 * page row, which is still there when its `Landed` applies (its17 * `AskClosed` comes after). A landing with no ask, or whose ask the front
250 unmodified lines
modified:packages/project/src/projection-d1/tables.ts+3 added-1 removed
55 unmodified lines
56 * and the projection's credits test applies the file and checks that it57 * does.58 *59 * - `projects`: one row per project the projection has heard from: its60 * posture (`open` until a `PostureChanged` ledger entry), its open asks60 * posture (`open` until a `PostureChanged` ledger entry; the fallback a61 * list keeps when it cannot read the project's own ledger, which it reads62 * for the posture standing now: `withStandingPosture`), its open asks63 * (counted from `front_page` whenever an ask opens or closes), and when64 * it last landed (null before its first landing).65 * - `credits`: how many landings credit a person (by `handle_key`, the66 * handle lowercased: `handleKey`) in one role, in one area of one project.
43 unmodified lines
modified:packages/project/src/front-page.ts+9 added-2 removed
1/**2 * The `FrontPage` port: the read side of the projection every project feeds3 * (the write side is `Projection`, whose rows come from each project's4 * outbox). The front page, the project list and a person's credits read from5 * here without waking any project.5 * here without waking any project. A listed project's `posture` is the6 * fallback only: a list's callers read each project's posture from its own7 * ledger at list time (`withStandingPosture`), since its standing changes8 * without any event this read model hears, and keep this one for a project9 * whose ledger could not be read.10 *11 * Adapters: `/memory` here, seeded from data, for tests; `projection-d1`12 * over D1 in the Worker.13 */
15 unmodified lines
2930export class FrontPage extends Context.Service<31 FrontPage,32 {29 /** Every project, with its posture and how many asks it has open. */33 /**34 * Every project, with how many asks it has open and the posture last35 * projected (the fallback for its standing posture).36 */37 readonly projects: () => Effect.Effect<readonly ProjectListing[]>;38 /** The open asks across every project, as the anonymous viewer sees them. */39 readonly entries: (40 filter: FrontPageFilter
7 unmodified lines
added:packages/project/src/standing-posture.ts+61 added
1/**2 * A listed project's posture, as its own record says it now. The3 * projection's `posture` moves only on a `PostureChanged` ledger entry, and4 * a project's standing changes without one: its first config stands at5 * once, a widening needs no decision, and an accepted narrowing takes6 * effect when its notice runs out, by the clock. So a list of projects7 * reads each one's ledger, the public read whose `posture` is the standing8 * at now (`visibility/ledger.ts`), and shows that: the badge and the ledger9 * are one value, read at the source, with nothing written to keep them in10 * step.11 *12 * Cost: one ledger read per listed project per list, at most 8 at once,13 * each read whole for its one `posture` field.14 */15import type { ProjectListing } from "@repo/protocol/domain/front-page";16import { Anonymous } from "@repo/protocol/principal";17import { Cause, Effect } from "effect";1819import { Projects } from "./projects.ts";2021/** How many projects a list reads at once (the platform handlers' `FAN_OUT`). */22const READS_AT_ONCE = 8;2324/**25 * `listed` with each project's posture as its own record says it now (its26 * ledger's standing), read at most 8 at once; a project never made, or one27 * whose read fails, keeps the posture the projection listed.28 */29export const withStandingPosture = (30 listed: readonly ProjectListing[]31): Effect.Effect<readonly ProjectListing[], never, Projects> =>32 Effect.gen(function* () {33 const projects = yield* Projects;3435 return yield* Effect.forEach(36 listed,37 (listing) =>38 Effect.suspend(() => projects.get(listing.project)).pipe(39 Effect.flatMap((api) => api.ledger(Anonymous.make({}))),40 Effect.map(({ posture }): ProjectListing => ({41 ...listing,42 posture,43 })),44 // The list answers whatever one project does, a `get` that45 // throws included: an interruption stays one, and anything else46 // keeps the projected posture.47 Effect.catchCause((cause) =>48 Cause.hasInterruptsOnly(cause)49 ? Effect.interrupt50 : Effect.as(51 Effect.logWarning(52 `${listing.project}'s posture is the projection's: its ledger was not read`,53 cause54 ),55 listing56 )57 )58 ),59 { concurrency: READS_AT_ONCE }60 );61 });
added:packages/project/test/standing-posture.test.ts+187 added
1/**2 * A listed project's posture, as its own record says it now: each listing3 * the projection gives is overlaid with the posture its project's ledger4 * stands at, read as anyone reads it, a few projects at once. A project the5 * platform never made, or one whose read breaks, keeps the posture the6 * projection listed, and the list still answers.7 */8import { describe, expect, it } from "@effect/vitest";9import type { ProjectListing } from "@repo/protocol/domain/front-page";10import { ProjectName } from "@repo/protocol/ids";11import type { Posture } from "@repo/protocol/posture";12import { Principal } from "@repo/protocol/principal";13import { UnknownProject } from "@repo/protocol/refusal";14import { untouchable } from "@repo/testkit/untouchable";15import { Array, DateTime, Duration, Effect, Layer, Logger, Ref } from "effect";1617import type { ProjectApi } from "../src/project.ts";18import { Projects } from "../src/projects.ts";19import { withStandingPosture } from "../src/standing-posture.ts";2021const datekit = ProjectName.make("datekit");2223const ghost = ProjectName.make("ghost");2425const broken = ProjectName.make("broken");2627const burrow = ProjectName.make("burrow");2829const landedAt = DateTime.makeUnsafe("2026-10-08T12:00:00Z");3031/**32 * An api whose only answered read is `ledger`, standing at `posture` for33 * anyone; a person's read is a defect, since a listing is public.34 */35const standingAt = (36 project: ProjectName,37 posture: Posture,38 before: Effect.Effect<void> = Effect.void39) =>40 untouchable<ProjectApi>("ProjectApi", {41 ledger: (caller) =>42 Principal.match(caller, {43 Anonymous: () =>44 Effect.as(before, {45 entries: [],46 layers: [],47 posture,48 postureLabel: "As its ledger says",49 project,50 }),51 Person: ({ handle }) =>52 Effect.die(`a listing read ${project}'s ledger as ${handle}`),53 }),54 });5556/**57 * The platform's projects: datekit's ledger stands sealed, burrow's open,58 * `broken`'s read dies, and `ghost` was never made.59 */60const platform = Layer.succeed(61 Projects,62 Projects.of({63 create: () => Effect.die("a listing makes no project"),64 get: (project) => {65 switch (project) {66 case datekit: {67 return Effect.succeed(standingAt(datekit, "openWithSealedEvidence"));68 }6970 case burrow: {71 return Effect.succeed(standingAt(burrow, "open"));72 }7374 case broken: {75 return Effect.succeed(76 untouchable<ProjectApi>("ProjectApi", {77 ledger: () => Effect.die("the project's storage is down"),78 })79 );80 }8182 default: {83 return Effect.fail(new UnknownProject({ project }));84 }85 }86 },87 })88);8990const listed: readonly ProjectListing[] = [91 { landedAt, openAsks: 3, posture: "open", project: datekit },92 { openAsks: 1, posture: "openCore", project: ghost },93 { openAsks: 0, posture: "sourceAvailable", project: broken },94 { landedAt, openAsks: 2, posture: "openOnSchedule", project: burrow },95];9697describe("a listing's standing posture", () => {98 it.effect("reads each project's posture from its own ledger", () =>99 Effect.gen(function* () {100 const standing = yield* withStandingPosture(listed);101102 expect(standing).toStrictEqual([103 {104 landedAt,105 openAsks: 3,106 posture: "openWithSealedEvidence",107 project: datekit,108 },109 { openAsks: 1, posture: "openCore", project: ghost },110 { openAsks: 0, posture: "sourceAvailable", project: broken },111 { landedAt, openAsks: 2, posture: "open", project: burrow },112 ]);113 }).pipe(Effect.provide(platform))114 );115116 it.effect(117 "warns once for each project whose posture stays the projection's",118 () =>119 Effect.gen(function* () {120 const warned: string[] = [];121122 const keeper = Logger.make(({ logLevel, message }) => {123 warned.push(`${logLevel}: ${String(message)}`);124 });125126 yield* withStandingPosture(listed).pipe(127 Effect.provide(Logger.layer([keeper]))128 );129130 expect(warned.toSorted()).toStrictEqual([131 "Warn: broken's posture is the projection's: its ledger was not read",132 "Warn: ghost's posture is the projection's: its ledger was not read",133 ]);134 }).pipe(Effect.provide(platform))135 );136137 it.effect("lists nothing for an empty list, and asks no project", () =>138 Effect.gen(function* () {139 expect(yield* withStandingPosture([])).toStrictEqual([]);140 }).pipe(141 Effect.provide(142 Layer.succeed(Projects, untouchable<Projects["Service"]>("Projects"))143 )144 )145 );146147 it.live("reads at most 8 projects at once", () =>148 Effect.gen(function* () {149 const under = yield* Ref.make(0);150 const peak = yield* Ref.make(0);151152 const timed = Effect.acquireUseRelease(153 Ref.updateAndGet(under, (count) => count + 1).pipe(154 Effect.tap((count) =>155 Ref.update(peak, (most) => Math.max(most, count))156 )157 ),158 () => Effect.sleep(Duration.millis(10)),159 () => Ref.update(under, (count) => count - 1)160 );161162 const many = Array.makeBy(20, (at): ProjectListing => ({163 openAsks: at,164 posture: "open",165 project: ProjectName.make(`p${at}`),166 }));167168 const standing = yield* withStandingPosture(many).pipe(169 Effect.provide(170 Layer.succeed(171 Projects,172 Projects.of({173 create: () => Effect.die("a listing makes no project"),174 get: (project) =>175 Effect.succeed(standingAt(project, "openCore", timed)),176 })177 )178 )179 );180181 expect(standing).toStrictEqual(182 many.map((listing) => ({ ...listing, posture: "openCore" }))183 );184 expect(yield* Ref.get(peak)).toBe(8);185 })186 );187});