Skip to content

Repository files navigation

TypeLens — Type-Aware Code Repair Agent

A "type-aware" code repair agent prototype that uses a language server (LSP) / static type system for context retrieval and post-generation validation, replacing traditional vector retrieval (RAG).

Core ideas drawn from Statically Contextualizing LLMs with Typed Holes (ChatLSP), Copiloting the Copilots (Repilot), and ContextBench.

How it differs from a generic coding agent

Generic RAG agent This prototype
Locating code Vector-similarity search, easily misled by lexically similar but semantically irrelevant text Static type retrieval: recursively expands symbol definitions along type-reference relationships
Post-generation validation Mostly none / tests only Pyright type checking + tests, with diagnostics fed back for self-correction (≤2 rounds)
Hallucination Fabricates non-existent fields/types Type errors are caught by static checks immediately after generation

Project layout

src/
  lsp_retriever.py   # AST symbol table + type-alias expansion + Pyright diagnostics
  agent.py           # retrieve → generate → validate → self-correct main loop
  llm.py             # Zero-dependency LLM client (OpenAI-compatible + Anthropic)
  __main__.py        # CLI entry point
mcp_server.py        # MCP server exposing retrieval/repair as stdio tools
web/
  app.py             # Flask web demo (retrieval + mock self-correction + real LLM)
  templates/index.html
examples/
  buggy.py           # Sample bug (order.username does not exist)
  test_buggy.py      # Test used to verify the fix
  rag_vs_typelens.py # RAG (lexical similarity) vs TypeLens (type references) comparison
tests/
  test_retriever.py  # Retriever unit tests

Quick start

pip install pyright            # type checking (optional but recommended)
pip install pytest             # to run the verification tests

# 1) Retrieval only (no API key required)
python -m src examples/buggy.py "get_user_name returns order.username but that field does not exist" --no-llm

# 2) Full repair (requires an LLM)
export LLM_API_KEY=... LLM_MODEL=gpt-4o-mini
python -m src examples/buggy.py "get_user_name returns order.username but that field does not exist" \
  --test "python -m pytest test_buggy.py -q"

MCP server

Expose retrieval / repair as MCP tools (stdio) that can be called by Claude, Cursor, and other MCP clients:

pip install mcp           # requires the MCP SDK (v2.x uses MCPServer)
python mcp_server.py      # tools: retrieve_context, repair

Web demo

Visualize the "retrieve → generate → validate → self-correct" loop (mock mode needs no API key):

pip install flask
python web/app.py         # open http://127.0.0.1:5000

RAG comparison

python examples/rag_vs_typelens.py   # shows RAG getting distracted by get_username while TypeLens hits the exact type chain

Chinese models work through any OpenAI-compatible endpoint:

export LLM_PROVIDER=openai
export LLM_BASE_URL=https://api.deepseek.com/v1
export LLM_MODEL=deepseek-chat
export LLM_API_KEY=...

References

  • Typed Holes / ChatLSP — static type retrieval as a replacement for vector retrieval
  • Repilot — language-server pruning and validation during/after generation
  • ContextBench — retrieval precision is the core bottleneck of coding agents
  • CodexGraph — an alternative form of structured code retrieval (graph database)

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages