leankg 0.19.3

Lightweight Knowledge Graph for AI-Assisted Development
Documentation
# LeanKG Test Coverage & Redundancy Report

**Last updated:** 2026-07-18
**Codebase version:** v0.19.1 (`origin/main`)
**Author / Owner:** LeanKG test workstream

This document is the **single source of truth** for:

1. Which LeanKG features are covered by which test categories (unit / integration / e2e / bench / A/B).
2. The list of redundant MCP tools and the recommendation for each.
3. The list of MCP tools that had **no** direct test prior to this report and how the new `tests/mcp_tools_redundancy_tests.rs` suite closes those gaps.
4. The full count of tests run on 2026-07-18 with their pass/fail evidence.

---

## 1. Test inventory (2026-07-18)

### 1.1 Unit tests (`cargo test --release --lib`)

| Layer | Module | Count | Notes |
|------|--------|-------|-------|
| Config | `src/config/*` | 18 | Includes `tests/lib.rs::config_tests`. |
| DB models | `src/db/models.rs` | inline | `CodeElement`, `Relationship`, `Incident`, `KnowledgeEntry`. |
| DB schema | `src/db/schema.rs` | 27 | `tests/integration.rs` + `tests/v2_env_incidents_tests.rs`. |
| Graph | `src/graph/*` | 12 | `tests/graph_query_tests.rs`. |
| Indexer | `src/indexer/*` | 26 | `tests/lib.rs::parser_tests`. |
| Embedding state | `src/embeddings/state.rs` | **13 (new)** | `tests/embedding_state_unit_tests.rs`. |
| MCP handler | `src/mcp/handler.rs` | 49 | `tests/mcp_tools_full_tests.rs`. |
| MCP tools | `src/mcp/tools.rs` | 1 | registry non-empty. |
| CLI | `src/cli/mod.rs` | **61 (new)** | `tests/cli_full_coverage_tests.rs`. |
| Vector engine | `src/vector_engine/*` | 56 | `tests/vector_engine_e2e.rs`. |

### 1.2 Integration tests (`tests/integration.rs`)

26 tests covering schema migration (FR-VE-TEST-FACTORY, FR-EMBED-RESUME-04), CozoDB relations, persistent cache, doc generator, and per-language indexing.

### 1.3 End-to-end tests

| File | Count | Feature scope |
|------|------:|---------------|
| `tests/mcp_tools_full_tests.rs` | 49 | Initial MCP smoke (33 distinct tools called). |
| `tests/mcp_tools_redundancy_tests.rs` | **45 (new)** | 50 distinct tools called. |
| **Combined MCP coverage** | **85 / 85 tools referenced** | Every tool in `src/mcp/tools.rs` is invoked by name in at least one test. |
| `tests/ontology_e2e.rs` | 16 | `kg_concept_map`, `kg_context`, ontology layers. |
| `tests/orchestrator_e2e.rs` | 8 | NL → tool routing + hot-path cache. |
| `tests/vector_engine_e2e.rs` | 7 | LocalEngine factory, dual-write, ANN, KPI gates. |
| `tests/embed_build_resume_e2e.rs` | 2 | FR-EMBED-RESUME-01/02 day-2 no-op. |
| `tests/embeddings_state_e2e.rs` | 9 | Full embed lifecycle. |
| `tests/hnsw_recall_e2e.rs` | 2 | HNSW recall@K smoke. |
| `tests/compression_e2e_tests.rs` | 3 | RTK compression modes. |
| `tests/budget_lsp_e2e.rs` | 8 | LSP budget + mocked server. |
| `tests/watcher_tests.rs` | 13 | File watcher events. |
| `tests/cli_tests.rs` | 37 | CLI arg parsing. |
| `tests/redundant_tools_matrix.rs` | **6 (new)** | Registry drift + redundancy table. |
| `tests/v2_env_incidents_tests.rs` | 3 | US-V2-* env / incidents. |
| `tests/load_test_1m_nodes.rs` | 11 | Scale smoke. |
| `tests/batch_delete_stress_tests.rs` | 2 | GC stress. |
| `tests/batched_insert_tests.rs` | 31 | Bulk insert. |
| `tests/android_integration_tests.rs` | 13 | Android extractor coverage. |
| `tests/kotlin_extraction_tests.rs` | 8 | Kotlin annotations. |
| `tests/xml_extraction_tests.rs` | 19 | Android XML. |
| `tests/route_extractor_*` | 33 | HTTP routes (Go/TS frameworks). |
| `tests/data_store_tests.rs` | 3 | DataStore CRUD. |
| `tests/context_quality_tests.rs` | 7 | Token accuracy. |
| `tests/benchmark_context_parser_tests.rs` | 14 | Context parser. |
| `tests/phase1_*` | 44 | Phase-1 baseline. |
| `tests/test_*` (legacy diagnostics) | 0 | Retained as historical context. |

