pi-roundtable 0.53.6 → 0.54.1

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.
Files changed (91) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +6 -2
  3. package/README.zh-TW.md +6 -2
  4. package/docs/plugins.md +199 -7
  5. package/package.json +3 -1
  6. package/runner/Dockerfile +46 -0
  7. package/runner/Dockerfile.dockerignore +5 -0
  8. package/src/cli/checks/images.ts +3 -2
  9. package/src/cli/checks/preset.ts +20 -0
  10. package/src/cli/checks/runner.ts +45 -0
  11. package/src/cli/doctor.ts +7 -1
  12. package/src/core/agents/agent-claim.ts +7 -4
  13. package/src/core/agents/agent-prompt.ts +11 -2
  14. package/src/core/agents/helper-workspace.ts +72 -0
  15. package/src/core/agents/team-options.ts +2 -0
  16. package/src/core/agents/team-prompt.ts +12 -1
  17. package/src/core/builtin/agent-server.ts +8 -0
  18. package/src/core/builtin/polls.ts +67 -0
  19. package/src/core/builtin/runner.ts +139 -0
  20. package/src/core/config/config-schema.ts +4 -0
  21. package/src/core/config/config.ts +49 -185
  22. package/src/core/config/preset.ts +51 -0
  23. package/src/core/config/resolve-config.ts +175 -0
  24. package/src/core/config/runner-config.ts +80 -0
  25. package/src/core/contract/polls.ts +71 -0
  26. package/src/core/contract/runtime.ts +12 -0
  27. package/src/core/contract/surface.ts +13 -0
  28. package/src/core/discord/discord-polls.ts +156 -0
  29. package/src/core/discord/discord-replies.ts +55 -0
  30. package/src/core/discord/discord-surface.ts +13 -43
  31. package/src/core/domain/errors.ts +5 -0
  32. package/src/core/domain/sessions.ts +8 -1
  33. package/src/core/host/define-roundtable.ts +8 -0
  34. package/src/core/host/host-registration.ts +7 -0
  35. package/src/core/host/host.ts +8 -0
  36. package/src/core/i18n/schedules.ts +2 -0
  37. package/src/core/modules/polls/poll-result-turns.ts +124 -0
  38. package/src/core/modules/polls/poll-rules.ts +108 -0
  39. package/src/core/modules/polls/poll-service.ts +477 -0
  40. package/src/core/modules/polls/poll-store.ts +336 -0
  41. package/src/core/modules/polls/poll-tools.ts +118 -0
  42. package/src/core/modules/polls/poll-turn-text.ts +60 -0
  43. package/src/core/plugin-api/define.ts +9 -2
  44. package/src/core/plugin-api/holds.ts +2 -0
  45. package/src/core/plugin-api/plugin.ts +8 -0
  46. package/src/core/runner/protocol.ts +456 -0
  47. package/src/core/runner/runner-client.ts +450 -0
  48. package/src/core/runner/runner-drivers.ts +356 -0
  49. package/src/core/runner/runner-files.ts +102 -0
  50. package/src/core/runner/runner-holds.ts +76 -0
  51. package/src/core/runner/runner-port.ts +77 -0
  52. package/src/core/runner/runner-probe.ts +53 -0
  53. package/src/core/runner/runner-prompt.ts +36 -0
  54. package/src/core/runner/runner-session.ts +131 -0
  55. package/src/core/runner/runner-tiers.ts +20 -0
  56. package/src/core/runner/runner-tools.ts +325 -0
  57. package/src/core/runner/settings.ts +108 -0
  58. package/src/core/runtime/conversation-sessions.ts +73 -15
  59. package/src/core/runtime/extensions/approval-card.ts +29 -2
  60. package/src/core/runtime/extensions/confirmation-gate.ts +24 -63
  61. package/src/core/runtime/extensions/gate-card.ts +105 -0
  62. package/src/core/runtime/extensions/yolo.ts +17 -3
  63. package/src/core/runtime/pi-agent-runtime.ts +37 -68
  64. package/src/core/runtime/runtime-plugin.ts +9 -0
  65. package/src/core/runtime/runtime-types.ts +29 -1
  66. package/src/core/runtime/session-factory.ts +59 -62
  67. package/src/core/runtime/session-shell.ts +43 -0
  68. package/src/core/runtime/session-tool-names.ts +78 -0
  69. package/src/core/runtime/turn-selection.ts +97 -0
  70. package/src/core/shared/generate-image-tool.ts +4 -4
  71. package/src/core/shared/poll-target.ts +14 -0
  72. package/src/core/shared/workspace-files.ts +42 -4
  73. package/src/core/testing/fake-polls.ts +131 -0
  74. package/src/core/testing/fake-runner.ts +146 -0
  75. package/src/core/testing/surface-contract-approvals.ts +215 -0
  76. package/src/core/testing/surface-contract-polls.ts +142 -0
  77. package/src/core/testing/surface-contract.ts +32 -208
  78. package/src/core/tool-set.snapshot.json +8 -0
  79. package/src/index.ts +17 -0
  80. package/src/kit/index.ts +6 -0
  81. package/src/kit/polls.ts +8 -0
  82. package/src/runner-server/exec.ts +174 -0
  83. package/src/runner-server/file-helper.ts +208 -0
  84. package/src/runner-server/files.ts +93 -0
  85. package/src/runner-server/host.ts +65 -0
  86. package/src/runner-server/main.ts +89 -0
  87. package/src/runner-server/server.ts +318 -0
  88. package/src/runner-server/shared-dirs.ts +45 -0
  89. package/src/runner-server/state.ts +293 -0
  90. package/src/runner-server/transports.ts +187 -0
  91. package/src/testing.ts +7 -0
