rx4 0.3.11

The agent harness engine — loop, tools, providers, sessions, permissions, computer-use
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
//! Built-in coding tools: read, write, edit, bash, grep, find, ls.
//! Uses rayon for parallel search (grok pattern).

use crate::agent::{ToolContext, ToolDefinition, ToolEffect, ToolFuture, ToolRegistry, ToolResult};
use crate::mode::Scope;
use crate::subagent::{SubagentConfig, SubagentManager};
use dashmap::DashMap;
use parking_lot::Mutex;
use std::io::Read;
use std::process::{Command, Stdio};
use std::sync::{Arc, OnceLock};
use std::time::{Duration, Instant};
use tracing::debug;

#[cfg(feature = "builtin-tools")]
use rayon::prelude::*;

pub fn register_builtin_tools(registry: &mut ToolRegistry) {
    registry.register(
        ToolDefinition::new_fn(
            "read",
            "Read the contents of a file at the given path. Returns content with line numbers.",
            r#"{"type":"object","properties":{"path":{"type":"string"},"offset":{"type":"integer"},"limit":{"type":"integer"}},"required":["path"]}"#,
            exec_read,
        )
        .with_effect(ToolEffect::Read),
    );
    registry.register(
        ToolDefinition::new_fn(
            "write",
            "Write content to a file, creating or overwriting it.",
            r#"{"type":"object","properties":{"path":{"type":"string"},"content":{"type":"string"}},"required":["path","content"]}"#,
            exec_write,
        )
        .with_effect(ToolEffect::Write),
    );
    registry.register(
        ToolDefinition::new_fn(
            "edit",
            "Perform a string replacement in a file. old_string must be unique.",
            r#"{"type":"object","properties":{"path":{"type":"string"},"old_string":{"type":"string"},"new_string":{"type":"string"}},"required":["path","old_string","new_string"]}"#,
            exec_edit,
        )
        .with_effect(ToolEffect::Write),
    );
    registry.register(
        ToolDefinition::new_fn(
            "bash",
            "Execute a shell command and return stdout/stderr. Optional timeout in seconds.",
            r#"{"type":"object","properties":{"command":{"type":"string"},"cwd":{"type":"string"},"timeout":{"type":"integer"}},"required":["command"]}"#,
            exec_bash,
        )
        .with_effect(ToolEffect::Process),
    );
    registry.register(
        ToolDefinition::new_fn(
            "grep",
            "Search file contents using regex. Returns matching lines with context.",
            r#"{"type":"object","properties":{"pattern":{"type":"string"},"path":{"type":"string"},"context":{"type":"integer"}},"required":["pattern"]}"#,
            exec_grep,
        )
        .with_effect(ToolEffect::Read),
    );
    registry.register(
        ToolDefinition::new_fn(
            "find",
            "Find files by glob pattern. Uses rayon for parallel traversal.",
            r#"{"type":"object","properties":{"pattern":{"type":"string"},"path":{"type":"string"}},"required":["pattern"]}"#,
            exec_find,
        )
        .with_effect(ToolEffect::Read),
    );
    registry.register(
        ToolDefinition::new_fn(
            "ls",
            "List entries in a directory.",
            r#"{"type":"object","properties":{"path":{"type":"string"}},"required":["path"]}"#,
            exec_ls,
        )
        .with_effect(ToolEffect::Read),
    );
    registry.register(
        ToolDefinition::new_fn(
            "web_fetch",
            "HTTP GET a URL and return response text (truncated). Requires providers feature.",
            r#"{"type":"object","properties":{"url":{"type":"string"},"max_bytes":{"type":"integer"}},"required":["url"]}"#,
            exec_web_fetch,
        )
        .with_effect(ToolEffect::Network),
    );
    registry.register(
        ToolDefinition::new_fn(
            "todo",
            "Manage an in-memory session todo list. Actions: list, add, update, complete, clear.",
            r#"{"type":"object","properties":{"action":{"type":"string","enum":["list","add","update","complete","clear"]},"items":{"type":"array","items":{"type":"object","properties":{"id":{"type":"string"},"content":{"type":"string"},"status":{"type":"string"}},"required":["content"]}}},"required":["action"]}"#,
            exec_todo,
        )
        .with_effect(ToolEffect::Write),
    );
    registry.register(
        ToolDefinition::new_fn(
            "spawn_agent",
            "Spawn a nested subagent with a prompt. Uses ToolContext provider/tools when present.",
            r#"{"type":"object","properties":{"prompt":{"type":"string"},"name":{"type":"string"},"model":{"type":"string"},"isolate":{"type":"boolean"}},"required":["prompt"]}"#,
            exec_spawn_agent,
        )
        .with_effect(ToolEffect::Process),
    );
    registry.register(
        ToolDefinition::new_fn(
            "enter_plan_mode",
            "Request Plan scope and return plan-mode instructions for the model.",
            r#"{"type":"object","properties":{}}"#,
            exec_enter_plan_mode,
        )
        .with_effect(ToolEffect::Read),
    );
    registry.register(
        ToolDefinition::new_fn(
            "exit_plan_mode",
            "Request Coding scope and leave plan mode.",
            r#"{"type":"object","properties":{}}"#,
            exec_exit_plan_mode,
        )
        .with_effect(ToolEffect::Read),
    );
    registry.register(
        ToolDefinition::new_fn(
            "lsp_diagnostics",
            "Get LSP diagnostics for a document URI and language.",
            r#"{"type":"object","properties":{"uri":{"type":"string"},"language":{"type":"string"}},"required":["uri","language"]}"#,
            exec_lsp_diagnostics,
        )
        .with_effect(ToolEffect::Read),
    );
    registry.register(
        ToolDefinition::new_fn(
            "lsp_definition",
            "Resolve definition locations via LSP.",
            r#"{"type":"object","properties":{"uri":{"type":"string"},"language":{"type":"string"},"line":{"type":"integer"},"character":{"type":"integer"}},"required":["uri","language","line","character"]}"#,
            exec_lsp_definition,
        )
        .with_effect(ToolEffect::Read),
    );
    registry.register(
        ToolDefinition::new_fn(
            "lsp_references",
            "Find references via LSP.",
            r#"{"type":"object","properties":{"uri":{"type":"string"},"language":{"type":"string"},"line":{"type":"integer"},"character":{"type":"integer"}},"required":["uri","language","line","character"]}"#,
            exec_lsp_references,
        )
        .with_effect(ToolEffect::Read),
    );
}

