graphrag-core 0.2.0

Core portable library for GraphRAG - works on native and WASM
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
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
//! Graph Weight Optimization (Simplified DW-GRPO)
//!
//! This module implements a simplified version of Dynamic Weighted Group Relative
//! Policy Optimization (DW-GRPO) for optimizing relationship weights in the knowledge graph.
//!
//! Key features:
//! - Heuristic-based optimization (not full reinforcement learning)
//! - Gradient-free hill climbing for weight adjustment
//! - Multi-objective optimization (relevance, faithfulness, conciseness)
//! - Stagnation detection and dynamic weight adjustment
//! - Performance tracking across iterations

use crate::{
    core::{GraphRAGError, KnowledgeGraph, Result},
    ollama::OllamaClient,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// A single optimization iteration with metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationStep {
    /// Iteration number (0-indexed)
    pub iteration: usize,

    /// Relevance score: how well results match the query (0.0-1.0)
    pub relevance_score: f32,

    /// Faithfulness score: how accurate results are vs ground truth (0.0-1.0)
    pub faithfulness_score: f32,

    /// Conciseness score: how compact/focused results are (0.0-1.0)
    pub conciseness_score: f32,

    /// Combined weighted score
    pub combined_score: f32,

    /// Snapshot of relationship weights at this iteration
    pub weights_snapshot: HashMap<String, f32>,
}

impl OptimizationStep {
    /// Create a new optimization step
    pub fn new(iteration: usize) -> Self {
        Self {
            iteration,
            relevance_score: 0.0,
            faithfulness_score: 0.0,
            conciseness_score: 0.0,
            combined_score: 0.0,
            weights_snapshot: HashMap::new(),
        }
    }

    /// Calculate combined score with dynamic weights
    pub fn calculate_combined(&mut self, weights: &ObjectiveWeights) {
        self.combined_score = self.relevance_score * weights.relevance
            + self.faithfulness_score * weights.faithfulness
            + self.conciseness_score * weights.conciseness;
    }
}

/// Weights for combining multiple objectives
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObjectiveWeights {
    /// Weight for relevance objective (default: 0.4)
    pub relevance: f32,

    /// Weight for faithfulness objective (default: 0.4)
    pub faithfulness: f32,

    /// Weight for conciseness objective (default: 0.2)
    pub conciseness: f32,
}

impl Default for ObjectiveWeights {
    fn default() -> Self {
        Self {
            relevance: 0.4,
            faithfulness: 0.4,
            conciseness: 0.2,
        }
    }
}

impl ObjectiveWeights {
    /// Normalize weights to sum to 1.0
    pub fn normalize(&mut self) {
        let sum = self.relevance + self.faithfulness + self.conciseness;
        if sum > 0.0 {
            self.relevance /= sum;
            self.faithfulness /= sum;
            self.conciseness /= sum;
        }
    }

    /// Increase weight for a specific objective
    pub fn boost_objective(&mut self, objective: &str, boost: f32) {
        match objective {
            "relevance" => self.relevance += boost,
            "faithfulness" => self.faithfulness += boost,
            "conciseness" => self.conciseness += boost,
            _ => {},
        }
        self.normalize();
    }
}

/// Test query with expected answer for evaluation
#[derive(Debug, Clone)]
pub struct TestQuery {
    /// The query string
    pub query: String,

    /// Expected answer or key entities
    pub expected_answer: String,

    /// Optional weight for this query (default: 1.0)
    pub weight: f32,
}

impl TestQuery {
    /// Create a new test query
    pub fn new(query: String, expected_answer: String) -> Self {
        Self {
            query,
            expected_answer,
            weight: 1.0,
        }
    }

    /// Create with custom weight
    pub fn with_weight(mut self, weight: f32) -> Self {
        self.weight = weight;
        self
    }
}

/// Configuration for the optimizer
#[derive(Debug, Clone)]
pub struct OptimizerConfig {
    /// Learning rate for weight adjustments (default: 0.1)
    pub learning_rate: f32,

    /// Maximum number of optimization iterations (default: 20)
    pub max_iterations: usize,

    /// Window size for slope calculation (default: 3)
    pub slope_window: usize,

    /// Minimum slope to avoid stagnation (default: 0.01)
    pub stagnation_threshold: f32,

