Files
oh-my-pi/packages/coding-agent/test/tool-discovery/initial-tools.test.ts
T
metaphorics 18ee97751a feat(coding-agent): opt-in experimental auto-learn (memory + isolated managed skills)
Add a default-off "auto-learn" loop. When `autolearn.enabled` is set, after the
agent stops a session controller nudges it to capture reusable lessons: durable
facts go to long-term memory and repeatable procedures become "managed skills" —
SKILL.md files written to an isolated ~/.omp/agent/managed-skills directory that is
discovered and surfaced like authored skills but never overwrites them.

Two tools back this:
- `manage_skill` — create/update/delete managed skills.
- `learn` — record a lesson, optionally minting/enhancing a managed skill in the
  same call (requires a hindsight/mnemopi memory backend).

The nudge is passive by default (a hidden reminder rides the next turn);
`autolearn.autoContinue` instead auto-runs one capture turn at stop, and
`autolearn.minToolCalls` (default 5) gates trivial turns. Plan/goal-mode turns and
subagents are never nudged, and the controller re-checks the live setting at fire
time so a mid-session opt-out takes effect.

Isolation & precedence: managed skills are a separate lowest-priority discovery
provider, so an authored skill of the same name wins across every provider and
custom directory regardless of third-party toggles; a disabled higher-priority
authored skill can never hide a managed one, and managed never masks an enabled
authored skill. Managed names and descriptions are sanitized on both write and
read (control/format chars, angle brackets, and Markdown fences) before they render
into the system prompt, and the SKILL.md byte cap is enforced on the final
serialized file.

Default off → zero footprint when disabled.
2026-06-14 10:45:23 +09:00

