From e1b6a24a4f37360abb1f5c77bedbfd470cf1c720 Mon Sep 17 00:00:00 2001 From: Arths17 Date: Wed, 25 Nov 2026 18:19:32 -0600 Subject: [PATCH] fix(hybrid): preserve reports and surface evidence write failures Signed-off-by: Arths17 --- CHANGELOG.md | 1 + crates/hybrid/src/lib.rs | 1 + crates/hybrid/src/main.rs | 94 ++++++++---- crates/hybrid/src/records.rs | 142 ++++++++++++++++++ .../research/crossover-baseline-2026-11-25.md | 10 ++ 5 files changed, 218 insertions(+), 30 deletions(-) create mode 100644 crates/hybrid/src/records.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d518bd..85347f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,7 @@ remains unsupported. - Extended the read-only RDNA2 planning CLI (`python3 -m qa rdna2`) with family/status filters, compact summaries, Codex-ready manifests under `artifacts/`, active-claim exclusion (family claims exclude those families; path-only claims block overlapping permitted files), coverage reconcile against `semantic_coverage` without inflating counts, and inventory diffs. Covered by `tests/qa/test_rdna2_plan.py`; planning only, no GPU production edits. ### Fixed +- Hybrid run and benchmark reports now use exclusive, unique filenames and report persistence failures as CLI errors. Earlier reports are preserved; this fixes evidence loss during baseline collection (#398). - Fixed the app's executable inspector so a late result from an earlier selection can no longer replace a newer one (#251). Each selection from the file picker, drag and drop or Finder Open With now takes a request number, and only the latest request may publish its report and status or clear the busy state. The previous report is cleared when a new file is chosen, and a failed inspection of the latest file is shown in red above its report. The read itself is unchanged: size checked before reading, balanced security-scoped access, nothing executed. `BetterXen --smoke-inspection-order`, run by `scripts/build-app.sh`, drives the model with an injected reader that blocks until the check releases each file, so the check (not timing) decides which read finishes first; it checks a stale success, a stale failure and a busy state held for the newer request, and all four checks fail without the change. Checked locally on Apple Silicon; not yet independently reviewed. - Startup reports now reject existing destinations before guest execution, surface journal write failures, and preserve late errors alongside signal termination. - Restored the DBT benchmark report-test fixture after the cache-invalidation counter was added. Its existing percentile, JSON and comparison tests now run in ordinary workspace CI instead of requiring a separate benchmark test-harness command. diff --git a/crates/hybrid/src/lib.rs b/crates/hybrid/src/lib.rs index 7c3053b..517b6e0 100644 --- a/crates/hybrid/src/lib.rs +++ b/crates/hybrid/src/lib.rs @@ -7,6 +7,7 @@ pub mod frames; pub mod host; pub mod json; pub mod memory; +pub mod records; pub mod run; pub mod toolchain; diff --git a/crates/hybrid/src/main.rs b/crates/hybrid/src/main.rs index 90e7a63..24a7c74 100644 --- a/crates/hybrid/src/main.rs +++ b/crates/hybrid/src/main.rs @@ -185,9 +185,25 @@ fn crossover_build() -> Option { }) } -fn save(dir: &std::path::Path, name: &str, j: &Json) { - let _ = std::fs::create_dir_all(dir); - let _ = std::fs::write(dir.join(name), j.render()); +fn save(dir: &std::path::Path, kind: &str, j: &Json) -> Result<(), String> { + betterxen_hybrid::records::save(dir, kind, j) + .map(|path| eprintln!("report saved: {}", path.display())) + .map_err(|error| format!("cannot save {kind} report in {}: {error}", dir.display())) +} + +fn finish_report(passed: bool, out: Option<&std::path::Path>, kind: &str, j: &Json) -> ExitCode { + print!("{}", j.render()); + if let Some(dir) = out + && let Err(error) = save(dir, kind, j) + { + eprintln!("{error}"); + return ExitCode::from(1); + } + if passed { + ExitCode::SUCCESS + } else { + ExitCode::from(1) + } } fn run_cmd(args: &[String]) -> ExitCode { @@ -199,24 +215,12 @@ fn run_cmd(args: &[String]) -> ExitCode { } }; let r = run::execute(&a.cfg, crossover_build()); - print!("{}", r.json.render()); - if let Some(dir) = &a.out { - save( - dir, - &format!( - "run-{}.json", - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map_or(0, |d| d.as_secs()) - ), - &r.json, - ); - } - if r.class == run::Class::Passed { - ExitCode::SUCCESS - } else { - ExitCode::from(1) - } + finish_report( + r.class == run::Class::Passed, + a.out.as_deref(), + "run", + &r.json, + ) } fn bench_cmd(args: &[String]) -> ExitCode { @@ -258,15 +262,7 @@ fn bench_cmd(args: &[String]) -> ExitCode { ("host", host::collect().to_json()), ("results", Json::Arr(per_backend)), ]); - print!("{}", doc.render()); - if let Some(dir) = &a.out { - save(dir, "bench.json", &doc); - } - if all_ok { - ExitCode::SUCCESS - } else { - ExitCode::from(1) - } + finish_report(all_ok, a.out.as_deref(), "bench", &doc) } fn main() -> ExitCode { @@ -316,3 +312,41 @@ fn main() -> ExitCode { } } } + +#[cfg(test)] +mod report_tests { + use super::*; + + #[test] + fn requested_evidence_failure_overrides_a_passing_workload() { + let path = std::env::temp_dir().join(format!( + "bx-report-invalid-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + std::fs::write(&path, "keep this file").unwrap(); + for kind in ["run", "bench"] { + assert_eq!( + finish_report(true, Some(&path), kind, &Json::Null), + ExitCode::from(1) + ); + } + assert_eq!(std::fs::read_to_string(&path).unwrap(), "keep this file"); + std::fs::remove_file(path).unwrap(); + } + + #[test] + fn stdout_only_reporting_preserves_workload_status() { + assert_eq!( + finish_report(true, None, "run", &Json::Null), + ExitCode::SUCCESS + ); + assert_eq!( + finish_report(false, None, "bench", &Json::Null), + ExitCode::from(1) + ); + } +} diff --git a/crates/hybrid/src/records.rs b/crates/hybrid/src/records.rs new file mode 100644 index 0000000..18eff41 --- /dev/null +++ b/crates/hybrid/src/records.rs @@ -0,0 +1,142 @@ +//! Exclusive report creation: previous evidence is never replaced. + +use crate::json::Json; +use std::fs::{File, OpenOptions}; +use std::io::{self, Write}; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::time::{SystemTime, UNIX_EPOCH}; + +static SEQUENCE: AtomicU64 = AtomicU64::new(0); + +/// Save a run or benchmark report under a unique name, reporting all I/O errors. +pub fn save(dir: &Path, kind: &str, report: &Json) -> io::Result { + if !matches!(kind, "run" | "bench") { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "unknown report kind", + )); + } + std::fs::create_dir_all(dir)?; + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_err(io::Error::other)? + .as_nanos(); + let bytes = report.render(); + loop { + let sequence = SEQUENCE.fetch_add(1, Ordering::Relaxed); + let path = dir.join(format!( + "{kind}-{nanos}-{}-{sequence}.json", + std::process::id() + )); + match write_new(&path, bytes.as_bytes(), |file, bytes| file.write_all(bytes)) { + Err(e) if e.kind() == io::ErrorKind::AlreadyExists => continue, + Err(e) => return Err(e), + Ok(()) => return Ok(path), + } + } +} + +fn write_new( + path: &Path, + bytes: &[u8], + write: impl FnOnce(&mut File, &[u8]) -> io::Result<()>, +) -> io::Result<()> { + let mut file = OpenOptions::new().write(true).create_new(true).open(path)?; + if let Err(error) = write(&mut file, bytes).and_then(|()| file.sync_all()) { + drop(file); + // Remove only the incomplete file this call exclusively created. + return match std::fs::remove_file(path) { + Ok(()) => Err(error), + Err(cleanup) => Err(io::Error::new( + error.kind(), + format!( + "{error}; cannot remove incomplete report {}: {cleanup}", + path.display() + ), + )), + }; + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn directory() -> PathBuf { + let id = SEQUENCE.fetch_add(1, Ordering::Relaxed); + let dir = std::env::temp_dir().join(format!("bx-records-{}-{id}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + dir + } + + #[test] + fn repeated_run_and_bench_reports_preserve_prior_evidence() { + let dir = directory(); + std::fs::write(dir.join("bench.json"), "previous evidence").unwrap(); + let first = save(&dir, "run", &Json::str("first")).unwrap(); + let second = save(&dir, "run", &Json::str("second")).unwrap(); + let bench1 = save(&dir, "bench", &Json::str("bench one")).unwrap(); + let bench2 = save(&dir, "bench", &Json::str("bench two")).unwrap(); + assert_ne!(first, second); + assert_ne!(bench1, bench2); + assert_eq!( + std::fs::read_to_string(first).unwrap(), + Json::str("first").render() + ); + assert_eq!( + std::fs::read_to_string(second).unwrap(), + Json::str("second").render() + ); + assert_eq!( + std::fs::read_to_string(bench1).unwrap(), + Json::str("bench one").render() + ); + assert_eq!( + std::fs::read_to_string(bench2).unwrap(), + Json::str("bench two").render() + ); + assert_eq!( + std::fs::read_to_string(dir.join("bench.json")).unwrap(), + "previous evidence" + ); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn existing_destination_is_never_truncated() { + let dir = directory(); + let path = dir.join("record.json"); + std::fs::write(&path, "original").unwrap(); + let error = + write_new(&path, b"replacement", |file, bytes| file.write_all(bytes)).unwrap_err(); + assert_eq!(error.kind(), io::ErrorKind::AlreadyExists); + assert_eq!(std::fs::read_to_string(&path).unwrap(), "original"); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn invalid_directory_is_an_error() { + let dir = directory(); + let path = dir.join("not-a-directory"); + std::fs::write(&path, "original").unwrap(); + assert!(save(&path, "run", &Json::Null).is_err()); + assert_eq!(std::fs::read_to_string(path).unwrap(), "original"); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn partial_write_failure_is_reported_and_incomplete_file_removed() { + let dir = directory(); + let path = dir.join("record.json"); + let error = write_new(&path, b"a complete report", |file, _| { + file.write_all(b"partial")?; + Err(io::Error::other("injected write failure")) + }) + .unwrap_err(); + assert!(error.to_string().contains("injected write failure")); + assert!(!path.exists()); + std::fs::remove_dir_all(dir).unwrap(); + } +} diff --git a/docs/research/crossover-baseline-2026-11-25.md b/docs/research/crossover-baseline-2026-11-25.md index a32e421..b52ff38 100644 --- a/docs/research/crossover-baseline-2026-11-25.md +++ b/docs/research/crossover-baseline-2026-11-25.md @@ -57,3 +57,13 @@ Observations, with their limits: ## What this does and does not establish Established on this machine and build: CrossOver 26.3 runs original x86-64 console, threaded, Direct3D 11 clear and triangle programs under four graphics backends with correct pixels; it does not run ARM64 Windows programs; BetterXen can detect, supervise, classify and record such runs. Not established: anything about Direct3D 12, real-scene performance, other macOS or CrossOver versions, other machines, or any game. A second contributor still has to reproduce this. + +## Saving new baseline evidence + +The runner now writes `run---.json` and +`bench---.json` when `--out` is supplied. Exclusive +creation preserves existing reports, including older `bench.json` files. The +selected path is printed to stderr; stdout retains the workload classification +and report JSON. A directory, creation or write error returns a nonzero CLI +status even if the workload itself passed. These persistence changes do not +revalidate or replace the historical results above.