Preserve acceptance order in retained thread context (#42770)

## Why

Queued prompts and `request_user_input` answers can be persisted in a different
order from when the host accepted them. Using persistence order can therefore
reorder retained instructions during replay or apply the wrong rollback
boundary.

## What changed

- Reserve an acceptance sequence for user prompts and elicitation responses
  when retained thread context is enabled.
- Persist that sequence with user-message metadata and verified answers, and
  use it when ordering, bounding, replaying, and rolling back retained context.
- Keep events without acceptance metadata compatible with legacy recording
  order.

## Testing

- Cover delayed queued-input recording, rejected prompts, checkpoint replay,
  compaction, rollback migration, and legacy retained-context behavior.

GitOrigin-RevId: 42d24465cc2033bc51c0c2da79d3c0884a8ad98a
This commit is contained in:
felixxia-oai authored and copyberry committed 2026-09-04 13:50:27 +00:00
1 parent 88f87d907a
commit 99d66aa1c5
24 files changed
+489 -152

No files matched your search

+11 -8
View File
@@ -313,7 +313,7 @@ async fn start_or_steer(
}
let mut task_input = merge_additional_context_input(session, additional_context).await;
if has_explicit_input {
task_input.push(pending_turn_input(input));
task_input.push(pending_turn_input(session, input).await);
}
session
.spawn_task(turn_context, task_input, RegularTask::new())
@@ -424,7 +424,7 @@ async fn start_if_idle(
if let SubmittedTurnInput::UserInput { content, .. } = &input {
turn_context.session_telemetry.user_prompt(content);
}
task_input.push(pending_turn_input(input));
task_input.push(pending_turn_input(session, input).await);
}
TurnStartKind::Automatic => {
// Empty automatic user input resumes sampling without a new message.
@@ -433,7 +433,7 @@ async fn start_if_idle(
.input_queue
.extend_pending_input_for_turn_state(
turn_state.as_ref(),
vec![pending_turn_input(input)],
vec![pending_turn_input(session, input).await],
)
.await;
}
@@ -621,9 +621,10 @@ impl Session {
TurnInput::UserInput {
content: std::mem::take(content),
client_id: client_id.clone(),
acceptance_order: self.reserve_user_input_order().await,
}
}
input => pending_turn_input(input.clone()),
input => pending_turn_input(self, input.clone()).await,
};
pending_input.push(input);
if active_task
@@ -663,11 +664,13 @@ async fn merge_additional_context_input(
.collect()
}
fn pending_turn_input(input: SubmittedTurnInput) -> TurnInput {
async fn pending_turn_input(session: &Session, input: SubmittedTurnInput) -> TurnInput {
match input {
SubmittedTurnInput::UserInput { content, client_id } => {
TurnInput::UserInput { content, client_id }
}
SubmittedTurnInput::UserInput { content, client_id } => TurnInput::UserInput {
content,
client_id,
acceptance_order: session.reserve_user_input_order().await,
},
SubmittedTurnInput::ResponseItem(mut item)
if matches!(
&item,