Files
oh-my-pi/packages/coding-agent/test/tools/index.test.ts
T
can1357 5ff277349c refactor(coding-agent): consolidated tool surface onto xd:// devices and hub
- Added the `xd://` virtual device protocol (`internal-urls/xd-protocol.ts`, `tools/xdev.ts`): tools declaring `loadMode: "discoverable"` are unmounted from the request tools array and driven via `read xd://` (list/docs+schema) and `write xd://<tool>` (execute), gated by the `tools.xdev` setting (default on) and inlined into the system prompt.
- Merged the `irc`, `job`, and `launch` tools into a single `hub` tool (`tools/hub/`, `async/job-manager.ts`): messaging keeps `send`/`inbox`/`list`, job control maps to `wait`/`cancel`/`jobs`, process supervision keeps `start`/`logs`/`stop`/`restart`/`describe` with `ps`, and the unified `wait` races background jobs against peer messages; SDK `IrcTool`/`JobTool`/`LaunchTool` are replaced by `HubTool`.
- Removed the hidden `resolve` tool in favor of the `xd://resolve`/`xd://reject`/`xd://propose` resolution devices, auto-including `write` whenever a deferrable tool or plan mode is present.
- Removed the BM25 tool-discovery system: the `search_tool_bm25` tool, the `tool-discovery` module, the `tools.discoveryMode`/`mcp.discoveryMode`/`mcp.discoveryDefaultServers`/`tools.essentialOverride` settings, per-tool MCP selection, and the `mcp_tool_selection` message type.
- Unified tool presentation on `ToolLoadMode` (`essential`|`discoverable`), replacing the custom-tool `xdev?: boolean` opt-out; custom, extension, MCP, RPC host, image-generation, and TTS tools now default to `discoverable`, and added a `satisfies` predicate to `SoftToolRequirement`.
- Removed the standalone `ssh` command tool and `ssh/ssh-executor` (the `ssh://` read/write/search protocol stays), and made `--tools` address hidden built-ins.
- Updated collab-web to render `xd://` dispatches and `hub` op families, dropped the `search_tool_bm25`/`ssh`/`report-finding` renderers, refreshed tool docs and prompts, and migrated the affected tests and changelogs.
2026-07-15 15:16:29 +02:00

