reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Budget Configuration for Adaptive Compute Time
//!
//! Enables --budget flag for controlling reasoning execution within constraints.
//!
//! ## Budget Types
//! - Time budget: Maximum wall-clock time (e.g., "10s", "2m")
//! - Token budget: Maximum tokens to consume
//! - Cost budget: Maximum USD to spend (e.g., "$0.10")
//!
//! ## Adaptive Behavior
//! When budget is constrained, the executor will:
//! 1. Skip optional steps if time is running low
//! 2. Reduce max_tokens for remaining steps
//! 3. Use faster/cheaper model tiers when approaching limit
//! 4. Terminate early if budget is exhausted

use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};

/// Budget configuration for protocol execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BudgetConfig {
    /// Maximum execution time
    pub time_limit: Option<Duration>,

    /// Maximum tokens to consume
    pub token_limit: Option<u32>,

    /// Maximum cost in USD
    pub cost_limit: Option<f64>,

    /// Strategy when approaching budget limits
    #[serde(default)]
    pub strategy: BudgetStrategy,

    /// Percentage of budget at which to start adapting (0.0-1.0)
    #[serde(default = "default_adapt_threshold")]
    pub adapt_threshold: f64,
}

fn default_adapt_threshold() -> f64 {
    0.7 // Start adapting at 70% budget usage
}

/// Strategy for handling budget constraints
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BudgetStrategy {
    /// Strict: Fail if budget would be exceeded
    Strict,
    /// Adaptive: Reduce quality/scope to stay within budget (default)
    #[default]
    Adaptive,
    /// BestEffort: Try to complete as much as possible, may exceed budget
    BestEffort,
}

impl Default for BudgetConfig {
    fn default() -> Self {
        Self {
            time_limit: None,
            token_limit: None,
            cost_limit: None,
            strategy: BudgetStrategy::default(),
            adapt_threshold: default_adapt_threshold(),
        }
    }
}

impl BudgetConfig {
    /// Create an unlimited budget (no constraints)
    pub fn unlimited() -> Self {
        Self::default()
    }

    /// Create a time-limited budget
    pub fn with_time(duration: Duration) -> Self {
        Self {
            time_limit: Some(duration),
            ..Default::default()
        }
    }

    /// Create a token-limited budget
    pub fn with_tokens(limit: u32) -> Self {
        Self {
            token_limit: Some(limit),
            ..Default::default()
        }
    }

    /// Create a cost-limited budget
    pub fn with_cost(usd: f64) -> Self {
        Self {
            cost_limit: Some(usd),
            ..Default::default()
        }
    }

    /// Set budget strategy
    pub fn with_strategy(mut self, strategy: BudgetStrategy) -> Self {
        self.strategy = strategy;
        self
    }

    /// Check if any limits are set
    pub fn is_constrained(&self) -> bool {
        self.time_limit.is_some() || self.token_limit.is_some() || self.cost_limit.is_some()
    }

    /// Parse a budget string like "10s", "1000t", "$0.50"
    pub fn parse(budget_str: &str) -> Result<Self, BudgetParseError> {
        let budget_str = budget_str.trim();

        if budget_str.is_empty() {
            return Err(BudgetParseError::Empty);
        }

        // Cost budget: $X.XX
        if let Some(cost) = budget_str.strip_prefix('$') {
            let usd: f64 = cost.parse().map_err(|_| BudgetParseError::InvalidCost)?;
            return Ok(Self::with_cost(usd));
        }

        // Token budget: XXXt or XXXtokens
        if budget_str.ends_with('t') || budget_str.ends_with("tokens") {
            let num_str = budget_str.trim_end_matches("tokens").trim_end_matches('t');
            let tokens: u32 = num_str
                .parse()
                .map_err(|_| BudgetParseError::InvalidTokens)?;
            return Ok(Self::with_tokens(tokens));
        }

        // Time budget: Xs, Xm, Xh
        if let Some(secs) = budget_str.strip_suffix('s') {
            let seconds: u64 = secs.parse().map_err(|_| BudgetParseError::InvalidTime)?;
            return Ok(Self::with_time(Duration::from_secs(seconds)));
        }

        if let Some(mins) = budget_str.strip_suffix('m') {
            let minutes: u64 = mins.parse().map_err(|_| BudgetParseError::InvalidTime)?;
            return Ok(Self::with_time(Duration::from_secs(minutes * 60)));
        }

        if let Some(hours) = budget_str.strip_suffix('h') {
            let hours_val: u64 = hours.parse().map_err(|_| BudgetParseError::InvalidTime)?;
            return Ok(Self::with_time(Duration::from_secs(hours_val * 3600)));
        }

        Err(BudgetParseError::UnknownFormat(budget_str.to_string()))
    }
}

