use crate::policy_prompt::builtin_subagent_role_instructions;
use merry_core::ToolName;
use merry_llm::{GenerationConfig, ModelName, ModelProvider, ModelRetryPolicy, ReasoningEffort};
use merry_runtime::{ChildWorkspaceScope, ParentPrimaryModel, SubagentTaskSpec};
use std::{
collections::{BTreeMap, BTreeSet},
fs,
path::Path,
sync::Arc,
};
use thiserror::Error;
pub const MAX_ROLE_PROMPT_FILE_BYTES: usize = 32 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SubagentWorkspaceAccess {
ReadOnlyWorkspace,
ReadWriteWorkspace,
}
#[derive(Clone)]
pub struct SubagentModelOverride {
pub provider: Arc<dyn ModelProvider>,
pub model: ModelName,
pub retry_policy: Option<ModelRetryPolicy>,
}
#[derive(Clone, Default)]
pub struct SubagentDefaultOverride {
pub model_override: Option<SubagentModelOverride>,
pub reasoning_effort: Option<ReasoningEffort>,
pub max_model_turns: Option<u32>,
pub timeout_secs: Option<u64>,
pub max_total_tokens: Option<u64>,
}
#[derive(Clone)]
pub struct SubagentRoleSpec {
pub name: String,
pub description: String,
pub enabled: bool,
pub workspace_access: SubagentWorkspaceAccess,
pub default_allowed_tools: Option<Vec<ToolName>>,
pub default_max_model_turns: u32,
pub max_model_turns_override: Option<u32>,
pub default_timeout_secs: Option<u64>,
pub max_total_tokens: Option<u64>,
pub instructions: Option<String>,
pub model_override: Option<SubagentModelOverride>,
pub reasoning_effort: Option<ReasoningEffort>,
}
#[derive(Clone, Default)]
pub struct SubagentRoleOverlay {
pub base: Option<String>,
pub enabled: Option<bool>,
pub description: Option<String>,
pub workspace_access: Option<SubagentWorkspaceAccess>,
pub model_override: Option<SubagentModelOverride>,
pub reasoning_effort: Option<ReasoningEffort>,
pub max_model_turns: Option<u32>,
pub timeout_secs: Option<u64>,
pub max_total_tokens: Option<u64>,
pub allowed_tools: Option<Vec<ToolName>>,
pub instructions: Option<String>,
}
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum SubagentRoleError {
#[error("invalid subagent role name {role:?}: use ASCII letters, digits, '_' or '-'")]
InvalidRoleName { role: String },
#[error("unknown base role {base:?} for subagent role {role:?}")]
UnknownBaseRole { role: String, base: String },
#[error("unknown subagent role {role:?}; available roles: {available}")]
UnknownRole { role: String, available: String },
#[error("subagent role {role:?} is disabled")]
DisabledRole { role: String },
#[error("invalid {field} for subagent role {role:?}: must be greater than zero")]
ZeroBudget { role: String, field: &'static str },
#[error("failed to load prompt_file {path:?} for subagent role {role:?}: {reason}")]
InvalidPromptFile {
role: String,
path: String,
reason: String,
},
}
#[derive(Clone)]
pub struct SubagentRoleCatalog {
default_override: SubagentDefaultOverride,
roles: BTreeMap<String, SubagentRoleSpec>,
}
impl Default for SubagentRoleCatalog {
fn default() -> Self {
Self::builtin()
}
}
impl SubagentRoleCatalog {
#[must_use]
pub fn builtin() -> Self {
let mut roles = BTreeMap::new();
roles.insert(
"explore".to_owned(),
SubagentRoleSpec {
name: "explore".to_owned(),
description: "fast read-only codebase researcher".to_owned(),
enabled: true,
workspace_access: SubagentWorkspaceAccess::ReadOnlyWorkspace,
default_allowed_tools: Some(tool_names(&["read_file", "run_process", "web_fetch"])),
default_max_model_turns: 30,
max_model_turns_override: None,
default_timeout_secs: Some(180),
max_total_tokens: None,
instructions: builtin_subagent_role_instructions("explore").map(str::to_owned),
model_override: None,
reasoning_effort: None,
},
);
roles.insert(
"coder".to_owned(),
SubagentRoleSpec {
name: "coder".to_owned(),
description: "scoped implementation and local verification".to_owned(),
enabled: true,
workspace_access: SubagentWorkspaceAccess::ReadWriteWorkspace,
default_allowed_tools: None,
default_max_model_turns: 80,
max_model_turns_override: None,
default_timeout_secs: Some(600),
max_total_tokens: None,
instructions: builtin_subagent_role_instructions("coder").map(str::to_owned),
model_override: None,
reasoning_effort: None,
},
);
roles.insert(
"review".to_owned(),
SubagentRoleSpec {
name: "review".to_owned(),
description: "independent skeptical code review and verification".to_owned(),
enabled: true,
workspace_access: SubagentWorkspaceAccess::ReadWriteWorkspace,
default_allowed_tools: Some(tool_names(&[
"read_file",
"run_process",
"request_permissions",
"web_fetch",
"browser_tab",
"browser_network",
"read_background_process",
"stop_background_process",
])),
default_max_model_turns: 40,
max_model_turns_override: None,
default_timeout_secs: Some(300),
max_total_tokens: None,
instructions: builtin_subagent_role_instructions("review").map(str::to_owned),
model_override: None,
reasoning_effort: None,
},
);
Self {
default_override: SubagentDefaultOverride::default(),
roles,
}
}
#[must_use]
pub fn with_default_override(mut self, default_override: SubagentDefaultOverride) -> Self {
self.default_override = default_override;
self
}
#[must_use]
pub fn default_override(&self) -> &SubagentDefaultOverride {
&self.default_override
}
pub fn apply_role_overlay(
&mut self,
name: &str,
overlay: SubagentRoleOverlay,
) -> Result<(), SubagentRoleError> {
validate_role_name(name)?;
if overlay.max_model_turns == Some(0) {
return Err(SubagentRoleError::ZeroBudget {
role: name.to_owned(),
field: "max_model_turns",
});
}
if overlay.timeout_secs == Some(0) {
return Err(SubagentRoleError::ZeroBudget {
role: name.to_owned(),
field: "timeout_secs",
});
}
if overlay.max_total_tokens == Some(0) {
return Err(SubagentRoleError::ZeroBudget {
role: name.to_owned(),
field: "max_total_tokens",
});
}
let mut spec = if let Some(base_name) = overlay.base.as_deref() {
let mut inherited = self.roles.get(base_name).cloned().ok_or_else(|| {
SubagentRoleError::UnknownBaseRole {
role: name.to_owned(),
base: base_name.to_owned(),
}
})?;
inherited.name = name.to_owned();
inherited
} else if let Some(existing) = self.roles.get(name) {
existing.clone()
} else {
SubagentRoleSpec {
name: name.to_owned(),
description: format!("custom {name} specialist"),
enabled: true,
workspace_access: SubagentWorkspaceAccess::ReadWriteWorkspace,
default_allowed_tools: None,
default_max_model_turns: 80,
max_model_turns_override: None,
default_timeout_secs: Some(600),
max_total_tokens: None,
instructions: None,
model_override: None,
reasoning_effort: None,
}
};
if let Some(enabled) = overlay.enabled {
spec.enabled = enabled;
}
if let Some(description) = overlay.description {
spec.description = description;
}
if let Some(access) = overlay.workspace_access {
spec.workspace_access = access;
}
if let Some(model_override) = overlay.model_override {
spec.model_override = Some(model_override);
}
if let Some(reasoning_effort) = overlay.reasoning_effort {
spec.reasoning_effort = Some(reasoning_effort);
}
if let Some(turns) = overlay.max_model_turns {
spec.default_max_model_turns = turns;
spec.max_model_turns_override = Some(turns);
}
if let Some(timeout) = overlay.timeout_secs {
spec.default_timeout_secs = Some(timeout);
}
if let Some(tokens) = overlay.max_total_tokens {
spec.max_total_tokens = Some(tokens);
}
if let Some(tools) = overlay.allowed_tools {
spec.default_allowed_tools = Some(tools);
}
if let Some(instructions) = overlay.instructions {
spec.instructions = Some(instructions);
}
self.roles.insert(name.to_owned(), spec);
Ok(())
}
pub fn apply_toggles(
&mut self,
toggles: &BTreeMap<String, bool>,
) -> Result<(), SubagentRoleError> {
for (name, enabled) in toggles {
let Some(spec) = self.roles.get_mut(name) else {
return Err(SubagentRoleError::UnknownRole {
role: name.clone(),
available: self.available_role_list(),
});
};
spec.enabled = *enabled;
}
Ok(())
}
#[must_use]
pub fn role(&self, name: &str) -> Option<&SubagentRoleSpec> {
self.roles.get(name)
}
#[must_use]
pub fn enabled_role_summaries(&self) -> Vec<(String, String)> {
self.roles
.values()
.filter(|spec| spec.enabled)
.map(|spec| (spec.name.clone(), spec.description.clone()))
.collect()
}
fn available_role_list(&self) -> String {
self.roles
.values()
.filter(|spec| spec.enabled)
.map(|spec| spec.name.as_str())
.collect::<Vec<_>>()
.join(", ")
}
}
pub struct SubagentResolutionInput<'a> {
pub task: &'a SubagentTaskSpec,
pub parent_allowed_tools: &'a [ToolName],
pub workspace_scope: &'a ChildWorkspaceScope,
pub parent_workspace_writable: bool,
pub depth: u8,
pub max_depth: u8,
pub max_model_turns_cap: u32,
pub parent_primary_model: Option<&'a ParentPrimaryModel>,
pub fallback_provider: Arc<dyn ModelProvider>,
pub fallback_model: &'a ModelName,
pub parent_generation_config: &'a GenerationConfig,
}
pub struct ResolvedSubagentRuntime {
pub role_name: String,
pub provider: Arc<dyn ModelProvider>,
pub model: ModelName,
pub retry_policy: Option<ModelRetryPolicy>,
pub generation_config: GenerationConfig,
pub workspace_writable: bool,
pub allowed_tools: Vec<ToolName>,
pub max_model_turns: u32,
pub timeout_secs: Option<u64>,
pub max_total_tokens: Option<u64>,
pub role_instructions: Option<String>,
}
pub fn resolve_subagent_runtime(
catalog: &SubagentRoleCatalog,
input: &SubagentResolutionInput<'_>,
) -> Result<ResolvedSubagentRuntime, SubagentRoleError> {
let requested_role = input.task.role().unwrap_or("coder");
let Some(role) = catalog.role(requested_role) else {
return Err(SubagentRoleError::UnknownRole {
role: requested_role.to_owned(),
available: catalog.available_role_list(),
});
};
if !role.enabled {
return Err(SubagentRoleError::DisabledRole {
role: role.name.clone(),
});
}
let task_forbids_writes =
input.task.write_scope_is_explicit() && input.workspace_scope.write_scope().is_empty();
let workspace_writable = input.parent_workspace_writable
&& role.workspace_access == SubagentWorkspaceAccess::ReadWriteWorkspace
&& !task_forbids_writes;
let mut allowed_tools = if input.task.allowed_tools_are_explicit() {
input.parent_allowed_tools.to_vec()
} else if let Some(role_tools) = role.default_allowed_tools.as_deref() {
let role_set: BTreeSet<&str> = role_tools.iter().map(ToolName::as_str).collect();
input
.parent_allowed_tools
.iter()
.filter(|tool| role_set.contains(tool.as_str()))
.cloned()
.collect()
} else {
input.parent_allowed_tools.to_vec()
};
if !workspace_writable {
allowed_tools.retain(|tool| tool.as_str() != "apply_patch");
}
allowed_tools = prune_orphaned_tools(allowed_tools, input.depth, input.max_depth);
let (provider, model, retry_policy) = if let Some(override_cfg) = role.model_override.as_ref() {
(
Arc::clone(&override_cfg.provider),
override_cfg.model.clone(),
override_cfg.retry_policy,
)
} else if let Some(default_cfg) = catalog.default_override.model_override.as_ref() {
(
Arc::clone(&default_cfg.provider),
default_cfg.model.clone(),
default_cfg.retry_policy,
)
} else if let Some(live) = input.parent_primary_model {
(
Arc::clone(&live.provider),
live.model.clone(),
Some(live.retry_policy),
)
} else {
(
Arc::clone(&input.fallback_provider),
input.fallback_model.clone(),
None,
)
};
let mut generation_config = input.parent_generation_config.clone();
if let Some(effort) = catalog.default_override.reasoning_effort.as_ref() {
generation_config = generation_config.with_reasoning_effort(Some(effort.clone()));
}
if let Some(effort) = role.reasoning_effort.as_ref() {
generation_config = generation_config.with_reasoning_effort(Some(effort.clone()));
}
if let Some(effort) = input.task.reasoning_effort() {
generation_config = generation_config.with_reasoning_effort(Some(effort.clone()));
}
let max_model_turns = if input.task.max_model_turns_is_explicit() {
input.task.max_model_turns()
} else {
role.max_model_turns_override
.or(catalog.default_override.max_model_turns)
.unwrap_or(role.default_max_model_turns)
.min(input.max_model_turns_cap.max(1))
};
let timeout_secs = input
.task
.timeout_secs()
.or(role.default_timeout_secs)
.or(catalog.default_override.timeout_secs);
let max_total_tokens = input
.task
.max_total_tokens()
.or(role.max_total_tokens)
.or(catalog.default_override.max_total_tokens);
Ok(ResolvedSubagentRuntime {
role_name: role.name.clone(),
provider,
model,
retry_policy,
generation_config,
workspace_writable,
allowed_tools,
max_model_turns,
timeout_secs,
max_total_tokens,
role_instructions: role.instructions.clone(),
})
}
fn prune_orphaned_tools(mut tools: Vec<ToolName>, depth: u8, max_depth: u8) -> Vec<ToolName> {
let can_spawn = depth < max_depth && tools.iter().any(|t| t.as_str() == "spawn_subagents");
if !can_spawn {
tools.retain(|tool| {
!matches!(
tool.as_str(),
"spawn_subagents" | "send_subagent_message" | "wait_subagents" | "cancel_subagents"
)
});
}
let can_run_process = tools.iter().any(|t| t.as_str() == "run_process");
if !can_run_process {
tools.retain(|tool| {
!matches!(
tool.as_str(),
"read_background_process" | "stop_background_process"
)
});
}
tools
}
pub fn load_workspace_prompt_file(
workspace_root: &Path,
role: &str,
relative_path: &str,
) -> Result<String, SubagentRoleError> {
let trimmed = relative_path.trim();
let rel = Path::new(trimmed);
if trimmed.is_empty()
|| rel.is_absolute()
|| rel
.components()
.any(|c| matches!(c, std::path::Component::ParentDir))
{
return Err(SubagentRoleError::InvalidPromptFile {
role: role.to_owned(),
path: relative_path.to_owned(),
reason: "path must be workspace-relative without parent traversal".to_owned(),
});
}
let full_path = workspace_root.join(rel);
let canonical_root =
fs::canonicalize(workspace_root).map_err(|error| SubagentRoleError::InvalidPromptFile {
role: role.to_owned(),
path: relative_path.to_owned(),
reason: error.to_string(),
})?;
let canonical_file =
fs::canonicalize(&full_path).map_err(|error| SubagentRoleError::InvalidPromptFile {
role: role.to_owned(),
path: relative_path.to_owned(),
reason: error.to_string(),
})?;
if !canonical_file.starts_with(&canonical_root) {
return Err(SubagentRoleError::InvalidPromptFile {
role: role.to_owned(),
path: relative_path.to_owned(),
reason: "prompt_file symlink escapes workspace root".to_owned(),
});
}
let bytes =
fs::read(&canonical_file).map_err(|error| SubagentRoleError::InvalidPromptFile {
role: role.to_owned(),
path: relative_path.to_owned(),
reason: error.to_string(),
})?;
if bytes.len() > MAX_ROLE_PROMPT_FILE_BYTES {
return Err(SubagentRoleError::InvalidPromptFile {
role: role.to_owned(),
path: relative_path.to_owned(),
reason: format!("exceeds {MAX_ROLE_PROMPT_FILE_BYTES} bytes"),
});
}
let text = String::from_utf8(bytes).map_err(|error| SubagentRoleError::InvalidPromptFile {
role: role.to_owned(),
path: relative_path.to_owned(),
reason: error.to_string(),
})?;
if text.trim().is_empty() {
return Err(SubagentRoleError::InvalidPromptFile {
role: role.to_owned(),
path: relative_path.to_owned(),
reason: "prompt_file must not be blank".to_owned(),
});
}
Ok(text)
}
fn validate_role_name(name: &str) -> Result<(), SubagentRoleError> {
if name.is_empty()
|| name.len() > 64
|| !name
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
{
return Err(SubagentRoleError::InvalidRoleName {
role: name.to_owned(),
});
}
Ok(())
}
fn tool_names(names: &[&str]) -> Vec<ToolName> {
names
.iter()
.map(|name| ToolName::new(name).expect("static built-in tool name is valid"))
.collect()
}
#[cfg(test)]
mod tests;