brainwires-agents 0.10.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
//! Integration tests for coordination patterns.
//!
//! Tests the ContractNet, Saga, and OptimisticConcurrency modules working
//! together and in realistic multi-step scenarios.

use brainwires_agents::contract_net::{
    BidEvaluationStrategy, ContractNetManager, ContractParticipant, TaskAnnouncement, TaskBid,
    TaskRequirements, TaskStatus,
};
use brainwires_agents::optimistic::{CommitResult, OptimisticController, ResolutionStrategy};
use brainwires_agents::saga::{NoOpCompensation, SagaExecutor, SagaOperationType, SagaStatus};
use std::sync::Arc;
use std::time::{Duration, Instant};

// ===========================================================================
// Contract-Net: full auction lifecycle with multiple participants
// ===========================================================================

#[tokio::test]
async fn contract_net_full_auction_lifecycle() {
    let manager = ContractNetManager::new();

    // Create participants with different capabilities
    let mut participant_rust =
        ContractParticipant::new("rust-agent", vec!["rust".to_string(), "cargo".to_string()])
            .with_max_concurrent(3);
    let mut participant_ts = ContractParticipant::new(
        "ts-agent",
        vec!["typescript".to_string(), "npm".to_string()],
    )
    .with_max_concurrent(2);
    let mut participant_full = ContractParticipant::new(
        "fullstack-agent",
        vec![
            "rust".to_string(),
            "typescript".to_string(),
            "cargo".to_string(),
        ],
    )
    .with_max_concurrent(2);

    // Connect participants
    participant_rust.connect(&manager);
    participant_ts.connect(&manager);
    participant_full.connect(&manager);

    // Announce a Rust task
    let announcement = TaskAnnouncement::new(
        "build-cache",
        "Implement LRU cache in Rust",
        "orchestrator",
        Instant::now() + Duration::from_secs(30),
    )
    .with_requirements(
        TaskRequirements::new()
            .with_capabilities(vec!["rust".to_string(), "cargo".to_string()])
            .with_complexity(7),
    );

    let task_id = manager.announce_task(announcement.clone()).await;

    // Check which participants should bid
    assert!(participant_rust.should_bid(&announcement).await);
    assert!(!participant_ts.should_bid(&announcement).await); // lacks rust
    assert!(participant_full.should_bid(&announcement).await);

    // Generate and submit bids
    let bid_rust = participant_rust.generate_bid(&announcement).await;
    let bid_full = participant_full.generate_bid(&announcement).await;

    assert_eq!(bid_rust.capability_score, 1.0); // has all required
    assert!(bid_full.capability_score > 0.0); // partial match (2/2 matched)

    manager.receive_bid(bid_rust).await.unwrap();
    manager.receive_bid(bid_full).await.unwrap();

    // Award
    let winner = manager.award_task(&task_id).await.unwrap();
    // Both have score 1.0 for capabilities, but load differs; just check we got a winner
    assert!(winner == "rust-agent" || winner == "fullstack-agent");

    // Accept
    manager.accept_award(&task_id, &winner).await.unwrap();
    assert_eq!(
        manager.get_task_status(&task_id).await,
        Some(TaskStatus::InProgress)
    );

    // Complete
    manager
        .complete_task(&task_id, &winner, true, Some("Cache implemented".into()))
        .await
        .unwrap();
    assert_eq!(
        manager.get_task_status(&task_id).await,
        Some(TaskStatus::Completed)
    );
}

#[tokio::test]
async fn contract_net_no_bids_results_in_no_award() {
    let manager = ContractNetManager::new();

    let announcement = TaskAnnouncement::new(
        "impossible-task",
        "Requires quantum computing",
        "orchestrator",
        Instant::now() + Duration::from_secs(30),
    );
    manager.announce_task(announcement).await;

    // No bids submitted
    let winner = manager.award_task("impossible-task").await;
    assert!(winner.is_none());
}