    /// Objective weights
    pub objective_weights: ObjectiveWeights,

    /// Use LLM for quality evaluation (default: true if Ollama available)
    pub use_llm_eval: bool,
}

impl Default for OptimizerConfig {
    fn default() -> Self {
        Self {
            learning_rate: 0.1,
            max_iterations: 20,
            slope_window: 3,
            stagnation_threshold: 0.01,
            objective_weights: ObjectiveWeights::default(),
            use_llm_eval: true,
        }
    }
}

/// Graph weight optimizer using simplified DW-GRPO approach
pub struct GraphWeightOptimizer {
    /// Configuration
    config: OptimizerConfig,

    /// Optimization history
    history: Vec<OptimizationStep>,

    /// Ollama client for LLM-based evaluation
    ollama_client: Option<OllamaClient>,

    /// Current objective weights (dynamic)
    current_weights: ObjectiveWeights,
}

impl GraphWeightOptimizer {
    /// Create a new optimizer with default configuration
    pub fn new() -> Self {
        Self {
            config: OptimizerConfig::default(),
            history: Vec::new(),
            ollama_client: None,
            current_weights: ObjectiveWeights::default(),
        }
    }

    /// Create optimizer with custom configuration
    pub fn with_config(config: OptimizerConfig) -> Self {
        let current_weights = config.objective_weights.clone();
        Self {
            config,
            history: Vec::new(),
            ollama_client: None,
            current_weights,
        }
    }

    /// Set Ollama client for LLM-based evaluation
    pub fn with_ollama_client(mut self, client: OllamaClient) -> Self {
        self.ollama_client = Some(client);
        self
    }

    /// Optimize graph relationship weights based on test queries
    ///
    /// # Arguments
    ///
    /// * `graph` - Mutable reference to the knowledge graph
    /// * `test_queries` - Test queries with expected answers for evaluation
    ///
    /// # Returns
    ///
    /// Result indicating success or error
    #[cfg(feature = "async")]
    pub async fn optimize_weights(
        &mut self,
        graph: &mut KnowledgeGraph,
        test_queries: &[TestQuery],
    ) -> Result<()> {
        if test_queries.is_empty() {
            return Err(GraphRAGError::Config {
                message: "No test queries provided for optimization".to_string(),
            });
        }

        #[cfg(feature = "tracing")]
        tracing::info!(
            max_iterations = self.config.max_iterations,
            num_queries = test_queries.len(),
            "Starting graph weight optimization"
        );

        // Main optimization loop
        for iteration in 0..self.config.max_iterations {
            let mut step = OptimizationStep::new(iteration);

            // Evaluate current graph performance
            let metrics = self.evaluate_graph_quality(graph, test_queries).await?;
            step.relevance_score = metrics.0;
            step.faithfulness_score = metrics.1;
            step.conciseness_score = metrics.2;

            // Calculate combined score
            step.calculate_combined(&self.current_weights);

            // Snapshot current weights
            step.weights_snapshot = self.snapshot_weights(graph);

            // Store step
            self.history.push(step.clone());

            #[cfg(feature = "tracing")]
            tracing::info!(
                iteration = iteration,
                relevance = step.relevance_score,
                faithfulness = step.faithfulness_score,
                conciseness = step.conciseness_score,
                combined = step.combined_score,
                "Optimization iteration complete"
            );

            // Check for stagnation and adjust weights
            if iteration >= self.config.slope_window {
                self.detect_and_adjust_stagnation();
            }

            // Early stopping if all metrics are excellent
            if step.relevance_score > 0.95
                && step.faithfulness_score > 0.95
                && step.conciseness_score > 0.95
            {
                #[cfg(feature = "tracing")]
                tracing::info!("Early stopping: all metrics excellent");
                break;
            }

            // Adjust graph weights for next iteration
            if iteration < self.config.max_iterations - 1 {
                self.adjust_graph_weights(graph, test_queries, &step)
                    .await?;
            }
        }

        #[cfg(feature = "tracing")]
        tracing::info!(
            iterations = self.history.len(),
            final_score = self.history.last().map(|s| s.combined_score).unwrap_or(0.0),
            "Optimization complete"
        );

        Ok(())
    }

