codetether_agent/config/guardrails.rs
1//! Cost-guardrail configuration.
2//!
3//! Lets users bound runaway spend on long-running agent sessions. Values
4//! may come from `[guardrails]` in the config file or from the
5//! `CODETETHER_COST_WARN_USD` / `CODETETHER_COST_LIMIT_USD` environment
6//! variables (env takes precedence).
7
8use serde::{Deserialize, Serialize};
9
10/// Spending caps applied to the agentic loop.
11///
12/// Both fields are `None` by default — i.e. no limits. Once `warn_usd`
13/// is reached the agent emits a one-shot `tracing::warn!`. Once
14/// `hard_limit_usd` is reached the agent refuses further provider
15/// requests and returns an error.
16#[derive(Debug, Clone, Default, Serialize, Deserialize)]
17pub struct CostGuardrails {
18 /// Warn once when cumulative session cost exceeds this USD threshold.
19 #[serde(default, skip_serializing_if = "Option::is_none")]
20 pub warn_usd: Option<f64>,
21
22 /// Hard-stop: refuse new provider requests above this USD threshold.
23 #[serde(default, skip_serializing_if = "Option::is_none")]
24 pub hard_limit_usd: Option<f64>,
25}
26
27impl CostGuardrails {
28 /// Overlay environment-variable overrides on top of a base config.
29 pub fn with_env_overrides(mut self) -> Self {
30 if let Some(v) = env_f64("CODETETHER_COST_WARN_USD") {
31 self.warn_usd = Some(v);
32 }
33 if let Some(v) = env_f64("CODETETHER_COST_LIMIT_USD") {
34 self.hard_limit_usd = Some(v);
35 }
36 self
37 }
38
39 /// Load guardrails using env-only (no config file). Used from code
40 /// paths that don't have a loaded [`crate::config::Config`] on hand.
41 pub fn from_env() -> Self {
42 Self::default().with_env_overrides()
43 }
44}
45
46fn env_f64(key: &str) -> Option<f64> {
47 std::env::var(key).ok().and_then(|v| v.trim().parse().ok())
48}