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/coding-plugin.ts
CHANGED
|
@@ -30,10 +30,12 @@ import {
|
|
|
30
30
|
memorySettings,
|
|
31
31
|
registerSettingsPage,
|
|
32
32
|
} from "./coding-settings.ts";
|
|
33
|
+
import type { CodingResult } from "./desk-types.ts";
|
|
33
34
|
import { checkCount, checkMaxWorkers } from "./limits.ts";
|
|
34
35
|
import { PiCodingWorker } from "./pi-coding-worker.ts";
|
|
35
36
|
import { RepoShelf, reportPost } from "./repo-shelf.ts";
|
|
36
37
|
import { taskTools } from "./task-tools.ts";
|
|
38
|
+
import { DEFAULT_TOOL_TEXT } from "./tool-text.ts";
|
|
37
39
|
import { workerHolds } from "./worker-holds.ts";
|
|
38
40
|
|
|
39
41
|
export type {
|
|
@@ -126,6 +128,18 @@ export function coding(options: CodingOptions) {
|
|
|
126
128
|
diagnosticChars: options.diagnosticChars,
|
|
127
129
|
holds: workerHolds(options.holds, () => context.sessions().holds),
|
|
128
130
|
});
|
|
131
|
+
const deliverReport =
|
|
132
|
+
options.onResult ??
|
|
133
|
+
(async (result: CodingResult) => {
|
|
134
|
+
await context.surfaces.sendReply(result.job.channel, {
|
|
135
|
+
chunks: [
|
|
136
|
+
codingReport(
|
|
137
|
+
result,
|
|
138
|
+
zonedStamp(result.job.startedAt, context.env.timeZone),
|
|
139
|
+
),
|
|
140
|
+
],
|
|
141
|
+
});
|
|
142
|
+
});
|
|
129
143
|
const desk = new CodingDesk({
|
|
130
144
|
shelf,
|
|
131
145
|
worker,
|
|
@@ -147,18 +161,13 @@ export function coding(options: CodingOptions) {
|
|
|
147
161
|
? { interimPrimaryChars: options.interimPrimaryChars }
|
|
148
162
|
: {}),
|
|
149
163
|
prompts: (channel) => context.surfaces.prompts(channel),
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
zonedStamp(result.job.startedAt, context.env.timeZone),
|
|
158
|
-
),
|
|
159
|
-
],
|
|
160
|
-
});
|
|
161
|
-
}),
|
|
164
|
+
// A report of work that ends while the host drains still gets through the closed queue.
|
|
165
|
+
deliver: (result) => {
|
|
166
|
+
const report = () => deliverReport(result);
|
|
167
|
+
return context.queue.finishing
|
|
168
|
+
? context.queue.finishing(report)
|
|
169
|
+
: report();
|
|
170
|
+
},
|
|
162
171
|
});
|
|
163
172
|
const ship = <T>(repo: string, operation: () => Promise<T>) =>
|
|
164
173
|
desk.ship(repo, operation);
|
|
@@ -190,22 +199,29 @@ export function coding(options: CodingOptions) {
|
|
|
190
199
|
}
|
|
191
200
|
};
|
|
192
201
|
const textOf = (tool: RepoToolName) => options.toolText?.[tool];
|
|
193
|
-
const describe = (tool: RepoToolName
|
|
194
|
-
textOf(tool)?.description ??
|
|
195
|
-
const about = (tool: RepoToolName, name: string
|
|
196
|
-
const description =
|
|
202
|
+
const describe = (tool: RepoToolName) =>
|
|
203
|
+
textOf(tool)?.description ?? DEFAULT_TOOL_TEXT[tool].description;
|
|
204
|
+
const about = (tool: RepoToolName, name: string) => {
|
|
205
|
+
const description =
|
|
206
|
+
textOf(tool)?.parameters?.[name] ??
|
|
207
|
+
DEFAULT_TOOL_TEXT[tool].parameters[name];
|
|
197
208
|
return description ? { description } : {};
|
|
198
209
|
};
|
|
199
210
|
const repoSchema = (tool: RepoToolName) =>
|
|
200
|
-
Type.String(about(tool, "repo"
|
|
211
|
+
Type.String(about(tool, "repo"));
|
|
201
212
|
return {
|
|
202
213
|
services: [
|
|
203
214
|
{
|
|
204
215
|
name: "coding-desk",
|
|
205
216
|
// The stored limit is read at start: setup must not query the database.
|
|
206
217
|
start: () => applyStoredLimit(store, desk, context.logger),
|
|
207
|
-
// The
|
|
208
|
-
|
|
218
|
+
// The shutdown starts no queued task and refuses new ones; queued tasks are reported at once.
|
|
219
|
+
beginDrain: () => desk.beginDrain(),
|
|
220
|
+
// The host names the channels whose work a shutdown waits for or aborts: running tasks
|
|
221
|
+
// and reports still going out.
|
|
222
|
+
busy: () => desk.busyChannels(),
|
|
223
|
+
// At the drain limit workers are told to stop, and their reports go out, before stop.
|
|
224
|
+
abort: () => desk.interrupt(),
|
|
209
225
|
stop: () => desk.stop(),
|
|
210
226
|
},
|
|
211
227
|
],
|
|
@@ -265,10 +281,7 @@ export function coding(options: CodingOptions) {
|
|
|
265
281
|
defineTool({
|
|
266
282
|
name: "repo_list",
|
|
267
283
|
minTier: "owner",
|
|
268
|
-
description: describe(
|
|
269
|
-
"repo_list",
|
|
270
|
-
"List managed clones, branch, upstream counts, dirty files, last commit, summary, CI hints and linked skills. Fetch first only when requested.",
|
|
271
|
-
),
|
|
284
|
+
description: describe("repo_list"),
|
|
272
285
|
parameters: Type.Object({
|
|
273
286
|
fetch: Type.Optional(Type.Boolean(about("repo_list", "fetch"))),
|
|
274
287
|
}),
|
|
@@ -280,23 +293,22 @@ export function coding(options: CodingOptions) {
|
|
|
280
293
|
skills: skills?.linkedFrom(repo.repo) ?? [],
|
|
281
294
|
}),
|
|
282
295
|
);
|
|
283
|
-
|
|
284
|
-
return options.presentation.list(repos, {
|
|
285
|
-
shelfDir: shelf.dir,
|
|
286
|
-
fetched: fetch === true,
|
|
287
|
-
});
|
|
288
|
-
return repos.length
|
|
296
|
+
const text = repos.length
|
|
289
297
|
? JSON.stringify(repos, null, 2)
|
|
290
298
|
: `No managed repositories in ${shelf.dir}; use repo_add.`;
|
|
299
|
+
return (
|
|
300
|
+
options.presentation?.list?.(
|
|
301
|
+
repos,
|
|
302
|
+
{ shelfDir: shelf.dir, fetched: fetch === true },
|
|
303
|
+
text,
|
|
304
|
+
) ?? text
|
|
305
|
+
);
|
|
291
306
|
}),
|
|
292
307
|
}),
|
|
293
308
|
defineTool({
|
|
294
309
|
name: "repo_add",
|
|
295
310
|
minTier: "owner",
|
|
296
|
-
description: describe(
|
|
297
|
-
"repo_add",
|
|
298
|
-
"Clone owner/repo into the shelf using the host's Git login (GitHub CLI by default).",
|
|
299
|
-
),
|
|
311
|
+
description: describe("repo_add"),
|
|
300
312
|
parameters: Type.Object({ repo: repoSchema("repo_add") }),
|
|
301
313
|
run: ({ repo }) =>
|
|
302
314
|
refusal(
|
|
@@ -306,10 +318,7 @@ export function coding(options: CodingOptions) {
|
|
|
306
318
|
defineTool({
|
|
307
319
|
name: "repo_change_report",
|
|
308
320
|
minTier: "owner",
|
|
309
|
-
description: describe(
|
|
310
|
-
"repo_change_report",
|
|
311
|
-
"Fetch and report commits and changed files for the default branch. Requires a clean, ahead-only clone. Review checks before requesting this report.",
|
|
312
|
-
),
|
|
321
|
+
description: describe("repo_change_report"),
|
|
313
322
|
parameters: Type.Object({
|
|
314
323
|
repo: repoSchema("repo_change_report"),
|
|
315
324
|
}),
|
|
@@ -318,13 +327,16 @@ export function coding(options: CodingOptions) {
|
|
|
318
327
|
ship(repo, async () => {
|
|
319
328
|
const report = await shelf.report(repo);
|
|
320
329
|
const direct = ownerOwned(repo);
|
|
321
|
-
const
|
|
322
|
-
report,
|
|
323
|
-
direct,
|
|
324
|
-
) ?? {
|
|
330
|
+
const standard = {
|
|
325
331
|
post: reportPost(report, direct),
|
|
326
332
|
result: reportPost(report, direct),
|
|
327
333
|
};
|
|
334
|
+
const text =
|
|
335
|
+
options.presentation?.changeReport?.(
|
|
336
|
+
report,
|
|
337
|
+
direct,
|
|
338
|
+
standard,
|
|
339
|
+
) ?? standard;
|
|
328
340
|
if (options.postChangeReport)
|
|
329
341
|
await options.postChangeReport(turn, text.post);
|
|
330
342
|
else
|
|
@@ -338,10 +350,7 @@ export function coding(options: CodingOptions) {
|
|
|
338
350
|
defineTool({
|
|
339
351
|
name: "repo_push",
|
|
340
352
|
minTier: "owner",
|
|
341
|
-
description: describe(
|
|
342
|
-
"repo_push",
|
|
343
|
-
"Push the SHA from the latest change report to its default branch, without force. Held for owner approval unless the trusted host policy explicitly marks the clone owner-owned.",
|
|
344
|
-
),
|
|
353
|
+
description: describe("repo_push"),
|
|
345
354
|
parameters: Type.Object({
|
|
346
355
|
repo: repoSchema("repo_push"),
|
|
347
356
|
sha: Type.String({
|
|
@@ -366,10 +375,7 @@ export function coding(options: CodingOptions) {
|
|
|
366
375
|
defineTool({
|
|
367
376
|
name: "repo_task",
|
|
368
377
|
minTier: "owner",
|
|
369
|
-
description: describe(
|
|
370
|
-
"repo_task",
|
|
371
|
-
"Start a background Pi coding worker on a clone. Each worker has its own worktree and branch, so several may work on one repository; when it finishes, its commits are rebased onto the clone and fast-forwarded, and a conflict keeps the branch and is reported. When every worker slot is taken the task waits in a queue. The worker reads repository instructions, edits, checks and commits; shipping uses a separate report and approval. Report returns to this channel.",
|
|
372
|
-
),
|
|
378
|
+
description: describe("repo_task"),
|
|
373
379
|
parameters: Type.Object({
|
|
374
380
|
repo: repoSchema("repo_task"),
|
|
375
381
|
task: Type.String({
|
|
@@ -444,18 +450,21 @@ export function coding(options: CodingOptions) {
|
|
|
444
450
|
const skippedText = skipped.length
|
|
445
451
|
? ` Unavailable implicit skills skipped: ${skipped.join(", ")}.`
|
|
446
452
|
: "";
|
|
447
|
-
if (job.queuePosition !== undefined)
|
|
453
|
+
if (job.queuePosition !== undefined) {
|
|
454
|
+
const queued = `Queued coding task #${job.id} in ${repo} at position ${job.queuePosition}: every worker slot is taken, and it starts on ${job.model} when one frees. Its report returns to ${job.channel}; repo_task_cancel cancels it.${skippedText}`;
|
|
448
455
|
return (
|
|
449
456
|
options.presentation?.taskQueued?.(
|
|
450
457
|
job,
|
|
451
458
|
job.queuePosition,
|
|
452
459
|
skipped,
|
|
453
|
-
|
|
454
|
-
|
|
460
|
+
queued,
|
|
461
|
+
) ?? queued
|
|
455
462
|
);
|
|
463
|
+
}
|
|
464
|
+
const started = `Started coding task #${job.id} in ${repo} on ${job.model}; its report returns to ${job.channel}, and repo_task_cancel cancels it.${skippedText}`;
|
|
456
465
|
return (
|
|
457
|
-
options.presentation?.taskStarted?.(job, skipped) ??
|
|
458
|
-
|
|
466
|
+
options.presentation?.taskStarted?.(job, skipped, started) ??
|
|
467
|
+
started
|
|
459
468
|
);
|
|
460
469
|
}),
|
|
461
470
|
}),
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { type HeldCall, SYSTEM_PRINCIPAL } from "pi-roundtable";
|
|
2
|
+
import {
|
|
3
|
+
type PromptSlot,
|
|
4
|
+
workTimeout,
|
|
5
|
+
YOLO_CARDED_SHELL_CLASSES,
|
|
6
|
+
YOLO_NEVER_CLASS,
|
|
7
|
+
} from "pi-roundtable/kit";
|
|
8
|
+
import type { CodingDeskOptions, CodingJob } from "./desk-types.ts";
|
|
9
|
+
import { checkTime } from "./limits.ts";
|
|
10
|
+
|
|
11
|
+
const DEFAULT_TIMEOUT_MS = 3_600_000;
|
|
12
|
+
/** The core's default long-running ceiling, four hours. */
|
|
13
|
+
const DEFAULT_LONG_RUNNING_MS = 4 * 60 * 60_000;
|
|
14
|
+
/** How often a run past its own limit looks again at the starter's long-running mode. */
|
|
15
|
+
const LONG_RUNNING_STEP_MS = 60_000;
|
|
16
|
+
|
|
17
|
+
/** The rule a class belongs to: the part before its first dot, or a rule's bare name. */
|
|
18
|
+
function ruleOf(id: string): string {
|
|
19
|
+
const dot = id.indexOf(".");
|
|
20
|
+
return dot === -1 ? id : id.slice(0, dot);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** What a job's starter's yolo and long-running modes allow its worker, read live. */
|
|
24
|
+
export class DeskModes {
|
|
25
|
+
readonly #options: CodingDeskOptions;
|
|
26
|
+
readonly #longRunningMs: number;
|
|
27
|
+
readonly #yoloApproves: (id: string) => boolean;
|
|
28
|
+
constructor(options: CodingDeskOptions) {
|
|
29
|
+
checkTime("timeoutMs", options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
30
|
+
const longRunning =
|
|
31
|
+
options.autonomy?.longRunningMaxMs ?? DEFAULT_LONG_RUNNING_MS;
|
|
32
|
+
checkTime("autonomy.longRunningMaxMs", longRunning);
|
|
33
|
+
this.#longRunningMs = longRunning;
|
|
34
|
+
const allow = new Set(options.autonomy?.yolo?.allow ?? []);
|
|
35
|
+
const card = new Set(options.autonomy?.yolo?.card ?? []);
|
|
36
|
+
const carded = new Set<string>(YOLO_CARDED_SHELL_CLASSES);
|
|
37
|
+
// The core's yolo rules: a carded class or rule never, an unclassed call only by its rule's name.
|
|
38
|
+
this.#yoloApproves = (id) => {
|
|
39
|
+
if (id.endsWith(`.${YOLO_NEVER_CLASS}`)) return false;
|
|
40
|
+
if (card.has(id) || card.has(ruleOf(id))) return false;
|
|
41
|
+
if (!id.includes(".")) return allow.has(id);
|
|
42
|
+
return !carded.has(id) || allow.has(id);
|
|
43
|
+
};
|
|
44
|
+
this.#options = options;
|
|
45
|
+
}
|
|
46
|
+
/** Whether the starter's modes may act: only for an owner other than the host's own principal. */
|
|
47
|
+
#modeOf(job: CodingJob): { yolo: boolean; longRunning: boolean } {
|
|
48
|
+
const { modes } = this.#options;
|
|
49
|
+
const speaker = job.starter?.speaker;
|
|
50
|
+
if (
|
|
51
|
+
!modes ||
|
|
52
|
+
!job.starter ||
|
|
53
|
+
speaker?.tier !== "owner" ||
|
|
54
|
+
speaker.principalId === SYSTEM_PRINCIPAL
|
|
55
|
+
)
|
|
56
|
+
return { yolo: false, longRunning: false };
|
|
57
|
+
const mode = modes.peek(job.starter.channel);
|
|
58
|
+
return { yolo: mode.yolo === true, longRunning: mode.longRunning === true };
|
|
59
|
+
}
|
|
60
|
+
/** Whether the starter's yolo mode approves the call now, as it would in that conversation. */
|
|
61
|
+
yolo(job: CodingJob, call: HeldCall): boolean {
|
|
62
|
+
if (!this.#modeOf(job).yolo || call.approverIds !== undefined) return false;
|
|
63
|
+
const classes = call.holdClasses;
|
|
64
|
+
if (!classes?.length || !classes.every(this.#yoloApproves)) return false;
|
|
65
|
+
const { starter } = job;
|
|
66
|
+
if (!starter) return false;
|
|
67
|
+
try {
|
|
68
|
+
this.#options.modes?.autoApproved?.({
|
|
69
|
+
channel: starter.channel,
|
|
70
|
+
tool: call.tool,
|
|
71
|
+
action: `coding #${job.id} in ${job.repo}: ${call.action}`,
|
|
72
|
+
classes: [...classes],
|
|
73
|
+
principalId: starter.speaker.principalId,
|
|
74
|
+
at: new Date(),
|
|
75
|
+
});
|
|
76
|
+
} catch {
|
|
77
|
+
this.#options.logger.warn(
|
|
78
|
+
{ job: job.id },
|
|
79
|
+
"Recording a yolo approval failed.",
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The worker's work-time budget: `timeoutMs`, then, while the starter's long-running mode is
|
|
86
|
+
* on, further steps up to `autonomy.longRunningMaxMs` in all. Card waits never count.
|
|
87
|
+
*/
|
|
88
|
+
budget(
|
|
89
|
+
job: CodingJob,
|
|
90
|
+
slot: PromptSlot,
|
|
91
|
+
expire: (workedMs: number) => void,
|
|
92
|
+
): () => void {
|
|
93
|
+
const base = this.#options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
94
|
+
let worked = 0;
|
|
95
|
+
let cancel = () => {};
|
|
96
|
+
const phase = (ms: number) => {
|
|
97
|
+
cancel = workTimeout(ms, slot, () => {
|
|
98
|
+
worked += ms;
|
|
99
|
+
if (this.#modeOf(job).longRunning && worked < this.#longRunningMs)
|
|
100
|
+
phase(Math.min(LONG_RUNNING_STEP_MS, this.#longRunningMs - worked));
|
|
101
|
+
else expire(worked);
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
phase(base);
|
|
105
|
+
return () => cancel();
|
|
106
|
+
}
|
|
107
|
+
}
|
package/src/desk-run.ts
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import type { HeldCall } from "pi-roundtable";
|
|
2
|
+
import {
|
|
3
|
+
AgentError,
|
|
4
|
+
approvalCard,
|
|
5
|
+
approvalDetails,
|
|
6
|
+
InterimPoster,
|
|
7
|
+
promptSlot,
|
|
8
|
+
} from "pi-roundtable/kit";
|
|
9
|
+
import { codingReport } from "./coding-report.ts";
|
|
10
|
+
import type { DeskModes } from "./desk-modes.ts";
|
|
11
|
+
import type {
|
|
12
|
+
CodingDeskOptions,
|
|
13
|
+
CodingJob,
|
|
14
|
+
CodingResult,
|
|
15
|
+
HeldCallAnswer,
|
|
16
|
+
} from "./desk-types.ts";
|
|
17
|
+
import type { RepoGate } from "./repo-gate.ts";
|
|
18
|
+
import type { Integration, RepoState, WorkerTree } from "./repo-shelf.ts";
|
|
19
|
+
import {
|
|
20
|
+
cancelledText,
|
|
21
|
+
interruptedText,
|
|
22
|
+
notMergedReason,
|
|
23
|
+
stoppedOutsideText,
|
|
24
|
+
} from "./task-control.ts";
|
|
25
|
+
import { CodingWorkerFailure } from "./worker-failure.ts";
|
|
26
|
+
|
|
27
|
+
const MAX_REPORT_CHARS = 20_000;
|
|
28
|
+
const MAX_HELD_ENTRIES = 10;
|
|
29
|
+
const MAX_HELD_CHARS = 1_000;
|
|
30
|
+
function bounded(text: string, max: number): string {
|
|
31
|
+
const suffix = "\n[truncated]";
|
|
32
|
+
return text.length > max
|
|
33
|
+
? `${text.slice(0, max - suffix.length)}${suffix}`
|
|
34
|
+
: text;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** One task's state while it runs; the desk keeps it until the worker settles. */
|
|
38
|
+
export interface Running {
|
|
39
|
+
job: CodingJob;
|
|
40
|
+
controller: AbortController;
|
|
41
|
+
done?: Promise<void>;
|
|
42
|
+
/** Who cancelled it; its branch is then kept, not merged. */
|
|
43
|
+
cancelledBy?: string;
|
|
44
|
+
/** The host's shutdown told it to stop; its branch is then kept, not merged. */
|
|
45
|
+
interrupted?: boolean;
|
|
46
|
+
/** Resolves with its kept commits once the worker settles. */
|
|
47
|
+
settled?: Promise<string[]>;
|
|
48
|
+
settle?: (commits: string[]) => void;
|
|
49
|
+
/** Its worktree, once made. */
|
|
50
|
+
dir?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** What a run takes from the desk that started it. */
|
|
54
|
+
export interface RunContext {
|
|
55
|
+
options: CodingDeskOptions;
|
|
56
|
+
modes: DeskModes;
|
|
57
|
+
gate: RepoGate;
|
|
58
|
+
/** Frees the job's slot and starts the next queued tasks. */
|
|
59
|
+
free(job: CodingJob): void;
|
|
60
|
+
/** Delivers a report, which a shutdown waits for meanwhile. */
|
|
61
|
+
deliver(result: CodingResult): Promise<void>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* One task from worktree to delivered report: runs the worker, answers its held calls, integrates its
|
|
66
|
+
* branch, and hands the report to the desk to deliver.
|
|
67
|
+
*/
|
|
68
|
+
export async function runCodingJob(
|
|
69
|
+
ctx: RunContext,
|
|
70
|
+
job: CodingJob,
|
|
71
|
+
run: Running,
|
|
72
|
+
): Promise<void> {
|
|
73
|
+
const { controller } = run;
|
|
74
|
+
const { shelf, worker, prompts, threads, threadText } = ctx.options;
|
|
75
|
+
const slot = promptSlot();
|
|
76
|
+
const held: string[] = [];
|
|
77
|
+
const autoApproved: string[] = [];
|
|
78
|
+
let omittedHeld = 0;
|
|
79
|
+
let omittedApproved = 0;
|
|
80
|
+
const maxReport = ctx.options.limits?.reportChars ?? MAX_REPORT_CHARS;
|
|
81
|
+
const maxHeldEntries = ctx.options.limits?.heldEntries ?? MAX_HELD_ENTRIES;
|
|
82
|
+
const maxHeldChars = ctx.options.limits?.heldChars ?? MAX_HELD_CHARS;
|
|
83
|
+
let cancel = () => {};
|
|
84
|
+
let interim: InterimPoster | undefined;
|
|
85
|
+
let timedOut: number | undefined;
|
|
86
|
+
let stoppedOutside = false;
|
|
87
|
+
let tree: WorkerTree | undefined;
|
|
88
|
+
let outcome: CodingResult["outcome"];
|
|
89
|
+
try {
|
|
90
|
+
tree = await shelf.addWorktree(job.repo, job.id);
|
|
91
|
+
job.startHead = tree.base.slice(0, 12);
|
|
92
|
+
job.branch = tree.branch;
|
|
93
|
+
run.dir = tree.dir;
|
|
94
|
+
if (controller.signal.aborted)
|
|
95
|
+
throw new AgentError("the worker was stopped");
|
|
96
|
+
if (threads) {
|
|
97
|
+
try {
|
|
98
|
+
const thread = await threads.open(
|
|
99
|
+
job.origin,
|
|
100
|
+
`${job.repo} #${job.id}`,
|
|
101
|
+
threadText?.initial?.(job) ?? `Task:\n${job.task}`,
|
|
102
|
+
);
|
|
103
|
+
if (thread) job.thread = thread;
|
|
104
|
+
} catch {
|
|
105
|
+
ctx.options.logger.warn({ job: job.id }, "Coding thread unavailable.");
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (controller.signal.aborted)
|
|
109
|
+
throw new AgentError("the worker was stopped");
|
|
110
|
+
// The worker's progress goes to its thread as it works, under the report's name.
|
|
111
|
+
if (job.thread?.interim && ctx.options.interimText !== "off")
|
|
112
|
+
interim = new InterimPoster(job.thread.interim, {
|
|
113
|
+
logger: ctx.options.logger,
|
|
114
|
+
channel: job.thread.channel,
|
|
115
|
+
...(ctx.options.interimPrimaryChars
|
|
116
|
+
? { primaryChars: ctx.options.interimPrimaryChars }
|
|
117
|
+
: {}),
|
|
118
|
+
});
|
|
119
|
+
const poster = interim;
|
|
120
|
+
slot.bind(
|
|
121
|
+
threads
|
|
122
|
+
? job.thread && prompts?.(job.thread.channel)
|
|
123
|
+
: prompts?.(job.channel),
|
|
124
|
+
`Coding worker #${job.id}`,
|
|
125
|
+
poster ? () => poster.flush() : undefined,
|
|
126
|
+
);
|
|
127
|
+
cancel = ctx.modes.budget(job, slot, (worked) => {
|
|
128
|
+
timedOut = worked;
|
|
129
|
+
controller.abort();
|
|
130
|
+
});
|
|
131
|
+
const review = async (call: HeldCall): Promise<HeldCallAnswer> => {
|
|
132
|
+
// The starter's yolo mode answers for a call it may approve, read at the time of the call.
|
|
133
|
+
if (!controller.signal.aborted && ctx.modes.yolo(job, call)) {
|
|
134
|
+
if (autoApproved.length < maxHeldEntries)
|
|
135
|
+
autoApproved.push(
|
|
136
|
+
bounded(`${call.action}: ${call.tool} ${call.input}`, maxHeldChars),
|
|
137
|
+
);
|
|
138
|
+
else omittedApproved++;
|
|
139
|
+
return "approved";
|
|
140
|
+
}
|
|
141
|
+
let answer: HeldCallAnswer = "held";
|
|
142
|
+
try {
|
|
143
|
+
const decision = await slot.prompts?.confirm(
|
|
144
|
+
threadText?.approvalTitle?.(job) ?? `${slot.asker} requests approval`,
|
|
145
|
+
approvalCard(call),
|
|
146
|
+
controller.signal,
|
|
147
|
+
undefined,
|
|
148
|
+
undefined,
|
|
149
|
+
approvalDetails(call),
|
|
150
|
+
);
|
|
151
|
+
if (decision === "approved" || decision === "declined")
|
|
152
|
+
answer = decision;
|
|
153
|
+
} catch {
|
|
154
|
+
/* A failed card never authorizes the call. */
|
|
155
|
+
}
|
|
156
|
+
// A call the owner declined is settled, not held for the report.
|
|
157
|
+
if (answer === "held") {
|
|
158
|
+
if (held.length < maxHeldEntries) {
|
|
159
|
+
const entry = bounded(
|
|
160
|
+
`${call.action}: ${call.tool} ${call.input}`,
|
|
161
|
+
maxHeldChars,
|
|
162
|
+
);
|
|
163
|
+
held.push(entry);
|
|
164
|
+
try {
|
|
165
|
+
await interim?.flush();
|
|
166
|
+
await job.thread?.post(
|
|
167
|
+
threadText?.held?.(entry, job) ?? `Held: ${entry}`,
|
|
168
|
+
);
|
|
169
|
+
} catch {
|
|
170
|
+
/* A failed progress post does not discard the held action. */
|
|
171
|
+
}
|
|
172
|
+
} else omittedHeld++;
|
|
173
|
+
}
|
|
174
|
+
return answer;
|
|
175
|
+
};
|
|
176
|
+
const report = await worker.run(
|
|
177
|
+
{ ...job, dir: tree.dir },
|
|
178
|
+
controller.signal,
|
|
179
|
+
review,
|
|
180
|
+
interim,
|
|
181
|
+
);
|
|
182
|
+
if (controller.signal.aborted)
|
|
183
|
+
throw new AgentError("the worker was stopped");
|
|
184
|
+
outcome = { ok: true, report: bounded(report, maxReport) };
|
|
185
|
+
} catch (error) {
|
|
186
|
+
// The host's own refusals and the worker's scrubbed, bounded error text reach the report.
|
|
187
|
+
let message =
|
|
188
|
+
"The worker failed; inspect the clone and worker configuration on the host.";
|
|
189
|
+
if (error instanceof AgentError) message = error.message;
|
|
190
|
+
if (error instanceof CodingWorkerFailure) {
|
|
191
|
+
if (error.category === "stopped" && !controller.signal.aborted)
|
|
192
|
+
stoppedOutside = true;
|
|
193
|
+
ctx.options.logger.warn(
|
|
194
|
+
{ job: job.id, category: error.category, exitCode: error.exitCode },
|
|
195
|
+
"Coding worker failed.",
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
if (controller.signal.aborted) message = "the worker was stopped";
|
|
199
|
+
if (stoppedOutside) message = stoppedOutsideText;
|
|
200
|
+
if (run.interrupted) message = interruptedText;
|
|
201
|
+
if (timedOut !== undefined)
|
|
202
|
+
message = `the worker ran out of time (${Math.round(timedOut / 60_000)} minutes)`;
|
|
203
|
+
if (run.cancelledBy !== undefined) message = cancelledText(run.cancelledBy);
|
|
204
|
+
outcome = { ok: false, error: message };
|
|
205
|
+
} finally {
|
|
206
|
+
cancel();
|
|
207
|
+
slot.unbind();
|
|
208
|
+
// What the worker wrote lands before its report.
|
|
209
|
+
await interim?.flush();
|
|
210
|
+
}
|
|
211
|
+
let integration: Integration | undefined;
|
|
212
|
+
// An aborted worker's commits stay on its branch for the owner to inspect, merge or delete.
|
|
213
|
+
if (tree && (controller.signal.aborted || run.interrupted || stoppedOutside))
|
|
214
|
+
integration = {
|
|
215
|
+
status: "refused",
|
|
216
|
+
branch: tree.branch,
|
|
217
|
+
dir: tree.dir,
|
|
218
|
+
reason: notMergedReason({ ...run, timedOut, stoppedOutside }),
|
|
219
|
+
};
|
|
220
|
+
else if (tree)
|
|
221
|
+
try {
|
|
222
|
+
integration = await ctx.gate.integrate(tree);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
integration = {
|
|
225
|
+
status: "refused",
|
|
226
|
+
branch: tree.branch,
|
|
227
|
+
dir: tree.dir,
|
|
228
|
+
reason:
|
|
229
|
+
error instanceof AgentError
|
|
230
|
+
? error.message
|
|
231
|
+
: "the merge failed; inspect the clone and the worktree on the host",
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
let state: RepoState | undefined;
|
|
235
|
+
let commits: string[] = [];
|
|
236
|
+
try {
|
|
237
|
+
if (
|
|
238
|
+
tree &&
|
|
239
|
+
(integration?.status === "conflict" || integration?.status === "refused")
|
|
240
|
+
) {
|
|
241
|
+
// The work stays on its branch: describe the worktree.
|
|
242
|
+
state = await shelf.treeState(tree);
|
|
243
|
+
commits = await shelf.treeCommits(tree);
|
|
244
|
+
} else {
|
|
245
|
+
state = await shelf.state(job.repo);
|
|
246
|
+
if (integration?.status === "merged") commits = integration.commits;
|
|
247
|
+
}
|
|
248
|
+
} catch {
|
|
249
|
+
ctx.options.logger.warn({ job: job.id }, "Repository state unreadable.");
|
|
250
|
+
}
|
|
251
|
+
if (omittedHeld)
|
|
252
|
+
held.push(`[${omittedHeld} more unapproved actions omitted]`);
|
|
253
|
+
if (omittedApproved)
|
|
254
|
+
autoApproved.push(`[${omittedApproved} more approved actions omitted]`);
|
|
255
|
+
const result: CodingResult = {
|
|
256
|
+
job,
|
|
257
|
+
outcome,
|
|
258
|
+
held,
|
|
259
|
+
...(autoApproved.length ? { autoApproved } : {}),
|
|
260
|
+
...(state ? { state } : {}),
|
|
261
|
+
commits,
|
|
262
|
+
...(integration ? { integration } : {}),
|
|
263
|
+
};
|
|
264
|
+
try {
|
|
265
|
+
await job.thread?.close(
|
|
266
|
+
bounded(
|
|
267
|
+
threadText?.report?.(result) ??
|
|
268
|
+
codingReport(result, job.startedAt.toISOString()),
|
|
269
|
+
maxReport,
|
|
270
|
+
),
|
|
271
|
+
);
|
|
272
|
+
} catch {
|
|
273
|
+
ctx.options.logger.warn({ job: job.id }, "Coding thread close failed.");
|
|
274
|
+
}
|
|
275
|
+
run.settle?.(commits);
|
|
276
|
+
// The work is over: free the slot and the repository before the report turn, so that turn can ship it.
|
|
277
|
+
ctx.free(job);
|
|
278
|
+
await ctx.deliver(result);
|
|
279
|
+
}
|
package/src/desk-types.ts
CHANGED
|
@@ -134,6 +134,11 @@ export interface CodingDeskOptions {
|
|
|
134
134
|
maxWorkers?: number;
|
|
135
135
|
/** Tasks waiting for a worker slot, first in first out; one more is refused. Default 16. */
|
|
136
136
|
maxQueued?: number;
|
|
137
|
+
/**
|
|
138
|
+
* The most `stop()` waits for workers to end and their reports to go out; a report still going
|
|
139
|
+
* then is left behind. Default 15 seconds.
|
|
140
|
+
*/
|
|
141
|
+
stopWaitMs?: number;
|
|
137
142
|
/**
|
|
138
143
|
* The conversations' modes: with them a worker follows the yolo and long-running modes of the
|
|
139
144
|
* conversation that started it, read live, when an owner other than the host spoke that turn.
|