use std::collections::HashSet;
use supercode::reduce::rehydrate::expand_reduction;
use supercode::reduce::{
invert, is_tool_error, mark_tool_error, project_messages, reduction_id, stub, verify_log,
ReductionKind, ReductionLog, ReductionPolicy, REDUCTION_SENTINEL,
};
use supercode::session::Session;
use supercode::{ChatMessage, FunctionCall, Role, ToolCall};
fn session_of(msgs: Vec<ChatMessage>) -> Session {
let mut session = Session::from_claude_code_str("").unwrap();
session.messages = msgs;
session
}
fn filler(len: usize) -> String {
(0..len).map(|i| (b'a' + (i % 26) as u8) as char).collect()
}
fn bash_call(id: &str, command: &str) -> ChatMessage {
ChatMessage {
role: Role::Assistant,
content: None,
content_parts: None,
tool_calls: Some(vec![ToolCall {
id: id.to_string(),
kind: "function".to_string(),
function: FunctionCall {
name: "bash".to_string(),
arguments: serde_json::json!({ "command": command }).to_string(),
},
}]),
tool_call_id: None,
name: None,
metadata: Default::default(),
}
}
fn bash_result(id: &str, content: impl Into<String>) -> ChatMessage {
ChatMessage::tool_result(id, "bash", content)
}
fn no_protection_policy() -> ReductionPolicy {
ReductionPolicy {
protect_last_n_tool_results: 0,
supersede_protect_last_n: 0,
..ReductionPolicy::default()
}
}
fn superseded_indices(log: &ReductionLog) -> HashSet<usize> {
log.reductions
.iter()
.filter_map(|r| match r.kind {
ReductionKind::Superseded { .. } => Some(r.ptr.addr.index),
_ => None,
})
.collect()
}
#[test]
fn dev01_four_runs_same_command_first_three_superseded_naming_the_fourth() {
let bodies: Vec<String> = vec![
format!("FAIL(assertion 1): {}", filler(500)),
format!("FAIL(assertion 2): {}", filler(500)),
format!("FAIL(timeout): {}", filler(500)),
format!("PASS: {}", filler(500)),
];
let mut msgs = vec![ChatMessage::user("run the test suite")];
let mut result_idx = Vec::new();
for (i, body) in bodies.iter().enumerate() {
let id = format!("c{i}");
msgs.push(bash_call(&id, "cargo test"));
result_idx.push(msgs.len());
msgs.push(bash_result(&id, body.clone()));
}
let (view, log) = project_messages(&msgs, &no_protection_policy(), &ReductionLog::default());
let superseded: Vec<_> = log
.reductions
.iter()
.filter(|r| matches!(r.kind, ReductionKind::Superseded { .. }))
.collect();
assert_eq!(
superseded.len(),
3,
"the first three runs must all be superseded: {:?}",
log.reductions
);
let newest_idx = result_idx[3];
for (i, &idx) in result_idx[..3].iter().enumerate() {
let r = superseded
.iter()
.find(|r| r.ptr.addr.index == idx)
.unwrap_or_else(|| panic!("run {i} (msg #{idx}) must be superseded: {log:?}"));
match r.kind {
ReductionKind::Superseded { by, original_bytes } => {
assert_eq!(
by.index, newest_idx,
"every superseded run must name the 4th (newest), not its immediate successor"
);
assert_eq!(by.role, Role::Tool);
assert_eq!(original_bytes, bodies[i].len());
}
_ => unreachable!(),
}
let content = view[idx].content.as_deref().unwrap();
assert!(
content.starts_with("[sc-reduced superseded"),
"message {idx}: {content}"
);
let (kind, rid, summary) = stub::parse(content).expect("stub must parse");
assert_eq!(kind, stub::Kind::Superseded);
assert_eq!(rid, r.id);
assert!(
summary.contains(&format!("msg #{newest_idx}")),
"stub must honestly name the successor: {summary}"
);
assert!(
summary.contains(&format!("expand_reduction(\"{}\")", r.id)),
"stub must tell the model how to restore it: {summary}"
);
}
assert_eq!(
view[newest_idx].content.as_deref(),
Some(bodies[3].as_str())
);
assert!(reduction_id(&view[newest_idx]).is_none());
let sidecar = session_of(msgs.clone());
verify_log(&log, &sidecar).expect("verify_log must pass clean");
let inverted = invert(&view, &log, &sidecar).expect("invert must pass clean");
for (&idx, body) in result_idx.iter().zip(bodies.iter()) {
assert_eq!(
inverted[idx].content.as_deref(),
Some(body.as_str()),
"run at msg #{idx} must invert byte-exact"
);
}
let first_id = superseded
.iter()
.find(|r| r.ptr.addr.index == result_idx[0])
.unwrap()
.id
.clone();
let expanded = expand_reduction(&log, &msgs, None, &first_id, None).unwrap();
assert_eq!(expanded.content, bodies[0]);
}
#[test]
fn dev02_different_args_never_superseded_git_diff_vs_stat() {
let body_plain = format!("diff-plain-{}", filler(300));
let body_stat = format!("diff-stat-{}", filler(300));
let msgs = vec![
ChatMessage::user("diff please"),
bash_call("c0", "git diff"),
bash_result("c0", body_plain.clone()),
bash_call("c1", "git diff --stat"),
bash_result("c1", body_stat.clone()),
];
let (view, log) = project_messages(&msgs, &no_protection_policy(), &ReductionLog::default());
assert!(
superseded_indices(&log).is_empty(),
"different arguments must never be superseded: {:?}",
log.reductions
);
assert_eq!(view[2].content.as_deref(), Some(body_plain.as_str()));
assert_eq!(view[4].content.as_deref(), Some(body_stat.as_str()));
}
#[test]
fn dev02_different_args_never_superseded_ls_a_vs_b() {
let body_a = format!("listing-a-{}", filler(300));
let body_b = format!("listing-b-{}", filler(300));
let msgs = vec![
ChatMessage::user("list both dirs"),
bash_call("c0", "ls a/"),
bash_result("c0", body_a.clone()),
bash_call("c1", "ls b/"),
bash_result("c1", body_b.clone()),
];
let (view, log) = project_messages(&msgs, &no_protection_policy(), &ReductionLog::default());
assert!(
superseded_indices(&log).is_empty(),
"different arguments must never be superseded: {:?}",
log.reductions
);
assert_eq!(view[2].content.as_deref(), Some(body_a.as_str()));
assert_eq!(view[4].content.as_deref(), Some(body_b.as_str()));
}
#[test]
fn dev02_v1_never_guesses_flag_reordered_commands_are_equivalent() {
let body_la = format!("la-{}", filler(300));
let body_al = format!("al-{}", filler(300));
let msgs = vec![
ChatMessage::user("list with flags"),
bash_call("c0", "ls -la"),
bash_result("c0", body_la.clone()),
bash_call("c1", "ls -al"),
bash_result("c1", body_al.clone()),
];
let (view, log) = project_messages(&msgs, &no_protection_policy(), &ReductionLog::default());
assert!(
superseded_indices(&log).is_empty(),
"v1 must never treat `ls -la`/`ls -al` as the same key: {:?}",
log.reductions
);
assert_eq!(view[2].content.as_deref(), Some(body_la.as_str()));
assert_eq!(view[4].content.as_deref(), Some(body_al.as_str()));
}
#[test]
fn dev03_protected_recency_zone_respects_the_boundary() {
let bodies: Vec<String> = (0..4).map(|i| format!("RUN-{i}-{}", filler(400))).collect();
let mut msgs = vec![ChatMessage::user("run it repeatedly")];
let mut result_idx = Vec::new();
for (i, body) in bodies.iter().enumerate() {
let id = format!("c{i}");
msgs.push(bash_call(&id, "cargo test"));
result_idx.push(msgs.len());
msgs.push(bash_result(&id, body.clone()));
}
let policy = ReductionPolicy {
protect_last_n_tool_results: 0,
supersede_protect_last_n: 2,
..ReductionPolicy::default()
};
let (view, log) = project_messages(&msgs, &policy, &ReductionLog::default());
let got = superseded_indices(&log);
let want: HashSet<usize> = [result_idx[0], result_idx[1]].into_iter().collect();
assert_eq!(
got, want,
"only occurrences outside the protected zone may be superseded"
);
assert_eq!(
view[result_idx[2]].content.as_deref(),
Some(bodies[2].as_str())
);
assert!(reduction_id(&view[result_idx[2]]).is_none());
assert_eq!(
view[result_idx[3]].content.as_deref(),
Some(bodies[3].as_str())
);
}
fn write_call(id: &str, path: &str, content: &str) -> ChatMessage {
ChatMessage {
role: Role::Assistant,
content: None,
content_parts: None,
tool_calls: Some(vec![ToolCall {
id: id.to_string(),
kind: "function".to_string(),
function: FunctionCall {
name: "write_file".to_string(),
arguments: serde_json::json!({ "path": path, "content": content }).to_string(),
},
}]),
tool_call_id: None,
name: None,
metadata: Default::default(),
}
}
fn write_err_result(id: &str) -> ChatMessage {
let mut m = ChatMessage::tool_result(id, "write_file", "Error: disk full");
mark_tool_error(&mut m);
m
}
fn args_of(msgs: &[ChatMessage], msg_index: usize) -> String {
msgs[msg_index].tool_calls()[0].function.arguments.clone()
}
fn parsed_args(msgs: &[ChatMessage], msg_index: usize) -> serde_json::Value {
serde_json::from_str(&args_of(msgs, msg_index)).unwrap()
}
#[test]
fn dev04_errored_call_large_input_pruned_only_after_n_turns_error_stays_visible_and_inverts() {
let big = filler(20_000);
let policy = ReductionPolicy::default();
let mut msgs = vec![write_call("w1", "out.txt", &big), write_err_result("w1")];
assert!(is_tool_error(&msgs[1]));
let (view0, log0) = project_messages(&msgs, &policy, &ReductionLog::default());
assert!(
log0.reductions.is_empty(),
"an errored call's oversized input must not be pruned before N turns elapse: {:?}",
log0.reductions
);
assert_eq!(args_of(&view0, 0), args_of(&msgs, 0));
for i in 0..policy.errored_input_prune_after_turns {
msgs.push(ChatMessage::assistant(format!("turn {i}")));
}
let (view, log) = project_messages(&msgs, &policy, &ReductionLog::default());
assert_eq!(log.reductions.len(), 1, "{:?}", log.reductions);
let r = &log.reductions[0];
match &r.kind {
ReductionKind::ToolInputElided {
call_id,
field,
original_bytes,
..
} => {
assert_eq!(call_id, "w1");
assert_eq!(field, "content");
assert_eq!(*original_bytes, big.len());
}
other => panic!("expected ToolInputElided, got {other:?}"),
}
let reduced = parsed_args(&view, 0);
let content_val = reduced.get("content").unwrap().as_str().unwrap();
assert!(content_val.contains(REDUCTION_SENTINEL), "{content_val}");
assert!(content_val.contains("tool-input"), "{content_val}");
assert!(
content_val.contains("errored call"),
"the stub should be honest that this is the errored-call case: {content_val}"
);
assert_eq!(view[1].content.as_deref(), Some("Error: disk full"));
assert!(reduction_id(&view[1]).is_none());
let sidecar = session_of(msgs.clone());
verify_log(&log, &sidecar).expect("verify_log must pass clean");
let inverted = invert(&view, &log, &sidecar).unwrap();
assert_eq!(
args_of(&inverted, 0),
args_of(&msgs, 0),
"invert must restore the original tool_call arguments byte-for-byte"
);
}
#[test]
fn dev04_successful_call_never_taken_by_errored_input_pruning() {
let big = filler(20_000);
let msgs = vec![
write_call("w1", "out.txt", &big),
ChatMessage::tool_result("w1", "write_file", format!("Wrote {} bytes", big.len())),
];
let (_, log) = project_messages(&msgs, &ReductionPolicy::default(), &ReductionLog::default());
assert_eq!(log.reductions.len(), 1, "{:?}", log.reductions);
match &log.reductions[0].kind {
ReductionKind::ToolInputElided { .. } => {}
other => panic!("expected ToolInputElided (TR-10's success path), got {other:?}"),
}
}
#[test]
fn dev05_reprojecting_the_same_transcript_twice_yields_identical_logs() {
let bodies: Vec<String> = vec![
format!("FAIL-1-{}", filler(400)),
format!("FAIL-2-{}", filler(400)),
format!("PASS-{}", filler(400)),
];
let mut msgs = vec![ChatMessage::user("run it")];
for (i, body) in bodies.iter().enumerate() {
let id = format!("c{i}");
msgs.push(bash_call(&id, "cargo test"));
msgs.push(bash_result(&id, body.clone()));
}
let policy = no_protection_policy();
let (_, log1) = project_messages(&msgs, &policy, &ReductionLog::default());
let (_, log2) = project_messages(&msgs, &policy, &ReductionLog::default());
assert_eq!(
log1, log2,
"reducing the same transcript twice from scratch must yield identical logs"
);
assert!(
!log1.reductions.is_empty(),
"sanity: this fixture must actually mint Superseded reductions"
);
let (view3, log3) = project_messages(&msgs, &policy, &log1);
assert_eq!(log3, log1);
let sidecar = session_of(msgs.clone());
verify_log(&log3, &sidecar).expect("verify_log must pass clean");
let inverted = invert(&view3, &log3, &sidecar).expect("invert must pass clean");
for (a, b) in inverted.iter().zip(msgs.iter()) {
assert_eq!(a.content, b.content);
}
}
#[test]
fn tiny_repeated_results_below_the_floor_are_never_superseded() {
let tiny = "ok"; let msgs = vec![
ChatMessage::user("run it"),
bash_call("c0", "cargo test"),
bash_result("c0", tiny),
bash_call("c1", "cargo test"),
bash_result("c1", tiny),
];
let (_, log) = project_messages(&msgs, &no_protection_policy(), &ReductionLog::default());
assert!(
superseded_indices(&log).is_empty(),
"content below the savings floor must never be superseded: {:?}",
log.reductions
);
}
#[test]
fn supersede_enabled_false_disables_minting_entirely() {
let bodies: Vec<String> = vec![
format!("FAIL-{}", filler(400)),
format!("PASS-{}", filler(400)),
];
let mut msgs = vec![ChatMessage::user("run it")];
for (i, body) in bodies.iter().enumerate() {
let id = format!("c{i}");
msgs.push(bash_call(&id, "cargo test"));
msgs.push(bash_result(&id, body.clone()));
}
let policy = ReductionPolicy {
supersede_enabled: false,
..no_protection_policy()
};
let (_, log) = project_messages(&msgs, &policy, &ReductionLog::default());
assert!(
superseded_indices(&log).is_empty(),
"supersede_enabled = false must mint no Superseded reductions: {:?}",
log.reductions
);
}
#[test]
fn non_command_tool_supersedes_on_the_whole_trimmed_arguments() {
let call = |id: &str, path: &str| ChatMessage {
role: Role::Assistant,
content: None,
content_parts: None,
tool_calls: Some(vec![ToolCall {
id: id.to_string(),
kind: "function".to_string(),
function: FunctionCall {
name: "list_dir".to_string(),
arguments: serde_json::json!({ "path": path }).to_string(),
},
}]),
tool_call_id: None,
name: None,
metadata: Default::default(),
};
let old_listing = format!("old-listing-{}", filler(400));
let new_listing = format!("new-listing-{}", filler(400));
let msgs = vec![
ChatMessage::user("list the dir twice"),
call("c0", "/repo"),
ChatMessage::tool_result("c0", "list_dir", old_listing.clone()),
call("c1", "/repo"),
ChatMessage::tool_result("c1", "list_dir", new_listing.clone()),
];
let (view, log) = project_messages(&msgs, &no_protection_policy(), &ReductionLog::default());
assert_eq!(
superseded_indices(&log),
[2usize].into_iter().collect::<HashSet<_>>(),
"the older same-path listing must be superseded by the newer one: {:?}",
log.reductions
);
assert_eq!(view[4].content.as_deref(), Some(new_listing.as_str()));
let other_listing = format!("other-listing-{}", filler(400));
let msgs2 = vec![
ChatMessage::user("list a different dir"),
call("c0", "/repo"),
ChatMessage::tool_result("c0", "list_dir", old_listing.clone()),
call("c1", "/elsewhere"),
ChatMessage::tool_result("c1", "list_dir", other_listing.clone()),
];
let (view2, log2) = project_messages(&msgs2, &no_protection_policy(), &ReductionLog::default());
assert!(
superseded_indices(&log2).is_empty(),
"a different path must never be superseded: {:?}",
log2.reductions
);
assert_eq!(view2[2].content.as_deref(), Some(old_listing.as_str()));
assert_eq!(view2[4].content.as_deref(), Some(other_listing.as_str()));
}
#[test]
fn quote_aware_collapse_never_falsely_supersedes_differently_spaced_quoted_literals() {
let body_two_spaces = format!("two-spaces-{}", filler(300));
let body_one_space = format!("one-space-{}", filler(300));
let msgs = vec![
ChatMessage::user("echo twice, differently spaced inside the quotes"),
bash_call("c0", r#"echo "a b""#),
bash_result("c0", body_two_spaces.clone()),
bash_call("c1", r#"echo "a b""#),
bash_result("c1", body_one_space.clone()),
];
let (view, log) = project_messages(&msgs, &no_protection_policy(), &ReductionLog::default());
assert!(
superseded_indices(&log).is_empty(),
"differently-spaced quoted literals must never be superseded: {:?}",
log.reductions
);
assert_eq!(view[2].content.as_deref(), Some(body_two_spaces.as_str()));
assert_eq!(view[4].content.as_deref(), Some(body_one_space.as_str()));
}
#[test]
fn quote_aware_collapse_single_quoted_never_falsely_supersedes_but_unquoted_reruns_still_do() {
let body_two_spaces = format!("two-spaces-{}", filler(300));
let body_one_space = format!("one-space-{}", filler(300));
let msgs = vec![
ChatMessage::user("echo twice with single quotes, differently spaced"),
bash_call("c0", "echo 'a b'"),
bash_result("c0", body_two_spaces.clone()),
bash_call("c1", "echo 'a b'"),
bash_result("c1", body_one_space.clone()),
];
let (view, log) = project_messages(&msgs, &no_protection_policy(), &ReductionLog::default());
assert!(
superseded_indices(&log).is_empty(),
"differently-spaced single-quoted literals must never be superseded: {:?}",
log.reductions
);
assert_eq!(view[2].content.as_deref(), Some(body_two_spaces.as_str()));
assert_eq!(view[4].content.as_deref(), Some(body_one_space.as_str()));
let body_fail = format!("FAIL-{}", filler(300));
let body_pass = format!("PASS-{}", filler(300));
let msgs2 = vec![
ChatMessage::user("run it twice"),
bash_call("c0", "cargo test"),
bash_result("c0", body_fail.clone()),
bash_call("c1", "cargo test"),
bash_result("c1", body_pass.clone()),
];
let (view2, log2) = project_messages(&msgs2, &no_protection_policy(), &ReductionLog::default());
assert_eq!(
superseded_indices(&log2),
[2usize].into_iter().collect::<HashSet<_>>(),
"unquoted separator whitespace must still collapse so a genuine re-run is superseded: {:?}",
log2.reductions
);
assert_eq!(view2[4].content.as_deref(), Some(body_pass.as_str()));
}
#[test]
fn quote_aware_collapse_grep_pattern_never_falsely_supersedes() {
let body_two_spaces = format!("match-two-spaces-{}", filler(300));
let body_one_space = format!("match-one-space-{}", filler(300));
let msgs = vec![
ChatMessage::user("grep twice, differently spaced pattern"),
bash_call("c0", r#"grep "foo bar" f"#),
bash_result("c0", body_two_spaces.clone()),
bash_call("c1", r#"grep "foo bar" f"#),
bash_result("c1", body_one_space.clone()),
];
let (view, log) = project_messages(&msgs, &no_protection_policy(), &ReductionLog::default());
assert!(
superseded_indices(&log).is_empty(),
"differently-spaced quoted grep patterns must never be superseded: {:?}",
log.reductions
);
assert_eq!(view[2].content.as_deref(), Some(body_two_spaces.as_str()));
assert_eq!(view[4].content.as_deref(), Some(body_one_space.as_str()));
}
#[test]
fn quote_aware_collapse_unbalanced_quote_command_never_falsely_supersedes() {
let body_unterminated = format!("unterminated-{}", filler(300));
let body_well_formed = format!("well-formed-{}", filler(300));
let msgs = vec![
ChatMessage::user("one unterminated quote, one well-formed command"),
bash_call("c0", r#"echo "unterminated"#),
bash_result("c0", body_unterminated.clone()),
bash_call("c1", "echo unterminated"),
bash_result("c1", body_well_formed.clone()),
];
let (view, log) = project_messages(&msgs, &no_protection_policy(), &ReductionLog::default());
assert!(
superseded_indices(&log).is_empty(),
"an unbalanced-quote command must never be falsely equated with another command: {:?}",
log.reductions
);
assert_eq!(view[2].content.as_deref(), Some(body_unterminated.as_str()));
assert_eq!(view[4].content.as_deref(), Some(body_well_formed.as_str()));
}
#[test]
fn recurring_hashes_guard_defers_byte_identical_pair_to_tr2_and_supersedes_the_differing_run() {
let body_x = format!("FAIL-X-{}", filler(400));
let body_y = format!("FAIL-Y-{}", filler(400));
let body_z = format!("PASS-Z-{}", filler(400));
let bodies = [
body_x.clone(),
body_x.clone(),
body_y.clone(),
body_z.clone(),
];
let mut msgs = vec![ChatMessage::user("run the flaky test repeatedly")];
let mut result_idx = Vec::new();
for (i, body) in bodies.iter().enumerate() {
let id = format!("c{i}");
msgs.push(bash_call(&id, "cargo test"));
result_idx.push(msgs.len());
msgs.push(bash_result(&id, body.clone()));
}
let (view, log) = project_messages(&msgs, &no_protection_policy(), &ReductionLog::default());
assert_eq!(
view[result_idx[0]].content.as_deref(),
Some(body_x.as_str()),
"the recurring-hash anchor occurrence is currently left unreduced (known limitation)"
);
assert!(reduction_id(&view[result_idx[0]]).is_none());
let dup = log
.reductions
.iter()
.find(|r| r.ptr.addr.index == result_idx[1])
.unwrap_or_else(|| panic!("occurrence #1 must be reduced: {:?}", log.reductions));
match &dup.kind {
ReductionKind::DuplicateOutput {
canonical,
original_bytes,
} => {
assert_eq!(canonical.index, result_idx[0]);
assert_eq!(*original_bytes, body_x.len());
}
other => panic!("expected DuplicateOutput for occurrence #1, got {other:?}"),
}
assert_eq!(
superseded_indices(&log),
[result_idx[2]].into_iter().collect::<HashSet<_>>(),
"only the differing (unique-hash) run may be Superseded: {:?}",
log.reductions
);
let superseded = log
.reductions
.iter()
.find(|r| r.ptr.addr.index == result_idx[2])
.unwrap();
match superseded.kind {
ReductionKind::Superseded { by, .. } => assert_eq!(by.index, result_idx[3]),
_ => unreachable!(),
}
assert_eq!(
view[result_idx[3]].content.as_deref(),
Some(body_z.as_str())
);
assert!(reduction_id(&view[result_idx[3]]).is_none());
let sidecar = session_of(msgs.clone());
verify_log(&log, &sidecar).expect("verify_log must pass clean");
let inverted = invert(&view, &log, &sidecar).expect("invert must pass clean");
for (&idx, body) in result_idx.iter().zip(bodies.iter()) {
assert_eq!(
inverted[idx].content.as_deref(),
Some(body.as_str()),
"occurrence at msg #{idx} must invert byte-exact"
);
}
}