swarm-engine-llm 0.1.6

LLM integration backends for SwarmEngine
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
//! Strategy Advisor - LLM による探索戦略アドバイス
//!
//! 探索の状態に基づいて最適な Selection 戦略を推奨する。
//!
//! # 設計
//!
//! ```text
//! StrategyContext (探索状態)
//!//!//! StrategyPromptBuilder.build()
//!//!//! LlmDecider.call_raw() ──────── 同期ブロッキング (~100ms)
//!//!//! StrategyResponseParser.parse()
//!//!//! StrategyAdvice (推奨戦略)
//! ```
//!
//! # 使用例
//!
//! ```ignore
//! use swarm_engine_llm::strategy_advisor::{LlmStrategyAdvisor, StrategyContext};
//! use swarm_engine_core::exploration::SelectionKind;
//!
//! let advisor = LlmStrategyAdvisor::new(decider, runtime);
//! let context = StrategyContext::new(15, 47, 0.23, SelectionKind::Ucb1);
//! let advice = advisor.advise(&context)?;
//! ```

use std::sync::Arc;

use fuzzy_parser::distance::{find_closest, Algorithm};
use fuzzy_parser::{repair_object_fields, sanitize_json, ObjectSchema};

// Core 層から型をインポート
pub use swarm_engine_core::exploration::{
    SelectionKind, StrategyAdvice, StrategyAdviceError, StrategyAdvisor, StrategyContext,
};

use crate::decider::{LlmDecider, LlmError, LoraConfig};
use crate::json_prompt::strategy_selection_template;

// ============================================================================
// SelectionKind 拡張 - 文字列からの fuzzy パース
// ============================================================================

/// SelectionKind の文字列からの fuzzy パース
pub fn parse_selection_kind_fuzzy(s: &str) -> Option<SelectionKind> {
    // 完全一致(大文字小文字無視)
    let upper = s.to_uppercase();
    match upper.as_str() {
        "FIFO" => return Some(SelectionKind::Fifo),
        "UCB1" => return Some(SelectionKind::Ucb1),
        "GREEDY" => return Some(SelectionKind::Greedy),
        "THOMPSON" => return Some(SelectionKind::Thompson),
        _ => {}
    }

    // Fuzzy match
    let candidates = ["FIFO", "UCB1", "Greedy", "Thompson"];
    if let Some(m) = find_closest(s, candidates, 0.6, Algorithm::JaroWinkler) {
        match m.candidate.as_str() {
            "FIFO" => Some(SelectionKind::Fifo),
            "UCB1" => Some(SelectionKind::Ucb1),
            "Greedy" => Some(SelectionKind::Greedy),
            "Thompson" => Some(SelectionKind::Thompson),
            _ => None,
        }
    } else {
        None
    }
}

// ============================================================================
// StrategyAdviceError 拡張
// ============================================================================

impl From<LlmError> for StrategyAdviceError {
    fn from(e: LlmError) -> Self {
        Self::LlmError(e.message().to_string())
    }
}

// ============================================================================
// StrategyPromptBuilder - プロンプト生成
// ============================================================================

/// 戦略アドバイス用プロンプトビルダー
#[derive(Debug, Clone, Default)]
pub struct StrategyPromptBuilder;

impl StrategyPromptBuilder {
    /// 新規作成
    pub fn new() -> Self {
        Self
    }

    /// プロンプトを生成
    pub fn build(&self, ctx: &StrategyContext) -> String {
        let depth_info = ctx
            .avg_depth
            .map(|d| format!(", depth={:.1}", d))
            .unwrap_or_default();

        // コンテキスト情報を構築
        let content = format!(
            "Strategies: FIFO, UCB1, Greedy, Thompson\n\
             Guidelines: visits<20→UCB1, failure>30%→Thompson, established+low failure→Greedy\n\
             User: frontier={}, visits={}, failure={:.0}%{}, current={}",
            ctx.frontier_count,
            ctx.total_visits,
            ctx.failure_rate * 100.0,
            depth_info,
            ctx.current_strategy,
        );

        // 共通テンプレートを使用
        strategy_selection_template().build(&content)
    }
}

// ============================================================================
// StrategyResponseParser - レスポンスパース
// ============================================================================

/// 戦略レスポンス用 ObjectSchema
const STRATEGY_FIELDS: ObjectSchema =
    ObjectSchema::new(&["strategy", "change", "confidence", "reason"]);

/// 戦略アドバイス用レスポンスパーサー
#[derive(Debug, Clone, Default)]
pub struct StrategyResponseParser;

