merry-tools 0.1.3

Workspace file, search, patch, web fetch, and document tools for the Merry agent runtime.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
use std::{
    fs,
    io::{BufReader, Read, Seek, SeekFrom},
    path::Path,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
    time::{Duration, Instant},
};

use merry_core::PendingToolCall;
use merry_runtime::{
    PathAccessRule, ToolExecutionContext, ToolExecutionError, ToolExecutionOutcome, ToolExecutor,
    ToolExecutorFuture,
};
use serde::Serialize;

use crate::{
    READ_FILE_TOOL,
    config::WorkspaceToolLimits,
    errors::{
        BlockingToolError, DomainError, ERROR_FILE_NOT_FOUND, ERROR_FILE_TOO_LARGE,
        ERROR_INVALID_ARGUMENTS, ERROR_NOT_FILE, ERROR_READ_FAILED, failed_outcome,
    },
    file::read_bounded,
    path::{
        ValidatedToolPath, open_file_for_read, resolve_existing_path,
        validate_workspace_path_argument,
    },
    state::WorkspaceToolState,
    trace::{
        WorkspaceTraceFinish, WorkspaceTracePath, WorkspaceTraceTarget, invalid_arguments_outcome,
        trace_workspace_tool_finish, trace_workspace_tool_start,
    },
};

mod bounded;
mod doc;
mod docx;
mod image;
mod input;
mod notebook;
mod ooxml;
mod pdf;
#[cfg(feature = "pdf")]
mod pdf_hayro;
mod pptx;
mod xlsx;

pub use image::WorkspaceReadImageSuccess;
#[cfg(test)]
pub(crate) use image::{
    MAX_EMBEDDED_IMAGE_DIMENSION, MAX_EMBEDDED_IMAGE_PIXELS, MAX_IMAGE_SOURCE_BYTES, png_crc32,
};
pub(crate) use input::{PdfReadFormat, ReadFileInput, spec};
pub use pdf::WorkspaceReadPdfPagesSuccess;
#[cfg(all(test, feature = "pdf"))]
pub(crate) use pdf::parse_page_range;

const IMAGE_SNIFF_BYTES: usize = 512;
const READ_FILE_TIMEOUT: Duration = Duration::from_secs(60);

#[derive(Debug)]
pub(crate) struct ReadFileExecutor {
    pub(crate) state: Arc<WorkspaceToolState>,
}

impl ToolExecutor for ReadFileExecutor {
    fn execute<'a>(
        &'a self,
        call: PendingToolCall,
        context: ToolExecutionContext,
    ) -> ToolExecutorFuture<'a> {
        Box::pin(async move {
            if context.cancellation_token().is_cancelled() {
                return Err(ToolExecutionError::Cancelled);
            }
            let args = match input::parse(&call) {
                Ok(args) => args,
                Err(message) => {
                    return Ok(invalid_arguments_outcome(
                        READ_FILE_TOOL,
                        call.id().as_str(),
                        message,
                    ));
                }
            };
            let trace_path = WorkspaceTracePath::new(args.path.as_str());
            trace_workspace_tool_start(
                READ_FILE_TOOL,
                call.id().as_str(),
                WorkspaceTraceTarget::Path(trace_path.as_ref()),
            );
            let state = Arc::clone(&self.state);
            let worker_token = context.cancellation_token().child_token();
            let _cancel_on_drop = worker_token.clone().drop_guard();
            let approved_path_grants = context.approved_path_grants().to_vec();
            let path_for_timeout = args.path.clone();
            let deadline = Instant::now() + READ_FILE_TIMEOUT;
            let timed_out = Arc::new(AtomicBool::new(false));
            let worker_timed_out = Arc::clone(&timed_out);
            let result = match tokio::task::spawn_blocking(move || {
                read_file_blocking_checked(&state, args, &approved_path_grants, &|| {
                    if worker_token.is_cancelled() {
                        return true;
                    }
                    if Instant::now() >= deadline {
                        worker_timed_out.store(true, Ordering::Relaxed);
                        return true;
                    }
                    false
                })
            })
            .await
            {
                Ok(Err(ToolExecutionError::Cancelled)) if timed_out.load(Ordering::Relaxed) => {
                    Ok(failed_outcome(
                        READ_FILE_TOOL,
                        ERROR_READ_FAILED,
                        format!("read_file timed out after {}s", READ_FILE_TIMEOUT.as_secs()),
                        Some(path_for_timeout),
                    ))
                }
                Ok(result) => result,
                Err(error) => Err(ToolExecutionError::infrastructure(format!(
                    "read_file task failed to join: {error}"
                ))),
            };
            let finish = match &result {
                Ok(outcome) => WorkspaceTraceFinish::Outcome(outcome),
                Err(error) => WorkspaceTraceFinish::from_error(error),
            };
            trace_workspace_tool_finish(
                READ_FILE_TOOL,
                call.id().as_str(),
                WorkspaceTraceTarget::Path(trace_path.as_ref()),
                finish,
            );
            result
        })
    }
}

