behest 0.2.1

A Rust-native cloud agent runtime with typed tools, pluggable memory, queues, and observability.
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//! Input admission and promotion pipeline.
//!
//! Provides event-sourced input lifecycle management: inputs are submitted,
//! validated, deduplicated, and admitted before entering the run loop.

use std::collections::HashSet;
use std::collections::hash_map::DefaultHasher;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::sync::Mutex;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Unique identifier for an input submission.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct InputId(Uuid);

impl InputId {
    /// Creates a new input identifier.
    #[must_use]
    pub fn new() -> Self {
        Self(Uuid::new_v4())
    }

    /// Creates an input identifier from an existing UUID.
    #[must_use]
    pub fn from_uuid(uuid: Uuid) -> Self {
        Self(uuid)
    }

    /// Returns the underlying UUID.
    #[must_use]
    pub fn as_uuid(&self) -> &Uuid {
        &self.0
    }
}

impl Default for InputId {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for InputId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Lifecycle state of an input submission.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum InputState {
    /// Input has been submitted but not yet validated.
    Submitted,
    /// Input passed validation and is admitted for processing.
    Admitted,
    /// Input is currently being processed by a run.
    Processing,
    /// Input processing completed successfully.
    Completed,
    /// Input was rejected during validation.
    Rejected,
}

impl InputState {
    /// Returns true if the input is in a terminal state.
    #[must_use]
    pub fn is_terminal(&self) -> bool {
        matches!(self, Self::Completed | Self::Rejected)
    }
}

/// Persistent record of an input submission.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InputRecord {
    /// Unique input identifier.
    pub id: InputId,
    /// Session this input belongs to.
    pub session_id: Uuid,
    /// Current state of the input.
    pub state: InputState,
    /// Input content.
    pub content: String,
    /// Fingerprint for deduplication.
    pub fingerprint: u64,
    /// Rejection reason (if rejected).
    pub rejection_reason: Option<String>,
    /// When the input was submitted.
    pub submitted_at: DateTime<Utc>,
    /// When the input was last updated.
    pub updated_at: DateTime<Utc>,
}

impl InputRecord {
    /// Creates a new input record in `Submitted` state.
    #[must_use]
    pub fn new(session_id: Uuid, content: String) -> Self {
        let fingerprint = Self::compute_fingerprint(&content);
        let now = Utc::now();
        Self {
            id: InputId::new(),
            session_id,
            state: InputState::Submitted,
            content,
            fingerprint,
            rejection_reason: None,
            submitted_at: now,
            updated_at: now,
        }
    }

    /// Computes a fingerprint for deduplication.
    fn compute_fingerprint(content: &str) -> u64 {
        let mut hasher = DefaultHasher::new();
        content.trim().hash(&mut hasher);
        hasher.finish()
    }

    /// Updates the state and timestamp.
    pub fn update_state(&mut self, state: InputState) {
        self.state = state;
        self.updated_at = Utc::now();
    }

    /// Rejects the input with a reason.
    pub fn reject(&mut self, reason: String) {
        self.state = InputState::Rejected;
        self.rejection_reason = Some(reason);
        self.updated_at = Utc::now();
    }
}

/// Event in the input lifecycle.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum InputEvent {
    /// Input was submitted.
    Submitted {
        /// Unique input identifier.
        input_id: InputId,
        /// Session this input belongs to.
        session_id: Uuid,
        /// Raw input content.
        content: String,
        /// Content fingerprint for deduplication.
        fingerprint: u64,
        /// Submission timestamp.
        timestamp: DateTime<Utc>,
    },
    /// Input was admitted for processing.
    Admitted {
        /// Unique input identifier.
        input_id: InputId,
        /// Admission timestamp.
        timestamp: DateTime<Utc>,
    },
    /// Input was rejected.
    Rejected {
        /// Unique input identifier.
        input_id: InputId,
        /// Rejection reason.
        reason: String,
        /// Rejection timestamp.
        timestamp: DateTime<Utc>,
    },
    /// Input processing started.
    Processing {
        /// Unique input identifier.
        input_id: InputId,
        /// Processing start timestamp.
        timestamp: DateTime<Utc>,
    },
    /// Input processing completed.
    Completed {
        /// Unique input identifier.
        input_id: InputId,
        /// Processing completion timestamp.
        timestamp: DateTime<Utc>,
    },
}

impl InputEvent {
    /// Returns the input ID associated with this event.
    #[must_use]
    pub fn input_id(&self) -> InputId {
        match self {
            Self::Submitted { input_id, .. }
            | Self::Admitted { input_id, .. }
            | Self::Rejected { input_id, .. }
            | Self::Processing { input_id, .. }
            | Self::Completed { input_id, .. } => *input_id,
        }
    }
}

/// Configuration for input admission.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InputAdmissionConfig {
    /// Whether admission is enabled.
    pub enabled: bool,
    /// Whether to reject empty inputs.
    pub reject_empty: bool,
    /// Whether to deduplicate inputs within a session.
    pub deduplicate: bool,
    /// Maximum input length (0 = unlimited).
    pub max_length: usize,
}

