import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { createServer } from "node:http"; import { tmpdir } from "node:os"; import { join } from "path"; import { describe, expect, it } from "vitest"; import type { ToolContext } from "@poncho-ai/sdk"; import { AgentHarness } from "../src/harness.js"; import { createEditTool } from "../src/default-tools.js"; import { loadSkillMetadata } from "../src/skill-context.js"; const stubContext: ToolContext = { runId: "test-run", agentId: "test-agent", step: 1, workingDir: "/tmp", parameters: {}, }; describe("agent harness", () => { it("registers default filesystem tools", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-default-tools-")); await writeFile( join(dir, "AGENT.md"), `--- name: default-tools-agent model: provider: anthropic name: claude-opus-4-5 --- # Default Tools Agent `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const names = harness.listTools().map((tool) => tool.name); expect(names).toContain("bash"); expect(names).toContain("web_search"); expect(names).toContain("todo_list"); }); it("disables write_file by default in production environment", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-prod-tools-")); await writeFile( join(dir, "AGENT.md"), `--- name: prod-tools-agent model: provider: anthropic name: claude-opus-4-5 --- # Prod Tools Agent `, "utf8", ); const harness = new AgentHarness({ workingDir: dir, environment: "production" }); await harness.initialize(); const names = harness.listTools().map((tool) => tool.name); // In production, bash is registered but poncho_docs is not expect(names).toContain("bash"); expect(names).not.toContain("poncho_docs"); }); it("allows disabling built-in tools via poncho.config.js", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-disable-default-tools-")); await writeFile( join(dir, "AGENT.md"), `--- name: disable-default-tools-agent model: provider: anthropic name: claude-opus-4-5 --- # Disable Default Tools Agent `, "utf8", ); await writeFile( join(dir, "poncho.config.js"), `export default { tools: { web_search: false } }; `, "utf8", ); const harness = new AgentHarness({ workingDir: dir, environment: "production" }); await harness.initialize(); const names = harness.listTools().map((tool) => tool.name); expect(names).toContain("bash"); expect(names).not.toContain("web_search"); }); it("supports per-environment tool overrides", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-env-tool-overrides-")); await writeFile( join(dir, "AGENT.md"), `--- name: env-tool-overrides-agent model: provider: anthropic name: claude-opus-4-5 --- # Env Tool Overrides Agent `, "utf8", ); await writeFile( join(dir, "poncho.config.js"), `export default { tools: { web_search: false, byEnvironment: { development: { web_search: true }, production: { web_fetch: false } } } }; `, "utf8", ); const developmentHarness = new AgentHarness({ workingDir: dir, environment: "development" }); await developmentHarness.initialize(); const developmentTools = developmentHarness.listTools().map((tool) => tool.name); expect(developmentTools).toContain("web_search"); expect(developmentTools).toContain("bash"); const productionHarness = new AgentHarness({ workingDir: dir, environment: "production" }); await productionHarness.initialize(); const productionTools = productionHarness.listTools().map((tool) => tool.name); expect(productionTools).not.toContain("web_search"); expect(productionTools).not.toContain("web_fetch"); }); it("does not auto-register exported tool objects from skill scripts", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-no-auto-tool-register-")); await writeFile( join(dir, "AGENT.md"), `--- name: no-auto-tool-register-agent model: provider: anthropic name: claude-opus-4-5 --- # No Auto Tool Register Agent `, "utf8", ); await mkdir(join(dir, "skills", "summarize", "scripts"), { recursive: true }); await writeFile( join(dir, "skills", "summarize", "SKILL.md"), `--- name: summarize description: Summarize text --- # Summarize Skill `, "utf8", ); await writeFile( join(dir, "skills", "summarize", "scripts", "summarize.ts"), `import { defineTool } from "@poncho-ai/sdk"; export default defineTool({ name: "summarize_text", description: "Summarize input text", inputSchema: { type: "object", properties: { content: { type: "string" } }, required: ["content"] }, async handler(input) { return { summary: String(input.content).slice(0, 20) }; } }); `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const names = harness.listTools().map((tool) => tool.name); expect(names).not.toContain("summarize_text"); expect(names).toContain("run_skill_script"); }); it("refreshes skill metadata and tools in development mode", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-skill-refresh-dev-")); await writeFile( join(dir, "AGENT.md"), `--- name: skill-refresh-dev-agent model: provider: anthropic name: claude-opus-4-5 --- # Skill Refresh Dev Agent `, "utf8", ); await mkdir(join(dir, "skills", "alpha"), { recursive: true }); await writeFile( join(dir, "skills", "alpha", "SKILL.md"), `--- name: alpha description: Alpha skill --- # Alpha `, "utf8", ); const harness = new AgentHarness({ workingDir: dir, environment: "development" }); await harness.initialize(); const activateBefore = harness.listTools().find((tool) => tool.name === "activate_skill"); expect(activateBefore).toBeDefined(); const unknownBefore = await activateBefore!.handler({ name: "beta" }, {} as any); expect(unknownBefore).toMatchObject({ error: expect.stringContaining('Unknown skill: "beta"'), }); await mkdir(join(dir, "skills", "beta"), { recursive: true }); await writeFile( join(dir, "skills", "beta", "SKILL.md"), `--- name: beta description: Beta skill --- # Beta `, "utf8", ); await (harness as any).refreshSkillsIfChanged(); const activateAfter = harness.listTools().find((tool) => tool.name === "activate_skill"); expect(activateAfter).toBeDefined(); const activated = await activateAfter!.handler({ name: "beta" }, {} as any); expect(activated).toMatchObject({ skill: "beta" }); }); it("prunes removed active skills after refresh in development mode", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-skill-refresh-prune-")); await writeFile( join(dir, "AGENT.md"), `--- name: skill-refresh-prune-agent model: provider: anthropic name: claude-opus-4-5 --- # Skill Refresh Prune Agent `, "utf8", ); await mkdir(join(dir, "skills", "obsolete"), { recursive: true }); await writeFile( join(dir, "skills", "obsolete", "SKILL.md"), `--- name: obsolete description: Obsolete skill --- # Obsolete `, "utf8", ); const harness = new AgentHarness({ workingDir: dir, environment: "development" }); await harness.initialize(); const activate = harness.listTools().find((tool) => tool.name === "activate_skill"); const listActive = harness.listTools().find((tool) => tool.name === "list_active_skills"); expect(activate).toBeDefined(); expect(listActive).toBeDefined(); await activate!.handler({ name: "obsolete" }, {} as any); expect(await listActive!.handler({}, {} as any)).toEqual({ activeSkills: ["obsolete"] }); await rm(join(dir, "skills", "obsolete"), { recursive: true, force: true }); await (harness as any).refreshSkillsIfChanged(); expect(await listActive!.handler({}, {} as any)).toEqual({ activeSkills: [] }); const activateAfter = harness.listTools().find((tool) => tool.name === "activate_skill"); const afterRemoval = await activateAfter!.handler({ name: "obsolete" }, {} as any); expect(afterRemoval).toMatchObject({ error: expect.stringContaining('Unknown skill: "obsolete"'), }); }); it("does not refresh skills outside development mode", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-skill-refresh-prod-")); await writeFile( join(dir, "AGENT.md"), `--- name: skill-refresh-prod-agent model: provider: anthropic name: claude-opus-4-5 --- # Skill Refresh Prod Agent `, "utf8", ); await mkdir(join(dir, "skills", "alpha"), { recursive: true }); await writeFile( join(dir, "skills", "alpha", "SKILL.md"), `--- name: alpha description: Alpha skill --- # Alpha `, "utf8", ); const harness = new AgentHarness({ workingDir: dir, environment: "production" }); await harness.initialize(); await mkdir(join(dir, "skills", "beta"), { recursive: true }); await writeFile( join(dir, "skills", "beta", "SKILL.md"), `--- name: beta description: Beta skill --- # Beta `, "utf8", ); await (harness as any).refreshSkillsIfChanged(); const activate = harness.listTools().find((tool) => tool.name === "activate_skill"); expect(activate).toBeDefined(); const unknown = await activate!.handler({ name: "beta" }, {} as any); expect(unknown).toMatchObject({ error: expect.stringContaining('Unknown skill: "beta"'), }); }); it("preserves active skills when skill metadata changes in development mode", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-skill-refresh-clear-active-")); await writeFile( join(dir, "AGENT.md"), `--- name: skill-refresh-clear-agent model: provider: anthropic name: claude-opus-4-5 --- # Skill Refresh Clear Agent `, "utf8", ); await mkdir(join(dir, "skills", "alpha"), { recursive: true }); await writeFile( join(dir, "skills", "alpha", "SKILL.md"), `--- name: alpha description: Alpha skill --- # Alpha `, "utf8", ); const harness = new AgentHarness({ workingDir: dir, environment: "development" }); await harness.initialize(); const activate = harness.listTools().find((tool) => tool.name === "activate_skill"); const listActive = harness.listTools().find((tool) => tool.name === "list_active_skills"); expect(activate).toBeDefined(); expect(listActive).toBeDefined(); await activate!.handler({ name: "alpha" }, {} as any); expect(await listActive!.handler({}, {} as any)).toEqual({ activeSkills: ["alpha"] }); // Update the skill metadata — the skill keeps the same name so it stays active await writeFile( join(dir, "skills", "alpha", "SKILL.md"), `--- name: alpha description: Alpha skill updated --- # Alpha Updated `, "utf8", ); await (harness as any).refreshSkillsIfChanged(); expect(await listActive!.handler({}, {} as any)).toEqual({ activeSkills: ["alpha"] }); }); it("lists skill scripts through list_skill_scripts", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-skill-script-list-")); await writeFile( join(dir, "AGENT.md"), `--- name: skill-script-list-agent model: provider: anthropic name: claude-opus-4-5 --- # Skill Script List Agent `, "utf8", ); await mkdir(join(dir, "skills", "math", "scripts", "nested"), { recursive: true }); await writeFile( join(dir, "skills", "math", "SKILL.md"), `--- name: math description: Simple math scripts --- # Math Skill `, "utf8", ); await writeFile( join(dir, "skills", "math", "scripts", "add.ts"), "export default async function run() { return { ok: true }; }\n", "utf8", ); await writeFile( join(dir, "skills", "math", "scripts", "nested", "multiply.js"), "export async function run() { return { ok: true }; }\n", "utf8", ); await writeFile( join(dir, "skills", "math", "fetch-page.ts"), "export default async function run() { return { ok: true, root: true }; }\n", "utf8", ); await writeFile( join(dir, "skills", "math", "scripts", "README.md"), "# not executable\n", "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const listScripts = harness.listTools().find((tool) => tool.name === "list_skill_scripts"); expect(listScripts).toBeDefined(); const result = await listScripts!.handler({ skill: "math" }, stubContext); expect(result).toEqual({ skill: "math", scripts: ["./fetch-page.ts", "scripts/add.ts", "scripts/nested/multiply.js"], }); }); it("runs JavaScript/TypeScript skill scripts through run_skill_script", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-skill-script-runner-")); await writeFile( join(dir, "AGENT.md"), `--- name: skill-script-runner-agent model: provider: anthropic name: claude-opus-4-5 --- # Skill Script Runner Agent `, "utf8", ); await mkdir(join(dir, "skills", "math", "scripts"), { recursive: true }); await writeFile( join(dir, "skills", "math", "SKILL.md"), `--- name: math description: Simple math scripts --- # Math Skill `, "utf8", ); await writeFile( join(dir, "skills", "math", "scripts", "add.ts"), `export default async function run(input) { const a = Number(input?.a ?? 0); const b = Number(input?.b ?? 0); return { sum: a + b }; } `, "utf8", ); await writeFile( join(dir, "skills", "math", "fetch-page.ts"), `export default async function run() { return { kind: "root-script" }; } `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const runner = harness.listTools().find((tool) => tool.name === "run_skill_script"); expect(runner).toBeDefined(); const result = await runner!.handler({ skill: "math", script: "scripts/add.ts", input: { a: 2, b: 3 }, }, stubContext); expect(result).toEqual({ skill: "math", script: "./scripts/add.ts", output: { sum: 5 }, }); const rootResult = await runner!.handler({ skill: "math", script: "./fetch-page.ts", }, stubContext); expect(rootResult).toEqual({ skill: "math", script: "./fetch-page.ts", output: { kind: "root-script" }, }); }); it("runs AGENT-scope scripts from root scripts directory", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-root-script-run-")); await mkdir(join(dir, "scripts"), { recursive: true }); await writeFile( join(dir, "scripts", "ping.ts"), "export default async function run() { return { pong: true }; }\n", "utf8", ); await writeFile( join(dir, "AGENT.md"), `--- name: root-run-agent model: provider: anthropic name: claude-opus-4-5 --- # Root Run Agent `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const runner = harness.listTools().find((tool) => tool.name === "run_skill_script"); expect(runner).toBeDefined(); const result = await runner!.handler({ script: "scripts/ping.ts" }, stubContext); expect(result).toEqual({ skill: null, script: "./scripts/ping.ts", output: { pong: true }, }); }); it("blocks path traversal in run_skill_script", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-skill-script-path-")); await writeFile( join(dir, "AGENT.md"), `--- name: skill-script-path-agent model: provider: anthropic name: claude-opus-4-5 --- # Skill Script Path Agent `, "utf8", ); await mkdir(join(dir, "skills", "safe", "scripts"), { recursive: true }); await writeFile( join(dir, "skills", "safe", "SKILL.md"), `--- name: safe description: Safe skill --- # Safe Skill `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const runner = harness.listTools().find((tool) => tool.name === "run_skill_script"); expect(runner).toBeDefined(); const result = await runner!.handler({ skill: "safe", script: "../outside.ts", }, stubContext); expect(result).toMatchObject({ error: expect.stringContaining("Expected a relative path"), }); }); it("requires allowed-tools entries for non-standard script directories", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-script-allowed-tools-")); await writeFile( join(dir, "AGENT.md"), `--- name: script-policy-agent model: provider: anthropic name: claude-opus-4-5 --- # Script Policy Agent `, "utf8", ); await mkdir(join(dir, "skills", "math", "scripts"), { recursive: true }); await mkdir(join(dir, "skills", "math", "tools"), { recursive: true }); await writeFile( join(dir, "skills", "math", "SKILL.md"), `--- name: math description: Math scripts allowed-tools: - ./tools/multiply.ts --- # Math `, "utf8", ); await writeFile( join(dir, "skills", "math", "scripts", "add.ts"), "export default async function run() { return { ok: true }; }\n", "utf8", ); await writeFile( join(dir, "skills", "math", "tools", "multiply.ts"), "export default async function run() { return { ok: true, kind: 'tools' }; }\n", "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const listScripts = harness.listTools().find((tool) => tool.name === "list_skill_scripts"); const runScript = harness.listTools().find((tool) => tool.name === "run_skill_script"); expect(listScripts).toBeDefined(); expect(runScript).toBeDefined(); const listed = await listScripts!.handler({ skill: "math" }, stubContext); expect(listed).toEqual({ skill: "math", scripts: ["scripts/add.ts", "tools/multiply.ts"], }); const result = await runScript!.handler({ skill: "math", script: "scripts/add.ts" }, stubContext); expect(result).toMatchObject({ output: { ok: true } }); const toolsResult = await runScript!.handler({ skill: "math", script: "./tools/multiply.ts", }, stubContext); expect(toolsResult).toMatchObject({ output: { ok: true, kind: "tools" } }); }); it("parses spec-style allowed-tools from SKILL.md frontmatter", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-allowed-tools-")); await mkdir(join(dir, "skills", "summarize"), { recursive: true }); await writeFile( join(dir, "skills", "summarize", "SKILL.md"), `--- name: summarize description: Summarize text allowed-tools: - mcp:linear/list_issues - mcp:linear/get_issue --- # Summarize `, "utf8", ); const metadata = await loadSkillMetadata(dir); expect(metadata).toHaveLength(1); expect(metadata[0]?.name).toBe("summarize"); expect(metadata[0]?.allowedTools.mcp).toEqual(["linear/list_issues", "linear/get_issue"]); expect(metadata[0]?.allowedTools.scripts).toEqual([]); expect(metadata[0]?.approvalRequired.mcp).toEqual([]); expect(metadata[0]?.approvalRequired.scripts).toEqual([]); }); it("parses approval-required patterns from SKILL.md frontmatter", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-approval-required-")); await mkdir(join(dir, "skills", "triage"), { recursive: true }); await writeFile( join(dir, "skills", "triage", "SKILL.md"), `--- name: triage description: Triage allowed-tools: - mcp:github/list_issues - mcp:github/create_issue - ./tools/open-pr.ts approval-required: - mcp:github/create_issue - ./scripts/review.ts - ./tools/open-pr.ts --- # Triage `, "utf8", ); const metadata = await loadSkillMetadata(dir); expect(metadata[0]?.approvalRequired.mcp).toEqual(["github/create_issue"]); expect(metadata[0]?.approvalRequired.scripts).toEqual([ "./scripts/review.ts", "./tools/open-pr.ts", ]); }); it("fails when SKILL.md includes invalid non-slash tool patterns", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-invalid-tools-")); await mkdir(join(dir, "skills", "legacy"), { recursive: true }); await writeFile( join(dir, "skills", "legacy", "SKILL.md"), `--- name: legacy description: Legacy skill allowed-tools: - mcp:legacy_tool --- # Legacy `, "utf8", ); await expect(loadSkillMetadata(dir)).rejects.toThrow( /Invalid MCP tool pattern/, ); }); it("registers MCP tools dynamically for stacked active skills and supports deactivation", async () => { process.env.LINEAR_TOKEN = "token-123"; const mcpServer = createServer(async (req, res) => { if (req.method === "DELETE") { res.statusCode = 200; res.end(); return; } const chunks: Buffer[] = []; for await (const chunk of req) chunks.push(Buffer.from(chunk)); const body = Buffer.concat(chunks).toString("utf8"); const payload = body.trim().length > 0 ? (JSON.parse(body) as any) : {}; if (payload.method === "initialize") { res.setHeader("Content-Type", "application/json"); res.setHeader("Mcp-Session-Id", "sess"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { protocolVersion: "2025-03-26", capabilities: { tools: { listChanged: true } }, serverInfo: { name: "remote", version: "1.0.0" }, }, }), ); return; } if (payload.method === "notifications/initialized") { res.statusCode = 202; res.end(); return; } if (payload.method === "tools/list") { res.setHeader("Content-Type", "application/json"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { tools: [ { name: "a", inputSchema: { type: "object", properties: {} } }, { name: "b", inputSchema: { type: "object", properties: {} } }, ], }, }), ); return; } if (payload.method === "tools/call") { res.setHeader("Content-Type", "application/json"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { result: { ok: true } }, }), ); return; } res.statusCode = 404; res.end(); }); await new Promise((resolveOpen) => mcpServer.listen(0, () => resolveOpen())); const address = mcpServer.address(); if (!address || typeof address === "string") throw new Error("Unexpected address"); const dir = await mkdtemp(join(tmpdir(), "poncho-harness-stacked-activation-")); await writeFile( join(dir, "AGENT.md"), `--- name: stacked-agent model: provider: anthropic name: claude-opus-4-5 --- # Stacked Agent `, "utf8", ); await writeFile( join(dir, "poncho.config.js"), `export default { mcp: [ { name: "remote", url: "http://127.0.0.1:${address.port}/mcp", auth: { type: "bearer", tokenEnv: "LINEAR_TOKEN" } } ] }; `, "utf8", ); await mkdir(join(dir, "skills", "skill-a"), { recursive: true }); await mkdir(join(dir, "skills", "skill-b"), { recursive: true }); await writeFile( join(dir, "skills", "skill-a", "SKILL.md"), `--- name: skill-a description: A allowed-tools: - mcp:remote/a --- # A `, "utf8", ); await writeFile( join(dir, "skills", "skill-b", "SKILL.md"), `--- name: skill-b description: B allowed-tools: - mcp:remote/b --- # B `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); expect(harness.listTools().map((tool) => tool.name)).not.toContain("remote/a"); expect(harness.listTools().map((tool) => tool.name)).not.toContain("remote/b"); const activate = harness.listTools().find((tool) => tool.name === "activate_skill"); const deactivate = harness.listTools().find((tool) => tool.name === "deactivate_skill"); expect(activate).toBeDefined(); expect(deactivate).toBeDefined(); await activate!.handler({ name: "skill-a" }, {} as any); expect(harness.listTools().map((tool) => tool.name)).toContain("remote/a"); expect(harness.listTools().map((tool) => tool.name)).not.toContain("remote/b"); await activate!.handler({ name: "skill-b" }, {} as any); const afterStack = harness.listTools().map((tool) => tool.name); expect(afterStack).toContain("remote/a"); expect(afterStack).toContain("remote/b"); await deactivate!.handler({ name: "skill-a" }, {} as any); const afterDeactivate = harness.listTools().map((tool) => tool.name); expect(afterDeactivate).not.toContain("remote/a"); expect(afterDeactivate).toContain("remote/b"); await harness.shutdown(); await new Promise((resolveClose) => mcpServer.close(() => resolveClose())); }); it("agent-level MCP tools persist when a skill is activated (additive)", async () => { process.env.LINEAR_TOKEN = "token-123"; const mcpServer = createServer(async (req, res) => { if (req.method === "DELETE") { res.statusCode = 200; res.end(); return; } const chunks: Buffer[] = []; for await (const chunk of req) chunks.push(Buffer.from(chunk)); const body = Buffer.concat(chunks).toString("utf8"); const payload = body.trim().length > 0 ? (JSON.parse(body) as any) : {}; if (payload.method === "initialize") { res.setHeader("Content-Type", "application/json"); res.setHeader("Mcp-Session-Id", "sess"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { protocolVersion: "2025-03-26", capabilities: { tools: { listChanged: true } }, serverInfo: { name: "remote", version: "1.0.0" }, }, }), ); return; } if (payload.method === "notifications/initialized") { res.statusCode = 202; res.end(); return; } if (payload.method === "tools/list") { res.setHeader("Content-Type", "application/json"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { tools: [ { name: "a", inputSchema: { type: "object", properties: {} } }, { name: "b", inputSchema: { type: "object", properties: {} } }, ], }, }), ); return; } if (payload.method === "tools/call") { res.setHeader("Content-Type", "application/json"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { result: { ok: true } }, }), ); return; } res.statusCode = 404; res.end(); }); await new Promise((resolveOpen) => mcpServer.listen(0, () => resolveOpen())); const address = mcpServer.address(); if (!address || typeof address === "string") throw new Error("Unexpected address"); const dir = await mkdtemp(join(tmpdir(), "poncho-harness-additive-mcp-")); await writeFile( join(dir, "AGENT.md"), `--- name: additive-agent model: provider: anthropic name: claude-opus-4-5 allowed-tools: - mcp:remote/a --- # Additive Agent `, "utf8", ); await writeFile( join(dir, "poncho.config.js"), `export default { mcp: [ { name: "remote", url: "http://127.0.0.1:${address.port}/mcp", auth: { type: "bearer", tokenEnv: "LINEAR_TOKEN" } } ] }; `, "utf8", ); await mkdir(join(dir, "skills", "skill-b"), { recursive: true }); await writeFile( join(dir, "skills", "skill-b", "SKILL.md"), `--- name: skill-b description: B allowed-tools: - mcp:remote/b --- # B `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const toolNames = () => harness.listTools().map((t) => t.name); expect(toolNames()).toContain("remote/a"); expect(toolNames()).not.toContain("remote/b"); const activate = harness.listTools().find((t) => t.name === "activate_skill")!; const deactivate = harness.listTools().find((t) => t.name === "deactivate_skill")!; await activate.handler({ name: "skill-b" }, {} as any); expect(toolNames()).toContain("remote/a"); expect(toolNames()).toContain("remote/b"); await deactivate.handler({ name: "skill-b" }, {} as any); expect(toolNames()).toContain("remote/a"); expect(toolNames()).not.toContain("remote/b"); await harness.shutdown(); await new Promise((resolveClose) => mcpServer.close(() => resolveClose())); }); it("unclaimed MCP tools are globally available without allowed-tools declaration", async () => { process.env.LINEAR_TOKEN = "token-123"; const mcpServer = createServer(async (req, res) => { if (req.method === "DELETE") { res.statusCode = 200; res.end(); return; } const chunks: Buffer[] = []; for await (const chunk of req) chunks.push(Buffer.from(chunk)); const body = Buffer.concat(chunks).toString("utf8"); const payload = body.trim().length > 0 ? (JSON.parse(body) as any) : {}; if (payload.method === "initialize") { res.setHeader("Content-Type", "application/json"); res.setHeader("Mcp-Session-Id", "sess"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { protocolVersion: "2025-03-26", capabilities: { tools: { listChanged: true } }, serverInfo: { name: "remote", version: "1.0.0" }, }, }), ); return; } if (payload.method === "notifications/initialized") { res.statusCode = 202; res.end(); return; } if (payload.method === "tools/list") { res.setHeader("Content-Type", "application/json"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { tools: [ { name: "list_issues", inputSchema: { type: "object", properties: {} } }, { name: "save_issue", inputSchema: { type: "object", properties: {} } }, ], }, }), ); return; } res.statusCode = 404; res.end(); }); await new Promise((resolveOpen) => mcpServer.listen(0, () => resolveOpen())); const address = mcpServer.address(); if (!address || typeof address === "string") throw new Error("Unexpected address"); const dir = await mkdtemp(join(tmpdir(), "poncho-harness-unclaimed-mcp-")); // AGENT.md with no allowed-tools and no skills — MCP tools should be globally available await writeFile( join(dir, "AGENT.md"), `--- name: unclaimed-mcp-agent model: provider: anthropic name: claude-opus-4-5 --- # Agent with unclaimed MCP tools `, "utf8", ); await writeFile( join(dir, "poncho.config.js"), `export default { mcp: [ { name: "linear", url: "http://127.0.0.1:${address.port}/mcp", auth: { type: "bearer", tokenEnv: "LINEAR_TOKEN" } } ] }; `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const toolNames = () => harness.listTools().map((t) => t.name); // Unclaimed tools should be globally available expect(toolNames()).toContain("linear/list_issues"); expect(toolNames()).toContain("linear/save_issue"); await harness.shutdown(); await new Promise((resolveClose) => mcpServer.close(() => resolveClose())); }); it("claiming any tool from a server scopes the entire server", async () => { process.env.LINEAR_TOKEN = "token-123"; const mcpServer = createServer(async (req, res) => { if (req.method === "DELETE") { res.statusCode = 200; res.end(); return; } const chunks: Buffer[] = []; for await (const chunk of req) chunks.push(Buffer.from(chunk)); const body = Buffer.concat(chunks).toString("utf8"); const payload = body.trim().length > 0 ? (JSON.parse(body) as any) : {}; if (payload.method === "initialize") { res.setHeader("Content-Type", "application/json"); res.setHeader("Mcp-Session-Id", "sess"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { protocolVersion: "2025-03-26", capabilities: { tools: { listChanged: true } }, serverInfo: { name: "remote", version: "1.0.0" }, }, }), ); return; } if (payload.method === "notifications/initialized") { res.statusCode = 202; res.end(); return; } if (payload.method === "tools/list") { res.setHeader("Content-Type", "application/json"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { tools: [ { name: "list_issues", inputSchema: { type: "object", properties: {} } }, { name: "save_issue", inputSchema: { type: "object", properties: {} } }, { name: "other_tool", inputSchema: { type: "object", properties: {} } }, ], }, }), ); return; } if (payload.method === "tools/call") { res.setHeader("Content-Type", "application/json"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { result: { ok: true } }, }), ); return; } res.statusCode = 404; res.end(); }); await new Promise((resolveOpen) => mcpServer.listen(0, () => resolveOpen())); const address = mcpServer.address(); if (!address || typeof address === "string") throw new Error("Unexpected address"); const dir = await mkdtemp(join(tmpdir(), "poncho-harness-server-scoped-")); await writeFile( join(dir, "AGENT.md"), `--- name: server-scoped-agent model: provider: anthropic name: claude-opus-4-5 --- # Server Scoped Agent `, "utf8", ); await writeFile( join(dir, "poncho.config.js"), `export default { mcp: [ { name: "remote", url: "http://127.0.0.1:${address.port}/mcp", auth: { type: "bearer", tokenEnv: "LINEAR_TOKEN" } } ] }; `, "utf8", ); // Skill claims only 2 of the 3 tools from "remote" server await mkdir(join(dir, "skills", "linear"), { recursive: true }); await writeFile( join(dir, "skills", "linear", "SKILL.md"), `--- name: linear description: Linear issue tracking allowed-tools: - mcp:remote/list_issues - mcp:remote/save_issue --- # Linear Skill `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const toolNames = () => harness.listTools().map((t) => t.name); // Before activation: entire server is skill-managed, so ALL tools are hidden // (even other_tool which the skill didn't explicitly claim) expect(toolNames()).not.toContain("remote/list_issues"); expect(toolNames()).not.toContain("remote/save_issue"); expect(toolNames()).not.toContain("remote/other_tool"); // Activate the linear skill — only its declared tools appear const activate = harness.listTools().find((t) => t.name === "activate_skill")!; const deactivate = harness.listTools().find((t) => t.name === "deactivate_skill")!; await activate.handler({ name: "linear" }, {} as any); expect(toolNames()).toContain("remote/list_issues"); expect(toolNames()).toContain("remote/save_issue"); // other_tool is NOT claimed by any active skill, and the server is managed expect(toolNames()).not.toContain("remote/other_tool"); // Deactivate — all tools from the managed server hidden again await deactivate.handler({ name: "linear" }, {} as any); expect(toolNames()).not.toContain("remote/list_issues"); expect(toolNames()).not.toContain("remote/save_issue"); expect(toolNames()).not.toContain("remote/other_tool"); await harness.shutdown(); await new Promise((resolveClose) => mcpServer.close(() => resolveClose())); }); it("wildcard skill claim scopes all tools from that server", async () => { process.env.LINEAR_TOKEN = "token-123"; const mcpServer = createServer(async (req, res) => { if (req.method === "DELETE") { res.statusCode = 200; res.end(); return; } const chunks: Buffer[] = []; for await (const chunk of req) chunks.push(Buffer.from(chunk)); const body = Buffer.concat(chunks).toString("utf8"); const payload = body.trim().length > 0 ? (JSON.parse(body) as any) : {}; if (payload.method === "initialize") { res.setHeader("Content-Type", "application/json"); res.setHeader("Mcp-Session-Id", "sess"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { protocolVersion: "2025-03-26", capabilities: { tools: { listChanged: true } }, serverInfo: { name: "linear", version: "1.0.0" }, }, }), ); return; } if (payload.method === "notifications/initialized") { res.statusCode = 202; res.end(); return; } if (payload.method === "tools/list") { res.setHeader("Content-Type", "application/json"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { tools: [ { name: "list_issues", inputSchema: { type: "object", properties: {} } }, { name: "save_issue", inputSchema: { type: "object", properties: {} } }, { name: "save_comment", inputSchema: { type: "object", properties: {} } }, ], }, }), ); return; } res.statusCode = 404; res.end(); }); await new Promise((resolveOpen) => mcpServer.listen(0, () => resolveOpen())); const address = mcpServer.address(); if (!address || typeof address === "string") throw new Error("Unexpected address"); const dir = await mkdtemp(join(tmpdir(), "poncho-harness-wildcard-claim-")); await writeFile( join(dir, "AGENT.md"), `--- name: wildcard-agent model: provider: anthropic name: claude-opus-4-5 --- # Wildcard Agent `, "utf8", ); await writeFile( join(dir, "poncho.config.js"), `export default { mcp: [ { name: "linear", url: "http://127.0.0.1:${address.port}/mcp", auth: { type: "bearer", tokenEnv: "LINEAR_TOKEN" } } ] }; `, "utf8", ); // Skill claims all linear tools with wildcard await mkdir(join(dir, "skills", "linear"), { recursive: true }); await writeFile( join(dir, "skills", "linear", "SKILL.md"), `--- name: linear description: Linear integration allowed-tools: - mcp:linear/* --- # Linear Skill `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const toolNames = () => harness.listTools().map((t) => t.name); // None of the linear tools should be available (all claimed by wildcard) expect(toolNames()).not.toContain("linear/list_issues"); expect(toolNames()).not.toContain("linear/save_issue"); expect(toolNames()).not.toContain("linear/save_comment"); // Activate the skill const activate = harness.listTools().find((t) => t.name === "activate_skill")!; await activate.handler({ name: "linear" }, {} as any); // All linear tools now available expect(toolNames()).toContain("linear/list_issues"); expect(toolNames()).toContain("linear/save_issue"); expect(toolNames()).toContain("linear/save_comment"); await harness.shutdown(); await new Promise((resolveClose) => mcpServer.close(() => resolveClose())); }); it("supports flat tool access config format", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-flat-tool-access-")); await writeFile( join(dir, "AGENT.md"), `--- name: flat-tool-access-agent model: provider: anthropic name: claude-opus-4-5 --- # Flat Tool Access Agent `, "utf8", ); await writeFile( join(dir, "poncho.config.js"), `export default { tools: { web_search: false, web_fetch: true, } }; `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const names = harness.listTools().map((tool) => tool.name); expect(names).toContain("web_fetch"); expect(names).not.toContain("web_search"); }); it("flat tool access takes priority over legacy defaults", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-flat-over-legacy-")); await writeFile( join(dir, "AGENT.md"), `--- name: flat-over-legacy-agent model: provider: anthropic name: claude-opus-4-5 --- # Flat Over Legacy Agent `, "utf8", ); await writeFile( join(dir, "poncho.config.js"), `export default { tools: { web_search: true, defaults: { web_search: false, }, } }; `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const names = harness.listTools().map((tool) => tool.name); expect(names).toContain("web_search"); }); it("byEnvironment overrides flat tool access", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-env-override-flat-")); await writeFile( join(dir, "AGENT.md"), `--- name: env-override-flat-agent model: provider: anthropic name: claude-opus-4-5 --- # Env Override Flat Agent `, "utf8", ); await writeFile( join(dir, "poncho.config.js"), `export default { tools: { web_search: false, byEnvironment: { development: { web_search: true, }, }, } }; `, "utf8", ); const harness = new AgentHarness({ workingDir: dir, environment: "development" }); await harness.initialize(); const names = harness.listTools().map((tool) => tool.name); expect(names).toContain("web_search"); }); it("registerTools skips tools disabled via config", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-register-tools-disabled-")); await writeFile( join(dir, "AGENT.md"), `--- name: register-tools-disabled-agent model: provider: anthropic name: claude-opus-4-5 --- # Register Tools Disabled Agent `, "utf8", ); await writeFile( join(dir, "poncho.config.js"), `export default { tools: { custom_tool: false, } }; `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); harness.registerTools([ { name: "custom_tool", description: "should be skipped", inputSchema: { type: "object", properties: {} }, handler: async () => ({ ok: true }), }, { name: "allowed_tool", description: "should be registered", inputSchema: { type: "object", properties: {} }, handler: async () => ({ ok: true }), }, ]); const names = harness.listTools().map((tool) => tool.name); expect(names).not.toContain("custom_tool"); expect(names).toContain("allowed_tool"); }); it("approval access level registers the tool but marks it for approval", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-approval-access-")); await writeFile( join(dir, "AGENT.md"), `--- name: approval-access-agent model: provider: anthropic name: claude-opus-4-5 --- # Approval Access Agent `, "utf8", ); await writeFile( join(dir, "poncho.config.js"), `export default { tools: { web_search: 'approval', } }; `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const names = harness.listTools().map((tool) => tool.name); expect(names).toContain("web_search"); const requiresApproval = (harness as any).requiresApprovalForToolCall("web_search", {}); expect(requiresApproval).toBe(true); }); it("tools without approval config do not require approval", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-harness-no-approval-")); await writeFile( join(dir, "AGENT.md"), `--- name: no-approval-agent model: provider: anthropic name: claude-opus-4-5 --- # No Approval Agent `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const requiresApproval = (harness as any).requiresApprovalForToolCall("web_search", {}); expect(requiresApproval).toBe(false); }); it("allows in-flight MCP calls to finish after skill deactivation", async () => { process.env.LINEAR_TOKEN = "token-123"; const mcpServer = createServer(async (req, res) => { if (req.method === "DELETE") { res.statusCode = 200; res.end(); return; } const chunks: Buffer[] = []; for await (const chunk of req) chunks.push(Buffer.from(chunk)); const body = Buffer.concat(chunks).toString("utf8"); const payload = body.trim().length > 0 ? (JSON.parse(body) as any) : {}; if (payload.method === "initialize") { res.setHeader("Content-Type", "application/json"); res.setHeader("Mcp-Session-Id", "sess"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { protocolVersion: "2025-03-26", capabilities: { tools: { listChanged: true } }, serverInfo: { name: "remote", version: "1.0.0" }, }, }), ); return; } if (payload.method === "notifications/initialized") { res.statusCode = 202; res.end(); return; } if (payload.method === "tools/list") { res.setHeader("Content-Type", "application/json"); res.end( JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { tools: [{ name: "slow", inputSchema: { type: "object", properties: {} } }] }, }), ); return; } if (payload.method === "tools/call") { await new Promise((resolveTimer) => setTimeout(resolveTimer, 25)); res.setHeader("Content-Type", "application/json"); res.end(JSON.stringify({ jsonrpc: "2.0", id: payload.id, result: { result: { done: true } } })); return; } res.statusCode = 404; res.end(); }); await new Promise((resolveOpen) => mcpServer.listen(0, () => resolveOpen())); const address = mcpServer.address(); if (!address || typeof address === "string") throw new Error("Unexpected address"); const dir = await mkdtemp(join(tmpdir(), "poncho-harness-inflight-")); await writeFile( join(dir, "AGENT.md"), `--- name: inflight-agent model: provider: anthropic name: claude-opus-4-5 --- # Inflight `, "utf8", ); await writeFile( join(dir, "poncho.config.js"), `export default { mcp: [ { name: "remote", url: "http://127.0.0.1:${address.port}/mcp", auth: { type: "bearer", tokenEnv: "LINEAR_TOKEN" } } ] }; `, "utf8", ); await mkdir(join(dir, "skills", "skill-slow"), { recursive: true }); await writeFile( join(dir, "skills", "skill-slow", "SKILL.md"), `--- name: skill-slow description: Slow allowed-tools: - mcp:remote/slow --- # Slow `, "utf8", ); const harness = new AgentHarness({ workingDir: dir }); await harness.initialize(); const activate = harness.listTools().find((tool) => tool.name === "activate_skill"); const deactivate = harness.listTools().find((tool) => tool.name === "deactivate_skill"); await activate!.handler({ name: "skill-slow" }, {} as any); const slowTool = harness.listTools().find((tool) => tool.name === "remote/slow"); expect(slowTool).toBeDefined(); const inFlight = slowTool!.handler({}, {} as any); await deactivate!.handler({ name: "skill-slow" }, {} as any); const output = await inFlight; expect(output).toEqual({ done: true }); await harness.shutdown(); await new Promise((resolveClose) => mcpServer.close(() => resolveClose())); }); }); describe("edit_file tool", () => { it("replaces a unique string match in a file", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-edit-tool-")); const filePath = join(dir, "test.txt"); await writeFile(filePath, "hello world\nfoo bar\nbaz qux\n", "utf8"); const tool = createEditTool(dir); const result = await tool.handler( { path: "test.txt", old_str: "foo bar", new_str: "replaced" }, stubContext, ); expect(result).toEqual({ path: "test.txt", edited: true }); const content = await readFile(filePath, "utf8"); expect(content).toBe("hello world\nreplaced\nbaz qux\n"); }); it("errors when old_str is not found in the file", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-edit-tool-notfound-")); await writeFile(join(dir, "test.txt"), "hello world\n", "utf8"); const tool = createEditTool(dir); await expect( tool.handler({ path: "test.txt", old_str: "nonexistent", new_str: "x" }, stubContext), ).rejects.toThrow("old_str not found in file"); }); it("errors when old_str matches multiple locations", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-edit-tool-multi-")); await writeFile(join(dir, "test.txt"), "aaa\nbbb\naaa\n", "utf8"); const tool = createEditTool(dir); await expect( tool.handler({ path: "test.txt", old_str: "aaa", new_str: "ccc" }, stubContext), ).rejects.toThrow("old_str appears multiple times"); }); it("deletes matched content when new_str is empty", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-edit-tool-delete-")); const filePath = join(dir, "test.txt"); await writeFile(filePath, "keep this\nremove this\nkeep this too\n", "utf8"); const tool = createEditTool(dir); await tool.handler({ path: "test.txt", old_str: "remove this\n", new_str: "" }, stubContext); const content = await readFile(filePath, "utf8"); expect(content).toBe("keep this\nkeep this too\n"); }); it("errors when old_str is empty", async () => { const dir = await mkdtemp(join(tmpdir(), "poncho-edit-tool-empty-")); await writeFile(join(dir, "test.txt"), "content\n", "utf8"); const tool = createEditTool(dir); await expect( tool.handler({ path: "test.txt", old_str: "", new_str: "x" }, stubContext), ).rejects.toThrow("old_str must not be empty"); }); });