mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Record active plugin inventory in turn analytics (#46323)
## What changed Add `active_plugin_ids_at_turn_start` to turn analytics, combining active host plugins with selected plugin packages. Prefer remote plugin IDs when present and otherwise use validated package IDs. Sort and deduplicate the inventory. Report `null` for unknown inventories, invalid IDs, IDs longer than 128 bytes, or inventories exceeding 512 distinct IDs; preserve `[]` for an observed empty inventory. Keep the first received inventory even when later resolved configuration updates arrive. ## Testing Add coverage for ID selection and validation, deduplication, size limits, serialization, and preservation of the first inventory. Extend app-server tests to check inventories after plugin reconciliation and with selected plugins, including turns without explicit plugin mentions. GitOrigin-RevId: 8a0de912e2570a902cd05bfc918dc8e6ff8b8527
This commit is contained in:
1 parent
1f631def3f
commit
a1efb59c4a
12 files changed
+368
-15
No files matched your search
@@ -0,0 +1,174 @@
|
||||
//! Tests source-aware plugin telemetry selection and complete-inventory bounds.
|
||||
|
||||
use crate::session::tests::make_session_and_context;
|
||||
use codex_extension_api::SelectedPluginIdentity;
|
||||
use codex_extension_api::SelectedPluginSnapshot;
|
||||
use codex_utils_plugins::PluginIdentity;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[tokio::test]
|
||||
async fn active_plugin_ids_select_and_validate_by_source() {
|
||||
let (_session, mut turn_context) = make_session_and_context().await;
|
||||
for (name, identities, selected_ids, expected) in [
|
||||
(
|
||||
"remote wins even when the package fallback is invalid",
|
||||
vec![
|
||||
("box@local", Some("plugins~Plugin_box")),
|
||||
("selected-root", Some("plugin_box-1")),
|
||||
],
|
||||
vec![],
|
||||
Some(vec!["plugin_box-1", "plugins~Plugin_box"]),
|
||||
),
|
||||
(
|
||||
"package fallback",
|
||||
vec![("my.tool@123", None)],
|
||||
vec!["executor-demo@1"],
|
||||
Some(vec!["executor-demo@1", "my.tool@123"]),
|
||||
),
|
||||
(
|
||||
"blank present remote",
|
||||
vec![("box@local", Some(""))],
|
||||
vec![],
|
||||
None,
|
||||
),
|
||||
(
|
||||
"remote is not trimmed",
|
||||
vec![("box@local", Some(" plugin_box "))],
|
||||
vec![],
|
||||
None,
|
||||
),
|
||||
(
|
||||
"package key is not a remote ID",
|
||||
vec![("box@local", Some("box@local"))],
|
||||
vec![],
|
||||
None,
|
||||
),
|
||||
(
|
||||
"remote-looking selected root is still a fallback",
|
||||
vec![],
|
||||
vec!["plugins~Plugin_box"],
|
||||
None,
|
||||
),
|
||||
(
|
||||
"one invalid identity makes the inventory unknown",
|
||||
vec![("box@local", Some("plugin_box")), ("bad..name@1", None)],
|
||||
vec![],
|
||||
None,
|
||||
),
|
||||
(
|
||||
"exact deduplication retains aliases and distinct remote objects",
|
||||
vec![
|
||||
("box@local", Some("plugins~Plugin_b")),
|
||||
("box@local", None),
|
||||
("box@local", Some("plugins~Plugin_a")),
|
||||
("box@other", Some("plugins~Plugin_a")),
|
||||
],
|
||||
vec!["box@local", "box@local"],
|
||||
Some(vec!["box@local", "plugins~Plugin_a", "plugins~Plugin_b"]),
|
||||
),
|
||||
("empty observation", vec![], vec![], Some(vec![])),
|
||||
] {
|
||||
let identities = identities
|
||||
.into_iter()
|
||||
.map(|(plugin_id, remote_plugin_id)| PluginIdentity {
|
||||
plugin_id: plugin_id.to_string(),
|
||||
remote_plugin_id: remote_plugin_id.map(str::to_string),
|
||||
})
|
||||
.collect();
|
||||
let selected_plugins = selected_ids
|
||||
.into_iter()
|
||||
.map(|plugin_id| SelectedPluginIdentity {
|
||||
selected_root_id: "capability-root".to_string(),
|
||||
plugin_id: plugin_id.to_string(),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
turn_context.active_host_plugin_identities = Some(identities);
|
||||
turn_context.extension_data.insert(SelectedPluginSnapshot {
|
||||
plugins: selected_plugins,
|
||||
..Default::default()
|
||||
});
|
||||
assert_eq!(
|
||||
turn_context.active_plugin_ids_for_telemetry(),
|
||||
expected.map(|ids| ids.into_iter().map(str::to_string).collect::<Vec<_>>()),
|
||||
"{name}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn active_plugin_ids_enforce_bounds_after_selection_and_deduplication() {
|
||||
let (_session, mut turn_context) = make_session_and_context().await;
|
||||
for length in [128, 129] {
|
||||
for remote in [false, true] {
|
||||
let id = if remote {
|
||||
"r".repeat(length)
|
||||
} else {
|
||||
format!("{}@local", "p".repeat(length - 6))
|
||||
};
|
||||
// A remote ID also wins over an overlong package key.
|
||||
let plugin_id = if remote {
|
||||
format!("{}@local", "p".repeat(/*n*/ 128))
|
||||
} else {
|
||||
id.clone()
|
||||
};
|
||||
let identities = vec![PluginIdentity {
|
||||
plugin_id,
|
||||
remote_plugin_id: remote.then_some(id.clone()),
|
||||
}];
|
||||
turn_context.active_host_plugin_identities = Some(identities);
|
||||
turn_context
|
||||
.extension_data
|
||||
.insert(SelectedPluginSnapshot::default());
|
||||
assert_eq!(
|
||||
turn_context.active_plugin_ids_for_telemetry(),
|
||||
(length == 128).then_some(vec![id]),
|
||||
"length={length}, remote={remote}"
|
||||
);
|
||||
}
|
||||
let id = format!("{}@local", "p".repeat(length - 6));
|
||||
let selected_plugin = SelectedPluginIdentity {
|
||||
selected_root_id: "capability-root".to_string(),
|
||||
plugin_id: id.clone(),
|
||||
};
|
||||
turn_context.active_host_plugin_identities = Some(Vec::new());
|
||||
turn_context.extension_data.insert(SelectedPluginSnapshot {
|
||||
plugins: vec![selected_plugin],
|
||||
..Default::default()
|
||||
});
|
||||
assert_eq!(
|
||||
turn_context.active_plugin_ids_for_telemetry(),
|
||||
(length == 128).then_some(vec![id]),
|
||||
"selected length={length}"
|
||||
);
|
||||
}
|
||||
|
||||
for count in [512, 513] {
|
||||
let ids = (0..count)
|
||||
.map(|index| format!("plugin_{index:03}@local"))
|
||||
.collect::<Vec<_>>();
|
||||
let identities = ids[..count / 2]
|
||||
.iter()
|
||||
.map(|id| PluginIdentity {
|
||||
plugin_id: id.clone(),
|
||||
remote_plugin_id: None,
|
||||
})
|
||||
.collect();
|
||||
let selected_plugins = ids[count / 2 - 1..]
|
||||
.iter()
|
||||
.map(|id| SelectedPluginIdentity {
|
||||
selected_root_id: "capability-root".to_string(),
|
||||
plugin_id: id.clone(),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
turn_context.active_host_plugin_identities = Some(identities);
|
||||
turn_context.extension_data.insert(SelectedPluginSnapshot {
|
||||
plugins: selected_plugins,
|
||||
..Default::default()
|
||||
});
|
||||
assert_eq!(
|
||||
turn_context.active_plugin_ids_for_telemetry(),
|
||||
(count == 512).then_some(ids),
|
||||
"{count} distinct IDs across host and selected inputs, with one overlap"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3743,6 +3743,7 @@ impl Session {
|
||||
.await
|
||||
}
|
||||
|
||||
/// Captures and retains a step with the required MCP startup dependencies.
|
||||
pub(crate) async fn capture_step_context_with_required_mcp_servers(
|
||||
self: &Arc<Self>,
|
||||
turn_context: Arc<TurnContext>,
|
||||
|
||||
@@ -160,6 +160,7 @@ pub(super) async fn spawn_review_thread(
|
||||
auth_manager: auth_manager_for_context,
|
||||
initial_settings: Arc::clone(&step_settings),
|
||||
disabled_plugin_ids: parent_turn_context.disabled_plugin_ids.clone(),
|
||||
active_host_plugin_identities: None,
|
||||
current_settings: ArcSwap::from(step_settings),
|
||||
session_telemetry: session_telemetry_for_context,
|
||||
provider: provider_for_context,
|
||||
|
||||
@@ -1194,6 +1194,7 @@ async fn track_turn_resolved_config_analytics(
|
||||
turn_id: turn_context.sub_id.clone(),
|
||||
thread_id: sess.thread_id.to_string(),
|
||||
turn_metadata: turn_context.turn_metadata_state.clone(),
|
||||
active_plugin_ids_at_turn_start: turn_context.active_plugin_ids_for_telemetry(),
|
||||
num_input_images: input
|
||||
.iter()
|
||||
.filter_map(|item| match item {
|
||||
@@ -1675,7 +1676,6 @@ pub(crate) async fn prepare_tool_recommendations(
|
||||
.services
|
||||
.plugins_manager
|
||||
.plugins_for_config(&turn_context.config.plugins_config_input())
|
||||
.instrument(trace_span!("built_tools.load_plugins"))
|
||||
.await
|
||||
.without_plugins(&turn_context.disabled_plugin_ids);
|
||||
let tool_suggest_is_enabled = tool_suggest_enabled(turn_context);
|
||||
|
||||
@@ -16,6 +16,7 @@ use codex_core_plugins::PluginCommandAttribution;
|
||||
use codex_core_plugins::ResolvedPluginMetricsOperation;
|
||||
use codex_core_plugins::TrustedPluginRoots;
|
||||
use codex_exec_server::ExecutorFileSystem;
|
||||
use codex_extension_api::SelectedPluginSnapshot;
|
||||
use codex_file_system::FileSystemSandboxContext;
|
||||
use codex_model_provider::SharedModelProvider;
|
||||
use codex_protocol::SessionId;
|
||||
@@ -37,6 +38,7 @@ use codex_sandboxing::policy_transforms::effective_permission_profile;
|
||||
use codex_skills_extension::HostSkillsSnapshot;
|
||||
use codex_skills_extension::SkillLoadOutcome;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use codex_utils_plugins::PluginIdentity;
|
||||
use futures::FutureExt;
|
||||
use futures::future::BoxFuture;
|
||||
use futures::future::Shared;
|
||||
@@ -318,6 +320,7 @@ pub struct TurnContext {
|
||||
pub(crate) initial_settings: Arc<ResolvedStepSettings>,
|
||||
/// Thread-owned plugin selection captured when this turn was admitted.
|
||||
pub(crate) disabled_plugin_ids: Vec<String>,
|
||||
pub(super) active_host_plugin_identities: Option<Vec<PluginIdentity>>,
|
||||
/// Snapshot for the next step; request consumers use their captured StepContext.
|
||||
pub(super) current_settings: ArcSwap<ResolvedStepSettings>,
|
||||
/// Turn-wide telemetry; model-attributed step work should use `StepContext::session_telemetry`.
|
||||
@@ -410,6 +413,46 @@ impl TurnContext {
|
||||
self.initial_settings.effective_collaboration_mode()
|
||||
}
|
||||
|
||||
/// Combines setup-time host identities with current ready selected packages.
|
||||
/// Keeps the complete observation or marks it unknown; never truncates membership.
|
||||
pub(super) fn active_plugin_ids_for_telemetry(&self) -> Option<Vec<String>> {
|
||||
const MAX_TELEMETRY_PLUGIN_IDS: usize = 512;
|
||||
const MAX_TELEMETRY_PLUGIN_ID_BYTES: usize = 128;
|
||||
|
||||
let mut identities = self.active_host_plugin_identities.clone()?;
|
||||
// Selected roots provide a package key, not a remote identity for that object.
|
||||
if let Some(selected) = self.extension_data.get::<SelectedPluginSnapshot>() {
|
||||
identities.extend(selected.plugins.iter().map(|plugin| PluginIdentity {
|
||||
plugin_id: plugin.plugin_id.clone(),
|
||||
remote_plugin_id: None,
|
||||
}));
|
||||
}
|
||||
let mut ids = Vec::with_capacity(identities.len());
|
||||
for identity in identities {
|
||||
let id = match identity.remote_plugin_id {
|
||||
Some(remote_id) => {
|
||||
if !codex_core_plugins::remote::is_valid_remote_plugin_id(&remote_id) {
|
||||
return None;
|
||||
}
|
||||
remote_id
|
||||
}
|
||||
None => {
|
||||
if codex_plugin::PluginId::parse(&identity.plugin_id).is_err() {
|
||||
return None;
|
||||
}
|
||||
identity.plugin_id
|
||||
}
|
||||
};
|
||||
if id.len() > MAX_TELEMETRY_PLUGIN_ID_BYTES {
|
||||
return None;
|
||||
}
|
||||
ids.push(id);
|
||||
}
|
||||
ids.sort_unstable();
|
||||
ids.dedup();
|
||||
(ids.len() <= MAX_TELEMETRY_PLUGIN_IDS).then_some(ids)
|
||||
}
|
||||
|
||||
pub(crate) fn plugin_attribution_for_command(
|
||||
&self,
|
||||
command: &[String],
|
||||
@@ -618,6 +661,7 @@ impl TurnContext {
|
||||
auth_manager: self.auth_manager.clone(),
|
||||
initial_settings: Arc::clone(&step_settings),
|
||||
disabled_plugin_ids: self.disabled_plugin_ids.clone(),
|
||||
active_host_plugin_identities: self.active_host_plugin_identities.clone(),
|
||||
current_settings: ArcSwap::from(step_settings),
|
||||
session_telemetry,
|
||||
provider: self.provider.clone(),
|
||||
@@ -933,6 +977,7 @@ impl Session {
|
||||
auth_manager,
|
||||
initial_settings: Arc::clone(&step_settings),
|
||||
disabled_plugin_ids: session_configuration.disabled_plugin_ids.clone(),
|
||||
active_host_plugin_identities: None,
|
||||
current_settings: ArcSwap::from(step_settings),
|
||||
session_telemetry: session_telemetry_for_context,
|
||||
provider,
|
||||
@@ -1174,6 +1219,17 @@ impl Session {
|
||||
);
|
||||
turn_context.code_mode_available = self.services.code_mode_service.is_available();
|
||||
turn_context.extension_data.insert(trusted_plugin_roots);
|
||||
turn_context.active_host_plugin_identities = Some(
|
||||
plugin_outcome
|
||||
.plugins()
|
||||
.iter()
|
||||
.filter(|plugin| plugin.is_active())
|
||||
.map(|plugin| PluginIdentity {
|
||||
plugin_id: plugin.config_name.clone(),
|
||||
remote_plugin_id: plugin.remote_plugin_id.clone(),
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
turn_context.realtime_active = self.conversation.running_state().await.is_some();
|
||||
|
||||
turn_context.final_output_json_schema = options.final_output_json_schema;
|
||||
@@ -1272,3 +1328,7 @@ impl Session {
|
||||
state.session_configuration.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "active_plugin_inventory_tests.rs"]
|
||||
mod tests;
|
||||
Reference in new issue
Block a user