@aiready/context-analyzer 0.9.33 → 0.9.35

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.9.33 build /Users/pengcao/projects/aiready/packages/context-analyzer
3
+ > @aiready/context-analyzer@0.9.35 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,16 +9,16 @@
9
9
  CLI Target: es2020
10
10
  CJS Build start
11
11
  ESM Build start
12
- CJS dist/cli.js 85.15 KB
13
- CJS dist/index.js 74.12 KB
14
- CJS ⚡️ Build success in 241ms
15
- ESM dist/index.mjs 666.00 B
16
12
  ESM dist/cli.mjs 18.50 KB
13
+ ESM dist/index.mjs 666.00 B
17
14
  ESM dist/python-context-PAETRLDY.mjs 5.48 KB
18
15
  ESM dist/chunk-EVX2W2BK.mjs 65.61 KB
19
- ESM ⚡️ Build success in 242ms
16
+ ESM ⚡️ Build success in 1679ms
17
+ CJS dist/cli.js 85.15 KB
18
+ CJS dist/index.js 74.12 KB
19
+ CJS ⚡️ Build success in 1680ms
20
20
  DTS Build start
21
- DTS ⚡️ Build success in 11127ms
21
+ DTS ⚡️ Build success in 68534ms
22
22
  DTS dist/cli.d.ts 20.00 B
23
23
  DTS dist/index.d.ts 8.07 KB
24
24
  DTS dist/cli.d.mts 20.00 B
@@ -1,32 +1,34 @@
1
1
 
2
2
  
3
- > @aiready/context-analyzer@0.9.33 test /Users/pengcao/projects/aiready/packages/context-analyzer
3
+ > @aiready/context-analyzer@0.9.35 test /Users/pengcao/projects/aiready/packages/context-analyzer
4
4
  > vitest run
5
5
 
