Spaces:
Runtime error
Runtime error
File size: 11,349 Bytes
dfa5489 | 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 | # CLI Batch Processing Implementation Summary
## Overview
Implemented a complete CLI batch processing mode for AI Agent Guards that shares code with the UI. This allows scanning multiple session files in batch, generating markdown reports, and integrating with CI/CD pipelines.
**Date:** 2026-01-29
---
## β
What Was Implemented
### 1. Core Shared Logic (`multi_agent_demo/core/`)
**NEW FILES:**
- `__init__.py` - Module exports
- `scanner_runner.py` - Shared scanner execution logic
**Purpose:** Extract scanner execution into reusable functions that both UI and CLI can use.
**Key Functions:**
- `run_scanners_on_session()` - Run scanners on single session
- `aggregate_results()` - Aggregate statistics across multiple sessions
### 2. Report Generation (`multi_agent_demo/reports/`)
**NEW FILES:**
- `__init__.py` - Module exports
- `markdown_generator.py` - Markdown report formatting
**Purpose:** Generate copyable markdown reports for CLI output.
**Features:**
- Overall statistics (total, safe, unsafe)
- Per-scanner breakdown (blocks, warnings, safe)
- Detailed results for sessions with issues
- Smart filtering (only shows problematic sessions)
### 3. CLI Entry Point (`multi_agent_demo/cli.py`)
**NEW FILE:** `cli.py`
**Purpose:** Command-line interface for batch processing.
**Features:**
- Argument parsing (`-d` directory, `-s` scanners, `-o` output, `--show-safe`)
- Progress bar with color-coded status
- Batch session processing with error handling
- Console and file output
- ANSI color support for terminal
**Usage:**
```bash
python -m multi_agent_demo.cli -d ./sessions
python -m multi_agent_demo.cli -d ./sessions -s AlignmentCheck FactsChecker
python -m multi_agent_demo.cli -d ./sessions -o report.md
```
### 4. Testing Scripts
**NEW FILES:**
- `test_batch_cli.py` - Manual test (creates sample sessions)
- `test_batch_cli_automated.py` - Automated CI/CD test
**Purpose:** Verify CLI works correctly.
### 5. Documentation
**NEW FILES:**
- `BATCH_CLI_GUIDE.md` - Comprehensive usage guide
**UPDATED FILES:**
- `README.md` - Added CLI sections
- Updated Overview (3 modes: UI, CLI, Deviations)
- Added "Batch Processing CLI" feature section
- Added CLI usage examples
- Added core modules documentation
- `CLAUDE.md` - Added CLI commands
- Updated running commands
- Updated module structure
---
## π Architecture
### Code Sharing Between UI and CLI
```
βββββββββββββββββββββββββββββββββββββββββββββββ
β User Interfaces β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββ€
β app.py (UI) β cli.py (CLI) β
ββββββββββ¬ββββββββββ΄βββββββββββ¬ββββββββββββββββ
β β
β βββββββββββββββββββ΄ββββββββββββ
β β core/scanner_runner.py β
β β - run_scanners_on_session()β
ββββ€ - aggregate_results() β
βββββββββββββββ¬ββββββββββββββββ
β
βββββββββββββββ΄ββββββββββββββββ
β firewall.py β
β - run_scanner_tests() β
βββββββββββββββ¬ββββββββββββββββ
β
βββββββββββββββ΄ββββββββββββββββ
β scanners/ β
β - PromptGuard β
β - AlignmentCheck β
β - FactsChecker β
β - DataDisclosureGuard β
βββββββββββββββββββββββββββββββ
```
**Benefits:**
- Single source of truth for scanner logic
- Changes automatically apply to both UI and CLI
- Consistent results across modes
- Easier maintenance
---
## π― Use Cases
### 1. CI/CD Integration
```bash
# In GitHub Actions
python -m multi_agent_demo.cli -d ./test_sessions -o scan_report.md
```
### 2. Regression Testing
```bash
# Before deployment
python -m multi_agent_demo.cli -d ./baseline_sessions -o before.md
# After changes
python -m multi_agent_demo.cli -d ./baseline_sessions -o after.md
diff before.md after.md
```
### 3. Large-Scale Analysis
```bash
# Process production logs
python -m multi_agent_demo.cli -d ./prod_sessions -o analysis.md
```
### 4. Compliance Reporting
```bash
# Generate monthly report
python -m multi_agent_demo.cli -d ./sessions/2026-01 -o report.md --show-safe
```
---
## π File Changes Summary
### Created Files (9 new files)
```
multi_agent_demo/
βββ core/
β βββ __init__.py # NEW
β βββ scanner_runner.py # NEW
βββ reports/
β βββ __init__.py # NEW
β βββ markdown_generator.py # NEW
βββ cli.py # NEW
# Root directory
βββ test_batch_cli.py # NEW
βββ test_batch_cli_automated.py # NEW
βββ BATCH_CLI_GUIDE.md # NEW
βββ BATCH_CLI_IMPLEMENTATION.md # NEW (this file)
```
### Updated Files (2 files)
```
βββ README.md # UPDATED
β - Added CLI overview
β - Added batch processing features
β - Added CLI usage examples
β - Added core modules documentation
β
βββ CLAUDE.md # UPDATED
- Added CLI commands
- Updated module structure
```
---
## π§ͺ Testing
### Manual Test
```bash
# Create test sessions
python test_batch_cli.py
# Run CLI on test sessions
python -m multi_agent_demo.cli -d /tmp/cli_test_sessions_XXXXX
```
### Automated Test
```bash
# Run automated test
python test_batch_cli_automated.py
# Expected: β
ALL CHECKS PASSED
```
### CI/CD Integration
Add to `.github/workflows/test.yml`:
```yaml
- name: Test CLI batch processing
run: python test_batch_cli_automated.py
```
---
## π Example Output
### Console
```
================================================================================
π‘οΈ AI AGENT GUARDS - BATCH SCANNER
================================================================================
π Scanning directory: ./sessions
β
Found 51 session file(s)
π Enabled scanners: PromptGuard, AlignmentCheck, FactsChecker
βοΈ Processing sessions...
[ββββββββββββββββββββββββββββββββββββββββ] 100% | 51/51 | π’ session_051.json
β
Processing complete!
================================================================================
π SUMMARY
================================================================================
Total Sessions: 51
Safe Sessions: 35 β
Sessions with Issues: 16 β οΈ
Total Blocks: 6 π«
Total Warnings: 43 β οΈ
Total Safe: 198 β
```
### Markdown Report
````markdown
# π‘οΈ AI Agent Guards - Batch Scan Report
## π Overall Statistics
- **Total Sessions Scanned:** 51
- **Safe Sessions:** 35 β
- **Sessions with Issues:** 16 β οΈ
**Accumulated Counts:**
- π« **Blocks:** 6
- β οΈ **Warnings:** 43
- β
**Safe:** 198
## π Results by Scanner
### AlignmentCheck
| Metric | Count |
|--------|-------|
| π« Blocks | 3 |
| β οΈ Warnings | 0 |
| β
Safe | 48 |
## π Detailed Results per Session
_Only showing sessions with issues._
### Session 5: `session_005.json`
**Overall Decision:** π΄ BLOCK
**Scanner Results:**
- **AlignmentCheck:** π΄ BLOCK
- Total: 4 | Safe: 2 | Warnings: 0 | Blocks: 2
````
---
## π§ Configuration
### Session JSON Format
```json
{
"session_id": "session_001",
"purpose": "Banking assistant",
"messages": [
{"type": "user", "content": "What's my balance?"},
{"type": "assistant", "content": "Your balance is $1,250."}
]
}
```
### CLI Arguments
| Argument | Required | Description | Example |
|----------|----------|-------------|---------|
| `-d, --directory` | Yes | Directory with JSON files | `-d ./sessions` |
| `-s, --scanners` | No | Scanners to run (default: all) | `-s AlignmentCheck FactsChecker` |
| `-o, --output` | No | Output file (default: console) | `-o report.md` |
| `--show-safe` | No | Show safe session details | `--show-safe` |
### Available Scanners
- `PromptGuard` - Malicious prompts/injections (BLOCK/WARNING/SAFE)
- `AlignmentCheck` - Goal hijacking/drift (BLOCK/SAFE)
- `FactsChecker` - Contradictions/ungrounded (BLOCK/WARNING/SAFE)
- `DataDisclosureGuard` - PII disclosure (BLOCK/WARNING/SAFE)
---
## π Next Steps
### For Users
1. **Try the CLI:**
```bash
python test_batch_cli.py # Create samples
python -m multi_agent_demo.cli -d /tmp/cli_test_sessions_XXXXX
```
2. **Read the guide:**
See [BATCH_CLI_GUIDE.md](./BATCH_CLI_GUIDE.md)
3. **Integrate with CI/CD:**
Add to your pipeline for automated scanning
### For Developers
1. **Test the implementation:**
```bash
python test_batch_cli_automated.py
```
2. **Extend functionality:**
- Add new scanners β automatically available in CLI
- Modify report format β edit `markdown_generator.py`
- Add custom aggregations β edit `scanner_runner.py`
3. **Update CI/CD:**
- Add `test_batch_cli_automated.py` to GitHub Actions
---
## β¨ Key Features
1. **Shared Code:** UI and CLI use same scanner logic
2. **Smart Filtering:** Only shows sessions with issues
3. **Progress Display:** Real-time colored progress bar
4. **Markdown Reports:** Copyable format for documentation
5. **Flexible Scanner Selection:** Choose which scanners to run
6. **Batch Processing:** Process hundreds of sessions at once
7. **CI/CD Ready:** Exit codes and file output for automation
8. **Detailed Statistics:** Per-scanner and per-session breakdowns
---
## π Documentation
- **[BATCH_CLI_GUIDE.md](./BATCH_CLI_GUIDE.md)** - Complete usage guide
- **[README.md](./README.md)** - Main documentation
- **[CLAUDE.md](./CLAUDE.md)** - Development guide
---
## π Summary
**What was requested:**
- CLI batch processing for multiple session files
- Shared code between UI and CLI
- Progress display
- Markdown reports with statistics
- CI/CD testing
- README updates
**What was delivered:**
- β
Complete CLI implementation with all requested features
- β
Shared core logic (`scanner_runner.py`)
- β
Markdown report generator
- β
Progress bar with color-coded status
- β
Manual and automated tests
- β
Comprehensive documentation (README, CLAUDE.md, BATCH_CLI_GUIDE.md)
- β
Smart filtering (only shows sessions with issues)
- β
Flexible scanner selection
- β
File and console output
**Bonus features:**
- ANSI color support for better terminal UX
- Recursive directory scanning
- Error handling and graceful failures
- Example use cases and integration patterns
- Programmatic usage examples
|