blyte-agent 0.88.1

The blyte agent loop: tools, context, persistent shell sessions, search and the command sandbox.
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
//! An [`Fs`] wrapper that saves the original bytes of every overwritten file before writing.

use std::collections::{BTreeMap, HashSet};
use std::io::{ErrorKind, Write};
use std::path::{Component, Path, PathBuf};
use std::process::Command;
use std::sync::Mutex;

use serde::{Deserialize, Serialize};

use crate::fs::Fs;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Change {
    pub path: String,
    pub existed: bool,
}

pub struct SnapshotFs<F: Fs> {
    inner: F,
    root: PathBuf,
    before_dir: PathBuf,
    manifest: PathBuf,
    recorded: Mutex<HashSet<String>>,
}

impl<F: Fs> SnapshotFs<F> {
    pub fn new(
        inner: F,
        root: PathBuf,
        before_dir: PathBuf,
        manifest: PathBuf,
    ) -> Result<Self, String> {
        let root = std::fs::canonicalize(&root)
            .map_err(|e| format!("cannot canonicalize {}: {e}", root.display()))?;
        std::fs::create_dir_all(&before_dir)
            .map_err(|e| format!("cannot create {}: {e}", before_dir.display()))?;
        let existing = read_manifest(&manifest)?;
        let recorded = existing.into_iter().map(|c| c.path).collect();
        Ok(Self {
            inner,
            root,
            before_dir,
            manifest,
            recorded: Mutex::new(recorded),
        })
    }

    fn resolve_inside_root(&self, path: &Path) -> Result<(PathBuf, PathBuf, String), String> {
        let abs = if path.is_absolute() {
            path.to_path_buf()
        } else {
            std::env::current_dir()
                .map_err(|e| format!("cannot resolve {}: {e}", path.display()))?
                .join(path)
        };
        let resolved = resolve_with_existing_ancestors(&abs, path)?;
        let rel = resolved.strip_prefix(&self.root).map_err(|_| {
            format!(
                "path {} resolves to {}, outside root {}",
                path.display(),
                resolved.display(),
                self.root.display()
            )
        })?;
        let mut parts = Vec::new();
        for comp in rel.components() {
            match comp {
                Component::Normal(s) => {
                    let part = s
                        .to_str()
                        .ok_or_else(|| format!("non-UTF-8 path: {}", path.display()))?;
                    parts.push(part);
                }
                _ => {
                    return Err(format!(
                        "path {} resolves outside root {}",
                        path.display(),
                        self.root.display()
                    ));
                }
            }
        }
        if parts.is_empty() {
            return Err(format!(
                "path {} refers to root directory {}",
                path.display(),
                self.root.display()
            ));
        }
        let rel_path = rel.to_path_buf();
        let rel_key = parts.join("/");
        Ok((resolved, rel_path, rel_key))
    }

    fn snapshot_before_first_write(
        &self,
        original_path: &Path,
        resolved: &Path,
        rel_path: &Path,
        rel_key: &str,
    ) -> Result<(), String> {
        let mut recorded = self.recorded.lock().map_err(|e| {
            format!(
                "cannot snapshot {}: snapshot state lock poisoned: {e}",
                original_path.display()
            )
        })?;
        if recorded.contains(rel_key) {
            return Ok(());
        }
        let existed = match std::fs::read(resolved) {
            Ok(bytes) => {
                let dest = self.before_dir.join(rel_path);
                if let Some(parent) = dest.parent() {
                    std::fs::create_dir_all(parent).map_err(|e| {
                        format!(
                            "cannot save original of {} in {}: {e}",
                            original_path.display(),
                            parent.display()
                        )
                    })?;
                }
                std::fs::write(&dest, &bytes).map_err(|e| {
                    format!(
                        "cannot save original of {} to {}: {e}",
                        original_path.display(),
                        dest.display()
                    )
                })?;
                true
            }
            Err(e) if e.kind() == ErrorKind::NotFound => false,
            Err(e) => {
                return Err(format!(
                    "cannot read original of {}: {e}",
                    original_path.display()
                ));
            }
        };
        let entry = Change {
            path: rel_key.to_string(),
            existed,
        };
        let line = serde_json::to_string(&entry).map_err(|e| {
            format!(
                "cannot serialize manifest entry for {}: {e}",
                original_path.display()
            )
        })?;
        if let Some(parent) = self.manifest.parent() {
            if !parent.as_os_str().is_empty() {
                std::fs::create_dir_all(parent).map_err(|e| {
                    format!(
                        "cannot record {} in {}: {e}",
                        original_path.display(),
                        self.manifest.display()
                    )
                })?;
            }
        }
        let mut file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&self.manifest)
            .map_err(|e| {
                format!(
                    "cannot record {} in {}: {e}",
                    original_path.display(),
                    self.manifest.display()
                )
            })?;
        writeln!(file, "{line}").map_err(|e| {
            format!(
                "cannot record {} in {}: {e}",
                original_path.display(),
                self.manifest.display()
            )
        })?;
        file.flush().map_err(|e| {
            format!(
                "cannot record {} in {}: {e}",
                original_path.display(),
                self.manifest.display()
            )
        })?;
        recorded.insert(rel_key.to_string());
        Ok(())
    }
}

