# Audit Logging Implementation Plan

> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.

**Goal:** Add structured logs under `logs/` for key API calls, LLM calls, strategy formulas, risk decisions, and execution results.

**Architecture:** Add a small JSONL audit logger in `src/binance_quant/audit_log.py`, then wire it into Binance REST calls, Codex prediction calls, AI trading orchestration, automatic buy/sell scanners, risk checks, and execution engines. Existing `ai_trader_llm.log` remains as the verbose prompt/stdout log.

**Tech Stack:** Python 3.11, stdlib JSON/file APIs, pytest.

---

### Task 1: Audit Logger Core

**Files:**
- Create: `src/binance_quant/audit_log.py`
- Test: `tests/test_audit_log.py`

- [x] Write failing tests for JSONL writing, secret redaction, Decimal serialization, and file rotation.
- [x] Implement `AuditLogger` and `audit_event`.
- [x] Run `pytest tests/test_audit_log.py -q`.

### Task 2: Binance REST API Audit

**Files:**
- Modify: `src/binance_quant/exchange.py`
- Test: `tests/test_exchange.py`

- [x] Write failing tests that public and signed API calls append sanitized request/result entries.
- [x] Add optional audit logger support to `BinanceSpotClient`.
- [x] Run `pytest tests/test_exchange.py -q`.

### Task 3: AI Trader Audit Nodes

**Files:**
- Modify: `src/binance_quant/ai_trader.py`
- Test: `tests/test_ai_trader.py`

- [x] Write failing tests for LLM call summary, model context, predictions, risk decisions, and execution results.
- [x] Wire audit events into predictor and AI trade orchestration.
- [x] Run `pytest tests/test_ai_trader.py -q`.

### Task 4: Auto Buy/Sell Logic Audit

**Files:**
- Modify: `src/binance_quant/auto_buy.py`
- Modify: `src/binance_quant/auto_sell.py`
- Test: `tests/test_auto_buy_strategy.py`
- Test: `tests/test_auto_sell_strategy.py`
- Test: `tests/test_auto_buy_runner.py`
- Test: `tests/test_auto_sell_runner.py`

- [x] Write failing tests for scanner formulas and risk/execution log nodes.
- [x] Add audit events without changing trading decisions.
- [x] Run targeted auto buy/sell tests.

### Task 5: Final Verification

- [x] Run the full test suite with `.venv\Scripts\python.exe -m pytest`.
- [x] Inspect `git diff` for unrelated changes and sensitive logging mistakes.