6
6
  [?25l
7
7
   RUN  v4.0.18 /Users/pengcao/projects/aiready/packages/context-analyzer
8
8
 
9
- ✓ dist/__tests__/scoring.test.js (7 tests) 4ms
10
- ✓ src/__tests__/scoring.test.ts (7 tests) 3ms
11
- ✓ src/__tests__/enhanced-cohesion.test.ts (6 tests) 2ms
12
- ✓ src/__tests__/file-classification.test.ts (63 tests) 10ms
13
- ✓ dist/__tests__/analyzer.test.js (14 tests) 30ms
14
- ✓ dist/__tests__/structural-cohesion.test.js (4 tests) 13ms
15
- ✓ src/__tests__/fragmentation-coupling.test.ts (2 tests) 13ms
16
- ✓ src/__tests__/auto-detection.test.ts (8 tests) 82ms
17
- ✓ src/__tests__/analyzer.test.ts (14 tests) 198ms
18
- ✓ dist/__tests__/auto-detection.test.js (8 tests) 155ms
19
- ✓ dist/__tests__/fragmentation-coupling.test.js (2 tests) 12ms
20
- ✓ dist/__tests__/enhanced-cohesion.test.js (6 tests) 2ms
21
- ✓ dist/__tests__/fragmentation-advanced.test.js (3 tests) 3ms
22
- ✓ src/__tests__/fragmentation-advanced.test.ts (3 tests) 16ms
23
- ✓ dist/__tests__/fragmentation-log.test.js (3 tests) 1ms
24
- ✓ src/__tests__/fragmentation-log.test.ts (3 tests) 1ms
25
- ✓ src/__tests__/structural-cohesion.test.ts (4 tests) 2ms
9
+ ✓ dist/__tests__/scoring.test.js (7 tests) 2ms
10
+ ✓ src/__tests__/fragmentation-log.test.ts (3 tests) 3ms
11
+ ✓ dist/__tests__/auto-detection.test.js (8 tests) 376ms
12
+ ✓ src/__tests__/fragmentation-advanced.test.ts (3 tests) 6ms
13
+ ✓ src/__tests__/analyzer.test.ts (14 tests) 461ms
14
+ ✓ should build a basic dependency graph  368ms
15
+ ✓ dist/__tests__/analyzer.test.js (14 tests) 430ms
16
+ ✓ should build a basic dependency graph  342ms
17
+ ✓ src/__tests__/auto-detection.test.ts (8 tests) 267ms
18
+ ✓ src/__tests__/scoring.test.ts (7 tests) 67ms
19
+ ✓ src/__tests__/file-classification.test.ts (63 tests) 241ms
20
+ ✓ dist/__tests__/fragmentation-coupling.test.js (2 tests) 526ms
21
+ ✓ src/__tests__/fragmentation-coupling.test.ts (2 tests) 478ms
22
+ ✓ src/__tests__/enhanced-cohesion.test.ts (6 tests) 213ms
23
+ ✓ dist/__tests__/fragmentation-advanced.test.js (3 tests) 21ms
24
+ ✓ dist/__tests__/enhanced-cohesion.test.js (6 tests) 6ms
25
+ ✓ src/__tests__/structural-cohesion.test.ts (4 tests) 95ms
26
+ ✓ dist/__tests__/fragmentation-log.test.js (3 tests) 4ms
27
+ ✓ dist/__tests__/structural-cohesion.test.js (4 tests) 6ms
26
28
 
27
29
   Test Files  17 passed (17)
28
30
   Tests  157 passed (157)
29
-  Start at  22:34:02
30
-  Duration  1.96s (transform 1.71s, setup 0ms, import 12.63s, tests 547ms, environment 8ms)
31
+  Start at  14:39:24
32
+  Duration  16.56s (transform 13.09s, setup 0ms, import 111.90s, tests 3.20s, environment 3ms)
31
33
 
32
34
  [?25h
package/README.md CHANGED
@@ -1,8 +1,13 @@
1
1
  # @aiready/context-analyzer
2
2
 
3
- > **AI context window cost analysis - Detect fragmented code, deep import chains, and expensive context budgets**
3
+ > AIReady Spoke: Analyzes import chains, fragmented code, and context window costs for AI tools.
4
4
 
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:
5
+ [![npm version](https://img.shields.io/npm/v/@aiready/context-analyzer.svg)](https://npmjs.com/package/@aiready/context-analyzer)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ ## Overview
9
+
10
+ AI model context windows are precious and expensive. The **Context Analyzer** identifies import chains, redundant dependencies, and complex data structures that bloat your context window and degrade AI reasoning performance.
6
11
 
7
12
  ## 🏛️ Architecture
8
13
 
@@ -10,965 +15,42 @@ When AI tools try to help with your code, they need to load files into their con
10
15
  🎯 USER
11
16
 
12
17
 
13
- 🎛️ CLI (orchestrator)
14
-
15
- ┌─────────────────┴─────────────────┐
16
- │ │
17
- ▼ ▼
18
- ┌────────┐ ┌────────┐
19
- │🎨 VIS- │ ANALY- │
20
- │UALIZER │ │ SIS │
21
- │✅ Ready│ │ SPOKES │
22
- └────────┘ └───┬────┘
23
- │ │
24
- │ ┌─────────────────────┼─────────────────────┐
25
- │ ▼ ▼ ▼
26
- │ ┌────────┐ ┌────────┐ ┌────────┐
27
- │ │📊 PAT- │ │📦 CON- │ │🔧 CON- │
28
- │ │TERN │ │TEXT │ │SISTENCY│
29
- │ │DETECT │ │ANALYZER│ │ │
30
- │ │ │ │ │ │ │
31
- │ │✅ Ready│ │✅ Ready│ │✅ Ready│
32
- │ └────────┘ └────────┘ └────────┘
33
- │ │ │
34
- │ ← YOU ARE HERE │
35
- │ │
36
- └───────────────────────────────────────────────────────┘
37
-
38
-
39
- 🏢 HUB (@aiready/core)
40
- ```
41
-
42
- ## 🌍 Language Support
43
-
44
- **Currently Supported (64% market coverage):**
45
-
46
- - ✅ **TypeScript** (`.ts`, `.tsx`) - Import chains, context budget, cohesion
47
- - ✅ **JavaScript** (`.js`, `.jsx`) - Import chains, context budget, cohesion
48
- - ✅ **Python** (`.py`) - Import chains, context budget, circular deps, cohesion
49
-
50
- **Roadmap:**
51
-
52
- - 🔜 **Java** (Q3 2026) - Package dependencies, Maven/Gradle analysis
53
- - 🔜 **Go** (Q4 2026) - Module dependencies, package cohesion
54
- - 🔜 **Rust** (Q4 2026) - Crate dependencies, module structure
55
- - 🔜 **C#** (Q1 2027) - Namespace dependencies, project references
56
-
57
- ## 🎯 Why This Tool?
58
-
59
- 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:
60
-
61
- - 📦 **High Context Budget**: Files that cost too many AI tokens to understand (file + dependencies)
62
- - 🔗 **Deep Import Chains**: Cascading dependencies that force AI to load many files
63
- - 🎯 **Low Cohesion**: Files mixing unrelated concerns (God objects)
64
- - 🗂️ **High Fragmentation**: Domains scattered across many directories
65
-
66
- ## 🚀 Quick Start
67
-
68
- **Zero config, works out of the box:**
69
-
70
- ```bash
71
- # Run without installation (recommended)
72
- npx @aiready/context-analyzer ./src
73
-
74
- # Or use the unified CLI (includes all AIReady tools)
75
- npx @aiready/cli scan ./src
76
-
77
- # Or install globally for simpler command and faster runs
78
- npm install -g @aiready/context-analyzer
79
- aiready-context ./src
80
- ```
81
-
82
- ### 🎯 Input & Output
83
-
84
- **Input:** Path to your source code directory
85
-
86
- ```bash
87
- aiready-context ./src
88
- ```
89
-
90
- **Output:** Terminal report + optional JSON file (saved to `.aiready/` directory)
91
-
92
- ```
93
- 📊 Context Analysis Results
94
- ━━━━━━━━━━━━━━━━━━━━━━━━━━━
95
- 📁 Files analyzed: 47
96
- ⚠️ Issues found: 8 files with problems
97
-
98
- CRITICAL (3 files)
99
- src/services/user.ts
100
- • Context budget: 15,234 tokens (HIGH)
101
- • Import depth: 8 levels (DEEP)
102
- • Cohesion: 0.23 (LOW)
103
- ```
104
-
105
- ### ✨ Smart Defaults (Zero Config)
106
-
107
- - ✅ **Auto-excludes** test files (`**/*.test.*`, `**/*.spec.*`, `**/__tests__/**`)
108
- - ✅ **Auto-excludes** build outputs (`dist/`, `build/`, `.next/`, `cdk.out/`)
109
- - ✅ **Auto-excludes** dependencies (`node_modules/`)
110
- - ✅ **Auto-detects** frameworks (Next.js, AWS CDK) and adjusts analysis
111
- - ✅ **Adaptive thresholds**: Adjusts issue detection based on project complexity
112
-
113
- > Override defaults with `--include-tests` or `--exclude <patterns>` as needed
114
-
115
- ## 🎯 Why This Tool?
116
-
117
- ### The AI Context Cost Problem
118
-
119
- AI coding assistants are limited by context windows, but teams unknowingly structure code in ways that maximize context consumption:
120
-
121
- ```typescript
122
- // Scattered user management across 8 files = 12,450 tokens
123
- src/user/get.ts // 850 tokens
124
- src/api/user.ts // 1,200 tokens
125
- src/services/user.ts // 2,100 tokens
126
- src/helpers/user.ts // 900 tokens
127
- src/utils/user.ts // 750 tokens
128
- src/lib/user-validation.ts // 1,800 tokens
129
- src/models/user.ts // 2,100 tokens
130
- src/types/user.ts // 2,750 tokens
131
-
132
- Result: AI hits context limit, gives incomplete answers ❌
133
-
134
- // Consolidated into 2 cohesive files = 2,100 tokens
135
- src/user/user.ts // 1,400 tokens (core logic)
136
- src/user/types.ts // 700 tokens (types)
137
-
138
- Result: AI sees everything, gives complete answers ✅
139
- ```
140
-
141
- ### What Makes Us Different?
142
-
143
- | Feature | madge | dependency-cruiser | @aiready/context-analyzer |
144
- |---------|-------|-------------------|--------------------------|
145
- | Focus | Circular dependencies | Dependency rules | AI context cost |
146
- | Metrics | Graph visualization | Rule violations | Token cost, fragmentation |
147
- | AI-Specific | ❌ No | ❌ No | ✅ Yes - quantifies AI impact |
148
- | Cohesion Analysis | ❌ No | ❌ No | ✅ Yes - detects mixed concerns |
149
- | Recommendations | Generic | Rule-based | AI context optimization |
150
-
151
- **Recommended Workflow:**
152
-
153
- - Use **dependency-cruiser** to enforce architecture rules (blocking)
154
- - Use **@aiready/context-analyzer** to optimize for AI tools (advisory)
155
- - Track improvements over time with SaaS tier
156
-
157
- **Related AIReady Tools:**
158
-
159
- - [**@aiready/cli**](https://www.npmjs.com/package/@aiready/cli) - Unified CLI with all analysis tools
160
- - [**@aiready/pattern-detect**](https://www.npmjs.com/package/@aiready/pattern-detect) - Semantic duplicate detection
161
- - [**@aiready/consistency**](https://www.npmjs.com/package/@aiready/consistency) - Consistency checking
162
-
163
- ## 🧠 Understanding the Metrics
164
-
165
- This tool measures four key dimensions that affect how much context AI tools need to load:
166
-
167
- ### 📊 Context Budget (Tokens)
168
-
169
- **What it measures:** Total AI tokens needed to understand a file (file content + all dependencies)
170
-
171
- **Why it matters:** AI tools have limited context windows (e.g., 128K tokens). Large context budgets mean:
172
-
173
- - AI needs to load more files to understand your code
174
- - Risk of hitting context limits → incomplete/wrong answers
175
- - Slower AI responses (more processing time)
176
-
177
- **Example:**
178
-
179
- ```typescript
180
- // High context budget (15,000 tokens)
181
- import { A, B, C } from './deeply/nested/utils' // +5,000 tokens
182
- import { X, Y, Z } from './another/chain' // +8,000 tokens
183
- // Your file: 2,000 tokens
184
- // Total: 15,000 tokens just to understand this one file!
185
-
186
- // Low context budget (2,500 tokens)
187
- // No deep imports, self-contained logic
188
- // Total: 2,500 tokens
189
- ```
190
-
191
- **🎯 Recommendation:** Files with high context budgets should be **split into smaller, more focused modules**.
192
-
193
- ---
194
-
195
- ### 🔗 Import Depth
196
-
197
- **What it measures:** How many layers deep your import chains go
198
-
199
- **Why it matters:** Deep import chains create cascading context loads:
200
-
201
- ```
202
- app.ts → service.ts → helper.ts → util.ts → core.ts → base.ts
203
- ```
204
-
205
- AI must load all 6 files just to understand app.ts!
206
-
207
- **Example:**
208
-
209
- ```typescript
210
- // Deep chain (depth 8) = AI loads 8+ files
211
- import { validate } from '../../../utils/validators/user/schema'
212
-
213
- // Shallow (depth 2) = AI loads 2 files
214
- import { validate } from './validators'
215
- ```
216
-
217
- **🎯 Recommendation:** Flatten dependency trees or use **facade patterns** to reduce depth.
218
-
219
- ---
220
-
221
- ### 🎯 Cohesion Score (0-1)
222
-
223
- **What it measures:** How related the exports in a file are to each other
224
-
225
- **How it's calculated:** Uses Shannon entropy of inferred domains
226
-
227
- - 1.0 = Perfect cohesion (all exports are related)
228
- - 0.0 = Zero cohesion (completely unrelated exports)
229
-
230
- **Why it matters:** Low cohesion = "God object" pattern = AI confusion
231
-
232
- ```typescript
233
- // Low cohesion (0.3) - mixing unrelated concerns
234
- export function validateUser() { } // User domain
235
- export function formatDate() { } // Date domain
236
- export function sendEmail() { } // Email domain
237
- export class DatabasePool { } // Database domain
238
- // AI thinks: "What does this file actually do?"
239
-
240
- // High cohesion (0.9) - focused responsibility
241
- export function validateUser() { }
242
- export function createUser() { }
243
- export function updateUser() { }
244
- export interface User { }
245
- // AI thinks: "Clear! This is user management."
246
- ```
247
-
248
- **🎯 Recommendation:** Files with low cohesion should be **split by domain** into separate, focused files.
249
-
250
- ---
251
-
252
- ### 🗂️ Fragmentation Score (0-1)
253
-
254
- **What it measures:** How scattered a domain/concept is across different directories
255
-
256
- **How it's calculated:** `(unique directories - 1) / (total files - 1)`
257
-
258
- - 0.0 = No fragmentation (all files in same directory)
259
- - 1.0 = Maximum fragmentation (each file in different directory)
260
-
261
- **Why it matters:** Scattered domains force AI to load many unrelated paths
262
-
263
- ```typescript
264
- // High fragmentation (0.8) - User domain scattered
265
- src/api/user-routes.ts // 800 tokens
266
- src/services/user-service.ts // 1,200 tokens
267
- src/helpers/user-helpers.ts // 600 tokens
268
- src/utils/user-utils.ts // 500 tokens
269
- src/validators/user-validator.ts // 700 tokens
270
- src/models/user-model.ts // 900 tokens
271
- // Total: 4,700 tokens spread across 6 directories!
272
- // AI must navigate entire codebase to understand "User"
273
-
274
- // Low fragmentation (0.0) - consolidated
275
- src/user/user.ts // 2,800 tokens
276
- src/user/types.ts // 600 tokens
277
- // Total: 3,400 tokens in one place (29% savings!)
278
- // AI finds everything in one logical location
279
- ```
280
-
281
- **🎯 Recommendation:** Domains with high fragmentation should be **consolidated** into cohesive modules.
282
-
283
- ### 🔎 Additional Fragmentation Signals (new)
284
-
285
- - **Path Entropy:** A normalized Shannon-entropy over directory distribution. Distinguishes "9 files in one folder + 1 outlier" from "5 and 5" — higher entropy means more evenly spread (worse).
286
- - **Directory Distance:** Measures how deep the common ancestor is across file pairs. Files sharing a deep common ancestor are considered "closer" (lower distance) than files only sharing the repo root.
287
- - **Import Cohesion (coupling discount):** If fragmented files actively import each other, the analyzer reduces the fragmentation penalty (up to 20%). This rewards intentional modularization where cross-folder imports are a natural design instead of accidental scattering.
288
-
289
- These signals are included in the JSON summary (`fragmentedModules` entries) so you can prioritize consolidation efforts with more context: `fragmentationScore`, `pathEntropy`, `directoryDistance`, and `importCohesion`.
290
-
291
- ---
292
-
293
- ### 🏷️ File Classification (Reducing False Positives)
294
-
295
- **What it measures:** The tool now classifies files into categories to distinguish real issues from acceptable patterns:
296
-
297
- | Classification | Description | Fragmentation Treatment |
298
- |---------------|-------------|------------------------|
299
- | **barrel-export** | Re-exports from other modules (index.ts) | 0% (expected behavior) |
300
- | **type-definition** | Primarily type/interface definitions | 0% (centralized types are good) |
301
- | **cohesive-module** | Single domain, high cohesion (≥70%) | 30% of original (large but focused is OK) |
302
- | **mixed-concerns** | Multiple domains, low cohesion | 100% (real issue) |
303
- | **unknown** | Doesn't fit other categories | 70% of original (benefit of doubt) |
304
-
305
- **Why it matters:** Previous versions could flag files that were actually well-designed:
306
-
307
- ```typescript
308
- // ❌ Old behavior: Flagged as "high fragmentation (99%)"
309
- // shared/src/index.ts - barrel export with 6 domains
310
- export * from './user';
311
- export * from './order';
312
- export * from './product';
313
- // ... more re-exports
314
-
315
- // ✅ New behavior: Classified as "barrel-export"
316
- // Fragmentation score: 0% (this is what barrel files DO)
317
- // Recommendation: "Barrel export file detected - multiple domains are expected here"
318
- ```
319
-
320
- ```typescript
321
- // ❌ Old behavior: Flagged as "high fragmentation (98%)"
322
- // gst-calculator/client.tsx - 346 lines, 7 dependencies, 1 domain
323
-
324
- // ✅ New behavior: Classified as "cohesive-module"
325
- // Fragmentation score: 29% (reduced from 98%)
326
- // Recommendation: "Module has good cohesion despite its size"
327
- ```
328
-
329
- **🎯 Benefit:** Focus on real issues while accepting legitimate patterns.
330
-
331
- **JSON Output includes classification:**
332
-
333
- ```json
334
- {
335
- "file": "src/index.ts",
336
- "fileClassification": "barrel-export",
337
- "fragmentationScore": 0,
338
- "severity": "info"
339
- }
340
- ```
341
-
342
- ---
343
-
344
- ### ⚖️ The Tradeoff: Splitting vs. Consolidating
345
-
346
- **Important:** These metrics can pull in opposite directions!
347
-
348
- | Action | Context Budget ⬇️ | Fragmentation ⬇️ | Cohesion ⬆️ |
349
- |--------|------------------|------------------|-------------|
350
- | **Split large file** | ✅ Reduces | ⚠️ May increase | ✅ Can improve |
351
- | **Consolidate scattered files** | ⚠️ May increase | ✅ Reduces | ⚠️ May decrease |
352
-
353
- **Best Practice:** Optimize for your use case:
354
-
355
- - **Large files with mixed concerns** → Split by domain (improves cohesion + reduces budget)
356
- - **Scattered single-domain files** → Consolidate (reduces fragmentation)
357
- - **Large files with high cohesion** → May be OK if under context budget threshold
358
- - **Small scattered files** → Consolidate into domain modules
359
-
360
- **The tool helps you identify the right balance!**
361
-
362
- ### 📋 Quick Reference Table
363
-
364
- | Metric | Good ✅ | Bad ❌ | Fix |
365
- |--------|---------|--------|-----|
366
- | **Context Budget** | < 10K tokens | > 25K tokens | Split large files |
367
- | **Import Depth** | ≤ 5 levels | ≥ 8 levels | Flatten dependencies |
368
- | **Cohesion** | > 0.6 (60%) | < 0.4 (40%) | Split by domain |
369
- | **Fragmentation** | < 0.5 (50%) | > 0.7 (70%) | Consolidate domain |
370
-
371
- **Rule of thumb:** The tool flags files that make AI's job harder (expensive to load, confusing to understand, scattered to find).
372
-
373
- ## 🚀 Installation
374
-
375
- ```bash
376
- npm install -g @aiready/context-analyzer
377
-
378
- # Or use directly with npx
379
- npx @aiready/context-analyzer ./src
380
- ```
381
-
382
- ## 📊 Usage
383
-
384
- ### CLI
385
-
386
- ```bash
387
- # Basic usage
388
- aiready-context ./src
389
-
390
- # Show more results in console (default: 10)
391
- aiready-context ./src --max-results 25
392
-
393
- # Focus on specific concerns
394
- aiready-context ./src --focus fragmentation
395
- aiready-context ./src --focus cohesion
396
- aiready-context ./src --focus depth
397
-
398
- # Set thresholds
399
- aiready-context ./src --max-depth 5 --max-context 10000 --min-cohesion 0.6
400
-
401
- # Export to JSON for full details (saved to .aiready/ by default)
402
- aiready-context ./src --output json
403
-
404
- # Or specify custom path
405
- aiready-context ./src --output json --output-file custom-report.json
406
- ```
407
-
408
- > **📁 Output Files:** By default, all output files are saved to the `.aiready/` directory in your project root. You can override this with `--output-file`.
409
-
410
- ## 🎛️ Tuning Guide
411
-
412
- **Smart defaults automatically adjust based on your repository size** to show ~10 most serious issues.
413
-
414
- > **💡 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.
415
-
416
- ### Understanding Threshold Tuning
417
-
418
- Each parameter controls **when the tool flags a file as problematic**. Think of them as sensitivity dials:
419
-
420
- - **Lower values** = More strict = More issues reported = More sensitive
421
- - **Higher values** = More lenient = Fewer issues reported = Less sensitive
422
-
423
- ### Getting More/Fewer Results
424
-
425
- **Want to catch MORE potential issues?** (More sensitive, shows smaller problems)
426
-
427
- ```bash
428
- # Lower thresholds to be more strict:
429
- aiready-context ./src --max-depth 3 --max-context 5000 --min-cohesion 0.7 --max-fragmentation 0.4
430
- # ↓ ↓ ↑ ↓
431
- # Catches depth≥4 Catches 5K+ tokens Requires 70%+ cohesion Catches 40%+ fragmentation
432
- ```
433
-
434
- **What this means:**
435
-
436
- - `--max-depth 3`: Flag files with import depth ≥4 (stricter than default 5-7)
437
- - `--max-context 5000`: Flag files needing 5K+ tokens (catches smaller files)
438
- - `--min-cohesion 0.7`: Require 70%+ cohesion (stricter about mixed concerns)
439
- - `--max-fragmentation 0.4`: Flag domains with 40%+ scatter (catches less severe fragmentation)
440
-
441
- **Want to see FEWER issues?** (Less noise, focus on critical problems only)
442
-
443
- ```bash
444
- # Raise thresholds to be more lenient:
445
- aiready-context ./src --max-depth 10 --max-context 30000 --min-cohesion 0.4 --max-fragmentation 0.8
446
- # ↑ ↑ ↓ ↑
447
- # Only depth≥11 Only 30K+ tokens Allows 40%+ cohesion Only 80%+ fragmentation
448
- ```
449
-
450
- **What this means:**
451
-
452
- - `--max-depth 10`: Only flag import depth ≥11 (very deep chains)
453
- - `--max-context 30000`: Only flag files needing 30K+ tokens (only huge files)
454
- - `--min-cohesion 0.4`: Accept 40%+ cohesion (more lenient about mixed concerns)
455
- - `--max-fragmentation 0.8`: Only flag 80%+ scatter (only severely fragmented)
456
-
457
- ### Threshold Parameters Explained
458
-
459
- | Parameter | Default (Auto) | Lower = More Strict | Higher = Less Strict | Impact |
460
- |-----------|---------------|-------------------|---------------------|--------|
461
- | `--max-depth` | 4-10* | Catches shallower imports | Only very deep chains | More splits → flatter structure |
462
- | `--max-context` | 8k-40k* | Catches smaller files | Only huge files | More splits → smaller modules |
463
- | `--min-cohesion` | 0.35-0.5* | Stricter about mixed concerns | More lenient | More splits → focused files |
464
- | `--max-fragmentation` | 0.5-0.8* | Catches less scattered code | Only severely scattered | More consolidation → domain modules |
465
-
466
- \* Auto-adjusted based on your repository size (100 files vs 2000+ files)
467
-
468
- ### Common Tuning Scenarios
469
-
470
- **Small codebase getting too many warnings?**
471
-
472
- ```bash
473
- aiready-context ./src --max-depth 6 --min-cohesion 0.5
474
- # Explanation: Allow slightly deeper imports and more mixed concerns
475
- # Use when: Your codebase is naturally small and warnings feel excessive
476
- ```
477
-
478
- **Large codebase showing too few issues?**
18
+ 🎛️ @aiready/cli (orchestrator)
19
+ │ │ │ │ │ │ │ │
20
+ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼
21
+ [PAT] [CTX] [CON] [AMP] [DEP] [DOC] [SIG] [AGT] [TST]
22
+ │ │ │ │ │ │ │ │ │
23
+ └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘
24
+
25
+
26
+ 🏢 @aiready/core
479
27
 
480
- ```bash
481
- aiready-context ./src --max-depth 5 --max-context 15000
482
- # Explanation: Be stricter about depth and context to catch more problems
483
- # Use when: You know there are issues but they're not being detected
484
- ```
485
-
486
- **Focus on critical issues only:**
487
-
488
- ```bash
489
- aiready-context ./src --max-depth 8 --max-context 25000 --min-cohesion 0.3
490
- # Explanation: Very lenient - only show the worst offenders
491
- # Use when: Fixing warnings in stages, start with critical issues first
492
- ```
493
-
494
- **Preparing for AI refactoring sprint:**
495
-
496
- ```bash
497
- aiready-context ./src --max-depth 4 --max-context 8000 --min-cohesion 0.6 --max-fragmentation 0.5
498
- # Explanation: Strict on all dimensions to get comprehensive issue list
499
- # Use when: Planning a major refactoring effort, need complete audit
500
- ```
501
-
502
- **Microservices architecture (naturally fragmented):**
503
-
504
- ```bash
505
- aiready-context ./src --max-fragmentation 0.9
506
- # Explanation: Very lenient on fragmentation (services are meant to be separate)
507
- # Use when: Analyzing microservices where fragmentation is intentional
508
- ```
509
-
510
- ## 📤 Output Options
511
-
512
- ### Console Output (Default)
513
-
514
- Shows a summary with top 10 results per category:
515
-
516
- ```bash
517
- # Default - shows top 10 items
518
- aiready-context ./src
519
-
520
- # Show more items (e.g., top 25)
521
- aiready-context ./src --max-results 25
522
-
523
- # Show all items (use a large number)
524
- aiready-context ./src --max-results 999
525
- ```
526
-
527
- ### JSON Output
528
-
529
- Get complete details of **all** issues (not limited to 10):
530
-
531
- ```bash
532
- # Generate JSON with all issues
533
- aiready-context ./src --output json
534
-
535
- # Custom output path
536
- aiready-context ./src --output json --output-file reports/analysis.json
537
- ```
538
-
539
- ### HTML Report
540
-
541
- Visual report with charts and detailed breakdown:
542
-
543
- ```bash
544
- # Generate HTML report
545
- aiready-context ./src --output html --output-file report.html
546
- ```
547
-
548
- ### Include/Exclude Patterns
549
-
550
- ```bash
551
- # Include/exclude patterns
552
- aiready-context ./src --exclude "**/test/**,**/*.test.ts"
553
- ```
554
-
555
- ### Configuration
556
-
557
- Create an `aiready.json` or `aiready.config.json` file in your project root:
558
-
559
- ```json
560
- {
561
- "scan": {
562
- "include": ["**/*.{ts,tsx,js,jsx}"],
563
- "exclude": ["**/test/**", "**/*.test.*"]
564
- },
565
- "tools": {
566
- "context-analyzer": {
567
- "maxDepth": 4,
568
- "maxContextBudget": 8000,
569
- "minCohesion": 0.7,
570
- "maxFragmentation": 0.6,
571
- "focus": "all",
572
- "maxResults": 10
573
- }
574
- },
575
- "output": {
576
- "format": "console"
577
- }
578
- }
579
- ```
580
-
581
- **Configuration Options:**
582
-
583
- | Option | Type | Default | Description |
584
- |--------|------|---------|-------------|
585
- | `maxDepth` | number | `5` | Max acceptable import depth |
586
- | `maxContextBudget` | number | `10000` | Max acceptable token budget |
587
- | `minCohesion` | number | `0.6` | Min acceptable cohesion score (0-1) |
588
- | `maxFragmentation` | number | `0.5` | Max acceptable fragmentation (0-1) |
589
- | `focus` | string | `'all'` | Focus: `'fragmentation'`, `'cohesion'`, `'depth'`, `'all'` |
590
- | `maxResults` | number | `10` | Max results per category in console |
591
- | `includeNodeModules` | boolean | `false` | Include node_modules in analysis |
592
-
593
- > **Note:** Domain detection is now fully automatic using semantic analysis (co-usage patterns + type dependencies). No domain configuration needed!
594
-
595
- ### Sample Output
596
-
597
- ```bash
598
- 🔍 Analyzing context window costs...
599
-
600
- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
601
- CONTEXT ANALYSIS SUMMARY
602
- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
603
-
604
- 📁 Files analyzed: 127
605
- 📊 Total tokens: 145,680
606
- 💰 Avg context budget: 1,147 tokens/file
607
- ⏱ Analysis time: 0.52s
608
-
609
- ⚠️ Issues Found:
610
-
611
- 🔴 Critical: 3
612
- 🟡 Major: 12
613
- 🔵 Minor: 8
614
-
615
- 💡 Potential savings: 28,450 tokens
616
-
617
- 📏 Deep Import Chains:
618
-
619
- Average depth: 3.2
620
- Maximum depth: 8
621
-
622
- → src/services/order.ts (depth: 8)
623
- → src/api/payment.ts (depth: 7)
624
- → src/lib/validation.ts (depth: 6)
625
-
626
- 🧩 Fragmented Modules:
627
-
628
- Average fragmentation: 42%
629
-
630
- ● user-management - 8 files, 67% scattered
631
- Token cost: 12,450, Cohesion: 45%
632
- ● order-processing - 12 files, 58% scattered
633
- Token cost: 18,200, Cohesion: 52%
634
-
635
- 🔀 Low Cohesion Files:
636
-
637
- Average cohesion: 68%
638
-
639
- ○ src/utils/helpers.ts (35% cohesion)
640
- ○ src/lib/shared.ts (42% cohesion)
641
-
642
- 💸 Most Expensive Files (Context Budget):
643
-
644
- ● src/services/order.ts - 8,450 tokens
645
- ● src/api/payment.ts - 6,200 tokens
646
- ● src/utils/helpers.ts - 5,100 tokens
647
-
648
- 💡 Top Recommendations:
649
-
650
- 1. src/services/order.ts
651
- • Flatten dependency tree or use facade pattern
652
- • Split file by domain - separate unrelated functionality
653
-
654
- 2. src/utils/helpers.ts
655
- • Very low cohesion (35%) - mixed concerns
656
- • Split file by domain - separate unrelated functionality
657
-
658
- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
659
-
660
- 💎 Roadmap: Historical trends and guided refactoring (planned)
661
- 💼 Roadmap: CI/CD integration and team benchmarks (planned)
662
- ```
663
-
664
- ### Programmatic API
665
-
666
- ```typescript
667
- import { analyzeContext, generateSummary } from '@aiready/context-analyzer';
668
-
669
- // Analyze entire project
670
- const results = await analyzeContext({
671
- rootDir: './src',
672
- maxDepth: 5,
673
- maxContextBudget: 10000,
674
- minCohesion: 0.6,
675
- maxFragmentation: 0.5,
676
- });
677
-
678
- // Generate summary
679
- const summary = generateSummary(results);
680
-
681
- console.log(`Total files: ${summary.totalFiles}`);
682
- console.log(`Total tokens: ${summary.totalTokens}`);
683
- console.log(`Avg context budget: ${summary.avgContextBudget}`);
684
- console.log(`Critical issues: ${summary.criticalIssues}`);
685
-
686
- // Find high-cost files
687
- const expensiveFiles = results.filter(r => r.contextBudget > 5000);
688
- console.log(`Files with >5000 token budgets: ${expensiveFiles.length}`);
689
-
690
- // Find fragmented modules
691
- const fragmented = summary.fragmentedModules.filter(m => m.fragmentationScore > 0.5);
692
- console.log(`Highly fragmented modules: ${fragmented.length}`);
693
-
694
- // Get refactoring recommendations
695
- for (const result of results) {
696
- if (result.severity === 'critical') {
697
- console.log(`${result.file}:`);
698
- result.recommendations.forEach(rec => console.log(` - ${rec}`));
699
- }
700
- }
701
- ```
702
-
703
- ## 📊 Metrics Explained
704
-
705
- ### Import Depth
706
-
707
- **What:** Maximum chain length of transitive dependencies
708
- **Impact:** Deeper chains = more files to load = higher context cost
709
- **Threshold:** >5 is concerning, >8 is critical
710
- **Fix:** Flatten dependency tree, use facade pattern, break circular deps
711
-
712
- ### Context Budget
713
-
714
- **What:** Total tokens AI needs to load to understand this file
715
- **Impact:** Higher budget = more expensive AI assistance
716
- **Threshold:** >10,000 tokens often hits context limits
717
- **Fix:** Split files, reduce dependencies, extract interfaces
718
-
719
- ### Fragmentation Score
720
-
721
- **What:** How scattered related code is across directories (0-100%)
722
- **Impact:** Higher = more files to load for domain understanding
723
- **Threshold:** >50% indicates poor organization
724
- **Fix:** Consolidate related code into cohesive modules
725
-
726
- ### Cohesion Score
727
-
728
- **What:** How related exports are within a file (0-100%)
729
- **Impact:** Lower = mixed concerns = wasted context
730
- **Threshold:** <60% indicates low cohesion
731
- **Fix:** Split by domain, separate unrelated functionality
732
-
733
- ## 🎯 Configuration
734
-
735
- ### CLI Options
736
-
737
- ```bash
738
- --max-depth <number> # Maximum acceptable import depth (default: 5)
739
- --max-context <number> # Maximum acceptable context budget in tokens (default: 10000)
740
- --min-cohesion <number> # Minimum acceptable cohesion score 0-1 (default: 0.6)
741
- --max-fragmentation <number> # Maximum acceptable fragmentation 0-1 (default: 0.5)
742
- --focus <type> # Analysis focus: fragmentation|cohesion|depth|all (default: all)
743
- --include-node-modules # Include node_modules in analysis (default: false)
744
- --include <patterns> # File patterns to include (comma-separated)
745
- --exclude <patterns> # File patterns to exclude (comma-separated)
746
- -o, --output <format> # Output format: console|json|html (default: console)
747
- --output-file <path> # Output file path (for json/html)
28
+ Legend:
29
+ PAT = pattern-detect CTX = context-analyzer
30
+ CON = consistency AMP = change-amplification
31
+ DEP = deps-health DOC = doc-drift
32
+ SIG = ai-signal-clarity AGT = agent-grounding
33
+ TST = testability ★ = YOU ARE HERE
748
34
  ```
749
35
 
750
- ### Default Exclusions
36
+ ## Features
751
37
 
752
- By default, these patterns are excluded (unless `--include-node-modules` is used):
38
+ - **Import Chain Analysis**: Detects deep dependency trees that force unnecessary files into AI context.
39
+ - **Fragmentation detection**: Identifies modules that are split across too many small, non-semantic files.
40
+ - **Context Budgeting**: Projects the dollar cost of loading specific modules into frontier models (GPT-4, Claude 3.5).
753
41
 
754
- ```bash
755
- # Dependencies (excluded by default, override with --include-node-modules)
756
- **/node_modules/**
757
-
758
- # Build outputs
759
- **/dist/**, **/build/**, **/out/**, **/output/**, **/target/**, **/bin/**, **/obj/**, **/cdk.out/**
760
-
761
- # Framework-specific build dirs
762
- **/.next/**, **/.nuxt/**, **/.vuepress/**, **/.cache/**, **/.turbo/**
763
-
764
- # Test and coverage
765
- **/coverage/**, **/.nyc_output/**, **/.jest/**
766
-
767
- # Version control and IDE
768
- **/.git/**, **/.svn/**, **/.hg/**, **/.vscode/**, **/.idea/**, **/*.swp, **/*.swo
769
-
770
- # Build artifacts and minified files
771
- **/*.min.js, **/*.min.css, **/*.bundle.js, **/*.tsbuildinfo
772
-
773
- # Logs and temporary files
774
- **/logs/**, **/*.log, **/.DS_Store
775
- ```
776
-
777
- ### API Options
778
-
779
- ```typescript
780
- interface ContextAnalyzerOptions {
781
- rootDir: string; // Root directory to analyze
782
- maxDepth?: number; // Maximum acceptable import depth (default: 5)
783
- maxContextBudget?: number; // Maximum acceptable token budget (default: 10000)
784
- minCohesion?: number; // Minimum acceptable cohesion score (default: 0.6)
785
- maxFragmentation?: number; // Maximum acceptable fragmentation (default: 0.5)
786
- focus?: 'fragmentation' | 'cohesion' | 'depth' | 'all'; // Analysis focus (default: 'all')
787
- includeNodeModules?: boolean; // Include node_modules (default: false)
788
- include?: string[]; // File patterns to include
789
- exclude?: string[]; // File patterns to exclude
790
- }
791
- ```
792
-
793
- ## 🔬 How It Works
794
-
795
- ### 1. Dependency Graph Builder
796
-
797
- Parses imports and exports to build a complete dependency graph of your codebase.
798
-
799
- ### 2. Depth Calculator
800
-
801
- Calculates maximum import chain depth using graph traversal, identifying circular dependencies.
802
-
803
- ### 3. Semantic Domain Detection
804
-
805
- Uses **co-usage patterns** (files imported together) and **type dependencies** (shared types) to automatically identify semantic domains. No configuration needed - the tool discovers relationships from actual code usage.
806
-
807
- ### 4. Fragmentation Detector
808
-
809
- Groups files by semantic domain and calculates how scattered they are across directories.
810
-
811
- ### 5. Cohesion Analyzer
812
-
813
- Uses entropy to measure how related exports are within each file (low entropy = high cohesion).
814
-
815
- ### 6. Context Budget Calculator
816
-
817
- Sums tokens across entire dependency tree to estimate AI context cost for each file.
818
-
819
- ## 🎨 Output Formats
820
-
821
- ### Console (Default)
822
-
823
- Rich formatted output with colors, emojis, and actionable recommendations.
824
-
825
- ### JSON
826
-
827
- Machine-readable output for CI/CD integration:
828
-
829
- ```json
830
- {
831
- "summary": {
832
- "totalFiles": 127,
833
- "totalTokens": 145680,
834
- "avgContextBudget": 1147,
835
- "criticalIssues": 3,
836
- "majorIssues": 12,
837
- "totalPotentialSavings": 28450
838
- },
839
- "results": [
840
- {
841
- "file": "src/services/order.ts",
842
- "tokenCost": 2100,
843
- "importDepth": 8,
844
- "contextBudget": 8450,
845
- "severity": "critical",
846
- "recommendations": ["Flatten dependency tree"]
847
- }
848
- ]
849
- }
850
- ```
851
-
852
- ### HTML
853
-
854
- Shareable report with tables and visualizations. Perfect for stakeholders:
855
-
856
- ```bash
857
- aiready-context ./src --output html --output-file context-report.html
858
- ```
859
-
860
- ## 🧭 Interactive Mode
861
-
862
- For first-time users, enable interactive guidance to apply smart defaults and focus areas:
863
-
864
- ```bash
865
- # Suggest excludes for common frameworks (Next.js, AWS CDK) and choose focus
866
- aiready-context ./src --interactive
867
- ```
868
-
869
- Interactive mode:
870
-
871
- - Detects frameworks and recommends excludes (e.g., .next, cdk.out)
872
- - Lets you choose focus areas: frontend, backend, or both
873
- - Applies configuration without modifying your files
874
-
875
- ## 🔗 Integration
876
-
877
- ### CI/CD
878
-
879
- ```yaml
880
- # .github/workflows/code-quality.yml
881
- - name: Analyze context costs
882
- run: npx @aiready/context-analyzer ./src --output json --output-file context-report.json
883
-
884
- - name: Check critical issues
885
- run: |
886
- CRITICAL=$(jq '.summary.criticalIssues' context-report.json)
887
- if [ $CRITICAL -gt 0 ]; then
888
- echo "❌ $CRITICAL critical context issues found"
889
- exit 1
890
- fi
891
- ```
892
-
893
- ### Pre-commit Hook
894
-
895
- ```bash
896
- #!/bin/sh
897
- # .git/hooks/pre-commit
898
- npx @aiready/context-analyzer ./src --max-depth 6 --max-context 8000 --output json > /tmp/context.json
899
- CRITICAL=$(jq '.summary.criticalIssues' /tmp/context.json)
900
- if [ $CRITICAL -gt 0 ]; then
901
- echo "❌ Critical context issues detected. Fix before committing."
902
- exit 1
903
- fi
904
- ```
905
-
906
- ### With Other Tools
42
+ ## Installation
907
43
 
908
44
  ```bash
909
- # Run all quality checks
910
- npm run lint # ESLint for code quality
911
- npm run type-check # TypeScript for type safety
912
- dependency-cruiser src # Architecture rules
913
- aiready-context src # AI context optimization
914
- aiready-patterns src # Duplicate pattern detection
45
+ pnpm add @aiready/context-analyzer
915
46
  ```
916
47
 
917
- ## 💰 SaaS Features (Coming Soon)
918
-
919
- ### Free Tier (CLI)
920
-
921
- ✅ One-time snapshot analysis
922
- ✅ All metrics and recommendations
923
- ✅ JSON/HTML export
924
-
925
- ## 🚧 Project Status
926
-
927
- The SaaS and hosted features are not live yet. Today, this package ships as a CLI/tool-only module focused on local analysis. Future SaaS features will include:
928
-
929
- - Historical trend tracking and team benchmarks
930
- - Automated refactoring plans
931
- - CI/CD integration and export APIs
932
-
933
- Follow progress in the monorepo and release notes.
934
-
935
- ## 🤝 Contributing
936
-
937
- Contributions welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
938
-
939
- ### Development Setup
48
+ ## Usage
940
49
 
941
50
  ```bash
942
- git clone https://github.com/caopengau/aiready-context-analyzer.git
943
- cd aiready-context-analyzer
944
- pnpm install
945
-
946
- # Build
947
- pnpm build
948
-
949
- # Run tests
950
- pnpm test
951
-
952
- # Dev mode (watch)
953
- pnpm --filter @aiready/context-analyzer dev
51
+ aiready scan . --tools context-analyzer
954
52
  ```
955
53
 
956
- ## 📝 License
957
-
958
- MIT © AIReady Team
959
-
960
- ## 🔗 Related Tools
961
-
962
- - **[@aiready/pattern-detect](https://www.npmjs.com/package/@aiready/pattern-detect)** - Find semantic duplicate patterns
963
- - **@aiready/doc-drift** - Detect stale documentation (coming soon)
964
- - **[@aiready/consistency](https://www.npmjs.com/package/@aiready/consistency)** - Check naming consistency
965
-
966
- ---
967
-
968
- ## 🌐 Visit Our Website
969
-
970
- **Try AIReady tools online and optimize your AI context:** [getaiready.dev](https://getaiready.dev)
971
-
972
- **Made with ❤️ for AI-assisted development**
54
+ ## License
973
55
 
974
- *Stop wasting context tokens on fragmented code.*
56
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiready/context-analyzer",
3
- "version": "0.9.33",
3
+ "version": "0.9.35",
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",
@@ -49,7 +49,7 @@
49
49
  "commander": "^14.0.0",
50
50
  "chalk": "^5.3.0",
51
51
  "prompts": "^2.4.2",
52
- "@aiready/core": "0.9.30"
52
+ "@aiready/core": "0.9.32"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@types/node": "^24.0.0",