    /// Evaluate graph quality across all test queries
    ///
    /// Returns (relevance, faithfulness, conciseness)
    #[cfg(feature = "async")]
    async fn evaluate_graph_quality(
        &self,
        graph: &KnowledgeGraph,
        test_queries: &[TestQuery],
    ) -> Result<(f32, f32, f32)> {
        let mut total_relevance = 0.0;
        let mut total_faithfulness = 0.0;
        let mut total_conciseness = 0.0;
        let mut total_weight = 0.0;

        for test_query in test_queries {
            // ✅ IMPLEMENTED: Real evaluation with heuristic and LLM-based metrics
            //
            // Strategy:
            // 1. If use_llm_eval=true and ollama_client available: Use LLM evaluation
            // 2. Otherwise: Use heuristic metrics (entity matching, string similarity)

            let (relevance, faithfulness, conciseness) =
                if self.config.use_llm_eval && self.ollama_client.is_some() {
                    // LLM-based evaluation
                    self.evaluate_with_llm(graph, test_query).await?
                } else {
                    // Heuristic evaluation (fallback)
                    self.evaluate_with_heuristics(graph, test_query)?
                };

            total_relevance += relevance * test_query.weight;
            total_faithfulness += faithfulness * test_query.weight;
            total_conciseness += conciseness * test_query.weight;
            total_weight += test_query.weight;
        }

        if total_weight > 0.0 {
            Ok((
                total_relevance / total_weight,
                total_faithfulness / total_weight,
                total_conciseness / total_weight,
            ))
        } else {
            Ok((0.0, 0.0, 0.0))
        }
    }

    /// Evaluate query quality using heuristic metrics
    ///
    /// Provides fast, deterministic evaluation without requiring LLM calls.
    fn evaluate_with_heuristics(
        &self,
        graph: &KnowledgeGraph,
        test_query: &TestQuery,
    ) -> Result<(f32, f32, f32)> {
        // Extract query tokens (simple tokenization)
        let query_tokens: Vec<String> = test_query
            .query
            .to_lowercase()
            .split_whitespace()
            .filter(|t| t.len() > 2) // Skip short words
            .map(|s| s.to_string())
            .collect();

        // Extract expected answer tokens
        let answer_tokens: Vec<String> = test_query
            .expected_answer
            .to_lowercase()
            .split_whitespace()
            .map(|s| s.to_string())
            .collect();

        // 1. Relevance: Count entities that match query tokens
        let mut matching_entities = 0;
        let mut total_entities = 0;

        for entity in graph.entities() {
            total_entities += 1;
            let entity_name_lower = entity.name.to_lowercase();

            if query_tokens
                .iter()
                .any(|token| entity_name_lower.contains(token))
            {
                matching_entities += 1;
            }
        }

        let relevance = if total_entities > 0 {
            (matching_entities as f32 / total_entities.min(10) as f32).min(1.0)
        } else {
            0.0
        };

        // 2. Faithfulness: Token overlap between expected answer and graph content
        let mut answer_token_found = 0;

        for token in &answer_tokens {
            // Check if token appears in any entity or relationship
            let found_in_graph = graph.entities().any(|e| {
                e.name.to_lowercase().contains(token)
                    || e.entity_type.to_lowercase().contains(token)
            }) || graph
                .get_all_relationships()
                .iter()
                .any(|r| r.relation_type.to_lowercase().contains(token));

            if found_in_graph {
                answer_token_found += 1;
            }
        }

        let faithfulness = if !answer_tokens.is_empty() {
            answer_token_found as f32 / answer_tokens.len() as f32
        } else {
            0.5 // Neutral if no expected answer provided
        };

        // 3. Conciseness: Inverse of graph complexity
        // Prefer graphs with fewer but higher-confidence relationships
        let avg_confidence: f32 = graph
            .get_all_relationships()
            .iter()
            .map(|r| r.confidence)
            .sum::<f32>()
            / graph.get_all_relationships().len().max(1) as f32;

        let complexity_penalty = (graph.get_all_relationships().len() as f32 / 100.0).min(1.0);
        let conciseness = (avg_confidence * 0.7) + ((1.0 - complexity_penalty) * 0.3);

        Ok((relevance, faithfulness, conciseness))
    }

