vv-agent 0.11.0

VectorVein agent runtime, SDK, CLI, tools, and workspace backends
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use std::fs::File;
use std::io::{Error, ErrorKind, Read};
use std::path::Path;
use std::sync::Arc;

use sha2::{Digest, Sha256};
use uuid::Uuid;

use crate::types::ToolArtifactRef;

use super::WorkspaceBackend;

pub(crate) const BOUNDED_TEXT_CHARS: usize = 12_000;
const PREVIEW_HEAD_CHARS: usize = 6_000;
const PREVIEW_TAIL_CHARS: usize = 5_953;
const PREVIEW_MARKER: &str = "\n... output omitted; full text in artifact ...\n";
const CAPTURE_CHUNK_BYTES: usize = 64 * 1024;

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct BoundedTextPreview {
    pub content: String,
    pub original_bytes: u64,
    pub visible_bytes: u64,
    pub truncated: bool,
}

pub(crate) fn bounded_text_preview(text: &str) -> BoundedTextPreview {
    let original_bytes = text.len() as u64;
    if text.chars().count() <= BOUNDED_TEXT_CHARS {
        return BoundedTextPreview {
            content: text.to_string(),
            original_bytes,
            visible_bytes: original_bytes,
            truncated: false,
        };
    }
    let head = text.chars().take(PREVIEW_HEAD_CHARS).collect::<String>();
    let tail = text
        .chars()
        .rev()
        .take(PREVIEW_TAIL_CHARS)
        .collect::<Vec<_>>()
        .into_iter()
        .rev()
        .collect::<String>();
    let content = format!("{head}{PREVIEW_MARKER}{tail}");
    debug_assert_eq!(content.chars().count(), BOUNDED_TEXT_CHARS);
    BoundedTextPreview {
        visible_bytes: content.len() as u64,
        content,
        original_bytes,
        truncated: true,
    }
}

pub(crate) fn bounded_captured_text_preview(path: &Path) -> std::io::Result<BoundedTextPreview> {
    let mut first = String::new();
    let mut first_chars = 0usize;
    let mut tail = String::new();
    let mut total_chars = 0usize;
    let mut original_bytes = 0u64;

    for chunk in CapturedTextChunks::open(path)? {
        let chunk = chunk?;
        total_chars = total_chars.saturating_add(chunk.chars().count());
        original_bytes = original_bytes.saturating_add(chunk.len() as u64);
        if first_chars < BOUNDED_TEXT_CHARS {
            let prefix = prefix_chars(&chunk, BOUNDED_TEXT_CHARS - first_chars);
            first_chars += prefix.chars().count();
            first.push_str(prefix);
        }
        tail.push_str(&chunk);
        retain_tail_chars(&mut tail, PREVIEW_TAIL_CHARS);
    }

    if total_chars <= BOUNDED_TEXT_CHARS {
        return Ok(BoundedTextPreview {
            visible_bytes: original_bytes,
            content: first,
            original_bytes,
            truncated: false,
        });
    }

    let head = prefix_chars(&first, PREVIEW_HEAD_CHARS);
    let content = format!("{head}{PREVIEW_MARKER}{tail}");
    debug_assert_eq!(content.chars().count(), BOUNDED_TEXT_CHARS);
    Ok(BoundedTextPreview {
        visible_bytes: content.len() as u64,
        content,
        original_bytes,
        truncated: true,
    })
}

pub(crate) fn read_captured_text_prefix(path: &Path, limit_chars: usize) -> String {
    if limit_chars == 0 {
        return String::new();
    }
    let Ok(chunks) = CapturedTextChunks::open(path) else {
        return String::new();
    };
    let mut output = String::new();
    let mut visible_chars = 0usize;
    for chunk in chunks {
        let Ok(chunk) = chunk else {
            return String::new();
        };
        let prefix = prefix_chars(&chunk, limit_chars.saturating_sub(visible_chars));
        visible_chars += prefix.chars().count();
        output.push_str(prefix);
        if visible_chars == limit_chars {
            break;
        }
    }
    output
}

pub(crate) fn persist_captured_text_artifact(
    backend: Arc<dyn WorkspaceBackend>,
    task_id: &str,
    tool_call_id: &str,
    capture_path: &Path,
) -> std::io::Result<ToolArtifactRef> {
    let task = artifact_segment(task_id, "task");
    let call = artifact_segment(tool_call_id, "call");
    let mut last_collision = None;
    for _ in 0..32 {
        let suffix = Uuid::new_v4().simple().to_string();
        let path = format!(".vv-agent/artifacts/{task}/{call}-{suffix}.txt");
        let mut chunks = HashingCapturedTextChunks::open(capture_path)?;
        match backend.write_text_chunks_exclusive(&path, &mut chunks) {
            Ok(written) => {
                let (size_bytes, sha256) = chunks.finish();
                if written != size_bytes as usize {
                    return Err(Error::new(
                        ErrorKind::WriteZero,
                        format!("artifact write reported {written} of {size_bytes} bytes"),
                    ));
                }
                return Ok(ToolArtifactRef {
                    path,
                    media_type: "text/plain".to_string(),
                    encoding: "utf-8".to_string(),
                    size_bytes,
                    sha256,
                });
            }
            Err(error) if error.kind() == ErrorKind::AlreadyExists => {
                last_collision = Some(error);
            }
            Err(error) => return Err(error),
        }
    }
    Err(last_collision.unwrap_or_else(|| {
        Error::new(
            ErrorKind::AlreadyExists,
            "could not allocate an exclusive artifact path",
        )
    }))
}

