a3s-code-core 5.2.3

A3S Code Core - Embeddable AI agent library with tool execution
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Grep tool - Search file contents with regex

use crate::tools::types::{Tool, ToolContext, ToolOutput};
use crate::tools::MAX_OUTPUT_SIZE;
use crate::workspace::{WorkspaceGrepRequest, WorkspaceGrepResult, WorkspacePath};
use anyhow::Result;
use async_trait::async_trait;
use regex::Regex;
use std::collections::{HashMap, HashSet};

const MAX_GREP_SOURCE_ANCHORS: usize = 64;
const MAX_GREP_FALLBACK_CANDIDATES: usize = MAX_GREP_SOURCE_ANCHORS * 4;

pub struct GrepTool;

#[async_trait]
impl Tool for GrepTool {
    fn name(&self) -> &str {
        "grep"
    }

    fn description(&self) -> &str {
        "Search for a pattern in files using ripgrep. Returns matching lines with file paths and line numbers."
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "pattern": {
                    "type": "string",
                    "description": "Required. Regular expression pattern to search for. Always provide this exact field name: 'pattern'."
                },
                "path": {
                    "type": "string",
                    "description": "Optional. Directory or file to search in. Default: workspace root."
                },
                "glob": {
                    "type": "string",
                    "description": "Optional. Glob pattern to filter files, for example '*.rs' or '*.{ts,tsx}'."
                },
                "context": {
                    "type": "integer",
                    "description": "Optional. Number of context lines to show before and after matches."
                },
                "-i": {
                    "type": "boolean",
                    "description": "Optional. Case insensitive search."
                }
            },
            "required": ["pattern"],
            "examples": [
                {
                    "pattern": "TODO"
                },
                {
                    "pattern": "fn main",
                    "path": "src",
                    "glob": "*.rs",
                    "context": 2
                }
            ]
        })
    }

    fn capabilities(&self, _args: &serde_json::Value) -> crate::tools::ToolCapabilities {
        crate::tools::ToolCapabilities::parallel_safe_read(16)
    }

    async fn execute(&self, args: &serde_json::Value, ctx: &ToolContext) -> Result<ToolOutput> {
        let pattern_str = match args.get("pattern").and_then(|v| v.as_str()) {
            Some(p) => p,
            None => return Ok(ToolOutput::error("pattern parameter is required")),
        };

        let case_insensitive = args.get("-i").and_then(|v| v.as_bool()).unwrap_or(false);

        let regex_pattern = if case_insensitive {
            format!("(?i){}", pattern_str)
        } else {
            pattern_str.to_string()
        };

        let regex = match Regex::new(&regex_pattern) {
            Ok(regex) => regex,
            Err(e) => {
                return Ok(ToolOutput::error(format!(
                    "Invalid regex pattern '{}': {}",
                    pattern_str, e
                )))
            }
        };

        let path_str = args.get("path").and_then(|v| v.as_str()).unwrap_or(".");
        let base = match ctx.resolve_workspace_path(path_str) {
            Ok(path) => path,
            Err(e) => return Ok(ToolOutput::error(format!("Failed to resolve path: {}", e))),
        };

        let glob_filter = args.get("glob").and_then(|v| v.as_str());
        let context_lines = args.get("context").and_then(|v| v.as_u64()).unwrap_or(0) as usize;

        let Some(search) = ctx.workspace_services.search() else {
            return Ok(ToolOutput::error(
                "grep is not available: this workspace backend did not provide search",
            ));
        };
        let request = WorkspaceGrepRequest {
            base,
            pattern: pattern_str.to_string(),
            glob: glob_filter.map(str::to_string),
            context_lines,
            case_insensitive,
            max_output_size: MAX_OUTPUT_SIZE,
        };
        let anchor_request = request.clone();
        let outcome = match ctx
            .workspace_services
            .run_with_timeout(
                "grep",
                async move { search.grep_with_sources(request).await },
            )
            .await
        {
            Ok(result) => result,
            Err(e) => return Ok(ToolOutput::error(format!("Grep search failed: {}", e))),
        };

        let source_anchors = grep_source_anchors(
            &outcome.result,
            outcome.matched_paths.as_deref(),
            &anchor_request,
            &regex,
            ctx,
        )
        .await;
        let result = outcome.result;
        let content = if result.match_count == 0 {
            format!("No matches found for pattern: {}", pattern_str)
        } else if result.truncated {
            format!(
                "{}\n... (output truncated)\nFound {} matches in {} files (output truncated)",
                result.output, result.match_count, result.file_count
            )
        } else {
            format!(
                "{}\n{} match(es) in {} file(s)",
                result.output, result.match_count, result.file_count
            )
        };
        let output = ToolOutput::success(content);
        if source_anchors.is_empty() {
            Ok(output)
        } else {
            Ok(output.with_metadata(serde_json::json!({
                "source_anchors": source_anchors,
            })))
        }
    }
}

