cfd38d82fc
* refactor delegation system and add PR summary comments Delegation system: - replace mode-based delegation with select_mode → delegate two-step flow - orchestrator crafts self-contained subagent prompts (clean context — no system/repo/event instructions leak) - add role-based tool filtering via FastMCP authenticate hook (?role=subagent hides orchestrator-only tools) - add select_mode tool for orchestrator guidance per mode - add ask_question tool for lightweight research subagents - extract shared subagent lifecycle into subagent.ts (create, complete, stdout, instructions) - route set_output to per-subagent state when activeSubagentId is set - track per-subagent state (SubagentState Map) replacing boolean delegationActive flag - capture and aggregate AgentUsage across all agents (claude, codex, gemini, opencode) - write usage summary table to GitHub job summary - block built-in subagent spawning (Task for Claude, Task(*) for Cursor) - increase activity timeout from 60s to 300s (subagent thinking phases) - fix gh CLI misguidance in system prompt — explicitly forbid usage PR summary comments: - add prSummaryComment trigger (DB schema + migrations + Zod + UI toggle) - dispatch mini-effort summary job alongside PR review on pr.created - add update_pull_request_body MCP tool - add defaultEffort option to webhook dispatch Hardening: - rewrite delegate/selectMode tests with simulated state management - add toolFiltering.test.ts for role extraction, canAccess, set_output routing - remove non-null assertions for PULLFROG_TEMP_DIR (proper error throws) - use fetchWithRetry for direct tarball downloads - DRY fix for rate limit check in test runner Co-authored-by: Cursor <cursoragent@cursor.com> * fix: add type keyword to Effort import in handleWebhook.ts Co-authored-by: Cursor <cursoragent@cursor.com> * clean up delegation system, improve code quality across the codebase - simplify delegate tool to instructions + effort params with subagent lifecycle in subagent.ts - add select_mode and ask_question orchestrator-only tools with canAccess filtering - replace delegate.test.ts/selectMode.test.ts with toolFiltering.test.ts (live MCP integration) - add set_output routing for subagent context and AgentUsage tracking across all agents - add PR summary comment trigger (schema, UI, webhook dispatch with silent flag) - add update_pull_request_body MCP tool - fix changed-agents.sh to always include claude canary for non-agent action changes - fix cursor pagination bug in getSelectedInstallationReposPage - remove destructuring patterns, inline type definitions, and unsafe type casts - replace non-null assertions with explicit checks in install.ts - convert multi-param functions to single param objects (postCleanup, runActionLocal, etc.) - use isHttpError helper in API routes instead of catch-any patterns - add adhoc test fixtures for delegation scenarios (context isolation, error handling, synthesis, etc.) Co-authored-by: Cursor <cursoragent@cursor.com> * no subagent mutation, one mcp per subagent * address review feedback: parallel-safe usage tracking, subagent isolation, minor improvements * fix subagent state isolation: replace Object.freeze with shallow copy Object.freeze throws TypeErrors when subagent tools (checkout_pr, report_progress) write scalar properties to toolState. A shallow copy achieves the same isolation for scalar fields while allowing tools to work normally. Shared references (subagents Map, usageEntries array) remain shared for coordination. --------- Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: pullfrog[bot] <226033991+pullfrog[bot]@users.noreply.github.com>
168 lines
6.5 KiB
TypeScript
168 lines
6.5 KiB
TypeScript
import { createServer } from "node:net";
|
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
import { type } from "arktype";
|
|
import { FastMCP } from "fastmcp";
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
import { ORCHESTRATOR_ONLY_TOOLS } from "./server.ts";
|
|
import { execute, tool } from "./shared.ts";
|
|
import { buildSubagentInstructions } from "./subagent.ts";
|
|
|
|
// ─── unit tests for pure exported functions ─────────────────────────────
|
|
|
|
describe("ORCHESTRATOR_ONLY_TOOLS", () => {
|
|
it("includes delegation tools", () => {
|
|
expect(ORCHESTRATOR_ONLY_TOOLS).toContain("select_mode");
|
|
expect(ORCHESTRATOR_ONLY_TOOLS).toContain("delegate");
|
|
expect(ORCHESTRATOR_ONLY_TOOLS).toContain("ask_question");
|
|
});
|
|
|
|
it("includes remote-mutating tools", () => {
|
|
expect(ORCHESTRATOR_ONLY_TOOLS).toContain("push_branch");
|
|
expect(ORCHESTRATOR_ONLY_TOOLS).toContain("push_tags");
|
|
expect(ORCHESTRATOR_ONLY_TOOLS).toContain("delete_branch");
|
|
expect(ORCHESTRATOR_ONLY_TOOLS).toContain("create_pull_request");
|
|
expect(ORCHESTRATOR_ONLY_TOOLS).toContain("update_pull_request_body");
|
|
});
|
|
});
|
|
|
|
describe("buildSubagentInstructions", () => {
|
|
it("returns clean-room instructions with only the orchestrator prompt", () => {
|
|
const prompt = "Read file.ts and fix the type error.";
|
|
const instructions = buildSubagentInstructions(prompt);
|
|
expect(instructions).toEqual({
|
|
full: prompt,
|
|
system: "",
|
|
user: prompt,
|
|
eventInstructions: "",
|
|
repo: "",
|
|
event: "",
|
|
runtime: "",
|
|
});
|
|
});
|
|
});
|
|
|
|
// ─── per-server tool isolation integration test ─────────────────────────
|
|
// demonstrates the architecture: orchestrator and subagent get separate servers
|
|
|
|
function getRandomPort(): Promise<number> {
|
|
return new Promise((resolve, reject) => {
|
|
const srv = createServer();
|
|
srv.listen(0, "127.0.0.1", () => {
|
|
const addr = srv.address();
|
|
if (!addr || typeof addr === "string") return reject(new Error("bad address"));
|
|
const port = addr.port;
|
|
srv.close(() => resolve(port));
|
|
});
|
|
});
|
|
}
|
|
|
|
async function connectMcpClient(url: string): Promise<Client> {
|
|
const transport = new StreamableHTTPClientTransport(new URL(url));
|
|
const client = new Client({ name: "test-client", version: "0.0.1" });
|
|
// @ts-expect-error — exactOptionalPropertyTypes mismatch: SDK Transport.sessionId?: string vs StreamableHTTPClientTransport getter returning string | undefined
|
|
await client.connect(transport);
|
|
return client;
|
|
}
|
|
|
|
function mockTool(name: string, description: string) {
|
|
return tool({
|
|
name,
|
|
description,
|
|
parameters: type({ value: "string" }),
|
|
execute: execute(async () => ({ ok: true })),
|
|
});
|
|
}
|
|
|
|
describe("per-server tool isolation - integration", () => {
|
|
let orchestratorServer: FastMCP;
|
|
let subagentServer: FastMCP;
|
|
let orchestratorUrl: string;
|
|
let subagentUrl: string;
|
|
const clients: Client[] = [];
|
|
|
|
beforeAll(async () => {
|
|
const [orchestratorPort, subagentPort] = await Promise.all([getRandomPort(), getRandomPort()]);
|
|
orchestratorUrl = `http://127.0.0.1:${orchestratorPort}/mcp`;
|
|
subagentUrl = `http://127.0.0.1:${subagentPort}/mcp`;
|
|
|
|
// orchestrator gets ALL tools (common + delegation + remote mutation)
|
|
orchestratorServer = new FastMCP({ name: "orchestrator", version: "0.0.1" });
|
|
orchestratorServer.addTool(mockTool("file_read", "read a file"));
|
|
orchestratorServer.addTool(mockTool("git", "run git commands"));
|
|
orchestratorServer.addTool(mockTool("set_output", "set output"));
|
|
orchestratorServer.addTool(mockTool("select_mode", "select a mode"));
|
|
orchestratorServer.addTool(mockTool("delegate", "delegate a task"));
|
|
orchestratorServer.addTool(mockTool("ask_question", "ask a question"));
|
|
orchestratorServer.addTool(mockTool("push_branch", "push branch"));
|
|
orchestratorServer.addTool(mockTool("create_pull_request", "create PR"));
|
|
|
|
// subagent gets ONLY common tools (no delegation, no remote mutation)
|
|
subagentServer = new FastMCP({ name: "subagent", version: "0.0.1" });
|
|
subagentServer.addTool(mockTool("file_read", "read a file"));
|
|
subagentServer.addTool(mockTool("git", "run git commands"));
|
|
subagentServer.addTool(mockTool("set_output", "set output"));
|
|
|
|
await Promise.all([
|
|
orchestratorServer.start({
|
|
transportType: "httpStream",
|
|
httpStream: { port: orchestratorPort, host: "127.0.0.1", endpoint: "/mcp" },
|
|
}),
|
|
subagentServer.start({
|
|
transportType: "httpStream",
|
|
httpStream: { port: subagentPort, host: "127.0.0.1", endpoint: "/mcp" },
|
|
}),
|
|
]);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
for (const client of clients) {
|
|
try {
|
|
await client.close();
|
|
} catch {
|
|
// best-effort cleanup
|
|
}
|
|
}
|
|
await Promise.all([orchestratorServer.stop(), subagentServer.stop()]);
|
|
});
|
|
|
|
it("orchestrator sees all tools including delegation and mutation", async () => {
|
|
const client = await connectMcpClient(orchestratorUrl);
|
|
clients.push(client);
|
|
const result = await client.listTools();
|
|
const names = result.tools.map((t) => t.name);
|
|
expect(names).toContain("select_mode");
|
|
expect(names).toContain("delegate");
|
|
expect(names).toContain("ask_question");
|
|
expect(names).toContain("push_branch");
|
|
expect(names).toContain("create_pull_request");
|
|
expect(names).toContain("file_read");
|
|
expect(names).toContain("git");
|
|
expect(names).toContain("set_output");
|
|
expect(names.length).toBe(8);
|
|
});
|
|
|
|
it("subagent cannot see delegation or mutation tools", async () => {
|
|
const client = await connectMcpClient(subagentUrl);
|
|
clients.push(client);
|
|
const result = await client.listTools();
|
|
const names = result.tools.map((t) => t.name);
|
|
expect(names).not.toContain("select_mode");
|
|
expect(names).not.toContain("delegate");
|
|
expect(names).not.toContain("ask_question");
|
|
expect(names).not.toContain("push_branch");
|
|
expect(names).not.toContain("create_pull_request");
|
|
});
|
|
|
|
it("subagent sees only common tools", async () => {
|
|
const client = await connectMcpClient(subagentUrl);
|
|
clients.push(client);
|
|
const result = await client.listTools();
|
|
const names = result.tools.map((t) => t.name);
|
|
expect(names).toContain("file_read");
|
|
expect(names).toContain("git");
|
|
expect(names).toContain("set_output");
|
|
expect(names.length).toBe(3);
|
|
});
|
|
});
|