rpi-tools 0.30.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
508
509
510
511
512
//! Reusable append handles must follow the file currently named by the path.

use std::fs::{File, OpenOptions};
use std::io::{self, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use async_trait::async_trait;

use super::read::OsReadFile;
use crate::env::{AppendFile, AppendReceipt, NativeAppendMetadata, ReadFile};
use crate::error::{io_to_file_error, FileError, FileErrorCode};

#[cfg(unix)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct FileIdentity {
    volume: u64,
    index: u128,
}

#[cfg(windows)]
#[derive(Clone, Debug, Eq, PartialEq)]
struct FileIdentity(Arc<same_file::Handle>);

#[cfg(not(any(unix, windows)))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct FileIdentity;

pub(super) struct OsAppendFile {
    path: PathBuf,
    state: Option<AppendState>,
    buffer: Vec<u8>,
    pending: Option<tokio::task::JoinHandle<AppendOutcome>>,
}

struct AppendState {
    file: Arc<Mutex<File>>,
    reader: Option<Arc<dyn ReadFile>>,
    identity: Option<FileIdentity>,
}

type Receipt = (Option<AppendReceipt>, Option<NativeAppendMetadata>);
type AppendOutcome = (Option<AppendState>, Vec<u8>, io::Result<Receipt>);

fn open_current(path: &Path) -> io::Result<AppendState> {
    if let Some(parent) = path.parent() {
        // Match append_file: the actual open decides whether the path is
        // writable, even if the caller cannot inspect an existing parent.
        let _ = std::fs::create_dir_all(parent);
    }
    // Read access is optional. Never require it for the legacy append path,
    // and never open a separate reader that might refer to a replaced path.
    let (file, readable) = match OpenOptions::new()
        .create(true)
        .append(true)
        .read(true)
        .open(path)
    {
        Ok(file) => (file, true),
        Err(_) => (
            OpenOptions::new().create(true).append(true).open(path)?,
            false,
        ),
    };
    // Some filesystems cannot provide stable IDs. Reopen on every append there,
    // preserving write-only access instead of requiring metadata permissions.
    let identity = identity_from_file(&file).ok();
    let file = Arc::new(Mutex::new(file));
    let reader = readable.then(|| {
        Arc::new(OsReadFile::from_shared(file.clone(), path.to_path_buf())) as Arc<dyn ReadFile>
    });
    Ok(AppendState {
        file,
        reader,
        identity,
    })
}

impl OsAppendFile {
    pub(super) async fn open(path: PathBuf) -> Result<Self, FileError> {
        let worker_path = path.clone();
        let state = tokio::task::spawn_blocking(move || open_current(&worker_path))
            .await
            .map_err(join_error)?
            .map_err(|error| io_to_file_error(error, Some(&path.to_string_lossy())))?;
        Ok(Self {
            path,
            state: Some(state),
            buffer: Vec::new(),
            pending: None,
        })
    }

    async fn finish_pending(&mut self) -> Result<Receipt, FileError> {
        let Some(pending) = self.pending.as_mut() else {
            return Ok((None, None));
        };
        // Await by reference: cancellation must leave the task owned by this
        // writer. The next append joins it before starting another write.
        let outcome = pending.await;
        self.pending = None;
        let (state, mut buffer, result) = outcome.map_err(join_error)?;
        buffer.clear();
        // Do not retain a large image/tool payload for the lifetime of a session.
        self.buffer = if buffer.capacity() <= 64 * 1024 {
            buffer
        } else {
            Vec::new()
        };
        self.state = if result.is_ok() { state } else { None };
        result.map_err(|error| io_to_file_error(error, Some(&self.path.to_string_lossy())))
    }

