Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestReviewGuideService.ts

523 lines19.8 KB

1import * as Cause from "effect/Cause";
2import * as Clock from "effect/Clock";
3import * as Context from "effect/Context";
4import * as DateTime from "effect/DateTime";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as Option from "effect/Option";
9import * as Ref from "effect/Ref";
10import * as Result from "effect/Result";
11import * as Semaphore from "effect/Semaphore";
12​
13import {
14 PullRequestOperationError,
15 type PullRequestDetail,
16 type PullRequestRef,
17 type PullRequestReviewGuide,
18 type PullRequestReviewGuideResult,
19 type PullRequestReviewGuideRun,
20 type PullRequestStack,
21 ProjectId,
22 type RepositoryIdentity,
23 type ThreadPullRequestKey,
24} from "@t3tools/contracts";
25import type { ReviewGuideStackLayer } from "@t3tools/provider-core/server/textGeneration";
26import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
27import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
28​
29import * as ServerConfig from "../config.ts";
30import * as PullRequestReviewGuides from "../persistence/PullRequestReviewGuides.ts";
31import * as ProjectService from "../project/ProjectService.ts";
32import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
33import * as ServerSettings from "../serverSettings.ts";
34import * as TextGeneration from "../textGeneration/TextGeneration.ts";
35import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
36import { PATCH_RENDER_PREFIX_ARGS } from "../vcs/GitVcsDriverCore.ts";
37import * as PullRequestService from "./PullRequestService.ts";
38import { pullRequestSyncKey } from "./pullRequestSyncKey.ts";
39import { reviewGuideInputFromPullRequest } from "./pullRequestReviewGuideInput.ts";
40import { normalizeReviewGuide, parseReviewGuideChange } from "./pullRequestReviewGuideNormalize.ts";
41​
42const COMMIT_HASH_PATTERN = /^[0-9a-f]{7,64}$/;
43​
44export class PullRequestReviewGuideService extends Context.Service<
45 PullRequestReviewGuideService,
46 {
47 readonly get: (
48 input: PullRequestRef,
49 ) => Effect.Effect<PullRequestReviewGuideResult, PullRequestService.PullRequestError>;
50 readonly generate: (
51 input: PullRequestRef,
52 ) => Effect.Effect<
53 PullRequestReviewGuideResult & { readonly started: boolean },
54 PullRequestService.PullRequestError
55 >;
56 }
57>()("t3/pullRequest/PullRequestReviewGuideService") {}
58​
59const keyString = (key: ThreadPullRequestKey) => `${key.host}/${key.repository}#${key.number}`;
60​
61const nowIso = Clock.currentTimeMillis.pipe(
62 Effect.map((millis) => DateTime.formatIso(DateTime.makeUnsafe(millis))),
63);
64​
65const make = Effect.gen(function* () {
66 const pullRequests = yield* PullRequestService.PullRequestService;
67 const guides = yield* PullRequestReviewGuides.PullRequestReviewGuideRepository;
68 const projects = yield* ProjectService.ProjectService;
69 const serverSettings = yield* ServerSettings.ServerSettingsService;
70 const providerRegistry = yield* ProviderRegistry.ProviderRegistry;
71 const textGeneration = yield* TextGeneration.TextGeneration;
72 const git = yield* GitVcsDriver.GitVcsDriver;
73 const fileSystem = yield* FileSystem.FileSystem;
74 const config = yield* ServerConfig.ServerConfig;
75 const serviceScope = yield* Effect.scope;
76 const runs = yield* Ref.make<ReadonlyMap<string, PullRequestReviewGuideRun>>(new Map());
77 let runCounter = 0;
78 // Serialize claims and guide writes so a newer claim cannot steal ownership between checking and storing.
79 const ownership = yield* Semaphore.make(1);
80​
81 const keyFor = Effect.fn("PullRequestReviewGuideService.keyFor")(function* (
82 input: PullRequestRef,
83 ) {
84 const project = Option.getOrUndefined(
85 yield* projects.getShell(input.projectId).pipe(Effect.orElseSucceed(() => Option.none())),
86 );
87 const key = pullRequestSyncKey(input, project?.repositoryIdentity ?? undefined);
88 if (key === null || project === undefined) {
89 return yield* new PullRequestOperationError({
90 operation: "reviewGuide",
91 detail: "Could not identify this pull request's host.",
92 });
93 }
94 return { key, workspaceRoot: project.workspaceRoot, identity: project.repositoryIdentity };
95 });
96​
97 const requireOwnRepository = Effect.fn("PullRequestReviewGuideService.requireOwnRepository")(
98 function* (key: ThreadPullRequestKey, identity: RepositoryIdentity | null | undefined) {
99 const own =
100 identity == null
101 ? null
102 : pullRequestSyncKey(
103 {
104 projectId: ProjectId.make("own"),
105 number: key.number,
106 repository: sourceControlDefinitions.repositorySelector(identity) ?? "",
107 },
108 identity,
109 );
110 if (
111 identity == null ||
112 own === null ||
113 own.host !== key.host ||
114 own.repository !== key.repository
115 ) {
116 return yield* new PullRequestOperationError({
117 operation: "generateReviewGuide",
118 detail:
119 "Guides are written from this project's checkout, and this pull request is in another repository. Write it from a project that is a checkout of that repository.",
120 });
121 }
122 return identity.locator;
123 },
124 );
125​
126 const readStored = (key: ThreadPullRequestKey) =>
127 guides.get(key).pipe(
128 Effect.mapError(
129 (cause) =>
130 new PullRequestOperationError({
131 operation: "reviewGuide",
132 detail: "Could not read the stored review guide.",
133 cause,
134 }),
135 ),
136 Effect.map(Option.getOrNull),
137 );
138​
139 const get: PullRequestReviewGuideService["Service"]["get"] = Effect.fn(
140 "PullRequestReviewGuideService.get",
141 )(function* (input) {
142 const { key } = yield* keyFor(input);
143 const [guide, detail, current] = yield* Effect.all(
144 [readStored(key), pullRequests.detail(input), Ref.get(runs)],
145 { concurrency: "unbounded" },
146 );
147 return {
148 guide,
149 run: current.get(keyString(key)) ?? null,
150 currentHeadSha: detail.headSha ?? null,
151 };
152 });
153​
154 const checkoutHead = Effect.fn("PullRequestReviewGuideService.checkoutHead")(function* (
155 workspaceRoot: string,
156 locator: RepositoryIdentity["locator"],
157 detail: PullRequestDetail,
158 headSha: string,
159 stack: PullRequestStack | null,
160 runId: string,
161 ) {
162 // Identity names can be stale; match effective fetch URLs, including insteadOf rewrites.
163 const names = yield* git
164 .execute({
165 operation: "PullRequestReviewGuideService.listRemotes",
166 cwd: workspaceRoot,
167 args: ["remote"],
168 })
169 .pipe(Effect.map((result) => result.stdout.split("\n").filter((name) => name.length > 0)));
170 const remote = yield* Effect.findFirst(
171 [locator.remoteName, ...names.filter((name) => name !== locator.remoteName)],
172 (name) =>
173 git
174 .execute({
175 operation: "PullRequestReviewGuideService.remoteUrl",
176 cwd: workspaceRoot,
177 args: ["remote", "get-url", "--", name],
178 })
179 .pipe(
180 Effect.map((result) => result.stdout.trim() === locator.remoteUrl),
181 Effect.orElseSucceed(() => false),
182 ),
183 ).pipe(Effect.map(Option.getOrUndefined));
184 if (remote === undefined) {
185 return yield* new PullRequestOperationError({
186 operation: "generateReviewGuide",
187 detail: "This project's checkout no longer has a remote for the pull request's repository.",
188 });
189 }
190 const fetch = (args: ReadonlyArray<string>) =>
191 git.execute({
192 operation: "PullRequestReviewGuideService.fetch",
193 cwd: workspaceRoot,
194 args: ["fetch", "--quiet", "--no-tags", "--", remote, ...args],
195 timeoutMs: 5 * 60_000,
196 });
197 // Reject invalid branch names and refspec operators before fetching host-provided names.
198 yield* git
199 .execute({
200 operation: "PullRequestReviewGuideService.checkBaseBranch",
201 cwd: workspaceRoot,
202 args: ["check-ref-format", "--branch", detail.baseBranch],
203 })
204 .pipe(
205 Effect.filterOrFail(
206 () => !detail.baseBranch.startsWith("+") && !detail.baseBranch.includes(":"),
207 () =>
208 new PullRequestOperationError({
209 operation: "generateReviewGuide",
210 detail: "The pull request's base branch name is not one git can fetch.",
211 }),
212 ),
213 );
214 // FETCH_HEAD is workspace-global; isolate this run's base from concurrent fetches.
215 const baseRef = `refs/t3/review-guide/${runId}/base`;
216 yield* fetch([`+refs/heads/${detail.baseBranch}:${baseRef}`]);
217 yield* Effect.addFinalizer(() =>
218 git
219 .execute({
220 operation: "PullRequestReviewGuideService.dropBaseRef",
221 cwd: workspaceRoot,
222 args: ["update-ref", "-d", baseRef],
223 })
224 .pipe(Effect.ignore({ log: true })),
225 );
226 const baseTip = yield* git.resolveCommit({ cwd: workspaceRoot, revision: baseRef });
227 const fetchHead = Effect.fn("PullRequestReviewGuideService.fetchHead")(function* (
228 number: number,
229 reportedSha: string,
230 branch: string,
231 ) {
232 const sha = reportedSha.toLowerCase();
233 if (!COMMIT_HASH_PATTERN.test(sha)) {
234 return yield* new PullRequestOperationError({
235 operation: "generateReviewGuide",
236 detail: `The host reported a head for #${number} that is not a commit hash.`,
237 });
238 }
239 // rev-parse prefers same-named branches/tags over short hashes.
240 const resolveHead = git.resolveCommit({ cwd: workspaceRoot, revision: sha }).pipe(
241 Effect.filterOrFail(
242 ({ commitSha }) => commitSha.startsWith(sha),
243 ({ commitSha }) =>
244 new PullRequestOperationError({
245 operation: "generateReviewGuide",
246 detail: `${sha} names ${commitSha} here, a branch or tag rather than the commit.`,
247 }),
248 ),
249 );
250 const fetchAndResolve = (ref: string) => fetch([ref]).pipe(Effect.andThen(resolveHead));
251 const headRef = sourceControlDefinitions.get(detail.provider).changeRequestHeadRef?.(number);
252 const branchRef = `refs/heads/${branch}`;
253 const checkBranch = git.execute({
254 operation: "PullRequestReviewGuideService.checkHeadBranch",
255 cwd: workspaceRoot,
256 args: ["check-ref-format", branchRef],
257 });
258 const sources = [
259 { source: "local objects", head: resolveHead },
260 ...(sha.length === 40 || sha.length === 64
261 ? [{ source: "the commit", head: fetchAndResolve(sha) }]
262 : []),
263 {
264 source: `branch ${branch}`,
265 head: checkBranch.pipe(Effect.andThen(fetchAndResolve(branchRef))),
266 },
267 ...(headRef === undefined ? [] : [{ source: headRef, head: fetchAndResolve(headRef) }]),
268 ];
269 const failures: Array<string> = [];
270 for (const { source, head } of sources) {
271 const result = yield* Effect.result(head);
272 if (Result.isSuccess(result)) return result.success;
273 failures.push(`${source}: ${result.failure.detail}`);
274 }
275 return yield* new PullRequestOperationError({
276 operation: "generateReviewGuide",
277 detail: `Could not find head ${sha} of #${number}. ${failures.join(" ")}`,
278 });
279 });
280 const resolvedHead = yield* fetchHead(detail.number, headSha, detail.headBranch);
281 const layers: ReadonlyArray<ReviewGuideStackLayer> = yield* Effect.forEach(
282 stack?.layers ?? [],
283 (layer) => {
284 const described = { number: layer.number, title: layer.title ?? null, state: layer.state };
285 if (layer.number === detail.number) return Effect.succeed({ ...described, headSha });
286 const sha = layer.headSha;
287 if (sha === undefined) return Effect.succeed({ ...described, headSha: null });
288 return fetchHead(layer.number, sha, layer.headBranch).pipe(
289 Effect.map(({ commitSha }) => ({ ...described, headSha: commitSha })),
290 Effect.catch((error) =>
291 Effect.logInfo("Review guide could not read a stack layer's head", {
292 layer: layer.number,
293 detail: error.detail,
294 }).pipe(Effect.as({ ...described, headSha: null })),
295 ),
296 );
297 },
298 { concurrency: 4 },
299 );
300 const directory = yield* fileSystem.makeTempDirectoryScoped({
301 directory: config.worktreesDir,
302 prefix: "review-guide-",
303 });
304 const path = `${directory}/checkout`;
305 yield* git.execute({
306 operation: "PullRequestReviewGuideService.addWorktree",
307 cwd: workspaceRoot,
308 args: ["worktree", "add", "--detach", "--quiet", path, resolvedHead.commitSha],
309 timeoutMs: 5 * 60_000,
310 });
311 yield* Effect.addFinalizer(() =>
312 git
313 .removeWorktree({ cwd: workspaceRoot, path, force: true })
314 .pipe(Effect.ignore({ log: true })),
315 );
316 const mergeBase = yield* git.execute({
317 operation: "PullRequestReviewGuideService.mergeBase",
318 cwd: path,
319 args: ["merge-base", baseTip.commitSha, "HEAD"],
320 });
321 return { path, baseCommit: mergeBase.stdout.trim(), stack: layers };
322 });
323​
324 const readChange = Effect.fn("PullRequestReviewGuideService.readChange")(function* (
325 cwd: string,
326 baseCommit: string,
327 ) {
328 const diff = (args: ReadonlyArray<string>) =>
329 git
330 .execute({
331 operation: "PullRequestReviewGuideService.readChange",
332 cwd,
333 // Override user diff prefixes so the parser can find headers.
334 args: [
335 "-c",
336 "core.quotePath=false",
337 "diff",
338 ...PATCH_RENDER_PREFIX_ARGS,
339 ...args,
340 `${baseCommit}...HEAD`,
341 ],
342 timeoutMs: 60_000,
343 maxOutputBytes: 64 * 1024 * 1024,
344 })
345 .pipe(Effect.map((result) => result.stdout));
346 const [nameOnly, patch] = yield* Effect.all(
347 [diff(["--name-only", "-z"]), diff(["--unified=0", "--no-color", "--no-ext-diff"])],
348 { concurrency: "unbounded" },
349 );
350 return parseReviewGuideChange(nameOnly, patch);
351 });
352​
353 const write = Effect.fn("PullRequestReviewGuideService.write")(function* (
354 input: PullRequestRef,
355 key: ThreadPullRequestKey,
356 workspaceRoot: string,
357 locator: RepositoryIdentity["locator"],
358 detail: PullRequestDetail,
359 headSha: string,
360 runId: string,
361 owns: Effect.Effect<boolean>,
362 ) {
363 const [activity, settings, providers, stack] = yield* Effect.all(
364 [
365 pullRequests.activity(input),
366 serverSettings.getSettings,
367 providerRegistry.getProviders,
368 pullRequests.stack(input).pipe(Effect.orElseSucceed(() => null)),
369 ],
370 { concurrency: "unbounded" },
371 );
372 const projectSettings = resolveProjectSettings(settings, input.projectId).settings;
373 const modelSelection = ServerSettings.resolveReviewGuideModelSelection(
374 projectSettings,
375 providers,
376 input.projectId,
377 );
378 const { generated, change } = yield* checkoutHead(
379 workspaceRoot,
380 locator,
381 detail,
382 headSha,
383 stack,
384 runId,
385 ).pipe(
386 Effect.flatMap((checkout) =>
387 Effect.all(
388 {
389 generated: textGeneration.generateReviewGuide({
390 ...reviewGuideInputFromPullRequest(detail, activity, checkout.baseCommit),
391 cwd: checkout.path,
392 modelSelection,
393 instructions: projectSettings.reviewGuideInstructions,
394 stack: checkout.stack,
395 }),
396 change: readChange(checkout.path, checkout.baseCommit),
397 },
398 { concurrency: "unbounded" },
399 ),
400 ),
401 Effect.scoped,
402 );
403 const guide: PullRequestReviewGuide = {
404 headSha,
405 generatedAt: yield* nowIso,
406 modelSelection,
407 ...normalizeReviewGuide(generated, change),
408 };
409 yield* ownership.withPermits(1)(
410 Effect.gen(function* () {
411 if (yield* owns) yield* guides.put(key, guide);
412 }),
413 );
414 });
415​
416 const failureDetail = (cause: Cause.Cause<unknown>): string => {
417 const error = Cause.squash(cause);
418 if (error instanceof Error && "detail" in error && typeof error.detail === "string") {
419 return error.detail;
420 }
421 return "The review guide could not be written.";
422 };
423​
424 const generate: PullRequestReviewGuideService["Service"]["generate"] = Effect.fn(
425 "PullRequestReviewGuideService.generate",
426 )(function* (input) {
427 const settings = yield* serverSettings.getSettings.pipe(
428 Effect.mapError(
429 (cause) =>
430 new PullRequestOperationError({
431 operation: "generateReviewGuide",
432 detail: "Could not read the server settings.",
433 cause,
434 }),
435 ),
436 );
437 if (!settings.reviewGuidesEnabled) {
438 return yield* new PullRequestOperationError({
439 operation: "generateReviewGuide",
440 detail: "Review guides are off. Turn them on in Settings → Source Control.",
441 });
442 }
443 const { key, workspaceRoot, identity } = yield* keyFor(input);
444 const locator = yield* requireOwnRepository(key, identity);
445 const runKey = keyString(key);
446 yield* pullRequests.invalidate({ reference: input });
447 const [detail, stored] = yield* Effect.all([pullRequests.detail(input), readStored(key)], {
448 concurrency: "unbounded",
449 });
450 const currentHeadSha = detail.headSha ?? null;
451 if (currentHeadSha === null) {
452 return yield* new PullRequestOperationError({
453 operation: "generateReviewGuide",
454 detail: "This host did not report the pull request's head commit.",
455 });
456 }
457 const running: PullRequestReviewGuideRun = {
458 _tag: "Running",
459 headSha: currentHeadSha,
460 startedAt: yield* nowIso,
461 };
462 // Claim atomically and re-read the head so stale clicks cannot replace a newer head's run.
463 const claimed = yield* ownership.withPermits(1)(
464 pullRequests.detail(input).pipe(
465 Effect.flatMap((latest) =>
466 (latest.headSha ?? null) !== currentHeadSha
467 ? Effect.fail(
468 new PullRequestOperationError({
469 operation: "generateReviewGuide",
470 detail: "The pull request changed while the guide was starting. Try again.",
471 }),
472 )
473 : Ref.modify(runs, (current) => {
474 const existing = current.get(runKey);
475 if (existing?._tag === "Running" && existing.headSha === currentHeadSha) {
476 return [existing, current] as const;
477 }
478 return [running, new Map(current).set(runKey, running)] as const;
479 }),
480 ),
481 ),
482 );
483 if (claimed !== running) return { guide: stored, run: claimed, currentHeadSha, started: false };
484​
485 const owns = Ref.get(runs).pipe(Effect.map((current) => current.get(runKey) === running));
486 const finish = (next: PullRequestReviewGuideRun | null) =>
487 Ref.update(runs, (current) => {
488 if (current.get(runKey) !== running) return current;
489 const updated = new Map(current);
490 if (next === null) updated.delete(runKey);
491 else updated.set(runKey, next);
492 return updated;
493 });
494 const runId = `${runCounter++}`;
495 yield* write(input, key, workspaceRoot, locator, detail, currentHeadSha, runId, owns).pipe(
496 Effect.andThen(finish(null)),
497 Effect.catchCause((cause) =>
498 Cause.hasInterruptsOnly(cause)
499 ? finish(null)
500 : Effect.logWarning("Review guide generation failed", {
501 key: runKey,
502 cause: Cause.pretty(cause),
503 }).pipe(
504 Effect.andThen(nowIso),
505 Effect.flatMap((failedAt) =>
506 finish({
507 _tag: "Failed",
508 headSha: currentHeadSha,
509 failedAt,
510 detail: failureDetail(cause),
511 }),
512 ),
513 ),
514 ),
515 Effect.forkIn(serviceScope),
516 );
517 return { guide: stored, run: running, currentHeadSha, started: true };
518 });
519​
520 return PullRequestReviewGuideService.of({ get, generate });
521});
522​
523export const layer = Layer.effect(PullRequestReviewGuideService, make);