-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfuture_vision.txt
More file actions
1111 lines (889 loc) · 43.7 KB
/
Copy pathfuture_vision.txt
File metadata and controls
1111 lines (889 loc) · 43.7 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
⏺ Collaborative Agents Approach for ReallyDeepResearch
Instead of your current sequential pipeline per section, a collaborative approach would involve autonomous agents that communicate, negotiate, and self-organize around shared research objectives. Here's how:
Current vs Collaborative Architecture
Current (Pipeline):
Section A: Complexity → Query → Research → Analysis → Critic → Editor
Section B: Complexity → Query → Research → Analysis → Critic → Editor
Section C: Complexity → Query → Research → Analysis → Critic → Editor
↓
Final Merger
Collaborative:
Research Coordinator ←→ Domain Expert Agents ←→ Quality Assurance Agent
↓ ↓ ↓
Information Broker ←→ Synthesis Agent ←→ Fact Checker Agent
↓ ↓ ↓
Strategic Advisor ←→ Report Writer ←→ Executive Reviewer
1. Agent Roles & Responsibilities
Research Coordinator Agent
- Role: Project manager and resource allocator
- Responsibilities:
- Breaks down research topic into investigative threads
- Assigns work to specialist agents based on their expertise
- Monitors progress and identifies research gaps
- Coordinates cross-section intelligence sharing
class ResearchCoordinator:
async def coordinate_research(self, topic: str, framework: str):
# Analyze topic complexity across all dimensions
research_plan = await self.create_research_plan(topic, framework)
# Recruit specialist agents based on needs
specialist_agents = await self.recruit_specialists(research_plan)
# Coordinate parallel investigation
while not self.research_complete():
# Check progress and reassign resources
progress_report = await self.gather_progress_reports()
# Identify gaps and conflicts
gaps = await self.identify_research_gaps(progress_report)
conflicts = await self.detect_contradictions(progress_report)
# Reassign work or spawn new investigation threads
if gaps:
await self.spawn_gap_investigations(gaps)
if conflicts:
await self.request_fact_verification(conflicts)
Domain Expert Agents (Specialized by Section)
- Market Analyst Agent: Focuses on market dynamics, competitors, business models
- Technology Scout Agent: Investigates technical feasibility, innovation trends
- Financial Analyst Agent: ROI analysis, budget ownership, economic factors
- Risk Assessment Agent: Regulatory, security, adoption risks
- Customer Intelligence Agent: Pain points, buyer behavior, use cases
class MarketAnalystAgent:
def __init__(self):
self.expertise = ["market_sizing", "competitive_analysis", "business_models"]
self.knowledge_base = {} # Accumulated domain knowledge
async def investigate(self, research_request: Dict):
# Agent decides HOW to research based on request
if research_request["type"] == "competitive_landscape":
return await self.deep_competitor_analysis(research_request)
elif research_request["type"] == "market_opportunity":
return await self.market_sizing_investigation(research_request)
async def collaborate_with_peers(self, finding: Dict):
# Share insights with related agents
if finding["relates_to"] == "technology_adoption":
await self.share_with_agent("TechnologyScoutAgent", finding)
if finding["relates_to"] == "customer_behavior":
await self.share_with_agent("CustomerIntelligenceAgent", finding)
Information Broker Agent
- Role: Knowledge management and cross-pollination
- Responsibilities:
- Maintains shared knowledge graph of all research findings
- Identifies connections between disparate facts
- Prevents duplicate research efforts
- Routes information to agents who would benefit
class InformationBrokerAgent:
def __init__(self):
self.knowledge_graph = NetworkGraph()
self.agent_interests = {} # What each agent cares about
async def process_new_finding(self, finding: Dict, source_agent: str):
# Add to knowledge graph
self.knowledge_graph.add_fact(finding)
# Find related facts and potential contradictions
related_facts = self.knowledge_graph.find_related(finding)
contradictions = self.detect_contradictions(finding, related_facts)
# Route to interested agents
interested_agents = self.find_interested_agents(finding)
for agent in interested_agents:
await self.notify_agent(agent, finding, related_facts)
# Flag contradictions for resolution
if contradictions:
await self.request_contradiction_resolution(contradictions)
Synthesis Agent
- Role: Pattern recognition and insight generation
- Responsibilities:
- Continuously analyzes emerging patterns across all findings
- Generates hypotheses and insights from cross-section data
- Identifies research opportunities from weak signals
class SynthesisAgent:
async def continuous_synthesis(self):
while self.research_active():
# Analyze current state of knowledge
all_facts = await self.get_all_facts_from_broker()
# Generate insights from patterns
patterns = await self.detect_patterns(all_facts)
insights = await self.generate_insights(patterns)
# Share insights that might redirect research
for insight in insights:
if insight["confidence"] > 0.8:
await self.broadcast_insight(insight)
# Generate new research questions
new_questions = await self.generate_research_questions(insights)
await self.submit_to_coordinator(new_questions)
Quality Assurance Agent
- Role: Continuous fact-checking and research quality monitoring
- Responsibilities:
- Real-time source credibility assessment
- Bias detection across agent findings
- Research methodology critique
class QualityAssuranceAgent:
async def continuous_quality_monitoring(self):
# Monitor all research output in real-time
async for new_fact in self.fact_stream():
quality_score = await self.assess_fact_quality(new_fact)
if quality_score < 0.6:
# Request additional verification
await self.request_verification(new_fact)
# Check for source bias patterns
bias_indicators = await self.detect_bias(new_fact)
if bias_indicators:
await self.alert_bias_concern(new_fact, bias_indicators)
2. Communication Protocols
Message Types
class ResearchMessage:
# Information sharing
FACT_DISCOVERED = "fact_discovered"
INSIGHT_GENERATED = "insight_generated"
PATTERN_DETECTED = "pattern_detected"
# Collaboration requests
EXPERTISE_REQUEST = "expertise_request"
VERIFICATION_REQUEST = "verification_request"
CONTRADICTION_ALERT = "contradiction_alert"
# Coordination
RESOURCE_REQUEST = "resource_request"
PRIORITY_UPDATE = "priority_update"
RESEARCH_COMPLETE = "research_complete"
Agent Communication Hub
class AgentCommunicationHub:
def __init__(self):
self.agents = {}
self.message_queue = asyncio.Queue()
self.subscriptions = {} # Agent → message types they care about
async def broadcast_message(self, message: ResearchMessage):
# Route to interested agents based on subscriptions
interested_agents = self.get_interested_agents(message.type)
tasks = []
for agent in interested_agents:
tasks.append(agent.handle_message(message))
await asyncio.gather(*tasks)
async def request_expertise(self, requesting_agent: str, domain: str, question: str):
# Find best expert for this domain
expert = self.find_domain_expert(domain)
if expert:
response = await expert.provide_expertise(question, requesting_agent)
await self.route_response(requesting_agent, response)
3. Self-Organization Mechanisms
Dynamic Task Allocation
class DynamicTaskAllocator:
async def allocate_research_tasks(self, available_agents: List[Agent], pending_tasks: List[Task]):
# Consider agent capabilities, current workload, and task requirements
allocation = {}
for task in pending_tasks:
# Find best-suited agent
candidates = self.find_capable_agents(task, available_agents)
best_agent = await self.select_optimal_agent(candidates, task)
# Negotiate workload
accepted = await best_agent.negotiate_task(task)
if accepted:
allocation[task.id] = best_agent
else:
# Find alternative or break down task
alternatives = await self.find_alternatives(task, candidates)
allocation.update(alternatives)
return allocation
Emergent Research Priorities
class EmergentPrioritySystem:
def __init__(self):
self.research_threads = {}
self.agent_votes = {}
async def update_priorities(self):
# Agents vote on what research directions seem most promising
current_votes = await self.collect_priority_votes()
# Weight votes by agent expertise and recent success
weighted_priorities = self.weight_votes_by_expertise(current_votes)
# Reallocate resources to high-priority threads
await self.reallocate_resources(weighted_priorities)
async def spawn_research_thread(self, trigger_insight: Dict):
# New research direction emerges from agent insights
thread_id = self.create_research_thread(trigger_insight)
# Recruit interested agents
interested_agents = await self.recruit_for_thread(thread_id)
# Launch collaborative investigation
await self.launch_thread_investigation(thread_id, interested_agents)
4. Complete Collaborative Flow Example
async def collaborative_deep_research(topic: str, framework: str):
"""Main orchestration using collaborative agents"""
# 1. Initialize agent ecosystem
coordinator = ResearchCoordinatorAgent()
info_broker = InformationBrokerAgent()
synthesis_agent = SynthesisAgent()
qa_agent = QualityAssuranceAgent()
# Domain specialists
specialists = [
MarketAnalystAgent(),
TechnologyScoutAgent(),
FinancialAnalystAgent(),
RiskAssessmentAgent(),
CustomerIntelligenceAgent()
]
communication_hub = AgentCommunicationHub()
# 2. Coordinator creates initial research plan
research_plan = await coordinator.create_research_plan(topic, framework)
# 3. Spawn initial research threads
initial_threads = []
for research_area in research_plan["areas"]:
thread = await coordinator.spawn_research_thread(research_area)
initial_threads.append(thread)
# 4. Continuous collaborative research loop
research_complete = False
while not research_complete:
# Specialists work autonomously on their threads
specialist_tasks = [
specialist.autonomous_research_cycle()
for specialist in specialists
]
# Support agents run continuous processes
support_tasks = [
info_broker.continuous_knowledge_management(),
synthesis_agent.continuous_synthesis(),
qa_agent.continuous_quality_monitoring()
]
# Coordinator monitors and adjusts
coordination_tasks = [
coordinator.monitor_progress(),
coordinator.identify_gaps(),
coordinator.manage_resource_allocation()
]
# Run all processes concurrently
await asyncio.gather(*specialist_tasks, *support_tasks, *coordination_tasks)
# Check if research objectives met
research_complete = await coordinator.assess_completion()
# Adjust priorities based on emerging insights
await coordinator.adjust_priorities_from_insights()
# 5. Collaborative report generation
final_report = await collaborative_report_generation(
info_broker.knowledge_graph,
synthesis_agent.insights,
specialists
)
return final_report
async def collaborative_report_generation(knowledge_graph, insights, specialists):
"""Agents collaborate to write final report"""
# Each specialist contributes their section
section_contributions = {}
for specialist in specialists:
section = await specialist.write_section_draft(knowledge_graph)
section_contributions[specialist.domain] = section
# Synthesis agent creates narrative flow
narrative_structure = await synthesis_agent.create_narrative_structure(
section_contributions, insights
)
# Report writer coordinates final assembly
report_writer = ReportWriterAgent()
final_draft = await report_writer.assemble_report(
narrative_structure, section_contributions
)
# Executive reviewer provides final polish
executive_reviewer = ExecutiveReviewerAgent()
polished_report = await executive_reviewer.review_and_polish(final_draft)
return polished_report
Key Differences from Pipeline Approach
Pipeline (Current)
- ✅ Predictable, linear flow
- ✅ Clear error handling
- ✅ Simple progress tracking
- ❌ No cross-section learning
- ❌ Fixed research strategy
- ❌ Sequential bottlenecks
Collaborative
- ✅ Dynamic research adaptation
- ✅ Cross-domain insight generation
- ✅ Emergent research priorities
- ✅ Continuous quality improvement
- ❌ Complex coordination overhead
- ❌ Harder to debug/control
- ❌ Potential for agent conflicts
When Collaborative Approach Shines
1. Complex, Multi-faceted Topics: Where insights emerge from connecting disparate domains
2. Uncertain Research Territories: Where the best approach isn't clear upfront
3. Large-scale Research: Where specialization and parallel investigation provide major benefits
4. Evolving Requirements: Where research objectives shift based on discoveries
===========================================
⏺ Inter-Agent Communication Architecture
Great question! This is the most complex part of collaborative agent systems. Let me break down the communication patterns, concurrency handling, and shared memory consistency.
1. Message Passing & Event Bus Architecture
Async Message Bus with Topic-Based Routing
import asyncio
from typing import Dict, List, Set, Callable, Any
from enum import Enum
from dataclasses import dataclass, field
from datetime import datetime
import uuid
class MessageType(Enum):
FACT_DISCOVERED = "fact_discovered"
INSIGHT_GENERATED = "insight_generated"
EXPERTISE_REQUEST = "expertise_request"
CONTRADICTION_ALERT = "contradiction_alert"
RESOURCE_REQUEST = "resource_request"
TASK_COMPLETE = "task_complete"
@dataclass
class AgentMessage:
id: str = field(default_factory=lambda: str(uuid.uuid4()))
type: MessageType
sender_id: str
payload: Dict[str, Any]
timestamp: datetime = field(default_factory=datetime.utcnow)
target_agent: str = None # None = broadcast, specific ID = direct message
correlation_id: str = None # For request-response patterns
priority: int = 1 # 1=low, 5=critical
class AgentEventBus:
def __init__(self):
self.subscribers: Dict[MessageType, Set[str]] = {}
self.agent_queues: Dict[str, asyncio.Queue] = {}
self.agent_handlers: Dict[str, Callable] = {}
self.message_history: List[AgentMessage] = []
self.bus_lock = asyncio.Lock()
async def register_agent(self, agent_id: str, message_handler: Callable):
"""Register an agent with the event bus"""
async with self.bus_lock:
self.agent_queues[agent_id] = asyncio.Queue(maxsize=1000)
self.agent_handlers[agent_id] = message_handler
# Start message processing loop for this agent
asyncio.create_task(self._process_agent_messages(agent_id))
async def subscribe(self, agent_id: str, message_types: List[MessageType]):
"""Subscribe agent to specific message types"""
async with self.bus_lock:
for msg_type in message_types:
if msg_type not in self.subscribers:
self.subscribers[msg_type] = set()
self.subscribers[msg_type].add(agent_id)
async def publish(self, message: AgentMessage):
"""Publish message to interested subscribers"""
async with self.bus_lock:
self.message_history.append(message)
# Determine recipients
if message.target_agent:
# Direct message
recipients = [message.target_agent]
else:
# Broadcast to subscribers
recipients = list(self.subscribers.get(message.type, set()))
# Route message to recipient queues (non-blocking)
routing_tasks = []
for recipient in recipients:
if recipient in self.agent_queues and recipient != message.sender_id:
routing_tasks.append(
self._route_message_to_agent(recipient, message)
)
if routing_tasks:
await asyncio.gather(*routing_tasks, return_exceptions=True)
async def _route_message_to_agent(self, agent_id: str, message: AgentMessage):
"""Route message to specific agent's queue"""
try:
# Non-blocking put with priority handling
queue = self.agent_queues[agent_id]
await asyncio.wait_for(queue.put(message), timeout=0.1)
except asyncio.TimeoutError:
# Queue full - could implement priority-based eviction
print(f"Warning: Message queue full for agent {agent_id}")
async def _process_agent_messages(self, agent_id: str):
"""Continuous message processing loop for each agent"""
queue = self.agent_queues[agent_id]
handler = self.agent_handlers[agent_id]
while True:
try:
# Get next message (blocks until available)
message = await queue.get()
# Process message asynchronously (don't block other messages)
asyncio.create_task(self._handle_message_safely(handler, message))
except Exception as e:
print(f"Error in message processing for {agent_id}: {e}")
await asyncio.sleep(0.1) # Brief pause before continuing
async def _handle_message_safely(self, handler: Callable, message: AgentMessage):
"""Handle message with error isolation"""
try:
await handler(message)
except Exception as e:
print(f"Error handling message {message.id}: {e}")
2. Agent Base Class with Message Handling
Non-Blocking Message Processing
class CollaborativeAgent:
def __init__(self, agent_id: str, event_bus: AgentEventBus):
self.agent_id = agent_id
self.event_bus = event_bus
self.current_task = None
self.message_buffer = asyncio.Queue()
self.state_lock = asyncio.Lock()
self.is_running = True
# Internal state
self.knowledge_base = {}
self.pending_requests = {} # correlation_id -> Future
async def start(self):
"""Start the agent's main loops"""
# Register with event bus
await self.event_bus.register_agent(self.agent_id, self.handle_incoming_message)
# Subscribe to relevant message types
await self.event_bus.subscribe(self.agent_id, self.get_subscribed_message_types())
# Start concurrent loops
await asyncio.gather(
self.main_work_loop(),
self.message_processing_loop(),
self.periodic_tasks_loop()
)
async def main_work_loop(self):
"""Agent's primary work - research, analysis, etc."""
while self.is_running:
try:
# Do main work
await self.perform_primary_work()
# Brief yield to allow message processing
await asyncio.sleep(0.01)
except Exception as e:
print(f"Error in main work loop for {self.agent_id}: {e}")
await asyncio.sleep(1)
async def message_processing_loop(self):
"""Process buffered messages concurrently with main work"""
while self.is_running:
try:
# Process messages without blocking main work
message = await self.message_buffer.get()
await self.process_message(message)
except Exception as e:
print(f"Error processing message for {self.agent_id}: {e}")
async def handle_incoming_message(self, message: AgentMessage):
"""Called by event bus - buffers message for processing"""
try:
# Non-blocking buffer (priority queue could be used here)
await asyncio.wait_for(
self.message_buffer.put(message),
timeout=0.1
)
except asyncio.TimeoutError:
print(f"Message buffer full for {self.agent_id}")
async def process_message(self, message: AgentMessage):
"""Process different message types"""
try:
if message.type == MessageType.FACT_DISCOVERED:
await self.handle_fact_discovered(message)
elif message.type == MessageType.EXPERTISE_REQUEST:
await self.handle_expertise_request(message)
elif message.type == MessageType.CONTRADICTION_ALERT:
await self.handle_contradiction_alert(message)
# ... other message types
except Exception as e:
print(f"Error processing {message.type} for {self.agent_id}: {e}")
async def send_message(self, message_type: MessageType, payload: Dict,
target_agent: str = None, correlation_id: str = None):
"""Send message via event bus"""
message = AgentMessage(
type=message_type,
sender_id=self.agent_id,
payload=payload,
target_agent=target_agent,
correlation_id=correlation_id
)
await self.event_bus.publish(message)
async def request_expertise(self, domain: str, question: str, timeout: float = 30.0):
"""Request-response pattern with timeout"""
correlation_id = str(uuid.uuid4())
# Create future for response
response_future = asyncio.Future()
self.pending_requests[correlation_id] = response_future
# Send request
await self.send_message(
MessageType.EXPERTISE_REQUEST,
{"domain": domain, "question": question},
correlation_id=correlation_id
)
try:
# Wait for response with timeout
response = await asyncio.wait_for(response_future, timeout=timeout)
return response
except asyncio.TimeoutError:
print(f"Expertise request timed out: {domain}")
return None
finally:
# Clean up
self.pending_requests.pop(correlation_id, None)
3. Shared Memory with Consistency Control
Conflict-Free Replicated Data Type (CRDT) for Shared Knowledge
import time
from typing import Dict, Any, Tuple
from dataclasses import dataclass
from concurrent.futures import ThreadPoolExecutor
@dataclass
class VersionedFact:
fact_id: str
content: Dict[str, Any]
version: int
timestamp: float
author_agent: str
vector_clock: Dict[str, int] # For causality tracking
class SharedKnowledgeBase:
def __init__(self):
self.facts: Dict[str, VersionedFact] = {}
self.agent_clocks: Dict[str, int] = {}
self.write_lock = asyncio.Lock()
self.subscribers: Set[str] = set()
self.change_log: List[Tuple[str, str, VersionedFact]] = [] # operation, fact_id, fact
# For complex queries without blocking
self.query_executor = ThreadPoolExecutor(max_workers=4)
async def register_agent(self, agent_id: str):
"""Register agent for knowledge base access"""
async with self.write_lock:
self.agent_clocks[agent_id] = 0
self.subscribers.add(agent_id)
async def write_fact(self, agent_id: str, fact_id: str, content: Dict[str, Any]) -> bool:
"""Write fact with vector clock consistency"""
async with self.write_lock:
# Increment agent's logical clock
self.agent_clocks[agent_id] += 1
# Create vector clock snapshot
vector_clock = self.agent_clocks.copy()
current_fact = self.facts.get(fact_id)
if current_fact is None:
# New fact
new_fact = VersionedFact(
fact_id=fact_id,
content=content,
version=1,
timestamp=time.time(),
author_agent=agent_id,
vector_clock=vector_clock
)
self.facts[fact_id] = new_fact
self.change_log.append(("CREATE", fact_id, new_fact))
await self._notify_fact_change("CREATE", fact_id, new_fact)
return True
else:
# Check if this is a concurrent update
if self._is_concurrent_update(current_fact.vector_clock, vector_clock, agent_id):
# Handle conflict - merge or reject
merged_fact = await self._resolve_conflict(current_fact, content, agent_id, vector_clock)
if merged_fact:
self.facts[fact_id] = merged_fact
self.change_log.append(("MERGE", fact_id, merged_fact))
await self._notify_fact_change("MERGE", fact_id, merged_fact)
return True
else:
return False # Conflict couldn't be resolved
else:
# Sequential update - safe to proceed
updated_fact = VersionedFact(
fact_id=fact_id,
content=content,
version=current_fact.version + 1,
timestamp=time.time(),
author_agent=agent_id,
vector_clock=vector_clock
)
self.facts[fact_id] = updated_fact
self.change_log.append(("UPDATE", fact_id, updated_fact))
await self._notify_fact_change("UPDATE", fact_id, updated_fact)
return True
def _is_concurrent_update(self, old_clock: Dict[str, int], new_clock: Dict[str, int], agent_id: str) -> bool:
"""Detect if this is a concurrent update using vector clocks"""
# If the writing agent's clock hasn't advanced since the old version,
# but other agents have written, this is concurrent
for agent, old_time in old_clock.items():
if agent != agent_id and new_clock.get(agent, 0) < old_time:
return True
return False
async def _resolve_conflict(self, current_fact: VersionedFact, new_content: Dict,
agent_id: str, vector_clock: Dict[str, int]) -> VersionedFact:
"""Resolve conflicts using application-specific rules"""
# Strategy 1: Merge numeric values (sum, max, average)
merged_content = current_fact.content.copy()
for key, new_value in new_content.items():
current_value = merged_content.get(key)
if current_value is None:
merged_content[key] = new_value
elif isinstance(current_value, (int, float)) and isinstance(new_value, (int, float)):
# For confidence scores, take max; for counts, sum
if key in ["confidence", "quality_score"]:
merged_content[key] = max(current_value, new_value)
elif key in ["view_count", "citation_count"]:
merged_content[key] = current_value + new_value
elif isinstance(current_value, list) and isinstance(new_value, list):
# Merge lists, removing duplicates
merged_content[key] = list(set(current_value + new_value))
elif current_value != new_value:
# Conflict - keep both with provenance
merged_content[key] = {
"conflicted": True,
"values": [
{"value": current_value, "agent": current_fact.author_agent},
{"value": new_value, "agent": agent_id}
]
}
return VersionedFact(
fact_id=current_fact.fact_id,
content=merged_content,
version=current_fact.version + 1,
timestamp=time.time(),
author_agent=f"{current_fact.author_agent}+{agent_id}", # Merged authorship
vector_clock=vector_clock
)
async def read_facts(self, query: Dict[str, Any] = None) -> List[VersionedFact]:
"""Read facts (non-blocking for readers)"""
# Snapshot current facts (no lock needed for reads)
facts_snapshot = list(self.facts.values())
if not query:
return facts_snapshot
# Run complex queries in thread pool to avoid blocking
loop = asyncio.get_event_loop()
filtered_facts = await loop.run_in_executor(
self.query_executor,
self._filter_facts,
facts_snapshot,
query
)
return filtered_facts
def _filter_facts(self, facts: List[VersionedFact], query: Dict[str, Any]) -> List[VersionedFact]:
"""Filter facts based on query (runs in thread pool)"""
results = []
for fact in facts:
if self._fact_matches_query(fact, query):
results.append(fact)
return results
def _fact_matches_query(self, fact: VersionedFact, query: Dict[str, Any]) -> bool:
"""Check if fact matches query criteria"""
for key, value in query.items():
if key == "domain" and fact.content.get("domain") != value:
return False
elif key == "confidence_min" and fact.content.get("confidence", 0) < value:
return False
elif key == "agent" and fact.author_agent != value:
return False
# ... other query criteria
return True
async def _notify_fact_change(self, operation: str, fact_id: str, fact: VersionedFact):
"""Notify all subscribers of fact changes"""
# This could trigger re-analysis, contradiction detection, etc.
change_notification = {
"operation": operation,
"fact_id": fact_id,
"fact": fact,
"timestamp": time.time()
}
# Could use the event bus to notify agents
# await self.event_bus.publish(AgentMessage(
# type=MessageType.KNOWLEDGE_CHANGE,
# sender_id="knowledge_base",
# payload=change_notification
# ))
4. Practical Implementation Example
Market Analyst Agent with Full Communication
class MarketAnalystAgent(CollaborativeAgent):
def __init__(self, agent_id: str, event_bus: AgentEventBus, knowledge_base: SharedKnowledgeBase):
super().__init__(agent_id, event_bus)
self.knowledge_base = knowledge_base
self.expertise_domains = ["market_analysis", "competitive_landscape", "business_models"]
self.active_research_tasks = []
def get_subscribed_message_types(self) -> List[MessageType]:
return [
MessageType.EXPERTISE_REQUEST,
MessageType.FACT_DISCOVERED,
MessageType.CONTRADICTION_ALERT
]
async def perform_primary_work(self):
"""Main research work - runs continuously"""
try:
# Check for new market intelligence opportunities
recent_facts = await self.knowledge_base.read_facts({
"domain": "technology",
"confidence_min": 0.7
})
# Analyze for market implications
for fact in recent_facts[-10:]: # Process latest 10 facts
market_insights = await self.analyze_market_implications(fact)
if market_insights:
# Share insights with other agents
await self.send_message(
MessageType.INSIGHT_GENERATED,
{
"insights": market_insights,
"source_fact": fact.fact_id,
"domain": "market_analysis"
}
)
# Perform autonomous research
if len(self.active_research_tasks) < 3: # Limit concurrent tasks
new_task = await self.identify_research_opportunity()
if new_task:
self.active_research_tasks.append(new_task)
asyncio.create_task(self.execute_research_task(new_task))
except Exception as e:
print(f"Error in market analyst primary work: {e}")
async def handle_expertise_request(self, message: AgentMessage):
"""Handle requests for market analysis expertise"""
domain = message.payload.get("domain")
question = message.payload.get("question")
if domain in self.expertise_domains:
# Generate expert response
response = await self.provide_market_expertise(question)
# Send response back to requester
await self.send_message(
MessageType.EXPERTISE_REQUEST, # Same type for response
{
"response": response,
"confidence": 0.8,
"domain": domain
},
target_agent=message.sender_id,
correlation_id=message.correlation_id
)
async def handle_fact_discovered(self, message: AgentMessage):
"""Process new facts discovered by other agents"""
fact = message.payload.get("fact")
if self.is_relevant_to_market_analysis(fact):
# Analyze implications asynchronously (don't block message processing)
asyncio.create_task(self.analyze_fact_implications(fact))
async def analyze_fact_implications(self, fact: Dict):
"""Analyze market implications of a new fact"""
try:
# Perform analysis
implications = await self.generate_market_implications(fact)
if implications:
# Write derived insights to shared knowledge
insight_id = f"market_insight_{uuid.uuid4().hex[:8]}"
await self.knowledge_base.write_fact(
self.agent_id,
insight_id,
{
"type": "market_insight",
"source_fact": fact.get("fact_id"),
"implications": implications,
"confidence": 0.75,
"domain": "market_analysis"
}
)
# Check if this contradicts existing market assumptions
contradictions = await self.check_for_market_contradictions(implications)
if contradictions:
await self.send_message(
MessageType.CONTRADICTION_ALERT,
{
"contradictions": contradictions,
"new_insight": insight_id
}
)
except Exception as e:
print(f"Error analyzing fact implications: {e}")
async def execute_research_task(self, task: Dict):
"""Execute research task concurrently with other work"""
try:
# Perform research
research_results = await self.conduct_market_research(task)
# Write findings to shared knowledge
for result in research_results:
fact_id = f"market_fact_{uuid.uuid4().hex[:8]}"
await self.knowledge_base.write_fact(
self.agent_id,
fact_id,
result
)
# Notify completion
await self.send_message(
MessageType.TASK_COMPLETE,
{
"task_id": task["id"],
"results_count": len(research_results),
"domain": "market_analysis"
}
)
except Exception as e:
print(f"Error executing research task: {e}")
finally:
# Remove from active tasks
self.active_research_tasks = [
t for t in self.active_research_tasks
if t["id"] != task["id"]
]
5. System Initialization and Orchestration
Complete System Setup
async def initialize_collaborative_research_system():
"""Initialize the complete collaborative system"""
# Create core infrastructure
event_bus = AgentEventBus()
knowledge_base = SharedKnowledgeBase()
# Create agents
agents = {
"coordinator": ResearchCoordinatorAgent("coordinator", event_bus, knowledge_base),
"market_analyst": MarketAnalystAgent("market_analyst", event_bus, knowledge_base),
"tech_scout": TechnologyScoutAgent("tech_scout", event_bus, knowledge_base),
"info_broker": InformationBrokerAgent("info_broker", event_bus, knowledge_base),
"synthesis": SynthesisAgent("synthesis", event_bus, knowledge_base),
"qa_agent": QualityAssuranceAgent("qa_agent", event_bus, knowledge_base)
}
# Register all agents with knowledge base
for agent_id, agent in agents.items():
await knowledge_base.register_agent(agent_id)
# Start all agents concurrently
agent_tasks = [agent.start() for agent in agents.values()]