impl<F: Fs> Fs for SnapshotFs<F> {
    fn is_local(&self) -> bool {
        self.inner.is_local()
    }

    fn read(&self, path: &Path) -> Result<String, String> {
        self.inner.read(path)
    }

    fn write(&self, path: &Path, content: &str) -> Result<(), String> {
        let (resolved, rel_path, rel_key) = self.resolve_inside_root(path)?;
        self.snapshot_before_first_write(path, &resolved, &rel_path, &rel_key)?;
        self.inner.write(path, content)
    }

    fn canonicalize(&self, path: &Path) -> Result<PathBuf, String> {
        self.inner.canonicalize(path)
    }
}

fn resolve_with_existing_ancestors(abs: &Path, display_path: &Path) -> Result<PathBuf, String> {
    let mut cur = PathBuf::new();
    let mut existing = true;
    for comp in abs.components() {
        match comp {
            Component::Prefix(p) => cur.push(p.as_os_str()),
            Component::RootDir => cur.push(comp.as_os_str()),
            Component::CurDir => {}
            Component::ParentDir => {
                cur.pop();
                if !existing {
                    if let Ok(canon) = std::fs::canonicalize(&cur) {
                        cur = canon;
                        existing = true;
                    }
                }
            }
            Component::Normal(part) => {
                cur.push(part);
                if existing {
                    match std::fs::canonicalize(&cur) {
                        Ok(canon) => cur = canon,
                        Err(e) => match std::fs::symlink_metadata(&cur) {
                            Err(meta_err) if meta_err.kind() == ErrorKind::NotFound => {
                                existing = false;
                            }
                            Ok(_) => {
                                return Err(format!(
                                    "cannot resolve {}: {e}",
                                    display_path.display()
                                ));
                            }
                            Err(meta_err) => {
                                return Err(format!(
                                    "cannot resolve {}: {meta_err}",
                                    display_path.display()
                                ));
                            }
                        },
                    }
                }
            }
        }
    }
    Ok(cur)
}

pub fn read_manifest(manifest: &Path) -> Result<Vec<Change>, String> {
    let text = match std::fs::read_to_string(manifest) {
        Ok(t) => t,
        Err(e) if e.kind() == ErrorKind::NotFound => return Ok(Vec::new()),
        Err(e) => return Err(format!("cannot read manifest {}: {e}", manifest.display())),
    };
    let mut changes = Vec::new();
    for (idx, line) in text.lines().enumerate() {
        let line_no = idx + 1;
        let change: Change = serde_json::from_str(line).map_err(|e| {
            format!(
                "cannot parse manifest {} line {line_no}: {e}",
                manifest.display()
            )
        })?;
        changes.push(change);
    }
    Ok(changes)
}

struct ChangedEntry {
    path: String,
    before: Option<Vec<u8>>,
    after: Option<Vec<u8>>,
}

fn read_optional_bytes(path: &Path) -> Result<Option<Vec<u8>>, String> {
    match std::fs::read(path) {
        Ok(b) => Ok(Some(b)),
        Err(e) if e.kind() == ErrorKind::NotFound => Ok(None),
        Err(e) => Err(format!("cannot read {}: {e}", path.display())),
    }
}

