pi-roundtable-coding 0.40.0 → 0.43.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 +26 -0
- package/README.md +23 -8
- package/package.json +3 -3
- package/src/coding-desk.ts +215 -369
- package/src/coding-options.ts +24 -5
- package/src/coding-plugin.ts +66 -54
- package/src/coding-report.ts +45 -0
- package/src/coding-settings.ts +107 -1
- package/src/desk-modes.ts +107 -0
- package/src/desk-types.ts +158 -0
- package/src/i18n/zh-tw.ts +15 -0
- package/src/index.ts +2 -0
- package/src/pi-coding-worker.ts +26 -3
- package/src/repo-gate.ts +77 -0
- package/src/repo-tools.ts +2 -0
- package/src/task-control.ts +122 -0
- package/src/task-tools.ts +105 -0
- package/src/tool-text.ts +54 -0
- package/src/worker-entry.ts +49 -3
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChannelKey,
|
|
3
|
+
ConversationModes,
|
|
4
|
+
HeldCall,
|
|
5
|
+
InterimTextMode,
|
|
6
|
+
Logger,
|
|
7
|
+
OwnerPrompts,
|
|
8
|
+
ThinkingLevel,
|
|
9
|
+
Tier,
|
|
10
|
+
} from "pi-roundtable";
|
|
11
|
+
import type { DispatchThread, DispatchThreads } from "pi-roundtable/kit";
|
|
12
|
+
import type { Integration, RepoShelf, RepoState } from "./repo-shelf.ts";
|
|
13
|
+
|
|
14
|
+
export type HeldCallAnswer = "approved" | "declined" | "held";
|
|
15
|
+
/** Who spoke the turn that started a job, as yolo and long-running modes need to know. */
|
|
16
|
+
export interface CodingSpeaker {
|
|
17
|
+
id: string;
|
|
18
|
+
principalId: string;
|
|
19
|
+
name?: string;
|
|
20
|
+
tier?: Tier;
|
|
21
|
+
}
|
|
22
|
+
/** The conversation whose turn started a job, whose modes the worker follows live. */
|
|
23
|
+
export interface CodingStarter {
|
|
24
|
+
channel: ChannelKey;
|
|
25
|
+
speaker: CodingSpeaker;
|
|
26
|
+
}
|
|
27
|
+
export interface CodingJob {
|
|
28
|
+
id: number;
|
|
29
|
+
repo: string;
|
|
30
|
+
task: string;
|
|
31
|
+
channel: ChannelKey;
|
|
32
|
+
/** The agent whose turn started it, which may list and cancel it. */
|
|
33
|
+
agent?: string;
|
|
34
|
+
/** The calling turn's channel, where a progress thread opens. */
|
|
35
|
+
origin?: ChannelKey;
|
|
36
|
+
/** The conversation that started it; its yolo and long-running modes apply while it works. */
|
|
37
|
+
starter?: CodingStarter;
|
|
38
|
+
/** Host-resolved names of the skills carried to this run. */
|
|
39
|
+
skillNames?: string[];
|
|
40
|
+
/** Progress thread, when the surface supports one. */
|
|
41
|
+
thread?: DispatchThread;
|
|
42
|
+
model: string;
|
|
43
|
+
thinking: ThinkingLevel;
|
|
44
|
+
skillFiles: string[];
|
|
45
|
+
/** When it was asked for; a queued job starts later. */
|
|
46
|
+
queuedAt?: Date;
|
|
47
|
+
/** When its worker started. */
|
|
48
|
+
startedAt: Date;
|
|
49
|
+
startHead: string;
|
|
50
|
+
/** The worker's own branch, in its own worktree, once it has started. */
|
|
51
|
+
branch?: string;
|
|
52
|
+
}
|
|
53
|
+
/** A started or queued job, as `CodingDesk.start` answers. */
|
|
54
|
+
export type CodingStart = CodingJob & {
|
|
55
|
+
/** Its place in the queue, from 1, when every worker slot is taken; absent when it started. */
|
|
56
|
+
queuePosition?: number;
|
|
57
|
+
};
|
|
58
|
+
/** What the worker writes and runs as it goes, for its thread's progress posts. */
|
|
59
|
+
export interface CodingProgress {
|
|
60
|
+
/** A finished message of the worker's session: its role, stop reason, and text. */
|
|
61
|
+
messageEnd(message: {
|
|
62
|
+
role: string;
|
|
63
|
+
content?: unknown;
|
|
64
|
+
stopReason?: string;
|
|
65
|
+
}): void;
|
|
66
|
+
/** A tool the worker started. */
|
|
67
|
+
toolStart(name: string): void;
|
|
68
|
+
}
|
|
69
|
+
export interface CodingWorker {
|
|
70
|
+
/** `progress`, when given, hears the session's messages and tools as the worker goes. */
|
|
71
|
+
run(
|
|
72
|
+
job: CodingJob & { dir: string },
|
|
73
|
+
signal: AbortSignal,
|
|
74
|
+
review: (call: HeldCall) => Promise<HeldCallAnswer>,
|
|
75
|
+
progress?: CodingProgress,
|
|
76
|
+
): Promise<string>;
|
|
77
|
+
}
|
|
78
|
+
export interface CodingResult {
|
|
79
|
+
job: CodingJob;
|
|
80
|
+
outcome: { ok: true; report: string } | { ok: false; error: string };
|
|
81
|
+
held: string[];
|
|
82
|
+
/** Held calls the starter's yolo mode approved without a card. */
|
|
83
|
+
autoApproved?: string[];
|
|
84
|
+
state?: RepoState;
|
|
85
|
+
commits: string[];
|
|
86
|
+
/** How the worker's branch came back into the clone; absent when no worktree was made. */
|
|
87
|
+
integration?: Integration;
|
|
88
|
+
}
|
|
89
|
+
/** The conversations' modes, read live for each held call and at each work-time limit. */
|
|
90
|
+
export type CodingModes = Pick<ConversationModes, "peek"> &
|
|
91
|
+
Partial<Pick<ConversationModes, "autoApproved">>;
|
|
92
|
+
/**
|
|
93
|
+
* The host's autonomy limits, as its `autonomy` config resolves them: what yolo may approve, and
|
|
94
|
+
* how long a long-running run works in all.
|
|
95
|
+
*/
|
|
96
|
+
export interface CodingAutonomy {
|
|
97
|
+
yolo?: { allow?: readonly string[]; card?: readonly string[] };
|
|
98
|
+
/** Default four hours; at most 2147483647. */
|
|
99
|
+
longRunningMaxMs?: number;
|
|
100
|
+
}
|
|
101
|
+
export interface CodingThreadText {
|
|
102
|
+
initial?(job: CodingJob): string;
|
|
103
|
+
held?(entry: string, job: CodingJob): string;
|
|
104
|
+
approvalTitle?(job: CodingJob): string;
|
|
105
|
+
report?(result: CodingResult): string;
|
|
106
|
+
}
|
|
107
|
+
export interface CodingDeskOptions {
|
|
108
|
+
shelf: Pick<
|
|
109
|
+
RepoShelf,
|
|
110
|
+
| "dirOf"
|
|
111
|
+
| "state"
|
|
112
|
+
| "addWorktree"
|
|
113
|
+
| "integrate"
|
|
114
|
+
| "treeState"
|
|
115
|
+
| "treeCommits"
|
|
116
|
+
>;
|
|
117
|
+
worker: CodingWorker;
|
|
118
|
+
prompts?(channel: ChannelKey): OwnerPrompts | undefined;
|
|
119
|
+
/** When configured, approval cards stay in the job's thread; no thread means held. */
|
|
120
|
+
threads?: Pick<DispatchThreads, "open">;
|
|
121
|
+
threadText?: CodingThreadText;
|
|
122
|
+
deliver(result: CodingResult): Promise<void>;
|
|
123
|
+
logger: Logger;
|
|
124
|
+
timeoutMs?: number;
|
|
125
|
+
limits?: CodingLimits;
|
|
126
|
+
/**
|
|
127
|
+
* Whether the worker posts the text it writes before its report into its thread as it goes,
|
|
128
|
+
* as the host's turns do; "on" by default. A thread whose host cannot edit its posts gets none.
|
|
129
|
+
*/
|
|
130
|
+
interimText?: InterimTextMode;
|
|
131
|
+
/** Length from which an intermediate text is posted as its own message; default 400. */
|
|
132
|
+
interimPrimaryChars?: number;
|
|
133
|
+
/** Workers running at once on the host, across repositories and channels; default 4. */
|
|
134
|
+
maxWorkers?: number;
|
|
135
|
+
/** Tasks waiting for a worker slot, first in first out; one more is refused. Default 16. */
|
|
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;
|
|
142
|
+
/**
|
|
143
|
+
* The conversations' modes: with them a worker follows the yolo and long-running modes of the
|
|
144
|
+
* conversation that started it, read live, when an owner other than the host spoke that turn.
|
|
145
|
+
*/
|
|
146
|
+
modes?: CodingModes;
|
|
147
|
+
/** The host's autonomy limits for those modes; pass the same values its `autonomy` config sets. */
|
|
148
|
+
autonomy?: CodingAutonomy;
|
|
149
|
+
}
|
|
150
|
+
/** What a coding report keeps of a long run; each is a count of characters or entries, or `Infinity` for no bound. */
|
|
151
|
+
export interface CodingLimits {
|
|
152
|
+
/** Longest worker report, and longest thread report; default 20,000 characters. */
|
|
153
|
+
reportChars?: number;
|
|
154
|
+
/** Held actions listed in the report, the rest counted; default 10. */
|
|
155
|
+
heldEntries?: number;
|
|
156
|
+
/** Longest held-action entry; default 1,000 characters. */
|
|
157
|
+
heldChars?: number;
|
|
158
|
+
}
|
package/src/i18n/zh-tw.ts
CHANGED
|
@@ -13,4 +13,19 @@ export const ZH_TW: Readonly<Record<string, string>> = {
|
|
|
13
13
|
"Give a whole number from 1 to {most}.": "請輸入 1 到 {most} 的整數。",
|
|
14
14
|
"Up to {max} coding workers now run at once.":
|
|
15
15
|
"現在同時最多執行 {max} 個程式工作者。",
|
|
16
|
+
Tasks: "任務",
|
|
17
|
+
Cancel: "取消",
|
|
18
|
+
"Running since {at}, started by {who}": "自 {at} 執行中,由 {who} 啟動",
|
|
19
|
+
"Queued (position {position}) since {at}, started by {who}":
|
|
20
|
+
"自 {at} 排隊中(第 {position} 位),由 {who} 啟動",
|
|
21
|
+
"Stop coding task #{id}? Its commits stay on its branch, not merged.":
|
|
22
|
+
"要停止程式任務 #{id} 嗎?它的提交會留在自己的分支上,不會合併。",
|
|
23
|
+
"Drop coding task #{id} from the queue?": "要把程式任務 #{id} 移出佇列嗎?",
|
|
24
|
+
"Coding task #{id} is no longer running or queued.":
|
|
25
|
+
"程式任務 #{id} 已不在執行或排隊中。",
|
|
26
|
+
"Coding task #{id} was dropped from the queue.":
|
|
27
|
+
"程式任務 #{id} 已移出佇列。",
|
|
28
|
+
"Coding task #{id} was stopped; its commits stay on {branch}, not merged.":
|
|
29
|
+
"程式任務 #{id} 已停止;它的提交留在 {branch},沒有合併。",
|
|
30
|
+
"Coding task #{id} was stopped.": "程式任務 #{id} 已停止。",
|
|
16
31
|
};
|
package/src/index.ts
CHANGED
|
@@ -42,4 +42,6 @@ export {
|
|
|
42
42
|
WORKTREES_DIR,
|
|
43
43
|
} from "./repo-shelf.ts";
|
|
44
44
|
export { REPO_TOOLS } from "./repo-tools.ts";
|
|
45
|
+
export type { CancelResult, CodingTask } from "./task-control.ts";
|
|
46
|
+
export { DEFAULT_TOOL_TEXT } from "./tool-text.ts";
|
|
45
47
|
export { CodingWorkerFailure } from "./worker-failure.ts";
|
package/src/pi-coding-worker.ts
CHANGED
|
@@ -16,6 +16,7 @@ import type {
|
|
|
16
16
|
CodingWorker,
|
|
17
17
|
HeldCallAnswer,
|
|
18
18
|
} from "./coding-desk.ts";
|
|
19
|
+
import { hostStopping } from "./task-control.ts";
|
|
19
20
|
import { CodingWorkerFailure } from "./worker-failure.ts";
|
|
20
21
|
|
|
21
22
|
export interface PiCodingWorkerOptions {
|
|
@@ -49,6 +50,11 @@ export interface PiCodingWorkerOptions {
|
|
|
49
50
|
workerEnv?: WorkerEnv;
|
|
50
51
|
/** What the worker's shell gets besides `SHELL_ENV_NAMES`, as the agents' shell; default the host's `env.shellEnv`. */
|
|
51
52
|
shellEnv?: ShellEnv;
|
|
53
|
+
/**
|
|
54
|
+
* How long a stopped worker gets to save its work and exit after SIGTERM before its process
|
|
55
|
+
* group is killed; default 5,000. 0 kills it at once.
|
|
56
|
+
*/
|
|
57
|
+
stopGraceMs?: number;
|
|
52
58
|
}
|
|
53
59
|
|
|
54
60
|
/** Host variables a coding worker's process gets for its model and Pi runtime, by name. */
|
|
@@ -104,6 +110,7 @@ interface WorkerMessage {
|
|
|
104
110
|
stopReason?: string;
|
|
105
111
|
text?: string;
|
|
106
112
|
name?: string;
|
|
113
|
+
stopped?: boolean;
|
|
107
114
|
}
|
|
108
115
|
function isMessage(value: unknown): value is WorkerMessage {
|
|
109
116
|
return (
|
|
@@ -154,6 +161,7 @@ export class PiCodingWorker implements CodingWorker {
|
|
|
154
161
|
const prompt = this.#options.prompt?.(job.dir);
|
|
155
162
|
let report: string | undefined;
|
|
156
163
|
let failure: string | undefined;
|
|
164
|
+
let windedDown = false;
|
|
157
165
|
const child = Bun.spawn(
|
|
158
166
|
[
|
|
159
167
|
process.execPath,
|
|
@@ -212,6 +220,7 @@ export class PiCodingWorker implements CodingWorker {
|
|
|
212
220
|
value.type === "failure" &&
|
|
213
221
|
typeof value.message === "string"
|
|
214
222
|
) {
|
|
223
|
+
if (value.stopped === true) windedDown = true;
|
|
215
224
|
failure = value.message.slice(
|
|
216
225
|
0,
|
|
217
226
|
this.#options.diagnosticChars === undefined
|
|
@@ -281,11 +290,25 @@ export class PiCodingWorker implements CodingWorker {
|
|
|
281
290
|
/* The group has already exited. */
|
|
282
291
|
}
|
|
283
292
|
};
|
|
284
|
-
|
|
293
|
+
// A host shutdown sends SIGTERM first, so the worker saves what it has; the group is killed when it ends
|
|
294
|
+
// or the grace runs out. A cancel or a timeout kills at once: that work is not saved or merged.
|
|
295
|
+
const stop = () => {
|
|
296
|
+
const grace = this.#options.stopGraceMs ?? 5_000;
|
|
297
|
+
if (grace <= 0 || signal.reason !== hostStopping) return kill();
|
|
298
|
+
try {
|
|
299
|
+
process.kill(child.pid, "SIGTERM");
|
|
300
|
+
} catch {
|
|
301
|
+
/* The worker has already exited. */
|
|
302
|
+
}
|
|
303
|
+
const timer = setTimeout(kill, grace);
|
|
304
|
+
void child.exited.then(() => clearTimeout(timer));
|
|
305
|
+
};
|
|
306
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
285
307
|
try {
|
|
286
|
-
if (signal.aborted)
|
|
308
|
+
if (signal.aborted) stop();
|
|
287
309
|
const code = await child.exited;
|
|
288
310
|
if (signal.aborted) throw new CodingWorkerFailure("stopped");
|
|
311
|
+
if (windedDown) throw new CodingWorkerFailure("stopped");
|
|
289
312
|
if (code !== 0)
|
|
290
313
|
throw new CodingWorkerFailure(
|
|
291
314
|
"exit",
|
|
@@ -296,7 +319,7 @@ export class PiCodingWorker implements CodingWorker {
|
|
|
296
319
|
if (!report?.trim()) throw new CodingWorkerFailure("missing-report");
|
|
297
320
|
return report;
|
|
298
321
|
} finally {
|
|
299
|
-
signal.removeEventListener("abort",
|
|
322
|
+
signal.removeEventListener("abort", stop);
|
|
300
323
|
kill(); // Also remove shell descendants the task left running.
|
|
301
324
|
}
|
|
302
325
|
}
|
package/src/repo-gate.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { AgentError } from "pi-roundtable/kit";
|
|
2
|
+
import type { Integration, RepoShelf, WorkerTree } from "./repo-shelf.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Orders the changes to one clone: integrations of its worker branches run one after another, and
|
|
6
|
+
* never during its report or push.
|
|
7
|
+
*/
|
|
8
|
+
export class RepoGate {
|
|
9
|
+
readonly #shelf: Pick<RepoShelf, "integrate">;
|
|
10
|
+
/** The last integration asked for in each repository, which the next waits for. */
|
|
11
|
+
readonly #integrations = new Map<string, Promise<unknown>>();
|
|
12
|
+
/** Repositories whose clone an integration is changing now. */
|
|
13
|
+
readonly #integrating = new Set<string>();
|
|
14
|
+
/** Reports and pushes in progress, by repository. */
|
|
15
|
+
readonly #shipping = new Map<string, Promise<unknown>>();
|
|
16
|
+
constructor(shelf: Pick<RepoShelf, "integrate">) {
|
|
17
|
+
this.#shelf = shelf;
|
|
18
|
+
}
|
|
19
|
+
/** Refuses while a worker's branch is being merged into the clone; running workers do not count. */
|
|
20
|
+
checkIdle(repo: string): void {
|
|
21
|
+
if (this.#integrating.has(repo))
|
|
22
|
+
throw new AgentError(
|
|
23
|
+
`A coding worker's commits are being merged into ${repo}; try again in a moment.`,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
/** Whether a report or push is in progress for the repository. */
|
|
27
|
+
shipping(repo: string): boolean {
|
|
28
|
+
return this.#shipping.has(repo);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Runs a report or push of the repository, one at a time; refuses during an integration, and
|
|
32
|
+
* an integration that comes due waits for it.
|
|
33
|
+
*/
|
|
34
|
+
async ship<T>(repo: string, operation: () => Promise<T>): Promise<T> {
|
|
35
|
+
this.checkIdle(repo);
|
|
36
|
+
if (this.#shipping.has(repo))
|
|
37
|
+
throw new AgentError(
|
|
38
|
+
"A report or push is already in progress for this repository.",
|
|
39
|
+
);
|
|
40
|
+
const done = operation();
|
|
41
|
+
this.#shipping.set(
|
|
42
|
+
repo,
|
|
43
|
+
done.catch(() => {}),
|
|
44
|
+
);
|
|
45
|
+
try {
|
|
46
|
+
return await done;
|
|
47
|
+
} finally {
|
|
48
|
+
this.#shipping.delete(repo);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** Integrations of one clone run one after another, and never during its report or push. */
|
|
52
|
+
async integrate(tree: WorkerTree): Promise<Integration> {
|
|
53
|
+
const { repo } = tree;
|
|
54
|
+
const previous = this.#integrations.get(repo) ?? Promise.resolve();
|
|
55
|
+
const mine = previous
|
|
56
|
+
.catch(() => {})
|
|
57
|
+
.then(async () => {
|
|
58
|
+
for (let ship = this.#shipping.get(repo); ship; ) {
|
|
59
|
+
await ship;
|
|
60
|
+
ship = this.#shipping.get(repo);
|
|
61
|
+
}
|
|
62
|
+
this.#integrating.add(repo);
|
|
63
|
+
try {
|
|
64
|
+
return await this.#shelf.integrate(tree);
|
|
65
|
+
} finally {
|
|
66
|
+
this.#integrating.delete(repo);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
this.#integrations.set(repo, mine);
|
|
70
|
+
try {
|
|
71
|
+
return await mine;
|
|
72
|
+
} finally {
|
|
73
|
+
if (this.#integrations.get(repo) === mine)
|
|
74
|
+
this.#integrations.delete(repo);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
package/src/repo-tools.ts
CHANGED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { SYSTEM_PRINCIPAL, type ToolTurn } from "pi-roundtable";
|
|
2
|
+
import type { CodingJob } from "./coding-desk.ts";
|
|
3
|
+
|
|
4
|
+
/** A running or queued coding task, as `CodingDesk.tasks` lists it. */
|
|
5
|
+
export interface CodingTask {
|
|
6
|
+
job: CodingJob;
|
|
7
|
+
status: "running" | "queued";
|
|
8
|
+
/** Its place in the queue, from 1, for a queued task. */
|
|
9
|
+
queuePosition?: number;
|
|
10
|
+
}
|
|
11
|
+
/** What `CodingDesk.cancel` did. */
|
|
12
|
+
export type CancelResult =
|
|
13
|
+
/** The task was queued and is dropped; it never ran. */
|
|
14
|
+
| { status: "dropped"; job: CodingJob }
|
|
15
|
+
/**
|
|
16
|
+
* The worker was stopped. Its commits stay on `branch` in the worktree `dir`, unmerged;
|
|
17
|
+
* `settled` is false when the worker had not settled yet as the answer was given.
|
|
18
|
+
*/
|
|
19
|
+
| {
|
|
20
|
+
status: "stopped";
|
|
21
|
+
job: CodingJob;
|
|
22
|
+
branch?: string;
|
|
23
|
+
dir?: string;
|
|
24
|
+
commits: string[];
|
|
25
|
+
settled: boolean;
|
|
26
|
+
}
|
|
27
|
+
/** No running or queued task has that id. */
|
|
28
|
+
| { status: "refused"; reason: string };
|
|
29
|
+
|
|
30
|
+
/** What a cancelled run's report says in place of the worker's own. */
|
|
31
|
+
export function cancelledText(by: string): string {
|
|
32
|
+
return `cancelled by ${by}; the worker was stopped and its commits were not merged into the clone`;
|
|
33
|
+
}
|
|
34
|
+
export function droppedText(by: string): string {
|
|
35
|
+
return `cancelled by ${by} before it ran; nothing ran, so start it again if it is still wanted`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Whether the turn may see and cancel the job: an owner speaking sees all, an agent its own. */
|
|
39
|
+
export function mayControl(job: CodingJob, turn: ToolTurn): boolean {
|
|
40
|
+
const { speaker } = turn;
|
|
41
|
+
if (speaker?.tier === "owner" && speaker.principalId !== SYSTEM_PRINCIPAL)
|
|
42
|
+
return true;
|
|
43
|
+
if (turn.agent) return job.agent === turn.agent.name;
|
|
44
|
+
// Outside an agent's turn, the person who started it, never the host's own turns.
|
|
45
|
+
return (
|
|
46
|
+
job.agent === undefined &&
|
|
47
|
+
speaker !== undefined &&
|
|
48
|
+
speaker.principalId !== SYSTEM_PRINCIPAL &&
|
|
49
|
+
job.starter?.speaker.principalId === speaker.principalId
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Who a turn is, for "cancelled by …". */
|
|
54
|
+
export function callerName(turn: ToolTurn): string {
|
|
55
|
+
return (
|
|
56
|
+
turn.speaker?.name ??
|
|
57
|
+
turn.agent?.name ??
|
|
58
|
+
turn.speaker?.principalId ??
|
|
59
|
+
"the host"
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** One line for a task in a list. */
|
|
64
|
+
export function taskLine(task: CodingTask): string {
|
|
65
|
+
const { job } = task;
|
|
66
|
+
const who = job.agent ?? job.starter?.speaker.name ?? "the owner";
|
|
67
|
+
const when =
|
|
68
|
+
task.status === "running"
|
|
69
|
+
? `running since ${job.startedAt.toISOString()}`
|
|
70
|
+
: `queued (position ${task.queuePosition}) since ${(job.queuedAt ?? job.startedAt).toISOString()}`;
|
|
71
|
+
const first = job.task.split("\n", 1)[0]?.slice(0, 200) ?? "";
|
|
72
|
+
return `#${job.id} ${job.repo}, ${when}, started by ${who}: ${first}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The promise's value, or undefined when it takes longer than `ms`. */
|
|
76
|
+
export async function within<T>(
|
|
77
|
+
promise: Promise<T>,
|
|
78
|
+
ms: number,
|
|
79
|
+
): Promise<T | undefined> {
|
|
80
|
+
let timer: Timer | undefined;
|
|
81
|
+
try {
|
|
82
|
+
return await Promise.race([
|
|
83
|
+
promise,
|
|
84
|
+
new Promise<undefined>((resolve) => {
|
|
85
|
+
timer = setTimeout(resolve, ms);
|
|
86
|
+
}),
|
|
87
|
+
]);
|
|
88
|
+
} finally {
|
|
89
|
+
clearTimeout(timer);
|
|
90
|
+
}
|
|
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
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineTool,
|
|
3
|
+
type ToolContribution,
|
|
4
|
+
type ToolTurn,
|
|
5
|
+
} from "pi-roundtable";
|
|
6
|
+
import { AgentError } from "pi-roundtable/kit";
|
|
7
|
+
import { type TString, Type } from "typebox";
|
|
8
|
+
import type { CodingDesk } from "./coding-desk.ts";
|
|
9
|
+
import type { RepoToolName } from "./coding-options.ts";
|
|
10
|
+
import { callerName, mayControl, taskLine } from "./task-control.ts";
|
|
11
|
+
|
|
12
|
+
/** The repo_task_list answer: the running and queued tasks the turn may see. */
|
|
13
|
+
export function listTasks(
|
|
14
|
+
desk: Pick<CodingDesk, "tasks">,
|
|
15
|
+
turn: ToolTurn,
|
|
16
|
+
repo?: string,
|
|
17
|
+
): string {
|
|
18
|
+
const lines = desk
|
|
19
|
+
.tasks()
|
|
20
|
+
.filter(
|
|
21
|
+
({ job }) =>
|
|
22
|
+
(repo === undefined || job.repo === repo) && mayControl(job, turn),
|
|
23
|
+
)
|
|
24
|
+
.map(taskLine);
|
|
25
|
+
return lines.length
|
|
26
|
+
? lines.join("\n")
|
|
27
|
+
: "No running or queued coding tasks you may see.";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The repo_task_cancel answer; a task the turn may not see is refused as if unknown. */
|
|
31
|
+
export async function cancelTask(
|
|
32
|
+
desk: Pick<CodingDesk, "tasks" | "cancel">,
|
|
33
|
+
turn: ToolTurn,
|
|
34
|
+
input: { repo: string; id: number; reason?: string },
|
|
35
|
+
): Promise<string> {
|
|
36
|
+
const task = desk
|
|
37
|
+
.tasks()
|
|
38
|
+
.find(({ job }) => job.id === input.id && job.repo === input.repo);
|
|
39
|
+
if (!task)
|
|
40
|
+
throw new AgentError(
|
|
41
|
+
`No running or queued coding task #${input.id} in ${input.repo}; it may have finished already. repo_task_list shows the tasks.`,
|
|
42
|
+
);
|
|
43
|
+
if (!mayControl(task.job, turn))
|
|
44
|
+
throw new AgentError(
|
|
45
|
+
`Coding task #${input.id} was started by someone else; only they or an owner may cancel it.`,
|
|
46
|
+
);
|
|
47
|
+
const reason = input.reason?.trim();
|
|
48
|
+
const by = `${callerName(turn)}${reason ? ` (${reason.slice(0, 200)})` : ""}`;
|
|
49
|
+
const result = await desk.cancel(input.id, by);
|
|
50
|
+
switch (result.status) {
|
|
51
|
+
case "refused":
|
|
52
|
+
throw new AgentError(result.reason);
|
|
53
|
+
case "dropped":
|
|
54
|
+
return `Dropped coding task #${input.id} in ${input.repo} from the queue; it never ran.`;
|
|
55
|
+
case "stopped": {
|
|
56
|
+
const kept = result.branch
|
|
57
|
+
? ` Its commits (${result.commits.length}) are kept, not merged, on the branch ${result.branch}${result.dir ? ` in the worktree ${result.dir}` : ""}; inspect, merge or delete them.`
|
|
58
|
+
: " It had not made its worktree yet.";
|
|
59
|
+
const pending = result.settled
|
|
60
|
+
? ""
|
|
61
|
+
: " The worker is still stopping; its report follows.";
|
|
62
|
+
return `Stopped coding task #${input.id} in ${input.repo}.${kept}${pending}`;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The repo_task_list and repo_task_cancel tools, worded like the other repository tools. */
|
|
68
|
+
export function taskTools(
|
|
69
|
+
desk: CodingDesk,
|
|
70
|
+
refusal: <T>(operation: () => Promise<T>) => Promise<T>,
|
|
71
|
+
describe: (tool: RepoToolName) => string,
|
|
72
|
+
about: (tool: RepoToolName, name: string) => { description?: string },
|
|
73
|
+
repoSchema: (tool: RepoToolName) => TString,
|
|
74
|
+
): ToolContribution[] {
|
|
75
|
+
return [
|
|
76
|
+
defineTool({
|
|
77
|
+
name: "repo_task_list",
|
|
78
|
+
minTier: "owner",
|
|
79
|
+
description: describe("repo_task_list"),
|
|
80
|
+
parameters: Type.Object({
|
|
81
|
+
repo: Type.Optional(Type.String(about("repo_task_list", "repo"))),
|
|
82
|
+
}),
|
|
83
|
+
run: ({ repo }, turn) => refusal(async () => listTasks(desk, turn, repo)),
|
|
84
|
+
}),
|
|
85
|
+
defineTool({
|
|
86
|
+
name: "repo_task_cancel",
|
|
87
|
+
minTier: "owner",
|
|
88
|
+
description: describe("repo_task_cancel"),
|
|
89
|
+
parameters: Type.Object({
|
|
90
|
+
repo: repoSchema("repo_task_cancel"),
|
|
91
|
+
id: Type.Integer({
|
|
92
|
+
minimum: 1,
|
|
93
|
+
...about("repo_task_cancel", "id"),
|
|
94
|
+
}),
|
|
95
|
+
reason: Type.Optional(
|
|
96
|
+
Type.String({
|
|
97
|
+
maxLength: 200,
|
|
98
|
+
...about("repo_task_cancel", "reason"),
|
|
99
|
+
}),
|
|
100
|
+
),
|
|
101
|
+
}),
|
|
102
|
+
run: (input, turn) => refusal(() => cancelTask(desk, turn, input)),
|
|
103
|
+
}),
|
|
104
|
+
];
|
|
105
|
+
}
|
package/src/tool-text.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { CodingToolText, RepoToolName } from "./coding-options.ts";
|
|
2
|
+
|
|
3
|
+
const REPO = "A managed repository, owner/repo.";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The wording of each repository tool as the package ships it: the description, and the argument
|
|
7
|
+
* descriptions by argument name (an argument absent here has none). A host words only what must
|
|
8
|
+
* differ through `toolText`, such as
|
|
9
|
+
* `{ repo_task: { description: `${DEFAULT_TOOL_TEXT.repo_task.description} Extra.` } }`, and
|
|
10
|
+
* compares against this to see what a new package version changed.
|
|
11
|
+
*/
|
|
12
|
+
export const DEFAULT_TOOL_TEXT: Readonly<
|
|
13
|
+
Record<RepoToolName, Required<CodingToolText>>
|
|
14
|
+
> = {
|
|
15
|
+
repo_list: {
|
|
16
|
+
description:
|
|
17
|
+
"List managed clones, branch, upstream counts, dirty files, last commit, summary, CI hints and linked skills. Fetch first only when requested.",
|
|
18
|
+
parameters: {},
|
|
19
|
+
},
|
|
20
|
+
repo_add: {
|
|
21
|
+
description:
|
|
22
|
+
"Clone owner/repo into the shelf using the host's Git login (GitHub CLI by default).",
|
|
23
|
+
parameters: { repo: REPO },
|
|
24
|
+
},
|
|
25
|
+
repo_change_report: {
|
|
26
|
+
description:
|
|
27
|
+
"Fetch and report commits and changed files for the default branch. Requires a clean, ahead-only clone. Review checks before requesting this report.",
|
|
28
|
+
parameters: { repo: REPO },
|
|
29
|
+
},
|
|
30
|
+
repo_push: {
|
|
31
|
+
description:
|
|
32
|
+
"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.",
|
|
33
|
+
parameters: { repo: REPO },
|
|
34
|
+
},
|
|
35
|
+
repo_task: {
|
|
36
|
+
description:
|
|
37
|
+
"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.",
|
|
38
|
+
parameters: { repo: REPO },
|
|
39
|
+
},
|
|
40
|
+
repo_task_list: {
|
|
41
|
+
description:
|
|
42
|
+
"List running and queued coding tasks with id, repository, who started each, when, and the first line of its task. An agent sees the tasks it started; an owner sees all.",
|
|
43
|
+
parameters: { repo: "Only this repository, owner/repo." },
|
|
44
|
+
},
|
|
45
|
+
repo_task_cancel: {
|
|
46
|
+
description:
|
|
47
|
+
"Cancel a coding task by id. A queued task is dropped and never runs; a running worker is stopped, and its commits stay on its branch and worktree, not merged into the clone. An agent may cancel the tasks it started; an owner may cancel any.",
|
|
48
|
+
parameters: {
|
|
49
|
+
repo: REPO,
|
|
50
|
+
id: "The task's number, as #N.",
|
|
51
|
+
reason: "Why, for its report.",
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
};
|