pub(crate) fn persist_text_artifact(
    backend: Arc<dyn WorkspaceBackend>,
    task_id: &str,
    tool_call_id: &str,
    content: &str,
) -> std::io::Result<ToolArtifactRef> {
    let task = artifact_segment(task_id, "task");
    let call = artifact_segment(tool_call_id, "call");
    let size_bytes = content.len() as u64;
    let sha256 = format!("{:x}", Sha256::digest(content.as_bytes()));
    let mut last_collision = None;
    for _ in 0..32 {
        let suffix = Uuid::new_v4().simple().to_string();
        let path = format!(".vv-agent/artifacts/{task}/{call}-{suffix}.txt");
        match backend.write_text_exclusive(&path, content) {
            Ok(written) => {
                if written != content.len() {
                    return Err(Error::new(
                        ErrorKind::WriteZero,
                        format!(
                            "artifact write reported {written} of {} bytes",
                            content.len()
                        ),
                    ));
                }
                return Ok(ToolArtifactRef {
                    path,
                    media_type: "text/plain".to_string(),
                    encoding: "utf-8".to_string(),
                    size_bytes,
                    sha256,
                });
            }
            Err(error) if error.kind() == ErrorKind::AlreadyExists => {
                last_collision = Some(error);
            }
            Err(error) => return Err(error),
        }
    }
    Err(last_collision.unwrap_or_else(|| {
        Error::new(
            ErrorKind::AlreadyExists,
            "could not allocate an exclusive artifact path",
        )
    }))
}

pub(crate) fn read_validated_text_artifact(
    backend: &dyn WorkspaceBackend,
    artifact: &ToolArtifactRef,
) -> std::io::Result<String> {
    artifact
        .validate()
        .map_err(|error| Error::new(ErrorKind::InvalidInput, error))?;
    let bytes = backend.read_bytes(&artifact.path)?;
    if bytes.len() as u64 != artifact.size_bytes {
        return Err(Error::new(
            ErrorKind::InvalidData,
            "artifact size_bytes does not match stored bytes",
        ));
    }
    let sha256 = format!("{:x}", Sha256::digest(&bytes));
    if sha256 != artifact.sha256 {
        return Err(Error::new(
            ErrorKind::InvalidData,
            "artifact sha256 does not match stored bytes",
        ));
    }
    String::from_utf8(bytes).map_err(|error| Error::new(ErrorKind::InvalidData, error))
}

pub(crate) fn artifact_write_error_code(error: &std::io::Error) -> &'static str {
    if error.kind() == ErrorKind::InvalidInput {
        "artifact_path_invalid"
    } else {
        "artifact_persist_failed"
    }
}

pub(super) fn is_reserved_artifact_path(path: &str) -> bool {
    super::normalize_workspace_path(path).starts_with(".vv-agent/artifacts/")
}

fn artifact_segment(value: &str, fallback: &str) -> String {
    let mut segment = String::with_capacity(64);
    for character in value.chars() {
        if segment.len() >= 64 {
            break;
        }
        if character.is_ascii_alphanumeric() || matches!(character, '.' | '_' | '-') {
            segment.push(character);
        } else if !segment.ends_with('-') {
            segment.push('-');
        }
    }
    while segment
        .chars()
        .next()
        .is_some_and(|character| !character.is_ascii_alphanumeric())
    {
        segment.remove(0);
    }
    if segment.is_empty() {
        fallback.to_string()
    } else {
        segment
    }
}

fn prefix_chars(value: &str, count: usize) -> &str {
    if count == 0 {
        return "";
    }
    value
        .char_indices()
        .nth(count)
        .map(|(index, _)| &value[..index])
        .unwrap_or(value)
}

fn retain_tail_chars(value: &mut String, count: usize) {
    let total = value.chars().count();
    if total <= count {
        return;
    }
    let remove_chars = total - count;
    let byte_index = value
        .char_indices()
        .nth(remove_chars)
        .map(|(index, _)| index)
        .unwrap_or(value.len());
    value.drain(..byte_index);
}

