microresolve 0.2.1

System 1 relay for LLM apps — sub-millisecond intent classification, safety gating, tool selection. CPU-only, continuous learning from corrections.
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
//! Auto-learn pipeline: the single unified learning path used by all flows.
//!
//! # Pipeline
//! Every learning trigger (auto worker, simulate, manual, review) calls:
//!   1. `full_review` — judges the routing result and generates phrases for misses.
//!      - With `ground_truth: Some(gt)` (simulate/training): Turn 1 LLM is skipped;
//!        correct/missed/wrong are computed by set math — cheaper and exact.
//!      - With `ground_truth: None` (auto worker / review): Turn 1 LLM judges.
//!      - Turn 2 LLM always runs for missed intents → generates candidate phrases.
//!   2. `apply_review` — applies the `FullReviewResult`:
//!      - phrase_pipeline → adds accepted phrases via `Resolver::index_phrase`
//!      - L2 Hebbian reinforcement on those phrases
//!      - Anti-Hebbian shrink (negative training) on tokens of wrong detections
//!
//! # LLM utilities
//! `call_llm`, `call_llm_smart`, `extract_json` at the bottom of this file.

use crate::state::*;
use axum::http::StatusCode;
use std::collections::HashMap;

/// Turn 1 (Judge): id + description only. No phrases — descriptions are the interface contract.
/// Flat cost regardless of how many phrases have been learned.
pub fn build_intent_labels(h: &microresolve::NamespaceHandle<'_>) -> String {
    let mut ids = h.intent_ids();
    ids.sort();
    ids.iter()
        .map(|id| {
            let desc = h.intent(id).map(|i| i.description).unwrap_or_default();
            if desc.is_empty() {
                format!("- {} [NO DESCRIPTION — cannot classify reliably]", id)
            } else {
                format!("- {} ({})", id, desc)
            }
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// Turn 2 context: phrases for specific intents, capped to avoid token bloat.
/// Shows most-recently-added phrases first (they reflect what was learned, not just bootstrap seeds).
fn intent_phrases_context(
    h: &microresolve::NamespaceHandle<'_>,
    intent_ids: &[String],
    cap: usize,
) -> String {
    intent_ids
        .iter()
        .map(|id| {
            let desc = h.intent(id).map(|i| i.description).unwrap_or_default();
            let phrases = h.training(id).unwrap_or_default();
            let shown: Vec<&String> = phrases.iter().rev().take(cap).collect();
            let desc_str = if desc.is_empty() {
                String::new()
            } else {
                format!(" ({})", desc)
            };
            format!("  {}{}: {:?}", id, desc_str, shown)
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// Extract JSON from LLM response that may be wrapped in markdown code fences.
/// Handles both object `{}` and array `[]` top-level values.
pub fn extract_json(text: &str) -> &str {
    let trimmed = text.trim();

    // First: try to extract from a ```json ... ``` code fence — most reliable signal.
    // The LLM often wraps JSON in ``` blocks; extract whatever is between the fences.
    if let Some(fence_start) = trimmed.find("```") {
        // skip past the opening fence line (```json\n or ```\n)
        let after_fence = &trimmed[fence_start + 3..];
        let content_start = after_fence.find('\n').map(|i| i + 1).unwrap_or(0);
        let content = &after_fence[content_start..];
        // trim at the closing fence
        let content = if let Some(end) = content.find("```") {
            content[..end].trim()
        } else {
            content.trim()
        };
        // Now extract array or object from the fenced content
        let arr = content.find('[');
        let obj = content.find('{');
        let use_array = match (arr, obj) {
            (Some(a), Some(o)) => a <= o,
            (Some(_), None) => true,
            _ => false,
        };
        if use_array {
            if let (Some(s), Some(e)) = (content.find('['), content.rfind(']')) {
                return &content[s..=e];
            }
        }
        if let (Some(s), Some(e)) = (content.find('{'), content.rfind('}')) {
            return &content[s..=e];
        }
    }

    // Fallback: scan for the last `[` ... `]` or `{` ... `}` that look like JSON
    // (the last `]` / `}` is more reliable than the first `[` / `{` in preamble text)
    let last_array_end = trimmed.rfind(']');
    let last_object_end = trimmed.rfind('}');
    match (last_array_end, last_object_end) {
        (Some(ae), Some(oe)) if ae > oe => {
            // array ends later — extract array
            if let (Some(s), Some(e)) = (trimmed.find('['), trimmed.rfind(']')) {
                return &trimmed[s..=e];
            }
        }
        (None, Some(_)) | (Some(_), Some(_)) => {
            if let (Some(s), Some(e)) = (trimmed.find('{'), trimmed.rfind('}')) {
                return &trimmed[s..=e];
            }
        }
        (Some(_), None) => {
            if let (Some(s), Some(e)) = (trimmed.find('['), trimmed.rfind(']')) {
                return &trimmed[s..=e];
            }
        }
        _ => {}
    }
    trimmed
}

/// Call LLM for text generation. Supports two formats:
/// - "anthropic": Anthropic Messages API (/v1/messages)
/// - "openai" (or anything else): OpenAI Chat Completions format (/v1/chat/completions)
///   Covers: OpenAI, Ollama, Groq, Together, DeepSeek, vLLM, LM Studio, any compatible endpoint.
pub async fn call_llm(
    state: &ServerState,
    prompt: &str,
    max_tokens: u32,
) -> Result<String, (StatusCode, String)> {
    let provider = std::env::var("LLM_PROVIDER").unwrap_or_else(|_| "anthropic".to_string());
    let model = std::env::var("LLM_MODEL").unwrap_or_else(|_| match provider.as_str() {
        "gemini" => "gemini-2.5-flash".to_string(),
        _ => "claude-haiku-4-5-20251001".to_string(),
    });
    call_llm_with_model(state, prompt, max_tokens, &model).await
}

async fn call_llm_with_model(
    state: &ServerState,
    prompt: &str,
    max_tokens: u32,
    model: &str,
) -> Result<String, (StatusCode, String)> {
    match call_llm_once(state, prompt, max_tokens, model).await {
        Ok(text) => Ok(text),
        Err((status, msg)) => {
            // One retry after short wait for rate limits (free tier APIs)
            let is_rate_limit = status == StatusCode::TOO_MANY_REQUESTS
                || msg.contains("429")
                || msg.contains("rate")
                || msg.contains("quota")
                || status == StatusCode::SERVICE_UNAVAILABLE
                || msg.contains("503")
                || msg.contains("overloaded");

            if is_rate_limit {
                eprintln!("[llm] rate limited, waiting 3s then retrying once");
                tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
                call_llm_once(state, prompt, max_tokens, model).await
            } else {
                Err((status, msg))
            }
        }
    }
}

async fn call_llm_once(
    state: &ServerState,
    prompt: &str,
    max_tokens: u32,
    model: &str,
) -> Result<String, (StatusCode, String)> {
    let key = state.llm_key.as_ref().ok_or_else(|| {
        (
            StatusCode::SERVICE_UNAVAILABLE,
            "LLM_API_KEY not set. Add it to .env file.".to_string(),
        )
    })?;

    let provider = std::env::var("LLM_PROVIDER").unwrap_or_else(|_| "anthropic".to_string());

    // Gemini thinking models need higher output budget (thinking tokens count against limit)
    // Gemini models: use reasonable output budget (not too high — causes slow thinking)
    let effective_max = if provider == "gemini" {
        max_tokens.max(512)
    } else {
        max_tokens
    };

    let resp = match provider.as_str() {
        "gemini" => {
            // Google Gemini API: key in query param, different body format
            let url = format!(
                "https://generativelanguage.googleapis.com/v1beta/models/{}:generateContent?key={}",
                model, key
            );
            let body = serde_json::json!({
                "contents": [{"parts": [{"text": prompt}]}],
                "generationConfig": {
                    "maxOutputTokens": effective_max,
                    "temperature": 0.3,
                    "thinkingConfig": { "thinkingBudget": 0 }
                }
            });
            state
                .http
                .post(&url)
                .header("content-type", "application/json")
                .json(&body)
                .send()
                .await
        }
        "anthropic" => {
            let url = std::env::var("LLM_API_URL")
                .unwrap_or_else(|_| "https://api.anthropic.com/v1/messages".to_string());
            let body = serde_json::json!({
                "model": model,
                "max_tokens": max_tokens,
                "messages": [{"role": "user", "content": prompt}],
            });
            state
                .http
                .post(&url)
                .header("x-api-key", key)
                .header("anthropic-version", "2023-06-01")
                .header("content-type", "application/json")
                .json(&body)
                .send()
                .await
        }
        _ => {
            // OpenAI-compatible (OpenAI, Ollama, Groq, DeepSeek, etc.)
            let url = std::env::var("LLM_API_URL")
                .unwrap_or_else(|_| "https://api.openai.com/v1/chat/completions".to_string());
            let body = serde_json::json!({
                "model": model,
                "max_tokens": max_tokens,
                "messages": [{"role": "user", "content": prompt}],
            });
            state
                .http
                .post(&url)
                .header("Authorization", format!("Bearer {}", key))
                .header("content-type", "application/json")
                .json(&body)
                .send()
                .await
        }
    }
    .map_err(|e| {
        (
            StatusCode::BAD_GATEWAY,
            format!("LLM request failed: {}", e),
        )
    })?;

    if !resp.status().is_success() {
        let status = resp.status().as_u16();
        let text = resp.text().await.unwrap_or_default();
        return Err((
            StatusCode::BAD_GATEWAY,
            format!("LLM API {}: {}", status, text),
        ));
    }

    let data: serde_json::Value = resp
        .json()
        .await
        .map_err(|e| (StatusCode::BAD_GATEWAY, format!("Bad response: {}", e)))?;

    // Extract text based on provider response format
    match provider.as_str() {
        "gemini" => {
            // Gemini: candidates[0].content.parts[0].text
            data["candidates"][0]["content"]["parts"][0]["text"]
                .as_str()
                .map(|s| s.trim().to_string())
                .ok_or_else(|| {
                    let raw = serde_json::to_string(&data).unwrap_or_default();
                    (
                        StatusCode::BAD_GATEWAY,
                        format!("Invalid JSON from LLM: {}", &raw[..raw.len().min(200)]),
                    )
                })
        }
        "anthropic" => data["content"][0]["text"]
            .as_str()
            .map(|s| s.trim().to_string())
            .ok_or_else(|| (StatusCode::BAD_GATEWAY, "No text in response".to_string())),
        _ => data["choices"][0]["message"]["content"]
            .as_str()
            .map(|s| s.trim().to_string())
            .ok_or_else(|| (StatusCode::BAD_GATEWAY, "No text in response".to_string())),
    }
}

// --- Shared phrase pipeline: guard + one LLM retry ---

/// Result of the phrase pipeline.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PhrasePipelineResult {
    /// Phrases that were successfully added: (intent_id, phrase)
    pub added: Vec<(String, String)>,
    /// Phrases permanently blocked after all retry rounds: (intent_id, phrase, reason)
    pub blocked: Vec<(String, String, String)>,
    /// How many phrases initially hit the collision guard
    pub initially_blocked: usize,
    /// How many of those were rescued by retry (added via alternative phrase)
    pub recovered_by_retry: usize,
    /// LLM-suggested alternatives for user-edited phrases (when auto_apply=false)
    pub suggestions: Vec<(String, String)>, // (intent_id, suggested_phrase)
}

/// Add phrases to Resolver storage. No collision guard — IDF self-regulates.
/// Duplicates and empty phrases are silently skipped.
pub async fn phrase_pipeline(
    state: &AppState,
    app_id: &str,
    phrases_by_intent: &HashMap<String, Vec<String>>,
    _auto_apply_retry: bool,
    lang: &str,
) -> PhrasePipelineResult {
    let mut added = Vec::new();
    let blocked_final = Vec::new();

    if let Some(h) = state.engine.try_namespace(app_id) {
        for (intent_id, phrases) in phrases_by_intent {
            for phrase in phrases {
                let s = phrase.trim().to_string();
                if s.is_empty() {
                    continue;
                }
                let result = h
                    .add_phrase(intent_id, &s, lang)
                    .expect("server is standalone; ConnectMode unreachable");
                if result.added {
                    added.push((intent_id.clone(), s));
                }
            }
        }
        if !added.is_empty() {
            maybe_commit(state, app_id);
        }
    }

    PhrasePipelineResult {
        added,
        blocked: blocked_final,
        initially_blocked: 0,
        recovered_by_retry: 0,
        suggestions: vec![],
    }
}

// --- Shared full review: 3 turns + guard + apply ---

/// A phrase that was blocked by the collision guard.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BlockedPhrase {
    pub intent: String,
    pub phrase: String,
    pub reason: String,
}

/// Result of a full review — returned by `full_review`, consumed by `apply_review`.
///
/// This is the single data contract between the review step and the apply step,
/// used by all flows: auto worker, simulate, manual, and review queue.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct FullReviewResult {
    /// Intents that were correctly detected
    pub correct_intents: Vec<String>,
    /// Intents that were incorrectly detected (false positives) — triggers L3 inhibition
    pub wrong_detections: Vec<String>,
    /// Intents the query expressed but the engine missed — triggers phrase gen + L2 learn
    pub missed_intents: Vec<String>,
    /// Detected query languages (used to pick phrase generation language)
    pub languages: Vec<String>,
    /// True when detection was perfect — no learning needed, Turns 2+ were skipped
    pub detection_perfect: bool,
    /// Phrases to add per intent (passed the collision guard in Turn 2)
    pub phrases_to_add: HashMap<String, Vec<String>>,
    /// Phrases blocked by the collision guard after retry
    pub phrases_blocked: Vec<BlockedPhrase>,
    /// Human-readable summary (empty when detection_perfect=true)
    pub summary: String,
    /// LLM-extracted spans from the query that express each intent.
    /// Used to learn precise n-gram patterns from the customer's own words.
    #[serde(default)]
    pub spans_to_learn: Vec<(String, String)>, // (intent_id, span text)
}

/// Run the full review for a query.
///
/// `ground_truth`: when provided (simulate/manual flows), Turn 1 LLM is skipped entirely —
/// correct/missed/wrong are computed by set math, which is both cheaper and more accurate.
/// When `None` (auto worker), Turn 1 LLM judges correctness from detected intents alone.
pub async fn full_review(
    state: &AppState,
    app_id: &str,
    query: &str,
    detected: &[String],
    ground_truth: Option<&[String]>,
) -> Result<FullReviewResult, String> {
    // ── When ground truth is known: skip Turn 1 LLM entirely ──────────────────
    if let Some(gt) = ground_truth {
        use std::collections::HashSet;
        let gt_set: HashSet<&str> = gt.iter().map(|s| s.as_str()).collect();
        let det_set: HashSet<&str> = detected.iter().map(|s| s.as_str()).collect();

        let correct_intents: Vec<String> = detected
            .iter()
            .filter(|s| gt_set.contains(s.as_str()))
            .cloned()
            .collect();
        let wrong_detections: Vec<String> = detected
            .iter()
            .filter(|s| !gt_set.contains(s.as_str()))
            .cloned()
            .collect();
        let missed_intents: Vec<String> = gt
            .iter()
            .filter(|s| !det_set.contains(s.as_str()))
            .cloned()
            .collect();

        eprintln!("[full_review] ground_truth provided — skipping Turn 1. correct={:?} wrong={:?} missed={:?}",
            correct_intents, wrong_detections, missed_intents);

        if wrong_detections.is_empty() && missed_intents.is_empty() {
            eprintln!("[full_review] detection perfect");
            return Ok(FullReviewResult {
                correct_intents,
                wrong_detections,
                missed_intents,
                languages: vec!["en".to_string()],
                detection_perfect: true,
                phrases_to_add: HashMap::new(),
                phrases_blocked: Vec::new(),
                summary: "Detection correct, no changes needed.".to_string(),
                spans_to_learn: vec![],
            });
        }

        // Run Turn 2 with the computed sets (reuse shared helper below)
        return full_review_from_sets(
            state,
            app_id,
            query,
            correct_intents,
            wrong_detections,
            missed_intents,
            vec!["en".to_string()],
        )
        .await;
    }

    // ── Confidence short-circuit (UI setting: review_skip_threshold) ──────────
    // If top detected intent scores above threshold, trust routing — skip Turn 1 LLM.
    let skip_threshold = state.ui_settings.read().unwrap().review_skip_threshold;
    if skip_threshold > 0.0 && !detected.is_empty() {
        let top_score = state
            .engine
            .try_namespace(app_id)
            .map(|h| {
                let (all_scores, _) = h.score_all(query);
                detected
                    .iter()
                    .filter_map(|id| all_scores.iter().find(|(s, _)| s == id).map(|(_, sc)| *sc))
                    .fold(0.0f32, f32::max)
            })
            .unwrap_or(0.0);
        if top_score >= skip_threshold {
            eprintln!("[full_review] confidence short-circuit: top score {:.2} >= threshold {:.2} — skipping Turn 1", top_score, skip_threshold);
            return Ok(FullReviewResult {
                correct_intents: detected.to_vec(),
                wrong_detections: vec![],
                missed_intents: vec![],
                languages: vec!["en".to_string()],
                detection_perfect: true,
                phrases_to_add: HashMap::new(),
                phrases_blocked: Vec::new(),
                summary: format!(
                    "High confidence ({:.0}%) — routing trusted, Turn 1 skipped.",
                    top_score * 100.0
                ),
                spans_to_learn: vec![],
            });
        }
    }

    // ── Turn 1 LLM judge (auto worker — ground truth unknown) ─────────────────
    let intent_labels = state
        .engine
        .try_namespace(app_id)
        .map(|h| build_intent_labels(&h))
        .unwrap_or_default();

    let detected_with_scores: String = state
        .engine
        .try_namespace(app_id)
        .map(|h| {
            let (all_scores, _) = h.score_all(query);
            let score_map: HashMap<&str, f32> =
                all_scores.iter().map(|(id, s)| (id.as_str(), *s)).collect();
            if detected.is_empty() {
                "(none detected)".to_string()
            } else {
                detected
                    .iter()
                    .map(|id| {
                        let score = score_map.get(id.as_str()).copied().unwrap_or(0.0);
                        format!("  {} (L2 score: {:.2})", id, score)
                    })
                    .collect::<Vec<_>>()
                    .join("\n")
            }
        })
        .unwrap_or_else(|| {
            if detected.is_empty() {
                "(none detected)".to_string()
            } else {
                detected
                    .iter()
                    .map(|id| format!("  {}", id))
                    .collect::<Vec<_>>()
                    .join("\n")
            }
        });

    // === Turn 1: Judge ===
    // Also asks: is this query relevant to this namespace at all?
    // out_of_scope = true means the query has no domain signal for any intent here —
    // pure conversational filler or a completely different domain. Skip all learning.
    let turn1_prompt = format!(
        "Customer query: \"{query}\"\n\
         Resolver detected:\n{detected_with_scores}\n\n\
         Available intents:\n{intent_labels}\n\n\
         Which intents does this query EXPLICITLY express? Only literal, not implied.\n\
         Which detected intents are WRONG (false positives)?\n\
         Which intents does the query express that were NOT detected (missed)?\n\
         What language is the query in?\n\
         Is this query completely irrelevant to all available intents? (pure filler, wrong domain, only pronouns with no actionable signal)\n\
         Respond with ONLY JSON:\n\
         {{\"correct_intents\": [\"intent_id\"], \"wrong_detections\": [\"intent_id\"], \"missed_intents\": [\"intent_id\"], \"languages\": [\"en\"], \"out_of_scope\": false}}\n"
    );

    let t1_response = call_llm(state, &turn1_prompt, 256)
        .await
        .map_err(|e| format!("Turn 1 failed: {}", e.1))?;
    let t1_parsed: serde_json::Value = serde_json::from_str(extract_json(&t1_response))
        .map_err(|e| format!("Turn 1 parse failed: {}", e))?;

    let correct_intents: Vec<String> = t1_parsed["correct_intents"]
        .as_array()
        .map(|a| {
            a.iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect()
        })
        .unwrap_or_default();
    let wrong_detections: Vec<String> = t1_parsed["wrong_detections"]
        .as_array()
        .map(|a| {
            a.iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect()
        })
        .unwrap_or_default();
    let missed_intents: Vec<String> = t1_parsed["missed_intents"]
        .as_array()
        .map(|a| {
            a.iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect()
        })
        .unwrap_or_default();
    let languages: Vec<String> = t1_parsed["languages"]
        .as_array()
        .map(|a| {
            a.iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect()
        })
        .unwrap_or_else(|| vec!["en".to_string()]);
    let out_of_scope = t1_parsed["out_of_scope"].as_bool().unwrap_or(false);

    eprintln!(
        "[full_review] Turn 1: correct={:?}, wrong={:?}, missed={:?}, langs={:?}, out_of_scope={}",
        correct_intents, wrong_detections, missed_intents, languages, out_of_scope
    );

    // === Early exit: out of scope — query irrelevant to this namespace ===
    if out_of_scope {
        eprintln!("[full_review] Query out of scope for namespace — skipping all learning");
        return Ok(FullReviewResult {
            correct_intents: vec![],
            wrong_detections: vec![],
            missed_intents: vec![],
            languages,
            detection_perfect: false,
            phrases_to_add: HashMap::new(),
            phrases_blocked: Vec::new(),
            summary: "Query out of scope for this namespace — no learning applied.".to_string(),
            spans_to_learn: vec![],
        });
    }

    // === Early exit: detection is perfect — skip Turns 2 + 3 ===
    if wrong_detections.is_empty() && missed_intents.is_empty() {
        eprintln!("[full_review] Detection perfect — skipping Turns 2+3");
        return Ok(FullReviewResult {
            correct_intents,
            wrong_detections,
            missed_intents,
            languages,
            detection_perfect: true,
            phrases_to_add: HashMap::new(),
            phrases_blocked: Vec::new(),
            summary: "Detection correct, no changes needed.".to_string(),
            spans_to_learn: vec![],
        });
    }

    full_review_from_sets(
        state,
        app_id,
        query,
        correct_intents,
        wrong_detections,
        missed_intents,
        languages,
    )
    .await
}

/// Shared Turn 2 logic — runs after correct/missed/wrong are known (either from Turn 1 LLM or set math).
async fn full_review_from_sets(
    state: &AppState,
    app_id: &str,
    query: &str,
    correct_intents: Vec<String>,
    wrong_detections: Vec<String>,
    missed_intents: Vec<String>,
    languages: Vec<String>,
) -> Result<FullReviewResult, String> {
    // Fix #1 (2026-04-24): Turn 2 has no job when there are no missed intents.
    // Previously, Turn 2 still ran if wrong_detections was non-empty — leaving
    // the prompt with an empty "intents that need coverage" list, which led
    // Haiku to invent intents (observed: "account_balance_inquiry" on benign
    // CLINC queries). Skip Turn 2 entirely in the (wrong≠∅, missed=∅) case.
    // wrong_detections still reach apply_review's anti-Hebbian branch.
    if missed_intents.is_empty() {
        eprintln!(
            "[full_review] no missed intents — skipping Turn 2 (no phrase-generation needed)"
        );
        return Ok(FullReviewResult {
            correct_intents,
            wrong_detections,
            missed_intents,
            languages,
            detection_perfect: false,
            phrases_to_add: HashMap::new(),
            phrases_blocked: Vec::new(),
            summary: String::new(),
            spans_to_learn: Vec::new(),
        });
    }

    // === Turn 2: Fix misses — generate training phrases for missed intents ===
    // Show the MISSED intents' current phrases so LLM avoids duplicating what's already there.
    // Include descriptions so the LLM understands each intent's scope.
    let all_relevant_intents: Vec<String> = missed_intents
        .iter()
        .chain(correct_intents.iter())
        .cloned()
        .collect::<std::collections::LinkedList<_>>()
        .into_iter()
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();

    let existing_phrases: String = state
        .engine
        .try_namespace(app_id)
        .map(|h| intent_phrases_context(&h, &all_relevant_intents, 15))
        .unwrap_or_default();

    // Language instruction: if non-English, generate phrases in that language too
    let detected_lang = languages.first().map(|s| s.as_str()).unwrap_or("en");
    let lang_instruction = if detected_lang == "en" {
        String::new()
    } else {
        format!("\nThe query is in \"{detected_lang}\". Generate phrases in \"{detected_lang}\" for missed intents.\n")
    };

    let missed_labels: String = state
        .engine
        .try_namespace(app_id)
        .map(|h| {
            missed_intents
                .iter()
                .map(|id| {
                    let desc = h.intent(id).map(|i| i.description).unwrap_or_default();
                    let count = h.training(id).unwrap_or_default().len();
                    let coverage = if count >= 20 {
                        format!(" [{} phrases — well covered, be very targeted]", count)
                    } else if count >= 10 {
                        format!(" [{} phrases — add vocabulary not yet represented]", count)
                    } else {
                        format!(" [{} phrases — add diverse new vocabulary]", count)
                    };
                    if desc.is_empty() {
                        format!("  - {}{}", id, coverage)
                    } else {
                        format!("  - {} ({}){}", id, desc, coverage)
                    }
                })
                .collect::<Vec<_>>()
                .join("\n")
        })
        .unwrap_or_else(|| {
            missed_intents
                .iter()
                .map(|id| format!("  - {}", id))
                .collect::<Vec<_>>()
                .join("\n")
        });

    let turn2_prompt = format!(
        "{guidelines}\n\n\
         Customer query: \"{query}\"\n\n\
         Intents that need more training coverage:\n{missed_labels}\n\n\
         Phrases already in the system for these intents (do not duplicate):\n{existing_phrases}\n\
         {lang_instruction}\
         Respond with ONLY JSON:\n\
         {{\"phrases_by_intent\": {{\"intent_id\": \"extracted span\"}}}}\n",
        guidelines = microresolve::phrase::REVIEW_FIX_GUIDELINES,
        query = query,
    );

    let t2_response = call_llm(state, &turn2_prompt, 150)
        .await
        .map_err(|e| format!("Turn 2 failed: {}", e.1))?;
    let t2_parsed: serde_json::Value =
        serde_json::from_str(extract_json(&t2_response)).map_err(|e| {
            eprintln!(
                "[full_review] Turn 2 parse error: {}. Raw: {}",
                e,
                &t2_response[..t2_response.len().min(300)]
            );
            format!("Turn 2 parse failed: {}", e)
        })?;

    // Pre-validate phrases through guard (read-only check + retry)
    let mut phrases_to_add: HashMap<String, Vec<String>> = HashMap::new();
    let phrases_blocked = Vec::new();
    let spans_to_learn: Vec<(String, String)> = Vec::new();

    if let Some(sbi) = t2_parsed
        .get("phrases_by_intent")
        .and_then(|v| v.as_object())
    {
        if let Some(h) = state.engine.try_namespace(app_id) {
            for (intent_id, phrase_val) in sbi {
                let exists = h.training(intent_id).is_some();
                if !exists {
                    eprintln!("[auto-learn/guard] skipping LLM-hallucinated intent '{}' (not in namespace)", intent_id);
                    continue;
                }
                let phrase_str = if let Some(s) = phrase_val.as_str() {
                    Some(s.to_string())
                } else if let Some(arr) = phrase_val.as_array() {
                    arr.first().and_then(|v| v.as_str()).map(|s| s.to_string())
                } else {
                    None
                };
                if let Some(s) = phrase_str {
                    let s = s.trim().to_string();
                    if s.is_empty() {
                        continue;
                    }
                    let check = h.check_phrase(intent_id, &s);
                    if !check.redundant
                        && check.warning.as_deref() != Some("No content terms after tokenization")
                    {
                        phrases_to_add.entry(intent_id.clone()).or_default().push(s);
                    }
                }
            }
        }
    }

    // spans_to_learn is now empty — span extraction was merged into phrases_by_intent.
    // The LLM extracts the intent-bearing span from the user's query and returns it
    // as the phrase directly, so it is stored in the phrase registry and survives rebuilds.

    eprintln!(
        "[full_review] Turn 2: phrases_to_add={:?}, blocked={}",
        phrases_to_add,
        phrases_blocked.len()
    );

    let summary = String::new();

    Ok(FullReviewResult {
        correct_intents,
        wrong_detections,
        missed_intents,
        languages,
        detection_perfect: false,
        phrases_to_add,
        phrases_blocked,
        summary,
        spans_to_learn,
    })
}

/// Apply a full review result: add phrases, update L2 weights, anti-Hebbian
/// shrink on wrong detections.
pub async fn apply_review(
    state: &AppState,
    app_id: &str,
    result: &FullReviewResult,
    original_query: &str,
) -> usize {
    let mut added = 0;

    // Add phrases to Resolver storage (phrase registry) — collision guard applies.
    // L2 learning is NOT gated by the Resolver — word_intent learns from ALL phrases.
    if !result.phrases_to_add.is_empty() {
        let lang = result.languages.first().map(|s| s.as_str()).unwrap_or("en");
        let pipeline_result =
            phrase_pipeline(state, app_id, &result.phrases_to_add, true, lang).await;
        added = pipeline_result.added.len();
    }

    // ── Resolver-local mutations ──────────────────────────────────────────────
    // The deterministic learning core lives in microresolve::Resolver — server
    // just calls it. Bindings (python/node) get the same behaviour for free.
    let has_learning = !result.correct_intents.is_empty()
        || !result.missed_intents.is_empty()
        || !result.wrong_detections.is_empty();

    if has_learning {
        let Some(h) = state.engine.try_namespace(app_id) else {
            return added;
        };

        let no_phrases: std::collections::HashMap<String, Vec<String>> =
            std::collections::HashMap::new();
        h.apply_review(
            &no_phrases,
            &result.spans_to_learn,
            &result.wrong_detections,
            original_query,
            0.1,
        )
        .expect("server is standalone; ConnectMode unreachable");
        if !result.wrong_detections.is_empty() {
            eprintln!(
                "[auto-learn/L2b] shrink weights on query tokens for wrong intents: {:?}",
                result.wrong_detections
            );
        }
        for (span_intent, span_text) in &result.spans_to_learn {
            eprintln!(
                "[auto-learn/query] span '{}' → '{}'",
                span_text, span_intent
            );
        }

        if let Err(e) = h.flush() {
            eprintln!("[auto-learn/L2] flush error: {}", e);
        } else {
            eprintln!("[auto-learn/L2] state persisted for '{}'", app_id);
        }
    }

    added
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_extract_json_fenced_array() {
        let s = "```json\n[\n  {\"from\": \"x\", \"to\": \"y\"}\n]\n```";
        let r = extract_json(s);
        assert!(r.starts_with('['), "expected array, got: {:?}", r);
    }
    #[test]
    fn test_extract_json_preamble_then_fence() {
        let s = "Here are edges for {cancel_sub, create_repo}:\n```json\n[\n  {\"from\": \"x\"}\n]\n```";
        let r = extract_json(s);
        assert!(r.starts_with('['), "expected array, got: {:?}", r);
    }
}