@a-t-h-i/bot-lobby 0.6.61 → 0.6.63
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 +8 -8
- package/package.json +2 -1
- package/prompts/designer.md +25 -15
- package/prompts/master.md +284 -491
- package/prompts/panel.md +7 -0
- package/prompts/planner.md +15 -4
- package/prompts/qa.md +5 -1
- package/prompts/worker.md +0 -7
- package/scripts/builtin/affected_packages.mjs +106 -0
- package/scripts/builtin/check_changed.mjs +51 -0
- package/scripts/builtin/check_contract_breakage.mjs +118 -0
- package/scripts/builtin/circular_dependency_check.mjs +79 -0
- package/scripts/builtin/code_duplication_check.mjs +86 -0
- package/scripts/builtin/diff_summary.mjs +62 -0
- package/scripts/builtin/failure_digest.mjs +107 -0
- package/scripts/builtin/find_generated_files.mjs +79 -0
- package/scripts/builtin/find_symbol.mjs +81 -0
- package/scripts/builtin/find_test_fixtures.mjs +73 -0
- package/scripts/builtin/find_tests_for.mjs +12 -0
- package/scripts/builtin/lib/common.mjs +267 -0
- package/scripts/builtin/outline_file.mjs +77 -0
- package/scripts/builtin/project_commands.mjs +49 -0
- package/scripts/builtin/recent_changes.mjs +14 -0
- package/scripts/builtin/repo_map.mjs +65 -0
- package/scripts/builtin/run_changed_tests.mjs +48 -0
- package/src/delivery/transport.ts +1 -1
- package/src/desk/client-extension.ts +1 -31
- package/src/desk/ipc.ts +1 -11
- package/src/desk/session.ts +4 -101
- package/src/execution/agent-runner.ts +50 -8
- package/src/execution/assigned-session.ts +263 -0
- package/src/execution/assignment-discovery.ts +128 -0
- package/src/execution/assignment-policy.ts +46 -0
- package/src/execution/assignment.ts +175 -0
- package/src/execution/delegation.ts +9 -94
- package/src/execution/pi-runner.ts +99 -9
- package/src/harness/instructions.ts +1 -1
- package/src/index.ts +3 -0
- package/src/knowledge/design-record.ts +394 -0
- package/src/knowledge/file-notes.ts +36 -8
- package/src/lobby/ask.ts +6 -0
- package/src/lobby/exec.ts +28 -0
- package/src/lobby/host.ts +2 -11
- package/src/lobby/pi-commands.ts +83 -0
- package/src/lobby/planner.ts +50 -124
- package/src/lobby/prompts.ts +1 -7
- package/src/lobby/runtime.ts +2 -26
- package/src/lobby/service.ts +28 -27
- package/src/lobby/topics.ts +0 -2
- package/src/master/evidence-freshness.ts +96 -0
- package/src/master/master.ts +248 -74
- package/src/master/scout-cache.ts +1 -1
- package/src/master/scout-lock.ts +44 -0
- package/src/master/scout-prefetch.ts +78 -0
- package/src/pi/activity.ts +2 -0
- package/src/pi/assignment-guard.ts +157 -0
- package/src/pi/delegation.ts +2 -21
- package/src/pi/model-support.ts +0 -6
- package/src/pi/start-task.ts +18 -7
- package/src/pi/tools.ts +41 -8
- package/src/roles/scout.ts +3 -1
- package/src/roles/worker.ts +107 -5
- package/src/schemas/configuration.ts +11 -45
- package/src/schemas/findings.ts +3 -0
- package/src/schemas/task.ts +30 -12
- package/src/scripts/library.ts +31 -1
- package/src/scripts/pick.ts +31 -15
- package/src/state/archive.ts +10 -0
- package/src/state/checkpoint.ts +0 -1
- package/src/state/gitignore.ts +39 -0
- package/src/state/persistence.ts +56 -0
- package/src/state/recovery.ts +33 -1
- package/src/webui/api/index.ts +6 -35
- package/src/webui/api/retention.ts +44 -0
- package/src/webui/api/status.ts +5 -6
- package/src/webui/dev/fake-service.ts +8 -117
- package/src/webui/dev/fixtures.ts +2 -16
- package/src/webui/protocol.ts +3 -142
- package/src/workflow/draft-assignments.ts +118 -0
- package/src/workflow/workflow.ts +271 -214
- package/webui/dist/assets/{CodeSurface-BtzJOVyH.js → CodeSurface-CdyiJTYa.js} +1 -1
- package/webui/dist/assets/{TerminalView-Cz-KNWsq.js → TerminalView-D2nbR2J1.js} +1 -1
- package/webui/dist/assets/index-BTyu99rI.css +2 -0
- package/webui/dist/assets/index-BUGK22Z6.js +112 -0
- package/webui/dist/build.json +32 -68
- package/webui/dist/index.html +2 -2
- package/prompts/expert.md +0 -17
- package/prompts/goal-check.md +0 -44
- package/prompts/pr-review.md +0 -62
- package/prompts/splitter.md +0 -58
- package/src/execution/worker-trees.ts +0 -396
- package/src/lobby/issues.ts +0 -219
- package/src/lobby/pr-review.ts +0 -366
- package/src/lobby/pulls.ts +0 -250
- package/src/lobby/split.ts +0 -262
- package/src/master/goal-check.ts +0 -115
- package/src/webui/api/gh-error.ts +0 -17
- package/src/webui/api/git.ts +0 -98
- package/src/webui/api/issues.ts +0 -50
- package/webui/dist/assets/index-D5fvY8f8.js +0 -112
- package/webui/dist/assets/index-DFvk8ADr.css +0 -2
|
@@ -1,396 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A worktree per worker for a parallel batch. Each worker gets its own
|
|
3
|
-
* checkout of the task's working tree as it stood when the batch began
|
|
4
|
-
* (uncommitted changes included), so workers never queue for each other's
|
|
5
|
-
* files; when the batch ends, each worker's change is applied back to the
|
|
6
|
-
* task's tree in turn. A change that no longer applies (two workers touched
|
|
7
|
-
* the same lines) is kept as a patch and a git ref for the oracle to hand
|
|
8
|
-
* back to its owner, never forced in: a patch that fails part way is rolled
|
|
9
|
-
* back file by file.
|
|
10
|
-
*
|
|
11
|
-
* Ignored folders (`node_modules`, virtualenvs, build caches) are linked
|
|
12
|
-
* into each worktree from the main checkout, so workers can build and test.
|
|
13
|
-
*/
|
|
14
|
-
import { execFile } from "node:child_process";
|
|
15
|
-
import { chmodSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readlinkSync, readdirSync, realpathSync, rmdirSync, rmSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs";
|
|
16
|
-
import { tmpdir } from "node:os";
|
|
17
|
-
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
18
|
-
|
|
19
|
-
const TIMEOUT_MS = 120_000;
|
|
20
|
-
/** The batch's own commits are never on a branch, so they carry the engine's name rather than needing the user's. */
|
|
21
|
-
const IDENTITY = { GIT_AUTHOR_NAME: "bot-lobby", GIT_AUTHOR_EMAIL: "bot-lobby@localhost", GIT_COMMITTER_NAME: "bot-lobby", GIT_COMMITTER_EMAIL: "bot-lobby@localhost" };
|
|
22
|
-
/** Ignored folders linked into a worktree, at most. */
|
|
23
|
-
const MAX_LINKS = 200;
|
|
24
|
-
|
|
25
|
-
interface GitResult {
|
|
26
|
-
stdout: string;
|
|
27
|
-
stderr: string;
|
|
28
|
-
code: number;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** Run git, resolving with its output and exit code (never rejecting on a non-zero exit). */
|
|
32
|
-
export function git(cwd: string, args: readonly string[], options: { env?: NodeJS.ProcessEnv; input?: string } = {}): Promise<GitResult> {
|
|
33
|
-
return new Promise((done) => {
|
|
34
|
-
const child = execFile("git", [...args], { cwd, timeout: TIMEOUT_MS, maxBuffer: 64 * 1024 * 1024, windowsHide: true, env: { ...process.env, ...options.env } }, (error, stdout, stderr) => {
|
|
35
|
-
const code = error ? (typeof (error as { code?: unknown }).code === "number" ? (error as { code: number }).code : 1) : 0;
|
|
36
|
-
done({ stdout: String(stdout), stderr: String(stderr), code });
|
|
37
|
-
});
|
|
38
|
-
if (options.input !== undefined) child.stdin?.end(options.input);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async function gitOk(cwd: string, args: readonly string[], options: { env?: NodeJS.ProcessEnv; input?: string } = {}): Promise<string> {
|
|
43
|
-
const result = await git(cwd, args, options);
|
|
44
|
-
if (result.code !== 0) throw new Error(result.stderr.trim().split("\n")[0]?.replace(/^(fatal|error):\s*/i, "") || `git ${args[0]} failed`);
|
|
45
|
-
return result.stdout.trim();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export interface WorkerTree {
|
|
49
|
-
/** The worktree's own folder. */
|
|
50
|
-
dir: string;
|
|
51
|
-
/** Where the worker runs: the task's folder inside the worktree. */
|
|
52
|
-
cwd: string;
|
|
53
|
-
/** Ignored folders linked in from the main checkout, relative to the worktree. */
|
|
54
|
-
links: string[];
|
|
55
|
-
/** Left on disk when the batch closes, because its change could not be saved any other way. */
|
|
56
|
-
keep?: boolean;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export interface TreeBatch {
|
|
60
|
-
/** The task tree's repository top, where changes are applied back. */
|
|
61
|
-
top: string;
|
|
62
|
-
/** The same top as the task's folder names it (through any symlink), for paths reported back. */
|
|
63
|
-
shownTop: string;
|
|
64
|
-
/** The commit (never on any branch) holding the tree as the batch began. */
|
|
65
|
-
base: string;
|
|
66
|
-
/** Where the batch's worktrees live, removed when it closes. */
|
|
67
|
-
home: string;
|
|
68
|
-
trees: Map<string, WorkerTree>;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* The working tree as a commit, untracked files included (ignored ones are
|
|
73
|
-
* not), built in a private index so the user's own index is never touched.
|
|
74
|
-
*/
|
|
75
|
-
export async function snapshotTree(top: string): Promise<string> {
|
|
76
|
-
const head = await gitOk(top, ["rev-parse", "--verify", "HEAD"]);
|
|
77
|
-
const scratch = mkdtempSync(join(tmpdir(), "bl-index-"));
|
|
78
|
-
const env = { GIT_INDEX_FILE: join(scratch, "index") };
|
|
79
|
-
try {
|
|
80
|
-
await gitOk(top, ["read-tree", head], { env });
|
|
81
|
-
await gitOk(top, ["add", "-A"], { env });
|
|
82
|
-
const tree = await gitOk(top, ["write-tree"], { env });
|
|
83
|
-
return await gitOk(top, ["commit-tree", tree, "-p", head, "-m", "bot-lobby: parallel batch base"], { env: { ...env, ...IDENTITY } });
|
|
84
|
-
} finally {
|
|
85
|
-
rmSync(scratch, { recursive: true, force: true });
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** Ignored folders of the main checkout, relative to its top (`node_modules`, `webui/node_modules`). */
|
|
90
|
-
async function ignoredFolders(top: string): Promise<string[]> {
|
|
91
|
-
const listed = await gitOk(top, ["ls-files", "--others", "--ignored", "--exclude-standard", "--directory"]).catch(() => "");
|
|
92
|
-
return listed
|
|
93
|
-
.split("\n")
|
|
94
|
-
.map((line) => line.trim())
|
|
95
|
-
.filter((line) => line.endsWith("/"))
|
|
96
|
-
.map((line) => line.slice(0, -1))
|
|
97
|
-
.filter((line) => line && !line.startsWith(".git"))
|
|
98
|
-
.slice(0, MAX_LINKS);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** Link each ignored folder of the main checkout into a worktree, where its parent exists and nothing is there yet. */
|
|
102
|
-
function linkIgnored(top: string, dir: string, folders: readonly string[]): string[] {
|
|
103
|
-
const linked: string[] = [];
|
|
104
|
-
for (const folder of folders) {
|
|
105
|
-
const target = join(dir, folder);
|
|
106
|
-
if (existsSync(target) || !existsSync(dirname(target))) continue;
|
|
107
|
-
try {
|
|
108
|
-
symlinkSync(join(top, folder), target, process.platform === "win32" ? "junction" : "dir");
|
|
109
|
-
linked.push(folder);
|
|
110
|
-
} catch {
|
|
111
|
-
// A folder that cannot be linked is left out: the worker finds it missing and says so.
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return linked;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* One worktree per worker, each a detached checkout of the tree as it stands
|
|
119
|
-
* now. Returns why not (not a repository, no commits yet) as one line when it
|
|
120
|
-
* cannot, and the batch then runs on the file desk.
|
|
121
|
-
*/
|
|
122
|
-
export async function openWorkerTrees(cwd: string, workers: readonly string[]): Promise<TreeBatch | string> {
|
|
123
|
-
let top: string;
|
|
124
|
-
try {
|
|
125
|
-
top = realpathSync(await gitOk(cwd, ["rev-parse", "--show-toplevel"]));
|
|
126
|
-
} catch {
|
|
127
|
-
return "this folder is not a git repository";
|
|
128
|
-
}
|
|
129
|
-
const realCwd = realpathSync(cwd);
|
|
130
|
-
const inside = relative(top, realCwd);
|
|
131
|
-
const folders = await ignoredFolders(top);
|
|
132
|
-
// A task folder inside a borrowed folder would put every worker back in the main checkout.
|
|
133
|
-
const borrowed = folders.find((folder) => inside === folder || inside.startsWith(`${folder}/`) || inside.startsWith(`${folder}\\`));
|
|
134
|
-
if (borrowed) return `the task's folder is inside the ignored folder ${borrowed}`;
|
|
135
|
-
let base: string;
|
|
136
|
-
try {
|
|
137
|
-
base = await snapshotTree(top);
|
|
138
|
-
} catch (error) {
|
|
139
|
-
return `git could not snapshot the working tree (${(error as Error).message})`;
|
|
140
|
-
}
|
|
141
|
-
const home = mkdtempSync(join(tmpdir(), "bl-trees-"));
|
|
142
|
-
const shownTop = resolve(cwd, relative(realCwd, top));
|
|
143
|
-
const batch: TreeBatch = { top, shownTop, base, home, trees: new Map() };
|
|
144
|
-
try {
|
|
145
|
-
for (const worker of workers) {
|
|
146
|
-
const dir = join(home, worker.replace(/[^A-Za-z0-9_-]/g, "-"));
|
|
147
|
-
await gitOk(top, ["worktree", "add", "--detach", dir, base]);
|
|
148
|
-
const links = linkIgnored(top, dir, folders);
|
|
149
|
-
const real = realpathSync(dir);
|
|
150
|
-
const workerCwd = inside && !inside.startsWith("..") ? join(real, inside) : real;
|
|
151
|
-
// An empty or untracked-only task folder is not in the snapshot; the worker still starts there.
|
|
152
|
-
mkdirSync(workerCwd, { recursive: true });
|
|
153
|
-
batch.trees.set(worker, { dir: real, cwd: workerCwd, links });
|
|
154
|
-
}
|
|
155
|
-
} catch (error) {
|
|
156
|
-
await closeWorkerTrees(batch);
|
|
157
|
-
return `git could not add a worktree (${(error as Error).message})`;
|
|
158
|
-
}
|
|
159
|
-
return batch;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/** Pathspecs leaving out a worktree's borrowed folders, matched literally from the top. */
|
|
163
|
-
function linkExcludes(tree: WorkerTree): string[] {
|
|
164
|
-
return tree.links.map((link) => `:(exclude,top,literal)${link}`);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* What one worker changed, as a commit on top of the batch's base (never on
|
|
169
|
-
* a branch). Staged from everything in its worktree except the borrowed
|
|
170
|
-
* folders, including anything the worker staged or committed itself.
|
|
171
|
-
*/
|
|
172
|
-
export async function workerCommit(batch: TreeBatch, worker: string): Promise<string> {
|
|
173
|
-
const tree = batch.trees.get(worker);
|
|
174
|
-
if (!tree) return batch.base;
|
|
175
|
-
await gitOk(tree.dir, ["add", "-A", "--", ".", ...linkExcludes(tree)]);
|
|
176
|
-
// A borrowed folder the worker staged itself is the main checkout's, never its change.
|
|
177
|
-
if (tree.links.length > 0) await gitOk(tree.dir, ["rm", "-r", "--cached", "--quiet", "--ignore-unmatch", "--", ...tree.links.map((link) => `:(top,literal)${link}`)]);
|
|
178
|
-
const written = await gitOk(tree.dir, ["write-tree"]);
|
|
179
|
-
const baseTree = await gitOk(tree.dir, ["rev-parse", `${batch.base}^{tree}`]);
|
|
180
|
-
if (written === baseTree) return batch.base;
|
|
181
|
-
return gitOk(tree.dir, ["commit-tree", written, "-p", batch.base, "-m", `bot-lobby: ${worker}'s change`], { env: IDENTITY });
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/** Paths a commit changes against the batch's base, each as it stands in the tree. */
|
|
185
|
-
async function changedPaths(batch: TreeBatch, commit: string): Promise<string[]> {
|
|
186
|
-
const listed = await gitOk(batch.top, ["diff", "--name-only", "-z", "--no-renames", batch.base, commit]);
|
|
187
|
-
return listed.split("\0").filter(Boolean);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/** Nested repositories the commit records (mode 160000): their files are not in it, only a pointer. */
|
|
191
|
-
async function gitlinks(batch: TreeBatch, commit: string): Promise<string[]> {
|
|
192
|
-
const raw = await gitOk(batch.top, ["diff", "--raw", "-z", "--no-renames", batch.base, commit]);
|
|
193
|
-
const fields = raw.split("\0");
|
|
194
|
-
const found: string[] = [];
|
|
195
|
-
for (let index = 0; index + 1 < fields.length; index += 2) {
|
|
196
|
-
const [, , newMode] = /^:(\d+) (\d+)/.exec(fields[index] ?? "") ?? [];
|
|
197
|
-
if (newMode === "160000") found.push(fields[index + 1]!);
|
|
198
|
-
}
|
|
199
|
-
return found;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
type Saved = { path: string; kind: "file"; data: Buffer; mode: number } | { path: string; kind: "link"; target: string } | { path: string; kind: "other" } | { path: string; kind: "none"; madeDirs: string[] };
|
|
203
|
-
|
|
204
|
-
/** Folders that would be made to hold `path`, nearest last, so a rollback can take them away again. */
|
|
205
|
-
function missingParents(top: string, path: string): string[] {
|
|
206
|
-
const missing: string[] = [];
|
|
207
|
-
for (let dir = dirname(join(top, path)); dir.length > top.length && !existsSync(dir); dir = dirname(dir)) missing.unshift(dir);
|
|
208
|
-
return missing;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/** The current contents of each path a patch is about to touch. */
|
|
212
|
-
function saveFiles(top: string, paths: readonly string[]): Saved[] {
|
|
213
|
-
return paths.map((path): Saved => {
|
|
214
|
-
const full = join(top, path);
|
|
215
|
-
let stat;
|
|
216
|
-
try {
|
|
217
|
-
stat = lstatSync(full);
|
|
218
|
-
} catch {
|
|
219
|
-
return { path, kind: "none", madeDirs: missingParents(top, path) };
|
|
220
|
-
}
|
|
221
|
-
if (stat.isSymbolicLink()) return { path, kind: "link", target: readlinkSync(full) };
|
|
222
|
-
if (stat.isFile()) return { path, kind: "file", data: readFileSync(full), mode: stat.mode };
|
|
223
|
-
return { path, kind: "other" };
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
function sameAsSaved(top: string, saved: Saved): boolean {
|
|
228
|
-
const full = join(top, saved.path);
|
|
229
|
-
try {
|
|
230
|
-
const stat = lstatSync(full);
|
|
231
|
-
if (saved.kind === "none") return false;
|
|
232
|
-
if (saved.kind === "link") return stat.isSymbolicLink() && readlinkSync(full) === saved.target;
|
|
233
|
-
if (saved.kind === "file") return stat.isFile() && (stat.mode & 0o777) === (saved.mode & 0o777) && readFileSync(full).equals(saved.data);
|
|
234
|
-
return true;
|
|
235
|
-
} catch {
|
|
236
|
-
return saved.kind === "none";
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/** Put each saved path back as it was; returns the paths that could not be. */
|
|
241
|
-
function restoreFiles(top: string, saved: readonly Saved[]): string[] {
|
|
242
|
-
for (const entry of saved) {
|
|
243
|
-
const full = join(top, entry.path);
|
|
244
|
-
try {
|
|
245
|
-
if (sameAsSaved(top, entry) || entry.kind === "other") continue;
|
|
246
|
-
if (existsSync(full) || isLink(full)) rmSync(full, { force: true });
|
|
247
|
-
if (entry.kind === "none") {
|
|
248
|
-
for (const dir of [...entry.madeDirs].reverse()) {
|
|
249
|
-
if (existsSync(dir) && readdirSync(dir).length === 0) rmdirSync(dir);
|
|
250
|
-
}
|
|
251
|
-
continue;
|
|
252
|
-
}
|
|
253
|
-
mkdirSync(dirname(full), { recursive: true });
|
|
254
|
-
if (entry.kind === "link") symlinkSync(entry.target, full);
|
|
255
|
-
else {
|
|
256
|
-
writeFileSync(full, entry.data);
|
|
257
|
-
chmodSync(full, entry.mode & 0o7777);
|
|
258
|
-
}
|
|
259
|
-
} catch {
|
|
260
|
-
// Reported below by the check.
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
return saved.filter((entry) => !sameAsSaved(top, entry)).map((entry) => entry.path);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
function isLink(path: string): boolean {
|
|
267
|
-
try {
|
|
268
|
-
return lstatSync(path).isSymbolicLink();
|
|
269
|
-
} catch {
|
|
270
|
-
return false;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
export type MergeOutcome =
|
|
275
|
-
| { worker: string; status: "applied" | "empty" }
|
|
276
|
-
| {
|
|
277
|
-
worker: string;
|
|
278
|
-
/** `conflict`: none of it is in the tree; `partial`: applying failed and some files could not be put back. */
|
|
279
|
-
status: "conflict" | "partial";
|
|
280
|
-
/** The change as a patch file ("" when it could not be written). */
|
|
281
|
-
patch: string;
|
|
282
|
-
/** A git ref holding the change as a commit, or the worktree kept on disk when not even that could be made. */
|
|
283
|
-
saved: string;
|
|
284
|
-
reason: string;
|
|
285
|
-
/** For `partial`: the files left changed. */
|
|
286
|
-
leftChanged?: string[];
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
function firstLines(text: string): string {
|
|
290
|
-
return text.trim().split("\n").slice(0, 3).join(" ");
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* Apply each worker's change back to the task's tree, in order. A change
|
|
295
|
-
* that does not apply cleanly is saved (a patch under `patchDir` and a ref
|
|
296
|
-
* under `refs/bot-lobby/`) and reported; when applying fails part way, the
|
|
297
|
-
* files it touched are put back as they were. A worker whose change could not
|
|
298
|
-
* even be read keeps its worktree, which `closeWorkerTrees` then leaves alone.
|
|
299
|
-
*/
|
|
300
|
-
export async function mergeWorkerTrees(batch: TreeBatch, order: readonly string[], patchDir: string): Promise<MergeOutcome[]> {
|
|
301
|
-
const outcomes: MergeOutcome[] = [];
|
|
302
|
-
const stamp = Date.now();
|
|
303
|
-
for (const worker of order) {
|
|
304
|
-
const tree = batch.trees.get(worker);
|
|
305
|
-
let commit: string;
|
|
306
|
-
try {
|
|
307
|
-
commit = await workerCommit(batch, worker);
|
|
308
|
-
} catch (error) {
|
|
309
|
-
if (tree) tree.keep = true;
|
|
310
|
-
outcomes.push({ worker, status: "conflict", patch: "", saved: tree?.dir ?? "", reason: `its change could not be read (${(error as Error).message}); its worktree is kept as it was` });
|
|
311
|
-
continue;
|
|
312
|
-
}
|
|
313
|
-
if (commit === batch.base) {
|
|
314
|
-
outcomes.push({ worker, status: "empty" });
|
|
315
|
-
continue;
|
|
316
|
-
}
|
|
317
|
-
const ref = `refs/bot-lobby/${worker.replace(/[^A-Za-z0-9_-]/g, "-")}-${stamp}`;
|
|
318
|
-
const keep = async (reason: string, status: "conflict" | "partial" = "conflict", leftChanged?: string[]): Promise<void> => {
|
|
319
|
-
let saved = ref;
|
|
320
|
-
if ((await git(batch.top, ["update-ref", ref, commit])).code !== 0 && tree) {
|
|
321
|
-
tree.keep = true;
|
|
322
|
-
saved = tree.dir;
|
|
323
|
-
}
|
|
324
|
-
let patch = "";
|
|
325
|
-
try {
|
|
326
|
-
mkdirSync(patchDir, { recursive: true });
|
|
327
|
-
const file = join(patchDir, `${worker}-${stamp}.patch`);
|
|
328
|
-
if ((await git(batch.top, ["diff", "--binary", "--no-renames", `--output=${file}`, batch.base, commit])).code === 0) patch = file;
|
|
329
|
-
} catch {
|
|
330
|
-
// The ref still holds it.
|
|
331
|
-
}
|
|
332
|
-
outcomes.push({ worker, status, patch, saved, reason, ...(leftChanged ? { leftChanged } : {}) });
|
|
333
|
-
};
|
|
334
|
-
try {
|
|
335
|
-
const nested = await gitlinks(batch, commit);
|
|
336
|
-
if (nested.length > 0) {
|
|
337
|
-
if (tree) tree.keep = true;
|
|
338
|
-
await keep(`it made a nested git repository (${nested.slice(0, 3).join(", ")}), whose files cannot be carried over as a change; its worktree is kept`);
|
|
339
|
-
continue;
|
|
340
|
-
}
|
|
341
|
-
const paths = await changedPaths(batch, commit);
|
|
342
|
-
const scratch = mkdtempSync(join(tmpdir(), "bl-patch-"));
|
|
343
|
-
const file = join(scratch, "change.patch");
|
|
344
|
-
try {
|
|
345
|
-
await gitOk(batch.top, ["diff", "--binary", "--no-renames", `--output=${file}`, batch.base, commit]);
|
|
346
|
-
const check = await git(batch.top, ["apply", "--check", "--binary", "--whitespace=nowarn", file]);
|
|
347
|
-
if (check.code !== 0) {
|
|
348
|
-
await keep(firstLines(check.stderr) || "it does not apply to the tree as the other workers left it");
|
|
349
|
-
continue;
|
|
350
|
-
}
|
|
351
|
-
const before = saveFiles(batch.top, paths);
|
|
352
|
-
const applied = await git(batch.top, ["apply", "--binary", "--whitespace=nowarn", file]);
|
|
353
|
-
if (applied.code === 0) {
|
|
354
|
-
outcomes.push({ worker, status: "applied" });
|
|
355
|
-
continue;
|
|
356
|
-
}
|
|
357
|
-
const left = restoreFiles(batch.top, before);
|
|
358
|
-
const reason = firstLines(applied.stderr) || "git could not apply it";
|
|
359
|
-
if (left.length === 0) await keep(reason);
|
|
360
|
-
else await keep(`${reason}; ${left.length} file(s) it touched could not be put back`, "partial", left);
|
|
361
|
-
} finally {
|
|
362
|
-
rmSync(scratch, { recursive: true, force: true });
|
|
363
|
-
}
|
|
364
|
-
} catch (error) {
|
|
365
|
-
await keep(`its change could not be applied (${(error as Error).message})`);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
return outcomes;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/** A path a worker edited inside its worktree, as the same path in the task's tree. */
|
|
372
|
-
export function mapToTop(batch: TreeBatch, worker: string, path: string): string {
|
|
373
|
-
const tree = batch.trees.get(worker);
|
|
374
|
-
if (!tree) return path;
|
|
375
|
-
const rel = relative(tree.dir, path);
|
|
376
|
-
return rel && !rel.startsWith("..") && !isAbsolute(rel) ? join(batch.shownTop, rel) : path;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
/** Remove every worktree of the batch and its folder, except one kept to save its change. Never throws. */
|
|
380
|
-
export async function closeWorkerTrees(batch: TreeBatch): Promise<void> {
|
|
381
|
-
let kept = false;
|
|
382
|
-
for (const tree of batch.trees.values()) {
|
|
383
|
-
// Unlink the borrowed folders first, so removing the worktree can never reach into the main checkout's.
|
|
384
|
-
for (const link of tree.links) {
|
|
385
|
-
try {
|
|
386
|
-
unlinkSync(join(tree.dir, link));
|
|
387
|
-
} catch {
|
|
388
|
-
// Already gone.
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
if (tree.keep) kept = true;
|
|
392
|
-
else await git(batch.top, ["worktree", "remove", "--force", tree.dir]);
|
|
393
|
-
}
|
|
394
|
-
if (!kept) rmSync(batch.home, { recursive: true, force: true });
|
|
395
|
-
await git(batch.top, ["worktree", "prune"]);
|
|
396
|
-
}
|
package/src/lobby/issues.ts
DELETED
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* GitHub issues through the `gh` CLI: list the repository's open issues, read
|
|
3
|
-
* one with its comments, and file a new one. `gh` owns authentication and repo
|
|
4
|
-
* detection, so bot-lobby holds no token; every failure becomes one readable
|
|
5
|
-
* line (not installed, not signed in, not a GitHub repository).
|
|
6
|
-
*/
|
|
7
|
-
import { execFile } from "node:child_process";
|
|
8
|
-
|
|
9
|
-
export interface ExecResult {
|
|
10
|
-
stdout: string;
|
|
11
|
-
stderr: string;
|
|
12
|
-
code: number;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** Runs a command without a shell; `execCommand` in the extension, a fake in tests. */
|
|
16
|
-
export type Exec = (command: string, args: string[], options?: { cwd?: string; timeout?: number }) => Promise<ExecResult>;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* `execFile` as an `Exec`: a non-zero exit resolves with its code and output,
|
|
20
|
-
* while a missing binary rejects with the spawn error (`ENOENT`), so the tab
|
|
21
|
-
* can say "gh is not installed" rather than "exited with code 1".
|
|
22
|
-
*/
|
|
23
|
-
export const execCommand: Exec = (command, args, options) =>
|
|
24
|
-
new Promise((resolve, reject) => {
|
|
25
|
-
execFile(command, args, { cwd: options?.cwd, timeout: options?.timeout, maxBuffer: 10 * 1024 * 1024, windowsHide: true }, (error, stdout, stderr) => {
|
|
26
|
-
if (error && typeof (error as NodeJS.ErrnoException).code === "string") return reject(error);
|
|
27
|
-
const code = error ? (typeof (error as { code?: unknown }).code === "number" ? (error as { code: number }).code : 1) : 0;
|
|
28
|
-
resolve({ stdout: String(stdout), stderr: String(stderr), code });
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
export interface IssueSummary {
|
|
33
|
-
number: number;
|
|
34
|
-
title: string;
|
|
35
|
-
labels: string[];
|
|
36
|
-
author?: string;
|
|
37
|
-
updatedAt?: string;
|
|
38
|
-
url?: string;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface IssueComment {
|
|
42
|
-
author?: string;
|
|
43
|
-
body: string;
|
|
44
|
-
createdAt?: string;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface IssueDetail extends IssueSummary {
|
|
48
|
-
body: string;
|
|
49
|
-
state?: string;
|
|
50
|
-
comments: IssueComment[];
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const TIMEOUT_MS = 20_000;
|
|
54
|
-
export const LIST_LIMIT = 50;
|
|
55
|
-
|
|
56
|
-
/** One readable line for a failed `gh` call. */
|
|
57
|
-
export function ghError(result: Partial<ExecResult> & { error?: string }): string {
|
|
58
|
-
const text = `${result.error ?? ""}\n${result.stderr ?? ""}`.trim();
|
|
59
|
-
if (result.code === 127 || /ENOENT|not found|command not found/i.test(text)) return "GitHub CLI (gh) is not installed — install it from https://cli.github.com and run `gh auth login`.";
|
|
60
|
-
if (/auth login|not logged|authentication|GH_TOKEN/i.test(text)) return "gh is not signed in — run `gh auth login` in a terminal.";
|
|
61
|
-
if (/not a git repository|no git remotes|could not determine|none of the git remotes/i.test(text)) return "This project has no GitHub remote gh can use.";
|
|
62
|
-
const first = text.split("\n").find((line) => line.trim()) ?? `gh exited with code ${result.code ?? "?"}`;
|
|
63
|
-
return first.length > 160 ? `${first.slice(0, 159)}…` : first;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export async function gh(exec: Exec, cwd: string, args: string[]): Promise<string> {
|
|
67
|
-
let result: ExecResult;
|
|
68
|
-
try {
|
|
69
|
-
result = await exec("gh", args, { cwd, timeout: TIMEOUT_MS });
|
|
70
|
-
} catch (error) {
|
|
71
|
-
throw new Error(ghError({ error: (error as Error).message }));
|
|
72
|
-
}
|
|
73
|
-
if (result.code !== 0) throw new Error(ghError(result));
|
|
74
|
-
return result.stdout;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
interface RawIssue {
|
|
78
|
-
number?: unknown;
|
|
79
|
-
title?: unknown;
|
|
80
|
-
body?: unknown;
|
|
81
|
-
state?: unknown;
|
|
82
|
-
url?: unknown;
|
|
83
|
-
updatedAt?: unknown;
|
|
84
|
-
author?: { login?: unknown } | null;
|
|
85
|
-
labels?: Array<{ name?: unknown }> | null;
|
|
86
|
-
comments?: Array<{ author?: { login?: unknown } | null; body?: unknown; createdAt?: unknown }> | null;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function str(value: unknown): string | undefined {
|
|
90
|
-
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function summary(raw: RawIssue): IssueSummary | undefined {
|
|
94
|
-
if (typeof raw.number !== "number" || typeof raw.title !== "string") return undefined;
|
|
95
|
-
const author = str(raw.author?.login);
|
|
96
|
-
const updatedAt = str(raw.updatedAt);
|
|
97
|
-
const url = str(raw.url);
|
|
98
|
-
return {
|
|
99
|
-
number: raw.number,
|
|
100
|
-
title: raw.title,
|
|
101
|
-
labels: (raw.labels ?? []).map((label) => str(label?.name)).filter((name): name is string => Boolean(name)),
|
|
102
|
-
...(author ? { author } : {}),
|
|
103
|
-
...(updatedAt ? { updatedAt } : {}),
|
|
104
|
-
...(url ? { url } : {}),
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function parseJson<T>(stdout: string): T {
|
|
109
|
-
try {
|
|
110
|
-
return JSON.parse(stdout) as T;
|
|
111
|
-
} catch {
|
|
112
|
-
throw new Error("gh returned output bot-lobby could not read");
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export async function listIssues(exec: Exec, cwd: string, limit = LIST_LIMIT): Promise<IssueSummary[]> {
|
|
117
|
-
const stdout = await gh(exec, cwd, ["issue", "list", "--state", "open", "--limit", String(limit), "--json", "number,title,labels,author,updatedAt,url"]);
|
|
118
|
-
const raw = parseJson<RawIssue[]>(stdout);
|
|
119
|
-
return (Array.isArray(raw) ? raw : []).map(summary).filter((issue): issue is IssueSummary => Boolean(issue));
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export async function viewIssue(exec: Exec, cwd: string, number: number): Promise<IssueDetail> {
|
|
123
|
-
const stdout = await gh(exec, cwd, ["issue", "view", String(number), "--json", "number,title,body,state,labels,author,url,updatedAt,comments"]);
|
|
124
|
-
const raw = parseJson<RawIssue>(stdout);
|
|
125
|
-
const base = summary(raw);
|
|
126
|
-
if (!base) throw new Error(`gh returned no issue #${number}`);
|
|
127
|
-
const state = str(raw.state);
|
|
128
|
-
return {
|
|
129
|
-
...base,
|
|
130
|
-
body: typeof raw.body === "string" ? raw.body : "",
|
|
131
|
-
...(state ? { state } : {}),
|
|
132
|
-
comments: (raw.comments ?? []).map((comment) => {
|
|
133
|
-
const author = str(comment?.author?.login);
|
|
134
|
-
const createdAt = str(comment?.createdAt);
|
|
135
|
-
return { body: typeof comment?.body === "string" ? comment.body : "", ...(author ? { author } : {}), ...(createdAt ? { createdAt } : {}) };
|
|
136
|
-
}),
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/** First non-empty line is the title, the rest is the body. */
|
|
141
|
-
export function splitIssueText(text: string): { title: string; body: string } {
|
|
142
|
-
const lines = text.replace(/\r\n/g, "\n").split("\n");
|
|
143
|
-
const first = lines.findIndex((line) => line.trim());
|
|
144
|
-
if (first < 0) return { title: "", body: "" };
|
|
145
|
-
return { title: lines[first]!.trim().replace(/^#+\s*/, ""), body: lines.slice(first + 1).join("\n").trim() };
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/** File an issue; returns its number and URL as `gh` prints it. */
|
|
149
|
-
export async function createIssue(exec: Exec, cwd: string, title: string, body: string): Promise<{ number?: number; url: string }> {
|
|
150
|
-
if (!title.trim()) throw new Error("an issue needs a title (the first line)");
|
|
151
|
-
const stdout = await gh(exec, cwd, ["issue", "create", "--title", title.trim(), "--body", body.trim() || " "]);
|
|
152
|
-
const url = stdout.trim().split("\n").reverse().find((line) => /^https?:\/\//.test(line.trim()))?.trim() ?? stdout.trim();
|
|
153
|
-
const number = Number(/\/issues\/(\d+)/.exec(url)?.[1]);
|
|
154
|
-
return { url, ...(Number.isFinite(number) ? { number } : {}) };
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/** Lobby state for the Issues tab: the list, the open issue, and what is loading. */
|
|
158
|
-
export class IssuesState {
|
|
159
|
-
issues: IssueSummary[] = [];
|
|
160
|
-
details = new Map<number, IssueDetail>();
|
|
161
|
-
loading = false;
|
|
162
|
-
loaded = false;
|
|
163
|
-
error?: string;
|
|
164
|
-
notice?: string;
|
|
165
|
-
private readonly exec: Exec;
|
|
166
|
-
private readonly cwd: string;
|
|
167
|
-
private readonly onChange: () => void;
|
|
168
|
-
|
|
169
|
-
constructor(exec: Exec, cwd: string, onChange: () => void = () => {}) {
|
|
170
|
-
this.exec = exec;
|
|
171
|
-
this.cwd = cwd;
|
|
172
|
-
this.onChange = onChange;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
async refresh(): Promise<void> {
|
|
176
|
-
if (this.loading) return;
|
|
177
|
-
this.loading = true;
|
|
178
|
-
this.error = undefined;
|
|
179
|
-
this.onChange();
|
|
180
|
-
try {
|
|
181
|
-
this.issues = await listIssues(this.exec, this.cwd);
|
|
182
|
-
this.loaded = true;
|
|
183
|
-
} catch (error) {
|
|
184
|
-
this.error = (error as Error).message;
|
|
185
|
-
} finally {
|
|
186
|
-
this.loading = false;
|
|
187
|
-
this.onChange();
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
async detail(number: number): Promise<IssueDetail | undefined> {
|
|
192
|
-
const cached = this.details.get(number);
|
|
193
|
-
if (cached) return cached;
|
|
194
|
-
try {
|
|
195
|
-
const detail = await viewIssue(this.exec, this.cwd, number);
|
|
196
|
-
this.details.set(number, detail);
|
|
197
|
-
this.onChange();
|
|
198
|
-
return detail;
|
|
199
|
-
} catch (error) {
|
|
200
|
-
this.error = (error as Error).message;
|
|
201
|
-
this.onChange();
|
|
202
|
-
return undefined;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async create(text: string): Promise<void> {
|
|
207
|
-
const { title, body } = splitIssueText(text);
|
|
208
|
-
try {
|
|
209
|
-
const created = await createIssue(this.exec, this.cwd, title, body);
|
|
210
|
-
this.notice = `created ${created.number ? `#${created.number}` : "issue"} — ${created.url}`;
|
|
211
|
-
this.error = undefined;
|
|
212
|
-
this.onChange();
|
|
213
|
-
await this.refresh();
|
|
214
|
-
} catch (error) {
|
|
215
|
-
this.error = (error as Error).message;
|
|
216
|
-
this.onChange();
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|