plato-nervous 0.1.0

Room-specific model distillation for PLATO nervous system
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
//! Ollama Integration for PLATO Nervous System
//!
//! Replaces the SIMULATED nano model with real LLM inference via ollama.
//! Connects to a local ollama server at localhost:11434.
//!
//! Two model tiers:
//! - Nano (350M): per-room anomaly detection (e.g. liquid-350m)
//! - Fleet (1.2B): cross-room coordination (e.g. liquid-1.2b)

use crate::{ResolutionLayer, SensorReading, Tile, TileType};
use serde::{Deserialize, Serialize};
use std::time::Instant;
use uuid::Uuid;

// ── Error Type ───────────────────────────────────────────────────────

#[derive(Debug)]
pub enum OllamaError {
    Http(reqwest::Error),
    Parse(String),
    Timeout,
    ModelNotAvailable(String),
    ServerOffline,
}

impl std::fmt::Display for OllamaError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            OllamaError::Http(e) => write!(f, "HTTP error: {}", e),
            OllamaError::Parse(msg) => write!(f, "Parse error: {}", msg),
            OllamaError::Timeout => write!(f, "Request timed out"),
            OllamaError::ModelNotAvailable(m) => write!(f, "Model '{}' not available", m),
            OllamaError::ServerOffline => write!(f, "Ollama server offline"),
        }
    }
}

impl std::error::Error for OllamaError {}

impl From<reqwest::Error> for OllamaError {
    fn from(e: reqwest::Error) -> Self {
        OllamaError::Http(e)
    }
}

// ── Ollama API Types ─────────────────────────────────────────────────

#[derive(Debug, Deserialize)]
struct OllamaGenerateResponse {
    response: String,
    #[serde(default)]
    #[allow(dead_code)]
    done: bool,
    #[serde(default)]
    #[allow(dead_code)]
    eval_duration: Option<u64>,
    #[serde(default)]
    #[allow(dead_code)]
    total_duration: Option<u64>,
}

#[derive(Debug, Deserialize)]
struct OllamaListResponse {
    models: Vec<OllamaModelInfo>,
}

#[derive(Debug, Deserialize)]
struct OllamaModelInfo {
    name: String,
    #[allow(dead_code)]
    modified_at: String,
    #[allow(dead_code)]
    size: u64,
}

#[derive(Debug, Serialize)]
struct OllamaGenerateRequest {
    model: String,
    prompt: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    stream: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    options: Option<OllamaOptions>,
}

#[derive(Debug, Serialize)]
struct OllamaOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    num_predict: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    top_p: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    seed: Option<u64>,
}

// ── OllamaClient ─────────────────────────────────────────────────────

/// HTTP client for the ollama API at localhost:11434
#[derive(Debug, Clone)]
pub struct OllamaClient {
    #[allow(dead_code)]
    base_url: String,
    client: reqwest::Client,
    #[allow(dead_code)]
    timeout_secs: u64,
}

impl Default for OllamaClient {
    fn default() -> Self {
        Self::new("http://localhost:11434".to_string(), 30)
    }
}