/// Register spawn_agent backed by a host-owned SubagentManager.
pub fn register_spawn_agent_tool(
    registry: &mut ToolRegistry,
    manager: Arc<Mutex<SubagentManager>>,
) {
    registry.register(
        ToolDefinition::new_boxed(
            "spawn_agent",
            "Spawn a nested subagent with a prompt via host SubagentManager.",
            r#"{"type":"object","properties":{"prompt":{"type":"string"},"name":{"type":"string"},"model":{"type":"string"},"isolate":{"type":"boolean"}},"required":["prompt"]}"#,
            Box::new(move |ctx, args| {
                let manager = Arc::clone(&manager);
                Box::pin(async move {
                    let v: serde_json::Value = match serde_json::from_str(&args) {
                        Ok(v) => v,
                        Err(e) => {
                            return ToolResult::err("spawn_agent", format!("invalid json: {e}"))
                        }
                    };
                    let prompt = match v.get("prompt").and_then(|p| p.as_str()) {
                        Some(p) if !p.is_empty() => p.to_string(),
                        _ => return ToolResult::err("spawn_agent", "prompt required"),
                    };
                    let name = v
                        .get("name")
                        .and_then(|n| n.as_str())
                        .unwrap_or("subagent")
                        .to_string();
                    let model = v
                        .get("model")
                        .and_then(|m| m.as_str())
                        .map(|s| s.to_string());
                    let isolate = v
                        .get("isolate")
                        .and_then(|i| i.as_bool())
                        .unwrap_or(false);
                    let config = SubagentConfig {
                        name: name.clone(),
                        model,
                        workspace_isolation: isolate,
                        ..SubagentConfig::default()
                    };
                    let mut mgr = manager.lock();
                    match mgr.spawn(config, &prompt, &ctx.workspace_root) {
                        Ok(handle) => {
                            let result = handle.wait_sync();
                            let body = serde_json::json!({
                                "id": handle.id(),
                                "name": handle.name(),
                                "status": format!("{:?}", handle.status()),
                                "output": result.output,
                                "tool_calls": result.tool_calls,
                                "error": result.error,
                            });
                            ToolResult::ok("spawn_agent", body.to_string())
                        }
                        Err(e) => ToolResult::err("spawn_agent", e.to_string()),
                    }
                })
            }),
        )
        .with_effect(ToolEffect::Process),
    );
}

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
struct TodoItem {
    id: String,
    content: String,
    status: String,
}

fn todo_store() -> &'static DashMap<String, Vec<TodoItem>> {
    static STORE: OnceLock<DashMap<String, Vec<TodoItem>>> = OnceLock::new();
    STORE.get_or_init(DashMap::new)
}

fn workspace_key(ctx: &ToolContext) -> String {
    ctx.workspace_root.to_string_lossy().into_owned()
}

