From 59cfa2959ffad7d3777a9967f067040cdd786609 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Sun, 1 Mar 2026 00:33:53 -0500 Subject: [PATCH] fix NaN panics and eliminate redundant graph rebuilds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - All partial_cmp().unwrap() → unwrap_or(Ordering::Equal) to prevent NaN panics in sort operations across neuro.rs, graph.rs, similarity.rs - replay_queue_with_graph: accepts pre-built graph, avoids rebuilding in agent_prompt (was building 2-3x per prompt) - differentiate_hub_with_graph: same pattern for differentiation - Simplify double-reverse history iteration to slice indexing Co-Authored-By: ProofOfConcept --- src/graph.rs | 6 +++--- src/similarity.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/graph.rs b/src/graph.rs index a2c87e8..c7cfc6c 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -180,7 +180,7 @@ impl Graph { let n = degrees.len(); if n < 2 { return 0.0; } - degrees.sort_by(|a, b| a.partial_cmp(b).unwrap()); + degrees.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)); let mean = degrees.iter().sum::() / n as f64; if mean < 1e-10 { return 0.0; } @@ -448,7 +448,7 @@ fn label_propagation( // Adopt the label with most votes if let Some((&best_label, _)) = votes.iter() - .max_by(|a, b| a.1.partial_cmp(b.1).unwrap()) + .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal)) { let current = labels[key]; if best_label != current { @@ -670,7 +670,7 @@ Categories: core={core} tech={tech} gen={gen} obs={obs} task={task}", // Show history trend if we have enough data points if history.len() >= 3 { report.push_str("\n\nMetrics history (last 5):\n"); - for snap in history.iter().rev().take(5).collect::>().into_iter().rev() { + for snap in &history[history.len().saturating_sub(5)..] { report.push_str(&format!(" {} — σ={:.1} α={:.2} gini={:.3} cc={:.4} fit={:.3}\n", snap.date, snap.sigma, snap.alpha, snap.gini, snap.avg_cc, snap.avg_schema_fit)); } diff --git a/src/similarity.rs b/src/similarity.rs index 53fe742..38a399c 100644 --- a/src/similarity.rs +++ b/src/similarity.rs @@ -100,7 +100,7 @@ pub fn pairwise_similar( } } - results.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap()); + results.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal)); results }