nika 0.20.0

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
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
//! Output Handling - task result processing (v0.2)
//!
//! Extracted from runner.rs for cleaner separation:
//! - `make_task_result`: Convert raw output to TaskResult with format handling
//! - `validate_schema`: Validate JSON output against JSON Schema (with caching)
//! - `validate_schema_ref`: Validate against SchemaRef (inline or file)
//! - `extract_json_from_output`: Extract JSON from markdown code blocks (v0.7.2)
//! - `format_validation_errors`: Format errors for retry feedback

use std::sync::{Arc, LazyLock};

use dashmap::DashMap;
use serde_json::Value;

use crate::ast::output::SchemaRef;
use crate::ast::OutputFormat;
use crate::error::NikaError;
use crate::store::TaskResult;

/// Global schema cache: path → parsed JSON schema
/// Avoids re-reading and re-parsing schema files on repeated validations.
static SCHEMA_CACHE: LazyLock<DashMap<Arc<str>, Arc<Value>>> = LazyLock::new(DashMap::new);

/// Extract JSON from LLM output, handling markdown code blocks.
///
/// LLMs often wrap JSON in markdown code blocks like:
/// ```json
/// {"key": "value"}
/// ```
///
/// This function tries multiple strategies:
/// 1. Direct JSON parsing (fast path)
/// 2. Extract from ```json ... ``` blocks
/// 3. Extract from ``` ... ``` blocks (no language)
/// 4. Find outermost { } or [ ] brackets
fn extract_json_from_output(output: &str) -> Result<Value, String> {
    let trimmed = output.trim();

    // Strategy 1: Direct parse (fast path for well-behaved LLMs)
    if let Ok(v) = serde_json::from_str::<Value>(trimmed) {
        return Ok(v);
    }

    // Strategy 2: Extract from ```json ... ``` blocks
    if let Some(start) = trimmed.find("```json") {
        let after_marker = &trimmed[start + 7..];
        if let Some(end) = after_marker.find("```") {
            let json_str = after_marker[..end].trim();
            if let Ok(v) = serde_json::from_str::<Value>(json_str) {
                return Ok(v);
            }
        }
    }

    // Strategy 3: Extract from ``` ... ``` blocks (no language specifier)
    if let Some(start) = trimmed.find("```\n") {
        let after_marker = &trimmed[start + 4..];
        if let Some(end) = after_marker.find("```") {
            let json_str = after_marker[..end].trim();
            if let Ok(v) = serde_json::from_str::<Value>(json_str) {
                return Ok(v);
            }
        }
    }

    // Strategy 4: Find outermost { } or [ ] brackets
    let first_brace = trimmed.find('{');
    let first_bracket = trimmed.find('[');

    let (start_char, end_char, start_pos) = match (first_brace, first_bracket) {
        (Some(b), Some(k)) if b < k => ('{', '}', b),
        (Some(_), Some(k)) => ('[', ']', k),
        (Some(b), None) => ('{', '}', b),
        (None, Some(k)) => ('[', ']', k),
        (None, None) => return Err("No JSON object or array found in output".to_string()),
    };

    // Find matching closing bracket (handle nesting)
    let substr = &trimmed[start_pos..];
    let mut depth = 0;
    let mut end_pos = None;

    for (i, c) in substr.char_indices() {
        if c == start_char {
            depth += 1;
        } else if c == end_char {
            depth -= 1;
            if depth == 0 {
                end_pos = Some(i + 1);
                break;
            }
        }
    }

    if let Some(end) = end_pos {
        let json_str = &substr[..end];
        if let Ok(v) = serde_json::from_str::<Value>(json_str) {
            return Ok(v);
        }
    }

    // All strategies failed - return original error
    Err(format!(
        "Failed to extract JSON from output. First 200 chars: {}",
        &trimmed[..trimmed.len().min(200)]
    ))
}