fn exec_web_fetch(_ctx: Arc<ToolContext>, args: String) -> ToolFuture {
    Box::pin(async move {
        let url = match parse_str_field(&args, "url") {
            Some(u) => u,
            None => return ToolResult::err("web_fetch", "url required"),
        };
        let max_bytes = parse_num_field(&args, "max_bytes").unwrap_or(100_000) as usize;
        let max_bytes = max_bytes.min(100_000);

        #[cfg(feature = "providers")]
        {
            let client = crate::http::global_client();
            match client.get(&url).send().await {
                Ok(resp) => {
                    let status = resp.status();
                    match resp.bytes().await {
                        Ok(bytes) => {
                            let truncated = bytes.len() > max_bytes;
                            let slice = &bytes[..bytes.len().min(max_bytes)];
                            let mut text = String::from_utf8_lossy(slice).into_owned();
                            if truncated {
                                text.push_str(&format!(
                                    "\n\n[truncated to {max_bytes} bytes; total {}]",
                                    bytes.len()
                                ));
                            }
                            if !status.is_success() {
                                text = format!("HTTP {status}\n{text}");
                            }
                            ToolResult::ok("web_fetch", text)
                        }
                        Err(e) => ToolResult::err("web_fetch", format!("body read failed: {e}")),
                    }
                }
                Err(e) => ToolResult::err("web_fetch", format!("request failed: {e}")),
            }
        }

        #[cfg(not(feature = "providers"))]
        {
            let _ = max_bytes;
            ToolResult::err(
                "web_fetch",
                format!("providers feature required to fetch {url}"),
            )
        }
    })
}

fn exec_todo(ctx: Arc<ToolContext>, args: String) -> ToolFuture {
    Box::pin(async move {
        let v: serde_json::Value = match serde_json::from_str(&args) {
            Ok(v) => v,
            Err(e) => return ToolResult::err("todo", format!("invalid json: {e}")),
        };
        let action = match v.get("action").and_then(|a| a.as_str()) {
            Some(a) => a,
            None => return ToolResult::err("todo", "action required"),
        };
        let key = workspace_key(&ctx);
        let store = todo_store();

        match action {
            "list" => {
                let items = store.get(&key).map(|e| e.clone()).unwrap_or_default();
                ToolResult::ok(
                    "todo",
                    serde_json::to_string_pretty(&items).unwrap_or_else(|_| "[]".into()),
                )
            }
            "clear" => {
                store.insert(key, Vec::new());
                ToolResult::ok("todo", "[]")
            }
            "add" => {
                let mut items = store.get(&key).map(|e| e.clone()).unwrap_or_default();
                let incoming = v
                    .get("items")
                    .and_then(|i| i.as_array())
                    .cloned()
                    .unwrap_or_default();
                if incoming.is_empty() {
                    return ToolResult::err("todo", "items required for add");
                }
                for item in incoming {
                    let content = item
                        .get("content")
                        .and_then(|c| c.as_str())
                        .unwrap_or("")
                        .to_string();
                    if content.is_empty() {
                        continue;
                    }
                    let id = item
                        .get("id")
                        .and_then(|i| i.as_str())
                        .map(|s| s.to_string())
                        .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
                    let status = item
                        .get("status")
                        .and_then(|s| s.as_str())
                        .unwrap_or("pending")
                        .to_string();
                    items.push(TodoItem {
                        id,
                        content,
                        status,
                    });
                }
                let out = serde_json::to_string_pretty(&items).unwrap_or_else(|_| "[]".into());
                store.insert(key, items);
                ToolResult::ok("todo", out)
            }
            "update" | "complete" => {
                let mut items = store.get(&key).map(|e| e.clone()).unwrap_or_default();
                let incoming = v
                    .get("items")
                    .and_then(|i| i.as_array())
                    .cloned()
                    .unwrap_or_default();
                if incoming.is_empty() {
                    return ToolResult::err("todo", format!("items required for {action}"));
                }
                for item in incoming {
                    let id = item.get("id").and_then(|i| i.as_str());
                    let content = item.get("content").and_then(|c| c.as_str());
                    let status = if action == "complete" {
                        Some("completed")
                    } else {
                        item.get("status").and_then(|s| s.as_str())
                    };
                    if let Some(id) = id {
                        if let Some(existing) = items.iter_mut().find(|t| t.id == id) {
                            if let Some(c) = content {
                                existing.content = c.to_string();
                            }
                            if let Some(s) = status {
                                existing.status = s.to_string();
                            }
                        }
                    } else if let Some(c) = content {
                        if let Some(existing) = items.iter_mut().find(|t| t.content == c) {
                            if let Some(s) = status {
                                existing.status = s.to_string();
                            }
                        }
                    }
                }
                let out = serde_json::to_string_pretty(&items).unwrap_or_else(|_| "[]".into());
                store.insert(key, items);
                ToolResult::ok("todo", out)
            }
            other => ToolResult::err("todo", format!("unknown action: {other}")),
        }
    })
}