impl OllamaClient {
    pub fn new(base_url: String, timeout_secs: u64) -> Self {
        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(timeout_secs))
            .build()
            .expect("Failed to build reqwest client");
        Self {
            base_url,
            client,
            timeout_secs,
        }
    }

    /// Generate a response from the given model and prompt.
    /// Returns (response_text, latency_ms).
    pub async fn generate(
        &self,
        model: &str,
        prompt: &str,
        options: Option<GenerateOptions>,
    ) -> Result<(String, u64), OllamaError> {
        let req = OllamaGenerateRequest {
            model: model.to_string(),
            prompt: prompt.to_string(),
            stream: Some(false),
            options: options.map(|o| OllamaOptions {
                temperature: o.temperature,
                num_predict: o.max_tokens,
                top_p: o.top_p,
                seed: o.seed,
            }),
        };

        let start = Instant::now();
        let resp = self
            .client
            .post(format!("{}/api/generate", self.base_url))
            .json(&req)
            .send()
            .await?;

        if resp.status() == reqwest::StatusCode::SERVICE_UNAVAILABLE
            || resp.status() == reqwest::StatusCode::BAD_GATEWAY
        {
            return Err(OllamaError::ServerOffline);
        }

        let body: OllamaGenerateResponse = resp.json().await?;
        let latency = start.elapsed().as_millis() as u64;

        Ok((body.response, latency))
    }

    /// Generate a structured (JSON) response. Expects the model to output valid JSON.
    pub async fn generate_structured<T: serde::de::DeserializeOwned>(
        &self,
        model: &str,
        prompt: &str,
        options: Option<GenerateOptions>,
    ) -> Result<(T, u64), OllamaError> {
        let (text, latency) = self.generate(model, prompt, options).await?;

        // Try to find JSON in the response (models sometimes wrap in markdown)
        let json_str = extract_json(&text)?;

        let parsed: T = serde_json::from_str(&json_str)
            .map_err(|e| OllamaError::Parse(format!("JSON parse error: {} — raw: {}", e, &text[..text.len().min(200)])))?;

        Ok((parsed, latency))
    }

    /// List all models available on the ollama server.
    pub async fn list_models(&self) -> Result<Vec<String>, OllamaError> {
        let resp = self
            .client
            .get(format!("{}/api/tags", self.base_url))
            .send()
            .await?;

        if !resp.status().is_success() {
            return Err(OllamaError::ServerOffline);
        }

        let body: OllamaListResponse = resp.json().await?;
        Ok(body.models.into_iter().map(|m| m.name).collect())
    }

    /// Ping the server to check availability.
    pub async fn is_available(&self) -> bool {
        self.client
            .get(format!("{}/api/tags", self.base_url))
            .send()
            .await
            .map(|r| r.status().is_success())
            .unwrap_or(false)
    }
}

// ── GenerateOptions ──────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct GenerateOptions {
    pub temperature: Option<f64>,
    pub max_tokens: Option<u32>,
    pub top_p: Option<f64>,
    pub seed: Option<u64>,
}

impl Default for GenerateOptions {
    fn default() -> Self {
        Self {
            temperature: Some(0.1),
            max_tokens: Some(128),
            top_p: Some(0.9),
            seed: None,
        }
    }
}

// ── OllamaModelConfig ────────────────────────────────────────────────

/// Configuration for an ollama model used in the PLATO pipeline.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OllamaModelConfig {
    /// The model name on the ollama server (e.g. "liquid-350m", "liquid-1.2b")
    pub model_name: String,
    /// Few-shot prompt template. Placeholders: {sensor_id}, {value}, {unit},
    /// {normal_min}, {normal_max}, {examples}
    pub prompt_template: String,
    /// Confidence threshold — below this, the signal escalates to the next layer
    pub confidence_threshold: f64,
    /// Temperature for generation (lower = more deterministic)
    pub temperature: f64,
    /// Maximum tokens to generate
    pub max_tokens: u32,
}

impl OllamaModelConfig {
    /// Default config for a nano model (350M class).
    pub fn nano_default(model_name: &str) -> Self {
        Self {
            model_name: model_name.to_string(),
            prompt_template: DEFAULT_NANO_TEMPLATE.to_string(),
            confidence_threshold: 0.7,
            temperature: 0.1,
            max_tokens: 64,
        }
    }

    /// Default config for a fleet coordinator (1.2B class).
    pub fn fleet_default(model_name: &str) -> Self {
        Self {
            model_name: model_name.to_string(),
            prompt_template: DEFAULT_FLEET_TEMPLATE.to_string(),
            confidence_threshold: 0.6,
            temperature: 0.2,
            max_tokens: 128,
        }
    }
}

// ── Default Prompt Templates ─────────────────────────────────────────

const DEFAULT_NANO_TEMPLATE: &str = r#"You are a room-level anomaly detection system.
Given a sensor reading, classify it as STATUS or ALERT and output JSON.

Rules:
- Values within normal range → STATUS, high confidence
- Values near the boundary (within 15% of edge) → STATUS, lower confidence (0.5-0.7)
- Values outside normal range → ALERT, moderate confidence (0.4-0.6)
- Extreme values (far outside range) → ALERT, low confidence (0.1-0.3) — escalate

Examples:
{sensor_id}={value}{unit} normal:{normal_min}-{normal_max}

Respond ONLY with valid JSON:
{"classification": "STATUS"|"ALERT", "confidence": 0.0-1.0, "reason": "short explanation"}

