co-maintainer 0.6.12 → 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.
Files changed (159) hide show
  1. package/README.md +1 -1
  2. package/dist/package.json +1 -1
  3. package/dist/src/ai/anthropic.js +2 -0
  4. package/dist/src/ai/batch.d.ts +3 -1
  5. package/dist/src/ai/batch.js +23 -2
  6. package/dist/src/ai/mermaid_loop.js +1 -1
  7. package/dist/src/ai/oauth/sign-in-with-chatgpt.js +8 -10
  8. package/dist/src/ai/openai.js +2 -0
  9. package/dist/src/ai/openrouter.js +2 -0
  10. package/dist/src/cli/args.js +47 -0
  11. package/dist/src/cli/command_json.d.ts +20 -1
  12. package/dist/src/cli/command_json.js +4 -0
  13. package/dist/src/cli/commands/config.d.ts +1 -1
  14. package/dist/src/cli/commands/config.js +1 -0
  15. package/dist/src/cli/commands/registry.js +226 -1
  16. package/dist/src/cli/commands/review.js +50 -9
  17. package/dist/src/cli/commands/serve.js +5 -0
  18. package/dist/src/cli/commands/set.js +75 -17
  19. package/dist/src/cli/commands/view.js +46 -5
  20. package/dist/src/cli/commands/view_sources.d.ts +11 -0
  21. package/dist/src/cli/commands/view_sources.js +40 -0
  22. package/dist/src/cli/main.js +5 -0
  23. package/dist/src/cli/pr_carry.d.ts +25 -0
  24. package/dist/src/cli/pr_carry.js +128 -0
  25. package/dist/src/cli/prompt.js +26 -1
  26. package/dist/src/cli/review_args.d.ts +8 -0
  27. package/dist/src/cli/review_args.js +18 -0
  28. package/dist/src/cli/review_carry.d.ts +32 -0
  29. package/dist/src/cli/review_carry.js +60 -0
  30. package/dist/src/cli/review_github.d.ts +16 -2
  31. package/dist/src/cli/review_github.js +49 -12
  32. package/dist/src/cli/review_intent.d.ts +25 -0
  33. package/dist/src/cli/review_intent.js +87 -0
  34. package/dist/src/cli/review_result.d.ts +53 -2
  35. package/dist/src/cli/review_result.js +90 -23
  36. package/dist/src/config.d.ts +30 -0
  37. package/dist/src/config.js +20 -12
  38. package/dist/src/extensions/command.d.ts +1 -0
  39. package/dist/src/extensions/command.js +355 -0
  40. package/dist/src/extensions/contexts.d.ts +65 -0
  41. package/dist/src/extensions/contexts.js +134 -0
  42. package/dist/src/extensions/cost.d.ts +8 -0
  43. package/dist/src/extensions/cost.js +6 -0
  44. package/dist/src/extensions/local_tracking.d.ts +7 -0
  45. package/dist/src/extensions/local_tracking.js +29 -0
  46. package/dist/src/extensions/mock.d.ts +29 -0
  47. package/dist/src/extensions/mock.js +67 -0
  48. package/dist/src/extensions/process.d.ts +43 -0
  49. package/dist/src/extensions/process.js +244 -0
  50. package/dist/src/extensions/registry.d.ts +79 -0
  51. package/dist/src/extensions/registry.js +240 -0
  52. package/dist/src/extensions/render_actions.d.ts +6 -0
  53. package/dist/src/extensions/render_actions.js +50 -0
  54. package/dist/src/extensions/render_cli.d.ts +18 -0
  55. package/dist/src/extensions/render_cli.js +88 -0
  56. package/dist/src/extensions/render_json.d.ts +51 -0
  57. package/dist/src/extensions/render_json.js +144 -0
  58. package/dist/src/extensions/render_markdown.d.ts +22 -0
  59. package/dist/src/extensions/render_markdown.js +96 -0
  60. package/dist/src/extensions/render_review.d.ts +44 -0
  61. package/dist/src/extensions/render_review.js +167 -0
  62. package/dist/src/extensions/report.d.ts +57 -0
  63. package/dist/src/extensions/report.js +97 -0
  64. package/dist/src/extensions/review_result.d.ts +2 -0
  65. package/dist/src/extensions/review_result.js +14 -0
  66. package/dist/src/extensions/run_folder.d.ts +74 -0
  67. package/dist/src/extensions/run_folder.js +172 -0
  68. package/dist/src/extensions/runner.d.ts +66 -0
  69. package/dist/src/extensions/runner.js +251 -0
  70. package/dist/src/extensions/schema.d.ts +103 -0
  71. package/dist/src/extensions/schema.js +539 -0
  72. package/dist/src/extensions/session.d.ts +56 -0
  73. package/dist/src/extensions/session.js +173 -0
  74. package/dist/src/github/collect.d.ts +7 -2
  75. package/dist/src/github/collect.js +66 -6
  76. package/dist/src/github/gh.d.ts +7 -0
  77. package/dist/src/github/gh.js +24 -29
  78. package/dist/src/knowledge/guide.js +3 -2
  79. package/dist/src/knowledge/instruction_files.d.ts +45 -0
  80. package/dist/src/knowledge/instruction_files.js +91 -0
  81. package/dist/src/knowledge/instructions.d.ts +78 -0
  82. package/dist/src/knowledge/instructions.js +280 -0
  83. package/dist/src/knowledge/rule_changes.d.ts +39 -0
  84. package/dist/src/knowledge/rule_changes.js +90 -0
  85. package/dist/src/knowledge/rules.d.ts +44 -0
  86. package/dist/src/knowledge/rules.js +82 -0
  87. package/dist/src/knowledge/synthesis.d.ts +1 -0
  88. package/dist/src/knowledge/synthesis.js +12 -5
  89. package/dist/src/knowledge/types.d.ts +4 -0
  90. package/dist/src/local/carry_over_store.d.ts +10 -0
  91. package/dist/src/local/carry_over_store.js +17 -0
  92. package/dist/src/local/review_local.js +100 -62
  93. package/dist/src/local/review_lock.js +15 -4
  94. package/dist/src/pr/diff_plan.d.ts +110 -0
  95. package/dist/src/pr/diff_plan.js +429 -0
  96. package/dist/src/pr/diff_summary.d.ts +17 -1
  97. package/dist/src/pr/diff_summary.js +87 -13
  98. package/dist/src/pr/review_diff.d.ts +47 -0
  99. package/dist/src/pr/review_diff.js +112 -0
  100. package/dist/src/pr/reviewer.d.ts +44 -19
  101. package/dist/src/pr/reviewer.js +114 -142
  102. package/dist/src/remote/client.js +67 -8
  103. package/dist/src/remote/http.d.ts +12 -0
  104. package/dist/src/remote/http.js +31 -0
  105. package/dist/src/remote/push_client.js +14 -3
  106. package/dist/src/remote/server/guides.js +12 -1
  107. package/dist/src/remote/server/routes.js +4 -2
  108. package/dist/src/remote/server/sessions.js +4 -1
  109. package/dist/src/remote/validate.js +11 -0
  110. package/dist/src/review/carry_over.d.ts +6 -1
  111. package/dist/src/review/carry_over.js +10 -2
  112. package/dist/src/review/coverage.d.ts +57 -0
  113. package/dist/src/review/coverage.js +186 -0
  114. package/dist/src/review/diagram.d.ts +73 -0
  115. package/dist/src/review/diagram.js +309 -0
  116. package/dist/src/review/engine.d.ts +2 -6
  117. package/dist/src/review/engine.js +1 -1
  118. package/dist/src/review/guides.d.ts +6 -0
  119. package/dist/src/review/guides.js +8 -1
  120. package/dist/src/review/intent.d.ts +24 -0
  121. package/dist/src/review/intent.js +28 -0
  122. package/dist/src/review/not_initialized.d.ts +13 -0
  123. package/dist/src/review/not_initialized.js +18 -0
  124. package/dist/src/review/system_prompt.d.ts +6 -0
  125. package/dist/src/review/system_prompt.js +13 -0
  126. package/dist/src/review/visible_paths.d.ts +22 -0
  127. package/dist/src/review/visible_paths.js +41 -0
  128. package/dist/src/server/auth.js +8 -1
  129. package/dist/src/server/pages/extensions.d.ts +5 -0
  130. package/dist/src/server/pages/extensions.js +72 -0
  131. package/dist/src/server/pages/pr.js +3 -2
  132. package/dist/src/services/dashboard.d.ts +8 -0
  133. package/dist/src/services/dashboard.js +8 -0
  134. package/dist/src/services/push_review.js +14 -5
  135. package/dist/src/services/remote_review.js +81 -26
  136. package/dist/src/services/replies.js +15 -8
  137. package/dist/src/services/review.d.ts +9 -3
  138. package/dist/src/services/review.js +109 -28
  139. package/dist/src/services/rules_build.d.ts +46 -0
  140. package/dist/src/services/rules_build.js +155 -0
  141. package/dist/src/services/setup.d.ts +31 -0
  142. package/dist/src/services/setup.js +120 -25
  143. package/dist/src/services/watch_comments.js +2 -0
  144. package/dist/src/store/app_db.d.ts +3 -1
  145. package/dist/src/store/app_db.js +7 -2
  146. package/dist/src/store/cache_db.js +3 -0
  147. package/dist/src/store/extensions.d.ts +51 -0
  148. package/dist/src/store/extensions.js +132 -0
  149. package/dist/src/store/migrations.js +40 -0
  150. package/dist/src/types.d.ts +22 -1
  151. package/dist/src/util/atomic.d.ts +15 -1
  152. package/dist/src/util/atomic.js +59 -29
  153. package/dist/src/util/interrupt.d.ts +6 -0
  154. package/dist/src/util/interrupt.js +27 -0
  155. package/dist/src/util/runtime.d.ts +3 -1
  156. package/dist/src/util/runtime.js +2 -2
  157. package/dist/src/util/test_location_guard.d.ts +6 -0
  158. package/dist/src/util/test_location_guard.js +47 -0
  159. package/package.json +1 -1
