Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,6 @@ pub(super) async fn handle_pending_thread_resume_request(
thread_status.clone(),
has_live_in_progress_turn,
);
let token_usage_turn_id = pending
.include_turns
.then(|| restored_token_usage_turn_id(&pending.history_items, thread.turns.as_slice()));
let mut initial_turns_page = if let Some(mut page) = pending.paginated_initial_turns_page.take()
{
if let (Some(active_turn), Some(params)) =
Expand Down Expand Up @@ -623,6 +620,12 @@ pub(super) async fn handle_pending_thread_resume_request(
} else {
None
};
let token_usage_turn_id = pending
.include_turns
.then(|| restored_token_usage_turn_id(&pending.history_items, thread.turns.as_slice()));
if pending.initial_turns_page.is_none() {
initial_turns_page = None;
}
if pending.redact_resume_payloads {
redact_thread_resume_payloads(&mut thread.turns);
if let Some(initial_turns_page) = initial_turns_page.as_mut() {
Expand Down
45 changes: 24 additions & 21 deletions codex-rs/app-server/src/request_processors/thread_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3156,8 +3156,6 @@ impl ThreadRequestProcessor {
matches!(thread.history_mode, ThreadHistoryMode::Paginated).then_some(thread.thread_id)
});
let paginated_resume = paginated_thread_id.is_some();
let needs_paginated_projection =
paginated_resume && (include_turns || initial_turns_page.is_some());

let history_cwd = thread_history.session_cwd();
let runtime_workspace_roots = runtime_workspace_roots.map(resolve_runtime_workspace_roots);
Expand Down Expand Up @@ -3246,7 +3244,7 @@ impl ThreadRequestProcessor {
// Paginated JSONL is canonical, but its SQLite projection can lag after a
// previous write failure. Persist after reopening the live writer so legacy
// response hydration reads the latest durable turns and items.
if needs_paginated_projection
if paginated_resume
&& let Err(error) = self
.thread_store
.persist_thread(thread_id)
Expand Down Expand Up @@ -3342,12 +3340,6 @@ impl ThreadRequestProcessor {
let active_permission_profile = thread_response_active_permission_profile(
config_snapshot.active_permission_profile,
);
let token_usage_turn_id = include_turns.then(|| {
restored_token_usage_turn_id(
response_history.get_rollout_items(),
thread.turns.as_slice(),
)
});
let mut initial_turns_page = if let Some(params) = initial_turns_page.as_ref() {
let initial_turns_page_result = if paginated_resume {
self.paginated_resume_initial_turns_page(thread_id, params)
Expand All @@ -3371,6 +3363,18 @@ impl ThreadRequestProcessor {
} else {
None
};
let token_usage_turn_id = (include_turns || paginated_resume)
.then(|| {
let turns = if thread.turns.is_empty() {
initial_turns_page
.as_ref()
.map_or(&[][..], |page| page.data.as_slice())
} else {
thread.turns.as_slice()
};
restored_token_usage_turn_id(response_history.get_rollout_items(), turns)
})
.filter(|turn_id| !turn_id.is_empty());
if redact_resume_payloads {
redact_thread_resume_payloads(&mut thread.turns);
if let Some(initial_turns_page) = initial_turns_page.as_mut() {
Expand Down Expand Up @@ -3583,6 +3587,12 @@ impl ThreadRequestProcessor {
)
.await?;
}
if paginated_resume && (include_turns || params.initial_turns_page.is_some()) {
self.thread_store
.persist_thread(existing_thread_id)
.await
.map_err(thread_store_resume_read_error)?;
}
let history_items = if needs_history {
source_thread
.history
Expand Down Expand Up @@ -3641,15 +3651,6 @@ impl ThreadRequestProcessor {
.thread_goal_processor
.pending_resume_goal_state(existing_thread.as_ref())
.await;
if paginated_resume && (include_turns || params.initial_turns_page.is_some()) {
// Paginated JSONL is canonical, but its SQLite projection can lag after a
// previous write failure. Persist before legacy response hydration reads the
// latest durable turns and items.
self.thread_store
.persist_thread(existing_thread_id)
.await
.map_err(thread_store_resume_read_error)?;
}
let paginated_turns = if paginated_resume && include_turns {
Some(self.paginated_thread_full_turns(existing_thread_id).await?)
} else {
Expand Down Expand Up @@ -4229,7 +4230,7 @@ impl ThreadRequestProcessor {
};
let ephemeral_token_usage_turn_id = (ephemeral && include_turns)
.then(|| restored_token_usage_turn_id(&history_items, ephemeral_turns.as_slice()));
let paginated_history_items = paginated_source.then(|| Arc::clone(&history_items));
let token_usage_history_items = paginated_source.then(|| Arc::clone(&history_items));

let new_thread = if let Some(prepared_fork) = prepared_fork {
self.thread_manager
Expand Down Expand Up @@ -4358,7 +4359,9 @@ impl ThreadRequestProcessor {
restored_token_usage_turn_id(
history
.as_ref()
.map_or(&[], |history| history.items.as_slice()),
.map(|history| history.items.as_slice())
.or_else(|| token_usage_history_items.as_deref().map(Vec::as_slice))
.unwrap_or(&[]),
thread.turns.as_slice(),
)
});
Expand All @@ -4379,7 +4382,7 @@ impl ThreadRequestProcessor {
if paginated_source && include_turns {
thread.turns = self.paginated_thread_full_turns(thread_id).await?;
token_usage_turn_id = Some(restored_token_usage_turn_id(
paginated_history_items
token_usage_history_items
.as_deref()
.map_or(&[], Vec::as_slice),
thread.turns.as_slice(),
Expand Down
14 changes: 14 additions & 0 deletions codex-rs/app-server/src/request_processors/token_usage_replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ fn latest_token_usage_turn_id_from_rollout_items(
builder.handle_rollout_item(item);
}

if turns.is_empty() {
return builder.active_turn_id_if_explicit();
}

let active_turn_id = builder.active_turn_id()?;
if turns.iter().any(|turn| turn.id == active_turn_id) {
Some(active_turn_id.to_string())
Expand Down Expand Up @@ -137,6 +141,16 @@ mod tests {
);
}

#[test]
fn replay_attribution_rejects_suffix_generated_turn_ids() {
let rollout_items = token_usage_history();

assert_eq!(
latest_token_usage_turn_id_from_rollout_items(&rollout_items, /*turns*/ &[]),
None
);
}

#[test]
fn replay_attribution_uses_latest_token_count_and_ignores_tail_turn() {
let mut rollout_items = token_usage_history();
Expand Down
Loading
Loading