brainwires-autonomy 0.8.0

Autonomous agent operations — self-improvement, Git workflows, and human-out-of-loop execution
Documentation
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! Safety mechanisms for autonomous operations.
//!
//! Provides circuit breakers, budget tracking, approval policies, and
//! dead man's switches to prevent runaway autonomous operations.

use std::fmt;
use std::sync::Arc;
use std::time::Instant;

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

use crate::config::{SafetyConfig, SelfImprovementConfig};

// ── Safety stop reasons ─────────────────────────────────────────────────────

/// Reason an autonomous operation was stopped by a safety mechanism.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SafetyStop {
    /// Budget limit was exceeded.
    BudgetExceeded(
        /// Total cost incurred.
        f64,
    ),
    /// Maximum cycle count reached.
    CycleLimitReached(
        /// Number of cycles completed.
        u32,
    ),
    /// Circuit breaker tripped after consecutive failures.
    CircuitBreakerTripped(
        /// Number of consecutive failures.
        u32,
    ),
    /// Total diff line limit exceeded.
    DiffLimitExceeded(
        /// Number of diff lines.
        u32,
    ),
    /// Dead man's switch heartbeat timed out.
    HeartbeatTimeout,
    /// Daily operation limit reached.
    DailyLimitReached(
        /// Number of daily operations completed.
        u32,
    ),
    /// Operation was explicitly rejected.
    OperationRejected(
        /// Rejection reason.
        String,
    ),
}

impl fmt::Display for SafetyStop {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SafetyStop::BudgetExceeded(cost) => write!(f, "Budget exceeded: ${cost:.2}"),
            SafetyStop::CycleLimitReached(cycles) => write!(f, "Cycle limit reached: {cycles}"),
            SafetyStop::CircuitBreakerTripped(failures) => {
                write!(
                    f,
                    "Circuit breaker tripped after {failures} consecutive failures"
                )
            }
            SafetyStop::DiffLimitExceeded(lines) => {
                write!(f, "Total diff limit exceeded: {lines} lines")
            }
            SafetyStop::HeartbeatTimeout => write!(f, "Dead man's switch: heartbeat timeout"),
            SafetyStop::DailyLimitReached(count) => {
                write!(f, "Daily operation limit reached: {count}")
            }
            SafetyStop::OperationRejected(reason) => {
                write!(f, "Operation rejected: {reason}")
            }
        }
    }
}

// ── Autonomous operation types ──────────────────────────────────────────────

/// Describes an autonomous operation that may require approval.
#[derive(Debug, Clone)]
pub enum AutonomousOperation {
    /// Start a self-improvement session.
    StartImprovement {
        /// Name of the improvement strategy.
        strategy: String,
        /// Estimated cost in USD.
        estimated_cost: f64,
    },
    /// Commit code changes.
    CommitChanges {
        /// Number of changed lines.
        diff_lines: u32,
        /// List of modified files.
        files: Vec<String>,
    },
    /// Create a pull request.
    CreatePullRequest {
        /// Branch name for the PR.
        branch: String,
        /// Title of the pull request.
        title: String,
    },
    /// Merge a pull request.
    MergePullRequest {
        /// Pull request identifier.
        pr_id: String,
        /// Confidence score for the merge.
        confidence: f64,
    },
    /// Spawn a new agent.
    SpawnAgent {
        /// Description of the agent's task.
        description: String,
    },
    /// Restart a running agent.
    RestartAgent {
        /// Identifier of the agent to restart.
        agent_id: String,
        /// Reason for the restart.
        reason: String,
    },
    /// Start a model training job.
    StartTrainingJob {
        /// Training provider name.
        provider: String,
        /// Number of training examples.
        dataset_size: usize,
    },
    /// Access a GPIO pin on the host system.
    GpioAccess {
        /// GPIO chip number.
        chip: u32,
        /// GPIO line number.
        line: u32,
        /// Direction (input/output).
        direction: String,
        /// Agent requesting access.
        agent_id: String,
    },
    /// Attempt crash recovery for a self-improvement session.
    CrashRecovery {
        /// Unique crash identifier.
        crash_id: String,
        /// Strategy to apply for recovery.
        fix_strategy: String,
    },
    /// Execute a scheduled autonomous task.
    ScheduledTask {
        /// Name of the scheduled task.
        name: String,
        /// Type of task being scheduled.
        task_type: String,
    },
    /// React to a file system change.
    ReactToFileChange {
        /// Path of the changed file.
        path: String,
        /// Type of change detected.
        event_type: String,
    },
    /// Manage a system service (systemd, docker, process).
    ManageService {
        /// Name of the service.
        name: String,
        /// Operation to perform (start, stop, restart, etc.).
        operation: String,
        /// Type of service (systemd, docker, process).
        service_type: String,
    },
}