fn collect_changed(
    root: &Path,
    before_dir: &Path,
    manifest: &Path,
) -> Result<Vec<ChangedEntry>, String> {
    let manifest_entries = read_manifest(manifest)?;
    let mut unique: BTreeMap<String, bool> = BTreeMap::new();
    for change in manifest_entries {
        unique.entry(change.path).or_insert(change.existed);
    }
    let mut out = Vec::new();
    for (rel_path, existed) in unique {
        let current_path = root.join(&rel_path);
        let after = read_optional_bytes(&current_path)?;
        if existed {
            let before_path = before_dir.join(&rel_path);
            let before = std::fs::read(&before_path).map_err(|e| {
                format!(
                    "missing saved original for {rel_path} at {}: {e}",
                    before_path.display()
                )
            })?;
            let changed = match &after {
                Some(cur) => cur != &before,
                None => true,
            };
            if changed {
                out.push(ChangedEntry {
                    path: rel_path,
                    before: Some(before),
                    after,
                });
            }
        } else if after.is_some() {
            out.push(ChangedEntry {
                path: rel_path,
                before: None,
                after,
            });
        }
    }
    Ok(out)
}

pub fn changed_files(
    root: &Path,
    before_dir: &Path,
    manifest: &Path,
) -> Result<Vec<String>, String> {
    let entries = collect_changed(root, before_dir, manifest)?;
    Ok(entries.into_iter().map(|e| e.path).collect())
}

pub fn unified_diff(root: &Path, before_dir: &Path, manifest: &Path) -> Result<String, String> {
    let entries = collect_changed(root, before_dir, manifest)?;
    let mut out = String::new();
    for entry in entries {
        let before_utf8 = entry
            .before
            .as_deref()
            .is_none_or(|b| std::str::from_utf8(b).is_ok());
        let after_utf8 = entry
            .after
            .as_deref()
            .is_none_or(|b| std::str::from_utf8(b).is_ok());
        if !before_utf8 || !after_utf8 {
            out.push_str(&format!(
                "Binary files a/{} and b/{} differ\n",
                entry.path, entry.path
            ));
            continue;
        }
        let (old_label, old_path) = if entry.before.is_some() {
            (format!("a/{}", entry.path), before_dir.join(&entry.path))
        } else {
            ("/dev/null".to_string(), PathBuf::from("/dev/null"))
        };
        let (new_label, new_path) = if entry.after.is_some() {
            (format!("b/{}", entry.path), root.join(&entry.path))
        } else {
            ("/dev/null".to_string(), PathBuf::from("/dev/null"))
        };
        let output = Command::new("diff")
            .arg("-u")
            .arg("--label")
            .arg(&old_label)
            .arg("--label")
            .arg(&new_label)
            .arg("--")
            .arg(&old_path)
            .arg(&new_path)
            .output()
            .map_err(|e| format!("cannot run diff for {}: {e}", entry.path))?;
        match output.status.code() {
            Some(0 | 1) => {
                let chunk = String::from_utf8(output.stdout).map_err(|e| {
                    format!("diff output for {} is not valid UTF-8: {e}", entry.path)
                })?;
                out.push_str(&chunk);
                if !chunk.is_empty() && !chunk.ends_with('\n') {
                    out.push('\n');
                }
            }
            code => {
                let stderr = String::from_utf8_lossy(&output.stderr);
                return Err(format!(
                    "diff failed for {} (exit {:?}): {}",
                    entry.path,
                    code,
                    stderr.trim()
                ));
            }
        }
    }
    Ok(out)
}

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

    #[test]
    fn a_poisoned_recorded_mutex_refuses_writes_and_leaves_the_file_untouched() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("folder");
        std::fs::create_dir_all(&root).unwrap();
        let before_dir = tmp.path().join("before");
        let manifest = tmp.path().join("manifest.jsonl");
        let target = root.join("notes.txt");
        std::fs::write(&target, "original\n").unwrap();

        let sfs = SnapshotFs::new(LocalFs, root, before_dir.clone(), manifest.clone()).unwrap();
        let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let _guard = sfs.recorded.lock().unwrap();
            panic!("poison snapshot lock");
        }));

        let err = sfs.write(&target, "updated\n").unwrap_err();
        assert!(err.contains("notes.txt"), "{err}");
        assert!(err.contains("poison"), "{err}");
        assert_eq!(std::fs::read_to_string(&target).unwrap(), "original\n");
        assert!(!before_dir.join("notes.txt").exists());
        assert!(read_manifest(&manifest).unwrap().is_empty());
    }
}