cased-kit

repository·main·Indexed 23 days ago

https://github.com/cased/kit

A modular toolkit for LLM-powered codebase understanding, version 3.5.1. It provides a Python CLI and TypeScript client for codebase mapping, symbol extraction, AST pattern search, and semantic search. Features include AI-powered PR reviews, commit message generation, and a Model Context Protocol (MCP) server for AI assistants. It supports local and remote repository analysis and integrates with Claude Code via a dedicated plugin.

Tokens
94.2K
Snippets
246
Records
428
Agent score
79%

What's inside cased-kit

  1. Overview of kit Code Intelligence Toolkit

    main

    What is kit?

    kit is a Python toolkit developed by Cased designed for building LLM-powered developer tools and workflows. Its primary purpose is to provide precise, accurate, and relevant code context to Large Language Models (LLMs).

    Key Capabilities

    • Codebase Mapping: Provides structured views including file trees, language-aware symbol extraction (using tree-sitter), and dependency insights. It supports over 12 languages and includes intelligent caching.
    • Hybrid Search: Supports multiple search methods, allowing you to combine fast text search with semantic vector search to optimize for speed and accuracy.
    • Docstring Context: Uses generated docstrings to facilitate finding code snippets, answering questions, and improving code generation through summarized content.
    • AI Workflow Utilities: Provides ready-made utilities for code chunking, context retrieval, and LLM interaction, including specialized features like PR summarization and intelligent commit message generation.
  2. Overview of kit capabilities

    main

    The kit toolkit is designed for codebase mapping, symbol extraction, code search, and LLM-powered developer workflows. It uses tree-sitter for multi-language support and provides a "mid-level API" for building custom tools like code review bots, semantic search, and documentation generators.

    Key value propositions include:

    • Unifying Code Access: A consistent Repository object for interacting with files, symbols, and search across different languages.
    • Deep Code Understanding: Accurate, language-specific parsing via tree-sitter for structural analysis.
    • Bridging Code and LLMs: Specialized tools for effective code chunking and context retrieval for LLMs.
  3. Understand the Symbol data structure

    main

    A symbol is a dictionary representing a code element. It contains the following properties:

    • name: The identifier of the symbol (e.g., function name).
    • type: The category of the symbol (e.g., function, class, method, variable, constant).
    • file: The file path where the symbol is located.
    • start_line: The 1-indexed starting line number.
    • end_line: The 1-indexed ending line number (inclusive).
    • code: The raw source code of the symbol.
    • language: The programming language of the symbol.
    {
        "name": "authenticate_user",
        "type": "function",
        "file": "src/auth.py",
        "start_line": 42,
        "end_line": 58,
        "code": "def authenticate_user(...):\n    ...",
        "language": "python"
    }
  4. Understand the Kit Client Library design principles

    main

    All Kit client libraries (including the existing TypeScript client and planned Go, Rust, and Ruby clients) follow a consistent architectural pattern:

    • Shell out to CLI: Clients wrap the Kit CLI rather than reimplementing functionality. This ensures the client remains a lightweight interface to the core logic.
    • Type safety: Clients provide strong typing for all commands and options.
    • Async/Promise-based: Clients use language-appropriate async patterns (e.g., Promises in TypeScript).
    • Error handling: Clients parse and wrap CLI errors into language-specific error types.
    • Zero Python dependencies: To use a client, you only need the Kit CLI installed; you do not need to install any Python packages in your project environment.
  5. Use incremental symbol extraction for high performance

    main

    For large repositories, use extract_symbols_incremental(file_path=None) instead of the standard extract_symbols(). This method uses intelligent caching (based on mtime, size, content hash, and git state) to provide 25-36x speedups on warm caches.

    Performance Characteristics:

    • Cold cache: Full analysis with cache building.
    • Warm cache: Extremely fast using cached results.
    • Automatic invalidation: The cache is automatically invalidated when git state changes or files are modified.

    Monitoring Performance: You can call get_incremental_stats() to view cache performance metrics.

    # First call builds cache
    symbols = repository.extract_symbols_incremental()
    
    # Subsequent calls use cache (much faster)
    symbols = repository.extract_symbols_incremental()
    
    # Check performance
    stats = repository.get_incremental_stats()
    print(f"Cache hit rate: {stats['cache_hit_rate']}")
  6. Manage Cache Memory and Thread Safety

    main

    The incremental analysis cache includes built-in memory management and specific requirements for concurrency:

    Memory Management

    • LRU eviction: Uses a Least Recently Used strategy to prevent unlimited memory growth.
    • Configurable cache size: The default limit is 10,000 files.
    • Automatic cleanup: Stale entries are cleaned up automatically.

    Thread Safety

    The cache is designed for single-threaded use. If you are working in a multi-threaded environment, you must create a separate Repository instance for each thread to avoid concurrency issues.

    # Create separate instances per thread
    def worker_thread():
        repo = Repository("/path/to/repo")  # New instance
        symbols = repo.extract_symbols_incremental()
  7. Understand the Incremental Analysis Cache Structure

    main

    Kit uses an incremental analysis system to speed up symbol extraction by caching file metadata and symbol data. This data is stored locally within the repository's .kit directory.

    Key files in the cache include:

    • .kit/incremental_cache/analysis_metadata.json: Stores file metadata such as mtime (modification time), file size, and hashes to detect changes.
    • .kit/incremental_cache/symbols_cache.json: Stores the actual cached symbol data.
  8. How to use the DependencyAnalyzer API

    main

    The DependencyAnalyzer class and its derivatives analyze dependencies between components in a codebase. They help detect circular dependencies, export dependency graphs, and generate LLM-friendly context about architecture.

    Because DependencyAnalyzer is an abstract base class, you should not instantiate it directly. Instead, use the Repository.get_dependency_analyzer(language) factory method to obtain the correct implementation for your target language (e.g., 'python' or 'terraform').

    from kit import Repository
    
    repo = Repository("/path/to/your/codebase")
    analyzer = repo.get_dependency_analyzer('python')  # or 'terraform'
  9. How incremental analysis and caching work in kit

    main

    Kit uses an incremental analysis system to avoid re-parsing unchanged files, significantly improving performance for repeated operations. The system consists of two main components:

    • FileAnalysisCache: Manages file-level caching using multiple invalidation strategies (mtime, file size, content hash, and git state).
    • IncrementalAnalyzer: Orchestrates the analysis process, tracks performance statistics, and manages the cache.

    When calling extract_symbols_incremental(), Kit performs file discovery, detects changes via metadata or hashes, selectively analyzes only the changed files using tree-sitter, and retrieves results for unchanged files from the cache. This makes analysis time proportional to the number of changes rather than the total repository size.

    from kit.repository import Repository
    
    # Create repository instance
    repo = Repository("/path/to/your/project")
    
    # First analysis - builds cache
    symbols = repo.extract_symbols_incremental()
    
    # Second analysis - uses cache (much faster)
    symbols = repo.extract_symbols_incremental()
  10. Configure LLM providers for kit

    main

    Kit supports multiple LLM providers for AI-powered features like code summarization, PR reviews, and commit message generation.

    Native Providers

    Kit has built-in support for:

    • OpenAI (GPT-4, GPT-5, o-series)
    • Anthropic (Claude Opus, Sonnet, Haiku)
    • Google (Gemini Pro, Flash)
    • Ollama (Local models, no API key required)

    OpenAI-Compatible Providers

    You can use OpenAIConfig with a custom base_url to access services like OpenRouter, Grok (X.AI), Groq, Together AI, or local servers (vLLM, text-generation-webui).

    Note: Core repository analysis features (symbol extraction, file trees, search, dependency analysis) do not require an LLM and work without any API keys.

  11. Choose the right search method in Kit

    main

    Kit provides four primary search and discovery methods depending on your goal:

    1. Text Search (Regex): Best for finding exact strings, patterns, security tokens (e.g., API_KEY), or TODOs. It is extremely fast and requires no setup.
    2. Symbol Search (Tree-sitter): Best for finding code by name (functions, classes, methods) and analyzing code structure. It is very fast and uses cached language-aware extraction.
    3. Semantic Search (Embeddings): Best for finding code by meaning when you don't know exact keywords (e.g., "How is authentication handled?"). Requires an embedding model and index building.
    4. Docstring Search (LLM + Embeddings): Best for understanding high-level intent or purpose (e.g., "retry logic with backoff"). It uses LLM-generated summaries and is the slowest method, requiring an LLM API key and indexing.
  12. How the kit Claude Skill works

    main

    The kit-cli plugin functions as a Claude Skill. This means you do not need to explicitly invoke kit commands. Instead, Claude autonomously decides when to use kit's tools based on the context of your natural language requests.

    For example:

    • If you ask "How does authentication work?", Claude may use kit symbols, kit search, and kit usages.
    • If you ask "Find all usages of the UserModel class", Claude will run kit usages UserModel.
    • If you ask "Show me the file structure of src/", Claude will run kit file-tree --subpath src.