English | 简体中文
A local code-search extension for the pi coding agent: tree-sitter extracts symbols and call edges, SQLite stores the graph. Registers four tools - code_find / code_trace / code_impact / code_map - plus the /reindex and /code doctor commands. Zero models, zero network, all local computation.
Locating code in an unfamiliar repo usually means many rounds of grep plus file-by-file reading - lots of turns, lots of wasted context. codegraph indexes the whole repo's symbol table and call graph into SQLite once, then:
- Know (or guess) a symbol name -> one
code_findjumps straight to the definition line - Follow call chains ->
code_trace(callers/callees) replaces multiple grep rounds - Gauge a change's blast radius ->
code_impactlists affected files, symbols, and tests - Map an unfamiliar repo ->
code_maprenders a PageRank-ranked overview
Measured cold-index times: aider (151 files) 952ms, hono (355 files) 2.2s, cobra (36 files) 0.4s.
Requirements: Node >= 24 (extension loading and unit tests rely on native TS type stripping), pi installed. Native modules (tree-sitter family, better-sqlite3) need a local build toolchain.
pi install npm:@zzz210s/pi-codegraph
# or from git:
pi install git:github.com/Zzz210s/pi-codegraphManual install:
git clone https://github.com/Zzz210s/pi-codegraph.git ~/pi-codegraph
bash ~/pi-codegraph/setup.shRestart pi (or run /reload inside pi) to activate.
Inside a pi session:
code_find- find definitions by symbol name (exact/prefix/substring); returnsfile:line kind name signature, hub files ranked firstcode_trace- who calls X / what X calls (direction=callers/callees)code_impact- blast radius of a change: affected files, symbols, tests (target = file path or symbol name)code_map- repo overview: files ranked by PageRank centrality with key symbol signatures/reindex <repo-root>- build the index (cold indexing is fast, ~1s for 151 files)/code doctor- environment and index health check (dependencies, index state)
Run /reindex <repo-root> the first time you use it in a repo. The index lives in the repo-local .codegraph/ directory (recommended to add to the project's .gitignore).
Languages supported: Python / TypeScript / TSX / Go / Java.
No configuration needed. Known boundaries:
code_findon generic names (e.g.run) guarantees hub-first ranking, not semantic perfection- monkeypatched / attribute-style calls produce no call edges
- Go / Java directory-level imports do not resolve to files (callee_file may be null)
- no hot-path caching (queries stay <10ms at current scale, acceptable)
Single-responsibility modules, every file <= 200 lines, pure logic separated from side effects:
index.ts- extension entry: registers tools and commandsparse*.ts- per-language tree-sitter parsing (symbols/call extraction);parse.tsdispatchesindexer.ts/graph.ts/edges.ts- graph construction: parse output -> symbol table + call edgespagerank.ts- centrality ranking (hub-first for code_find / code_map)store.ts/store-io.ts- SQLite access, migration, and validationfind.ts/trace.ts/blast.ts/map.ts- core query logic for the four toolstool-*.ts- tool parameter schemas and output formattingdoctor.ts/command-*.ts- /code doctor and /reindex command implementations*.test.js- 19 test files (node --test, 80+ cases)
cd extensions/codegraph
npm install # includes dev dependencies
npm test # node --test, all casesbash setup.sh --test # run tests, then deploy to ~/.pi/agent/extensions/codegraph/After changes, /reload hot-reloads; re-run /reindex when the index is stale. If the index database is corrupted, the tools return a hint to delete .codegraph/ and /reindex.
MIT