async fn grep_source_anchors(
    result: &WorkspaceGrepResult,
    matched_paths: Option<&[WorkspacePath]>,
    request: &WorkspaceGrepRequest,
    regex: &Regex,
    ctx: &ToolContext,
) -> Vec<String> {
    let mut anchors = Vec::new();
    let mut seen = HashSet::new();
    if let Some(matched_paths) = matched_paths {
        for path in matched_paths {
            let Ok(path) = ctx.resolve_workspace_path(path.as_str()) else {
                continue;
            };
            if !path.is_root() {
                let path = path.as_str().to_string();
                if !seen.insert(path.clone()) {
                    continue;
                }
                anchors.push(path);
                if anchors.len() >= MAX_GREP_SOURCE_ANCHORS {
                    break;
                }
            }
        }
        return anchors;
    }

    // Legacy/custom backends may provide only display output. Treat it as
    // untrusted: parse only actual-match lines, bound delimiter scanning, and
    // verify every candidate against the original request and file contents.
    let mut candidates: Vec<(String, Vec<usize>)> = Vec::new();
    let mut candidate_indices: HashMap<String, usize> = HashMap::new();
    let mut scanned_candidates = 0usize;
    'lines: for line in result.output.lines() {
        let Some(line) = line.strip_prefix('>') else {
            continue;
        };
        for (delimiter, _) in line.match_indices(':') {
            if scanned_candidates >= MAX_GREP_FALLBACK_CANDIDATES {
                break 'lines;
            }
            scanned_candidates += 1;

            let remainder = &line[delimiter + 1..];
            let digit_count = remainder.bytes().take_while(u8::is_ascii_digit).count();
            if digit_count == 0 || remainder.as_bytes().get(digit_count) != Some(&b':') {
                continue;
            }
            let candidate = &line[..delimiter];
            if candidate.is_empty() || candidate.chars().any(char::is_control) {
                continue;
            }
            let Ok(line_number) = remainder[..digit_count].parse::<usize>() else {
                continue;
            };
            if line_number == 0 {
                continue;
            }
            let Ok(path) = ctx.resolve_workspace_path(candidate) else {
                continue;
            };
            if path.is_root() || !path_matches_grep_request(&path, request) {
                continue;
            }
            let path = path.as_str().to_string();
            if let Some(index) = candidate_indices.get(&path).copied() {
                candidates[index].1.push(line_number);
            } else {
                candidate_indices.insert(path.clone(), candidates.len());
                candidates.push((path, vec![line_number]));
            }
        }
    }

    let fs = ctx.workspace_services.fs();
    for (path, line_numbers) in candidates {
        let workspace_path = WorkspacePath::from_normalized(path.clone());
        let Ok(content) = ctx
            .workspace_services
            .run_with_timeout("grep source verification", fs.read_text(&workspace_path))
            .await
        else {
            continue;
        };
        if line_numbers.into_iter().any(|line_number| {
            content
                .lines()
                .nth(line_number - 1)
                .is_some_and(|line| regex.is_match(line))
        }) {
            if !seen.insert(path.clone()) {
                continue;
            }
            anchors.push(path);
            if anchors.len() >= MAX_GREP_SOURCE_ANCHORS {
                break;
            }
        }
    }
    anchors
}

