use std::time::Duration;
use switchyard_protocol::ModelId;
use super::safety::{BreakerConfig, KillSwitch};
use crate::{LibsyError, Result};
pub const ACTIVE_APPROVAL: &str = "prospective-validation-and-canary-approved";
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub enum ServingMode {
#[default]
Off,
Evaluate,
Shadow,
Active {
approval: String,
},
}
#[derive(Clone, Debug)]
pub struct Targets {
pub efficient: ModelId,
pub capable: ModelId,
pub judge: Option<ModelId>,
pub capable_judge: Option<ModelId>,
}
#[derive(Clone, Debug)]
pub struct VgrConfig {
pub targets: Targets,
pub mode: ServingMode,
pub deadline: Duration,
pub bounded_efficient: bool,
pub kill_switch: Option<KillSwitch>,
pub breaker: BreakerConfig,
pub task_typing: bool,
pub efficient_supports_attachments: bool,
pub confirmed_recovery_min_clean_tail: Option<u32>,
pub agentic_handoff: bool,
pub compact_handoff: bool,
pub efficient_turn_budget: Option<Duration>,
pub latch_escalation: bool,
}
impl VgrConfig {
pub fn new(efficient: ModelId, capable: ModelId) -> Self {
Self {
targets: Targets {
efficient,
capable,
judge: None,
capable_judge: None,
},
mode: ServingMode::Off,
deadline: Duration::from_secs(30),
bounded_efficient: true,
kill_switch: None,
breaker: BreakerConfig::default(),
task_typing: true,
efficient_supports_attachments: false,
confirmed_recovery_min_clean_tail: None,
agentic_handoff: false,
compact_handoff: false,
efficient_turn_budget: None,
latch_escalation: false,
}
}
pub(super) fn validate(&self) -> Result<()> {
if self.compact_handoff && !self.agentic_handoff {
return Err(LibsyError::AlgorithmError {
message: "compact_handoff requires agentic_handoff".into(),
});
}
if self.targets.efficient == self.targets.capable {
return Err(LibsyError::AlgorithmError {
message: "vgr requires distinct efficient and capable model IDs".into(),
});
}
if self.deadline.is_zero() || self.breaker.threshold == 0 {
return Err(LibsyError::AlgorithmError {
message: "vgr deadline and breaker threshold must be non-zero".into(),
});
}
if let ServingMode::Active { approval } = &self.mode
&& approval != ACTIVE_APPROVAL
{
return Err(LibsyError::AlgorithmError {
message: format!(
"vgr active mode requires approval attestation {ACTIVE_APPROVAL:?}"
),
});
}
Ok(())
}
pub(super) fn judge(&self) -> &ModelId {
self.targets
.judge
.as_ref()
.unwrap_or(&self.targets.efficient)
}
pub(super) fn confirmed_min_clean_tail(&self) -> Option<i32> {
self.confirmed_recovery_min_clean_tail
.map(|tail| i32::try_from(tail.max(1)).unwrap_or(i32::MAX))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn compaction_requires_handoff_for_direct_callers() {
let mut config = VgrConfig::new("efficient".into(), "capable".into());
assert!(!config.compact_handoff);
config.compact_handoff = true;
assert!(super::super::Vgr::new(config.clone()).is_err());
config.agentic_handoff = true;
assert!(super::super::Vgr::new(config).is_ok());
}
#[test]
fn direct_callers_must_use_distinct_efficient_and_capable_ids() {
assert!(super::super::Vgr::new(VgrConfig::new("same".into(), "same".into())).is_err());
assert!(
super::super::Vgr::new(VgrConfig::new("efficient".into(), "capable".into())).is_ok()
);
}
}