codebrief 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Michael Abrt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,244 @@
1
+ # codebrief
2
+
3
+ Bootstrap optimized AI context files for any project. Auto-detects your tech stack, generates code snapshots, and produces config for Claude Code, Cursor, Copilot, Windsurf, Cline, Continue, Aider, and more.
4
+
5
+ ![codebrief demo](demo.gif)
6
+
7
+ ## Quick Start
8
+
9
+ ```bash
10
+ npx codebrief
11
+ ```
12
+
13
+ Run in your project root. It will:
14
+
15
+ 1. **Auto-detect** your tech stack (language, framework, package manager, linter)
16
+ 2. **Ask** a few questions (which AI tool, project purpose, key patterns)
17
+ 3. **Scan** source files for a code snapshot (types, store shapes, component props)
18
+ 4. **Generate** optimized context files for your chosen tool
19
+ 5. **Show** a summary with token savings estimate
20
+
21
+ On first run, your answers are saved to `.codebrief.json` so future runs skip the prompts entirely.
22
+
23
+ ## What It Does
24
+
25
+ codebrief builds a comprehensive understanding of your codebase and distills it into context files that AI agents can load instantly — eliminating the expensive exploration phase where agents read dozens of files just to understand the architecture.
26
+
27
+ The generated context includes:
28
+
29
+ - **Tech stack summary** with detected frameworks, versions, and usage counts
30
+ - **Code snapshot** of all public types, interfaces, function signatures, and component props
31
+ - **Key files** ranked by centrality (PageRank) with stability warnings
32
+ - **Architecture layers** showing dependency flow
33
+ - **Change coupling** — file pairs that frequently change together
34
+ - **Circular dependencies** detected via Tarjan's SCC algorithm
35
+ - **Module clusters** — automatically detected groups of related files
36
+ - **Git activity** — recently active files and churn patterns
37
+ - **Framework conventions** — best practices for your specific stack
38
+
39
+ ## How It Works
40
+
41
+ codebrief runs a pipeline of static analysis on your codebase. Here's what each step does and why it matters.
42
+
43
+ ### Mapping dependencies
44
+
45
+ **Problem:** AI agents don't know which files depend on each other, so they waste time reading unrelated code.
46
+
47
+ codebrief parses all `import`/`require`/`use` statements across your source files and builds a dependency graph. This graph powers most of the analysis below — it's how codebrief understands the structure of your project without running it.
48
+
49
+ ### Ranking files by importance (PageRank)
50
+
51
+ **Problem:** In a project with hundreds of files, which ones should an agent read first?
52
+
53
+ codebrief runs the same algorithm Google uses to rank web pages — but on your import graph instead of the internet. Files that are imported by many important files get a high score. For example, your main `types.ts` or a shared `api-client.ts` would rank near the top because they sit at the center of your dependency tree. These high-centrality files are surfaced first so agents understand your architecture before diving into details.
54
+
55
+ ### Removing dead exports
56
+
57
+ **Problem:** Source files often contain exported functions, types, or constants that nothing actually imports — leftover refactors, unused utilities, or over-exported modules. Including these in context wastes tokens.
58
+
59
+ codebrief cross-references every named export against the import graph. If no file in the project imports it, it's excluded from the code snapshot.
60
+
61
+ ### Fitting context into a token budget
62
+
63
+ **Problem:** A full code snapshot of every type, interface, and function signature might exceed the token budget — especially in large projects.
64
+
65
+ codebrief uses a greedy knapsack approach: it prioritizes entries from the most central files, recently active files, and core categories (types, store shapes) first, then fills the remaining budget with lower-priority items. This ensures the most valuable context always makes it in, even under tight limits.
66
+
67
+ ### Detecting architectural layers
68
+
69
+ **Problem:** Agents need to understand the high-level shape of a project — where the types live, where the API calls happen, where the UI components are.
70
+
71
+ codebrief classifies files into layers (types, stores, hooks, services, components, pages, utils, config) based on directory and naming conventions, then analyzes how those layers depend on each other. This gives agents a quick mental model like "services call the API, components use hooks, hooks read from stores."
72
+
73
+ ### Finding circular dependencies (Tarjan's SCC)
74
+
75
+ **Problem:** Circular imports cause subtle bugs, make refactoring dangerous, and confuse AI agents trying to understand dependency flow.
76
+
77
+ codebrief uses Tarjan's algorithm to find groups of files that form import cycles. For example, if `auth.ts → user.ts → permissions.ts → auth.ts`, all three files are reported as a circular dependency cluster. This is surfaced in the context so agents avoid introducing more cycles.
78
+
79
+ ### Flagging unstable files
80
+
81
+ **Problem:** Some files change frequently but are imported by many others — any modification risks breaking things across the project.
82
+
83
+ codebrief computes an instability score for each file based on how many files it imports vs. how many files import it. Files that are both highly unstable and widely depended on are flagged as risk zones — agents will know to be extra careful when modifying them.
84
+
85
+ ### Detecting change coupling
86
+
87
+ **Problem:** Some files always need to change together (e.g., a component and its test, or a route and its handler) but there's no import relationship between them.
88
+
89
+ codebrief analyzes 90 days of git history to find file pairs that frequently appear in the same commits. If two files were changed together in many commits, they're reported as coupled — so agents know that touching one likely means touching the other.
90
+
91
+ ### Discovering module clusters
92
+
93
+ **Problem:** Large projects have natural groupings of related files (an auth module, a payments module) but these aren't always obvious from the folder structure.
94
+
95
+ codebrief uses label propagation on the import graph: each file starts with its own label and iteratively adopts the most common label among its neighbors. Files that end up sharing a label form a natural cluster. This reveals logical modules even when the folder layout doesn't match.
96
+
97
+ ### Tracking git activity
98
+
99
+ **Problem:** Agents don't know which parts of the codebase are actively being worked on vs. which are stable.
100
+
101
+ codebrief counts commits per file over the last 90 days to surface hot spots and recently active files. This helps agents understand where current development is focused.
102
+
103
+ ### Detecting stale snapshots
104
+
105
+ **Problem:** After a refactor, the code snapshot in your context file may be outdated — describing types and signatures that no longer exist.
106
+
107
+ codebrief hashes all source file paths and modification times. When you run `--check`, it compares this hash against the stored one to tell you if the snapshot needs refreshing.
108
+
109
+ ## Supported Tools
110
+
111
+ | Tool | Files Generated |
112
+ |------|----------------|
113
+ | Claude Code | `CLAUDE.md` |
114
+ | Cursor | `CLAUDE.md` + `.cursor/rules/*.md` (glob-scoped) |
115
+ | OpenCode | `AGENTS.md` |
116
+ | GitHub Copilot | `.github/copilot-instructions.md` |
117
+ | Windsurf | `.windsurfrules` |
118
+ | Cline | `.clinerules` |
119
+ | Continue.dev | `.continuerules` |
120
+ | Aider | `.aider.conf.yml` |
121
+ | Generic | `CONTEXT.md` |
122
+
123
+ ## Options
124
+
125
+ ```bash
126
+ npx codebrief [directory] [options]
127
+ ```
128
+
129
+ | Flag | Description |
130
+ |------|-------------|
131
+ | `directory` | Path to analyze (defaults to current directory) |
132
+ | `--force` | Overwrite existing files without asking |
133
+ | `--dry-run` | Show what would be generated without writing any files |
134
+ | `--refresh-snapshot` | Re-scan source files and update the code snapshot in your existing context file |
135
+ | `--reconfigure` | Force re-prompting even if `.codebrief.json` exists |
136
+ | `--check` | Check if the snapshot is stale (exit code 0 = fresh, 1 = stale). Designed for shell integration. |
137
+ | `--max-tokens=N` | Set the token budget for the code snapshot |
138
+
139
+ ### Refreshing Snapshots
140
+
141
+ After a refactor, update just the code snapshot without re-generating the entire file:
142
+
143
+ ```bash
144
+ npx codebrief --refresh-snapshot
145
+ ```
146
+
147
+ This auto-detects your context file (CLAUDE.md, AGENTS.md, etc.), finds the `<!-- CODE SNAPSHOT -->` markers, re-scans source files, and replaces just that section in-place.
148
+
149
+ ## Shell Integration
150
+
151
+ Use `--check` to automatically detect stale snapshots when you `cd` into a project:
152
+
153
+ ### zsh
154
+
155
+ ```zsh
156
+ # Add to ~/.zshrc
157
+ chpwd() {
158
+ if [[ -f .codebrief.json ]]; then
159
+ npx --yes codebrief --check 2>/dev/null
160
+ fi
161
+ }
162
+ ```
163
+
164
+ ### bash
165
+
166
+ ```bash
167
+ # Add to ~/.bashrc
168
+ cd() {
169
+ builtin cd "$@" || return
170
+ if [[ -f .codebrief.json ]]; then
171
+ npx --yes codebrief --check 2>/dev/null
172
+ fi
173
+ }
174
+ ```
175
+
176
+ ### fish
177
+
178
+ ```fish
179
+ # Add to ~/.config/fish/conf.d/codebrief.fish
180
+ function __codebrief_check --on-variable PWD
181
+ if test -f .codebrief.json
182
+ npx --yes codebrief --check 2>/dev/null
183
+ end
184
+ end
185
+ ```
186
+
187
+ ## Config File
188
+
189
+ On first run, codebrief saves your answers to `.codebrief.json`:
190
+
191
+ ```json
192
+ {
193
+ "_version": 1,
194
+ "ide": "cursor",
195
+ "projectPurpose": "A mobile AI chat app...",
196
+ "keyPatterns": "Zustand slices for state...",
197
+ "gotchas": "Never use FadeIn/FadeOut...",
198
+ "generateSnapshot": true,
199
+ "snapshotPaths": [],
200
+ "stackCorrections": "",
201
+ "generatePerPackage": false
202
+ }
203
+ ```
204
+
205
+ Subsequent runs load this config and skip all prompts. Use `--reconfigure` to re-prompt with your saved values as defaults.
206
+
207
+ Add `.codebrief.json` to your `.gitignore` — it's a local tool config, not project documentation.
208
+
209
+ ## Framework Conventions
210
+
211
+ codebrief detects specific frameworks and automatically includes relevant best practices in the generated output:
212
+
213
+ - **Next.js** — App Router vs Pages Router patterns, server components, route handlers
214
+ - **Express** — middleware chain, error handling, router organization
215
+ - **FastAPI** — dependency injection, Pydantic models, async endpoints
216
+ - **Django** — apps structure, models-views-templates, migrations
217
+ - **NestJS** — modules, controllers, providers, guards
218
+ - **SvelteKit** — load functions, form actions, server routes
219
+ - **Expo / React Native** — routing, native modules, platform-specific files
220
+ - **Vue / Nuxt** — Composition API, auto-imports, data fetching
221
+ - And more: Fastify, Hono, Angular, Svelte, Prisma, Drizzle, Tailwind CSS, Electron
222
+
223
+ ## Monorepo Support
224
+
225
+ codebrief detects monorepo tooling and can generate per-package context files:
226
+
227
+ - **pnpm workspaces** (`pnpm-workspace.yaml`)
228
+ - **Turborepo** (`turbo.json`)
229
+ - **Nx** (`nx.json`)
230
+
231
+ When a monorepo is detected, you'll be asked if you want per-package context files. Each package gets its own scoped context with that package's specific dependencies, frameworks, and code snapshot.
232
+
233
+ ## Living Documents
234
+
235
+ Generated files include maintenance directives telling your AI agent to keep them up to date as the project evolves. The code snapshot section is marked with HTML comments so it's clear what to refresh after refactors.
236
+
237
+ ## Development
238
+
239
+ ```bash
240
+ npm install
241
+ npm run build # Build with tsup
242
+ npm run dev # Watch mode
243
+ npm run typecheck # Type-check without emitting
244
+ ```