struct CapturedTextChunks {
    file: File,
    pending: Vec<u8>,
    eof: bool,
}

impl CapturedTextChunks {
    fn open(path: &Path) -> std::io::Result<Self> {
        Ok(Self {
            file: File::open(path)?,
            pending: Vec::with_capacity(CAPTURE_CHUNK_BYTES * 2),
            eof: false,
        })
    }

    fn next_decoded_chunk(&mut self) -> Option<String> {
        if self.pending.is_empty() {
            return None;
        }
        match std::str::from_utf8(&self.pending) {
            Ok(text) => {
                let output = text.to_string();
                self.pending.clear();
                Some(output)
            }
            Err(error) if error.valid_up_to() > 0 => {
                let valid = error.valid_up_to();
                let output = String::from_utf8(self.pending.drain(..valid).collect())
                    .expect("valid UTF-8 prefix");
                Some(output)
            }
            Err(error) if error.error_len().is_some() => {
                let invalid_len = error.error_len().expect("checked invalid length");
                self.pending.drain(..invalid_len);
                Some("\u{fffd}".to_string())
            }
            Err(_) => None,
        }
    }
}

impl Iterator for CapturedTextChunks {
    type Item = std::io::Result<String>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if let Some(chunk) = self.next_decoded_chunk() {
                return Some(Ok(chunk));
            }
            if self.eof {
                if self.pending.is_empty() {
                    return None;
                }
                let output = String::from_utf8_lossy(&self.pending).into_owned();
                self.pending.clear();
                return Some(Ok(output));
            }

            let mut buffer = [0u8; CAPTURE_CHUNK_BYTES];
            match self.file.read(&mut buffer) {
                Ok(0) => self.eof = true,
                Ok(read) => self.pending.extend_from_slice(&buffer[..read]),
                Err(error) => return Some(Err(error)),
            }
        }
    }
}

struct HashingCapturedTextChunks {
    inner: CapturedTextChunks,
    digest: Sha256,
    size_bytes: u64,
}

impl HashingCapturedTextChunks {
    fn open(path: &Path) -> std::io::Result<Self> {
        Ok(Self {
            inner: CapturedTextChunks::open(path)?,
            digest: Sha256::new(),
            size_bytes: 0,
        })
    }

    fn finish(self) -> (u64, String) {
        (self.size_bytes, format!("{:x}", self.digest.finalize()))
    }
}

impl Iterator for HashingCapturedTextChunks {
    type Item = std::io::Result<String>;

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next().map(|chunk| {
            chunk.inspect(|chunk| {
                self.size_bytes = self.size_bytes.saturating_add(chunk.len() as u64);
                self.digest.update(chunk.as_bytes());
            })
        })
    }
}

#[cfg(test)]
mod tests {
    use std::any::Any;
    use std::io::ErrorKind;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::{Arc, Mutex};

    use super::*;
    use crate::workspace::{FileInfo, MemoryWorkspaceBackend};

    struct ExclusiveProbeBackend {
        inner: MemoryWorkspaceBackend,
        collisions_remaining: AtomicUsize,
        failure: Option<ErrorKind>,
        attempted_paths: Mutex<Vec<String>>,
    }

    impl ExclusiveProbeBackend {
        fn new(collisions: usize, failure: Option<ErrorKind>) -> Self {
            Self {
                inner: MemoryWorkspaceBackend::default(),
                collisions_remaining: AtomicUsize::new(collisions),
                failure,
                attempted_paths: Mutex::new(Vec::new()),
            }
        }
    }

    impl WorkspaceBackend for ExclusiveProbeBackend {
        fn as_any(&self) -> &dyn Any {
            self
        }

        fn list_files(&self, base: &str, glob: &str) -> std::io::Result<Vec<String>> {
            self.inner.list_files(base, glob)
        }

        fn read_text(&self, path: &str) -> std::io::Result<String> {
            self.inner.read_text(path)
        }

        fn read_bytes(&self, path: &str) -> std::io::Result<Vec<u8>> {
            self.inner.read_bytes(path)
        }

        fn write_text(&self, path: &str, content: &str, append: bool) -> std::io::Result<usize> {
            self.inner.write_text(path, content, append)
        }

        fn write_text_exclusive(&self, path: &str, content: &str) -> std::io::Result<usize> {
            let mut chunks = std::iter::once(Ok(content.to_string()));
            self.write_text_chunks_exclusive(path, &mut chunks)
        }

        fn write_text_chunks_exclusive(
            &self,
            path: &str,
            chunks: &mut dyn Iterator<Item = std::io::Result<String>>,
        ) -> std::io::Result<usize> {
            self.attempted_paths
                .lock()
                .expect("attempted paths")
                .push(path.to_string());
            if let Some(kind) = self.failure {
                return Err(Error::new(kind, "fixture artifact failure"));
            }
            if self
                .collisions_remaining
                .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |remaining| {
                    remaining.checked_sub(1)
                })
                .is_ok()
            {
                return Err(Error::new(ErrorKind::AlreadyExists, "fixture collision"));
            }
            self.inner.write_text_chunks_exclusive(path, chunks)
        }

