ai-sdlc 0.2.0-alpha.6 → 0.2.0-alpha.60

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 (135) hide show
  1. package/README.md +53 -1058
  2. package/dist/agents/implementation.d.ts +36 -1
  3. package/dist/agents/implementation.d.ts.map +1 -1
  4. package/dist/agents/implementation.js +259 -30
  5. package/dist/agents/implementation.js.map +1 -1
  6. package/dist/agents/index.d.ts +2 -0
  7. package/dist/agents/index.d.ts.map +1 -1
  8. package/dist/agents/index.js +2 -0
  9. package/dist/agents/index.js.map +1 -1
  10. package/dist/agents/orchestrator.d.ts +61 -0
  11. package/dist/agents/orchestrator.d.ts.map +1 -0
  12. package/dist/agents/orchestrator.js +443 -0
  13. package/dist/agents/orchestrator.js.map +1 -0
  14. package/dist/agents/planning.d.ts +1 -1
  15. package/dist/agents/planning.d.ts.map +1 -1
  16. package/dist/agents/planning.js +55 -4
  17. package/dist/agents/planning.js.map +1 -1
  18. package/dist/agents/refinement.d.ts.map +1 -1
  19. package/dist/agents/refinement.js +22 -3
  20. package/dist/agents/refinement.js.map +1 -1
  21. package/dist/agents/research.d.ts +85 -1
  22. package/dist/agents/research.d.ts.map +1 -1
  23. package/dist/agents/research.js +506 -16
  24. package/dist/agents/research.js.map +1 -1
  25. package/dist/agents/review.d.ts +103 -2
  26. package/dist/agents/review.d.ts.map +1 -1
  27. package/dist/agents/review.js +777 -93
  28. package/dist/agents/review.js.map +1 -1
  29. package/dist/agents/rework.d.ts.map +1 -1
  30. package/dist/agents/rework.js +25 -4
  31. package/dist/agents/rework.js.map +1 -1
  32. package/dist/agents/single-task.d.ts +41 -0
  33. package/dist/agents/single-task.d.ts.map +1 -0
  34. package/dist/agents/single-task.js +357 -0
  35. package/dist/agents/single-task.js.map +1 -0
  36. package/dist/agents/state-assessor.d.ts +3 -3
  37. package/dist/agents/state-assessor.d.ts.map +1 -1
  38. package/dist/agents/state-assessor.js +6 -6
  39. package/dist/agents/state-assessor.js.map +1 -1
  40. package/dist/agents/test-pattern-detector.d.ts +49 -0
  41. package/dist/agents/test-pattern-detector.d.ts.map +1 -0
  42. package/dist/agents/test-pattern-detector.js +273 -0
  43. package/dist/agents/test-pattern-detector.js.map +1 -0
  44. package/dist/agents/verification.d.ts +11 -0
  45. package/dist/agents/verification.d.ts.map +1 -1
  46. package/dist/agents/verification.js +99 -12
  47. package/dist/agents/verification.js.map +1 -1
  48. package/dist/cli/commands/migrate.js +1 -1
  49. package/dist/cli/commands/migrate.js.map +1 -1
  50. package/dist/cli/commands.d.ts +66 -3
  51. package/dist/cli/commands.d.ts.map +1 -1
  52. package/dist/cli/commands.js +1548 -198
  53. package/dist/cli/commands.js.map +1 -1
  54. package/dist/cli/daemon.d.ts.map +1 -1
  55. package/dist/cli/daemon.js +25 -3
  56. package/dist/cli/daemon.js.map +1 -1
  57. package/dist/cli/runner.d.ts.map +1 -1
  58. package/dist/cli/runner.js +35 -12
  59. package/dist/cli/runner.js.map +1 -1
  60. package/dist/core/auth.d.ts +43 -0
  61. package/dist/core/auth.d.ts.map +1 -1
  62. package/dist/core/auth.js +105 -1
  63. package/dist/core/auth.js.map +1 -1
  64. package/dist/core/client.d.ts +25 -1
  65. package/dist/core/client.d.ts.map +1 -1
  66. package/dist/core/client.js +247 -7
  67. package/dist/core/client.js.map +1 -1
  68. package/dist/core/config.d.ts +32 -1
  69. package/dist/core/config.d.ts.map +1 -1
  70. package/dist/core/config.js +146 -3
  71. package/dist/core/config.js.map +1 -1
  72. package/dist/core/conflict-detector.d.ts +108 -0
  73. package/dist/core/conflict-detector.d.ts.map +1 -0
  74. package/dist/core/conflict-detector.js +413 -0
  75. package/dist/core/conflict-detector.js.map +1 -0
  76. package/dist/core/git-utils.d.ts +28 -0
  77. package/dist/core/git-utils.d.ts.map +1 -0
  78. package/dist/core/git-utils.js +146 -0
  79. package/dist/core/git-utils.js.map +1 -0
  80. package/dist/core/index.d.ts +19 -0
  81. package/dist/core/index.d.ts.map +1 -0
  82. package/dist/core/index.js +19 -0
  83. package/dist/core/index.js.map +1 -0
  84. package/dist/core/kanban.d.ts +1 -1
  85. package/dist/core/kanban.d.ts.map +1 -1
  86. package/dist/core/kanban.js +3 -3
  87. package/dist/core/kanban.js.map +1 -1
  88. package/dist/core/llm-utils.d.ts +103 -0
  89. package/dist/core/llm-utils.d.ts.map +1 -0
  90. package/dist/core/llm-utils.js +368 -0
  91. package/dist/core/llm-utils.js.map +1 -0
  92. package/dist/core/logger.d.ts +92 -0
  93. package/dist/core/logger.d.ts.map +1 -0
  94. package/dist/core/logger.js +221 -0
  95. package/dist/core/logger.js.map +1 -0
  96. package/dist/core/process-manager.d.ts +15 -0
  97. package/dist/core/process-manager.d.ts.map +1 -0
  98. package/dist/core/process-manager.js +132 -0
  99. package/dist/core/process-manager.js.map +1 -0
  100. package/dist/core/story-logger.d.ts +102 -0
  101. package/dist/core/story-logger.d.ts.map +1 -0
  102. package/dist/core/story-logger.js +265 -0
  103. package/dist/core/story-logger.js.map +1 -0
  104. package/dist/core/story.d.ts +113 -20
  105. package/dist/core/story.d.ts.map +1 -1
  106. package/dist/core/story.js +328 -40
  107. package/dist/core/story.js.map +1 -1
  108. package/dist/core/task-parser.d.ts +59 -0
  109. package/dist/core/task-parser.d.ts.map +1 -0
  110. package/dist/core/task-parser.js +235 -0
  111. package/dist/core/task-parser.js.map +1 -0
  112. package/dist/core/task-progress.d.ts +92 -0
  113. package/dist/core/task-progress.d.ts.map +1 -0
  114. package/dist/core/task-progress.js +280 -0
  115. package/dist/core/task-progress.js.map +1 -0
  116. package/dist/core/workflow-state.d.ts +45 -6
  117. package/dist/core/workflow-state.d.ts.map +1 -1
  118. package/dist/core/workflow-state.js +201 -12
  119. package/dist/core/workflow-state.js.map +1 -1
  120. package/dist/core/worktree.d.ts +186 -0
  121. package/dist/core/worktree.d.ts.map +1 -0
  122. package/dist/core/worktree.js +554 -0
  123. package/dist/core/worktree.js.map +1 -0
  124. package/dist/index.js +145 -5
  125. package/dist/index.js.map +1 -1
  126. package/dist/services/error-classifier.d.ts +119 -0
  127. package/dist/services/error-classifier.d.ts.map +1 -0
  128. package/dist/services/error-classifier.js +182 -0
  129. package/dist/services/error-classifier.js.map +1 -0
  130. package/dist/types/index.d.ts +381 -1
  131. package/dist/types/index.d.ts.map +1 -1
  132. package/dist/types/index.js +1 -0
  133. package/dist/types/index.js.map +1 -1
  134. package/package.json +5 -2
  135. package/templates/story.md +5 -0
