geographdb-core 0.5.4

Geometric graph database core - 3D spatial indexing for code analysis
Documentation
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
#!/usr/bin/env python3
"""GeoMetriDB Autonomous Research Agent — Honest Implementation

Discovers mathematical formulas from arXiv, validates them against
actual GeoMetriDB Rust code via Python FFI, and reports honest metrics.

No synthetic benchmarks. No fake test counts. No deterministic seeds.
Real arXiv queries, real code execution, real measurements.

Usage:
    PYTHONPATH=/path/to/geographdb-py/python python3 research_agent/run.py --query-set 1
    PYTHONPATH=/path/to/geographdb-py/python python3 research_agent/run.py --mode cron
"""

import argparse
import json
import os
import subprocess
import sys
import time
import urllib.request
import urllib.parse
from pathlib import Path
from typing import List, Dict, Optional, Any, Set

# GeoMetriDB Python FFI
sys.path.insert(0, '/home/feanor/Projects/geographdb-core/geographdb-py/python')
import geographdb as gdb

# ── Persistent State ─────────────────────────────────────────────────────────

OUTPUT_DIR = Path("/home/feanor/Projects/geographdb-core/research_agent/output")
SEEN_PAPERS_FILE = OUTPUT_DIR / "seen_papers.json"
ALL_FINDINGS_FILE = OUTPUT_DIR / "all_findings.jsonl"

def load_seen_papers() -> Set[str]:
    """Load the set of previously seen paper titles (persistent across runs)."""
    if SEEN_PAPERS_FILE.exists():
        try:
            data = json.loads(SEEN_PAPERS_FILE.read_text())
            return set(data.get("titles", []))
        except (json.JSONDecodeError, KeyError):
            return set()
    return set()

def save_seen_papers(titles: Set[str]):
    """Save the set of seen paper titles."""
    SEEN_PAPERS_FILE.parent.mkdir(parents=True, exist_ok=True)
    SEEN_PAPERS_FILE.write_text(json.dumps({"titles": sorted(titles)}, indent=2))

def append_finding(record: Dict[str, Any]):
    """Append a finding to the persistent JSONL file."""
    ALL_FINDINGS_FILE.parent.mkdir(parents=True, exist_ok=True)
    with open(ALL_FINDINGS_FILE, "a") as f:
        f.write(json.dumps(record, default=str) + "\n")

def load_all_findings() -> List[Dict[str, Any]]:
    """Load all historical findings from JSONL."""
    if not ALL_FINDINGS_FILE.exists():
        return []
    findings = []
    with open(ALL_FINDINGS_FILE) as f:
        for line in f:
            line = line.strip()
            if line:
                try:
                    findings.append(json.loads(line))
                except json.JSONDecodeError:
                    pass
    return findings

# ── Configuration ────────────────────────────────────────────────────────────

QUERY_SETS = {
    1: {
        "name": "Tensor Networks & Compression",
        "queries": [
            "cat:cs.LG AND (\"matrix product state\" OR \"tensor train\" OR \"MPS\" OR \"MPO\") AND (\"attention\" OR \"transformer\" OR \"KV cache\")",
            "cat:cs.LG AND (\"low-rank\" OR \"SVD truncation\" OR \"compression\") AND (\"language model\" OR \"inference\")",
        ],
    },
    2: {
        "name": "Differential Geometry & Manifolds",
        "queries": [
            "cat:math.DG AND (\"Ricci curvature\" OR \"manifold learning\")",
            "cat:cs.LG AND (\"Cartan moving frame\" OR \"differential geometry\" OR \"information geometry\")",
        ],
    },
    3: {
        "name": "Topological Data Analysis",
        "queries": [
            "cat:math.AT AND (\"persistent homology\" OR \"topological data analysis\")",
            "cat:cs.LG AND (\"Betti numbers\" OR \"barcode\" OR \"topological\") AND (\"neural\" OR \"deep learning\")",
        ],
    },
    4: {
        "name": "Information Geometry & Optimization",
        "queries": [
            "cat:cs.LG AND (\"natural gradient\" OR \"Fisher information metric\" OR \"information geometry\")",
            "cat:stat.ML AND (\"KL divergence\" OR \"Wasserstein distance\") AND (\"optimization\" OR \"gradient\")",
        ],
    },
    5: {
        "name": "Spatiotemporal & Dynamic Graphs",
        "queries": [
            "cat:cs.DS AND (\"temporal graph\" OR \"dynamic graph\" OR \"spatiotemporal\") AND (\"algorithm\" OR \"pathfinding\")",
            "cat:cs.LG AND (\"time-aware\" OR \"4D graph\" OR \"temporal path\") AND (\"neural network\" OR \"GNN\")",
        ],
    },
    6: {
        "name": "Combinatorial & Discrete Methods",
        "queries": [
            "cat:cs.DS AND (\"percolation\" OR \"phase transition\" OR \"community detection\")",
            "cat:cs.LG AND (\"sparse attention\" OR \"block sparse\" OR \"combinatorial optimization\") AND (\"transformer\" OR \"LLM\")",
        ],
    },
}

