ai-agent 0.88.0

Idiomatic agent sdk inspired by the claude code source leak
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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
use crate::AgentError;
use crate::query_engine::{QueryEngine, QueryEngineConfig, empty_json_value};
use crate::tools::deferred_tools::{
    ToolSearchQuery, parse_tool_name, parse_tool_search_query, search_tools_with_keywords,
};
use crate::tools::get_all_base_tools;
use crate::tools::search::ToolSearchTool;
use crate::types::{Message, MessageRole, ToolCall, ToolContext, ToolDefinition, ToolInputSchema};

#[tokio::test]
async fn test_engine_creation() {
    let engine = QueryEngine::new(QueryEngineConfig {
        cwd: "/tmp".to_string(),
        model: "claude-sonnet-4-6".to_string(),
        api_key: None,
        base_url: None,
        tools: vec![],
        system_prompt: None,
        max_turns: 10,
        max_budget_usd: None,
        max_tokens: 16384,
        fallback_model: None,
        user_context: std::collections::HashMap::new(),
        system_context: std::collections::HashMap::new(),
        can_use_tool: None,
        on_event: None,
        thinking: None,
        abort_controller: None,
        token_budget: None,
        agent_id: None,
        session_state: None,
        loaded_nested_memory_paths: std::collections::HashSet::new(),
            task_budget: None,
            orphaned_permission: None,
    });
    assert_eq!(engine.get_turn_count(), 0);
}

#[tokio::test]
async fn test_engine_submit_message() {
    let mut engine = QueryEngine::new(QueryEngineConfig {
        cwd: "/tmp".to_string(),
        model: "claude-sonnet-4-6".to_string(),
        api_key: None,
        base_url: None,
        tools: vec![],
        system_prompt: None,
        max_turns: 10,
        max_budget_usd: None,
        max_tokens: 16384,
        fallback_model: None,
        user_context: std::collections::HashMap::new(),
        system_context: std::collections::HashMap::new(),
        can_use_tool: None,
        on_event: None,
        thinking: None,
        abort_controller: None,
        token_budget: None,
        agent_id: None,
        session_state: None,
        loaded_nested_memory_paths: std::collections::HashSet::new(),
            task_budget: None,
            orphaned_permission: None,
    });

    let result = engine.submit_message("Hello").await;
    // Should fail because no API key
    assert!(result.is_err());
}

#[test]
fn test_strip_thinking() {
    use crate::query_engine::strip_thinking;

    // Test stripping thinking tags from content
    let content =
        "<think>I should list the files here.</think>Here are the files: file1.txt, file2.txt";
    let result = strip_thinking(content);
    assert_eq!(result, "Here are the files: file1.txt, file2.txt");

    // Test content without thinking tags
    let content2 = "Hello world";
    let result2 = strip_thinking(content2);
    assert_eq!(result2, "Hello world");

    // Test content with only thinking tags
    let content3 = "<think>Thinking...</think>";
    let result3 = strip_thinking(content3);
    assert_eq!(result3, "");

    // Test multiple thinking blocks (no spaces between thinking and text in input)
    let content4 = "<think>First think</think>Hello<think>Second think</think>World";
    let result4 = strip_thinking(content4);
    assert_eq!(result4, "HelloWorld");
}

#[test]
fn test_strip_thinking_utf8() {
    use crate::query_engine::strip_thinking;

    // Test UTF-8 multi-byte characters (arrow → is 3 bytes)
    let content = "<think>思考</think>Hello → World";
    let result = strip_thinking(content);
    assert_eq!(result, "Hello → World");

    // Test Chinese characters (each char is 3 bytes)
    let content2 = "<think>中文</think>你好世界";
    let result2 = strip_thinking(content2);
    assert_eq!(result2, "你好世界");

    // Test emoji (4 bytes each)
    let content3 = "<think>thinking emoji 🎭</think>Hello 👋 World";
    let result3 = strip_thinking(content3);
    assert_eq!(result3, "Hello 👋 World");

    // Test mixed content with UTF-8
    let content4 = "<think>The → symbol is here</think>Result: 你好 🎉";
    let result4 = strip_thinking(content4);
    assert_eq!(result4, "Result: 你好 🎉");

    // Test thinking at start with UTF-8
    let content5 = "<think>thinking开始啦</think>继续内容";
    let result5 = strip_thinking(content5);
    assert_eq!(result5, "继续内容");

    // Test thinking at end with UTF-8
    let content6 = "开始内容<think>thinking结束啦</think>";
    let result6 = strip_thinking(content6);
    assert_eq!(result6, "开始内容");

    // Test multiple UTF-8 thinking blocks
    let content7 = "<think>第一步思考→思考第二步</think>执行→完成";
    let result7 = strip_thinking(content7);
    assert_eq!(result7, "执行→完成");
}