279 lines
8.7 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "bun:test";
import { type SettingPath, Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
import { createTools, HIDDEN_TOOLS, type ToolSession } from "@oh-my-pi/pi-coding-agent/tools";
Bun.env.PI_PYTHON_SKIP_CHECK = "1";
function createTestSession(overrides: Partial<ToolSession> = {}): ToolSession {
return {
cwd: "/tmp/test",
hasUI: false,
getSessionFile: () => null,
getSessionSpawns: () => "*",
settings: Settings.isolated(),
...overrides,
};
}
function createSettingsWithOverrides(overrides: Partial<Record<SettingPath, unknown>> = {}): Settings {
return Settings.isolated({
"lsp.formatOnWrite": true,
"bashInterceptor.enabled": true,
...overrides,
});
}
function createActiveGoalState() {
return {
enabled: true,
mode: "active" as const,
goal: {
id: "goal-1",
objective: "Ship the release",
status: "active" as const,
tokenBudget: 25,
tokensUsed: 5,
timeUsedSeconds: 0,
createdAt: 1,
updatedAt: 1,
},
};
}
describe("createTools", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("creates all builtin tools by default", async () => {
// xdev mounting (default-on) would unmount discoverables like lsp and
// web_search into xd://; disable it to assert the full builtin set.
const session = createTestSession({ settings: createSettingsWithOverrides({ "tools.xdev": false }) });
const tools = await createTools(session);
const names = tools.map(t => t.name);
// Core tools should always be present
expect(names).toContain("eval");
expect(names).toContain("bash");
expect(names).toContain("read");
expect(names).toContain("edit");
expect(names).toContain("write");
expect(names).toContain("grep");
expect(names).toContain("glob");
expect(names).toContain("lsp");
expect(names).toContain("task");
expect(names).toContain("todo");
expect(names).toContain("web_search");
expect(names).not.toContain("fetch");
expect(names).not.toContain("vim");
});
it("normalizes legacy explicit tool names", async () => {
const session = createTestSession({
settings: createSettingsWithOverrides({ "astGrep.enabled": false }),
});
const tools = await createTools(session, ["search", "find", "grep"]);
const names = tools.map(t => t.name);
expect(names.filter(name => name === "grep")).toHaveLength(1);
expect(names).toContain("glob");
expect(names).not.toContain("search");
expect(names).not.toContain("find");
});
it("includes bash and eval when both eval backends are allowed", async () => {
const session = createTestSession({
settings: createSettingsWithOverrides({
"eval.py": true,
"eval.js": true,
}),
});
const tools = await createTools(session);
const names = tools.map(t => t.name);
expect(names).toContain("eval");
expect(names).toContain("bash");
});
it("still exposes eval when only the js backend is allowed", async () => {
const session = createTestSession({
settings: createSettingsWithOverrides({
"eval.py": false,
"eval.js": true,
}),
});
const tools = await createTools(session);
const names = tools.map(t => t.name);
expect(names).toContain("bash");
expect(names).toContain("eval");
});
it("still exposes eval when python kernel is unavailable (dispatches to js)", async () => {
const session = createTestSession();
vi.spyOn(
await import("@oh-my-pi/pi-coding-agent/eval/py/kernel"),
"checkPythonKernelAvailability",
).mockResolvedValue({
ok: false,
reason: "missing python",
});
const tools = await createTools(session, ["eval"]);
const names = tools.map(t => t.name);
expect(names).toContain("eval");
});
it("excludes lsp tool when session disables LSP", async () => {
const session = createTestSession({ enableLsp: false });
const tools = await createTools(session, ["read", "lsp", "write"]);
const names = tools.map(t => t.name);
expect(names).toEqual(["read", "write"]);
});
it("excludes lsp tool when disabled", async () => {
const session = createTestSession({ enableLsp: false });
const tools = await createTools(session);
const names = tools.map(t => t.name);
expect(names).not.toContain("lsp");
});
it("respects requested tool subset", async () => {
const session = createTestSession();
const tools = await createTools(session, ["read", "write"]);
const names = tools.map(t => t.name);
expect(names).toEqual(["read", "write"]);
});
it("lowercases requested tool subset", async () => {
const session = createTestSession();
const tools = await createTools(session, ["Read", "Write"]);
const names = tools.map(t => t.name);
expect(names).toEqual(["read", "write"]);
});
it("includes hidden tools when explicitly requested", async () => {
const session = createTestSession();
const tools = await createTools(session, ["yield"]);
const names = tools.map(t => t.name);
expect(names).toEqual(["yield"]);
});
it("includes yield tool when required", async () => {
const session = createTestSession({ requireYieldTool: true });
const tools = await createTools(session);
const names = tools.map(t => t.name);
expect(names).toContain("yield");
});
it("excludes todo from yield sessions unless prewalk is armed", async () => {
// Subagents (requireYieldTool) never get todo — except when the spawn is
// prewalk-armed: the prewalk plan nudge + todo gate need the child to
// commit its own todo list before the model hand-off.
const subagent = await createTools(createTestSession({ requireYieldTool: true }));
expect(subagent.map(t => t.name)).not.toContain("todo");
const prewalkSubagent = await createTools(createTestSession({ requireYieldTool: true, prewalkArmed: true }));
expect(prewalkSubagent.map(t => t.name)).toContain("todo");
});
it("excludes ask tool when hasUI is false", async () => {
const session = createTestSession({ hasUI: false });
const tools = await createTools(session);
const names = tools.map(t => t.name);
expect(names).not.toContain("ask");
});
it("includes ask tool when hasUI is true", async () => {
const session = createTestSession({ hasUI: true });
const tools = await createTools(session);
const names = tools.map(t => t.name);
expect(names).toContain("ask");
});
it("excludes ask tool when ask.enabled is false", async () => {
const session = createTestSession({
hasUI: true,
settings: createSettingsWithOverrides({ "ask.enabled": false }),
});
const tools = await createTools(session);
expect(tools.map(t => t.name)).not.toContain("ask");
const requested = await createTools(session, ["ask", "read"]);
expect(requested.map(t => t.name)).toEqual(["read", "write"]);
});
it("includes ask tool when ask.enabled is true and hasUI is true", async () => {
const session = createTestSession({
hasUI: true,
settings: createSettingsWithOverrides({ "ask.enabled": true }),
});
const tools = await createTools(session);
expect(tools.map(t => t.name)).toContain("ask");
});
it("filters disabled builtin tools by settings", async () => {
const session = createTestSession({
settings: createSettingsWithOverrides({
"glob.enabled": false,
"grep.enabled": false,
"astGrep.enabled": false,
"astEdit.enabled": false,
"bash.enabled": false,
"launch.enabled": false,
"web_search.enabled": false,
"browser.enabled": false,
"inspect_image.enabled": false,
}),
});
const tools = await createTools(session);
const names = tools.map(t => t.name);
expect(names).not.toContain("bash");
expect(names).not.toContain("launch");
expect(names).not.toContain("glob");
expect(names).not.toContain("grep");
expect(names).not.toContain("ast_grep");
expect(names).not.toContain("ast_edit");
expect(names).not.toContain("web_search");
expect(names).not.toContain("browser");
expect(names).not.toContain("inspect_image");
const requestedTools = await createTools(session, ["bash", "read"]);
expect(requestedTools.map(t => t.name)).toEqual(["read", "write"]);
});
it("auto-includes goal when goal mode is active", async () => {
const session = createTestSession({
settings: createSettingsWithOverrides({
"goal.enabled": true,
}),
getGoalModeState: () => createActiveGoalState(),
});
const tools = await createTools(session, ["read"]);
const names = tools.map(t => t.name);
expect(names).toEqual(["read", "goal"]);
});
it("records active tools on the original session object", async () => {
const session = createTestSession();
await createTools(session, ["bash"]);
expect(session.isToolActive?.("bash")).toBe(true);
expect(session.isToolActive?.("read")).toBe(false);
});
it("HIDDEN_TOOLS contains yield and goal", () => {
expect(Object.keys(HIDDEN_TOOLS).sort()).toEqual(["goal", "yield"]);
});
});