mirror of
https://github.com/openai/codex.git
synced 2026-11-24 23:20:47 +00:00
Trace fork history preparation and spawn-edge persistence (#63719)
## Why Agent spawn traces need separate timings for preparing forked history and persisting the parent-child relationship. The persistence write runs in a separate Tokio task, so it needs the caller's tracing context. ## What changed Add spans for fork history preparation and the spawn-edge upsert. Carry the caller's span and subscriber into the persistence task, and enter the upsert span both when calling the graph store and when polling its future. ## Testing Extend the failed-publication test to verify cleanup after a lost commit acknowledgement and check the exported trace. It asserts that fork preparation finishes before the upsert starts, that both spans belong to the spawn caller, and that the graph store span is nested under the upsert. GitOrigin-RevId: 28bf2d69ce494ab146aa124f045e28d6a0f5dab9
This commit is contained in:
@@ -52,6 +52,7 @@ use futures::StreamExt;
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Weak;
|
||||
use tracing::Instrument;
|
||||
use tracing::warn;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -677,14 +678,15 @@ impl LocalAgentControl {
|
||||
let Some(agent_graph_store) = state.agent_graph_store() else {
|
||||
return Ok(false);
|
||||
};
|
||||
if let Err(err) = agent_graph_store
|
||||
.upsert_thread_spawn_edge(
|
||||
let span = tracing::info_span!("agent.spawn.upsert_thread_spawn_edge").or_current();
|
||||
let upsert = span.in_scope(|| {
|
||||
agent_graph_store.upsert_thread_spawn_edge(
|
||||
parent_thread_id,
|
||||
child_thread_id,
|
||||
codex_agent_graph_store::ThreadSpawnEdgeStatus::Open,
|
||||
)
|
||||
.await
|
||||
{
|
||||
});
|
||||
if let Err(err) = upsert.instrument(span).await {
|
||||
warn!("failed to persist thread-spawn edge: {err}");
|
||||
return Err(CodexErr::Fatal(
|
||||
"failed to persist spawned agent relationship".to_string(),
|
||||
|
||||
@@ -27,6 +27,8 @@ use codex_utils_path_uri::PathUri;
|
||||
use futures::StreamExt;
|
||||
use futures::stream;
|
||||
use std::time::Instant;
|
||||
use tracing::Instrument;
|
||||
use tracing::instrument::WithSubscriber;
|
||||
|
||||
const AGENT_NAMES: &str = include_str!("../../../assets/agent/agent_names.txt");
|
||||
|
||||
@@ -913,11 +915,19 @@ impl LocalAgentControl {
|
||||
let control = self.clone();
|
||||
let source = notification_source.clone();
|
||||
let ephemeral = config.ephemeral;
|
||||
pending_spawn.set_edge_write(tokio::spawn(async move {
|
||||
control
|
||||
.persist_thread_spawn_edge_for_source(ephemeral, child_thread_id, source.as_ref())
|
||||
.await
|
||||
}));
|
||||
pending_spawn.set_edge_write(tokio::spawn(
|
||||
async move {
|
||||
control
|
||||
.persist_thread_spawn_edge_for_source(
|
||||
ephemeral,
|
||||
child_thread_id,
|
||||
source.as_ref(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
.in_current_span()
|
||||
.with_current_subscriber(),
|
||||
));
|
||||
pending_spawn.wait_for_edge().await?;
|
||||
let edge_write = edge_write_started_at.elapsed();
|
||||
let child_create_started_at = Instant::now();
|
||||
|
||||
@@ -9,7 +9,12 @@ use codex_agent_graph_store::AgentGraphStoreError;
|
||||
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::trace::SdkTracerProvider;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tracing::Instrument;
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum SpawnEdgeFailure {
|
||||
@@ -41,6 +46,7 @@ impl FaultingGraphStore {
|
||||
}
|
||||
|
||||
impl AgentGraphStore for FaultingGraphStore {
|
||||
#[tracing::instrument(name = "test_agent_graph_store_upsert", skip_all)]
|
||||
fn upsert_thread_spawn_edge(
|
||||
&self,
|
||||
parent: ThreadId,
|
||||
@@ -139,12 +145,13 @@ async fn publication_harness(
|
||||
async fn spawn_publication_child(
|
||||
harness: &AgentControlHarness,
|
||||
parent: ThreadId,
|
||||
options: SpawnAgentOptions,
|
||||
) -> CodexResult<ThreadId> {
|
||||
harness
|
||||
.control
|
||||
.spawn_agent(
|
||||
.spawn_agent_internal(
|
||||
harness.config.clone(),
|
||||
text_input("child task"),
|
||||
spawn::SpawnInitialInput::UserInput(text_input("child task")),
|
||||
Some(SessionSource::SubAgent(SubAgentSource::ThreadSpawn {
|
||||
parent_thread_id: parent,
|
||||
depth: 1,
|
||||
@@ -152,8 +159,10 @@ async fn spawn_publication_child(
|
||||
agent_nickname: None,
|
||||
agent_role: None,
|
||||
})),
|
||||
options,
|
||||
)
|
||||
.await
|
||||
.map(|(agent, _)| agent.thread_id)
|
||||
}
|
||||
|
||||
async fn assert_publication_closed(
|
||||
@@ -204,7 +213,11 @@ async fn cancelled_spawn_edge_publication_cleanup(
|
||||
) {
|
||||
let (harness, graph) = publication_harness(failure, ThreadId::new).await;
|
||||
let parent = ThreadId::new();
|
||||
let mut spawn = Box::pin(spawn_publication_child(&harness, parent));
|
||||
let mut spawn = Box::pin(spawn_publication_child(
|
||||
&harness,
|
||||
parent,
|
||||
SpawnAgentOptions::default(),
|
||||
));
|
||||
tokio::select! {
|
||||
biased;
|
||||
() = graph.held.cancelled() => {},
|
||||
@@ -235,19 +248,66 @@ async fn cancelled_spawn_edge_publication_cleanup(
|
||||
assert_publication_closed(&harness, &graph, parent).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||
async fn failed_spawn_edge_publication_cleanup() {
|
||||
let (harness, graph) =
|
||||
publication_harness(SpawnEdgeFailure::ErrorAfterCommit, ThreadId::new).await;
|
||||
let parent = ThreadId::new();
|
||||
let error = spawn_publication_child(&harness, parent)
|
||||
.await
|
||||
.expect_err("lost commit acknowledgement rejects spawn");
|
||||
let (parent, thread) = harness.start_paginated_thread().await;
|
||||
let call_id = "fork-publication";
|
||||
let turn = thread.session.new_default_turn().await;
|
||||
thread
|
||||
.session
|
||||
.record_conversation_items(
|
||||
turn.as_ref(),
|
||||
turn.model_info(),
|
||||
&[spawn_agent_call(call_id)],
|
||||
)
|
||||
.await;
|
||||
|
||||
let exporter = InMemorySpanExporter::default();
|
||||
let provider = SdkTracerProvider::builder()
|
||||
.with_simple_exporter(exporter.clone())
|
||||
.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);
|
||||
// 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),
|
||||
);
|
||||
let error = spawn_publication_child(
|
||||
&harness,
|
||||
parent,
|
||||
SpawnAgentOptions {
|
||||
fork_mode: Some(SpawnAgentForkMode::FullHistory),
|
||||
fork_parent_spawn_call_id: Some(call_id.to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.instrument(tracing::info_span!("test_spawn_publication"))
|
||||
.await
|
||||
.expect_err("lost commit acknowledgement rejects spawn");
|
||||
assert_eq!(
|
||||
error.to_string(),
|
||||
"Fatal error: failed to persist spawned agent relationship"
|
||||
);
|
||||
assert_publication_closed(&harness, &graph, parent).await;
|
||||
|
||||
let spans = exporter.get_finished_spans().expect("exported spawn spans");
|
||||
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");
|
||||
let upsert = span("agent.spawn.upsert_thread_spawn_edge");
|
||||
let backend = span("test_agent_graph_store_upsert");
|
||||
for (child, parent) in [(fork, caller), (upsert, caller), (backend, upsert)] {
|
||||
assert_eq!(child.parent_span_id, parent.span_context.span_id());
|
||||
assert_eq!(
|
||||
child.span_context.trace_id(),
|
||||
parent.span_context.trace_id()
|
||||
);
|
||||
}
|
||||
assert!(fork.end_time <= upsert.start_time);
|
||||
}
|
||||
|
||||
#[test_case::test_case(SkippedPublication::Ephemeral; "ephemeral")]
|
||||
@@ -319,7 +379,7 @@ async fn repeated_child_id_preserves_existing_thread_and_graph() {
|
||||
.list_thread_spawn_children(parent, /*status_filter*/ None)
|
||||
.await
|
||||
.expect("initial graph");
|
||||
let error = spawn_publication_child(&harness, parent)
|
||||
let error = spawn_publication_child(&harness, parent, SpawnAgentOptions::default())
|
||||
.await
|
||||
.expect_err("loaded IDs are rejected");
|
||||
assert_eq!(
|
||||
|
||||
@@ -345,6 +345,7 @@ pub(super) async fn load_agent_model_context(
|
||||
}
|
||||
|
||||
/// Prepare copied history while leaving startup settings and timing with the caller.
|
||||
#[tracing::instrument(name = "agent.spawn.prepare_forked_history", skip_all)]
|
||||
pub(super) async fn prepare_forked_history(
|
||||
state: &ThreadManagerState,
|
||||
session_source: &SessionSource,
|
||||
|
||||
Reference in New Issue
Block a user