mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
27724f6ead
This is PR 3 of the app-server tracing rollout. PRs https://github.com/openai/codex/pull/13285 and https://github.com/openai/codex/pull/13368 gave us inbound request spans in app-server and propagated trace context through Submission. This change finishes the next piece in core: when a request actually starts a turn, we now create a core-owned long-lived span that stays open for the real lifetime of the turn. What changed: - `Session::spawn_task` can now optionally create a long-lived turn span and run the spawned task inside it - `turn/start` uses that path, so normal turn execution stays under a single core-owned span after the async handoff - `review/start` uses the same pattern - added a unit test that verifies the spawned turn task inherits the submission dispatch trace ancestry **Why** The app-server request span is intentionally short-lived. Once work crosses into core, we still want one span that covers the actual execution window until completion or interruption. This keeps that ownership where it belongs: in the layer that owns the runtime lifecycle.
97 lines
2.5 KiB
Rust
97 lines
2.5 KiB
Rust
use std::sync::Arc;
|
|
use std::sync::Mutex;
|
|
|
|
use crate::client::ModelClient;
|
|
use crate::client::ModelClientSession;
|
|
use crate::client_common::Prompt;
|
|
use crate::codex::TurnContext;
|
|
use crate::codex::run_turn;
|
|
use crate::error::Result as CodexResult;
|
|
use crate::state::TaskKind;
|
|
use async_trait::async_trait;
|
|
use codex_protocol::user_input::UserInput;
|
|
use tokio_util::sync::CancellationToken;
|
|
use tracing::Instrument;
|
|
use tracing::trace_span;
|
|
|
|
use super::SessionTask;
|
|
use super::SessionTaskContext;
|
|
|
|
pub(crate) struct RegularTask {
|
|
prewarmed_session: Mutex<Option<ModelClientSession>>,
|
|
}
|
|
|
|
impl Default for RegularTask {
|
|
fn default() -> Self {
|
|
Self {
|
|
prewarmed_session: Mutex::new(None),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl RegularTask {
|
|
pub(crate) async fn with_startup_prewarm(
|
|
model_client: ModelClient,
|
|
prompt: Prompt,
|
|
turn_context: Arc<TurnContext>,
|
|
turn_metadata_header: Option<String>,
|
|
) -> CodexResult<Self> {
|
|
let mut client_session = model_client.new_session();
|
|
client_session
|
|
.prewarm_websocket(
|
|
&prompt,
|
|
&turn_context.model_info,
|
|
&turn_context.otel_manager,
|
|
turn_context.reasoning_effort,
|
|
turn_context.reasoning_summary,
|
|
turn_context.config.service_tier,
|
|
turn_metadata_header.as_deref(),
|
|
)
|
|
.await?;
|
|
|
|
Ok(Self {
|
|
prewarmed_session: Mutex::new(Some(client_session)),
|
|
})
|
|
}
|
|
|
|
async fn take_prewarmed_session(&self) -> Option<ModelClientSession> {
|
|
self.prewarmed_session
|
|
.lock()
|
|
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
|
.take()
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl SessionTask for RegularTask {
|
|
fn kind(&self) -> TaskKind {
|
|
TaskKind::Regular
|
|
}
|
|
|
|
fn span_name(&self) -> &'static str {
|
|
"session_task.turn"
|
|
}
|
|
|
|
async fn run(
|
|
self: Arc<Self>,
|
|
session: Arc<SessionTaskContext>,
|
|
ctx: Arc<TurnContext>,
|
|
input: Vec<UserInput>,
|
|
cancellation_token: CancellationToken,
|
|
) -> Option<String> {
|
|
let sess = session.clone_session();
|
|
let run_turn_span = trace_span!("run_turn");
|
|
sess.set_server_reasoning_included(false).await;
|
|
let prewarmed_client_session = self.take_prewarmed_session().await;
|
|
run_turn(
|
|
sess,
|
|
ctx,
|
|
input,
|
|
prewarmed_client_session,
|
|
cancellation_token,
|
|
)
|
|
.instrument(run_turn_span)
|
|
.await
|
|
}
|
|
}
|