ai-sdlc 0.2.0-alpha.2 → 0.2.0-alpha.20
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.
- package/dist/agents/implementation.d.ts +62 -0
- package/dist/agents/implementation.d.ts.map +1 -1
- package/dist/agents/implementation.js +502 -89
- package/dist/agents/implementation.js.map +1 -1
- package/dist/agents/planning.js +2 -2
- package/dist/agents/planning.js.map +1 -1
- package/dist/agents/refinement.js +2 -2
- package/dist/agents/refinement.js.map +1 -1
- package/dist/agents/research.js +2 -2
- package/dist/agents/research.js.map +1 -1
- package/dist/agents/review.d.ts +12 -0
- package/dist/agents/review.d.ts.map +1 -1
- package/dist/agents/review.js +100 -9
- package/dist/agents/review.js.map +1 -1
- package/dist/agents/rework.js +3 -3
- package/dist/agents/rework.js.map +1 -1
- package/dist/agents/state-assessor.d.ts +3 -3
- package/dist/agents/state-assessor.d.ts.map +1 -1
- package/dist/agents/state-assessor.js +6 -6
- package/dist/agents/state-assessor.js.map +1 -1
- package/dist/agents/verification.d.ts +11 -0
- package/dist/agents/verification.d.ts.map +1 -1
- package/dist/agents/verification.js +74 -1
- package/dist/agents/verification.js.map +1 -1
- package/dist/cli/commands/migrate.js +1 -1
- package/dist/cli/commands/migrate.js.map +1 -1
- package/dist/cli/commands.d.ts +30 -1
- package/dist/cli/commands.d.ts.map +1 -1
- package/dist/cli/commands.js +637 -171
- package/dist/cli/commands.js.map +1 -1
- package/dist/cli/daemon.d.ts.map +1 -1
- package/dist/cli/daemon.js +23 -10
- package/dist/cli/daemon.js.map +1 -1
- package/dist/cli/formatting.js +1 -1
- package/dist/cli/formatting.js.map +1 -1
- package/dist/cli/runner.d.ts.map +1 -1
- package/dist/cli/runner.js +39 -20
- package/dist/cli/runner.js.map +1 -1
- package/dist/core/auth.d.ts +51 -2
- package/dist/core/auth.d.ts.map +1 -1
- package/dist/core/auth.js +267 -7
- package/dist/core/auth.js.map +1 -1
- package/dist/core/client.d.ts +6 -0
- package/dist/core/client.d.ts.map +1 -1
- package/dist/core/client.js +34 -2
- package/dist/core/client.js.map +1 -1
- package/dist/core/config.d.ts +36 -1
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js +161 -1
- package/dist/core/config.js.map +1 -1
- package/dist/core/conflict-detector.d.ts +108 -0
- package/dist/core/conflict-detector.d.ts.map +1 -0
- package/dist/core/conflict-detector.js +413 -0
- package/dist/core/conflict-detector.js.map +1 -0
- package/dist/core/git-utils.d.ts +28 -0
- package/dist/core/git-utils.d.ts.map +1 -0
- package/dist/core/git-utils.js +146 -0
- package/dist/core/git-utils.js.map +1 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +16 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/kanban.d.ts +1 -6
- package/dist/core/kanban.d.ts.map +1 -1
- package/dist/core/kanban.js +10 -49
- package/dist/core/kanban.js.map +1 -1
- package/dist/core/logger.d.ts +92 -0
- package/dist/core/logger.d.ts.map +1 -0
- package/dist/core/logger.js +221 -0
- package/dist/core/logger.js.map +1 -0
- package/dist/core/story.d.ts +108 -20
- package/dist/core/story.d.ts.map +1 -1
- package/dist/core/story.js +340 -59
- package/dist/core/story.js.map +1 -1
- package/dist/core/workflow-state.d.ts +45 -6
- package/dist/core/workflow-state.d.ts.map +1 -1
- package/dist/core/workflow-state.js +185 -12
- package/dist/core/workflow-state.js.map +1 -1
- package/dist/core/worktree.d.ts +77 -0
- package/dist/core/worktree.d.ts.map +1 -0
- package/dist/core/worktree.js +246 -0
- package/dist/core/worktree.js.map +1 -0
- package/dist/index.js +49 -3
- package/dist/index.js.map +1 -1
- package/dist/types/index.d.ts +101 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/package.json +3 -1
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { spawn, spawnSync } from 'child_process';
|
|
2
2
|
import path from 'path';
|
|
3
|
-
import { parseStory, writeStory, updateStoryStatus, updateStoryField } from '../core/story.js';
|
|
3
|
+
import { parseStory, writeStory, updateStoryStatus, updateStoryField, resetImplementationRetryCount, incrementImplementationRetryCount, getEffectiveMaxImplementationRetries, } from '../core/story.js';
|
|
4
4
|
import { runAgentQuery } from '../core/client.js';
|
|
5
5
|
import { loadConfig, DEFAULT_TDD_CONFIG } from '../core/config.js';
|
|
6
6
|
import { verifyImplementation } from './verification.js';
|
|
7
|
+
import { createHash } from 'crypto';
|
|
7
8
|
export const TDD_SYSTEM_PROMPT = `You are practicing strict Test-Driven Development.
|
|
8
9
|
|
|
9
10
|
Your workflow MUST follow this exact cycle:
|
|
@@ -38,7 +39,6 @@ When implementing:
|
|
|
38
39
|
5. Update the plan checkboxes as you complete tasks
|
|
39
40
|
6. Do NOT create temporary files, shell scripts, or documentation files - keep all notes in the story file
|
|
40
41
|
7. Follow the Testing Pyramid: prioritize unit tests (colocated with source, e.g., src/foo.test.ts), then integration tests (in tests/integration/)
|
|
41
|
-
8. Do NOT commit changes - that happens in the review phase
|
|
42
42
|
|
|
43
43
|
CRITICAL RULES ABOUT TESTS:
|
|
44
44
|
- Test updates are PART of implementation, not a separate phase
|
|
@@ -161,12 +161,16 @@ function escapeShellArg(arg) {
|
|
|
161
161
|
*/
|
|
162
162
|
export async function commitIfAllTestsPass(workingDir, message, testTimeout, testRunner = runAllTests) {
|
|
163
163
|
try {
|
|
164
|
-
//
|
|
165
|
-
|
|
164
|
+
// Security: Validate working directory before use
|
|
165
|
+
validateWorkingDir(workingDir);
|
|
166
|
+
// Check for uncommitted changes using spawn with shell: false
|
|
167
|
+
const statusResult = spawnSync('git', ['status', '--porcelain'], {
|
|
166
168
|
cwd: workingDir,
|
|
167
|
-
encoding: 'utf-8'
|
|
169
|
+
encoding: 'utf-8',
|
|
170
|
+
shell: false,
|
|
171
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
168
172
|
});
|
|
169
|
-
if (!
|
|
173
|
+
if (statusResult.status !== 0 || !statusResult.stdout || !statusResult.stdout.trim()) {
|
|
170
174
|
return { committed: false, reason: 'nothing to commit' };
|
|
171
175
|
}
|
|
172
176
|
// Run FULL test suite
|
|
@@ -174,12 +178,23 @@ export async function commitIfAllTestsPass(workingDir, message, testTimeout, tes
|
|
|
174
178
|
if (!testResult.passed) {
|
|
175
179
|
return { committed: false, reason: 'tests failed' };
|
|
176
180
|
}
|
|
177
|
-
// Commit changes
|
|
178
|
-
|
|
179
|
-
execSync(`git commit -m ${escapeShellArg(message)}`, {
|
|
181
|
+
// Commit changes using spawn with shell: false
|
|
182
|
+
const addResult = spawnSync('git', ['add', '-A'], {
|
|
180
183
|
cwd: workingDir,
|
|
181
|
-
|
|
184
|
+
shell: false,
|
|
185
|
+
stdio: 'pipe',
|
|
182
186
|
});
|
|
187
|
+
if (addResult.status !== 0) {
|
|
188
|
+
throw new Error(`git add failed: ${addResult.stderr}`);
|
|
189
|
+
}
|
|
190
|
+
const commitResult = spawnSync('git', ['commit', '-m', message], {
|
|
191
|
+
cwd: workingDir,
|
|
192
|
+
shell: false,
|
|
193
|
+
stdio: 'pipe',
|
|
194
|
+
});
|
|
195
|
+
if (commitResult.status !== 0) {
|
|
196
|
+
throw new Error(`git commit failed: ${commitResult.stderr}`);
|
|
197
|
+
}
|
|
183
198
|
return { committed: true };
|
|
184
199
|
}
|
|
185
200
|
catch (error) {
|
|
@@ -470,6 +485,20 @@ export async function runTDDImplementation(story, sdlcRoot, options = {}) {
|
|
|
470
485
|
};
|
|
471
486
|
}
|
|
472
487
|
changesMade.push('REFACTOR: All tests still pass');
|
|
488
|
+
// Commit changes after successful TDD cycle
|
|
489
|
+
try {
|
|
490
|
+
const commitResult = await commitIfAllTestsPass(workingDir, `feat(${story.slug}): TDD cycle ${cycleNumber} - ${redResult.testName}`, testTimeout, allTests);
|
|
491
|
+
if (commitResult.committed) {
|
|
492
|
+
changesMade.push(`Committed: TDD cycle ${cycleNumber} - ${redResult.testName}`);
|
|
493
|
+
}
|
|
494
|
+
else {
|
|
495
|
+
changesMade.push(`Skipped commit: ${commitResult.reason}`);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
catch (error) {
|
|
499
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
500
|
+
changesMade.push(`Commit warning: ${errorMsg} (continuing implementation)`);
|
|
501
|
+
}
|
|
473
502
|
// Record the completed cycle
|
|
474
503
|
const cycle = recordTDDCycle(cycleNumber, redResult, greenResult, refactorResult);
|
|
475
504
|
// Update story with cycle history
|
|
@@ -479,7 +508,7 @@ export async function runTDDImplementation(story, sdlcRoot, options = {}) {
|
|
|
479
508
|
story.frontmatter.tdd_test_history = history.slice(-100);
|
|
480
509
|
story.frontmatter.tdd_current_test = cycle;
|
|
481
510
|
// Persist the TDD cycle history to disk
|
|
482
|
-
writeStory(story);
|
|
511
|
+
await writeStory(story);
|
|
483
512
|
changesMade.push(`Completed TDD cycle ${cycleNumber}`);
|
|
484
513
|
// Check if all AC are now covered
|
|
485
514
|
// Re-read story to get latest content (agent may have updated checkboxes)
|
|
@@ -502,6 +531,189 @@ export async function runTDDImplementation(story, sdlcRoot, options = {}) {
|
|
|
502
531
|
error: `TDD: Maximum cycles (${tddConfig.maxCycles}) reached without completing all acceptance criteria.`,
|
|
503
532
|
};
|
|
504
533
|
}
|
|
534
|
+
/**
|
|
535
|
+
* Attempt implementation with retry logic
|
|
536
|
+
*
|
|
537
|
+
* Runs the implementation loop, retrying on test failures up to maxRetries times.
|
|
538
|
+
* Includes no-change detection to exit early if the agent makes no progress.
|
|
539
|
+
*
|
|
540
|
+
* @param options Retry attempt options
|
|
541
|
+
* @param changesMade Array to track changes (mutated in place)
|
|
542
|
+
* @returns AgentResult with success/failure status
|
|
543
|
+
*/
|
|
544
|
+
export async function attemptImplementationWithRetries(options, changesMade) {
|
|
545
|
+
const { story, storyPath, workingDir, maxRetries, reworkContext, onProgress } = options;
|
|
546
|
+
let attemptNumber = 0;
|
|
547
|
+
let lastVerification = null;
|
|
548
|
+
let lastDiffHash = ''; // Initialize to empty string, will capture after first failure
|
|
549
|
+
const attemptHistory = [];
|
|
550
|
+
while (attemptNumber <= maxRetries) {
|
|
551
|
+
attemptNumber++;
|
|
552
|
+
let prompt = `Implement this story based on the plan:
|
|
553
|
+
|
|
554
|
+
Title: ${story.frontmatter.title}
|
|
555
|
+
|
|
556
|
+
Story content:
|
|
557
|
+
${story.content}`;
|
|
558
|
+
if (reworkContext) {
|
|
559
|
+
prompt += `
|
|
560
|
+
|
|
561
|
+
---
|
|
562
|
+
${reworkContext}
|
|
563
|
+
---
|
|
564
|
+
|
|
565
|
+
IMPORTANT: This is a refinement iteration. The previous implementation did not pass review.
|
|
566
|
+
You MUST fix all the issues listed above. Pay special attention to blocker and critical
|
|
567
|
+
severity issues - these must be resolved. Review the specific feedback and make targeted fixes.`;
|
|
568
|
+
}
|
|
569
|
+
// Add retry context if this is a retry attempt
|
|
570
|
+
if (attemptNumber > 1 && lastVerification) {
|
|
571
|
+
prompt += '\n\n' + buildRetryPrompt(lastVerification.testsOutput, lastVerification.buildOutput, attemptNumber, maxRetries);
|
|
572
|
+
}
|
|
573
|
+
else {
|
|
574
|
+
prompt += `
|
|
575
|
+
|
|
576
|
+
Execute the implementation plan. For each task:
|
|
577
|
+
1. Read relevant existing files
|
|
578
|
+
2. Make necessary code changes
|
|
579
|
+
3. Write tests if applicable
|
|
580
|
+
4. Verify the changes work
|
|
581
|
+
|
|
582
|
+
Use the available tools to read files, write code, and run commands as needed.`;
|
|
583
|
+
}
|
|
584
|
+
// Send progress callback for all attempts (not just retries)
|
|
585
|
+
if (onProgress) {
|
|
586
|
+
if (attemptNumber === 1) {
|
|
587
|
+
onProgress({ type: 'assistant_message', content: `Starting implementation attempt 1/${maxRetries + 1}...` });
|
|
588
|
+
}
|
|
589
|
+
else {
|
|
590
|
+
onProgress({ type: 'assistant_message', content: `Analyzing test failures, retrying implementation (${attemptNumber - 1}/${maxRetries})...` });
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
const implementationResult = await runAgentQuery({
|
|
594
|
+
prompt,
|
|
595
|
+
systemPrompt: IMPLEMENTATION_SYSTEM_PROMPT,
|
|
596
|
+
workingDirectory: workingDir,
|
|
597
|
+
onProgress,
|
|
598
|
+
});
|
|
599
|
+
// Add implementation notes to the story
|
|
600
|
+
const notePrefix = attemptNumber > 1 ? `Implementation Notes - Retry ${attemptNumber - 1}` : 'Implementation Notes';
|
|
601
|
+
const implementationNotes = `
|
|
602
|
+
### ${notePrefix} (${new Date().toISOString().split('T')[0]})
|
|
603
|
+
|
|
604
|
+
${implementationResult}
|
|
605
|
+
`;
|
|
606
|
+
// Append to story content
|
|
607
|
+
const updatedStory = parseStory(storyPath);
|
|
608
|
+
updatedStory.content += '\n\n' + implementationNotes;
|
|
609
|
+
await writeStory(updatedStory);
|
|
610
|
+
changesMade.push(attemptNumber > 1 ? `Added retry ${attemptNumber - 1} notes` : 'Added implementation notes');
|
|
611
|
+
changesMade.push('Running verification before marking complete...');
|
|
612
|
+
const verification = await verifyImplementation(updatedStory, workingDir);
|
|
613
|
+
await updateStoryField(updatedStory, 'last_test_run', {
|
|
614
|
+
passed: verification.passed,
|
|
615
|
+
failures: verification.failures,
|
|
616
|
+
timestamp: verification.timestamp,
|
|
617
|
+
});
|
|
618
|
+
if (verification.passed) {
|
|
619
|
+
// Success! Reset retry count and return success
|
|
620
|
+
await resetImplementationRetryCount(updatedStory);
|
|
621
|
+
changesMade.push('Verification passed - implementation successful');
|
|
622
|
+
// Send success progress callback
|
|
623
|
+
if (onProgress) {
|
|
624
|
+
onProgress({ type: 'assistant_message', content: `Implementation succeeded on attempt ${attemptNumber}` });
|
|
625
|
+
}
|
|
626
|
+
return {
|
|
627
|
+
success: true,
|
|
628
|
+
story: parseStory(storyPath),
|
|
629
|
+
changesMade,
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
// Verification failed - check for retry conditions
|
|
633
|
+
lastVerification = verification;
|
|
634
|
+
// Capture current diff hash for no-change detection
|
|
635
|
+
const currentDiffHash = captureCurrentDiffHash(workingDir);
|
|
636
|
+
// Track retry attempt
|
|
637
|
+
await incrementImplementationRetryCount(updatedStory);
|
|
638
|
+
// Extract first 100 chars of test and build output for history
|
|
639
|
+
const testSnippet = verification.testsOutput.substring(0, 100).replace(/\n/g, ' ');
|
|
640
|
+
const buildSnippet = verification.buildOutput.substring(0, 100).replace(/\n/g, ' ');
|
|
641
|
+
// Determine if there are build failures (build output contains error indicators)
|
|
642
|
+
const hasBuildErrors = verification.buildOutput &&
|
|
643
|
+
(verification.buildOutput.includes('error') ||
|
|
644
|
+
verification.buildOutput.includes('Error') ||
|
|
645
|
+
verification.buildOutput.includes('failed'));
|
|
646
|
+
const buildFailures = hasBuildErrors ? 1 : 0;
|
|
647
|
+
// Record this attempt in history with both test and build failures
|
|
648
|
+
attemptHistory.push({
|
|
649
|
+
attempt: attemptNumber,
|
|
650
|
+
testFailures: verification.failures,
|
|
651
|
+
buildFailures,
|
|
652
|
+
testSnippet,
|
|
653
|
+
buildSnippet,
|
|
654
|
+
});
|
|
655
|
+
// Add structured retry entry to changes array
|
|
656
|
+
if (attemptNumber > 1) {
|
|
657
|
+
changesMade.push(`Implementation retry ${attemptNumber - 1}/${maxRetries}: ${verification.failures} test(s) failing`);
|
|
658
|
+
}
|
|
659
|
+
else {
|
|
660
|
+
changesMade.push(`Attempt ${attemptNumber}: ${verification.failures} test(s) failing`);
|
|
661
|
+
}
|
|
662
|
+
// Check for no-change scenario (agent made no progress)
|
|
663
|
+
// Only check after first failure (attemptNumber > 1)
|
|
664
|
+
// Check this BEFORE max retries to fail fast on identical changes
|
|
665
|
+
if (attemptNumber > 1 && lastDiffHash && lastDiffHash === currentDiffHash) {
|
|
666
|
+
return {
|
|
667
|
+
success: false,
|
|
668
|
+
story: parseStory(storyPath),
|
|
669
|
+
changesMade,
|
|
670
|
+
error: `Implementation blocked: No progress detected on retry attempt ${attemptNumber - 1}. Agent made identical changes. Stopping retries early.\n\nLast test output:\n${truncateTestOutput(verification.testsOutput, 1000)}`,
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
// Check if we've reached max retries
|
|
674
|
+
if (attemptHistory.length > maxRetries) {
|
|
675
|
+
const attemptSummary = attemptHistory
|
|
676
|
+
.map((a) => {
|
|
677
|
+
const parts = [];
|
|
678
|
+
if (a.testFailures > 0) {
|
|
679
|
+
parts.push(`${a.testFailures} test(s)`);
|
|
680
|
+
}
|
|
681
|
+
if (a.buildFailures > 0) {
|
|
682
|
+
parts.push(`${a.buildFailures} build error(s)`);
|
|
683
|
+
}
|
|
684
|
+
const errors = parts.length > 0 ? parts.join(', ') : 'verification failed';
|
|
685
|
+
const snippets = [];
|
|
686
|
+
if (a.testSnippet && a.testSnippet.trim()) {
|
|
687
|
+
snippets.push(`[test: ${a.testSnippet}]`);
|
|
688
|
+
}
|
|
689
|
+
if (a.buildSnippet && a.buildSnippet.trim()) {
|
|
690
|
+
snippets.push(`[build: ${a.buildSnippet}]`);
|
|
691
|
+
}
|
|
692
|
+
const snippetText = snippets.length > 0 ? ` - ${snippets.join(' ')}` : '';
|
|
693
|
+
return ` Attempt ${a.attempt}: ${errors}${snippetText}`;
|
|
694
|
+
})
|
|
695
|
+
.join('\n');
|
|
696
|
+
return {
|
|
697
|
+
success: false,
|
|
698
|
+
story: parseStory(storyPath),
|
|
699
|
+
changesMade,
|
|
700
|
+
error: `Implementation blocked after ${attemptNumber} attempts:\n${attemptSummary}\n\nLast test output:\n${truncateTestOutput(verification.testsOutput, 5000)}`,
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
lastDiffHash = currentDiffHash;
|
|
704
|
+
// Continue to next retry attempt - send progress update
|
|
705
|
+
if (onProgress) {
|
|
706
|
+
onProgress({ type: 'assistant_message', content: `Retry ${attemptNumber} failed: ${verification.failures} test(s) failing, attempting retry ${attemptNumber + 1}...` });
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
// If we exit the loop without returning, all retries exhausted (shouldn't normally reach here)
|
|
710
|
+
return {
|
|
711
|
+
success: false,
|
|
712
|
+
story: parseStory(storyPath),
|
|
713
|
+
changesMade,
|
|
714
|
+
error: 'Implementation failed: All retry attempts exhausted without resolution.',
|
|
715
|
+
};
|
|
716
|
+
}
|
|
505
717
|
/**
|
|
506
718
|
* Implementation Agent
|
|
507
719
|
*
|
|
@@ -513,28 +725,50 @@ export async function runImplementationAgent(storyPath, sdlcRoot, options = {})
|
|
|
513
725
|
const changesMade = [];
|
|
514
726
|
const workingDir = path.dirname(sdlcRoot);
|
|
515
727
|
try {
|
|
728
|
+
// Security: Validate working directory before git operations
|
|
729
|
+
validateWorkingDir(workingDir);
|
|
516
730
|
// Create a feature branch for this story
|
|
517
731
|
const branchName = `ai-sdlc/${story.slug}`;
|
|
732
|
+
// Security: Validate branch name before use
|
|
733
|
+
validateBranchName(branchName);
|
|
518
734
|
try {
|
|
519
|
-
// Check if we're in a git repo
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
735
|
+
// Check if we're in a git repo using spawn with shell: false
|
|
736
|
+
const revParseResult = spawnSync('git', ['rev-parse', '--git-dir'], {
|
|
737
|
+
cwd: workingDir,
|
|
738
|
+
shell: false,
|
|
739
|
+
stdio: 'pipe',
|
|
740
|
+
});
|
|
741
|
+
if (revParseResult.status !== 0) {
|
|
742
|
+
changesMade.push('No git repo detected, skipping branch creation');
|
|
525
743
|
}
|
|
526
|
-
|
|
527
|
-
//
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
744
|
+
else {
|
|
745
|
+
// Create and checkout branch (or checkout if exists) using spawn with shell: false
|
|
746
|
+
const checkoutNewResult = spawnSync('git', ['checkout', '-b', branchName], {
|
|
747
|
+
cwd: workingDir,
|
|
748
|
+
shell: false,
|
|
749
|
+
stdio: 'pipe',
|
|
750
|
+
});
|
|
751
|
+
if (checkoutNewResult.status === 0) {
|
|
752
|
+
changesMade.push(`Created branch: ${branchName}`);
|
|
531
753
|
}
|
|
532
|
-
|
|
533
|
-
//
|
|
754
|
+
else {
|
|
755
|
+
// Branch might already exist, try to checkout
|
|
756
|
+
const checkoutResult = spawnSync('git', ['checkout', branchName], {
|
|
757
|
+
cwd: workingDir,
|
|
758
|
+
shell: false,
|
|
759
|
+
stdio: 'pipe',
|
|
760
|
+
});
|
|
761
|
+
if (checkoutResult.status === 0) {
|
|
762
|
+
changesMade.push(`Checked out existing branch: ${branchName}`);
|
|
763
|
+
}
|
|
764
|
+
else {
|
|
765
|
+
// Not a git repo or other error, continue without branching
|
|
766
|
+
changesMade.push('Failed to create or checkout branch, continuing without branching');
|
|
767
|
+
}
|
|
534
768
|
}
|
|
769
|
+
// Update story with branch info
|
|
770
|
+
await updateStoryField(story, 'branch', branchName);
|
|
535
771
|
}
|
|
536
|
-
// Update story with branch info
|
|
537
|
-
updateStoryField(story, 'branch', branchName);
|
|
538
772
|
}
|
|
539
773
|
catch {
|
|
540
774
|
// Not a git repo, continue without branching
|
|
@@ -542,7 +776,7 @@ export async function runImplementationAgent(storyPath, sdlcRoot, options = {})
|
|
|
542
776
|
}
|
|
543
777
|
// Update status to in-progress if not already there
|
|
544
778
|
if (story.frontmatter.status !== 'in-progress') {
|
|
545
|
-
story = updateStoryStatus(story, 'in-progress');
|
|
779
|
+
story = await updateStoryStatus(story, 'in-progress');
|
|
546
780
|
currentStoryPath = story.path;
|
|
547
781
|
changesMade.push('Updated status to in-progress');
|
|
548
782
|
}
|
|
@@ -558,22 +792,28 @@ export async function runImplementationAgent(storyPath, sdlcRoot, options = {})
|
|
|
558
792
|
// Merge changes
|
|
559
793
|
changesMade.push(...tddResult.changesMade);
|
|
560
794
|
if (tddResult.success) {
|
|
561
|
-
|
|
795
|
+
// TDD completed all cycles - now verify with retry support
|
|
796
|
+
changesMade.push('Running final verification...');
|
|
562
797
|
const verification = await verifyImplementation(tddResult.story, workingDir);
|
|
563
|
-
updateStoryField(tddResult.story, 'last_test_run', {
|
|
798
|
+
await updateStoryField(tddResult.story, 'last_test_run', {
|
|
564
799
|
passed: verification.passed,
|
|
565
800
|
failures: verification.failures,
|
|
566
801
|
timestamp: verification.timestamp,
|
|
567
802
|
});
|
|
568
803
|
if (!verification.passed) {
|
|
804
|
+
// TDD final verification failed - this is unexpected since TDD should ensure all tests pass
|
|
805
|
+
// Reset retry count since this is the first failure at this stage
|
|
806
|
+
await resetImplementationRetryCount(tddResult.story);
|
|
569
807
|
return {
|
|
570
808
|
success: false,
|
|
571
809
|
story: parseStory(currentStoryPath),
|
|
572
810
|
changesMade,
|
|
573
|
-
error: `
|
|
811
|
+
error: `TDD implementation blocked: ${verification.failures} test(s) failing after completing all cycles.\nThis is unexpected - TDD cycles should ensure all tests pass.\n\nTest output:\n${truncateTestOutput(verification.testsOutput, 1000)}`,
|
|
574
812
|
};
|
|
575
813
|
}
|
|
576
|
-
|
|
814
|
+
// Success - reset retry count
|
|
815
|
+
await resetImplementationRetryCount(tddResult.story);
|
|
816
|
+
await updateStoryField(tddResult.story, 'implementation_complete', true);
|
|
577
817
|
changesMade.push('Marked implementation_complete: true');
|
|
578
818
|
return {
|
|
579
819
|
success: true,
|
|
@@ -590,66 +830,39 @@ export async function runImplementationAgent(storyPath, sdlcRoot, options = {})
|
|
|
590
830
|
};
|
|
591
831
|
}
|
|
592
832
|
}
|
|
593
|
-
// Standard implementation (non-TDD mode)
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
---
|
|
604
|
-
${options.reworkContext}
|
|
605
|
-
---
|
|
606
|
-
|
|
607
|
-
IMPORTANT: This is a refinement iteration. The previous implementation did not pass review.
|
|
608
|
-
You MUST fix all the issues listed above. Pay special attention to blocker and critical
|
|
609
|
-
severity issues - these must be resolved. Review the specific feedback and make targeted fixes.`;
|
|
610
|
-
}
|
|
611
|
-
prompt += `
|
|
612
|
-
|
|
613
|
-
Execute the implementation plan. For each task:
|
|
614
|
-
1. Read relevant existing files
|
|
615
|
-
2. Make necessary code changes
|
|
616
|
-
3. Write tests if applicable
|
|
617
|
-
4. Verify the changes work
|
|
618
|
-
|
|
619
|
-
Use the available tools to read files, write code, and run commands as needed.`;
|
|
620
|
-
const implementationResult = await runAgentQuery({
|
|
621
|
-
prompt,
|
|
622
|
-
systemPrompt: IMPLEMENTATION_SYSTEM_PROMPT,
|
|
623
|
-
workingDirectory: workingDir,
|
|
833
|
+
// Standard implementation (non-TDD mode) with retry logic
|
|
834
|
+
// Use per-story override if set, otherwise config default (capped at upper bound)
|
|
835
|
+
const maxRetries = getEffectiveMaxImplementationRetries(story, config);
|
|
836
|
+
// Use extracted retry function for better testability
|
|
837
|
+
const retryResult = await attemptImplementationWithRetries({
|
|
838
|
+
story,
|
|
839
|
+
storyPath: currentStoryPath,
|
|
840
|
+
workingDir,
|
|
841
|
+
maxRetries,
|
|
842
|
+
reworkContext: options.reworkContext,
|
|
624
843
|
onProgress: options.onProgress,
|
|
625
|
-
});
|
|
626
|
-
//
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
`;
|
|
632
|
-
// Append to story content
|
|
844
|
+
}, changesMade);
|
|
845
|
+
// If retry loop failed, return the failure result
|
|
846
|
+
if (!retryResult.success) {
|
|
847
|
+
return retryResult;
|
|
848
|
+
}
|
|
849
|
+
// If we get here, verification passed
|
|
633
850
|
const updatedStory = parseStory(currentStoryPath);
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
});
|
|
644
|
-
if (!verification.passed) {
|
|
645
|
-
return {
|
|
646
|
-
success: false,
|
|
647
|
-
story: parseStory(currentStoryPath),
|
|
648
|
-
changesMade,
|
|
649
|
-
error: `Implementation blocked: ${verification.failures} test(s) failing. Fix tests before completing.`,
|
|
650
|
-
};
|
|
851
|
+
// Commit changes after successful standard implementation
|
|
852
|
+
try {
|
|
853
|
+
const commitResult = await commitIfAllTestsPass(workingDir, `feat(${story.slug}): ${story.frontmatter.title}`, config.timeouts?.testTimeout || 300000);
|
|
854
|
+
if (commitResult.committed) {
|
|
855
|
+
changesMade.push(`Committed: ${story.frontmatter.title}`);
|
|
856
|
+
}
|
|
857
|
+
else {
|
|
858
|
+
changesMade.push(`Skipped commit: ${commitResult.reason}`);
|
|
859
|
+
}
|
|
651
860
|
}
|
|
652
|
-
|
|
861
|
+
catch (error) {
|
|
862
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
863
|
+
changesMade.push(`Commit warning: ${errorMsg} (continuing implementation)`);
|
|
864
|
+
}
|
|
865
|
+
await updateStoryField(updatedStory, 'implementation_complete', true);
|
|
653
866
|
changesMade.push('Marked implementation_complete: true');
|
|
654
867
|
return {
|
|
655
868
|
success: true,
|
|
@@ -666,4 +879,204 @@ ${implementationResult}
|
|
|
666
879
|
};
|
|
667
880
|
}
|
|
668
881
|
}
|
|
882
|
+
/**
|
|
883
|
+
* Validate working directory path for safety
|
|
884
|
+
* @param workingDir The working directory path to validate
|
|
885
|
+
* @throws Error if path contains shell metacharacters or traversal attempts
|
|
886
|
+
*/
|
|
887
|
+
function validateWorkingDir(workingDir) {
|
|
888
|
+
// Check for shell metacharacters that could be used in command injection
|
|
889
|
+
if (/[;&|`$()<>]/.test(workingDir)) {
|
|
890
|
+
throw new Error('Invalid working directory: contains shell metacharacters');
|
|
891
|
+
}
|
|
892
|
+
// Prevent path traversal attempts
|
|
893
|
+
const normalizedPath = path.normalize(workingDir);
|
|
894
|
+
if (normalizedPath.includes('..')) {
|
|
895
|
+
throw new Error('Invalid working directory: path traversal attempt detected');
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* Validate branch name for safety
|
|
900
|
+
* @param branchName The branch name to validate
|
|
901
|
+
* @throws Error if branch name contains invalid characters
|
|
902
|
+
*/
|
|
903
|
+
function validateBranchName(branchName) {
|
|
904
|
+
// Git branch names must match safe pattern (alphanumeric, dash, slash, underscore)
|
|
905
|
+
if (!/^[a-zA-Z0-9_/-]+$/.test(branchName)) {
|
|
906
|
+
throw new Error('Invalid branch name: contains unsafe characters');
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Capture the current git diff hash for no-change detection
|
|
911
|
+
* @param workingDir The working directory
|
|
912
|
+
* @returns SHA256 hash of git diff HEAD
|
|
913
|
+
*/
|
|
914
|
+
export function captureCurrentDiffHash(workingDir) {
|
|
915
|
+
try {
|
|
916
|
+
// Security: Validate working directory before use
|
|
917
|
+
validateWorkingDir(workingDir);
|
|
918
|
+
// Use spawnSync with shell: false to prevent command injection
|
|
919
|
+
const result = spawnSync('git', ['diff', 'HEAD'], {
|
|
920
|
+
cwd: workingDir,
|
|
921
|
+
shell: false,
|
|
922
|
+
encoding: 'utf-8',
|
|
923
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
924
|
+
});
|
|
925
|
+
if (result.status === 0 && result.stdout) {
|
|
926
|
+
return createHash('sha256').update(result.stdout).digest('hex');
|
|
927
|
+
}
|
|
928
|
+
// Git command failed, return empty hash
|
|
929
|
+
return '';
|
|
930
|
+
}
|
|
931
|
+
catch (error) {
|
|
932
|
+
// If validation fails or git command fails, return empty hash
|
|
933
|
+
return '';
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
/**
|
|
937
|
+
* Check if changes have occurred since last diff hash
|
|
938
|
+
* @param previousHash Previous diff hash
|
|
939
|
+
* @param currentHash Current diff hash
|
|
940
|
+
* @returns True if changes occurred (hashes are different)
|
|
941
|
+
*/
|
|
942
|
+
export function hasChangesOccurred(previousHash, currentHash) {
|
|
943
|
+
return previousHash !== currentHash;
|
|
944
|
+
}
|
|
945
|
+
/**
|
|
946
|
+
* Sanitize test output to remove ANSI escape sequences and potential injection patterns
|
|
947
|
+
* @param output Test output string
|
|
948
|
+
* @returns Sanitized output
|
|
949
|
+
*/
|
|
950
|
+
export function sanitizeTestOutput(output) {
|
|
951
|
+
if (!output)
|
|
952
|
+
return '';
|
|
953
|
+
let sanitized = output
|
|
954
|
+
// Remove ANSI CSI sequences (SGR parameters - colors, styles)
|
|
955
|
+
.replace(/\x1B\[[0-9;]*[a-zA-Z]/g, '')
|
|
956
|
+
// Remove ANSI DCS sequences (Device Control String)
|
|
957
|
+
.replace(/\x1BP[^\x1B]*\x1B\\/g, '')
|
|
958
|
+
// Remove ANSI PM sequences (Privacy Message)
|
|
959
|
+
.replace(/\x1B\^[^\x1B]*\x1B\\/g, '')
|
|
960
|
+
// Remove ANSI OSC sequences (Operating System Command) - terminated by BEL or ST
|
|
961
|
+
.replace(/\x1B\][^\x07\x1B]*(\x07|\x1B\\)/g, '')
|
|
962
|
+
// Remove any remaining standalone escape characters
|
|
963
|
+
.replace(/\x1B/g, '')
|
|
964
|
+
// Remove other control characters except newline, tab, carriage return
|
|
965
|
+
.replace(/[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F-\x9F]/g, '');
|
|
966
|
+
return sanitized;
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* Truncate test output to prevent overwhelming the LLM
|
|
970
|
+
* @param output Test output string
|
|
971
|
+
* @param maxLength Maximum length (default 5000 chars)
|
|
972
|
+
* @returns Truncated and sanitized output with notice if truncated
|
|
973
|
+
*/
|
|
974
|
+
export function truncateTestOutput(output, maxLength = 5000) {
|
|
975
|
+
if (!output)
|
|
976
|
+
return '';
|
|
977
|
+
// First sanitize to remove ANSI and control characters
|
|
978
|
+
const sanitized = sanitizeTestOutput(output);
|
|
979
|
+
if (sanitized.length <= maxLength) {
|
|
980
|
+
return sanitized;
|
|
981
|
+
}
|
|
982
|
+
const truncated = sanitized.substring(0, maxLength);
|
|
983
|
+
return truncated + `\n\n[Output truncated. Showing first ${maxLength} characters of ${sanitized.length} total.]`;
|
|
984
|
+
}
|
|
985
|
+
/**
|
|
986
|
+
* Detect if errors are related to missing dependencies
|
|
987
|
+
* Returns module names that are missing, if any
|
|
988
|
+
*/
|
|
989
|
+
export function detectMissingDependencies(output) {
|
|
990
|
+
if (!output)
|
|
991
|
+
return [];
|
|
992
|
+
const missingModules = [];
|
|
993
|
+
// Pattern: Cannot find module 'package-name'
|
|
994
|
+
const cannotFindPattern = /Cannot find module ['"]([^'"]+)['"]/g;
|
|
995
|
+
let match;
|
|
996
|
+
while ((match = cannotFindPattern.exec(output)) !== null) {
|
|
997
|
+
const moduleName = match[1];
|
|
998
|
+
// Only include external packages, not relative imports
|
|
999
|
+
if (!moduleName.startsWith('.') && !moduleName.startsWith('/')) {
|
|
1000
|
+
// Extract base package name (handle scoped packages like @types/foo)
|
|
1001
|
+
const baseName = moduleName.startsWith('@')
|
|
1002
|
+
? moduleName.split('/').slice(0, 2).join('/')
|
|
1003
|
+
: moduleName.split('/')[0];
|
|
1004
|
+
if (!missingModules.includes(baseName)) {
|
|
1005
|
+
missingModules.push(baseName);
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
// Pattern: Module not found: Error: Can't resolve 'package-name'
|
|
1010
|
+
const cantResolvePattern = /(?:Module not found|Can't resolve)[:\s]+['"]([^'"]+)['"]/g;
|
|
1011
|
+
while ((match = cantResolvePattern.exec(output)) !== null) {
|
|
1012
|
+
const moduleName = match[1];
|
|
1013
|
+
if (!moduleName.startsWith('.') && !moduleName.startsWith('/')) {
|
|
1014
|
+
const baseName = moduleName.startsWith('@')
|
|
1015
|
+
? moduleName.split('/').slice(0, 2).join('/')
|
|
1016
|
+
: moduleName.split('/')[0];
|
|
1017
|
+
if (!missingModules.includes(baseName)) {
|
|
1018
|
+
missingModules.push(baseName);
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
return missingModules;
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* Build retry prompt for implementation agent
|
|
1026
|
+
* @param testOutput Test failure output
|
|
1027
|
+
* @param buildOutput Build output
|
|
1028
|
+
* @param attemptNumber Current attempt number (1-indexed)
|
|
1029
|
+
* @param maxRetries Maximum number of retries
|
|
1030
|
+
* @returns Prompt string for retry attempt
|
|
1031
|
+
*/
|
|
1032
|
+
export function buildRetryPrompt(testOutput, buildOutput, attemptNumber, maxRetries) {
|
|
1033
|
+
const truncatedTestOutput = truncateTestOutput(testOutput);
|
|
1034
|
+
const truncatedBuildOutput = truncateTestOutput(buildOutput);
|
|
1035
|
+
// Detect if this is a dependency issue
|
|
1036
|
+
const combinedOutput = (buildOutput || '') + '\n' + (testOutput || '');
|
|
1037
|
+
const missingDeps = detectMissingDependencies(combinedOutput);
|
|
1038
|
+
let prompt = `CRITICAL: Tests are failing. You attempted implementation but verification failed.
|
|
1039
|
+
|
|
1040
|
+
This is retry attempt ${attemptNumber} of ${maxRetries}. Previous attempts failed with similar errors.
|
|
1041
|
+
|
|
1042
|
+
`;
|
|
1043
|
+
// Add special guidance for missing dependencies
|
|
1044
|
+
if (missingDeps.length > 0) {
|
|
1045
|
+
prompt += `**DEPENDENCY ISSUE DETECTED**
|
|
1046
|
+
|
|
1047
|
+
The errors indicate missing npm packages: ${missingDeps.join(', ')}
|
|
1048
|
+
|
|
1049
|
+
This is NOT a code bug - the packages need to be installed. Before making any code changes:
|
|
1050
|
+
1. Run \`npm install ${missingDeps.join(' ')}\` to add the missing packages
|
|
1051
|
+
2. If these are type definitions, also run \`npm install -D @types/${missingDeps.filter(d => !d.startsWith('@')).join(' @types/')}\`
|
|
1052
|
+
3. Re-run the build/tests after installing
|
|
1053
|
+
|
|
1054
|
+
`;
|
|
1055
|
+
}
|
|
1056
|
+
if (buildOutput && buildOutput.trim().length > 0) {
|
|
1057
|
+
prompt += `Build Output:
|
|
1058
|
+
\`\`\`
|
|
1059
|
+
${truncatedBuildOutput}
|
|
1060
|
+
\`\`\`
|
|
1061
|
+
|
|
1062
|
+
`;
|
|
1063
|
+
}
|
|
1064
|
+
if (testOutput && testOutput.trim().length > 0) {
|
|
1065
|
+
prompt += `Test Output:
|
|
1066
|
+
\`\`\`
|
|
1067
|
+
${truncatedTestOutput}
|
|
1068
|
+
\`\`\`
|
|
1069
|
+
|
|
1070
|
+
`;
|
|
1071
|
+
}
|
|
1072
|
+
prompt += `Your task:
|
|
1073
|
+
1. ANALYZE the test/build output above - what is actually failing?
|
|
1074
|
+
2. Compare EXPECTED vs ACTUAL results in the errors
|
|
1075
|
+
3. Identify the root cause in your implementation code
|
|
1076
|
+
4. Fix ONLY the production code (do NOT modify tests unless they're clearly wrong)
|
|
1077
|
+
5. Re-run verification
|
|
1078
|
+
|
|
1079
|
+
Focus on fixing the specific failures shown above.`;
|
|
1080
|
+
return prompt;
|
|
1081
|
+
}
|
|
669
1082
|
//# sourceMappingURL=implementation.js.map
|