steps.ts
This file
Path
packages/project/src/answer-runs/steps.ts
318 lines · 9.4 KiB · TypeScript
Last changed by
Merge build/comments-project into mainoscabrielbefore adoption49efd2a
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge refusal/stale-base-movers into mainoscabrielbefore adoptiona23e097
line 1why, one step back1import type { RepoHost } from "@repo/artifacts/repo-host";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 2–20why, one step back2import { CheckAt, CheckRunner } from "@repo/checks/check-runner";3import type { CheckLane, CheckRun } from "@repo/checks/check-runner";4import { classifyFailure } from "@repo/gate/classify";5import type { LaneOutcome, LaneResult } from "@repo/protocol/domain/answer";6import { LandedSha } from "@repo/protocol/ids";7import type { LaneName, RepoPath, RequestId } from "@repo/protocol/ids";8import type { Refusal, VerifierBlocked } from "@repo/protocol/refusal";9import { Array, Effect, Option } from "effect";1011import { runBlockOf } from "../answer-runs.ts";12import type { AnswerQueue, AnswerReport, AnswerRun } from "../answer-runs.ts";13import { noRunEnv } from "../env-port.ts";14import { Lander, Rebuild } from "../lander.ts";15import {16 LandingCommand,17 LandingEffect,18 LandingState,19 pendingEffects,20} from "../landing.ts";Merge refusal/stale-base-movers into mainoscabrielbefore adoptiona23e097
lines 21–22why, one step back21import { movedBy } from "./movers.ts";22import type { LandingsOf } from "./movers.ts";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 23–33why, one step back2324export interface Reported {25 readonly command: LandingCommand;26 readonly report?: AnswerReport;27}2829export const isStep = (effect: LandingEffect) =>30 LandingEffect.$is("Merge")(effect) ||31 LandingEffect.$is("RunChecks")(effect) ||32 LandingEffect.$is("Land")(effect);33Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 34why, one step back34/** A land runs only while the request holds the project's landing slot. */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 35–55why, one step back35export const nextSteps = (run: AnswerRun): readonly LandingEffect[] =>36 LandingState.guards.Landing(run.state) && !run.admitted37 ? []38 : pendingEffects(run.state);3940const attemptOf = (state: LandingState) =>41 LandingState.matchOrElse(42 state,43 {44 Checking: ({ attempt }) => attempt,45 Landing: ({ attempt }) => attempt,46 Merging: ({ attempt }) => attempt,47 },48 () => 049 );5051const reported = (command: LandingCommand, report?: AnswerReport): Reported =>52 report === undefined ? { command } : { command, report };5354const merge = (55 run: AnswerRun,Merge refusal/stale-base-movers into mainoscabrielbefore adoptiona23e097
lines 56–57why, one step back56 step: Extract<LandingEffect, { readonly _tag: "Merge" }>,57 landings: LandingsOfMerge 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 58–72why, one step back58) =>59 Lander.use((lander) =>60 lander.merge({61 base: step.base,62 fork: step.fork,63 head: step.head,64 main: step.main,65 repo: run.repo,66 })67 ).pipe(68 Effect.map(({ changes, tree }) =>69 reported(LandingCommand.Merged({ changes, main: step.main, tree }))70 ),71 Effect.catchTags({72 StaleBase: ({ conflicts }) =>Merge refusal/stale-base-movers into mainoscabrielbefore adoptiona23e097
lines 73–87why, one step back73 Effect.map(74 movedBy(75 { base: step.base, conflicts, main: step.main, repo: run.repo },76 landings77 ),78 (moved) =>79 reported(80 Array.isReadonlyArrayNonEmpty(moved)81 ? LandingCommand.MergeConflict({82 conflicts,83 main: step.main,84 movedBy: moved,85 })86 : LandingCommand.MergeConflict({ conflicts, main: step.main })87 )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 88–104why, one step back88 ),89 VerifierBlocked: () =>90 Effect.succeed(91 reported(LandingCommand.Blocked({ attempt: attemptOf(run.state) }))92 ),93 })94 );9596const resultsOf = (run: CheckRun): ReadonlyMap<LaneName, LaneResult> =>97 new Map(run.lanes.map((lane) => [lane.lane, lane]));9899const isTrouble = (result: LaneOutcome) =>100 result === "flaked" || result === "blocked";101102const logRefOf = (requestId: RequestId, lane: LaneName) =>103 `${requestId}/${lane}`;104Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 105why, one step back105/** Every lane every time; a failing project lane is run on main too, since red on main is the gate's trouble. The ask's own check is red on main by design, so it passes there. */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 106–115why, one step back106const checks = (107 run: AnswerRun,108 checking: Extract<LandingState, { readonly _tag: "Checking" }>109): Effect.Effect<Reported, VerifierBlocked, CheckRunner> =>110 Effect.gen(function* () {111 const runner = yield* CheckRunner;112 const { request } = checking;113114 const block = yield* runBlockOf(run);115Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 116why, one step back116 // A Checker fetches the repo a job names: the merged tree lives in the answer's fork (the merge ref), main in the project's repo.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 117–122why, one step back117 const job = (at: CheckAt, lanes: readonly CheckLane[]) =>118 runner.run({119 ...(run.env ?? noRunEnv),120 at,121 lanes,122 project: request.project,Check a decider's own landing in the fork that holds its merged tree, and intercept at most 60 proxy hostsoscabrielbefore adoption191b3b5
line 123why, one step back123 repo: CheckAt.guards.Tree(at) ? request.fork : run.repo,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 124–245why, one step back124 run: block,125 });126127 const lanesNamed = (names: ReadonlySet<LaneName>) =>128 run.lanes.filter(({ lane }) => names.has(lane));129130 const onTree = CheckAt.cases.Tree.make({ tree: checking.tree });131 const merged = yield* job(onTree, run.lanes);132 const failing = merged.lanes.filter(({ result }) => result !== "passed");133134 const troubled = new Set(135 failing.filter(({ result }) => isTrouble(result)).map(({ lane }) => lane)136 );137138 const onMainNeeded = new Set(139 failing.map(({ lane }) => lane).filter((lane) => lane !== run.askLane)140 );141142 const rerun =143 troubled.size === 0144 ? new Map<LaneName, LaneResult>()145 : resultsOf(yield* job(onTree, lanesNamed(troubled)));146147 const onMain =148 onMainNeeded.size === 0149 ? new Map<LaneName, LaneResult>()150 : resultsOf(151 yield* job(152 CheckAt.cases.Commit.make({ commit: checking.main }),153 lanesNamed(onMainNeeded)154 )155 );156157 const changed = new Set<RepoPath>(checking.changes.map(({ path }) => path));158159 const refusals = Array.getSomes(160 failing.map((lane) =>161 classifyFailure(162 {163 command: lane.command,164 failingPaths: [],165 isDoctor: false,166 lane: lane.lane,167 logRef: lane.logRef ?? logRefOf(request.requestId, lane.lane),168 onMain: onMainNeeded.has(lane.lane)169 ? (onMain.get(lane.lane)?.result ?? "blocked")170 : "passed",171 onMerged: lane.result,172 rerun: Option.map(173 Option.fromUndefinedOr(rerun.get(lane.lane)),174 ({ result }) => result175 ),176 },177 changed,178 { base: request.base, main: checking.main }179 )180 )181 );182183 const lanes = merged.lanes.map((lane) => rerun.get(lane.lane) ?? lane);184185 return reported(186 LandingCommand.Checked({187 decisions: [],188 finish: run.finish,189 refusals: refusals satisfies readonly Refusal[],190 tree: checking.tree,191 }),192 { heldBack: merged.heldBack, lanes }193 );194 });195196const land = (197 run: AnswerRun,198 landing: Extract<LandingState, { readonly _tag: "Landing" }>,199 message: string200) =>201 Lander.use((lander) =>202 lander.land({203 author: landing.request.answerer,204 main: landing.main,205 message,206 rebuild: Rebuild.Merge({207 base: landing.request.base,208 fork: landing.request.fork,209 head: landing.request.head,210 }),211 repo: run.repo,212 requestId: landing.request.requestId,213 tree: landing.tree,214 verdict: Option.fromUndefinedOr(run.verdict),215 })216 ).pipe(217 Effect.map(({ pending, sha }) =>218 reported(219 LandingCommand.LandedOk({ sha }),220 Array.isReadonlyArrayNonEmpty(pending) ? { pending } : undefined221 )222 ),223 Effect.catchTags({224 RepoBusy: () =>225 Effect.succeed(226 reported(LandingCommand.Busy({ attempt: landing.attempt }))227 ),228 StaleBase: ({ main }) =>229 Effect.succeed(230 reported(231 LandingCommand.MainMoved({232 from: landing.main,233 main: LandedSha.make(main),234 })235 )236 ),237 VerifierBlocked: () =>238 Effect.succeed(239 reported(LandingCommand.Blocked({ attempt: landing.attempt }))240 ),241 })242 );243244export const runStep = (245 run: AnswerRun,Merge refusal/stale-base-movers into mainoscabrielbefore adoptiona23e097
lines 246–252why, one step back246 effect: LandingEffect,247 landings: LandingsOf248): Effect.Effect<249 Option.Option<Reported>,250 never,251 CheckRunner | Lander | RepoHost252> =>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 253–258why, one step back253 LandingEffect.$match(effect, {254 Emit: () => Effect.succeedNone,255 Land: () =>256 LandingState.guards.Landing(run.state) && run.message !== undefined257 ? Effect.asSome(land(run, run.state, run.message))258 : Effect.succeedNone,Merge refusal/stale-base-movers into mainoscabrielbefore adoptiona23e097
line 259why, one step back259 Merge: (merging) => Effect.asSome(merge(run, merging, landings)),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 260–279why, one step back260 Record: () => Effect.succeedNone,261 Retry: () => Effect.succeedNone,262 RunChecks: () =>263 LandingState.guards.Checking(run.state)264 ? checks(run, run.state).pipe(265 Effect.catchTag("VerifierBlocked", () =>266 Effect.succeed(267 reported(268 LandingCommand.Blocked({ attempt: attemptOf(run.state) })269 )270 )271 ),272 Effect.asSome273 )274 : Effect.succeedNone,275 });276277export const drive = (278 queue: AnswerQueue,279 requestId: RequestIdMerge refusal/stale-base-movers into mainoscabrielbefore adoptiona23e097
line 280why, one step back280): Effect.Effect<void, never, CheckRunner | Lander | RepoHost> => {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 281–282why, one step back281 const go = (282 steps: readonly LandingEffect[]Merge refusal/stale-base-movers into mainoscabrielbefore adoptiona23e097
line 283why, one step back283 ): Effect.Effect<void, never, CheckRunner | Lander | RepoHost> =>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 284–296why, one step back284 Effect.gen(function* () {285 const effect = steps.find(isStep);286287 if (effect === undefined || steps.some(LandingEffect.$is("Retry"))) {288 return;289 }290291 const run = yield* queue.answer(requestId);292293 if (Option.isNone(run)) {294 return;295 }296Merge refusal/stale-base-movers into mainoscabrielbefore adoptiona23e097
line 297why, one step back297 const step = yield* runStep(run.value, effect, queue.landings);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 298–318why, one step back298299 if (Option.isNone(step)) {300 return;301 }302303 const advanced = yield* queue.advanceAnswer(304 requestId,305 step.value.command,306 step.value.report307 );308309 yield* go(advanced.effects);310 });311312 return Effect.flatMap(queue.answer(requestId), (run) =>313 Option.match(run, {314 onNone: () => Effect.void,315 onSome: (found) => go(nextSteps(found)),316 })317 );318};