#[derive(Debug, Serialize)]
struct ReadTextSuccess<'a> {
    ok: bool,
    tool: &'static str,
    path: &'a str,
    start_line: usize,
    end_line: Option<usize>,
    lines: usize,
    bytes: usize,
    truncated: bool,
    content: &'a str,
}

impl ReadTextSuccess<'_> {
    fn model_text(&self) -> String {
        let Some(end_line) = self.end_line else {
            return if self.start_line == 1 {
                format!("{}: empty file", self.path)
            } else {
                format!(
                    "{}: no lines returned from line {} (past end of file)",
                    self.path, self.start_line
                )
            };
        };
        let mut text = format!(
            "{}:{}-{end_line}\n{}",
            self.path, self.start_line, self.content
        );
        if self.truncated {
            if !text.ends_with('\n') {
                text.push('\n');
            }
            text.push_str(&format!(
                "[truncated: more file content remains; read from line {}]",
                end_line.saturating_add(1)
            ));
        }
        text
    }
}

#[cfg(test)]
pub(crate) fn read_file_blocking(
    state: &WorkspaceToolState,
    args: ReadFileInput,
) -> ToolExecutionOutcome {
    read_file_blocking_checked(state, args, &[], &|| false)
        .expect("uncancelled file read should return an outcome")
}

fn read_file_blocking_checked(
    state: &WorkspaceToolState,
    args: ReadFileInput,
    approved_path_grants: &[PathAccessRule],
    is_cancelled: &dyn Fn() -> bool,
) -> Result<ToolExecutionOutcome, ToolExecutionError> {
    if is_cancelled() {
        return Err(ToolExecutionError::Cancelled);
    }
    let relative = match validate_workspace_path_argument(&args.path, &state.root) {
        Ok(relative) => relative,
        Err(error) => {
            return Ok(failed_outcome(
                READ_FILE_TOOL,
                error.code,
                error.message,
                error.path,
            ));
        }
    };
    let start_line = args.start_line.unwrap_or(1);
    let max_lines = args.max_lines.unwrap_or(state.limits.max_read_lines);
    if start_line == 0 || max_lines == 0 || max_lines > state.limits.max_read_lines {
        return Ok(failed_outcome(
            READ_FILE_TOOL,
            ERROR_INVALID_ARGUMENTS,
            "read_file line range is outside the configured limit",
            Some(relative.display),
        ));
    }
    if is_cancelled() {
        return Err(ToolExecutionError::Cancelled);
    }
    if relative.relative_spelling().is_none()
        && state.is_hard_denied_for_read(Path::new(&relative.display), approved_path_grants)
    {
        return Ok(failed_outcome(
            READ_FILE_TOOL,
            crate::errors::ERROR_PATH_DENIED,
            "path is denied by Merry security policy",
            Some(relative.display),
        ));
    }
    let resolved = match resolve_existing_path(&relative, state.read_roots()) {
        Ok(Some(resolved)) => resolved,
        Ok(None) => {
            return Ok(failed_outcome(
                READ_FILE_TOOL,
                ERROR_FILE_NOT_FOUND,
                "workspace file was not found",
                Some(relative.display),
            ));
        }
        Err(error) => {
            return Ok(failed_outcome(
                READ_FILE_TOOL,
                error.code,
                error.message,
                Some(relative.display),
            ));
        }
    };
    if let Err(error) = state.validate_read_boundary(&relative, &resolved) {
        return Ok(failed_outcome(
            READ_FILE_TOOL,
            error.code,
            error.message,
            Some(relative.display),
        ));
    }
    match state.authorize_read_access_policy(&resolved, approved_path_grants) {
        Ok(()) => {}
        Err(crate::state::ReadAccessPolicyError::Denied(error)) => {
            return Ok(failed_outcome(
                READ_FILE_TOOL,
                error.code,
                error.message,
                Some(relative.display),
            ));
        }
        Err(crate::state::ReadAccessPolicyError::PermissionRequired(requirement)) => {
            return Err(ToolExecutionError::PermissionRequired(requirement));
        }
    }
    let options = ReadFileOptions {
        start_line,
        max_lines,
        pages: args.pages.as_deref(),
        pdf_format: args.format.unwrap_or_default(),
    };
    match read_resolved_file(&relative, &resolved, options, &state.limits, is_cancelled) {
        Ok((outcome, is_text)) => {
            if is_text
                && let Some(policy) = &state.external_tree_policy
                && let Err(error) = policy.on_text_read(&resolved)
            {
                tracing::debug!(
                    error = %error,
                    path = %resolved.display(),
                    "external tree text-read hook reported an error"
                );
            }
            Ok(outcome)
        }
        Err(BlockingToolError::Cancelled) => Err(ToolExecutionError::Cancelled),
        Err(BlockingToolError::Domain(error)) => Ok(failed_outcome(
            READ_FILE_TOOL,
            error.code,
            error.message,
            Some(relative.display),
        )),
    }
}

