movers.ts
This file
Path
packages/project/src/answer-runs/movers.ts
132 lines · 3.6 KiB · TypeScript
Last changed by
Merge build/comments-project into mainoscabrielbefore adoption49efd2a
History of this fileTake it
Merge refusal/stale-base-movers into mainoscabrielbefore adoptiona23e097
lines 1–132why, one step back1import { RepoHost, Revision } from "@repo/artifacts/repo-host";2import type { Commit, RepoNotFound } from "@repo/artifacts/repo-host";3import type { LandingView } from "@repo/protocol/domain/landing";4import type {5 CommitSha,6 LandedSha,7 RepoName,8 RepoPath,9} from "@repo/protocol/ids";10import type { MovedBy } from "@repo/protocol/refusal";11import { Array, Cause, Effect, Equal, Option } from "effect";1213import { asOneView } from "../code/cache.ts";14import { entryAt, READS_AT_ONCE } from "../code/tree.ts";1516export const MOVER_SCAN = 50;1718export type LandingsOf = (19 shas: readonly CommitSha[]20) => Effect.Effect<readonly LandingView[]>;2122export interface Conflicted {23 readonly repo: RepoName;24 readonly base: CommitSha;25 readonly main: LandedSha;26 readonly conflicts: readonly RepoPath[];27}2829const idAt = (repo: RepoName, commit: Commit | undefined, path: RepoPath) =>30 commit === undefined31 ? Effect.succeedNone32 : Effect.map(33 entryAt(repo, commit.tree, path),34 Option.map(({ mode, sha }) => `${mode} ${sha}`)35 );3637const moverOf = (38 repo: RepoName,39 scan: readonly Commit[],40 since: number,41 path: RepoPath42): Effect.Effect<Option.Option<CommitSha>, RepoNotFound, RepoHost> =>43 Effect.map(44 Effect.forEach(45 Array.makeBy(since + 1, (at) => scan[at]),46 (commit) => idAt(repo, commit, path),47 { concurrency: READS_AT_ONCE }48 ),49 (ids) =>50 Array.findFirst(scan.slice(0, since), (commit, at) =>51 Equal.equals(ids[at], ids[at + 1])52 ? Option.none()53 : Option.some(commit.sha)54 )55 );5657const moversOf = (input: Conflicted) =>58 asOneView(59 Effect.gen(function* () {60 const scan = yield* RepoHost.use((host) =>61 host.log(62 input.repo,63 Revision.Commit({ sha: input.main }),64 MOVER_SCAN + 165 )66 );6768 const based = scan.findIndex(({ sha }) => sha === input.base);69 const since = based === -1 ? Math.min(scan.length, MOVER_SCAN) : based;7071 const found = yield* Effect.forEach(72 input.conflicts,73 (path) =>74 Effect.map(moverOf(input.repo, scan, since, path), (commit) =>75 Option.map(commit, (sha) => ({ path, sha }))76 ),77 { concurrency: READS_AT_ONCE }78 );7980 return Array.getSomes(found);81 })82 );8384const movedByOf = (path: RepoPath, landing: LandingView): MovedBy => {85 const ask = landing.ask ?? landing.opened;8687 return ask === undefined88 ? { author: landing.author, landing: landing.sha, path }89 : { ask, author: landing.author, landing: landing.sha, path };90};9192export const movedBy = (93 input: Conflicted,94 landings: LandingsOf95): Effect.Effect<readonly MovedBy[], never, RepoHost> =>96 Effect.gen(function* () {97 if (input.conflicts.length === 0) {98 return [];99 }100101 const movers = yield* moversOf(input);102103 if (movers.length === 0) {104 return [];105 }106107 const bySha = new Map<string, LandingView>(108 (yield* landings(Array.dedupe(movers.map(({ sha }) => sha)))).map(109 (landing) => [landing.sha, landing]110 )111 );112113 return movers.flatMap(({ path, sha }) => {114 const landing = bySha.get(sha);115116 return landing === undefined ? [] : [movedByOf(path, landing)];117 });118 }).pipe(119 // Who moved a path is a courtesy on the refusal, never a reason to120 // block it: an unreadable repo leaves it out.121 Effect.catchCause((cause) =>122 Cause.hasInterruptsOnly(cause)123 ? Effect.interrupt124 : Effect.as(125 Effect.logWarning(126 "who moved a merge's conflicting paths was not found",127 cause128 ),129 []130 )131 )132 );