opencode-magi 0.0.0-dev-20260924073647 → 0.0.0-dev-20261027030404
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.ja.md +1 -1
- package/README.md +1 -1
- package/dist/config/resolve.js +11 -10
- package/dist/index.js +28 -10
- package/dist/magi.js +50 -67
- package/dist/tools/clear/index.js +14 -12
- package/dist/tools/merge/action.js +4 -4
- package/dist/tools/merge/context.js +2 -2
- package/dist/tools/merge/editor.js +18 -12
- package/dist/tools/merge/index.js +11 -9
- package/dist/tools/merge/merge.js +2 -2
- package/dist/tools/merge/report.js +2 -2
- package/dist/tools/review/action.js +14 -14
- package/dist/tools/review/check.js +15 -12
- package/dist/tools/review/context.js +5 -5
- package/dist/tools/review/index.js +11 -9
- package/dist/tools/review/report.js +2 -2
- package/dist/tools/review/review.js +7 -7
- package/dist/tools/review/reviewer.js +9 -9
- package/dist/tools/triage/index.js +11 -9
- package/dist/tools/validate/index.js +6 -6
- package/dist/utils/github.js +6 -3
- package/dist/utils/opencode.js +4 -12
- package/package.json +20 -21
- package/schema.json +1 -1
package/README.ja.md
CHANGED
package/README.md
CHANGED
package/dist/config/resolve.js
CHANGED
|
@@ -12,28 +12,29 @@ export function resolvePermissions(permissions) {
|
|
|
12
12
|
"edit",
|
|
13
13
|
"glob",
|
|
14
14
|
"grep",
|
|
15
|
-
"
|
|
16
|
-
"
|
|
15
|
+
"shell",
|
|
16
|
+
"subagent",
|
|
17
17
|
"skill",
|
|
18
18
|
"lsp",
|
|
19
19
|
"webfetch",
|
|
20
20
|
"websearch",
|
|
21
21
|
"external_directory",
|
|
22
22
|
"doom_loop",
|
|
23
|
-
].map((
|
|
24
|
-
action
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
].map((action) => ({
|
|
24
|
+
action,
|
|
25
|
+
effect: permissions,
|
|
26
|
+
resource: "*",
|
|
27
27
|
}));
|
|
28
28
|
else
|
|
29
29
|
return Object.entries(permissions).flatMap(([permission, rule]) => {
|
|
30
|
+
const action = { bash: "shell", patch: "edit", task: "subagent", write: "edit" }[permission] ?? permission;
|
|
30
31
|
if (isString(rule))
|
|
31
|
-
return [{ action: rule,
|
|
32
|
+
return [{ action, effect: rule, resource: "*" }];
|
|
32
33
|
else
|
|
33
|
-
return Object.entries(rule).map(([
|
|
34
|
+
return Object.entries(rule).map(([resource, effect]) => ({
|
|
34
35
|
action,
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
effect,
|
|
37
|
+
resource,
|
|
37
38
|
}));
|
|
38
39
|
});
|
|
39
40
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,32 @@
|
|
|
1
|
+
import { Plugin } from "@opencode/plugin";
|
|
1
2
|
import { commands } from "@/commands";
|
|
2
3
|
import { Magi } from "./magi";
|
|
3
4
|
import { tools } from "./tools";
|
|
4
|
-
export const plugin =
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
export const plugin = Plugin.define({
|
|
6
|
+
id: "opencode-magi",
|
|
7
|
+
async setup(context) {
|
|
8
|
+
const magi = new Magi(context);
|
|
9
|
+
await context.command.transform((editor) => {
|
|
10
|
+
Object.entries(commands).forEach(([name, command]) => {
|
|
11
|
+
editor.add({
|
|
12
|
+
description: command.description,
|
|
13
|
+
async execute({ delivery, prompt, sessionID }) {
|
|
14
|
+
await context.session.prompt({
|
|
15
|
+
...prompt,
|
|
16
|
+
delivery,
|
|
17
|
+
sessionID,
|
|
18
|
+
text: command.template.replaceAll("$ARGUMENTS", () => prompt.text),
|
|
19
|
+
});
|
|
20
|
+
},
|
|
21
|
+
name,
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
await context.tool.transform((editor) => {
|
|
26
|
+
Object.values(tools).forEach((tool) => {
|
|
27
|
+
Object.values(tool(magi)).forEach((definition) => editor.add(definition));
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
});
|
|
14
32
|
export default plugin;
|
package/dist/magi.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createOpencodeClient } from "@opencode-ai/sdk/v2";
|
|
2
1
|
import { print } from "graphql";
|
|
3
2
|
import { randomUUID } from "node:crypto";
|
|
4
3
|
import { appendFile, mkdir, readdir, readFile, writeFile, } from "node:fs/promises";
|
|
@@ -6,7 +5,7 @@ import { dirname, isAbsolute, join } from "node:path";
|
|
|
6
5
|
import { Octokit } from "octokit";
|
|
7
6
|
import { getConfig, resolvePermissions, validateConfig } from "@/config";
|
|
8
7
|
import { graphql } from "@/graphql";
|
|
9
|
-
import { command, createExec, filterEmpty, isArray, isNumber, isObject,
|
|
8
|
+
import { command, createExec, filterEmpty, isArray, isNumber, isObject, merge, quote, rm, } from "@/utils";
|
|
10
9
|
const active = new Set(["preparing", "running"]);
|
|
11
10
|
export class MagiError extends Error {
|
|
12
11
|
status;
|
|
@@ -20,15 +19,10 @@ export class Magi {
|
|
|
20
19
|
input;
|
|
21
20
|
options;
|
|
22
21
|
exec;
|
|
23
|
-
constructor(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
fetch: input.client.session._client.getConfig().fetch,
|
|
28
|
-
});
|
|
29
|
-
this.input = { ...input, client };
|
|
30
|
-
this.options = options;
|
|
31
|
-
this.exec = createExec(input.directory);
|
|
22
|
+
constructor(context) {
|
|
23
|
+
this.input = { client: context, directory: context.location.directory };
|
|
24
|
+
this.options = context.options;
|
|
25
|
+
this.exec = createExec(context.location.directory);
|
|
32
26
|
}
|
|
33
27
|
getGhToken(account, signal) {
|
|
34
28
|
return this.exec(command("gh", "auth", "token", account && "--user", account && quote(account)), { signal });
|
|
@@ -200,84 +194,73 @@ export class Magi {
|
|
|
200
194
|
variant,
|
|
201
195
|
},
|
|
202
196
|
parentID,
|
|
203
|
-
|
|
197
|
+
permissions: resolvePermissions(permissions),
|
|
204
198
|
title,
|
|
205
199
|
}, { signal });
|
|
206
|
-
|
|
207
|
-
if (!isUndefined(result.response) && result.response.statusText)
|
|
208
|
-
throw new Error(result.response.statusText);
|
|
209
|
-
else if (result.error instanceof Error)
|
|
210
|
-
throw new Error(result.error.message);
|
|
211
|
-
else
|
|
212
|
-
throw new Error(JSON.stringify(result.error));
|
|
213
|
-
}
|
|
214
|
-
else {
|
|
215
|
-
const id = result.data.id;
|
|
216
|
-
return id;
|
|
217
|
-
}
|
|
200
|
+
return result.id;
|
|
218
201
|
}
|
|
219
202
|
async promptSession(sessionID, text, signal) {
|
|
203
|
+
signal?.throwIfAborted();
|
|
220
204
|
const controller = new AbortController();
|
|
221
|
-
const
|
|
205
|
+
const cancelled = Promise.withResolvers();
|
|
206
|
+
const abort = () => cancelled.reject(signal?.reason);
|
|
207
|
+
const events = this.input.client.event.subscribe({
|
|
222
208
|
signal: controller.signal,
|
|
223
209
|
});
|
|
210
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
224
211
|
try {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
212
|
+
return await Promise.race([
|
|
213
|
+
cancelled.promise,
|
|
214
|
+
this.monitorSession(sessionID, events),
|
|
215
|
+
this.readSessionOutput(sessionID, text, signal
|
|
216
|
+
? AbortSignal.any([signal, controller.signal])
|
|
217
|
+
: controller.signal),
|
|
231
218
|
]);
|
|
232
|
-
if (result.error) {
|
|
233
|
-
if (!isUndefined(result.response) && result.response.statusText)
|
|
234
|
-
throw new Error(result.response.statusText);
|
|
235
|
-
else if (result.error instanceof Error)
|
|
236
|
-
throw new Error(result.error.message);
|
|
237
|
-
else
|
|
238
|
-
throw new Error(JSON.stringify(result.error));
|
|
239
|
-
}
|
|
240
|
-
else {
|
|
241
|
-
const output = result.data.parts
|
|
242
|
-
.filter((part) => part.type === "text")
|
|
243
|
-
.map((part) => part.text)
|
|
244
|
-
.join("\n");
|
|
245
|
-
if (!output)
|
|
246
|
-
throw new Error("OpenCode session.prompt did not return text output.");
|
|
247
|
-
return output;
|
|
248
|
-
}
|
|
249
219
|
}
|
|
250
220
|
catch (e) {
|
|
251
|
-
if (e instanceof MagiError)
|
|
252
|
-
await this.input.client.session.
|
|
221
|
+
if (e instanceof MagiError || signal?.aborted)
|
|
222
|
+
await this.input.client.session.interrupt({ resume: false, sessionID });
|
|
253
223
|
throw e;
|
|
254
224
|
}
|
|
255
225
|
finally {
|
|
226
|
+
signal?.removeEventListener("abort", abort);
|
|
256
227
|
controller.abort();
|
|
257
228
|
}
|
|
258
229
|
}
|
|
230
|
+
async readSessionOutput(sessionID, text, signal) {
|
|
231
|
+
const prompt = await this.input.client.session.prompt({ sessionID, text }, { signal });
|
|
232
|
+
await this.input.client.session.wait({ sessionID }, { signal });
|
|
233
|
+
const messages = await this.input.client.session.context({ sessionID }, { signal });
|
|
234
|
+
const response = messages.findLast((message) => message.type === "assistant");
|
|
235
|
+
if (!response || response.time.created < prompt.time.created)
|
|
236
|
+
throw new Error("OpenCode session.prompt did not return text output.");
|
|
237
|
+
if (response.error)
|
|
238
|
+
throw new Error(response.error.message);
|
|
239
|
+
const output = response.content
|
|
240
|
+
.filter((part) => part.type === "text")
|
|
241
|
+
.map((part) => part.text)
|
|
242
|
+
.join("\n");
|
|
243
|
+
if (!output)
|
|
244
|
+
throw new Error("OpenCode session.prompt did not return text output.");
|
|
245
|
+
return output;
|
|
246
|
+
}
|
|
259
247
|
async monitorSession(sessionID, events) {
|
|
260
248
|
for await (const ev of events) {
|
|
261
|
-
if (ev.type === "permission.asked" &&
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
249
|
+
if (ev.type === "permission.asked" && ev.data.sessionID === sessionID) {
|
|
250
|
+
const { action, id, resources } = ev.data;
|
|
251
|
+
await this.input.client.permission
|
|
252
|
+
.reply({
|
|
253
|
+
decision: "reject",
|
|
266
254
|
requestID: id,
|
|
267
|
-
|
|
268
|
-
|
|
255
|
+
sessionID,
|
|
256
|
+
})
|
|
257
|
+
.catch(() => {
|
|
269
258
|
throw new MagiError("blocked", "Could not reject permission request.");
|
|
270
|
-
throw new MagiError("blocked", `OpenCode session requested ${permission} permission for ${patterns.join(", ")}.`);
|
|
271
|
-
}
|
|
272
|
-
if (ev.type === "question.asked" &&
|
|
273
|
-
ev.properties.sessionID === sessionID) {
|
|
274
|
-
const result = await this.input.client.question.reject({
|
|
275
|
-
requestID: ev.properties.id,
|
|
276
259
|
});
|
|
277
|
-
|
|
278
|
-
throw new MagiError("blocked", "Could not reject user question.");
|
|
279
|
-
throw new MagiError("blocked", "OpenCode session requested user input.");
|
|
260
|
+
throw new MagiError("blocked", `OpenCode session requested ${action} permission for ${resources.join(", ")}.`);
|
|
280
261
|
}
|
|
262
|
+
if (ev.type === "form.created" && ev.data.form.sessionID === sessionID)
|
|
263
|
+
throw new MagiError("blocked", "OpenCode session requested user input.");
|
|
281
264
|
}
|
|
282
265
|
return new Promise(() => { });
|
|
283
266
|
}
|
|
@@ -286,7 +269,7 @@ export class Magi {
|
|
|
286
269
|
let count = 0;
|
|
287
270
|
for (const sessionID of sessionIds)
|
|
288
271
|
try {
|
|
289
|
-
await this.input.client.session.
|
|
272
|
+
await this.input.client.session.remove({ sessionID });
|
|
290
273
|
count += 1;
|
|
291
274
|
}
|
|
292
275
|
catch { }
|
|
@@ -1,21 +1,23 @@
|
|
|
1
|
-
import { tool } from "@opencode-ai/plugin";
|
|
2
1
|
export const clear = function (magi) {
|
|
3
2
|
return {
|
|
4
|
-
magi_clear:
|
|
5
|
-
args: {},
|
|
3
|
+
magi_clear: {
|
|
6
4
|
description: "Clear all inactive Magi runs by deleting configured sessions, worktrees, branches, and output artifacts.",
|
|
7
5
|
async execute() {
|
|
8
6
|
const config = await magi.getConfig();
|
|
9
7
|
const summary = await magi.clear(config);
|
|
10
|
-
return
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
return {
|
|
9
|
+
content: [
|
|
10
|
+
`Cleared Magi runs: ${summary.run}`,
|
|
11
|
+
`Skipped active runs: ${summary.skipped}`,
|
|
12
|
+
`Sessions deleted: ${summary.session}`,
|
|
13
|
+
`Worktrees deleted: ${summary.worktree}`,
|
|
14
|
+
`Branches deleted: ${summary.branch}`,
|
|
15
|
+
`Outputs deleted: ${summary.output}`,
|
|
16
|
+
].join("\n"),
|
|
17
|
+
};
|
|
18
18
|
},
|
|
19
|
-
|
|
19
|
+
input: { additionalProperties: false, properties: {}, type: "object" },
|
|
20
|
+
name: "magi_clear",
|
|
21
|
+
},
|
|
20
22
|
};
|
|
21
23
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MagiError } from "@/magi";
|
|
2
2
|
import { retry } from "@/utils";
|
|
3
3
|
export async function editCycles(cb) {
|
|
4
|
-
this.context.
|
|
4
|
+
this.context.signal.throwIfAborted();
|
|
5
5
|
const error = new Error("Continue edit cycle.");
|
|
6
6
|
const verdict = await retry(async (cycle) => {
|
|
7
7
|
const verdict = await cb(cycle);
|
|
@@ -15,13 +15,13 @@ export async function editCycles(cb) {
|
|
|
15
15
|
await this.updateEvent(`Attempt ${count} failed to edit cycles. Retrying...`);
|
|
16
16
|
},
|
|
17
17
|
retries: this.config.merge.maxThreadResolutionCycles,
|
|
18
|
-
signal: this.context.
|
|
18
|
+
signal: this.context.signal,
|
|
19
19
|
});
|
|
20
20
|
if (!verdict || verdict === "CHANGES_REQUESTED")
|
|
21
21
|
throw new MagiError("blocked", `Reached maximum edit cycles.`);
|
|
22
22
|
}
|
|
23
23
|
export async function postReplies() {
|
|
24
|
-
this.context.
|
|
24
|
+
this.context.signal.throwIfAborted();
|
|
25
25
|
if (this.state.dryRun)
|
|
26
26
|
return;
|
|
27
27
|
const output = this.state.editor?.outputs?.at(-1);
|
|
@@ -32,7 +32,7 @@ export async function postReplies() {
|
|
|
32
32
|
await this.updateEvent(`Posting editor replies.`);
|
|
33
33
|
if (!this.state.editor?.account)
|
|
34
34
|
throw new MagiError("blocked", "Editor account not found.");
|
|
35
|
-
const octokit = await this.magi.createOctokit(this.config, this.context.
|
|
35
|
+
const octokit = await this.magi.createOctokit(this.config, this.context.signal, this.state.editor.account);
|
|
36
36
|
const args = {
|
|
37
37
|
owner: this.config.github.owner,
|
|
38
38
|
pull_number: this.number,
|
|
@@ -2,7 +2,7 @@ import { MagiError } from "@/magi";
|
|
|
2
2
|
import { getClosingIssues, getComments, getInlineCommentTargets, getReviewThreads, } from "@/tools/review/context";
|
|
3
3
|
import { marker } from "@/utils";
|
|
4
4
|
export async function fetchMergeContext() {
|
|
5
|
-
this.context.
|
|
5
|
+
this.context.signal.throwIfAborted();
|
|
6
6
|
const output = this.state.editor?.outputs?.at(-1);
|
|
7
7
|
if (!output)
|
|
8
8
|
throw new MagiError("blocked", "Editor output not found.");
|
|
@@ -29,7 +29,7 @@ export async function fetchMergeContext() {
|
|
|
29
29
|
await this.updateEvent(`Finished fetching merge context.`);
|
|
30
30
|
}
|
|
31
31
|
export async function markRepliedReviewers() {
|
|
32
|
-
this.context.
|
|
32
|
+
this.context.signal.throwIfAborted();
|
|
33
33
|
await this.updateEvent(`Marking replied reviewers.`);
|
|
34
34
|
const output = this.state.editor?.outputs?.at(-1);
|
|
35
35
|
if (!output)
|
|
@@ -3,7 +3,7 @@ import { Prompt } from "@/prompts";
|
|
|
3
3
|
import { getMetadata } from "@/tools/review/check";
|
|
4
4
|
import { command, filterDuplicates, filterEmpty, quote, retry } from "@/utils";
|
|
5
5
|
export async function edit() {
|
|
6
|
-
this.context.
|
|
6
|
+
this.context.signal.throwIfAborted();
|
|
7
7
|
await this.updateEvent(`Editing.`);
|
|
8
8
|
await setAccount.call(this);
|
|
9
9
|
const unresolvedThreads = await getUnresolvedThreads.call(this);
|
|
@@ -24,7 +24,7 @@ export async function edit() {
|
|
|
24
24
|
worktreePath: this.state.worktree.path,
|
|
25
25
|
});
|
|
26
26
|
const output = await retry(async (count, e) => {
|
|
27
|
-
const raw = await this.magi.promptSession(this.state.editor.sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.
|
|
27
|
+
const raw = await this.magi.promptSession(this.state.editor.sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.signal);
|
|
28
28
|
await this.createAgentFile("edit", "editor", raw, count, cycle);
|
|
29
29
|
const parsed = prompt.parse(raw);
|
|
30
30
|
if (!prompt.validate(parsed))
|
|
@@ -36,7 +36,7 @@ export async function edit() {
|
|
|
36
36
|
if (output.mode === "EDITED") {
|
|
37
37
|
const head = await this.exec(command("git", "rev-parse", "HEAD"), {
|
|
38
38
|
cwd: this.state.worktree.path,
|
|
39
|
-
signal: this.context.
|
|
39
|
+
signal: this.context.signal,
|
|
40
40
|
});
|
|
41
41
|
if (head !== output.commitSha)
|
|
42
42
|
throw new Error(`Editor reported commit ${output.commitSha}, but worktree HEAD is ${head}.`);
|
|
@@ -49,7 +49,7 @@ export async function edit() {
|
|
|
49
49
|
await this.updateEvent(`Attempt ${count} failed to edit. Retrying...`);
|
|
50
50
|
},
|
|
51
51
|
retries: this.config.output.repairAttempts,
|
|
52
|
-
signal: this.context.
|
|
52
|
+
signal: this.context.signal,
|
|
53
53
|
});
|
|
54
54
|
if (!output)
|
|
55
55
|
throw new MagiError("blocked", "Invalid output for editor.");
|
|
@@ -93,11 +93,14 @@ export async function edit() {
|
|
|
93
93
|
return output.mode === "EDITED" && !this.state.dryRun;
|
|
94
94
|
}
|
|
95
95
|
export async function resolveConflict() {
|
|
96
|
-
this.context.
|
|
96
|
+
this.context.signal.throwIfAborted();
|
|
97
97
|
await this.updateEvent(`Resolving merge conflicts.`);
|
|
98
98
|
if (!this.state.pr?.metadata)
|
|
99
99
|
throw new MagiError("blocked", "PR metadata not found.");
|
|
100
|
-
const options = {
|
|
100
|
+
const options = {
|
|
101
|
+
cwd: this.state.worktree.path,
|
|
102
|
+
signal: this.context.signal,
|
|
103
|
+
};
|
|
101
104
|
await setAccount.call(this);
|
|
102
105
|
await this.exec(command("git", "fetch", "--no-tags", quote(this.state.pr.metadata.base.repo.clone_url), quote(`refs/heads/${this.state.pr.metadata.base.ref}`)), options);
|
|
103
106
|
const conflictedFiles = await getConflictedFiles.call(this);
|
|
@@ -115,7 +118,7 @@ export async function resolveConflict() {
|
|
|
115
118
|
worktreePath: this.state.worktree.path,
|
|
116
119
|
});
|
|
117
120
|
const output = await retry(async (count, e) => {
|
|
118
|
-
const raw = await this.magi.promptSession(this.state.editor.sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.
|
|
121
|
+
const raw = await this.magi.promptSession(this.state.editor.sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.signal);
|
|
119
122
|
await this.createAgentFile("conflict", "editor", raw, count, cycle);
|
|
120
123
|
const parsed = prompt.parse(raw);
|
|
121
124
|
if (!prompt.validate(parsed))
|
|
@@ -144,7 +147,7 @@ export async function resolveConflict() {
|
|
|
144
147
|
await this.updateEvent(`Attempt ${count} failed to resolve conflicts. Retrying...`);
|
|
145
148
|
},
|
|
146
149
|
retries: this.config.output.repairAttempts,
|
|
147
|
-
signal: this.context.
|
|
150
|
+
signal: this.context.signal,
|
|
148
151
|
});
|
|
149
152
|
if (!output)
|
|
150
153
|
throw new MagiError("blocked", "Invalid output for conflict editor.");
|
|
@@ -178,12 +181,12 @@ async function setAccount() {
|
|
|
178
181
|
throw new MagiError("blocked", "Editor author not found.");
|
|
179
182
|
if (!this.state.worktree)
|
|
180
183
|
throw new MagiError("blocked", "PR worktree not found.");
|
|
181
|
-
const options = { cwd: this.state.worktree.path, signal: this.context.
|
|
184
|
+
const options = { cwd: this.state.worktree.path, signal: this.context.signal };
|
|
182
185
|
await this.exec(command("git", "config", "user.name", quote(this.state.editor.author.name)), options);
|
|
183
186
|
await this.exec(command("git", "config", "user.email", quote(this.state.editor.author.email)), options);
|
|
184
187
|
}
|
|
185
188
|
async function push() {
|
|
186
|
-
const token = await this.magi.getGhToken(this.state.editor.account, this.context.
|
|
189
|
+
const token = await this.magi.getGhToken(this.state.editor.account, this.context.signal);
|
|
187
190
|
const url = `https://${this.config.github.host}/${this.state.pr.metadata.head.repo.owner.login}/${this.state.pr.metadata.head.repo.name}.git`;
|
|
188
191
|
const ref = `HEAD:refs/heads/${this.state.pr.metadata.head.ref}`;
|
|
189
192
|
await this.exec(command("git", "push", quote(url), quote(ref)), {
|
|
@@ -199,12 +202,15 @@ async function push() {
|
|
|
199
202
|
GIT_PASSWORD: token,
|
|
200
203
|
GIT_TERMINAL_PROMPT: "0",
|
|
201
204
|
},
|
|
202
|
-
signal: this.context.
|
|
205
|
+
signal: this.context.signal,
|
|
203
206
|
});
|
|
204
207
|
return getMetadata.call(this);
|
|
205
208
|
}
|
|
206
209
|
async function getConflictedFiles() {
|
|
207
|
-
const options = {
|
|
210
|
+
const options = {
|
|
211
|
+
cwd: this.state.worktree.path,
|
|
212
|
+
signal: this.context.signal,
|
|
213
|
+
};
|
|
208
214
|
try {
|
|
209
215
|
await this.exec(command("git", "merge", "--no-commit", "--no-ff", "FETCH_HEAD"), options);
|
|
210
216
|
await this.exec(command("git", "merge", "--abort"), options);
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { tool } from "@opencode-ai/plugin";
|
|
2
1
|
import { parsePrs, split, Worker } from "@/utils";
|
|
3
2
|
import { Merge } from "./merge";
|
|
4
3
|
async function editCycle(run, { conflict, cycle } = {}) {
|
|
@@ -90,18 +89,14 @@ function overrideConfig(config, args, dryRun = false) {
|
|
|
90
89
|
}
|
|
91
90
|
export const merge = function (magi) {
|
|
92
91
|
return {
|
|
93
|
-
magi_merge:
|
|
94
|
-
args: {
|
|
95
|
-
dryRun: tool.schema.boolean().optional(),
|
|
96
|
-
prs: tool.schema.string(),
|
|
97
|
-
},
|
|
92
|
+
magi_merge: {
|
|
98
93
|
description: "Review, fix, and merge one or more pull requests.",
|
|
99
94
|
async execute(args, context) {
|
|
100
95
|
const prs = parsePrs(args.prs);
|
|
101
96
|
const { config, dryRun } = overrideConfig(await magi.getConfig({ editor: true, reviewers: true }), split(args.prs), args.dryRun);
|
|
102
97
|
const worker = new Worker(config.review.concurrency.runs);
|
|
103
98
|
const reports = await Promise.all(prs.map(async (pr) => {
|
|
104
|
-
context.
|
|
99
|
+
context.signal.throwIfAborted();
|
|
105
100
|
return worker.run(async () => {
|
|
106
101
|
const run = await Merge.init(pr, magi, config, context, {
|
|
107
102
|
dryRun,
|
|
@@ -144,8 +139,15 @@ export const merge = function (magi) {
|
|
|
144
139
|
const errors = reports.filter(({ error }) => error);
|
|
145
140
|
if (errors.length)
|
|
146
141
|
throw new Error(errors.map(({ report }) => report).join("\n\n"));
|
|
147
|
-
return reports.map(({ report }) => report).join("\n\n");
|
|
142
|
+
return { content: reports.map(({ report }) => report).join("\n\n") };
|
|
143
|
+
},
|
|
144
|
+
input: {
|
|
145
|
+
additionalProperties: false,
|
|
146
|
+
properties: { dryRun: { type: "boolean" }, prs: { type: "string" } },
|
|
147
|
+
required: ["prs"],
|
|
148
|
+
type: "object",
|
|
148
149
|
},
|
|
149
|
-
|
|
150
|
+
name: "magi_merge",
|
|
151
|
+
},
|
|
150
152
|
};
|
|
151
153
|
};
|
|
@@ -26,7 +26,7 @@ export class Merge extends Review {
|
|
|
26
26
|
postReplies = postReplies;
|
|
27
27
|
resolveConflict = resolveConflict;
|
|
28
28
|
async createSession() {
|
|
29
|
-
this.context.
|
|
29
|
+
this.context.signal.throwIfAborted();
|
|
30
30
|
if (!this.state.editor)
|
|
31
31
|
throw new MagiError("blocked", "Editor not found.");
|
|
32
32
|
await this.updateEvent(`Creating editor session.`);
|
|
@@ -34,7 +34,7 @@ export class Merge extends Review {
|
|
|
34
34
|
sessionId: await this.magi.createSession(this.state.sessionId, `magi merge #${this.number} editor`, {
|
|
35
35
|
model: this.state.editor.model,
|
|
36
36
|
permissions: this.state.editor.permissions,
|
|
37
|
-
}, this.context.
|
|
37
|
+
}, this.context.signal),
|
|
38
38
|
};
|
|
39
39
|
await this.updateState({ editor });
|
|
40
40
|
await this.updateEvent(`Finished creating editor session.`);
|
|
@@ -4,7 +4,7 @@ import { MagiError } from "@/magi";
|
|
|
4
4
|
import { createCheckContent, createMetaContent, createReviewerContent, } from "@/tools/review/report";
|
|
5
5
|
import { filterEmpty, toTitleCase } from "@/utils";
|
|
6
6
|
export async function createReport(e) {
|
|
7
|
-
this.context.
|
|
7
|
+
this.context.signal.throwIfAborted();
|
|
8
8
|
if (!e) {
|
|
9
9
|
const status = "completed";
|
|
10
10
|
const text = await createContent.call(this, { status });
|
|
@@ -19,7 +19,7 @@ export async function createReport(e) {
|
|
|
19
19
|
const error = e instanceof Error ? e.message : "Unknown error";
|
|
20
20
|
const status = e instanceof MagiError
|
|
21
21
|
? e.status
|
|
22
|
-
: this.context.
|
|
22
|
+
: this.context.signal.aborted
|
|
23
23
|
? "cancelled"
|
|
24
24
|
: "failed";
|
|
25
25
|
const text = await createContent.call(this, { error, status });
|
|
@@ -9,7 +9,7 @@ const events = {
|
|
|
9
9
|
};
|
|
10
10
|
const mergeWorker = new Worker(1);
|
|
11
11
|
export async function postReviews() {
|
|
12
|
-
this.context.
|
|
12
|
+
this.context.signal.throwIfAborted();
|
|
13
13
|
if (this.state.dryRun)
|
|
14
14
|
return;
|
|
15
15
|
await this.updateEvent(`Posting reviews.`);
|
|
@@ -25,7 +25,7 @@ export async function postReviews() {
|
|
|
25
25
|
if (this.config.mode === "single") {
|
|
26
26
|
if (!this.state.pr?.verdict)
|
|
27
27
|
throw new MagiError("blocked", "PR verdict not found.");
|
|
28
|
-
const octokit = await this.magi.createOctokit(this.config, this.context.
|
|
28
|
+
const octokit = await this.magi.createOctokit(this.config, this.context.signal, this.config.account);
|
|
29
29
|
const graphql = this.magi.createGraphql(octokit);
|
|
30
30
|
const reviewers = Object.entries(this.state.reviewers).filter(([, { status }]) => status !== "skip");
|
|
31
31
|
await Promise.all(reviewers.flatMap(([, { outputs }]) => (outputs?.at(-1)?.resolves ?? []).map(({ threadId }) => graphql.resolveReviewThread({ threadId }))));
|
|
@@ -80,7 +80,7 @@ export async function postReviews() {
|
|
|
80
80
|
verdict: this.state.pr.verdict,
|
|
81
81
|
});
|
|
82
82
|
const output = await retry(async (count, e) => {
|
|
83
|
-
const raw = await this.magi.promptSession(this.state.operator.sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.
|
|
83
|
+
const raw = await this.magi.promptSession(this.state.operator.sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.signal);
|
|
84
84
|
await this.createAgentFile("comment", "operator", raw, count);
|
|
85
85
|
const parsed = prompt.parse(raw);
|
|
86
86
|
if (!prompt.validate(parsed))
|
|
@@ -93,7 +93,7 @@ export async function postReviews() {
|
|
|
93
93
|
await this.updateEvent(`Attempt ${count} failed to post comment by operator. Retrying...`);
|
|
94
94
|
},
|
|
95
95
|
retries: this.config.output.repairAttempts,
|
|
96
|
-
signal: this.context.
|
|
96
|
+
signal: this.context.signal,
|
|
97
97
|
});
|
|
98
98
|
if (!output)
|
|
99
99
|
throw new MagiError("blocked", "Invalid output for operator.");
|
|
@@ -132,7 +132,7 @@ export async function postReviews() {
|
|
|
132
132
|
const output = outputs?.at(-1);
|
|
133
133
|
if (!output)
|
|
134
134
|
throw new MagiError("blocked", "Reviewer output not found.");
|
|
135
|
-
const octokit = await this.magi.createOctokit(this.config, this.context.
|
|
135
|
+
const octokit = await this.magi.createOctokit(this.config, this.context.signal, account);
|
|
136
136
|
const graphql = this.magi.createGraphql(octokit);
|
|
137
137
|
await Promise.all((output.resolves ?? []).map(({ threadId }) => graphql.resolveReviewThread({ threadId })));
|
|
138
138
|
await Promise.all((output.followUps ?? []).map(({ body, commentId }) => octokit.rest.pulls.createReplyForReviewComment({
|
|
@@ -175,7 +175,7 @@ export async function postReviews() {
|
|
|
175
175
|
}
|
|
176
176
|
}
|
|
177
177
|
export async function automate() {
|
|
178
|
-
this.context.
|
|
178
|
+
this.context.signal.throwIfAborted();
|
|
179
179
|
if (!this.state.pr?.metadata)
|
|
180
180
|
throw new MagiError("blocked", "PR metadata not found.");
|
|
181
181
|
if (!this.state.pr.verdict)
|
|
@@ -210,11 +210,11 @@ export async function automate() {
|
|
|
210
210
|
await this.updateEvent(`Skipped ${action} automation during dry run.`);
|
|
211
211
|
return "SKIPPED";
|
|
212
212
|
}
|
|
213
|
-
const token = await this.magi.getGhToken(account, this.context.
|
|
213
|
+
const token = await this.magi.getGhToken(account, this.context.signal);
|
|
214
214
|
if (action === "merge" && !this.config.review.merge.queue) {
|
|
215
215
|
const rules = JSON.parse(await this.exec(command("gh", "api", quote(`repos/${this.config.github.owner}/${this.config.github.repo}/rules/branches/${this.state.pr.metadata.base.ref}`)), {
|
|
216
216
|
env: { GH_TOKEN: token },
|
|
217
|
-
signal: this.context.
|
|
217
|
+
signal: this.context.signal,
|
|
218
218
|
}));
|
|
219
219
|
if (isArray(rules)) {
|
|
220
220
|
const enabledMergeQueue = rules.some((rule) => !!rule &&
|
|
@@ -230,7 +230,7 @@ export async function automate() {
|
|
|
230
230
|
else
|
|
231
231
|
await this.updateEvent(`Waiting to merge.`);
|
|
232
232
|
if (action === "merge" && this.config.review.merge.queue) {
|
|
233
|
-
const octokit = await this.magi.createOctokit(this.config, this.context.
|
|
233
|
+
const octokit = await this.magi.createOctokit(this.config, this.context.signal, account);
|
|
234
234
|
const graphql = this.magi.createGraphql(octokit);
|
|
235
235
|
const enqueuedAt = new Date().toISOString();
|
|
236
236
|
try {
|
|
@@ -272,10 +272,10 @@ export async function automate() {
|
|
|
272
272
|
if (this.config.review.merge.deleteBranch)
|
|
273
273
|
args.push("--delete-branch");
|
|
274
274
|
}
|
|
275
|
-
const options = { env: { GH_TOKEN: token }, signal: this.context.
|
|
275
|
+
const options = { env: { GH_TOKEN: token }, signal: this.context.signal };
|
|
276
276
|
const waitMerge = async () => {
|
|
277
277
|
return await loop(async () => {
|
|
278
|
-
this.context.
|
|
278
|
+
this.context.signal.throwIfAborted();
|
|
279
279
|
const data = await this.exec(command(...prefix, "view", ...suffix, "--json", "autoMergeRequest,mergeStateStatus,state,statusCheckRollup"), options);
|
|
280
280
|
const { autoMergeRequest: auto, mergeStateStatus: status, state, statusCheckRollup: checks, } = JSON.parse(data);
|
|
281
281
|
if (state === "MERGED")
|
|
@@ -389,7 +389,7 @@ function getCheckTime(check) {
|
|
|
389
389
|
}, 0);
|
|
390
390
|
}
|
|
391
391
|
async function waitMergeQueue(enqueuedAt, leftMergeQueue = false, retries = 0) {
|
|
392
|
-
this.context.
|
|
392
|
+
this.context.signal.throwIfAborted();
|
|
393
393
|
if (!this.state.pr?.metadata)
|
|
394
394
|
throw new MagiError("blocked", "PR metadata not found.");
|
|
395
395
|
const { repository } = await this.graphql.mergeQueueStatus({
|
|
@@ -428,13 +428,13 @@ async function waitMergeQueue(enqueuedAt, leftMergeQueue = false, retries = 0) {
|
|
|
428
428
|
}
|
|
429
429
|
throw new MagiError("blocked", `PR left the merge queue before merging.`);
|
|
430
430
|
}
|
|
431
|
-
await wait(30_000, this.context.
|
|
431
|
+
await wait(30_000, this.context.signal);
|
|
432
432
|
return await waitMergeQueue.call(this, enqueuedAt, nextLeftMergeQueue, retries);
|
|
433
433
|
}
|
|
434
434
|
async function isConflict() {
|
|
435
435
|
if (!this.state.worktree)
|
|
436
436
|
return false;
|
|
437
|
-
const options = { cwd: this.state.worktree.path, signal: this.context.
|
|
437
|
+
const options = { cwd: this.state.worktree.path, signal: this.context.signal };
|
|
438
438
|
const status = await this.exec(command("git", "status", "--porcelain"), options);
|
|
439
439
|
if (status)
|
|
440
440
|
throw new MagiError("blocked", "PR worktree has uncommitted changes.");
|
|
@@ -16,7 +16,10 @@ const ci = {
|
|
|
16
16
|
return check.bucket === "fail" || check.state === "FAILURE";
|
|
17
17
|
},
|
|
18
18
|
isPassed(check) {
|
|
19
|
-
return check.bucket === "pass" ||
|
|
19
|
+
return (check.bucket === "pass" ||
|
|
20
|
+
check.bucket === "skipping" ||
|
|
21
|
+
check.state === "SUCCESS" ||
|
|
22
|
+
check.state === "SKIPPED");
|
|
20
23
|
},
|
|
21
24
|
isPending(check) {
|
|
22
25
|
return !this.isFailed(check) && !this.isPassed(check);
|
|
@@ -84,7 +87,7 @@ function getSafetyErrors(conditions, metadata, files) {
|
|
|
84
87
|
});
|
|
85
88
|
}
|
|
86
89
|
export async function checkPr() {
|
|
87
|
-
this.context.
|
|
90
|
+
this.context.signal.throwIfAborted();
|
|
88
91
|
await this.updateState({ status: "running" });
|
|
89
92
|
await this.updateEvent(`Checking PR.`);
|
|
90
93
|
const { files, metadata } = await getMetadata.call(this);
|
|
@@ -110,7 +113,7 @@ export async function checkPr() {
|
|
|
110
113
|
await this.updateEvent(`Finished checking PR.`);
|
|
111
114
|
}
|
|
112
115
|
export async function checkCi(wait = this.config.review.checks.wait) {
|
|
113
|
-
this.context.
|
|
116
|
+
this.context.signal.throwIfAborted();
|
|
114
117
|
await this.updateEvent(`Checking CI.`);
|
|
115
118
|
if (wait)
|
|
116
119
|
await watchChecks.call(this);
|
|
@@ -119,7 +122,7 @@ export async function checkCi(wait = this.config.review.checks.wait) {
|
|
|
119
122
|
await this.updateEvent(`Finished checking CI.`);
|
|
120
123
|
}
|
|
121
124
|
export async function classifyChecks() {
|
|
122
|
-
this.context.
|
|
125
|
+
this.context.signal.throwIfAborted();
|
|
123
126
|
if (!this.state.pr?.checks?.failed.length)
|
|
124
127
|
return;
|
|
125
128
|
await this.updateEvent(`Classifying CI checks.`);
|
|
@@ -148,7 +151,7 @@ export async function classifyChecks() {
|
|
|
148
151
|
throw new Error(`No session ID found for reviewer ${id}.`);
|
|
149
152
|
await this.updateEvent(`Classifying CI checks with reviewer ${id}.`);
|
|
150
153
|
const output = await retry(async (count, e) => {
|
|
151
|
-
const raw = await this.magi.promptSession(sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.
|
|
154
|
+
const raw = await this.magi.promptSession(sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.signal);
|
|
152
155
|
await this.createAgentFile("ci-classification", id, raw, count);
|
|
153
156
|
const parsed = prompt.parse(raw);
|
|
154
157
|
if (!prompt.validate(parsed))
|
|
@@ -161,7 +164,7 @@ export async function classifyChecks() {
|
|
|
161
164
|
await this.updateEvent(`Attempt ${count} failed to classify CI checks with reviewer ${id}. Retrying...`);
|
|
162
165
|
},
|
|
163
166
|
retries: this.config.output.repairAttempts,
|
|
164
|
-
signal: this.context.
|
|
167
|
+
signal: this.context.signal,
|
|
165
168
|
});
|
|
166
169
|
if (!output)
|
|
167
170
|
throw new MagiError("blocked", `Invalid output for reviewer ${id}.`);
|
|
@@ -204,7 +207,7 @@ export async function classifyChecks() {
|
|
|
204
207
|
await this.updateEvent(`Finished classifying CI checks.`);
|
|
205
208
|
}
|
|
206
209
|
export async function rerunChecks() {
|
|
207
|
-
this.context.
|
|
210
|
+
this.context.signal.throwIfAborted();
|
|
208
211
|
if (!this.state.pr?.checks)
|
|
209
212
|
throw new MagiError("blocked", "PR checks not found.");
|
|
210
213
|
const checks = {
|
|
@@ -247,7 +250,7 @@ export async function rerunChecks() {
|
|
|
247
250
|
}, {
|
|
248
251
|
error: (_, count) => this.updateEvent(`Attempt ${count} failed to rerun checks ${label}. Retrying...`),
|
|
249
252
|
retries: this.config.review.checks.retryFailedJobs,
|
|
250
|
-
signal: this.context.
|
|
253
|
+
signal: this.context.signal,
|
|
251
254
|
});
|
|
252
255
|
}
|
|
253
256
|
const passedChecksAfterRerun = checks.passed.filter((check) => "scope" in check && !check.scope);
|
|
@@ -292,7 +295,7 @@ async function getChecks() {
|
|
|
292
295
|
pending: [],
|
|
293
296
|
};
|
|
294
297
|
try {
|
|
295
|
-
const raw = await this.exec(command("gh", "pr", "checks", this.number, "--repo", this.state.repo, "--json", fields, "--required"), { signal: this.context.
|
|
298
|
+
const raw = await this.exec(command("gh", "pr", "checks", this.number, "--repo", this.state.repo, "--json", fields, "--required"), { signal: this.context.signal });
|
|
296
299
|
const data = JSON.parse(raw);
|
|
297
300
|
await Promise.all(data.map(async (check) => {
|
|
298
301
|
check.id = check.link.match(/\/actions\/runs\/\d+\/job\/(\d+)/)[1];
|
|
@@ -318,7 +321,7 @@ async function getChecks() {
|
|
|
318
321
|
return checks;
|
|
319
322
|
}
|
|
320
323
|
async function getCheckLog(id) {
|
|
321
|
-
const log = await this.exec(command("gh", "run", "view", "--repo", this.state.repo, "--job", quote(id), "--log-failed"), { signal: this.context.
|
|
324
|
+
const log = await this.exec(command("gh", "run", "view", "--repo", this.state.repo, "--job", quote(id), "--log-failed"), { signal: this.context.signal });
|
|
322
325
|
return (log
|
|
323
326
|
// oxlint-disable-next-line no-control-regex
|
|
324
327
|
.replaceAll(/\u001B\[[0-9;]*m/g, "")
|
|
@@ -327,11 +330,11 @@ async function getCheckLog(id) {
|
|
|
327
330
|
.join("\n"));
|
|
328
331
|
}
|
|
329
332
|
async function rerunCheck(id) {
|
|
330
|
-
await this.exec(command("gh", "run", "rerun", "--repo", this.state.repo, "--job", quote(id)), { signal: this.context.
|
|
333
|
+
await this.exec(command("gh", "run", "rerun", "--repo", this.state.repo, "--job", quote(id)), { signal: this.context.signal });
|
|
331
334
|
}
|
|
332
335
|
async function watchChecks() {
|
|
333
336
|
try {
|
|
334
|
-
await this.exec(command("gh", "pr", "checks", this.number, "--repo", this.state.repo, "--watch", "--required"), { signal: this.context.
|
|
337
|
+
await this.exec(command("gh", "pr", "checks", this.number, "--repo", this.state.repo, "--watch", "--required"), { signal: this.context.signal });
|
|
335
338
|
}
|
|
336
339
|
catch { }
|
|
337
340
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MagiError } from "@/magi";
|
|
2
2
|
import { command, filterEmpty, marker, quote } from "@/utils";
|
|
3
3
|
export async function checkExistingReviews() {
|
|
4
|
-
this.context.
|
|
4
|
+
this.context.signal.throwIfAborted();
|
|
5
5
|
await this.updateEvent(`Fetching existing reviews.`);
|
|
6
6
|
if (!this.state.pr?.metadata)
|
|
7
7
|
throw new MagiError("blocked", "PR metadata not found.");
|
|
@@ -101,7 +101,7 @@ export async function checkExistingReviews() {
|
|
|
101
101
|
return skip;
|
|
102
102
|
}
|
|
103
103
|
export async function fetchReviewContext() {
|
|
104
|
-
this.context.
|
|
104
|
+
this.context.signal.throwIfAborted();
|
|
105
105
|
await this.updateEvent(`Fetching review context.`);
|
|
106
106
|
const [comments, issues, threads] = await Promise.all([
|
|
107
107
|
getComments.call(this),
|
|
@@ -178,7 +178,7 @@ export async function getInlineCommentTargets() {
|
|
|
178
178
|
const url = source === "base"
|
|
179
179
|
? this.state.pr.metadata.base.repo.clone_url
|
|
180
180
|
: this.state.pr.metadata.head.repo.clone_url;
|
|
181
|
-
await this.exec(command("git", "fetch", "--no-tags", quote(url), quote(`refs/heads/${ref}`)), { cwd: this.state.worktree.path, signal: this.context.
|
|
181
|
+
await this.exec(command("git", "fetch", "--no-tags", quote(url), quote(`refs/heads/${ref}`)), { cwd: this.state.worktree.path, signal: this.context.signal });
|
|
182
182
|
}
|
|
183
183
|
for (const commit of missing) {
|
|
184
184
|
if (await hasCommit.call(this, commit.sha))
|
|
@@ -188,7 +188,7 @@ export async function getInlineCommentTargets() {
|
|
|
188
188
|
: this.state.pr.metadata.head.ref;
|
|
189
189
|
throw new MagiError("blocked", `${commit.label} commit ${commit.sha} is unavailable after fetching ${commit.source} ref ${ref}.`);
|
|
190
190
|
}
|
|
191
|
-
const diff = await this.exec(command("git", "diff", "--no-ext-diff", "--unified=3", quote(`${fromSha}...${toSha}`)), { cwd: this.state.worktree.path, signal: this.context.
|
|
191
|
+
const diff = await this.exec(command("git", "diff", "--no-ext-diff", "--unified=3", quote(`${fromSha}...${toSha}`)), { cwd: this.state.worktree.path, signal: this.context.signal });
|
|
192
192
|
let path;
|
|
193
193
|
let line;
|
|
194
194
|
for (const entry of diff.split("\n")) {
|
|
@@ -244,7 +244,7 @@ async function hasCommit(sha) {
|
|
|
244
244
|
try {
|
|
245
245
|
await this.exec(command("git", "cat-file", "-e", quote(`${sha}^{commit}`)), {
|
|
246
246
|
cwd: this.state.worktree.path,
|
|
247
|
-
signal: this.context.
|
|
247
|
+
signal: this.context.signal,
|
|
248
248
|
});
|
|
249
249
|
return true;
|
|
250
250
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { tool } from "@opencode-ai/plugin";
|
|
2
1
|
import { parsePrs, split, Worker } from "@/utils";
|
|
3
2
|
import { Review } from "./review";
|
|
4
3
|
function overrideConfig(config, args, dryRun = false) {
|
|
@@ -50,18 +49,14 @@ function overrideConfig(config, args, dryRun = false) {
|
|
|
50
49
|
}
|
|
51
50
|
export const review = function (magi) {
|
|
52
51
|
return {
|
|
53
|
-
magi_review:
|
|
54
|
-
args: {
|
|
55
|
-
dryRun: tool.schema.boolean().optional(),
|
|
56
|
-
prs: tool.schema.string(),
|
|
57
|
-
},
|
|
52
|
+
magi_review: {
|
|
58
53
|
description: "Review one or more pull requests and post the reviews.",
|
|
59
54
|
async execute(args, context) {
|
|
60
55
|
const prs = parsePrs(args.prs);
|
|
61
56
|
const { config, dryRun } = overrideConfig(await magi.getConfig({ reviewers: true }), split(args.prs), args.dryRun);
|
|
62
57
|
const worker = new Worker(config.review.concurrency.runs);
|
|
63
58
|
const reports = await Promise.all(prs.map(async (pr) => {
|
|
64
|
-
context.
|
|
59
|
+
context.signal.throwIfAborted();
|
|
65
60
|
return worker.run(async () => {
|
|
66
61
|
const run = await Review.init(pr, magi, config, context, {
|
|
67
62
|
dryRun,
|
|
@@ -97,8 +92,15 @@ export const review = function (magi) {
|
|
|
97
92
|
const errors = reports.filter(({ error }) => error);
|
|
98
93
|
if (errors.length)
|
|
99
94
|
throw new Error(errors.map(({ report }) => report).join("\n\n"));
|
|
100
|
-
return reports.map(({ report }) => report).join("\n\n");
|
|
95
|
+
return { content: reports.map(({ report }) => report).join("\n\n") };
|
|
96
|
+
},
|
|
97
|
+
input: {
|
|
98
|
+
additionalProperties: false,
|
|
99
|
+
properties: { dryRun: { type: "boolean" }, prs: { type: "string" } },
|
|
100
|
+
required: ["prs"],
|
|
101
|
+
type: "object",
|
|
101
102
|
},
|
|
102
|
-
|
|
103
|
+
name: "magi_review",
|
|
104
|
+
},
|
|
103
105
|
};
|
|
104
106
|
};
|
|
@@ -3,7 +3,7 @@ import { join } from "node:path";
|
|
|
3
3
|
import { MagiError } from "@/magi";
|
|
4
4
|
import { filterEmpty, toTitleCase } from "@/utils";
|
|
5
5
|
export async function createReport(e) {
|
|
6
|
-
this.context.
|
|
6
|
+
this.context.signal.throwIfAborted();
|
|
7
7
|
if (!e) {
|
|
8
8
|
const status = "completed";
|
|
9
9
|
const text = await createContent.call(this, { status });
|
|
@@ -18,7 +18,7 @@ export async function createReport(e) {
|
|
|
18
18
|
const error = e instanceof Error ? e.message : "Unknown error";
|
|
19
19
|
const status = e instanceof MagiError
|
|
20
20
|
? e.status
|
|
21
|
-
: this.context.
|
|
21
|
+
: this.context.signal.aborted
|
|
22
22
|
? "cancelled"
|
|
23
23
|
: "failed";
|
|
24
24
|
const text = await createContent.call(this, { error, status });
|
|
@@ -34,7 +34,7 @@ export class Review {
|
|
|
34
34
|
}
|
|
35
35
|
static async setup(number, magi, config, context) {
|
|
36
36
|
const url = `${config.github.url}/pull/${number}`;
|
|
37
|
-
const octokit = await magi.createOctokit(config, context.
|
|
37
|
+
const octokit = await magi.createOctokit(config, context.signal);
|
|
38
38
|
const graphql = magi.createGraphql(octokit);
|
|
39
39
|
const reviewers = Object.fromEntries(config.review.reviewers.map(({ account, id, model, permissions }) => [id, { account, model, permissions }]));
|
|
40
40
|
const operator = config.review.operator
|
|
@@ -66,7 +66,7 @@ export class Review {
|
|
|
66
66
|
automate = automate;
|
|
67
67
|
createReport = createReport;
|
|
68
68
|
async cleanup() {
|
|
69
|
-
if (this.context.
|
|
69
|
+
if (this.context.signal.aborted &&
|
|
70
70
|
["preparing", "running"].includes(this.state.status))
|
|
71
71
|
await this.updateState({
|
|
72
72
|
completedAt: new Date().toISOString(),
|
|
@@ -88,7 +88,7 @@ export class Review {
|
|
|
88
88
|
await this.magi.createAgentFile(this.state.output, phase, id, content, attempt, cycle);
|
|
89
89
|
}
|
|
90
90
|
async createSessions() {
|
|
91
|
-
this.context.
|
|
91
|
+
this.context.signal.throwIfAborted();
|
|
92
92
|
if (!this.state.reviewers)
|
|
93
93
|
throw new MagiError("blocked", "Reviewers not found.");
|
|
94
94
|
if (!this.state.operator)
|
|
@@ -96,21 +96,21 @@ export class Review {
|
|
|
96
96
|
const reviewers = Object.fromEntries(await Promise.all(Object.entries(this.state.reviewers).map(async ([id, { model, permissions }]) => [
|
|
97
97
|
id,
|
|
98
98
|
{
|
|
99
|
-
sessionId: await this.magi.createSession(this.state.sessionId, `magi review #${this.number} ${id}`, { model, permissions }, this.context.
|
|
99
|
+
sessionId: await this.magi.createSession(this.state.sessionId, `magi review #${this.number} ${id}`, { model, permissions }, this.context.signal),
|
|
100
100
|
},
|
|
101
101
|
])));
|
|
102
102
|
const operator = {
|
|
103
103
|
sessionId: await this.magi.createSession(this.state.sessionId, `magi review #${this.number} operator`, {
|
|
104
104
|
model: this.state.operator.model,
|
|
105
105
|
permissions: this.state.operator.permissions,
|
|
106
|
-
}, this.context.
|
|
106
|
+
}, this.context.signal),
|
|
107
107
|
};
|
|
108
108
|
await this.updateState({ operator, reviewers });
|
|
109
109
|
}
|
|
110
110
|
async createWorktree() {
|
|
111
|
-
this.context.
|
|
111
|
+
this.context.signal.throwIfAborted();
|
|
112
112
|
await this.updateEvent(`Creating worktree.`);
|
|
113
|
-
const worktree = await this.magi.createWorktree(this.config.review.worktree, this.number, this.state.id, this.context.
|
|
113
|
+
const worktree = await this.magi.createWorktree(this.config.review.worktree, this.number, this.state.id, this.context.signal);
|
|
114
114
|
await this.updateState({ worktree });
|
|
115
115
|
await this.updateEvent(`Finished creating worktree.`);
|
|
116
116
|
}
|
|
@@ -2,7 +2,7 @@ import { MagiError } from "@/magi";
|
|
|
2
2
|
import { Prompt } from "@/prompts";
|
|
3
3
|
import { filterEmpty, marker, omitNullish, retry, toTitleCase, Worker, } from "@/utils";
|
|
4
4
|
export async function review() {
|
|
5
|
-
this.context.
|
|
5
|
+
this.context.signal.throwIfAborted();
|
|
6
6
|
await this.updateEvent(`Reviewing.`);
|
|
7
7
|
if (!this.config.review.reviewers?.length)
|
|
8
8
|
throw new MagiError("blocked", "No reviewers configured.");
|
|
@@ -95,7 +95,7 @@ export async function review() {
|
|
|
95
95
|
worktreePath: this.state.worktree.path,
|
|
96
96
|
}));
|
|
97
97
|
const output = await retry(async (count, e) => {
|
|
98
|
-
const raw = await this.magi.promptSession(sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.
|
|
98
|
+
const raw = await this.magi.promptSession(sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.signal);
|
|
99
99
|
await this.createAgentFile(rereview ? "rereview" : "review", id, raw, count, cycle);
|
|
100
100
|
const parsed = prompt.parse(raw);
|
|
101
101
|
if (!prompt.validate(parsed))
|
|
@@ -131,7 +131,7 @@ export async function review() {
|
|
|
131
131
|
await this.updateEvent(`Attempt ${count} failed to ${label} with reviewer ${id}. Retrying...`);
|
|
132
132
|
},
|
|
133
133
|
retries: this.config.output.repairAttempts,
|
|
134
|
-
signal: this.context.
|
|
134
|
+
signal: this.context.signal,
|
|
135
135
|
});
|
|
136
136
|
if (!output)
|
|
137
137
|
throw new MagiError("blocked", `Invalid output for reviewer ${id}.`);
|
|
@@ -146,7 +146,7 @@ export async function review() {
|
|
|
146
146
|
await this.updateEvent(`Finished reviewing.`);
|
|
147
147
|
}
|
|
148
148
|
export async function validateFindings() {
|
|
149
|
-
this.context.
|
|
149
|
+
this.context.signal.throwIfAborted();
|
|
150
150
|
if (!this.config.review.reviewers?.length)
|
|
151
151
|
throw new MagiError("blocked", "No reviewers configured.");
|
|
152
152
|
if (!this.state.reviewers)
|
|
@@ -170,7 +170,7 @@ export async function validateFindings() {
|
|
|
170
170
|
await this.updateEvent(`Finished validating review findings.`);
|
|
171
171
|
}
|
|
172
172
|
export async function reconsiderClose() {
|
|
173
|
-
this.context.
|
|
173
|
+
this.context.signal.throwIfAborted();
|
|
174
174
|
if (this.config.review.merge.approvalPolicy !== "unanimous")
|
|
175
175
|
return;
|
|
176
176
|
if (!this.config.review.reviewers?.length)
|
|
@@ -204,7 +204,7 @@ export async function reconsiderClose() {
|
|
|
204
204
|
});
|
|
205
205
|
await this.updateEvent(`Reconsidering close verdict with reviewer ${id}.`);
|
|
206
206
|
const output = await retry(async (count, e) => {
|
|
207
|
-
const raw = await this.magi.promptSession(sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.
|
|
207
|
+
const raw = await this.magi.promptSession(sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.signal);
|
|
208
208
|
await this.createAgentFile("close-reconsideration", id, raw, count, cycle);
|
|
209
209
|
const parsed = prompt.parse(raw);
|
|
210
210
|
if (!prompt.validate(parsed))
|
|
@@ -218,7 +218,7 @@ export async function reconsiderClose() {
|
|
|
218
218
|
await this.updateEvent(`Attempt ${count} failed to reconsider close verdict with reviewer ${id}. Retrying...`);
|
|
219
219
|
},
|
|
220
220
|
retries: this.config.output.repairAttempts,
|
|
221
|
-
signal: this.context.
|
|
221
|
+
signal: this.context.signal,
|
|
222
222
|
});
|
|
223
223
|
if (!output)
|
|
224
224
|
throw new MagiError("blocked", `Invalid close reconsideration output for reviewer ${id}.`);
|
|
@@ -270,7 +270,7 @@ async function collectAcceptedFindings(findings) {
|
|
|
270
270
|
repo: this.config.github.repo,
|
|
271
271
|
});
|
|
272
272
|
const output = await retry(async (count, e) => {
|
|
273
|
-
const raw = await this.magi.promptSession(sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.
|
|
273
|
+
const raw = await this.magi.promptSession(sessionId, count === 1 ? taskMessage : await prompt.repair(e), this.context.signal);
|
|
274
274
|
await this.createAgentFile("finding-validation", id, raw, count);
|
|
275
275
|
const parsed = prompt.parse(raw);
|
|
276
276
|
if (!prompt.validate(parsed))
|
|
@@ -297,7 +297,7 @@ async function collectAcceptedFindings(findings) {
|
|
|
297
297
|
await this.updateEvent(`Attempt ${count} failed to validate review findings with reviewer ${id}. Retrying...`);
|
|
298
298
|
},
|
|
299
299
|
retries: this.config.output.repairAttempts,
|
|
300
|
-
signal: this.context.
|
|
300
|
+
signal: this.context.signal,
|
|
301
301
|
});
|
|
302
302
|
if (!output)
|
|
303
303
|
throw new MagiError("blocked", `Invalid finding validation output for reviewer ${id}.`);
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import { tool } from "@opencode-ai/plugin";
|
|
2
1
|
export const triage = function (magi) {
|
|
3
2
|
return {
|
|
4
|
-
magi_triage:
|
|
5
|
-
args: {
|
|
6
|
-
dryRun: tool.schema.boolean().optional(),
|
|
7
|
-
issues: tool.schema.string(),
|
|
8
|
-
},
|
|
3
|
+
magi_triage: {
|
|
9
4
|
description: "Triage one or more issues with configured Magi triage voters.",
|
|
10
|
-
async execute(
|
|
5
|
+
async execute() {
|
|
11
6
|
const _config = await magi.getConfig({ creator: true, voters: true });
|
|
12
|
-
return "";
|
|
7
|
+
return { content: "" };
|
|
8
|
+
},
|
|
9
|
+
input: {
|
|
10
|
+
additionalProperties: false,
|
|
11
|
+
properties: { dryRun: { type: "boolean" }, issues: { type: "string" } },
|
|
12
|
+
required: ["issues"],
|
|
13
|
+
type: "object",
|
|
13
14
|
},
|
|
14
|
-
|
|
15
|
+
name: "magi_triage",
|
|
16
|
+
},
|
|
15
17
|
};
|
|
16
18
|
};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { tool } from "@opencode-ai/plugin";
|
|
2
1
|
import { getConfig, validateConfig } from "@/config";
|
|
3
2
|
function createReport(errors) {
|
|
4
3
|
return [
|
|
@@ -10,19 +9,20 @@ function createReport(errors) {
|
|
|
10
9
|
}
|
|
11
10
|
export const validate = function (magi) {
|
|
12
11
|
return {
|
|
13
|
-
magi_validate:
|
|
14
|
-
args: {},
|
|
12
|
+
magi_validate: {
|
|
15
13
|
description: "Validate global and project Magi config presence, merged settings, reviewer rules, model IDs, and account authentication.",
|
|
16
14
|
async execute() {
|
|
17
15
|
try {
|
|
18
16
|
const config = await getConfig(magi.input);
|
|
19
17
|
const errors = await validateConfig(config, { exec: magi.exec });
|
|
20
|
-
return createReport(errors);
|
|
18
|
+
return { content: createReport(errors) };
|
|
21
19
|
}
|
|
22
20
|
catch (e) {
|
|
23
|
-
return createReport([e.message]);
|
|
21
|
+
return { content: createReport([e.message]) };
|
|
24
22
|
}
|
|
25
23
|
},
|
|
26
|
-
|
|
24
|
+
input: { additionalProperties: false, properties: {}, type: "object" },
|
|
25
|
+
name: "magi_validate",
|
|
26
|
+
},
|
|
27
27
|
};
|
|
28
28
|
};
|
package/dist/utils/github.js
CHANGED
|
@@ -32,8 +32,11 @@ function getErrorMessage(e) {
|
|
|
32
32
|
function isGitHubCommand(command) {
|
|
33
33
|
return /(^|\s)gh\s+(api|auth|pr|run)\b/.test(command);
|
|
34
34
|
}
|
|
35
|
-
function
|
|
36
|
-
|
|
35
|
+
function isRetryableError(command, e) {
|
|
36
|
+
const message = getErrorMessage(e);
|
|
37
|
+
return (/rate limit/i.test(message) ||
|
|
38
|
+
(/(^|\s)gh\s+run\s+view\b.*\s--log-failed\b/.test(command) &&
|
|
39
|
+
/opening cache entry: zip: not a valid zip file/i.test(message)));
|
|
37
40
|
}
|
|
38
41
|
export function createExecWithGitHubApiRetry(exec, retryAttempts, delay = 1_000) {
|
|
39
42
|
return async (command, options) => {
|
|
@@ -44,7 +47,7 @@ export function createExecWithGitHubApiRetry(exec, retryAttempts, delay = 1_000)
|
|
|
44
47
|
catch (e) {
|
|
45
48
|
if (attempt >= retryAttempts ||
|
|
46
49
|
!isGitHubCommand(command) ||
|
|
47
|
-
!
|
|
50
|
+
!isRetryableError(command, e))
|
|
48
51
|
throw e;
|
|
49
52
|
const ms = delay * 2 ** attempt;
|
|
50
53
|
if (0 < ms) {
|
package/dist/utils/opencode.js
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
|
-
import { isArray } from "./assertion";
|
|
2
1
|
import { createExec } from "./exec";
|
|
3
2
|
export async function getModels(input) {
|
|
4
|
-
const
|
|
5
|
-
.
|
|
6
|
-
.
|
|
7
|
-
|
|
8
|
-
const all = data && "providers" in data ? data.providers : data?.all;
|
|
9
|
-
const models = isArray(all)
|
|
10
|
-
? all.flatMap((provider) => {
|
|
11
|
-
const models = Object.keys(provider.models);
|
|
12
|
-
return provider.id ? models.map((id) => `${provider.id}/${id}`) : models;
|
|
13
|
-
})
|
|
14
|
-
: [];
|
|
3
|
+
const models = await input.client.model
|
|
4
|
+
.list({ location: { directory: input.directory } })
|
|
5
|
+
.then(({ data }) => data.map(({ id, providerID }) => `${providerID}/${id}`))
|
|
6
|
+
.catch(() => []);
|
|
15
7
|
if (models.length)
|
|
16
8
|
return models;
|
|
17
9
|
else
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-magi",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-20261027030404",
|
|
4
4
|
"description": "Multi-agent PR review and merge orchestration plugin for OpenCode.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Hirotomo Yamada <hirotomo.yamada@avap.co.jp>",
|
|
@@ -28,40 +28,39 @@
|
|
|
28
28
|
"schema.json"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@opencode
|
|
32
|
-
"@opencode-ai/sdk": "^1.18.4",
|
|
31
|
+
"@opencode/plugin": "2.0.29",
|
|
33
32
|
"ajv": "^8.20.0",
|
|
34
33
|
"graphql": "^17.0.2",
|
|
35
34
|
"graphql-tag": "^2.12.7",
|
|
36
35
|
"octokit": "^5.0.5",
|
|
37
|
-
"picomatch": "^4.0.
|
|
38
|
-
"valibot": "^1.
|
|
36
|
+
"picomatch": "^4.0.7",
|
|
37
|
+
"valibot": "^1.5.0"
|
|
39
38
|
},
|
|
40
39
|
"devDependencies": {
|
|
41
|
-
"@changesets/changelog-github": "^0.
|
|
42
|
-
"@changesets/cli": "^
|
|
43
|
-
"@commitlint/cli": "^21.2.
|
|
44
|
-
"@commitlint/config-conventional": "^21.2.
|
|
45
|
-
"@graphql-codegen/cli": "^7.
|
|
40
|
+
"@changesets/changelog-github": "^1.0.1",
|
|
41
|
+
"@changesets/cli": "^3.0.3",
|
|
42
|
+
"@commitlint/cli": "^21.2.4",
|
|
43
|
+
"@commitlint/config-conventional": "^21.2.4",
|
|
44
|
+
"@graphql-codegen/cli": "^7.4.6",
|
|
46
45
|
"@graphql-codegen/typescript-generic-sdk": "^5.0.1",
|
|
47
|
-
"@graphql-codegen/typescript-operations": "^6.1.
|
|
46
|
+
"@graphql-codegen/typescript-operations": "^6.1.10",
|
|
48
47
|
"@limegrass/eslint-plugin-import-alias": "^1.6.1",
|
|
49
48
|
"@octokit/graphql-schema": "^15.26.1",
|
|
50
|
-
"@octokit/plugin-throttling": "^11.0.
|
|
49
|
+
"@octokit/plugin-throttling": "^11.0.5",
|
|
51
50
|
"@stylistic/eslint-plugin": "^5.10.0",
|
|
52
|
-
"@types/node": "^26.
|
|
51
|
+
"@types/node": "^26.6.5",
|
|
53
52
|
"@types/picomatch": "^4.0.3",
|
|
54
53
|
"@typescript/native-preview": "7.0.0-dev.20260707.2",
|
|
55
|
-
"@vitest/coverage-v8": "^
|
|
56
|
-
"@vitest/ui": "^
|
|
57
|
-
"eslint-plugin-perfectionist": "^5.
|
|
54
|
+
"@vitest/coverage-v8": "^5.0.5",
|
|
55
|
+
"@vitest/ui": "^5.0.5",
|
|
56
|
+
"eslint-plugin-perfectionist": "^5.13.0",
|
|
58
57
|
"eslint-plugin-unused-imports": "^4.4.1",
|
|
59
|
-
"lefthook": "^2.
|
|
60
|
-
"oxfmt": "^0.
|
|
61
|
-
"oxlint": "^1.
|
|
62
|
-
"oxlint-tsgolint": "^7.0.
|
|
58
|
+
"lefthook": "^2.2.2",
|
|
59
|
+
"oxfmt": "^0.74.0",
|
|
60
|
+
"oxlint": "^1.89.0",
|
|
61
|
+
"oxlint-tsgolint": "^7.0.2004",
|
|
63
62
|
"rimraf": "^6.1.3",
|
|
64
|
-
"vitest": "^
|
|
63
|
+
"vitest": "^5.0.5"
|
|
65
64
|
},
|
|
66
65
|
"scripts": {
|
|
67
66
|
"clean": "rimraf node_modules dist coverage",
|
package/schema.json
CHANGED
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"type": "integer",
|
|
38
38
|
"minimum": 0,
|
|
39
39
|
"default": 3,
|
|
40
|
-
"description": "Number of retry attempts for GitHub CLI
|
|
40
|
+
"description": "Number of retry attempts for GitHub CLI rate limit errors and invalid ZIP cache errors when fetching failed run logs."
|
|
41
41
|
},
|
|
42
42
|
"owner": { "type": "string", "minLength": 1 },
|
|
43
43
|
"repo": { "type": "string", "minLength": 1 },
|