mstro-app 0.3.7 → 0.3.9

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.
Files changed (131) hide show
  1. package/README.md +4 -8
  2. package/bin/mstro.js +54 -15
  3. package/dist/server/cli/headless/claude-invoker.d.ts.map +1 -1
  4. package/dist/server/cli/headless/claude-invoker.js +18 -9
  5. package/dist/server/cli/headless/claude-invoker.js.map +1 -1
  6. package/dist/server/cli/headless/headless-logger.d.ts +10 -0
  7. package/dist/server/cli/headless/headless-logger.d.ts.map +1 -0
  8. package/dist/server/cli/headless/headless-logger.js +66 -0
  9. package/dist/server/cli/headless/headless-logger.js.map +1 -0
  10. package/dist/server/cli/headless/mcp-config.d.ts.map +1 -1
  11. package/dist/server/cli/headless/mcp-config.js +6 -5
  12. package/dist/server/cli/headless/mcp-config.js.map +1 -1
  13. package/dist/server/cli/headless/runner.d.ts.map +1 -1
  14. package/dist/server/cli/headless/runner.js +4 -0
  15. package/dist/server/cli/headless/runner.js.map +1 -1
  16. package/dist/server/cli/headless/stall-assessor.d.ts +21 -0
  17. package/dist/server/cli/headless/stall-assessor.d.ts.map +1 -1
  18. package/dist/server/cli/headless/stall-assessor.js +74 -20
  19. package/dist/server/cli/headless/stall-assessor.js.map +1 -1
  20. package/dist/server/cli/headless/tool-watchdog.d.ts +0 -12
  21. package/dist/server/cli/headless/tool-watchdog.d.ts.map +1 -1
  22. package/dist/server/cli/headless/tool-watchdog.js +30 -9
  23. package/dist/server/cli/headless/tool-watchdog.js.map +1 -1
  24. package/dist/server/cli/headless/types.d.ts +8 -1
  25. package/dist/server/cli/headless/types.d.ts.map +1 -1
  26. package/dist/server/cli/improvisation-session-manager.d.ts +16 -0
  27. package/dist/server/cli/improvisation-session-manager.d.ts.map +1 -1
  28. package/dist/server/cli/improvisation-session-manager.js +94 -11
  29. package/dist/server/cli/improvisation-session-manager.js.map +1 -1
  30. package/dist/server/index.js +0 -4
  31. package/dist/server/index.js.map +1 -1
  32. package/dist/server/mcp/bouncer-cli.d.ts +3 -0
  33. package/dist/server/mcp/bouncer-cli.d.ts.map +1 -0
  34. package/dist/server/mcp/bouncer-cli.js +54 -0
  35. package/dist/server/mcp/bouncer-cli.js.map +1 -0
  36. package/dist/server/mcp/bouncer-integration.d.ts +2 -0
  37. package/dist/server/mcp/bouncer-integration.d.ts.map +1 -1
  38. package/dist/server/mcp/bouncer-integration.js +55 -39
  39. package/dist/server/mcp/bouncer-integration.js.map +1 -1
  40. package/dist/server/mcp/bouncer-sandbox.d.ts +60 -0
  41. package/dist/server/mcp/bouncer-sandbox.d.ts.map +1 -0
  42. package/dist/server/mcp/bouncer-sandbox.js +182 -0
  43. package/dist/server/mcp/bouncer-sandbox.js.map +1 -0
  44. package/dist/server/mcp/security-patterns.d.ts +6 -12
  45. package/dist/server/mcp/security-patterns.d.ts.map +1 -1
  46. package/dist/server/mcp/security-patterns.js +197 -10
  47. package/dist/server/mcp/security-patterns.js.map +1 -1
  48. package/dist/server/services/plan/composer.d.ts +4 -0
  49. package/dist/server/services/plan/composer.d.ts.map +1 -0
  50. package/dist/server/services/plan/composer.js +181 -0
  51. package/dist/server/services/plan/composer.js.map +1 -0
  52. package/dist/server/services/plan/dependency-resolver.d.ts +28 -0
  53. package/dist/server/services/plan/dependency-resolver.d.ts.map +1 -0
  54. package/dist/server/services/plan/dependency-resolver.js +152 -0
  55. package/dist/server/services/plan/dependency-resolver.js.map +1 -0
  56. package/dist/server/services/plan/executor.d.ts +91 -0
  57. package/dist/server/services/plan/executor.d.ts.map +1 -0
  58. package/dist/server/services/plan/executor.js +545 -0
  59. package/dist/server/services/plan/executor.js.map +1 -0
  60. package/dist/server/services/plan/parser.d.ts +11 -0
  61. package/dist/server/services/plan/parser.d.ts.map +1 -0
  62. package/dist/server/services/plan/parser.js +415 -0
  63. package/dist/server/services/plan/parser.js.map +1 -0
  64. package/dist/server/services/plan/state-reconciler.d.ts +2 -0
  65. package/dist/server/services/plan/state-reconciler.d.ts.map +1 -0
  66. package/dist/server/services/plan/state-reconciler.js +105 -0
  67. package/dist/server/services/plan/state-reconciler.js.map +1 -0
  68. package/dist/server/services/plan/types.d.ts +120 -0
  69. package/dist/server/services/plan/types.d.ts.map +1 -0
  70. package/dist/server/services/plan/types.js +4 -0
  71. package/dist/server/services/plan/types.js.map +1 -0
  72. package/dist/server/services/plan/watcher.d.ts +14 -0
  73. package/dist/server/services/plan/watcher.d.ts.map +1 -0
  74. package/dist/server/services/plan/watcher.js +69 -0
  75. package/dist/server/services/plan/watcher.js.map +1 -0
  76. package/dist/server/services/websocket/file-explorer-handlers.js +20 -0
  77. package/dist/server/services/websocket/file-explorer-handlers.js.map +1 -1
  78. package/dist/server/services/websocket/handler.d.ts +0 -1
  79. package/dist/server/services/websocket/handler.d.ts.map +1 -1
  80. package/dist/server/services/websocket/handler.js +28 -2
  81. package/dist/server/services/websocket/handler.js.map +1 -1
  82. package/dist/server/services/websocket/plan-handlers.d.ts +6 -0
  83. package/dist/server/services/websocket/plan-handlers.d.ts.map +1 -0
  84. package/dist/server/services/websocket/plan-handlers.js +494 -0
  85. package/dist/server/services/websocket/plan-handlers.js.map +1 -0
  86. package/dist/server/services/websocket/quality-handlers.d.ts +4 -0
  87. package/dist/server/services/websocket/quality-handlers.d.ts.map +1 -0
  88. package/dist/server/services/websocket/quality-handlers.js +470 -0
  89. package/dist/server/services/websocket/quality-handlers.js.map +1 -0
  90. package/dist/server/services/websocket/quality-persistence.d.ts +45 -0
  91. package/dist/server/services/websocket/quality-persistence.d.ts.map +1 -0
  92. package/dist/server/services/websocket/quality-persistence.js +187 -0
  93. package/dist/server/services/websocket/quality-persistence.js.map +1 -0
  94. package/dist/server/services/websocket/quality-service.d.ts +54 -0
  95. package/dist/server/services/websocket/quality-service.d.ts.map +1 -0
  96. package/dist/server/services/websocket/quality-service.js +816 -0
  97. package/dist/server/services/websocket/quality-service.js.map +1 -0
  98. package/dist/server/services/websocket/session-handlers.d.ts.map +1 -1
  99. package/dist/server/services/websocket/session-handlers.js +23 -0
  100. package/dist/server/services/websocket/session-handlers.js.map +1 -1
  101. package/dist/server/services/websocket/types.d.ts +2 -2
  102. package/dist/server/services/websocket/types.d.ts.map +1 -1
  103. package/package.json +3 -2
  104. package/server/cli/headless/claude-invoker.ts +21 -9
  105. package/server/cli/headless/headless-logger.ts +78 -0
  106. package/server/cli/headless/mcp-config.ts +6 -5
  107. package/server/cli/headless/runner.ts +4 -0
  108. package/server/cli/headless/stall-assessor.ts +101 -20
  109. package/server/cli/headless/tool-watchdog.ts +18 -9
  110. package/server/cli/headless/types.ts +10 -1
  111. package/server/cli/improvisation-session-manager.ts +118 -11
  112. package/server/index.ts +0 -4
  113. package/server/mcp/bouncer-cli.ts +73 -0
  114. package/server/mcp/bouncer-integration.ts +66 -44
  115. package/server/mcp/bouncer-sandbox.ts +214 -0
  116. package/server/mcp/security-patterns.ts +206 -10
  117. package/server/services/plan/composer.ts +199 -0
  118. package/server/services/plan/dependency-resolver.ts +179 -0
  119. package/server/services/plan/executor.ts +604 -0
  120. package/server/services/plan/parser.ts +459 -0
  121. package/server/services/plan/state-reconciler.ts +132 -0
  122. package/server/services/plan/types.ts +164 -0
  123. package/server/services/plan/watcher.ts +73 -0
  124. package/server/services/websocket/file-explorer-handlers.ts +20 -0
  125. package/server/services/websocket/handler.ts +28 -2
  126. package/server/services/websocket/plan-handlers.ts +592 -0
  127. package/server/services/websocket/quality-handlers.ts +570 -0
  128. package/server/services/websocket/quality-persistence.ts +250 -0
  129. package/server/services/websocket/quality-service.ts +975 -0
  130. package/server/services/websocket/session-handlers.ts +26 -0
  131. package/server/services/websocket/types.ts +62 -2
