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
//! Configuration types for autonomous operations.

use serde::{Deserialize, Serialize};

/// Default dead man's switch heartbeat timeout in seconds (30 minutes).
const DEFAULT_HEARTBEAT_TIMEOUT_SECS: u64 = 1800;

/// Top-level configuration for the autonomy subsystem.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AutonomyConfig {
    /// Self-improvement session configuration.
    #[serde(default)]
    pub self_improve: SelfImprovementConfig,
    /// Safety and budget limits.
    #[serde(default)]
    pub safety: SafetyConfig,
    /// Git workflow configuration.
    #[serde(default)]
    pub git_workflow: GitWorkflowConfig,
    /// Crash recovery configuration.
    #[serde(default)]
    pub crash_recovery: CrashRecoveryConfig,
    /// GPIO hardware access configuration.
    #[serde(default)]
    pub gpio: GpioConfig,
}

/// Configuration for self-improvement sessions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SelfImprovementConfig {
    /// Maximum improvement cycles to run.
    pub max_cycles: u32,
    /// Maximum total cost in USD.
    pub max_budget: f64,
    /// If true, generate tasks but don't execute them.
    pub dry_run: bool,
    /// Enabled strategy names (empty = all).
    pub strategies: Vec<String>,
    /// Max iterations per agent task.
    pub agent_iterations: u32,
    /// Max diff lines per single task.
    pub max_diff_per_task: u32,
    /// Max total diff lines across entire session.
    pub max_total_diff: u32,
    /// Create PRs for committed changes.
    pub create_prs: bool,
    /// Git branch prefix for improvement branches.
    pub branch_prefix: String,
    /// Override model for agent tasks.
    pub model: Option<String>,
    /// Override provider.
    pub provider: Option<String>,
    /// Consecutive failures before circuit breaker trips.
    pub circuit_breaker_threshold: u32,
}

impl Default for SelfImprovementConfig {
    fn default() -> Self {
        Self {
            max_cycles: 10,
            max_budget: 10.0,
            dry_run: false,
            strategies: Vec::new(),
            agent_iterations: 25,
            max_diff_per_task: 200,
            max_total_diff: 1000,
            create_prs: false,
            branch_prefix: "self-improve/".to_string(),
            model: None,
            provider: None,
            circuit_breaker_threshold: 3,
        }
    }
}

impl SelfImprovementConfig {
    /// Check if a given strategy name is enabled (empty list = all enabled).
    pub fn is_strategy_enabled(&self, name: &str) -> bool {
        self.strategies.is_empty() || self.strategies.iter().any(|s| s == name)
    }
}

/// Per-strategy configuration passed to strategy task generators during scanning.
#[derive(Debug, Clone)]
pub struct StrategyConfig {
    /// Path to the repository root.
    pub repo_path: String,
    /// Maximum tasks to generate per strategy.
    pub max_tasks_per_strategy: usize,
}

impl Default for StrategyConfig {
    fn default() -> Self {
        Self {
            repo_path: ".".to_string(),
            max_tasks_per_strategy: 5,
        }
    }
}

/// Safety and budget configuration for autonomous operations.
///
/// Controls cost limits, operation quotas, circuit breaker behavior, and
/// file path restrictions that apply across all autonomous features.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SafetyConfig {
    /// Maximum total cost in USD across all operations.
    pub max_total_cost: f64,
    /// Maximum cost per single operation.
    pub max_per_operation_cost: f64,
    /// Maximum daily operations.
    pub max_daily_operations: u32,
    /// Consecutive failure threshold for circuit breaker.
    pub circuit_breaker_threshold: u32,
    /// Circuit breaker cooldown in seconds.
    pub circuit_breaker_cooldown_secs: u64,
    /// Max diff lines per task.
    pub max_diff_per_task: u32,
    /// Max total diff lines per session.
    pub max_total_diff: u32,
    /// Max concurrent agents.
    pub max_concurrent_agents: u32,
    /// Dead man's switch heartbeat timeout in seconds.
    pub heartbeat_timeout_secs: u64,
    /// Allowed path globs for file modifications.
    pub allowed_paths: Vec<String>,
    /// Forbidden path globs (takes precedence over allowed).
    pub forbidden_paths: Vec<String>,
}

