ifc_lite_geometry/router/diagnostics/
budget.rs

1// SPDX-License-Identifier: MPL-2.0
2//! Work-budget refusals are safety-policy diagnostics, never unsupported items.
3use super::GeometryRouter;
4use ifc_lite_core::{slot::IfcRoot, DecodedEntity};
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
8#[serde(rename_all = "camelCase")]
9#[non_exhaustive]
10pub struct BudgetRefusal {
11    pub budget: String,
12    pub limit: u32,
13    /// Demand including the refused attempt/list, rather than only admitted work.
14    pub reached: u64,
15    pub source_id: u32,
16    pub product_id: Option<u32>,
17    pub product_type: Option<String>,
18    #[serde(rename = "GlobalId")]
19    pub global_id: Option<String>,
20}
21
22/// Owned only after refusal, never carried through successful execution.
23#[derive(Debug, Clone)]
24pub(crate) struct BudgetProductIdentity {
25    id: u32,
26    ifc_type: ifc_lite_core::IfcType,
27    global_id: Option<String>,
28}
29impl BudgetProductIdentity {
30    #[cold]
31    pub(crate) fn from_product(product: &DecodedEntity) -> Self {
32        Self { id: product.id, ifc_type: product.ifc_type.clone(),
33            global_id: product.get(IfcRoot::GLOBAL_ID.index())
34                .and_then(|a| a.as_string()).map(str::to_owned) }
35    }
36}
37
38/// Maximum retained detail records per diagnostic drain. Counts remain complete.
39pub const BUDGET_REFUSAL_DETAILS_LIMIT: usize = 16;
40
41#[derive(Default)]
42pub(super) struct BudgetRefusalLog {
43    entries: Vec<BudgetRefusal>,
44    seen: rustc_hash::FxHashSet<(Option<u32>, u32, String, u32, u64)>,
45}
46
47impl GeometryRouter {
48    #[cold]
49    #[inline(never)]
50    pub(crate) fn record_budget_refusal(&self, source_id: u32, budget: &str, limit: u32,
51        reached: u64, product: Option<&DecodedEntity>) {
52        let product = product.map(BudgetProductIdentity::from_product);
53        self.record_budget_refusal_identity(source_id, budget, limit, reached, product.as_ref());
54    }
55
56    #[cold]
57    #[inline(never)]
58    pub(crate) fn record_budget_refusal_identity(&self, source_id: u32, budget: &str, limit: u32,
59        reached: u64, product: Option<&BudgetProductIdentity>) {
60        let mut state = self.unsupported.borrow_mut();
61        let log = state.budget_refusals.get_or_insert_with(Box::default);
62        if !log.seen.insert((product.map(|p| p.id), source_id, budget.into(), limit, reached)) {
63            return;
64        }
65        if log.entries.len() < BUDGET_REFUSAL_DETAILS_LIMIT {
66            log.entries.push(BudgetRefusal {
67                budget: budget.into(), limit, reached, source_id,
68                product_id: product.map(|p| p.id),
69                product_type: product.map(|p| p.ifc_type.to_string()),
70                global_id: product.and_then(|p| p.global_id.clone()),
71            });
72        }
73    }
74
75    /// Drain bounded details and the complete distinct-refusal count together.
76    /// The dedup set lives until this drain, including across repeated walks.
77    pub fn take_budget_refusal_report(&self) -> (u64, Vec<BudgetRefusal>) {
78        self.unsupported.borrow_mut().budget_refusals.take()
79            .map_or_else(|| (0, Vec::new()), |log| (log.seen.len() as u64, log.entries))
80    }
81
82    /// Drain the bounded detail list. Use `take_budget_refusal_report` for totals.
83    pub fn take_budget_refusals(&self) -> Vec<BudgetRefusal> {
84        self.take_budget_refusal_report().1
85    }
86}