co-maintainer 0.6.13 → 0.7.0-beta.1
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/README.md +1 -1
- package/dist/package.json +1 -1
- package/dist/src/ai/anthropic.js +2 -0
- package/dist/src/ai/batch.d.ts +3 -1
- package/dist/src/ai/batch.js +23 -2
- package/dist/src/ai/mermaid_loop.js +1 -1
- package/dist/src/ai/oauth/sign-in-with-chatgpt.js +8 -10
- package/dist/src/ai/openai.js +2 -0
- package/dist/src/ai/openrouter.js +2 -0
- package/dist/src/cli/args.js +47 -0
- package/dist/src/cli/command_json.d.ts +20 -1
- package/dist/src/cli/command_json.js +4 -0
- package/dist/src/cli/commands/config.d.ts +1 -1
- package/dist/src/cli/commands/config.js +1 -0
- package/dist/src/cli/commands/registry.js +226 -1
- package/dist/src/cli/commands/review.js +50 -9
- package/dist/src/cli/commands/serve.js +5 -0
- package/dist/src/cli/commands/set.js +70 -2
- package/dist/src/cli/commands/view.js +46 -5
- package/dist/src/cli/commands/view_sources.d.ts +11 -0
- package/dist/src/cli/commands/view_sources.js +40 -0
- package/dist/src/cli/main.js +5 -0
- package/dist/src/cli/pr_carry.d.ts +25 -0
- package/dist/src/cli/pr_carry.js +128 -0
- package/dist/src/cli/prompt.js +26 -1
- package/dist/src/cli/review_args.d.ts +8 -0
- package/dist/src/cli/review_args.js +18 -0
- package/dist/src/cli/review_carry.d.ts +32 -0
- package/dist/src/cli/review_carry.js +60 -0
- package/dist/src/cli/review_github.d.ts +16 -2
- package/dist/src/cli/review_github.js +49 -12
- package/dist/src/cli/review_intent.d.ts +25 -0
- package/dist/src/cli/review_intent.js +87 -0
- package/dist/src/cli/review_result.d.ts +53 -2
- package/dist/src/cli/review_result.js +90 -23
- package/dist/src/config.d.ts +27 -0
- package/dist/src/config.js +20 -12
- package/dist/src/extensions/command.d.ts +1 -0
- package/dist/src/extensions/command.js +355 -0
- package/dist/src/extensions/contexts.d.ts +65 -0
- package/dist/src/extensions/contexts.js +134 -0
- package/dist/src/extensions/cost.d.ts +8 -0
- package/dist/src/extensions/cost.js +6 -0
- package/dist/src/extensions/local_tracking.d.ts +7 -0
- package/dist/src/extensions/local_tracking.js +29 -0
- package/dist/src/extensions/mock.d.ts +29 -0
- package/dist/src/extensions/mock.js +67 -0
- package/dist/src/extensions/process.d.ts +43 -0
- package/dist/src/extensions/process.js +244 -0
- package/dist/src/extensions/registry.d.ts +79 -0
- package/dist/src/extensions/registry.js +240 -0
- package/dist/src/extensions/render_actions.d.ts +6 -0
- package/dist/src/extensions/render_actions.js +50 -0
- package/dist/src/extensions/render_cli.d.ts +18 -0
- package/dist/src/extensions/render_cli.js +88 -0
- package/dist/src/extensions/render_json.d.ts +51 -0
- package/dist/src/extensions/render_json.js +144 -0
- package/dist/src/extensions/render_markdown.d.ts +22 -0
- package/dist/src/extensions/render_markdown.js +96 -0
- package/dist/src/extensions/render_review.d.ts +44 -0
- package/dist/src/extensions/render_review.js +167 -0
- package/dist/src/extensions/report.d.ts +57 -0
- package/dist/src/extensions/report.js +97 -0
- package/dist/src/extensions/review_result.d.ts +2 -0
- package/dist/src/extensions/review_result.js +14 -0
- package/dist/src/extensions/run_folder.d.ts +74 -0
- package/dist/src/extensions/run_folder.js +172 -0
- package/dist/src/extensions/runner.d.ts +66 -0
- package/dist/src/extensions/runner.js +251 -0
- package/dist/src/extensions/schema.d.ts +103 -0
- package/dist/src/extensions/schema.js +539 -0
- package/dist/src/extensions/session.d.ts +56 -0
- package/dist/src/extensions/session.js +173 -0
- package/dist/src/github/collect.d.ts +7 -2
- package/dist/src/github/collect.js +66 -6
- package/dist/src/github/gh.d.ts +7 -0
- package/dist/src/github/gh.js +24 -29
- package/dist/src/knowledge/guide.js +3 -2
- package/dist/src/knowledge/instruction_files.d.ts +45 -0
- package/dist/src/knowledge/instruction_files.js +91 -0
- package/dist/src/knowledge/instructions.d.ts +78 -0
- package/dist/src/knowledge/instructions.js +280 -0
- package/dist/src/knowledge/rule_changes.d.ts +39 -0
- package/dist/src/knowledge/rule_changes.js +90 -0
- package/dist/src/knowledge/rules.d.ts +44 -0
- package/dist/src/knowledge/rules.js +82 -0
- package/dist/src/knowledge/synthesis.d.ts +1 -0
- package/dist/src/knowledge/synthesis.js +12 -5
- package/dist/src/knowledge/types.d.ts +4 -0
- package/dist/src/local/carry_over_store.d.ts +10 -0
- package/dist/src/local/carry_over_store.js +17 -0
- package/dist/src/local/review_local.js +100 -62
- package/dist/src/local/review_lock.js +15 -4
- package/dist/src/pr/diff_plan.d.ts +110 -0
- package/dist/src/pr/diff_plan.js +429 -0
- package/dist/src/pr/diff_summary.d.ts +17 -1
- package/dist/src/pr/diff_summary.js +87 -13
- package/dist/src/pr/review_diff.d.ts +47 -0
- package/dist/src/pr/review_diff.js +112 -0
- package/dist/src/pr/reviewer.d.ts +44 -19
- package/dist/src/pr/reviewer.js +114 -142
- package/dist/src/remote/client.js +67 -8
- package/dist/src/remote/http.d.ts +12 -0
- package/dist/src/remote/http.js +31 -0
- package/dist/src/remote/push_client.js +14 -3
- package/dist/src/remote/server/guides.js +12 -1
- package/dist/src/remote/server/routes.js +4 -2
- package/dist/src/remote/server/sessions.js +4 -1
- package/dist/src/remote/validate.js +11 -0
- package/dist/src/review/carry_over.d.ts +6 -1
- package/dist/src/review/carry_over.js +10 -2
- package/dist/src/review/coverage.d.ts +57 -0
- package/dist/src/review/coverage.js +186 -0
- package/dist/src/review/diagram.d.ts +73 -0
- package/dist/src/review/diagram.js +309 -0
- package/dist/src/review/engine.d.ts +2 -6
- package/dist/src/review/engine.js +1 -1
- package/dist/src/review/guides.d.ts +6 -0
- package/dist/src/review/guides.js +8 -1
- package/dist/src/review/intent.d.ts +24 -0
- package/dist/src/review/intent.js +28 -0
- package/dist/src/review/not_initialized.d.ts +13 -0
- package/dist/src/review/not_initialized.js +18 -0
- package/dist/src/review/system_prompt.d.ts +6 -0
- package/dist/src/review/system_prompt.js +13 -0
- package/dist/src/review/visible_paths.d.ts +22 -0
- package/dist/src/review/visible_paths.js +41 -0
- package/dist/src/server/pages/extensions.d.ts +5 -0
- package/dist/src/server/pages/extensions.js +72 -0
- package/dist/src/server/pages/pr.js +3 -2
- package/dist/src/services/dashboard.d.ts +8 -0
- package/dist/src/services/dashboard.js +8 -0
- package/dist/src/services/push_review.js +14 -5
- package/dist/src/services/remote_review.js +81 -26
- package/dist/src/services/replies.js +15 -8
- package/dist/src/services/review.d.ts +9 -3
- package/dist/src/services/review.js +109 -28
- package/dist/src/services/rules_build.d.ts +46 -0
- package/dist/src/services/rules_build.js +155 -0
- package/dist/src/services/setup.d.ts +31 -0
- package/dist/src/services/setup.js +120 -25
- package/dist/src/services/watch_comments.js +2 -0
- package/dist/src/store/cache_db.js +3 -0
- package/dist/src/store/extensions.d.ts +51 -0
- package/dist/src/store/extensions.js +132 -0
- package/dist/src/store/migrations.js +40 -0
- package/dist/src/types.d.ts +22 -1
- package/dist/src/util/atomic.d.ts +15 -1
- package/dist/src/util/atomic.js +59 -29
- package/dist/src/util/interrupt.d.ts +6 -0
- package/dist/src/util/interrupt.js +27 -0
- package/dist/src/util/runtime.d.ts +3 -1
- package/dist/src/util/runtime.js +2 -2
- package/dist/src/util/test_location_guard.d.ts +6 -0
- package/dist/src/util/test_location_guard.js +47 -0
- package/package.json +1 -1
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import { DIFF_SUMMARY_THRESHOLD } from "./diff_summary.js";
|
|
2
|
+
import { numberPatch } from "./hunks.js";
|
|
3
|
+
/** A longer patch is shown up to here and marked `partial`. */
|
|
4
|
+
export const MAX_FILE_PATCH_CHARS = 12_000;
|
|
5
|
+
/** The whole DIFF section never grows past this. */
|
|
6
|
+
export const MAX_REVIEW_DIFF_CHARS = 240_000;
|
|
7
|
+
/** What the plan counts for one summary before the summary exists. The real
|
|
8
|
+
* length is checked again once the summaries come back. */
|
|
9
|
+
export const PLANNED_SUMMARY_CHARS = 700;
|
|
10
|
+
/** Only a patch longer than this is summarized. A shorter one saves too little
|
|
11
|
+
* room to be worth a model call and the loss of its code, so it is shown in
|
|
12
|
+
* full or dropped. */
|
|
13
|
+
export const SUMMARY_MIN_PATCH_CHARS = 4_000;
|
|
14
|
+
/** At most this many summaries per review, largest files first. Each is one
|
|
15
|
+
* low-model call, and a review made of summaries has no code to judge. */
|
|
16
|
+
export const MAX_SUMMARIES = 40;
|
|
17
|
+
function count(value) {
|
|
18
|
+
const number = Number(value ?? 0);
|
|
19
|
+
return Number.isFinite(number) ? number : 0;
|
|
20
|
+
}
|
|
21
|
+
/** A pure rename: GitHub reports it as `renamed` with no changed lines and
|
|
22
|
+
* sends no patch, because there is none. */
|
|
23
|
+
function pureRename(status, changes) {
|
|
24
|
+
return status === "renamed" && changes === 0;
|
|
25
|
+
}
|
|
26
|
+
/** A file from GitHub's pull request or compare API. GitHub leaves `patch`
|
|
27
|
+
* out for a file it considers too large or binary and gives no reason, so an
|
|
28
|
+
* empty patch is `withheld`. The one exception is a pure rename, which has
|
|
29
|
+
* nothing to withhold. */
|
|
30
|
+
export function diffFileFromGithub(file) {
|
|
31
|
+
const patch = String(file.patch ?? "");
|
|
32
|
+
const status = String(file.status ?? "modified");
|
|
33
|
+
const changes = count(file.changes);
|
|
34
|
+
const empty = patch !== ""
|
|
35
|
+
? undefined
|
|
36
|
+
: pureRename(status, changes)
|
|
37
|
+
? "renamed"
|
|
38
|
+
: "withheld";
|
|
39
|
+
return {
|
|
40
|
+
path: String(file.filename ?? ""),
|
|
41
|
+
previousPath: file.previous_filename
|
|
42
|
+
? String(file.previous_filename)
|
|
43
|
+
: null,
|
|
44
|
+
status,
|
|
45
|
+
additions: count(file.additions),
|
|
46
|
+
deletions: count(file.deletions),
|
|
47
|
+
changes,
|
|
48
|
+
patch,
|
|
49
|
+
...(empty ? { empty } : {}),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** A file from a `Revision`. A revision built from git knows why a patch is
|
|
53
|
+
* empty, so a binary file or a pure rename is labeled as what it is instead
|
|
54
|
+
* of being described as withheld. */
|
|
55
|
+
export function diffFileFromRevision(file, producer) {
|
|
56
|
+
const changes = file.additions + file.deletions;
|
|
57
|
+
const empty = file.patch !== ""
|
|
58
|
+
? undefined
|
|
59
|
+
: file.binary
|
|
60
|
+
? "binary"
|
|
61
|
+
: pureRename(file.status, changes)
|
|
62
|
+
? "renamed"
|
|
63
|
+
: producer === "github"
|
|
64
|
+
? "withheld"
|
|
65
|
+
: "no_changes";
|
|
66
|
+
return {
|
|
67
|
+
path: file.path,
|
|
68
|
+
previousPath: file.previousPath,
|
|
69
|
+
status: file.status,
|
|
70
|
+
additions: file.additions,
|
|
71
|
+
deletions: file.deletions,
|
|
72
|
+
changes,
|
|
73
|
+
patch: file.patch,
|
|
74
|
+
...(empty ? { empty } : {}),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** The patch cut to at most `max` characters at a line end, so a partial file
|
|
78
|
+
* never shows half a line and its next line has a clean offset. */
|
|
79
|
+
export function cutAtLine(patch, max = MAX_FILE_PATCH_CHARS) {
|
|
80
|
+
if (patch.length <= max)
|
|
81
|
+
return patch;
|
|
82
|
+
const end = patch.lastIndexOf("\n", max);
|
|
83
|
+
return end > 0 ? patch.slice(0, end) : patch.slice(0, max);
|
|
84
|
+
}
|
|
85
|
+
/** GitHub omits `patch` for files it considers too large, and the file still
|
|
86
|
+
* appears with only its counts. Rendering that as an empty body reads as "this
|
|
87
|
+
* file did not change", and a reviewer then reports the absence as a finding
|
|
88
|
+
* (a lockfile that was in fact regenerated, say). Say plainly that the hunks
|
|
89
|
+
* are missing. A file git reports without hunks gets its real reason. */
|
|
90
|
+
function emptyLabel(file) {
|
|
91
|
+
switch (file.empty) {
|
|
92
|
+
case "binary":
|
|
93
|
+
return `[${file.status}: binary file]`;
|
|
94
|
+
case "renamed":
|
|
95
|
+
return `[renamed without changes${file.previousPath ? ` from ${file.previousPath}` : ""}]`;
|
|
96
|
+
case "no_changes":
|
|
97
|
+
return `[${file.status}: no changed lines]`;
|
|
98
|
+
default: {
|
|
99
|
+
const counts = file.changes > 0
|
|
100
|
+
? `${file.changes} changed lines (+${file.additions} -${file.deletions})`
|
|
101
|
+
: "an unreported number of changed lines";
|
|
102
|
+
return (`[${file.status}: ${counts}, diff withheld by GitHub, not shown here. ` +
|
|
103
|
+
`Do not treat this file as unchanged and do not report its contents.]`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** The same facts as `emptyLabel`, as one line of the NO DIFF TEXT list. */
|
|
108
|
+
function noDiffLine(file) {
|
|
109
|
+
switch (file.empty) {
|
|
110
|
+
case "binary":
|
|
111
|
+
return `- ${file.path}: binary file`;
|
|
112
|
+
case "renamed":
|
|
113
|
+
return `- ${file.path}: renamed without changes${file.previousPath ? ` from ${file.previousPath}` : ""}`;
|
|
114
|
+
case "no_changes":
|
|
115
|
+
return `- ${file.path}: no changed lines`;
|
|
116
|
+
default:
|
|
117
|
+
return `- ${file.path}: diff withheld by GitHub (+${file.additions} -${file.deletions})`;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const NO_DIFF_HEAD = [
|
|
121
|
+
"NO DIFF TEXT: these files changed, but there is no diff text to show for",
|
|
122
|
+
"them. Do not treat a withheld file as unchanged and do not report its contents.",
|
|
123
|
+
].join("\n");
|
|
124
|
+
/** The NO DIFF TEXT list, or `""` when no file is listed. */
|
|
125
|
+
export function noDiffBlock(files) {
|
|
126
|
+
const listed = files.filter((file) => file.listed);
|
|
127
|
+
if (listed.length === 0)
|
|
128
|
+
return "";
|
|
129
|
+
return [NO_DIFF_HEAD, ...capped(listed.map(noDiffLine))].join("\n");
|
|
130
|
+
}
|
|
131
|
+
/** One file's block in the DIFF section, or `""` for a dropped file, which
|
|
132
|
+
* is listed under NOT SHOWN instead, and for a listed file, which is one line
|
|
133
|
+
* of NO DIFF TEXT. */
|
|
134
|
+
export function renderSection(file, options) {
|
|
135
|
+
const head = `FILE: ${file.path}\n`;
|
|
136
|
+
if (file.listed)
|
|
137
|
+
return "";
|
|
138
|
+
switch (file.state) {
|
|
139
|
+
case "dropped":
|
|
140
|
+
return "";
|
|
141
|
+
case "withheld":
|
|
142
|
+
return `${head}${emptyLabel(file)}`;
|
|
143
|
+
case "full":
|
|
144
|
+
return `${head}${file.patch === "" ? emptyLabel(file) : numberPatch(file.patch)}`;
|
|
145
|
+
case "partial": {
|
|
146
|
+
const shown = cutAtLine(file.patch);
|
|
147
|
+
// A short file lands here only when its summary failed. Nothing was
|
|
148
|
+
// cut, so no marker claims otherwise.
|
|
149
|
+
if (shown === file.patch)
|
|
150
|
+
return `${head}${numberPatch(file.patch)}`;
|
|
151
|
+
const next = shown.split("\n").length;
|
|
152
|
+
const rest = options.tools
|
|
153
|
+
? `Call read-full-diff with path "${file.path}" and offset=${next} for the rest.`
|
|
154
|
+
: "The rest cannot be read in this review.";
|
|
155
|
+
return (`${head}${numberPatch(shown)}\n[${file.status}: ${file.changes} ` +
|
|
156
|
+
`changed lines total. This file's diff is cut off here, later hunks are ` +
|
|
157
|
+
`not shown. ${rest}]`);
|
|
158
|
+
}
|
|
159
|
+
case "summarized": {
|
|
160
|
+
const read = options.tools
|
|
161
|
+
? ` call read-full-diff("${file.path}") for the complete diff if this is not enough`
|
|
162
|
+
: "";
|
|
163
|
+
return `${head}[${file.changes} changed lines, summarized below.${read}]\n${options.summary ?? ""}`;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/** A list of file names in the prompt names at most this many, then says how
|
|
168
|
+
* many more there are. A local review of a moved or vendored tree has no
|
|
169
|
+
* file limit, and an unbounded list could outgrow the model's context. */
|
|
170
|
+
export const MAX_LISTED_FILES = 200;
|
|
171
|
+
const NOT_SHOWN_HEAD = [
|
|
172
|
+
"NOT SHOWN (diff budget): these files changed, but their diff did not fit in",
|
|
173
|
+
"this prompt and they were not reviewed.",
|
|
174
|
+
];
|
|
175
|
+
const NOT_SHOWN_HOW = {
|
|
176
|
+
tools: "Call read-full-diff to open one before reporting on it.",
|
|
177
|
+
none: "This model cannot call tools, so they cannot be read here. Do not report on them.",
|
|
178
|
+
};
|
|
179
|
+
function droppedLine(file) {
|
|
180
|
+
return `- ${file.path} (+${file.additions} -${file.deletions})`;
|
|
181
|
+
}
|
|
182
|
+
/** At most `MAX_LISTED_FILES` lines, then one line for the rest. */
|
|
183
|
+
function capped(lines) {
|
|
184
|
+
if (lines.length <= MAX_LISTED_FILES)
|
|
185
|
+
return lines;
|
|
186
|
+
return [
|
|
187
|
+
...lines.slice(0, MAX_LISTED_FILES),
|
|
188
|
+
`- and ${lines.length - MAX_LISTED_FILES} more`,
|
|
189
|
+
];
|
|
190
|
+
}
|
|
191
|
+
/** The NOT SHOWN list for dropped files, or `""` when none was dropped. */
|
|
192
|
+
export function notShownBlock(files, options) {
|
|
193
|
+
const dropped = files.filter((file) => file.state === "dropped");
|
|
194
|
+
if (dropped.length === 0)
|
|
195
|
+
return "";
|
|
196
|
+
const [first, second] = NOT_SHOWN_HEAD;
|
|
197
|
+
const how = options.tools ? NOT_SHOWN_HOW.tools : NOT_SHOWN_HOW.none;
|
|
198
|
+
return [first, `${second} ${how}`, ...capped(dropped.map(droppedLine))].join("\n");
|
|
199
|
+
}
|
|
200
|
+
const SEPARATOR = "\n\n".length;
|
|
201
|
+
/** Room for the "- and N more" line and its line break, for any N. */
|
|
202
|
+
const MORE_LINE_CHARS = "\n- and 9999999999 more".length;
|
|
203
|
+
/** The most a capped list can add: its heading, the longest
|
|
204
|
+
* `MAX_LISTED_FILES` lines it could show, and the "and N more" line. Kept as
|
|
205
|
+
* running totals, so a plan over thousands of files stays linear. */
|
|
206
|
+
class ListBound {
|
|
207
|
+
count = 0;
|
|
208
|
+
sum = 0;
|
|
209
|
+
longest = 0;
|
|
210
|
+
head;
|
|
211
|
+
constructor(head) {
|
|
212
|
+
this.head = head;
|
|
213
|
+
}
|
|
214
|
+
add(line) {
|
|
215
|
+
this.count++;
|
|
216
|
+
this.sum += line + 1;
|
|
217
|
+
this.longest = Math.max(this.longest, line + 1);
|
|
218
|
+
}
|
|
219
|
+
/** The longest line stays as it was, which keeps this an upper bound. */
|
|
220
|
+
remove(line) {
|
|
221
|
+
this.count--;
|
|
222
|
+
this.sum -= line + 1;
|
|
223
|
+
}
|
|
224
|
+
size() {
|
|
225
|
+
if (this.count === 0)
|
|
226
|
+
return 0;
|
|
227
|
+
const more = this.count > MAX_LISTED_FILES ? MORE_LINE_CHARS : 0;
|
|
228
|
+
return (this.head + Math.min(this.sum, MAX_LISTED_FILES * this.longest) + more);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
/** What the DIFF section will weigh: the file sections joined by blank lines,
|
|
232
|
+
* then the NO DIFF TEXT and NOT SHOWN lists. All of it counts against
|
|
233
|
+
* `MAX_REVIEW_DIFF_CHARS`. */
|
|
234
|
+
class Budget {
|
|
235
|
+
sections = 0;
|
|
236
|
+
shown = 0;
|
|
237
|
+
dropped = new ListBound(NOT_SHOWN_HEAD.join("\n").length +
|
|
238
|
+
Math.max(NOT_SHOWN_HOW.tools.length, NOT_SHOWN_HOW.none.length) +
|
|
239
|
+
2);
|
|
240
|
+
noDiff = new ListBound(NO_DIFF_HEAD.length);
|
|
241
|
+
size;
|
|
242
|
+
constructor(files, size) {
|
|
243
|
+
this.size = size;
|
|
244
|
+
for (const file of files) {
|
|
245
|
+
if (file.state === "dropped")
|
|
246
|
+
this.dropped.add(droppedLine(file).length);
|
|
247
|
+
else if (file.listed)
|
|
248
|
+
this.noDiff.add(noDiffLine(file).length);
|
|
249
|
+
else
|
|
250
|
+
this.show(file);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/** Moves a file with no patch text from its own block into the list. */
|
|
254
|
+
list(file) {
|
|
255
|
+
this.hide(file);
|
|
256
|
+
file.listed = true;
|
|
257
|
+
this.noDiff.add(noDiffLine(file).length);
|
|
258
|
+
}
|
|
259
|
+
show(file) {
|
|
260
|
+
this.sections += this.size(file);
|
|
261
|
+
this.shown++;
|
|
262
|
+
}
|
|
263
|
+
hide(file) {
|
|
264
|
+
this.sections -= this.size(file);
|
|
265
|
+
this.shown--;
|
|
266
|
+
}
|
|
267
|
+
drop(file) {
|
|
268
|
+
this.hide(file);
|
|
269
|
+
file.state = "dropped";
|
|
270
|
+
this.dropped.add(droppedLine(file).length);
|
|
271
|
+
}
|
|
272
|
+
restore(file, state) {
|
|
273
|
+
this.dropped.remove(droppedLine(file).length);
|
|
274
|
+
file.state = state;
|
|
275
|
+
this.show(file);
|
|
276
|
+
}
|
|
277
|
+
total() {
|
|
278
|
+
const parts = [
|
|
279
|
+
this.shown > 0 ? this.sections + SEPARATOR * (this.shown - 1) : 0,
|
|
280
|
+
this.noDiff.size(),
|
|
281
|
+
this.dropped.size(),
|
|
282
|
+
].filter((part) => part > 0);
|
|
283
|
+
return (parts.reduce((sum, part) => sum + part, 0) +
|
|
284
|
+
SEPARATOR * Math.max(0, parts.length - 1));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
const DROPPABLE = new Set(["full", "partial", "summarized"]);
|
|
288
|
+
/** Plain code-point order. `localeCompare` follows the process locale, so a
|
|
289
|
+
* server and a laptop could drop different files from the same diff. */
|
|
290
|
+
export function byPath(a, b) {
|
|
291
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
292
|
+
}
|
|
293
|
+
/** Over budget, every file with no patch text becomes one line of the capped
|
|
294
|
+
* NO DIFF TEXT list. Its marker carries no code, so it goes before any file
|
|
295
|
+
* that has a patch loses room. */
|
|
296
|
+
function listPatchless(files, budget, max) {
|
|
297
|
+
if (budget.total() <= max)
|
|
298
|
+
return;
|
|
299
|
+
for (const file of files) {
|
|
300
|
+
if (file.patch === "" && !file.listed && file.state !== "dropped") {
|
|
301
|
+
budget.list(file);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
/** Drops files from the end of path order until the DIFF section fits. Both
|
|
306
|
+
* the plan and the pass after the real summaries use it. Only a file with a
|
|
307
|
+
* patch is dropped: files without one are listed first. Mutates `files`. */
|
|
308
|
+
export function dropUntilFits(files, size, max = MAX_REVIEW_DIFF_CHARS) {
|
|
309
|
+
const budget = new Budget(files, size);
|
|
310
|
+
listPatchless(files, budget, max);
|
|
311
|
+
if (budget.total() <= max)
|
|
312
|
+
return;
|
|
313
|
+
const fromEnd = [...files].sort((a, b) => byPath(b.path, a.path));
|
|
314
|
+
for (const file of fromEnd) {
|
|
315
|
+
if (budget.total() <= max)
|
|
316
|
+
return;
|
|
317
|
+
if (!DROPPABLE.has(file.state) || file.patch === "")
|
|
318
|
+
continue;
|
|
319
|
+
budget.drop(file);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
/** The state a file has without a summary: its whole patch, or the first
|
|
323
|
+
* `MAX_FILE_PATCH_CHARS` of it. */
|
|
324
|
+
function shownState(file) {
|
|
325
|
+
if (file.patch === "") {
|
|
326
|
+
return file.empty === "withheld" || file.empty === undefined
|
|
327
|
+
? "withheld"
|
|
328
|
+
: "full";
|
|
329
|
+
}
|
|
330
|
+
return file.patch.length > MAX_FILE_PATCH_CHARS ? "partial" : "full";
|
|
331
|
+
}
|
|
332
|
+
function summaryCandidate(file) {
|
|
333
|
+
return file.patch.length > SUMMARY_MIN_PATCH_CHARS;
|
|
334
|
+
}
|
|
335
|
+
/** After the drop pass the budget can have room left, because a large file
|
|
336
|
+
* went where several small ones would have fit. Dropped files that fit come
|
|
337
|
+
* back, smallest first, without a summary. Mutates `files`. */
|
|
338
|
+
export function restoreIntoRoom(files, size, max = MAX_REVIEW_DIFF_CHARS) {
|
|
339
|
+
const budget = new Budget(files, size);
|
|
340
|
+
if (budget.total() >= max)
|
|
341
|
+
return;
|
|
342
|
+
const dropped = files
|
|
343
|
+
.filter((file) => file.state === "dropped")
|
|
344
|
+
.map((file) => ({ file, state: shownState(file) }))
|
|
345
|
+
.map((item) => ({
|
|
346
|
+
...item,
|
|
347
|
+
chars: size({ ...item.file, state: item.state }),
|
|
348
|
+
}))
|
|
349
|
+
.sort((a, b) => a.chars - b.chars || byPath(a.file.path, b.file.path));
|
|
350
|
+
for (const { file, state } of dropped) {
|
|
351
|
+
budget.restore(file, state);
|
|
352
|
+
if (budget.total() > max)
|
|
353
|
+
budget.drop(file);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/** The plan. Files keep their input order, and only dropping looks at path
|
|
357
|
+
* order. Preference is always full, then partial or summarized, then
|
|
358
|
+
* dropped.
|
|
359
|
+
*
|
|
360
|
+
* 1. A file over `DIFF_SUMMARY_THRESHOLD` changed lines is summarized.
|
|
361
|
+
* 2. A patch over `MAX_FILE_PATCH_CHARS` is partial.
|
|
362
|
+
* 3. While the total is over `MAX_REVIEW_DIFF_CHARS`, the largest full or
|
|
363
|
+
* partial file is summarized.
|
|
364
|
+
* 4. If that is not enough, files are dropped from the end of path order.
|
|
365
|
+
* 5. Dropped files that fit in the room left come back, smallest first.
|
|
366
|
+
*
|
|
367
|
+
* Only a patch over `SUMMARY_MIN_PATCH_CHARS` is ever summarized, by rule 1
|
|
368
|
+
* or 3, and at most `MAX_SUMMARIES` per review, largest first. */
|
|
369
|
+
export function planDiff(files, max = MAX_REVIEW_DIFF_CHARS) {
|
|
370
|
+
const planned = files.map((file) => ({
|
|
371
|
+
...file,
|
|
372
|
+
state: shownState(file),
|
|
373
|
+
}));
|
|
374
|
+
const overThreshold = planned
|
|
375
|
+
.filter((file) => file.changes > DIFF_SUMMARY_THRESHOLD && summaryCandidate(file))
|
|
376
|
+
.sort((a, b) => b.patch.length - a.patch.length)
|
|
377
|
+
.slice(0, MAX_SUMMARIES);
|
|
378
|
+
for (const file of overThreshold)
|
|
379
|
+
file.state = "summarized";
|
|
380
|
+
let summaries = overThreshold.length;
|
|
381
|
+
const cache = new Map();
|
|
382
|
+
const size = (file) => {
|
|
383
|
+
const key = `${file.state}\0${file.path}`;
|
|
384
|
+
const known = cache.get(key);
|
|
385
|
+
if (known !== undefined)
|
|
386
|
+
return known;
|
|
387
|
+
const length = renderSection(file, {
|
|
388
|
+
tools: true,
|
|
389
|
+
summary: "x".repeat(PLANNED_SUMMARY_CHARS),
|
|
390
|
+
}).length;
|
|
391
|
+
cache.set(key, length);
|
|
392
|
+
return length;
|
|
393
|
+
};
|
|
394
|
+
const asSummary = (file) => size({ ...file, state: "summarized" });
|
|
395
|
+
const budget = new Budget(planned, size);
|
|
396
|
+
listPatchless(planned, budget, max);
|
|
397
|
+
while (budget.total() > max && summaries < MAX_SUMMARIES) {
|
|
398
|
+
let largest;
|
|
399
|
+
for (const file of planned) {
|
|
400
|
+
if (file.state !== "full" && file.state !== "partial")
|
|
401
|
+
continue;
|
|
402
|
+
if (!summaryCandidate(file))
|
|
403
|
+
continue;
|
|
404
|
+
if (!largest || size(file) > size(largest))
|
|
405
|
+
largest = file;
|
|
406
|
+
}
|
|
407
|
+
if (!largest || size(largest) <= asSummary(largest))
|
|
408
|
+
break;
|
|
409
|
+
budget.hide(largest);
|
|
410
|
+
largest.state = "summarized";
|
|
411
|
+
budget.show(largest);
|
|
412
|
+
summaries++;
|
|
413
|
+
}
|
|
414
|
+
dropUntilFits(planned, size, max);
|
|
415
|
+
restoreIntoRoom(planned, size, max);
|
|
416
|
+
return planned;
|
|
417
|
+
}
|
|
418
|
+
/** The coverage block for a planned round. A file carried over unchanged from
|
|
419
|
+
* an earlier round counts as full: it needs no new review. */
|
|
420
|
+
export function coverageFromPlan(planned, unchangedPaths = []) {
|
|
421
|
+
const paths = (state) => planned.filter((file) => file.state === state).map((file) => file.path);
|
|
422
|
+
return {
|
|
423
|
+
full: paths("full").length + unchangedPaths.length,
|
|
424
|
+
partial: paths("partial"),
|
|
425
|
+
summarized: paths("summarized"),
|
|
426
|
+
dropped: paths("dropped"),
|
|
427
|
+
withheld: paths("withheld"),
|
|
428
|
+
};
|
|
429
|
+
}
|
|
@@ -2,5 +2,21 @@ import type { AiProvider, AiResponse, Json } from "../types.ts";
|
|
|
2
2
|
export declare const DIFF_SUMMARY_THRESHOLD = 500;
|
|
3
3
|
export declare function needsSummary(changes: number, patch: string): boolean;
|
|
4
4
|
export declare function summarizeDiff(path: string, patch: string, provider: AiProvider, usage?: (response: AiResponse) => Promise<void>): Promise<string>;
|
|
5
|
+
/** Default for `reviewReadDiffMaxChars`: the most characters one
|
|
6
|
+
* `read-full-diff` answer returns. Every tool result is sent again in each
|
|
7
|
+
* later round of the tool loop, so one big read would otherwise be paid for
|
|
8
|
+
* many times. */
|
|
9
|
+
export declare const DEFAULT_READ_DIFF_MAX_CHARS = 20000;
|
|
10
|
+
/** The repository's cap on one `read-full-diff` answer, or `null` for no
|
|
11
|
+
* limit. Unset takes the default, and 0 or a negative number means no limit,
|
|
12
|
+
* the same rule as `reviewSystemPromptMaxChars`. */
|
|
13
|
+
export declare function readDiffMaxChars(repoConfig: {
|
|
14
|
+
reviewReadDiffMaxChars?: number;
|
|
15
|
+
} | undefined): number | null;
|
|
5
16
|
export declare const READ_FULL_DIFF_TOOL: Json;
|
|
6
|
-
|
|
17
|
+
/** What `read-full-diff` knows about one changed file. */
|
|
18
|
+
export type ReadableDiff = {
|
|
19
|
+
state: "full" | "partial" | "summarized" | "dropped" | "withheld";
|
|
20
|
+
patch: string;
|
|
21
|
+
};
|
|
22
|
+
export declare function readFullDiff(files: Map<string, ReadableDiff>, args: unknown, maxChars?: number | null): string;
|
|
@@ -19,11 +19,27 @@ ${patch}`,
|
|
|
19
19
|
await usage(response);
|
|
20
20
|
return response.text.trim();
|
|
21
21
|
}
|
|
22
|
+
/** Default for `reviewReadDiffMaxChars`: the most characters one
|
|
23
|
+
* `read-full-diff` answer returns. Every tool result is sent again in each
|
|
24
|
+
* later round of the tool loop, so one big read would otherwise be paid for
|
|
25
|
+
* many times. */
|
|
26
|
+
export const DEFAULT_READ_DIFF_MAX_CHARS = 20_000;
|
|
27
|
+
/** The repository's cap on one `read-full-diff` answer, or `null` for no
|
|
28
|
+
* limit. Unset takes the default, and 0 or a negative number means no limit,
|
|
29
|
+
* the same rule as `reviewSystemPromptMaxChars`. */
|
|
30
|
+
export function readDiffMaxChars(repoConfig) {
|
|
31
|
+
const raw = repoConfig?.reviewReadDiffMaxChars;
|
|
32
|
+
if (raw === undefined)
|
|
33
|
+
return DEFAULT_READ_DIFF_MAX_CHARS;
|
|
34
|
+
if (!Number.isFinite(raw) || raw <= 0)
|
|
35
|
+
return null;
|
|
36
|
+
return Math.floor(raw);
|
|
37
|
+
}
|
|
22
38
|
export const READ_FULL_DIFF_TOOL = {
|
|
23
39
|
type: "function",
|
|
24
40
|
function: {
|
|
25
41
|
name: "read-full-diff",
|
|
26
|
-
description: "Read the
|
|
42
|
+
description: "Read the diff of one changed file in pages. Use it for a file whose diff is cut off, summarized, or listed under NOT SHOWN. Files shown in full are already in the prompt. offset and limit count diff lines from 0, and every line keeps its line number in the new file. A long answer is cut and ends with the offset to call again with.",
|
|
27
43
|
parameters: {
|
|
28
44
|
type: "object",
|
|
29
45
|
properties: {
|
|
@@ -31,22 +47,80 @@ export const READ_FULL_DIFF_TOOL = {
|
|
|
31
47
|
type: "string",
|
|
32
48
|
description: "The file path exactly as it appears in this prompt's DIFF or PULL REQUEST section.",
|
|
33
49
|
},
|
|
50
|
+
offset: {
|
|
51
|
+
type: "integer",
|
|
52
|
+
minimum: 0,
|
|
53
|
+
description: "The first diff line to return, counted from 0.",
|
|
54
|
+
},
|
|
55
|
+
limit: {
|
|
56
|
+
type: "integer",
|
|
57
|
+
minimum: 1,
|
|
58
|
+
description: "The most diff lines to return.",
|
|
59
|
+
},
|
|
34
60
|
},
|
|
35
61
|
required: ["path"],
|
|
36
62
|
additionalProperties: false,
|
|
37
63
|
},
|
|
38
64
|
},
|
|
39
65
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
66
|
+
function lineArg(value, fallback, least) {
|
|
67
|
+
if (value === undefined || value === null)
|
|
68
|
+
return fallback;
|
|
69
|
+
const number = Number(value);
|
|
70
|
+
if (!Number.isInteger(number) || number < least)
|
|
71
|
+
return Number.NaN;
|
|
72
|
+
return number;
|
|
73
|
+
}
|
|
74
|
+
// The patch is already in memory from the compare API or git, so this is a
|
|
75
|
+
// plain lookup, not a network or git call.
|
|
76
|
+
export function readFullDiff(files, args, maxChars = DEFAULT_READ_DIFF_MAX_CHARS) {
|
|
77
|
+
const input = (args ?? {});
|
|
78
|
+
const path = String(input.path ?? "");
|
|
79
|
+
const file = files.get(path);
|
|
80
|
+
if (!file) {
|
|
81
|
+
const readable = [...files]
|
|
82
|
+
.filter(([, item]) => item.state !== "full" && item.state !== "withheld")
|
|
83
|
+
.map(([name]) => name);
|
|
84
|
+
return readable.length === 0
|
|
85
|
+
? "Every file's diff is already shown in full above."
|
|
86
|
+
: `No changed file \`${path}\` can be read in this review. Files you can read: ${readable.join(", ")}`;
|
|
87
|
+
}
|
|
88
|
+
if (file.state === "full") {
|
|
89
|
+
return `The whole diff of \`${path}\` is already shown in the DIFF section.`;
|
|
90
|
+
}
|
|
91
|
+
if (file.state === "withheld" || file.patch === "") {
|
|
92
|
+
return `There is no diff text for \`${path}\` to read.`;
|
|
93
|
+
}
|
|
94
|
+
const lines = numberPatch(file.patch).split("\n");
|
|
95
|
+
const offset = lineArg(input.offset, 0, 0);
|
|
96
|
+
const limit = lineArg(input.limit, lines.length, 1);
|
|
97
|
+
if (Number.isNaN(offset) || Number.isNaN(limit)) {
|
|
98
|
+
return "offset must be a whole number of 0 or more, and limit a whole number of 1 or more.";
|
|
99
|
+
}
|
|
100
|
+
if (offset >= lines.length) {
|
|
101
|
+
return `\`${path}\` has ${lines.length} diff lines, so offset=${offset} is past the end.`;
|
|
102
|
+
}
|
|
103
|
+
const end = Math.min(lines.length, offset + limit);
|
|
104
|
+
const out = [];
|
|
105
|
+
let used = 0;
|
|
106
|
+
let next = offset;
|
|
107
|
+
while (next < end) {
|
|
108
|
+
const line = lines[next];
|
|
109
|
+
const add = line.length + (out.length > 0 ? 1 : 0);
|
|
110
|
+
if (maxChars !== null && used + add > maxChars) {
|
|
111
|
+
// A single line longer than the cap is cut rather than returned whole,
|
|
112
|
+
// so no answer goes past the cap. The marker says so, because the rest
|
|
113
|
+
// of that line cannot be read with any offset.
|
|
114
|
+
if (out.length === 0) {
|
|
115
|
+
out.push(`${line.slice(0, maxChars)}\n[line cut at ${maxChars} characters, the rest of this line cannot be read]`);
|
|
116
|
+
next++;
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
out.push(line);
|
|
121
|
+
used += add;
|
|
122
|
+
next++;
|
|
123
|
+
}
|
|
124
|
+
const more = next < lines.length ? `\n[more: call again with offset=${next}]` : "";
|
|
125
|
+
return `${out.join("\n")}${more}`;
|
|
52
126
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** Turns a review's changed files into the DIFF section the model reads, the
|
|
2
|
+
* `read-full-diff` tool, and the coverage the result reports. Pull request,
|
|
3
|
+
* local, remote and push review all call this one function, so the states a
|
|
4
|
+
* file can reach and the warnings they leave are the same everywhere. */
|
|
5
|
+
import type { ToolHandler } from "../ai/mermaid_loop.ts";
|
|
6
|
+
import { type Coverage, type CoverageWarning } from "../review/coverage.ts";
|
|
7
|
+
import type { AiProvider, AiResponse } from "../types.ts";
|
|
8
|
+
import { type DiffFile, type PlannedFile } from "./diff_plan.ts";
|
|
9
|
+
export type PreparedDiff = {
|
|
10
|
+
/** The joined file sections, in the order the files came in. */
|
|
11
|
+
sections: string;
|
|
12
|
+
/** The NO DIFF TEXT and NOT SHOWN lists, or `""` when no file is listed. */
|
|
13
|
+
notShown: string;
|
|
14
|
+
/** The whole DIFF section: the sections, then the lists. */
|
|
15
|
+
diff: string;
|
|
16
|
+
/** `read-full-diff`, present when any file can be read with it. */
|
|
17
|
+
tools: ToolHandler[];
|
|
18
|
+
coverage: Coverage;
|
|
19
|
+
warnings: CoverageWarning[];
|
|
20
|
+
/** The files the model received this round. Callers save them through
|
|
21
|
+
* `visiblePathsToSave`, which adds the files skipped as unchanged. */
|
|
22
|
+
seenPaths: string[];
|
|
23
|
+
planned: PlannedFile[];
|
|
24
|
+
};
|
|
25
|
+
export type PrepareDiffInput = {
|
|
26
|
+
files: DiffFile[];
|
|
27
|
+
/** Files left out because an earlier round saw the same patch. */
|
|
28
|
+
unchangedPaths?: string[];
|
|
29
|
+
/** Whether the next run of this mode sends the files this run dropped,
|
|
30
|
+
* see `coverageWarnings`. */
|
|
31
|
+
catchesUp: boolean;
|
|
32
|
+
/** Whether the review model can call tools. */
|
|
33
|
+
tools: boolean;
|
|
34
|
+
/** The most summary calls in flight at once, `--ai-concurrent`. */
|
|
35
|
+
concurrency: number;
|
|
36
|
+
/** The cap on one `read-full-diff` answer, `null` for none. */
|
|
37
|
+
readDiffMaxChars: number | null;
|
|
38
|
+
/** The low-model provider. Called only when a file is summarized, because
|
|
39
|
+
* building a real provider needs a key a fake-AI run never has. */
|
|
40
|
+
summaryProvider: () => AiProvider;
|
|
41
|
+
usage?: (response: AiResponse) => Promise<void>;
|
|
42
|
+
report: (message: string) => void;
|
|
43
|
+
};
|
|
44
|
+
/** `fn` over `items` with at most `limit` calls running at once, results in
|
|
45
|
+
* input order. */
|
|
46
|
+
export declare function mapWithLimit<T, R>(items: T[], limit: number, fn: (item: T) => Promise<R>): Promise<R[]>;
|
|
47
|
+
export declare function prepareReviewDiff(input: PrepareDiffInput): Promise<PreparedDiff>;
|