rpi-tools 0.28.0

Built-in agent tools (read/write/edit/bash/grep/find/ls) + ExecutionEnv, Rust port of pi harness tools
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
//! Mirrors `packages/agent/src/harness/types.ts::ExecutionEnv` — the trait every
//! tool and test-double implements. `ExecutionEnv = FileSystem + Shell`.
//!
//! Contract (plan §5.13): **every method must never panic.** All failures are
//! encoded in `Result<_, FileError>` / `Result<_, ExecutionError>`. Paths may be
//! absolute or relative to [`FileSystem::cwd`]; returned paths are "addressed"
//! (symlink-canonicalized only by [`FileSystem::canonical_path`]).
//!
//! The TS `AbortSignal` becomes `&tokio_util::sync::CancellationToken` on every
//! method. Backends SHOULD check `is_cancelled()` at entry and honor cooperative
//! cancellation during blocking ops via `tokio::select!`.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use async_trait::async_trait;
use tokio_util::sync::CancellationToken;

use crate::error::{ExecutionError, FileError, FileErrorCode};

/// `file` | `directory` | `symlink`. Mirrors TS `FileKind`.
///
/// Backends detect via `symlink_metadata` (lstat) — symlinks are NOT followed
/// automatically by any `FileSystem` method except `canonical_path`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileKind {
    File,
    Directory,
    Symlink,
}

/// Mirrors TS `FileInfo`. `path` is the absolute addressed (resolved) path —
/// NOT symlink-canonicalized. `mtime_ms` is ms since Unix epoch.
#[derive(Debug, Clone)]
pub struct FileInfo {
    pub name: String,
    pub path: PathBuf,
    pub kind: FileKind,
    pub size: u64,
    pub mtime_ms: i64,
}

/// Write/append payload. Mirrors the TS `string | Uint8Array` union.
#[derive(Debug, Clone)]
pub enum FileContent {
    Text(String),
    Bytes(Vec<u8>),
}

impl FileContent {
    pub fn as_bytes(&self) -> &[u8] {
        match self {
            FileContent::Text(s) => s.as_bytes(),
            FileContent::Bytes(b) => b,
        }
    }
}

impl From<String> for FileContent {
    fn from(s: String) -> Self {
        FileContent::Text(s)
    }
}

impl From<&str> for FileContent {
    fn from(s: &str) -> Self {
        FileContent::Text(s.to_string())
    }
}

impl From<Vec<u8>> for FileContent {
    fn from(b: Vec<u8>) -> Self {
        FileContent::Bytes(b)
    }
}

/// Result of `Shell::exec`. Mirrors the TS inline return type
/// `{ stdout, stderr, exitCode }`. Signal kills collapse to `exit_code: 0`
/// (matching the TS `code ?? 0` rule).
#[derive(Debug, Clone)]
pub struct ShellOutput {
    pub stdout: String,
    pub stderr: String,
    pub exit_code: i32,
}

/// Mirrors TS `ShellExecOptions` (note: `timeout` is in **seconds**, matching
/// the TS contract). `on_stdout` / `on_stderr` fire per chunk, interleaved in
/// arrival order; the bash-tool wraps these with throttling + capture.
#[derive(Clone, Debug)]
pub struct ShellOverride {
    pub program: PathBuf,
    pub args: Vec<String>,
}

pub struct ShellExecOptions<'a> {
    /// Optional executable and fixed arguments. `None` keeps the environment's
    /// default shell (Bash on POSIX/Windows), while tools such as PowerShell
    /// can select their native interpreter without going through Bash.
    pub shell: Option<ShellOverride>,
    pub cwd: Option<PathBuf>,
    pub env: Option<HashMap<String, String>>,
    /// Default `true`. When `false`, ONLY `env` is used (no process env, no
    /// shell base env).
    pub inherit_env: bool,
    /// Seconds. `None` = no timeout.
    pub timeout: Option<f64>,
    pub cancel: Option<&'a CancellationToken>,
    pub on_stdout: Option<Box<dyn FnMut(&str) + Send + 'a>>,
    pub on_stderr: Option<Box<dyn FnMut(&str) + Send + 'a>>,
}

impl<'a> Default for ShellExecOptions<'a> {
    fn default() -> Self {
        Self {
            shell: None,
            cwd: None,
            env: None,
            inherit_env: true,
            timeout: None,
            cancel: None,
            on_stdout: None,
            on_stderr: None,
        }
    }
}

