@codemafia0000/d0
Version:
Claude Multi-Agent Automated Development AI - Revolutionary development environment where multiple AI agents collaborate to automate software development
267 lines (215 loc) β’ 8.05 kB
JavaScript
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const CONSTANTS = require('../lib/constants');
/**
* Team Completion Checker
* Checks worker completion status and handles team-wide notifications
* Part of the d0 CLI agent communication system
*/
class TeamCompletionChecker {
constructor(totalWorkers = CONSTANTS.DEFAULT_WORKERS) {
this.totalWorkers = totalWorkers;
this.d0Dir = CONSTANTS.D0_DIR;
this.tmpDir = CONSTANTS.TMP_DIR;
}
checkWorkerCompletion() {
let completedCount = 0;
const workerStatus = {};
console.log('π Checking team completion status...');
for (let i = 1; i <= this.totalWorkers; i++) {
const completionFile = path.join(this.tmpDir, `worker${i}_done.txt`);
const isCompleted = fs.existsSync(completionFile);
workerStatus[`worker${i}`] = isCompleted;
if (isCompleted) {
completedCount++;
console.log(`β
Worker${i} completed`);
} else {
console.log(`β³ Worker${i} in progress`);
}
}
return { completedCount, workerStatus };
}
sendMessage(recipient, message) {
try {
const cmd = `d0 tell ${recipient} "${message.replace(/"/g, '\\"')}"`;
execSync(cmd, { stdio: 'inherit' });
return true;
} catch (error) {
console.error(`β Failed to send message to ${recipient}:`, error.message);
return false;
}
}
logProgress(message) {
const timestamp = new Date().toISOString();
const progressLog = path.join(process.cwd(), 'progress.log');
try {
fs.appendFileSync(progressLog, `[${timestamp}] ${message}\n`);
} catch (error) {
console.warn('β οΈ Could not write to progress.log:', error.message);
}
}
handleAllWorkersCompleted() {
console.log('π All workers completed! Notifying PRESIDENT...');
const completionMessage = `γProject Milestone Achievedγ
All worker tasks completed successfully!
## Team Results
- Worker1: Frontend β
- Worker2: Backend β
- Worker3: Infrastructure β
## Quality Metrics
- All tests passing
- Performance targets met
- Security requirements satisfied
## Next Steps
Ready for integration testing and deployment.
Project ready for your review.
Timestamp: ${new Date().toLocaleString()}`;
const success = this.sendMessage('president', completionMessage);
if (success) {
this.cleanupCompletionMarkers();
this.createMilestoneMarker();
}
return success;
}
cleanupCompletionMarkers() {
for (let i = 1; i <= this.totalWorkers; i++) {
const completionFile = path.join(this.tmpDir, `worker${i}_done.txt`);
try {
if (fs.existsSync(completionFile)) {
fs.unlinkSync(completionFile);
}
} catch (error) {
console.warn(`β οΈ Could not remove ${completionFile}:`, error.message);
}
}
this.logProgress('All workers completed - Reset for next project phase');
console.log('π Reset for next project phase');
}
createMilestoneMarker() {
const milestoneFile = path.join(this.tmpDir, 'milestone_completed.json');
const milestoneData = {
timestamp: new Date().toISOString(),
completedWorkers: this.totalWorkers,
nextPhase: 'integration_testing'
};
try {
fs.writeFileSync(milestoneFile, JSON.stringify(milestoneData, null, 2));
console.log('β¨ Milestone marker created');
} catch (error) {
console.warn('β οΈ Could not create milestone marker:', error.message);
}
}
generateProgressReport(completedCount, workerStatus) {
const percentage = Math.round((completedCount / this.totalWorkers) * 100);
console.log('\nπ Team Progress Summary');
console.log('β'.repeat(40));
console.log(`Progress: ${completedCount}/${this.totalWorkers} workers completed (${percentage}%)`);
if (completedCount > 0 && completedCount < this.totalWorkers) {
console.log('\nπ Still in progress:');
for (let i = 1; i <= this.totalWorkers; i++) {
if (!workerStatus[`worker${i}`]) {
console.log(` β’ Worker${i}: awaiting completion`);
}
}
const estimatedMinutesRemaining = (this.totalWorkers - completedCount) * 30;
console.log(`\nβ° Estimated completion: ~${estimatedMinutesRemaining} minutes`);
}
return { completedCount, totalWorkers: this.totalWorkers, percentage };
}
resetAllMarkers() {
console.log('π Resetting all completion markers...');
for (let i = 1; i <= this.totalWorkers; i++) {
const completionFile = path.join(this.tmpDir, `worker${i}_done.txt`);
try {
if (fs.existsSync(completionFile)) {
fs.unlinkSync(completionFile);
console.log(` β
Reset worker${i} marker`);
}
} catch (error) {
console.warn(` β οΈ Could not reset worker${i}:`, error.message);
}
}
console.log('π All markers reset');
}
run(statusOnly = false, reset = false) {
console.log('π― Team Completion Checker v1.0');
console.log(`Checking ${this.totalWorkers} workers...`);
if (!fs.existsSync(this.tmpDir)) {
console.error('β .d0/tmp directory not found. Make sure d0 is initialized.');
process.exit(1);
}
if (reset) {
this.resetAllMarkers();
return 0;
}
const { completedCount, workerStatus } = this.checkWorkerCompletion();
if (completedCount === this.totalWorkers) {
if (!statusOnly) {
this.handleAllWorkersCompleted();
}
return 0;
} else {
const progressData = this.generateProgressReport(completedCount, workerStatus);
if (!statusOnly) {
this.logProgress(`Team progress: ${progressData.completedCount}/${progressData.totalWorkers} (${progressData.percentage}%)`);
}
if (completedCount > 0) {
console.log(`\nπ― ${completedCount} worker(s) completed. Continuing...`);
} else {
console.log('\nβ³ All workers still in progress. Check back later.');
}
return 1;
}
}
static showHelp() {
console.log(`
Team Completion Checker - d0 CLI Agent Communication
Usage: node check_team_completion.js [options]
Options:
-h, --help Show this help message
--status-only Only show status, don't send notifications
--reset Reset all completion markers
--workers <n> Override number of workers (default: 3)
Examples:
node check_team_completion.js # Check and handle completion
node check_team_completion.js --status-only # Just show current status
node check_team_completion.js --reset # Reset all markers
node check_team_completion.js --workers 5 # Check 5 workers instead of 3
This script is part of the d0 CLI agent communication system.
It tracks worker completion and automatically notifies the president
when all workers have finished their tasks.
`);
}
}
// CLI interface
function main() {
const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h')) {
TeamCompletionChecker.showHelp();
process.exit(0);
}
const statusOnly = args.includes('--status-only');
const reset = args.includes('--reset');
let totalWorkers = 3;
const workersIndex = args.indexOf('--workers');
if (workersIndex !== -1 && args[workersIndex + 1]) {
totalWorkers = parseInt(args[workersIndex + 1], 10);
if (isNaN(totalWorkers) || totalWorkers < 1) {
console.error('β Workers count must be a positive number');
process.exit(1);
}
}
const checker = new TeamCompletionChecker(totalWorkers);
const exitCode = checker.run(statusOnly, reset);
process.exit(exitCode);
}
// Export for potential use as a module
if (typeof module !== 'undefined' && module.exports) {
module.exports = TeamCompletionChecker;
}
// Run if called directly
if (require.main === module) {
main();
}