use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use async_trait::async_trait;
use supercode_harness::reduce::rehydrate::expand_reduction;
use supercode_harness::reduce::{self, ReductionKind, ReductionLog, ReductionPolicy};
use supercode_harness::session::{Session, SessionFormat};
use supercode_harness::sidecar::SidecarWriter;
use supercode_harness::store::SessionStore;
use supercode_harness::tokens::{estimate_view_tokens, fmt_approx_tokens};
use supercode_harness::tools::{Tool, ToolContext, ToolRegistry};
use supercode_harness::{
Agent, ChatMessage, ChatRequest, Config, Error, FunctionCall, Provider, Role, ToolCall, Usage,
};
fn temp_dir(tag: &str) -> PathBuf {
static N: AtomicUsize = AtomicUsize::new(0);
let dir = std::env::temp_dir().join(format!(
"supercode-e2e-rescue-{tag}-{}-{}",
std::process::id(),
N.fetch_add(1, Ordering::SeqCst)
));
std::fs::create_dir_all(&dir).unwrap();
dir
}
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 repeat_lines(prefix: &str, n: usize) -> String {
let mut s = String::new();
for i in 0..n {
s.push_str(&format!(
"{prefix} line {i:04} - lorem ipsum dolor sit amet consectetur adipiscing elit\n"
));
}
s
}
fn lib_rs_source(fix_line: &str) -> String {
format!(
"//! src/lib.rs -- arithmetic helpers under active investigation\n\n{}\npub fn compute(x: i32) -> i32 {{\n {fix_line}\n}}\n\n{}\n",
repeat_lines("context", 80),
repeat_lines("trailer", 80),
)
}
fn config_rs_source(fix_line: &str) -> String {
format!(
"//! src/config.rs -- tunable constants\n\n{}\npub const FACTOR: i32 = {{\n {fix_line}\n}};\n\n{}\n",
repeat_lines("context", 35),
repeat_lines("trailer", 35),
)
}
fn notes_md(n: usize) -> String {
let mut s = String::from("# Investigation Notes\n\n");
for i in 0..n {
s.push_str(&format!(
"- item {i:04}: verified behavior around compute() edge case reproduction step, all clear\n"
));
}
s.push_str("\nFINAL-NOTES-MARKER-e2e-rescue-9f21\n");
s
}
fn cargo_output(base: &str, run_marker: &str) -> String {
format!("$ cargo test\n{base}\n{run_marker}\n")
}
const PROVIDER_A_QUOTA_TOKENS: u64 = 25_000;
struct ScriptedBashTool {
outputs: Vec<String>,
calls: AtomicUsize,
}
#[async_trait]
impl Tool for ScriptedBashTool {
fn name(&self) -> &str {
"bash"
}
fn description(&self) -> &str {
"Execute a shell command (scripted fake for the e2e rescue demo)."
}
fn parameters(&self) -> serde_json::Value {
serde_json::json!({
"type": "object",
"properties": {"command": {"type": "string"}},
"required": ["command"],
})
}
async fn execute(
&self,
_args: serde_json::Value,
_ctx: &ToolContext,
) -> supercode_harness::Result<String> {
let n = self.calls.fetch_add(1, Ordering::SeqCst);
Ok(self
.outputs
.get(n)
.cloned()
.unwrap_or_else(|| "(no more scripted bash output)".to_string()))
}
}
enum Step {
Call {
id: &'static str,
tool: &'static str,
arguments: String,
},
Text(&'static str),
}
fn assert_message_byte_exact(restored: &ChatMessage, original: &ChatMessage, context: &str) {
assert_eq!(restored.role, original.role, "{context}: role mismatch");
assert_eq!(
restored.content, original.content,
"{context}: content mismatch"
);
assert_eq!(
restored.content_parts, original.content_parts,
"{context}: content_parts mismatch"
);
assert_eq!(
restored.tool_call_id, original.tool_call_id,
"{context}: tool_call_id mismatch"
);
assert_eq!(restored.name, original.name, "{context}: name mismatch");
match (&restored.tool_calls, &original.tool_calls) {
(None, None) => {}
(Some(r), Some(o)) => {
assert_eq!(r.len(), o.len(), "{context}: tool_calls length mismatch");
for (rc, oc) in r.iter().zip(o.iter()) {
assert_eq!(rc.id, oc.id, "{context}: tool_call id mismatch");
assert_eq!(rc.kind, oc.kind, "{context}: tool_call kind mismatch");
assert_eq!(
rc.function.name, oc.function.name,
"{context}: tool_call function name mismatch"
);
assert_eq!(
rc.function.arguments, oc.function.arguments,
"{context}: tool_call function arguments mismatch -- this is exactly the \
byte-exactness TR-10's ToolInputElided restoration into \
tool_calls[..].function.arguments must uphold"
);
}
}
(r, o) => panic!(
"{context}: tool_calls presence mismatch: restored.is_some()={} \
original.is_some()={}",
r.is_some(),
o.is_some()
),
}
}
fn call_msg(id: &'static str, tool: &'static str, arguments: String) -> 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: tool.to_string(),
arguments,
},
}]),
tool_call_id: None,
name: None,
metadata: Default::default(),
}
}
struct ScriptedTurns {
calls: AtomicUsize,
steps: Vec<Step>,
}
#[async_trait]
impl Provider for ScriptedTurns {
async fn complete(
&self,
_req: &ChatRequest,
_on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
) -> supercode_harness::Result<(ChatMessage, Usage)> {
let n = self.calls.fetch_add(1, Ordering::SeqCst);
let step = self
.steps
.get(n)
.unwrap_or_else(|| panic!("ScriptedTurns ran out of steps at call {n}"));
let msg = match step {
Step::Call {
id,
tool,
arguments,
} => call_msg(id, tool, arguments.clone()),
Step::Text(t) => ChatMessage::assistant(t.to_string()),
};
Ok((msg, Usage::default()))
}
}
struct RateLimitedProviderA {
budget_tokens: u64,
}
#[async_trait]
impl Provider for RateLimitedProviderA {
async fn complete(
&self,
req: &ChatRequest,
_on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
) -> supercode_harness::Result<(ChatMessage, Usage)> {
let est = estimate_view_tokens(&req.messages);
if est > self.budget_tokens {
return Err(Error::Other(format!(
"rate_limit_exceeded: request is ~{est} tokens, over this account's current \
quota of ~{} tokens (HTTP 429 Too Many Tokens)",
self.budget_tokens
)));
}
Ok((
ChatMessage::assistant("(would have succeeded)"),
Usage::default(),
))
}
}
struct ProviderB {
last_request_tokens: std::sync::Mutex<Option<u64>>,
}
#[async_trait]
impl Provider for ProviderB {
async fn complete(
&self,
req: &ChatRequest,
_on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
) -> supercode_harness::Result<(ChatMessage, Usage)> {
let est = estimate_view_tokens(&req.messages);
*self.last_request_tokens.lock().unwrap() = Some(est);
Ok((
ChatMessage::assistant(
"Confirmed: the fix in src/lib.rs is correct (compute() now returns x + 1), \
cargo test passes, and NOTES.md records the investigation.",
),
Usage::default(),
))
}
}
#[tokio::test]
async fn rate_limit_rescue_continues_losslessly_with_massive_token_reduction() {
let dir = temp_dir("main");
let store_dir = dir.join("store");
let cwd = dir.join("project");
let store = SessionStore::open(&store_dir).unwrap();
let name = "e2e-rescue-demo";
let sidecar_path = store.sidecar_path(name);
std::fs::create_dir_all(&cwd).unwrap();
for i in 0..30 {
std::fs::write(cwd.join(format!("file_{i:02}.txt")), "x").unwrap();
}
std::fs::create_dir_all(cwd.join("src")).unwrap();
let v0 = lib_rs_source("x * 2 // BUG: should add one, TODO fix");
std::fs::write(cwd.join("src/lib.rs"), &v0).unwrap();
let config_v0 = config_rs_source("2 // placeholder default");
std::fs::write(cwd.join("src/config.rs"), &config_v0).unwrap();
let cargo_base = load_raw("cargo_build"); let docker_base = load_raw("docker_pull");
let run0 = cargo_output(
&cargo_base,
"test result: FAILED. 3 passed; 1 failed; run=0 (initial reproduction) FAIL-MARKER-e2e-0",
);
let run1 = cargo_output(&cargo_base, "test result: FAILED. 3 passed; 1 failed; run=1 (after first fix attempt, still broken) FAIL-MARKER-e2e-1");
let run2 = cargo_output(&cargo_base, "test result: FAILED. 3 passed; 1 failed; run=2 (after second fix attempt, still broken) FAIL-MARKER-e2e-2");
let run3 = cargo_output(
&cargo_base,
"test result: ok. 4 passed; 0 failed; run=3 (fix confirmed) PASS-MARKER-e2e-3",
);
let v1 = lib_rs_source("x * 3 // WIP: adjusting multiplier");
let v2 = lib_rs_source("x + 1 // FIXED: correct increment logic");
let notes = notes_md(150);
let config_v1 = config_rs_source("3 // TUNED-MARKER-e2e-rescue: raised after profiling");
let bash_outputs = vec![
run0.clone(),
run1.clone(),
docker_base.clone(),
run2.clone(),
run3.clone(),
];
let steps = vec![
Step::Call {
id: "a1",
tool: "bash",
arguments: serde_json::json!({"command": "cargo test"}).to_string(),
},
Step::Call {
id: "a2",
tool: "read_file",
arguments: serde_json::json!({"path": "src/lib.rs"}).to_string(),
},
Step::Call {
id: "a3",
tool: "write_file",
arguments: serde_json::json!({"path": "src/lib.rs", "content": v1}).to_string(),
},
Step::Call {
id: "a4",
tool: "bash",
arguments: serde_json::json!({"command": "cargo test"}).to_string(),
},
Step::Text("Applied a first fix attempt; tests still failing, checking further."),
Step::Call {
id: "b0r",
tool: "read_file",
arguments: serde_json::json!({"path": "src/config.rs"}).to_string(),
},
Step::Call {
id: "b0w",
tool: "write_file",
arguments: serde_json::json!({"path": "src/config.rs", "content": config_v1})
.to_string(),
},
Step::Call {
id: "b0r2",
tool: "read_file",
arguments: serde_json::json!({"path": "src/config.rs"}).to_string(),
},
Step::Call {
id: "b1",
tool: "read_file",
arguments: serde_json::json!({"path": "src/lib.rs"}).to_string(),
},
Step::Call {
id: "b2",
tool: "bash",
arguments: serde_json::json!({"command": "docker pull myimage:latest"}).to_string(),
},
Step::Call {
id: "b3",
tool: "write_file",
arguments: serde_json::json!({"path": "src/lib.rs", "content": v2}).to_string(),
},
Step::Call {
id: "b4",
tool: "bash",
arguments: serde_json::json!({"command": "cargo test"}).to_string(),
},
Step::Call {
id: "b5",
tool: "list_dir",
arguments: serde_json::json!({"path": "."}).to_string(),
},
Step::Text("Second attempt applied; re-running tests."),
Step::Call {
id: "c1",
tool: "read_file",
arguments: serde_json::json!({"path": "src/lib.rs"}).to_string(),
},
Step::Call {
id: "c2",
tool: "bash",
arguments: serde_json::json!({"command": "cargo test"}).to_string(),
},
Step::Call {
id: "c3",
tool: "list_dir",
arguments: serde_json::json!({"path": "."}).to_string(),
},
Step::Call {
id: "c4",
tool: "search",
arguments: serde_json::json!({"pattern": "lorem"}).to_string(),
},
Step::Call {
id: "c5",
tool: "write_file",
arguments: serde_json::json!({"path": "NOTES.md", "content": notes}).to_string(),
},
Step::Call {
id: "c6",
tool: "read_file",
arguments: serde_json::json!({"path": "NOTES.md"}).to_string(),
},
Step::Call {
id: "c7",
tool: "list_dir",
arguments: serde_json::json!({"path": "src"}).to_string(),
},
Step::Call {
id: "c8",
tool: "search",
arguments: serde_json::json!({"pattern": "FIXED"}).to_string(),
},
Step::Call {
id: "c9",
tool: "list_dir",
arguments: serde_json::json!({"path": "."}).to_string(),
},
Step::Text("All done -- tests pass, notes recorded, fix confirmed."),
];
let config = Config::builder()
.cwd(cwd.clone())
.system_prompt("you are a careful coding agent investigating a failing test")
.build();
let mut registry = ToolRegistry::with_builtins();
registry.register(ScriptedBashTool {
outputs: bash_outputs,
calls: AtomicUsize::new(0),
});
let mut agent1 = Agent::with_parts(
config,
Box::new(ScriptedTurns {
calls: AtomicUsize::new(0),
steps,
}),
registry,
);
let empty_session = Session::from_claude_code_str("").unwrap();
let writer = SidecarWriter::create(&sidecar_path, &empty_session).unwrap();
agent1.set_recorder(writer);
let reply_a = agent1
.send("There's a failing test around compute(); investigate.")
.await
.unwrap();
assert!(reply_a.contains("still failing"));
let reply_b = agent1
.send("Still failing -- dig deeper and try again.")
.await
.unwrap();
assert!(reply_b.contains("Second attempt"));
let reply_c = agent1
.send("Confirm the fix, clean up, and summarize.")
.await
.unwrap();
assert!(reply_c.contains("All done"));
let tokens_before = estimate_view_tokens(agent1.history());
println!(
"[e2e-rescue] tokens_before (full, unreduced context): {}",
fmt_approx_tokens(tokens_before)
);
let sidecar_before_rescue_text = std::fs::read_to_string(&sidecar_path).unwrap();
let export_before_rescue =
reduce::export_session(&sidecar_before_rescue_text, SessionFormat::ClaudeCode)
.expect("export_session must succeed on the pristine pre-rescue sidecar");
assert!(
tokens_before > 10_000,
"the fixture must be genuinely large for the rate-limit rescue narrative to be real: \
only ~{tokens_before} tokens"
);
assert!(
tokens_before > PROVIDER_A_QUOTA_TOKENS,
"the full request must genuinely exceed the fixed, independent quota: \
tokens_before=~{tokens_before} must be > {PROVIDER_A_QUOTA_TOKENS}"
);
let provider_a_tightened = RateLimitedProviderA {
budget_tokens: PROVIDER_A_QUOTA_TOKENS,
};
let full_request = ChatRequest::new("test-model", agent1.history().to_vec());
let noop = |_: &str| {};
let rejection = provider_a_tightened.complete(&full_request, &noop).await;
let rejection_err = rejection.expect_err(
"provider A must reject the full-size (unreduced) request once its quota has tightened",
);
let rejection_msg = rejection_err.to_string();
println!("[e2e-rescue] provider A rejected the full-size request: {rejection_msg}");
assert!(rejection_msg.contains("rate_limit_exceeded"));
let policy = ReductionPolicy::default();
let session_before = Session::from_sidecar_str(&sidecar_before_rescue_text).unwrap();
let (reduced_view, log_computed) =
reduce::project_messages(&session_before.messages, &policy, &ReductionLog::default());
let mut full_reduced_request_messages = vec![agent1.history()[0].clone()];
full_reduced_request_messages.extend(reduced_view.clone());
let tokens_after = estimate_view_tokens(&full_reduced_request_messages);
let reduction_pct = 1.0 - (tokens_after as f64 / tokens_before as f64);
println!(
"[e2e-rescue] tokens_after (reduced view sent to provider B): {}",
fmt_approx_tokens(tokens_after)
);
println!(
"[e2e-rescue] HEADLINE: {:.1}% token reduction ({} -> {})",
reduction_pct * 100.0,
fmt_approx_tokens(tokens_before),
fmt_approx_tokens(tokens_after),
);
assert!(
reduction_pct >= 0.70,
"expected a massive (>=70%) reduction, got {:.1}% ({tokens_before} -> {tokens_after})",
reduction_pct * 100.0
);
assert!(
tokens_after < PROVIDER_A_QUOTA_TOKENS,
"the reduced view must land under the fixed quota: tokens_after=~{tokens_after} must \
be < {PROVIDER_A_QUOTA_TOKENS}"
);
let reduced_request = ChatRequest::new("test-model", full_reduced_request_messages.clone());
let acceptance = provider_a_tightened.complete(&reduced_request, &noop).await;
let (accepted_msg, _usage) = acceptance.unwrap_or_else(|e| {
panic!(
"the SAME rate-limited provider A that rejected the full-size request must ACCEPT \
the reduced request now that it is under the identical fixed quota of ~{} tokens: {e}",
PROVIDER_A_QUOTA_TOKENS
)
});
println!(
"[e2e-rescue] provider A (the SAME instance, SAME fixed quota of {}) now ACCEPTS the \
reduced request: {:?}",
fmt_approx_tokens(PROVIDER_A_QUOTA_TOKENS),
accepted_msg.content
);
let sidecar_after_rescue_before_continue = std::fs::read_to_string(&sidecar_path).unwrap();
assert_eq!(
sidecar_after_rescue_before_continue, sidecar_before_rescue_text,
"the rescue (installing a policy + projecting) must add ZERO bytes to the sidecar"
);
let mut kinds: Vec<&'static str> = log_computed
.reductions
.iter()
.map(|r| match r.kind {
ReductionKind::ToolOutputTruncated { .. } => "ToolOutputTruncated",
ReductionKind::FileReadElided { .. } => "FileReadElided",
ReductionKind::ImageRedacted { .. } => "ImageRedacted",
ReductionKind::TurnsCleared { .. } => "TurnsCleared",
ReductionKind::ToolInputElided { .. } => "ToolInputElided",
ReductionKind::OutputNormalized { .. } => "OutputNormalized",
ReductionKind::FileReadDiffed { .. } => "FileReadDiffed",
ReductionKind::DuplicateOutput { .. } => "DuplicateOutput",
ReductionKind::Superseded { .. } => "Superseded",
})
.collect();
kinds.sort_unstable();
kinds.dedup();
println!("[e2e-rescue] reduction kinds exercised: {kinds:?}");
assert!(
kinds.len() >= 4,
"expected the fixture to exercise a rich mix of reduction techniques, only saw: {kinds:?}"
);
let config_b = Config::builder().cwd(cwd.clone()).build();
let provider_b_shared = std::sync::Arc::new(ProviderB {
last_request_tokens: std::sync::Mutex::new(None),
});
let mut agent2 = Agent::with_provider_arc(
config_b,
provider_b_shared.clone() as std::sync::Arc<dyn Provider>,
);
agent2.set_recorder(SidecarWriter::open_append(&sidecar_path).unwrap());
agent2.load_session(session_before.clone());
agent2.set_reduction_policy(policy.clone());
let continuation_reply = agent2
.send("Continue: confirm everything passes and summarize the fix.")
.await
.unwrap();
assert!(continuation_reply.contains("Confirmed"));
assert_eq!(
agent2.reduction_log(),
&log_computed,
"agent2's own live projection must match the standalone measurement exactly"
);
let wire_tokens_b = provider_b_shared
.last_request_tokens
.lock()
.unwrap()
.expect("provider B must have received a request");
println!(
"[e2e-rescue] wire request to provider B: {} (tokens_after was {})",
fmt_approx_tokens(wire_tokens_b),
fmt_approx_tokens(tokens_after)
);
assert!(
wire_tokens_b < tokens_before / 2,
"the request to provider B must be drastically smaller than the original: \
{wire_tokens_b} vs tokens_before={tokens_before}"
);
assert!(
wire_tokens_b <= tokens_after + 200,
"the request to provider B should be within a small margin of tokens_after \
(just the new turn added): {wire_tokens_b} vs tokens_after={tokens_after}"
);
store
.save_reduction_log(name, agent2.reduction_log())
.unwrap();
let sidecar_final_text = store
.load_sidecar(name)
.unwrap()
.expect("sidecar must exist on disk");
let sidecar_final = Session::from_sidecar_str(&sidecar_final_text).unwrap();
let log_final = store
.load_reduction_log(name)
.unwrap()
.expect("reduction log must exist on disk");
assert_eq!(log_final, log_computed);
let original_message_count = session_before.messages.len();
assert!(
sidecar_final.messages.len() > original_message_count,
"the continuation must genuinely have appended new messages to the SAME sidecar"
);
let base_messages = &sidecar_final.messages[..original_message_count];
reduce::verify_log(&log_final, &sidecar_final)
.expect("verify_log must pass clean against the reloaded-from-disk sidecar");
println!("[e2e-rescue] lossless check (a) verify_log: OK");
let (fresh_view, reprojected_log) =
reduce::project_messages(base_messages, &policy, &ReductionLog::default());
assert_eq!(
reprojected_log, log_final,
"reprojecting from the reloaded sidecar must reproduce the identical log"
);
let inverted = reduce::invert(&fresh_view, &log_final, &sidecar_final)
.expect("invert must pass clean against the reloaded-from-disk sidecar");
assert_eq!(inverted.len(), base_messages.len());
for (i, (restored, original)) in inverted.iter().zip(base_messages).enumerate() {
assert_message_byte_exact(
restored,
original,
&format!("invert byte-exact check, message index {i}"),
);
}
println!(
"[e2e-rescue] lossless check (b) invert byte-exact: OK ({} messages)",
inverted.len()
);
let restored_text = inverted
.iter()
.filter_map(|m| m.content.clone())
.collect::<Vec<_>>()
.join("\n---\n");
assert!(restored_text.contains("FIXED: correct increment logic"));
assert!(restored_text.contains("FINAL-NOTES-MARKER-e2e-rescue-9f21"));
assert!(restored_text.contains(&v0));
assert!(restored_text.contains(&v1));
assert!(restored_text.contains(&v2));
assert!(restored_text.contains(¬es));
assert!(restored_text.contains(&run0));
assert!(restored_text.contains(&run1));
assert!(restored_text.contains(&run2));
assert!(restored_text.contains(&config_v0));
assert!(restored_text.contains(&config_v1));
let planted_payload_by_call_id: std::collections::HashMap<&str, &str> = [
("a3", v1.as_str()),
("b0w", config_v1.as_str()),
("b3", v2.as_str()),
("c5", notes.as_str()),
]
.into_iter()
.collect();
let tool_input_elisions: Vec<&reduce::Reduction> = log_final
.reductions
.iter()
.filter(|r| matches!(r.kind, ReductionKind::ToolInputElided { .. }))
.collect();
assert!(
!tool_input_elisions.is_empty(),
"at least one ToolInputElided reduction expected"
);
for r in &tool_input_elisions {
let call_id = match &r.kind {
ReductionKind::ToolInputElided { call_id, .. } => call_id.as_str(),
_ => unreachable!("filtered to ToolInputElided above"),
};
let expected = *planted_payload_by_call_id.get(call_id).unwrap_or_else(|| {
panic!(
"ToolInputElided reduction {} addresses unexpected call_id {call_id} -- not one \
of the scripted write_file calls (a3/b0w/b3/c5)",
r.id
)
});
let outcome = expand_reduction(&log_final, &sidecar_final.messages, None, &r.id, None)
.unwrap_or_else(|e| {
panic!(
"expand_reduction({}) must resolve cleanly against the reloaded sidecar: {e}",
r.id
)
});
let expanded_plain: String =
serde_json::from_str(&outcome.content).unwrap_or_else(|_| outcome.content.clone());
assert_eq!(
expanded_plain, expected,
"expand_reduction({}) (call_id {call_id}) must restore EXACTLY the payload planted \
for that specific write, byte-exact",
r.id
);
}
let elided_write = tool_input_elisions[0];
println!(
"[e2e-rescue] lossless check (c) expand_reduction byte-exact: OK ({} ToolInputElided \
reduction(s), each matched 1:1 to its planted payload)",
tool_input_elisions.len()
);
let export_after_rescue_before_continue = reduce::export_session(
&sidecar_after_rescue_before_continue,
SessionFormat::ClaudeCode,
)
.unwrap();
assert_eq!(
export_before_rescue, export_after_rescue_before_continue,
"export right after the rescue (pre-continuation) must be byte-identical to the \
pre-rescue export"
);
let export_final =
reduce::export_session(&sidecar_final_text, SessionFormat::ClaudeCode).unwrap();
assert!(
export_final.starts_with(&export_before_rescue),
"the full-fidelity export after the rescue+continuation must carry the ENTIRE \
original pre-rescue export as a byte-identical prefix"
);
println!("[e2e-rescue] lossless check (d) export_session byte-identical: OK");
let per_kind_breakdown: Vec<serde_json::Value> = log_final
.reductions
.iter()
.map(|r| {
let kind_name = match &r.kind {
ReductionKind::ToolOutputTruncated { .. } => "ToolOutputTruncated",
ReductionKind::FileReadElided { .. } => "FileReadElided",
ReductionKind::ImageRedacted { .. } => "ImageRedacted",
ReductionKind::TurnsCleared { .. } => "TurnsCleared",
ReductionKind::ToolInputElided { .. } => "ToolInputElided",
ReductionKind::OutputNormalized { .. } => "OutputNormalized",
ReductionKind::FileReadDiffed { .. } => "FileReadDiffed",
ReductionKind::DuplicateOutput { .. } => "DuplicateOutput",
ReductionKind::Superseded { .. } => "Superseded",
};
serde_json::json!({
"id": r.id,
"kind": kind_name,
"placeholder": r.placeholder,
})
})
.collect();
let artifact = serde_json::json!({
"test": "rate_limit_rescue_continues_losslessly_with_massive_token_reduction",
"description": "Feature 3 flagship proof: pick up a real session, hit a provider \
rate-limit, rescue it by reducing tokens + switching providers at minimum cost, \
continue it, and export/rehydrate back losslessly.",
"measured": {
"tokens_before": tokens_before,
"tokens_after": tokens_after,
"reduction_ratio": tokens_after as f64 / tokens_before as f64,
"reduction_pct": reduction_pct,
"wire_tokens_to_provider_b": wire_tokens_b,
},
"rate_limit_rejection": {
"provider_a_quota_tokens_fixed_independent": PROVIDER_A_QUOTA_TOKENS,
"rejection_message": rejection_msg,
"reduced_request_replayed_against_same_provider_a_instance": true,
"reduced_request_accepted": true,
},
"reduction_breakdown": per_kind_breakdown,
"continuation_turn": {
"user_message": "Continue: confirm everything passes and summarize the fix.",
"assistant_reply": continuation_reply,
},
"lossless_verification": {
"verify_log": "ok",
"invert_byte_exact_full_chatmessage": true,
"invert_messages_checked": inverted.len(),
"expand_reduction_byte_exact_1to1_all_tool_input_elisions": true,
"expand_reduction_tool_input_elisions_checked": tool_input_elisions.len(),
"expand_reduction_sample_id": elided_write.id,
"export_session_byte_identical_prefix": true,
},
"scope_notes": {
"token_counts_are_message_payload_only": "tokens_before/tokens_after/the fixed \
quota all count `messages` only via tokens::estimate_view_tokens -- none \
include the `tools` JSON-schema block a live provider request also carries. \
The omission is symmetric across before/after/quota, so it does not distort \
the reduction ratio or the quota-crossing comparison, but these are not full \
wire-request byte counts.",
"fixture_is_curated_not_statistically_average": "hand-assembled to exercise every \
reduction kind (repeated test reruns, file re-reads, large writes -- all \
realistic redundancy patterns) in one pass; the measured reduction_pct is an \
ACHIEVABLE end-to-end figure demonstrating the mechanism across all kinds, not \
a claim about an average session's reduction.",
"provider_b_reply_is_scripted": "proves cross-provider continuation PLUMBING on \
reduced context (request built/sent/threaded back onto the same sidecar), not \
that a live model produces a correct answer -- same idiom as \
tr9_handoff_guarantor.rs/tr2_dedup_guarantor.rs/tr6_supersede_guarantor.rs/\
tr10_input_guarantor.rs's scripted providers.",
},
});
let workspace_target = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target");
std::fs::create_dir_all(&workspace_target).ok();
let artifact_path = workspace_target.join("e2e-rescue-demo.json");
std::fs::write(
&artifact_path,
serde_json::to_string_pretty(&artifact).unwrap(),
)
.unwrap();
assert!(artifact_path.exists());
println!(
"[e2e-rescue] artifact archived at {}",
artifact_path.display()
);
println!(
"\n=== HEADLINE ===\n{} -> {} ({:.1}% reduction) | provider B saw only {} on the wire\n================\n",
fmt_approx_tokens(tokens_before),
fmt_approx_tokens(tokens_after),
reduction_pct * 100.0,
fmt_approx_tokens(wire_tokens_b),
);
std::fs::remove_dir_all(&dir).ok();
}