@mergelight/cli 0.1.14 → 0.1.16
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 +2 -2
- package/dist/checks.js +108 -0
- package/dist/cli.js +67 -7
- package/dist/cloud.js +383 -0
- package/dist/mcp/findings.js +5 -0
- package/dist/prompt.js +258 -5
- package/dist/providers/codex.js +26 -0
- package/dist/publish/closed.js +58 -0
- package/dist/publish/handled.js +22 -4
- package/dist/publish/markdown.js +3 -1
- package/dist/publish/publish.js +184 -29
- package/dist/publish/state.js +7 -3
- package/dist/publish/team-checks.js +72 -0
- package/dist/render.js +23 -0
- package/dist/review.js +102 -6
- package/dist/schema.js +49 -0
- package/dist/skills.js +177 -0
- package/dist/workspace.js +193 -3
- package/package.json +1 -1
package/dist/schema.js
CHANGED
|
@@ -27,6 +27,30 @@ export const candidateSchema = {
|
|
|
27
27
|
},
|
|
28
28
|
},
|
|
29
29
|
};
|
|
30
|
+
/** The answer to one of the team's own checks. */
|
|
31
|
+
export const checkSchema = {
|
|
32
|
+
type: "object",
|
|
33
|
+
additionalProperties: false,
|
|
34
|
+
required: ["verdict", "summary", "problems"],
|
|
35
|
+
properties: {
|
|
36
|
+
verdict: { type: "string", enum: ["pass", "fail", "unsure"], description: "pass: you read the change and it does what the check asks. fail: you can name the place where it does not. unsure: you couldn't see enough to tell." },
|
|
37
|
+
summary: { type: "string", description: "One or two plain sentences: what you checked and what you found." },
|
|
38
|
+
problems: {
|
|
39
|
+
type: "array",
|
|
40
|
+
description: "When it fails: each place that breaks the check, 10 at most. Empty when it passes.",
|
|
41
|
+
items: {
|
|
42
|
+
type: "object",
|
|
43
|
+
additionalProperties: false,
|
|
44
|
+
required: ["file", "line", "note"],
|
|
45
|
+
properties: {
|
|
46
|
+
file: { type: "string" },
|
|
47
|
+
line: { type: "integer", description: "The line in the head version, or 0 when no one line stands for it." },
|
|
48
|
+
note: { type: "string", description: "One sentence: what is wrong here." },
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
};
|
|
30
54
|
export const reviewSchema = {
|
|
31
55
|
type: "object",
|
|
32
56
|
additionalProperties: false,
|
|
@@ -104,3 +128,28 @@ export const reviewSchema = {
|
|
|
104
128
|
},
|
|
105
129
|
},
|
|
106
130
|
};
|
|
131
|
+
/**
|
|
132
|
+
* The review schema for a verifier that was given the team's earlier decisions. Each rejected
|
|
133
|
+
* candidate then names the decision that caused it, or null. Reviews without decisions keep the
|
|
134
|
+
* plain schema, so nothing changes for them.
|
|
135
|
+
*/
|
|
136
|
+
export const reviewSchemaWithDecisions = {
|
|
137
|
+
...reviewSchema,
|
|
138
|
+
properties: {
|
|
139
|
+
...reviewSchema.properties,
|
|
140
|
+
rejected: {
|
|
141
|
+
...reviewSchema.properties.rejected,
|
|
142
|
+
items: {
|
|
143
|
+
...reviewSchema.properties.rejected.items,
|
|
144
|
+
required: ["candidate", "reason", "decision"],
|
|
145
|
+
properties: {
|
|
146
|
+
...reviewSchema.properties.rejected.items.properties,
|
|
147
|
+
decision: {
|
|
148
|
+
type: ["string", "null"],
|
|
149
|
+
description: "The id of the team's earlier decision that is why you rejected it, like M12. Null when you rejected it for any other reason.",
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
};
|
package/dist/skills.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { lstatSync, mkdirSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { run } from "./run.js";
|
|
4
|
+
const MAX_SKILLS = 10;
|
|
5
|
+
const MAX_FILES = 30;
|
|
6
|
+
const MAX_FILE_BYTES = 60_000;
|
|
7
|
+
const MAX_SKILL_BYTES = 200_000;
|
|
8
|
+
/** The copy is a help, not a need: a git call that hangs, or a copy that takes too long, is given up. */
|
|
9
|
+
const GIT_TIMEOUT_MS = 20_000;
|
|
10
|
+
const IMPORT_TIMEOUT_MS = 60_000;
|
|
11
|
+
export { GIT_TIMEOUT_MS as SKILL_GIT_TIMEOUT_MS, IMPORT_TIMEOUT_MS as SKILL_IMPORT_TIMEOUT_MS };
|
|
12
|
+
/** A folder in the repository, written plainly: no wildcards, no "..", no leading slash. */
|
|
13
|
+
export function isSkillPath(v) {
|
|
14
|
+
return typeof v === "string" && /^[\w.@+\-]+(\/[\w.@+\-]+)*$/.test(v) && v.length <= 200 && !v.split("/").some((part) => part === ".." || part === ".");
|
|
15
|
+
}
|
|
16
|
+
/** One line of plain text from a skill's own words, cut to size. */
|
|
17
|
+
function line(text, max) {
|
|
18
|
+
return text.replace(/[\u0000-\u001f\u007f-\u009f\u200e\u200f\u2028-\u202e\u2066-\u2069]+/g, " ").replace(/ {2,}/g, " ").trim().slice(0, max).trim();
|
|
19
|
+
}
|
|
20
|
+
/** The name and description from the top of a SKILL.md, the way Claude Code and Codex write them. */
|
|
21
|
+
export function describeSkill(markdown, folder) {
|
|
22
|
+
const front = /^---\r?\n([\s\S]*?)\r?\n---/.exec(markdown)?.[1] ?? "";
|
|
23
|
+
const lines = front.split(/\r?\n/);
|
|
24
|
+
const field = (key) => {
|
|
25
|
+
const at = lines.findIndex((l) => l.startsWith(`${key}:`));
|
|
26
|
+
if (at < 0)
|
|
27
|
+
return "";
|
|
28
|
+
const first = lines[at].slice(key.length + 1).trim();
|
|
29
|
+
// The text can go on over the lines below, indented: after a block marker like > or |, or as a long line that wraps.
|
|
30
|
+
const more = [];
|
|
31
|
+
for (const l of lines.slice(at + 1)) {
|
|
32
|
+
if (l.trim() && !/^[ \t]/.test(l))
|
|
33
|
+
break;
|
|
34
|
+
more.push(l.trim());
|
|
35
|
+
}
|
|
36
|
+
const value = [/^[>|][+-]?\d?$/.test(first) ? "" : first, ...more].filter(Boolean).join(" ");
|
|
37
|
+
return value.replace(/^(["'])(.*)\1$/, "$2");
|
|
38
|
+
};
|
|
39
|
+
const body = markdown.replace(/^---\r?\n[\s\S]*?\r?\n---/, "");
|
|
40
|
+
const firstLine = body.split("\n").map((l) => l.replace(/^#+\s*/, "").trim()).find(Boolean) ?? "";
|
|
41
|
+
return {
|
|
42
|
+
name: line(field("name"), 80) || line(folder.split("/").pop() ?? "skill", 80),
|
|
43
|
+
description: line(field("description"), 400) || line(firstLine, 400),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** The most a written skill can hold, the same as the team's server allows. */
|
|
47
|
+
export const SKILL_TEXT_MAX = 8000;
|
|
48
|
+
const IGNORABLE = /^\p{Default_Ignorable_Code_Point}$/u;
|
|
49
|
+
/** A letter, a mark on a letter, or an emoji: something a person sees. */
|
|
50
|
+
const SEEN = /^[\p{L}\p{M}\p{Extended_Pictographic}\p{Emoji_Modifier}]$/u;
|
|
51
|
+
/**
|
|
52
|
+
* The text without the characters a person can't see: zero-width characters, marks that reorder
|
|
53
|
+
* text, Unicode "tag" characters and the like. One kind stays: a single joiner between two letters
|
|
54
|
+
* or two emoji. Persian and Indic words are written with one, and so are emoji made of several.
|
|
55
|
+
*/
|
|
56
|
+
export function withoutHidden(v) {
|
|
57
|
+
const points = Array.from(v);
|
|
58
|
+
return points
|
|
59
|
+
.filter((ch, i) => !IGNORABLE.test(ch) || ((ch === "\u200c" || ch === "\u200d") && SEEN.test(points[i - 1] ?? "") && SEEN.test(points[i + 1] ?? "")))
|
|
60
|
+
.join("");
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Whether a text holds characters a person can't see. The team reads its rules and skills on a
|
|
64
|
+
* dashboard, so what the reviewing AI reads must be what the team saw there.
|
|
65
|
+
*/
|
|
66
|
+
export function hasHiddenCharacters(v) {
|
|
67
|
+
return withoutHidden(v) !== v;
|
|
68
|
+
}
|
|
69
|
+
/** Plain text over several lines. Line breaks and tabs are fine. Other control characters, and characters that hide or reorder text, are not. */
|
|
70
|
+
export function isSkillText(v) {
|
|
71
|
+
return typeof v === "string" && v.length <= SKILL_TEXT_MAX && v.trim().length > 0 && !/[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u2028\u2029]/.test(v) && !hasHiddenCharacters(v);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Writes the skills a team wrote on its dashboard into the checkout, each as a file a reviewer can
|
|
75
|
+
* read, next to the copies of the picked skill folders. It goes after importSkills, which empties
|
|
76
|
+
* the folder first. Nothing is written through a link: the folder has to be the checkout's own.
|
|
77
|
+
*/
|
|
78
|
+
export function writeSkills(checkout, written) {
|
|
79
|
+
const skills = [];
|
|
80
|
+
if (!written.length)
|
|
81
|
+
return skills;
|
|
82
|
+
const dir = join(checkout, ".mergelight", "skills");
|
|
83
|
+
mkdirSync(dir, { recursive: true });
|
|
84
|
+
if (realpathSync(dir) !== join(realpathSync(checkout), ".mergelight", "skills"))
|
|
85
|
+
return skills;
|
|
86
|
+
for (const [index, skill] of written.slice(0, MAX_SKILLS).entries()) {
|
|
87
|
+
// "w" keeps these apart from the numbered folders of the picked skills.
|
|
88
|
+
const folder = `w${index + 1}`;
|
|
89
|
+
rmSync(join(dir, folder), { recursive: true, force: true });
|
|
90
|
+
mkdirSync(join(dir, folder));
|
|
91
|
+
writeFileSync(join(dir, folder, "SKILL.md"), `# ${skill.name}\n\n${skill.text}\n`);
|
|
92
|
+
skills.push({ name: skill.name, description: skill.description, read: `.mergelight/skills/${folder}/SKILL.md` });
|
|
93
|
+
}
|
|
94
|
+
return skills;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* An empty folder in the checkout for the copies. The checkout holds the pull request's own files,
|
|
98
|
+
* so it could carry a link or a file at this very place: to send the copies somewhere else on the
|
|
99
|
+
* machine, to put files of its own next to a skill, or to stop the copy and so switch the team's
|
|
100
|
+
* skills off. Whatever the pull request put there is removed first, and a link is never written through.
|
|
101
|
+
*/
|
|
102
|
+
function emptySkillsFolder(checkout) {
|
|
103
|
+
const root = join(checkout, ".mergelight");
|
|
104
|
+
const there = lstatSync(root, { throwIfNoEntry: false });
|
|
105
|
+
// Removing a link or a file removes that entry alone, never what a link points at.
|
|
106
|
+
if (there && !there.isDirectory())
|
|
107
|
+
rmSync(root, { force: true });
|
|
108
|
+
const dir = join(root, "skills");
|
|
109
|
+
rmSync(dir, { recursive: true, force: true });
|
|
110
|
+
mkdirSync(dir, { recursive: true });
|
|
111
|
+
return realpathSync(dir) === join(realpathSync(checkout), ".mergelight", "skills") ? dir : null;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Copies the team's picked skill folders into the checkout, from the base branch (the commit in `base`) and
|
|
115
|
+
* never from the pull request. A pull request can change a skill's file, and its author must not be able to
|
|
116
|
+
* tell the reviewer what to do that way: the copy the reviewer reads is the one the team already merged.
|
|
117
|
+
*
|
|
118
|
+
* Only ordinary files are copied. A link in the folder could point anywhere, so it is left out.
|
|
119
|
+
*/
|
|
120
|
+
export async function importSkills(mirror, base, checkout, folders, git = (args) => run("git", ["-C", mirror, ...args], { timeoutMs: GIT_TIMEOUT_MS }), now = Date.now) {
|
|
121
|
+
const skills = [];
|
|
122
|
+
const into = emptySkillsFolder(checkout);
|
|
123
|
+
if (!into)
|
|
124
|
+
return skills;
|
|
125
|
+
const deadline = now() + IMPORT_TIMEOUT_MS;
|
|
126
|
+
for (const [index, folder] of folders.filter(isSkillPath).slice(0, MAX_SKILLS).entries()) {
|
|
127
|
+
if (now() > deadline)
|
|
128
|
+
break;
|
|
129
|
+
const listed = await git(["ls-tree", "-r", "-z", base, "--", `${folder}/`]);
|
|
130
|
+
if (listed.code !== 0)
|
|
131
|
+
continue;
|
|
132
|
+
const files = listed.stdout
|
|
133
|
+
.split("\0")
|
|
134
|
+
.filter(Boolean)
|
|
135
|
+
.map((entry) => /^(\d{6}) (\w+) ([0-9a-f]{40,64})\t([\s\S]+)$/.exec(entry))
|
|
136
|
+
.filter((m) => Boolean(m) && m[2] === "blob" && (m[1] === "100644" || m[1] === "100755"))
|
|
137
|
+
.map((m) => ({ sha: m[3], path: m[4] }))
|
|
138
|
+
.filter((f) => f.path.startsWith(`${folder}/`) && isSkillPath(f.path));
|
|
139
|
+
const main = files.find((f) => f.path === `${folder}/SKILL.md`);
|
|
140
|
+
// No SKILL.md on the base branch: there is no skill there yet, whatever the pull request adds.
|
|
141
|
+
// A folder that is a link to another folder has none either: git lists the link, not what is behind it.
|
|
142
|
+
if (!main)
|
|
143
|
+
continue;
|
|
144
|
+
const target = join(".mergelight", "skills", String(index + 1));
|
|
145
|
+
let total = 0;
|
|
146
|
+
let markdown = "";
|
|
147
|
+
for (const file of [main, ...files.filter((f) => f !== main)].slice(0, MAX_FILES)) {
|
|
148
|
+
if (now() > deadline)
|
|
149
|
+
break;
|
|
150
|
+
const sized = await git(["cat-file", "-s", file.sha]);
|
|
151
|
+
const size = sized.code === 0 ? Number(sized.stdout.trim()) : NaN;
|
|
152
|
+
const read = Number.isFinite(size) && size <= MAX_FILE_BYTES && total + size <= MAX_SKILL_BYTES ? await git(["cat-file", "blob", file.sha]) : null;
|
|
153
|
+
if (!read || read.code !== 0) {
|
|
154
|
+
if (file === main)
|
|
155
|
+
break;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const content = read.stdout;
|
|
159
|
+
// Not text: a picture, or a file in another encoding. It was read as text, so a copy would be a broken one.
|
|
160
|
+
if (content.includes("\uFFFD") && file !== main)
|
|
161
|
+
continue;
|
|
162
|
+
total += size;
|
|
163
|
+
if (file === main)
|
|
164
|
+
markdown = content;
|
|
165
|
+
// Without its SKILL.md there is no skill, so its other files aren't read or copied either.
|
|
166
|
+
if (!markdown)
|
|
167
|
+
break;
|
|
168
|
+
const dest = join(into, String(index + 1), file.path.slice(folder.length + 1));
|
|
169
|
+
mkdirSync(dirname(dest), { recursive: true });
|
|
170
|
+
writeFileSync(dest, content);
|
|
171
|
+
}
|
|
172
|
+
if (!markdown)
|
|
173
|
+
continue;
|
|
174
|
+
skills.push({ ...describeSkill(markdown, folder), read: `${target}/SKILL.md` });
|
|
175
|
+
}
|
|
176
|
+
return skills;
|
|
177
|
+
}
|
package/dist/workspace.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, mkdtempSync, rmSync, statSync } from "node:fs";
|
|
1
|
+
import { existsSync, lstatSync, mkdirSync, mkdtempSync, rmSync, statSync } from "node:fs";
|
|
2
2
|
import { homedir, tmpdir } from "node:os";
|
|
3
|
-
import { join } from "node:path";
|
|
3
|
+
import { join, posix } from "node:path";
|
|
4
4
|
import { run, runOk } from "./run.js";
|
|
5
|
+
import { importSkills, SKILL_GIT_TIMEOUT_MS, SKILL_IMPORT_TIMEOUT_MS, writeSkills } from "./skills.js";
|
|
5
6
|
/** Lockfiles and generated files are left out of the diff the model sees. */
|
|
6
7
|
const EXCLUDES = [
|
|
7
8
|
"package-lock.json",
|
|
@@ -30,6 +31,172 @@ export function cacheDir() {
|
|
|
30
31
|
function git(args, cwd) {
|
|
31
32
|
return runOk("git", ["-c", "credential.helper=", "-c", "credential.helper=!gh auth git-credential", ...args], { cwd, env: { ...process.env, GIT_TERMINAL_PROMPT: "0" } });
|
|
32
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Mergelight's own folder in the checkout, for the files it writes there: a diff too large for the
|
|
36
|
+
* prompt, and the team's skills. The checkout holds the pull request's files, so the pull request
|
|
37
|
+
* could have put a link at one of those names, to make a write land somewhere else on the machine.
|
|
38
|
+
* A link or a file in the way is removed, never written through.
|
|
39
|
+
*/
|
|
40
|
+
export function ownFolder(checkout) {
|
|
41
|
+
const dir = join(checkout, ".mergelight");
|
|
42
|
+
if (lstatSync(dir, { throwIfNoEntry: false })?.isDirectory() === false)
|
|
43
|
+
rmSync(dir, { force: true });
|
|
44
|
+
mkdirSync(dir, { recursive: true });
|
|
45
|
+
// Removing a link removes the link alone, not what it points at.
|
|
46
|
+
for (const name of ["pr.diff", "skills"])
|
|
47
|
+
rmSync(join(dir, name), { recursive: true, force: true });
|
|
48
|
+
return dir;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Skill folders Codex finds by itself and offers to the model. In the checkout they are the pull
|
|
52
|
+
* request's, so its author could plant one that tells the reviewers what to skip. Codex has a switch
|
|
53
|
+
* to stop that, but not every version has it, so the folders go. The diff still shows them.
|
|
54
|
+
*/
|
|
55
|
+
export const AGENT_SKILL_FOLDERS = [".agents/skills", ".codex/skills"];
|
|
56
|
+
export function removeAgentSkills(checkout) {
|
|
57
|
+
for (const folder of AGENT_SKILL_FOLDERS) {
|
|
58
|
+
const parent = join(checkout, folder.split("/")[0]);
|
|
59
|
+
const kind = lstatSync(parent, { throwIfNoEntry: false });
|
|
60
|
+
if (!kind)
|
|
61
|
+
continue;
|
|
62
|
+
// .codex or .agents can be a link the pull request made, to a folder anywhere on the machine.
|
|
63
|
+
// Going through it would delete that folder's skills. A link or a file goes, alone.
|
|
64
|
+
if (!kind.isDirectory()) {
|
|
65
|
+
rmSync(parent, { force: true });
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
// rmSync removes a link at this name without following it.
|
|
69
|
+
rmSync(join(parent, "skills"), { recursive: true, force: true });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Copies the team's skills, and never lets that stop or hold up a review. The mirror has no file
|
|
74
|
+
* contents of its own, so reading a skill the pull request changed asks GitHub for it: with the
|
|
75
|
+
* same login as every other git call here, never with a prompt, and never for long.
|
|
76
|
+
*/
|
|
77
|
+
async function skillsFor(mirror, base, checkout, folders) {
|
|
78
|
+
if (!folders.length)
|
|
79
|
+
return [];
|
|
80
|
+
const gitInMirror = (args) => run("git", ["-c", "credential.helper=", "-c", "credential.helper=!gh auth git-credential", "-C", mirror, ...args], {
|
|
81
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
|
|
82
|
+
timeoutMs: SKILL_GIT_TIMEOUT_MS,
|
|
83
|
+
});
|
|
84
|
+
let timer;
|
|
85
|
+
// A last stop, for a git process that doesn't end even when told to.
|
|
86
|
+
const gaveUp = new Promise((resolve) => {
|
|
87
|
+
timer = setTimeout(() => resolve([]), SKILL_IMPORT_TIMEOUT_MS + 2 * SKILL_GIT_TIMEOUT_MS);
|
|
88
|
+
timer.unref();
|
|
89
|
+
});
|
|
90
|
+
try {
|
|
91
|
+
return await Promise.race([importSkills(mirror, base, checkout, folders, gitInMirror).catch(() => []), gaveUp]);
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
clearTimeout(timer);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/** The root instruction files whose conventions the reviewers follow, read from the base branch. */
|
|
98
|
+
export const CONVENTION_FILES = ["AGENTS.md", "CLAUDE.md", "CONTRIBUTING.md", ".github/CONTRIBUTING.md", "docs/CONTRIBUTING.md", ".github/copilot-instructions.md"];
|
|
99
|
+
/** Per file, so one long file can't fill every prompt. */
|
|
100
|
+
export const MAX_CONVENTION_CHARS = 12_000;
|
|
101
|
+
/** All files together, and how many. */
|
|
102
|
+
export const MAX_CONVENTIONS_CHARS = 40_000;
|
|
103
|
+
export const MAX_CONVENTION_FILES = 12;
|
|
104
|
+
/** A file larger than this isn't read at all. */
|
|
105
|
+
const MAX_CONVENTION_BYTES = 256_000;
|
|
106
|
+
/** Which instruction files apply: the root ones, AGENTS.md or CLAUDE.md in a folder above a changed file, and .github/instructions. */
|
|
107
|
+
export function conventionPaths(all, changedPaths) {
|
|
108
|
+
const folders = new Set();
|
|
109
|
+
for (const changed of changedPaths) {
|
|
110
|
+
const parts = changed.split("/").slice(0, -1);
|
|
111
|
+
for (let i = 1; i <= parts.length; i++)
|
|
112
|
+
folders.add(parts.slice(0, i).join("/"));
|
|
113
|
+
}
|
|
114
|
+
const present = new Set(all);
|
|
115
|
+
const root = CONVENTION_FILES.filter((p) => present.has(p));
|
|
116
|
+
const github = all.filter((p) => p.startsWith(".github/instructions/") && p.endsWith(".instructions.md")).sort();
|
|
117
|
+
const nested = all
|
|
118
|
+
.filter((p) => {
|
|
119
|
+
const at = p.lastIndexOf("/");
|
|
120
|
+
return at > 0 && ["AGENTS.md", "CLAUDE.md"].includes(p.slice(at + 1)) && folders.has(p.slice(0, at));
|
|
121
|
+
})
|
|
122
|
+
// Closest to the root first: a folder's file comes before the ones in its subfolders.
|
|
123
|
+
.sort((a, b) => a.split("/").length - b.split("/").length || a.localeCompare(b));
|
|
124
|
+
// The rules closest to the changed code come before .github/instructions, so they aren't the ones crowded out.
|
|
125
|
+
return [...root, ...nested, ...github];
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The team's instruction files as the base branch has them. The checkout is the pull request's, and
|
|
129
|
+
* its author could add or rewrite AGENTS.md there to steer the reviewers. A file that is a link is
|
|
130
|
+
* followed once, to a file inside the repository. A file that is missing, too large, or can't be
|
|
131
|
+
* read in time is left out: it never stops a review.
|
|
132
|
+
*/
|
|
133
|
+
export async function conventionsFor(mirror, base, changedPaths = []) {
|
|
134
|
+
const git = (args) => run("git", ["-c", "credential.helper=", "-c", "credential.helper=!gh auth git-credential", "-C", mirror, ...args], {
|
|
135
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
|
|
136
|
+
timeoutMs: SKILL_GIT_TIMEOUT_MS,
|
|
137
|
+
}).catch(() => null);
|
|
138
|
+
const listed = await git(["ls-tree", "-r", "-z", "--full-tree", base]);
|
|
139
|
+
if (listed?.code !== 0)
|
|
140
|
+
return [];
|
|
141
|
+
const tree = new Map();
|
|
142
|
+
for (const entry of listed.stdout.split("\0")) {
|
|
143
|
+
const m = /^(\d{6}) blob ([0-9a-f]{40,64})\t([\s\S]+)$/.exec(entry);
|
|
144
|
+
if (m)
|
|
145
|
+
tree.set(m[3], { mode: m[1], sha: m[2] });
|
|
146
|
+
}
|
|
147
|
+
const blob = async (sha) => {
|
|
148
|
+
const sized = await git(["cat-file", "-s", sha]);
|
|
149
|
+
if (sized?.code !== 0 || !(Number(sized.stdout.trim()) <= MAX_CONVENTION_BYTES))
|
|
150
|
+
return null;
|
|
151
|
+
const read = await git(["cat-file", "blob", sha]);
|
|
152
|
+
return read?.code === 0 ? read.stdout : null;
|
|
153
|
+
};
|
|
154
|
+
const read = async (path) => {
|
|
155
|
+
let file = tree.get(path);
|
|
156
|
+
if (file?.mode === "120000") {
|
|
157
|
+
// A link: git keeps the target's path as its content.
|
|
158
|
+
const target = (await blob(file.sha))?.trim() ?? "";
|
|
159
|
+
const resolved = posix.normalize(posix.join(posix.dirname(path), target));
|
|
160
|
+
if (!target || target.startsWith("/") || resolved === ".." || resolved.startsWith("../"))
|
|
161
|
+
return null;
|
|
162
|
+
file = tree.get(resolved);
|
|
163
|
+
}
|
|
164
|
+
if (!file || (file.mode !== "100644" && file.mode !== "100755"))
|
|
165
|
+
return null;
|
|
166
|
+
const text = (await blob(file.sha))?.trim();
|
|
167
|
+
return text ? { path, sha: file.sha, text } : null;
|
|
168
|
+
};
|
|
169
|
+
const found = await Promise.all(conventionPaths([...tree.keys()], changedPaths).slice(0, MAX_CONVENTION_FILES * 2).map(read));
|
|
170
|
+
const conventions = [];
|
|
171
|
+
const seen = new Set();
|
|
172
|
+
let total = 0;
|
|
173
|
+
for (const f of found) {
|
|
174
|
+
// AGENTS.md and CLAUDE.md are often one file and a link to it: the text goes in once.
|
|
175
|
+
if (!f || seen.has(f.sha) || conventions.length >= MAX_CONVENTION_FILES)
|
|
176
|
+
continue;
|
|
177
|
+
const room = Math.min(MAX_CONVENTION_CHARS, MAX_CONVENTIONS_CHARS - total);
|
|
178
|
+
if (room <= 0)
|
|
179
|
+
break;
|
|
180
|
+
seen.add(f.sha);
|
|
181
|
+
conventions.push({ path: f.path, text: f.text.slice(0, room), truncated: f.text.length > room });
|
|
182
|
+
total += Math.min(f.text.length, room);
|
|
183
|
+
}
|
|
184
|
+
return conventions;
|
|
185
|
+
}
|
|
186
|
+
/** The answer, or the fallback once the time is up, for work that must never hold up a review. */
|
|
187
|
+
async function withDeadline(work, ms, fallback) {
|
|
188
|
+
let timer;
|
|
189
|
+
const gaveUp = new Promise((resolve) => {
|
|
190
|
+
timer = setTimeout(() => resolve(fallback), ms);
|
|
191
|
+
timer.unref();
|
|
192
|
+
});
|
|
193
|
+
try {
|
|
194
|
+
return await Promise.race([work.catch(() => fallback), gaveUp]);
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
clearTimeout(timer);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
33
200
|
async function hasCommit(mirror, sha) {
|
|
34
201
|
const r = await run("git", ["-C", mirror, "cat-file", "-e", `${sha}^{commit}`]);
|
|
35
202
|
return r.code === 0;
|
|
@@ -67,7 +234,7 @@ async function withLock(dir, fn) {
|
|
|
67
234
|
* Keeps a blobless bare mirror of the repository in the cache, fetches the PR,
|
|
68
235
|
* and checks out the commit under review in a temporary worktree.
|
|
69
236
|
*/
|
|
70
|
-
export async function prepareWorkspace(pr, at) {
|
|
237
|
+
export async function prepareWorkspace(pr, at, skillFolders = [], writtenSkills = []) {
|
|
71
238
|
const mirror = join(cacheDir(), "repos", pr.owner, `${pr.repo}.git`);
|
|
72
239
|
mkdirSync(join(cacheDir(), "repos", pr.owner), { recursive: true });
|
|
73
240
|
if (!existsSync(mirror)) {
|
|
@@ -117,8 +284,27 @@ export async function prepareWorkspace(pr, at) {
|
|
|
117
284
|
const [added, removed, path] = line.split("\t");
|
|
118
285
|
return { path: path ?? "", added: Number(added) || 0, removed: Number(removed) || 0 };
|
|
119
286
|
});
|
|
287
|
+
// -z gives each path as it is, ended by a NUL. Without it git puts quotes around a name with odd characters in it.
|
|
288
|
+
const touchedPaths = (await git(["diff", "--name-only", "-z", "--no-renames", "--no-color", "--no-ext-diff", range], mirror)).stdout.split("\0").filter(Boolean);
|
|
120
289
|
const dir = mkdtempSync(join(tmpdir(), `mergelight-${pr.repo}-${pr.number}-`));
|
|
121
290
|
await git(["worktree", "add", "--quiet", "--detach", dir, headSha], mirror);
|
|
291
|
+
ownFolder(dir);
|
|
292
|
+
removeAgentSkills(dir);
|
|
293
|
+
// From the base branch as it is now: what the team merged. Not from the pull request, and not from
|
|
294
|
+
// the commit it branched from either. Its author picks that commit, and could pick one from
|
|
295
|
+
// before a skill was written or tightened.
|
|
296
|
+
// The same goes for the instruction files.
|
|
297
|
+
const teamBase = haveBaseBranch ? `refs/mergelight/base/${pr.baseRef}` : baseCandidate;
|
|
298
|
+
const skills = await skillsFor(mirror, teamBase, dir, skillFolders);
|
|
299
|
+
// The skills the team wrote on its dashboard go next to them. A skill that can't be written is one fewer, never a failed review.
|
|
300
|
+
let written = [];
|
|
301
|
+
try {
|
|
302
|
+
written = writeSkills(dir, writtenSkills);
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
written = [];
|
|
306
|
+
}
|
|
307
|
+
const conventions = await withDeadline(conventionsFor(mirror, teamBase, touchedPaths), SKILL_IMPORT_TIMEOUT_MS + 2 * SKILL_GIT_TIMEOUT_MS, []);
|
|
122
308
|
return {
|
|
123
309
|
dir,
|
|
124
310
|
mergeBase,
|
|
@@ -127,6 +313,10 @@ export async function prepareWorkspace(pr, at) {
|
|
|
127
313
|
diffStat,
|
|
128
314
|
diffTruncated: diff.length > MAX_INLINE_DIFF,
|
|
129
315
|
changedFiles,
|
|
316
|
+
touchedPaths,
|
|
317
|
+
skills,
|
|
318
|
+
writtenSkills: written,
|
|
319
|
+
conventions,
|
|
130
320
|
cleanup: async () => {
|
|
131
321
|
await run("git", ["-C", mirror, "worktree", "remove", "--force", dir]);
|
|
132
322
|
rmSync(dir, { recursive: true, force: true });
|