impl Default for InputAdmissionConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            reject_empty: true,
            deduplicate: true,
            max_length: 0,
        }
    }
}

impl InputAdmissionConfig {
    /// Sets whether admission is enabled.
    #[must_use]
    pub fn with_enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }

    /// Sets whether to reject empty inputs.
    #[must_use]
    pub fn with_reject_empty(mut self, reject_empty: bool) -> Self {
        self.reject_empty = reject_empty;
        self
    }

    /// Sets whether to deduplicate inputs.
    #[must_use]
    pub fn with_deduplicate(mut self, deduplicate: bool) -> Self {
        self.deduplicate = deduplicate;
        self
    }

    /// Sets the maximum input length.
    #[must_use]
    pub fn with_max_length(mut self, max_length: usize) -> Self {
        self.max_length = max_length;
        self
    }
}

/// Input admission pipeline.
///
/// Validates, deduplicates, and admits inputs before they enter the run loop.
pub struct InputAdmission {
    config: InputAdmissionConfig,
    seen_fingerprints: Mutex<HashSet<u64>>,
}

impl InputAdmission {
    /// Creates a new input admission pipeline.
    #[must_use]
    pub fn new(config: InputAdmissionConfig) -> Self {
        Self {
            config,
            seen_fingerprints: Mutex::new(HashSet::new()),
        }
    }

    /// Admits an input record, returning events generated.
    ///
    /// If the input is rejected, the record is mutated and a `Rejected` event is returned.
    /// If admitted, an `Admitted` event is returned.
    ///
    /// # Errors
    ///
    /// Returns `InputAdmissionError` if the lock is poisoned.
    pub fn admit(&self, record: &mut InputRecord) -> Result<Vec<InputEvent>, InputAdmissionError> {
        if !self.config.enabled {
            record.update_state(InputState::Admitted);
            return Ok(vec![InputEvent::Admitted {
                input_id: record.id,
                timestamp: Utc::now(),
            }]);
        }

        let mut events = vec![InputEvent::Submitted {
            input_id: record.id,
            session_id: record.session_id,
            content: record.content.clone(),
            fingerprint: record.fingerprint,
            timestamp: record.submitted_at,
        }];

        // Validate: reject empty
        if self.config.reject_empty && record.content.trim().is_empty() {
            record.reject("empty input".to_string());
            events.push(InputEvent::Rejected {
                input_id: record.id,
                reason: "empty input".to_string(),
                timestamp: Utc::now(),
            });
            return Ok(events);
        }

        // Validate: max length
        if self.config.max_length > 0 && record.content.len() > self.config.max_length {
            let reason = format!("input exceeds maximum length of {}", self.config.max_length);
            record.reject(reason.clone());
            events.push(InputEvent::Rejected {
                input_id: record.id,
                reason,
                timestamp: Utc::now(),
            });
            return Ok(events);
        }

        // Deduplicate
        if self.config.deduplicate {
            let mut seen = self
                .seen_fingerprints
                .lock()
                .map_err(|_| InputAdmissionError::LockPoisoned)?;
            if !seen.insert(record.fingerprint) {
                record.reject("duplicate input".to_string());
                events.push(InputEvent::Rejected {
                    input_id: record.id,
                    reason: "duplicate input".to_string(),
                    timestamp: Utc::now(),
                });
                return Ok(events);
            }
        }

        // Admit
        record.update_state(InputState::Admitted);
        events.push(InputEvent::Admitted {
            input_id: record.id,
            timestamp: Utc::now(),
        });

        Ok(events)
    }

    /// Marks an input as processing.
    #[must_use]
    pub fn mark_processing(&self, record: &mut InputRecord) -> InputEvent {
        record.update_state(InputState::Processing);
        InputEvent::Processing {
            input_id: record.id,
            timestamp: Utc::now(),
        }
    }

    /// Marks an input as completed.
    #[must_use]
    pub fn mark_completed(&self, record: &mut InputRecord) -> InputEvent {
        record.update_state(InputState::Completed);
        InputEvent::Completed {
            input_id: record.id,
            timestamp: Utc::now(),
        }
    }

    /// Clears the deduplication cache.
    ///
    /// # Errors
    ///
    /// Returns [`InputAdmissionError::LockPoisoned`] if the internal
    /// fingerprint mutex has been poisoned by a panicking thread.
    pub fn clear_cache(&self) -> Result<(), InputAdmissionError> {
        let mut seen = self
            .seen_fingerprints
            .lock()
            .map_err(|_| InputAdmissionError::LockPoisoned)?;
        seen.clear();
        Ok(())
    }
}

/// Error from input admission.
#[derive(Debug, Clone)]
pub enum InputAdmissionError {
    /// Lock was poisoned.
    LockPoisoned,
}

