mstro-app 0.3.8 → 0.4.0

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 (109) hide show
  1. package/LICENSE +191 -21
  2. package/PRIVACY.md +286 -62
  3. package/README.md +81 -58
  4. package/bin/commands/status.js +1 -1
  5. package/dist/server/cli/headless/claude-invoker.d.ts.map +1 -1
  6. package/dist/server/cli/headless/claude-invoker.js +22 -12
  7. package/dist/server/cli/headless/claude-invoker.js.map +1 -1
  8. package/dist/server/cli/headless/headless-logger.d.ts +10 -0
  9. package/dist/server/cli/headless/headless-logger.d.ts.map +1 -0
  10. package/dist/server/cli/headless/headless-logger.js +66 -0
  11. package/dist/server/cli/headless/headless-logger.js.map +1 -0
  12. package/dist/server/cli/headless/mcp-config.d.ts.map +1 -1
  13. package/dist/server/cli/headless/mcp-config.js +6 -5
  14. package/dist/server/cli/headless/mcp-config.js.map +1 -1
  15. package/dist/server/cli/headless/runner.d.ts.map +1 -1
  16. package/dist/server/cli/headless/runner.js +4 -0
  17. package/dist/server/cli/headless/runner.js.map +1 -1
  18. package/dist/server/cli/headless/stall-assessor.d.ts +21 -0
  19. package/dist/server/cli/headless/stall-assessor.d.ts.map +1 -1
  20. package/dist/server/cli/headless/stall-assessor.js +100 -24
  21. package/dist/server/cli/headless/stall-assessor.js.map +1 -1
  22. package/dist/server/cli/headless/tool-watchdog.d.ts +0 -12
  23. package/dist/server/cli/headless/tool-watchdog.d.ts.map +1 -1
  24. package/dist/server/cli/headless/tool-watchdog.js +22 -9
  25. package/dist/server/cli/headless/tool-watchdog.js.map +1 -1
  26. package/dist/server/cli/headless/types.d.ts +8 -1
  27. package/dist/server/cli/headless/types.d.ts.map +1 -1
  28. package/dist/server/cli/improvisation-session-manager.d.ts +16 -0
  29. package/dist/server/cli/improvisation-session-manager.d.ts.map +1 -1
  30. package/dist/server/cli/improvisation-session-manager.js +94 -11
  31. package/dist/server/cli/improvisation-session-manager.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/services/plan/composer.d.ts +4 -0
  37. package/dist/server/services/plan/composer.d.ts.map +1 -0
  38. package/dist/server/services/plan/composer.js +181 -0
  39. package/dist/server/services/plan/composer.js.map +1 -0
  40. package/dist/server/services/plan/dependency-resolver.d.ts +28 -0
  41. package/dist/server/services/plan/dependency-resolver.d.ts.map +1 -0
  42. package/dist/server/services/plan/dependency-resolver.js +154 -0
  43. package/dist/server/services/plan/dependency-resolver.js.map +1 -0
  44. package/dist/server/services/plan/executor.d.ts +110 -0
  45. package/dist/server/services/plan/executor.d.ts.map +1 -0
  46. package/dist/server/services/plan/executor.js +641 -0
  47. package/dist/server/services/plan/executor.js.map +1 -0
  48. package/dist/server/services/plan/parser.d.ts +11 -0
  49. package/dist/server/services/plan/parser.d.ts.map +1 -0
  50. package/dist/server/services/plan/parser.js +445 -0
  51. package/dist/server/services/plan/parser.js.map +1 -0
  52. package/dist/server/services/plan/state-reconciler.d.ts +2 -0
  53. package/dist/server/services/plan/state-reconciler.d.ts.map +1 -0
  54. package/dist/server/services/plan/state-reconciler.js +145 -0
  55. package/dist/server/services/plan/state-reconciler.js.map +1 -0
  56. package/dist/server/services/plan/types.d.ts +121 -0
  57. package/dist/server/services/plan/types.d.ts.map +1 -0
  58. package/dist/server/services/plan/types.js +4 -0
  59. package/dist/server/services/plan/types.js.map +1 -0
  60. package/dist/server/services/plan/watcher.d.ts +14 -0
  61. package/dist/server/services/plan/watcher.d.ts.map +1 -0
  62. package/dist/server/services/plan/watcher.js +69 -0
  63. package/dist/server/services/plan/watcher.js.map +1 -0
  64. package/dist/server/services/websocket/file-explorer-handlers.js +20 -0
  65. package/dist/server/services/websocket/file-explorer-handlers.js.map +1 -1
  66. package/dist/server/services/websocket/handler.d.ts.map +1 -1
  67. package/dist/server/services/websocket/handler.js +21 -0
  68. package/dist/server/services/websocket/handler.js.map +1 -1
  69. package/dist/server/services/websocket/plan-handlers.d.ts +6 -0
  70. package/dist/server/services/websocket/plan-handlers.d.ts.map +1 -0
  71. package/dist/server/services/websocket/plan-handlers.js +494 -0
  72. package/dist/server/services/websocket/plan-handlers.js.map +1 -0
  73. package/dist/server/services/websocket/quality-handlers.d.ts.map +1 -1
  74. package/dist/server/services/websocket/quality-handlers.js +384 -12
  75. package/dist/server/services/websocket/quality-handlers.js.map +1 -1
  76. package/dist/server/services/websocket/quality-persistence.d.ts +45 -0
  77. package/dist/server/services/websocket/quality-persistence.d.ts.map +1 -0
  78. package/dist/server/services/websocket/quality-persistence.js +187 -0
  79. package/dist/server/services/websocket/quality-persistence.js.map +1 -0
  80. package/dist/server/services/websocket/quality-service.d.ts +12 -2
  81. package/dist/server/services/websocket/quality-service.d.ts.map +1 -1
  82. package/dist/server/services/websocket/quality-service.js +162 -18
  83. package/dist/server/services/websocket/quality-service.js.map +1 -1
  84. package/dist/server/services/websocket/types.d.ts +2 -2
  85. package/dist/server/services/websocket/types.d.ts.map +1 -1
  86. package/package.json +3 -3
  87. package/server/cli/headless/claude-invoker.ts +25 -12
  88. package/server/cli/headless/headless-logger.ts +78 -0
  89. package/server/cli/headless/mcp-config.ts +6 -5
  90. package/server/cli/headless/runner.ts +4 -0
  91. package/server/cli/headless/stall-assessor.ts +131 -24
  92. package/server/cli/headless/tool-watchdog.ts +10 -9
  93. package/server/cli/headless/types.ts +10 -1
  94. package/server/cli/improvisation-session-manager.ts +118 -11
  95. package/server/mcp/bouncer-cli.ts +73 -0
  96. package/server/services/plan/composer.ts +199 -0
  97. package/server/services/plan/dependency-resolver.ts +182 -0
  98. package/server/services/plan/executor.ts +700 -0
  99. package/server/services/plan/parser.ts +491 -0
  100. package/server/services/plan/state-reconciler.ts +174 -0
  101. package/server/services/plan/types.ts +166 -0
  102. package/server/services/plan/watcher.ts +73 -0
  103. package/server/services/websocket/file-explorer-handlers.ts +20 -0
  104. package/server/services/websocket/handler.ts +21 -0
  105. package/server/services/websocket/plan-handlers.ts +592 -0
  106. package/server/services/websocket/quality-handlers.ts +450 -12
  107. package/server/services/websocket/quality-persistence.ts +250 -0
  108. package/server/services/websocket/quality-service.ts +183 -18
  109. package/server/services/websocket/types.ts +48 -2
