switchyard_libsy/core/outcome_metadata.rs
1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Metadata describing a routing outcome.
5
6use serde_json::Value;
7use switchyard_protocol::ModelId;
8
9/// A judge's selection after applying its routing policy.
10#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
11pub struct JudgeDecision {
12 /// Selected target, or none when the judge abstains.
13 pub target: Option<ModelId>,
14 /// Category of the selected target, when known.
15 pub category: Option<String>,
16 /// Available scores, thresholds or failure reasons.
17 pub evidence: Option<Value>,
18}
19
20/// Identity and optional algorithm evidence attached to a successful routing outcome.
21#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
22pub struct OutcomeMetadata {
23 outcome_id: String,
24 /// Stable name of the algorithm that produced the outcome.
25 pub algorithm: String,
26 /// Optional algorithm-defined JSON evidence.
27 ///
28 /// Built-in algorithms include a `source` and relevant scores or decision details.
29 /// Not every algorithm or decision produces evidence.
30 pub evidence: Option<Value>,
31 /// Judge selections retained separately from the final routing reason.
32 #[serde(default, skip_serializing_if = "Vec::is_empty")]
33 pub judgments: Vec<JudgeDecision>,
34}
35
36impl OutcomeMetadata {
37 /// Creates outcome metadata with a new UUIDv7 identifier.
38 pub fn new(algorithm: String, evidence: Option<Value>) -> Self {
39 Self {
40 outcome_id: uuid::Uuid::now_v7().to_string(),
41 algorithm,
42 evidence,
43 judgments: Vec::new(),
44 }
45 }
46
47 /// Returns the unique identifier for this outcome.
48 pub fn outcome_id(&self) -> &str {
49 &self.outcome_id
50 }
51}