fn exec_spawn_agent(ctx: Arc<ToolContext>, args: String) -> ToolFuture {
    Box::pin(async move {
        let v: serde_json::Value = match serde_json::from_str(&args) {
            Ok(v) => v,
            Err(e) => return ToolResult::err("spawn_agent", format!("invalid json: {e}")),
        };
        let prompt = match v.get("prompt").and_then(|p| p.as_str()) {
            Some(p) if !p.is_empty() => p.to_string(),
            _ => return ToolResult::err("spawn_agent", "prompt required"),
        };
        let name = v
            .get("name")
            .and_then(|n| n.as_str())
            .unwrap_or("subagent")
            .to_string();
        let model = v
            .get("model")
            .and_then(|m| m.as_str())
            .map(|s| s.to_string());
        let isolate = v.get("isolate").and_then(|i| i.as_bool()).unwrap_or(false);

        let mut manager = SubagentManager::new();
        if let Some(provider) = ctx.provider.clone() {
            manager = manager.with_provider(provider);
        }
        if let Some(tools) = ctx.tools.clone() {
            manager = manager.with_tools(tools);
        }
        let config = SubagentConfig {
            name: name.clone(),
            model,
            workspace_isolation: isolate,
            ..SubagentConfig::default()
        };
        match manager.spawn(config, &prompt, &ctx.workspace_root) {
            Ok(handle) => {
                let result = handle.wait_sync();
                let body = serde_json::json!({
                    "id": handle.id(),
                    "name": handle.name(),
                    "status": format!("{:?}", handle.status()),
                    "output": result.output,
                    "tool_calls": result.tool_calls,
                    "error": result.error,
                });
                ToolResult::ok("spawn_agent", body.to_string())
            }
            Err(e) => ToolResult::err("spawn_agent", e.to_string()),
        }
    })
}

fn exec_enter_plan_mode(ctx: Arc<ToolContext>, _args: String) -> ToolFuture {
    Box::pin(async move {
        if let Some(slot) = ctx.pending_scope.as_ref() {
            *slot.lock() = Some(Scope::Plan);
        }
        ToolResult::ok(
            "enter_plan_mode",
            "Entered plan mode. Produce a concrete multi-step plan: files, risks, verification. Do not modify the workspace. Host should set_scope(Plan).",
        )
    })
}

fn exec_exit_plan_mode(ctx: Arc<ToolContext>, _args: String) -> ToolFuture {
    Box::pin(async move {
        if let Some(slot) = ctx.pending_scope.as_ref() {
            *slot.lock() = Some(Scope::Coding);
        }
        ToolResult::ok(
            "exit_plan_mode",
            "Exited plan mode. Resume coding scope. Host should set_scope(Coding).",
        )
    })
}

fn exec_lsp_diagnostics(ctx: Arc<ToolContext>, args: String) -> ToolFuture {
    Box::pin(async move {
        #[cfg(feature = "ipc")]
        {
            let uri = match parse_str_field(&args, "uri") {
                Some(u) => u,
                None => return ToolResult::err("lsp_diagnostics", "uri required"),
            };
            let language = match parse_str_field(&args, "language") {
                Some(l) => l,
                None => return ToolResult::err("lsp_diagnostics", "language required"),
            };
            let Some(lsp) = ctx.lsp.clone() else {
                return ToolResult::err("lsp_diagnostics", "lsp not configured");
            };
            match lsp.diagnostics(&uri, &language).await {
                Ok(diags) => ToolResult::ok(
                    "lsp_diagnostics",
                    serde_json::to_string_pretty(&diags).unwrap_or_else(|e| e.to_string()),
                ),
                Err(e) => ToolResult::err("lsp_diagnostics", e.to_string()),
            }
        }
        #[cfg(not(feature = "ipc"))]
        {
            let _ = (ctx, args);
            ToolResult::err("lsp_diagnostics", "lsp not configured")
        }
    })
}

fn exec_lsp_definition(ctx: Arc<ToolContext>, args: String) -> ToolFuture {
    Box::pin(async move {
        #[cfg(feature = "ipc")]
        {
            let uri = match parse_str_field(&args, "uri") {
                Some(u) => u,
                None => return ToolResult::err("lsp_definition", "uri required"),
            };
            let language = match parse_str_field(&args, "language") {
                Some(l) => l,
                None => return ToolResult::err("lsp_definition", "language required"),
            };
            let line = match parse_num_field(&args, "line") {
                Some(n) => n as u32,
                None => return ToolResult::err("lsp_definition", "line required"),
            };
            let character = parse_num_field(&args, "character")
                .or_else(|| parse_num_field(&args, "char"))
                .unwrap_or(0) as u32;
            let Some(lsp) = ctx.lsp.clone() else {
                return ToolResult::err("lsp_definition", "lsp not configured");
            };
            match lsp.definition(&uri, &language, line, character).await {
                Ok(locs) => ToolResult::ok(
                    "lsp_definition",
                    serde_json::to_string_pretty(&locs).unwrap_or_else(|e| e.to_string()),
                ),
                Err(e) => ToolResult::err("lsp_definition", e.to_string()),
            }
        }
        #[cfg(not(feature = "ipc"))]
        {
            let _ = (ctx, args);
            ToolResult::err("lsp_definition", "lsp not configured")
        }
    })
}