#[test]
fn test_fallback_tool_call_extraction() {
    // Test that fallback path extracts tool calls from non-streaming response
    use serde_json::json;

    // Simulate a non-streaming response with tool calls
    let response = json!({
        "choices": [
            {
                "message": {
                    "content": null,
                    "tool_calls": [
                        {
                            "id": "call_123",
                            "type": "function",
                            "function": {
                                "name": "Bash",
                                "arguments": "{\"command\": \"ls -la\"}"
                            }
                        }
                    ]
                },
                "finish_reason": "tool_calls"
            }
        ],
        "usage": {
            "prompt_tokens": 100,
            "completion_tokens": 50
        }
    });

    // Extract tool calls like the fallback code does
    let mut tool_calls = Vec::new();
    if let Some(choices) = response.get("choices").and_then(|c| c.as_array()) {
        if let Some(first) = choices.first() {
            if let Some(msg) = first.get("message") {
                if let Some(tc_array) = msg.get("tool_calls").and_then(|t| t.as_array()) {
                    for tc in tc_array {
                        let id = tc.get("id").and_then(|i| i.as_str()).unwrap_or("");
                        let func = tc.get("function");
                        let name = func
                            .and_then(|f| f.get("name"))
                            .and_then(|n| n.as_str())
                            .unwrap_or("");
                        let args = func.and_then(|f| f.get("arguments"));
                        let args_val = if let Some(args_str) = args.and_then(|a| a.as_str()) {
                            serde_json::from_str(args_str).unwrap_or_else(|_| empty_json_value())
                        } else {
                            args.cloned().unwrap_or_else(|| empty_json_value())
                        };
                        tool_calls.push(serde_json::json!({
                            "id": id,
                            "name": name,
                            "arguments": args_val,
                        }));
                    }
                }
            }
        }
    }

    assert_eq!(tool_calls.len(), 1);
    assert_eq!(tool_calls[0]["name"], "Bash");
    assert_eq!(tool_calls[0]["id"], "call_123");
}

#[test]
fn test_streaming_tool_call_extraction() {
    // Test that streaming path can extract tool calls from SSE-like data
    use serde_json::json;

    // Simulate a streaming chunk with tool call delta
    let chunk = json!({
        "choices": [
            {
                "delta": {
                    "tool_calls": [
                        {
                            "id": "call_456",
                            "type": "function",
                            "function": {
                                "name": "Read",
                                "arguments": "{\"file_path\": \"/tmp/test\"}"
                            }
                        }
                    ]
                },
                "finish_reason": "tool_calls"
            }
        ]
    });

    // Verify the chunk has tool_calls
    let tool_calls = chunk
        .get("choices")
        .and_then(|c| c.as_array())
        .and_then(|choices| choices.first())
        .and_then(|choice| choice.get("delta"))
        .and_then(|delta| delta.get("tool_calls"))
        .and_then(|tc| tc.as_array());

    assert!(tool_calls.is_some());
    let tc = tool_calls.unwrap().first().unwrap();
    assert_eq!(tc.get("id").and_then(|i| i.as_str()), Some("call_456"));
    assert_eq!(
        tc.get("function")
            .and_then(|f| f.get("name"))
            .and_then(|n| n.as_str()),
        Some("Read")
    );
}

// =========================================================================
// Tool Calling Tests
// =========================================================================

#[test]
fn test_tool_definition_serialization() {
    let tools = get_all_base_tools();
    assert!(!tools.is_empty());

    // Test that tools can be serialized to OpenAI function format
    for tool in &tools {
        let tool_json = serde_json::json!({
            "type": "function",
            "function": {
                "name": tool.name,
                "description": tool.description,
                "parameters": tool.input_schema
            }
        });

        // Verify all required fields exist
        assert!(tool_json.get("type").is_some());
        assert!(tool_json.get("function").is_some());
        let func = tool_json.get("function").unwrap();
        assert!(func.get("name").is_some());
        assert!(func.get("description").is_some());
        assert!(func.get("parameters").is_some());

        // Verify name is not empty
        let name = func.get("name").unwrap().as_str().unwrap();
        assert!(!name.is_empty());
    }
}

#[test]
fn test_tool_call_parsing() {
    // Test parsing tool calls from message
    let tool_calls = vec![
        ToolCall {
            id: "call_abc123".to_string(),
            r#type: "function".to_string(),
            name: "Bash".to_string(),
            arguments: serde_json::json!({"command": "ls -la"}),
        },
        ToolCall {
            id: "call_def456".to_string(),
            r#type: "function".to_string(),
            name: "Read".to_string(),
            arguments: serde_json::json!({"path": "/tmp/test.txt"}),
        },
    ];

    // Verify tool call structure
    assert_eq!(tool_calls.len(), 2);
    assert_eq!(tool_calls[0].id, "call_abc123");
    assert_eq!(tool_calls[0].name, "Bash");
    assert_eq!(tool_calls[1].id, "call_def456");
    assert_eq!(tool_calls[1].name, "Read");
}

