Files
codex/codex-rs/guardian-context/src/reviews_tests.rs
T
ioanbudea-OAI 1c9b058d26 Unify Guardian context preparation across reviewers (#60498)
## Why

Asynchronous Guardian collection applied entry caps and compressed heartbeat messages before request preparation, while synchronous collection preserved originals. Preserve the captured evidence so request-time profiles and budgeting determine what is delivered.

## What changed

- Replace `prepare_sync_context` with a shared `prepare_context` entry point for synchronous and asynchronous presentations.
- Defer text caps, native-message omission, and heartbeat compression to preparation, preserving heartbeat reference numbering across transcript deltas.
- Support elastic transcript preparation for both reviewer profiles.
- Borrow collected asynchronous context instead of cloning the full transcript, and filter previous reviews against reviewer history during preparation.

## Testing

Add coverage for collection preserving originals, fixed preparation truncating text and omitting oversized native entries, and elastic preparation deferring entry caps for both profiles. Update heartbeat tests to cover full and delta preparation without mutating collected evidence.

GitOrigin-RevId: 4d4e448acf5dc8880a05c3c9a9754378135f61eb
2026-11-10 02:18:39 +00:00

114 lines
3.8 KiB
Rust

//! Constructor boundaries preserve valid evidence and reject oversize sections.
use super::*;
use codex_protocol_test_support::uuid_v7_item_id_generator_for_test;
use pretty_assertions::assert_eq;
#[test]
fn previous_reviews_checks_count_and_size_without_rewriting_fragments() {
let max_bytes = TruncationPolicy::Tokens(MAX_REVIEW_FRAGMENT_TOKENS).byte_budget();
let fragment = "é".repeat(max_bytes / 2);
let fragments = vec![
PreviousReview {
id: uuid_v7_item_id_generator_for_test()("review"),
fragment: fragment.clone(),
};
MAX_PREVIOUS_REVIEWS
];
let reviews = PreviousReviews::try_from_fragments(fragments.clone()).unwrap();
let ResponseItem::Message { content, .. } = reviews.into_annotated_message().item else {
panic!("expected a review message");
};
assert_eq!(
&content[1..],
fragments
.into_iter()
.map(|review| ContentItem::InputText {
text: review.fragment
})
.collect::<Vec<_>>()
.as_slice()
);
for invalid in [
vec!["review".to_owned(); MAX_PREVIOUS_REVIEWS + 1],
vec![format!("{fragment}a")],
] {
assert_eq!(
PreviousReviews::try_from_fragments(
invalid
.into_iter()
.map(|fragment| PreviousReview {
id: uuid_v7_item_id_generator_for_test()("review"),
fragment,
})
.collect()
),
Err(SectionError::EvidenceLimitExceeded {
section: "previous_reviews",
})
);
}
}
#[test]
fn review_delivery_requires_complete_host_metadata_and_preserves_distinct_completions() {
let first = PreviousReview {
id: uuid_v7_item_id_generator_for_test()("review"),
fragment: "Approved reading README.md.".to_owned(),
};
// A different completion remains new evidence even when its text is identical.
let second = PreviousReview {
id: uuid_v7_item_id_generator_for_test()("review"),
fragment: first.fragment.clone(),
};
let first_message = PreviousReviews::try_from_fragments(vec![first.clone()])
.unwrap()
.into_annotated_message();
let second_message = PreviousReviews::try_from_fragments(vec![second.clone()])
.unwrap()
.into_annotated_message();
let mut shortened = first_message.clone();
shortened
.metadata
.as_mut()
.unwrap()
.mark_retained_sources_incomplete();
let both = vec![first, second.clone()];
for (history, expected) in [
(Vec::new(), both.clone()),
(
vec![ResponseItemEnvelope::new(first_message.item.clone())],
both.clone(),
),
(vec![shortened], both.clone()),
(vec![first_message.clone()], vec![second]),
(vec![first_message, second_message], Vec::new()),
] {
let context = CollectedContext {
sections: vec![ContextSection::PreviousReviews(
PreviousReviews::try_from_fragments(both.clone()).unwrap(),
)],
};
let delivered = crate::prepare_context(
std::borrow::Cow::Borrowed(&context),
&crate::ContextProfile::asynchronous(),
/*entry_number_offset*/ 0,
crate::ContextPresentation::Async,
crate::ContextBudgeting::Fixed,
&history,
)
.unwrap()
.into_annotated_messages();
let expected = if expected.is_empty() {
Vec::new()
} else {
vec![
PreviousReviews::try_from_fragments(expected)
.unwrap()
.into_annotated_message(),
]
};
assert_eq!(delivered, expected);
}
}