nika 0.35.4

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
//! Document Symbol Handler
//!
//! Provides document outline for Nika workflows:
//! - Workflow name
//! - Tasks with their verbs
//! - MCP server configurations
//! - Context and include sections
//!
//! ## Phase 2 Architecture
//!
//! The symbol handler can use AstIndex for accurate span-based locations:
//! - Use `compute_document_symbols_with_ast` when AstIndex is available
//! - Returns hierarchical DocumentSymbol structure (tasks contain verbs)
//! - TaskTable provides O(1) task lookup with precise spans

#[cfg(feature = "lsp")]
use tower_lsp_server::ls_types::*;

#[cfg(feature = "lsp")]
pub use tower_lsp_server::ls_types::Uri;

#[cfg(feature = "lsp")]
use super::super::ast_index::AstIndex;
#[cfg(feature = "lsp")]
use crate::lsp::conversion::span_to_range;

/// Compute hierarchical document symbols using AstIndex for accurate positions
///
/// Returns a nested `DocumentSymbol` structure where:
/// - Top-level: workflow, mcp, context, tasks section
/// - Tasks contain their verbs as children
/// - All spans come from the AST for precise locations
#[cfg(feature = "lsp")]
pub fn compute_document_symbols_with_ast(
    ast_index: &AstIndex,
    uri: &Uri,
    text: &str,
) -> Vec<DocumentSymbol> {
    let mut symbols = Vec::new();

    // Try to get cached AST
    if let Some(cached) = ast_index.get(uri) {
        if let Some(ref analyzed) = cached.analyzed {
            // Workflow name
            if let Some(ref name) = analyzed.name {
                symbols.push(create_doc_symbol(
                    format!("workflow: {}", name),
                    SymbolKind::MODULE,
                    range_for_line(text, 1), // Usually line 1 or 2
                    None,
                ));
            }

            // Schema version
            let schema_line = find_line_with_prefix(text, "schema:");
            if let Some(line_num) = schema_line {
                let line = text.lines().nth(line_num).unwrap_or("");
                symbols.push(create_doc_symbol(
                    line.trim().to_string(),
                    SymbolKind::NAMESPACE,
                    range_for_line(text, line_num),
                    None,
                ));
            }

            // Provider
            if let Some(ref provider) = analyzed.provider {
                symbols.push(create_doc_symbol(
                    format!("provider: {}", provider),
                    SymbolKind::CONSTANT,
                    find_range_with_prefix(text, "provider:"),
                    None,
                ));
            }

            // MCP servers
            if !analyzed.mcp_servers.is_empty() {
                let server_children: Vec<DocumentSymbol> = analyzed
                    .mcp_servers
                    .iter()
                    .map(|(name, server)| {
                        create_doc_symbol(
                            format!("🔌 {}", name),
                            SymbolKind::INTERFACE,
                            span_to_range(&server.span, text),
                            None,
                        )
                    })
                    .collect();

                symbols.push(create_doc_symbol(
                    "mcp.servers".to_string(),
                    SymbolKind::NAMESPACE,
                    find_range_with_prefix(text, "servers:"),
                    Some(server_children),
                ));
            }

            // Context files
            if !analyzed.context_files.is_empty() {
                let file_children: Vec<DocumentSymbol> = analyzed
                    .context_files
                    .iter()
                    .filter_map(|cf| {
                        cf.alias.as_ref().map(|alias| {
                            create_doc_symbol(
                                format!("📄 {}", alias),
                                SymbolKind::FILE,
                                span_to_range(&cf.span, text),
                                None,
                            )
                        })
                    })
                    .collect();

                if !file_children.is_empty() {
                    symbols.push(create_doc_symbol(
                        "context.files".to_string(),
                        SymbolKind::NAMESPACE,
                        find_range_with_prefix(text, "context:"),
                        Some(file_children),
                    ));
                }
            }

            // Tasks with their verbs as children
            if !analyzed.tasks.is_empty() {
                let task_symbols: Vec<DocumentSymbol> = analyzed
                    .tasks
                    .iter()
                    .map(|task| {
                        let task_range = span_to_range(&task.span, text);
                        let verb_child = task_verb_symbol(&task.action, task_range);
                        create_doc_symbol(
                            format!("task: {}", task.name),
                            SymbolKind::FUNCTION,
                            task_range,
                            verb_child.map(|v| vec![v]),
                        )
                    })
                    .collect();

                symbols.push(create_doc_symbol(
                    "tasks".to_string(),
                    SymbolKind::NAMESPACE,
                    find_range_with_prefix(text, "tasks:"),
                    Some(task_symbols),
                ));
            }

            return symbols;
        }
    }

    // Fallback to text-based symbols converted to DocumentSymbol
    compute_document_symbols(text)
        .into_iter()
        .map(symbol_info_to_doc_symbol)
        .collect()
}

