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
339 lines • 13.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkflowManager = void 0;
class WorkflowManager {
constructor() {
this.workflowStepMap = new Map();
this.taskWorkflowState = new Map();
this.taskFeatureMap = new Map();
this.initializeWorkflowSteps();
}
/**
* Initialize workflow step configurations
*/
initializeWorkflowSteps() {
const workflowSteps = [
{
step: 'DEFINE_REQUIREMENTS',
agentType: 'SpecWriter',
requiredPreviousSteps: [],
critiqueRequired: true,
errorRecoveryEnabled: true
},
{
step: 'FORMALIZE_CONTRACTS',
agentType: 'Formalizer',
requiredPreviousSteps: ['DEFINE_REQUIREMENTS'],
critiqueRequired: true,
errorRecoveryEnabled: true
},
{
step: 'PROTOTYPE_LOGIC',
agentType: 'Prototyper',
requiredPreviousSteps: ['FORMALIZE_CONTRACTS'],
critiqueRequired: true,
errorRecoveryEnabled: true
},
{
step: 'DESIGN_ARCHITECTURE',
agentType: 'Architect',
requiredPreviousSteps: ['PROTOTYPE_LOGIC'],
critiqueRequired: true,
errorRecoveryEnabled: true
},
{
step: 'IMPLEMENT_CODE',
agentType: 'Coder',
requiredPreviousSteps: ['DESIGN_ARCHITECTURE'],
critiqueRequired: false,
errorRecoveryEnabled: true
},
{
step: 'EXECUTE_TESTS',
agentType: 'Tester',
requiredPreviousSteps: ['IMPLEMENT_CODE'],
critiqueRequired: false,
errorRecoveryEnabled: true
}
];
workflowSteps.forEach(config => {
this.workflowStepMap.set(config.step, config);
});
}
/**
* Initialize workflow states for all tasks
*/
async initializeTaskWorkflowStates(tasks) {
for (const task of tasks) {
const feature = this.taskFeatureMap.get(task.id);
if (feature) {
const initialStep = this.determineInitialWorkflowStep(feature.agent);
this.taskWorkflowState.set(task.id, {
currentStep: initialStep,
completedSteps: []
});
}
}
}
/**
* Set task-feature mapping
*/
setTaskFeatureMapping(taskId, feature) {
this.taskFeatureMap.set(taskId, feature);
}
/**
* Get workflow step configuration
*/
getWorkflowStepConfig(step) {
return this.workflowStepMap.get(step);
}
/**
* Get task workflow state
*/
getTaskWorkflowState(taskId) {
return this.taskWorkflowState.get(taskId);
}
/**
* Update task workflow state
*/
updateTaskWorkflowState(taskId, state) {
this.taskWorkflowState.set(taskId, state);
}
/**
* Check if task is ready for current workflow step
*/
checkWorkflowStepReadiness(taskId) {
const workflowState = this.taskWorkflowState.get(taskId);
if (!workflowState)
return false;
const stepConfig = this.workflowStepMap.get(workflowState.currentStep);
if (!stepConfig)
return false;
// Check if all required previous steps are completed
for (const requiredStep of stepConfig.requiredPreviousSteps) {
if (!workflowState.completedSteps.includes(requiredStep)) {
return false;
}
}
return true;
}
/**
* Advance task to next workflow step
*/
advanceToNextStep(taskId) {
const workflowState = this.taskWorkflowState.get(taskId);
if (!workflowState)
return false;
// Mark current step as completed
workflowState.completedSteps.push(workflowState.currentStep);
// Get next step
const nextStep = this.getNextWorkflowStep(workflowState.currentStep);
if (nextStep) {
workflowState.currentStep = nextStep;
console.log(`Task ${taskId} advanced to workflow step ${nextStep}`);
return true;
}
return false;
}
/**
* Get next workflow step in sequence
*/
getNextWorkflowStep(currentStep) {
const stepOrder = [
'DEFINE_REQUIREMENTS',
'FORMALIZE_CONTRACTS',
'PROTOTYPE_LOGIC',
'DESIGN_ARCHITECTURE',
'IMPLEMENT_CODE',
'EXECUTE_TESTS'
];
const currentIndex = stepOrder.indexOf(currentStep);
if (currentIndex >= 0 && currentIndex < stepOrder.length - 1) {
return stepOrder[currentIndex + 1];
}
return null;
}
/**
* Determine initial workflow step for agent type
*/
determineInitialWorkflowStep(agentType) {
const agentStepMap = {
'SpecWriter': 'DEFINE_REQUIREMENTS',
'Formalizer': 'FORMALIZE_CONTRACTS',
'Prototyper': 'PROTOTYPE_LOGIC',
'Architect': 'DESIGN_ARCHITECTURE',
'Coder': 'IMPLEMENT_CODE',
'Tester': 'EXECUTE_TESTS',
// Default mappings for other agent types
'Reflector': 'DEFINE_REQUIREMENTS',
'Monitor': 'DEFINE_REQUIREMENTS',
'Guide': 'DEFINE_REQUIREMENTS',
'Navigator': 'DEFINE_REQUIREMENTS',
'ChicagoTester': 'EXECUTE_TESTS',
'LondonTester': 'EXECUTE_TESTS',
'PropertyTester': 'EXECUTE_TESTS',
'MutationTester': 'EXECUTE_TESTS',
'Debugger': 'DEFINE_REQUIREMENTS',
'Governor': 'DEFINE_REQUIREMENTS',
'Critique': 'DEFINE_REQUIREMENTS',
'KnowledgeUpdater': 'DEFINE_REQUIREMENTS',
'PerformanceOptimizer': 'IMPLEMENT_CODE',
'QualityGatekeeper': 'EXECUTE_TESTS',
'TestResultDocumenter': 'EXECUTE_TESTS'
};
return agentStepMap[agentType] || 'DEFINE_REQUIREMENTS';
}
/**
* Check if critique is required for current step
*/
isCritiqueRequired(taskId) {
const workflowState = this.taskWorkflowState.get(taskId);
if (!workflowState)
return false;
const stepConfig = this.workflowStepMap.get(workflowState.currentStep);
return stepConfig?.critiqueRequired || false;
}
/**
* Check if error recovery is enabled for current step
*/
isErrorRecoveryEnabled(taskId) {
const workflowState = this.taskWorkflowState.get(taskId);
if (!workflowState)
return false;
const stepConfig = this.workflowStepMap.get(workflowState.currentStep);
return stepConfig?.errorRecoveryEnabled || false;
}
/**
* Get agent type for current workflow step
*/
getAgentTypeForCurrentStep(taskId) {
const workflowState = this.taskWorkflowState.get(taskId);
if (!workflowState)
return null;
const stepConfig = this.workflowStepMap.get(workflowState.currentStep);
return stepConfig?.agentType || null;
}
/**
* Get workflow step guidance
*/
getStepSpecificGuidance(step) {
const guidance = {
'DEFINE_REQUIREMENTS': 'Focus on capturing clear, testable requirements and user stories with acceptance criteria.',
'FORMALIZE_CONTRACTS': 'Transform requirements into formal contracts with preconditions, postconditions, and invariants.',
'PROTOTYPE_LOGIC': 'Create working prototypes that validate contract feasibility and identify implementation challenges.',
'DESIGN_ARCHITECTURE': 'Design system architecture based on validated prototypes and formal contracts.',
'IMPLEMENT_CODE': 'Implement features according to architectural designs and formal specifications.',
'EXECUTE_TESTS': 'Create comprehensive test suites that verify contract compliance and functionality.'
};
return guidance[step] || 'Follow standard development practices for this task.';
}
/**
* Get required inputs for workflow step
*/
getRequiredInputsForStep(step) {
const inputs = {
'DEFINE_REQUIREMENTS': ['stakeholder needs', 'user personas', 'business goals'],
'FORMALIZE_CONTRACTS': ['BDD specifications', 'acceptance criteria', 'domain model'],
'PROTOTYPE_LOGIC': ['formal contracts', 'key interfaces', 'validation scenarios'],
'DESIGN_ARCHITECTURE': ['validated prototypes', 'contract interfaces', 'quality requirements'],
'IMPLEMENT_CODE': ['architectural design', 'interface specifications', 'design patterns'],
'EXECUTE_TESTS': ['implemented code', 'acceptance criteria', 'performance requirements']
};
return inputs[step] || [];
}
/**
* Get expected outputs for workflow step
*/
getExpectedOutputsForStep(step) {
const outputs = {
'DEFINE_REQUIREMENTS': ['.feature files', 'user stories', 'acceptance criteria', 'requirements documentation'],
'FORMALIZE_CONTRACTS': ['interface contracts', 'formal specifications', 'validation rules'],
'PROTOTYPE_LOGIC': ['working prototypes', 'validation results', 'feasibility assessment'],
'DESIGN_ARCHITECTURE': ['architectural diagrams', 'component specifications', 'design documentation'],
'IMPLEMENT_CODE': ['production code', 'unit tests', 'API documentation'],
'EXECUTE_TESTS': ['test suites', 'test reports', 'coverage analysis', 'quality metrics']
};
return outputs[step] || [];
}
/**
* Enhance query for workflow step context
*/
enhanceQueryForWorkflowStep(baseQuery, step) {
const stepKeywords = {
'DEFINE_REQUIREMENTS': ['requirements', 'specifications', 'user stories', 'acceptance criteria'],
'FORMALIZE_CONTRACTS': ['contracts', 'interfaces', 'preconditions', 'postconditions', 'invariants'],
'PROTOTYPE_LOGIC': ['prototype', 'proof of concept', 'validation', 'logic', 'implementation'],
'DESIGN_ARCHITECTURE': ['architecture', 'design', 'patterns', 'structure', 'components'],
'IMPLEMENT_CODE': ['implementation', 'code', 'functions', 'classes', 'modules'],
'EXECUTE_TESTS': ['tests', 'testing', 'validation', 'verification', 'quality assurance']
};
const keywords = stepKeywords[step] || [];
return `${baseQuery} ${keywords.join(' ')}`;
}
/**
* Get targeted node types for workflow step
*/
getTargetedNodeTypes(step) {
const nodeTypeMap = {
'DEFINE_REQUIREMENTS': 'requirement',
'FORMALIZE_CONTRACTS': 'contract',
'PROTOTYPE_LOGIC': 'prototype',
'DESIGN_ARCHITECTURE': 'architecture',
'IMPLEMENT_CODE': 'code',
'EXECUTE_TESTS': 'test'
};
return nodeTypeMap[step] || 'artifact';
}
/**
* Get relevant properties for workflow step
*/
getRelevantProperties(step) {
const propertyMap = {
'DEFINE_REQUIREMENTS': ['title', 'description', 'acceptanceCriteria', 'priority'],
'FORMALIZE_CONTRACTS': ['interface', 'preconditions', 'postconditions', 'invariants'],
'PROTOTYPE_LOGIC': ['implementation', 'validation', 'testResults'],
'DESIGN_ARCHITECTURE': ['components', 'patterns', 'dependencies', 'interfaces'],
'IMPLEMENT_CODE': ['functions', 'classes', 'modules', 'dependencies'],
'EXECUTE_TESTS': ['testCases', 'coverage', 'results', 'performance']
};
return propertyMap[step] || ['name', 'description', 'type'];
}
/**
* Mark task for retry after error
*/
async markTaskForRetry(taskId, step) {
const workflowState = this.taskWorkflowState.get(taskId);
if (workflowState) {
console.log(`Marking task ${taskId} for retry at step ${step || workflowState.currentStep}`);
// Update task state to indicate retry is needed
// The actual retry logic will be handled by the orchestrator
}
}
/**
* Skip current step in workflow
*/
async skipCurrentStep(taskId, reason) {
const workflowState = this.taskWorkflowState.get(taskId);
if (workflowState) {
console.log(`Skipping step ${workflowState.currentStep} for task ${taskId}: ${reason}`);
// Mark current step as completed (even though skipped)
workflowState.completedSteps.push(workflowState.currentStep);
// Advance to next step
const nextStep = this.getNextWorkflowStep(workflowState.currentStep);
if (nextStep) {
workflowState.currentStep = nextStep;
console.log(`Task ${taskId} advanced to workflow step ${nextStep} after skipping`);
}
}
}
/**
* Pause downstream tasks
*/
async pauseDownstreamTasks(taskId, duration) {
console.log(`Pausing downstream tasks for ${duration}ms due to task ${taskId}`);
// In a full implementation, this would identify and pause related tasks
// For now, we log the action and could implement task dependency tracking
}
}
exports.WorkflowManager = WorkflowManager;
//# sourceMappingURL=workflow-manager.js.map