use std::collections::HashMap;
use crate::{EvaluationResult, LayerResult, ScanExecution, SecurityScanResult};
pub fn degraded_fallback_confidence(confidence: f64) -> f64 {
(confidence * 0.5).min(0.5)
}
pub(crate) fn mark_decision_degraded(result: &mut SecurityScanResult, reason: &str) {
if let Some(decision) = &mut result.decision {
decision.final_result.class_name = result.class_name.clone();
decision.final_result.confidence = result.confidence;
decision.terminality.completion = "degraded".to_string();
decision.terminality.degraded = true;
decision.terminality.degradation_reason = Some(reason.to_string());
}
}
pub fn l3_pending_layer(result: &EvaluationResult, execution: &ScanExecution) -> LayerResult {
LayerResult {
level: "L3".to_string(),
layer_type: "l3_pending".to_string(),
class_name: result.class_name.clone(),
confidence: 0.0,
matched: false,
duration_ms: 0.0,
thresholds: HashMap::new(),
details: HashMap::from([
("queued".to_string(), serde_json::json!(true)),
(
"fallback_level".to_string(),
serde_json::json!(result.level.clone()),
),
(
"fallback_class".to_string(),
serde_json::json!(result.class_name.clone()),
),
(
"fallback_confidence".to_string(),
serde_json::json!(result.confidence),
),
(
"batch_mode".to_string(),
serde_json::json!(execution.onnx_batch_mode().as_str()),
),
(
"backend".to_string(),
serde_json::json!(execution.backend().as_str()),
),
]),
}
}
pub fn has_l3_pending(result: &SecurityScanResult) -> bool {
result
.layers
.iter()
.any(|layer| layer.level == "L3" && layer.layer_type == "l3_pending")
}
pub fn degraded_timeout_result(
mut result: SecurityScanResult,
queued_ms: f64,
ttl_ms: u64,
degraded_factor: f64,
) -> SecurityScanResult {
result.confidence = (result.confidence * degraded_factor).clamp(0.0, 1.0);
for layer in &mut result.layers {
if layer.level == result.level && layer.layer_type != "l3_pending" {
layer.confidence = result.confidence;
layer.matched = true;
layer
.details
.insert("degraded".to_string(), serde_json::json!(true));
layer.details.insert(
"degraded_reason".to_string(),
serde_json::json!("l3_timeout"),
);
}
}
mark_decision_degraded(&mut result, "l3_timeout");
result.layers.push(LayerResult {
level: "L3".to_string(),
layer_type: "degraded_timeout".to_string(),
class_name: "timeout".to_string(),
confidence: 0.0,
matched: false,
duration_ms: 0.0,
thresholds: HashMap::new(),
details: HashMap::from([
(
"fallback_due_to_timeout".to_string(),
serde_json::json!(true),
),
("degraded".to_string(), serde_json::json!(true)),
("queued_ms".to_string(), serde_json::json!(queued_ms)),
("ttl_ms".to_string(), serde_json::json!(ttl_ms)),
(
"degraded_factor".to_string(),
serde_json::json!(degraded_factor),
),
]),
});
result.duration_ms = result.layers.iter().map(|layer| layer.duration_ms).sum();
result
}
pub fn degraded_error_result(
mut result: SecurityScanResult,
queued_ms: f64,
ttl_ms: u64,
degraded_factor: f64,
error: String,
) -> SecurityScanResult {
result.confidence = (result.confidence * degraded_factor).clamp(0.0, 1.0);
for layer in &mut result.layers {
if layer.level == result.level && layer.layer_type != "l3_pending" {
layer.confidence = result.confidence;
layer.matched = true;
layer
.details
.insert("degraded".to_string(), serde_json::json!(true));
layer
.details
.insert("degraded_reason".to_string(), serde_json::json!("l3_error"));
}
}
mark_decision_degraded(&mut result, "l3_error");
result.layers.push(LayerResult {
level: "L3".to_string(),
layer_type: "degraded_error".to_string(),
class_name: "error".to_string(),
confidence: 0.0,
matched: false,
duration_ms: 0.0,
thresholds: HashMap::new(),
details: HashMap::from([
("fallback_due_to_error".to_string(), serde_json::json!(true)),
("degraded".to_string(), serde_json::json!(true)),
("error".to_string(), serde_json::json!(error)),
("queued_ms".to_string(), serde_json::json!(queued_ms)),
("ttl_ms".to_string(), serde_json::json!(ttl_ms)),
(
"degraded_factor".to_string(),
serde_json::json!(degraded_factor),
),
]),
});
result.duration_ms = result.layers.iter().map(|layer| layer.duration_ms).sum();
result
}
pub fn l3_metadata_layer(
class_name: &str,
model: &str,
confidence: f64,
duration_ms: f64,
) -> LayerResult {
LayerResult {
level: "L3".to_string(),
layer_type: "onnx".to_string(),
class_name: class_name.to_string(),
confidence,
matched: true,
duration_ms,
thresholds: HashMap::new(),
details: HashMap::from([("model".to_string(), serde_json::json!(model))]),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
DecisionCandidate, DecisionEnvelope, DecisionProvenance, DecisionRecommendation,
DecisionResult, DecisionTerminality,
};
#[test]
fn degraded_timeout_marks_decision_terminality_degraded() {
let result = SecurityScanResult {
category: "threat".to_string(),
class_name: "benign".to_string(),
confidence: 0.8,
level: "L2".to_string(),
model: "unified-v3-threat".to_string(),
duration_ms: 1.0,
layers: vec![LayerResult {
level: "L2".to_string(),
layer_type: "ntdb_l2".to_string(),
class_name: "benign".to_string(),
confidence: 0.8,
matched: true,
duration_ms: 1.0,
thresholds: HashMap::new(),
details: HashMap::new(),
}],
internal_l2_chunk_outputs: Vec::new(),
evidence_spans: Vec::new(),
label_scores: Vec::new(),
decision: Some(DecisionEnvelope {
schema_version: "ark.decision.v1".to_string(),
final_result: DecisionResult {
class_name: "benign".to_string(),
confidence: 0.8,
source: "l2".to_string(),
},
decision_candidate: Some(DecisionCandidate {
source: "l2".to_string(),
class_name: "instruction_override".to_string(),
confidence: 0.9,
acceptance_threshold: 0.86471,
accepted: true,
evidence: None,
chunk_evidence: None,
}),
recommendation: DecisionRecommendation {
accepted: true,
final_arbitration: "l2".to_string(),
operating_point: "best_f1".to_string(),
acceptance_threshold: Some(0.86471),
},
candidates: Vec::new(),
terminality: DecisionTerminality {
completion: "complete".to_string(),
degraded: false,
degradation_reason: None,
},
provenance: DecisionProvenance {
ark_version: "test".to_string(),
schema_version: "ark.decision.v1".to_string(),
model: "unified-v3-threat".to_string(),
},
}),
};
let degraded = degraded_timeout_result(result, 100.0, 50, 0.75);
let terminality = °raded.decision.unwrap().terminality;
assert_eq!(terminality.completion, "degraded");
assert!(terminality.degraded);
assert_eq!(
terminality.degradation_reason.as_deref(),
Some("l3_timeout")
);
}
}