impl StrategyResponseParser {
    /// 新規作成
    pub fn new() -> Self {
        Self
    }

    /// レスポンスをパース
    pub fn parse(&self, response: &str) -> Result<StrategyAdvice, StrategyAdviceError> {
        // JSON を抽出
        let json_str = self.extract_json(response)?;

        // 構文修復
        let sanitized = sanitize_json(&json_str);
        tracing::debug!(sanitized = %sanitized, "Sanitized strategy JSON");

        // パース
        self.parse_json(&sanitized)
    }

    /// JSON を抽出(```json ブロック対応、自然言語フォールバック)
    fn extract_json(&self, text: &str) -> Result<String, StrategyAdviceError> {
        // ```json ... ``` ブロックを探す
        if let Some(start) = text.find("```json") {
            let content_start = start + 7;
            let remaining = &text[content_start..];
            if let Some(end) = remaining.find("```") {
                let json = remaining[..end].trim();
                if !json.is_empty() {
                    return Ok(json.to_string());
                }
            }
        }

        // { ... } を探す(バランスを取って)
        if let Some(json) = self.extract_balanced_json(text) {
            return Ok(json);
        }

        // フォールバック: 自然言語から戦略キーワードを抽出して JSON 生成
        if let Some(json) = self.extract_from_natural_language(text) {
            tracing::debug!(fallback_json = %json, "Extracted strategy from natural language");
            return Ok(json);
        }

        Err(StrategyAdviceError::ParseError(format!(
            "No JSON found in response: {}",
            text
        )))
    }

    /// 自然言語から戦略キーワードを抽出して JSON 生成
    fn extract_from_natural_language(&self, text: &str) -> Option<String> {
        let text_upper = text.to_uppercase();

        // 優先度順に戦略を検索(推奨を示す文脈を考慮)
        let recommend_patterns = ["RECOMMEND", "SUGGEST", "USE ", "PREFER", "OPTIMAL", "BEST"];
        let strategies = [
            ("THOMPSON", "Thompson"),
            ("UCB1", "UCB1"),
            ("UCB", "UCB1"),
            ("GREEDY", "Greedy"),
            ("FIFO", "FIFO"),
        ];

        // まず推奨文脈付きの戦略を探す
        for pattern in &recommend_patterns {
            if let Some(pos) = text_upper.find(pattern) {
                // パターン後の50文字以内で戦略を探す
                let search_range = &text_upper[pos..std::cmp::min(pos + 50, text_upper.len())];
                for (keyword, strategy) in &strategies {
                    if search_range.contains(keyword) {
                        return Some(format!(
                            r#"{{"strategy":"{}","change":true,"confidence":0.6,"reason":"Extracted from natural language response"}}"#,
                            strategy
                        ));
                    }
                }
            }
        }

        // 推奨文脈がなければ、最初に出現した戦略を使用
        let mut first_match: Option<(usize, &str)> = None;
        for (keyword, strategy) in &strategies {
            if let Some(pos) = text_upper.find(keyword) {
                if first_match.is_none() || pos < first_match.unwrap().0 {
                    first_match = Some((pos, strategy));
                }
            }
        }

        first_match.map(|(_, strategy)| {
            format!(
                r#"{{"strategy":"{}","change":false,"confidence":0.5,"reason":"Extracted from natural language response"}}"#,
                strategy
            )
        })
    }

    /// バランスの取れた JSON を抽出
    fn extract_balanced_json(&self, text: &str) -> Option<String> {
        let start = text.find('{')?;
        let chars: Vec<char> = text[start..].chars().collect();
        let mut depth = 0;
        let mut in_string = false;
        let mut escape_next = false;

        for (i, &ch) in chars.iter().enumerate() {
            if escape_next {
                escape_next = false;
                continue;
            }

            match ch {
                '\\' if in_string => escape_next = true,
                '"' => in_string = !in_string,
                '{' if !in_string => depth += 1,
                '}' if !in_string => {
                    depth -= 1;
                    if depth == 0 {
                        return Some(chars[..=i].iter().collect());
                    }
                }
                _ => {}
            }
        }

        None
    }

