watasu 0.1.66

Rust SDK for Watasu
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
use serde_json::Value;

use crate::error::{Error, Result};
use crate::process_socket::ProcessSocket;
use crate::transport::DataPlaneClient;
use base64::Engine;

/// File type returned by sandbox filesystem metadata.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FileType {
    /// Regular file.
    File,
    /// Directory.
    Dir,
    /// Symbolic link.
    Symlink,
    /// Any file type not recognized by this SDK version.
    Other(String),
}

/// Metadata for one sandbox filesystem entry.
#[derive(Clone, Debug, Default)]
pub struct EntryInfo {
    /// Basename of the entry.
    pub name: String,
    /// Absolute sandbox path.
    pub path: String,
    /// File type, when returned by the runtime.
    pub file_type: Option<FileType>,
    /// Size in bytes, when known.
    pub size: Option<u64>,
    /// Runtime metadata map.
    pub metadata: serde_json::Map<String, Value>,
}

/// Metadata returned by write operations.
pub type WriteInfo = EntryInfo;

/// File path and bytes used by `Filesystem::write_files`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WriteEntry {
    /// Absolute sandbox path to write.
    pub path: String,
    /// File content bytes.
    pub data: Vec<u8>,
}

impl WriteEntry {
    /// Create a file write entry.
    pub fn new(path: impl Into<String>, data: impl AsRef<[u8]>) -> Self {
        Self {
            path: path.into(),
            data: data.as_ref().to_vec(),
        }
    }
}

/// Options for bounded file reads.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FileReadOptions {
    /// Maximum bytes to read before failing. When omitted, the full file is read.
    pub max_bytes: Option<usize>,
}

/// Options for applying a patch payload inside the sandbox.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ApplyDiffOptions {
    /// Optional working directory used to resolve relative patch paths.
    pub cwd: Option<String>,
}

/// Failed hunk metadata returned by `Filesystem::apply_diff`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ApplyDiffFailedHunk {
    /// One-based hunk index within the failed file patch.
    pub index: usize,
    /// Original starting line from the failed hunk.
    pub old_start: usize,
}

/// One failed file patch returned by `Filesystem::apply_diff`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ApplyDiffFailure {
    /// Path that failed to apply.
    pub path: String,
    /// Human-readable failure reason.
    pub error: String,
    /// Failed hunk details when available.
    pub failed_hunk: Option<ApplyDiffFailedHunk>,
}

/// Per-file patch summary returned by `Filesystem::apply_diff`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ApplyDiffFileSummary {
    /// Target path.
    pub path: String,
    /// Source path for move/rename patches.
    pub source_path: Option<String>,
    /// Patch kind such as `created`, `updated`, or `deleted`.
    pub kind: String,
    /// Added line count.
    pub added: usize,
    /// Removed line count.
    pub removed: usize,
}

/// Aggregate patch summary returned by `Filesystem::apply_diff`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ApplyDiffSummary {
    /// Number of file patches requested.
    pub requested: usize,
    /// Number of file patches applied.
    pub applied: usize,
    /// Number of file patches that failed.
    pub failed: usize,
}

/// Structured result returned by `Filesystem::apply_diff`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ApplyDiffReport {
    /// Overall status: `applied`, `partial`, or `failed`.
    pub status: String,
    /// Number of parsed diff blocks.
    pub parsed_diff_blocks: usize,
    /// Number of parsed file patches.
    pub patches: usize,
    /// Per-file patch summaries.
    pub files: Vec<ApplyDiffFileSummary>,
    /// Aggregate patch summary.
    pub summary: ApplyDiffSummary,
    /// Paths successfully applied.
    pub applied: Vec<String>,
    /// Per-file failures.
    pub failed: Vec<ApplyDiffFailure>,
    /// Paths touched by the operation.
    pub touched: Vec<String>,
    /// Raw runtime response.
    pub raw: Value,
}