# Map paper keywords to GeoMetriDB modules for actual benchmarking
MODULE_MAP = {
    "kv_cache_mps": {
        "keywords": ["kv cache", "key value", "attention cache", "low-rank", "svd", "compression"],
        "bench": "bench_kv_cache_mps",
    },
    "kv_frame_codec": {
        "keywords": ["frame", "codec", "differential", "cartan", "moving frame", "transition"],
        "bench": "bench_kv_frame_codec",
    },
    "mpo": {
        "keywords": ["matrix product operator", "mpo", "tensor operator", "tensor network"],
        "bench": "bench_mpo",
    },
    "mps": {
        "keywords": ["matrix product state", "mps", "tensor train"],
        "bench": "bench_mps",
    },
    "sparse_attn": {
        "keywords": ["sparse attention", "sparsity", "pruning attention", "block sparse"],
        "bench": "bench_sparse_attn",
    },
    "ricci": {
        "keywords": ["ricci", "curvature", "manifold", "geometry"],
        "bench": "bench_ricci",
    },
    "percolation": {
        "keywords": ["percolation", "phase transition", "threshold", "connectivity"],
        "bench": "bench_percolation",
    },
    "persistence": {
        "keywords": ["persistent homology", "topological", "barcode", "betti"],
        "bench": "bench_persistence",
    },
    "astar": {
        "keywords": ["a*", "pathfinding", "shortest path", "route"],
        "bench": "bench_astar",
    },
    "four_d": {
        "keywords": ["4d", "spatiotemporal", "temporal graph", "time-aware"],
        "bench": "bench_four_d",
    },
    "natural_grad": {
        "keywords": ["natural gradient", "fisher information", "information geometry"],
        "bench": "bench_natural_grad",
    },
    "infogeo": {
        "keywords": ["information geometry", "kl divergence", "metric tensor"],
        "bench": "bench_infogeo",
    },
}

# ── Phase 1: DISCOVER ────────────────────────────────────────────────────────

def search_arxiv(query: str, max_results: int = 10) -> List[Dict[str, Any]]:
    """Search arXiv API. Returns empty list on any failure."""
    encoded = urllib.parse.quote(query)
    url = f"http://export.arxiv.org/api/query?search_query={encoded}&max_results={max_results}&sortBy=submittedDate&sortOrder=descending"

    try:
        req = urllib.request.Request(url, headers={"User-Agent": "geographdb-research/1.0"})
        with urllib.request.urlopen(req, timeout=20) as resp:
            data = resp.read().decode("utf-8")
    except Exception as e:
        print(f"   arXiv search failed: {e}")
        return []

    import xml.etree.ElementTree as ET
    try:
        root = ET.fromstring(data)
    except ET.ParseError:
        return []

    ns = {"atom": "http://www.w3.org/2005/Atom"}
    entries = []
    for entry in root.findall("atom:entry", ns):
        title = entry.find("atom:title", ns)
        summary = entry.find("atom:summary", ns)
        published = entry.find("atom:published", ns)
        id_elem = entry.find("atom:id", ns)

        if title is not None and summary is not None:
            year = 2024
            if published is not None and published.text:
                try:
                    year = int(published.text[:4])
                except ValueError:
                    pass
            entries.append({
                "title": (title.text or "").strip().replace("\n", " "),
                "abstract": (summary.text or "").strip().replace("\n", " ")[:800],
                "year": year,
                "url": id_elem.text if id_elem is not None else "",
                "source": "arxiv",
            })

    return entries


