Preserve harness metadata across conversation history (#38058)

## What changed

- Wrap response items with optional harness-owned metadata while keeping the persisted response payload shape backward compatible.
- Store compacted-history metadata in an aligned sidecar and reject malformed sidecars.
- Carry metadata through history normalization, compaction, resume, fork, truncation, migration, and rollout recording without sending it to model providers.

## Testing

- Cover legacy rollout compatibility, metadata serialization and validation, compaction retention, and resume/fork reconstruction.

GitOrigin-RevId: 17a6127c9048ff984cb4df4d97c0751a38c29f5f
This commit is contained in:
rka-oai authored and copyberry committed 2026-08-11 19:30:12 +00:00
1 parent 44d992c14e
commit 3a6f747d77
67 files changed
+2075 -655

No files matched your search

@@ -114,13 +114,13 @@ impl LegacyRolloutCanonicalizer {
let bytes_before = self.bytes_written;
match line.item {
RolloutItem::SessionMeta(_) => return Ok(0),
RolloutItem::ResponseItem(ResponseItem::Other) => {
return Err(migration_error(
"legacy rollout contains an unsupported response item",
));
}
RolloutItem::ResponseItem(response) => {
let hook = match &response {
if matches!(&response.item, ResponseItem::Other) {
return Err(migration_error(
"legacy rollout contains an unsupported response item",
));
}
let hook = match &response.item {
ResponseItem::Message {
role, content, id, ..
} if role == "user" => parse_hook_prompt_message(id.as_deref(), content),
@@ -12,6 +12,7 @@ use codex_protocol::items::parse_hook_prompt_fragment;
use codex_protocol::models::ContentItem;
use codex_protocol::models::ResponseItem;
use codex_protocol::protocol::InterAgentCommunication;
use std::borrow::Borrow;
/// Match the rollback boundaries used by legacy history reconstruction without
/// treating every persisted lifecycle as a user turn. This intentionally stays
@@ -40,14 +41,17 @@ 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(history: &mut Vec<ResponseItem>, num_turns: u32) {
pub(super) fn drop_last_n_user_turns<T>(history: &mut Vec<T>, num_turns: u32)
where
T: Borrow<ResponseItem>,
{
if num_turns == 0 {
return;
}
let user_positions = history
.iter()
.enumerate()
.filter_map(|(index, item)| counts_as_boundary(item).then_some(index))
.filter_map(|(index, item)| counts_as_boundary(item.borrow()).then_some(index))
.collect::<Vec<_>>();
let Some(&first_turn_index) = user_positions.first() else {
return;
@@ -58,7 +62,9 @@ pub(super) fn drop_last_n_user_turns(history: &mut Vec<ResponseItem>, num_turns:
} else {
user_positions[user_positions.len() - count]
};
while cut_index > first_turn_index && is_pre_turn_context_update(&history[cut_index - 1]) {
while cut_index > first_turn_index
&& is_pre_turn_context_update(history[cut_index - 1].borrow())
{
cut_index -= 1;
}
history.truncate(cut_index);
@@ -121,7 +121,9 @@ impl RollbackPlanner {
_ => None,
};
let paired_delivery_boundary = match (&self.pending_delivery_boundary, &line.item) {
(Some(boundary), RolloutItem::ResponseItem(ResponseItem::AgentMessage { .. })) => {
(Some(boundary), RolloutItem::ResponseItem(response))
if matches!(&response.item, ResponseItem::AgentMessage { .. }) =>
{
Some(*boundary)
}
_ => None,
@@ -134,9 +136,9 @@ impl RollbackPlanner {
RolloutItem::ResponseItem(response) => {
if let Some(boundary) = paired_delivery_boundary {
self.record_boundaries[index] = Some(boundary);
} else if rollback::counts_as_boundary(response) {
} else if rollback::counts_as_boundary(&response.item) {
let boundary = self.start_boundary(index);
if let ResponseItem::Message { role, content, .. } = response
if let ResponseItem::Message { role, content, .. } = &response.item
&& role == "user"
{
self.pending_user_response = Some(PendingUserResponse {
@@ -144,7 +146,7 @@ impl RollbackPlanner {
content: content.clone(),
});
}
} else if rollback::is_pre_turn_context_update(response) {
} else if rollback::is_pre_turn_context_update(&response.item) {
// Until another user boundary arrives, this is trailing context for the
// previous turn. Keep that fallback owner so rollback drops it when there is
// no later turn to attach it to.
@@ -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) => {
RolloutItem::ResponseItem(response) if rollback::counts_as_boundary(&response.item) => {
ReplayRecord::UserBoundary
}
RolloutItem::SessionMeta(_)