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.
- package/CHANGELOG.md +19 -0
- package/README.md +14 -2
- package/README.zh-TW.md +14 -2
- package/docs/plugins.md +201 -8
- package/package.json +3 -1
- package/runner/Dockerfile +46 -0
- package/runner/Dockerfile.dockerignore +5 -0
- package/src/cli/checks/images.ts +3 -2
- package/src/cli/checks/preset.ts +20 -0
- package/src/cli/checks/runner.ts +45 -0
- package/src/cli/doctor.ts +7 -1
- package/src/core/agents/agent-claim.ts +7 -4
- package/src/core/agents/agent-prompt.ts +11 -2
- package/src/core/agents/helper-workspace.ts +72 -0
- package/src/core/agents/team-options.ts +2 -0
- package/src/core/agents/team-prompt.ts +12 -1
- package/src/core/attachments/attachment-expiry.ts +12 -0
- package/src/core/attachments/attachment-store.ts +20 -5
- package/src/core/attachments/model-images.ts +3 -2
- package/src/core/builtin/agent-server.ts +8 -0
- package/src/core/builtin/polls.ts +67 -0
- package/src/core/builtin/runner.ts +139 -0
- package/src/core/config/config-schema.ts +4 -0
- package/src/core/config/config.ts +49 -185
- package/src/core/config/preset.ts +51 -0
- package/src/core/config/resolve-config.ts +175 -0
- package/src/core/config/runner-config.ts +80 -0
- package/src/core/contract/attachments.ts +2 -0
- package/src/core/contract/polls.ts +71 -0
- package/src/core/contract/runtime.ts +12 -0
- package/src/core/contract/surface.ts +13 -0
- package/src/core/discord/discord-polls.ts +156 -0
- package/src/core/discord/discord-replies.ts +55 -0
- package/src/core/discord/discord-surface.ts +13 -43
- package/src/core/domain/client.ts +6 -2
- package/src/core/domain/errors.ts +5 -0
- package/src/core/domain/sessions.ts +8 -1
- package/src/core/host/define-roundtable.ts +8 -0
- package/src/core/host/host-registration.ts +7 -0
- package/src/core/host/host.ts +8 -0
- package/src/core/i18n/schedules.ts +2 -0
- package/src/core/modules/polls/poll-result-turns.ts +124 -0
- package/src/core/modules/polls/poll-rules.ts +108 -0
- package/src/core/modules/polls/poll-service.ts +477 -0
- package/src/core/modules/polls/poll-store.ts +336 -0
- package/src/core/modules/polls/poll-tools.ts +118 -0
- package/src/core/modules/polls/poll-turn-text.ts +60 -0
- package/src/core/plugin-api/define.ts +9 -2
- package/src/core/plugin-api/holds.ts +2 -0
- package/src/core/plugin-api/plugin.ts +8 -0
- package/src/core/runner/protocol.ts +456 -0
- package/src/core/runner/runner-client.ts +450 -0
- package/src/core/runner/runner-drivers.ts +356 -0
- package/src/core/runner/runner-files.ts +102 -0
- package/src/core/runner/runner-holds.ts +76 -0
- package/src/core/runner/runner-port.ts +77 -0
- package/src/core/runner/runner-probe.ts +53 -0
- package/src/core/runner/runner-prompt.ts +36 -0
- package/src/core/runner/runner-session.ts +131 -0
- package/src/core/runner/runner-tiers.ts +20 -0
- package/src/core/runner/runner-tools.ts +325 -0
- package/src/core/runner/settings.ts +108 -0
- package/src/core/runtime/conversation-sessions.ts +73 -15
- package/src/core/runtime/extensions/approval-card.ts +29 -2
- package/src/core/runtime/extensions/confirmation-gate.ts +24 -63
- package/src/core/runtime/extensions/gate-card.ts +105 -0
- package/src/core/runtime/extensions/yolo.ts +17 -3
- package/src/core/runtime/pi-agent-runtime.ts +37 -68
- package/src/core/runtime/runtime-plugin.ts +9 -0
- package/src/core/runtime/runtime-types.ts +29 -1
- package/src/core/runtime/session-factory.ts +59 -62
- package/src/core/runtime/session-shell.ts +43 -0
- package/src/core/runtime/session-tool-names.ts +78 -0
- package/src/core/runtime/turn-selection.ts +97 -0
- package/src/core/shared/attachment-reader.ts +10 -1
- package/src/core/shared/generate-image-tool.ts +4 -4
- package/src/core/shared/poll-target.ts +14 -0
- package/src/core/shared/workspace-files.ts +42 -4
- package/src/core/testing/fake-polls.ts +131 -0
- package/src/core/testing/fake-runner.ts +146 -0
- package/src/core/testing/surface-contract-approvals.ts +215 -0
- package/src/core/testing/surface-contract-polls.ts +142 -0
- package/src/core/testing/surface-contract.ts +32 -208
- package/src/core/tool-set.snapshot.json +8 -0
- package/src/index.ts +17 -0
- package/src/kit/index.ts +6 -0
- package/src/kit/polls.ts +8 -0
- package/src/runner-server/exec.ts +174 -0
- package/src/runner-server/file-helper.ts +208 -0
- package/src/runner-server/files.ts +93 -0
- package/src/runner-server/host.ts +65 -0
- package/src/runner-server/main.ts +89 -0
- package/src/runner-server/server.ts +318 -0
- package/src/runner-server/shared-dirs.ts +45 -0
- package/src/runner-server/state.ts +293 -0
- package/src/runner-server/transports.ts +187 -0
- package/src/testing.ts +7 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type { AgentSessions } from "../contract/runtime.ts";
|
|
2
|
+
import type { ChannelKey } from "../domain/conversation.ts";
|
|
3
|
+
import type { AgentTurnScope } from "../domain/sessions.ts";
|
|
4
|
+
import type { Logger } from "../kernel/log.ts";
|
|
5
|
+
import type { HoldContext } from "../plugin-api/holds.ts";
|
|
6
|
+
import type { ToolTiers } from "../runtime/tool-tiers.ts";
|
|
7
|
+
import type { WorkspaceRoots } from "../shared/workspace-files.ts";
|
|
8
|
+
import { RUNNER_WORKSPACES } from "./protocol.ts";
|
|
9
|
+
import { runnerWorkspaceFiles } from "./runner-files.ts";
|
|
10
|
+
import { runnerHoldContext } from "./runner-holds.ts";
|
|
11
|
+
import type { RunnerPort } from "./runner-port.ts";
|
|
12
|
+
import { runnerTiers } from "./runner-tiers.ts";
|
|
13
|
+
import type { RunnerBinding } from "./runner-tools.ts";
|
|
14
|
+
import {
|
|
15
|
+
type RunnerSettings,
|
|
16
|
+
runnerConversationId,
|
|
17
|
+
runnerWorkspace,
|
|
18
|
+
} from "./settings.ts";
|
|
19
|
+
|
|
20
|
+
/** The runner as the runtime's sessions use it, when the host configures one. */
|
|
21
|
+
export interface RunnerSessions {
|
|
22
|
+
settings: RunnerSettings;
|
|
23
|
+
/** The runner's client, read when a session is built. */
|
|
24
|
+
port(): RunnerPort;
|
|
25
|
+
/** Where the runner keeps the workspaces; only a test names another place. */
|
|
26
|
+
root?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Whether the runner serves a session: every session but the owners' own, which keep the host's
|
|
31
|
+
* shell: the owner's conversations (kind `"owner"`), and an agent's conversation only the owners
|
|
32
|
+
* reach (`ownersOnly`, such as its room on the web app). Not an agent's on a chat-only host, which
|
|
33
|
+
* has no shell at all.
|
|
34
|
+
*/
|
|
35
|
+
export function runnerServes(
|
|
36
|
+
session: { agent: boolean; kind: string; ownersOnly?: boolean },
|
|
37
|
+
chatOnly: boolean,
|
|
38
|
+
): boolean {
|
|
39
|
+
if (!session.agent) return session.kind !== "owner";
|
|
40
|
+
return !chatOnly && session.ownersOnly !== true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Where the runner runs the tools of a session of `kind`, an agent's or not, whose conversation's
|
|
45
|
+
* home is `home`; undefined without a runner or for a session it does not serve. An agent's turns
|
|
46
|
+
* work in the home `agents.runnerHome` names (a temp knight's origin's), else in `home`; an agent
|
|
47
|
+
* conversation `agents.ownersOnly` names is the host's.
|
|
48
|
+
*/
|
|
49
|
+
export function runnerPlace(
|
|
50
|
+
runner: RunnerSessions | undefined,
|
|
51
|
+
session: {
|
|
52
|
+
agent: AgentTurnScope | undefined;
|
|
53
|
+
kind: string;
|
|
54
|
+
home: ChannelKey;
|
|
55
|
+
},
|
|
56
|
+
chatOnly: boolean,
|
|
57
|
+
agents: Pick<AgentSessions, "runnerHome" | "ownersOnly"> | undefined,
|
|
58
|
+
): RunnerBinding | undefined {
|
|
59
|
+
const { agent, kind, home } = session;
|
|
60
|
+
const ownersOnly =
|
|
61
|
+
agent !== undefined && agents?.ownersOnly?.(agent) === true;
|
|
62
|
+
if (
|
|
63
|
+
!runner ||
|
|
64
|
+
!runnerServes({ agent: agent !== undefined, kind, ownersOnly }, chatOnly)
|
|
65
|
+
)
|
|
66
|
+
return undefined;
|
|
67
|
+
return runnerBinding(runner, (agent && agents?.runnerHome?.(agent)) ?? home);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The runner's place for the conversation whose home channel is `home`. */
|
|
71
|
+
export function runnerBinding(
|
|
72
|
+
runner: RunnerSessions,
|
|
73
|
+
home: ChannelKey,
|
|
74
|
+
): RunnerBinding {
|
|
75
|
+
const { maxFileBytes, maxOutputBytes, commandTimeoutMs, keepDays, minTier } =
|
|
76
|
+
runner.settings;
|
|
77
|
+
return {
|
|
78
|
+
...runnerWorkspace(home, runner.root ?? RUNNER_WORKSPACES),
|
|
79
|
+
port: runner.port(),
|
|
80
|
+
settings: {
|
|
81
|
+
maxFileBytes,
|
|
82
|
+
maxOutputBytes,
|
|
83
|
+
commandTimeoutMs,
|
|
84
|
+
keepDays,
|
|
85
|
+
minTier,
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The session's workspace roots in the runner, its files reached through the runner alone. */
|
|
91
|
+
export function runnerRoots(binding: RunnerBinding): Required<WorkspaceRoots> {
|
|
92
|
+
return {
|
|
93
|
+
workspace: binding.workspace,
|
|
94
|
+
scratchDir: binding.scratchDir,
|
|
95
|
+
files: runnerWorkspaceFiles(binding),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** What a session's gate needs to judge a call the runner would run. */
|
|
100
|
+
export function runnerGate(
|
|
101
|
+
binding: RunnerBinding,
|
|
102
|
+
tiers: ToolTiers,
|
|
103
|
+
): { context: HoldContext; tiers: ToolTiers } {
|
|
104
|
+
return {
|
|
105
|
+
context: runnerHoldContext(binding),
|
|
106
|
+
tiers: runnerTiers(tiers, binding.settings.minTier),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Removes the runner workspace of a conversation being deleted, as the conversation's retention
|
|
112
|
+
* does; a runner that cannot be reached is logged, and its own sweep removes the workspace once
|
|
113
|
+
* it has been idle for `keepDays`.
|
|
114
|
+
*/
|
|
115
|
+
export async function removeRunnerWorkspace(
|
|
116
|
+
options: { runner?: RunnerSessions; logger: Logger },
|
|
117
|
+
channel: ChannelKey,
|
|
118
|
+
): Promise<void> {
|
|
119
|
+
const { runner, logger } = options;
|
|
120
|
+
if (!runner) return;
|
|
121
|
+
const conversation = runnerConversationId(channel);
|
|
122
|
+
try {
|
|
123
|
+
if (await runner.port().removeWorkspace(conversation))
|
|
124
|
+
logger.info({ channel, conversation }, "runner workspace removed");
|
|
125
|
+
} catch (error) {
|
|
126
|
+
logger.warn(
|
|
127
|
+
{ channel, conversation, err: error },
|
|
128
|
+
"runner workspace not removed; the runner's sweep removes it once idle",
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Tier, tierAtLeast } from "../identity/speakers.ts";
|
|
2
|
+
import { SHELL_TOOLS } from "../modules/host-shell/shell-policy.ts";
|
|
3
|
+
import type { ToolTiers } from "../runtime/tool-tiers.ts";
|
|
4
|
+
|
|
5
|
+
const SHELL: ReadonlySet<string> = new Set(SHELL_TOOLS);
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The tiers of a session the runner serves: `bash`, `read`, `edit` and `write` need `minTier`
|
|
9
|
+
* (`runner.minTier`), whatever the host's tiers say of them on its own machine; every other tool
|
|
10
|
+
* needs what `base` says. The turn's tools and the held-action gate read the same view, so a
|
|
11
|
+
* speaker who may use a runner tool may also approve its held call.
|
|
12
|
+
*/
|
|
13
|
+
export function runnerTiers(base: ToolTiers, minTier: Tier): ToolTiers {
|
|
14
|
+
const tierOf = (tool: string): Tier =>
|
|
15
|
+
SHELL.has(tool) ? minTier : base.minTier(tool);
|
|
16
|
+
return {
|
|
17
|
+
minTier: tierOf,
|
|
18
|
+
allows: (tier, tool) => tierAtLeast(tier, tierOf(tool)),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { readFile, rm } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { basename, join, posix } from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
type BashOperations,
|
|
6
|
+
createBashToolDefinition,
|
|
7
|
+
createEditToolDefinition,
|
|
8
|
+
createReadToolDefinition,
|
|
9
|
+
createWriteToolDefinition,
|
|
10
|
+
type EditOperations,
|
|
11
|
+
type ReadOperations,
|
|
12
|
+
type ToolDefinition,
|
|
13
|
+
type WriteOperations,
|
|
14
|
+
} from "@earendil-works/pi-coding-agent";
|
|
15
|
+
import { ConfigError } from "../domain/errors.ts";
|
|
16
|
+
import { confinedPath } from "./protocol.ts";
|
|
17
|
+
import { RunnerError, type RunnerPort } from "./runner-port.ts";
|
|
18
|
+
import type { RunnerSettings, RunnerWorkspace } from "./settings.ts";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Pi's built-in tools the runner does not run: they would read the bot's own disk, or run its own
|
|
22
|
+
* shell. A session the runner serves registers none of them, and a selection naming one is
|
|
23
|
+
* refused, so it fails before the turn rather than quietly losing the tool.
|
|
24
|
+
*/
|
|
25
|
+
export const HOST_ONLY_TOOLS: readonly string[] = Object.freeze([
|
|
26
|
+
"grep",
|
|
27
|
+
"find",
|
|
28
|
+
"ls",
|
|
29
|
+
"powershell",
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
/** Refuses names among `HOST_ONLY_TOOLS` in the selection of a session the runner serves. */
|
|
33
|
+
export function refuseHostOnlyTools(
|
|
34
|
+
names: Iterable<string>,
|
|
35
|
+
where: string,
|
|
36
|
+
): void {
|
|
37
|
+
const found = [...new Set(names)].filter((name) =>
|
|
38
|
+
HOST_ONLY_TOOLS.includes(name),
|
|
39
|
+
);
|
|
40
|
+
if (found.length > 0)
|
|
41
|
+
throw new ConfigError(
|
|
42
|
+
`${where} names ${found.join(", ")}, which would run on the bot's own machine: in a conversation the runner (config runner) serves, only Pi's bash, read, edit and write run, in the runner. Take ${found.join(", ")} out of the selection; bash runs grep, find and ls in the runner.`,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** One conversation's place in the runner, with the runner that serves it and the host's limits. */
|
|
47
|
+
export interface RunnerBinding extends RunnerWorkspace {
|
|
48
|
+
port: RunnerPort;
|
|
49
|
+
settings: Pick<
|
|
50
|
+
RunnerSettings,
|
|
51
|
+
| "commandTimeoutMs"
|
|
52
|
+
| "maxOutputBytes"
|
|
53
|
+
| "maxFileBytes"
|
|
54
|
+
| "keepDays"
|
|
55
|
+
| "minTier"
|
|
56
|
+
>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Pi's bash operations against the runner: the command runs in the conversation's workspace, for
|
|
61
|
+
* at most the tool call's own timeout and never longer than `commandTimeoutMs`, with its output
|
|
62
|
+
* cut at `maxOutputBytes`. Whatever it started ends with it. A timeout and an abort end the call
|
|
63
|
+
* as Pi's own shell ends it.
|
|
64
|
+
*/
|
|
65
|
+
export function runnerBashOperations(binding: RunnerBinding): BashOperations {
|
|
66
|
+
const { port, conversation, workspace, settings } = binding;
|
|
67
|
+
return {
|
|
68
|
+
exec: async (command, cwd, { onData, signal, timeout }) => {
|
|
69
|
+
const limit = settings.commandTimeoutMs;
|
|
70
|
+
const asked =
|
|
71
|
+
timeout !== undefined && Number.isFinite(timeout) && timeout > 0
|
|
72
|
+
? Math.round(timeout * 1000)
|
|
73
|
+
: limit;
|
|
74
|
+
const timeoutMs = Math.max(1, Math.min(asked, limit));
|
|
75
|
+
let exit: Awaited<ReturnType<RunnerPort["exec"]>>;
|
|
76
|
+
try {
|
|
77
|
+
exit = await port.exec({
|
|
78
|
+
conversation,
|
|
79
|
+
command,
|
|
80
|
+
cwd: confinedPath(workspace, cwd) ?? workspace,
|
|
81
|
+
timeoutMs,
|
|
82
|
+
maxOutputBytes: settings.maxOutputBytes,
|
|
83
|
+
onData,
|
|
84
|
+
...(signal ? { signal } : {}),
|
|
85
|
+
});
|
|
86
|
+
} catch (error) {
|
|
87
|
+
if (
|
|
88
|
+
signal?.aborted ||
|
|
89
|
+
(error instanceof RunnerError && error.code === "aborted")
|
|
90
|
+
)
|
|
91
|
+
throw new Error("aborted");
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
if (signal?.aborted) throw new Error("aborted");
|
|
95
|
+
if (exit.timedOut)
|
|
96
|
+
throw new Error(`timeout:${Math.max(1, Math.round(timeoutMs / 1000))}`);
|
|
97
|
+
if (exit.cut)
|
|
98
|
+
onData(
|
|
99
|
+
Buffer.from(
|
|
100
|
+
`\n[the output passed ${settings.maxOutputBytes} bytes, and the command was stopped there]\n`,
|
|
101
|
+
),
|
|
102
|
+
);
|
|
103
|
+
return { exitCode: exit.code };
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Pi's read operations against the runner, each file at most `maxFileBytes`. */
|
|
109
|
+
export function runnerReadOperations(binding: RunnerBinding): ReadOperations {
|
|
110
|
+
const { port, conversation, settings } = binding;
|
|
111
|
+
return {
|
|
112
|
+
readFile: (path) => port.read(conversation, path, settings.maxFileBytes),
|
|
113
|
+
access: (path) => port.access(conversation, path, "r"),
|
|
114
|
+
detectImageMimeType: (path) => port.detectImage(conversation, path),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Pi's write operations against the runner. */
|
|
119
|
+
export function runnerWriteOperations(binding: RunnerBinding): WriteOperations {
|
|
120
|
+
const { port, conversation, settings } = binding;
|
|
121
|
+
return {
|
|
122
|
+
writeFile: (path, content) =>
|
|
123
|
+
port.write(
|
|
124
|
+
conversation,
|
|
125
|
+
path,
|
|
126
|
+
Buffer.from(content, "utf8"),
|
|
127
|
+
settings.maxFileBytes,
|
|
128
|
+
),
|
|
129
|
+
mkdir: (dir) => port.mkdir(conversation, dir),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Pi's edit operations against the runner. */
|
|
134
|
+
export function runnerEditOperations(binding: RunnerBinding): EditOperations {
|
|
135
|
+
const { port, conversation, settings } = binding;
|
|
136
|
+
return {
|
|
137
|
+
readFile: (path) => port.read(conversation, path, settings.maxFileBytes),
|
|
138
|
+
writeFile: (path, content) =>
|
|
139
|
+
port.write(
|
|
140
|
+
conversation,
|
|
141
|
+
path,
|
|
142
|
+
Buffer.from(content, "utf8"),
|
|
143
|
+
settings.maxFileBytes,
|
|
144
|
+
),
|
|
145
|
+
access: (path) => port.access(conversation, path, "rw"),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* The extension context with `cwd` at the workspace: Pi's tools resolve paths against the
|
|
151
|
+
* context's directory before their own, and the session's is the host's.
|
|
152
|
+
*/
|
|
153
|
+
function inWorkspace<T extends object>(context: T, workspace: string): T {
|
|
154
|
+
return new Proxy(context, {
|
|
155
|
+
get: (target, key) => {
|
|
156
|
+
if (key === "cwd") return workspace;
|
|
157
|
+
const value: unknown = Reflect.get(target, key, target);
|
|
158
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** A definition whose calls see the workspace as their working directory. */
|
|
164
|
+
function workingIn(
|
|
165
|
+
definition: ToolDefinition,
|
|
166
|
+
workspace: string,
|
|
167
|
+
): ToolDefinition {
|
|
168
|
+
return {
|
|
169
|
+
...definition,
|
|
170
|
+
execute: (id, params, signal, onUpdate, context) =>
|
|
171
|
+
definition.execute(
|
|
172
|
+
id,
|
|
173
|
+
params,
|
|
174
|
+
signal,
|
|
175
|
+
onUpdate,
|
|
176
|
+
context === undefined ? context : inWorkspace(context, workspace),
|
|
177
|
+
),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** A copy of `value` without its `key`, or with it set to `to`; anything but an object as it is. */
|
|
182
|
+
function withField<T>(value: T, key: string, to: string | undefined): T {
|
|
183
|
+
if (typeof value !== "object" || value === null || !(key in value))
|
|
184
|
+
return value;
|
|
185
|
+
const { [key]: _old, ...rest } = value as Record<string, unknown>;
|
|
186
|
+
// SAFETY: the same object but for one optional string field, which stays a string when kept.
|
|
187
|
+
return (to === undefined ? rest : { ...rest, [key]: to }) as T;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The file Pi's bash says, in a result's or an update's details, it kept a long output's whole
|
|
192
|
+
* text in: only a `pi-bash-*.log` directly in the host's temp directory. Never read from the
|
|
193
|
+
* output's text, which the command writes and could name any file.
|
|
194
|
+
*/
|
|
195
|
+
function keptOutputPath(details: unknown): string | undefined {
|
|
196
|
+
if (typeof details !== "object" || details === null) return undefined;
|
|
197
|
+
const path = (details as { fullOutputPath?: unknown }).fullOutputPath;
|
|
198
|
+
if (typeof path !== "string") return undefined;
|
|
199
|
+
const name = basename(path);
|
|
200
|
+
return /^pi-bash-[\w-]+\.log$/.test(name) && path === join(tmpdir(), name)
|
|
201
|
+
? path
|
|
202
|
+
: undefined;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Pi keeps the whole text of an output too long to show in a file on the host, which no tool of a
|
|
207
|
+
* runner session reaches, and names it to the model: the file is moved into the workspace's
|
|
208
|
+
* scratch dir under its own name, and the call names that path instead; when the workspace cannot
|
|
209
|
+
* take it, the call says the whole output was not kept. Nothing of it stays on the host.
|
|
210
|
+
*/
|
|
211
|
+
function keepingWholeOutput(
|
|
212
|
+
definition: ToolDefinition,
|
|
213
|
+
binding: RunnerBinding,
|
|
214
|
+
): ToolDefinition {
|
|
215
|
+
const { port, conversation, scratchDir, settings } = binding;
|
|
216
|
+
const keep = async (hostPath: string): Promise<string | undefined> => {
|
|
217
|
+
try {
|
|
218
|
+
const target = posix.join(scratchDir, basename(hostPath));
|
|
219
|
+
const data = await readFile(hostPath);
|
|
220
|
+
await port.write(conversation, target, data, settings.maxFileBytes);
|
|
221
|
+
return target;
|
|
222
|
+
} catch {
|
|
223
|
+
return undefined;
|
|
224
|
+
} finally {
|
|
225
|
+
await rm(hostPath, { force: true }).catch(() => undefined);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
const rewrite = (text: string, hostPath: string, kept?: string) =>
|
|
229
|
+
kept === undefined
|
|
230
|
+
? text
|
|
231
|
+
.replaceAll(
|
|
232
|
+
`Full output: ${hostPath}`,
|
|
233
|
+
"The whole output was not kept",
|
|
234
|
+
)
|
|
235
|
+
.replaceAll(hostPath, "(not kept)")
|
|
236
|
+
: text.replaceAll(hostPath, kept);
|
|
237
|
+
return {
|
|
238
|
+
...definition,
|
|
239
|
+
execute: async (id, params, signal, onUpdate, context) => {
|
|
240
|
+
// Pi names the file in every update once the output is long, and in the result.
|
|
241
|
+
let hostPath: string | undefined;
|
|
242
|
+
const seen = (details: unknown) => {
|
|
243
|
+
hostPath = keptOutputPath(details) ?? hostPath;
|
|
244
|
+
};
|
|
245
|
+
let result: Awaited<ReturnType<ToolDefinition["execute"]>>;
|
|
246
|
+
try {
|
|
247
|
+
result = await definition.execute(
|
|
248
|
+
id,
|
|
249
|
+
params,
|
|
250
|
+
signal,
|
|
251
|
+
(update) => {
|
|
252
|
+
seen(update.details);
|
|
253
|
+
onUpdate?.({
|
|
254
|
+
...update,
|
|
255
|
+
details: withField(update.details, "fullOutputPath", undefined),
|
|
256
|
+
});
|
|
257
|
+
},
|
|
258
|
+
context,
|
|
259
|
+
);
|
|
260
|
+
} catch (error) {
|
|
261
|
+
const path = hostPath;
|
|
262
|
+
if (error instanceof Error && path !== undefined)
|
|
263
|
+
error.message = rewrite(error.message, path, await keep(path));
|
|
264
|
+
throw error;
|
|
265
|
+
}
|
|
266
|
+
seen(result.details);
|
|
267
|
+
const path = hostPath;
|
|
268
|
+
if (path === undefined) return result;
|
|
269
|
+
const kept = await keep(path);
|
|
270
|
+
const structured = result.structuredContent;
|
|
271
|
+
return {
|
|
272
|
+
...result,
|
|
273
|
+
content: result.content.map((part) =>
|
|
274
|
+
part.type === "text"
|
|
275
|
+
? { ...part, text: rewrite(part.text, path, kept) }
|
|
276
|
+
: part,
|
|
277
|
+
),
|
|
278
|
+
details: withField(result.details, "fullOutputPath", kept),
|
|
279
|
+
...(structured === undefined
|
|
280
|
+
? {}
|
|
281
|
+
: {
|
|
282
|
+
structuredContent: withField(
|
|
283
|
+
structured,
|
|
284
|
+
"full_output_path",
|
|
285
|
+
kept,
|
|
286
|
+
),
|
|
287
|
+
}),
|
|
288
|
+
};
|
|
289
|
+
},
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* `bash`, `read`, `edit` and `write` as Pi defines them, run in the runner: they replace Pi's
|
|
295
|
+
* built-ins of the same names, so no call of a session the runner serves reaches the host's shell
|
|
296
|
+
* or files.
|
|
297
|
+
*/
|
|
298
|
+
export function runnerTools(binding: RunnerBinding): ToolDefinition[] {
|
|
299
|
+
const { workspace } = binding;
|
|
300
|
+
// SAFETY: Pi's definitions are ToolDefinitions typed by their own parameter schemas, which a session takes as any custom tool.
|
|
301
|
+
const definitions = [
|
|
302
|
+
createBashToolDefinition(workspace, {
|
|
303
|
+
operations: runnerBashOperations(binding),
|
|
304
|
+
// The runner's commands get none of the session's variables.
|
|
305
|
+
exposeSessionEnvironment: false,
|
|
306
|
+
}),
|
|
307
|
+
createReadToolDefinition(workspace, {
|
|
308
|
+
operations: runnerReadOperations(binding),
|
|
309
|
+
}),
|
|
310
|
+
createEditToolDefinition(workspace, {
|
|
311
|
+
operations: runnerEditOperations(binding),
|
|
312
|
+
}),
|
|
313
|
+
createWriteToolDefinition(workspace, {
|
|
314
|
+
operations: runnerWriteOperations(binding),
|
|
315
|
+
}),
|
|
316
|
+
] as unknown as ToolDefinition[];
|
|
317
|
+
return definitions.map((definition) =>
|
|
318
|
+
workingIn(
|
|
319
|
+
definition.name === "bash"
|
|
320
|
+
? keepingWholeOutput(definition, binding)
|
|
321
|
+
: definition,
|
|
322
|
+
workspace,
|
|
323
|
+
),
|
|
324
|
+
);
|
|
325
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import type { ChannelKey } from "../domain/conversation.ts";
|
|
3
|
+
import type { Tier } from "../identity/speakers.ts";
|
|
4
|
+
import {
|
|
5
|
+
isConversationId,
|
|
6
|
+
RUNNER_DEFAULTS,
|
|
7
|
+
RUNNER_WORKSPACES,
|
|
8
|
+
scratchDirOf,
|
|
9
|
+
workspaceDirOf,
|
|
10
|
+
} from "./protocol.ts";
|
|
11
|
+
|
|
12
|
+
/** How the host reaches its runner: a container it runs itself, or a sidecar's socket. */
|
|
13
|
+
export type RunnerDriverConfig =
|
|
14
|
+
| {
|
|
15
|
+
driver: "docker";
|
|
16
|
+
/** The runner image, built from `runner/Dockerfile`. */
|
|
17
|
+
image: string;
|
|
18
|
+
/** The container's name; default `roundtable-runner-<hash of the data directory>`. */
|
|
19
|
+
name?: string;
|
|
20
|
+
/** Default 1024. */
|
|
21
|
+
memoryMb?: number;
|
|
22
|
+
/** Default 1. */
|
|
23
|
+
cpus?: number;
|
|
24
|
+
/** The most processes the container runs at once; default 256. */
|
|
25
|
+
pids?: number;
|
|
26
|
+
}
|
|
27
|
+
| {
|
|
28
|
+
driver: "socket";
|
|
29
|
+
/** The runner's unix socket, in a directory only the bot's account may enter. */
|
|
30
|
+
socket: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** What the host's `runner` setting says, every limit optional. */
|
|
34
|
+
export type RunnerConfig = RunnerDriverConfig & {
|
|
35
|
+
/** A command's time limit, at most 900,000; default 120,000. A tool call's own timeout is capped by it. */
|
|
36
|
+
commandTimeoutMs?: number;
|
|
37
|
+
/** A command's output limit; default 1 MiB. */
|
|
38
|
+
maxOutputBytes?: number;
|
|
39
|
+
/** A file operation's limit; default 10 MiB. */
|
|
40
|
+
maxFileBytes?: number;
|
|
41
|
+
/** Days a workspace is kept after its last use; default 30. */
|
|
42
|
+
keepDays?: number;
|
|
43
|
+
/** The tier `bash`, `read`, `edit` and `write` need in a session the runner serves; default "member". */
|
|
44
|
+
minTier?: Tier;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** The runner setting with its defaults filled in. */
|
|
48
|
+
export type RunnerSettings = RunnerDriverConfig & {
|
|
49
|
+
commandTimeoutMs: number;
|
|
50
|
+
maxOutputBytes: number;
|
|
51
|
+
maxFileBytes: number;
|
|
52
|
+
keepDays: number;
|
|
53
|
+
minTier: Tier;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/** The docker driver's container limits unless the setting names them. */
|
|
57
|
+
export const RUNNER_DOCKER_DEFAULTS = Object.freeze({
|
|
58
|
+
memoryMb: 1024,
|
|
59
|
+
cpus: 1,
|
|
60
|
+
pids: 256,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/** The setting with its defaults; the values themselves were checked with the configuration. */
|
|
64
|
+
export function runnerSettings(config: RunnerConfig): RunnerSettings {
|
|
65
|
+
return Object.freeze({
|
|
66
|
+
...config,
|
|
67
|
+
commandTimeoutMs:
|
|
68
|
+
config.commandTimeoutMs ?? RUNNER_DEFAULTS.commandTimeoutMs,
|
|
69
|
+
maxOutputBytes: config.maxOutputBytes ?? RUNNER_DEFAULTS.maxOutputBytes,
|
|
70
|
+
maxFileBytes: config.maxFileBytes ?? RUNNER_DEFAULTS.maxFileBytes,
|
|
71
|
+
keepDays: config.keepDays ?? RUNNER_DEFAULTS.keepDays,
|
|
72
|
+
minTier: config.minTier ?? "member",
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The runner's id of the conversation whose home channel is `channel`: the channel's segment, as
|
|
78
|
+
* its session directory is named, or a digest of the channel when the segment is not one the
|
|
79
|
+
* runner accepts (too long).
|
|
80
|
+
*/
|
|
81
|
+
export function runnerConversationId(channel: ChannelKey): string {
|
|
82
|
+
const segment = channel.replace(/[^A-Za-z0-9_-]/g, "_");
|
|
83
|
+
if (isConversationId(segment)) return segment;
|
|
84
|
+
return `h-${createHash("sha256").update(channel).digest("hex").slice(0, 40)}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Where one conversation's tools work in the runner. */
|
|
88
|
+
export interface RunnerWorkspace {
|
|
89
|
+
/** The runner's id of the conversation. */
|
|
90
|
+
conversation: string;
|
|
91
|
+
/** Its workspace, the tools' working directory, as the runner names it. */
|
|
92
|
+
workspace: string;
|
|
93
|
+
/** Its scratch dir, the commands' `TMPDIR`, inside the workspace. */
|
|
94
|
+
scratchDir: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The workspace of the conversation whose home channel is `channel`; `root` only for tests. */
|
|
98
|
+
export function runnerWorkspace(
|
|
99
|
+
channel: ChannelKey,
|
|
100
|
+
root: string = RUNNER_WORKSPACES,
|
|
101
|
+
): RunnerWorkspace {
|
|
102
|
+
const conversation = runnerConversationId(channel);
|
|
103
|
+
return {
|
|
104
|
+
conversation,
|
|
105
|
+
workspace: workspaceDirOf(conversation, root),
|
|
106
|
+
scratchDir: scratchDirOf(conversation, root),
|
|
107
|
+
};
|
|
108
|
+
}
|