pi_agent_rust 0.8.0

Native AI coding agent CLI - Rust port of Pi Agent
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
//! Explicit capture of one click-triggered Chromium download.

use super::cdp::{Cdp, DownloadRecord};
use super::interaction::{self, References};
use super::{output, policy, required};
use crate::agent_cx::AgentCx;
use crate::error::{Error, Result};
use crate::tools::ToolOutput;
use serde_json::{Value, json};
use std::collections::BTreeSet;
use std::io::Read as _;
use std::path::{Component, Path};

const MAX_DOWNLOAD_BYTES: u64 = 100 * 1024 * 1024;
const MAX_DOWNLOAD_BYTES_F64: f64 = 100.0 * 1024.0 * 1024.0;
const MAX_WAIT_EVENTS: usize = 8192;

fn error(message: impl Into<String>) -> Error {
    Error::tool("browser", message)
}

fn lexical_output_path(path: &str) -> Result<()> {
    if path.is_empty()
        || path.len() > 4096
        || path.contains('\0')
        || path.contains('\\')
        || path.chars().any(char::is_control)
    {
        return Err(error(
            "download output_path must be a nonempty control-free relative path using slash separators",
        ));
    }
    let path = Path::new(path);
    if path.is_absolute()
        || path.file_name().is_none()
        || path
            .components()
            .any(|part| !matches!(part, Component::Normal(_) | Component::CurDir))
    {
        return Err(error(
            "download output_path must name a relative workspace file without parent traversal",
        ));
    }
    Ok(())
}

pub(super) fn validate(args: &Value) -> Result<()> {
    let object = args
        .as_object()
        .ok_or_else(|| error("download arguments must be an object"))?;
    for field in object.keys() {
        if !matches!(
            field.as_str(),
            "action" | "tab" | "selector" | "output_path" | "timeout_ms"
        ) {
            return Err(error(format!("unsupported download parameter: {field}")));
        }
    }
    let selector = required(args, "selector")?;
    if selector.is_empty() || selector.len() > 4096 || selector.contains('\0') {
        return Err(error(
            "download selector must be nonempty, NUL-free and at most 4096 bytes",
        ));
    }
    if let Some(path) = args.get("output_path") {
        lexical_output_path(
            path.as_str()
                .ok_or_else(|| error("download output_path must be a string"))?,
        )?;
    }
    Ok(())
}

fn safe_filename(value: &str) -> String {
    let leaf = value.rsplit(['/', '\\']).next().unwrap_or_default();
    let mut result = String::new();
    for ch in leaf.chars().take(180) {
        if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '-' | '_' | ' ') {
            result.push(ch);
        } else {
            result.push('_');
        }
    }
    let trimmed = result.trim_matches(['.', ' ']).to_string();
    if trimmed.is_empty() || matches!(trimmed.as_str(), "." | "..") {
        format!("download-{}", uuid::Uuid::new_v4().simple())
    } else {
        trimmed
    }
}

fn first_new(cdp: &Cdp, before: &BTreeSet<String>) -> Result<Option<DownloadRecord>> {
    let fresh = cdp.downloads_since(before);
    if fresh.len() > 1 {
        return Err(error(
            "one download action started multiple transfers; none was published",
        ));
    }
    Ok(fresh.into_iter().next())
}

fn validate_download_guid(guid: &str) -> Result<()> {
    // CDP is a protocol boundary, not a source of trusted filesystem paths.
    // In particular, Path::join would discard the private directory for an
    // absolute GUID, and allowAndName does not make a received string safe.
    let mut components = Path::new(guid).components();
    if guid.is_empty()
        || guid.len() > 128
        || guid.contains('/')
        || guid.contains('\\')
        || guid.chars().any(char::is_control)
        || !matches!(components.next(), Some(Component::Normal(_)))
        || components.next().is_some()
    {
        return Err(error("download GUID must be a single bounded filename"));
    }
    Ok(())
}