def discover_formulas(query_set_id: int) -> List[Dict[str, Any]]:
    """Phase 1: Search arXiv for papers matching query set."""
    print("\n" + "=" * 70)
    print("PHASE 1: DISCOVER")
    print("=" * 70)

    query_set = QUERY_SETS.get(query_set_id, QUERY_SETS[1])
    print(f"Query Set {query_set_id}: {query_set['name']}")

    all_entries = []
    for query in query_set["queries"]:
        print(f"\n🔍 Query: {query[:80]}...")
        entries = search_arxiv(query, max_results=10)
        print(f"   Found {len(entries)} papers")
        all_entries.extend(entries)
        time.sleep(3)  # Rate limit: be nice to arXiv

    # Deduplicate by title (within this run)
    seen = set()
    unique = []
    for e in all_entries:
        key = e["title"].lower()[:100]
        if key not in seen:
            seen.add(key)
            unique.append(e)

    # Deduplicate against historically seen papers (across all runs)
    historically_seen = load_seen_papers()
    new_papers = []
    for e in unique:
        key = e["title"].lower()[:100]
        if key in historically_seen:
            print(f"   ⏭ SKIP (already seen): {e['title'][:60]}...")
        else:
            new_papers.append(e)
            historically_seen.add(key)

    # Save updated seen set
    save_seen_papers(historically_seen)

    print(f"\n📊 Total unique papers this query: {len(unique)}")
    print(f"📊 New papers (never seen): {len(new_papers)}")
    print(f"📊 Skipped (already seen): {len(unique) - len(new_papers)}")
    print(f"📊 Total historically tracked: {len(historically_seen)}")
    return new_papers


# ── Phase 2: MAP ─────────────────────────────────────────────────────────────

def map_paper_to_module(paper: Dict[str, Any]) -> Optional[str]:
    """Map paper to GeoMetriDB module based on keyword matching."""
    text = (paper["title"] + " " + paper["abstract"]).lower()

    best_module = None
    best_score = 0

    for module, info in MODULE_MAP.items():
        score = sum(1 for kw in info["keywords"] if kw in text)
        if score > best_score:
            best_score = score
            best_module = module

    return best_module if best_score >= 1 else None


