import { setup as setupProfileScenario, done as profileDone } from "./subagent-profile-scenario.js"; import { createRecordingSubagentSessionStore } from "./subagent-test-store.js"; import { describe, expect, it, vi } from "vitest"; import { createTestModelSelection } from "./test-model-selection.js"; import { mkdtemp, readFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { CoreErrorType, type HttpClientPort, type McpPort, type SkillContent, type SkillLoadOutcome, type SkillPort, SEND_MESSAGE_TOOL_NAME, RESPOND_TO_COORDINATOR_TOOL_NAME, SessionEventType, type SessionId, type SessionEvent, type SessionStorePort, createModelId, createModelProviderId, createSessionId, createToolCallId, createTraceId, createTurnId, isCoreError, modelMessageContentToText, } from "@zcode/contracts"; import { createTestAgentRuntime } from "./test-agent-runtime.js"; import { toMcpToolName } from "../src/mcp/index.js"; import { PermissionService } from "../src/permission/index.js"; import { createExploreSubagentPort } from "../src/subagent/runner.js"; import { createTestSessionEventStore } from "./test-event-store.js"; import { createTestModelFactory, type TestModelExecutionObservation, } from "./test-runtime-model.js"; const exploreAgentPrompt = `You are ZCode Explore, a file search and codebase research specialist for ZCode CLI. You excel at thoroughly navigating and exploring codebases. === CRITICAL: READ-ONLY MODE - NO FILE MODIFICATIONS === This is a READ-ONLY exploration task. You are STRICTLY PROHIBITED from: - Creating new files (no Write, touch, or file creation of any kind) - Modifying existing files (no Edit operations) - Deleting files (no rm or deletion) - Moving or copying files (no mv or cp) - Creating temporary files anywhere, including /tmp - Using redirect operators (>, >>, |) or heredocs to write to files - Running ANY commands that change system state Your role is EXCLUSIVELY to search and analyze existing code. You do NOT have access to file editing tools - attempting to edit files will fail. Your strengths: - Rapidly finding files using glob patterns - Searching code and text with powerful regex patterns - Reading and analyzing file contents Guidelines: - Use \`find\` via Bash for broad file pattern matching - Use \`grep\` via Bash for searching file contents with regex - Use Read when you know the specific file path you need to read - Use Bash ONLY for read-only operations (ls, git status, git log, git diff, find, grep, cat, head, tail) - NEVER use Bash for: mkdir, touch, rm, cp, mv, git add, git commit, npm install, pip install, or any file creation/modification - Adapt your search approach based on the thoroughness level specified by the caller - Communicate your final report directly as a regular message - do NOT attempt to create files NOTE: You are meant to be a fast agent that returns output as quickly as possible. In order to achieve this you must: - Make efficient use of the tools that you have at your disposal: be smart about how you search for files and implementations - Wherever possible you should try to spawn multiple parallel tool calls for grepping and reading files Complete the user's search request efficiently and report your findings clearly.`; const subagentCommonNotes = `Notes: - Agent threads always have their cwd reset between bash calls, as a result please only use absolute file paths. - In your final response, share file paths (always absolute, never relative) that are relevant to the task. Include code snippets only when the exact text is load-bearing (e.g., a bug you found, a function signature the caller asked for) — do not recap code you merely read. - For clear communication with the user the assistant MUST avoid using emojis. - Do not use a colon before tool calls. Text like "Let me read the file:" followed by a read tool call should just be "Let me read the file." with a period. - Do NOT Write report/summary/findings/analysis .md files. Return findings directly as your final assistant message — the parent agent reads your text output, not files you create.`; const subagentEnvironmentContext = `Here is useful information about the environment you are running in: Working directory: /Users/dev/Desktop/Z/z-code/apps/zcode-cli Is directory a git repo: Yes Platform: darwin Shell: Git Bash OS Version: Darwin 24.3.0 You are powered by the model named anthropic/claude-haiku-4-5-20251001-cc.`; const exploreSystemMessages = [ "You are ZCode, an interactive coding agent", // 正文已合并为一段;保留原 prompt、Notes、环境及换行顺序的逐字校验。 `\n${exploreAgentPrompt}\n\n${subagentCommonNotes}\n\n${subagentEnvironmentContext}`, ]; async function waitForCondition(check: () => boolean, timeoutMs = 1_000): Promise { const startedAt = Date.now(); while (!check()) { if (Date.now() - startedAt > timeoutMs) { throw new Error("Timed out waiting for condition"); } await new Promise((resolve) => setTimeout(resolve, 5)); } } describe("Explore subagent", () => { it("fails a foreground run after the child runner stays inactive", async () => { const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-inactive-")); const emittedEvents: any[] = []; const port = createExploreSubagentPort({ createAgentId: () => "agent_inactive", inactivityTimeoutMs: 20, outputRootDir, async emitParentEvent(event) { emittedEvents.push(event); }, runExploreAgent: async (request) => { await request.onSessionReady?.(); return new Promise(() => {}); }, }); const runPromise = port .run({ agentType: "Explore", description: "Inactive child", parentToolCallId: createToolCallId("call_inactive_child"), prompt: "Search until the child stops reporting activity.", sessionId: createSessionId("parent_inactive_child"), trace: { traceId: createTraceId(), sessionId: createSessionId("parent_inactive_child"), turnId: createTurnId("turn_inactive_child"), }, turnId: createTurnId("turn_inactive_child"), workingDirectory: "/tmp", workspaceRoot: "/tmp", }) .then( () => ({ kind: "completed" as const }), (error: unknown) => ({ error, kind: "failed" as const }), ); const result = await Promise.race([ runPromise, new Promise<{ kind: "hung" }>((resolve) => setTimeout(() => resolve({ kind: "hung" }), 160)), ]); expect(result.kind).toBe("failed"); if (result.kind !== "failed") return; expect(isCoreError(result.error)).toBe(true); if (isCoreError(result.error)) { expect(result.error.type).toBe(CoreErrorType.ToolTimeout); } expect( emittedEvents.some( (event) => event.type === SessionEventType.SubagentStopped && (event.payload as any).status === "failed", ), ).toBe(true); }); it("fails a foreground run when the child stays inactive before session ready", async () => { const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-pre-ready-inactive-")); const emittedEvents: SessionEvent[] = []; const childStarted = createDeferred(); const port = createExploreSubagentPort({ createAgentId: () => "agent_pre_ready_inactive", inactivityTimeoutMs: 20, outputRootDir, async emitParentEvent(event) { emittedEvents.push(event); }, runExploreAgent: async () => { childStarted.resolve(); return new Promise(() => {}); }, }); const runPromise = port .run({ agentType: "Explore", description: "Pre-ready inactive child", parentToolCallId: createToolCallId("call_pre_ready_inactive_child"), prompt: "Never finish child setup.", sessionId: createSessionId("parent_pre_ready_inactive_child"), trace: { traceId: createTraceId(), sessionId: createSessionId("parent_pre_ready_inactive_child"), turnId: createTurnId("turn_pre_ready_inactive_child"), }, turnId: createTurnId("turn_pre_ready_inactive_child"), workingDirectory: "/tmp", workspaceRoot: "/tmp", }) .then( () => ({ kind: "completed" as const }), (error: unknown) => ({ error, kind: "failed" as const }), ); await childStarted.promise; const result = await Promise.race([ runPromise, new Promise<{ kind: "hung" }>((resolve) => setTimeout(() => resolve({ kind: "hung" }), 160)), ]); expect(result.kind).toBe("failed"); if (result.kind !== "failed") return; expect(isCoreError(result.error)).toBe(true); if (isCoreError(result.error)) { expect(result.error.type).toBe(CoreErrorType.ToolTimeout); } expect(emittedEvents).toEqual([]); await expect(port.getTask?.("agent_pre_ready_inactive")).resolves.toBeUndefined(); }); it("stops waiting when the parent aborts while the child runner is unresponsive", async () => { const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-abort-")); const abortController = new AbortController(); const emittedEvents: any[] = []; const port = createExploreSubagentPort({ createAgentId: () => "agent_unresponsive", outputRootDir, async emitParentEvent(event) { emittedEvents.push(event); }, runExploreAgent: async (request) => { await request.onSessionReady?.(); return new Promise(() => {}); }, }); const runPromise = port .run( { agentType: "Explore", description: "Unresponsive child", parentToolCallId: createToolCallId("call_unresponsive_child"), prompt: "Search until the model stops responding.", sessionId: createSessionId("parent_unresponsive_child"), trace: { traceId: createTraceId(), sessionId: createSessionId("parent_unresponsive_child"), turnId: createTurnId("turn_unresponsive_child"), }, turnId: createTurnId("turn_unresponsive_child"), workingDirectory: "/tmp", workspaceRoot: "/tmp", }, { signal: abortController.signal }, ) .then( () => ({ kind: "completed" as const }), (error: unknown) => ({ error, kind: "failed" as const }), ); await waitForCondition(() => emittedEvents.some((event) => event.type === SessionEventType.SubagentSpawned), ); abortController.abort(new Error("parent aborted unresponsive subagent")); const result = await Promise.race([ runPromise, new Promise<{ kind: "hung" }>((resolve) => setTimeout(() => resolve({ kind: "hung" }), 80)), ]); expect(result.kind).toBe("failed"); if (result.kind !== "failed") return; expect(isCoreError(result.error)).toBe(true); if (isCoreError(result.error)) { expect(result.error.type).toBe(CoreErrorType.ToolCancelled); } expect( emittedEvents.some( (event) => event.type === SessionEventType.SubagentStopped && (event.payload as any).status === "failed", ), ).toBe(true); }); it("stops waiting when the parent aborts before the child session is ready", async () => { const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-pre-ready-abort-")); const abortController = new AbortController(); const emittedEvents: SessionEvent[] = []; const childStarted = createDeferred(); const port = createExploreSubagentPort({ createAgentId: () => "agent_pre_ready_abort", outputRootDir, async emitParentEvent(event) { emittedEvents.push(event); }, runExploreAgent: async () => { childStarted.resolve(); return new Promise(() => {}); }, }); const runPromise = port .run( { agentType: "Explore", description: "Pre-ready aborted child", parentToolCallId: createToolCallId("call_pre_ready_aborted_child"), prompt: "Never finish child setup.", sessionId: createSessionId("parent_pre_ready_aborted_child"), trace: { traceId: createTraceId(), sessionId: createSessionId("parent_pre_ready_aborted_child"), turnId: createTurnId("turn_pre_ready_aborted_child"), }, turnId: createTurnId("turn_pre_ready_aborted_child"), workingDirectory: "/tmp", workspaceRoot: "/tmp", }, { signal: abortController.signal }, ) .then( () => ({ kind: "completed" as const }), (error: unknown) => ({ error, kind: "failed" as const }), ); await childStarted.promise; abortController.abort(new Error("parent aborted before child session ready")); const result = await Promise.race([ runPromise, new Promise<{ kind: "hung" }>((resolve) => setTimeout(() => resolve({ kind: "hung" }), 80)), ]); expect(result.kind).toBe("failed"); if (result.kind !== "failed") return; expect(isCoreError(result.error)).toBe(true); if (isCoreError(result.error)) { expect(result.error.type).toBe(CoreErrorType.ToolCancelled); } expect(emittedEvents).toEqual([]); await expect(port.getTask?.("agent_pre_ready_abort")).resolves.toBeUndefined(); }); it("runs through Agent with a read-only Explore tool pool and preserves trace context", async () => { const sessionId = createSessionId("runtime-explore-subagent"); const eventStore = createTestSessionEventStore(); const sessionStore = createRecordingSubagentSessionStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const childObservations: TestModelExecutionObservation[] = []; const parentSecondRequests: any[] = []; const liveEvents: SessionEvent[] = []; const childPersistedAtSpawn: boolean[] = []; let parentCallCount = 0; const modelSelection = { providerId: createModelProviderId("anthropic"), modelId: createModelId("claude-haiku-4-5-20251001-cc"), }; const runtime = createTestAgentRuntime( sessionId, { mode: "build", modelSelection: createTestModelSelection(modelSelection), bashShellSelection: { dialect: "git-bash", display: { name: "Git Bash" }, path: "C:\\Program Files\\Git\\bin\\bash.exe", source: "user-config", }, workingDirectory: "/Users/dev/Desktop/Z/z-code/apps/zcode-cli", currentDate: "2026-06-04", projectContext: { type: "node", packageManager: "pnpm", scripts: { test: "vitest", }, }, envInfo: { cwd: "/Users/dev/Desktop/Z/z-code/apps/zcode-cli", platform: "darwin", shell: "zsh", osVersion: "Darwin 24.3.0", nodeVersion: "24.14.0", isGitRepository: true, }, }, { eventStore, eventSink: { async onSessionEvent(event) { liveEvents.push(event); if (event.type === SessionEventType.SubagentSpawned) { const childSessionId = (event.payload as { childSessionId: string }).childSessionId; const child = await sessionStore.getSession(childSessionId as SessionId); childPersistedAtSpawn.push(child?.taskType === "subagent_child"); } }, }, sessionStore, modelFactory: createTestModelFactory({ properties: { supportsNativeWebSearch: true }, async generateText(request: any, observation) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); parentCallCount++; if (parentCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_explore", name: "Agent", input: { description: "Find runtime loop", prompt: "Find where the runtime injects tool results.", subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } parentSecondRequests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "parent used explore result", usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5, }, }; } childRequests.push(request); childObservations.push(observation); return { finishReason: "stop", providerMetadata: undefined, text: "Explore found runtime tool result injection in packages/core/src/runtime.ts.", usage: { inputTokens: 3, outputTokens: 4, totalTokens: 7, }, }; }, } as never), }, ); const result = await runtime.executeTurn("Use an explore agent for this question"); const parentEvents = await eventStore.getEvents(sessionId); const spawnEvent = parentEvents.find( (event) => event.type === SessionEventType.SubagentSpawned, ); const stoppedEvent = parentEvents.find( (event) => event.type === SessionEventType.SubagentStopped, ); const toolResultEvent = parentEvents.find( (event) => event.type === SessionEventType.ToolCallResult, ); const childSessionId = (spawnEvent?.payload as any)?.childSessionId; const childEvents = await eventStore.getEvents(childSessionId); const childPersistedMessages = await sessionStore.messages({ sessionID: childSessionId }); const childPersistedUserMessage = childPersistedMessages.find( (message) => message.info.role === "user", ); const initialModelTimeline = childPersistedMessages .flatMap((message) => message.parts) .find((part) => part.type === "timeline" && part.timelineType === "model_change"); const parentFirstRequestText = parentRequests[0].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); const parentFirstToolNames = parentRequests[0].tools.map((tool: any) => tool.name); const childRequestText = childRequests[0].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); const parentToolMessage = parentSecondRequests[0].messages.find( (message: any) => message.role === "tool", ); expect(result.response).toBe("parent used explore result"); expect(parentFirstRequestText).not.toContain("# agentsMd"); expect(parentFirstRequestText).not.toContain("# claudeMd"); expect(parentFirstRequestText).not.toContain("Project context:"); expect(parentFirstRequestText).not.toContain("- Package manager: pnpm"); expect(parentFirstRequestText).not.toContain("- `test`: vitest"); expect(parentFirstRequestText).toContain("# currentDate"); expect(parentFirstToolNames).toContain("Agent"); expect(parentFirstToolNames).not.toContain("Task"); expect(parentFirstToolNames).not.toContain("Glob"); expect(parentFirstToolNames).not.toContain("Grep"); expect(parentFirstToolNames).not.toContain("GoalRead"); expect(childRequests).toHaveLength(1); expect(childRequestText).not.toContain("# agentsMd"); expect(childRequestText).not.toContain("# claudeMd"); expect(childRequestText).not.toContain("# user_instructions"); expect(childRequestText).not.toContain("Project context:"); expect(childRequestText).not.toContain("- Package manager: pnpm"); expect(childRequestText).toContain("# currentDate"); expect(childRequestText).toContain("Today's date is 2026-06-04."); expect(childObservations[0].model).toMatchObject({ providerId: modelSelection.providerId, modelId: modelSelection.modelId, }); expect(childObservations[0].factoryInput.providerOptions).toBeUndefined(); expect(childRequests[0].options?.maxOutputTokens).toBe(32_000); // Explore 工具面:本地搜索/读取工具 + WebSearch client-side wrapper。 // provider-visible 顺序由最终输出边界统一排序,本地工具追加在后。 expect(childRequests[0].tools.map((tool: any) => tool.name)).toEqual([ "Bash", "Read", "TodoWrite", "WebFetch", "WebSearch", RESPOND_TO_COORDINATOR_TOOL_NAME, ]); const childToolsByName = new Map(childRequests[0].tools.map((tool: any) => [tool.name, tool])); expect(childToolsByName.has("Glob")).toBe(false); expect(childToolsByName.has("Grep")).toBe(false); expect(childRequests[0].tools.map((tool: any) => tool.name)).not.toContain("Agent"); expect(childRequests[0].tools.map((tool: any) => tool.name)).not.toContain("Task"); expect( childRequests[0].messages .filter((message: any) => message.role === "system") .map((message: any) => message.content), ).toEqual(exploreSystemMessages); expect(childRequests[0].messages[1].content).not.toContain( "You help the user with software engineering work in the current workspace.", ); expect(childPersistedUserMessage?.info.contextSnapshot?.envInfo?.shell).toBe("Git Bash"); expect(childObservations[0].invocationContext?.traceContext?.traceId).toBe(result.traceId); if (!spawnEvent || !stoppedEvent || !toolResultEvent) { throw new Error("Expected subagent lifecycle events"); } const spawnPayload = spawnEvent.payload as any; const stoppedPayload = stoppedEvent.payload as any; const toolResultPayload = toolResultEvent.payload as any; expect(spawnPayload.allowedTools).toEqual([ "Bash", "Read", "WebFetch", "WebSearch", "TodoWrite", ]); expect(stoppedPayload.status).toBe("completed"); expect(toolResultPayload.toolCallId).toBe("call_explore"); expect(parentToolMessage.toolCallId).toBe("call_explore"); expect(parentToolMessage.content).toContain( "Explore found runtime tool result injection in packages/core/src/runtime.ts.", ); expect(parentToolMessage.content).toContain("agentId: agent_"); expect(parentToolMessage.content).toContain("use SendMessage with to:"); expect(parentToolMessage.content).toContain("subagent_tokens:"); expect(parentToolMessage.content).toContain("tool_uses:"); expect(parentToolMessage.content).toContain("duration_ms:"); expect(parentToolMessage.content).not.toContain("totalToolUseCount"); expect(childEvents.length).toBeGreaterThan(0); const initialModelSelected = childEvents.find( (event) => event.type === SessionEventType.ModelSelected, )?.payload; expect(initialModelSelected).toMatchObject({ previousModelSelection: null, modelSelection: { providerId: modelSelection.providerId, modelId: modelSelection.modelId, }, }); expect( initialModelSelected && "modelSelection" in initialModelSelected ? initialModelSelected.modelSelection : undefined, ).not.toHaveProperty("role"); expect(initialModelSelected).not.toHaveProperty("origin"); expect(initialModelTimeline).toMatchObject({ timelineType: "model_change", toModel: { providerId: modelSelection.providerId, modelId: modelSelection.modelId, }, }); expect( initialModelTimeline?.type === "timeline" ? initialModelTimeline.fromModel : "unexpected", ).toBeUndefined(); expect(childPersistedAtSpawn).toEqual([true]); expect( liveEvents.filter((event) => event.sessionId === childSessionId).map((event) => event.id), ).toEqual(childEvents.map((event) => event.id)); expect(new Set([...parentEvents, ...childEvents].map((event) => event.traceId))).toEqual( new Set([result.traceId]), ); }); it("reports child turn tool call count in the Agent result usage trailer", async () => { const sessionId = createSessionId("runtime-explore-subagent-tool-count"); const eventStore = createTestSessionEventStore(); const sessionStore = createRecordingSubagentSessionStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const childObservations: TestModelExecutionObservation[] = []; let parentCallCount = 0; let childCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", }, { eventStore, sessionStore, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); parentCallCount++; if (parentCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_counted_explore", name: "Agent", input: { description: "Count child tools", prompt: "Use one todo update before returning.", subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "parent saw counted child tool use", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); childObservations.push(observation); childCallCount++; if (childCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_child_todo", name: "TodoWrite", input: { todos: [ { content: "Count child tool calls", priority: "high", status: "in_progress", }, ], }, }, ], usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "Child completed after one TodoWrite.", usage: { inputTokens: 3, outputTokens: 4, totalTokens: 7 }, }; }, } as never), }, ); const result = await runtime.executeTurn("Use an explore agent and report its usage"); const parentEvents = await eventStore.getEvents(sessionId); const stoppedEvent = parentEvents.find( (event) => event.type === SessionEventType.SubagentStopped, ); const parentToolMessage = parentRequests[1].messages.find( (message: any) => message.role === "tool", ); expect(result.response).toBe("parent saw counted child tool use"); expect(childRequests).toHaveLength(2); expect((stoppedEvent?.payload as any)?.totalToolUseCount).toBe(1); expect(parentToolMessage.content).toContain("Child completed after one TodoWrite."); expect(parentToolMessage.content).toContain("tool_uses: 1"); expect(parentToolMessage.content).not.toContain("tool_uses: 0"); }); it("normalizes lowercase built-in Explore subagent_type before launching", async () => { const sessionId = createSessionId("runtime-lowercase-explore-subagent"); const eventStore = createTestSessionEventStore(); const childRequests: any[] = []; let parentCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentCallCount++; if (parentCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_lowercase_explore", name: "Agent", input: { description: "Find runtime loop", prompt: "Find where runtime loop is implemented.", subagent_type: "explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } const toolResultText = request.messages .filter((message: any) => message.role === "tool") .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); expect(toolResultText).not.toContain("Unknown subagent type"); return { finishReason: "stop", providerMetadata: undefined, text: "parent used normalized explore result", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "lowercase explore resolved to the built-in Explore agent.", usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 }, }; }, } as never), }, ); const result = await runtime.executeTurn("Use explore to inspect the runtime"); expect(result.response).toBe("parent used normalized explore result"); expect(childRequests).toHaveLength(1); const childToolNames = childRequests[0].tools.map((tool: any) => tool.name); expect(childToolNames).toContain("Read"); expect(childToolNames).toContain("Bash"); expect(childToolNames).toContain("TodoWrite"); expect(childToolNames).not.toContain("Agent"); expect(childToolNames).not.toContain("Write"); }); it("reports ambiguous normalized subagent_type from the launch path", async () => { const sessionId = createSessionId("runtime-ambiguous-normalized-subagent"); const eventStore = createTestSessionEventStore(); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "foo-bar", description: "First normalized collision.", source: "project", systemPrompt: "first", tools: ["Read"], }, { name: "foo_bar", description: "Second normalized collision.", source: "project", systemPrompt: "second", tools: ["Read"], background: true, }, ], }, }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (!toolNames.includes("Agent")) { throw new Error("Ambiguous subagent_type must not launch a child agent"); } const toolResultText = request.messages .filter((message: any) => message.role === "tool") .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); if (toolResultText) { expect(toolResultText).toContain("Agent type 'foo bar' is ambiguous"); expect(toolResultText).toContain("foo-bar"); expect(toolResultText).toContain("foo_bar"); return { finishReason: "stop", providerMetadata: undefined, text: "parent saw ambiguous agent type", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_ambiguous_agent", name: "Agent", input: { description: "Resolve ambiguous agent", prompt: "Try the ambiguous agent.", subagent_type: "foo bar", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("Use the ambiguous custom agent"); expect(result.response).toBe("parent saw ambiguous agent type"); }); it("normalizes subagent_type without stripping arbitrary punctuation", async () => { const sessionId = createSessionId("runtime-punctuation-subagent"); const eventStore = createTestSessionEventStore(); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "report-v2", description: "Report agent.", source: "project", systemPrompt: "report", tools: ["Read"], }, ], }, }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (!toolNames.includes("Agent")) { throw new Error("Punctuation mismatch must not launch a child agent"); } const toolResultText = request.messages .filter((message: any) => message.role === "tool") .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); if (toolResultText) { expect(toolResultText).toContain("Agent type 'report.v2' not found"); expect(toolResultText).toContain("report-v2"); return { finishReason: "stop", providerMetadata: undefined, text: "parent saw unknown agent type", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_punctuation_agent", name: "Agent", input: { description: "Resolve punctuation agent", prompt: "Try the punctuation agent.", subagent_type: "report.v2", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("Use the report.v2 custom agent"); expect(result.response).toBe("parent saw unknown agent type"); }); // docs/dynamic-workflow/launch.md「Gray release」:父会话关着灰度时,Agent 工具不能成为 // 绕过它的后门;父会话开着(或根本不参与灰度)时子代理照旧拿得到。 // // 这里刻意用**显式声明 tools 的自定义 agent profile**:默认 agent 的子工具面是「父 // registry 里现存工具名」的交集,父会话已经被剃过,子代理自然也没有——那条路径挡得住, // 但挡住它的不是灰度门。profile 显式写 `tools: [...]` 时 resolveSubagentToolAllowlist // 走的是另一条分支(request.allowedTools 原样保留,只过 disallow 规则),父 registry // 不再参与。唯一能挡住它的就是子 runtime 自己继承到的 dynamicWorkflowEnabled。 describe("dynamic-workflow gray gate inheritance into subagent children", () => { async function childToolNamesForParent( parentConfig: Record, options: { activateParentFirst?: boolean } = {}, ): Promise<{ child: string[]; parent: string[] }> { const sessionId = createSessionId( [ "runtime-gray-gate-subagent", String(parentConfig.dynamicWorkflowEnabled), String(parentConfig.dynamicWorkflowToolsOnDemand), String(options.activateParentFirst), ].join("-"), ); const childRequests: any[] = []; let parentCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", ...parentConfig, modelSelection: createTestModelSelection({ providerId: createModelProviderId("provider-main"), modelId: createModelId("main-model"), }), workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "workflow-author", description: "显式点名工作流工具的自定义 agent。", source: "project", systemPrompt: "You author workflows.", tools: ["Read", "CreateWorkflow", "ResumeWorkflowRun"], model: "main", maxTurns: 2, }, ], }, }, { eventStore: createTestSessionEventStore(), modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentCallCount++; if (parentCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_gray_gate_agent", name: "Agent", input: { description: "Author a workflow", prompt: "Try to author a workflow.", subagent_type: "workflow-author", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "parent done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "child done.", usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 }, }; }, } as never), }, ); if (options.activateParentFirst) { await runtime.activateDynamicWorkflowTools({ source: "command" }); } await runtime.executeTurn("Use the workflow-author agent"); return { child: childRequests[0].tools.map((tool: any) => tool.name), parent: runtime.getTools().map((tool) => tool.name), }; } it.each([ { parentConfig: { dynamicWorkflowEnabled: false }, available: false }, { parentConfig: { dynamicWorkflowEnabled: true }, available: true }, // 不参与灰度的父会话(TUI / headless `-p`,DWG-04):子代理必须保留全部工具。 { parentConfig: {}, available: true }, // DWG-12:onDemand 父会话未激活时子代理没有工作流工具;父已激活则子出生即有 // (launch.md「On demand: activation」Children 段)。 { parentConfig: { dynamicWorkflowEnabled: true, dynamicWorkflowToolsOnDemand: true }, available: false, }, { parentConfig: { dynamicWorkflowEnabled: true, dynamicWorkflowToolsOnDemand: true }, activateParentFirst: true, available: true, }, ])("parent %j", async ({ parentConfig, available, activateParentFirst }) => { const { child, parent } = await childToolNamesForParent(parentConfig, { activateParentFirst, }); for (const name of ["CreateWorkflow", "ResumeWorkflowRun"]) { expect({ name, inParent: parent.includes(name) }).toEqual({ name, inParent: available }); expect({ name, inChild: child.includes(name) }).toEqual({ name, inChild: available }); } // 反向断言:profile 里其余工具在三种取值下都照常给到,门只碰工作流那几件。 expect(child).toContain("Read"); }); }); it("routes Explore provider runtime headers refreshes through the parent session", async () => { const sessionId = createSessionId("runtime-explore-provider-headers-parent-route"); const eventStore = createTestSessionEventStore(); // 主 runtime 每次调用报自己的 sessionId;子 runtime 拿到的是父 runtime 派生的实例 // (helpers/child-client-ports.ts),派生层把 sessionId 改写成父会话。 const refreshes: Array<{ providerId: string; boundSessionId: string; routedSessionId?: string }> = []; const childRequests: any[] = []; let parentCallCount = 0; const modelSelection = { providerId: createModelProviderId("provider-start-plan"), modelId: createModelId("start-plan-model"), }; const runtime = createTestAgentRuntime( sessionId, { mode: "build", modelSelection: createTestModelSelection(modelSelection), workingDirectory: "/workspace/project", }, { eventStore, providerRuntimeHeadersPort: { shouldRefreshBeforeModelRequest: () => true, async refreshBeforeModelRequest(input) { refreshes.push({ providerId: input.providerId, boundSessionId: sessionId, // 回归守卫:子 runtime 的刷新到这里必须已经是父会话的 sessionId。若派生层漏掉 // 改写,子 runtime 会把子会话发给客户端(2026-09-09 挂死事故)。 routedSessionId: input.sessionId, }); return { headersApplied: true, }; }, }, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { await observation.invocationContext?.refreshRuntimeHeadersBeforeAttempt?.({ attempt: 1, abortSignal: request.abortSignal, }); const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentCallCount++; if (parentCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_explore_headers", name: "Agent", input: { description: "Check provider headers", prompt: "Check the provider headers route.", subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "parent completed after explore", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "Explore checked the provider headers route.", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("Use an explore agent to check headers"); const parentEvents = await eventStore.getEvents(sessionId); const spawnEvent = parentEvents.find( (event) => event.type === SessionEventType.SubagentSpawned, ); if (!spawnEvent) { throw new Error("Expected subagent spawn event"); } const childSessionId = (spawnEvent.payload as any).childSessionId; const childEvents = await eventStore.getEvents(childSessionId); expect(childRequests).toHaveLength(1); expect(childSessionId).not.toBe(sessionId); expect(childEvents.length).toBeGreaterThan(0); // 三次刷新(父两次 + 子一次)全部以父会话为路由身份到达端口。 expect(refreshes).toEqual([ { providerId: "provider-start-plan", boundSessionId: sessionId, routedSessionId: sessionId }, { providerId: "provider-start-plan", boundSessionId: sessionId, routedSessionId: sessionId }, { providerId: "provider-start-plan", boundSessionId: sessionId, routedSessionId: sessionId }, ]); }); it("passes the parent HTTP client port into Explore WebFetch calls", async () => { const sessionId = createSessionId("runtime-explore-webfetch-http-client"); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const childObservations: TestModelExecutionObservation[] = []; const webFetchProcessingRequests: any[] = []; let parentCallCount = 0; let childCallCount = 0; const body = new TextEncoder().encode( "

Explore WebFetch

http client port is wired.

", ); const httpClientPort: HttpClientPort = { request: vi.fn(async (request) => { expect(request.url).toBe("https://example.com/explore-webfetch-port"); return { body, bytes: body.byteLength, durationMs: 7, headers: { "content-type": "text/html; charset=utf-8" }, status: 200, statusText: "OK", url: "https://example.com/explore-webfetch-port", }; }), }; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", }, { eventStore, httpClientPort, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); parentCallCount++; if (parentCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_explore_webfetch", name: "Agent", input: { description: "Fetch a public page", prompt: "Use WebFetch to inspect the page.", subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "parent saw Explore WebFetch result", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (toolNames.includes("WebFetch")) { childRequests.push(request); childCallCount++; if (childCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_child_webfetch", name: "WebFetch", input: { prompt: "What confirms the HTTP client port wiring?", url: "https://example.com/explore-webfetch-port", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "Explore fetched the page through the injected HTTP client.", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } webFetchProcessingRequests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "The page confirms the HTTP client port is wired.", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("Use an explore agent to fetch a URL"); expect(result.response).toBe("parent saw Explore WebFetch result"); expect(parentRequests).toHaveLength(2); expect(childRequests).toHaveLength(2); expect(httpClientPort.request).toHaveBeenCalledTimes(1); expect(webFetchProcessingRequests).toHaveLength(1); expect( webFetchProcessingRequests[0].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"), ).toContain("http client port is wired"); }); it("runs a custom profile-backed subagent with its prompt and tool allowlist", async () => { const sessionId = createSessionId("runtime-custom-subagent-profile"); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "zcode-reviewer", description: "用中文检查实现边界和风险。", source: "project", systemPrompt: "你是 zcode-reviewer,只输出中文结论。", tools: ["Read"], disallowedTools: ["Bash"], model: "main", maxTurns: 2, }, ], }, }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_custom_agent", name: "Agent", input: { description: "检查实现边界", prompt: "检查 subagent 与 main agent 的边界。", subagent_type: "ZCode Reviewer", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "父 agent 采纳了自定义 agent 的结论。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); childObservations.push(observation); return { finishReason: "stop", providerMetadata: undefined, text: "自定义 agent 已按中文 system prompt 完成边界检查。", usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 }, }; }, } as never), }, ); const result = await runtime.executeTurn("让自定义 agent 检查边界"); const parentFirstRequestText = parentRequests[0].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); const childRequestText = childRequests[0].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); const childToolNames = childRequests[0].tools.map((tool: any) => tool.name); const parentAgentToolDescription = parentRequests[0].tools.find((tool: any) => tool.name === "Agent")?.description ?? ""; expect(result.response).toBe("父 agent 采纳了自定义 agent 的结论。"); expect(parentFirstRequestText).toContain("Available agent types for the Agent tool:"); expect(parentFirstRequestText).toContain("zcode-reviewer"); expect(parentAgentToolDescription).toContain( "Available agent types are listed in messages in the conversation.", ); expect(parentFirstRequestText).toContain( "- zcode-reviewer: 用中文检查实现边界和风险。 (Tools: Read)", ); expect(childRequestText).toContain("你是 zcode-reviewer,只输出中文结论。"); expect(childRequestText).not.toContain("READ-ONLY MODE"); expect(childToolNames).toContain("Read"); expect(childToolNames).not.toContain("Bash"); expect(childToolNames).not.toContain("Edit"); expect(childToolNames).not.toContain("Agent"); expect(childToolNames).not.toContain("Task"); }); it("runs a custom Explore with its own prompt, main tools, MCP descriptor, and parent permissions", async () => { const sessionId = createSessionId("runtime-custom-explore-profile"); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const descriptor = { serverName: "search__server", toolName: "lookup__item", description: "Look up an indexed item.", inputSchema: { type: "object", properties: {} }, }; const mcpToolName = toMcpToolName(descriptor); const connectedStatus = { status: "connected" as const, toolCount: 1, transport: "stdio" as const, updatedAt: new Date(0).toISOString(), }; const callTool = vi.fn(async () => ({ content: [{ text: "found", type: "text" as const }] })); const mcpPort = createMockMcpPort({ callTool, async connectConfiguredServers() { return { statuses: { search__server: connectedStatus }, tools: [descriptor], }; }, }); const parentPermissionService = new PermissionService({ allowedTools: new Set([mcpToolName]), disallowedTools: new Set(), autoApproveHighRisk: false, allowMediumRiskInAutoMode: false, }); const checkPermission = vi.spyOn(parentPermissionService, "checkPermission"); const runtime = createTestAgentRuntime( sessionId, { mcp: { enabled: true, servers: { search__server: { command: "search-mcp", type: "stdio" }, }, }, mode: "build", workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "Explore", description: "自定义可写 Explore。", source: "project", systemPrompt: "你是项目自定义 Explore,可以执行写入和 MCP 查询。", tools: ["Write", mcpToolName], disallowedTools: ["Bash"], mcpServers: ["search__server"], model: "main", maxTurns: 2, }, ], }, }, { eventStore, mcpPort, permissionService: parentPermissionService, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_custom_agent", name: "Agent", input: { description: "检查实现边界", prompt: "检查 subagent 与 main agent 的边界。", subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "父 agent 采纳了自定义 agent 的结论。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); if (childRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_custom_explore_mcp", name: mcpToolName, input: {}, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "自定义 Explore 已完成边界检查。", usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 }, }; }, } as never), }, ); const result = await runtime.executeTurn("让自定义 agent 检查边界"); const parentFirstRequestText = parentRequests[0].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); const childRequestText = childRequests[0].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); const childToolNames = childRequests[0].tools.map((tool: any) => tool.name); const parentAgentToolDescription = parentRequests[0].tools.find((tool: any) => tool.name === "Agent")?.description ?? ""; expect(result.response).toBe("父 agent 采纳了自定义 agent 的结论。"); expect(parentFirstRequestText).toContain("Available agent types for the Agent tool:"); expect(parentFirstRequestText).toContain("自定义可写 Explore"); expect(parentAgentToolDescription).toContain( "Available agent types are listed in messages in the conversation.", ); expect(parentFirstRequestText).toContain( `- Explore: 自定义可写 Explore。 (Tools: Write, ${mcpToolName})`, ); expect(childRequestText).toContain("你是项目自定义 Explore,可以执行写入和 MCP 查询。"); expect(childRequestText).not.toContain("READ-ONLY MODE"); expect(childToolNames).toContain("Write"); expect(childToolNames).toContain(mcpToolName); expect(childToolNames).not.toContain("Bash"); expect(childToolNames).not.toContain("Agent"); expect(childToolNames).not.toContain("Task"); expect(callTool).toHaveBeenCalledTimes(1); expect( checkPermission.mock.calls.some( ([context]) => context.toolName === mcpToolName && context.mode === "build", ), ).toBe(true); }); it.each([ { label: "omitted", mcpServers: undefined }, { label: "empty", mcpServers: [] as readonly string[] }, ])( "keeps an empty-prompt custom Explore unrestricted when mcpServers is $label", async ({ label, mcpServers }) => { const sessionId = createSessionId(`runtime-custom-explore-unrestricted-${label}`); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const descriptor = { serverName: "plugin:android-emulator:android-emulator", toolName: "android_preflight", description: "Check Android emulator prerequisites.", inputSchema: { type: "object", properties: {} }, }; const mcpToolName = toMcpToolName(descriptor); const connectedStatus = { status: "connected" as const, toolCount: 1, transport: "stdio" as const, updatedAt: new Date(0).toISOString(), }; const connectConfiguredServers = vi.fn(async () => ({ statuses: { "plugin:android-emulator:android-emulator": connectedStatus, }, tools: [descriptor], })); const listTools = vi.fn(async () => []); const status = vi.fn(async () => ({})); const mcpPort = createMockMcpPort({ connectConfiguredServers, listTools, status }); const runtime = createTestAgentRuntime( sessionId, { mcp: { enabled: true, servers: { "plugin:android-emulator:android-emulator": { command: "android-emulator-mcp", type: "stdio", }, }, }, mode: "build", workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "Explore", description: "Custom Explore using the unrestricted parent MCP snapshot.", source: "user", systemPrompt: "", tools: ["*"], ...(mcpServers === undefined ? {} : { mcpServers }), }, ], }, }, { eventStore, mcpPort, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: `call_custom_explore_unrestricted_${label}`, name: "Agent", input: { description: "Test unrestricted MCP snapshot", prompt: "Confirm the custom Explore can use the parent MCP snapshot.", subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "parent received the custom Explore result", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "custom Explore received the unrestricted MCP descriptor", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("Use the custom Explore agent"); const childRequestText = childRequests[0].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); const childToolNames = childRequests[0].tools.map((tool: any) => tool.name); expect(result.response).toBe("parent received the custom Explore result"); expect(childRequests).toHaveLength(1); expect(childRequestText).not.toContain("READ-ONLY MODE"); expect(childToolNames).toContain(mcpToolName); expect(connectConfiguredServers).toHaveBeenCalledTimes(1); expect(status).not.toHaveBeenCalled(); expect(listTools).not.toHaveBeenCalled(); }, ); it("removes plan-mode tools from custom wildcard subagents", async () => { const sessionId = createSessionId("runtime-custom-subagent-no-plan-mode-tools"); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "wildcard-reviewer", description: "Use every safe inherited tool.", source: "project", systemPrompt: "Review with inherited tools, but do not enter planning mode.", tools: ["*"], }, ], }, }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_wildcard_agent", name: "Agent", input: { description: "检查 wildcard custom agent", prompt: "检查 custom wildcard 子 agent 工具面。", subagent_type: "wildcard-reviewer", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "父 agent 收到 wildcard 结果。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "wildcard child done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("让 wildcard-reviewer 跑一下"); const childToolNames = childRequests[0].tools.map((tool: any) => tool.name); expect(result.response).toBe("父 agent 收到 wildcard 结果。"); expect(childToolNames).toContain("Read"); expect(childToolNames).toContain("Bash"); expect(childToolNames).not.toContain("EnterPlanMode"); expect(childToolNames).not.toContain("ExitPlanMode"); expect(childToolNames).not.toContain("Agent"); expect(childToolNames).not.toContain("Task"); }); it("removes plan-mode tools from explicit custom subagent tool lists", async () => { const sessionId = createSessionId("runtime-explicit-custom-subagent-no-plan-tools"); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "explicit-reviewer", description: "Asks for an explicit tool subset.", source: "project", systemPrompt: "Review with explicitly declared tools.", tools: ["Read", "EnterPlanMode", "ExitPlanMode"], }, ], }, }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_explicit_agent", name: "Agent", input: { description: "检查 explicit custom agent", prompt: "检查 custom explicit 子 agent 工具面。", subagent_type: "explicit-reviewer", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "父 agent 收到 explicit 结果。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "explicit child done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("让 explicit-reviewer 跑一下"); const childToolNames = childRequests[0].tools.map((tool: any) => tool.name); const parentAgentToolDescription = parentRequests[0].tools.find((tool: any) => tool.name === "Agent")?.description ?? ""; expect(result.response).toBe("父 agent 收到 explicit 结果。"); expect( modelMessageContentToText( parentRequests[0].messages.find((message: any) => modelMessageContentToText(message.content).includes( "Available agent types for the Agent tool:", ), ).content, ), ).toContain("- explicit-reviewer: Asks for an explicit tool subset. (Tools: Read)"); expect(parentAgentToolDescription).not.toContain("EnterPlanMode"); expect(parentAgentToolDescription).not.toContain("ExitPlanMode"); expect(childToolNames).toContain("Read"); expect(childToolNames).not.toContain("EnterPlanMode"); expect(childToolNames).not.toContain("ExitPlanMode"); }); it("inherits disabled native find and grep enhancements in child Bash", async () => { const sessionId = createSessionId("runtime-subagent-native-search-disabled"); const eventStore = createTestSessionEventStore(); const backend = { kind: "native-binaries" as const, findCommand: "/tools/bfs", grepCommand: "/tools/ugrep", rgCommand: "/tools/rg", }; let childCallCount = 0; let parentCallCount = 0; let childBashPrelude: unknown; const runtime = createTestAgentRuntime( sessionId, { bashShellSelection: { dialect: "posix", display: { name: "bash" }, id: "posix:/bin/bash", label: "bash", path: "/bin/bash", source: "auto-detected", }, embeddedSearchBackend: backend, mode: "yolo", nativeSearchEnhancementsEnabled: false, workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "search-child", description: "Runs one search command.", source: "project", systemPrompt: "Run the requested search.", tools: ["Bash"], }, ], }, }, { eventStore, executionPort: { async run(request) { childBashPrelude = request.bashPrelude; const now = new Date(); return { status: "completed", exitCode: 0, stdout: { text: "needle", bytes: 6, truncated: false }, stderr: { text: "", bytes: 0, truncated: false }, durationMs: 1, timedOut: false, cancelled: false, startedAt: now, completedAt: now, }; }, }, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentCallCount++; if (parentCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_native_search_child", name: "Agent", input: { description: "检查搜索继承", prompt: "搜索 needle。", subagent_type: "search-child", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "parent done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childCallCount++; if (childCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_child_grep", name: "Bash", input: { command: "grep needle file.txt" }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "child done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("让 search-child 搜索"); expect(childBashPrelude).toEqual({ kind: "embedded-search", backend, findAndGrepEnabled: false, }); }); it("does not let a subagent profile re-add tools removed by the parent runtime", async () => { const sessionId = createSessionId("runtime-subagent-parent-disallowed-tools"); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", toolDisallowlist: ["Bash(git *)"], workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "bashy-reviewer", description: "Requests Bash and Read.", source: "project", systemPrompt: "Review without shell access.", tools: ["Bash", "Read"], model: "main", maxTurns: 2, }, ], }, }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_bashy_agent", name: "Agent", input: { description: "检查 Bash 是否被移除", prompt: "检查子 agent 工具面。", subagent_type: "bashy-reviewer", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "父 agent 收到结果。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "child done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("让 bashy-reviewer 跑一下"); const childToolNames = childRequests[0].tools.map((tool: any) => tool.name); expect(result.response).toBe("父 agent 收到结果。"); expect(childToolNames).toContain("Read"); expect(childToolNames).not.toContain("Bash"); }); it("auto-enables Skill and allows bare aliases for declared plugin skills", async () => { const sessionId = createSessionId("runtime-subagent-filtered-skills"); const eventStore = createTestSessionEventStore(); const loadedSkills: string[] = []; const allowedPluginSkill = { ...skillMetadata("allowed-skill", "/plugins/superpowers/skills/allowed/SKILL.md"), pluginName: "superpowers", qualifiedName: "superpowers:allowed-skill", rootPath: "/plugins/superpowers/skills", scope: "system" as const, source: "plugin" as const, }; const conflictingPluginSkill = { ...skillMetadata("allowed-skill", "/plugins/other/skills/allowed/SKILL.md"), pluginName: "other-plugin", qualifiedName: "other-plugin:allowed-skill", rootPath: "/plugins/other/skills", scope: "system" as const, source: "plugin" as const, }; const skillOutcome: SkillLoadOutcome = { diagnostics: [], skills: [ conflictingPluginSkill, allowedPluginSkill, skillMetadata("blocked-skill", "/skills/blocked/SKILL.md"), ], totalDiscovered: 3, }; const skillPort: SkillPort = { async discoverSkills() { return skillOutcome; }, async loadSkill(request): Promise { const metadata = skillOutcome.skills.find( (skill) => skill.name === request.name || skill.qualifiedName === request.name, ); if (!metadata) throw new Error(`Skill not found: ${request.name}`); loadedSkills.push(metadata.qualifiedName ?? metadata.name); return { baseDirectory: metadata.directory, bytesRead: 12, content: `# ${metadata.qualifiedName ?? metadata.name}\n\n只使用允许的 skill。`, metadata, sizeBytes: 12, truncated: false, }; }, }; const parentRequests: any[] = []; const childRequests: any[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "skill-agent", description: "使用声明的 skill 完成任务。", source: "project", systemPrompt: "你必须先加载允许的 skill。", tools: ["Read"], skills: ["superpowers:allowed-skill"], }, ], }, }, { eventStore, skillPort, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_skill_agent", name: "Agent", input: { description: "测试 skill 过滤", prompt: "加载 allowed-skill 后返回结论。", subagent_type: "skill-agent", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "父 agent 收到了 skill-agent 的结论。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); if (childRequests.length === 1) { expect(toolNames).toContain("Skill"); expect(toolNames).toContain("Read"); expect(toolNames).not.toContain("Agent"); expect(toolNames).not.toContain("Task"); const requestText = request.messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); expect(requestText).toContain("superpowers:allowed-skill"); expect(requestText).not.toContain("other-plugin:allowed-skill"); expect(requestText).not.toContain("blocked-skill"); return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_allowed_skill", name: "Skill", input: { skill: "allowed-skill" }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "skill-agent 已加载允许的 skill。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("让 skill-agent 跑一下"); expect(result.response).toBe("父 agent 收到了 skill-agent 的结论。"); expect(loadedSkills).toEqual(["superpowers:allowed-skill"]); expect(childRequests).toHaveLength(2); }); it("fails before the child request when a scoped MCP server is unavailable in the parent startup snapshot", async () => { const sessionId = createSessionId("runtime-subagent-scoped-mcp-server-unavailable"); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const mcpPort = createMockMcpPort(); const runtime = createTestAgentRuntime( sessionId, { mcp: { enabled: true, servers: {} }, mode: "build", workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "mcp-agent", description: "需要 MCP server 的 agent。", source: "project", systemPrompt: "已配置的 MCP server 在 parent 启动快照中不可用时不应该启动。", mcpServers: ["required-server"], }, ], }, }, { eventStore, mcpPort, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_mcp_agent", name: "Agent", input: { description: "测试 scoped MCP server 不可用", prompt: "检查已配置的 MCP server 是否在 parent 启动快照中已连接。", subagent_type: "mcp-agent", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "父 agent 看到了 scoped MCP server 不可用错误。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "mcp-agent 正常启动。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("让 mcp-agent 跑一下"); const secondParentRequestText = parentRequests[1].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); expect(result.response).toBe("父 agent 看到了 scoped MCP server 不可用错误。"); expect(childRequests).toEqual([]); expect(secondParentRequestText).toContain("Required MCP server is not connected"); expect(secondParentRequestText).toContain("required-server"); }); it("fails fast when an explicitly allowed MCP descriptor is absent from the parent startup snapshot", async () => { const sessionId = createSessionId("runtime-subagent-missing-mcp-descriptor"); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const mcpPort = createMockMcpPort(); const runtime = createTestAgentRuntime( sessionId, { mcp: { enabled: true, servers: {} }, mode: "build", workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "mcp-agent", description: "需要 MCP tool 的 agent。", source: "project", systemPrompt: "如果显式 MCP tool 不在 parent 启动快照中就不应该启动。", tools: ["mcp__required-server__search"], }, ], }, }, { eventStore, mcpPort, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_mcp_agent", name: "Agent", input: { description: "测试 MCP descriptor 缺失", prompt: "检查显式 MCP tool 是否存在于 parent 启动快照。", subagent_type: "mcp-agent", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "父 agent 看到了 MCP tool 缺失错误。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "不应该真正启动子 agent。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("让 mcp-agent 跑一下"); const secondParentRequestText = parentRequests[1].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); expect(result.response).toBe("父 agent 看到了 MCP tool 缺失错误。"); expect(childRequests).toEqual([]); expect(secondParentRequestText).toContain( "Required MCP tool is not available in the parent startup snapshot", ); expect(secondParentRequestText).toContain("mcp__required-server__search"); }); it("starts a wildcard general-purpose subagent when plugin MCP status uses raw server names", async () => { const sessionId = createSessionId("runtime-subagent-wildcard-plugin-mcp"); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const connectedStatus = { status: "connected" as const, toolCount: 1, transport: "stdio" as const, updatedAt: new Date(0).toISOString(), }; const mcpPort = createMockMcpPort({ async connectConfiguredServers() { return { statuses: { "plugin:android-emulator:android-emulator": connectedStatus, }, tools: [ { serverName: "plugin:android-emulator:android-emulator", toolName: "android_preflight", description: "Check Android emulator prerequisites.", inputSchema: { type: "object", properties: {} }, }, ], }; }, async listTools() { return [ { serverName: "plugin:android-emulator:android-emulator", toolName: "android_preflight", description: "Check Android emulator prerequisites.", inputSchema: { type: "object", properties: {} }, }, ]; }, async status() { return { "plugin:android-emulator:android-emulator": connectedStatus, }; }, }); const runtime = createTestAgentRuntime( sessionId, { mcp: { enabled: true, servers: { "plugin:android-emulator:android-emulator": { command: "android-emulator-mcp", type: "stdio", }, }, }, mode: "build", workingDirectory: "/workspace/project", }, { eventStore, mcpPort, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { expect(toolNames).toContain( "mcp__plugin_android-emulator_android-emulator__android_preflight", ); return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_general_purpose", name: "Agent", input: { description: "测试 plugin MCP 名称", prompt: "确认 general-purpose 可以启动。", subagent_type: "general-purpose", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "父 agent 收到了 general-purpose 结果。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); const childToolNames = (request.tools ?? []).map((tool: any) => tool.name); expect(childToolNames).toContain( "mcp__plugin_android-emulator_android-emulator__android_preflight", ); return { finishReason: "stop", providerMetadata: undefined, text: "general-purpose 正常启动。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("让 general-purpose 跑一下"); const secondParentRequestText = parentRequests[1].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); expect(result.response).toBe("父 agent 收到了 general-purpose 结果。"); expect(childRequests).toHaveLength(1); expect(secondParentRequestText).not.toContain("Required MCP server is not connected"); }); it("scopes custom Explore wildcard MCP tools to the fixed parent startup snapshot", async () => { const sessionId = createSessionId("runtime-custom-explore-wildcard-mcp"); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const allowedDescriptor = { serverName: "alpha__server", toolName: "allowed__lookup", description: "Allowed alpha lookup.", inputSchema: { type: "object", properties: {} }, }; const blockedDescriptor = { serverName: "alpha__server", toolName: "blocked__lookup", description: "Blocked alpha lookup.", inputSchema: { type: "object", properties: {} }, }; const outOfScopeDescriptor = { serverName: "beta__server", toolName: "beta__lookup", description: "Out-of-scope beta lookup.", inputSchema: { type: "object", properties: {} }, }; const lateDescriptor = { serverName: "alpha__server", toolName: "late__lookup", description: "Only visible after startup.", inputSchema: { type: "object", properties: {} }, }; const allowedMcpToolName = toMcpToolName(allowedDescriptor); const blockedMcpToolName = toMcpToolName(blockedDescriptor); const outOfScopeMcpToolName = toMcpToolName(outOfScopeDescriptor); const lateMcpToolName = toMcpToolName(lateDescriptor); const connectedStatus = { status: "connected" as const, toolCount: 2, transport: "stdio" as const, updatedAt: new Date(0).toISOString(), }; const connectConfiguredServers = vi.fn(async () => ({ statuses: { alpha__server: connectedStatus, beta__server: { ...connectedStatus, toolCount: 1 }, }, tools: [allowedDescriptor, blockedDescriptor, outOfScopeDescriptor], })); const listTools = vi.fn(async () => [lateDescriptor]); const status = vi.fn(async () => ({ alpha__server: { ...connectedStatus, toolCount: 1 }, })); const mcpPort = createMockMcpPort({ connectConfiguredServers, listTools, status, }); const runtime = createTestAgentRuntime( sessionId, { mcp: { enabled: true, servers: { alpha__server: { command: "alpha-mcp", type: "stdio" }, beta__server: { command: "beta-mcp", type: "stdio" }, }, }, mode: "build", workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "Explore", description: "Custom wildcard Explore.", source: "user", systemPrompt: "Use every inherited tool within the configured MCP scope.", tools: ["*"], disallowedTools: [blockedMcpToolName], mcpServers: ["alpha__server"], }, ], }, }, { eventStore, mcpPort, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_custom_explore_wildcard", name: "Agent", input: { description: "测试 wildcard MCP snapshot", prompt: "确认 custom Explore 只看到 scoped startup snapshot。", subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "父 agent 收到了 custom Explore 结果。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); const childToolNames = (request.tools ?? []).map((tool: any) => tool.name); expect(childToolNames).toContain(allowedMcpToolName); expect(childToolNames).not.toContain(blockedMcpToolName); expect(childToolNames).not.toContain(outOfScopeMcpToolName); expect(childToolNames).not.toContain(lateMcpToolName); expect(childToolNames).not.toContain("Agent"); expect(childToolNames).not.toContain("Task"); expect(childToolNames).not.toContain("EnterPlanMode"); expect(childToolNames).not.toContain("ExitPlanMode"); return { finishReason: "stop", providerMetadata: undefined, text: "custom Explore 正常启动。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("让 custom Explore 跑一下"); expect(result.response).toBe("父 agent 收到了 custom Explore 结果。"); expect(childRequests).toHaveLength(1); expect(connectConfiguredServers).toHaveBeenCalledTimes(1); expect(status).not.toHaveBeenCalled(); expect(listTools).not.toHaveBeenCalled(); }); it("does not require an MCP server for a parent-denied subagent tool", async () => { const sessionId = createSessionId("runtime-subagent-parent-denied-mcp-tool"); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const mcpPort = createMockMcpPort(); const runtime = createTestAgentRuntime( sessionId, { mcp: { enabled: true, servers: {} }, mode: "build", toolDisallowlist: ["mcp__required-server__search"], workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "mcp-agent", description: "声明了会被 parent deny 的 MCP tool。", source: "project", systemPrompt: "被 parent 移除的 MCP tool 不应该触发 server 检查。", tools: ["mcp__required-server__search", "Read"], }, ], }, }, { eventStore, mcpPort, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_mcp_agent", name: "Agent", input: { description: "测试被 deny 的 MCP tool", prompt: "检查被 parent deny 的 MCP tool 是否还会要求 server。", subagent_type: "mcp-agent", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "父 agent 收到了 mcp-agent 结果。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "mcp-agent 正常启动。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("让 mcp-agent 跑一下"); const childToolNames = childRequests[0].tools.map((tool: any) => tool.name); const secondParentRequestText = parentRequests[1].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); expect(result.response).toBe("父 agent 收到了 mcp-agent 结果。"); expect(childToolNames).toContain("Read"); expect(childToolNames).not.toContain("mcp__required-server__search"); expect(secondParentRequestText).not.toContain("Required MCP server is not connected"); }); it("returns after launching background Explore and consumes notification on the next turn", async () => { const sessionId = createSessionId("runtime-background-explore-join"); const eventStore = createTestSessionEventStore(); const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-output-")); const parentRequests: any[] = []; const childRequests: any[] = []; let releaseChild!: () => void; const childCanComplete = new Promise((resolve) => { releaseChild = resolve; }); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", subagents: { outputRootDir, }, }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_background_explore", name: "Agent", input: { description: "Find runtime join", prompt: "Find whether the parent turn waits for background results.", run_in_background: true, subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (parentRequests.length === 2) { return { finishReason: "stop", providerMetadata: undefined, text: "background explore launched", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "parent summarized the background explore result", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); await childCanComplete; return { finishReason: "stop", providerMetadata: undefined, text: "Background Explore found the join boundary in turn-stop.ts.", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); let firstTurnSettled = false; const turnPromise = runtime.executeTurn("Use a background explore agent").then((value) => { firstTurnSettled = true; return value; }); await waitForCondition(() => parentRequests.length === 2 && childRequests.length === 1); await waitForCondition(() => firstTurnSettled); const eventsBeforeChildCompletes = await eventStore.getEvents(sessionId); expect( eventsBeforeChildCompletes.some((event) => event.type === SessionEventType.TurnComplete), ).toBe(true); expect( eventsBeforeChildCompletes.some((event) => event.type === SessionEventType.SubagentStopped), ).toBe(false); const launchResult = await turnPromise; releaseChild(); await waitForCondition(async () => { const events = await eventStore.getEvents(sessionId); return events.some((event) => event.type === SessionEventType.SubagentStopped); }); await waitForCondition(async () => { const events = await eventStore.getEvents(sessionId); return events.some((event) => event.type === SessionEventType.BackgroundTaskCompleted); }); await waitForCondition(() => parentRequests.length === 3); const parentThirdRequestText = parentRequests[2].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); const events = await eventStore.getEvents(sessionId); const stoppedIndex = events.findIndex( (event) => event.type === SessionEventType.SubagentStopped, ); expect(launchResult.response).toBe("background explore launched"); expect(parentRequests).toHaveLength(3); expect(parentThirdRequestText).toContain(""); expect(parentThirdRequestText).toContain("call_background_explore"); expect(parentThirdRequestText).not.toContain("local_agent"); expect(parentThirdRequestText).toContain(""); expect(parentThirdRequestText).toContain("completed"); expect(parentThirdRequestText).toContain(""); expect(parentThirdRequestText).toContain(""); expect(parentThirdRequestText).toContain(""); expect(parentThirdRequestText).toContain(""); expect(parentThirdRequestText).toContain("Background Explore found the join boundary"); expect(stoppedIndex).toBeGreaterThanOrEqual(0); const stoppedPayload = events[stoppedIndex]?.payload as any; const agentOutputDir = join(outputRootDir, sessionId, stoppedPayload.agentId); await expect(readFile(join(agentOutputDir, "output.txt"), "utf8")).resolves.toContain( "Background Explore found the join boundary", ); await expect(readFile(join(agentOutputDir, "task.output"), "utf8")).resolves.toContain( "Background Explore found the join boundary", ); await expect(readFile(join(agentOutputDir, "transcript.jsonl"), "utf8")).rejects.toMatchObject({ code: "ENOENT", }); const metadata = JSON.parse(await readFile(join(agentOutputDir, "metadata.json"), "utf8")); expect(metadata).toMatchObject({ agentId: stoppedPayload.agentId, parentSessionId: sessionId, parentToolUseId: "call_background_explore", profileId: "Explore", status: "completed", }); expect(metadata).not.toHaveProperty("transcriptFile"); }); it("launches background Explore by default when run_in_background is requested", async () => { const sessionId = createSessionId("runtime-subagent-background-default"); const eventStore = createTestSessionEventStore(); const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-output-")); const parentRequests: any[] = []; const childRequests: any[] = []; const childGate = createDeferred(); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", subagents: { outputRootDir, }, }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_legacy_background_explore", name: "Agent", input: { description: "Legacy background", prompt: "Return after the background child finishes.", run_in_background: true, subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "foreground subagent completed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); await childGate.promise; return { finishReason: "stop", providerMetadata: undefined, text: "Background child finished after run_in_background detached.", usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 }, }; }, } as never), }, ); let turnSettled = false; const turnPromise = runtime.executeTurn("Use a legacy background explore").then((value) => { turnSettled = true; return value; }); await waitFor(async () => childRequests.length === 1); await waitFor(() => parentRequests.length === 2); await waitFor(() => turnSettled); const eventsBeforeChildCompletes = await eventStore.getEvents(sessionId); expect(turnSettled).toBe(true); expect( eventsBeforeChildCompletes.some((event) => event.type === SessionEventType.TurnComplete), ).toBe(true); expect( eventsBeforeChildCompletes.some( (event) => event.type === SessionEventType.BackgroundTaskStarted, ), ).toBe(true); expect( eventsBeforeChildCompletes.some((event) => event.type === SessionEventType.SubagentStopped), ).toBe(false); childGate.resolve(); const result = await turnPromise; await waitFor(async () => { const events = await eventStore.getEvents(sessionId); return events.some((event) => event.type === SessionEventType.BackgroundTaskCompleted); }); await waitFor(() => parentRequests.length === 3); const notificationRequestText = parentRequests[2].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); const events = await eventStore.getEvents(sessionId); expect(result.response).toBe("foreground subagent completed"); expect(parentRequests).toHaveLength(3); expect(notificationRequestText).toContain(""); expect(notificationRequestText).toContain( "call_legacy_background_explore", ); expect(notificationRequestText).toContain( "Background child finished after run_in_background detached.", ); expect(events.some((event) => event.type === SessionEventType.SubagentStopped)).toBe(true); expect(events.some((event) => event.type === SessionEventType.BackgroundTaskStarted)).toBe( true, ); expect(events.some((event) => event.type === SessionEventType.BackgroundTaskCompleted)).toBe( true, ); }); it("runs foreground subagents in parallel when run_in_background is omitted", async () => { const sessionId = createSessionId("runtime-subagent-foreground-parallel"); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const childGate = createDeferred(); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_foreground_parallel_one", name: "Agent", input: { description: "Parallel one", prompt: "Run foreground child one.", subagent_type: "Explore", }, }, { id: "call_foreground_parallel_two", name: "Agent", input: { description: "Parallel two", prompt: "Run foreground child two.", subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "foreground parallel subagents completed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } const childIndex = childRequests.length + 1; childRequests.push(request); await childGate.promise; return { finishReason: "stop", providerMetadata: undefined, text: `Parallel foreground child ${childIndex} finished.`, usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 }, }; }, } as never), }, ); let turnSettled = false; const turnPromise = runtime.executeTurn("Use parallel foreground agents").then((value) => { turnSettled = true; return value; }); await waitFor(async () => childRequests.length === 2); await new Promise((resolve) => setTimeout(resolve, 25)); const eventsBeforeChildCompletes = await eventStore.getEvents(sessionId); expect(turnSettled).toBe(false); expect( eventsBeforeChildCompletes.some((event) => event.type === SessionEventType.TurnComplete), ).toBe(false); expect( eventsBeforeChildCompletes.some( (event) => event.type === SessionEventType.BackgroundTaskStarted, ), ).toBe(false); childGate.resolve(); const result = await turnPromise; const finalParentRequestText = parentRequests[1].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); const events = await eventStore.getEvents(sessionId); const spawnEvents = events.filter((event) => event.type === SessionEventType.SubagentSpawned); expect(result.response).toBe("foreground parallel subagents completed"); expect(childRequests).toHaveLength(2); expect(parentRequests).toHaveLength(2); expect(finalParentRequestText).toContain("Parallel foreground child 1 finished."); expect(finalParentRequestText).toContain("Parallel foreground child 2 finished."); expect(spawnEvents).toHaveLength(2); expect(spawnEvents.every((event) => (event.payload as any).background !== true)).toBe(true); expect(events.some((event) => event.type === SessionEventType.BackgroundTaskStarted)).toBe( false, ); expect(events.some((event) => event.type === SessionEventType.BackgroundTaskCompleted)).toBe( false, ); }); it("can SendMessage to a running background local_agent", async () => { const sessionId = createSessionId("runtime-background-local-agent-send-message"); const eventStore = createTestSessionEventStore(); const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-output-")); const parentRequests: any[] = []; const childRequests: any[] = []; let capturedAgentId = ""; let releaseChild!: () => void; const childCanComplete = new Promise((resolve) => { releaseChild = resolve; }); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", subagents: { outputRootDir, }, }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_background_agent", name: "Agent", input: { description: "后台 agent 等待消息", prompt: "先等待父 agent 的补充消息。", run_in_background: true, subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (parentRequests.length === 2) { return { finishReason: "stop", providerMetadata: undefined, text: "background launched", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (parentRequests.length === 3) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_SendMessage_to_agent", name: SEND_MESSAGE_TOOL_NAME, input: { to: capturedAgentId, summary: "补充检查目标", message: "请把检查目标改成权限链路,并在最终回答里说明已收到消息。", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "message sent", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); if (childRequests.length === 1) { await childCanComplete; return { finishReason: "stop", providerMetadata: undefined, text: "child initial result", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "child handled coordinator message", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const launchResult = await runtime.executeTurn("启动后台 agent"); await waitForCondition(() => childRequests.length === 1); const spawnEvent = (await eventStore.getEvents(sessionId)).find( (event) => event.type === SessionEventType.SubagentSpawned, ); capturedAgentId = String((spawnEvent?.payload as any)?.agentId ?? ""); const sendResult = await runtime.executeTurn("给后台 agent 发送补充消息"); releaseChild(); await waitForCondition(() => childRequests.length >= 2); const secondChildRequestText = childRequests[1].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); expect(launchResult.response).toBe("background launched"); expect(sendResult.response).toBe("message sent"); expect(capturedAgentId).toMatch(/^agent_/u); expect(childRequests[1].messages.slice(-2).map((message: any) => message.role)).toEqual([ "assistant", "user", ]); expect(secondChildRequestText).toContain( "The coordinator sent a message while you were working:", ); expect(secondChildRequestText).toContain("补充检查目标"); expect(secondChildRequestText).toContain("Address this before completing your current task."); expect(secondChildRequestText).toContain("请把检查目标改成权限链路"); }); it("routes RespondToCoordinator to the parent and continues the child tool loop", async () => { const sessionId = createSessionId("runtime-child-respond-to-coordinator"); const eventStore = createTestSessionEventStore(); const sessionStore = createRecordingSubagentSessionStore(); const parentRequests: any[] = []; const childRequests: any[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/workspace/project", }, { eventStore, sessionStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); if (parentRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_child_response_agent", name: "Agent", input: { description: "检查权限链路", prompt: "检查权限链路,并在收到询问时回复进度后继续任务。", subagent_type: "general-purpose", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "parent received child response", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); if (childRequests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_child_todo_before_response", name: "TodoRead", input: {}, }, { id: "call_child_response_progress", name: RESPOND_TO_COORDINATOR_TOOL_NAME, input: { summary: "权限链路进度", message: "已完成前半段检查,正在继续剩余权限分支。", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (childRequests.length === 2) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_child_todo_after_response", name: "TodoRead", input: {}, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "child completed the remaining permission work", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("让 child 检查权限链路"); const secondChildRequestText = childRequests[1].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); const finalParentRequestText = parentRequests[1].messages .map((message: any) => modelMessageContentToText(message.content)) .join("\n"); expect(result.response).toBe("parent received child response"); expect(parentRequests).toHaveLength(2); expect(childRequests).toHaveLength(3); expect(childRequests[0].tools.map((tool: any) => tool.name)).toContain( RESPOND_TO_COORDINATOR_TOOL_NAME, ); expect(secondChildRequestText).toContain("was queued for the coordinator"); expect(finalParentRequestText).toContain(""); expect(finalParentRequestText).toContain("权限链路进度"); expect(finalParentRequestText).toContain( "已完成前半段检查,正在继续剩余权限分支。", ); expect(finalParentRequestText).toContain("child completed the remaining permission work"); }); it("runs built-in Explore on the main model by default while attributing usage to the subagent role", async () => { const sessionId = createSessionId("runtime-explore-main-model"); const eventStore = createTestSessionEventStore(); const childRequests: any[] = []; const childObservations: TestModelExecutionObservation[] = []; let parentCallCount = 0; const modelSelection = { providerId: createModelProviderId("provider-main"), modelId: createModelId("main-model"), }; const runtime = createTestAgentRuntime( sessionId, { mode: "build", modelSelection: createTestModelSelection(modelSelection), workingDirectory: "/workspace/project", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentCallCount++; if (parentCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_explore_main", name: "Agent", input: { description: "Find runtime loop", prompt: "Find where the runtime injects tool results.", subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); childObservations.push(observation); return { finishReason: "stop", providerMetadata: undefined, text: "Explore used the inherited main model.", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("Use an explore agent"); expect(childRequests).toHaveLength(1); // Explore 默认继承主 Loop 的 Model;usage 由 child runtime 的 taskType 归因, // 不再依赖旧 ModelSelection.role 修改模型身份。 expect(childObservations[0].model).toMatchObject({ providerId: modelSelection.providerId, modelId: modelSelection.modelId, }); expect(childObservations[0].factoryInput.providerOptions).toBeUndefined(); }); it("ignores a legacy call model and runs the built-in general-purpose agent on its configured model", async () => { const sessionId = createSessionId("runtime-general-purpose-built-in-model-override"); const eventStore = createTestSessionEventStore(); const childRequests: any[] = []; const childObservations: TestModelExecutionObservation[] = []; let parentCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", modelSelection: { providerId: createModelProviderId("provider-main"), modelId: createModelId("main-model"), }, workingDirectory: "/workspace/project", subagents: { builtInModelSelectionOverrides: { "general-purpose": { providerId: "custom-openai", modelId: "gpt-5.4", options: { reasoningLevel: "high" }, }, }, }, }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentCallCount++; if (parentCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_general_purpose_override", name: "Agent", input: { description: "Investigate default agent override", model: "haiku", prompt: "Check the configured model override.", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); childObservations.push(observation); return { finishReason: "stop", providerMetadata: undefined, text: "general-purpose used the override.", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("Use the default Agent"); expect(childRequests).toHaveLength(1); expect(childObservations[0].model).toMatchObject({ providerId: "custom-openai", modelId: "gpt-5.4", }); }); it("ignores a legacy call model and uses the built-in Explore override for child request, spawn event, and tool branch capability", async () => { const sessionId = createSessionId("runtime-explore-built-in-model-override"); const eventStore = createTestSessionEventStore(); const childRequests: any[] = []; const childObservations: TestModelExecutionObservation[] = []; let parentCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", modelSelection: { providerId: createModelProviderId("provider-main"), modelId: createModelId("main-model"), }, workingDirectory: "/workspace/project", subagents: { builtInModelSelectionOverrides: { Explore: { providerId: "custom-openai", modelId: "glm-5.2" }, }, }, }, { eventStore, modelFactory: createTestModelFactory({ properties: { supportsNativeWebSearch: true }, async generateText(request: any, observation) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentCallCount++; if (parentCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_explore_override", name: "Agent", input: { description: "Check Explore override", // 修复原因:旧 transcript 可能继续生成调用级 model;Explore 也必须只消费 profile。 model: "haiku", prompt: "Check the configured model override.", subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); childObservations.push(observation); return { finishReason: "stop", providerMetadata: undefined, text: "Explore used the override.", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("Use an explore agent"); const events = await eventStore.getEvents(sessionId); const spawnEvent = events.find((event) => event.type === SessionEventType.SubagentSpawned); const childToolNames = childRequests[0].tools.map((tool: any) => tool.name); expect(childRequests).toHaveLength(1); expect(childObservations[0].model).toMatchObject({ providerId: "custom-openai", modelId: "glm-5.2", }); expect(spawnEvent?.payload).toMatchObject({ agentType: "Explore", model: "custom-openai/glm-5.2", }); expect(childToolNames).toEqual([ "Bash", "Read", "TodoWrite", "WebFetch", "WebSearch", RESPOND_TO_COORDINATOR_TOOL_NAME, ]); }); it("freezes each custom child from the latest ModelFactory facts", async () => { const sessionId = createSessionId("runtime-subagent-concrete-model"); const eventStore = createTestSessionEventStore(); const childRequests: any[] = []; const childObservations: TestModelExecutionObservation[] = []; let parentCallCount = 0; let currentMaxOutputTokens = 64_000; const modelSelection = { providerId: createModelProviderId("provider-main"), modelId: createModelId("main-model"), }; const runtime = createTestAgentRuntime( sessionId, { mode: "build", modelSelection: createTestModelSelection(modelSelection), workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "code-reviewer", description: "Review code", source: "user", systemPrompt: "Review carefully.", modelSelection: { providerId: "custom-openai", modelId: "gpt-5.4" }, tools: ["Read"], }, ], }, }, { eventStore, modelFactory: createTestModelFactory({ maxOutputTokens: (input) => input.selection.providerId === "custom-openai" && input.selection.modelId === "gpt-5.4" ? currentMaxOutputTokens : 32_000, properties: { contextWindow: 128_000 }, async generateText(request: any, observation) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentCallCount++; if (parentCallCount <= 2) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: `call_custom_model_agent_${parentCallCount}`, name: "Agent", input: { description: "Review changed files", prompt: "Review changed files.", subagent_type: "code-reviewer", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); childObservations.push(observation); currentMaxOutputTokens = 16_000; return { finishReason: "stop", providerMetadata: undefined, text: "custom model reviewer finished.", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("Use custom reviewer"); expect(childRequests).toHaveLength(2); expect( childRequests.map((request, index) => ({ maxOutputTokens: request.options?.maxOutputTokens, modelId: childObservations[index].model.modelId, providerId: childObservations[index].model.providerId, })), ).toEqual([ { maxOutputTokens: 64_000, modelId: "gpt-5.4", providerId: "custom-openai", }, { maxOutputTokens: 16_000, modelId: "gpt-5.4", providerId: "custom-openai", }, ]); }); it("resolves an explicit profile again for a resumed child new input without changing the profile", async () => { const sessionId = createSessionId("subagent-effective-resume"); const original = { providerId: "account:personal", modelId: "fixture", options: { reasoningLevel: "high" }, }; let effective = { ...original, providerId: "account:team" }; const resolveEffectiveModelSelection = vi.fn(() => ({ effectiveSelection: effective })); const requests: string[] = []; const requestMessages: unknown[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "reviewer", description: "Review", source: "user", systemPrompt: "Review", tools: ["Read"], modelSelection: original, }, ], }, }, { eventStore: createTestSessionEventStore(), sessionStore: createRecordingSubagentSessionStore(), resolveEffectiveModelSelection, modelFactory: createTestModelFactory({ async generateText(_request, observation) { requests.push(observation.model.providerId); requestMessages.push(_request.messages); return { finishReason: "stop", text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, }), }, ); const port = (runtime as any).subagentPort; const turnId = createTurnId("effective-resume-turn"); const trace = { traceId: createTraceId("effective-resume-trace"), sessionId, turnId }; const first = await port.run({ agentType: "reviewer", description: "Review", parentToolCallId: "first", prompt: "Review first", sessionId, trace, turnId, workingDirectory: "/workspace/project", workspaceRoot: "/workspace/project", }); expect(first.status).toBe("completed"); effective = original; const resumed = await port.sendMessage({ to: first.agentId, message: "Review next", summary: "Next", parentToolCallId: "second", sessionId, trace, turnId, workingDirectory: "/workspace/project", workspaceRoot: "/workspace/project", }); expect(resumed.delivery).toBe("resumed_background"); expect((await port.waitForTask(first.agentId))?.status).toBe("completed"); expect(requests).toEqual(["account:team", "account:personal"]); expect(JSON.stringify(requestMessages[1])).toContain("Review next"); expect(JSON.stringify(requestMessages[1])).not.toContain( "The coordinator sent a message while you were working:", ); expect(resolveEffectiveModelSelection).toHaveBeenCalledTimes(2); expect(original.providerId).toBe("account:personal"); }); it("resumes a failed child with the next context profile while retaining identity and transcript", async () => { const profile = (version: string) => ({ name: "reviewer", source: "user" as const, description: "Review", tools: version === "old" ? ["Read"] : ["Grep"], systemPrompt: `PROFILE_${version}`, modelSelection: { providerId: "custom", modelId: version, options: { reasoningLevel: version === "old" ? "low" : "high" }, }, }); const scenario = setupProfileScenario(async ({ model }) => { if (model === "old") throw new Error("Invalid model configuration"); return profileDone(); }); scenario.update(profile("old")); const { runtime, store: sessionStore, children: requests } = scenario; const sessionId = runtime.sessionId; const internal = runtime as any; const turnId = createTurnId(); const trace = { traceId: createTraceId(), sessionId, turnId }; const context = { sessionId, turnId, trace, workingDirectory: "/workspace/project", workspaceRoot: "/workspace/project", }; const prepare = () => internal.prepareAgentDefinitions({ signal: new AbortController().signal, traceContext: trace, }); await prepare(); await expect( internal.subagentPort.run({ ...context, agentType: "reviewer", description: "Review", parentToolCallId: "first", prompt: "ORIGINAL_CHILD_INPUT", }), ).rejects.toThrow("Turn execution failed"); const first = Object.values(internal.runtimeTaskRegistry.all())[0] as { status: string; agentId: string; }; expect(first.status).toBe("failed"); const originalTask = await internal.subagentPort.getTask(first.agentId); scenario.update(profile("new")); // 保存发生在当前上下文内:同轮 SendMessage 仍使用旧 profile,直到下轮 prepare。 await internal.subagentPort.sendMessage({ ...context, to: first.agentId, message: "SAME_TURN_RETRY", summary: "Retry", parentToolCallId: "same-turn", }); expect((await internal.subagentPort.waitForTask(first.agentId))?.status).toBe("failed"); expect(requests.at(-1)?.model).toBe("old"); await prepare(); const resumed = await internal.subagentPort.sendMessage({ ...context, to: first.agentId, message: "RESUME_CHILD_INPUT", summary: "Next", parentToolCallId: "second", }); expect(resumed.delivery).toBe("resumed_background"); const task = await internal.subagentPort.waitForTask(first.agentId); expect(task).toMatchObject({ status: "completed", agentId: first.agentId, childSessionId: originalTask.childSessionId, }); const captured = requests.at(-1)!; const request = { ...captured, content: JSON.stringify(captured.request.messages), tools: captured.request.tools?.map((tool) => tool.name) ?? [], }; expect(request.model).toBe("new"); expect(request.reasoning).toBe("high"); expect(request.content).toContain("PROFILE_new"); expect(request.content).not.toContain("PROFILE_old"); expect(request.content).toContain("ORIGINAL_CHILD_INPUT"); expect(request.content).toContain("RESUME_CHILD_INPUT"); expect(request.tools).toContain("Grep"); expect(request.tools).not.toContain("Read"); const modelEntry = ( await sessionStore.sessionEntries({ sessionID: task.childSessionId, type: "runtime/model_selection", }) ).at(-1); expect(modelEntry?.data).toMatchObject({ modelId: "new", options: { reasoningLevel: "high" } }); const history = await sessionStore.messages({ sessionID: task.childSessionId }); expect(history.flatMap((message) => message.parts)).toEqual( expect.arrayContaining([ expect.objectContaining({ type: "timeline", timelineType: "model_change", fromModel: expect.objectContaining({ modelId: "old" }), toModel: expect.objectContaining({ modelId: "new" }), }), ]), ); scenario.load.mockResolvedValueOnce({ activeAgents: [] }); await prepare(); const missing = await internal.subagentPort.sendMessage({ ...context, to: first.agentId, message: "Again", summary: "Next", parentToolCallId: "third", }); expect(missing.status).toBe("failed"); expect(JSON.stringify(missing)).toContain("profile reviewer is unavailable"); scenario.update(profile("new")); await prepare(); const childSessions = async () => (await sessionStore.listSessions({} as never)).filter((session) => session.id !== sessionId); const sessionsBeforeMissingHistory = await childSessions(); sessionStore.messages = async () => []; await expect( internal.subagentPort.sendMessage({ ...context, to: first.agentId, message: "Again", summary: "Next", parentToolCallId: "fourth", }), ).rejects.toThrow("No transcript found"); expect(await childSessions()).toEqual(sessionsBeforeMissingHistory); }); it("fails custom profiles instead of falling back when the configured model is invalid", async () => { const sessionId = createSessionId("runtime-subagent-invalid-model"); const eventStore = createTestSessionEventStore(); const parentRequests: any[] = []; const childRequests: any[] = []; let parentCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", modelSelection: { providerId: createModelProviderId("provider-main"), modelId: createModelId("main-model"), }, workingDirectory: "/workspace/project", subagents: { profiles: [ { name: "broken-reviewer", description: "Review code", source: "user", systemPrompt: "Review carefully.", modelSelection: { providerId: "missing-provider", modelId: "missing-model" }, tools: ["Read"], }, ], }, }, { eventStore, modelFactory: ((factory) => (input: Parameters[0]) => { if ( input.selection.providerId === "missing-provider" && input.selection.modelId === "missing-model" ) { throw new Error("Invalid subagent model configuration"); } return factory(input); })( createTestModelFactory({ async generateText(request: any) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentRequests.push(request); parentCallCount++; if (parentCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_invalid_model_agent", name: "Agent", input: { description: "Review changed files", prompt: "Review changed files.", subagent_type: "broken-reviewer", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "should not run", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), ), }, ); const result = await runtime.executeTurn("Use broken reviewer"); const toolBatchCompleteEvent = result.events.find( (event) => event.type === SessionEventType.ToolBatchComplete, ); const toolMessage = parentRequests[1]?.messages.find((message: any) => message.role === "tool"); expect(toolMessage).toMatchObject({ content: "Invalid subagent model configuration", isError: true, toolName: "Agent", }); expect(toolBatchCompleteEvent?.payload).toMatchObject({ successCount: 0, errorCount: 1, }); expect(childRequests).toHaveLength(0); }); it("inherits parent model streaming for Explore child requests", async () => { const sessionId = createSessionId("runtime-explore-streaming-inheritance"); const eventStore = createTestSessionEventStore(); const streamRequests: any[] = []; const childStreamRequests: any[] = []; let parentCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", modelStreaming: "on", workingDirectory: "/workspace/project", }, { eventStore, modelFactory: createTestModelFactory({ properties: { supportsNativeWebSearch: true }, async generateText() { throw new Error("generateText should not be used when modelStreaming is on"); }, async *streamText(request: any) { streamRequests.push(request); const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentCallCount++; if (parentCallCount === 1) { yield { type: "tool_call", toolCall: { id: "call_explore_stream", name: "Agent", input: { description: "Find runtime loop", prompt: "Find where the runtime injects tool results.", subagent_type: "Explore", }, }, }; yield { type: "finish", finishReason: "tool-calls", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; return; } yield { type: "text_delta", text: "parent used streamed explore result" }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; return; } childStreamRequests.push(request); yield { type: "text_delta", text: "Explore streamed child result." }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("Use an explore agent with streaming"); expect(result.response).toBe("parent used streamed explore result"); expect(streamRequests).toHaveLength(3); expect(childStreamRequests).toHaveLength(1); expect(childStreamRequests[0].tools.map((tool: any) => tool.name)).toEqual([ "Bash", "Read", "TodoWrite", "WebFetch", "WebSearch", RESPOND_TO_COORDINATOR_TOOL_NAME, ]); }); it("uses the latest parent model config when spawning Explore", async () => { const sessionId = createSessionId("runtime-explore-model-update"); const eventStore = createTestSessionEventStore(); const childRequests: any[] = []; const childObservations: TestModelExecutionObservation[] = []; let parentCallCount = 0; const initialModelSelection = { providerId: createModelProviderId("provider-old"), modelId: createModelId("old-model"), }; const updatedModelSelection = { providerId: createModelProviderId("provider-new"), modelId: createModelId("new-model"), }; const runtime = createTestAgentRuntime( sessionId, { mode: "build", modelSelection: createTestModelSelection(initialModelSelection), workingDirectory: "/workspace/project", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { const toolNames = (request.tools ?? []).map((tool: any) => tool.name); if (toolNames.includes("Agent")) { parentCallCount++; if (parentCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "call_explore_updated_model", name: "Agent", input: { description: "Check model", prompt: "Check the inherited model.", subagent_type: "Explore", }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } childRequests.push(request); childObservations.push(observation); return { finishReason: "stop", providerMetadata: undefined, text: "child used updated model", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); runtime.setSessionModelSelection({ providerId: String(updatedModelSelection.providerId), modelId: String(updatedModelSelection.modelId), }); await runtime.executeTurn("Use an explore agent"); expect(childRequests).toHaveLength(1); expect(childObservations[0].model).toMatchObject({ providerId: updatedModelSelection.providerId, modelId: updatedModelSelection.modelId, }); }); it("does not register Agent when subagents are disabled", () => { const runtime = createTestAgentRuntime( createSessionId("runtime-explore-disabled"), { subagents: { enabled: false, }, }, { eventStore: createTestSessionEventStore(), modelFactory: createTestModelFactory({ async generateText() { throw new Error("model should not be called"); }, } as never), }, ); expect(runtime.getToolRegistry().has("Agent")).toBe(false); expect(runtime.getToolRegistry().has("Task")).toBe(false); }); }); function createDeferred(): { promise: Promise; reject: (reason?: unknown) => void; resolve: (value?: T | PromiseLike) => void; } { let resolve!: (value?: T | PromiseLike) => void; let reject!: (reason?: unknown) => void; const promise = new Promise((innerResolve, innerReject) => { resolve = (value) => innerResolve(value as T | PromiseLike); reject = innerReject; }); return { promise, reject, resolve }; } async function waitFor( predicate: () => boolean | Promise, timeoutMs = 2_500, ): Promise { const deadline = Date.now() + timeoutMs; while (Date.now() < deadline) { if (await predicate()) return; await new Promise((resolve) => setTimeout(resolve, 10)); } throw new Error("Timed out waiting for condition"); } function skillMetadata(name: string, path: string): SkillLoadOutcome["skills"][number] { return { description: `${name} description`, directory: path.slice(0, path.lastIndexOf("/")), frontmatterKeys: ["name", "description"], name, path, rootPath: "/skills", safeToAutoLoad: true, scope: "project", source: "zcode", }; } function createMockMcpPort(overrides: Partial = {}): McpPort { return { async callTool() { return { content: [{ text: "ok", type: "text" }] }; }, async close() {}, async connectConfiguredServers() { return { statuses: {}, tools: [] }; }, async connectServer() { return { status: "connected", toolCount: 0, transport: "stdio", updatedAt: new Date(0).toISOString(), }; }, async disconnectServer() { return undefined; }, async listTools() { return []; }, async status() { return {}; }, ...overrides, }; }