patronus-ark 0.1.17

Hybrid Rust security scanners for prompt injection, DLP, PII, and agentic tool risks.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// SPDX-License-Identifier: GPL-3.0-only
//! Package-v4 joint mmBERT L2 runtime and promoter.

use std::{
    collections::{HashMap, HashSet},
    path::Path,
    sync::Arc,
};

use ort::{
    session::{Session, SessionInputValue},
    value::Tensor,
};
use rayon::prelude::*;

use crate::NtdbOperatingPoint;

use super::{
    lightgbm::LightGbmModel,
    manifest::{JointV3Manifest, TaskManifest},
    ntdb_error,
    package::{
        JointV3CandidatePolicy, JointV3DecisionContext, NtdbChunkInference, PreparedDocument,
        ScoreOutput,
    },
    session::load_single_thread_session,
    NtdbResult,
};

pub(super) struct JointV3Runtime {
    neural: Session,
    neural_inputs: Vec<String>,
    heads: Vec<JointV3HeadRuntime>,
    promoter_feature_dim: usize,
    promoter: JointV3PromoterRuntime,
    document_decision: super::manifest::JointV3DocumentDecisionManifest,
    attack_threshold: Option<f32>,
}

struct JointV3HeadRuntime {
    id: String,
    class_count: usize,
    lightgbm: LightGbmModel,
}

struct JointV3PromoterRuntime {
    models: HashMap<String, LightGbmModel>,
    utility: ActionablePolicy,
    best_promote: ActionablePolicy,
}

#[derive(Clone)]
struct ActionablePolicy {
    gate: String,
    threshold: f32,
    aggregation: String,
    document_risk_margin_threshold: f32,
}

pub(super) struct JointChunkInference {
    pub(super) probabilities: Vec<f32>,
    pub(super) promote_score: f32,
    pub(super) promote_threshold: f32,
}

struct JointPrediction {
    probabilities: Vec<f32>,
    promoter_features: Vec<f32>,
}

impl JointV3Runtime {
    pub(super) fn load(package_dir: &Path, manifest: &JointV3Manifest) -> NtdbResult<Self> {
        let mut head_by_id = manifest
            .heads
            .iter()
            .map(|head| (head.id.as_str(), head))
            .collect::<HashMap<_, _>>();
        let heads = manifest
            .neural_stack
            .head_order
            .iter()
            .zip(&manifest.neural_stack.head_class_counts)
            .map(|(id, class_count)| {
                let head = head_by_id
                    .remove(id.as_str())
                    .ok_or_else(|| ntdb_error(format!("NTDB package v4 is missing head {id}")))?;
                let lightgbm = LightGbmModel::load(package_dir.join(&head.frozen_lightgbm))?;
                if lightgbm.feature_count() != 384 {
                    return Err(ntdb_error(format!(
                        "NTDB package v4 head {id} LightGBM expects {} features, expected 384",
                        lightgbm.feature_count()
                    )));
                }
                Ok(JointV3HeadRuntime {
                    id: id.clone(),
                    class_count: *class_count,
                    lightgbm,
                })
            })
            .collect::<NtdbResult<Vec<_>>>()?;
        let promoter = JointV3PromoterRuntime::load(package_dir, manifest)?;
        Ok(Self {
            neural: load_single_thread_session(package_dir.join(&manifest.neural_stack.onnx))?,
            neural_inputs: manifest.neural_stack.input_names.clone(),
            heads,
            promoter_feature_dim: manifest.neural_stack.promoter_feature_dim,
            promoter,
            document_decision: manifest.document_decision.clone(),
            attack_threshold: None,
        })
    }