#[test]
fn test_tool_result_message_format() {
    // Test that tool results can be created with tool_call_id
    let msg = Message {
        role: MessageRole::Tool,
        content: "file content here".to_string(),
        tool_call_id: Some("call_abc123".to_string()),
        is_error: Some(false),
        ..Default::default()
    };

    assert_eq!(msg.role, MessageRole::Tool);
    assert_eq!(msg.tool_call_id, Some("call_abc123".to_string()));
    assert_eq!(msg.is_error, Some(false));
}

#[test]
fn test_tool_execution_context() {
    let ctx = ToolContext {
        cwd: "/tmp/test".to_string(),
        abort_signal: Default::default(),
    };

    assert_eq!(ctx.cwd, "/tmp/test");
}

#[test]
fn test_base_tools_available() {
    let tools = get_all_base_tools();

    // Verify essential tools are available
    let tool_names: Vec<&str> = tools.iter().map(|t| t.name.as_str()).collect();

    // Must have Bash tool
    assert!(tool_names.contains(&"Bash"), "Bash tool must be available");

    // Must have Read tool
    assert!(
        tool_names.contains(&"Read"),
        "FileRead tool must be available"
    );

    // Must have Write tool
    assert!(
        tool_names.contains(&"Write"),
        "FileWrite tool must be available"
    );

    // Must have Glob tool
    assert!(tool_names.contains(&"Glob"), "Glob tool must be available");

    // Must have Grep tool
    assert!(tool_names.contains(&"Grep"), "Grep tool must be available");

    // Must have Edit tool
    assert!(
        tool_names.contains(&"FileEdit"),
        "FileEdit tool must be available"
    );
}

#[test]
fn test_tool_schemas_have_required_fields() {
    let tools = get_all_base_tools();

    for tool in &tools {
        // Name must not be empty
        assert!(!tool.name.is_empty(), "Tool {} has empty name", tool.name);

        // Description must not be empty
        assert!(
            !tool.description.is_empty(),
            "Tool {} has empty description",
            tool.name
        );

        // Input schema must have required fields
        let schema = &tool.input_schema;
        assert!(
            !schema.schema_type.is_empty(),
            "Tool {} has empty schema_type",
            tool.name
        );
        assert!(
            schema.properties.is_object(),
            "Tool {} has non-object properties",
            tool.name
        );
    }
}

#[test]
fn test_tool_schema_has_required_parameters() {
    let tools = get_all_base_tools();

    // Find Bash tool and verify it has command parameter
    let bash_tool = tools.iter().find(|t| t.name == "Bash").unwrap();
    let props = &bash_tool.input_schema.properties;
    assert!(
        props.get("command").is_some(),
        "Bash tool must have 'command' parameter"
    );

    // Find Read tool and verify it has file_path parameter
    let read_tool = tools.iter().find(|t| t.name == "Read").unwrap();
    let read_props = &read_tool.input_schema.properties;
    assert!(
        read_props.get("file_path").is_some(),
        "Read tool must have 'file_path' parameter"
    );

    // Find Write tool and verify it has file_path and content parameters
    let write_tool = tools.iter().find(|t| t.name == "Write").unwrap();
    let write_props = &write_tool.input_schema.properties;
    assert!(
        write_props.get("file_path").is_some(),
        "Write tool must have 'file_path' parameter"
    );
    assert!(
        write_props.get("content").is_some(),
        "Write tool must have 'content' parameter"
    );

    // Verify required arrays are defined
    assert!(
        bash_tool.input_schema.required.is_some(),
        "Bash tool must have required parameters"
    );
}

#[tokio::test]
async fn test_engine_with_tools_config() {
    let tools = get_all_base_tools();

    let engine = QueryEngine::new(QueryEngineConfig {
        cwd: "/tmp".to_string(),
        model: "claude-sonnet-4-6".to_string(),
        api_key: None,
        base_url: None,
        tools: tools.clone(),
        system_prompt: Some("You are a helpful assistant.".to_string()),
        max_turns: 10,
        max_budget_usd: None,
        max_tokens: 16384,
        fallback_model: None,
        user_context: std::collections::HashMap::new(),
        system_context: std::collections::HashMap::new(),
        can_use_tool: None,
        on_event: None,
        thinking: None,
        abort_controller: None,
        token_budget: None,
        agent_id: None,
        session_state: None,
        loaded_nested_memory_paths: std::collections::HashSet::new(),
            task_budget: None,
            orphaned_permission: None,
    });

    // Verify tools are stored in config
    assert!(!engine.config.tools.is_empty());
}

#[tokio::test]
async fn test_engine_system_prompt_includes_tool_guidance() {
    // Test that system prompt includes tool usage guidance
    let engine = QueryEngine::new(QueryEngineConfig {
        cwd: "/tmp".to_string(),
        model: "claude-sonnet-4-6".to_string(),
        api_key: None,
        base_url: None,
        tools: vec![],
        system_prompt: Some("You are an agent that helps users with software engineering tasks. Use the tools available to you to assist the user.".to_string()),
        max_turns: 10,
        max_budget_usd: None,
        max_tokens: 16384,
        fallback_model: None,
        user_context: std::collections::HashMap::new(),
        system_context: std::collections::HashMap::new(),
        can_use_tool: None,
        on_event: None,
        thinking: None,
        abort_controller: None,
        token_budget: None,
        agent_id: None,
        session_state: None,
        loaded_nested_memory_paths: std::collections::HashSet::new(),
            task_budget: None,
            orphaned_permission: None,
    });

    // Verify system prompt is set
    assert!(engine.config.system_prompt.is_some());
    let prompt = engine.config.system_prompt.as_ref().unwrap();
    assert!(prompt.contains("tools"));
}

#[test]
fn test_tool_call_arguments_json() {
    // Test that tool call arguments can be serialized/deserialized as JSON
    let tc = ToolCall {
        id: "call_test".to_string(),
        r#type: "function".to_string(),
        name: "Bash".to_string(),
        arguments: serde_json::json!({
            "command": "echo hello"
        }),
    };

    // Serialize arguments to string
    let args_str = tc.arguments.to_string();
    assert!(!args_str.is_empty());

    // Deserialize back
    let parsed: serde_json::Value = serde_json::from_str(&args_str).unwrap();
    assert_eq!(
        parsed.get("command").and_then(|v| v.as_str()),
        Some("echo hello")
    );
}

#[test]
fn test_build_api_messages_includes_tools_info() {
    // This test verifies that the system prompt structure supports tool calling
    let system_prompt =
        "You are an agent. Use the tools available to you: Bash, Read, Write, Glob, Grep, Edit.";

    // Verify the prompt mentions tools
    assert!(system_prompt.contains("tools"));
    assert!(system_prompt.contains("Bash"));
}

#[tokio::test]
async fn test_query_engine_tool_registration() {
    let tools = get_all_base_tools();
    let tool_names: Vec<String> = tools.iter().map(|t| t.name.clone()).collect();

    // Verify we have multiple tools registered
    assert!(tool_names.len() >= 10, "Should have at least 10 tools");

    // Verify key tools exist
    assert!(tool_names.contains(&"Bash".to_string()));
    assert!(tool_names.contains(&"Read".to_string()));
    assert!(tool_names.contains(&"Write".to_string()));
    assert!(tool_names.contains(&"Glob".to_string()));
    assert!(tool_names.contains(&"Grep".to_string()));
    assert!(tool_names.contains(&"FileEdit".to_string()));
}

#[test]
fn test_openai_tool_format_compatibility() {
    // Test that tools serialize to OpenAI-compatible format
    let tools = get_all_base_tools();
    let bash_tool = tools.iter().find(|t| t.name == "Bash").unwrap();

    let openai_format = serde_json::json!({
        "type": "function",
        "function": {
            "name": bash_tool.name,
            "description": bash_tool.description,
            "parameters": bash_tool.input_schema
        }
    });

    // Verify OpenAI format structure
    assert_eq!(openai_format.get("type").unwrap(), "function");
    let func = openai_format.get("function").unwrap();
    assert!(func.get("name").is_some());
    assert!(func.get("description").is_some());
    assert!(func.get("parameters").is_some());

    // Verify it can be serialized to JSON string
    let json_str = openai_format.to_string();
    assert!(!json_str.is_empty());

    // Verify it can be deserialized back
    let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
    assert_eq!(parsed.get("type").unwrap(), "function");
}

#[tokio::test]
async fn test_engine_message_history_with_tool_calls() {
    let mut engine = QueryEngine::new(QueryEngineConfig {
        cwd: "/tmp".to_string(),
        model: "claude-sonnet-4-6".to_string(),
        api_key: None,
        base_url: None,
        tools: vec![],
        system_prompt: None,
        max_turns: 10,
        max_budget_usd: None,
        max_tokens: 16384,
        fallback_model: None,
        user_context: std::collections::HashMap::new(),
        system_context: std::collections::HashMap::new(),
        can_use_tool: None,
        on_event: None,
        thinking: None,
        abort_controller: None,
        token_budget: None,
        agent_id: None,
        session_state: None,
        loaded_nested_memory_paths: std::collections::HashSet::new(),
            task_budget: None,
            orphaned_permission: None,
    });

    // Add user message
    engine.messages.push(Message {
        role: MessageRole::User,
        content: "List files in /tmp".to_string(),
        ..Default::default()
    });

    // Add assistant message with tool call
    engine.messages.push(Message {
        role: MessageRole::Assistant,
        content: "".to_string(),
        tool_calls: Some(vec![ToolCall {
            id: "call_123".to_string(),
            r#type: "function".to_string(),
            name: "Bash".to_string(),
            arguments: serde_json::json!({"command": "ls /tmp"}),
        }]),
        ..Default::default()
    });

    // Add tool result message
    engine.messages.push(Message {
        role: MessageRole::Tool,
        content: "file1.txt\nfile2.txt".to_string(),
        tool_call_id: Some("call_123".to_string()),
        ..Default::default()
    });

    // Verify message history
    assert_eq!(engine.messages.len(), 3);
    assert_eq!(engine.messages[1].role, MessageRole::Assistant);
    assert!(engine.messages[1].tool_calls.is_some());
    assert_eq!(engine.messages[2].role, MessageRole::Tool);
    assert_eq!(
        engine.messages[2].tool_call_id,
        Some("call_123".to_string())
    );
}

#[test]
fn test_tool_result_error_handling() {
    // Test tool result with error
    let error_msg = Message {
        role: MessageRole::Tool,
        content: "Error: Permission denied".to_string(),
        tool_call_id: Some("call_err".to_string()),
        is_error: Some(true),
        ..Default::default()
    };

    assert_eq!(error_msg.is_error, Some(true));
    assert!(error_msg.content.contains("Error"));
}

// ========================================================================
// Deferred Tool Loading Tests
// ========================================================================

fn make_deferred_tool(name: &str, should_defer: bool, is_mcp: bool) -> ToolDefinition {
    let mut t = ToolDefinition {
        name: name.to_string(),
        description: format!("{} tool", name),
        input_schema: ToolInputSchema {
            schema_type: "object".to_string(),
            properties: serde_json::json!({}),
            required: None,
        },
        annotations: None,
        should_defer: if should_defer { Some(true) } else { None },
        always_load: None,
        is_mcp: if is_mcp { Some(true) } else { None },
        search_hint: Some(format!("{} capability", name.to_lowercase())),
        aliases: None,
        user_facing_name: None,
        interrupt_behavior: None,
    };
    t
}

/// Test that separate_tools_for_request correctly splits upfront vs deferred tools
#[test]
fn test_separate_tools_upfront_vs_deferred() {
    // Enable tool search for this test
    unsafe { std::env::set_var("ENABLE_TOOL_SEARCH", "1") };
    let mut engine = QueryEngine::new(QueryEngineConfig {
        model: "test-model".to_string(),
        tools: vec![
            make_deferred_tool("Bash", false, false),     // upfront
            make_deferred_tool("Read", false, false), // upfront
            make_deferred_tool("WebSearch", true, false), // deferred
            make_deferred_tool("WebFetch", true, false),  // deferred
            make_deferred_tool("mcp__slack__send", true, true), // deferred (MCP)
        ],
        cwd: "/tmp".to_string(),
        ..Default::default()
    });

    let (upfront, deferred) = engine.separate_tools_for_request();

    // 2 upfront tools
    assert_eq!(upfront.len(), 2);
    assert!(upfront.iter().any(|t| t.name == "Bash"));
    assert!(upfront.iter().any(|t| t.name == "Read"));

    // 3 deferred tools
    assert_eq!(deferred.len(), 3);
    assert!(deferred.iter().any(|t| t.name == "WebSearch"));
    assert!(deferred.iter().any(|t| t.name == "WebFetch"));
    assert!(deferred.iter().any(|t| t.name == "mcp__slack__send"));
}

/// Test that after discovering a deferred tool via tool_reference,
/// it moves from deferred to upfront on the next request
#[test]
fn test_discovered_deferred_tool_moves_to_upfront() {
    // Enable tool search for this test
    unsafe { std::env::set_var("ENABLE_TOOL_SEARCH", "1") };
    let mut engine = QueryEngine::new(QueryEngineConfig {
        model: "test-model".to_string(),
        tools: vec![
            make_deferred_tool("Bash", false, false),
            make_deferred_tool("WebSearch", true, false),
            make_deferred_tool("WebFetch", true, false),
        ],
        cwd: "/tmp".to_string(),
        ..Default::default()
    });

    // Initially, only Bash is upfront
    let (upfront, deferred) = engine.separate_tools_for_request();
    assert_eq!(upfront.len(), 1);
    assert_eq!(upfront[0].name, "Bash");
    assert_eq!(deferred.len(), 2);

    // Simulate: model called ToolSearch, got tool_reference for WebSearch
    // This is what the API response looks like after tool_reference expansion
    let tool_search_result = Message {
        role: MessageRole::User,
        content: serde_json::json!([{
            "type": "tool_result",
            "tool_use_id": "call_search_123",
            "content": [
                {"type": "tool_reference", "tool_name": "WebSearch"}
            ]
        }])
        .to_string(),
        tool_call_id: Some("call_search_123".to_string()),
        ..Default::default()
    };
    engine.messages.push(tool_search_result);

    // Now separate again - WebSearch should have moved to upfront
    let (upfront2, deferred2) = engine.separate_tools_for_request();

    assert_eq!(upfront2.len(), 2);
    assert!(upfront2.iter().any(|t| t.name == "Bash"));
    assert!(upfront2.iter().any(|t| t.name == "WebSearch"));

    assert_eq!(deferred2.len(), 1);
    assert_eq!(deferred2[0].name, "WebFetch");
}

/// Test full flow: initial call -> ToolSearch -> discover tool -> use it
/// This simulates what happens when the LLM needs to find an unregistered tool
#[test]
fn test_full_deferred_tool_discovery_flow() {
    // Enable tool search for this test
    unsafe { std::env::set_var("ENABLE_TOOL_SEARCH", "1") };
    // Scenario: LLM wants to use WebSearch but it's deferred
    // Step 1: Initial state - WebSearch is deferred, not in upfront tools
    let tools = vec![
        make_deferred_tool("Bash", false, false),
        make_deferred_tool("Read", false, false),
        make_deferred_tool("WebSearch", true, false),
    ];

    let engine = QueryEngine::new(QueryEngineConfig {
        model: "test-model".to_string(),
        tools: tools.clone(),
        cwd: "/tmp".to_string(),
        ..Default::default()
    });

    // Step 2: Get upfront tools - WebSearch should NOT be here
    let (upfront, deferred) = engine.separate_tools_for_request();
    assert_eq!(upfront.len(), 2);
    assert!(upfront.iter().all(|t| t.name != "WebSearch"));

    // Step 3: LLM sees <available-deferred-tools> block with WebSearch name
    // LLM calls ToolSearchTool with "select:WebSearch"
    // ToolSearchTool returns tool_reference block
    let tool_reference_result = ToolSearchTool::build_tool_reference_result(
        &["WebSearch".to_string()],
        "call_toolsearch_001",
    );

    // Step 4: Verify tool_reference format
    assert_eq!(tool_reference_result["type"], "tool_result");
    let content = tool_reference_result["content"].as_array().unwrap();
    assert_eq!(content.len(), 1);
    assert_eq!(content[0]["type"], "tool_reference");
    assert_eq!(content[0]["tool_name"], "WebSearch");

    // Step 5: The API expands tool_reference and model can now call WebSearch
    // Simulate the model calling WebSearch - the response appears in messages
    let mut engine2 = QueryEngine::new(QueryEngineConfig {
        model: "test-model".to_string(),
        tools: tools.clone(),
        cwd: "/tmp".to_string(),
        ..Default::default()
    });

    // Simulate tool_reference result message (what the API sends back after expansion)
    let discovered_msg = Message {
        role: MessageRole::User,
        content: serde_json::json!([{
            "type": "tool_result",
            "tool_use_id": "call_toolsearch_001",
            "content": [
                {"type": "tool_reference", "tool_name": "WebSearch"}
            ]
        }])
        .to_string(),
        tool_call_id: Some("call_toolsearch_001".to_string()),
        ..Default::default()
    };
    engine2.messages.push(discovered_msg);

    // Step 6: Now WebSearch should be in upfront tools
    let (upfront_after, deferred_after) = engine2.separate_tools_for_request();
    assert!(upfront_after.iter().any(|t| t.name == "WebSearch"));
    assert!(deferred_after.is_empty() || deferred_after.iter().all(|t| t.name != "WebSearch"));
}

/// Test that multiple deferred tools can be discovered in one ToolSearch call
#[test]
fn test_discover_multiple_deferred_tools() {
    // Enable tool search for this test
    unsafe { std::env::set_var("ENABLE_TOOL_SEARCH", "1") };
    let tools = vec![
        make_deferred_tool("Bash", false, false),
        make_deferred_tool("WebSearch", true, false),
        make_deferred_tool("WebFetch", true, false),
        make_deferred_tool("mcp__github__pr", true, true),
    ];

    let mut engine = QueryEngine::new(QueryEngineConfig {
        model: "test-model".to_string(),
        tools,
        cwd: "/tmp".to_string(),
        ..Default::default()
    });

    // Initially only Bash is upfront
    let (upfront, deferred) = engine.separate_tools_for_request();
    assert_eq!(upfront.len(), 1);
    assert_eq!(deferred.len(), 3);

    // LLM calls ToolSearch with "select:WebSearch,WebFetch"
    let multi_discovery = ToolSearchTool::build_tool_reference_result(
        &["WebSearch".to_string(), "WebFetch".to_string()],
        "call_toolsearch_002",
    );

    // Verify both tool_references are in the result
    let content = multi_discovery["content"].as_array().unwrap();
    assert_eq!(content.len(), 2);
    assert_eq!(content[0]["tool_name"], "WebSearch");
    assert_eq!(content[1]["tool_name"], "WebFetch");

    // Add the discovery to engine messages
    let discovered_msg = Message {
        role: MessageRole::User,
        content: serde_json::json!([{
            "type": "tool_result",
            "tool_use_id": "call_toolsearch_002",
            "content": [
                {"type": "tool_reference", "tool_name": "WebSearch"},
                {"type": "tool_reference", "tool_name": "WebFetch"}
            ]
        }])
        .to_string(),
        tool_call_id: Some("call_toolsearch_002".to_string()),
        ..Default::default()
    };
    engine.messages.push(discovered_msg);

    // Now both WebSearch and WebFetch should be upfront
    let (upfront_after, deferred_after) = engine.separate_tools_for_request();
    assert_eq!(upfront_after.len(), 3);
    assert!(upfront_after.iter().any(|t| t.name == "Bash"));
    assert!(upfront_after.iter().any(|t| t.name == "WebSearch"));
    assert!(upfront_after.iter().any(|t| t.name == "WebFetch"));

    // Only MCP tool remains deferred
    assert_eq!(deferred_after.len(), 1);
    assert_eq!(deferred_after[0].name, "mcp__github__pr");
}

/// Test that <available-deferred-tools> block is correctly injected into messages
#[test]
fn test_available_deferred_tools_block_injection() {
    // Enable tool search for this test
    unsafe { std::env::set_var("ENABLE_TOOL_SEARCH", "1") };
    let tools = vec![
        make_deferred_tool("Bash", false, false),
        make_deferred_tool("WebSearch", true, false),
        make_deferred_tool("WebFetch", true, false),
    ];

    let engine = QueryEngine::new(QueryEngineConfig {
        model: "test-model".to_string(),
        tools,
        cwd: "/tmp".to_string(),
        ..Default::default()
    });

    let mut api_messages = vec![
        serde_json::json!({"role": "user", "content": "Search the web for Rust"}),
        serde_json::json!({
            "role": "assistant",
            "content": "Calling tool: Bash"
        }),
    ];

    engine.maybe_inject_deferred_tools_block(&mut api_messages);

    // Should have injected the <available-deferred-tools> block
    assert_eq!(api_messages.len(), 3);
    let injected = &api_messages[0];
    let content = injected["content"].as_str().unwrap();
    assert!(content.contains("<available-deferred-tools>"));
    assert!(content.contains("WebSearch"));
    assert!(content.contains("WebFetch"));
    assert!(content.contains("ToolSearchTool"));
}

/// Test that discovered tools are NOT shown in <available-deferred-tools>
#[test]
fn test_discovered_tools_excluded_from_available_block() {
    // Enable tool search for this test
    unsafe { std::env::set_var("ENABLE_TOOL_SEARCH", "1") };
    let tools = vec![
        make_deferred_tool("Bash", false, false),
        make_deferred_tool("WebSearch", true, false),
        make_deferred_tool("WebFetch", true, false),
    ];

    let engine = QueryEngine::new(QueryEngineConfig {
        model: "test-model".to_string(),
        tools,
        cwd: "/tmp".to_string(),
        ..Default::default()
    });

    // WebSearch is already discovered - include it in api_messages
    let mut api_messages = vec![
        // Previously discovered WebSearch via tool_reference
        serde_json::json!({
            "role": "user",
            "content": serde_json::json!([{
                "type": "tool_result",
                "tool_use_id": "call_123",
                "content": [
                    {"type": "tool_reference", "tool_name": "WebSearch"}
                ]
            }]).to_string()
        }),
        serde_json::json!({"role": "user", "content": "Now fetch a URL"}),
    ];

    engine.maybe_inject_deferred_tools_block(&mut api_messages);

    // Should inject, but WebSearch should NOT be in the list
    assert_eq!(api_messages.len(), 3);
    // Find the injected block (it's inserted at position 0, before the discovered message)
    let injected = &api_messages[0];
    let content = injected["content"].as_str().unwrap();
    assert!(content.contains("WebFetch")); // Still deferred
    assert!(!content.contains("WebSearch")); // Already discovered
}

/// Test that when no deferred tools exist, nothing is injected
#[test]
fn test_no_injection_when_no_deferred_tools() {
    let tools = vec![
        make_deferred_tool("Bash", false, false),
        make_deferred_tool("Read", false, false),
    ];

    let engine = QueryEngine::new(QueryEngineConfig {
        model: "test-model".to_string(),
        tools,
        cwd: "/tmp".to_string(),
        ..Default::default()
    });

    let mut api_messages = vec![serde_json::json!({"role": "user", "content": "Read a file"})];

    engine.maybe_inject_deferred_tools_block(&mut api_messages);

    // No injection should happen
    assert_eq!(api_messages.len(), 1);
}

/// Test keyword search finds deferred tools by capability phrase (search_hint)
#[test]
fn test_keyword_search_finds_deferred_tools_by_hint() {
    let web_search = make_deferred_tool("WebSearch", true, false);
    let web_fetch = make_deferred_tool("WebFetch", true, false);
    let bash = make_deferred_tool("Bash", false, false);

    let tools = vec![&web_search, &web_fetch, &bash];

    // Search by capability
    let results = search_tools_with_keywords("search web", &tools, 5);
    assert!(results.contains(&"WebSearch".to_string()));

    let results = search_tools_with_keywords("fetch url", &tools, 5);
    assert!(results.contains(&"WebFetch".to_string()));

    // Search by tool name
    let results = search_tools_with_keywords("search", &tools, 5);
    assert!(results.contains(&"WebSearch".to_string()));
}

/// Test that the tool_reference content format matches what the API expects
#[test]
fn test_tool_reference_format_for_api_expansion() {
    // This test verifies the exact format that the API uses to expand tool_references
    let matches = vec!["WebSearch".to_string()];
    let result = ToolSearchTool::build_tool_reference_result(&matches, "call_abc");

    // The API looks for: content[].type == "tool_reference" && content[].tool_name
    let content_array = result["content"].as_array().unwrap();
    assert_eq!(content_array.len(), 1);

    let ref_block = &content_array[0];
    assert_eq!(ref_block["type"], "tool_reference");
    assert_eq!(ref_block["tool_name"], "WebSearch");

    // This is the format the API expands into the model's context
    // After expansion, the model sees the full tool schema and can call it
}

/// Test select: query parsing for ToolSearchTool
#[test]
fn test_tool_search_select_query() {
    // Single tool select
    let query = parse_tool_search_query("select:WebSearch");
    match query {
        ToolSearchQuery::Select(tools) => {
            assert_eq!(tools, vec!["WebSearch"]);
        }
        _ => panic!("Expected Select query"),
    }

    // Multi-tool select
    let query = parse_tool_search_query("select:WebSearch,WebFetch");
    match query {
        ToolSearchQuery::Select(tools) => {
            assert_eq!(tools, vec!["WebSearch", "WebFetch"]);
        }
        _ => panic!("Expected Select query"),
    }

    // Keyword query (no select: prefix)
    let query = parse_tool_search_query("find information online");
    match query {
        ToolSearchQuery::Keyword(s) => {
            assert_eq!(s, "find information online");
        }
        _ => panic!("Expected Keyword query"),
    }
}

/// Test that MCP tools are correctly identified as deferred
#[test]
fn test_mcp_tools_are_deferred() {
    let mcp_tool = make_deferred_tool("mcp__github__get_pr", false, true);
    assert!(crate::tools::deferred_tools::is_deferred_tool(&mcp_tool));

    // Even if should_defer is false, MCP tools are deferred
    let mcp_tool_no_defer = make_deferred_tool("mcp__slack__send", false, true);
    assert!(crate::tools::deferred_tools::is_deferred_tool(
        &mcp_tool_no_defer
    ));
}

/// Test that tool names are correctly parsed for keyword search
#[test]
fn test_parse_tool_name_for_search() {
    // Regular tool
    let regular = parse_tool_name("Read");
    assert!(!regular.is_mcp);

    // MCP tool
    let mcp = parse_tool_name("mcp__github__get_pull_request");
    assert!(mcp.is_mcp);
    assert_eq!(mcp.parts, vec!["github", "get", "pull", "request"]);
}

/// Test that search handles exact tool name match (fast path)
#[test]
fn test_keyword_search_exact_match_fast_path() {
    let web_search = make_deferred_tool("WebSearch", true, false);
    let tools = vec![&web_search];

    // Exact tool name match should return immediately
    let results = search_tools_with_keywords("WebSearch", &tools, 5);
    assert_eq!(results, vec!["WebSearch"]);
}

/// Test that search handles MCP prefix queries
#[test]
fn test_keyword_search_mcp_prefix() {
    let mcp_github_pr = make_deferred_tool("mcp__github__get_pr", true, true);
    let mcp_slack_send = make_deferred_tool("mcp__slack__send_message", true, true);
    let tools = vec![&mcp_github_pr, &mcp_slack_send];

    // Query by MCP server prefix
    let results = search_tools_with_keywords("mcp__github", &tools, 5);
    assert!(results.contains(&"mcp__github__get_pr".to_string()));

    let results = search_tools_with_keywords("mcp__slack", &tools, 5);
    assert!(results.contains(&"mcp__slack__send_message".to_string()));
}