use std::path::{Path, PathBuf};
use supercode::reduce::rehydrate::{expand_reduction, sidecar_search};
use supercode::reduce::{
self, invert, normalize, project_messages, reduction_id, stub, verify_log, ReductionKind,
ReductionLog, ReductionPolicy, REDUCTION_SENTINEL,
};
use supercode::session::Session;
use supercode::{ChatMessage, FunctionCall, Role, ToolCall};
fn fixtures_dir() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/terminal")
}
fn load_raw(name: &str) -> String {
std::fs::read_to_string(fixtures_dir().join(format!("{name}.raw")))
.unwrap_or_else(|e| panic!("reading {name}.raw: {e}"))
}
fn load_golden(name: &str) -> String {
std::fs::read_to_string(fixtures_dir().join(format!("{name}.golden.txt")))
.unwrap_or_else(|e| panic!("reading {name}.golden.txt: {e}"))
}
fn pct_reduction(original: usize, normalized: usize) -> f64 {
if original == 0 {
0.0
} else {
100.0 * (1.0 - (normalized as f64 / original as f64))
}
}
#[test]
fn cargo_and_npm_fixtures_meet_the_80_percent_reduction_floor() {
for name in ["cargo_build", "npm_install"] {
let raw = load_raw(name);
let golden = load_golden(name);
let normalized = normalize::normalize(&raw);
assert_eq!(
normalized, golden,
"{name}: normalize() must match the checked-in golden"
);
let pct = pct_reduction(raw.len(), normalized.len());
assert!(
pct >= 80.0,
"{name}: reduction {pct:.1}% must be >= 80% (orig={}, normalized={})",
raw.len(),
normalized.len()
);
println!(
"{name}: {} B -> {} B ({pct:.1}% reduction)",
raw.len(),
normalized.len()
);
}
}
#[test]
fn pip_and_docker_fixtures_normalize_to_golden_final_rendered_content() {
for name in ["pip_download", "docker_pull"] {
let raw = load_raw(name);
let golden = load_golden(name);
let normalized = normalize::normalize(&raw);
assert_eq!(
normalized, golden,
"{name}: normalize() must match the checked-in golden"
);
let pct = pct_reduction(raw.len(), normalized.len());
assert!(
pct > 0.0,
"{name}: normalization must report a nonzero savings figure, got {pct:.1}%"
);
println!(
"{name}: {} B -> {} B ({pct:.1}% reduction)",
raw.len(),
normalized.len()
);
}
}
fn assistant_calling(call_id: &str, tool_name: &str) -> ChatMessage {
ChatMessage {
role: Role::Assistant,
content: None,
content_parts: None,
tool_calls: Some(vec![ToolCall {
id: call_id.to_string(),
kind: "function".to_string(),
function: FunctionCall {
name: tool_name.to_string(),
arguments: r#"{"command":"cargo build"}"#.to_string(),
},
}]),
tool_call_id: None,
name: None,
metadata: Default::default(),
}
}
fn bash_result_messages(content: &str) -> Vec<ChatMessage> {
vec![
ChatMessage::user("please build the project"),
assistant_calling("call-1", "bash"),
ChatMessage::tool_result("call-1", "bash", content.to_string()),
]
}
fn bash_idx() -> usize {
2
}
fn session_of(msgs: Vec<ChatMessage>) -> Session {
let mut session = Session::from_claude_code_str("").unwrap();
session.messages = msgs;
session
}
fn find_output_normalized(log: &ReductionLog) -> &reduce::Reduction {
log.reductions
.iter()
.find(|r| matches!(r.kind, ReductionKind::OutputNormalized { .. }))
.expect("expected an OutputNormalized reduction in the log")
}
#[test]
fn project_messages_creates_output_normalized_reduction_for_a_noisy_bash_result() {
let raw = load_raw("cargo_build");
let msgs = bash_result_messages(&raw);
let (view, log) =
project_messages(&msgs, &ReductionPolicy::default(), &ReductionLog::default());
assert_eq!(log.reductions.len(), 1, "{:?}", log.reductions);
let r = find_output_normalized(&log);
let (original_bytes, normalized_bytes) = match r.kind {
ReductionKind::OutputNormalized {
original_bytes,
normalized_bytes,
} => (original_bytes, normalized_bytes),
_ => unreachable!(),
};
assert_eq!(original_bytes, raw.len());
assert_eq!(normalized_bytes, normalize::normalize(&raw).len());
assert!(original_bytes - normalized_bytes >= normalize::DEFAULT_MIN_SAVINGS);
let expected_normalized = normalize::normalize(&raw);
let view_content = view[bash_idx()].content.as_deref().unwrap();
assert!(view_content.starts_with(&expected_normalized));
assert!(view_content.ends_with(&r.placeholder));
assert!(view_content.contains(REDUCTION_SENTINEL));
assert_eq!(reduction_id(&view[bash_idx()]), Some(r.id.as_str()));
let (kind, id, summary) = stub::parse(&r.placeholder).expect("placeholder must parse");
assert_eq!(kind, stub::Kind::OutputNormalized);
assert_eq!(kind.as_str(), "output-normalized");
assert_eq!(id, r.id);
assert!(summary.contains(&format!("{}", original_bytes)) || summary.contains("collapsed"));
}
fn load_codex_session() -> Session {
Session::from_codex(
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/codex_session.jsonl"),
)
.unwrap()
}
fn bashify_tool_output(session: &mut Session, ordinal: usize, content: &str) -> usize {
let idx = session
.messages
.iter()
.enumerate()
.filter(|(_, m)| m.role == Role::Tool)
.map(|(i, _)| i)
.nth(ordinal)
.expect("session must have at least ordinal+1 tool messages");
session.messages[idx].name = Some("bash".to_string());
session.messages[idx].content = Some(content.to_string());
idx
}
#[test]
fn invert_restores_raw_bytes_byte_exact_after_output_normalization() {
let raw = load_raw("cargo_build");
let mut session = load_codex_session();
let idx = bashify_tool_output(&mut session, 0, &raw);
let (view, log) = reduce::project(
&session,
&ReductionPolicy::default(),
&ReductionLog::default(),
);
assert!(log.reductions.iter().any(
|r| r.ptr.addr.index == idx && matches!(r.kind, ReductionKind::OutputNormalized { .. })
));
verify_log(&log, &session).expect("log must verify cleanly against the sidecar");
let restored = invert(&view, &log, &session).expect("invert must succeed");
assert_eq!(
restored[idx].content.as_deref(),
Some(raw.as_str()),
"invert must restore the exact raw (pre-normalization) bytes"
);
assert_eq!(
reduction_id(&restored[idx]),
None,
"sc.reduction must be stripped after invert"
);
}
#[test]
fn reduction_is_prefix_stable_across_reprojection() {
let raw = load_raw("npm_install");
let mut session = load_codex_session();
let idx = bashify_tool_output(&mut session, 0, &raw);
let (view1, log1) = reduce::project(
&session,
&ReductionPolicy::default(),
&ReductionLog::default(),
);
let (view2, log2) = reduce::project(&session, &ReductionPolicy::default(), &log1);
assert_eq!(log1.reductions.len(), 1);
assert_eq!(log1.reductions, log2.reductions);
assert_eq!(view1[idx].content, view2[idx].content);
assert_eq!(reduction_id(&view1[idx]), reduction_id(&view2[idx]));
}
#[test]
fn unsupported_escape_sequence_survives_through_the_full_pipeline() {
let mut raw = load_raw("cargo_build");
raw.push_str("before-dsr\x1b[6nafter-dsr\n");
let msgs = bash_result_messages(&raw);
let (view, log) =
project_messages(&msgs, &ReductionPolicy::default(), &ReductionLog::default());
let r = find_output_normalized(&log);
let expected_normalized = normalize::normalize(&raw);
let view_content = view[bash_idx()].content.as_deref().unwrap();
assert!(view_content.starts_with(&expected_normalized));
assert!(
expected_normalized.contains("before-dsr\x1b[6nafter-dsr"),
"the unsupported DSR sequence must survive verbatim in the normalized text: {expected_normalized:?}"
);
assert_eq!(
normalize::normalize(&expected_normalized),
expected_normalized
);
let mut session = load_codex_session();
let idx = bashify_tool_output(&mut session, 0, &raw);
let (view2, log2) = reduce::project(
&session,
&ReductionPolicy::default(),
&ReductionLog::default(),
);
let restored = invert(&view2, &log2, &session).unwrap();
assert_eq!(restored[idx].content.as_deref(), Some(raw.as_str()));
assert_eq!(reduction_id(&view[bash_idx()]), Some(r.id.as_str()));
}
#[test]
fn oversized_normalized_output_falls_through_to_bounded_a7_truncation() {
let mut raw = load_raw("cargo_build");
raw.push_str(&"y".repeat(20_000));
let normalized_bytes = normalize::normalize(&raw).len();
let policy = ReductionPolicy::default();
assert!(
normalized_bytes > policy.tool_output_trigger_bytes,
"test fixture must exercise the still-oversized-after-normalization case; got \
{normalized_bytes}B normalized vs a {}B trigger",
policy.tool_output_trigger_bytes
);
let mut msgs = vec![
ChatMessage::user("please build the project"),
assistant_calling("call-1", "bash"),
ChatMessage::tool_result("call-1", "bash", raw.clone()),
];
for i in 0..3 {
let call_id = format!("call-{}", i + 2);
msgs.push(assistant_calling(&call_id, "noop"));
msgs.push(ChatMessage::tool_result(call_id, "noop", "ok"));
}
let noisy_idx = 2;
let (view, log) = project_messages(&msgs, &policy, &ReductionLog::default());
let claiming: Vec<&reduce::Reduction> = log
.reductions
.iter()
.filter(|r| r.ptr.addr.index == noisy_idx)
.collect();
assert_eq!(
claiming.len(),
1,
"exactly one reduction must claim the noisy message, never two: {:?}",
log.reductions
);
assert!(
matches!(claiming[0].kind, ReductionKind::ToolOutputTruncated { .. }),
"a normalized rendering still over the trigger must defer to A7's bounded truncation, \
not be claimed by OutputNormalized (the P7 regression this fix closes): {:?}",
claiming[0].kind
);
let view_content = view[noisy_idx].content.as_deref().unwrap();
assert!(
view_content.len() <= policy.tool_output_trigger_bytes,
"P7 safety net violated: projected wire bytes ({}) exceed tool_output_trigger_bytes \
({}) — a normalized-but-still-huge terminal output must never ride the wire uncapped",
view_content.len(),
policy.tool_output_trigger_bytes
);
assert!(
view_content.len() < normalized_bytes,
"wire content ({}) must be strictly smaller than the (still oversized) normalized \
rendering ({normalized_bytes}) — proof A7 actually truncated raw bytes, not the \
normalized text",
view_content.len()
);
assert!(view_content.starts_with(&raw[..policy.tool_output_keep_bytes.min(raw.len())]));
}
#[test]
fn duplicate_ansi_noisy_output_is_deduped_not_normalized() {
let raw = load_raw("cargo_build");
let msgs = vec![
ChatMessage::user("please build the project twice"),
assistant_calling("call-1", "bash"),
ChatMessage::tool_result("call-1", "bash", raw.clone()),
assistant_calling("call-2", "bash"),
ChatMessage::tool_result("call-2", "bash", raw.clone()),
];
let first_idx = 2;
let second_idx = 4;
let policy = ReductionPolicy {
protect_last_n_tool_results: 0,
..ReductionPolicy::default()
};
let (view, log) = project_messages(&msgs, &policy, &ReductionLog::default());
let second_claims: Vec<&reduce::Reduction> = log
.reductions
.iter()
.filter(|r| r.ptr.addr.index == second_idx)
.collect();
assert_eq!(
second_claims.len(),
1,
"exactly one reduction must claim the later (duplicate) occurrence: {:?}",
log.reductions
);
assert!(
matches!(second_claims[0].kind, ReductionKind::DuplicateOutput { .. }),
"TR-2 must win precedence over OutputNormalized for a byte-identical later occurrence: \
{:?}",
second_claims[0].kind
);
let first_claim = log
.reductions
.iter()
.find(|r| r.ptr.addr.index == first_idx)
.expect("canonical occurrence must also have a reduction");
assert!(
matches!(first_claim.kind, ReductionKind::OutputNormalized { .. }),
"canonical occurrence must be normalized independently: {:?}",
first_claim.kind
);
let session = session_of(msgs.clone());
let inverted = invert(&view, &log, &session).expect("invert must succeed");
assert_eq!(inverted[first_idx].content.as_deref(), Some(raw.as_str()));
assert_eq!(inverted[second_idx].content.as_deref(), Some(raw.as_str()));
}
#[test]
fn plain_bash_output_yields_no_reduction() {
let raw = load_raw("plain_output");
let msgs = bash_result_messages(&raw);
let (view, log) =
project_messages(&msgs, &ReductionPolicy::default(), &ReductionLog::default());
assert!(
log.reductions.is_empty(),
"plain (no ANSI/CR) output must yield NO reduction: {:?}",
log.reductions
);
assert_eq!(view[bash_idx()].content.as_deref(), Some(raw.as_str()));
assert_eq!(reduction_id(&view[bash_idx()]), None);
}
#[test]
fn expand_reduction_returns_the_exact_raw_bytes() {
let raw = load_raw("cargo_build");
let msgs = bash_result_messages(&raw);
let (_view, log) =
project_messages(&msgs, &ReductionPolicy::default(), &ReductionLog::default());
let r = find_output_normalized(&log);
let outcome = expand_reduction(&log, &msgs, None, &r.id, None).expect("expand must succeed");
assert_eq!(
outcome.content, raw,
"expand_reduction must return the exact raw bytes"
);
assert_eq!(outcome.total_bytes, raw.len());
}
#[test]
fn sidecar_search_finds_raw_ansi_content_hidden_from_the_view() {
let raw = load_raw("cargo_build");
let msgs = bash_result_messages(&raw);
let (view, log) =
project_messages(&msgs, &ReductionPolicy::default(), &ReductionLog::default());
let r = find_output_normalized(&log);
let hits =
sidecar_search(&log, &msgs, None, "\u{1b}[1m\u{1b}[32m").expect("search must succeed");
assert!(
!hits.matches.is_empty(),
"expected matches in the raw ANSI bytes"
);
assert!(hits.matches.iter().all(|m| m.reduction_id == r.id));
assert!(hits.matches.iter().all(|m| m.kind == "output-normalized"));
let view_content = view[bash_idx()].content.as_deref().unwrap();
assert!(!view_content.contains("\u{1b}[1m\u{1b}[32m"));
}