/// Create a symbol for a task's verb action
#[cfg(feature = "lsp")]
fn task_verb_symbol(
    action: &crate::ast::analyzed::AnalyzedTaskAction,
    task_range: Range,
) -> Option<DocumentSymbol> {
    use crate::ast::analyzed::AnalyzedTaskAction;

    let (icon, name, kind) = match action {
        AnalyzedTaskAction::Infer(params) => {
            let detail = truncate_prompt(&params.prompt, 30);
            ("", format!("infer: {}", detail), SymbolKind::METHOD)
        }
        AnalyzedTaskAction::Exec(params) => {
            let cmd = truncate_prompt(&params.command, 30);
            ("📟", format!("exec: {}", cmd), SymbolKind::METHOD)
        }
        AnalyzedTaskAction::Fetch(params) => {
            let url = truncate_prompt(&params.url, 30);
            ("🛰️", format!("fetch: {}", url), SymbolKind::METHOD)
        }
        AnalyzedTaskAction::Invoke(params) => {
            let tool = truncate_prompt(&params.tool, 30);
            ("🔌", format!("invoke: {}", tool), SymbolKind::METHOD)
        }
        AnalyzedTaskAction::Agent(params) => {
            let detail = truncate_prompt(&params.prompt, 30);
            ("🐔", format!("agent: {}", detail), SymbolKind::METHOD)
        }
    };

    Some(create_doc_symbol(
        format!("{} {}", icon, name),
        kind,
        task_range, // Use task range for the verb symbol
        None,
    ))
}

/// Truncate a prompt for display
#[cfg(feature = "lsp")]
fn truncate_prompt(s: &str, max_len: usize) -> String {
    if s.len() > max_len {
        format!("{}...", &s[..max_len.saturating_sub(3)])
    } else {
        s.to_string()
    }
}

/// Create a DocumentSymbol
#[cfg(feature = "lsp")]
#[allow(deprecated)] // deprecated field still required
fn create_doc_symbol(
    name: String,
    kind: SymbolKind,
    range: Range,
    children: Option<Vec<DocumentSymbol>>,
) -> DocumentSymbol {
    DocumentSymbol {
        name,
        kind,
        range,
        selection_range: range,
        detail: None,
        tags: None,
        deprecated: None,
        children,
    }
}

/// Convert a SymbolInformation to DocumentSymbol (for fallback)
#[cfg(feature = "lsp")]
#[allow(deprecated)]
fn symbol_info_to_doc_symbol(info: SymbolInformation) -> DocumentSymbol {
    DocumentSymbol {
        name: info.name,
        kind: info.kind,
        range: info.location.range,
        selection_range: info.location.range,
        detail: None,
        tags: None,
        deprecated: None,
        children: None,
    }
}

/// Find the line number containing a prefix
#[cfg(feature = "lsp")]
fn find_line_with_prefix(text: &str, prefix: &str) -> Option<usize> {
    text.lines()
        .enumerate()
        .find(|(_, line)| line.trim().starts_with(prefix))
        .map(|(i, _)| i)
}