fn exec_lsp_references(ctx: Arc<ToolContext>, args: String) -> ToolFuture {
    Box::pin(async move {
        #[cfg(feature = "ipc")]
        {
            let uri = match parse_str_field(&args, "uri") {
                Some(u) => u,
                None => return ToolResult::err("lsp_references", "uri required"),
            };
            let language = match parse_str_field(&args, "language") {
                Some(l) => l,
                None => return ToolResult::err("lsp_references", "language required"),
            };
            let line = match parse_num_field(&args, "line") {
                Some(n) => n as u32,
                None => return ToolResult::err("lsp_references", "line required"),
            };
            let character = parse_num_field(&args, "character")
                .or_else(|| parse_num_field(&args, "char"))
                .unwrap_or(0) as u32;
            let Some(lsp) = ctx.lsp.clone() else {
                return ToolResult::err("lsp_references", "lsp not configured");
            };
            match lsp.references(&uri, &language, line, character).await {
                Ok(locs) => ToolResult::ok(
                    "lsp_references",
                    serde_json::to_string_pretty(&locs).unwrap_or_else(|e| e.to_string()),
                ),
                Err(e) => ToolResult::err("lsp_references", e.to_string()),
            }
        }
        #[cfg(not(feature = "ipc"))]
        {
            let _ = (ctx, args);
            ToolResult::err("lsp_references", "lsp not configured")
        }
    })
}

fn parse_str_field(args: &str, field: &str) -> Option<String> {
    let v: serde_json::Value = serde_json::from_str(args).ok()?;
    v.get(field)?.as_str().map(|s| s.to_string())
}

fn parse_num_field(args: &str, field: &str) -> Option<u64> {
    let v: serde_json::Value = serde_json::from_str(args).ok()?;
    v.get(field)?.as_u64()
}

fn resolve_path(ctx: &ToolContext, path: &str, write: bool) -> Result<std::path::PathBuf, String> {
    let p = std::path::PathBuf::from(path);
    let full = if p.is_absolute() {
        p
    } else {
        ctx.workspace_root.join(p)
    };
    if let Some(sb) = ctx.sandbox.as_ref() {
        sb.validate_path(&full, write).map_err(|e| e.to_string())?;
    }
    Ok(full)
}

fn exec_read(ctx: Arc<ToolContext>, args: String) -> ToolFuture {
    Box::pin(async move {
        let path = match parse_str_field(&args, "path") {
            Some(p) => p,
            None => return ToolResult::err("read", "path required"),
        };
        let offset = parse_num_field(&args, "offset").unwrap_or(0) as usize;
        let limit = parse_num_field(&args, "limit").unwrap_or(2000) as usize;

        let full = match resolve_path(&ctx, &path, false) {
            Ok(p) => p,
            Err(e) => return ToolResult::err("read", e),
        };
        match std::fs::read_to_string(&full) {
            Ok(content) => {
                let lines: Vec<&str> = content.lines().collect();
                let start = offset.min(lines.len());
                let end = (start + limit).min(lines.len());
                let mut out = String::new();
                for (i, line) in lines[start..end].iter().enumerate() {
                    out.push_str(&format!("{:>6}\t{}\n", start + i + 1, line));
                }
                if out.is_empty() {
                    out = "(empty file)".to_string();
                }
                ToolResult::ok("read", out)
            }
            Err(e) => ToolResult::err("read", format!("{e}")),
        }
    })
}

fn exec_write(ctx: Arc<ToolContext>, args: String) -> ToolFuture {
    Box::pin(async move {
        let path = match parse_str_field(&args, "path") {
            Some(p) => p,
            None => return ToolResult::err("write", "path required"),
        };
        let content = match parse_str_field(&args, "content") {
            Some(c) => c,
            None => return ToolResult::err("write", "content required"),
        };
        let full = match resolve_path(&ctx, &path, true) {
            Ok(p) => p,
            Err(e) => return ToolResult::err("write", e),
        };
        if let Some(parent) = full.parent() {
            if !parent.exists() {
                if let Err(e) = std::fs::create_dir_all(parent) {
                    return ToolResult::err("write", format!("mkdir failed: {e}"));
                }
            }
        }
        match std::fs::write(&full, &content) {
            Ok(_) => {
                debug!("wrote {} bytes to {}", content.len(), full.display());
                ToolResult::ok(
                    "write",
                    format!("wrote {} bytes to {}", content.len(), path),
                )
            }
            Err(e) => ToolResult::err("write", format!("{e}")),
        }
    })
}

