pi-roundtable 0.53.5 → 0.54.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.
Files changed (97) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +14 -2
  3. package/README.zh-TW.md +14 -2
  4. package/docs/plugins.md +201 -8
  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/attachments/attachment-expiry.ts +12 -0
  18. package/src/core/attachments/attachment-store.ts +20 -5
  19. package/src/core/attachments/model-images.ts +3 -2
  20. package/src/core/builtin/agent-server.ts +8 -0
  21. package/src/core/builtin/polls.ts +67 -0
  22. package/src/core/builtin/runner.ts +139 -0
  23. package/src/core/config/config-schema.ts +4 -0
  24. package/src/core/config/config.ts +49 -185
  25. package/src/core/config/preset.ts +51 -0
  26. package/src/core/config/resolve-config.ts +175 -0
  27. package/src/core/config/runner-config.ts +80 -0
  28. package/src/core/contract/attachments.ts +2 -0
  29. package/src/core/contract/polls.ts +71 -0
  30. package/src/core/contract/runtime.ts +12 -0
  31. package/src/core/contract/surface.ts +13 -0
  32. package/src/core/discord/discord-polls.ts +156 -0
  33. package/src/core/discord/discord-replies.ts +55 -0
  34. package/src/core/discord/discord-surface.ts +13 -43
  35. package/src/core/domain/client.ts +6 -2
  36. package/src/core/domain/errors.ts +5 -0
  37. package/src/core/domain/sessions.ts +8 -1
  38. package/src/core/host/define-roundtable.ts +8 -0
  39. package/src/core/host/host-registration.ts +7 -0
  40. package/src/core/host/host.ts +8 -0
  41. package/src/core/i18n/schedules.ts +2 -0
  42. package/src/core/modules/polls/poll-result-turns.ts +124 -0
  43. package/src/core/modules/polls/poll-rules.ts +108 -0
  44. package/src/core/modules/polls/poll-service.ts +477 -0
  45. package/src/core/modules/polls/poll-store.ts +336 -0
  46. package/src/core/modules/polls/poll-tools.ts +118 -0
  47. package/src/core/modules/polls/poll-turn-text.ts +60 -0
  48. package/src/core/plugin-api/define.ts +9 -2
  49. package/src/core/plugin-api/holds.ts +2 -0
  50. package/src/core/plugin-api/plugin.ts +8 -0
  51. package/src/core/runner/protocol.ts +456 -0
  52. package/src/core/runner/runner-client.ts +450 -0
  53. package/src/core/runner/runner-drivers.ts +356 -0
  54. package/src/core/runner/runner-files.ts +102 -0
  55. package/src/core/runner/runner-holds.ts +76 -0
  56. package/src/core/runner/runner-port.ts +77 -0
  57. package/src/core/runner/runner-probe.ts +53 -0
  58. package/src/core/runner/runner-prompt.ts +36 -0
  59. package/src/core/runner/runner-session.ts +131 -0
  60. package/src/core/runner/runner-tiers.ts +20 -0
  61. package/src/core/runner/runner-tools.ts +325 -0
  62. package/src/core/runner/settings.ts +108 -0
  63. package/src/core/runtime/conversation-sessions.ts +73 -15
  64. package/src/core/runtime/extensions/approval-card.ts +29 -2
  65. package/src/core/runtime/extensions/confirmation-gate.ts +24 -63
  66. package/src/core/runtime/extensions/gate-card.ts +105 -0
  67. package/src/core/runtime/extensions/yolo.ts +17 -3
  68. package/src/core/runtime/pi-agent-runtime.ts +37 -68
  69. package/src/core/runtime/runtime-plugin.ts +9 -0
  70. package/src/core/runtime/runtime-types.ts +29 -1
  71. package/src/core/runtime/session-factory.ts +59 -62
  72. package/src/core/runtime/session-shell.ts +43 -0
  73. package/src/core/runtime/session-tool-names.ts +78 -0
  74. package/src/core/runtime/turn-selection.ts +97 -0
  75. package/src/core/shared/attachment-reader.ts +10 -1
  76. package/src/core/shared/generate-image-tool.ts +4 -4
  77. package/src/core/shared/poll-target.ts +14 -0
  78. package/src/core/shared/workspace-files.ts +42 -4
  79. package/src/core/testing/fake-polls.ts +131 -0
  80. package/src/core/testing/fake-runner.ts +146 -0
  81. package/src/core/testing/surface-contract-approvals.ts +215 -0
  82. package/src/core/testing/surface-contract-polls.ts +142 -0
  83. package/src/core/testing/surface-contract.ts +32 -208
  84. package/src/core/tool-set.snapshot.json +8 -0
  85. package/src/index.ts +17 -0
  86. package/src/kit/index.ts +6 -0
  87. package/src/kit/polls.ts +8 -0
  88. package/src/runner-server/exec.ts +174 -0
  89. package/src/runner-server/file-helper.ts +208 -0
  90. package/src/runner-server/files.ts +93 -0
  91. package/src/runner-server/host.ts +65 -0
  92. package/src/runner-server/main.ts +89 -0
  93. package/src/runner-server/server.ts +318 -0
  94. package/src/runner-server/shared-dirs.ts +45 -0
  95. package/src/runner-server/state.ts +293 -0
  96. package/src/runner-server/transports.ts +187 -0
  97. package/src/testing.ts +7 -0
