vtcode-core 0.181.8

Core library for VT Code - a Rust-based terminal coding agent
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
use anyhow::{Context, Result, bail};
use serde::Deserialize;
use std::time::Duration;
use vtcode_commons::http::send_with_retry;
use vtcode_commons::retry::RetryPolicy;
use vtcode_commons::utils::calculate_sha256;

const AST_GREP_RELEASE_API: &str = "https://api.github.com/repos/ast-grep/ast-grep/releases/latest";

/// Retry budget for installer fetches: a single GitHub flake must not fail the
/// install (failures arm a 24h cooldown), but the install must not hang either:
/// 3 attempts over ~3s of jittered backoff, then the existing error path.
fn installer_retry_policy() -> RetryPolicy {
    RetryPolicy::from_retries(2, Duration::from_secs(1), Duration::from_secs(30), 2.0)
        .with_jitter(RetryPolicy::DEFAULT_JITTER)
}

#[derive(Debug, Deserialize)]
pub(super) struct ReleaseInfo {
    tag_name: String,
    assets: Vec<ReleaseAsset>,
}

#[derive(Debug, Clone, Deserialize)]
pub(super) struct ReleaseAsset {
    pub(super) name: String,
    browser_download_url: String,
}

#[derive(Debug, Clone, Copy)]
pub(super) struct PlatformAssetSpec {
    candidate_triples: &'static [&'static str],
    archive_exts: &'static [&'static str],
}

#[derive(Debug, Clone)]
pub(super) struct SelectedAsset {
    pub(super) tag_name: String,
    pub(super) asset: ReleaseAsset,
}

pub(super) async fn fetch_latest_release(client: &reqwest::Client) -> Result<ReleaseInfo> {
    fetch_latest_release_from(client, AST_GREP_RELEASE_API).await
}

pub(super) async fn fetch_latest_release_from(client: &reqwest::Client, url: &str) -> Result<ReleaseInfo> {
    send_with_retry(&installer_retry_policy(), || client.get(url).send())
        .await
        .context("Failed to fetch ast-grep release metadata")?
        .error_for_status()
        .context("ast-grep release endpoint returned non-success status")?
        .json::<ReleaseInfo>()
        .await
        .context("Failed to parse ast-grep release metadata")
}

/// Actionable manual install steps shown whenever automatic installation fails.
pub(super) fn manual_install_guide() -> &'static str {
    "Install ast-grep manually and rerun `vtcode dependencies status ast-grep`:\n\
     - macOS: brew install ast-grep\n\
     - Any OS with Rust: cargo install ast-grep --locked\n\
     - Or download app-<target>.zip from https://github.com/ast-grep/ast-grep/releases/latest, \
     unpack the ast-grep binary into ~/.local/bin (or set VTCODE_AST_GREP_BIN to its path)"
}

pub(super) fn current_platform_asset_spec() -> Result<PlatformAssetSpec> {
    match (std::env::consts::OS, std::env::consts::ARCH) {
        ("macos", "x86_64") => Ok(PlatformAssetSpec {
            candidate_triples: &["x86_64-apple-darwin"],
            archive_exts: &[".zip", ".tar.gz"],
        }),
        ("macos", "aarch64") => Ok(PlatformAssetSpec {
            candidate_triples: &["aarch64-apple-darwin"],
            archive_exts: &[".zip", ".tar.gz"],
        }),
        ("linux", "x86_64") => Ok(PlatformAssetSpec {
            candidate_triples: &["x86_64-unknown-linux-musl", "x86_64-unknown-linux-gnu"],
            archive_exts: &[".zip", ".tar.gz"],
        }),
        ("linux", "aarch64") => Ok(PlatformAssetSpec {
            candidate_triples: &["aarch64-unknown-linux-musl", "aarch64-unknown-linux-gnu"],
            archive_exts: &[".zip", ".tar.gz"],
        }),
        ("windows", "x86_64") => Ok(PlatformAssetSpec {
            candidate_triples: &["x86_64-pc-windows-msvc"],
            archive_exts: &[".zip"],
        }),
        ("windows", "aarch64") => Ok(PlatformAssetSpec {
            candidate_triples: &["aarch64-pc-windows-msvc"],
            archive_exts: &[".zip"],
        }),
        _ => bail!(
            "Unsupported platform for VT Code-managed ast-grep install ({} {}).\n{}",
            std::env::consts::OS,
            std::env::consts::ARCH,
            manual_install_guide()
        ),
    }
}