/// Filesystem event returned by a watch stream.
#[derive(Clone, Debug, Default)]
pub struct FilesystemEvent {
    /// Event type such as `create`, `write`, `remove`, or `rename`.
    pub event_type: String,
    /// Absolute sandbox path.
    pub path: String,
    /// Basename of the changed path.
    pub name: String,
    /// Entry metadata when the runtime can still stat the changed path.
    pub entry: Option<EntryInfo>,
    /// Full raw event payload.
    pub raw: Value,
}

/// Options for `Filesystem::watch_dir`.
#[derive(Clone, Debug, Default)]
pub struct WatchOptions {
    /// Whether to watch recursively.
    pub recursive: bool,
    /// Whether to include entry metadata when available.
    pub include_entry: bool,
}

/// Live filesystem watch stream.
pub struct WatchHandle {
    socket: ProcessSocket,
}

impl WatchHandle {
    /// Read the next batch of filesystem events.
    pub async fn next_events(&mut self) -> Result<Option<Vec<FilesystemEvent>>> {
        while let Some(frame) = self.socket.next_frame().await? {
            if frame.get("type").and_then(Value::as_str) != Some("events") {
                continue;
            }
            let events = frame
                .get("events")
                .and_then(Value::as_array)
                .cloned()
                .unwrap_or_default()
                .into_iter()
                .map(filesystem_event)
                .collect();
            return Ok(Some(events));
        }
        Ok(None)
    }

    /// Stop watching the directory.
    pub async fn stop(&mut self) -> Result<()> {
        self.socket.close().await
    }
}

/// Filesystem helper for a sandbox data-plane session.
#[derive(Clone)]
pub struct Filesystem {
    data_plane: DataPlaneClient,
}

impl Filesystem {
    pub(crate) fn new(data_plane: DataPlaneClient) -> Self {
        Self { data_plane }
    }

    /// Read a file as UTF-8 text.
    pub async fn read_text(&self, path: &str) -> Result<String> {
        let bytes = self.read_bytes(path).await?;
        Ok(String::from_utf8_lossy(&bytes).into_owned())
    }

    /// Read a file as raw bytes.
    pub async fn read_bytes(&self, path: &str) -> Result<Vec<u8>> {
        self.read_bytes_with_options(path, FileReadOptions::default())
            .await
    }

    /// Read a file as raw bytes with an optional SDK-side byte cap.
    pub async fn read_bytes_with_options(
        &self,
        path: &str,
        opts: FileReadOptions,
    ) -> Result<Vec<u8>> {
        self.data_plane
            .get_bytes_with_limit(
                &format!("/runtime/v1/files?path={}", urlencoding(path)),
                opts.max_bytes,
            )
            .await
    }

    /// Write bytes or text to a file.
    pub async fn write(&self, path: &str, data: impl AsRef<[u8]>) -> Result<WriteInfo> {
        let payload = self
            .data_plane
            .put_bytes(
                &format!("/runtime/v1/files?path={}", urlencoding(path)),
                data.as_ref().to_vec(),
            )
            .await?;
        Ok(entry_info(payload.get("file").unwrap_or(&payload)))
    }

    /// Write several files in one runtime API call.
    pub async fn write_files(&self, files: Vec<WriteEntry>) -> Result<Vec<WriteInfo>> {
        if files.is_empty() {
            return Ok(vec![]);
        }
        let payload = self
            .data_plane
            .post_json("/runtime/v1/files/write_files", write_files_payload(&files))
            .await?;
        Ok(payload
            .get("files")
            .and_then(Value::as_array)
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(|value| entry_info(&value))
            .collect())
    }

    /// List directory entries below `path`.
    pub async fn list(&self, path: &str) -> Result<Vec<EntryInfo>> {
        let payload = self
            .data_plane
            .get_json(&format!(
                "/runtime/v1/directories?path={}",
                urlencoding(path)
            ))
            .await?;
        Ok(payload
            .get("entries")
            .and_then(Value::as_array)
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(|value| entry_info(&value))
            .collect())
    }

