mirror of
https://github.com/openai/codex.git
synced 2026-09-20 12:47:38 +00:00
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.
132 lines
4.1 KiB
Rust
132 lines
4.1 KiB
Rust
use std::sync::Arc;
|
|
|
|
use crate::codex::TurnContext;
|
|
use crate::protocol::EventMsg;
|
|
use crate::protocol::UndoCompletedEvent;
|
|
use crate::protocol::UndoStartedEvent;
|
|
use crate::state::TaskKind;
|
|
use crate::tasks::SessionTask;
|
|
use crate::tasks::SessionTaskContext;
|
|
use async_trait::async_trait;
|
|
use codex_git::RestoreGhostCommitOptions;
|
|
use codex_git::restore_ghost_commit_with_options;
|
|
use codex_protocol::models::ResponseItem;
|
|
use codex_protocol::user_input::UserInput;
|
|
use tokio_util::sync::CancellationToken;
|
|
use tracing::error;
|
|
use tracing::info;
|
|
use tracing::warn;
|
|
|
|
pub(crate) struct UndoTask;
|
|
|
|
impl UndoTask {
|
|
pub(crate) fn new() -> Self {
|
|
Self
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl SessionTask for UndoTask {
|
|
fn kind(&self) -> TaskKind {
|
|
TaskKind::Regular
|
|
}
|
|
|
|
fn span_name(&self) -> &'static str {
|
|
"session_task.undo"
|
|
}
|
|
|
|
async fn run(
|
|
self: Arc<Self>,
|
|
session: Arc<SessionTaskContext>,
|
|
ctx: Arc<TurnContext>,
|
|
_input: Vec<UserInput>,
|
|
cancellation_token: CancellationToken,
|
|
) -> Option<String> {
|
|
let _ = session
|
|
.session
|
|
.services
|
|
.otel_manager
|
|
.counter("codex.task.undo", 1, &[]);
|
|
let sess = session.clone_session();
|
|
sess.send_event(
|
|
ctx.as_ref(),
|
|
EventMsg::UndoStarted(UndoStartedEvent {
|
|
message: Some("Undo in progress...".to_string()),
|
|
}),
|
|
)
|
|
.await;
|
|
|
|
if cancellation_token.is_cancelled() {
|
|
sess.send_event(
|
|
ctx.as_ref(),
|
|
EventMsg::UndoCompleted(UndoCompletedEvent {
|
|
success: false,
|
|
message: Some("Undo cancelled.".to_string()),
|
|
}),
|
|
)
|
|
.await;
|
|
return None;
|
|
}
|
|
|
|
let history = sess.clone_history().await;
|
|
let mut items = history.raw_items().to_vec();
|
|
let mut completed = UndoCompletedEvent {
|
|
success: false,
|
|
message: None,
|
|
};
|
|
|
|
let Some((idx, ghost_commit)) =
|
|
items
|
|
.iter()
|
|
.enumerate()
|
|
.rev()
|
|
.find_map(|(idx, item)| match item {
|
|
ResponseItem::GhostSnapshot { ghost_commit } => {
|
|
Some((idx, ghost_commit.clone()))
|
|
}
|
|
_ => None,
|
|
})
|
|
else {
|
|
completed.message = Some("No ghost snapshot available to undo.".to_string());
|
|
sess.send_event(ctx.as_ref(), EventMsg::UndoCompleted(completed))
|
|
.await;
|
|
return None;
|
|
};
|
|
|
|
let commit_id = ghost_commit.id().to_string();
|
|
let repo_path = ctx.cwd.clone();
|
|
let ghost_snapshot = ctx.ghost_snapshot.clone();
|
|
let restore_result = tokio::task::spawn_blocking(move || {
|
|
let options = RestoreGhostCommitOptions::new(&repo_path).ghost_snapshot(ghost_snapshot);
|
|
restore_ghost_commit_with_options(&options, &ghost_commit)
|
|
})
|
|
.await;
|
|
|
|
match restore_result {
|
|
Ok(Ok(())) => {
|
|
items.remove(idx);
|
|
let reference_context_item = sess.reference_context_item().await;
|
|
sess.replace_history(items, reference_context_item).await;
|
|
let short_id: String = commit_id.chars().take(7).collect();
|
|
info!(commit_id = commit_id, "Undo restored ghost snapshot");
|
|
completed.success = true;
|
|
completed.message = Some(format!("Undo restored snapshot {short_id}."));
|
|
}
|
|
Ok(Err(err)) => {
|
|
let message = format!("Failed to restore snapshot {commit_id}: {err}");
|
|
warn!("{message}");
|
|
completed.message = Some(message);
|
|
}
|
|
Err(err) => {
|
|
let message = format!("Failed to restore snapshot {commit_id}: {err}");
|
|
error!("{message}");
|
|
completed.message = Some(message);
|
|
}
|
|
}
|
|
|
|
sess.send_event(ctx.as_ref(), EventMsg::UndoCompleted(completed))
|
|
.await;
|
|
None
|
|
}
|
|
}
|