Reading: {sensor_id}={value}{unit} (normal range: {normal_min}-{normal_max})"#;

const DEFAULT_FLEET_TEMPLATE: &str = r#"You are a cross-room fleet coordinator.
Several rooms reported readings at the same time. Determine if they're related.

Readings:
{readings}

Respond ONLY with valid JSON:
{"related": true|false, "root_cause": "description or null", "coordination_tile": "action needed", "confidence": 0.0-1.0}"#;

// ── Helper: Extract JSON from model output ───────────────────────────

fn extract_json(text: &str) -> Result<String, OllamaError> {
    let trimmed = text.trim();

    // Try direct parse
    if trimmed.starts_with('{') {
        return Ok(trimmed.to_string());
    }

    // Try to find a JSON block if wrapped in markdown ```json ... ```
    if let Some(start) = trimmed.find("{") {
        if let Some(end) = trimmed.rfind("}") {
            return Ok(trimmed[start..=end].to_string());
        }
    }

    Err(OllamaError::Parse(format!(
        "No JSON object found in response: {}",
        &text[..text.len().min(150)]
    )))
}

// ── RealNanoModel ────────────────────────────────────────────────────

/// Wraps `OllamaClient` with the nano model config to replace the simulated
/// `NanoModel`. Reads a sensor reading, builds a prompt, calls ollama,
/// and parses the result into a (Tile, confidence) tuple.
#[derive(Debug, Clone)]
pub struct RealNanoModel {
    pub client: OllamaClient,
    pub config: OllamaModelConfig,
    pub tiles_produced: usize,
    pub avg_confidence: f64,
    pub avg_latency_ms: f64,
}

impl RealNanoModel {
    pub fn new(client: OllamaClient, config: OllamaModelConfig) -> Self {
        Self {
            client,
            config,
            tiles_produced: 0,
            avg_confidence: 0.5,
            avg_latency_ms: 0.0,
        }
    }

    /// Format the few-shots template with the reading's values.
    fn format_prompt(&self, reading: &SensorReading, examples: &[NanoExample]) -> String {
        let examples_str: String = examples
            .iter()
            .map(|ex| {
                format!(
                    "Input: {}={:.1}{} normal:{:.1}-{:.1}{} (confidence {:.2})",
                    ex.sensor_id, ex.value, ex.unit,
                    ex.normal_min, ex.normal_max,
                    ex.classification, ex.confidence,
                )
            })
            .collect::<Vec<_>>()
            .join("\n");

        self.config
            .prompt_template
            .replace("{sensor_id}", &reading.sensor_id)
            .replace("{value}", &format!("{:.1}", reading.value))
            .replace("{unit}", &reading.unit)
            .replace("{normal_min}", &format!("{:.1}", reading.normal_min))
            .replace("{normal_max}", &format!("{:.1}", reading.normal_max))
            .replace("{examples}", &examples_str)
    }

    /// Infer from a sensor reading. Returns None if confidence is below threshold
    /// (meaning the signal should escalate to the next layer).
    pub async fn infer(&mut self, reading: &SensorReading) -> Option<(Tile, f64)> {
        // Build examples for few-shot
        let examples = default_examples();
        let prompt = self.format_prompt(reading, &examples);

        let options = GenerateOptions {
            temperature: Some(self.config.temperature),
            max_tokens: Some(self.config.max_tokens),
            ..Default::default()
        };

        let (response, latency) = self
            .client
            .generate(&self.config.model_name, &prompt, Some(options))
            .await
            .ok()?;

        // Parse the JSON response
        let parsed: NanoResponse = parse_nano_response(&response).ok()?;

        if parsed.confidence >= self.config.confidence_threshold {
            self.tiles_produced += 1;
            let alpha = 1.0 / self.tiles_produced as f64;
            self.avg_confidence =
                self.avg_confidence * (1.0 - alpha) + parsed.confidence * alpha;
            self.avg_latency_ms =
                self.avg_latency_ms * (1.0 - alpha) + latency as f64 * alpha;

            let tile_type = match parsed.classification.to_uppercase().as_str() {
                "ALERT" => TileType::Alert,
                "PREDICTION" => TileType::Prediction,
                _ => TileType::Status,
            };

            Some((
                Tile {
                    id: Uuid::new_v4(),
                    room_id: reading.room_id.clone(),
                    tile_type,
                    content: parsed.reason,
                    confidence: parsed.confidence,
                    resolved_by: ResolutionLayer::NanoModel,
                    timestamp_ms: reading.timestamp_ms,
                    sensor_reading: Some(reading.clone()),
                },
                parsed.confidence,
            ))
        } else {
            None
        }
    }
}

