caxton 0.1.4

A secure WebAssembly runtime for multi-agent systems
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
# Caxton Testing Strategy

## Overview

This document defines the comprehensive testing strategy for Caxton, covering unit tests, integration tests, system tests, and chaos engineering. The strategy emphasizes testing distributed systems behaviors, agent interactions, and fault tolerance.

## Testing Principles

1. **Test at Multiple Levels**: Unit, integration, system, and chaos tests
2. **Focus on Behavior**: Test observable behavior, not implementation details
3. **Deterministic Tests**: Avoid flaky tests through proper synchronization
4. **Fast Feedback**: Quick unit tests, comprehensive integration tests
5. **Production-Like**: Test environments should mirror production

## Testing Levels

### 1. Unit Tests

#### Scope
Individual components and functions in isolation.

#### Framework
```toml
[dev-dependencies]
# Testing framework
tokio-test = "0.4"
mockall = "0.12"  # Mocking framework
proptest = "1.0"  # Property-based testing
criterion = "0.5" # Benchmarking
```

#### Example Unit Test
```rust
#[cfg(test)]
mod tests {
    use super::*;
    use mockall::predicate::*;

    #[tokio::test]
    async fn test_message_routing() {
        // Arrange
        let mut mock_agent = MockAgent::new();
        mock_agent
            .expect_receive()
            .with(eq(test_message()))
            .times(1)
            .returning(|_| Ok(()));

        let router = MessageRouter::new();
        router.register_agent("test-agent", mock_agent);

        // Act
        let result = router.route(test_message()).await;

        // Assert
        assert!(result.is_ok());
        assert_eq!(result.unwrap().status, DeliveryStatus::Delivered);
    }

    #[test]
    fn test_agent_registry_concurrent_access() {
        use proptest::prelude::*;

        proptest!(|(
            agents in prop::collection::vec(any::<AgentId>(), 1..100),
            operations in prop::collection::vec(any::<Operation>(), 1..1000)
        )| {
            let registry = Arc::new(RwLock::new(AgentRegistry::new()));

            // Spawn concurrent operations
            let handles: Vec<_> = operations
                .into_iter()
                .map(|op| {
                    let reg = registry.clone();
                    tokio::spawn(async move {
                        match op {
                            Operation::Insert(agent) => reg.write().await.insert(agent),
                            Operation::Remove(agent) => reg.write().await.remove(agent),
                            Operation::Lookup(agent) => reg.read().await.get(agent),
                        }
                    })
                })
                .collect();

            // All operations should complete without panic
            for handle in handles {
                assert!(handle.await.is_ok());
            }
        });
    }
}
```

### 2. Integration Tests

#### Scope
Multiple components working together, including external dependencies.

#### Test Categories

##### Agent Integration Tests
```rust
#[tokio::test]
async fn test_agent_deployment_lifecycle() {
    // Start test Caxton instance
    let caxton = TestCaxton::start().await;

    // Deploy agent
    let agent_path = test_fixtures::simple_agent_wasm();
    let deployment = caxton
        .deploy_agent(agent_path, DeploymentConfig::default())
        .await
        .expect("deployment failed");

    // Verify agent is running
    assert!(caxton.is_agent_running(&deployment.agent_id).await);

    // Send message to agent
    let response = caxton
        .send_message(FipaMessage::request(
            "system",
            &deployment.agent_id,
            "ping",
        ))
        .await
        .expect("message send failed");

    assert_eq!(response.body, "pong");

    // Remove agent
    caxton.remove_agent(&deployment.agent_id).await.expect("removal failed");

    // Verify agent is gone
    assert!(!caxton.is_agent_running(&deployment.agent_id).await);
}
```

##### Cluster Integration Tests
```rust
#[tokio::test]
async fn test_cluster_formation() {
    // Start seed node
    let node1 = TestNode::start_seed().await;

    // Start additional nodes
    let node2 = TestNode::join(&node1).await;
    let node3 = TestNode::join(&node1).await;

    // Wait for convergence
    tokio::time::sleep(Duration::from_secs(2)).await;

    // Verify all nodes see each other
    assert_eq!(node1.cluster_size().await, 3);
    assert_eq!(node2.cluster_size().await, 3);
    assert_eq!(node3.cluster_size().await, 3);

    // Verify agent registry is synchronized
    node1.deploy_agent("agent-1").await;
    tokio::time::sleep(Duration::from_millis(500)).await;

    assert!(node2.has_agent("agent-1").await);
    assert!(node3.has_agent("agent-1").await);
}
```

### 3. System Tests