@@ -1,12 +1,16 @@
1
- import { execSync, spawn } from 'child_process';
1
+ import { execSync, spawn, spawnSync } from 'child_process';
2
2
  import path from 'path';
3
3
  import fs from 'fs';
4
4
  import { z } from 'zod';
5
- import { parseStory, updateStoryStatus, appendToSection, updateStoryField, isAtMaxRetries, appendReviewHistory, snapshotMaxRetries, getEffectiveMaxRetries } from '../core/story.js';
5
+ import { ProcessManager } from '../core/process-manager.js';
6
+ import { parseStory, updateStoryStatus, appendToSection, updateStoryField, isAtMaxRetries, appendReviewHistory, snapshotMaxRetries, getEffectiveMaxRetries, getEffectiveMaxImplementationRetries } from '../core/story.js';
6
7
  import { runAgentQuery } from '../core/client.js';
8
+ import { getLogger } from '../core/logger.js';
7
9
  import { loadConfig, DEFAULT_TIMEOUTS } from '../core/config.js';
10
+ import { extractStructuredResponseSync } from '../core/llm-utils.js';
8
11
  import { ReviewDecision, ReviewSeverity } from '../types/index.js';
9
12
  import { sanitizeInput, truncateText } from '../cli/formatting.js';
13
+ import { detectTestDuplicationPatterns } from './test-pattern-detector.js';
10
14
  /**
11
15
  * Security: Validate Git branch name to prevent command injection
12
16
  * Only allows alphanumeric characters, hyphens, underscores, and forward slashes
@@ -93,7 +97,9 @@ const ReviewIssueSchema = z.object({
93
97
  // This handles LLM responses that return {"line": null} instead of omitting the field
94
98
  file: z.string().nullish().transform(v => v ?? undefined),
95
99
  line: z.number().int().positive().nullish().transform(v => v ?? undefined),
96
- suggestedFix: z.string().max(2000).nullish().transform(v => v ?? undefined),
100
+ suggestedFix: z.string().max(5000).nullish().transform(v => v ?? undefined),
101
+ // Perspectives field for unified review (optional for backward compatibility)
102
+ perspectives: z.array(z.enum(['code', 'security', 'po'])).optional(),
97
103
  });
98
104
  const ReviewResponseSchema = z.object({
99
105
  passed: z.boolean(),
@@ -168,6 +174,7 @@ async function runCommandAsync(command, workingDir, timeout, onProgress) {
168
174
  cwd: workingDir,
169
175
  stdio: ['pipe', 'pipe', 'pipe'],
170
176
  });
177
+ ProcessManager.getInstance().registerChild(child);
171
178
  const timeoutId = setTimeout(() => {
172
179
  killed = true;
173
180
  child.kill('SIGTERM');
@@ -248,23 +255,138 @@ Output your review as a JSON object with this structure:
248
255
  "issues": [
249
256
  {
250
257
  "severity": "blocker" | "critical" | "major" | "minor",
251
- "category": "code_quality" | "security" | "requirements" | "testing" | etc,
258
+ "category": "code_quality" | "security" | "requirements" | "testing" | "test_alignment" | etc,
252
259
  "description": "Detailed description of the issue",
253
260
  "file": "path/to/file.ts" (if applicable),
254
261
  "line": 42 (if applicable),
255
- "suggestedFix": "How to fix this issue"
262
+ "suggestedFix": "How to fix this issue",
263
+ "perspectives": ["code", "security", "po"] (which perspectives this issue relates to)
256
264
  }
257
265
  ]
258
266
  }
259
267
 
260
268
  Severity guidelines:
261
- - blocker: Must be fixed before merging (security holes, broken functionality)
269
+ - blocker: Must be fixed before merging (security holes, broken functionality, test misalignment)
262
270
  - critical: Should be fixed before merging (major bugs, poor practices)
263
271
  - major: Should be addressed soon (code quality, maintainability)
264
272
  - minor: Nice to have improvements (style, optimizations)
265
273
 
266
274
  If no issues found, return: {"passed": true, "issues": []}
267
275
  `;
276
+ /**
277
+ * Unified Review Prompt - combines code, security, and product owner perspectives
278
+ * into a single collaborative review to eliminate duplicate issues.
279
+ */
280
+ const UNIFIED_REVIEW_PROMPT = `You are a senior engineering team conducting a comprehensive collaborative review.
281
+
282
+ You must evaluate the implementation from THREE perspectives simultaneously, but produce ONE unified set of issues:
283
+
284
+ ## Perspective 1: Code Quality (Senior Developer)
285
+ Evaluate:
286
+ - Code quality and maintainability
287
+ - Following best practices and design patterns
288
+ - Potential bugs or logic errors
289
+ - Test coverage adequacy and test quality
290
+ - Error handling completeness
291
+ - Performance considerations
292
+
293
+ ## Perspective 2: Security (Security Engineer)
294
+ Evaluate:
295
+ - OWASP Top 10 vulnerabilities
296
+ - Input validation and sanitization
297
+ - Authentication and authorization issues
298
+ - Data exposure risks
299
+ - Command injection vulnerabilities
300
+ - Secure coding practices
301
+
302
+ ## Perspective 3: Requirements (Product Owner)
303
+ Evaluate:
304
+ - Does it meet the acceptance criteria stated in the story?
305
+ - Is the user experience appropriate and intuitive?
306
+ - Are edge cases and error scenarios handled?
307
+ - Is documentation adequate for users and maintainers?
308
+ - Does the implementation align with the story goals?
309
+
310
+ ## Test-Implementation Alignment (BLOCKER category)
311
+
312
+ **CRITICAL PRE-REVIEW REQUIREMENT**: Tests have already been executed and passed. However, passing tests don't guarantee correctness if they verify outdated behavior.
313
+
314
+ During code review, you MUST verify test alignment:
315
+
316
+ 1. **For each changed production file, identify its test file**
317
+ - Check if tests exist for modified functions/modules
318
+ - Read the test assertions carefully
319
+
320
+ 2. **Verify tests match NEW behavior, not OLD**
321
+ - Do test assertions expect the current implementation behavior?
322
+ - If production code changed from sync to async, do tests use await?
323
+ - If function signature changed, do tests call it correctly?
324
+ - If return values changed, do tests expect the new values?
325
+
326
+ 3. **Flag misalignment as BLOCKER**
327
+ - If tests reference changed code but still expect old behavior:
328
+ - This is a **BLOCKER** severity issue
329
+ - Category MUST be: \`"test_alignment"\`
330
+ - Specify which test files need updating and why
331
+ - Provide example of correct assertion for new behavior
332
+
333
+ **Example of misaligned test (BLOCKER):**
334
+ \`\`\`typescript
335
+ // Production code changed from sync to async
336
+ async function loadConfig(): Promise<Config> {
337
+ return await fetchConfig();
338
+ }
339
+
340
+ // Test still expects sync behavior - MISSING await (BLOCKER)
341
+ test('loads config', () => {
342
+ const config = loadConfig(); // ❌ Missing await! Returns Promise<Config>, not Config
343
+ expect(config.port).toBe(3000); // ❌ Checking Promise.port, not config.port
344
+ });
345
+
346
+ // Correct aligned test:
347
+ test('loads config', async () => {
348
+ const config = await loadConfig(); // ✅ Awaits async function
349
+ expect(config.port).toBe(3000); // ✅ Checks actual config
350
+ });
351
+ \`\`\`
352
+
353
+ **When to flag test_alignment issues:**
354
+ - Tests verify old function signatures that no longer exist
355
+ - Tests expect old return value formats that changed
356
+ - Tests miss new error conditions introduced
357
+ - Tests pass but don't exercise the new code paths
358
+ - Mock expectations don't match the new implementation calls
359
+
360
+ ## CRITICAL DEDUPLICATION INSTRUCTIONS:
361
+
362
+ 1. **DO NOT repeat the same underlying issue from different perspectives**
363
+ - If multiple perspectives notice the same problem, list it ONCE
364
+ - Use the \`perspectives\` array to indicate which perspectives it affects
365
+
366
+ 2. **Prioritize by actual impact, not by how many perspectives notice it**
367
+ - A issue seen by all 3 perspectives is still just ONE issue
368
+ - Focus on the distinct, actionable problems that need fixing
369
+
370
+ 3. **If the fundamental problem is "no implementation exists" or "functionality completely missing":**
371
+ - Report this as ONE blocker issue, not three separate issues
372
+ - Use perspectives: ["code", "security", "po"] to show all perspectives agree
373
+
374
+ 4. **Combine related issues into single, comprehensive descriptions:**
375
+ - Instead of: "No tests" (code) + "Untested security" (security) + "No validation tests" (po)
376
+ - Write: "No tests exist for the implementation" with perspectives: ["code", "security", "po"]
377
+
378
+ 5. **Each issue should have a clear, single suggested fix**
379
+ - Avoid vague suggestions like "improve everything"
380
+ - Be specific and actionable
381
+
382
+ ${REVIEW_OUTPUT_FORMAT}
383
+
384
+ Remember: Your goal is to produce a clean, deduplicated list of actual distinct problems, not to maximize issue count.`;
385
+ /**
386
+ * Legacy prompts - kept for reference only
387
+ * @deprecated These are replaced by UNIFIED_REVIEW_PROMPT which combines all three perspectives.
388
+ * The unified prompt reduces LLM calls from 3 to 1 and eliminates duplicate issues.
389
+ */
268
390
  const CODE_REVIEW_PROMPT = `You are a senior code reviewer. Review the implementation for:
269
391
  1. Code quality and maintainability
270
392
  2. Following best practices
@@ -272,6 +394,9 @@ const CODE_REVIEW_PROMPT = `You are a senior code reviewer. Review the implement
272
394
  4. Test coverage adequacy