pub(super) fn select_release_asset(release: &ReleaseInfo, platform: &PlatformAssetSpec) -> Result<SelectedAsset> {
    for triple in platform.candidate_triples {
        if let Some(asset) = release
            .assets
            .iter()
            .find(|asset| asset_matches_target(&asset.name, triple, platform.archive_exts))
        {
            return Ok(SelectedAsset {
                tag_name: release.tag_name.clone(),
                asset: asset.clone(),
            });
        }
    }

    let mut available: Vec<&str> = release.assets.iter().map(|asset| asset.name.as_str()).collect();
    available.sort_unstable();
    const MAX_LISTED: usize = 12;
    let listed = if available.len() > MAX_LISTED {
        format!("{} (and {} more)", available[..MAX_LISTED].join(", "), available.len() - MAX_LISTED)
    } else {
        available.join(", ")
    };
    bail!(
        "No ast-grep release asset matched the current platform ({}) in {} (available: {}).\n{}",
        platform.candidate_triples.join(", "),
        release.tag_name,
        if listed.is_empty() { "none".to_string() } else { listed },
        manual_install_guide()
    )
}

pub(super) fn asset_matches_target(asset_name: &str, triple: &str, archive_exts: &[&str]) -> bool {
    let name = asset_name.to_ascii_lowercase();
    name.contains(triple)
        && archive_exts.iter().any(|archive_ext| name.ends_with(archive_ext))
        && !name.ends_with(".sha256")
        && !name.contains("checksum")
        && !name.ends_with(".sig")
}

pub(super) async fn download_release_asset(client: &reqwest::Client, asset: &ReleaseAsset) -> Result<Vec<u8>> {
    send_with_retry(&installer_retry_policy(), || client.get(&asset.browser_download_url).send())
        .await
        .with_context(|| format!("Failed to download {}", asset.name))?
        .error_for_status()
        .with_context(|| format!("Download failed for {}", asset.name))?
        .bytes()
        .await
        .with_context(|| format!("Failed to read {}", asset.name))
        .map(|bytes| bytes.to_vec())
}

pub(super) async fn verify_checksum_if_available(
    client: &reqwest::Client,
    release: &ReleaseInfo,
    selected: &SelectedAsset,
    archive_bytes: &[u8],
) -> Result<Option<String>> {
    let Some(checksum_asset) = find_checksum_asset(release, &selected.asset.name) else {
        return Ok(Some(format!(
            "Checksum metadata was not published for {}. Continuing without checksum verification.",
            selected.asset.name
        )));
    };

    let checksum_response =
        match send_with_retry(&installer_retry_policy(), || client.get(&checksum_asset.browser_download_url).send())
            .await
        {
            Ok(response) => response,
            Err(err) => {
                return Ok(Some(format!(
                    "Checksum metadata for {} was unavailable ({}). Continuing without checksum verification.",
                    selected.asset.name, err
                )));
            }
        };
    let checksum_response = match checksum_response.error_for_status() {
        Ok(response) => response,
        Err(err) => {
            return Ok(Some(format!(
                "Checksum metadata for {} was unavailable ({}). Continuing without checksum verification.",
                selected.asset.name, err
            )));
        }
    };

    let checksum_text = match checksum_response
        .text()
        .await
        .with_context(|| format!("Failed to read checksum metadata {}", checksum_asset.name))
    {
        Ok(text) => text,
        Err(err) => {
            return Ok(Some(format!(
                "Checksum metadata for {} was unavailable ({}). Continuing without checksum verification.",
                selected.asset.name, err
            )));
        }
    };

    let Some(expected_checksum) = parse_expected_checksum(&checksum_text, &checksum_asset.name, &selected.asset.name)
    else {
        return Ok(Some(format!(
            "Checksum metadata for {} did not contain an entry for {}. Continuing without checksum verification.",
            checksum_asset.name, selected.asset.name
        )));
    };

    let actual_checksum = sha256_hex(archive_bytes);
    if actual_checksum != expected_checksum {
        bail!(
            "Checksum mismatch for {} (expected {}, got {})",
            selected.asset.name,
            expected_checksum,
            actual_checksum
        );
    }

    Ok(None)
}

fn find_checksum_asset<'a>(release: &'a ReleaseInfo, archive_name: &str) -> Option<&'a ReleaseAsset> {
    let archive_sha_name = format!("{archive_name}.sha256").to_ascii_lowercase();
    release.assets.iter().find(|asset| {
        let name = asset.name.to_ascii_lowercase();
        name == archive_sha_name || name == "checksums.txt" || name == "sha256sums.txt"
    })
}

pub(super) fn parse_expected_checksum(
    checksum_text: &str,
    checksum_asset_name: &str,
    archive_name: &str,
) -> Option<String> {
    if checksum_asset_name.ends_with(".sha256") {
        return checksum_text.split_whitespace().next().map(|value| value.to_string());
    }

    checksum_text
        .lines()
        .find(|line| line.contains(archive_name))
        .and_then(|line| line.split_whitespace().next())
        .map(|value| value.to_string())
}