    pub(super) fn infer_chunks(
        &mut self,
        task: &TaskManifest,
        prepared: &PreparedDocument,
        document_chunk_count: usize,
        operating_point: NtdbOperatingPoint,
    ) -> NtdbResult<Vec<JointChunkInference>> {
        let predictions = self.predict(prepared, prepared.chunks.len(), 1)?;
        let operating_point =
            short_injection_operating_point(&task.kind, operating_point, document_chunk_count);
        predictions
            .into_iter()
            .map(|prediction| {
                let (promote_score, promote_threshold) = self.promoter.score(
                    &prediction.probabilities,
                    &prediction.promoter_features,
                    operating_point,
                )?;
                Ok(JointChunkInference {
                    probabilities: prediction.probabilities,
                    promote_score,
                    promote_threshold,
                })
            })
            .collect()
    }

    pub(super) fn aggregate_chunk_inferences(
        &self,
        task: &TaskManifest,
        chunks: &[NtdbChunkInference],
        operating_point: NtdbOperatingPoint,
    ) -> NtdbResult<ScoreOutput> {
        let chunks = chunks
            .iter()
            .map(|chunk| JointChunkInference {
                probabilities: chunk.class_probabilities.clone(),
                promote_score: chunk.promote_score,
                promote_threshold: chunk.promote_threshold,
            })
            .collect::<Vec<_>>();
        self.output_from_inferences(task, chunks, operating_point)
    }

    fn output_from_inferences(
        &self,
        task: &TaskManifest,
        chunks: Vec<JointChunkInference>,
        operating_point: NtdbOperatingPoint,
    ) -> NtdbResult<ScoreOutput> {
        let operating_point =
            short_injection_operating_point(&task.kind, operating_point, chunks.len());
        let promote_threshold = chunks
            .first()
            .map(|chunk| chunk.promote_threshold)
            .unwrap_or(f32::INFINITY);
        let promote_score = chunks
            .iter()
            .map(|chunk| chunk.promote_score)
            .fold(f32::NEG_INFINITY, f32::max);
        let context = Arc::new(self.decision_context(task, operating_point)?);
        let chunk_count = chunks.len();
        let (chunk_class_probabilities, chunk_promote_scores): (Vec<_>, Vec<_>) = chunks
            .into_iter()
            .map(|chunk| (chunk.probabilities, Some(chunk.promote_score)))
            .unzip();
        let class_scores =
            aggregate_probabilities(&chunk_class_probabilities, &context.l2.aggregation)?;
        let predicted_index = argmax(&class_scores)?;
        Ok(ScoreOutput {
            aggregator_id: "joint_v3".to_string(),
            task: task.kind.clone(),
            labels: task.labels.clone(),
            predicted_label: task.labels[predicted_index].clone(),
            predicted_index,
            class_scores,
            class_logits: Vec::new(),
            chunks: chunk_count,
            attack_threshold: self.attack_threshold,
            promote_score: Some(promote_score),
            promote_threshold: Some(promote_threshold),
            chunk_promote_scores,
            l3_candidate_spans: Vec::new(),
            l3_candidates: Vec::new(),
            l2_chunk_outputs: Vec::new(),
            chunk_class_probabilities,
            joint_v3_decision: Some(context),
        })
    }

    fn output_from_chunks(
        &self,
        task: &TaskManifest,
        chunks: Vec<JointPrediction>,
        operating_point: NtdbOperatingPoint,
    ) -> NtdbResult<ScoreOutput> {
        let operating_point =
            short_injection_operating_point(&task.kind, operating_point, chunks.len());
        let inferences = chunks
            .into_iter()
            .map(|chunk| {
                let (promote_score, promote_threshold) = self.promoter.score(
                    &chunk.probabilities,
                    &chunk.promoter_features,
                    operating_point,
                )?;
                Ok(JointChunkInference {
                    probabilities: chunk.probabilities,
                    promote_score,
                    promote_threshold,
                })
            })
            .collect::<NtdbResult<Vec<_>>>()?;
        self.output_from_inferences(task, inferences, operating_point)
    }