273
395
 
274
396
  ${REVIEW_OUTPUT_FORMAT}`;
397
+ /**
398
+ * @deprecated Use UNIFIED_REVIEW_PROMPT instead
399
+ */
275
400
  const SECURITY_REVIEW_PROMPT = `You are a security specialist. Review the implementation for:
276
401
  1. OWASP Top 10 vulnerabilities
277
402
  2. Input validation issues
@@ -279,6 +404,9 @@ const SECURITY_REVIEW_PROMPT = `You are a security specialist. Review the implem
279
404
  4. Data exposure risks
280
405
 
281
406
  ${REVIEW_OUTPUT_FORMAT}`;
407
+ /**
408
+ * @deprecated Use UNIFIED_REVIEW_PROMPT instead
409
+ */
282
410
  const PO_REVIEW_PROMPT = `You are a product owner validating the implementation. Check:
283
411
  1. Does it meet the acceptance criteria?
284
412
  2. Is the user experience appropriate?
@@ -288,26 +416,25 @@ const PO_REVIEW_PROMPT = `You are a product owner validating the implementation.
288
416
  ${REVIEW_OUTPUT_FORMAT}`;
289
417
  /**
290
418
  * Parse review response and extract structured issues
419
+ * Uses extractStructuredResponseSync for robust parsing with multiple strategies:
420
+ * 1. Direct JSON parse
421
+ * 2. JSON within markdown code blocks
422
+ * 3. JSON with leading/trailing text stripped
423
+ * 4. YAML format fallback
424
+ *
291
425
  * Security: Uses zod schema validation to prevent malicious JSON
292
426
  */
293
427
  function parseReviewResponse(response, reviewType) {
294
- try {
295
- // Try to extract JSON from the response
296
- const jsonMatch = response.match(/\{[\s\S]*\}/);
297
- if (!jsonMatch) {
298
- // Fallback: no JSON found, analyze text
299
- return parseTextReview(response, reviewType);
300
- }
301
- const parsed = JSON.parse(jsonMatch[0]);
302
- // Security: Validate against zod schema before using the data
303
- const validationResult = ReviewResponseSchema.safeParse(parsed);
304
- if (!validationResult.success) {
305
- // Log validation errors for debugging
306
- console.warn('Review response failed schema validation:', validationResult.error);
307
- // Fallback to text analysis
308
- return parseTextReview(response, reviewType);
309
- }
310
- const validated = validationResult.data;
428
+ const logger = getLogger();
429
+ // Use the robust extraction utility with all strategies
430
+ const extractionResult = extractStructuredResponseSync(response, ReviewResponseSchema, false);
431
+ if (extractionResult.success && extractionResult.data) {
432
+ const validated = extractionResult.data;
433
+ logger.debug('review', `Successfully parsed review response using strategy: ${extractionResult.strategy}`, {
434
+ reviewType,
435
+ strategy: extractionResult.strategy,
436
+ issueCount: validated.issues.length,
437
+ });
311
438
  // Map validated data to ReviewIssue format (additional sanitization)
312
439
  const issues = validated.issues.map((issue) => ({
313
440
  severity: issue.severity,
@@ -316,17 +443,20 @@ function parseReviewResponse(response, reviewType) {
316
443
  file: issue.file,
317
444
  line: issue.line,
318
445
  suggestedFix: issue.suggestedFix,
446
+ perspectives: issue.perspectives,
319
447
  }));
320
448
  return {
321
449
  passed: validated.passed !== false && issues.filter(i => i.severity === 'blocker' || i.severity === 'critical').length === 0,
322
450
  issues,
323
451
  };
324
452
  }
325
- catch (error) {
326
- // Fallback to text analysis if JSON parsing fails
327
- console.warn('Review response parsing error:', error);
328
- return parseTextReview(response, reviewType);
329
- }
453
+ // All extraction strategies failed - log raw response for debugging and use text fallback
454
+ logger.warn('review', 'All extraction strategies failed for review response', {
455
+ reviewType,
456
+ error: extractionResult.error,
457
+ responsePreview: response.substring(0, 200),
458
+ });
459
+ return parseTextReview(response, reviewType);
330
460
  }
331
461
  /**
332
462
  * Fallback: Parse text-based review response (for when LLM doesn't return JSON)
@@ -383,8 +513,35 @@ function determineReviewSeverity(issues) {
383
513
  return ReviewSeverity.LOW;
384
514
  }
385
515
  }
516
+ /**
517
+ * Derive individual perspective pass/fail status from issues
518
+ *
519
+ * For backward compatibility with ReviewAttempt structure, determines whether
520
+ * each perspective (code, security, po) would pass based on issues flagged
521
+ * for that perspective.
522
+ *
523
+ * A perspective fails if it has any blocker or critical issues.
524
+ *
525
+ * @param issues - Array of review issues with perspectives field
526
+ * @returns Object with pass/fail status for each perspective
527
+ */
528
+ export function deriveIndividualPassFailFromPerspectives(issues) {
529
+ // Check if any blocker/critical issues exist for each perspective
530
+ const codeIssues = issues.filter(i => i.perspectives?.includes('code') &&
531
+ (i.severity === 'blocker' || i.severity === 'critical'));
532
+ const securityIssues = issues.filter(i => i.perspectives?.includes('security') &&
533
+ (i.severity === 'blocker' || i.severity === 'critical'));
534
+ const poIssues = issues.filter(i => i.perspectives?.includes('po') &&
535
+ (i.severity === 'blocker' || i.severity === 'critical'));
536
+ return {
537
+ codeReviewPassed: codeIssues.length === 0,
538
+ securityReviewPassed: securityIssues.length === 0,
539
+ poReviewPassed: poIssues.length === 0,
540
+ };
541
+ }
386
542
  /**
387
543
  * Aggregate issues from multiple reviews and determine overall pass/fail
544
+ * @deprecated No longer used with unified review. Kept for reference only.
388
545
  */
389
546
  function aggregateReviews(codeResult, securityResult, poResult) {
390
547
  const allIssues = [...codeResult.issues, ...securityResult.issues, ...poResult.issues];
@@ -399,6 +556,7 @@ function aggregateReviews(codeResult, securityResult, poResult) {
399
556
  }
400
557
  /**
401
558
  * Format issues for display in review notes
559
+ * Shows perspectives (code, security, po) when available
402
560
  */
403
561
  function formatIssuesForDisplay(issues) {
404
562
  if (issues.length === 0) {
@@ -417,7 +575,11 @@ function formatIssuesForDisplay(issues) {
417
575
  const icon = severity === 'blocker' ? '🛑' : severity === 'critical' ? '⚠️' : severity === 'major' ? '📋' : 'ℹ️';
418
576
  output += `\n#### ${icon} ${severity.toUpperCase()} (${issueList.length})\n\n`;