    async fn append_impl(
        &mut self,
        bytes: &[u8],
        track_native: bool,
    ) -> Result<Receipt, FileError> {
        // A cancelled caller's receipt is discarded, but its write must finish
        // (and any real error propagate) before a subsequent append can start.
        self.finish_pending().await?;
        let path = self.path.clone();
        let mut state = self.state.take();
        let mut buffer = std::mem::take(&mut self.buffer);
        buffer.extend_from_slice(bytes);
        self.pending = Some(tokio::task::spawn_blocking(move || {
            // Metadata, write and visibility flush share one blocking dispatch.
            // Splitting them into separate async fs operations wakes workers
            // repeatedly for every recovery frame.
            let result = (|| {
                let current = state.as_ref().and_then(|state| state.identity.clone());
                if current.is_none() || identity_at_path(&path).ok() != current {
                    state = Some(open_current(&path)?);
                }
                let state = state.as_ref().expect("current append file was opened");
                let mut file = state
                    .file
                    .lock()
                    .map_err(|_| io::Error::other("append mutex poisoned"))?;
                let before = if track_native {
                    file.metadata().ok()
                } else {
                    None
                };
                file.write_all(&buffer)?;
                file.flush()?;
                let metadata = before.and_then(|before| {
                    Some(NativeAppendMetadata {
                        before,
                        after: file.metadata().ok()?,
                    })
                });
                // Locate the range using the written handle, never its path.
                // Seeking to the end also handles empty appends and platforms
                // whose append writes do not advance the readable cursor.
                // Under the single-owner journal contract there is no other
                // writer interleaving the write_all calls. Receipt discovery
                // is best effort AFTER commit; a seek failure is not a failed
                // append and must never invite a duplicate retry.
                let receipt = state.reader.as_ref().and_then(|reader| {
                    let length = u64::try_from(buffer.len()).ok()?;
                    let offset = file.seek(SeekFrom::End(0)).ok()?.checked_sub(length)?;
                    Some(AppendReceipt {
                        reader: reader.clone(),
                        offset,
                        length: buffer.len(),
                    })
                });
                Ok((receipt, metadata))
            })();
            (state, buffer, result)
        }));
        self.finish_pending().await
    }
}

fn join_error(error: tokio::task::JoinError) -> FileError {
    FileError::new(FileErrorCode::Unknown, error.to_string()).with_source(Box::new(error))
}

#[async_trait]
impl AppendFile for OsAppendFile {
    async fn append(&mut self, bytes: &[u8]) -> Result<(), FileError> {
        self.append_with_receipt(bytes).await.map(|_| ())
    }

    async fn append_with_receipt(
        &mut self,
        bytes: &[u8],
    ) -> Result<Option<AppendReceipt>, FileError> {
        self.append_impl(bytes, false)
            .await
            .map(|(receipt, _)| receipt)
    }

    async fn append_with_native_metadata(&mut self, bytes: &[u8]) -> Result<Receipt, FileError> {
        self.append_impl(bytes, true).await
    }
}

#[cfg(unix)]
fn identity_from_file(file: &File) -> io::Result<FileIdentity> {
    use std::os::unix::fs::MetadataExt;
    let metadata = file.metadata()?;
    Ok(FileIdentity {
        volume: metadata.dev(),
        index: metadata.ino().into(),
    })
}

#[cfg(unix)]
fn identity_at_path(path: &Path) -> io::Result<FileIdentity> {
    use std::os::unix::fs::MetadataExt;
    let metadata = std::fs::metadata(path)?;
    Ok(FileIdentity {
        volume: metadata.dev(),
        index: metadata.ino().into(),
    })
}

// Keep identity handles open while comparing them: Windows file IDs can be
// reused once handles close. The locked same-file crate exposes this safely.
#[cfg(windows)]
fn identity_from_file(file: &File) -> io::Result<FileIdentity> {
    same_file::Handle::from_file(file.try_clone()?).map(|handle| FileIdentity(Arc::new(handle)))
}

#[cfg(windows)]
fn identity_at_path(path: &Path) -> io::Result<FileIdentity> {
    use std::os::windows::fs::OpenOptionsExt;

    // File identity queries need metadata access only. Opening another data
    // reader after every write can force expensive Windows cache/oplock work.
    // access_mode(0) overrides GENERIC_READ, but still allows handle metadata
    // queries. Keep this handle alive through the same-file comparison so IDs
    // cannot be reused; never substitute pathname/stat heuristics for identity.
    let file = OpenOptions::new().read(true).access_mode(0).open(path)?;
    same_file::Handle::from_file(file).map(|handle| FileIdentity(Arc::new(handle)))
}

#[cfg(not(any(unix, windows)))]
fn identity_from_file(_file: &File) -> io::Result<FileIdentity> {
    Err(io::ErrorKind::Unsupported.into())
}

