An open API service indexing awesome lists of open source software.

https://github.com/sauravbhattacharya001/ai

Contract-enforced sandbox for studying AI agent self-replication safety
https://github.com/sauravbhattacharya001/ai

agent-safety ai-agents ai-governance ai-safety anomaly-detection chaos-engineering containment docker forensics game-theory kill-switch prompt-injection python red-teaming replication-control research safety-research sandboxing self-replication threat-modeling

Last synced: 3 months ago
JSON representation

Contract-enforced sandbox for studying AI agent self-replication safety

Awesome Lists containing this project

README

          

# ๐Ÿค– AI Replication Sandbox

**A comprehensive, contract-enforced framework for studying AI agent self-replication safety**

[![CI](https://github.com/sauravbhattacharya001/ai/actions/workflows/ci.yml/badge.svg)](https://github.com/sauravbhattacharya001/ai/actions/workflows/ci.yml)
[![CodeQL](https://github.com/sauravbhattacharya001/ai/actions/workflows/codeql.yml/badge.svg)](https://github.com/sauravbhattacharya001/ai/actions/workflows/codeql.yml)
[![Docker](https://github.com/sauravbhattacharya001/ai/actions/workflows/docker.yml/badge.svg)](https://github.com/sauravbhattacharya001/ai/actions/workflows/docker.yml)
[![codecov](https://codecov.io/gh/sauravbhattacharya001/ai/graph/badge.svg)](https://codecov.io/gh/sauravbhattacharya001/ai)
[![PyPI](https://img.shields.io/pypi/v/ai-replication-sandbox?color=blue&logo=pypi&logoColor=white)](https://pypi.org/project/ai-replication-sandbox/)
[![Python 3.10+](https://img.shields.io/badge/Python-3.10%2B-3776AB?logo=python&logoColor=white)](https://python.org)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
[![Docs](https://img.shields.io/badge/docs-GitHub%20Pages-blue?logo=github)](https://sauravbhattacharya001.github.io/ai/)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/sauravbhattacharya001/ai/pulls)
[![Dependabot](https://img.shields.io/badge/dependabot-enabled-025E8C?logo=dependabot)](https://github.com/sauravbhattacharya001/ai/network/updates)
[![Security Policy](https://img.shields.io/badge/security-policy-green?logo=shield)](SECURITY.md)

*144 modules ยท 100+ CLI commands ยท Zero external dependencies*

---

## Why This Exists

As AI agents gain autonomy, controlling **how and when they spawn copies of themselves** becomes a critical safety challenge. This project provides a complete testbed for designing, validating, and stress-testing replication controls โ€” from contract enforcement and kill switches to game-theoretic analysis and adversarial red-teaming.

No Docker or Podman required. Everything runs as pure Python simulation while documenting the exact resource and network boundaries each worker would get in production.

## Architecture

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Controller โ”‚
โ”‚ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ โ”‚ Contract โ”‚ โ”‚ Registry โ”‚ โ”‚ Kill Switch โ”‚ โ”‚
โ”‚ โ”‚ Engine โ”‚ โ”‚ & Quotas โ”‚ โ”‚ & Audit Trail โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”‚ sign โ”‚ register โ”‚ kill โ”‚
โ”‚ โ–ผ โ–ผ โ–ผ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ โ”‚ HMAC-Signed Manifest + Policy Rules โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ–ผ โ–ผ โ–ผ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ โ”‚ Workers โ”‚ โ”‚ Sandbox โ”‚ โ”‚ Observability โ”‚ โ”‚
โ”‚ โ”‚ (Agents) โ”‚ โ”‚ Orchestr. โ”‚ โ”‚ & Structured Logs โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

## Features

### ๐Ÿ”’ Core Safety Controls
- **Contract Engine** โ€” HMAC-signed replication contracts with resource specs, network policies, and stop conditions
- **Kill Switch** โ€” Conservative, aggressive, and quarantine strategies with cooldown and fleet-wide coordination
- **Circuit Breaker** โ€” Autonomous trip-and-recover pattern for safety controls
- **Quarantine Manager** โ€” Isolate misbehaving agents with severity-based policies
- **Access Control** โ€” RBAC/ABAC simulation with privilege escalation detection
- **Safety Gate** โ€” Pre-deployment go/no-go readiness checks

### ๐Ÿ” Threat Detection & Analysis
- **Kill Chain Analyzer** โ€” MITRE ATT&CK-style multi-stage attack detection
- **Escalation Detector** โ€” Privilege escalation chain analysis with stealth scoring
- **Covert Channel Detector** โ€” Detect hidden agent-to-agent communication
- **Collusion Detector** โ€” Identify coordinated multi-agent safety bypasses
- **Self-Modification Detector** โ€” Track agents attempting to alter their own code or contracts
- **Deception Detector** โ€” Agent honesty and trust analysis
- **Evasion Simulator** โ€” Test how agents might evade safety controls
- **Lateral Movement Detector** โ€” Cross-sandbox movement pattern detection
- **Prompt Injection Tester** โ€” Adversarial prompt injection analysis
- **Steganography Detector** โ€” Hidden data in agent communications
- **Shadow AI Detector** โ€” Find unauthorized AI deployments bypassing controls

### ๐Ÿ“Š Risk & Compliance
- **Compliance Auditor** โ€” NIST AI RMF, EU AI Act, ISO 42001, OECD framework checks
- **Regulatory Mapper** โ€” Map findings to specific regulatory articles
- **Risk Register** โ€” Formal risk tracking with lifecycle management
- **Risk Profiler** โ€” Unified agent dossiers with fleet-wide risk scoring
- **Risk Heatmap** โ€” Interactive HTML heatmap visualization
- **Safety Scorecard** โ€” Multi-dimensional safety evaluation
- **Safety Diff** โ€” Compare safety posture snapshots over time
- **Nutrition Label** โ€” FDA-style safety labels for AI agents
- **Model Card** โ€” Standardized safety documentation generation

### ๐Ÿงช Testing & Simulation
- **Simulator** โ€” Full replication simulations with configurable strategies (greedy, cautious, cooperative, etc.)
- **Monte Carlo Analyzer** โ€” Statistical risk analysis across thousands of runs
- **Chaos Runner** โ€” Fault injection (kill workers, delay, corrupt, partition)
- **Boundary Tester** โ€” Probe agent capability limits with fault injection
- **Sensitivity Analyzer** โ€” Parameter sensitivity with tipping-point detection
- **Mutation Tester** โ€” Verify safety policies catch real violations
- **Safety Drill Runner** โ€” Automated readiness exercises
- **Safety Benchmark** โ€” Standardized control benchmarks with latency stats
- **What-If Analyzer** โ€” Explore configuration change impact
- **Fleet Simulator** โ€” Interactive animated HTML fleet visualization

### ๐ŸŽฎ Advanced Analysis
- **Game Theory** โ€” Prisoner's Dilemma, Stag Hunt, Chicken for agent interactions
- **Trust Propagation** โ€” Network trust with Sybil attack detection
- **Swarm Analyzer** โ€” Emergent behavior detection in agent populations
- **Consensus Protocol** โ€” Multi-agent voting with Byzantine fault tolerance
- **Threat Correlator** โ€” Cross-module signal correlation with compound threat detection
- **Attack Tree / Attack Graph** โ€” Threat modeling with choke-point analysis
- **Goal Inference** โ€” Infer agent intentions from observed behavior
- **Influence Propagation** โ€” Track how agents influence each other

### ๐Ÿšจ Incident Response
- **Incident Responder** โ€” Automated playbooks with severity-based routing
- **Incident Forecaster** โ€” Predict future incidents from historical patterns
- **Forensic Analyzer** โ€” Post-incident analysis with counterfactual reasoning
- **Root Cause Analysis** โ€” 5 Whys, Fishbone, Fault Tree with cut-set analysis
- **Postmortem Generator** โ€” Structured blameless postmortem documents
- **War Room** โ€” Interactive HTML incident command dashboard
- **Situational Awareness (SITREP)** โ€” DEFCON-style unified threat level reporting
- **Containment Planner** โ€” Breach response strategy ranking
- **Evidence Collector** โ€” Audit artifact collection for compliance

### ๐Ÿ“ˆ Monitoring & Operations
- **Drift Detector** โ€” Behavioral drift with directional analysis
- **Regression Detector** โ€” Safety metric regression between baselines
- **Trend Tracker** โ€” Scorecard trends over time with regression alerts
- **Adaptive Thresholds** โ€” Self-tuning safety thresholds with breach forecasting
- **Safety Autopilot** โ€” Autonomous monitoring loop with corrective actions
- **Alert Router** โ€” Rule-based routing with rate limiting and quiet hours
- **Fatigue Detector** โ€” Alert fatigue analysis
- **SLA Monitor** โ€” Check results against safety SLA targets
- **Metrics Aggregator** โ€” Consolidated safety dashboard
- **Fleet Overview** โ€” kubectl-style worker status

### ๐Ÿ“„ Governance & Documentation
- **Contract Templates** โ€” Pre-built contracts for common scenarios
- **Contract Wizard** โ€” Interactive HTML builder for safe configs
- **Contract Optimizer** โ€” Multi-objective parameter optimization
- **Policy Linter** โ€” Detect misconfigurations and coverage gaps
- **Safety Checklist** โ€” Customizable pre-deployment checklists
- **Audit Trail** โ€” Tamper-evident hash-chained event logs
- **Maturity Model** โ€” 8-dimension safety maturity assessment
- **Culture Survey** โ€” Organizational AI safety culture evaluation
- **Safety Quiz** โ€” Training quiz generation from knowledge base
- **Red Team / Blue Team** โ€” Adversarial debate with judge verdict
- **Tabletop Exercises** โ€” Structured scenario planning
- **ROI Calculator** โ€” Cost-benefit analysis for safety controls

## Installation

```bash
pip install ai-replication-sandbox
```

Or install from source:

```bash
git clone https://github.com/sauravbhattacharya001/ai.git
cd ai
pip install -e ".[dev]"
```

### Docker

```bash
docker pull ghcr.io/sauravbhattacharya001/ai:latest
docker run --rm ghcr.io/sauravbhattacharya001/ai simulate --strategy greedy
```

## Quick Start

### Run a Simulation

```python
from replication import Simulator, ScenarioConfig, Strategy

config = ScenarioConfig(
max_workers=10,
max_depth=3,
rounds=50,
strategy=Strategy.CAUTIOUS,
)
report = Simulator(config).run()
print(f"Peak workers: {report.peak_workers}")
print(f"Safety violations: {report.violations}")
```

### Safety Scorecard

```python
from replication import SafetyScorecard, ScorecardConfig

scorecard = SafetyScorecard(ScorecardConfig())
result = scorecard.evaluate(simulation_report)
print(f"Overall grade: {result.grade}")
for dim in result.dimensions:
print(f" {dim.name}: {dim.score}/100")
```

### Compliance Audit

```python
from replication import ComplianceAuditor, AuditConfig, Framework

auditor = ComplianceAuditor(AuditConfig(
frameworks=[Framework.NIST_AI_RMF, Framework.EU_AI_ACT]
))
result = auditor.audit(simulation_report)
for fw_result in result.framework_results:
print(f"{fw_result.framework}: {fw_result.verdict}")
```

### Kill Switch

```python
from replication import create_conservative_killswitch

ks = create_conservative_killswitch()
result = ks.evaluate(current_metrics)
if result.triggered:
print(f"Kill switch activated: {result.strategy.kind}")
```

## CLI Reference

The unified CLI provides 100+ subcommands:

```bash
# Simulation & Analysis
python -m replication simulate --strategy greedy
python -m replication montecarlo --runs 500
python -m replication chaos --faults kill_worker,delay
python -m replication sensitivity --param max_depth
python -m replication what-if --param max_children=5

# Threat Detection
python -m replication killchain
python -m replication escalation
python -m replication covert-channels
python -m replication selfmod
python -m replication collusion

# Compliance & Governance
python -m replication compliance --framework nist_ai_rmf
python -m replication regulatory-map
python -m replication lint
python -m replication gate
python -m replication checklist

# Monitoring
python -m replication drift --window 20
python -m replication scorecard
python -m replication quick-scan
python -m replication sitrep

# Incident Response
python -m replication forensics
python -m replication root-cause
python -m replication postmortem
python -m replication warroom

# Interactive HTML Tools
python -m replication playground -o playground.html
python -m replication threat-matrix -o matrix.html
python -m replication fleet-sim -o fleet.html
python -m replication risk-heatmap -o heatmap.html
python -m replication radar -o radar.html

# Full list
python -m replication --list
```

## Tech Stack

| Component | Technology |
|-----------|-----------|
| Language | Python 3.10+ |
| Build | Hatchling |
| Testing | pytest + coverage |
| Linting | flake8 + mypy (strict) |
| CI/CD | GitHub Actions |
| Security | CodeQL + Dependabot |
| Docs | MkDocs (GitHub Pages) |
| Container | Docker (multi-stage) |
| Package | PyPI |

## Project Structure

```
src/replication/
โ”œโ”€โ”€ contract.py # Core contract engine
โ”œโ”€โ”€ controller.py # Replication controller & registry
โ”œโ”€โ”€ worker.py # Agent worker implementation
โ”œโ”€โ”€ simulator.py # Simulation engine
โ”œโ”€โ”€ kill_switch.py # Kill switch strategies
โ”œโ”€โ”€ circuit_breaker.py # Safety circuit breaker
โ”œโ”€โ”€ killchain.py # Kill chain analysis
โ”œโ”€โ”€ escalation.py # Privilege escalation detection
โ”œโ”€โ”€ compliance.py # Multi-framework compliance
โ”œโ”€โ”€ forensics.py # Post-incident forensics
โ”œโ”€โ”€ game_theory.py # Game-theoretic analysis
โ”œโ”€โ”€ montecarlo.py # Monte Carlo risk analysis
โ”œโ”€โ”€ chaos.py # Chaos/fault injection
โ”œโ”€โ”€ ... (144 modules) # See full docs
tests/ # Comprehensive test suite
docs/ # MkDocs documentation
demos/ # Example scripts
```

## Documentation

Full documentation is available at **[sauravbhattacharya001.github.io/ai](https://sauravbhattacharya001.github.io/ai/)**.

## Contributing

Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.

1. Fork the repository
2. Create a feature branch
3. Add tests for new functionality
4. Ensure `pytest` and `flake8` pass
5. Submit a pull request

## Security

Found a vulnerability? Please see our [Security Policy](SECURITY.md) for responsible disclosure.

## License

This project is licensed under the MIT License โ€” see [LICENSE](LICENSE) for details.

---

**[Documentation](https://sauravbhattacharya001.github.io/ai/)** ยท **[PyPI](https://pypi.org/project/ai-replication-sandbox/)** ยท **[Issues](https://github.com/sauravbhattacharya001/ai/issues)** ยท **[Changelog](CHANGELOG.md)**