Skip to content

Commit 8cfac95

Browse files
authored
feat: add init command for AI agent adoption (#34)
## Why codebase-intelligence has the data (architecture, impact, risk metrics) — but AI coding agents default to grep/read instead of querying it. `init` closes that adoption gap. Native TypeScript, zero new runtime deps, no LLM. ## What `codebase-intelligence init [path]` writes an **idempotent, marked** instruction block ("query CI before grep/read") into each agent's repo file, and installs a portable skill: | Layer | Target | |---|---| | Repo instructions | `AGENTS.md`, `CLAUDE.md`, `.cursor/rules/codebase-intelligence.mdc`, `.github/copilot-instructions.md`, `GEMINI.md`, `CONVENTIONS.md` | | Portable skill | `~/.claude/skills/codebase-intelligence/SKILL.md` | | Registry | `skills/codebase-intelligence/SKILL.md` → `ags install` / `npx skills add` | Flags: `--agents <list>` (subset), `--no-skill`, `--json`. ## Safety / correctness - `upsertManagedBlock()` is pure + idempotent. Only content between `<!-- codebase-intelligence:start -->` / `:end` is ever touched — **existing user content is preserved**. - Single source of truth: one `renderBlock()`/`renderSkill()`; 6 agents are a thin path+preamble table. - Drift-guard test ties the committed `SKILL.md` to `renderSkill()`. ## Tests / gates - 18 new tests (real fs, temp dirs — no mocks): create / append / replace / idempotent / preserve-outside / subset / merge-without-clobber. - Full suite: **310 passed**. lint / typecheck / build all green. ## Docs README (Agent Adoption section + TOC + Features), `docs/cli-reference.md`, `docs/architecture.md`, `llms.txt`, `llms-full.txt`, and a new `CHANGELOG.md`. ## Follow-up (manual) skills.sh **directory listing** needs a logged-in web submit at agentskill.sh; the `SKILL.md` is spec-valid so `npx skills add github.com/bntvllnt/codebase-intelligence` works once merged.
1 parent 78a23b3 commit 8cfac95

13 files changed

Lines changed: 738 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Changelog
2+
3+
All notable changes to this project are documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
### Added
11+
12+
- **`init` command** — agent adoption layer. `codebase-intelligence init [path]` writes
13+
an idempotent, marked instruction block ("query CI before grep/read") into each
14+
agent's repo file (`AGENTS.md`, `CLAUDE.md`,
15+
`.cursor/rules/codebase-intelligence.mdc`, `.github/copilot-instructions.md`,
16+
`GEMINI.md`, `CONVENTIONS.md`) and installs a portable skill to
17+
`~/.claude/skills/codebase-intelligence/SKILL.md`.
18+
- `--agents <list>` to target a subset of agents (default: all).
19+
- `--no-skill` to skip the global skill install.
20+
- `--json` for machine-readable output.
21+
- Writes are idempotent — only content between the
22+
`codebase-intelligence:start`/`:end` markers is ever touched; existing user content
23+
is preserved.
24+
- **`src/install/` module** — managed-block upsert engine, per-agent target registry,
25+
and shared block/skill content (single source of truth).
26+
- **Registry skill**`skills/codebase-intelligence/SKILL.md`, installable via
27+
`ags install codebase-intelligence` or `npx skills add`.
28+
29+
### Changed
30+
31+
- Docs updated for the new command: `README.md`, `docs/cli-reference.md`,
32+
`docs/architecture.md`, `llms.txt`, `llms-full.txt`.
33+
34+
## [2.3.0] - 2026
35+
36+
Baseline for this changelog. For release history prior to and including 2.3.0, see the
37+
[git tags](https://github.com/bntvllnt/codebase-intelligence/tags) and commit history.
38+
39+
[Unreleased]: https://github.com/bntvllnt/codebase-intelligence/compare/v2.3.0...HEAD
40+
[2.3.0]: https://github.com/bntvllnt/codebase-intelligence/releases/tag/v2.3.0

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ src/
2020
process/index.ts <- Entry point detection + call chain tracing
2121
community/index.ts <- Louvain clustering
2222
persistence/index.ts <- Graph export/import to .code-visualizer/
23+
install/index.ts <- Agent adoption: managed-block engine + per-agent files + skill (init)
2324
cli.ts <- CLI entry point (commander)
2425
docs/
2526
architecture.md <- Pipeline, module map, data flow, design decisions

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ claude mcp add -s user -t stdio codebase-intelligence -- npx -y codebase-intelli
4242
- [Features](#features)
4343
- [Installation](#installation)
4444
- [CLI Usage](#cli-usage)
45+
- [Agent Adoption](#agent-adoption)
4546
- [MCP Integration (Secondary)](#mcp-integration-secondary)
4647
- [Metrics](#metrics)
4748
- [Architecture](#architecture)
@@ -54,14 +55,15 @@ claude mcp add -s user -t stdio codebase-intelligence -- npx -y codebase-intelli
5455

5556
## Features
5657

57-
- **15 CLI commands** for architecture analysis, dependency impact, dead code detection, and search
58+
- **16 CLI commands** for architecture analysis, dependency impact, dead code detection, search, and agent setup
5859
- **Machine-readable JSON output** (`--json`) for automation and CI pipelines
5960
- **Auto-cached index** in `.code-visualizer/` for fast repeat queries
6061
- **11 architectural metrics** — PageRank, betweenness, coupling, cohesion, tension, churn, complexity, blast radius, dead exports, test coverage, escape velocity
6162
- **Symbol-level analysis** — callers/callees, symbol importance, impact blast radius
6263
- **BM25 search** — ranked keyword search across files and symbols
6364
- **Process tracing** — detect entry points and execution flows through the call graph
6465
- **Community detection** — Louvain clustering for natural file groupings
66+
- **Agent adoption**`init` writes per-agent instruction files + installs a skill so AI agents query CI before grep/read
6567
- **MCP parity (secondary)** — same analysis available as 15 MCP tools, 2 prompts, and 3 resources
6668

6769
## Installation
@@ -104,6 +106,7 @@ codebase-intelligence <command> <path> [options]
104106
| `rename` | Reference discovery for rename planning |
105107
| `processes` | Entry-point execution flow tracing |
106108
| `clusters` | Community-detected file clusters |
109+
| `init` | Make AI agents use CI — writes per-agent instruction files + installs the skill |
107110

108111
### Useful flags
109112

@@ -116,6 +119,39 @@ codebase-intelligence <command> <path> [options]
116119

117120
For full command details, see [docs/cli-reference.md](docs/cli-reference.md).
118121

122+
## Agent Adoption
123+
124+
codebase-intelligence has the data — but AI agents only benefit if they actually
125+
*query* it instead of defaulting to grep/read. `init` closes that gap.
126+
127+
```bash
128+
codebase-intelligence init # current repo, all agents + skill
129+
codebase-intelligence init ./repo --agents claude,agents
130+
codebase-intelligence init --no-skill
131+
```
132+
133+
It writes an idempotent, marked instruction block ("query CI before grep/read") into
134+
each agent's native file, and installs a portable skill:
135+
136+
| Layer | Target |
137+
|---|---|
138+
| Repo instructions | `AGENTS.md`, `CLAUDE.md`, `.cursor/rules/codebase-intelligence.mdc`, `.github/copilot-instructions.md`, `GEMINI.md`, `CONVENTIONS.md` (Aider) |
139+
| Portable skill | `~/.claude/skills/codebase-intelligence/SKILL.md` |
140+
141+
Writes are idempotent — only content between the
142+
`<!-- codebase-intelligence:start -->` / `:end` markers is ever touched, so re-running
143+
is safe and your own edits are preserved.
144+
145+
### Install the skill directly
146+
147+
The skill is also published to the [skills.sh](https://www.skills.sh/) registry:
148+
149+
```bash
150+
ags install codebase-intelligence
151+
# or
152+
npx skills add github.com/bntvllnt/codebase-intelligence
153+
```
154+
119155
## MCP Integration (Secondary)
120156

121157
Running without a subcommand starts the MCP stdio server (backward compatible):
@@ -208,7 +244,8 @@ codebase-intelligence <command> <path>
208244

209245
## Release
210246

211-
Publishing is automated through GitHub Actions.
247+
Publishing is automated through GitHub Actions. See [CHANGELOG.md](CHANGELOG.md) for
248+
release notes.
212249

213250
### Normal CI (before release)
214251

docs/architecture.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ src/
4242
process/index.ts <- Entry point detection + call chain tracing
4343
community/index.ts <- Louvain clustering
4444
persistence/index.ts <- Graph export/import to .code-visualizer/
45+
install/index.ts <- Agent adoption: managed-block engine + per-agent file targets + skill
4546
server/graph-store.ts <- Global graph state (shared by CLI + MCP)
4647
cli.ts <- Entry point, CLI commands + MCP fallback
4748
```

docs/cli-reference.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CLI Reference
22

3-
15 commands for terminal and CI use. Full parity with MCP tools. All commands auto-cache the index to `.code-visualizer/`.
3+
16 commands for terminal and CI use. The 15 analysis commands have full parity with MCP tools and auto-cache the index to `.code-visualizer/`; `init` sets up agent adoption.
44

55
## Commands
66

@@ -156,6 +156,20 @@ codebase-intelligence clusters <path> [--min-files <n>] [--json] [--force]
156156

157157
**Output:** clusters with files, file count, cohesion.
158158

159+
### init
160+
161+
Make AI agents use codebase-intelligence: write a managed instruction block into each
162+
agent's repo file (`AGENTS.md`, `CLAUDE.md`, `.cursor/rules/codebase-intelligence.mdc`,
163+
`.github/copilot-instructions.md`, `GEMINI.md`, `CONVENTIONS.md`) and install the
164+
portable skill to `~/.claude/skills/`. Idempotent — only content between the
165+
`codebase-intelligence:start`/`:end` markers is touched.
166+
167+
```bash
168+
codebase-intelligence init [path] [--agents <list>] [--no-skill] [--json]
169+
```
170+
171+
**Output:** per-file actions (created / updated / unchanged) and skill install status.
172+
159173
## Flags
160174

161175
| Flag | Available On | Description |
@@ -173,6 +187,8 @@ codebase-intelligence clusters <path> [--min-files <n>] [--json] [--force]
173187
| `--entry <name>` | processes | Filter by entry point name |
174188
| `--min-files <n>` | clusters | Min files per cluster |
175189
| `--no-dry-run` | rename | Actually perform the rename (default: dry run) |
190+
| `--agents <list>` | init | Comma-separated agents (default: all) |
191+
| `--no-skill` | init | Skip installing the global Claude skill |
176192

177193
## Behavior
178194

llms-full.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ Community-detected file clusters. Input: `{ minFiles?: number }`. Returns: clust
283283

284284
# CLI Reference
285285

286-
15 commands — full parity with MCP tools.
286+
16 commands — 15 analysis commands (full parity with MCP tools) plus `init` for agent adoption.
287287

288288
## Commands
289289

@@ -377,6 +377,18 @@ codebase-intelligence clusters <path> [--min-files <n>] [--json] [--force]
377377
```
378378
Community-detected file clusters (Louvain algorithm).
379379

380+
### init
381+
```bash
382+
codebase-intelligence init [path] [--agents <list>] [--no-skill] [--json]
383+
```
384+
Make AI agents actually use codebase-intelligence. Writes an idempotent, marked
385+
instruction block ("query CI before grep/read") into each agent's repo file —
386+
`AGENTS.md`, `CLAUDE.md`, `.cursor/rules/codebase-intelligence.mdc`,
387+
`.github/copilot-instructions.md`, `GEMINI.md`, `CONVENTIONS.md` — and installs the
388+
portable skill to `~/.claude/skills/codebase-intelligence/SKILL.md`. Only content
389+
between the `codebase-intelligence:start`/`:end` markers is ever touched, so re-running
390+
is safe. `--agents` limits targets (default: all); `--no-skill` skips the skill.
391+
380392
## Global Behavior
381393

382394
- **Auto-caching**: First run parses and saves index to `.code-visualizer/`. Subsequent runs use cache if HEAD unchanged.

llms.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ MCP mode (AI agents):
1717
codebase-intelligence ./path/to/project
1818
```
1919

20-
CLI mode (humans/CI) — 15 commands, full MCP parity:
20+
CLI mode (humans/CI) — 16 commands (15 analysis with MCP parity + `init` for agent adoption):
2121
```bash
2222
codebase-intelligence overview ./src
2323
codebase-intelligence hotspots ./src --metric coupling
@@ -34,6 +34,7 @@ codebase-intelligence impact ./src getUserById
3434
codebase-intelligence rename ./src oldName newName
3535
codebase-intelligence processes ./src --entry main
3636
codebase-intelligence clusters ./src --min-files 3
37+
codebase-intelligence init . # make AI agents use CI
3738
```
3839

3940
## Optional
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
name: codebase-intelligence
3+
description: Query the codebase-intelligence CLI to understand TypeScript architecture, dependencies, blast radius, and risk before reading files. Use for any "how is this structured", "what breaks if I change X", "where is the complexity" question.
4+
---
5+
6+
# Codebase Intelligence
7+
8+
`codebase-intelligence` turns a TypeScript codebase into a queryable graph of
9+
architecture, dependencies, and risk metrics. **Prefer it over grep/read** when the
10+
task is about structure, impact, or risk — it is faster and more accurate than
11+
scanning files one by one.
12+
13+
## When to use
14+
15+
| Goal | Command |
16+
|------|---------|
17+
| First look / architecture | `codebase-intelligence overview <path>` |
18+
| Risk & complexity ranking | `codebase-intelligence hotspots <path>` |
19+
| Impact of changing a symbol | `codebase-intelligence impact <path> <symbol>` |
20+
| File-level blast radius | `codebase-intelligence dependents <path> <file>` |
21+
| Unused exports | `codebase-intelligence dead-exports <path>` |
22+
| Keyword search | `codebase-intelligence search <path> <query>` |
23+
| Rename planning | `codebase-intelligence rename <path> <old> <new>` |
24+
| Module structure | `codebase-intelligence modules <path>` |
25+
26+
## Rules
27+
28+
- Run `overview` first to orient, then drill down (hotspots → file/symbol → impact).
29+
- Always pass `--json` in automation/subagents for structured output.
30+
- Use `impact`/`dependents` BEFORE editing to gauge blast radius.
31+
- No global install? Prefix any command with `npx codebase-intelligence@latest`.
32+
- Full reference: `codebase-intelligence --help`.

specs/history.log

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
2026-03-11 | shipped | fix-error-handling | 1h→0.5h | 1d | Consistent impact_analysis error handling, LOC off-by-one fix, empty file guard. 17 regression tests.
55
2026-03-11 | shipped | fix-metrics-test-files | 2h→1.5h | 1d | Exclude test files from coverage/coupling metrics, isTestFile detection, coupling formula fix. 19 regression tests.
66
2026-03-11 | shipped | feat-metric-quality | 3h→2h | 1d | LEAF verdict for single-file modules, tension suppression for type hubs/entry points, file_context path normalization. 20 regression tests.
7+
2026-05-30 | shipped | feat-agent-adoption-init | 3h→1h | 1d | `init` command: idempotent managed-block instructions for 6 agents + portable skill + registry SKILL.md. 18 tests, docs + CHANGELOG. PR #34.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Spec: Agent Adoption — `init` command
2+
3+
## Problem
4+
5+
codebase-intelligence has the data (architecture, impact, risk metrics) but AI coding
6+
agents don't *use* it. They default to grep/read. Missing layer: durable, in-repo
7+
instructions + a portable skill that tell agents "query CI first."
8+
9+
This is the adoption/distribution layer — persistent agent instructions + an installable
10+
skill — implemented natively in TypeScript. No new runtime deps, no LLM, no Python.
11+
12+
## Goal
13+
14+
One command — `codebase-intelligence init [path]` — that:
15+
1. Writes a managed instruction block into per-agent repo files (6 agents).
16+
2. Installs a portable Claude skill (`~/.claude/skills/`).
17+
3. (Maintainer side) ship a registry-ready SKILL.md for skills.sh / `ags install`.
18+
19+
## Design
20+
21+
```
22+
init [path]
23+
├── installRepoFiles(repoRoot, targets) → Layer 1 (committed, team-wide)
24+
└── installGlobalSkill(homeDir) → Layer 2 (per-dev Claude skill)
25+
26+
renderBlock() ── single source of truth ──► upsertManagedBlock() ──► each target file
27+
renderSkill() ── single source of truth ──► SKILL.md (global + registry)
28+
```
29+
30+
### Managed-block engine (correctness core)
31+
32+
`upsertManagedBlock(existing, block, markers) -> string` — pure, fs-free.
33+
34+
- no markers in `existing` → append block (preserve original).
35+
- markers present → replace between (preserve content before & after).
36+
- empty `existing` → block only.
37+
- idempotent: f(f(x)) == f(x).
38+
39+
Markers (HTML comments, work in all markdown/.mdc):
40+
```
41+
<!-- codebase-intelligence:start (auto-generated — edits here are overwritten) -->
42+
<!-- codebase-intelligence:end -->
43+
```
44+
45+
### Agent target table (single source, adapters differ by path/preamble)
46+
47+
| id | repo file | preamble |
48+
|----|-----------|----------|
49+
| agents | `AGENTS.md` | — (cross-agent std; covers Codex) |
50+
| claude | `CLAUDE.md` ||
51+
| cursor | `.cursor/rules/codebase-intelligence.mdc` | mdc frontmatter |
52+
| copilot | `.github/copilot-instructions.md` ||
53+
| gemini | `GEMINI.md` ||
54+
| aider | `CONVENTIONS.md` ||
55+
56+
### Instruction block content
57+
58+
Discovery-first mandate ("use BEFORE grep/read for architecture/impact/risk") +
59+
command cheatsheet table + `--json` note + MCP pointer.
60+
61+
### Global skill / registry
62+
63+
- `installGlobalSkill``~/.claude/skills/codebase-intelligence/SKILL.md` (skill = Claude concept).
64+
- Registry: commit `skills/codebase-intelligence/SKILL.md` + README `ags install` / `npx skills add` docs.
65+
skills.sh directory submission = manual web/PR step (flagged, can't automate).
66+
67+
## State Machine
68+
69+
N/A — Stateless. Each `init` run is an idempotent upsert (input files → output files).
70+
No transitions; re-runnable to convergence.
71+
72+
## Test Plan (real fs, temp dirs — no mocks)
73+
74+
- upsertManagedBlock: empty / append / replace / idempotent / preserve-outside.
75+
- installRepoFiles: creates all targets, merges into pre-existing CLAUDE.md without clobber.
76+
- renderBlock/renderSkill: contain mandate keywords + command names.
77+
78+
## Out of Scope
79+
80+
- Auto-indexing on init (block tells agent to run overview).
81+
- Non-Claude global skill dirs (other agents covered by repo files + ags distribution).
82+
- Visualization / narrative report / multimodal outputs (separate, future work).

0 commit comments

Comments
 (0)