419
577
  for (const issue of issueList) {
420
- output += `**${issue.category}**: ${issue.description}\n`;
578
+ // Format perspectives indicator if present
579
+ const perspectivesTag = issue.perspectives && issue.perspectives.length > 0
580
+ ? ` [${issue.perspectives.join(', ')}]`
581
+ : '';
582
+ output += `**${issue.category}**${perspectivesTag}: ${issue.description}\n`;
421
583
  if (issue.file) {
422
584
  output += ` - File: \`${issue.file}\`${issue.line ? `:${issue.line}` : ''}\n`;
423
585
  }
@@ -429,6 +591,162 @@ function formatIssuesForDisplay(issues) {
429
591
  }
430
592
  return output;
431
593
  }
594
+ /**
595
+ * Get source code changes from git diff
596
+ *
597
+ * Returns list of source files that have been modified (excludes tests and story files).
598
+ * Uses spawnSync for security (prevents command injection).
599
+ *
600
+ * @param workingDir - Working directory to run git diff in
601
+ * @returns Array of source file paths that have changed, or ['unknown'] if git fails
602
+ */
603
+ export function getSourceCodeChanges(workingDir) {
604
+ try {
605
+ // Security: Use spawnSync with explicit args (not shell) to prevent injection
606
+ const result = spawnSync('git', ['diff', '--name-only', 'HEAD~1'], {
607
+ cwd: workingDir,
608
+ encoding: 'utf-8',
609
+ stdio: ['ignore', 'pipe', 'pipe'],
610
+ });
611
+ if (result.status !== 0) {
612
+ // Git command failed - fail open (assume changes exist)
613
+ return ['unknown'];
614
+ }
615
+ const output = result.stdout.toString();
616
+ return output
617
+ .split('\n')
618
+ .filter(f => f.trim())
619
+ .filter(f => /\.(ts|tsx|js|jsx)$/.test(f)) // Source files only
620
+ .filter(f => !f.includes('.test.')) // Exclude test files
621
+ .filter(f => !f.includes('.spec.')) // Exclude spec files
622
+ .filter(f => !f.startsWith('.ai-sdlc/')); // Exclude story files
623
+ }
624
+ catch {
625
+ // If git diff fails, assume there are changes (fail open, not closed)
626
+ return ['unknown'];
627
+ }
628
+ }
629
+ /**
630
+ * Get configuration file changes from git diff
631
+ *
632
+ * Detects changes to configuration files including:
633
+ * - .claude/ directory (Agent SDK skills, CLAUDE.md)
634
+ * - .github/ directory (workflows, actions, issue templates)
635
+ * - Root config files (tsconfig.json, package.json, .gitignore, vitest.config.ts, etc.)
636
+ *
637
+ * Uses spawnSync for security (prevents command injection).
638
+ *
639
+ * @param workingDir - Working directory to run git diff in
640
+ * @returns Array of configuration file paths that have changed, or ['unknown'] if git fails
641
+ */
642
+ export function getConfigurationChanges(workingDir) {
643
+ try {
644
+ // Security: Use spawnSync with explicit args (not shell) to prevent injection
645
+ const result = spawnSync('git', ['diff', '--name-only', 'HEAD~1'], {
646
+ cwd: workingDir,
647
+ encoding: 'utf-8',
648
+ stdio: ['ignore', 'pipe', 'pipe'],
649
+ });
650
+ if (result.status !== 0) {
651
+ // Git command failed - fail open (assume changes exist)
652
+ return ['unknown'];
653
+ }
654
+ const output = result.stdout.toString();
655
+ return output
656
+ .split('\n')
657
+ .filter(f => f.trim())
658
+ .filter(f => {
659
+ // Configuration directories
660
+ if (f.startsWith('.claude/'))
661
+ return true;
662
+ if (f.startsWith('.github/'))
663
+ return true;
664
+ // Root configuration files (common patterns)
665
+ const rootConfigs = [
666
+ 'tsconfig.json',
667
+ 'package.json',
668
+ 'package-lock.json',
669
+ '.gitignore',
670
+ '.gitattributes',
671
+ 'vitest.config.ts',
672
+ 'vitest.config.js',
673
+ 'jest.config.js',
674
+ 'jest.config.ts',
675
+ '.eslintrc',
676
+ '.eslintrc.js',
677
+ '.eslintrc.json',
678
+ '.prettierrc',
679
+ '.prettierrc.js',
680
+ '.prettierrc.json',
681
+ 'Makefile',
682
+ 'Dockerfile',
683
+ 'docker-compose.yml',
684
+ '.env.example',
685
+ ];
686
+ return rootConfigs.includes(f);
687
+ });
688
+ }
689
+ catch {
690
+ // If git diff fails, assume there are changes (fail open, not closed)
691
+ return ['unknown'];
692
+ }
693
+ }
694
+ /**
695
+ * Determine the effective content type for validation
696
+ *
697
+ * Resolves the final content type based on story frontmatter fields:
698
+ * 1. If requires_source_changes === false, treat as 'configuration'
699
+ * 2. If requires_source_changes === true, treat as 'code'
700
+ * 3. Otherwise, use content_type field (default: 'code' for backward compatibility)
701
+ *
702
+ * @param story - Story with frontmatter to analyze
703
+ * @returns The effective content type to use for validation
704
+ */
705
+ export function determineEffectiveContentType(story) {
706
+ const frontmatter = story.frontmatter;
707
+ // Manual override takes precedence
708
+ if (frontmatter.requires_source_changes === false) {
709
+ return 'configuration';
710
+ }
711
+ if (frontmatter.requires_source_changes === true) {
712
+ return 'code';
713
+ }
714
+ // Use explicit content_type or default to 'code'
715
+ return frontmatter.content_type || 'code';
716
+ }
717
+ /**
718
+ * Check if test files exist in git diff
719
+ *
720
+ * Returns true if any test files have been modified/added, false otherwise.
721
+ * Uses spawnSync for security (prevents command injection).
722
+ *
723
+ * @param workingDir - Working directory to run git diff in
724
+ * @returns True if test files exist in changes, false otherwise
725
+ */
726
+ export function hasTestFiles(workingDir) {
727
+ try {
728
+ // Security: Use spawnSync with explicit args (not shell) to prevent injection
729
+ const result = spawnSync('git', ['diff', '--name-only', 'HEAD~1'], {
730
+ cwd: workingDir,
731
+ encoding: 'utf-8',
732
+ stdio: ['ignore', 'pipe', 'pipe'],
733
+ });
734
+ if (result.status !== 0) {
735
+ // Git command failed - fail open (assume tests exist to avoid false blocks)
736
+ return true;
737
+ }
738
+ const output = result.stdout.toString();
739
+ const files = output.split('\n').filter(f => f.trim());
740
+ // Check if any files match test patterns
741
+ return files.some(f => f.includes('.test.') ||
742
+ f.includes('.spec.') ||
743
+ f.includes('__tests__/'));
744
+ }
745
+ catch {
746
+ // If git diff fails, assume tests exist (fail open, not closed)
747
+ return true;
748
+ }
749
+ }
432
750
  /**
433
751
  * Generate executive summary from review issues (1-3 sentences)
434
752
  *
@@ -526,9 +844,15 @@ export function generateReviewSummary(issues, terminalWidth) {
526
844
  * Now returns structured ReviewResult with pass/fail and issues.
527
845
  */
