@aiready/context-analyzer 0.9.20 → 0.9.23

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.20 build /Users/pengcao/projects/aiready/packages/context-analyzer
3
+ > @aiready/context-analyzer@0.9.23 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,18 +9,18 @@
9
9
  CLI Target: es2020
10
10
  CJS Build start
11
11
  ESM Build start
12
- CJS dist/cli.js 62.68 KB
13
- CJS dist/index.js 50.93 KB
14
- CJS ⚡️ Build success in 83ms
12
+ CJS dist/index.js 55.45 KB
13
+ CJS dist/cli.js 67.03 KB
14
+ CJS ⚡️ Build success in 162ms
15
15
  ESM dist/cli.mjs 18.53 KB
16
- ESM dist/chunk-VTALAPQZ.mjs 42.65 KB
17
- ESM dist/chunk-Y6FXYEAI.mjs 390.00 B
16
+ ESM dist/chunk-MBE4AQP5.mjs 47.02 KB
18
17
  ESM dist/python-context-UOPTQH44.mjs 5.64 KB
19
- ESM dist/index.mjs 585.00 B
20
- ESM ⚡️ Build success in 84ms
18
+ ESM dist/chunk-Y6FXYEAI.mjs 390.00 B
19
+ ESM dist/index.mjs 697.00 B
20
+ ESM ⚡️ Build success in 162ms
21
21
  DTS Build start
22
- DTS ⚡️ Build success in 8122ms
22
+ DTS ⚡️ Build success in 7868ms
23
23
  DTS dist/cli.d.ts 20.00 B
24
- DTS dist/index.d.ts 6.03 KB
24
+ DTS dist/index.d.ts 7.37 KB
25
25
  DTS dist/cli.d.mts 20.00 B
26
- DTS dist/index.d.mts 6.03 KB
26
+ DTS dist/index.d.mts 7.37 KB
@@ -1,31 +1,35 @@
1
1
 
2
2
  
3
- > @aiready/context-analyzer@0.9.20 test /Users/pengcao/projects/aiready/packages/context-analyzer
3
+ > @aiready/context-analyzer@0.9.23 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
- ✓ src/__tests__/scoring.test.ts (7 tests) 5ms
10
- ✓ dist/__tests__/scoring.test.js (7 tests) 10ms
11
- ✓ src/__tests__/structural-cohesion.test.ts (4 tests) 6ms
12
- ✓ src/__tests__/fragmentation-coupling.test.ts (2 tests) 161ms
13
- ✓ src/__tests__/analyzer.test.ts (14 tests) 134ms
14
- ✓ dist/__tests__/enhanced-cohesion.test.js (6 tests) 66ms
9
+ ✓ dist/__tests__/scoring.test.js (7 tests) 27ms
10
+ ✓ src/__tests__/scoring.test.ts (7 tests) 11ms
11
+ ✓ src/__tests__/auto-detection.test.ts (8 tests) 612ms
12
+ ✓ should auto-detect domain keywords from folder paths  315ms
13
+ ✓ src/__tests__/file-classification.test.ts (17 tests) 242ms
14
+ ✓ src/__tests__/analyzer.test.ts (14 tests) 491ms
15
+ ✓ should build a basic dependency graph  379ms
16
+ ✓ dist/__tests__/fragmentation-coupling.test.js (2 tests) 38ms
17
+ ✓ src/__tests__/fragmentation-coupling.test.ts (2 tests) 271ms
18
+ ✓ dist/__tests__/fragmentation-log.test.js (3 tests) 6ms
19
+ ✓ dist/__tests__/analyzer.test.js (14 tests) 542ms
20
+ ✓ should calculate import depth correctly  314ms
21
+ ✓ dist/__tests__/auto-detection.test.js (8 tests) 536ms
22
+ ✓ src/__tests__/fragmentation-advanced.test.ts (3 tests) 111ms
23
+ ✓ dist/__tests__/enhanced-cohesion.test.js (6 tests) 24ms
15
24
  ✓ dist/__tests__/structural-cohesion.test.js (4 tests) 1ms
