This commit is contained in:
Ahmed Ibrahim
2025-09-08 15:41:02 -07:00
parent f7671481c1
commit e7a20f5109
9 changed files with 115 additions and 82 deletions
+46 -11
View File
@@ -17,7 +17,7 @@ use codex_apply_patch::ApplyPatchAction;
use codex_apply_patch::MaybeApplyPatchVerified;
use codex_apply_patch::maybe_parse_apply_patch_verified;
use codex_protocol::mcp_protocol::ConversationId;
use codex_protocol::protocol::ConversationHistoryResponseEvent;
use codex_protocol::protocol::ConversationPathResponseEvent;
use codex_protocol::protocol::TaskStartedEvent;
use codex_protocol::protocol::TurnAbortReason;
use codex_protocol::protocol::TurnAbortedEvent;
@@ -571,7 +571,9 @@ impl Session {
Some(turn_context.sandbox_policy.clone()),
Some(self.user_shell.clone()),
)));
self.record_conversation_items(&conversation_items).await;
for item in conversation_items {
self.record_conversation_items(item).await;
}
vec![]
}
@@ -590,6 +592,7 @@ impl Session {
let mut msgs = Vec::new();
for item in items.clone() {
match item {
<<<<<<< HEAD
RolloutItem::ResponseItem(ref v) => {
responses.extend(v.clone());
let new_msgs: Vec<EventMsg> = v
@@ -598,6 +601,13 @@ impl Session {
map_response_item_to_event_messages(ri, self.show_raw_agent_reasoning)
})
.collect();
=======
RolloutItem::ResponseItem(response) => {
let new_msgs: Vec<EventMsg> = map_response_item_to_event_messages(
&response,
self.show_raw_agent_reasoning,
);
>>>>>>> 4245659e (progress)
if before_resume_session {
msgs.extend(new_msgs);
} else {
@@ -608,8 +618,15 @@ impl Session {
);
}
}
<<<<<<< HEAD
RolloutItem::Event(events) => msgs.extend(events.iter().map(|e| e.msg.clone())),
RolloutItem::SessionMeta(..) => {}
=======
RolloutItem::Event(event) => msgs.push(event.msg.clone()),
RolloutItem::SessionMeta(..) => {
// Session meta does not emit events
}
>>>>>>> 4245659e (progress)
}
}
@@ -632,7 +649,7 @@ impl Session {
};
if let Some(rec) = recorder
&& let Err(e) = rec
.record_items(crate::rollout::RolloutItem::Event(vec![event_to_record]))
.record_items(crate::rollout::RolloutItem::Event(event_to_record))
.await
{
error!("failed to record rollout event: {e:#}");
@@ -725,6 +742,7 @@ impl Session {
/// Records items to both the rollout and the chat completions/ZDR
/// transcript, if enabled.
<<<<<<< HEAD
async fn record_conversation_items(&self, items: &[ResponseItem]) {
self.record_conversation_items_internal(items, true).await;
}
@@ -734,6 +752,20 @@ impl Session {
if persist {
self.record_state_snapshot(RolloutItem::ResponseItem(items.to_vec()))
.await;
=======
async fn record_conversation_items<T>(&self, item: T)
where
T: Into<RolloutItem>,
{
let item: RolloutItem = item.into();
debug!("Recording items for conversation: {item:?}");
self.record_state_snapshot(item.clone()).await;
if let RolloutItem::ResponseItem(response_item) = &item {
self.state
.lock_unchecked()
.history
.record_items(std::slice::from_ref(response_item));
>>>>>>> 4245659e (progress)
}
self.state.lock_unchecked().history.record_items(items);
@@ -1187,13 +1219,13 @@ async fn submission_loop(
// Install the new persistent context for subsequent tasks/turns.
turn_context = Arc::new(new_turn_context);
if cwd.is_some() || approval_policy.is_some() || sandbox_policy.is_some() {
sess.record_conversation_items(&[ResponseItem::from(EnvironmentContext::new(
sess.record_conversation_items(ResponseItem::from(EnvironmentContext::new(
cwd,
approval_policy,
sandbox_policy,
// Shell is not configurable from turn to turn
None,
))])
)))
.await;
}
}
@@ -1402,12 +1434,12 @@ async fn submission_loop(
sess.send_event(event).await;
break;
}
Op::GetHistory => {
Op::GetConversationPath => {
let sub_id = sub.id.clone();
let event = Event {
id: sub_id.clone(),
msg: EventMsg::ConversationHistory(ConversationHistoryResponseEvent {
msg: EventMsg::ConversationHistory(ConversationPathResponseEvent {
conversation_id: sess.conversation_id,
entries: sess.state.lock_unchecked().history.contents(),
}),
@@ -1453,7 +1485,7 @@ async fn run_task(
sess.send_event(event).await;
let initial_input_for_turn: ResponseInputItem = ResponseInputItem::from(input);
sess.record_conversation_items(&[ResponseItem::from(initial_input_for_turn.clone())])
sess.record_conversation_items(ResponseItem::from(initial_input_for_turn.clone()))
.await;
let mut last_agent_message: Option<String> = None;
@@ -1470,7 +1502,9 @@ async fn run_task(
.into_iter()
.map(ResponseItem::from)
.collect::<Vec<ResponseItem>>();
sess.record_conversation_items(&pending_input).await;
for item in pending_input.iter().cloned() {
sess.record_conversation_items(item).await;
}
// Construct the input that we will send to the model. When using the
// Chat completions API (or ZDR clients), the model needs the full
@@ -1597,8 +1631,9 @@ async fn run_task(
// Only attempt to take the lock if there is something to record.
if !items_to_record_in_conversation_history.is_empty() {
sess.record_conversation_items(&items_to_record_in_conversation_history)
.await;
for item in items_to_record_in_conversation_history.iter().cloned() {
sess.record_conversation_items(item).await;
}
}
if responses.is_empty() {