#### End-to-End Scenarios
```rust
#[tokio::test]
async fn test_multi_agent_conversation() {
    let cluster = TestCluster::new(3).await;

    // Deploy buyer agent on node 1
    let buyer = cluster.nodes[0]
        .deploy_agent(test_fixtures::buyer_agent())
        .await;

    // Deploy seller agent on node 2
    let seller = cluster.nodes[1]
        .deploy_agent(test_fixtures::seller_agent())
        .await;

    // Deploy mediator agent on node 3
    let mediator = cluster.nodes[2]
        .deploy_agent(test_fixtures::mediator_agent())
        .await;

    // Initiate negotiation
    let negotiation = cluster.nodes[0]
        .send_message(FipaMessage::cfp(
            &buyer.agent_id,
            &seller.agent_id,
            "product-123",
        ))
        .await;

    // Wait for conversation to complete
    let result = cluster
        .wait_for_conversation_completion(&negotiation.conversation_id)
        .await;

    // Verify outcome
    assert!(result.is_agreement_reached());
    assert!(result.price > 0.0);
}
```

### 4. Performance Tests

#### Benchmark Suite
```rust
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn routing_benchmark(c: &mut Criterion) {
    let runtime = tokio::runtime::Runtime::new().unwrap();
    let router = runtime.block_on(setup_router());

    c.bench_function("local routing", |b| {
        b.to_async(&runtime).iter(|| async {
            router.route_local(black_box(test_message())).await
        });
    });

    c.bench_function("remote routing", |b| {
        b.to_async(&runtime).iter(|| async {
            router.route_remote(black_box(test_message())).await
        });
    });
}

fn agent_startup_benchmark(c: &mut Criterion) {
    let runtime = tokio::runtime::Runtime::new().unwrap();
    let wasm_module = load_test_agent();

    c.bench_function("agent startup", |b| {
        b.to_async(&runtime).iter(|| async {
            let agent = Agent::new(black_box(wasm_module.clone())).await;
            agent.start().await
        });
    });
}

criterion_group!(benches, routing_benchmark, agent_startup_benchmark);
criterion_main!(benches);
```

### 5. Chaos Testing

#### Failure Injection
```rust
pub struct ChaosTest {
    cluster: TestCluster,
    chaos: ChaosMonkey,
}

impl ChaosTest {
    pub async fn test_partition_tolerance() {
        // Deploy agents across cluster
        self.deploy_test_agents().await;

        // Start continuous workload
        let workload = self.start_workload().await;

        // Inject network partition
        self.chaos.partition_network(
            &self.cluster.nodes[0..2],
            &self.cluster.nodes[2..],
        ).await;

        // Verify system continues operating
        assert!(workload.is_still_running().await);

        // Heal partition
        self.chaos.heal_partition().await;

        // Verify recovery
        tokio::time::sleep(Duration::from_secs(5)).await;
        assert!(self.cluster.is_consistent().await);

        // Check for data loss
        assert_eq!(workload.messages_lost(), 0);
    }

    pub async fn test_cascading_failures() {
        // Setup dependency chain
        let agents = self.deploy_dependent_agents().await;

        // Kill critical agent
        self.chaos.kill_agent(&agents[0]).await;

        // Verify circuit breakers activate
        assert!(self.cluster.circuit_breakers_active().await);

        // Verify system doesn't cascade fail
        assert!(self.cluster.healthy_agents() > agents.len() / 2);

        // Restart failed agent
        self.cluster.restart_agent(&agents[0]).await;

        // Verify recovery
        assert!(self.cluster.all_agents_healthy().await);
    }
}
```

## Test Infrastructure

### Test Fixtures
```rust
pub mod test_fixtures {
    pub fn simple_agent_wasm() -> Vec<u8> {
        include_bytes!("../../fixtures/simple_agent.wasm").to_vec()
    }

    pub fn complex_agent_wasm() -> Vec<u8> {
        include_bytes!("../../fixtures/complex_agent.wasm").to_vec()
    }

    pub fn malicious_agent_wasm() -> Vec<u8> {
        // Agent that tries to exceed resource limits
        include_bytes!("../../fixtures/malicious_agent.wasm").to_vec()
    }
}
```

### Test Harness
```rust
pub struct TestHarness {
    docker: Docker,
    network: Network,
    nodes: Vec<Container>,
}

impl TestHarness {
    pub async fn setup() -> Self {
        let docker = Docker::connect().await;
        let network = docker.create_network("caxton-test").await;

        Self {
            docker,
            network,
            nodes: Vec::new(),
        }
    }

    pub async fn start_node(&mut self, config: NodeConfig) -> NodeHandle {
        let container = self.docker
            .run_container("caxton:test", config)
            .with_network(&self.network)
            .await;

        self.nodes.push(container.clone());
        NodeHandle::new(container)
    }

    pub async fn cleanup(self) {
        for node in self.nodes {
            node.stop().await;
            node.remove().await;
        }
        self.network.remove().await;
    }
}
```

## Test Data Management