    pub(super) fn score_batch(
        &mut self,
        task: &TaskManifest,
        prepared: &[std::sync::Arc<PreparedDocument>],
        operating_point: NtdbOperatingPoint,
    ) -> NtdbResult<Vec<ScoreOutput>> {
        if prepared.is_empty() {
            return Ok(Vec::new());
        }
        let counts = prepared
            .iter()
            .map(|document| document.chunks.len())
            .collect::<Vec<_>>();
        let embeddings = prepared
            .iter()
            .flat_map(|document| document.raw_embeddings.iter().copied())
            .collect::<Vec<_>>();
        let total_chunks = counts.iter().sum::<usize>();
        let predictions =
            self.predict_tensors(embeddings, vec![true; total_chunks], total_chunks, 1)?;
        let mut predictions = predictions.into_iter();
        counts
            .into_iter()
            .map(|count| {
                self.output_from_chunks(
                    task,
                    predictions.by_ref().take(count).collect(),
                    operating_point,
                )
            })
            .collect()
    }

    fn predict(
        &mut self,
        prepared: &PreparedDocument,
        batch: usize,
        chunks_per_batch: usize,
    ) -> NtdbResult<Vec<JointPrediction>> {
        let chunk_count = prepared.chunks.len();
        if batch.saturating_mul(chunks_per_batch) != chunk_count {
            return Err(ntdb_error("invalid NTDB package v4 neural batch shape"));
        }
        self.predict_tensors(
            prepared.raw_embeddings.clone(),
            vec![true; chunk_count],
            batch,
            chunks_per_batch,
        )
    }

