mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
Support plugin agent roles
This commit is contained in:
@@ -8,6 +8,8 @@ use codex_exec_server::ExecutorFileSystem;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_absolute_path::AbsolutePathBufGuard;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use codex_utils_plugins::PluginAgentRoot;
|
||||
use codex_utils_plugins::plugin_namespace_for_skill_path;
|
||||
use serde::Deserialize;
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::BTreeSet;
|
||||
@@ -115,6 +117,81 @@ pub(crate) async fn load_agent_roles(
|
||||
Ok(roles)
|
||||
}
|
||||
|
||||
pub(crate) async fn load_plugin_agent_roles(
|
||||
fs: &dyn ExecutorFileSystem,
|
||||
plugin_agent_roots: Vec<PluginAgentRoot>,
|
||||
startup_warnings: &mut Vec<String>,
|
||||
) -> std::io::Result<BTreeMap<String, AgentRoleConfig>> {
|
||||
let mut roles = BTreeMap::new();
|
||||
for plugin_agent_root in plugin_agent_roots {
|
||||
let discovered_roles = discover_agent_roles_in_dir(
|
||||
fs,
|
||||
&plugin_agent_root.path,
|
||||
&BTreeSet::new(),
|
||||
startup_warnings,
|
||||
)
|
||||
.await?;
|
||||
if discovered_roles.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let Some(plugin_namespace) = plugin_agent_role_namespace(fs, &plugin_agent_root).await
|
||||
else {
|
||||
push_agent_role_warning(
|
||||
startup_warnings,
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
format!(
|
||||
"unable to determine plugin namespace for agent roles in {}",
|
||||
plugin_agent_root.path.as_path().display()
|
||||
),
|
||||
),
|
||||
);
|
||||
continue;
|
||||
};
|
||||
|
||||
for (role_name, role) in discovered_roles {
|
||||
let namespaced_role_name = format!("{plugin_namespace}:{role_name}");
|
||||
if roles.contains_key(&namespaced_role_name) {
|
||||
push_agent_role_warning(
|
||||
startup_warnings,
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
format!(
|
||||
"duplicate plugin agent role name `{namespaced_role_name}` discovered in {}",
|
||||
plugin_agent_root.path.as_path().display()
|
||||
),
|
||||
),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if let Err(err) = validate_required_agent_role_description(
|
||||
&namespaced_role_name,
|
||||
role.description.as_deref(),
|
||||
) {
|
||||
push_agent_role_warning(startup_warnings, err);
|
||||
continue;
|
||||
}
|
||||
roles.insert(namespaced_role_name, role);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(roles)
|
||||
}
|
||||
|
||||
async fn plugin_agent_role_namespace(
|
||||
fs: &dyn ExecutorFileSystem,
|
||||
plugin_agent_root: &PluginAgentRoot,
|
||||
) -> Option<String> {
|
||||
plugin_namespace_for_skill_path(fs, &plugin_agent_root.path)
|
||||
.await
|
||||
.or_else(|| {
|
||||
plugin_agent_root
|
||||
.plugin_id
|
||||
.split_once('@')
|
||||
.map(|(name, _)| name.to_string())
|
||||
})
|
||||
}
|
||||
|
||||
fn push_agent_role_warning(startup_warnings: &mut Vec<String>, err: std::io::Error) {
|
||||
let message = format!("Ignoring malformed agent role definition: {err}");
|
||||
tracing::warn!("{message}");
|
||||
|
||||
@@ -95,6 +95,7 @@ use codex_protocol::protocol::NetworkAccess;
|
||||
use codex_protocol::protocol::RealtimeVoice;
|
||||
use codex_protocol::protocol::SandboxPolicy;
|
||||
use codex_utils_path_uri::LegacyAppPathString;
|
||||
use codex_utils_plugins::PluginAgentRoot;
|
||||
use serde::Deserialize;
|
||||
use tempfile::tempdir;
|
||||
|
||||
@@ -7464,6 +7465,62 @@ nickname_candidates = ["Noether"]
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn plugin_agent_roles_are_namespaced_with_plugin_manifest_name() -> std::io::Result<()> {
|
||||
let temp_dir = TempDir::new()?;
|
||||
let plugin_root = temp_dir.path().join("plugins").join("sample");
|
||||
let agents_dir = plugin_root.join("agents");
|
||||
let agent_path = agents_dir.join("researcher.toml");
|
||||
tokio::fs::create_dir_all(plugin_root.join(".codex-plugin")).await?;
|
||||
tokio::fs::write(
|
||||
plugin_root.join(".codex-plugin").join("plugin.json"),
|
||||
r#"{"name":"sample-plugin"}"#,
|
||||
)
|
||||
.await?;
|
||||
tokio::fs::create_dir_all(&agents_dir).await?;
|
||||
tokio::fs::write(
|
||||
&agent_path,
|
||||
r#"
|
||||
name = "researcher"
|
||||
description = "Research role"
|
||||
nickname_candidates = ["Hypatia"]
|
||||
developer_instructions = "Research carefully"
|
||||
model = "gpt-5.2"
|
||||
"#,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let mut startup_warnings = Vec::new();
|
||||
let roles = agent_roles::load_plugin_agent_roles(
|
||||
LOCAL_FS.as_ref(),
|
||||
vec![PluginAgentRoot {
|
||||
path: agents_dir.abs(),
|
||||
plugin_id: "fallback@test".to_string(),
|
||||
plugin_root: plugin_root.abs(),
|
||||
}],
|
||||
&mut startup_warnings,
|
||||
)
|
||||
.await?;
|
||||
|
||||
assert_eq!(startup_warnings, Vec::<String>::new());
|
||||
assert_eq!(
|
||||
roles.get("sample-plugin:researcher").map(|role| (
|
||||
role.description.as_deref(),
|
||||
role.config_file.as_ref(),
|
||||
role.nickname_candidates
|
||||
.as_ref()
|
||||
.map(|candidates| candidates.iter().map(String::as_str).collect::<Vec<_>>())
|
||||
)),
|
||||
Some((
|
||||
Some("Research role"),
|
||||
Some(&agent_path),
|
||||
Some(vec!["Hypatia"])
|
||||
))
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn agent_role_file_without_developer_instructions_is_dropped_with_warning()
|
||||
-> std::io::Result<()> {
|
||||
|
||||
@@ -2,6 +2,7 @@ use super::*;
|
||||
use crate::environment_selection::TurnEnvironmentSnapshot;
|
||||
use crate::shell_snapshot::ShellSnapshotFile;
|
||||
use codex_core_skills::HostSkillsSnapshot;
|
||||
use codex_exec_server::LOCAL_FS;
|
||||
use codex_file_system::FileSystemSandboxContext;
|
||||
use codex_model_provider::SharedModelProvider;
|
||||
use codex_model_provider::create_model_provider;
|
||||
@@ -684,7 +685,7 @@ impl Session {
|
||||
.as_ref()
|
||||
.and_then(|turn_environment| turn_environment.cwd().to_abs_path().ok())
|
||||
.unwrap_or_else(|| session_configuration.cwd().clone());
|
||||
let per_turn_config = Self::build_per_turn_config(&session_configuration, cwd.clone());
|
||||
let mut per_turn_config = Self::build_per_turn_config(&session_configuration, cwd.clone());
|
||||
{
|
||||
let mcp_runtime = self.services.latest_mcp_runtime();
|
||||
let mcp_connection_manager = mcp_runtime.manager();
|
||||
@@ -720,6 +721,27 @@ impl Session {
|
||||
.plugins_manager
|
||||
.plugins_for_config(&plugins_input)
|
||||
.await;
|
||||
let mut plugin_agent_role_warnings = Vec::new();
|
||||
match crate::config::agent_roles::load_plugin_agent_roles(
|
||||
LOCAL_FS.as_ref(),
|
||||
plugin_outcome.effective_plugin_agent_roots(),
|
||||
&mut plugin_agent_role_warnings,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(plugin_agent_roles) => {
|
||||
for (role_name, role) in plugin_agent_roles {
|
||||
per_turn_config.agent_roles.entry(role_name).or_insert(role);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
plugin_agent_role_warnings
|
||||
.push(format!("Failed to load plugin agent roles: {err}"));
|
||||
}
|
||||
}
|
||||
per_turn_config
|
||||
.startup_warnings
|
||||
.extend(plugin_agent_role_warnings);
|
||||
let effective_skill_roots = plugin_outcome.effective_plugin_skill_roots();
|
||||
let plugin_skill_snapshots = self
|
||||
.services
|
||||
|
||||
@@ -29,6 +29,7 @@ use core_test_support::test_codex::TestCodex;
|
||||
use core_test_support::test_codex::test_codex;
|
||||
use core_test_support::wait_for_event;
|
||||
use core_test_support::wait_for_mcp_server;
|
||||
use serde_json::Value;
|
||||
use tempfile::TempDir;
|
||||
use wiremock::MockServer;
|
||||
|
||||
@@ -39,6 +40,8 @@ const SAMPLE_PLUGIN_APP_NAMESPACE: &str = "mcp__codex_apps__google_calendar";
|
||||
const SAMPLE_PLUGIN_MCP_NAMESPACE: &str = "mcp__sample";
|
||||
const PLUGIN_APP_SEARCH_CALL_ID: &str = "plugin-app-search";
|
||||
const PLUGIN_MCP_SEARCH_CALL_ID: &str = "plugin-mcp-search";
|
||||
const MULTI_AGENT_V1_NAMESPACE: &str = "multi_agent_v1";
|
||||
const SPAWN_AGENT_TOOL_NAME: &str = "spawn_agent";
|
||||
|
||||
fn sample_plugin_root(home: &TempDir) -> std::path::PathBuf {
|
||||
home.path().join("plugins/cache/test/sample/local")
|
||||
@@ -76,6 +79,20 @@ fn write_plugin_skill_plugin(home: &TempDir) -> std::path::PathBuf {
|
||||
skill_dir.join("SKILL.md")
|
||||
}
|
||||
|
||||
fn write_plugin_agent_plugin(home: &TempDir) {
|
||||
let plugin_root = write_sample_plugin_manifest_and_config(home);
|
||||
let agents_dir = plugin_root.join("agents");
|
||||
std::fs::create_dir_all(&agents_dir).expect("create plugin agents dir");
|
||||
std::fs::write(
|
||||
agents_dir.join("researcher.toml"),
|
||||
r#"name = "researcher"
|
||||
description = "Research sample data"
|
||||
developer_instructions = "Research the sample data carefully"
|
||||
"#,
|
||||
)
|
||||
.expect("write plugin agent role");
|
||||
}
|
||||
|
||||
fn write_plugin_mcp_plugin(home: &TempDir, command: &str) {
|
||||
let plugin_root = write_sample_plugin_manifest_and_config(home);
|
||||
std::fs::write(
|
||||
@@ -204,6 +221,15 @@ fn searched_plugin_tools(
|
||||
)
|
||||
}
|
||||
|
||||
fn tool_parameter_description(tool: &Value, parameter_name: &str) -> Option<String> {
|
||||
tool.get("parameters")
|
||||
.and_then(|parameters| parameters.get("properties"))
|
||||
.and_then(|properties| properties.get(parameter_name))
|
||||
.and_then(|parameter| parameter.get("description"))
|
||||
.and_then(Value::as_str)
|
||||
.map(str::to_string)
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn capability_sections_render_in_developer_message_in_order() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
@@ -274,6 +300,42 @@ async fn capability_sections_render_in_developer_message_in_order() -> Result<()
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn plugin_agent_roles_are_available_to_spawn_agent() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
let server = start_mock_server().await;
|
||||
let resp_mock = mount_sse_once(
|
||||
&server,
|
||||
sse(vec![ev_response_created("resp1"), ev_completed("resp1")]),
|
||||
)
|
||||
.await;
|
||||
|
||||
let codex_home = Arc::new(TempDir::new()?);
|
||||
write_plugin_agent_plugin(codex_home.as_ref());
|
||||
let mut builder = test_codex().with_home(codex_home).with_config(|config| {
|
||||
config
|
||||
.features
|
||||
.enable(Feature::Collab)
|
||||
.expect("test config should allow feature update");
|
||||
config.multi_agent_v2.hide_spawn_agent_metadata = false;
|
||||
});
|
||||
let test_codex = builder.build(&server).await?;
|
||||
|
||||
test_codex.submit_turn("hello").await?;
|
||||
|
||||
let body = resp_mock.single_request().body_json();
|
||||
let spawn_agent = namespace_child_tool(&body, MULTI_AGENT_V1_NAMESPACE, SPAWN_AGENT_TOOL_NAME)
|
||||
.expect("spawn_agent should be available");
|
||||
let agent_type_description = tool_parameter_description(spawn_agent, "agent_type")
|
||||
.expect("spawn_agent agent_type description");
|
||||
assert!(
|
||||
agent_type_description.contains("sample:researcher: {\nResearch sample data\n}"),
|
||||
"expected plugin agent role in spawn_agent description: {agent_type_description:?}"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn explicit_plugin_mentions_use_apps_for_chatgpt_dual_surface_plugins() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
Reference in New Issue
Block a user