# Architecture memories (scope: project)
### [architecture] Module boundary rule (scope: project)
`src/core/` is UI- and transport-free: it never imports `clap`, `ratatui`, `crossterm`, or `rmcp`. `lib.rs` re-exports `core` for integration tests. All CLI/TUI/MCP concerns live in `src/cli_*.rs`, `src/tui.rs`, `src/mcp.rs`. This is invariant #3 in SPEC.md and is checked by feature-gated builds.
### [architecture] Proxy three-task data flow (scope: project)
`run_proxy` spawns the child with piped stdin/stdout/stderr and runs three `tokio::spawn` tasks joined together: (1) host stdin → child stdin, parsing and inserting a `PendingCall` into `Arc<tokio::sync::Mutex<HashMap<id, PendingCall>>>` **before** forwarding, then one `write_all`+`flush`; (2) child stdout → host stdout byte-for-byte, matching `id`, recording `RpcRecord`; (3) child stderr → host stderr. Both directions also run `jsonrpc::validate_frame` and write `schema_violations`. Insert-before-forward avoids races with fast responses; the stderr task prevents pipe-buffer deadlock with chatty children.
### [architecture] SQLite schema and write model (scope: project)
`rpc_calls` holds calls; `schema_violations` holds violations with direction/code/detail/redacted preview. WAL + `synchronous=NORMAL` + `busy_timeout=5000`. **Writes are synchronous** (`TelemetryDb::record` / `record_violation` open a connection per call), directly from the proxy task; WAL plus the busy timeout allow concurrent wrapped servers. Async methods only cover queries (`spawn_blocking`). SPEC.md §5 now describes this write model. `server_summary`/`tool_summary` compute avg, p50, p95 and error counts; `prune` deletes from both tables then VACUUMs.
### [learned-pattern] Compare command stays binary-only (scope: project)
`src/cli_compare.rs` holds pure `token_saving_pct` / `compare_servers` / renderers plus a thin `run`; it joins two `TelemetryDb::server_summary` results by server name, bases savings on total (req + resp) tokens, reports `n/a` for zero baselines, and keeps servers present in only one database. `tests/compare_tests.rs` seeds two temp DBs and spawns the binary with `--json`. No `src/core/` changes were needed.
### [learned-pattern] Pure formatter pattern for CLI modules (scope: project)
Each `cli_*.rs` exposes pure functions (`render_table`, `render_json`, `render_violations`, `cutoff_ms`, `wrap_servers`) with inline `#[cfg(test)]` tests, plus a thin synchronous `run` that opens the DB/Tokio runtime. Test the formatter, not I/O. This keeps new CLI features testable without spawning a binary.
### [architecture] MCP and TUI interfaces (scope: project)
`src/mcp.rs` uses rmcp `#[tool_router]` with `get_mcp_stats` and `get_slow_calls`; `get_info` is manually overridden to advertise name `mcpspy` and crate version because the macro's default reads the wrong crate. Payload builders (`stats_payload`, `slow_calls_payload`) are pure and unit-tested behind `#[cfg(any(feature = "mcp", test))]`. `src/tui.rs` mirrors this with `server_lines`/`tool_lines`/`recent_lines` plus `run_tui` (`q` quit, `c` clear), gated by `#[cfg(any(feature = "tui", test))]`.
### [architecture] Redaction and token estimation (scope: project)
`Redactor::new()` holds regexes for Bearer tokens, `sk-`, `ghp_`, `AKIA`, generic key/password/token values, and DB URLs; shared global instance via `OnceLock`. Used for previews **and** JSON-RPC error messages before storage; `--no-payloads` suppresses previews entirely. `TokenEstimator` uses ~4 chars/token plus 0.25 per JSON structural char — comparative auditing only, not a real tokenizer.