Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions codex-rs/analytics/src/analytics_client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,8 @@ fn compaction_event_serializes_expected_shape() {
error: None,
active_context_tokens_before: 120_000,
active_context_tokens_after: 18_000,
retained_image_count: None,
compaction_summary_tokens: None,
started_at: 100,
completed_at: 106,
duration_ms: Some(6543),
Expand Down Expand Up @@ -1301,6 +1303,8 @@ fn compaction_event_serializes_expected_shape() {
"error": null,
"active_context_tokens_before": 120000,
"active_context_tokens_after": 18000,
"retained_image_count": null,
"compaction_summary_tokens": null,
"started_at": 100,
"completed_at": 106,
"duration_ms": 6543
Expand Down Expand Up @@ -1821,6 +1825,8 @@ async fn compaction_event_ingests_custom_fact() {
error: Some("context limit exceeded".to_string()),
active_context_tokens_before: 131_000,
active_context_tokens_after: 131_000,
retained_image_count: None,
compaction_summary_tokens: None,
started_at: 100,
completed_at: 101,
duration_ms: Some(1200),
Expand Down Expand Up @@ -2743,6 +2749,8 @@ async fn subagent_thread_started_inherits_parent_connection_for_new_thread() {
error: None,
active_context_tokens_before: 131_000,
active_context_tokens_after: 64_000,
retained_image_count: None,
compaction_summary_tokens: None,
started_at: 100,
completed_at: 101,
duration_ms: Some(1200),
Expand Down
4 changes: 4 additions & 0 deletions codex-rs/analytics/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,8 @@ pub(crate) struct CodexCompactionEventParams {
pub(crate) error: Option<String>,
pub(crate) active_context_tokens_before: i64,
pub(crate) active_context_tokens_after: i64,
pub(crate) retained_image_count: Option<usize>,
pub(crate) compaction_summary_tokens: Option<i64>,
pub(crate) started_at: u64,
pub(crate) completed_at: u64,
pub(crate) duration_ms: Option<u64>,
Expand Down Expand Up @@ -970,6 +972,8 @@ pub(crate) fn codex_compaction_event_params(
error: input.error,
active_context_tokens_before: input.active_context_tokens_before,
active_context_tokens_after: input.active_context_tokens_after,
retained_image_count: input.retained_image_count,
compaction_summary_tokens: input.compaction_summary_tokens,
started_at: input.started_at,
completed_at: input.completed_at,
duration_ms: input.duration_ms,
Expand Down
2 changes: 2 additions & 0 deletions codex-rs/analytics/src/facts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ pub struct CodexCompactionEvent {
pub error: Option<String>,
pub active_context_tokens_before: i64,
pub active_context_tokens_after: i64,
pub retained_image_count: Option<usize>,
pub compaction_summary_tokens: Option<i64>,
pub started_at: u64,
pub completed_at: u64,
pub duration_ms: Option<u64>,
Expand Down
22 changes: 18 additions & 4 deletions codex-rs/core/src/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async fn run_compact_task_inner(
sess.as_ref(),
CompactionStatus::Interrupted,
Some(error),
/*active_context_tokens_before*/ None,
CompactionAnalyticsDetails::default(),
)
.await;
return Err(CodexErr::TurnAborted);
Expand All @@ -174,7 +174,7 @@ async fn run_compact_task_inner(
sess.as_ref(),
status,
error,
/*active_context_tokens_before*/ None,
CompactionAnalyticsDetails::default(),
)
.await;
return Err(CodexErr::TurnAborted);
Expand All @@ -185,7 +185,7 @@ async fn run_compact_task_inner(
sess.as_ref(),
status,
error,
/*active_context_tokens_before*/ None,
CompactionAnalyticsDetails::default(),
)
.await;
result.map(|_| ())
Expand Down Expand Up @@ -335,6 +335,13 @@ pub(crate) struct CompactionAnalyticsAttempt {
start_instant: Instant,
}

#[derive(Clone, Copy, Default)]
pub(crate) struct CompactionAnalyticsDetails {
pub(crate) active_context_tokens_before: Option<i64>,
pub(crate) retained_image_count: Option<usize>,
pub(crate) compaction_summary_tokens: Option<i64>,
}

impl CompactionAnalyticsAttempt {
pub(crate) async fn begin(
sess: &Session,
Expand Down Expand Up @@ -363,8 +370,13 @@ impl CompactionAnalyticsAttempt {
sess: &Session,
status: CompactionStatus,
error: Option<String>,
active_context_tokens_before: Option<i64>,
details: CompactionAnalyticsDetails,
) {
let CompactionAnalyticsDetails {
active_context_tokens_before,
retained_image_count,
compaction_summary_tokens,
} = details;
let active_context_tokens_before =
active_context_tokens_before.unwrap_or(self.active_context_tokens_before);
let active_context_tokens_after = sess.get_total_token_usage().await;
Expand All @@ -382,6 +394,8 @@ impl CompactionAnalyticsAttempt {
error,
active_context_tokens_before,
active_context_tokens_after,
retained_image_count,
compaction_summary_tokens,
started_at: self.started_at,
completed_at: now_unix_seconds(),
duration_ms: Some(
Expand Down
34 changes: 16 additions & 18 deletions codex-rs/core/src/compact_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use crate::Prompt;
use crate::client::CompactConversationRequestSettings;
use crate::compact::CompactionAnalyticsAttempt;
use crate::compact::CompactionAnalyticsDetails;
use crate::compact::InitialContextInjection;
use crate::compact::compaction_status_from_result;
use crate::compact::insert_initial_context_before_last_real_user_or_summary;
Expand Down Expand Up @@ -100,7 +101,10 @@ async fn run_remote_compact_task_inner(
CompactionImplementation::ResponsesCompact,
phase,
);
let mut active_context_tokens_before = sess.get_total_token_usage().await;
let mut analytics_details = CompactionAnalyticsDetails {
active_context_tokens_before: Some(sess.get_total_token_usage().await),
..Default::default()
};
let attempt = CompactionAnalyticsAttempt::begin(
sess.as_ref(),
turn_context.as_ref(),
Expand All @@ -120,7 +124,7 @@ async fn run_remote_compact_task_inner(
sess.as_ref(),
codex_analytics::CompactionStatus::Interrupted,
Some(error),
Some(active_context_tokens_before),
analytics_details,
)
.await;
return Err(CodexErr::TurnAborted);
Expand All @@ -131,7 +135,7 @@ async fn run_remote_compact_task_inner(
turn_context,
initial_context_injection,
compaction_metadata,
&mut active_context_tokens_before,
&mut analytics_details,
)
.await;
let status = compaction_status_from_result(&result);
Expand All @@ -140,23 +144,13 @@ async fn run_remote_compact_task_inner(
let post_compact_outcome = run_post_compact_hooks(sess, turn_context, trigger).await;
if let PostCompactHookOutcome::Stopped = post_compact_outcome {
attempt
.track(
sess.as_ref(),
status,
error,
Some(active_context_tokens_before),
)
.track(sess.as_ref(), status, error, analytics_details)
.await;
return Err(CodexErr::TurnAborted);
}
}
attempt
.track(
sess.as_ref(),
status,
error.clone(),
Some(active_context_tokens_before),
)
.track(sess.as_ref(), status, error.clone(), analytics_details)
.await;
if let Err(err) = result {
sess.track_turn_codex_error(turn_context, &err);
Expand All @@ -174,7 +168,7 @@ async fn run_remote_compact_task_inner_impl(
turn_context: &Arc<TurnContext>,
initial_context_injection: InitialContextInjection,
compaction_metadata: CompactionTurnMetadata,
active_context_tokens_before: &mut i64,
analytics_details: &mut CompactionAnalyticsDetails,
) -> CodexResult<()> {
let context_compaction_item = ContextCompactionItem::new();
// Use the UI compaction item ID as the trace compaction ID so protocol lifecycle events,
Expand Down Expand Up @@ -208,8 +202,12 @@ async fn run_remote_compact_task_inner_impl(
.get_total_token_usage_breakdown()
.await
.estimated_tokens_of_items_added_since_last_successful_api_response;
*active_context_tokens_before = (*active_context_tokens_before)
.saturating_sub(estimated_deleted_tokens.min(max_local_deleted_tokens));
analytics_details.active_context_tokens_before = analytics_details
.active_context_tokens_before
.map(|active_context_tokens_before| {
active_context_tokens_before
.saturating_sub(estimated_deleted_tokens.min(max_local_deleted_tokens))
});
}
// This is the history selected for remote compaction, after any output rewriting required to
// fit the compact endpoint. The checkpoint below records it separately from the next sampling
Expand Down
Loading
Loading