$ show FreePeak/LeanKG
LeanKG is a Go-based knowledge graph engine for AI-assisted development. It indexes source code from multi-language repositories into a graph of elements (functions, types, classes) and relationships (calls, imports, implements), stored in SQLite or PostgreSQL with optional vector embeddings for semantic search. The system provides a CLI, REST API, MCP (Model Context Protocol) server, and a React web UI (force-directed graph, file tree, code panel). Core capabilities include incremental indexing with three-signal change detection, a four-tier query ladder (cold/exact/fuzzy/semantic), impact analysis with blast radius, organizational knowledge management (incidents, notes, env reports), ontology matching, federation across servers, and token-budgeted compression for AI tool integration. The architecture is modular: the store backend interface supports SQLite and PostgreSQL with a shared migration ledger; indexing uses regex extractors for 40+ languages with an optional tree-sitter CGO tier; embeddings use a provider abstraction (OpenAI-compatible, llama.cpp sidecar, deterministic). The codebase is well-structured with extensive tests, hermetic test patterns, and deterministic output design. Notable reusability includes the compress package (shell output, git diff), auth token system, store abstraction pattern, and A/B benchmarking harness.
The LLM identified 5 files worth highlighting for reuse:
Provides adaptive compression for shell output, git diffs, cargo test results, and file reader responses. Uses LITM reordering (L-curve reordering by line category), entropy-based line filtering, symbol maps for identifier shortening, and session caching with eviction scoring. Self-contained package with no imports from internal LeanKG packages beyond standard library and compressed types; could be extracted as a standalone library for compressing command output in AI tool contexts.
Implements RBAC token-based authentication with Admin, Contributor, and Viewer roles. Provides Mint (SHA-256 hashed token creation), Verify (token lookup with expiry check), role clamping (can only mint tokens at or below own role), and soft/hard revocation. Stores tokens in DB via AuthTokenStore interface. Self-contained, no deep internal imports beyond store interface; could be reused for any application needing token auth with role hierarchy.
Defines a Judge interface for learning-to-rank judgments over typed questions (choice, score, noul). Implements a server backend (HTTP) and a local sidecar (Python Laya), with confidence-gated fallback to native rules on failure. Provides question batcher with cost/retry logic. Clean interface with clear separation of concerns; could be extracted to add learned quality judgments to any system.
Defines the Backend interface (OpenBackend, Migrate, elements, relationships, vectors, FTS, tokens, accounts, audit, metrics, etc.) with compile-time checks. The SQLite and PostgreSQL implementations share a common test suite pattern. The interface is comprehensive yet focused on graph+embedding storage; the abstraction pattern is directly reusable for any application needing switchable storage backends.
Production-grade A/B test harness for measuring LLM agent tool impact. Records per-trial output with pinned SHA256 provenance (corpus, tool, prompt hashes), scores with blind judgment, enforces minimum trials per arm, and checks a zg pitfalls checklist. Supports configurable arms (with/without LeanKG), task-driven corpus generation, and scoring output to JSON/CSV. Self-contained in benchmark directory; could be extracted as a generic LLM tool benchmarking framework.