mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Preserve usage limit windows in turn and compaction analytics (#48174)
## Why Usage limit errors discarded the server-selected window responsible for the limit, leaving turn and compaction analytics unable to distinguish five-hour limits from weekly limits. ## What changed Preserve optional `limit_window_minutes` from usage limit error responses in `UsageLimitReachedError` and report it as `usage_limit_window_minutes` in turn and compaction events. Accept unsigned integers that fit in `u16`; treat missing, null, malformed, or out-of-range values as unknown. Events for other error kinds report `null`. ## Testing Add coverage for HTTP and wrapped WebSocket error mapping, analytics serialization and reduction, and app-server propagation to turn and local/remote compaction events. Cases include five-hour and weekly windows, missing or invalid values, and unrelated errors. GitOrigin-RevId: a32066a007559d91a3c868a756bfd0f976777176
This commit is contained in:
@@ -878,6 +878,7 @@ async fn guardian_events_keep_thread_source_and_originator_with_explicit_turn_co
|
||||
status: CompactionStatus::Completed,
|
||||
codex_error_kind: None,
|
||||
codex_error_http_status_code: None,
|
||||
usage_limit_window_minutes: None,
|
||||
active_context_tokens_before: 131_000,
|
||||
active_context_tokens_after: 64_000,
|
||||
retained_image_count: None,
|
||||
|
||||
@@ -398,6 +398,7 @@ async fn thread_originator_overrides_shared_connection_across_thread_events() {
|
||||
status: CompactionStatus::Completed,
|
||||
codex_error_kind: None,
|
||||
codex_error_http_status_code: None,
|
||||
usage_limit_window_minutes: None,
|
||||
active_context_tokens_before: 131_000,
|
||||
active_context_tokens_after: 64_000,
|
||||
retained_image_count: None,
|
||||
|
||||
@@ -45,6 +45,7 @@ fn compaction_event_serializes_expected_shape() {
|
||||
status: CompactionStatus::Completed,
|
||||
codex_error_kind: None,
|
||||
codex_error_http_status_code: None,
|
||||
usage_limit_window_minutes: None,
|
||||
active_context_tokens_before: 120_000,
|
||||
active_context_tokens_after: 18_000,
|
||||
retained_image_count: None,
|
||||
@@ -98,6 +99,7 @@ fn compaction_event_serializes_expected_shape() {
|
||||
"status": "completed",
|
||||
"codex_error_kind": null,
|
||||
"codex_error_http_status_code": null,
|
||||
"usage_limit_window_minutes": null,
|
||||
"active_context_tokens_before": 120000,
|
||||
"active_context_tokens_after": 18000,
|
||||
"retained_image_count": null,
|
||||
@@ -194,6 +196,7 @@ async fn compaction_event_ingests_custom_fact() {
|
||||
status: CompactionStatus::Failed,
|
||||
codex_error_kind: Some(CodexErrKind::ContextWindowExceeded),
|
||||
codex_error_http_status_code: None,
|
||||
usage_limit_window_minutes: None,
|
||||
active_context_tokens_before: 131_000,
|
||||
active_context_tokens_after: 131_000,
|
||||
retained_image_count: None,
|
||||
|
||||
@@ -38,6 +38,7 @@ use codex_app_server_protocol::ThreadRealtimeStartedNotification;
|
||||
use codex_app_server_protocol::TurnStatus as AppServerTurnStatus;
|
||||
use codex_app_server_protocol::WebSearchItem;
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::UsageLimitReachedError;
|
||||
use codex_protocol::protocol::RealtimeConversationVersion;
|
||||
use codex_protocol::protocol::ThreadSource;
|
||||
use pretty_assertions::assert_eq;
|
||||
@@ -148,6 +149,7 @@ fn turn_event_serializes_expected_shape() {
|
||||
turn_error: None,
|
||||
codex_error_kind: None,
|
||||
codex_error_http_status_code: None,
|
||||
usage_limit_window_minutes: None,
|
||||
steer_count: Some(0),
|
||||
total_tool_call_count: None,
|
||||
shell_command_count: None,
|
||||
@@ -238,6 +240,7 @@ fn turn_event_serializes_expected_shape() {
|
||||
"turn_error": null,
|
||||
"codex_error_kind": null,
|
||||
"codex_error_http_status_code": null,
|
||||
"usage_limit_window_minutes": null,
|
||||
"steer_count": 0,
|
||||
"total_tool_call_count": null,
|
||||
"shell_command_count": null,
|
||||
@@ -811,6 +814,97 @@ async fn turn_lifecycle_emits_failed_turn_event() {
|
||||
payload["event_params"]["codex_error_http_status_code"],
|
||||
json!(null)
|
||||
);
|
||||
assert_eq!(
|
||||
payload["event_params"]["usage_limit_window_minutes"],
|
||||
json!(null)
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn turn_event_reports_decisive_usage_limit_window() {
|
||||
let usage_limit = |limit_window_minutes| {
|
||||
CodexErr::UsageLimitReached(UsageLimitReachedError {
|
||||
plan_type: None,
|
||||
resets_at: None,
|
||||
rate_limits: None,
|
||||
promo_message: None,
|
||||
rate_limit_reached_type: None,
|
||||
limit_window_minutes,
|
||||
})
|
||||
};
|
||||
let cases = [
|
||||
(
|
||||
usage_limit(Some(300)),
|
||||
codex_app_server_protocol::CodexErrorInfo::UsageLimitExceeded,
|
||||
json!(["usageLimitExceeded", "usage_limit_reached", 300]),
|
||||
),
|
||||
(
|
||||
usage_limit(Some(10_080)),
|
||||
codex_app_server_protocol::CodexErrorInfo::UsageLimitExceeded,
|
||||
json!(["usageLimitExceeded", "usage_limit_reached", 10080]),
|
||||
),
|
||||
(
|
||||
usage_limit(None),
|
||||
codex_app_server_protocol::CodexErrorInfo::UsageLimitExceeded,
|
||||
json!(["usageLimitExceeded", "usage_limit_reached", null]),
|
||||
),
|
||||
(
|
||||
CodexErr::InvalidRequest("invalid request".to_string()),
|
||||
codex_app_server_protocol::CodexErrorInfo::BadRequest,
|
||||
json!(["badRequest", "invalid_request", null]),
|
||||
),
|
||||
];
|
||||
|
||||
for (error, error_info, expected) in cases {
|
||||
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::TurnCodexError(Box::new(
|
||||
TurnCodexErrorFact::from_codex_err(
|
||||
"thread-2".to_string(),
|
||||
"turn-2".to_string(),
|
||||
&error,
|
||||
),
|
||||
))),
|
||||
&mut out,
|
||||
)
|
||||
.await;
|
||||
reducer
|
||||
.ingest(
|
||||
AnalyticsFact::Notification(Box::new(sample_turn_completed_notification(
|
||||
"thread-2",
|
||||
"turn-2",
|
||||
AppServerTurnStatus::Failed,
|
||||
Some(error_info),
|
||||
))),
|
||||
&mut out,
|
||||
)
|
||||
.await;
|
||||
|
||||
let [event] = out.as_slice() else {
|
||||
panic!("expected one turn event");
|
||||
};
|
||||
let payload = serde_json::to_value(event).expect("serialize turn event");
|
||||
let params = &payload["event_params"];
|
||||
assert_eq!(
|
||||
json!([
|
||||
params["turn_error"],
|
||||
params["codex_error_kind"],
|
||||
params["usage_limit_window_minutes"],
|
||||
]),
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user