    /// Evaluate query quality using LLM
    ///
    /// Uses Ollama to judge relevance, faithfulness, and conciseness.
    #[cfg(feature = "async")]
    async fn evaluate_with_llm(
        &self,
        graph: &KnowledgeGraph,
        test_query: &TestQuery,
    ) -> Result<(f32, f32, f32)> {
        let ollama_client = self
            .ollama_client
            .as_ref()
            .ok_or_else(|| GraphRAGError::Config {
                message: "LLM evaluation requested but no Ollama client available".to_string(),
            })?;

        // Build context from graph
        let context = self.build_graph_context(graph, &test_query.query, 5);

        // Prompt for LLM evaluation
        let prompt = format!(
            "Evaluate the quality of information retrieval for this query.\n\n\
             Query: {}\n\
             Expected Answer: {}\n\n\
             Retrieved Information:\n{}\n\n\
             Please evaluate on three dimensions (0.0-1.0 scale):\n\
             1. Relevance: How well does the retrieved information match the query?\n\
             2. Faithfulness: How accurate is the information compared to the expected answer?\n\
             3. Conciseness: How focused and non-redundant is the information?\n\n\
             Respond with JSON format:\n\
             {{\"relevance\": 0.8, \"faithfulness\": 0.7, \"conciseness\": 0.9}}",
            test_query.query, test_query.expected_answer, context
        );

        // Call LLM
        let response =
            ollama_client
                .generate(&prompt)
                .await
                .map_err(|e| GraphRAGError::LanguageModel {
                    message: format!("LLM evaluation failed: {}", e),
                })?;

        // Parse JSON response
        self.parse_llm_evaluation(&response)
    }

    /// Build graph context for a query (top-K relevant entities/relationships)
    fn build_graph_context(&self, graph: &KnowledgeGraph, query: &str, top_k: usize) -> String {
        let query_lower = query.to_lowercase();
        let query_tokens: Vec<_> = query_lower.split_whitespace().collect();

        // Find relevant entities
        let mut entity_scores: Vec<_> = graph
            .entities()
            .map(|e| {
                let name_lower = e.name.to_lowercase();
                let score = query_tokens
                    .iter()
                    .filter(|&&token| name_lower.contains(token))
                    .count();
                (e, score)
            })
            .filter(|(_, score)| *score > 0)
            .collect();

        entity_scores.sort_by_key(|item| std::cmp::Reverse(item.1));

        let mut context = String::new();
        context.push_str("Entities:\n");
        for (entity, _) in entity_scores.iter().take(top_k) {
            context.push_str(&format!("- {} ({})\n", entity.name, entity.entity_type));
        }

        context.push_str("\nRelationships:\n");
        for rel in graph.get_all_relationships().iter().take(top_k) {
            context.push_str(&format!(
                "- {} --[{}]--> {}\n",
                rel.source.0, rel.relation_type, rel.target.0
            ));
        }

        context
    }

    /// Parse LLM evaluation response
    fn parse_llm_evaluation(&self, response: &str) -> Result<(f32, f32, f32)> {
        // Try to extract JSON from response
        let json_start = response.find('{');
        let json_end = response.rfind('}');

        if let (Some(start), Some(end)) = (json_start, json_end) {
            if end > start {
                let json_str = &response[start..=end];
                if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(json_str) {
                    let relevance = parsed["relevance"].as_f64().unwrap_or(0.5) as f32;
                    let faithfulness = parsed["faithfulness"].as_f64().unwrap_or(0.5) as f32;
                    let conciseness = parsed["conciseness"].as_f64().unwrap_or(0.5) as f32;

                    return Ok((
                        relevance.clamp(0.0, 1.0),
                        faithfulness.clamp(0.0, 1.0),
                        conciseness.clamp(0.0, 1.0),
                    ));
                }
            }
        }

        // Fallback to heuristic values if parsing fails
        #[cfg(feature = "tracing")]
        tracing::warn!("Failed to parse LLM evaluation, using default scores");

        Ok((0.5, 0.5, 0.5))
    }

    /// Snapshot current relationship weights
    fn snapshot_weights(&self, graph: &KnowledgeGraph) -> HashMap<String, f32> {
        let mut weights = HashMap::new();

        for rel in graph.get_all_relationships() {
            let key = format!("{}_{}", rel.source.0, rel.target.0);
            weights.insert(key, rel.confidence);
        }

        weights
    }

