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
|
use std::{
io::BufRead,
path::{Path, PathBuf},
time::Duration,
};
use eyre::Result;
const DEFAULT_FILE_READ_LINES: u64 = 100;
const MAX_FILE_READ_LINES: u64 = 1000;
pub(crate) mod descriptor;
use crate::permissions::rule::Rule;
/// Check whether a file path matches a scope glob pattern.
///
/// Resolves relative paths against the current directory before matching so
/// that `./foo.md` and `/cwd/foo.md` match the same glob. Supports `*`, `**`,
/// `?`, and `[...]` via `glob_match`.
fn path_matches_scope(path: &Path, scope: &str) -> bool {
let path = if path.is_relative() {
std::env::current_dir()
.map(|cwd| cwd.join(path))
.unwrap_or_else(|_| path.to_path_buf())
} else {
path.to_path_buf()
};
// Normalize to forward slashes so globs work on Windows too.
let path_str = path.to_string_lossy().replace('\\', "/");
// If the scope is also relative, try matching against both the absolute
// path and just the filename/relative portion.
if !scope.starts_with('/') {
// Match against filename (e.g. "*.md" matches any .md file)
if let Some(name) = path.file_name().and_then(|n| n.to_str())
&& glob_match::glob_match(scope, name)
{
return true;
}
// Also try matching against the full absolute path in case the scope
// is a relative multi-segment pattern like "crates/**/*.rs"
if glob_match::glob_match(scope, &path_str) {
return true;
}
// And match relative to cwd (so "src/*.rs" works from project root)
if let Ok(cwd) = std::env::current_dir()
&& let Ok(rel) = path.strip_prefix(&cwd)
{
let rel_str = rel.to_string_lossy().replace('\\', "/");
return glob_match::glob_match(scope, &rel_str);
}
return false;
}
// Absolute scope — match against absolute path
glob_match::glob_match(scope, &path_str)
}
/// Result of executing a client-side tool.
pub(crate) enum ToolOutcome {
/// Simple success with a text result (used by Read, AtuinHistory).
Success(String),
/// Error with a message.
Error(String),
/// Structured shell result with separated stdout, stderr, exit code, and duration.
Structured {
stdout: String,
stderr: String,
exit_code: Option<i32>,
duration_ms: u64,
interrupted: bool,
},
}
impl ToolOutcome {
/// Format this outcome as a string for the tool result sent to the LLM.
pub fn format_for_llm(&self) -> String {
match self {
ToolOutcome::Success(s) => s.clone(),
ToolOutcome::Error(e) => e.clone(),
ToolOutcome::Structured {
stdout,
stderr,
exit_code,
duration_ms,
interrupted,
} => {
let mut parts = Vec::new();
if let Some(code) = exit_code {
parts.push(format!("Exit code: {code}"));
}
parts.push(format!("Duration: {duration_ms}ms"));
if !stdout.is_empty() {
parts.push(format!("stdout:\n{stdout}"));
} else {
parts.push("stdout: (empty)".to_string());
}
if !stderr.is_empty() {
parts.push(format!("stderr:\n{stderr}"));
} else {
parts.push("stderr: (empty)".to_string());
}
if *interrupted {
parts.push("[Interrupted by user]".to_string());
}
parts.join("\n\n")
}
}
}
/// Whether this outcome represents an error.
pub fn is_error(&self) -> bool {
match self {
ToolOutcome::Error(_) => true,
ToolOutcome::Structured {
exit_code: Some(code),
..
} if *code != 0 => true,
_ => false,
}
}
}
/// Cached VT100 preview data for a shell tool call.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ToolPreview {
pub lines: Vec<String>,
pub exit_code: Option<i32>,
pub interrupted: bool,
}
/// Lifecycle phase of a tracked tool call.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ToolPhase {
CheckingPermissions,
AskingForPermission,
#[expect(dead_code)]
Denied(String),
#[expect(dead_code)]
Executing,
/// Shell command is executing with live preview output.
ExecutingWithPreview {
command: String,
/// Current VT100 screen lines (plain text, viewport-sized).
output_lines: Vec<String>,
/// Exit code once the process completes.
exit_code: Option<i32>,
/// Whether the command was interrupted by the user.
interrupted: bool,
},
/// Tool execution has completed. Preview is cached for rendering history.
Completed {
preview: Option<ToolPreview>,
},
}
/// A tracked tool call through its full lifecycle.
#[derive(Debug)]
pub(crate) struct TrackedTool {
pub id: String,
pub tool: ClientToolCall,
pub phase: ToolPhase,
/// Sender to interrupt a running shell command (only set during ExecutingWithPreview).
pub abort_tx: Option<tokio::sync::oneshot::Sender<()>>,
}
impl TrackedTool {
pub(crate) fn target_dir(&self) -> Option<&Path> {
self.tool.target_dir()
}
pub fn mark_asking(&mut self) {
self.phase = ToolPhase::AskingForPermission;
}
pub fn mark_executing_preview(&mut self, command: String) {
self.phase = ToolPhase::ExecutingWithPreview {
command,
output_lines: Vec::new(),
exit_code: None,
interrupted: false,
};
}
pub fn complete(&mut self, preview: Option<ToolPreview>) {
self.phase = ToolPhase::Completed { preview };
self.abort_tx = None;
}
/// Extract the current preview, whether live or completed.
pub fn preview(&self) -> Option<ToolPreview> {
match &self.phase {
ToolPhase::ExecutingWithPreview {
output_lines,
exit_code,
interrupted,
..
} => Some(ToolPreview {
lines: output_lines.clone(),
exit_code: *exit_code,
interrupted: *interrupted,
}),
ToolPhase::Completed { preview } => preview.clone(),
_ => None,
}
}
}
/// Tracks all tool calls through their full lifecycle.
///
/// Single source of truth for tool execution state. Entries persist after
/// completion so cached previews remain available for rendering history.
#[derive(Debug)]
pub(crate) struct ToolTracker {
tools: Vec<TrackedTool>,
}
impl ToolTracker {
pub fn new() -> Self {
Self { tools: Vec::new() }
}
/// Insert a new tool call in CheckingPermissions phase.
pub fn insert(&mut self, id: String, tool: ClientToolCall) {
self.tools.push(TrackedTool {
id,
tool,
phase: ToolPhase::CheckingPermissions,
abort_tx: None,
});
}
pub fn get(&self, id: &str) -> Option<&TrackedTool> {
self.tools.iter().find(|t| t.id == id)
}
pub fn get_mut(&mut self, id: &str) -> Option<&mut TrackedTool> {
self.tools.iter_mut().find(|t| t.id == id)
}
/// Remove a tool by ID and return it.
#[expect(dead_code)]
pub fn remove(&mut self, id: &str) -> Option<TrackedTool> {
let pos = self.tools.iter().position(|t| t.id == id)?;
Some(self.tools.remove(pos))
}
/// True if any tool is still awaiting a permission decision.
#[expect(dead_code)]
pub fn has_unresolved(&self) -> bool {
self.tools.iter().any(|t| {
matches!(
t.phase,
ToolPhase::CheckingPermissions | ToolPhase::AskingForPermission
)
})
}
/// True if any tool has not yet reached the Completed phase.
/// Use this to gate `ContinueAfterTools` — we must wait for all tools
/// (including those still executing) before resuming the conversation.
pub fn has_pending(&self) -> bool {
self.tools
.iter()
.any(|t| !matches!(t.phase, ToolPhase::Completed { .. }))
}
/// True if any tool is currently executing with a preview.
pub fn has_executing_preview(&self) -> bool {
self.tools
.iter()
.any(|t| matches!(t.phase, ToolPhase::ExecutingWithPreview { .. }))
}
/// Find the first tool that is asking for permission.
pub fn asking_for_permission(&self) -> Option<&TrackedTool> {
self.tools
.iter()
.find(|t| t.phase == ToolPhase::AskingForPermission)
}
/// Find the first tool that is asking for permission (mutable).
#[expect(dead_code)]
pub fn asking_for_permission_mut(&mut self) -> Option<&mut TrackedTool> {
self.tools
.iter_mut()
.find(|t| t.phase == ToolPhase::AskingForPermission)
}
/// Get the preview for a tool by ID (live or cached).
pub fn preview_for(&self, id: &str) -> Option<ToolPreview> {
self.get(id)?.preview()
}
/// Iterate mutably over all tracked tools.
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut TrackedTool> {
self.tools.iter_mut()
}
}
/// A tool call from the server, with parsed input parameters.
#[derive(Debug, Clone)]
pub(crate) enum ClientToolCall {
Read(ReadToolCall),
Write(WriteToolCall),
Shell(ShellToolCall),
AtuinHistory(AtuinHistoryToolCall),
}
impl TryFrom<(&str, &serde_json::Value)> for ClientToolCall {
type Error = eyre::Error;
fn try_from((name, input): (&str, &serde_json::Value)) -> Result<Self, Self::Error> {
match name {
"read_file" => Ok(ClientToolCall::Read(ReadToolCall::try_from(input)?)),
"create_file" => Ok(ClientToolCall::Write(WriteToolCall::try_from(input)?)),
// "append_to_file" => Ok(ClientToolCall::Append(AppendToolCall::try_from(input)?)),
// "str_replace" => Ok(ClientToolCall::StrReplace(StrReplaceToolCall::try_from(input)?)),
"execute_shell_command" => Ok(ClientToolCall::Shell(ShellToolCall::try_from(input)?)),
"atuin_history" => Ok(ClientToolCall::AtuinHistory(
AtuinHistoryToolCall::try_from(input)?,
)),
_ => Err(eyre::eyre!("Unknown tool call: {name}")),
}
}
}
impl ClientToolCall {
pub(crate) fn descriptor(&self) -> &'static descriptor::ToolDescriptor {
match self {
ClientToolCall::Read(_) => descriptor::READ,
ClientToolCall::Write(_) => descriptor::WRITE,
ClientToolCall::Shell(_) => descriptor::SHELL,
ClientToolCall::AtuinHistory(_) => descriptor::ATUIN_HISTORY,
}
}
/// The permission rule name for this tool category (e.g. "Write" covers
/// str_replace, file_create, file_insert).
pub(crate) fn rule_name(&self) -> &'static str {
match self {
ClientToolCall::Read(_) => "Read",
ClientToolCall::Write(_) => "Write",
ClientToolCall::Shell(_) => "Shell",
ClientToolCall::AtuinHistory(_) => "AtuinHistory",
}
}
pub(crate) fn matches_rule(&self, rule: &Rule) -> bool {
match self {
ClientToolCall::Read(tool) => tool.matches_rule(rule),
ClientToolCall::Write(tool) => tool.matches_rule(rule),
ClientToolCall::Shell(tool) => tool.matches_rule(rule),
ClientToolCall::AtuinHistory(tool) => tool.matches_rule(rule),
}
}
pub(crate) fn target_dir(&self) -> Option<&Path> {
match self {
ClientToolCall::Read(tool) => tool.target_dir(),
ClientToolCall::Write(tool) => tool.target_dir(),
ClientToolCall::Shell(tool) => tool.target_dir(),
ClientToolCall::AtuinHistory(tool) => tool.target_dir(),
}
}
/// Execute this client-side tool and return the result.
pub async fn execute(&self, db: &atuin_client::database::Sqlite) -> ToolOutcome {
match self {
ClientToolCall::Read(tool) => tool.execute(),
ClientToolCall::AtuinHistory(tool) => tool.execute(db).await,
_ => ToolOutcome::Error("Client-side tool execution not yet implemented".to_string()),
}
}
}
/// A trait for tool calls that can be checked against permission rules.
pub(crate) trait PermissableToolCall {
/// Checks if this tool call matches the given permission rule.
fn matches_rule(&self, rule: &Rule) -> bool;
/// Returns the target directory of this tool call, if applicable, for checking against directory-based rules.
fn target_dir(&self) -> Option<&Path> {
None
}
}
impl PermissableToolCall for ClientToolCall {
fn matches_rule(&self, rule: &Rule) -> bool {
self.matches_rule(rule)
}
fn target_dir(&self) -> Option<&Path> {
self.target_dir()
}
}
#[derive(Debug, Clone)]
pub(crate) struct ReadToolCall {
pub path: PathBuf,
pub offset: u64,
pub limit: u64,
}
impl TryFrom<&serde_json::Value> for ReadToolCall {
type Error = eyre::Error;
fn try_from(value: &serde_json::Value) -> Result<Self, Self::Error> {
let path = value
.get("file_path")
.and_then(|v| v.as_str())
.ok_or(eyre::eyre!("Missing path"))?;
let offset = value.get("offset").and_then(|v| v.as_u64()).unwrap_or(0);
let limit = value
.get("limit")
.and_then(|v| v.as_u64())
.unwrap_or(DEFAULT_FILE_READ_LINES)
.min(MAX_FILE_READ_LINES);
Ok(ReadToolCall {
path: PathBuf::from(path),
offset,
limit,
})
}
}
impl ReadToolCall {
fn execute(&self) -> ToolOutcome {
let mut path = self.path.clone();
if path.is_relative()
&& let Ok(current_dir) = std::env::current_dir()
{
path = current_dir.join(path);
}
if !path.exists() {
return ToolOutcome::Error(format!("Error: file does not exist: {}", path.display()));
}
if path.is_dir() {
let Some(files) = std::fs::read_dir(&path).ok().and_then(|entries| {
entries
.filter_map(|entry| entry.ok())
.map(|entry| entry.file_name().to_string_lossy().to_string())
.collect::<Vec<_>>()
.into()
}) else {
return ToolOutcome::Error(format!(
"Error: could not read directory: {}",
path.display()
));
};
return ToolOutcome::Success(format!("Directory contents:\n{}", files.join("\n")));
}
let file = match std::fs::File::open(&path) {
Ok(file) => file,
Err(e) => return ToolOutcome::Error(format!("Error opening file: {e}")),
};
let reader = std::io::BufReader::new(file);
let relevent_lines = reader
.lines()
.skip(self.offset as usize)
.take(self.limit as usize)
.collect::<Result<Vec<_>, _>>();
match relevent_lines {
Ok(lines) => {
let joined = lines.join("\n");
if joined.len() > 100_000 {
ToolOutcome::Error(format!(
"Error: file is too large to read ({} bytes in {} lines); use view_range to read a subset of the file",
joined.len(),
lines.len()
))
} else {
ToolOutcome::Success(joined)
}
}
Err(e) => ToolOutcome::Error(format!("Error reading file: {e}")),
}
}
}
impl PermissableToolCall for ReadToolCall {
fn target_dir(&self) -> Option<&Path> {
Some(&self.path)
}
fn matches_rule(&self, rule: &Rule) -> bool {
if rule.tool != "Read" {
return false;
}
match rule.scope.as_deref() {
None | Some("*") => true,
Some(scope) => path_matches_scope(&self.path, scope),
}
}
}
#[derive(Debug, Clone)]
#[expect(dead_code)]
pub(crate) struct WriteToolCall {
pub path: PathBuf,
pub content: String,
}
impl TryFrom<&serde_json::Value> for WriteToolCall {
type Error = eyre::Error;
fn try_from(value: &serde_json::Value) -> Result<Self, Self::Error> {
let path = value
.get("path")
.and_then(|v| v.as_str())
.ok_or(eyre::eyre!("Missing path"))?;
let content = value
.get("content")
.and_then(|v| v.as_str())
.ok_or(eyre::eyre!("Missing content"))?;
Ok(WriteToolCall {
path: PathBuf::from(path),
content: content.to_string(),
})
}
}
impl PermissableToolCall for WriteToolCall {
fn target_dir(&self) -> Option<&Path> {
Some(&self.path)
}
fn matches_rule(&self, rule: &Rule) -> bool {
if rule.tool != "Write" {
return false;
}
match rule.scope.as_deref() {
None | Some("*") => true,
Some(scope) => path_matches_scope(&self.path, scope),
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct ShellToolCall {
pub dir: Option<PathBuf>,
pub command: String,
pub shell: String,
}
impl TryFrom<&serde_json::Value> for ShellToolCall {
type Error = eyre::Error;
fn try_from(value: &serde_json::Value) -> Result<Self, Self::Error> {
let dir = value.get("dir").and_then(|v| v.as_str());
let command = value
.get("command")
.and_then(|v| v.as_str())
.ok_or(eyre::eyre!("Missing command"))?;
let shell = value
.get("shell")
.and_then(|v| v.as_str())
.unwrap_or("bash")
.to_string();
Ok(ShellToolCall {
dir: dir.map(PathBuf::from),
command: command.to_string(),
shell,
})
}
}
impl PermissableToolCall for ShellToolCall {
fn target_dir(&self) -> Option<&Path> {
self.dir.as_deref()
}
fn matches_rule(&self, rule: &Rule) -> bool {
if rule.tool != "Shell" {
return false;
}
let Some(scope) = rule.scope.as_ref() else {
// Shell without scope matches all shell commands
return true;
};
let shell_kind = crate::permissions::shell::ShellKind::from_shell_name(&self.shell);
let parsed = crate::permissions::shell::parse_shell_command(&self.command, shell_kind);
crate::permissions::shell::any_subcommand_matches(&parsed.subcommands, scope)
}
}
/// Preview viewport height for VT100 emulation.
const PREVIEW_HEIGHT: u16 = 10;
/// Default terminal width for VT100 emulation.
const PREVIEW_WIDTH: u16 = 120;
/// Extract plain text lines from a VT100 screen buffer.
fn vt100_screen_lines(screen: &vt100::Screen) -> Vec<String> {
let (rows, cols) = screen.size();
let mut lines = Vec::with_capacity(rows as usize);
for row in 0..rows {
let mut line = String::with_capacity(cols as usize);
for col in 0..cols {
if let Some(cell) = screen.cell(row, col) {
line.push_str(cell.contents());
}
}
// Trim trailing whitespace for cleaner display
lines.push(line.trim_end().to_string());
}
lines
}
/// Strip ANSI escape sequences from raw bytes using a VT100 parser.
///
/// Uses a large virtual screen so scrollback is preserved, then extracts
/// the plain text contents. This handles all escape sequences (colors,
/// cursor movement, progress bars, etc.) not just simple SGR codes.
fn strip_ansi_via_vt100(raw: &[u8]) -> String {
if raw.is_empty() {
return String::new();
}
// Use the contents_formatted → screen approach: feed bytes into a parser
// with enough rows to hold everything, then read back the plain text.
// Estimate rows: one row per ~PREVIEW_WIDTH bytes, plus generous padding.
let estimated_rows = (raw.len() / PREVIEW_WIDTH as usize + 1).min(10_000) as u16;
let mut parser = vt100::Parser::new(estimated_rows, PREVIEW_WIDTH, 0);
parser.process(raw);
let screen = parser.screen();
// screen.contents() returns the full plain-text content with trailing
// whitespace trimmed per line and trailing blank lines removed.
screen.contents()
}
/// Execute a shell command with VT100 emulation and streaming output.
///
/// Feeds stdout+stderr into a `vt100::Parser` so that ANSI escape sequences,
/// progress bars (`\r`), and cursor movement are handled correctly. Periodically
/// sends the current screen state as `Vec<String>` through `output_tx` for the
/// live preview.
///
/// Captures the FULL stdout and stderr separately for the tool result sent to the LLM.
/// Returns a `ToolOutcome::Structured` with full output, exit code, and duration.
pub(crate) async fn execute_shell_command_streaming(
shell_call: &ShellToolCall,
output_tx: tokio::sync::mpsc::Sender<Vec<String>>,
mut interrupt_rx: tokio::sync::oneshot::Receiver<()>,
) -> ToolOutcome {
use tokio::io::AsyncReadExt;
let start = std::time::Instant::now();
// TODO: check if this is proper for all shells we support
let mut cmd = tokio::process::Command::new(&shell_call.shell);
cmd.arg("-c").arg(&shell_call.command);
cmd.stdout(std::process::Stdio::piped());
cmd.stderr(std::process::Stdio::piped());
if let Some(ref dir) = shell_call.dir {
cmd.current_dir(dir);
}
let mut child = match cmd.spawn() {
Ok(child) => child,
Err(e) => return ToolOutcome::Error(format!("Failed to spawn command: {e}")),
};
let stdout = child.stdout.take().expect("stdout was piped");
let stderr = child.stderr.take().expect("stderr was piped");
// VT100 emulator for the live preview (viewport-sized)
let mut parser = vt100::Parser::new(PREVIEW_HEIGHT, PREVIEW_WIDTH, 0);
let mut stdout_reader = tokio::io::BufReader::new(stdout);
let mut stderr_reader = tokio::io::BufReader::new(stderr);
let mut stdout_buf = [0u8; 4096];
let mut stderr_buf = [0u8; 4096];
let mut stdout_done = false;
let mut stderr_done = false;
// Full output buffers (for the LLM, not the preview)
let mut full_stdout = Vec::<u8>::new();
let mut full_stderr = Vec::<u8>::new();
let mut interval = tokio::time::interval(Duration::from_millis(50));
// Send initial empty screen
let initial_lines = vt100_screen_lines(parser.screen());
let _ = output_tx.send(initial_lines).await;
let mut interrupted = false;
loop {
tokio::select! {
biased;
// Check for interrupt signal
_ = &mut interrupt_rx, if !interrupted => {
interrupted = true;
let _ = child.start_kill();
}
// Read stdout
result = stdout_reader.read(&mut stdout_buf), if !stdout_done => {
match result {
Ok(0) => stdout_done = true,
Ok(n) => {
full_stdout.extend_from_slice(&stdout_buf[..n]);
parser.process(&stdout_buf[..n]);
}
Err(_) => stdout_done = true,
}
}
// Read stderr
result = stderr_reader.read(&mut stderr_buf), if !stderr_done => {
match result {
Ok(0) => stderr_done = true,
Ok(n) => {
full_stderr.extend_from_slice(&stderr_buf[..n]);
// Feed stderr to the preview parser too, so it shows in the VT100 screen
parser.process(&stderr_buf[..n]);
}
Err(_) => stderr_done = true,
}
}
// Periodic screen snapshot for preview
_ = interval.tick() => {
let lines = vt100_screen_lines(parser.screen());
let _ = output_tx.send(lines).await;
}
}
// Exit when both streams are done
if stdout_done && stderr_done {
break;
}
}
// Wait for process to finish
let exit_code = match child.wait().await {
Ok(status) => status.code(),
Err(e) => {
if interrupted {
None
} else {
return ToolOutcome::Error(format!("Failed to wait for command: {e}"));
}
}
};
let duration = start.elapsed();
// Send final screen state
let final_lines = vt100_screen_lines(parser.screen());
let _ = output_tx.send(final_lines).await;
// Strip ANSI escape sequences for clean LLM output by running
// the raw bytes through a VT100 parser and extracting plain text.
let stdout_text = strip_ansi_via_vt100(&full_stdout);
let stderr_text = strip_ansi_via_vt100(&full_stderr);
ToolOutcome::Structured {
stdout: stdout_text,
stderr: stderr_text,
exit_code,
duration_ms: duration.as_millis() as u64,
interrupted,
}
}
#[derive(Debug, Clone)]
pub(crate) struct AtuinHistoryToolCall {
pub filter_modes: Vec<HistorySearchFilterMode>,
pub query: String,
pub limit: i64,
}
#[derive(Debug, Clone)]
pub(crate) enum HistorySearchFilterMode {
Global,
Host,
Session,
Directory,
Workspace,
}
impl From<&HistorySearchFilterMode> for atuin_client::settings::FilterMode {
fn from(mode: &HistorySearchFilterMode) -> Self {
match mode {
HistorySearchFilterMode::Global => Self::Global,
HistorySearchFilterMode::Host => Self::Host,
HistorySearchFilterMode::Session => Self::Session,
HistorySearchFilterMode::Directory => Self::Directory,
HistorySearchFilterMode::Workspace => Self::Workspace,
}
}
}
impl TryFrom<&serde_json::Value> for AtuinHistoryToolCall {
type Error = eyre::Error;
fn try_from(value: &serde_json::Value) -> Result<Self, Self::Error> {
let filter_modes = value
.get("filter_modes")
.and_then(|v| v.as_array())
.ok_or(eyre::eyre!("Missing filter_modes"))?;
let filter_modes = filter_modes
.iter()
.map(|v| {
let mode = v.as_str().ok_or(eyre::eyre!("Invalid filter mode"))?;
match mode {
"global" => Ok(HistorySearchFilterMode::Global),
"host" => Ok(HistorySearchFilterMode::Host),
"session" => Ok(HistorySearchFilterMode::Session),
"directory" => Ok(HistorySearchFilterMode::Directory),
"workspace" => Ok(HistorySearchFilterMode::Workspace),
_ => Err(eyre::eyre!("Invalid filter mode: {mode}")),
}
})
.collect::<Result<Vec<HistorySearchFilterMode>>>()?;
let query = value
.get("query")
.and_then(|v| v.as_str())
.ok_or(eyre::eyre!("Missing query"))?;
let limit = value
.get("limit")
.and_then(|v| v.as_i64())
.unwrap_or(10)
.clamp(1, 50);
Ok(AtuinHistoryToolCall {
filter_modes,
query: query.to_string(),
limit,
})
}
}
impl PermissableToolCall for AtuinHistoryToolCall {
fn target_dir(&self) -> Option<&Path> {
None
}
fn matches_rule(&self, rule: &Rule) -> bool {
rule.tool == "AtuinHistory"
}
}
impl AtuinHistoryToolCall {
pub(crate) async fn execute(&self, db: &atuin_client::database::Sqlite) -> ToolOutcome {
use atuin_client::database::{self, Database as _, OptFilters};
use atuin_client::settings::SearchMode;
use time::UtcOffset;
let context = match database::current_context().await {
Ok(ctx) => ctx,
Err(e) => return ToolOutcome::Error(format!("Failed to get history context: {e}")),
};
let filter_mode = self
.filter_modes
.first()
.map(atuin_client::settings::FilterMode::from)
.unwrap_or(atuin_client::settings::FilterMode::Global);
let filter_options = OptFilters {
limit: Some(self.limit),
..Default::default()
};
let results = match db
.search(
SearchMode::Fuzzy,
filter_mode,
&context,
&self.query,
filter_options,
)
.await
{
Ok(results) => results,
Err(e) => return ToolOutcome::Error(format!("History search failed: {e}")),
};
if results.is_empty() {
return ToolOutcome::Success("No matching history entries found.".to_string());
}
let local_offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC);
let formatted: Vec<String> = results
.iter()
.enumerate()
.map(|(i, h)| {
let ts = h.timestamp.to_offset(local_offset);
let time_str = format!(
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
ts.year(),
ts.month() as u8,
ts.day(),
ts.hour(),
ts.minute(),
ts.second(),
);
let duration_str = format_duration(h.duration);
format!(
"{}. `{}` [{}] ({}, exit: {}){}",
i + 1,
h.command,
time_str,
h.cwd,
h.exit,
duration_str,
)
})
.collect();
ToolOutcome::Success(formatted.join("\n"))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn read_rule(scope: Option<&str>) -> Rule {
Rule {
tool: "Read".to_string(),
scope: scope.map(String::from),
}
}
fn write_rule(scope: Option<&str>) -> Rule {
Rule {
tool: "Write".to_string(),
scope: scope.map(String::from),
}
}
fn read_tool(path: &str) -> ReadToolCall {
ReadToolCall {
path: PathBuf::from(path),
offset: 0,
limit: 100,
}
}
fn write_tool(path: &str) -> WriteToolCall {
WriteToolCall {
path: PathBuf::from(path),
content: String::new(),
}
}
// ── Cross-platform tests ──
#[test]
fn no_scope_matches_everything() {
assert!(read_tool("any/path.txt").matches_rule(&read_rule(None)));
assert!(write_tool("any/path.txt").matches_rule(&write_rule(None)));
}
#[test]
fn wildcard_star_matches_everything() {
assert!(read_tool("foo/bar.rs").matches_rule(&read_rule(Some("*"))));
}
#[test]
fn wrong_tool_never_matches() {
assert!(!read_tool("foo.txt").matches_rule(&write_rule(None)));
assert!(!write_tool("foo.txt").matches_rule(&read_rule(None)));
}
#[test]
fn extension_glob() {
assert!(read_tool("notes.md").matches_rule(&read_rule(Some("*.md"))));
assert!(!read_tool("notes.txt").matches_rule(&read_rule(Some("*.md"))));
}
#[test]
fn relative_multi_segment_glob() {
// This matches against the path relative to cwd
let cwd = std::env::current_dir().unwrap();
let abs = cwd
.join("crates")
.join("atuin-ai")
.join("src")
.join("lib.rs");
let tool = read_tool(abs.to_str().unwrap());
assert!(tool.matches_rule(&read_rule(Some("crates/**/*.rs"))));
assert!(!tool.matches_rule(&read_rule(Some("crates/**/*.py"))));
}
// ── Unix-specific tests (absolute paths with forward slashes) ──
#[cfg(unix)]
mod unix {
use super::*;
#[test]
fn absolute_glob() {
assert!(
read_tool("/home/user/src/main.rs")
.matches_rule(&read_rule(Some("/home/user/src/*.rs")))
);
assert!(
!read_tool("/home/user/docs/readme.md")
.matches_rule(&read_rule(Some("/home/user/src/*.rs")))
);
}
#[test]
fn double_star_glob() {
assert!(
read_tool("/project/crates/foo/src/lib.rs")
.matches_rule(&read_rule(Some("/project/crates/**/*.rs")))
);
assert!(
!read_tool("/project/crates/foo/src/lib.py")
.matches_rule(&read_rule(Some("/project/crates/**/*.rs")))
);
}
}
// ── Windows-specific tests (absolute paths with drive letters) ──
#[cfg(windows)]
mod windows {
use super::*;
#[test]
fn absolute_glob() {
assert!(
read_tool(r"C:\Users\dev\src\main.rs")
.matches_rule(&read_rule(Some("C:/Users/dev/src/*.rs")))
);
assert!(
!read_tool(r"C:\Users\dev\docs\readme.md")
.matches_rule(&read_rule(Some("C:/Users/dev/src/*.rs")))
);
}
#[test]
fn double_star_glob() {
assert!(
read_tool(r"C:\project\crates\foo\src\lib.rs")
.matches_rule(&read_rule(Some("C:/project/crates/**/*.rs")))
);
assert!(
!read_tool(r"C:\project\crates\foo\src\lib.py")
.matches_rule(&read_rule(Some("C:/project/crates/**/*.rs")))
);
}
}
}
fn format_duration(nanos: i64) -> String {
if nanos <= 0 {
return String::new();
}
let total_secs = nanos / 1_000_000_000;
let millis = (nanos % 1_000_000_000) / 1_000_000;
if total_secs >= 3600 {
let hours = total_secs / 3600;
let mins = (total_secs % 3600) / 60;
let secs = total_secs % 60;
format!(", {hours}h{mins}m{secs}s")
} else if total_secs >= 60 {
let mins = total_secs / 60;
let secs = total_secs % 60;
format!(", {mins}m{secs}s")
} else if total_secs > 0 {
if millis > 0 {
format!(", {total_secs}.{millis:03}s")
} else {
format!(", {total_secs}s")
}
} else {
format!(", {millis}ms")
}
}
|