impl fmt::Display for InputAdmissionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LockPoisoned => write!(f, "input admission lock poisoned"),
        }
    }
}

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

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn input_id_generation() {
        let id1 = InputId::new();
        let id2 = InputId::new();
        assert_ne!(id1, id2);
    }

    #[test]
    fn input_state_terminal() {
        assert!(!InputState::Submitted.is_terminal());
        assert!(!InputState::Admitted.is_terminal());
        assert!(!InputState::Processing.is_terminal());
        assert!(InputState::Completed.is_terminal());
        assert!(InputState::Rejected.is_terminal());
    }

    #[test]
    fn input_record_creation() {
        let session_id = Uuid::new_v4();
        let record = InputRecord::new(session_id, "hello world".to_string());
        assert_eq!(record.state, InputState::Submitted);
        assert_eq!(record.content, "hello world");
        assert!(record.rejection_reason.is_none());
    }

    #[test]
    fn admission_admits_valid_input() {
        let config = InputAdmissionConfig::default();
        let admission = InputAdmission::new(config);
        let session_id = Uuid::new_v4();
        let mut record = InputRecord::new(session_id, "hello".to_string());

        let events = admission.admit(&mut record).unwrap();
        assert_eq!(record.state, InputState::Admitted);
        assert_eq!(events.len(), 2); // Submitted + Admitted
    }

    #[test]
    fn admission_rejects_empty_input() {
        let config = InputAdmissionConfig::default();
        let admission = InputAdmission::new(config);
        let session_id = Uuid::new_v4();
        let mut record = InputRecord::new(session_id, "   ".to_string());

        let events = admission.admit(&mut record).unwrap();
        assert_eq!(record.state, InputState::Rejected);
        assert_eq!(record.rejection_reason.as_deref(), Some("empty input"));
        assert_eq!(events.len(), 2); // Submitted + Rejected
    }

    #[test]
    fn admission_rejects_duplicate_input() {
        let config = InputAdmissionConfig::default();
        let admission = InputAdmission::new(config);
        let session_id = Uuid::new_v4();

        let mut record1 = InputRecord::new(session_id, "hello".to_string());
        let events1 = admission.admit(&mut record1).unwrap();
        assert_eq!(record1.state, InputState::Admitted);
        assert_eq!(events1.len(), 2);

        let mut record2 = InputRecord::new(session_id, "hello".to_string());
        let events2 = admission.admit(&mut record2).unwrap();
        assert_eq!(record2.state, InputState::Rejected);
        assert_eq!(record2.rejection_reason.as_deref(), Some("duplicate input"));
        assert_eq!(events2.len(), 2); // Submitted + Rejected
    }

    #[test]
    fn admission_rejects_over_length_input() {
        let config = InputAdmissionConfig::default().with_max_length(5);
        let admission = InputAdmission::new(config);
        let session_id = Uuid::new_v4();
        let mut record = InputRecord::new(session_id, "hello world".to_string());

        let events = admission.admit(&mut record).unwrap();
        assert_eq!(record.state, InputState::Rejected);
        assert!(
            record
                .rejection_reason
                .as_ref()
                .unwrap()
                .contains("maximum length")
        );
        assert_eq!(events.len(), 2); // Submitted + Rejected
    }

    #[test]
    fn admission_disabled_admits_all() {
        let config = InputAdmissionConfig::default().with_enabled(false);
        let admission = InputAdmission::new(config);
        let session_id = Uuid::new_v4();
        let mut record = InputRecord::new(session_id, String::new());

        let events = admission.admit(&mut record).unwrap();
        assert_eq!(record.state, InputState::Admitted);
        assert_eq!(events.len(), 1); // Only Admitted (no Submitted event when disabled)
    }

    #[test]
    fn admission_dedup_disabled_allows_duplicates() {
        let config = InputAdmissionConfig::default().with_deduplicate(false);
        let admission = InputAdmission::new(config);
        let session_id = Uuid::new_v4();

        let mut record1 = InputRecord::new(session_id, "hello".to_string());
        admission.admit(&mut record1).unwrap();

        let mut record2 = InputRecord::new(session_id, "hello".to_string());
        let events = admission.admit(&mut record2).unwrap();
        assert_eq!(record2.state, InputState::Admitted);
        assert_eq!(events.len(), 2); // Submitted + Admitted
    }

    #[test]
    fn mark_processing_and_completed() {
        let config = InputAdmissionConfig::default();
        let admission = InputAdmission::new(config);
        let session_id = Uuid::new_v4();
        let mut record = InputRecord::new(session_id, "hello".to_string());
        admission.admit(&mut record).unwrap();

        let event = admission.mark_processing(&mut record);
        assert_eq!(record.state, InputState::Processing);
        assert!(matches!(event, InputEvent::Processing { .. }));

        let event = admission.mark_completed(&mut record);
        assert_eq!(record.state, InputState::Completed);
        assert!(matches!(event, InputEvent::Completed { .. }));
    }

    #[test]
    fn clear_cache_allows_duplicate() {
        let config = InputAdmissionConfig::default();
        let admission = InputAdmission::new(config);
        let session_id = Uuid::new_v4();

        let mut record1 = InputRecord::new(session_id, "hello".to_string());
        admission.admit(&mut record1).unwrap();

        admission.clear_cache().unwrap();

        let mut record2 = InputRecord::new(session_id, "hello".to_string());
        let events = admission.admit(&mut record2).unwrap();
        assert_eq!(record2.state, InputState::Admitted);
        assert_eq!(events.len(), 2);
    }
}