forked from warpdotdev/warp
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathpermissions.rs
More file actions
1214 lines (1100 loc) · 45.4 KB
/
Copy pathpermissions.rs
File metadata and controls
1214 lines (1100 loc) · 45.4 KB
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
use std::{
collections::{HashMap, HashSet},
path::PathBuf,
};
use crate::{
ai::{
agent::conversation::AIConversationId,
execution_profiles::{
profiles::{AIExecutionProfilesModel, ClientProfileId},
AIExecutionProfile, ActionPermission, AskUserQuestionPermission, WriteToPtyPermission,
},
},
report_if_error,
settings::{AISettings, AgentModeCodingPermissionsType, AgentModeCommandExecutionPredicate},
workspaces::{user_workspaces::UserWorkspaces, workspace::AiAutonomySettings},
};
use warp_core::execution_mode::AppExecutionMode;
use crate::ai::mcp::mcp_provider_from_file_path;
#[cfg(not(target_family = "wasm"))]
use crate::ai::mcp::TemplatableMCPServerManager;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use warp_completer::parsers::simple::{command_without_leading_env_vars, decompose_command};
use warp_core::user_preferences::GetUserPreferences;
use warp_core::{features::FeatureFlag, settings::Setting};
use warp_util::path::EscapeChar;
use warpui::{AppContext, Entity, EntityId, ModelContext, SingletonEntity};
use super::BlocklistAIHistoryModel;
/// Whether or not a command can be auto-executed, along with a detailed reason.
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
pub enum CommandExecutionPermission {
Allowed(CommandExecutionPermissionAllowedReason),
Denied(CommandExecutionPermissionDeniedReason),
}
/// Why a command can be auto-executed.
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
pub enum CommandExecutionPermissionAllowedReason {
Dispatched,
ExplicitlyAllowlisted,
IsReadOnlyAndSettingEnabled,
AgentDecided,
AlwaysAllowed,
RunToCompletion,
}
/// Why a command can't be auto-executed.
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
pub enum CommandExecutionPermissionDeniedReason {
AutonomyForceDisabled,
AlwaysAskEnabled,
ExplicitlyDenylisted,
ContainsRedirection,
Inconclusive,
AgentDecided,
}
impl CommandExecutionPermission {
pub fn is_allowed(&self) -> bool {
matches!(self, Self::Allowed(..))
}
}
/// Whether or not a file can be auto-read, along with a detailed reason.
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
pub enum FileReadPermission {
Allowed(FileReadPermissionAllowedReason),
Denied(FileReadPermissionDeniedReason),
}
/// Why a file can be auto-read.
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
pub enum FileReadPermissionAllowedReason {
Dispatched,
AlreadyReadInConvo,
ExplicitlyAllowlisted,
AutoreadSettingEnabled,
AgentDecided,
RunToCompletion,
}
/// Why a file can't be auto-read.
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
pub enum FileReadPermissionDeniedReason {
AutonomyForceDisabled,
AlwaysAskEnabled,
Inconclusive,
AgentDecided,
}
impl FileReadPermission {
pub fn is_allowed(&self) -> bool {
matches!(self, Self::Allowed(..))
}
}
/// Whether or not a file can be auto-written, along with a detailed reason.
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
pub enum FileWritePermission {
Allowed(FileWritePermissionAllowedReason),
Denied(FileWritePermissionDeniedReason),
}
impl FileWritePermission {
pub fn is_allowed(&self) -> bool {
matches!(self, Self::Allowed(..))
}
}
/// Why a file can be written automatically.
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
pub enum FileWritePermissionAllowedReason {
Dispatched,
AgentDecided,
AutowriteSettingEnabled,
RunToCompletion,
}
/// Why a file can't be written automatically.
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
pub enum FileWritePermissionDeniedReason {
AutonomyForceDisabled,
AlwaysAskEnabled,
Inconclusive,
AgentDecided,
/// The path is a system-protected file (e.g. an MCP config) that must never
/// be auto-written regardless of user autonomy settings.
ProtectedPath,
}
/// Describes permissions that Agent Mode has, backed by [`AISettings`].
pub struct BlocklistAIPermissions {
/// A set of one-off files that the user has allowed Agent Mode
/// to read for the duration of a given conversation.
///
/// TODO: remove this once AM doesn't re-request access to the same file in a given convo.
temporary_file_permissions: HashMap<AIConversationId, HashSet<PathBuf>>,
}
impl BlocklistAIPermissions {
pub fn new(ctx: &mut ModelContext<Self>) -> Self {
// Migrate the old `AgentModeAutoReadFiles` setting to the new [`AgentModeCodingPermissionsType`].
if let Some(can_read_files) = ctx
.private_user_preferences()
.read_value("AgentModeAutoReadFiles")
.ok()
.flatten()
.and_then(|s| s.parse().ok())
{
if let Err(e) = ctx
.private_user_preferences()
.remove_value("AgentModeAutoReadFiles")
{
log::error!("Failed to remove old AgentModeAutoReadFiles user pref: {e}");
}
if can_read_files {
report_if_error!(AISettings::handle(ctx).update(ctx, |settings, ctx| {
settings
.agent_mode_coding_permissions
.set_value(AgentModeCodingPermissionsType::AlwaysAllowReading, ctx)
}));
}
}
Self {
temporary_file_permissions: Default::default(),
}
}
/// Returns the active permissions profile, accounting for any enterprise overrides.
pub fn permissions_profile_for_id(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> AIExecutionProfile {
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
let profile = profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx));
let profile_data = profile.data();
AIExecutionProfile {
// Some fields may have an enterprise override.
apply_code_diffs: self.get_apply_code_diffs_setting_for_profile(ctx, profile_id),
read_files: self.get_read_files_setting_for_profile(ctx, profile_id),
execute_commands: self.get_execute_commands_setting_for_profile(ctx, profile_id),
mcp_permissions: self.get_mcp_permissions_setting_for_profile(ctx, profile_id),
write_to_pty: self.get_write_to_pty_setting_for_profile(ctx, profile_id),
command_allowlist: self.get_execute_commands_allowlist_for_profile(ctx, profile_id),
command_denylist: self.get_execute_commands_denylist_for_profile(ctx, profile_id),
directory_allowlist: self.get_read_files_allowlist_for_profile(ctx, profile_id),
mcp_allowlist: self.get_mcp_allowlist_for_profile(ctx, profile_id),
mcp_denylist: self.get_mcp_denylist_for_profile(ctx, profile_id),
computer_use: self.get_computer_use_setting_for_profile(ctx, profile_id),
ask_user_question: self.get_ask_user_question_setting_for_profile(ctx, profile_id),
// Some fields are read directly from the profile.
name: profile_data.name.clone(),
is_default_profile: profile_data.is_default_profile,
base_model: profile_data.base_model.clone(),
coding_model: profile_data.coding_model.clone(),
cli_agent_model: profile_data.cli_agent_model.clone(),
computer_use_model: profile_data.computer_use_model.clone(),
title_model: profile_data.title_model.clone(),
active_ai_model: profile_data.active_ai_model.clone(),
next_command_model: profile_data.next_command_model.clone(),
context_window_limit: profile_data.context_window_limit,
autosync_plans_to_warp_drive: profile_data.autosync_plans_to_warp_drive,
web_search_enabled: profile_data.web_search_enabled,
}
}
pub fn active_permissions_profile(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> AIExecutionProfile {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.permissions_profile_for_id(ctx, *active_profile.id())
}
/// Returns the applicable workspace autonomy settings based on execution mode.
/// In sandboxed mode, returns settings derived from the sandboxed agent config.
/// In unsandboxed mode, returns the standard AI autonomy settings.
fn workspace_autonomy_settings(ctx: &AppContext) -> AiAutonomySettings {
if AppExecutionMode::as_ref(ctx).is_sandboxed() {
let sandboxed = UserWorkspaces::as_ref(ctx).sandboxed_agent_settings();
AiAutonomySettings {
execute_commands_denylist: sandboxed.and_then(|s| s.execute_commands_denylist),
..Default::default()
}
} else {
UserWorkspaces::as_ref(ctx).ai_autonomy_settings()
}
}
pub fn get_apply_code_diffs_setting_for_profile(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> ActionPermission {
let autonomy_settings = Self::workspace_autonomy_settings(ctx);
let apply_code_diffs_workspace_setting = autonomy_settings.apply_code_diffs_setting;
apply_code_diffs_workspace_setting.unwrap_or_else(|| {
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx))
.data()
.apply_code_diffs
})
}
/// Returns what the current setting is for applying code diffs,
/// based on the workspace setting and the active profile.
pub fn get_apply_code_diffs_setting(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> ActionPermission {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.get_apply_code_diffs_setting_for_profile(ctx, *active_profile.id())
}
pub fn get_read_files_setting_for_profile(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> ActionPermission {
let autonomy_settings = Self::workspace_autonomy_settings(ctx);
let read_files_workspace_setting = autonomy_settings.read_files_setting;
read_files_workspace_setting.unwrap_or_else(|| {
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx))
.data()
.read_files
})
}
/// Returns what the current setting is for reading files,
/// based on the workspace setting and the active profile.
pub fn get_read_files_setting(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> ActionPermission {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.get_read_files_setting_for_profile(ctx, *active_profile.id())
}
pub fn get_read_files_allowlist_for_profile(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> Vec<PathBuf> {
let autonomy_settings = Self::workspace_autonomy_settings(ctx);
let read_files_workspace_allowlist = autonomy_settings.read_files_allowlist;
read_files_workspace_allowlist.unwrap_or_else(|| {
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx))
.data()
.directory_allowlist
.clone()
})
}
/// Returns an allowlist of paths that AM should be able to auto-read.
/// Note that the caller is responsible for deciding how the workspace's/user's settings
/// should affect how this gets used, if at all.
pub fn get_read_files_allowlist(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> Vec<PathBuf> {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.get_read_files_allowlist_for_profile(ctx, *active_profile.id())
}
pub fn get_execute_commands_setting_for_profile(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> ActionPermission {
let autonomy_settings = Self::workspace_autonomy_settings(ctx);
let execute_commands_workspace_setting = autonomy_settings.execute_commands_setting;
execute_commands_workspace_setting.unwrap_or_else(|| {
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx))
.data()
.execute_commands
})
}
/// Returns what the current setting is for executing commands,
/// based on the workspace setting and the active profile.
pub fn get_execute_commands_setting(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> ActionPermission {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.get_execute_commands_setting_for_profile(ctx, *active_profile.id())
}
pub fn get_execute_commands_allowlist_for_profile(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> Vec<AgentModeCommandExecutionPredicate> {
let autonomy_settings = Self::workspace_autonomy_settings(ctx);
let execute_commands_workspace_allowlist = autonomy_settings.execute_commands_allowlist;
execute_commands_workspace_allowlist.unwrap_or_else(|| {
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx))
.data()
.command_allowlist
.clone()
})
}
/// Returns an allowlist of command regexes that AM should be able to auto-execute.
/// Note that the caller is responsible for deciding how the workspace's/user's settings
/// should affect how this gets used, if at all.
pub fn get_execute_commands_allowlist(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> Vec<AgentModeCommandExecutionPredicate> {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.get_execute_commands_allowlist_for_profile(ctx, *active_profile.id())
}
pub fn get_execute_commands_denylist_for_profile(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> Vec<AgentModeCommandExecutionPredicate> {
let autonomy_settings = Self::workspace_autonomy_settings(ctx);
autonomy_settings
.execute_commands_denylist
.unwrap_or_else(|| {
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx))
.data()
.command_denylist
.clone()
})
}
/// Returns a denylist of command regexes that AM should not auto-execute.
/// Note that the caller is responsible for deciding how the workspace's/user's settings
/// should affect how this gets used, if at all.
pub fn get_execute_commands_denylist(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> Vec<AgentModeCommandExecutionPredicate> {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.get_execute_commands_denylist_for_profile(ctx, *active_profile.id())
}
pub fn get_write_to_pty_setting_for_profile(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> WriteToPtyPermission {
let autonomy_settings = Self::workspace_autonomy_settings(ctx);
let write_to_pty_workspace_setting = autonomy_settings.write_to_pty_setting;
write_to_pty_workspace_setting.unwrap_or_else(|| {
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx))
.data()
.write_to_pty
})
}
pub fn get_write_to_pty_setting(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> WriteToPtyPermission {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.get_write_to_pty_setting_for_profile(ctx, *active_profile.id())
}
pub fn can_write_to_pty(
&self,
conversation_id: &AIConversationId,
terminal_view_id: Option<EntityId>,
ctx: &AppContext,
) -> WriteToPtyPermission {
if BlocklistAIHistoryModel::as_ref(ctx)
.conversation(conversation_id)
.is_some_and(|convo| convo.autoexecute_any_action())
{
return WriteToPtyPermission::AlwaysAllow;
}
self.get_write_to_pty_setting(ctx, terminal_view_id)
}
pub fn get_mcp_permissions_setting_for_profile(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> ActionPermission {
// TODO: allow a workspace override on MCP permissions.
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx))
.data()
.mcp_permissions
}
pub fn get_mcp_permissions_setting(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> ActionPermission {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.get_mcp_permissions_setting_for_profile(ctx, *active_profile.id())
}
pub fn get_mcp_allowlist_for_profile(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> Vec<uuid::Uuid> {
// TODO: allow a workspace override on MCP allowlist.
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx))
.data()
.mcp_allowlist
.clone()
}
pub fn get_mcp_allowlist(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> Vec<uuid::Uuid> {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.get_mcp_allowlist_for_profile(ctx, *active_profile.id())
}
pub fn get_mcp_denylist_for_profile(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> Vec<uuid::Uuid> {
// TODO: allow a workspace override on MCP denylist.
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx))
.data()
.mcp_denylist
.clone()
}
pub fn get_mcp_denylist(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> Vec<uuid::Uuid> {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.get_mcp_denylist_for_profile(ctx, *active_profile.id())
}
pub fn get_web_search_enabled_for_profile(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> bool {
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx))
.data()
.web_search_enabled
}
pub fn get_web_search_enabled(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> bool {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.get_web_search_enabled_for_profile(ctx, *active_profile.id())
}
pub fn get_computer_use_setting_for_profile(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> crate::ai::execution_profiles::ComputerUsePermission {
let autonomy_settings = Self::workspace_autonomy_settings(ctx);
let computer_use_workspace_setting = autonomy_settings.computer_use_setting;
computer_use_workspace_setting.unwrap_or_else(|| {
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx))
.data()
.computer_use
})
}
pub fn get_computer_use_setting(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> crate::ai::execution_profiles::ComputerUsePermission {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.get_computer_use_setting_for_profile(ctx, *active_profile.id())
}
pub fn get_ask_user_question_setting_for_profile(
&self,
ctx: &AppContext,
profile_id: ClientProfileId,
) -> AskUserQuestionPermission {
let profiles_model = AIExecutionProfilesModel::as_ref(ctx);
profiles_model
.get_profile_by_id(profile_id, ctx)
.unwrap_or_else(|| profiles_model.default_profile(ctx))
.data()
.ask_user_question
}
pub fn get_ask_user_question_setting(
&self,
ctx: &AppContext,
terminal_view_id: Option<EntityId>,
) -> AskUserQuestionPermission {
let active_profile =
AIExecutionProfilesModel::as_ref(ctx).active_profile(terminal_view_id, ctx);
self.get_ask_user_question_setting_for_profile(ctx, *active_profile.id())
}
/// Returns whether or not Agent Mode can auto-read the given files.
pub fn can_read_files_with_conversation(
&self,
conversation_id: &AIConversationId,
paths: Vec<PathBuf>,
terminal_view_id: Option<EntityId>,
ctx: &AppContext,
) -> FileReadPermission {
if BlocklistAIHistoryModel::as_ref(ctx)
.conversation(conversation_id)
.is_some_and(|convo| convo.autoexecute_any_action())
{
return FileReadPermission::Allowed(FileReadPermissionAllowedReason::RunToCompletion);
}
self.can_read_files(Some(conversation_id), paths, terminal_view_id, ctx)
}
/// Returns whether or not Zap can auto-read the given files (e.g. for codebase indexing).
pub fn can_read_files(
&self,
conversation_id: Option<&AIConversationId>,
paths: Vec<PathBuf>,
terminal_view_id: Option<EntityId>,
ctx: &AppContext,
) -> FileReadPermission {
if paths.is_empty() {
// We can vacuously read 0 files.
return FileReadPermission::Allowed(
FileReadPermissionAllowedReason::ExplicitlyAllowlisted,
);
}
// Check if we've already been given permission to read these files in this conversation.
if let Some(temp_permissions) =
conversation_id.and_then(|id| self.temporary_file_permissions.get(id))
{
if paths.iter().all(|path| {
temp_permissions
.iter()
.any(|allowed| path.starts_with(allowed))
}) {
return FileReadPermission::Allowed(
FileReadPermissionAllowedReason::AlreadyReadInConvo,
);
}
}
match self.get_read_files_setting(ctx, terminal_view_id) {
ActionPermission::AgentDecides | ActionPermission::Unknown => {
// For now, we always read files. We don't ask the user for permission.
FileReadPermission::Allowed(FileReadPermissionAllowedReason::AgentDecided)
}
ActionPermission::AlwaysAllow => {
FileReadPermission::Allowed(FileReadPermissionAllowedReason::AutoreadSettingEnabled)
}
ActionPermission::AlwaysAsk => {
let allowlisted_paths = self.get_read_files_allowlist(ctx, terminal_view_id);
if paths
.iter()
.all(|p| allowlisted_paths.iter().any(|dir| p.starts_with(dir)))
{
FileReadPermission::Allowed(
FileReadPermissionAllowedReason::ExplicitlyAllowlisted,
)
} else {
FileReadPermission::Denied(FileReadPermissionDeniedReason::AlwaysAskEnabled)
}
}
}
}
/// Returns whether or not Agent Mode can automatically write to files.
pub fn can_write_files(
&self,
conversation_id: &AIConversationId,
paths: &[PathBuf],
terminal_view_id: Option<EntityId>,
ctx: &AppContext,
) -> FileWritePermission {
// Protected paths are always denied, regardless of autonomy settings.
if let Some(denied) = check_protected_write_paths(paths) {
return denied;
}
if BlocklistAIHistoryModel::as_ref(ctx)
.conversation(conversation_id)
.is_some_and(|convo| convo.autoexecute_any_action())
{
return FileWritePermission::Allowed(FileWritePermissionAllowedReason::RunToCompletion);
}
self.determine_write_permissions_from_active_profile(terminal_view_id, ctx)
}
#[cfg(not(target_family = "wasm"))]
pub fn can_call_mcp_tool(
&self,
server_id: Option<&uuid::Uuid>,
name: &str,
conversation_id: &AIConversationId,
terminal_view_id: Option<EntityId>,
ctx: &AppContext,
) -> bool {
let templatable_manager = TemplatableMCPServerManager::as_ref(ctx);
// Try resolving via server UUID first, then fall back to tool-name lookup.
// On recent clients, the server UUID should always be set - we should eventually
// require the server UUID.
let mut uuid_of_mcp_server =
server_id.and_then(|id| templatable_manager.get_template_uuid(*id));
// Prefer templatable MCP servers over legacy when a tool name exists in both.
// Fall back to legacy behavior if templatable lookup fails or is disabled.
if uuid_of_mcp_server.is_none() {
uuid_of_mcp_server = templatable_manager
.server_from_tool(name.to_string())
.copied()
.and_then(|installation_uuid| {
templatable_manager.get_template_uuid(installation_uuid)
});
}
self.can_use_mcp_server(conversation_id, uuid_of_mcp_server, terminal_view_id, ctx)
}
/// Returns whether or not Agent Mode can automatically read the given MCP resource.
#[cfg(not(target_family = "wasm"))]
pub fn can_read_mcp_resource(
&self,
server_id: Option<&uuid::Uuid>,
name: &str,
uri: Option<&str>,
conversation_id: &AIConversationId,
terminal_view_id: Option<EntityId>,
ctx: &AppContext,
) -> bool {
let templatable_manager = TemplatableMCPServerManager::as_ref(ctx);
// Try resolving via server UUID first, then fall back to resource name/URI lookup.
// On recent clients, the server UUID should always be set - we should eventually
// require the server UUID.
let mut uuid_of_mcp_server =
server_id.and_then(|id| templatable_manager.get_template_uuid(*id));
// Prefer templatable MCP servers over legacy when a resource name exists in both.
// Fall back to legacy behavior if templatable lookup fails or is disabled.
if uuid_of_mcp_server.is_none() {
uuid_of_mcp_server = templatable_manager
.server_from_resource(name, uri)
.copied()
.and_then(|installation_uuid| {
templatable_manager.get_template_uuid(installation_uuid)
});
}
self.can_use_mcp_server(conversation_id, uuid_of_mcp_server, terminal_view_id, ctx)
}
/// Checks whether the given MCP server (identified by its template UUID) is permitted
/// to be used based on the current MCP permission setting and allowlist/denylist.
#[cfg(not(target_family = "wasm"))]
fn can_use_mcp_server(
&self,
conversation_id: &AIConversationId,
uuid_of_mcp_server: Option<uuid::Uuid>,
terminal_view_id: Option<EntityId>,
ctx: &AppContext,
) -> bool {
if BlocklistAIHistoryModel::as_ref(ctx)
.conversation(conversation_id)
.is_some_and(|convo| convo.autoexecute_any_action())
{
return true;
}
let allowlisted = uuid_of_mcp_server
.is_some_and(|uid| self.get_mcp_allowlist(ctx, terminal_view_id).contains(&uid));
let denylisted = uuid_of_mcp_server
.is_some_and(|uid| self.get_mcp_denylist(ctx, terminal_view_id).contains(&uid));
match self.get_mcp_permissions_setting(ctx, terminal_view_id) {
ActionPermission::AgentDecides | ActionPermission::Unknown => {
allowlisted && !denylisted
}
ActionPermission::AlwaysAllow => !denylisted,
ActionPermission::AlwaysAsk => allowlisted && !denylisted,
}
}
// Helper function to evaluate the active profile + workspace settings.
fn determine_write_permissions_from_active_profile(
&self,
terminal_view_id: Option<EntityId>,
ctx: &AppContext,
) -> FileWritePermission {
match self.get_apply_code_diffs_setting(ctx, terminal_view_id) {
ActionPermission::AgentDecides | ActionPermission::Unknown => {
FileWritePermission::Denied(FileWritePermissionDeniedReason::AgentDecided)
}
ActionPermission::AlwaysAllow => FileWritePermission::Allowed(
FileWritePermissionAllowedReason::AutowriteSettingEnabled,
),
ActionPermission::AlwaysAsk => {
FileWritePermission::Denied(FileWritePermissionDeniedReason::AlwaysAskEnabled)
}
}
}
#[allow(clippy::too_many_arguments)]
/// Returns whether or not Agent Mode can auto-execute the given command.
pub fn can_autoexecute_command(
&self,
conversation_id: &AIConversationId,
command: &str,
escape_char: EscapeChar,
is_read_only: bool,
is_risky: Option<bool>,
terminal_view_id: Option<EntityId>,
ctx: &AppContext,
) -> CommandExecutionPermission {
// Normalize line continuations based on shell type.
// POSIX shells (bash/zsh/fish) use backslash, PowerShell uses backtick.
let normalized_command = match escape_char {
EscapeChar::Backslash => command.replace("\\\n", " "),
EscapeChar::Backtick => command.replace("`\n", " "),
};
// The command string might be composed of multiple commands so let's
// break it up first.
let (commands, contains_redirection) = decompose_command(&normalized_command, escape_char);
// Strip leading env-var assignments (e.g. `X=1 rm file.txt`) before denylist
// matching so a blocked command can't be hidden behind an env-var prefix. This
// is applied to the denylist check ONLY — never to allowlist matching below.
let commands_for_denylist = commands
.iter()
.map(|command| command_for_execution_predicates(command, escape_char))
.collect::<Vec<_>>();
// The denylist takes precedence over all other conditions.
let denylist = self.get_execute_commands_denylist(ctx, terminal_view_id);
if commands_for_denylist
.iter()
.any(|c| denylist.iter().any(|d| d.matches(c)))
{
return CommandExecutionPermission::Denied(
CommandExecutionPermissionDeniedReason::ExplicitlyDenylisted,
);
}
if BlocklistAIHistoryModel::as_ref(ctx)
.conversation(conversation_id)
.is_some_and(|convo| convo.autoexecute_any_action())
{
return CommandExecutionPermission::Allowed(
CommandExecutionPermissionAllowedReason::RunToCompletion,
);
}
match self.get_execute_commands_setting(ctx, terminal_view_id) {
ActionPermission::AgentDecides | ActionPermission::Unknown => {
if FeatureFlag::AgentDecidesCommandExecution.is_enabled() && is_risky == Some(false)
{
return CommandExecutionPermission::Allowed(
CommandExecutionPermissionAllowedReason::AgentDecided,
);
}
if contains_redirection {
return CommandExecutionPermission::Denied(
CommandExecutionPermissionDeniedReason::ContainsRedirection,
);
}
let allowlist = self.get_execute_commands_allowlist(ctx, terminal_view_id);
if commands.iter().all(|command| {
allowlist
.iter()
.any(|allowlist_item| allowlist_item.matches(command))
}) {
return CommandExecutionPermission::Allowed(
CommandExecutionPermissionAllowedReason::ExplicitlyAllowlisted,
);
}
// For now, the heuristic is if the command is read only or if we're executing
// a plan. Otherwise, we don't want to autoexecute.
if is_read_only {
CommandExecutionPermission::Allowed(
CommandExecutionPermissionAllowedReason::AgentDecided,
)
} else {
CommandExecutionPermission::Denied(
CommandExecutionPermissionDeniedReason::AgentDecided,
)
}
}
ActionPermission::AlwaysAllow => CommandExecutionPermission::Allowed(
CommandExecutionPermissionAllowedReason::AlwaysAllowed,
),
ActionPermission::AlwaysAsk => {
let allowlist = self.get_execute_commands_allowlist(ctx, terminal_view_id);
if commands.iter().all(|command| {
allowlist
.iter()
.any(|allowlist_item| allowlist_item.matches(command))
}) {
CommandExecutionPermission::Allowed(
CommandExecutionPermissionAllowedReason::ExplicitlyAllowlisted,
)
} else {
CommandExecutionPermission::Denied(
CommandExecutionPermissionDeniedReason::AlwaysAskEnabled,
)
}
}
}
}
/// Allows Agent Mode to auto-execute commands that match `command`.
///
/// The denylist (see [`Self::add_command_to_autoexecution_denylist`])
/// takes precedence over the allowlist.
pub fn add_command_to_autoexecution_allowlist(
&mut self,
command: AgentModeCommandExecutionPredicate,
ctx: &mut ModelContext<Self>,
) -> Result<()> {
let mut allowlist = AISettings::as_ref(ctx)
.agent_mode_command_execution_allowlist
.clone();
allowlist.push(command);
AISettings::handle(ctx).update(ctx, |settings, ctx| {
settings
.agent_mode_command_execution_allowlist
.set_value(allowlist, ctx)
})
}
/// Removes `command` from the auto-execution allowlist.
///
/// See [`Self::add_command_to_autoexecution_allowlist`] for more about the allowlist.
pub fn remove_command_from_autoexecution_allowlist(
&mut self,
command: &AgentModeCommandExecutionPredicate,
ctx: &mut ModelContext<Self>,
) -> Result<()> {
let mut allowlist = AISettings::as_ref(ctx)
.agent_mode_command_execution_allowlist
.clone();
allowlist.retain(|c| c != command);
AISettings::handle(ctx).update(ctx, |settings, ctx| {
settings
.agent_mode_command_execution_allowlist
.set_value(allowlist, ctx)
})
}
/// Forces Agent Mode to ask for user consent before executing commands that match `command`.
pub fn add_command_to_autoexecution_denylist(
&mut self,
command: AgentModeCommandExecutionPredicate,
ctx: &mut ModelContext<Self>,
) -> Result<()> {
let mut denylist = AISettings::as_ref(ctx)
.agent_mode_command_execution_denylist
.clone();
denylist.push(command);
AISettings::handle(ctx).update(ctx, |settings, ctx| {
settings
.agent_mode_command_execution_denylist
.set_value(denylist, ctx)
})
}