pi-roundtable-coding 0.42.0 → 0.44.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/CHANGELOG.md +20 -0
- package/README.md +6 -6
- package/package.json +3 -3
- package/src/coding-desk.ts +94 -407
- package/src/coding-options.ts +21 -4
- package/src/coding-plugin.ts +63 -54
- package/src/desk-modes.ts +107 -0
- package/src/desk-run.ts +279 -0
- package/src/desk-types.ts +5 -0
- package/src/git-run.ts +120 -0
- package/src/index.ts +1 -0
- package/src/pi-coding-worker.ts +26 -3
- package/src/repo-gate.ts +77 -0
- package/src/repo-metadata.ts +72 -0
- package/src/repo-report.ts +16 -0
- package/src/repo-shelf.ts +55 -438
- package/src/repo-types.ts +75 -0
- package/src/task-control.ts +31 -0
- package/src/task-tools.ts +7 -25
- package/src/tool-text.ts +54 -0
- package/src/worker-entry.ts +49 -3
- package/src/worktrees.ts +248 -0
package/src/repo-shelf.ts
CHANGED
|
@@ -1,258 +1,51 @@
|
|
|
1
1
|
import {
|
|
2
|
-
closeSync,
|
|
3
|
-
constants,
|
|
4
2
|
existsSync,
|
|
5
|
-
fstatSync,
|
|
6
3
|
lstatSync,
|
|
7
4
|
mkdirSync,
|
|
8
|
-
openSync,
|
|
9
5
|
readdirSync,
|
|
10
|
-
readSync,
|
|
11
6
|
realpathSync,
|
|
12
7
|
renameSync,
|
|
13
|
-
rmSync,
|
|
14
8
|
} from "node:fs";
|
|
15
9
|
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
16
|
-
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
branch: string;
|
|
42
|
-
/** Commits ahead of and behind its upstream, as of the last fetch; absent without one. */
|
|
43
|
-
upstream?: { ahead: number; behind: number };
|
|
44
|
-
uncommitted: number;
|
|
45
|
-
/** `<short sha> <subject> (<date>)`, absent in an empty repository. */
|
|
46
|
-
lastCommit?: string;
|
|
47
|
-
summary?: string;
|
|
48
|
-
ciOnPush: boolean;
|
|
49
|
-
/** Set when git could not describe it. */
|
|
50
|
-
error?: string;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** A clone's state, measured around a coding worker's run (behavior 8). */
|
|
54
|
-
export interface RepoState {
|
|
55
|
-
branch: string;
|
|
56
|
-
head: string;
|
|
57
|
-
/** `git status --porcelain` lines. */
|
|
58
|
-
uncommitted: string[];
|
|
59
|
-
}
|
|
10
|
+
import { AgentError, checkRepoName } from "pi-roundtable/kit";
|
|
11
|
+
import { GitRunner, ghCloneWith } from "./git-run.ts";
|
|
12
|
+
import { ciOnPush, summaryOf } from "./repo-metadata.ts";
|
|
13
|
+
import type {
|
|
14
|
+
ChangeReport,
|
|
15
|
+
CloneCommand,
|
|
16
|
+
GithubToken,
|
|
17
|
+
Integration,
|
|
18
|
+
RepoState,
|
|
19
|
+
RepoSummary,
|
|
20
|
+
WorkerTree,
|
|
21
|
+
} from "./repo-types.ts";
|
|
22
|
+
import { WorkerTrees } from "./worktrees.ts";
|
|
23
|
+
|
|
24
|
+
export { reportPost } from "./repo-report.ts";
|
|
25
|
+
export type {
|
|
26
|
+
ChangeReport,
|
|
27
|
+
CloneCommand,
|
|
28
|
+
GithubToken,
|
|
29
|
+
Integration,
|
|
30
|
+
RepoState,
|
|
31
|
+
RepoSummary,
|
|
32
|
+
WorkerTree,
|
|
33
|
+
} from "./repo-types.ts";
|
|
34
|
+
export { WORKTREES_DIR } from "./repo-types.ts";
|
|
60
35
|
|
|
61
|
-
/**
|
|
62
|
-
* The GitHub token git and gh calls get as GH_TOKEN, or a function read at each call; undefined
|
|
63
|
-
* leaves them to gh's own login.
|
|
64
|
-
*/
|
|
65
|
-
export type GithubToken = string | undefined | (() => string | undefined);
|
|
66
|
-
|
|
67
|
-
export type CloneCommand = (repo: string, dir: string) => Promise<void>;
|
|
68
|
-
|
|
69
|
-
/** A coding worker's own checkout: a linked worktree on its own branch, cut from the clone's HEAD. */
|
|
70
|
-
export interface WorkerTree {
|
|
71
|
-
repo: string;
|
|
72
|
-
/** The worktree's real path, outside the shelf's list of managed repositories. */
|
|
73
|
-
dir: string;
|
|
74
|
-
/** The worker's branch, such as `worker/3`. */
|
|
75
|
-
branch: string;
|
|
76
|
-
/** The clone's full HEAD the branch was cut from. */
|
|
77
|
-
base: string;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** What bringing a worker's branch back into its clone did. */
|
|
81
|
-
export type Integration =
|
|
82
|
-
/** The branch had no commits beyond the clone; the worktree and branch are gone. */
|
|
83
|
-
| { status: "nothing" }
|
|
84
|
-
/** Rebased onto the clone's branch and fast-forwarded it; the worktree and branch are gone. */
|
|
85
|
-
| { status: "merged"; branch: string; commits: string[] }
|
|
86
|
-
/** The rebase stopped on these files; it was aborted, and the branch and worktree are kept. */
|
|
87
|
-
| { status: "conflict"; branch: string; dir: string; files: string[] }
|
|
88
|
-
/** The clone or the worktree had uncommitted changes, or the clone moved; nothing was touched. */
|
|
89
|
-
| {
|
|
90
|
-
status: "refused";
|
|
91
|
-
branch: string;
|
|
92
|
-
dir: string;
|
|
93
|
-
reason: string;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
/** The folder under the shelf that holds worker worktrees; the shelf lists no repository there. */
|
|
97
|
-
export const WORKTREES_DIR = ".worktrees";
|
|
98
|
-
|
|
99
|
-
const GIT_TIMEOUT_MS = 60_000;
|
|
100
36
|
const MAX_STAT_FILES = 40;
|
|
101
|
-
const MAX_SUMMARY_CHARS = 120;
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Host variables git and gh need besides the shell's: their config files and homes, the identity a
|
|
105
|
-
* rebase commits as, ssh agent and ssh command, and gh's host; never a token, which comes from
|
|
106
|
-
* `githubToken`.
|
|
107
|
-
*/
|
|
108
|
-
const GIT_ENV_NAMES = [
|
|
109
|
-
"XDG_CONFIG_HOME",
|
|
110
|
-
"GIT_CONFIG_GLOBAL",
|
|
111
|
-
"GIT_CONFIG_SYSTEM",
|
|
112
|
-
"GIT_CONFIG_NOSYSTEM",
|
|
113
|
-
"GIT_AUTHOR_NAME",
|
|
114
|
-
"GIT_AUTHOR_EMAIL",
|
|
115
|
-
"GIT_COMMITTER_NAME",
|
|
116
|
-
"GIT_COMMITTER_EMAIL",
|
|
117
|
-
"GH_CONFIG_DIR",
|
|
118
|
-
"GH_HOST",
|
|
119
|
-
"SSH_AUTH_SOCK",
|
|
120
|
-
"GIT_SSH",
|
|
121
|
-
"GIT_SSH_COMMAND",
|
|
122
|
-
];
|
|
123
37
|
|
|
124
|
-
/**
|
|
125
|
-
function gitEnv(token: string | undefined): Record<string, string> {
|
|
126
|
-
return childEnv(process.env, {
|
|
127
|
-
pass: GIT_ENV_NAMES,
|
|
128
|
-
...(token ? { set: { GH_TOKEN: token } } : {}),
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
async function run(
|
|
133
|
-
command: string[],
|
|
134
|
-
cwd: string | undefined,
|
|
135
|
-
env: Record<string, string>,
|
|
136
|
-
): Promise<{ out: string; err: string; code: number }> {
|
|
137
|
-
const safeCommand =
|
|
138
|
-
command[0] === "git"
|
|
139
|
-
? [
|
|
140
|
-
"git",
|
|
141
|
-
"-c",
|
|
142
|
-
"core.fsmonitor=false",
|
|
143
|
-
"-c",
|
|
144
|
-
"core.hooksPath=/dev/null",
|
|
145
|
-
...command.slice(1),
|
|
146
|
-
]
|
|
147
|
-
: command;
|
|
148
|
-
const child = Bun.spawn(safeCommand, {
|
|
149
|
-
...(cwd ? { cwd } : {}),
|
|
150
|
-
env,
|
|
151
|
-
stdout: "pipe",
|
|
152
|
-
stderr: "pipe",
|
|
153
|
-
timeout: GIT_TIMEOUT_MS,
|
|
154
|
-
});
|
|
155
|
-
const [out, err, code] = await Promise.all([
|
|
156
|
-
new Response(child.stdout).text(),
|
|
157
|
-
new Response(child.stderr).text(),
|
|
158
|
-
child.exited,
|
|
159
|
-
]);
|
|
160
|
-
return { out: out.trimEnd(), err: err.trim(), code };
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Clones with gh, which authenticates with `token` as GH_TOKEN (default the host's GH_TOKEN, read
|
|
165
|
-
* at each clone) or its own login; a failure's stderr is cut at `diagnosticChars`.
|
|
166
|
-
*/
|
|
167
|
-
const ghCloneWith =
|
|
168
|
-
(diagnosticChars?: number, token?: () => string | undefined): CloneCommand =>
|
|
169
|
-
async (repo, dir) => {
|
|
170
|
-
checkRepoName(repo);
|
|
171
|
-
if (repo.startsWith("-"))
|
|
172
|
-
throw new AgentError("Repository owner cannot start with a dash.");
|
|
173
|
-
const { err, code } = await run(
|
|
174
|
-
["gh", "repo", "clone", repo, dir, "--", "--quiet"],
|
|
175
|
-
undefined,
|
|
176
|
-
gitEnv(token ? token() : process.env.GH_TOKEN),
|
|
177
|
-
);
|
|
178
|
-
if (code !== 0)
|
|
179
|
-
throw new AgentError(
|
|
180
|
-
`Cloning ${repo} failed: ${scrubDiagnostic(err, diagnosticChars) || `exit ${code}`}`,
|
|
181
|
-
);
|
|
182
|
-
};
|
|
38
|
+
/** Clones with gh and the host's token; see `ghCloneWith`. */
|
|
183
39
|
export const ghClone: CloneCommand = ghCloneWith();
|
|
184
40
|
|
|
185
|
-
const MAX_METADATA_BYTES = 65_536;
|
|
186
|
-
|
|
187
|
-
/** Metadata discovery never follows links or opens devices/FIFOs; each file is bounded. */
|
|
188
|
-
function metadataText(path: string): string | undefined {
|
|
189
|
-
let fd: number | undefined;
|
|
190
|
-
try {
|
|
191
|
-
if (!lstatSync(path).isFile()) return undefined;
|
|
192
|
-
fd = openSync(
|
|
193
|
-
path,
|
|
194
|
-
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
|
|
195
|
-
);
|
|
196
|
-
if (!fstatSync(fd).isFile()) return undefined;
|
|
197
|
-
const bytes = Buffer.alloc(MAX_METADATA_BYTES);
|
|
198
|
-
const count = readSync(fd, bytes, 0, bytes.length, 0);
|
|
199
|
-
return bytes.subarray(0, count).toString("utf8");
|
|
200
|
-
} catch {
|
|
201
|
-
return undefined;
|
|
202
|
-
} finally {
|
|
203
|
-
if (fd !== undefined) closeSync(fd);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/** The first prose line of a README or AGENTS.md: no frontmatter, heading, badge, or markup. */
|
|
208
|
-
function summaryOf(dir: string): string | undefined {
|
|
209
|
-
for (const name of ["README.md", "AGENTS.md"]) {
|
|
210
|
-
const file = join(dir, name);
|
|
211
|
-
const content = metadataText(file);
|
|
212
|
-
if (!content) continue;
|
|
213
|
-
const text = content.replace(/^---\n[\s\S]*?\n---\n/, "");
|
|
214
|
-
const line = text
|
|
215
|
-
.split("\n")
|
|
216
|
-
.map((l) => l.trim())
|
|
217
|
-
.find((l) => /\p{L}/u.test(l) && !/^([#!<>|`[\-*=]|\d+\.)/.test(l));
|
|
218
|
-
if (line)
|
|
219
|
-
return line.length > MAX_SUMMARY_CHARS
|
|
220
|
-
? `${line.slice(0, MAX_SUMMARY_CHARS - 1)}…`
|
|
221
|
-
: line;
|
|
222
|
-
}
|
|
223
|
-
return undefined;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function ciOnPush(dir: string): boolean {
|
|
227
|
-
const workflows = join(dir, ".github", "workflows");
|
|
228
|
-
try {
|
|
229
|
-
if (
|
|
230
|
-
!lstatSync(join(dir, ".github")).isDirectory() ||
|
|
231
|
-
!lstatSync(workflows).isDirectory()
|
|
232
|
-
)
|
|
233
|
-
return false;
|
|
234
|
-
return readdirSync(workflows)
|
|
235
|
-
.filter((name) => /\.ya?ml$/.test(name))
|
|
236
|
-
.some((name) =>
|
|
237
|
-
/^\s*(on:\s*\[?[^\n]*\bpush\b|push\s*:)/m.test(
|
|
238
|
-
metadataText(join(workflows, name)) ?? "",
|
|
239
|
-
),
|
|
240
|
-
);
|
|
241
|
-
} catch {
|
|
242
|
-
return false;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
41
|
/** The managed repositories under one folder, read and pushed through git. */
|
|
247
42
|
export class RepoShelf {
|
|
248
43
|
readonly dir: string;
|
|
249
44
|
readonly #clone: CloneCommand;
|
|
250
45
|
readonly #reports = new Map<string, ChangeReport>();
|
|
251
46
|
readonly #adding = new Set<string>();
|
|
252
|
-
|
|
253
|
-
readonly #
|
|
254
|
-
|
|
255
|
-
readonly #token: () => string | undefined;
|
|
47
|
+
readonly #runner: GitRunner;
|
|
48
|
+
readonly #trees: WorkerTrees;
|
|
256
49
|
|
|
257
50
|
constructor(
|
|
258
51
|
dir: string,
|
|
@@ -263,19 +56,16 @@ export class RepoShelf {
|
|
|
263
56
|
} = {},
|
|
264
57
|
) {
|
|
265
58
|
this.dir = resolve(dir);
|
|
266
|
-
this.#diagnosticChars = options.diagnosticChars;
|
|
267
59
|
const { githubToken = process.env.GH_TOKEN } = options;
|
|
268
|
-
|
|
60
|
+
const token =
|
|
269
61
|
typeof githubToken === "function" ? githubToken : () => githubToken;
|
|
270
|
-
this.#
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
): Promise<{ out: string; err: string; code: number }> {
|
|
278
|
-
return run(command, cwd, gitEnv(this.#token()));
|
|
62
|
+
this.#runner = new GitRunner(token, options.diagnosticChars);
|
|
63
|
+
this.#trees = new WorkerTrees(
|
|
64
|
+
this.dir,
|
|
65
|
+
(repo) => this.dirOf(repo),
|
|
66
|
+
this.#runner,
|
|
67
|
+
);
|
|
68
|
+
this.#clone = clone ?? ghCloneWith(options.diagnosticChars, token);
|
|
279
69
|
}
|
|
280
70
|
|
|
281
71
|
/** Adopt an existing standalone clone at startup; an existing destination is never replaced. */
|
|
@@ -492,44 +282,8 @@ export class RepoShelf {
|
|
|
492
282
|
* Cuts a worktree for coding job `id` from the clone's HEAD, on a fresh branch `worker/<id>`
|
|
493
283
|
* (with a suffix when an earlier run kept one of that name), under `<shelf>/.worktrees/`.
|
|
494
284
|
*/
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
const base = await this.#git(clone, "rev-parse", "HEAD");
|
|
498
|
-
const parent = join(this.dir, WORKTREES_DIR, repo);
|
|
499
|
-
mkdirSync(parent, { recursive: true });
|
|
500
|
-
const rel = relative(realpathSync(this.dir), realpathSync(parent));
|
|
501
|
-
if (rel.startsWith("..") || isAbsolute(rel))
|
|
502
|
-
throw new AgentError("The worktree folder escapes the shelf.");
|
|
503
|
-
for (let attempt = 1; ; attempt++) {
|
|
504
|
-
const name = attempt === 1 ? `${id}` : `${id}-${attempt}`;
|
|
505
|
-
const branch = `worker/${name}`;
|
|
506
|
-
const dir = join(parent, name);
|
|
507
|
-
const taken =
|
|
508
|
-
existsSync(dir) ||
|
|
509
|
-
(
|
|
510
|
-
await this.#run([
|
|
511
|
-
"git",
|
|
512
|
-
"-C",
|
|
513
|
-
clone,
|
|
514
|
-
"rev-parse",
|
|
515
|
-
"--verify",
|
|
516
|
-
"--quiet",
|
|
517
|
-
`refs/heads/${branch}`,
|
|
518
|
-
])
|
|
519
|
-
).code === 0;
|
|
520
|
-
if (taken) continue;
|
|
521
|
-
await this.#git(
|
|
522
|
-
clone,
|
|
523
|
-
"worktree",
|
|
524
|
-
"add",
|
|
525
|
-
"--quiet",
|
|
526
|
-
"-b",
|
|
527
|
-
branch,
|
|
528
|
-
dir,
|
|
529
|
-
base,
|
|
530
|
-
);
|
|
531
|
-
return { repo, dir: realpathSync(dir), branch, base };
|
|
532
|
-
}
|
|
285
|
+
addWorktree(repo: string, id: number): Promise<WorkerTree> {
|
|
286
|
+
return this.#trees.add(repo, id);
|
|
533
287
|
}
|
|
534
288
|
|
|
535
289
|
/**
|
|
@@ -538,147 +292,23 @@ export class RepoShelf {
|
|
|
538
292
|
* mid-rebase: a conflict aborts the rebase in the worktree and keeps both. A clone or worktree
|
|
539
293
|
* with uncommitted changes is not touched. The caller serializes integrations of one clone.
|
|
540
294
|
*/
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
const kept = { branch: tree.branch, dir: tree.dir };
|
|
544
|
-
const dirty = await this.#git(clone, "status", "--porcelain");
|
|
545
|
-
if (dirty)
|
|
546
|
-
return {
|
|
547
|
-
status: "refused",
|
|
548
|
-
...kept,
|
|
549
|
-
reason: `the clone has uncommitted changes, so it was left as it is:\n${dirty}`,
|
|
550
|
-
};
|
|
551
|
-
const head = await this.#git(clone, "rev-parse", "HEAD");
|
|
552
|
-
const ahead = Number(
|
|
553
|
-
await this.#git(clone, "rev-list", "--count", `${head}..${tree.branch}`),
|
|
554
|
-
);
|
|
555
|
-
if (ahead === 0) {
|
|
556
|
-
await this.removeWorktree(tree);
|
|
557
|
-
return { status: "nothing" };
|
|
558
|
-
}
|
|
559
|
-
const treeDir = this.#treeDir(tree);
|
|
560
|
-
const treeDirty = await this.#git(treeDir, "status", "--porcelain");
|
|
561
|
-
if (treeDirty)
|
|
562
|
-
return {
|
|
563
|
-
status: "refused",
|
|
564
|
-
...kept,
|
|
565
|
-
reason: `the worker left uncommitted changes in its worktree:\n${treeDirty}`,
|
|
566
|
-
};
|
|
567
|
-
const rebase = await this.#run([
|
|
568
|
-
"git",
|
|
569
|
-
"-C",
|
|
570
|
-
treeDir,
|
|
571
|
-
"rebase",
|
|
572
|
-
"--quiet",
|
|
573
|
-
head,
|
|
574
|
-
]);
|
|
575
|
-
if (rebase.code !== 0) {
|
|
576
|
-
const conflicted = await this.#run([
|
|
577
|
-
"git",
|
|
578
|
-
"-C",
|
|
579
|
-
treeDir,
|
|
580
|
-
"diff",
|
|
581
|
-
"--name-only",
|
|
582
|
-
"--diff-filter=U",
|
|
583
|
-
]);
|
|
584
|
-
await this.#run(["git", "-C", treeDir, "rebase", "--abort"]);
|
|
585
|
-
return {
|
|
586
|
-
status: "conflict",
|
|
587
|
-
...kept,
|
|
588
|
-
files: conflicted.out ? conflicted.out.split("\n") : [],
|
|
589
|
-
};
|
|
590
|
-
}
|
|
591
|
-
const merge = await this.#run([
|
|
592
|
-
"git",
|
|
593
|
-
"-C",
|
|
594
|
-
clone,
|
|
595
|
-
"merge",
|
|
596
|
-
"--ff-only",
|
|
597
|
-
"--quiet",
|
|
598
|
-
tree.branch,
|
|
599
|
-
]);
|
|
600
|
-
if (merge.code !== 0)
|
|
601
|
-
return {
|
|
602
|
-
status: "refused",
|
|
603
|
-
...kept,
|
|
604
|
-
reason: `the clone could not fast-forward to the branch: ${scrubDiagnostic(merge.err, this.#diagnosticChars) || `exit ${merge.code}`}`,
|
|
605
|
-
};
|
|
606
|
-
const log = await this.#git(
|
|
607
|
-
clone,
|
|
608
|
-
"log",
|
|
609
|
-
"--format=%h %s",
|
|
610
|
-
"--abbrev=12",
|
|
611
|
-
`${head}..HEAD`,
|
|
612
|
-
);
|
|
613
|
-
await this.removeWorktree(tree);
|
|
614
|
-
return {
|
|
615
|
-
status: "merged",
|
|
616
|
-
branch: tree.branch,
|
|
617
|
-
commits: log ? log.split("\n") : [],
|
|
618
|
-
};
|
|
295
|
+
integrate(tree: WorkerTree): Promise<Integration> {
|
|
296
|
+
return this.#trees.integrate(tree);
|
|
619
297
|
}
|
|
620
298
|
|
|
621
299
|
/** A worker worktree's state, as `state` reads a clone's. */
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
const status = await this.#git(dir, "status", "--porcelain");
|
|
625
|
-
return {
|
|
626
|
-
branch: await this.#git(dir, "rev-parse", "--abbrev-ref", "HEAD"),
|
|
627
|
-
head: await this.#git(dir, "rev-parse", "--short=12", "HEAD"),
|
|
628
|
-
uncommitted: status ? status.split("\n") : [],
|
|
629
|
-
};
|
|
300
|
+
treeState(tree: WorkerTree): Promise<RepoState> {
|
|
301
|
+
return this.#trees.state(tree);
|
|
630
302
|
}
|
|
631
303
|
|
|
632
304
|
/** `<short sha> <subject>` of each commit on the worker's branch after its base, newest first. */
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
this.#treeDir(tree),
|
|
636
|
-
"log",
|
|
637
|
-
"--format=%h %s",
|
|
638
|
-
"--abbrev=12",
|
|
639
|
-
`${tree.base}..HEAD`,
|
|
640
|
-
);
|
|
641
|
-
return log ? log.split("\n") : [];
|
|
305
|
+
treeCommits(tree: WorkerTree): Promise<string[]> {
|
|
306
|
+
return this.#trees.commits(tree);
|
|
642
307
|
}
|
|
643
308
|
|
|
644
309
|
/** Removes a worker's worktree and its branch; what is already gone is skipped. */
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
this.#treeDir(tree, false);
|
|
648
|
-
await this.#run([
|
|
649
|
-
"git",
|
|
650
|
-
"-C",
|
|
651
|
-
clone,
|
|
652
|
-
"worktree",
|
|
653
|
-
"remove",
|
|
654
|
-
"--force",
|
|
655
|
-
tree.dir,
|
|
656
|
-
]);
|
|
657
|
-
if (existsSync(tree.dir))
|
|
658
|
-
rmSync(tree.dir, { recursive: true, force: true });
|
|
659
|
-
await this.#run(["git", "-C", clone, "worktree", "prune"]);
|
|
660
|
-
await this.#run([
|
|
661
|
-
"git",
|
|
662
|
-
"-C",
|
|
663
|
-
clone,
|
|
664
|
-
"branch",
|
|
665
|
-
"-D",
|
|
666
|
-
"--quiet",
|
|
667
|
-
tree.branch,
|
|
668
|
-
]);
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
/** A worktree path, refused unless it lies in this repository's worktree folder. */
|
|
672
|
-
#treeDir(tree: WorkerTree, mustExist = true): string {
|
|
673
|
-
const parent = resolve(this.dir, WORKTREES_DIR, tree.repo);
|
|
674
|
-
const path = mustExist ? realpathSync(tree.dir) : resolve(tree.dir);
|
|
675
|
-
const root = existsSync(parent) ? realpathSync(parent) : parent;
|
|
676
|
-
const rel = relative(root, path);
|
|
677
|
-
if (!rel || rel.startsWith("..") || isAbsolute(rel) || rel.includes("/"))
|
|
678
|
-
throw new AgentError(
|
|
679
|
-
"The worktree lies outside the shelf's worktree folder.",
|
|
680
|
-
);
|
|
681
|
-
return path;
|
|
310
|
+
removeWorktree(tree: WorkerTree): Promise<void> {
|
|
311
|
+
return this.#trees.remove(tree);
|
|
682
312
|
}
|
|
683
313
|
|
|
684
314
|
async #summary(repo: string, fetch: boolean): Promise<RepoSummary> {
|
|
@@ -772,13 +402,15 @@ export class RepoShelf {
|
|
|
772
402
|
return head.out.slice("origin/".length);
|
|
773
403
|
}
|
|
774
404
|
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
405
|
+
#run(
|
|
406
|
+
command: string[],
|
|
407
|
+
cwd?: string,
|
|
408
|
+
): Promise<{ out: string; err: string; code: number }> {
|
|
409
|
+
return this.#runner.run(command, cwd);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
#git(dir: string, ...args: string[]): Promise<string> {
|
|
413
|
+
return this.#runner.git(dir, ...args);
|
|
782
414
|
}
|
|
783
415
|
}
|
|
784
416
|
|
|
@@ -789,18 +421,3 @@ function refuseHidden(repo: string): void {
|
|
|
789
421
|
`"${repo}" is not a repository: owners starting with a dot are reserved for the shelf.`,
|
|
790
422
|
);
|
|
791
423
|
}
|
|
792
|
-
|
|
793
|
-
/** The report as posted for the owner. */
|
|
794
|
-
export function reportPost(report: ChangeReport, direct = false): string {
|
|
795
|
-
return [
|
|
796
|
-
`**Change report**: \`${report.repo}\`, commit \`${report.sha}\`, default branch \`${report.branch}\`.`,
|
|
797
|
-
`**Push destination**: \`${report.target}\``,
|
|
798
|
-
`**Commits (${report.commits.length})**`,
|
|
799
|
-
...report.commits.map((c) => `- \`${c.slice(0, 12)}\`${c.slice(12)}`),
|
|
800
|
-
"**Changed files**",
|
|
801
|
-
`\`\`\`\n${report.diffstat}\n\`\`\``,
|
|
802
|
-
direct
|
|
803
|
-
? "This repository is explicitly owner-owned; repo_push does not need an approval card."
|
|
804
|
-
: `After approval: repo_push ${report.repo} ${report.sha}`,
|
|
805
|
-
].join("\n");
|
|
806
|
-
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/** What a clone would push to its default branch (repos-and-skills spec behavior 10). */
|
|
2
|
+
export interface ChangeReport {
|
|
3
|
+
repo: string;
|
|
4
|
+
/** The default branch the push goes to. */
|
|
5
|
+
branch: string;
|
|
6
|
+
/** HEAD's full SHA, the exact commit to push. */
|
|
7
|
+
sha: string;
|
|
8
|
+
/** `<short sha> <subject>`, newest first. */
|
|
9
|
+
commits: string[];
|
|
10
|
+
diffstat: string;
|
|
11
|
+
/** The credential-free configured push destination shown on the approval card. */
|
|
12
|
+
target: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** One managed repository as repo_list shows it (behavior 3). */
|
|
16
|
+
export interface RepoSummary {
|
|
17
|
+
repo: string;
|
|
18
|
+
dir: string;
|
|
19
|
+
branch: string;
|
|
20
|
+
/** Commits ahead of and behind its upstream, as of the last fetch; absent without one. */
|
|
21
|
+
upstream?: { ahead: number; behind: number };
|
|
22
|
+
uncommitted: number;
|
|
23
|
+
/** `<short sha> <subject> (<date>)`, absent in an empty repository. */
|
|
24
|
+
lastCommit?: string;
|
|
25
|
+
summary?: string;
|
|
26
|
+
ciOnPush: boolean;
|
|
27
|
+
/** Set when git could not describe it. */
|
|
28
|
+
error?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A clone's state, measured around a coding worker's run (behavior 8). */
|
|
32
|
+
export interface RepoState {
|
|
33
|
+
branch: string;
|
|
34
|
+
head: string;
|
|
35
|
+
/** `git status --porcelain` lines. */
|
|
36
|
+
uncommitted: string[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The GitHub token git and gh calls get as GH_TOKEN, or a function read at each call; undefined
|
|
41
|
+
* leaves them to gh's own login.
|
|
42
|
+
*/
|
|
43
|
+
export type GithubToken = string | undefined | (() => string | undefined);
|
|
44
|
+
|
|
45
|
+
export type CloneCommand = (repo: string, dir: string) => Promise<void>;
|
|
46
|
+
|
|
47
|
+
/** A coding worker's own checkout: a linked worktree on its own branch, cut from the clone's HEAD. */
|
|
48
|
+
export interface WorkerTree {
|
|
49
|
+
repo: string;
|
|
50
|
+
/** The worktree's real path, outside the shelf's list of managed repositories. */
|
|
51
|
+
dir: string;
|
|
52
|
+
/** The worker's branch, such as `worker/3`. */
|
|
53
|
+
branch: string;
|
|
54
|
+
/** The clone's full HEAD the branch was cut from. */
|
|
55
|
+
base: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** What bringing a worker's branch back into its clone did. */
|
|
59
|
+
export type Integration =
|
|
60
|
+
/** The branch had no commits beyond the clone; the worktree and branch are gone. */
|
|
61
|
+
| { status: "nothing" }
|
|
62
|
+
/** Rebased onto the clone's branch and fast-forwarded it; the worktree and branch are gone. */
|
|
63
|
+
| { status: "merged"; branch: string; commits: string[] }
|
|
64
|
+
/** The rebase stopped on these files; it was aborted, and the branch and worktree are kept. */
|
|
65
|
+
| { status: "conflict"; branch: string; dir: string; files: string[] }
|
|
66
|
+
/** The clone or the worktree had uncommitted changes, or the clone moved; nothing was touched. */
|
|
67
|
+
| {
|
|
68
|
+
status: "refused";
|
|
69
|
+
branch: string;
|
|
70
|
+
dir: string;
|
|
71
|
+
reason: string;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/** The folder under the shelf that holds worker worktrees; the shelf lists no repository there. */
|
|
75
|
+
export const WORKTREES_DIR = ".worktrees";
|
package/src/task-control.ts
CHANGED
|
@@ -89,3 +89,34 @@ export async function within<T>(
|
|
|
89
89
|
clearTimeout(timer);
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
+
|
|
93
|
+
/** What a report says in place of the worker's own when the host's shutdown stopped it. */
|
|
94
|
+
export const interruptedText =
|
|
95
|
+
"the host shut down while the worker ran, so the worker was stopped; its commits were not merged into the clone";
|
|
96
|
+
export const stoppedOutsideText =
|
|
97
|
+
"the worker was stopped by a signal from outside the desk; its commits were not merged into the clone";
|
|
98
|
+
export const stoppedOutsideReason =
|
|
99
|
+
"the worker was stopped from outside the desk (a signal to its process); its tracked changes were saved in a wip: commit, untracked files were not, and the work stays on its branch and worktree";
|
|
100
|
+
export const interruptedReason =
|
|
101
|
+
"the host shut down while the task ran; its tracked changes were saved in a wip: commit, untracked files were not, and the work stays on its branch and worktree";
|
|
102
|
+
/**
|
|
103
|
+
* The abort reason of a host shutdown. A worker stopped with it is asked to wind down and save a
|
|
104
|
+
* wip commit; one stopped for any other reason (a cancel, its time) is killed and leaves nothing.
|
|
105
|
+
*/
|
|
106
|
+
export const hostStopping = "the host is stopping";
|
|
107
|
+
/** Said when a task could not start because the host is draining before a restart. */
|
|
108
|
+
export const notStartedText =
|
|
109
|
+
"not started: host restarting, dispatch again after restart";
|
|
110
|
+
/** Why an aborted worker's branch is not merged. */
|
|
111
|
+
export function notMergedReason(run: {
|
|
112
|
+
cancelledBy?: string | undefined;
|
|
113
|
+
interrupted?: boolean | undefined;
|
|
114
|
+
timedOut?: number | undefined;
|
|
115
|
+
stoppedOutside?: boolean | undefined;
|
|
116
|
+
}): string {
|
|
117
|
+
if (run.cancelledBy !== undefined)
|
|
118
|
+
return `the task was cancelled by ${run.cancelledBy}`;
|
|
119
|
+
if (run.timedOut === undefined && run.interrupted) return interruptedReason;
|
|
120
|
+
if (run.stoppedOutside) return stoppedOutsideReason;
|
|
121
|
+
return `the worker was stopped${run.timedOut === undefined ? "" : ` after ${Math.round(run.timedOut / 60_000)} minutes without finishing`}; its work stays on its branch and worktree`;
|
|
122
|
+
}
|