Centralize compaction checkpoint selection and validation (#46009)

## What changed

Add `codex_history::CompactionCheckpoint` to keep a checkpoint and its recorded producer model hash together, with shared checks for usability and reviewer compatibility.

Replace `ConversationHistorySnapshot::latest_compaction_model_hash()` with `latest_compaction()` and use the shared abstraction in Guardian context selection, review sessions, and parent compaction reuse. Preserve selection of the latest checkpoint even when unusable, and leave missing producer metadata unknown.

## Testing

Add coverage showing that the latest checkpoint retains its own provenance even with missing or empty encrypted content, and that compatibility requires matching producer and reviewer hashes. Adapt existing history, migration, and parent compaction tests to the new API.

GitOrigin-RevId: 98e794644edf979c98fcc8294baa48ff82ea0ed0
This commit is contained in:
felixxia-oai
2026-09-16 19:25:50 +00:00
committed by copyberry
parent 2b59d92dbd
commit 7f0ab95827
12 changed files with 187 additions and 105 deletions
@@ -5,7 +5,6 @@ use codex_extension_api::ConversationHistorySnapshot;
use codex_features::Feature;
use codex_features::Features;
use codex_history::ResponseItemEnvelope;
use codex_protocol::models::ResponseItem;
/// Selects legacy compatibility or thread-owned evidence.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
@@ -38,21 +37,9 @@ impl GuardianContextMode {
items: &[ResponseItemEnvelope],
reviewer_compaction_hash: Option<&str>,
) -> Self {
let checkpoint = items.iter().rev().find(|envelope| {
matches!(
envelope.item,
ResponseItem::Compaction { .. } | ResponseItem::ContextCompaction { .. }
)
});
if checkpoint.is_none_or(|checkpoint| {
let producer_hash = checkpoint
.metadata
.as_ref()
.and_then(|metadata| metadata.compaction_model_hash.as_deref());
producer_hash
.zip(reviewer_compaction_hash)
.is_some_and(|(producer, reviewer)| !producer.is_empty() && producer == reviewer)
}) {
if codex_history::CompactionCheckpoint::latest(items)
.is_none_or(|checkpoint| checkpoint.is_compatible_with(reviewer_compaction_hash))
{
self
} else {
Self::Legacy
+2 -12
View File
@@ -125,18 +125,8 @@ pub(crate) enum HistoryReplacement {
}
impl ConversationHistorySnapshot for SharedConversationHistory {
fn latest_compaction_model_hash(&self) -> Option<&str> {
self.items
.iter()
.rev()
.find(|envelope| {
matches!(
envelope.item,
ResponseItem::Compaction { .. } | ResponseItem::ContextCompaction { .. }
)
})
.and_then(|envelope| envelope.metadata.as_ref())
.and_then(|metadata| metadata.compaction_model_hash.as_deref())
fn latest_compaction(&self) -> Option<codex_history::CompactionCheckpoint<'_>> {
codex_history::CompactionCheckpoint::latest(&self.items)
}
fn retained_context(&self) -> Option<&RetainedContext> {
@@ -205,11 +205,14 @@ fn conversation_history_snapshot_binds_review_mode_and_hash_to_the_latest_item(
assert_eq!(
history
.conversation_history_snapshot()
.latest_compaction_model_hash(),
.latest_compaction()
.and_then(|checkpoint| checkpoint.model_hash),
latest_hash
);
assert_eq!(
snapshot.latest_compaction_model_hash(),
snapshot
.latest_compaction()
.and_then(|checkpoint| checkpoint.model_hash),
Some("producer-hash")
);
assert_eq!(
@@ -230,7 +233,8 @@ fn conversation_history_snapshot_binds_review_mode_and_hash_to_the_latest_item(
assert_eq!(
history
.conversation_history_snapshot()
.latest_compaction_model_hash(),
.latest_compaction()
.and_then(|checkpoint| checkpoint.model_hash),
Some("producer-hash")
);
}
@@ -274,7 +278,12 @@ fn checkpoint_retained_evidence_survives_legacy_review(saved_context: serde_json
expected_mode
);
assert_eq!(snapshot.retained_context(), Some(&retained));
assert_eq!(snapshot.latest_compaction_model_hash(), None);
assert_eq!(
snapshot
.latest_compaction()
.and_then(|checkpoint| checkpoint.model_hash),
None
);
}
}
@@ -54,29 +54,12 @@ impl ReviewContextPolicy {
if self == Self::Legacy {
return Ok(None);
}
let Some(envelope) = history.annotated_items().iter().rev().find(|envelope| {
matches!(
envelope.item,
ResponseItem::Compaction { .. } | ResponseItem::ContextCompaction { .. }
)
}) else {
let Some(checkpoint) =
codex_history::CompactionCheckpoint::latest(history.annotated_items())
else {
return Ok(None);
};
let item = &envelope.item;
let valid = match item {
ResponseItem::Compaction {
id: Some(_),
encrypted_content,
..
} if !encrypted_content.is_empty() => true,
ResponseItem::ContextCompaction {
id: Some(_),
encrypted_content: Some(encrypted_content),
..
} if !encrypted_content.is_empty() => true,
_ => false,
};
let valid = checkpoint.is_usable();
if !valid && !strict {
return Ok(None);
}
@@ -88,12 +71,10 @@ impl ReviewContextPolicy {
// A resumed parent may now use a different model. Compare the actual
// checkpoint producer with the selected reviewer, not the live parent model.
anyhow::ensure!(
GuardianContextMode::ThreadOwned
.for_checkpoint(history.annotated_items(), reviewer_compaction_hash)
== GuardianContextMode::ThreadOwned,
checkpoint.is_compatible_with(reviewer_compaction_hash),
"parent compaction checkpoint is incompatible with the Guardian review model or its compatibility is unknown"
);
}
Ok(Some(item.clone()))
Ok(Some(checkpoint.item.clone()))
}
}