co-maintainer 0.6.9 → 0.6.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/package.json +1 -1
  2. package/dist/src/ai/fake.d.ts +10 -1
  3. package/dist/src/ai/fake.js +39 -4
  4. package/dist/src/cli/args.js +2 -0
  5. package/dist/src/cli/command_json.d.ts +6 -0
  6. package/dist/src/cli/command_json.js +1 -0
  7. package/dist/src/cli/commands/probe.js +25 -0
  8. package/dist/src/cli/commands/registry.js +11 -0
  9. package/dist/src/cli/commands/review.js +5 -1
  10. package/dist/src/cli/commands/serve.js +4 -0
  11. package/dist/src/cli/commands/set.js +4 -2
  12. package/dist/src/cli/review_args.d.ts +9 -0
  13. package/dist/src/cli/review_args.js +36 -1
  14. package/dist/src/github/app.d.ts +4 -0
  15. package/dist/src/github/app.js +10 -1
  16. package/dist/src/github/permissions.js +40 -0
  17. package/dist/src/local/git_ops.js +3 -2
  18. package/dist/src/local/git_revision.d.ts +5 -2
  19. package/dist/src/local/git_revision.js +17 -10
  20. package/dist/src/pr/reviewer.d.ts +2 -0
  21. package/dist/src/pr/reviewer.js +1 -1
  22. package/dist/src/remote/push_client.d.ts +7 -0
  23. package/dist/src/remote/push_client.js +287 -0
  24. package/dist/src/remote/server/routes.d.ts +1 -0
  25. package/dist/src/remote/server/routes.js +61 -4
  26. package/dist/src/remote/server/sync.js +11 -3
  27. package/dist/src/remote/validate.js +17 -0
  28. package/dist/src/server/api/repos.js +13 -2
  29. package/dist/src/server/pages/layout.js +1 -0
  30. package/dist/src/server/pages/repo.d.ts +3 -1
  31. package/dist/src/server/pages/repo.js +4 -1
  32. package/dist/src/server/pages/repo_pushes.d.ts +7 -0
  33. package/dist/src/server/pages/repo_pushes.js +144 -0
  34. package/dist/src/server/pages/repo_settings.js +45 -1
  35. package/dist/src/server/pages/router.js +22 -3
  36. package/dist/src/server/webhook/index.js +8 -1
  37. package/dist/src/services/dashboard.d.ts +46 -0
  38. package/dist/src/services/dashboard.js +24 -0
  39. package/dist/src/services/jobs.js +10 -0
  40. package/dist/src/services/push_review.d.ts +51 -0
  41. package/dist/src/services/push_review.js +878 -0
  42. package/dist/src/services/setup.js +21 -0
  43. package/dist/src/services/watch_comments.d.ts +10 -0
  44. package/dist/src/services/watch_comments.js +179 -0
  45. package/dist/src/services/watch_issues.d.ts +70 -0
  46. package/dist/src/services/watch_issues.js +266 -0
  47. package/dist/src/services/watch_notes.d.ts +8 -0
  48. package/dist/src/services/watch_notes.js +80 -0
  49. package/dist/src/services/watch_status.d.ts +25 -0
  50. package/dist/src/services/watch_status.js +101 -0
  51. package/dist/src/services/webhook.js +107 -3
  52. package/dist/src/store/migrations.js +63 -0
  53. package/dist/src/store/repos.d.ts +1 -1
  54. package/dist/src/store/rows.d.ts +6 -0
  55. package/dist/src/store/watch.d.ts +108 -0
  56. package/dist/src/store/watch.js +186 -0
  57. package/dist/src/types.d.ts +2 -0
  58. package/dist/src/util/repo_name.d.ts +2 -0
  59. package/dist/src/util/repo_name.js +56 -0
  60. package/package.json +1 -1
