Metadata-Version: 2.4
Name: ctxloop
Version: 0.3.0
Summary: Token-saving layer on top of Graphify: symbol-level code slicing, context packs, a savings ledger with an honest baseline, hooks and MCP for Claude Code, Copilot and Codex.
License-Expression: Apache-2.0
Project-URL: Repository, https://github.com/naubadnithinkumar/ctxloop
Keywords: graphify,knowledge-graph,tokens,claude-code,copilot,codex,mcp,tree-sitter
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: graphifyy>=0.9
Provides-Extra: accurate
Requires-Dist: tiktoken>=0.7; extra == "accurate"
Provides-Extra: semantic
Requires-Dist: fastembed>=0.3; extra == "semantic"
Requires-Dist: numpy>=1.24; extra == "semantic"
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Dynamic: license-file

# CtxLoop (formerly GraphSnip)

CtxLoop builds and reuses repository context. The new distribution and CLI are `ctxloop`; legacy `graphsnip` commands, imports, MCP tools and project storage remain supported. The new distribution must be published before installing it from a registry.

<div align="center">

# graphsnip

**Give your coding agent the exact code it needs, not whole files, and prove the savings.**

A token-saving layer on top of [Graphify](https://github.com/Graphify-Labs/graphify) for **Claude Code**, **GitHub Copilot** and **Codex**.

![License](https://img.shields.io/badge/license-Apache--2.0-0f766e)
![Python](https://img.shields.io/badge/python-3.10%2B-3776ab)
![Clients](https://img.shields.io/badge/Claude%20Code%20%C2%B7%20Copilot%20%C2%B7%20Codex-supported-6366f1)
![Built on](https://img.shields.io/badge/built%20on-Graphify-14b8a6)

<br>

<img src="https://raw.githubusercontent.com/naubadnithin7/codegraf/main/docs/img/dashboard-overview.png" alt="graphsnip dashboard: tokens saved, reduction, per-call chart" width="900">

<sub>The dashboard <code>graphsnip report</code> writes. Real data from the Flask codebase.</sub>

</div>

---

## At a glance

| | Without a graph | Graphify alone | **Graphify + graphsnip** |
|---|---|---|---|
| What the agent receives | whole files from grep | a map, **no source code** | **the exact code**, ranked and budgeted |
| Tokens for 8 Flask questions | 366,565 | 13,008 (then it must read files) | **17,869** |
| Tokens vs grep + read | baseline | −96% (map only) | **−95%, with code** |
| Questions answered with source | 8 / 8 | symbol named, no code | **8 / 8** |

<div align="center">
<img src="https://raw.githubusercontent.com/naubadnithin7/codegraf/main/docs/img/benchmark.svg" alt="Tokens per approach on Flask and Express" width="860">
</div>

Graphify is excellent at telling an agent *where* things are. graphsnip adds the step after: *hand over just that code*, stop re-sending code the agent already has, and measure what it saved. Baseline here is deliberately realistic: **grep the question's terms and read the top 8 matching files in full**. It is far less than "read the whole repo".

## Try it

**Interactive explorer.** Pick a layer, move the sliders (questions per day, price per million tokens, how many questions go to a cheap model) and watch the monthly numbers change. Starts from the measured Flask and Express results.

- Open [`docs/explorer.html`](docs/explorer.html) in a browser (download it, it is one self-contained file), or
- publish it for your team with GitHub Pages: *Settings → Pages → Deploy from branch → `main` / `docs`*, then visit `https://naubadnithin7.github.io/codegraf/explorer.html`.

<div align="center">
<img src="https://raw.githubusercontent.com/naubadnithin7/codegraf/main/docs/img/explorer.png" alt="Interactive explorer with layer toggles and sliders" width="760">
</div>

## Quick start

```bash
pipx install graphsnip            # or: pipx install git+https://github.com/naubadnithin7/codegraf
cd your-repo
graphsnip init                    # build graph, wire up Claude Code / Copilot / Codex, write the dashboard
graphsnip report --open           # savings dashboard (grows as agents use it)
```

No LLM key needed. Everything runs locally: Graphify parses with tree-sitter, graphsnip slices with tree-sitter.

Want a one-command demo on any repo? `graphsnip demo --open` builds the graph, runs a benchmark and opens the dashboard.

## Memory Engine: learn from previous coding tasks (optional)

GraphSnip integrates with the independent, open-source [Memory Engine](https://github.com/naubadnithinkumar/memory-engine). The graph describes the current code; memory captures evidence-backed lessons from previous work; approved skills capture reusable procedures. Memory is local, project-scoped and opt-in. No model API key is needed for this walkthrough.

```mermaid
flowchart LR
    A[Previous coding task] --> B[Candidate memory]
    B --> C[Independent test and review]
    C --> D[Verified memory]
    D --> E[Corroborated skill proposal]
    E --> F[Independent approval]
    G[Current code graph] --> H[gs_pack]
    D --> H
    F --> H
    H --> I[Budgeted agent context]
    H --> J[Memory usage dashboard]
```

### 1. Install the integration

Use Python 3.10+ and Node.js 22.13+. The following POSIX shell commands install the current GraphSnip source and the exact Memory Engine revision used by the interoperability CI. The new integration is not assumed to be available in an older registry release.

```bash
pipx install 'git+https://github.com/naubadnithinkumar/graphsnip.git'
npm install -g https://github.com/naubadnithinkumar/memory-engine/archive/befb5607a7109d8bcc8160a1947878de09a3d807.tar.gz
command -v graphsnip
command -v memory-engine
```

If GraphSnip is already installed via pipx, use a separate development environment or explicitly replace that installation. The Memory Engine executable must also be on the PATH of your IDE/MCP server, not just your interactive shell.

### 2. Create a disposable demonstration project

```bash
DEMO=$(mktemp -d)
cd "$DEMO"
cat > validators.py <<'PY'
def validate_email(email):
    return bool(email and "@" in email)

def persist_email(email, records):
    if not validate_email(email):
        raise ValueError("Invalid email")
    records.append(email)
PY
cat > test_demo.py <<'PY'
import unittest
from validators import persist_email

class EmailTests(unittest.TestCase):
    def test_email(self):
        records = []
        persist_email("demo@example.com", records)
        self.assertEqual(records, ["demo@example.com"])

    def test_missing_at(self):
        records = []
        with self.assertRaises(ValueError):
            persist_email("invalid", records)
        self.assertEqual(records, [])

    def test_empty(self):
        records = []
        with self.assertRaises(ValueError):
            persist_email("", records)
        self.assertEqual(records, [])
PY
```

This tiny fixture demonstrates the workflow, not a production-grade email validator. Stop if any test fails; the verification and approval commands below assume you have reviewed the corresponding passing tests.

### 3. Record and verify a learning

The CLI accepts one version-1 JSON request on stdin and returns a JSON envelope containing `ok` and `result`. Each invocation opens the database for the current working directory.

```bash
mem() { printf '%s\n' "$1" | memory-engine; }

FIRST=$(mem '{"version":1,"operation":"record","params":{"value":{"type":"corrective","title":"Validate email","learning":"Validate email before persisting.","evidence":["test:email"]}}}' | python3 -c 'import json,sys; print(json.load(sys.stdin)["result"]["id"])')

mem '{"version":1,"operation":"retrieve","params":{"query":"validate email","context":"project","adapter":"coding"}}'
```

Expected: an empty result array, because a newly recorded memory is a candidate. Run the demonstration test and review its result before verifying:

```bash
python3 -m unittest test_demo.EmailTests.test_email
mem "{\"version\":1,\"operation\":\"verify\",\"params\":{\"id\":\"$FIRST\",\"evidence\":{\"adapter\":\"coding\",\"test\":\"email\",\"evidenceId\":\"test:email\",\"context\":\"project\",\"result\":\"passed\",\"reviewed\":true}}}"
mem '{"version":1,"operation":"retrieve","params":{"query":"validate email","context":"project","adapter":"coding"}}'
mem '{"version":1,"operation":"stats"}'
```

Expected: the verified record is now returned, including its ID and learning. A different context returns no match. The evidence receipt is supplied by the reviewer; Memory Engine does not run or independently authenticate the test.

### 4. Turn corroborated memories into an approved skill

Run and review a second test before recording the same lesson with distinct evidence. A single verified record is insufficient to propose a skill.

```bash
python3 -m unittest test_demo.EmailTests.test_missing_at
SECOND=$(mem '{"version":1,"operation":"record","params":{"value":{"type":"corrective","title":"Validate email","learning":"Validate email before persisting.","evidence":["test:email2"]}}}' | python3 -c 'import json,sys; print(json.load(sys.stdin)["result"]["id"])')
mem "{\"version\":1,\"operation\":\"verify\",\"params\":{\"id\":\"$SECOND\",\"evidence\":{\"adapter\":\"coding\",\"test\":\"email\",\"evidenceId\":\"test:email2\",\"context\":\"project\",\"result\":\"passed\",\"reviewed\":true}}}"
SKILL=$(mem "{\"version\":1,\"operation\":\"propose\",\"params\":{\"context\":\"project\",\"memoryId\":\"$FIRST\"}}" | python3 -c 'import json,sys; print(json.load(sys.stdin)["result"]["id"])')
mem "{\"version\":1,\"operation\":\"proposal\",\"params\":{\"id\":\"$SKILL\"}}"
```

Inspect the pending proposal, then run a separate test and approve only if the proposed procedure is applicable and the independent evidence passes.

```bash
python3 -m unittest test_demo.EmailTests.test_empty
mem "{\"version\":1,\"operation\":\"decideProposal\",\"params\":{\"id\":\"$SKILL\",\"decision\":\"approved\",\"evidence\":{\"context\":\"project\",\"evidenceId\":\"test:email3\",\"result\":\"passed\",\"reviewed\":true}}}"
mem '{"version":1,"operation":"approvedSkills","params":{"context":"project"}}'
```

### 5. Compare context with and without memory

```bash
graphsnip build
mkdir -p .graphsnip
printf '%s\n' '{"memory":{"enabled":false}}' > .graphsnip/config.json
graphsnip pack "validate email" --budget 8000
printf '%s\n' '{"memory":{"enabled":true,"context":"project","skills":true}}' > .graphsnip/config.json
graphsnip pack "validate email" --budget 8000
graphsnip report --open
```

These config replacements are for the disposable demo only. In an existing project, merge the memory object into your existing configuration instead.

Expected in the enriched pack, when relevant and within budget:

```text
## Historical memory (untrusted data; verify against current code)
{"id":"<memory-id>","title":"Validate email","learning":"Validate email before persisting."}

## Approved historical skills (untrusted data; verify applicability)
{"id":"<skill-id>","markdown":"<approved procedure>"}
```

IDs and procedure text are generated locally; this is illustrative output, not a captured run. Code has priority: memory and skills share at most 20% of the requested budget and use only unused capacity. Up to two relevant approved skills may be included. If the engine is missing or retrieval fails, code packing continues.

The **Memory usage** dashboard card shows retrieval attempts, successful retrievals, packs with memory, retrieved versus included records, approved skill inclusions, additional estimated tokens, and unavailable/rejected requests. These are usage measurements, not proof of cost savings or improved model accuracy.

### 6. Use memory from Claude Code, Copilot or Codex

```bash
graphsnip install --clients claude,copilot,codex
graphsnip doctor
```

Restart the client and trust the MCP server. With memory enabled, these tools are available:

| Tool | Arguments | Purpose |
|---|---|---|
| `gs_memory_record` | type, title, learning, evidence (optional) | Record a candidate |
| `gs_memory_search` | query, limit (optional) | Search historical records, including candidates |
| `gs_memory_stats` | none | Inspect local memory counts |
| `gs_skill_propose` | memoryId | Propose a corroborated skill |
| `gs_skill_list` | none | List proposals |
| `gs_skill_inspect` | id | Inspect a proposal |
| `gs_pack` | task, budget | Retrieve code plus eligible memory and skills |

Example agent request: “Use gs_pack to locate email validation. After the change and tests, record any reusable correction as a candidate with gs_memory_record. Do not mark your own work verified.”

Verification and approval are intentionally not exposed as GraphSnip MCP tools. Automatic learning from every execution is not yet an end-to-end feature.

### 7. Invalidate stale learning and check isolation

```bash
mem "{\"version\":1,\"operation\":\"archive\",\"params\":{\"id\":\"$FIRST\"}}"
mem '{"version":1,"operation":"approvedSkills","params":{"context":"project"}}'
(cd "$(mktemp -d)" && mem "{\"version\":1,\"operation\":\"get\",\"params\":{\"id\":\"$FIRST\"}}")
```

Expected: the skill is no longer eligible because one of its verified sources was archived; the other project returns null for the original record. The second verified memory may still be retrieved in the original project.

Storage is under `.memory-engine/memory/memory.db` in the project root. Keep that directory out of version control and do not store credentials or confidential customer data. Local retrieval does not imply that downstream agent providers keep the retrieved text local: context supplied to an external model may leave your machine.

### 8. Run the automated interoperability tests

From a GraphSnip source checkout, with the pinned Memory Engine installed:

```bash
python3 -m venv .venv
source .venv/bin/activate
python -m pip install '.[dev]'
GRAPHSNIP_TEST_MEMORY_ENGINE=1 python -m pytest -v \
  tests/test_memory_cross_runtime.py tests/test_memory_client.py \
  tests/test_memory_integration.py tests/test_memory_eval.py
```

See [Memory evaluation](docs/memory-evaluation.md) for the four-arm comparison (baseline, graph, memory, combined) and the offline analyzer:

```bash
python -m graphsnip.memory_eval results.jsonl
```

The analyzer requires actual externally collected trial results; it does not execute model trials. The existing two-arm `graphsnip ab` is not a four-arm memory benchmark. No measured memory-specific effectiveness claim is made here.


## What you get

| | |
|---|---|
| **Verified memory and approved skills (optional)** | Reuse evidence-backed lessons in context packs with the standalone Memory Engine. |
| **Exact symbol, not the file** | `gs_symbol` returns one function or class with its callers and callees. Docstrings trimmed, big classes shown as skeletons. |
| **Context packs** | `gs_pack` bundles the ranked code for a whole task under a token budget in one call. |
| **No repeats** | Code the agent already has in this session comes back as a one-line pointer. |
| **Slash commands** | `/graphsnip <question>` in Claude Code and Copilot Chat, `/prompts:graphsnip` in Codex. |
| **Model routing** | Light questions go to a cheaper model, hard ones are handed to a stronger one. Counted only when it really happened. |
| **Hooks** | Claude Code is nudged to use the graph instead of broad `Read` / `grep`. Fail-open, off-switch included. |
| **Shell compression** | `graphsnip run -- pytest` squeezes noisy output and keeps the failure lines. Savings are exact. |
| **Dashboard** | Savings per call, efficiency vs accuracy, routing log, benchmark, and Graphify's interactive graph. |
| **Safe installs** | Idempotent and reversible: `graphsnip install` / `graphsnip uninstall`. Your files are never overwritten. |

## How it fits together

```mermaid
flowchart TB
    R[your repo] --> G["Graphify<br/>tree-sitter AST → graph.json<br/>graph.html · communities"]
    G --> S
    subgraph S["graphsnip"]
        direction LR
        A["symbol slicer · context packs<br/>session dedupe · budgets"]
        B["router · hooks · shell compression"]
        C["savings ledger · dashboard"]
    end
    S --> M["MCP server<br/>gs_pack · gs_find · gs_symbol<br/>gs_impact · gs_outline · gs_route"]
    M --> CC[Claude Code]
    M --> CP[Copilot]
    M --> CX[Codex]
    S --> D[".graphsnip/dashboard.html"]
```

## See it working

<table>
<tr>
<td width="50%">

**Every call, grey vs teal.** Each pair is one tool call: what grep + read would have cost against what graphsnip returned (log scale), with the multiplier on top.

<img src="https://raw.githubusercontent.com/naubadnithin7/codegraf/main/docs/img/chart-per-call.png" alt="Per-call tokens, log scale">

</td>
<td width="50%">

**Same answers, a fraction of the tokens.** On these questions every approach found the symbols, so the difference is what it cost to get there.

<img src="https://raw.githubusercontent.com/naubadnithin7/codegraf/main/docs/img/chart-efficiency-accuracy.png" alt="Efficiency vs accuracy">

</td>
</tr>
</table>

**Graphify's own graph** is embedded in the dashboard (and works offline). Every node is a symbol, colours are detected communities.

<div align="center">
<img src="https://raw.githubusercontent.com/naubadnithin7/codegraf/main/docs/img/graphify-graph.png" alt="Graphify knowledge graph of Flask" width="860">
</div>

## Slash commands

`graphsnip install` writes these for you:

| Client | Command | File |
|---|---|---|
| Claude Code | `/graphsnip <question>` and `/graphsnip-deep` | `.claude/commands/` |
| Copilot (VS Code, Local chat) | `/graphsnip <question>` and `/graphsnip-deep` | `.github/prompts/*.prompt.md` |
| Codex | `/prompts:graphsnip <question>` | `~/.codex/prompts/graphsnip.md` (Codex only reads prompts from your home folder; restart Codex) |

```text
/graphsnip how does request routing work
/graphsnip impact Flask.dispatch_request
/graphsnip savings
```

## Model routing

graphsnip cannot switch an agent's model by itself (an MCP server has no such power). What it does is decide, steer, and account for it honestly.

```mermaid
flowchart LR
    T["/graphsnip task"] --> R{"gs_route<br/>wording + graph size"}
    R -- light --> L["cheap model answers<br/>(Haiku)"]
    R -- "too hard" --> E["prints: Escalating to Sonnet / Opus…"]
    E --> H["stronger subagent finishes the job"]
    L --> A[answer]
    H --> A
```

| Client | How the model is switched | Verified by |
|---|---|---|
| **Claude Code** | `/graphsnip` is pinned to the light model; hard tasks hand off to a Sonnet / Opus subagent automatically | session transcripts (`graphsnip usage`) |
| **Codex** | `graphsnip codex "task"` picks the tier and runs `codex exec -m <model>`: a real switch graphsnip performs | the launcher knows the model it passed |
| **Copilot** | `/graphsnip` pinned only if you set `routing.models.copilot.light`; hard tasks need `/graphsnip-deep` | not available |

- **Statuses.** `routed` (running tier matched the decision), `escalate`, `session` (ran on your own model on purpose), `overpowered`, `advisory` (nobody verified which model ran). Dollars are credited **only for `routed`**.
- **Routing saves dollars per token, not tokens.** The token savings above come from the graph alone, and the dashboard keeps the two apart.
- **Prices are editable placeholders** under `routing.prices` in `.graphsnip/config.json`. Put your real ones in before showing a dollar figure. Codex model names go under `routing.models.codex`.

<details>
<summary><b>Codex setup</b></summary>

```json
// .graphsnip/config.json
{ "routing": { "models": { "codex": { "light": "<your light model>", "standard": "<your main model>", "heavy": "<your strongest model>" } } } }
```
```bash
graphsnip codex "where is the auth middleware defined"      # routes, then runs codex exec -m <model>
graphsnip codex --dry-run "refactor session handling"      # print the command only
```
`graphsnip install` also writes matching `graphsnip-light|standard|heavy` profiles into the Codex config.
</details>

## Semantic search (optional)

Keyword search finds code that shares words with your question. A vague question such as "who is allowed to login" may share none, so
graphsnip can also match by meaning. Install the extra and build the index once:

```bash
pip install 'graphsnip[semantic]'
graphsnip embed          # one vector per function/class, stored in .graphsnip/semantic/
graphsnip eval           # keyword-only vs hybrid on your own questions
```

- **What is embedded:** each graph symbol's name, file, the names it calls and is called by, and the start of its body. The vectors live in
  three small derived files (`vectors.npy`, `ids.json`, `meta.json`) next to the ledger. Graphify's `graph.json` is not touched.
- **Local only:** a small ONNX model (`BAAI/bge-small-en-v1.5` by default) runs on your machine. Nothing is sent anywhere. No vector database is needed.
- **Hybrid ranking:** keyword and semantic hits are merged by reciprocal rank fusion, then expanded through the graph (callers, callees) and
  packed within the token budget, as before. `gs_find` and `gs_pack` get better; no new tool is added, so there is no extra tool overhead.
- **Stays current:** after a graph refresh only new or changed symbols are re-embedded, and deleted ones are dropped.
- **Falls back:** without the extra or an index, behaviour is exactly the keyword search you had before.

**Proof, not promise.** `graphsnip eval` asks the same questions with and without the index and shows what each found (see
[`docs/semantic-tasks.example.json`](docs/semantic-tasks.example.json) for the format). If hybrid does not beat keyword on your repo,
the dashboard says so. Whether better retrieval also lowers your bill is a separate question that `graphsnip ab` answers.

### Safer graph refresh

The graph now refreshes under a cross-process lock (two editors on one repo no longer rebuild at once). The MCP server refreshes in the
background and answers from the previous snapshot meanwhile. Changes are found by content hash, so a `git checkout` that touches files
without changing them does nothing, and deleted files are noticed. `graphsnip status` lists what changed.

## Measured A/B: real tokens and real cost

The dashboard's savings are estimates against a hypothetical baseline. To get **measured** numbers, run the same questions through headless Claude Code with and without graphsnip:

```bash
graphsnip ab --dry-run              # preview: how many sessions, the exact command
graphsnip ab --yes --repeats 2      # run it (spends real usage; capped at $5 total by default)
graphsnip report --open             # "Measured A/B (real runs)" card + headline KPI
```

- Two throwaway copies of your repo: one with graphsnip uninstalled, one with it installed. Your repo is never modified.
- Both arms are read-only, use the same model and prompt, and start a fresh session per run, in shuffled order.
- Cost and tokens are what `claude -p --output-format json` reports (`total_cost_usd`, input, cache and output tokens), so caching and every turn are included.
- Results are medians over repeats; model runs are noisy, so use `--repeats 3` or more for a number you will quote.
- "Found" only checks that the expected symbols appear. The full answers are saved under `.graphsnip/ab/` so you can judge quality yourself.
- Needs the `claude` CLI on your machine. Codex A/B is not implemented yet.

## Commands

<details>
<summary><b>All commands</b></summary>

| Command | What it does |
|---|---|
| `graphsnip init` | build graph, install integrations, benchmark, write dashboard (`--dry-run` previews, writes nothing) |
| `graphsnip demo [--open] [--bundle DIR]` | graph + benchmark + dashboard in one go; `--bundle` makes a shareable folder |
| `graphsnip build [--force]` | build or refresh the Graphify graph (code only, no LLM) |
| `graphsnip embed [--force] [--status]` | build or refresh the optional semantic index (only changed symbols are re-embedded) |
| `graphsnip eval [--tasks FILE]` | keyword-only vs hybrid retrieval on your own questions; feeds the dashboard |
| `graphsnip pack "task"` | token-budgeted bundle of the code relevant to a task |
| `graphsnip find "question"` | budgeted map: where things live, how they connect |
| `graphsnip symbol Name` | one function or class: exact lines, callers, callees |
| `graphsnip impact Name` | what depends on a symbol |
| `graphsnip outline path` | symbols in a file with line numbers |
| `graphsnip route "task"` | which model tier does this need? (logs the decision) |
| `graphsnip codex "task"` | route, then run `codex exec -m <model for the tier>` |
| `graphsnip run -- <cmd>` | run a command, compress its output, keep the exit code |
| `graphsnip stats` / `report` | savings in the terminal / the HTML dashboard |
| `graphsnip usage` | which models actually answered (from Claude Code transcripts) |
| `graphsnip bench` | offline benchmark: grep+read vs Graphify vs graphsnip |
| `graphsnip ab` | **measured** A/B in headless Claude Code: real tokens and cost, with vs without graphsnip |
| `graphsnip install` / `uninstall` | per-client config (`--clients claude,copilot,codex`) |
| `graphsnip doctor` | check Python, Graphify, tree-sitter, graph freshness, client configs |

Every query command prints `~N tokens returned vs ~M for grep+read → saved P%` to stderr.
</details>

<details>
<summary><b>MCP tools</b></summary>

| Tool | Use it for |
|---|---|
| `gs_pack(task, budget)` | first call for "how does X work" or before a change |
| `gs_find(question, budget)` | locate things (map only) |
| `gs_symbol(name, max_lines, docs)` | read one definition instead of the file |
| `gs_impact(name, depth)` | blast radius before changing a signature |
| `gs_outline(path)` | symbols of a big file before opening it |
| `gs_route(task, running_tier)` | which model tier this task needs |
| `gs_savings()` | tokens saved so far |
| `gs_memory_*` / `gs_skill_*` | optional memory and skill tools; see the [Memory Engine walkthrough](#memory-engine-learn-from-previous-coding-tasks-optional) |

A small dependency-free stdio JSON-RPC server (it does not depend on the MCP SDK's changing API). It refreshes a stale graph and records every call in `.graphsnip/ledger.jsonl`.
</details>

<details>
<summary><b>What <code>install</code> writes</b></summary>

Idempotent (run it twice, nothing changes) and reversible (`graphsnip uninstall`). Existing content is preserved: text files get a marked block, JSON is merged.

| Client | Files |
|---|---|
| Claude Code | `.mcp.json`, `.claude/settings.json` (PreToolUse hook), `CLAUDE.md` block, `.claude/commands/graphsnip*.md`, `.claude/agents/graphsnip-{explorer,standard,heavy}.md` |
| Copilot (VS Code) | `.vscode/mcp.json`, `.github/copilot-instructions.md` block, `.github/prompts/graphsnip*.prompt.md` |
| Codex | `.codex/config.toml` (or `~/.codex/config.toml` with `--codex-scope user`), `AGENTS.md` block, `~/.codex/prompts/graphsnip.md` |

Flags: `--allow-tools` pre-approves the read-only tools in Claude Code, `--portable` writes bare `graphsnip` instead of this machine's absolute path (use it before committing configs), `--strict` sets hook mode to strict.
</details>

<details>
<summary><b>Hooks (Claude Code)</b></summary>

Fail-open: any error means "do nothing". Configure in `.graphsnip/config.json` (or `GRAPHSNIP_MODE`).

| Mode | Behaviour |
|---|---|
| `nudge` (default) | adds context: the file's outline for big reads, `gs_symbol("X")` when a grep pattern is a known symbol. Blocks nothing |
| `strict` | denies those same calls with a reason that tells the agent what to call. Literal-string greps and reads with `offset`/`limit` are never blocked |
| `off` | nothing |

`shell_filter: true` additionally rewrites noisy test/build commands to run through `graphsnip run`. It is **off by default** because the rewrite returns an `allow` decision, which skips the permission prompt; it is limited to an allow-list and never touches pipelines or chained commands. POSIX shells only.
</details>

## How the numbers are measured (and what they are not)

- **Baseline per call.** `find` / `pack`: grep the question's terms, read the top 8 matching files in full. `symbol`: read the whole file. `impact`: grep the name, read matching files (cap 10).
- **Counted once.** In a real session, tokens that stay in context are re-sent every turn, so true savings compound. The dashboard does not claim that (the explorer has an optional slider to show it).
- **Quality is checked, not assumed.** A task counts as answered only if the expected symbol appears with its source. That measures whether the right code was delivered, not whether a model's final answer was right.
- **Shell savings are exact** (raw vs compressed output). Token counts elsewhere are `chars / 4` estimates; install `graphsnip[accurate]` for tiktoken.
- **Your repo will differ.** Savings are larger where files are large and lookups are broad. Replace the auto-generated tasks in `.graphsnip/tasks.json` with questions you really ask, then `graphsnip bench`.
- **Dollars** on the main dashboard are `tokens × price per million input tokens`, ignoring output tokens and caching: a list-price estimate. For measured dollars use `graphsnip ab`.

## Known limits

- The hooks and the three client configs follow each tool's documented formats; the MCP server was verified against the official MCP Python client. Run `graphsnip doctor` and a short session in each client before relying on them.
- Symbol slicing is exact for the ~25 languages with tree-sitter grammars installed; elsewhere it falls back to indentation or brace matching.
- The graph only covers what Graphify extracts from code. Docs, PDFs and images need Graphify's semantic pass (needs an LLM key) and are not part of graphsnip's tools.
- Graphify's `graph.html` loads vis-network from a CDN; graphsnip caches it locally so the dashboard works offline (needs internet once).
- Model routing: Copilot cannot hand off to another model, Codex prompt files cannot pin one (use `graphsnip codex`), and the "measured" model mix works only for Claude Code.

## Development

```bash
pip install -e ".[dev]"
pytest -q
```

Publishing: `python -m build && twine upload dist/*` (PyPI), `cd npm && npm publish` (thin npm wrapper that needs Python 3.10+ and pipx or uv).

## License

Apache-2.0. graphsnip is a separate project that calls Graphify (also Apache-2.0) as a library; see `NOTICE`.
