use bb_ir::proto::onnx::NodeProto;
use bb_runtime::atomic::DispatchResult;
use bb_runtime::bus::OpError;
use bb_runtime::runtime::RuntimeResourceRef;
use bb_runtime::slot_value::SlotValue;
pub struct DedupGateRxOp;
pub const DOMAIN: &str = "ai.bytesandbrains.syscall";
pub const OP_TYPE: &str = "DedupGateRx";
pub fn invoke(
_node: &NodeProto,
inputs: &[(&str, &dyn SlotValue)],
ctx: &mut RuntimeResourceRef<'_>,
) -> Result<DispatchResult, OpError> {
let (_, value) = inputs.first().ok_or_else(|| OpError {
detail: "DedupGateRx requires one input".into(),
..Default::default()
})?;
let bytes = value.to_wire_bytes().map_err(|e| OpError {
kind: bb_runtime::bus::OpErrorKind::ExecutionFailed,
reason: "wire_encode_failed",
detail: format!("DedupGateRx: input wire encode failed: {e}"),
})?;
let hash = fnv1a_64(&bytes);
if ctx.net.dedup.record(hash) {
return Err(OpError {
detail: "DedupGateRx dropped envelope: reason=duplicate".into(),
..Default::default()
});
}
Ok(DispatchResult::Immediate(vec![(
"value".to_string(),
value.clone_boxed(),
)]))
}
fn fnv1a_64(bytes: &[u8]) -> u64 {
const FNV_OFFSET: u64 = 0xcbf2_9ce4_8422_2325;
const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;
let mut hash = FNV_OFFSET;
for b in bytes {
hash ^= *b as u64;
hash = hash.wrapping_mul(FNV_PRIME);
}
hash
}
use bb_runtime::registry::OpRegistration as _BbOpsSyscallReg;
inventory::submit! {
_BbOpsSyscallReg {
domain: DOMAIN,
op_type: OP_TYPE,
invoke,
kind: bb_runtime::registry::RegistrationKind::Syscall,
}
}