use bb_ir::proto::onnx::NodeProto;
use bb_runtime::atomic::DispatchResult;
use bb_runtime::bus::OpError;
use bb_runtime::framework::{BlockReason, Decision};
use bb_runtime::runtime::RuntimeResourceRef;
use bb_runtime::slot_value::SlotValue;
pub struct PeerHealthGateRxOp;
pub const DOMAIN: &str = "ai.bytesandbrains.syscall";
pub const OP_TYPE: &str = "PeerHealthGateRx";
pub fn invoke(
_node: &NodeProto,
inputs: &[(&str, &dyn SlotValue)],
ctx: &mut RuntimeResourceRef<'_>,
) -> Result<DispatchResult, OpError> {
let (_, value) = inputs.first().ok_or_else(|| OpError {
detail: "PeerHealthGateRx requires one input".into(),
..Default::default()
})?;
let Some(src_peer) = ctx.current.inbound.src_peer else {
return Err(OpError {
detail: "PeerHealthGateRx: no envelope source peer in runtime context".into(),
..Default::default()
});
};
match ctx.peers.governor.check_inbound(src_peer) {
Decision::Allow => Ok(DispatchResult::Immediate(vec![(
"value".to_string(),
value.clone_boxed(),
)])),
Decision::Deny(reason) => Err(OpError {
detail: format!(
"PeerHealthGateRx denied envelope from peer {src_peer:?}: reason={}",
reason_label(&reason),
),
..Default::default()
}),
}
}
pub fn reason_label(reason: &BlockReason) -> &'static str {
match reason {
BlockReason::Blocklisted => "blocklisted",
BlockReason::NotAllowlisted => "not_allowlisted",
BlockReason::Cooldown { .. } => "cooldown",
}
}
use bb_runtime::registry::OpRegistration as _BbOpsSyscallReg;
inventory::submit! {
_BbOpsSyscallReg {
domain: DOMAIN,
op_type: OP_TYPE,
invoke,
kind: bb_runtime::registry::RegistrationKind::Syscall,
}
}