From 59465c79c193c2d83d946eed61065e6a951e55c7 Mon Sep 17 00:00:00 2001 From: Muyue Date: Sat, 18 Oct 2025 23:27:09 +0200 Subject: [PATCH] Refactor: Sequential document collaboration with explicit system initialization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Major improvements: - SYSTEM creates base structure (not an AI agent) in startSession - Round 1 begins with clean document v0 - Each AI receives the EXACT document version from previous agent - Enhanced logging shows 'AGENT n/N' with version numbers - Each AI sees full updated document before modifying - Clear v0 → v1 → v2 → vN progression - Better deduplication through explicit sequential flow The collaboraion is now explicitly sequential: SYSTEM → IA1 (receives v0, creates v1) → IA2 (receives v1, creates v2) etc. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../src/services/collaborativeOrchestrator.js | 73 +++++++++---------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/backend/src/services/collaborativeOrchestrator.js b/backend/src/services/collaborativeOrchestrator.js index 0ec0307..e7b3056 100644 --- a/backend/src/services/collaborativeOrchestrator.js +++ b/backend/src/services/collaborativeOrchestrator.js @@ -82,7 +82,7 @@ class CollaborativeOrchestrator { } /** - * Start a session - create initial document with first agent + * Start a session - SYSTEM creates base structure, then Round 1 begins with Agent 1 */ async startSession(sessionId) { try { @@ -91,27 +91,23 @@ class CollaborativeOrchestrator { console.log(`[Session ${sessionId}] Starting session with ${session.agents.length} agents: ${session.agents.join(', ')}`) - const firstAgent = session.agents[0] - console.log(`[Session ${sessionId}] ${firstAgent} creating initial document...`) + // STEP 1: SYSTEM creates base structure (not an AI agent) + console.log(`[Session ${sessionId}] [SYSTEM] Creating base document structure...`) + const baseStructure = `# ${session.initialPrompt.split('\n')[0] || 'Project Overview'}\n\nThis document will be collaboratively developed by ${session.agents.length} AI specialists.` - // Generate initial document - const initialResponse = await generateAgentResponseSync( - firstAgent, - session.initialPrompt, - '' - ) + console.log(`[Session ${sessionId}] [SYSTEM] Base structure created (${baseStructure.length} chars)`) + console.log(`[Session ${sessionId}] ===== BASE STRUCTURE (v0) =====`) + console.log(baseStructure) + console.log(`[Session ${sessionId}] ===== END BASE STRUCTURE =====`) - const initialDocument = extractSection(initialResponse) - const thinking = extractThinking(initialResponse) - - // Save to DB + // Save base structure to DB const insertStmt = db.prepare( 'INSERT INTO document_versions (session_id, version_number, content, modified_by, modification_reason, round_number) VALUES (?, ?, ?, ?, ?, ?)' ) - insertStmt.run(sessionId, 0, initialDocument, firstAgent, 'Initial document creation', 0) + insertStmt.run(sessionId, 0, baseStructure, 'SYSTEM', 'Base structure creation', 0) // Update session - session.currentDocument = initialDocument + session.currentDocument = baseStructure session.versionNumber = 0 session.started = true session.consecutiveNoChanges = 0 @@ -120,22 +116,17 @@ class CollaborativeOrchestrator { const updateStmt = db.prepare('UPDATE collaborative_sessions SET status = ? WHERE id = ?') updateStmt.run('ongoing', sessionId) - console.log(`[Session ${sessionId}] Initial document created (${initialDocument.length} chars)`) - console.log(`[Session ${sessionId}] ===== INITIAL DOCUMENT =====`) - console.log(initialDocument) - console.log(`[Session ${sessionId}] ===== END INITIAL DOCUMENT =====`) - - // Broadcast initial document + // Broadcast base structure this.broadcast(sessionId, { type: 'initial_document_created', - content: initialDocument, - agentName: firstAgent, - thinking, + content: baseStructure, + agentName: 'SYSTEM', + thinking: 'Created base structure for collaboration', roundNumber: 0 }) - console.log(`[Session ${sessionId}] Scheduling first round in 2s...`) - // Auto-start first round + console.log(`[Session ${sessionId}] Base structure ready. Scheduling Round 1 in 2s...`) + // Auto-start first round with all agents setTimeout(() => this.runRound(sessionId), 2000) } catch (error) { console.error('Error starting session:', error) @@ -160,11 +151,17 @@ class CollaborativeOrchestrator { console.log(`[Session ${sessionId}] ===== ROUND ${roundNumber} START =====`) - // Each agent reviews the document + // Each agent reviews the document SEQUENTIALLY, receiving the updated version from the previous agent for (let i = 0; i < agentsInRound.length; i++) { const agentName = agentsInRound[i] + const documentVersion = session.versionNumber - console.log(`[Session ${sessionId}] Round ${roundNumber}: ${agentName} reviewing...`) + // STEP: Agent receives current document state + console.log(`[Session ${sessionId}] Round ${roundNumber}: AGENT ${i + 1}/${agentsInRound.length} - ${agentName}`) + console.log(`[Session ${sessionId}] ${agentName} receives document v${documentVersion} (${session.currentDocument.length} chars)`) + console.log(`[Session ${sessionId}] ===== DOCUMENT SEEN BY ${agentName} (v${documentVersion}) =====`) + console.log(session.currentDocument) + console.log(`[Session ${sessionId}] ===== END DOCUMENT SEEN BY ${agentName} =====`) // Broadcast that this agent is working this.broadcast(sessionId, { @@ -174,21 +171,22 @@ class CollaborativeOrchestrator { }) try { + console.log(`[Session ${sessionId}] ${agentName} analyzing and generating response...`) const response = await generateAgentResponseSync( agentName, session.initialPrompt, - session.currentDocument + session.currentDocument // <-- This is always the latest version ) const thinking = extractThinking(response) const section = extractSection(response) - console.log(`[Session ${sessionId}] Round ${roundNumber}: ${agentName} response received (${response.length} chars)`) - console.log(`[Session ${sessionId}] --- THINKING ---`) + console.log(`[Session ${sessionId}] ${agentName} response received (${response.length} chars)`) + console.log(`[Session ${sessionId}] --- THINKING (${agentName}) ---`) console.log(thinking) - console.log(`[Session ${sessionId}] --- SECTION ---`) + console.log(`[Session ${sessionId}] --- SECTION (${agentName}) ---`) console.log(section) - console.log(`[Session ${sessionId}] --- END RESPONSE ---`) + console.log(`[Session ${sessionId}] --- END RESPONSE (${agentName}) ---`) // Broadcast agent's thinking in real-time this.broadcast(sessionId, { @@ -215,12 +213,13 @@ class CollaborativeOrchestrator { const isNewSection = !docBefore.includes(headerText) const action = isNewSection ? 'CREATED NEW SECTION' : 'MODIFIED' - console.log(`[Session ${sessionId}] Round ${roundNumber}: ${agentName} ${action}: "${headerText}" (v${session.versionNumber})`) - console.log(`[Session ${sessionId}] ===== DOCUMENT BEFORE (v${session.versionNumber - 1}) =====`) + console.log(`[Session ${sessionId}] ✓ ${agentName} ${action}: "${headerText}"`) + console.log(`[Session ${sessionId}] Document updated: v${session.versionNumber - 1} → v${session.versionNumber} (${docBefore.length} → ${updatedDocument.length} chars)`) + console.log(`[Session ${sessionId}] ===== BEFORE (v${session.versionNumber - 1}) =====`) console.log(docBefore) - console.log(`[Session ${sessionId}] ===== DOCUMENT AFTER (v${session.versionNumber}) =====`) + console.log(`[Session ${sessionId}] ===== AFTER (v${session.versionNumber}) [${agentName}] =====`) console.log(updatedDocument) - console.log(`[Session ${sessionId}] ===== END DOCUMENT =====`) + console.log(`[Session ${sessionId}] ===== END CHANGE =====`) // Save version to DB const insertStmt = db.prepare(