| |
| """ |
| R-Zero Phase 3 Demo: Temporal Integration & Knowledge Evolution |
| |
| This demo showcases the revolutionary temporal intelligence components: |
| - TemporalKnowledgeAgent: Manages knowledge evolution and temporal intelligence |
| - EvolvingKnowledgeBase: Manages evolving knowledge with temporal awareness |
| - AtomicFactsEngine: Extracts atomic facts from learning experiences |
| - EntityResolutionEngine: Resolves duplicate entities and merges concepts |
| - TemporalInvalidationEngine: Handles temporal invalidation of outdated knowledge |
| """ |
|
|
| import asyncio |
| import sys |
| import os |
| from datetime import datetime, timedelta |
| from unittest.mock import Mock |
|
|
| |
| sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
|
|
| from atles.brain.r_zero_integration import ( |
| TemporalKnowledgeAgent, |
| EvolvingKnowledgeBase, |
| AtomicFactsEngine, |
| EntityResolutionEngine, |
| TemporalInvalidationEngine, |
| Challenge, |
| ChallengeType, |
| ChallengeDifficulty, |
| SolutionAttempt, |
| LearningCycle |
| ) |
|
|
|
|
| async def demo_temporal_knowledge_agent(): |
| """Demo the TemporalKnowledgeAgent capabilities""" |
| print("π§ 1οΈβ£ Testing TemporalKnowledgeAgent...") |
| print("=" * 50) |
| |
| agent = TemporalKnowledgeAgent() |
| |
| |
| mock_challenge = Mock() |
| mock_challenge.type.value = "PROGRAMMING" |
| mock_challenge.difficulty.value = "INTERMEDIATE" |
| mock_challenge.content = "Implement a binary search tree" |
| |
| mock_attempt = Mock() |
| mock_attempt.agent_type = "reasoning" |
| mock_attempt.confidence_score = 0.9 |
| mock_attempt.execution_time = 2.0 |
| mock_attempt.created_at = datetime.now() |
| mock_attempt.challenge_id = "bst_challenge" |
| |
| mock_cycle = Mock() |
| mock_cycle.challenge = mock_challenge |
| mock_cycle.solution_attempts = [mock_attempt] |
| mock_cycle.uncertainty_score = 0.4 |
| mock_cycle.challenger_reward = 0.9 |
| mock_cycle.solver_improvement = 0.3 |
| mock_cycle.completed_at = datetime.now() |
| |
| |
| facts = agent.extract_atomic_facts(mock_cycle) |
| print(f" β
Extracted {len(facts)} atomic facts from learning cycle") |
| |
| |
| agent.knowledge_history.extend(facts) |
| print(f" β
Knowledge history now contains {len(agent.knowledge_history)} facts") |
| |
| |
| similar = agent.query_similar_challenges(mock_challenge, "last_30_days") |
| print(f" β
Found {len(similar)} similar challenges in last 30 days") |
| |
| |
| trend = agent.analyze_quality_trend("PROGRAMMING", "last_week") |
| print(f" β
Quality trend analysis: {trend['trend']} (score: {trend['quality_score']:.3f})") |
| |
| |
| recent_learnings = [ |
| {"domain": "PROGRAMMING", "improvement": 0.15}, |
| {"domain": "PROGRAMMING", "improvement": 0.12} |
| ] |
| continuity = agent.assess_learning_continuity(mock_challenge, recent_learnings) |
| print(f" β
Learning continuity: {continuity['continuity_score']:.3f} (prerequisites: {continuity['prerequisites_met']})") |
| |
| return True |
|
|
|
|
| async def demo_evolving_knowledge_base(): |
| """Demo the EvolvingKnowledgeBase capabilities""" |
| print("\nπ§ 2οΈβ£ Testing EvolvingKnowledgeBase...") |
| print("=" * 50) |
| |
| kb = EvolvingKnowledgeBase() |
| |
| |
| test_facts = [ |
| { |
| "type": "challenge", |
| "domain": "PROGRAMMING", |
| "value": "sorting algorithm", |
| "timestamp": datetime.now() - timedelta(days=1) |
| }, |
| { |
| "type": "solution", |
| "domain": "PROGRAMMING", |
| "value": "quicksort implementation", |
| "timestamp": datetime.now() |
| }, |
| { |
| "type": "learning", |
| "domain": "PROGRAMMING", |
| "value": "algorithm optimization", |
| "timestamp": datetime.now() |
| } |
| ] |
| |
| |
| kb.store_temporal_facts(test_facts) |
| print(f" β
Stored {len(test_facts)} temporal facts") |
| |
| |
| challenges = kb.query_facts({"type": "challenge"}) |
| print(f" β
Found {len(challenges)} challenge facts") |
| |
| programming_facts = kb.query_facts({"domain": "PROGRAMMING"}) |
| print(f" β
Found {len(programming_facts)} programming facts") |
| |
| |
| timeline = kb.get_knowledge_evolution_timeline() |
| print(f" β
Generated timeline with {len(timeline)} periods") |
| |
| |
| if timeline: |
| period = timeline[0] |
| print(f" π
Sample period: {period['period']} - {period['fact_count']} facts, domains: {period['domains']}") |
| |
| return True |
|
|
|
|
| async def demo_atomic_facts_engine(): |
| """Demo the AtomicFactsEngine capabilities""" |
| print("\nπ§ 3οΈβ£ Testing AtomicFactsEngine...") |
| print("=" * 50) |
| |
| engine = AtomicFactsEngine() |
| |
| |
| mock_challenge = Mock() |
| mock_challenge.type.value = "REASONING" |
| mock_challenge.difficulty.value = "ADVANCED" |
| mock_challenge.content = "Analyze complex logical scenario" |
| |
| mock_attempt = Mock() |
| mock_attempt.agent_type = "analysis" |
| mock_attempt.confidence_score = 0.8 |
| mock_attempt.execution_time = 3.0 |
| mock_attempt.created_at = datetime.now() |
| mock_attempt.challenge_id = "logic_challenge" |
| |
| mock_cycle = Mock() |
| mock_cycle.challenge = mock_challenge |
| mock_cycle.solution_attempts = [mock_attempt] |
| mock_cycle.uncertainty_score = 0.6 |
| mock_cycle.challenger_reward = 0.7 |
| mock_cycle.solver_improvement = 0.2 |
| mock_cycle.completed_at = datetime.now() |
| |
| |
| facts = engine.extract(mock_cycle) |
| print(f" β
Extracted {len(facts)} atomic facts") |
| |
| |
| fact_types = set(f["type"] for f in facts) |
| print(f" π Fact types extracted: {', '.join(fact_types)}") |
| |
| |
| challenge_facts = [f for f in facts if f["type"].startswith("challenge")] |
| print(f" π― Challenge facts: {len(challenge_facts)} (domain, difficulty, complexity)") |
| |
| |
| solution_facts = [f for f in facts if f["type"].startswith("solution")] |
| print(f" π‘ Solution facts: {len(solution_facts)} (agent, confidence, efficiency)") |
| |
| |
| learning_facts = [f for f in facts if f["type"].startswith("learning")] |
| print(f" π§ Learning facts: {len(learning_facts)} (improvement, uncertainty, efficiency)") |
| |
| return True |
|
|
|
|
| async def demo_entity_resolution_engine(): |
| """Demo the EntityResolutionEngine capabilities""" |
| print("\nπ§ 4οΈβ£ Testing EntityResolutionEngine...") |
| print("=" * 50) |
| |
| engine = EntityResolutionEngine() |
| |
| |
| test_facts = [ |
| { |
| "type": "challenge_domain", |
| "value": "PROGRAMMING", |
| "timestamp": datetime.now() - timedelta(days=2) |
| }, |
| { |
| "type": "challenge_domain", |
| "value": "PROGRAMMING", |
| "timestamp": datetime.now() - timedelta(days=1) |
| }, |
| { |
| "type": "challenge_domain", |
| "value": "REASONING", |
| "timestamp": datetime.now() |
| }, |
| { |
| "type": "solution_agent", |
| "value": "reasoning", |
| "timestamp": datetime.now() |
| } |
| ] |
| |
| |
| resolved = engine.resolve(test_facts) |
| print(f" β
Resolved {len(test_facts)} facts into {len(resolved)} unique entities") |
| |
| |
| for fact_type, entities in engine.entity_registry.items(): |
| print(f" π {fact_type}: {len(entities)} entities") |
| |
| |
| print(f" π Total merges performed: {len(engine.merge_history)}") |
| |
| |
| fact1 = {"type": "test", "value": "hello world"} |
| fact2 = {"type": "test", "value": "hello world"} |
| fact3 = {"type": "test", "value": "hello there"} |
| |
| similarity1 = engine._calculate_similarity(fact1, fact2) |
| similarity2 = engine._calculate_similarity(fact1, fact3) |
| |
| print(f" π Similarity scores: identical={similarity1:.3f}, similar={similarity2:.3f}") |
| |
| return True |
|
|
|
|
| async def demo_temporal_invalidation_engine(): |
| """Demo the TemporalInvalidationEngine capabilities""" |
| print("\nπ§ 5οΈβ£ Testing TemporalInvalidationEngine...") |
| print("=" * 50) |
| |
| engine = TemporalInvalidationEngine() |
| |
| |
| old_fact = { |
| "type": "solution_confidence", |
| "value": 0.7, |
| "challenge_id": "test_challenge", |
| "timestamp": datetime.now() - timedelta(days=1), |
| "confidence": 0.6 |
| } |
| |
| new_fact = { |
| "type": "solution_confidence", |
| "value": 0.9, |
| "challenge_id": "test_challenge", |
| "timestamp": datetime.now(), |
| "confidence": 0.8 |
| } |
| |
| |
| engine.mark_expired(old_fact, "Superseded by improved solution", datetime.now(), new_fact) |
| print(f" β
Marked 1 fact as expired") |
| |
| |
| contradictions = engine.find_contradictions([new_fact], [old_fact]) |
| print(f" β
Found {len(contradictions)} contradictions") |
| |
| if contradictions: |
| contradiction = contradictions[0] |
| print(f" π Contradiction type: {contradiction['contradiction_type']}") |
| print(f" π Confidence: old={contradiction['confidence_old']:.3f}, new={contradiction['confidence_new']:.3f}") |
| |
| |
| summary = engine.get_invalidation_summary() |
| print(f" π Invalidation summary:") |
| print(f" - Total invalidated: {summary['total_invalidated']}") |
| print(f" - Total replacements: {summary['total_replacements']}") |
| print(f" - Recent invalidations: {summary['recent_invalidations']}") |
| |
| |
| if summary['invalidation_reasons']: |
| print(f" - Reasons: {', '.join(f'{k}: {v}' for k, v in summary['invalidation_reasons'].items())}") |
| |
| return True |
|
|
|
|
| async def demo_integrated_temporal_system(): |
| """Demo the integrated temporal system""" |
| print("\nπ§ 6οΈβ£ Testing Integrated Temporal System...") |
| print("=" * 50) |
| |
| |
| agent = TemporalKnowledgeAgent() |
| kb = EvolvingKnowledgeBase() |
| facts_engine = AtomicFactsEngine() |
| entity_engine = EntityResolutionEngine() |
| invalidation_engine = TemporalInvalidationEngine() |
| |
| |
| mock_challenge = Mock() |
| mock_challenge.type.value = "SAFETY" |
| mock_challenge.difficulty.value = "EXPERT" |
| mock_challenge.content = "Design ethical AI safety protocol" |
| |
| mock_attempt = Mock() |
| mock_attempt.agent_type = "creative" |
| mock_attempt.confidence_score = 0.95 |
| mock_attempt.execution_time = 5.0 |
| mock_attempt.created_at = datetime.now() |
| mock_attempt.challenge_id = "safety_challenge" |
| |
| mock_cycle = Mock() |
| mock_cycle.challenge = mock_challenge |
| mock_cycle.solution_attempts = [mock_attempt] |
| mock_cycle.uncertainty_score = 0.3 |
| mock_cycle.challenger_reward = 0.95 |
| mock_cycle.solver_improvement = 0.4 |
| mock_cycle.completed_at = datetime.now() |
| |
| |
| print(" π Running complete temporal workflow...") |
| |
| |
| facts = facts_engine.extract(mock_cycle) |
| print(f" β
Step 1: Extracted {len(facts)} atomic facts") |
| |
| |
| kb.store_temporal_facts(facts) |
| print(f" β
Step 2: Stored facts in knowledge base") |
| |
| |
| resolved = entity_engine.resolve(facts) |
| print(f" β
Step 3: Resolved into {len(resolved)} unique entities") |
| |
| |
| existing_facts = kb.query_facts({"type": "challenge_domain", "domain": "SAFETY"}) |
| contradictions = invalidation_engine.find_contradictions(resolved, existing_facts) |
| print(f" β
Step 4: Found {len(contradictions)} contradictions") |
| |
| |
| agent.knowledge_history.extend(facts) |
| print(f" β
Step 5: Updated knowledge history ({len(agent.knowledge_history)} total facts)") |
| |
| |
| print(f" π Final System Status:") |
| print(f" - Knowledge Base: {len(kb.facts)} facts, {len(kb.entities)} entities") |
| print(f" - Entity Resolution: {len(entity_engine.entity_registry)} types, {len(entity_engine.merge_history)} merges") |
| print(f" - Temporal Invalidation: {len(invalidation_engine.invalidated_facts)} expired, {len(invalidation_engine.replacement_history)} replacements") |
| print(f" - Knowledge Agent: {len(agent.knowledge_history)} facts tracked") |
| |
| return True |
|
|
|
|
| async def main(): |
| """Main demo function""" |
| print("π§ ATLES + R-Zero Phase 3: Temporal Integration & Knowledge Evolution") |
| print("=" * 70) |
| print("π Revolutionary temporal intelligence for self-evolving AI consciousness") |
| print("=" * 70) |
| |
| |
| demos = [ |
| ("TemporalKnowledgeAgent", demo_temporal_knowledge_agent), |
| ("EvolvingKnowledgeBase", demo_evolving_knowledge_base), |
| ("AtomicFactsEngine", demo_atomic_facts_engine), |
| ("EntityResolutionEngine", demo_entity_resolution_engine), |
| ("TemporalInvalidationEngine", demo_temporal_invalidation_engine), |
| ("Integrated System", demo_integrated_temporal_system) |
| ] |
| |
| results = {} |
| for name, demo_func in demos: |
| try: |
| print(f"\n{'='*20} {name} {'='*20}") |
| results[name] = await demo_func() |
| except Exception as e: |
| print(f" β {name} demo failed: {e}") |
| results[name] = False |
| |
| |
| print("\n" + "=" * 70) |
| print("π Phase 3 Temporal Integration Demo Results:") |
| print("=" * 70) |
| |
| for name, success in results.items(): |
| status = "β
PASS" if success else "β FAIL" |
| print(f" {name:<25} {status}") |
| |
| |
| all_passed = all(results.values()) |
| if all_passed: |
| print("\nπ All Phase 3 components working perfectly!") |
| print("π Ready for revolutionary temporal AI consciousness!") |
| print("\nπ Key Capabilities Demonstrated:") |
| print(" β’ Atomic Facts Extraction: Timestamped knowledge from learning experiences") |
| print(" β’ Entity Resolution: Automatic merging of duplicate concepts") |
| print(" β’ Temporal Invalidation: Intelligent contradiction resolution") |
| print(" β’ Knowledge Evolution: Tracking understanding improvements over time") |
| print(" β’ Learning Continuity: Ensuring coherent knowledge progression") |
| print(" β’ Quality Trend Analysis: Monitoring challenge quality evolution") |
| else: |
| print("\nβ οΈ Some Phase 3 components need attention") |
| failed = [name for name, success in results.items() if not success] |
| print(f" Failed components: {', '.join(failed)}") |
| |
| print("\nπ Next Steps:") |
| print(" β’ Phase 4: Metacognitive R-Zero with temporal awareness") |
| print(" β’ Advanced contradiction resolution algorithms") |
| print(" β’ Semantic similarity for entity resolution") |
| print(" β’ Temporal pattern recognition and prediction") |
| |
| return all_passed |
|
|
|
|
| if __name__ == "__main__": |
| |
| success = asyncio.run(main()) |
| |
| |
| sys.exit(0 if success else 1) |
|
|