mirror of
https://github.com/openai/codex.git
synced 2026-11-23 16:12:00 +08:00
Tag goal creation metrics with reasoning effort (#63281)
## Why The `codex.goal.created` counter cannot distinguish goals created at different reasoning efforts. A tool call can also run at a different effort from the thread's default after a turn settings update. ## What changed Add a `reasoning_effort` attribute to `codex.goal.created`. For goals created by `create_goal`, read the effort from the invoking step's metadata. For goals created outside a turn, use the thread's configured effort or fall back to the model default. Label custom values `custom` and unavailable values `unknown`. ## Testing Extend the thread resume integration test to check that the exported metric records `medium` for a goal set before the first turn and `high` for a goal created by a tool after a turn settings update. GitOrigin-RevId: 83b3c8c1073551b67453314eb559c5e648af3521
This commit is contained in:
1 parent
69a470755d
commit
a1604a8c05
4 files changed
+150
-10
No files matched your search
@@ -13,6 +13,7 @@ use app_test_support::create_final_assistant_message_sse_response;
|
||||
use app_test_support::create_mock_responses_server_repeating_assistant;
|
||||
use app_test_support::create_mock_responses_server_sequence;
|
||||
use app_test_support::create_mock_responses_server_sequence_unchecked;
|
||||
use app_test_support::create_request_user_input_sse_response;
|
||||
use app_test_support::rollout_path;
|
||||
use app_test_support::test_absolute_path;
|
||||
use app_test_support::to_response;
|
||||
@@ -82,6 +83,7 @@ use codex_app_server_protocol::TurnStatus;
|
||||
use codex_app_server_protocol::UserInput;
|
||||
use codex_config::types::AuthCredentialsStoreMode;
|
||||
use codex_core::ARCHIVED_SESSIONS_SUBDIR;
|
||||
use codex_core::test_support::all_model_presets;
|
||||
use codex_exec_server::LOCAL_ENVIRONMENT_ID;
|
||||
use codex_features::Feature;
|
||||
use codex_login::REFRESH_TOKEN_URL_OVERRIDE_ENV_VAR;
|
||||
@@ -3397,6 +3399,7 @@ async fn thread_resume_rejects_archived_session_by_id() -> Result<()> {
|
||||
#[tokio::test]
|
||||
async fn thread_resume_keeps_tool_paused_goal_paused() -> Result<()> {
|
||||
let server = create_mock_responses_server_sequence(vec![
|
||||
create_request_user_input_sse_response("confirm-goal")?,
|
||||
responses::sse(vec![
|
||||
responses::ev_response_created("create-goal"),
|
||||
responses::ev_function_call(
|
||||
@@ -3417,7 +3420,10 @@ async fn thread_resume_keeps_tool_paused_goal_paused() -> Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
mock_responses_config(&server.uri())
|
||||
.enable_feature(Feature::Goals)
|
||||
.enable_feature(Feature::DefaultModeRequestUserInput)
|
||||
.enable_feature(Feature::StepModelSwitching)
|
||||
.write(codex_home.path())?;
|
||||
let collector = super::auth_storage_originator::configure_collector(codex_home.path()).await?;
|
||||
|
||||
let mut mcp = TestAppServer::builder()
|
||||
.with_codex_home(codex_home.path())
|
||||
@@ -3425,19 +3431,39 @@ async fn thread_resume_keeps_tool_paused_goal_paused() -> Result<()> {
|
||||
.build_initialized()
|
||||
.await?;
|
||||
|
||||
let model = all_model_presets()
|
||||
.iter()
|
||||
.find(|model| {
|
||||
model.show_in_picker
|
||||
&& model.default_reasoning_effort == ReasoningEffort::Medium
|
||||
&& model
|
||||
.supported_reasoning_efforts
|
||||
.iter()
|
||||
.any(|level| level.effort == ReasoningEffort::High)
|
||||
})
|
||||
.expect("a bundled model defaults to medium and supports high effort");
|
||||
let start_id = mcp
|
||||
.send_thread_start_request_with_auto_env(ThreadStartParams {
|
||||
model: Some("gpt-5.2-codex".to_string()),
|
||||
model: Some(model.id.clone()),
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
let ThreadStartResponse { thread, .. } =
|
||||
timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(start_id)).await??;
|
||||
|
||||
let turn_id = mcp
|
||||
// Before the first turn, /goal inherits the model default.
|
||||
let set_id = mcp
|
||||
.send_raw_request(
|
||||
"thread/goal/set",
|
||||
Some(json!({"threadId": thread.id, "objective": "initial goal", "status": "complete"})),
|
||||
)
|
||||
.await?;
|
||||
let _: ThreadGoalSetResponse =
|
||||
timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(set_id)).await??;
|
||||
|
||||
let start_id = mcp
|
||||
.send_turn_start_request(TurnStartParams {
|
||||
thread_id: thread.id.clone(),
|
||||
client_user_message_id: None,
|
||||
input: vec![UserInput::Text {
|
||||
text: "Create a goal to keep polishing, then pause it.".to_string(),
|
||||
text_elements: Vec::new(),
|
||||
@@ -3445,11 +3471,32 @@ async fn thread_resume_keeps_tool_paused_goal_paused() -> Result<()> {
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
let _turn_resp: JSONRPCResponse = timeout(
|
||||
let TurnStartResponse { turn } =
|
||||
timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(start_id)).await??;
|
||||
let request = timeout(
|
||||
DEFAULT_READ_TIMEOUT,
|
||||
mcp.read_stream_until_response_message(RequestId::Integer(turn_id)),
|
||||
mcp.read_stream_until_request_message(),
|
||||
)
|
||||
.await??;
|
||||
let ServerRequest::ToolRequestUserInput { request_id, .. } = request else {
|
||||
anyhow::bail!("expected request_user_input, received {request:?}");
|
||||
};
|
||||
// Only the active turn changes. The next step calls create_goal at high
|
||||
// while the thread still defaults to medium.
|
||||
let update_id = mcp
|
||||
.send_raw_request(
|
||||
"turn/settings/update",
|
||||
Some(json!({"threadId": thread.id, "turnId": turn.id, "effort": "high"})),
|
||||
)
|
||||
.await?;
|
||||
let outcome: serde_json::Value =
|
||||
timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(update_id)).await??;
|
||||
assert_eq!(outcome, json!({"status": "applied"}));
|
||||
mcp.send_response(
|
||||
request_id,
|
||||
json!({"answers": {"confirm_path": {"answers": ["Yes (Recommended)"]}}}),
|
||||
)
|
||||
.await?;
|
||||
timeout(
|
||||
DEFAULT_READ_TIMEOUT,
|
||||
mcp.read_stream_until_notification_message("turn/completed"),
|
||||
@@ -3492,6 +3539,40 @@ async fn thread_resume_keeps_tool_paused_goal_paused() -> Result<()> {
|
||||
"paused goal should not continue after thread resume"
|
||||
);
|
||||
|
||||
assert!(
|
||||
timeout(DEFAULT_READ_TIMEOUT, mcp.shutdown_gracefully())
|
||||
.await??
|
||||
.success()
|
||||
);
|
||||
let mut efforts = Vec::new();
|
||||
for request in collector.received_requests().await.expect("OTLP requests") {
|
||||
let payload: serde_json::Value = request.body_json()?;
|
||||
let points = payload["resourceMetrics"]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.flat_map(|resource| resource["scopeMetrics"].as_array().into_iter().flatten())
|
||||
.flat_map(|scope| scope["metrics"].as_array().into_iter().flatten())
|
||||
.filter(|metric| metric["name"] == "codex.goal.created")
|
||||
.flat_map(|metric| metric["sum"]["dataPoints"].as_array().into_iter().flatten());
|
||||
for point in points {
|
||||
let effort = point["attributes"]
|
||||
.as_array()
|
||||
.expect("attributes")
|
||||
.iter()
|
||||
.find(|tag| tag["key"] == "reasoning_effort")
|
||||
.expect("goal creation should include reasoning effort");
|
||||
efforts.push(
|
||||
effort["value"]["stringValue"]
|
||||
.as_str()
|
||||
.expect("effort")
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
efforts.sort();
|
||||
assert_eq!(efforts, ["high", "medium"]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
use std::sync::Weak;
|
||||
|
||||
use codex_core::ThreadManager;
|
||||
use codex_otel::GOAL_BLOCKED_METRIC;
|
||||
use codex_otel::GOAL_BUDGET_LIMITED_METRIC;
|
||||
use codex_otel::GOAL_COMPLETED_METRIC;
|
||||
@@ -7,6 +10,14 @@ use codex_otel::GOAL_RESUMED_METRIC;
|
||||
use codex_otel::GOAL_TOKEN_COUNT_METRIC;
|
||||
use codex_otel::GOAL_USAGE_LIMITED_METRIC;
|
||||
use codex_otel::MetricsClient;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::openai_models::ReasoningEffort;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ToolMetadata {
|
||||
reasoning_effort: Option<ReasoningEffort>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub(crate) struct GoalMetrics {
|
||||
@@ -18,11 +29,55 @@ impl GoalMetrics {
|
||||
Self { metrics_client }
|
||||
}
|
||||
|
||||
pub(crate) fn record_created(&self) {
|
||||
let Some(metrics_client) = self.metrics_client.as_ref() else {
|
||||
pub(crate) fn record_tool_created(&self, metadata: Option<&str>) {
|
||||
let Some(client) = &self.metrics_client else {
|
||||
return;
|
||||
};
|
||||
let _ = metrics_client.counter(GOAL_CREATED_METRIC, /*inc*/ 1, &[]);
|
||||
// The invoking step's settings can differ from the thread's current selection.
|
||||
let effort = metadata
|
||||
.and_then(|metadata| serde_json::from_str::<ToolMetadata>(metadata).ok())
|
||||
.and_then(|metadata| metadata.reasoning_effort);
|
||||
Self::record_created(client, effort);
|
||||
}
|
||||
|
||||
pub(crate) async fn record_external_created(
|
||||
&self,
|
||||
manager: &Weak<ThreadManager>,
|
||||
thread_id: ThreadId,
|
||||
) {
|
||||
let Some(client) = &self.metrics_client else {
|
||||
return;
|
||||
};
|
||||
// A goal can be the first action on a thread, before any turn metadata exists.
|
||||
let effort = if let Some(manager) = manager.upgrade()
|
||||
&& let Ok(thread) = manager.get_thread(thread_id).await
|
||||
{
|
||||
let settings = thread.thread_settings_snapshot().await;
|
||||
match settings.reasoning_effort {
|
||||
Some(effort) => Some(effort),
|
||||
None => thread
|
||||
.model_info(&settings.model, &Default::default())
|
||||
.await
|
||||
.default_reasoning_level
|
||||
.clone(),
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Self::record_created(client, effort);
|
||||
}
|
||||
|
||||
fn record_created(client: &MetricsClient, effort: Option<ReasoningEffort>) {
|
||||
let label = match &effort {
|
||||
Some(ReasoningEffort::Custom(_)) => "custom",
|
||||
Some(effort) => effort.as_str(),
|
||||
None => "unknown",
|
||||
};
|
||||
let _ = client.counter(
|
||||
GOAL_CREATED_METRIC,
|
||||
/*inc*/ 1,
|
||||
&[("reasoning_effort", label)],
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn record_resumed(&self) {
|
||||
|
||||
@@ -200,7 +200,10 @@ impl GoalRuntimeHandle {
|
||||
.as_ref()
|
||||
.is_some_and(|previous_goal| previous_goal.goal_id != goal.goal_id);
|
||||
if previous_goal.is_none() || replaced_existing_goal {
|
||||
self.inner.metrics.record_created();
|
||||
self.inner
|
||||
.metrics
|
||||
.record_external_created(&self.inner.thread_manager, self.inner.thread_id)
|
||||
.await;
|
||||
self.inner
|
||||
.analytics
|
||||
.created(&goal, GoalEventAttribution::NoTurn);
|
||||
|
||||
@@ -252,7 +252,8 @@ impl GoalToolExecutor {
|
||||
)
|
||||
.await;
|
||||
}
|
||||
self.metrics.record_created();
|
||||
self.metrics
|
||||
.record_tool_created(invocation.codex_turn_metadata.as_deref());
|
||||
self.analytics.created(
|
||||
&goal,
|
||||
GoalEventAttribution::Turn(invocation.turn_id.as_str()),
|
||||
|
||||
Reference in new issue
Block a user