ai-sdlc 0.2.0-alpha.5 → 0.2.0-alpha.51

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