528
846
  export async function runReviewAgent(storyPath, sdlcRoot, options) {
847
+ const logger = getLogger();
848
+ const startTime = Date.now();
529
849
  const story = parseStory(storyPath);
530
850
  const changesMade = [];
531
851
  const workingDir = path.dirname(sdlcRoot);
852
+ logger.info('review', 'Starting review phase', {
853
+ storyId: story.frontmatter.id,
854
+ retryCount: story.frontmatter.retry_count || 0,
855
+ });
532
856
  // Security: Validate working directory before any operations
533
857
  try {
534
858
  validateWorkingDirectory(workingDir);
@@ -554,14 +878,14 @@ export async function runReviewAgent(storyPath, sdlcRoot, options) {
554
878
  const config = loadConfig(workingDir);
555
879
  try {
556
880
  // Snapshot max_retries from config (protects against mid-cycle config changes)
557
- snapshotMaxRetries(story, config);
881
+ await snapshotMaxRetries(story, config);
558
882
  // Check if story has reached max retries
559
883
  if (isAtMaxRetries(story, config)) {
560
884
  const retryCount = story.frontmatter.retry_count || 0;
561
885
  const maxRetries = getEffectiveMaxRetries(story, config);
562
886
  const maxRetriesDisplay = Number.isFinite(maxRetries) ? maxRetries : '∞';
563
887
  const errorMsg = `Story has reached maximum retry limit (${retryCount}/${maxRetriesDisplay}). Manual intervention required.`;
564
- updateStoryField(story, 'last_error', errorMsg);
888
+ await updateStoryField(story, 'last_error', errorMsg);
565
889
  changesMade.push(errorMsg);
566
890
  return {
567
891
  success: false,
@@ -579,6 +903,167 @@ export async function runReviewAgent(storyPath, sdlcRoot, options) {
579
903
  feedback: errorMsg,
580
904
  };
581
905
  }
906
+ // PRE-CHECK GATE: Content type-aware validation before running expensive LLM reviews
907
+ const contentType = determineEffectiveContentType(story);
908
+ logger.info('review', 'Running content-type-specific validation', {
909
+ storyId: story.frontmatter.id,
910
+ contentType,
911
+ explicitContentType: story.frontmatter.content_type,
912
+ requiresSourceChanges: story.frontmatter.requires_source_changes,
913
+ });
914
+ // Validation flags
915
+ let validationFailed = false;
916
+ let validationReason = '';
917
+ let validationCategory = 'implementation';
918
+ // Check source code changes for 'code' and 'mixed' types
919
+ if (contentType === 'code' || contentType === 'mixed') {
920
+ const sourceChanges = getSourceCodeChanges(workingDir);
921
+ if (sourceChanges.length === 0) {
922
+ validationFailed = true;
923
+ validationReason = contentType === 'mixed'
924
+ ? 'Mixed story requires both source AND configuration changes - no source code was modified.'
925
+ : 'Implementation wrote documentation/planning only - no source code was modified.';
926
+ logger.warn('review', 'Source code validation failed', {
927
+ storyId: story.frontmatter.id,
928
+ contentType,
929
+ sourceChangesFound: sourceChanges.length,
930
+ });
931
+ }
932
+ else {
933
+ logger.info('review', 'Source code changes detected', {
934
+ storyId: story.frontmatter.id,
935
+ fileCount: sourceChanges.length,
936
+ });
937
+ }
938
+ }
939
+ // Check configuration changes for 'configuration' and 'mixed' types
940
+ if (!validationFailed && (contentType === 'configuration' || contentType === 'mixed')) {
941
+ const configChanges = getConfigurationChanges(workingDir);
942
+ if (configChanges.length === 0) {
943
+ validationFailed = true;
944
+ validationReason = contentType === 'mixed'
945
+ ? 'Mixed story requires both source AND configuration changes. No configuration file changes detected.'
946
+ : 'Configuration story requires changes to config files (.claude/, .github/, or root config files). No configuration changes detected.';
947
+ logger.warn('review', 'Configuration validation failed', {
948
+ storyId: story.frontmatter.id,
949
+ contentType,
950
+ configChangesFound: configChanges.length,
951
+ });
952
+ }
953
+ else {
954
+ logger.info('review', 'Configuration changes detected', {
955
+ storyId: story.frontmatter.id,
956
+ fileCount: configChanges.length,
957
+ });
958
+ }
959
+ }
960
+ // For 'documentation' type, skip all file change validation
961
+ if (contentType === 'documentation') {
962
+ logger.info('review', 'Documentation story - skipping file change validation', {
963
+ storyId: story.frontmatter.id,
964
+ });
965
+ }
966
+ // Handle validation failure (if any)
967
+ if (validationFailed) {
968
+ const retryCount = story.frontmatter.implementation_retry_count || 0;
969
+ const maxRetries = getEffectiveMaxImplementationRetries(story, config);
970
+ if (retryCount < maxRetries) {
971
+ // RECOVERABLE: Trigger implementation recovery
972
+ logger.warn('review', 'Validation failed - triggering implementation recovery', {
973
+ storyId: story.frontmatter.id,
974
+ retryCount,
975
+ maxRetries,
976
+ contentType,
977
+ });
978
+ await updateStoryField(story, 'implementation_complete', false);
979
+ // Set restart reason (backward compatible message for default code stories)
980
+ const restartReason = contentType === 'configuration'
981
+ ? 'Configuration story requires changes to config files (.claude/, .github/, or root config files). No configuration changes detected.'
982
+ : contentType === 'mixed'
983
+ ? 'Mixed story requires both source AND configuration changes - no source code was modified.'
984
+ : 'No source code changes detected. Implementation wrote documentation only.';
985
+ await updateStoryField(story, 'last_restart_reason', restartReason);
986
+ // Create user-friendly recovery description
987
+ const recoveryDescription = contentType === 'configuration'
988
+ ? 'No configuration file modifications detected. Re-running implementation phase.'
989
+ : contentType === 'mixed'
990
+ ? 'No source code modifications detected. Re-running implementation phase.'
991
+ : 'No source code modifications detected. Re-running implementation phase.';
992
+ return {
993
+ success: true,
994
+ story: parseStory(storyPath),
995
+ changesMade: ['Detected incomplete implementation', 'Triggered implementation recovery'],
996
+ passed: false,
997
+ decision: ReviewDecision.RECOVERY,
998
+ reviewType: 'pre-check',
999
+ issues: [{
1000
+ severity: 'critical',
1001
+ category: validationCategory,
1002
+ description: recoveryDescription,
1003
+ }],
1004
+ feedback: `Implementation recovery triggered - ${validationReason}`,
1005
+ };
1006
+ }
1007
+ else {
1008
+ // NON-RECOVERABLE: Max retries reached
1009
+ const maxRetriesDisplay = Number.isFinite(maxRetries) ? maxRetries : '∞';
1010
+ logger.error('review', 'Validation failed and max implementation retries reached', {
1011
+ storyId: story.frontmatter.id,
1012
+ retryCount,
1013
+ maxRetries,
1014
+ contentType,
1015
+ });
1016
+ return {
1017
+ success: true,
1018
+ story: parseStory(storyPath),
1019
+ changesMade: ['Detected incomplete implementation', 'Max retries reached'],
1020
+ passed: false,
1021
+ decision: ReviewDecision.FAILED,
1022
+ severity: ReviewSeverity.CRITICAL,
1023
+ reviewType: 'pre-check',
1024
+ issues: [{
1025
+ severity: 'blocker',
1026
+ category: validationCategory,
1027
+ description: `${validationReason} This has occurred ${retryCount} time(s) (max: ${maxRetriesDisplay}). Manual intervention required.`,
1028
+ suggestedFix: 'Review the story requirements and implementation plan. Verify the content_type field matches the expected implementation. Consider simplifying the story or providing more explicit guidance.',
1029
+ }],
1030
+ feedback: 'Implementation failed validation after multiple attempts.',
1031
+ };
1032
+ }
1033
+ }
1034
+ // Validation passed - proceed with normal review flow
1035
+ logger.info('review', 'Content validation passed - proceeding with verification', {
1036
+ storyId: story.frontmatter.id,
1037
+ contentType,
1038
+ });
1039
+ // PRE-CHECK GATE: Check if test files exist
1040
+ const testsExist = hasTestFiles(workingDir);
1041
+ if (!testsExist) {
1042
+ logger.warn('review', 'No test files detected in implementation changes', {
1043
+ storyId: story.frontmatter.id,
1044
+ });
1045
+ return {
1046
+ success: true,
1047
+ story: parseStory(storyPath),
1048
+ changesMade: ['No test files found for implementation'],
1049
+ passed: false,
1050
+ decision: ReviewDecision.REJECTED,
1051
+ severity: ReviewSeverity.CRITICAL,
1052
+ reviewType: 'pre-check',
1053
+ issues: [{
1054
+ severity: 'blocker',
1055
+ category: 'testing',
1056
+ description: 'No tests found for this implementation. All implementations must include tests.',
1057
+ suggestedFix: 'Add test files (*.test.ts, *.spec.ts, or files in __tests__/ directory) that verify the implementation.',
1058
+ }],
1059
+ feedback: formatIssuesForDisplay([{
1060
+ severity: 'blocker',
1061
+ category: 'testing',
1062
+ description: 'No tests found for this implementation. All implementations must include tests.',
1063
+ suggestedFix: 'Add test files (*.test.ts, *.spec.ts, or files in __tests__/ directory) that verify the implementation.',
1064
+ }]),
1065
+ };
1066
+ }
582
1067
  // Run build and tests BEFORE reviews (async with progress)
583
1068
  changesMade.push('Running build and test verification...');
584
1069
  const verification = await runVerificationAsync(workingDir, config, options?.onVerificationProgress);
@@ -625,7 +1110,7 @@ export async function runReviewAgent(storyPath, sdlcRoot, options) {
625
1110
  severity: 'blocker',
626
1111
  category: 'testing',
627
1112
  description: `Tests must pass before code review can proceed.\n\nCommand: ${config.testCommand}\n\nTest output:\n\`\`\`\n${testOutput}${truncationNote}\n\`\`\``,
628
- suggestedFix: 'Fix failing tests before review can proceed.',
1113
+ suggestedFix: 'Fix failing tests before review can proceed. If tests are failing after implementation changes, verify that tests were updated to match the new behavior (not just the old behavior).',
629
1114
  });
630
1115
  verificationContext += `\n## Test Results ❌\nTest command \`${config.testCommand}\` FAILED:\n\`\`\`\n${testOutput}${truncationNote}\n\`\`\`\n`;
631
1116
  }
@@ -646,60 +1131,82 @@ export async function runReviewAgent(storyPath, sdlcRoot, options) {
646
1131
  feedback: formatIssuesForDisplay(verificationIssues),
647
1132
  };
648
1133
  }
649
- // Verification passed - proceed with all reviews in parallel, passing verification context
650
- changesMade.push('Verification passed - proceeding with code/security/PO reviews');
651
- const [codeReview, securityReview, poReview] = await Promise.all([
652
- runSubReview(story, CODE_REVIEW_PROMPT, 'Code Review', workingDir, verificationContext),
653
- runSubReview(story, SECURITY_REVIEW_PROMPT, 'Security Review', workingDir, verificationContext),
654
- runSubReview(story, PO_REVIEW_PROMPT, 'Product Owner Review', workingDir, verificationContext),
655
- ]);
656
- // Parse each review response into structured issues
657
- const codeResult = parseReviewResponse(codeReview, 'Code Review');
658
- const securityResult = parseReviewResponse(securityReview, 'Security Review');
659
- const poResult = parseReviewResponse(poReview, 'Product Owner Review');
1134
+ // Verification passed - proceed with unified collaborative review
1135
+ changesMade.push('Verification passed - proceeding with unified collaborative review');
1136
+ // Run test pattern detection if enabled
1137
+ let testPatternIssues = [];
1138
+ if (config.reviewConfig.detectTestAntipatterns !== false) {
1139
+ try {
1140
+ changesMade.push('Running test anti-pattern detection...');
1141
+ testPatternIssues = await detectTestDuplicationPatterns(workingDir);
1142
+ if (testPatternIssues.length > 0) {
1143
+ changesMade.push(`Detected ${testPatternIssues.length} test anti-pattern(s)`);
1144
+ }
1145
+ else {
1146
+ changesMade.push('No test anti-patterns detected');
1147
+ }
1148
+ }
1149
+ catch (error) {
1150
+ // Don't fail review if detection errors - just log and continue
1151
+ const errorMsg = error instanceof Error ? error.message : String(error);
1152
+ changesMade.push(`Test pattern detection error: ${errorMsg}`);
1153
+ }
1154
+ }
1155
+ const unifiedReviewResponse = await runSubReview(story, UNIFIED_REVIEW_PROMPT, 'Unified Collaborative Review', workingDir, verificationContext);
1156
+ // Parse unified review response into structured issues
1157
+ const unifiedResult = parseReviewResponse(unifiedReviewResponse, 'Unified Review');
660
1158
  // TDD Validation: Check TDD cycle completeness if TDD was enabled for this story
661
1159
  const tddEnabled = story.frontmatter.tdd_enabled ?? config.tdd?.enabled ?? false;
662
1160
  if (tddEnabled && story.frontmatter.tdd_test_history?.length) {
663
1161
  const tddViolations = validateTDDCycles(story.frontmatter.tdd_test_history);
664
1162
  if (tddViolations.length > 0) {
665
1163
  const tddIssues = generateTDDIssues(tddViolations);
666
- codeResult.issues.push(...tddIssues);
667
- codeResult.passed = false;
1164
+ unifiedResult.issues.push(...tddIssues);
1165
+ unifiedResult.passed = false;
668
1166
  changesMade.push(`TDD validation: ${tddViolations.length} violation(s) detected`);
669
1167
  }
670
1168
  else {
671
1169
  changesMade.push('TDD validation: All cycles completed correctly');
672
1170
  }
673
1171
  }
674
- // Add verification issues to code result (they're code-quality related)
675
- codeResult.issues.unshift(...verificationIssues);
1172
+ // Add test pattern issues to unified result (they're code-quality related)
1173
+ if (testPatternIssues.length > 0) {
1174
+ unifiedResult.issues.push(...testPatternIssues);
1175
+ unifiedResult.passed = false;
1176
+ }
1177
+ // Add verification issues to unified result (they're code-quality related)
1178
+ unifiedResult.issues.unshift(...verificationIssues);
676
1179
  if (verificationIssues.length > 0) {
677
- codeResult.passed = false;
1180
+ unifiedResult.passed = false;
678
1181
  }
679
- // Aggregate all issues and determine overall pass/fail
680
- const { passed, allIssues, severity } = aggregateReviews(codeResult, securityResult, poResult);
681
- // Compile review notes with structured format
1182
+ // Determine overall pass/fail from unified review
1183
+ const allIssues = unifiedResult.issues;
1184
+ const blockerCount = allIssues.filter(i => i.severity === 'blocker').length;
1185
+ const criticalCount = allIssues.filter(i => i.severity === 'critical').length;
1186
+ const passed = blockerCount === 0 && criticalCount < 2;
1187
+ const severity = determineReviewSeverity(allIssues);
1188
+ // Derive individual perspective pass/fail for backward compatibility
1189
+ const { codeReviewPassed, securityReviewPassed, poReviewPassed } = deriveIndividualPassFailFromPerspectives(allIssues);
1190
+ // Compile review notes with structured format for unified review
682
1191
  const reviewNotes = `
683
- ### Code Review
684
- ${formatIssuesForDisplay(codeResult.issues)}
1192
+ ### Unified Collaborative Review
685
1193
 
686
- ### Security Review
687
- ${formatIssuesForDisplay(securityResult.issues)}
1194
+ ${formatIssuesForDisplay(allIssues)}
688
1195
 
689
- ### Product Owner Review
690
- ${formatIssuesForDisplay(poResult.issues)}
1196
+ ### Perspective Summary
1197
+ - Code Quality: ${codeReviewPassed ? '✅ Passed' : '❌ Failed'}
1198
+ - Security: ${securityReviewPassed ? '✅ Passed' : '❌ Failed'}
1199
+ - Requirements (PO): ${poReviewPassed ? '✅ Passed' : '❌ Failed'}
691
1200
 
692
1201
  ### Overall Result
693
1202
  ${passed ? '✅ **PASSED** - All reviews approved' : '❌ **FAILED** - Issues must be addressed'}
694
1203
 
695
1204
  ---
696
- *Reviews completed: ${new Date().toISOString().split('T')[0]}*
1205
+ *Review completed: ${new Date().toISOString().split('T')[0]}*
697
1206
  `;
698
1207
  // Append reviews to story
699
- appendToSection(story, 'Review Notes', reviewNotes);
700
- changesMade.push('Added code review notes');
701
- changesMade.push('Added security review notes');
702
- changesMade.push('Added product owner review notes');
1208
+ await appendToSection(story, 'Review Notes', reviewNotes);
1209
+ changesMade.push('Added unified collaborative review notes');
703
1210
  // Determine decision
704
1211
  const decision = passed ? ReviewDecision.APPROVED : ReviewDecision.REJECTED;
705
1212
  // Create review attempt record (omit undefined fields to avoid YAML serialization errors)
@@ -709,21 +1216,28 @@ ${passed ? '✅ **PASSED** - All reviews approved' : '❌ **FAILED** - Issues mu
709
1216
  ...(passed ? {} : { severity }),
710
1217
  feedback: passed ? 'All reviews passed' : formatIssuesForDisplay(allIssues),
711
1218
  blockers: allIssues.filter(i => i.severity === 'blocker').map(i => i.description),
712
- codeReviewPassed: codeResult.passed,
713
- securityReviewPassed: securityResult.passed,
714
- poReviewPassed: poResult.passed,
1219
+ codeReviewPassed,
1220
+ securityReviewPassed,
1221
+ poReviewPassed,
715
1222
  };
716
1223
  // Append to review history
717
- appendReviewHistory(story, reviewAttempt);
1224
+ await appendReviewHistory(story, reviewAttempt);
718
1225
  changesMade.push('Recorded review attempt in history');
719
1226
  if (passed) {
720
- updateStoryField(story, 'reviews_complete', true);
1227
+ await updateStoryField(story, 'reviews_complete', true);
721
1228
  changesMade.push('Marked reviews_complete: true');
722
1229
  }
723
1230
  else {
724
1231
  changesMade.push(`Reviews failed with ${allIssues.length} issue(s) - rework required`);
725
1232
  // Don't mark reviews_complete, this will trigger rework
726
1233
  }
1234
+ logger.info('review', 'Review phase complete', {
1235
+ storyId: story.frontmatter.id,
1236
+ durationMs: Date.now() - startTime,
1237
+ passed,
1238
+ decision,
1239
+ issueCount: allIssues.length,
1240
+ });
727
1241
  return {
728
1242
  success: true,
729
1243
  story: parseStory(storyPath),
@@ -739,6 +1253,11 @@ ${passed ? '✅ **PASSED** - All reviews approved' : '❌ **FAILED** - Issues mu
739
1253
  catch (error) {
740
1254
  // Review agent failure - return FAILED decision (doesn't count as retry)
741
1255
  const errorMsg = error instanceof Error ? error.message : String(error);
1256
+ logger.error('review', 'Review phase failed', {
1257
+ storyId: story.frontmatter.id,
1258
+ durationMs: Date.now() - startTime,
1259
+ error: errorMsg,
1260
+ });
742
1261
  return {
743
1262
  success: false,
744
1263
  story,
@@ -756,6 +1275,139 @@ ${passed ? '✅ **PASSED** - All reviews approved' : '❌ **FAILED** - Issues mu
756
1275
  };
757
1276
  }
758
1277
  }
1278
+ /**
1279
+ * Parse story content into sections by level-2 headers (##)
1280
+ * Returns array of {title, content} objects
1281
+ */
1282
+ export function parseContentSections(content) {
1283
+ const sections = [];
1284
+ const lines = content.split('\n');
1285
+ let currentSection = null;
1286
+ for (const line of lines) {
1287
+ const headerMatch = line.match(/^##\s+(.+)$/);
1288
+ if (headerMatch) {
1289
+ if (currentSection)
1290
+ sections.push(currentSection);
1291
+ currentSection = { title: headerMatch[1], content: '' };
1292
+ }
1293
+ else if (currentSection) {
1294
+ currentSection.content += line + '\n';
1295
+ }
1296
+ }
1297
+ if (currentSection)
1298
+ sections.push(currentSection);
1299
+ return sections;
1300
+ }
1301
+ /**
1302
+ * Remove unfinished checkboxes from content (per CLAUDE.md requirement)
1303
+ * Removes lines with `- [ ]` or `* [ ]` patterns
1304
+ * Preserves completed checkboxes `- [x]` and `- [X]`
1305
+ */
1306
+ export function removeUnfinishedCheckboxes(content) {
1307
+ const lines = content.split('\n');
1308
+ const filteredLines = [];
1309
+ for (let i = 0; i < lines.length; i++) {
1310
+ const line = lines[i];
1311
+ // Match unchecked boxes: - [ ] or * [ ] with optional leading whitespace
1312
+ const isUnchecked = /^\s*[-*] \[ \]/.test(line);
1313
+ if (!isUnchecked) {
1314
+ filteredLines.push(line);
1315
+ }
1316
+ }
1317
+ return filteredLines.join('\n');
1318
+ }
1319
+ /**
1320
+ * Generate GitHub blob URL for story file
1321
+ * Parses remote URL and constructs link to story in repository
1322
+ */
1323
+ export function getStoryFileURL(storyPath, branch, workingDir) {
1324
+ try {
1325
+ const remoteUrl = execSync('git remote get-url origin', { cwd: workingDir, encoding: 'utf-8' }).trim();
1326
+ // Parse owner/repo from URL
1327
+ // HTTPS: https://github.com/owner/repo.git
1328
+ // SSH: git@github.com:owner/repo.git
1329
+ const match = remoteUrl.match(/github\.com[:/]([^/]+)\/(.+?)(\.git)?$/);
1330
+ if (!match)
1331
+ return '';
1332
+ const [, owner, repo] = match;
1333
+ const relativePath = path.relative(workingDir, storyPath);
1334
+ return `https://github.com/${owner}/${repo}/blob/${branch}/${relativePath}`;
1335
+ }
1336
+ catch {
1337
+ return '';
1338
+ }
1339
+ }
1340
+ /**
1341
+ * Format PR description from story sections
1342
+ * Includes: Story ID, User Story, Summary, Acceptance Criteria, Implementation Summary
1343
+ * Removes unfinished checkboxes from all sections
1344
+ */
1345
+ export function formatPRDescription(story, storyFileUrl) {
1346
+ const sections = parseContentSections(story.content);
1347
+ // Extract key sections
1348
+ const userStory = sections.find(s => s.title === 'User Story')?.content || '';
1349
+ const summary = sections.find(s => s.title === 'Summary')?.content || '';
1350
+ const acceptanceCriteria = sections.find(s => s.title === 'Acceptance Criteria')?.content || '';
1351
+ const implementationSummary = sections.find(s => s.title === 'Implementation Summary')?.content || '';
1352
+ // Remove unfinished checkboxes from all sections
1353
+ const cleanAcceptanceCriteria = removeUnfinishedCheckboxes(acceptanceCriteria);
1354
+ const cleanImplementationSummary = removeUnfinishedCheckboxes(implementationSummary);
1355
+ // Build PR body
1356
+ let prBody = `## Story ID\n\n${story.frontmatter.id}\n\n`;
1357
+ if (userStory.trim()) {
1358
+ prBody += `## User Story\n\n${userStory.trim()}\n\n`;
1359
+ }
1360
+ if (summary.trim()) {
1361
+ prBody += `## Summary\n\n${summary.trim()}\n\n`;
1362
+ }
1363
+ if (cleanAcceptanceCriteria.trim()) {
1364
+ prBody += `## Acceptance Criteria\n\n${cleanAcceptanceCriteria.trim()}\n\n`;
1365
+ }
1366
+ if (cleanImplementationSummary.trim()) {
1367
+ prBody += `## Implementation Summary\n\n${cleanImplementationSummary.trim()}\n\n`;
1368
+ }
1369
+ // Add story file link
1370
+ if (storyFileUrl) {
1371
+ prBody += `---\n\n📋 [View Full Story](${storyFileUrl})\n`;
1372
+ }
1373
+ return prBody;
1374
+ }
1375
+ /**
1376
+ * Truncate PR body to respect GitHub's 65K character limit
1377
+ * Truncates Implementation Summary first (most verbose section)
1378
+ * Adds clear truncation indicator with story link
1379
+ */
1380
+ export function truncatePRBody(body, maxLength = 64000) {
1381
+ // Check if truncation needed
1382
+ if (body.length <= maxLength) {
1383
+ return body;
1384
+ }
1385
+ // Find Implementation Summary section
1386
+ const implSummaryMatch = body.match(/(## Implementation Summary\n\n)([\s\S]*?)(\n\n##|\n\n---|\n\n📋|$)/);
1387
+ if (implSummaryMatch) {
1388
+ const [fullMatch, header, content, trailer] = implSummaryMatch;
1389
+ const beforeImpl = body.substring(0, body.indexOf(fullMatch));
1390
+ const afterImpl = body.substring(body.indexOf(fullMatch) + fullMatch.length);
1391
+ // Calculate how much we need to remove
1392
+ const overhead = beforeImpl.length + header.length + trailer.length + afterImpl.length;
1393
+ const truncationIndicator = '\n\n⚠️ Implementation Summary truncated due to length. See full story for complete details.\n';
1394
+ const availableForContent = maxLength - overhead - truncationIndicator.length;
1395
+ if (availableForContent > 100) {
1396
+ // Truncate Implementation Summary at paragraph boundary
1397
+ let truncatedContent = content.substring(0, availableForContent);
1398
+ const lastParagraph = truncatedContent.lastIndexOf('\n\n');
1399
+ if (lastParagraph > 0) {
1400
+ truncatedContent = truncatedContent.substring(0, lastParagraph);
1401
+ }
1402
+ return beforeImpl + header + truncatedContent + truncationIndicator + trailer + afterImpl;
1403
+ }
1404
+ }
1405
+ // Fallback: simple truncation if no Implementation Summary found
1406
+ const truncatedBody = body.substring(0, maxLength - 200);
1407
+ const lastParagraph = truncatedBody.lastIndexOf('\n\n');
1408
+ const finalBody = lastParagraph > 0 ? truncatedBody.substring(0, lastParagraph) : truncatedBody;
1409
+ return finalBody + '\n\n⚠️ Description truncated due to length. See full story for complete details.\n';
1410
+ }
759
1411
  /**
760
1412
  * Run a sub-review with a specific prompt
761
1413
  */
@@ -782,7 +1434,7 @@ Provide your ${reviewType} feedback. Be specific and actionable.`;
782
1434
  /**
783
1435
  * Create a pull request for the completed story
784
1436
  */
785
- export async function createPullRequest(storyPath, sdlcRoot) {
1437
+ export async function createPullRequest(storyPath, sdlcRoot, options) {
786
1438
  let story = parseStory(storyPath);
787
1439
  const changesMade = [];
788
1440
  const workingDir = path.dirname(sdlcRoot);
@@ -819,7 +1471,7 @@ export async function createPullRequest(storyPath, sdlcRoot) {
819
1471
  catch {
820
1472
  changesMade.push('GitHub CLI not available - PR creation skipped');
821
1473
  // Still update to done for MVP
822
- story = updateStoryStatus(story, 'done');
1474
+ story = await updateStoryStatus(story, 'done');
823
1475
  changesMade.push('Updated status to done');
824
1476
  return {
825
1477
  success: true,
@@ -844,37 +1496,69 @@ export async function createPullRequest(storyPath, sdlcRoot) {
844
1496
  // Push branch (already validated)
845
1497
  execSync(`git push -u origin ${branchName}`, { cwd: workingDir, stdio: 'pipe' });
846
1498
  changesMade.push(`Pushed branch: ${branchName}`);
847
- // Create PR using gh CLI with safe arguments
848
- // Security: Use escaped arguments to prevent shell injection
1499
+ // Check if PR already exists for this branch
1500
+ try {
1501
+ const existingPROutput = execSync('gh pr view --json url', { cwd: workingDir, encoding: 'utf-8', stdio: 'pipe' });
1502
+ const prData = JSON.parse(existingPROutput);
1503
+ if (prData.url) {
1504
+ changesMade.push(`PR already exists: ${prData.url}`);
1505
+ // Update story with PR URL if missing
1506
+ if (!story.frontmatter.pr_url) {
1507
+ await updateStoryField(story, 'pr_url', prData.url);
1508
+ changesMade.push('Updated story with existing PR URL');
1509
+ }
1510
+ // Don't create duplicate - skip to status update
1511
+ story = await updateStoryStatus(story, 'done');
1512
+ changesMade.push('Updated status to done');
1513
+ return {
1514
+ success: true,
1515
+ story,
1516
+ changesMade,
1517
+ };
1518
+ }
1519
+ }
1520
+ catch {
1521
+ // No existing PR - proceed with creation
1522
+ }
1523
+ // Create PR using gh CLI with rich formatted body
1524
+ // Security: Use escaped arguments and heredoc to prevent shell injection
849
1525
  const prTitle = story.frontmatter.title;
850
- const prBody = `## Summary
851
-
852
- ${story.frontmatter.title}
853
-
854
- ## Story
855
-
856
- ${story.content.substring(0, 1000)}...
857
-
858
- ## Checklist
859
-
860
- - [x] Implementation complete
861
- - [x] Code review passed
862
- - [x] Security review passed
863
- - [x] Product owner approved
864
-
865
- ---
866
- *Created by ai-sdlc*`;
867
- const prOutput = execSync(`gh pr create --title ${escapeShellArg(prTitle)} --body ${escapeShellArg(prBody)}`, { cwd: workingDir, encoding: 'utf-8' });
1526
+ // Generate story file URL
1527
+ const storyFileUrl = getStoryFileURL(storyPath, branchName, workingDir);
1528
+ // Format rich PR description
1529
+ let prBody = formatPRDescription(story, storyFileUrl);
1530
+ // Truncate if needed to respect GitHub's 65K limit
1531
+ prBody = truncatePRBody(prBody);
1532
+ // Determine if draft PR should be created
1533
+ // Options parameter takes precedence, then config, default is false
1534
+ const config = loadConfig(workingDir);
1535
+ const createAsDraft = options?.draft ?? config.github?.createDraftPRs ?? false;
1536
+ const draftFlag = createAsDraft ? ' --draft' : '';
1537
+ // Use heredoc pattern for multi-line body to preserve formatting
1538
+ const ghCommand = `gh pr create --title ${escapeShellArg(prTitle)}${draftFlag} --body "$(cat <<'EOF'
1539
+ ${prBody}
1540
+ EOF
1541
+ )"`;
1542
+ const prOutput = execSync(ghCommand, { cwd: workingDir, encoding: 'utf-8' });
868
1543
  const prUrl = prOutput.trim();
869
- updateStoryField(story, 'pr_url', prUrl);
870
- changesMade.push(`Created PR: ${prUrl}`);
1544
+ await updateStoryField(story, 'pr_url', prUrl);
1545
+ const prTypeLabel = createAsDraft ? 'draft PR' : 'PR';
1546
+ changesMade.push(`Created ${prTypeLabel}: ${prUrl}`);
871
1547
  }
872
1548
  catch (error) {
873
1549
  const sanitizedError = sanitizeErrorMessage(error instanceof Error ? error.message : String(error), workingDir);
874
- changesMade.push(`PR creation failed: ${sanitizedError}`);
1550
+ // Provide actionable error messages for common issues
1551
+ let errorMessage = `PR creation failed: ${sanitizedError}`;
1552
+ if (sanitizedError.includes('authentication') || sanitizedError.includes('auth') || sanitizedError.includes('credentials')) {
1553
+ errorMessage = `GitHub authentication failed. Please authenticate using one of:
1554
+ 1. Set GITHUB_TOKEN env var: export GITHUB_TOKEN=ghp_xxx
1555
+ 2. Run: gh auth login
1556
+ 3. Check: gh auth status`;
1557
+ }
1558
+ changesMade.push(errorMessage);
875
1559
  }
876
1560
  // Update status to done
877
- story = updateStoryStatus(story, 'done');
1561
+ story = await updateStoryStatus(story, 'done');
878
1562
  changesMade.push('Updated status to done');
879
1563
  return {
880
1564
  success: true,