impl Default for SafetyConfig {
    fn default() -> Self {
        Self {
            max_total_cost: 50.0,
            max_per_operation_cost: 5.0,
            max_daily_operations: 100,
            circuit_breaker_threshold: 3,
            circuit_breaker_cooldown_secs: 300,
            max_diff_per_task: 200,
            max_total_diff: 1000,
            max_concurrent_agents: 5,
            heartbeat_timeout_secs: DEFAULT_HEARTBEAT_TIMEOUT_SECS,
            allowed_paths: Vec::new(),
            forbidden_paths: Vec::new(),
        }
    }
}

/// Git workflow pipeline configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitWorkflowConfig {
    /// Branch prefix for autonomous fix branches.
    pub branch_prefix: String,
    /// Whether to auto-merge PRs when policy allows.
    pub auto_merge: bool,
    /// Default merge method.
    pub merge_method: String,
    /// Minimum investigation confidence to proceed with fix.
    pub min_confidence: f64,
    /// Webhook server configuration.
    #[serde(default)]
    pub webhook: WebhookConfig,
}

impl Default for GitWorkflowConfig {
    fn default() -> Self {
        Self {
            branch_prefix: "autonomy/".to_string(),
            auto_merge: false,
            merge_method: "squash".to_string(),
            min_confidence: 0.7,
            webhook: WebhookConfig::default(),
        }
    }
}

/// Webhook server configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebhookConfig {
    /// Listen address.
    pub listen_addr: String,
    /// Listen port.
    pub port: u16,
    /// Webhook secret for HMAC verification.
    pub secret: Option<String>,
    /// Directory for webhook event logs.
    #[serde(default = "default_webhook_log_dir")]
    pub log_dir: String,
    /// Number of days to keep webhook logs.
    #[serde(default = "default_webhook_keep_days")]
    pub keep_days: u32,
    /// Per-repository webhook configuration.
    #[serde(default)]
    pub repos: std::collections::HashMap<String, WebhookRepoConfig>,
}

fn default_webhook_log_dir() -> String {
    dirs::home_dir()
        .map(|h| {
            h.join(".brainwires")
                .join("webhook-logs")
                .to_string_lossy()
                .to_string()
        })
        .unwrap_or_else(|| "/tmp/brainwires/webhook-logs".to_string())
}

fn default_webhook_keep_days() -> u32 {
    30
}

impl Default for WebhookConfig {
    fn default() -> Self {
        Self {
            listen_addr: "0.0.0.0".to_string(),
            port: 3000,
            secret: None,
            log_dir: default_webhook_log_dir(),
            keep_days: default_webhook_keep_days(),
            repos: std::collections::HashMap::new(),
        }
    }
}

/// Per-repository webhook configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebhookRepoConfig {
    /// Which events to handle (e.g., "issues", "push", "pull_request").
    #[serde(default)]
    pub events: Vec<String>,
    /// Whether to automatically investigate issues.
    #[serde(default)]
    pub auto_investigate: bool,
    /// Whether to automatically apply fixes.
    #[serde(default)]
    pub auto_fix: bool,
    /// Whether to automatically merge PRs when policy allows.
    #[serde(default)]
    pub auto_merge: bool,
    /// Only handle issues with these labels (empty = all).
    #[serde(default)]
    pub labels_filter: Vec<String>,
    /// Commands to run after processing an event.
    #[serde(default)]
    pub post_commands: Vec<CommandConfig>,
}

impl Default for WebhookRepoConfig {
    fn default() -> Self {
        Self {
            events: vec!["issues".to_string()],
            auto_investigate: true,
            auto_fix: false,
            auto_merge: false,
            labels_filter: Vec::new(),
            post_commands: Vec::new(),
        }
    }
}

/// Command to execute with variable interpolation support.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommandConfig {
    /// Command to run.
    pub cmd: String,
    /// Arguments for the command.
    #[serde(default)]
    pub args: Vec<String>,
    /// Working directory (supports variables like `${REPO_NAME}`).
    #[serde(default)]
    pub working_dir: Option<String>,
}

/// Crash recovery configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrashRecoveryConfig {
    /// Maximum fix attempts before giving up.
    pub max_fix_attempts: u32,
    /// Path to crash recovery state file.
    pub state_file: String,
    /// Whether crash recovery is enabled.
    pub enabled: bool,
}

