mirror of
https://github.com/openai/codex.git
synced 2026-11-25 15:24:26 +00:00
Correlate code mode tool analytics with model responses (#36729)
## What changed - Emit dynamic tool-call analytics for code mode `exec` and `wait` calls, including duration and terminal status. - Associate code-mode cells and their nested tool calls with the originating and subsequent model response IDs. - Buffer correlated tool events until the next response is known, while flushing pending events when turns, threads, or the analytics queue close. ## Testing - Add reducer coverage for correlating `exec`, `wait`, and nested tool events across turns. - Add an app-server test that verifies production analytics include the cell and response IDs for a code-mode `exec` call. GitOrigin-RevId: 8b34ab59c1ad68ebbd57f9c8a05f19ab04df48a4
This commit is contained in:
1 parent
dae2122214
commit
155f1ca9e5
13 files changed
+797
-35
No files matched your search
@@ -71,6 +71,8 @@ use crate::facts::AnalyticsFact;
|
||||
use crate::facts::AnalyticsJsonRpcError;
|
||||
use crate::facts::AppMentionedInput;
|
||||
use crate::facts::AppUsedInput;
|
||||
use crate::facts::CodeModeToolCallFact;
|
||||
use crate::facts::CodeModeToolCallStatus;
|
||||
use crate::facts::CodexCompactionEvent;
|
||||
use crate::facts::CodexGoalEvent;
|
||||
use crate::facts::CustomAnalyticsFact;
|
||||
@@ -145,8 +147,10 @@ use codex_protocol::request_permissions::PermissionGrantScope as CorePermissionG
|
||||
use codex_protocol::request_permissions::RequestPermissionsResponse as CoreRequestPermissionsResponse;
|
||||
use sha1::Digest;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::VecDeque;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
const MAX_TOOL_RESPONSE_ENTRIES: usize = 256;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct AnalyticsReducer {
|
||||
@@ -155,6 +159,8 @@ pub(crate) struct AnalyticsReducer {
|
||||
connections: HashMap<u64, ConnectionState>,
|
||||
threads: HashMap<String, ThreadAnalyticsState>,
|
||||
tool_items_started_at_ms: HashMap<ToolItemKey, u64>,
|
||||
tool_response_states: HashMap<(String, String), ToolResponseState>,
|
||||
code_mode_cells: HashMap<String, HashMap<String, CodeModeCellState>>,
|
||||
pending_reviews: HashMap<RequestId, PendingReviewState>,
|
||||
item_review_summaries: HashMap<ToolItemKey, ItemReviewSummary>,
|
||||
}
|
||||
@@ -384,13 +390,31 @@ struct TurnState {
|
||||
tool_counts: TurnToolCounts,
|
||||
}
|
||||
|
||||
#[derive(Hash, Eq, PartialEq)]
|
||||
#[derive(Clone, Hash, Eq, PartialEq)]
|
||||
struct ToolItemKey {
|
||||
thread_id: String,
|
||||
turn_id: String,
|
||||
item_id: String,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct ToolResponseState {
|
||||
response_ids_by_call_id: HashMap<String, String>,
|
||||
cell_ids_by_child_call_id: HashMap<String, String>,
|
||||
pending_tool_events: VecDeque<TrackEventRequest>,
|
||||
}
|
||||
|
||||
struct CodeModeCellState {
|
||||
parent_call_id: String,
|
||||
originating_response_id: Option<String>,
|
||||
closed_in_turn_id: Option<String>,
|
||||
}
|
||||
|
||||
enum ToolEventEmission {
|
||||
ImmediateUnlessCorrelated,
|
||||
AwaitResponse,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct TurnToolCounts {
|
||||
total: usize,
|
||||
@@ -522,6 +546,9 @@ impl AnalyticsReducer {
|
||||
self.ingest_server_request_aborted(completed_at_ms, request_id, out);
|
||||
}
|
||||
AnalyticsFact::Custom(input) => match input {
|
||||
CustomAnalyticsFact::CodeModeToolCall(input) => {
|
||||
self.ingest_code_mode_tool_call(input, out);
|
||||
}
|
||||
CustomAnalyticsFact::SubAgentThreadStarted(input) => {
|
||||
self.ingest_subagent_thread_started(input, out);
|
||||
}
|
||||
@@ -583,6 +610,281 @@ impl AnalyticsReducer {
|
||||
}
|
||||
}
|
||||
|
||||
fn ingest_code_mode_tool_call(
|
||||
&mut self,
|
||||
input: CodeModeToolCallFact,
|
||||
out: &mut Vec<TrackEventRequest>,
|
||||
) {
|
||||
let thread_id = match &input {
|
||||
CodeModeToolCallFact::CellStarted { thread_id, .. }
|
||||
| CodeModeToolCallFact::ChildStarted { thread_id, .. }
|
||||
| CodeModeToolCallFact::CellClosed { thread_id, .. }
|
||||
| CodeModeToolCallFact::SamplingResponseCompleted { thread_id, .. }
|
||||
| CodeModeToolCallFact::Completed { thread_id, .. } => thread_id,
|
||||
};
|
||||
let has_thread_context = self.threads.get(thread_id).is_some_and(|thread| {
|
||||
thread.metadata.is_some()
|
||||
&& thread
|
||||
.connection_id
|
||||
.is_some_and(|connection_id| self.connections.contains_key(&connection_id))
|
||||
});
|
||||
if !has_thread_context {
|
||||
return;
|
||||
}
|
||||
|
||||
match input {
|
||||
CodeModeToolCallFact::CellStarted {
|
||||
thread_id,
|
||||
turn_id,
|
||||
call_id,
|
||||
cell_id,
|
||||
} => {
|
||||
let cells = self.code_mode_cells.entry(thread_id.clone()).or_default();
|
||||
if cells.contains_key(&cell_id) || cells.len() < MAX_TOOL_RESPONSE_ENTRIES {
|
||||
let originating_response_id = self
|
||||
.tool_response_states
|
||||
.get(&(thread_id, turn_id))
|
||||
.and_then(|state| state.response_ids_by_call_id.get(&call_id))
|
||||
.cloned();
|
||||
cells.insert(
|
||||
cell_id,
|
||||
CodeModeCellState {
|
||||
parent_call_id: call_id,
|
||||
originating_response_id,
|
||||
closed_in_turn_id: None,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
CodeModeToolCallFact::ChildStarted {
|
||||
thread_id,
|
||||
turn_id,
|
||||
call_id,
|
||||
cell_id,
|
||||
} => {
|
||||
if self
|
||||
.code_mode_cells
|
||||
.get(&thread_id)
|
||||
.is_some_and(|cells| cells.contains_key(&cell_id))
|
||||
{
|
||||
let state = self
|
||||
.tool_response_states
|
||||
.entry((thread_id, turn_id))
|
||||
.or_default();
|
||||
if state.cell_ids_by_child_call_id.contains_key(&call_id)
|
||||
|| state.cell_ids_by_child_call_id.len() < MAX_TOOL_RESPONSE_ENTRIES
|
||||
{
|
||||
state.cell_ids_by_child_call_id.insert(call_id, cell_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
CodeModeToolCallFact::CellClosed {
|
||||
thread_id,
|
||||
turn_id,
|
||||
cell_id,
|
||||
} => {
|
||||
if let Some(cell) = self
|
||||
.code_mode_cells
|
||||
.get_mut(&thread_id)
|
||||
.and_then(|cells| cells.get_mut(&cell_id))
|
||||
{
|
||||
cell.closed_in_turn_id = Some(turn_id);
|
||||
}
|
||||
}
|
||||
CodeModeToolCallFact::SamplingResponseCompleted {
|
||||
thread_id,
|
||||
turn_id,
|
||||
response_id,
|
||||
tool_call_ids,
|
||||
} => {
|
||||
self.ingest_sampling_response_completed(
|
||||
thread_id,
|
||||
turn_id,
|
||||
response_id,
|
||||
tool_call_ids,
|
||||
out,
|
||||
);
|
||||
}
|
||||
CodeModeToolCallFact::Completed {
|
||||
thread_id,
|
||||
turn_id,
|
||||
call_id,
|
||||
cell_id,
|
||||
tool_name,
|
||||
started_at_ms,
|
||||
completed_at_ms,
|
||||
status,
|
||||
} => {
|
||||
let drop_site = AnalyticsDropSite {
|
||||
event_name: "code mode tool",
|
||||
thread_id: &thread_id,
|
||||
turn_id: Some(&turn_id),
|
||||
review_id: None,
|
||||
item_id: Some(&call_id),
|
||||
};
|
||||
let Some((connection_state, thread_state, thread_metadata)) =
|
||||
self.thread_context_or_warn(drop_site)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let terminal_status = match status {
|
||||
CodeModeToolCallStatus::Completed => ToolItemTerminalStatus::Completed,
|
||||
CodeModeToolCallStatus::Failed => ToolItemTerminalStatus::Failed,
|
||||
CodeModeToolCallStatus::Interrupted => ToolItemTerminalStatus::Interrupted,
|
||||
};
|
||||
let success = status == CodeModeToolCallStatus::Completed;
|
||||
let mut base = tool_item_base(
|
||||
&thread_id,
|
||||
&turn_id,
|
||||
call_id,
|
||||
tool_name.clone(),
|
||||
ToolItemOutcome {
|
||||
terminal_status,
|
||||
failure_kind: (status == CodeModeToolCallStatus::Failed)
|
||||
.then_some(ToolItemFailureKind::ToolError),
|
||||
execution_duration_ms: observed_duration_ms(started_at_ms, completed_at_ms),
|
||||
},
|
||||
ToolItemContext {
|
||||
started_at_ms,
|
||||
completed_at_ms,
|
||||
connection_state,
|
||||
thread_state,
|
||||
thread_metadata,
|
||||
review_summary: None,
|
||||
},
|
||||
);
|
||||
base.cell_id = cell_id;
|
||||
let event = TrackEventRequest::DynamicToolCall(CodexDynamicToolCallEventRequest {
|
||||
event_type: "codex_dynamic_tool_call_event",
|
||||
event_params: CodexDynamicToolCallEventParams {
|
||||
base,
|
||||
dynamic_tool_name: tool_name,
|
||||
success: Some(success),
|
||||
output_content_item_count: None,
|
||||
output_text_item_count: None,
|
||||
output_image_item_count: None,
|
||||
output_audio_item_count: None,
|
||||
},
|
||||
});
|
||||
let counts = &mut self.turns.entry(turn_id.clone()).or_default().tool_counts;
|
||||
counts.total += 1;
|
||||
counts.dynamic_tool_call += 1;
|
||||
self.record_tool_event(
|
||||
&thread_id,
|
||||
&turn_id,
|
||||
event,
|
||||
ToolEventEmission::AwaitResponse,
|
||||
out,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn ingest_sampling_response_completed(
|
||||
&mut self,
|
||||
thread_id: String,
|
||||
turn_id: String,
|
||||
response_id: String,
|
||||
tool_call_ids: Vec<String>,
|
||||
out: &mut Vec<TrackEventRequest>,
|
||||
) {
|
||||
let turn_key = (thread_id.clone(), turn_id);
|
||||
let state = self.tool_response_states.entry(turn_key).or_default();
|
||||
for call_id in tool_call_ids {
|
||||
if state.response_ids_by_call_id.contains_key(&call_id)
|
||||
|| state.response_ids_by_call_id.len() < MAX_TOOL_RESPONSE_ENTRIES
|
||||
{
|
||||
state
|
||||
.response_ids_by_call_id
|
||||
.insert(call_id, response_id.clone());
|
||||
}
|
||||
}
|
||||
if let Some(cells) = self.code_mode_cells.get_mut(&thread_id) {
|
||||
for cell in cells.values_mut() {
|
||||
if cell.originating_response_id.is_none()
|
||||
&& let Some(originating_response_id) =
|
||||
state.response_ids_by_call_id.get(&cell.parent_call_id)
|
||||
{
|
||||
cell.originating_response_id = Some(originating_response_id.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut remaining = VecDeque::new();
|
||||
while let Some(mut event) = state.pending_tool_events.pop_front() {
|
||||
enrich_tool_response_event(&mut event, state, self.code_mode_cells.get(&thread_id));
|
||||
let is_subsequent_response = tool_event_base_mut(&mut event)
|
||||
.and_then(|base| base.originating_response_id.as_deref())
|
||||
.is_some_and(|originating_response_id| originating_response_id != response_id);
|
||||
if is_subsequent_response {
|
||||
if let Some(base) = tool_event_base_mut(&mut event) {
|
||||
base.subsequent_response_id = Some(response_id.clone());
|
||||
}
|
||||
out.push(event);
|
||||
} else {
|
||||
remaining.push_back(event);
|
||||
}
|
||||
}
|
||||
state.pending_tool_events = remaining;
|
||||
}
|
||||
|
||||
fn record_tool_event(
|
||||
&mut self,
|
||||
thread_id: &str,
|
||||
turn_id: &str,
|
||||
mut event: TrackEventRequest,
|
||||
emission: ToolEventEmission,
|
||||
out: &mut Vec<TrackEventRequest>,
|
||||
) {
|
||||
if tool_event_base_mut(&mut event).is_none() {
|
||||
out.push(event);
|
||||
return;
|
||||
}
|
||||
let state = self
|
||||
.tool_response_states
|
||||
.entry((thread_id.to_string(), turn_id.to_string()))
|
||||
.or_default();
|
||||
enrich_tool_response_event(&mut event, state, self.code_mode_cells.get(thread_id));
|
||||
let is_correlated = tool_event_base_mut(&mut event)
|
||||
.is_some_and(|base| base.cell_id.is_some() || base.originating_response_id.is_some());
|
||||
if matches!(emission, ToolEventEmission::ImmediateUnlessCorrelated) && !is_correlated {
|
||||
out.push(event);
|
||||
return;
|
||||
}
|
||||
if state.pending_tool_events.len() == MAX_TOOL_RESPONSE_ENTRIES
|
||||
&& let Some(oldest) = state.pending_tool_events.pop_front()
|
||||
{
|
||||
out.push(oldest);
|
||||
}
|
||||
state.pending_tool_events.push_back(event);
|
||||
}
|
||||
|
||||
fn flush_pending_tool_events(
|
||||
&mut self,
|
||||
thread_id: &str,
|
||||
turn_id: &str,
|
||||
out: &mut Vec<TrackEventRequest>,
|
||||
) {
|
||||
let key = (thread_id.to_string(), turn_id.to_string());
|
||||
if let Some(state) = self.tool_response_states.remove(&key) {
|
||||
out.extend(state.pending_tool_events);
|
||||
}
|
||||
let cells = self.code_mode_cells.get_mut(thread_id);
|
||||
let remove_cells = cells.is_some_and(|cells| {
|
||||
cells.retain(|_, cell| cell.closed_in_turn_id.as_deref() != Some(turn_id));
|
||||
cells.is_empty()
|
||||
});
|
||||
if remove_cells {
|
||||
self.code_mode_cells.remove(thread_id);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn flush(&mut self, out: &mut Vec<TrackEventRequest>) {
|
||||
for state in self.tool_response_states.values_mut() {
|
||||
out.extend(state.pending_tool_events.drain(..));
|
||||
}
|
||||
}
|
||||
|
||||
fn ingest_initialize(
|
||||
&mut self,
|
||||
connection_id: u64,
|
||||
@@ -1347,7 +1649,13 @@ impl AnalyticsReducer {
|
||||
thread_metadata,
|
||||
review_summary: self.item_review_summaries.get(&key),
|
||||
}) {
|
||||
out.push(event);
|
||||
self.record_tool_event(
|
||||
¬ification.thread_id,
|
||||
¬ification.turn_id,
|
||||
event,
|
||||
ToolEventEmission::ImmediateUnlessCorrelated,
|
||||
out,
|
||||
);
|
||||
}
|
||||
self.item_review_summaries.remove(&key);
|
||||
}
|
||||
@@ -1357,6 +1665,17 @@ impl AnalyticsReducer {
|
||||
ServerNotification::ItemGuardianApprovalReviewCompleted(notification) => {
|
||||
self.ingest_guardian_review_completed(notification, out);
|
||||
}
|
||||
ServerNotification::ThreadClosed(notification) => {
|
||||
self.tool_response_states.retain(|(thread_id, _), state| {
|
||||
if thread_id == ¬ification.thread_id {
|
||||
out.extend(state.pending_tool_events.drain(..));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
self.code_mode_cells.remove(¬ification.thread_id);
|
||||
}
|
||||
ServerNotification::TurnStarted(notification) => {
|
||||
let turn_state = self.turns.entry(notification.turn.id).or_default();
|
||||
turn_state.started_at = notification
|
||||
@@ -1370,6 +1689,7 @@ impl AnalyticsReducer {
|
||||
turn_state.latest_diff = Some(notification.diff);
|
||||
}
|
||||
ServerNotification::TurnCompleted(notification) => {
|
||||
self.flush_pending_tool_events(¬ification.thread_id, ¬ification.turn.id, out);
|
||||
let turn_state = self.turns.entry(notification.turn.id.clone()).or_default();
|
||||
turn_state.completed = Some(CompletedTurnState {
|
||||
status: analytics_turn_status(notification.turn.status),
|
||||
@@ -1809,6 +2129,49 @@ fn tracked_tool_item_id(item: &ThreadItem) -> Option<&str> {
|
||||
}
|
||||
}
|
||||
|
||||
fn tool_event_base_mut(event: &mut TrackEventRequest) -> Option<&mut CodexToolItemEventBase> {
|
||||
match event {
|
||||
TrackEventRequest::CommandExecution(event) => Some(&mut event.event_params.base),
|
||||
TrackEventRequest::FileChange(event) => Some(&mut event.event_params.base),
|
||||
TrackEventRequest::McpToolCall(event) => Some(&mut event.event_params.base),
|
||||
TrackEventRequest::DynamicToolCall(event) => Some(&mut event.event_params.base),
|
||||
TrackEventRequest::CollabAgentToolCall(event) => Some(&mut event.event_params.base),
|
||||
TrackEventRequest::WebSearch(event) => Some(&mut event.event_params.base),
|
||||
TrackEventRequest::ImageGeneration(event) => Some(&mut event.event_params.base),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn enrich_tool_response_event(
|
||||
event: &mut TrackEventRequest,
|
||||
state: &ToolResponseState,
|
||||
cells: Option<&HashMap<String, CodeModeCellState>>,
|
||||
) {
|
||||
let Some(base) = tool_event_base_mut(event) else {
|
||||
return;
|
||||
};
|
||||
if base.cell_id.is_none() {
|
||||
base.cell_id = state.cell_ids_by_child_call_id.get(&base.item_id).cloned();
|
||||
}
|
||||
|
||||
let cell = base
|
||||
.cell_id
|
||||
.as_ref()
|
||||
.and_then(|cell_id| cells?.get(cell_id));
|
||||
if let Some(cell) = cell {
|
||||
base.parent_call_id =
|
||||
(cell.parent_call_id != base.item_id).then(|| cell.parent_call_id.clone());
|
||||
} else {
|
||||
base.cell_id = None;
|
||||
base.parent_call_id = None;
|
||||
}
|
||||
base.originating_response_id = state
|
||||
.response_ids_by_call_id
|
||||
.get(&base.item_id)
|
||||
.cloned()
|
||||
.or_else(|| cell.and_then(|cell| cell.originating_response_id.clone()));
|
||||
}
|
||||
|
||||
fn item_review_summary_key(pending_review: &PendingReviewState) -> Option<ToolItemKey> {
|
||||
match pending_review.subject_kind {
|
||||
ReviewSubjectKind::CommandExecution
|
||||
@@ -2229,6 +2592,10 @@ fn tool_item_base(
|
||||
session_id: thread_metadata.session_id.clone(),
|
||||
turn_id: turn_id.to_string(),
|
||||
item_id,
|
||||
cell_id: None,
|
||||
parent_call_id: None,
|
||||
originating_response_id: None,
|
||||
subsequent_response_id: None,
|
||||
app_server_client: context
|
||||
.thread_state
|
||||
.app_server_client(context.connection_state),
|
||||
|
||||
Reference in new issue
Block a user