brainwires-agents 0.7.0

Agent orchestration, coordination, and lifecycle management for the Brainwires Agent Framework
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
//! Git operation coordination for multi-agent systems
//!
//! Maps git tool operations to their required resource locks and
//! coordinates with the resource lock manager to ensure safe concurrent access.
//!
//! ## Lock Requirements by Git Operation
//!
//! | Git Tool | Required Locks | Notes |
//! |----------|---------------|-------|
//! | git_status, git_diff, git_log, git_search, git_fetch | None | Read-only |
//! | git_stage, git_unstage | GitIndex | Modifies staging area |
//! | git_commit | GitIndex, GitCommit | Creates commit |
//! | git_push | GitRemoteWrite | Writes to remote |
//! | git_pull | GitRemoteMerge, GitIndex | Reads remote, modifies working tree |
//! | git_branch | GitBranch | Branch operations |
//! | git_discard | GitDestructive, GitIndex | Dangerous: loses changes |

use std::path::PathBuf;
use std::sync::Arc;

use anyhow::Result;

use crate::communication::{AgentMessage, CommunicationHub, GitOperationType};
use crate::resource_checker::ResourceChecker;
use crate::resource_locks::{ResourceLockGuard, ResourceLockManager, ResourceScope, ResourceType};

/// Git tool name constants
pub mod git_tools {
    /// Git status tool name.
    pub const STATUS: &str = "git_status";
    /// Git diff tool name.
    pub const DIFF: &str = "git_diff";
    /// Git log tool name.
    pub const LOG: &str = "git_log";
    /// Git search tool name.
    pub const SEARCH: &str = "git_search";
    /// Git fetch tool name.
    pub const FETCH: &str = "git_fetch";
    /// Git stage tool name.
    pub const STAGE: &str = "git_stage";
    /// Git unstage tool name.
    pub const UNSTAGE: &str = "git_unstage";
    /// Git commit tool name.
    pub const COMMIT: &str = "git_commit";
    /// Git push tool name.
    pub const PUSH: &str = "git_push";
    /// Git pull tool name.
    pub const PULL: &str = "git_pull";
    /// Git branch tool name.
    pub const BRANCH: &str = "git_branch";
    /// Git discard tool name.
    pub const DISCARD: &str = "git_discard";
}

/// Lock requirements for a git operation
#[derive(Debug, Clone)]
pub struct GitLockRequirements {
    /// Primary resource types needed
    pub resource_types: Vec<ResourceType>,
    /// Whether to check for file write conflicts
    pub check_file_conflicts: bool,
    /// Whether to check for build conflicts
    pub check_build_conflicts: bool,
    /// Git operation type for messaging
    pub operation_type: GitOperationType,
    /// Human-readable description
    pub description: &'static str,
}

impl GitLockRequirements {
    /// Returns true if no locks are needed (read-only operation)
    pub fn is_read_only(&self) -> bool {
        self.resource_types.is_empty()
    }
}

/// Get the lock requirements for a git tool
pub fn get_lock_requirements(tool_name: &str) -> GitLockRequirements {
    match tool_name {
        // Read-only operations - no locks needed
        git_tools::STATUS | git_tools::DIFF | git_tools::LOG | git_tools::SEARCH => {
            GitLockRequirements {
                resource_types: vec![],
                check_file_conflicts: false,
                check_build_conflicts: false,
                operation_type: GitOperationType::ReadOnly,
                description: "Read-only git operation",
            }
        }
        git_tools::FETCH => GitLockRequirements {
            resource_types: vec![],
            check_file_conflicts: false,
            check_build_conflicts: false,
            operation_type: GitOperationType::ReadOnly,
            description: "Fetch from remote",
        },

        // Staging operations
        git_tools::STAGE | git_tools::UNSTAGE => GitLockRequirements {
            resource_types: vec![ResourceType::GitIndex],
            check_file_conflicts: true, // Wait for files being edited
            check_build_conflicts: false,
            operation_type: GitOperationType::Staging,
            description: "Staging area modification",
        },

        // Commit operation
        git_tools::COMMIT => GitLockRequirements {
            resource_types: vec![ResourceType::GitIndex, ResourceType::GitCommit],
            check_file_conflicts: true,  // Wait for files being edited
            check_build_conflicts: true, // Wait for builds to complete
            operation_type: GitOperationType::Commit,
            description: "Create commit",
        },

        // Remote write operation
        git_tools::PUSH => GitLockRequirements {
            resource_types: vec![ResourceType::GitRemoteWrite],
            check_file_conflicts: false,
            check_build_conflicts: true, // Don't push during active build
            operation_type: GitOperationType::RemoteWrite,
            description: "Push to remote",
        },

        // Remote merge operation
        git_tools::PULL => GitLockRequirements {
            resource_types: vec![ResourceType::GitRemoteMerge, ResourceType::GitIndex],
            check_file_conflicts: true, // Wait for files being edited (pull modifies working tree)
            check_build_conflicts: true, // Don't pull during active build
            operation_type: GitOperationType::RemoteMerge,
            description: "Pull from remote",
        },

        // Branch operation
        git_tools::BRANCH => GitLockRequirements {
            resource_types: vec![ResourceType::GitBranch],
            check_file_conflicts: false,
            check_build_conflicts: false,
            operation_type: GitOperationType::Branch,
            description: "Branch operation",
        },

        // Destructive operation
        git_tools::DISCARD => GitLockRequirements {
            resource_types: vec![ResourceType::GitDestructive, ResourceType::GitIndex],
            check_file_conflicts: true,  // Wait for files being edited
            check_build_conflicts: true, // Wait for builds
            operation_type: GitOperationType::Destructive,
            description: "Discard changes (destructive)",
        },

        // Unknown operation - treat as read-only for safety
        _ => GitLockRequirements {
            resource_types: vec![],
            check_file_conflicts: false,
            check_build_conflicts: false,
            operation_type: GitOperationType::ReadOnly,
            description: "Unknown git operation",
        },
    }
}

