From b2a3144c5e249bca373397f990f8dc0623c0d0bf Mon Sep 17 00:00:00 2001 From: charlesgong-openai Date: Wed, 8 Jul 2026 14:48:32 -0400 Subject: [PATCH] Discover repository marketplaces for Claude projects --- codex-rs/app-server/README.md | 2 +- .../src/config/external_agent_config.rs | 96 +++++++++++---- .../src/config/external_agent_config_tests.rs | 115 ++++++++++++++++++ .../tests/suite/v2/external_agent_config.rs | 98 +++++++++++++++ 4 files changed, 285 insertions(+), 26 deletions(-) diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index 6ee6baf2dc..e43f048235 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -239,7 +239,7 @@ Example with notification opt-out: - `windowsSandbox/setupStart` — start Windows sandbox setup for the selected mode (`elevated` or `unelevated`); accepts an optional absolute `cwd` to target setup for a specific workspace, returns `{ started: true }` immediately, and later emits `windowsSandbox/setupCompleted`. - `feedback/upload` — submit a feedback report (classification + optional reason/logs, conversation_id, and optional `extraLogFiles` attachments array); returns the tracking thread id. - `config/read` — fetch the effective config on disk after resolving config layering, including opaque `desktop` values stored in `config.toml`. -- `externalAgentConfig/detect` — detect migratable external-agent artifacts with `includeHome` and optional `cwds`; each detected item includes `cwd` (`null` for home), and multi-item migrations may additionally include structured `details` with plugin ids, skill names, session metadata, or other artifact names. +- `externalAgentConfig/detect` — detect migratable external-agent artifacts with `includeHome` and optional `cwds`. When `includeHome` is true, detection also checks project roots recorded in the external agent's home project registry and recent session metadata, so project-scoped configuration and repository marketplaces can be included without the caller enumerating every project. Each detected item includes `cwd` (`null` for home), and multi-item migrations may additionally include structured `details` with plugin ids, skill names, session metadata, or other artifact names. - `externalAgentConfig/import` — apply selected external-agent migration items by passing explicit `migrationItems` with `cwd` (`null` for home) and any `details` returned by detect. Callers may pass `source` to identify the product that initiated the import; omitted or `null` means unspecified. The response acknowledges the synchronous import phase with an `importId`. Expected migration failures are reported as per-item failures rather than JSON-RPC errors, so the server still returns that `importId` and emits `externalAgentConfig/import/completed` with the same ID once all synchronous and background work finishes. The completion notification contains type-level `itemTypeResults` with successes and failures, including raw failure messages for the client to report separately. - `config/value/write` — write a single config key/value to the user's config.toml on disk; dotted paths such as `desktop.someKey` use the same generic write surface. - `config/batchWrite` — apply multiple config edits atomically to the user's config.toml on disk, with optional `reloadUserConfig: true` to hot-reload loaded threads, including multiple `desktop.*` edits. diff --git a/codex-rs/app-server/src/config/external_agent_config.rs b/codex-rs/app-server/src/config/external_agent_config.rs index 563f6ef5a9..00ffcb44a8 100644 --- a/codex-rs/app-server/src/config/external_agent_config.rs +++ b/codex-rs/app-server/src/config/external_agent_config.rs @@ -38,6 +38,7 @@ const EXTERNAL_AGENT_CONFIG_DETECT_METRIC: &str = "codex.external_agent_config.d const EXTERNAL_AGENT_CONFIG_IMPORT_METRIC: &str = "codex.external_agent_config.import"; const EXTERNAL_AGENT_DIR: &str = ".claude"; const EXTERNAL_AGENT_CONFIG_MD: &str = "CLAUDE.md"; +const EXTERNAL_AGENT_PROJECTS_CONFIG_FILE: &str = ".claude.json"; const EXTERNAL_OFFICIAL_MARKETPLACE_NAME: &str = "claude-plugins-official"; const EXTERNAL_OFFICIAL_MARKETPLACE_SOURCE: &str = "anthropics/claude-plugins-official"; @@ -202,16 +203,83 @@ impl ExternalAgentConfigService { params: ExternalAgentConfigDetectOptions, ) -> io::Result> { let mut items = Vec::new(); - for cwd in params.cwds.as_deref().unwrap_or(&[]) { + let sessions = if params.include_home { + detect_recent_sessions(&self.external_agent_home, &self.codex_home)? + } else { + Vec::new() + }; + let mut project_cwds = params.cwds.unwrap_or_default(); + if params.include_home { + let project_config_path = self + .external_agent_home + .parent() + .map(|parent| parent.join(EXTERNAL_AGENT_PROJECTS_CONFIG_FILE)) + .unwrap_or_else(|| PathBuf::from(EXTERNAL_AGENT_PROJECTS_CONFIG_FILE)); + match read_external_settings(&project_config_path) { + Ok(Some(settings)) => { + if let Some(projects) = settings + .as_object() + .and_then(|settings| settings.get("projects")) + .and_then(JsonValue::as_object) + { + for project_root in projects.keys().map(PathBuf::from) { + if project_root.is_absolute() { + project_cwds.push(project_root); + } else { + tracing::warn!( + project_root = %project_root.display(), + path = %project_config_path.display(), + "ignoring non-absolute external agent project root during import detection" + ); + } + } + } + } + Ok(None) => {} + Err(err) => { + tracing::warn!( + path = %project_config_path.display(), + error = %err, + "failed to read external agent project registry during import detection" + ); + } + } + project_cwds.extend(sessions.iter().map(|session| session.cwd.clone())); + } + + let mut detected_repo_roots = HashSet::new(); + for cwd in &project_cwds { let Some(repo_root) = find_repo_root(Some(cwd))? else { continue; }; + if !detected_repo_roots.insert(repo_root.clone()) { + continue; + } self.detect_migrations(Some(&repo_root), &mut items).await?; } if params.include_home { self.detect_migrations(/*repo_root*/ None, &mut items) .await?; + if !sessions.is_empty() { + items.push(ExternalAgentConfigMigrationItem { + item_type: ExternalAgentConfigMigrationItemType::Sessions, + description: format!( + "Migrate recent sessions from {}", + self.external_agent_home.join("projects").display() + ), + cwd: None, + details: Some(MigrationDetails { + sessions, + ..Default::default() + }), + }); + emit_migration_metric( + EXTERNAL_AGENT_CONFIG_DETECT_METRIC, + ExternalAgentConfigMigrationItemType::Sessions, + /*skills_count*/ None, + ); + } } deduplicate_plugin_migration_items(&mut items); @@ -734,29 +802,6 @@ impl ExternalAgentConfigService { } } - if repo_root.is_none() { - let sessions = detect_recent_sessions(&self.external_agent_home, &self.codex_home)?; - if !sessions.is_empty() { - items.push(ExternalAgentConfigMigrationItem { - item_type: ExternalAgentConfigMigrationItemType::Sessions, - description: format!( - "Migrate recent sessions from {}", - self.external_agent_home.join("projects").display() - ), - cwd: None, - details: Some(MigrationDetails { - sessions, - ..Default::default() - }), - }); - emit_migration_metric( - EXTERNAL_AGENT_CONFIG_DETECT_METRIC, - ExternalAgentConfigMigrationItemType::Sessions, - /*skills_count*/ None, - ); - } - } - Ok(()) } @@ -1453,7 +1498,8 @@ fn extract_plugin_migration_details( plugin = %plugin_id.as_key(), marketplace = %plugin_id.marketplace_name, source_root = %source_root.display(), - "could not find marketplace source needed to reinstall enabled external agent plugin" + repo_root = ?repo_root, + "found enabled external agent plugin in config but could not find marketplace source needed to reinstall it during import" ); continue; } diff --git a/codex-rs/app-server/src/config/external_agent_config_tests.rs b/codex-rs/app-server/src/config/external_agent_config_tests.rs index f096f2915d..699b05032a 100644 --- a/codex-rs/app-server/src/config/external_agent_config_tests.rs +++ b/codex-rs/app-server/src/config/external_agent_config_tests.rs @@ -221,6 +221,121 @@ async fn detect_home_lists_recent_sessions() { ); } +#[tokio::test] +async fn detect_home_discovers_repo_plugins_from_external_agent_project_registry() { + let (root, external_agent_home, codex_home) = fixture_paths(); + let repo_root = root.path().join("repo"); + fs::create_dir_all(&external_agent_home).expect("create external agent home"); + fs::create_dir_all(&codex_home).expect("create codex home"); + write_repo_marketplace_plugin(&repo_root, "treehouse", "build-cop"); + fs::write( + external_agent_home.join("settings.json"), + r#"{ + "enabledPlugins": { + "build-cop@treehouse": true + } + }"#, + ) + .expect("write home settings"); + fs::write( + root.path().join(EXTERNAL_AGENT_PROJECT_CONFIG_FILE), + serde_json::json!({ + "projects": { + repo_root.display().to_string(): {} + } + }) + .to_string(), + ) + .expect("write external agent project registry"); + + let items = service_for_paths(external_agent_home, codex_home) + .detect(ExternalAgentConfigDetectOptions { + include_home: true, + cwds: None, + }) + .await + .expect("detect"); + + let plugin_items = items + .into_iter() + .filter(|item| item.item_type == ExternalAgentConfigMigrationItemType::Plugins) + .collect::>(); + assert_eq!(plugin_items.len(), 1); + assert_eq!(plugin_items[0].cwd.as_ref(), Some(&repo_root)); + assert_eq!( + plugin_items[0] + .details + .as_ref() + .expect("plugin details") + .plugins, + vec![PluginsMigration { + marketplace_name: "treehouse".to_string(), + plugin_names: vec!["build-cop".to_string()], + }] + ); +} + +#[tokio::test] +async fn detect_home_discovers_repo_plugins_from_recent_session_cwds() { + let (root, external_agent_home, codex_home) = fixture_paths(); + let repo_root = root.path().join("repo"); + let session_path = external_agent_home + .join("projects") + .join("repo") + .join("session.jsonl"); + let recent_timestamp = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true); + fs::create_dir_all(&external_agent_home).expect("create external agent home"); + fs::create_dir_all(&codex_home).expect("create codex home"); + fs::create_dir_all(session_path.parent().expect("session parent")).expect("create sessions"); + write_repo_marketplace_plugin(&repo_root, "treehouse", "build-cop"); + fs::write( + external_agent_home.join("settings.json"), + r#"{ + "enabledPlugins": { + "build-cop@treehouse": true + } + }"#, + ) + .expect("write home settings"); + fs::write( + session_path, + serde_json::json!({ + "type": "user", + "cwd": &repo_root, + "timestamp": &recent_timestamp, + "message": { "content": "first request" }, + }) + .to_string(), + ) + .expect("write session"); + + let items = service_for_paths(external_agent_home, codex_home) + .detect(ExternalAgentConfigDetectOptions { + include_home: true, + cwds: None, + }) + .await + .expect("detect"); + + let plugin_items = items + .into_iter() + .filter(|item| item.item_type == ExternalAgentConfigMigrationItemType::Plugins) + .collect::>(); + assert_eq!(plugin_items.len(), 1); + assert_eq!(plugin_items[0].cwd.as_ref(), Some(&repo_root)); + assert_eq!( + plugin_items[0] + .details + .as_ref() + .expect("plugin details") + .plugins, + vec![PluginsMigration { + marketplace_name: "treehouse".to_string(), + plugin_names: vec!["build-cop".to_string()], + }] + ); +} + #[tokio::test] async fn detect_repo_lists_agents_md_for_each_cwd() { let root = TempDir::new().expect("create tempdir"); diff --git a/codex-rs/app-server/tests/suite/v2/external_agent_config.rs b/codex-rs/app-server/tests/suite/v2/external_agent_config.rs index 6bef968941..2bbbea97d7 100644 --- a/codex-rs/app-server/tests/suite/v2/external_agent_config.rs +++ b/codex-rs/app-server/tests/suite/v2/external_agent_config.rs @@ -13,10 +13,13 @@ use codex_app_server_protocol::ExternalAgentConfigImportCompletedNotification; use codex_app_server_protocol::ExternalAgentConfigImportHistoriesReadResponse; use codex_app_server_protocol::ExternalAgentConfigImportProgressNotification; use codex_app_server_protocol::ExternalAgentConfigImportResponse; +use codex_app_server_protocol::ExternalAgentConfigMigrationItem; use codex_app_server_protocol::ExternalAgentConfigMigrationItemType; use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::MigrationDetails; use codex_app_server_protocol::PluginListParams; use codex_app_server_protocol::PluginListResponse; +use codex_app_server_protocol::PluginsMigration; use codex_app_server_protocol::RequestId; use codex_app_server_protocol::ThreadItem; use codex_app_server_protocol::ThreadListParams; @@ -417,6 +420,101 @@ async fn external_agent_config_import_completed_tracks_analytics_event() -> Resu Ok(()) } +#[tokio::test] +async fn external_agent_config_detect_discovers_repo_marketplace_from_project_registry() +-> Result<()> { + let codex_home = TempDir::new()?; + let source_home = external_agent_home(codex_home.path()); + let repo_root = codex_home.path().join("repo"); + let plugin_root = repo_root.join("plugins/build-cop"); + std::fs::create_dir_all(&source_home)?; + std::fs::create_dir_all(repo_root.join(".git"))?; + std::fs::create_dir_all(repo_root.join(".claude-plugin"))?; + std::fs::create_dir_all(plugin_root.join(".codex-plugin"))?; + std::fs::write( + source_home.join("settings.json"), + r#"{ + "enabledPlugins": { + "build-cop@treehouse": true + } +}"#, + )?; + std::fs::write( + codex_home.path().join(".claude.json"), + serde_json::json!({ + "projects": { + repo_root.display().to_string(): {} + } + }) + .to_string(), + )?; + std::fs::write( + repo_root.join(".claude-plugin/marketplace.json"), + r#"{ + "name": "treehouse", + "plugins": [{ + "name": "build-cop", + "source": { + "source": "local", + "path": "./plugins/build-cop" + } + }] +}"#, + )?; + std::fs::write( + plugin_root.join(".codex-plugin/plugin.json"), + r#"{"name":"build-cop","version":"0.1.0"}"#, + )?; + + let home_dir = codex_home.path().display().to_string(); + let mut mcp = TestAppServer::builder() + .with_codex_home(codex_home.path()) + .without_auto_env() + .with_env_overrides(&[("HOME", Some(home_dir.as_str()))]) + .build() + .await?; + timeout(DEFAULT_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp + .send_raw_request( + "externalAgentConfig/detect", + Some(serde_json::json!({ "includeHome": true })), + ) + .await?; + let response: JSONRPCResponse = timeout( + DEFAULT_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + let response: ExternalAgentConfigDetectResponse = to_response(response)?; + let plugin_items = response + .items + .into_iter() + .filter(|item| item.item_type == ExternalAgentConfigMigrationItemType::Plugins) + .collect::>(); + + assert_eq!( + plugin_items, + vec![ExternalAgentConfigMigrationItem { + item_type: ExternalAgentConfigMigrationItemType::Plugins, + description: format!( + "Migrate enabled plugins from {}", + repo_root.join(".claude/settings.json").display() + ), + cwd: Some(repo_root), + details: Some(MigrationDetails { + plugins: vec![PluginsMigration { + marketplace_name: "treehouse".to_string(), + plugin_names: vec!["build-cop".to_string()], + }], + ..Default::default() + }), + }] + ); + + Ok(()) +} + #[tokio::test] async fn external_agent_config_import_sends_completion_notification_for_local_plugins() -> Result<()> {