16
- ✓ dist/__tests__/fragmentation-coupling.test.js (2 tests) 202ms
17
- ✓ dist/__tests__/fragmentation-log.test.js (3 tests) 4ms
18
- ✓ src/__tests__/auto-detection.test.ts (8 tests) 205ms
19
- ✓ dist/__tests__/analyzer.test.js (14 tests) 610ms
20
- ✓ dist/__tests__/auto-detection.test.js (8 tests) 317ms
21
- ✓ src/__tests__/fragmentation-advanced.test.ts (3 tests) 2ms
22
- ✓ dist/__tests__/fragmentation-advanced.test.js (3 tests) 8ms
23
- ✓ src/__tests__/enhanced-cohesion.test.ts (6 tests) 4ms
24
- ✓ src/__tests__/fragmentation-log.test.ts (3 tests) 47ms
25
+ ✓ src/__tests__/enhanced-cohesion.test.ts (6 tests) 31ms
26
+ ✓ src/__tests__/fragmentation-log.test.ts (3 tests) 4ms
27
+ ✓ src/__tests__/structural-cohesion.test.ts (4 tests) 240ms
28
+ ✓ dist/__tests__/fragmentation-advanced.test.js (3 tests) 6ms
25
29
 
26
-  Test Files  16 passed (16)
27
-  Tests  94 passed (94)
28
-  Start at  15:34:38
29
-  Duration  10.60s (transform 21.01s, setup 0ms, import 58.32s, tests 1.78s, environment 37ms)
30
+  Test Files  17 passed (17)
31
+  Tests  111 passed (111)
32
+  Start at  11:30:31
33
+  Duration  10.28s (transform 3.68s, setup 0ms, import 60.43s, tests 3.19s, environment 12ms)
30
34
 
