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-desk.ts
CHANGED
|
@@ -1,37 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
} from "pi-roundtable";
|
|
6
|
-
import {
|
|
7
|
-
AgentError,
|
|
8
|
-
approvalCard,
|
|
9
|
-
approvalDetails,
|
|
10
|
-
InterimPoster,
|
|
11
|
-
type PromptSlot,
|
|
12
|
-
promptSlot,
|
|
13
|
-
workTimeout,
|
|
14
|
-
YOLO_CARDED_SHELL_CLASSES,
|
|
15
|
-
YOLO_NEVER_CLASS,
|
|
16
|
-
} from "pi-roundtable/kit";
|
|
17
|
-
import { codingReport } from "./coding-report.ts";
|
|
1
|
+
import type { ChannelKey } from "pi-roundtable";
|
|
2
|
+
import { AgentError } from "pi-roundtable/kit";
|
|
3
|
+
import { DeskModes } from "./desk-modes.ts";
|
|
4
|
+
import { type RunContext, type Running, runCodingJob } from "./desk-run.ts";
|
|
18
5
|
import type {
|
|
19
6
|
CodingDeskOptions,
|
|
20
7
|
CodingJob,
|
|
21
8
|
CodingResult,
|
|
22
9
|
CodingStart,
|
|
23
|
-
HeldCallAnswer,
|
|
24
10
|
} from "./desk-types.ts";
|
|
25
|
-
import { checkCount, checkMaxWorkers
|
|
26
|
-
import
|
|
11
|
+
import { checkCount, checkMaxWorkers } from "./limits.ts";
|
|
12
|
+
import { RepoGate } from "./repo-gate.ts";
|
|
27
13
|
import {
|
|
28
14
|
type CancelResult,
|
|
29
15
|
type CodingTask,
|
|
30
|
-
cancelledText,
|
|
31
16
|
droppedText,
|
|
17
|
+
hostStopping,
|
|
18
|
+
notStartedText,
|
|
32
19
|
within,
|
|
33
20
|
} from "./task-control.ts";
|
|
34
|
-
import { CodingWorkerFailure } from "./worker-failure.ts";
|
|
35
21
|
|
|
36
22
|
export { codingReport, integrationText } from "./coding-report.ts";
|
|
37
23
|
export type {
|
|
@@ -52,42 +38,10 @@ export type {
|
|
|
52
38
|
export { checkLimit } from "./limits.ts";
|
|
53
39
|
|
|
54
40
|
export const MAX_CODING_TASK_CHARS = 8_000;
|
|
55
|
-
const MAX_REPORT_CHARS = 20_000;
|
|
56
|
-
const MAX_HELD_ENTRIES = 10;
|
|
57
|
-
const MAX_HELD_CHARS = 1_000;
|
|
58
|
-
function bounded(text: string, max: number): string {
|
|
59
|
-
const suffix = "\n[truncated]";
|
|
60
|
-
return text.length > max
|
|
61
|
-
? `${text.slice(0, max - suffix.length)}${suffix}`
|
|
62
|
-
: text;
|
|
63
|
-
}
|
|
64
41
|
|
|
65
|
-
const DEFAULT_TIMEOUT_MS = 3_600_000;
|
|
66
|
-
/** The core's default long-running ceiling, four hours. */
|
|
67
|
-
const DEFAULT_LONG_RUNNING_MS = 4 * 60 * 60_000;
|
|
68
|
-
/** How often a run past its own limit looks again at the starter's long-running mode. */
|
|
69
|
-
const LONG_RUNNING_STEP_MS = 60_000;
|
|
70
42
|
const DEFAULT_MAX_WORKERS = 4;
|
|
71
43
|
const DEFAULT_MAX_QUEUED = 16;
|
|
72
|
-
|
|
73
|
-
/** The rule a class belongs to: the part before its first dot, or a rule's bare name. */
|
|
74
|
-
function ruleOf(id: string): string {
|
|
75
|
-
const dot = id.indexOf(".");
|
|
76
|
-
return dot === -1 ? id : id.slice(0, dot);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
interface Running {
|
|
80
|
-
job: CodingJob;
|
|
81
|
-
controller: AbortController;
|
|
82
|
-
done?: Promise<void>;
|
|
83
|
-
/** Who cancelled it; its branch is then kept, not merged. */
|
|
84
|
-
cancelledBy?: string;
|
|
85
|
-
/** Resolves with its kept commits once the worker settles. */
|
|
86
|
-
settled?: Promise<string[]>;
|
|
87
|
-
settle?: (commits: string[]) => void;
|
|
88
|
-
/** Its worktree, once made. */
|
|
89
|
-
dir?: string;
|
|
90
|
-
}
|
|
44
|
+
const DEFAULT_STOP_WAIT_MS = 15_000;
|
|
91
45
|
|
|
92
46
|
/**
|
|
93
47
|
* Up to `maxWorkers` workers at once, each in its own worktree; more tasks wait in a FIFO queue.
|
|
@@ -98,43 +52,28 @@ export class CodingDesk {
|
|
|
98
52
|
readonly #options: CodingDeskOptions;
|
|
99
53
|
#maxWorkers: number;
|
|
100
54
|
readonly #maxQueued: number;
|
|
101
|
-
readonly #
|
|
102
|
-
readonly #yoloApproves: (id: string) => boolean;
|
|
55
|
+
readonly #modes: DeskModes;
|
|
103
56
|
/** Every run until its report is delivered, for idle(). */
|
|
104
57
|
readonly #pending = new Set<Promise<void>>();
|
|
105
58
|
readonly #running = new Map<number, Running>();
|
|
106
59
|
readonly #queue: CodingJob[] = [];
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
readonly #integrating = new Set<string>();
|
|
111
|
-
/** Reports and pushes in progress, by repository. */
|
|
112
|
-
readonly #shipping = new Map<string, Promise<unknown>>();
|
|
60
|
+
readonly #gate: RepoGate;
|
|
61
|
+
/** The channels of reports being delivered, one entry each, until `deliver` settles. */
|
|
62
|
+
readonly #delivering: ChannelKey[] = [];
|
|
113
63
|
#nextId = 1;
|
|
114
64
|
#stopped = false;
|
|
65
|
+
/** The host's shutdown is waiting for work: nothing new starts. */
|
|
66
|
+
#draining = false;
|
|
115
67
|
constructor(options: CodingDeskOptions) {
|
|
116
|
-
|
|
117
|
-
const longRunning =
|
|
118
|
-
options.autonomy?.longRunningMaxMs ?? DEFAULT_LONG_RUNNING_MS;
|
|
119
|
-
checkTime("autonomy.longRunningMaxMs", longRunning);
|
|
68
|
+
this.#modes = new DeskModes(options);
|
|
120
69
|
this.#maxWorkers = options.maxWorkers ?? DEFAULT_MAX_WORKERS;
|
|
121
70
|
this.#maxQueued = options.maxQueued ?? DEFAULT_MAX_QUEUED;
|
|
122
71
|
checkMaxWorkers(this.#maxWorkers);
|
|
123
72
|
checkCount("maxQueued", this.#maxQueued, 0);
|
|
124
|
-
this.#longRunningMs = longRunning;
|
|
125
|
-
const allow = new Set(options.autonomy?.yolo?.allow ?? []);
|
|
126
|
-
const card = new Set(options.autonomy?.yolo?.card ?? []);
|
|
127
|
-
const carded = new Set<string>(YOLO_CARDED_SHELL_CLASSES);
|
|
128
|
-
// The core's yolo rules: a carded class or rule never, an unclassed call only by its rule's name.
|
|
129
|
-
this.#yoloApproves = (id) => {
|
|
130
|
-
if (id.endsWith(`.${YOLO_NEVER_CLASS}`)) return false;
|
|
131
|
-
if (card.has(id) || card.has(ruleOf(id))) return false;
|
|
132
|
-
if (!id.includes(".")) return allow.has(id);
|
|
133
|
-
return !carded.has(id) || allow.has(id);
|
|
134
|
-
};
|
|
135
73
|
this.#options = options;
|
|
74
|
+
this.#gate = new RepoGate(options.shelf);
|
|
136
75
|
}
|
|
137
|
-
/** The channels of running workers
|
|
76
|
+
/** The channels of running workers. */
|
|
138
77
|
runningChannels(): ChannelKey[] {
|
|
139
78
|
return [...this.#running.values()].map(({ job }) => job.channel);
|
|
140
79
|
}
|
|
@@ -142,6 +81,14 @@ export class CodingDesk {
|
|
|
142
81
|
queuedChannels(): ChannelKey[] {
|
|
143
82
|
return this.#queue.map((job) => job.channel);
|
|
144
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* The channels of work a shutdown waits for: running tasks, and the reports of finished or
|
|
86
|
+
* dropped ones still being delivered, which a stop must not cut off. Queued tasks are not
|
|
87
|
+
* among them: `beginDrain` drops them.
|
|
88
|
+
*/
|
|
89
|
+
busyChannels(): ChannelKey[] {
|
|
90
|
+
return [...this.runningChannels(), ...this.#delivering];
|
|
91
|
+
}
|
|
145
92
|
/** Workers allowed at once, and how many run and wait now. */
|
|
146
93
|
workers(): { max: number; running: number; queued: number } {
|
|
147
94
|
const { size: running } = this.#running;
|
|
@@ -166,35 +113,15 @@ export class CodingDesk {
|
|
|
166
113
|
}
|
|
167
114
|
/** Refuses while a worker's branch is being merged into the clone; running workers do not count. */
|
|
168
115
|
checkIdle(repo: string): void {
|
|
169
|
-
|
|
170
|
-
throw new AgentError(
|
|
171
|
-
`A coding worker's commits are being merged into ${repo}; try again in a moment.`,
|
|
172
|
-
);
|
|
116
|
+
this.#gate.checkIdle(repo);
|
|
173
117
|
}
|
|
174
118
|
/** Whether a report or push is in progress for the repository. */
|
|
175
119
|
shipping(repo: string): boolean {
|
|
176
|
-
return this.#shipping
|
|
120
|
+
return this.#gate.shipping(repo);
|
|
177
121
|
}
|
|
178
|
-
/**
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
*/
|
|
182
|
-
async ship<T>(repo: string, operation: () => Promise<T>): Promise<T> {
|
|
183
|
-
this.checkIdle(repo);
|
|
184
|
-
if (this.#shipping.has(repo))
|
|
185
|
-
throw new AgentError(
|
|
186
|
-
"A report or push is already in progress for this repository.",
|
|
187
|
-
);
|
|
188
|
-
const done = operation();
|
|
189
|
-
this.#shipping.set(
|
|
190
|
-
repo,
|
|
191
|
-
done.catch(() => {}),
|
|
192
|
-
);
|
|
193
|
-
try {
|
|
194
|
-
return await done;
|
|
195
|
-
} finally {
|
|
196
|
-
this.#shipping.delete(repo);
|
|
197
|
-
}
|
|
122
|
+
/** Runs a report or push of the repository, one at a time; see `RepoGate.ship`. */
|
|
123
|
+
ship<T>(repo: string, operation: () => Promise<T>): Promise<T> {
|
|
124
|
+
return this.#gate.ship(repo, operation);
|
|
198
125
|
}
|
|
199
126
|
async idle(): Promise<void> {
|
|
200
127
|
// A run that ends starts the next queued one before it settles, so look again.
|
|
@@ -246,17 +173,62 @@ export class CodingDesk {
|
|
|
246
173
|
settled: commits !== undefined,
|
|
247
174
|
};
|
|
248
175
|
}
|
|
249
|
-
/**
|
|
176
|
+
/**
|
|
177
|
+
* The host's shutdown began waiting for work: no queued task starts and `start` refuses, so the
|
|
178
|
+
* drain waits for the running workers only. Each queued task gets a report at once that it did
|
|
179
|
+
* not start, with the time left to deliver it before the queue is sealed.
|
|
180
|
+
*/
|
|
181
|
+
beginDrain(): void {
|
|
182
|
+
this.#draining = true;
|
|
183
|
+
for (const job of this.#queue.splice(0))
|
|
184
|
+
this.#track(this.#drop(job, notStartedText));
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* The host's drain ran out of time: tells each running worker to stop, as a cancel does, so its
|
|
188
|
+
* commits stay on its branch and its report names the worktree, and drops the queued tasks with
|
|
189
|
+
* a report that they never ran. Starts nothing more. Whether any task was told.
|
|
190
|
+
*/
|
|
191
|
+
interrupt(): boolean {
|
|
192
|
+
this.#stopped = true;
|
|
193
|
+
const queued = this.#queue.splice(0);
|
|
194
|
+
for (const job of queued) this.#track(this.#drop(job));
|
|
195
|
+
let told = queued.length > 0;
|
|
196
|
+
for (const run of this.#running.values()) {
|
|
197
|
+
if (run.interrupted) continue;
|
|
198
|
+
run.interrupted = true;
|
|
199
|
+
run.controller.abort(hostStopping);
|
|
200
|
+
told = true;
|
|
201
|
+
}
|
|
202
|
+
return told;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Stops the running workers as a host shutdown does and drops the queued tasks, telling each
|
|
206
|
+
* that it never ran. Waits at most `stopWaitMs` for the reports: a report turn that is still
|
|
207
|
+
* going then is left behind, not waited for.
|
|
208
|
+
*/
|
|
250
209
|
async stop(): Promise<void> {
|
|
251
210
|
this.#stopped = true;
|
|
252
211
|
for (const job of this.#queue.splice(0)) this.#track(this.#drop(job));
|
|
253
|
-
for (const run of this.#running.values())
|
|
254
|
-
|
|
212
|
+
for (const run of this.#running.values()) {
|
|
213
|
+
run.interrupted = true;
|
|
214
|
+
run.controller.abort(hostStopping);
|
|
215
|
+
}
|
|
216
|
+
const waited = this.#options.stopWaitMs ?? DEFAULT_STOP_WAIT_MS;
|
|
217
|
+
const ended = await within(
|
|
218
|
+
this.idle().then(() => true),
|
|
219
|
+
waited,
|
|
220
|
+
);
|
|
221
|
+
if (!ended)
|
|
222
|
+
this.#options.logger.warn(
|
|
223
|
+
{ waitedMs: waited },
|
|
224
|
+
"Coding reports still going out; not waiting for them.",
|
|
225
|
+
);
|
|
255
226
|
}
|
|
256
227
|
async start(
|
|
257
228
|
request: Omit<CodingJob, "id" | "startedAt" | "startHead" | "thread">,
|
|
258
229
|
): Promise<CodingStart> {
|
|
259
230
|
if (this.#stopped) throw new AgentError("The coding desk is stopped.");
|
|
231
|
+
if (this.#draining) throw new AgentError(notStartedText);
|
|
260
232
|
const task = request.task.trim();
|
|
261
233
|
if (!task) throw new AgentError("The task is required.");
|
|
262
234
|
if (task.length > MAX_CODING_TASK_CHARS)
|
|
@@ -297,7 +269,7 @@ export class CodingDesk {
|
|
|
297
269
|
run.settle = resolve;
|
|
298
270
|
});
|
|
299
271
|
this.#running.set(job.id, run);
|
|
300
|
-
run.done = this.#
|
|
272
|
+
run.done = runCodingJob(this.#context(), job, run)
|
|
301
273
|
.catch(() => {
|
|
302
274
|
this.#options.logger.error(
|
|
303
275
|
{ job: job.id },
|
|
@@ -318,6 +290,7 @@ export class CodingDesk {
|
|
|
318
290
|
#fill(): void {
|
|
319
291
|
while (
|
|
320
292
|
!this.#stopped &&
|
|
293
|
+
!this.#draining &&
|
|
321
294
|
this.#running.size < this.#maxWorkers &&
|
|
322
295
|
this.#queue.length
|
|
323
296
|
) {
|
|
@@ -330,7 +303,7 @@ export class CodingDesk {
|
|
|
330
303
|
error = "the host stopped before this queued task started; nothing ran, so start it again",
|
|
331
304
|
): Promise<void> {
|
|
332
305
|
try {
|
|
333
|
-
await this.#
|
|
306
|
+
await this.#deliver({
|
|
334
307
|
job,
|
|
335
308
|
outcome: { ok: false, error },
|
|
336
309
|
held: [],
|
|
@@ -343,308 +316,22 @@ export class CodingDesk {
|
|
|
343
316
|
);
|
|
344
317
|
}
|
|
345
318
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
speaker?.tier !== "owner" ||
|
|
354
|
-
speaker.principalId === SYSTEM_PRINCIPAL
|
|
355
|
-
)
|
|
356
|
-
return { yolo: false, longRunning: false };
|
|
357
|
-
const mode = modes.peek(job.starter.channel);
|
|
358
|
-
return { yolo: mode.yolo === true, longRunning: mode.longRunning === true };
|
|
359
|
-
}
|
|
360
|
-
/** Whether the starter's yolo mode approves the call now, as it would in that conversation. */
|
|
361
|
-
#yolo(job: CodingJob, call: HeldCall): boolean {
|
|
362
|
-
if (!this.#modeOf(job).yolo || call.approverIds !== undefined) return false;
|
|
363
|
-
const classes = call.holdClasses;
|
|
364
|
-
if (!classes?.length || !classes.every(this.#yoloApproves)) return false;
|
|
365
|
-
const { starter } = job;
|
|
366
|
-
if (!starter) return false;
|
|
367
|
-
try {
|
|
368
|
-
this.#options.modes?.autoApproved?.({
|
|
369
|
-
channel: starter.channel,
|
|
370
|
-
tool: call.tool,
|
|
371
|
-
action: `coding #${job.id} in ${job.repo}: ${call.action}`,
|
|
372
|
-
classes: [...classes],
|
|
373
|
-
principalId: starter.speaker.principalId,
|
|
374
|
-
at: new Date(),
|
|
375
|
-
});
|
|
376
|
-
} catch {
|
|
377
|
-
this.#options.logger.warn(
|
|
378
|
-
{ job: job.id },
|
|
379
|
-
"Recording a yolo approval failed.",
|
|
380
|
-
);
|
|
381
|
-
}
|
|
382
|
-
return true;
|
|
383
|
-
}
|
|
384
|
-
/**
|
|
385
|
-
* The worker's work-time budget: `timeoutMs`, then, while the starter's long-running mode is
|
|
386
|
-
* on, further steps up to `autonomy.longRunningMaxMs` in all. Card waits never count.
|
|
387
|
-
*/
|
|
388
|
-
#budget(
|
|
389
|
-
job: CodingJob,
|
|
390
|
-
slot: PromptSlot,
|
|
391
|
-
expire: (workedMs: number) => void,
|
|
392
|
-
): () => void {
|
|
393
|
-
const base = this.#options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
394
|
-
let worked = 0;
|
|
395
|
-
let cancel = () => {};
|
|
396
|
-
const phase = (ms: number) => {
|
|
397
|
-
cancel = workTimeout(ms, slot, () => {
|
|
398
|
-
worked += ms;
|
|
399
|
-
if (this.#modeOf(job).longRunning && worked < this.#longRunningMs)
|
|
400
|
-
phase(Math.min(LONG_RUNNING_STEP_MS, this.#longRunningMs - worked));
|
|
401
|
-
else expire(worked);
|
|
402
|
-
});
|
|
319
|
+
#context(): RunContext {
|
|
320
|
+
return {
|
|
321
|
+
options: this.#options,
|
|
322
|
+
modes: this.#modes,
|
|
323
|
+
gate: this.#gate,
|
|
324
|
+
free: (job) => this.#free(job),
|
|
325
|
+
deliver: (result) => this.#deliver(result),
|
|
403
326
|
};
|
|
404
|
-
phase(base);
|
|
405
|
-
return () => cancel();
|
|
406
327
|
}
|
|
407
|
-
/**
|
|
408
|
-
async #
|
|
409
|
-
|
|
410
|
-
const previous = this.#integrations.get(repo) ?? Promise.resolve();
|
|
411
|
-
const mine = previous
|
|
412
|
-
.catch(() => {})
|
|
413
|
-
.then(async () => {
|
|
414
|
-
for (let ship = this.#shipping.get(repo); ship; ) {
|
|
415
|
-
await ship;
|
|
416
|
-
ship = this.#shipping.get(repo);
|
|
417
|
-
}
|
|
418
|
-
this.#integrating.add(repo);
|
|
419
|
-
try {
|
|
420
|
-
return await this.#options.shelf.integrate(tree);
|
|
421
|
-
} finally {
|
|
422
|
-
this.#integrating.delete(repo);
|
|
423
|
-
}
|
|
424
|
-
});
|
|
425
|
-
this.#integrations.set(repo, mine);
|
|
328
|
+
/** Delivers a report, which a shutdown waits for meanwhile. */
|
|
329
|
+
async #deliver(result: CodingResult): Promise<void> {
|
|
330
|
+
this.#delivering.push(result.job.channel);
|
|
426
331
|
try {
|
|
427
|
-
|
|
332
|
+
await this.#options.deliver(result);
|
|
428
333
|
} finally {
|
|
429
|
-
|
|
430
|
-
this.#integrations.delete(repo);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
async #run(job: CodingJob, run: Running): Promise<void> {
|
|
434
|
-
const { controller } = run;
|
|
435
|
-
const { shelf, worker, prompts, threads, threadText, deliver } =
|
|
436
|
-
this.#options;
|
|
437
|
-
const slot = promptSlot();
|
|
438
|
-
const held: string[] = [];
|
|
439
|
-
const autoApproved: string[] = [];
|
|
440
|
-
let omittedHeld = 0;
|
|
441
|
-
let omittedApproved = 0;
|
|
442
|
-
const maxReport = this.#options.limits?.reportChars ?? MAX_REPORT_CHARS;
|
|
443
|
-
const maxHeldEntries =
|
|
444
|
-
this.#options.limits?.heldEntries ?? MAX_HELD_ENTRIES;
|
|
445
|
-
const maxHeldChars = this.#options.limits?.heldChars ?? MAX_HELD_CHARS;
|
|
446
|
-
let cancel = () => {};
|
|
447
|
-
let interim: InterimPoster | undefined;
|
|
448
|
-
let timedOut: number | undefined;
|
|
449
|
-
let tree: WorkerTree | undefined;
|
|
450
|
-
let outcome: CodingResult["outcome"];
|
|
451
|
-
try {
|
|
452
|
-
tree = await shelf.addWorktree(job.repo, job.id);
|
|
453
|
-
job.startHead = tree.base.slice(0, 12);
|
|
454
|
-
job.branch = tree.branch;
|
|
455
|
-
run.dir = tree.dir;
|
|
456
|
-
if (controller.signal.aborted)
|
|
457
|
-
throw new AgentError("the worker was stopped");
|
|
458
|
-
if (threads) {
|
|
459
|
-
try {
|
|
460
|
-
const thread = await threads.open(
|
|
461
|
-
job.origin,
|
|
462
|
-
`${job.repo} #${job.id}`,
|
|
463
|
-
threadText?.initial?.(job) ?? `Task:\n${job.task}`,
|
|
464
|
-
);
|
|
465
|
-
if (thread) job.thread = thread;
|
|
466
|
-
} catch {
|
|
467
|
-
this.#options.logger.warn(
|
|
468
|
-
{ job: job.id },
|
|
469
|
-
"Coding thread unavailable.",
|
|
470
|
-
);
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
if (controller.signal.aborted)
|
|
474
|
-
throw new AgentError("the worker was stopped");
|
|
475
|
-
// The worker's progress goes to its thread as it works, under the report's name.
|
|
476
|
-
if (job.thread?.interim && this.#options.interimText !== "off")
|
|
477
|
-
interim = new InterimPoster(job.thread.interim, {
|
|
478
|
-
logger: this.#options.logger,
|
|
479
|
-
channel: job.thread.channel,
|
|
480
|
-
...(this.#options.interimPrimaryChars
|
|
481
|
-
? { primaryChars: this.#options.interimPrimaryChars }
|
|
482
|
-
: {}),
|
|
483
|
-
});
|
|
484
|
-
const poster = interim;
|
|
485
|
-
slot.bind(
|
|
486
|
-
threads
|
|
487
|
-
? job.thread && prompts?.(job.thread.channel)
|
|
488
|
-
: prompts?.(job.channel),
|
|
489
|
-
`Coding worker #${job.id}`,
|
|
490
|
-
poster ? () => poster.flush() : undefined,
|
|
491
|
-
);
|
|
492
|
-
cancel = this.#budget(job, slot, (worked) => {
|
|
493
|
-
timedOut = worked;
|
|
494
|
-
controller.abort();
|
|
495
|
-
});
|
|
496
|
-
const review = async (call: HeldCall): Promise<HeldCallAnswer> => {
|
|
497
|
-
// The starter's yolo mode answers for a call it may approve, read at the time of the call.
|
|
498
|
-
if (!controller.signal.aborted && this.#yolo(job, call)) {
|
|
499
|
-
if (autoApproved.length < maxHeldEntries)
|
|
500
|
-
autoApproved.push(
|
|
501
|
-
bounded(
|
|
502
|
-
`${call.action}: ${call.tool} ${call.input}`,
|
|
503
|
-
maxHeldChars,
|
|
504
|
-
),
|
|
505
|
-
);
|
|
506
|
-
else omittedApproved++;
|
|
507
|
-
return "approved";
|
|
508
|
-
}
|
|
509
|
-
let answer: HeldCallAnswer = "held";
|
|
510
|
-
try {
|
|
511
|
-
const decision = await slot.prompts?.confirm(
|
|
512
|
-
threadText?.approvalTitle?.(job) ??
|
|
513
|
-
`${slot.asker} requests approval`,
|
|
514
|
-
approvalCard(call),
|
|
515
|
-
controller.signal,
|
|
516
|
-
undefined,
|
|
517
|
-
undefined,
|
|
518
|
-
approvalDetails(call),
|
|
519
|
-
);
|
|
520
|
-
if (decision === "approved" || decision === "declined")
|
|
521
|
-
answer = decision;
|
|
522
|
-
} catch {
|
|
523
|
-
/* A failed card never authorizes the call. */
|
|
524
|
-
}
|
|
525
|
-
// A call the owner declined is settled, not held for the report.
|
|
526
|
-
if (answer === "held") {
|
|
527
|
-
if (held.length < maxHeldEntries) {
|
|
528
|
-
const entry = bounded(
|
|
529
|
-
`${call.action}: ${call.tool} ${call.input}`,
|
|
530
|
-
maxHeldChars,
|
|
531
|
-
);
|
|
532
|
-
held.push(entry);
|
|
533
|
-
try {
|
|
534
|
-
await interim?.flush();
|
|
535
|
-
await job.thread?.post(
|
|
536
|
-
threadText?.held?.(entry, job) ?? `Held: ${entry}`,
|
|
537
|
-
);
|
|
538
|
-
} catch {
|
|
539
|
-
/* A failed progress post does not discard the held action. */
|
|
540
|
-
}
|
|
541
|
-
} else omittedHeld++;
|
|
542
|
-
}
|
|
543
|
-
return answer;
|
|
544
|
-
};
|
|
545
|
-
const report = await worker.run(
|
|
546
|
-
{ ...job, dir: tree.dir },
|
|
547
|
-
controller.signal,
|
|
548
|
-
review,
|
|
549
|
-
interim,
|
|
550
|
-
);
|
|
551
|
-
if (controller.signal.aborted)
|
|
552
|
-
throw new AgentError("the worker was stopped");
|
|
553
|
-
outcome = { ok: true, report: bounded(report, maxReport) };
|
|
554
|
-
} catch (error) {
|
|
555
|
-
// The host's own refusals and the worker's scrubbed, bounded error text reach the report.
|
|
556
|
-
let message =
|
|
557
|
-
"The worker failed; inspect the clone and worker configuration on the host.";
|
|
558
|
-
if (error instanceof AgentError) message = error.message;
|
|
559
|
-
if (error instanceof CodingWorkerFailure) {
|
|
560
|
-
this.#options.logger.warn(
|
|
561
|
-
{ job: job.id, category: error.category, exitCode: error.exitCode },
|
|
562
|
-
"Coding worker failed.",
|
|
563
|
-
);
|
|
564
|
-
}
|
|
565
|
-
if (controller.signal.aborted) message = "the worker was stopped";
|
|
566
|
-
if (timedOut !== undefined)
|
|
567
|
-
message = `the worker ran out of time (${Math.round(timedOut / 60_000)} minutes)`;
|
|
568
|
-
if (run.cancelledBy !== undefined)
|
|
569
|
-
message = cancelledText(run.cancelledBy);
|
|
570
|
-
outcome = { ok: false, error: message };
|
|
571
|
-
} finally {
|
|
572
|
-
cancel();
|
|
573
|
-
slot.unbind();
|
|
574
|
-
// What the worker wrote lands before its report.
|
|
575
|
-
await interim?.flush();
|
|
576
|
-
}
|
|
577
|
-
let integration: Integration | undefined;
|
|
578
|
-
// A cancelled worker's commits stay on its branch for the owner to inspect, merge or delete.
|
|
579
|
-
if (tree && run.cancelledBy !== undefined)
|
|
580
|
-
integration = {
|
|
581
|
-
status: "refused",
|
|
582
|
-
branch: tree.branch,
|
|
583
|
-
dir: tree.dir,
|
|
584
|
-
reason: `the task was cancelled by ${run.cancelledBy}`,
|
|
585
|
-
};
|
|
586
|
-
else if (tree)
|
|
587
|
-
try {
|
|
588
|
-
integration = await this.#integrate(tree);
|
|
589
|
-
} catch (error) {
|
|
590
|
-
integration = {
|
|
591
|
-
status: "refused",
|
|
592
|
-
branch: tree.branch,
|
|
593
|
-
dir: tree.dir,
|
|
594
|
-
reason:
|
|
595
|
-
error instanceof AgentError
|
|
596
|
-
? error.message
|
|
597
|
-
: "the merge failed; inspect the clone and the worktree on the host",
|
|
598
|
-
};
|
|
599
|
-
}
|
|
600
|
-
let state: RepoState | undefined;
|
|
601
|
-
let commits: string[] = [];
|
|
602
|
-
try {
|
|
603
|
-
if (
|
|
604
|
-
tree &&
|
|
605
|
-
(integration?.status === "conflict" ||
|
|
606
|
-
integration?.status === "refused")
|
|
607
|
-
) {
|
|
608
|
-
// The work stays on its branch: describe the worktree.
|
|
609
|
-
state = await shelf.treeState(tree);
|
|
610
|
-
commits = await shelf.treeCommits(tree);
|
|
611
|
-
} else {
|
|
612
|
-
state = await shelf.state(job.repo);
|
|
613
|
-
if (integration?.status === "merged") commits = integration.commits;
|
|
614
|
-
}
|
|
615
|
-
} catch {
|
|
616
|
-
this.#options.logger.warn(
|
|
617
|
-
{ job: job.id },
|
|
618
|
-
"Repository state unreadable.",
|
|
619
|
-
);
|
|
620
|
-
}
|
|
621
|
-
if (omittedHeld)
|
|
622
|
-
held.push(`[${omittedHeld} more unapproved actions omitted]`);
|
|
623
|
-
if (omittedApproved)
|
|
624
|
-
autoApproved.push(`[${omittedApproved} more approved actions omitted]`);
|
|
625
|
-
const result: CodingResult = {
|
|
626
|
-
job,
|
|
627
|
-
outcome,
|
|
628
|
-
held,
|
|
629
|
-
...(autoApproved.length ? { autoApproved } : {}),
|
|
630
|
-
...(state ? { state } : {}),
|
|
631
|
-
commits,
|
|
632
|
-
...(integration ? { integration } : {}),
|
|
633
|
-
};
|
|
634
|
-
try {
|
|
635
|
-
await job.thread?.close(
|
|
636
|
-
bounded(
|
|
637
|
-
threadText?.report?.(result) ??
|
|
638
|
-
codingReport(result, job.startedAt.toISOString()),
|
|
639
|
-
maxReport,
|
|
640
|
-
),
|
|
641
|
-
);
|
|
642
|
-
} catch {
|
|
643
|
-
this.#options.logger.warn({ job: job.id }, "Coding thread close failed.");
|
|
334
|
+
this.#delivering.splice(this.#delivering.indexOf(result.job.channel), 1);
|
|
644
335
|
}
|
|
645
|
-
run.settle?.(commits);
|
|
646
|
-
// The work is over: free the slot and the repository before the report turn, so that turn can ship it.
|
|
647
|
-
this.#free(job);
|
|
648
|
-
await deliver(result);
|
|
649
336
|
}
|
|
650
337
|
}
|
package/src/coding-options.ts
CHANGED
|
@@ -44,27 +44,44 @@ export interface CodingToolText {
|
|
|
44
44
|
parameters?: Record<string, string>;
|
|
45
45
|
}
|
|
46
46
|
export interface CodingPresentation {
|
|
47
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* The repo_list result; unset: the managed clones as JSON. `defaultText` is that default, so a
|
|
49
|
+
* hook can append to it.
|
|
50
|
+
*/
|
|
48
51
|
list?(
|
|
49
52
|
repos: (RepoSummary & { skills: string[] })[],
|
|
50
53
|
context: { shelfDir: string; fetched: boolean },
|
|
54
|
+
defaultText: string,
|
|
51
55
|
): string;
|
|
52
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* Separate the owner's posted record from the model's shipping instructions. `defaults` is the
|
|
58
|
+
* package's own pair, so a hook can append to either.
|
|
59
|
+
*/
|
|
53
60
|
changeReport?(
|
|
54
61
|
report: ChangeReport,
|
|
55
62
|
direct: boolean,
|
|
63
|
+
defaults: { post: string; result: string },
|
|
56
64
|
): { post: string; result: string };
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
/** The repo_task result for a started task; `defaultText` is the package's, which names `repo_task_cancel`. */
|
|
66
|
+
taskStarted?(
|
|
67
|
+
job: CodingJob,
|
|
68
|
+
skippedSkills: string[],
|
|
69
|
+
defaultText: string,
|
|
70
|
+
): string;
|
|
59
71
|
/**
|
|
60
72
|
* Wording of the Settings page, keyed by its English text, such as
|
|
61
73
|
* `{ "Coding workers": "..." }`; it wins over the built-in zh-TW wording, key by key.
|
|
62
74
|
*/
|
|
63
75
|
messages?: Readonly<Record<string, string>>;
|
|
76
|
+
/**
|
|
77
|
+
* The repo_task result for a task waiting for a worker slot, at `position` (from 1) in the
|
|
78
|
+
* queue; `defaultText` is the package's.
|
|
79
|
+
*/
|
|
64
80
|
taskQueued?(
|
|
65
81
|
job: CodingJob,
|
|
66
82
|
position: number,
|
|
67
83
|
skippedSkills: string[],
|
|
84
|
+
defaultText: string,
|
|
68
85
|
): string;
|
|
69
86
|
}
|
|
70
87
|
export interface CodingOptions {
|