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
|
@@ -1,10 +1,32 @@
|
|
|
1
|
-
import { readFile, realpath, stat } from "node:fs/promises";
|
|
2
|
-
import {
|
|
1
|
+
import { mkdir, readFile, realpath, stat, writeFile } from "node:fs/promises";
|
|
2
|
+
import {
|
|
3
|
+
basename,
|
|
4
|
+
dirname,
|
|
5
|
+
isAbsolute,
|
|
6
|
+
relative,
|
|
7
|
+
resolve,
|
|
8
|
+
sep,
|
|
9
|
+
} from "node:path";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Where a session's files live when they are not on the host's disk, such as the runner's
|
|
13
|
+
* workspace: the core's file tools reach them through it, never through a host path.
|
|
14
|
+
*/
|
|
15
|
+
export interface WorkspaceFiles {
|
|
16
|
+
/** `findWorkspaceFile` for a path in these files. */
|
|
17
|
+
find(path: string): Promise<WorkspaceFile>;
|
|
18
|
+
/** `readWorkspaceFile` for a file `find` found. */
|
|
19
|
+
read(file: WorkspaceFile, maxBytes: number): Promise<Uint8Array>;
|
|
20
|
+
/** Writes `data` to the absolute `path`, making its directory; refused outside the roots. */
|
|
21
|
+
write(path: string, data: Uint8Array): Promise<void>;
|
|
22
|
+
}
|
|
3
23
|
|
|
4
24
|
/** Where a session may read files by path: its shared workspace and its scratch dir. */
|
|
5
25
|
export interface WorkspaceRoots {
|
|
6
26
|
workspace: string;
|
|
7
27
|
scratchDir?: string;
|
|
28
|
+
/** Where the files are when they are not on the host's disk; absent for a host path. */
|
|
29
|
+
files?: WorkspaceFiles;
|
|
8
30
|
}
|
|
9
31
|
|
|
10
32
|
/** A file refused by path; its message names the path and is safe to show the model. */
|
|
@@ -12,7 +34,7 @@ export class WorkspaceFileError extends Error {
|
|
|
12
34
|
override name = "WorkspaceFileError";
|
|
13
35
|
}
|
|
14
36
|
|
|
15
|
-
/** A
|
|
37
|
+
/** A file found inside the roots, not read yet. */
|
|
16
38
|
export interface WorkspaceFile {
|
|
17
39
|
/** The path as the caller gave it. */
|
|
18
40
|
path: string;
|
|
@@ -21,6 +43,8 @@ export interface WorkspaceFile {
|
|
|
21
43
|
/** Its base name, the default name it is sent under. */
|
|
22
44
|
name: string;
|
|
23
45
|
size: number;
|
|
46
|
+
/** The files it was found in, which read it; absent for a file on the host's disk. */
|
|
47
|
+
files?: WorkspaceFiles;
|
|
24
48
|
}
|
|
25
49
|
|
|
26
50
|
/** Whether `child` is `parent` or inside it, judged by the paths alone (resolve symlinks first). */
|
|
@@ -38,12 +62,14 @@ async function realRoot(root: string): Promise<string | undefined> {
|
|
|
38
62
|
|
|
39
63
|
/**
|
|
40
64
|
* Finds a regular file by path, relative paths against the workspace, and refuses one that does
|
|
41
|
-
* not resolve inside the workspace or the scratch dir, so a symlink counts by its target.
|
|
65
|
+
* not resolve inside the workspace or the scratch dir, so a symlink counts by its target. Roots
|
|
66
|
+
* with `files` are looked up there alone.
|
|
42
67
|
*/
|
|
43
68
|
export async function findWorkspaceFile(
|
|
44
69
|
path: string,
|
|
45
70
|
roots: WorkspaceRoots,
|
|
46
71
|
): Promise<WorkspaceFile> {
|
|
72
|
+
if (roots.files) return roots.files.find(path);
|
|
47
73
|
const given = resolve(roots.workspace, path);
|
|
48
74
|
let real: string;
|
|
49
75
|
try {
|
|
@@ -70,6 +96,7 @@ export async function readWorkspaceFile(
|
|
|
70
96
|
file: WorkspaceFile,
|
|
71
97
|
maxBytes: number,
|
|
72
98
|
): Promise<Uint8Array> {
|
|
99
|
+
if (file.files) return file.files.read(file, maxBytes);
|
|
73
100
|
const data = new Uint8Array(await readFile(file.realPath));
|
|
74
101
|
if (data.byteLength > maxBytes)
|
|
75
102
|
throw new WorkspaceFileError(
|
|
@@ -77,3 +104,14 @@ export async function readWorkspaceFile(
|
|
|
77
104
|
);
|
|
78
105
|
return data;
|
|
79
106
|
}
|
|
107
|
+
|
|
108
|
+
/** Writes a new file at the absolute `path` in the roots' files, making its directory first. */
|
|
109
|
+
export async function writeWorkspaceFile(
|
|
110
|
+
path: string,
|
|
111
|
+
data: Uint8Array,
|
|
112
|
+
roots: WorkspaceRoots,
|
|
113
|
+
): Promise<void> {
|
|
114
|
+
if (roots.files) return roots.files.write(path, data);
|
|
115
|
+
await mkdir(dirname(path), { recursive: true });
|
|
116
|
+
await writeFile(path, data);
|
|
117
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PollLimits,
|
|
3
|
+
PollResults,
|
|
4
|
+
PollVoter,
|
|
5
|
+
SurfacePoll,
|
|
6
|
+
SurfacePolls,
|
|
7
|
+
} from "../contract/polls.ts";
|
|
8
|
+
import type { ChannelKey } from "../domain/sessions.ts";
|
|
9
|
+
|
|
10
|
+
/** One poll a fake surface holds: what was posted, and what happened to it since. */
|
|
11
|
+
export interface FakePoll extends SurfacePoll {
|
|
12
|
+
id: string;
|
|
13
|
+
channel: ChannelKey;
|
|
14
|
+
/** Each answer's voters, by the answer's index. */
|
|
15
|
+
votes: PollVoter[][];
|
|
16
|
+
ended: boolean;
|
|
17
|
+
/** How often `end` was called for it. */
|
|
18
|
+
endCalls: number;
|
|
19
|
+
deleted: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** In-memory native polls, with the hands of the people who vote and delete. */
|
|
23
|
+
export interface FakeSurfacePolls extends SurfacePolls {
|
|
24
|
+
/** Every poll posted, oldest first. */
|
|
25
|
+
readonly posted: FakePoll[];
|
|
26
|
+
/** Someone votes for the answer at `index`; ignored once the poll ended. */
|
|
27
|
+
vote(pollId: string, index: number, voter: PollVoter): void;
|
|
28
|
+
/** Someone deletes the poll's message. */
|
|
29
|
+
remove(pollId: string): void;
|
|
30
|
+
/** The next `count` calls of `end` or `results` reject, as an outage would. */
|
|
31
|
+
failNext(count?: number): void;
|
|
32
|
+
/** Every post from now rejects with the message, as a missing permission would. */
|
|
33
|
+
refusePosts(message: string): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Discord's limits, which the fake keeps unless told otherwise. */
|
|
37
|
+
export const FAKE_POLL_LIMITS: PollLimits = {
|
|
38
|
+
answers: 10,
|
|
39
|
+
questionChars: 300,
|
|
40
|
+
answerChars: 55,
|
|
41
|
+
maxDurationMs: 768 * 3_600_000,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Native polls held in memory, which keep the poll part of the surface contract: `end` twice is
|
|
46
|
+
* harmless, results read after the end list each answer's votes and voters (at most
|
|
47
|
+
* `votersListed` each, as Discord lists 100), and a deleted poll reads `"gone"`.
|
|
48
|
+
*/
|
|
49
|
+
export function fakeSurfacePolls(
|
|
50
|
+
options: { limits?: Partial<PollLimits>; votersListed?: number } = {},
|
|
51
|
+
): FakeSurfacePolls {
|
|
52
|
+
const posted: FakePoll[] = [];
|
|
53
|
+
const listed = options.votersListed ?? 100;
|
|
54
|
+
let failures = 0;
|
|
55
|
+
let refusal: string | undefined;
|
|
56
|
+
const find = (channel: ChannelKey | undefined, id: string) =>
|
|
57
|
+
posted.find(
|
|
58
|
+
(poll) =>
|
|
59
|
+
poll.id === id && (channel === undefined || poll.channel === channel),
|
|
60
|
+
);
|
|
61
|
+
const fail = () => {
|
|
62
|
+
if (failures === 0) return;
|
|
63
|
+
failures -= 1;
|
|
64
|
+
throw new Error("the fake surface is unreachable");
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
limits: { ...FAKE_POLL_LIMITS, ...options.limits },
|
|
68
|
+
posted,
|
|
69
|
+
post: async (channel, poll) => {
|
|
70
|
+
if (refusal !== undefined) throw new Error(refusal);
|
|
71
|
+
const made: FakePoll = {
|
|
72
|
+
...poll,
|
|
73
|
+
answers: [...poll.answers],
|
|
74
|
+
id: `fake-poll-${posted.length + 1}`,
|
|
75
|
+
channel,
|
|
76
|
+
votes: poll.answers.map(() => []),
|
|
77
|
+
ended: false,
|
|
78
|
+
endCalls: 0,
|
|
79
|
+
deleted: false,
|
|
80
|
+
};
|
|
81
|
+
posted.push(made);
|
|
82
|
+
return { pollId: made.id };
|
|
83
|
+
},
|
|
84
|
+
end: async (channel, pollId) => {
|
|
85
|
+
fail();
|
|
86
|
+
const poll = find(channel, pollId);
|
|
87
|
+
if (!poll) return;
|
|
88
|
+
poll.endCalls += 1;
|
|
89
|
+
poll.ended = true;
|
|
90
|
+
},
|
|
91
|
+
results: async (channel, pollId): Promise<PollResults | "gone"> => {
|
|
92
|
+
fail();
|
|
93
|
+
const poll = find(channel, pollId);
|
|
94
|
+
if (!poll || poll.deleted) return "gone";
|
|
95
|
+
const answers = poll.answers.map((text, index) => {
|
|
96
|
+
const voters = poll.votes[index] ?? [];
|
|
97
|
+
return {
|
|
98
|
+
text,
|
|
99
|
+
votes: voters.length,
|
|
100
|
+
voters: voters.slice(0, listed),
|
|
101
|
+
votersCut: voters.length > listed,
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
const totalVoters = poll.multi
|
|
105
|
+
? new Set(answers.flatMap((answer) => answer.voters.map((v) => v.id)))
|
|
106
|
+
.size
|
|
107
|
+
: answers.reduce((sum, answer) => sum + answer.votes, 0);
|
|
108
|
+
return { answers, totalVoters };
|
|
109
|
+
},
|
|
110
|
+
vote: (pollId, index, voter) => {
|
|
111
|
+
const poll = find(undefined, pollId);
|
|
112
|
+
if (!poll || poll.ended || poll.deleted) return;
|
|
113
|
+
if (!poll.multi)
|
|
114
|
+
for (const voters of poll.votes) {
|
|
115
|
+
const at = voters.findIndex((v) => v.id === voter.id);
|
|
116
|
+
if (at >= 0) voters.splice(at, 1);
|
|
117
|
+
}
|
|
118
|
+
poll.votes[index]?.push(voter);
|
|
119
|
+
},
|
|
120
|
+
remove: (pollId) => {
|
|
121
|
+
const poll = find(undefined, pollId);
|
|
122
|
+
if (poll) poll.deleted = true;
|
|
123
|
+
},
|
|
124
|
+
failNext: (count = 1) => {
|
|
125
|
+
failures += count;
|
|
126
|
+
},
|
|
127
|
+
refusePosts: (message) => {
|
|
128
|
+
refusal = message;
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { posix } from "node:path";
|
|
2
|
+
import {
|
|
3
|
+
confinedPath,
|
|
4
|
+
RUNNER_WORKSPACES,
|
|
5
|
+
type RunnerStat,
|
|
6
|
+
workspaceDirOf,
|
|
7
|
+
} from "../runner/protocol.ts";
|
|
8
|
+
import {
|
|
9
|
+
type RunnerCommand,
|
|
10
|
+
RunnerError,
|
|
11
|
+
type RunnerExit,
|
|
12
|
+
type RunnerPort,
|
|
13
|
+
} from "../runner/runner-port.ts";
|
|
14
|
+
|
|
15
|
+
/** One call the fake runner got, by its protocol type. */
|
|
16
|
+
export interface FakeRunnerCall {
|
|
17
|
+
op: string;
|
|
18
|
+
conversation?: string;
|
|
19
|
+
path?: string;
|
|
20
|
+
command?: string;
|
|
21
|
+
cwd?: string;
|
|
22
|
+
timeoutMs?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A runner in memory, for tests of the host's side: files by absolute path, symlinks it follows
|
|
27
|
+
* as the runner does (one leading outside the conversation's workspace is refused), and commands
|
|
28
|
+
* answered by `onExec` (by default, their text echoed back). Every call is recorded in `calls`.
|
|
29
|
+
*/
|
|
30
|
+
export class FakeRunner implements RunnerPort {
|
|
31
|
+
readonly calls: FakeRunnerCall[] = [];
|
|
32
|
+
readonly files = new Map<string, Buffer>();
|
|
33
|
+
readonly links = new Map<string, string>();
|
|
34
|
+
onExec: (command: RunnerCommand) => RunnerExit | Promise<RunnerExit> = (
|
|
35
|
+
command,
|
|
36
|
+
) => {
|
|
37
|
+
command.onData(Buffer.from(`ran: ${command.command}\n`));
|
|
38
|
+
return { code: 0, cut: false, timedOut: false };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
constructor(readonly root: string = RUNNER_WORKSPACES) {}
|
|
42
|
+
|
|
43
|
+
/** The path inside the conversation's workspace, symlinks followed; refused as the runner refuses. */
|
|
44
|
+
#resolve(conversation: string, path: string): string {
|
|
45
|
+
const dir = workspaceDirOf(conversation, this.root);
|
|
46
|
+
const inside = confinedPath(dir, path);
|
|
47
|
+
if (inside === undefined)
|
|
48
|
+
throw new RunnerError("outside", `${path} is outside the workspace`);
|
|
49
|
+
const target = this.links.get(inside);
|
|
50
|
+
if (target === undefined) return inside;
|
|
51
|
+
const real = posix.resolve(posix.dirname(inside), target);
|
|
52
|
+
if (confinedPath(dir, real) === undefined)
|
|
53
|
+
throw new RunnerError("outside", `${path} is outside the workspace`);
|
|
54
|
+
return real;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#file(conversation: string, path: string): { real: string; data: Buffer } {
|
|
58
|
+
const real = this.#resolve(conversation, path);
|
|
59
|
+
const data = this.files.get(real);
|
|
60
|
+
if (data === undefined)
|
|
61
|
+
throw new RunnerError("not-found", `read ${path}: ENOENT`);
|
|
62
|
+
return { real, data };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async exec(command: RunnerCommand): Promise<RunnerExit> {
|
|
66
|
+
this.calls.push({
|
|
67
|
+
op: "exec",
|
|
68
|
+
conversation: command.conversation,
|
|
69
|
+
command: command.command,
|
|
70
|
+
cwd: command.cwd,
|
|
71
|
+
timeoutMs: command.timeoutMs,
|
|
72
|
+
});
|
|
73
|
+
return this.onExec(command);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async read(conversation: string, path: string, maxBytes: number) {
|
|
77
|
+
this.calls.push({ op: "read", conversation, path });
|
|
78
|
+
const { data } = this.#file(conversation, path);
|
|
79
|
+
if (data.byteLength > maxBytes)
|
|
80
|
+
throw new RunnerError("too-large", `${path} is over ${maxBytes} bytes`);
|
|
81
|
+
return data;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async write(
|
|
85
|
+
conversation: string,
|
|
86
|
+
path: string,
|
|
87
|
+
data: Uint8Array,
|
|
88
|
+
maxBytes: number,
|
|
89
|
+
) {
|
|
90
|
+
this.calls.push({ op: "write", conversation, path });
|
|
91
|
+
if (data.byteLength > maxBytes)
|
|
92
|
+
throw new RunnerError(
|
|
93
|
+
"too-large",
|
|
94
|
+
`the content is over ${maxBytes} bytes`,
|
|
95
|
+
);
|
|
96
|
+
this.files.set(this.#resolve(conversation, path), Buffer.from(data));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async mkdir(conversation: string, path: string) {
|
|
100
|
+
this.calls.push({ op: "mkdir", conversation, path });
|
|
101
|
+
this.#resolve(conversation, path);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async access(conversation: string, path: string) {
|
|
105
|
+
this.calls.push({ op: "access", conversation, path });
|
|
106
|
+
this.#file(conversation, path);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async stat(conversation: string, path: string): Promise<RunnerStat> {
|
|
110
|
+
this.calls.push({ op: "stat", conversation, path });
|
|
111
|
+
const real = this.#resolve(conversation, path);
|
|
112
|
+
const data = this.files.get(real);
|
|
113
|
+
if (data !== undefined)
|
|
114
|
+
return { kind: "file", size: data.byteLength, mtimeMs: 0, path: real };
|
|
115
|
+
const prefix = `${real}/`;
|
|
116
|
+
if ([...this.files.keys()].some((file) => file.startsWith(prefix)))
|
|
117
|
+
return { kind: "directory", size: 0, mtimeMs: 0, path: real };
|
|
118
|
+
throw new RunnerError("not-found", `stat ${path}: ENOENT`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async detectImage(conversation: string, path: string) {
|
|
122
|
+
this.calls.push({ op: "detect-image", conversation, path });
|
|
123
|
+
const { data } = this.#file(conversation, path);
|
|
124
|
+
return data[0] === 0x89 && data[1] === 0x50 ? "image/png" : null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async removeWorkspace(conversation: string) {
|
|
128
|
+
this.calls.push({ op: "remove-workspace", conversation });
|
|
129
|
+
const prefix = `${workspaceDirOf(conversation, this.root)}/`;
|
|
130
|
+
const owned = [...this.files.keys()].filter((file) =>
|
|
131
|
+
file.startsWith(prefix),
|
|
132
|
+
);
|
|
133
|
+
for (const file of owned) this.files.delete(file);
|
|
134
|
+
return owned.length > 0;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async sweep(keepDays: number) {
|
|
138
|
+
this.calls.push({ op: `sweep ${keepDays}` });
|
|
139
|
+
return [];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async ping() {
|
|
143
|
+
this.calls.push({ op: "ping" });
|
|
144
|
+
return { version: 1, uptimeMs: 0 };
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import type { Approval } from "../interactions/prompts.ts";
|
|
2
|
+
import type { SurfaceCheck } from "./surface-contract.ts";
|
|
3
|
+
import {
|
|
4
|
+
answeredOnlyByThem,
|
|
5
|
+
approval,
|
|
6
|
+
ContractBroken,
|
|
7
|
+
expectThat,
|
|
8
|
+
namedApproverAnswers,
|
|
9
|
+
nextPrompt,
|
|
10
|
+
scopeOf,
|
|
11
|
+
seen,
|
|
12
|
+
speakerOf,
|
|
13
|
+
until,
|
|
14
|
+
} from "./surface-contract-support.ts";
|
|
15
|
+
|
|
16
|
+
export const APPROVAL_CHECKS: readonly SurfaceCheck[] = [
|
|
17
|
+
{
|
|
18
|
+
name: "an approval resolves as the person answers it, and closes",
|
|
19
|
+
applies: (subject) =>
|
|
20
|
+
subject.surface.prompts !== undefined && subject.answer !== undefined,
|
|
21
|
+
run: async (running) => {
|
|
22
|
+
const approved = await approval(running, true);
|
|
23
|
+
expectThat(approved === "approved", `approving answered ${approved}`);
|
|
24
|
+
const declined = await approval(running, false);
|
|
25
|
+
expectThat(declined === "declined", `declining answered ${declined}`);
|
|
26
|
+
await until(
|
|
27
|
+
() => seen(running, "prompt_closed").length >= 2,
|
|
28
|
+
"both approvals close",
|
|
29
|
+
);
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: "a stopped turn's approval resolves cancelled and closes",
|
|
34
|
+
applies: (subject) =>
|
|
35
|
+
subject.surface.prompts !== undefined && subject.answer !== undefined,
|
|
36
|
+
run: async (running) => {
|
|
37
|
+
const { surface, channel } = running.subject;
|
|
38
|
+
const prompts = surface.prompts?.(channel, scopeOf(running.subject));
|
|
39
|
+
if (!prompts) throw new ContractBroken("the surface shows no prompts");
|
|
40
|
+
const stop = new AbortController();
|
|
41
|
+
const before = seen(running, "prompt").length;
|
|
42
|
+
const result = prompts.confirm(
|
|
43
|
+
"Approve?",
|
|
44
|
+
"Send it.",
|
|
45
|
+
stop.signal,
|
|
46
|
+
speakerOf(running.subject).tier,
|
|
47
|
+
);
|
|
48
|
+
const id = await nextPrompt(running, before);
|
|
49
|
+
stop.abort();
|
|
50
|
+
const answer: Approval = await result;
|
|
51
|
+
expectThat(answer === "cancelled", `a stopped turn answered ${answer}`);
|
|
52
|
+
await until(
|
|
53
|
+
() =>
|
|
54
|
+
seen(running, "prompt_closed").some(
|
|
55
|
+
(o) => o.kind === "prompt_closed" && o.id === id,
|
|
56
|
+
),
|
|
57
|
+
"the stopped approval closes",
|
|
58
|
+
);
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: "an approval its plugin withdraws resolves cancelled and closes; one answered first stays answered",
|
|
63
|
+
applies: (subject) =>
|
|
64
|
+
subject.surface.prompts !== undefined && subject.answer !== undefined,
|
|
65
|
+
run: async (running) => {
|
|
66
|
+
const { surface, channel, answer } = running.subject;
|
|
67
|
+
const prompts = surface.prompts?.(channel, scopeOf(running.subject));
|
|
68
|
+
if (!prompts || !answer)
|
|
69
|
+
throw new ContractBroken("the surface shows no prompts");
|
|
70
|
+
const ask = (supersede: AbortSignal) =>
|
|
71
|
+
prompts.confirm(
|
|
72
|
+
"Approve?",
|
|
73
|
+
"Send it.",
|
|
74
|
+
undefined,
|
|
75
|
+
speakerOf(running.subject).tier,
|
|
76
|
+
undefined,
|
|
77
|
+
undefined,
|
|
78
|
+
{ supersede },
|
|
79
|
+
);
|
|
80
|
+
const closes = (id: string) =>
|
|
81
|
+
seen(running, "prompt_closed").filter(
|
|
82
|
+
(o) => o.kind === "prompt_closed" && o.id === id,
|
|
83
|
+
).length;
|
|
84
|
+
const withdrawn = new AbortController();
|
|
85
|
+
let before = seen(running, "prompt").length;
|
|
86
|
+
const first = ask(withdrawn.signal);
|
|
87
|
+
const firstId = await nextPrompt(running, before);
|
|
88
|
+
withdrawn.abort("replaced by a newer request");
|
|
89
|
+
const result = await Promise.race([
|
|
90
|
+
first,
|
|
91
|
+
Bun.sleep(1000).then(() => "still open" as const),
|
|
92
|
+
]);
|
|
93
|
+
expectThat(
|
|
94
|
+
result === "cancelled",
|
|
95
|
+
`a withdrawn approval answered ${result}`,
|
|
96
|
+
);
|
|
97
|
+
await until(() => closes(firstId) > 0, "the withdrawn approval closes");
|
|
98
|
+
// An answer to the closed approval changes nothing, as for any ended prompt.
|
|
99
|
+
await answer(firstId, true).catch(() => undefined);
|
|
100
|
+
expectThat(closes(firstId) === 1, "the withdrawn approval closed twice");
|
|
101
|
+
// An approval answered before the withdrawal stays answered.
|
|
102
|
+
const later = new AbortController();
|
|
103
|
+
before = seen(running, "prompt").length;
|
|
104
|
+
const second = ask(later.signal);
|
|
105
|
+
const secondId = await nextPrompt(running, before);
|
|
106
|
+
await answer(secondId, true);
|
|
107
|
+
later.abort();
|
|
108
|
+
const approved: Approval = await second;
|
|
109
|
+
expectThat(
|
|
110
|
+
approved === "approved",
|
|
111
|
+
`an answered approval came back ${approved}`,
|
|
112
|
+
);
|
|
113
|
+
await Bun.sleep(20);
|
|
114
|
+
expectThat(closes(secondId) === 1, "the answered approval closed twice");
|
|
115
|
+
// A signal that is already aborted shows nothing.
|
|
116
|
+
const gone = new AbortController();
|
|
117
|
+
gone.abort();
|
|
118
|
+
before = seen(running, "prompt").length;
|
|
119
|
+
const none: Approval = await ask(gone.signal);
|
|
120
|
+
expectThat(
|
|
121
|
+
none === "cancelled",
|
|
122
|
+
`an approval withdrawn before it opened came back ${none}`,
|
|
123
|
+
);
|
|
124
|
+
expectThat(
|
|
125
|
+
seen(running, "prompt").length === before,
|
|
126
|
+
"a withdrawn approval was shown",
|
|
127
|
+
);
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
name: "another person cannot answer the person's approval",
|
|
132
|
+
applies: (subject) =>
|
|
133
|
+
subject.surface.prompts !== undefined &&
|
|
134
|
+
subject.answer !== undefined &&
|
|
135
|
+
subject.stranger !== undefined,
|
|
136
|
+
run: (running) =>
|
|
137
|
+
answeredOnlyByThem(running, running.subject.stranger, "shared"),
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
name: "another principal cannot answer for the person in their private conversation, even an owner",
|
|
141
|
+
applies: (subject) =>
|
|
142
|
+
subject.surface.prompts !== undefined &&
|
|
143
|
+
subject.answer !== undefined &&
|
|
144
|
+
subject.owner !== undefined,
|
|
145
|
+
run: (running) =>
|
|
146
|
+
answeredOnlyByThem(running, running.subject.owner, "private"),
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
name: "an approval that is the speaker's alone is not answered by an owner in a shared conversation",
|
|
150
|
+
applies: (subject) =>
|
|
151
|
+
subject.surface.prompts !== undefined &&
|
|
152
|
+
subject.answer !== undefined &&
|
|
153
|
+
subject.owner !== undefined &&
|
|
154
|
+
subject.speakerOnly === true &&
|
|
155
|
+
subject.privateOnly !== true,
|
|
156
|
+
run: (running) =>
|
|
157
|
+
answeredOnlyByThem(
|
|
158
|
+
running,
|
|
159
|
+
running.subject.owner,
|
|
160
|
+
"shared",
|
|
161
|
+
undefined,
|
|
162
|
+
true,
|
|
163
|
+
),
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
name: "a named approver answers an approval instead of the person; the person and another person cannot",
|
|
167
|
+
applies: (subject) =>
|
|
168
|
+
subject.surface.prompts !== undefined &&
|
|
169
|
+
subject.answer !== undefined &&
|
|
170
|
+
subject.stranger !== undefined &&
|
|
171
|
+
subject.approver !== undefined &&
|
|
172
|
+
subject.privateOnly !== true,
|
|
173
|
+
run: namedApproverAnswers,
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
name: "a private conversation is answered by its person alone, whoever else a call names",
|
|
177
|
+
applies: (subject) =>
|
|
178
|
+
subject.surface.prompts !== undefined &&
|
|
179
|
+
subject.answer !== undefined &&
|
|
180
|
+
subject.approver !== undefined,
|
|
181
|
+
run: (running) =>
|
|
182
|
+
answeredOnlyByThem(running, running.subject.approver, "private", [
|
|
183
|
+
speakerOf(running.subject).principalId,
|
|
184
|
+
running.subject.approver?.principalId ?? "",
|
|
185
|
+
]),
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
name: "a private conversation's approval above the person's tier goes to no one: it comes back at once, unshown",
|
|
189
|
+
applies: (subject) =>
|
|
190
|
+
subject.surface.prompts !== undefined &&
|
|
191
|
+
speakerOf(subject).tier !== "owner",
|
|
192
|
+
run: async (running) => {
|
|
193
|
+
const { surface, channel } = running.subject;
|
|
194
|
+
const prompts = surface.prompts?.(
|
|
195
|
+
channel,
|
|
196
|
+
scopeOf(running.subject, "private"),
|
|
197
|
+
);
|
|
198
|
+
if (!prompts) throw new ContractBroken("the surface shows no prompts");
|
|
199
|
+
const before = seen(running, "prompt").length;
|
|
200
|
+
const answer = await Promise.race([
|
|
201
|
+
prompts.confirm("Approve?", "Delete the notes.", undefined, "owner"),
|
|
202
|
+
Bun.sleep(1000).then(() => "still open" as const),
|
|
203
|
+
]);
|
|
204
|
+
expectThat(
|
|
205
|
+
// `expired` is what surfaces answered before `unavailable` existed.
|
|
206
|
+
answer === "unavailable" || answer === "expired",
|
|
207
|
+
`an approval no one may answer came back ${answer}`,
|
|
208
|
+
);
|
|
209
|
+
expectThat(
|
|
210
|
+
seen(running, "prompt").length === before,
|
|
211
|
+
"an approval no one may answer was shown",
|
|
212
|
+
);
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
];
|