#[tokio::test]
async fn contract_net_load_balancing_strategy() {
    let manager = ContractNetManager::with_strategy(BidEvaluationStrategy::LoadBalancing);

    let announcement = TaskAnnouncement::new(
        "balanced-task",
        "Some work",
        "orchestrator",
        Instant::now() + Duration::from_secs(30),
    );
    manager.announce_task(announcement).await;

    // Agent with high load
    manager
        .receive_bid(
            TaskBid::new("busy-agent", "balanced-task")
                .with_capability_score(0.9)
                .with_load(0.9),
        )
        .await
        .unwrap();

    // Agent with low load
    manager
        .receive_bid(
            TaskBid::new("idle-agent", "balanced-task")
                .with_capability_score(0.5)
                .with_load(0.1),
        )
        .await
        .unwrap();

    let winner = manager.award_task("balanced-task").await;
    assert_eq!(winner, Some("idle-agent".to_string()));
}

#[tokio::test]
async fn contract_net_decline_and_reannounce() {
    let manager = ContractNetManager::new();

    let announcement = TaskAnnouncement::new(
        "flakey-task",
        "Might be declined",
        "orchestrator",
        Instant::now() + Duration::from_secs(30),
    );
    manager.announce_task(announcement).await;

    manager
        .receive_bid(TaskBid::new("agent-1", "flakey-task").with_capability_score(0.9))
        .await
        .unwrap();

    manager.award_task("flakey-task").await;
    manager
        .decline_award("flakey-task", "agent-1", "State changed since bid")
        .await
        .unwrap();

    // Task should no longer have an award
    // Re-announce would be a new announcement
    assert!(
        manager.get_task_status("flakey-task").await.is_none()
            || manager.get_task_status("flakey-task").await == Some(TaskStatus::OpenForBids)
    );
}

// ===========================================================================
// Saga: multi-step execution with compensation
// ===========================================================================

#[tokio::test]
async fn saga_multi_step_success_then_complete() {
    let saga = SagaExecutor::new("agent-1", "deploy pipeline");

    // Step 1: Generic operation
    let op1 = Arc::new(NoOpCompensation {
        description: "Compile code".into(),
        op_type: SagaOperationType::Build,
    });
    let r1 = saga.execute_step(op1).await.unwrap();
    assert!(r1.success);

    // Step 2: File write (compensable)
    let op2 = Arc::new(NoOpCompensation {
        description: "Write config".into(),
        op_type: SagaOperationType::FileWrite,
    });
    let r2 = saga.execute_step(op2).await.unwrap();
    assert!(r2.success);

    // Step 3: Git stage (compensable)
    let op3 = Arc::new(NoOpCompensation {
        description: "Stage changes".into(),
        op_type: SagaOperationType::GitStage,
    });
    let r3 = saga.execute_step(op3).await.unwrap();
    assert!(r3.success);

    assert_eq!(saga.operation_count().await, 3);

    saga.complete().await;
    assert_eq!(saga.status().await, SagaStatus::Completed);
}

#[tokio::test]
async fn saga_compensation_reverses_completed_operations() {
    let saga = SagaExecutor::new("agent-1", "risky deploy");

    // Execute two compensable steps
    saga.execute_step(Arc::new(NoOpCompensation {
        description: "Write file A".into(),
        op_type: SagaOperationType::FileWrite,
    }))
    .await
    .unwrap();

    saga.execute_step(Arc::new(NoOpCompensation {
        description: "Stage file A".into(),
        op_type: SagaOperationType::GitStage,
    }))
    .await
    .unwrap();

    // Something went wrong -- trigger compensation
    saga.fail().await;
    assert_eq!(saga.status().await, SagaStatus::Failed);

    let report = saga.compensate_all().await.unwrap();
    assert_eq!(saga.status().await, SagaStatus::Compensated);

    // Both operations should have been compensated (in reverse order)
    assert_eq!(report.operations.len(), 2);
    assert!(report.all_successful());
}