/// The expected JSON structure from the nano model.
#[derive(Debug, Deserialize)]
struct NanoResponse {
    classification: String,
    confidence: f64,
    reason: String,
}

fn parse_nano_response(text: &str) -> Result<NanoResponse, OllamaError> {
    let json_str = extract_json(text)?;
    serde_json::from_str(&json_str)
        .map_err(|e| OllamaError::Parse(format!("NanoResponse parse: {} — raw: {}", e, &json_str)))
}

/// Default few-shot examples for the nano model.
struct NanoExample {
    sensor_id: String,
    value: f64,
    unit: String,
    normal_min: f64,
    normal_max: f64,
    classification: String,
    confidence: f64,
}

fn default_examples() -> Vec<NanoExample> {
    vec![
        NanoExample {
            sensor_id: "temp".into(),
            value: 22.0,
            unit: "C".into(),
            normal_min: 15.0,
            normal_max: 30.0,
            classification: "STATUS".into(),
            confidence: 0.95,
        },
        NanoExample {
            sensor_id: "temp".into(),
            value: 29.5,
            unit: "C".into(),
            normal_min: 15.0,
            normal_max: 30.0,
            classification: "STATUS".into(),
            confidence: 0.65,
        },
        NanoExample {
            sensor_id: "rpm".into(),
            value: 3200.0,
            unit: "rpm".into(),
            normal_min: 1000.0,
            normal_max: 3000.0,
            classification: "ALERT".into(),
            confidence: 0.55,
        },
        NanoExample {
            sensor_id: "oil_pressure".into(),
            value: 15.0,
            unit: "psi".into(),
            normal_min: 30.0,
            normal_max: 80.0,
            classification: "ALERT".into(),
            confidence: 0.25,
        },
    ]
}

// ── FleetCoordinatorResponse ─────────────────────────────────────────

#[derive(Debug, Deserialize)]
struct FleetCoordinatorResponse {
    #[allow(dead_code)]
    related: bool,
    root_cause: Option<String>,
    coordination_tile: Option<String>,
    confidence: f64,
}

// ── RealFleetModel ────────────────────────────────────────────────────

/// Wraps `OllamaClient` with the fleet model config for cross-room coordination.
/// Takes multiple sensor readings and determines if they're causally related.
#[derive(Debug, Clone)]
pub struct RealFleetModel {
    pub client: OllamaClient,
    pub config: OllamaModelConfig,
    pub coordination_count: usize,
}

impl RealFleetModel {
    pub fn new(client: OllamaClient, config: OllamaModelConfig) -> Self {
        Self {
            client,
            config,
            coordination_count: 0,
        }
    }

    /// Format the readings block for the fleet template.
    fn format_readings(&self, readings: &[SensorReading]) -> String {
        readings
            .iter()
            .map(|r| {
                format!(
                    "[{}] {}={:.1}{} (range: {:.1}-{:.1}, room: {})",
                    r.timestamp_ms, r.sensor_id, r.value, r.unit,
                    r.normal_min, r.normal_max, r.room_id,
                )
            })
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// Analyze several sensor readings from different rooms and determine
    /// if they're related. If so, returns a coordination tile.
    pub async fn analyze(
        &mut self,
        readings: &[SensorReading],
    ) -> Option<(Tile, f64)> {
        if readings.is_empty() {
            return None;
        }

        let readings_str = self.format_readings(readings);
        let prompt = self
            .config
            .prompt_template
            .replace("{readings}", &readings_str);

        let options = GenerateOptions {
            temperature: Some(self.config.temperature),
            max_tokens: Some(self.config.max_tokens),
            ..Default::default()
        };

        let (response, _latency) = self
            .client
            .generate(&self.config.model_name, &prompt, Some(options))
            .await
            .ok()?;

        let parsed: FleetCoordinatorResponse = {
            let json_str = extract_json(&response).ok()?;
            serde_json::from_str(&json_str).ok()?
        };

        if parsed.confidence < self.config.confidence_threshold {
            return None;
        }

        // Combine the room IDs into a single room string
        let room_ids: Vec<&str> = readings.iter().map(|r| r.room_id.as_str()).collect();
        let room_id = room_ids.join("+");
        let content = parsed
            .coordination_tile
            .unwrap_or_else(|| parsed.root_cause.clone().unwrap_or_default());

        self.coordination_count += 1;

        Some((
            Tile {
                id: Uuid::new_v4(),
                room_id,
                tile_type: TileType::Coordination,
                content,
                confidence: parsed.confidence,
                resolved_by: ResolutionLayer::FleetCoord,
                timestamp_ms: readings
                    .iter()
                    .map(|r| r.timestamp_ms)
                    .max()
                    .unwrap_or(0),
                sensor_reading: None,
            },
            parsed.confidence,
        ))
    }
}

// ── Tests ─────────────────────────────────────────────────────────────

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