    /// JSON をパース(fuzzy repair 対応)
    fn parse_json(&self, json: &str) -> Result<StrategyAdvice, StrategyAdviceError> {
        let mut parsed: serde_json::Value = serde_json::from_str(json)
            .map_err(|e| StrategyAdviceError::ParseError(format!("JSON parse error: {}", e)))?;

        // フィールド名の typo 修復
        if let Some(obj) = parsed.as_object_mut() {
            let corrections = repair_object_fields(obj, &STRATEGY_FIELDS, "$", &Default::default());
            if !corrections.is_empty() {
                tracing::debug!(
                    corrections = ?corrections.iter().map(|c| format!("{}{}", c.original, c.corrected)).collect::<Vec<_>>(),
                    "Fuzzy repaired strategy field names"
                );
            }
        }

        // strategy フィールドをパース(fuzzy repair 対応)
        let strategy_str = parsed["strategy"]
            .as_str()
            .ok_or_else(|| StrategyAdviceError::ParseError("Missing 'strategy' field".into()))?;

        let recommended = parse_selection_kind_fuzzy(strategy_str).ok_or_else(|| {
            StrategyAdviceError::ParseError(format!("Unknown strategy: {}", strategy_str))
        })?;

        let should_change = parsed["change"].as_bool().unwrap_or(false);
        let confidence = parsed["confidence"].as_f64().unwrap_or(0.5).clamp(0.0, 1.0);
        let reason = parsed["reason"]
            .as_str()
            .unwrap_or("No reason provided")
            .to_string();

        Ok(StrategyAdvice {
            recommended,
            should_change,
            reason,
            confidence,
        })
    }
}

// ============================================================================
// LlmStrategyAdvisor - LLM ベースの実装
// ============================================================================

/// LLM ベースの戦略アドバイザー
pub struct LlmStrategyAdvisor {
    decider: Arc<dyn LlmDecider>,
    runtime: tokio::runtime::Handle,
    prompt_builder: StrategyPromptBuilder,
    response_parser: StrategyResponseParser,
    /// 信頼度閾値(これ以下のアドバイスは変更しない)
    confidence_threshold: f64,
    /// LoRA 設定(None の場合はベースモデルのみ)
    lora: Option<LoraConfig>,
}

impl LlmStrategyAdvisor {
    /// 新しい LlmStrategyAdvisor を作成
    pub fn new(decider: Arc<dyn LlmDecider>, runtime: tokio::runtime::Handle) -> Self {
        Self {
            decider,
            runtime,
            prompt_builder: StrategyPromptBuilder::new(),
            response_parser: StrategyResponseParser::new(),
            confidence_threshold: 0.6,
            lora: None,
        }
    }

    /// 信頼度閾値を設定
    pub fn with_confidence_threshold(mut self, threshold: f64) -> Self {
        self.confidence_threshold = threshold.clamp(0.0, 1.0);
        self
    }

    /// 信頼度閾値を取得
    pub fn confidence_threshold(&self) -> f64 {
        self.confidence_threshold
    }

    /// LoRA 設定を設定
    pub fn with_lora(mut self, lora: LoraConfig) -> Self {
        self.lora = Some(lora);
        self
    }

    /// LoRA 設定を取得
    pub fn lora(&self) -> Option<&LoraConfig> {
        self.lora.as_ref()
    }
}

impl StrategyAdvisor for LlmStrategyAdvisor {
    fn advise(&self, context: &StrategyContext) -> Result<StrategyAdvice, StrategyAdviceError> {
        // プロンプト生成
        let prompt = self.prompt_builder.build(context);
        tracing::debug!(prompt = %prompt, "Strategy advisor prompt");

        // 同期ブロッキング呼び出し(~100ms 想定)
        let lora = self.lora.as_ref();
        let response = self
            .runtime
            .block_on(async { self.decider.call_raw(&prompt, lora).await })?;

        tracing::debug!(response = %response, "Strategy advisor raw response");

        // レスポンスパース
        let mut advice = self.response_parser.parse(&response)?;

        // 信頼度が閾値以下なら変更しない
        if advice.confidence < self.confidence_threshold {
            tracing::debug!(
                confidence = advice.confidence,
                threshold = self.confidence_threshold,
                "Low confidence, not changing strategy"
            );
            advice.should_change = false;
            advice.reason = format!(
                "Low confidence ({:.2} < {:.2}): {}",
                advice.confidence, self.confidence_threshold, advice.reason
            );
        }

        tracing::info!(
            recommended = %advice.recommended,
            should_change = advice.should_change,
            confidence = advice.confidence,
            reason = %advice.reason,
            "Strategy advice"
        );

        Ok(advice)
    }

