Fix convergence logic and stop button functionality

- Fix convergence: use agentsInRound.length as fallback if agentCount is undefined
- This was causing infinite loops when agentCount wasn't properly initialized
- Simplify convergence check: only schedule next round if !hasConverged
- Add 'stopped' flag to sessions to prevent scheduled rounds from executing
- Modify runRound to check 'stopped' flag and exit early if set
- Modify completeSession to set 'stopped = true' to abort scheduled rounds
- This fixes the issue where stop button wasn't actually stopping the session
- Add better logging for convergence state
This commit is contained in:
Augustin ROUX 2025-10-19 00:08:16 +02:00
parent a42b48dd30
commit a5e184af8e

View File

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