        fn file_info(&self, path: &str) -> std::io::Result<Option<FileInfo>> {
            self.inner.file_info(path)
        }

        fn exists(&self, path: &str) -> bool {
            self.inner.exists(path)
        }

        fn is_file(&self, path: &str) -> bool {
            self.inner.is_file(path)
        }

        fn mkdir(&self, path: &str) -> std::io::Result<()> {
            self.inner.mkdir(path)
        }
    }

    fn capture_output(content: &str) -> tempfile::NamedTempFile {
        use std::io::Write;

        let mut capture = tempfile::NamedTempFile::new().expect("capture file");
        capture
            .write_all(content.as_bytes())
            .expect("capture output");
        capture
    }

    #[test]
    fn artifact_collision_selects_a_new_exclusive_path() {
        let backend = Arc::new(ExclusiveProbeBackend::new(1, None));
        let capture = capture_output("complete");
        let artifact =
            persist_captured_text_artifact(backend.clone(), "task-7", "call-bash", capture.path())
                .expect("artifact after collision");
        let attempted = backend.attempted_paths.lock().expect("attempted paths");
        assert_eq!(attempted.len(), 2);
        assert_ne!(attempted[0], attempted[1]);
        assert_eq!(artifact.path, attempted[1]);
        assert_eq!(backend.read_text(&artifact.path).unwrap(), "complete");
    }

    #[test]
    fn artifact_write_failure_is_not_reported_as_success() {
        let backend = Arc::new(ExclusiveProbeBackend::new(
            0,
            Some(ErrorKind::PermissionDenied),
        ));
        let capture = capture_output("complete");
        let error = persist_captured_text_artifact(backend, "task-7", "call-bash", capture.path())
            .expect_err("artifact persistence must fail");
        assert_eq!(error.kind(), ErrorKind::PermissionDenied);
        assert_eq!(artifact_write_error_code(&error), "artifact_persist_failed");
    }

    #[test]
    fn local_artifact_cannot_be_overwritten_through_a_normalized_alias() {
        let workspace = tempfile::tempdir().expect("workspace");
        let backend = Arc::new(crate::workspace::LocalWorkspaceBackend::new(
            workspace.path(),
        ));
        let capture = capture_output("complete output");
        let artifact =
            persist_captured_text_artifact(backend.clone(), "task-7", "call-bash", capture.path())
                .expect("artifact");
        let alias = artifact.path.replacen(
            ".vv-agent/artifacts/",
            ".vv-agent/transient/../artifacts/",
            1,
        );

        let error = backend
            .write_text(&alias, "overwritten", false)
            .expect_err("normalized alias must remain immutable");
        assert_eq!(error.kind(), ErrorKind::PermissionDenied);
        assert_eq!(
            backend
                .read_text(&artifact.path)
                .expect("artifact contents"),
            "complete output"
        );
    }

    #[cfg(unix)]
    #[test]
    fn local_artifact_storage_isolated_from_workspace_symlinks() {
        use std::os::unix::fs::symlink;

        let workspace = tempfile::tempdir().expect("workspace");
        let outside = tempfile::tempdir().expect("outside");
        std::fs::create_dir_all(workspace.path().join(".vv-agent/artifacts"))
            .expect("artifact root");
        symlink(
            outside.path(),
            workspace.path().join(".vv-agent/artifacts/task-7"),
        )
        .expect("task symlink");
        let backend = Arc::new(crate::workspace::LocalWorkspaceBackend::new(
            workspace.path(),
        ));

        let capture = capture_output("complete");
        let artifact =
            persist_captured_text_artifact(backend.clone(), "task-7", "call-bash", capture.path())
                .expect("private artifact write");
        assert_eq!(
            backend.read_text(&artifact.path).expect("private artifact"),
            "complete"
        );
        assert!(std::fs::read_dir(outside.path())
            .expect("outside directory")
            .next()
            .is_none());
    }

    #[test]
    fn capture_decoder_preserves_a_split_utf8_scalar_and_replaces_invalid_bytes() {
        use std::io::Write;

        let mut capture = tempfile::NamedTempFile::new().expect("capture file");
        capture
            .write_all(&[b'a'; CAPTURE_CHUNK_BYTES - 1])
            .expect("prefix");
        capture.write_all("".as_bytes()).expect("scalar");
        capture.write_all(b"\xffdone").expect("invalid suffix");

        let output = read_captured_text_prefix(capture.path(), CAPTURE_CHUNK_BYTES + 8);
        assert!(output.ends_with("\u{fffd}done"));
    }
}