diff --git a/backend/src/services/collaborativeOrchestrator.js b/backend/src/services/collaborativeOrchestrator.js index 3b6caae..e48f81b 100644 --- a/backend/src/services/collaborativeOrchestrator.js +++ b/backend/src/services/collaborativeOrchestrator.js @@ -66,6 +66,7 @@ class CollaborativeOrchestrator { versionNumber: 0, conversationHistory: [], started: false, + stopped: false, // Flag to stop scheduled rounds consecutiveNoChanges: 0, // Counter for convergence lastModifiedAgent: null }) @@ -145,6 +146,12 @@ class CollaborativeOrchestrator { const session = this.activeSessions.get(sessionId) if (!session) return + // Check if session has been stopped + if (session.stopped) { + console.log(`[Session ${sessionId}] Session is stopped, ignoring runRound call`) + return + } + const roundNumber = session.conversationHistory.length + 1 const agentsInRound = session.agents const modifiedAgents = [] @@ -272,7 +279,9 @@ class CollaborativeOrchestrator { session.consecutiveNoChanges = 0 } - console.log(`[Session ${sessionId}] Round ${roundNumber} complete: ${modifiedAgents.length}/${agentsInRound.length} agents modified (consecutive no-change: ${session.consecutiveNoChanges}/${session.agentCount})`) + // Ensure agentCount is properly set + const agentCountToCheck = session.agentCount || agentsInRound.length + console.log(`[Session ${sessionId}] Round ${roundNumber} complete: ${modifiedAgents.length}/${agentsInRound.length} agents modified (consecutive no-change: ${session.consecutiveNoChanges}/${agentCountToCheck})`) // Save round to DB const roundStmt = db.prepare( @@ -293,7 +302,7 @@ class CollaborativeOrchestrator { }) // Broadcast round complete - const hasConverged = session.consecutiveNoChanges >= session.agentCount + const hasConverged = session.consecutiveNoChanges >= agentCountToCheck this.broadcast(sessionId, { type: 'round_complete', roundNumber, @@ -303,11 +312,11 @@ class CollaborativeOrchestrator { }) // Auto-schedule next round if not converged - if (!hasConverged && session.consecutiveNoChanges < session.agentCount) { + if (!hasConverged) { console.log(`[Session ${sessionId}] Scheduling round ${roundNumber + 1} in 2s...`) setTimeout(() => this.runRound(sessionId), 2000) - } else if (hasConverged) { - console.log(`[Session ${sessionId}] CONVERGED! All ${session.agentCount} agents made no changes in consecutive rounds`) + } else { + console.log(`[Session ${sessionId}] CONVERGED! All ${agentCountToCheck} agents made no changes in ${session.consecutiveNoChanges} consecutive rounds`) // All agents agreed, auto-complete setTimeout(() => this.completeSession(sessionId), 2000) } @@ -394,6 +403,7 @@ class CollaborativeOrchestrator { const stmt = db.prepare('UPDATE collaborative_sessions SET status = ?, completed_at = CURRENT_TIMESTAMP, final_document = ? WHERE id = ?') const session = this.activeSessions.get(sessionId) if (session) { + session.stopped = true // Stop any scheduled rounds stmt.run('completed', session.currentDocument, sessionId) console.log(`[Session ${sessionId}] SESSION COMPLETED - Final document: ${session.currentDocument.length} chars, ${session.versionNumber} versions, ${session.conversationHistory.length} rounds`) }