    fn name(&self) -> &str {
        "LlmStrategyAdvisor"
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    // ========================================================================
    // SelectionKind Tests
    // ========================================================================

    #[test]
    fn test_selection_kind_display() {
        assert_eq!(SelectionKind::Fifo.to_string(), "FIFO");
        assert_eq!(SelectionKind::Ucb1.to_string(), "UCB1");
        assert_eq!(SelectionKind::Greedy.to_string(), "Greedy");
        assert_eq!(SelectionKind::Thompson.to_string(), "Thompson");
    }

    #[test]
    fn test_selection_kind_from_str_exact() {
        assert_eq!(
            parse_selection_kind_fuzzy("FIFO"),
            Some(SelectionKind::Fifo)
        );
        assert_eq!(
            parse_selection_kind_fuzzy("UCB1"),
            Some(SelectionKind::Ucb1)
        );
        assert_eq!(
            parse_selection_kind_fuzzy("Greedy"),
            Some(SelectionKind::Greedy)
        );
        assert_eq!(
            parse_selection_kind_fuzzy("Thompson"),
            Some(SelectionKind::Thompson)
        );
    }

    #[test]
    fn test_selection_kind_from_str_case_insensitive() {
        assert_eq!(
            parse_selection_kind_fuzzy("fifo"),
            Some(SelectionKind::Fifo)
        );
        assert_eq!(
            parse_selection_kind_fuzzy("ucb1"),
            Some(SelectionKind::Ucb1)
        );
        assert_eq!(
            parse_selection_kind_fuzzy("GREEDY"),
            Some(SelectionKind::Greedy)
        );
        assert_eq!(
            parse_selection_kind_fuzzy("THOMPSON"),
            Some(SelectionKind::Thompson)
        );
    }

    #[test]
    fn test_selection_kind_from_str_fuzzy() {
        // Typos should be repaired
        assert_eq!(
            parse_selection_kind_fuzzy("Thomspon"),
            Some(SelectionKind::Thompson)
        );
        assert_eq!(
            parse_selection_kind_fuzzy("Gredy"),
            Some(SelectionKind::Greedy)
        );
    }

    #[test]
    fn test_selection_kind_from_str_invalid() {
        assert_eq!(parse_selection_kind_fuzzy("Unknown"), None);
        assert_eq!(parse_selection_kind_fuzzy("Random"), None);
    }

    // ========================================================================
    // StrategyContext Tests
    // ========================================================================

    #[test]
    fn test_strategy_context_new() {
        let ctx = StrategyContext::new(15, 47, 0.23, SelectionKind::Ucb1);
        assert_eq!(ctx.frontier_count, 15);
        assert_eq!(ctx.total_visits, 47);
        assert!((ctx.failure_rate - 0.23).abs() < 0.001);
        assert!((ctx.success_rate - 0.77).abs() < 0.001);
        assert_eq!(ctx.current_strategy, SelectionKind::Ucb1);
        assert!(ctx.avg_depth.is_none());
    }

    #[test]
    fn test_strategy_context_with_depth() {
        let ctx = StrategyContext::new(10, 100, 0.1, SelectionKind::Greedy).with_avg_depth(3.5);
        assert_eq!(ctx.avg_depth, Some(3.5));
    }

    // ========================================================================
    // StrategyAdvice Tests
    // ========================================================================

    #[test]
    fn test_strategy_advice_no_change() {
        let advice = StrategyAdvice::no_change(SelectionKind::Ucb1, "Exploration phase");
        assert_eq!(advice.recommended, SelectionKind::Ucb1);
        assert!(!advice.should_change);
        assert_eq!(advice.reason, "Exploration phase");
        assert!((advice.confidence - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_strategy_advice_change_to() {
        let advice = StrategyAdvice::change_to(SelectionKind::Greedy, "Patterns established", 0.85);
        assert_eq!(advice.recommended, SelectionKind::Greedy);
        assert!(advice.should_change);
        assert_eq!(advice.reason, "Patterns established");
        assert!((advice.confidence - 0.85).abs() < 0.001);
    }

    // ========================================================================
    // StrategyPromptBuilder Tests
    // ========================================================================

    #[test]
    fn test_prompt_builder_basic() {
        let builder = StrategyPromptBuilder::new();
        let ctx = StrategyContext::new(15, 47, 0.23, SelectionKind::Ucb1);
        let prompt = builder.build(&ctx);

        // Few-shot example format
        assert!(prompt.contains("Example interaction:"));
        assert!(prompt.contains("Your JSON:"));

        // Context values
        assert!(prompt.contains("frontier=15"));
        assert!(prompt.contains("visits=47"));
        assert!(prompt.contains("failure=23%"));
        assert!(prompt.contains("current=UCB1"));

        // Strategy names
        assert!(prompt.contains("FIFO"));
        assert!(prompt.contains("Greedy"));
        assert!(prompt.contains("Thompson"));
    }

    #[test]
    fn test_prompt_builder_with_depth() {
        let builder = StrategyPromptBuilder::new();
        let ctx = StrategyContext::new(10, 100, 0.1, SelectionKind::Greedy).with_avg_depth(3.5);
        let prompt = builder.build(&ctx);

        assert!(prompt.contains("depth=3.5"));
    }

    // ========================================================================
    // StrategyResponseParser Tests
    // ========================================================================

    #[test]
    fn test_parse_valid_json() {
        let parser = StrategyResponseParser::new();
        let response = r#"{"strategy": "Greedy", "change": true, "confidence": 0.85, "reason": "Low failure rate"}"#;
        let advice = parser.parse(response).unwrap();

        assert_eq!(advice.recommended, SelectionKind::Greedy);
        assert!(advice.should_change);
        assert!((advice.confidence - 0.85).abs() < 0.001);
        assert_eq!(advice.reason, "Low failure rate");
    }

    #[test]
    fn test_parse_json_with_prefix() {
        let parser = StrategyResponseParser::new();
        let response = r#"Based on the analysis: {"strategy": "Thompson", "change": true, "confidence": 0.7, "reason": "High variance"}"#;
        let advice = parser.parse(response).unwrap();

        assert_eq!(advice.recommended, SelectionKind::Thompson);
    }

    #[test]
    fn test_parse_json_markdown_block() {
        let parser = StrategyResponseParser::new();
        let response = r#"```json
{"strategy": "UCB1", "change": false, "confidence": 0.9, "reason": "Still exploring"}
```"#;
        let advice = parser.parse(response).unwrap();

        assert_eq!(advice.recommended, SelectionKind::Ucb1);
        assert!(!advice.should_change);
    }

    #[test]
    fn test_parse_json_typo_repair() {
        let parser = StrategyResponseParser::new();
        // "straegy" typo should be repaired to "strategy"
        let response =
            r#"{"straegy": "Greedy", "change": true, "confidnce": 0.8, "reason": "test"}"#;
        let advice = parser.parse(response).unwrap();

        assert_eq!(advice.recommended, SelectionKind::Greedy);
    }

    #[test]
    fn test_parse_json_strategy_typo() {
        let parser = StrategyResponseParser::new();
        // "Thomspon" typo should be repaired to "Thompson"
        let response =
            r#"{"strategy": "Thomspon", "change": true, "confidence": 0.75, "reason": "variance"}"#;
        let advice = parser.parse(response).unwrap();

        assert_eq!(advice.recommended, SelectionKind::Thompson);
    }

    #[test]
    fn test_parse_json_defaults() {
        let parser = StrategyResponseParser::new();
        // Missing change and confidence should use defaults
        let response = r#"{"strategy": "FIFO", "reason": "simple"}"#;
        let advice = parser.parse(response).unwrap();

        assert_eq!(advice.recommended, SelectionKind::Fifo);
        assert!(!advice.should_change); // default false
        assert!((advice.confidence - 0.5).abs() < 0.001); // default 0.5
    }

    #[test]
    fn test_parse_json_missing_strategy() {
        let parser = StrategyResponseParser::new();
        let response = r#"{"change": true, "confidence": 0.8}"#;
        let result = parser.parse(response);

        assert!(result.is_err());
        assert!(matches!(result, Err(StrategyAdviceError::ParseError(_))));
    }

    #[test]
    fn test_parse_no_json() {
        let parser = StrategyResponseParser::new();
        let response = "This is just plain text without any JSON.";
        let result = parser.parse(response);

        assert!(result.is_err());
        assert!(matches!(result, Err(StrategyAdviceError::ParseError(_))));
    }

    #[test]
    fn test_parse_confidence_clamping() {
        let parser = StrategyResponseParser::new();
        // Confidence > 1.0 should be clamped
        let response =
            r#"{"strategy": "Greedy", "change": true, "confidence": 1.5, "reason": "test"}"#;
        let advice = parser.parse(response).unwrap();
        assert!((advice.confidence - 1.0).abs() < 0.001);

        // Confidence < 0.0 should be clamped
        let response =
            r#"{"strategy": "Greedy", "change": true, "confidence": -0.5, "reason": "test"}"#;
        let advice = parser.parse(response).unwrap();
        assert!((advice.confidence - 0.0).abs() < 0.001);
    }
}