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
package/src/coding-options.ts
CHANGED
|
@@ -34,7 +34,9 @@ export type RepoToolName =
|
|
|
34
34
|
| "repo_add"
|
|
35
35
|
| "repo_change_report"
|
|
36
36
|
| "repo_push"
|
|
37
|
-
| "repo_task"
|
|
37
|
+
| "repo_task"
|
|
38
|
+
| "repo_task_list"
|
|
39
|
+
| "repo_task_cancel";
|
|
38
40
|
/** Trusted wording of one repository tool, as the model reads it; unset fields keep the defaults. */
|
|
39
41
|
export interface CodingToolText {
|
|
40
42
|
description?: string;
|
|
@@ -42,27 +44,44 @@ export interface CodingToolText {
|
|
|
42
44
|
parameters?: Record<string, string>;
|
|
43
45
|
}
|
|
44
46
|
export interface CodingPresentation {
|
|
45
|
-
/**
|
|
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
|
+
*/
|
|
46
51
|
list?(
|
|
47
52
|
repos: (RepoSummary & { skills: string[] })[],
|
|
48
53
|
context: { shelfDir: string; fetched: boolean },
|
|
54
|
+
defaultText: string,
|
|
49
55
|
): string;
|
|
50
|
-
/**
|
|
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
|
+
*/
|
|
51
60
|
changeReport?(
|
|
52
61
|
report: ChangeReport,
|
|
53
62
|
direct: boolean,
|
|
63
|
+
defaults: { post: string; result: string },
|
|
54
64
|
): { post: string; result: string };
|
|
55
|
-
|
|
56
|
-
|
|
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;
|
|
57
71
|
/**
|
|
58
72
|
* Wording of the Settings page, keyed by its English text, such as
|
|
59
73
|
* `{ "Coding workers": "..." }`; it wins over the built-in zh-TW wording, key by key.
|
|
60
74
|
*/
|
|
61
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
|
+
*/
|
|
62
80
|
taskQueued?(
|
|
63
81
|
job: CodingJob,
|
|
64
82
|
position: number,
|
|
65
83
|
skippedSkills: string[],
|
|
84
|
+
defaultText: string,
|
|
66
85
|
): string;
|
|
67
86
|
}
|
|
68
87
|
export interface CodingOptions {
|
package/src/coding-plugin.ts
CHANGED
|
@@ -30,9 +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";
|
|
37
|
+
import { taskTools } from "./task-tools.ts";
|
|
38
|
+
import { DEFAULT_TOOL_TEXT } from "./tool-text.ts";
|
|
36
39
|
import { workerHolds } from "./worker-holds.ts";
|
|
37
40
|
|
|
38
41
|
export type {
|
|
@@ -125,6 +128,18 @@ export function coding(options: CodingOptions) {
|
|
|
125
128
|
diagnosticChars: options.diagnosticChars,
|
|
126
129
|
holds: workerHolds(options.holds, () => context.sessions().holds),
|
|
127
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
|
+
});
|
|
128
143
|
const desk = new CodingDesk({
|
|
129
144
|
shelf,
|
|
130
145
|
worker,
|
|
@@ -146,18 +161,13 @@ export function coding(options: CodingOptions) {
|
|
|
146
161
|
? { interimPrimaryChars: options.interimPrimaryChars }
|
|
147
162
|
: {}),
|
|
148
163
|
prompts: (channel) => context.surfaces.prompts(channel),
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
zonedStamp(result.job.startedAt, context.env.timeZone),
|
|
157
|
-
),
|
|
158
|
-
],
|
|
159
|
-
});
|
|
160
|
-
}),
|
|
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
|
+
},
|
|
161
171
|
});
|
|
162
172
|
const ship = <T>(repo: string, operation: () => Promise<T>) =>
|
|
163
173
|
desk.ship(repo, operation);
|
|
@@ -189,22 +199,29 @@ export function coding(options: CodingOptions) {
|
|
|
189
199
|
}
|
|
190
200
|
};
|
|
191
201
|
const textOf = (tool: RepoToolName) => options.toolText?.[tool];
|
|
192
|
-
const describe = (tool: RepoToolName
|
|
193
|
-
textOf(tool)?.description ??
|
|
194
|
-
const about = (tool: RepoToolName, name: string
|
|
195
|
-
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];
|
|
196
208
|
return description ? { description } : {};
|
|
197
209
|
};
|
|
198
210
|
const repoSchema = (tool: RepoToolName) =>
|
|
199
|
-
Type.String(about(tool, "repo"
|
|
211
|
+
Type.String(about(tool, "repo"));
|
|
200
212
|
return {
|
|
201
213
|
services: [
|
|
202
214
|
{
|
|
203
215
|
name: "coding-desk",
|
|
204
216
|
// The stored limit is read at start: setup must not query the database.
|
|
205
217
|
start: () => applyStoredLimit(store, desk, context.logger),
|
|
206
|
-
// The
|
|
207
|
-
|
|
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(),
|
|
208
225
|
stop: () => desk.stop(),
|
|
209
226
|
},
|
|
210
227
|
],
|
|
@@ -264,10 +281,7 @@ export function coding(options: CodingOptions) {
|
|
|
264
281
|
defineTool({
|
|
265
282
|
name: "repo_list",
|
|
266
283
|
minTier: "owner",
|
|
267
|
-
description: describe(
|
|
268
|
-
"repo_list",
|
|
269
|
-
"List managed clones, branch, upstream counts, dirty files, last commit, summary, CI hints and linked skills. Fetch first only when requested.",
|
|
270
|
-
),
|
|
284
|
+
description: describe("repo_list"),
|
|
271
285
|
parameters: Type.Object({
|
|
272
286
|
fetch: Type.Optional(Type.Boolean(about("repo_list", "fetch"))),
|
|
273
287
|
}),
|
|
@@ -279,23 +293,22 @@ export function coding(options: CodingOptions) {
|
|
|
279
293
|
skills: skills?.linkedFrom(repo.repo) ?? [],
|
|
280
294
|
}),
|
|
281
295
|
);
|
|
282
|
-
|
|
283
|
-
return options.presentation.list(repos, {
|
|
284
|
-
shelfDir: shelf.dir,
|
|
285
|
-
fetched: fetch === true,
|
|
286
|
-
});
|
|
287
|
-
return repos.length
|
|
296
|
+
const text = repos.length
|
|
288
297
|
? JSON.stringify(repos, null, 2)
|
|
289
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
|
+
);
|
|
290
306
|
}),
|
|
291
307
|
}),
|
|
292
308
|
defineTool({
|
|
293
309
|
name: "repo_add",
|
|
294
310
|
minTier: "owner",
|
|
295
|
-
description: describe(
|
|
296
|
-
"repo_add",
|
|
297
|
-
"Clone owner/repo into the shelf using the host's Git login (GitHub CLI by default).",
|
|
298
|
-
),
|
|
311
|
+
description: describe("repo_add"),
|
|
299
312
|
parameters: Type.Object({ repo: repoSchema("repo_add") }),
|
|
300
313
|
run: ({ repo }) =>
|
|
301
314
|
refusal(
|
|
@@ -305,10 +318,7 @@ export function coding(options: CodingOptions) {
|
|
|
305
318
|
defineTool({
|
|
306
319
|
name: "repo_change_report",
|
|
307
320
|
minTier: "owner",
|
|
308
|
-
description: describe(
|
|
309
|
-
"repo_change_report",
|
|
310
|
-
"Fetch and report commits and changed files for the default branch. Requires a clean, ahead-only clone. Review checks before requesting this report.",
|
|
311
|
-
),
|
|
321
|
+
description: describe("repo_change_report"),
|
|
312
322
|
parameters: Type.Object({
|
|
313
323
|
repo: repoSchema("repo_change_report"),
|
|
314
324
|
}),
|
|
@@ -317,13 +327,16 @@ export function coding(options: CodingOptions) {
|
|
|
317
327
|
ship(repo, async () => {
|
|
318
328
|
const report = await shelf.report(repo);
|
|
319
329
|
const direct = ownerOwned(repo);
|
|
320
|
-
const
|
|
321
|
-
report,
|
|
322
|
-
direct,
|
|
323
|
-
) ?? {
|
|
330
|
+
const standard = {
|
|
324
331
|
post: reportPost(report, direct),
|
|
325
332
|
result: reportPost(report, direct),
|
|
326
333
|
};
|
|
334
|
+
const text =
|
|
335
|
+
options.presentation?.changeReport?.(
|
|
336
|
+
report,
|
|
337
|
+
direct,
|
|
338
|
+
standard,
|
|
339
|
+
) ?? standard;
|
|
327
340
|
if (options.postChangeReport)
|
|
328
341
|
await options.postChangeReport(turn, text.post);
|
|
329
342
|
else
|
|
@@ -337,10 +350,7 @@ export function coding(options: CodingOptions) {
|
|
|
337
350
|
defineTool({
|
|
338
351
|
name: "repo_push",
|
|
339
352
|
minTier: "owner",
|
|
340
|
-
description: describe(
|
|
341
|
-
"repo_push",
|
|
342
|
-
"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.",
|
|
343
|
-
),
|
|
353
|
+
description: describe("repo_push"),
|
|
344
354
|
parameters: Type.Object({
|
|
345
355
|
repo: repoSchema("repo_push"),
|
|
346
356
|
sha: Type.String({
|
|
@@ -365,10 +375,7 @@ export function coding(options: CodingOptions) {
|
|
|
365
375
|
defineTool({
|
|
366
376
|
name: "repo_task",
|
|
367
377
|
minTier: "owner",
|
|
368
|
-
description: describe(
|
|
369
|
-
"repo_task",
|
|
370
|
-
"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.",
|
|
371
|
-
),
|
|
378
|
+
description: describe("repo_task"),
|
|
372
379
|
parameters: Type.Object({
|
|
373
380
|
repo: repoSchema("repo_task"),
|
|
374
381
|
task: Type.String({
|
|
@@ -419,6 +426,7 @@ export function coding(options: CodingOptions) {
|
|
|
419
426
|
const { speaker } = turn;
|
|
420
427
|
const job = await desk.start({
|
|
421
428
|
...run,
|
|
429
|
+
...(turn.agent ? { agent: turn.agent.name } : {}),
|
|
422
430
|
// Its modes follow the conversation this turn runs in, as the turn's own would.
|
|
423
431
|
...(speaker
|
|
424
432
|
? {
|
|
@@ -442,21 +450,25 @@ export function coding(options: CodingOptions) {
|
|
|
442
450
|
const skippedText = skipped.length
|
|
443
451
|
? ` Unavailable implicit skills skipped: ${skipped.join(", ")}.`
|
|
444
452
|
: "";
|
|
445
|
-
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}`;
|
|
446
455
|
return (
|
|
447
456
|
options.presentation?.taskQueued?.(
|
|
448
457
|
job,
|
|
449
458
|
job.queuePosition,
|
|
450
459
|
skipped,
|
|
451
|
-
|
|
452
|
-
|
|
460
|
+
queued,
|
|
461
|
+
) ?? queued
|
|
453
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}`;
|
|
454
465
|
return (
|
|
455
|
-
options.presentation?.taskStarted?.(job, skipped) ??
|
|
456
|
-
|
|
466
|
+
options.presentation?.taskStarted?.(job, skipped, started) ??
|
|
467
|
+
started
|
|
457
468
|
);
|
|
458
469
|
}),
|
|
459
470
|
}),
|
|
471
|
+
...taskTools(desk, refusal, describe, about, repoSchema),
|
|
460
472
|
],
|
|
461
473
|
};
|
|
462
474
|
},
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { CodingResult } from "./coding-desk.ts";
|
|
2
|
+
import type { Integration } from "./repo-shelf.ts";
|
|
3
|
+
|
|
4
|
+
/** What became of the worker's branch, for the report. */
|
|
5
|
+
export function integrationText(
|
|
6
|
+
integration: Integration | undefined,
|
|
7
|
+
branch: string | undefined,
|
|
8
|
+
): string | undefined {
|
|
9
|
+
switch (integration?.status) {
|
|
10
|
+
case "merged":
|
|
11
|
+
return `Merged ${integration.branch}: rebased onto the clone's branch and fast-forwarded it.`;
|
|
12
|
+
case "nothing":
|
|
13
|
+
return `${branch ?? "The worker's branch"} added no commits; its worktree is removed.`;
|
|
14
|
+
case "conflict":
|
|
15
|
+
return `Rebasing ${integration.branch} onto the clone's branch conflicted in:\n${integration.files.join("\n") || "(files not listed)"}\nThe rebase was aborted and the clone is unchanged; the branch ${integration.branch} and its worktree ${integration.dir} are kept to resolve.`;
|
|
16
|
+
case "refused":
|
|
17
|
+
return `Not merged: ${integration.reason}\nThe branch ${integration.branch} and its worktree ${integration.dir} are kept.`;
|
|
18
|
+
default:
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function codingReport(result: CodingResult, started: string): string {
|
|
24
|
+
const { job, outcome, state, commits, held, autoApproved, integration } =
|
|
25
|
+
result;
|
|
26
|
+
const merge = integrationText(integration, job.branch);
|
|
27
|
+
return [
|
|
28
|
+
`## Coding task #${job.id}: ${job.repo}`,
|
|
29
|
+
`Started: ${started}`,
|
|
30
|
+
outcome.ok ? outcome.report : outcome.error,
|
|
31
|
+
"### Held",
|
|
32
|
+
held.length ? held.join("\n") : "None.",
|
|
33
|
+
...(autoApproved?.length
|
|
34
|
+
? ["### Approved by yolo mode", autoApproved.join("\n")]
|
|
35
|
+
: []),
|
|
36
|
+
"### Repository",
|
|
37
|
+
[
|
|
38
|
+
...(merge ? [merge] : []),
|
|
39
|
+
state
|
|
40
|
+
? `Branch ${state.branch}, HEAD ${state.head} (was ${job.startHead}).\nNew commits:\n${commits.join("\n") || "None."}\nUncommitted:\n${state.uncommitted.join("\n") || "None."}`
|
|
41
|
+
: "Unreadable.",
|
|
42
|
+
].join("\n"),
|
|
43
|
+
"Review the changes and checks, then use repo_change_report before repo_push.",
|
|
44
|
+
].join("\n\n");
|
|
45
|
+
}
|
package/src/coding-settings.ts
CHANGED
|
@@ -77,6 +77,8 @@ export interface SettingsWebApp {
|
|
|
77
77
|
}
|
|
78
78
|
interface SettingsViewer {
|
|
79
79
|
tier: Tier;
|
|
80
|
+
/** The name the app shows for them, for a cancelled task's report. */
|
|
81
|
+
name?: string;
|
|
80
82
|
}
|
|
81
83
|
type SettingsResult =
|
|
82
84
|
| { ok: true; notice?: string }
|
|
@@ -90,6 +92,20 @@ export interface SettingsPage {
|
|
|
90
92
|
title: string;
|
|
91
93
|
sections: readonly (
|
|
92
94
|
| { kind: "markdown"; text: string }
|
|
95
|
+
| {
|
|
96
|
+
kind: "list";
|
|
97
|
+
title?: string;
|
|
98
|
+
rows: {
|
|
99
|
+
title: string;
|
|
100
|
+
subtitle?: string;
|
|
101
|
+
actions?: {
|
|
102
|
+
id: string;
|
|
103
|
+
label: string;
|
|
104
|
+
style?: "danger";
|
|
105
|
+
confirm?: string;
|
|
106
|
+
}[];
|
|
107
|
+
}[];
|
|
108
|
+
}
|
|
93
109
|
| {
|
|
94
110
|
kind: "form";
|
|
95
111
|
action: string;
|
|
@@ -123,7 +139,7 @@ export const SETTINGS_PAGE_ID = "coding-workers";
|
|
|
123
139
|
export function settingsWording(
|
|
124
140
|
locale: Locale,
|
|
125
141
|
messages: Readonly<Record<string, string>> = {},
|
|
126
|
-
): (text: string, values?: Record<string, number>) => string {
|
|
142
|
+
): (text: string, values?: Record<string, number | string>) => string {
|
|
127
143
|
const catalog = { ...(locale === "zh-TW" ? ZH_TW : {}), ...messages };
|
|
128
144
|
return (text, values = {}) =>
|
|
129
145
|
(catalog[text] ?? text).replace(/\{(\w+)\}/g, (whole, name: string) =>
|
|
@@ -156,6 +172,7 @@ export function settingsPage(
|
|
|
156
172
|
{ max, running, queued },
|
|
157
173
|
),
|
|
158
174
|
},
|
|
175
|
+
...taskList(desk, t),
|
|
159
176
|
{
|
|
160
177
|
kind: "form",
|
|
161
178
|
action: "save",
|
|
@@ -184,6 +201,34 @@ export function settingsPage(
|
|
|
184
201
|
act: async (viewer, action, input) => {
|
|
185
202
|
if (!owner(viewer))
|
|
186
203
|
return { ok: false, error: t("Only an owner changes this.") };
|
|
204
|
+
const cancel = /^cancel:(\d{1,9})$/.exec(action);
|
|
205
|
+
if (cancel) {
|
|
206
|
+
const id = Number(cancel[1]);
|
|
207
|
+
const result = await desk.cancel(
|
|
208
|
+
id,
|
|
209
|
+
viewer.name ? `${viewer.name} (web app)` : "an owner (web app)",
|
|
210
|
+
);
|
|
211
|
+
changed();
|
|
212
|
+
if (result.status === "refused")
|
|
213
|
+
return {
|
|
214
|
+
ok: false,
|
|
215
|
+
error: t("Coding task #{id} is no longer running or queued.", {
|
|
216
|
+
id,
|
|
217
|
+
}),
|
|
218
|
+
};
|
|
219
|
+
return {
|
|
220
|
+
ok: true,
|
|
221
|
+
notice:
|
|
222
|
+
result.status === "dropped"
|
|
223
|
+
? t("Coding task #{id} was dropped from the queue.", { id })
|
|
224
|
+
: result.branch
|
|
225
|
+
? t(
|
|
226
|
+
"Coding task #{id} was stopped; its commits stay on {branch}, not merged.",
|
|
227
|
+
{ id, branch: result.branch },
|
|
228
|
+
)
|
|
229
|
+
: t("Coding task #{id} was stopped.", { id }),
|
|
230
|
+
};
|
|
231
|
+
}
|
|
187
232
|
if (action !== "save")
|
|
188
233
|
return { ok: false, error: t("This page has no such action.") };
|
|
189
234
|
const text = input.maxWorkers;
|
|
@@ -213,6 +258,67 @@ export function settingsPage(
|
|
|
213
258
|
};
|
|
214
259
|
}
|
|
215
260
|
|
|
261
|
+
/** The running and queued tasks, each with a Cancel action; none when nothing runs or waits. */
|
|
262
|
+
function taskList(
|
|
263
|
+
desk: CodingDesk,
|
|
264
|
+
t: ReturnType<typeof settingsWording>,
|
|
265
|
+
): {
|
|
266
|
+
kind: "list";
|
|
267
|
+
title: string;
|
|
268
|
+
rows: {
|
|
269
|
+
title: string;
|
|
270
|
+
subtitle: string;
|
|
271
|
+
actions: {
|
|
272
|
+
id: string;
|
|
273
|
+
label: string;
|
|
274
|
+
style: "danger";
|
|
275
|
+
confirm: string;
|
|
276
|
+
}[];
|
|
277
|
+
}[];
|
|
278
|
+
}[] {
|
|
279
|
+
const tasks = desk.tasks().slice(0, 100);
|
|
280
|
+
if (!tasks.length) return [];
|
|
281
|
+
return [
|
|
282
|
+
{
|
|
283
|
+
kind: "list",
|
|
284
|
+
title: t("Tasks"),
|
|
285
|
+
rows: tasks.map((task) => {
|
|
286
|
+
const { job } = task;
|
|
287
|
+
const id = job.id;
|
|
288
|
+
const who = job.agent ?? job.starter?.speaker.name ?? "";
|
|
289
|
+
return {
|
|
290
|
+
title: `#${id} ${job.repo}: ${job.task.split("\n", 1)[0] ?? ""}`,
|
|
291
|
+
subtitle:
|
|
292
|
+
task.status === "running"
|
|
293
|
+
? t("Running since {at}, started by {who}", {
|
|
294
|
+
at: job.startedAt.toISOString(),
|
|
295
|
+
who,
|
|
296
|
+
})
|
|
297
|
+
: t("Queued (position {position}) since {at}, started by {who}", {
|
|
298
|
+
position: task.queuePosition ?? 0,
|
|
299
|
+
at: (job.queuedAt ?? job.startedAt).toISOString(),
|
|
300
|
+
who,
|
|
301
|
+
}),
|
|
302
|
+
actions: [
|
|
303
|
+
{
|
|
304
|
+
id: `cancel:${id}`,
|
|
305
|
+
label: t("Cancel"),
|
|
306
|
+
style: "danger",
|
|
307
|
+
confirm:
|
|
308
|
+
task.status === "running"
|
|
309
|
+
? t(
|
|
310
|
+
"Stop coding task #{id}? Its commits stay on its branch, not merged.",
|
|
311
|
+
{ id },
|
|
312
|
+
)
|
|
313
|
+
: t("Drop coding task #{id} from the queue?", { id }),
|
|
314
|
+
},
|
|
315
|
+
],
|
|
316
|
+
};
|
|
317
|
+
}),
|
|
318
|
+
},
|
|
319
|
+
];
|
|
320
|
+
}
|
|
321
|
+
|
|
216
322
|
/** Registers the settings page when the web app is there; the web app must be registered first. */
|
|
217
323
|
export function registerSettingsPage(
|
|
218
324
|
context: Pick<PluginContext, "services" | "logger" | "env">,
|
|
@@ -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
|
+
}
|