167 lines
5.6 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
import type { BuiltinToolLoadMode, ToolSession } from "@oh-my-pi/pi-coding-agent/tools";
import {
AskTool,
BUILTIN_TOOLS,
computeEssentialBuiltinNames,
createTools,
DEFAULT_ESSENTIAL_TOOL_NAMES,
filterInitialToolsForDiscoveryAll,
IrcTool,
JobTool,
SshTool,
} from "@oh-my-pi/pi-coding-agent/tools";
const allToolsSettings = Settings.isolated({
"astGrep.enabled": true,
"astEdit.enabled": true,
"renderMermaid.enabled": true,
"debug.enabled": true,
"find.enabled": true,
"search.enabled": true,
"github.enabled": true,
"lsp.enabled": true,
"inspect_image.enabled": true,
"web_search.enabled": true,
"browser.enabled": true,
"checkpoint.enabled": true,
"todo.enabled": true,
"memory.backend": "mnemopi",
"autolearn.enabled": true,
"tools.discoveryMode": "all",
});
const toolSession: ToolSession = {
cwd: "/tmp/test",
hasUI: false,
getSessionFile: () => null,
getSessionSpawns: () => null,
settings: allToolsSettings,
isToolDiscoveryEnabled: () => true,
getSelectedDiscoveredToolNames: () => [],
activateDiscoveredTools: async names => names,
};
async function getToolMetadata(): Promise<Map<string, { loadMode?: string; summary?: string }>> {
const tools = await createTools(toolSession, Object.keys(BUILTIN_TOOLS));
const metadata = new Map(tools.map(tool => [tool.name, { loadMode: tool.loadMode, summary: tool.summary }]));
for (const tool of [
new AskTool({ ...toolSession, hasUI: true }),
new SshTool(toolSession, [], new Map(), ""),
new JobTool(toolSession),
new IrcTool(toolSession),
]) {
metadata.set(tool.name, { loadMode: tool.loadMode, summary: tool.summary });
}
return metadata;
}
describe("BUILTIN_TOOLS public factory map", () => {
it("sets loading fields on tool definitions without wrapping factories", async () => {
const metadata = await getToolMetadata();
const missing = Object.keys(BUILTIN_TOOLS).filter(name => metadata.get(name)?.loadMode === undefined);
expect(missing).toEqual([]);
});
});
describe("built-in tool loadMode annotations", () => {
it("provides a summary for every discoverable tool", async () => {
const missing: string[] = [];
const metadata = await getToolMetadata();
for (const [name, meta] of metadata) {
if (meta.loadMode === "discoverable" && !meta.summary) {
missing.push(name);
}
}
expect(missing).toEqual([]);
});
});
describe("computeEssentialBuiltinNames", () => {
it("returns DEFAULT_ESSENTIAL_TOOL_NAMES when override is empty", () => {
const settings = Settings.isolated({});
expect(computeEssentialBuiltinNames(settings).sort()).toEqual([...DEFAULT_ESSENTIAL_TOOL_NAMES].sort());
});
it("respects tools.essentialOverride when provided", () => {
const settings = Settings.isolated({ "tools.essentialOverride": ["read", "find"] });
expect(computeEssentialBuiltinNames(settings).sort()).toEqual(["find", "read"]);
});
it("filters override entries that are not known built-in tools", () => {
const settings = Settings.isolated({
"tools.essentialOverride": ["read", "not_a_real_tool", "edit"],
});
expect(computeEssentialBuiltinNames(settings).sort()).toEqual(["edit", "read"]);
});
it("trims whitespace and drops empty entries from the override", () => {
const settings = Settings.isolated({
"tools.essentialOverride": [" read ", "", " "],
});
expect(computeEssentialBuiltinNames(settings)).toEqual(["read"]);
});
it("falls back to defaults when override is non-empty but contains only invalid names", () => {
// The filtered list is empty (no valid names), but the override was provided —
// current behavior returns the empty filtered list (caller can decide). Document the behavior.
const settings = Settings.isolated({
"tools.essentialOverride": ["not_a_real_tool"],
});
expect(computeEssentialBuiltinNames(settings)).toEqual([]);
});
});
describe("tools.discoveryMode settings schema", () => {
it("defaults to auto discovery mode", () => {
const settings = Settings.isolated({});
expect(settings.get("tools.discoveryMode")).toBe("auto");
});
it("back-compat: mcp.discoveryMode still accepted", () => {
const settings = Settings.isolated({ "mcp.discoveryMode": true });
expect(settings.get("mcp.discoveryMode")).toBe(true);
});
});
describe("filterInitialToolsForDiscoveryAll", () => {
const loadModes: Record<string, BuiltinToolLoadMode> = {
read: "essential",
edit: "essential",
todo: "discoverable",
find: "discoverable",
};
const base = {
loadModeOf: (name: string): BuiltinToolLoadMode | undefined => loadModes[name],
essentialNames: new Set(["read", "bash", "edit"]),
explicitlyRequested: new Set<string>(),
restored: new Set<string>(),
forceActive: new Set<string>(),
};
it("hides non-essential discoverable built-ins", () => {
expect(filterInitialToolsForDiscoveryAll(["read", "edit", "todo", "find"], base)).toEqual(["read", "edit"]);
});
it("keeps discoverable tools required by a forced tool_choice (eager todo)", () => {
const result = filterInitialToolsForDiscoveryAll(["read", "todo", "find"], {
...base,
forceActive: new Set(["todo"]),
});
expect(result).toEqual(["read", "todo"]);
});
it("keeps explicitly requested and restored discoverable tools", () => {
const result = filterInitialToolsForDiscoveryAll(["todo", "find"], {
...base,
explicitlyRequested: new Set(["find"]),
restored: new Set(["todo"]),
});
expect([...result].sort()).toEqual(["find", "todo"]);
});
it("never hides tools without a built-in loadMode (MCP/custom/extension)", () => {
expect(filterInitialToolsForDiscoveryAll(["mcp__server__tool", "find"], base)).toEqual(["mcp__server__tool"]);
});
});