mirror of
https://github.com/openai/codex.git
synced 2026-11-22 23:00:42 +00:00
Add metrics for extension-rendered skill catalogs (#35597)
## What changed - Add an optional `ExtensionMetrics` capability to thread, turn-input, and world-state contributors, backed by session telemetry in the host. - Record skill counts, omissions, and truncated description characters for extension-rendered catalogs, tagged by the catalog surface. - Preserve host session attribution on extension metrics, including the effective model for each turn, and prevent extension tags from overriding host metadata. - Avoid duplicate host-catalog samples by recording world-state metrics only when the section is published or changes. ## Testing - Cover metric values and surface tags, session metadata preservation, turn-level model changes, and host world-state publication behavior. GitOrigin-RevId: 9059e521943a8f81df25a38b03692eddbc1d990d
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use codex_extension_api::ExtensionMetrics;
|
||||
use codex_otel::SessionTelemetry;
|
||||
|
||||
struct SessionTelemetryExtensionMetrics {
|
||||
session_telemetry: SessionTelemetry,
|
||||
}
|
||||
|
||||
impl ExtensionMetrics for SessionTelemetryExtensionMetrics {
|
||||
fn histogram(&self, name: &str, value: i64, tags: &[(&str, &str)]) {
|
||||
self.session_telemetry.histogram(name, value, tags);
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn from_session_telemetry(
|
||||
session_telemetry: SessionTelemetry,
|
||||
) -> Arc<dyn ExtensionMetrics> {
|
||||
Arc::new(SessionTelemetryExtensionMetrics { session_telemetry })
|
||||
}
|
||||
@@ -206,6 +206,7 @@ use codex_protocol::exec_output::StreamOutput;
|
||||
mod code_mode_warning;
|
||||
mod config_lock;
|
||||
pub(crate) mod context_window;
|
||||
mod extension_metrics;
|
||||
mod handlers;
|
||||
mod inject;
|
||||
mod input_queue;
|
||||
@@ -3352,12 +3353,17 @@ impl Session {
|
||||
.extension_data
|
||||
.get::<HostSkillsCatalogInWorldState>()
|
||||
.is_some();
|
||||
let side_effects = if host_catalog_in_world_state {
|
||||
SkillRenderSideEffects::None
|
||||
} else {
|
||||
SkillRenderSideEffects::ThreadStart {
|
||||
session_telemetry: &self.services.session_telemetry,
|
||||
}
|
||||
};
|
||||
let available_skills = build_available_skills(
|
||||
turn_context.turn_skills.snapshot.outcome(),
|
||||
default_skill_metadata_budget(turn_context.model_info.context_window),
|
||||
SkillRenderSideEffects::ThreadStart {
|
||||
session_telemetry: &self.services.session_telemetry,
|
||||
},
|
||||
side_effects,
|
||||
);
|
||||
if let Some(available_skills) = available_skills {
|
||||
let warning_message = available_skills.warning_message.clone();
|
||||
|
||||
@@ -1065,6 +1065,8 @@ impl Session {
|
||||
let session_extension_data =
|
||||
codex_extension_api::ExtensionData::new(session_id.to_string());
|
||||
let mcp_resource_client = Arc::new(McpResourceClient::new(Arc::clone(&mcp_runtime)));
|
||||
let extension_metrics =
|
||||
extension_metrics::from_session_telemetry(session_telemetry.clone());
|
||||
for contributor in extensions.thread_lifecycle_contributors() {
|
||||
contributor.on_thread_start(codex_extension_api::ThreadStartInput {
|
||||
config: config.as_ref(),
|
||||
@@ -1072,6 +1074,7 @@ impl Session {
|
||||
persistent_thread_state_available: state_db_ctx.is_some(),
|
||||
environments: session_configuration.environment_selections(),
|
||||
mcp_resource_client: Some(Arc::clone(&mcp_resource_client)),
|
||||
extension_metrics: Some(Arc::clone(&extension_metrics)),
|
||||
session_store: &session_extension_data,
|
||||
thread_store: &thread_extension_data,
|
||||
}).await;
|
||||
|
||||
@@ -354,6 +354,155 @@ fn histogram_sum(resource_metrics: &ResourceMetrics, name: &str) -> u64 {
|
||||
}
|
||||
}
|
||||
|
||||
fn single_histogram_attributes(
|
||||
resource_metrics: &ResourceMetrics,
|
||||
name: &str,
|
||||
) -> BTreeMap<String, String> {
|
||||
let metric = find_metric(resource_metrics, name);
|
||||
let AggregatedMetrics::F64(data) = metric.data() else {
|
||||
panic!("expected floating-point histogram");
|
||||
};
|
||||
let MetricData::Histogram(histogram) = data else {
|
||||
panic!("expected histogram");
|
||||
};
|
||||
let points = histogram.data_points().collect::<Vec<_>>();
|
||||
assert_eq!(points.len(), 1);
|
||||
points[0]
|
||||
.attributes()
|
||||
.map(|attribute| {
|
||||
(
|
||||
attribute.key.as_str().to_string(),
|
||||
attribute.value.as_str().to_string(),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extension_metrics_preserve_session_metadata_tags() {
|
||||
let metrics = MetricsClient::new(
|
||||
MetricsConfig::in_memory(
|
||||
"test",
|
||||
"codex-core",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
InMemoryMetricExporter::default(),
|
||||
)
|
||||
.with_runtime_reader(),
|
||||
)
|
||||
.expect("in-memory metrics client");
|
||||
let session_telemetry = SessionTelemetry::new(
|
||||
ThreadId::new(),
|
||||
"gpt-5.4",
|
||||
"gpt-5.4",
|
||||
/*account_id*/ None,
|
||||
/*account_email*/ None,
|
||||
Some(TelemetryAuthMode::Chatgpt),
|
||||
"test_originator".to_string(),
|
||||
/*log_user_prompts*/ false,
|
||||
"tty".to_string(),
|
||||
SessionSource::Cli,
|
||||
)
|
||||
.with_metrics_service_name("test_service")
|
||||
.with_metrics(metrics.clone());
|
||||
let extension_metrics = super::extension_metrics::from_session_telemetry(session_telemetry);
|
||||
|
||||
extension_metrics.histogram(
|
||||
"codex.test.extension",
|
||||
/*value*/ 7,
|
||||
&[
|
||||
("component", "skills"),
|
||||
("app.version", "extension-version"),
|
||||
("auth_mode", "extension-auth"),
|
||||
("model", "extension-model"),
|
||||
("originator", "extension-originator"),
|
||||
("service_name", "extension-service"),
|
||||
("session_source", "extension-source"),
|
||||
],
|
||||
);
|
||||
|
||||
let snapshot = metrics.snapshot().expect("metrics snapshot");
|
||||
let attributes = single_histogram_attributes(&snapshot, "codex.test.extension");
|
||||
assert_eq!(
|
||||
attributes,
|
||||
BTreeMap::from([
|
||||
(
|
||||
"app.version".to_string(),
|
||||
env!("CARGO_PKG_VERSION").to_string(),
|
||||
),
|
||||
(
|
||||
"auth_mode".to_string(),
|
||||
TelemetryAuthMode::Chatgpt.to_string(),
|
||||
),
|
||||
("component".to_string(), "skills".to_string()),
|
||||
("model".to_string(), "gpt-5.4".to_string()),
|
||||
("originator".to_string(), "test_originator".to_string()),
|
||||
("service_name".to_string(), "test_service".to_string()),
|
||||
("session_source".to_string(), "cli".to_string()),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn world_state_extension_metrics_follow_turn_model_switch() {
|
||||
struct WorldStateMetricsRecorder;
|
||||
|
||||
impl codex_extension_api::ContextContributor for WorldStateMetricsRecorder {
|
||||
fn contribute_world_state<'a>(
|
||||
&'a self,
|
||||
input: codex_extension_api::WorldStateContributionInput<'a>,
|
||||
) -> codex_extension_api::ExtensionFuture<
|
||||
'a,
|
||||
Vec<codex_extension_api::WorldStateSectionContribution>,
|
||||
> {
|
||||
Box::pin(async move {
|
||||
input
|
||||
.extension_metrics
|
||||
.expect("turn metrics should be available")
|
||||
.histogram("codex.test.extension.turn", /*value*/ 1, &[]);
|
||||
Vec::new()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let metrics = MetricsClient::new(
|
||||
MetricsConfig::in_memory(
|
||||
"test",
|
||||
"codex-core",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
InMemoryMetricExporter::default(),
|
||||
)
|
||||
.with_runtime_reader(),
|
||||
)
|
||||
.expect("in-memory metrics client");
|
||||
let (mut session, mut turn_context) = make_session_and_context().await;
|
||||
turn_context.session_telemetry = turn_context
|
||||
.session_telemetry
|
||||
.clone()
|
||||
.with_metrics(metrics.clone());
|
||||
let next_model = if turn_context.model_info.slug == "gpt-5.4" {
|
||||
"gpt-5.2"
|
||||
} else {
|
||||
"gpt-5.4"
|
||||
};
|
||||
let turn_context = Arc::new(
|
||||
turn_context
|
||||
.with_model(next_model.to_string(), &session.services.models_manager)
|
||||
.await,
|
||||
);
|
||||
let mut builder = codex_extension_api::ExtensionRegistryBuilder::<crate::config::Config>::new();
|
||||
builder.prompt_contributor(Arc::new(WorldStateMetricsRecorder));
|
||||
session.services.extensions = Arc::new(builder.build());
|
||||
|
||||
let _world_state = build_world_state_from_turn_context(&session, &turn_context).await;
|
||||
|
||||
let snapshot = metrics.snapshot().expect("metrics snapshot");
|
||||
let attributes = single_histogram_attributes(&snapshot, "codex.test.extension.turn");
|
||||
assert_eq!(
|
||||
attributes.get("model").map(String::as_str),
|
||||
Some(next_model)
|
||||
);
|
||||
}
|
||||
|
||||
fn skill_message(text: &str) -> ResponseItem {
|
||||
ResponseItem::Message {
|
||||
id: None,
|
||||
|
||||
@@ -760,12 +760,15 @@ async fn build_extension_turn_input_items(
|
||||
user_input: user_input.to_vec(),
|
||||
environments,
|
||||
};
|
||||
let extension_metrics =
|
||||
super::extension_metrics::from_session_telemetry(turn_context.session_telemetry.clone());
|
||||
|
||||
let mut items = Vec::new();
|
||||
for contributor in contributors {
|
||||
let contributed_fragments = contributor
|
||||
.contribute(
|
||||
input.clone(),
|
||||
Some(Arc::clone(&extension_metrics)),
|
||||
&sess.services.session_extension_data,
|
||||
&sess.services.thread_extension_data,
|
||||
turn_context.extension_data.as_ref(),
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::session::Session;
|
||||
use super::step_context::StepContext;
|
||||
use crate::connectors;
|
||||
@@ -165,6 +167,9 @@ impl Session {
|
||||
.iter()
|
||||
.map(|root| root.selected_root().clone())
|
||||
.collect::<Vec<_>>();
|
||||
let extension_metrics = super::extension_metrics::from_session_telemetry(
|
||||
turn_context.session_telemetry.clone(),
|
||||
);
|
||||
for contributor in self.services.extensions.context_contributors() {
|
||||
for section in contributor
|
||||
.contribute_world_state(WorldStateContributionInput {
|
||||
@@ -175,6 +180,7 @@ impl Session {
|
||||
executor_capability_discovery: step_context
|
||||
.executor_capability_discovery
|
||||
.as_deref(),
|
||||
extension_metrics: Some(Arc::clone(&extension_metrics)),
|
||||
session_store: &self.services.session_extension_data,
|
||||
thread_store: &self.services.thread_extension_data,
|
||||
turn_store: turn_context.extension_data.as_ref(),
|
||||
|
||||
Reference in New Issue
Block a user