impl<'a> ShellExecOptions<'a> {
    pub fn new() -> Self {
        Self::default()
    }
}

/// A process-local capability proving exclusive ownership of a file-backed
/// writer. Backends that cannot provide an OS/process boundary return `None`
/// from [`FileSystem::try_acquire_writer_lease`]; those backends retain their
/// existing in-process semantics and must not be described as cross-process
/// fenced.
///
/// A backend may also persist a monotonic fence value in the lease record and
/// verify it before a mutation. This is an additional stale-binding check, not
/// by itself a complete crash/fencing proof.
pub trait WriterLease: Send + Sync {
    fn verify_fence(&self) -> Result<(), FileError> {
        Ok(())
    }
}

/// A completed append range bound to the file generation actually written.
/// The reader remains valid if the journal path is subsequently replaced.
#[derive(Clone)]
pub struct AppendReceipt {
    pub reader: Arc<dyn ReadFile>,
    pub offset: u64,
    pub length: usize,
}

/// An optional, single-owner append capability for a journal.
#[async_trait]
pub trait AppendFile: Send + Sync {
    /// Append and finish the write before acknowledging it. This is the same
    /// visibility boundary as `FileSystem::append_file`, not a power-loss fsync.
    async fn append(&mut self, bytes: &[u8]) -> Result<(), FileError>;

    /// Append with an optional stable-generation range. Missing read permission
    /// or receipt support must not turn a successful append into a failure.
    async fn append_with_receipt(
        &mut self,
        bytes: &[u8],
    ) -> Result<Option<AppendReceipt>, FileError> {
        self.append(bytes).await?;
        Ok(None)
    }
}

/// Read capability bound to one opened file, even if its path is replaced.
/// Implementations serialize cursor access and return errors on short ranges.
#[async_trait]
pub trait ReadFile: Send + Sync {
    async fn read_all(&self) -> Result<Vec<u8>, FileError>;
    async fn read_range(&self, offset: u64, length: usize) -> Result<Vec<u8>, FileError>;

    /// Current byte length of this opened generation, when supported. This is
    /// not a snapshot: later appends or truncation may change the length.
    async fn byte_len(&self) -> Result<Option<u64>, FileError> {
        Ok(None)
    }
}

/// Mirrors TS `FileSystem`. All methods async, never panic.
/// Backends should check cancellation at entry and during long operations.
#[async_trait]
pub trait FileSystem: Send + Sync {
    /// The environment's working directory (absolute).
    fn cwd(&self) -> &Path;

    /// Resolve `path` (relative-to-cwd, `~`, `file://`, absolute) to an absolute
    /// addressed path. Pure — no FS access, no abort. Mirrors TS `absolutePath`.
    async fn absolute_path(
        &self,
        path: &str,
        cancel: Option<&CancellationToken>,
    ) -> Result<PathBuf, FileError>;

    /// Join path components. Pure. Mirrors TS `joinPath`.
    async fn join_path(
        &self,
        parts: &[&str],
        cancel: Option<&CancellationToken>,
    ) -> Result<PathBuf, FileError>;

    async fn read_text_file(
        &self,
        path: &str,
        cancel: Option<&CancellationToken>,
    ) -> Result<String, FileError>;

    /// Stream-read up to `max_lines` lines (split on `\n`; `max_lines <= 0`
    /// → empty). Mirrors TS `readTextLines{maxLines}`.
    async fn read_text_lines(
        &self,
        path: &str,
        max_lines: Option<usize>,
        cancel: Option<&CancellationToken>,
    ) -> Result<Vec<String>, FileError>;

    async fn read_binary_file(
        &self,
        path: &str,
        cancel: Option<&CancellationToken>,
    ) -> Result<Vec<u8>, FileError>;

    /// Creates parent directories (recursive) automatically. Mirrors TS
    /// `writeFile`.
    async fn write_file(
        &self,
        path: &str,
        content: FileContent,
        cancel: Option<&CancellationToken>,
    ) -> Result<(), FileError>;

    async fn append_file(
        &self,
        path: &str,
        content: FileContent,
        cancel: Option<&CancellationToken>,
    ) -> Result<(), FileError>;

    /// Optional reusable append handle for a single-owner journal. Backends
    /// without this capability keep the existing per-call append path.
    async fn open_append_file(
        &self,
        _path: &str,
    ) -> Result<Option<Box<dyn AppendFile>>, FileError> {
        Ok(None)
    }