#[cfg(not(any(unix, windows)))]
fn identity_at_path(_path: &Path) -> io::Result<FileIdentity> {
    Err(io::ErrorKind::Unsupported.into())
}

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

    #[cfg(windows)]
    #[test]
    fn metadata_identity_matches_open_file_without_reading_contents() {
        use std::io::Read;
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("journal");
        std::fs::write(&path, b"synthetic contents").unwrap();
        let file = OpenOptions::new()
            .read(true)
            .append(true)
            .open(&path)
            .unwrap();
        let original = identity_from_file(&file).unwrap();
        let metadata = identity_at_path(&path).unwrap();
        assert_eq!(metadata, original);
        let mut metadata_file = metadata.0.as_file().try_clone().unwrap();
        assert_eq!(metadata_file.metadata().unwrap().len(), 18);
        assert!(
            metadata_file.read(&mut [0; 1]).is_err(),
            "identity handle must not read payloads"
        );
        let replacement = directory.path().join("replacement");
        std::fs::write(&replacement, b"different generation").unwrap();
        std::fs::rename(&replacement, &path).unwrap();
        assert_ne!(identity_at_path(&path).unwrap(), original);
        assert_eq!(
            metadata, original,
            "retired handles keep their original identity"
        );
    }

    #[tokio::test]
    async fn unknown_identity_reopens_after_external_replacement() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("session.jsonl");
        let mut writer = OsAppendFile::open(path.clone()).await.unwrap();
        writer.append(b"before\n").await.unwrap();
        writer.state.as_mut().unwrap().identity = None;
        let replacement = directory.path().join("replacement");
        std::fs::write(&replacement, b"copied\n").unwrap();
        std::fs::rename(&replacement, &path).unwrap();
        writer.append(b"after\n").await.unwrap();
        assert_eq!(std::fs::read(path).unwrap(), b"copied\nafter\n");
    }

    #[tokio::test]
    async fn missing_path_is_recreated_instead_of_writing_to_removed_file() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("session.jsonl");
        let backup = directory.path().join("backup");
        let mut writer = OsAppendFile::open(path.clone()).await.unwrap();
        writer.append(b"before\n").await.unwrap();
        std::fs::rename(&path, &backup).unwrap();
        writer.append(b"after\n").await.unwrap();
        assert_eq!(std::fs::read(backup).unwrap(), b"before\n");
        assert_eq!(std::fs::read(path).unwrap(), b"after\n");
    }

    #[tokio::test]
    async fn failed_reopen_does_not_acknowledge_a_write_to_the_old_file() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("session.jsonl");
        let backup = directory.path().join("backup");
        let mut writer = OsAppendFile::open(path.clone()).await.unwrap();
        writer.append(b"before\n").await.unwrap();
        std::fs::rename(&path, &backup).unwrap();
        std::fs::create_dir(&path).unwrap();
        assert!(writer.append_with_receipt(b"after\n").await.is_err());
        assert_eq!(std::fs::read(backup).unwrap(), b"before\n");
    }

    #[tokio::test]
    async fn cancelled_append_wait_keeps_the_pending_writer_ordered() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("session.jsonl");
        let mut writer = OsAppendFile::open(path.clone()).await.unwrap();
        let state = writer.state.take().unwrap();
        let (release, wait) = tokio::sync::oneshot::channel();
        writer.pending = Some(tokio::spawn(async move {
            wait.await.unwrap();
            let result = state
                .file
                .lock()
                .unwrap()
                .write_all(b"pending\n")
                .map(|_| (None, None));
            (Some(state), Vec::new(), result)
        }));
        assert!(tokio::time::timeout(
            std::time::Duration::from_millis(10),
            writer.append(b"cancelled\n")
        )
        .await
        .is_err());
        assert!(writer.pending.is_some());
        release.send(()).unwrap();
        let receipt = writer
            .append_with_receipt(b"next\n")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(receipt.offset, 8);
        assert_eq!(receipt.length, 5);
        assert_eq!(
            receipt
                .reader
                .read_range(receipt.offset, receipt.length)
                .await
                .unwrap(),
            b"next\n"
        );
        assert_eq!(std::fs::read(path).unwrap(), b"pending\nnext\n");
    }

    #[test]
    fn cancelled_receipt_wait_preserves_its_already_queued_append() {
        // One blocking worker makes cancellation deterministic without holding
        // a file lock across an await or relying on platform file identities.
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .max_blocking_threads(1)
            .build()
            .unwrap();
        runtime.block_on(async {
            let directory = tempfile::tempdir().unwrap();
            let path = directory.path().join("journal");
            let mut writer = OsAppendFile::open(path.clone()).await.unwrap();
            let (started, ready) = tokio::sync::oneshot::channel();
            let (release, wait) = std::sync::mpsc::channel();
            let blocker = tokio::task::spawn_blocking(move || {
                let _ = started.send(());
                let _ = wait.recv();
            });
            ready.await.unwrap();
            let cancelled = tokio::time::timeout(
                std::time::Duration::from_millis(10),
                writer.append_with_receipt(b"pending\n"),
            )
            .await
            .is_err();
            release.send(()).unwrap();
            blocker.await.unwrap();
            assert!(cancelled);
            assert!(writer.pending.is_some());
            let receipt = writer
                .append_with_receipt(b"next\n")
                .await
                .unwrap()
                .unwrap();
            assert_eq!((receipt.offset, receipt.length), (8, 5));
            assert_eq!(receipt.reader.read_range(8, 5).await.unwrap(), b"next\n");
            assert_eq!(std::fs::read(path).unwrap(), b"pending\nnext\n");
        });
    }

    #[tokio::test]
    async fn receipts_identify_written_ranges_and_share_the_opened_reader() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("journal");
        std::fs::write(&path, b"seed\n").unwrap();
        let mut writer = OsAppendFile::open(path.clone()).await.unwrap();
        let first = writer.append_with_receipt(b"one\n").await.unwrap().unwrap();
        let second = writer.append_with_receipt(b"two\n").await.unwrap().unwrap();
        assert_eq!((first.offset, first.length), (5, 4));
        assert_eq!((second.offset, second.length), (9, 4));
        // Mainstream OS backends share a reader across the whole generation;
        // otherwise every persisted cold entry would pin another file handle.
        #[cfg(any(unix, windows))]
        assert!(Arc::ptr_eq(&first.reader, &second.reader));
        let cloned = first.clone();
        assert!(Arc::ptr_eq(&cloned.reader, &first.reader));
        assert_eq!(cloned.reader.read_range(5, 4).await.unwrap(), b"one\n");
        assert_eq!(second.reader.read_range(9, 4).await.unwrap(), b"two\n");
        // A read moves the shared cursor; empty append still reports EOF.
        let empty = writer.append_with_receipt(b"").await.unwrap().unwrap();
        assert_eq!((empty.offset, empty.length), (13, 0));
        assert!(empty.reader.read_range(13, 0).await.unwrap().is_empty());
        let (read, appended) = tokio::join!(
            first.reader.read_range(first.offset, first.length),
            writer.append_with_receipt(b"three\n")
        );
        assert_eq!(read.unwrap(), b"one\n");
        let third = appended.unwrap().unwrap();
        assert_eq!((third.offset, third.length), (13, 6));
        assert_eq!(third.reader.byte_len().await.unwrap(), Some(19));
        assert_eq!(std::fs::read(path).unwrap(), b"seed\none\ntwo\nthree\n");
        assert!(third.reader.read_range(18, 2).await.is_err());
    }

    #[tokio::test]
    async fn receipts_survive_atomic_replacement_and_new_receipts_follow_the_path() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("journal");
        std::fs::write(&path, b"base\n").unwrap();
        let mut writer = OsAppendFile::open(path.clone()).await.unwrap();
        let old = writer.append_with_receipt(b"old\n").await.unwrap().unwrap();
        let replacement = directory.path().join("replacement");
        std::fs::write(&replacement, b"replacement\n").unwrap();
        std::fs::rename(&replacement, &path).unwrap();
        let new = writer.append_with_receipt(b"new\n").await.unwrap().unwrap();
        assert!(!Arc::ptr_eq(&old.reader, &new.reader));
        assert_eq!(old.reader.byte_len().await.unwrap(), Some(9));
        assert_eq!(new.reader.byte_len().await.unwrap(), Some(16));
        assert_eq!((old.offset, old.length), (5, 4));
        assert_eq!((new.offset, new.length), (12, 4));
        drop(writer);
        assert_eq!(old.reader.read_all().await.unwrap(), b"base\nold\n");
        assert_eq!(
            old.reader.read_range(old.offset, old.length).await.unwrap(),
            b"old\n"
        );
        assert_eq!(
            new.reader.read_range(new.offset, new.length).await.unwrap(),
            b"new\n"
        );
        assert_eq!(new.reader.read_all().await.unwrap(), b"replacement\nnew\n");
    }

    #[tokio::test]
    async fn large_append_does_not_retain_its_buffer() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("journal");
        let mut writer = OsAppendFile::open(path).await.unwrap();
        let bytes = vec![b'x'; 64 * 1024 + 1];
        writer.append_with_receipt(&bytes).await.unwrap();
        assert_eq!(writer.buffer.capacity(), 0);
        let receipt = writer.append_with_receipt(b"tail").await.unwrap().unwrap();
        assert_eq!(receipt.offset, bytes.len() as u64);
        assert!(writer.buffer.capacity() <= 64 * 1024);
        assert!(writer.buffer.is_empty());
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn write_only_permissions_do_not_require_receipts() {
        use std::os::unix::fs::PermissionsExt;
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("journal");
        std::fs::write(&path, b"seed\n").unwrap();
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o200)).unwrap();
        // Privileged test users can bypass mode bits; no permission boundary
        // can be exercised in that environment.
        if OpenOptions::new().read(true).open(&path).is_ok() {
            return;
        }
        let mut writer = OsAppendFile::open(path.clone()).await.unwrap();
        assert!(writer.state.as_ref().unwrap().reader.is_none());
        assert!(writer
            .append_with_receipt(b"frame\n")
            .await
            .unwrap()
            .is_none());
        writer.append(b"legacy\n").await.unwrap();
        drop(writer);
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
        assert_eq!(std::fs::read(path).unwrap(), b"seed\nframe\nlegacy\n");
    }
}