/// Error parsing budget string
#[derive(Debug, Clone)]
pub enum BudgetParseError {
    /// Budget string is empty
    Empty,
    /// Invalid time format
    InvalidTime,
    /// Invalid token count
    InvalidTokens,
    /// Invalid cost value
    InvalidCost,
    /// Unknown format string
    UnknownFormat(String),
}

impl std::fmt::Display for BudgetParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BudgetParseError::Empty => write!(f, "Budget string is empty"),
            BudgetParseError::InvalidTime => write!(f, "Invalid time format (use Xs, Xm, or Xh)"),
            BudgetParseError::InvalidTokens => write!(f, "Invalid token count (use Xt or Xtokens)"),
            BudgetParseError::InvalidCost => write!(f, "Invalid cost format (use $X.XX)"),
            BudgetParseError::UnknownFormat(s) => write!(f, "Unknown budget format: {}", s),
        }
    }
}

impl std::error::Error for BudgetParseError {}

/// Runtime budget tracker
#[derive(Debug, Clone)]
pub struct BudgetTracker {
    /// Configuration
    config: BudgetConfig,

    /// When execution started
    start_time: Instant,

    /// Tokens consumed so far
    tokens_used: u32,

    /// Cost incurred so far (USD)
    cost_incurred: f64,

    /// Steps completed
    steps_completed: usize,

    /// Steps skipped due to budget
    steps_skipped: usize,
}

impl BudgetTracker {
    /// Create a new budget tracker
    pub fn new(config: BudgetConfig) -> Self {
        Self {
            config,
            start_time: Instant::now(),
            tokens_used: 0,
            cost_incurred: 0.0,
            steps_completed: 0,
            steps_skipped: 0,
        }
    }

    /// Record token and cost usage
    pub fn record_usage(&mut self, tokens: u32, cost: f64) {
        self.tokens_used += tokens;
        self.cost_incurred += cost;
        self.steps_completed += 1;
    }

    /// Record a skipped step
    pub fn record_skip(&mut self) {
        self.steps_skipped += 1;
    }

    /// Get elapsed time
    pub fn elapsed(&self) -> Duration {
        self.start_time.elapsed()
    }

    /// Get time remaining (if time limit set)
    pub fn time_remaining(&self) -> Option<Duration> {
        self.config
            .time_limit
            .map(|limit| limit.saturating_sub(self.elapsed()))
    }

    /// Get tokens remaining (if token limit set)
    pub fn tokens_remaining(&self) -> Option<u32> {
        self.config
            .token_limit
            .map(|limit| limit.saturating_sub(self.tokens_used))
    }

    /// Get cost remaining (if cost limit set)
    pub fn cost_remaining(&self) -> Option<f64> {
        self.config
            .cost_limit
            .map(|limit| (limit - self.cost_incurred).max(0.0))
    }

    /// Check if budget is exhausted
    pub fn is_exhausted(&self) -> bool {
        if let Some(remaining) = self.time_remaining() {
            if remaining.is_zero() {
                return true;
            }
        }
        if let Some(remaining) = self.tokens_remaining() {
            if remaining == 0 {
                return true;
            }
        }
        if let Some(remaining) = self.cost_remaining() {
            if remaining <= 0.0 {
                return true;
            }
        }
        false
    }

    /// Calculate budget usage ratio (0.0-1.0)
    pub fn usage_ratio(&self) -> f64 {
        let mut max_ratio = 0.0f64;

        if let Some(limit) = self.config.time_limit {
            let ratio = self.elapsed().as_secs_f64() / limit.as_secs_f64();
            max_ratio = max_ratio.max(ratio);
        }

        if let Some(limit) = self.config.token_limit {
            let ratio = self.tokens_used as f64 / limit as f64;
            max_ratio = max_ratio.max(ratio);
        }

        if let Some(limit) = self.config.cost_limit {
            let ratio = self.cost_incurred / limit;
            max_ratio = max_ratio.max(ratio);
        }

        max_ratio.min(1.0)
    }

