Skip to content

Commit 91aa0e2

Browse files
Joey Barkleyclaude
andcommitted
fix(opencode): accept agent and agent_type as task tool parameter aliases
Non-Claude models consistently send "agent" or "agent_type" instead of "subagent_type" when calling the task tool, causing repeated Zod validation failures. Accept all three parameter names with a clear precedence order: subagent_type > agent > agent_type. Fixes anomalyco#20804 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 8daeacc commit 91aa0e2

3 files changed

Lines changed: 94 additions & 7 deletions

File tree

‎packages/opencode/src/tool/task.ts‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,18 @@ import { Permission } from "@/permission"
1515
const parameters = z.object({
1616
description: z.string().describe("A short (3-5 words) description of the task"),
1717
prompt: z.string().describe("The task for the agent to perform"),
18-
subagent_type: z.string().describe("The type of specialized agent to use for this task"),
18+
subagent_type: z
19+
.string()
20+
.describe("The type of specialized agent to use for this task")
21+
.optional(),
22+
agent: z
23+
.string()
24+
.describe("Alias for subagent_type — the agent to use for this task")
25+
.optional(),
26+
agent_type: z
27+
.string()
28+
.describe("Alias for subagent_type — the agent to use for this task")
29+
.optional(),
1930
task_id: z
2031
.string()
2132
.describe(
@@ -25,6 +36,19 @@ const parameters = z.object({
2536
command: z.string().describe("The command that triggered this task").optional(),
2637
})
2738

39+
/**
40+
* Resolve the agent type from params, accepting "agent" and "agent_type" as
41+
* aliases for "subagent_type". Non-Claude models frequently use the shorter
42+
* parameter names when calling the task tool.
43+
*/
44+
export function resolveAgentType(params: z.infer<typeof parameters>): string {
45+
const resolved = params.subagent_type || params.agent || params.agent_type
46+
if (!resolved) {
47+
throw new Error("One of subagent_type, agent, or agent_type is required")
48+
}
49+
return resolved
50+
}
51+
2852
export const TaskTool = Tool.define("task", async (ctx) => {
2953
const agents = await Agent.list().then((x) => x.filter((a) => a.mode !== "primary"))
3054

@@ -45,23 +69,25 @@ export const TaskTool = Tool.define("task", async (ctx) => {
4569
description,
4670
parameters,
4771
async execute(params: z.infer<typeof parameters>, ctx) {
72+
const agentType = resolveAgentType(params)
73+
4874
const config = await Config.get()
4975

5076
// Skip permission check when user explicitly invoked via @ or command subtask
5177
if (!ctx.extra?.bypassAgentCheck) {
5278
await ctx.ask({
5379
permission: "task",
54-
patterns: [params.subagent_type],
80+
patterns: [agentType],
5581
always: ["*"],
5682
metadata: {
5783
description: params.description,
58-
subagent_type: params.subagent_type,
84+
subagent_type: agentType,
5985
},
6086
})
6187
}
6288

63-
const agent = await Agent.get(params.subagent_type)
64-
if (!agent) throw new Error(`Unknown agent type: ${params.subagent_type} is not a valid agent type`)
89+
const agent = await Agent.get(agentType)
90+
if (!agent) throw new Error(`Unknown agent type: ${agentType} is not a valid agent type`)
6591

6692
const hasTaskPermission = agent.permission.some((rule) => rule.permission === "task")
6793
const hasTodoWritePermission = agent.permission.some((rule) => rule.permission === "todowrite")

‎packages/opencode/test/session/prompt-effect.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,9 @@ it.live(
628628
parameters: z.object({
629629
description: z.string(),
630630
prompt: z.string(),
631-
subagent_type: z.string(),
631+
subagent_type: z.string().optional(),
632+
agent: z.string().optional(),
633+
agent_type: z.string().optional(),
632634
task_id: z.string().optional(),
633635
command: z.string().optional(),
634636
}),

‎packages/opencode/test/tool/task.test.ts‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { afterEach, describe, expect, test } from "bun:test"
22
import { Agent } from "../../src/agent/agent"
33
import { Instance } from "../../src/project/instance"
4-
import { TaskTool } from "../../src/tool/task"
4+
import { TaskTool, resolveAgentType } from "../../src/tool/task"
55
import { tmpdir } from "../fixture/fixture"
66

77
afterEach(async () => {
@@ -47,3 +47,62 @@ describe("tool.task", () => {
4747
})
4848
})
4949
})
50+
51+
describe("resolveAgentType", () => {
52+
test("returns subagent_type when provided", () => {
53+
const result = resolveAgentType({
54+
description: "test",
55+
prompt: "test",
56+
subagent_type: "Engineer",
57+
})
58+
expect(result).toBe("Engineer")
59+
})
60+
61+
test("falls back to agent when subagent_type is missing", () => {
62+
const result = resolveAgentType({
63+
description: "test",
64+
prompt: "test",
65+
agent: "Engineer",
66+
})
67+
expect(result).toBe("Engineer")
68+
})
69+
70+
test("falls back to agent_type when subagent_type and agent are missing", () => {
71+
const result = resolveAgentType({
72+
description: "test",
73+
prompt: "test",
74+
agent_type: "Engineer",
75+
})
76+
expect(result).toBe("Engineer")
77+
})
78+
79+
test("prefers subagent_type over agent and agent_type", () => {
80+
const result = resolveAgentType({
81+
description: "test",
82+
prompt: "test",
83+
subagent_type: "Architect",
84+
agent: "Engineer",
85+
agent_type: "Designer",
86+
})
87+
expect(result).toBe("Architect")
88+
})
89+
90+
test("prefers agent over agent_type when subagent_type is missing", () => {
91+
const result = resolveAgentType({
92+
description: "test",
93+
prompt: "test",
94+
agent: "Engineer",
95+
agent_type: "Designer",
96+
})
97+
expect(result).toBe("Engineer")
98+
})
99+
100+
test("throws when no agent parameter is provided", () => {
101+
expect(() =>
102+
resolveAgentType({
103+
description: "test",
104+
prompt: "test",
105+
}),
106+
).toThrow("One of subagent_type, agent, or agent_type is required")
107+
})
108+
})

0 commit comments

Comments
 (0)