    /// Try to acquire an exclusive writer lease for one addressed journal.
    ///
    /// `Some` is a held capability whose lifetime owns the backend lock;
    /// `None` means this backend has no process-level lease primitive (for
    /// example the in-memory test backend). Contention is an error, not a
    /// successful `None`, so callers fail closed when a real backend refuses
    /// ownership.
    async fn try_acquire_writer_lease(
        &self,
        _path: &str,
    ) -> Result<Option<Box<dyn WriterLease>>, FileError> {
        Ok(None)
    }

    /// Flush a staged file's contents through the backend's durability boundary.
    /// The default is a deliberate no-op for memory/Faux backends; callers must
    /// not treat it as evidence of physical persistence.
    async fn sync_file(
        &self,
        _path: &str,
        _data_only: bool,
        _cancel: Option<&CancellationToken>,
    ) -> Result<(), FileError> {
        Ok(())
    }

    /// Flush the directory metadata needed to make a completed rename durable.
    /// The default is a deliberate no-op for backends without physical files.
    async fn sync_directory(
        &self,
        _path: &str,
        _cancel: Option<&CancellationToken>,
    ) -> Result<(), FileError> {
        Ok(())
    }

    /// Optional stable-generation reader. Unsupported backends retain eager
    /// history loading; an actual open failure must be returned, not hidden.
    async fn open_read_file(
        &self,
        _path: &str,
    ) -> Result<Option<std::sync::Arc<dyn ReadFile>>, FileError> {
        Ok(None)
    }

    /// Cut the file at `path` back to `len` bytes, discarding a tail that a
    /// failed append left behind. It never grows a file. Backends without it
    /// fail, so a caller cannot take a tail for clean when it was not cut.
    async fn truncate_file(
        &self,
        path: &str,
        len: u64,
        cancel: Option<&CancellationToken>,
    ) -> Result<(), FileError> {
        let _ = (len, cancel);
        Err(FileError::new(
            FileErrorCode::NotSupported,
            "file truncation is unavailable",
        )
        .with_path(path))
    }

    async fn rename_file(
        &self,
        source: &str,
        dest: &str,
        cancel: Option<&CancellationToken>,
    ) -> Result<(), FileError>;

    /// Uses `symlink_metadata` (lstat — symlinks not followed). Mirrors TS
    /// `fileInfo`.
    async fn file_info(
        &self,
        path: &str,
        cancel: Option<&CancellationToken>,
    ) -> Result<FileInfo, FileError>;

    async fn list_dir(
        &self,
        path: &str,
        cancel: Option<&CancellationToken>,
    ) -> Result<Vec<FileInfo>, FileError>;

    /// Follows symlinks (`canonicalize`). Mirrors TS `canonicalPath`. Backends
    /// may return `not_found` or `not_supported` for missing/unsupported paths;
    /// callers (mutation queue) fall back to absolute path on those codes.
    async fn canonical_path(
        &self,
        path: &str,
        cancel: Option<&CancellationToken>,
    ) -> Result<PathBuf, FileError>;

    /// `false` ONLY for `not_found`; other errors propagate. Mirrors TS
    /// `exists` (which delegates to `fileInfo`, NOT `access(F_OK)`).
    async fn exists(
        &self,
        path: &str,
        cancel: Option<&CancellationToken>,
    ) -> Result<bool, FileError>;

    /// Default `recursive=true`. Mirrors TS `createDir{recursive}`.
    async fn create_dir(
        &self,
        path: &str,
        recursive: bool,
        cancel: Option<&CancellationToken>,
    ) -> Result<(), FileError>;

    /// Defaults `recursive=false`, `force=false`. Mirrors TS `remove`.
    async fn remove(
        &self,
        path: &str,
        recursive: bool,
        force: bool,
        cancel: Option<&CancellationToken>,
    ) -> Result<(), FileError>;

    /// Default prefix `"tmp-"`. Mirrors TS `createTempDir{prefix}`.
    async fn create_temp_dir(
        &self,
        prefix: Option<&str>,
        cancel: Option<&CancellationToken>,
    ) -> Result<PathBuf, FileError>;