    /// Detect stagnation in metrics and adjust objective weights
    fn detect_and_adjust_stagnation(&mut self) {
        let window_size = self.config.slope_window;
        let history_len = self.history.len();

        if history_len < window_size + 1 {
            return;
        }

        // Calculate slopes for each metric
        let relevance_slope = self.calculate_slope(window_size, |s| s.relevance_score);
        let faithfulness_slope = self.calculate_slope(window_size, |s| s.faithfulness_score);
        let conciseness_slope = self.calculate_slope(window_size, |s| s.conciseness_score);

        #[cfg(feature = "tracing")]
        tracing::debug!(
            relevance_slope = relevance_slope,
            faithfulness_slope = faithfulness_slope,
            conciseness_slope = conciseness_slope,
            threshold = self.config.stagnation_threshold,
            "Stagnation detection"
        );

        // Boost weights for stagnating metrics (DW-GRPO inspired)
        if relevance_slope.abs() < self.config.stagnation_threshold {
            self.current_weights.boost_objective("relevance", 0.05);
            #[cfg(feature = "tracing")]
            tracing::info!("Boosting relevance weight due to stagnation");
        }

        if faithfulness_slope.abs() < self.config.stagnation_threshold {
            self.current_weights.boost_objective("faithfulness", 0.05);
            #[cfg(feature = "tracing")]
            tracing::info!("Boosting faithfulness weight due to stagnation");
        }

        if conciseness_slope.abs() < self.config.stagnation_threshold {
            self.current_weights.boost_objective("conciseness", 0.05);
            #[cfg(feature = "tracing")]
            tracing::info!("Boosting conciseness weight due to stagnation");
        }
    }

    /// Calculate slope of a metric over recent window
    fn calculate_slope<F>(&self, window_size: usize, metric_fn: F) -> f32
    where
        F: Fn(&OptimizationStep) -> f32,
    {
        let history_len = self.history.len();
        if history_len < window_size + 1 {
            return 0.0;
        }

        let recent_steps = &self.history[history_len - window_size - 1..];
        let first_value = metric_fn(&recent_steps[0]);
        let last_value = metric_fn(&recent_steps[window_size]);

        (last_value - first_value) / window_size as f32
    }

    /// Adjust graph relationship weights using hill climbing
    #[cfg(feature = "async")]
    async fn adjust_graph_weights(
        &self,
        graph: &mut KnowledgeGraph,
        _test_queries: &[TestQuery],
        current_step: &OptimizationStep,
    ) -> Result<()> {
        // Identify which metrics need improvement
        let needs_relevance = current_step.relevance_score < 0.8;
        let needs_faithfulness = current_step.faithfulness_score < 0.8;
        let needs_conciseness = current_step.conciseness_score < 0.8;

        // Adjust relationship confidences
        let relationships = graph.get_all_relationships().to_vec();
        for rel in relationships {
            let mut new_confidence = rel.confidence;

            // Heuristic adjustments based on relationship properties
            if needs_relevance {
                // Boost relationships with high semantic similarity (if embeddings present)
                if rel.embedding.is_some() {
                    new_confidence *= 1.0 + self.config.learning_rate * 0.5;
                }
            }

            if needs_faithfulness {
                // Boost relationships with temporal/causal evidence
                if rel.temporal_type.is_some() || rel.causal_strength.is_some() {
                    new_confidence *= 1.0 + self.config.learning_rate * 0.3;
                }
            }

            if needs_conciseness {
                // Slightly reduce weights to encourage more focused results
                new_confidence *= 1.0 - self.config.learning_rate * 0.1;
            }

            // Clamp to valid range
            new_confidence = new_confidence.clamp(0.1, 1.0);

            // Update in graph (would need graph API to update relationship confidence)
            // For now, this is a placeholder - actual implementation would modify the graph
            let _ = new_confidence; // Use to avoid unused warning
        }

        Ok(())
    }

    /// Get optimization history
    pub fn history(&self) -> &[OptimizationStep] {
        &self.history
    }