fn exec_edit(ctx: Arc<ToolContext>, args: String) -> ToolFuture {
    Box::pin(async move {
        let path = match parse_str_field(&args, "path") {
            Some(p) => p,
            None => return ToolResult::err("edit", "path required"),
        };
        let old_string = match parse_str_field(&args, "old_string") {
            Some(s) => s,
            None => return ToolResult::err("edit", "old_string required"),
        };
        let new_string = match parse_str_field(&args, "new_string") {
            Some(s) => s,
            None => return ToolResult::err("edit", "new_string required"),
        };
        let full = match resolve_path(&ctx, &path, true) {
            Ok(p) => p,
            Err(e) => return ToolResult::err("edit", e),
        };
        let content = match std::fs::read_to_string(&full) {
            Ok(c) => c,
            Err(e) => return ToolResult::err("edit", format!("read failed: {e}")),
        };
        let occurrences = content.matches(&old_string).count();
        if occurrences == 0 {
            return ToolResult::err("edit", "old_string not found in file");
        }
        if occurrences > 1 {
            return ToolResult::err(
                "edit",
                format!("old_string found {occurrences} times — must be unique"),
            );
        }
        let new_content = content.replacen(&old_string, &new_string, 1);
        match std::fs::write(&full, &new_content) {
            Ok(_) => ToolResult::ok("edit", format!("edited {}", path)),
            Err(e) => ToolResult::err("edit", format!("write failed: {e}")),
        }
    })
}

fn exec_bash(ctx: Arc<ToolContext>, args: String) -> ToolFuture {
    Box::pin(async move {
        let command = match parse_str_field(&args, "command") {
            Some(c) => c,
            None => return ToolResult::err("bash", "command required"),
        };
        let cwd = parse_str_field(&args, "cwd");
        let timeout_secs = parse_num_field(&args, "timeout").unwrap_or(120);

        if let Some(sb) = ctx.sandbox.as_ref() {
            if let Err(e) = sb.validate_command(&command) {
                return ToolResult::err("bash", e.to_string());
            }
        }

        let working_dir = if let Some(cwd) = cwd {
            match resolve_path(&ctx, &cwd, false) {
                Ok(p) => p,
                Err(e) => return ToolResult::err("bash", e),
            }
        } else if let Some(sb) = ctx.sandbox.as_ref() {
            if let Err(e) = sb.validate_path(&ctx.workspace_root, false) {
                return ToolResult::err("bash", e.to_string());
            }
            ctx.workspace_root.clone()
        } else {
            ctx.workspace_root.clone()
        };

        let mut cmd = if let Some(os) = ctx.os_sandbox.as_ref() {
            // Wrap bash -c under seatbelt/bwrap when OS sandbox is configured.
            match os.command("bash", &["-c", &command]) {
                Ok(mut c) => {
                    c.current_dir(&working_dir);
                    c
                }
                Err(e) => return ToolResult::err("bash", e.to_string()),
            }
        } else if cfg!(target_os = "windows") {
            let mut c = Command::new("cmd");
            c.arg("/C").arg(&command);
            c.current_dir(&working_dir);
            c
        } else {
            let mut c = Command::new("bash");
            c.arg("-c").arg(&command);
            c.current_dir(&working_dir);
            c
        };

        cmd.stdout(Stdio::piped()).stderr(Stdio::piped());

        let mut child = match cmd.spawn() {
            Ok(c) => c,
            Err(e) => return ToolResult::err("bash", format!("failed to execute: {e}")),
        };

        let deadline = Instant::now() + Duration::from_secs(timeout_secs);
        loop {
            #[cfg(feature = "ipc")]
            if ctx.cancellation.is_canceled() {
                let _ = child.kill();
                let _ = child.wait();
                return ToolResult::err("bash", "command cancelled");
            }
            match child.try_wait() {
                Ok(Some(_)) => break,
                Ok(None) => {
                    if Instant::now() >= deadline {
                        let _ = child.kill();
                        let _ = child.wait();
                        return ToolResult::err(
                            "bash",
                            format!("command timed out after {timeout_secs}s"),
                        );
                    }
                    std::thread::sleep(Duration::from_millis(10));
                }
                Err(e) => return ToolResult::err("bash", format!("wait failed: {e}")),
            }
        }

        let mut stdout = String::new();
        let mut stderr = String::new();
        if let Some(mut out) = child.stdout.take() {
            let mut buf = Vec::new();
            let _ = out.read_to_end(&mut buf);
            stdout = String::from_utf8_lossy(&buf).to_string();
        }
        if let Some(mut err) = child.stderr.take() {
            let mut buf = Vec::new();
            let _ = err.read_to_end(&mut buf);
            stderr = String::from_utf8_lossy(&buf).to_string();
        }
        let exit_code = child.wait().ok().and_then(|s| s.code()).unwrap_or(-1);

        let mut result = String::new();
        if !stdout.is_empty() {
            result.push_str(&stdout);
        }
        if !stderr.is_empty() {
            if !result.is_empty() {
                result.push_str("\n--- stderr ---\n");
            }
            result.push_str(&stderr);
        }
        if exit_code != 0 {
            result.push_str(&format!("\n(exit code: {exit_code})"));
        }
        if result.is_empty() {
            result = "(no output)".to_string();
        }
        ToolResult::ok("bash", result)
    })
}

