code-knowledge-graph

module
v0.0.0-...-b0488a8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2026 License: AGPL-3.0

README

CKG — Code Knowledge Graph

CI

Build a queryable knowledge graph from a code path. Point CKG at a directory and it parses the source (Go / TypeScript / Solidity) into a graph of files, symbols, and relationships you can query from the CLI, an MCP-enabled LLM, or a 3D web viewer.

Features

  • Multi-language parsing — Go, TypeScript, Solidity via tree-sitter
  • Persistent graph store — SQLite (the maintained backend; the PostgreSQL backend is deprecated, see ADR-0003)
  • Multiple query surfaces — REST API, MCP server, 3D viewer
  • Incremental builds — file-level caching for fast re-indexing
  • Rich schema — 33 node kinds, dependency / call / git-history edges

Quick start

git clone https://github.com/0xmhha/code-knowledge-graph
cd code-knowledge-graph
make build-full   # viewer + binary; `make build` = binary only (no 3D viewer UI)

# 1. Build a graph from any code path
./bin/ckg build --src=/path/to/repo --out=/tmp/ckg

# 2. Query via HTTP + 3D viewer
./bin/ckg serve --graph=/tmp/ckg --open      # http://localhost:8080

# 3. Query from Claude Code via MCP
claude mcp add ckg --command ./bin/ckg --args "mcp,--graph=/tmp/ckg"

Building a graph

ckg build parses a source tree into a graph database (<out>/graph.db). Common patterns:

# Whole tree, auto-detect languages (Go / TypeScript / Solidity / proto)
./bin/ckg build --src=/path/to/repo --out=/tmp/ckg

# Restrict to specific languages
./bin/ckg build --src=/path/to/repo --out=/tmp/ckg --lang=go,sol

# Build from a specific commit — uses a git worktree, leaves the --src
# working tree untouched, and indexes only that commit's tracked files
./bin/ckg build --src=/path/to/repo --out=/tmp/ckg --at-commit=<sha>

# Index only a curated file set via a --files-from whitelist
./bin/ckg build --src=/path/to/repo --out=/tmp/ckg \
    --files-from=eval/stablenet/stablenet-files-with-tests.json

The build is deterministic: the same source tree + commit + ckg binary yields the same graph (see docs/adr/0002-staged-graph-composition.md).

Filtering (--files-from). A JSON { "include": [...], "exclude": [...] } of globs (** spans any path depth). Without it, ckg indexes the whole tree (every .go/.ts/.sol/.proto, tests included). Two ready-made filters for the go-stablenet corpus live under eval/stablenet/:

Filter Tests Use
stablenet-files.json excluded clean retrieval-eval corpus
stablenet-files-with-tests.json included knowledge-data (cks/ckv), keeps test few-shot value

Add --out-tag=auto-commit-hash to suffix the output dir with the source's short commit SHA (e.g. --out=/data/stablenet/data/stablenet-0bf2f4d1b). Run ckg build --help for the full flag list.

Commands

Command Purpose
ckg build Parse a code path into a graph database
ckg serve HTTP API + embedded 3D viewer
ckg mcp MCP server for LLM agents (Claude Code, etc.)
ckg export-static Export viewer + chunked JSON for static hosting
ckg export-postgres Migrate a SQLite graph to PostgreSQL (deprecated, ADR-0003)
ckg eval Compare graph-context vs raw-file context on benchmark tasks
ckg audit Validate graph integrity

Run ckg <command> --help for flags.

Production deployment

ckg serve ships an embedded viewer for local use. For shared deployments, split the static viewer from the API:

./bin/ckg export-static --graph=/tmp/ckg --out=/srv/ckg/static
./bin/ckg serve --graph=/tmp/ckg --port=8080 --no-viewer

