File size: 11,829 Bytes
dfa5489 bfd7a80 4c687cf bfd7a80 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 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 | # CLI Batch Processing Guide
## Overview
The AI Agent Guards platform now supports **batch processing mode** via a command-line interface (CLI). This allows you to scan multiple session files at once, get aggregated statistics, and generate markdown reports.
**Key Benefits:**
- Process hundreds of sessions in one command
- CI/CD integration for automated testing
- Markdown reports for documentation
- Shared code with UI ensures consistency
- Smart filtering: only shows sessions with issues
---
## Quick Start
### 1. Basic Usage
Scan all JSON files in a directory:
```bash
python -m multi_agent_demo.cli -d ./sessions
```
### 2. Select Specific Scanners
```bash
python -m multi_agent_demo.cli -d ./sessions -s AlignmentCheck FactsChecker
```
### 3. Save Report to File
```bash
python -m multi_agent_demo.cli -d ./sessions -o report.md
```
### 4. Include Safe Sessions in Report
By default, only sessions with issues are detailed. To show all:
```bash
python -m multi_agent_demo.cli -d ./sessions --show-safe
```
---
## Available Scanners
| Scanner | What It Detects | Decisions |
|---------|----------------|-----------|
| **PromptGuard** | Malicious prompts and injections | BLOCK, WARNING, SAFE |
| **AlignmentCheck** | Goal hijacking and behavioral drift | BLOCK, SAFE _(no warnings)_ |
| **FactsChecker** | Contradictions and ungrounded claims | BLOCK, WARNING, SAFE |
| **DataDisclosureGuard** | PII disclosure issues | BLOCK, WARNING, SAFE |
**Note:** If you see "Total Warnings: 0", it's normal when running only AlignmentCheck, which doesn't produce warnings (only BLOCK or SAFE).
---
## Session JSON Format
The CLI supports **two formats**:
### Format 1: Langfuse Export Format (OpenOps)
```json
{
"scenario_name": "environment_prod_0450c00c",
"agent_purpose": "You are the OpenOps Agent, an AI assistant...",
"messages": [
{"type": "user", "content": "create a workflow that monitors price..."},
{"type": "assistant", "content": "I'll help you create that workflow..."}
],
"exported_at": "2026-01-26T19:06:23.108931+00:00",
"format_version": "1.0"
}
```
**Required Fields:**
- `agent_purpose` - Agent's purpose/role description
- `messages` - Array of message objects with `type` and `content`
**Optional Fields:**
- `scenario_name` - Scenario/session identifier
- `exported_at` - Export timestamp
- `format_version` - Format version
### Format 2: Simple Format
```json
{
"session_id": "session_001",
"purpose": "Banking assistant that helps users check balances",
"messages": [
{"type": "user", "content": "What's my account balance?"},
{"type": "assistant", "content": "Your current balance is $1,250.00"}
]
}
```
**Required Fields:**
- `purpose` - Agent's purpose/role description
- `messages` - Array of message objects with `type` and `content`
**Optional Fields:**
- `session_id` - Session identifier
- `agent_name` - Agent name
- `agent_role` - Agent role
---
**Note:** The CLI automatically detects which format you're using. It checks for `agent_purpose` first (Langfuse format), then falls back to `purpose` (simple format).
---
## Output Example
### Console Output
```
================================================================================
π‘οΈ AI AGENT GUARDS - BATCH SCANNER
================================================================================
π Scanning directory: ./sessions
β
Found 51 session file(s)
π Enabled scanners: PromptGuard, AlignmentCheck, FactsChecker, DataDisclosureGuard
βοΈ Processing sessions...
[ββββββββββββββββββββββββββββββββββββββββ] 100% | 51/51 | π’ session_051.json
β
Processing complete!
π Aggregating results...
π Generating report...
================================================================================
π SUMMARY
================================================================================
Total Sessions: 51
Safe Sessions: 35 β
Sessions with Issues: 16 β οΈ
Total Blocks: 6 π«
Total Warnings: 43 β οΈ
Total Safe: 198 β
================================================================================
```
### Markdown Report Structure
```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 |
### FactsChecker
| Metric | Count |
|--------|-------|
| π« Blocks | 3 |
| β οΈ Warnings | 43 |
| β
Safe | 5 |
---
## π Detailed Results per Session
_Note: Only showing sessions with issues. Safe sessions are omitted for brevity._
### Session 5: `session_005_goal_hijacking.json`
**Overall Decision:** π΄ BLOCK
**Scanner Results:**
- **AlignmentCheck:** π΄ BLOCK
- Total: 4 | Safe: 2 | Warnings: 0 | Blocks: 2
- _Reason:_ Agent redirected conversation from stated purpose...
- **FactsChecker:** π‘ WARNING
- Total: 4 | Safe: 2 | Warnings: 2 | Blocks: 0
- _Reason:_ Detected ungrounded claims in messages 3 and 4...
---
```
**NEW: Google Sheets Integration** π
The report now includes **two formats** for easy data analysis:
1. **Sessions Summary Table** - Markdown table with all sessions and per-scanner results
- Format: `DECISION (total: safe/warning/block)`
- Example: `SAFE (3: 3/0/0)` = 3 messages, all safe
- Includes "Overall" column showing worst decision
2. **Copy-Paste Format (CSV)** - Comma-separated values for direct paste into Google Sheets
- Format: `DECISION (safe/warning/block)`
- Shorter format, perfect for spreadsheet analysis
- Just copy and paste - columns align automatically!
**For complete guide on using the reports in Google Sheets, see:**
π **[BATCH_CLI_REPORT_FORMAT.md](./BATCH_CLI_REPORT_FORMAT.md)**
**Quick example:**
```
Session AlignmentCheck PromptGuard FactsChecker Overall
session1.json SAFE (3/0/0) SAFE (2/0/0) WARNING (3/2/0) WARNING
session2.json BLOCK (1/0/2) SAFE (2/0/0) SAFE (5/0/0) BLOCK
```
Copy β Paste into Google Sheets β Done! β¨
---
## Use Cases
### 1. CI/CD Integration
Add to your GitHub Actions workflow:
```yaml
- name: Scan agent sessions
run: |
python -m multi_agent_demo.cli -d ./test_sessions -o scan_report.md
- name: Upload report
uses: actions/upload-artifact@v3
with:
name: security-scan-report
path: scan_report.md
```
### 2. Regression Testing
Before deploying changes:
```bash
# Scan baseline sessions
python -m multi_agent_demo.cli -d ./baseline_sessions -o baseline.md
# Make changes to agent
# Scan again and compare
python -m multi_agent_demo.cli -d ./baseline_sessions -o updated.md
diff baseline.md updated.md
```
### 3. Large-Scale Analysis
Process production logs:
```bash
# Export sessions from production to JSON
python export_sessions.py --output ./prod_sessions
# Scan all sessions
python -m multi_agent_demo.cli -d ./prod_sessions -o prod_analysis.md
# Review report for patterns
cat prod_analysis.md | grep "BLOCK"
```
### 4. Compliance Reporting
Generate reports for audits:
```bash
# Scan last month's sessions
python -m multi_agent_demo.cli \
-d ./sessions/2026-01 \
-o compliance_report_jan_2026.md \
--show-safe
# Include in compliance documentation
```
---
## Testing the CLI
### Manual Test
Create test sessions:
```bash
python test_batch_cli.py
```
This creates a temp directory with sample sessions and shows you the command to run.
### Automated Test
Run the automated test:
```bash
python test_batch_cli_automated.py
```
This creates sessions, runs the CLI, and validates the output automatically.
---
## Architecture
The CLI shares code with the UI to ensure consistency:
```
multi_agent_demo/
βββ cli.py # CLI entry point
βββ app.py # UI entry point
βββ core/ # Shared logic
β βββ scanner_runner.py # Scanner execution (used by both CLI and UI)
βββ reports/ # CLI-specific
β βββ markdown_generator.py # Markdown report generation
βββ firewall.py # Scanner orchestration (shared)
βββ scanners/ # Scanner implementations (shared)
```
**Benefits of shared code:**
- Changes to scanner logic automatically apply to both CLI and UI
- Same validation rules everywhere
- Consistent results across modes
- Single source of truth
---
## Advanced Options
### Custom Session Format
If your sessions have a different structure, create a wrapper:
```python
from multi_agent_demo.core import run_scanners_on_session
# Load your custom format
custom_session = load_my_session("session.json")
# Convert to expected format
session_data = {
"purpose": custom_session["agent_purpose"],
"messages": [
{"type": msg["role"], "content": msg["text"]}
for msg in custom_session["conversation"]
]
}
# Run scanners
result = run_scanners_on_session(
session_data=session_data,
enabled_scanners=["AlignmentCheck", "FactsChecker"]
)
```
### Programmatic Usage
Use the CLI logic in your own scripts:
```python
from multi_agent_demo.core import run_scanners_on_session, aggregate_results
from multi_agent_demo.reports import generate_markdown_report
# Load sessions
sessions = [load_json(f) for f in session_files]
# Run scanners
results = [
run_scanners_on_session(session, ["AlignmentCheck"])
for session in sessions
]
# Aggregate
stats = aggregate_results(results)
# Generate report
report = generate_markdown_report(results, session_files, stats)
print(report)
```
---
## Troubleshooting
### Error: "No JSON files found"
Check:
- Directory path is correct
- JSON files have `.json` extension
- Files are not in subdirectories (CLI scans recursively with `**/*.json`)
### Error: "Module not found"
Run from project root:
```bash
cd /path/to/mutli-agent-demo
python -m multi_agent_demo.cli -d ./sessions
```
### Error: "TOGETHER_API_KEY not found"
Set environment variable:
```bash
export TOGETHER_API_KEY=your_key_here
python -m multi_agent_demo.cli -d ./sessions
```
Or create `.env` file:
```
TOGETHER_API_KEY=your_key_here
OPENAI_API_KEY=your_key_here
```
### Slow Performance
Use fewer scanners:
```bash
# Fast: Only AlignmentCheck
python -m multi_agent_demo.cli -d ./sessions -s AlignmentCheck
# Slower: All scanners
python -m multi_agent_demo.cli -d ./sessions
```
---
## FAQ
**Q: Can I use the CLI without the UI?**
A: Yes! The CLI is standalone. Just ensure dependencies are installed.
**Q: Does the CLI support the same scanners as the UI?**
A: Yes, exactly the same scanners with the same logic.
**Q: Can I integrate with other CI/CD tools?**
A: Yes! The CLI is a standard Python script with exit codes:
- `0` = success
- `1` = error
**Q: How do I get only the statistics without the full report?**
A: Redirect stderr to see just the summary:
```bash
python -m multi_agent_demo.cli -d ./sessions 2>&1 | tail -20
```
**Q: Can I scan a single file?**
A: Yes, put it in a directory:
```bash
mkdir temp_scan
cp session.json temp_scan/
python -m multi_agent_demo.cli -d temp_scan
```
---
## Next Steps
1. **Try it**: Run `python test_batch_cli.py` to create sample sessions
2. **Integrate**: Add to your CI/CD pipeline
3. **Customize**: Adjust report format in `markdown_generator.py`
4. **Scale**: Process production logs for analysis
For more details, see:
- [README.md](./README.md) - Full documentation
- [CLAUDE.md](./CLAUDE.md) - Development guide
- [INSTALL.md](./INSTALL.md) - Installation guide
|