impl fmt::Display for AutonomousOperation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::StartImprovement { strategy, .. } => write!(f, "start improvement: {strategy}"),
            Self::CommitChanges { diff_lines, .. } => {
                write!(f, "commit {diff_lines} changed lines")
            }
            Self::CreatePullRequest { title, .. } => write!(f, "create PR: {title}"),
            Self::MergePullRequest { pr_id, .. } => write!(f, "merge PR: {pr_id}"),
            Self::SpawnAgent { description } => write!(f, "spawn agent: {description}"),
            Self::RestartAgent { agent_id, .. } => write!(f, "restart agent: {agent_id}"),
            Self::StartTrainingJob { provider, .. } => write!(f, "training job: {provider}"),
            Self::GpioAccess {
                chip,
                line,
                direction,
                ..
            } => write!(f, "GPIO access: chip{chip}/line{line} ({direction})"),
            Self::CrashRecovery {
                crash_id,
                fix_strategy,
            } => {
                write!(f, "crash recovery {crash_id}: {fix_strategy}")
            }
            Self::ScheduledTask { name, task_type } => {
                write!(f, "scheduled task: {name} ({task_type})")
            }
            Self::ReactToFileChange { path, event_type } => {
                write!(f, "react to {event_type}: {path}")
            }
            Self::ManageService {
                name,
                operation,
                service_type,
            } => write!(f, "{operation} {service_type} service: {name}"),
        }
    }
}

// ── Approval policy ─────────────────────────────────────────────────────────

/// Gate for autonomous operations — implementations decide whether to allow
/// or reject an operation before it executes.
///
/// Used by the safety guard, pipeline, and supervisor to enforce approval
/// requirements on sensitive operations (PR creation, merging, agent restarts).
#[async_trait]
pub trait ApprovalPolicy: Send + Sync {
    /// Check whether the given operation is allowed.
    async fn check(&self, op: &AutonomousOperation) -> Result<(), SafetyStop>;
}

/// Default policy that approves everything (for testing / trusted environments).
pub struct AlwaysApprove;

#[async_trait]
impl ApprovalPolicy for AlwaysApprove {
    async fn check(&self, _op: &AutonomousOperation) -> Result<(), SafetyStop> {
        Ok(())
    }
}

/// Policy that rejects all autonomous operations (require manual execution).
pub struct AlwaysReject;

#[async_trait]
impl ApprovalPolicy for AlwaysReject {
    async fn check(&self, op: &AutonomousOperation) -> Result<(), SafetyStop> {
        Err(SafetyStop::OperationRejected(format!(
            "Manual approval required for: {op}"
        )))
    }
}

// ── Circuit breaker ─────────────────────────────────────────────────────────

/// Circuit breaker state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CircuitBreakerState {
    /// Normal operation — failures are counted.
    Closed,
    /// Tripped — all operations rejected until cooldown expires.
    Open,
    /// Cooldown expired — allow one probe operation.
    HalfOpen,
}

/// Circuit breaker that trips after consecutive failures and resets after a cooldown period.
///
/// Follows the closed -> open -> half-open pattern: closed allows operations,
/// open rejects them, and half-open permits a single probe after cooldown.
#[derive(Debug)]
pub struct CircuitBreaker {
    state: CircuitBreakerState,
    consecutive_failures: u32,
    threshold: u32,
    cooldown_secs: u64,
    last_failure: Option<Instant>,
}

impl CircuitBreaker {
    /// Create a new circuit breaker with the given failure threshold and cooldown.
    pub fn new(threshold: u32, cooldown_secs: u64) -> Self {
        Self {
            state: CircuitBreakerState::Closed,
            consecutive_failures: 0,
            threshold,
            cooldown_secs,
            last_failure: None,
        }
    }

    /// Check if the circuit breaker allows operations.
    pub fn check(&mut self) -> Result<(), SafetyStop> {
        match self.state {
            CircuitBreakerState::Closed => Ok(()),
            CircuitBreakerState::Open => {
                // Check if cooldown has expired
                if let Some(last) = self.last_failure
                    && last.elapsed().as_secs() >= self.cooldown_secs
                {
                    self.state = CircuitBreakerState::HalfOpen;
                    tracing::info!("Circuit breaker entering half-open state");
                    return Ok(());
                }
                Err(SafetyStop::CircuitBreakerTripped(self.consecutive_failures))
            }
            CircuitBreakerState::HalfOpen => {
                // Allow one probe operation
                Ok(())
            }
        }
    }

    /// Record a successful operation.
    pub fn record_success(&mut self) {
        self.consecutive_failures = 0;
        self.state = CircuitBreakerState::Closed;
    }