@@ -0,0 +1,240 @@
1
+ /** Extension registrations: what `extension register` saves, how a value is
2
+ * checked, and which registrations apply to one review.
3
+ *
4
+ * Registrations live in `config.json` as an `extensions` array. The array
5
+ * order is the registration order, which breaks priority ties, so an update
6
+ * rewrites an entry in place instead of moving it to the end. */
7
+ import { readConfig, writeUserConfig } from "../config.js";
8
+ import { getEnv } from "../util/runtime.js";
9
+ /** The review kinds an extension can run in. `pr` is a pull request review,
10
+ * from the CLI or from `serve`. */
11
+ export const REVIEW_TYPES = ["local", "pr", "remote"];
12
+ /** The run-after target that means "the AI review itself". */
13
+ export const REVIEW_TARGET = "review";
14
+ export const MAX_PRIORITY = 3;
15
+ export const DEFAULT_TIMEOUT_SECONDS = 600;
16
+ export const DEFAULT_MAX_REQUESTED_RETRIES = 3;
17
+ const MAX_TIMEOUT_SECONDS = 6 * 3600;
18
+ const MAX_RETRIES = 10;
19
+ /** `CM_EXTENSIONS_DISABLED=1` turns extensions off hard. The hosted cloud sets
20
+ * it from outside the container, so nothing in the user's data volume can turn
21
+ * them back on. */
22
+ export function extensionsDisabled() {
23
+ return getEnv("CM_EXTENSIONS_DISABLED") === "1";
24
+ }
25
+ export const DISABLED_MESSAGE = "Extensions are turned off on this machine (CM_EXTENSIONS_DISABLED=1).";
26
+ const NAME = /^[a-z0-9][a-z0-9._-]{0,63}$/i;
27
+ export function nameProblem(name) {
28
+ if (!NAME.test(name)) {
29
+ return "--name must be 1 to 64 letters, digits, dots, dashes or underscores, starting with a letter or digit";
30
+ }
31
+ if (name.toLowerCase() === REVIEW_TARGET) {
32
+ return `--name cannot be "${REVIEW_TARGET}", it is the name of the AI review`;
33
+ }
34
+ return undefined;
35
+ }
36
+ const REPO_PART = /^(\*|[A-Za-z0-9_.-]+)$/;
37
+ /** Checks a `--repos` value. Each entry is `*` or `owner/repo` where either
38
+ * side may be `*`. */
39
+ export function reposProblem(value) {
40
+ const entries = value.split(",").map((entry) => entry.trim());
41
+ for (const entry of entries) {
42
+ if (entry === "*")
43
+ continue;
44
+ const parts = entry.split("/");
45
+ if (parts.length !== 2 ||
46
+ !REPO_PART.test(parts[0]) ||
47
+ !REPO_PART.test(parts[1])) {
48
+ return `--repos has "${entry}", which is not owner/repo, owner/*, */repo or *`;
49
+ }
50
+ }
51
+ return undefined;
52
+ }
53
+ /** Whether a registration's `repos` pattern covers `repo`. GitHub names are
54
+ * case-insensitive, so the match is too. */
55
+ export function matchesRepo(pattern, repo) {
56
+ const [owner = "", name = ""] = repo.toLowerCase().split("/");
57
+ return pattern.split(",").some((raw) => {
58
+ const entry = raw.trim().toLowerCase();
59
+ if (entry === "*")
60
+ return true;
61
+ const [wantOwner, wantName] = entry.split("/");
62
+ return ((wantOwner === "*" || wantOwner === owner) &&
63
+ (wantName === "*" || wantName === name));
64
+ });
65
+ }
66
+ export function parseReviewTypes(value) {
67
+ const types = value
68
+ .split(",")
69
+ .map((item) => item.trim())
70
+ .filter(Boolean);
71
+ if (types.length === 0)
72
+ return "--review-types needs at least one type";
73
+ for (const type of types) {
74
+ if (!REVIEW_TYPES.includes(type)) {
75
+ return `--review-types must be a list of: ${REVIEW_TYPES.join(", ")}`;
76
+ }
77
+ }
78
+ return [...new Set(types)];
79
+ }
80
+ /** `10m`, `30s`, `1h` or a plain number of seconds. */
81
+ export function parseTimeout(value) {
82
+ const match = value.trim().match(/^(\d+)(s|m|h)?$/);
83
+ const problem = "--timeout must look like 30s, 10m or 1h, at most 6h";
84
+ if (!match)
85
+ return problem;
86
+ const unit = match[2] ?? "s";
87
+ const seconds = Number(match[1]) * (unit === "h" ? 3600 : unit === "m" ? 60 : 1);
88
+ if (seconds < 1 || seconds > MAX_TIMEOUT_SECONDS)
89
+ return problem;
90
+ return seconds;
91
+ }
92
+ /** `600` reads back as `10m`. */
93
+ export function formatTimeout(seconds) {
94
+ if (seconds % 3600 === 0)
95
+ return `${seconds / 3600}h`;
96
+ if (seconds % 60 === 0)
97
+ return `${seconds / 60}m`;
98
+ return `${seconds}s`;
99
+ }
100
+ export function parseCount(value, flag, max) {
101
+ const number = Number(value);
102
+ if (value.trim() === "" || !Number.isInteger(number) || number < 0) {
103
+ return `${flag} must be a whole number from 0 to ${max}`;
104
+ }
105
+ if (number > max)
106
+ return `${flag} must be a whole number from 0 to ${max}`;
107
+ return number;
108
+ }
109
+ export const LIMITS = { maxRetries: MAX_RETRIES };
110
+ export function parseBoolean(value, flag) {
111
+ if (value === "true")
112
+ return true;
113
+ if (value === "false")
114
+ return false;
115
+ return `${flag} must be true or false`;
116
+ }
117
+ function whole(value, fallback, min, max) {
118
+ if (typeof value !== "number" || !Number.isFinite(value))
119
+ return fallback;
120
+ return Math.min(max, Math.max(min, Math.floor(value)));
121
+ }
122
+ /** `review`, in any case, is the AI review. */
123
+ export function normalRunAfter(value) {
124
+ if (typeof value !== "string" || value.trim() === "")
125
+ return undefined;
126
+ return value.toLowerCase() === REVIEW_TARGET ? REVIEW_TARGET : value;
127
+ }
128
+ /** Why an entry from config.json cannot run, or its normal form. A hand edit
129
+ * gets the same defaults and limits `register` applies, so a missing timeout
130
+ * or retry limit cannot turn into an instant timeout or an endless loop. */
131
+ export function normalizeRegistration(entry) {
132
+ if (typeof entry !== "object" || entry === null)
133
+ return "it is not an object";
134
+ const raw = entry;
135
+ if (typeof raw.name !== "string")
136
+ return "it has no name";
137
+ const problem = nameProblem(raw.name);
138
+ if (problem)
139
+ return problem.replace("--name", "its name");
140
+ if (typeof raw.command !== "string" || raw.command.trim() === "") {
141
+ return "it has no command";
142
+ }
143
+ const repos = typeof raw.repos === "string" ? raw.repos : "*";
144
+ const reposIssue = reposProblem(repos);
145
+ if (reposIssue)
146
+ return reposIssue.replace("--repos", "its repos");
147
+ const types = Array.isArray(raw.reviewTypes)
148
+ ? raw.reviewTypes.filter((type) => REVIEW_TYPES.includes(type))
149
+ : [];
150
+ const registration = {
151
+ name: raw.name,
152
+ command: raw.command,
153
+ repos,
154
+ ...(types.length > 0 ? { reviewTypes: [...new Set(types)] } : {}),
155
+ parallelism: raw.parallelism !== false,
156
+ priority: whole(raw.priority, 0, 0, MAX_PRIORITY),
157
+ failFast: raw.failFast === true,
158
+ retryOnFailTimes: whole(raw.retryOnFailTimes, 0, 0, MAX_RETRIES),
159
+ maxRequestedRetries: whole(raw.maxRequestedRetries, DEFAULT_MAX_REQUESTED_RETRIES, 0, MAX_RETRIES),
160
+ timeoutSeconds: typeof raw.timeoutSeconds === "number" &&
161
+ Number.isFinite(raw.timeoutSeconds)
162
+ ? Math.min(MAX_TIMEOUT_SECONDS, Math.max(1, raw.timeoutSeconds))
163
+ : DEFAULT_TIMEOUT_SECONDS,
164
+ };
165
+ const runAfter = normalRunAfter(raw.runAfter);
166
+ if (runAfter)
167
+ registration.runAfter = runAfter;
168
+ if (typeof raw.rerunOnRestart === "boolean") {
169
+ registration.rerunOnRestart = raw.rerunOnRestart;
170
+ }
171
+ if (raw.disabled === true)
172
+ registration.disabled = true;
173
+ return registration;
174
+ }
175
+ const warned = new Set();
176
+ /** The registrations in config.json, oldest first, in their normal form. An
177
+ * entry that cannot run is left out with a warning that names it, rather than
178
+ * failing every review. */
179
+ export function readRegistrations(config = readConfig(), warn = (message) => console.error(message)) {
180
+ const raw = config.extensions;
181
+ if (!Array.isArray(raw))
182
+ return [];
183
+ const registrations = [];
184
+ raw.forEach((entry, index) => {
185
+ const normal = normalizeRegistration(entry);
186
+ if (typeof normal !== "string") {
187
+ registrations.push(normal);
188
+ return;
189
+ }
190
+ const name = typeof entry?.name === "string"
191
+ ? JSON.stringify(entry.name)
192
+ : `number ${index + 1}`;
193
+ const message = `[extension] the entry ${name} in config.json is ignored: ${normal}.`;
194
+ if (!warned.has(message)) {
195
+ warned.add(message);
196
+ warn(message);
197
+ }
198
+ });
199
+ return registrations;
200
+ }
201
+ /** Writes the registrations. Entries `readRegistrations` ignored stay in the
202
+ * file as they were, so a command never deletes what someone wrote by hand. */
203
+ export async function writeRegistrations(registrations) {
204
+ const raw = readConfig().extensions;
205
+ const ignored = Array.isArray(raw)
206
+ ? raw.filter((entry) => typeof normalizeRegistration(entry) === "string")
207
+ : [];
208
+ const all = [...registrations, ...ignored];
209
+ await writeUserConfig({ extensions: all.length > 0 ? all : undefined });
210
+ }
211
+ /** The first cycle the run-after links form, as `a -> b -> a`, or undefined.
212
+ * `review` is the start point and never waits, so it cannot close a cycle. */
213
+ export function runAfterCycle(registrations) {
214
+ const next = new Map(registrations.map((entry) => [entry.name, entry.runAfter]));
215
+ for (const start of next.keys()) {
216
+ const seen = [start];
217
+ let current = next.get(start);
218
+ while (current && current !== REVIEW_TARGET && next.has(current)) {
219
+ if (current === start)
220
+ return [...seen, start].join(" -> ");
221
+ if (seen.includes(current))
222
+ break;
223
+ seen.push(current);
224
+ current = next.get(current);
225
+ }
226
+ }
227
+ return undefined;
228
+ }
229
+ /** Whether `rerunOnRestart` is on for one registration: its own value, then the
230
+ * global config key, then on. */
231
+ export function rerunsOnRestart(registration, config = readConfig()) {
232
+ return registration.rerunOnRestart ?? config.extensionRerunOnRestart ?? true;
233
+ }
234
+ /** The registrations that run for one review: enabled, matching the repository
235
+ * and the review type, in registration order. */
236
+ export function applicableRegistrations(registrations, repo, type) {
237
+ return registrations.filter((entry) => !entry.disabled &&
238
+ matchesRepo(entry.repos ?? "*", repo) &&
239
+ (!entry.reviewTypes || entry.reviewTypes.includes(type)));
240
+ }
@@ -0,0 +1,6 @@
1
+ import type { HumanFinding } from "../cli/review_result.ts";
2
+ import { type ExtensionReport } from "./report.ts";
3
+ export declare function extensionHumanFindings(report: ExtensionReport | undefined): HumanFinding[];
4
+ /** The job summary with the extensions' part added after the AI part, cut to
5
+ * the 1 MiB limit by dropping `markdown`, longest first. */
6
+ export declare function jobSummaryWithExtensions(aiSummary: string, report: ExtensionReport | undefined): string;
@@ -0,0 +1,50 @@
1
+ /** The extensions in `review --output=github`.
2
+ *
3
+ * A finding with a location becomes an annotation with the extension label,
4
+ * inside the same 10 per level GitHub shows. The job summary lists every
5
+ * finding and carries each extension's `markdown`. The 250k cap of a GitHub
6
+ * review does not apply: there is no dashboard in this mode, so a `markdown`
7
+ * dropped at that size would be lost. The job summary's own 1 MiB limit does,
8
+ * and the longest `markdown` goes first. */
9
+ import { stripControlChars } from "../util/sanitize.js";
10
+ import { effectiveBlocking, extensionLabel, } from "./report.js";
11
+ import { extensionsSection, fitMarkdowns, JOB_SUMMARY_LIMIT_NOTE, JOB_SUMMARY_MAX_BYTES, utf8Bytes, } from "./render_markdown.js";
12
+ export function extensionHumanFindings(report) {
13
+ if (!report)
14
+ return [];
15
+ return report.findings.map((row) => ({
16
+ state: row.state,
17
+ severity: row.severity,
18
+ blocking: effectiveBlocking(report, row),
19
+ path: row.path,
20
+ lineFrom: row.lineFrom,
21
+ lineTo: row.lineTo,
22
+ title: row.title,
23
+ body: row.body,
24
+ suggestion: null,
25
+ label: extensionLabel(report, row),
26
+ }));
27
+ }
28
+ /** The lines naming each extension that did not return `ok`, with its reason.
29
+ * In the CLI the reason is shown directly. */
30
+ function outcomeLines(report) {
31
+ return report.results
32
+ .filter((result) => result.status !== "ok")
33
+ .map((result) => `- ${stripControlChars(result.name)} ${result.status}: ${stripControlChars(result.reason ?? "").replace(/\s+/g, " ")}`);
34
+ }
35
+ /** The job summary with the extensions' part added after the AI part, cut to
36
+ * the 1 MiB limit by dropping `markdown`, longest first. */
37
+ export function jobSummaryWithExtensions(aiSummary, report) {
38
+ if (!report || report.results.length === 0)
39
+ return aiSummary;
40
+ const outcomes = outcomeLines(report);
41
+ return fitMarkdowns(report, (markdowns) => [
42
+ aiSummary,
43
+ ...(outcomes.length > 0 ? ["", ...outcomes] : []),
44
+ ...(markdowns.size > 0
45
+ ? ["", extensionsSection(report, markdowns)]
46
+ : []),
47
+ ].join("\n"),
48
+ // The summary file is appended with a newline.
49
+ JOB_SUMMARY_MAX_BYTES - 1, utf8Bytes, JOB_SUMMARY_LIMIT_NOTE);
50
+ }
@@ -0,0 +1,18 @@
1
+ import { type ExtensionReport } from "./report.ts";
2
+ export declare function locationOf(finding: {
3
+ path: string | null;
4
+ lineFrom: number | null;
5
+ lineTo: number | null;
6
+ }): string;
7
+ /** The `Extensions` block, or nothing when no extension ran. */
8
+ export declare function cliExtensionLines(report: ExtensionReport): string[];
9
+ /** The extensions' share of the summary line's counts. */
10
+ export declare function extensionCounts(report: ExtensionReport): {
11
+ new: number;
12
+ open: number;
13
+ closed: number;
14
+ blocking: number;
15
+ };
16
+ /** `Extensions: qa-vision · Failed: none`, with the skipped ones when there
17
+ * are any. */
18
+ export declare function cliExtensionFooter(report: ExtensionReport): string;
@@ -0,0 +1,88 @@
1
+ /** The extensions in the terminal. Built from `findings` and `data`, never
2
+ * from `markdown`, which is written for GitHub.
3
+ *
4
+ * Extensions
5
+ * qa-vision · request_changes
6
+ * • [P2 · blocking] Snapshot button missing
7
+ * video main: https://...
8
+ * video pr: https://...
9
+ *
10
+ * Extensions: qa-vision · Failed: none
11
+ *
12
+ * Everything an extension wrote is untrusted text, so control characters are
13
+ * stripped before it reaches the terminal. */
14
+ import { stripControlChars } from "../util/sanitize.js";
15
+ import { effectiveBlocking, namesWith, } from "./report.js";
16
+ function clean(text) {
17
+ return stripControlChars(text.replace(/\s*\n\s*/g, " ")).trim();
18
+ }
19
+ export function locationOf(finding) {
20
+ if (!finding.path)
21
+ return "";
22
+ if (!finding.lineFrom)
23
+ return finding.path;
24
+ const to = finding.lineTo ?? finding.lineFrom;
25
+ return to === finding.lineFrom
26
+ ? `${finding.path}:${finding.lineFrom}`
27
+ : `${finding.path}:${finding.lineFrom}-${to}`;
28
+ }
29
+ function findingLine(report, row) {
30
+ const label = `[${row.severity} · ${effectiveBlocking(report, row) ? "blocking" : "non-blocking"}]`;
31
+ const where = locationOf(row);
32
+ const bullet = row.state === "closed" ? "✓" : "•";
33
+ const state = row.state === "closed"
34
+ ? " (closed)"
35
+ : row.state === "open"
36
+ ? " (still open)"
37
+ : "";
38
+ return ` ${bullet} ${where ? `${clean(where)} ` : ""}${label} ${clean(row.title)}${state}`;
39
+ }
40
+ /** The `Extensions` block, or nothing when no extension ran. */
41
+ export function cliExtensionLines(report) {
42
+ if (report.results.length === 0)
43
+ return [];
44
+ const lines = ["Extensions"];
45
+ for (const result of report.results) {
46
+ const outcome = result.status === "ok"
47
+ ? (result.verdict ?? "ok")
48
+ : `${result.status}: ${clean(result.reason ?? "")}`;
49
+ lines.push(` ${clean(result.name)} · ${outcome}`);
50
+ for (const row of report.findings.filter((finding) => finding.extensionId === result.name)) {
51
+ lines.push(findingLine(report, row));
52
+ }
53
+ const media = result.data.media;
54
+ const width = Math.max(0, ...media.map((item) => clean(item.label).length));
55
+ for (const item of media) {
56
+ lines.push(` ${clean(item.kind)} ${`${clean(item.label)}:`.padEnd(width + 1)} ${clean(item.url)}`);
57
+ }
58
+ for (const metric of result.data.metrics) {
59
+ lines.push(` ${clean(metric.name)}: ${metric.value}${metric.unit ? ` ${clean(metric.unit)}` : ""}`);
60
+ }
61
+ }
62
+ return lines;
63
+ }
64
+ /** The extensions' share of the summary line's counts. */
65
+ export function extensionCounts(report) {
66
+ const rows = report.findings;
67
+ return {
68
+ new: rows.filter((row) => row.state === "new").length,
69
+ open: rows.filter((row) => row.state === "open").length,
70
+ closed: rows.filter((row) => row.state === "closed").length,
71
+ blocking: rows.filter((row) => effectiveBlocking(report, row)).length,
72
+ };
73
+ }
74
+ function list(names) {
75
+ return names.length === 0 ? "none" : names.map(clean).join(", ");
76
+ }
77
+ /** `Extensions: qa-vision · Failed: none`, with the skipped ones when there
78
+ * are any. */
79
+ export function cliExtensionFooter(report) {
80
+ if (report.results.length === 0)
81
+ return "";
82
+ const skipped = namesWith(report, "skipped");
83
+ return [
84
+ `Extensions: ${list(namesWith(report, "ok"))}`,
85
+ `Failed: ${list(namesWith(report, "failed"))}`,
86
+ ...(skipped.length > 0 ? [`Skipped: ${list(skipped)}`] : []),
87
+ ].join(" · ");
88
+ }
@@ -0,0 +1,51 @@
1
+ /** The extensions in `review --json`, in the remote result, and in the cost
2
+ * line.
3
+ *
4
+ * `extensions[]` holds each result. Extension findings join the top-level
5
+ * `findings[]` with `extensionId`, and AI findings carry `extensionId: null`,
6
+ * so a reader tells them apart without a second list. `markdown` is empty
7
+ * unless `--include-extension-markdowns` asks for it: it is written for GitHub
8
+ * and can be large. */
9
+ import { type ExtensionReport } from "./report.ts";
10
+ import type { RunSummary } from "../util/run_summary.ts";
11
+ type Counts = {
12
+ new: number;
13
+ open: number;
14
+ closed: number;
15
+ blocking: number;
16
+ };
17
+ export declare function jsonExtensionResults(report: ExtensionReport, includeMarkdown: boolean): unknown[];
18
+ export declare function jsonExtensionFindings(report: ExtensionReport): unknown[];
19
+ /** `usage` with the extensions' cost joined in. `costUsd` is the total,
20
+ * `reviewCostUsd` the AI review alone. An unknown review cost keeps the total
21
+ * unknown. Unchanged when no extension ran. */
22
+ export declare function usageWithExtensions<T extends {
23
+ costUsd: number | null;
24
+ } | undefined>(usage: T, report?: ExtensionReport): T;
25
+ /** The terminal's cost line with the extensions' cost in the total. */
26
+ export declare function runSummaryWithExtensions(summary: RunSummary, report?: ExtensionReport): RunSummary;
27
+ /** Adds the extensions to a review JSON document: their findings, their
28
+ * counts in `summary`, their results and their cost. */
29
+ export declare function withExtensionsJson<T extends {
30
+ findings: object[];
31
+ summary: Counts;
32
+ usage?: {
33
+ costUsd: number | null;
34
+ };
35
+ }>(doc: T, report?: ExtensionReport, includeMarkdown?: boolean): Omit<T, "findings"> & {
36
+ findings: object[];
37
+ extensions: unknown[];
38
+ };
39
+ /** A remote result arrives with every `markdown`. The client empties them
40
+ * unless it was asked for them, the same as a local `--json`. */
41
+ export declare function stripExtensionMarkdowns(result: Record<string, unknown>, includeMarkdown: boolean): Record<string, unknown>;
42
+ /** A remote result's extensions back in the shape the outputs read, and its
43
+ * AI findings on their own. A server older than extensions sends neither
44
+ * field, which reads as no extension. */
45
+ export declare function splitRemoteResult(result: Record<string, unknown>): {
46
+ aiFindings: Record<string, unknown>[];
47
+ report: ExtensionReport;
48
+ };
49
+ /** Whether a remote result's extensions make the review blocking. */
50
+ export declare function jsonExtensionsBlock(result: Record<string, unknown>): boolean;
51
+ export {};
@@ -0,0 +1,144 @@
1
+ /** The extensions in `review --json`, in the remote result, and in the cost
2
+ * line.
3
+ *
4
+ * `extensions[]` holds each result. Extension findings join the top-level
5
+ * `findings[]` with `extensionId`, and AI findings carry `extensionId: null`,
6
+ * so a reader tells them apart without a second list. `markdown` is empty
7
+ * unless `--include-extension-markdowns` asks for it: it is written for GitHub
8
+ * and can be large. */
9
+ import { effectiveBlocking, EMPTY_REPORT, extensionsCostUsd, hasExtensions, } from "./report.js";
10
+ import { extensionCounts } from "./render_cli.js";
11
+ export function jsonExtensionResults(report, includeMarkdown) {
12
+ return report.results.map((result) => ({
13
+ id: result.name,
14
+ status: result.status,
15
+ verdict: result.verdict,
16
+ reason: result.reason,
17
+ markdown: includeMarkdown ? result.markdown : "",
18
+ data: result.data,
19
+ extraData: result.extraData,
20
+ usage: result.usage,
21
+ durationMs: result.durationMs,
22
+ }));
23
+ }
24
+ export function jsonExtensionFindings(report) {
25
+ return report.findings.map((row) => ({
26
+ id: row.id,
27
+ state: row.state,
28
+ closeReason: null,
29
+ severity: row.severity,
30
+ blocking: effectiveBlocking(report, row),
31
+ path: row.path,
32
+ lineFrom: row.lineFrom,
33
+ lineTo: row.lineTo,
34
+ title: row.title,
35
+ body: row.body,
36
+ suggestion: null,
37
+ extensionId: row.extensionId,
38
+ key: row.key,
39
+ }));
40
+ }
41
+ /** `usage` with the extensions' cost joined in. `costUsd` is the total,
42
+ * `reviewCostUsd` the AI review alone. An unknown review cost keeps the total
43
+ * unknown. Unchanged when no extension ran. */
44
+ export function usageWithExtensions(usage, report = EMPTY_REPORT) {
45
+ if (!usage || !hasExtensions(report))
46
+ return usage;
47
+ const extensions = extensionsCostUsd(report);
48
+ return {
49
+ ...usage,
50
+ costUsd: usage.costUsd === null ? null : usage.costUsd + extensions,
51
+ reviewCostUsd: usage.costUsd,
52
+ extensionsCostUsd: extensions,
53
+ };
54
+ }
55
+ /** The terminal's cost line with the extensions' cost in the total. */
56
+ export function runSummaryWithExtensions(summary, report = EMPTY_REPORT) {
57
+ if (!hasExtensions(report) || summary.costUsd === null)
58
+ return summary;
59
+ return { ...summary, costUsd: summary.costUsd + extensionsCostUsd(report) };
60
+ }
61
+ /** Adds the extensions to a review JSON document: their findings, their
62
+ * counts in `summary`, their results and their cost. */
63
+ export function withExtensionsJson(doc, report = EMPTY_REPORT, includeMarkdown = false) {
64
+ const extra = extensionCounts(report);
65
+ return {
66
+ ...doc,
67
+ summary: {
68
+ new: doc.summary.new + extra.new,
69
+ open: doc.summary.open + extra.open,
70
+ closed: doc.summary.closed + extra.closed,
71
+ blocking: doc.summary.blocking + extra.blocking,
72
+ },
73
+ findings: [
74
+ ...doc.findings.map((row) => ({ ...row, extensionId: null })),
75
+ ...jsonExtensionFindings(report),
76
+ ],
77
+ extensions: jsonExtensionResults(report, includeMarkdown),
78
+ ...(doc.usage ? { usage: usageWithExtensions(doc.usage, report) } : {}),
79
+ };
80
+ }
81
+ /** A remote result arrives with every `markdown`. The client empties them
82
+ * unless it was asked for them, the same as a local `--json`. */
83
+ export function stripExtensionMarkdowns(result, includeMarkdown) {
84
+ if (includeMarkdown || !Array.isArray(result.extensions))
85
+ return result;
86
+ return {
87
+ ...result,
88
+ extensions: result.extensions.map((entry) => ({ ...entry, markdown: "" })),
89
+ };
90
+ }
91
+ /** A remote result's extensions back in the shape the outputs read, and its
92
+ * AI findings on their own. A server older than extensions sends neither
93
+ * field, which reads as no extension. */
94
+ export function splitRemoteResult(result) {
95
+ const findings = Array.isArray(result.findings)
96
+ ? result.findings
97
+ : [];
98
+ const entries = Array.isArray(result.extensions)
99
+ ? result.extensions
100
+ : [];
101
+ return {
102
+ aiFindings: findings.filter((row) => !row.extensionId),
103
+ report: {
104
+ results: entries.map((entry) => ({
105
+ name: String(entry.id ?? ""),
106
+ status: entry.status ?? "failed",
107
+ verdict: entry.verdict ?? null,
108
+ reason: entry.reason ?? null,
109
+ markdown: typeof entry.markdown === "string" ? entry.markdown : "",
110
+ findings: [],
111
+ data: entry.data ?? {
112
+ media: [],
113
+ metrics: [],
114
+ },
115
+ extraData: entry.extraData ?? null,
116
+ usage: entry.usage ?? null,
117
+ details: null,
118
+ durationMs: Number(entry.durationMs ?? 0),
119
+ attempts: 0,
120
+ })),
121
+ findings: findings
122
+ .filter((row) => row.extensionId)
123
+ .map((row) => ({
124
+ id: String(row.id ?? ""),
125
+ extensionId: String(row.extensionId),
126
+ key: row.key ?? null,
127
+ state: row.state ?? "new",
128
+ severity: row.severity ?? "P2",
129
+ blocking: row.blocking === true,
130
+ title: String(row.title ?? ""),
131
+ body: String(row.body ?? ""),
132
+ path: row.path ?? null,
133
+ lineFrom: row.lineFrom ?? null,
134
+ lineTo: row.lineTo ?? null,
135
+ })),
136
+ },
137
+ };
138
+ }
139
+ /** Whether a remote result's extensions make the review blocking. */
140
+ export function jsonExtensionsBlock(result) {
141
+ if (!Array.isArray(result.extensions))
142
+ return false;
143
+ return result.extensions.some((entry) => entry.status === "ok" && entry.verdict === "request_changes");
144
+ }
@@ -0,0 +1,22 @@
1
+ import type { ExtensionReport } from "./report.ts";
2
+ /** GitHub accepted a 250,000 character review body. */
3
+ export declare const REVIEW_BODY_MAX_CHARS = 250000;
4
+ /** The job summary limit reported for one step. Measured in bytes. */
5
+ export declare const JOB_SUMMARY_MAX_BYTES: number;
6
+ export declare const REVIEW_LIMIT_NOTE = "Not added to this comment because of the length limit, see the dashboard.";
7
+ export declare const JOB_SUMMARY_LIMIT_NOTE = "Not added to the job summary because of the length limit.";
8
+ export declare function utf8Bytes(text: string): number;
9
+ /** Closes a fence left open, so it cannot swallow what follows it. */
10
+ export declare function closeFences(text: string): string;
11
+ /** An extension's `markdown` made unable to leave the `<details>` block
12
+ * co-maintainer puts it in, or to forge a co-maintainer section. What it
13
+ * prints is shaped by a stranger's pull request. Mentions and images stay:
14
+ * they are the extension owner's responsibility. */
15
+ export declare function containMarkdown(markdown: string): string;
16
+ /** The `#### Extensions` section from each extension's `markdown`, or "" when
17
+ * no extension sent any. `markdowns` holds what is shown, which the length
18
+ * limit may have replaced. */
19
+ export declare function extensionsSection(report: ExtensionReport, markdowns: Map<string, string>): string;
20
+ /** Renders with every extension's `markdown`, then replaces the longest one
21
+ * with `note` until `measure` fits `limit`. */
22
+ export declare function fitMarkdowns(report: ExtensionReport, render: (markdowns: Map<string, string>) => string, limit: number, measure: (text: string) => number, note: string): string;