Preserve turn attribution across recovery and compaction (#51402)

## Why

Recovering an interrupted turn replaced its original trigger with `retry` and restored only its root lineage from model context. Turns suspended before that context was written could lose their attribution entirely.

## What changed

- Persist regular-turn attribution in `TurnStartedEvent` and compaction resume metadata, including the trigger, parent and root turn IDs, and initiating agent path.
- Restore matching attribution during recovery, keep unknown triggers absent, and retain the model-context root fallback for older records.
- Reconstruct attribution from surviving turns across rollback and late completion or abort events, and clear inherited attribution when spawning a forked agent.

## Testing

Add coverage for repeated cold recovery, suspension before context persistence, legacy rollouts, fresh attribution on subsequent turns, compaction checkpoints, rollback, and late terminal events.

GitOrigin-RevId: 85b5763e8fbd48a1ea2c07cfda8196e63c2cd33a
This commit is contained in:
Eddie Chen
2026-10-06 15:50:27 +00:00
committed by copyberry
parent a4ebc509f4
commit 551bd409eb
50 changed files with 912 additions and 38 deletions
+1
View File
@@ -1201,6 +1201,7 @@ impl LocalAgentControl {
compacted.latest_token_usage_record = None;
if let Some(resume_metadata) = &mut compacted.resume_metadata {
resume_metadata.multi_agent_version = Some(multi_agent_version);
resume_metadata.turn_attribution = None;
if !preserve_context_baselines {
resume_metadata.previous_turn_settings = None;
}
+12 -1
View File
@@ -565,6 +565,7 @@ async fn get_status_returns_not_found_without_manager() {
#[tokio::test]
async fn on_event_updates_status_from_task_started() {
let status = agent_status_from_event(&EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: "turn-1".to_string(),
root_turn_id: None,
trace_id: None,
@@ -2256,7 +2257,14 @@ async fn spawn_agent_fork_sanitizes_inherited_compaction_metadata() {
let (parent_thread_id, parent_thread) = harness.start_paginated_thread().await;
let parent_resume_metadata = codex_history::CompactionResumeMetadata {
multi_agent_version: Some(MultiAgentVersion::V2),
last_started_turn_id: Some("parent-turn".into()),
last_started_turn_id: Some("parent-turn".to_string()),
turn_attribution: Some(codex_history::TurnAttribution {
turn_id: "parent-turn".to_string(),
turn_trigger: Some("automation".to_string()),
parent_turn_id: Some("initiating-turn".to_string()),
initiating_agent_path: Some(codex_protocol::AgentPath::root()),
root_turn_id: Some("root-turn".to_string()),
}),
previous_turn_settings: Some(codex_history::PreviousTurnSettings {
model: "parent-model".into(),
comp_hash: None,
@@ -2382,6 +2390,7 @@ async fn spawn_agent_fork_sanitizes_inherited_compaction_metadata() {
inherited_resume_metadata,
&codex_history::CompactionResumeMetadata {
multi_agent_version: Some(MultiAgentVersion::V1),
turn_attribution: None,
..parent_resume_metadata
}
);
@@ -3287,6 +3296,7 @@ async fn spawn_agent_full_fork_legacy_compaction_rebuilds_child_instructions_onc
resume_metadata: Some(codex_history::CompactionResumeMetadata {
multi_agent_version: Some(MultiAgentVersion::V2),
last_started_turn_id: None,
turn_attribution: None,
previous_turn_settings: Some(codex_history::PreviousTurnSettings {
model: "parent-model".into(),
comp_hash: None,
@@ -3380,6 +3390,7 @@ async fn spawn_agent_full_fork_legacy_compaction_rebuilds_child_instructions_onc
Some(codex_history::CompactionResumeMetadata {
multi_agent_version: Some(MultiAgentVersion::V2),
last_started_turn_id: None,
turn_attribution: None,
previous_turn_settings: None,
})
);
+1 -8
View File
@@ -474,16 +474,9 @@ impl CodexThread {
trace,
cyber_access_program,
} = request;
let root_turn_id = self
.session
.reference_context_item()
.await
.filter(|context| context.turn_id.as_deref() == Some(turn_id.as_str()))
.and_then(|context| context.root_turn_id);
let start_options = TurnStartOptions {
cyber_access_program,
root_turn_id,
..Default::default()
..self.session.recovered_turn_start_options(&turn_id).await
};
match self
.io
@@ -37,6 +37,7 @@ pub(super) async fn record(session: &Session, text: String) -> std::io::Result<(
let now = chrono::Utc::now();
if recording_turn {
let started = EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: turn.sub_id.clone(),
root_turn_id: Some(turn.sub_id.clone()),
trace_id: turn.trace_id.clone(),
@@ -47,7 +48,9 @@ pub(super) async fn record(session: &Session, text: String) -> std::io::Result<(
session
.persist_rollout_items(&[RolloutItem::EventMsg(started)])
.await;
session.record_started_turn(&turn.sub_id).await;
session
.record_started_turn(&turn.sub_id, /*attribution*/ None)
.await;
}
let item = session.response_item_from_user_input(content.clone());
session
@@ -28,6 +28,7 @@ fn started_state() -> RealtimeHistoryState {
let mut state = RealtimeHistoryState::default();
state.observe(&EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: "turn-1".to_string(),
root_turn_id: None,
trace_id: None,
@@ -129,6 +130,7 @@ fn interrupted_turn_is_not_associated_with_a_new_voice_session(
let mut state = RealtimeHistoryState::default();
state.observe(&EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: "turn-1".to_string(),
root_turn_id: None,
trace_id: None,
+12 -2
View File
@@ -1,5 +1,5 @@
//! Captures recorded, uncanceled regular turns using one thread-configured local executor.
//! Callers must flush the rollout after capture before persisting the snapshot.
//! Captures interrupted turns for daemon recovery and restores persisted turn attribution.
//! Callers must flush the rollout after capturing a live turn before persisting its snapshot.
use super::Session;
use codex_protocol::protocol::EnvironmentConfigState;
@@ -9,6 +9,16 @@ use codex_protocol::protocol::TurnEnvironmentSelection;
pub(super) struct RecordedTurnInput;
impl Session {
pub(crate) async fn recovered_turn_start_options(
&self,
turn_id: &str,
) -> crate::TurnStartOptions {
self.state
.lock()
.await
.recovered_turn_start_options(turn_id)
}
/// Captures a regular turn only after its input is recorded. The caller must flush the rollout.
pub(crate) async fn interrupted_turn(
&self,
+6 -1
View File
@@ -318,6 +318,7 @@ use crate::state::PendingRequestPermissions;
use crate::state::ReasoningEffortPin;
use crate::state::SessionServices;
use crate::state::SessionState;
use crate::state::TaskKind;
#[cfg(test)]
use crate::stream_events_utils::HandleOutputCtx;
#[cfg(test)]
@@ -1754,6 +1755,7 @@ impl Session {
last_started_turn_id,
previous_turn_settings,
reference_context_item,
turn_attribution,
world_state_baseline,
window_number,
first_window_id,
@@ -1809,6 +1811,7 @@ impl Session {
reviewer_compaction_hash.as_deref(),
);
state.last_started_turn_id = last_started_turn_id;
state.turn_attribution = turn_attribution;
if let Some(world_state) = world_state_baseline {
state.history.set_world_state_baseline(world_state);
}
@@ -2259,8 +2262,9 @@ impl Session {
.trusted_guardian_reviewer
}
pub(crate) async fn emit_turn_started(&self, turn_context: &TurnContext) {
pub(crate) async fn emit_turn_started(&self, turn_context: &TurnContext, task_kind: TaskKind) {
let event = TurnStartedEvent {
turn_attribution: (task_kind == TaskKind::Regular).then(|| turn_context.attribution()),
turn_id: turn_context.sub_id.clone(),
root_turn_id: Some(
turn_context
@@ -4082,6 +4086,7 @@ impl Session {
resume_metadata: Some(CompactionResumeMetadata {
multi_agent_version: self.multi_agent_version(),
last_started_turn_id: state.last_started_turn_id.clone(),
turn_attribution: state.turn_attribution.clone(),
previous_turn_settings: state.previous_turn_settings(),
}),
}
@@ -2,6 +2,7 @@ use super::*;
use crate::context::world_state::WorldStateSnapshot;
use crate::context_manager::is_user_turn_boundary;
use codex_history::ResponseItemEnvelope;
use codex_history::TurnAttribution;
use codex_protocol::protocol::SessionContextWindow;
use codex_protocol::protocol::ThreadHistoryMode;
use uuid::Uuid;
@@ -16,6 +17,7 @@ pub(super) struct RolloutReconstruction {
pub(super) last_started_turn_id: Option<String>,
pub(super) previous_turn_settings: Option<PreviousTurnSettings>,
pub(super) reference_context_item: Option<TurnContextItem>,
pub(super) turn_attribution: Option<TurnAttribution>,
pub(super) world_state_baseline: Option<WorldStateSnapshot>,
pub(super) window_number: u64,
pub(super) first_window_id: Option<Uuid>,
@@ -94,11 +96,25 @@ struct ActiveReplaySegment<'a> {
counts_as_user_turn: bool,
previous_turn_settings: Option<PreviousTurnSettings>,
reference_context_item: TurnReferenceContextItem,
turn_attribution: Option<TurnAttribution>,
world_state_replay: Vec<&'a RolloutItem>,
history_checkpoint: Option<ReplayCheckpoint<'a>>,
window: Option<ReconstructedWindow>,
}
impl ActiveReplaySegment<'_> {
fn take_checkpoint_attribution(&mut self, checkpoint: &mut Option<TurnAttribution>) {
if checkpoint.as_ref().is_some_and(|attribution| {
self.turn_id
.as_ref()
.is_none_or(|id| id == &attribution.turn_id)
}) {
// Consume a matching checkpoint even when rollback discards this segment.
self.turn_attribution = self.turn_attribution.take().or(checkpoint.take());
}
}
}
fn turn_ids_are_compatible(active_turn_id: Option<&str>, item_turn_id: Option<&str>) -> bool {
active_turn_id
.is_none_or(|turn_id| item_turn_id.is_none_or(|item_turn_id| item_turn_id == turn_id))
@@ -112,7 +128,7 @@ fn finalize_active_segment<'a>(
world_state_replay: &mut Vec<&'a RolloutItem>,
window: &mut Option<ReconstructedWindow>,
pending_rollback_turns: &mut usize,
) {
) -> Option<TurnAttribution> {
// Thread rollback drops the newest surviving real user-message boundaries. In replay, that
// means skipping the next finalized segments that contain a non-contextual
// `EventMsg::UserMessage`.
@@ -120,7 +136,7 @@ fn finalize_active_segment<'a>(
if active_segment.counts_as_user_turn {
*pending_rollback_turns -= 1;
}
return;
return None;
}
// Full world-state snapshots are persisted after installing initial context. They still
@@ -163,6 +179,9 @@ fn finalize_active_segment<'a>(
{
*reference_context_item = active_segment.reference_context_item;
}
// Automatic turns can persist attribution without establishing a model-context baseline.
// Select recovery metadata from the same surviving segments, independently of that baseline.
active_segment.turn_attribution
}
impl Session {
@@ -197,11 +216,11 @@ impl Session {
.and_then(|checkpoint| reconstructed_window_from_compaction(checkpoint.compacted));
// Scan the selected items backward to find the newest surviving turn state.
let last_started_turn_id = replay_items
let mut started_turn_ids = replay_items
.iter()
.rev()
.find_map(|item| match item {
RolloutItem::EventMsg(EventMsg::TurnStarted(event)) => Some(event.turn_id.clone()),
.filter_map(|item| match item {
RolloutItem::EventMsg(EventMsg::TurnStarted(event)) => Some(event.turn_id.as_str()),
RolloutItem::SessionMeta(_)
| RolloutItem::ResponseItem(_)
| RolloutItem::InterAgentCommunication(_)
@@ -215,19 +234,37 @@ impl Session {
| RolloutItem::Compacted(_)
| RolloutItem::EventMsg(_) => None,
})
.peekable();
let last_started_turn_id = started_turn_ids
.peek()
.map(|id| (*id).to_owned())
.or_else(|| resume_metadata.and_then(|metadata| metadata.last_started_turn_id.clone()));
// Saved attribution still identifies the turn when continuation eligibility was cleared.
let checkpoint_turn_id = resume_metadata.and_then(|metadata| {
metadata.last_started_turn_id.as_deref().or_else(|| {
metadata
.turn_attribution
.as_ref()
.map(|attribution| attribution.turn_id.as_str())
})
});
let mut previous_turn_settings = None;
let mut reference_context_item = TurnReferenceContextItem::NeverSet;
let mut turn_attribution = None;
let mut world_state_replay = Vec::new();
// Rollback is "drop the newest N user turns". While scanning in reverse, that becomes
// "skip the next N user-turn segments we finalize".
let mut pending_rollback_turns = 0usize;
// Reverse replay accumulates rollout items into the newest in-progress turn segment until
// we hit its matching `TurnStarted`, at which point the segment can be finalized.
let mut checkpoint_attribution =
resume_metadata.and_then(|metadata| metadata.turn_attribution.clone());
let mut late_completed_turn_ids = HashSet::new();
let mut active_segment: Option<ActiveReplaySegment<'_>> = None;
for (index, item) in replay_items.iter().enumerate().rev() {
let preceding_turn_id = started_turn_ids.peek().copied().or(checkpoint_turn_id);
match item {
RolloutItem::Compacted(compacted) => {
let active_segment =
@@ -261,6 +298,11 @@ impl Session {
.saturating_add(usize::try_from(rollback.num_turns).unwrap_or(usize::MAX));
}
RolloutItem::EventMsg(EventMsg::TurnComplete(event)) => {
// Stop hooks can finish after a newer turn has started.
if !turn_ids_are_compatible(preceding_turn_id, Some(event.turn_id.as_str())) {
late_completed_turn_ids.insert(event.turn_id.as_str());
continue;
}
let active_segment =
active_segment.get_or_insert_with(ActiveReplaySegment::default);
active_segment.turn_completed = true;
@@ -271,6 +313,9 @@ impl Session {
}
}
RolloutItem::EventMsg(EventMsg::TurnAborted(event)) => {
if !turn_ids_are_compatible(preceding_turn_id, event.turn_id.as_deref()) {
continue;
}
if let Some(active_segment) = active_segment.as_mut() {
if active_segment.turn_id.is_none()
&& let Some(turn_id) = &event.turn_id
@@ -322,15 +367,34 @@ impl Session {
active_segment.world_state_replay.push(item);
}
RolloutItem::EventMsg(EventMsg::TurnStarted(event)) => {
started_turn_ids.next();
// Startup can be suspended before any input or context is written.
// Explicit start attribution is authoritative, including unknown fields.
if let Some(attribution) = &event.turn_attribution
&& attribution.turn_id == event.turn_id
{
let segment =
active_segment.get_or_insert_with(ActiveReplaySegment::default);
if turn_ids_are_compatible(
segment.turn_id.as_deref(),
Some(event.turn_id.as_str()),
) {
segment.turn_attribution = Some(attribution.clone());
}
}
// `TurnStarted` is the oldest boundary of the active reverse segment.
if active_segment.as_ref().is_some_and(|active_segment| {
turn_ids_are_compatible(
active_segment.turn_id.as_deref(),
Some(event.turn_id.as_str()),
)
}) && let Some(active_segment) = active_segment.take()
}) && let Some(mut active_segment) = active_segment.take()
{
finalize_active_segment(
active_segment
.turn_id
.get_or_insert_with(|| event.turn_id.clone());
active_segment.take_checkpoint_attribution(&mut checkpoint_attribution);
let segment_context = finalize_active_segment(
active_segment,
&mut history_checkpoint,
&mut previous_turn_settings,
@@ -339,6 +403,7 @@ impl Session {
&mut window,
&mut pending_rollback_turns,
);
turn_attribution = turn_attribution.or(segment_context);
}
}
RolloutItem::ResponseItem(response_item) => {
@@ -363,12 +428,18 @@ impl Session {
}
if let Some(mut active_segment) = active_segment.take() {
active_segment.take_checkpoint_attribution(&mut checkpoint_attribution);
active_segment.turn_completed |= active_segment
.turn_id
.as_deref()
.or(checkpoint_turn_id)
.is_some_and(|turn_id| late_completed_turn_ids.contains(turn_id));
// A companion turn context only restores the context baseline. Once that turn
// completes, its settings are newer than the compaction metadata.
if resume_metadata.is_some() && !active_segment.turn_completed {
active_segment.previous_turn_settings = None;
}
finalize_active_segment(
let segment_context = finalize_active_segment(
active_segment,
&mut history_checkpoint,
&mut previous_turn_settings,
@@ -377,6 +448,11 @@ impl Session {
&mut window,
&mut pending_rollback_turns,
);
turn_attribution = turn_attribution.or(segment_context);
}
if pending_rollback_turns == 0 {
turn_attribution = turn_attribution.or(checkpoint_attribution);
}
if previous_turn_settings.is_none() {
@@ -527,6 +603,7 @@ impl Session {
history: history.into_annotated_items(),
previous_turn_settings,
reference_context_item,
turn_attribution,
world_state_baseline,
window_number: window.number,
first_window_id: window.first_id,
@@ -260,6 +260,7 @@ fn completed_user_turn_rollout(
let mut rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -524,6 +525,7 @@ async fn record_initial_history_resumed_hydrates_previous_turn_settings_from_lif
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -581,14 +583,22 @@ async fn reconstruct_history_rollback_keeps_history_and_metadata_in_sync_for_com
let (session, turn_context) = make_session_and_context().await;
let mut first_context_item = turn_context.to_turn_context_item();
first_context_item.cyber_access_program = Some(CyberAccessProgram::DaybreakBlue);
first_context_item.root_turn_id = Some("root-turn".to_owned());
let first_turn_id = first_context_item
.turn_id
.clone()
.expect("turn context should have turn_id");
let first_attribution: codex_history::TurnAttribution = object!({
"turn_id": first_turn_id,
"turn_trigger": "composer",
"parent_turn_id": "parent-turn",
"root_turn_id": "root-turn"
});
let mut rolled_back_context_item = first_context_item.clone();
rolled_back_context_item.turn_id = Some("rolled-back-turn".to_string());
rolled_back_context_item.model = "rolled-back-model".to_string();
rolled_back_context_item.cyber_access_program = Some(CyberAccessProgram::DaybreakRed);
rolled_back_context_item.root_turn_id = Some("rolled-back-root".to_owned());
let rolled_back_turn_id = rolled_back_context_item
.turn_id
.clone()
@@ -601,6 +611,7 @@ async fn reconstruct_history_rollback_keeps_history_and_metadata_in_sync_for_com
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: Some(first_attribution.clone()),
turn_id: first_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -638,6 +649,12 @@ async fn reconstruct_history_rollback_keeps_history_and_metadata_in_sync_for_com
)),
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: Some(object!({
"turn_id": rolled_back_turn_id,
"turn_trigger": "automation",
"parent_turn_id": "rolled-back-parent",
"root_turn_id": "rolled-back-root"
})),
turn_id: rolled_back_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -696,6 +713,7 @@ async fn reconstruct_history_rollback_keeps_history_and_metadata_in_sync_for_com
"cyber_access_program": "daybreak_blue",
})
);
assert_eq!(reconstructed.turn_attribution, Some(first_attribution));
assert_eq!(
serde_json::to_value(reconstructed.reference_context_item)
.expect("serialize reconstructed reference context item"),
@@ -726,6 +744,7 @@ async fn reconstruct_history_rollback_keeps_history_and_metadata_in_sync_for_inc
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: first_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -760,6 +779,7 @@ async fn reconstruct_history_rollback_keeps_history_and_metadata_in_sync_for_inc
)),
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: incomplete_turn_id,
root_turn_id: None,
trace_id: None,
@@ -829,6 +849,7 @@ async fn reconstruct_history_rollback_skips_non_user_turns_for_history_and_metad
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: first_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -863,6 +884,7 @@ async fn reconstruct_history_rollback_skips_non_user_turns_for_history_and_metad
)),
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: second_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -896,6 +918,7 @@ async fn reconstruct_history_rollback_skips_non_user_turns_for_history_and_metad
)),
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: standalone_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -970,6 +993,7 @@ async fn reconstruct_history_rollback_counts_inter_agent_assistant_turns() {
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: first_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -1004,6 +1028,7 @@ async fn reconstruct_history_rollback_counts_inter_agent_assistant_turns() {
)),
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: assistant_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -1070,6 +1095,7 @@ async fn reconstruct_history_rollback_clears_history_and_metadata_when_exceeding
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: only_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -1114,6 +1140,7 @@ async fn reconstruct_history_rollback_clears_history_and_metadata_when_exceeding
assert_eq!(reconstructed.history, Vec::new());
assert_eq!(reconstructed.previous_turn_settings, None);
assert!(reconstructed.reference_context_item.is_none());
assert!(reconstructed.turn_attribution.is_none());
}
#[tokio::test]
@@ -1128,6 +1155,7 @@ async fn record_initial_history_resumed_rollback_skips_only_user_turns() {
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: user_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -1161,6 +1189,7 @@ async fn record_initial_history_resumed_rollback_skips_only_user_turns() {
// Standalone task turn (no UserMessage) should not consume rollback skips.
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: standalone_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -1211,6 +1240,7 @@ async fn record_initial_history_resumed_rollback_drops_incomplete_user_turn_comp
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: previous_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -1243,6 +1273,7 @@ async fn record_initial_history_resumed_rollback_drops_incomplete_user_turn_comp
)),
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: incomplete_turn_id,
root_turn_id: None,
trace_id: None,
@@ -1581,6 +1612,7 @@ async fn bounded_replay_matches_full_replay_after_empty_turn_compactions(
resume_metadata: current.then(|| codex_history::CompactionResumeMetadata {
multi_agent_version: None,
last_started_turn_id: Some(format!("wake-{window_number}")),
turn_attribution: None,
previous_turn_settings: Some(PreviousTurnSettings {
model: format!("metadata-model-{window_number}"),
comp_hash: Some(format!("metadata-hash-{window_number}")),
@@ -1687,17 +1719,147 @@ async fn paginated_compaction_does_not_restore_missing_companions_from_older_his
assert_eq!(reconstructed.previous_turn_settings, None);
assert_eq!(reconstructed.reference_context_item, None);
assert_eq!(reconstructed.turn_attribution, None);
assert_eq!(reconstructed.world_state_baseline, None);
}
}
#[derive(Clone, Copy)]
enum AttributionEvidence {
StartAndContext,
StartOnly,
}
#[test_case(AttributionEvidence::StartAndContext; "late completion")]
#[test_case(AttributionEvidence::StartOnly; "late abort before current context")]
#[tokio::test]
async fn completed_turn_suffix_after_compaction_overrides_resume_metadata() {
async fn reconstruct_attribution_ignores_previous_turn_terminal(evidence: AttributionEvidence) {
let (session, turn_context) = make_session_and_context().await;
let older_attribution = turn_context.attribution();
let expected: codex_history::TurnAttribution = object!({
"turn_id": "current-turn",
"turn_trigger": "automation",
"parent_turn_id": "calling-turn",
"root_turn_id": "root-turn",
"initiating_agent_path": "/root/requester"
});
let mut current_context = turn_context.to_turn_context_item();
current_context.turn_id = Some(expected.turn_id.clone());
current_context.root_turn_id = expected.root_turn_id.clone();
let mut rollout = completed_user_turn_rollout(turn_context.to_turn_context_item(), Vec::new());
let RolloutItem::EventMsg(EventMsg::TurnStarted(start)) = &mut rollout[0] else {
panic!("expected previous turn start");
};
start.turn_attribution = Some(older_attribution.clone());
let mut late_terminal = rollout.pop().expect("previous turn completion");
let mut current = completed_user_turn_rollout(current_context, Vec::new());
current.pop().expect("current turn completion");
let RolloutItem::EventMsg(EventMsg::TurnStarted(start)) = &mut current[0] else {
panic!("expected current turn start");
};
start.turn_attribution = Some(expected.clone());
if matches!(evidence, AttributionEvidence::StartOnly) {
current.truncate(1);
late_terminal = RolloutItem::EventMsg(EventMsg::TurnAborted(object!({
"turn_id": older_attribution.turn_id,
"reason": "interrupted"
})));
}
rollout.extend(current);
rollout.push(late_terminal);
let reconstructed = session
.reconstruct_history_from_rollout(&turn_context, &rollout)
.await;
assert_eq!(reconstructed.turn_attribution, Some(expected));
if matches!(evidence, AttributionEvidence::StartAndContext) {
rollout.push(RolloutItem::EventMsg(EventMsg::ThreadRolledBack(
ThreadRolledBackEvent { num_turns: 1 },
)));
let rolled_back = session
.reconstruct_history_from_rollout(&turn_context, &rollout)
.await;
assert_eq!(rolled_back.turn_attribution, Some(older_attribution));
}
}
#[test_case(Some("current-turn"); "continuation marker retained")]
#[test_case(None; "continuation marker cleared")]
#[tokio::test]
async fn reconstruct_attribution_ignores_previous_terminal_after_checkpoint(
last_started_turn_id: Option<&str>,
) {
let (session, turn_context) = make_session_and_context().await;
let expected: codex_history::TurnAttribution = object!({
"turn_id": "current-turn",
"turn_trigger": "automation",
"parent_turn_id": "calling-turn",
"root_turn_id": "root-turn"
});
let mut context = turn_context.to_turn_context_item();
context.turn_id = Some(expected.turn_id.clone());
context.root_turn_id = expected.root_turn_id.clone();
let settings: PreviousTurnSettings = object!({"model": "checkpoint-model"});
let late_terminal =
completed_user_turn_rollout(turn_context.to_turn_context_item(), Vec::new())
.pop()
.expect("previous turn completion");
let mut rollout = vec![
RolloutItem::Compacted(object!({
"message": "summary",
"replacement_history": [user_message("original work")],
"window_number": 1,
"resume_metadata": {
"last_started_turn_id": last_started_turn_id,
"turn_attribution": expected,
"previous_turn_settings": settings
}
})),
RolloutItem::ResponseItem(user_message("continued work").into()),
RolloutItem::TurnContext(context.clone()),
late_terminal,
];
let reconstructed = session
.reconstruct_history_from_rollout(&turn_context, &rollout)
.await;
assert_eq!(reconstructed.turn_attribution, Some(expected));
assert_eq!(reconstructed.reference_context_item, Some(context));
assert_eq!(reconstructed.previous_turn_settings, Some(settings));
assert_eq!(
reconstructed.last_started_turn_id.as_deref(),
last_started_turn_id
);
rollout.push(RolloutItem::EventMsg(EventMsg::ThreadRolledBack(
ThreadRolledBackEvent { num_turns: 1 },
)));
let rolled_back = session
.reconstruct_history_from_rollout(&turn_context, &rollout)
.await;
assert_eq!(rolled_back.turn_attribution, None);
}
#[test_case(false, false; "completion before next turn")]
#[test_case(true, false; "completion after next turn starts")]
#[test_case(true, true; "late completion without checkpoint turn identity")]
#[tokio::test]
async fn completed_turn_suffix_after_compaction_overrides_resume_metadata(
late_completion: bool,
legacy_checkpoint: bool,
) {
let (session, turn_context) = make_session_and_context().await;
let mut newer_context = turn_context.to_turn_context_item();
newer_context.turn_id = Some("newer-turn".to_string());
newer_context.model = "newer-model".to_string();
newer_context.comp_hash = Some("newer-hash".to_string());
let checkpoint_attribution = (!legacy_checkpoint).then(|| codex_history::TurnAttribution {
turn_id: "newer-turn".to_string(),
turn_trigger: Some("automation".to_string()),
parent_turn_id: None,
initiating_agent_path: None,
root_turn_id: None,
});
let expected_settings = PreviousTurnSettings {
model: newer_context.model.clone(),
comp_hash: newer_context.comp_hash.clone(),
@@ -1710,6 +1872,8 @@ async fn completed_turn_suffix_after_compaction_overrides_resume_metadata() {
"replacement_history": [user_message("seed"), assistant_message("summary")],
"window_number": 1,
"resume_metadata": {
"last_started_turn_id": checkpoint_attribution.as_ref().map(|value| value.turn_id.as_str()),
"turn_attribution": checkpoint_attribution,
"previous_turn_settings": {
"model": "metadata-model",
"comp_hash": "metadata-hash"
@@ -1725,6 +1889,17 @@ async fn completed_turn_suffix_after_compaction_overrides_resume_metadata() {
.into_iter()
.skip(2),
);
let next_attribution: Option<codex_history::TurnAttribution> =
late_completion.then(|| object!({"turn_id": "next-turn", "turn_trigger": "user"}));
if let Some(attribution) = &next_attribution {
rollout_items.insert(
rollout_items.len() - 1,
RolloutItem::EventMsg(EventMsg::TurnStarted(object!({
"turn_id": attribution.turn_id,
"turn_attribution": attribution
}))),
);
}
let reconstructed = session
.reconstruct_history_from_rollout(&turn_context, &rollout_items)
@@ -1734,7 +1909,35 @@ async fn completed_turn_suffix_after_compaction_overrides_resume_metadata() {
reconstructed.previous_turn_settings,
Some(expected_settings)
);
assert_eq!(reconstructed.reference_context_item, Some(newer_context));
assert_eq!(
reconstructed.turn_attribution,
next_attribution.or(checkpoint_attribution)
);
assert_eq!(
reconstructed.reference_context_item,
Some(newer_context.clone())
);
// A rollback of a newer user turn restores the checkpoint's older regular turn.
let RolloutItem::Compacted(checkpoint) = &mut rollout_items[0] else {
panic!("expected checkpoint");
};
let metadata = checkpoint
.resume_metadata
.as_mut()
.expect("resume metadata");
let older_attribution: codex_history::TurnAttribution =
object!({"turn_id": "older-turn", "turn_trigger": "automation"});
metadata.turn_attribution = Some(older_attribution.clone());
rollout_items.truncate(1);
rollout_items.extend(completed_user_turn_rollout(newer_context, Vec::new()));
rollout_items.push(RolloutItem::EventMsg(EventMsg::ThreadRolledBack(
codex_protocol::protocol::ThreadRolledBackEvent { num_turns: 1 },
)));
let rolled_back = session
.reconstruct_history_from_rollout(&turn_context, &rollout_items)
.await;
assert_eq!(rolled_back.turn_attribution, Some(older_attribution));
}
#[tokio::test]
@@ -1870,6 +2073,7 @@ async fn reconstruct_history_legacy_compaction_without_replacement_history_clear
}),
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: current_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -1948,6 +2152,7 @@ async fn record_initial_history_resumed_turn_context_after_compaction_reestablis
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: previous_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -2088,6 +2293,7 @@ async fn record_initial_history_resumed_aborted_turn_without_id_clears_active_tu
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: previous_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -2120,6 +2326,7 @@ async fn record_initial_history_resumed_aborted_turn_without_id_clears_active_tu
)),
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: aborted_turn_id,
root_turn_id: None,
trace_id: None,
@@ -2228,6 +2435,7 @@ async fn record_initial_history_resumed_unmatched_abort_preserves_active_turn_fo
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: previous_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -2260,6 +2468,7 @@ async fn record_initial_history_resumed_unmatched_abort_preserves_active_turn_fo
)),
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: current_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -2369,6 +2578,7 @@ async fn record_initial_history_resumed_trailing_incomplete_turn_compaction_clea
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: previous_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -2401,6 +2611,7 @@ async fn record_initial_history_resumed_trailing_incomplete_turn_compaction_clea
)),
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: incomplete_turn_id,
root_turn_id: None,
trace_id: None,
@@ -2468,6 +2679,7 @@ async fn record_initial_history_resumed_trailing_incomplete_turn_preserves_turn_
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: current_turn_id,
root_turn_id: None,
trace_id: None,
@@ -2557,6 +2769,7 @@ async fn record_initial_history_resumed_replaced_incomplete_compacted_turn_clear
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: previous_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -2589,6 +2802,7 @@ async fn record_initial_history_resumed_replaced_incomplete_compacted_turn_clear
)),
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: compacted_incomplete_turn_id,
root_turn_id: None,
trace_id: None,
@@ -2625,6 +2839,7 @@ async fn record_initial_history_resumed_replaced_incomplete_compacted_turn_clear
// completion/abort for the old one.
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: replacing_turn_id,
root_turn_id: None,
trace_id: None,
+90 -2
View File
@@ -4338,6 +4338,7 @@ async fn record_initial_history_forked_hydrates_previous_turn_settings() {
let rollout_items = vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(
codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -6134,6 +6135,18 @@ async fn compaction_persists_resume_metadata_and_companion_records() {
let (mut session, turn_context) = make_session_and_context().await;
let rollout_path = attach_thread_persistence(&mut session).await;
let turn_context = Arc::new(turn_context);
turn_context
.turn_metadata_state
.set_turn_trigger("automation".to_string());
turn_context
.turn_metadata_state
.set_parent_turn_id("parent-turn".to_string());
turn_context
.turn_metadata_state
.set_root_turn_id("root-turn".to_string());
turn_context.turn_metadata_state.set_initiating_agent_path(
codex_protocol::AgentPath::try_from("/root/requester").expect("requester path"),
);
let turn_context_baseline = turn_context.to_turn_context_item();
let world_state = Arc::new(build_world_state_from_turn_context(&session, &turn_context).await);
let previous_turn_settings = PreviousTurnSettings {
@@ -6146,11 +6159,19 @@ async fn compaction_persists_resume_metadata_and_companion_records() {
.set_previous_turn_settings(Some(previous_turn_settings.clone()))
.await;
session.state.lock().await.last_started_turn_id = Some("checkpoint-turn".into());
session.multi_agent_version = std::sync::OnceLock::from(MultiAgentVersion::V2);
let expected = CompactionResumeMetadata {
multi_agent_version: Some(MultiAgentVersion::V2),
last_started_turn_id: Some("checkpoint-turn".into()),
last_started_turn_id: Some(turn_context.sub_id.clone()),
turn_attribution: Some(codex_history::TurnAttribution {
turn_id: turn_context.sub_id.clone(),
turn_trigger: Some("automation".to_string()),
parent_turn_id: Some("parent-turn".to_string()),
initiating_agent_path: Some(
codex_protocol::AgentPath::try_from("/root/requester").expect("requester path"),
),
root_turn_id: turn_context_baseline.root_turn_id.clone(),
}),
previous_turn_settings: Some(previous_turn_settings),
};
let expected_settings = codex_protocol::protocol::ThreadSettingsAppliedEvent {
@@ -6158,6 +6179,21 @@ async fn compaction_persists_resume_metadata_and_companion_records() {
thread_settings: session.thread_settings_snapshot().await,
};
session
.record_started_turn(&turn_context.sub_id, Some(turn_context.attribution()))
.await;
let compact_context = session.new_default_turn().await;
session
.record_started_turn(&compact_context.sub_id, /*attribution*/ None)
.await;
assert_eq!(
session.state.lock().await.turn_attribution,
expected.turn_attribution
);
session
.record_started_turn(&turn_context.sub_id, Some(turn_context.attribution()))
.await;
let input_goal_ids =
crate::context::UserGoalUpdate::message_ids(session.clone_history().await.raw_items());
// The goal edit is accepted after the compaction input was captured.
@@ -6217,6 +6253,58 @@ async fn compaction_persists_resume_metadata_and_companion_records() {
);
assert_eq!(saved_turn_context, &turn_context_baseline);
assert_eq!(saved_settings, &expected_settings);
for same_turn_input in [false, true] {
let mut rollback_items = vec![RolloutItem::Compacted(compacted.clone())];
if same_turn_input {
rollback_items.push(RolloutItem::ResponseItem(
user_message("steered work").into(),
));
}
rollback_items.push(RolloutItem::EventMsg(EventMsg::ThreadRolledBack(
codex_protocol::protocol::ThreadRolledBackEvent { num_turns: 1 },
)));
let rolled_back = session
.reconstruct_history_from_rollout(&turn_context, &rollback_items)
.await;
assert_eq!(rolled_back.turn_attribution, None);
}
// Settings invalidate continuation eligibility; standalone compaction replaces its ID.
// Neither changes the regular work whose attribution the checkpoint preserves.
for last_started_turn_id in [None, Some("compact-turn".to_string())] {
let mut checkpoint = compacted.clone();
checkpoint
.resume_metadata
.as_mut()
.expect("resume metadata")
.last_started_turn_id = last_started_turn_id.clone();
let mut items = vec![RolloutItem::Compacted(checkpoint)];
if let Some(turn_id) = last_started_turn_id {
items.push(RolloutItem::EventMsg(EventMsg::TurnComplete(
TurnCompleteEvent {
turn_id,
last_agent_message: None,
error: None,
started_at: None,
completed_at: None,
duration_ms: None,
time_to_first_token_ms: None,
},
)));
}
let reconstructed = session
.reconstruct_history_from_rollout(&turn_context, &items)
.await;
assert_eq!(reconstructed.turn_attribution, expected.turn_attribution);
assert_eq!(reconstructed.reference_context_item, None);
items.push(RolloutItem::EventMsg(EventMsg::ThreadRolledBack(
codex_protocol::protocol::ThreadRolledBackEvent { num_turns: 1 },
)));
let rolled_back = session
.reconstruct_history_from_rollout(&turn_context, &items)
.await;
assert_eq!(rolled_back.turn_attribution, None);
}
}
#[tokio::test]
+10
View File
@@ -523,6 +523,16 @@ impl TurnContext {
.strict_auto_review_enabled
}
pub(crate) fn attribution(&self) -> codex_history::TurnAttribution {
codex_history::TurnAttribution {
turn_id: self.sub_id.clone(),
turn_trigger: self.turn_metadata_state.current_turn_trigger(),
parent_turn_id: self.turn_metadata_state.parent_turn_id(),
initiating_agent_path: self.turn_metadata_state.initiating_agent_path().cloned(),
root_turn_id: self.turn_metadata_state.root_turn_id(),
}
}
/// Captures current model metadata without preparing a step.
pub(crate) fn capture_current_model_info(&self) -> Arc<ModelInfo> {
Arc::clone(&self.next_step_settings.load().model_info)
+17 -6
View File
@@ -138,6 +138,7 @@ impl PreparedTurnInputSettings {
final_output_json_schema,
service_tier,
parent_turn_id,
initiating_agent_path,
root_turn_id,
cyber_access_program,
} = self.start_options;
@@ -183,6 +184,11 @@ impl PreparedTurnInputSettings {
thread_settings::emit_applied(session, submission_id, settings_snapshot).await;
}
if let Some(parent_turn_id) = parent_turn_id {
if let Some(initiating_agent_path) = initiating_agent_path {
turn_context
.turn_metadata_state
.set_initiating_agent_path(initiating_agent_path);
}
turn_context
.turn_metadata_state
.set_parent_turn_id(parent_turn_id);
@@ -288,10 +294,7 @@ pub(super) async fn handle_recovery(
} = thread_settings.into();
let request = TurnInputRequest::user_input(Vec::new())
.with_thread_settings(thread_settings)
.on_start(TurnStartOptions {
turn_trigger: Some("retry".to_string()),
..start_options
});
.on_start(start_options);
let result = start_if_idle(
session,
WithTurnExtensionData {
@@ -633,8 +636,16 @@ async fn steer(
impl Session {
/// Called under the active-turn lock before running any task or lifecycle callback.
pub(crate) async fn record_started_turn(&self, turn_id: &str) {
self.state.lock().await.last_started_turn_id = Some(turn_id.to_string());
pub(crate) async fn record_started_turn(
&self,
turn_id: &str,
attribution: Option<codex_history::TurnAttribution>,
) {
let mut state = self.state.lock().await;
state.last_started_turn_id = Some(turn_id.to_owned());
if let Some(attribution) = attribution {
state.turn_attribution = Some(attribution);
}
}
pub(crate) async fn route_realtime_text_input(
+21
View File
@@ -11,6 +11,7 @@ use super::AdditionalContextStore;
use super::auto_compact_window::AutoCompactWindow;
use super::auto_compact_window::AutoCompactWindowIds;
use super::auto_compact_window::AutoCompactWindowSnapshot;
use crate::TurnStartOptions;
use crate::context_manager::ContextManager;
use crate::context_manager::HistoryReplacement;
use crate::session::PreviousTurnSettings;
@@ -18,6 +19,7 @@ use crate::session::session::SessionConfiguration;
use crate::session::startup_prewarm::SessionStartupPrewarmHandle;
use crate::session::time_reminder::CurrentTimeReminderState;
use codex_history::ResponseItemEnvelope;
use codex_history::TurnAttribution;
use codex_protocol::SessionId;
use codex_protocol::ThreadId;
use codex_protocol::protocol::RateLimitSnapshot;
@@ -84,6 +86,8 @@ pub(crate) struct SessionState {
/// Latest task admitted in this runtime, retained across completion and history edits.
/// Cleared by standalone settings changes to invalidate pending continuation.
pub(crate) last_started_turn_id: Option<String>,
/// Latest regular turn, retained across completion, settings changes, and compaction.
pub(crate) turn_attribution: Option<TurnAttribution>,
/// Runtime accounting state for the active auto-compaction window.
auto_compact_window: AutoCompactWindow,
/// Original request effort for the current model while configuration updates remain active.
@@ -129,6 +133,7 @@ impl SessionState {
additional_context: AdditionalContextStore::default(),
previous_turn_settings: None,
last_started_turn_id: None,
turn_attribution: None,
auto_compact_window: AutoCompactWindow::new_with_ids(auto_compact_window_ids),
reasoning_effort_pin: ReasoningEffortPin::Unset,
shutting_down: false,
@@ -191,6 +196,7 @@ impl SessionState {
.history
.replace_compacted(items, reviewer_compaction_hash.as_deref()),
HistoryReplacement::Reset => {
self.turn_attribution = None;
self.history.replace_annotated(items);
true
}
@@ -253,6 +259,21 @@ impl SessionState {
self.history.reference_context_item()
}
pub(crate) fn recovered_turn_start_options(&self, turn_id: &str) -> TurnStartOptions {
self.turn_attribution
.as_ref()
.filter(|attribution| attribution.turn_id == turn_id)
.map(TurnAttribution::start_options)
.unwrap_or_else(|| TurnStartOptions {
// Older rollouts only persisted the root on the model-context record.
root_turn_id: self
.reference_context_item()
.filter(|context| context.turn_id.as_deref() == Some(turn_id))
.and_then(|context| context.root_turn_id),
..Default::default()
})
}
// Token/rate limit helpers
pub(crate) fn update_token_info_from_usage(
&mut self,
+1 -1
View File
@@ -41,7 +41,7 @@ impl SessionTask for CompactTask {
turn.id = %ctx.sub_id,
);
// Preparation errors must reach the task runner, which reports them to the client.
session.emit_turn_started(&ctx).await;
session.emit_turn_started(&ctx, TaskKind::Compact).await;
let step_context = session
.capture_step_context(Arc::clone(&ctx), &cancellation_token)
.await?;
+5 -1
View File
@@ -315,7 +315,11 @@ impl Session {
let (pending_items, _) = self.input_queue.drain_mailbox_input_items().await;
let turn_state = {
let mut active = self.active_turn.lock().await;
self.record_started_turn(&turn_context.sub_id).await;
self.record_started_turn(
&turn_context.sub_id,
(task_kind == TaskKind::Regular).then(|| turn_context.attribution()),
)
.await;
let turn = active.get_or_insert_with(ActiveTurn::default);
debug_assert!(turn.task.is_none());
Arc::clone(&turn.turn_state)
+1 -1
View File
@@ -48,7 +48,7 @@ impl SessionTask for RegularTask {
// Regular turns emit `TurnStarted` inline so first-turn lifecycle does
// not wait on startup prewarm resolution.
let prewarmed_client_session = async {
sess.emit_turn_started(&ctx).await;
sess.emit_turn_started(&ctx, TaskKind::Regular).await;
// Regular-start contributors run once, after the task is visible and interruptible.
let prepares_mcp = sess
.services
+3 -1
View File
@@ -121,7 +121,9 @@ pub(crate) async fn execute_user_shell_command(
// standalone lifecycle tasks (for example /shell, and review once it emits TurnStarted).
// `/compact` is an intentional exception because compaction requests should not include
// freshly reinjected context before the summary/replacement history is applied.
session.emit_turn_started(&turn_context).await;
session
.emit_turn_started(&turn_context, TaskKind::Regular)
.await;
}
let Some((turn_environment, environment_shell)) = turn_context
@@ -799,6 +799,7 @@ fn out_of_range_truncation_drops_pre_user_active_turn_prefix() {
RolloutItem::ResponseItem(user_msg("u1").into()),
RolloutItem::ResponseItem(assistant_msg("a1").into()),
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: "turn-2".to_string(),
root_turn_id: None,
trace_id: None,
@@ -3125,6 +3126,7 @@ async fn interrupted_fork_snapshot_preserves_explicit_turn_id() {
history_mode: Some(ThreadHistoryMode::Legacy),
initial_history: InitialHistory::Forked(vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: "turn-explicit".to_string(),
root_turn_id: None,
trace_id: None,
@@ -43,6 +43,7 @@ fn assistant_msg(text: &str) -> ResponseItem {
fn turn_started(turn_id: &str) -> RolloutItem {
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: turn_id.to_string(),
root_turn_id: None,
trace_id: None,
@@ -62,6 +62,7 @@ fn resume_history(
conversation_id: ThreadId::default(),
history: Arc::new(vec![
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -28,6 +28,7 @@ use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::SubAgentSource;
use codex_protocol::protocol::ThreadSettingsOverrides;
use codex_protocol::protocol::TurnEnvironmentSelections;
use codex_protocol::turn_input::TurnAttribution;
use codex_protocol::user_input::UserInput;
use core_test_support::responses;
use core_test_support::responses::ev_completed;
@@ -663,13 +664,318 @@ async fn recover_turn_if_idle_preserves_id_and_resumes_plan_mode() {
.expect("recovered turn should include turn metadata"),
)
.expect("recovered turn metadata should be valid JSON");
assert_eq!(turn_metadata["turn_trigger"].as_str(), Some("retry"));
assert_eq!(
(
turn_metadata.get("turn_trigger"),
turn_metadata.get("parent_turn_id"),
turn_metadata["root_turn_id"].as_str(),
),
(None, None, Some(turn_id))
);
let user_input_groups = request.message_input_text_groups("user");
assert_eq!(user_input_groups.len(), 1);
assert_eq!(user_input_groups[0].len(), 1);
assert!(user_input_groups[0][0].starts_with("<environment_context>"));
}
#[derive(Default)]
struct RecoveryLifecycleBarrier {
block_next: AtomicBool,
entered: tokio::sync::Notify,
block_stop: AtomicBool,
stop_entered: tokio::sync::Notify,
release_stop: tokio::sync::Notify,
}
impl codex_extension_api::TurnLifecycleContributor for RecoveryLifecycleBarrier {
fn turn_start_phase(
&self,
_thread_store: &codex_extension_api::ExtensionData,
) -> codex_extension_api::TurnStartPhase {
codex_extension_api::TurnStartPhase::RegularTaskStart
}
fn on_turn_start<'a>(
&'a self,
_input: codex_extension_api::TurnStartInput<'a>,
) -> codex_extension_api::ExtensionFuture<'a, ()> {
Box::pin(async move {
if self.block_next.swap(false, Ordering::SeqCst) {
self.entered.notify_one();
std::future::pending::<()>().await;
}
})
}
fn on_turn_stop<'a>(
&'a self,
_input: codex_extension_api::TurnStopInput<'a>,
) -> codex_extension_api::ExtensionFuture<'a, ()> {
Box::pin(async move {
if self.block_stop.swap(false, Ordering::SeqCst) {
self.stop_entered.notify_one();
self.release_stop.notified().await;
}
})
}
}
enum RecoveryPause {
AfterContext,
BeforeContext,
AfterLateCompletion,
LegacyRollout,
}
/// Cold recovery preserves attribution across repeated restarts, leaves unknown
/// triggers absent, and starts new turns with fresh attribution.
#[test_case(Some("automation"), RecoveryPause::AfterContext; "automation")]
#[test_case(None, RecoveryPause::AfterContext; "unknown_trigger")]
#[test_case(None, RecoveryPause::LegacyRollout; "legacy_root")]
#[test_case(Some("automation"), RecoveryPause::BeforeContext; "before_context_is_written")]
#[test_case(Some("composer"), RecoveryPause::AfterLateCompletion; "after_previous_turn_completes_late")]
#[tokio::test]
async fn recovery_preserves_durable_turn_attribution(
trigger: Option<&str>,
pause: RecoveryPause,
) -> anyhow::Result<()> {
let mut server = responses::start_mock_server().await;
responses::mount_sse_once(&server, responses::sse_completed("prior-user-turn")).await;
let barrier = Arc::new(RecoveryLifecycleBarrier::default());
let mut extensions = ExtensionRegistryBuilder::new();
extensions.turn_lifecycle_contributor(barrier.clone());
let mut builder = test_codex().with_extensions(Arc::new(extensions.build()));
let mut test = builder.build_with_auto_env(&server).await?;
barrier.block_stop.store(
matches!(pause, RecoveryPause::AfterLateCompletion),
Ordering::SeqCst,
);
let TurnInputSubmission::Started {
turn_id: previous_turn_id,
} = submit_user_message(&test.codex, "establish the initial context baseline").await?
else {
panic!("expected the initial turn");
};
if matches!(pause, RecoveryPause::AfterLateCompletion) {
timeout(
Duration::from_secs(/*secs*/ 10),
barrier.stop_entered.notified(),
)
.await?;
} else {
wait_for_event(&test.codex, |event| {
matches!(event, EventMsg::TurnComplete(event) if event.turn_id == previous_turn_id)
})
.await;
}
let request_received = Arc::new(tokio::sync::Notify::new());
if matches!(pause, RecoveryPause::BeforeContext) {
barrier.block_next.store(true, Ordering::SeqCst);
} else {
let notify = Arc::clone(&request_received);
responses::mount_response_once_match(
&server,
move |_: &wiremock::Request| {
notify.notify_one();
true
},
responses::sse_response(responses::sse_completed("initial"))
.set_delay(Duration::from_secs(/*secs*/ 60)),
)
.await;
}
let parent = trigger.map(|_| "parent-turn");
let initiating_agent_path =
parent.map(|_| AgentPath::root().join("requester").expect("requester path"));
let root = "root-turn";
let request = if trigger == Some("automation") {
TurnInputRequest::user_input(Vec::new())
} else {
user_message_request("recover this work")
};
let request = request.on_start(TurnStartOptions {
turn_trigger: trigger.map(str::to_owned),
parent_turn_id: parent.map(str::to_owned),
root_turn_id: Some(root.to_owned()),
initiating_agent_path: initiating_agent_path.clone(),
..Default::default()
});
let turn_id = if matches!(pause, RecoveryPause::AfterLateCompletion) {
let TurnInputSubmission::Started { turn_id } =
test.codex.start_or_steer_turn(request).await?
else {
panic!("expected a new turn while the previous stop hook is pending");
};
turn_id
} else {
let StartIfIdleSubmission::Started { turn_id } =
test.codex.start_turn_if_idle(request).await?
else {
panic!("expected a new turn");
};
turn_id
};
timeout(Duration::from_secs(/*secs*/ 10), async {
if matches!(pause, RecoveryPause::BeforeContext) {
barrier.entered.notified().await;
} else {
request_received.notified().await;
}
})
.await?;
if matches!(pause, RecoveryPause::AfterLateCompletion) {
barrier.release_stop.notify_one();
wait_for_event(&test.codex, |event| {
matches!(event, EventMsg::TurnComplete(event) if event.turn_id == previous_turn_id)
})
.await;
}
let thread_settings = test.codex.restorable_thread_settings().await;
if let Some(url) = test.executor_environment().exec_server_url() {
builder = builder.with_exec_server_url(url);
}
for attempt in 0..2 {
assert_eq!(
test.codex.suspend_turn_and_shutdown().await?,
codex_core::SuspendTurnOutcome::Suspended {
turn_id: turn_id.clone()
},
);
let rollout_path = test.codex.rollout_path().expect("durable rollout");
test.thread_manager
.remove_thread(&test.session_configured.thread_id)
.await
.expect("suspended thread is loaded");
if attempt == 0 && matches!(pause, RecoveryPause::LegacyRollout) {
let mut legacy_rollout = String::new();
for line in std::fs::read_to_string(&rollout_path)?.lines() {
let mut item: Value = serde_json::from_str(line)?;
if let Some(payload) = item["payload"].as_object_mut() {
payload.remove("turn_attribution");
}
legacy_rollout.push_str(&serde_json::to_string(&item)?);
legacy_rollout.push('\n');
}
std::fs::write(&rollout_path, legacy_rollout)?;
}
server = responses::start_mock_server().await;
let notify = Arc::clone(&request_received);
let response = responses::mount_response_once_match(
&server,
move |_: &wiremock::Request| {
notify.notify_one();
true
},
responses::sse_response(responses::sse_completed("recovered"))
.set_delay(Duration::from_secs(/*secs*/ 60)),
)
.await;
test = builder
.resume(&server, Arc::clone(&test.home), rollout_path)
.await?;
assert_eq!(
test.codex
.recover_turn_if_idle(RecoverTurnRequest {
turn_id: turn_id.clone(),
thread_settings: ThreadSettingsOverrides {
environments: thread_settings.environments.clone(),
..Default::default()
},
trace: None,
cyber_access_program: None,
})
.await?,
StartIfIdleSubmission::Started {
turn_id: turn_id.clone()
},
);
let attribution = wait_for_event_match(&test.codex, |event| match event {
EventMsg::TurnStarted(event) if event.turn_id == turn_id => {
Some(event.turn_attribution.clone())
}
_ => None,
})
.await;
assert_eq!(
attribution,
Some(TurnAttribution {
turn_id: turn_id.clone(),
turn_trigger: trigger.map(str::to_owned),
parent_turn_id: parent.map(str::to_owned),
root_turn_id: Some(root.to_owned()),
initiating_agent_path: initiating_agent_path.clone(),
}),
);
timeout(
Duration::from_secs(/*secs*/ 10),
request_received.notified(),
)
.await?;
let request = response.single_request();
let metadata: Value = serde_json::from_str(
&request
.header("x-codex-turn-metadata")
.expect("turn metadata"),
)?;
assert_eq!(
(
metadata["turn_id"].as_str(),
metadata["turn_trigger"].as_str(),
metadata["parent_turn_id"].as_str(),
metadata["root_turn_id"].as_str(),
),
(Some(turn_id.as_str()), trigger, parent, Some(root)),
"recovery attempt {attempt}",
);
}
test.codex.submit(Op::Interrupt).await?;
wait_for_event(&test.codex, |event| {
matches!(event, EventMsg::TurnAborted(_))
})
.await;
if trigger != Some("automation") {
return Ok(());
}
let next_server = responses::start_mock_server().await;
test = builder.restart(&next_server, &test).await?;
test.codex.restore_thread_settings(thread_settings).await?;
let response = responses::mount_sse_once(&next_server, responses::sse_completed("new")).await;
let TurnInputSubmission::Started {
turn_id: next_turn_id,
} = test
.codex
.start_or_steer_turn(
user_message_request("new human work").on_start(TurnStartOptions {
turn_trigger: Some("composer".to_owned()),
..Default::default()
}),
)
.await?
else {
panic!("expected a new turn");
};
wait_for_event(&test.codex, |event| {
matches!(event, EventMsg::TurnComplete(_))
})
.await;
let request = response.single_request();
let metadata: Value = serde_json::from_str(
&request
.header("x-codex-turn-metadata")
.expect("turn metadata"),
)?;
assert_eq!(
(
metadata["turn_trigger"].as_str(),
metadata.get("parent_turn_id"),
metadata["root_turn_id"].as_str()
),
(Some("composer"), None, Some(next_turn_id.as_str())),
);
Ok(())
}
/// Internal continuation creates a new turn without adding user authorization.
#[tokio::test]
async fn continue_turn_if_idle_starts_new_turn_with_internal_input() {