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>
104 lines
3.2 KiB
TypeScript
104 lines
3.2 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { writeFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import type { Effort } from "../external.ts";
|
|
import { markActivity } from "../utils/activity.ts";
|
|
import type { ResolvedInstructions } from "../utils/instructions.ts";
|
|
import { type SubagentState, startSubagentMcpServer, type ToolContext } from "./server.ts";
|
|
|
|
type CreateSubagentParams = {
|
|
ctx: ToolContext;
|
|
mode: string;
|
|
};
|
|
|
|
export function createSubagentState(params: CreateSubagentParams): SubagentState {
|
|
const id = randomUUID();
|
|
const stdoutFilePath = join(params.ctx.tmpdir, `subagent-${id}.log`);
|
|
const state: SubagentState = {
|
|
id,
|
|
status: "running",
|
|
mode: params.mode,
|
|
stdoutFilePath,
|
|
output: undefined,
|
|
usage: undefined,
|
|
startedAt: Date.now(),
|
|
keepAliveInterval: undefined,
|
|
};
|
|
params.ctx.toolState.subagents.set(id, state);
|
|
params.ctx.toolState.activeSubagentId = id;
|
|
return state;
|
|
}
|
|
|
|
type CompleteSubagentParams = {
|
|
ctx: ToolContext;
|
|
subagent: SubagentState;
|
|
success: boolean;
|
|
};
|
|
|
|
function completeSubagent(params: CompleteSubagentParams): void {
|
|
params.subagent.status = params.success ? "completed" : "failed";
|
|
if (params.subagent.keepAliveInterval) {
|
|
clearInterval(params.subagent.keepAliveInterval);
|
|
params.subagent.keepAliveInterval = undefined;
|
|
}
|
|
if (params.subagent.usage) {
|
|
params.ctx.toolState.usageEntries.push(params.subagent.usage);
|
|
}
|
|
params.ctx.toolState.activeSubagentId = undefined;
|
|
// keep completed subagents in the map for post-completion inspection
|
|
}
|
|
|
|
export function buildSubagentInstructions(orchestratorPrompt: string): ResolvedInstructions {
|
|
return {
|
|
full: orchestratorPrompt,
|
|
system: "",
|
|
user: orchestratorPrompt,
|
|
eventInstructions: "",
|
|
repo: "",
|
|
event: "",
|
|
runtime: "",
|
|
};
|
|
}
|
|
|
|
type RunSubagentParams = {
|
|
ctx: ToolContext;
|
|
subagent: SubagentState;
|
|
effort: Effort;
|
|
instructions: string;
|
|
};
|
|
|
|
type RunSubagentResult = {
|
|
success: boolean;
|
|
error: string | undefined;
|
|
};
|
|
|
|
export async function runSubagent(params: RunSubagentParams): Promise<RunSubagentResult> {
|
|
params.subagent.keepAliveInterval = setInterval(markActivity, 30_000);
|
|
const mcpServer = await startSubagentMcpServer(params.ctx);
|
|
try {
|
|
const subagentPayload = { ...params.ctx.payload, effort: params.effort };
|
|
const subagentInstructions = buildSubagentInstructions(params.instructions);
|
|
const result = await params.ctx.agent.run({
|
|
payload: subagentPayload,
|
|
mcpServerUrl: mcpServer.url,
|
|
tmpdir: params.ctx.tmpdir,
|
|
instructions: subagentInstructions,
|
|
});
|
|
params.subagent.usage = result.usage;
|
|
writeFileSync(params.subagent.stdoutFilePath, result.output ?? "", "utf-8");
|
|
completeSubagent({ ctx: params.ctx, subagent: params.subagent, success: result.success });
|
|
return { success: result.success, error: result.error };
|
|
} catch (err) {
|
|
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
try {
|
|
writeFileSync(params.subagent.stdoutFilePath, "", "utf-8");
|
|
} catch {
|
|
// best-effort
|
|
}
|
|
completeSubagent({ ctx: params.ctx, subagent: params.subagent, success: false });
|
|
return { success: false, error: errorMessage };
|
|
} finally {
|
|
await mcpServer.stop();
|
|
}
|
|
}
|