Skip to content
Myr

loop.ts

This file

Path

scripts/lib/smoke/loop.ts

369 lines · 8.8 KiB · TypeScript

Last changed by

Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf

History of this file

Take it

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

line 1why, one step back
1import { Brand } from "@repo/brand";

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 2–3why, one step back
2import { keyOf, ProjectName } from "@repo/protocol/ids";
3import type { Handle, LandedSha } from "@repo/protocol/ids";

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 4–18why, one step back
4import {
5 Console,6 DateTime,7 Duration,8 Effect,9 FileSystem,10 Option,11 Schema,12} from "effect";13import type { Redacted } from "effect";14import type { ChildProcessSpawner } from "effect/process";1516import { recordCreated } from "../../../infra/created.ts";17import type { Created } from "../../../infra/created.ts";18import {

Add a prod stage served on the brand's own domainoscabrielbefore adoption78a5062

lines 19–23why, one step back
19 declaredStage,
20 publicStages,21 resources,22} from "../../../infra/stages.ts";23import {

Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f

lines 24–25why, one step back
24 acceptDraft,
25 awaitDraft,

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 26–31why, one step back
26 awaitLanding,
27 claimAsk,28 initProject,29 LoopFailed,30 openAsk,31 personAt,

Merge build/1007oscabrielbefore adoption77fa97a

line 32why, one step back
32 projectIsFree,

Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f

line 33why, one step back
33 proposeDraft,

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 34–42why, one step back
34 pushAnswer,
35 pushOutOfScope,36 readLanding,37 readVerdictNote,38} from "./core-loop.ts";39import type { LoopStep, Patience } from "./core-loop.ts";4041export { LoopFailed, LoopStep } from "./core-loop.ts";42

Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf

lines 43–45why, one step back
43// A throwaway project would show on a public stage's front page.
44export class PublicStageRefused extends Schema.TaggedError<PublicStageRefused>()(45 "PublicStageRefused",

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 46–57why, one step back
46 { stage: Schema.String }
47) {48 override get message() {49 return `--loop makes a throwaway project, which would show on ${this.stage}'s front page: run it on a dev stage`;50 }51}5253export interface StepTiming {54 readonly step: LoopStep;55 readonly ms: number;56}57

Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f

lines 58–60why, one step back
58export interface DraftReport {
59 readonly model: string;60 readonly attempts: number;

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

line 61why, one step back
61 // ms

Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f

lines 62–65why, one step back
62 readonly ms: number;
63 readonly trailers: readonly string[];64}65

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 66–74why, one step back
66export interface SmokeLoopReport {
67 readonly project: ProjectName;68 readonly refusedWith: readonly string[];69 readonly landed: LandedSha;70 readonly author: string;71 readonly committer: string;72 readonly trailers: readonly string[];73 readonly noteFound: boolean;74 readonly timings: readonly StepTiming[];

Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f

line 75why, one step back
75 readonly draft?: DraftReport;

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 76–84why, one step back
76}
7778export interface LoopInput {79 readonly stage: string;80 readonly run: string;81 readonly origin: string;82 readonly decider: Redacted.Redacted;83 readonly answerer: Redacted.Redacted;84 readonly patience?: Patience;

Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f

line 85why, one step back
85 readonly draft?: boolean;

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 86–138why, one step back
86}
8788const PATIENCE: Patience = {89 every: Duration.seconds(2),90 within: Duration.minutes(10),91};9293const timed =94 (stage: string, step: LoopStep) =>95 <A, R>(96 self: Effect.Effect<A, LoopFailed, R>97 ): Effect.Effect<readonly [A, StepTiming], LoopFailed, R> =>98 Effect.timed(self).pipe(99 Effect.map(([elapsed, value]) => {100 const ms = Math.round(Duration.toMillis(elapsed));101102 return [value, { ms, step }] as const;103 }),104 Effect.tap(([, { ms }]) =>105 Console.log(`smoke ${stage}: loop ${step} passed in ${ms} ms`)106 )107 );108109const recordThrowaway = (stage: string, kind: Created["kind"], name: string) =>110 Effect.gen(function* () {111 const namespace = Option.map(112 declaredStage(stage),113 (declared) => resources(declared).projects114 );115116 const entry: Created = {117 at: yield* DateTime.now,118 by: "smoke",119 kind,120 name,121 stage,122 };123124 yield* recordCreated(125 kind === "repo" && Option.isSome(namespace)126 ? { ...entry, namespace: namespace.value }127 : entry128 );129 }).pipe(130 Effect.mapError(131 ({ message }) =>132 new LoopFailed({133 reason: `the throwaway ledger: ${message}`,134 step: kind === "project" ? "init" : "claim",135 })136 )137 );138

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 139–140why, one step back
139const projectOf = (decider: Handle, run: string) =>
140 Schema.decodeEffect(ProjectName)(`${decider}/smoke-${run}`).pipe(

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 141–143why, one step back
141 Effect.mapError(
142 () =>143 new LoopFailed({

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 144why, one step back
144 reason: `${decider}/smoke-${run} is no project name: a run is lowercase words joined by dashes`,

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 145–153why, one step back
145 step: "init",
146 })147 )148 );149150export const smokeLoop = (151 input: LoopInput152): Effect.Effect<153 SmokeLoopReport,

Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf

