# perfxpert Architecture _Last refreshed: v0.2.0. Source of truth: local multi-agent-perfxpert design spec (kept in the contributor's working copy)._ ## High-level shape perfxpert is a **multi-agent system** for GPU performance analysis on AMD ROCm. The shipped entry surfaces are the batch CLI (`perfxpert analyze`), the interactive launcher (`perfxpert-code`), the stdio MCP server (`perfxpert-mcp`), and the in-process Python API (`perfxpert.api.*`). All four resolve into the same session/runtime layer in `perfxpert.agents.runtime`. ## Agents (spec ยง2) ```mermaid flowchart TD classDef brain fill:#fff8c7,stroke:#9a6b00,color:#3d2b00,stroke-width:2px classDef specialist fill:#eaf8ef,stroke:#227343,color:#143b26,stroke-width:1px subgraph brainSection["๐Ÿง  PerfXpert agent brain"] root["Root
intent router"] analysis["Analysis
classifies bottlenecks + gathers metrics"] recommendation["Recommendation
hands off to specialists"] correctness["Correctness
summarizes gate verdicts"] end compute["compute_specialist"] memory["memory_specialist"] latency["latency_specialist"] diff["diff_specialist"] root --> analysis root --> recommendation root --> correctness analysis --> compute analysis --> memory analysis --> latency analysis --> diff recommendation --> compute recommendation --> memory recommendation --> latency recommendation --> diff class root,analysis,recommendation,correctness brain class compute,memory,latency,diff specialist ``` 8 agents total. Each has โ‰ค 400 lines of fence + โ‰ค 5 tools + โ‰ค 10 input / โ‰ค 5 output fields. Narrow scope is CI-enforced. See [architecture/agent-hierarchy.md](architecture/agent-hierarchy.md) for the tier-by-tier map, fence-slice pattern, and source-tree locations, and [architecture/gate-cascade.md](architecture/gate-cascade.md) for the 5-gate correctness middleware that sits between the agents. The full architecture docs index lives at [architecture/README.md](architecture/README.md). ## Tools (spec ยง3 + Appendix A) perfxpert ships dozens of deterministic Python tools. The current MCP surface exposes **56 READ_ONLY tools**; side-effecting tools remain in-process only. The split is: - **READ_ONLY** โ€” safe for MCP (agent lookups, analysis, classification) - **EXECUTION** โ€” in-process only (profile.run, compile.build, patch.apply) Every tool is pure (modulo knowledge YAML loads and SQL reads); < 100 ms p99. ## Knowledge (spec ยง3 Appendix B) 22 YAML files under `perfxpert/knowledge/`, each paired with a JSON schema in `_schemas/`. CI validates every YAML against its schema on every PR. ## Providers (spec ยง3) 5 LLM provider adapters under `perfxpert/providers/`: - Anthropic (Claude Sonnet family) - OpenAI (GPT-4 family) - Ollama (local) - Private (any OpenAI-compatible endpoint) - Opencode (bundled subprocess wrapper) All five verified nightly via `.github/workflows/perfxpert-nightly.yml`. ## Runtime middleware (spec ยง5) `perfxpert/runtime/`: - `gate_cascade.py` โ€” cascaded 5-gate correctness pipeline - `intent_classifier.py` โ€” deterministic router (air-gap mode) - `recursion_guard.py` โ€” blocks opencode-in-opencode recursion ## Data flow ### Batch CLI ``` user โ†’ perfxpert analyze -i trace.db โ†’ perfxpert.agents.runtime.build_session(...) โ†’ session.run_root(RootInput(...)) โ†’ agent runtime (Root โ†’ Analysis โ†’ bottleneck.classify โ€ฆ) โ†’ Recommendation hands off to specialist โ†’ Correctness runs 5 gates โ†’ formatters โ†’ text / json / markdown / html ``` ### Interactive / backend TUIs ``` user โ†’ perfxpert-code โ†’ default patched opencode path (repo-local build in source checkouts, bundled binary in wheels) OR native claude / gemini / codex CLI โ†’ perfxpert-mcp + staged prompt / gate config as needed โ†’ same agent runtime under the hood ``` ### Library API ```python # Agentic library API โ€” v0.2.0+ from perfxpert.agents.runtime import build_session from perfxpert.agents.schemas import RootInput session = build_session(airgap=True) # or provider='anthropic' output = session.run_root(RootInput( user_query="Summarize the primary bottleneck.", airgap=True, )) print(output.primary_bottleneck) ``` ### MCP + Python embedding External clients and in-process callers use the same underlying brain: - `perfxpert.api.agent_*` calls the same session runners directly. - `perfxpert-mcp` re-exposes the same READ_ONLY agent and classifier tools over MCP. - `perfxpert-code` backends differ only in TUI / config / gate-plumbing, not in analysis semantics. ## Test pyramid (spec ยง6) ``` Level 5 โ€” Benchmarks (nightly) TritonBench + KernelBench Level 4 โ€” End-to-end CLI + SDK (PR) pytest tests/e2e Level 3 โ€” Agent integration (PR) pytest tests/test_integration Level 2 โ€” Per-agent isolation (PR) pytest tests/test_agents Level 1 โ€” Per-tool unit (PR) pytest tests/test_tools Level 0 โ€” Knowledge YAML (PR) pytest tests/test_knowledge ``` ## Correctness gates (spec ยง5) 1. **Claims** โ€” magnitude within proven_optimizations range 2. **Sakana** โ€” hardware-counter sanity 3. **Schema** โ€” output shape valid 4. **Regression** โ€” no hot kernel regressed > 5% (weighted-geomean definition) 5. **Correctness** โ€” semantic preservation (structural) ## What's NOT in this diagram The following symbols were deleted during the agentic refactor and are no longer present: - `interactive.py`, `llm_conversation.py` โ€” bespoke LLM-session state machine (removed in Phase 7.1), superseded by OpenAI Agents SDK sessions. - `perfxpert/ai_analysis/` module โ€” removed in Phase 7.1 and superseded by `perfxpert/agents/`. Consult the git history or [CHANGELOG.md](../CHANGELOG.md) for the old code. ## Pointers - Full spec and phase plans: living documents in the contributor's working copy (not tracked in the repo). - Contributor entry: `CONTRIBUTING.md` - RFCs: `docs/rfcs/`