    fn test_reading(sensor_id: &str, value: f64, min: f64, max: f64) -> SensorReading {
        SensorReading {
            sensor_id: sensor_id.to_string(),
            room_id: "engine-room".to_string(),
            value,
            unit: "units".to_string(),
            timestamp_ms: 1000,
            normal_min: min,
            normal_max: max,
        }
    }

    // ── Prompt Formatting Tests ────────────────────────────────────

    #[test]
    fn test_nano_prompt_format_contains_placeholders() {
        let config = OllamaModelConfig::nano_default("liquid-350m");
        let client = OllamaClient::default();
        let model = RealNanoModel::new(client, config);

        let reading = test_reading("temp", 25.0, 15.0, 30.0);
        let examples = default_examples();
        let prompt = model.format_prompt(&reading, &examples);

        // Should contain the sensor values
        assert!(prompt.contains("temp"), "prompt should contain sensor_id");
        assert!(prompt.contains("25.0"), "prompt should contain value");
        assert!(prompt.contains("15.0"), "prompt should contain normal_min");
        assert!(prompt.contains("30.0"), "prompt should contain normal_max");

        // Should contain few-shot examples
        assert!(prompt.contains("STATUS"), "prompt should contain examples");
        assert!(prompt.contains("ALERT"), "prompt should contain examples");
    }

    #[test]
    fn test_fleet_prompt_format() {
        let config = OllamaModelConfig::fleet_default("liquid-1.2b");
        let client = OllamaClient::default();
        let model = RealFleetModel::new(client, config);

        let readings = vec![
            test_reading("temp", 45.0, 15.0, 30.0),
            test_reading("rpm", 5000.0, 1000.0, 3000.0),
        ];
        let formatted = model.format_readings(&readings);

        assert!(formatted.contains("engine-room"), "should contain room ids");
        assert!(formatted.contains("45.0"), "should contain values");
        assert!(formatted.contains("5000.0"), "should contain values");
    }

    // ── Response Parsing Tests ─────────────────────────────────────

    #[test]
    fn test_parse_direct_json() {
        let response = r#"{"classification": "STATUS", "confidence": 0.95, "reason": "within normal range"}"#;
        let parsed = parse_nano_response(response).unwrap();
        assert_eq!(parsed.classification, "STATUS");
        assert!((parsed.confidence - 0.95).abs() < 0.01);
        assert_eq!(parsed.reason, "within normal range");
    }

    #[test]
    fn test_parse_json_in_markdown() {
        let response = r#"Here is the JSON:
```json
{"classification": "ALERT", "confidence": 0.45, "reason": "temperature too high"}
```
"#;
        let parsed = parse_nano_response(response).unwrap();
        assert_eq!(parsed.classification, "ALERT");
        assert!((parsed.confidence - 0.45).abs() < 0.01);
    }

    #[test]
    fn test_parse_json_with_extra_text() {
        let response = r#"I think the classification is ALERT. {"classification": "ALERT", "confidence": 0.55, "reason": "boundary exceeded"}"#;
        let parsed = parse_nano_response(response).unwrap();
        assert_eq!(parsed.classification, "ALERT");
    }

    #[test]
    fn test_parse_invalid_json_returns_error() {
        let response = "no JSON here at all";
        let parsed = parse_nano_response(response);
        assert!(parsed.is_err());
    }