    fn predict_tensors(
        &mut self,
        raw_embeddings: Vec<f32>,
        mask: Vec<bool>,
        batch: usize,
        chunks_per_batch: usize,
    ) -> NtdbResult<Vec<JointPrediction>> {
        let chunk_count = batch.saturating_mul(chunks_per_batch);
        let head_probabilities = self
            .heads
            .par_iter()
            .map(|head| {
                let mut values = Vec::with_capacity(chunk_count * head.class_count);
                for (chunk_index, embedding) in
                    raw_embeddings.as_chunks::<384>().0.iter().enumerate()
                {
                    if !mask[chunk_index] {
                        values.extend(std::iter::repeat_n(0.0, head.class_count));
                        continue;
                    }
                    let row = head.lightgbm.predict_probabilities(embedding);
                    if row.len() != head.class_count {
                        return Err(ntdb_error(format!(
                            "NTDB package v4 head {} returned {} classes, expected {}",
                            head.id,
                            row.len(),
                            head.class_count
                        )));
                    }
                    values.extend(row);
                }
                Ok(values)
            })
            .collect::<NtdbResult<Vec<_>>>()?;

        let mut inputs =
            Vec::<(String, SessionInputValue<'_>)>::with_capacity(self.heads.len() + 2);
        inputs.push((
            "raw_embeddings".to_string(),
            Tensor::from_array(([batch, chunks_per_batch, 384], raw_embeddings))?.into(),
        ));
        inputs.push((
            "attention_mask".to_string(),
            Tensor::from_array(([batch, chunks_per_batch], mask))?.into(),
        ));
        for (index, (head, values)) in self.heads.iter().zip(head_probabilities).enumerate() {
            let input_name = self
                .neural_inputs
                .get(index + 2)
                .cloned()
                .unwrap_or_else(|| format!("{}_lgb_probabilities", head.id));
            inputs.push((
                input_name,
                Tensor::from_array(([batch, chunks_per_batch, head.class_count], values))?.into(),
            ));
        }
        let outputs = self.neural.run(inputs)?;
        let probabilities = tensor_values(&outputs, "l2_probabilities")?;
        let logits = tensor_values(&outputs, "l2_logits")?;
        let promoter_features = tensor_values(&outputs, "promoter_features")?;
        let class_count = probabilities.len() / batch;
        if class_count == 0
            || probabilities.len() != batch * class_count
            || logits.len() != batch * class_count
            || promoter_features.len() != batch * self.promoter_feature_dim
        {
            return Err(ntdb_error(format!(
                "NTDB package v4 neural outputs do not match batch={batch}, classes={class_count}, promoter_dim={}",
                self.promoter_feature_dim
            )));
        }
        Ok((0..batch)
            .map(|index| JointPrediction {
                probabilities: probabilities[index * class_count..(index + 1) * class_count]
                    .to_vec(),
                promoter_features: promoter_features
                    [index * self.promoter_feature_dim..(index + 1) * self.promoter_feature_dim]
                    .to_vec(),
            })
            .collect())
    }

    fn decision_context(
        &self,
        task: &TaskManifest,
        operating_point: NtdbOperatingPoint,
    ) -> NtdbResult<JointV3DecisionContext> {
        let profile = match operating_point {
            NtdbOperatingPoint::BestFprInF1 => "best_fpr_in_f1",
            NtdbOperatingPoint::BestFnrInF1 => "best_fnr_in_f1",
            _ => self.document_decision.default_operating_point.as_str(),
        };
        let l2 = document_policy(&self.document_decision, "l2_only", profile)?;
        let l3 = document_policy(&self.document_decision, "l3_only", profile)?;
        let union = if matches!(
            operating_point,
            NtdbOperatingPoint::BestPromote
                | NtdbOperatingPoint::BestF1
                | NtdbOperatingPoint::BestLatencyInF1
        ) {
            let point = self.promoter.policy(operating_point);
            JointV3CandidatePolicy {
                aggregation: point.aggregation.clone(),
                risk_margin_threshold: point.document_risk_margin_threshold,
            }
        } else {
            document_policy(&self.document_decision, "union_l2_l3", profile)?
        };
        let default_class_index = task
            .no_risk_class
            .as_ref()
            .and_then(|class| task.labels.iter().position(|label| label == class))
            .unwrap_or(0);
        Ok(JointV3DecisionContext {
            labels: task.labels.clone(),
            default_class_index,
            l2,
            l3,
            union,
        })
    }
}

fn short_injection_operating_point(
    task_kind: &str,
    operating_point: NtdbOperatingPoint,
    chunk_count: usize,
) -> NtdbOperatingPoint {
    if operating_point == NtdbOperatingPoint::ArkApiShortInjectionUtility {
        if task_kind == "injection" && chunk_count <= 2 {
            // JointV3PromoterRuntime maps BestF1 to the manifest's utility promoter.
            return NtdbOperatingPoint::BestF1;
        }
        return NtdbOperatingPoint::BestPromote;
    }
    operating_point
}

fn document_policy(
    decision: &super::manifest::JointV3DocumentDecisionManifest,
    mode: &str,
    profile: &str,
) -> NtdbResult<JointV3CandidatePolicy> {
    let mode = decision
        .modes
        .get(mode)
        .ok_or_else(|| ntdb_error(format!("missing Package-v4 document mode {mode}")))?;
    let point = mode.operating_points.get(profile).ok_or_else(|| {
        ntdb_error(format!(
            "missing Package-v4 document operating point {profile}"
        ))
    })?;
    Ok(JointV3CandidatePolicy {
        aggregation: mode.aggregation.clone(),
        risk_margin_threshold: point.threshold,
    })
}

pub(crate) fn aggregate_probabilities(values: &[Vec<f32>], method: &str) -> NtdbResult<Vec<f32>> {
    let classes = values
        .first()
        .map(Vec::len)
        .ok_or_else(|| ntdb_error("cannot aggregate zero chunks"))?;
    if classes == 0 || values.iter().any(|row| row.len() != classes) {
        return Err(ntdb_error("chunk probability shape mismatch"));
    }
    let mut result = vec![0.0_f32; classes];
    match method {
        "max" => {
            result.fill(f32::NEG_INFINITY);
            for row in values {
                for (target, value) in result.iter_mut().zip(row) {
                    *target = target.max(*value);
                }
            }
        }
        "mean" => {
            for row in values {
                for (target, value) in result.iter_mut().zip(row) {
                    *target += *value;
                }
            }
            for value in &mut result {
                *value /= values.len() as f32;
            }
        }
        "smoothmax" => {
            for class in 0..classes {
                let maximum = values
                    .iter()
                    .map(|row| row[class])
                    .fold(f32::NEG_INFINITY, f32::max);
                let mut weighted = 0.0_f32;
                let mut weight_sum = 0.0_f32;
                for row in values {
                    let weight = (10.0 * (row[class] - maximum)).exp();
                    weighted += row[class] * weight;
                    weight_sum += weight;
                }
                result[class] = weighted / weight_sum.max(1e-12);
            }
        }
        other => {
            return Err(ntdb_error(format!(
                "unsupported Package-v4 aggregation: {other}"
            )))
        }
    }
    Ok(result)
}

impl JointV3PromoterRuntime {
    fn load(package_dir: &Path, manifest: &JointV3Manifest) -> NtdbResult<Self> {
        if manifest.promoter.scope != "per_chunk" {
            return Err(ntdb_error(
                "NTDB package v4 promoter must have per_chunk scope",
            ));
        }
        if manifest.promoter.implementation != "LightGBM" {
            return Err(ntdb_error("NTDB package v4 promoter must use LightGBM"));
        }
        let utility = manifest
            .promoter
            .operating_points
            .get("utility_promote")
            .map(ActionablePolicy::from)
            .ok_or_else(|| ntdb_error("NTDB package v4 has no utility_promote operating point"))?;
        let best_promote = manifest
            .promoter
            .operating_points
            .get("best_promote")
            .map(ActionablePolicy::from)
            .ok_or_else(|| ntdb_error("NTDB package v4 has no best_promote operating point"))?;
        let required = [&utility, &best_promote]
            .into_iter()
            .flat_map(required_models)
            .collect::<HashSet<_>>();
        if required.is_empty() {
            return Err(ntdb_error(
                "NTDB package v4 references no supported promoter model",
            ));
        }
        let models = required
            .into_iter()
            .map(|name| Ok((name.to_string(), load_gate(package_dir, manifest, name)?)))
            .collect::<NtdbResult<HashMap<_, _>>>()?;
        Ok(Self {
            models,
            utility,
            best_promote,
        })
    }

