,
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,
{
// 本例只验证 HTTP client 透传。Explore 现在继承父会话模式,build 下非预批 URL 会请求审批,
// 所以父会话用 yolo 让 WebFetch 直接执行。
mode: "yolo",
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("projects a custom profile 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);
expect(
eventsBeforeChildCompletes.some(
(event) => event.type === SessionEventType.BackgroundTaskStarted,
),
).toBe(true);
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("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);
// 文本回复后取出的协调方消息之后是子 agent 的 token 预算提醒:子 agent 不重新起算,
// 从 0 计到当前上下文(usage 1 + 1)。
expect(childRequests[1].messages.slice(-3).map((message: any) => message.role)).toEqual([
"assistant",
"user",
"user",
]);
expect(secondChildRequestText).toContain("14999998 tokens left");
expect(secondChildRequestText).toContain(
"The coordinator sent a message while you were working:",
);
// summary 只用于 UI 预览,子 agent 只收到 message。
expect(secondChildRequestText).not.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("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.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("keeps the original task description when SendMessage resumes a terminal child", async () => {
const sessionId = createSessionId("subagent-resume-description");
const runtime = createTestAgentRuntime(
sessionId,
{ mode: "yolo", workingDirectory: "/workspace/project" },
{
eventStore: createTestSessionEventStore(),
sessionStore: createRecordingSubagentSessionStore(),
modelFactory: createTestModelFactory({
async generateText() {
return {
finishReason: "stop",
text: "done",
usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
};
},
}),
},
);
const port = (runtime as any).subagentPort;
const turnId = createTurnId("resume-description-turn");
const trace = { traceId: createTraceId("resume-description-trace"), sessionId, turnId };
const first = await port.run({
agentType: "general-purpose",
description: "Check login module",
parentToolCallId: "first",
prompt: "Check the login module",
sessionId,
trace,
turnId,
workingDirectory: "/workspace/project",
workspaceRoot: "/workspace/project",
});
expect(first.status).toBe("completed");
const resumed = await port.sendMessage({
to: first.agentId,
message: "Write two more tests",
summary: "Add tests",
parentToolCallId: "second",
sessionId,
trace,
turnId,
workingDirectory: "/workspace/project",
workspaceRoot: "/workspace/project",
});
// summary 只用于预览;恢复后的任务、完成通知和卡片仍用原描述。
expect(resumed.delivery).toBe("resumed_background");
expect((await port.waitForTask(first.agentId))?.description).toBe("Check login module");
});
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("uses the resolved child model budget for child requests and Compact", async () => {
const sessionId = createSessionId("runtime-subagent-compact-model-budget");
const sessionStore = createRecordingSubagentSessionStore();
const debug = vi.fn();
const logger = {
child: () => logger,
debug,
error: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
};
const childRequests: Array<{
maxOutputTokens: number | undefined;
querySource: string | undefined;
}> = [];
let currentMaxOutputTokens: number | undefined = 64_000;
const runtime = createTestAgentRuntime(
sessionId,
{
mode: "build",
modelSelection: {
providerId: createModelProviderId("provider-main"),
modelId: createModelId("main-model"),
},
modelContextBudgetStrategy: "legacy",
subagents: {
outputRootDir: await mkdtemp(join(tmpdir(), "zcode-subagent-compact-budget-")),
profiles: [
{
name: "compact-reviewer",
description: "Review code with compact continuity",
source: "user",
systemPrompt: "Review carefully.",
modelSelection: { providerId: "custom-openai", modelId: "gpt-5.4" },
tools: ["Read"],
},
],
},
workingDirectory: "/workspace/project",
},
{
eventStore: createTestSessionEventStore(),
logger,
modelFactory: createTestModelFactory({
maxOutputTokens: () => currentMaxOutputTokens ?? 32_000,
properties: { contextWindow: 128_000 },
async generateText(request: any, observation) {
childRequests.push({
maxOutputTokens: request.options?.maxOutputTokens,
querySource: observation.invocationContext?.metadata?.querySource,
});
const requestText = request.messages
.map((message: any) => modelMessageContentToText(message.content))
.join("\n");
if (requestText.includes("create a detailed summary")) {
return {
finishReason: "stop",
providerMetadata: undefined,
text: "Child retained the budgeted turns.",
usage: { inputTokens: 20, outputTokens: 5, totalTokens: 25 },
};
}
return {
finishReason: "stop",
providerMetadata: undefined,
text: "child turn complete",
usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
};
},
} as never),
sessionStore,
},
);
const subagentPort = (runtime as any).subagentPort;
const firstTurn = await subagentPort.run({
agentType: "compact-reviewer",
description: "Review first change",
parentToolCallId: "call_subagent_compact_first",
prompt: "Review the first change.",
sessionId,
trace: {
traceId: createTraceId("trace-subagent-compact-budget"),
sessionId,
turnId: createTurnId("turn-subagent-compact-first"),
},
turnId: createTurnId("turn-subagent-compact-first"),
workingDirectory: "/workspace/project",
workspaceRoot: "/workspace/project",
});
expect(firstTurn.status).toBe("completed");
const resumeChild = async (message: string, suffix: string) => {
const result = await subagentPort.sendMessage({
message,
parentToolCallId: `call_subagent_compact_${suffix}`,
sessionId,
summary: `Child compact ${suffix}`,
to: firstTurn.agentId,
trace: {
traceId: createTraceId(`trace-subagent-compact-${suffix}`),
sessionId,
turnId: createTurnId(`turn-subagent-compact-${suffix}`),
},
turnId: createTurnId(`turn-subagent-compact-${suffix}`),
workingDirectory: "/workspace/project",
workspaceRoot: "/workspace/project",
});
expect(result).toMatchObject({
delivery: "resumed_background",
status: "success",
taskId: firstTurn.agentId,
});
const task = await subagentPort.waitForTask(firstTurn.agentId);
expect(task?.status).toBe("completed");
};
await resumeChild("Review the second change.", "second");
await resumeChild("/compact", "manual");
currentMaxOutputTokens = undefined;
await resumeChild("Review the third change.", "fallback-third");
await resumeChild("Review the fourth change.", "fallback-fourth");
await resumeChild("/compact", "fallback-manual");
const childTask = await subagentPort.getTask(firstTurn.agentId);
const childMessages = childTask?.childSessionId
? await sessionStore.messages({ sessionID: childTask.childSessionId })
: [];
const sourceLessModelChanges = childMessages
.flatMap((message) => message.parts)
.filter(
(part) =>
part.type === "timeline" &&
part.timelineType === "model_change" &&
part.fromModel === undefined,
);
expect(
childRequests.filter(
(request) => request.querySource === "subagent" || request.querySource === "compact",
),
).toEqual([
{
maxOutputTokens: 64_000,
querySource: "subagent",
},
{
maxOutputTokens: 64_000,
querySource: "subagent",
},
{
maxOutputTokens: 20_000,
querySource: "compact",
},
{
maxOutputTokens: 32_000,
querySource: "subagent",
},
{
maxOutputTokens: 32_000,
querySource: "subagent",
},
{
maxOutputTokens: 20_000,
querySource: "compact",
},
]);
expect(sourceLessModelChanges).toHaveLength(1);
const autoCompact64KLog = debug.mock.calls
.map((call) => call[1])
.find(
(context) =>
context?.event === "compact.auto.skipped" &&
context?.contextWindow === 128_000 &&
context?.maxOutputTokens === 64_000,
);
// Compact 最多预留 21K;子请求仍使用各自的模型输出预算。
expect(autoCompact64KLog).toMatchObject({
contextWindow: 128_000,
effectiveContextWindow: 107_000,
maxOutputTokens: 64_000,
outputReserveTokens: 21_000,
threshold: 94_000,
});
const autoCompact32KLog = debug.mock.calls
.map((call) => call[1])
.find(
(context) =>
context?.event === "compact.auto.skipped" &&
context?.contextWindow === 128_000 &&
context?.maxOutputTokens === 32_000,
);
expect(autoCompact32KLog).toMatchObject({
contextWindow: 128_000,
effectiveContextWindow: 107_000,
maxOutputTokens: 32_000,
outputReserveTokens: 21_000,
threshold: 94_000,
});
});
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("can launch Explore in the background and inject completion on a later turn", async () => {
const sessionId = createSessionId("runtime-explore-background");
const eventStore = createTestSessionEventStore();
const childRequests: any[] = [];
const parentRequests: any[] = [];
let parentCallCount = 0;
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")) {
parentCallCount++;
parentRequests.push(request);
if (parentCallCount === 1) {
return {
finishReason: "tool-calls",
providerMetadata: undefined,
text: "",
toolCalls: [
{
id: "call_background_explore",
name: "Agent",
input: {
description: "Find queue",
prompt: "Find where background notifications should be queued.",
run_in_background: true,
subagent_type: "Explore",
},
},
],
usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
};
}
return {
finishReason: "stop",
providerMetadata: undefined,
text:
parentCallCount === 2 ? "background launched" : "parent saw background result",
usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
};
}
childRequests.push(request);
await childGate.promise;
return {
finishReason: "stop",
providerMetadata: undefined,
text: "Background child completed with async result.",
usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 },
};
},
} as never),
},
);
let firstTurnSettled = false;
const firstTurnPromise = runtime.executeTurn("Start background explore").then((value) => {
firstTurnSettled = true;
return value;
});
await waitFor(async () => parentRequests.length === 2 && childRequests.length === 1);
await waitFor(async () => firstTurnSettled);
const eventsBeforeChildCompletes = await eventStore.getEvents(sessionId);
expect(
eventsBeforeChildCompletes.some((event) => event.type === SessionEventType.TurnComplete),
).toBe(true);
childGate.resolve();
await waitFor(async () => {
const events = await eventStore.getEvents(sessionId);
return events.some((event) => event.type === SessionEventType.SubagentStopped);
});
await waitFor(async () => {
const events = await eventStore.getEvents(sessionId);
return events.some((event) => event.type === SessionEventType.BackgroundTaskCompleted);
});
const firstTurn = await firstTurnPromise;
const parentEvents = await eventStore.getEvents(sessionId);
const spawnEvent = parentEvents.find(
(event) => event.type === SessionEventType.SubagentSpawned,
);
const backgroundStartedEvent = parentEvents.find(
(event) => event.type === SessionEventType.BackgroundTaskStarted,
);
const backgroundCompletedEvent = parentEvents.find(
(event) => event.type === SessionEventType.BackgroundTaskCompleted,
);
const toolResultEvent = parentEvents.find(
(event) => event.type === SessionEventType.ToolCallResult,
);
if (!spawnEvent || !backgroundStartedEvent || !backgroundCompletedEvent || !toolResultEvent) {
throw new Error("Expected background subagent lifecycle events");
}
const spawnPayload = spawnEvent.payload as any;
const backgroundStartedPayload = backgroundStartedEvent.payload as any;
const backgroundCompletedPayload = backgroundCompletedEvent.payload as any;
const toolResultPayload = toolResultEvent.payload as any;
expect(spawnPayload.background).toBe(true);
expect(backgroundStartedPayload.toolName).toBe("Agent");
expect(backgroundStartedPayload.taskKind).toBe("subagent");
expect(backgroundStartedPayload.childSessionId).toBe(spawnPayload.childSessionId);
expect(backgroundStartedPayload.cancellable).toBe(true);
expect(backgroundCompletedPayload.taskKind).toBe("subagent");
expect(backgroundCompletedPayload.childSessionId).toBe(spawnPayload.childSessionId);
expect(backgroundCompletedPayload.status).toBe("completed");
expect(toolResultPayload.result.content).toContain("Async agent launched successfully.");
expect(toolResultPayload.result.content).toContain("internal ID - do not mention to user");
expect(toolResultPayload.result.content).toContain("Use SendMessage with to:");
expect(toolResultPayload.result.content).toContain("output_file:");
await waitForCondition(() => parentRequests.length === 3);
const finalRequest = parentRequests.at(-1);
const finalRequestMessages = finalRequest.messages
.map((message: any) => message.content)
.join("\n");
expect(firstTurn.response).toBe("background launched");
expect(parentRequests).toHaveLength(3);
expect(finalRequestMessages).toContain("");
expect(finalRequestMessages).toContain("Background child completed with async result");
const notificationMessage = finalRequest.messages.find((message: any) =>
modelMessageContentToText(message.content).includes(""),
);
expect(notificationMessage?.role).toBe("user");
const notificationText = modelMessageContentToText(notificationMessage?.content ?? "");
expect(notificationText).toMatch(
/^\n\[SYSTEM NOTIFICATION - NOT USER INPUT\]\n/,
);
expect(notificationText).toMatch(/\n<\/system-reminder>$/);
expect(notificationText.match(/<\/?system-reminder>/g)).toEqual([
"",
"",
]);
});
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,
};
}
describe("session-owned subagent shutdown", () => {
it.each(["foreground", "background", "auto-background", "resume", "startup"])(
"closes %s admission and waits for actual child cleanup",
async (mode) => {
const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-owned-agent-"));
const ready = createDeferred();
const cancelled = createDeferred();
const cleanup = createDeferred();
const sessionId = createSessionId("owned-close");
let childSignal: AbortSignal | undefined;
let runs = 0;
const port = createExploreSubagentPort({
outputRootDir,
autoBackgroundMs: mode === "auto-background" ? 1 : undefined,
emitParentEvent: async () => undefined,
enqueueParentTaskNotification: () => undefined,
runExploreAgent: async (request, options) => {
if (mode === "resume" && runs++ === 0)
return { response: "done", events: [], traceId: request.traceContext.traceId };
childSignal = options?.signal;
if (mode !== "startup") await request.onSessionReady?.();
ready.resolve();
await new Promise((resolve) => {
if (childSignal?.aborted) resolve();
else childSignal?.addEventListener("abort", () => resolve(), { once: true });
});
cancelled.resolve();
await cleanup.promise;
throw childSignal?.reason;
},
});
const request = {
agentType: "Explore",
description: "owned work",
prompt: "inspect",
sessionId,
trace: { traceId: createTraceId(), sessionId },
workingDirectory: outputRootDir,
workspaceRoot: outputRootDir,
};
const completed = mode === "resume" ? await port.run(request) : undefined;
const launched = completed
? port.sendMessage!({
...request,
to: completed.agentId,
message: "continue",
summary: "resume",
})
: mode === "background" || mode === "startup"
? port.start!(request)
: port.run(request);
void launched.catch(() => undefined);
await ready.promise;
if (mode !== "foreground" && mode !== "startup") await launched;
let closed = false;
const closing = port.close!().then(() => {
closed = true;
});
await cancelled.promise;
expect(childSignal?.aborted).toBe(true);
expect(closed).toBe(false);
await expect(port.start!(request)).rejects.toThrow(/closed/i);
cleanup.resolve();
await closing;
await port.close!();
expect(closed).toBe(true);
},
);
it("closing one owner leaves an independent background agent running", async () => {
const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-owner-isolation-"));
const signals: AbortSignal[] = [];
const makeOwner = () =>
createExploreSubagentPort({
outputRootDir,
emitParentEvent: async () => undefined,
enqueueParentTaskNotification: () => undefined,
runExploreAgent: async (request, options) => {
const signal = options!.signal!;
signals.push(signal);
await request.onSessionReady?.();
await new Promise((resolve) => {
if (signal.aborted) resolve();
else signal.addEventListener("abort", () => resolve(), { once: true });
});
throw signal.reason;
},
});
const left = makeOwner();
const right = makeOwner();
const request = (id: string) => ({
agentType: "Explore",
description: "isolated",
prompt: "wait",
sessionId: createSessionId(id),
trace: { traceId: createTraceId(), sessionId: createSessionId(id) },
workingDirectory: outputRootDir,
workspaceRoot: outputRootDir,
});
try {
await left.start!(request("left-owner"));
const task = await right.start!(request("right-owner"));
await left.close!();
expect(signals[0]?.aborted).toBe(true);
expect(signals[1]?.aborted).toBe(false);
expect((await right.getTask?.(task.agentId))?.status).toBe("running");
} finally {
await Promise.all([left.close!(), right.close!()]);
}
});
});