Files
stack/packages/discord/tests/fake-pi.mjs
T
jason.woltjeandClaude Opus 5 1ac812d3d5 feat(discord): read-only tools for the Discord Sage through a Mosaic pi extension confined to declared roots (#1509)
A binding may declare `tools` with named roots. pi starts with
--no-builtin-tools and the package's own extension, allowlisting
list_dir, read_file and search. src/tools.mjs holds the rules: names
not paths, per-segment lstat walk, one checked descriptor read that
refuses symlinks, swaps, FIFOs, hard links and oversize files, credential
shapes refusing the whole read, and a per-message call budget. The engine
settles on agent_end and records tool calls in the turn record.

Jason's rulings R1-R7 in the brief, section 7. rev-code-02 approved
round 2 (comment 26276) on tree 43f0329b after four round 1 fixes.
Suite 48/48, node tests 116. Not pushed.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-09-14 19:52:21 -05:00

142 lines
5.9 KiB
JavaScript

// A stand-in for `pi --mode rpc`. Reads JSONL commands on stdin, writes
// events on stdout. Behaviour is scripted per prompt text:
// "slow <ms>" answer "slow reply" after <ms>
// "garbage" emit one malformed line
// "error" end the turn with stopReason error
// "tools <n>" a first turn that calls <n> tools (read_file, with a
// tool_execution_start/end pair each, the last one refused),
// then a second turn that answers "read <n> file(s)"
// "toolonly" a run whose only turn calls a tool and never answers
// "retry" an agent_end with willRetry, then the real answer
// "late <ms>" ignore abort; after <ms> emit a tool pair and a tool turn,
// then answer "late reply", like a run that outlives its
// client-side timeout
// anything else answer "echo: <text>" immediately
// A prompt received while busy without streamingBehavior is refused, as pi
// does. Every command is mirrored to FAKE_PI_LOG when set.
import { appendFileSync } from "node:fs";
const logPath = process.env.FAKE_PI_LOG;
const out = (o) => process.stdout.write(JSON.stringify(o) + "\n");
let busy = false;
const queue = [];
function assistant(text, stopReason = "stop") {
return { role: "assistant", content: text === null ? [] : [{ type: "text", text }], stopReason, usage: { input: 3, output: 2 }, model: "fake", provider: "fake" };
}
function run(text) {
busy = true;
out({ type: "agent_start" });
out({ type: "turn_start" });
const finish = (message) => {
out({ type: "turn_end", message, toolResults: [] });
out({ type: "agent_end", messages: [message] });
if (queue.length > 0) {
run(queue.shift());
return;
}
busy = false;
out({ type: "agent_settled" });
};
const tm = /^tools (\d+)$/.exec(text);
if (tm || text === "toolonly") {
const n = tm ? Number(tm[1]) : 1;
const calls = [];
for (let i = 1; i <= n; i += 1) {
const id = `call_${i}`;
const last = i === n && n > 1;
calls.push({ type: "toolCall", id, name: "read_file", arguments: { root: "docs", path: `f${i}.md` } });
out({ type: "tool_execution_start", toolCallId: id, toolName: "read_file", args: { root: "docs", path: `f${i}.md` } });
out({ type: "tool_execution_end", toolCallId: id, toolName: "read_file", isError: false, result: { content: [{ type: "text", text: last ? "refused: budget" : "1: hello" }], details: last ? { tool: "read_file", root: "docs", path: `f${i}.md`, ok: false, reason: "tool budget for this message is used up", ms: 1 } : { tool: "read_file", root: "docs", path: `f${i}.md`, ok: true, bytes: 9, ms: 2 } } });
}
const toolTurn = { role: "assistant", content: calls, stopReason: "toolUse", usage: { input: 3, output: 2 }, model: "fake", provider: "fake" };
out({ type: "turn_end", message: toolTurn, toolResults: [] });
if (text === "toolonly") {
out({ type: "agent_end", messages: [toolTurn] });
busy = false;
out({ type: "agent_settled" });
return;
}
out({ type: "turn_start" });
const answer = assistant(`read ${n} file(s)`);
out({ type: "turn_end", message: answer, toolResults: [] });
out({ type: "agent_end", messages: [toolTurn, answer] });
if (queue.length > 0) {
run(queue.shift());
return;
}
busy = false;
out({ type: "agent_settled" });
return;
}
const lm = /^late (\d+)$/.exec(text);
if (lm) {
setTimeout(() => {
const args = { root: "docs", path: "late.md" };
out({ type: "tool_execution_start", toolCallId: "call_late", toolName: "read_file", args });
out({ type: "tool_execution_end", toolCallId: "call_late", toolName: "read_file", isError: false, result: { content: [{ type: "text", text: "1: late" }], details: { tool: "read_file", ...args, ok: true, bytes: 5, ms: 1 } } });
out({ type: "turn_end", message: { role: "assistant", content: [{ type: "toolCall", id: "call_late", name: "read_file", arguments: args }], stopReason: "toolUse", usage: { input: 3, output: 2 }, model: "fake", provider: "fake" }, toolResults: [] });
out({ type: "turn_start" });
finish(assistant("late reply"));
}, Number(lm[1]));
return;
}
if (text === "retry") {
out({ type: "agent_end", messages: [], willRetry: true });
finish(assistant("after retry"));
return;
}
const m = /^slow (\d+)$/.exec(text);
if (m) {
const timer = setTimeout(() => finish(assistant("slow reply")), Number(m[1]));
current = { timer, finish };
return;
}
if (text === "garbage") {
process.stdout.write("this is not json\n");
finish(assistant("after garbage"));
return;
}
if (text === "error") {
finish({ ...assistant(null, "error"), errorMessage: "fake provider error" });
return;
}
finish(assistant(`echo: ${text}`));
}
let current = null;
let buffer = "";
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => {
buffer += chunk;
let idx;
while ((idx = buffer.indexOf("\n")) !== -1) {
const line = buffer.slice(0, idx);
buffer = buffer.slice(idx + 1);
if (!line) continue;
const cmd = JSON.parse(line);
if (logPath) appendFileSync(logPath, line + "\n");
if (cmd.type === "prompt") {
if (busy && !cmd.streamingBehavior) {
out({ id: cmd.id, type: "response", command: "prompt", success: false, error: "agent is streaming; specify streamingBehavior" });
continue;
}
out({ id: cmd.id, type: "response", command: "prompt", success: true });
if (busy) queue.push(cmd.message);
else run(cmd.message);
} else if (cmd.type === "abort") {
out({ id: cmd.id, type: "response", command: "abort", success: true });
if (current) {
clearTimeout(current.timer);
const f = current.finish;
current = null;
f(assistant("", "aborted"));
}
} else {
out({ id: cmd.id, type: "response", command: cmd.type, success: true, data: {} });
}
}
});
process.stdin.on("end", () => process.exit(0));