@aiready/context-analyzer 0.4.6 → 0.5.1

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.
@@ -1,6 +1,6 @@
1
1
 
2
2
  
3
- > @aiready/context-analyzer@0.4.6 build /Users/pengcao/projects/aiready/packages/context-analyzer
3
+ > @aiready/context-analyzer@0.5.1 build /Users/pengcao/projects/aiready/packages/context-analyzer
4
4
  > tsup src/index.ts src/cli.ts --format cjs,esm --dts
5
5
 
6
6
  CLI Building entry: src/cli.ts, src/index.ts
@@ -9,15 +9,15 @@
9
9
  CLI Target: es2020
10
10
  CJS Build start
11
11
  ESM Build start
12
- CJS dist/cli.js 39.27 KB
13
12
  CJS dist/index.js 20.62 KB
14
- CJS ⚡️ Build success in 55ms
13
+ CJS dist/cli.js 39.27 KB
14
+ CJS ⚡️ Build success in 48ms
15
15
  ESM dist/index.mjs 164.00 B
16
- ESM dist/chunk-NJUW6VED.mjs 19.48 KB
17
16
  ESM dist/cli.mjs 18.45 KB
18
- ESM ⚡️ Build success in 55ms
17
+ ESM dist/chunk-NJUW6VED.mjs 19.48 KB
18
+ ESM ⚡️ Build success in 48ms
19
19
  DTS Build start
20
- DTS ⚡️ Build success in 703ms
20
+ DTS ⚡️ Build success in 547ms
21
21
  DTS dist/cli.d.ts 20.00 B
22
22
  DTS dist/index.d.ts 2.44 KB
23
23
  DTS dist/cli.d.mts 20.00 B
@@ -1,6 +1,6 @@
1
1
 
2
2
  
3
- > @aiready/context-analyzer@0.4.6 test /Users/pengcao/projects/aiready/packages/context-analyzer
3
+ > @aiready/context-analyzer@0.5.1 test /Users/pengcao/projects/aiready/packages/context-analyzer
4
4
  > vitest run
5
5
 
6
6
 
@@ -30,7 +30,7 @@
30
30
 
31
31
   Test Files  1 passed (1)
32
32
   Tests  13 passed (13)
33
-  Start at  07:25:31
34
-  Duration  318ms (transform 56ms, setup 0ms, collect 64ms, tests 3ms, environment 0ms, prepare 40ms)
33
+  Start at  08:00:54
34
+  Duration  315ms (transform 75ms, setup 0ms, collect 83ms, tests 3ms, environment 0ms, prepare 44ms)
35
35
 