fn path_matches_grep_request(path: &WorkspacePath, request: &WorkspaceGrepRequest) -> bool {
    let relative = if request.base.is_root() {
        path.as_str()
    } else if path == &request.base {
        path.as_str().rsplit('/').next().unwrap_or(path.as_str())
    } else {
        let Some(relative) = path
            .as_str()
            .strip_prefix(request.base.as_str())
            .and_then(|suffix| suffix.strip_prefix('/'))
        else {
            return false;
        };
        relative
    };

    let Some(glob) = request.glob.as_deref() else {
        return true;
    };
    let Ok(pattern) = glob::Pattern::new(glob) else {
        return false;
    };
    if glob.contains('/') {
        pattern.matches(relative)
    } else {
        pattern.matches(relative.rsplit('/').next().unwrap_or(relative))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    #[tokio::test]
    async fn test_grep_find_pattern() {
        let temp = tempfile::tempdir().unwrap();
        std::fs::write(
            temp.path().join("a.txt"),
            "hello world\nfoo bar\nhello again",
        )
        .unwrap();
        std::fs::write(temp.path().join("b.txt"), "no match here").unwrap();

        let tool = GrepTool;
        let ctx = ToolContext::new(temp.path().to_path_buf());

        let result = tool
            .execute(&serde_json::json!({"pattern": "hello"}), &ctx)
            .await
            .unwrap();

        assert!(result.success);
        assert!(result.content.contains("hello world"));
        assert!(result.content.contains("hello again"));
        assert!(result.content.contains("2 match(es)"));
        assert_eq!(
            result.metadata.unwrap()["source_anchors"],
            serde_json::json!(["a.txt"])
        );
    }

    #[tokio::test]
    async fn test_grep_no_match() {
        let temp = tempfile::tempdir().unwrap();
        std::fs::write(temp.path().join("a.txt"), "hello").unwrap();

        let tool = GrepTool;
        let ctx = ToolContext::new(temp.path().to_path_buf());

        let result = tool
            .execute(&serde_json::json!({"pattern": "xyz"}), &ctx)
            .await
            .unwrap();

        assert!(result.success);
        assert!(result.content.contains("No matches found"));
    }

    #[tokio::test]
    async fn test_grep_case_insensitive() {
        let temp = tempfile::tempdir().unwrap();
        std::fs::write(temp.path().join("a.txt"), "Hello World\nhello world").unwrap();

        let tool = GrepTool;
        let ctx = ToolContext::new(temp.path().to_path_buf());

        let result = tool
            .execute(&serde_json::json!({"pattern": "hello", "-i": true}), &ctx)
            .await
            .unwrap();

        assert!(result.success);
        assert!(result.content.contains("2 match(es)"));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_grep_source_anchor_preserves_newline_filename_without_injection() {
        let temp = tempfile::tempdir().unwrap();
        let filename = "actual\n>injected.txt";
        std::fs::write(temp.path().join(filename), "needle").unwrap();
        let tool = GrepTool;
        let ctx = ToolContext::new(temp.path().to_path_buf());

        let result = tool
            .execute(&serde_json::json!({"pattern": "needle"}), &ctx)
            .await
            .unwrap();

        assert!(result.content.contains(r"actual\n>injected.txt"));
        assert!(!result
            .content
            .lines()
            .any(|line| line.starts_with(">injected.txt:")));
        assert_eq!(
            result.metadata.unwrap()["source_anchors"],
            serde_json::json!([filename])
        );
    }

    #[tokio::test]
    #[cfg(not(windows))]
    async fn test_grep_source_anchor_preserves_colon_filename() {
        let temp = tempfile::tempdir().unwrap();
        let filename = "notes:2026.txt";
        std::fs::write(temp.path().join(filename), "needle").unwrap();
        let tool = GrepTool;
        let ctx = ToolContext::new(temp.path().to_path_buf());

        let result = tool
            .execute(&serde_json::json!({"pattern": "needle"}), &ctx)
            .await
            .unwrap();

        assert_eq!(
            result.metadata.unwrap()["source_anchors"],
            serde_json::json!([filename])
        );
    }

    #[tokio::test]
    async fn test_grep_source_anchor_rejects_nonexistent_injected_path() {
        let temp = tempfile::tempdir().unwrap();
        let ctx = ToolContext::new(temp.path().to_path_buf());
        let request = WorkspaceGrepRequest {
            base: WorkspacePath::root(),
            pattern: "needle".to_string(),
            glob: None,
            context_lines: 0,
            case_insensitive: false,
            max_output_size: MAX_OUTPUT_SIZE,
        };
        let result = WorkspaceGrepResult {
            output: ">ghost.txt:1: needle\n".to_string(),
            match_count: 1,
            file_count: 1,
            truncated: false,
        };

        assert!(grep_source_anchors(
            &result,
            None,
            &request,
            &Regex::new("needle").unwrap(),
            &ctx
        )
        .await
        .is_empty());
    }

    #[tokio::test]
    async fn test_grep_invalid_regex() {
        let tool = GrepTool;
        let ctx = ToolContext::new(PathBuf::from("/tmp"));

        let result = tool
            .execute(&serde_json::json!({"pattern": "[invalid"}), &ctx)
            .await
            .unwrap();

        assert!(!result.success);
        assert!(result.content.contains("Invalid regex"));
    }

    #[tokio::test]
    async fn test_grep_missing_pattern() {
        let tool = GrepTool;
        let ctx = ToolContext::new(PathBuf::from("/tmp"));

        let result = tool.execute(&serde_json::json!({}), &ctx).await.unwrap();
        assert!(!result.success);
    }

    #[test]
    fn test_grep_schema_is_canonical() {
        let tool = GrepTool;
        let params = tool.parameters();
        assert_eq!(params["additionalProperties"], false);
        assert_eq!(params["required"], serde_json::json!(["pattern"]));
        let examples = params["examples"].as_array().unwrap();
        assert_eq!(examples[0]["pattern"], "TODO");
        assert!(examples[0].get("query").is_none());
    }
}