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
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
//! Tool specification traits for the CodeWhale agent system.
//!
//! This module defines the core abstractions for tools:
//! - `ToolSpec`: The main trait that all tools must implement
//! - `ToolContext`: Execution context passed to tools
//! - `ToolResult`: Unified result type for tool execution
//! - `ToolCapability`: Capabilities and requirements of tools
use std::collections::HashMap;
use std::fs;
use std::path::{Component, Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::SystemTime;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio_util::sync::CancellationToken;
use unicode_normalization::UnicodeNormalization;
use crate::features::Features;
use crate::lsp::LspManager;
use crate::network_policy::NetworkPolicyDecider;
use crate::rlm::session::SessionObjectSnapshot;
use crate::rlm::session::{SharedRlmSessionStore, new_shared_rlm_session_store};
use crate::sandbox::backend::SandboxBackend;
use crate::tools::handle::{SharedHandleStore, new_shared_handle_store};
use crate::tools::shell::{SharedShellManager, new_shared_shell_manager};
use crate::worker_profile::ShellPolicy;
#[allow(unused_imports)]
pub use codewhale_tools::{
ApprovalRequirement, PreparedToolCall, ResourceClaim, ToolCapability, ToolError,
ToolExecutionOutcome, ToolResult, ToolResultContentBlock, ToolTerminalStatus, optional_bool,
optional_bool_opt, optional_str, optional_u64, required_str, required_u64,
schedule_non_conflicting, type_mismatch,
};
/// Text plus provider-neutral rich blocks at the conversation boundary.
#[derive(Debug, Clone)]
pub(crate) struct RichToolResult {
pub result: ToolResult,
pub content_blocks: Vec<ToolResultContentBlock>,
}
impl RichToolResult {
#[must_use]
pub fn plain(result: ToolResult) -> Self {
Self {
result,
content_blocks: Vec::new(),
}
}
#[must_use]
pub fn with_content_blocks(
result: ToolResult,
content_blocks: Vec<ToolResultContentBlock>,
) -> Self {
Self {
result,
content_blocks,
}
}
#[must_use]
pub fn into_result(self) -> ToolResult {
self.result
}
}
impl std::ops::Deref for RichToolResult {
type Target = ToolResult;
fn deref(&self) -> &Self::Target {
&self.result
}
}
#[async_trait]
pub trait DynamicToolExecutor: Send + Sync {
async fn execute_dynamic_tool(
&self,
thread_id: Option<String>,
namespace: Option<String>,
name: String,
input: Value,
) -> Result<ToolResult, ToolError>;
}
/// Optional durable runtime services made available to model-visible tools.
///
/// These are intentionally optional so existing unit tests and one-off tool
/// contexts keep working. Tools that need durable task/automation state fail
/// closed with a clear "not available" error when the relevant service is not
/// attached.
#[derive(Clone)]
pub struct RuntimeToolServices {
pub shell_manager: Option<SharedShellManager>,
/// True only for the real headless exec host after it has established the
/// explicit authority required to transfer `persist:true` services.
pub persist_services_enabled: bool,
pub task_manager: Option<crate::task_manager::SharedTaskManager>,
pub automations: Option<crate::automation_manager::SharedAutomationManager>,
pub task_data_dir: Option<PathBuf>,
pub active_task_id: Option<String>,
pub active_thread_id: Option<String>,
pub dynamic_tool_executor: Option<Arc<dyn DynamicToolExecutor>>,
/// Active-session Work Graph authority plus its legacy Plan/To-do views.
pub work: Option<crate::work_graph::SharedWorkRuntime>,
/// Hook executor for `shell_env` injection (#456) and any future
/// tool-side hook events. `None` outside the live engine — test
/// contexts that don't care about hooks get a no-op.
pub hook_executor: Option<std::sync::Arc<crate::hooks::HookExecutor>>,
/// Per-session backing store for `var_handle` payloads. Cloned tool
/// contexts share this Arc so handles survive across turns.
pub handle_store: SharedHandleStore,
/// Per-session persistent RLM kernels, keyed by caller-chosen context name.
pub rlm_sessions: SharedRlmSessionStore,
}
impl Default for RuntimeToolServices {
fn default() -> Self {
Self {
shell_manager: None,
persist_services_enabled: false,
task_manager: None,
automations: None,
task_data_dir: None,
active_task_id: None,
active_thread_id: None,
dynamic_tool_executor: None,
work: None,
hook_executor: None,
handle_store: new_shared_handle_store(),
rlm_sessions: new_shared_rlm_session_store(),
}
}
}
impl std::fmt::Debug for RuntimeToolServices {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RuntimeToolServices")
.field("shell_manager", &self.shell_manager.is_some())
.field("persist_services_enabled", &self.persist_services_enabled)
.field("task_manager", &self.task_manager.is_some())
.field("automations", &self.automations.is_some())
.field("task_data_dir", &self.task_data_dir)
.field("active_task_id", &self.active_task_id)
.field("active_thread_id", &self.active_thread_id)
.field(
"dynamic_tool_executor",
&self.dynamic_tool_executor.is_some(),
)
.field("work", &self.work.is_some())
.field("hook_executor", &self.hook_executor.is_some())
.field("handle_store", &true)
.field("rlm_sessions", &true)
.finish()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct FileReadSnapshot {
len: u64,
modified: Option<SystemTime>,
}
#[derive(Debug, Default)]
pub struct FileReadTracker {
reads: HashMap<PathBuf, FileReadSnapshot>,
}
pub type SharedFileReadTracker = Arc<Mutex<FileReadTracker>>;
pub(crate) fn new_shared_file_read_tracker() -> SharedFileReadTracker {
Arc::new(Mutex::new(FileReadTracker::default()))
}
fn file_read_snapshot(path: &Path) -> Result<FileReadSnapshot, ToolError> {
let metadata = fs::metadata(path).map_err(|e| {
ToolError::execution_failed(format!("Failed to inspect {}: {e}", path.display()))
})?;
Ok(FileReadSnapshot {
len: metadata.len(),
modified: metadata.modified().ok(),
})
}
/// Sandbox policy for command execution.
#[derive(Debug, Clone, Default)]
pub enum SandboxPolicy {
/// No sandboxing (dangerous but sometimes needed)
#[default]
None,
}
/// Machine-readable mutation boundary for a headless worker process.
///
/// Fleet serializes this envelope onto the exact `codewhale exec` argv. The
/// child installs it before constructing its engine, and every ToolContext in
/// that process inherits the same outer cap. Nested agents may narrow this
/// boundary, but cannot remove or expand it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ToolAuthorityEnvelope {
pub schema_version: u32,
pub owner: String,
pub authority: ToolMutationAuthority,
/// Optional outer network cap for headless workers. `None` preserves the
/// behavior of v1 envelopes written before this field existed; new Fleet
/// launches always carry the resolved worker permission explicitly.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub network_access: Option<bool>,
/// Explicit shell cap for a headless worker. Older v1 envelopes omit this
/// field and therefore remain shell-less; mutation authority is never
/// treated as an implicit shell grant.
#[serde(default, skip_serializing_if = "ToolShellAuthority::is_none")]
pub shell: ToolShellAuthority,
/// Narrow process-start authority for the built-in verification surface.
/// This is separate from both mutation and shell authority: a verifier may
/// run classifier-bounded workspace checks, but that never grants Bash or
/// an operator-supplied command line.
#[serde(default, skip_serializing_if = "ToolVerificationAuthority::is_none")]
pub verification: ToolVerificationAuthority,
#[serde(default)]
pub writable_roots: Vec<String>,
#[serde(default)]
pub writable_files: Vec<String>,
#[serde(default)]
pub coordination_contracts: Vec<String>,
}
/// Shell authority carried across the Fleet subprocess boundary.
///
/// Full/arbitrary shell is intentionally not representable here. Fleet can
/// opt a Scout/Reviewer worker into the classifier-proven read subset, while every
/// other headless role keeps the historical shell-less posture.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolShellAuthority {
#[default]
None,
ReadOnly,
}
impl ToolShellAuthority {
#[must_use]
pub const fn is_none(&self) -> bool {
matches!(self, Self::None)
}
#[must_use]
const fn shell_policy(self) -> ShellPolicy {
match self {
Self::None => ShellPolicy::None,
Self::ReadOnly => ShellPolicy::ReadOnly,
}
}
}
/// Process authority for Fleet's dedicated verifier role.
///
/// Arbitrary execution is intentionally not representable. The registry and
/// dispatch boundary admit only calls that the shared verification classifier
/// proves are default workspace checks or pure test selection.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolVerificationAuthority {
#[default]
None,
Bounded,
}
impl ToolVerificationAuthority {
#[must_use]
pub const fn is_none(&self) -> bool {
matches!(self, Self::None)
}
}
/// Whether a headless Fleet process should register the one read-only Bash
/// surface after intersecting the transported cap with explicit tool denies.
#[must_use]
pub(crate) fn fleet_exec_shell_enabled(
fleet_authority_active: bool,
shell_authority: ToolShellAuthority,
disallowed_tools: Option<&[String]>,
) -> bool {
fleet_authority_active
&& shell_authority == ToolShellAuthority::ReadOnly
&& !disallowed_tools.is_some_and(|rules| {
rules.iter().any(|rule| {
let rule = rule.trim().to_ascii_lowercase();
rule.strip_suffix('*')
.map_or_else(|| rule == "bash", |prefix| "bash".starts_with(prefix))
})
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolMutationAuthority {
ReadOnly,
ScopedWrite,
}
static PROCESS_TOOL_AUTHORITY: OnceLock<Arc<ToolAuthorityEnvelope>> = OnceLock::new();
impl ToolAuthorityEnvelope {
pub fn normalized(mut self) -> Result<Self, String> {
if self.schema_version != 1 {
return Err(format!(
"unsupported tool authority schema version {}",
self.schema_version
));
}
self.owner = bounded_authority_value("owner", &self.owner, 128)?;
self.writable_roots = normalize_authority_paths(&self.writable_roots, "writable_roots")?;
self.writable_files = normalize_authority_paths(&self.writable_files, "writable_files")?;
self.coordination_contracts = normalize_authority_values(
&self.coordination_contracts,
"coordination_contracts",
16,
128,
)?;
if self.authority == ToolMutationAuthority::ScopedWrite
&& self.writable_roots.is_empty()
&& self.writable_files.is_empty()
&& self.coordination_contracts.is_empty()
{
return Err(
"scoped_write authority requires a writable root, exact file, or coordination contract"
.to_string(),
);
}
if self.authority == ToolMutationAuthority::ReadOnly
&& (!self.writable_roots.is_empty()
|| !self.writable_files.is_empty()
|| !self.coordination_contracts.is_empty())
{
return Err("read_only authority cannot carry mutation scope".to_string());
}
if self.verification == ToolVerificationAuthority::Bounded
&& (self.authority != ToolMutationAuthority::ReadOnly
|| self.shell != ToolShellAuthority::None)
{
return Err(
"bounded verification requires read_only mutation authority and no Bash authority"
.to_string(),
);
}
Ok(self)
}
pub fn from_json(raw: &str) -> Result<Self, String> {
serde_json::from_str::<Self>(raw)
.map_err(|error| format!("invalid tool authority envelope: {error}"))?
.normalized()
}
#[cfg(test)]
fn is_within(&self, outer: &Self) -> bool {
if self.shell > outer.shell
|| self.verification > outer.verification
|| (outer.network_access == Some(false) && self.network_access != Some(false))
{
return false;
}
if self.authority == ToolMutationAuthority::ReadOnly {
return true;
}
if outer.authority != ToolMutationAuthority::ScopedWrite {
return false;
}
self.writable_roots.iter().all(|path| {
outer
.writable_roots
.iter()
.any(|root| authority_path_is_within_root(path, root))
}) && self.writable_files.iter().all(|path| {
outer.writable_files.contains(path)
|| outer
.writable_roots
.iter()
.any(|root| authority_path_is_within_root(path, root))
}) && self
.coordination_contracts
.iter()
.all(|contract| outer.coordination_contracts.contains(contract))
}
pub fn permits_mutation_path(
&self,
context: &ToolContext,
raw_path: &str,
) -> Result<bool, ToolError> {
if self.authority == ToolMutationAuthority::ReadOnly {
return Ok(false);
}
let target = resolve_strict_authority_path(context, raw_path)?;
for file in &self.writable_files {
if resolve_strict_authority_path(context, file)? == target {
return Ok(true);
}
}
for root in &self.writable_roots {
if target.starts_with(resolve_strict_authority_path(context, root)?) {
return Ok(true);
}
}
Ok(false)
}
}
#[cfg(test)]
fn authority_path_is_within_root(path: &str, root: &str) -> bool {
root == "."
|| path == root
|| path
.strip_prefix(root)
.is_some_and(|suffix| suffix.starts_with('/'))
}
pub fn install_process_tool_authority(envelope: ToolAuthorityEnvelope) -> Result<(), String> {
let envelope = Arc::new(envelope.normalized()?);
if let Some(existing) = PROCESS_TOOL_AUTHORITY.get() {
return if existing.as_ref() == envelope.as_ref() {
Ok(())
} else {
Err("tool authority envelope was already installed for this process".to_string())
};
}
PROCESS_TOOL_AUTHORITY
.set(envelope)
.map_err(|_| "tool authority envelope was already installed for this process".to_string())
}
fn process_tool_authority() -> Option<Arc<ToolAuthorityEnvelope>> {
PROCESS_TOOL_AUTHORITY.get().cloned()
}
fn bounded_authority_value(field: &str, value: &str, max_chars: usize) -> Result<String, String> {
let value = value.trim().nfc().collect::<String>();
if value.is_empty()
|| value.chars().count() > max_chars
|| value.chars().any(|ch| matches!(ch, '\0' | '\r' | '\n'))
{
return Err(format!(
"tool authority {field} must be one non-empty line of at most {max_chars} characters"
));
}
Ok(value)
}
fn normalize_authority_paths(values: &[String], field: &str) -> Result<Vec<String>, String> {
if values.len() > 32 {
return Err(format!("tool authority {field} accepts at most 32 entries"));
}
let mut normalized = Vec::new();
for raw in values {
let raw = bounded_authority_value(field, raw, 512)?.replace('\\', "/");
let windows_drive = raw.as_bytes().get(1) == Some(&b':')
&& raw.as_bytes().first().is_some_and(u8::is_ascii_alphabetic);
if raw.starts_with('/') || raw.starts_with("//") || windows_drive {
return Err(format!(
"tool authority {field} entries must be repo-relative"
));
}
let mut segments = Vec::new();
for segment in raw.split('/') {
match segment {
"" | "." => {}
".." => {
return Err(format!(
"tool authority {field} cannot contain parent traversal"
));
}
value => segments.push(value),
}
}
let path = if segments.is_empty() {
".".to_string()
} else {
segments.join("/")
};
if !normalized.contains(&path) {
normalized.push(path);
}
}
Ok(normalized)
}
fn normalize_authority_values(
values: &[String],
field: &str,
max_entries: usize,
max_chars: usize,
) -> Result<Vec<String>, String> {
if values.len() > max_entries {
return Err(format!(
"tool authority {field} accepts at most {max_entries} entries"
));
}
let mut normalized = Vec::new();
for value in values {
let value = bounded_authority_value(field, value, max_chars)?;
if !normalized.contains(&value) {
normalized.push(value);
}
}
Ok(normalized)
}
pub(crate) fn resolve_strict_authority_path(
context: &ToolContext,
raw_path: &str,
) -> Result<PathBuf, ToolError> {
let normalized = normalize_authority_paths(&[raw_path.to_string()], "mutation_path")
.map_err(ToolError::permission_denied)?
.into_iter()
.next()
.ok_or_else(|| ToolError::permission_denied("mutation path cannot be empty"))?;
let workspace = context.workspace.canonicalize().map_err(|error| {
ToolError::execution_failed(format!(
"Failed to canonicalize authority workspace {}: {error}",
context.workspace.display()
))
})?;
let mut current = workspace.clone();
if normalized != "." {
for segment in normalized.split('/') {
current.push(segment);
match fs::symlink_metadata(¤t) {
Ok(metadata) if metadata.file_type().is_symlink() => {
return Err(ToolError::permission_denied(format!(
"machine-readable authority paths must not traverse symlinks: {}",
current.display()
)));
}
Ok(_) => {}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(error) => {
return Err(ToolError::execution_failed(format!(
"Failed to inspect authority path {}: {error}",
current.display()
)));
}
}
}
}
if !current.starts_with(&workspace) {
return Err(ToolError::permission_denied(format!(
"machine-readable authority path escapes workspace: {}",
current.display()
)));
}
Ok(current)
}
/// Context passed to tools during execution.
#[derive(Clone)]
pub struct ToolContext {
/// The workspace root directory
pub workspace: PathBuf,
/// Per-turn policy and attached services. Kept behind one owned group so
/// cloning a context preserves the historical value semantics while the
/// top-level context remains small and stable as services evolve.
pub execution: Box<ToolExecutionState>,
}
/// Policy and service state attached to one tool-execution context.
///
/// `ToolContext` dereferences to this group for source compatibility with
/// existing tools. New code can use `context.execution` when the grouping is
/// useful, without growing the top-level context by another field per feature.
#[derive(Clone)]
pub struct ToolExecutionState {
/// Shared shell manager for background tasks and streaming IO.
pub shell_manager: SharedShellManager,
/// Per-session snapshots for files successfully observed by `read_file`.
/// Mutation tools use this to reject narrow edits against unread or stale
/// content.
pub file_read_tracker: SharedFileReadTracker,
/// Sub-agent that owns tool work started through this context. Root user
/// turns leave this unset; child contexts stamp it so long-running shell
/// jobs can be attributed in UI surfaces.
pub owner_agent_id: Option<String>,
pub owner_agent_name: Option<String>,
/// Outer process authority cap installed by Fleet/headless dispatch.
/// `None` for ordinary interactive/root sessions.
pub(crate) tool_authority: Option<Arc<ToolAuthorityEnvelope>>,
/// Whether to allow paths outside workspace
pub trust_mode: bool,
/// Current sandbox policy
#[allow(dead_code)]
pub sandbox_policy: SandboxPolicy,
/// Path for notes file
pub notes_path: PathBuf,
/// MCP configuration path
#[allow(dead_code)]
pub mcp_config_path: PathBuf,
/// Explicit skills directory used for model-visible skill discovery.
pub skills_dir: Option<PathBuf>,
/// Restrict skill discovery to CodeWhale-owned roots plus `skills_dir`.
pub skills_scan_codewhale_only: bool,
/// Immutable registry snapshot for this workspace/engine context.
pub plugin_registry: Option<Arc<crate::plugins::PluginRegistry>>,
/// Elevated sandbox policy override (used when retrying after sandbox denial).
/// This overrides the default sandbox behavior for shell commands.
pub elevated_sandbox_policy: Option<crate::sandbox::SandboxPolicy>,
/// Whether the enclosing host is the real headless `codewhale exec`
/// process. `persist:true` background services are only permitted there;
/// interactive TUI, desktop/app-server, and hosted runtime-thread engines
/// leave this false so the feature fails closed.
pub persist_services_enabled: bool,
/// Optional user-facing hint for shell commands that fail because the
/// active sandbox policy intentionally denies outbound network access.
pub shell_network_denied_hint: Option<String>,
/// Whether tools should auto-approve without safety checks (YOLO mode).
/// When true, command safety analysis is skipped for shell execution.
pub auto_approve: bool,
/// Effective shell policy for this execution context.
pub shell_policy: ShellPolicy,
/// Effective feature flag set for the running session.
pub features: Features,
/// Namespace for tool state that should be scoped to the current session/thread.
pub state_namespace: String,
/// Effective context window for the active provider/model route. Web tools
/// use this to keep inline page content below three percent of the route.
pub route_context_window: Option<u32>,
/// User-trusted external paths the agent may read/write even when they
/// fall outside `workspace`. Loaded from `~/.deepseek/workspace-trust.json`
/// and refreshed when the user runs `/trust add <path>`. Distinct from
/// `trust_mode`, which is the all-or-nothing legacy switch (#29).
pub trusted_external_paths: Vec<PathBuf>,
/// Whether to follow symbolic links during file discovery and tool
/// operations. When `true`, symlinked directories are traversed and
/// symlinked paths that resolve outside the workspace are still allowed
/// (the symlink itself must be inside the workspace). Mirrors the
/// `workspace_follow_symlinks` setting.
pub follow_symlinks: bool,
/// Per-domain network policy (#135). When `None`, network tools fall back
/// to a permissive default that mirrors pre-v0.7.0 behavior so tests and
/// other contexts that don't construct a real policy keep working.
pub network_policy: Option<NetworkPolicyDecider>,
/// Durable runtime services for task, gate, PR-attempt, GitHub evidence,
/// and automation tools.
pub runtime: RuntimeToolServices,
/// Snapshot of the active prompt/session/history exposed as symbolic RLM
/// objects. Tools only receive compact cards unless explicitly opening a
/// bounded object through `rlm_open`.
pub session_objects: Option<SessionObjectSnapshot>,
/// Cancellation token for the active engine turn. Tools that may wait on
/// external work should observe this so UI cancel can interrupt them.
pub cancel_token: Option<CancellationToken>,
/// Optional external sandbox backend for shell execution.
/// When set, exec_shell routes commands through this instead of spawning
/// a local process.
pub sandbox_backend: Option<std::sync::Arc<dyn SandboxBackend>>,
/// Path to the user memory file. `None` when the user-memory feature
/// (#489) is disabled — tools that read or write the file should
/// short-circuit on `None` rather than fall back to a workspace-local
/// default.
pub memory_path: Option<PathBuf>,
/// LSP manager for post-edit diagnostics injection (#428). `None` when
/// LSP is disabled or the context is constructed in a test that does not
/// need diagnostics. Edit tools append a `<diagnostics>` block to their
/// result when this is present and the manager is enabled.
pub lsp_manager: Option<Arc<LspManager>>,
/// Large-output router (#548). When `Some`, tool results that exceed the
/// configured token threshold are routed through a V4-Flash synthesis
/// sub-agent before being returned to the parent context. `None` disables
/// routing (e.g. in sub-agents and test contexts to avoid recursion).
pub large_output_router: Option<crate::tools::large_output_router::LargeOutputRouter>,
/// Which search backend `web_search` should use. Default: Firecrawl. Set via
/// `[search] provider` in config.toml.
pub search_provider: crate::config::SearchProvider,
/// Optional Firecrawl key, or required key for other API search providers.
/// Metaso also falls back to the `METASO_API_KEY` env var.
/// Baidu also falls back to `BAIDU_SEARCH_API_KEY`.
pub search_api_key: Option<String>,
/// Optional DuckDuckGo-compatible HTML endpoint override for `web_search`.
pub search_base_url: Option<String>,
/// Opaque client for the active route's documented first-party search
/// tool. It owns provider authentication internally and is attached only
/// when the exact route capability says server-side search is supported.
pub(crate) provider_native_search: Option<crate::client::ProviderNativeSearchClient>,
/// Exact active route capability facts. Unknown stays fail-closed.
pub(crate) route_capabilities: codewhale_config::route::RouteCapabilities,
/// Per-session workshop variable store (#548). Holds the raw content of
/// the most recent large-tool routing event so the parent can call
/// `promote_to_context` later. `None` when the router is disabled.
pub workshop_vars: Option<
std::sync::Arc<tokio::sync::Mutex<crate::tools::large_output_router::WorkshopVariables>>,
>,
}
impl std::ops::Deref for ToolContext {
type Target = ToolExecutionState;
fn deref(&self) -> &Self::Target {
&self.execution
}
}
impl std::ops::DerefMut for ToolContext {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.execution
}
}
impl ToolContext {
/// Create a new `ToolContext` with default settings.
#[must_use]
pub fn new(workspace: impl Into<PathBuf>) -> Self {
let workspace = workspace.into();
// Prefer .codewhale, fall back to .deepseek for project-local state
let notes_path = codewhale_config::resolve_project_state_dir(&workspace, "notes.md")
.expect("hardcoded project notes state path is valid")
.1;
let mcp_config_path = codewhale_config::resolve_project_state_dir(&workspace, "mcp.json")
.expect("hardcoded project MCP state path is valid")
.1;
Self::with_options(workspace, false, notes_path, mcp_config_path)
}
/// Create a `ToolContext` with all settings specified.
#[allow(dead_code)]
pub fn with_options(
workspace: impl Into<PathBuf>,
trust_mode: bool,
notes_path: impl Into<PathBuf>,
mcp_config_path: impl Into<PathBuf>,
) -> Self {
let workspace = workspace.into();
let shell_manager = new_shared_shell_manager(workspace.clone());
let tool_authority = process_tool_authority();
let shell_policy = match tool_authority.as_deref() {
Some(cap) => cap.shell.shell_policy(),
None => ShellPolicy::Full,
};
Self {
workspace,
execution: Box::new(ToolExecutionState {
shell_manager,
file_read_tracker: new_shared_file_read_tracker(),
owner_agent_id: None,
owner_agent_name: None,
tool_authority,
trust_mode,
sandbox_policy: SandboxPolicy::None,
notes_path: notes_path.into(),
mcp_config_path: mcp_config_path.into(),
skills_dir: None,
skills_scan_codewhale_only: false,
plugin_registry: None,
elevated_sandbox_policy: None,
persist_services_enabled: false,
shell_network_denied_hint: None,
auto_approve: false,
shell_policy,
features: Features::with_defaults(),
state_namespace: "workspace".to_string(),
route_context_window: None,
trusted_external_paths: Vec::new(),
follow_symlinks: false,
network_policy: None,
runtime: RuntimeToolServices::default(),
session_objects: None,
cancel_token: None,
sandbox_backend: None,
memory_path: None,
lsp_manager: None,
large_output_router: None,
search_provider: crate::config::SearchProvider::default(),
search_api_key: None,
search_base_url: None,
provider_native_search: None,
route_capabilities: codewhale_config::route::RouteCapabilities::default(),
workshop_vars: None,
}),
}
}
/// Create a `ToolContext` with auto-approve mode (YOLO).
pub fn with_auto_approve(
workspace: impl Into<PathBuf>,
trust_mode: bool,
notes_path: impl Into<PathBuf>,
mcp_config_path: impl Into<PathBuf>,
auto_approve: bool,
) -> Self {
let mut context = Self::with_options(workspace, trust_mode, notes_path, mcp_config_path);
context.auto_approve = auto_approve;
context
}
/// Attach a per-domain network policy to this context (#135).
#[must_use]
pub fn with_network_policy(mut self, policy: NetworkPolicyDecider) -> Self {
self.network_policy = Some(policy);
self
}
/// Attach durable runtime services to tools.
#[must_use]
pub fn with_runtime_services(mut self, runtime: RuntimeToolServices) -> Self {
self.runtime = runtime;
self
}
/// Stamp tool work with the sub-agent that owns it.
#[must_use]
pub fn with_owner_agent(
mut self,
agent_id: impl Into<String>,
agent_name: impl Into<String>,
) -> Self {
let agent_id = agent_id.into();
let agent_name = agent_name.into();
self.owner_agent_id = (!agent_id.trim().is_empty()).then_some(agent_id);
self.owner_agent_name = (!agent_name.trim().is_empty()).then_some(agent_name);
self
}
#[cfg(test)]
pub(crate) fn with_tool_authority(
mut self,
envelope: ToolAuthorityEnvelope,
) -> Result<Self, String> {
let envelope = envelope.normalized()?;
if let Some(outer) = self.tool_authority.as_ref()
&& !envelope.is_within(outer)
{
return Err(
"nested tool authority cannot expand its process authority cap".to_string(),
);
}
self.tool_authority = Some(Arc::new(envelope));
self.shell_policy = self.authority_clamped_shell_policy(self.shell_policy);
Ok(self)
}
/// Attach skill discovery settings for tools that need to resolve
/// model-visible skills by name.
#[must_use]
pub fn with_skills_config(
mut self,
skills_dir: impl Into<PathBuf>,
scan_codewhale_only: bool,
) -> Self {
self.skills_dir = Some(skills_dir.into());
self.skills_scan_codewhale_only = scan_codewhale_only;
self
}
#[must_use]
pub fn with_plugin_registry(mut self, registry: Arc<crate::plugins::PluginRegistry>) -> Self {
self.plugin_registry = Some(registry);
self
}
/// Attach active prompt/history/session symbolic objects for RLM tools.
#[must_use]
pub fn with_session_objects(mut self, snapshot: SessionObjectSnapshot) -> Self {
self.session_objects = Some(snapshot);
self
}
/// Attach the active engine cancellation token.
#[must_use]
pub fn with_cancel_token(mut self, cancel_token: CancellationToken) -> Self {
self.cancel_token = Some(cancel_token);
self
}
/// Attach the effective shell policy for this turn.
#[must_use]
pub fn with_shell_policy(mut self, policy: ShellPolicy) -> Self {
self.shell_policy = self.authority_clamped_shell_policy(policy);
self
}
/// Replace the turn shell policy while retaining the process authority as
/// an outer ceiling. Live mode changes rebuild this value on every tool
/// call, so the clamp belongs here rather than only at engine startup.
pub(crate) fn set_shell_policy(&mut self, policy: ShellPolicy) {
self.shell_policy = self.authority_clamped_shell_policy(policy);
}
fn authority_clamped_shell_policy(&self, policy: ShellPolicy) -> ShellPolicy {
match self.tool_authority.as_deref() {
Some(cap) => policy.min_with(cap.shell.shell_policy()),
None => policy,
}
}
/// Attach an external sandbox backend for remote shell execution.
#[must_use]
#[allow(dead_code)]
pub fn with_sandbox_backend(mut self, backend: std::sync::Arc<dyn SandboxBackend>) -> Self {
self.sandbox_backend = Some(backend);
self
}
/// Set the user's trusted external paths (loaded from
/// `~/.deepseek/workspace-trust.json`). See [`Self::resolve_path`] for
/// how the list is consulted.
#[must_use]
pub fn with_trusted_external_paths(mut self, paths: Vec<PathBuf>) -> Self {
self.trusted_external_paths = paths;
self
}
/// Set whether tools should follow symbolic links. When `true`,
/// `resolve_path` allows symlinked paths that resolve outside the
/// workspace, and walk-based tools traverse symlinked directories.
/// Mirrors the `workspace_follow_symlinks` setting.
#[must_use]
pub fn with_follow_symlinks(mut self, follow: bool) -> Self {
self.follow_symlinks = follow;
self
}
/// Attach an LSP manager so that edit tools can auto-inject diagnostics
/// into their results after a successful file modification (#428).
#[must_use]
#[allow(dead_code)]
pub fn with_lsp_manager(mut self, manager: Arc<LspManager>) -> Self {
self.lsp_manager = Some(manager);
self
}
/// Remember that the caller has observed the current on-disk state of a
/// file. This is intentionally best-effort so successful reads/writes do
/// not fail after completing only because a post-operation metadata lookup
/// raced with filesystem changes.
pub fn note_file_read(&self, path: &Path) {
let Ok(snapshot) = file_read_snapshot(path) else {
return;
};
let Ok(mut tracker) = self.file_read_tracker.lock() else {
return;
};
tracker.reads.insert(path.to_path_buf(), snapshot);
}
/// Require a successful, still-fresh `read_file` snapshot before a narrow
/// in-place edit. This catches model edits made against guessed or stale
/// content while leaving transactional patch preflight separate.
pub fn require_fresh_file_read(
&self,
path: &Path,
requested_path: &str,
) -> Result<(), ToolError> {
let prior = {
let tracker = self.file_read_tracker.lock().map_err(|_| {
ToolError::execution_failed(
"Failed to check read-before-edit state: tracker lock poisoned".to_string(),
)
})?;
tracker.reads.get(path).cloned()
};
let Some(prior) = prior else {
return Err(ToolError::execution_failed(format!(
"Refusing File action=\"edit\" for {} because it has not been read in this session. \
Recovery: call File with action=\"read\" path=\"{requested_path}\" to inspect the current contents, \
then retry File action=\"edit\" with a unique search string.",
path.display()
)));
};
let current = file_read_snapshot(path).map_err(|e| {
ToolError::execution_failed(format!(
"Refusing File action=\"edit\" for {} because the file could not be checked for staleness ({e}). \
Recovery: call File with action=\"read\" path=\"{requested_path}\" again, then retry File action=\"edit\".",
path.display()
))
})?;
if current != prior {
return Err(ToolError::execution_failed(format!(
"Refusing File action=\"edit\" for {} because it changed since the last File action=\"read\" call. \
Recovery: call File with action=\"read\" path=\"{requested_path}\" again and retry with the current contents.",
path.display()
)));
}
Ok(())
}
/// Resolve a path relative to workspace, validating it doesn't escape.
///
/// This handles both existing files (using canonicalize) and non-existent files
/// (for write operations) by canonicalizing the parent directory and appending
/// the filename.
/// Resolve a path relative to workspace, validating it doesn't escape.
///
/// # Examples
///
/// ```ignore
/// # use crate::tools::spec::ToolContext;
/// let ctx = ToolContext::new(".");
/// let path = ctx.resolve_path("README.md")?;
/// # Ok::<(), crate::tools::spec::ToolError>(())
/// ```
pub fn resolve_path(&self, raw: &str) -> Result<PathBuf, ToolError> {
let candidate = if std::path::Path::new(raw).is_absolute() {
PathBuf::from(raw)
} else {
self.workspace.join(raw)
};
// In trust mode, allow any path without validation
if self.trust_mode {
// Still try to canonicalize for consistency, but don't require it
return Ok(candidate.canonicalize().unwrap_or(candidate));
}
// Try to canonicalize the workspace
let workspace_canonical = self
.workspace
.canonicalize()
.unwrap_or_else(|_| self.workspace.clone());
// When follow_symlinks is enabled, check the non-canonical (symlink)
// path against the workspace first. A symlink inside the workspace
// that resolves outside is allowed — the symlink itself is the gate.
if self.follow_symlinks {
let candidate_normalized = normalize_path(&candidate);
let workspace_normalized = normalize_path(&self.workspace);
let workspace_canonical_normalized = normalize_path(&workspace_canonical);
if candidate_normalized.starts_with(&workspace_normalized)
|| candidate_normalized.starts_with(&workspace_canonical_normalized)
{
// The symlink (or plain path) is inside the workspace.
// Return the canonicalized target so file I/O works correctly.
if candidate.exists() {
return Ok(candidate.canonicalize().unwrap_or(candidate));
}
// Non-existent path: canonicalize the deepest existing ancestor
return self.resolve_nonexistent_path(candidate, &workspace_canonical);
}
// Path is outside workspace even before resolving symlinks.
// Fall through to the standard escape check.
}
// For the initial check, also try to canonicalize the candidate if possible
// This handles symlinks like /var -> /private/var on macOS
let candidate_canonical = candidate
.canonicalize()
.unwrap_or_else(|_| normalize_path(&candidate));
let workspace_normalized = normalize_path(&workspace_canonical);
// Check if the candidate is under the workspace (comparing canonical paths)
if !candidate_canonical.starts_with(&workspace_normalized) {
// Also try with non-canonical workspace for cases where workspace itself
// hasn't been canonicalized yet
let workspace_plain = normalize_path(&self.workspace);
let candidate_normalized = normalize_path(&candidate);
if !candidate_normalized.starts_with(&workspace_plain)
&& !self.is_trusted_external_path(&candidate_canonical)
&& !self.is_trusted_external_path(&candidate_normalized)
{
return Err(ToolError::PathEscape {
path: candidate_canonical,
});
}
}
// For existing paths, use canonicalize directly
if candidate.exists() {
let canonical = candidate.canonicalize().map_err(|e| {
ToolError::execution_failed(format!(
"Failed to canonicalize {}: {}",
candidate.display(),
e
))
})?;
if !canonical.starts_with(&workspace_canonical)
&& !self.is_trusted_external_path(&canonical)
{
return Err(ToolError::PathEscape { path: canonical });
}
return Ok(canonical);
}
self.resolve_nonexistent_path(candidate, &workspace_canonical)
}
/// Resolve a non-existent path by canonicalizing its deepest existing
/// ancestor and validating the result is under the workspace or a
/// trusted external path.
fn resolve_nonexistent_path(
&self,
candidate: PathBuf,
workspace_canonical: &Path,
) -> Result<PathBuf, ToolError> {
let workspace_normalized = normalize_path(workspace_canonical);
let workspace_plain = normalize_path(&self.workspace);
let mut existing_ancestor = candidate.clone();
let mut suffix_parts: Vec<std::ffi::OsString> = Vec::new();
while !existing_ancestor.exists() {
if let Some(file_name) = existing_ancestor.file_name() {
suffix_parts.push(file_name.to_owned());
}
match existing_ancestor.parent() {
Some(parent) if !parent.as_os_str().is_empty() => {
existing_ancestor = parent.to_path_buf();
}
_ => {
// No existing parent found; fall back to simple check
break;
}
}
}
let ancestor_normalized = normalize_path(&existing_ancestor);
let canonical_ancestor = if existing_ancestor.exists() {
existing_ancestor
.canonicalize()
.unwrap_or(existing_ancestor)
} else {
existing_ancestor
};
// Rebuild the full path from canonicalized ancestor
let mut canonical = canonical_ancestor;
for part in suffix_parts.into_iter().rev() {
canonical.push(part);
}
let canonical = normalize_path(&canonical);
if self.follow_symlinks
&& (ancestor_normalized.starts_with(&workspace_plain)
|| ancestor_normalized.starts_with(&workspace_normalized))
{
return Ok(canonical);
}
// Validate it's under workspace, OR is under a user-trusted external
// path (`/trust add <path>` from the slash command, persisted in
// `~/.deepseek/workspace-trust.json`).
if !canonical.starts_with(workspace_canonical)
&& !canonical.starts_with(&workspace_normalized)
&& !self.is_trusted_external_path(&canonical)
{
return Err(ToolError::PathEscape { path: canonical });
}
Ok(canonical)
}
/// Whether `path` is under any of the user-trusted external roots. The
/// caller should pass an already-canonicalized (or normalized) path.
fn is_trusted_external_path(&self, path: &Path) -> bool {
self.trusted_external_paths
.iter()
.any(|trusted| path.starts_with(trusted))
}
/// Set the trust mode.
#[allow(dead_code)]
pub fn with_trust_mode(mut self, trust: bool) -> Self {
self.trust_mode = trust;
self
}
/// Set the sandbox policy.
#[allow(dead_code)]
pub fn with_sandbox_policy(mut self, policy: SandboxPolicy) -> Self {
self.sandbox_policy = policy;
self
}
/// Set feature flags for tool execution.
pub fn with_features(mut self, features: Features) -> Self {
self.features = features;
self
}
/// Override the shared shell manager.
pub fn with_shell_manager(mut self, shell_manager: SharedShellManager) -> Self {
self.shell_manager = shell_manager;
self
}
/// Reuse the engine's session-scoped read snapshots across tool-context
/// rebuilds. A fresh context is assembled for each turn, but successful
/// reads must remain authoritative until the observed file changes.
pub fn with_file_read_tracker(mut self, tracker: SharedFileReadTracker) -> Self {
self.file_read_tracker = tracker;
self
}
/// Set the elevated sandbox policy override.
///
/// This is used when retrying a tool after a sandbox denial, to run
/// with elevated permissions.
pub fn with_elevated_sandbox_policy(mut self, policy: crate::sandbox::SandboxPolicy) -> Self {
self.elevated_sandbox_policy = Some(policy);
self
}
/// Set the shell network-denial hint used by network-restricted modes.
pub fn with_shell_network_denied_hint(mut self, hint: impl Into<String>) -> Self {
self.shell_network_denied_hint = Some(hint.into());
self
}
/// Set the namespace used for session-scoped tool state.
pub fn with_state_namespace(mut self, namespace: impl Into<String>) -> Self {
self.state_namespace = namespace.into();
self
}
/// Attach the active route's effective context window.
#[must_use]
pub fn with_route_context_window(mut self, context_window: u32) -> Self {
self.route_context_window = (context_window > 0).then_some(context_window);
self
}
/// Attach the large-output router (#548). When set, tool results that
/// exceed the configured token threshold are synthesised by a V4-Flash
/// sub-agent before being returned to the parent context.
#[must_use]
pub fn with_large_output_router(
mut self,
router: crate::tools::large_output_router::LargeOutputRouter,
vars: std::sync::Arc<
tokio::sync::Mutex<crate::tools::large_output_router::WorkshopVariables>,
>,
) -> Self {
self.large_output_router = Some(router);
self.workshop_vars = Some(vars);
self
}
}
/// Gather LSP diagnostics for `paths` using the manager stored in `context`,
/// and return the rendered `<diagnostics …>` blocks joined by newlines.
///
/// Returns an empty string when:
/// - `context.lsp_manager` is `None`
/// - the manager's `enabled` flag is `false`
/// - none of the files produce diagnostics (e.g. all clean, or language unknown)
///
/// This function is non-blocking by design: every failure mode (missing LSP
/// binary, timeout, unknown language) degrades to an empty string rather than
/// propagating an error to the caller.
pub async fn lsp_diagnostics_for_paths(context: &ToolContext, paths: &[PathBuf]) -> String {
use crate::lsp::render_blocks;
let manager = match context.lsp_manager.as_ref() {
Some(m) if m.config().enabled => m,
_ => return String::new(),
};
let mut blocks = Vec::new();
for (idx, path) in paths.iter().enumerate() {
if let Some(block) = manager.diagnostics_for(path, idx as u64).await {
blocks.push(block);
}
}
render_blocks(&blocks)
}
pub(crate) fn normalize_path(path: &Path) -> PathBuf {
let mut prefix: Option<std::ffi::OsString> = None;
let mut is_root = false;
let mut stack: Vec<std::ffi::OsString> = Vec::new();
for component in path.components() {
match component {
Component::Prefix(prefix_component) => {
prefix = Some(prefix_component.as_os_str().to_owned());
}
Component::RootDir => {
is_root = true;
}
Component::CurDir => {}
Component::ParentDir => {
let parent = Component::ParentDir.as_os_str();
if let Some(last) = stack.pop() {
if last == parent {
stack.push(last);
stack.push(parent.to_owned());
}
} else if !is_root {
stack.push(parent.to_owned());
}
}
Component::Normal(part) => {
stack.push(part.to_owned());
}
}
}
let mut normalized = PathBuf::new();
if let Some(prefix) = prefix {
normalized.push(prefix);
}
if is_root {
normalized.push(Path::new(std::path::MAIN_SEPARATOR_STR));
}
for part in stack {
normalized.push(part);
}
normalized
}
/// The core trait that all tools must implement.
#[async_trait]
pub trait ToolSpec: Send + Sync {
/// Returns the unique name of this tool (used in API calls).
fn name(&self) -> &str;
/// Returns a human-readable description of what this tool does.
fn description(&self) -> &str;
/// Returns the JSON Schema for the tool's input parameters.
fn input_schema(&self) -> Value;
/// Returns the capabilities this tool has.
fn capabilities(&self) -> Vec<ToolCapability>;
/// Returns the approval requirement for this tool.
fn approval_requirement(&self) -> ApprovalRequirement {
let caps = self.capabilities();
if caps.contains(&ToolCapability::ExecutesCode) {
ApprovalRequirement::Required
} else if caps.contains(&ToolCapability::WritesFiles) {
ApprovalRequirement::Suggest
} else {
ApprovalRequirement::Auto
}
}
/// Returns the approval requirement for this concrete tool input.
fn approval_requirement_for(&self, _input: &Value) -> ApprovalRequirement {
self.approval_requirement()
}
/// Returns whether this tool is sandboxable.
#[allow(dead_code)]
fn is_sandboxable(&self) -> bool {
self.capabilities().contains(&ToolCapability::Sandboxable)
}
/// Returns whether this tool is read-only.
fn is_read_only(&self) -> bool {
let caps = self.capabilities();
caps.contains(&ToolCapability::ReadOnly)
&& !caps.contains(&ToolCapability::WritesFiles)
&& !caps.contains(&ToolCapability::ExecutesCode)
}
/// Returns whether this concrete tool input is read-only.
fn is_read_only_for(&self, _input: &Value) -> bool {
self.is_read_only()
}
/// Returns whether this tool can be executed in parallel with others.
fn supports_parallel(&self) -> bool {
false
}
/// Returns whether this concrete tool input can run in parallel.
fn supports_parallel_for(&self, _input: &Value) -> bool {
self.supports_parallel()
}
/// Returns whether this input starts durable/detached work and returns
/// immediately. Detached starts are not read-only, but in auto-approved
/// turns they do not need to block neighboring read-only inspections.
fn starts_detached_for(&self, _input: &Value) -> bool {
false
}
/// Resolve input-specific policy without performing external side effects.
///
/// Resource claims deliberately default to global exclusivity until a
/// first-party tool opts into narrower, canonicalized claims. The initial
/// seam records this decision but leaves the existing scheduler unchanged.
fn prepare(&self, input: Value, _context: &ToolContext) -> Result<PreparedToolCall, ToolError> {
Ok(PreparedToolCall {
name: self.name().to_string(),
description: self.description().to_string(),
read_only: self.is_read_only_for(&input),
supports_parallel: self.supports_parallel_for(&input),
starts_detached: self.starts_detached_for(&input),
approval: self.approval_requirement_for(&input),
resources: vec![ResourceClaim::GlobalExclusive],
input,
})
}
/// Returns whether this tool should be excluded from the model-visible
/// tool catalog (deferred loading). Tools marked `true` are registered
/// but not sent to the model until explicitly activated via tool search.
fn defer_loading(&self) -> bool {
false
}
/// Returns whether this tool should be advertised in the model-facing
/// catalog. Hidden compatibility tools remain registered and executable
/// by name so saved transcripts can replay without teaching new sessions
/// the deprecated spelling.
fn model_visible(&self) -> bool {
true
}
/// Execute the tool with the given input and context.
async fn execute(&self, input: Value, context: &ToolContext) -> Result<ToolResult, ToolError>;
/// Execute with rich result blocks. Existing tools inherit text-only
/// behavior; tools such as lowercase `read` can opt in without changing
/// the published `ToolResult` struct.
async fn execute_rich(
&self,
input: Value,
context: &ToolContext,
) -> Result<RichToolResult, ToolError> {
self.execute(input, context)
.await
.map(RichToolResult::plain)
}
}
#[cfg(test)]
mod tests;