mirror of
https://github.com/openai/codex.git
synced 2026-05-19 10:43:38 +00:00
CXC-392 [With 401](https://openai.sentry.io/issues/7333870443/?project=4510195390611458&query=019ce8f8-560c-7f10-a00a-c59553740674&referrer=issue-stream) <img width="1909" height="555" alt="401 auth tags in Sentry" src="https://github.com/user-attachments/assets/412ea950-61c4-4780-9697-15c270971ee3" /> - auth_401_*: preserved facts from the latest unauthorized response snapshot - auth_*: latest auth-related facts from the latest request attempt - auth_recovery_*: unauthorized recovery state and follow-up result Without 401 <img width="1917" height="522" alt="happy-path auth tags in Sentry" src="https://github.com/user-attachments/assets/3381ed28-8022-43b0-b6c0-623a630e679f" /> ###### Summary - Add client-visible 401 diagnostics for auth attachment, upstream auth classification, and 401 request id / cf-ray correlation. - Record unauthorized recovery mode, phase, outcome, and retry/follow-up status without changing auth behavior. - Surface the highest-signal auth and recovery fields on uploaded client bug reports so they are usable in Sentry. - Preserve original unauthorized evidence under `auth_401_*` while keeping follow-up result tags separate. ###### Rationale (from spec findings) - The dominant bucket needed proof of whether the client attached auth before send or upstream still classified the request as missing auth. - Client uploads needed to show whether unauthorized recovery ran and what the client tried next. - Request id and cf-ray needed to be preserved on the unauthorized response so server-side correlation is immediate. - The bug-report path needed the same auth evidence as the request telemetry path, otherwise the observability would not be operationally useful. ###### Scope - Add auth 401 and unauthorized-recovery observability in `codex-rs/core`, `codex-rs/codex-api`, and `codex-rs/otel`, including feedback-tag surfacing. - Keep auth semantics, refresh behavior, retry behavior, endpoint classification, and geo-denial follow-up work out of this PR. ###### Trade-offs - This exports only safe auth evidence: header presence/name, upstream auth classification, request ids, and recovery state. It does not export token values or raw upstream bodies. - This keeps websocket connection reuse as a transport clue because it can help distinguish stale reused sessions from fresh reconnects. - Misroute/base-url classification and geo-denial are intentionally deferred to a separate follow-up PR so this review stays focused on the dominant auth 401 bucket. ###### Client follow-up - PR 2 will add misroute/provider and geo-denial observability plus the matching feedback-tag surfacing. - A separate host/app-server PR should log auth-decision inputs so pre-send host auth state can be correlated with client request evidence. - `device_id` remains intentionally separate until there is a safe existing source on the feedback upload path. ###### Testing - `cargo test -p codex-core refresh_available_models_sorts_by_priority` - `cargo test -p codex-core emit_feedback_request_tags_` - `cargo test -p codex-core emit_feedback_auth_recovery_tags_` - `cargo test -p codex-core auth_request_telemetry_context_tracks_attached_auth_and_retry_phase` - `cargo test -p codex-core extract_response_debug_context_decodes_identity_headers` - `cargo test -p codex-core identity_auth_details` - `cargo test -p codex-core telemetry_error_messages_preserve_non_http_details` - `cargo test -p codex-core --all-features --no-run` - `cargo test -p codex-otel otel_export_routing_policy_routes_api_request_auth_observability` - `cargo test -p codex-otel otel_export_routing_policy_routes_websocket_connect_auth_observability` - `cargo test -p codex-otel otel_export_routing_policy_routes_websocket_request_transport_observability`
384 lines
12 KiB
Rust
384 lines
12 KiB
Rust
use super::*;
|
|
use std::collections::BTreeMap;
|
|
use std::sync::Arc;
|
|
use std::sync::Mutex;
|
|
use tracing::Event;
|
|
use tracing::Subscriber;
|
|
use tracing::field::Visit;
|
|
use tracing_subscriber::Layer;
|
|
use tracing_subscriber::layer::Context;
|
|
use tracing_subscriber::layer::SubscriberExt;
|
|
use tracing_subscriber::registry::LookupSpan;
|
|
use tracing_subscriber::util::SubscriberInitExt;
|
|
|
|
#[test]
|
|
fn test_try_parse_error_message() {
|
|
let text = r#"{
|
|
"error": {
|
|
"message": "Your refresh token has already been used to generate a new access token. Please try signing in again.",
|
|
"type": "invalid_request_error",
|
|
"param": null,
|
|
"code": "refresh_token_reused"
|
|
}
|
|
}"#;
|
|
let message = try_parse_error_message(text);
|
|
assert_eq!(
|
|
message,
|
|
"Your refresh token has already been used to generate a new access token. Please try signing in again."
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_try_parse_error_message_no_error() {
|
|
let text = r#"{"message": "test"}"#;
|
|
let message = try_parse_error_message(text);
|
|
assert_eq!(message, r#"{"message": "test"}"#);
|
|
}
|
|
|
|
#[test]
|
|
fn feedback_tags_macro_compiles() {
|
|
#[derive(Debug)]
|
|
struct OnlyDebug;
|
|
|
|
feedback_tags!(model = "gpt-5", cached = true, debug_only = OnlyDebug);
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct TagCollectorVisitor {
|
|
tags: BTreeMap<String, String>,
|
|
}
|
|
|
|
impl Visit for TagCollectorVisitor {
|
|
fn record_bool(&mut self, field: &tracing::field::Field, value: bool) {
|
|
self.tags
|
|
.insert(field.name().to_string(), value.to_string());
|
|
}
|
|
|
|
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
|
|
self.tags
|
|
.insert(field.name().to_string(), value.to_string());
|
|
}
|
|
|
|
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
|
|
self.tags
|
|
.insert(field.name().to_string(), format!("{value:?}"));
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
struct TagCollectorLayer {
|
|
tags: Arc<Mutex<BTreeMap<String, String>>>,
|
|
}
|
|
|
|
impl<S> Layer<S> for TagCollectorLayer
|
|
where
|
|
S: Subscriber + for<'a> LookupSpan<'a>,
|
|
{
|
|
fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) {
|
|
if event.metadata().target() != "feedback_tags" {
|
|
return;
|
|
}
|
|
let mut visitor = TagCollectorVisitor::default();
|
|
event.record(&mut visitor);
|
|
self.tags.lock().unwrap().extend(visitor.tags);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn emit_feedback_request_tags_records_sentry_feedback_fields() {
|
|
let tags = Arc::new(Mutex::new(BTreeMap::new()));
|
|
let _guard = tracing_subscriber::registry()
|
|
.with(TagCollectorLayer { tags: tags.clone() })
|
|
.set_default();
|
|
|
|
emit_feedback_request_tags(&FeedbackRequestTags {
|
|
endpoint: "/responses",
|
|
auth_header_attached: true,
|
|
auth_header_name: Some("authorization"),
|
|
auth_mode: Some("chatgpt"),
|
|
auth_retry_after_unauthorized: Some(false),
|
|
auth_recovery_mode: Some("managed"),
|
|
auth_recovery_phase: Some("refresh_token"),
|
|
auth_connection_reused: Some(true),
|
|
auth_request_id: Some("req-123"),
|
|
auth_cf_ray: Some("ray-123"),
|
|
auth_error: Some("missing_authorization_header"),
|
|
auth_error_code: Some("token_expired"),
|
|
auth_recovery_followup_success: Some(true),
|
|
auth_recovery_followup_status: Some(200),
|
|
});
|
|
|
|
let tags = tags.lock().unwrap().clone();
|
|
assert_eq!(
|
|
tags.get("endpoint").map(String::as_str),
|
|
Some("\"/responses\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_header_attached").map(String::as_str),
|
|
Some("true")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_header_name").map(String::as_str),
|
|
Some("\"authorization\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_request_id").map(String::as_str),
|
|
Some("\"req-123\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_error_code").map(String::as_str),
|
|
Some("\"token_expired\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_recovery_followup_success")
|
|
.map(String::as_str),
|
|
Some("\"true\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_recovery_followup_status")
|
|
.map(String::as_str),
|
|
Some("\"200\"")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn emit_feedback_auth_recovery_tags_preserves_401_specific_fields() {
|
|
let tags = Arc::new(Mutex::new(BTreeMap::new()));
|
|
let _guard = tracing_subscriber::registry()
|
|
.with(TagCollectorLayer { tags: tags.clone() })
|
|
.set_default();
|
|
|
|
emit_feedback_auth_recovery_tags(
|
|
"managed",
|
|
"refresh_token",
|
|
"recovery_succeeded",
|
|
Some("req-401"),
|
|
Some("ray-401"),
|
|
Some("missing_authorization_header"),
|
|
Some("token_expired"),
|
|
);
|
|
|
|
let tags = tags.lock().unwrap().clone();
|
|
assert_eq!(
|
|
tags.get("auth_401_request_id").map(String::as_str),
|
|
Some("\"req-401\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_401_cf_ray").map(String::as_str),
|
|
Some("\"ray-401\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_401_error").map(String::as_str),
|
|
Some("\"missing_authorization_header\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_401_error_code").map(String::as_str),
|
|
Some("\"token_expired\"")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn emit_feedback_auth_recovery_tags_clears_stale_401_fields() {
|
|
let tags = Arc::new(Mutex::new(BTreeMap::new()));
|
|
let _guard = tracing_subscriber::registry()
|
|
.with(TagCollectorLayer { tags: tags.clone() })
|
|
.set_default();
|
|
|
|
emit_feedback_auth_recovery_tags(
|
|
"managed",
|
|
"refresh_token",
|
|
"recovery_failed_transient",
|
|
Some("req-401-a"),
|
|
Some("ray-401-a"),
|
|
Some("missing_authorization_header"),
|
|
Some("token_expired"),
|
|
);
|
|
emit_feedback_auth_recovery_tags(
|
|
"managed",
|
|
"done",
|
|
"recovery_not_run",
|
|
Some("req-401-b"),
|
|
None,
|
|
None,
|
|
None,
|
|
);
|
|
|
|
let tags = tags.lock().unwrap().clone();
|
|
assert_eq!(
|
|
tags.get("auth_401_request_id").map(String::as_str),
|
|
Some("\"req-401-b\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_401_cf_ray").map(String::as_str),
|
|
Some("\"\"")
|
|
);
|
|
assert_eq!(tags.get("auth_401_error").map(String::as_str), Some("\"\""));
|
|
assert_eq!(
|
|
tags.get("auth_401_error_code").map(String::as_str),
|
|
Some("\"\"")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn emit_feedback_request_tags_preserves_latest_auth_fields_after_unauthorized() {
|
|
let tags = Arc::new(Mutex::new(BTreeMap::new()));
|
|
let _guard = tracing_subscriber::registry()
|
|
.with(TagCollectorLayer { tags: tags.clone() })
|
|
.set_default();
|
|
|
|
emit_feedback_request_tags(&FeedbackRequestTags {
|
|
endpoint: "/responses",
|
|
auth_header_attached: true,
|
|
auth_header_name: Some("authorization"),
|
|
auth_mode: Some("chatgpt"),
|
|
auth_retry_after_unauthorized: Some(true),
|
|
auth_recovery_mode: Some("managed"),
|
|
auth_recovery_phase: Some("refresh_token"),
|
|
auth_connection_reused: None,
|
|
auth_request_id: Some("req-123"),
|
|
auth_cf_ray: Some("ray-123"),
|
|
auth_error: Some("missing_authorization_header"),
|
|
auth_error_code: Some("token_expired"),
|
|
auth_recovery_followup_success: Some(false),
|
|
auth_recovery_followup_status: Some(401),
|
|
});
|
|
|
|
let tags = tags.lock().unwrap().clone();
|
|
assert_eq!(
|
|
tags.get("auth_request_id").map(String::as_str),
|
|
Some("\"req-123\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_cf_ray").map(String::as_str),
|
|
Some("\"ray-123\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_error").map(String::as_str),
|
|
Some("\"missing_authorization_header\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_error_code").map(String::as_str),
|
|
Some("\"token_expired\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_recovery_followup_success")
|
|
.map(String::as_str),
|
|
Some("\"false\"")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn emit_feedback_request_tags_clears_stale_latest_auth_fields() {
|
|
let tags = Arc::new(Mutex::new(BTreeMap::new()));
|
|
let _guard = tracing_subscriber::registry()
|
|
.with(TagCollectorLayer { tags: tags.clone() })
|
|
.set_default();
|
|
|
|
emit_feedback_request_tags(&FeedbackRequestTags {
|
|
endpoint: "/responses",
|
|
auth_header_attached: true,
|
|
auth_header_name: Some("authorization"),
|
|
auth_mode: Some("chatgpt"),
|
|
auth_retry_after_unauthorized: Some(false),
|
|
auth_recovery_mode: Some("managed"),
|
|
auth_recovery_phase: Some("refresh_token"),
|
|
auth_connection_reused: Some(true),
|
|
auth_request_id: Some("req-123"),
|
|
auth_cf_ray: Some("ray-123"),
|
|
auth_error: Some("missing_authorization_header"),
|
|
auth_error_code: Some("token_expired"),
|
|
auth_recovery_followup_success: Some(true),
|
|
auth_recovery_followup_status: Some(200),
|
|
});
|
|
emit_feedback_request_tags(&FeedbackRequestTags {
|
|
endpoint: "/responses",
|
|
auth_header_attached: true,
|
|
auth_header_name: None,
|
|
auth_mode: None,
|
|
auth_retry_after_unauthorized: None,
|
|
auth_recovery_mode: None,
|
|
auth_recovery_phase: None,
|
|
auth_connection_reused: None,
|
|
auth_request_id: None,
|
|
auth_cf_ray: None,
|
|
auth_error: None,
|
|
auth_error_code: None,
|
|
auth_recovery_followup_success: None,
|
|
auth_recovery_followup_status: None,
|
|
});
|
|
|
|
let tags = tags.lock().unwrap().clone();
|
|
assert_eq!(
|
|
tags.get("auth_header_name").map(String::as_str),
|
|
Some("\"\"")
|
|
);
|
|
assert_eq!(tags.get("auth_mode").map(String::as_str), Some("\"\""));
|
|
assert_eq!(
|
|
tags.get("auth_request_id").map(String::as_str),
|
|
Some("\"\"")
|
|
);
|
|
assert_eq!(tags.get("auth_cf_ray").map(String::as_str), Some("\"\""));
|
|
assert_eq!(tags.get("auth_error").map(String::as_str), Some("\"\""));
|
|
assert_eq!(
|
|
tags.get("auth_error_code").map(String::as_str),
|
|
Some("\"\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_recovery_followup_success")
|
|
.map(String::as_str),
|
|
Some("\"\"")
|
|
);
|
|
assert_eq!(
|
|
tags.get("auth_recovery_followup_status")
|
|
.map(String::as_str),
|
|
Some("\"\"")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn normalize_thread_name_trims_and_rejects_empty() {
|
|
assert_eq!(normalize_thread_name(" "), None);
|
|
assert_eq!(
|
|
normalize_thread_name(" my thread "),
|
|
Some("my thread".to_string())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn resume_command_prefers_name_over_id() {
|
|
let thread_id = ThreadId::from_string("123e4567-e89b-12d3-a456-426614174000").unwrap();
|
|
let command = resume_command(Some("my-thread"), Some(thread_id));
|
|
assert_eq!(command, Some("codex resume my-thread".to_string()));
|
|
}
|
|
|
|
#[test]
|
|
fn resume_command_with_only_id() {
|
|
let thread_id = ThreadId::from_string("123e4567-e89b-12d3-a456-426614174000").unwrap();
|
|
let command = resume_command(None, Some(thread_id));
|
|
assert_eq!(
|
|
command,
|
|
Some("codex resume 123e4567-e89b-12d3-a456-426614174000".to_string())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn resume_command_with_no_name_or_id() {
|
|
let command = resume_command(None, None);
|
|
assert_eq!(command, None);
|
|
}
|
|
|
|
#[test]
|
|
fn resume_command_quotes_thread_name_when_needed() {
|
|
let command = resume_command(Some("-starts-with-dash"), None);
|
|
assert_eq!(
|
|
command,
|
|
Some("codex resume -- -starts-with-dash".to_string())
|
|
);
|
|
|
|
let command = resume_command(Some("two words"), None);
|
|
assert_eq!(command, Some("codex resume 'two words'".to_string()));
|
|
|
|
let command = resume_command(Some("quote'case"), None);
|
|
assert_eq!(command, Some("codex resume \"quote'case\"".to_string()));
|
|
}
|