36
36
  [?25h
package/README.md CHANGED
@@ -4,10 +4,15 @@
4
4
 
5
5
  When AI tools try to help with your code, they need to load files into their context window. Fragmented code structures make this expensive and sometimes impossible. This tool analyzes your codebase to identify:
6
6
 
7
- - Deep import chains that require loading dozens of files
8
- - Fragmented modules scattered across many directories
9
- - Low-cohesion files mixing unrelated concerns
10
- - Files with excessive context budgets
7
+ - 📦 **High Context Budget**: Files that cost too many AI tokens to understand (file + dependencies)
8
+ - 🔗 **Deep Import Chains**: Cascading dependencies that force AI to load many files
9
+ - 🎯 **Low Cohesion**: Files mixing unrelated concerns (God objects)
10
+ - 🗂️ **High Fragmentation**: Domains scattered across many directories
11
+
12
+ **Quick Start:**
13
+ ```bash
14
+ npx @aiready/context-analyzer ./src
15
+ ```
11
16
 
12
17
  ## 🎯 Why This Tool?
13
18
 
@@ -50,6 +55,147 @@ Result: AI sees everything, gives complete answers ✅
50
55
  - Use **@aiready/context-analyzer** to optimize for AI tools (advisory)
51
56
  - Track improvements over time with SaaS tier
52
57
 
58
+ ## 🧠 Understanding the Metrics
59
+
60
+ This tool measures four key dimensions that affect how much context AI tools need to load:
61
+
62
+ ### 📊 Context Budget (Tokens)
63
+
64
+ **What it measures:** Total AI tokens needed to understand a file (file content + all dependencies)
65
+
66
+ **Why it matters:** AI tools have limited context windows (e.g., 128K tokens). Large context budgets mean:
67
+ - AI needs to load more files to understand your code
68
+ - Risk of hitting context limits → incomplete/wrong answers
69
+ - Slower AI responses (more processing time)
70
+
71
+ **Example:**
72
+ ```typescript
73
+ // High context budget (15,000 tokens)
74
+ import { A, B, C } from './deeply/nested/utils' // +5,000 tokens
75
+ import { X, Y, Z } from './another/chain' // +8,000 tokens
76
+ // Your file: 2,000 tokens
77
+ // Total: 15,000 tokens just to understand this one file!
78
+
79
+ // Low context budget (2,500 tokens)
80
+ // No deep imports, self-contained logic
81
+ // Total: 2,500 tokens
82
+ ```
83
+
84
+ **🎯 Recommendation:** Files with high context budgets should be **split into smaller, more focused modules**.
85
+
86
+ ---
87
+
88
+ ### 🔗 Import Depth
89
+
90
+ **What it measures:** How many layers deep your import chains go
91
+
92
+ **Why it matters:** Deep import chains create cascading context loads:
93
+ ```
94
+ app.ts → service.ts → helper.ts → util.ts → core.ts → base.ts
95
+ ```
96
+ AI must load all 6 files just to understand app.ts!
97
+
98
+ **Example:**
99
+ ```typescript
100
+ // Deep chain (depth 8) = AI loads 8+ files
101
+ import { validate } from '../../../utils/validators/user/schema'
102
+
103
+ // Shallow (depth 2) = AI loads 2 files
104
+ import { validate } from './validators'
105
+ ```
106
+
107
+ **🎯 Recommendation:** Flatten dependency trees or use **facade patterns** to reduce depth.
108
+
109
+ ---
110
+
111
+ ### 🎯 Cohesion Score (0-1)
112
+
113
+ **What it measures:** How related the exports in a file are to each other
114
+
115
+ **How it's calculated:** Uses Shannon entropy of inferred domains
116
+ - 1.0 = Perfect cohesion (all exports are related)
117
+ - 0.0 = Zero cohesion (completely unrelated exports)
118
+
119
+ **Why it matters:** Low cohesion = "God object" pattern = AI confusion
120
+ ```typescript
121
+ // Low cohesion (0.3) - mixing unrelated concerns
122
+ export function validateUser() { } // User domain
123
+ export function formatDate() { } // Date domain
124
+ export function sendEmail() { } // Email domain
125
+ export class DatabasePool { } // Database domain
126
+ // AI thinks: "What does this file actually do?"
127
+
128
+ // High cohesion (0.9) - focused responsibility
129
+ export function validateUser() { }
130
+ export function createUser() { }
131
+ export function updateUser() { }
132
+ export interface User { }
133
+ // AI thinks: "Clear! This is user management."
134
+ ```
135
+
136
+ **🎯 Recommendation:** Files with low cohesion should be **split by domain** into separate, focused files.
137
+
138
+ ---
139
+
140
+ ### 🗂️ Fragmentation Score (0-1)
141
+
142
+ **What it measures:** How scattered a domain/concept is across different directories
143
+
144
+ **How it's calculated:** `(unique directories - 1) / (total files - 1)`
145
+ - 0.0 = No fragmentation (all files in same directory)
146
+ - 1.0 = Maximum fragmentation (each file in different directory)
147
+
148
+ **Why it matters:** Scattered domains force AI to load many unrelated paths
149
+ ```typescript
150
+ // High fragmentation (0.8) - User domain scattered
151
+ src/api/user-routes.ts // 800 tokens
152
+ src/services/user-service.ts // 1,200 tokens
153
+ src/helpers/user-helpers.ts // 600 tokens
154
+ src/utils/user-utils.ts // 500 tokens
155
+ src/validators/user-validator.ts // 700 tokens
156
+ src/models/user-model.ts // 900 tokens
157
+ // Total: 4,700 tokens spread across 6 directories!
158
+ // AI must navigate entire codebase to understand "User"
159
+
160
+ // Low fragmentation (0.0) - consolidated
161
+ src/user/user.ts // 2,800 tokens
162
+ src/user/types.ts // 600 tokens
163
+ // Total: 3,400 tokens in one place (29% savings!)
164
+ // AI finds everything in one logical location
165
+ ```
166
+
167
+ **🎯 Recommendation:** Domains with high fragmentation should be **consolidated** into cohesive modules.
168
+
169
+ ---
170
+
171
+ ### ⚖️ The Tradeoff: Splitting vs. Consolidating
172
+
173
+ **Important:** These metrics can pull in opposite directions!
174
+
175
+ | Action | Context Budget ⬇️ | Fragmentation ⬇️ | Cohesion ⬆️ |
176
+ |--------|------------------|------------------|-------------|
177
+ | **Split large file** | ✅ Reduces | ⚠️ May increase | ✅ Can improve |
178
+ | **Consolidate scattered files** | ⚠️ May increase | ✅ Reduces | ⚠️ May decrease |
179
+
180
+ **Best Practice:** Optimize for your use case:
181
+ - **Large files with mixed concerns** → Split by domain (improves cohesion + reduces budget)
182
+ - **Scattered single-domain files** → Consolidate (reduces fragmentation)
183
+ - **Large files with high cohesion** → May be OK if under context budget threshold
184
+ - **Small scattered files** → Consolidate into domain modules
185
+
186
+ **The tool helps you identify the right balance!**
187
+
188
+ ### 📋 Quick Reference Table
189
+
190
+ | Metric | Good ✅ | Bad ❌ | Fix |
191
+ |--------|---------|--------|-----|
192
+ | **Context Budget** | < 10K tokens | > 25K tokens | Split large files |
193
+ | **Import Depth** | ≤ 5 levels | ≥ 8 levels | Flatten dependencies |
194
+ | **Cohesion** | > 0.6 (60%) | < 0.4 (40%) | Split by domain |
195
+ | **Fragmentation** | < 0.5 (50%) | > 0.7 (70%) | Consolidate domain |
196
+
197
+ **Rule of thumb:** The tool flags files that make AI's job harder (expensive to load, confusing to understand, scattered to find).
198
+
53
199
  ## 🚀 Installation
54
200
 
55
201
  ```bash
@@ -67,6 +213,9 @@ npx @aiready/context-analyzer ./src
67
213
  # Basic usage
68
214
  aiready-context ./src
69
215
 
216
+ # Show more results in console (default: 10)
217
+ aiready-context ./src --max-results 25
218
+
70
219
  # Focus on specific concerns
71
220
  aiready-context ./src --focus fragmentation
72
221
  aiready-context ./src --focus cohesion
@@ -75,7 +224,7 @@ aiready-context ./src --focus depth
75
224
  # Set thresholds
76
225
  aiready-context ./src --max-depth 5 --max-context 10000 --min-cohesion 0.6
77
226
 
78
- # Export to JSON (saved to .aiready/ by default)
227
+ # Export to JSON for full details (saved to .aiready/ by default)
79
228
  aiready-context ./src --output json
80
229
 
81
230
  # Or specify custom path
@@ -88,6 +237,15 @@ aiready-context ./src --output json --output-file custom-report.json
88
237
 
89
238
  **Smart defaults automatically adjust based on your repository size** to show ~10 most serious issues.
90
239
 
240
+ > **💡 Tip:** By default, console output shows the top 10 results per category. Use `--max-results <number>` to see more, or use `--output json` to get complete details of all issues.
241
+
242
+ ### Understanding Threshold Tuning
243
+
244
+ Each parameter controls **when the tool flags a file as problematic**. Think of them as sensitivity dials:
245
+
246
+ - **Lower values** = More strict = More issues reported = More sensitive
247
+ - **Higher values** = More lenient = Fewer issues reported = Less sensitive
248
+
91
249
  ### Getting More/Fewer Results
92
250
 
93
251
  **Want to catch MORE potential issues?** (More sensitive, shows smaller problems)
@@ -95,23 +253,39 @@ aiready-context ./src --output json --output-file custom-report.json
95
253
  ```bash
96
254
  # Lower thresholds to be more strict:
97
255
  aiready-context ./src --max-depth 3 --max-context 5000 --min-cohesion 0.7 --max-fragmentation 0.4
256
+ # ↓ ↓ ↑ ↓
257
+ # Catches depth≥4 Catches 5K+ tokens Requires 70%+ cohesion Catches 40%+ fragmentation
98
258
  ```
99
259
 
260
+ **What this means:**
261
+ - `--max-depth 3`: Flag files with import depth ≥4 (stricter than default 5-7)
262
+ - `--max-context 5000`: Flag files needing 5K+ tokens (catches smaller files)
263
+ - `--min-cohesion 0.7`: Require 70%+ cohesion (stricter about mixed concerns)
264
+ - `--max-fragmentation 0.4`: Flag domains with 40%+ scatter (catches less severe fragmentation)
265
+
100
266
  **Want to see FEWER issues?** (Less noise, focus on critical problems only)
101
267
 
102
268
  ```bash
103
269
  # Raise thresholds to be more lenient:
104
270
  aiready-context ./src --max-depth 10 --max-context 30000 --min-cohesion 0.4 --max-fragmentation 0.8
271
+ # ↑ ↑ ↓ ↑
272
+ # Only depth≥11 Only 30K+ tokens Allows 40%+ cohesion Only 80%+ fragmentation
105
273
  ```
106
274
 
275
+ **What this means:**
276
+ - `--max-depth 10`: Only flag import depth ≥11 (very deep chains)
277
+ - `--max-context 30000`: Only flag files needing 30K+ tokens (only huge files)
278
+ - `--min-cohesion 0.4`: Accept 40%+ cohesion (more lenient about mixed concerns)
279
+ - `--max-fragmentation 0.8`: Only flag 80%+ scatter (only severely fragmented)
280
+
107
281
  ### Threshold Parameters Explained
108
282
 
109
- | Parameter | Default (Auto) | Lower = More Strict | Higher = Less Strict |
110
- |-----------|---------------|-------------------|---------------------|
111
- | `--max-depth` | 4-10* | Catches shallower imports | Only very deep chains |
112
- | `--max-context` | 8k-40k* | Catches smaller files | Only huge files |
113
- | `--min-cohesion` | 0.35-0.5* | Stricter about mixed concerns | More lenient |
114
- | `--max-fragmentation` | 0.5-0.8* | Catches less scattered code | Only severely scattered |
283
+ | Parameter | Default (Auto) | Lower = More Strict | Higher = Less Strict | Impact |
284
+ |-----------|---------------|-------------------|---------------------|--------|
285
+ | `--max-depth` | 4-10* | Catches shallower imports | Only very deep chains | More splits → flatter structure |
286
+ | `--max-context` | 8k-40k* | Catches smaller files | Only huge files | More splits → smaller modules |
287
+ | `--min-cohesion` | 0.35-0.5* | Stricter about mixed concerns | More lenient | More splits → focused files |
288
+ | `--max-fragmentation` | 0.5-0.8* | Catches less scattered code | Only severely scattered | More consolidation → domain modules |
115
289
 
116
290
  \* Auto-adjusted based on your repository size (100 files vs 2000+ files)
117
291
 
@@ -120,21 +294,79 @@ aiready-context ./src --max-depth 10 --max-context 30000 --min-cohesion 0.4 --ma
120
294
  **Small codebase getting too many warnings?**
121
295
  ```bash
122
296
  aiready-context ./src --max-depth 6 --min-cohesion 0.5
297
+ # Explanation: Allow slightly deeper imports and more mixed concerns
298
+ # Use when: Your codebase is naturally small and warnings feel excessive
123
299
  ```
124
300
 
125
301
  **Large codebase showing too few issues?**
126
302
  ```bash
127
303
  aiready-context ./src --max-depth 5 --max-context 15000
304
+ # Explanation: Be stricter about depth and context to catch more problems
305
+ # Use when: You know there are issues but they're not being detected
128
306
  ```
129
307
 
130
308
  **Focus on critical issues only:**
131
309
  ```bash
132
310
  aiready-context ./src --max-depth 8 --max-context 25000 --min-cohesion 0.3
311
+ # Explanation: Very lenient - only show the worst offenders
312
+ # Use when: Fixing warnings in stages, start with critical issues first
313
+ ```
314
+
315
+ **Preparing for AI refactoring sprint:**
316
+ ```bash
317
+ aiready-context ./src --max-depth 4 --max-context 8000 --min-cohesion 0.6 --max-fragmentation 0.5
318
+ # Explanation: Strict on all dimensions to get comprehensive issue list
319
+ # Use when: Planning a major refactoring effort, need complete audit
133
320
  ```
134
321
 
322
+ **Microservices architecture (naturally fragmented):**
323
+ ```bash
324
+ aiready-context ./src --max-fragmentation 0.9
325
+ # Explanation: Very lenient on fragmentation (services are meant to be separate)
326
+ # Use when: Analyzing microservices where fragmentation is intentional
327
+ ```
328
+
329
+ ## 📤 Output Options
330
+
331
+ ### Console Output (Default)
332
+
333
+ Shows a summary with top 10 results per category:
334
+
335
+ ```bash
336
+ # Default - shows top 10 items
337
+ aiready-context ./src
338
+
339
+ # Show more items (e.g., top 25)
340
+ aiready-context ./src --max-results 25
341
+
342
+ # Show all items (use a large number)
343
+ aiready-context ./src --max-results 999
344
+ ```
345
+
346
+ ### JSON Output
347
+
348
+ Get complete details of **all** issues (not limited to 10):
349
+
350
+ ```bash
351
+ # Generate JSON with all issues
352
+ aiready-context ./src --output json
353
+
354
+ # Custom output path
355
+ aiready-context ./src --output json --output-file reports/analysis.json
356
+ ```
357
+
358
+ ### HTML Report
359
+
360
+ Visual report with charts and detailed breakdown:
361
+
362
+ ```bash
135
363
  # Generate HTML report
136
364
  aiready-context ./src --output html --output-file report.html
365
+ ```
366
+
367
+ ### Include/Exclude Patterns
137
368
 
369
+ ```bash
138
370
  # Include/exclude patterns
139
371
  aiready-context ./src --exclude "**/test/**,**/*.test.ts"
140
372
  ```
@@ -154,12 +386,29 @@ Create an `aiready.json` or `aiready.config.json` file in your project root:
154
386
  "maxDepth": 4,
155
387
  "maxContextBudget": 8000,
156
388
  "minCohesion": 0.7,
157
- "includeNodeModules": false
389
+ "maxFragmentation": 0.6,
390
+ "focus": "all",
391
+ "maxResults": 10
158
392
  }
393
+ },
394
+ "output": {
395
+ "format": "console"
159
396
  }
160
397
  }
161
398
  ```
162
399
 
400
+ **Configuration Options:**
401
+
402
+ | Option | Type | Default | Description |
403
+ |--------|------|---------|-------------|
404
+ | `maxDepth` | number | `5` | Max acceptable import depth |
405
+ | `maxContextBudget` | number | `10000` | Max acceptable token budget |
406
+ | `minCohesion` | number | `0.6` | Min acceptable cohesion score (0-1) |
407
+ | `maxFragmentation` | number | `0.5` | Max acceptable fragmentation (0-1) |
408
+ | `focus` | string | `'all'` | Focus: `'fragmentation'`, `'cohesion'`, `'depth'`, `'all'` |
409
+ | `maxResults` | number | `10` | Max results per category in console |
410
+ | `includeNodeModules` | boolean | `false` | Include node_modules in analysis |
411
+
163
412
  ### Sample Output
164
413
 
165
414
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiready/context-analyzer",
3
- "version": "0.4.6",
3
+ "version": "0.5.1",
4
4
  "description": "AI context window cost analysis - detect fragmented code, deep import chains, and expensive context budgets",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",