@@ -0,0 +1,456 @@
1
+ import { isAbsolute, posix } from "node:path";
2
+
3
+ /**
4
+ * The runner protocol, version 1: what the host's runner client and the runner server inside its
5
+ * container say to each other. It imports nothing else of the core, because the runner image
6
+ * copies this file beside the server and runs it with no installed packages.
7
+ *
8
+ * Frames are newline-delimited JSON objects of at most `MAX_FRAME_BYTES` each; bytes travel as
9
+ * base64. The client's first frame is `hello`; the server answers `welcome` or an `error` without
10
+ * an id and closes. Every request after it carries an `id` the client picks, unique among its
11
+ * requests in flight; the server answers `ok` with a result or `error`, except `exec`, which
12
+ * streams `data` frames and ends with `exit` (or `error`). `abort { id }` stops a request: a
13
+ * queued one ends with `error` code `aborted`, a running command is killed and ends with `exit`.
14
+ */
15
+ export const RUNNER_PROTOCOL_VERSION = 1;
16
+
17
+ /** The largest frame either side sends or accepts, newline excluded. */
18
+ export const MAX_FRAME_BYTES = 16 * 1024 * 1024;
19
+
20
+ /** Where the runner keeps the conversations' workspaces, inside its container. */
21
+ export const RUNNER_WORKSPACES = "/roundtable-runner/ws";
22
+ /** The runner server's socket directory and socket, inside its container. */
23
+ export const RUNNER_SOCKET_DIR = "/run/roundtable-runner";
24
+ export const RUNNER_SOCKET = `${RUNNER_SOCKET_DIR}/runner.sock`;
25
+ /** The per-boot token's file name, beside the socket. */
26
+ export const RUNNER_TOKEN_FILE = "token";
27
+
28
+ const MIB = 1024 * 1024;
29
+
30
+ /** The limits a host uses unless its configuration says otherwise. */
31
+ export const RUNNER_DEFAULTS = {
32
+ commandTimeoutMs: 120_000,
33
+ maxOutputBytes: MIB,
34
+ maxFileBytes: 10 * MIB,
35
+ keepDays: 30,
36
+ } as const;
37
+
38
+ /** The most a request may ask for; the server refuses a request over them. */
39
+ export const RUNNER_CEILINGS = {
40
+ commandTimeoutMs: 900_000,
41
+ maxOutputBytes: 8 * MIB,
42
+ maxFileBytes: 11 * MIB,
43
+ } as const;
44
+
45
+ /** A protocol violation; the stream it came from is no longer usable. */
46
+ export class FrameError extends Error {
47
+ override name = "FrameError";
48
+ }
49
+
50
+ export type AccessMode = "r" | "rw";
51
+
52
+ export type RunnerRequest =
53
+ | {
54
+ type: "exec";
55
+ id: number;
56
+ conversation: string;
57
+ command: string;
58
+ /** An absolute path inside the conversation's workspace. */
59
+ cwd: string;
60
+ timeoutMs: number;
61
+ maxOutputBytes: number;
62
+ }
63
+ | {
64
+ type: "read";
65
+ id: number;
66
+ conversation: string;
67
+ path: string;
68
+ maxBytes: number;
69
+ }
70
+ | {
71
+ type: "write";
72
+ id: number;
73
+ conversation: string;
74
+ path: string;
75
+ /** The file's bytes, base64. */
76
+ data: string;
77
+ maxBytes: number;
78
+ }
79
+ | { type: "mkdir"; id: number; conversation: string; path: string }
80
+ | {
81
+ type: "access";
82
+ id: number;
83
+ conversation: string;
84
+ path: string;
85
+ mode: AccessMode;
86
+ }
87
+ | { type: "stat"; id: number; conversation: string; path: string }
88
+ | { type: "detect-image"; id: number; conversation: string; path: string }
89
+ | { type: "remove-workspace"; id: number; conversation: string }
90
+ | { type: "sweep"; id: number; keepDays: number }
91
+ | { type: "ping"; id: number };
92
+
93
+ export type ClientFrame =
94
+ | { type: "hello"; version: number; token?: string }
95
+ | { type: "abort"; id: number }
96
+ | RunnerRequest;
97
+
98
+ /** A file's facts; `path` is where it resolves, symlinks followed, always inside the workspace. */
99
+ export interface RunnerStat {
100
+ kind: "file" | "directory" | "other";
101
+ size: number;
102
+ mtimeMs: number;
103
+ path: string;
104
+ }
105
+
106
+ /** The `ok` result of each request type; `exec` ends with `exit` instead. */
107
+ export interface RunnerResults {
108
+ read: { data: string };
109
+ write: Record<string, never>;
110
+ mkdir: Record<string, never>;
111
+ access: Record<string, never>;
112
+ stat: RunnerStat;
113
+ "detect-image": { mimeType: string | null };
114
+ "remove-workspace": { removed: boolean };
115
+ sweep: { removed: string[] };
116
+ ping: { version: number; uptimeMs: number };
117
+ }
118
+
119
+ export const RUNNER_ERROR_CODES = [
120
+ /** A frame the server cannot use; for a hello, the stream closes. */
121
+ "bad-frame",
122
+ /** The hello's version is not the server's. */
123
+ "version",
124
+ /** The hello's token is missing or wrong. */
125
+ "auth",
126
+ "not-found",
127
+ /** The path is outside the conversation's workspace, lexically or through a symlink. */
128
+ "outside",
129
+ "too-large",
130
+ "not-a-file",
131
+ "denied",
132
+ "aborted",
133
+ "failed",
134
+ ] as const;
135
+ export type RunnerErrorCode = (typeof RUNNER_ERROR_CODES)[number];
136
+
137
+ export type ServerFrame =
138
+ | { type: "welcome"; version: number }
139
+ | { type: "ok"; id: number; result: unknown }
140
+ | { type: "error"; id?: number; code: RunnerErrorCode; message: string }
141
+ | { type: "data"; id: number; data: string }
142
+ | {
143
+ type: "exit";
144
+ id: number;
145
+ /** The exit code, 128 + the signal's number for a killed command. */
146
+ code: number;
147
+ /** The output passed `maxOutputBytes`; the command was killed there. */
148
+ cut: boolean;
149
+ timedOut: boolean;
150
+ };
151
+
152
+ /** One frame as a line; refuses a frame over the limit. */
153
+ export function encodeFrame(frame: object, limit = MAX_FRAME_BYTES): string {
154
+ const text = JSON.stringify(frame);
155
+ if (Buffer.byteLength(text) > limit)
156
+ throw new FrameError(`a frame is over ${limit} bytes`);
157
+ return `${text}\n`;
158
+ }
159
+
160
+ const NEWLINE = 0x0a;
161
+
162
+ /** Splits a byte stream into frames, refusing a line over the limit as soon as it passes it. */
163
+ export class FrameReader {
164
+ #parts: Uint8Array[] = [];
165
+ #size = 0;
166
+ readonly #decoder = new TextDecoder("utf-8", { fatal: true });
167
+
168
+ constructor(readonly limit = MAX_FRAME_BYTES) {}
169
+
170
+ /** The complete frames `chunk` finishes, as parsed JSON; throws `FrameError` on a bad stream. */
171
+ push(chunk: Uint8Array): unknown[] {
172
+ const frames: unknown[] = [];
173
+ let start = 0;
174
+ for (;;) {
175
+ const end = chunk.indexOf(NEWLINE, start);
176
+ if (end < 0) break;
177
+ const piece = chunk.subarray(start, end);
178
+ this.#take(piece);
179
+ const line = this.#join();
180
+ if (line.length > 0) frames.push(this.#parse(line));
181
+ start = end + 1;
182
+ }
183
+ if (start < chunk.length) this.#take(chunk.subarray(start));
184
+ return frames;
185
+ }
186
+
187
+ #take(piece: Uint8Array): void {
188
+ this.#size += piece.length;
189
+ if (this.#size > this.limit) {
190
+ this.#parts = [];
191
+ throw new FrameError(`a frame is over ${this.limit} bytes`);
192
+ }
193
+ if (piece.length > 0) this.#parts.push(piece.slice());
194
+ }
195
+
196
+ #join(): Uint8Array {
197
+ const line =
198
+ this.#parts.length === 1
199
+ ? (this.#parts[0] as Uint8Array)
200
+ : Buffer.concat(this.#parts);
201
+ this.#parts = [];
202
+ this.#size = 0;
203
+ return line;
204
+ }
205
+
206
+ #parse(line: Uint8Array): unknown {
207
+ try {
208
+ return JSON.parse(this.#decoder.decode(line));
209
+ } catch {
210
+ throw new FrameError("a frame is not a JSON line");
211
+ }
212
+ }
213
+ }
214
+
215
+ const CONVERSATION = /^[A-Za-z0-9_-]{1,200}$/;
216
+
217
+ /** A conversation id the runner accepts: a channel segment, so never a dot name or a path. */
218
+ export function isConversationId(value: string): boolean {
219
+ return CONVERSATION.test(value);
220
+ }
221
+
222
+ export function workspaceDirOf(
223
+ conversation: string,
224
+ root: string = RUNNER_WORKSPACES,
225
+ ): string {
226
+ return posix.join(root, conversation);
227
+ }
228
+
229
+ export function scratchDirOf(
230
+ conversation: string,
231
+ root: string = RUNNER_WORKSPACES,
232
+ ): string {
233
+ return posix.join(workspaceDirOf(conversation, root), ".tmp");
234
+ }
235
+
236
+ /**
237
+ * `path` normalized when it is absolute and lexically `dir` or inside it, else undefined. Symlinks
238
+ * are not looked at; the runner checks where they lead separately.
239
+ */
240
+ export function confinedPath(dir: string, path: string): string | undefined {
241
+ if (path.includes("\u0000") || !isAbsolute(path)) return undefined;
242
+ const normal = posix.normalize(path);
243
+ const base = posix.normalize(dir);
244
+ return normal === base || normal.startsWith(`${base}/`) ? normal : undefined;
245
+ }
246
+
247
+ type Fields = Record<string, unknown>;
248
+
249
+ const fail = (message: string): never => {
250
+ throw new FrameError(message);
251
+ };
252
+
253
+ function record(value: unknown): Fields {
254
+ if (typeof value !== "object" || value === null || Array.isArray(value))
255
+ fail("a frame is not an object");
256
+ return value as Fields;
257
+ }
258
+
259
+ function only(frame: Fields, keys: readonly string[]): void {
260
+ for (const key of Object.keys(frame))
261
+ if (!keys.includes(key)) fail(`a ${frame.type} frame has no ${key}`);
262
+ }
263
+
264
+ function text(frame: Fields, key: string): string {
265
+ const value = frame[key];
266
+ return typeof value === "string"
267
+ ? value
268
+ : fail(`${frame.type}.${key} is not a string`);
269
+ }
270
+
271
+ function count(frame: Fields, key: string, min: number, max: number): number {
272
+ const value = frame[key];
273
+ return typeof value === "number" &&
274
+ Number.isInteger(value) &&
275
+ value >= min &&
276
+ value <= max
277
+ ? value
278
+ : fail(`${frame.type}.${key} is not a whole number in ${min}..${max}`);
279
+ }
280
+
281
+ function bool(frame: Fields, key: string): boolean {
282
+ const value = frame[key];
283
+ return typeof value === "boolean"
284
+ ? value
285
+ : fail(`${frame.type}.${key} is not true or false`);
286
+ }
287
+
288
+ const BASE64 = /^[A-Za-z0-9+/]*={0,2}$/;
289
+
290
+ function base64(frame: Fields, key: string): string {
291
+ const value = text(frame, key);
292
+ return value.length % 4 === 0 && BASE64.test(value)
293
+ ? value
294
+ : fail(`${frame.type}.${key} is not base64`);
295
+ }
296
+
297
+ function conversationOf(frame: Fields): string {
298
+ const value = text(frame, "conversation");
299
+ return isConversationId(value)
300
+ ? value
301
+ : fail(`${frame.type}.conversation is not a conversation id`);
302
+ }
303
+
304
+ const ID = (frame: Fields) => count(frame, "id", 1, Number.MAX_SAFE_INTEGER);
305
+
306
+ /** A client frame checked field by field; throws `FrameError` for anything else. */
307
+ export function parseClientFrame(value: unknown): ClientFrame {
308
+ const frame = record(value);
309
+ const file = (extra: readonly string[] = []) =>
310
+ only(frame, ["type", "id", "conversation", "path", ...extra]);
311
+ switch (frame.type) {
312
+ case "hello":
313
+ only(frame, ["type", "version", "token"]);
314
+ return frame.token === undefined
315
+ ? { type: "hello", version: count(frame, "version", 1, 1_000_000) }
316
+ : {
317
+ type: "hello",
318
+ version: count(frame, "version", 1, 1_000_000),
319
+ token: text(frame, "token"),
320
+ };
321
+ case "abort":
322
+ only(frame, ["type", "id"]);
323
+ return { type: "abort", id: ID(frame) };
324
+ case "ping":
325
+ only(frame, ["type", "id"]);
326
+ return { type: "ping", id: ID(frame) };
327
+ case "sweep":
328
+ only(frame, ["type", "id", "keepDays"]);
329
+ return {
330
+ type: "sweep",
331
+ id: ID(frame),
332
+ keepDays: count(frame, "keepDays", 0, 100_000),
333
+ };
334
+ case "remove-workspace":
335
+ only(frame, ["type", "id", "conversation"]);
336
+ return {
337
+ type: "remove-workspace",
338
+ id: ID(frame),
339
+ conversation: conversationOf(frame),
340
+ };
341
+ case "exec":
342
+ only(frame, [
343
+ "type",
344
+ "id",
345
+ "conversation",
346
+ "command",
347
+ "cwd",
348
+ "timeoutMs",
349
+ "maxOutputBytes",
350
+ ]);
351
+ return {
352
+ type: "exec",
353
+ id: ID(frame),
354
+ conversation: conversationOf(frame),
355
+ command: text(frame, "command"),
356
+ cwd: text(frame, "cwd"),
357
+ timeoutMs: count(
358
+ frame,
359
+ "timeoutMs",
360
+ 1,
361
+ RUNNER_CEILINGS.commandTimeoutMs,
362
+ ),
363
+ maxOutputBytes: count(
364
+ frame,
365
+ "maxOutputBytes",
366
+ 1,
367
+ RUNNER_CEILINGS.maxOutputBytes,
368
+ ),
369
+ };
370
+ case "read":
371
+ file(["maxBytes"]);
372
+ return {
373
+ type: "read",
374
+ id: ID(frame),
375
+ conversation: conversationOf(frame),
376
+ path: text(frame, "path"),
377
+ maxBytes: count(frame, "maxBytes", 1, RUNNER_CEILINGS.maxFileBytes),
378
+ };
379
+ case "write":
380
+ file(["data", "maxBytes"]);
381
+ return {
382
+ type: "write",
383
+ id: ID(frame),
384
+ conversation: conversationOf(frame),
385
+ path: text(frame, "path"),
386
+ data: base64(frame, "data"),
387
+ maxBytes: count(frame, "maxBytes", 1, RUNNER_CEILINGS.maxFileBytes),
388
+ };
389
+ case "access": {
390
+ file(["mode"]);
391
+ const mode = frame.mode;
392
+ if (mode !== "r" && mode !== "rw") fail("access.mode is not r or rw");
393
+ return {
394
+ type: "access",
395
+ id: ID(frame),
396
+ conversation: conversationOf(frame),
397
+ path: text(frame, "path"),
398
+ mode: mode as AccessMode,
399
+ };
400
+ }
401
+ case "mkdir":
402
+ case "stat":
403
+ case "detect-image":
404
+ file();
405
+ return {
406
+ type: frame.type,
407
+ id: ID(frame),
408
+ conversation: conversationOf(frame),
409
+ path: text(frame, "path"),
410
+ };
411
+ default:
412
+ return fail(`no frame type ${String(frame.type)}`);
413
+ }
414
+ }
415
+
416
+ /** A server frame checked by shape; an `ok` result is checked by the request that awaits it. */
417
+ export function parseServerFrame(value: unknown): ServerFrame {
418
+ const frame = record(value);
419
+ switch (frame.type) {
420
+ case "welcome":
421
+ only(frame, ["type", "version"]);
422
+ return {
423
+ type: "welcome",
424
+ version: count(frame, "version", 1, 1_000_000),
425
+ };
426
+ case "ok":
427
+ only(frame, ["type", "id", "result"]);
428
+ return { type: "ok", id: ID(frame), result: frame.result };
429
+ case "error": {
430
+ only(frame, ["type", "id", "code", "message"]);
431
+ const code = text(frame, "code");
432
+ if (!(RUNNER_ERROR_CODES as readonly string[]).includes(code))
433
+ fail(`no error code ${code}`);
434
+ const base = {
435
+ type: "error" as const,
436
+ code: code as RunnerErrorCode,
437
+ message: text(frame, "message"),
438
+ };
439
+ return frame.id === undefined ? base : { ...base, id: ID(frame) };
440
+ }
441
+ case "data":
442
+ only(frame, ["type", "id", "data"]);
443
+ return { type: "data", id: ID(frame), data: base64(frame, "data") };
444
+ case "exit":
445
+ only(frame, ["type", "id", "code", "cut", "timedOut"]);
446
+ return {
447
+ type: "exit",
448
+ id: ID(frame),
449
+ code: count(frame, "code", 0, 255),
450
+ cut: bool(frame, "cut"),
451
+ timedOut: bool(frame, "timedOut"),
452
+ };
453
+ default:
454
+ return fail(`no frame type ${String(frame.type)}`);
455
+ }
456
+ }