Lightweight coding agent that runs in your terminal
Rust
core: preserve last model ids in feedback tags (#21026)
sayan-oai8126af38799f
diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rsindex c60c4507d7..d11fda47dc 100644--- a/codex-rs/core/src/client.rs+++ b/codex-rs/core/src/client.rs@@ -107,6 +107,7 @@ use tracing::warn;use crate::client_common::Prompt;use crate::client_common::ResponseEvent;use crate::client_common::ResponseStream;+use crate::feedback_tags;use crate::flags::CODEX_RS_SSE_FIXTURE;use crate::util::emit_feedback_auth_recovery_tags;use codex_api::map_api_error;@@ -1685,6 +1686,9 @@ wherelet mut items_added: Vec<ResponseItem> = Vec::new();let mut api_stream = api_stream;let upstream_request_id = upstream_request_id.as_deref();+ if let Some(upstream_request_id) = upstream_request_id {+ feedback_tags!(last_model_request_id = upstream_request_id);+ }loop {let event = tokio::select! {_ = consumer_dropped.cancelled() => {@@ -1721,6 +1725,7 @@ wheretoken_usage,end_turn,}) => {+ feedback_tags!(last_model_response_id = &response_id);if let Some(usage) = &token_usage {session_telemetry.sse_event_completed(usage.input_tokens,@@ -1769,6 +1774,9 @@ whereextract_response_debug_context_from_api_error(&err);let upstream_request_id =upstream_request_id.or(response_debug_context.request_id.as_deref());+ if let Some(upstream_request_id) = upstream_request_id {+ feedback_tags!(last_model_request_id = upstream_request_id);+ }let mapped = map_api_error(err);inference_trace_attempt.record_failed(&mapped,diff --git a/codex-rs/core/src/client_tests.rs b/codex-rs/core/src/client_tests.rsindex e56500ba5f..13c5603baa 100644--- a/codex-rs/core/src/client_tests.rs+++ b/codex-rs/core/src/client_tests.rs@@ -31,14 +31,24 @@ use codex_rollout_trace::replay_bundle;use futures::StreamExt;use pretty_assertions::assert_eq;use serde_json::json;+use std::collections::BTreeMap;use std::collections::VecDeque;use std::pin::Pin;use std::sync::Arc;+use std::sync::Mutex;use std::task::Context;use std::task::Poll;use std::time::Duration;use tempfile::TempDir;use tokio::sync::Notify;+use tracing::Event;+use tracing::Subscriber;+use tracing::field::Visit;+use tracing_subscriber::Layer;+use tracing_subscriber::layer::Context as LayerContext;+use tracing_subscriber::layer::SubscriberExt;+use tracing_subscriber::registry::LookupSpan;+use tracing_subscriber::util::SubscriberInitExt;fn test_model_client(session_source: SessionSource) -> ModelClient {let provider = create_oss_provider_with_base_url("https://example.com/v1", WireApi::Responses);@@ -100,6 +110,42 @@ fn test_session_telemetry() -> SessionTelemetry {)}+#[derive(Default)]+struct TagCollectorVisitor {+ tags: BTreeMap<String, String>,+}++impl Visit for TagCollectorVisitor {+ 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: LayerContext<'_, S>) {+ if event.metadata().target() != "feedback_tags" {+ return;+ }+ let mut visitor = TagCollectorVisitor::default();+ event.record(&mut visitor);+ self.tags.lock().unwrap().extend(visitor.tags);+ }+}+fn started_inference_attempt(temp: &TempDir) -> anyhow::Result<InferenceTraceAttempt> {let writer = Arc::new(TraceWriter::create(temp.path(),@@ -316,6 +362,41 @@ async fn dropped_response_stream_traces_cancelled_partial_output() -> anyhow::ReOk(())}+#[tokio::test]+async fn response_stream_records_last_model_feedback_ids() {+ let tags = Arc::new(Mutex::new(BTreeMap::new()));+ let _guard = tracing_subscriber::registry()+ .with(TagCollectorLayer { tags: tags.clone() })+ .set_default();++ let api_stream = futures::stream::iter([+ Ok(ResponseEvent::Created),+ Ok(ResponseEvent::Completed {+ response_id: "resp-123".to_string(),+ token_usage: None,+ end_turn: Some(true),+ }),+ ]);+ let (mut stream, _) = super::map_response_events(+ Some("req-123".to_string()),+ api_stream,+ test_session_telemetry(),+ InferenceTraceAttempt::disabled(),+ );++ while stream.next().await.is_some() {}++ let tags = tags.lock().unwrap().clone();+ assert_eq!(+ tags.get("last_model_request_id").map(String::as_str),+ Some("\"req-123\"")+ );+ assert_eq!(+ tags.get("last_model_response_id").map(String::as_str),+ Some("\"resp-123\"")+ );+}+#[tokio::test]async fn dropped_backpressured_response_stream_traces_cancelled_partial_output()-> anyhow::Result<()> {