/// Convert execution output to TaskResult, parsing as JSON if output format is json.
/// Also validates against schema if declared.
///
/// v0.12.1: Empty output with JSON format returns `null` instead of failing.
/// This enables graceful handling of commands that produce no output.
pub async fn make_task_result(
    output: String,
    policy: Option<&crate::ast::OutputPolicy>,
    duration: std::time::Duration,
) -> TaskResult {
    if let Some(policy) = policy {
        if policy.format == OutputFormat::Json {
            // v0.12.1: Handle empty output gracefully - return null instead of error
            if output.trim().is_empty() {
                tracing::debug!(
                    target: "nika::output",
                    "Empty output with JSON format, returning null"
                );
                return TaskResult::success(Value::Null, duration);
            }

            // Parse as JSON, handling markdown code blocks
            let json_value = match extract_json_from_output(&output) {
                Ok(v) => v,
                Err(e) => {
                    return TaskResult::failed(
                        format!("NIKA-060: Invalid JSON output: {}", e),
                        duration,
                    );
                }
            };

            // Validate against schema if declared
            if let Some(schema_ref) = &policy.schema {
                if let Err(e) = validate_schema_ref(&json_value, schema_ref).await {
                    return TaskResult::failed(e.to_string(), duration);
                }
            }

            return TaskResult::success(json_value, duration);
        }
    }
    TaskResult::success_str(output, duration)
}

/// Validate JSON value against a JSON Schema file (with caching)
///
/// Schema files are cached after first load to avoid repeated file I/O.
pub async fn validate_schema(value: &Value, schema_path: &str) -> Result<(), NikaError> {
    // Try cache first (fast path)
    let schema = if let Some(cached) = SCHEMA_CACHE.get(schema_path) {
        Arc::clone(cached.value())
    } else {
        // Cache miss: read and parse schema
        let schema_str =
            tokio::fs::read_to_string(schema_path)
                .await
                .map_err(|e| NikaError::SchemaFailed {
                    details: format!("Failed to read schema '{}': {}", schema_path, e),
                })?;

        let schema: Value =
            serde_json::from_str(&schema_str).map_err(|e| NikaError::SchemaFailed {
                details: format!("Invalid JSON in schema '{}': {}", schema_path, e),
            })?;

        // Store in cache
        let schema = Arc::new(schema);
        SCHEMA_CACHE.insert(Arc::from(schema_path), Arc::clone(&schema));
        schema
    };

    // Compile and validate (compilation is fast, validation needs fresh instance)
    let compiled = jsonschema::validator_for(&schema).map_err(|e| NikaError::SchemaFailed {
        details: format!("Invalid schema '{}': {}", schema_path, e),
    })?;

    // Collect all validation errors
    let errors: Vec<_> = compiled.iter_errors(value).collect();
    if errors.is_empty() {
        Ok(())
    } else {
        let error_msgs: Vec<String> = errors.iter().map(|e| e.to_string()).collect();
        Err(NikaError::SchemaFailed {
            details: error_msgs.join("; "),
        })
    }
}

/// Validate a JSON value against a SchemaRef (inline or file)
pub async fn validate_schema_ref(value: &Value, schema_ref: &SchemaRef) -> Result<(), NikaError> {
    match schema_ref {
        SchemaRef::File(path) => validate_schema(value, path).await,
        SchemaRef::Inline(schema) => validate_inline_schema(value, schema),
    }
}

/// Validate against an inline JSON Schema
pub fn validate_inline_schema(value: &Value, schema: &Value) -> Result<(), NikaError> {
    let compiled = jsonschema::validator_for(schema).map_err(|e| NikaError::SchemaFailed {
        details: format!("Invalid inline schema: {e}"),
    })?;

    let errors: Vec<_> = compiled.iter_errors(value).collect();
    if errors.is_empty() {
        Ok(())
    } else {
        let error_msgs: Vec<String> = errors
            .iter()
            .map(|e| format!("- {}: {}", e.instance_path, e))
            .collect();
        Err(NikaError::SchemaFailed {
            details: format!("Output validation failed:\n{}", error_msgs.join("\n")),
        })
    }
}