    /// Mirrors TS `createTempFile{prefix,suffix}` (defaults empty). Note: the
    /// TS impl creates an isolated temp **dir** per file; backends may simplify
    /// to a single file in the OS temp dir (document any divergence).
    async fn create_temp_file(
        &self,
        prefix: &str,
        suffix: &str,
        cancel: Option<&CancellationToken>,
    ) -> Result<PathBuf, FileError>;

    /// Best-effort, never returns `Err`. Mirrors TS `cleanup` (kills tracked
    /// child processes, etc.).
    async fn cleanup(&self);
}

/// Mirrors TS `Shell`.
#[async_trait]
pub trait Shell: Send + Sync {
    /// Execute `command` in a bash shell. The TS env resolves a bash binary
    /// (git-bash on Windows, `/bin/bash` on POSIX, `/bin/sh` fallback on POSIX).
    /// There is NO cmd.exe fallback on Windows — failure to find bash is
    /// `ExecutionError::ShellUnavailable`.
    async fn exec<'a>(
        &'a self,
        command: &str,
        options: ShellExecOptions<'a>,
    ) -> Result<ShellOutput, ExecutionError>;

    /// Best-effort, never returns `Err`. Mirrors TS `Shell.cleanup`.
    async fn cleanup(&self);
}

/// Mirrors TS `ExecutionEnv extends FileSystem, Shell`. A marker combining both
/// capabilities — tools receive `Arc<dyn ExecutionEnv>` (or a concrete env) and
/// use `FileSystem` + `Shell` methods interchangeably.
#[async_trait]
pub trait ExecutionEnv: FileSystem + Shell {}

/// Check `cancel` at a method entry point, returning `FileError::aborted()`
/// when set. Mirrors the TS `abortResult<T>(signal, path?)` helper used at the
/// top of every `FileSystem` method.
pub fn check_cancel_file(
    cancel: Option<&CancellationToken>,
    path: Option<&str>,
) -> Result<(), FileError> {
    if let Some(t) = cancel {
        if t.is_cancelled() {
            let mut err = FileError::aborted();
            if let Some(p) = path {
                err = err.with_path(p);
            }
            return Err(err);
        }
    }
    Ok(())
}

/// Same as [`check_cancel_file`] but for `Shell`-side (returns
/// `ExecutionError::aborted()`).
pub fn check_cancel_exec(cancel: Option<&CancellationToken>) -> Result<(), ExecutionError> {
    if let Some(t) = cancel {
        if t.is_cancelled() {
            return Err(ExecutionError::aborted());
        }
    }
    Ok(())
}

#[cfg(test)]
mod capability_tests {
    use super::*;

    // Deliberately implement only the original required methods. These doubles
    // guard source compatibility as well as dispatch through the trait object.
    #[derive(Default)]
    struct LegacyAppender(Vec<u8>);

    #[async_trait]
    impl AppendFile for LegacyAppender {
        async fn append(&mut self, bytes: &[u8]) -> Result<(), FileError> {
            self.0.extend_from_slice(bytes);
            Ok(())
        }
    }

    struct LegacyReader;

    #[async_trait]
    impl ReadFile for LegacyReader {
        async fn read_all(&self) -> Result<Vec<u8>, FileError> {
            Ok(b"legacy".to_vec())
        }

        async fn read_range(&self, _offset: u64, _length: usize) -> Result<Vec<u8>, FileError> {
            Err(FileError::new(FileErrorCode::NotSupported, "legacy reader"))
        }
    }

    #[tokio::test]
    async fn legacy_capabilities_keep_their_default_behavior() {
        let mut writer = LegacyAppender::default();
        let appender: &mut dyn AppendFile = &mut writer;
        assert!(appender
            .append_with_receipt(b"frame\n")
            .await
            .unwrap()
            .is_none());
        assert_eq!(writer.0, b"frame\n");
        let reader: Arc<dyn ReadFile> = Arc::new(LegacyReader);
        assert_eq!(reader.byte_len().await.unwrap(), None);
        assert_eq!(reader.read_all().await.unwrap(), b"legacy");
    }

    #[tokio::test]
    async fn default_receipt_method_preserves_append_errors() {
        struct FailingAppender;
        #[async_trait]
        impl AppendFile for FailingAppender {
            async fn append(&mut self, _bytes: &[u8]) -> Result<(), FileError> {
                Err(FileError::new(FileErrorCode::Unknown, "append failed"))
            }
        }
        assert!(FailingAppender.append_with_receipt(b"frame").await.is_err());
    }
}