use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use supercode::session::Session;
fn bin() -> PathBuf {
PathBuf::from(env!("CARGO_BIN_EXE_supercode"))
}
fn fixture(name: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../harness/tests/fixtures")
.join(name)
}
fn fresh_home(tag: &str) -> PathBuf {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
let dir = std::env::temp_dir().join(format!(
"supercode-codex-fidelity-{tag}-{}-{nanos}",
std::process::id()
));
std::fs::create_dir_all(&dir).unwrap();
dir
}
fn run(home: &Path, extra: &[&str]) -> Output {
let mut args = vec!["--api-key", "x", "--base-url", "http://127.0.0.1:1"];
args.extend_from_slice(extra);
Command::new(bin())
.env("SUPERCODE_HOME", home)
.env_remove("OPENROUTER_API_KEY")
.env_remove("OPENAI_API_KEY")
.env_remove("ANTHROPIC_API_KEY")
.args(&args)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.expect("failed to spawn the supercode binary")
}
fn stdout(out: &Output) -> String {
String::from_utf8_lossy(&out.stdout).into_owned()
}
fn stderr(out: &Output) -> String {
String::from_utf8_lossy(&out.stderr).into_owned()
}
#[test]
fn parity9_diagonal_preserves_every_record_type_byte_for_byte() {
let home = fresh_home("p9-diag");
let f = fixture("codex_session_eventmsg.jsonl");
let out_path = home.join("roundtrip.jsonl");
let out = run(
&home,
&[
"convert",
f.to_str().unwrap(),
"--to",
"codex",
"-o",
out_path.to_str().unwrap(),
],
);
assert!(out.status.success(), "{}", stderr(&out));
assert!(
stderr(&out).contains("byte-identical"),
"diagonal convert must claim byte-identity: {}",
stderr(&out)
);
let original = std::fs::read(&f).unwrap();
let converted = std::fs::read(&out_path).unwrap();
assert_eq!(
original, converted,
"Codex->Codex diagonal must reproduce the source file byte-for-byte \
(PARITY-9: no 411->402-style count drop)"
);
let original_session = Session::from_codex(&f).expect("load original");
let converted_session = Session::from_codex(&out_path).expect("load converted");
assert_eq!(
original_session.messages.len(),
converted_session.messages.len(),
"re-inspecting the diagonal output must report the same message count"
);
std::fs::remove_dir_all(&home).ok();
}
#[test]
fn parity9_codex_to_codex_to_codex_round_trip_is_semantically_identical() {
let home = fresh_home("p9-roundtrip");
let f = fixture("codex_session_eventmsg.jsonl");
let hop1 = home.join("hop1.jsonl");
let hop2 = home.join("hop2.jsonl");
let out1 = run(
&home,
&[
"convert",
f.to_str().unwrap(),
"--to",
"codex",
"-o",
hop1.to_str().unwrap(),
],
);
assert!(out1.status.success(), "{}", stderr(&out1));
let out2 = run(
&home,
&[
"convert",
hop1.to_str().unwrap(),
"--to",
"codex",
"-o",
hop2.to_str().unwrap(),
],
);
assert!(out2.status.success(), "{}", stderr(&out2));
let original = std::fs::read(&f).unwrap();
let after_two_hops = std::fs::read(&hop2).unwrap();
assert_eq!(
original, after_two_hops,
"two diagonal hops must still reproduce the source file byte-for-byte"
);
let orig_session = Session::from_codex(&f).unwrap();
let hop2_session = Session::from_codex(&hop2).unwrap();
assert_eq!(
orig_session.messages.len(),
hop2_session.messages.len(),
"messages, tool calls, and event outputs must match after a two-hop round trip"
);
assert!(after_two_hops
.windows(b"thread_rolled_back".len())
.any(|w| w == b"thread_rolled_back"));
assert!(after_two_hops
.windows(b"window_id".len())
.any(|w| w == b"window_id"));
std::fs::remove_dir_all(&home).ok();
}
#[test]
#[ignore = "requires local session corpus; set SUPERCODE_CORPUS=1"]
fn parity9_dev03_diagonal_holds_over_ten_real_codex_sessions() {
if std::env::var("SUPERCODE_CORPUS").is_err() {
panic!(
"SUPERCODE_CORPUS not set — this corpus test asserts nothing without \
a real ~/.codex/sessions corpus; set SUPERCODE_CORPUS=1 to run it."
);
}
let codex_home = std::env::var("HOME").expect("HOME must be set");
let corpus_dir = Path::new(&codex_home).join(".codex/sessions");
let mut sessions: Vec<PathBuf> = Vec::new();
collect_jsonl_files(&corpus_dir, &mut sessions);
sessions.sort();
let home = fresh_home("p9-dev03-corpus");
let mut checked = 0usize;
let mut non_identical: Vec<String> = Vec::new();
let mut cross_format_failures: Vec<String> = Vec::new();
for f in &sessions {
let original = match std::fs::read(f) {
Ok(b) if !b.is_empty() => b,
_ => continue,
};
let diag_out = home.join(format!("diag-{checked}.jsonl"));
let out = run(
&home,
&[
"convert",
f.to_str().unwrap(),
"--to",
"codex",
"-o",
diag_out.to_str().unwrap(),
],
);
if !out.status.success() {
non_identical.push(format!(
"{}: convert --to codex exited non-zero: {}",
f.display(),
stderr(&out)
));
continue;
}
checked += 1;
let converted = std::fs::read(&diag_out).unwrap_or_default();
if converted != original {
non_identical.push(format!(
"{}: diagonal convert NOT byte-identical ({} vs {} bytes)",
f.display(),
original.len(),
converted.len()
));
}
let cc_out = home.join(format!("cc-{checked}.jsonl"));
let cc = run(
&home,
&[
"convert",
f.to_str().unwrap(),
"--to",
"claude-code",
"-o",
cc_out.to_str().unwrap(),
],
);
if !cc.status.success() {
cross_format_failures.push(format!(
"{}: convert --to claude-code exited non-zero: {}",
f.display(),
stderr(&cc)
));
}
}
eprintln!(
"PARITY-9 dev/03 real-corpus diagonal check: {checked} sessions, \
{} non-identical, {} cross-format failures",
non_identical.len(),
cross_format_failures.len()
);
assert!(
checked >= 10,
"need at least 10 real Codex sessions to prove PARITY-9 dev/03 at scale; \
only found {checked} under {}",
corpus_dir.display()
);
assert!(
non_identical.is_empty(),
"diagonal convert was not byte-identical on {} real session(s):\n{}",
non_identical.len(),
non_identical.join("\n")
);
assert!(
cross_format_failures.is_empty(),
"codex -> claude-code convert failed on {} real session(s):\n{}",
cross_format_failures.len(),
cross_format_failures.join("\n")
);
std::fs::remove_dir_all(&home).ok();
}
fn collect_jsonl_files(dir: &Path, out: &mut Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
collect_jsonl_files(&path, out);
} else if path.extension().and_then(|e| e.to_str()) == Some("jsonl") {
out.push(path);
}
}
}
#[test]
fn parity12_event_stream_records_survive_the_diagonal_convert() {
let home = fresh_home("p12-diag");
let f = fixture("codex_session_eventmsg.jsonl");
let out_path = home.join("roundtrip.jsonl");
let out = run(
&home,
&[
"convert",
f.to_str().unwrap(),
"--to",
"codex",
"-o",
out_path.to_str().unwrap(),
],
);
assert!(out.status.success(), "{}", stderr(&out));
let converted = std::fs::read_to_string(&out_path).unwrap();
for marker in [
"\"type\":\"patch_apply_end\"",
"\"type\":\"mcp_tool_call_end\"",
"\"type\":\"thread_rolled_back\"",
"\"type\":\"thread_goal_updated\"",
"\"type\":\"exited_review_mode\"",
] {
assert!(
converted.contains(marker),
"diagonal convert must not drop `{marker}`: not found in output"
);
}
std::fs::remove_dir_all(&home).ok();
}
#[test]
fn parity12_cross_format_export_retains_tool_outputs_as_transcript_content() {
let home = fresh_home("p12-cross");
let f = fixture("codex_session_eventmsg_precompact.jsonl");
let expected = [
"test result: FAILED",
"Success. Updated the following files",
"Known flaky: test_retry_backoff",
];
let unique_to_patch_diff = "--- a/src/lib.rs\n+++ b/src/lib.rs";
let assert_retained = |target: &str, reloaded: &Session| {
let all_text: String = reloaded
.messages
.iter()
.filter_map(|m| m.content.clone())
.collect::<Vec<_>>()
.join("\n");
for expect in expected {
assert!(
all_text.contains(expect),
"{target}: replayable tool output `{expect}` missing from reloaded transcript:\n{all_text}"
);
}
assert!(
!all_text.contains(unique_to_patch_diff),
"{target}: `patch_apply_end`'s unique `unified_diff` unexpectedly \
SURVIVED cross-format export — if the loader now captures it, \
`event_msg/patch_apply_end`'s coverage doc in `audit.rs` \
(D1) must be updated to say so honestly, not left as `Dropped`:\n{all_text}"
);
};
let out_path = home.join("as-claude-code.jsonl");
let out = run(
&home,
&[
"convert",
f.to_str().unwrap(),
"--to",
"claude-code",
"-o",
out_path.to_str().unwrap(),
],
);
assert!(out.status.success(), "claude-code: {}", stderr(&out));
let written = std::fs::read_to_string(&out_path).unwrap();
let reloaded = Session::from_claude_code_str(&written)
.unwrap_or_else(|e| panic!("claude-code: export must load back via its own loader: {e}"));
assert_retained("claude-code", &reloaded);
let out_path = home.join("as-opencode.jsonl");
let out = run(
&home,
&[
"convert",
f.to_str().unwrap(),
"--to",
"opencode",
"-o",
out_path.to_str().unwrap(),
],
);
assert!(out.status.success(), "opencode: {}", stderr(&out));
let written = std::fs::read_to_string(&out_path).unwrap();
let reloaded = Session::from_opencode_str(&written)
.unwrap_or_else(|e| panic!("opencode: export must load back via its own loader: {e}"));
assert_retained("opencode", &reloaded);
let source = std::fs::read_to_string(&f).unwrap();
assert!(
source.contains("unified_diff")
&& source.contains("--- a/src/lib.rs")
&& source.contains("+++ b/src/lib.rs"),
"fixture sanity: `patch_apply_end.changes[..].unified_diff` must contain the \
literal diff header lines this test keys off of:\n{source}"
);
let source_session = Session::from_codex(&f).expect("load source fixture");
let source_text: String = source_session
.messages
.iter()
.filter_map(|m| m.content.clone())
.collect::<Vec<_>>()
.join("\n");
assert!(
!source_text.contains(unique_to_patch_diff),
"loading the Codex source directly must not surface the unified_diff either \
(same residue, same honesty bar):\n{source_text}"
);
std::fs::remove_dir_all(&home).ok();
}
#[test]
fn parity12_parity13_audit_reports_retained_not_dropped() {
let home = fresh_home("p1213-audit");
let corpus_dir = home.join("corpus");
std::fs::create_dir_all(&corpus_dir).unwrap();
std::fs::copy(
fixture("codex_session_eventmsg.jsonl"),
corpus_dir.join("codex_session_eventmsg.jsonl"),
)
.unwrap();
let out = run(
&home,
&[
"audit",
corpus_dir.to_str().unwrap(),
"--format",
"codex",
"--json",
],
);
assert!(out.status.success(), "{}", stderr(&out));
let json: serde_json::Value = serde_json::from_str(&stdout(&out)).expect("valid JSON");
let records = json["records"].as_object().expect("records object");
let coverage_of = |key: &str| -> String {
records
.get(key)
.unwrap_or_else(|| panic!("missing record `{key}` in audit output: {records:#?}"))
.get("coverage")
.and_then(|c| c.as_str())
.unwrap()
.to_string()
};
for key in ["session_meta", "turn_context"] {
assert_eq!(
coverage_of(key),
"retained",
"`{key}` is captured by the loader; audit must not call it `dropped`"
);
}
for key in [
"event_msg/agent_message",
"event_msg/thread_rolled_back",
"event_msg/thread_goal_updated",
"event_msg/exited_review_mode",
] {
assert_eq!(
coverage_of(key),
"retained",
"`{key}` is consumed by the loader; audit must not call it `dropped`"
);
}
assert_eq!(
coverage_of("response_item/reasoning"),
"retained",
"`response_item/reasoning` is captured (summary + encrypted_content flag) \
by the loader; audit must not call it `dropped`"
);
for key in [
"event_msg/token_count",
"event_msg/patch_apply_end",
"event_msg/mcp_tool_call_end",
] {
assert_eq!(
coverage_of(key),
"dropped",
"`{key}` genuinely has no unique effect on the canonical Session; \
it should stay `dropped`, not be overclaimed as `retained`"
);
}
std::fs::remove_dir_all(&home).ok();
}