pub(crate) const MINIMAX_NOISE_TOKEN: &str = "]<]minimax[>[";
const PROVIDER_NOISE_TOKENS: &[&str] = &[MINIMAX_NOISE_TOKEN];
pub(crate) const RECOVERY_NOISE_FALLBACK: &str = "I was unable to complete this task from the available context. Re-run the request, \
or provide more specific guidance so I can proceed without re-reading the same resources.";
#[inline]
pub(crate) fn contains_provider_noise(text: &str) -> bool {
PROVIDER_NOISE_TOKENS.iter().any(|token| text.contains(token))
}
#[inline]
pub(crate) fn noise_token_partial_suffix(text: &str) -> Option<usize> {
for token in PROVIDER_NOISE_TOKENS {
for prefix_len in (1..token.len()).rev() {
if text.ends_with(&token[..prefix_len]) {
return Some(prefix_len);
}
}
}
None
}
#[inline]
pub(crate) fn strip_provider_noise(text: &str) -> String {
let mut cleaned = text.to_string();
for token in PROVIDER_NOISE_TOKENS {
if cleaned.contains(token) {
cleaned = cleaned.replace(token, "");
}
}
cleaned
}
#[inline]
pub(crate) fn sanitize_recovery_answer(text: String) -> String {
let cleaned = strip_provider_noise(&text);
if cleaned.trim().is_empty() {
RECOVERY_NOISE_FALLBACK.to_string()
} else {
cleaned
}
}
#[cfg(test)]
mod tests {
use super::{
RECOVERY_NOISE_FALLBACK, contains_provider_noise, noise_token_partial_suffix, sanitize_recovery_answer,
strip_provider_noise,
};
#[test]
fn detects_noise_presence() {
assert!(contains_provider_noise("]<]minimax[>[hello"));
assert!(contains_provider_noise("text ]<]minimax[>[ more"));
assert!(!contains_provider_noise("clean text"));
assert!(!contains_provider_noise(""));
}
#[test]
fn strips_single_occurrence() {
assert_eq!(strip_provider_noise("]<]minimax[>[Here is my summary."), "Here is my summary.");
}
#[test]
fn strips_repeated_occurrences() {
assert_eq!(strip_provider_noise("]<]minimax[>[]<]minimax[>[Real answer"), "Real answer");
}
#[test]
fn strips_mid_text_occurrences() {
assert_eq!(strip_provider_noise("Before ]<]minimax[>[ after"), "Before after");
}
#[test]
fn preserves_clean_text() {
assert_eq!(strip_provider_noise("All tests pass."), "All tests pass.");
}
#[test]
fn preserves_empty_text() {
assert_eq!(strip_provider_noise(""), "");
}
#[test]
fn recovery_strips_and_keeps_content() {
assert_eq!(sanitize_recovery_answer("]<]minimax[>[Here is my summary.".to_string()), "Here is my summary.");
}
#[test]
fn recovery_substitutes_fallback_for_noise_only() {
assert_eq!(sanitize_recovery_answer("]<]minimax[>[".to_string()), RECOVERY_NOISE_FALLBACK);
}
#[test]
fn recovery_substitutes_fallback_for_empty() {
assert_eq!(sanitize_recovery_answer(String::new()), RECOVERY_NOISE_FALLBACK);
}
#[test]
fn recovery_substitutes_fallback_for_whitespace() {
assert_eq!(sanitize_recovery_answer(" \n\t ".to_string()), RECOVERY_NOISE_FALLBACK);
}
#[test]
fn recovery_preserves_legitimate_text() {
assert_eq!(sanitize_recovery_answer("All tests pass.".to_string()), "All tests pass.");
}
#[test]
fn recovery_strips_repeated_noise() {
assert_eq!(sanitize_recovery_answer("]<]minimax[>[]<]minimax[>[Real answer".to_string()), "Real answer");
}
#[test]
fn partial_suffix_detects_token_start() {
assert_eq!(noise_token_partial_suffix("Before ]<]mini"), Some(7));
}
#[test]
fn partial_suffix_detects_short_prefix() {
assert_eq!(noise_token_partial_suffix("text ]"), Some(1));
}
#[test]
fn partial_suffix_returns_none_for_clean_text() {
assert_eq!(noise_token_partial_suffix("clean text"), None);
assert_eq!(noise_token_partial_suffix(""), None);
}
#[test]
fn partial_suffix_returns_none_for_complete_token() {
assert_eq!(noise_token_partial_suffix("]<]minimax[>["), None);
}
#[test]
fn partial_suffix_finds_longest_match() {
assert_eq!(noise_token_partial_suffix("Before ]<]minimax"), Some(10));
}
}