#[tokio::test]
async fn saga_skips_non_compensable_operations() {
    let saga = SagaExecutor::new("agent-1", "mixed ops");

    // Non-compensable (Build)
    saga.execute_step(Arc::new(NoOpCompensation {
        description: "Build project".into(),
        op_type: SagaOperationType::Build,
    }))
    .await
    .unwrap();

    // Compensable (FileWrite)
    saga.execute_step(Arc::new(NoOpCompensation {
        description: "Write output".into(),
        op_type: SagaOperationType::FileWrite,
    }))
    .await
    .unwrap();

    saga.fail().await;
    let report = saga.compensate_all().await.unwrap();

    // Should have 2 entries: 1 compensated, 1 skipped
    assert_eq!(report.operations.len(), 2);
    assert!(report.all_successful()); // skipped counts as successful
    assert!(report.summary().contains("1 skipped"));
    assert!(report.summary().contains("1 successful"));
}

#[tokio::test]
async fn saga_cannot_execute_after_failure() {
    let saga = SagaExecutor::new("agent-1", "stopped saga");

    saga.fail().await;

    let result = saga
        .execute_step(Arc::new(NoOpCompensation {
            description: "Should not run".into(),
            op_type: SagaOperationType::Generic,
        }))
        .await;

    assert!(result.is_err());
}

// ===========================================================================
// Optimistic concurrency: multi-agent conflict scenarios
// ===========================================================================

#[tokio::test]
async fn optimistic_sequential_commits_succeed() {
    let controller = OptimisticController::new();

    // Agent 1 edits file
    let token1 = controller.begin_optimistic("agent-1", "config.toml").await;
    let v1 = controller
        .commit_optimistic(token1, "hash-v1")
        .await
        .unwrap();
    assert_eq!(v1, 1);

    // Agent 2 edits same file after agent 1
    let token2 = controller.begin_optimistic("agent-2", "config.toml").await;
    assert_eq!(token2.base_version, 1); // sees agent-1's commit
    let v2 = controller
        .commit_optimistic(token2, "hash-v2")
        .await
        .unwrap();
    assert_eq!(v2, 2);
}

#[tokio::test]
async fn optimistic_concurrent_conflict_detected() {
    let controller = OptimisticController::new();

    // Both agents read version 0
    let token_a = controller.begin_optimistic("agent-a", "shared.rs").await;
    let token_b = controller.begin_optimistic("agent-b", "shared.rs").await;

    // Agent A commits first
    controller
        .commit_optimistic(token_a, "hash-a")
        .await
        .unwrap();

    // Agent B's commit should conflict
    let conflict = controller
        .commit_optimistic(token_b, "hash-b")
        .await
        .unwrap_err();

    assert_eq!(conflict.expected_version, 0);
    assert_eq!(conflict.actual_version, 1);
    assert_eq!(conflict.holder_agent, "agent-a");
    assert_eq!(conflict.conflicting_agent, "agent-b");
}

#[tokio::test]
async fn optimistic_last_writer_wins_resolves_conflict() {
    let controller =
        OptimisticController::with_default_strategy(ResolutionStrategy::LastWriterWins);

    let token_a = controller.begin_optimistic("agent-a", "data.json").await;
    let token_b = controller.begin_optimistic("agent-b", "data.json").await;

    controller
        .commit_optimistic(token_a, "hash-a")
        .await
        .unwrap();

    // Agent B commits with auto-resolution
    let result = controller
        .commit_or_resolve(token_b, "hash-b", None)
        .await
        .unwrap();

    assert!(result.is_success());
    let version = controller.get_version("data.json").await.unwrap();
    assert_eq!(version.last_modifier, "agent-b"); // last writer won
}

