pub mod config;
pub mod defensive;
pub mod history;
pub mod types;
pub use config::{MinimalSanitizeConfig, SanitizeError};
pub use defensive::sanitize;
pub use history::{drop_blocks, strip_ephemeral, BlockPredicate};
pub use types::{BlockType, DeltaType, EPHEMERAL_MARKER_FIELD};
#[cfg(test)]
mod tests {
use super::config::{MinimalSanitizeConfig, SanitizeError};
use super::defensive::sanitize;
use super::history::{drop_blocks, strip_ephemeral};
use super::types::BlockType;
use serde_json::{json, Value};
fn msgs(v: Value) -> Vec<Value> {
v.as_array().unwrap().clone()
}
#[test]
fn strip_ephemeral_thinking_hard_exempt() {
let input = msgs(json!([{
"role": "assistant",
"content": [
{ "type": "thinking", "thinking": "deep", "acosmi_ephemeral": true },
{ "type": "redacted_thinking", "data": "x", "acosmi_ephemeral": true },
{ "type": "text", "text": "ephemeral note", "acosmi_ephemeral": true },
{ "type": "text", "text": "kept" }
]
}]));
let out = strip_ephemeral(input);
let content = out[0]["content"].as_array().unwrap();
let types: Vec<&str> = content
.iter()
.map(|b| b["type"].as_str().unwrap())
.collect();
assert_eq!(types, vec!["thinking", "redacted_thinking", "text"]);
assert_eq!(content[2]["text"], "kept");
}
#[test]
fn drop_blocks_links_tool_result_and_drops_empty_message() {
let input = msgs(json!([
{ "role": "user", "content": [{ "type": "text", "text": "hi" }] },
{
"role": "assistant",
"content": [{ "type": "tool_use", "id": "call_1", "name": "f", "acosmi_ephemeral": true }]
},
{
"role": "user",
"content": [{ "type": "tool_result", "tool_use_id": "call_1", "content": "r" }]
}
]));
let out = strip_ephemeral(input);
assert_eq!(out.len(), 1);
assert_eq!(out[0]["role"], "user");
assert_eq!(out[0]["content"][0]["text"], "hi");
}
#[test]
fn drop_blocks_keeps_partial_message_and_unrelated_tool_result() {
let input = msgs(json!([
{
"role": "assistant",
"content": [
{ "type": "tool_use", "id": "call_1", "name": "f", "acosmi_ephemeral": true },
{ "type": "text", "text": "narration" }
]
},
{
"role": "user",
"content": [
{ "type": "tool_result", "tool_use_id": "call_1", "content": "r1" },
{ "type": "tool_result", "tool_use_id": "call_2", "content": "r2" }
]
}
]));
let out = strip_ephemeral(input);
assert_eq!(out.len(), 2);
let a = out[0]["content"].as_array().unwrap();
assert_eq!(a.len(), 1);
assert_eq!(a[0]["type"], "text");
let u = out[1]["content"].as_array().unwrap();
assert_eq!(u.len(), 1);
assert_eq!(u[0]["tool_use_id"], "call_2");
}
#[test]
fn drop_blocks_predicate_collects_across_message_order() {
let input = msgs(json!([
{ "role": "user", "content": [{ "type": "tool_result", "tool_use_id": "x", "content": "r" }] },
{ "role": "assistant", "content": [{ "type": "tool_use", "id": "x", "name": "f" }] }
]));
let out = drop_blocks(input, &|b| {
b.get("type").and_then(Value::as_str) == Some("tool_use")
});
assert!(out.is_empty());
}
#[test]
fn media_size_over_limit_returns_size_error() {
let input = msgs(json!([{
"role": "user",
"content": [{ "type": "image", "source": { "type": "base64", "data": "AAAA" } }]
}]));
let cfg = MinimalSanitizeConfig {
max_image_bytes: Some(2),
..Default::default()
};
let err = sanitize(input, &cfg).unwrap_err();
match err {
SanitizeError::Size {
block_type,
actual,
limit,
} => {
assert_eq!(block_type, BlockType::Image);
assert_eq!(actual, 3);
assert_eq!(limit, 2);
}
other => panic!("expected Size, got {other:?}"),
}
}
#[test]
fn media_size_data_url_prefix_stripped() {
let input = msgs(json!([{
"role": "user",
"content": [{
"type": "image",
"source": { "type": "base64", "data": "data:image/png;base64,AAAA" }
}]
}]));
let cfg_ok = MinimalSanitizeConfig {
max_image_bytes: Some(3),
..Default::default()
};
assert!(sanitize(input.clone(), &cfg_ok).is_ok());
let cfg_bad = MinimalSanitizeConfig {
max_image_bytes: Some(2),
..Default::default()
};
match sanitize(input, &cfg_bad).unwrap_err() {
SanitizeError::Size { actual, .. } => assert_eq!(actual, 3),
other => panic!("expected Size, got {other:?}"),
}
}
#[test]
fn base64_decoded_len_padding_matches_go() {
let input = msgs(json!([{
"role": "user",
"content": [{ "type": "image", "source": { "type": "base64", "data": "AAA=" } }]
}]));
let cfg = MinimalSanitizeConfig {
max_image_bytes: Some(1),
..Default::default()
};
match sanitize(input, &cfg).unwrap_err() {
SanitizeError::Size { actual, .. } => assert_eq!(actual, 2),
other => panic!("expected Size, got {other:?}"),
}
}
#[test]
fn url_media_skipped() {
let input = msgs(json!([{
"role": "user",
"content": [{ "type": "image", "source": { "type": "url", "url": "http://x/y.png" } }]
}]));
let cfg = MinimalSanitizeConfig {
max_image_bytes: Some(1),
..Default::default()
};
assert!(sanitize(input, &cfg).is_ok());
}
#[test]
fn history_too_deep() {
let input = msgs(json!([
{ "role": "user", "content": "a" },
{ "role": "assistant", "content": "b" },
{ "role": "user", "content": "c" }
]));
let cfg = MinimalSanitizeConfig {
max_messages_turns: Some(2),
..Default::default()
};
assert_eq!(
sanitize(input, &cfg).unwrap_err(),
SanitizeError::HistoryTooDeep
);
}
#[test]
fn deny_list_strips_block() {
let input = msgs(json!([{
"role": "assistant",
"content": [
{ "type": "thinking", "thinking": "x" },
{ "type": "text", "text": "y" }
]
}]));
let cfg = MinimalSanitizeConfig {
permanent_deny_blocks: vec![BlockType::Thinking],
..Default::default()
};
let out = sanitize(input, &cfg).unwrap();
let content = out[0]["content"].as_array().unwrap();
assert_eq!(content.len(), 1);
assert_eq!(content[0]["type"], "text");
}
#[test]
fn malformed_elements_pass_through() {
let input = msgs(json!([
"not-an-object",
42,
{ "role": "user", "content": "plain text not array" },
{ "role": "system" }
]));
let cfg = MinimalSanitizeConfig {
permanent_deny_blocks: vec![BlockType::Image],
max_image_bytes: Some(1),
..Default::default()
};
let out = sanitize(input.clone(), &cfg).unwrap();
assert_eq!(out, input); }
#[test]
fn config_is_empty() {
assert!(MinimalSanitizeConfig::default().is_empty());
assert!(!MinimalSanitizeConfig {
max_image_bytes: Some(1),
..Default::default()
}
.is_empty());
}
}