fn open_completed(directory: &Path, guid: &str) -> Result<std::fs::File> {
    validate_download_guid(guid)?;
    #[cfg(all(unix, not(any(target_os = "espidf", target_os = "redox"))))]
    {
        use rustix::fs::{Mode, OFlags};
        let directory = rustix::fs::open(
            directory,
            OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC | OFlags::NOFOLLOW,
            Mode::empty(),
        )
        .map_err(|failure| error(format!("cannot pin private download directory: {failure}")))?;
        // NONBLOCK prevents a raced-in FIFO from hanging before we can inspect
        // the descriptor. NOFOLLOW rejects a substituted symlink atomically.
        let file = rustix::fs::openat(
            &directory,
            guid,
            OFlags::RDONLY | OFlags::CLOEXEC | OFlags::NOFOLLOW | OFlags::NONBLOCK,
            Mode::empty(),
        )
        .map_err(|failure| error(format!("cannot safely open completed download: {failure}")))?;
        Ok(std::fs::File::from(file))
    }
    #[cfg(not(all(unix, not(any(target_os = "espidf", target_os = "redox")))))]
    {
        let path = directory.join(guid);
        let metadata = std::fs::symlink_metadata(&path).map_err(|failure| {
            error(format!("completed download file is unavailable: {failure}"))
        })?;
        if !metadata.is_file() || metadata.file_type().is_symlink() {
            return Err(error("download result must be a regular file"));
        }
        Ok(std::fs::File::open(path)?)
    }
}

#[expect(clippy::cast_precision_loss)]
fn read_completed(directory: &Path, progress: &DownloadRecord) -> Result<Vec<u8>> {
    if progress.state != "completed" {
        return Err(error("download has not completed"));
    }
    if [progress.received_bytes, progress.total_bytes]
        .into_iter()
        .any(|value| !value.is_finite() || !(0.0..=MAX_DOWNLOAD_BYTES_F64).contains(&value))
    {
        return Err(error(
            "download progress is outside the 100 MiB capture budget",
        ));
    }
    let mut file = open_completed(directory, &progress.guid)?;
    // Validate the object actually opened, not a pathname checked before open.
    let metadata = file.metadata()?;
    if !metadata.is_file() || metadata.len() > MAX_DOWNLOAD_BYTES {
        return Err(error(
            "download result must be a regular file no larger than 100 MiB",
        ));
    }
    let mut bytes = Vec::new();
    file.by_ref()
        .take(MAX_DOWNLOAD_BYTES + 1)
        .read_to_end(&mut bytes)?;
    if bytes.len() as u64 > MAX_DOWNLOAD_BYTES {
        return Err(error("download grew beyond the 100 MiB capture budget"));
    }
    // All allowed byte counts are exactly representable in f64. Zero is a
    // real completed byte count, not permission to skip the integrity check.
    if (progress.received_bytes - bytes.len() as f64).abs() > 0.0 {
        return Err(error(
            "completed download byte count did not match Chromium progress",
        ));
    }
    Ok(bytes)
}

struct DownloadRequest<'a> {
    cwd: &'a Path,
    tab: &'a str,
    refs: Option<&'a References>,
    selector: &'a str,
    explicit: Option<&'a crate::artifact_output::OutputTarget>,
    allowlist: Option<&'a [String]>,
}

async fn capture_download(
    owner: &AgentCx,
    cdp: &mut Cdp,
    directory: &Path,
    req: &DownloadRequest<'_>,
) -> Result<(
    DownloadRecord,
    DownloadRecord,
    Vec<u8>,
    crate::artifact_output::OutputTarget,
)> {
    let before = cdp.download_ids();
    let click_args = json!({"action":"click","selector":req.selector});
    interaction::execute(owner, cdp, req.tab, req.refs, &click_args).await?;

    let mut start = None;
    for _ in 0..MAX_WAIT_EVENTS {
        if let Some(record) = first_new(cdp, &before)? {
            start = Some(record);
            break;
        }
        cdp.pump_event(owner).await?;
    }
    let start = start.ok_or_else(|| error("click produced no download event"))?;
    validate_download_guid(&start.guid)?;
    if let Err(failure) = policy::check_navigation(&start.url, req.allowlist) {
        let _ = cdp
            .browser_command(owner, "Browser.cancelDownload", json!({"guid":start.guid}))
            .await;
        return Err(failure);
    }

    let mut final_record = None;
    for _ in 0..MAX_WAIT_EVENTS {
        let record = cdp
            .download_record(&start.guid)
            .ok_or_else(|| error("download tracking state disappeared"))?;
        if record.received_bytes > MAX_DOWNLOAD_BYTES_F64
            || record.total_bytes > MAX_DOWNLOAD_BYTES_F64
        {
            let _ = cdp
                .browser_command(owner, "Browser.cancelDownload", json!({"guid":start.guid}))
                .await;
            return Err(error("download exceeded the 100 MiB capture budget"));
        }
        match record.state.as_str() {
            "completed" => {
                final_record = Some(record);
                break;
            }
            "canceled" => return Err(error("Chromium canceled the download")),
            "inProgress" => cdp.pump_event(owner).await?,
            _ => return Err(error("Chromium reported an unknown download state")),
        }
    }
    let final_record = final_record
        .ok_or_else(|| error("download did not complete within the CDP event budget"))?;
    owner
        .checkpoint()
        .map_err(|_| error("download cancelled before reading completed bytes"))?;
    let bytes = read_completed(directory, &final_record)?;
    let target = if let Some(target) = req.explicit {
        (*target).clone()
    } else {
        let filename = safe_filename(&start.suggested_filename);
        crate::artifact_output::resolve_new(req.cwd, &format!("downloads/{filename}"), "browser")?
    };
    Ok((start, final_record, bytes, target))
}

