pub fn resolve_agent(
dir: &Path,
name: &str,
) -> Result<AgentConfig, ResolveAgentError>Expand description
Resolve a named agent’s config, disk taking precedence over the embedded default roster (#3046 — the last gap in the #2958 embedded-roster arc).
Why: Slice E1-E3 (#2958) built a working embedded fallback for
load_all_agents (the dir-wide scan), but five other production call
sites — runner::in_process::InProcessAgentRunner::load_agent,
run_task::execute_run_task’s and task::executor’s PM-config loads,
run_task::resolve_agent_model_slug, and
tools::delegate::DelegateToAgentTool’s pre-flight check — read
<dir>/<name>.md directly by single-agent name, with no embedded
consultation at all, so the 34-agent roster was unreachable from a real
CLI run on a fresh project with no .claude/agents/. This is the ONE
shared resolution helper all of them now route through, so the
disk-wins/embedded-fallback precedence is written and tested exactly
once.
What: If <dir>/<name>.md EXISTS, loads it via md_loader::load_md_agent
— a parse/compose error on an EXISTING disk file is returned as-is
(ResolveAgentError::Load), NEVER silently falling through to an
embedded copy of the same name: a project that deliberately overrode
name gets its own broken override surfaced, not masked. If the disk
file does not exist, looks name up in crate::assets::DEFAULT_AGENTS:
a Direct entry projects via [md_loader::project_embedded_md], and a
malformed permissions: block is ResolveAgentError::Load (#7948); a
Composed entry projects via
md_loader::project_embedded_md_with_extends, and on Err (a
build-time asset defect — see [load_embedded_default_agents]’s
identical handling) logs tracing::error! and falls through to the
not-found case below rather than propagating. If nothing matches
anywhere, returns ResolveAgentError::NotFound with the disk ∪ embedded
name list (available_agent_names) for a caller to surface.
A namespaced <plugin>:<local-name> (issue #3539 — Phase 1 Claude Code
plugin support) routes to crate::plugins::agents::find_plugin_agent_config
instead of the disk/embedded precedence below: plugin agents are an
independent, additive tier, never falling back to (or being shadowed
by) disk/embedded resolution of the bare local name.
Test: resolve_agent_disk_wins_over_embedded,
resolve_agent_falls_back_to_embedded_when_disk_misses,
resolve_agent_disk_parse_error_does_not_fall_back_to_embedded,
resolve_agent_unknown_name_lists_available_agents,
resolve_agent_namespaced_name_resolves_plugin_agent,
resolve_agent_namespaced_unknown_plugin_is_not_found.