    fn policy(&self, operating_point: NtdbOperatingPoint) -> &ActionablePolicy {
        if matches!(
            operating_point,
            NtdbOperatingPoint::BestPromote | NtdbOperatingPoint::ArkApiShortInjectionUtility
        ) {
            &self.best_promote
        } else {
            &self.utility
        }
    }

    fn score(
        &self,
        _l2_probabilities: &[f32],
        features: &[f32],
        operating_point: NtdbOperatingPoint,
    ) -> NtdbResult<(f32, f32)> {
        let policy = self.policy(operating_point);
        let model = |name: &str| {
            self.models.get(name).ok_or_else(|| {
                ntdb_error(format!(
                    "NTDB package v4 did not load promoter model {name}"
                ))
            })
        };
        let score = match policy.gate.as_str() {
            "direct_actionable" => model("direct_actionable_gate")?.predict_probability(features),
            "factorized_actionable" => {
                model("disagreement_gate")?.predict_probability(features)
                    * model("pre_l3_benefit_gate")?.predict_probability(features)
            }
            other => {
                return Err(ntdb_error(format!(
                    "unsupported actionable promoter gate: {other}"
                )))
            }
        };
        Ok((score, policy.threshold))
    }
}

fn required_models(policy: &ActionablePolicy) -> &'static [&'static str] {
    match policy.gate.as_str() {
        "direct_actionable" => &["direct_actionable_gate"],
        "factorized_actionable" => &["disagreement_gate", "pre_l3_benefit_gate"],
        _ => &[],
    }
}

