@aiready/context-analyzer 0.5.0 → 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.5.0 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 42ms
15
- ESM dist/cli.mjs 18.45 KB
13
+ CJS dist/cli.js 39.27 KB
14
+ CJS ⚡️ Build success in 48ms
16
15
  ESM dist/index.mjs 164.00 B
16
+ ESM dist/cli.mjs 18.45 KB
17
17
  ESM dist/chunk-NJUW6VED.mjs 19.48 KB
18
- ESM ⚡️ Build success in 42ms
18
+ ESM ⚡️ Build success in 48ms
19
19
  DTS Build start
20
- DTS ⚡️ Build success in 551ms
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,33 +1,12 @@
1
1
 
2
2
  
3
- > @aiready/context-analyzer@0.5.0 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
 
7
7
   RUN  v2.1.9 /Users/pengcao/projects/aiready/packages/context-analyzer
8
8
 
9
- [?25l · src/__tests__/analyzer.test.ts (13)
10
- · buildDependencyGraph (1)
11
- · should build a basic dependency graph
12
- · calculateImportDepth (2)
13
- · should calculate import depth correctly
14
- · should handle circular dependencies gracefully
15
- · getTransitiveDependencies (1)
16
- · should get all transitive dependencies
17
- · calculateContextBudget (1)
18
- · should calculate total token cost including dependencies
19
- · detectCircularDependencies (2)
20
- · should detect circular dependencies
21
- · should return empty for no circular dependencies
22
- · calculateCohesion (3)
23
- · should return 1 for single export
24
- · should return high cohesion for related exports
25
- · should return low cohesion for mixed exports
26
- · calculateFragmentation (3)
27
- · should return 0 for single file
28
- · should return 0 for files in same directory
29
- · should return high fragmentation for scattered files
30
- [?25l ✓ src/__tests__/analyzer.test.ts (13)
9
+ ✓ src/__tests__/analyzer.test.ts (13)
31
10
  ✓ buildDependencyGraph (1)
32
11
  ✓ should build a basic dependency graph
33
12
  ✓ calculateImportDepth (2)
@@ -51,7 +30,7 @@
51
30
 
52
31
   Test Files  1 passed (1)
53
32
   Tests  13 passed (13)
54
-  Start at  07:46:59
55
-  Duration  394ms (transform 66ms, setup 0ms, collect 75ms, tests 41ms, environment 0ms, prepare 42ms)
33
+  Start at  08:00:54
34
+  Duration  315ms (transform 75ms, setup 0ms, collect 83ms, tests 3ms, environment 0ms, prepare 44ms)
56
35
 
57
- [?25h[?25h
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
@@ -93,6 +239,13 @@ aiready-context ./src --output json --output-file custom-report.json
93
239
 
94
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.
95
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
+
96
249
  ### Getting More/Fewer Results
97
250
 
98
251
  **Want to catch MORE potential issues?** (More sensitive, shows smaller problems)
@@ -100,23 +253,39 @@ aiready-context ./src --output json --output-file custom-report.json
100
253
  ```bash
101
254
  # Lower thresholds to be more strict:
102
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
103
258
  ```
104
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
+
105
266
  **Want to see FEWER issues?** (Less noise, focus on critical problems only)
106
267
 
107
268
  ```bash
108
269
  # Raise thresholds to be more lenient:
109
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
110
273
  ```
111
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
+
112
281
  ### Threshold Parameters Explained
113
282
 
114
- | Parameter | Default (Auto) | Lower = More Strict | Higher = Less Strict |
115
- |-----------|---------------|-------------------|---------------------|
116
- | `--max-depth` | 4-10* | Catches shallower imports | Only very deep chains |
117
- | `--max-context` | 8k-40k* | Catches smaller files | Only huge files |
118
- | `--min-cohesion` | 0.35-0.5* | Stricter about mixed concerns | More lenient |
119
- | `--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 |
120
289
 
121
290
  \* Auto-adjusted based on your repository size (100 files vs 2000+ files)
122
291
 
@@ -125,16 +294,36 @@ aiready-context ./src --max-depth 10 --max-context 30000 --min-cohesion 0.4 --ma
125
294
  **Small codebase getting too many warnings?**
126
295
  ```bash
127
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
128
299
  ```
129
300
 
130
301
  **Large codebase showing too few issues?**
131
302
  ```bash
132
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
133
306
  ```
134
307
 
135
308
  **Focus on critical issues only:**
136
309
  ```bash
137
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
320
+ ```
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
138
327
  ```
139
328
 
140
329
  ## 📤 Output Options
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiready/context-analyzer",
3
- "version": "0.5.0",
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",