struct ReadFileOptions<'a> {
    start_line: usize,
    max_lines: usize,
    pages: Option<&'a str>,
    pdf_format: PdfReadFormat,
}

fn read_resolved_file(
    relative: &ValidatedToolPath,
    path: &Path,
    options: ReadFileOptions<'_>,
    limits: &WorkspaceToolLimits,
    is_cancelled: &dyn Fn() -> bool,
) -> Result<(ToolExecutionOutcome, bool), BlockingToolError> {
    if is_cancelled() {
        return Err(BlockingToolError::Cancelled);
    }
    let mut file = open_file_for_read(path)?;
    let metadata = file.metadata().map_err(|_| {
        DomainError::new(
            ERROR_READ_FAILED,
            "could not inspect workspace file metadata",
        )
    })?;
    if !metadata.is_file() {
        return Err(
            DomainError::new(ERROR_NOT_FILE, "workspace path is not a regular file").into(),
        );
    }

    let sniff = sniff_leading_bytes(&mut file)?;

    if has_extension(path, "svg") && sniff.starts_with(b"\x89PNG\r\n\x1a\n") {
        let bytes = read_bounded(&mut file, limits.max_read_bytes, is_cancelled)?;
        if let Some(svg_text) = image::extract_svg_after_png_prefix(&bytes) {
            return Ok((
                slice_text_outcome(
                    relative,
                    &svg_text,
                    options.start_line,
                    options.max_lines,
                    limits,
                ),
                true,
            ));
        }
    }

    if image::detect_raster_image_format(path, &sniff).is_some() {
        let outcome = image::read_resolved_image(relative, &mut file, is_cancelled)?;
        return Ok((outcome, false));
    }

    if pdf::is_pdf_file(path, &sniff) {
        let outcome = pdf::read_resolved_pdf(
            relative,
            &mut file,
            pdf::PdfReadRequest {
                pages: options.pages,
                format: options.pdf_format,
                start_line: options.start_line,
                max_lines: options.max_lines,
            },
            limits,
            is_cancelled,
        )?;
        return Ok((outcome, false));
    }

    if has_extension(path, "pptx") {
        let outcome = pptx::read_resolved_pptx(
            relative,
            &mut file,
            options.start_line,
            options.max_lines,
            limits,
            is_cancelled,
        )?;
        return Ok((outcome, false));
    }

    if has_extension(path, "docx") {
        let outcome = docx::read_resolved_docx(
            relative,
            &mut file,
            options.start_line,
            options.max_lines,
            limits,
            is_cancelled,
        )?;
        return Ok((outcome, false));
    }

    if has_extension(path, "doc") {
        let outcome = doc::read_resolved_doc(
            relative,
            &mut file,
            options.start_line,
            options.max_lines,
            limits,
            is_cancelled,
        )?;
        return Ok((outcome, false));
    }

    if has_extension(path, "xlsx") {
        let outcome = xlsx::read_resolved_xlsx(
            relative,
            &mut file,
            options.start_line,
            options.max_lines,
            limits,
            is_cancelled,
        )?;
        return Ok((outcome, false));
    }

    if has_extension(path, "ipynb") {
        let outcome = notebook::read_resolved_notebook(
            relative,
            &mut file,
            options.start_line,
            options.max_lines,
            limits,
            is_cancelled,
        )?;
        return Ok((outcome, false));
    }

    let outcome = read_resolved_text(
        relative,
        file,
        metadata.len(),
        options.start_line,
        options.max_lines,
        limits,
        is_cancelled,
    )?;
    Ok((outcome, true))
}

