cortexweaver
Version:
CortexWeaver is a command-line interface (CLI) tool that orchestrates a swarm of specialized AI agents, powered by Claude Code and Gemini CLI, to assist in software development. It transforms a high-level project plan (plan.md) into a series of coordinate
251 lines • 11.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoderAgentImplementation = void 0;
const agent_1 = require("../../agent");
const coder_utilities_1 = require("./coder-utilities");
const contract_builder_1 = require("./contract-builder");
const prompt_templates_1 = require("./prompt-templates");
/**
* CoderAgentImplementation contains the core implementation logic for the CoderAgent
*/
class CoderAgentImplementation extends agent_1.Agent {
constructor() {
super(...arguments);
this.codeAttempts = [];
this.testAttempts = 0;
this.maxAttempts = 2;
}
/**
* Execute the coding task with impasse detection
*/
async executeTask() {
if (!this.currentTask || !this.taskContext) {
throw new Error('No task or context available');
}
await this.reportProgress('started', 'Beginning code generation for task');
try {
// Reset attempts for new task
this.codeAttempts = [];
this.testAttempts = 0;
// Get coding standards from Cognitive Canvas
const codingStandards = await this.getCodingStandards();
// Generate initial code implementation
const codeResult = await this.generateCodeWithRetries(codingStandards);
// Generate unit tests
const testsResult = await this.generateTestsWithRetries(codeResult.finalCode);
// Run compilation and tests
await this.verifyImplementation();
// Commit changes to git
const commitResult = await this.commitChanges();
await this.reportProgress('completed', 'Code implementation completed successfully');
return {
codeGenerated: true,
testsGenerated: testsResult.success,
commitCreated: commitResult.success,
compilationSuccessful: true,
testsPassedSuccessful: true,
codeAttempts: this.codeAttempts,
finalCode: codeResult.finalCode,
finalTests: testsResult.tests,
commitHash: commitResult.commitHash
};
}
catch (error) {
if (error.message.includes('IMPASSE')) {
const failureType = coder_utilities_1.CoderUtilities.getFailureType(error);
const attemptCount = failureType === 'testing' ? this.testAttempts : this.codeAttempts.length;
await this.reportImpasse(error.message, {
failureType,
attemptCount,
lastError: coder_utilities_1.CoderUtilities.getLastErrorMessage(error),
conversationHistory: this.conversationHistory,
taskId: this.currentTask.id,
taskTitle: this.currentTask.title,
fullCodeAttempts: this.codeAttempts
}, 'high');
}
throw error;
}
}
/**
* Generate code implementation with retry logic and impasse detection
*/
async generateCodeWithRetries(codingStandards) {
let lastError = '';
for (let attempt = 1; attempt <= this.maxAttempts; attempt++) {
try {
await this.reportProgress('in_progress', `Code generation attempt ${attempt}`);
// Build contract and architectural context
const formalContracts = this.taskContext?.formalContracts;
const architecturalDesign = this.taskContext?.architecturalDesign;
const contractContext = contract_builder_1.ContractBuilder.buildContractContext(formalContracts);
const architecturalContext = contract_builder_1.ContractBuilder.buildArchitecturalContext(architecturalDesign);
const prompt = this.formatPrompt(prompt_templates_1.PromptTemplates.getCodePromptTemplate(), {
taskDescription: this.currentTask.description,
language: this.taskContext.projectInfo?.language || 'typescript',
framework: this.taskContext.projectInfo?.framework || 'node',
dependencies: this.taskContext.dependencies?.join(', ') || '',
codingStandards: coder_utilities_1.CoderUtilities.formatCodingStandards(codingStandards),
...contractContext,
...architecturalContext
});
// Add context from previous attempts if retrying
let fullPrompt = prompt;
if (attempt > 1) {
fullPrompt += `\n\nPrevious attempt failed with error: ${lastError}\n\nPlease fix the issues and provide corrected code.`;
}
const response = await this.sendToClaude(fullPrompt);
const codeAttempt = {
code: response.content,
timestamp: new Date().toISOString(),
attemptNumber: attempt
};
this.codeAttempts.push(codeAttempt);
// Write the code to workspace
await this.writeCodeToWorkspace(response.content);
// Try to compile the code
const compileResult = await this.executeCommand('npm run build || tsc --noEmit');
if (compileResult.exitCode === 0) {
return { success: true, finalCode: response.content };
}
else {
lastError = compileResult.stderr || compileResult.stdout;
codeAttempt.errors = [lastError];
if (attempt >= this.maxAttempts) {
throw new Error(`IMPASSE: Code compilation failed after ${this.maxAttempts} attempts. Last error: ${lastError}`);
}
}
}
catch (error) {
if (error.message.includes('IMPASSE')) {
throw error;
}
lastError = error.message;
if (attempt >= this.maxAttempts) {
throw new Error(`IMPASSE: Code generation failed after ${this.maxAttempts} attempts. Last error: ${lastError}`);
}
}
}
throw new Error(`IMPASSE: Code generation failed after ${this.maxAttempts} attempts. Last error: ${lastError}`);
}
/**
* Generate unit tests with retry logic
*/
async generateTestsWithRetries(codeToTest) {
let lastError = '';
for (let attempt = 1; attempt <= this.maxAttempts; attempt++) {
try {
this.testAttempts = attempt;
await this.reportProgress('in_progress', `Test generation attempt ${attempt}`);
// Build contract testing context
const formalContracts = this.taskContext?.formalContracts;
const contractTestContext = contract_builder_1.ContractBuilder.buildContractTestContext(formalContracts);
const prompt = this.formatPrompt(prompt_templates_1.PromptTemplates.getTestPromptTemplate(), {
codeToTest,
language: this.taskContext.projectInfo?.language || 'typescript',
framework: this.taskContext.projectInfo?.framework || 'node',
...contractTestContext
});
let fullPrompt = prompt;
if (attempt > 1) {
fullPrompt += `\n\nPrevious test attempt failed with error: ${lastError}\n\nPlease fix the test issues.`;
}
const response = await this.sendToClaude(fullPrompt);
// Write tests to workspace
await this.writeTestsToWorkspace(response.content);
// Run tests
const testResult = await this.executeCommand('npm test');
if (testResult.exitCode === 0) {
return { success: true, tests: response.content };
}
else {
lastError = testResult.stderr || testResult.stdout;
if (attempt >= this.maxAttempts) {
throw new Error(`IMPASSE: Tests failed after ${this.maxAttempts} attempts. Last error: ${lastError}`);
}
}
}
catch (error) {
if (error.message.includes('IMPASSE')) {
throw error;
}
lastError = error.message;
if (attempt >= this.maxAttempts) {
throw new Error(`IMPASSE: Test generation failed after ${this.maxAttempts} attempts. Last error: ${lastError}`);
}
}
}
throw new Error(`IMPASSE: Test generation failed after ${this.maxAttempts} attempts. Last error: ${lastError}`);
}
/**
* Verify the implementation by running compilation and tests
*/
async verifyImplementation() {
await this.reportProgress('in_progress', 'Verifying implementation');
// Final compilation check
const compileResult = await this.executeCommand('npm run build || tsc --noEmit');
if (compileResult.exitCode !== 0) {
throw new Error(`Final compilation failed: ${compileResult.stderr || compileResult.stdout}`);
}
// Final test run
const testResult = await this.executeCommand('npm test');
if (testResult.exitCode !== 0) {
throw new Error(`Final tests failed: ${testResult.stderr || testResult.stdout}`);
}
}
/**
* Commit changes to the worktree
*/
async commitChanges() {
try {
await this.reportProgress('in_progress', 'Committing changes to git');
const commitMessage = coder_utilities_1.CoderUtilities.createCommitMessage(this.currentTask);
const commitHash = await this.workspace.commitChanges(this.currentTask.id, commitMessage);
return {
success: true,
commitHash: commitHash
};
}
catch (error) {
console.warn(`Git commit failed: ${error.message}`);
return { success: false };
}
}
/**
* Get coding standards from Cognitive Canvas
*/
async getCodingStandards() {
try {
const pheromones = await this.cognitiveCanvas.getPheromonesByType('coding_standards');
return pheromones.length > 0 ? pheromones[0].metadata : coder_utilities_1.CoderUtilities.getDefaultCodingStandards();
}
catch (error) {
console.warn(`Failed to retrieve coding standards: ${error.message}`);
return coder_utilities_1.CoderUtilities.getDefaultCodingStandards();
}
}
/**
* Get prompt template for the coder agent
*/
getPromptTemplate() {
return prompt_templates_1.PromptTemplates.getCodePromptTemplate();
}
/**
* Write code to the appropriate workspace file
*/
async writeCodeToWorkspace(code) {
const language = this.taskContext.projectInfo?.language || 'typescript';
const paths = coder_utilities_1.CoderUtilities.generateFilePaths(this.currentTask.id, language);
await this.writeFile(paths.codeFile, code);
}
/**
* Write tests to the appropriate workspace file
*/
async writeTestsToWorkspace(tests) {
const language = this.taskContext.projectInfo?.language || 'typescript';
const paths = coder_utilities_1.CoderUtilities.generateFilePaths(this.currentTask.id, language);
await this.writeFile(paths.testFile, tests);
}
}
exports.CoderAgentImplementation = CoderAgentImplementation;
//# sourceMappingURL=coder-agent.js.map