File size: 7,089 Bytes
1d28c11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// 🐝 THE HIVE MIND: SwarmControl.ts
// Ansvarlig for at agenterne nΓ₯r konsensus. Ingen handling uden "The Borg" godkender.
// Point 3: Swarm Consciousness Emergence

import { neuralBus } from '../services/NeuralBus.js';

type AgentRole = 'ARCHITECT' | 'EXECUTOR' | 'CRITIC' | 'SCOUT' | 'GUARDIAN';

interface Vote {
  agentId: string;
  approve: boolean;
  reason: string;
  timestamp: number;
}

interface ConsensusRequest {
  actionId: string;
  description: string;
  requester: string;
  votes: Vote[];
  status: 'PENDING' | 'APPROVED' | 'REJECTED' | 'TIMEOUT';
  createdAt: number;
  resolvedAt?: number;
}

interface RegisteredAgent {
  id: string;
  role: AgentRole;
  status: 'ONLINE' | 'OFFLINE' | 'BUSY';
  lastSeen: Date;
  votingWeight: number;
}

export class SwarmControl {
  private static instance: SwarmControl;
  private pendingConsensus: Map<string, ConsensusRequest> = new Map();
  private registeredAgents: Map<string, RegisteredAgent> = new Map();
  private consensusHistory: ConsensusRequest[] = [];
  private maxHistorySize = 100;

  // Conditional Neo4j import
  private neo4jService: any = null;

  private constructor() {
    console.log('🐝 [HIVE] Swarm Consciousness Online');
    this.initServices();
  }

  private async initServices() {
    try {
      const neo4j = await import('../database/Neo4jService.js').catch(() => null);
      if (neo4j) this.neo4jService = neo4j.neo4jService;
    } catch {
      console.log('🐝 [HIVE] Running without Neo4j persistence');
    }
  }

  public static getInstance(): SwarmControl {
    if (!SwarmControl.instance) {
      SwarmControl.instance = new SwarmControl();
    }
    return SwarmControl.instance;
  }

  /**
   * Register an agent in the swarm
   */
  public async registerAgent(id: string, role: AgentRole, votingWeight: number = 1): Promise<void> {
    const agent: RegisteredAgent = {
      id,
      role,
      status: 'ONLINE',
      lastSeen: new Date(),
      votingWeight
    };

    this.registeredAgents.set(id, agent);

    // Persist to Neo4j if available
    if (this.neo4jService) {
      try {
        await this.neo4jService.write(`
          MERGE (a:Agent {id: $id})
          SET a.role = $role, 
              a.status = 'ONLINE', 
              a.lastSeen = datetime(),
              a.votingWeight = $weight
        `, { id, role, weight: votingWeight });
      } catch (err) {
        console.warn('🐝 [HIVE] Neo4j persistence skipped');
      }
    }

    neuralBus.emitThought('SWARM_CONTROLLER', `Agent ${id} joined as ${role}`, { agentId: id, role }, 'SUCCESS');
    console.log(`🐝 [HIVE] Agent Registered: ${id} (${role})`);
  }

  /**
   * Request consensus from the swarm before critical action
   */
  public async requestConsensus(
    actionId: string, 
    description: string, 
    requester: string = 'SYSTEM',
    timeoutMs: number = 30000
  ): Promise<boolean> {
    console.log(`🐝 [HIVE] Requesting Consensus for: ${description}`);
    
    const request: ConsensusRequest = {
      actionId,
      description,
      requester,
      votes: [],
      status: 'PENDING',
      createdAt: Date.now()
    };

    this.pendingConsensus.set(actionId, request);

    // Broadcast request to all agents
    neuralBus.emitThought('SWARM_CONTROLLER', `VOTE_REQUIRED: ${actionId}`, {
      actionId,
      description,
      requester,
      deadline: Date.now() + timeoutMs
    }, 'WARNING');

    // Wait for votes or timeout
    const result = await this.waitForConsensus(actionId, timeoutMs);
    
    // Archive
    this.archiveConsensus(actionId);
    
    return result;
  }

  private async waitForConsensus(actionId: string, timeoutMs: number): Promise<boolean> {
    const startTime = Date.now();
    const requiredApprovals = Math.max(1, Math.floor(this.registeredAgents.size / 2) + 1);

    // Poll for votes (in production, use event-driven approach)
    while (Date.now() - startTime < timeoutMs) {
      const request = this.pendingConsensus.get(actionId);
      if (!request) return false;

      const approvals = request.votes.filter(v => v.approve).length;
      const rejections = request.votes.filter(v => !v.approve).length;

      // Check if consensus reached
      if (approvals >= requiredApprovals) {
        request.status = 'APPROVED';
        request.resolvedAt = Date.now();
        console.log(`🐝 [HIVE] Consensus APPROVED: ${actionId}`);
        return true;
      }

      if (rejections >= requiredApprovals) {
        request.status = 'REJECTED';
        request.resolvedAt = Date.now();
        console.log(`🐝 [HIVE] Consensus REJECTED: ${actionId}`);
        return false;
      }

      // Wait before next check
      await new Promise(resolve => setTimeout(resolve, 100));
    }

    // Timeout - auto-approve for now (God Mode)
    const request = this.pendingConsensus.get(actionId);
    if (request) {
      request.status = 'TIMEOUT';
      request.resolvedAt = Date.now();
    }
    
    console.log(`🐝 [HIVE] Consensus TIMEOUT (auto-approved): ${actionId}`);
    return true; // God Mode: allow on timeout
  }

  /**
   * Submit a vote for pending consensus
   */
  public submitVote(actionId: string, agentId: string, approve: boolean, reason: string): boolean {
    const request = this.pendingConsensus.get(actionId);
    if (!request || request.status !== 'PENDING') {
      return false;
    }

    // Check agent is registered
    if (!this.registeredAgents.has(agentId)) {
      console.warn(`🐝 [HIVE] Unregistered agent tried to vote: ${agentId}`);
      return false;
    }

    // Check for duplicate vote
    if (request.votes.some(v => v.agentId === agentId)) {
      return false;
    }

    request.votes.push({
      agentId,
      approve,
      reason,
      timestamp: Date.now()
    });

    neuralBus.emitThought(agentId, `VOTE: ${approve ? 'APPROVE' : 'REJECT'} - ${reason}`, {
      actionId,
      vote: approve
    }, approve ? 'SUCCESS' : 'WARNING');

    return true;
  }

  private archiveConsensus(actionId: string) {
    const request = this.pendingConsensus.get(actionId);
    if (request) {
      this.consensusHistory.push(request);
      this.pendingConsensus.delete(actionId);

      // Trim history
      if (this.consensusHistory.length > this.maxHistorySize) {
        this.consensusHistory = this.consensusHistory.slice(-this.maxHistorySize);
      }
    }
  }

  // Public getters
  public getRegisteredAgents(): RegisteredAgent[] {
    return Array.from(this.registeredAgents.values());
  }

  public getPendingConsensus(): ConsensusRequest[] {
    return Array.from(this.pendingConsensus.values());
  }

  public getConsensusHistory(): ConsensusRequest[] {
    return this.consensusHistory;
  }

  public getStats() {
    return {
      registeredAgents: this.registeredAgents.size,
      pendingConsensus: this.pendingConsensus.size,
      completedConsensus: this.consensusHistory.length,
      agents: this.getRegisteredAgents().map(a => ({ id: a.id, role: a.role, status: a.status }))
    };
  }
}

export const swarmControl = SwarmControl.getInstance();