334 lines
12 KiB
Rust
334 lines
12 KiB
Rust
//! The model list, the swarm's cap and what a run is started with.
|
|
|
|
use super::*;
|
|
|
|
// One test, not two: `subagent_otel_attrs` reads the real process-wide
|
|
// `OTEL_RESOURCE_ATTRIBUTES` env var, and cargo runs tests in parallel
|
|
// threads by default — two separate tests each mutating that global
|
|
// raced each other (and, in this container, lost to the agent's own
|
|
// real ambient value). Sequencing both assertions in one test removes
|
|
// the race instead of papering over it with a mutex.
|
|
#[test]
|
|
fn otel_attrs_append_ambient_value_or_stand_alone() {
|
|
// SAFETY: test-only env mutation; sequenced within this one test so
|
|
// no other test's concurrent read/write of the same var can race it.
|
|
unsafe {
|
|
std::env::remove_var("OTEL_RESOURCE_ATTRIBUTES");
|
|
}
|
|
assert_eq!(subagent_otel_attrs("batch-1"), "subagent=batch-1");
|
|
|
|
unsafe {
|
|
std::env::set_var("OTEL_RESOURCE_ATTRIBUTES", "agent=damocles");
|
|
}
|
|
assert_eq!(
|
|
subagent_otel_attrs("batch-1"),
|
|
"agent=damocles,subagent=batch-1"
|
|
);
|
|
unsafe {
|
|
std::env::remove_var("OTEL_RESOURCE_ATTRIBUTES");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn build_config_only_appends_system_prompt_when_given() {
|
|
let with = build_config("n", None, None, Some("/tmp/p.md"), None, None);
|
|
assert!(
|
|
with.extra_args
|
|
.contains(&"--append-system-prompt-file".to_owned())
|
|
);
|
|
assert!(with.extra_args.contains(&"/tmp/p.md".to_owned()));
|
|
|
|
let without = build_config("n", None, None, None, None, None);
|
|
assert!(
|
|
!without
|
|
.extra_args
|
|
.contains(&"--append-system-prompt-file".to_owned())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_role_with_an_unreadable_task_file_refuses_rather_than_dropping_either() {
|
|
let dir = std::env::temp_dir().join("hive-subagent-session-test-unreadable");
|
|
let err = compose_prompt(
|
|
&dir,
|
|
"n",
|
|
Some("a role"),
|
|
"/tmp/no-such-task-file-here.md",
|
|
"go",
|
|
)
|
|
.expect_err("an unreadable task file cannot silently become a role-only prompt");
|
|
assert!(err.to_string().contains("task instructions"), "{err}");
|
|
}
|
|
|
|
/// `--tools` reaches the argv on every caller shape, carrying a non-empty
|
|
/// value. Non-empty matters separately from "the flag is there": what an
|
|
/// empty value grants is disputed and release-dependent, so we never ship
|
|
/// one and never depend on the answer. Checked for each shape because a
|
|
/// flag that appears on only some spawn paths is no restriction at all.
|
|
/// `signal_url` stays `None` throughout — it only steers `mcp_config`,
|
|
/// which `--tools` does not govern, and passing it would make this test
|
|
/// need the container's injected `HYPERHIVE_HARNESS_DIR`.
|
|
#[test]
|
|
fn build_config_always_passes_a_non_empty_tools_list() {
|
|
for config in [
|
|
build_config("n", None, None, None, None, None),
|
|
build_config("n", None, None, Some("/tmp/p.md"), None, None),
|
|
build_config("n", None, None, None, Some("/tmp/wt"), None),
|
|
] {
|
|
let tools = spawned_tools(&config);
|
|
assert!(!tools.is_empty(), "--tools must never be empty");
|
|
assert!(tools.split(',').all(|t| !t.trim().is_empty()));
|
|
}
|
|
}
|
|
|
|
/// A subagent's built-ins are a subset of `subagent_builtin_tools_for` —
|
|
/// the parent's own resolved set, plus `Bash` iff the parent holds
|
|
/// `Execution` (see that function's doc comment). This is the property
|
|
/// that makes shipping the daemon safe: a subagent reaching a tool
|
|
/// outside that resolution is a privilege escalation dressed as a
|
|
/// convenience. Read from `hive_sh4re::permissions`, deliberately: it is
|
|
/// the parent harness's own resolver, so this compares against what the
|
|
/// parent actually licenses rather than against a restatement of it that
|
|
/// could drift.
|
|
///
|
|
/// ⚠️ **Scope: this catches a code divergence, not a read one.** Both
|
|
/// sides resolve off one permission set here; that the parent and this
|
|
/// daemon read the same key is a deployment property.
|
|
#[test]
|
|
fn a_subagent_gets_no_builtin_its_parent_lacks() {
|
|
let allowed = hive_sh4re::permissions::subagent_builtin_tools_for(
|
|
&crate::swarm_models::permissions_now(),
|
|
);
|
|
let config = build_config("n", None, None, None, None, None);
|
|
for tool in spawned_tools(&config).split(',') {
|
|
assert!(
|
|
allowed.contains(&tool),
|
|
"subagent got {tool}, which the parent's resolved set does not license"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Named individually rather than inferred from "the list is short": the
|
|
/// tools this daemon used to hand a subagent that reach outside the run
|
|
/// it was started for. They are absent because the parent has never had
|
|
/// them — this pins the consequence, not a second allow-list.
|
|
///
|
|
/// ⚠️ **A canary, not a guarantee.** The names are hardcoded, so this
|
|
/// only fails for a tool someone thought to list here; the *guarantee*
|
|
/// is the subset property above, which holds for tools nobody has
|
|
/// imagined yet. Do not read a pass as "no escaping tool is reachable" —
|
|
/// read it as "none of these ten came back".
|
|
#[test]
|
|
fn no_spawned_tool_escapes_the_session() {
|
|
let config = build_config("n", None, None, None, None, None);
|
|
let tools: Vec<&str> = spawned_tools(&config).split(',').collect();
|
|
for escaping in [
|
|
"SendMessage",
|
|
"ListAgents",
|
|
"Task",
|
|
"TaskStop",
|
|
"TaskOutput",
|
|
"CronCreate",
|
|
"CronDelete",
|
|
"RemoteTrigger",
|
|
"EnterWorktree",
|
|
"ExitWorktree",
|
|
] {
|
|
assert!(
|
|
!tools.contains(&escaping),
|
|
"{escaping} must not be in a subagent's --tools"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn build_config_defaults_cwd_to_the_work_dir_when_no_dir_is_given() {
|
|
let with = build_config("n", None, None, None, Some("/tmp/some-worktree"), None);
|
|
assert_eq!(with.cwd, Some(PathBuf::from("/tmp/some-worktree")));
|
|
|
|
let root = std::env::temp_dir().join(format!(
|
|
"hive-subagent-mcp-test-work-dir-{}",
|
|
std::process::id()
|
|
));
|
|
let state = root.join("state");
|
|
let work = root.join("work");
|
|
std::fs::create_dir_all(&state).unwrap();
|
|
// SAFETY: test-only env mutation; the other `build_config` tests pass
|
|
// an explicit `dir` or read nothing derived from the state dir.
|
|
unsafe {
|
|
std::env::set_var("HYPERHIVE_STATE_DIR", &state);
|
|
}
|
|
|
|
let absent = build_config("n", None, None, None, None, None);
|
|
std::fs::create_dir_all(&work).unwrap();
|
|
let present = build_config("n", None, None, None, None, None);
|
|
|
|
// SAFETY: see above.
|
|
unsafe {
|
|
std::env::remove_var("HYPERHIVE_STATE_DIR");
|
|
}
|
|
let _ = std::fs::remove_dir_all(&root);
|
|
|
|
assert_eq!(
|
|
absent.cwd, None,
|
|
"a work dir that isn't there yet must not become a spawn cwd"
|
|
);
|
|
assert_eq!(present.cwd, Some(work));
|
|
}
|
|
|
|
#[test]
|
|
fn build_config_sets_effort_alongside_model() {
|
|
let config = build_config(
|
|
"n",
|
|
Some("opus".to_owned()),
|
|
Some("high".to_owned()),
|
|
None,
|
|
None,
|
|
None,
|
|
);
|
|
assert_eq!(config.model, Some("opus".to_owned()));
|
|
assert_eq!(config.effort, Some("high".to_owned()));
|
|
}
|
|
|
|
#[test]
|
|
fn build_config_defaults_effort_to_medium_when_omitted() {
|
|
// Unlike `model`, an omitted `effort` does not fall through to
|
|
// claude's own default (`high` on most models) — this daemon picks
|
|
// `medium` itself, a deliberate cost-conscious choice for subagent
|
|
// work.
|
|
let config = build_config("n", None, None, None, None, None);
|
|
assert_eq!(config.effort, Some("medium".to_owned()));
|
|
}
|
|
|
|
// ---- turn continuation -------------------------------------------------
|
|
|
|
#[test]
|
|
fn an_unset_model_whitelist_allows_any_model() {
|
|
assert!(check_model(Some("opus"), &local(None)).is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn an_unset_model_whitelist_allows_an_omitted_model() {
|
|
assert!(check_model(None, &local(None)).is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn a_listed_model_is_allowed() {
|
|
assert!(check_model(Some("sonnet"), &local(Some("haiku,sonnet,opus"))).is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn an_unlisted_model_is_rejected_naming_what_is_permitted() {
|
|
let err = check_model(Some("opus"), &local(Some("haiku,sonnet")))
|
|
.expect_err("a model outside the list must be refused")
|
|
.to_string();
|
|
assert!(
|
|
err.contains("opus") && err.contains("haiku, sonnet"),
|
|
"the rejection is all the caller sees, so it must name the model \
|
|
and the permitted ones: {err}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_set_whitelist_still_allows_an_omitted_model() {
|
|
assert!(
|
|
check_model(None, &local(Some("haiku"))).is_ok(),
|
|
"omitting `model` lets claude pick its own default — it is not a \
|
|
choice of model, so there is nothing to gate"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn whitespace_and_empty_entries_are_ignored() {
|
|
assert!(check_model(Some("sonnet"), &local(Some(" haiku , sonnet ,, "))).is_ok());
|
|
assert!(check_model(Some("opus"), &local(Some(" haiku , sonnet ,, "))).is_err());
|
|
assert!(
|
|
check_model(Some("opus"), &local(Some(" ,, "))).is_ok(),
|
|
"a var naming nothing restricts nothing — default open"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn the_cap_is_the_swarm_value_and_none_otherwise() {
|
|
assert_eq!(subagent_cap(Some(&capped(3)), "a"), Some(3));
|
|
assert_eq!(subagent_cap(Some(&unset()), "a"), None);
|
|
assert_eq!(subagent_cap(None, "a"), None);
|
|
let failed = crate::swarm_models::Read::Failed("no answer".to_owned());
|
|
assert_eq!(subagent_cap(Some(&failed), "a"), None);
|
|
}
|
|
|
|
#[test]
|
|
fn a_failed_cap_read_warns_naming_the_agent_and_the_error() {
|
|
let logs = CapturedLogs::default();
|
|
let subscriber = tracing_subscriber::fmt()
|
|
.with_writer(logs.clone())
|
|
.with_ansi(false)
|
|
.finish();
|
|
let failed = crate::swarm_models::Read::Failed("no answer".to_owned());
|
|
let cap = tracing::subscriber::with_default(subscriber, || subagent_cap(Some(&failed), "iris"));
|
|
assert_eq!(cap, None);
|
|
let logged = logs.text();
|
|
assert!(
|
|
logged.contains("WARN") && logged.contains("iris") && logged.contains("no answer"),
|
|
"{logged}"
|
|
);
|
|
}
|
|
|
|
/// The swarm's list replaces the local one, so it can widen it as well
|
|
/// as narrow it.
|
|
#[test]
|
|
fn a_swarm_list_replaces_the_local_one() {
|
|
let env = || Some("haiku,sonnet".to_owned());
|
|
let list = model_list(Some(swarm(&["opus"])), env());
|
|
assert_eq!(list, ModelList::Swarm(vec!["opus".to_owned()]));
|
|
assert!(check_model(Some("opus"), &list).is_ok());
|
|
assert!(check_model(Some("sonnet"), &list).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn without_a_swarm_list_the_local_one_applies() {
|
|
let env = || Some("haiku,sonnet".to_owned());
|
|
for read in [None, Some(unset())] {
|
|
assert_eq!(model_list(read, env()), local(Some("haiku,sonnet")));
|
|
}
|
|
assert_eq!(model_list(Some(unset()), None), local(None));
|
|
}
|
|
|
|
#[test]
|
|
fn a_failed_read_falls_back_to_the_local_list_and_warns() {
|
|
let logs = CapturedLogs::default();
|
|
let subscriber = tracing_subscriber::fmt()
|
|
.with_writer(logs.clone())
|
|
.with_ansi(false)
|
|
.finish();
|
|
let list = tracing::subscriber::with_default(subscriber, || {
|
|
model_list(
|
|
Some(crate::swarm_models::Read::Failed("no answer".to_owned())),
|
|
Some("haiku".to_owned()),
|
|
)
|
|
});
|
|
assert_eq!(list, local(Some("haiku")));
|
|
let logged = logs.text();
|
|
assert!(
|
|
logged.contains("WARN") && logged.contains("no answer"),
|
|
"the fallback is logged as a warning with the cause: {logged}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_model_outside_the_swarm_list_is_refused_naming_the_list_and_its_source() {
|
|
let list = model_list(Some(swarm(&["haiku", "sonnet"])), Some("opus".to_owned()));
|
|
let err = check_model(Some("opus"), &list)
|
|
.expect_err("a model outside the swarm's list must be refused")
|
|
.to_string();
|
|
assert!(
|
|
err.contains("opus")
|
|
&& err.contains("haiku, sonnet")
|
|
&& err.contains("swarm's subagent_models"),
|
|
"{err}"
|
|
);
|
|
assert!(
|
|
check_model(None, &list).is_ok(),
|
|
"an omitted model is not checked against the swarm's list either"
|
|
);
|
|
}
|