aitruism-newUI / BATCH_CLI_IMPLEMENTATION.md
philjosephcohen's picture
CLI mode
dfa5489
|
Raw
History Blame Contribute Delete
11.3 kB

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:

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

  1. Try the CLI:

    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

  3. Integrate with CI/CD: Add to your pipeline for automated scanning

For Developers

  1. Test the implementation:

    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


πŸŽ‰ 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