Skip to content

Commit a9281f6

Browse files
committed
fix(code): fix subagent sessions
1 parent 25fb4cf commit a9281f6

3 files changed

Lines changed: 112 additions & 13 deletions

File tree

apps/code/src/main/services/agent/service.ts

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,56 @@ const onAgentLog: OnLogCallback = (level, scope, message, data) => {
178178
}
179179
};
180180

181+
const HAIKU_EXPLORE_AGENT_OVERRIDE = {
182+
description:
183+
'Fast agent for exploring and understanding codebases. Use this when you need to find files by pattern (eg. "src/components/**/*.tsx"), search for code or keywords (eg. "where is the auth middleware?"), or answer questions about how the codebase works (eg. "how does the session service handle reconnects?"). When calling this agent, specify a thoroughness level: "quick" for targeted lookups, "medium" for broader exploration, or "very thorough" for comprehensive analysis across multiple locations.',
184+
model: "haiku",
185+
prompt: `You are a fast, read-only codebase exploration agent.
186+
187+
Your job is to find files, search code, read the most relevant sources, and report findings clearly.
188+
189+
Rules:
190+
- Never create, modify, delete, move, or copy files.
191+
- Never use shell redirection or any command that changes system state.
192+
- Use Glob for broad file pattern matching.
193+
- Use Grep for searching file contents.
194+
- Use Read when you know the exact file path to inspect.
195+
- Use Bash only for safe read-only commands like ls, git status, git log, git diff, find, cat, head, and tail.
196+
- Adapt your search approach based on the thoroughness level specified by the caller.
197+
- Return file paths as absolute paths in your final response.
198+
- Avoid using emojis.
199+
- Wherever possible, spawn multiple parallel tool calls for grepping and reading files.
200+
- Search efficiently, then read only the most relevant files.
201+
- Return findings directly in your final response — do not create files.`,
202+
tools: [
203+
"Bash",
204+
"Glob",
205+
"Grep",
206+
"Read",
207+
"WebFetch",
208+
"WebSearch",
209+
"NotebookRead",
210+
"TodoWrite",
211+
],
212+
};
213+
214+
function buildClaudeCodeOptions(args: {
215+
additionalDirectories?: string[];
216+
effort?: EffortLevel;
217+
plugins: { type: "local"; path: string }[];
218+
}) {
219+
return {
220+
...(args.additionalDirectories?.length && {
221+
additionalDirectories: args.additionalDirectories,
222+
}),
223+
...(args.effort && { effort: args.effort }),
224+
plugins: args.plugins,
225+
agents: {
226+
"ph-explore": HAIKU_EXPLORE_AGENT_OVERRIDE,
227+
},
228+
};
229+
}
230+
181231
interface SessionConfig {
182232
taskId: string;
183233
taskRunId: string;
@@ -631,6 +681,11 @@ When creating pull requests, add the following footer at the end of the PR descr
631681
},
632682
...externalPlugins,
633683
];
684+
const claudeCodeOptions = buildClaudeCodeOptions({
685+
additionalDirectories,
686+
effort,
687+
plugins,
688+
});
634689

635690
let configOptions: SessionConfigOption[] | undefined;
636691
let agentSessionId: string;
@@ -679,13 +734,7 @@ When creating pull requests, add the following footer at the end of the PR descr
679734
...(permissionMode && { permissionMode }),
680735
...(model != null && { model }),
681736
claudeCode: {
682-
options: {
683-
...(additionalDirectories?.length && {
684-
additionalDirectories,
685-
}),
686-
...(effort && { effort }),
687-
plugins,
688-
},
737+
options: claudeCodeOptions,
689738
},
690739
},
691740
});
@@ -712,11 +761,7 @@ When creating pull requests, add the following footer at the end of the PR descr
712761
...(permissionMode && { permissionMode }),
713762
...(model != null && { model }),
714763
claudeCode: {
715-
options: {
716-
...(additionalDirectories?.length && { additionalDirectories }),
717-
...(effort && { effort }),
718-
plugins,
719-
},
764+
options: claudeCodeOptions,
720765
},
721766
},
722767
});

packages/agent/src/adapters/claude/hooks.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,56 @@ export const createPostToolUseHook =
7171
return { continue: true };
7272
};
7373

74+
/**
75+
* Rewrites Agent tool calls targeting built-in subagent types to use our custom
76+
* definitions instead. This works around a Claude Agent SDK bug where
77+
* `options.agents` cannot override built-in agent definitions because the
78+
* built-ins appear first in the agents array and `Array.find()` returns the
79+
* first match.
80+
*
81+
* By giving our custom agent a different name (e.g. "ph-explore") and rewriting
82+
* the subagent_type in the tool input, we sidestep the collision entirely.
83+
*
84+
* https://github.com/anthropics/claude-agent-sdk-typescript/issues/267
85+
*/
86+
const SUBAGENT_REWRITES: Record<string, string> = {
87+
Explore: "ph-explore",
88+
};
89+
90+
export const createSubagentRewriteHook =
91+
(logger: Logger): HookCallback =>
92+
async (input: HookInput, _toolUseID: string | undefined) => {
93+
if (input.hook_event_name !== "PreToolUse") {
94+
return { continue: true };
95+
}
96+
97+
if (input.tool_name !== "Agent") {
98+
return { continue: true };
99+
}
100+
101+
const toolInput = input.tool_input as Record<string, unknown> | undefined;
102+
const subagentType = toolInput?.subagent_type;
103+
if (typeof subagentType !== "string" || !SUBAGENT_REWRITES[subagentType]) {
104+
return { continue: true };
105+
}
106+
107+
const target = SUBAGENT_REWRITES[subagentType];
108+
logger.info(
109+
`[SubagentRewriteHook] Rewriting subagent_type: ${subagentType}${target}`,
110+
);
111+
112+
return {
113+
continue: true,
114+
hookSpecificOutput: {
115+
hookEventName: "PreToolUse" as const,
116+
updatedInput: {
117+
...toolInput,
118+
subagent_type: target,
119+
},
120+
},
121+
};
122+
};
123+
74124
export const createPreToolUseHook =
75125
(settingsManager: SettingsManager, logger: Logger): HookCallback =>
76126
async (input: HookInput, _toolUseID: string | undefined) => {

packages/agent/src/adapters/claude/session/options.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { Logger } from "../../../utils/logger";
1414
import {
1515
createPostToolUseHook,
1616
createPreToolUseHook,
17+
createSubagentRewriteHook,
1718
type OnModeChange,
1819
} from "../hooks";
1920
import type { CodeExecutionMode } from "../tools";
@@ -117,7 +118,10 @@ function buildHooks(
117118
PreToolUse: [
118119
...(userHooks?.PreToolUse || []),
119120
{
120-
hooks: [createPreToolUseHook(settingsManager, logger)],
121+
hooks: [
122+
createPreToolUseHook(settingsManager, logger),
123+
createSubagentRewriteHook(logger),
124+
],
121125
},
122126
],
123127
};

0 commit comments

Comments
 (0)