From 976c0fbbfc19dec7340b1d81d56d20930c880d89 Mon Sep 17 00:00:00 2001 From: Anthony Tafoya Date: Tue, 1 Dec 2026 03:15:24 +0000 Subject: [PATCH] Stabilize span assertions in spawn publication tests (#64915) ## Why SQLite workers can retain spans after acknowledging a write, so reading the exporter immediately can miss spans. A paginated history reader can also keep the fork span open after history preparation returns. ## What changed Collect exported spans until the tracer is released, with a bounded timeout. Cover failed spawn publication cleanup with both paginated and in-memory history against the SQLite graph store. Check that fork preparation starts before or at the graph upsert in the paginated case, and that its span ends before or at the upsert's start in the in-memory case. GitOrigin-RevId: 2438ed49bc6c6d6c74c2695c9397a39dacc1dee2 --- .../agent/control/spawn_publication_tests.rs | 85 +++++++++++++++---- 1 file changed, 70 insertions(+), 15 deletions(-) diff --git a/codex-rs/core/src/agent/control/spawn_publication_tests.rs b/codex-rs/core/src/agent/control/spawn_publication_tests.rs index 70f360f34b..c1a1f27a5b 100644 --- a/codex-rs/core/src/agent/control/spawn_publication_tests.rs +++ b/codex-rs/core/src/agent/control/spawn_publication_tests.rs @@ -10,12 +10,20 @@ use codex_agent_graph_store::AgentGraphStoreFuture; use codex_agent_graph_store::LocalAgentGraphStore; use codex_agent_graph_store::ThreadSpawnEdgeStatus; use opentelemetry::trace::TracerProvider as _; -use opentelemetry_sdk::trace::InMemorySpanExporter; +use opentelemetry_sdk::error::OTelSdkResult; use opentelemetry_sdk::trace::SdkTracerProvider; +use opentelemetry_sdk::trace::SpanData; +use opentelemetry_sdk::trace::SpanExporter; use pretty_assertions::assert_eq; use tracing::Instrument; use tracing_subscriber::layer::SubscriberExt; +#[derive(Clone, Copy)] +enum PublicationHistory { + Local, + InMemory, +} + #[derive(Clone, Copy)] enum SpawnEdgeFailure { CancelledBeforeCommit, @@ -105,6 +113,7 @@ impl AgentGraphStore for FaultingGraphStore { async fn publication_harness( failure: SpawnEdgeFailure, + history: PublicationHistory, generate_thread_id: impl Fn() -> ThreadId + Send + Sync + 'static, ) -> (AgentControlHarness, Arc) { let mut harness = AgentControlHarness::new().await; @@ -132,7 +141,13 @@ async fn publication_harness( Arc::new(crate::test_support::EmptyUserInstructionsProvider), /*analytics_events_client*/ None, harness.manager.image_store(), - crate::ThreadStoreBuilder::from_config(&harness.config, harness.state_db.clone()).build(), + match history { + PublicationHistory::Local => { + crate::ThreadStoreBuilder::from_config(&harness.config, harness.state_db.clone()) + .build() + } + PublicationHistory::InMemory => Arc::new(InMemoryThreadStore::default()), + }, Some(graph.clone()), /*attestation_provider*/ None, /*external_time_provider*/ None, @@ -211,7 +226,8 @@ async fn cancelled_spawn_edge_publication_cleanup( failure: SpawnEdgeFailure, committed_children: usize, ) { - let (harness, graph) = publication_harness(failure, ThreadId::new).await; + let (harness, graph) = + publication_harness(failure, PublicationHistory::Local, ThreadId::new).await; let parent = ThreadId::new(); let mut spawn = Box::pin(spawn_publication_child( &harness, @@ -248,11 +264,26 @@ async fn cancelled_spawn_edge_publication_cleanup( assert_publication_closed(&harness, &graph, parent).await; } +#[derive(Debug)] +struct SpawnSpanExporter(tokio::sync::mpsc::UnboundedSender>); + +impl SpanExporter for SpawnSpanExporter { + async fn export(&self, batch: Vec) -> OTelSdkResult { + let _ = self.0.send(batch); + Ok(()) + } +} + +#[test_case::test_case(PublicationHistory::Local; "paginated_history")] +#[test_case::test_case(PublicationHistory::InMemory; "span_timing")] #[tokio::test(flavor = "multi_thread", worker_threads = 1)] -async fn failed_spawn_edge_publication_cleanup() { +async fn failed_spawn_edge_publication_cleanup(history: PublicationHistory) { let (harness, graph) = - publication_harness(SpawnEdgeFailure::ErrorAfterCommit, ThreadId::new).await; - let (parent, thread) = harness.start_paginated_thread().await; + publication_harness(SpawnEdgeFailure::ErrorAfterCommit, history, ThreadId::new).await; + let (parent, thread) = match history { + PublicationHistory::Local => harness.start_paginated_thread().await, + PublicationHistory::InMemory => harness.start_thread().await, + }; let call_id = "fork-publication"; let turn = thread.session.new_default_turn().await; thread @@ -264,14 +295,14 @@ async fn failed_spawn_edge_publication_cleanup() { ) .await; - let exporter = InMemorySpanExporter::default(); + let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); let provider = SdkTracerProvider::builder() - .with_simple_exporter(exporter.clone()) + .with_simple_exporter(SpawnSpanExporter(tx)) .build(); let subscriber = tracing_subscriber::registry() .with(tracing_opentelemetry::layer().with_tracer(provider.tracer("spawn-publication"))); // The test driver owns the subscriber. The separate Tokio worker must receive it from Core. - let _subscriber = tracing::subscriber::set_default(subscriber); + let guard = tracing::subscriber::set_default(subscriber); // Keep parallel tests from caching disabled interest when a callsite is first registered. let _parallel_dispatch = tracing::Dispatch::new( tracing_subscriber::registry().with(tracing_subscriber::filter::LevelFilter::OFF), @@ -294,7 +325,18 @@ async fn failed_spawn_edge_publication_cleanup() { ); assert_publication_closed(&harness, &graph, parent).await; - let spans = exporter.get_finished_spans().expect("exported spawn spans"); + // SQLite workers may keep spans alive after acknowledging a write. Once this + // scope and all workers release the tracer, the export channel closes. + drop(guard); + drop(provider); + let mut spans = Vec::new(); + timeout(Duration::from_secs(10), async { + while let Some(batch) = rx.recv().await { + spans.extend(batch); + } + }) + .await + .expect("spawn tracer should be released"); let span = |name| spans.iter().find(|span| span.name == name).expect(name); let caller = span("test_spawn_publication"); let fork = span("agent.spawn.prepare_forked_history"); @@ -307,15 +349,24 @@ async fn failed_spawn_edge_publication_cleanup() { parent.span_context.trace_id() ); } - assert!(fork.end_time <= upsert.start_time); + // A paginated history reader can retain the fork span after preparation returns. + // Check its end time with in-memory history and the same SQLite graph write. + match history { + PublicationHistory::Local => assert!(fork.start_time <= upsert.start_time), + PublicationHistory::InMemory => assert!(fork.end_time <= upsert.start_time), + } } #[test_case::test_case(SkippedPublication::Ephemeral; "ephemeral")] #[test_case::test_case(SkippedPublication::NoParent; "no_parent")] #[tokio::test] async fn failed_spawn_without_publication_does_not_close_graph(skipped: SkippedPublication) { - let (mut harness, graph) = - publication_harness(SpawnEdgeFailure::ErrorAfterCommit, ThreadId::new).await; + let (mut harness, graph) = publication_harness( + SpawnEdgeFailure::ErrorAfterCommit, + PublicationHistory::Local, + ThreadId::new, + ) + .await; let parent = ThreadId::new(); let source = match skipped { SkippedPublication::Ephemeral => { @@ -371,8 +422,12 @@ async fn failed_spawn_without_publication_does_not_close_graph(skipped: SkippedP #[tokio::test] async fn repeated_child_id_preserves_existing_thread_and_graph() { let repeated_id = ThreadId::new(); - let (harness, graph) = - publication_harness(SpawnEdgeFailure::ErrorAfterCommit, move || repeated_id).await; + let (harness, graph) = publication_harness( + SpawnEdgeFailure::ErrorAfterCommit, + PublicationHistory::Local, + move || repeated_id, + ) + .await; let (parent, existing_thread) = harness.start_thread().await; assert_eq!(parent, repeated_id); let children_before = graph