Refactor realtime handoff and history access (#62245)

## What changed

Move realtime text routing into `realtime_handoff` and keep it on the shared start-or-steer path. Extract UTF-8-safe range selection for realtime delegation truncation.

Use `RolloutItem` accessors for realtime timestamps and items in recording, clock restoration, and thread history reads. Pass annotated response items to turn-boundary checks in core and migration rollback so callers can work directly with history envelopes.

GitOrigin-RevId: 3dd37cf91ee5abdca84e87b7d7e304749d9baf04
This commit is contained in:
Zahan Malkani
2026-11-18 09:55:30 +00:00
committed by copyberry
parent ca21fe65d1
commit 6d9f0199ed
18 changed files with 152 additions and 104 deletions
@@ -1220,11 +1220,10 @@ impl LocalThreadStore {
} else {
project_rollout_line(&line)
},
realtime_item: match line.item {
RolloutItem::RealtimeItem(item) if !is_inherited_subagent_history => {
Some(item)
}
_ => None,
realtime_item: if is_inherited_subagent_history {
None
} else {
line.item.realtime_item().map(std::borrow::Cow::into_owned)
},
},
)));
@@ -14,14 +14,15 @@ use codex_protocol::models::ResponseItem;
use codex_protocol::protocol::APPS_INSTRUCTIONS_OPEN_TAG;
use codex_protocol::protocol::CATALOG_APPS_INSTRUCTIONS_OPEN_TAG;
use codex_protocol::protocol::InterAgentCommunication;
use std::borrow::Borrow;
use codex_rollout::ResponseItemEnvelope;
/// Match the rollback boundaries used by legacy history reconstruction without
/// treating every persisted lifecycle as a user turn. This intentionally stays
/// local to the frozen migration adapter: the full core predicate also knows
/// about dynamically registered contextual fragments that thread-store cannot
/// depend on.
pub(super) fn counts_as_boundary(response: &ResponseItem) -> bool {
pub(super) fn counts_as_boundary(envelope: &ResponseItemEnvelope) -> bool {
let response = &envelope.item;
if matches!(response, ResponseItem::AgentMessage { .. }) {
return true;
}
@@ -43,17 +44,14 @@ pub(super) fn is_pre_turn_context_update(response: &ResponseItem) -> bool {
/// Apply the same response-history cut used by legacy cold resume to a persisted
/// compaction checkpoint. The migration adapter uses a frozen contextual-fragment
/// matcher because thread-store cannot depend on core's runtime fragment registry.
pub(super) fn drop_last_n_user_turns<T>(history: &mut Vec<T>, num_turns: u32)
where
T: Borrow<ResponseItem>,
{
pub(super) fn drop_last_n_user_turns(history: &mut Vec<ResponseItemEnvelope>, num_turns: u32) {
if num_turns == 0 {
return;
}
let user_positions = history
.iter()
.enumerate()
.filter_map(|(index, item)| counts_as_boundary(item.borrow()).then_some(index))
.filter_map(|(index, item)| counts_as_boundary(item).then_some(index))
.collect::<Vec<_>>();
let Some(&first_turn_index) = user_positions.first() else {
return;
@@ -64,9 +62,7 @@ where
} else {
user_positions[user_positions.len() - count]
};
while cut_index > first_turn_index
&& is_pre_turn_context_update(history[cut_index - 1].borrow())
{
while cut_index > first_turn_index && is_pre_turn_context_update(&history[cut_index - 1].item) {
cut_index -= 1;
}
history.truncate(cut_index);
@@ -182,7 +182,7 @@ impl RollbackPlanner {
self.record_boundaries[index] = Some(boundary);
self.boundaries[boundary].message_id = response.id().cloned();
self.boundaries[boundary].input_source = response.metadata.as_ref().into();
} else if rollback::counts_as_boundary(&response.item) {
} else if rollback::counts_as_boundary(response) {
let boundary = self.start_boundary(index);
self.boundaries[boundary].message_id = response.id().cloned();
self.boundaries[boundary].input_source = response.metadata.as_ref().into();
@@ -68,7 +68,7 @@ impl ModelReplayPlanner {
RolloutItem::TurnContext(context) => ReplayRecord::TurnContext(context.turn_id.clone()),
RolloutItem::EventMsg(EventMsg::UserMessage(_))
| RolloutItem::InterAgentCommunication(_) => ReplayRecord::UserBoundary,
RolloutItem::ResponseItem(response) if rollback::counts_as_boundary(&response.item) => {
RolloutItem::ResponseItem(response) if rollback::counts_as_boundary(response) => {
ReplayRecord::UserBoundary
}
RolloutItem::DynamicToolsUpdated { .. }
@@ -12,7 +12,6 @@ use chrono::DateTime;
use codex_app_server_protocol::ThreadHistoryChangeSet;
use codex_app_server_protocol::project_rollout_line;
use codex_protocol::ThreadId;
use codex_rollout::RolloutItem;
use tracing::warn;
use super::LocalThreadStore;
@@ -318,12 +317,16 @@ fn read_projection_batches(
} else {
project_rollout_line(&line)
};
let realtime_item = if is_inherited_subagent_history {
None
} else {
line.item.realtime_item().map(std::borrow::Cow::into_owned)
};
let fallback_created_at_ms = if changes
.changed_items
.iter()
.any(|change| change.item.started_at_ms.is_none())
|| (!is_inherited_subagent_history
&& matches!(&line.item, RolloutItem::RealtimeItem(_)))
|| realtime_item.is_some()
{
match DateTime::parse_from_rfc3339(line.timestamp.as_str()) {
Ok(timestamp) => Some(timestamp.timestamp_millis()),
@@ -389,10 +392,7 @@ fn read_projection_batches(
end_byte_offset: line_end_offset,
fallback_created_at_ms,
changes,
realtime_item: match line.item {
RolloutItem::RealtimeItem(item) if !is_inherited_subagent_history => Some(item),
_ => None,
},
realtime_item,
},
)));
next_ordinal = next_line_ordinal;