@@ -2,10 +2,53 @@
2
2
  // Licensed under the MIT License. See LICENSE file for details.
3
3
 
4
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';
5
8
  import type { HandlerContext } from './handler-context.js';
6
- import { detectTools, installTools, runQualityScan } from './quality-service.js';
9
+ import { QualityPersistence } from './quality-persistence.js';
10
+ import { detectTools, installTools, recomputeWithAiReview, runQualityScan } from './quality-service.js';
7
11
  import type { WebSocketMessage, WSContext } from './types.js';
8
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
+
9
52
  export function handleQualityMessage(
10
53
  ctx: HandlerContext,
11
54
  ws: WSContext,
@@ -18,6 +61,9 @@ export function handleQualityMessage(
18
61
  qualityScan: () => handleScan(ctx, ws, msg, workingDir),
19
62
  qualityInstallTools: () => handleInstallTools(ctx, ws, msg, workingDir),
20
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),
21
67
  };
22
68
 
23
69
  const handler = handlers[msg.type];
@@ -40,6 +86,44 @@ function resolvePath(workingDir: string, dirPath?: string): string {
40
86
  return join(workingDir, dirPath);
41
87
  }
42
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
+
43
127
  async function handleDetectTools(
44
128
  ctx: HandlerContext,
45
129
  ws: WSContext,
@@ -71,16 +155,29 @@ async function handleScan(
71
155
  const reportPath = msg.data?.path || '.';
72
156
 
73
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
+
74
162
  const results = await runQualityScan(dirPath, (progress) => {
75
163
  ctx.send(ws, {
76
164
  type: 'qualityScanProgress',
77
165
  data: { path: reportPath, progress },
78
166
  });
79
- });
167
+ }, installedToolNames);
80
168
  ctx.send(ws, {
81
169
  type: 'qualityScanResults',
82
170
  data: { path: reportPath, results },
83
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
+ }
84
181
  } catch (error) {
85
182
  ctx.send(ws, {
86
183
  type: 'qualityError',
@@ -119,22 +216,363 @@ async function handleInstallTools(
119
216
  }
120
217
  }
121
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
+
122
355
  async function handleCodeReview(
123
356
  ctx: HandlerContext,
124
357
  ws: WSContext,
125
358
  msg: WebSocketMessage,
126
- _workingDir: string,
359
+ workingDir: string,
127
360
  ): Promise<void> {
361
+ const dirPath = resolvePath(workingDir, msg.data?.path);
128
362
  const reportPath = msg.data?.path || '.';
129
363
 
130
- // Code review via headless Claude will be implemented in a follow-up.
131
- // For now, send an empty result to unblock the UI.
132
- ctx.send(ws, {
133
- type: 'qualityCodeReview',
134
- data: {
135
- path: reportPath,
136
- findings: [],
137
- summary: 'Code review requires a Claude Code session. Run from the Chat view for a detailed code review.',
138
- },
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
+ // Recompute overall score with AI review findings included
416
+ let updatedResults: import('./quality-service.js').QualityResults | null = null;
417
+ try {
418
+ const persistence = getPersistence(workingDir);
419
+ const existingReport = persistence.loadReport(reportPath);
420
+ if (existingReport) {
421
+ updatedResults = recomputeWithAiReview(existingReport, findings);
422
+ updatedResults = { ...updatedResults, codeReview: findings as unknown as typeof updatedResults.codeReview };
423
+ persistence.saveReport(reportPath, updatedResults);
424
+ persistence.appendHistory(updatedResults, reportPath);
425
+ }
426
+ persistence.saveCodeReview(reportPath, findings as unknown as Record<string, unknown>[], summary);
427
+ } catch {
428
+ // Persistence failure should not break the review flow
429
+ }
430
+
431
+ ctx.send(ws, {
432
+ type: 'qualityCodeReview',
433
+ data: { path: reportPath, findings, summary, results: updatedResults },
434
+ });
435
+ } catch (error) {
436
+ ctx.send(ws, {
437
+ type: 'qualityError',
438
+ data: { path: reportPath, error: error instanceof Error ? error.message : String(error) },
439
+ });
440
+ } finally {
441
+ activeReviews.delete(dirPath);
442
+ }
443
+ }
444
+
445
+ // ============================================================================
446
+ // Fix Issues Agent
447
+ // ============================================================================
448
+
449
+ interface FindingForFix {
450
+ severity: string;
451
+ category: string;
452
+ file: string;
453
+ line: number | null;
454
+ title: string;
455
+ description: string;
456
+ suggestion?: string;
457
+ }
458
+
459
+ function buildFixPrompt(findings: FindingForFix[], section?: string): string {
460
+ const filtered = section ? findings.filter((f) => f.category === section) : findings;
461
+ const sorted = filtered.sort((a, b) => {
462
+ const order: Record<string, number> = { critical: 0, high: 1, medium: 2, low: 3 };
463
+ return (order[a.severity] ?? 4) - (order[b.severity] ?? 4);
139
464
  });
465
+
466
+ const issueList = sorted.slice(0, 30).map((f, i) => {
467
+ const loc = f.line ? `${f.file}:${f.line}` : f.file;
468
+ const parts = [`${i + 1}. [${f.severity.toUpperCase()}] ${loc} — ${f.title}`];
469
+ if (f.description) parts.push(` ${f.description}`);
470
+ if (f.suggestion) parts.push(` Suggestion: ${f.suggestion}`);
471
+ return parts.join('\n');
472
+ }).join('\n\n');
473
+
474
+ return `You are a code quality fix agent. Fix the following quality issues in the codebase.
475
+
476
+ ## Issues to Fix (${sorted.length} total, showing top ${Math.min(30, sorted.length)})
477
+
478
+ ${issueList}
479
+
480
+ ## Rules
481
+
482
+ - Fix each issue by editing the relevant file at the specified location.
483
+ - For complexity issues: refactor into smaller functions. For long files: split or extract modules. For long functions: break into smaller functions.
484
+ - For security issues: apply the suggested fix or use secure coding best practices.
485
+ - For bugs: fix the root cause, not just the symptom.
486
+ - For linting/formatting: apply the standard for the project.
487
+ - Do NOT introduce new issues. Make minimal, focused changes.
488
+ - After fixing, verify the changes compile/pass linting if tools are available.
489
+ - Work through the issues systematically from most to least severe.`;
490
+ }
491
+
492
+ const activeFixes = new Set<string>();
493
+
494
+ async function handleFixIssues(
495
+ ctx: HandlerContext,
496
+ ws: WSContext,
497
+ msg: WebSocketMessage,
498
+ workingDir: string,
499
+ ): Promise<void> {
500
+ const dirPath = resolvePath(workingDir, msg.data?.path);
501
+ const reportPath = msg.data?.path || '.';
502
+ const section: string | undefined = msg.data?.section;
503
+ const findings: FindingForFix[] = msg.data?.findings || [];
504
+
505
+ if (activeFixes.has(dirPath)) {
506
+ ctx.send(ws, {
507
+ type: 'qualityError',
508
+ data: { path: reportPath, error: 'A fix operation is already running for this directory.' },
509
+ });
510
+ return;
511
+ }
512
+
513
+ if (findings.length === 0) {
514
+ ctx.send(ws, {
515
+ type: 'qualityError',
516
+ data: { path: reportPath, error: 'No findings to fix.' },
517
+ });
518
+ return;
519
+ }
520
+
521
+ activeFixes.add(dirPath);
522
+ try {
523
+ ctx.send(ws, {
524
+ type: 'qualityFixProgress',
525
+ data: { path: reportPath, message: 'Starting Claude Code to fix issues...' },
526
+ });
527
+
528
+ const prompt = buildFixPrompt(findings, section);
529
+
530
+ const runner = new HeadlessRunner({
531
+ workingDir: dirPath,
532
+ directPrompt: prompt,
533
+ stallWarningMs: 120_000,
534
+ stallKillMs: 600_000,
535
+ stallHardCapMs: 900_000,
536
+ toolUseCallback: createToolProgressCallback(ctx, ws, reportPath),
537
+ });
538
+
539
+ await runWithFileLogger('code-review-fix', () => runner.run());
540
+
541
+ ctx.send(ws, {
542
+ type: 'qualityFixProgress',
543
+ data: { path: reportPath, message: 'Fixes applied. Re-running quality checks...' },
544
+ });
545
+
546
+ // Re-run quality scan after fixing
547
+ const { tools: detectedTools } = await detectTools(dirPath);
548
+ const installedToolNames = detectedTools.filter((t) => t.installed).map((t) => t.name);
549
+
550
+ const results = await runQualityScan(dirPath, (progress) => {
551
+ ctx.send(ws, {
552
+ type: 'qualityScanProgress',
553
+ data: { path: reportPath, progress },
554
+ });
555
+ }, installedToolNames);
556
+
557
+ ctx.send(ws, {
558
+ type: 'qualityFixComplete',
559
+ data: { path: reportPath, results },
560
+ });
561
+
562
+ // Persist
563
+ try {
564
+ const persistence = getPersistence(workingDir);
565
+ persistence.saveReport(reportPath, results);
566
+ persistence.appendHistory(results, reportPath);
567
+ } catch {
568
+ // Persistence failure should not break the fix flow
569
+ }
570
+ } catch (error) {
571
+ ctx.send(ws, {
572
+ type: 'qualityError',
573
+ data: { path: reportPath, error: error instanceof Error ? error.message : String(error) },
574
+ });
575
+ } finally {
576
+ activeFixes.delete(dirPath);
577
+ }
140
578
  }