    /// Record a failed operation.
    pub fn record_failure(&mut self) {
        self.consecutive_failures += 1;
        self.last_failure = Some(Instant::now());

        if self.consecutive_failures >= self.threshold {
            self.state = CircuitBreakerState::Open;
            tracing::warn!(
                "Circuit breaker tripped after {} consecutive failures",
                self.consecutive_failures
            );
        }
    }

    /// Return the current circuit breaker state.
    pub fn state(&self) -> CircuitBreakerState {
        self.state
    }

    /// Return the number of consecutive failures recorded.
    pub fn consecutive_failures(&self) -> u32 {
        self.consecutive_failures
    }
}

// ── Budget tracker ──────────────────────────────────────────────────────────

/// Tracks spending and enforces budget limits.
///
/// Enforces three constraints: total cumulative cost, per-operation cost ceiling,
/// and a daily operation count limit that resets at midnight UTC.
#[derive(Debug)]
pub struct BudgetTracker {
    total_cost: f64,
    max_total: f64,
    max_per_op: f64,
    daily_operations: u32,
    max_daily_operations: u32,
    day_start: DateTime<Utc>,
}

impl BudgetTracker {
    /// Create a new budget tracker with the given limits.
    pub fn new(max_total: f64, max_per_op: f64, max_daily_operations: u32) -> Self {
        Self {
            total_cost: 0.0,
            max_total,
            max_per_op,
            daily_operations: 0,
            max_daily_operations,
            day_start: Utc::now(),
        }
    }

    /// Check if the budget allows an operation with the estimated cost.
    pub fn check(&mut self, estimated_cost: f64) -> Result<(), SafetyStop> {
        self.maybe_reset_daily();

        if self.total_cost + estimated_cost > self.max_total {
            return Err(SafetyStop::BudgetExceeded(self.total_cost));
        }
        if estimated_cost > self.max_per_op {
            return Err(SafetyStop::BudgetExceeded(estimated_cost));
        }
        if self.daily_operations >= self.max_daily_operations {
            return Err(SafetyStop::DailyLimitReached(self.daily_operations));
        }
        Ok(())
    }

    /// Record an operation's actual cost.
    pub fn record_cost(&mut self, cost: f64) {
        self.total_cost += cost;
        self.daily_operations += 1;
    }

    /// Return the total cost incurred so far.
    pub fn total_cost(&self) -> f64 {
        self.total_cost
    }

    /// Return the number of operations performed today.
    pub fn daily_operations(&self) -> u32 {
        self.daily_operations
    }

    fn maybe_reset_daily(&mut self) {
        let now = Utc::now();
        if now.date_naive() != self.day_start.date_naive() {
            self.daily_operations = 0;
            self.day_start = now;
        }
    }
}

// ── Dead man's switch ───────────────────────────────────────────────────────

/// Dead man's switch that trips if no heartbeat is received within the timeout.
#[derive(Debug)]
pub struct DeadManSwitch {
    last_heartbeat: Instant,
    timeout_secs: u64,
}

impl DeadManSwitch {
    /// Create a new dead man's switch with the given timeout in seconds.
    pub fn new(timeout_secs: u64) -> Self {
        Self {
            last_heartbeat: Instant::now(),
            timeout_secs,
        }
    }

    /// Send a heartbeat to keep the switch alive.
    pub fn heartbeat(&mut self) {
        self.last_heartbeat = Instant::now();
    }

    /// Check if the switch has timed out.
    pub fn check(&self) -> Result<(), SafetyStop> {
        if self.last_heartbeat.elapsed().as_secs() >= self.timeout_secs {
            Err(SafetyStop::HeartbeatTimeout)
        } else {
            Ok(())
        }
    }

    /// Return the number of seconds since the last heartbeat.
    pub fn elapsed_secs(&self) -> u64 {
        self.last_heartbeat.elapsed().as_secs()
    }
}

// ── SafetyGuard (composite) ─────────────────────────────────────────────────

/// Composite safety guard combining circuit breaker, budget tracker, diff limits,
/// and dead man's switch.
pub struct SafetyGuard {
    circuit_breaker: CircuitBreaker,
    budget: BudgetTracker,
    dead_man: DeadManSwitch,
    total_diff_lines: u32,
    max_total_diff: u32,
    cycles_completed: u32,
    max_cycles: u32,
    approval_policy: Arc<dyn ApprovalPolicy>,
}

impl SafetyGuard {
    /// Create from a full SafetyConfig.
    pub fn from_config(config: &SafetyConfig, max_cycles: u32) -> Self {
        Self {
            circuit_breaker: CircuitBreaker::new(
                config.circuit_breaker_threshold,
                config.circuit_breaker_cooldown_secs,
            ),
            budget: BudgetTracker::new(
                config.max_total_cost,
                config.max_per_operation_cost,
                config.max_daily_operations,
            ),
            dead_man: DeadManSwitch::new(config.heartbeat_timeout_secs),
            total_diff_lines: 0,
            max_total_diff: config.max_total_diff,
            cycles_completed: 0,
            max_cycles,
            approval_policy: Arc::new(AlwaysApprove),
        }
    }