fn sha256_hex(bytes: &[u8]) -> String {
    calculate_sha256(bytes)
}

#[cfg(test)]
mod tests {
    use super::{
        PlatformAssetSpec, ReleaseAsset, ReleaseInfo, asset_matches_target, fetch_latest_release_from,
        manual_install_guide, parse_expected_checksum, select_release_asset,
    };

    fn real_release_0_51_1() -> ReleaseInfo {
        ReleaseInfo {
            tag_name: "0.51.1".to_string(),
            assets: [
                "app-aarch64-apple-darwin.zip",
                "app-aarch64-pc-windows-msvc.zip",
                "app-aarch64-unknown-linux-gnu.zip",
                "app-i686-pc-windows-msvc.zip",
                "app-x86_64-apple-darwin.zip",
                "app-x86_64-pc-windows-msvc.zip",
                "app-x86_64-unknown-linux-gnu.zip",
            ]
            .into_iter()
            .map(|name| ReleaseAsset {
                name: name.to_string(),
                browser_download_url: format!("https://example.com/{name}"),
            })
            .collect(),
        }
    }

    #[test]
    fn release_asset_selection_uses_first_supported_triple() {
        let release = ReleaseInfo {
            tag_name: "v0.40.0".to_string(),
            assets: vec![
                ReleaseAsset {
                    name: "app-x86_64-unknown-linux-gnu.tar.gz".to_string(),
                    browser_download_url: "https://example.com/gnu".to_string(),
                },
                ReleaseAsset {
                    name: "app-x86_64-unknown-linux-musl.tar.gz".to_string(),
                    browser_download_url: "https://example.com/musl".to_string(),
                },
            ],
        };
        let platform = PlatformAssetSpec {
            candidate_triples: &["x86_64-unknown-linux-musl", "x86_64-unknown-linux-gnu"],
            archive_exts: &[".zip", ".tar.gz"],
        };

        let selected = select_release_asset(&release, &platform).expect("selected asset");
        assert_eq!(selected.asset.browser_download_url, "https://example.com/musl");
    }

    #[test]
    fn release_asset_selection_accepts_zip_assets() {
        let release = ReleaseInfo {
            tag_name: "v0.42.1".to_string(),
            assets: vec![ReleaseAsset {
                name: "app-aarch64-apple-darwin.zip".to_string(),
                browser_download_url: "https://example.com/macos".to_string(),
            }],
        };
        let platform = PlatformAssetSpec {
            candidate_triples: &["aarch64-apple-darwin"],
            archive_exts: &[".zip", ".tar.gz"],
        };

        let selected = select_release_asset(&release, &platform).expect("selected asset");
        assert_eq!(selected.asset.browser_download_url, "https://example.com/macos");
    }

    #[test]
    fn asset_matching_filters_checksum_files() {
        assert!(asset_matches_target(
            "app-aarch64-apple-darwin.tar.gz",
            "aarch64-apple-darwin",
            &[".zip", ".tar.gz"]
        ));
        assert!(asset_matches_target("app-aarch64-apple-darwin.zip", "aarch64-apple-darwin", &[".zip", ".tar.gz"]));
        assert!(!asset_matches_target(
            "app-aarch64-apple-darwin.tar.gz.sha256",
            "aarch64-apple-darwin",
            &[".zip", ".tar.gz"]
        ));
        assert!(!asset_matches_target("checksums.txt", "aarch64-apple-darwin", &[".zip", ".tar.gz"]));
    }

    #[test]
    fn release_asset_selection_matches_real_linux_gnu_zip() {
        // Regression test for issue #802: the real ast-grep 0.51.1 release
        // ships only app-*.zip assets (no musl, no tar.gz). The Linux x86_64
        // spec must still resolve via the gnu fallback triple.
        let release = real_release_0_51_1();
        let platform = PlatformAssetSpec {
            candidate_triples: &["x86_64-unknown-linux-musl", "x86_64-unknown-linux-gnu"],
            archive_exts: &[".zip", ".tar.gz"],
        };
        let selected = select_release_asset(&release, &platform).expect("gnu fallback must match");
        assert!(selected.asset.name.contains("x86_64-unknown-linux-gnu"), "{}", selected.asset.name);
    }

    #[test]
    fn release_asset_selection_matches_windows_aarch64() {
        let release = real_release_0_51_1();
        let platform = PlatformAssetSpec {
            candidate_triples: &["aarch64-pc-windows-msvc"],
            archive_exts: &[".zip"],
        };
        let selected = select_release_asset(&release, &platform).expect("win aarch64 must match");
        assert_eq!(selected.asset.name, "app-aarch64-pc-windows-msvc.zip");
    }

