Files
codex/codex-rs/guardian-context/src/cursor_tests.rs
T
felixxia-oai b2592701e1 Keep Guardian transcript records structured through budget recovery (#51466)
## What changed

Introduce `TranscriptRecord` and `PreparedTranscript` to retain attributed transcript text through preparation and composition. Cache rendered content, byte counts, and token estimates, refreshing them when text or framing changes.

Keep JSON records structured during budget recovery so truncation replaces only `text`, preserving metadata, field order, and framing without reparsing serialized JSON. Preserve whole-entry truncation for line transcripts and update synchronous and asynchronous consumers to use the preparation API.

## Testing

Expand coverage for cached estimates and delivery with escaped text, Unicode, and large payloads; metadata and framing preservation during recovery; and stable asynchronous history prefixes in both line and JSON formats.

GitOrigin-RevId: 8b3b5231ca4a6ee47d17dcc788ea68c56dd1a9bc
2026-10-06 19:31:24 +00:00

77 lines
2.6 KiB
Rust

//! Checks that profile retention cannot move the collected-transcript cursor backwards.
use super::*;
use crate::ContextProfile;
use crate::ConversationTranscriptEntryKind;
use pretty_assertions::assert_eq;
#[test]
fn delta_cursor_tracks_collected_entries_across_sliding_window_retention() {
let entries = ["first", "second", "third", "fourth"]
.into_iter()
.map(|text| ConversationTranscriptEntry {
kind: ConversationTranscriptEntryKind::Assistant,
content: crate::TranscriptContent::Text(text.to_owned()),
original_bytes: text.len(),
retained_source: None,
})
.collect::<Vec<_>>();
let mut profile = ContextProfile::asynchronous();
profile.retention.max_recent_non_user_entries = 1;
let (selection, cursor) =
TranscriptMode::Full.select(&entries[..3], /*parent_history_version*/ 7);
let TranscriptSelection::Full(initial) = selection else {
panic!("first request must select a full transcript");
};
let initial = profile.prepare_transcript(initial, /*entry_number_offset*/ 0);
assert_eq!(
initial
.items
.into_iter()
.map(|item| match item.content {
crate::TranscriptContent::Record(record) =>
crate::TranscriptContent::Text(record.to_string()),
content => content,
})
.collect::<Vec<_>>(),
vec![crate::TranscriptContent::Text(
"[3] assistant: third\n".to_owned()
)],
);
let (selection, next_cursor) =
TranscriptMode::Delta { cursor }.select(&entries, /*parent_history_version*/ 7);
let TranscriptSelection::Delta {
entries: delta,
offset,
} = selection
else {
panic!("an append must select only the new entry");
};
let delta = profile.prepare_transcript(delta, offset);
assert_eq!(
(
delta
.items
.into_iter()
.map(|item| match item.content {
crate::TranscriptContent::Record(record) =>
crate::TranscriptContent::Text(record.to_string()),
content => content,
})
.collect::<Vec<_>>(),
next_cursor,
),
(
vec![crate::TranscriptContent::Text(
"[4] assistant: fourth\n".to_owned()
)],
TranscriptCursor {
parent_history_version: 7,
transcript_entry_count: 4,
},
),
);
}