    /// Create from the simpler SelfImprovementConfig (backwards-compatible).
    pub fn new(config: &SelfImprovementConfig) -> Self {
        Self {
            circuit_breaker: CircuitBreaker::new(config.circuit_breaker_threshold, 300),
            budget: BudgetTracker::new(config.max_budget, config.max_budget, 1000),
            dead_man: DeadManSwitch::new(1800),
            total_diff_lines: 0,
            max_total_diff: config.max_total_diff,
            cycles_completed: 0,
            max_cycles: config.max_cycles,
            approval_policy: Arc::new(AlwaysApprove),
        }
    }

    /// Set the approval policy.
    pub fn with_approval_policy(mut self, policy: Arc<dyn ApprovalPolicy>) -> Self {
        self.approval_policy = policy;
        self
    }

    /// Check all safety constraints before continuing.
    pub fn check_can_continue(&mut self) -> Result<(), SafetyStop> {
        self.circuit_breaker.check()?;
        self.budget.check(0.0)?;
        self.dead_man.check()?;

        if self.cycles_completed >= self.max_cycles {
            return Err(SafetyStop::CycleLimitReached(self.cycles_completed));
        }
        if self.total_diff_lines >= self.max_total_diff {
            return Err(SafetyStop::DiffLimitExceeded(self.total_diff_lines));
        }
        Ok(())
    }

    /// Check approval policy for an operation.
    pub async fn check_approval(&self, op: &AutonomousOperation) -> Result<(), SafetyStop> {
        self.approval_policy.check(op).await
    }

    /// Record a successful cycle.
    pub fn record_success(&mut self, diff_lines: u32) {
        self.circuit_breaker.record_success();
        self.total_diff_lines += diff_lines;
        self.cycles_completed += 1;
    }

    /// Record a failed cycle.
    pub fn record_failure(&mut self) {
        self.circuit_breaker.record_failure();
        self.cycles_completed += 1;
    }

    /// Record cost for budget tracking.
    pub fn record_cost(&mut self, cost: f64) {
        self.budget.record_cost(cost);
    }

    /// Send heartbeat to dead man's switch.
    pub fn heartbeat(&mut self) {
        self.dead_man.heartbeat();
    }

    /// Return the number of cycles completed.
    pub fn cycles_completed(&self) -> u32 {
        self.cycles_completed
    }

    /// Return the total cost tracked by the budget.
    pub fn total_cost(&self) -> f64 {
        self.budget.total_cost()
    }

    /// Return the total number of diff lines accumulated.
    pub fn total_diff_lines(&self) -> u32 {
        self.total_diff_lines
    }

    /// Return the current circuit breaker state.
    pub fn circuit_breaker_state(&self) -> CircuitBreakerState {
        self.circuit_breaker.state()
    }
}

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

    #[test]
    fn test_circuit_breaker_trips() {
        let mut cb = CircuitBreaker::new(3, 60);
        assert_eq!(cb.state(), CircuitBreakerState::Closed);

        cb.record_failure();
        cb.record_failure();
        assert_eq!(cb.state(), CircuitBreakerState::Closed);

        cb.record_failure();
        assert_eq!(cb.state(), CircuitBreakerState::Open);
        assert!(cb.check().is_err());
    }

    #[test]
    fn test_circuit_breaker_resets_on_success() {
        let mut cb = CircuitBreaker::new(3, 60);
        cb.record_failure();
        cb.record_failure();
        cb.record_success();
        assert_eq!(cb.consecutive_failures(), 0);
        assert_eq!(cb.state(), CircuitBreakerState::Closed);
    }

    #[test]
    fn test_budget_tracker() {
        let mut bt = BudgetTracker::new(10.0, 5.0, 100);
        assert!(bt.check(3.0).is_ok());
        bt.record_cost(3.0);
        assert!(bt.check(3.0).is_ok());
        bt.record_cost(3.0);
        // Now at $6, trying to add $5 would exceed $10
        assert!(bt.check(5.0).is_err());
    }

    #[test]
    fn test_dead_man_switch() {
        let dms = DeadManSwitch::new(1800);
        assert!(dms.check().is_ok());
    }

    #[test]
    fn test_safety_guard_cycle_limit() {
        let config = SelfImprovementConfig {
            max_cycles: 2,
            ..Default::default()
        };
        let mut guard = SafetyGuard::new(&config);
        assert!(guard.check_can_continue().is_ok());
        guard.record_success(10);
        assert!(guard.check_can_continue().is_ok());
        guard.record_success(10);
        assert!(guard.check_can_continue().is_err());
    }
}