line 154why, one step back
154 LoopFailed | PublicStageRefused,

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 155–159why, one step back
155 ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem
156> =>157 Effect.gen(function* () {158 const { origin, stage } = input;159

Add a prod stage served on the brand's own domainoscabrielbefore adoption78a5062

line 160why, one step back
160 if (publicStages.has(stage)) {

Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf

line 161why, one step back
161 return yield* new PublicStageRefused({ stage });

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 162–195why, one step back
162 }
163164 const fs = yield* FileSystem.FileSystem;165166 const dir = yield* fs167 .makeTempDirectoryScoped({ prefix: `smoke-${input.run}-` })168 .pipe(169 Effect.mapError(170 ({ message }) =>171 new LoopFailed({172 reason: `a folder for the run: ${message}`,173 step: "people",174 })175 )176 );177178 const [{ answerer, decider }, people] = yield* timed(179 stage,180 "people"181 )(182 Effect.all({183 answerer: personAt({184 home: `${dir}/answerer`,185 origin,186 token: input.answerer,187 }),188 decider: personAt({189 home: `${dir}/decider`,190 origin,191 token: input.decider,192 }),193 })194 );195

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 196–197why, one step back
196 const project = yield* projectOf(decider.handle, input.run);
197

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

lines 198–200why, one step back
198 // Checked free, then recorded, then made: an init that makes the project
199 // and then fails still leaves it in the ledger, and a name someone else200 // holds never enters teardown's plan.

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 201–204why, one step back
201 const [, init] = yield* timed(
202 stage,203 "init"204 )(

Merge build/1007oscabrielbefore adoption77fa97a

lines 205–206why, one step back
205 Effect.gen(function* () {
206 yield* projectIsFree(decider, project);

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 207–208why, one step back
207 yield* recordThrowaway(stage, "project", keyOf(project));
208 yield* recordThrowaway(stage, "repo", keyOf(project));

Merge build/1007oscabrielbefore adoption77fa97a

lines 209–216why, one step back
209
210 return yield* initProject({211 answerer: answerer.handle,212 decider,213 dir: `${dir}/start`,214 project,215 });216 })

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 217–302why, one step back
217 );
218219 const [, opened] = yield* timed(220 stage,221 "open-ask"222 )(openAsk(decider, project));223224 const [claim, claimed] = yield* timed(225 stage,226 "claim"227 )(228 claimAsk(answerer, project).pipe(229 Effect.tap(({ fork }) => recordThrowaway(stage, "repo", fork))230 )231 );232233 const work = `${answerer.home}/answer`;234235 const [refusedWith, refused] = yield* timed(236 stage,237 "out-of-scope"238 )(pushOutOfScope({ answerer, claim, dir: work }));239240 const [head, answered] = yield* timed(241 stage,242 "answer"243 )(pushAnswer(answerer, work));244245 const [{ landed, read }, landing] = yield* timed(246 stage,247 "landing"248 )(249 Effect.gen(function* () {250 const sha = yield* awaitLanding({251 answerer,252 head,253 patience: input.patience ?? PATIENCE,254 project,255 });256257 const found = yield* readLanding({258 answerer,259 dir: work,260 landed: sha,261 origin,262 project,263 });264265 const wanted = [266 ["author", found.author, answerer.handle],267 // The platform commits every landing under its own name.268 ["committer", found.committer, Brand.name],269 ] as const;270271 for (const [what, got, expected] of wanted) {272 if (got !== expected) {273 return yield* new LoopFailed({274 reason: `the landing ${sha}'s ${what} is ${got}, not ${expected}`,275 step: "landing",276 });277 }278 }279280 const missing = [281 `Asked-by: ${decider.handle} `,282 `Signed-off-by: ${answerer.handle} `,283 ].filter(284 (prefix) => !found.trailers.some((line) => line.startsWith(prefix))285 );286287 if (missing.length > 0) {288 return yield* new LoopFailed({289 reason: `the landing ${sha} lacks ${missing.map((prefix) => prefix.trim()).join(" and ")}: its trailers are ${found.trailers.join("; ") || "none"}`,290 step: "landing",291 });292 }293294 return { landed: sha, read: found };295 })296 );297298 const [, noted] = yield* timed(299 stage,300 "verdict-note"301 )(readVerdictNote({ answerer, dir: work, head }));302

Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f

lines 303–314why, one step back
303 const timings = [
304 people,305 init,306 opened,307 claimed,308 refused,309 answered,310 landing,311 noted,312 ];313314 const report = {

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 315–320why, one step back
315 author: read.author,
316 committer: read.committer,317 landed,318 noteFound: true,319 project,320 refusedWith,

Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f

line 321why, one step back
321 timings,

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

line 322why, one step back
322 trailers: read.trailers,

Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f

lines 323–367why, one step back
323 };
324325 if (input.draft !== true) {326 return report;327 }328329 const [proposal, proposed] = yield* timed(330 stage,331 "draft-propose"332 )(proposeDraft(answerer, project));333334 const [ready, drafted] = yield* timed(335 stage,336 "draft-ready"337 )(338 awaitDraft({339 patience: input.patience ?? PATIENCE,340 project,341 proposal,342 reader: answerer,343 })344 );345346 const [trailers, accepted] = yield* timed(347 stage,348 "draft-accept"349 )(350 acceptDraft({351 decider,352 dir: `${dir}/start`,353 origin,354 project,355 proposal,356 proposer: answerer.handle,357 })358 );359360 return {361 ...report,362 draft: {363 ...ready,364 ms: proposed.ms + drafted.ms,365 trailers,366 },367 timings: [...timings, proposed, drafted, accepted],

Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6

lines 368–369why, one step back
368 };
369 }).pipe(Effect.scoped);