impl Default for CrashRecoveryConfig {
    fn default() -> Self {
        Self {
            max_fix_attempts: 3,
            state_file: dirs::home_dir()
                .map(|h| {
                    h.join(".brainwires")
                        .join("crash-recovery.json")
                        .to_string_lossy()
                        .to_string()
                })
                .unwrap_or_else(|| "/tmp/brainwires/crash-recovery.json".to_string()),
            enabled: true,
        }
    }
}

/// GPIO hardware access configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpioConfig {
    /// Allowed (chip, line) pairs — empty means no access.
    #[serde(default)]
    pub allowed_pins: Vec<(u32, u32)>,
    /// Maximum concurrent pins an agent may hold.
    pub max_concurrent_pins: usize,
    /// Timeout in seconds before auto-releasing a pin from an unhealthy agent.
    pub auto_release_timeout_secs: u64,
}

impl Default for GpioConfig {
    fn default() -> Self {
        Self {
            allowed_pins: Vec::new(),
            max_concurrent_pins: 4,
            auto_release_timeout_secs: 300,
        }
    }
}

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

    #[test]
    fn autonomy_config_default_succeeds() {
        let config = AutonomyConfig::default();
        // Verify field types are accessible
        let _si: &SelfImprovementConfig = &config.self_improve;
        let _safety: &SafetyConfig = &config.safety;
        let _git: &GitWorkflowConfig = &config.git_workflow;
    }

    #[test]
    fn self_improvement_config_default_has_sensible_values() {
        let config = SelfImprovementConfig::default();
        assert_eq!(config.max_cycles, 10);
        assert!((config.max_budget - 10.0).abs() < f64::EPSILON);
        assert!(!config.dry_run);
        assert!(config.strategies.is_empty());
        assert_eq!(config.agent_iterations, 25);
        assert_eq!(config.circuit_breaker_threshold, 3);
        assert_eq!(config.branch_prefix, "self-improve/");
        assert!(config.model.is_none());
        assert!(config.provider.is_none());
    }

    #[test]
    fn safety_config_default_has_sensible_values() {
        let config = SafetyConfig::default();
        assert!((config.max_total_cost - 50.0).abs() < f64::EPSILON);
        assert!((config.max_per_operation_cost - 5.0).abs() < f64::EPSILON);
        assert_eq!(config.max_daily_operations, 100);
        assert_eq!(config.circuit_breaker_threshold, 3);
        assert_eq!(config.circuit_breaker_cooldown_secs, 300);
        assert_eq!(config.max_concurrent_agents, 5);
        assert_eq!(config.heartbeat_timeout_secs, 1800);
        assert!(config.allowed_paths.is_empty());
        assert!(config.forbidden_paths.is_empty());
    }

    #[test]
    fn git_workflow_config_default_has_sensible_values() {
        let config = GitWorkflowConfig::default();
        assert_eq!(config.branch_prefix, "autonomy/");
        assert!(!config.auto_merge);
        assert_eq!(config.merge_method, "squash");
        assert!((config.min_confidence - 0.7).abs() < f64::EPSILON);
        assert_eq!(config.webhook.port, 3000);
    }

    #[test]
    fn serde_roundtrip_autonomy_config() {
        let config = AutonomyConfig::default();
        let json = serde_json::to_string(&config).expect("serialize");
        let deserialized: AutonomyConfig = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(
            deserialized.self_improve.max_cycles,
            config.self_improve.max_cycles
        );
        assert_eq!(
            deserialized.safety.max_total_cost,
            config.safety.max_total_cost
        );
        assert_eq!(
            deserialized.git_workflow.branch_prefix,
            config.git_workflow.branch_prefix
        );
    }

    #[test]
    fn is_strategy_enabled_empty_list_enables_all() {
        let config = SelfImprovementConfig::default();
        assert!(config.is_strategy_enabled("clippy"));
        assert!(config.is_strategy_enabled("anything"));
    }

    #[test]
    fn is_strategy_enabled_specific_list() {
        let config = SelfImprovementConfig {
            strategies: vec!["clippy".to_string(), "todo".to_string()],
            ..Default::default()
        };
        assert!(config.is_strategy_enabled("clippy"));
        assert!(config.is_strategy_enabled("todo"));
        assert!(!config.is_strategy_enabled("dead_code"));
    }
}