use super::*;
use crate::agent::{
AgentRuntimeSpec, ModelPolicy, ModelSelection, PromptPolicy, ToolCapability, ToolPolicy,
};
use rho_providers::credentials::{
save_codex_tokens, save_provider_api_key, save_xai_tokens, CodexTokens, MemoryCredentialStore,
XaiTokens,
};
fn capability_set(names: &[&str]) -> AgentCapabilities {
AgentCapabilities::new(
names
.iter()
.map(|name| ToolCapability::parse((*name).to_string()))
.collect(),
)
}
fn definition(tools: ToolPolicy) -> Arc<AgentDefinition> {
Arc::new(AgentDefinition {
id: AgentId::new("test").unwrap(),
description: "test".into(),
prompt: PromptPolicy::Extend("instructions".into()),
runtime: AgentRuntimeSpec::Rho {
tools,
model: ModelPolicy::Inherit,
reasoning: None,
},
})
}
fn capabilities() -> AgentCapabilities {
capability_set(&[
"read_file",
"write",
"agent",
"agents",
"questionnaire",
"todo",
])
}
fn credentials(available: &[&str]) -> MemoryCredentialStore {
let store = MemoryCredentialStore::default();
for auth in available {
match *auth {
"xai-oauth" => save_xai_tokens(
&store,
&XaiTokens {
access_token: "test-access-token".into(),
refresh_token: None,
expires_at_unix: None,
id_token: None,
},
),
"codex" => save_codex_tokens(
&store,
&CodexTokens {
access_token: "test-access-token".into(),
refresh_token: None,
id_token: None,
account_id: None,
},
),
auth => save_provider_api_key(&store, auth, "test-api-key"),
}
.unwrap();
}
store
}
#[test]
fn bound_capabilities_narrow_by_role_and_what_the_host_offers() {
let allow = |names: &[&str]| {
ToolPolicy::Allow(
names
.iter()
.map(|name| ToolCapability::parse((*name).to_string()))
.collect(),
)
};
let cases = [
(
"delegated all keeps an offered questionnaire",
AgentRole::Delegated,
ToolPolicy::All,
capabilities(),
&["read_file", "write", "questionnaire", "todo"][..],
),
(
"delegated allowlist keeps an offered questionnaire",
AgentRole::Delegated,
allow(&["read_file", "questionnaire"]),
capabilities(),
&["read_file", "questionnaire"][..],
),
(
"delegated allowlist omits an unoffered questionnaire",
AgentRole::Delegated,
allow(&["read_file", "questionnaire"]),
capability_set(&["read_file", "write"]),
&["read_file"][..],
),
(
"delegated allowlist omits an unoffered browser",
AgentRole::Delegated,
allow(&["read_file", "browser"]),
capability_set(&["read_file", "write"]),
&["read_file"][..],
),
(
"delegated allowlist omits recursive tools",
AgentRole::Delegated,
allow(&["read_file", "agent", "agents", "advisor", "workflow"]),
capability_set(&["read_file", "agent", "agents", "advisor", "workflow"]),
&["read_file"][..],
),
(
"workflow drops orchestration and questionnaire",
AgentRole::Workflow,
ToolPolicy::All,
capability_set(&[
"read_file",
"write",
"agent",
"agents",
"questionnaire",
"todo",
"rho",
"workflow",
]),
&["read_file", "write", "todo"][..],
),
];
for (label, role, tools, available_tools, expected) in cases {
let bound = AgentBinder::bind(
definition(tools),
AgentInvocation {
role,
available_tools,
},
&Config::with_test_model(),
)
.unwrap();
assert_eq!(
bound.rho_capabilities(),
Some(&capability_set(expected)),
"{label}"
);
}
}
#[test]
fn frozen_binding_does_not_rebind_and_narrows_current_policy() {
let source = definition(ToolPolicy::All);
let frozen = crate::workflow::ResolvedAgent {
agent_id: source.id.to_string(),
fingerprint: source.fingerprint().to_string(),
runtime: crate::workflow::AgentRuntime::Rho,
source_origin: "project".into(),
trust_required: true,
plugin_trust_required: false,
prompt_policy: "replace:frozen".into(),
provider: Some("anthropic".into()),
model: Some("claude-sonnet-4-6".into()),
reasoning: None,
step_limit: 17,
capabilities: ["read_file", "write", "workflow"]
.into_iter()
.map(str::to_owned)
.collect(),
permission_ceiling: "auto".into(),
auth_profile: Some("anthropic".into()),
fast: false,
executable: None,
executable_identity: None,
arguments: Vec::new(),
};
let current = Config {
permission_mode: crate::permission::PermissionMode::Plan,
..Config::with_model("openai", "changed", "api-key")
};
let bound = AgentBinder::bind_frozen(&frozen, ¤t, &capabilities()).unwrap();
assert_eq!(bound.prompt(), &PromptPolicy::Replace("frozen".into()));
assert_eq!(bound.step_limit(), 17);
assert_eq!(
bound
.rho_config()
.unwrap()
.expect_conversation_model()
.provider,
"anthropic"
);
assert_eq!(
bound
.rho_config()
.unwrap()
.expect_conversation_model()
.model,
"claude-sonnet-4-6"
);
assert_eq!(
bound.rho_config().unwrap().permission_mode,
crate::permission::PermissionMode::Plan
);
assert_eq!(
bound.rho_capabilities(),
Some(&capability_set(&["read_file", "write"]))
);
}
#[test]
fn frozen_claude_cli_bypass_ceiling_narrows_to_current_auto() {
let source = claude_definition(ModelPolicy::Inherit);
let frozen = crate::workflow::ResolvedAgent {
agent_id: source.id.to_string(),
fingerprint: source.fingerprint().to_string(),
runtime: crate::workflow::AgentRuntime::ClaudeCli,
source_origin: "project".into(),
trust_required: true,
plugin_trust_required: false,
prompt_policy: "replace:frozen".into(),
provider: None,
model: Some("opus".into()),
reasoning: None,
step_limit: 9,
capabilities: ["Read", "Bash(git status:*)"]
.into_iter()
.map(str::to_owned)
.collect(),
permission_ceiling: "bypass".into(),
auth_profile: None,
fast: false,
executable: None,
executable_identity: None,
arguments: Vec::new(),
};
let current = Config {
permission_mode: crate::permission::PermissionMode::Auto,
..Config::default()
};
let bound = AgentBinder::bind_frozen(&frozen, ¤t, &capabilities()).unwrap();
match bound.runtime() {
BoundRuntime::ClaudeCli {
model,
tools,
permission_mode,
max_turns,
..
} => {
assert_eq!(model.as_deref(), Some("opus"));
assert_eq!(
tools.as_slice(),
["Bash(git status:*)".to_string(), "Read".to_string()].as_slice()
);
assert_eq!(*permission_mode, crate::permission::PermissionMode::Auto);
assert_eq!(*max_turns, 9);
}
BoundRuntime::Rho { .. } | BoundRuntime::Antigravity { .. } => {
panic!("expected Claude bound runtime")
}
}
}
#[test]
fn frozen_antigravity_requires_an_agy_acp_plan() {
let frozen = |arguments: &[&str]| crate::workflow::ResolvedAgent {
agent_id: "agy".into(),
fingerprint: "0".repeat(64),
runtime: crate::workflow::AgentRuntime::Antigravity,
source_origin: "project".into(),
trust_required: false,
plugin_trust_required: false,
prompt_policy: "extend:frozen".into(),
provider: None,
model: Some("gemini-3.8-flash".into()),
reasoning: None,
step_limit: 9,
capabilities: ["run_command".to_owned()].into_iter().collect(),
permission_ceiling: "bypass".into(),
auth_profile: None,
fast: false,
executable: None,
executable_identity: None,
arguments: arguments.iter().map(|arg| (*arg).to_owned()).collect(),
};
let current = Config {
permission_mode: crate::permission::PermissionMode::Bypass,
..Config::default()
};
assert!(AgentBinder::bind_frozen(&frozen(&["--uid="]), ¤t, &capabilities()).is_err());
let bound = AgentBinder::bind_frozen(
&frozen(crate::antigravity_runtime::executable::SERVER_ARGS),
¤t,
&capabilities(),
)
.unwrap();
assert!(matches!(
bound.runtime(),
BoundRuntime::Antigravity { tools, .. }
if tools == &[crate::agent::AntigravityTool::RunCommand]
));
}
#[test]
fn frozen_cursor_plan_loads_and_asks_to_plan_again() {
let saved = serde_json::json!({
"agent_id": "cursor-worker",
"fingerprint": "0".repeat(64),
"runtime": "cursor",
"source_origin": "project",
"trust_required": true,
"prompt_policy": "extend:frozen",
"provider": null,
"model": "composer-2.5",
"reasoning": null,
"step_limit": 9,
"capabilities": ["read_tool_call"],
"permission_ceiling": "bypass",
"auth_profile": null,
"executable": null,
"executable_identity": null,
"arguments": [],
});
let frozen: crate::workflow::ResolvedAgent = serde_json::from_value(saved.clone()).unwrap();
assert_eq!(frozen.runtime, crate::workflow::AgentRuntime::RetiredCursor);
assert_eq!(serde_json::to_value(&frozen).unwrap(), saved);
let error = AgentBinder::bind_frozen(&frozen, &Config::default(), &capabilities()).unwrap_err();
assert_eq!(
error.to_string(),
"frozen agent 'cursor-worker' was planned for the retired cursor runtime; plan the workflow again"
);
}
#[test]
fn bind_keeps_web_search_ceiling_when_search_is_currently_off() {
let mut host = Config::with_model("openai", "gpt-5.5", "api-key");
host.web_search.mode = crate::config::WebSearchMode::Off;
let available = capability_set(&["read_file", "web_search"]);
let bound = AgentBinder::bind(
definition(ToolPolicy::All),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: available,
},
&host,
)
.unwrap();
assert!(bound
.rho_capabilities()
.unwrap()
.contains(&ToolCapability::WebSearch));
}
#[test]
fn unavailable_explicit_tool_fails_before_execution() {
let error = AgentBinder::bind(
definition(ToolPolicy::Allow(
["write".to_string()]
.into_iter()
.map(crate::agent::ToolCapability::parse)
.collect(),
)),
AgentInvocation {
role: AgentRole::AutomationRoot,
available_tools: capability_set(&["read_file"]),
},
&Config::with_test_model(),
)
.unwrap_err();
assert!(error.to_string().contains("write"));
}
#[test]
fn agent_model_alias_conflicting_with_pinned_provider_errors() {
let config = Config {
model_aliases: aliases(&[("deep", "anthropic/claude-opus-4-8")]),
..Config::default()
};
let error = AgentBinder::bind(
definition_with_model(ModelPolicy::Select(crate::agent::ModelSelection {
provider: Some("openai".into()),
model: "@deep".into(),
auth: None,
fast: false,
})),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&config,
)
.unwrap_err();
assert!(
error.to_string().contains(
"model alias '@deep' resolves to provider 'anthropic', which conflicts with the agent's provider 'openai'"
),
"{error:#}"
);
}
fn definition_with_model(model: ModelPolicy) -> Arc<AgentDefinition> {
Arc::new(AgentDefinition {
runtime: AgentRuntimeSpec::Rho {
tools: ToolPolicy::All,
model,
reasoning: None,
},
..definition(ToolPolicy::All).as_ref().clone()
})
}
fn aliases(pairs: &[(&str, &str)]) -> crate::model_aliases::ModelAliases {
crate::model_aliases::ModelAliases::from_entries(
pairs
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect(),
)
.unwrap()
}
#[test]
fn agent_pin_to_a_decision_model_host_is_refused() {
let pins = [
("provider", Some("typesafe"), None),
("auth", None, Some("typesafe-api-key")),
];
for (name, provider, auth) in pins {
let error = AgentBinder::bind(
definition_with_model(ModelPolicy::Select(crate::agent::ModelSelection {
provider: provider.map(str::to_owned),
model: "jev-latest".into(),
auth: auth.map(str::to_owned),
fast: false,
})),
AgentInvocation {
role: AgentRole::InteractiveRoot,
available_tools: capabilities(),
},
&Config::default(),
)
.unwrap_err();
assert_eq!(
format!("{error:#}"),
"agent 'test': provider 'typesafe' serves decision models, not chat models",
"{name}"
);
}
}
#[test]
fn only_an_agent_that_names_its_whole_model_binds_while_none_is_set_up() {
let bind = |model: ModelPolicy| {
AgentBinder::bind_with_credentials(
definition_with_model(model),
AgentInvocation {
role: AgentRole::DaemonRoot,
available_tools: capabilities(),
},
&Config::default(),
None,
&credentials(&["xai-oauth"]),
)
.map(|bound| {
let config = bound.rho_config().unwrap();
format!(
"{}/{}",
config.expect_conversation_model().provider,
config.expect_conversation_model().model
)
})
.map_err(|error| error.downcast::<crate::config::ModelRequired>().unwrap())
};
let pin = |provider: Option<&str>| {
ModelPolicy::Select(ModelSelection {
provider: provider.map(str::to_owned),
model: "grok-4.5".into(),
auth: None,
fast: false,
})
};
assert_eq!(
[
bind(ModelPolicy::Inherit),
bind(pin(None)),
bind(pin(Some("xai"))),
],
[
Err(crate::config::ModelRequired),
Err(crate::config::ModelRequired),
Ok("xai/grok-4.5".to_owned()),
]
);
}
fn claude_definition(model: ModelPolicy) -> Arc<AgentDefinition> {
let claude_model = match &model {
ModelPolicy::Inherit => None,
ModelPolicy::Select(selection)
| ModelPolicy::Prefer(selection)
| ModelPolicy::Require(selection) => Some(selection.model.clone()),
};
Arc::new(AgentDefinition {
id: AgentId::new("claude-test").unwrap(),
description: "claude".into(),
prompt: PromptPolicy::Replace("plan".into()),
runtime: AgentRuntimeSpec::ClaudeCli(crate::agent::ClaudeAgentConfig {
tools: crate::agent::ClaudeToolPolicy::Allow(vec!["Read".into(), "Bash(git *)".into()]),
inherit_claude_config: true,
model: claude_model,
reasoning: None,
}),
})
}
#[test]
fn claude_binding_is_typed_and_does_not_resolve_aliases_or_mutate_host_config() {
let host = Config {
model_aliases: aliases(&[("deep", "anthropic/claude-opus-4-8")]),
permission_mode: crate::permission::PermissionMode::Plan,
..Config::with_model("openai", "gpt-5.5", "api-key")
};
let bound = AgentBinder::bind(
claude_definition(ModelPolicy::Select(ModelSelection {
provider: None,
model: "opus".into(),
auth: None,
fast: false,
})),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&host,
)
.unwrap();
match bound.runtime() {
BoundRuntime::ClaudeCli {
model,
tools,
inherit_claude_config,
permission_mode,
max_turns,
reasoning,
} => {
assert_eq!(model.as_deref(), Some("opus"));
assert_eq!(
tools.as_slice(),
["Read".to_string(), "Bash(git *)".to_string()].as_slice()
);
assert!(*inherit_claude_config);
assert_eq!(*permission_mode, crate::permission::PermissionMode::Plan);
assert_eq!(
*max_turns,
crate::app::sdk_config::run_step_limit().get() as u64
);
assert!(reasoning.is_none());
}
BoundRuntime::Rho { .. } | BoundRuntime::Antigravity { .. } => {
panic!("expected Claude bound runtime")
}
}
assert!(bound.rho_config().is_none());
assert!(bound.rho_capabilities().is_none());
assert_eq!(host.expect_conversation_model().provider, "openai");
assert_eq!(host.expect_conversation_model().model, "gpt-5.5");
}
#[test]
fn claude_runtime_binds_only_where_rho_runs_it_over_acp() {
let binds = |role| {
AgentBinder::bind(
claude_definition(ModelPolicy::Inherit),
AgentInvocation {
role,
available_tools: capabilities(),
},
&Config::default(),
)
.is_ok()
};
pretty_assertions::assert_eq!(
[
AgentRole::InteractiveRoot,
AgentRole::AutomationRoot,
AgentRole::DaemonRoot,
AgentRole::Delegated,
]
.map(binds),
[false, false, true, true]
);
}
#[test]
fn claude_runtime_rejects_alias_models_at_bind() {
let error = AgentBinder::bind(
claude_definition(ModelPolicy::Select(ModelSelection {
provider: None,
model: "@deep".into(),
auth: None,
fast: false,
})),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&Config::default(),
)
.unwrap_err();
assert!(
error
.to_string()
.contains("does not resolve Rho model aliases"),
"{error:#}"
);
assert!(error.to_string().contains("@deep"), "{error:#}");
}
#[test]
fn claude_runtime_maps_supported_reasoning_and_rejects_unmapped() {
let mut mapped = claude_definition(ModelPolicy::Inherit).as_ref().clone();
if let AgentRuntimeSpec::ClaudeCli(config) = &mut mapped.runtime {
config.reasoning = Some(rho_sdk::ReasoningLevel::High);
}
let bound = AgentBinder::bind(
Arc::new(mapped),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&Config::default(),
)
.unwrap();
match bound.runtime() {
BoundRuntime::ClaudeCli { reasoning, .. } => {
assert_eq!(*reasoning, Some(rho_sdk::ReasoningLevel::High));
}
BoundRuntime::Rho { .. } | BoundRuntime::Antigravity { .. } => {
panic!("expected Claude bound runtime")
}
}
let mut unmapped = claude_definition(ModelPolicy::Inherit).as_ref().clone();
if let AgentRuntimeSpec::ClaudeCli(config) = &mut unmapped.runtime {
config.reasoning = Some(rho_sdk::ReasoningLevel::Minimal);
}
let error = AgentBinder::bind(
Arc::new(unmapped),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&Config::default(),
)
.unwrap_err();
assert!(
error.to_string().contains("not a Claude Code effort level"),
"{error:#}"
);
}
#[test]
fn artifact_identity_stamps_bound_reasoning_onto_starting_status() {
let host = Config {
reasoning: rho_sdk::ReasoningLevel::Low,
..Config::with_test_model()
};
let rho = AgentBinder::bind(
definition(ToolPolicy::All),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&host,
)
.unwrap();
let rho_identity = rho.artifact_identity();
assert_eq!(rho_identity.reasoning, Some(rho_sdk::ReasoningLevel::Low));
assert_eq!(
rho_identity.starting_status().reasoning,
Some(rho_sdk::ReasoningLevel::Low)
);
let inherit = AgentBinder::bind(
claude_definition(ModelPolicy::Inherit),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&Config::default(),
)
.unwrap();
let inherit_identity = inherit.artifact_identity();
assert_eq!(inherit_identity.reasoning, None);
assert_eq!(inherit_identity.starting_status().reasoning, None);
let mut mapped = claude_definition(ModelPolicy::Inherit).as_ref().clone();
if let AgentRuntimeSpec::ClaudeCli(config) = &mut mapped.runtime {
config.reasoning = Some(rho_sdk::ReasoningLevel::High);
}
let high = AgentBinder::bind(
Arc::new(mapped),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&Config::default(),
)
.unwrap();
let high_identity = high.artifact_identity();
assert_eq!(high_identity.reasoning, Some(rho_sdk::ReasoningLevel::High));
assert_eq!(
high_identity.starting_status().reasoning,
Some(rho_sdk::ReasoningLevel::High)
);
}
#[test]
fn provider_without_auth_keeps_compatible_host_auth() {
let host = Config::with_model("xai", "grok-4.5", "xai-oauth");
let bound = AgentBinder::bind_with_credentials(
definition_with_model(ModelPolicy::Prefer(ModelSelection {
provider: Some("xai".into()),
model: "grok-4.5".into(),
auth: None,
fast: false,
})),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&host,
None,
&credentials(&["xai-api-key"]),
)
.unwrap();
let config = bound.rho_config().unwrap();
assert_eq!(config.expect_conversation_model().provider, "xai");
assert_eq!(config.expect_conversation_model().auth, "xai-oauth");
assert_eq!(config.expect_conversation_model().model, "grok-4.5");
}
#[test]
fn explicit_auth_pin_overrides_host_auth() {
let host = Config::with_model("xai", "grok-4.5", "xai-oauth");
let bound = AgentBinder::bind_with_credentials(
definition_with_model(ModelPolicy::Select(ModelSelection {
provider: Some("xai".into()),
model: "grok-4.5".into(),
auth: Some("xai-api-key".into()),
fast: false,
})),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&host,
None,
&credentials(&["xai-oauth"]),
)
.unwrap();
let config = bound.rho_config().unwrap();
assert_eq!(config.expect_conversation_model().provider, "xai");
assert_eq!(config.expect_conversation_model().auth, "xai-api-key");
}
#[test]
fn provider_switch_without_auth_uses_available_host_credentials() {
let thread = std::thread::current();
let test_name = thread.name().expect("named test thread");
const CHILD_TEST: &str = "RHO_AGENT_BINDING_CREDENTIAL_TEST";
if std::env::var(CHILD_TEST).as_deref() != Ok(test_name) {
let mut command = std::process::Command::new(std::env::current_exe().unwrap());
command.args(["--exact", test_name, "--nocapture"]);
for name in rho_providers::credential_env_vars() {
command.env_remove(name);
}
let output = command
.env(CHILD_TEST, test_name)
.output()
.expect("run isolated credential test");
assert!(
output.status.success(),
"{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
return;
}
let host = Config {
model_aliases: aliases(&[("grok", "xai/grok-4.5")]),
..Config::with_model("openai-codex", "gpt-5.5", "codex")
};
let cases: &[(&str, &[&str], &str)] = &[
(
"provider: xai\nmodel: grok-4.5",
&["xai-oauth"],
"xai-oauth",
),
(
"provider: xai\nmodel: grok-4.5",
&["xai-api-key"],
"xai-api-key",
),
(
"provider: xai\nmodel: grok-4.5",
&["xai-oauth", "xai-api-key"],
"xai-api-key",
),
("provider: xai\nmodel: grok-4.5", &[], "xai-api-key"),
("provider: xai\nmodel: grok-4.5", &["codex"], "xai-api-key"),
("model: '@grok'", &["xai-oauth"], "xai-oauth"),
(
"provider: xai-oauth\nmodel: grok-4.5",
&["xai-api-key"],
"xai-oauth",
),
];
for (selection, available, expected_auth) in cases {
let definition = crate::agent::parse_definition(
std::path::Path::new("grok.md"),
"grok",
&format!("---\ndescription: Code reviewer\n{selection}\n---\nReview the code."),
)
.unwrap();
let store = credentials(available);
let bound = AgentBinder::bind_with_credentials(
Arc::new(definition),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&host,
None,
&store,
)
.unwrap();
let config = bound.rho_config().unwrap();
pretty_assertions::assert_eq!(
(
config.expect_conversation_model().provider.as_str(),
config.expect_conversation_model().auth.as_str(),
config.expect_conversation_model().model.as_str()
),
("xai", *expected_auth, "grok-4.5"),
"selection={selection}, available={available:?}",
);
}
}
#[test]
fn bound_agent_prompt_model_reflects_model_policy() {
use crate::model_identity::PromptModel;
let host = Config {
model_aliases: aliases(&[("fast", "xai/grok-4.5"), ("bare", "gpt-5.6-sol")]),
..Config::with_model("openai", "gpt-5.5", "api-key")
};
let policies = [
(
"inherit",
ModelPolicy::Inherit,
PromptModel::Rho {
provider: "openai".into(),
model: "gpt-5.5".into(),
},
),
(
"model only",
ModelPolicy::Select(ModelSelection {
provider: None,
model: "gpt-5.6-sol".into(),
auth: None,
fast: false,
}),
PromptModel::Rho {
provider: "openai".into(),
model: "gpt-5.6-sol".into(),
},
),
(
"provider and model",
ModelPolicy::Require(ModelSelection {
provider: Some("xai".into()),
model: "grok-4.5".into(),
auth: None,
fast: false,
}),
PromptModel::Rho {
provider: "xai".into(),
model: "grok-4.5".into(),
},
),
(
"alias that carries a provider",
ModelPolicy::Prefer(ModelSelection {
provider: None,
model: "@fast".into(),
auth: None,
fast: false,
}),
PromptModel::Rho {
provider: "xai".into(),
model: "grok-4.5".into(),
},
),
(
"alias that keeps the host provider",
ModelPolicy::Select(ModelSelection {
provider: None,
model: "@bare".into(),
auth: None,
fast: false,
}),
PromptModel::Rho {
provider: "openai".into(),
model: "gpt-5.6-sol".into(),
},
),
(
"auth pin without provider",
ModelPolicy::Select(ModelSelection {
provider: None,
model: "claude-fable-5".into(),
auth: Some("anthropic-api-key".into()),
fast: false,
}),
PromptModel::Rho {
provider: "anthropic".into(),
model: "claude-fable-5".into(),
},
),
];
for (name, policy, expected) in policies {
let definition = definition_with_model(policy);
let store = credentials(&["xai-oauth"]);
let bound = AgentBinder::bind_with_credentials(
Arc::clone(&definition),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&host,
None,
&store,
)
.unwrap();
pretty_assertions::assert_eq!(bound.prompt_model().unwrap(), expected, "{name}");
}
}
#[test]
fn bound_claude_agent_prompt_model_is_the_pass_through_value() {
use crate::model_identity::PromptModel;
for model in [Some("opus".to_string()), None] {
let definition = Arc::new(AgentDefinition {
runtime: AgentRuntimeSpec::ClaudeCli(crate::agent::ClaudeAgentConfig {
tools: crate::agent::ClaudeToolPolicy::None,
inherit_claude_config: false,
model: model.clone(),
reasoning: None,
}),
..definition(ToolPolicy::All).as_ref().clone()
});
let bound = AgentBinder::bind(
Arc::clone(&definition),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&Config::default(),
)
.unwrap();
pretty_assertions::assert_eq!(
bound.prompt_model().unwrap(),
PromptModel::ExternalCli {
runtime: crate::agent::AgentRuntime::ClaudeCli,
requested: model,
resolved: None,
}
);
}
}
#[test]
fn delegated_fast_mode_follows_definition_not_parent() {
let pinned = |provider: &str, model: &str, auth: &str, fast: bool| {
Arc::new(AgentDefinition {
runtime: AgentRuntimeSpec::Rho {
tools: ToolPolicy::All,
model: ModelPolicy::Select(ModelSelection {
provider: Some(provider.into()),
model: model.into(),
auth: Some(auth.into()),
fast,
}),
reasoning: None,
},
..definition(ToolPolicy::All).as_ref().clone()
})
};
let inherit = definition(ToolPolicy::All);
let store = credentials(&["codex", "xai-oauth", "xai-api-key"]);
let cases: [(Arc<AgentDefinition>, bool, Result<bool, ()>); 6] = [
(
pinned("openai-codex", "gpt-5.6-sol", "codex", true),
false,
Ok(true),
),
(
pinned("openai-codex", "gpt-5.6-sol", "codex", false),
true,
Ok(false),
),
(inherit.clone(), true, Ok(false)),
(
pinned("xai", "grok-4.7", "xai-oauth", true),
false,
Ok(true),
),
(
pinned("xai", "grok-4.7", "xai-api-key", true),
false,
Err(()),
),
(
pinned("openai-codex", "gpt-5.3-codex", "codex", true),
true,
Err(()),
),
];
for (index, (definition, parent_fast, expected)) in cases.into_iter().enumerate() {
let host = Config {
fast_mode: parent_fast,
..Config::with_model("openai-codex", "gpt-5.6-sol", "codex")
};
let result = AgentBinder::bind_with_credentials(
definition,
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&host,
None,
&store,
);
let actual = result
.map(|bound| bound.rho_config().expect("rho config").fast_mode)
.map_err(|_| ());
assert_eq!(actual, expected, "case {index}");
}
}
#[test]
fn configured_model_override_replaces_the_definition_model() {
let pinned = Arc::new(AgentDefinition {
runtime: AgentRuntimeSpec::Rho {
tools: ToolPolicy::All,
model: ModelPolicy::Select(ModelSelection {
provider: Some("openai-codex".into()),
model: "gpt-5.5".into(),
auth: Some("codex".into()),
fast: true,
}),
reasoning: Some(ReasoningLevel::High),
},
..definition(ToolPolicy::All).as_ref().clone()
});
let store = credentials(&["codex", "xai-api-key"]);
let cases = [
(
None,
None,
("xai", "grok-4.7", "xai-api-key", ReasoningLevel::High),
),
(
Some(ReasoningLevel::Low),
None,
("xai", "grok-4.7", "xai-api-key", ReasoningLevel::Low),
),
(
Some(ReasoningLevel::Low),
Some("openai-codex/gpt-6.1-sol"),
("openai-codex", "gpt-6.1-sol", "codex", ReasoningLevel::Low),
),
];
for (configured, per_call, expected) in cases {
let mut host = Config::default();
let mut entry = crate::config::InternalAgentModelConfig::new(
"xai".into(),
"grok-4.7".into(),
"xai-api-key".into(),
);
entry.reasoning = configured;
host.set_internal_agent_model_config("test", entry);
let bound = AgentBinder::bind_with_credentials(
pinned.clone(),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&host,
per_call.map(ModelOverride::Reference),
&store,
)
.expect("bind");
let config = bound.rho_config().expect("rho config");
assert_eq!(
(
config.expect_conversation_model().provider.as_str(),
config.expect_conversation_model().model.as_str(),
config.expect_conversation_model().auth.as_str(),
config.fast_mode,
config.reasoning,
),
(expected.0, expected.1, expected.2, false, expected.3),
"{configured:?} {per_call:?}"
);
}
}
#[test]
fn configured_model_override_refuses_a_runtime_switch() {
use crate::agent::configured_model::ConfiguredModelError;
let rho = definition(ToolPolicy::All);
let claude = claude_definition(ModelPolicy::Inherit);
let cases = [
(
rho.clone(),
crate::config::InternalAgentModelConfig::claude_cli(None),
ConfiguredModelError::ClaudeCodeTarget { id: rho.id.clone() },
),
(
claude.clone(),
crate::config::InternalAgentModelConfig::new(
"xai".into(),
"grok-4.7".into(),
"xai-api-key".into(),
),
ConfiguredModelError::ExternalRuntime {
id: claude.id.clone(),
runtime: claude.runtime.runtime().to_string(),
},
),
];
for (agent, entry, expected) in cases {
let mut host = Config::default();
host.set_internal_agent_model_config(agent.id.as_str(), entry);
let result = AgentBinder::bind_with_credentials(
agent.clone(),
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&host,
None,
&credentials(&["xai-api-key"]),
);
let error = result.expect_err("bound with a runtime switch");
assert_eq!(
error.downcast_ref::<ConfiguredModelError>(),
Some(&expected)
);
}
}
#[test]
fn configured_model_override_skips_built_in_agents() {
let explorer = Arc::new(AgentDefinition {
id: AgentId::new("explorer").unwrap(),
..definition_with_model(ModelPolicy::Select(ModelSelection {
provider: Some("openai-codex".into()),
model: "gpt-5.5".into(),
auth: Some("codex".into()),
fast: false,
}))
.as_ref()
.clone()
});
let mut host = Config::default();
host.set_internal_agent_model_config(
"explorer",
crate::config::InternalAgentModelConfig::new(
"xai".into(),
"grok-4.7".into(),
"xai-api-key".into(),
),
);
let bound = AgentBinder::bind_with_credentials(
explorer,
AgentInvocation {
role: AgentRole::Delegated,
available_tools: capabilities(),
},
&host,
None,
&credentials(&["codex", "xai-api-key"]),
)
.expect("bind");
let config = bound.rho_config().expect("rho config");
assert_eq!(
(
config.expect_conversation_model().provider.as_str(),
config.expect_conversation_model().model.as_str()
),
("openai-codex", "gpt-5.5")
);
}