use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TurnProgress<'a> {
pub api_calls: u32,
pub tool_calls: u32,
pub structured_output_calls: u32,
pub stop_reason: &'a str,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TurnStep {
Continue,
Stop { reason: String },
}
impl TurnStep {
pub fn stop(reason: impl Into<String>) -> Self {
Self::Stop {
reason: reason.into(),
}
}
pub fn is_stop(&self) -> bool {
matches!(self, Self::Stop { .. })
}
}
pub trait TurnPolicy: Send + Sync {
fn before_model_call(&self, _progress: &TurnProgress<'_>) -> TurnStep {
TurnStep::Continue
}
fn after_model_call(&self, _progress: &TurnProgress<'_>) -> TurnStep {
TurnStep::Continue
}
}
pub struct LimitsPolicy {
pub max_api_calls: u32,
pub max_structured_output_retries: u32,
}
impl LimitsPolicy {
pub fn new(settings_max_api_calls: u32, scene_max_api_calls: u32, max_retries: u32) -> Self {
Self {
max_api_calls: settings_max_api_calls.min(scene_max_api_calls),
max_structured_output_retries: max_retries,
}
}
}
impl TurnPolicy for LimitsPolicy {
fn before_model_call(&self, progress: &TurnProgress<'_>) -> TurnStep {
if progress.api_calls >= self.max_api_calls {
return TurnStep::stop("max_turns");
}
TurnStep::Continue
}
fn after_model_call(&self, progress: &TurnProgress<'_>) -> TurnStep {
if progress.structured_output_calls >= self.max_structured_output_retries {
return TurnStep::stop("max_structured_output_retries");
}
TurnStep::Continue
}
}
pub struct FirstOf(pub Vec<Arc<dyn TurnPolicy>>);
impl TurnPolicy for FirstOf {
fn before_model_call(&self, progress: &TurnProgress<'_>) -> TurnStep {
for p in &self.0 {
let step = p.before_model_call(progress);
if step.is_stop() {
return step;
}
}
TurnStep::Continue
}
fn after_model_call(&self, progress: &TurnProgress<'_>) -> TurnStep {
for p in &self.0 {
let step = p.after_model_call(progress);
if step.is_stop() {
return step;
}
}
TurnStep::Continue
}
}
#[cfg(test)]
mod tests {
use super::*;
fn at(api_calls: u32, structured: u32) -> TurnProgress<'static> {
TurnProgress {
api_calls,
tool_calls: 0,
structured_output_calls: structured,
stop_reason: "",
}
}
fn limits() -> LimitsPolicy {
LimitsPolicy::new(25, u32::MAX, 5)
}
#[test]
fn the_call_ceiling_stops_at_it_not_after_it() {
assert_eq!(limits().before_model_call(&at(24, 0)), TurnStep::Continue);
assert_eq!(
limits().before_model_call(&at(25, 0)),
TurnStep::stop("max_turns"),
"the check is `>=`; an off-by-one here is 26 calls billed instead of 25"
);
}
#[test]
fn the_tighter_of_the_two_ceilings_wins() {
let scene_is_tighter = LimitsPolicy::new(25, 3, 5);
assert_eq!(scene_is_tighter.max_api_calls, 3);
let settings_are_tighter = LimitsPolicy::new(2, 10, 5);
assert_eq!(settings_are_tighter.max_api_calls, 2);
}
#[test]
fn the_retry_ceiling_is_checked_after_the_call_not_before() {
assert_eq!(
limits().before_model_call(&at(0, 99)),
TurnStep::Continue,
"a retry count must not end a turn before the call that would raise it"
);
assert_eq!(
limits().after_model_call(&at(0, 5)),
TurnStep::stop("max_structured_output_retries")
);
}
#[test]
fn a_host_ceiling_composes_with_the_engines_rather_than_replacing_it() {
struct StopAtThree;
impl TurnPolicy for StopAtThree {
fn before_model_call(&self, progress: &TurnProgress<'_>) -> TurnStep {
if progress.api_calls >= 3 {
return TurnStep::stop("host_ceiling");
}
TurnStep::Continue
}
}
let composed = FirstOf(vec![Arc::new(limits()), Arc::new(StopAtThree)]);
assert_eq!(composed.before_model_call(&at(2, 0)), TurnStep::Continue);
assert_eq!(
composed.before_model_call(&at(3, 0)),
TurnStep::stop("host_ceiling"),
"the tighter one wins even though the engine's would still allow it"
);
assert_eq!(
composed.after_model_call(&at(0, 5)),
TurnStep::stop("max_structured_output_retries"),
"and the engine's still applies where the host's says nothing"
);
}
#[test]
fn a_stop_is_never_overridden() {
struct NeverStops;
impl TurnPolicy for NeverStops {}
let composed = FirstOf(vec![Arc::new(limits()), Arc::new(NeverStops)]);
assert_eq!(
composed.before_model_call(&at(25, 0)),
TurnStep::stop("max_turns")
);
}
}