    /// Return whether a file or directory exists at `path`.
    pub async fn exists(&self, path: &str) -> Result<bool> {
        match self.get_info(path).await {
            Ok(_) => Ok(true),
            Err(Error::FileNotFound(_)) | Err(Error::NotFound(_)) => Ok(false),
            Err(error) => Err(error),
        }
    }

    /// Return stat metadata for `path`.
    pub async fn get_info(&self, path: &str) -> Result<EntryInfo> {
        let payload = self
            .data_plane
            .get_json(&format!(
                "/runtime/v1/files/stat?path={}",
                urlencoding(path)
            ))
            .await?;
        Ok(entry_info(
            payload
                .get("file")
                .or_else(|| payload.get("entry"))
                .unwrap_or(&payload),
        ))
    }

    /// Remove a file at `path`.
    pub async fn remove(&self, path: &str) -> Result<()> {
        self.data_plane
            .delete_json(&format!("/runtime/v1/files?path={}", urlencoding(path)))
            .await?;
        Ok(())
    }

    /// Move or rename a file.
    pub async fn rename(&self, old_path: &str, new_path: &str) -> Result<EntryInfo> {
        let payload = self
            .data_plane
            .post_json(
                "/runtime/v1/files/move",
                serde_json::json!({"from_path": old_path, "to_path": new_path}),
            )
            .await?;
        Ok(entry_info(payload.get("file").unwrap_or(&payload)))
    }

    /// Apply a git-style unified diff or Codex apply_patch payload inside the sandbox.
    pub async fn apply_diff(&self, diff: &str, opts: ApplyDiffOptions) -> Result<ApplyDiffReport> {
        let payload = self
            .data_plane
            .post_json(
                "/runtime/v1/files/apply_diff",
                apply_diff_payload(diff, &opts),
            )
            .await?;
        Ok(apply_diff_report(payload))
    }

    /// Create a directory.
    pub async fn make_dir(&self, path: &str) -> Result<bool> {
        self.data_plane
            .post_json(
                &format!("/runtime/v1/directories?path={}", urlencoding(path)),
                serde_json::json!({}),
            )
            .await?;
        Ok(true)
    }

    /// Start watching a directory for filesystem events.
    pub async fn watch_dir(&self, path: &str, opts: WatchOptions) -> Result<WatchHandle> {
        let query = format!(
            "path={}&recursive={}&include_entry={}",
            urlencoding(path),
            opts.recursive,
            opts.include_entry
        );
        let socket = ProcessSocket::connect(
            &self.data_plane.base_url,
            &self.data_plane.token,
            &format!("/runtime/v1/files/watch?{query}"),
        )
        .await?;
        Ok(WatchHandle { socket })
    }
}

fn entry_info(value: &Value) -> EntryInfo {
    EntryInfo {
        name: value
            .get("name")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string(),
        path: value
            .get("path")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string(),
        file_type: value.get("type").and_then(Value::as_str).map(file_type),
        size: value
            .get("bytes")
            .or_else(|| value.get("size"))
            .and_then(Value::as_u64),
        metadata: value
            .get("metadata")
            .and_then(Value::as_object)
            .cloned()
            .unwrap_or_default(),
    }
}

fn file_type(value: &str) -> FileType {
    match value {
        "file" => FileType::File,
        "dir" | "directory" => FileType::Dir,
        "symlink" => FileType::Symlink,
        other => FileType::Other(other.to_string()),
    }
}

fn filesystem_event(value: Value) -> FilesystemEvent {
    let path = value
        .get("path")
        .and_then(Value::as_str)
        .unwrap_or_default()
        .to_string();
    let event_type = match value
        .get("type")
        .and_then(Value::as_str)
        .unwrap_or("modify")
    {
        "delete" => "remove",
        "modify" => "write",
        other => other,
    }
    .to_string();
    let entry = value.get("file").map(entry_info);
    FilesystemEvent {
        name: path
            .trim_end_matches('/')
            .rsplit('/')
            .next()
            .unwrap_or_default()
            .to_string(),
        path,
        event_type,
        entry,
        raw: value,
    }
}