pub(super) fn has_extension(path: &Path, expected: &str) -> bool {
    path.extension()
        .and_then(|ext| ext.to_str())
        .is_some_and(|ext| ext.eq_ignore_ascii_case(expected))
}

fn sniff_leading_bytes(file: &mut fs::File) -> Result<Vec<u8>, DomainError> {
    let mut sniff = Vec::with_capacity(IMAGE_SNIFF_BYTES);
    Read::by_ref(file)
        .take(IMAGE_SNIFF_BYTES as u64)
        .read_to_end(&mut sniff)
        .map_err(|_| DomainError::new(ERROR_READ_FAILED, "could not read workspace file"))?;
    file.seek(SeekFrom::Start(0))
        .map_err(|_| DomainError::new(ERROR_READ_FAILED, "could not seek workspace file"))?;
    Ok(sniff)
}

/// Applies `start_line`, `max_lines`, and `max_read_bytes` to extracted text
/// (used by PDF text mode, PPTX slides, Jupyter notebooks, and SVG fallback).
pub(super) fn slice_text_outcome(
    relative: &ValidatedToolPath,
    full_text: &str,
    start_line: usize,
    max_lines: usize,
    limits: &WorkspaceToolLimits,
) -> ToolExecutionOutcome {
    let mut content = String::new();
    let mut selected_lines = 0usize;
    let mut current_line = 0usize;
    let mut truncated = false;

    for line in full_text.split_inclusive('\n') {
        current_line = current_line.saturating_add(1);
        if current_line < start_line {
            continue;
        }
        if selected_lines == max_lines {
            truncated = true;
            break;
        }
        if content.len().saturating_add(line.len()) > limits.max_read_bytes {
            if selected_lines == 0 {
                let mut end = limits.max_read_bytes.min(line.len());
                while end > 0 && !line.is_char_boundary(end) {
                    end -= 1;
                }
                content.push_str(&line[..end]);
                selected_lines = 1;
            }
            truncated = true;
            break;
        }
        content.push_str(line);
        selected_lines += 1;
    }

    let payload = ReadTextSuccess {
        ok: true,
        tool: READ_FILE_TOOL,
        path: &relative.display,
        start_line,
        end_line: (selected_lines > 0).then(|| start_line.saturating_add(selected_lines - 1)),
        lines: selected_lines,
        bytes: content.len(),
        truncated,
        content: &content,
    };
    ToolExecutionOutcome::succeeded_json(
        serde_json::to_string(&payload).expect("read_file success envelope serializes"),
    )
    .with_model_text(payload.model_text())
}

fn read_resolved_text(
    relative: &ValidatedToolPath,
    file: fs::File,
    file_bytes: u64,
    start_line: usize,
    max_lines: usize,
    limits: &WorkspaceToolLimits,
    is_cancelled: &dyn Fn() -> bool,
) -> Result<ToolExecutionOutcome, BlockingToolError> {
    if is_cancelled() {
        return Err(BlockingToolError::Cancelled);
    }
    let byte_limit = u64::try_from(limits.max_read_bytes)
        .map_err(|_| DomainError::new(ERROR_FILE_TOO_LARGE, "workspace read limit is too large"))?;
    let mut reader = BufReader::new(file.take(byte_limit.saturating_add(1)));
    let mut content = String::new();
    let mut scanned_bytes = 0usize;
    let mut selected_lines = 0usize;
    let mut current_line = 0usize;
    let mut truncated = false;
    loop {
        let line = bounded::read_line(
            &mut reader,
            limits.max_read_bytes - scanned_bytes,
            is_cancelled,
        )?;
        if line.is_empty() {
            break;
        }
        scanned_bytes += line.len();
        current_line = current_line.saturating_add(1);
        if current_line < start_line {
            continue;
        }
        content.push_str(&line);
        selected_lines += 1;
        if selected_lines == max_lines {
            truncated = (scanned_bytes as u64) < file_bytes;
            break;
        }
    }
    if is_cancelled() {
        return Err(BlockingToolError::Cancelled);
    }
    let payload = ReadTextSuccess {
        ok: true,
        tool: READ_FILE_TOOL,
        path: &relative.display,
        start_line,
        end_line: (selected_lines > 0).then(|| start_line.saturating_add(selected_lines - 1)),
        lines: selected_lines,
        bytes: content.len(),
        truncated,
        content: &content,
    };
    Ok(ToolExecutionOutcome::succeeded_json(
        serde_json::to_string(&payload).expect("read_file success envelope serializes"),
    )
    .with_model_text(payload.model_text()))
}