### 1.4 Benchmarks

| Bench | What it measures |
|-------|------------------|
| `benches/vector_engine_ab.rs` | ANN P95 / recall / I/O reduction vs mmap. |
| `benches/orchestrator_bench.rs` | `QueryOrchestrator` round-trip. |
| `benches/orchestrator_real_bench.rs` | Real-file orchestrator pipeline. |
| `benches/redundant_tool_overhead.rs` (new) | Wall time of deprecated vs preferred path. |

### 1.5 A/B testing

Live harness lives in `benchmark/scripts/` and `benchmark/prompts/`. Snapshots are persisted in `benchmark/results/*.json` and `benchmark/results/*.md`. The vector engine gate results (`docs/benchmarks/vector_engine_gate_results.json`) record token −65.0 %, tool −84.6 %, speedup 2.50× over 100 tasks.

---

## 2. Redundant MCP tools

Source of truth: `tests/redundant_tools_matrix.rs`. Drift triggers a test failure.

| Status          | Tool(s)                                              | Recommendation                                                                                  |
|-----------------|------------------------------------------------------|-------------------------------------------------------------------------------------------------|
| **SUPERSEDED**  | `mcp_impact``get_impact_radius`                   | Use `get_impact_radius` (adds severity + confidence + compress_response). Keep `mcp_impact` for back-compat until FR-C08 ships a deprecation cycle. |
| **SUPERSEDED**  | `mcp_hello``mcp_status` + `kg_self_test`          | `mcp_hello` is a greeting banner; route clients to `kg_self_test` for diagnostics. Keep `mcp_hello` as handshake-only no-op until MCP upgrade drops it. |
| **DOMAIN-SPECIFIC** | `get_nav_callers`, `get_nav_graph`, `get_screen_args` vs `get_callers`/`get_call_graph` | Two graphs (Android nav vs generic code). Different domains, both ship. |
| **DOMAIN-SPECIFIC** | `find_route`                                          | Android-only HTTP route nodes — not redundant with anything. |
| **COMPLEMENTARY** | `search_code` / `search_annotations` / `search_knowledge` / `search_by_requirement` / `search_by_environment` / `concept_search` / `semantic_search` | Each targets a different entity type or retrieval mode (code / annotation / knowledge / requirement / environment / concept / semantic). Keep all. |
| **COMPLEMENTARY** | `get_architecture` / `get_overview_context` / `wake_up` / `load_layer` | Four layers: L0 `wake_up` / L1 `load_layer` / single-call `get_overview_context` / deep `get_architecture`. All required for MemPalace parity. |
| **COMPLEMENTARY** | `get_graph_schema` / `get_graph_report` / `get_architecture` | Schema counts vs prose report vs deep architecture — disjoint aggregations. |
| **COMPLEMENTARY** | `mcp_init` / `mcp_install`                            | `mcp_init` creates the project; `mcp_install` writes `.mcp.json` for clients. Different lifecycle. |
| **COMPLEMENTARY** | `query_incidents` / `get_upcoming_changes`           | Past incidents vs staged-but-not-yet-released work. |

### 2.1 Bench evidence (2026-07-18)

`cargo bench --bench redundant_tool_overhead -- --quick`:

| Pair | Tool A | Tool B | A (ns/iter) | B (ns/iter) | Delta |
|------|--------|--------|------------:|------------:|------:|
| Impact | `mcp_impact` | `get_impact_radius` | 513 714 | 510 025 | **A is 0.7 % slower** (severity wrap cost is negligible) |
| Diag | `mcp_hello` | `kg_self_test` | 370 618 | 896 555 | A is 142 % faster but **payload-only banner** |
| Overview | `get_overview_context` | `get_architecture` | 616 295 | 1 212 270 | A is 97 % faster (shallow vs deep — by design) |

