Open History to see the last commit
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 [
348 diff(["--name-only", "-z"]),
349 diff(["--unified=0", "--no-color", "--no-ext-diff", "--no-textconv"]),
350 ],
351 { concurrency: "unbounded" },
352 );
353 return parseReviewGuideChange(nameOnly, patch);
354 });
355
356 const write = Effect.fn("PullRequestReviewGuideService.write")(function* (
357 input: PullRequestRef,
358 key: ThreadPullRequestKey,
359 workspaceRoot: string,
360 locator: RepositoryIdentity["locator"],
361 detail: PullRequestDetail,
362 headSha: string,
363 runId: string,
364 owns: Effect.Effect<boolean>,
365 ) {
366 const [activity, settings, providers, stack] = yield* Effect.all(
367 [
368 pullRequests.activity(input),
369 serverSettings.getSettings,
370 providerRegistry.getProviders,
371 pullRequests.stack(input).pipe(Effect.orElseSucceed(() => null)),
372 ],
373 { concurrency: "unbounded" },
374 );
375 const projectSettings = resolveProjectSettings(settings, input.projectId).settings;
376 const modelSelection = ServerSettings.resolveReviewGuideModelSelection(
377 projectSettings,
378 providers,
379 input.projectId,
380 );
381 const { generated, change } = yield* checkoutHead(
382 workspaceRoot,
383 locator,
384 detail,
385 headSha,
386 stack,
387 runId,
388 ).pipe(
389 Effect.flatMap((checkout) =>
390 Effect.all(
391 {
392 generated: textGeneration.generateReviewGuide({
393 ...reviewGuideInputFromPullRequest(detail, activity, checkout.baseCommit),
394 cwd: checkout.path,
395 modelSelection,
396 instructions: projectSettings.reviewGuideInstructions,
397 stack: checkout.stack,
398 }),
399 change: readChange(checkout.path, checkout.baseCommit),
400 },
401 { concurrency: "unbounded" },
402 ),
403 ),
404 Effect.scoped,
405 );
406 const guide: PullRequestReviewGuide = {
407 headSha,
408 generatedAt: yield* nowIso,
409 modelSelection,
410 ...normalizeReviewGuide(generated, change),
411 };
412 yield* ownership.withPermits(1)(
413 Effect.gen(function* () {
414 if (yield* owns) yield* guides.put(key, guide);
415 }),
416 );
417 });
418
419 const failureDetail = (cause: Cause.Cause<unknown>): string => {
420 const error = Cause.squash(cause);
421 if (error instanceof Error && "detail" in error && typeof error.detail === "string") {
422 return error.detail;
423 }
424 return "The review guide could not be written.";
425 };
426
427 const generate: PullRequestReviewGuideService["Service"]["generate"] = Effect.fn(
428 "PullRequestReviewGuideService.generate",
429 )(function* (input) {
430 const settings = yield* serverSettings.getSettings.pipe(
431 Effect.mapError(
432 (cause) =>
433 new PullRequestOperationError({
434 operation: "generateReviewGuide",
435 detail: "Could not read the server settings.",
436 cause,
437 }),
438 ),
439 );
440 if (!settings.reviewGuidesEnabled) {
441 return yield* new PullRequestOperationError({
442 operation: "generateReviewGuide",
443 detail: "Review guides are off. Turn them on in Settings → Source Control.",
444 });
445 }
446 const { key, workspaceRoot, identity } = yield* keyFor(input);
447 const locator = yield* requireOwnRepository(key, identity);
448 const runKey = keyString(key);
449 yield* pullRequests.invalidate({ reference: input });
450 const [detail, stored] = yield* Effect.all([pullRequests.detail(input), readStored(key)], {
451 concurrency: "unbounded",
452 });
453 const currentHeadSha = detail.headSha ?? null;
454 if (currentHeadSha === null) {
455 return yield* new PullRequestOperationError({
456 operation: "generateReviewGuide",
457 detail: "This host did not report the pull request's head commit.",
458 });
459 }
460 const running: PullRequestReviewGuideRun = {
461 _tag: "Running",
462 headSha: currentHeadSha,
463 startedAt: yield* nowIso,
464 };
465 // Claim atomically and re-read the head so stale clicks cannot replace a newer head's run.
466 const claimed = yield* ownership.withPermits(1)(
467 pullRequests.detail(input).pipe(
468 Effect.flatMap((latest) =>
469 (latest.headSha ?? null) !== currentHeadSha
470 ? Effect.fail(
471 new PullRequestOperationError({
472 operation: "generateReviewGuide",
473 detail: "The pull request changed while the guide was starting. Try again.",
474 }),
475 )
476 : Ref.modify(runs, (current) => {
477 const existing = current.get(runKey);
478 if (existing?._tag === "Running" && existing.headSha === currentHeadSha) {
479 return [existing, current] as const;
480 }
481 return [running, new Map(current).set(runKey, running)] as const;
482 }),
483 ),
484 ),
485 );
486 if (claimed !== running) return { guide: stored, run: claimed, currentHeadSha, started: false };
487
488 const owns = Ref.get(runs).pipe(Effect.map((current) => current.get(runKey) === running));
489 const finish = (next: PullRequestReviewGuideRun | null) =>
490 Ref.update(runs, (current) => {
491 if (current.get(runKey) !== running) return current;
492 const updated = new Map(current);
493 if (next === null) updated.delete(runKey);
494 else updated.set(runKey, next);
495 return updated;
496 });
497 const runId = `${runCounter++}`;
498 yield* write(input, key, workspaceRoot, locator, detail, currentHeadSha, runId, owns).pipe(
499 Effect.andThen(finish(null)),
500 Effect.catchCause((cause) =>
501 Cause.hasInterruptsOnly(cause)
502 ? finish(null)
503 : Effect.logWarning("Review guide generation failed", {
504 key: runKey,
505 cause: Cause.pretty(cause),
506 }).pipe(
507 Effect.andThen(nowIso),
508 Effect.flatMap((failedAt) =>
509 finish({
510 _tag: "Failed",
511 headSha: currentHeadSha,
512 failedAt,
513 detail: failureDetail(cause),
514 }),
515 ),
516 ),
517 ),
518 Effect.forkIn(serviceScope),
519 );
520 return { guide: stored, run: running, currentHeadSha, started: true };
521 });
522
523 return PullRequestReviewGuideService.of({ get, generate });
524});
525
526export const layer = Layer.effect(PullRequestReviewGuideService, make);