impl From<&super::manifest::JointV3PromoterOperatingPoint> for ActionablePolicy {
    fn from(point: &super::manifest::JointV3PromoterOperatingPoint) -> Self {
        Self {
            gate: point.gate.clone(),
            threshold: point.promote_threshold,
            aggregation: point.aggregation.clone(),
            document_risk_margin_threshold: point.document_risk_margin_threshold,
        }
    }
}

fn load_gate(
    package_dir: &Path,
    manifest: &JointV3Manifest,
    name: &str,
) -> NtdbResult<LightGbmModel> {
    let gate = gate_manifest(manifest, name)?;
    validate_promoter_dim(gate.n_features, manifest)?;
    let path = gate
        .lightgbm_model
        .as_ref()
        .ok_or_else(|| ntdb_error(format!("NTDB package v4 gate {name} has no LightGBM model")))?;
    let model = LightGbmModel::load(package_dir.join(path))?;
    validate_model_dim(&model, gate.n_features, path)?;
    Ok(model)
}

fn gate_manifest(
    manifest: &JointV3Manifest,
    name: &str,
) -> NtdbResult<super::manifest::JointV3GateManifest> {
    let value = manifest
        .promoter
        .models
        .get(name)
        .ok_or_else(|| ntdb_error(format!("missing NTDB package v4 gate {name}")))?;
    serde_json::from_value(value.clone())
        .map_err(|error| ntdb_error(format!("invalid NTDB package v4 gate {name}: {error}")))
}

fn validate_promoter_dim(feature_count: usize, manifest: &JointV3Manifest) -> NtdbResult<()> {
    if feature_count == manifest.neural_stack.promoter_feature_dim
        && manifest.promoter.feature_dim == feature_count
    {
        Ok(())
    } else {
        Err(ntdb_error(format!(
            "NTDB package v4 promoter declares {feature_count} features, neural stack produces {}",
            manifest.neural_stack.promoter_feature_dim
        )))
    }
}

fn validate_model_dim(model: &LightGbmModel, expected: usize, path: &str) -> NtdbResult<()> {
    if model.feature_count() == expected {
        Ok(())
    } else {
        Err(ntdb_error(format!(
            "NTDB package v4 LightGBM {path} expects {} features, manifest declares {expected}",
            model.feature_count()
        )))
    }
}

fn tensor_values<'a, 'r>(
    outputs: &'a ort::session::SessionOutputs<'r>,
    name: &str,
) -> NtdbResult<&'a [f32]> {
    let value = outputs
        .get(name)
        .ok_or_else(|| ntdb_error(format!("NTDB package v4 output {name} is missing")))?;
    let (_shape, values) = value.try_extract_tensor::<f32>()?;
    Ok(values)
}

fn argmax(values: &[f32]) -> NtdbResult<usize> {
    values
        .iter()
        .copied()
        .enumerate()
        .max_by(|left, right| left.1.total_cmp(&right.1))
        .map(|(index, _)| index)
        .ok_or_else(|| ntdb_error("NTDB package v4 produced no class scores"))
}

#[cfg(test)]
mod tests {
    use super::short_injection_operating_point;
    use crate::NtdbOperatingPoint;

    #[test]
    fn ark_api_utility_profile_is_limited_to_short_injection_documents() {
        let profile = NtdbOperatingPoint::ArkApiShortInjectionUtility;
        assert_eq!(
            short_injection_operating_point("injection", profile, 2),
            NtdbOperatingPoint::BestF1
        );
        assert_eq!(
            short_injection_operating_point("injection", profile, 3),
            NtdbOperatingPoint::BestPromote
        );
        assert_eq!(
            short_injection_operating_point("threat", profile, 1),
            NtdbOperatingPoint::BestPromote
        );
        assert_eq!(
            short_injection_operating_point("injection", NtdbOperatingPoint::BestPromote, 1),
            NtdbOperatingPoint::BestPromote
        );
    }
}