/// Get a range for a specific line
#[cfg(feature = "lsp")]
fn range_for_line(text: &str, line_num: usize) -> Range {
    let line_len = text.lines().nth(line_num).map(|l| l.len()).unwrap_or(0) as u32;
    Range {
        start: Position {
            line: line_num as u32,
            character: 0,
        },
        end: Position {
            line: line_num as u32,
            character: line_len,
        },
    }
}

/// Find range for a line with given prefix
#[cfg(feature = "lsp")]
fn find_range_with_prefix(text: &str, prefix: &str) -> Range {
    find_line_with_prefix(text, prefix)
        .map(|line_num| range_for_line(text, line_num))
        .unwrap_or(Range {
            start: Position {
                line: 0,
                character: 0,
            },
            end: Position {
                line: 0,
                character: 0,
            },
        })
}

/// Compute document symbols for the workflow
#[cfg(feature = "lsp")]
pub fn compute_document_symbols(text: &str) -> Vec<SymbolInformation> {
    let mut symbols = Vec::new();

    // Track current section for context
    let mut current_task_id: Option<String> = None;
    let mut task_start_line: u32 = 0;

    for (line_num, line) in text.lines().enumerate() {
        let line_num = line_num as u32;
        let trimmed = line.trim();
        let indent = line.len() - trimmed.len();

        // Schema declaration
        if let Some(value) = trimmed.strip_prefix("schema:") {
            symbols.push(create_symbol(
                format!("schema: {}", value.trim()),
                SymbolKind::NAMESPACE,
                line_num,
                0,
                line.len() as u32,
            ));
        }

        // Workflow name
        if let Some(value) = trimmed.strip_prefix("workflow:") {
            let name = value.trim().trim_matches('"').trim_matches('\'');
            symbols.push(create_symbol(
                format!("workflow: {}", name),
                SymbolKind::MODULE,
                line_num,
                0,
                line.len() as u32,
            ));
        }

        // Provider
        if let Some(value) = trimmed.strip_prefix("provider:") {
            symbols.push(create_symbol(
                format!("provider: {}", value.trim()),
                SymbolKind::CONSTANT,
                line_num,
                0,
                line.len() as u32,
            ));
        }

        // Task ID - handle both "id:" and "- id:" (list item syntax)
        let task_id_value = if let Some(stripped) = trimmed.strip_prefix("- id:") {
            Some(stripped.trim())
        } else if let Some(stripped) = trimmed.strip_prefix("id:") {
            if indent > 0 {
                Some(stripped.trim())
            } else {
                None
            }
        } else {
            None
        };

        if let Some(id_str) = task_id_value {
            // Save previous task if exists
            if let Some(ref task_id) = current_task_id {
                // We'd update the end range here if we tracked it
                let _ = task_id; // suppress warning
            }

            let task_id = id_str.trim_matches('"').trim_matches('\'');
            current_task_id = Some(task_id.to_string());
            task_start_line = line_num;

            symbols.push(create_symbol(
                format!("task: {}", task_id),
                SymbolKind::FUNCTION,
                line_num,
                indent as u32,
                line.len() as u32,
            ));
        }

        // Verb detection (infer, exec, fetch, invoke, agent)
        for (verb, kind, icon) in VERB_SYMBOLS {
            if trimmed.starts_with(&format!("{}:", verb)) {
                let detail = extract_verb_detail(trimmed, verb);
                symbols.push(create_symbol(
                    format!("{} {}", icon, detail),
                    *kind,
                    line_num,
                    indent as u32,
                    line.len() as u32,
                ));
            }
        }

        // MCP servers section
        if trimmed == "servers:" {
            symbols.push(create_symbol(
                "mcp.servers".to_string(),
                SymbolKind::NAMESPACE,
                line_num,
                indent as u32,
                line.len() as u32,
            ));
        }

        // Individual MCP server
        if indent == 6 && trimmed.ends_with(':') && !trimmed.contains(' ') {
            let server_name = trimmed.trim_end_matches(':');
            symbols.push(create_symbol(
                format!("🔌 {}", server_name),
                SymbolKind::INTERFACE,
                line_num,
                indent as u32,
                line.len() as u32,
            ));
        }

        // Context section
        if trimmed == "context:" {
            symbols.push(create_symbol(
                "context".to_string(),
                SymbolKind::NAMESPACE,
                line_num,
                0,
                line.len() as u32,
            ));
        }

        // Context files
        if trimmed == "files:" && indent == 2 {
            symbols.push(create_symbol(
                "context.files".to_string(),
                SymbolKind::NAMESPACE,
                line_num,
                indent as u32,
                line.len() as u32,
            ));
        }

        // Individual context file
        if indent == 4 && trimmed.contains(':') && !trimmed.starts_with('-') {
            let file_name = trimmed.split(':').next().unwrap_or("");
            if !file_name.is_empty() && !RESERVED_FIELDS.contains(&file_name) {
                symbols.push(create_symbol(
                    format!("📄 {}", file_name),
                    SymbolKind::FILE,
                    line_num,
                    indent as u32,
                    line.len() as u32,
                ));
            }
        }

        // Include section
        if trimmed == "include:" {
            symbols.push(create_symbol(
                "include".to_string(),
                SymbolKind::NAMESPACE,
                line_num,
                0,
                line.len() as u32,
            ));
        }

        // Include path
        if trimmed.starts_with("path:") && indent > 0 {
            let path = trimmed[5..].trim().trim_matches('"').trim_matches('\'');
            symbols.push(create_symbol(
                format!("📦 {}", path),
                SymbolKind::PACKAGE,
                line_num,
                indent as u32,
                line.len() as u32,
            ));
        }

        // Skills section
        if trimmed == "skills:" {
            symbols.push(create_symbol(
                "skills".to_string(),
                SymbolKind::NAMESPACE,
                line_num,
                0,
                line.len() as u32,
            ));
        }

        // For each parallel iteration
        if trimmed.starts_with("for_each:") {
            symbols.push(create_symbol(
                "🔄 for_each".to_string(),
                SymbolKind::EVENT,
                line_num,
                indent as u32,
                line.len() as u32,
            ));
        }
    }

    // Clear last task
    current_task_id = None;
    let _ = (current_task_id, task_start_line); // suppress warnings

    symbols
}