fn exec_grep(ctx: Arc<ToolContext>, args: String) -> ToolFuture {
    Box::pin(async move {
        let pattern = match parse_str_field(&args, "pattern") {
            Some(p) => p,
            None => return ToolResult::err("grep", "pattern required"),
        };
        let path = parse_str_field(&args, "path").unwrap_or_else(|| ".".to_string());
        let context = parse_num_field(&args, "context").unwrap_or(0) as usize;

        let full = match resolve_path(&ctx, &path, false) {
            Ok(p) => p,
            Err(e) => return ToolResult::err("grep", e),
        };

        #[cfg(feature = "builtin-tools")]
        {
            let regex = match regex::Regex::new(&pattern) {
                Ok(r) => r,
                Err(e) => return ToolResult::err("grep", format!("invalid regex: {e}")),
            };

            let files: Vec<std::path::PathBuf> = if full.is_file() {
                vec![full.clone()]
            } else {
                collect_files(&full)
            };

            let results: Vec<String> = files
                .par_iter()
                .filter_map(|file| {
                    let content = std::fs::read_to_string(file).ok()?;
                    let lines: Vec<&str> = content.lines().collect();
                    let mut matches = Vec::new();
                    for (i, line) in lines.iter().enumerate() {
                        if regex.is_match(line) {
                            let start = i.saturating_sub(context);
                            let end = (i + context + 1).min(lines.len());
                            for (j, ctx_line) in lines[start..end].iter().enumerate() {
                                let line_num = start + j + 1;
                                let marker = if start + j == i { ">" } else { " " };
                                matches.push(format!(
                                    "{marker} {:>6}\t{}:{line_num}\t{ctx_line}",
                                    "",
                                    file.file_name()?.to_string_lossy(),
                                    line_num = line_num
                                ));
                            }
                            if context > 0 && end < lines.len() {
                                matches.push("  ---".to_string());
                            }
                        }
                    }
                    if matches.is_empty() {
                        None
                    } else {
                        Some(matches.join("\n"))
                    }
                })
                .collect();

            if results.is_empty() {
                ToolResult::ok("grep", "(no matches)")
            } else {
                ToolResult::ok("grep", results.join("\n"))
            }
        }

        #[cfg(not(feature = "builtin-tools"))]
        {
            let _ = (pattern, context, full);
            ToolResult::err("grep", "builtin-tools feature not enabled")
        }
    })
}

fn exec_find(ctx: Arc<ToolContext>, args: String) -> ToolFuture {
    Box::pin(async move {
        let pattern = match parse_str_field(&args, "pattern") {
            Some(p) => p,
            None => return ToolResult::err("find", "pattern required"),
        };
        let path = parse_str_field(&args, "path").unwrap_or_else(|| ".".to_string());
        let full = match resolve_path(&ctx, &path, false) {
            Ok(p) => p,
            Err(e) => return ToolResult::err("find", e),
        };

        #[cfg(feature = "builtin-tools")]
        {
            let files = collect_files(&full);
            let glob_pattern = match glob::Pattern::new(&pattern) {
                Ok(p) => p,
                Err(e) => return ToolResult::err("find", format!("invalid glob: {e}")),
            };

            let matched: Vec<String> = files
                .par_iter()
                .filter_map(|f| {
                    let name = f.file_name()?.to_string_lossy().to_string();
                    let path_str = f.to_string_lossy().to_string();
                    if glob_pattern.matches(&name) || glob_pattern.matches(&path_str) {
                        Some(path_str)
                    } else {
                        None
                    }
                })
                .collect();

            if matched.is_empty() {
                ToolResult::ok("find", "(no files found)")
            } else {
                ToolResult::ok("find", matched.join("\n"))
            }
        }

        #[cfg(not(feature = "builtin-tools"))]
        {
            let _ = (pattern, full);
            ToolResult::err("find", "builtin-tools feature not enabled")
        }
    })
}

fn exec_ls(ctx: Arc<ToolContext>, args: String) -> ToolFuture {
    Box::pin(async move {
        let path = match parse_str_field(&args, "path") {
            Some(p) => p,
            None => return ToolResult::err("ls", "path required"),
        };
        let full = match resolve_path(&ctx, &path, false) {
            Ok(p) => p,
            Err(e) => return ToolResult::err("ls", e),
        };
        match std::fs::read_dir(&full) {
            Ok(entries) => {
                let mut items: Vec<(String, bool)> = entries
                    .flatten()
                    .filter_map(|e| {
                        let name = e.file_name().to_string_lossy().to_string();
                        let is_dir = e.file_type().ok()?.is_dir();
                        Some((name, is_dir))
                    })
                    .collect();
                items.sort_by(|a, b| a.0.cmp(&b.0));
                let out: Vec<String> = items
                    .iter()
                    .map(|(name, is_dir)| {
                        if *is_dir {
                            format!("{name}/")
                        } else {
                            name.clone()
                        }
                    })
                    .collect();
                if out.is_empty() {
                    ToolResult::ok("ls", "(empty directory)")
                } else {
                    ToolResult::ok("ls", out.join("\n"))
                }
            }
            Err(e) => ToolResult::err("ls", format!("{e}")),
        }
    })
}

