feat(#513): add ToolGroup enum, derive allowed_mcp_tools from groups + HIVE_TOOL_GROUPS env

This commit is contained in:
damocles 2026-06-01 11:29:26 +02:00 committed by mara
commit 98d9204ebf
4 changed files with 173 additions and 58 deletions

View file

@ -468,10 +468,9 @@ impl AgentServer {
}
// IMPORTANT: when adding a new `#[tool]` fn to this impl, also add
// its name to `allowed_mcp_tools(Flavor::Agent)` below. Claude
// Code's permission gate refuses uninlisted MCP tools in
// non-interactive `--print` mode with "permissions not granted yet"
// — keep the two lists in lockstep.
// its name to the matching `ToolGroup::tools()` slice in hive-sh4re.
// Claude Code's permission gate refuses uninlisted MCP tools in
// non-interactive `--print` mode with "permissions not granted yet".
#[tool_router]
impl AgentServer {
#[tool(
@ -1062,10 +1061,9 @@ impl ManagerServer {
}
// IMPORTANT: when adding a new `#[tool]` fn to this impl, also add
// its name to `allowed_mcp_tools(Flavor::Manager)` below. Claude
// Code's permission gate refuses uninlisted MCP tools in
// non-interactive `--print` mode with "permissions not granted yet"
// — keep the two lists in lockstep.
// its name to the matching `ToolGroup::tools()` slice in hive-sh4re.
// Claude Code's permission gate refuses uninlisted MCP tools in
// non-interactive `--print` mode with "permissions not granted yet".
#[tool_router]
impl ManagerServer {
#[tool(
@ -1731,56 +1729,70 @@ pub enum Flavor {
Manager,
}
/// MCP tools claude is allowed to call without prompting. Mirrors the
/// hyperhive surface so a new tool added in the corresponding `#[tool_router]`
/// impl needs to be listed here too.
#[must_use]
pub fn allowed_mcp_tools(flavor: Flavor) -> Vec<String> {
let names: &[&str] = match flavor {
Flavor::Agent => &[
"send",
"recv",
"ask",
"answer",
"remind",
"get_loose_ends",
"set_status",
"get_agent_meta",
"cancel_loose_end",
],
Flavor::Manager => &[
"send",
"recv",
"request_init_config",
"kill",
"start",
"restart",
"update",
"request_apply_commit",
// The remaining manager tools below were added incrementally
// and have historically been missed in the allow-list when
// their `#[tool]` impls landed. Claude Code's permission gate
// refuses uninlisted tools in non-interactive `--print` mode
// with a "permissions not granted yet" error — keep this
// block in lockstep with the `#[tool]` fns in `ManagerServer`.
"request_update_meta_inputs",
"request_schedule_prompt",
"fire_schedule_now",
"cancel_schedule",
"edit_schedule",
"list_schedules",
"ask",
"answer",
"get_logs",
"get_loose_ends",
"remind",
"set_status",
"get_agent_meta",
"cancel_loose_end",
],
/// Env var written by the meta renderer with a comma-separated list of
/// `hive_sh4re::ToolGroup` snake_case names (e.g. `"messaging,inbox,meta"`).
/// When present, the harness expands the groups into per-tool allow entries
/// instead of using the hardcoded flavor default. See `docs/conventions.md::Tool groups`.
const TOOL_GROUPS_ENV: &str = "HIVE_TOOL_GROUPS";
/// Resolve the active tool groups for a harness session.
///
/// Reads `HIVE_TOOL_GROUPS` from the environment first. Each comma-separated
/// token is matched (case-insensitive) against the `ToolGroup` serde names
/// (`messaging`, `meta`, `inbox`, `lifecycle`, `approvals`, `scheduling`,
/// `diagnostics`). Unrecognised tokens are logged and skipped. Falls back to
/// the flavor default when the env var is absent or empty.
fn effective_tool_groups(flavor: Flavor) -> Vec<hive_sh4re::ToolGroup> {
let raw = match std::env::var(TOOL_GROUPS_ENV) {
Ok(v) if !v.trim().is_empty() => v,
_ => {
// No env var — use the flavor default unchanged.
let defaults = match flavor {
Flavor::Agent => hive_sh4re::ToolGroup::AGENT_DEFAULT,
Flavor::Manager => hive_sh4re::ToolGroup::MANAGER_DEFAULT,
};
return defaults.to_vec();
}
};
let mut out: Vec<String> = names
let mut groups = Vec::new();
for token in raw.split(',') {
let t = token.trim().to_ascii_lowercase();
// Parse via serde_json (the canonical deserialization path).
match serde_json::from_value::<hive_sh4re::ToolGroup>(
serde_json::Value::String(t.clone()),
) {
Ok(g) => groups.push(g),
Err(_) => tracing::warn!(
token = %t,
"{TOOL_GROUPS_ENV}: unknown tool group, skipping"
),
}
}
if groups.is_empty() {
tracing::warn!(
"{TOOL_GROUPS_ENV} set but contained no recognised groups; \
falling back to flavor default"
);
return match flavor {
Flavor::Agent => hive_sh4re::ToolGroup::AGENT_DEFAULT.to_vec(),
Flavor::Manager => hive_sh4re::ToolGroup::MANAGER_DEFAULT.to_vec(),
};
}
groups
}
/// MCP tools claude is allowed to call without prompting, derived from
/// the supplied tool groups. Adding a new `#[tool]` fn to a server impl
/// requires updating the matching `ToolGroup::tools()` slice in hive-sh4re
/// (single source of truth). See `docs/conventions.md::Tool groups`.
#[must_use]
pub fn allowed_mcp_tools(groups: &[hive_sh4re::ToolGroup]) -> Vec<String> {
// Collect all tool names, deduplicating while preserving order.
let mut seen = std::collections::HashSet::new();
let mut out: Vec<String> = groups
.iter()
.flat_map(|g| g.tools())
.filter(|t| seen.insert(*t))
.map(|t| format!("mcp__{SERVER_NAME}__{t}"))
.collect();
// Extra MCP servers declared via `hyperhive.extraMcpServers` in
@ -1822,7 +1834,8 @@ pub fn allowed_tools_arg(flavor: Flavor) -> String {
}
})
.collect();
all.extend(allowed_mcp_tools(flavor));
let groups = effective_tool_groups(flavor);
all.extend(allowed_mcp_tools(&groups));
all.join(",")
}