@sun-asterisk/impact-analyzer 1.0.3 → 1.0.5

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.
@@ -0,0 +1,284 @@
1
+ # Task 001: Refactor API Detector for Performance & Simplicity
2
+
3
+ **Status:** 📋 TODO
4
+ **Priority:** 🔴 HIGH
5
+ **Assignee:** AI Agent
6
+ **Created:** 2025-12-25
7
+
8
+ ---
9
+
10
+ ## 📝 Overview
11
+
12
+ Refactor API detector to use `core/` folder structure, optimize performance, remove unused code, and improve logging with verbose mode.
13
+
14
+ ## 🎯 Goals
15
+
16
+ 1. **Migrate from `modules/` to `core/`** - Use consistent folder structure
17
+ 2. **Optimize Performance** - Remove unused functions, improve call graph traversal
18
+ 3. **Simplify Documentation** - Clear docs for AI code generation
19
+ 4. **Improve Logging** - Add verbose mode with clear, contextual logs
20
+
21
+ ## 📋 Tasks
22
+
23
+ ### 1. Folder Structure Migration
24
+
25
+ **Current (if using modules):**
26
+ ```
27
+ modules/
28
+ ├── api-detector.js
29
+ ├── call-graph-builder.js
30
+ └── utils.js
31
+ ```
32
+
33
+ **Target:**
34
+ ```
35
+ core/
36
+ ├── detectors/
37
+ │ └── endpoint-detector.js # API impact detection
38
+ └── utils/
39
+ ├── method-call-graph.js # Call graph building
40
+ └── ast-parser.js # AST parsing utilities
41
+ ```
42
+
43
+ **Action Items:**
44
+ - [ ] Move API detector logic to `core/detectors/endpoint-detector.js`
45
+ - [ ] Move call graph logic to `core/utils/method-call-graph.js`
46
+ - [ ] Update all imports to reference new paths
47
+ - [ ] Remove old `modules/` folder references
48
+
49
+ ### 2. Performance Optimization
50
+
51
+ **Remove Unused Functions:**
52
+ - [ ] Audit all exported functions - remove if not called
53
+ - [ ] Remove debug functions not used in production
54
+ - [ ] Remove duplicate utility functions
55
+ - [ ] Consolidate similar AST traversal logic
56
+
57
+ **Optimize Call Graph Traversal:**
58
+ - [ ] Use Set for visited nodes (avoid array lookups)
59
+ - [ ] Add max depth limit (default: 10 levels)
60
+ - [ ] Early return when endpoint found
61
+ - [ ] Cache decorator lookups per method
62
+
63
+ **Example Optimization:**
64
+ ```javascript
65
+ // Before: Array lookup O(n)
66
+ if (visited.includes(method)) return;
67
+
68
+ // After: Set lookup O(1)
69
+ if (visited.has(method)) return;
70
+ ```
71
+
72
+ **Optimize AST Parsing:**
73
+ - [ ] Parse files only once, cache results
74
+ - [ ] Skip node_modules and test files
75
+ - [ ] Use ts-morph's built-in filters
76
+
77
+ ### 3. Code Simplification
78
+
79
+ **Single Responsibility:**
80
+ - [ ] `endpoint-detector.js` - Only finds affected endpoints
81
+ - [ ] `method-call-graph.js` - Only builds call graphs
82
+ - [ ] `ast-parser.js` - Only parses AST and extracts symbols
83
+
84
+ **Remove Complex Logic:**
85
+ - [ ] Remove async command/queue detection (move to separate detector if needed)
86
+ - [ ] Focus on synchronous flow: Service → Controller → Endpoint
87
+ - [ ] Remove SNS/SQS specific logic (too specific)
88
+
89
+ **Simplified Algorithm:**
90
+ ```javascript
91
+ // core/detectors/endpoint-detector.js
92
+ export function findAffectedEndpoints(changedMethods, callGraph) {
93
+ const endpoints = [];
94
+
95
+ for (const method of changedMethods) {
96
+ const found = traverseToEndpoint(method, callGraph);
97
+ endpoints.push(...found);
98
+ }
99
+
100
+ return endpoints;
101
+ }
102
+
103
+ function traverseToEndpoint(method, callGraph, visited = new Set(), depth = 0) {
104
+ if (depth > 10 || visited.has(method)) return [];
105
+ visited.add(method);
106
+
107
+ // Check if endpoint
108
+ if (isHttpEndpoint(method)) {
109
+ return [extractEndpointInfo(method)];
110
+ }
111
+
112
+ // Traverse callers
113
+ const endpoints = [];
114
+ const callers = callGraph.getCallers(method);
115
+
116
+ for (const caller of callers) {
117
+ const found = traverseToEndpoint(caller, callGraph, visited, depth + 1);
118
+ endpoints.push(...found);
119
+ }
120
+
121
+ return endpoints;
122
+ }
123
+ ```
124
+
125
+ ### 4. Logging with Verbose Mode
126
+
127
+ **Add Verbose Flag:**
128
+ ```javascript
129
+ // Pass verbose option from CLI
130
+ const verbose = process.env.VERBOSE === 'true' || options.verbose;
131
+ ```
132
+
133
+ **Logging Levels:**
134
+
135
+ **Normal Mode (default):**
136
+ - [ ] Log only essential info: Start, Progress, Results
137
+ - [ ] No internal details
138
+ - [ ] Clean output for CI/CD
139
+
140
+ ```javascript
141
+ console.log('🔍 Analyzing API impacts...');
142
+ console.log('📡 Found 3 affected endpoints');
143
+ ```
144
+
145
+ **Verbose Mode (`--verbose`):**
146
+ - [ ] Log each step of traversal
147
+ - [ ] Show which methods are visited
148
+ - [ ] Display decorator information
149
+ - [ ] Show performance metrics
150
+
151
+ ```javascript
152
+ if (verbose) {
153
+ console.log(`[API Detector] Starting traversal from: ${method.name}`);
154
+ console.log(`[API Detector] Visiting caller: ${caller.name} (depth: ${depth})`);
155
+ console.log(`[API Detector] Found endpoint: ${httpMethod} ${path}`);
156
+ console.log(`[API Detector] Completed in ${duration}ms`);
157
+ }
158
+ ```
159
+
160
+ **Implementation Pattern:**
161
+ ```javascript
162
+ // core/utils/logger.js
163
+ export function createLogger(verbose = false) {
164
+ return {
165
+ info: (msg) => console.log(msg),
166
+ debug: (msg) => verbose && console.log(`[DEBUG] ${msg}`),
167
+ verbose: (component, msg) => verbose && console.log(`[${component}] ${msg}`)
168
+ };
169
+ }
170
+
171
+ // Usage in endpoint-detector.js
172
+ import { createLogger } from '../utils/logger.js';
173
+
174
+ export function findAffectedEndpoints(methods, callGraph, options = {}) {
175
+ const logger = createLogger(options.verbose);
176
+
177
+ logger.info('🔍 Analyzing API impacts...');
178
+ logger.verbose('EndpointDetector', `Processing ${methods.length} changed methods`);
179
+
180
+ for (const method of methods) {
181
+ logger.verbose('EndpointDetector', `Traversing from: ${method.name}`);
182
+ // ... detection logic
183
+ }
184
+
185
+ logger.info(`📡 Found ${endpoints.length} affected endpoints`);
186
+ return endpoints;
187
+ }
188
+ ```
189
+
190
+ ### 5. Documentation Updates
191
+
192
+ **File Headers:**
193
+ ```javascript
194
+ /**
195
+ * @file endpoint-detector.js
196
+ * @description Detects API endpoints affected by code changes via call graph traversal
197
+ *
198
+ * Algorithm:
199
+ * 1. Start from changed method
200
+ * 2. Traverse upward through callers
201
+ * 3. Stop when HTTP decorator found
202
+ * 4. Extract endpoint info (method, path)
203
+ *
204
+ * @example
205
+ * const endpoints = findAffectedEndpoints(changedMethods, callGraph);
206
+ * // Returns: [{ method: 'GET', path: '/users/:id', ... }]
207
+ */
208
+ ```
209
+
210
+ **Function JSDoc:**
211
+ ```javascript
212
+ /**
213
+ * Find API endpoints affected by changed methods
214
+ * @param {Array<string>} changedMethods - List of changed method names
215
+ * @param {CallGraph} callGraph - Method call graph with caller/callee relationships
216
+ * @param {Object} options - Options { verbose: boolean }
217
+ * @returns {Array<Endpoint>} List of affected endpoints
218
+ */
219
+ export function findAffectedEndpoints(changedMethods, callGraph, options = {}) {
220
+ // Implementation
221
+ }
222
+ ```
223
+
224
+ ## ✅ Acceptance Criteria
225
+
226
+ - [ ] All code moved to `core/` folder structure
227
+ - [ ] No references to old `modules/` folder
228
+ - [ ] Unused functions removed (document what was removed)
229
+ - [ ] Performance improved (measure before/after)
230
+ - [ ] Verbose mode works with `--verbose` flag
231
+ - [ ] Normal mode shows clean output
232
+ - [ ] All functions have JSDoc comments
233
+ - [ ] Code follows single responsibility principle
234
+ - [ ] Tests pass (if any exist)
235
+
236
+ ## 📊 Performance Targets
237
+
238
+ - [ ] Call graph traversal: < 100ms for 1000 methods
239
+ - [ ] Endpoint detection: < 50ms per changed method
240
+ - [ ] Memory usage: < 500MB for large codebases
241
+
242
+ ## 🧪 Testing
243
+
244
+ **Manual Test:**
245
+ ```bash
246
+ # Normal mode
247
+ node cli.js --input=src --base=origin/main
248
+
249
+ # Verbose mode
250
+ node cli.js --input=src --base=origin/main --verbose
251
+ ```
252
+
253
+ **Expected Output (Normal):**
254
+ ```
255
+ 🔍 Analyzing code changes...
256
+ 📊 Found 5 changed methods
257
+ 🔍 Analyzing API impacts...
258
+ 📡 Found 3 affected endpoints
259
+ ✅ Analysis complete
260
+ ```
261
+
262
+ **Expected Output (Verbose):**
263
+ ```
264
+ 🔍 Analyzing code changes...
265
+ [ChangeDetector] Parsing git diff: origin/main..HEAD
266
+ [ChangeDetector] Found 2 modified files
267
+ [ChangeDetector] Extracted 5 changed methods
268
+ 📊 Found 5 changed methods
269
+
270
+ 🔍 Analyzing API impacts...
271
+ [EndpointDetector] Processing 5 changed methods
272
+ [EndpointDetector] Traversing from: UserService.updateEmail
273
+ [EndpointDetector] Visiting caller: UserController.updateProfile (depth: 1)
274
+ [EndpointDetector] Found endpoint: PUT /users/:id
275
+ [EndpointDetector] Completed in 45ms
276
+ 📡 Found 3 affected endpoints
277
+ ✅ Analysis complete
278
+ ```
279
+
280
+ ## 📚 References
281
+
282
+ - API Impact Detection Spec: `.specify/specs/features/api-impact-detection.md`
283
+ - Architecture Doc: `.specify/plans/architecture.md`
284
+ - Code Rules: `.github/copilot-instructions.md`