    #[test]
    fn test_extract_json_direct() {
        let json = r#"{"a": 1}"#;
        let result = extract_json(json).unwrap();
        assert_eq!(result, r#"{"a": 1}"#);
    }

    #[test]
    fn test_extract_json_from_markdown_block() {
        let text = "```json\n{\"a\": 1}\n```";
        let result = extract_json(text).unwrap();
        assert_eq!(result, r#"{"a": 1}"#);
    }

    #[test]
    fn test_extract_json_surrounded_by_text() {
        let text = "Response: {\"a\": 1} -- done";
        let result = extract_json(text).unwrap();
        assert_eq!(result, r#"{"a": 1}"#);
    }

    // ── Full RealNanoModel Pipeline (mocked HTTP) ──────────────────

    /// A mock HTTP server that returns canned responses for testing.
    /// We use a lightweight approach: start a tiny HTTP server on a random port.
    fn start_mock_ollama() -> (String, std::sync::Arc<std::sync::atomic::AtomicBool>) {
        use std::sync::atomic::AtomicBool;
        use std::sync::Arc;

        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        let base = format!("http://127.0.0.1:{}", port);
        let shutdown = Arc::new(AtomicBool::new(false));
        let shutdown_clone = shutdown.clone();

        std::thread::spawn(move || {
            listener.set_nonblocking(true).unwrap();
            for stream in listener.incoming() {
                if shutdown_clone.load(std::sync::atomic::Ordering::Relaxed) {
                    break;
                }
                if let Ok(mut stream) = stream {
                    use std::io::{Read, Write};
                    let mut buf = [0u8; 8192];
                    if stream.read(&mut buf).is_ok() {
                        // Return a mock JSON response
                        let body = r#"{"response": "{\"classification\": \"STATUS\", \"confidence\": 0.92, \"reason\": \"within normal range\"}", "done": true, "eval_duration": 50000000, "total_duration": 100000000}"#;
                        let response = format!(
                            "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                            body.len(),
                            body
                        );
                        let _ = stream.write_all(response.as_bytes());
                        let _ = stream.flush();
                    }
                }
            }
        });

        (base, shutdown)
    }

    #[tokio::test]
    async fn test_real_nano_model_with_mocked_ollama() {
        let (base_url, shutdown) = start_mock_ollama();

        let client = OllamaClient::new(base_url, 5);
        let config = OllamaModelConfig::nano_default("test-model");
        let mut model = RealNanoModel::new(client, config);

        let reading = test_reading("temp", 22.0, 15.0, 30.0);
        let result = model.infer(&reading).await;

        // Shut down the mock server
        shutdown.store(true, std::sync::atomic::Ordering::Relaxed);

        assert!(result.is_some(), "Expected a tile from mocked ollama");
        let (tile, confidence) = result.unwrap();
        assert_eq!(tile.tile_type, TileType::Status);
        assert!(confidence >= 0.7);
        assert_eq!(model.tiles_produced, 1);
    }

    #[tokio::test]
    async fn test_real_nano_model_low_confidence_returns_none() {
        // Mock returning a low-confidence response
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        let base = format!("http://127.0.0.1:{}", port);
        let shutdown = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        let s = shutdown.clone();

        std::thread::spawn(move || {
            listener.set_nonblocking(true).unwrap();
            for stream in listener.incoming() {
                if s.load(std::sync::atomic::Ordering::Relaxed) {
                    break;
                }
                if let Ok(mut stream) = stream {
                    use std::io::{Read, Write};
                    let mut buf = [0u8; 8192];
                    if stream.read(&mut buf).is_ok() {
                        // Low confidence response (< 0.7 threshold)
                        let body = r#"{"response": "{\"classification\": \"ALERT\", \"confidence\": 0.35, \"reason\": \"outside normal range\"}", "done": true, "eval_duration": 40000000, "total_duration": 90000000}"#;
                        let response = format!(
                            "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                            body.len(),
                            body
                        );
                        let _ = stream.write_all(response.as_bytes());
                        let _ = stream.flush();
                    }
                }
            }
        });

        let client = OllamaClient::new(base, 5);
        let mut config = OllamaModelConfig::nano_default("test-model");
        config.confidence_threshold = 0.7; // Ensure threshold > 0.35
        let mut model = RealNanoModel::new(client, config);

        let reading = test_reading("temp", 35.0, 15.0, 30.0);
        let result = model.infer(&reading).await;

        shutdown.store(true, std::sync::atomic::Ordering::Relaxed);

        assert!(result.is_none(), "Low confidence should return None (escalate)");
        assert_eq!(model.tiles_produced, 0);
    }

    #[tokio::test]
    async fn test_real_fleet_model_with_mocked_ollama() {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        let base = format!("http://127.0.0.1:{}", port);
        let shutdown = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        let s = shutdown.clone();

        std::thread::spawn(move || {
            listener.set_nonblocking(true).unwrap();
            for stream in listener.incoming() {
                if s.load(std::sync::atomic::Ordering::Relaxed) {
                    break;
                }
                if let Ok(mut stream) = stream {
                    use std::io::{Read, Write};
                    let mut buf = [0u8; 8192];
                    if stream.read(&mut buf).is_ok() {
                        let body = r#"{"response": "{\"related\": true, \"root_cause\": \"coolant pump failure in engine room\", \"coordination_tile\": \"Check coolant in all connected rooms\", \"confidence\": 0.85}", "done": true, "eval_duration": 120000000, "total_duration": 200000000}"#;
                        let response = format!(
                            "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                            body.len(),
                            body
                        );
                        let _ = stream.write_all(response.as_bytes());
                        let _ = stream.flush();
                    }
                }
            }
        });

        let client = OllamaClient::new(base, 5);
        let config = OllamaModelConfig::fleet_default("test-fleet");
        let mut model = RealFleetModel::new(client, config);

        let readings = vec![
            test_reading("coolant", 95.0, 60.0, 90.0),
            test_reading("rpm", 4500.0, 1000.0, 3000.0),
        ];
        let result = model.analyze(&readings).await;

        shutdown.store(true, std::sync::atomic::Ordering::Relaxed);

        assert!(result.is_some(), "Expected a coordination tile");
        let (tile, confidence) = result.unwrap();
        assert_eq!(tile.tile_type, TileType::Coordination);
        assert!(confidence >= 0.6);
        assert_eq!(model.coordination_count, 1);
    }

    // ── OllamaClient Tests (no actual HTTP) ────────────────────────

    #[test]
    fn test_ollama_client_default() {
        let client = OllamaClient::default();
        // Should point to localhost:11434
        assert!(client.base_url.contains("localhost:11434"));
    }

    #[test]
    fn test_ollama_client_custom_url() {
        let client = OllamaClient::new("http://ollama.local:8080".to_string(), 60);
        assert_eq!(client.base_url, "http://ollama.local:8080");
        assert_eq!(client.timeout_secs, 60);
    }

    #[tokio::test]
    async fn test_ollama_client_server_offline() {
        // Connect to a port that's definitely not running
        let client = OllamaClient::new("http://127.0.0.1:19999".to_string(), 2);
        let result = client.generate("test-model", "ping", None).await;
        assert!(result.is_err(), "Should error when server is offline");
    }

    #[tokio::test]
    async fn test_list_models_server_offline() {
        let client = OllamaClient::new("http://127.0.0.1:19998".to_string(), 2);
        let result = client.list_models().await;
        assert!(result.is_err());
    }

    // ── OllamaModelConfig Tests ────────────────────────────────────

    #[test]
    fn test_nano_config_uses_correct_model() {
        let config = OllamaModelConfig::nano_default("liquid-350m");
        assert_eq!(config.model_name, "liquid-350m");
        assert!((config.confidence_threshold - 0.7).abs() < 0.01);
        assert_eq!(config.max_tokens, 64);
    }

    #[test]
    fn test_fleet_config_uses_correct_model() {
        let config = OllamaModelConfig::fleet_default("liquid-1.2b");
        assert_eq!(config.model_name, "liquid-1.2b");
        assert!((config.confidence_threshold - 0.6).abs() < 0.01);
        assert_eq!(config.max_tokens, 128);
    }

    // ── Error Display Tests ────────────────────────────────────────

    #[test]
    fn test_ollama_error_display() {
        let err = OllamaError::Parse("bad json".to_string());
        assert!(err.to_string().contains("bad json"));

        let err = OllamaError::Timeout;
        assert_eq!(err.to_string(), "Request timed out");

        let err = OllamaError::ModelNotAvailable("foo".to_string());
        assert!(err.to_string().contains("foo"));
    }
}