def map_papers(papers: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
    """Phase 2: Map papers to GeoMetriDB modules."""
    print("\n" + "=" * 70)
    print("PHASE 2: MAP")
    print("=" * 70)

    mapped = []
    for p in papers:
        module = map_paper_to_module(p)
        if module:
            p["mapped_module"] = module
            mapped.append(p)
            print(f"{p['title'][:60]}... -> {module}")
        else:
            p["mapped_module"] = None
            print(f"{p['title'][:60]}... -> no match")

    print(f"\n📊 Mapped: {len(mapped)}/{len(papers)}")
    return papers  # Return all, including unmapped


# ── Phase 3: VALIDATE ────────────────────────────────────────────────────────

def run_cargo_tests(module: str) -> Dict[str, Any]:
    """Run actual GeoMetriDB cargo tests for a module. Returns real counts."""
    result = {
        "tests_passed": 0,
        "tests_total": 0,
        "tests_failed": 0,
        "success": False,
        "output": "",
    }

    try:
        proc = subprocess.run(
            ["cargo", "test", "--lib", module],
            cwd="/home/feanor/Projects/geographdb-core",
            capture_output=True,
            text=True,
            timeout=120,
        )
        output = proc.stdout + proc.stderr
        result["output"] = output[-2000:]  # Last 2000 chars

        # Parse real test results
        import re
        # Look for "test result: ok. X passed; Y failed"
        match = re.search(r"test result: ok\.\s+(\d+) passed;\s+(\d+) failed", output)
        if match:
            result["tests_passed"] = int(match.group(1))
            result["tests_failed"] = int(match.group(2))
            result["tests_total"] = result["tests_passed"] + result["tests_failed"]
            result["success"] = result["tests_failed"] == 0
        else:
            # Look for "test result: FAILED. X passed; Y failed"
            match = re.search(r"test result: FAILED\.\s+(\d+) passed;\s+(\d+) failed", output)
            if match:
                result["tests_passed"] = int(match.group(1))
                result["tests_failed"] = int(match.group(2))
                result["tests_total"] = result["tests_passed"] + result["tests_failed"]
                result["success"] = False
            else:
                # Could not parse — maybe no tests matched
                result["tests_total"] = 0
                result["success"] = proc.returncode == 0
    except subprocess.TimeoutExpired:
        result["output"] = "TIMEOUT after 120s"
        result["success"] = False
    except Exception as e:
        result["output"] = f"ERROR: {e}"
        result["success"] = False

    return result


def bench_module(module: str) -> Optional[Dict[str, Any]]:
    """Run actual benchmark against GeoMetriDB via Python FFI."""
    if module == "astar":
        return bench_astar()
    elif module == "four_d":
        return bench_four_d()
    elif module == "scc":
        return bench_scc()
    elif module in ("kv_cache_mps", "kv_frame_codec", "mpo", "mps", "sparse_attn",
                     "ricci", "percolation", "persistence", "natural_grad", "infogeo"):
        # These modules don't have Python FFI bindings yet — run Rust tests only
        return None
    else:
        return None


def bench_astar() -> Dict[str, Any]:
    """Benchmark A* pathfinding on actual GeoMetriDB code."""
    import random

    results = []
    for n in [10, 20, 30, 50]:
        graph = gdb.Graph4D()
        for i in range(n):
            for j in range(n):
                node = gdb.GraphNode4D(
                    id=i * n + j, x=float(i), y=float(j), z=0.0,
                    begin_ts=0, end_ts=1000,
                )
                graph.add_node(node)
        for i in range(n):
            for j in range(n):
                idx = i * n + j
                if i < n - 1:
                    graph.add_edge(idx, (i + 1) * n + j, 1.0, 0, 1000)
                if j < n - 1:
                    graph.add_edge(idx, i * n + (j + 1), 1.0, 0, 1000)

        ctx = gdb.TraversalContext4D(time_start=0, time_end=1000)

        # Warmup
        _ = gdb.py_astar_find_path_4d(graph, 0, n * n - 1, ctx)

        # Measure
        times = []
        for _ in range(5):
            start = time.perf_counter()
            path = gdb.py_astar_find_path_4d(graph, 0, n * n - 1, ctx)
            elapsed = (time.perf_counter() - start) * 1000
            times.append(elapsed)

        avg_time = sum(times) / len(times)
        results.append({
            "n": n,
            "nodes": n * n,
            "path_len": len(path) if path else 0,
            "avg_ms": round(avg_time, 3),
            "min_ms": round(min(times), 3),
            "max_ms": round(max(times), 3),
        })

    return {
        "benchmark": "astar_grid",
        "results": results,
    }


def bench_four_d() -> Dict[str, Any]:
    """Benchmark fastest temporal path on actual GeoMetriDB code."""
    results = []
    for n in [10, 20, 30]:
        graph = gdb.Graph4D()
        for i in range(n):
            for j in range(n):
                node = gdb.GraphNode4D(
                    id=i * n + j, x=float(i), y=float(j), z=0.0,
                    begin_ts=0, end_ts=1000,
                )
                graph.add_node(node)
        for i in range(n):
            for j in range(n):
                idx = i * n + j
                if i < n - 1:
                    graph.add_edge(idx, (i + 1) * n + j, 1.0, 0, 1000)
                if j < n - 1:
                    graph.add_edge(idx, i * n + (j + 1), 1.0, 0, 1000)

        ctx = gdb.TraversalContext4D(time_start=0, time_end=1000)

        # Warmup
        _ = gdb.py_fastest_temporal_path_4d(graph, 0, n * n - 1, ctx)

        times = []
        for _ in range(5):
            start = time.perf_counter()
            path = gdb.py_fastest_temporal_path_4d(graph, 0, n * n - 1, ctx)
            elapsed = (time.perf_counter() - start) * 1000
            times.append(elapsed)

        avg_time = sum(times) / len(times)
        results.append({
            "n": n,
            "nodes": n * n,
            "path_len": len(path) if path else 0,
            "avg_ms": round(avg_time, 3),
            "min_ms": round(min(times), 3),
            "max_ms": round(max(times), 3),
        })

    return {
        "benchmark": "fastest_temporal_path_grid",
        "results": results,
    }


def bench_scc() -> Dict[str, Any]:
    """Benchmark strongly connected components on actual GeoMetriDB code."""
    results = []
    for n in [10, 20, 30, 50]:
        graph = gdb.Graph4D()
        for i in range(n):
            for j in range(n):
                node = gdb.GraphNode4D(
                    id=i * n + j, x=float(i), y=float(j), z=0.0,
                    begin_ts=0, end_ts=1000,
                )
                graph.add_node(node)
        for i in range(n):
            for j in range(n):
                idx = i * n + j
                if i < n - 1:
                    graph.add_edge(idx, (i + 1) * n + j, 1.0, 0, 1000)
                if j < n - 1:
                    graph.add_edge(idx, i * n + (j + 1), 1.0, 0, 1000)

        ctx = gdb.TraversalContext4D(time_start=0, time_end=1000)

        times = []
        for _ in range(5):
            start = time.perf_counter()
            sccs = gdb.py_strongly_connected_components_4d(graph, ctx)
            elapsed = (time.perf_counter() - start) * 1000
            times.append(elapsed)

        avg_time = sum(times) / len(times)
        results.append({
            "n": n,
            "nodes": n * n,
            "scc_count": len(sccs) if sccs else 0,
            "avg_ms": round(avg_time, 3),
            "min_ms": round(min(times), 3),
            "max_ms": round(max(times), 3),
        })

    return {
        "benchmark": "scc_grid",
        "results": results,
    }


def validate_paper(paper: Dict[str, Any]) -> Dict[str, Any]:
    """Phase 3: Validate a paper by running actual GeoMetriDB tests and benchmarks."""
    module = paper.get("mapped_module")
    if not module:
        return {
            "paper_title": paper["title"],
            "module": None,
            "tests": {"tests_passed": 0, "tests_total": 0, "success": False},
            "benchmark": None,
            "accepted": False,
            "reason": "No module mapping",
        }

    print(f"\n   Validating: {paper['title'][:50]}...")
    print(f"   Module: {module}")

    # Run cargo tests
    test_results = run_cargo_tests(module)
    print(f"   Tests: {test_results['tests_passed']}/{test_results['tests_total']} passed")

    # Run benchmark (if FFI available)
    bench_results = bench_module(module)
    if bench_results:
        print(f"   Benchmark: {bench_results['benchmark']}")
        for r in bench_results["results"]:
            print(f"     n={r.get('n', '?')}: {r.get('avg_ms', '?')}ms avg")
    else:
        print(f"   Benchmark: No Python FFI benchmark available (Rust tests only)")

    # Acceptance: tests must pass
    accepted = test_results["success"] and test_results["tests_total"] > 0

    status = "✓ ACCEPTED" if accepted else "✗ REJECTED"
    print(f"   {status}")

    return {
        "paper_title": paper["title"],
        "module": module,
        "tests": test_results,
        "benchmark": bench_results,
        "accepted": accepted,
        "reason": "Tests passed" if accepted else "Tests failed or no tests found",
    }


def validate_papers(papers: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
    """Phase 3: Validate all mapped papers."""
    print("\n" + "=" * 70)
    print("PHASE 3: VALIDATE")
    print("=" * 70)

    results = []
    for p in papers:
        if p.get("mapped_module"):
            result = validate_paper(p)
            results.append(result)

    accepted = sum(1 for r in results if r["accepted"])
    print(f"\n📊 Validated: {len(results)}, Accepted: {accepted}")
    return results


# ── Phase 4-5: COMBINE & PRUNE ──────────────────────────────────────────────

def combine_results(results: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
    """Phase 4: Report distinct module pairs that both have passing tests."""
    print("\n" + "=" * 70)
    print("PHASE 4: COMBINE")
    print("=" * 70)

    accepted = [r for r in results if r["accepted"]]
    if len(accepted) < 2:
        print("   Not enough validated modules for meaningful combinations")
        return []

    # Get unique modules (not papers — same module can map to multiple papers)
    unique_modules = sorted(set(r["module"] for r in accepted))

    combinations = []
    for i, a in enumerate(unique_modules):
        for b in unique_modules[i + 1:]:
            combo = {
                "module_a": a,
                "module_b": b,
                "note": "Both modules have passing GeoMetriDB tests. Combined use depends on application.",
            }
            combinations.append(combo)
            print(f"   {a} + {b}: both pass tests")

    print(f"\n📊 Distinct module pairs: {len(combinations)}")
    return combinations


def prune_combinations(combinations: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
    """Phase 5: No fake Pareto frontier. Return all valid combinations."""
    print("\n" + "=" * 70)
    print("PHASE 5: PRUNE")
    print("=" * 70)
    print(f"   All {len(combinations)} combinations are valid (no pruning needed)")
    return combinations


# ── Phase 6: DOCUMENT ────────────────────────────────────────────────────────

def document_findings(
    papers: List[Dict[str, Any]],
    validations: List[Dict[str, Any]],
    combinations: List[Dict[str, Any]],
    query_set_id: int,
) -> str:
    """Phase 6: Write honest, structured findings."""
    print("\n" + "=" * 70)
    print("PHASE 6: DOCUMENT")
    print("=" * 70)

    timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
    query_set = QUERY_SETS.get(query_set_id, QUERY_SETS[1])

    report = f"""# GeoMetriDB Research Agent Report

**Run Date:** {timestamp}
**Query Set:** {query_set_id}{query_set['name']}
**Total Papers Found:** {len(papers)}
**Mapped to Modules:** {sum(1 for p in papers if p.get('mapped_module'))}
**Validated:** {len(validations)}
**Accepted:** {sum(1 for v in validations if v['accepted'])}
**Combinations:** {len(combinations)}

## Validated Papers

| Paper | Module | Tests | Status | Benchmark |
|-------|--------|-------|--------|-----------|
"""

    for v in validations:
        status = "" if v["accepted"] else ""
        tests = f"{v['tests']['tests_passed']}/{v['tests']['tests_total']}"
        bench = v["benchmark"]["benchmark"] if v["benchmark"] else "N/A"
        report += f"| {v['paper_title'][:50]}... | {v['module']} | {tests} | {status} | {bench} |\n"

    report += "\n## Test Details\n\n"
    for v in validations:
        report += f"### {v['paper_title'][:60]}...\n"
        report += f"- **Module:** {v['module']}\n"
        report += f"- **Tests:** {v['tests']['tests_passed']}/{v['tests']['tests_total']} passed\n"
        report += f"- **Accepted:** {v['accepted']}\n"
        report += f"- **Reason:** {v['reason']}\n"
        if v["benchmark"]:
            report += f"- **Benchmark:** {v['benchmark']['benchmark']}\n"
            for r in v["benchmark"]["results"]:
                report += f"  - n={r.get('n', '?')}: {r.get('avg_ms', '?')}ms avg"
                if "path_len" in r:
                    report += f", path_len={r['path_len']}"
                if "scc_count" in r:
                    report += f", scc_count={r['scc_count']}"
                report += "\n"
        report += "\n"

    report += "\n## Module Combinations\n\n"
    if combinations:
        report += "| Module A | Module B | Note |\n"
        report += "|----------|----------|------|\n"
        for c in combinations:
            report += f"| {c['module_a']} | {c['module_b']} | {c['note']} |\n"
    else:
        report += "No combinations evaluated (insufficient validated modules).\n"

    report += "\n## Raw Papers\n\n"
    for p in papers:
        module = p.get("mapped_module") or "unmapped"
        report += f"- **{p['title'][:70]}** ({p['year']}) -> {module}\n"
        report += f"  Source: {p['url']}\n"
        if p['abstract']:
            report += f"  Abstract: {p['abstract'][:250]}...\n"
        report += "\n"

    # Write report
    output_dir = Path("/home/feanor/Projects/geographdb-core/research_agent/output")
    output_dir.mkdir(parents=True, exist_ok=True)

    report_path = output_dir / "report.md"
    report_path.write_text(report)

    # Write JSON
    json_path = output_dir / "findings.json"
    json_path.write_text(json.dumps({
        "timestamp": timestamp,
        "query_set_id": query_set_id,
        "query_set_name": query_set["name"],
        "papers": papers,
        "validations": validations,
        "combinations": combinations,
    }, indent=2, default=str))

    print(f"   Report: {report_path}")
    print(f"   JSON: {json_path}")

    return str(report_path)


# ── Main ─────────────────────────────────────────────────────────────────────

def run_research_cycle(query_set_id: int) -> str:
    """Run one full research cycle."""
    print("=" * 70)
    print("GEOMETRIDB AUTONOMOUS RESEARCH AGENT")
    print("=" * 70)
    print(f"Query Set: {query_set_id}")

    t0 = time.time()

    # Phase 1: Discover
    papers = discover_formulas(query_set_id)

    # Phase 2: Map
    papers = map_papers(papers)

    # Phase 3: Validate
    validations = validate_papers(papers)

    # Phase 4: Combine
    combinations = combine_results(validations)

    # Phase 5: Prune
    pruned = prune_combinations(combinations)

    # Phase 6: Document
    report_path = document_findings(papers, validations, pruned, query_set_id)

    # Append to persistent findings log (only if we found new papers)
    if papers:
        record = {
            "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
            "query_set_id": query_set_id,
            "query_set_name": QUERY_SETS.get(query_set_id, QUERY_SETS[1])["name"],
            "new_papers_count": len(papers),
            "validations_count": len(validations),
            "accepted_count": sum(1 for v in validations if v["accepted"]),
            "combinations_count": len(pruned),
            "paper_titles": [p["title"] for p in papers],
        }
        append_finding(record)
        print(f"   Appended to historical findings log")

    elapsed = time.time() - t0
    print(f"\n{'=' * 70}")
    print(f"CYCLE COMPLETE in {elapsed:.1f}s")
    print(f"{'=' * 70}")

    return report_path


def main():
    parser = argparse.ArgumentParser(description="GeoMetriDB Autonomous Research Agent")
    parser.add_argument("--query-set", type=int, default=1, help="Query set ID (1-6)")
    parser.add_argument("--mode", choices=["once", "cron"], default="once", help="Run mode")

    args = parser.parse_args()

    if args.mode == "cron":
        # Rotate through query sets
        state_file = Path("/home/feanor/Projects/geographdb-core/research_agent/output/state.json")
        if state_file.exists():
            state = json.loads(state_file.read_text())
            query_set = state.get("last_query_set", 0) + 1
            if query_set > 6:
                query_set = 1
        else:
            query_set = 1

        state = {"last_query_set": query_set}
        state_file.parent.mkdir(parents=True, exist_ok=True)
        state_file.write_text(json.dumps(state))

        # Load historical stats for summary
        all_findings = load_all_findings()
        total_historical = len(all_findings)

        report = run_research_cycle(query_set)

        # After run, show summary with historical context
        new_findings = load_all_findings()
        new_runs = len(new_findings) - total_historical

        print(f"\n{'=' * 70}")
        print("CRON SUMMARY")
        print(f"{'=' * 70}")
        print(f"Historical runs tracked: {len(new_findings)}")
        print(f"Total unique papers seen (all time): {len(load_seen_papers())}")
        print(f"Next query set will be: {(query_set % 6) + 1}")
    else:
        report = run_research_cycle(args.query_set)

    print(f"\nReport saved to: {report}")


if __name__ == "__main__":
    main()