diff --git a/crates/goose/src/sources.rs b/crates/goose/src/sources.rs index e597f4166626..b00ffdd60b36 100644 --- a/crates/goose/src/sources.rs +++ b/crates/goose/src/sources.rs @@ -324,22 +324,6 @@ fn builtin_skill_entry(mut source: SourceEntry) -> SourceEntry { source } -fn agent_base_dir(global: bool, project_dir: Option<&str>) -> Result { - if global { - Ok(Paths::agents_dir()) - } else { - let project_dir = project_dir.ok_or_else(|| { - Error::invalid_params().data("projectDir is required when global is false") - })?; - if project_dir.trim().is_empty() { - return Err( - Error::invalid_params().data("projectDir must not be empty when global is false") - ); - } - Ok(Path::new(project_dir).join(".agents").join("agents")) - } -} - fn validate_agent_name(name: &str) -> Result<(), Error> { let trimmed = name.trim(); if trimmed.is_empty() { @@ -623,10 +607,13 @@ fn create_agent_source( content: &str, properties: HashMap, global: bool, - project_dir: Option<&str>, ) -> Result { + if !global { + return Err(Error::invalid_params() + .data("Project-scoped Agent creation and import are not supported")); + } validate_agent_name(name)?; - let base = agent_base_dir(global, project_dir)?; + let base = Paths::agents_dir(); let slug = slugify_agent_name(name); let mut file_path = base.join(format!("{slug}.md")); if file_path.exists() { @@ -686,7 +673,7 @@ pub fn create_source( ) -> Result { require_mutable_type(source_type)?; if source_type == SourceType::Agent { - return create_agent_source(name, description, content, properties, global, project_dir); + return create_agent_source(name, description, content, properties, global); } match source_type { @@ -1198,15 +1185,8 @@ pub fn import_sources( .or_insert_with(|| value.clone()); } } - return create_agent_source( - &name, - &description, - &content, - properties, - global, - project_dir, - ) - .map(|source| vec![source]); + return create_agent_source(&name, &description, &content, properties, global) + .map(|source| vec![source]); } match source_type { diff --git a/crates/goose/tests/acp_custom_requests_test.rs b/crates/goose/tests/acp_custom_requests_test.rs index 56d7dfef88b7..0594e47ff402 100644 --- a/crates/goose/tests/acp_custom_requests_test.rs +++ b/crates/goose/tests/acp_custom_requests_test.rs @@ -767,6 +767,70 @@ fn test_custom_list_builtin_skill_sources() { }); } +#[test] +#[serial] +fn test_custom_agent_source_mutations_reject_project_scopes() { + write_acp_global_config(DEFAULT_ACP_TEST_CONFIG); + run_test(async move { + let project_dir = tempfile::tempdir().unwrap(); + let project = goose::sources::create_source( + goose_acp_types::custom_requests::SourceType::Project, + "agent-scope-regression", + "Project scope regression", + "", + true, + None, + std::collections::HashMap::from([( + "workingDirs".to_owned(), + serde_json::json!([project_dir.path()]), + )]), + ) + .unwrap(); + let openai = OpenAiFixture::new(vec![], Arc::new(EnforceSessionId::default())).await; + let conn = AcpServerConnection::new(TestConnectionConfig::default(), openai).await; + + for target in [ + serde_json::json!({ "scope": "projectDir", "projectDir": project_dir.path() }), + serde_json::json!({ "scope": "projectId", "projectId": project.name }), + ] { + for (method, request) in [ + ( + "_goose/unstable/sources/create", + serde_json::json!({ + "type": "agent", + "name": "Project Agent", + "description": "An agent", + "content": "Instructions", + "target": target + }), + ), + ( + "_goose/unstable/sources/import", + serde_json::json!({ + "data": serde_json::json!({ + "version": 1, + "type": "agent", + "name": "Imported Agent", + "content": "Instructions" + }).to_string(), + "target": target + }), + ), + ] { + let error = send_custom(conn.cx(), method, request).await.unwrap_err(); + assert_eq!(error.code, agent_client_protocol::ErrorCode::InvalidParams); + assert!(error.to_string().contains("Project-scoped Agent")); + } + } + assert_eq!(std::fs::read_dir(project_dir.path()).unwrap().count(), 0); + goose::sources::delete_source( + goose_acp_types::custom_requests::SourceType::Project, + &project.path, + ) + .unwrap(); + }); +} + #[test] #[serial] fn test_custom_provider_inventory_includes_metadata() { diff --git a/crates/goose/tests/agent_source_mutation_test.rs b/crates/goose/tests/agent_source_mutation_test.rs new file mode 100644 index 000000000000..07e72d2e242e --- /dev/null +++ b/crates/goose/tests/agent_source_mutation_test.rs @@ -0,0 +1,120 @@ +use agent_client_protocol::ErrorCode; +use goose::config::paths::Paths; +use goose::sources::{create_source, import_sources, list_sources}; +use goose_acp_types::custom_requests::SourceType; +use serde_json::json; +use std::collections::HashMap; +use std::path::Path; + +fn assert_project_mutations_rejected(project: &Path) { + let create = create_source( + SourceType::Agent, + "Project Agent", + "An agent", + "Instructions", + false, + project.to_str(), + HashMap::new(), + ); + let import = import_sources( + &json!({ + "version": 1, + "type": "agent", + "name": "Imported Agent", + "instructions": "Imported instructions", + "metadata": { "model": "test-model" } + }) + .to_string(), + false, + project.to_str(), + ); + for error in [create.unwrap_err(), import.unwrap_err()] { + assert_eq!(error.code, ErrorCode::InvalidParams); + assert!(error.to_string().contains("Project-scoped Agent")); + } +} + +#[test] +fn project_agent_create_and_import_do_not_create_directories() { + let tmp = tempfile::tempdir().unwrap(); + let project = tmp.path().join("project"); + assert_project_mutations_rejected(&project); + assert!(!project.exists()); + + std::fs::create_dir(&project).unwrap(); + assert_project_mutations_rejected(&project); + assert_eq!(std::fs::read_dir(project).unwrap().count(), 0); +} + +#[test] +fn existing_project_agents_remain_discoverable() { + let tmp = tempfile::tempdir().unwrap(); + let _env = env_lock::lock_env([("GOOSE_PATH_ROOT", tmp.path().to_str())]); + let project = tmp.path().join("project"); + let agent_dir = project.join(".agents/agents"); + std::fs::create_dir_all(&agent_dir).unwrap(); + let agent_path = agent_dir.join("existing.md"); + let contents = "---\nname: Existing Project Agent\ndescription: Existing\n---\n\nInstructions"; + std::fs::write(&agent_path, contents).unwrap(); + + let sources = list_sources(Some(SourceType::Agent), project.to_str(), false).unwrap(); + let agent = sources + .iter() + .find(|source| source.name == "Existing Project Agent") + .unwrap(); + assert!(!agent.global); + assert_eq!(agent.content, "Instructions"); + assert_eq!(std::fs::read_to_string(agent_path).unwrap(), contents); + assert_eq!(std::fs::read_dir(agent_dir).unwrap().count(), 1); +} + +#[test] +fn global_agent_create_and_import_preserve_content_and_collision_naming() { + let tmp = tempfile::tempdir().unwrap(); + let _env = env_lock::lock_env([("GOOSE_PATH_ROOT", tmp.path().to_str())]); + let properties = HashMap::from([("model".to_owned(), json!("test-model"))]); + let created = create_source( + SourceType::Agent, + "Global Agent", + "Global description", + "Global instructions", + true, + None, + properties.clone(), + ) + .unwrap(); + assert!(created.global); + assert!(created.writable); + assert_eq!(created.properties, properties); + assert_eq!( + Path::new(&created.path), + Paths::agents_dir().join("global-agent.md") + ); + + let imported = import_sources( + &json!({ + "version": 1, + "type": "agent", + "name": created.name, + "description": created.description, + "content": created.content, + "properties": properties + }) + .to_string(), + true, + None, + ) + .unwrap() + .remove(0); + assert!(imported.global); + assert!(imported.writable); + assert_eq!(imported.name, created.name); + assert_eq!(imported.description, created.description); + assert_eq!(imported.content, created.content); + assert_eq!(imported.properties, properties); + assert_eq!( + Path::new(&imported.path), + Paths::agents_dir().join("global-agent-2.md") + ); + assert!(Path::new(&created.path).is_file()); +}