mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
codex: address PR review feedback (#31729)
This commit is contained in:
@@ -97,7 +97,3 @@ pub struct JSONRPCErrorError {
|
||||
pub data: Option<serde_json::Value>,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "rpc_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
use codex_protocol::protocol::W3cTraceContext;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::JSONRPCNotification;
|
||||
|
||||
#[test]
|
||||
fn notification_trace_is_optional_and_omitted_when_absent() {
|
||||
let notification: JSONRPCNotification = serde_json::from_value(serde_json::json!({
|
||||
"method": "initialized",
|
||||
"params": {}
|
||||
}))
|
||||
.expect("notification without trace should deserialize");
|
||||
|
||||
assert_eq!(notification.trace, None);
|
||||
assert_eq!(
|
||||
serde_json::to_value(notification).expect("notification should serialize"),
|
||||
serde_json::json!({
|
||||
"method": "initialized",
|
||||
"params": {}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn notification_trace_round_trips() {
|
||||
let trace = W3cTraceContext {
|
||||
traceparent: Some("00-00000000000000000000000000000001-0000000000000002-01".to_string()),
|
||||
tracestate: Some("vendor=value".to_string()),
|
||||
};
|
||||
let notification = JSONRPCNotification {
|
||||
method: "process/exited".to_string(),
|
||||
params: Some(serde_json::json!({"processId": "process-1"})),
|
||||
trace: Some(trace.clone()),
|
||||
};
|
||||
|
||||
let encoded = serde_json::to_value(¬ification).expect("notification should serialize");
|
||||
assert_eq!(
|
||||
encoded["trace"],
|
||||
serde_json::to_value(trace).expect("trace should serialize")
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_value::<JSONRPCNotification>(encoded)
|
||||
.expect("notification should deserialize"),
|
||||
notification
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_notification_trace_is_ignored() {
|
||||
for trace in [serde_json::json!(7), serde_json::json!({"traceparent": 7})] {
|
||||
let notification: JSONRPCNotification = serde_json::from_value(serde_json::json!({
|
||||
"method": "process/exited",
|
||||
"params": {},
|
||||
"trace": trace,
|
||||
}))
|
||||
.expect("notification with malformed trace should deserialize");
|
||||
|
||||
assert_eq!(notification.trace, None);
|
||||
}
|
||||
}
|
||||
@@ -29,9 +29,6 @@ use tokio::time::timeout;
|
||||
use crate::connection::JsonRpcConnection;
|
||||
use crate::connection::JsonRpcConnectionEvent;
|
||||
use crate::connection::JsonRpcTransport;
|
||||
use crate::protocol::EXEC_CLOSED_METHOD;
|
||||
use crate::protocol::EXEC_EXITED_METHOD;
|
||||
use crate::protocol::HTTP_REQUEST_BODY_DELTA_METHOD;
|
||||
|
||||
pub(crate) const SESSION_ALREADY_ATTACHED_ERROR_CODE: i64 = -32010;
|
||||
const MAX_IN_FLIGHT_REGULAR_CALLS: usize = 1024;
|
||||
@@ -110,17 +107,12 @@ impl RpcNotificationSender {
|
||||
params: &P,
|
||||
) -> Result<(), JSONRPCErrorError> {
|
||||
let params = serde_json::to_value(params).map_err(|err| internal_error(err.to_string()))?;
|
||||
let trace = if should_trace_server_notification(method, ¶ms) {
|
||||
codex_otel::current_span_w3c_trace_context()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
self.outgoing_tx
|
||||
.send(RpcServerOutboundMessage::Notification(
|
||||
JSONRPCNotification {
|
||||
method: method.to_string(),
|
||||
params: Some(params),
|
||||
trace,
|
||||
trace: codex_otel::current_span_w3c_trace_context(),
|
||||
},
|
||||
))
|
||||
.await
|
||||
@@ -128,22 +120,6 @@ impl RpcNotificationSender {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn should_trace_server_notification(method: &str, params: &Value) -> bool {
|
||||
// Output and nonterminal body deltas can be extremely frequent. Carry and
|
||||
// consume trace context only for lifecycle boundaries with bounded volume.
|
||||
match method {
|
||||
EXEC_EXITED_METHOD | EXEC_CLOSED_METHOD => true,
|
||||
HTTP_REQUEST_BODY_DELTA_METHOD => {
|
||||
let Some(params) = params.as_object() else {
|
||||
return false;
|
||||
};
|
||||
params.get("done").and_then(Value::as_bool) == Some(true)
|
||||
|| params.get("error").is_some_and(|error| !error.is_null())
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct RpcRouter<S> {
|
||||
request_routes: HashMap<&'static str, RequestRoute<S>>,
|
||||
notification_routes: HashMap<&'static str, NotificationRoute<S>>,
|
||||
@@ -756,8 +732,6 @@ mod tests {
|
||||
use super::MAX_IN_FLIGHT_REGULAR_CALLS;
|
||||
use super::RpcCallError;
|
||||
use super::RpcClient;
|
||||
use super::RpcNotificationSender;
|
||||
use super::RpcServerOutboundMessage;
|
||||
use crate::connection::JsonRpcConnection;
|
||||
use crate::connection::JsonRpcConnectionEvent;
|
||||
use crate::connection::JsonRpcTransport;
|
||||
@@ -1188,67 +1162,4 @@ mod tests {
|
||||
assert_ne!(parts[2], expected_parts[2]);
|
||||
assert_eq!(trace.tracestate, expected_trace.tracestate);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
#[serial_test::serial(exec_server_tracing)]
|
||||
async fn notification_senders_propagate_current_trace_context() {
|
||||
let span_exporter = InMemorySpanExporter::default();
|
||||
let tracer_provider = SdkTracerProvider::builder()
|
||||
.with_simple_exporter(span_exporter)
|
||||
.build();
|
||||
let tracer = tracer_provider.tracer("exec-server-test");
|
||||
let subscriber = tracing_subscriber::registry().with(
|
||||
tracing_opentelemetry::layer()
|
||||
.with_tracer(tracer)
|
||||
.with_filter(filter_fn(codex_otel::OtelProvider::trace_export_filter)),
|
||||
);
|
||||
let _subscriber_guard = tracing::subscriber::set_default(subscriber);
|
||||
tracing::callsite::rebuild_interest_cache();
|
||||
let parent_span = tracing::info_span!("outbound-notification-parent");
|
||||
let expected_trace = codex_otel::span_w3c_trace_context(&parent_span)
|
||||
.expect("parent span should have trace context");
|
||||
|
||||
let (outgoing_tx, mut outgoing_rx) = tokio::sync::mpsc::channel(/*buffer*/ 1);
|
||||
let sender = RpcNotificationSender::new(outgoing_tx);
|
||||
sender
|
||||
.notify("process/exited", &serde_json::json!({}))
|
||||
.instrument(parent_span.clone())
|
||||
.await
|
||||
.expect("server notification should send");
|
||||
let server_trace = match outgoing_rx.recv().await {
|
||||
Some(RpcServerOutboundMessage::Notification(notification)) => notification.trace,
|
||||
other => panic!("expected server notification, got {other:?}"),
|
||||
}
|
||||
.expect("server notification trace context");
|
||||
sender
|
||||
.notify("process/output", &serde_json::json!({}))
|
||||
.instrument(parent_span.clone())
|
||||
.await
|
||||
.expect("hot-path server notification should send");
|
||||
let hot_path_trace = match outgoing_rx.recv().await {
|
||||
Some(RpcServerOutboundMessage::Notification(notification)) => notification.trace,
|
||||
other => panic!("expected server notification, got {other:?}"),
|
||||
};
|
||||
|
||||
let (client_stdin, server_reader) = tokio::io::duplex(4096);
|
||||
let (_server_writer, client_stdout) = tokio::io::duplex(4096);
|
||||
let connection =
|
||||
JsonRpcConnection::from_stdio(client_stdout, client_stdin, "test-rpc".to_string());
|
||||
let (client, _events_rx) = RpcClient::new(connection);
|
||||
client
|
||||
.notify("initialized", &serde_json::json!({}))
|
||||
.instrument(parent_span)
|
||||
.await
|
||||
.expect("client notification should send");
|
||||
let mut lines = BufReader::new(server_reader).lines();
|
||||
let client_trace = match read_jsonrpc_line(&mut lines).await {
|
||||
JSONRPCMessage::Notification(notification) => notification.trace,
|
||||
other => panic!("expected client notification, got {other:?}"),
|
||||
}
|
||||
.expect("client notification trace context");
|
||||
|
||||
assert_eq!(server_trace, expected_trace);
|
||||
assert_eq!(hot_path_trace, None);
|
||||
assert_eq!(client_trace, expected_trace);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,7 +350,6 @@ mod tests {
|
||||
|
||||
use super::request_span;
|
||||
use super::run_connection;
|
||||
use super::set_notification_span_parent;
|
||||
use crate::ExecServerRuntimePaths;
|
||||
use crate::ProcessId;
|
||||
use crate::connection::JsonRpcConnection;
|
||||
@@ -426,58 +425,6 @@ mod tests {
|
||||
assert_eq!(request_span.parent_span_id, parent_span_id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial_test::serial(exec_server_tracing)]
|
||||
fn notification_span_uses_inbound_trace_parent() {
|
||||
let span_exporter = InMemorySpanExporter::default();
|
||||
let tracer_provider = SdkTracerProvider::builder()
|
||||
.with_simple_exporter(span_exporter.clone())
|
||||
.build();
|
||||
let tracer = tracer_provider.tracer("exec-server-test");
|
||||
let subscriber = tracing_subscriber::registry().with(
|
||||
tracing_opentelemetry::layer()
|
||||
.with_tracer(tracer)
|
||||
.with_filter(filter_fn(codex_otel::OtelProvider::trace_export_filter)),
|
||||
);
|
||||
let trace_id = TraceId::from_hex("00000000000000000000000000000001").expect("trace id");
|
||||
let parent_span_id = SpanId::from_hex("0000000000000002").expect("span id");
|
||||
let notification = JSONRPCNotification {
|
||||
method: INITIALIZED_METHOD.to_string(),
|
||||
params: None,
|
||||
trace: Some(codex_protocol::protocol::W3cTraceContext {
|
||||
traceparent: Some(format!("00-{trace_id}-{parent_span_id}-01")),
|
||||
tracestate: None,
|
||||
}),
|
||||
};
|
||||
|
||||
tracing::subscriber::with_default(subscriber, || {
|
||||
tracing::callsite::rebuild_interest_cache();
|
||||
let notification_span = tracing::info_span!(
|
||||
"codex.exec_server.notification",
|
||||
otel.kind = "server",
|
||||
otel.name = tracing::field::Empty,
|
||||
rpc.system = "jsonrpc",
|
||||
rpc.method = INITIALIZED_METHOD,
|
||||
rpc.transport = "stdio",
|
||||
method = INITIALIZED_METHOD,
|
||||
result = tracing::field::Empty,
|
||||
);
|
||||
set_notification_span_parent(¬ification_span, ¬ification);
|
||||
notification_span.record("otel.name", INITIALIZED_METHOD);
|
||||
notification_span.in_scope(|| {});
|
||||
drop(notification_span);
|
||||
});
|
||||
|
||||
tracer_provider.force_flush().expect("flush traces");
|
||||
let spans = span_exporter.get_finished_spans().expect("span export");
|
||||
let notification_span = spans
|
||||
.iter()
|
||||
.find(|span| span.name.as_ref() == INITIALIZED_METHOD)
|
||||
.expect("initialized notification span");
|
||||
assert_eq!(notification_span.span_context.trace_id(), trace_id);
|
||||
assert_eq!(notification_span.parent_span_id, parent_span_id);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn connection_accepts_pipelined_scalar_requests() {
|
||||
let registry = SessionRegistry::new(crate::ExecServerTelemetry::default());
|
||||
|
||||
Reference in New Issue
Block a user