pi-roundtable 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 +53 -0
- package/README.md +6 -4
- package/README.zh-TW.md +6 -4
- package/docs/plugins.md +32 -5
- package/package.json +2 -2
- package/src/core/agents/agent-claim.ts +6 -2
- package/src/core/agents/agent-team-fixture.ts +9 -3
- package/src/core/agents/agent-team.ts +7 -8
- package/src/core/agents/agent-tools.ts +12 -4
- package/src/core/agents/routes.ts +24 -7
- package/src/core/agents/team-options.ts +2 -0
- package/src/core/builtin/agent-server-options.ts +101 -0
- package/src/core/builtin/agent-server.ts +34 -100
- package/src/core/config/adapters.ts +13 -7
- package/src/core/config/agent-team.ts +45 -4
- package/src/core/config/config-schema.ts +3 -1
- package/src/core/config/config.ts +23 -23
- package/src/core/config/ops.ts +1 -1
- package/src/core/config/routing.ts +3 -3
- package/src/core/config/team.ts +30 -0
- package/src/core/contract/channels.ts +8 -0
- package/src/core/db/team-move.ts +103 -0
- package/src/core/host/define-roundtable.ts +15 -5
- package/src/core/host/drain.ts +46 -0
- package/src/core/host/host-registration.ts +5 -0
- package/src/core/host/host.ts +31 -8
- package/src/core/plugin-api/holds.ts +31 -0
- package/src/core/plugin-api/plugin.ts +6 -3
- package/src/core/plugin-api/services.ts +4 -5
- package/src/core/presentation/diagram-text.ts +17 -8
- package/src/core/routing/channel-queue.ts +36 -3
- package/src/core/runtime/extensions/yolo.ts +3 -0
- package/src/core/runtime/mcp.ts +10 -0
- package/src/core/testing/partial.ts +8 -0
- package/src/kit/holds.ts +1 -0
- package/src/kit/index.ts +6 -1
- package/src/kit/worker.ts +1 -1
- package/src/testing.ts +4 -4
|
@@ -173,10 +173,15 @@ function discordAssembly(
|
|
|
173
173
|
};
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
/** The id the team's rows are stored under: the configured one, else the agent guild's, else `local`. */
|
|
177
|
+
function teamIdOf(config: ResolvedConfig): string {
|
|
178
|
+
return config.team.id ?? config.discord?.guild ?? LOCAL_TEAM_ID;
|
|
179
|
+
}
|
|
180
|
+
|
|
176
181
|
/**
|
|
177
182
|
* The agent server and what only it uses, each in its place in the plugin list: the skills addon
|
|
178
|
-
* and the seed agents. Its team lives in the Discord guild when there is one,
|
|
179
|
-
* the rooms of the surfaces that host them.
|
|
183
|
+
* and the seed agents. Its team lives in the Discord guild `discord.guild` names when there is one,
|
|
184
|
+
* and otherwise in the rooms of the surfaces that host them.
|
|
180
185
|
*/
|
|
181
186
|
interface AgentAssembly {
|
|
182
187
|
skills: RoundtablePlugin[];
|
|
@@ -195,6 +200,7 @@ function agentAssembly(
|
|
|
195
200
|
): AgentAssembly {
|
|
196
201
|
const { name, discord } = config;
|
|
197
202
|
const owner = discordOwnerOf(config);
|
|
203
|
+
const teamId = teamIdOf(config);
|
|
198
204
|
const sharedPrompt = config.prompts.shared
|
|
199
205
|
? readPrompt(config.prompts.shared, "prompts.shared")
|
|
200
206
|
: readPrompt(join(ASSETS, "prompts", "shared.md"), "prompts.shared");
|
|
@@ -206,7 +212,7 @@ function agentAssembly(
|
|
|
206
212
|
skills: config.skills
|
|
207
213
|
? [
|
|
208
214
|
skillsPlugin({
|
|
209
|
-
guildId:
|
|
215
|
+
guildId: teamId,
|
|
210
216
|
reposDir: config.skills.reposDir ?? join(config.dataDir, "repos"),
|
|
211
217
|
writtenDir: join(config.dataDir, "skills"),
|
|
212
218
|
builtinDir: config.skills.builtinDir ?? join(ASSETS, "skills"),
|
|
@@ -215,7 +221,10 @@ function agentAssembly(
|
|
|
215
221
|
]
|
|
216
222
|
: [],
|
|
217
223
|
agentServer: agentServerPlugin({
|
|
218
|
-
|
|
224
|
+
teamId,
|
|
225
|
+
formerTeamIds: config.team.formerIds,
|
|
226
|
+
// Without the agent guild the team lives in the app's rooms, as on a host without Discord.
|
|
227
|
+
...(discord?.guild
|
|
219
228
|
? {
|
|
220
229
|
guildId: discord.guild,
|
|
221
230
|
...(discord.entryChannel
|
|
@@ -319,6 +328,7 @@ export async function defineRoundtable(
|
|
|
319
328
|
return {
|
|
320
329
|
options: {
|
|
321
330
|
logger,
|
|
331
|
+
autonomy: config.autonomy,
|
|
322
332
|
environment: {
|
|
323
333
|
locale: config.locale,
|
|
324
334
|
timeZone: config.timeZone,
|
|
@@ -356,7 +366,7 @@ export async function defineRoundtable(
|
|
|
356
366
|
scheduleStorePlugin(),
|
|
357
367
|
precheckPlugin(),
|
|
358
368
|
// Before the agent server and the operator's plugins, which register the texts the owner edits.
|
|
359
|
-
promptsPlugin({ guildId:
|
|
369
|
+
promptsPlugin({ guildId: teamIdOf(config) }),
|
|
360
370
|
// Before Discord, whose services hand out its instance, and the modules and agent server that use it.
|
|
361
371
|
threadsPlugin({ dataDir: config.dataDir }),
|
|
362
372
|
channelContextPlugin(),
|
package/src/core/host/drain.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { DRAIN_LIMIT_MS } from "../config/config-schema.ts";
|
|
2
2
|
import type { Logger } from "../kernel/log.ts";
|
|
3
|
+
import type { Service } from "../plugin-api/plugin.ts";
|
|
3
4
|
|
|
4
5
|
/** How long work that was told to stop gets to end before the shutdown goes on without it. */
|
|
5
6
|
const ABORT_GRACE_MS = 10_000;
|
|
@@ -56,6 +57,51 @@ export async function drainWork(
|
|
|
56
57
|
return left;
|
|
57
58
|
}
|
|
58
59
|
|
|
60
|
+
/** Tells each service the drain begins, as `Service.beginDrain` says; a failure is logged and the rest are still told. */
|
|
61
|
+
export function beginServicesDrain(
|
|
62
|
+
services: readonly Pick<Service, "name" | "beginDrain">[],
|
|
63
|
+
logger: Logger,
|
|
64
|
+
): void {
|
|
65
|
+
for (const service of services) {
|
|
66
|
+
try {
|
|
67
|
+
service.beginDrain?.();
|
|
68
|
+
} catch (error) {
|
|
69
|
+
logger.error(
|
|
70
|
+
{ service: service.name, err: error },
|
|
71
|
+
"the service could not be told the drain begins",
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Tells each service that still has work to stop it, as `Service.abort` says; a failure is
|
|
79
|
+
* logged and the rest are still told. Whether any service told some work.
|
|
80
|
+
*/
|
|
81
|
+
export function abortServices(
|
|
82
|
+
services: readonly Pick<Service, "name" | "busy" | "abort">[],
|
|
83
|
+
logger: Logger,
|
|
84
|
+
): boolean {
|
|
85
|
+
let told = false;
|
|
86
|
+
for (const service of services) {
|
|
87
|
+
if (!service.abort || !service.busy?.().length) continue;
|
|
88
|
+
try {
|
|
89
|
+
const aborted = service.abort();
|
|
90
|
+
logger.warn(
|
|
91
|
+
{ service: service.name, told: aborted },
|
|
92
|
+
"stopping the work",
|
|
93
|
+
);
|
|
94
|
+
told ||= aborted;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
logger.error(
|
|
97
|
+
{ service: service.name, err: error },
|
|
98
|
+
"the work could not be stopped",
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return told;
|
|
103
|
+
}
|
|
104
|
+
|
|
59
105
|
/**
|
|
60
106
|
* Tells each channel's running turn to stop, as the owner's stop does; a failure is logged and the
|
|
61
107
|
* rest are still told. Whether any turn was running to be told.
|
|
@@ -17,6 +17,7 @@ import { setLocale } from "../i18n/index.ts";
|
|
|
17
17
|
import { contactsOf } from "../identity/identity-view.ts";
|
|
18
18
|
import { NotLinkedError } from "../kernel/errors.ts";
|
|
19
19
|
import { setTimeZone } from "../kernel/time.ts";
|
|
20
|
+
import { refuseYoloOverrides } from "../plugin-api/holds.ts";
|
|
20
21
|
import type {
|
|
21
22
|
HostEnv,
|
|
22
23
|
LinkedSessions,
|
|
@@ -232,6 +233,10 @@ export class Linkage {
|
|
|
232
233
|
);
|
|
233
234
|
const { routes, channels } = this.registry;
|
|
234
235
|
this.sessions = linkSessions(this.registry);
|
|
236
|
+
refuseYoloOverrides(
|
|
237
|
+
this.registry.holdRules,
|
|
238
|
+
this.#options.autonomy?.yolo.allow ?? [],
|
|
239
|
+
);
|
|
235
240
|
this.events.link(this.registry.handlers);
|
|
236
241
|
const identity = services.find(IDENTITY);
|
|
237
242
|
this.router = new ChannelRouter({
|
package/src/core/host/host.ts
CHANGED
|
@@ -10,6 +10,7 @@ import type { Locale } from "../i18n/index.ts";
|
|
|
10
10
|
import type { JudgeModel } from "../judging/model-judge.ts";
|
|
11
11
|
import { MigrationError, PluginError } from "../kernel/errors.ts";
|
|
12
12
|
import type { Logger } from "../kernel/log.ts";
|
|
13
|
+
import type { Autonomy } from "../modes/autonomy.ts";
|
|
13
14
|
import type {
|
|
14
15
|
AgentTools,
|
|
15
16
|
RoundtablePlugin,
|
|
@@ -18,7 +19,13 @@ import type {
|
|
|
18
19
|
import { RUNTIME } from "../plugin-api/services.ts";
|
|
19
20
|
import type { ToolTierTable } from "../runtime/tool-tiers.ts";
|
|
20
21
|
import type { ShellEnvConfig } from "../shared/child-env.ts";
|
|
21
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
abortServices,
|
|
24
|
+
beginServicesDrain,
|
|
25
|
+
type DrainOptions,
|
|
26
|
+
drainWork,
|
|
27
|
+
stopTurns,
|
|
28
|
+
} from "./drain.ts";
|
|
22
29
|
import { attempt, startInBackground } from "./host-background.ts";
|
|
23
30
|
import { Linkage, type RegistrationReport } from "./host-registration.ts";
|
|
24
31
|
|
|
@@ -63,6 +70,11 @@ export interface RoundtableOptions {
|
|
|
63
70
|
* the peers' messages at `peerMessageChars`; `defineRoundtable` does both.
|
|
64
71
|
*/
|
|
65
72
|
peerBots?: PeerBotsConfig;
|
|
73
|
+
/**
|
|
74
|
+
* What yolo mode may approve. Stops the start when `autonomy.yolo.allow` names a hold rule that
|
|
75
|
+
* yolo mode never approves (`HoldRule.yolo`).
|
|
76
|
+
*/
|
|
77
|
+
autonomy?: Pick<Autonomy, "yolo">;
|
|
66
78
|
/** The model the default judge asks, when no plugin provides a judge. */
|
|
67
79
|
judgeModel?: JudgeModel;
|
|
68
80
|
/**
|
|
@@ -270,20 +282,31 @@ export class Roundtable {
|
|
|
270
282
|
// Turns already running finish, but nothing new starts from here on, whoever asks.
|
|
271
283
|
logger.info({ signal }, "shutting down once idle");
|
|
272
284
|
this.#link.queue.close();
|
|
285
|
+
beginServicesDrain(this.#started, logger);
|
|
286
|
+
const stopQueuedTurns = () =>
|
|
287
|
+
stopTurns(
|
|
288
|
+
this.#link.queue.busy(),
|
|
289
|
+
(channel) => this.#link.router?.stop(channel as ChannelKey) ?? false,
|
|
290
|
+
logger,
|
|
291
|
+
);
|
|
273
292
|
const left = await drainWork({
|
|
274
293
|
...drain,
|
|
275
294
|
busy: () => [
|
|
276
295
|
...this.#link.queue.busy(),
|
|
277
296
|
...this.#started.flatMap((service) => service.busy?.() ?? []),
|
|
278
297
|
],
|
|
279
|
-
// A turn the limit finds still running is stopped, as the owner's stop would
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
298
|
+
// A turn the limit finds still running is stopped, as the owner's stop would, and a
|
|
299
|
+
// service is told to stop its own work before it is stopped.
|
|
300
|
+
abort: () => {
|
|
301
|
+
const turns = stopQueuedTurns();
|
|
302
|
+
const services = abortServices(this.#started, logger);
|
|
303
|
+
return turns || services;
|
|
304
|
+
},
|
|
286
305
|
});
|
|
306
|
+
// Only what ran before the drain ended finishes; nothing delivered later starts.
|
|
307
|
+
this.#link.queue.seal();
|
|
308
|
+
// A turn that started during the abort grace, a report's, is stopped too, so the teardown does not wait for it.
|
|
309
|
+
if (left.length > 0) stopQueuedTurns();
|
|
287
310
|
if (left.length > 0) {
|
|
288
311
|
logger.warn(
|
|
289
312
|
{ aborted: left },
|
|
@@ -73,6 +73,13 @@ export type HoldDescription =
|
|
|
73
73
|
*/
|
|
74
74
|
export interface HoldRule {
|
|
75
75
|
name: string;
|
|
76
|
+
/**
|
|
77
|
+
* `"never"` marks a rule whose held calls yolo mode never approves, whatever the configuration
|
|
78
|
+
* says. Its calls carry the class `<name>.yolo-never` (the classes it reports are ignored), which
|
|
79
|
+
* the yolo approver always cards, and the host refuses to start when `autonomy.yolo.allow` names
|
|
80
|
+
* the rule or one of its classes.
|
|
81
|
+
*/
|
|
82
|
+
yolo?: "never";
|
|
76
83
|
/**
|
|
77
84
|
* Whether the rule stands for other rules' calls, as the precheck scripts' does, and may report
|
|
78
85
|
* their ids in `holdClasses`. Any other rule may report only ids that start with its own name
|
|
@@ -155,6 +162,29 @@ export interface HoldRule {
|
|
|
155
162
|
mayName?(tool: string): boolean;
|
|
156
163
|
}
|
|
157
164
|
|
|
165
|
+
/** The class a `yolo: "never"` rule's calls carry, after `<rule>.`; yolo mode never approves it. */
|
|
166
|
+
export const YOLO_NEVER_CLASS = "yolo-never";
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Refuses a yolo configuration that would approve a call of a `yolo: "never"` rule: `allow` naming
|
|
170
|
+
* the rule or one of its classes. Naming it in `card` is harmless and passes.
|
|
171
|
+
*/
|
|
172
|
+
export function refuseYoloOverrides(
|
|
173
|
+
rules: readonly HoldRule[],
|
|
174
|
+
allow: readonly string[],
|
|
175
|
+
): void {
|
|
176
|
+
for (const rule of rules) {
|
|
177
|
+
if (rule.yolo !== "never") continue;
|
|
178
|
+
const named = allow.find(
|
|
179
|
+
(id) => id === rule.name || id.startsWith(`${rule.name}.`),
|
|
180
|
+
);
|
|
181
|
+
if (named !== undefined)
|
|
182
|
+
throw new PluginError(
|
|
183
|
+
`autonomy.yolo.allow names ${named}, but hold rule ${rule.name} is one yolo mode never approves. Remove ${named} from autonomy.yolo.allow.`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
158
188
|
/** The description of a held call with its hold classes. */
|
|
159
189
|
export interface ClassedHold {
|
|
160
190
|
description: HoldDescription;
|
|
@@ -394,6 +424,7 @@ function validClasses(
|
|
|
394
424
|
rule: HoldRule,
|
|
395
425
|
names: ReadonlySet<string>,
|
|
396
426
|
): readonly string[] {
|
|
427
|
+
if (rule.yolo === "never") return [`${rule.name}.${YOLO_NEVER_CLASS}`];
|
|
397
428
|
if (!Array.isArray(reported) || reported.length === 0) return [rule.name];
|
|
398
429
|
const ids = reported as unknown[];
|
|
399
430
|
if (!ids.every((id): id is string => isClassOf(id, rule, names)))
|
|
@@ -46,14 +46,17 @@ export interface Service {
|
|
|
46
46
|
name: string;
|
|
47
47
|
start?(): Promise<void> | void;
|
|
48
48
|
/**
|
|
49
|
-
* Starts once every service's `start` and the HTTP listeners are up, without holding up the
|
|
50
|
-
*
|
|
51
|
-
* other services' background starts still run. `stop` runs for it like for any started service.
|
|
49
|
+
* Starts once every service's `start` and the HTTP listeners are up, without holding up the boot: a failure is logged, heard
|
|
50
|
+
* as `serviceStarted` with `failed`, never fatal, and the other background starts still run. `stop` runs for it as for any service.
|
|
52
51
|
*/
|
|
53
52
|
startInBackground?(): Promise<void>;
|
|
54
53
|
stop?(): Promise<void> | void;
|
|
55
54
|
/** Work still running or waiting, one entry each; the shutdown drain waits until every list is empty. */
|
|
56
55
|
busy?(): string[];
|
|
56
|
+
/** As the shutdown begins to wait for work, before the drain: from here on the service should start no work of its own. */
|
|
57
|
+
beginDrain?(): void;
|
|
58
|
+
/** At the drain limit, tells the work `busy` lists to stop gently, before the abort grace and `stop`; whether it told any. */
|
|
59
|
+
abort?(): boolean;
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
/** How a service's background start ended. */
|
|
@@ -264,8 +264,10 @@ export interface AgentChange {
|
|
|
264
264
|
|
|
265
265
|
/** The agent team as plugins use it: who is in it, what model they run, and their channels. */
|
|
266
266
|
export interface AgentTeam extends HelperTeam {
|
|
267
|
-
/** The team's id: the Discord guild,
|
|
267
|
+
/** The team's id: what its rows are stored under, the Discord guild's id unless the host sets one, else `"local"`. */
|
|
268
268
|
readonly guildId: string;
|
|
269
|
+
/** The Discord guild the team's channels live in; absent on a team without Discord channels. */
|
|
270
|
+
readonly discordGuild?: string | undefined;
|
|
269
271
|
/** Called when a turn starts or ends and when an agent or group changes. */
|
|
270
272
|
onChange(listener: () => void): void;
|
|
271
273
|
/** Every active agent and group, with what each is doing now. */
|
|
@@ -280,10 +282,7 @@ export interface AgentTeam extends HelperTeam {
|
|
|
280
282
|
* Discord, the one `coordinator` names (the first seed by default); undefined while none is active.
|
|
281
283
|
*/
|
|
282
284
|
coordinator(): string | undefined;
|
|
283
|
-
/**
|
|
284
|
-
* Every key an agent or group is addressed by: its Discord channel when it has one, then its
|
|
285
|
-
* room on each surface with `agentRooms`; empty for an unknown name.
|
|
286
|
-
*/
|
|
285
|
+
/** Every key an agent or group is addressed by: its Discord channel, then its room on each `agentRooms` surface; empty for an unknown name. */
|
|
287
286
|
roomsOf(name: string): ChannelKey[];
|
|
288
287
|
/** The model and thinking setting of an agent's next run: its own, or the assistant's. */
|
|
289
288
|
modelOf(name: string): { model: string; thinking: ThinkingSetting };
|
|
@@ -52,6 +52,14 @@ function stop(current: Drawer): void {
|
|
|
52
52
|
if (drawer === current) drawer = undefined;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
type Drawn = { text: string } | "refused" | "slow" | "unavailable";
|
|
56
|
+
|
|
57
|
+
/** What a test replaces: the drawing itself and the clock that measures it, so no test depends on real elapsed time. */
|
|
58
|
+
export interface DrawerSeams {
|
|
59
|
+
draw?: (source: string, waitMs: number) => Drawn;
|
|
60
|
+
now?: () => number;
|
|
61
|
+
}
|
|
62
|
+
|
|
55
63
|
/**
|
|
56
64
|
* A drawer of Mermaid diagrams as Unicode box drawing for one reply: it gives undefined for a source
|
|
57
65
|
* that cannot be shown right (see `mermaidText` of `pi-better-show/server`), is longer than
|
|
@@ -65,7 +73,11 @@ function stop(current: Drawer): void {
|
|
|
65
73
|
* left undrawn for the budget, or because no worker could be started, is no verdict and is tried
|
|
66
74
|
* again in a later reply.
|
|
67
75
|
*/
|
|
68
|
-
export function diagramDrawer(
|
|
76
|
+
export function diagramDrawer(
|
|
77
|
+
seams: DrawerSeams = {},
|
|
78
|
+
): (source: string) => string | undefined {
|
|
79
|
+
const drawOne = seams.draw ?? draw;
|
|
80
|
+
const now = seams.now ?? (() => performance.now());
|
|
69
81
|
let left = DIAGRAM_TEXT_DEADLINE_MS;
|
|
70
82
|
let fresh = 0;
|
|
71
83
|
return (source) => {
|
|
@@ -82,9 +94,9 @@ export function diagramDrawer(): (source: string) => string | undefined {
|
|
|
82
94
|
if (fresh >= MAX_DRAWN_PER_REPLY || left <= 0) return undefined;
|
|
83
95
|
fresh += 1;
|
|
84
96
|
const waitMs = left;
|
|
85
|
-
const started =
|
|
86
|
-
const result =
|
|
87
|
-
left -=
|
|
97
|
+
const started = now();
|
|
98
|
+
const result = drawOne(source, waitMs);
|
|
99
|
+
left -= now() - started;
|
|
88
100
|
if (typeof result === "object") remember(source, result.text);
|
|
89
101
|
else if (result === "refused") remember(source, null);
|
|
90
102
|
else if (result === "slow" && waitMs >= DIAGRAM_TEXT_DEADLINE_MS) {
|
|
@@ -110,10 +122,7 @@ function remember(source: string, text: string | null): void {
|
|
|
110
122
|
}
|
|
111
123
|
}
|
|
112
124
|
|
|
113
|
-
function draw(
|
|
114
|
-
source: string,
|
|
115
|
-
waitMs: number,
|
|
116
|
-
): { text: string } | "refused" | "slow" | "unavailable" {
|
|
125
|
+
function draw(source: string, waitMs: number): Drawn {
|
|
117
126
|
if (unavailable) return "unavailable";
|
|
118
127
|
drawer ??= start();
|
|
119
128
|
const current = drawer;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
1
2
|
import type { ChannelKey } from "../domain/sessions.ts";
|
|
2
3
|
import { HostStoppingError } from "../kernel/errors.ts";
|
|
3
4
|
|
|
@@ -6,7 +7,10 @@ export class ChannelQueue {
|
|
|
6
7
|
readonly #tails = new Map<ChannelKey, Promise<unknown>>();
|
|
7
8
|
readonly #sizes = new Map<ChannelKey, number>();
|
|
8
9
|
readonly #listeners: ((channel: ChannelKey) => void)[] = [];
|
|
10
|
+
/** Set inside `finishing`, so what a delivery starts and awaits sees the queue as open. */
|
|
11
|
+
readonly #finishing = new AsyncLocalStorage<true>();
|
|
9
12
|
#closed = false;
|
|
13
|
+
#sealed = false;
|
|
10
14
|
|
|
11
15
|
/** Called whenever a channel's count of running and waiting tasks changes. */
|
|
12
16
|
onChange(listener: (channel: ChannelKey) => void): void {
|
|
@@ -17,9 +21,16 @@ export class ChannelQueue {
|
|
|
17
21
|
for (const listener of this.#listeners) listener(channel);
|
|
18
22
|
}
|
|
19
23
|
|
|
20
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* Whether the host is shutting down, so no task starts any more; false for work inside
|
|
26
|
+
* `finishing` until the queue is sealed.
|
|
27
|
+
*/
|
|
21
28
|
get closed(): boolean {
|
|
22
|
-
return this.#closed;
|
|
29
|
+
return this.#closed && !this.#admitted();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
#admitted(): boolean {
|
|
33
|
+
return !this.#sealed && this.#finishing.getStore() === true;
|
|
23
34
|
}
|
|
24
35
|
|
|
25
36
|
/**
|
|
@@ -30,9 +41,31 @@ export class ChannelQueue {
|
|
|
30
41
|
this.#closed = true;
|
|
31
42
|
}
|
|
32
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Ends the drain: from now on `finishing` admits nothing either. The host seals the queue once
|
|
46
|
+
* the drain has waited, before it stops the services.
|
|
47
|
+
*/
|
|
48
|
+
seal(): void {
|
|
49
|
+
this.#closed = true;
|
|
50
|
+
this.#sealed = true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Runs `task` as the delivery of work the shutdown drain waits for, such as the report of a
|
|
55
|
+
* task that was running when the host began to stop. A queue task started inside it, and
|
|
56
|
+
* everything that task awaits, is admitted although the queue is closed, until the drain ends.
|
|
57
|
+
* Outside a shutdown it changes nothing.
|
|
58
|
+
*/
|
|
59
|
+
finishing<T>(task: () => Promise<T>): Promise<T> {
|
|
60
|
+
return this.#finishing.run(true, task);
|
|
61
|
+
}
|
|
62
|
+
|
|
33
63
|
run<T>(channel: ChannelKey, task: () => Promise<T>): Promise<T> {
|
|
64
|
+
const admitted = this.#admitted();
|
|
34
65
|
const start = () =>
|
|
35
|
-
this.#closed
|
|
66
|
+
this.#closed && !(admitted && !this.#sealed)
|
|
67
|
+
? Promise.reject(new HostStoppingError())
|
|
68
|
+
: task();
|
|
36
69
|
this.#sizes.set(channel, this.size(channel) + 1);
|
|
37
70
|
this.#changed(channel);
|
|
38
71
|
const previous = this.#tails.get(channel) ?? Promise.resolve();
|
|
@@ -6,6 +6,7 @@ import type { ApprovedBy } from "../../interactions/prompts.ts";
|
|
|
6
6
|
import { type Autonomy, DEFAULT_AUTONOMY } from "../../modes/autonomy.ts";
|
|
7
7
|
import type { ConversationModes } from "../../modes/conversation-modes.ts";
|
|
8
8
|
import { YOLO_CARDED_SHELL_CLASSES } from "../../modules/host-shell/shell-policy.ts";
|
|
9
|
+
import { YOLO_NEVER_CLASS } from "../../plugin-api/holds.ts";
|
|
9
10
|
import type { PiAgentRuntimeOptions } from "../runtime-types.ts";
|
|
10
11
|
import type { AutoApprover } from "./confirmation-gate.ts";
|
|
11
12
|
import type { SpeakerRef } from "./gate-refusals.ts";
|
|
@@ -73,6 +74,8 @@ export function yoloApprover(seams: YoloSeams): AutoApprover {
|
|
|
73
74
|
const card = new Set(autonomy.yolo.card);
|
|
74
75
|
const carded = new Set(YOLO_CARDED_SHELL_CLASSES);
|
|
75
76
|
const approves = (id: string) => {
|
|
77
|
+
// A `yolo: "never"` rule's call, which no configuration lets through.
|
|
78
|
+
if (id.endsWith(`.${YOLO_NEVER_CLASS}`)) return false;
|
|
76
79
|
if (card.has(id) || card.has(ruleOf(id))) return false;
|
|
77
80
|
// A rule that reports classes has opted in; one that reports none is approved only by name.
|
|
78
81
|
if (unclassed(id)) return allow.has(id);
|
package/src/core/runtime/mcp.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
import type { ExtensionFactory } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { mcpAdapterExtension } from "../shared/mcp-adapter.ts";
|
|
3
3
|
|
|
4
|
+
/** A tool's MCP safety hints as its server declares them; a hint it leaves out is absent. */
|
|
5
|
+
export interface McpToolHints {
|
|
6
|
+
readOnlyHint?: boolean;
|
|
7
|
+
destructiveHint?: boolean;
|
|
8
|
+
idempotentHint?: boolean;
|
|
9
|
+
openWorldHint?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
4
12
|
/** An MCP server a session connects to, with the tools it offers. */
|
|
5
13
|
export interface VirtualServer {
|
|
6
14
|
name: string;
|
|
7
15
|
url: string;
|
|
8
16
|
tools: string[];
|
|
17
|
+
/** Each tool's hints by tool name; a tool without an entry declared none. Absent when its source reads no hints. */
|
|
18
|
+
hints?: Readonly<Record<string, McpToolHints>>;
|
|
9
19
|
}
|
|
10
20
|
|
|
11
21
|
/** MCP endpoints supplied by a session plugin, reached with its bearer token. */
|
|
@@ -19,3 +19,11 @@ export function partial<T extends object>(given: Partial<T>): T {
|
|
|
19
19
|
},
|
|
20
20
|
}) as T;
|
|
21
21
|
}
|
|
22
|
+
|
|
23
|
+
/** A member of an object that may be a `partial()` stand-in: its value, or undefined when the stand-in was not given it. */
|
|
24
|
+
export function memberOf<T extends object, K extends keyof T>(
|
|
25
|
+
object: T,
|
|
26
|
+
member: K,
|
|
27
|
+
): T[K] | undefined {
|
|
28
|
+
return Reflect.has(object, member) ? object[member] : undefined;
|
|
29
|
+
}
|
package/src/kit/holds.ts
CHANGED
package/src/kit/index.ts
CHANGED
|
@@ -57,7 +57,11 @@ export {
|
|
|
57
57
|
THINKING_LEVELS,
|
|
58
58
|
thinkingLabel,
|
|
59
59
|
} from "./domain.ts";
|
|
60
|
-
export {
|
|
60
|
+
export {
|
|
61
|
+
HOLD_DESCRIBE_TIMEOUT_MS,
|
|
62
|
+
holdChain,
|
|
63
|
+
YOLO_NEVER_CLASS,
|
|
64
|
+
} from "./holds.ts";
|
|
61
65
|
export type {
|
|
62
66
|
CodexFetch,
|
|
63
67
|
CodexImage,
|
|
@@ -210,6 +214,7 @@ export type {
|
|
|
210
214
|
CardLimits,
|
|
211
215
|
InterimPosterOptions,
|
|
212
216
|
McpEndpoint,
|
|
217
|
+
McpToolHints,
|
|
213
218
|
PromptSlot,
|
|
214
219
|
VirtualServer,
|
|
215
220
|
} from "./worker.ts";
|
package/src/kit/worker.ts
CHANGED
|
@@ -9,7 +9,7 @@ export {
|
|
|
9
9
|
export { canonicalJson } from "../core/runtime/extensions/confirmation-gate.ts";
|
|
10
10
|
export type { InterimPosterOptions } from "../core/runtime/interim-text.ts";
|
|
11
11
|
export { InterimPoster } from "../core/runtime/interim-text.ts";
|
|
12
|
-
export type { VirtualServer } from "../core/runtime/mcp.ts";
|
|
12
|
+
export type { McpToolHints, VirtualServer } from "../core/runtime/mcp.ts";
|
|
13
13
|
export { mcpExtension } from "../core/runtime/mcp.ts";
|
|
14
14
|
export type { PromptSlot } from "../core/runtime/prompt-slot.ts";
|
|
15
15
|
export { promptSlot, workTimeout } from "../core/runtime/prompt-slot.ts";
|
package/src/testing.ts
CHANGED
|
@@ -86,6 +86,7 @@ import { type ToolTierTable, toolTiers } from "./core/runtime/tool-tiers.ts";
|
|
|
86
86
|
import { PERSONAL_TARGET } from "./core/shared/personal-target.ts";
|
|
87
87
|
import { fakeDiscordPoster } from "./core/testing/fake-poster.ts";
|
|
88
88
|
import { mapIdentity } from "./core/testing/map-identity.ts";
|
|
89
|
+
import { memberOf } from "./core/testing/partial.ts";
|
|
89
90
|
import {
|
|
90
91
|
callRegisteredTool,
|
|
91
92
|
registeredTools,
|
|
@@ -587,10 +588,9 @@ export async function testPlugin(
|
|
|
587
588
|
? [
|
|
588
589
|
agentClaim({
|
|
589
590
|
owner,
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
>[0]["team"],
|
|
591
|
+
discordGuild: memberOf(agentServer.team, "discordGuild"),
|
|
592
|
+
// SAFETY: the claim reads the concrete team's members (owns, answerOwner, answerGroup, answerBackground, startFresh), which a test that gives a team for it supplies.
|
|
593
|
+
team: agentServer.team as never,
|
|
594
594
|
runtime: {
|
|
595
595
|
steer: (...args) => services.get(AGENTS).runtime.steer(...args),
|
|
596
596
|
stop: (channel) => services.get(AGENTS).runtime.stop(channel),
|