/// Coordinator for git operations across agents
pub struct GitCoordinator {
    resource_locks: Arc<ResourceLockManager>,
    resource_checker: Option<Arc<ResourceChecker>>,
    communication_hub: Option<Arc<CommunicationHub>>,
    project_root: PathBuf,
}

impl GitCoordinator {
    /// Create a new git coordinator
    pub fn new(resource_locks: Arc<ResourceLockManager>, project_root: PathBuf) -> Self {
        Self {
            resource_locks,
            resource_checker: None,
            communication_hub: None,
            project_root,
        }
    }

    /// Create a git coordinator with full integration
    pub fn with_full_integration(
        resource_locks: Arc<ResourceLockManager>,
        resource_checker: Arc<ResourceChecker>,
        communication_hub: Arc<CommunicationHub>,
        project_root: PathBuf,
    ) -> Self {
        Self {
            resource_locks,
            resource_checker: Some(resource_checker),
            communication_hub: Some(communication_hub),
            project_root,
        }
    }

    /// Get the project scope for this coordinator
    pub fn project_scope(&self) -> ResourceScope {
        ResourceScope::Project(self.project_root.clone())
    }

    /// Acquire all locks needed for a git operation
    ///
    /// Returns a vector of lock guards that must be held during the operation.
    /// The guards are released when dropped.
    #[tracing::instrument(name = "agent.git.acquire", skip(self))]
    pub async fn acquire_for_git_op(
        &self,
        agent_id: &str,
        tool_name: &str,
    ) -> Result<GitOperationLocks> {
        let requirements = get_lock_requirements(tool_name);

        // If read-only, no locks needed
        if requirements.is_read_only() {
            return Ok(GitOperationLocks {
                guards: vec![],
                operation_type: requirements.operation_type,
                description: requirements.description.to_string(),
            });
        }

        let scope = self.project_scope();

        // Check for cross-resource conflicts if resource checker is available
        if let Some(checker) = &self.resource_checker {
            // Check file conflicts
            if requirements.check_file_conflicts {
                let git_op_type = match requirements.operation_type {
                    GitOperationType::Staging => ResourceType::GitIndex,
                    GitOperationType::Commit => ResourceType::GitCommit,
                    GitOperationType::RemoteWrite => ResourceType::GitRemoteWrite,
                    GitOperationType::RemoteMerge => ResourceType::GitRemoteMerge,
                    GitOperationType::Branch => ResourceType::GitBranch,
                    GitOperationType::Destructive => ResourceType::GitDestructive,
                    GitOperationType::ReadOnly => ResourceType::GitIndex, // fallback
                };

                let conflict_check = checker
                    .can_start_git_operation(git_op_type, &scope, agent_id)
                    .await;

                if conflict_check.is_blocked() {
                    let conflicts: Vec<String> = conflict_check
                        .conflicts()
                        .iter()
                        .map(|c| format!("{}: {} by {}", c.resource, c.status, c.holder_agent))
                        .collect();

                    return Err(anyhow::anyhow!(
                        "Git operation blocked by conflicts: {}",
                        conflicts.join(", ")
                    ));
                }
            }
        }

        // Broadcast operation start if communication hub is available
        if let Some(hub) = &self.communication_hub {
            let _ = hub
                .broadcast(
                    agent_id.to_string(),
                    AgentMessage::GitOperationStarted {
                        agent_id: agent_id.to_string(),
                        git_op: requirements.operation_type,
                        branch: None, // Could be enhanced to include branch info
                        description: requirements.description.to_string(),
                    },
                )
                .await;
        }

        // Acquire all required locks
        let mut guards = Vec::new();
        for resource_type in &requirements.resource_types {
            let guard = self
                .resource_locks
                .acquire_resource(
                    agent_id,
                    *resource_type,
                    scope.clone(),
                    requirements.description,
                )
                .await?;
            guards.push(guard);
        }

        Ok(GitOperationLocks {
            guards,
            operation_type: requirements.operation_type,
            description: requirements.description.to_string(),
        })
    }

