Clarify remote plugin config write routing

This commit is contained in:
xli-oai
2026-04-28 22:17:31 -07:00
parent bdcf098809
commit 0af813a408
2 changed files with 149 additions and 68 deletions
+81 -68
View File
@@ -39,7 +39,6 @@ use codex_app_server_protocol::ClientInfo;
use codex_app_server_protocol::ClientNotification;
use codex_app_server_protocol::ClientRequest;
use codex_app_server_protocol::ConfigBatchWriteParams;
use codex_app_server_protocol::ConfigEdit;
use codex_app_server_protocol::ConfigValueWriteParams;
use codex_app_server_protocol::ConfigWarningNotification;
use codex_app_server_protocol::ConfigWriteResponse;
@@ -995,9 +994,12 @@ impl MessageProcessor {
request_id: ConnectionRequestId,
params: ConfigValueWriteParams,
) {
let result = self
.write_config_value_with_remote_plugin_sync(params)
.await;
let result = if let Some(write) = remote_plugin_enabled_config_value_write(&params) {
self.write_remote_plugin_enabled_config_value(params, write)
.await
} else {
self.config_api.write_value(params).await
};
self.handle_config_mutation_result(request_id, result).await
}
@@ -1006,91 +1008,54 @@ impl MessageProcessor {
request_id: ConnectionRequestId,
params: ConfigBatchWriteParams,
) {
let result = self
.batch_write_config_with_remote_plugin_sync(params)
.await;
let result = match remote_plugin_enabled_config_batch_write(&params) {
Ok(Some(write)) => {
self.write_remote_plugin_enabled_config_batch(params, write)
.await
}
Ok(None) => self.config_api.batch_write(params).await,
Err(err) => Err(err),
};
self.handle_config_mutation_result(request_id, result).await;
}
async fn write_config_value_with_remote_plugin_sync(
async fn write_remote_plugin_enabled_config_value(
&self,
params: ConfigValueWriteParams,
write: RemotePluginEnabledConfigEdit,
) -> Result<ConfigWriteResponse, JSONRPCErrorError> {
let ConfigValueWriteParams {
key_path,
value,
merge_strategy,
file_path,
expected_version,
..
} = params;
if let Some((plugin_id, enabled)) = remote_plugin_enabled_config_edit(&key_path, &value) {
let response = self
.config_api
.batch_write(ConfigBatchWriteParams {
edits: Vec::new(),
file_path,
expected_version,
reload_user_config: false,
})
.await?;
self.codex_message_processor
.sync_remote_plugin_enabled_config_write(plugin_id, enabled)
.await?;
return Ok(response);
}
self.config_api
.write_value(ConfigValueWriteParams {
key_path,
value,
merge_strategy,
let response = self
.config_api
.batch_write(ConfigBatchWriteParams {
edits: Vec::new(),
file_path,
expected_version,
reload_user_config: false,
})
.await
.await?;
self.codex_message_processor
.sync_remote_plugin_enabled_config_write(write.plugin_id, write.enabled)
.await?;
Ok(response)
}
async fn batch_write_config_with_remote_plugin_sync(
async fn write_remote_plugin_enabled_config_batch(
&self,
params: ConfigBatchWriteParams,
write: RemotePluginEnabledConfigBatchWrite,
) -> Result<ConfigWriteResponse, JSONRPCErrorError> {
let ConfigBatchWriteParams {
edits,
file_path,
expected_version,
reload_user_config,
..
} = params;
let mut local_edits = Vec::<ConfigEdit>::new();
let mut remote_plugin_toggles = BTreeMap::<String, bool>::new();
for edit in edits {
if let Some((plugin_id, enabled)) =
remote_plugin_enabled_config_edit(&edit.key_path, &edit.value)
{
remote_plugin_toggles.insert(plugin_id, enabled);
} else {
local_edits.push(edit);
}
}
if !remote_plugin_toggles.is_empty() && !local_edits.is_empty() {
return Err(invalid_request(
"remote plugin enablement edits cannot be batched with local config edits",
));
}
if remote_plugin_toggles.is_empty() {
return self
.config_api
.batch_write(ConfigBatchWriteParams {
edits: local_edits,
file_path,
expected_version,
reload_user_config,
})
.await;
}
let response = self
.config_api
@@ -1102,7 +1067,7 @@ impl MessageProcessor {
})
.await?;
for (plugin_id, enabled) in remote_plugin_toggles {
for (plugin_id, enabled) in write.toggles {
self.codex_message_processor
.sync_remote_plugin_enabled_config_write(plugin_id, enabled)
.await?;
@@ -1404,7 +1369,52 @@ fn migration_items_need_runtime_refresh(items: &[ExternalAgentConfigMigrationIte
})
}
fn remote_plugin_enabled_config_edit(key_path: &str, value: &JsonValue) -> Option<(String, bool)> {
struct RemotePluginEnabledConfigEdit {
plugin_id: String,
enabled: bool,
}
struct RemotePluginEnabledConfigBatchWrite {
toggles: BTreeMap<String, bool>,
}
fn remote_plugin_enabled_config_value_write(
params: &ConfigValueWriteParams,
) -> Option<RemotePluginEnabledConfigEdit> {
remote_plugin_enabled_config_edit(&params.key_path, &params.value)
}
fn remote_plugin_enabled_config_batch_write(
params: &ConfigBatchWriteParams,
) -> Result<Option<RemotePluginEnabledConfigBatchWrite>, JSONRPCErrorError> {
let mut toggles = BTreeMap::<String, bool>::new();
let mut has_local_edits = false;
for edit in &params.edits {
if let Some(edit) = remote_plugin_enabled_config_edit(&edit.key_path, &edit.value) {
toggles.insert(edit.plugin_id, edit.enabled);
} else {
has_local_edits = true;
}
}
if toggles.is_empty() {
return Ok(None);
}
if has_local_edits {
return Err(invalid_request(
"remote plugin enablement edits cannot be batched with local config edits",
));
}
Ok(Some(RemotePluginEnabledConfigBatchWrite { toggles }))
}
fn remote_plugin_enabled_config_edit(
key_path: &str,
value: &JsonValue,
) -> Option<RemotePluginEnabledConfigEdit> {
let enabled = value.as_bool()?;
let mut segments = key_path.split('.');
let table = segments.next()?;
@@ -1415,7 +1425,10 @@ fn remote_plugin_enabled_config_edit(key_path: &str, value: &JsonValue) -> Optio
&& segments.next().is_none()
&& is_remote_plugin_config_id(plugin_id)
{
return Some((plugin_id.to_string(), enabled));
return Some(RemotePluginEnabledConfigEdit {
plugin_id: plugin_id.to_string(),
enabled,
});
}
None
}
@@ -23,6 +23,7 @@ use codex_app_server_protocol::AppInfo;
use codex_app_server_protocol::AppSummary;
use codex_app_server_protocol::ConfigBatchWriteParams;
use codex_app_server_protocol::ConfigEdit;
use codex_app_server_protocol::ConfigValueWriteParams;
use codex_app_server_protocol::ConfigWriteResponse;
use codex_app_server_protocol::JSONRPCResponse;
use codex_app_server_protocol::MergeStrategy;
@@ -258,6 +259,58 @@ async fn plugin_install_writes_remote_plugin_to_cloud_and_cache() -> Result<()>
Ok(())
}
#[tokio::test]
async fn config_value_write_enables_remote_plugin_via_remote_enable_without_config_write()
-> Result<()> {
let codex_home = TempDir::new()?;
let server = MockServer::start().await;
let installed_path = codex_home
.path()
.join("plugins/cache/chatgpt-global/linear/1.2.3");
std::fs::create_dir_all(installed_path.join(".codex-plugin"))?;
std::fs::write(
installed_path.join(".codex-plugin/plugin.json"),
r#"{"name":"linear"}"#,
)?;
configure_remote_plugin_test(codex_home.path(), &server)?;
mount_remote_plugin_enablement(&server, REMOTE_PLUGIN_ID, /*enabled*/ true).await;
let mut mcp = McpProcess::new(codex_home.path()).await?;
timeout(DEFAULT_TIMEOUT, mcp.initialize()).await??;
let request_id = send_remote_plugin_enabled_config_value_write(
&mut mcp,
REMOTE_PLUGIN_ID,
/*enabled*/ true,
)
.await?;
let response: JSONRPCResponse = timeout(
DEFAULT_TIMEOUT,
mcp.read_stream_until_response_message(RequestId::Integer(request_id)),
)
.await??;
let response: ConfigWriteResponse = to_response(response)?;
assert_eq!(response.status, WriteStatus::Ok);
wait_for_remote_plugin_request_count(
&server,
"POST",
&format!("/backend-api/plugins/{REMOTE_PLUGIN_ID}/enable"),
/*expected_count*/ 1,
)
.await?;
wait_for_remote_plugin_request_count(
&server,
"POST",
&format!("/backend-api/ps/plugins/{REMOTE_PLUGIN_ID}/install"),
/*expected_count*/ 0,
)
.await?;
assert!(installed_path.join(".codex-plugin/plugin.json").is_file());
assert_config_does_not_contain_remote_plugin_id(codex_home.path(), REMOTE_PLUGIN_ID)?;
Ok(())
}
#[tokio::test]
async fn config_batch_write_enables_remote_plugin_via_remote_enable_without_cache_install()
-> Result<()> {
@@ -1459,6 +1512,21 @@ async fn send_remote_plugin_enabled_config_write(
.await
}
async fn send_remote_plugin_enabled_config_value_write(
mcp: &mut McpProcess,
remote_plugin_id: &str,
enabled: bool,
) -> Result<i64> {
mcp.send_config_value_write_request(ConfigValueWriteParams {
key_path: format!("plugins.{remote_plugin_id}.enabled"),
value: json!(enabled),
merge_strategy: MergeStrategy::Upsert,
file_path: None,
expected_version: None,
})
.await
}
fn assert_config_does_not_contain_remote_plugin_id(
codex_home: &std::path::Path,
remote_plugin_id: &str,