pi-roundtable-coding 0.43.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 +6 -0
- package/README.md +1 -1
- package/package.json +3 -3
- package/src/coding-desk.ts +13 -265
- package/src/desk-run.ts +279 -0
- package/src/git-run.ts +120 -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/worktrees.ts +248 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.44.0] - 2026-11-09
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- Two coding workers that start at once on one repository no longer fail now and then with `fatal: failed to read .git/worktrees/1/commondir`, which ended one of the tasks before its worker ran. Git lists every linked worktree while it adds, removes or prunes one and fails on a folder another such call is still writing, so the desk now makes, removes and prunes the worktrees of one clone one at a time.
|
|
10
|
+
|
|
5
11
|
## [0.43.0] - 2026-11-09
|
|
6
12
|
|
|
7
13
|
### Added
|
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ This package is a reference for the plugin guide's [Helpers for a Pi session of
|
|
|
7
7
|
## Requirements
|
|
8
8
|
|
|
9
9
|
- Bun 1.4.4 or newer on a POSIX host (Linux or macOS).
|
|
10
|
-
- pi-roundtable `>=0.
|
|
10
|
+
- pi-roundtable `>=0.44.0 <0.45.0` as a peer dependency; this package uses only its public main, kit and testing entries.
|
|
11
11
|
- Pi `>=1.0.0 <2`, shared with the host's core dependencies.
|
|
12
12
|
A core-only host still pinned to Pi 0.99.x must update its Pi dependencies before adding coding, so cross-package extension and session types resolve to one Pi version.
|
|
13
13
|
- Git, plus GitHub CLI (`gh`) for the default clone implementation.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-roundtable-coding",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.44.0",
|
|
4
4
|
"description": "Repository shelves and owner-approved Pi coding workers for pi-roundtable",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,13 +38,13 @@
|
|
|
38
38
|
"typebox": "1.3.37"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"pi-roundtable": ">=0.
|
|
41
|
+
"pi-roundtable": ">=0.44.0 <0.45.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@earendil-works/pi-ai": ">=1.0.0 <2",
|
|
45
45
|
"@biomejs/biome": "2.5.15",
|
|
46
46
|
"@types/bun": "1.4.3",
|
|
47
|
-
"pi-roundtable": "0.
|
|
47
|
+
"pi-roundtable": "0.44.0",
|
|
48
48
|
"typescript": "7.0.2"
|
|
49
49
|
}
|
|
50
50
|
}
|
package/src/coding-desk.ts
CHANGED
|
@@ -1,36 +1,23 @@
|
|
|
1
|
-
import type { ChannelKey
|
|
2
|
-
import {
|
|
3
|
-
AgentError,
|
|
4
|
-
approvalCard,
|
|
5
|
-
approvalDetails,
|
|
6
|
-
InterimPoster,
|
|
7
|
-
promptSlot,
|
|
8
|
-
} from "pi-roundtable/kit";
|
|
9
|
-
import { codingReport } from "./coding-report.ts";
|
|
1
|
+
import type { ChannelKey } from "pi-roundtable";
|
|
2
|
+
import { AgentError } from "pi-roundtable/kit";
|
|
10
3
|
import { DeskModes } from "./desk-modes.ts";
|
|
4
|
+
import { type RunContext, type Running, runCodingJob } from "./desk-run.ts";
|
|
11
5
|
import type {
|
|
12
6
|
CodingDeskOptions,
|
|
13
7
|
CodingJob,
|
|
14
8
|
CodingResult,
|
|
15
9
|
CodingStart,
|
|
16
|
-
HeldCallAnswer,
|
|
17
10
|
} from "./desk-types.ts";
|
|
18
11
|
import { checkCount, checkMaxWorkers } from "./limits.ts";
|
|
19
12
|
import { RepoGate } from "./repo-gate.ts";
|
|
20
|
-
import type { Integration, RepoState, WorkerTree } from "./repo-shelf.ts";
|
|
21
13
|
import {
|
|
22
14
|
type CancelResult,
|
|
23
15
|
type CodingTask,
|
|
24
|
-
cancelledText,
|
|
25
16
|
droppedText,
|
|
26
17
|
hostStopping,
|
|
27
|
-
interruptedText,
|
|
28
|
-
notMergedReason,
|
|
29
18
|
notStartedText,
|
|
30
|
-
stoppedOutsideText,
|
|
31
19
|
within,
|
|
32
20
|
} from "./task-control.ts";
|
|
33
|
-
import { CodingWorkerFailure } from "./worker-failure.ts";
|
|
34
21
|
|
|
35
22
|
export { codingReport, integrationText } from "./coding-report.ts";
|
|
36
23
|
export type {
|
|
@@ -51,35 +38,11 @@ export type {
|
|
|
51
38
|
export { checkLimit } from "./limits.ts";
|
|
52
39
|
|
|
53
40
|
export const MAX_CODING_TASK_CHARS = 8_000;
|
|
54
|
-
const MAX_REPORT_CHARS = 20_000;
|
|
55
|
-
const MAX_HELD_ENTRIES = 10;
|
|
56
|
-
const MAX_HELD_CHARS = 1_000;
|
|
57
|
-
function bounded(text: string, max: number): string {
|
|
58
|
-
const suffix = "\n[truncated]";
|
|
59
|
-
return text.length > max
|
|
60
|
-
? `${text.slice(0, max - suffix.length)}${suffix}`
|
|
61
|
-
: text;
|
|
62
|
-
}
|
|
63
41
|
|
|
64
42
|
const DEFAULT_MAX_WORKERS = 4;
|
|
65
43
|
const DEFAULT_MAX_QUEUED = 16;
|
|
66
44
|
const DEFAULT_STOP_WAIT_MS = 15_000;
|
|
67
45
|
|
|
68
|
-
interface Running {
|
|
69
|
-
job: CodingJob;
|
|
70
|
-
controller: AbortController;
|
|
71
|
-
done?: Promise<void>;
|
|
72
|
-
/** Who cancelled it; its branch is then kept, not merged. */
|
|
73
|
-
cancelledBy?: string;
|
|
74
|
-
/** The host's shutdown told it to stop; its branch is then kept, not merged. */
|
|
75
|
-
interrupted?: boolean;
|
|
76
|
-
/** Resolves with its kept commits once the worker settles. */
|
|
77
|
-
settled?: Promise<string[]>;
|
|
78
|
-
settle?: (commits: string[]) => void;
|
|
79
|
-
/** Its worktree, once made. */
|
|
80
|
-
dir?: string;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
46
|
/**
|
|
84
47
|
* Up to `maxWorkers` workers at once, each in its own worktree; more tasks wait in a FIFO queue.
|
|
85
48
|
* A finished worker's branch is rebased onto its clone and fast-forwarded, one clone at a time.
|
|
@@ -306,7 +269,7 @@ export class CodingDesk {
|
|
|
306
269
|
run.settle = resolve;
|
|
307
270
|
});
|
|
308
271
|
this.#running.set(job.id, run);
|
|
309
|
-
run.done = this.#
|
|
272
|
+
run.done = runCodingJob(this.#context(), job, run)
|
|
310
273
|
.catch(() => {
|
|
311
274
|
this.#options.logger.error(
|
|
312
275
|
{ job: job.id },
|
|
@@ -353,6 +316,15 @@ export class CodingDesk {
|
|
|
353
316
|
);
|
|
354
317
|
}
|
|
355
318
|
}
|
|
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),
|
|
326
|
+
};
|
|
327
|
+
}
|
|
356
328
|
/** Delivers a report, which a shutdown waits for meanwhile. */
|
|
357
329
|
async #deliver(result: CodingResult): Promise<void> {
|
|
358
330
|
this.#delivering.push(result.job.channel);
|
|
@@ -362,228 +334,4 @@ export class CodingDesk {
|
|
|
362
334
|
this.#delivering.splice(this.#delivering.indexOf(result.job.channel), 1);
|
|
363
335
|
}
|
|
364
336
|
}
|
|
365
|
-
async #run(job: CodingJob, run: Running): Promise<void> {
|
|
366
|
-
const { controller } = run;
|
|
367
|
-
const { shelf, worker, prompts, threads, threadText } = this.#options;
|
|
368
|
-
const slot = promptSlot();
|
|
369
|
-
const held: string[] = [];
|
|
370
|
-
const autoApproved: string[] = [];
|
|
371
|
-
let omittedHeld = 0;
|
|
372
|
-
let omittedApproved = 0;
|
|
373
|
-
const maxReport = this.#options.limits?.reportChars ?? MAX_REPORT_CHARS;
|
|
374
|
-
const maxHeldEntries =
|
|
375
|
-
this.#options.limits?.heldEntries ?? MAX_HELD_ENTRIES;
|
|
376
|
-
const maxHeldChars = this.#options.limits?.heldChars ?? MAX_HELD_CHARS;
|
|
377
|
-
let cancel = () => {};
|
|
378
|
-
let interim: InterimPoster | undefined;
|
|
379
|
-
let timedOut: number | undefined;
|
|
380
|
-
let stoppedOutside = false;
|
|
381
|
-
let tree: WorkerTree | undefined;
|
|
382
|
-
let outcome: CodingResult["outcome"];
|
|
383
|
-
try {
|
|
384
|
-
tree = await shelf.addWorktree(job.repo, job.id);
|
|
385
|
-
job.startHead = tree.base.slice(0, 12);
|
|
386
|
-
job.branch = tree.branch;
|
|
387
|
-
run.dir = tree.dir;
|
|
388
|
-
if (controller.signal.aborted)
|
|
389
|
-
throw new AgentError("the worker was stopped");
|
|
390
|
-
if (threads) {
|
|
391
|
-
try {
|
|
392
|
-
const thread = await threads.open(
|
|
393
|
-
job.origin,
|
|
394
|
-
`${job.repo} #${job.id}`,
|
|
395
|
-
threadText?.initial?.(job) ?? `Task:\n${job.task}`,
|
|
396
|
-
);
|
|
397
|
-
if (thread) job.thread = thread;
|
|
398
|
-
} catch {
|
|
399
|
-
this.#options.logger.warn(
|
|
400
|
-
{ job: job.id },
|
|
401
|
-
"Coding thread unavailable.",
|
|
402
|
-
);
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
if (controller.signal.aborted)
|
|
406
|
-
throw new AgentError("the worker was stopped");
|
|
407
|
-
// The worker's progress goes to its thread as it works, under the report's name.
|
|
408
|
-
if (job.thread?.interim && this.#options.interimText !== "off")
|
|
409
|
-
interim = new InterimPoster(job.thread.interim, {
|
|
410
|
-
logger: this.#options.logger,
|
|
411
|
-
channel: job.thread.channel,
|
|
412
|
-
...(this.#options.interimPrimaryChars
|
|
413
|
-
? { primaryChars: this.#options.interimPrimaryChars }
|
|
414
|
-
: {}),
|
|
415
|
-
});
|
|
416
|
-
const poster = interim;
|
|
417
|
-
slot.bind(
|
|
418
|
-
threads
|
|
419
|
-
? job.thread && prompts?.(job.thread.channel)
|
|
420
|
-
: prompts?.(job.channel),
|
|
421
|
-
`Coding worker #${job.id}`,
|
|
422
|
-
poster ? () => poster.flush() : undefined,
|
|
423
|
-
);
|
|
424
|
-
cancel = this.#modes.budget(job, slot, (worked) => {
|
|
425
|
-
timedOut = worked;
|
|
426
|
-
controller.abort();
|
|
427
|
-
});
|
|
428
|
-
const review = async (call: HeldCall): Promise<HeldCallAnswer> => {
|
|
429
|
-
// The starter's yolo mode answers for a call it may approve, read at the time of the call.
|
|
430
|
-
if (!controller.signal.aborted && this.#modes.yolo(job, call)) {
|
|
431
|
-
if (autoApproved.length < maxHeldEntries)
|
|
432
|
-
autoApproved.push(
|
|
433
|
-
bounded(
|
|
434
|
-
`${call.action}: ${call.tool} ${call.input}`,
|
|
435
|
-
maxHeldChars,
|
|
436
|
-
),
|
|
437
|
-
);
|
|
438
|
-
else omittedApproved++;
|
|
439
|
-
return "approved";
|
|
440
|
-
}
|
|
441
|
-
let answer: HeldCallAnswer = "held";
|
|
442
|
-
try {
|
|
443
|
-
const decision = await slot.prompts?.confirm(
|
|
444
|
-
threadText?.approvalTitle?.(job) ??
|
|
445
|
-
`${slot.asker} requests approval`,
|
|
446
|
-
approvalCard(call),
|
|
447
|
-
controller.signal,
|
|
448
|
-
undefined,
|
|
449
|
-
undefined,
|
|
450
|
-
approvalDetails(call),
|
|
451
|
-
);
|
|
452
|
-
if (decision === "approved" || decision === "declined")
|
|
453
|
-
answer = decision;
|
|
454
|
-
} catch {
|
|
455
|
-
/* A failed card never authorizes the call. */
|
|
456
|
-
}
|
|
457
|
-
// A call the owner declined is settled, not held for the report.
|
|
458
|
-
if (answer === "held") {
|
|
459
|
-
if (held.length < maxHeldEntries) {
|
|
460
|
-
const entry = bounded(
|
|
461
|
-
`${call.action}: ${call.tool} ${call.input}`,
|
|
462
|
-
maxHeldChars,
|
|
463
|
-
);
|
|
464
|
-
held.push(entry);
|
|
465
|
-
try {
|
|
466
|
-
await interim?.flush();
|
|
467
|
-
await job.thread?.post(
|
|
468
|
-
threadText?.held?.(entry, job) ?? `Held: ${entry}`,
|
|
469
|
-
);
|
|
470
|
-
} catch {
|
|
471
|
-
/* A failed progress post does not discard the held action. */
|
|
472
|
-
}
|
|
473
|
-
} else omittedHeld++;
|
|
474
|
-
}
|
|
475
|
-
return answer;
|
|
476
|
-
};
|
|
477
|
-
const report = await worker.run(
|
|
478
|
-
{ ...job, dir: tree.dir },
|
|
479
|
-
controller.signal,
|
|
480
|
-
review,
|
|
481
|
-
interim,
|
|
482
|
-
);
|
|
483
|
-
if (controller.signal.aborted)
|
|
484
|
-
throw new AgentError("the worker was stopped");
|
|
485
|
-
outcome = { ok: true, report: bounded(report, maxReport) };
|
|
486
|
-
} catch (error) {
|
|
487
|
-
// The host's own refusals and the worker's scrubbed, bounded error text reach the report.
|
|
488
|
-
let message =
|
|
489
|
-
"The worker failed; inspect the clone and worker configuration on the host.";
|
|
490
|
-
if (error instanceof AgentError) message = error.message;
|
|
491
|
-
if (error instanceof CodingWorkerFailure) {
|
|
492
|
-
if (error.category === "stopped" && !controller.signal.aborted)
|
|
493
|
-
stoppedOutside = true;
|
|
494
|
-
this.#options.logger.warn(
|
|
495
|
-
{ job: job.id, category: error.category, exitCode: error.exitCode },
|
|
496
|
-
"Coding worker failed.",
|
|
497
|
-
);
|
|
498
|
-
}
|
|
499
|
-
if (controller.signal.aborted) message = "the worker was stopped";
|
|
500
|
-
if (stoppedOutside) message = stoppedOutsideText;
|
|
501
|
-
if (run.interrupted) message = interruptedText;
|
|
502
|
-
if (timedOut !== undefined)
|
|
503
|
-
message = `the worker ran out of time (${Math.round(timedOut / 60_000)} minutes)`;
|
|
504
|
-
if (run.cancelledBy !== undefined)
|
|
505
|
-
message = cancelledText(run.cancelledBy);
|
|
506
|
-
outcome = { ok: false, error: message };
|
|
507
|
-
} finally {
|
|
508
|
-
cancel();
|
|
509
|
-
slot.unbind();
|
|
510
|
-
// What the worker wrote lands before its report.
|
|
511
|
-
await interim?.flush();
|
|
512
|
-
}
|
|
513
|
-
let integration: Integration | undefined;
|
|
514
|
-
// An aborted worker's commits stay on its branch for the owner to inspect, merge or delete.
|
|
515
|
-
if (
|
|
516
|
-
tree &&
|
|
517
|
-
(controller.signal.aborted || run.interrupted || stoppedOutside)
|
|
518
|
-
)
|
|
519
|
-
integration = {
|
|
520
|
-
status: "refused",
|
|
521
|
-
branch: tree.branch,
|
|
522
|
-
dir: tree.dir,
|
|
523
|
-
reason: notMergedReason({ ...run, timedOut, stoppedOutside }),
|
|
524
|
-
};
|
|
525
|
-
else if (tree)
|
|
526
|
-
try {
|
|
527
|
-
integration = await this.#gate.integrate(tree);
|
|
528
|
-
} catch (error) {
|
|
529
|
-
integration = {
|
|
530
|
-
status: "refused",
|
|
531
|
-
branch: tree.branch,
|
|
532
|
-
dir: tree.dir,
|
|
533
|
-
reason:
|
|
534
|
-
error instanceof AgentError
|
|
535
|
-
? error.message
|
|
536
|
-
: "the merge failed; inspect the clone and the worktree on the host",
|
|
537
|
-
};
|
|
538
|
-
}
|
|
539
|
-
let state: RepoState | undefined;
|
|
540
|
-
let commits: string[] = [];
|
|
541
|
-
try {
|
|
542
|
-
if (
|
|
543
|
-
tree &&
|
|
544
|
-
(integration?.status === "conflict" ||
|
|
545
|
-
integration?.status === "refused")
|
|
546
|
-
) {
|
|
547
|
-
// The work stays on its branch: describe the worktree.
|
|
548
|
-
state = await shelf.treeState(tree);
|
|
549
|
-
commits = await shelf.treeCommits(tree);
|
|
550
|
-
} else {
|
|
551
|
-
state = await shelf.state(job.repo);
|
|
552
|
-
if (integration?.status === "merged") commits = integration.commits;
|
|
553
|
-
}
|
|
554
|
-
} catch {
|
|
555
|
-
this.#options.logger.warn(
|
|
556
|
-
{ job: job.id },
|
|
557
|
-
"Repository state unreadable.",
|
|
558
|
-
);
|
|
559
|
-
}
|
|
560
|
-
if (omittedHeld)
|
|
561
|
-
held.push(`[${omittedHeld} more unapproved actions omitted]`);
|
|
562
|
-
if (omittedApproved)
|
|
563
|
-
autoApproved.push(`[${omittedApproved} more approved actions omitted]`);
|
|
564
|
-
const result: CodingResult = {
|
|
565
|
-
job,
|
|
566
|
-
outcome,
|
|
567
|
-
held,
|
|
568
|
-
...(autoApproved.length ? { autoApproved } : {}),
|
|
569
|
-
...(state ? { state } : {}),
|
|
570
|
-
commits,
|
|
571
|
-
...(integration ? { integration } : {}),
|
|
572
|
-
};
|
|
573
|
-
try {
|
|
574
|
-
await job.thread?.close(
|
|
575
|
-
bounded(
|
|
576
|
-
threadText?.report?.(result) ??
|
|
577
|
-
codingReport(result, job.startedAt.toISOString()),
|
|
578
|
-
maxReport,
|
|
579
|
-
),
|
|
580
|
-
);
|
|
581
|
-
} catch {
|
|
582
|
-
this.#options.logger.warn({ job: job.id }, "Coding thread close failed.");
|
|
583
|
-
}
|
|
584
|
-
run.settle?.(commits);
|
|
585
|
-
// The work is over: free the slot and the repository before the report turn, so that turn can ship it.
|
|
586
|
-
this.#free(job);
|
|
587
|
-
await this.#deliver(result);
|
|
588
|
-
}
|
|
589
337
|
}
|
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
|
+
}
|