Front both with a reverse proxy: /api/* → :8080, /* → /srv/ckg/static.

Documentation

Contributing

Contributions are welcome. To get started:

  1. Fork the repository and create a feature branch from main.
  2. Run make test and make lint before submitting.
  3. Use conventional commit prefixes (feat:, fix:, docs:, ...).
  4. Open a pull request describing the change and its motivation.

For larger changes, please open an issue first to discuss the design. Report bugs and request features at https://github.com/0xmhha/code-knowledge-graph/issues.

License

CKG is licensed under the GNU Affero General Public License v3.0 — see LICENSE.

AGPL-3.0 requires that if you run a modified version of CKG as a network-accessible service, you must make the corresponding source available to its users.

Directories

Path Synopsis
cmd
ckg command
cmd/ckg/bench_mcp.go — `ckg bench-mcp` measures p50/p95/p99 latency of every registered MCP tool against a real graph.db.
cmd/ckg/bench_mcp.go — `ckg bench-mcp` measures p50/p95/p99 latency of every registered MCP tool against a real graph.db.
eval-gate command
eval-gate is a CI regression gate that compares the latest `make eval` output against the committed baseline.
eval-gate is a CI regression gate that compares the latest `make eval` output against the committed baseline.
internal
audit
Package audit compares the authoritative Go build file set (go/packages.Load) against the file set recorded in the CKG database.
Package audit compares the authoritative Go build file set (go/packages.Load) against the file set recorded in the CKG database.
buildpipe
Package buildpipe — cache.go implements the A3 file-level incremental cache (spec v0.2 § 4 Phase 1).
Package buildpipe — cache.go implements the A3 file-level incremental cache (spec v0.2 § 4 Phase 1).
cluster
Package cluster — Leiden community detection (V0 implementation).
Package cluster — Leiden community detection (V0 implementation).
eval/retrieval
Package retrieval implements the LLM-free retrieval-accuracy measurement (EV1 Phase 2) — load YAML probe fixtures, dispatch each to the matching MCP tool through StoreReader, score result symbols against an expected set with recall / precision / F1.
Package retrieval implements the LLM-free retrieval-accuracy measurement (EV1 Phase 2) — load YAML probe fixtures, dispatch each to the matching MCP tool through StoreReader, score result symbols against an expected set with recall / precision / F1.
filterlist
Package filterlist implements the --files-from JSON include/exclude filter for ckg build.
Package filterlist implements the --files-from JSON include/exclude filter for ckg build.
link
Package link — http_match.go implements W2 of schema 1.9 (cross-language HTTP client → server matching).
Package link — http_match.go implements W2 of schema 1.9 (cross-language HTTP client → server matching).
mcp
Package mcp — bench.go exposes the registered tool handlers for in-process latency measurement.
Package mcp — bench.go exposes the registered tool handlers for in-process latency measurement.
parse/golang
Package golang is the Go-language parser for CKG.
Package golang is the Go-language parser for CKG.
parse/proto
Package proto implements the CKG parser for `.proto` files (schema 1.9 W3a).
Package proto implements the CKG parser for `.proto` files (schema 1.9 W3a).
parse/solidity
Package solidity implements the CKG parser for .sol files (spec §4.6.3).
Package solidity implements the CKG parser for .sol files (spec §4.6.3).
parse/solidity/binding
Package binding wraps the tree-sitter-solidity grammar (vendored from github.com/JoranHonig/tree-sitter-solidity v1.2.11, MIT-licensed) into a *sitter.Language usable by github.com/tree-sitter/go-tree-sitter.
Package binding wraps the tree-sitter-solidity grammar (vendored from github.com/JoranHonig/tree-sitter-solidity v1.2.11, MIT-licensed) into a *sitter.Language usable by github.com/tree-sitter/go-tree-sitter.
parse/typescript
Package typescript — body_walk.go implements P3 of the TS parser: statement-level call extraction inside function/method bodies.
Package typescript — body_walk.go implements P3 of the TS parser: statement-level call extraction inside function/method bodies.
persist
Package persist — node_attrs.go (W-C W11 V7, 2026-05-19) bridges the per-node JSON-blob `attrs` column to the type fields on types.Node that have no dedicated SQLite column.
Package persist — node_attrs.go (W-C W11 V7, 2026-05-19) bridges the per-node JSON-blob `attrs` column to the type fields on types.Node that have no dedicated SQLite column.
score
Package score — centrality.go provides Brandes betweenness centrality with source sampling, used by the GRAPH_REPORT generator to surface "bridge nodes": symbols that connect otherwise-distant parts of the graph and therefore drive cross-cutting concerns.
Package score — centrality.go provides Brandes betweenness centrality with source sampling, used by the GRAPH_REPORT generator to surface "bridge nodes": symbols that connect otherwise-distant parts of the graph and therefore drive cross-cutting concerns.
server
Package server implements the HTTP API and serves the embedded viewer.
Package server implements the HTTP API and serves the embedded viewer.
temporal
Package temporal extracts git-history derived facts (commits + per-file touch lists) used to emit CKS G6 Temporal edges (`changed_in`, `blame`) in the build pipeline.
Package temporal extracts git-history derived facts (commits + per-file touch lists) used to emit CKS G6 Temporal edges (`changed_in`, `blame`) in the build pipeline.
validate
Package validate provides post-build inspectors that audit a constructed graph for integrity issues.
Package validate provides post-build inspectors that audit a constructed graph for integrity issues.
pkg
bm25
Package bm25 provides Okapi BM25 ranking for code-aware corpora.
Package bm25 provides Okapi BM25 ranking for code-aware corpora.
concurrency
Package concurrency computes the concurrency blast radius of a symbol: every module that affects or is affected by it via the contract's five concurrency edge types (spawns, sends_to, recvs_from, acquires_lock, accessed_under_lock), BFS in both directions over a store.Reader.
Package concurrency computes the concurrency blast radius of a symbol: every module that affects or is affected by it via the contract's five concurrency edge types (spawns, sends_to, recvs_from, acquires_lock, accessed_under_lock), BFS in both directions over a store.Reader.
evidence
Package evidence — cache.go amortises the per-call corpus indexing across BuildPack invocations.
Package evidence — cache.go amortises the per-call corpus indexing across BuildPack invocations.
hunkmodifies
Package hunkmodifies computes the H2 line-overlap `modifies` edges that connect git Hunk nodes to the code nodes they touch.
Package hunkmodifies computes the H2 line-overlap `modifies` edges that connect git Hunk nodes to the code nodes they touch.
impact
Package impact is the shared "impact_of_change" implementation used by both internal/mcp.impact_of_change and internal/server's HTTP /api/impact handler.
Package impact is the shared "impact_of_change" implementation used by both internal/mcp.impact_of_change and internal/server's HTTP /api/impact handler.
mcphandlers
Package mcphandlers exposes ckg's MCP tool handlers as a public, reusable surface so sister repos (code-knowledge-system / cks, code-knowledge-vector / ckv) can wire their own mcp-go servers to the same eight-tool set without re-implementing the bodies.
Package mcphandlers exposes ckg's MCP tool handlers as a public, reusable surface so sister repos (code-knowledge-system / cks, code-knowledge-vector / ckv) can wire their own mcp-go servers to the same eight-tool set without re-implementing the bodies.
policy
Package policy loads governance/protocol policy metadata from a YAML file and converts it into NodePolicy + EdgeGovernedBy rows that fold into the main CKG graph.
Package policy loads governance/protocol policy metadata from a YAML file and converts it into NodePolicy + EdgeGovernedBy rows that fold into the main CKG graph.
security
Package security loads security risk pattern annotations from a YAML file and converts them into NodeSecurityPattern + EdgeHasSecurityPattern rows that fold into the main CKG graph.
Package security loads security risk pattern annotations from a YAML file and converts them into NodeSecurityPattern + EdgeHasSecurityPattern rows that fold into the main CKG graph.
smartctx
Package smartctx is the shared "smart 1-shot retrieval" implementation used by both internal/mcp.get_context_for_task and internal/eval's δ baseline.
Package smartctx is the shared "smart 1-shot retrieval" implementation used by both internal/mcp.get_context_for_task and internal/eval's δ baseline.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL