tandem-tools 0.4.39

Tooling and integrations for the Tandem engine
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
1143
1144
1145
1146
1147
1148
1149
1150
#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashSet;
    use std::path::PathBuf;
    use std::sync::{Arc, Mutex, OnceLock};
    use tandem_types::ToolProgressSink;
    use tempfile::TempDir;
    use tokio::fs;
    use tokio_util::sync::CancellationToken;

    #[derive(Clone, Default)]
    struct RecordingProgressSink {
        events: Arc<Mutex<Vec<ToolProgressEvent>>>,
    }

    impl ToolProgressSink for RecordingProgressSink {
        fn publish(&self, event: ToolProgressEvent) {
            self.events.lock().expect("progress lock").push(event);
        }
    }

    struct TestTool {
        schema: ToolSchema,
    }

    #[async_trait]
    impl Tool for TestTool {
        fn schema(&self) -> ToolSchema {
            self.schema.clone()
        }

        async fn execute(&self, _args: Value) -> anyhow::Result<ToolResult> {
            Ok(ToolResult {
                output: "ok".to_string(),
                metadata: json!({}),
            })
        }

        async fn execute_with_cancel(
            &self,
            args: Value,
            _cancel: CancellationToken,
        ) -> anyhow::Result<ToolResult> {
            self.execute(args).await
        }
    }

    fn search_env_lock() -> &'static Mutex<()> {
        static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
        LOCK.get_or_init(|| Mutex::new(()))
    }

    fn clear_search_env() {
        let test_settings_path = std::env::temp_dir().join(format!(
            "tandem-search-settings-test-{}.env",
            std::process::id()
        ));
        let _ = std::fs::remove_file(&test_settings_path);
        std::env::set_var(
            "TANDEM_SEARCH_SETTINGS_FILE",
            test_settings_path.to_string_lossy().to_string(),
        );
        std::env::remove_var("TANDEM_SEARCH_BACKEND");
        std::env::remove_var("TANDEM_SEARCH_URL");
        std::env::remove_var("TANDEM_SEARXNG_URL");
        std::env::remove_var("TANDEM_SEARXNG_ENGINES");
        std::env::remove_var("TANDEM_SEARCH_TIMEOUT_MS");
        std::env::remove_var("TANDEM_EXA_API_KEY");
        std::env::remove_var("TANDEM_EXA_SEARCH_API_KEY");
        std::env::remove_var("EXA_API_KEY");
        std::env::remove_var("TANDEM_BRAVE_SEARCH_API_KEY");
        std::env::remove_var("BRAVE_SEARCH_API_KEY");
    }

    #[test]
    fn validator_rejects_array_without_items() {
        let schemas = vec![ToolSchema::new(
            "bad",
            "bad schema",
            json!({
                "type":"object",
                "properties":{"todos":{"type":"array"}}
            }),
        )];
        let err = validate_tool_schemas(&schemas).expect_err("expected schema validation failure");
        assert_eq!(err.tool_name, "bad");
        assert!(err.path.contains("properties.todos"));
    }

    #[tokio::test]
    async fn registry_schemas_are_unique_and_valid() {
        let registry = ToolRegistry::new();
        let schemas = registry.list().await;
        validate_tool_schemas(&schemas).expect("registry tool schemas should validate");
        let unique = schemas
            .iter()
            .map(|schema| schema.name.as_str())
            .collect::<HashSet<_>>();
        assert_eq!(
            unique.len(),
            schemas.len(),
            "tool schemas must be unique by name"
        );
    }

    #[tokio::test]
    async fn core_tool_schemas_include_expected_capabilities() {
        let registry = ToolRegistry::new();
        let schemas = registry.list().await;
        let schema_by_name = schemas
            .iter()
            .map(|schema| (schema.name.as_str(), schema))
            .collect::<HashMap<_, _>>();

        let read = schema_by_name.get("read").expect("read tool");
        assert!(read.capabilities.reads_workspace);
        assert!(read.capabilities.preferred_for_discovery);
        assert_eq!(
            read.capabilities.effects,
            vec![tandem_types::ToolEffect::Read]
        );

        let write = schema_by_name.get("write").expect("write tool");
        assert!(write.capabilities.writes_workspace);
        assert!(write.capabilities.requires_verification);
        assert_eq!(
            write.capabilities.effects,
            vec![tandem_types::ToolEffect::Write]
        );

        let grep = schema_by_name.get("grep").expect("grep tool");
        assert!(grep.capabilities.reads_workspace);
        assert!(grep.capabilities.preferred_for_discovery);
        assert_eq!(
            grep.capabilities.effects,
            vec![tandem_types::ToolEffect::Search]
        );

        let bash = schema_by_name.get("bash").expect("bash tool");
        assert!(bash.capabilities.destructive);
        assert!(bash.capabilities.network_access);
        assert_eq!(
            bash.capabilities.effects,
            vec![tandem_types::ToolEffect::Execute]
        );

        let webfetch = schema_by_name.get("webfetch").expect("webfetch tool");
        assert!(webfetch.capabilities.network_access);
        assert!(webfetch.capabilities.preferred_for_discovery);
        assert_eq!(
            webfetch.capabilities.effects,
            vec![tandem_types::ToolEffect::Fetch]
        );

        let apply_patch = schema_by_name.get("apply_patch").expect("apply_patch tool");
        assert!(apply_patch.capabilities.reads_workspace);
        assert!(apply_patch.capabilities.writes_workspace);
        assert!(apply_patch.capabilities.requires_verification);
        assert_eq!(
            apply_patch.capabilities.effects,
            vec![tandem_types::ToolEffect::Patch]
        );
    }

    fn grep_args(root: &Path, pattern: &str) -> Value {
        let root = root.to_string_lossy().to_string();
        json!({
            "pattern": pattern,
            "path": root.clone(),
            "__workspace_root": root.clone(),
            "__effective_cwd": root,
        })
    }

    #[tokio::test]
    async fn grep_tool_reports_matches_while_skipping_ignored_and_binary_paths() {
        let tempdir = TempDir::new().expect("tempdir");
        let root = tempdir.path();
        let visible = root.join("src").join("nested").join("notes.txt");
        let ignored = root.join(".tandem").join("private").join("secret.txt");
        let binary = root.join("binary.bin");

        std::fs::create_dir_all(visible.parent().expect("visible parent"))
            .expect("create visible dir");
        std::fs::create_dir_all(ignored.parent().expect("ignored parent"))
            .expect("create ignored dir");
        std::fs::write(&visible, "first line\nneedle here\nlast line").expect("write visible file");
        std::fs::write(&ignored, "needle should stay hidden").expect("write ignored file");
        std::fs::write(&binary, b"\0needle after null\n").expect("write binary file");

        let tool = GrepTool;
        let result = tool
            .execute(grep_args(root, "needle"))
            .await
            .expect("grep result");

        assert_eq!(result.metadata["count"], json!(1));
        assert_eq!(
            result.output,
            format!("{}:2:needle here", visible.display())
        );
        assert!(!result.output.contains(".tandem/private/secret.txt"));
    }

    #[tokio::test]
    async fn grep_tool_streams_chunk_and_done_events() {
        let tempdir = TempDir::new().expect("tempdir");
        let root = tempdir.path();
        let first = root.join("a.txt");
        let second = root.join("b.txt");

        std::fs::write(
            &first,
            [
                "needle a1",
                "needle a2",
                "needle a3",
                "needle a4",
                "needle a5",
                "needle a6",
            ]
            .join("\n"),
        )
        .expect("write first file");
        std::fs::write(
            &second,
            [
                "needle b1",
                "needle b2",
                "needle b3",
                "needle b4",
                "needle b5",
                "needle b6",
            ]
            .join("\n"),
        )
        .expect("write second file");

        let sink = RecordingProgressSink::default();
        let events = Arc::clone(&sink.events);
        let progress: SharedToolProgressSink = Arc::new(sink);

        let tool = GrepTool;
        let result = tool
            .execute_with_progress(
                grep_args(root, "needle"),
                CancellationToken::new(),
                Some(progress),
            )
            .await
            .expect("grep result");

        assert_eq!(result.metadata["count"], json!(12));
        let lines = result.output.lines().collect::<Vec<_>>();
        assert_eq!(lines.len(), 12);
        assert!(lines[0].starts_with(&first.display().to_string()));
        assert!(lines[11].starts_with(&second.display().to_string()));

        let events = events.lock().expect("events").clone();
        assert!(!events.is_empty());
        assert!(events
            .iter()
            .any(|event| event.event_type == "tool.search.chunk"));
        let done = events
            .iter()
            .rev()
            .find(|event| event.event_type == "tool.search.done")
            .expect("done event");
        assert_eq!(done.properties["count"], json!(12));
        assert_eq!(done.properties["tool"], json!("grep"));
    }

    #[tokio::test]
    async fn grep_tool_caps_results_at_100_hits() {
        let tempdir = TempDir::new().expect("tempdir");
        let root = tempdir.path();
        let source = root.join("many.txt");
        let lines = (1..=120)
            .map(|idx| format!("match line {}", idx))
            .collect::<Vec<_>>()
            .join("\n");
        std::fs::write(&source, lines).expect("write source file");

        let tool = GrepTool;
        let result = tool
            .execute(grep_args(root, "match"))
            .await
            .expect("grep result");

        assert_eq!(result.metadata["count"], json!(100));
        assert_eq!(result.output.lines().count(), 100);
        assert!(result.output.contains("match line 100"));
        assert!(!result.output.contains("match line 101"));
    }

    #[tokio::test]
    async fn grep_tool_rejects_invalid_regex_patterns() {
        let tempdir = TempDir::new().expect("tempdir");
        let root = tempdir.path();
        std::fs::write(root.join("notes.txt"), "needle").expect("write file");

        let tool = GrepTool;
        let err = tool.execute(grep_args(root, "(")).await;

        assert!(err.is_err(), "expected invalid regex to fail");
    }

    #[tokio::test]
    async fn mcp_server_names_returns_unique_sorted_names() {
        let registry = ToolRegistry::new();
        registry
            .register_tool(
                "mcp.notion.search_pages".to_string(),
                Arc::new(TestTool {
                    schema: ToolSchema::new("mcp.notion.search_pages", "search", json!({})),
                }),
            )
            .await;
        registry
            .register_tool(
                "mcp.github.list_prs".to_string(),
                Arc::new(TestTool {
                    schema: ToolSchema::new("mcp.github.list_prs", "list", json!({})),
                }),
            )
            .await;
        registry
            .register_tool(
                "mcp.github.get_pr".to_string(),
                Arc::new(TestTool {
                    schema: ToolSchema::new("mcp.github.get_pr", "get", json!({})),
                }),
            )
            .await;

        let servers = registry.mcp_server_names().await;
        assert_eq!(servers, vec!["github".to_string(), "notion".to_string()]);
    }

    #[tokio::test]
    async fn unregister_by_prefix_removes_index_vectors_for_removed_tools() {
        let registry = ToolRegistry::new();
        registry
            .register_tool(
                "mcp.test.search".to_string(),
                Arc::new(TestTool {
                    schema: ToolSchema::new("mcp.test.search", "search", json!({})),
                }),
            )
            .await;
        registry
            .register_tool(
                "mcp.test.get".to_string(),
                Arc::new(TestTool {
                    schema: ToolSchema::new("mcp.test.get", "get", json!({})),
                }),
            )
            .await;

        registry
            .tool_vectors
            .write()
            .await
            .insert("mcp.test.search".to_string(), vec![1.0, 0.0, 0.0]);
        registry
            .tool_vectors
            .write()
            .await
            .insert("mcp.test.get".to_string(), vec![0.0, 1.0, 0.0]);

        let removed = registry.unregister_by_prefix("mcp.test.").await;
        assert_eq!(removed, 2);
        let vectors = registry.tool_vectors.read().await;
        assert!(!vectors.contains_key("mcp.test.search"));
        assert!(!vectors.contains_key("mcp.test.get"));
    }

    #[test]
    fn websearch_query_extraction_accepts_aliases_and_nested_shapes() {
        let direct = json!({"query":"meaning of life"});
        assert_eq!(
            extract_websearch_query(&direct).as_deref(),
            Some("meaning of life")
        );

        let alias = json!({"q":"hello"});
        assert_eq!(extract_websearch_query(&alias).as_deref(), Some("hello"));

        let nested = json!({"arguments":{"search_query":"rust tokio"}});
        assert_eq!(
            extract_websearch_query(&nested).as_deref(),
            Some("rust tokio")
        );

        let as_string = json!("find docs");
        assert_eq!(
            extract_websearch_query(&as_string).as_deref(),
            Some("find docs")
        );

        let malformed = json!({"query":"websearch query</arg_key><arg_value>taj card what is it benefits how to apply</arg_value>"});
        assert_eq!(
            extract_websearch_query(&malformed).as_deref(),
            Some("taj card what is it benefits how to apply")
        );
    }

    #[test]
    fn websearch_limit_extraction_clamps_and_reads_nested_fields() {
        assert_eq!(extract_websearch_limit(&json!({"limit": 100})), Some(10));
        assert_eq!(
            extract_websearch_limit(&json!({"arguments":{"numResults": 0}})),
            Some(1)
        );
        assert_eq!(
            extract_websearch_limit(&json!({"input":{"num_results": 6}})),
            Some(6)
        );
    }

    #[test]
    fn search_backend_defaults_to_searxng_when_configured() {
        let _guard = search_env_lock().lock().expect("env lock");
        clear_search_env();
        std::env::set_var("TANDEM_SEARXNG_URL", "http://localhost:8080");

        let backend = SearchBackend::from_env();

        match backend {
            SearchBackend::Searxng { base_url, .. } => {
                assert_eq!(base_url, "http://localhost:8080");
            }
            other => panic!("expected searxng backend, got {other:?}"),
        }

        clear_search_env();
    }

    #[test]
    fn search_backend_defaults_to_tandem_when_search_url_configured() {
        let _guard = search_env_lock().lock().expect("env lock");
        clear_search_env();
        std::env::set_var("TANDEM_SEARCH_URL", "https://search.tandem.ac");

        let backend = SearchBackend::from_env();

        match backend {
            SearchBackend::Tandem { base_url, .. } => {
                assert_eq!(base_url, "https://search.tandem.ac");
            }
            other => panic!("expected tandem backend, got {other:?}"),
        }

        clear_search_env();
    }

    #[test]
    fn search_backend_explicit_auto_is_supported() {
        let _guard = search_env_lock().lock().expect("env lock");
        clear_search_env();
        std::env::set_var("TANDEM_SEARCH_BACKEND", "auto");
        std::env::set_var("TANDEM_BRAVE_SEARCH_API_KEY", "brave-test-key");
        std::env::set_var("TANDEM_EXA_API_KEY", "exa-test-key");

        let backend = SearchBackend::from_env();

        match backend {
            SearchBackend::Auto { backends } => {
                assert_eq!(backends.len(), 2);
                assert!(matches!(backends[0], SearchBackend::Brave { .. }));
                assert!(matches!(backends[1], SearchBackend::Exa { .. }));
            }
            other => panic!("expected auto backend, got {other:?}"),
        }

        clear_search_env();
    }

    #[test]
    fn search_backend_implicit_auto_failover_when_multiple_backends_are_configured() {
        let _guard = search_env_lock().lock().expect("env lock");
        clear_search_env();
        std::env::set_var("TANDEM_BRAVE_SEARCH_API_KEY", "brave-test-key");
        std::env::set_var("TANDEM_EXA_API_KEY", "exa-test-key");

        let backend = SearchBackend::from_env();

        match backend {
            SearchBackend::Auto { backends } => {
                assert_eq!(backends.len(), 2);
                assert!(matches!(backends[0], SearchBackend::Brave { .. }));
                assert!(matches!(backends[1], SearchBackend::Exa { .. }));
            }
            other => panic!("expected auto backend, got {other:?}"),
        }

        clear_search_env();
    }

    #[test]
    fn search_backend_supports_legacy_exa_env_key() {
        let _guard = search_env_lock().lock().expect("env lock");
        clear_search_env();
        std::env::set_var("TANDEM_SEARCH_BACKEND", "exa");
        std::env::set_var("TANDEM_EXA_SEARCH_API_KEY", "legacy-exa-test-key");

        let backend = SearchBackend::from_env();

        match backend {
            SearchBackend::Exa { api_key, .. } => {
                assert_eq!(api_key, "legacy-exa-test-key");
            }
            other => panic!("expected exa backend, got {other:?}"),
        }

        clear_search_env();
    }

    #[test]
    fn normalize_brave_results_accepts_standard_web_payload_rows() {
        let raw = vec![json!({
            "title": "Agentic workflows",
            "url": "https://example.com/agentic",
            "description": "A practical overview of agentic workflows.",
            "profile": {
                "long_name": "example.com"
            }
        })];

        let results = normalize_brave_results(&raw, 5);

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].title, "Agentic workflows");
        assert_eq!(results[0].url, "https://example.com/agentic");
        assert_eq!(
            results[0].snippet,
            "A practical overview of agentic workflows."
        );
        assert_eq!(results[0].source, "brave:example.com");
    }

    #[test]
    fn search_backend_explicit_none_disables_websearch() {
        let _guard = search_env_lock().lock().expect("env lock");
        clear_search_env();
        std::env::set_var("TANDEM_SEARCH_BACKEND", "none");
        std::env::set_var("TANDEM_SEARXNG_URL", "http://localhost:8080");

        let backend = SearchBackend::from_env();

        assert!(matches!(backend, SearchBackend::Disabled { .. }));

        clear_search_env();
    }

    #[tokio::test]
    async fn tool_registry_includes_websearch_by_default() {
        let _guard = search_env_lock().lock().expect("env lock");
        clear_search_env();

        let registry = ToolRegistry::new();
        let names = registry
            .list()
            .await
            .into_iter()
            .map(|schema| schema.name)
            .collect::<Vec<_>>();

        assert!(names.iter().any(|name| name == "websearch"));

        clear_search_env();
    }

    #[tokio::test]
    async fn tool_registry_keeps_websearch_registered_when_search_backend_explicitly_disabled() {
        let _guard = search_env_lock().lock().expect("env lock");
        clear_search_env();
        std::env::set_var("TANDEM_SEARCH_BACKEND", "none");

        let registry = ToolRegistry::new();
        let names = registry
            .list()
            .await
            .into_iter()
            .map(|schema| schema.name)
            .collect::<Vec<_>>();

        assert!(names.iter().any(|name| name == "websearch"));

        clear_search_env();
    }

    #[test]
    fn search_backend_reads_managed_settings_file_live_without_restart() {
        let _guard = search_env_lock().lock().expect("env lock");
        clear_search_env();

        let temp_dir = TempDir::new().expect("temp dir");
        let settings_path = temp_dir.path().join("engine.env");
        std::env::set_var(
            "TANDEM_SEARCH_SETTINGS_FILE",
            settings_path.to_string_lossy().to_string(),
        );

        std::fs::write(
            &settings_path,
            "TANDEM_SEARCH_BACKEND=brave\nTANDEM_BRAVE_SEARCH_API_KEY=brave-live-key\n",
        )
        .expect("write brave settings");
        let first = SearchBackend::from_env();
        match first {
            SearchBackend::Brave { api_key, .. } => {
                assert_eq!(api_key, "brave-live-key");
            }
            other => panic!("expected brave backend, got {other:?}"),
        }

        std::fs::write(
            &settings_path,
            "TANDEM_SEARCH_BACKEND=exa\nTANDEM_EXA_API_KEY=exa-live-key\n",
        )
        .expect("write exa settings");
        let second = SearchBackend::from_env();
        match second {
            SearchBackend::Exa { api_key, .. } => {
                assert_eq!(api_key, "exa-live-key");
            }
            other => panic!("expected exa backend, got {other:?}"),
        }

        clear_search_env();
    }

    #[test]
    fn normalize_searxng_results_preserves_title_url_and_engine() {
        let results = normalize_searxng_results(
            &[json!({
                "title": "Tandem Docs",
                "url": "https://docs.tandem.ac/",
                "content": "Official documentation for Tandem.",
                "engine": "duckduckgo"
            })],
            8,
        );

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].title, "Tandem Docs");
        assert_eq!(results[0].url, "https://docs.tandem.ac/");
        assert_eq!(results[0].snippet, "Official documentation for Tandem.");
        assert_eq!(results[0].source, "searxng:duckduckgo");
    }

    #[test]
    fn test_html_stripping_and_markdown_reduction() {
        let html = r#"
            <!DOCTYPE html>
            <html>
            <head>
                <title>Test Page</title>
                <style>
                    body { color: red; }
                </style>
                <script>
                    console.log("noisy script");
                </script>
            </head>
            <body>
                <h1>Hello World</h1>
                <p>This is a <a href="https://example.com">link</a>.</p>
                <noscript>Enable JS</noscript>
            </body>
            </html>
        "#;

        let cleaned = strip_html_noise(html);
        assert!(!cleaned.contains("noisy script"));
        assert!(!cleaned.contains("color: red"));
        assert!(!cleaned.contains("Enable JS"));
        assert!(cleaned.contains("Hello World"));

        let markdown = html2md::parse_html(&cleaned);
        let text = markdown_to_text(&markdown);

        // Raw length includes all the noise
        let raw_len = html.len();
        // Markdown length should be significantly smaller
        let md_len = markdown.len();

        println!("Raw: {}, Markdown: {}", raw_len, md_len);
        assert!(
            md_len < raw_len / 2,
            "Markdown should be < 50% of raw HTML size"
        );
        assert!(text.contains("Hello World"));
        assert!(text.contains("link"));
    }

    #[test]
    fn memory_scope_defaults_to_hidden_context() {
        let args = json!({
            "__session_id": "session-123",
            "__project_id": "workspace-abc"
        });
        assert_eq!(memory_session_id(&args).as_deref(), Some("session-123"));
        assert_eq!(memory_project_id(&args).as_deref(), Some("workspace-abc"));
        assert!(global_memory_enabled(&args));
    }

    #[test]
    fn memory_scope_policy_can_disable_global_visibility() {
        let args = json!({
            "__session_id": "session-123",
            "__project_id": "workspace-abc",
            "__memory_max_visible_scope": "project"
        });
        assert_eq!(memory_visible_scope(&args), MemoryVisibleScope::Project);
        assert!(!global_memory_enabled(&args));
    }

    #[test]
    fn memory_db_path_ignores_public_db_path_arg() {
        std::env::set_var("TANDEM_MEMORY_DB_PATH", "/tmp/global-memory.sqlite");
        let resolved = resolve_memory_db_path(&json!({
            "db_path": "/home/user123/tandem"
        }));
        assert_eq!(resolved, PathBuf::from("/tmp/global-memory.sqlite"));
        std::env::remove_var("TANDEM_MEMORY_DB_PATH");
    }

    #[test]
    fn memory_db_path_accepts_hidden_override() {
        std::env::set_var("TANDEM_MEMORY_DB_PATH", "/tmp/global-memory.sqlite");
        let resolved = resolve_memory_db_path(&json!({
            "__memory_db_path": "/tmp/internal-memory.sqlite",
            "db_path": "/home/user123/tandem"
        }));
        assert_eq!(resolved, PathBuf::from("/tmp/internal-memory.sqlite"));
        std::env::remove_var("TANDEM_MEMORY_DB_PATH");
    }

    #[tokio::test]
    async fn memory_search_uses_global_by_default() {
        let tool = MemorySearchTool;
        let result = tool
            .execute(json!({
                "query": "global pattern",
                "tier": "global"
            }))
            .await
            .expect("memory_search should return ToolResult");
        assert!(
            result.output.contains("memory database not found")
                || result.output.contains("memory embeddings unavailable")
        );
        assert_eq!(result.metadata["ok"], json!(false));
        let reason = result
            .metadata
            .get("reason")
            .and_then(|v| v.as_str())
            .unwrap_or_default();
        assert!(matches!(
            reason,
            "memory_db_missing" | "embeddings_unavailable"
        ));
    }

    #[tokio::test]
    async fn memory_store_uses_hidden_project_scope_by_default() {
        let tool = MemoryStoreTool;
        let result = tool
            .execute(json!({
                "content": "remember this",
                "__session_id": "session-123",
                "__project_id": "workspace-abc"
            }))
            .await
            .expect("memory_store should return ToolResult");
        assert!(
            result.output.contains("memory embeddings unavailable")
                || result.output.contains("memory database not found")
        );
        let reason = result
            .metadata
            .get("reason")
            .and_then(|v| v.as_str())
            .unwrap_or_default();
        assert!(matches!(
            reason,
            "embeddings_unavailable" | "memory_db_missing"
        ));
    }

    #[tokio::test]
    async fn memory_delete_uses_hidden_project_scope_by_default() {
        let tool = MemoryDeleteTool;
        let result = tool
            .execute(json!({
                "chunk_id": "chunk-123",
                "__session_id": "session-123",
                "__project_id": "workspace-abc",
                "__memory_db_path": "/tmp/tandem-memory-delete-test.sqlite"
            }))
            .await
            .expect("memory_delete should return ToolResult");
        assert_eq!(result.metadata["tier"], json!("project"));
        assert_eq!(result.metadata["project_id"], json!("workspace-abc"));
        assert!(matches!(
            result
                .metadata
                .get("reason")
                .and_then(|v| v.as_str())
                .unwrap_or_default(),
            "not_found"
        ));
    }

    #[test]
    fn translate_windows_ls_with_all_flag() {
        let translated = translate_windows_shell_command("ls -la").expect("translation");
        assert!(translated.contains("Get-ChildItem"));
        assert!(translated.contains("-Force"));
    }

    #[test]
    fn translate_windows_find_name_pattern() {
        let translated =
            translate_windows_shell_command("find . -type f -name \"*.rs\"").expect("translation");
        assert!(translated.contains("Get-ChildItem"));
        assert!(translated.contains("-Recurse"));
        assert!(translated.contains("-Filter"));
    }

    #[test]
    fn windows_guardrail_blocks_untranslatable_unix_command() {
        assert_eq!(
            windows_guardrail_reason("sed -n '1,5p' README.md"),
            Some("unix_command_untranslatable")
        );
    }

    #[test]
    fn path_policy_rejects_tool_markup_and_globs() {
        assert!(resolve_tool_path(
            "<tool_call><function=glob><parameter=pattern>**/*</parameter></function></tool_call>",
            &json!({})
        )
        .is_none());
        assert!(resolve_tool_path("**/*", &json!({})).is_none());
        assert!(resolve_tool_path("/", &json!({})).is_none());
        assert!(resolve_tool_path("C:\\", &json!({})).is_none());
    }

    #[tokio::test]
    async fn glob_allows_tandem_artifact_paths() {
        let root =
            std::env::temp_dir().join(format!("tandem-glob-artifacts-{}", uuid_like(now_ms_u64())));
        let artifacts_dir = root.join(".tandem").join("artifacts");
        std::fs::create_dir_all(&artifacts_dir).expect("create artifacts dir");
        let artifact = artifacts_dir.join("report.json");
        std::fs::write(&artifact, "{\"ok\":true}").expect("write artifact");

        let tool = GlobTool;
        let result = tool
            .execute(json!({
                "pattern": ".tandem/artifacts/*.json",
                "__workspace_root": root.to_string_lossy().to_string(),
                "__effective_cwd": root.to_string_lossy().to_string(),
            }))
            .await
            .expect("glob result");

        assert!(
            result.output.contains(".tandem/artifacts/report.json"),
            "expected artifact path in glob output, got: {}",
            result.output
        );
    }

    #[tokio::test]
    async fn glob_still_hides_non_artifact_tandem_paths() {
        let root =
            std::env::temp_dir().join(format!("tandem-glob-hidden-{}", uuid_like(now_ms_u64())));
        let tandem_dir = root.join(".tandem");
        let artifacts_dir = tandem_dir.join("artifacts");
        std::fs::create_dir_all(&artifacts_dir).expect("create tandem dirs");
        std::fs::write(tandem_dir.join("secrets.json"), "{\"hidden\":true}")
            .expect("write hidden file");

        let tool = GlobTool;
        let result = tool
            .execute(json!({
                "pattern": ".tandem/*.json",
                "__workspace_root": root.to_string_lossy().to_string(),
                "__effective_cwd": root.to_string_lossy().to_string(),
            }))
            .await
            .expect("glob result");

        assert!(
            result.output.trim().is_empty(),
            "expected non-artifact tandem paths to stay hidden, got: {}",
            result.output
        );
    }

    #[test]
    fn normalize_recursive_wildcard_pattern_fixes_common_invalid_forms() {
        assert_eq!(
            normalize_recursive_wildcard_pattern("docs/**.md").as_deref(),
            Some("docs/**/*.md")
        );
        assert_eq!(
            normalize_recursive_wildcard_pattern("src/**README*").as_deref(),
            Some("src/**/README*")
        );
        assert_eq!(
            normalize_recursive_wildcard_pattern("**.{md,mdx,txt}").as_deref(),
            Some("**/*.{md,mdx,txt}")
        );
        assert_eq!(normalize_recursive_wildcard_pattern("docs/**/*.md"), None);
    }

    #[tokio::test]
    async fn glob_recovers_from_invalid_recursive_wildcard_syntax() {
        let root =
            std::env::temp_dir().join(format!("tandem-glob-recover-{}", uuid_like(now_ms_u64())));
        let docs_dir = root.join("docs").join("guides");
        std::fs::create_dir_all(&docs_dir).expect("create docs dir");
        let guide = docs_dir.join("intro.md");
        std::fs::write(&guide, "# intro").expect("write guide");

        let tool = GlobTool;
        let result = tool
            .execute(json!({
                "pattern": "docs/**.md",
                "__workspace_root": root.to_string_lossy().to_string(),
                "__effective_cwd": root.to_string_lossy().to_string(),
            }))
            .await
            .expect("glob result");

        assert!(
            result.output.contains("docs/guides/intro.md"),
            "expected recovered glob output, got: {}",
            result.output
        );
        assert_eq!(
            result.metadata["effective_pattern"],
            json!(format!("{}/docs/**/*.md", root.to_string_lossy()))
        );
    }

    #[cfg(windows)]
    #[test]
    fn path_policy_allows_windows_verbatim_paths_within_workspace() {
        let args = json!({
            "__workspace_root": r"C:\tandem-examples",
            "__effective_cwd": r"C:\tandem-examples\docs"
        });
        assert!(resolve_tool_path(r"\\?\C:\tandem-examples\docs\index.html", &args).is_some());
    }

    #[cfg(not(windows))]
    #[test]
    fn path_policy_allows_absolute_linux_paths_within_workspace() {
        let args = json!({
            "__workspace_root": "/tmp/tandem-examples",
            "__effective_cwd": "/tmp/tandem-examples/docs"
        });
        assert!(resolve_tool_path("/tmp/tandem-examples/docs/index.html", &args).is_some());
        assert!(resolve_tool_path("/etc/passwd", &args).is_none());
    }

    #[test]
    fn read_fallback_resolves_unique_suffix_filename() {
        let root =
            std::env::temp_dir().join(format!("tandem-read-fallback-{}", uuid_like(now_ms_u64())));
        std::fs::create_dir_all(&root).expect("create root");
        let target = root.join("T1011U kitöltési útmutató.pdf");
        std::fs::write(&target, b"stub").expect("write test file");

        let args = json!({
            "__workspace_root": root.to_string_lossy().to_string(),
            "__effective_cwd": root.to_string_lossy().to_string()
        });
        let resolved = resolve_read_path_fallback("útmutató.pdf", &args)
            .expect("expected unique suffix match");
        assert_eq!(resolved, target);

        let _ = std::fs::remove_dir_all(&root);
    }

    #[tokio::test]
    async fn write_tool_rejects_empty_content_by_default() {
        let tool = WriteTool;
        let result = tool
            .execute(json!({
                "path":"target/write_guard_test.txt",
                "content":""
            }))
            .await
            .expect("write tool should return ToolResult");
        assert!(result.output.contains("non-empty `content`"));
        assert_eq!(result.metadata["reason"], json!("empty_content"));
        assert!(!Path::new("target/write_guard_test.txt").exists());
    }

    #[tokio::test]
    async fn registry_resolves_default_api_namespaced_tool() {
        let registry = ToolRegistry::new();
        let result = registry
            .execute("default_api:read", json!({"path":"Cargo.toml"}))
            .await
            .expect("registry execute should return ToolResult");
        assert!(!result.output.starts_with("Unknown tool:"));
    }

    #[tokio::test]
    async fn batch_resolves_default_api_namespaced_tool() {
        let tool = BatchTool;
        let result = tool
            .execute(json!({
                "tool_calls":[
                    {"tool":"default_api:read","args":{"path":"Cargo.toml"}}
                ]
            }))
            .await
            .expect("batch should return ToolResult");
        assert!(!result.output.contains("Unknown tool: default_api:read"));
    }

    #[tokio::test]
    async fn batch_prefers_name_when_tool_is_default_api_wrapper() {
        let tool = BatchTool;
        let result = tool
            .execute(json!({
                "tool_calls":[
                    {"tool":"default_api","name":"read","args":{"path":"Cargo.toml"}}
                ]
            }))
            .await
            .expect("batch should return ToolResult");
        assert!(!result.output.contains("Unknown tool: default_api"));
    }

    #[tokio::test]
    async fn batch_resolves_nested_function_name_for_wrapper_tool() {
        let tool = BatchTool;
        let result = tool
            .execute(json!({
                "tool_calls":[
                    {
                        "tool":"default_api",
                        "function":{"name":"read"},
                        "args":{"path":"Cargo.toml"}
                    }
                ]
            }))
            .await
            .expect("batch should return ToolResult");
        assert!(!result.output.contains("Unknown tool: default_api"));
    }

    #[tokio::test]
    async fn batch_drops_wrapper_calls_without_resolvable_name() {
        let tool = BatchTool;
        let result = tool
            .execute(json!({
                "tool_calls":[
                    {"tool":"default_api","args":{"path":"Cargo.toml"}}
                ]
            }))
            .await
            .expect("batch should return ToolResult");
        assert_eq!(result.metadata["count"], json!(0));
    }

    #[test]
    fn sanitize_member_name_normalizes_agent_aliases() {
        assert_eq!(sanitize_member_name("A2").expect("valid"), "A2");
        assert_eq!(sanitize_member_name("a7").expect("valid"), "A7");
        assert_eq!(
            sanitize_member_name("  qa reviewer ").expect("valid"),
            "qa-reviewer"
        );
        assert!(sanitize_member_name("   ").is_err());
    }

    #[tokio::test]
    async fn next_default_member_name_skips_existing_indices() {
        let root = std::env::temp_dir().join(format!(
            "tandem-agent-team-test-{}",
            uuid_like(now_ms_u64())
        ));
        let paths = AgentTeamPaths::new(root.join(".tandem"));
        let team_name = "alpha";
        fs::create_dir_all(paths.team_dir(team_name))
            .await
            .expect("create team dir");
        write_json_file(
            paths.members_file(team_name),
            &json!([
                {"name":"A1"},
                {"name":"A2"},
                {"name":"agent-x"},
                {"name":"A5"}
            ]),
        )
        .await
        .expect("write members");

        let next = next_default_member_name(&paths, team_name)
            .await
            .expect("next member");
        assert_eq!(next, "A6");

        let _ =
            fs::remove_dir_all(PathBuf::from(paths.root().parent().unwrap_or(paths.root()))).await;
    }
}

async fn find_symbol_references(symbol: &str, root: &Path) -> String {
    if symbol.trim().is_empty() {
        return "missing symbol".to_string();
    }
    let escaped = regex::escape(symbol);
    let re = Regex::new(&format!(r"\b{}\b", escaped));
    let Ok(re) = re else {
        return "invalid symbol".to_string();
    };
    let mut refs = Vec::new();
    for entry in WalkBuilder::new(root).build().flatten() {
        if !entry.file_type().map(|t| t.is_file()).unwrap_or(false) {
            continue;
        }
        let path = entry.path();
        if let Ok(content) = fs::read_to_string(path).await {
            for (idx, line) in content.lines().enumerate() {
                if re.is_match(line) {
                    refs.push(format!("{}:{}:{}", path.display(), idx + 1, line.trim()));
                    if refs.len() >= 200 {
                        return refs.join("\n");
                    }
                }
            }
        }
    }
    refs.join("\n")
}