Skip to main content

rigspark_runtime/
coverage.rs

1use futures_util::TryStreamExt;
2use rigspark_core::coverage::{INVENTORY_URL, MAX_INVENTORY_BYTES, parse_inventory};
3use std::{io, time::Duration};
4use tokio::io::{AsyncRead, AsyncReadExt};
5use tokio_util::{io::StreamReader, sync::CancellationToken};
6
7pub async fn read_inventory_response(
8    status: u16,
9    declared_bytes: Option<u64>,
10    body: impl AsyncRead + Unpin,
11    cancel: &CancellationToken,
12) -> io::Result<Vec<String>> {
13    let operation = async {
14        if !(200..300).contains(&status) {
15            return Err(io::Error::other(format!(
16                "Ollama library inventory returned status {status}"
17            )));
18        }
19        if declared_bytes.is_some_and(|bytes| bytes > MAX_INVENTORY_BYTES as u64) {
20            return Err(io::Error::other(
21                "Ollama library inventory response is oversized",
22            ));
23        }
24        let mut bytes = Vec::new();
25        body.take(MAX_INVENTORY_BYTES as u64 + 1)
26            .read_to_end(&mut bytes)
27            .await?;
28        if bytes.len() > MAX_INVENTORY_BYTES {
29            return Err(io::Error::other(
30                "Ollama library inventory response is oversized",
31            ));
32        }
33        let source =
34            std::str::from_utf8(&bytes).map_err(|_| io::Error::other("inventory is not UTF-8"))?;
35        parse_inventory(source).map_err(io::Error::other)
36    };
37    tokio::select! {
38        biased;
39        _=cancel.cancelled()=>Err(io::Error::new(io::ErrorKind::Interrupted,"coverage cancelled")),
40        result=tokio::time::timeout(Duration::from_secs(15),operation)=>result.map_err(|_|io::Error::new(io::ErrorKind::TimedOut,"coverage timed out"))?,
41    }
42}
43
44pub async fn fetch_inventory(cancel: &CancellationToken) -> io::Result<Vec<String>> {
45    let operation = async {
46        let client = reqwest::Client::builder()
47            .https_only(true)
48            .no_proxy()
49            .redirect(reqwest::redirect::Policy::none())
50            .connect_timeout(Duration::from_secs(5))
51            .timeout(Duration::from_secs(15))
52            .build()
53            .map_err(|_| io::Error::other("coverage client initialization failed"))?;
54        let response = client
55            .get(INVENTORY_URL)
56            .send()
57            .await
58            .map_err(|_| io::Error::other("coverage request failed"))?;
59        let status = response.status().as_u16();
60        let declared = response.content_length();
61        let body = StreamReader::new(response.bytes_stream().map_err(io::Error::other));
62        read_inventory_response(status, declared, body, cancel).await
63    };
64    tokio::select! {
65        biased;
66        _=cancel.cancelled()=>Err(io::Error::new(io::ErrorKind::Interrupted,"coverage cancelled")),
67        result=tokio::time::timeout(Duration::from_secs(15),operation)=>result.map_err(|_|io::Error::new(io::ErrorKind::TimedOut,"coverage timed out"))?,
68    }
69}