switchyard_libsy/algorithms/vgr/
config.rs1use std::time::Duration;
5
6use switchyard_protocol::ModelId;
7
8use super::safety::{BreakerConfig, KillSwitch};
9use crate::{LibsyError, Result};
10
11pub const ACTIVE_APPROVAL: &str = "prospective-validation-and-canary-approved";
13
14#[derive(Clone, Debug, Default, Eq, PartialEq)]
16pub enum ServingMode {
17 #[default]
19 Off,
20 Evaluate,
22 Shadow,
24 Active {
26 approval: String,
28 },
29}
30
31#[derive(Clone, Debug)]
33pub struct Targets {
34 pub local: ModelId,
36 pub cloud: ModelId,
38 pub judge: Option<ModelId>,
40 pub cloud_judge: Option<ModelId>,
42}
43
44#[derive(Clone, Debug)]
46pub struct VgrConfig {
47 pub targets: Targets,
49 pub mode: ServingMode,
51 pub deadline: Duration,
53 pub kill_switch: Option<KillSwitch>,
55 pub breaker: BreakerConfig,
57 pub task_typing: bool,
59 pub local_supports_images: bool,
61 pub confirmed_recovery_min_clean_tail: Option<u32>,
64 pub agentic_handoff: bool,
67 pub local_turn_budget: Option<Duration>,
70}
71
72impl VgrConfig {
73 pub fn new(local: ModelId, cloud: ModelId) -> Self {
75 Self {
76 targets: Targets {
77 local,
78 cloud,
79 judge: None,
80 cloud_judge: None,
81 },
82 mode: ServingMode::Off,
83 deadline: Duration::from_secs(30),
84 kill_switch: None,
85 breaker: BreakerConfig::default(),
86 task_typing: true,
87 local_supports_images: false,
88 confirmed_recovery_min_clean_tail: None,
89 agentic_handoff: false,
90 local_turn_budget: None,
91 }
92 }
93
94 pub(super) fn validate(&self) -> Result<()> {
95 if self.targets.local == self.targets.cloud {
96 return Err(LibsyError::AlgorithmError {
97 message: "vgr requires distinct local and cloud model IDs".into(),
98 });
99 }
100 if self.deadline.is_zero() || self.breaker.threshold == 0 {
101 return Err(LibsyError::AlgorithmError {
102 message: "vgr deadline and breaker threshold must be non-zero".into(),
103 });
104 }
105 if let ServingMode::Active { approval } = &self.mode
106 && approval != ACTIVE_APPROVAL
107 {
108 return Err(LibsyError::AlgorithmError {
109 message: format!(
110 "vgr active mode requires approval attestation {ACTIVE_APPROVAL:?}"
111 ),
112 });
113 }
114 Ok(())
115 }
116
117 pub(super) fn judge(&self) -> &ModelId {
118 self.targets.judge.as_ref().unwrap_or(&self.targets.local)
119 }
120
121 pub(super) fn confirmed_min_clean_tail(&self) -> Option<i32> {
122 self.confirmed_recovery_min_clean_tail
123 .map(|tail| i32::try_from(tail.max(1)).unwrap_or(i32::MAX))
124 }
125}
126
127#[cfg(test)]
128mod tests {
129 use super::*;
130
131 #[test]
132 fn direct_callers_must_use_distinct_local_and_cloud_ids() {
133 assert!(super::super::Vgr::new(VgrConfig::new("same".into(), "same".into())).is_err());
134 assert!(super::super::Vgr::new(VgrConfig::new("local".into(), "cloud".into())).is_ok());
135 }
136}