fn apply_diff_report(value: Value) -> ApplyDiffReport {
    ApplyDiffReport {
        status: value
            .get("status")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string(),
        parsed_diff_blocks: usize_value(value.get("parsed_diff_blocks")),
        patches: usize_value(value.get("patches")),
        files: value
            .get("files")
            .and_then(Value::as_array)
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(apply_diff_file_summary)
            .collect(),
        summary: apply_diff_summary(value.get("summary").unwrap_or(&Value::Null)),
        applied: string_vec(value.get("applied")),
        failed: value
            .get("failed")
            .and_then(Value::as_array)
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(apply_diff_failure)
            .collect(),
        touched: string_vec(value.get("touched")),
        raw: value,
    }
}

fn apply_diff_summary(value: &Value) -> ApplyDiffSummary {
    ApplyDiffSummary {
        requested: usize_value(value.get("requested")),
        applied: usize_value(value.get("applied")),
        failed: usize_value(value.get("failed")),
    }
}

fn apply_diff_file_summary(value: Value) -> ApplyDiffFileSummary {
    ApplyDiffFileSummary {
        path: value
            .get("path")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string(),
        source_path: value
            .get("source_path")
            .and_then(Value::as_str)
            .map(str::to_string),
        kind: value
            .get("kind")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string(),
        added: usize_value(value.get("added")),
        removed: usize_value(value.get("removed")),
    }
}

fn apply_diff_failure(value: Value) -> ApplyDiffFailure {
    let failed_hunk = value.get("failed_hunk").and_then(|hunk| {
        hunk.as_object().map(|_| ApplyDiffFailedHunk {
            index: usize_value(hunk.get("index")),
            old_start: usize_value(hunk.get("old_start")),
        })
    });
    ApplyDiffFailure {
        path: value
            .get("path")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string(),
        error: value
            .get("error")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string(),
        failed_hunk,
    }
}

fn usize_value(value: Option<&Value>) -> usize {
    value.and_then(Value::as_u64).unwrap_or(0) as usize
}

fn string_vec(value: Option<&Value>) -> Vec<String> {
    value
        .and_then(Value::as_array)
        .cloned()
        .unwrap_or_default()
        .into_iter()
        .filter_map(|value| value.as_str().map(str::to_string))
        .collect()
}

fn urlencoding(value: &str) -> String {
    url::form_urlencoded::byte_serialize(value.as_bytes()).collect()
}

fn write_files_payload(files: &[WriteEntry]) -> Value {
    serde_json::json!({
        "files": files
            .iter()
            .map(|file| {
                serde_json::json!({
                    "path": file.path.as_str(),
                    "data_base64": base64::engine::general_purpose::STANDARD.encode(&file.data),
                })
            })
            .collect::<Vec<_>>()
    })
}

fn apply_diff_payload(diff: &str, opts: &ApplyDiffOptions) -> Value {
    let mut payload = serde_json::json!({ "diff": diff });
    if let Some(cwd) = opts.cwd.as_deref().filter(|cwd| !cwd.is_empty()) {
        payload["cwd"] = Value::String(cwd.to_string());
    }
    payload
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::{apply_diff_payload, write_files_payload, ApplyDiffOptions, WriteEntry};

    #[test]
    fn write_files_payload_uses_snake_case_base64_entries() {
        assert_eq!(
            write_files_payload(&[
                WriteEntry::new("/tmp/a.txt", "abc"),
                WriteEntry::new("/tmp/b.bin", [0, 1, 2]),
            ]),
            json!({
                "files": [
                    {"path": "/tmp/a.txt", "data_base64": "YWJj"},
                    {"path": "/tmp/b.bin", "data_base64": "AAEC"}
                ]
            })
        );
    }

    #[test]
    fn apply_diff_payload_uses_snake_case_route_body() {
        assert_eq!(
            apply_diff_payload(
                "diff --git a/a.txt b/a.txt",
                &ApplyDiffOptions {
                    cwd: Some("/workspace/app".to_string()),
                }
            ),
            json!({
                "diff": "diff --git a/a.txt b/a.txt",
                "cwd": "/workspace/app"
            })
        );
    }
}