    /// Check if a git operation can be performed without blocking
    pub async fn can_perform_git_op(&self, agent_id: &str, tool_name: &str) -> bool {
        let requirements = get_lock_requirements(tool_name);

        // Read-only operations can always proceed
        if requirements.is_read_only() {
            return true;
        }

        let scope = self.project_scope();

        // Check resource locks
        for resource_type in &requirements.resource_types {
            if !self
                .resource_locks
                .can_acquire(agent_id, *resource_type, &scope)
                .await
            {
                return false;
            }
        }

        // Check cross-resource conflicts
        if let Some(checker) = &self.resource_checker
            && requirements.check_file_conflicts
        {
            let git_op_type = match requirements.operation_type {
                GitOperationType::Staging => ResourceType::GitIndex,
                GitOperationType::Commit => ResourceType::GitCommit,
                GitOperationType::RemoteWrite => ResourceType::GitRemoteWrite,
                GitOperationType::RemoteMerge => ResourceType::GitRemoteMerge,
                GitOperationType::Branch => ResourceType::GitBranch,
                GitOperationType::Destructive => ResourceType::GitDestructive,
                GitOperationType::ReadOnly => return true,
            };

            let check = checker
                .can_start_git_operation(git_op_type, &scope, agent_id)
                .await;

            if check.is_blocked() {
                return false;
            }
        }

        true
    }

    /// Broadcast that a git operation has completed
    pub async fn broadcast_completion(
        &self,
        agent_id: &str,
        operation_type: GitOperationType,
        success: bool,
        summary: &str,
    ) {
        if let Some(hub) = &self.communication_hub {
            let _ = hub
                .broadcast(
                    agent_id.to_string(),
                    AgentMessage::GitOperationCompleted {
                        agent_id: agent_id.to_string(),
                        git_op: operation_type,
                        success,
                        summary: summary.to_string(),
                    },
                )
                .await;
        }
    }
}

/// Holds locks for a git operation
///
/// The locks are released when this struct is dropped.
pub struct GitOperationLocks {
    guards: Vec<ResourceLockGuard>,
    /// Type of git operation.
    pub operation_type: GitOperationType,
    /// Human-readable description.
    pub description: String,
}

impl GitOperationLocks {
    /// Returns true if this represents a read-only operation (no locks held)
    pub fn is_read_only(&self) -> bool {
        self.guards.is_empty()
    }

    /// Get the number of locks held
    pub fn lock_count(&self) -> usize {
        self.guards.len()
    }
}

