diff --git a/crates/anyagents/src/create.rs b/crates/anyagents/src/create.rs index 476b66ca..ea6d77d7 100644 --- a/crates/anyagents/src/create.rs +++ b/crates/anyagents/src/create.rs @@ -227,6 +227,14 @@ fn create_with_seat( .map(str::to_string) .or_else(|| new.description.clone()); new.parent_id = request.parent_id; + // Refused before the row exists: a creation rolled back later costs a + // delete of everything keyed by the agent. + ensure_harness_runs_profile( + data, + &normalized_harness, + request.profile.as_deref(), + new.model.as_deref(), + )?; let agent = store.create_agent(new)?; let data = std::path::absolute(data)?; // A checkout the consumer produced for this creation; rollback removes it. @@ -5177,6 +5185,48 @@ pub(crate) fn ensure_profile_compatible_and_mapped( Ok(()) } +/// Whether `harness` can run the provider profile a creation names, checked +/// before anything is created: a creation refused after its row exists is +/// rolled back with a delete of everything keyed by the agent. Only +/// compatibility is checked here; [`ensure_profile_compatible_and_mapped`] +/// still checks the profile's mapping once the agent's config exists. +pub(crate) fn ensure_harness_runs_profile( + data: &Path, + harness: &str, + profile: Option<&str>, + model: Option<&str>, +) -> Result<(), Error> { + let Some(explicit) = profile.map(str::trim).filter(|s| !s.is_empty()) else { + return Ok(()); + }; + let harness = normalize_harness_name(harness); + let (provider, profile_name) = match explicit.split_once('/') { + Some((p, n)) => (p.trim().to_string(), n.trim().to_string()), + None => (upstream_provider_of(harness, model), explicit.to_string()), + }; + let plain = |s: &str| { + !s.is_empty() + && s.bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_' || b == b'.') + && s != "." + && s != ".." + }; + if !plain(&provider) || !plain(&profile_name) { + // The full check refuses it once the config exists. + return Ok(()); + } + let profile_dir = data.join("profiles").join(&provider).join(&profile_name); + let compatible = profile_compatible_harnesses(&profile_dir, &provider); + if compatible.iter().any(|h| h == harness) { + return Ok(()); + } + Err(Error::Message(format!( + "harness `{harness}` is not compatible with provider profile `{provider}/{profile_name}` \ + (compatible harnesses: {})", + compatible.join(", ") + ))) +} + fn copy_host_file(src: &Path, dst: &Path) -> bool { let Ok(meta) = std::fs::symlink_metadata(src) else { return false; @@ -13785,6 +13835,51 @@ Connection: close assert!(trav.to_string().contains("escapes base"), "{trav}"); } + #[test] + fn a_creation_its_profile_cannot_run_is_refused_and_leaves_no_agent() { + let dir = crate::test_tempdir(); + let data = dir.path(); + let store = Store::open(data.join("state.db")).unwrap(); + store + .hold_capability_key("0123456789abcdef0123456789abcdef") + .unwrap(); + // Report #34's pairing is a supported one (see vendor's Sylphx route). + ensure_harness_runs_profile( + data, + "claude-code", + Some("sylphx/default"), + Some("sylphx/openai/gpt-6.1-sol"), + ) + .unwrap(); + let refused = create( + &store, + data, + CreateRequest { + name: "agy-on-sylphx".to_string(), + role: None, + harness: "agy".to_string(), + model: None, + profile: Some("sylphx/default".to_string()), + repo: None, + branch: None, + base: None, + workdir: None, + workdir_default: Some(data.to_path_buf()), + description: Some("test".to_string()), + instructions: Some("test".to_string()), + parent_id: None, + }, + ) + .unwrap_err() + .to_string(); + assert!( + refused.contains("not compatible with provider profile `sylphx/default`"), + "{refused}" + ); + // Nothing to roll back: no row was written. + assert!(store.agent_by_name("agy-on-sylphx").is_err()); + } + #[test] fn vendor_neutral_profiles_and_fail_closed_harness_mappings() { let dir = crate::test_tempdir(); diff --git a/crates/anyagents/src/mcp_config.rs b/crates/anyagents/src/mcp_config.rs index 22bfa0fb..101102fd 100644 --- a/crates/anyagents/src/mcp_config.rs +++ b/crates/anyagents/src/mcp_config.rs @@ -438,6 +438,55 @@ fn resolve_sylphx_codex_provider(config_dir: Option<&Path>) -> (String, Option, +} + +/// The Sylphx route of a Claude Code agent, or `None` when it is not bound to +/// Sylphx (or no key is found, which leaves Claude Code's own sign-in). +pub(crate) fn claude_sylphx_route( + config_dir: &Path, + model: Option<&str>, +) -> Option { + let profile_is_sylphx = std::fs::read_to_string(config_dir.join(".anyagents-profile")) + .ok() + .is_some_and(|s| { + let t = s.trim(); + t == "sylphx" || t.starts_with("sylphx/") + }); + let model = model.map(str::trim).filter(|m| !m.is_empty()); + let model_is_sylphx = model.is_some_and(|m| m.starts_with("sylphx/")); + if !profile_is_sylphx && !model_is_sylphx { + return None; + } + let (base_url, key) = resolve_sylphx_codex_provider(Some(config_dir)); + let token = key.filter(|k| !k.is_empty())?; + let origin = base_url.trim_end_matches('/'); + let origin = origin.strip_suffix("/v1").unwrap_or(origin).to_string(); + let model = model.map(|m| { + let base = m.split('#').next().unwrap_or(m).trim(); + base.strip_prefix("sylphx/").unwrap_or(base).to_string() + }); + Some(ClaudeSylphxRoute { + base_url: origin, + token, + model, + }) +} + fn toml_args(args: &[String]) -> String { let parts: Vec = args .iter() diff --git a/crates/anyagents/src/vendor.rs b/crates/anyagents/src/vendor.rs index 99b7ed34..39a17754 100644 --- a/crates/anyagents/src/vendor.rs +++ b/crates/anyagents/src/vendor.rs @@ -1787,6 +1787,29 @@ fn plan_adapter( "CLAUDE_CONFIG_DIR".to_string(), request.config_dir.display().to_string(), )); + // A Sylphx-bound agent reaches its model through Sylphx's Anthropic + // Messages API with the profile's key, and asks for the id Sylphx + // serves; the background calls Claude Code makes on its small model + // go to the same model rather than to a Claude id Sylphx may not have. + let sylphx = crate::mcp_config::claude_sylphx_route(&request.config_dir, model.as_deref()); + if let Some(route) = &sylphx { + env.retain(|(key, _)| { + !matches!( + key.as_str(), + "ANTHROPIC_BASE_URL" + | "ANTHROPIC_AUTH_TOKEN" + | "ANTHROPIC_API_KEY" + | "ANTHROPIC_SMALL_FAST_MODEL" + | "ANTHROPIC_DEFAULT_HAIKU_MODEL" + ) + }); + env.push(("ANTHROPIC_BASE_URL".to_string(), route.base_url.clone())); + env.push(("ANTHROPIC_AUTH_TOKEN".to_string(), route.token.clone())); + if let Some(served) = &route.model { + env.push(("ANTHROPIC_SMALL_FAST_MODEL".to_string(), served.clone())); + env.push(("ANTHROPIC_DEFAULT_HAIKU_MODEL".to_string(), served.clone())); + } + } let session = session_from_file(&request.config_dir, "claude-session-id")?; let args = if scripted { Vec::new() @@ -1800,8 +1823,12 @@ fn plan_adapter( } _ => (model, None), }; + let served = sylphx + .as_ref() + .and_then(|route| route.model.clone()) + .unwrap_or_else(|| base_model.to_string()); args.push("--model".to_string()); - args.push(base_model.to_string()); + args.push(served); if let Some(v) = variant { effort = v.to_string(); } @@ -3920,6 +3947,104 @@ done ); } + /// Report #34: a claude-code agent on a Sylphx profile had no way to its + /// model and failed every turn "Not logged in". It is started against + /// Sylphx's Anthropic Messages API with the profile's key and the model id + /// Sylphx serves; an agent on Anthropic is started exactly as before. + #[test] + fn a_claude_code_agent_on_sylphx_reaches_it_with_the_profile_key() { + use anyagents_core::{NewAgent, Store}; + let _env = crate::server::tests::env_lock(); + let dir = crate::test_tempdir(); + let data = dir.path(); + let store = Store::open(data.join("state.db")).unwrap(); + let key_dir = data.join("profiles/sylphx/default/.config/sylphx"); + std::fs::create_dir_all(&key_dir).unwrap(); + std::fs::write(key_dir.join("key"), "sk-sylphx-fixture-key\n").unwrap(); + + let launch = |name: &str, model: &str, profile: Option<&str>| { + let mut new = NewAgent::new(name, "claude-code"); + new.model = Some(model.to_string()); + let agent = store.create_agent(new).unwrap(); + let config = anyagents_core::agent_config_dir(data, &agent.id); + std::fs::create_dir_all(&config).unwrap(); + if let Some(profile) = profile { + std::fs::write(config.join(".anyagents-profile"), format!("{profile}\n")).unwrap(); + } + let request = LaunchRequest { + agent, + data_dir: data.to_owned(), + config_dir: config, + work_dir: data.join("work"), + extra_env: vec![], + lock: None, + }; + match plan_adapter( + &request, + data, + vec![("ANTHROPIC_API_KEY".into(), "leaked".into())], + ) + .unwrap() + .launch + { + AdapterLaunch::Claude { args, env, .. } => (args, env), + other => panic!("{other:?}"), + } + }; + let value = |env: &[(String, String)], key: &str| { + env.iter() + .rev() + .find(|(k, _)| k == key) + .map(|(_, v)| v.clone()) + }; + let scripted = std::env::var_os("ANYAGENTS_FAKE_MODE").is_some(); + + let (args, env) = launch( + "on-sylphx", + "sylphx/openai/gpt-6.1-sol#high", + Some("sylphx/default"), + ); + assert_eq!( + value(&env, "ANTHROPIC_BASE_URL").as_deref(), + Some("https://api.sylphx.com") + ); + assert_eq!( + value(&env, "ANTHROPIC_AUTH_TOKEN").as_deref(), + Some("sk-sylphx-fixture-key") + ); + assert_eq!( + value(&env, "ANTHROPIC_API_KEY"), + None, + "a stray key would win" + ); + assert_eq!( + value(&env, "ANTHROPIC_SMALL_FAST_MODEL").as_deref(), + Some("openai/gpt-6.1-sol") + ); + if !scripted { + let model_at = args.iter().position(|a| a == "--model").unwrap(); + assert_eq!(args[model_at + 1], "openai/gpt-6.1-sol"); + let effort_at = args.iter().position(|a| a == "--effort").unwrap(); + assert_eq!(args[effort_at + 1], "high"); + } + + // A Sylphx model without the profile marker takes the same route. + let (_, env) = launch("model-only", "sylphx/openai/gpt-6.1-sol", None); + assert_eq!( + value(&env, "ANTHROPIC_BASE_URL").as_deref(), + Some("https://api.sylphx.com") + ); + + // An agent on Anthropic gets nothing of the kind. + let (args, env) = launch("on-anthropic", "claude-opus-5-5", Some("anthropic/default")); + assert_eq!(value(&env, "ANTHROPIC_BASE_URL"), None); + assert_eq!(value(&env, "ANTHROPIC_AUTH_TOKEN"), None); + if !scripted { + let model_at = args.iter().position(|a| a == "--model").unwrap(); + assert_eq!(args[model_at + 1], "claude-opus-5-5"); + } + } + #[test] fn shipping_launch_boundary_pins_before_marker_and_refreshes_only_on_cutover() { use anyagents_core::{NewAgent, Store, host_session};