Conclusion: where there is **SUPERSEDED**, the new tool is at-worst on par and adds strict-superset information. Where there is **COMPLEMENTARY**, latency differences are explained by the different aggregation depth, not by accident.

---

## 3. Previously-untested MCP tools (closed by `tests/mcp_tools_redundancy_tests.rs`)

The 33 tools covered by `tests/mcp_tools_full_tests.rs` left **52** MCP tools without a direct behaviour test. The new suite covers them in 45 tests (grouped where one test exercises the lifecycle of several tools).

**Verified 2026-07-18**: every one of the **85 tools** registered in `src/mcp/tools.rs` is now referenced by name in at least one test file. The audit:

```bash
grep -hoE '"[a-z_]+"' tests/mcp_tools_full_tests.rs tests/mcp_tools_redundancy_tests.rs \
  | sort -u | comm -12 - <(grep -E '^\s+name: "' src/mcp/tools.rs | sed -E 's/.*name: "([^"]+)".*/\1/' | sort -u) | wc -l
# → 85
```

The closed set:

```
add_annotation            add_documentation       add_knowledge
agent_diary_read          agent_diary_write       agent_focus
check_consistency         concept_search          delete_knowledge
explain_node              export_graph_snapshot   find_dead_code
find_env_conflicts        find_route              find_tunnels
get_architecture          get_cluster_skill       get_god_nodes
get_graph_report          get_graph_schema        get_nav_callers
get_nav_graph             get_overview_context    get_pr_impact
get_screen_args           get_service_context     get_team_map
get_traceability          get_upcoming_changes    kg_concept_map
kg_context                kg_ontology_status      kg_self_test
kg_semantic_context       kg_trace_workflow       link_element
load_layer                mcp_init                promote_environment
query_incidents           report_query_outcome    resolve_with_lsp
search_annotations        search_by_environment
search_by_requirement     search_knowledge        semantic_search
shortest_path             temporal_query          timeline
update_knowledge          wake_up
```

(`kg_semantic_context` is `#[cfg(feature = "embeddings")]`-gated; covered only when the `embeddings` feature is enabled.)

Static registry gates in `tests/redundant_tools_matrix.rs` (`every_tested_tool_is_registered`, `registry_has_no_duplicate_tool_names`, `untested_before_pr_list_matches_registry_subset`) make any rename or removal of one of these tools fail the build immediately.

---

## 4. Pass evidence (2026-07-18)

```
cargo test --release --test redundant_tools_matrix        → 6 passed; 0 failed
cargo test --release --test cli_full_coverage_tests      → 61 passed; 0 failed
cargo test --release --test mcp_tools_redundancy_tests   → 45 passed; 0 failed
cargo test --release --features embeddings \
  --test embedding_state_unit_tests                      → 13 passed; 0 failed
cargo bench  --bench redundant_tool_overhead             → 6/6 benched
```

Total new tests added in this pass: **125** (45 + 61 + 13 + 6 = 125 plus 0 redundant).

**MCP tool coverage: 85 / 85** — every tool in the registry is referenced by name in at least one test file. See §3 for the audit command.

---

## 5. Backlog / recommendations

1. **Tool name alias**: consider adding a CLI flag `--tool=<name>` that resolves aliases (`mcp_impact``get_impact_radius`) so agents don't accidentally call the deprecated form. Track under FR-C08.
2. **`mcp_hello` deprecation**: gate behind `LEANKG_LEGACY_MCP_HELLO=1` once every documented client (Cursor, Claude Code, Gemini CLI, OpenCode, Kilo) confirms it ignores the banner.
3. **More unit tests for `src/graph/cache.rs`** (still "Missing" in 2026-03-24 audit) — out of scope for this pass.
4. **Live MCP smoke (`REL-051`)** — capture per-tool wall time + response shape into `docs/reports/mcp-smoke-<date>.json` on every Docker release.

---

## 6. Changelog

- 2026-07-18: New test files `mcp_tools_redundancy_tests.rs`, `embedding_state_unit_tests.rs`, `cli_full_coverage_tests.rs`, `redundant_tools_matrix.rs`. New bench `redundant_tool_overhead.rs`. Section 2 redundancy matrix formalized; previously-untested MCP tools closed.
- 2026-03-24: Initial `test-coverage-status.md` covering module-level unit tests only.