31
35
  [?25h
package/README.md CHANGED
@@ -7,31 +7,48 @@ When AI tools try to help with your code, they need to load files into their con
7
7
  ## 🏛️ Architecture
8
8
 
9
9
  ```
10
- 🎯 USER
11
-
12
-
13
- 🎛️ CLI (orchestrator)
14
-
15
-
16
- 🏢 HUB (core)
17
-
18
- ┌──────────────────────┼───────────┬───────────┐
19
- ▼ ▼ ▼ ▼
20
- 📊 PATTERN ┌─────────────┐ 🔧 CONSIST 📚 DOC
21
- DETECT 📦 CONTEXT ⬅ YOU ARE HERE
22
- ✅ Ready │ ANALYZER │ ENCY DRIFT
23
- │ ✅ Ready ✅ Ready 🔜 Soon
24
- └─────────────┘
10
+ 🎯 USER
11
+
12
+
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)
25
40
  ```
26
41
 
27
42
  ## 🌍 Language Support
28
43
 
29
44
  **Currently Supported (64% market coverage):**
45
+
30
46
  - ✅ **TypeScript** (`.ts`, `.tsx`) - Import chains, context budget, cohesion
31
47
  - ✅ **JavaScript** (`.js`, `.jsx`) - Import chains, context budget, cohesion
32
48
  - ✅ **Python** (`.py`) - Import chains, context budget, circular deps, cohesion
33
49
 
34
50
  **Roadmap:**
51
+
35
52
  - 🔜 **Java** (Q3 2026) - Package dependencies, Maven/Gradle analysis
36
53
  - 🔜 **Go** (Q4 2026) - Module dependencies, package cohesion
37
54
  - 🔜 **Rust** (Q4 2026) - Crate dependencies, module structure
@@ -65,11 +82,13 @@ aiready-context ./src
65
82
  ### 🎯 Input & Output
66
83
 
67
84
  **Input:** Path to your source code directory
85
+
68
86
  ```bash
69
87
  aiready-context ./src
70
88
  ```
71
89
 
72
90
  **Output:** Terminal report + optional JSON file (saved to `.aiready/` directory)
91
+
73
92
  ```
74
93
  📊 Context Analysis Results
75
94
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━
@@ -130,11 +149,13 @@ Result: AI sees everything, gives complete answers ✅
130
149
  | Recommendations | Generic | Rule-based | AI context optimization |
131
150
 
132
151
  **Recommended Workflow:**
152
+
133
153
  - Use **dependency-cruiser** to enforce architecture rules (blocking)
134
154
  - Use **@aiready/context-analyzer** to optimize for AI tools (advisory)
135
155
  - Track improvements over time with SaaS tier
136
156
 
137
157
  **Related AIReady Tools:**
158
+
138
159
  - [**@aiready/cli**](https://www.npmjs.com/package/@aiready/cli) - Unified CLI with all analysis tools
139
160
  - [**@aiready/pattern-detect**](https://www.npmjs.com/package/@aiready/pattern-detect) - Semantic duplicate detection
140
161
  - [**@aiready/consistency**](https://www.npmjs.com/package/@aiready/consistency) - Consistency checking
@@ -148,11 +169,13 @@ This tool measures four key dimensions that affect how much context AI tools nee
148
169
  **What it measures:** Total AI tokens needed to understand a file (file content + all dependencies)
149
170
 
150
171
  **Why it matters:** AI tools have limited context windows (e.g., 128K tokens). Large context budgets mean:
172
+
151
173
  - AI needs to load more files to understand your code
152
174
  - Risk of hitting context limits → incomplete/wrong answers
153
175
  - Slower AI responses (more processing time)
154
176
 
155
177
  **Example:**
178
+
156
179
  ```typescript
157
180
  // High context budget (15,000 tokens)
158
181
  import { A, B, C } from './deeply/nested/utils' // +5,000 tokens
@@ -174,12 +197,15 @@ import { X, Y, Z } from './another/chain' // +8,000 tokens
174
197
  **What it measures:** How many layers deep your import chains go
175
198
 
176
199
  **Why it matters:** Deep import chains create cascading context loads:
200
+
177
201
  ```
178
202
  app.ts → service.ts → helper.ts → util.ts → core.ts → base.ts
179
203
  ```
204
+
180
205
  AI must load all 6 files just to understand app.ts!
181
206
 
182
207
  **Example:**
208
+
183
209
  ```typescript
184
210
  // Deep chain (depth 8) = AI loads 8+ files
185
211
  import { validate } from '../../../utils/validators/user/schema'
@@ -197,10 +223,12 @@ import { validate } from './validators'
197
223
  **What it measures:** How related the exports in a file are to each other
198
224
 
199
225
  **How it's calculated:** Uses Shannon entropy of inferred domains
226
+
200
227
  - 1.0 = Perfect cohesion (all exports are related)
201
228
  - 0.0 = Zero cohesion (completely unrelated exports)
202
229
 
203
230
  **Why it matters:** Low cohesion = "God object" pattern = AI confusion
231
+
204
232
  ```typescript
205
233
  // Low cohesion (0.3) - mixing unrelated concerns
206
234
  export function validateUser() { } // User domain
@@ -226,10 +254,12 @@ export interface User { }
226
254
  **What it measures:** How scattered a domain/concept is across different directories
227
255
 
228
256
  **How it's calculated:** `(unique directories - 1) / (total files - 1)`
257
+
229
258
  - 0.0 = No fragmentation (all files in same directory)
230
259
  - 1.0 = Maximum fragmentation (each file in different directory)
231
260
 
232
261
  **Why it matters:** Scattered domains force AI to load many unrelated paths
262
+
233
263
  ```typescript
234
264
  // High fragmentation (0.8) - User domain scattered
235
265
  src/api/user-routes.ts // 800 tokens
@@ -260,6 +290,57 @@ These signals are included in the JSON summary (`fragmentedModules` entries) so
260
290
 
261
291
  ---
262
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
+
263
344
  ### ⚖️ The Tradeoff: Splitting vs. Consolidating
264
345
 
265
346
  **Important:** These metrics can pull in opposite directions!
@@ -270,6 +351,7 @@ These signals are included in the JSON summary (`fragmentedModules` entries) so
270
351
  | **Consolidate scattered files** | ⚠️ May increase | ✅ Reduces | ⚠️ May decrease |
271
352
 
272
353
  **Best Practice:** Optimize for your use case:
354
+
273
355
  - **Large files with mixed concerns** → Split by domain (improves cohesion + reduces budget)
274
356
  - **Scattered single-domain files** → Consolidate (reduces fragmentation)
275
357
  - **Large files with high cohesion** → May be OK if under context budget threshold
@@ -350,6 +432,7 @@ aiready-context ./src --max-depth 3 --max-context 5000 --min-cohesion 0.7 --max-
350
432
  ```
351
433
 
352
434
  **What this means:**
435
+
353
436
  - `--max-depth 3`: Flag files with import depth ≥4 (stricter than default 5-7)
354
437
  - `--max-context 5000`: Flag files needing 5K+ tokens (catches smaller files)
355
438
  - `--min-cohesion 0.7`: Require 70%+ cohesion (stricter about mixed concerns)
@@ -365,6 +448,7 @@ aiready-context ./src --max-depth 10 --max-context 30000 --min-cohesion 0.4 --ma
365
448
  ```
366
449
 
367
450
  **What this means:**
451
+
368
452
  - `--max-depth 10`: Only flag import depth ≥11 (very deep chains)
369
453
  - `--max-context 30000`: Only flag files needing 30K+ tokens (only huge files)
370
454
  - `--min-cohesion 0.4`: Accept 40%+ cohesion (more lenient about mixed concerns)
@@ -384,6 +468,7 @@ aiready-context ./src --max-depth 10 --max-context 30000 --min-cohesion 0.4 --ma
384
468
  ### Common Tuning Scenarios
385
469
 
386
470
  **Small codebase getting too many warnings?**
471
+
387
472
  ```bash
388
473
  aiready-context ./src --max-depth 6 --min-cohesion 0.5
389
474
  # Explanation: Allow slightly deeper imports and more mixed concerns
@@ -391,6 +476,7 @@ aiready-context ./src --max-depth 6 --min-cohesion 0.5
391
476
  ```
392
477
 
393
478
  **Large codebase showing too few issues?**
479
+
394
480
  ```bash
395
481
  aiready-context ./src --max-depth 5 --max-context 15000
396
482
  # Explanation: Be stricter about depth and context to catch more problems
@@ -398,6 +484,7 @@ aiready-context ./src --max-depth 5 --max-context 15000
398
484
  ```
399
485
 
400
486
  **Focus on critical issues only:**
487
+
401
488
  ```bash
402
489
  aiready-context ./src --max-depth 8 --max-context 25000 --min-cohesion 0.3
403
490
  # Explanation: Very lenient - only show the worst offenders
@@ -405,6 +492,7 @@ aiready-context ./src --max-depth 8 --max-context 25000 --min-cohesion 0.3
405
492
  ```
406
493
 
407
494
  **Preparing for AI refactoring sprint:**
495
+
408
496
  ```bash
409
497
  aiready-context ./src --max-depth 4 --max-context 8000 --min-cohesion 0.6 --max-fragmentation 0.5
410
498
  # Explanation: Strict on all dimensions to get comprehensive issue list
@@ -412,6 +500,7 @@ aiready-context ./src --max-depth 4 --max-context 8000 --min-cohesion 0.6 --max-
412
500
  ```
413
501
 
414
502
  **Microservices architecture (naturally fragmented):**
503
+
415
504
  ```bash
416
505
  aiready-context ./src --max-fragmentation 0.9
417
506
  # Explanation: Very lenient on fragmentation (services are meant to be separate)
@@ -614,24 +703,28 @@ for (const result of results) {
614
703
  ## 📊 Metrics Explained
615
704
 
616
705
  ### Import Depth
706
+
617
707
  **What:** Maximum chain length of transitive dependencies
618
708
  **Impact:** Deeper chains = more files to load = higher context cost
619
709
  **Threshold:** >5 is concerning, >8 is critical
620
710
  **Fix:** Flatten dependency tree, use facade pattern, break circular deps
621
711
 
622
712
  ### Context Budget
713
+
623
714
  **What:** Total tokens AI needs to load to understand this file
624
715
  **Impact:** Higher budget = more expensive AI assistance
625
716
  **Threshold:** >10,000 tokens often hits context limits
626
717
  **Fix:** Split files, reduce dependencies, extract interfaces
627
718
 
628
719
  ### Fragmentation Score
720
+
629
721
  **What:** How scattered related code is across directories (0-100%)
630
722
  **Impact:** Higher = more files to load for domain understanding
631
723
  **Threshold:** >50% indicates poor organization
632
724
  **Fix:** Consolidate related code into cohesive modules
633
725
 
634
726
  ### Cohesion Score
727
+
635
728
  **What:** How related exports are within a file (0-100%)
636
729
  **Impact:** Lower = mixed concerns = wasted context
637
730
  **Threshold:** <60% indicates low cohesion
@@ -657,6 +750,7 @@ for (const result of results) {
657
750
  ### Default Exclusions
658
751
 
659
752
  By default, these patterns are excluded (unless `--include-node-modules` is used):
753
+
660
754
  ```bash
661
755
  # Dependencies (excluded by default, override with --include-node-modules)
662
756
  **/node_modules/**
@@ -699,29 +793,37 @@ interface ContextAnalyzerOptions {
699
793
  ## 🔬 How It Works
700
794
 
701
795
  ### 1. Dependency Graph Builder
796
+
702
797
  Parses imports and exports to build a complete dependency graph of your codebase.
703
798
 
704
799
  ### 2. Depth Calculator
800
+
705
801
  Calculates maximum import chain depth using graph traversal, identifying circular dependencies.
706
802
 
707
803
  ### 3. Semantic Domain Detection
804
+
708
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.
709
806
 
710
807
  ### 4. Fragmentation Detector
808
+
711
809
  Groups files by semantic domain and calculates how scattered they are across directories.
712
810
 
713
811
  ### 5. Cohesion Analyzer
812
+
714
813
  Uses entropy to measure how related exports are within each file (low entropy = high cohesion).
715
814
 
716
815
  ### 6. Context Budget Calculator
816
+
717
817
  Sums tokens across entire dependency tree to estimate AI context cost for each file.
718
818
 
719
819
  ## 🎨 Output Formats
720
820
 
721
821
  ### Console (Default)
822
+
722
823
  Rich formatted output with colors, emojis, and actionable recommendations.
723
824
 
724
825
  ### JSON
826
+
725
827
  Machine-readable output for CI/CD integration:
726
828
 
727
829
  ```json
@@ -748,6 +850,7 @@ Machine-readable output for CI/CD integration:
748
850
  ```
749
851
 
750
852
  ### HTML
853
+
751
854
  Shareable report with tables and visualizations. Perfect for stakeholders:
752
855
 
753
856
  ```bash
@@ -764,6 +867,7 @@ aiready-context ./src --interactive
764
867
  ```
765
868
 
766
869
  Interactive mode:
870
+
767
871
  - Detects frameworks and recommends excludes (e.g., .next, cdk.out)
768
872
  - Lets you choose focus areas: frontend, backend, or both
769
873
  - Applies configuration without modifying your files
@@ -813,6 +917,7 @@ aiready-patterns src # Duplicate pattern detection
813
917
  ## 💰 SaaS Features (Coming Soon)
814
918
 
815
919
  ### Free Tier (CLI)
920
+
816
921
  ✅ One-time snapshot analysis
817
922
  ✅ All metrics and recommendations
818
923
  ✅ JSON/HTML export