use agent_base::ChatMessage;
pub fn image_residue_lines(old_messages: &[ChatMessage]) -> Vec<String> {
let mut out = Vec::new();
for msg in old_messages {
let (content, images) = match msg {
ChatMessage::User {
content, images, ..
}
| ChatMessage::Tool {
content, images, ..
} => (content, images),
_ => continue,
};
if images.is_empty() {
continue;
}
let path =
extract_image_label_path(content).unwrap_or_else(|| "<path not recorded>".to_string());
out.push(format!(
"[an image attached earlier was rotated out of context — \
path={path}; call read_image(path) to re-inspect]"
));
}
out
}
fn extract_image_label_path(content: &str) -> Option<String> {
let start = content.find("path=\"")? + "path=\"".len();
let end = start + content[start..].find('"')?;
Some(content[start..end].to_string())
}
#[cfg(test)]
mod tests {
use super::*;
use agent_base::ImageAttachment;
fn png_msg(content: &str) -> ChatMessage {
ChatMessage::user_with_images(
content,
vec![ImageAttachment::Base64 {
data: "aGVsbG8=".into(),
media_type: Some("image/png".into()),
detail: None,
}],
)
}
#[test]
fn residue_names_the_path_from_the_label() {
let old = vec![png_msg(
"<image name=1 path=\"/s/shot.png\">\nwhat is broken?",
)];
let lines = image_residue_lines(&old);
assert_eq!(lines.len(), 1);
assert!(
lines[0].contains("/s/shot.png"),
"the path must survive: {}",
lines[0]
);
assert!(
lines[0].contains("read_image"),
"the recovery verb must be named: {}",
lines[0]
);
}
#[test]
fn residue_without_a_label_says_so() {
let old = vec![png_msg("what is broken?")];
let lines = image_residue_lines(&old);
assert!(lines[0].contains("<path not recorded>"));
}
#[test]
fn text_only_messages_leave_no_residue() {
let old = vec![ChatMessage::user("plain question")];
assert!(image_residue_lines(&old).is_empty());
}
#[test]
fn residue_never_carries_pixels() {
let old = vec![png_msg("<image name=1 path=\"/s/shot.png\">")];
for line in image_residue_lines(&old) {
assert!(!line.contains("aGVsbG8="));
}
}
#[test]
fn reset_window_carries_image_residue_but_not_pixels() {
use crate::rotation_policy::{HintSlot, TokenBudgetConfig, TokenBudgetCore};
let core = TokenBudgetCore::new(TokenBudgetConfig::default(), Some("You are helpful."));
let old = vec![
ChatMessage::system("You are helpful."),
png_msg("<image name=1 path=\"/s/shot.png\">\nwhat is broken?"),
];
let window = core.build_reset_messages(&old, HintSlot::default(), 0);
let mut saw_residue = false;
for msg in &window {
let (content, images): (&str, &[ImageAttachment]) = match msg {
ChatMessage::System { content, .. } => (content.as_str(), &[]),
ChatMessage::User {
content, images, ..
}
| ChatMessage::Tool {
content, images, ..
} => (content.as_str(), images.as_slice()),
_ => ("", &[]),
};
assert!(
!content.contains("aGVsbG8="),
"pixels must not ride the fresh window"
);
assert!(
images.is_empty(),
"a fresh window carries no picture attachments"
);
if content.contains("rotated out of context") {
saw_residue = true;
assert!(content.contains("/s/shot.png"), "the path must survive");
assert!(
content.contains("read_image"),
"the recovery verb must be named"
);
}
}
assert!(saw_residue, "the fresh window must carry the residue line");
}
}