    /// Check if we should start adapting (approaching limit)
    pub fn should_adapt(&self) -> bool {
        self.usage_ratio() >= self.config.adapt_threshold
    }

    /// Get adaptive max_tokens based on remaining budget
    pub fn adaptive_max_tokens(&self, requested: u32) -> u32 {
        if let Some(remaining) = self.tokens_remaining() {
            // Reserve some tokens for remaining steps
            let reserve = remaining / 4;
            return requested.min(remaining - reserve);
        }
        requested
    }

    /// Check if step should be skipped (non-essential and low budget)
    pub fn should_skip_step(&self, is_essential: bool) -> bool {
        if is_essential {
            return false;
        }

        match self.config.strategy {
            BudgetStrategy::Strict => self.is_exhausted(),
            BudgetStrategy::Adaptive => self.usage_ratio() > 0.9,
            BudgetStrategy::BestEffort => false,
        }
    }

    /// Get budget summary
    pub fn summary(&self) -> BudgetSummary {
        BudgetSummary {
            elapsed: self.elapsed(),
            tokens_used: self.tokens_used,
            cost_incurred: self.cost_incurred,
            steps_completed: self.steps_completed,
            steps_skipped: self.steps_skipped,
            usage_ratio: self.usage_ratio(),
            exhausted: self.is_exhausted(),
        }
    }
}

/// Summary of budget usage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BudgetSummary {
    /// Time elapsed
    #[serde(with = "duration_serde")]
    pub elapsed: Duration,

    /// Tokens consumed
    pub tokens_used: u32,

    /// Cost in USD
    pub cost_incurred: f64,

    /// Steps completed
    pub steps_completed: usize,

    /// Steps skipped due to budget
    pub steps_skipped: usize,

    /// Usage ratio (0.0-1.0)
    pub usage_ratio: f64,

    /// Whether budget was exhausted
    pub exhausted: bool,
}

mod duration_serde {
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::time::Duration;

    pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        duration.as_millis().serialize(serializer)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error>
    where
        D: Deserializer<'de>,
    {
        let millis = u64::deserialize(deserializer)?;
        Ok(Duration::from_millis(millis))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_time_seconds() {
        let budget = BudgetConfig::parse("30s").unwrap();
        assert_eq!(budget.time_limit, Some(Duration::from_secs(30)));
    }

    #[test]
    fn test_parse_time_minutes() {
        let budget = BudgetConfig::parse("5m").unwrap();
        assert_eq!(budget.time_limit, Some(Duration::from_secs(300)));
    }

    #[test]
    fn test_parse_tokens() {
        let budget = BudgetConfig::parse("1000t").unwrap();
        assert_eq!(budget.token_limit, Some(1000));
    }

    #[test]
    fn test_parse_tokens_full() {
        let budget = BudgetConfig::parse("5000tokens").unwrap();
        assert_eq!(budget.token_limit, Some(5000));
    }

    #[test]
    fn test_parse_cost() {
        let budget = BudgetConfig::parse("$0.50").unwrap();
        assert_eq!(budget.cost_limit, Some(0.50));
    }

    #[test]
    fn test_budget_tracker_usage() {
        let config = BudgetConfig::with_tokens(1000);
        let mut tracker = BudgetTracker::new(config);

        tracker.record_usage(200, 0.01);
        assert_eq!(tracker.tokens_remaining(), Some(800));
        assert!(!tracker.is_exhausted());

        tracker.record_usage(800, 0.04);
        assert_eq!(tracker.tokens_remaining(), Some(0));
        assert!(tracker.is_exhausted());
    }

    #[test]
    fn test_budget_tracker_adapt() {
        let config = BudgetConfig::with_tokens(1000);
        let mut tracker = BudgetTracker::new(config);

        tracker.record_usage(600, 0.03);
        assert!(!tracker.should_adapt()); // 60% < 70% threshold

        tracker.record_usage(150, 0.01);
        assert!(tracker.should_adapt()); // 75% > 70% threshold
    }

    #[test]
    fn test_adaptive_max_tokens() {
        let config = BudgetConfig::with_tokens(1000);
        let mut tracker = BudgetTracker::new(config);

        // Initially can request full amount
        assert_eq!(tracker.adaptive_max_tokens(500), 500);

        // After using some, it limits
        tracker.record_usage(800, 0.04);
        assert!(tracker.adaptive_max_tokens(500) < 200);
    }
}