mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
## Why When a Node REPL-backed MCP tool requests approval through an elicitation callback, the trace loses the parent-child relationship between the callback and the Guardian review. ## What changed Parent the Guardian review span to the callback span when its `traceparent` is well formed and its trace ID matches the active tool invocation. Preserve the invocation's sampling flags and `tracestate`, and strip both trace fields from the elicitation metadata before it reaches approval handling or user prompts. ## Testing Cover callback span parenting and trace propagation to Guardian inference, including rejection of unrelated trace IDs and malformed headers. Verify that form and URL prompts retain other metadata while dropping trace fields. GitOrigin-RevId: 942b61a0da81b5d549c9f4813b28101bba622a26
93 lines
3.1 KiB
Rust
93 lines
3.1 KiB
Rust
use anyhow::Context;
|
|
use opentelemetry::global;
|
|
use opentelemetry::trace::TracerProvider as _;
|
|
use opentelemetry_sdk::propagation::TraceContextPropagator;
|
|
use opentelemetry_sdk::trace::SdkTracerProvider;
|
|
use tracing::dispatcher::DefaultGuard;
|
|
use tracing_subscriber::layer::SubscriberExt;
|
|
use tracing_subscriber::util::SubscriberInitExt;
|
|
|
|
/// Runs this libtest case alone so its process-wide subscriber sees worker spans.
|
|
/// Returns true in the parent once the child has passed. The child runs the test body.
|
|
pub async fn run_in_trace_subprocess() -> anyhow::Result<bool> {
|
|
const CHILD: &str = "CODEX_TRACE_TEST_CHILD";
|
|
if std::env::var_os(CHILD).is_some() {
|
|
return Ok(false);
|
|
}
|
|
let thread = std::thread::current();
|
|
let name = thread.name().context("libtest case name")?;
|
|
let output = tokio::process::Command::new(std::env::current_exe()?)
|
|
.args(["--exact", name])
|
|
.env(CHILD, "1")
|
|
.output()
|
|
.await?;
|
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
anyhow::ensure!(
|
|
output.status.success() && stdout.contains("1 passed"),
|
|
"isolated {name} failed: {stdout}{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
Ok(true)
|
|
}
|
|
|
|
/// Receives named trace events on a test's current-thread runtime.
|
|
pub struct NamedTraceEvents {
|
|
events: tokio::sync::mpsc::UnboundedReceiver<()>,
|
|
_subscriber: DefaultGuard,
|
|
}
|
|
|
|
impl NamedTraceEvents {
|
|
pub fn subscribe(name: &'static str) -> Self {
|
|
struct EventLayer {
|
|
name: &'static str,
|
|
sender: tokio::sync::mpsc::UnboundedSender<()>,
|
|
}
|
|
impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for EventLayer {
|
|
fn on_event(
|
|
&self,
|
|
event: &tracing::Event<'_>,
|
|
_: tracing_subscriber::layer::Context<'_, S>,
|
|
) {
|
|
if event.metadata().name() == self.name {
|
|
let _ = self.sender.send(());
|
|
}
|
|
}
|
|
}
|
|
|
|
let (sender, events) = tokio::sync::mpsc::unbounded_channel();
|
|
let subscriber = tracing_subscriber::registry()
|
|
.with(EventLayer { name, sender })
|
|
.set_default();
|
|
Self {
|
|
events,
|
|
_subscriber: subscriber,
|
|
}
|
|
}
|
|
|
|
pub async fn next(&mut self) -> anyhow::Result<()> {
|
|
tokio::time::timeout(std::time::Duration::from_secs(10), self.events.recv())
|
|
.await
|
|
.context("timed out waiting for trace event")?
|
|
.context("trace collector closed")
|
|
}
|
|
}
|
|
|
|
pub struct TestTracingContext {
|
|
_provider: SdkTracerProvider,
|
|
_guard: DefaultGuard,
|
|
}
|
|
|
|
pub fn install_test_tracing(tracer_name: &str) -> TestTracingContext {
|
|
global::set_text_map_propagator(TraceContextPropagator::new());
|
|
|
|
let provider = SdkTracerProvider::builder().build();
|
|
let tracer = provider.tracer(tracer_name.to_string());
|
|
let subscriber =
|
|
tracing_subscriber::registry().with(tracing_opentelemetry::layer().with_tracer(tracer));
|
|
|
|
TestTracingContext {
|
|
_provider: provider,
|
|
_guard: subscriber.set_default(),
|
|
}
|
|
}
|