/// Format validation errors for retry feedback to LLM
pub fn format_validation_errors(value: &Value, schema: &Value) -> String {
    let compiled = match jsonschema::validator_for(schema) {
        Ok(c) => c,
        Err(e) => return format!("Invalid schema: {e}"),
    };

    let errors: Vec<_> = compiled.iter_errors(value).collect();
    if errors.is_empty() {
        return "No validation errors".to_string();
    }

    errors
        .iter()
        .map(|e| {
            format!(
                "- Path '{}': {} (got: {})",
                e.instance_path,
                e,
                serde_json::to_string(&*e.instance).unwrap_or_default()
            )
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// Extract JSON from LLM output - public version for executor retry loop
pub fn extract_json(output: &str) -> Result<Value, String> {
    extract_json_from_output(output)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use std::time::Duration;
    use tempfile::NamedTempFile;

    #[tokio::test]
    async fn schema_cache_works() {
        // Create a temp schema file
        let mut schema_file = NamedTempFile::new().unwrap();
        writeln!(
            schema_file,
            r#"{{"type": "object", "properties": {{"name": {{"type": "string"}}}}}}"#
        )
        .unwrap();
        let schema_path = schema_file.path().to_str().unwrap();

        // First validation - cache miss
        let value = serde_json::json!({"name": "test"});
        assert!(validate_schema(&value, schema_path).await.is_ok());

        // Second validation - cache hit (same path)
        assert!(validate_schema(&value, schema_path).await.is_ok());

        // Cache should have the entry
        assert!(SCHEMA_CACHE.contains_key(schema_path));
    }

    #[tokio::test]
    async fn schema_validation_rejects_invalid() {
        let mut schema_file = NamedTempFile::new().unwrap();
        writeln!(schema_file, r#"{{"type": "object", "properties": {{"age": {{"type": "number"}}}}, "required": ["age"]}}"#).unwrap();
        let schema_path = schema_file.path().to_str().unwrap();

        // Missing required field
        let value = serde_json::json!({"name": "test"});
        assert!(validate_schema(&value, schema_path).await.is_err());

        // Correct value
        let value = serde_json::json!({"age": 25});
        assert!(validate_schema(&value, schema_path).await.is_ok());
    }

    #[tokio::test]
    async fn make_task_result_validates_json_file_schema() {
        use crate::ast::OutputPolicy;

        let mut schema_file = NamedTempFile::new().unwrap();
        writeln!(schema_file, r#"{{"type": "object"}}"#).unwrap();
        let schema_path = schema_file.path().to_string_lossy().to_string();

        let policy = OutputPolicy {
            format: OutputFormat::Json,
            schema: Some(SchemaRef::File(schema_path)),
            max_retries: None,
        };

        // Valid JSON object
        let result = make_task_result(
            r#"{"key": "value"}"#.to_string(),
            Some(&policy),
            Duration::from_millis(100),
        )
        .await;
        assert!(result.is_success());

        // Invalid JSON
        let result = make_task_result(
            "not json".to_string(),
            Some(&policy),
            Duration::from_millis(100),
        )
        .await;
        assert!(!result.is_success());
    }

    #[tokio::test]
    async fn make_task_result_validates_json_inline_schema() {
        use crate::ast::OutputPolicy;

        let inline_schema = serde_json::json!({
            "type": "object",
            "properties": {
                "name": { "type": "string" }
            },
            "required": ["name"]
        });

        let policy = OutputPolicy {
            format: OutputFormat::Json,
            schema: Some(SchemaRef::Inline(inline_schema)),
            max_retries: None,
        };

        // Valid JSON with required field
        let result = make_task_result(
            r#"{"name": "test"}"#.to_string(),
            Some(&policy),
            Duration::from_millis(100),
        )
        .await;
        assert!(result.is_success());

        // Invalid JSON missing required field
        let result = make_task_result(
            r#"{"other": "value"}"#.to_string(),
            Some(&policy),
            Duration::from_millis(100),
        )
        .await;
        assert!(!result.is_success());
    }

    // ══════════════════════════════════════════════════════════════
    // make_task_result EDGE CASES
    // ══════════════════════════════════════════════════════════════

    #[tokio::test]
    async fn make_task_result_no_policy_returns_text() {
        let result = make_task_result(
            "plain text output".to_string(),
            None,
            Duration::from_millis(50),
        )
        .await;

        assert!(result.is_success());
        // Without policy, output should be stored as string (success_str)
        assert_eq!(
            result.output.as_ref(),
            &serde_json::Value::String("plain text output".to_string())
        );
    }

    #[tokio::test]
    async fn make_task_result_json_no_schema_parses_json() {
        use crate::ast::OutputPolicy;

        let policy = OutputPolicy {
            format: OutputFormat::Json,
            schema: None, // No schema validation
            max_retries: None,
        };

        let result = make_task_result(
            r#"{"key": "value", "nested": {"a": 1}}"#.to_string(),
            Some(&policy),
            Duration::from_millis(50),
        )
        .await;

        assert!(result.is_success());
        // Should be parsed as JSON object, not string
        assert!(result.output.is_object());
        assert_eq!(result.output["key"], "value");
        assert_eq!(result.output["nested"]["a"], 1);
    }

    #[tokio::test]
    async fn make_task_result_invalid_json_returns_error_with_code() {
        use crate::ast::OutputPolicy;

        let policy = OutputPolicy {
            format: OutputFormat::Json,
            schema: None,
            max_retries: None,
        };

        let result = make_task_result(
            "{ invalid json".to_string(),
            Some(&policy),
            Duration::from_millis(50),
        )
        .await;

        assert!(!result.is_success());
        // Error should contain NIKA-060 code
        let error_msg = result.error().expect("Should have error");
        assert!(
            error_msg.contains("NIKA-060"),
            "Error should contain NIKA-060 code: {}",
            error_msg
        );
    }

    #[tokio::test]
    async fn make_task_result_text_format_returns_raw_string() {
        use crate::ast::OutputPolicy;

        let policy = OutputPolicy {
            format: OutputFormat::Text,
            schema: None,
            max_retries: None,
        };

        // Even valid JSON should be treated as text
        let result = make_task_result(
            r#"{"key": "value"}"#.to_string(),
            Some(&policy),
            Duration::from_millis(50),
        )
        .await;

        assert!(result.is_success());
        // Should be stored as string, not parsed JSON
        assert!(result.output.is_string());
        assert_eq!(
            result.output.as_ref(),
            &serde_json::Value::String(r#"{"key": "value"}"#.to_string())
        );
    }

    // ══════════════════════════════════════════════════════════════
    // validate_schema ERROR PATHS
    // ══════════════════════════════════════════════════════════════

    #[tokio::test]
    async fn validate_schema_file_not_found_returns_error() {
        let value = serde_json::json!({"name": "test"});
        let result = validate_schema(&value, "/nonexistent/path/schema.json").await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_string = err.to_string();
        assert!(
            err_string.contains("Failed to read schema"),
            "Error should mention file read failure: {}",
            err_string
        );
    }

    #[tokio::test]
    async fn validate_schema_invalid_json_in_schema_file() {
        let mut schema_file = NamedTempFile::new().unwrap();
        writeln!(schema_file, "{{ not valid json").unwrap();
        let schema_path = schema_file.path().to_str().unwrap();

        let value = serde_json::json!({"name": "test"});
        let result = validate_schema(&value, schema_path).await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_string = err.to_string();
        assert!(
            err_string.contains("Invalid JSON in schema"),
            "Error should mention invalid JSON: {}",
            err_string
        );
    }

    #[tokio::test]
    async fn validate_schema_invalid_schema_structure() {
        let mut schema_file = NamedTempFile::new().unwrap();
        // Valid JSON but not a valid JSON Schema (type must be a string, not number)
        writeln!(schema_file, r#"{{"type": 123}}"#).unwrap();
        let schema_path = schema_file.path().to_str().unwrap();

        let value = serde_json::json!({"name": "test"});
        let result = validate_schema(&value, schema_path).await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_string = err.to_string();
        assert!(
            err_string.contains("Invalid schema"),
            "Error should mention invalid schema: {}",
            err_string
        );
    }

    #[tokio::test]
    async fn validate_schema_multiple_validation_errors() {
        let mut schema_file = NamedTempFile::new().unwrap();
        writeln!(
            schema_file,
            r#"{{
                "type": "object",
                "properties": {{
                    "name": {{"type": "string"}},
                    "age": {{"type": "number"}}
                }},
                "required": ["name", "age"]
            }}"#
        )
        .unwrap();
        let schema_path = schema_file.path().to_str().unwrap();

        // Missing both required fields
        let value = serde_json::json!({});
        let result = validate_schema(&value, schema_path).await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_string = err.to_string();
        // Should mention both missing fields
        assert!(
            err_string.contains("name") || err_string.contains("required"),
            "Error should mention validation issues: {}",
            err_string
        );
    }

    // ══════════════════════════════════════════════════════════════
    // EDGE CASES
    // ══════════════════════════════════════════════════════════════

    #[tokio::test]
    async fn make_task_result_large_json_output() {
        use crate::ast::OutputPolicy;

        let policy = OutputPolicy {
            format: OutputFormat::Json,
            schema: None,
            max_retries: None,
        };

        // Generate large JSON array
        let large_array: Vec<i32> = (0..10000).collect();
        let json_str = serde_json::to_string(&large_array).unwrap();

        let result = make_task_result(json_str, Some(&policy), Duration::from_millis(100)).await;

        assert!(result.is_success());
        assert!(result.output.is_array());
        assert_eq!(result.output.as_array().unwrap().len(), 10000);
    }

    #[tokio::test]
    async fn make_task_result_unicode_content() {
        use crate::ast::OutputPolicy;

        let policy = OutputPolicy {
            format: OutputFormat::Json,
            schema: None,
            max_retries: None,
        };

        // JSON with various Unicode characters
        let json_str = r#"{"greeting": "你好世界", "emoji": "🚀✨", "japanese": "こんにちは"}"#;

        let result = make_task_result(
            json_str.to_string(),
            Some(&policy),
            Duration::from_millis(50),
        )
        .await;

        assert!(result.is_success());
        assert_eq!(result.output["greeting"], "你好世界");
        assert_eq!(result.output["emoji"], "🚀✨");
        assert_eq!(result.output["japanese"], "こんにちは");
    }

    #[tokio::test]
    async fn schema_cache_concurrent_access() {
        // Create a temp schema file
        let mut schema_file = NamedTempFile::new().unwrap();
        writeln!(schema_file, r#"{{"type": "object"}}"#).unwrap();
        let schema_path = schema_file.path().to_str().unwrap().to_string();

        // Spawn multiple concurrent validation tasks
        let handles: Vec<_> = (0..10)
            .map(|i| {
                let path = schema_path.clone();
                tokio::spawn(async move {
                    let value = serde_json::json!({"id": i});
                    validate_schema(&value, &path).await
                })
            })
            .collect();

        // All should succeed
        for handle in handles {
            let result = handle.await.unwrap();
            assert!(result.is_ok());
        }
    }

    #[tokio::test]
    async fn make_task_result_preserves_duration() {
        let duration = Duration::from_secs(5);
        let result = make_task_result("output".to_string(), None, duration).await;

        assert_eq!(result.duration, duration);
    }

    #[tokio::test]
    async fn make_task_result_json_array() {
        use crate::ast::OutputPolicy;

        let policy = OutputPolicy {
            format: OutputFormat::Json,
            schema: None,
            max_retries: None,
        };

        let result = make_task_result(
            r#"[1, 2, 3, "four"]"#.to_string(),
            Some(&policy),
            Duration::from_millis(50),
        )
        .await;

        assert!(result.is_success());
        assert!(result.output.is_array());
        let arr = result.output.as_array().unwrap();
        assert_eq!(arr.len(), 4);
        assert_eq!(arr[3], "four");
    }

    // ══════════════════════════════════════════════════════════════
    // EXTRACT_JSON_FROM_OUTPUT TESTS (v0.7.2)
    // ══════════════════════════════════════════════════════════════

    #[test]
    fn extract_json_direct_parse() {
        let input = r#"{"key": "value"}"#;
        let result = extract_json_from_output(input).unwrap();
        assert_eq!(result["key"], "value");
    }

    #[test]
    fn extract_json_with_whitespace() {
        let input = r#"
            {"key": "value"}
        "#;
        let result = extract_json_from_output(input).unwrap();
        assert_eq!(result["key"], "value");
    }

    #[test]
    fn extract_json_from_markdown_json_block() {
        let input = r#"Here's the JSON:

```json
{"name": "Thibaut", "score": 42}
```

Hope this helps!"#;
        let result = extract_json_from_output(input).unwrap();
        assert_eq!(result["name"], "Thibaut");
        assert_eq!(result["score"], 42);
    }

    #[test]
    fn extract_json_from_markdown_plain_block() {
        let input = r#"The result:

```
{"items": [1, 2, 3]}
```
"#;
        let result = extract_json_from_output(input).unwrap();
        assert!(result["items"].is_array());
    }

    #[test]
    fn extract_json_from_prose_with_braces() {
        let input = r#"I'll generate the fortune for you:

The cosmic reading reveals: {"sign": "scorpio", "lucky_number": 7, "message": "Great things await"}

This is based on ancient wisdom."#;
        let result = extract_json_from_output(input).unwrap();
        assert_eq!(result["sign"], "scorpio");
        assert_eq!(result["lucky_number"], 7);
    }

    #[test]
    fn extract_json_array_from_markdown() {
        let input = r#"```json
[{"id": 1}, {"id": 2}, {"id": 3}]
```"#;
        let result = extract_json_from_output(input).unwrap();
        assert!(result.is_array());
        assert_eq!(result.as_array().unwrap().len(), 3);
    }

    #[test]
    fn extract_json_nested_objects() {
        let input = r#"Result: {"outer": {"inner": {"deep": "value"}}}"#;
        let result = extract_json_from_output(input).unwrap();
        assert_eq!(result["outer"]["inner"]["deep"], "value");
    }

    #[test]
    fn extract_json_with_escaped_braces_in_strings() {
        let input = r#"{"template": "Use {{variable}} syntax", "count": 1}"#;
        let result = extract_json_from_output(input).unwrap();
        assert_eq!(result["template"], "Use {{variable}} syntax");
    }

    #[test]
    fn extract_json_no_json_found() {
        let input = "This is just plain text without any JSON.";
        let result = extract_json_from_output(input);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .contains("No JSON object or array found"));
    }

    #[tokio::test]
    async fn make_task_result_handles_markdown_wrapped_json() {
        use crate::ast::OutputPolicy;

        let policy = OutputPolicy {
            format: OutputFormat::Json,
            schema: None,
            max_retries: None,
        };

        // Simulate LLM output with markdown code block
        let llm_output = r#"Here's your fortune:

```json
{
  "sign": "scorpio",
  "lucky_number": 7,
  "message": "The stars align in your favor"
}
```

Enjoy your reading!"#;

        let result = make_task_result(
            llm_output.to_string(),
            Some(&policy),
            Duration::from_millis(100),
        )
        .await;

        assert!(result.is_success(), "Should parse JSON from markdown block");
        assert_eq!(result.output["sign"], "scorpio");
        assert_eq!(result.output["lucky_number"], 7);
    }

    #[tokio::test]
    async fn make_task_result_empty_output_returns_null() {
        use crate::ast::OutputPolicy;

        let policy = OutputPolicy {
            format: OutputFormat::Json,
            schema: None,
            max_retries: None,
        };

        // v0.12.1: Empty output with JSON format returns null
        let empty_output = "".to_string();
        let result = make_task_result(empty_output, Some(&policy), std::time::Duration::ZERO).await;

        assert!(result.is_success(), "Empty output should succeed with null");
        assert!(result.output.is_null(), "Empty output should return null");
    }

    #[tokio::test]
    async fn make_task_result_whitespace_output_returns_null() {
        use crate::ast::OutputPolicy;

        let policy = OutputPolicy {
            format: OutputFormat::Json,
            schema: None,
            max_retries: None,
        };

        // v0.12.1: Whitespace-only output also returns null
        let whitespace_output = "   \n\t  ".to_string();
        let result =
            make_task_result(whitespace_output, Some(&policy), std::time::Duration::ZERO).await;

        assert!(
            result.is_success(),
            "Whitespace-only output should succeed with null"
        );
        assert!(
            result.output.is_null(),
            "Whitespace-only output should return null"
        );
    }

    // ══════════════════════════════════════════════════════════════
    // INLINE SCHEMA VALIDATION TESTS
    // ══════════════════════════════════════════════════════════════

    #[tokio::test]
    async fn validate_schema_ref_inline_success() {
        let schema = serde_json::json!({
            "type": "object",
            "properties": {
                "name": { "type": "string" }
            },
            "required": ["name"]
        });
        let value = serde_json::json!({"name": "test"});
        let result = validate_schema_ref(&value, &SchemaRef::Inline(schema)).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn validate_schema_ref_inline_failure() {
        let schema = serde_json::json!({
            "type": "object",
            "properties": {
                "name": { "type": "string" }
            },
            "required": ["name"]
        });
        let value = serde_json::json!({"other": "field"});
        let result = validate_schema_ref(&value, &SchemaRef::Inline(schema)).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("required") || err.contains("name"),
            "Error should mention missing required field: {}",
            err
        );
    }

    #[test]
    fn format_validation_errors_shows_details() {
        let schema = serde_json::json!({
            "type": "object",
            "properties": {
                "age": { "type": "integer", "minimum": 0 }
            },
            "required": ["age"]
        });
        let value = serde_json::json!({"age": -5});
        let errors = format_validation_errors(&value, &schema);
        assert!(errors.contains("-5"), "Should show the invalid value");
    }
}