co-maintainer 0.6.8 → 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.
- package/dist/package.json +1 -1
- package/dist/src/ai/fake.d.ts +10 -1
- package/dist/src/ai/fake.js +39 -4
- package/dist/src/cli/args.js +2 -0
- package/dist/src/cli/command_json.d.ts +6 -0
- package/dist/src/cli/command_json.js +1 -0
- package/dist/src/cli/commands/probe.js +25 -0
- package/dist/src/cli/commands/registry.js +11 -0
- package/dist/src/cli/commands/review.js +5 -1
- package/dist/src/cli/commands/serve.js +10 -0
- package/dist/src/cli/commands/set.js +23 -2
- package/dist/src/cli/review_args.d.ts +9 -0
- package/dist/src/cli/review_args.js +36 -1
- package/dist/src/cli/review_github.js +6 -2
- package/dist/src/cli/review_result.js +6 -5
- package/dist/src/github/app.d.ts +4 -0
- package/dist/src/github/app.js +10 -1
- package/dist/src/github/permissions.js +40 -0
- package/dist/src/local/git_ops.js +3 -2
- package/dist/src/local/git_revision.d.ts +5 -2
- package/dist/src/local/git_revision.js +17 -10
- package/dist/src/pr/checkout.js +14 -5
- package/dist/src/pr/codegraph_tools.js +5 -3
- package/dist/src/pr/reviewer.d.ts +2 -0
- package/dist/src/pr/reviewer.js +1 -1
- package/dist/src/remote/client.js +14 -4
- package/dist/src/remote/push_client.d.ts +7 -0
- package/dist/src/remote/push_client.js +287 -0
- package/dist/src/remote/server/body.d.ts +11 -0
- package/dist/src/remote/server/body.js +14 -5
- package/dist/src/remote/server/routes.d.ts +1 -0
- package/dist/src/remote/server/routes.js +61 -4
- package/dist/src/remote/server/sync.js +11 -3
- package/dist/src/remote/validate.js +17 -0
- package/dist/src/server/api/repos.js +13 -2
- package/dist/src/server/app.js +5 -6
- package/dist/src/server/http.js +5 -0
- package/dist/src/server/pages/layout.js +1 -0
- package/dist/src/server/pages/repo.d.ts +3 -1
- package/dist/src/server/pages/repo.js +4 -1
- package/dist/src/server/pages/repo_pushes.d.ts +7 -0
- package/dist/src/server/pages/repo_pushes.js +144 -0
- package/dist/src/server/pages/repo_settings.js +45 -1
- package/dist/src/server/pages/router.js +34 -5
- package/dist/src/server/pages/settings.js +1 -1
- package/dist/src/server/webhook/index.js +24 -7
- package/dist/src/services/dashboard.d.ts +46 -0
- package/dist/src/services/dashboard.js +24 -0
- package/dist/src/services/jobs.js +18 -3
- package/dist/src/services/push_review.d.ts +51 -0
- package/dist/src/services/push_review.js +878 -0
- package/dist/src/services/setup.js +21 -0
- package/dist/src/services/watch_comments.d.ts +10 -0
- package/dist/src/services/watch_comments.js +179 -0
- package/dist/src/services/watch_issues.d.ts +70 -0
- package/dist/src/services/watch_issues.js +266 -0
- package/dist/src/services/watch_notes.d.ts +8 -0
- package/dist/src/services/watch_notes.js +80 -0
- package/dist/src/services/watch_status.d.ts +25 -0
- package/dist/src/services/watch_status.js +101 -0
- package/dist/src/services/webhook.js +107 -3
- package/dist/src/store/migrations.js +63 -0
- package/dist/src/store/repos.d.ts +1 -1
- package/dist/src/store/rows.d.ts +6 -0
- package/dist/src/store/sessions.d.ts +3 -0
- package/dist/src/store/sessions.js +5 -0
- package/dist/src/store/watch.d.ts +108 -0
- package/dist/src/store/watch.js +186 -0
- package/dist/src/types.d.ts +2 -0
- package/dist/src/util/repo_name.d.ts +2 -0
- package/dist/src/util/repo_name.js +56 -0
- package/dist/src/util/runtime.js +7 -0
- package/dist/src/util/sanitize.d.ts +6 -0
- package/dist/src/util/sanitize.js +10 -0
- package/package.json +1 -1
|
@@ -21,6 +21,7 @@ import { closeAppDb, isAppDbOpen, openAppDb } from "../store/app_db.js";
|
|
|
21
21
|
import { getRepo, markKnowledgeBuilt, registerCliRepo, } from "../store/repos.js";
|
|
22
22
|
import { nowIso } from "../util/time.js";
|
|
23
23
|
import { mkdir, readTextFile, remove } from "../util/runtime.js";
|
|
24
|
+
import { applyWatchNotes, withMaintainerNotes } from "./watch_notes.js";
|
|
24
25
|
import { addResponseCost, emptyTally } from "../util/cost.js";
|
|
25
26
|
import { costLabel } from "../util/run_summary.js";
|
|
26
27
|
import { formatError } from "../cli/error.js";
|
|
@@ -114,6 +115,21 @@ async function writeReviewDocuments(repo, documents, signalCount) {
|
|
|
114
115
|
await remove(`${directory}/PR_REVIEW_DETAILED_GUIDE.md`).catch(() => { });
|
|
115
116
|
}
|
|
116
117
|
}
|
|
118
|
+
/** Puts the commit watch maintainer notes at the end of the detailed guide,
|
|
119
|
+
* which every review reads. Called after the guides are written, so a guide
|
|
120
|
+
* the AI just rebuilt keeps them. */
|
|
121
|
+
async function writeMaintainerNotes(repo, lines) {
|
|
122
|
+
const path = `${reposDir()}/${repo}/PR_REVIEW_DETAILED_GUIDE.md`;
|
|
123
|
+
let current = "";
|
|
124
|
+
try {
|
|
125
|
+
current = await readTextFile(path);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
// No detailed guide yet: the notes start one.
|
|
129
|
+
}
|
|
130
|
+
const { writeTextFileAtomic } = await import("../util/atomic.js");
|
|
131
|
+
await writeTextFileAtomic(path, withMaintainerNotes(current, lines));
|
|
132
|
+
}
|
|
117
133
|
/** Writes the codebase-description sections (layout/style/tests/devloop) to
|
|
118
134
|
* their own file, so `review` can check a pull request against how this
|
|
119
135
|
* repository's code actually looks, not just the review-bar checklist. Every
|
|
@@ -242,6 +258,8 @@ async function initOrRemake(options) {
|
|
|
242
258
|
}
|
|
243
259
|
let result = await timed("assemble skill", options.logTime, () => assembleSkill(options.repo, facts, previousMarkdown, previous?.sectionHashes ?? {}, overrides));
|
|
244
260
|
result.markdown = addReviewLink(result.markdown, reviewDocuments);
|
|
261
|
+
// What maintainers said on commit watch issues since the last sync.
|
|
262
|
+
const noteLines = await applyWatchNotes(options.repo, lowAi, (response) => usage("watch_note", response));
|
|
245
263
|
const validation = await timed("validate skill", options.logTime, () => validateSkill(result.markdown, source));
|
|
246
264
|
// There is no deterministic fallback to reassemble from: a skill the AI could
|
|
247
265
|
// not write correctly is reported rather than rebuilt from facts.
|
|
@@ -262,6 +280,9 @@ async function initOrRemake(options) {
|
|
|
262
280
|
await writeTextFileAtomic(path, result.markdown);
|
|
263
281
|
await writeCodebaseDocument(options.repo, overrides);
|
|
264
282
|
await writeReviewDocuments(options.repo, reviewDocuments, reviewSignalCount(facts));
|
|
283
|
+
if (noteLines.length > 0) {
|
|
284
|
+
await writeMaintainerNotes(options.repo, noteLines);
|
|
285
|
+
}
|
|
265
286
|
const head = source.commits[0];
|
|
266
287
|
const baseSha = head?.sha ? String(head.sha) : new Date().toISOString();
|
|
267
288
|
// A CLI init has no app.db row for the repository yet; without one the
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const WATCH_COMMENT_JOB = "watch_comment";
|
|
2
|
+
export type CommentKind = "question" | "ignore" | "info";
|
|
3
|
+
export type Classification = {
|
|
4
|
+
forUs: boolean;
|
|
5
|
+
kind: CommentKind | "other";
|
|
6
|
+
};
|
|
7
|
+
/** Reads the classifier's answer. Anything unreadable is "not for us", so a
|
|
8
|
+
* broken answer never makes co-maintainer speak. */
|
|
9
|
+
export declare function parseClassification(text: string): Classification;
|
|
10
|
+
export declare function registerWatchCommentHandler(): void;
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/** Comments on commit watch issues.
|
|
2
|
+
*
|
|
3
|
+
* The webhook already checked that the issue is ours and that the author has
|
|
4
|
+
* write access. The low model then decides whether the comment is meant for
|
|
5
|
+
* co-maintainer at all ("@asd can you take a look" is not) and whether it is
|
|
6
|
+
* a question, a request to ignore the finding, or information. A question
|
|
7
|
+
* gets an answer from the high model, in the comment's language. Ignore and
|
|
8
|
+
* information get a short "noted" and wait for the next sync, which puts the
|
|
9
|
+
* note in the detailed review guide and, for ignore, stops reporting the
|
|
10
|
+
* finding. */
|
|
11
|
+
import { createAiProvider } from "../ai/provider.js";
|
|
12
|
+
import { FakeAiProvider } from "../ai/fake.js";
|
|
13
|
+
import { completeWithMermaidTools } from "../ai/mermaid_loop.js";
|
|
14
|
+
import { loadGuides } from "../review/guides.js";
|
|
15
|
+
import { getEnv } from "../util/runtime.js";
|
|
16
|
+
import { getWatchIssue, insertWatchNote } from "../store/watch.js";
|
|
17
|
+
import { registerHandler } from "./jobs.js";
|
|
18
|
+
import { recordAiCost } from "./setup.js";
|
|
19
|
+
import { aiFor, clientFor, humanCopy, resolveInstallationId, reviewOptions, } from "./review.js";
|
|
20
|
+
import { commentOnIssue, excerpt, fileAt, NOTED_REPLY, } from "./watch_issues.js";
|
|
21
|
+
export const WATCH_COMMENT_JOB = "watch_comment";
|
|
22
|
+
function replyMarker(commentId) {
|
|
23
|
+
return `<!-- co-maintainer:watch-reply ${commentId} -->`;
|
|
24
|
+
}
|
|
25
|
+
function lowAiFor(options) {
|
|
26
|
+
if (getEnv("CM_FAKE_AI") === "1")
|
|
27
|
+
return new FakeAiProvider();
|
|
28
|
+
const provider = createAiProvider(options, options.lowModel ?? "");
|
|
29
|
+
if (!provider)
|
|
30
|
+
throw new Error("commit watch comments need an AI provider");
|
|
31
|
+
return provider;
|
|
32
|
+
}
|
|
33
|
+
/** Reads the classifier's answer. Anything unreadable is "not for us", so a
|
|
34
|
+
* broken answer never makes co-maintainer speak. */
|
|
35
|
+
export function parseClassification(text) {
|
|
36
|
+
const match = /\{[\s\S]*\}/.exec(text);
|
|
37
|
+
if (!match)
|
|
38
|
+
return { forUs: false, kind: "other" };
|
|
39
|
+
try {
|
|
40
|
+
const value = JSON.parse(match[0]);
|
|
41
|
+
const kind = value.kind === "question" ||
|
|
42
|
+
value.kind === "ignore" ||
|
|
43
|
+
value.kind === "info"
|
|
44
|
+
? value.kind
|
|
45
|
+
: "other";
|
|
46
|
+
return { forUs: value.forUs === true, kind };
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return { forUs: false, kind: "other" };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function classifyPrompt(issueTitle, issueBody, comment) {
|
|
53
|
+
return `An issue was opened by co-maintainer, an automated code reviewer, for a
|
|
54
|
+
problem its review found on the default branch. A maintainer commented on it.
|
|
55
|
+
|
|
56
|
+
Decide two things and answer with one JSON object and nothing else:
|
|
57
|
+
{"forUs": true|false, "kind": "question"|"ignore"|"info"|"other"}
|
|
58
|
+
|
|
59
|
+
forUs: true only when the comment speaks to co-maintainer (or to the bot, or
|
|
60
|
+
to whoever opened the issue). A comment that addresses another person, for
|
|
61
|
+
example "@asd can you take a look", is false.
|
|
62
|
+
kind:
|
|
63
|
+
- "question": asks co-maintainer something about the problem.
|
|
64
|
+
- "ignore": says the finding is not a problem, is intended, or should be
|
|
65
|
+
ignored from now on.
|
|
66
|
+
- "info": tells co-maintainer something it should know for later reviews.
|
|
67
|
+
- "other": anything else.
|
|
68
|
+
|
|
69
|
+
ISSUE TITLE: ${issueTitle}
|
|
70
|
+
ISSUE:
|
|
71
|
+
${issueBody.slice(0, 4000)}
|
|
72
|
+
|
|
73
|
+
COMMENT:
|
|
74
|
+
${comment.slice(0, 6000)}`;
|
|
75
|
+
}
|
|
76
|
+
async function alreadyAnswered(client, repo, issueNumber, commentId) {
|
|
77
|
+
try {
|
|
78
|
+
const comments = await client.pages(`repos/${repo}/issues/${issueNumber}/comments`, 300);
|
|
79
|
+
const marker = replyMarker(commentId);
|
|
80
|
+
return comments.some((comment) => String(comment.body ?? "").includes(marker));
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** True when `login` can write to the repository. The webhook's
|
|
87
|
+
* author_association is only a first filter: MEMBER means a member of the
|
|
88
|
+
* organization and COLLABORATOR may be read-only. Any error counts as no. */
|
|
89
|
+
async function hasWriteAccess(client, repo, login) {
|
|
90
|
+
if (!login)
|
|
91
|
+
return false;
|
|
92
|
+
try {
|
|
93
|
+
const result = await client.request(`repos/${repo}/collaborators/${encodeURIComponent(login)}/permission`);
|
|
94
|
+
return ["admin", "maintain", "write"].includes(String(result.role_name ?? result.permission ?? ""));
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
async function runWatchComment(job, log) {
|
|
101
|
+
const args = JSON.parse(job.args);
|
|
102
|
+
const issue = getWatchIssue(job.repo, args.issueNumber);
|
|
103
|
+
if (!issue) {
|
|
104
|
+
log("info", "the issue is no longer tracked");
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const installationId = await resolveInstallationId(job.repo);
|
|
108
|
+
if (installationId === undefined) {
|
|
109
|
+
throw new Error("no GitHub App installation covers this repository");
|
|
110
|
+
}
|
|
111
|
+
const client = clientFor(installationId);
|
|
112
|
+
if (!(await hasWriteAccess(client, job.repo, args.author))) {
|
|
113
|
+
log("info", `${args.author ?? "the commenter"} has no write access`);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (await alreadyAnswered(client, job.repo, args.issueNumber, args.commentId)) {
|
|
117
|
+
log("info", "this comment was already answered");
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const options = reviewOptions(job.repo, 0);
|
|
121
|
+
const usage = async (response) => {
|
|
122
|
+
await recordAiCost(job.repo, "watch_comment", response);
|
|
123
|
+
};
|
|
124
|
+
const classified = await lowAiFor(options).complete({
|
|
125
|
+
job: "watch_comment_classify",
|
|
126
|
+
prompt: classifyPrompt(issue.title, issue.body_md, args.body),
|
|
127
|
+
maxTokens: 200,
|
|
128
|
+
});
|
|
129
|
+
await usage(classified);
|
|
130
|
+
const decision = parseClassification(classified.text);
|
|
131
|
+
log("info", `comment classified: forUs=${decision.forUs} kind=${decision.kind}`);
|
|
132
|
+
if (!decision.forUs || decision.kind === "other")
|
|
133
|
+
return;
|
|
134
|
+
const marker = replyMarker(args.commentId);
|
|
135
|
+
if (decision.kind === "ignore" || decision.kind === "info") {
|
|
136
|
+
insertWatchNote({
|
|
137
|
+
repo: job.repo,
|
|
138
|
+
issueNumber: args.issueNumber,
|
|
139
|
+
commentId: args.commentId,
|
|
140
|
+
author: args.author,
|
|
141
|
+
kind: decision.kind,
|
|
142
|
+
body: args.body,
|
|
143
|
+
});
|
|
144
|
+
await commentOnIssue(client, job.repo, args.issueNumber, `${NOTED_REPLY}\n\n${marker}`);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const guides = await loadGuides(job.repo);
|
|
148
|
+
const content = issue.path
|
|
149
|
+
? await fileAt(client, job.repo, issue.path, "")
|
|
150
|
+
: undefined;
|
|
151
|
+
const code = content && issue.path
|
|
152
|
+
? `${issue.path}:\n${excerpt(content, issue.line_from ?? 1, issue.line_to ?? issue.line_from ?? 1)}`
|
|
153
|
+
: "The file is not available.";
|
|
154
|
+
const answer = await completeWithMermaidTools(aiFor(options), {
|
|
155
|
+
job: "watch_comment_answer",
|
|
156
|
+
reasoningEffort: "high",
|
|
157
|
+
maxTokens: 6000,
|
|
158
|
+
system: `You are co-maintainer, the code reviewer that opened this issue. Answer the maintainer's question about the problem in plain prose. Write the answer in the same language as the question. Do not use semicolons.
|
|
159
|
+
|
|
160
|
+
REVIEW GUIDE:
|
|
161
|
+
${(guides.shortGuide || guides.skill || "").slice(0, 20_000)}`,
|
|
162
|
+
prompt: `ISSUE #${args.issueNumber}: ${issue.title}
|
|
163
|
+
${issue.body_md.slice(0, 6000)}
|
|
164
|
+
|
|
165
|
+
CURRENT CODE:
|
|
166
|
+
${code.slice(0, 20_000)}
|
|
167
|
+
|
|
168
|
+
QUESTION from ${args.author ?? "a maintainer"}:
|
|
169
|
+
${args.body.slice(0, 6000)}`,
|
|
170
|
+
}, 5);
|
|
171
|
+
await usage(answer);
|
|
172
|
+
const text = humanCopy(answer.text).trim();
|
|
173
|
+
if (!text)
|
|
174
|
+
throw new Error("the AI returned an empty answer");
|
|
175
|
+
await commentOnIssue(client, job.repo, args.issueNumber, `${text}\n\n${marker}`);
|
|
176
|
+
}
|
|
177
|
+
export function registerWatchCommentHandler() {
|
|
178
|
+
registerHandler(WATCH_COMMENT_JOB, { run: runWatchComment });
|
|
179
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { GitHubClient } from "../types.ts";
|
|
2
|
+
export declare const ISSUE_LABEL = "co-maintainer";
|
|
3
|
+
export declare const ISSUE_SEVERITIES: Set<string>;
|
|
4
|
+
export declare const CLOSE_DENIED_COMMENT = "I tried to close this issue, but I do not have permission to close issues in this repository.";
|
|
5
|
+
export declare const NOTED_REPLY = "Noted, I will take this into account on the next sync.";
|
|
6
|
+
export declare function shortSha(sha: string): string;
|
|
7
|
+
/** `abc1234..def5678`, the range a push review covered. */
|
|
8
|
+
export declare function pushRange(before: string, after: string): string;
|
|
9
|
+
export declare function findingMarker(id: string): string;
|
|
10
|
+
/** The finding in a few words, for the issue title and for later prompts:
|
|
11
|
+
* the body's first line, which the review writes as one sentence naming the
|
|
12
|
+
* defect. The heading (`[P1 · blocking] \`path\`: \`symbol\``) is the
|
|
13
|
+
* fallback. */
|
|
14
|
+
export declare function findingSummary(heading: string, body: string): string;
|
|
15
|
+
export declare function issueTitle(severity: string, summary: string): string;
|
|
16
|
+
export declare function permalink(repo: string, sha: string, path: string, from: number | null, to: number | null): string;
|
|
17
|
+
export declare function issueBody(input: {
|
|
18
|
+
id: string;
|
|
19
|
+
body: string;
|
|
20
|
+
repo: string;
|
|
21
|
+
after: string;
|
|
22
|
+
before: string;
|
|
23
|
+
branch: string;
|
|
24
|
+
path: string | null;
|
|
25
|
+
lineFrom: number | null;
|
|
26
|
+
lineTo: number | null;
|
|
27
|
+
}): string;
|
|
28
|
+
/** `commit` changed the finding's lines, `after` is the push. When they are
|
|
29
|
+
* the same commit it is written once. */
|
|
30
|
+
export declare function stillFoundComment(commit: string, after: string, range: string): string;
|
|
31
|
+
export declare function fixedComment(commit: string, range: string): string;
|
|
32
|
+
export declare function closedStillFoundComment(range: string): string;
|
|
33
|
+
/** Creates each label the issue needs. A label that already exists answers
|
|
34
|
+
* 422, which is fine. */
|
|
35
|
+
export declare function ensureLabels(client: GitHubClient, repo: string, names: string[]): Promise<void>;
|
|
36
|
+
export declare function openIssue(client: GitHubClient, repo: string, input: {
|
|
37
|
+
title: string;
|
|
38
|
+
body: string;
|
|
39
|
+
severity: string;
|
|
40
|
+
}): Promise<number>;
|
|
41
|
+
export declare function commentOnIssue(client: GitHubClient, repo: string, issueNumber: number, body: string): Promise<string>;
|
|
42
|
+
/** Closes the issue, or leaves the comment that says it could not. Returns
|
|
43
|
+
* whether it closed. */
|
|
44
|
+
export declare function closeIssue(client: GitHubClient, repo: string, issueNumber: number): Promise<boolean>;
|
|
45
|
+
export type IssueState = {
|
|
46
|
+
open: boolean;
|
|
47
|
+
closedByBot: boolean;
|
|
48
|
+
};
|
|
49
|
+
/** Whether the issue is open on GitHub, and when closed whether a bot (this
|
|
50
|
+
* App) closed it. */
|
|
51
|
+
export declare function readIssueState(client: GitHubClient, repo: string, issueNumber: number): Promise<IssueState | undefined>;
|
|
52
|
+
/** A file's text at a commit (the default branch when `sha` is empty), or
|
|
53
|
+
* undefined when it is gone or unreadable. */
|
|
54
|
+
export declare function fileAt(client: GitHubClient, repo: string, path: string, sha: string): Promise<string | undefined>;
|
|
55
|
+
/** The newest commit at or before `sha` that touched `path`, which inside a
|
|
56
|
+
* push that changed the file is the commit that changed it. */
|
|
57
|
+
export declare function lastCommitFor(client: GitHubClient, repo: string, path: string, sha: string): Promise<string | undefined>;
|
|
58
|
+
/** Where `anchor` sits in `content`, the closest match to `oldFrom` when it
|
|
59
|
+
* appears more than once. Blank lines are skipped on both sides, the same
|
|
60
|
+
* way `normalizeAnchor` drops them. */
|
|
61
|
+
export declare function locateAnchor(content: string, anchor: string | null, oldFrom: number): {
|
|
62
|
+
from: number;
|
|
63
|
+
to: number;
|
|
64
|
+
} | undefined;
|
|
65
|
+
/** The new-file line numbers a patch adds or changes. */
|
|
66
|
+
export declare function changedLines(patch: string): Set<number>;
|
|
67
|
+
/** The finding's code with line numbers and a few lines around it, for the
|
|
68
|
+
* known-issue check. */
|
|
69
|
+
export declare function excerpt(content: string, from: number, to: number): string;
|
|
70
|
+
export declare function samePath(a: string | null, b: string | null): boolean;
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/** The GitHub side of commit watch issues: the words co-maintainer writes,
|
|
2
|
+
* opening, commenting on and closing an issue, and finding a tracked
|
|
3
|
+
* finding's code again in a later commit.
|
|
4
|
+
*
|
|
5
|
+
* Issues are written in English. A reply to a comment is written in the
|
|
6
|
+
* comment's language, which is the comment job's business, not this file's. */
|
|
7
|
+
import { normalizeAnchor, normalizePath } from "../review/revision.js";
|
|
8
|
+
import { stripSuggestion } from "../pr/suggestion.js";
|
|
9
|
+
import { redact } from "../util/redact.js";
|
|
10
|
+
export const ISSUE_LABEL = "co-maintainer";
|
|
11
|
+
export const ISSUE_SEVERITIES = new Set(["P0", "P1"]);
|
|
12
|
+
export const CLOSE_DENIED_COMMENT = "I tried to close this issue, but I do not have permission to close issues in this repository.";
|
|
13
|
+
export const NOTED_REPLY = "Noted, I will take this into account on the next sync.";
|
|
14
|
+
const LABEL_COLORS = {
|
|
15
|
+
[ISSUE_LABEL]: "1f883d",
|
|
16
|
+
"cm-P0": "b60205",
|
|
17
|
+
"cm-P1": "d93f0b",
|
|
18
|
+
};
|
|
19
|
+
export function shortSha(sha) {
|
|
20
|
+
return sha.slice(0, 7);
|
|
21
|
+
}
|
|
22
|
+
/** `abc1234..def5678`, the range a push review covered. */
|
|
23
|
+
export function pushRange(before, after) {
|
|
24
|
+
return `${shortSha(before)}..${shortSha(after)}`;
|
|
25
|
+
}
|
|
26
|
+
export function findingMarker(id) {
|
|
27
|
+
return `<!-- co-maintainer:finding ${id} -->`;
|
|
28
|
+
}
|
|
29
|
+
/** Model text goes to a public issue, so a secret it quoted is masked and a
|
|
30
|
+
* suggestion block, which only makes sense on a diff, is dropped. */
|
|
31
|
+
function clean(text) {
|
|
32
|
+
return redact(stripSuggestion(text)).trim();
|
|
33
|
+
}
|
|
34
|
+
/** The finding in a few words, for the issue title and for later prompts:
|
|
35
|
+
* the body's first line, which the review writes as one sentence naming the
|
|
36
|
+
* defect. The heading (`[P1 · blocking] \`path\`: \`symbol\``) is the
|
|
37
|
+
* fallback. */
|
|
38
|
+
export function findingSummary(heading, body) {
|
|
39
|
+
const first = clean(body)
|
|
40
|
+
.split("\n")
|
|
41
|
+
.map((line) => line.trim())
|
|
42
|
+
.find(Boolean);
|
|
43
|
+
const text = first ??
|
|
44
|
+
clean(heading)
|
|
45
|
+
.replace(/^\[P[0-3][^\]]*\]\s*/, "")
|
|
46
|
+
.replace(/`/g, "");
|
|
47
|
+
const flat = text.replace(/\s+/g, " ");
|
|
48
|
+
return flat.length > 200 ? `${flat.slice(0, 199)}…` : flat;
|
|
49
|
+
}
|
|
50
|
+
export function issueTitle(severity, summary) {
|
|
51
|
+
return `[${severity}] ${summary}`;
|
|
52
|
+
}
|
|
53
|
+
export function permalink(repo, sha, path, from, to) {
|
|
54
|
+
const lines = from && from > 0
|
|
55
|
+
? to && to > from
|
|
56
|
+
? `#L${from}-L${to}`
|
|
57
|
+
: `#L${from}`
|
|
58
|
+
: "";
|
|
59
|
+
return `https://github.com/${repo}/blob/${sha}/${path}${lines}`;
|
|
60
|
+
}
|
|
61
|
+
export function issueBody(input) {
|
|
62
|
+
const where = input.path
|
|
63
|
+
? permalink(input.repo, input.after, input.path, input.lineFrom, input.lineTo)
|
|
64
|
+
: "the whole push";
|
|
65
|
+
return [
|
|
66
|
+
clean(input.body),
|
|
67
|
+
"",
|
|
68
|
+
`**Where:** ${where}`,
|
|
69
|
+
`**Push:** \`${pushRange(input.before, input.after)}\` on \`${input.branch}\``,
|
|
70
|
+
"",
|
|
71
|
+
"Feel free to ask questions here if you need more detail.",
|
|
72
|
+
"",
|
|
73
|
+
"Opened by co-maintainer commit watch review.",
|
|
74
|
+
findingMarker(input.id),
|
|
75
|
+
].join("\n");
|
|
76
|
+
}
|
|
77
|
+
/** `commit` changed the finding's lines, `after` is the push. When they are
|
|
78
|
+
* the same commit it is written once. */
|
|
79
|
+
export function stillFoundComment(commit, after, range) {
|
|
80
|
+
const where = shortSha(commit) === shortSha(after)
|
|
81
|
+
? shortSha(commit)
|
|
82
|
+
: `${shortSha(commit)} (push ${shortSha(after)})`;
|
|
83
|
+
return `change in ${where}: changed, still found by the review of ${range}`;
|
|
84
|
+
}
|
|
85
|
+
export function fixedComment(commit, range) {
|
|
86
|
+
return `change in ${shortSha(commit)}: changed, no longer found by the review of ${range}. Closing.`;
|
|
87
|
+
}
|
|
88
|
+
export function closedStillFoundComment(range) {
|
|
89
|
+
return `This issue was closed, but the review of push ${range} still finds the problem. If there is something I should know, please leave it as a comment.`;
|
|
90
|
+
}
|
|
91
|
+
function write(client, endpoint, body) {
|
|
92
|
+
if (!client.write)
|
|
93
|
+
throw new Error("this GitHub client cannot post");
|
|
94
|
+
return client.write(endpoint, body);
|
|
95
|
+
}
|
|
96
|
+
/** Creates each label the issue needs. A label that already exists answers
|
|
97
|
+
* 422, which is fine. */
|
|
98
|
+
export async function ensureLabels(client, repo, names) {
|
|
99
|
+
for (const name of names) {
|
|
100
|
+
try {
|
|
101
|
+
await write(client, `repos/${repo}/labels`, {
|
|
102
|
+
name,
|
|
103
|
+
color: LABEL_COLORS[name] ?? "ededed",
|
|
104
|
+
description: "co-maintainer commit watch",
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
if (error.status !== 422)
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
export async function openIssue(client, repo, input) {
|
|
114
|
+
const labels = [ISSUE_LABEL, `cm-${input.severity}`];
|
|
115
|
+
await ensureLabels(client, repo, labels);
|
|
116
|
+
const created = await write(client, `repos/${repo}/issues`, {
|
|
117
|
+
title: input.title,
|
|
118
|
+
body: input.body,
|
|
119
|
+
labels,
|
|
120
|
+
});
|
|
121
|
+
const number = Number(created.number ?? 0);
|
|
122
|
+
if (!Number.isSafeInteger(number) || number <= 0) {
|
|
123
|
+
throw new Error("GitHub did not return the new issue's number");
|
|
124
|
+
}
|
|
125
|
+
return number;
|
|
126
|
+
}
|
|
127
|
+
export async function commentOnIssue(client, repo, issueNumber, body) {
|
|
128
|
+
const posted = await write(client, `repos/${repo}/issues/${issueNumber}/comments`, { body });
|
|
129
|
+
return String(posted.id ?? "");
|
|
130
|
+
}
|
|
131
|
+
/** Closes the issue, or leaves the comment that says it could not. Returns
|
|
132
|
+
* whether it closed. */
|
|
133
|
+
export async function closeIssue(client, repo, issueNumber) {
|
|
134
|
+
try {
|
|
135
|
+
if (!client.patch)
|
|
136
|
+
throw Object.assign(new Error("no PATCH"), { status: 403 });
|
|
137
|
+
await client.patch(`repos/${repo}/issues/${issueNumber}`, {
|
|
138
|
+
state: "closed",
|
|
139
|
+
state_reason: "completed",
|
|
140
|
+
});
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
const status = error.status;
|
|
145
|
+
if (status !== 403 && status !== 404)
|
|
146
|
+
throw error;
|
|
147
|
+
await commentOnIssue(client, repo, issueNumber, CLOSE_DENIED_COMMENT);
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** Whether the issue is open on GitHub, and when closed whether a bot (this
|
|
152
|
+
* App) closed it. */
|
|
153
|
+
export async function readIssueState(client, repo, issueNumber) {
|
|
154
|
+
try {
|
|
155
|
+
const issue = await client.request(`repos/${repo}/issues/${issueNumber}`);
|
|
156
|
+
const closedBy = issue.closed_by ?? undefined;
|
|
157
|
+
const login = String(closedBy?.login ?? "");
|
|
158
|
+
return {
|
|
159
|
+
open: issue.state === "open",
|
|
160
|
+
closedByBot: String(closedBy?.type ?? "") === "Bot" || login.endsWith("[bot]"),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/** A file's text at a commit (the default branch when `sha` is empty), or
|
|
168
|
+
* undefined when it is gone or unreadable. */
|
|
169
|
+
export async function fileAt(client, repo, path, sha) {
|
|
170
|
+
const encoded = path.split("/").map(encodeURIComponent).join("/");
|
|
171
|
+
try {
|
|
172
|
+
const query = sha ? `?ref=${sha}` : "";
|
|
173
|
+
const file = await client.request(`repos/${repo}/contents/${encoded}` + query);
|
|
174
|
+
if (typeof file.content !== "string")
|
|
175
|
+
return undefined;
|
|
176
|
+
return Buffer.from(file.content, "base64").toString("utf8");
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/** The newest commit at or before `sha` that touched `path`, which inside a
|
|
183
|
+
* push that changed the file is the commit that changed it. */
|
|
184
|
+
export async function lastCommitFor(client, repo, path, sha) {
|
|
185
|
+
try {
|
|
186
|
+
const commits = await client.request(`repos/${repo}/commits?sha=${sha}&path=${encodeURIComponent(path)}&per_page=1`);
|
|
187
|
+
const first = Array.isArray(commits) ? commits[0] : undefined;
|
|
188
|
+
return typeof first?.sha === "string" ? first.sha : undefined;
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/** Where `anchor` sits in `content`, the closest match to `oldFrom` when it
|
|
195
|
+
* appears more than once. Blank lines are skipped on both sides, the same
|
|
196
|
+
* way `normalizeAnchor` drops them. */
|
|
197
|
+
export function locateAnchor(content, anchor, oldFrom) {
|
|
198
|
+
if (!anchor)
|
|
199
|
+
return undefined;
|
|
200
|
+
const want = normalizeAnchor(anchor).split("\n").filter(Boolean);
|
|
201
|
+
if (want.length === 0)
|
|
202
|
+
return undefined;
|
|
203
|
+
const lines = content
|
|
204
|
+
.split("\n")
|
|
205
|
+
.map((text, index) => ({
|
|
206
|
+
number: index + 1,
|
|
207
|
+
text: text.trim().replace(/\s+/g, " "),
|
|
208
|
+
}))
|
|
209
|
+
.filter((line) => line.text);
|
|
210
|
+
let best;
|
|
211
|
+
for (let i = 0; i + want.length <= lines.length; i++) {
|
|
212
|
+
let ok = true;
|
|
213
|
+
for (let j = 0; j < want.length; j++) {
|
|
214
|
+
if (lines[i + j].text !== want[j]) {
|
|
215
|
+
ok = false;
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (!ok)
|
|
220
|
+
continue;
|
|
221
|
+
const from = lines[i].number;
|
|
222
|
+
const to = lines[i + want.length - 1].number;
|
|
223
|
+
const distance = Math.abs(from - oldFrom);
|
|
224
|
+
if (!best || distance < best.distance)
|
|
225
|
+
best = { from, to, distance };
|
|
226
|
+
}
|
|
227
|
+
return best ? { from: best.from, to: best.to } : undefined;
|
|
228
|
+
}
|
|
229
|
+
/** The new-file line numbers a patch adds or changes. */
|
|
230
|
+
export function changedLines(patch) {
|
|
231
|
+
const changed = new Set();
|
|
232
|
+
let next = 0;
|
|
233
|
+
for (const text of patch.split("\n")) {
|
|
234
|
+
const hunk = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(text);
|
|
235
|
+
if (hunk) {
|
|
236
|
+
next = Number(hunk[1]);
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (next === 0)
|
|
240
|
+
continue;
|
|
241
|
+
if (text.startsWith("+"))
|
|
242
|
+
changed.add(next++);
|
|
243
|
+
else if (text.startsWith("-")) {
|
|
244
|
+
// A removed line changes the place where it was.
|
|
245
|
+
changed.add(next);
|
|
246
|
+
}
|
|
247
|
+
else if (!text.startsWith("\\"))
|
|
248
|
+
next++;
|
|
249
|
+
}
|
|
250
|
+
return changed;
|
|
251
|
+
}
|
|
252
|
+
/** The finding's code with line numbers and a few lines around it, for the
|
|
253
|
+
* known-issue check. */
|
|
254
|
+
export function excerpt(content, from, to) {
|
|
255
|
+
const lines = content.split("\n");
|
|
256
|
+
const start = Math.max(1, from - 5);
|
|
257
|
+
const end = Math.min(lines.length, Math.max(from, to) + 5);
|
|
258
|
+
const out = [];
|
|
259
|
+
for (let n = start; n <= end; n++) {
|
|
260
|
+
out.push(`${String(n).padStart(6)} ${lines[n - 1] ?? ""}`);
|
|
261
|
+
}
|
|
262
|
+
return out.join("\n");
|
|
263
|
+
}
|
|
264
|
+
export function samePath(a, b) {
|
|
265
|
+
return Boolean(a && b && normalizePath(a) === normalizePath(b));
|
|
266
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AiProvider, AiResponse } from "../types.ts";
|
|
2
|
+
/** Applies the notes left since the last sync and returns every note line,
|
|
3
|
+
* old and new, for the detailed guide. With no app.db (a CLI sync on another
|
|
4
|
+
* machine) there are no notes. */
|
|
5
|
+
export declare function applyWatchNotes(repo: string, lowAi: AiProvider, usage?: (response: AiResponse) => Promise<void> | void): Promise<string[]>;
|
|
6
|
+
/** The guide with its maintainer notes section replaced by `lines`. The
|
|
7
|
+
* section is always last and starts with a marker, so it can be found again. */
|
|
8
|
+
export declare function withMaintainerNotes(guide: string, lines: string[]): string;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/** What `sync` does with the notes maintainers left on commit watch issues.
|
|
2
|
+
*
|
|
3
|
+
* Each new note is turned into one guide line by the low model. The lines go
|
|
4
|
+
* into the detailed review guide, so every later review knows them. A note
|
|
5
|
+
* that said to ignore the finding also marks its issue ignored, the hard
|
|
6
|
+
* filter: no issue, comment or report for that finding again. */
|
|
7
|
+
import { getWatchIssue, listWatchNotes, markWatchNoteApplied, updateWatchIssue, } from "../store/watch.js";
|
|
8
|
+
import { log } from "../util/log.js";
|
|
9
|
+
const SECTION_START = "<!-- co-maintainer:maintainer-notes -->";
|
|
10
|
+
const SECTION_TITLE = "## Maintainer notes";
|
|
11
|
+
function notePrompt(note, issueTitle, path) {
|
|
12
|
+
return `A maintainer replied on an issue an automated code reviewer opened.
|
|
13
|
+
Turn the reply into one line for the repository's review guide: a general
|
|
14
|
+
statement a future reviewer must follow, in English, at most 200 characters,
|
|
15
|
+
with no names, mentions, issue numbers or quotes. Return only that line.
|
|
16
|
+
|
|
17
|
+
The reviewer had reported: ${issueTitle}${path ? ` (in ${path})` : ""}
|
|
18
|
+
The maintainer ${note.kind === "ignore" ? "said to ignore it" : "added information"}:
|
|
19
|
+
${note.body.slice(0, 4000)}`;
|
|
20
|
+
}
|
|
21
|
+
function oneLine(text) {
|
|
22
|
+
return text
|
|
23
|
+
.replace(/```[\s\S]*?```/g, "")
|
|
24
|
+
.replace(/@[A-Za-z0-9_-]{2,}/g, "")
|
|
25
|
+
.replace(/\s+/g, " ")
|
|
26
|
+
.replace(/^[-*\s]+/, "")
|
|
27
|
+
.trim()
|
|
28
|
+
.slice(0, 300);
|
|
29
|
+
}
|
|
30
|
+
/** Applies the notes left since the last sync and returns every note line,
|
|
31
|
+
* old and new, for the detailed guide. With no app.db (a CLI sync on another
|
|
32
|
+
* machine) there are no notes. */
|
|
33
|
+
export async function applyWatchNotes(repo, lowAi, usage) {
|
|
34
|
+
let notes;
|
|
35
|
+
try {
|
|
36
|
+
notes = listWatchNotes(repo);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
for (const note of notes) {
|
|
42
|
+
if (note.applied_at)
|
|
43
|
+
continue;
|
|
44
|
+
const issue = getWatchIssue(repo, note.issue_number);
|
|
45
|
+
const response = await lowAi.complete({
|
|
46
|
+
job: "watch_note",
|
|
47
|
+
prompt: notePrompt(note, issue?.title ?? "a finding", issue?.path ?? null),
|
|
48
|
+
maxTokens: 300,
|
|
49
|
+
});
|
|
50
|
+
await usage?.(response);
|
|
51
|
+
const line = oneLine(response.text) || oneLine(note.body);
|
|
52
|
+
const about = issue?.path ? ` (about \`${issue.path}\`)` : "";
|
|
53
|
+
note.note = `${line}${about}`;
|
|
54
|
+
markWatchNoteApplied(note.id, note.note);
|
|
55
|
+
if (note.kind === "ignore" && issue) {
|
|
56
|
+
updateWatchIssue(issue.id, { state: "ignored" });
|
|
57
|
+
}
|
|
58
|
+
log("write", `maintainer note from issue #${note.issue_number} added to the guide`);
|
|
59
|
+
}
|
|
60
|
+
return notes
|
|
61
|
+
.map((note) => note.note)
|
|
62
|
+
.filter((line) => Boolean(line));
|
|
63
|
+
}
|
|
64
|
+
/** The guide with its maintainer notes section replaced by `lines`. The
|
|
65
|
+
* section is always last and starts with a marker, so it can be found again. */
|
|
66
|
+
export function withMaintainerNotes(guide, lines) {
|
|
67
|
+
const at = guide.indexOf(SECTION_START);
|
|
68
|
+
const body = (at === -1 ? guide : guide.slice(0, at)).trimEnd();
|
|
69
|
+
if (lines.length === 0)
|
|
70
|
+
return body ? `${body}\n` : "";
|
|
71
|
+
const section = [
|
|
72
|
+
SECTION_START,
|
|
73
|
+
SECTION_TITLE,
|
|
74
|
+
"",
|
|
75
|
+
"Maintainers said these on commit watch issues. Follow them in every review.",
|
|
76
|
+
"",
|
|
77
|
+
...lines.map((line) => `- ${line}`),
|
|
78
|
+
].join("\n");
|
|
79
|
+
return body ? `${body}\n\n${section}\n` : `# Review notes\n\n${section}\n`;
|
|
80
|
+
}
|