Skip to main content

qualia_core_db/net/peer/cells/
isolation.rs

1//! Isolate exceptional compute from essential network reserves (E10.4).
2//!
3//! [`super::admit::llm_shares_ordinary_network_reserve`] is already false.
4//! An LLM request cannot consume bytes that must remain available for the
5//! network reserve. SentinelPass backing cannot be used as LLM backing.
6
7use super::admit::{CellProfile, MAX_ORDINARY_CELL};
8use super::host_owner::HostAdmission;
9use crate::governance::webizen::SENTINEL_PASS_BYTES;
10use crate::net::qdnf::errors::QdnfError;
11
12/// Gate: remaining host bytes after `network_reserved` must cover `llm_request`.
13///
14/// Does not admit a cell (no leaked slot). SentinelPass size cannot back LLM.
15pub fn admit_llm_cannot_starve_network(
16    host: &mut HostAdmission,
17    network_reserved: u64,
18    llm_request: u64,
19) -> Result<(), QdnfError> {
20    if llm_request > MAX_ORDINARY_CELL {
21        return Err(QdnfError::Capacity);
22    }
23    if llm_request == SENTINEL_PASS_BYTES as u64 {
24        return Err(QdnfError::Capacity);
25    }
26    let remaining = host.remaining_host_bytes();
27    let after_net = remaining.saturating_sub(network_reserved);
28    if after_net < llm_request {
29        return Err(QdnfError::BudgetExhausted);
30    }
31    Ok(())
32}
33
34/// SentinelPass cannot be used as LLM backing (profile form of E10.4).
35pub fn llm_backing_profile_allowed(profile: CellProfile) -> Result<(), QdnfError> {
36    match profile {
37        CellProfile::LlmException => Ok(()),
38        CellProfile::SentinelPass => Err(QdnfError::Capacity),
39        CellProfile::Ordinary | CellProfile::NetworkSmall => Err(QdnfError::Denied),
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    use crate::net::peer::cells::admit::llm_shares_ordinary_network_reserve;
47    use crate::net::peer::cells::host_owner::HostAdmission;
48    use crate::net::qdnf::errors::QdnfError;
49
50    #[test]
51    fn llm_does_not_share_and_cannot_take_network_reserve() {
52        assert!(!llm_shares_ordinary_network_reserve());
53        let mut host = HostAdmission::new(100).unwrap();
54        admit_llm_cannot_starve_network(&mut host, 40, 60).expect("60 fits after 40 reserve");
55        assert_eq!(host.remaining_host_bytes(), 100);
56        assert_eq!(
57            admit_llm_cannot_starve_network(&mut host, 40, 61),
58            Err(QdnfError::BudgetExhausted)
59        );
60        assert_eq!(host.occupied_cells(), 0);
61    }
62
63    #[test]
64    fn sentinel_pass_cannot_back_llm() {
65        let mut host = HostAdmission::new(MAX_ORDINARY_CELL).unwrap();
66        assert_eq!(
67            admit_llm_cannot_starve_network(&mut host, 0, SENTINEL_PASS_BYTES as u64),
68            Err(QdnfError::Capacity)
69        );
70        assert_eq!(
71            llm_backing_profile_allowed(CellProfile::SentinelPass),
72            Err(QdnfError::Capacity)
73        );
74        llm_backing_profile_allowed(CellProfile::LlmException).unwrap();
75    }
76
77    #[test]
78    fn occupied_network_reduces_llm_headroom() {
79        let mut host = HostAdmission::new(100).unwrap();
80        let net = host
81            .admit_cell(CellProfile::NetworkSmall, 40)
82            .expect("network cell");
83        assert_eq!(
84            admit_llm_cannot_starve_network(&mut host, 40, 21),
85            Err(QdnfError::BudgetExhausted)
86        );
87        admit_llm_cannot_starve_network(&mut host, 40, 20).unwrap();
88        host.release_cell(net).unwrap();
89    }
90}