Compare commits

...
3 changed files with 116 additions and 29 deletions

No files matched your search

+8
View File
@@ -6,6 +6,7 @@ use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::AtomicU64;
use std::time::Instant;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;
@@ -473,6 +474,7 @@ impl Codex {
}
async fn spawn_internal(args: CodexSpawnArgs) -> CodexResult<CodexSpawnOk> {
let configuration_resolution_started_at = Instant::now();
let CodexSpawnArgs {
mut config,
installation_id,
@@ -623,6 +625,7 @@ impl Codex {
// Generate a unique ID for the lifetime of this Codex session.
let session_source_clone = session_configuration.session_source.clone();
let (agent_status_tx, agent_status_rx) = watch::channel(AgentStatus::PendingInit);
let configuration_resolution_duration = configuration_resolution_started_at.elapsed();
let session = Box::pin(Session::new(
session_configuration,
@@ -653,6 +656,11 @@ impl Codex {
map_session_init_error(&e, &config.codex_home)
})?;
let thread_id = session.thread_id;
session.services.session_telemetry.record_startup_phase(
"thread_initialization_configuration_resolution",
configuration_resolution_duration,
Some("ready"),
);
// This task will run until Op::Shutdown is received.
let session_for_loop = Arc::clone(&session);
+54 -28
View File
@@ -13,6 +13,7 @@ use codex_protocol::protocol::MultiAgentVersion;
use codex_protocol::protocol::ThreadSource;
use codex_protocol::protocol::TurnEnvironmentSelection;
use std::sync::OnceLock;
use std::time::Instant;
use tokio::sync::Semaphore;
/// Context for an initialized model agent
@@ -529,6 +530,7 @@ impl Session {
// - initialize thread persistence with new or resumed session info
// - perform default shell discovery
// - load history metadata (skipped for subagents)
let dependency_loading_started_at = Instant::now();
let thread_persistence_fut = async {
if config.ephemeral {
Ok::<_, anyhow::Error>(None)
@@ -653,7 +655,35 @@ impl Session {
auth_and_mcp_fut,
plugin_and_skill_warmup_fut
);
let dependency_loading_duration = dependency_loading_started_at.elapsed();
let session_construction_started_at = Instant::now();
let auth = auth.as_ref();
let auth_mode = auth.map(CodexAuth::auth_mode).map(TelemetryAuthMode::from);
let account_id = auth.and_then(CodexAuth::get_account_id);
let account_email = auth.and_then(CodexAuth::get_account_email);
let originator = originator().value;
let terminal_type = user_agent();
let session_model = session_configuration.collaboration_mode.model().to_string();
let auth_env_telemetry = collect_auth_env_telemetry(
&session_configuration.provider,
auth_manager.codex_api_key_env_enabled(),
);
let mut session_telemetry = SessionTelemetry::new(
thread_id,
session_model.as_str(),
session_model.as_str(),
account_id.clone(),
account_email.clone(),
auth_mode,
originator.clone(),
config.otel.log_user_prompt,
terminal_type.clone(),
session_configuration.session_source.clone(),
)
.with_auth_env(auth_env_telemetry.to_otel_metadata());
if let Some(service_name) = session_configuration.metrics_service_name.as_deref() {
session_telemetry = session_telemetry.with_metrics_service_name(service_name);
}
for err in &plugin_skill_errors {
error!(
"failed to load skill {}: {}",
@@ -667,6 +697,11 @@ impl Session {
error!("failed to initialize thread persistence: {e:#}");
e
})?);
session_telemetry.record_startup_phase(
"thread_initialization_session_dependency_loading",
dependency_loading_duration,
Some("ready"),
);
let session_result: anyhow::Result<Arc<Self>> = async {
let rollout_path = if let Some(live_thread) = live_thread_init.as_ref() {
live_thread.local_rollout_path().await?
@@ -746,33 +781,6 @@ impl Session {
});
}
let auth = auth.as_ref();
let auth_mode = auth.map(CodexAuth::auth_mode).map(TelemetryAuthMode::from);
let account_id = auth.and_then(CodexAuth::get_account_id);
let account_email = auth.and_then(CodexAuth::get_account_email);
let originator = originator().value;
let terminal_type = user_agent();
let session_model = session_configuration.collaboration_mode.model().to_string();
let auth_env_telemetry = collect_auth_env_telemetry(
&session_configuration.provider,
auth_manager.codex_api_key_env_enabled(),
);
let mut session_telemetry = SessionTelemetry::new(
thread_id,
session_model.as_str(),
session_model.as_str(),
account_id.clone(),
account_email.clone(),
auth_mode,
originator.clone(),
config.otel.log_user_prompt,
terminal_type.clone(),
session_configuration.session_source.clone(),
)
.with_auth_env(auth_env_telemetry.to_otel_metadata());
if let Some(service_name) = session_configuration.metrics_service_name.as_deref() {
session_telemetry = session_telemetry.with_metrics_service_name(service_name);
}
let network_proxy_audit_metadata = NetworkProxyAuditMetadata {
conversation_id: Some(thread_id.to_string()),
app_version: Some(env!("CARGO_PKG_VERSION").to_string()),
@@ -1107,6 +1115,12 @@ impl Session {
sess.send_event_raw(event).await;
}
sess.services.session_telemetry.record_startup_phase(
"thread_initialization_session_construction",
session_construction_started_at.elapsed(),
Some("ready"),
);
let mcp_startup_started_at = Instant::now();
let mut required_mcp_servers: Vec<String> = mcp_servers
.iter()
.filter(|(_, server)| server.enabled() && server.required())
@@ -1213,6 +1227,13 @@ impl Session {
anyhow::bail!("required MCP servers failed to initialize: {details}");
}
}
sess.services.session_telemetry.record_startup_phase(
"thread_initialization_mcp_startup",
mcp_startup_started_at.elapsed(),
Some("ready"),
);
let session_activation_started_at = Instant::now();
sess.schedule_startup_prewarm(session_configuration.base_instructions.clone())
.await;
let session_start_source = match &initial_history {
@@ -1229,6 +1250,11 @@ impl Session {
let mut state = sess.state.lock().await;
state.queue_pending_session_start_source(session_start_source);
}
sess.services.session_telemetry.record_startup_phase(
"thread_initialization_session_activation",
session_activation_started_at.elapsed(),
Some("ready"),
);
Ok(sess)
}
+54 -1
View File
@@ -73,11 +73,15 @@ use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::time::Duration;
use std::time::Instant;
use tokio::sync::RwLock;
use tokio::sync::broadcast;
use tracing::warn;
const THREAD_CREATED_CHANNEL_CAPACITY: usize = 1024;
const THREAD_INITIALIZATION_EXISTING_THREAD_LOOKUP_PHASE: &str =
"thread_initialization_existing_thread_lookup";
const THREAD_INITIALIZATION_THREAD_SPAWN_PHASE: &str = "thread_initialization_thread_spawn";
/// Test-only override for enabling thread-manager behaviors used by integration
/// tests.
///
@@ -1270,8 +1274,11 @@ impl ThreadManagerState {
environments: Vec<TurnEnvironmentSelection>,
user_shell_override: Option<crate::shell::Shell>,
) -> CodexResult<NewThread> {
let thread_spawn_started_at = Instant::now();
let is_resumed_thread = matches!(&initial_history, InitialHistory::Resumed(_));
let mut existing_thread_lookup_duration = None;
if let InitialHistory::Resumed(resumed) = &initial_history {
let existing_thread_lookup_started_at = Instant::now();
let mut threads = self.threads.write().await;
if let Some(thread) = threads.get(&resumed.conversation_id).cloned() {
if thread.is_running() {
@@ -1283,15 +1290,33 @@ impl ThreadManagerState {
resumed.conversation_id
)));
}
let session_configured = thread.session_configured();
drop(threads);
let existing_thread_lookup_duration =
existing_thread_lookup_started_at.elapsed();
let thread_spawn_duration = thread_spawn_started_at.elapsed();
let session_telemetry = thread.session_telemetry();
session_telemetry.record_startup_phase(
THREAD_INITIALIZATION_EXISTING_THREAD_LOOKUP_PHASE,
existing_thread_lookup_duration,
Some("loaded"),
);
session_telemetry.record_startup_phase(
THREAD_INITIALIZATION_THREAD_SPAWN_PHASE,
thread_spawn_duration,
Some("ready"),
);
return Ok(NewThread {
thread_id: resumed.conversation_id,
session_configured: thread.session_configured(),
session_configured,
thread,
});
}
threads.remove(&resumed.conversation_id);
}
existing_thread_lookup_duration = Some(existing_thread_lookup_started_at.elapsed());
}
let thread_preparation_started_at = Instant::now();
let environment_selections =
resolve_environment_selections(self.environment_manager.as_ref(), &environments)?;
let parent_rollout_thread_trace = self
@@ -1306,6 +1331,7 @@ impl ThreadManagerState {
forked_from_thread_id,
)
.await;
let thread_preparation_duration = thread_preparation_started_at.elapsed();
let CodexSpawnOk {
codex, thread_id, ..
} = Box::pin(Codex::spawn(CodexSpawnArgs {
@@ -1338,12 +1364,39 @@ impl ThreadManagerState {
inherited_multi_agent_version: multi_agent_version,
}))
.await?;
let session_telemetry = codex.session.services.session_telemetry.clone();
if let Some(existing_thread_lookup_duration) = existing_thread_lookup_duration {
session_telemetry.record_startup_phase(
THREAD_INITIALIZATION_EXISTING_THREAD_LOOKUP_PHASE,
existing_thread_lookup_duration,
Some("cold"),
);
}
session_telemetry.record_startup_phase(
"thread_initialization_thread_preparation",
thread_preparation_duration,
Some("ready"),
);
let registration_started_at = Instant::now();
let new_thread = self
.finalize_thread_spawn(codex, thread_id, tracked_session_source)
.await?;
if is_resumed_thread {
new_thread.thread.emit_thread_resume_lifecycle().await;
}
let registration_duration = registration_started_at.elapsed();
let thread_spawn_duration = thread_spawn_started_at.elapsed();
session_telemetry.record_startup_phase(
"thread_initialization_thread_registration",
registration_duration,
Some("ready"),
);
session_telemetry.record_startup_phase(
THREAD_INITIALIZATION_THREAD_SPAWN_PHASE,
thread_spawn_duration,
Some("ready"),
);
Ok(new_thread)
}