# pi-codebase-memory

Native [pi](https://pi.dev) tools wrapping the `codebase-memory-mcp` binary's `cli` subcommand. Replaces the MCP-server transport with first-class `cbm_*` pi tools - same engine and graph DB, only the transport changes.

## Requirements

- `codebase-memory-mcp` on `PATH`
- `git` (for worktree-aware project resolution)

## Install

```bash
pi install /path/to/codebase-memory      # local path
# or
pi install git:github.com/<user>/codebase-memory
```

## Tools

Eager: `cbm_list_projects`, `cbm_search_graph`, `cbm_search_code`, `cbm_get_code_snippet`, `cbm_index_repository`, `cbm_search_tools`.

Lazy (activated via `cbm_search_tools`): `cbm_query_graph`, `cbm_trace_path`, `cbm_detect_changes`, `cbm_manage_adr`, `cbm_ingest_traces`, `cbm_get_graph_schema`.

Commands: `/cbm:index-status`, `/cbm:index-repository`.

## Legacy generator compatibility

At session start, the extension deactivates the unprefixed tools produced by older `codebase-memory-mcp` Pi generators. Those generators used the obsolete `{ name, run }` registration shape and emitted tools without input schemas, causing Claude requests to fail with errors such as:

```text
tools.4.custom.input_schema: Field required
```

The supported `cbm_*` tools remain active. No aliases are registered.

## Worktree awareness

Indexes are keyed by the canonical checkout's absolute path. Inside a linked Git worktree:

- `resolveProject()` translates the worktree cwd to the canonical checkout path (dirname of `git rev-parse --git-common-dir`) before matching project roots.
- Every tool retries once with the cwd-resolved project when the binary answers "project not found or not indexed", so agents in worktrees don't conclude they must re-index.

## Layout

```
src/index.ts                   - extension entry (registers everything)
src/active-tools.ts            - lazy activation and legacy-tool quarantine
src/cli.ts                     - runCli() + resolveProject() (binary interface)
src/schemas.ts                 - TOOLS: tool defs + typed typebox schemas
src/commands.ts                - /cbm:index-status, /cbm:index-repository
src/tools/generic.ts           - passthrough cbm_* tools
src/tools/index-repository.ts  - bespoke renderer
src/tools/search-graph.ts      - bespoke found/not-found renderer
src/tools/get-code-snippet.ts  - bespoke retrieved/not-found renderer
src/tools/search-code.ts       - bespoke found/not-found renderer
src/tools/list-projects.ts     - bespoke count renderer
src/tools/search-tools.ts      - lazy-tool loader
```