pub(super) async fn execute(
    owner: &AgentCx,
    cdp: &mut Cdp,
    cwd: &Path,
    tab: &str,
    refs: Option<&References>,
    args: &Value,
    allowlist: Option<&[String]>,
) -> Result<ToolOutput> {
    validate(args)?;
    let selector = required(args, "selector")?;
    let explicit = args
        .get("output_path")
        .and_then(Value::as_str)
        .map(|path| crate::artifact_output::resolve_new(cwd, path, "browser"))
        .transpose()?;
    let directory = tempfile::Builder::new()
        .prefix("pi-browser-download-")
        .tempdir()?;
    let download_path = directory
        .path()
        .to_str()
        .ok_or_else(|| error("private download directory path is not UTF-8"))?;
    cdp.browser_command(
        owner,
        "Browser.setDownloadBehavior",
        json!({
            "behavior":"allowAndName",
            "downloadPath":download_path,
            "eventsEnabled":true
        }),
    )
    .await?;

    let req = DownloadRequest {
        cwd,
        tab,
        refs,
        selector,
        explicit: explicit.as_ref(),
        allowlist,
    };
    let capture = capture_download(owner, cdp, directory.path(), &req).await;

    // Ordinary errors attempt to return to deny. If outer cancellation drops
    // this future, Session leaves its policy marker false so the next operation
    // re-applies deny before doing anything else.
    let reset = cdp
        .browser_command(
            owner,
            "Browser.setDownloadBehavior",
            json!({"behavior":"deny","eventsEnabled":true}),
        )
        .await;

    let (start, progress, bytes, target) = capture?;
    reset.map_err(|failure| {
        error(format!(
            "download completed but browser download policy could not be restored: {failure}"
        ))
    })?;
    owner
        .checkpoint()
        .map_err(|_| error("download cancelled before publication"))?;
    crate::artifact_output::publish(&target, &bytes, "browser")?;

    Ok(output(
        format!(
            "Downloaded {} bytes from {} to {}",
            bytes.len(),
            start.url,
            target.path().display()
        ),
        json!({
            "action":"download","tab":tab,"selector":selector,
            "saved_path":target.path().display().to_string(),
            "url":start.url,"suggested_filename":start.suggested_filename,
            "size_bytes":bytes.len(),"received_bytes":progress.received_bytes,
            "backend":"cdp"
        }),
    ))
}

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

    fn completed_record(guid: &str, received_bytes: f64) -> DownloadRecord {
        DownloadRecord {
            guid: guid.into(),
            url: "https://example.com/file".into(),
            suggested_filename: "file".into(),
            state: "completed".into(),
            received_bytes,
            total_bytes: received_bytes,
        }
    }

    #[test]
    fn parameters_and_filenames_fail_closed() {
        assert!(validate(&json!({"action":"download","selector":"#link"})).is_ok());
        for args in [
            json!({"action":"download"}),
            json!({"action":"download","selector":"#x","files":[]}),
            json!({"action":"download","selector":"#x","output_path":"../escape.bin"}),
            json!({"action":"download","selector":"#x","output_path":"/tmp/escape.bin"}),
            json!({"action":"download","selector":"#x","output_path":"a\\escape.bin"}),
        ] {
            assert!(validate(&args).is_err(), "{args}");
        }
        assert_eq!(safe_filename("../../report final.pdf"), "report final.pdf");
        assert!(!safe_filename("..").is_empty());
        assert!(!safe_filename("bad\nname?.txt").contains('\n'));
        assert!(!safe_filename("bad/name.txt").contains('/'));
    }

    #[test]
    fn completed_file_must_match_progress_and_budget() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("guid"), b"payload").unwrap();
        let record = completed_record("guid", 7.0);
        assert_eq!(read_completed(dir.path(), &record).unwrap(), b"payload");
        for received in [
            0.0,
            7.5,
            8.0,
            -1.0,
            f64::NAN,
            f64::INFINITY,
            MAX_DOWNLOAD_BYTES_F64 + 1.0,
        ] {
            let mut wrong = record.clone();
            wrong.received_bytes = received;
            assert!(read_completed(dir.path(), &wrong).is_err(), "{received}");
        }
        let mut pending = record.clone();
        pending.state = "inProgress".into();
        assert!(read_completed(dir.path(), &pending).is_err());
        let mut invalid_total = record;
        invalid_total.total_bytes = f64::NAN;
        assert!(read_completed(dir.path(), &invalid_total).is_err());
    }

    #[test]
    fn download_guids_cannot_escape_private_staging() {
        let dir = tempfile::tempdir().unwrap();
        let outside = tempfile::tempdir().unwrap();
        let secret = outside.path().join("secret.bin");
        std::fs::write(&secret, b"secret").unwrap();
        let record = completed_record(secret.to_str().unwrap(), 6.0);
        assert!(read_completed(dir.path(), &record).is_err());
        for guid in [
            "",
            ".",
            "..",
            "../secret.bin",
            "nested/../../secret.bin",
            "nested/file",
            "file/",
            "nested\\file",
            "C:\\secret.bin",
            "bad\0guid",
            "bad\nguid",
        ] {
            let record = completed_record(guid, 6.0);
            assert!(read_completed(dir.path(), &record).is_err(), "{guid:?}");
        }
        assert!(validate_download_guid(&"x".repeat(129)).is_err());
        for guid in [
            "guid",
            "opaque-id_42",
            "opaque.name",
            "12345678-abcd-1234-abcd-123456789abc",
        ] {
            assert!(validate_download_guid(guid).is_ok(), "{guid}");
        }
    }

    #[test]
    fn genuinely_empty_completed_file_is_read_without_bypassing_counts() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("guid"), b"").unwrap();
        let record = completed_record("guid", 0.0);
        assert!(read_completed(dir.path(), &record).unwrap().is_empty());
    }

    #[cfg(all(unix, not(any(target_os = "espidf", target_os = "redox"))))]
    #[test]
    fn completed_file_rejects_linked_private_directory() {
        use std::os::unix::fs::symlink;
        let parent = tempfile::tempdir().unwrap();
        let outside = tempfile::tempdir().unwrap();
        std::fs::write(outside.path().join("guid"), b"secret").unwrap();
        let linked = parent.path().join("staging");
        symlink(outside.path(), &linked).unwrap();
        assert!(read_completed(&linked, &completed_record("guid", 6.0)).is_err());
    }

    #[cfg(all(unix, not(any(target_os = "espidf", target_os = "redox"))))]
    #[test]
    fn completed_file_rejects_symlink_and_fifo_leaves() {
        use rustix::fs::Mode;
        use std::os::unix::fs::symlink;
        let dir = tempfile::tempdir().unwrap();
        let outside = tempfile::tempdir().unwrap();
        let secret = outside.path().join("secret.bin");
        std::fs::write(&secret, b"secret").unwrap();
        symlink(&secret, dir.path().join("linked")).unwrap();
        assert!(read_completed(dir.path(), &completed_record("linked", 6.0)).is_err());
        rustix::fs::mkfifoat(
            rustix::fs::CWD,
            dir.path().join("fifo"),
            Mode::RUSR | Mode::WUSR,
        )
        .unwrap();
        assert!(read_completed(dir.path(), &completed_record("fifo", 0.0)).is_err());
    }
}