/// Helper trait for git operations with automatic lock management
#[async_trait::async_trait]
pub trait GitOperationRunner {
    /// Run a git operation with automatic lock acquisition and release
    async fn run_with_locks<F, T>(
        &self,
        agent_id: &str,
        tool_name: &str,
        operation: F,
    ) -> Result<T>
    where
        F: FnOnce() -> Result<T> + Send,
        T: Send;
}

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

    #[test]
    fn test_read_only_operations() {
        assert!(get_lock_requirements(git_tools::STATUS).is_read_only());
        assert!(get_lock_requirements(git_tools::DIFF).is_read_only());
        assert!(get_lock_requirements(git_tools::LOG).is_read_only());
        assert!(get_lock_requirements(git_tools::SEARCH).is_read_only());
        assert!(get_lock_requirements(git_tools::FETCH).is_read_only());
    }

    #[test]
    fn test_staging_operations() {
        let stage_req = get_lock_requirements(git_tools::STAGE);
        assert!(!stage_req.is_read_only());
        assert!(stage_req.resource_types.contains(&ResourceType::GitIndex));
        assert!(matches!(
            stage_req.operation_type,
            GitOperationType::Staging
        ));

        let unstage_req = get_lock_requirements(git_tools::UNSTAGE);
        assert!(!unstage_req.is_read_only());
        assert!(unstage_req.resource_types.contains(&ResourceType::GitIndex));
    }

    #[test]
    fn test_commit_operation() {
        let req = get_lock_requirements(git_tools::COMMIT);
        assert!(!req.is_read_only());
        assert!(req.resource_types.contains(&ResourceType::GitIndex));
        assert!(req.resource_types.contains(&ResourceType::GitCommit));
        assert!(req.check_file_conflicts);
        assert!(req.check_build_conflicts);
        assert!(matches!(req.operation_type, GitOperationType::Commit));
    }

    #[test]
    fn test_push_operation() {
        let req = get_lock_requirements(git_tools::PUSH);
        assert!(!req.is_read_only());
        assert!(req.resource_types.contains(&ResourceType::GitRemoteWrite));
        assert!(!req.check_file_conflicts);
        assert!(req.check_build_conflicts);
        assert!(matches!(req.operation_type, GitOperationType::RemoteWrite));
    }

    #[test]
    fn test_pull_operation() {
        let req = get_lock_requirements(git_tools::PULL);
        assert!(!req.is_read_only());
        assert!(req.resource_types.contains(&ResourceType::GitRemoteMerge));
        assert!(req.resource_types.contains(&ResourceType::GitIndex));
        assert!(req.check_file_conflicts);
        assert!(req.check_build_conflicts);
        assert!(matches!(req.operation_type, GitOperationType::RemoteMerge));
    }

    #[test]
    fn test_destructive_operation() {
        let req = get_lock_requirements(git_tools::DISCARD);
        assert!(!req.is_read_only());
        assert!(req.resource_types.contains(&ResourceType::GitDestructive));
        assert!(req.resource_types.contains(&ResourceType::GitIndex));
        assert!(req.check_file_conflicts);
        assert!(req.check_build_conflicts);
        assert!(matches!(req.operation_type, GitOperationType::Destructive));
    }

    #[test]
    fn test_unknown_operation() {
        let req = get_lock_requirements("unknown_git_tool");
        assert!(req.is_read_only());
    }

    #[tokio::test]
    async fn test_coordinator_read_only_no_locks() {
        let resource_locks = Arc::new(ResourceLockManager::new());
        let coordinator = GitCoordinator::new(resource_locks, PathBuf::from("/test/project"));

        let locks = coordinator
            .acquire_for_git_op("agent-1", git_tools::STATUS)
            .await
            .unwrap();

        assert!(locks.is_read_only());
        assert_eq!(locks.lock_count(), 0);
    }

    #[tokio::test]
    async fn test_coordinator_staging_acquires_index_lock() {
        let resource_locks = Arc::new(ResourceLockManager::new());
        let coordinator =
            GitCoordinator::new(resource_locks.clone(), PathBuf::from("/test/project"));

        let locks = coordinator
            .acquire_for_git_op("agent-1", git_tools::STAGE)
            .await
            .unwrap();

        assert!(!locks.is_read_only());
        assert_eq!(locks.lock_count(), 1);
        assert!(matches!(locks.operation_type, GitOperationType::Staging));

        // Verify the lock is held
        let scope = ResourceScope::Project(PathBuf::from("/test/project"));
        assert!(
            !resource_locks
                .can_acquire("agent-2", ResourceType::GitIndex, &scope)
                .await
        );
    }

    #[tokio::test]
    async fn test_coordinator_commit_acquires_multiple_locks() {
        let resource_locks = Arc::new(ResourceLockManager::new());
        let coordinator =
            GitCoordinator::new(resource_locks.clone(), PathBuf::from("/test/project"));

        let locks = coordinator
            .acquire_for_git_op("agent-1", git_tools::COMMIT)
            .await
            .unwrap();

        assert!(!locks.is_read_only());
        assert_eq!(locks.lock_count(), 2); // GitIndex + GitCommit
        assert!(matches!(locks.operation_type, GitOperationType::Commit));
    }

    #[tokio::test]
    async fn test_can_perform_git_op() {
        let resource_locks = Arc::new(ResourceLockManager::new());
        let coordinator =
            GitCoordinator::new(resource_locks.clone(), PathBuf::from("/test/project"));

        // Read-only should always be allowed
        assert!(
            coordinator
                .can_perform_git_op("agent-1", git_tools::STATUS)
                .await
        );

        // Staging should be allowed when no conflicts
        assert!(
            coordinator
                .can_perform_git_op("agent-1", git_tools::STAGE)
                .await
        );

        // Agent 1 acquires the index lock
        let _locks = coordinator
            .acquire_for_git_op("agent-1", git_tools::STAGE)
            .await
            .unwrap();

        // Agent 2 should not be able to stage
        assert!(
            !coordinator
                .can_perform_git_op("agent-2", git_tools::STAGE)
                .await
        );

        // But agent 1 can (idempotent)
        assert!(
            coordinator
                .can_perform_git_op("agent-1", git_tools::STAGE)
                .await
        );
    }
}