How unknownswordsman/codegraph works
1. What this repo is
- CodeGraph is a TypeScript/Node-based CLI plus embeddable library that builds a local code knowledge graph for a project.
- The core product boundary is the per-project .codegraph/ directory, which holds the SQLite index and metadata; everything else orchestrates how that index is built, queried, kept fresh, and exposed to AI agents.
2. Top-level runtime split
- There are two execution modes:
- Bundled CLI/runtime for end users (src/bin, install scripts, bundle scripts).
- Library/API mode for embedding in another Node 22.5+ process (src/index.ts and exports from the package entrypoint).
- The CLI is what agents and users interact with; the library is the same engine used programmatically.
3. Indexing pipeline
- The indexing flow lives mainly under src/extraction, src/resolution, src/graph, and src/db.
- Source files are parsed with tree-sitter, language-specific extractors identify symbols/edges, then a resolution pass connects references across files, imports, call sites, inheritance, and framework conventions.
- The result is stored in SQLite with FTS5 for search.
4. Storage boundary
- src/db defines schema, migrations, and query helpers for the on-disk graph database.
- The repository uses SQLite as the single source of truth; the architecture is intentionally local-first and does not depend on a remote indexing service.
- Tests around reopen/locking/performance indicate the DB layer is tuned for concurrent read-heavy access and resilience to file replacement.
5. Graph and query subsystem
- src/graph and src/search provide read/query primitives over the indexed data: symbol lookup, callers/callees, impact radius, and search parsing.
- src/context builds “agent-ready” context bundles from the graph: ranked symbols, formatted snippets, and boundary markers used to answer questions directly.
- This is the main data-serving layer once indexing is complete.
6. Resolution and cross-language synthesis
- src/resolution is where CodeGraph becomes more than syntax extraction.
- It contains framework resolvers, import resolution, path aliases, workspace package handling, and synthetic edge generators for hard-to-follow flows such as callbacks, dispatch systems, Swift/ObjC bridging, React Native bridges, Expo modules, and framework-specific route conventions.
- These heuristics are a major architectural boundary because they close gaps that raw AST parsing cannot.
7. Language frontends
- src/extraction/languages contains per-language extractors for TypeScript/JavaScript, Python, Go, Rust, Java, C#, PHP, Ruby, C/C++, Swift, Kotlin, Scala, Dart, Lua/Luau, R, Liquid, Pascal/Delphi, and several frontend frameworks.
- Some extra file-format extractors exist alongside the language layer, such as Astro, Svelte, Vue, Liquid, Razor, MyBatis, and DFM-like forms.
- The extractors are modular, which makes language support an additive subsystem rather than a monolith.
8. MCP server architecture
- src/mcp implements the server that AI agents actually talk to.
- It exposes the CodeGraph query surface through MCP, manages session lifecycle, transport, query workers, daemon/shared-process behavior, and watchdogs for liveness and parent-process death.
- This layer is the boundary between agent tool calls and the local graph database.
9. Freshness and syncing
- src/sync handles watch-based incremental updates using native file-system events, debounce policy, git-hook support, and worktree awareness.
- The architecture favors “always fresh” indexes: change detection feeds incremental resyncs, and stale-file safeguards are surfaced to the agent when the graph may lag behind the working tree.
- This is important because the agent-facing answers depend on the current project state, not a stale snapshot.
10. Installer and agent integration
- src/installer and scripts/add-lang / install.sh / install.ps1 are the operational layer that wires CodeGraph into external agents and user machines.
- The installer detects supported agents, writes their MCP configuration, and optionally injects instructions into agent-specific guidance files.
- This is the integration boundary to Claude Code, Cursor, Codex, opencode, Gemini, Antigravity, Kiro, and Hermes.
11. Telemetry and external service boundary
- src/telemetry collects anonymous usage data, while telemetry-worker/ hosts the ingestion endpoint as a separate deployable service.
- The architecture keeps code-content and file/path data local; telemetry is limited to aggregated usage metadata.
- In contrast to the core product, telemetry is the only clearly separate cloud-facing subsystem in the repo.
12. Distribution, website, and validation
- site/ is an Astro documentation/marketing site published via GitHub Pages workflows.
- scripts/ contains release, packaging, benchmark, and evaluation tooling; this repo treats release engineering and agent-eval experiments as first-class operational concerns.
- __tests__ is extensive and maps directly onto the major boundaries above, especially extraction, resolution, daemon/watch behavior, MCP behavior, installer behavior, and SQLite persistence.
13. End-to-end data flow
- A user initializes a project → the CLI scans source files → extractors build symbols and raw edges → resolvers add cross-file/framework/bridge links → SQLite stores the graph → MCP queries read from that graph → watch/sync keeps it current as files change.
- The same graph also powers CLI commands like search, callers/callees, impact analysis, and affected-test detection.
14. Primary architectural shape
- This repo is best understood as a local indexing engine with three outer shells:
- Build shell: extraction + resolution + storage.
- Serve shell: CLI + MCP + query/context APIs.
- Operate shell: installer, watcher/sync, telemetry, packaging, and docs.
- The center of gravity is the project-local SQLite graph; everything else exists to populate it, keep it fresh, or expose it to agents.
Main entry points: graph/index.ts Graph API · mcp/index.ts MCP server · extraction/index.ts Extractors · codegraph.ts CLI
Components 18
Build shell
Extraction, resolution, and SQLite indexing pipeline.
- Extractors · Tree-sitter and language-specific symbol/edge extraction.
src/extraction/index.ts - Languages · Per-language and file-format extractors for supported syntaxes.
src/extraction/languages/index.ts - Resolution · Import, alias, framework, and synthetic edge resolution.
src/resolution/index.ts - Framework rules · Framework-specific heuristics for routes, dispatch, and bridges.
src/resolution/frameworks/index.ts - SQLite DB · On-disk project database with schema, migrations, and query helpers.
src/db/index.ts
Serve shell
CLI, MCP server, graph queries, and agent-facing context.
- CLI · Bundled command-line entrypoint and process supervision.
src/bin/codegraph.ts - Library API · Embeddable Node API surface for programmatic use.
src/index.ts - Graph API · Symbol lookup, traversal, callers/callees, and impact analysis.
src/graph/index.ts - Search parser · Search query syntax and parsing utilities over indexed content.
src/search/query-parser.ts - Context builder · Ranks symbols and formats snippets into answer-ready bundles.
src/context/index.ts - MCP server · Agent-facing server, session management, workers, and transport.
src/mcp/index.ts
Operate shell
Freshness, installation, telemetry, packaging, and docs.
- Sync watcher · File watching, debounce, git-hook, and worktree-aware incremental resync.
src/sync/index.ts - Installer · Configures external agents and writes MCP integration settings.
src/installer/index.ts - Telemetry · Anonymous usage collection and client-side telemetry hooks.
src/telemetry/index.ts - Reasoning · Support for user configuration, credentials, and reasoning workflows.
src/reasoning/reasoner.ts - Upgrade · Upgrade and compatibility flows for installed environments.
src/upgrade/index.ts - Docs site · Astro documentation and marketing site.
site/src/pages/index.astro - Telemetry worker · Separate deployable ingestion endpoint for telemetry.
telemetry-worker/src/index.ts
Connections 21
- CLI → Extractors: indexes · CLI drives project scanning and extraction.
- Extractors → Languages: delegates · Shared extraction core dispatches to per-language frontends.
- Extractors → Resolution: raw edges · Extraction outputs symbols and unresolved relationships into resolution.
- Resolution → Framework rules: applies · Framework resolvers add synthetic and convention-based links.
- Resolution → SQLite DB: stores · Resolved graph is persisted in SQLite.
- SQLite DB → Graph API: reads · Graph queries are served from the project-local database.
- Graph API → Search parser: parses · Search layer interprets user query syntax.
- Graph API → Context builder: feeds · Query results become agent-ready context bundles.
- MCP server → Graph API: serves · MCP exposes graph queries to external agents.
- MCP server → Context builder: formats · Agent responses are assembled from graph context.
- CLI → MCP server: hosts · CLI starts and supervises MCP sessions and transports.
- Sync watcher → SQLite DB: refreshes · File-system changes trigger incremental graph updates.
- Sync watcher → MCP server: signals · Staleness and catch-up state affect agent-facing behavior.
- Installer → MCP server: configures · Installer writes agent configuration for MCP access.
- Installer → CLI: installs · Operational scripts wire the CLI into user environments.
- Telemetry → Telemetry worker: reports · Client telemetry is sent to the separate ingestion service.
- Library API → Extractors: embeds · Programmatic API uses the same indexing engine.
- Library API → Graph API: queries · Library consumers can read the graph directly.
- Upgrade → CLI: supports · Upgrade flow keeps installed CLI/runtime compatible.
- Docs site → Installer: documents · Docs site explains installation and integrations.
- Docs site → Graph API: explains · Documentation covers core graph concepts and queries.