@@ -0,0 +1,570 @@
1
+ // Copyright (c) 2025-present Mstro, Inc. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file for details.
3
+
4
+ import { join } from 'node:path';
5
+ import { runWithFileLogger } from '../../cli/headless/headless-logger.js';
6
+ import { HeadlessRunner } from '../../cli/headless/index.js';
7
+ import type { ToolUseEvent } from '../../cli/headless/types.js';
8
+ import type { HandlerContext } from './handler-context.js';
9
+ import { QualityPersistence } from './quality-persistence.js';
10
+ import { detectTools, installTools, runQualityScan } from './quality-service.js';
11
+ import type { WebSocketMessage, WSContext } from './types.js';
12
+
13
+ const TOOL_MESSAGES: Record<string, string> = {
14
+ Read: 'Reading files to understand issues...',
15
+ Edit: 'Applying fixes...',
16
+ Write: 'Writing fixes...',
17
+ Grep: 'Searching for related code...',
18
+ Bash: 'Running verification...',
19
+ };
20
+
21
+ function createToolProgressCallback(ctx: HandlerContext, ws: WSContext, reportPath: string) {
22
+ const seenTools = new Set<string>();
23
+ return (event: ToolUseEvent) => {
24
+ if (event.type === 'tool_start' && event.toolName && !seenTools.has(event.toolName)) {
25
+ seenTools.add(event.toolName);
26
+ const message = TOOL_MESSAGES[event.toolName];
27
+ if (message) {
28
+ ctx.send(ws, { type: 'qualityFixProgress', data: { path: reportPath, message } });
29
+ }
30
+ }
31
+ if (event.type === 'tool_complete' && event.toolName === 'Edit' && event.completeInput?.file_path) {
32
+ ctx.send(ws, {
33
+ type: 'qualityFixProgress',
34
+ data: { path: reportPath, message: `Fixed ${String(event.completeInput.file_path).split('/').slice(-2).join('/')}` },
35
+ });
36
+ }
37
+ };
38
+ }
39
+
40
+ const persistenceCache = new Map<string, QualityPersistence>();
41
+ const activeReviews = new Set<string>();
42
+
43
+ function getPersistence(workingDir: string): QualityPersistence {
44
+ let persistence = persistenceCache.get(workingDir);
45
+ if (!persistence) {
46
+ persistence = new QualityPersistence(workingDir);
47
+ persistenceCache.set(workingDir, persistence);
48
+ }
49
+ return persistence;
50
+ }
51
+
52
+ export function handleQualityMessage(
53
+ ctx: HandlerContext,
54
+ ws: WSContext,
55
+ msg: WebSocketMessage,
56
+ _tabId: string,
57
+ workingDir: string,
58
+ ): void {
59
+ const handlers: Record<string, () => void> = {
60
+ qualityDetectTools: () => handleDetectTools(ctx, ws, msg, workingDir),
61
+ qualityScan: () => handleScan(ctx, ws, msg, workingDir),
62
+ qualityInstallTools: () => handleInstallTools(ctx, ws, msg, workingDir),
63
+ qualityCodeReview: () => handleCodeReview(ctx, ws, msg, workingDir),
64
+ qualityFixIssues: () => handleFixIssues(ctx, ws, msg, workingDir),
65
+ qualityLoadState: () => handleLoadState(ctx, ws, workingDir),
66
+ qualitySaveDirectories: () => handleSaveDirectories(ctx, ws, msg, workingDir),
67
+ };
68
+
69
+ const handler = handlers[msg.type];
70
+ if (!handler) return;
71
+
72
+ try {
73
+ handler();
74
+ } catch (error) {
75
+ const errMsg = error instanceof Error ? error.message : String(error);
76
+ ctx.send(ws, {
77
+ type: 'qualityError',
78
+ data: { path: msg.data?.path || workingDir, error: errMsg },
79
+ });
80
+ }
81
+ }
82
+
83
+ function resolvePath(workingDir: string, dirPath?: string): string {
84
+ if (!dirPath || dirPath === '.' || dirPath === './') return workingDir;
85
+ if (dirPath.startsWith('/')) return dirPath;
86
+ return join(workingDir, dirPath);
87
+ }
88
+
89
+ async function handleLoadState(
90
+ ctx: HandlerContext,
91
+ ws: WSContext,
92
+ workingDir: string,
93
+ ): Promise<void> {
94
+ try {
95
+ const persistence = getPersistence(workingDir);
96
+ const state = persistence.loadState();
97
+ ctx.send(ws, {
98
+ type: 'qualityStateLoaded',
99
+ data: state,
100
+ });
101
+ } catch (error) {
102
+ ctx.send(ws, {
103
+ type: 'qualityError',
104
+ data: { path: '.', error: error instanceof Error ? error.message : String(error) },
105
+ });
106
+ }
107
+ }
108
+
109
+ async function handleSaveDirectories(
110
+ ctx: HandlerContext,
111
+ ws: WSContext,
112
+ msg: WebSocketMessage,
113
+ workingDir: string,
114
+ ): Promise<void> {
115
+ try {
116
+ const persistence = getPersistence(workingDir);
117
+ const directories: Array<{ path: string; label: string }> = msg.data?.directories || [];
118
+ persistence.saveConfig(directories);
119
+ } catch (error) {
120
+ ctx.send(ws, {
121
+ type: 'qualityError',
122
+ data: { path: '.', error: error instanceof Error ? error.message : String(error) },
123
+ });
124
+ }
125
+ }
126
+
127
+ async function handleDetectTools(
128
+ ctx: HandlerContext,
129
+ ws: WSContext,
130
+ msg: WebSocketMessage,
131
+ workingDir: string,
132
+ ): Promise<void> {
133
+ const dirPath = resolvePath(workingDir, msg.data?.path);
134
+ try {
135
+ const { tools, ecosystem } = await detectTools(dirPath);
136
+ ctx.send(ws, {
137
+ type: 'qualityToolsDetected',
138
+ data: { path: msg.data?.path || '.', tools, ecosystem },
139
+ });
140
+ } catch (error) {
141
+ ctx.send(ws, {
142
+ type: 'qualityError',
143
+ data: { path: msg.data?.path || '.', error: error instanceof Error ? error.message : String(error) },
144
+ });
145
+ }
146
+ }
147
+
148
+ async function handleScan(
149
+ ctx: HandlerContext,
150
+ ws: WSContext,
151
+ msg: WebSocketMessage,
152
+ workingDir: string,
153
+ ): Promise<void> {
154
+ const dirPath = resolvePath(workingDir, msg.data?.path);
155
+ const reportPath = msg.data?.path || '.';
156
+
157
+ try {
158
+ // Detect installed tools so the scan can skip unavailable categories
159
+ const { tools: detectedTools } = await detectTools(dirPath);
160
+ const installedToolNames = detectedTools.filter((t) => t.installed).map((t) => t.name);
161
+
162
+ const results = await runQualityScan(dirPath, (progress) => {
163
+ ctx.send(ws, {
164
+ type: 'qualityScanProgress',
165
+ data: { path: reportPath, progress },
166
+ });
167
+ }, installedToolNames);
168
+ ctx.send(ws, {
169
+ type: 'qualityScanResults',
170
+ data: { path: reportPath, results },
171
+ });
172
+
173
+ // Persist report and append to history
174
+ try {
175
+ const persistence = getPersistence(workingDir);
176
+ persistence.saveReport(reportPath, results);
177
+ persistence.appendHistory(results, reportPath);
178
+ } catch {
179
+ // Persistence failure should not break the scan flow
180
+ }
181
+ } catch (error) {
182
+ ctx.send(ws, {
183
+ type: 'qualityError',
184
+ data: { path: reportPath, error: error instanceof Error ? error.message : String(error) },
185
+ });
186
+ }
187
+ }
188
+
189
+ async function handleInstallTools(
190
+ ctx: HandlerContext,
191
+ ws: WSContext,
192
+ msg: WebSocketMessage,
193
+ workingDir: string,
194
+ ): Promise<void> {
195
+ const dirPath = resolvePath(workingDir, msg.data?.path);
196
+ const reportPath = msg.data?.path || '.';
197
+ const toolNames: string[] | undefined = msg.data?.tools;
198
+
199
+ try {
200
+ ctx.send(ws, {
201
+ type: 'qualityInstallProgress',
202
+ data: { path: reportPath, installing: true },
203
+ });
204
+
205
+ const { tools, ecosystem } = await installTools(dirPath, toolNames);
206
+
207
+ ctx.send(ws, {
208
+ type: 'qualityInstallComplete',
209
+ data: { path: reportPath, tools, ecosystem },
210
+ });
211
+ } catch (error) {
212
+ ctx.send(ws, {
213
+ type: 'qualityError',
214
+ data: { path: reportPath, error: error instanceof Error ? error.message : String(error) },
215
+ });
216
+ }
217
+ }
218
+
219
+ // ============================================================================
220
+ // Code Review Agent
221
+ // ============================================================================
222
+
223
+ function buildCodeReviewPrompt(dirPath: string): string {
224
+ return `You are an expert code review agent. Your task is to perform a comprehensive, language-agnostic code review of the project in the current working directory.
225
+
226
+ IMPORTANT: Your current working directory is "${dirPath}". Only review files within this directory. Do NOT traverse parent directories or review files outside this path.
227
+
228
+ ## Review Process
229
+
230
+ 1. **Discover**: Use Glob to find source files (e.g. "**/*.{ts,tsx,js,py,rs,go,java,rb,php}"). Understand the project structure. Only search within the current directory.
231
+ 2. **Read**: Read the most important files — entry points, core modules, handlers, services. Prioritize files with recent git changes (\`git diff --name-only HEAD~5\` via Bash if available).
232
+ 3. **Analyze**: Look for real, actionable issues across these categories:
233
+ - **security**: Injection vulnerabilities (SQL, XSS, command), hardcoded secrets/credentials, auth bypasses, insecure crypto, path traversal, SSRF, unsafe deserialization
234
+ - **bugs**: Null/undefined errors, race conditions, logic errors, unhandled edge cases, off-by-one errors, resource leaks, incorrect error handling
235
+ - **performance**: N+1 queries, unnecessary re-renders, missing memoization, blocking I/O in hot paths, unbounded data structures, missing pagination
236
+ - **maintainability**: God functions (>100 lines), deep nesting (>4 levels), duplicated logic, missing error handling at system boundaries, tight coupling
237
+
238
+ ## Rules
239
+
240
+ - Only report findings you are >80% confident about. No speculative or low-confidence issues.
241
+ - Focus on bugs and security over style. Skip formatting, naming preferences, and minor nits.
242
+ - Each finding MUST reference a specific file and line number. Do not report vague or file-level issues.
243
+ - Limit to the 20 most important findings, ranked by severity.
244
+ - Do NOT modify any files. This is a read-only review.
245
+
246
+ ## Output
247
+
248
+ After your analysis, output EXACTLY one JSON code block with your findings. No other text after the JSON block.
249
+
250
+ \`\`\`json
251
+ {
252
+ "findings": [
253
+ {
254
+ "severity": "critical|high|medium|low",
255
+ "category": "security|bugs|performance|maintainability",
256
+ "file": "relative/path/to/file.ts",
257
+ "line": 42,
258
+ "title": "Short title describing the issue",
259
+ "description": "What the problem is and why it matters.",
260
+ "suggestion": "How to fix it."
261
+ }
262
+ ],
263
+ "summary": "Brief 1-2 sentence summary of overall code quality."
264
+ }
265
+ \`\`\``;
266
+ }
267
+
268
+ interface CodeReviewFinding {
269
+ severity: 'critical' | 'high' | 'medium' | 'low';
270
+ category: 'security' | 'bugs' | 'performance' | 'maintainability';
271
+ file: string;
272
+ line: number | null;
273
+ title: string;
274
+ description: string;
275
+ suggestion?: string;
276
+ }
277
+
278
+ const VALID_SEVERITIES = new Set(['critical', 'high', 'medium', 'low']);
279
+ const VALID_CATEGORIES = new Set(['security', 'bugs', 'performance', 'maintainability']);
280
+
281
+ function normalizeFinding(f: Record<string, unknown>): CodeReviewFinding | null {
282
+ if (typeof f.file !== 'string' || typeof f.title !== 'string') return null;
283
+ return {
284
+ severity: VALID_SEVERITIES.has(f.severity as string) ? f.severity as CodeReviewFinding['severity'] : 'medium',
285
+ category: VALID_CATEGORIES.has(f.category as string) ? f.category as CodeReviewFinding['category'] : 'maintainability',
286
+ file: f.file as string,
287
+ line: typeof f.line === 'number' ? f.line : null,
288
+ title: f.title as string,
289
+ description: typeof f.description === 'string' ? f.description : '',
290
+ suggestion: typeof f.suggestion === 'string' ? f.suggestion : undefined,
291
+ };
292
+ }
293
+
294
+ function extractJson(response: string): string {
295
+ // Try ```json ... ``` first, then plain ``` ... ```, then largest {...} block
296
+ const fencedJson = response.match(/```json\s*([\s\S]*?)```/);
297
+ if (fencedJson) return fencedJson[1].trim();
298
+
299
+ const fencedPlain = response.match(/```\s*([\s\S]*?)```/);
300
+ if (fencedPlain) return fencedPlain[1].trim();
301
+
302
+ const braceMatch = response.match(/\{[\s\S]*\}/);
303
+ if (braceMatch) return braceMatch[0].trim();
304
+
305
+ return response.trim();
306
+ }
307
+
308
+ function parseCodeReviewResponse(response: string): { findings: CodeReviewFinding[]; summary: string } {
309
+ const jsonStr = extractJson(response);
310
+
311
+ try {
312
+ const parsed = JSON.parse(jsonStr);
313
+ const rawFindings: Record<string, unknown>[] = Array.isArray(parsed.findings) ? parsed.findings : [];
314
+ const findings = rawFindings.map(normalizeFinding).filter((f): f is CodeReviewFinding => f !== null);
315
+ const summary = typeof parsed.summary === 'string' ? parsed.summary : `Found ${findings.length} issue(s).`;
316
+ return { findings, summary };
317
+ } catch {
318
+ return { findings: [], summary: 'Failed to parse code review results.' };
319
+ }
320
+ }
321
+
322
+ const TOOL_START_MESSAGES: Record<string, string> = {
323
+ Glob: 'Discovering project files...',
324
+ Read: 'Reading source files...',
325
+ Grep: 'Searching codebase...',
326
+ Bash: 'Running analysis command...',
327
+ };
328
+
329
+ function getToolCompleteMessage(event: ToolUseEvent): string | null {
330
+ const input = event.completeInput;
331
+ if (!input) return null;
332
+ if (event.toolName === 'Read' && input.file_path) {
333
+ return `Reviewed ${String(input.file_path).split('/').slice(-2).join('/')}`;
334
+ }
335
+ if (event.toolName === 'Grep' && input.pattern) {
336
+ return `Searched for "${String(input.pattern).slice(0, 40)}"`;
337
+ }
338
+ return null;
339
+ }
340
+
341
+ function createCodeReviewProgressTracker() {
342
+ const seenToolStarts = new Set<string>();
343
+
344
+ return (event: ToolUseEvent): string | null => {
345
+ if (event.type === 'tool_start' && event.toolName) {
346
+ if (seenToolStarts.has(event.toolName)) return null;
347
+ seenToolStarts.add(event.toolName);
348
+ return TOOL_START_MESSAGES[event.toolName] ?? null;
349
+ }
350
+ if (event.type === 'tool_complete') return getToolCompleteMessage(event);
351
+ return null;
352
+ };
353
+ }
354
+
355
+ async function handleCodeReview(
356
+ ctx: HandlerContext,
357
+ ws: WSContext,
358
+ msg: WebSocketMessage,
359
+ workingDir: string,
360
+ ): Promise<void> {
361
+ const dirPath = resolvePath(workingDir, msg.data?.path);
362
+ const reportPath = msg.data?.path || '.';
363
+
364
+ if (activeReviews.has(dirPath)) {
365
+ ctx.send(ws, {
366
+ type: 'qualityError',
367
+ data: { path: reportPath, error: 'A code review is already running for this directory.' },
368
+ });
369
+ return;
370
+ }
371
+
372
+ activeReviews.add(dirPath);
373
+ try {
374
+ // Send initial progress
375
+ ctx.send(ws, {
376
+ type: 'qualityCodeReviewProgress',
377
+ data: { path: reportPath, message: 'Starting AI code review...' },
378
+ });
379
+
380
+ const runner = new HeadlessRunner({
381
+ workingDir: dirPath,
382
+ directPrompt: buildCodeReviewPrompt(dirPath),
383
+ stallWarningMs: 120_000,
384
+ stallKillMs: 600_000,
385
+ stallHardCapMs: 900_000,
386
+ toolUseCallback: (() => {
387
+ const getProgressMessage = createCodeReviewProgressTracker();
388
+ return (event: ToolUseEvent) => {
389
+ const message = getProgressMessage(event);
390
+ if (message) {
391
+ ctx.send(ws, {
392
+ type: 'qualityCodeReviewProgress',
393
+ data: { path: reportPath, message },
394
+ });
395
+ }
396
+ };
397
+ })(),
398
+ });
399
+
400
+ ctx.send(ws, {
401
+ type: 'qualityCodeReviewProgress',
402
+ data: { path: reportPath, message: 'Claude is analyzing your codebase...' },
403
+ });
404
+
405
+ const result = await runWithFileLogger('code-review', () => runner.run());
406
+
407
+ ctx.send(ws, {
408
+ type: 'qualityCodeReviewProgress',
409
+ data: { path: reportPath, message: 'Generating review report...' },
410
+ });
411
+
412
+ const responseText = result.assistantResponse || '';
413
+ const { findings, summary } = parseCodeReviewResponse(responseText);
414
+
415
+ ctx.send(ws, {
416
+ type: 'qualityCodeReview',
417
+ data: { path: reportPath, findings, summary },
418
+ });
419
+
420
+ // Persist code review results
421
+ try {
422
+ const persistence = getPersistence(workingDir);
423
+ persistence.saveCodeReview(reportPath, findings as unknown as Record<string, unknown>[], summary);
424
+ } catch {
425
+ // Persistence failure should not break the review flow
426
+ }
427
+ } catch (error) {
428
+ ctx.send(ws, {
429
+ type: 'qualityError',
430
+ data: { path: reportPath, error: error instanceof Error ? error.message : String(error) },
431
+ });
432
+ } finally {
433
+ activeReviews.delete(dirPath);
434
+ }
435
+ }
436
+
437
+ // ============================================================================
438
+ // Fix Issues Agent
439
+ // ============================================================================
440
+
441
+ interface FindingForFix {
442
+ severity: string;
443
+ category: string;
444
+ file: string;
445
+ line: number | null;
446
+ title: string;
447
+ description: string;
448
+ suggestion?: string;
449
+ }
450
+
451
+ function buildFixPrompt(findings: FindingForFix[], section?: string): string {
452
+ const filtered = section ? findings.filter((f) => f.category === section) : findings;
453
+ const sorted = filtered.sort((a, b) => {
454
+ const order: Record<string, number> = { critical: 0, high: 1, medium: 2, low: 3 };
455
+ return (order[a.severity] ?? 4) - (order[b.severity] ?? 4);
456
+ });
457
+
458
+ const issueList = sorted.slice(0, 30).map((f, i) => {
459
+ const loc = f.line ? `${f.file}:${f.line}` : f.file;
460
+ const parts = [`${i + 1}. [${f.severity.toUpperCase()}] ${loc} — ${f.title}`];
461
+ if (f.description) parts.push(` ${f.description}`);
462
+ if (f.suggestion) parts.push(` Suggestion: ${f.suggestion}`);
463
+ return parts.join('\n');
464
+ }).join('\n\n');
465
+
466
+ return `You are a code quality fix agent. Fix the following quality issues in the codebase.
467
+
468
+ ## Issues to Fix (${sorted.length} total, showing top ${Math.min(30, sorted.length)})
469
+
470
+ ${issueList}
471
+
472
+ ## Rules
473
+
474
+ - Fix each issue by editing the relevant file at the specified location.
475
+ - For complexity issues: refactor into smaller functions. For long files: split or extract modules. For long functions: break into smaller functions.
476
+ - For security issues: apply the suggested fix or use secure coding best practices.
477
+ - For bugs: fix the root cause, not just the symptom.
478
+ - For linting/formatting: apply the standard for the project.
479
+ - Do NOT introduce new issues. Make minimal, focused changes.
480
+ - After fixing, verify the changes compile/pass linting if tools are available.
481
+ - Work through the issues systematically from most to least severe.`;
482
+ }
483
+
484
+ const activeFixes = new Set<string>();
485
+
486
+ async function handleFixIssues(
487
+ ctx: HandlerContext,
488
+ ws: WSContext,
489
+ msg: WebSocketMessage,
490
+ workingDir: string,
491
+ ): Promise<void> {
492
+ const dirPath = resolvePath(workingDir, msg.data?.path);
493
+ const reportPath = msg.data?.path || '.';
494
+ const section: string | undefined = msg.data?.section;
495
+ const findings: FindingForFix[] = msg.data?.findings || [];
496
+
497
+ if (activeFixes.has(dirPath)) {
498
+ ctx.send(ws, {
499
+ type: 'qualityError',
500
+ data: { path: reportPath, error: 'A fix operation is already running for this directory.' },
501
+ });
502
+ return;
503
+ }
504
+
505
+ if (findings.length === 0) {
506
+ ctx.send(ws, {
507
+ type: 'qualityError',
508
+ data: { path: reportPath, error: 'No findings to fix.' },
509
+ });
510
+ return;
511
+ }
512
+
513
+ activeFixes.add(dirPath);
514
+ try {
515
+ ctx.send(ws, {
516
+ type: 'qualityFixProgress',
517
+ data: { path: reportPath, message: 'Starting Claude Code to fix issues...' },
518
+ });
519
+
520
+ const prompt = buildFixPrompt(findings, section);
521
+
522
+ const runner = new HeadlessRunner({
523
+ workingDir: dirPath,
524
+ directPrompt: prompt,
525
+ stallWarningMs: 120_000,
526
+ stallKillMs: 600_000,
527
+ stallHardCapMs: 900_000,
528
+ toolUseCallback: createToolProgressCallback(ctx, ws, reportPath),
529
+ });
530
+
531
+ await runWithFileLogger('code-review-fix', () => runner.run());
532
+
533
+ ctx.send(ws, {
534
+ type: 'qualityFixProgress',
535
+ data: { path: reportPath, message: 'Fixes applied. Re-running quality checks...' },
536
+ });
537
+
538
+ // Re-run quality scan after fixing
539
+ const { tools: detectedTools } = await detectTools(dirPath);
540
+ const installedToolNames = detectedTools.filter((t) => t.installed).map((t) => t.name);
541
+
542
+ const results = await runQualityScan(dirPath, (progress) => {
543
+ ctx.send(ws, {
544
+ type: 'qualityScanProgress',
545
+ data: { path: reportPath, progress },
546
+ });
547
+ }, installedToolNames);
548
+
549
+ ctx.send(ws, {
550
+ type: 'qualityFixComplete',
551
+ data: { path: reportPath, results },
552
+ });
553
+
554
+ // Persist
555
+ try {
556
+ const persistence = getPersistence(workingDir);
557
+ persistence.saveReport(reportPath, results);
558
+ persistence.appendHistory(results, reportPath);
559
+ } catch {
560
+ // Persistence failure should not break the fix flow
561
+ }
562
+ } catch (error) {
563
+ ctx.send(ws, {
564
+ type: 'qualityError',
565
+ data: { path: reportPath, error: error instanceof Error ? error.message : String(error) },
566
+ });
567
+ } finally {
568
+ activeFixes.delete(dirPath);
569
+ }
570
+ }