Skip to content

Back to the catalog

trailhq/Graft
github.comrepo

trailhq/Graft

Graft (npm package @nanonets/graft) builds a local, regenerable code graph from a repo — markdown nodes summarizing subsystems plus a per-symbol tree-sitter wiring graph — and wires it into coding agents like Claude Code, Codex, Cursor and Gemini so they no longer re-explore the codebase from scratch every session. It ships an MCP server, CLI commands (ask/grep/callers/skeleton/map/blast/viz), and Claude Code-specific hooks (statusline, blast-radius warnings, auto-resync). The project reports its own benchmarks: a 162-run internal harness and a 50-instance SWE-bench Verified comparison where graft-augmented Claude Sonnet 5 resolved 33/50 instances vs. 27/50 for cold Claude Code, with fewer tool calls, tokens and wall-clock time.

TL;DR A tree-sitter-built, markdown code graph that gives Claude Code/Codex/Cursor persistent codebase context instead of re-exploring every session, benchmarked on SWE-bench Verified.
Document

What it claims

Graft prebuilds a codebase map so a coding agent does not re-explore the repo each session. The structural part needs no model. The prose part is written by an LLM using your own key. On a 50-instance SWE-bench Verified run with Claude Sonnet 5, the Graft arm resolved 33 instances against 27 for cold Claude Code. It also used 25% fewer tool calls, 23% fewer tokens and 32% less wall-clock time. All benchmarks on the page were run by the authors. The licence is MIT.

How it works

The build has two tiers, and the split decides whether you need an API key at all:

flowchart LR
    S[Source files] --> T[tree-sitter pass<br/>no key, no network]
    S --> D{--deep?}
    D -- yes, your API key --> L[LLM: summarize files,<br/>group into nodes]
    D -- no --> X[skip]
    T --> W[graft/.graph/wiring.json]
    L --> M[graft/*.md nodes]
  • Structural tier (graft build). Deterministic tree-sitter extraction of symbols and call edges.
  • Full fidelity, with cross-file resolution, covers TS/JS, Python, Go, Java, Kotlin, PHP, Swift and R.
  • Broad tier, with name-resolved edges only, covers Rust, C, C++, C#, Ruby, Scala, Elixir, Solidity, OCaml, Zig, Dart, Clojure, Nix and Lua.
  • --lsp adds compiler-grade edges if rust-analyzer, clangd, gopls, pyright or typescript-language-server is on PATH.
  • Deep tier (graft build --deep). Pass 1 summarizes each file. Pass 2 groups the summaries into a few dozen nodes with typed links. It also adds a per-symbol summary and "crux" excerpt.
  • Caching and freshness. Every pass is cached by content hash. Before answering, each query stats the working tree (about 3ms) and does a structural rebuild only if something changed. Uncommitted edits are included. Auto-sync never calls the LLM. On Graft's own 124-file repo, a cold build took 0.74s and a rebuild after one edit took 0.18s.

Evidence

  • Internal harness. 162 runs across two repos (Graft itself and a Node/Express auth service), 3 trials each. It compared three Sonnet 5 variants: cold, push (a graft ask bundle injected up front) and pull (MCP tools only). An Opus 4.8 judge scored the answers with a required-keyword floor. Push was never less correct than cold. Pull reached 98% correctness, 5 points above cold, but gave up most of the speed gain. The text does not give push's speed figures.
  • SWE-bench Verified. 50 instances, scored by the official swebench 4.1.0 grader with the same Docker images and turn limits on both arms. Token, cost and call figures are computed only over instances both arms resolved. The cold baseline's failures were incomplete multi-file fixes:
  • On django-11532 it patched 1 of 5 required files.
  • On django-16263 it patched 1 of 4.
  • PocketBase. 15 tasks run with Claude Opus, headless via claude -p. The page reports up to 4× cheaper and 3× faster. The cross-file auth question dropped from $2.19 to $0.84. All 5 merged PRs were reproduced, but "reproduced" means the diff touched the same files as the merged PR, not that tests passed.

How to use it

npm install -g @nanonets/graft   # install the CLI, once
graft init                       # build the graph + wire it into Claude Code
  • Preview first. graft init --dry-run lists every file it would touch.
  • Wire specific agents. Use --agents <ids> (agents, cursor, gemini, grok, copilot, kiro, windsurf, adal, claude).
  • CI and scripts. In a non-TTY context, init writes nothing unless you pass --agents or --yes.
  • Team setup. Commit the generated .claude/ wiring. Each teammate then runs graft build locally; graft/ is gitignored.
  • Provider for --deep. Set GRAFT_PROVIDER (openai, anthropic, litellm, orcarouter), GRAFT_API_KEY, GRAFT_MODEL and, for OpenAI-compatible endpoints, GRAFT_BASE_URL.
  • CI use. graft blast --base origin/main --format markdown produces a PR comment. graft check exits 1 when the graph has drifted from the code.
  • Removal. graft uninstall -y reverses init.

Limits

  • Benchmark scale and scoring. All benchmarks are vendor-run. The SWE-bench sample is 50 instances. The PR tasks are scored by which files were touched, not by correctness.
  • Unfinished feature. The crux is not yet inlined into the markdown nodes. It exists only in the per-symbol graph.
  • Language coverage. Files in unlisted languages are skipped. LSP edges are best-effort.
  • Machine-wide changes. If ~/.codex/ exists, init writes user-level Codex config that applies to every repo. --no-global prevents this.
  • Telemetry. Telemetry is on by default: a daily npm version check plus one batched usage ping. Disable it with graft telemetry disable or DO_NOT_TRACK=1.
  • Trail service. graft trail push sends repo history to the Trail service, and the first run signs you up through the browser. In repos with a Trail attached, the Claude Code hook also runs graft trail push in the background, at most once a day. Disable that with GRAFT_TRAIL_AUTOPUSH=0.
  • Repo naming. The development instructions clone NanoNets/context-graph-engine, not trailhq/Graft. Confirm which repo is canonical before building from source.

Organisations named here

  • Anthropic
  • OpenAI
  • OpenRouter
  • Fireworks
  • Groq
  • Nanonets
  • xAI
  • PocketBase
  • SWE-bench Verified

Technologies named here

  • Claude Sonnet 5
  • Graft
  • Opus 4.8
  • Claude Code
  • Claude Opus
  • Cursor
  • Codex
  • Gemini
  • MCP server
  • tree-sitter
  • LiteLLM
  • OpenRouter
  • Fireworks
  • Groq
  • OrcaRouter
  • Grok
  • AdaL
  • Kiro
  • Windsurf
  • GitHub Copilot
  • TRAIL
  • swebench harness
  • Model Context Protocol
  • Gemini CLI
  • Node.js
  • TypeScript
  • Rust
  • Git
  • GitHub
  • Go
  • Python

Techniques named here

  • Two-pass LLM graph construction
  • Content-hash caching
  • Structural auto-refresh / freshness check
  • Push context strategy
  • Pull context strategy
  • Tree-sitter structural code graph
  • LSP-resolved compiler-grade edges
  • Blast radius analysis
  • Crux stored as code, not line range