    /// Get final metrics after optimization
    pub fn final_metrics(&self) -> Option<(f32, f32, f32, f32)> {
        self.history.last().map(|step| {
            (
                step.relevance_score,
                step.faithfulness_score,
                step.conciseness_score,
                step.combined_score,
            )
        })
    }

    /// Get improvement from first to last iteration
    pub fn total_improvement(&self) -> f32 {
        if self.history.len() < 2 {
            return 0.0;
        }

        let first = self.history.first().expect("non-empty").combined_score;
        let last = self.history.last().expect("non-empty").combined_score;
        last - first
    }
}

impl Default for GraphWeightOptimizer {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_optimization_step_creation() {
        let step = OptimizationStep::new(0);
        assert_eq!(step.iteration, 0);
        assert_eq!(step.relevance_score, 0.0);
    }

    #[test]
    fn test_objective_weights_normalization() {
        let mut weights = ObjectiveWeights {
            relevance: 2.0,
            faithfulness: 2.0,
            conciseness: 2.0,
        };

        weights.normalize();

        // Should sum to 1.0
        let sum = weights.relevance + weights.faithfulness + weights.conciseness;
        assert!((sum - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_objective_weights_boost() {
        let mut weights = ObjectiveWeights::default();
        let original_relevance = weights.relevance;

        weights.boost_objective("relevance", 0.1);

        // Should be boosted and normalized
        assert!(weights.relevance > original_relevance);

        let sum = weights.relevance + weights.faithfulness + weights.conciseness;
        assert!((sum - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_test_query_creation() {
        let query = TestQuery::new("test query".to_string(), "expected".to_string());
        assert_eq!(query.weight, 1.0);

        let weighted = TestQuery::new("test".to_string(), "expected".to_string()).with_weight(2.0);
        assert_eq!(weighted.weight, 2.0);
    }

    #[test]
    fn test_optimizer_initialization() {
        let optimizer = GraphWeightOptimizer::new();
        assert_eq!(optimizer.history.len(), 0);
        assert_eq!(optimizer.config.max_iterations, 20);
    }

    #[test]
    fn test_slope_calculation() {
        let mut optimizer = GraphWeightOptimizer::new();

        // Create history with increasing scores
        for i in 0..5 {
            let mut step = OptimizationStep::new(i);
            step.relevance_score = 0.5 + (i as f32 * 0.1);
            optimizer.history.push(step);
        }

        let slope = optimizer.calculate_slope(3, |s| s.relevance_score);

        // Should be positive (increasing)
        assert!(slope > 0.0);
    }

    #[test]
    fn test_combined_score_calculation() {
        let weights = ObjectiveWeights {
            relevance: 0.5,
            faithfulness: 0.3,
            conciseness: 0.2,
        };

        let mut step = OptimizationStep::new(0);
        step.relevance_score = 0.8;
        step.faithfulness_score = 0.6;
        step.conciseness_score = 0.9;

        step.calculate_combined(&weights);

        let expected = 0.8 * 0.5 + 0.6 * 0.3 + 0.9 * 0.2;
        assert!((step.combined_score - expected).abs() < 0.001);
    }

    #[test]
    fn test_heuristic_evaluation() {
        use crate::core::{Entity, EntityId, Relationship};

        // Create a test graph
        let mut graph = KnowledgeGraph::new();

        // Add entities
        let socrates = Entity {
            id: EntityId("socrates".to_string()),
            name: "Socrates".to_string(),
            entity_type: "PERSON".to_string(),
            confidence: 0.95,
            mentions: vec![],
            embedding: None,
            first_mentioned: None,
            last_mentioned: None,
            temporal_validity: None,
        };

        let philosophy = Entity {
            id: EntityId("philosophy".to_string()),
            name: "Philosophy".to_string(),
            entity_type: "CONCEPT".to_string(),
            confidence: 0.9,
            mentions: vec![],
            embedding: None,
            first_mentioned: None,
            last_mentioned: None,
            temporal_validity: None,
        };

        graph.add_entity(socrates).unwrap();
        graph.add_entity(philosophy).unwrap();

        // Add relationship
        let rel = Relationship::new(
            EntityId("socrates".to_string()),
            EntityId("philosophy".to_string()),
            "FOUNDED".to_string(),
            0.9,
        );
        graph.add_relationship(rel).unwrap();

        // Create optimizer
        let optimizer = GraphWeightOptimizer::new();

        // Create test query - use entity names that appear in the graph
        let query = TestQuery::new(
            "Tell me about Socrates and philosophy".to_string(),
            "Socrates founded philosophy".to_string(),
        );

        // Evaluate
        let (relevance, faithfulness, conciseness) =
            optimizer.evaluate_with_heuristics(&graph, &query).unwrap();

        // Check results are in valid range
        assert!(
            (0.0..=1.0).contains(&relevance),
            "Relevance out of range: {}",
            relevance
        );
        assert!(
            (0.0..=1.0).contains(&faithfulness),
            "Faithfulness out of range: {}",
            faithfulness
        );
        assert!(
            (0.0..=1.0).contains(&conciseness),
            "Conciseness out of range: {}",
            conciseness
        );

        // Should have some relevance since entities match query tokens
        // Query has "Socrates" and "philosophy" which should match entity names
        assert!(
            relevance > 0.0,
            "Should find some relevant entities (relevance={})",
            relevance
        );

        // Should have some faithfulness since expected answer mentions "Socrates", "founded", "philosophy"
        assert!(
            faithfulness > 0.0,
            "Should match expected answer (faithfulness={})",
            faithfulness
        );
    }

    #[test]
    fn test_heuristic_evaluation_empty_graph() {
        let graph = KnowledgeGraph::new();
        let optimizer = GraphWeightOptimizer::new();

        let query = TestQuery::new("test query".to_string(), "test answer".to_string());

        let (relevance, faithfulness, conciseness) =
            optimizer.evaluate_with_heuristics(&graph, &query).unwrap();

        // Empty graph should return low scores
        assert_eq!(relevance, 0.0, "Empty graph should have zero relevance");
        assert!(faithfulness >= 0.0, "Faithfulness should be non-negative");
        assert!(conciseness >= 0.0, "Conciseness should be non-negative");
    }

    #[test]
    fn test_graph_context_building() {
        use crate::core::{Entity, EntityId, Relationship};

        let mut graph = KnowledgeGraph::new();

        // Add entities
        for i in 0..5 {
            let entity = Entity {
                id: EntityId(format!("entity_{}", i)),
                name: format!("Entity {}", i),
                entity_type: "TEST".to_string(),
                confidence: 0.9,
                mentions: vec![],
                embedding: None,
                first_mentioned: None,
                last_mentioned: None,
                temporal_validity: None,
            };
            graph.add_entity(entity).unwrap();
        }

        // Add relationships
        for i in 0..4 {
            let rel = Relationship::new(
                EntityId(format!("entity_{}", i)),
                EntityId(format!("entity_{}", i + 1)),
                "RELATES_TO".to_string(),
                0.8,
            );
            graph.add_relationship(rel).unwrap();
        }

        let optimizer = GraphWeightOptimizer::new();
        let context = optimizer.build_graph_context(&graph, "entity 0", 3);

        // Should include entities and relationships
        assert!(
            context.contains("Entities:"),
            "Context should include entities"
        );
        assert!(
            context.contains("Relationships:"),
            "Context should include relationships"
        );
        assert!(!context.is_empty(), "Context should not be empty");
    }

    #[test]
    fn test_llm_evaluation_parse_json() {
        let optimizer = GraphWeightOptimizer::new();

        // Valid JSON response
        let response = r#"Here is my evaluation:
        {"relevance": 0.8, "faithfulness": 0.7, "conciseness": 0.9}
        That's my assessment."#;

        let (relevance, faithfulness, conciseness) =
            optimizer.parse_llm_evaluation(response).unwrap();

        assert!((relevance - 0.8).abs() < 0.001);
        assert!((faithfulness - 0.7).abs() < 0.001);
        assert!((conciseness - 0.9).abs() < 0.001);
    }

    #[test]
    fn test_llm_evaluation_parse_fallback() {
        let optimizer = GraphWeightOptimizer::new();

        // Invalid/malformed response
        let response = "This is not JSON at all";

        let (relevance, faithfulness, conciseness) =
            optimizer.parse_llm_evaluation(response).unwrap();

        // Should fall back to default values
        assert_eq!(relevance, 0.5);
        assert_eq!(faithfulness, 0.5);
        assert_eq!(conciseness, 0.5);
    }
}