/// Placeholder URL used for symbols (valid file URI)
///
/// Note: Symbols have no native way to exclude URI in tower-lsp.
/// The actual document URI should ideally be passed, but compute_document_symbols
/// doesn't receive it. This is a limitation of the current API.
#[cfg(feature = "lsp")]
fn placeholder_url() -> Uri {
    // Use `expect` since this is a known-valid URL literal that cannot fail
    "file:///placeholder"
        .parse::<Uri>()
        .expect("static valid URL")
}

/// Create a SymbolInformation
#[cfg(feature = "lsp")]
fn create_symbol(
    name: String,
    kind: SymbolKind,
    line: u32,
    start_char: u32,
    end_char: u32,
) -> SymbolInformation {
    #[allow(deprecated)] // container_name is deprecated but still required
    SymbolInformation {
        name,
        kind,
        tags: None,
        deprecated: None,
        location: Location {
            uri: placeholder_url(),
            range: Range {
                start: Position {
                    line,
                    character: start_char,
                },
                end: Position {
                    line,
                    character: end_char,
                },
            },
        },
        container_name: None,
    }
}

/// Extract a detail string from a verb line
#[cfg(feature = "lsp")]
fn extract_verb_detail(line: &str, verb: &str) -> String {
    let after_colon = &line[verb.len() + 1..];
    let trimmed = after_colon.trim();

    // Shorthand form: verb: "value"
    if trimmed.starts_with('"') || trimmed.starts_with('\'') {
        let quote = trimmed.chars().next().unwrap();
        if let Some(end) = trimmed[1..].find(quote) {
            let value = &trimmed[1..end + 1];
            // Truncate long prompts
            if value.len() > 40 {
                return format!("{}: {}...", verb, crate::util::truncate_str(value, 37));
            }
            return format!("{}: {}", verb, value);
        }
    }

    // Full form or empty
    verb.to_string()
}

