Spaces:
Runtime error
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 exportsscanner_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 sessionaggregate_results()- Aggregate statistics across multiple sessions
2. Report Generation (multi_agent_demo/reports/)
NEW FILES:
__init__.py- Module exportsmarkdown_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 (
-ddirectory,-sscanners,-ooutput,--show-safe) - Progress bar with color-coded status
- Batch session processing with error handling
- Console and file output
- ANSI color support for terminal
Usage:
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
# In GitHub Actions
python -m multi_agent_demo.cli -d ./test_sessions -o scan_report.md
2. Regression Testing
# 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
# Process production logs
python -m multi_agent_demo.cli -d ./prod_sessions -o analysis.md
4. Compliance Reporting
# 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
# 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
# Run automated test
python test_batch_cli_automated.py
# Expected: β
ALL CHECKS PASSED
CI/CD Integration
Add to .github/workflows/test.yml:
- 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
# π‘οΈ 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
{
"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
Try the CLI:
python test_batch_cli.py # Create samples python -m multi_agent_demo.cli -d /tmp/cli_test_sessions_XXXXXRead the guide: See BATCH_CLI_GUIDE.md
Integrate with CI/CD: Add to your pipeline for automated scanning
For Developers
Test the implementation:
python test_batch_cli_automated.pyExtend functionality:
- Add new scanners β automatically available in CLI
- Modify report format β edit
markdown_generator.py - Add custom aggregations β edit
scanner_runner.py
Update CI/CD:
- Add
test_batch_cli_automated.pyto GitHub Actions
- Add
β¨ Key Features
- Shared Code: UI and CLI use same scanner logic
- Smart Filtering: Only shows sessions with issues
- Progress Display: Real-time colored progress bar
- Markdown Reports: Copyable format for documentation
- Flexible Scanner Selection: Choose which scanners to run
- Batch Processing: Process hundreds of sessions at once
- CI/CD Ready: Exit codes and file output for automation
- Detailed Statistics: Per-scanner and per-session breakdowns
π Documentation
- BATCH_CLI_GUIDE.md - Complete usage guide
- README.md - Main documentation
- 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