    #[test]
    fn asset_mismatch_error_lists_assets_and_manual_guide() {
        let release = ReleaseInfo {
            tag_name: "v9.9.9".to_string(),
            assets: vec![ReleaseAsset {
                name: "app-x86_64-apple-darwin.zip".to_string(),
                browser_download_url: "https://example.com/macos".to_string(),
            }],
        };
        let platform = PlatformAssetSpec {
            candidate_triples: &["x86_64-unknown-linux-musl", "x86_64-unknown-linux-gnu"],
            archive_exts: &[".zip", ".tar.gz"],
        };
        let err = select_release_asset(&release, &platform).expect_err("must fail without a linux asset");
        let message = format!("{err:#}");
        assert!(message.contains("No ast-grep release asset matched"), "{message}");
        assert!(message.contains("app-x86_64-apple-darwin.zip"), "{message}");
        assert!(message.contains(manual_install_guide()), "{message}");
    }

    #[test]
    fn checksum_parser_supports_sha256_sidecars() {
        let parsed = parse_expected_checksum("abc123  app.tar.gz", "app.tar.gz.sha256", "app.tar.gz");
        assert_eq!(parsed.as_deref(), Some("abc123"));
    }

    #[test]
    fn checksum_parser_supports_checksums_txt() {
        let parsed = parse_expected_checksum("abc123  app.tar.gz\nfff999 other.tar.gz", "checksums.txt", "app.tar.gz");
        assert_eq!(parsed.as_deref(), Some("abc123"));
    }

    #[test]
    fn asset_mismatch_error_truncates_long_asset_lists() {
        let release_with = |count: usize| ReleaseInfo {
            tag_name: "v9.9.9".to_string(),
            assets: (0..count)
                .map(|index| ReleaseAsset {
                    name: format!("app-unknown-platform-{index:02}.zip"),
                    browser_download_url: format!("https://example.com/{index}"),
                })
                .collect(),
        };
        let platform = PlatformAssetSpec {
            candidate_triples: &["x86_64-unknown-linux-gnu"],
            archive_exts: &[".zip", ".tar.gz"],
        };

        let full = select_release_asset(&release_with(12), &platform).expect_err("must fail without a match");
        let full_message = format!("{full:#}");
        assert!(!full_message.contains("(and "), "{full_message}");

        let truncated = select_release_asset(&release_with(13), &platform).expect_err("must fail without a match");
        let truncated_message = format!("{truncated:#}");
        assert!(truncated_message.contains("(and 1 more)"), "{truncated_message}");
    }

    /// Start a wiremock server, or skip when the sandbox forbids binding.
    /// Mirrors the `bind_fixture_listener` precedent in `vtcode-commons`.
    async fn start_mock_server_or_skip() -> Option<wiremock::MockServer> {
        match std::net::TcpListener::bind("127.0.0.1:0") {
            Ok(probe) => {
                drop(probe);
                Some(wiremock::MockServer::start().await)
            }
            Err(err) if err.kind() == std::io::ErrorKind::PermissionDenied => None,
            Err(err) => panic!("mock server probe failed: {err}"),
        }
    }

    #[tokio::test]
    async fn fetch_recovers_from_a_transient_503() {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, ResponseTemplate};

        let Some(server) = start_mock_server_or_skip().await else {
            return;
        };
        // First-mounted mock wins while it matches: the one-shot 503 answers
        // the first attempt, then the installer's retry transparently recovers
        // instead of failing the install into the 24h cooldown.
        Mock::given(method("GET"))
            .and(path("/releases/latest"))
            .respond_with(ResponseTemplate::new(503))
            .up_to_n_times(1)
            .expect(1)
            .mount(&server)
            .await;
        Mock::given(method("GET"))
            .and(path("/releases/latest"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "tag_name": "1.2.3",
                "assets": []
            })))
            .expect(1)
            .mount(&server)
            .await;

        let client = vtcode_commons::http::create_download_client("vtcode-test");
        let url = format!("{}/releases/latest", server.uri());
        let release = fetch_latest_release_from(&client, &url)
            .await
            .expect("transient 503 must be retried");
        assert_eq!(release.tag_name, "1.2.3");
    }

    #[test]
    fn manual_guide_covers_fallback_methods() {
        let guide = manual_install_guide();
        // "manually" is the marker the install-cache dedup checks for.
        assert!(guide.contains("manually"), "{guide}");
        assert!(guide.contains("brew install ast-grep"), "{guide}");
        assert!(guide.contains("cargo install ast-grep"), "{guide}");
        assert!(guide.contains("ast-grep/ast-grep/releases"), "{guide}");
        assert!(guide.contains("VTCODE_AST_GREP_BIN"), "{guide}");
    }
}