@mergeline/agent 0.6.0 → 0.7.0
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 +53 -0
- package/dist/cli.js +6 -2
- package/dist/engines.js +5 -1
- package/dist/mcp.js +1 -1
- package/dist/repo.js +3 -1
- package/dist/review.js +407 -0
- package/dist/service.js +13 -0
- package/dist/worker.js +473 -15
- package/engine/prompt.md +3 -0
- package/engine/review.md +93 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -232,6 +232,59 @@ The run is released with nothing pushed, the log names the keys added,
|
|
|
232
232
|
removed or changed (never their values), and the worktree is left for you
|
|
233
233
|
to inspect.
|
|
234
234
|
|
|
235
|
+
#### The reviewer: the same worker with a reviewer's key
|
|
236
|
+
|
|
237
|
+
An agent with the role `reviewer` runs the same command. Its worker claims
|
|
238
|
+
the board's review runs (a builder's PR waiting in Agent review), never a
|
|
239
|
+
ticket of its own:
|
|
240
|
+
|
|
241
|
+
```sh
|
|
242
|
+
board watch --agent rv-1 --engine codex --follow
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Every 0.7.0 worker asks the board for all three kinds of run (implement,
|
|
246
|
+
rework, review); which one it gets decides what it does. A builder's
|
|
247
|
+
rework run (after the reviewer's or a person's return) is worked as any
|
|
248
|
+
other run, and its prompt says the run is done when each point of the
|
|
249
|
+
note is fixed or answered.
|
|
250
|
+
|
|
251
|
+
For each review run the worker fetches `origin` and reviews the tip of the
|
|
252
|
+
ticket's branch there, in a fresh detached worktree at
|
|
253
|
+
`../.wt/<ticket>-review`. The engine gets `.mergeline-run/ticket.md` (the
|
|
254
|
+
spec from `origin/main`, the answer and notes, the round, the PR, the diff
|
|
255
|
+
to read and the earlier findings), `.mergeline-run/rules.md` and the
|
|
256
|
+
package's review prompt, `engine/review.md`, and leaves
|
|
257
|
+
`.mergeline-run/review.json`: a verdict, a summary, a risk and findings. A
|
|
258
|
+
finding blocks only when the PR introduced it, it breaks the main flow,
|
|
259
|
+
loses data, is a security issue, a red gate, a broken rule or a missed
|
|
260
|
+
requirement, and it comes with proof; nitpicks are not reported.
|
|
261
|
+
|
|
262
|
+
`rules.md` is the project's rules for reviews: the file that
|
|
263
|
+
`.mergeline.json`'s `review` key names (`"review": "docs/review-rules.md"`,
|
|
264
|
+
added by hand; `board init` does not write it). The worker reads it from
|
|
265
|
+
`origin/main`, never from the PR's branch, so a PR cannot change the rules
|
|
266
|
+
it is judged by. With no key or no file, the engine reviews by the prompt
|
|
267
|
+
alone.
|
|
268
|
+
|
|
269
|
+
After the engine, the worker checks that neither the worktree's `HEAD` nor
|
|
270
|
+
the branch on origin moved; if one did, it sends nothing and releases the
|
|
271
|
+
run. It runs the repository's `test` command (from `origin/main`) as a
|
|
272
|
+
gate, and a failing one adds a blocking finding, "The test command fails",
|
|
273
|
+
with its output as proof. A run with no `review.json`, or a malformed one,
|
|
274
|
+
is released, and the board queues the review again (three tries, then the
|
|
275
|
+
PR comes to a person marked not reviewed). The worktree is always removed:
|
|
276
|
+
what the engine changed there is never kept or pushed.
|
|
277
|
+
|
|
278
|
+
When `gh` is installed and logged in, the worker reads the PR's checks
|
|
279
|
+
(as gates) and posts the review to the PR as one review comment, with a
|
|
280
|
+
line comment for each finding on a line of the diff, **as the person who
|
|
281
|
+
runs the worker**: it is `gh`'s login on that machine, not a bot. It
|
|
282
|
+
always posts with `event: COMMENT`: it never approves and never requests
|
|
283
|
+
changes, so it never blocks a merge. The links go to the board with the
|
|
284
|
+
review. Without `gh`, or when GitHub refuses (403, 422), the worker logs
|
|
285
|
+
one line and the review goes to the board without them. The reviewer's
|
|
286
|
+
worker never commits, pushes, opens a PR or merges.
|
|
287
|
+
|
|
235
288
|
### Any other tool: `board watch -- <command>`
|
|
236
289
|
|
|
237
290
|
The command form runs any tool whenever the agent's queue has work (on
|
package/dist/cli.js
CHANGED
|
@@ -28,7 +28,7 @@ import { join, resolve } from "node:path";
|
|
|
28
28
|
import { init } from "./init.js";
|
|
29
29
|
import { runMcpServer } from "./mcp.js";
|
|
30
30
|
import { watch } from "./watch.js";
|
|
31
|
-
import { enginePrompt, ENGINES } from "./engines.js";
|
|
31
|
+
import { enginePrompt, ENGINES, reviewPrompt } from "./engines.js";
|
|
32
32
|
import { mainCheckout, work } from "./worker.js";
|
|
33
33
|
import { forgetAgent, pickSaved, saveAgent, savedAgents } from "./credentials.js";
|
|
34
34
|
import { agentCredentialsFromEnv } from "./pb.js";
|
|
@@ -74,7 +74,10 @@ Usage:
|
|
|
74
74
|
claude, codex. --timeout: the most one engine run may take (default
|
|
75
75
|
90). What follows -- is added to the engine's command line.
|
|
76
76
|
Stops when the queue is empty (unless --follow), after
|
|
77
|
-
--max-runs tickets, or after 3 failed runs in a row
|
|
77
|
+
--max-runs tickets, or after 3 failed runs in a row.
|
|
78
|
+
With a reviewer's key it reviews PRs instead: the engine
|
|
79
|
+
reviews in ../.wt/<ticket>-review, the worker sends the
|
|
80
|
+
review to the board and, with gh, comments on the PR
|
|
78
81
|
board whoami check the connection and show the agent
|
|
79
82
|
board list tickets assigned to you that are not done
|
|
80
83
|
board claim take your next ticket (or {"ticket": null})
|
|
@@ -376,6 +379,7 @@ async function main(argvIn) {
|
|
|
376
379
|
engine,
|
|
377
380
|
engineArgs: after,
|
|
378
381
|
prompt: enginePrompt(),
|
|
382
|
+
reviewPrompt: reviewPrompt(),
|
|
379
383
|
repo: checkout.repo,
|
|
380
384
|
// BOARD_WATCH_*: for tests only (not in board help).
|
|
381
385
|
timeoutSec: envSec("BOARD_WATCH_TIMEOUT_SEC") ?? num("--timeout", 90) * 60,
|
package/dist/engines.js
CHANGED
|
@@ -7,10 +7,14 @@ import { readFileSync } from "node:fs";
|
|
|
7
7
|
import { join } from "node:path";
|
|
8
8
|
import { PACKAGE_NAME } from "./init.js";
|
|
9
9
|
import { errorFromClaudeJson, errorFromCodexJson, usageFromClaudeJson, usageFromCodexJson } from "./watch.js";
|
|
10
|
-
/** The prompt every engine gets (agent/engine/prompt.md). */
|
|
10
|
+
/** The prompt every engine gets on a builder's run (agent/engine/prompt.md). */
|
|
11
11
|
export function enginePrompt() {
|
|
12
12
|
return readFileSync(new URL("../engine/prompt.md", import.meta.url), "utf8");
|
|
13
13
|
}
|
|
14
|
+
/** The prompt an engine gets on a review run (agent/engine/review.md). */
|
|
15
|
+
export function reviewPrompt() {
|
|
16
|
+
return readFileSync(new URL("../engine/review.md", import.meta.url), "utf8");
|
|
17
|
+
}
|
|
14
18
|
/**
|
|
15
19
|
* Allow rules of the kit the engine must not get: everything that reaches
|
|
16
20
|
* the board, origin or GitHub is the worker's.
|
package/dist/mcp.js
CHANGED
|
@@ -91,7 +91,7 @@ export function createServer(board) {
|
|
|
91
91
|
"question block and comes back to you with the answer on a later claim.",
|
|
92
92
|
inputSchema: {
|
|
93
93
|
ticket_id: ticketId,
|
|
94
|
-
question: z.string().min(1).max(
|
|
94
|
+
question: z.string().min(1).max(5000).describe("One concrete question a human can answer in a sentence"),
|
|
95
95
|
},
|
|
96
96
|
}, ({ ticket_id, question }) => run(() => board.askHuman(ticket_id, question)));
|
|
97
97
|
server.registerTool("submit_for_review", {
|
package/dist/repo.js
CHANGED
|
@@ -50,7 +50,7 @@ export function readRepoFile(file) {
|
|
|
50
50
|
catch {
|
|
51
51
|
throw new Error(`${file} is not valid JSON; run board init --url <board> to write it again`);
|
|
52
52
|
}
|
|
53
|
-
const { board, kit, test } = (data ?? {});
|
|
53
|
+
const { board, kit, test, review } = (data ?? {});
|
|
54
54
|
const out = {};
|
|
55
55
|
if (board !== undefined) {
|
|
56
56
|
if (typeof board !== "string" || !board)
|
|
@@ -66,6 +66,8 @@ export function readRepoFile(file) {
|
|
|
66
66
|
out.kit = kit;
|
|
67
67
|
if (typeof test === "string" && test.trim())
|
|
68
68
|
out.test = test.trim();
|
|
69
|
+
if (typeof review === "string" && review.trim())
|
|
70
|
+
out.review = review.trim();
|
|
69
71
|
return out;
|
|
70
72
|
}
|
|
71
73
|
/** The kit version of the repository around cwd, or undefined (no file, no version, or a broken file). */
|
package/dist/review.js
ADDED
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
// The reviewer mode of the worker (round 9, R6: D5, D7, D12): the parts that
|
|
2
|
+
// are plain data. worker.ts runs the review run itself: the head, the
|
|
3
|
+
// worktree, the engine, the checks after it, GitHub and the board.
|
|
4
|
+
//
|
|
5
|
+
// What the engine leaves (.mergeline-run/review.json) is untrusted text: it is
|
|
6
|
+
// read like result.json, checked field by field, and cut to the board's
|
|
7
|
+
// limits with the worker's own mark before anything is sent.
|
|
8
|
+
import { lstatSync, readFileSync } from "node:fs";
|
|
9
|
+
export const REVIEW_MAX = 256 * 1024;
|
|
10
|
+
/** The board's limits on a review (R5's route): what is over them is cut, never refused. */
|
|
11
|
+
export const LIMITS = {
|
|
12
|
+
findings: 50,
|
|
13
|
+
gates: 20,
|
|
14
|
+
title: 200,
|
|
15
|
+
text: 5000,
|
|
16
|
+
proof: 2000,
|
|
17
|
+
summary: 5000,
|
|
18
|
+
short: 500,
|
|
19
|
+
path: 300,
|
|
20
|
+
ref: 60,
|
|
21
|
+
};
|
|
22
|
+
const VERDICTS = ["approve", "approve_with_comments", "changes"];
|
|
23
|
+
const SEVERITIES = ["blocking", "non_blocking"];
|
|
24
|
+
const STATUSES = ["open", "fixed", "dismissed"];
|
|
25
|
+
const RISKS = ["low", "medium", "high"];
|
|
26
|
+
/** Over max: the start of s and the worker's mark, all within max (R23); s trimmed first, as the board does. */
|
|
27
|
+
export function cut(s, max) {
|
|
28
|
+
s = s.trim();
|
|
29
|
+
if (s.length <= max)
|
|
30
|
+
return s;
|
|
31
|
+
const mark = (n) => ` … [cut by the worker: ${n} more characters]`;
|
|
32
|
+
let keep = max;
|
|
33
|
+
for (let i = 0; i < 3; i++)
|
|
34
|
+
keep = max - mark(s.length - keep).length;
|
|
35
|
+
if (keep <= 0)
|
|
36
|
+
return s.slice(0, max);
|
|
37
|
+
const c = s.charCodeAt(keep - 1);
|
|
38
|
+
if (c >= 0xd800 && c <= 0xdbff)
|
|
39
|
+
keep--;
|
|
40
|
+
return s.slice(0, keep) + mark(s.length - keep);
|
|
41
|
+
}
|
|
42
|
+
/** A path in the repository as the board takes it: relative, no "..", no scheme, no control characters. */
|
|
43
|
+
export function badPath(path) {
|
|
44
|
+
return /^[/\\]/.test(path) || path.includes("..") || /^[A-Za-z][A-Za-z0-9+.-]*:/.test(path) || /[\u0000-\u001f\u007f]/.test(path);
|
|
45
|
+
}
|
|
46
|
+
const isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
47
|
+
const absent = (v) => v === undefined || v === null;
|
|
48
|
+
/**
|
|
49
|
+
* Reads .mergeline-run/review.json as readResult reads result.json: at most 256 KB, one JSON object, a verdict
|
|
50
|
+
* from the list, a summary, an optional risk, and findings of the right shape. Anything else is no review, with why.
|
|
51
|
+
*/
|
|
52
|
+
export function readReview(file) {
|
|
53
|
+
let st;
|
|
54
|
+
try {
|
|
55
|
+
st = lstatSync(file);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return { ok: false, why: "no review.json" };
|
|
59
|
+
}
|
|
60
|
+
if (!st.isFile())
|
|
61
|
+
return { ok: false, why: "review.json is not a regular file" };
|
|
62
|
+
if (st.size > REVIEW_MAX)
|
|
63
|
+
return { ok: false, why: "review.json is over 256 KB" };
|
|
64
|
+
let data;
|
|
65
|
+
try {
|
|
66
|
+
data = JSON.parse(readFileSync(file, "utf8"));
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return { ok: false, why: "review.json is not valid JSON" };
|
|
70
|
+
}
|
|
71
|
+
return checkReview(data);
|
|
72
|
+
}
|
|
73
|
+
/** The checks of readReview on parsed JSON. */
|
|
74
|
+
export function checkReview(data) {
|
|
75
|
+
const no = (why) => ({ ok: false, why });
|
|
76
|
+
if (!isObject(data))
|
|
77
|
+
return no("review.json is not a JSON object");
|
|
78
|
+
const verdict = data.verdict;
|
|
79
|
+
if (typeof verdict !== "string" || !VERDICTS.includes(verdict)) {
|
|
80
|
+
return no(`"verdict" must be one of ${VERDICTS.join(", ")} (got ${JSON.stringify(verdict ?? null)})`);
|
|
81
|
+
}
|
|
82
|
+
if (typeof data.summary !== "string" || !data.summary.trim())
|
|
83
|
+
return no('"summary" must be a non-empty string');
|
|
84
|
+
let risk;
|
|
85
|
+
if (!absent(data.risk)) {
|
|
86
|
+
const r = data.risk;
|
|
87
|
+
if (!isObject(r) || typeof r.level !== "string" || !RISKS.includes(r.level)) {
|
|
88
|
+
return no('"risk" must be {"level": "low" | "medium" | "high", "why": "..."}');
|
|
89
|
+
}
|
|
90
|
+
if (!absent(r.why) && typeof r.why !== "string")
|
|
91
|
+
return no('"risk.why" must be a string');
|
|
92
|
+
risk = { level: r.level, why: typeof r.why === "string" ? r.why : "" };
|
|
93
|
+
}
|
|
94
|
+
if (!Array.isArray(data.findings))
|
|
95
|
+
return no('"findings" must be a list (empty when there are none)');
|
|
96
|
+
const findings = [];
|
|
97
|
+
for (const [i, f] of data.findings.entries()) {
|
|
98
|
+
const at = `findings[${i}]`;
|
|
99
|
+
if (!isObject(f))
|
|
100
|
+
return no(`${at} is not an object`);
|
|
101
|
+
if (!absent(f.ref) && typeof f.ref !== "string" && !(typeof f.ref === "number" && Number.isInteger(f.ref))) {
|
|
102
|
+
return no(`${at}.ref must be a string`);
|
|
103
|
+
}
|
|
104
|
+
if (typeof f.severity !== "string" || !SEVERITIES.includes(f.severity)) {
|
|
105
|
+
return no(`${at}.severity must be blocking or non_blocking`);
|
|
106
|
+
}
|
|
107
|
+
if (typeof f.status !== "string" || !STATUSES.includes(f.status)) {
|
|
108
|
+
return no(`${at}.status must be open, fixed or dismissed`);
|
|
109
|
+
}
|
|
110
|
+
let path;
|
|
111
|
+
if (!absent(f.path)) {
|
|
112
|
+
if (typeof f.path !== "string")
|
|
113
|
+
return no(`${at}.path must be a string`);
|
|
114
|
+
path = f.path.trim().replace(/^\.\//, "");
|
|
115
|
+
if (path && badPath(path))
|
|
116
|
+
return no(`${at}.path must be a path in the repository: relative, no "..", no scheme`);
|
|
117
|
+
// The board refuses a path over its limit: the finding goes without a place instead.
|
|
118
|
+
if (!path || path.length > LIMITS.path)
|
|
119
|
+
path = undefined;
|
|
120
|
+
}
|
|
121
|
+
let line;
|
|
122
|
+
if (!absent(f.line)) {
|
|
123
|
+
if (typeof f.line !== "number" || !Number.isInteger(f.line) || f.line < 1)
|
|
124
|
+
return no(`${at}.line must be a positive whole number`);
|
|
125
|
+
line = f.line;
|
|
126
|
+
}
|
|
127
|
+
if (typeof f.title !== "string" || !f.title.trim())
|
|
128
|
+
return no(`${at}.title must be a non-empty string`);
|
|
129
|
+
for (const k of ["text", "proof"]) {
|
|
130
|
+
if (!absent(f[k]) && typeof f[k] !== "string")
|
|
131
|
+
return no(`${at}.${k} must be a string`);
|
|
132
|
+
}
|
|
133
|
+
findings.push({
|
|
134
|
+
ref: absent(f.ref) ? "" : String(f.ref).trim(),
|
|
135
|
+
severity: f.severity,
|
|
136
|
+
status: f.status,
|
|
137
|
+
...(path ? { path, ...(line ? { line } : {}) } : {}),
|
|
138
|
+
title: f.title,
|
|
139
|
+
text: typeof f.text === "string" ? f.text : "",
|
|
140
|
+
proof: typeof f.proof === "string" ? f.proof : "",
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return { ok: true, review: { verdict: verdict, summary: data.summary, ...(risk ? { risk } : {}), findings } };
|
|
144
|
+
}
|
|
145
|
+
/** The title of the finding the worker adds for a failing `test` command (D7: a red gate blocks). */
|
|
146
|
+
export const TEST_FINDING = "The test command fails";
|
|
147
|
+
/**
|
|
148
|
+
* The findings with the worker's own for the `test` command (R6):
|
|
149
|
+
* - it failed, and no open blocking finding with proof names the command: a blocking finding with the command and the end of
|
|
150
|
+
* its output as proof, no path (the earlier one of the worker's, when there is one, is opened again by its number);
|
|
151
|
+
* - it passed: the worker's earlier open finding for it, when the review gave it no status, is fixed.
|
|
152
|
+
*/
|
|
153
|
+
export function withTestFinding(findings, earlier, test) {
|
|
154
|
+
const mine = earlier.find((f) => f.title === TEST_FINDING && !f.path);
|
|
155
|
+
if (test.passed) {
|
|
156
|
+
if (!mine || mine.status !== "open" || findings.some((f) => f.ref === mine.ref))
|
|
157
|
+
return findings;
|
|
158
|
+
return [...findings, { ref: mine.ref, severity: "blocking", status: "fixed", title: TEST_FINDING, text: `\`${test.command}\` passes now.`, proof: "" }];
|
|
159
|
+
}
|
|
160
|
+
// Only a finding the board keeps blocking covers the red test: a new one without proof is stored non-blocking (D7);
|
|
161
|
+
// an earlier one keeps the severity it was stored with, whatever is sent with its status.
|
|
162
|
+
const stored = new Map(earlier.map((f) => [f.ref, f]));
|
|
163
|
+
const covers = (f) => {
|
|
164
|
+
if (f.status !== "open")
|
|
165
|
+
return false;
|
|
166
|
+
const old = f.ref ? stored.get(f.ref) : undefined;
|
|
167
|
+
if (old)
|
|
168
|
+
return old.severity === "blocking" && (old.ref === mine?.ref || [old.title ?? "", old.text ?? "", old.proof ?? ""].some((s) => s.includes(test.command)));
|
|
169
|
+
return f.severity === "blocking" && f.proof.trim() !== "" && [f.title, f.text, f.proof].some((s) => s.includes(test.command));
|
|
170
|
+
};
|
|
171
|
+
if (findings.some(covers))
|
|
172
|
+
return findings;
|
|
173
|
+
const added = {
|
|
174
|
+
ref: mine?.ref ?? "worker-test",
|
|
175
|
+
severity: "blocking",
|
|
176
|
+
status: "open",
|
|
177
|
+
title: TEST_FINDING,
|
|
178
|
+
text: `The repository's test command \`${test.command}\` fails on the PR's head (run by the worker after the review).`,
|
|
179
|
+
proof: `$ ${test.command}\n${test.output}`,
|
|
180
|
+
};
|
|
181
|
+
return [...findings.filter((f) => !(mine && f.ref === mine.ref)), added];
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* The findings with every ref that is not an earlier finding's number replaced by a key of the worker's that cannot
|
|
185
|
+
* look like a number (new-1, new-2, ...): the board (R5's mergeFindings) would otherwise take a sent ref that equals a
|
|
186
|
+
* number it has just given in the same review for that finding, and merge the two.
|
|
187
|
+
*/
|
|
188
|
+
export function newRefs(findings, earlier) {
|
|
189
|
+
const refs = new Set(earlier.map((f) => f.ref));
|
|
190
|
+
let n = 0;
|
|
191
|
+
return findings.map((f) => (f.ref && refs.has(f.ref) ? f : { ...f, ref: `new-${++n}` }));
|
|
192
|
+
}
|
|
193
|
+
/** The findings within the board's limits: at most 50 (blocking ones kept first), each text cut with the worker's mark. */
|
|
194
|
+
export function fitFindings(findings) {
|
|
195
|
+
const kept = findings.length <= LIMITS.findings
|
|
196
|
+
? findings
|
|
197
|
+
: [...findings.filter((f) => f.severity === "blocking"), ...findings.filter((f) => f.severity !== "blocking")].slice(0, LIMITS.findings);
|
|
198
|
+
return kept.map((f) => ({
|
|
199
|
+
...f,
|
|
200
|
+
ref: f.ref.length > LIMITS.ref ? f.ref.slice(0, LIMITS.ref) : f.ref,
|
|
201
|
+
title: cut(f.title, LIMITS.title),
|
|
202
|
+
text: cut(f.text, LIMITS.text),
|
|
203
|
+
proof: cut(f.proof, LIMITS.proof),
|
|
204
|
+
}));
|
|
205
|
+
}
|
|
206
|
+
/** A gate within the board's limits. */
|
|
207
|
+
export function fitGate(g) {
|
|
208
|
+
return { ...g, name: cut(g.name, LIMITS.title), detail: cut(g.detail, LIMITS.short) };
|
|
209
|
+
}
|
|
210
|
+
/** The end of a command's output within max characters (a gate's detail, a proof). */
|
|
211
|
+
export function tailWithin(text, max) {
|
|
212
|
+
const t = text.replace(/\s+$/, "");
|
|
213
|
+
if (t.length <= max)
|
|
214
|
+
return t;
|
|
215
|
+
const lead = "… ";
|
|
216
|
+
return lead + t.slice(-(max - lead.length));
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The number the board will give each finding (R5's mergeFindings): an earlier finding's own, else the next F<n>
|
|
220
|
+
* in the order they are sent.
|
|
221
|
+
*/
|
|
222
|
+
export function boardNumbers(earlier, findings) {
|
|
223
|
+
const refs = new Set(earlier.map((f) => f.ref));
|
|
224
|
+
let last = 0;
|
|
225
|
+
for (const f of earlier) {
|
|
226
|
+
const n = Number(String(f.ref).slice(1));
|
|
227
|
+
if (Number.isInteger(n) && n > last)
|
|
228
|
+
last = n;
|
|
229
|
+
}
|
|
230
|
+
return findings.map((f) => (f.ref && refs.has(f.ref) ? f.ref : `F${++last}`));
|
|
231
|
+
}
|
|
232
|
+
/** The PR's repository and number, from a GitHub PR URL (as the board reads it), or null. */
|
|
233
|
+
export function githubPr(url) {
|
|
234
|
+
const m = /^https:\/\/github\.com\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)\/pull\/(\d+)(?:[/?#]|$)/i.exec(url.trim());
|
|
235
|
+
return m ? { owner: m[1], repo: m[2], number: m[3] } : null;
|
|
236
|
+
}
|
|
237
|
+
/** The lines of each file a diff's hunks cover on its new side (where GitHub takes a line comment). */
|
|
238
|
+
export function diffHunks(diff) {
|
|
239
|
+
const hunks = new Map();
|
|
240
|
+
let file = null;
|
|
241
|
+
for (const line of diff.split("\n")) {
|
|
242
|
+
if (line.startsWith("+++ ")) {
|
|
243
|
+
const name = line.slice(4);
|
|
244
|
+
// A quoted name (odd characters) or a deleted file: no comments on it.
|
|
245
|
+
file = name.startsWith("b/") ? name.slice(2) : null;
|
|
246
|
+
if (file && !hunks.has(file))
|
|
247
|
+
hunks.set(file, []);
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (line.startsWith("diff --git ")) {
|
|
251
|
+
file = null;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const m = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/.exec(line);
|
|
255
|
+
if (m && file) {
|
|
256
|
+
const start = Number(m[1]);
|
|
257
|
+
const count = m[2] === undefined ? 1 : Number(m[2]);
|
|
258
|
+
if (count > 0)
|
|
259
|
+
hunks.get(file).push([start, start + count - 1]);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return hunks;
|
|
263
|
+
}
|
|
264
|
+
export function inHunks(hunks, path, line) {
|
|
265
|
+
if (!path || !line)
|
|
266
|
+
return false;
|
|
267
|
+
return (hunks.get(path) ?? []).some(([a, b]) => line >= a && line <= b);
|
|
268
|
+
}
|
|
269
|
+
const place = (f) => (f.path ? ` \`${f.path}${f.line ? `:${f.line}` : ""}\`` : "");
|
|
270
|
+
const GATE_MARK = { passed: "passed", failed: "FAILED", not_run: "not run" };
|
|
271
|
+
/** One finding as GitHub shows it: its number first, which is how its comment is found again. */
|
|
272
|
+
export function findingMarkdown(number, f) {
|
|
273
|
+
const head = `**${number}** (${f.severity === "blocking" ? "blocking" : "non-blocking"}, ${f.status})${place(f)}: ${f.title}`;
|
|
274
|
+
return [head, f.text, f.proof ? `Proof:\n\n\`\`\`\n${f.proof}\n\`\`\`` : ""].filter(Boolean).join("\n\n");
|
|
275
|
+
}
|
|
276
|
+
/** GitHub's limit on a review's body is 65,536 characters. */
|
|
277
|
+
const GITHUB_BODY_MAX = 60_000;
|
|
278
|
+
/** The PR review's body (D12): the verdict, the gates and every finding with its number. */
|
|
279
|
+
export function githubBody(o) {
|
|
280
|
+
const lines = [
|
|
281
|
+
`**Review by ${o.agent}** (Mergeline), round ${o.round}, at ${o.head.slice(0, 12)}: **${o.verdict.replace(/_/g, " ")}**`,
|
|
282
|
+
...(o.verdictNote ? [o.verdictNote] : []),
|
|
283
|
+
...(o.risk ? [`Merge risk: ${o.risk.level}${o.risk.why ? ` (${o.risk.why})` : ""}`] : []),
|
|
284
|
+
"",
|
|
285
|
+
o.summary,
|
|
286
|
+
"",
|
|
287
|
+
"**Gates**",
|
|
288
|
+
...(o.gates.length ? o.gates.map((g) => `- ${g.name}: ${GATE_MARK[g.result]} (${g.source})${g.detail ? `: ${g.detail.split("\n").at(-1)}` : ""}`) : ["- none"]),
|
|
289
|
+
"",
|
|
290
|
+
"**Findings**",
|
|
291
|
+
...(o.findings.length ? o.findings.map((f, i) => `- ${findingMarkdown(o.numbers[i], f).replace(/\n/g, "\n ")}`) : ["- none"]),
|
|
292
|
+
"",
|
|
293
|
+
"_Posted by the Mergeline worker as a comment: it never approves or requests changes._",
|
|
294
|
+
];
|
|
295
|
+
return cut(lines.join("\n"), GITHUB_BODY_MAX);
|
|
296
|
+
}
|
|
297
|
+
/** A PR's checks (`gh pr view --json statusCheckRollup`) as gates of source `ci`; a check still running is not_run. */
|
|
298
|
+
export function ciGates(json) {
|
|
299
|
+
let data;
|
|
300
|
+
try {
|
|
301
|
+
data = JSON.parse(json);
|
|
302
|
+
}
|
|
303
|
+
catch {
|
|
304
|
+
return [];
|
|
305
|
+
}
|
|
306
|
+
const list = isObject(data) && Array.isArray(data.statusCheckRollup) ? data.statusCheckRollup : [];
|
|
307
|
+
const gates = [];
|
|
308
|
+
for (const c of list) {
|
|
309
|
+
if (!isObject(c))
|
|
310
|
+
continue;
|
|
311
|
+
const name = String(c.name ?? c.context ?? "").trim();
|
|
312
|
+
if (!name)
|
|
313
|
+
continue;
|
|
314
|
+
let result;
|
|
315
|
+
let detail;
|
|
316
|
+
if (c.__typename === "StatusContext" || ("state" in c && !("conclusion" in c))) {
|
|
317
|
+
const state = String(c.state ?? "").toUpperCase();
|
|
318
|
+
result = state === "SUCCESS" ? "passed" : state === "FAILURE" || state === "ERROR" ? "failed" : "not_run";
|
|
319
|
+
detail = state.toLowerCase();
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
const status = String(c.status ?? "").toUpperCase();
|
|
323
|
+
const conclusion = String(c.conclusion ?? "").toUpperCase();
|
|
324
|
+
if (status !== "COMPLETED") {
|
|
325
|
+
result = "not_run";
|
|
326
|
+
detail = status.toLowerCase() || "pending";
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
result = ["SUCCESS", "NEUTRAL", "SKIPPED"].includes(conclusion) ? "passed" : "failed";
|
|
330
|
+
detail = conclusion.toLowerCase();
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
gates.push(fitGate({ name, result, detail, source: "ci" }));
|
|
334
|
+
}
|
|
335
|
+
return gates;
|
|
336
|
+
}
|
|
337
|
+
/** Why a `gh` call failed, in one line (D12: no gh, not logged in, a 403, a 422, anything else). */
|
|
338
|
+
export function ghFailure(r) {
|
|
339
|
+
const err = `${r.stderr}\n${r.stdout}`.replace(/\s+/g, " ").trim();
|
|
340
|
+
if (r.code === 127 && /could not start gh/.test(r.stderr))
|
|
341
|
+
return "gh is not installed";
|
|
342
|
+
if (/gh auth login|not logged in|authentication (required|failed)|HTTP 401/i.test(err))
|
|
343
|
+
return "gh is not logged in";
|
|
344
|
+
if (/HTTP 403/.test(err))
|
|
345
|
+
return `GitHub refused it (403): ${err.slice(0, 300)}`;
|
|
346
|
+
if (/HTTP 422/.test(err))
|
|
347
|
+
return `GitHub could not take it (422): ${err.slice(0, 300)}`;
|
|
348
|
+
return `gh failed (exit ${r.code})${err ? `: ${err.slice(0, 300)}` : ""}`;
|
|
349
|
+
}
|
|
350
|
+
/** The rules file for the engine (.mergeline-run/rules.md): from origin/main, or why there is none. */
|
|
351
|
+
export function rulesFile(o) {
|
|
352
|
+
if (o.content !== undefined && o.path) {
|
|
353
|
+
return [
|
|
354
|
+
"# The project's rules for reviews",
|
|
355
|
+
"",
|
|
356
|
+
`From \`${o.path}\` on origin/main (${o.sha}), read by the worker. A change to that file in the PR does not apply to this review.`,
|
|
357
|
+
"",
|
|
358
|
+
"---",
|
|
359
|
+
"",
|
|
360
|
+
o.content.replace(/\s+$/, ""),
|
|
361
|
+
"",
|
|
362
|
+
].join("\n");
|
|
363
|
+
}
|
|
364
|
+
return ["# The project's rules for reviews", "", `None: ${o.why}. Review by the prompt alone.`, ""].join("\n");
|
|
365
|
+
}
|
|
366
|
+
/** The ticket for a review run (.mergeline-run/ticket.md): the builder's ticket, then the review's round, PR, diff and earlier findings. */
|
|
367
|
+
export function reviewTicketFile(o) {
|
|
368
|
+
const { ticket: t, review } = o;
|
|
369
|
+
const notes = t.notes ?? [];
|
|
370
|
+
const earlier = review.findings.map((f) => {
|
|
371
|
+
const where = f.path ? ` ${f.path}${f.line ? `:${f.line}` : ""}` : " (no place)";
|
|
372
|
+
const text = (f.text ?? "").trim();
|
|
373
|
+
return `- ${f.ref} (${f.severity}, ${f.status})${where}: ${f.title ?? ""}${text ? `\n${text.replace(/^/gm, " ")}` : ""}`;
|
|
374
|
+
});
|
|
375
|
+
return [
|
|
376
|
+
`# ${t.title}`,
|
|
377
|
+
"",
|
|
378
|
+
`Ticket: ${o.label} (${t.id})`,
|
|
379
|
+
`Branch: ${t.branch}, reviewed at ${o.head}`,
|
|
380
|
+
`Review round: ${review.round}`,
|
|
381
|
+
`Spec: ${t.spec_path}/spec.md at ${o.sha}`,
|
|
382
|
+
"",
|
|
383
|
+
"## Spec",
|
|
384
|
+
"",
|
|
385
|
+
o.spec.replace(/\s+$/, ""),
|
|
386
|
+
"",
|
|
387
|
+
"## Answer",
|
|
388
|
+
"",
|
|
389
|
+
t.answer?.trim() ? t.answer.trim() : "None.",
|
|
390
|
+
"",
|
|
391
|
+
"## Notes, oldest first",
|
|
392
|
+
"",
|
|
393
|
+
...(notes.length ? notes.map((n) => `- ${n.kind}, ${n.at}: ${n.text}`) : ["None."]),
|
|
394
|
+
"",
|
|
395
|
+
"## The review",
|
|
396
|
+
"",
|
|
397
|
+
`- Round: ${review.round}`,
|
|
398
|
+
`- PR: ${review.pr_url || "(none)"}`,
|
|
399
|
+
`- The head reviewed (HEAD here): ${o.head}`,
|
|
400
|
+
"- The diff to read: `git diff origin/main...HEAD`",
|
|
401
|
+
"",
|
|
402
|
+
"## Earlier findings",
|
|
403
|
+
"",
|
|
404
|
+
...(earlier.length ? [...earlier, "", "Give each open one a status in review.json, by its number as `ref`."] : ["None."]),
|
|
405
|
+
"",
|
|
406
|
+
].join("\n");
|
|
407
|
+
}
|
package/dist/service.js
CHANGED
|
@@ -153,8 +153,21 @@ export class TicketService {
|
|
|
153
153
|
...(kit === undefined ? {} : { kit }),
|
|
154
154
|
...(run.runner === undefined ? {} : { runner: run.runner }),
|
|
155
155
|
...(run.engine === undefined ? {} : { engine: run.engine }),
|
|
156
|
+
...(run.runs === undefined ? {} : { runs: run.runs }),
|
|
156
157
|
});
|
|
157
158
|
}
|
|
159
|
+
/** Extends the lease of one of this agent's runs (a review run: it is not the agent's ticket). */
|
|
160
|
+
heartbeatRun(runId) {
|
|
161
|
+
return this.post("heartbeat", { run_id: runId });
|
|
162
|
+
}
|
|
163
|
+
/** Ends one of this agent's runs as released; for a review run the board queues the review again (D9). */
|
|
164
|
+
releaseRun(runId, reason, usage) {
|
|
165
|
+
return this.post("release", { run_id: runId, reason, ...(usage ? { usage } : {}) });
|
|
166
|
+
}
|
|
167
|
+
/** The review of this agent's open review run (R5). `demoted`: the blocking findings stored as non-blocking (no proof). */
|
|
168
|
+
sendReview(runId, review) {
|
|
169
|
+
return this.post("review", { run_id: runId, ...review });
|
|
170
|
+
}
|
|
158
171
|
heartbeat(ticketId, note) {
|
|
159
172
|
return this.post("heartbeat", { ticket_id: ticketId, note: note ?? "" });
|
|
160
173
|
}
|
package/dist/worker.js
CHANGED
|
@@ -12,6 +12,14 @@
|
|
|
12
12
|
// leaves is untrusted text. What this does not stop (the same OS user) is
|
|
13
13
|
// in docs/security.md.
|
|
14
14
|
//
|
|
15
|
+
// With a reviewer's key the same worker reviews (round 9, R6: D5, D12): it
|
|
16
|
+
// claims review runs, gives the engine the PR's head in a detached worktree at
|
|
17
|
+
// ../.wt/<ticket>-review with the ticket, the earlier findings and the
|
|
18
|
+
// project's rules for reviews (from origin/main), and the review prompt. The
|
|
19
|
+
// engine leaves review.json; the worker checks nothing moved, runs the `test`
|
|
20
|
+
// command as a gate, posts the review to the PR as a comment when gh works,
|
|
21
|
+
// and sends it to the board. It never commits, pushes or approves.
|
|
22
|
+
//
|
|
15
23
|
// One worker per agent and machine (the lock), one board session for its
|
|
16
24
|
// whole life. It stops when the queue is empty (unless --follow), after
|
|
17
25
|
// --max-runs claims that got a ticket, or after 3 failed runs (releases) in a
|
|
@@ -24,7 +32,9 @@ import { createHash } from "node:crypto";
|
|
|
24
32
|
import { closeSync, existsSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readlinkSync, readSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
25
33
|
import { homedir, tmpdir } from "node:os";
|
|
26
34
|
import { join, resolve } from "node:path";
|
|
27
|
-
import {
|
|
35
|
+
import { reviewPrompt } from "./engines.js";
|
|
36
|
+
import { badPath, boardNumbers, ciGates, diffHunks, findingMarkdown, fitFindings, fitGate, ghFailure, githubBody, githubPr, inHunks, LIMITS, newRefs, readReview, reviewTicketFile, rulesFile, tailWithin, TEST_FINDING, withTestFinding, } from "./review.js";
|
|
37
|
+
import { BoardError, BoardPaused, } from "./service.js";
|
|
28
38
|
import { hasWork, isUnreachable, label, sleep, takeLock, untilAnswered } from "./watch.js";
|
|
29
39
|
const FAILURE_STOP = 3;
|
|
30
40
|
const RUN_DIR = ".mergeline-run";
|
|
@@ -40,8 +50,30 @@ const OUTPUT_KEEP = 256 * 1024;
|
|
|
40
50
|
const KILL_GRACE_MS = 2000;
|
|
41
51
|
/** The board's answers to a heartbeat that mean the ticket is no longer this session's. */
|
|
42
52
|
const REFUSALS = [403, 404, 409];
|
|
53
|
+
/** What a 0.7.0 worker can do (D13): a builder gets its tickets, a reviewer its review runs. */
|
|
54
|
+
const RUN_KINDS = ["implement", "rework", "review"];
|
|
55
|
+
/** The most of the rules file the engine gets. */
|
|
56
|
+
const RULES_MAX = 256 * 1024;
|
|
43
57
|
const FENCED_VARS = ["GH_TOKEN", "GITHUB_TOKEN", "GH_ENTERPRISE_TOKEN", "GITHUB_ENTERPRISE_TOKEN", "SSH_AUTH_SOCK"];
|
|
44
58
|
const clip = (s, n) => (s.length > n ? s.slice(0, n) : s);
|
|
59
|
+
/** The board's limits on a question and a release reason (block_text's max), and on a PR summary the worker sends. */
|
|
60
|
+
const LONG_MAX = 5000;
|
|
61
|
+
const SUMMARY_MAX = 2000;
|
|
62
|
+
/** The worker's own line after an engine's question (withBefore). */
|
|
63
|
+
const WORKER_LINE_MAX = 2000;
|
|
64
|
+
/** Over max: the start of s and " … [cut by the worker: <n> more characters]", all within max (R23). */
|
|
65
|
+
export function cut(s, max) {
|
|
66
|
+
if (s.length <= max)
|
|
67
|
+
return s;
|
|
68
|
+
const mark = (n) => ` … [cut by the worker: ${n} more characters]`;
|
|
69
|
+
let keep = max;
|
|
70
|
+
for (let i = 0; i < 3; i++)
|
|
71
|
+
keep = max - mark(s.length - keep).length;
|
|
72
|
+
const c = s.charCodeAt(keep - 1);
|
|
73
|
+
if (c >= 0xd800 && c <= 0xdbff)
|
|
74
|
+
keep--;
|
|
75
|
+
return s.slice(0, keep) + mark(s.length - keep);
|
|
76
|
+
}
|
|
45
77
|
/** Reads .mergeline-run/result.json; anything but one of the three forms is no result, with why. */
|
|
46
78
|
export function readResult(file) {
|
|
47
79
|
const st = lstatOrNull(file);
|
|
@@ -62,7 +94,7 @@ export function readResult(file) {
|
|
|
62
94
|
return { outcome: "none", why: "result.json is not a JSON object" };
|
|
63
95
|
const r = data;
|
|
64
96
|
const field = { done: "summary", ask: "question", release: "reason" };
|
|
65
|
-
const max = { done:
|
|
97
|
+
const max = { done: SUMMARY_MAX, ask: LONG_MAX, release: LONG_MAX };
|
|
66
98
|
const outcome = r.outcome;
|
|
67
99
|
if (typeof outcome !== "string" || !(outcome in field)) {
|
|
68
100
|
return { outcome: "none", why: `unknown outcome ${JSON.stringify(outcome ?? null)}` };
|
|
@@ -71,7 +103,7 @@ export function readResult(file) {
|
|
|
71
103
|
const text = r[field[o]];
|
|
72
104
|
if (typeof text !== "string" || !text.trim())
|
|
73
105
|
return { outcome: "none", why: `"${field[o]}" must be a non-empty string` };
|
|
74
|
-
const value =
|
|
106
|
+
const value = cut(text.trim(), max[o]);
|
|
75
107
|
if (o === "done")
|
|
76
108
|
return { outcome: "done", summary: value };
|
|
77
109
|
if (o === "ask")
|
|
@@ -531,7 +563,7 @@ export async function work(opts) {
|
|
|
531
563
|
: null;
|
|
532
564
|
if (unsafe) {
|
|
533
565
|
try {
|
|
534
|
-
await settle(lbl, () => board.release(ticket.id, unsafe));
|
|
566
|
+
await settle(lbl, () => board.release(ticket.id, cut(unsafe, LONG_MAX)));
|
|
535
567
|
say(`released: ${unsafe}`);
|
|
536
568
|
}
|
|
537
569
|
catch (err) {
|
|
@@ -597,12 +629,14 @@ export async function work(opts) {
|
|
|
597
629
|
const release = async (reason) => {
|
|
598
630
|
if (refused)
|
|
599
631
|
throw new Refused(refused);
|
|
632
|
+
reason = cut(reason.trim(), LONG_MAX);
|
|
600
633
|
await settle(lbl, () => board.release(ticket.id, reason, usage ?? undefined));
|
|
601
634
|
say(`released: ${reason}`);
|
|
602
635
|
return "failed";
|
|
603
636
|
};
|
|
604
637
|
const ask = async (question) => {
|
|
605
638
|
held();
|
|
639
|
+
question = cut(question.trim(), LONG_MAX);
|
|
606
640
|
await settle(lbl, () => board.askHuman(ticket.id, question));
|
|
607
641
|
say(`asked: ${question.split("\n")[0]}`);
|
|
608
642
|
if (usage) {
|
|
@@ -813,7 +847,7 @@ export async function work(opts) {
|
|
|
813
847
|
const what = conflicts.length
|
|
814
848
|
? `Merging origin/${ticket.branch} (it moved on origin during the run) conflicts in: ${conflicts.join(", ")}.`
|
|
815
849
|
: `Bringing in origin/${ticket.branch} (it moved on origin during the run) failed: ${oneLine(merged.stderr || merged.stdout)}.`;
|
|
816
|
-
return await ask(withBefore(result,
|
|
850
|
+
return await ask(withBefore(result, what, lost));
|
|
817
851
|
}
|
|
818
852
|
if (result.outcome === "done") {
|
|
819
853
|
const failed = await runTest();
|
|
@@ -824,7 +858,7 @@ export async function work(opts) {
|
|
|
824
858
|
if (pushed.code === 0)
|
|
825
859
|
pushedOk = true;
|
|
826
860
|
else {
|
|
827
|
-
return await ask(withBefore(result, `The push to ${ticket.branch} was rejected twice: ${oneLine(pushed.stderr)}
|
|
861
|
+
return await ask(withBefore(result, `The push to ${ticket.branch} was rejected twice: ${oneLine(pushed.stderr)}.`, lost));
|
|
828
862
|
}
|
|
829
863
|
}
|
|
830
864
|
}
|
|
@@ -923,6 +957,385 @@ export async function work(opts) {
|
|
|
923
957
|
worktreeGit = null;
|
|
924
958
|
}
|
|
925
959
|
};
|
|
960
|
+
/**
|
|
961
|
+
* One review run (round 9, R6), from the PR's head to the review on the board. Returns whether it was a failed
|
|
962
|
+
* run. The worker never commits or pushes here: what the engine changes goes with the worktree.
|
|
963
|
+
*/
|
|
964
|
+
/** The last review run this worker opened: a 409 "already working on ticket <id>" for its ticket is released by run id. */
|
|
965
|
+
let lastReview = null;
|
|
966
|
+
const runReview = async (n, ticket, runId, claim, resumed) => {
|
|
967
|
+
lastReview = { ticket: ticket.id, run: runId };
|
|
968
|
+
const lbl = label(ticket);
|
|
969
|
+
const say = (line) => log(`run ${n}: ${lbl}: ${line}`);
|
|
970
|
+
log(`run ${n}: ${resumed ? "resumed" : "claimed"} the review of ${lbl} (${ticket.branch})`);
|
|
971
|
+
const review = {
|
|
972
|
+
pr_url: claim?.pr_url ?? ticket.pr_url ?? "",
|
|
973
|
+
round: claim?.round ?? 1,
|
|
974
|
+
findings: Array.isArray(claim?.findings) ? claim.findings : [],
|
|
975
|
+
};
|
|
976
|
+
const unsafe = !/^[a-z0-9]+$/.test(ticket.id)
|
|
977
|
+
? `the ticket id ${JSON.stringify(ticket.id)} is not a safe folder name`
|
|
978
|
+
: ticket.branch !== `agent/${ticket.id}`
|
|
979
|
+
? `the ticket's branch ${JSON.stringify(ticket.branch)} is not agent/${ticket.id}`
|
|
980
|
+
: null;
|
|
981
|
+
if (unsafe) {
|
|
982
|
+
try {
|
|
983
|
+
await settle(lbl, () => board.releaseRun(runId, cut(unsafe, LONG_MAX)));
|
|
984
|
+
say(`released: ${unsafe}`);
|
|
985
|
+
}
|
|
986
|
+
catch (err) {
|
|
987
|
+
if (err instanceof Abandoned)
|
|
988
|
+
return "failed";
|
|
989
|
+
say(`not released (${err instanceof Error ? err.message : String(err)}): ${unsafe}`);
|
|
990
|
+
}
|
|
991
|
+
return "failed";
|
|
992
|
+
}
|
|
993
|
+
/** The board said the run is no longer this session's (403, 404, 409): no other call for it. */
|
|
994
|
+
let refused = null;
|
|
995
|
+
/** The board turned a heartbeat down for another reason (a 4xx but 429): the run stops and is released. */
|
|
996
|
+
let rejected = null;
|
|
997
|
+
let current = null;
|
|
998
|
+
const beat = async () => {
|
|
999
|
+
if (refused || rejected)
|
|
1000
|
+
return;
|
|
1001
|
+
try {
|
|
1002
|
+
await board.heartbeatRun(runId);
|
|
1003
|
+
}
|
|
1004
|
+
catch (err) {
|
|
1005
|
+
const status = err instanceof BoardError && !(err instanceof BoardPaused) ? err.status : 0;
|
|
1006
|
+
if (REFUSALS.includes(status)) {
|
|
1007
|
+
refused ??= err.message;
|
|
1008
|
+
if (current)
|
|
1009
|
+
killGroup(current);
|
|
1010
|
+
}
|
|
1011
|
+
else if (status >= 400 && status < 500 && status !== 429) {
|
|
1012
|
+
rejected ??= `the board turned down a heartbeat (${status}): ${err.message}`;
|
|
1013
|
+
if (current)
|
|
1014
|
+
killGroup(current);
|
|
1015
|
+
}
|
|
1016
|
+
else {
|
|
1017
|
+
say(`heartbeat failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
};
|
|
1021
|
+
const held = () => {
|
|
1022
|
+
if (refused)
|
|
1023
|
+
throw new Refused(refused);
|
|
1024
|
+
if (rejected) {
|
|
1025
|
+
const why = rejected;
|
|
1026
|
+
rejected = null;
|
|
1027
|
+
throw new Error(why);
|
|
1028
|
+
}
|
|
1029
|
+
};
|
|
1030
|
+
const beats = setInterval(() => void beat(), opts.beatSec * 1000);
|
|
1031
|
+
const scratch = mkdtempSync(join(tmpdir(), "mergeline-review-"));
|
|
1032
|
+
const emptyDir = join(scratch, "board");
|
|
1033
|
+
const ghDir = join(scratch, "gh");
|
|
1034
|
+
mkdirSync(emptyDir, { mode: 0o700 });
|
|
1035
|
+
mkdirSync(ghDir, { mode: 0o700 });
|
|
1036
|
+
const fence = fencedEnv(process.env, emptyDir, ghDir, originUrls(repo));
|
|
1037
|
+
const path = resolve(repo, "..", ".wt", `${ticket.id}-review`);
|
|
1038
|
+
let usage = null;
|
|
1039
|
+
const release = async (reason) => {
|
|
1040
|
+
if (refused)
|
|
1041
|
+
throw new Refused(refused);
|
|
1042
|
+
reason = cut(reason.trim(), LONG_MAX);
|
|
1043
|
+
await settle(lbl, () => board.releaseRun(runId, reason, usage ?? undefined));
|
|
1044
|
+
say(`released: ${reason}`);
|
|
1045
|
+
return "failed";
|
|
1046
|
+
};
|
|
1047
|
+
try {
|
|
1048
|
+
// The head: the tip of origin/<branch> now.
|
|
1049
|
+
const fetched = await git(repo, "fetch", "origin", "main");
|
|
1050
|
+
if (fetched.code !== 0)
|
|
1051
|
+
return await release(`git fetch origin main failed: ${oneLine(fetched.stderr)}`);
|
|
1052
|
+
const sha = (await git(repo, "rev-parse", "origin/main")).stdout.trim();
|
|
1053
|
+
const listed = await git(repo, "ls-remote", "--heads", "origin", ticket.branch);
|
|
1054
|
+
if (listed.code !== 0)
|
|
1055
|
+
return await release(`git ls-remote origin failed: ${oneLine(listed.stderr)}`);
|
|
1056
|
+
if (!listed.stdout.trim())
|
|
1057
|
+
return await release("the PR's branch is not on origin");
|
|
1058
|
+
const tracking = `refs/remotes/origin/${ticket.branch}`;
|
|
1059
|
+
const fetchBranch = () => git(repo, "fetch", "origin", `+refs/heads/${ticket.branch}:${tracking}`);
|
|
1060
|
+
const f = await fetchBranch();
|
|
1061
|
+
if (f.code !== 0)
|
|
1062
|
+
return await release(`git fetch origin ${ticket.branch} failed: ${oneLine(f.stderr)}`);
|
|
1063
|
+
const tipOf = async () => (await git(repo, "rev-parse", "--verify", "-q", `${tracking}^{commit}`)).stdout.trim();
|
|
1064
|
+
const head = await tipOf();
|
|
1065
|
+
if (!/^[0-9a-f]{40}$/.test(head))
|
|
1066
|
+
return await release(`could not read the tip of origin/${ticket.branch}`);
|
|
1067
|
+
const specFile = `${ticket.spec_path}/spec.md`;
|
|
1068
|
+
const spec = await git(repo, "show", `origin/main:${specFile}`);
|
|
1069
|
+
if (spec.code !== 0 || !ticket.spec_path)
|
|
1070
|
+
return await release(`there is no ${specFile} on main (${sha}) to review against`);
|
|
1071
|
+
const testCommand = await repoTest(git, repo);
|
|
1072
|
+
const rules = await repoRules(git, repo, sha);
|
|
1073
|
+
// A fresh worktree at the head, detached.
|
|
1074
|
+
await removeWorktree(path);
|
|
1075
|
+
const added = await git(repo, "worktree", "add", "--detach", path, head);
|
|
1076
|
+
if (added.code !== 0)
|
|
1077
|
+
return await release(`git worktree add failed: ${oneLine(added.stderr)}`);
|
|
1078
|
+
const runDir = join(path, RUN_DIR);
|
|
1079
|
+
mkdirSync(runDir, { recursive: true });
|
|
1080
|
+
writeFileSync(join(runDir, ".gitignore"), "*\n");
|
|
1081
|
+
writeFileSync(join(runDir, "ticket.md"), reviewTicketFile({ ticket, label: lbl, head, sha, spec: spec.stdout, review }));
|
|
1082
|
+
writeFileSync(join(runDir, "rules.md"), rules);
|
|
1083
|
+
// The engine, fenced as a builder's.
|
|
1084
|
+
const deadline = Date.now() + opts.timeoutSec * 1000;
|
|
1085
|
+
const { cmd, args } = engine.command(opts.reviewPrompt ?? reviewPrompt(), opts.engineArgs, { cwd: path, env: fence, scratch });
|
|
1086
|
+
held();
|
|
1087
|
+
if (state.stopped)
|
|
1088
|
+
return await release(`board watch was stopped (${stopSignal ?? "SIGTERM"})`);
|
|
1089
|
+
worktreeGit = join(path, ".git");
|
|
1090
|
+
configBefore = new Map(gitConfigFiles(configFile, path).map((file) => [file, { state: fileState(file), entries: file === worktreeGit ? "replaced" : configEntries(file) }]));
|
|
1091
|
+
say(`${engine.name} started the review in ../.wt/${ticket.id}-review at ${head}`);
|
|
1092
|
+
const ran = await exec(cmd, args, {
|
|
1093
|
+
cwd: path,
|
|
1094
|
+
env: fence,
|
|
1095
|
+
group: true,
|
|
1096
|
+
passStdout: true,
|
|
1097
|
+
timeoutMs: deadline - Date.now(),
|
|
1098
|
+
track: (c) => {
|
|
1099
|
+
current = c;
|
|
1100
|
+
engineChild = c;
|
|
1101
|
+
},
|
|
1102
|
+
});
|
|
1103
|
+
if (ran.code === 127 && ran.stderr.startsWith("could not start"))
|
|
1104
|
+
say(oneLine(ran.stderr));
|
|
1105
|
+
held();
|
|
1106
|
+
usage = engine.usage(ran.stdout);
|
|
1107
|
+
if (engineStopped)
|
|
1108
|
+
return await release(`board watch was stopped (${engineStopped})`);
|
|
1109
|
+
checkConfig();
|
|
1110
|
+
// Nothing may have moved: the worktree's HEAD, and the branch on origin.
|
|
1111
|
+
const now = (await git(path, "rev-parse", "--verify", "-q", "HEAD")).stdout.trim();
|
|
1112
|
+
if (now !== head) {
|
|
1113
|
+
return await release(`the engine moved HEAD during the review (${head} to ${now || "nothing"}); no review is sent, and its changes are discarded with the worktree`);
|
|
1114
|
+
}
|
|
1115
|
+
const again = await fetchBranch();
|
|
1116
|
+
if (again.code !== 0)
|
|
1117
|
+
return await release(`could not fetch origin/${ticket.branch} after the review (${oneLine(again.stderr)}); no review is sent`);
|
|
1118
|
+
const tip = await tipOf();
|
|
1119
|
+
if (tip !== head)
|
|
1120
|
+
return await release(`origin/${ticket.branch} moved during the review (${head} to ${tip || "nothing"}); no review is sent`);
|
|
1121
|
+
const read = ran.killed ? { ok: false, why: `the engine ran past the timeout (${opts.timeoutSec}s) and was stopped` } : readReview(join(runDir, "review.json"));
|
|
1122
|
+
if (!read.ok) {
|
|
1123
|
+
say(`no review from the engine: ${read.why}`);
|
|
1124
|
+
const error = engine.error(ran.stdout);
|
|
1125
|
+
return await release(`the engine ended without a review (exit ${ran.code}): ${read.why}${error ? `; ${error}` : ""}`);
|
|
1126
|
+
}
|
|
1127
|
+
const result = read.review;
|
|
1128
|
+
// The gates: the repository's test command, then the PR's checks.
|
|
1129
|
+
const gates = [];
|
|
1130
|
+
let findings = result.findings;
|
|
1131
|
+
if (testCommand) {
|
|
1132
|
+
held();
|
|
1133
|
+
// The gate is on the PR's code, not the engine's edits: tracked files back to HEAD, untracked ones git does
|
|
1134
|
+
// not ignore removed (without -x, so the installed dependencies and .mergeline-run stay).
|
|
1135
|
+
const restored = await git(path, "checkout", "--", ".");
|
|
1136
|
+
const cleaned = restored.code === 0 ? await git(path, "clean", "-fdq") : restored;
|
|
1137
|
+
if (cleaned.code !== 0)
|
|
1138
|
+
return await release(`could not put the worktree back to the head before the test command: ${oneLine(cleaned.stderr)}; no review is sent`);
|
|
1139
|
+
checkConfig();
|
|
1140
|
+
say(`running the test command: ${testCommand}`);
|
|
1141
|
+
const t = await exec("sh", ["-c", testCommand], {
|
|
1142
|
+
cwd: path,
|
|
1143
|
+
env: fence,
|
|
1144
|
+
group: true,
|
|
1145
|
+
timeoutMs: deadline - Date.now(),
|
|
1146
|
+
track: (c) => (current = c),
|
|
1147
|
+
});
|
|
1148
|
+
held();
|
|
1149
|
+
checkConfig();
|
|
1150
|
+
const passed = t.code === 0 && !t.killed;
|
|
1151
|
+
const end = tail(t.output);
|
|
1152
|
+
gates.push(fitGate({
|
|
1153
|
+
name: testCommand,
|
|
1154
|
+
result: t.killed ? "not_run" : passed ? "passed" : "failed",
|
|
1155
|
+
detail: t.killed ? "stopped: it ran past the worker's timeout" : tailWithin(passed ? end : `exit ${t.code}\n${end}`, LIMITS.short),
|
|
1156
|
+
source: "worker",
|
|
1157
|
+
}));
|
|
1158
|
+
if (!t.killed) {
|
|
1159
|
+
findings = withTestFinding(findings, review.findings, {
|
|
1160
|
+
command: testCommand,
|
|
1161
|
+
passed,
|
|
1162
|
+
output: tailWithin(`exit ${t.code}\n${end}`, LIMITS.proof - testCommand.length - 3),
|
|
1163
|
+
});
|
|
1164
|
+
}
|
|
1165
|
+
if (!passed)
|
|
1166
|
+
say(`the test command ${t.killed ? "ran past the timeout" : `failed (exit ${t.code})`}`);
|
|
1167
|
+
}
|
|
1168
|
+
// The worker's own blocking finding for a red test makes the verdict "changes", whatever the engine wrote.
|
|
1169
|
+
const ownTest = findings.find((x) => x.title === TEST_FINDING && x.status === "open" && x.severity === "blocking" && !result.findings.includes(x));
|
|
1170
|
+
const verdict = ownTest ? "changes" : result.verdict;
|
|
1171
|
+
const sendable = fitFindings(newRefs(findings, review.findings));
|
|
1172
|
+
const numbers = boardNumbers(review.findings, sendable);
|
|
1173
|
+
const ownNumber = ownTest ? numbers[sendable.findIndex((x) => x.title === TEST_FINDING && x.status === "open" && !x.path)] : undefined;
|
|
1174
|
+
const verdictNote = ownTest && result.verdict !== "changes"
|
|
1175
|
+
? `The verdict is changes, not the reviewer's ${result.verdict.replace(/_/g, " ")}: the worker added ${ownNumber ?? "a finding"}, the test command fails.`
|
|
1176
|
+
: undefined;
|
|
1177
|
+
// GitHub (D12), as the person who runs the worker: the checks, then one COMMENT review.
|
|
1178
|
+
const pr = githubPr(review.pr_url);
|
|
1179
|
+
let prReviewUrl = "";
|
|
1180
|
+
if (!pr) {
|
|
1181
|
+
say(`not posted to the PR: ${review.pr_url ? `${review.pr_url} is not a GitHub PR` : "the ticket has no PR"}`);
|
|
1182
|
+
}
|
|
1183
|
+
else {
|
|
1184
|
+
const ghEnv = withoutBoard(process.env);
|
|
1185
|
+
const gh = (...a) => {
|
|
1186
|
+
checkConfig();
|
|
1187
|
+
return exec("gh", a, { cwd: repo, env: ghEnv, group: true });
|
|
1188
|
+
};
|
|
1189
|
+
const prPath = `repos/${pr.owner}/${pr.repo}/pulls/${pr.number}`;
|
|
1190
|
+
const posted = await (async () => {
|
|
1191
|
+
const checks = await gh("pr", "view", review.pr_url, "--json", "statusCheckRollup");
|
|
1192
|
+
if (checks.code !== 0)
|
|
1193
|
+
return ghFailure(checks);
|
|
1194
|
+
gates.push(...ciGates(checks.stdout));
|
|
1195
|
+
const diff = await git(path, "diff", "--no-color", "--no-ext-diff", "--no-textconv", "--unified=3", "origin/main...HEAD");
|
|
1196
|
+
if (diff.code !== 0)
|
|
1197
|
+
return `git diff failed: ${oneLine(diff.stderr)}`;
|
|
1198
|
+
const hunks = diffHunks(diff.stdout);
|
|
1199
|
+
const comments = sendable.flatMap((x, i) => inHunks(hunks, x.path, x.line) ? [{ path: x.path, line: x.line, side: "RIGHT", body: findingMarkdown(numbers[i], x) }] : []);
|
|
1200
|
+
const body = githubBody({
|
|
1201
|
+
agent: board.agentName,
|
|
1202
|
+
round: review.round,
|
|
1203
|
+
head,
|
|
1204
|
+
verdict,
|
|
1205
|
+
verdictNote,
|
|
1206
|
+
risk: result.risk,
|
|
1207
|
+
summary: result.summary.trim(),
|
|
1208
|
+
gates: gates.slice(0, LIMITS.gates),
|
|
1209
|
+
findings: sendable,
|
|
1210
|
+
numbers,
|
|
1211
|
+
});
|
|
1212
|
+
const input = join(scratch, "pr-review.json");
|
|
1213
|
+
const post = (withComments) => {
|
|
1214
|
+
writeFileSync(input, JSON.stringify({ commit_id: head, event: "COMMENT", body, comments: withComments }));
|
|
1215
|
+
return gh("api", "--method", "POST", `${prPath}/reviews`, "--input", input);
|
|
1216
|
+
};
|
|
1217
|
+
let made = await post(comments);
|
|
1218
|
+
// A 422 with line comments: a hunk GitHub sees differently. Once more as the body only, so the review is on the PR.
|
|
1219
|
+
if (made.code !== 0 && comments.length && /HTTP 422/.test(`${made.stderr}\n${made.stdout}`)) {
|
|
1220
|
+
say(`gh: ${ghFailure(made)}; posting the review again without line comments`);
|
|
1221
|
+
comments.length = 0;
|
|
1222
|
+
made = await post(comments);
|
|
1223
|
+
}
|
|
1224
|
+
if (made.code !== 0)
|
|
1225
|
+
return ghFailure(made);
|
|
1226
|
+
let id = 0;
|
|
1227
|
+
try {
|
|
1228
|
+
const r = JSON.parse(made.stdout);
|
|
1229
|
+
id = typeof r.id === "number" ? r.id : 0;
|
|
1230
|
+
if (typeof r.html_url === "string" && prLink(r.html_url, pr, /#pullrequestreview-\d+$/))
|
|
1231
|
+
prReviewUrl = r.html_url;
|
|
1232
|
+
}
|
|
1233
|
+
catch {
|
|
1234
|
+
// posted, but no answer to read links from
|
|
1235
|
+
}
|
|
1236
|
+
say(`posted the review to the PR${prReviewUrl ? `: ${prReviewUrl}` : ""}, with ${comments.length} line comment(s)`);
|
|
1237
|
+
if (!id || !comments.length)
|
|
1238
|
+
return null;
|
|
1239
|
+
const listed = await gh("api", `${prPath}/reviews/${id}/comments?per_page=100`);
|
|
1240
|
+
if (listed.code !== 0)
|
|
1241
|
+
return `the comments' links are not read: ${ghFailure(listed)}`;
|
|
1242
|
+
try {
|
|
1243
|
+
const urls = new Map();
|
|
1244
|
+
for (const c of JSON.parse(listed.stdout)) {
|
|
1245
|
+
const m = typeof c.body === "string" ? /^\*\*(F\d+)\*\*/.exec(c.body) : null;
|
|
1246
|
+
if (m && typeof c.html_url === "string" && prLink(c.html_url, pr, /#(?:discussion_r|r)\d+$/))
|
|
1247
|
+
urls.set(m[1], c.html_url);
|
|
1248
|
+
}
|
|
1249
|
+
sendable.forEach((x, i) => {
|
|
1250
|
+
const url = urls.get(numbers[i]);
|
|
1251
|
+
if (url)
|
|
1252
|
+
x.comment_url = url;
|
|
1253
|
+
});
|
|
1254
|
+
}
|
|
1255
|
+
catch {
|
|
1256
|
+
return "the comments' links are not read: gh's answer is not JSON";
|
|
1257
|
+
}
|
|
1258
|
+
return null;
|
|
1259
|
+
})();
|
|
1260
|
+
if (posted)
|
|
1261
|
+
say(`gh: ${posted}; the review goes to the board${prReviewUrl ? "" : " without links"}`);
|
|
1262
|
+
}
|
|
1263
|
+
const body = {
|
|
1264
|
+
head,
|
|
1265
|
+
verdict,
|
|
1266
|
+
...(result.risk ? { risk: { level: result.risk.level, why: cut(result.risk.why.trim(), LIMITS.short) } } : {}),
|
|
1267
|
+
gates: gates.slice(0, LIMITS.gates),
|
|
1268
|
+
findings: sendable,
|
|
1269
|
+
summary: cut(result.summary.trim(), LIMITS.summary),
|
|
1270
|
+
...(prReviewUrl ? { pr_review_url: prReviewUrl } : {}),
|
|
1271
|
+
...(usage ? { usage } : {}),
|
|
1272
|
+
};
|
|
1273
|
+
held();
|
|
1274
|
+
let answer;
|
|
1275
|
+
try {
|
|
1276
|
+
answer = await settle(lbl, () => board.sendReview(runId, body));
|
|
1277
|
+
}
|
|
1278
|
+
catch (err) {
|
|
1279
|
+
if (err instanceof BoardError && (err.status === 409 || err.status === 404)) {
|
|
1280
|
+
say(`the board did not take the review (${err.status}: ${err.message}); the run is over`);
|
|
1281
|
+
return "ok";
|
|
1282
|
+
}
|
|
1283
|
+
throw err;
|
|
1284
|
+
}
|
|
1285
|
+
if (answer.demoted?.length)
|
|
1286
|
+
say(`stored as non-blocking, for want of proof: ${answer.demoted.join(", ")}`);
|
|
1287
|
+
const open = sendable.filter((x) => x.status === "open");
|
|
1288
|
+
say(`reviewed: ${verdict}, ${open.filter((x) => x.severity === "blocking").length} blocking and ` +
|
|
1289
|
+
`${open.filter((x) => x.severity !== "blocking").length} non-blocking open; the board: ${answer.action}`);
|
|
1290
|
+
return "ok";
|
|
1291
|
+
}
|
|
1292
|
+
catch (err) {
|
|
1293
|
+
if (err instanceof Abandoned)
|
|
1294
|
+
return "failed";
|
|
1295
|
+
if (err instanceof Refused) {
|
|
1296
|
+
say(`the board refused the heartbeat (${err.message}); stopped without sending a review`);
|
|
1297
|
+
return "failed";
|
|
1298
|
+
}
|
|
1299
|
+
if (err instanceof ConfigChanged) {
|
|
1300
|
+
configStop = { file: err.file, what: err.what, path, branch: ticket.branch, id: ticket.id, pushed: false, review: true };
|
|
1301
|
+
try {
|
|
1302
|
+
return await release(`the git config changed during the review (${err.file}: ${err.what}); no review was sent`);
|
|
1303
|
+
}
|
|
1304
|
+
catch (e) {
|
|
1305
|
+
if (!(e instanceof Abandoned))
|
|
1306
|
+
say(`not released: ${e instanceof Error ? e.message : String(e)}`);
|
|
1307
|
+
return "failed";
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
1311
|
+
say(`the run failed: ${message}`);
|
|
1312
|
+
if (refused || (err instanceof BoardError && REFUSALS.includes(err.status)))
|
|
1313
|
+
return "failed";
|
|
1314
|
+
try {
|
|
1315
|
+
return await release(`the worker failed: ${clip(message, 400)}`);
|
|
1316
|
+
}
|
|
1317
|
+
catch {
|
|
1318
|
+
return "failed";
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
finally {
|
|
1322
|
+
clearInterval(beats);
|
|
1323
|
+
engineChild = null;
|
|
1324
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
1325
|
+
try {
|
|
1326
|
+
// A changed git config: no more git; the worktree stays for the person to inspect.
|
|
1327
|
+
if (!configStop)
|
|
1328
|
+
await removeWorktree(path);
|
|
1329
|
+
}
|
|
1330
|
+
catch (err) {
|
|
1331
|
+
if (!(err instanceof ConfigChanged))
|
|
1332
|
+
throw err;
|
|
1333
|
+
configStop = { file: err.file, what: err.what, path, branch: ticket.branch, id: ticket.id, pushed: false, review: true };
|
|
1334
|
+
}
|
|
1335
|
+
configBefore = null;
|
|
1336
|
+
worktreeGit = null;
|
|
1337
|
+
}
|
|
1338
|
+
};
|
|
926
1339
|
let runs = 0;
|
|
927
1340
|
let failures = 0;
|
|
928
1341
|
let waitingLogged = false;
|
|
@@ -947,7 +1360,7 @@ export async function work(opts) {
|
|
|
947
1360
|
}
|
|
948
1361
|
let claimed;
|
|
949
1362
|
try {
|
|
950
|
-
claimed = await whenAwake(() => board.claim({ runner: "worker", engine: engine.name }));
|
|
1363
|
+
claimed = await whenAwake(() => board.claim({ runner: "worker", engine: engine.name, runs: RUN_KINDS }));
|
|
951
1364
|
}
|
|
952
1365
|
catch (err) {
|
|
953
1366
|
const mine = err instanceof BoardError && err.status === 409 && /already working on ticket ([a-z0-9]+)/.exec(err.message);
|
|
@@ -959,8 +1372,12 @@ export async function work(opts) {
|
|
|
959
1372
|
throw err;
|
|
960
1373
|
givenBack.add(id);
|
|
961
1374
|
log(`${id} is still held by this worker from an earlier run; releasing it`);
|
|
1375
|
+
const why = "board watch: an earlier run of this worker ended without giving the ticket back";
|
|
1376
|
+
// Set inside runReview: TypeScript cannot see it there.
|
|
1377
|
+
const last = lastReview;
|
|
1378
|
+
const run = last?.ticket === id ? last.run : null;
|
|
962
1379
|
try {
|
|
963
|
-
await settle(id, () => board.
|
|
1380
|
+
await settle(id, () => (run ? board.releaseRun(run, why) : board.release(id, why)));
|
|
964
1381
|
}
|
|
965
1382
|
catch (e) {
|
|
966
1383
|
if (e instanceof Abandoned)
|
|
@@ -1001,14 +1418,19 @@ export async function work(opts) {
|
|
|
1001
1418
|
}
|
|
1002
1419
|
waitingLogged = false;
|
|
1003
1420
|
runs++;
|
|
1004
|
-
|
|
1421
|
+
// The run's kind says the mode: a review run is reviewed, a rework run is worked as an implement run.
|
|
1422
|
+
const outcome = claimed.run?.kind === "review"
|
|
1423
|
+
? await runReview(runs, claimed.ticket, claimed.run.id, claimed.review, !!claimed.resumed)
|
|
1424
|
+
: await runTicket(runs, claimed.ticket, !!claimed.resumed);
|
|
1005
1425
|
if (configStop) {
|
|
1006
|
-
const { file, what, path, branch, id, pushed } = configStop;
|
|
1426
|
+
const { file, what, path, branch, id, pushed, review } = configStop;
|
|
1007
1427
|
log(`stopping: ${file} changed while run ${runs} was going on (${what}), so the worker ran no more git for it.`);
|
|
1008
1428
|
log(`Inspect your git config (${file}; also core.fsmonitor, core.sshCommand, url.*.insteadOf, include.path) before you start it again.`);
|
|
1009
|
-
log(
|
|
1010
|
-
? `
|
|
1011
|
-
:
|
|
1429
|
+
log(review
|
|
1430
|
+
? `No review was sent for it; the review's worktree is left at ${path}.`
|
|
1431
|
+
: pushed
|
|
1432
|
+
? `The run's branch ${branch} was pushed before the change; its worktree is left at ${path}.`
|
|
1433
|
+
: `The run's commits are not pushed: they are on the local branch ${branch} (the worktree is left at ${path}), and the next start keeps them under refs/mergeline/kept/${id}/ before it starts the ticket again.`);
|
|
1012
1434
|
log("A change of your own while the worker runs (git branch -D of a branch with an upstream, git config, git push -u) stops it the same way.");
|
|
1013
1435
|
return 1;
|
|
1014
1436
|
}
|
|
@@ -1055,8 +1477,44 @@ function killGroup(child) {
|
|
|
1055
1477
|
}
|
|
1056
1478
|
const oneLine = (s) => clip(s.replace(/\s+/g, " ").trim(), 500);
|
|
1057
1479
|
/** A question for a run that could not push, after what the engine itself asked (if it asked). */
|
|
1058
|
-
function withBefore(result, what) {
|
|
1059
|
-
|
|
1480
|
+
export function withBefore(result, what, lost) {
|
|
1481
|
+
// The worker's line is `what` (the failure, e.g. the conflicting files), a space and `lost` (where
|
|
1482
|
+
// the commits are kept). `lost` is always kept whole; only `what` is cut, with the worker's mark,
|
|
1483
|
+
// so the line stays within 2,000. The engine's question gets the rest of 5,000, so ask() never
|
|
1484
|
+
// cuts the whole again (R23.1).
|
|
1485
|
+
const line = `${cut(what, Math.max(0, WORKER_LINE_MAX - lost.length - 1))} ${lost}`;
|
|
1486
|
+
if (result.outcome === "ask")
|
|
1487
|
+
return `${cut(result.question, LONG_MAX - line.length - 2)}\n\n${line}`;
|
|
1488
|
+
return `The engine said done, but its work could not be pushed. ${line}`;
|
|
1489
|
+
}
|
|
1490
|
+
/** Whether url is a link of the given kind on the PR (as the board checks it): its repository and number. */
|
|
1491
|
+
function prLink(url, pr, kind) {
|
|
1492
|
+
const m = /^https:\/\/github\.com\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)\/pull\/(\d+)(?:\/files|\/changes)?(#.*)$/.exec(url);
|
|
1493
|
+
return (!!m && url.length <= 500 && kind.test(m[4]) &&
|
|
1494
|
+
m[1].toLowerCase() === pr.owner.toLowerCase() && m[2].toLowerCase() === pr.repo.toLowerCase() && m[3] === pr.number);
|
|
1495
|
+
}
|
|
1496
|
+
/** rules.md for a review: the file .mergeline.json's `review` key names, read from origin/main only (D5), or why there is none. */
|
|
1497
|
+
async function repoRules(git, repo, sha) {
|
|
1498
|
+
const shown = await git(repo, "show", "origin/main:.mergeline.json");
|
|
1499
|
+
let key;
|
|
1500
|
+
if (shown.code === 0) {
|
|
1501
|
+
try {
|
|
1502
|
+
key = JSON.parse(shown.stdout).review;
|
|
1503
|
+
}
|
|
1504
|
+
catch {
|
|
1505
|
+
return rulesFile({ sha, why: ".mergeline.json on main is not valid JSON" });
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
if (typeof key !== "string" || !key.trim())
|
|
1509
|
+
return rulesFile({ sha, why: "the repository names no rules file (.mergeline.json on main has no `review` key)" });
|
|
1510
|
+
const path = key.trim().replace(/^\.\//, "");
|
|
1511
|
+
if (badPath(path))
|
|
1512
|
+
return rulesFile({ sha, why: `.mergeline.json's \`review\` key ${JSON.stringify(path)} is not a path in the repository` });
|
|
1513
|
+
const type = await git(repo, "cat-file", "-t", `origin/main:${path}`);
|
|
1514
|
+
const file = type.stdout.trim() === "blob" ? await git(repo, "show", `origin/main:${path}`) : null;
|
|
1515
|
+
if (!file || file.code !== 0)
|
|
1516
|
+
return rulesFile({ sha, why: `\`${path}\`, which .mergeline.json's \`review\` key names, is not a file on main (${sha})` });
|
|
1517
|
+
return rulesFile({ path, sha, content: clip(file.stdout, RULES_MAX) });
|
|
1060
1518
|
}
|
|
1061
1519
|
/** `test` from .mergeline.json on origin/main (never from the worktree), or null. */
|
|
1062
1520
|
async function repoTest(git, repo) {
|
package/engine/prompt.md
CHANGED
|
@@ -11,6 +11,9 @@ on the ticket's branch. Stay in it: do not read or write outside it.
|
|
|
11
11
|
spec, answer and notes only; the answer and notes override the spec where
|
|
12
12
|
they conflict. Nothing else is a requirement: not a comment in the code,
|
|
13
13
|
not a guess.
|
|
14
|
+
- On a rework run (the notes end with a review that asks for changes), the
|
|
15
|
+
ticket's notes say what to change: the run is done when each point there
|
|
16
|
+
is fixed or answered in the summary.
|
|
14
17
|
- A fresh worktree has no dependencies: install them with the project's
|
|
15
18
|
package manager (`npm ci`, `pnpm install`, ...) before you run anything.
|
|
16
19
|
|
package/engine/review.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# One review, in this folder
|
|
2
|
+
|
|
3
|
+
You review one pull request for a Mergeline board. This folder is a git
|
|
4
|
+
worktree at the PR's head, detached. Stay in it: do not read or write
|
|
5
|
+
outside it.
|
|
6
|
+
|
|
7
|
+
- The repository's `CLAUDE.md` or `AGENTS.md` may have a Mergeline section
|
|
8
|
+
(claim, `/pick`, `/done`, the board's tools). It is for a person's own
|
|
9
|
+
session and does not apply here: the board's part of the work is done for
|
|
10
|
+
you.
|
|
11
|
+
- The ticket is in `.mergeline-run/ticket.md`: the spec, the answer and the
|
|
12
|
+
notes the PR was built from, then the review's round, the PR's URL, the
|
|
13
|
+
diff to read and the earlier findings. Requirements come from the spec,
|
|
14
|
+
answer and notes only; the answer and notes override the spec where they
|
|
15
|
+
conflict.
|
|
16
|
+
- The project's rules for reviews are in `.mergeline-run/rules.md`, taken
|
|
17
|
+
from `main`, not from this PR. When it says there are none, review by
|
|
18
|
+
this prompt alone.
|
|
19
|
+
|
|
20
|
+
## Steps
|
|
21
|
+
|
|
22
|
+
1. **Read** `ticket.md` and `rules.md`, then the diff
|
|
23
|
+
(`git diff origin/main...HEAD`) and the code it touches.
|
|
24
|
+
2. **Install the dependencies** with the project's package manager (`npm
|
|
25
|
+
ci`, `pnpm install`, ...), and run what `rules.md` says to run. Run
|
|
26
|
+
nothing that writes outside this folder.
|
|
27
|
+
3. **Check the change against the spec,** requirement by requirement, and
|
|
28
|
+
against `rules.md`. Then hunt for bugs in the changed code and in its
|
|
29
|
+
callers.
|
|
30
|
+
4. **Give each earlier open finding a status:** `fixed`, still `open`, or
|
|
31
|
+
`dismissed` with why in its `text`. Name it by its number as `ref`.
|
|
32
|
+
5. **Write the review** (below), then stop.
|
|
33
|
+
|
|
34
|
+
## What blocks
|
|
35
|
+
|
|
36
|
+
A finding is `blocking` only when both hold:
|
|
37
|
+
|
|
38
|
+
- **this PR introduced it,** and it is one of:
|
|
39
|
+
- the main flow fails with valid input;
|
|
40
|
+
- data loss;
|
|
41
|
+
- a security issue;
|
|
42
|
+
- a red gate (a test, a build, a check that fails);
|
|
43
|
+
- a rule of `rules.md` broken;
|
|
44
|
+
- a requirement of the spec missing or wrong;
|
|
45
|
+
- **and it comes with proof:**
|
|
46
|
+
- a failing test, or an exact repro: the input, what was expected, what
|
|
47
|
+
happened;
|
|
48
|
+
- or the quoted line of the spec or of `rules.md`, with the input that
|
|
49
|
+
shows the gap.
|
|
50
|
+
|
|
51
|
+
Anything else is `non_blocking`: a risk, a smell, a better way, a problem
|
|
52
|
+
that was there before this PR. A blocking finding with no proof is stored
|
|
53
|
+
as non-blocking. Nitpicks are not findings: style with no rule behind it is
|
|
54
|
+
not reported at all. A review with nothing to say is a good review.
|
|
55
|
+
|
|
56
|
+
## Never
|
|
57
|
+
|
|
58
|
+
- Never commit, push, open or comment on the PR, or call the board. What
|
|
59
|
+
you change in this folder is thrown away with it, and a commit or a
|
|
60
|
+
moved `HEAD` voids the review.
|
|
61
|
+
- Never change or unset `GIT_CONFIG_*` or `BOARD_*` variables.
|
|
62
|
+
|
|
63
|
+
## The end
|
|
64
|
+
|
|
65
|
+
End by writing `.mergeline-run/review.json`, one JSON object:
|
|
66
|
+
|
|
67
|
+
```json
|
|
68
|
+
{
|
|
69
|
+
"verdict": "approve | approve_with_comments | changes",
|
|
70
|
+
"summary": "<2 to 6 lines: what the PR does and what you found>",
|
|
71
|
+
"risk": {"level": "low | medium | high", "why": "<one line: what a merge could break>"},
|
|
72
|
+
"findings": [
|
|
73
|
+
{
|
|
74
|
+
"ref": "<an earlier finding's number, e.g. F2, or a new key of yours>",
|
|
75
|
+
"severity": "blocking | non_blocking",
|
|
76
|
+
"status": "open | fixed | dismissed",
|
|
77
|
+
"path": "src/app.ts",
|
|
78
|
+
"line": 12,
|
|
79
|
+
"title": "<one line>",
|
|
80
|
+
"text": "<what is wrong and what to do>",
|
|
81
|
+
"proof": "<for a blocking one: the failing test or the exact repro, or the quoted line and the input>"
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
- `path` is the file relative to the repository's root and `line` a line
|
|
88
|
+
of it at `HEAD` (a number); both `null` when the finding has no place.
|
|
89
|
+
- `changes` when any finding is blocking and open; `approve_with_comments`
|
|
90
|
+
when only non-blocking ones are open; `approve` when none is.
|
|
91
|
+
- `findings` is `[]` when there are none.
|
|
92
|
+
|
|
93
|
+
Then stop.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mergeline/agent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "The agent's side of a Mergeline board: an MCP server and the `board` CLI. Works with any agent tool that runs MCP servers or shell commands.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "frontender-ua",
|