#!/usr/bin/env python3
"""
Test: LLM Research Report - Unexplored Frontiers
This task uses web search to gather latest research on LLM limitations and unexplored areas,
then saves the knowledge to external memory for future reference.
"""
import sys
sys.path.insert(0, '.')

from dotenv import load_dotenv
load_dotenv()

from gcri.config import scope as gcri_scope
from gcri.graphs.gcri_unit import GCRI


@gcri_scope.observe('research_report', priority=0, default=True, chain_with='external_memory')
def research_report(config):
    """Configure for research report task with web search."""
    config.protocols.max_iterations = 3
    config.branches_generator_type = 'adaptive'
    config.num_branches = 2
    return config


RESEARCH_TASK = """
# LLM Research Report: Unexplored Frontiers and Open Problems

## Task
Write a comprehensive research report on the unexplored frontiers and open problems in Large Language Model (LLM) research. 
Use web search to gather the latest information (2024-2025).

## Requirements

### 1. Research Areas to Cover
- **Reasoning & Logic**: Current limitations in mathematical reasoning, multi-step logic, planning
- **Memory & Context**: Long-term memory, context window limitations, retrieval augmentation gaps
- **Hallucination & Factuality**: Root causes, detection methods, mitigation strategies
- **Multimodal Understanding**: Vision-language gaps, audio-text integration challenges
- **Efficiency & Scaling**: Training efficiency, inference optimization, model compression
- **Safety & Alignment**: Jailbreaking, alignment tax, value learning challenges
- **Emergent Capabilities**: Unpredictable behaviors, capability thresholds, interpretability

### 2. Output Format
Create a markdown file `llm_research_report.md` with:
- Executive summary
- For each area: Current state, Key limitations, Recent breakthroughs, Open problems
- References with dates (important for knowledge freshness)

### 3. Knowledge Extraction
After completing the report, use the `save_knowledge` tool to store key findings:
- Domain: "llm_research"
- Include source_date for each piece of knowledge
- Tag appropriately (e.g., "reasoning", "hallucination", "alignment")

## Important
- Use `web_search` tool to find recent papers and articles
- Focus on 2024-2025 research for freshness
- Include specific paper names, authors when possible
- Save at least 5-7 distinct knowledge entries to external memory
"""


@gcri_scope
def main(config):
    import os
    import json
    
    print('=' * 70)
    print('🔬 LLM Research Report: Unexplored Frontiers')
    print('   Gathering knowledge via web search and saving to external memory')
    print('=' * 70)
    
    # Check external memory before
    ext_memory_path = '/Users/dirac_on/Library/Mobile Documents/com~apple~CloudDocs/Projects/GCRI/.gcri/external_memory.json'
    if os.path.exists(ext_memory_path):
        with open(ext_memory_path, 'r') as f:
            data = json.load(f)
        knowledge_count = sum(len(v) for v in data.get('knowledge', {}).values())
        print(f'\n🧠 External Memory Before: {knowledge_count} knowledge items')
    
    print('\n' + '=' * 70)
    print('📋 Running Research Task...')
    print('=' * 70 + '\n')
    
    gcri = GCRI(config)
    result = gcri(RESEARCH_TASK)
    
    # Check external memory after
    print('\n' + '=' * 70)
    if os.path.exists(ext_memory_path):
        with open(ext_memory_path, 'r') as f:
            data = json.load(f)
        knowledge_count_after = sum(len(v) for v in data.get('knowledge', {}).values())
        print(f'🧠 External Memory After: {knowledge_count_after} knowledge items')
        if 'llm_research' in data.get('knowledge', {}):
            print(f'   - llm_research: {len(data["knowledge"]["llm_research"])} new entries')
    
    if result.get('decision'):
        print('📊 Result: ✅ SUCCESS')
    else:
        print('📊 Result: ❌ FAILED')
    print('=' * 70)
    
    return result


if __name__ == '__main__':
    result = main()
    sys.exit(0 if result.get('decision') else 1)
