Fully Automated - 100% Backpressure - Pipeline runner ๐Ÿƒ for AI workflows. ๐Ÿš€

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

@@ -6,7 +6,8 @@
66 /// Keep full history โ€” no truncation.
77 #[default]
88 Full,
9- /// Truncate old messages, keeping the most recent N messages.
9+ /// Keep the system prompt (first message) plus the most recent `keep_last - 1`
10+ /// messages, for `keep_last` total. The first message is always preserved.
1011 Truncate { keep_last: usize },
1112 /// Compact old messages into a summary.
1213 Compact,
@@ -47,14 +48,16 @@
4748 FidelityMode::Truncate { keep_last } => {
4849 if messages.len() <= *keep_last {
4950 messages.to_vec()
50- } else if *keep_last == 0 {
51- Vec::new()
5251 } else {
5352 // Always preserve the first message (system prompt), then
5453 // keep the most recent (keep_last - 1) messages.
55- let tail_count = keep_last - 1;
54+ // saturating_sub handles keep_last=0 without underflow and
55+ // ensures the system prompt is always included.
56+ let tail_count = keep_last.saturating_sub(1);
5657 let mut result = vec![messages[0].clone()];
57- result.extend_from_slice(&messages[messages.len() - tail_count..]);
58+ if tail_count > 0 {
59+ result.extend_from_slice(&messages[messages.len() - tail_count..]);
60+ }
5861 result
5962 }
6063 }
@@ -150,6 +153,15 @@
150153 }
151154
152155 #[test]
156+ fn apply_fidelity_truncate_keep_last_zero_preserves_first() {
157+ // keep_last=0 should still preserve the system prompt (first message),
158+ // consistent with the invariant that the first message is never dropped.
159+ let msgs = vec![1, 2, 3, 4, 5];
160+ let result = apply_fidelity(&msgs, &FidelityMode::Truncate { keep_last: 0 });
161+ assert_eq!(result, vec![1]);
162+ }
163+
164+ #[test]
153165 fn apply_fidelity_compact_keeps_all_for_now() {
154166 let msgs = vec![1, 2, 3];
155167 let result = apply_fidelity(&msgs, &FidelityMode::Compact);