#[tokio::test]
async fn optimistic_first_writer_wins_rejects_late_commit() {
    let controller =
        OptimisticController::with_default_strategy(ResolutionStrategy::FirstWriterWins);

    let token_a = controller.begin_optimistic("agent-a", "stable.rs").await;
    let token_b = controller.begin_optimistic("agent-b", "stable.rs").await;

    controller
        .commit_optimistic(token_a, "hash-a")
        .await
        .unwrap();

    let result = controller
        .commit_or_resolve(token_b, "hash-b", None)
        .await
        .unwrap();

    match result {
        CommitResult::Rejected { reason } => {
            assert!(reason.contains("agent-a"));
        }
        _ => panic!("Expected Rejected, got {:?}", result),
    }
}

#[tokio::test]
async fn optimistic_per_resource_strategy_override() {
    let controller = OptimisticController::new(); // default = FirstWriterWins

    // Override for a specific resource
    controller
        .register_strategy("mutable.json", ResolutionStrategy::LastWriterWins)
        .await;

    let token_a = controller.begin_optimistic("agent-a", "mutable.json").await;
    let token_b = controller.begin_optimistic("agent-b", "mutable.json").await;

    controller.commit_optimistic(token_a, "ha").await.unwrap();

    // Should use LastWriterWins for this resource
    let result = controller
        .commit_or_resolve(token_b, "hb", None)
        .await
        .unwrap();
    assert!(result.is_success());
}

#[tokio::test]
async fn optimistic_conflict_history_tracked() {
    let controller = OptimisticController::new();

    // Create two conflicts
    for resource in ["file1.rs", "file2.rs"] {
        let t1 = controller.begin_optimistic("a1", resource).await;
        let t2 = controller.begin_optimistic("a2", resource).await;
        controller.commit_optimistic(t1, "h1").await.unwrap();
        let _ = controller.commit_or_resolve(t2, "h2", None).await;
    }

    let stats = controller.get_stats().await;
    assert_eq!(stats.total_conflicts, 2);
    assert_eq!(stats.total_resources, 2);

    let history = controller.get_conflict_history().await;
    assert_eq!(history.len(), 2);

    controller.clear_history().await;
    assert!(controller.get_conflict_history().await.is_empty());
}

// ===========================================================================
// Cross-pattern: Saga + Optimistic concurrency
// ===========================================================================

#[tokio::test]
async fn saga_with_optimistic_version_check() {
    // Scenario: A saga performs operations, each guarded by optimistic versioning.
    // If a version conflict occurs mid-saga, the saga compensates.

    let controller = OptimisticController::new();
    let saga = SagaExecutor::new("agent-1", "versioned deploy");

    // Step 1: Read current version and write
    let token = controller.begin_optimistic("agent-1", "app.rs").await;
    let commit_result = controller.commit_optimistic(token, "v1-hash").await;
    assert!(commit_result.is_ok());

    saga.execute_step(Arc::new(NoOpCompensation {
        description: "Write app.rs v1".into(),
        op_type: SagaOperationType::FileWrite,
    }))
    .await
    .unwrap();

    // Step 2: Another agent modifies the same resource concurrently
    let other_token = controller.begin_optimistic("agent-2", "app.rs").await;
    assert_eq!(other_token.base_version, 1);
    controller
        .commit_optimistic(other_token, "v2-hash-by-agent2")
        .await
        .unwrap();

    // Step 3: Our saga tries to commit again -- conflict!
    let stale_token = controller.begin_optimistic("agent-1", "app.rs").await;
    // Simulate our agent having an old token (version 1, but current is 2)
    let stale = brainwires_agents::optimistic::OptimisticToken {
        resource_id: "app.rs".into(),
        base_version: 1,
        base_hash: "v1-hash".into(),
        agent_id: "agent-1".into(),
        created_at: stale_token.created_at,
    };
    let conflict = controller.commit_optimistic(stale, "v3-hash").await;
    assert!(conflict.is_err());

    // Conflict detected -- compensate the saga
    saga.fail().await;
    let report = saga.compensate_all().await.unwrap();
    assert!(report.all_successful());
    assert_eq!(saga.status().await, SagaStatus::Compensated);
}