mirror of
https://github.com/openai/codex.git
synced 2026-04-24 14:45:27 +00:00
[codex-analytics] add steering metadata
This commit is contained in:
@@ -7,6 +7,7 @@ use crate::events::CodexPluginEventRequest;
|
||||
use crate::events::CodexPluginUsedEventRequest;
|
||||
use crate::events::CodexRuntimeMetadata;
|
||||
use crate::events::CodexTurnEventRequest;
|
||||
use crate::events::CodexTurnSteerEventRequest;
|
||||
use crate::events::ThreadInitializationMode;
|
||||
use crate::events::ThreadInitializedEvent;
|
||||
use crate::events::ThreadInitializedEventParams;
|
||||
@@ -14,10 +15,12 @@ use crate::events::TrackEventRequest;
|
||||
use crate::events::codex_app_metadata;
|
||||
use crate::events::codex_plugin_metadata;
|
||||
use crate::events::codex_plugin_used_metadata;
|
||||
use crate::events::codex_turn_steer_event_params;
|
||||
use crate::facts::AnalyticsFact;
|
||||
use crate::facts::AppInvocation;
|
||||
use crate::facts::AppMentionedInput;
|
||||
use crate::facts::AppUsedInput;
|
||||
use crate::facts::CodexTurnSteerEvent;
|
||||
use crate::facts::CustomAnalyticsFact;
|
||||
use crate::facts::InvocationType;
|
||||
use crate::facts::PluginState;
|
||||
@@ -28,6 +31,9 @@ use crate::facts::SkillInvokedInput;
|
||||
use crate::facts::TrackEventsContext;
|
||||
use crate::facts::TurnResolvedConfigFact;
|
||||
use crate::facts::TurnStatus;
|
||||
use crate::facts::TurnSteerInput;
|
||||
use crate::facts::TurnSteerRejectionReason;
|
||||
use crate::facts::TurnSteerResult;
|
||||
use crate::facts::TurnSubmissionType;
|
||||
use crate::reducer::AnalyticsReducer;
|
||||
use crate::reducer::normalize_path_for_skill_id;
|
||||
@@ -921,7 +927,7 @@ fn turn_event_serializes_expected_shape() {
|
||||
is_first_turn: true,
|
||||
status: Some(TurnStatus::Completed),
|
||||
turn_error: None,
|
||||
steer_count: None,
|
||||
steer_count: Some(0),
|
||||
total_tool_call_count: None,
|
||||
shell_command_count: None,
|
||||
file_change_count: None,
|
||||
@@ -967,7 +973,7 @@ fn turn_event_serializes_expected_shape() {
|
||||
"is_first_turn": true,
|
||||
"status": "completed",
|
||||
"turn_error": null,
|
||||
"steer_count": null,
|
||||
"steer_count": 0,
|
||||
"total_tool_call_count": null,
|
||||
"shell_command_count": null,
|
||||
"file_change_count": null,
|
||||
@@ -989,6 +995,90 @@ fn turn_event_serializes_expected_shape() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn turn_steer_event_serializes_expected_shape() {
|
||||
let tracking = TrackEventsContext {
|
||||
model_slug: "gpt-5".to_string(),
|
||||
thread_id: "thread-2".to_string(),
|
||||
turn_id: "turn-2".to_string(),
|
||||
};
|
||||
let event = TrackEventRequest::TurnSteer(CodexTurnSteerEventRequest {
|
||||
event_type: "codex_turn_steer_event",
|
||||
event_params: codex_turn_steer_event_params(
|
||||
&tracking,
|
||||
CodexTurnSteerEvent {
|
||||
expected_turn_id: Some("turn-2".to_string()),
|
||||
accepted_turn_id: Some("turn-2".to_string()),
|
||||
num_input_images: 2,
|
||||
result: TurnSteerResult::Accepted,
|
||||
rejection_reason: None,
|
||||
created_at: 1_716_000_123,
|
||||
},
|
||||
),
|
||||
});
|
||||
|
||||
let payload = serde_json::to_value(&event).expect("serialize turn steer event");
|
||||
|
||||
assert_eq!(
|
||||
payload,
|
||||
json!({
|
||||
"event_type": "codex_turn_steer_event",
|
||||
"event_params": {
|
||||
"thread_id": "thread-2",
|
||||
"expected_turn_id": "turn-2",
|
||||
"accepted_turn_id": "turn-2",
|
||||
"product_client_id": originator().value,
|
||||
"num_input_images": 2,
|
||||
"result": "accepted",
|
||||
"rejection_reason": null,
|
||||
"created_at": 1_716_000_123
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejected_turn_steer_event_serializes_expected_shape() {
|
||||
let tracking = TrackEventsContext {
|
||||
model_slug: "gpt-5".to_string(),
|
||||
thread_id: "thread-3".to_string(),
|
||||
turn_id: "turn-3".to_string(),
|
||||
};
|
||||
let event = TrackEventRequest::TurnSteer(CodexTurnSteerEventRequest {
|
||||
event_type: "codex_turn_steer_event",
|
||||
event_params: codex_turn_steer_event_params(
|
||||
&tracking,
|
||||
CodexTurnSteerEvent {
|
||||
expected_turn_id: Some("turn-expected".to_string()),
|
||||
accepted_turn_id: None,
|
||||
num_input_images: 1,
|
||||
result: TurnSteerResult::Rejected,
|
||||
rejection_reason: Some(TurnSteerRejectionReason::ExpectedTurnMismatch),
|
||||
created_at: 1_716_000_124,
|
||||
},
|
||||
),
|
||||
});
|
||||
|
||||
let payload = serde_json::to_value(&event).expect("serialize rejected turn steer event");
|
||||
|
||||
assert_eq!(
|
||||
payload,
|
||||
json!({
|
||||
"event_type": "codex_turn_steer_event",
|
||||
"event_params": {
|
||||
"thread_id": "thread-3",
|
||||
"expected_turn_id": "turn-expected",
|
||||
"accepted_turn_id": null,
|
||||
"product_client_id": originator().value,
|
||||
"num_input_images": 1,
|
||||
"result": "rejected",
|
||||
"rejection_reason": "expected_turn_mismatch",
|
||||
"created_at": 1_716_000_124
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn turn_lifecycle_emits_turn_event() {
|
||||
let mut reducer = AnalyticsReducer::default();
|
||||
@@ -1026,6 +1116,7 @@ async fn turn_lifecycle_emits_turn_event() {
|
||||
);
|
||||
assert_eq!(payload["event_params"]["num_input_images"], json!(1));
|
||||
assert_eq!(payload["event_params"]["status"], json!("completed"));
|
||||
assert_eq!(payload["event_params"]["steer_count"], json!(0));
|
||||
assert_eq!(payload["event_params"]["created_at"], json!(455));
|
||||
assert_eq!(payload["event_params"]["completed_at"], json!(456));
|
||||
assert_eq!(payload["event_params"]["duration_ms"], json!(1234));
|
||||
@@ -1039,6 +1130,104 @@ async fn turn_lifecycle_emits_turn_event() {
|
||||
assert_eq!(payload["event_params"]["total_tokens"], json!(321));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn accepted_steers_increment_turn_steer_count() {
|
||||
let mut reducer = AnalyticsReducer::default();
|
||||
let mut out = Vec::new();
|
||||
|
||||
ingest_turn_prerequisites(
|
||||
&mut reducer,
|
||||
&mut out,
|
||||
/*include_initialize*/ true,
|
||||
/*include_resolved_config*/ true,
|
||||
/*include_started*/ true,
|
||||
/*include_token_usage*/ false,
|
||||
)
|
||||
.await;
|
||||
|
||||
reducer
|
||||
.ingest(
|
||||
AnalyticsFact::Custom(CustomAnalyticsFact::TurnSteer(TurnSteerInput {
|
||||
tracking: TrackEventsContext {
|
||||
model_slug: "gpt-5".to_string(),
|
||||
thread_id: "thread-2".to_string(),
|
||||
turn_id: "turn-2".to_string(),
|
||||
},
|
||||
turn_steer: CodexTurnSteerEvent {
|
||||
expected_turn_id: Some("turn-2".to_string()),
|
||||
accepted_turn_id: Some("turn-2".to_string()),
|
||||
num_input_images: 0,
|
||||
result: TurnSteerResult::Accepted,
|
||||
rejection_reason: None,
|
||||
created_at: 1,
|
||||
},
|
||||
})),
|
||||
&mut out,
|
||||
)
|
||||
.await;
|
||||
|
||||
reducer
|
||||
.ingest(
|
||||
AnalyticsFact::Custom(CustomAnalyticsFact::TurnSteer(TurnSteerInput {
|
||||
tracking: TrackEventsContext {
|
||||
model_slug: "gpt-5".to_string(),
|
||||
thread_id: "thread-2".to_string(),
|
||||
turn_id: "turn-2".to_string(),
|
||||
},
|
||||
turn_steer: CodexTurnSteerEvent {
|
||||
expected_turn_id: None,
|
||||
accepted_turn_id: None,
|
||||
num_input_images: 0,
|
||||
result: TurnSteerResult::Rejected,
|
||||
rejection_reason: Some(TurnSteerRejectionReason::NoActiveTurn),
|
||||
created_at: 2,
|
||||
},
|
||||
})),
|
||||
&mut out,
|
||||
)
|
||||
.await;
|
||||
|
||||
reducer
|
||||
.ingest(
|
||||
AnalyticsFact::Custom(CustomAnalyticsFact::TurnSteer(TurnSteerInput {
|
||||
tracking: TrackEventsContext {
|
||||
model_slug: "gpt-5".to_string(),
|
||||
thread_id: "thread-2".to_string(),
|
||||
turn_id: "turn-2".to_string(),
|
||||
},
|
||||
turn_steer: CodexTurnSteerEvent {
|
||||
expected_turn_id: Some("turn-2".to_string()),
|
||||
accepted_turn_id: Some("turn-2".to_string()),
|
||||
num_input_images: 1,
|
||||
result: TurnSteerResult::Accepted,
|
||||
rejection_reason: None,
|
||||
created_at: 3,
|
||||
},
|
||||
})),
|
||||
&mut out,
|
||||
)
|
||||
.await;
|
||||
|
||||
reducer
|
||||
.ingest(
|
||||
AnalyticsFact::Notification(Box::new(sample_turn_completed_notification(
|
||||
"thread-2",
|
||||
"turn-2",
|
||||
AppServerTurnStatus::Completed,
|
||||
/*codex_error_info*/ None,
|
||||
))),
|
||||
&mut out,
|
||||
)
|
||||
.await;
|
||||
|
||||
let turn_event = out
|
||||
.iter()
|
||||
.find(|event| matches!(event, TrackEventRequest::TurnEvent(_)))
|
||||
.expect("turn event should be emitted");
|
||||
let payload = serde_json::to_value(turn_event).expect("serialize turn event");
|
||||
assert_eq!(payload["event_params"]["steer_count"], json!(2));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn queued_submission_type_emits_queued_turn_event() {
|
||||
let mut reducer = AnalyticsReducer::default();
|
||||
|
||||
@@ -6,6 +6,7 @@ use crate::facts::AnalyticsFact;
|
||||
use crate::facts::AppInvocation;
|
||||
use crate::facts::AppMentionedInput;
|
||||
use crate::facts::AppUsedInput;
|
||||
use crate::facts::CodexTurnSteerEvent;
|
||||
use crate::facts::CustomAnalyticsFact;
|
||||
use crate::facts::PluginState;
|
||||
use crate::facts::PluginStateChangedInput;
|
||||
@@ -13,6 +14,7 @@ use crate::facts::SkillInvocation;
|
||||
use crate::facts::SkillInvokedInput;
|
||||
use crate::facts::TrackEventsContext;
|
||||
use crate::facts::TurnResolvedConfigFact;
|
||||
use crate::facts::TurnSteerInput;
|
||||
use crate::reducer::AnalyticsReducer;
|
||||
use codex_app_server_protocol::ClientRequest;
|
||||
use codex_app_server_protocol::ClientResponse;
|
||||
@@ -189,6 +191,15 @@ impl AnalyticsEventsClient {
|
||||
));
|
||||
}
|
||||
|
||||
pub fn track_turn_steer(&self, tracking: TrackEventsContext, turn_steer: CodexTurnSteerEvent) {
|
||||
self.record_fact(AnalyticsFact::Custom(CustomAnalyticsFact::TurnSteer(
|
||||
TurnSteerInput {
|
||||
tracking,
|
||||
turn_steer,
|
||||
},
|
||||
)));
|
||||
}
|
||||
|
||||
pub fn track_plugin_installed(&self, plugin: PluginTelemetryMetadata) {
|
||||
self.record_fact(AnalyticsFact::Custom(
|
||||
CustomAnalyticsFact::PluginStateChanged(PluginStateChangedInput {
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
use crate::facts::AppInvocation;
|
||||
use crate::facts::CodexTurnSteerEvent;
|
||||
use crate::facts::InvocationType;
|
||||
use crate::facts::PluginState;
|
||||
use crate::facts::TrackEventsContext;
|
||||
use crate::facts::TurnStatus;
|
||||
use crate::facts::TurnSteerRejectionReason;
|
||||
use crate::facts::TurnSteerResult;
|
||||
use crate::facts::TurnSubmissionType;
|
||||
use codex_app_server_protocol::CodexErrorInfo;
|
||||
use codex_login::default_client::originator;
|
||||
@@ -39,6 +42,7 @@ pub(crate) enum TrackEventRequest {
|
||||
AppMentioned(CodexAppMentionedEventRequest),
|
||||
AppUsed(CodexAppUsedEventRequest),
|
||||
TurnEvent(Box<CodexTurnEventRequest>),
|
||||
TurnSteer(CodexTurnSteerEventRequest),
|
||||
PluginUsed(CodexPluginUsedEventRequest),
|
||||
PluginInstalled(CodexPluginEventRequest),
|
||||
PluginUninstalled(CodexPluginEventRequest),
|
||||
@@ -170,6 +174,24 @@ pub(crate) struct CodexTurnEventRequest {
|
||||
pub(crate) event_params: CodexTurnEventParams,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub(crate) struct CodexTurnSteerEventParams {
|
||||
pub(crate) thread_id: String,
|
||||
pub(crate) expected_turn_id: Option<String>,
|
||||
pub(crate) accepted_turn_id: Option<String>,
|
||||
pub(crate) product_client_id: Option<String>,
|
||||
pub(crate) num_input_images: usize,
|
||||
pub(crate) result: TurnSteerResult,
|
||||
pub(crate) rejection_reason: Option<TurnSteerRejectionReason>,
|
||||
pub(crate) created_at: u64,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub(crate) struct CodexTurnSteerEventRequest {
|
||||
pub(crate) event_type: &'static str,
|
||||
pub(crate) event_params: CodexTurnSteerEventParams,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub(crate) struct CodexPluginMetadata {
|
||||
pub(crate) plugin_id: Option<String>,
|
||||
@@ -261,6 +283,22 @@ pub(crate) fn codex_plugin_used_metadata(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn codex_turn_steer_event_params(
|
||||
tracking: &TrackEventsContext,
|
||||
turn_steer: CodexTurnSteerEvent,
|
||||
) -> CodexTurnSteerEventParams {
|
||||
CodexTurnSteerEventParams {
|
||||
thread_id: tracking.thread_id.clone(),
|
||||
expected_turn_id: turn_steer.expected_turn_id,
|
||||
accepted_turn_id: turn_steer.accepted_turn_id,
|
||||
product_client_id: Some(originator().value),
|
||||
num_input_images: turn_steer.num_input_images,
|
||||
result: turn_steer.result,
|
||||
rejection_reason: turn_steer.rejection_reason,
|
||||
created_at: turn_steer.created_at,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn thread_source_name(thread_source: &SessionSource) -> Option<&'static str> {
|
||||
match thread_source {
|
||||
SessionSource::Cli | SessionSource::VSCode | SessionSource::Exec => Some("user"),
|
||||
|
||||
@@ -72,6 +72,33 @@ pub enum TurnStatus {
|
||||
Interrupted,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum TurnSteerResult {
|
||||
Accepted,
|
||||
Rejected,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum TurnSteerRejectionReason {
|
||||
NoActiveTurn,
|
||||
ExpectedTurnMismatch,
|
||||
NonSteerableReview,
|
||||
NonSteerableCompact,
|
||||
EmptyInput,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CodexTurnSteerEvent {
|
||||
pub expected_turn_id: Option<String>,
|
||||
pub accepted_turn_id: Option<String>,
|
||||
pub num_input_images: usize,
|
||||
pub result: TurnSteerResult,
|
||||
pub rejection_reason: Option<TurnSteerRejectionReason>,
|
||||
pub created_at: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SkillInvocation {
|
||||
pub skill_name: String,
|
||||
@@ -119,6 +146,7 @@ pub(crate) enum AnalyticsFact {
|
||||
|
||||
pub(crate) enum CustomAnalyticsFact {
|
||||
TurnResolvedConfig(Box<TurnResolvedConfigFact>),
|
||||
TurnSteer(TurnSteerInput),
|
||||
SkillInvoked(SkillInvokedInput),
|
||||
AppMentioned(AppMentionedInput),
|
||||
AppUsed(AppUsedInput),
|
||||
@@ -126,6 +154,11 @@ pub(crate) enum CustomAnalyticsFact {
|
||||
PluginStateChanged(PluginStateChangedInput),
|
||||
}
|
||||
|
||||
pub(crate) struct TurnSteerInput {
|
||||
pub tracking: TrackEventsContext,
|
||||
pub turn_steer: CodexTurnSteerEvent,
|
||||
}
|
||||
|
||||
pub(crate) struct SkillInvokedInput {
|
||||
pub tracking: TrackEventsContext,
|
||||
pub invocations: Vec<SkillInvocation>,
|
||||
|
||||
@@ -6,11 +6,14 @@ mod reducer;
|
||||
pub use client::AnalyticsEventsClient;
|
||||
pub use events::AppServerRpcTransport;
|
||||
pub use facts::AppInvocation;
|
||||
pub use facts::CodexTurnSteerEvent;
|
||||
pub use facts::InvocationType;
|
||||
pub use facts::SkillInvocation;
|
||||
pub use facts::TrackEventsContext;
|
||||
pub use facts::TurnResolvedConfigFact;
|
||||
pub use facts::TurnStatus;
|
||||
pub use facts::TurnSteerRejectionReason;
|
||||
pub use facts::TurnSteerResult;
|
||||
pub use facts::TurnSubmissionType;
|
||||
pub use facts::build_track_events_context;
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ use crate::events::CodexPluginUsedEventRequest;
|
||||
use crate::events::CodexRuntimeMetadata;
|
||||
use crate::events::CodexTurnEventParams;
|
||||
use crate::events::CodexTurnEventRequest;
|
||||
use crate::events::CodexTurnSteerEventRequest;
|
||||
use crate::events::SkillInvocationEventParams;
|
||||
use crate::events::SkillInvocationEventRequest;
|
||||
use crate::events::ThreadInitializationMode;
|
||||
@@ -16,6 +17,7 @@ use crate::events::TrackEventRequest;
|
||||
use crate::events::codex_app_metadata;
|
||||
use crate::events::codex_plugin_metadata;
|
||||
use crate::events::codex_plugin_used_metadata;
|
||||
use crate::events::codex_turn_steer_event_params;
|
||||
use crate::events::plugin_state_event_type;
|
||||
use crate::events::thread_source_name;
|
||||
use crate::facts::AnalyticsFact;
|
||||
@@ -28,6 +30,8 @@ use crate::facts::PluginUsedInput;
|
||||
use crate::facts::SkillInvokedInput;
|
||||
use crate::facts::TurnResolvedConfigFact;
|
||||
use crate::facts::TurnStatus;
|
||||
use crate::facts::TurnSteerInput;
|
||||
use crate::facts::TurnSteerResult;
|
||||
use codex_app_server_protocol::ClientRequest;
|
||||
use codex_app_server_protocol::ClientResponse;
|
||||
use codex_app_server_protocol::CodexErrorInfo;
|
||||
@@ -86,6 +90,7 @@ struct TurnState {
|
||||
created_at: Option<u64>,
|
||||
token_usage: Option<TokenUsageBreakdown>,
|
||||
completed: Option<CompletedTurnState>,
|
||||
steer_count: usize,
|
||||
}
|
||||
|
||||
impl AnalyticsReducer {
|
||||
@@ -126,6 +131,9 @@ impl AnalyticsReducer {
|
||||
CustomAnalyticsFact::TurnResolvedConfig(input) => {
|
||||
self.ingest_turn_resolved_config(*input, out);
|
||||
}
|
||||
CustomAnalyticsFact::TurnSteer(input) => {
|
||||
self.ingest_turn_steer(input, out);
|
||||
}
|
||||
CustomAnalyticsFact::SkillInvoked(input) => {
|
||||
self.ingest_skill_invoked(input, out).await;
|
||||
}
|
||||
@@ -210,6 +218,7 @@ impl AnalyticsReducer {
|
||||
created_at: None,
|
||||
token_usage: None,
|
||||
completed: None,
|
||||
steer_count: 0,
|
||||
});
|
||||
turn_state.thread_id = Some(thread_id);
|
||||
turn_state.num_input_images = Some(num_input_images);
|
||||
@@ -363,6 +372,7 @@ impl AnalyticsReducer {
|
||||
created_at: None,
|
||||
token_usage: None,
|
||||
completed: None,
|
||||
steer_count: 0,
|
||||
});
|
||||
turn_state.connection_id = Some(connection_id);
|
||||
turn_state.thread_id = Some(pending_request.thread_id);
|
||||
@@ -388,6 +398,7 @@ impl AnalyticsReducer {
|
||||
created_at: None,
|
||||
token_usage: None,
|
||||
completed: None,
|
||||
steer_count: 0,
|
||||
});
|
||||
turn_state.created_at = u64::try_from(notification.created_at).ok();
|
||||
}
|
||||
@@ -400,6 +411,7 @@ impl AnalyticsReducer {
|
||||
created_at: None,
|
||||
token_usage: None,
|
||||
completed: None,
|
||||
steer_count: 0,
|
||||
});
|
||||
turn_state.token_usage = Some(notification.token_usage.last);
|
||||
}
|
||||
@@ -415,6 +427,7 @@ impl AnalyticsReducer {
|
||||
created_at: None,
|
||||
token_usage: None,
|
||||
completed: None,
|
||||
steer_count: 0,
|
||||
});
|
||||
turn_state.completed = Some(CompletedTurnState {
|
||||
status: analytics_turn_status(notification.turn.status),
|
||||
@@ -465,6 +478,23 @@ impl AnalyticsReducer {
|
||||
));
|
||||
}
|
||||
|
||||
fn ingest_turn_steer(&mut self, input: TurnSteerInput, out: &mut Vec<TrackEventRequest>) {
|
||||
let TurnSteerInput {
|
||||
tracking,
|
||||
turn_steer,
|
||||
} = input;
|
||||
if matches!(turn_steer.result, TurnSteerResult::Accepted)
|
||||
&& let Some(accepted_turn_id) = turn_steer.accepted_turn_id.as_ref()
|
||||
&& let Some(turn_state) = self.turns.get_mut(accepted_turn_id)
|
||||
{
|
||||
turn_state.steer_count += 1;
|
||||
}
|
||||
out.push(TrackEventRequest::TurnSteer(CodexTurnSteerEventRequest {
|
||||
event_type: "codex_turn_steer_event",
|
||||
event_params: codex_turn_steer_event_params(&tracking, turn_steer),
|
||||
}));
|
||||
}
|
||||
|
||||
fn maybe_emit_turn_event(&mut self, turn_id: &str, out: &mut Vec<TrackEventRequest>) {
|
||||
let Some(turn_state) = self.turns.get(turn_id) else {
|
||||
return;
|
||||
@@ -550,7 +580,7 @@ fn codex_turn_event_params(
|
||||
is_first_turn,
|
||||
status: completed.status,
|
||||
turn_error: completed.turn_error,
|
||||
steer_count: None,
|
||||
steer_count: Some(turn_state.steer_count),
|
||||
total_tool_call_count: None,
|
||||
shell_command_count: None,
|
||||
file_change_count: None,
|
||||
|
||||
@@ -5,6 +5,8 @@ use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicU64;
|
||||
use std::time::SystemTime;
|
||||
use std::time::UNIX_EPOCH;
|
||||
|
||||
use crate::agent::AgentControl;
|
||||
use crate::agent::AgentStatus;
|
||||
@@ -52,8 +54,12 @@ use chrono::Local;
|
||||
use chrono::Utc;
|
||||
use codex_analytics::AnalyticsEventsClient;
|
||||
use codex_analytics::AppInvocation;
|
||||
use codex_analytics::CodexTurnSteerEvent;
|
||||
use codex_analytics::InvocationType;
|
||||
use codex_analytics::TrackEventsContext;
|
||||
use codex_analytics::TurnResolvedConfigFact;
|
||||
use codex_analytics::TurnSteerRejectionReason;
|
||||
use codex_analytics::TurnSteerResult;
|
||||
use codex_analytics::TurnSubmissionType;
|
||||
use codex_analytics::build_track_events_context;
|
||||
use codex_app_server_protocol::McpServerElicitationRequest;
|
||||
@@ -237,6 +243,18 @@ impl SteerInputError {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn to_turn_steer_rejection_reason(&self) -> TurnSteerRejectionReason {
|
||||
match self {
|
||||
Self::NoActiveTurn(_) => TurnSteerRejectionReason::NoActiveTurn,
|
||||
Self::ExpectedTurnMismatch { .. } => TurnSteerRejectionReason::ExpectedTurnMismatch,
|
||||
Self::ActiveTurnNotSteerable { turn_kind } => match turn_kind {
|
||||
NonSteerableTurnKind::Review => TurnSteerRejectionReason::NonSteerableReview,
|
||||
NonSteerableTurnKind::Compact => TurnSteerRejectionReason::NonSteerableCompact,
|
||||
},
|
||||
Self::EmptyInput => TurnSteerRejectionReason::EmptyInput,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Notes from the previous real user turn.
|
||||
@@ -4027,47 +4045,159 @@ impl Session {
|
||||
input: Vec<UserInput>,
|
||||
expected_turn_id: Option<&str>,
|
||||
) -> Result<String, SteerInputError> {
|
||||
let created_at = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs();
|
||||
let thread_id = self.conversation_id.to_string();
|
||||
let fallback_tracking = || {
|
||||
build_track_events_context(
|
||||
String::new(),
|
||||
thread_id.clone(),
|
||||
expected_turn_id.unwrap_or_default().to_string(),
|
||||
)
|
||||
};
|
||||
let track_rejection = |tracking: TrackEventsContext,
|
||||
expected_turn_id: Option<String>,
|
||||
rejection_reason: TurnSteerRejectionReason,
|
||||
num_input_images: usize| {
|
||||
self.services.analytics_events_client.track_turn_steer(
|
||||
tracking,
|
||||
CodexTurnSteerEvent {
|
||||
expected_turn_id,
|
||||
accepted_turn_id: None,
|
||||
num_input_images,
|
||||
result: TurnSteerResult::Rejected,
|
||||
rejection_reason: Some(rejection_reason),
|
||||
created_at,
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
if input.is_empty() {
|
||||
return Err(SteerInputError::EmptyInput);
|
||||
let err = SteerInputError::EmptyInput;
|
||||
track_rejection(
|
||||
fallback_tracking(),
|
||||
expected_turn_id.map(str::to_string),
|
||||
err.to_turn_steer_rejection_reason(),
|
||||
/*num_input_images*/ 0,
|
||||
);
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
let num_input_images = input
|
||||
.iter()
|
||||
.filter(|item| matches!(item, UserInput::Image { .. } | UserInput::LocalImage { .. }))
|
||||
.count();
|
||||
|
||||
let mut active = self.active_turn.lock().await;
|
||||
let Some(active_turn) = active.as_mut() else {
|
||||
return Err(SteerInputError::NoActiveTurn(input));
|
||||
let err = SteerInputError::NoActiveTurn(input);
|
||||
track_rejection(
|
||||
fallback_tracking(),
|
||||
expected_turn_id.map(str::to_string),
|
||||
err.to_turn_steer_rejection_reason(),
|
||||
num_input_images,
|
||||
);
|
||||
return Err(err);
|
||||
};
|
||||
|
||||
let Some((active_turn_id, _)) = active_turn.tasks.first() else {
|
||||
return Err(SteerInputError::NoActiveTurn(input));
|
||||
let Some((active_turn_id, task)) = active_turn.tasks.first() else {
|
||||
let err = SteerInputError::NoActiveTurn(input);
|
||||
track_rejection(
|
||||
fallback_tracking(),
|
||||
expected_turn_id.map(str::to_string),
|
||||
err.to_turn_steer_rejection_reason(),
|
||||
num_input_images,
|
||||
);
|
||||
return Err(err);
|
||||
};
|
||||
let active_turn_id = active_turn_id.clone();
|
||||
let tracking = build_track_events_context(
|
||||
task.turn_context.model_info.slug.clone(),
|
||||
thread_id.clone(),
|
||||
task.turn_context.sub_id.clone(),
|
||||
);
|
||||
|
||||
if let Some(expected_turn_id) = expected_turn_id
|
||||
&& expected_turn_id != active_turn_id
|
||||
{
|
||||
return Err(SteerInputError::ExpectedTurnMismatch {
|
||||
let err = SteerInputError::ExpectedTurnMismatch {
|
||||
expected: expected_turn_id.to_string(),
|
||||
actual: active_turn_id.clone(),
|
||||
});
|
||||
actual: active_turn_id,
|
||||
};
|
||||
track_rejection(
|
||||
tracking,
|
||||
Some(expected_turn_id.to_string()),
|
||||
err.to_turn_steer_rejection_reason(),
|
||||
num_input_images,
|
||||
);
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
match active_turn.tasks.first().map(|(_, task)| task.kind) {
|
||||
Some(crate::state::TaskKind::Regular) => {}
|
||||
Some(crate::state::TaskKind::Review) => {
|
||||
return Err(SteerInputError::ActiveTurnNotSteerable {
|
||||
let err = SteerInputError::ActiveTurnNotSteerable {
|
||||
turn_kind: NonSteerableTurnKind::Review,
|
||||
});
|
||||
};
|
||||
track_rejection(
|
||||
tracking,
|
||||
expected_turn_id
|
||||
.map(str::to_string)
|
||||
.or(Some(active_turn_id.clone())),
|
||||
err.to_turn_steer_rejection_reason(),
|
||||
num_input_images,
|
||||
);
|
||||
return Err(err);
|
||||
}
|
||||
Some(crate::state::TaskKind::Compact) => {
|
||||
return Err(SteerInputError::ActiveTurnNotSteerable {
|
||||
let err = SteerInputError::ActiveTurnNotSteerable {
|
||||
turn_kind: NonSteerableTurnKind::Compact,
|
||||
});
|
||||
};
|
||||
track_rejection(
|
||||
tracking,
|
||||
expected_turn_id
|
||||
.map(str::to_string)
|
||||
.or(Some(active_turn_id.clone())),
|
||||
err.to_turn_steer_rejection_reason(),
|
||||
num_input_images,
|
||||
);
|
||||
return Err(err);
|
||||
}
|
||||
None => {
|
||||
let err = SteerInputError::NoActiveTurn(input);
|
||||
track_rejection(
|
||||
fallback_tracking(),
|
||||
expected_turn_id.map(str::to_string),
|
||||
err.to_turn_steer_rejection_reason(),
|
||||
num_input_images,
|
||||
);
|
||||
return Err(err);
|
||||
}
|
||||
None => return Err(SteerInputError::NoActiveTurn(input)),
|
||||
}
|
||||
|
||||
let mut turn_state = active_turn.turn_state.lock().await;
|
||||
turn_state.push_pending_input(input.into());
|
||||
turn_state.accept_mailbox_delivery_for_current_turn();
|
||||
Ok(active_turn_id.clone())
|
||||
drop(turn_state);
|
||||
drop(active);
|
||||
|
||||
let expected_turn_id = expected_turn_id
|
||||
.map(str::to_string)
|
||||
.unwrap_or_else(|| active_turn_id.clone());
|
||||
self.services.analytics_events_client.track_turn_steer(
|
||||
tracking,
|
||||
CodexTurnSteerEvent {
|
||||
expected_turn_id: Some(expected_turn_id),
|
||||
accepted_turn_id: Some(active_turn_id.clone()),
|
||||
num_input_images,
|
||||
result: TurnSteerResult::Accepted,
|
||||
rejection_reason: None,
|
||||
created_at,
|
||||
},
|
||||
);
|
||||
Ok(active_turn_id)
|
||||
}
|
||||
|
||||
/// Returns the input if there was no task running to inject into.
|
||||
|
||||
Reference in New Issue
Block a user