Skip to content

Commit 63d7f0c

Browse files
author
SuperInstance DocBot
committed
Add dependency on fastrand and fix borrow checker error
1 parent 377fed7 commit 63d7f0c

9 files changed

Lines changed: 3392 additions & 0 deletions

File tree

hybrid-bridge/Cargo.toml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "hybrid-bridge"
3+
version = "0.1.0"
4+
edition = "2021"
5+
license.workspace = true
6+
repository.workspace = true
7+
homepage.workspace = true
8+
description = "Hybrid Manifold communication backbone — connects Matrix Engine, Room Agents, and Veto Engine"
9+
10+
[dependencies]
11+
pincher-core = { path = "../pincher-core" }
12+
ternary-types = { workspace = true }
13+
ndarray = { version = "0.16", features = ["serde"] }
14+
serde = { workspace = true }
15+
serde_json.workspace = true
16+
tokio = { workspace = true }
17+
thiserror = { workspace = true }
18+
tracing = { workspace = true }
19+
arrow = { version = "53", features = ["ipc"] }
20+
async-trait = "0.1"
21+
chrono = { workspace = true }
22+
uuid = { version = "1", features = ["v4", "serde"] }
23+
fastrand = "2.4.1"
24+
25+
[lib]
26+
name = "hybrid_bridge"
27+
path = "src/lib.rs"

hybrid-bridge/src/bridge.rs

Lines changed: 643 additions & 0 deletions
Large diffs are not rendered by default.

hybrid-bridge/src/chaos.rs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
//! Chaos testing utilities for the hybrid bridge.
2+
//!
3+
//! These utilities deliberately inject pathological data into the feature
4+
//! tensor to verify that downstream components (the veto engine, matrix
5+
//! masking logic, safe-mode protocols) handle them gracefully.
6+
7+
/// The number of non-finite cells that triggers safe mode.
8+
pub const SAFE_MODE_THRESHOLD: usize = 1;
9+
10+
/// Result of a chaos injection + recovery cycle.
11+
#[derive(Debug, Clone)]
12+
pub struct ChaosTestResult {
13+
/// Number of non-finite values injected.
14+
pub injected: usize,
15+
/// Number of non-finite values detected.
16+
pub detected: usize,
17+
/// Number of non-finite values masked.
18+
pub masked: usize,
19+
/// Whether safe mode would have been triggered.
20+
pub safe_mode_triggered: bool,
21+
/// Any failures encountered during the cycle.
22+
pub failures: Vec<String>,
23+
}
24+
25+
/// Inject NaN cells into a tensor at random positions and test recovery.
26+
pub fn inject_nan_random(
27+
tensor: &mut ndarray::Array3<f32>,
28+
count: usize,
29+
) -> Vec<(usize, usize, usize)> {
30+
let mut rng = fastrand::Rng::new();
31+
let mut coords = Vec::with_capacity(count);
32+
let shape = tensor.shape();
33+
for _ in 0..count {
34+
let s = rng.usize(0..shape[0]);
35+
let f = rng.usize(0..shape[1]);
36+
let t = rng.usize(0..shape[2]);
37+
tensor[[s, f, t]] = f32::NAN;
38+
coords.push((s, f, t));
39+
}
40+
coords
41+
}
42+
43+
/// Inject Inf cells into a tensor at random positions.
44+
pub fn inject_inf_random(
45+
tensor: &mut ndarray::Array3<f32>,
46+
count: usize,
47+
) -> Vec<(usize, usize, usize)> {
48+
let mut rng = fastrand::Rng::new();
49+
let mut coords = Vec::with_capacity(count);
50+
let shape = tensor.shape();
51+
for _ in 0..count {
52+
let s = rng.usize(0..shape[0]);
53+
let f = rng.usize(0..shape[1]);
54+
let t = rng.usize(0..shape[2]);
55+
tensor[[s, f, t]] = f32::INFINITY;
56+
coords.push((s, f, t));
57+
}
58+
coords
59+
}
60+
61+
/// Run a full chaos cycle: inject → detect → mask → verify.
62+
pub fn run_chaos_cycle(tensor: &mut ndarray::Array3<f32>, n_inject: usize) -> ChaosTestResult {
63+
let mut failures = Vec::new();
64+
65+
// 1. Inject NaNs
66+
let _coords = inject_nan_random(tensor, n_inject);
67+
68+
// 2. Detect using the crate-level detection function
69+
let detected = crate::types::detect_non_finite(tensor);
70+
let n_detected = detected.len();
71+
let safe_mode = n_detected >= SAFE_MODE_THRESHOLD;
72+
73+
if n_detected == 0 {
74+
failures.push("Expected non-finite detection but found none".to_string());
75+
}
76+
77+
// 3. Mask
78+
let n_masked = crate::types::mask_non_finite(tensor);
79+
80+
// 4. Verify
81+
let remaining = crate::types::detect_non_finite(tensor);
82+
if !remaining.is_empty() {
83+
failures.push(format!(
84+
"{} non-finite values remained after masking",
85+
remaining.len()
86+
));
87+
}
88+
89+
// 5. Verify the tensor can be meaningfully used
90+
let total: f32 = tensor.iter().copied().sum();
91+
if !total.is_finite() {
92+
failures.push(format!("Tensor sum is non-finite after masking: {}", total));
93+
}
94+
95+
ChaosTestResult {
96+
injected: n_inject,
97+
detected: n_detected,
98+
masked: n_masked,
99+
safe_mode_triggered: safe_mode,
100+
failures,
101+
}
102+
}
103+
104+
#[cfg(test)]
105+
mod tests {
106+
use super::*;
107+
use crate::types::{detect_non_finite, mask_non_finite};
108+
use ndarray::Array3;
109+
110+
#[test]
111+
fn test_inject_nan_random_injects_expected_count() {
112+
let mut tensor = Array3::<f32>::zeros((5, 10, 20));
113+
let coords = inject_nan_random(&mut tensor, 7);
114+
assert_eq!(coords.len(), 7);
115+
for &(s, f, t) in &coords {
116+
assert!(tensor[[s, f, t]].is_nan());
117+
}
118+
}
119+
120+
#[test]
121+
fn test_run_chaos_cycle_clean() {
122+
let mut tensor = Array3::<f32>::zeros((3, 4, 5));
123+
let result = run_chaos_cycle(&mut tensor, 5);
124+
assert!(result.failures.is_empty(), "Failures: {:?}", result.failures);
125+
assert_eq!(result.detected, result.masked);
126+
assert!(result.safe_mode_triggered);
127+
}
128+
129+
#[test]
130+
fn test_run_chaos_cycle_large_injection() {
131+
let mut tensor = Array3::<f32>::ones((10, 20, 30));
132+
let result = run_chaos_cycle(&mut tensor, 50);
133+
assert!(result.failures.is_empty(), "Failures: {:?}", result.failures);
134+
assert_eq!(result.detected, result.masked);
135+
assert_eq!(result.injected, 50);
136+
}
137+
138+
#[test]
139+
fn test_run_chaos_cycle_mixed_nan_inf() {
140+
let mut tensor = Array3::<f32>::zeros((4, 4, 4));
141+
tensor[[0, 0, 0]] = f32::NAN;
142+
tensor[[1, 1, 1]] = f32::INFINITY;
143+
tensor[[2, 2, 2]] = f32::NEG_INFINITY;
144+
145+
assert_eq!(detect_non_finite(&tensor).len(), 3);
146+
assert_eq!(mask_non_finite(&mut tensor), 3);
147+
assert!(detect_non_finite(&tensor).is_empty());
148+
}
149+
}

0 commit comments

Comments
 (0)