#[cfg(feature = "builtin-tools")]
fn collect_files(root: &std::path::Path) -> Vec<std::path::PathBuf> {
    use rayon::prelude::*;
    let mut files = Vec::new();

    if root.is_file() {
        files.push(root.to_path_buf());
        return files;
    }

    #[cfg(feature = "builtin-tools")]
    {
        let walker = ignore::WalkBuilder::new(root)
            .hidden(false)
            .git_ignore(true)
            .build();

        let entries: Vec<_> = walker.filter_map(|e| e.ok()).collect();
        files = entries
            .par_iter()
            .filter_map(|e| {
                if e.file_type()?.is_file() {
                    Some(e.path().to_path_buf())
                } else {
                    None
                }
            })
            .collect();
    }

    files
}

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

    #[tokio::test]
    async fn test_write_and_read() {
        let tmp = TempDir::new().unwrap();
        let ctx = Arc::new(ToolContext::new(tmp.path()));

        let write_result = exec_write(
            ctx.clone(),
            r#"{"path":"test.txt","content":"hello world"}"#.to_string(),
        )
        .await;
        assert!(!write_result.is_error);

        let read_result = exec_read(ctx, r#"{"path":"test.txt"}"#.to_string()).await;
        assert!(!read_result.is_error);
        assert!(read_result.content.contains("hello world"));
    }

    #[tokio::test]
    async fn test_edit() {
        let tmp = TempDir::new().unwrap();
        let ctx = Arc::new(ToolContext::new(tmp.path()));

        exec_write(
            ctx.clone(),
            r#"{"path":"edit.txt","content":"foo bar baz"}"#.to_string(),
        )
        .await;
        let edit_result = exec_edit(
            ctx,
            r#"{"path":"edit.txt","old_string":"bar","new_string":"qux"}"#.to_string(),
        )
        .await;
        assert!(!edit_result.is_error);

        let content = std::fs::read_to_string(tmp.path().join("edit.txt")).unwrap();
        assert_eq!(content, "foo qux baz");
    }

    #[tokio::test]
    async fn test_bash() {
        let tmp = TempDir::new().unwrap();
        let ctx = Arc::new(ToolContext::new(tmp.path()));
        let result = exec_bash(ctx, r#"{"command":"echo hello"}"#.to_string()).await;
        assert!(!result.is_error);
        assert!(result.content.contains("hello"));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_bash_timeout() {
        let tmp = TempDir::new().unwrap();
        let ctx = Arc::new(ToolContext::new(tmp.path()));
        let result = exec_bash(ctx, r#"{"command":"sleep 2","timeout":1}"#.to_string()).await;
        assert!(result.is_error);
        assert!(result.content.contains("timed out"));
    }

    #[cfg(feature = "ipc")]
    #[tokio::test]
    async fn test_bash_cancellation() {
        let source = cancellation_token::CancellationTokenSource::new();
        let mut ctx = ToolContext::new(".");
        ctx.cancellation = source.token();
        source.cancel();
        let result = exec_bash(Arc::new(ctx), r#"{"command":"sleep 5"}"#.to_string()).await;
        assert!(result.is_error);
        assert_eq!(result.content, "command cancelled");
    }

    #[tokio::test]
    async fn test_todo_add_list_complete() {
        let tmp = TempDir::new().unwrap();
        let ctx = Arc::new(ToolContext::new(tmp.path()));
        let add = exec_todo(
            ctx.clone(),
            r#"{"action":"add","items":[{"id":"1","content":"ship tools"}]}"#.to_string(),
        )
        .await;
        assert!(!add.is_error, "{}", add.content);
        assert!(add.content.contains("ship tools"));

        let listed = exec_todo(ctx.clone(), r#"{"action":"list"}"#.to_string()).await;
        assert!(!listed.is_error);
        assert!(listed.content.contains("ship tools"));

        let done = exec_todo(
            ctx.clone(),
            r#"{"action":"complete","items":[{"id":"1"}]}"#.to_string(),
        )
        .await;
        assert!(!done.is_error);
        assert!(done.content.contains("completed"));

        let _ = exec_todo(ctx, r#"{"action":"clear"}"#.to_string()).await;
    }

    #[tokio::test]
    async fn test_enter_plan_mode_sets_pending_scope() {
        let pending = Arc::new(parking_lot::Mutex::new(None));
        let mut ctx = ToolContext::new(".");
        ctx.pending_scope = Some(Arc::clone(&pending));
        let result = exec_enter_plan_mode(Arc::new(ctx), "{}".to_string()).await;
        assert!(!result.is_error);
        assert_eq!(*pending.lock(), Some(Scope::Plan));
    }

    #[tokio::test]
    async fn test_web_fetch_without_providers_or_offline() {
        let ctx = Arc::new(ToolContext::new("."));
        let result = exec_web_fetch(ctx, r#"{"url":"https://example.invalid/"}"#.to_string()).await;
        assert!(result.is_error);
        assert!(
            result.content.contains("providers feature required")
                || result.content.contains("request failed")
                || result.content.contains("error")
        );
    }
}