codex: simplify thread read size metrics

This commit is contained in:
Tom Wiltzius
2026-06-24 20:05:25 -07:00
parent 7981353f76
commit 7b83805eab
10 changed files with 231 additions and 648 deletions
+1
View File
@@ -110,6 +110,7 @@ mod request_processors;
mod request_serialization;
mod server_request_error;
mod skills_watcher;
mod thread_read_metrics;
mod thread_state;
mod thread_status;
mod transport;
@@ -429,8 +429,6 @@ use codex_protocol::protocol::W3cTraceContext;
use codex_protocol::user_input::MAX_USER_INPUT_TEXT_CHARS;
use codex_protocol::user_input::UserInput as CoreInputItem;
use codex_rmcp_client::perform_oauth_login_return_url;
use codex_rollout::RolloutProjectionMeasurement;
use codex_rollout::RolloutProjectionTelemetry;
use codex_rollout::is_persisted_rollout_item;
use codex_rollout::state_db::StateDbHandle;
use codex_rollout::state_db::reconcile_rollout;
@@ -621,43 +619,3 @@ pub(crate) fn build_api_turns_from_rollout_items(items: &[RolloutItem]) -> Vec<T
}
builder.finish()
}
fn prepare_rollout_projection_measurement(
thread_id: ThreadId,
turns: &[Turn],
) -> Option<RolloutProjectionMeasurement> {
let item_count = turns
.iter()
.map(|turn| turn.items.len() as u64)
.sum::<u64>();
RolloutProjectionTelemetry::new(thread_id).prepare_response_measurement(
turns.len() as u64,
item_count,
turns
.iter()
.filter(|turn| is_completed_user_assistant_turn(turn))
.map(|turn| turn.items.len() as u64)
.collect(),
)
}
fn is_completed_user_assistant_turn(turn: &Turn) -> bool {
turn.status == TurnStatus::Completed
&& turn
.items
.iter()
.any(|item| matches!(item, ThreadItem::UserMessage { .. }))
&& turn.items.iter().any(|item| {
matches!(
item,
ThreadItem::AgentMessage {
phase: None | Some(codex_protocol::models::MessagePhase::FinalAnswer),
..
}
)
})
}
#[cfg(test)]
#[path = "request_processors_projection_metrics_tests.rs"]
mod projection_metrics_tests;
@@ -1,5 +1,6 @@
use super::*;
use crate::error_code::method_not_found;
use crate::thread_read_metrics::ThreadReadMeasurement;
use codex_app_server_protocol::SelectedCapabilityRoot;
use codex_extension_api::ExtensionDataInit;
use codex_protocol::config_types::MultiAgentMode;
@@ -19,11 +20,6 @@ struct ThreadListFilters {
parent_thread_id: Option<ThreadId>,
}
struct ThreadReadView {
thread: Thread,
projection_measurement: Option<RolloutProjectionMeasurement>,
}
fn collect_resume_override_mismatches(
request: &ThreadResumeParams,
config_snapshot: &ThreadConfigSnapshot,
@@ -675,27 +671,22 @@ impl ThreadRequestProcessor {
request_id: &ConnectionRequestId,
params: ThreadReadParams,
) -> Result<Option<ClientResponsePayload>, JSONRPCErrorError> {
let view = self.thread_read_response_inner(params).await?;
let response = ThreadReadResponse {
thread: view.thread,
let response = self.thread_read_response_inner(params).await?;
let Some(measurement) = ThreadReadMeasurement::prepare(&response.thread) else {
return Ok(Some(response.into()));
};
if let Some(measurement) = view.projection_measurement {
let write_complete_rx = self
.outgoing
.send_response_with_write_complete(request_id.clone(), response)
.await;
tokio::spawn(async move {
if let Ok(write_complete) = write_complete_rx.await
&& let Some(serialized_bytes) = write_complete.serialized_bytes
{
measurement.record_serialized_response(serialized_bytes);
}
});
} else {
self.outgoing
.send_response(request_id.clone(), response)
.await;
}
let write_complete_rx = self
.outgoing
.send_response_with_write_complete(request_id.clone(), response)
.await;
tokio::spawn(async move {
if let Ok(write_complete) = write_complete_rx.await
&& let Some(serialized_bytes) = write_complete.serialized_bytes
{
measurement.record_serialized_response(serialized_bytes);
}
});
Ok(None)
}
@@ -2178,7 +2169,7 @@ impl ThreadRequestProcessor {
async fn thread_read_response_inner(
&self,
params: ThreadReadParams,
) -> Result<ThreadReadView, JSONRPCErrorError> {
) -> Result<ThreadReadResponse, JSONRPCErrorError> {
let ThreadReadParams {
thread_id,
include_turns,
@@ -2187,9 +2178,11 @@ impl ThreadRequestProcessor {
let thread_uuid = ThreadId::from_string(&thread_id)
.map_err(|err| invalid_request(format!("invalid thread id: {err}")))?;
self.read_thread_view(thread_uuid, include_turns)
let thread = self
.read_thread_view(thread_uuid, include_turns)
.await
.map_err(thread_read_view_error)
.map_err(thread_read_view_error)?;
Ok(ThreadReadResponse { thread })
}
/// Builds the API view for `thread/read` from persisted metadata plus optional live state.
@@ -2197,59 +2190,50 @@ impl ThreadRequestProcessor {
&self,
thread_id: ThreadId,
include_turns: bool,
) -> Result<ThreadReadView, ThreadReadViewError> {
) -> Result<Thread, ThreadReadViewError> {
let loaded_thread = self.thread_manager.get_thread(thread_id).await.ok();
let mut view = if include_turns {
let mut thread = if include_turns {
if let Some(loaded_thread) = loaded_thread.as_ref() {
// Loaded thread with turns: use persisted metadata when it exists,
// but reconstruct turns from the live ThreadStore history.
let persisted_thread = self
.load_persisted_thread_for_read(thread_id, /*include_turns*/ false)
.await?
.map(|view| view.thread);
ThreadReadView {
thread: self
.load_live_thread_view(
thread_id,
include_turns,
loaded_thread,
persisted_thread,
)
.await?,
projection_measurement: None,
}
} else if let Some(view) = self
.await?;
self.load_live_thread_view(
thread_id,
include_turns,
loaded_thread,
persisted_thread,
)
.await?
} else if let Some(thread) = self
.load_persisted_thread_for_read(thread_id, include_turns)
.await?
{
// Unloaded thread with turns: load metadata and history together
// from the ThreadStore.
view
thread
} else {
return Err(ThreadReadViewError::InvalidRequest(format!(
"thread not loaded: {thread_id}"
)));
}
} else if let Some(view) = self
} else if let Some(thread) = self
.load_persisted_thread_for_read(thread_id, include_turns)
.await?
{
// Persisted metadata-only read: no live thread state is needed.
view
thread
} else if let Some(loaded_thread) = loaded_thread.as_ref() {
// Loaded metadata-only read before persistence is materialized: build
// the response from the live thread snapshot.
ThreadReadView {
thread: self
.load_live_thread_view(
thread_id,
include_turns,
loaded_thread,
/*persisted_thread*/ None,
)
.await?,
projection_measurement: None,
}
self.load_live_thread_view(
thread_id,
include_turns,
loaded_thread,
/*persisted_thread*/ None,
)
.await?
} else {
return Err(ThreadReadViewError::InvalidRequest(format!(
"thread not loaded: {thread_id}"
@@ -2264,22 +2248,22 @@ impl ThreadRequestProcessor {
let thread_status = self
.thread_watch_manager
.loaded_status_for_thread(&view.thread.id)
.loaded_status_for_thread(&thread.id)
.await;
set_thread_status_and_interrupt_stale_turns(
&mut view.thread,
&mut thread,
thread_status,
has_live_in_progress_turn,
);
Ok(view)
Ok(thread)
}
async fn load_persisted_thread_for_read(
&self,
thread_id: ThreadId,
include_turns: bool,
) -> Result<Option<ThreadReadView>, ThreadReadViewError> {
) -> Result<Option<Thread>, ThreadReadViewError> {
let fallback_provider = self.config.model_provider_id.as_str();
match self
.thread_store
@@ -2293,16 +2277,10 @@ impl ThreadRequestProcessor {
Ok(stored_thread) => {
let (mut thread, history) =
thread_from_stored_thread(stored_thread, fallback_provider, &self.config.cwd);
let mut projection_measurement = None;
if include_turns && let Some(history) = history {
thread.turns = build_api_turns_from_rollout_items(&history.items);
projection_measurement =
prepare_rollout_projection_measurement(thread_id, &thread.turns);
}
Ok(Some(ThreadReadView {
thread,
projection_measurement,
}))
Ok(Some(thread))
}
Err(ThreadStoreError::InvalidRequest { message })
if message == format!("no rollout found for thread id {thread_id}") =>
@@ -1,55 +0,0 @@
use codex_protocol::protocol::AgentMessageEvent;
use codex_protocol::protocol::AgentReasoningRawContentEvent;
use codex_protocol::protocol::EventMsg;
use codex_protocol::protocol::RolloutItem;
use codex_protocol::protocol::TurnCompleteEvent;
use codex_protocol::protocol::TurnStartedEvent;
use codex_protocol::protocol::UserMessageEvent;
use pretty_assertions::assert_eq;
use super::build_api_turns_from_rollout_items;
use super::is_completed_user_assistant_turn;
#[test]
fn cold_projection_coalesces_repeated_rollout_updates() {
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
turn_id: "turn-1".to_string(),
trace_id: None,
started_at: None,
model_context_window: None,
collaboration_mode_kind: Default::default(),
})),
RolloutItem::EventMsg(EventMsg::UserMessage(UserMessageEvent {
message: "question".to_string(),
..Default::default()
})),
RolloutItem::EventMsg(EventMsg::AgentReasoningRawContent(
AgentReasoningRawContentEvent {
text: "first".to_string(),
},
)),
RolloutItem::EventMsg(EventMsg::AgentReasoningRawContent(
AgentReasoningRawContentEvent {
text: "second".to_string(),
},
)),
RolloutItem::EventMsg(EventMsg::AgentMessage(AgentMessageEvent {
message: "answer".to_string(),
phase: None,
memory_citation: None,
})),
RolloutItem::EventMsg(EventMsg::TurnComplete(TurnCompleteEvent {
turn_id: "turn-1".to_string(),
last_agent_message: Some("answer".to_string()),
completed_at: None,
duration_ms: None,
time_to_first_token_ms: None,
})),
];
let turns = build_api_turns_from_rollout_items(&rollout_items);
assert_eq!(turns.len(), 1);
assert_eq!(turns[0].items.len(), 3);
assert!(is_completed_user_assistant_turn(&turns[0]));
}
@@ -0,0 +1,120 @@
use std::collections::HashSet;
use std::sync::LazyLock;
use std::sync::Mutex;
use codex_app_server_protocol::Thread;
use codex_app_server_protocol::ThreadItem;
use codex_app_server_protocol::ThreadStatus;
use codex_app_server_protocol::Turn;
use codex_app_server_protocol::TurnStatus;
use codex_otel::MetricsClient;
use codex_protocol::models::MessagePhase;
const RESPONSE_BYTES_METRIC: &str = "codex.app_server.thread_read.response_bytes";
const COMPLETED_TURNS_METRIC: &str = "codex.app_server.thread_read.completed_turns";
const COMPLETED_TURN_ITEMS_METRIC: &str = "codex.app_server.thread_read.completed_turn_items";
const SAMPLE_DENOMINATOR: u64 = 100;
const SAMPLE_RATE_LABEL: &str = "0.01";
static MEASURED_THREADS: LazyLock<Mutex<HashSet<String>>> =
LazyLock::new(|| Mutex::new(HashSet::new()));
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
struct ThreadReadCounts {
completed_turns: u64,
completed_turn_items: u64,
}
pub(crate) struct ThreadReadMeasurement {
metrics: MetricsClient,
counts: ThreadReadCounts,
}
impl ThreadReadMeasurement {
pub(crate) fn prepare(thread: &Thread) -> Option<Self> {
if !matches!(thread.status, ThreadStatus::NotLoaded) || !is_thread_sampled(&thread.id) {
return None;
}
let metrics = codex_otel::global()?;
let counts = count_completed_turns(&thread.turns);
if counts.completed_turns == 0
|| !MEASURED_THREADS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(thread.id.clone())
{
return None;
}
Some(Self { metrics, counts })
}
pub(crate) fn record_serialized_response(self, response_bytes: usize) {
let tags = [
("encoding", "app_server_transport_json"),
("sample_rate", SAMPLE_RATE_LABEL),
("source", "cold_thread_read"),
];
let _ = self.metrics.histogram(
RESPONSE_BYTES_METRIC,
saturating_i64(response_bytes as u64),
&tags,
);
let _ = self.metrics.histogram(
COMPLETED_TURNS_METRIC,
saturating_i64(self.counts.completed_turns),
&tags,
);
let _ = self.metrics.histogram(
COMPLETED_TURN_ITEMS_METRIC,
saturating_i64(self.counts.completed_turn_items),
&tags,
);
}
}
fn count_completed_turns(turns: &[Turn]) -> ThreadReadCounts {
let mut counts = ThreadReadCounts::default();
for turn in turns
.iter()
.filter(|turn| is_completed_user_assistant_turn(turn))
{
counts.completed_turns = counts.completed_turns.saturating_add(1);
counts.completed_turn_items = counts
.completed_turn_items
.saturating_add(turn.items.len() as u64);
}
counts
}
fn is_completed_user_assistant_turn(turn: &Turn) -> bool {
turn.status == TurnStatus::Completed
&& turn
.items
.iter()
.any(|item| matches!(item, ThreadItem::UserMessage { .. }))
&& turn.items.iter().any(|item| {
matches!(
item,
ThreadItem::AgentMessage {
phase: None | Some(MessagePhase::FinalAnswer),
..
}
)
})
}
fn is_thread_sampled(thread_id: &str) -> bool {
let hash = thread_id
.bytes()
.fold(0xcbf29ce484222325_u64, |hash, byte| {
(hash ^ u64::from(byte)).wrapping_mul(0x100000001b3)
});
hash % SAMPLE_DENOMINATOR == 0
}
fn saturating_i64(value: u64) -> i64 {
value.try_into().unwrap_or(i64::MAX)
}
#[cfg(test)]
#[path = "thread_read_metrics_tests.rs"]
mod tests;
@@ -0,0 +1,62 @@
use codex_app_server_protocol::ThreadItem;
use codex_app_server_protocol::Turn;
use codex_app_server_protocol::TurnItemsView;
use codex_app_server_protocol::TurnStatus;
use codex_protocol::models::MessagePhase;
use pretty_assertions::assert_eq;
use super::ThreadReadCounts;
use super::count_completed_turns;
fn turn(status: TurnStatus, items: Vec<ThreadItem>) -> Turn {
Turn {
id: "turn".to_string(),
items,
items_view: TurnItemsView::Full,
status,
error: None,
started_at: None,
completed_at: None,
duration_ms: None,
}
}
#[test]
fn counts_items_in_completed_user_assistant_turns() {
let user_message = || ThreadItem::UserMessage {
id: "user".to_string(),
client_id: None,
content: Vec::new(),
};
let agent_message = |phase| ThreadItem::AgentMessage {
id: "agent".to_string(),
text: "answer".to_string(),
phase,
memory_citation: None,
};
let turns = vec![
turn(
TurnStatus::Completed,
vec![user_message(), agent_message(None)],
),
turn(
TurnStatus::Completed,
vec![
user_message(),
agent_message(Some(MessagePhase::Commentary)),
],
),
turn(
TurnStatus::InProgress,
vec![user_message(), agent_message(None)],
),
];
assert_eq!(
count_completed_turns(&turns),
ThreadReadCounts {
completed_turns: 1,
completed_turn_items: 2,
}
);
}