@@ -0,0 +1,187 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import {
3
+ chmod,
4
+ chown,
5
+ lstat,
6
+ mkdir,
7
+ readFile,
8
+ rename,
9
+ rm,
10
+ writeFile,
11
+ } from "node:fs/promises";
12
+ import { connect, createServer, type Server, type Socket } from "node:net";
13
+ import { join } from "node:path";
14
+ import type { Duplex, Readable, Writable } from "node:stream";
15
+ import {
16
+ encodeFrame,
17
+ FrameError,
18
+ FrameReader,
19
+ MAX_FRAME_BYTES,
20
+ parseClientFrame,
21
+ RUNNER_TOKEN_FILE,
22
+ } from "../core/runner/protocol.ts";
23
+ import type { RunnerServer } from "./server.ts";
24
+
25
+ /**
26
+ * Checks the socket directory, making it when missing, and returns its owner: the one account
27
+ * (the bot's, in a sidecar) that may reach the socket and read the token. A directory any other
28
+ * account can enter is refused, so conversation uids never reach the socket.
29
+ */
30
+ export async function prepareSocketDir(dir: string): Promise<number> {
31
+ let info = await lstat(dir).catch(() => undefined);
32
+ if (info === undefined) {
33
+ await mkdir(dir, { recursive: true, mode: 0o700 });
34
+ await chmod(dir, 0o700);
35
+ info = await lstat(dir);
36
+ }
37
+ if (!info.isDirectory()) throw new Error(`${dir} is not a directory`);
38
+ if ((info.mode & 0o077) !== 0)
39
+ throw new Error(
40
+ `${dir} is mode ${(info.mode & 0o777).toString(8)}; the runner's socket directory must be 0700 (for a tmpfs, mount it with mode=0700)`,
41
+ );
42
+ return info.uid;
43
+ }
44
+
45
+ /** Writes a fresh random token for this boot to `<dir>/token`, mode 0600, owned by `owner`. */
46
+ export async function writeBootToken(
47
+ dir: string,
48
+ owner: number,
49
+ ): Promise<string> {
50
+ const token = randomBytes(32).toString("hex");
51
+ const file = join(dir, RUNNER_TOKEN_FILE);
52
+ const temp = `${file}.tmp`;
53
+ await rm(temp, { force: true });
54
+ await writeFile(temp, token, { mode: 0o600, flag: "wx" });
55
+ await chown(temp, owner, -1);
56
+ await rename(temp, file);
57
+ return token;
58
+ }
59
+
60
+ /** Serves one client stream: frames in, frames out; a broken stream ends the connection. */
61
+ export function serveStream(server: RunnerServer, stream: Duplex): void {
62
+ const reader = new FrameReader();
63
+ let ended = false;
64
+ const conn = server.connect({
65
+ send: (frame) => {
66
+ if (ended) return;
67
+ try {
68
+ stream.write(encodeFrame(frame));
69
+ } catch (error) {
70
+ stream.write(
71
+ encodeFrame({
72
+ type: "error",
73
+ ...("id" in frame ? { id: frame.id } : {}),
74
+ code: "failed",
75
+ message: (error as Error).message,
76
+ }),
77
+ );
78
+ }
79
+ },
80
+ close: () => {
81
+ ended = true;
82
+ stream.end();
83
+ },
84
+ });
85
+ stream.on("data", (chunk: Buffer) => {
86
+ if (ended) return;
87
+ try {
88
+ for (const value of reader.push(chunk)) conn.receive(value);
89
+ } catch (error) {
90
+ const message =
91
+ error instanceof FrameError ? error.message : String(error);
92
+ stream.write(encodeFrame({ type: "error", code: "bad-frame", message }));
93
+ ended = true;
94
+ stream.end();
95
+ conn.closed();
96
+ }
97
+ });
98
+ stream.on("error", () => undefined);
99
+ stream.on("close", () => {
100
+ ended = true;
101
+ conn.closed();
102
+ });
103
+ }
104
+
105
+ /** Listens on the unix socket `path`, mode 0600 and owned by `owner`, replacing a stale one. */
106
+ export async function listenSocket(
107
+ server: RunnerServer,
108
+ path: string,
109
+ owner: number,
110
+ ): Promise<Server> {
111
+ const stale = await lstat(path).catch(() => undefined);
112
+ if (stale?.isSocket()) await rm(path);
113
+ else if (stale !== undefined)
114
+ throw new Error(`${path} exists and is not a socket`);
115
+ const listener = createServer((socket: Socket) =>
116
+ serveStream(server, socket),
117
+ );
118
+ await new Promise<void>((resolve, reject) => {
119
+ listener.once("error", reject);
120
+ listener.listen(path, () => {
121
+ listener.off("error", reject);
122
+ resolve();
123
+ });
124
+ });
125
+ await chmod(path, 0o600);
126
+ await chown(path, owner, -1);
127
+ return listener;
128
+ }
129
+
130
+ /**
131
+ * `serve --stdio`: relays one client on stdin and stdout (a `docker exec -i` from the host) to the
132
+ * running server's socket. The relay adds the boot token to the client's hello, so whoever may
133
+ * exec into the container needs no token: Docker access is the authentication.
134
+ */
135
+ export async function relayStdio(options: {
136
+ socket: string;
137
+ tokenFile: string;
138
+ input: Readable;
139
+ output: Writable;
140
+ }): Promise<void> {
141
+ const token = (await readFile(options.tokenFile, "utf8")).trim();
142
+ const conn = connect(options.socket);
143
+ await new Promise<void>((resolve, reject) => {
144
+ conn.once("connect", resolve);
145
+ conn.once("error", reject);
146
+ });
147
+ const done = new Promise<void>((resolve) =>
148
+ conn.once("close", () => resolve()),
149
+ );
150
+ conn.on("error", () => undefined);
151
+ conn.pipe(options.output);
152
+ let first: Buffer | undefined = Buffer.alloc(0);
153
+ const forward = (chunk: Buffer) => {
154
+ if (first === undefined) {
155
+ conn.write(chunk);
156
+ return;
157
+ }
158
+ first = Buffer.concat([first, chunk]);
159
+ const end = first.indexOf(0x0a);
160
+ if (end < 0) {
161
+ if (first.length > MAX_FRAME_BYTES) conn.destroy();
162
+ return;
163
+ }
164
+ const line = first.subarray(0, end).toString();
165
+ const rest = first.subarray(end + 1);
166
+ first = undefined;
167
+ let hello: string;
168
+ try {
169
+ const frame = parseClientFrame(JSON.parse(line));
170
+ hello =
171
+ frame.type === "hello" ? encodeFrame({ ...frame, token }) : `${line}\n`;
172
+ } catch {
173
+ // Not a hello: passed on as it came, and the server refuses it.
174
+ hello = `${line}\n`;
175
+ }
176
+ conn.write(hello);
177
+ if (rest.length > 0) conn.write(rest);
178
+ };
179
+ options.input.on("data", forward);
180
+ options.input.once("end", () => conn.end());
181
+ options.input.once("error", () => conn.destroy());
182
+ await done;
183
+ options.input.off("data", forward);
184
+ // The last frames are flushed before the caller may exit.
185
+ if (!options.output.writableFinished && !options.output.destroyed)
186
+ await new Promise<void>((resolve) => options.output.end(() => resolve()));
187
+ }
package/src/testing.ts CHANGED
@@ -90,6 +90,11 @@ export type {
90
90
  FakeDiscordAttachment,
91
91
  } from "./core/testing/fake-discord.ts";
92
92
  export { fakeDiscord } from "./core/testing/fake-discord.ts";
93
+ export type {
94
+ FakePoll,
95
+ FakeSurfacePolls,
96
+ } from "./core/testing/fake-polls.ts";
97
+ export { fakeSurfacePolls } from "./core/testing/fake-polls.ts";
93
98
  export type {
94
99
  FakeDiscordPoster,
95
100
  FakeForum,
@@ -137,6 +142,7 @@ export { recordingLogger } from "./core/testing/recording-logger.ts";
137
142
  export type { ServicePair } from "./core/testing/service-pair.ts";
138
143
  export { servicePair } from "./core/testing/service-pair.ts";
139
144
  export type {
145
+ PollsContractSubject,
140
146
  SurfaceContractAnswerer,
141
147
  SurfaceContractFailure,
142
148
  SurfaceContractSubject,
@@ -145,6 +151,7 @@ export type {
145
151
  export {
146
152
  checkSurfaceContract,
147
153
  describeBlocksContract,
154
+ describePollsContract,
148
155
  describeSurfaceContract,
149
156
  } from "./core/testing/surface-contract.ts";
150
157
  export type { TestHost, TestHostOptions } from "./core/testing/test-host.ts";