@@ -0,0 +1,878 @@
1
+ /** Commit watch: review a push to the default branch.
2
+ *
3
+ * A push reaches the server two ways, and either may be the only one: the
4
+ * GitHub App's `push` webhook (when the owner turned that event on) or a CI
5
+ * step running `review --remote --range`. Both land in `queuePush`, which
6
+ * keys the push by its `after` sha, so when both report it there is one
7
+ * review and the CI step waits on it.
8
+ *
9
+ * The review runs on the server and does not depend on the CI step staying
10
+ * connected: a step that times out leaves the review running.
11
+ *
12
+ * Issues need the App. Without it a push review still runs and reports to
13
+ * CI, but opens, comments on and closes nothing. */
14
+ import { parseFindings } from "../pr/findings.js";
15
+ import { githubFileToRevisionFile } from "../pr/github_revision.js";
16
+ import { reviewWorkspaceRevision } from "../pr/reviewer.js";
17
+ import { anchorTextFromPatch, parsePreviousVerdicts, } from "../review/carry_over.js";
18
+ import { normalizeAnchor } from "../review/revision.js";
19
+ import { revisionFromSubmitJson } from "../remote/revision_from_submit.js";
20
+ import { revisionStats, sortResolvedFindings, summaryCounts, toJsonFinding, } from "../cli/review_result.js";
21
+ import { reviewBlockingFrom } from "../review/blocking.js";
22
+ import { withLogSink } from "../util/log.js";
23
+ import { redact } from "../util/redact.js";
24
+ import { readConfig, resolveAppPrivateKey } from "../config.js";
25
+ import { addResponseCost, costColumns, costUsage, emptyTally, settle, } from "../util/cost.js";
26
+ import { getRepo } from "../store/repos.js";
27
+ import { getJob, insertJob, listJobs, setJobStatus } from "../store/jobs.js";
28
+ import { insertFinding, listFindingsForReview } from "../store/findings.js";
29
+ import { getReview, getReviewByJobId, setReviewStatus, } from "../store/reviews.js";
30
+ import { addPushSource, deletePushInput, getOrCreatePushSubject, getPushEvent, getPushInput, insertPushEvent, insertPushReview, insertWatchIssue, listWatchIssues, movePushEvents, pushSources, savePushInput, savePushResult, setPushEventReview, updateWatchIssue, } from "../store/watch.js";
31
+ import { getAppDb } from "../store/app_db.js";
32
+ import { cancel, registerHandler } from "./jobs.js";
33
+ import { recordAiCost } from "./setup.js";
34
+ import { aiFor, clientFor, humanCopy, resolveInstallationId, reviewOptions, } from "./review.js";
35
+ import { changedLines, closeIssue, closedStillFoundComment, commentOnIssue, excerpt, fileAt, findingSummary, fixedComment, ISSUE_SEVERITIES, issueBody, issueTitle, lastCommitFor, locateAnchor, openIssue, pushRange, readIssueState, samePath, stillFoundComment, } from "./watch_issues.js";
36
+ export const PUSH_JOB = "push_review";
37
+ export const PUSH_CHECK_JOB = "push_check";
38
+ export const CHECK_NAME = "co-maintainer commit watch";
39
+ const SHA = /^[0-9a-f]{40}$/;
40
+ const ZERO = /^0{40}$/;
41
+ export function isSha(value) {
42
+ return typeof value === "string" && SHA.test(value);
43
+ }
44
+ export function isZeroSha(value) {
45
+ return ZERO.test(value);
46
+ }
47
+ export const NOT_INITIALIZED = "This repository is not initialized.";
48
+ function branchOf(ref) {
49
+ return ref.startsWith("refs/heads/") ? ref.slice("refs/heads/".length) : ref;
50
+ }
51
+ function appConfigured() {
52
+ const config = readConfig();
53
+ return Boolean(config.githubAppId && resolveAppPrivateKey(config));
54
+ }
55
+ /** The App client for a repository, or undefined when no App covers it. */
56
+ async function appClientFor(repo) {
57
+ if (!appConfigured())
58
+ return undefined;
59
+ try {
60
+ const id = await resolveInstallationId(repo);
61
+ return id === undefined ? undefined : clientFor(id);
62
+ }
63
+ catch {
64
+ return undefined;
65
+ }
66
+ }
67
+ /** True when the repository row says an App installation covers it. Cheap
68
+ * and synchronous, for the decisions `queuePush` makes before any call. */
69
+ function appCovers(repo) {
70
+ return appConfigured() && Boolean(getRepo(repo)?.installation_id);
71
+ }
72
+ function activePushJobs(repo) {
73
+ return [
74
+ ...listJobs({ status: "running", repo }),
75
+ ...listJobs({ status: "queued", repo }),
76
+ ].filter((job) => job.type === PUSH_JOB);
77
+ }
78
+ function enqueueSkippedCheck(repo, after) {
79
+ insertJob({
80
+ id: crypto.randomUUID(),
81
+ type: PUSH_CHECK_JOB,
82
+ repo,
83
+ args: { after, title: NOT_INITIALIZED },
84
+ });
85
+ }
86
+ /** Records a push and decides what happens to it. Synchronous, so the
87
+ * webhook and the remote submit route can call it as they are. */
88
+ export function queuePush(req) {
89
+ const row = getRepo(req.repo);
90
+ if (!row || row.active !== 1) {
91
+ return {
92
+ kind: "skipped",
93
+ reason: "repo-not-active",
94
+ message: "This repository is not active on the server.",
95
+ checkRun: false,
96
+ };
97
+ }
98
+ if (isZeroSha(req.before))
99
+ return { kind: "no_changes" };
100
+ const existing = getPushEvent(req.repo, req.after);
101
+ if (existing?.job_id) {
102
+ addPushSource(req.repo, req.after, req.source);
103
+ const review = existing.review_id
104
+ ? getReview(existing.review_id)
105
+ : undefined;
106
+ if (req.source === "ci" && review) {
107
+ return { kind: "attached", jobId: existing.job_id, reviewId: review.id };
108
+ }
109
+ // The App joins a review CI started. A queued or running review reads
110
+ // the sources again when it ends. One that already ended gets its check
111
+ // from a job of its own.
112
+ const sources = pushSources(existing);
113
+ if (req.source === "app" &&
114
+ review?.status === "done" &&
115
+ !review.check_run_id &&
116
+ !sources.includes("app")) {
117
+ insertJob({
118
+ id: crypto.randomUUID(),
119
+ type: PUSH_CHECK_JOB,
120
+ repo: req.repo,
121
+ args: { after: req.after, reviewId: review.id },
122
+ });
123
+ }
124
+ return {
125
+ kind: "skipped",
126
+ reason: "already-reviewed",
127
+ message: "This push is already reviewed.",
128
+ checkRun: false,
129
+ };
130
+ }
131
+ if (!row.knowledge_built_at) {
132
+ if (!existing) {
133
+ insertPushEvent({
134
+ repo: req.repo,
135
+ afterSha: req.after,
136
+ beforeSha: req.before,
137
+ ref: req.ref,
138
+ source: req.source,
139
+ });
140
+ }
141
+ const covered = appCovers(req.repo);
142
+ if (covered && !existing)
143
+ enqueueSkippedCheck(req.repo, req.after);
144
+ return {
145
+ kind: "skipped",
146
+ reason: "not-initialized",
147
+ message: NOT_INITIALIZED,
148
+ checkRun: covered,
149
+ };
150
+ }
151
+ let before = req.before;
152
+ const merged = row.watch_push_mode === "cancel-and-merge"
153
+ ? activePushJobs(req.repo).sort((a, b) => a.created_at.localeCompare(b.created_at))
154
+ : [];
155
+ if (merged.length > 0) {
156
+ // The merged review starts where the oldest folded one started.
157
+ const oldest = JSON.parse(merged[0].args);
158
+ if (oldest.before)
159
+ before = oldest.before;
160
+ if (req.revision && before !== req.before && !appCovers(req.repo)) {
161
+ return { kind: "extend", before };
162
+ }
163
+ }
164
+ const branch = branchOf(req.ref);
165
+ const subject = getOrCreatePushSubject(req.repo, branch);
166
+ const jobId = crypto.randomUUID();
167
+ const reviewId = crypto.randomUUID();
168
+ const db = getAppDb();
169
+ db.exec("BEGIN");
170
+ try {
171
+ if (!existing) {
172
+ insertPushEvent({
173
+ repo: req.repo,
174
+ afterSha: req.after,
175
+ beforeSha: req.before,
176
+ ref: req.ref,
177
+ source: req.source,
178
+ });
179
+ }
180
+ else {
181
+ addPushSource(req.repo, req.after, req.source);
182
+ }
183
+ insertPushReview({
184
+ id: reviewId,
185
+ subjectId: subject.id,
186
+ repo: req.repo,
187
+ branch,
188
+ jobId,
189
+ beforeSha: before,
190
+ afterSha: req.after,
191
+ model: readConfig().highModel ?? "unknown",
192
+ });
193
+ insertJob({
194
+ id: jobId,
195
+ type: PUSH_JOB,
196
+ repo: req.repo,
197
+ deliveryId: req.deliveryId,
198
+ queueKey: `push:${req.repo}:${req.after}`,
199
+ args: { reviewId, before, after: req.after, branch },
200
+ });
201
+ setPushEventReview(req.repo, req.after, reviewId, jobId);
202
+ // A client diff only covers its own range, so after a merge the server
203
+ // builds the merged diff from GitHub instead.
204
+ const clientDiffFits = !req.revision ? false : before === req.before;
205
+ if (req.revision && clientDiffFits) {
206
+ savePushInput(jobId, JSON.stringify(req.revision));
207
+ }
208
+ db.exec("COMMIT");
209
+ }
210
+ catch (error) {
211
+ db.exec("ROLLBACK");
212
+ throw error;
213
+ }
214
+ for (const job of merged) {
215
+ const old = getReviewByJobId(job.id);
216
+ if (old)
217
+ movePushEvents(old.id, reviewId, jobId);
218
+ if (job.status === "queued") {
219
+ setJobStatus(job.id, "canceled", {
220
+ superseded_by: jobId,
221
+ cancel_reason: "merged",
222
+ });
223
+ if (old)
224
+ setReviewStatus(old.id, "canceled");
225
+ deletePushInput(job.id);
226
+ }
227
+ else {
228
+ cancel(job.id, "merged");
229
+ }
230
+ }
231
+ return { kind: "enqueued", jobId, reviewId };
232
+ }
233
+ /** The diff of `before..after`, from the CI step when it sent one, otherwise
234
+ * from GitHub's compare API. */
235
+ async function pushRevision(job, client, before, after, branch) {
236
+ const input = getPushInput(job.id);
237
+ if (input) {
238
+ const revision = revisionFromSubmitJson(JSON.parse(input));
239
+ return { ...revision, baseLabel: revision.baseLabel || branch };
240
+ }
241
+ if (!client) {
242
+ throw new Error("this push has no diff: the CI step did not send one and no GitHub App covers the repository");
243
+ }
244
+ const compare = await client.request(`repos/${job.repo}/compare/${before}...${after}`);
245
+ const files = Array.isArray(compare.files) ? compare.files : [];
246
+ return {
247
+ files: files.map((file) => githubFileToRevisionFile(file)),
248
+ title: "",
249
+ description: "",
250
+ baseLabel: branch,
251
+ producer: "github",
252
+ };
253
+ }
254
+ /** Brings each tracked issue in line with GitHub (a maintainer may have
255
+ * closed or reopened it), then picks the ones this push review must judge:
256
+ * open issues whose lines the push changed, and each issue a maintainer
257
+ * closed that has not been checked once since. */
258
+ async function knownChecks(client, repo, revision, after, log) {
259
+ const checks = [];
260
+ const byPath = new Map(revision.files.map((file) => [file.path, file]));
261
+ for (const issue of listWatchIssues(repo)) {
262
+ // A renamed file takes its issue along, whatever the issue's state, so an
263
+ // ignored or closed finding is still recognized under the new name. That
264
+ // is following the code, not a verdict on it.
265
+ const renamed = revision.files.find((f) => f.status === "renamed" && samePath(f.previousPath, issue.path));
266
+ if (renamed) {
267
+ updateWatchIssue(issue.id, { path: renamed.path });
268
+ issue.path = renamed.path;
269
+ }
270
+ if (issue.state === "ignored")
271
+ continue;
272
+ if (issue.state === "open" || issue.state === "closed_by_user") {
273
+ const state = await readIssueState(client, repo, issue.issue_number);
274
+ if (state && issue.state === "open" && !state.open) {
275
+ updateWatchIssue(issue.id, {
276
+ state: state.closedByBot ? "closed_by_us" : "closed_by_user",
277
+ closed_check: state.closedByBot ? null : "pending",
278
+ });
279
+ issue.state = state.closedByBot ? "closed_by_us" : "closed_by_user";
280
+ issue.closed_check = state.closedByBot ? null : "pending";
281
+ log("info", `issue #${issue.issue_number} was closed on GitHub`);
282
+ }
283
+ else if (state && issue.state === "closed_by_user" && state.open) {
284
+ updateWatchIssue(issue.id, { state: "open", closed_check: null });
285
+ issue.state = "open";
286
+ issue.closed_check = null;
287
+ log("info", `issue #${issue.issue_number} was reopened on GitHub`);
288
+ }
289
+ }
290
+ const closedOnce = issue.state === "closed_by_user" && issue.closed_check === "pending";
291
+ if (issue.state !== "open" && !closedOnce)
292
+ continue;
293
+ if (!issue.path)
294
+ continue;
295
+ const file = byPath.get(issue.path);
296
+ if (!file && !closedOnce)
297
+ continue;
298
+ const content = await fileAt(client, repo, issue.path, after);
299
+ const at = content === undefined
300
+ ? undefined
301
+ : locateAnchor(content, issue.anchor_text, issue.line_from ?? 1);
302
+ let touched = false;
303
+ if (file) {
304
+ if (file.status === "removed" || !at)
305
+ touched = true;
306
+ else {
307
+ const lines = changedLines(file.patch);
308
+ for (let n = at.from; n <= at.to; n++)
309
+ if (lines.has(n))
310
+ touched = true;
311
+ }
312
+ }
313
+ if (at && !touched) {
314
+ updateWatchIssue(issue.id, { line_from: at.from, line_to: at.to });
315
+ }
316
+ if (!touched && !closedOnce)
317
+ continue;
318
+ checks.push({
319
+ issue,
320
+ promptId: `F${checks.length + 1}`,
321
+ at,
322
+ touched,
323
+ closedOnce,
324
+ content,
325
+ });
326
+ }
327
+ return checks;
328
+ }
329
+ function knownPrompt(checks) {
330
+ if (checks.length === 0)
331
+ return "";
332
+ const entries = checks.map((check) => {
333
+ const { content, issue } = check;
334
+ const where = check.at
335
+ ? `${issue.path}:${check.at.from}-${check.at.to}`
336
+ : `${issue.path} (the recorded code is no longer where it was)`;
337
+ // A deleted file is not by itself a fix: the code may have moved. The
338
+ // model decides, from the DIFF.
339
+ const code = content === undefined
340
+ ? "This file no longer exists at this commit. If this code moved to another place in the DIFF, answer open with that path:from-to. If it is gone, answer closed."
341
+ : check.at
342
+ ? excerpt(content, check.at.from, check.at.to)
343
+ : excerpt(content, issue.line_from ?? 1, issue.line_to ?? issue.line_from ?? 1);
344
+ return [
345
+ `${check.promptId} · known issue #${issue.issue_number} · ${where}`,
346
+ issue.title,
347
+ issue.body_md.slice(0, 600),
348
+ "CURRENT CODE:",
349
+ code,
350
+ ].join("\n");
351
+ });
352
+ return [
353
+ "KNOWN ISSUES (problems this repository already has an open issue for):",
354
+ "These were found by earlier reviews. Review the DIFF from scratch as",
355
+ "usual, and in addition decide for each entry below whether the problem",
356
+ "is still in the CURRENT CODE shown. Answer every id in previousFindings,",
357
+ 'state "open" when the problem is still there and "closed" only when the',
358
+ "code now clearly fixes it. When the code moved, give its new place as",
359
+ "open path:from-to. Do not report these again under findings. The same",
360
+ "kind of problem in other code, in this file or another one, is a new",
361
+ "finding: report it under findings as usual.",
362
+ "",
363
+ entries.join("\n\n"),
364
+ "",
365
+ ].join("\n");
366
+ }
367
+ function sameFinding(issue, finding) {
368
+ if (!samePath(issue.path, finding.path))
369
+ return false;
370
+ if (issue.anchor_text && finding.anchorText) {
371
+ return (normalizeAnchor(issue.anchor_text) === normalizeAnchor(finding.anchorText));
372
+ }
373
+ const from = issue.line_from ?? 0;
374
+ const to = issue.line_to ?? from;
375
+ return ((finding.lineFrom ?? 0) <= to &&
376
+ from <= (finding.lineTo ?? finding.lineFrom ?? 0));
377
+ }
378
+ function resolvedNew(parsed, revision) {
379
+ const byPath = new Map(revision.files.map((file) => [file.path, file]));
380
+ return parsed.map((finding) => {
381
+ const file = finding.path ? byPath.get(finding.path) : undefined;
382
+ return {
383
+ id: crypto.randomUUID(),
384
+ state: "new",
385
+ path: finding.path,
386
+ lineFrom: finding.from,
387
+ lineTo: finding.to,
388
+ title: finding.heading || finding.path,
389
+ bodyMd: finding.excerpt,
390
+ anchorText: file && finding.from
391
+ ? anchorTextFromPatch(file.patch, finding.from, finding.to ?? finding.from)
392
+ : null,
393
+ severity: finding.severity ?? "P2",
394
+ carriedFromId: null,
395
+ firstSeenReviewId: null,
396
+ };
397
+ });
398
+ }
399
+ function annotationLevel(severity) {
400
+ if (severity === "P0" || severity === "P1")
401
+ return "failure";
402
+ return severity === "P2" ? "warning" : "notice";
403
+ }
404
+ async function startCheckRun(client, repo, after, reviewId, log) {
405
+ if (!client.createCheckRun)
406
+ return undefined;
407
+ try {
408
+ const check = await client.createCheckRun(`repos/${repo}/check-runs`, {
409
+ name: CHECK_NAME,
410
+ head_sha: after,
411
+ status: "in_progress",
412
+ started_at: new Date().toISOString(),
413
+ external_id: reviewId,
414
+ });
415
+ const id = String(check.id ?? "");
416
+ if (id)
417
+ setReviewStatus(reviewId, "running", { check_run_id: id });
418
+ return id || undefined;
419
+ }
420
+ catch (error) {
421
+ log("info", `GitHub Check Run unavailable: ${redact(String(error))}`);
422
+ return undefined;
423
+ }
424
+ }
425
+ /** Completes the App's check run: the one this review started, or a new one
426
+ * already completed when the App's delivery arrived after the review began.
427
+ * Every end goes through here, canceled and failed included, so no check is
428
+ * left in progress. */
429
+ async function completeCheckRun(client, repo, input, log) {
430
+ const body = {
431
+ status: "completed",
432
+ // Commit watch reports, it never blocks: the push is already in.
433
+ conclusion: "success",
434
+ completed_at: new Date().toISOString(),
435
+ output: {
436
+ title: humanCopy(input.title),
437
+ summary: humanCopy(input.summary),
438
+ annotations: input.findings
439
+ .filter((finding) => finding.path)
440
+ .slice(0, 50)
441
+ .map((finding) => {
442
+ const start = Math.max(1, finding.lineFrom ?? 1);
443
+ return {
444
+ path: finding.path,
445
+ start_line: start,
446
+ end_line: Math.max(start, finding.lineTo ?? start),
447
+ annotation_level: annotationLevel(finding.severity),
448
+ title: humanCopy(`${finding.severity}: ${finding.title}`),
449
+ message: humanCopy(finding.bodyMd),
450
+ };
451
+ }),
452
+ },
453
+ };
454
+ try {
455
+ if (input.checkRunId) {
456
+ if (!client.updateCheckRun)
457
+ return;
458
+ await client.updateCheckRun(`repos/${repo}/check-runs/${input.checkRunId}`, body);
459
+ return;
460
+ }
461
+ if (!client.createCheckRun)
462
+ return;
463
+ const check = await client.createCheckRun(`repos/${repo}/check-runs`, {
464
+ ...body,
465
+ name: CHECK_NAME,
466
+ head_sha: input.after,
467
+ external_id: input.reviewId,
468
+ });
469
+ const id = String(check.id ?? "");
470
+ if (id) {
471
+ setReviewStatus(input.reviewId, getReview(input.reviewId)?.status ?? "done", { check_run_id: id });
472
+ }
473
+ }
474
+ catch (error) {
475
+ log("info", `GitHub Check Run update failed: ${redact(String(error))}`);
476
+ }
477
+ }
478
+ /** Where a tracked finding is now, and its code, so the next push finds it.
479
+ * The model's "open path:from-to" wins, since the push may have rewritten
480
+ * the very lines the old code fragment matched. */
481
+ function trackedLocation(check, answer, revision) {
482
+ if (answer?.state === "open" && answer.from) {
483
+ const to = Math.max(answer.from, answer.to ?? answer.from);
484
+ if (!answer.path || samePath(answer.path, check.issue.path)) {
485
+ const lines = check.content?.split("\n");
486
+ const anchor = lines?.slice(answer.from - 1, to).join("\n");
487
+ if (anchor?.trim()) {
488
+ return { line_from: answer.from, line_to: to, anchor_text: anchor };
489
+ }
490
+ }
491
+ else {
492
+ // The code moved to another file, which the model can only point at
493
+ // inside the DIFF, so its code comes from that file's patch.
494
+ const moved = revision.files.find((f) => samePath(f.path, answer.path));
495
+ const anchor = moved
496
+ ? anchorTextFromPatch(moved.patch, answer.from, to)
497
+ : null;
498
+ if (moved && anchor?.trim()) {
499
+ return {
500
+ path: moved.path,
501
+ line_from: answer.from,
502
+ line_to: to,
503
+ anchor_text: anchor,
504
+ };
505
+ }
506
+ }
507
+ }
508
+ return check.at ? { line_from: check.at.from, line_to: check.at.to } : {};
509
+ }
510
+ /** Applies the review's verdicts to the tracked issues and opens an issue
511
+ * for each new P0/P1 finding. Returns the findings left to report, without
512
+ * the ones a maintainer said to ignore. */
513
+ async function applyIssues(input) {
514
+ const { client, repo, log } = input;
515
+ const range = pushRange(input.before, input.after);
516
+ const activity = {
517
+ opened: [],
518
+ stillFound: [],
519
+ closed: [],
520
+ closeDenied: [],
521
+ closedStillFound: [],
522
+ };
523
+ const now = new Date().toISOString();
524
+ for (const check of input.checks) {
525
+ const issue = check.issue;
526
+ // No answer counts as still there: closing needs an explicit "fixed".
527
+ const answer = input.verdicts.get(check.promptId);
528
+ const verdict = answer?.state ?? "open";
529
+ const location = trackedLocation(check, answer, input.revision);
530
+ if (check.closedOnce) {
531
+ if (verdict === "open") {
532
+ await commentOnIssue(client, repo, issue.issue_number, closedStillFoundComment(range));
533
+ activity.closedStillFound.push(issue.issue_number);
534
+ }
535
+ updateWatchIssue(issue.id, {
536
+ ...location,
537
+ closed_check: verdict === "open" ? "still_found" : "fixed",
538
+ last_review_id: input.reviewId,
539
+ last_check: verdict === "open" ? `still found by ${range}` : `fixed in ${range}`,
540
+ last_check_at: now,
541
+ });
542
+ continue;
543
+ }
544
+ // A deleted file has no last commit at `after`. Then the push itself is
545
+ // the commit that moved or deleted it.
546
+ const where = location.path ?? issue.path;
547
+ const commit = (where && (await lastCommitFor(client, repo, where, input.after))) ||
548
+ input.after;
549
+ if (verdict === "closed") {
550
+ await commentOnIssue(client, repo, issue.issue_number, fixedComment(commit, range));
551
+ const closed = await closeIssue(client, repo, issue.issue_number);
552
+ (closed ? activity.closed : activity.closeDenied).push(issue.issue_number);
553
+ // An issue it could not close is still open on GitHub, so it stays open
554
+ // here too: counted on the dashboard, and no duplicate is opened.
555
+ updateWatchIssue(issue.id, {
556
+ ...location,
557
+ state: closed ? "closed_by_us" : "open",
558
+ last_review_id: input.reviewId,
559
+ last_check: `fixed in ${range}`,
560
+ last_check_at: now,
561
+ });
562
+ }
563
+ else {
564
+ await commentOnIssue(client, repo, issue.issue_number, stillFoundComment(commit, input.after, range));
565
+ activity.stillFound.push(issue.issue_number);
566
+ updateWatchIssue(issue.id, {
567
+ ...location,
568
+ last_review_id: input.reviewId,
569
+ last_check: `still found by ${range}`,
570
+ last_check_at: now,
571
+ });
572
+ }
573
+ }
574
+ const tracked = listWatchIssues(repo);
575
+ const report = [];
576
+ for (const finding of input.found) {
577
+ // One problem can have had several issues over time (fixed, back again).
578
+ // An ignore on any of them holds for good.
579
+ const matches = tracked.filter((issue) => sameFinding(issue, finding));
580
+ if (matches.some((issue) => issue.state === "ignored"))
581
+ continue;
582
+ report.push(finding);
583
+ if (!input.openIssues || !ISSUE_SEVERITIES.has(finding.severity))
584
+ continue;
585
+ // The same problem already has an issue. A maintainer closed it and it is
586
+ // still there: it stays closed, once per closing. Fixed and back again:
587
+ // a new issue.
588
+ if (matches.some((issue) => issue.state === "open" ||
589
+ (issue.state === "closed_by_user" && issue.closed_check !== "fixed"))) {
590
+ continue;
591
+ }
592
+ const summary = findingSummary(finding.title, finding.bodyMd);
593
+ const number = await openIssue(client, repo, {
594
+ title: issueTitle(finding.severity, summary),
595
+ severity: finding.severity,
596
+ body: issueBody({
597
+ id: finding.id,
598
+ body: finding.bodyMd,
599
+ repo,
600
+ after: input.after,
601
+ before: input.before,
602
+ branch: input.branch,
603
+ path: finding.path,
604
+ lineFrom: finding.lineFrom,
605
+ lineTo: finding.lineTo,
606
+ }),
607
+ });
608
+ insertWatchIssue({
609
+ repo,
610
+ issueNumber: number,
611
+ severity: finding.severity,
612
+ title: summary,
613
+ bodyMd: finding.bodyMd,
614
+ path: finding.path,
615
+ lineFrom: finding.lineFrom,
616
+ lineTo: finding.lineTo,
617
+ anchorText: finding.anchorText,
618
+ reviewId: input.reviewId,
619
+ });
620
+ activity.opened.push(number);
621
+ log("info", `opened issue #${number} for a ${finding.severity} finding`);
622
+ }
623
+ return { report, activity };
624
+ }
625
+ /** Ignored findings are dropped even with no App: the hard filter is in
626
+ * SQLite and needs no GitHub call. */
627
+ function withoutIgnored(repo, found) {
628
+ const ignored = listWatchIssues(repo).filter((issue) => issue.state === "ignored");
629
+ return found.filter((finding) => !ignored.some((issue) => sameFinding(issue, finding)));
630
+ }
631
+ async function runPushReview(job, log, signal) {
632
+ const args = JSON.parse(job.args);
633
+ const review = getReview(args.reviewId);
634
+ if (!review)
635
+ throw new Error("push review row missing");
636
+ const range = pushRange(args.before, args.after);
637
+ const event = getPushEvent(job.repo, args.after);
638
+ const sources = event ? pushSources(event) : [];
639
+ setReviewStatus(review.id, "running");
640
+ log("info", `push review started for ${job.repo} ${range} on ${args.branch}`);
641
+ let completed = false;
642
+ let client;
643
+ let checkRunId;
644
+ try {
645
+ client = await appClientFor(job.repo);
646
+ checkRunId =
647
+ client && sources.includes("app")
648
+ ? await startCheckRun(client, job.repo, args.after, review.id, log)
649
+ : undefined;
650
+ const revision = await pushRevision(job, client, args.before, args.after, args.branch);
651
+ const checks = client
652
+ ? await knownChecks(client, job.repo, revision, args.after, log)
653
+ : [];
654
+ const options = reviewOptions(job.repo, 0);
655
+ const ai = aiFor(options);
656
+ const costTally = emptyTally();
657
+ let tokensIn = 0;
658
+ let tokensOut = 0;
659
+ const started = performance.now();
660
+ const response = revision.files.length === 0 && checks.length === 0
661
+ ? undefined
662
+ : await withLogSink((phase, message) => log("info", `[${phase}] ${message}`), () => reviewWorkspaceRevision(revision, options, args.after, async (call) => {
663
+ tokensIn += call.tokensIn;
664
+ tokensOut += call.tokensOut;
665
+ addResponseCost(costTally, call);
666
+ await recordAiCost(job.repo, "review_push", call);
667
+ }, ai, (message) => log("info", message), { carryPrompt: knownPrompt(checks), job: "review_push" }));
668
+ if (signal.aborted)
669
+ return;
670
+ const text = response?.text ?? "";
671
+ const verdicts = new Map(parsePreviousVerdicts(text).map((v) => [v.id, v]));
672
+ const found = sortResolvedFindings(resolvedNew(parseFindings(text), revision));
673
+ const row = getRepo(job.repo);
674
+ const { report, activity } = client
675
+ ? await applyIssues({
676
+ client,
677
+ repo: job.repo,
678
+ branch: args.branch,
679
+ before: args.before,
680
+ after: args.after,
681
+ reviewId: review.id,
682
+ checks,
683
+ verdicts,
684
+ found,
685
+ revision,
686
+ openIssues: row?.watch_open_issues !== 0,
687
+ log,
688
+ })
689
+ : {
690
+ report: withoutIgnored(job.repo, found),
691
+ activity: {
692
+ opened: [],
693
+ stillFound: [],
694
+ closed: [],
695
+ closeDenied: [],
696
+ closedStillFound: [],
697
+ },
698
+ };
699
+ for (const finding of report) {
700
+ insertFinding({
701
+ id: finding.id,
702
+ reviewId: review.id,
703
+ severity: finding.severity,
704
+ path: finding.path ?? undefined,
705
+ lineFrom: finding.lineFrom ?? undefined,
706
+ lineTo: finding.lineTo ?? undefined,
707
+ title: finding.title,
708
+ bodyMd: finding.bodyMd,
709
+ anchorText: finding.anchorText,
710
+ state: "new",
711
+ });
712
+ }
713
+ const blockingMode = reviewBlockingFrom(options.reviewBlocking);
714
+ const settled = settle(costTally);
715
+ const result = {
716
+ subject: { repo: job.repo, branch: args.branch, prNumber: null },
717
+ push: { before: args.before, after: args.after, range },
718
+ revision: revisionStats(revision),
719
+ guide: { builtAt: response?.guideBuiltAt ?? null },
720
+ codegraph: { state: response?.codegraphState ?? "disabled" },
721
+ summary: summaryCounts(report, blockingMode),
722
+ findings: report.map((finding) => toJsonFinding(finding, blockingMode)),
723
+ issues: activity,
724
+ usage: {
725
+ tokensIn,
726
+ tokensOut,
727
+ ...costUsage(costTally),
728
+ billedTo: "server",
729
+ },
730
+ remote: { jobId: job.id, reviewId: review.id },
731
+ };
732
+ savePushResult(review.id, result);
733
+ setReviewStatus(review.id, "done", {
734
+ findings_count: report.length,
735
+ open_count: report.length,
736
+ guide_built_at: response?.guideBuiltAt ?? null,
737
+ duration_ms: Math.round(performance.now() - started),
738
+ tokens_in: tokensIn,
739
+ tokens_out: tokensOut,
740
+ billed_to: "server",
741
+ ...costColumns(settled),
742
+ });
743
+ deletePushInput(job.id);
744
+ completed = true;
745
+ // The App's delivery may have joined this review while it ran (CI came
746
+ // first). Read the sources again now that the review is done: a later
747
+ // delivery sees "done" and queues the check itself.
748
+ const joined = getPushEvent(job.repo, args.after);
749
+ if (client &&
750
+ (checkRunId || (joined && pushSources(joined).includes("app")))) {
751
+ await completeCheckRun(client, job.repo, {
752
+ after: args.after,
753
+ reviewId: review.id,
754
+ checkRunId,
755
+ ...resultCheckText(report.length, range),
756
+ findings: report,
757
+ }, log);
758
+ }
759
+ log("info", `push review finished (${report.length} findings)`);
760
+ }
761
+ finally {
762
+ if (!completed) {
763
+ setReviewStatus(review.id, signal.aborted ? "canceled" : "failed");
764
+ if (!signal.aborted)
765
+ deletePushInput(job.id);
766
+ if (client && checkRunId) {
767
+ await completeCheckRun(client, job.repo, {
768
+ after: args.after,
769
+ reviewId: review.id,
770
+ checkRunId,
771
+ ...stoppedCheckText(signal.aborted, range),
772
+ findings: [],
773
+ }, log);
774
+ }
775
+ }
776
+ }
777
+ }
778
+ function stoppedCheckText(aborted, range) {
779
+ return aborted
780
+ ? {
781
+ title: "Review canceled",
782
+ summary: `The review of ${range} was canceled. When a newer push was merged into it, that review covers this push.`,
783
+ }
784
+ : {
785
+ title: "Review did not finish",
786
+ summary: `The review of ${range} did not finish. The dashboard has the reason.`,
787
+ };
788
+ }
789
+ function resultCheckText(count, range) {
790
+ return count === 0
791
+ ? { title: `No findings in ${range}`, summary: "No actionable findings." }
792
+ : {
793
+ title: `${count} finding${count === 1 ? "" : "s"} in ${range}`,
794
+ summary: `${count} finding(s) reported.`,
795
+ };
796
+ }
797
+ /** The App's check for a review that finished before the App's delivery
798
+ * arrived, built from what the review stored. */
799
+ async function publishLateCheck(job, reviewId, after, log) {
800
+ const review = getReview(reviewId);
801
+ if (!review || review.check_run_id)
802
+ return;
803
+ const client = await appClientFor(job.repo);
804
+ if (!client)
805
+ return;
806
+ const findings = listFindingsForReview(reviewId).map((row) => ({
807
+ path: row.path,
808
+ lineFrom: row.line_from,
809
+ lineTo: row.line_to,
810
+ severity: row.severity,
811
+ title: row.title,
812
+ bodyMd: row.body_md,
813
+ }));
814
+ await completeCheckRun(client, job.repo, {
815
+ after,
816
+ reviewId,
817
+ checkRunId: undefined,
818
+ ...resultCheckText(findings.length, pushRange(review.base_sha ?? after, after)),
819
+ findings,
820
+ }, log);
821
+ }
822
+ async function runSkippedCheck(job, log) {
823
+ const args = JSON.parse(job.args);
824
+ if (args.reviewId) {
825
+ await publishLateCheck(job, args.reviewId, args.after, log);
826
+ return;
827
+ }
828
+ const client = await appClientFor(job.repo);
829
+ if (!client?.createCheckRun) {
830
+ log("info", "no GitHub App covers this repository, no check run");
831
+ return;
832
+ }
833
+ await client.createCheckRun(`repos/${job.repo}/check-runs`, {
834
+ name: CHECK_NAME,
835
+ head_sha: args.after,
836
+ status: "completed",
837
+ conclusion: "skipped",
838
+ completed_at: new Date().toISOString(),
839
+ output: { title: args.title ?? "", summary: args.title ?? "" },
840
+ });
841
+ }
842
+ export function registerPushReviewHandlers() {
843
+ registerHandler(PUSH_JOB, {
844
+ run: runPushReview,
845
+ // A push review that was running when the server stopped may have opened
846
+ // issues already. Running it again could open them twice, so it ends.
847
+ // The check it started is completed here, since its finally never ran.
848
+ async reconcile(job, log) {
849
+ const review = getReviewByJobId(job.id);
850
+ if (!review)
851
+ return "canceled";
852
+ setReviewStatus(review.id, "failed");
853
+ if (review.check_run_id) {
854
+ const args = JSON.parse(job.args);
855
+ const client = await appClientFor(job.repo);
856
+ if (client) {
857
+ await completeCheckRun(client, job.repo, {
858
+ after: args.after,
859
+ reviewId: review.id,
860
+ checkRunId: review.check_run_id,
861
+ ...stoppedCheckText(false, pushRange(args.before, args.after)),
862
+ findings: [],
863
+ }, log);
864
+ }
865
+ }
866
+ return "canceled";
867
+ },
868
+ });
869
+ registerHandler(PUSH_CHECK_JOB, {
870
+ async run(job, log) {
871
+ await runSkippedCheck(job, log);
872
+ },
873
+ });
874
+ }
875
+ /** For the sync route: the stored result of a finished push review. */
876
+ export function pushJobStatus(jobId) {
877
+ return getJob(jobId);
878
+ }