Commit 2cdb9b65
2cdb9b65dc91Merged
Scott Nixon <[email protected]> ยท 5 months ago ยท 2 files ยท ~2 modified
Description
fix: correct truncation arithmetic and docs for multibyte strings
truncation.rs: HeadTail and Tail modes were computing split points by subtracting a char count from output.len() (bytes), producing positions far too close to the end for multibyte input. Rewrite both paths to use char_indices().nth() for correct char-indexed byte offsets. Remove the now-unused floor/ceil_char_boundary helpers. Add two new tests that exercise actual truncation of multibyte (emoji) content.
Also fix the misleading fast-path comment: the relationship is bytes >= chars in UTF-8, not the reverse.
fidelity.rs: Correct the Truncate variant doc to describe the actual invariant โ the system prompt is always preserved as one of the keep_last slots, so keep_last-1 recent messages are kept alongside it.
Details
- Commit
- 2cdb9b65dc91
- Parent
- 706fd2d1c013
crates/attractor-agent/src/fidelity.rsmodifiedView file
/// Keep full history โ no truncation. #[default] Full, /// Truncate old messages, keeping the most recent N messages. /// Keep the system prompt (first message) plus the most recent `keep_last - 1` /// messages, for `keep_last` total. The first message is always preserved. Truncate { keep_last: usize }, /// Compact old messages into a summary. Compact, FidelityMode::Truncate { keep_last } => { if messages.len() <= *keep_last { messages.to_vec() } else if *keep_last == 0 { Vec::new() } else { // Always preserve the first message (system prompt), then // keep the most recent (keep_last - 1) messages. let tail_count = keep_last - 1; // saturating_sub handles keep_last=0 without underflow and // ensures the system prompt is always included. let tail_count = keep_last.saturating_sub(1); let mut result = vec![messages[0].clone()]; result.extend_from_slice(&messages[messages.len() - tail_count..]); if tail_count > 0 { result.extend_from_slice(&messages[messages.len() - tail_count..]); } result } } } #[test] fn apply_fidelity_truncate_keep_last_zero_preserves_first() { // keep_last=0 should still preserve the system prompt (first message), // consistent with the invariant that the first message is never dropped. let msgs = vec![1, 2, 3, 4, 5]; let result = apply_fidelity(&msgs, &FidelityMode::Truncate { keep_last: 0 }); assert_eq!(result, vec![1]); } #[test] fn apply_fidelity_compact_keeps_all_for_now() { let msgs = vec![1, 2, 3]; let result = apply_fidelity(&msgs, &FidelityMode::Compact);crates/attractor-tools/src/truncation.rsmodifiedView file
Loading diffโฆ