/// Verb symbols with their kinds and icons
#[cfg(feature = "lsp")]
const VERB_SYMBOLS: &[(&str, SymbolKind, &str)] = &[
    ("infer", SymbolKind::METHOD, ""),
    ("exec", SymbolKind::METHOD, "📟"),
    ("fetch", SymbolKind::METHOD, "🛰️"),
    ("invoke", SymbolKind::METHOD, "🔌"),
    ("agent", SymbolKind::METHOD, "🐔"),
];

/// Reserved field names (not context files)
#[cfg(feature = "lsp")]
const RESERVED_FIELDS: &[&str] = &[
    "schema",
    "workflow",
    "tasks",
    "flows",
    "mcp",
    "context",
    "include",
    "skills",
    "provider",
    "id",
    "with",
    "for_each",
    "as",
    "concurrency",
    "fail_fast",
    "infer",
    "exec",
    "fetch",
    "invoke",
    "agent",
    "servers",
    "files",
    "session",
    "path",
    "prefix",
    "alias",
];

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

    #[test]
    #[cfg(feature = "lsp")]
    fn test_document_symbols_basic() {
        let text = r#"
schema: nika/workflow@0.12
workflow: my-workflow

tasks:
  - id: step1
    infer: "Generate content"
  - id: step2
    exec: "npm run build"
"#;
        let symbols = compute_document_symbols(text);

        // Check we found schema, workflow, tasks, and verbs
        let names: Vec<_> = symbols.iter().map(|s| s.name.as_str()).collect();
        assert!(names.iter().any(|n| n.contains("schema")));
        assert!(names.iter().any(|n| n.contains("workflow: my-workflow")));
        assert!(names.iter().any(|n| n.contains("task: step1")));
        assert!(names.iter().any(|n| n.contains("task: step2")));
        assert!(names.iter().any(|n| n.contains(""))); // infer icon
        assert!(names.iter().any(|n| n.contains("📟"))); // exec icon
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_document_symbols_mcp() {
        let text = r#"
schema: nika/workflow@0.12
mcp:
  servers:
    novanet:
      command: node
    perplexity:
      command: npx
"#;
        let symbols = compute_document_symbols(text);

        let names: Vec<_> = symbols.iter().map(|s| s.name.as_str()).collect();
        assert!(names.iter().any(|n| n.contains("mcp.servers")));
        assert!(names.iter().any(|n| n.contains("novanet")));
        assert!(names.iter().any(|n| n.contains("perplexity")));
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_document_symbols_context() {
        let text = r#"
schema: nika/workflow@0.12
context:
  files:
    brand: ./brand.md
    data: ./data.json
"#;
        let symbols = compute_document_symbols(text);

        let names: Vec<_> = symbols.iter().map(|s| s.name.as_str()).collect();
        assert!(names.contains(&"context"));
        assert!(names.contains(&"context.files"));
        assert!(names.iter().any(|n| n.contains("brand")));
        assert!(names.iter().any(|n| n.contains("data")));
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_document_symbols_for_each() {
        let text = r#"
tasks:
  - id: parallel_task
    for_each: ["a", "b", "c"]
    as: item
    infer: "Process {{with.item}}"
"#;
        let symbols = compute_document_symbols(text);

        let names: Vec<_> = symbols.iter().map(|s| s.name.as_str()).collect();
        assert!(names.iter().any(|n| n.contains("for_each")));
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_extract_verb_detail() {
        assert_eq!(
            extract_verb_detail("infer: \"Generate a headline\"", "infer"),
            "infer: Generate a headline"
        );

        // Test truncation of long prompts
        let long_prompt = "infer: \"This is a very long prompt that should be truncated because it exceeds forty characters\"";
        let detail = extract_verb_detail(long_prompt, "infer");
        assert!(detail.ends_with("..."));
        assert!(detail.len() < 50);
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_verb_icon_mapping() {
        let text = r#"
tasks:
  - id: t1
    infer: "test"
  - id: t2
    agent:
      prompt: "test"
"#;
        let symbols = compute_document_symbols(text);

        let names: Vec<_> = symbols.iter().map(|s| s.name.as_str()).collect();
        assert!(names.iter().any(|n| n.starts_with(""))); // infer
        assert!(names.iter().any(|n| n.starts_with("🐔"))); // agent
    }

    // ============================================================================
    // AST-Aware Symbol Tests (Phase 2)
    // ============================================================================

    #[test]
    #[cfg(feature = "lsp")]
    fn test_document_symbols_with_ast_hierarchical() {
        let text = r#"schema: nika/workflow@0.12
workflow: test-workflow

tasks:
  - id: step1
    infer: "Generate content"
  - id: step2
    exec: "npm run build"
"#;
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let ast_index = AstIndex::new();

        // Parse document into AST index
        ast_index.parse_document(&uri, text, 0);

        let symbols = compute_document_symbols_with_ast(&ast_index, &uri, text);

        // Should have hierarchical structure
        assert!(!symbols.is_empty());

        // Check for top-level symbols
        let names: Vec<_> = symbols.iter().map(|s| s.name.as_str()).collect();
        assert!(names.iter().any(|n| n.contains("workflow: test-workflow")));
        assert!(names.iter().any(|n| n.contains("schema:")));
        assert!(names.contains(&"tasks"));
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_document_symbols_with_ast_tasks_have_verb_children() {
        let text = r#"schema: nika/workflow@0.12
workflow: test
tasks:
  - id: generate
    infer: "Generate something"
"#;
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let ast_index = AstIndex::new();

        // Parse document into AST index
        ast_index.parse_document(&uri, text, 0);

        let symbols = compute_document_symbols_with_ast(&ast_index, &uri, text);

        // Find the tasks section
        let tasks_symbol = symbols.iter().find(|s| s.name == "tasks");
        assert!(tasks_symbol.is_some());

        // Tasks should have children (task symbols)
        let tasks = tasks_symbol.unwrap();
        assert!(tasks.children.is_some());
        let task_children = tasks.children.as_ref().unwrap();
        assert!(!task_children.is_empty());

        // Each task should have a verb child
        let task = task_children.iter().find(|s| s.name.contains("generate"));
        assert!(task.is_some());
        let task = task.unwrap();
        assert!(task.children.is_some());

        // Verb child should have the infer icon
        let verb = &task.children.as_ref().unwrap()[0];
        assert!(verb.name.contains(""));
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_document_symbols_with_ast_fallback() {
        // When AST has no cached document, falls back to text-based
        let text = r#"
schema: nika/workflow@0.12
workflow: fallback-test
tasks:
  - id: step1
    infer: "Hello"
"#;
        let uri = "file:///uncached.nika.yaml".parse::<Uri>().unwrap();
        let ast_index = AstIndex::new();
        // Deliberately NOT parsing into AST index

        let symbols = compute_document_symbols_with_ast(&ast_index, &uri, text);

        // Should still return symbols via fallback
        assert!(!symbols.is_empty());
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_truncate_prompt() {
        assert_eq!(truncate_prompt("short", 30), "short");
        assert_eq!(
            truncate_prompt("this is a very long prompt that exceeds the limit", 30),
            "this is a very long prompt ..."
        );
    }
}