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
package/dist/src/config.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { writeTextFileAtomic } from "./util/atomic.js";
|
|
2
|
+
import { guardTestLocation } from "./util/test_location_guard.js";
|
|
3
|
+
import { currentPlatform, getEnv, isNotFound, readTextFileSync, setEnv, } from "./util/runtime.js";
|
|
2
4
|
/** The provider's own key variable. It is read only when that provider runs,
|
|
3
5
|
* so a key meant for one provider is never sent to another. */
|
|
4
6
|
export function providerKeyEnv(ai, env = getEnv) {
|
|
@@ -53,9 +55,15 @@ function homeDir(env) {
|
|
|
53
55
|
throw new Error("Could not determine the home directory");
|
|
54
56
|
return home;
|
|
55
57
|
}
|
|
58
|
+
/** The real environment under another name, so the test location guard can
|
|
59
|
+
* resolve every platform's directory without checking itself again. */
|
|
60
|
+
const readEnv = (name) => getEnv(name);
|
|
56
61
|
/** Platform/env injection keeps the OS branches testable: the
|
|
57
62
|
* migration's `"windows"` vs `"win32"` trap is invisible otherwise. */
|
|
58
63
|
export function getConfigDir(os = currentPlatform(), env = getEnv) {
|
|
64
|
+
if (env === getEnv) {
|
|
65
|
+
guardTestLocation("config", (other) => getConfigDir(other, readEnv));
|
|
66
|
+
}
|
|
59
67
|
const home = homeDir(env);
|
|
60
68
|
switch (os) {
|
|
61
69
|
case "windows":
|
|
@@ -67,6 +75,9 @@ export function getConfigDir(os = currentPlatform(), env = getEnv) {
|
|
|
67
75
|
}
|
|
68
76
|
}
|
|
69
77
|
export function getCacheDir(os = currentPlatform(), env = getEnv) {
|
|
78
|
+
if (env === getEnv) {
|
|
79
|
+
guardTestLocation("cache", (other) => getCacheDir(other, readEnv));
|
|
80
|
+
}
|
|
70
81
|
const home = homeDir(env);
|
|
71
82
|
switch (os) {
|
|
72
83
|
case "windows":
|
|
@@ -152,6 +163,9 @@ export function loadEnvFile(path) {
|
|
|
152
163
|
setEnv(match[1], value);
|
|
153
164
|
}
|
|
154
165
|
}
|
|
166
|
+
/** No retry on a parse error: every write replaces the file whole, so a file
|
|
167
|
+
* that does not parse is broken, not half written, and reading it again would
|
|
168
|
+
* give the same answer. */
|
|
155
169
|
export function readConfig() {
|
|
156
170
|
try {
|
|
157
171
|
return JSON.parse(readTextFileSync(configPath()));
|
|
@@ -193,18 +207,12 @@ export function appPrivateKeyFileMissing(config) {
|
|
|
193
207
|
return true;
|
|
194
208
|
}
|
|
195
209
|
}
|
|
210
|
+
/** `serve` reads config.json on every job while `set` can write it from
|
|
211
|
+
* another process, so the file is replaced whole and a reader never sees half
|
|
212
|
+
* of it. config.json can hold an API token (see UserConfig.token), so it stays
|
|
213
|
+
* readable only by the current user where the platform supports it. */
|
|
196
214
|
async function writeConfig(config) {
|
|
197
|
-
await
|
|
198
|
-
const path = configPath();
|
|
199
|
-
await writeTextFile(path, `${JSON.stringify(config, null, 2)}\n`);
|
|
200
|
-
// config.json can hold an API token (see UserConfig.token) — keep it
|
|
201
|
-
// readable only by the current user where the platform supports it.
|
|
202
|
-
try {
|
|
203
|
-
await chmod(path, 0o600);
|
|
204
|
-
}
|
|
205
|
-
catch {
|
|
206
|
-
// Windows has no POSIX chmod; NotSupported there is expected.
|
|
207
|
-
}
|
|
215
|
+
await writeTextFileAtomic(configPath(), `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 });
|
|
208
216
|
}
|
|
209
217
|
/** Merges `patch` into `repos[repo]` in config.json, dropping undefined
|
|
210
218
|
* fields. Never call this with a token or other secret. */
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runExtensionCommand(args: string[]): Promise<void>;
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
/** `co-maintainer extension register | update | list | remove | disable |
|
|
2
|
+
* enable | debug-run`.
|
|
3
|
+
*
|
|
4
|
+
* Management is CLI only. Every write goes through `config.json`, so `serve`
|
|
5
|
+
* picks a change up on its next review without a restart. */
|
|
6
|
+
import { CliError, die, EXIT_RUNTIME, exitWith } from "../cli/error.js";
|
|
7
|
+
import { askConfirm } from "../cli/prompt.js";
|
|
8
|
+
import { canPrompt } from "../tools/codegraph.js";
|
|
9
|
+
import { renderCommandHelp } from "../cli/commands/registry.js";
|
|
10
|
+
import { configPath } from "../config.js";
|
|
11
|
+
import { DEFAULT_MAX_REQUESTED_RETRIES, DEFAULT_TIMEOUT_SECONDS, DISABLED_MESSAGE, extensionsDisabled, formatTimeout, LIMITS, MAX_PRIORITY, nameProblem, normalRunAfter, parseBoolean, parseCount, parseReviewTypes, parseTimeout, readRegistrations, reposProblem, rerunsOnRestart, REVIEW_TARGET, runAfterCycle, writeRegistrations, } from "./registry.js";
|
|
12
|
+
import { substitute, unknownVariables } from "./process.js";
|
|
13
|
+
import { dependencyOf, runAttempts } from "./runner.js";
|
|
14
|
+
import { openRunFolder, removeRunFolder, writeReviewResult, } from "./run_folder.js";
|
|
15
|
+
import { MOCK_DETAILS, MOCK_DIFF, MOCK_REVIEW_RESULT } from "./mock.js";
|
|
16
|
+
import { buildReport } from "./report.js";
|
|
17
|
+
import { cliExtensionFooter, cliExtensionLines } from "./render_cli.js";
|
|
18
|
+
import { stripControlChars } from "../util/sanitize.js";
|
|
19
|
+
const SUBCOMMANDS = [
|
|
20
|
+
"register",
|
|
21
|
+
"update",
|
|
22
|
+
"list",
|
|
23
|
+
"remove",
|
|
24
|
+
"disable",
|
|
25
|
+
"enable",
|
|
26
|
+
"debug-run",
|
|
27
|
+
];
|
|
28
|
+
/** Flags each subcommand accepts, without the leading dashes. */
|
|
29
|
+
const SETTING_FLAGS = [
|
|
30
|
+
"command",
|
|
31
|
+
"repos",
|
|
32
|
+
"review-types",
|
|
33
|
+
"run-after",
|
|
34
|
+
"parallelism",
|
|
35
|
+
"priority",
|
|
36
|
+
"fail-fast",
|
|
37
|
+
"retry-on-fail-times",
|
|
38
|
+
"max-requested-retries",
|
|
39
|
+
"timeout",
|
|
40
|
+
"rerun-on-restart",
|
|
41
|
+
];
|
|
42
|
+
const ACCEPTED = {
|
|
43
|
+
register: ["name", ...SETTING_FLAGS, "yes"],
|
|
44
|
+
update: ["name", ...SETTING_FLAGS, "yes"],
|
|
45
|
+
list: ["json"],
|
|
46
|
+
remove: ["name"],
|
|
47
|
+
disable: ["name"],
|
|
48
|
+
enable: ["name"],
|
|
49
|
+
"debug-run": ["name", "command"],
|
|
50
|
+
};
|
|
51
|
+
function parseFlags(sub, args) {
|
|
52
|
+
const flags = new Map();
|
|
53
|
+
const accepted = ACCEPTED[sub] ?? [];
|
|
54
|
+
for (const arg of args) {
|
|
55
|
+
if (!arg.startsWith("--"))
|
|
56
|
+
die(`Unexpected argument: ${arg}`);
|
|
57
|
+
const eq = arg.indexOf("=");
|
|
58
|
+
const name = eq === -1 ? arg.slice(2) : arg.slice(2, eq);
|
|
59
|
+
if (!accepted.includes(name)) {
|
|
60
|
+
die(`Unknown option for extension ${sub}: --${name}`);
|
|
61
|
+
}
|
|
62
|
+
flags.set(name, eq === -1 ? true : arg.slice(eq + 1));
|
|
63
|
+
}
|
|
64
|
+
return flags;
|
|
65
|
+
}
|
|
66
|
+
function stringFlag(flags, name) {
|
|
67
|
+
const value = flags.get(name);
|
|
68
|
+
if (value === true)
|
|
69
|
+
die(`--${name} needs a value: --${name}=...`);
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
function requiredName(flags) {
|
|
73
|
+
const name = stringFlag(flags, "name");
|
|
74
|
+
if (!name)
|
|
75
|
+
die("--name is required");
|
|
76
|
+
return name;
|
|
77
|
+
}
|
|
78
|
+
/** A parsed value, or the problem a parser returned, which ends the command. */
|
|
79
|
+
function valid(value) {
|
|
80
|
+
if (typeof value === "string")
|
|
81
|
+
die(value);
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
/** Applies the setting flags on top of `base`. Only the flags given change
|
|
85
|
+
* anything, so `update` keeps every other value. */
|
|
86
|
+
function applySettings(base, flags) {
|
|
87
|
+
const next = { ...base };
|
|
88
|
+
const command = stringFlag(flags, "command");
|
|
89
|
+
if (command !== undefined) {
|
|
90
|
+
if (command.trim() === "")
|
|
91
|
+
die("--command cannot be empty");
|
|
92
|
+
const unknown = unknownVariables(command);
|
|
93
|
+
if (unknown.length > 0) {
|
|
94
|
+
die(`--command uses an unknown variable: {{${unknown[0]}}}. Known: {{details_path}}, {{review_result_path}}, {{diff_path}}`);
|
|
95
|
+
}
|
|
96
|
+
next.command = command;
|
|
97
|
+
}
|
|
98
|
+
const repos = stringFlag(flags, "repos");
|
|
99
|
+
if (repos !== undefined) {
|
|
100
|
+
const problem = reposProblem(repos);
|
|
101
|
+
if (problem)
|
|
102
|
+
die(problem);
|
|
103
|
+
next.repos = repos
|
|
104
|
+
.split(",")
|
|
105
|
+
.map((entry) => entry.trim())
|
|
106
|
+
.join(",");
|
|
107
|
+
}
|
|
108
|
+
const types = stringFlag(flags, "review-types");
|
|
109
|
+
if (types !== undefined) {
|
|
110
|
+
next.reviewTypes =
|
|
111
|
+
types === "all" ? undefined : valid(parseReviewTypes(types));
|
|
112
|
+
}
|
|
113
|
+
const runAfter = stringFlag(flags, "run-after");
|
|
114
|
+
if (runAfter !== undefined) {
|
|
115
|
+
next.runAfter = runAfter === "none" ? undefined : normalRunAfter(runAfter);
|
|
116
|
+
}
|
|
117
|
+
const parallelism = stringFlag(flags, "parallelism");
|
|
118
|
+
if (parallelism !== undefined) {
|
|
119
|
+
next.parallelism = valid(parseBoolean(parallelism, "--parallelism"));
|
|
120
|
+
}
|
|
121
|
+
const priority = stringFlag(flags, "priority");
|
|
122
|
+
if (priority !== undefined) {
|
|
123
|
+
next.priority = valid(parseCount(priority, "--priority", MAX_PRIORITY));
|
|
124
|
+
}
|
|
125
|
+
const failFast = flags.get("fail-fast");
|
|
126
|
+
if (failFast !== undefined) {
|
|
127
|
+
next.failFast =
|
|
128
|
+
failFast === true ? true : valid(parseBoolean(failFast, "--fail-fast"));
|
|
129
|
+
}
|
|
130
|
+
const retries = stringFlag(flags, "retry-on-fail-times");
|
|
131
|
+
if (retries !== undefined) {
|
|
132
|
+
next.retryOnFailTimes = valid(parseCount(retries, "--retry-on-fail-times", LIMITS.maxRetries));
|
|
133
|
+
}
|
|
134
|
+
const requested = stringFlag(flags, "max-requested-retries");
|
|
135
|
+
if (requested !== undefined) {
|
|
136
|
+
next.maxRequestedRetries = valid(parseCount(requested, "--max-requested-retries", LIMITS.maxRetries));
|
|
137
|
+
}
|
|
138
|
+
const timeout = stringFlag(flags, "timeout");
|
|
139
|
+
if (timeout !== undefined) {
|
|
140
|
+
next.timeoutSeconds = valid(parseTimeout(timeout));
|
|
141
|
+
}
|
|
142
|
+
const rerun = stringFlag(flags, "rerun-on-restart");
|
|
143
|
+
if (rerun !== undefined) {
|
|
144
|
+
next.rerunOnRestart = valid(parseBoolean(rerun, "--rerun-on-restart"));
|
|
145
|
+
}
|
|
146
|
+
return next;
|
|
147
|
+
}
|
|
148
|
+
/** The run-after target must not close a cycle, and a name nobody registered
|
|
149
|
+
* is most likely a typo, so it is asked about. */
|
|
150
|
+
async function checkRunAfter(registration, all, yes) {
|
|
151
|
+
const target = registration.runAfter;
|
|
152
|
+
if (target === registration.name) {
|
|
153
|
+
die("--run-after cannot name the extension itself");
|
|
154
|
+
}
|
|
155
|
+
const cycle = runAfterCycle(all);
|
|
156
|
+
if (cycle)
|
|
157
|
+
die(`--run-after would make a cycle: ${cycle}`);
|
|
158
|
+
if (!target ||
|
|
159
|
+
target === REVIEW_TARGET ||
|
|
160
|
+
all.some((entry) => entry.name === target)) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const question = `No extension named "${target}" is registered. Until one is, ${registration.name} is skipped. Are you sure?`;
|
|
164
|
+
if (yes)
|
|
165
|
+
return;
|
|
166
|
+
if (!canPrompt()) {
|
|
167
|
+
throw new CliError("usage", `No extension named "${target}" is registered.`, "Register it first, or pass --yes to save the run-after anyway.");
|
|
168
|
+
}
|
|
169
|
+
if (!(await askConfirm(question)))
|
|
170
|
+
die("Nothing was saved.");
|
|
171
|
+
}
|
|
172
|
+
const DEFAULTS = {
|
|
173
|
+
repos: "*",
|
|
174
|
+
parallelism: true,
|
|
175
|
+
priority: 0,
|
|
176
|
+
failFast: false,
|
|
177
|
+
retryOnFailTimes: 0,
|
|
178
|
+
maxRequestedRetries: DEFAULT_MAX_REQUESTED_RETRIES,
|
|
179
|
+
timeoutSeconds: DEFAULT_TIMEOUT_SECONDS,
|
|
180
|
+
};
|
|
181
|
+
async function register(flags) {
|
|
182
|
+
if (extensionsDisabled()) {
|
|
183
|
+
throw new CliError("extensions_disabled", DISABLED_MESSAGE);
|
|
184
|
+
}
|
|
185
|
+
const name = requiredName(flags);
|
|
186
|
+
const problem = nameProblem(name);
|
|
187
|
+
if (problem)
|
|
188
|
+
die(problem);
|
|
189
|
+
if (!flags.has("command"))
|
|
190
|
+
die("--command is required");
|
|
191
|
+
const all = readRegistrations();
|
|
192
|
+
if (all.some((entry) => entry.name === name)) {
|
|
193
|
+
die(`An extension named ${name} is already registered. Use extension update to change it.`);
|
|
194
|
+
}
|
|
195
|
+
const registration = applySettings({ name, command: "", ...DEFAULTS }, flags);
|
|
196
|
+
const next = [...all, registration];
|
|
197
|
+
await checkRunAfter(registration, next, flags.has("yes"));
|
|
198
|
+
await writeRegistrations(next);
|
|
199
|
+
console.log(`[extension] registered ${name}`);
|
|
200
|
+
console.log(`Saved to ${configPath()}`);
|
|
201
|
+
}
|
|
202
|
+
async function update(flags) {
|
|
203
|
+
const name = requiredName(flags);
|
|
204
|
+
const all = readRegistrations();
|
|
205
|
+
const index = all.findIndex((entry) => entry.name === name);
|
|
206
|
+
if (index === -1)
|
|
207
|
+
die(`No extension named ${name} is registered.`);
|
|
208
|
+
if (![...flags.keys()].some((flag) => SETTING_FLAGS.includes(flag))) {
|
|
209
|
+
die("Nothing to update. Pass at least one setting, such as --command=...");
|
|
210
|
+
}
|
|
211
|
+
const registration = applySettings(all[index], flags);
|
|
212
|
+
const next = all.map((entry, at) => (at === index ? registration : entry));
|
|
213
|
+
await checkRunAfter(registration, next, flags.has("yes"));
|
|
214
|
+
await writeRegistrations(next);
|
|
215
|
+
console.log(`[extension] updated ${name}`);
|
|
216
|
+
}
|
|
217
|
+
async function setDisabled(flags, disabled) {
|
|
218
|
+
const name = requiredName(flags);
|
|
219
|
+
const all = readRegistrations();
|
|
220
|
+
const index = all.findIndex((entry) => entry.name === name);
|
|
221
|
+
if (index === -1)
|
|
222
|
+
die(`No extension named ${name} is registered.`);
|
|
223
|
+
const next = all.map((entry, at) => {
|
|
224
|
+
if (at !== index)
|
|
225
|
+
return entry;
|
|
226
|
+
const { disabled: _old, ...rest } = entry;
|
|
227
|
+
return disabled ? { ...rest, disabled: true } : rest;
|
|
228
|
+
});
|
|
229
|
+
await writeRegistrations(next);
|
|
230
|
+
console.log(`[extension] ${disabled ? "disabled" : "enabled"} ${name}`);
|
|
231
|
+
}
|
|
232
|
+
async function removeOne(flags) {
|
|
233
|
+
const name = requiredName(flags);
|
|
234
|
+
const all = readRegistrations();
|
|
235
|
+
if (!all.some((entry) => entry.name === name)) {
|
|
236
|
+
die(`No extension named ${name} is registered.`);
|
|
237
|
+
}
|
|
238
|
+
await writeRegistrations(all.filter((entry) => entry.name !== name));
|
|
239
|
+
console.log(`[extension] removed ${name}`);
|
|
240
|
+
const waiting = all.filter((entry) => entry.runAfter === name);
|
|
241
|
+
for (const entry of waiting) {
|
|
242
|
+
console.log(`${entry.name} runs after ${name}, so it is skipped until that changes.`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function describe(entry) {
|
|
246
|
+
const after = dependencyOf(entry);
|
|
247
|
+
return [
|
|
248
|
+
`${entry.name}${entry.disabled ? " (disabled)" : ""}`,
|
|
249
|
+
` command: ${entry.command}`,
|
|
250
|
+
` repos: ${entry.repos ?? "*"} · review types: ${entry.reviewTypes?.join(",") ?? "all"}`,
|
|
251
|
+
` runs: ${after ? `after ${after}` : "with the review"} · priority ${entry.priority ?? 0}${entry.failFast ? " · fail fast" : ""}`,
|
|
252
|
+
` timeout ${formatTimeout(entry.timeoutSeconds)} · retries on a bad response ${entry.retryOnFailTimes} · requested retries honoured ${entry.maxRequestedRetries} · rerun on restart ${rerunsOnRestart(entry) ? "yes" : "no"}`,
|
|
253
|
+
];
|
|
254
|
+
}
|
|
255
|
+
function list(flags) {
|
|
256
|
+
const all = readRegistrations();
|
|
257
|
+
if (flags.has("json")) {
|
|
258
|
+
console.log(JSON.stringify({
|
|
259
|
+
schemaVersion: 1,
|
|
260
|
+
ok: true,
|
|
261
|
+
disabledByEnvironment: extensionsDisabled(),
|
|
262
|
+
extensions: all,
|
|
263
|
+
}));
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (extensionsDisabled())
|
|
267
|
+
console.log(DISABLED_MESSAGE);
|
|
268
|
+
if (all.length === 0) {
|
|
269
|
+
console.log("No extensions registered.");
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
console.log(all.flatMap(describe).join("\n"));
|
|
273
|
+
}
|
|
274
|
+
/** Runs one extension against the built-in mock review. No AI, no network,
|
|
275
|
+
* nothing posted. The command runs exactly as a review would run it, so an
|
|
276
|
+
* extension that starts before the review sees an empty review-results path
|
|
277
|
+
* here too. */
|
|
278
|
+
async function debugRun(flags) {
|
|
279
|
+
if (extensionsDisabled()) {
|
|
280
|
+
throw new CliError("extensions_disabled", DISABLED_MESSAGE);
|
|
281
|
+
}
|
|
282
|
+
const name = requiredName(flags);
|
|
283
|
+
const registered = readRegistrations().find((entry) => entry.name === name);
|
|
284
|
+
const command = stringFlag(flags, "command");
|
|
285
|
+
if (!registered && command === undefined) {
|
|
286
|
+
die(`No extension named ${name} is registered. Register it, or pass --command=... to try a command first.`);
|
|
287
|
+
}
|
|
288
|
+
const registration = applySettings(registered ?? { name, command: "", ...DEFAULTS }, command === undefined ? new Map() : new Map([["command", command]]));
|
|
289
|
+
const waitsForReview = dependencyOf(registration) !== undefined;
|
|
290
|
+
const folder = await openRunFolder({
|
|
291
|
+
runId: `debug-${crypto.randomUUID()}`,
|
|
292
|
+
owner: "cli",
|
|
293
|
+
details: MOCK_DETAILS,
|
|
294
|
+
diff: MOCK_DIFF,
|
|
295
|
+
});
|
|
296
|
+
try {
|
|
297
|
+
if (waitsForReview)
|
|
298
|
+
await writeReviewResult(folder, MOCK_REVIEW_RESULT);
|
|
299
|
+
console.log(`debug-run · ${name} · ${MOCK_DETAILS.repo} PR ${MOCK_DETAILS.prNumber} (built-in mock)`);
|
|
300
|
+
console.log(waitsForReview
|
|
301
|
+
? "Review results: given, this extension starts after the review."
|
|
302
|
+
: "Review results: empty, this extension starts with the review.");
|
|
303
|
+
const result = await runAttempts({
|
|
304
|
+
registration,
|
|
305
|
+
command: substitute(registration.command, {
|
|
306
|
+
details_path: folder.detailsPath,
|
|
307
|
+
review_result_path: waitsForReview ? folder.reviewResultPath : "",
|
|
308
|
+
diff_path: folder.diffPath,
|
|
309
|
+
}),
|
|
310
|
+
log: (message) => console.error(`[extension] ${message}`),
|
|
311
|
+
});
|
|
312
|
+
const report = buildReport([result]);
|
|
313
|
+
console.log("");
|
|
314
|
+
console.log(cliExtensionLines(report).join("\n"));
|
|
315
|
+
console.log("");
|
|
316
|
+
console.log(cliExtensionFooter(report));
|
|
317
|
+
if (result.usage?.costUsd !== undefined) {
|
|
318
|
+
console.log(`Cost: $${result.usage.costUsd.toFixed(4)}`);
|
|
319
|
+
}
|
|
320
|
+
if (result.status === "failed" && result.details) {
|
|
321
|
+
console.log("");
|
|
322
|
+
console.log("Details:");
|
|
323
|
+
console.log(stripControlChars(result.details));
|
|
324
|
+
}
|
|
325
|
+
exitWith(result.status === "failed" ? EXIT_RUNTIME : 0);
|
|
326
|
+
}
|
|
327
|
+
finally {
|
|
328
|
+
await removeRunFolder(folder);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
export async function runExtensionCommand(args) {
|
|
332
|
+
const [sub, ...rest] = args;
|
|
333
|
+
if (!sub || sub === "--help" || sub === "-h" || sub === "help") {
|
|
334
|
+
console.log(renderCommandHelp("extension"));
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
if (!SUBCOMMANDS.includes(sub)) {
|
|
338
|
+
die(`Unknown extension subcommand: ${sub}. Use one of: ${SUBCOMMANDS.join(", ")}`);
|
|
339
|
+
}
|
|
340
|
+
const flags = parseFlags(sub, rest);
|
|
341
|
+
if (sub === "register")
|
|
342
|
+
await register(flags);
|
|
343
|
+
else if (sub === "update")
|
|
344
|
+
await update(flags);
|
|
345
|
+
else if (sub === "list")
|
|
346
|
+
list(flags);
|
|
347
|
+
else if (sub === "remove")
|
|
348
|
+
await removeOne(flags);
|
|
349
|
+
else if (sub === "disable")
|
|
350
|
+
await setDisabled(flags, true);
|
|
351
|
+
else if (sub === "enable")
|
|
352
|
+
await setDisabled(flags, false);
|
|
353
|
+
else
|
|
354
|
+
await debugRun(flags);
|
|
355
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** The `details.json` context of each review type, built from what that
|
|
2
|
+
* review already has in hand. Keeping them here leaves each review mode one
|
|
3
|
+
* call to make. */
|
|
4
|
+
import type { GitHubClient, Json } from "../types.ts";
|
|
5
|
+
import type { Revision } from "../review/revision.ts";
|
|
6
|
+
import { type DiffFile, type ExtensionTracking, type ReviewContext } from "./session.ts";
|
|
7
|
+
/** GitHub's file list as diff files. */
|
|
8
|
+
export declare function githubDiffFiles(files: Json[]): DiffFile[];
|
|
9
|
+
/** A local review: the command runs in the checkout. */
|
|
10
|
+
export declare function localContext(input: {
|
|
11
|
+
repo: string;
|
|
12
|
+
branch: string;
|
|
13
|
+
root: string;
|
|
14
|
+
headSha: string;
|
|
15
|
+
baseSha: string;
|
|
16
|
+
revision: Revision;
|
|
17
|
+
/** `--fresh` starts the extension findings over too. */
|
|
18
|
+
fresh: boolean;
|
|
19
|
+
}): ReviewContext;
|
|
20
|
+
/** A pull request reviewed from the CLI. The pull request and its files are
|
|
21
|
+
* read only when an extension is going to run. */
|
|
22
|
+
export declare function cliPrContext(input: {
|
|
23
|
+
client: GitHubClient;
|
|
24
|
+
repo: string;
|
|
25
|
+
prNumber: number;
|
|
26
|
+
/** What the carry-over code already read, so nothing is read twice. */
|
|
27
|
+
prefetched?: {
|
|
28
|
+
pull: Json;
|
|
29
|
+
files: Json[];
|
|
30
|
+
};
|
|
31
|
+
}): ReviewContext;
|
|
32
|
+
/** `serve` keeps each review's extensions in app.db, and the next review of
|
|
33
|
+
* the same subject compares against them. */
|
|
34
|
+
export declare function storeTracking(subjectId: string, reviewId: string, countRound?: boolean): ExtensionTracking;
|
|
35
|
+
/** A pull request reviewed by `serve` as the GitHub App. The job id is the run
|
|
36
|
+
* id, so a rerun after a restart finds its folder. */
|
|
37
|
+
export declare function appPrContext(input: {
|
|
38
|
+
jobId: string;
|
|
39
|
+
repo: string;
|
|
40
|
+
prNumber: number;
|
|
41
|
+
headSha: string;
|
|
42
|
+
baseSha: string;
|
|
43
|
+
branch: string | null;
|
|
44
|
+
scope: string;
|
|
45
|
+
sinceCommit: string | null;
|
|
46
|
+
trigger: string;
|
|
47
|
+
round: number;
|
|
48
|
+
files: Json[];
|
|
49
|
+
subjectId: string;
|
|
50
|
+
reviewId: string;
|
|
51
|
+
signal?: AbortSignal;
|
|
52
|
+
log?: (message: string) => void;
|
|
53
|
+
}): ReviewContext;
|
|
54
|
+
/** A remote review: `serve` runs its own registrations on the client's diff.
|
|
55
|
+
* The client's checkout is on another machine, so there is no workdir. */
|
|
56
|
+
export declare function remoteContext(input: {
|
|
57
|
+
jobId: string;
|
|
58
|
+
repo: string;
|
|
59
|
+
branch: string | null;
|
|
60
|
+
revision: Revision;
|
|
61
|
+
subjectId: string | undefined;
|
|
62
|
+
reviewId: string;
|
|
63
|
+
signal?: AbortSignal;
|
|
64
|
+
log?: (message: string) => void;
|
|
65
|
+
}): ReviewContext;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { unifiedDiff, } from "./session.js";
|
|
2
|
+
import { earlierReviewCount, previousExtensionFindings, saveExtensionReport, } from "../store/extensions.js";
|
|
3
|
+
import { localTracking } from "./local_tracking.js";
|
|
4
|
+
/** GitHub's file list as diff files. */
|
|
5
|
+
export function githubDiffFiles(files) {
|
|
6
|
+
return files.map((file) => ({
|
|
7
|
+
path: String(file.filename ?? ""),
|
|
8
|
+
previousPath: typeof file.previous_filename === "string"
|
|
9
|
+
? file.previous_filename
|
|
10
|
+
: null,
|
|
11
|
+
status: String(file.status ?? "modified"),
|
|
12
|
+
patch: String(file.patch ?? ""),
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
15
|
+
/** A local review: the command runs in the checkout. */
|
|
16
|
+
export function localContext(input) {
|
|
17
|
+
return {
|
|
18
|
+
tracking: localTracking({
|
|
19
|
+
repo: input.repo,
|
|
20
|
+
root: input.root,
|
|
21
|
+
branch: input.branch,
|
|
22
|
+
fresh: input.fresh,
|
|
23
|
+
}),
|
|
24
|
+
owner: "cli",
|
|
25
|
+
reviewType: "local",
|
|
26
|
+
repo: input.repo,
|
|
27
|
+
prNumber: null,
|
|
28
|
+
branch: input.branch,
|
|
29
|
+
workdir: input.root,
|
|
30
|
+
headSha: input.headSha,
|
|
31
|
+
baseSha: input.baseSha,
|
|
32
|
+
scope: "workspace",
|
|
33
|
+
sinceCommit: null,
|
|
34
|
+
trigger: "cli",
|
|
35
|
+
round: 1,
|
|
36
|
+
diff: () => unifiedDiff(input.revision.files),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** A pull request reviewed from the CLI. The pull request and its files are
|
|
40
|
+
* read only when an extension is going to run. */
|
|
41
|
+
export function cliPrContext(input) {
|
|
42
|
+
const context = {
|
|
43
|
+
owner: "cli",
|
|
44
|
+
reviewType: "pr",
|
|
45
|
+
repo: input.repo,
|
|
46
|
+
prNumber: input.prNumber,
|
|
47
|
+
branch: null,
|
|
48
|
+
workdir: "",
|
|
49
|
+
headSha: null,
|
|
50
|
+
baseSha: null,
|
|
51
|
+
scope: "whole-pr",
|
|
52
|
+
sinceCommit: null,
|
|
53
|
+
trigger: "cli",
|
|
54
|
+
round: 1,
|
|
55
|
+
diff: async () => {
|
|
56
|
+
const [pr, files] = input.prefetched
|
|
57
|
+
? [input.prefetched.pull, input.prefetched.files]
|
|
58
|
+
: await Promise.all([
|
|
59
|
+
input.client.request(`repos/${input.repo}/pulls/${input.prNumber}`),
|
|
60
|
+
input.client.pages(`repos/${input.repo}/pulls/${input.prNumber}/files`),
|
|
61
|
+
]);
|
|
62
|
+
// The shas come with the same request, and details.json is written
|
|
63
|
+
// after the diff, so they are filled in here.
|
|
64
|
+
context.headSha =
|
|
65
|
+
String(pr.head?.sha ?? "") || null;
|
|
66
|
+
context.baseSha =
|
|
67
|
+
String(pr.base?.sha ?? "") || null;
|
|
68
|
+
context.branch = String(pr.head?.ref ?? "") || null;
|
|
69
|
+
return unifiedDiff(githubDiffFiles(files));
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
return context;
|
|
73
|
+
}
|
|
74
|
+
/** `serve` keeps each review's extensions in app.db, and the next review of
|
|
75
|
+
* the same subject compares against them. */
|
|
76
|
+
export function storeTracking(subjectId, reviewId, countRound = false) {
|
|
77
|
+
return {
|
|
78
|
+
load: async () => ({
|
|
79
|
+
...(countRound
|
|
80
|
+
? { round: earlierReviewCount(subjectId, reviewId) + 1 }
|
|
81
|
+
: {}),
|
|
82
|
+
previous: previousExtensionFindings(subjectId, reviewId),
|
|
83
|
+
}),
|
|
84
|
+
save: async (report) => saveExtensionReport(reviewId, report),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/** A pull request reviewed by `serve` as the GitHub App. The job id is the run
|
|
88
|
+
* id, so a rerun after a restart finds its folder. */
|
|
89
|
+
export function appPrContext(input) {
|
|
90
|
+
return {
|
|
91
|
+
tracking: storeTracking(input.subjectId, input.reviewId),
|
|
92
|
+
runId: input.jobId,
|
|
93
|
+
owner: "serve",
|
|
94
|
+
reviewType: "pr",
|
|
95
|
+
repo: input.repo,
|
|
96
|
+
prNumber: input.prNumber,
|
|
97
|
+
branch: input.branch,
|
|
98
|
+
workdir: "",
|
|
99
|
+
headSha: input.headSha || null,
|
|
100
|
+
baseSha: input.baseSha || null,
|
|
101
|
+
scope: input.scope,
|
|
102
|
+
sinceCommit: input.sinceCommit,
|
|
103
|
+
trigger: input.trigger,
|
|
104
|
+
round: input.round,
|
|
105
|
+
diff: () => unifiedDiff(githubDiffFiles(input.files)),
|
|
106
|
+
signal: input.signal,
|
|
107
|
+
log: input.log,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/** A remote review: `serve` runs its own registrations on the client's diff.
|
|
111
|
+
* The client's checkout is on another machine, so there is no workdir. */
|
|
112
|
+
export function remoteContext(input) {
|
|
113
|
+
return {
|
|
114
|
+
...(input.subjectId
|
|
115
|
+
? { tracking: storeTracking(input.subjectId, input.reviewId, true) }
|
|
116
|
+
: {}),
|
|
117
|
+
runId: input.jobId,
|
|
118
|
+
owner: "serve",
|
|
119
|
+
reviewType: "remote",
|
|
120
|
+
repo: input.repo,
|
|
121
|
+
prNumber: null,
|
|
122
|
+
branch: input.branch,
|
|
123
|
+
workdir: "",
|
|
124
|
+
headSha: null,
|
|
125
|
+
baseSha: null,
|
|
126
|
+
scope: "workspace",
|
|
127
|
+
sinceCommit: null,
|
|
128
|
+
trigger: "remote",
|
|
129
|
+
round: 1,
|
|
130
|
+
diff: () => unifiedDiff(input.revision.files),
|
|
131
|
+
signal: input.signal,
|
|
132
|
+
log: input.log,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** A review's total cost includes its extensions. The review's own cost is
|
|
2
|
+
* the total minus the extensions, and each extension's cost comes from the
|
|
3
|
+
* `usage` it reported. */
|
|
4
|
+
import type { CostTally } from "../util/cost.ts";
|
|
5
|
+
import { type ExtensionReport } from "./report.ts";
|
|
6
|
+
/** Adds the extensions' reported cost to a review's tally. It adds no AI
|
|
7
|
+
* call, so an AI cost that is unknown keeps the total unknown. */
|
|
8
|
+
export declare function addExtensionCost(tally: CostTally, report: ExtensionReport): void;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { extensionsCostUsd } from "./report.js";
|
|
2
|
+
/** Adds the extensions' reported cost to a review's tally. It adds no AI
|
|
3
|
+
* call, so an AI cost that is unknown keeps the total unknown. */
|
|
4
|
+
export function addExtensionCost(tally, report) {
|
|
5
|
+
tally.cost += extensionsCostUsd(report);
|
|
6
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Extension findings across local review rounds. They sit in the cache next
|
|
2
|
+
* to the local carry-over, under their own key, so the AI carry-over format
|
|
3
|
+
* does not change. `--fresh` starts them over too. */
|
|
4
|
+
import { cacheDelete, cacheGet, cacheSet } from "../store/cache_db.js";
|
|
5
|
+
const NS = "local-extension-findings";
|
|
6
|
+
function key(repo, root, branch) {
|
|
7
|
+
return `${repo}\0${root}\0${branch}`;
|
|
8
|
+
}
|
|
9
|
+
export function localTracking(input) {
|
|
10
|
+
const id = key(input.repo, input.root, input.branch);
|
|
11
|
+
let round = 1;
|
|
12
|
+
return {
|
|
13
|
+
async load() {
|
|
14
|
+
if (input.fresh) {
|
|
15
|
+
await cacheDelete(NS, id).catch(() => { });
|
|
16
|
+
return { round, previous: [] };
|
|
17
|
+
}
|
|
18
|
+
const raw = await cacheGet(NS, id);
|
|
19
|
+
if (!raw)
|
|
20
|
+
return { round, previous: [] };
|
|
21
|
+
const stored = JSON.parse(raw);
|
|
22
|
+
round = (stored.round ?? 0) + 1;
|
|
23
|
+
return { round, previous: stored.previous ?? [] };
|
|
24
|
+
},
|
|
25
|
+
async save(_report, previous) {
|
|
26
|
+
await cacheSet(NS, id, JSON.stringify({ round, previous }));
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|