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
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