### Test Data Generation
```rust
use proptest::prelude::*;

prop_compose! {
    fn arb_agent_id()(
        name in "[a-z]{5,10}",
        uuid in any::<u128>()
    ) -> AgentId {
        AgentId::new(format!("{}-{:x}", name, uuid))
    }
}

prop_compose! {
    fn arb_fipa_message()(
        performative in prop_oneof![
            Just(Performative::Request),
            Just(Performative::Inform),
            Just(Performative::Query),
        ],
        sender in arb_agent_id(),
        receiver in arb_agent_id(),
        content in any::<String>(),
    ) -> FipaMessage {
        FipaMessage {
            performative,
            sender,
            receiver,
            content,
            ..Default::default()
        }
    }
}
```

## Continuous Integration

### CI Pipeline
```yaml
name: Test Suite

on: [push, pull_request]

jobs:
  unit-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rs/toolchain@v1
      - name: Run unit tests
        run: cargo test --lib

  integration-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Start test infrastructure
        run: docker-compose -f test/docker-compose.yml up -d
      - name: Run integration tests
        run: cargo test --test integration

  system-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Build test image
        run: docker build -t caxton:test .
      - name: Run system tests
        run: cargo test --test system

  performance-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Run benchmarks
        run: cargo bench
      - name: Upload results
        uses: actions/upload-artifact@v3
        with:
          name: benchmarks
          path: target/criterion
```

## Test Coverage

### Coverage Requirements
- Unit tests: 80% line coverage
- Integration tests: 70% branch coverage
- Critical paths: 100% coverage

### Coverage Measurement
```bash
# Install coverage tools
cargo install cargo-tarpaulin

# Run with coverage
cargo tarpaulin --out Html --output-dir coverage

# Check coverage thresholds
cargo tarpaulin --fail-under 80
```

## Debugging Failed Tests

### Test Debugging Tools
```rust
#[tokio::test]
async fn test_with_debugging() {
    // Enable detailed logging for debugging
    let _ = env_logger::builder()
        .filter_level(log::LevelFilter::Trace)
        .try_init();

    // Capture test events
    let (tx, rx) = mpsc::channel();
    let recorder = TestRecorder::new(tx);

    // Run test with recording
    let result = run_test_with_recorder(recorder).await;

    // On failure, dump events
    if result.is_err() {
        let events: Vec<_> = rx.try_iter().collect();
        eprintln!("Test failed. Events:");
        for event in events {
            eprintln!("  {:?}", event);
        }
    }

    result.unwrap();
}
```

## Test Maintenance

### Test Organization
```
tests/
├── unit/
│   ├── agents/
│   ├── routing/
│   └── cluster/
├── integration/
│   ├── agent_lifecycle.rs
│   ├── cluster_formation.rs
│   └── message_routing.rs
├── system/
│   ├── end_to_end.rs
│   └── scenarios/
├── performance/
│   └── benchmarks.rs
└── chaos/
    ├── partition.rs
    └── failures.rs
```

### Test Documentation
Each test should include:
- Purpose description
- Setup requirements
- Expected behavior
- Cleanup needs

## Security Testing

### Security Test Suite
```rust
#[tokio::test]
async fn test_resource_limits_enforced() {
    let caxton = TestCaxton::start().await;

    // Deploy malicious agent that tries to exceed limits
    let agent = caxton
        .deploy_agent(test_fixtures::malicious_agent_wasm())
        .await;

    // Agent should be terminated for exceeding limits
    tokio::time::sleep(Duration::from_secs(2)).await;
    assert!(!caxton.is_agent_running(&agent.agent_id).await);

    // Verify termination reason
    let status = caxton.get_agent_status(&agent.agent_id).await;
    assert_eq!(status.termination_reason, Some("MemoryLimitExceeded"));
}

#[tokio::test]
async fn test_message_authentication() {
    let cluster = TestCluster::new(2).await;

    // Try to send unauthenticated message
    let result = cluster.nodes[0]
        .send_unauthenticated_message(test_message())
        .await;

    assert!(result.is_err());
    assert_eq!(result.unwrap_err().kind(), ErrorKind::Unauthorized);
}
```

## Test Metrics

### Key Metrics to Track
- Test execution time
- Test flakiness rate
- Coverage trends
- Performance regression detection
- Failure patterns

### Test Dashboard
Monitor test health with metrics:
```rust
pub struct TestMetrics {
    test_duration: Histogram,
    test_failures: Counter,
    flaky_tests: Gauge,
    coverage_percentage: Gauge,
}
```

## Best Practices

1. **Isolation**: Tests should not depend on external state
2. **Determinism**: Use fixed seeds for random data
3. **Cleanup**: Always clean up test resources
4. **Naming**: Use descriptive test names
5. **Speed**: Keep unit tests under 1ms
6. **Documentation**: Document complex test scenarios
7. **Maintenance**: Regularly review and update tests
8. **Parallelization**: Design tests to run in parallel

## Future Enhancements

1. **Fuzzing**: Add fuzzing for protocol implementations
2. **Load Testing**: Comprehensive load testing framework
3. **Compliance Testing**: FIPA protocol compliance suite
4. **Visual Testing**: UI component testing (if applicable)
5. **Contract Testing**: Agent interaction contract tests