kache 1.2.0

Compiler cache for Rust, C/C++ and CUDA, with S3, GCS, shared-filesystem and OCI remotes.
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
//! OCI Distribution storage. Each object is a single-layer artifact. Ordinary
//! keys are encoded in tags for cheap listing; long keys use hashed tags and
//! manifest annotations. Reads validate the manifest's original key binding.

use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::time::Instant;

use anyhow::{Context, Result};
use async_trait::async_trait;
use base64::Engine;
use bytes::Bytes;
use docker_credential::{CredentialRetrievalError, DockerCredential};
use oci_client::client::{ClientConfig, ClientProtocol, Config, ImageLayer};
use oci_client::errors::{OciDistributionError, OciErrorCode};
use oci_client::manifest::{OciDescriptor, OciImageManifest};
use oci_client::secrets::RegistryAuth;
use oci_client::token_cache::RegistryOperation;
use oci_client::{Client, Reference};
use opendal::{Error, ErrorKind};
use sha2::{Digest, Sha256};
use tokio::io::{AsyncWrite, AsyncWriteExt};

use super::download_memory::{BudgetedBody, DOWNLOAD_MEMORY};
use super::{GetObject, GetTransfer, RemoteBackend};
use crate::config::{OciRemoteConfig, normalize_remote_prefix};

const TAG_PREFIX: &str = "kache-v2-";
const LEGACY_TAG_PREFIX: &str = "kache-v1-";
const KEY_ANNOTATION: &str = "ninja.kunobi.kache.key";
const ARTIFACT_TYPE: &str = "application/vnd.kache.cache-object.v1";
const EMPTY_CONFIG_TYPE: &str = "application/vnd.oci.empty.v1+json";
const PAGE_SIZE: usize = 100;
const MAX_REGISTRY_METADATA_BYTES: usize = 8 << 20;
const MAX_EMBEDDED_BYTES: usize = 16 << 10;
// 87 binary bytes encode to 116 characters; the tag prefix adds 11.
const MAX_TAG_KEY_BYTES: usize = 87;

mod auth;
mod transport;

pub(super) struct OciBackend {
    repository: Reference,
    transport: transport::Transport,
    base_url: String,
}

impl OciBackend {
    pub(super) async fn new(config: &OciRemoteConfig) -> Result<Self> {
        let repository = config.reference()?;
        let source = auth::CredentialSource::from_environment()?;
        // Validate the provider before starting a daemon with unusable auth.
        source.load(repository.resolve_registry()).await?;
        Self::with_credentials(config, source)
    }

    #[cfg(test)]
    fn with_auth(config: &OciRemoteConfig, auth: RegistryAuth) -> Result<Self> {
        Self::with_credentials(config, auth::CredentialSource::Fixed(auth))
    }

    fn with_credentials(
        config: &OciRemoteConfig,
        credentials: auth::CredentialSource,
    ) -> Result<Self> {
        super::ensure_rustls_provider();
        let repository = config.reference()?;
        let transport = transport::Transport::new(config, credentials)?;
        let base_url = format!(
            "{}://{}/v2/{}",
            if config.insecure { "http" } else { "https" },
            repository.resolve_registry(),
            repository.repository()
        );
        Ok(Self {
            repository,
            transport,
            base_url,
        })
    }

    async fn object_manifest(
        &self,
        key: &str,
    ) -> Result<Option<(OciImageManifest, Option<String>)>> {
        if let Some(manifest) = self.manifest(&object_tag(key)).await? {
            return Ok(Some(manifest));
        }
        self.manifest(&legacy_object_tag(key)).await
    }

    async fn manifest(&self, tag: &str) -> Result<Option<(OciImageManifest, Option<String>)>> {
        let Some((body, digest)) = self
            .metadata_get(&format!("{}/manifests/{tag}", self.base_url))
            .await?
        else {
            return Ok(None);
        };
        let actual = ImageLayer::new(body.clone(), String::new(), None).sha256_digest();
        if digest.is_some_and(|digest| digest != actual) {
            return Err(
                Error::new(ErrorKind::RangeNotSatisfied, "OCI manifest digest mismatch").into(),
            );
        }
        let mut value: serde_json::Value =
            serde_json::from_slice(&body).context("invalid OCI cache manifest")?;
        let data = value
            .get_mut("layers")
            .and_then(serde_json::Value::as_array_mut)
            .and_then(|layers| layers.first_mut())
            .and_then(|layer| layer.as_object_mut())
            .and_then(|layer| layer.remove("data"))
            .map(serde_json::from_value::<String>)
            .transpose()
            .context("invalid OCI embedded data")?;
        Ok(Some((
            serde_json::from_value(value).context("invalid OCI cache manifest")?,
            data,
        )))
    }

    /// Metadata has a total request deadline and a bounded response body.
    async fn metadata_get(&self, url: &str) -> Result<Option<(Bytes, Option<String>)>> {
        let mut response = self
            .transport
            .request(
                reqwest::Method::GET,
                url,
                RegistryOperation::Pull,
                None,
                None,
                true,
            )
            .await
            .context("reading OCI registry metadata")?;
        let status = response.status().as_u16();
        if status == 404 {
            return Ok(None);
        }
        if status != 200 {
            return Err(registry_error(OciDistributionError::ServerError {
                code: status,
                url: url.to_string(),
                message: "metadata request failed".to_string(),
            }));
        }
        let digest = response
            .headers()
            .get("docker-content-digest")
            .map(|value| value.to_str().map(str::to_string))
            .transpose()?;
        if response
            .content_length()
            .is_some_and(|size| size > MAX_REGISTRY_METADATA_BYTES as u64)
        {
            anyhow::bail!("OCI registry metadata too large");
        }
        let mut body = Vec::new();
        while let Some(chunk) = response
            .chunk()
            .await
            .context("reading OCI registry metadata body")?
        {
            if chunk.len() > MAX_REGISTRY_METADATA_BYTES - body.len() {
                anyhow::bail!("OCI registry metadata too large");
            }
            body.extend_from_slice(&chunk);
        }
        Ok(Some((body.into(), digest)))
    }
}

fn client_config(insecure: bool) -> ClientConfig {
    ClientConfig {
        protocol: if insecure {
            ClientProtocol::Http
        } else {
            ClientProtocol::Https
        },
        connect_timeout: Some(super::CONNECT_TIMEOUT),
        read_timeout: Some(super::READ_INACTIVITY_TIMEOUT),
        ..Default::default()
    }
}

fn check_list_limits(entries: usize, key_bytes: usize) -> Result<()> {
    if entries > super::LIST_MAX_ENTRIES || key_bytes > super::LIST_MAX_KEY_BYTES {
        anyhow::bail!("OCI LIST exceeded limits");
    }
    Ok(())
}

fn object_tag(key: &str) -> String {
    // OCI permits 128-byte tags, shorter than most hex-digest object paths.
    // Encode 64-character lowercase hex runs as 32 bytes with a NUL marker.
    // Valid keys cannot contain NUL, so the transformation is reversible.
    // Ordinary Kache keys fit and LIST can recover them from tags alone.
    let mut encoded = Vec::new();
    let mut rest = key.as_bytes();
    while !rest.is_empty() {
        if rest.len() >= 64 && rest[..64].iter().all(|byte| is_lower_hex(*byte)) {
            encoded.push(0);
            encoded.extend_from_slice(&hex::decode(&rest[..64]).expect("validated hex run"));
            rest = &rest[64..];
        } else {
            encoded.push(rest[0]);
            rest = &rest[1..];
        }
    }
    if encoded.len() <= MAX_TAG_KEY_BYTES {
        format!(
            "{TAG_PREFIX}b-{}",
            base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(encoded)
        )
    } else {
        format!("{TAG_PREFIX}h-{}", blake3::hash(key.as_bytes()).to_hex())
    }
}

fn legacy_object_tag(key: &str) -> String {
    format!(
        "{LEGACY_TAG_PREFIX}{}",
        blake3::hash(key.as_bytes()).to_hex()
    )
}

fn tag_key(tag: &str) -> Result<Option<String>> {
    let Some(encoded) = tag.strip_prefix(&format!("{TAG_PREFIX}b-")) else {
        return Ok(None);
    };
    // The codec emits at most 127 bytes, below OCI's 128-byte ceiling.
    if tag.len() > 127 {
        anyhow::bail!("OCI object tag too long");
    }
    let bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD
        .decode(encoded)
        .context("invalid OCI object tag")?;
    let mut key = Vec::new();
    let mut rest = bytes.as_slice();
    while !rest.is_empty() {
        if rest[0] == 0 {
            if rest.len() < 33 {
                anyhow::bail!("truncated OCI object tag digest");
            }
            key.extend_from_slice(hex::encode(&rest[1..33]).as_bytes());
            rest = &rest[33..];
        } else {
            key.push(rest[0]);
            rest = &rest[1..];
        }
    }
    let key = String::from_utf8(key).context("invalid OCI object tag key")?;
    validate_key(&key)?;
    if object_tag(&key) != tag {
        anyhow::bail!("noncanonical OCI object tag");
    }
    Ok(Some(key))
}

fn is_lower_hex(byte: u8) -> bool {
    byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)
}

fn validate_key(key: &str) -> Result<()> {
    if key.is_empty()
        || key.len() > 4096
        || key.chars().any(char::is_control)
        || normalize_remote_prefix(key)? != key
    {
        anyhow::bail!("invalid OCI cache key {key:?}");
    }
    Ok(())
}

fn validate_blob_digest(digest: &str) -> Result<()> {
    let Some(hash) = digest.strip_prefix("sha256:") else {
        anyhow::bail!("unsupported OCI object digest");
    };
    if hash.len() != 64 || !hash.bytes().all(is_lower_hex) {
        anyhow::bail!("invalid OCI object digest");
    }
    Ok(())
}

fn verify_object_digest(expected: &str, actual: &[u8]) -> Result<()> {
    validate_blob_digest(expected)?;
    if format!("sha256:{}", hex::encode(actual)) != expected {
        return Err(Error::new(ErrorKind::RangeNotSatisfied, "OCI object digest mismatch").into());
    }
    Ok(())
}

fn object_descriptor<'a>(manifest: &'a OciImageManifest, key: &str) -> Result<&'a OciDescriptor> {
    let annotated = manifest
        .annotations
        .as_ref()
        .and_then(|map| map.get(KEY_ANNOTATION));
    if manifest.schema_version != 2
        || manifest.artifact_type.as_deref() != Some(ARTIFACT_TYPE)
        || annotated.map(String::as_str) != Some(key)
        || manifest.layers.len() != 1
    {
        anyhow::bail!("invalid OCI cache artifact for {key:?}");
    }
    let descriptor = &manifest.layers[0];
    u64::try_from(descriptor.size).context("negative OCI object size")?;
    Ok(descriptor)
}

fn credentials(
    result: std::result::Result<DockerCredential, CredentialRetrievalError>,
) -> Result<RegistryAuth> {
    match result {
        Ok(DockerCredential::UsernamePassword(username, password)) => {
            Ok(RegistryAuth::Basic(username, password))
        }
        Ok(DockerCredential::IdentityToken(token)) => Ok(RegistryAuth::Bearer(token)),
        Err(CredentialRetrievalError::NoCredentialConfigured) => Ok(RegistryAuth::Anonymous),
        // Helper output can contain credentials. Do not include it in errors.
        Err(_) => Err(Error::new(
            ErrorKind::PermissionDenied,
            "cannot read OCI credentials from Docker config or credential helper",
        )
        .into()),
    }
}

fn load_credentials(directory: &std::path::Path, registry: &str) -> Result<RegistryAuth> {
    let file = match std::fs::File::open(directory.join("config.json")) {
        Ok(file) => file,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            return Ok(RegistryAuth::Anonymous);
        }
        Err(error) => return Err(error).context("opening Docker credential config"),
    };
    credentials(docker_credential::get_credential_from_reader(
        std::io::BufReader::new(file),
        registry,
    ))
}

/// Preserve typed transport failures for Kache's existing retry and auth policy.
fn registry_error(error: OciDistributionError) -> anyhow::Error {
    let error = match error {
        OciDistributionError::RequestError(error) => return error.into(),
        OciDistributionError::IoError(error) => return error.into(),
        error => error,
    };
    let kind = match &error {
        OciDistributionError::ImageManifestNotFoundError(_) => ErrorKind::NotFound,
        OciDistributionError::AuthenticationFailure(_)
        | OciDistributionError::UnauthorizedError { .. } => ErrorKind::PermissionDenied,
        OciDistributionError::ServerError { code: 404, .. } => ErrorKind::NotFound,
        OciDistributionError::ServerError {
            code: 401 | 403, ..
        } => ErrorKind::PermissionDenied,
        OciDistributionError::ServerError { code: 429, .. } => ErrorKind::RateLimited,
        OciDistributionError::ServerError {
            code: 500..=599, ..
        } => {
            return Error::new(ErrorKind::Unexpected, "OCI registry unavailable")
                .set_temporary()
                .set_source(error)
                .into();
        }
        OciDistributionError::RegistryError { envelope, .. } => {
            if envelope.errors.iter().any(|error| {
                matches!(
                    error.code,
                    OciErrorCode::Unauthorized | OciErrorCode::Denied
                )
            }) {
                ErrorKind::PermissionDenied
            } else if !envelope.errors.is_empty()
                && envelope.errors.iter().all(|error| {
                    matches!(
                        error.code,
                        OciErrorCode::ManifestUnknown
                            | OciErrorCode::NameUnknown
                            | OciErrorCode::BlobUnknown
                            | OciErrorCode::NotFound
                    )
                })
            {
                ErrorKind::NotFound
            } else if envelope
                .errors
                .iter()
                .any(|error| error.code == OciErrorCode::Toomanyrequests)
            {
                ErrorKind::RateLimited
            } else {
                ErrorKind::Unexpected
            }
        }
        OciDistributionError::DigestError(_) => ErrorKind::RangeNotSatisfied,
        _ => ErrorKind::ConfigInvalid,
    };
    Error::new(kind, "OCI registry request failed")
        .set_source(error)
        .into()
}

#[async_trait]
impl RemoteBackend for OciBackend {
    async fn head(&self, key: &str) -> Result<bool> {
        validate_key(key)?;
        let Some((manifest, _)) = self.object_manifest(key).await? else {
            return Ok(false);
        };
        object_descriptor(&manifest, key)?;
        Ok(true)
    }

    async fn get(&self, key: &str, max_bytes: Option<u64>) -> Result<Option<GetObject>> {
        let memory = DOWNLOAD_MEMORY.acquire(max_bytes).await?;
        let mut body = Vec::new();
        let Some(transfer) = self.get_into(key, max_bytes, &mut body).await? else {
            return Ok(None);
        };
        Ok(Some(GetObject {
            body: Bytes::from_owner(BudgetedBody {
                body: Bytes::from(body),
                _memory: memory,
            }),
            request_ms: transfer.request_ms,
            body_ms: transfer.body_ms,
        }))
    }

    async fn get_into(
        &self,
        key: &str,
        max_bytes: Option<u64>,
        destination: &mut (dyn AsyncWrite + Unpin + Send),
    ) -> Result<Option<GetTransfer>> {
        validate_key(key)?;
        let started = Instant::now();
        let Some((manifest, embedded)) = self.object_manifest(key).await? else {
            return Ok(None);
        };
        let descriptor = object_descriptor(&manifest, key)?;
        let expected = u64::try_from(descriptor.size)?;
        if max_bytes.is_some_and(|limit| expected > limit) {
            anyhow::bail!("OCI object {key:?} too large: {expected} bytes");
        }
        if let Some(embedded) = embedded {
            if expected > MAX_EMBEDDED_BYTES as u64
                || embedded.len() > MAX_EMBEDDED_BYTES.div_ceil(3) * 4
            {
                anyhow::bail!("OCI embedded object too large");
            }
            let body = base64::engine::general_purpose::STANDARD
                .decode(embedded)
                .context("invalid OCI embedded data")?;
            super::verify_complete_body(Some(expected), body.len() as u64, &self.describe(key))?;
            verify_object_digest(&descriptor.digest, &Sha256::digest(&body))?;
            let request_ms = started.elapsed().as_millis() as u64;
            let started = Instant::now();
            destination
                .write_all(&body)
                .await
                .context("writing OCI embedded object")?;
            destination
                .flush()
                .await
                .context("flushing OCI embedded object")?;
            return Ok(Some(GetTransfer {
                bytes: expected,
                request_ms,
                body_ms: started.elapsed().as_millis() as u64,
            }));
        }
        // Objects published by Kache use SHA-256. Reject a malformed or
        // unsupported digest before constructing the repository blob URL.
        validate_blob_digest(&descriptor.digest)?;
        // Only use the repository digest. Descriptor URLs must not redirect
        // cache reads to a different host.
        let mut response = self
            .transport
            .request(
                reqwest::Method::GET,
                &format!("{}/blobs/{}", self.base_url, descriptor.digest),
                RegistryOperation::Pull,
                None,
                None,
                false,
            )
            .await
            .context("reading OCI object")?;
        transport::require_status(&response, 200)?;
        if max_bytes
            .zip(response.content_length())
            .is_some_and(|(limit, length)| length > limit)
        {
            anyhow::bail!("OCI object {key:?} advertised body too large");
        }
        let request_ms = started.elapsed().as_millis() as u64;
        let started = Instant::now();
        let mut length = 0_u64;
        let mut digest = Sha256::new();
        while let Some(chunk) = response.chunk().await.context("reading OCI object body")? {
            length = length
                .checked_add(chunk.len() as u64)
                .context("OCI object size overflow")?;
            if max_bytes.is_some_and(|limit| length > limit) {
                anyhow::bail!("OCI object {key:?} streamed body too large");
            }
            digest.update(&chunk);
            destination
                .write_all(&chunk)
                .await
                .context("writing OCI object body")?;
        }
        super::verify_complete_body(Some(expected), length, &self.describe(key))?;
        verify_object_digest(&descriptor.digest, &digest.finalize())?;
        destination
            .flush()
            .await
            .context("flushing OCI object body")?;
        Ok(Some(GetTransfer {
            bytes: length,
            request_ms,
            body_ms: started.elapsed().as_millis() as u64,
        }))
    }

    async fn put(&self, key: &str, body: Vec<u8>, content_type: Option<&str>) -> Result<()> {
        validate_key(key)?;
        let layer = ImageLayer::new(
            body,
            content_type
                .unwrap_or("application/octet-stream")
                .to_string(),
            Some(BTreeMap::from([(
                "org.opencontainers.image.title".to_string(),
                "object".to_string(),
            )])),
        );
        let config = Config::new(b"{}".to_vec(), EMPTY_CONFIG_TYPE.to_string(), None);
        let mut manifest = OciImageManifest::build(
            std::slice::from_ref(&layer),
            &config,
            Some(BTreeMap::from([(
                KEY_ANNOTATION.to_string(),
                key.to_string(),
            )])),
        );
        manifest.artifact_type = Some(ARTIFACT_TYPE.to_string());
        let mut value = serde_json::to_value(&manifest)?;
        if content_type == Some("application/json") && layer.data.len() <= MAX_EMBEDDED_BYTES {
            value["layers"][0]["data"] = base64::engine::general_purpose::STANDARD
                .encode(&layer.data)
                .into();
        }
        self.transport
            .put_blob(layer.data, &manifest.layers[0].digest)
            .await?;
        self.transport
            .ensure_config(config.data, &manifest.config.digest)
            .await?;
        let body: Bytes = serde_json::to_vec(&value)?.into();
        let digest = ImageLayer::new(body.clone(), String::new(), None).sha256_digest();
        let response = self
            .transport
            .request(
                reqwest::Method::PUT,
                &format!("{}/manifests/{}", self.base_url, object_tag(key)),
                RegistryOperation::Push,
                Some(body),
                Some("application/vnd.oci.image.manifest.v1+json"),
                true,
            )
            .await
            .with_context(|| format!("publishing OCI object {}", self.describe(key)))?;
        transport::require_status(&response, 201)?;
        transport::verify_digest_header(&response, &digest)?;
        Ok(())
    }

    // OCI Distribution does not guarantee conditional tag writes. The trait's
    // Unsupported defaults avoid pretending HEAD followed by PUT is atomic.

    async fn list(&self, prefix: &str) -> Result<Vec<String>> {
        let mut keys = BTreeSet::new();
        let mut seen = HashSet::new();
        let mut key_bytes = 0_usize;
        let mut last: Option<String> = None;
        let started = Instant::now();
        loop {
            let remaining = super::LIST_TOTAL_TIMEOUT
                .checked_sub(started.elapsed())
                .context("OCI LIST exceeded total deadline")?;
            let mut url = reqwest::Url::parse(&format!("{}/tags/list", self.base_url))?;
            url.query_pairs_mut()
                .append_pair("n", &PAGE_SIZE.to_string());
            if let Some(last) = &last {
                url.query_pairs_mut().append_pair("last", last);
            }
            let Some((body, _)) = tokio::time::timeout(remaining, self.metadata_get(url.as_str()))
                .await
                .context("OCI LIST exceeded total deadline")??
            else {
                break;
            };
            let page: oci_client::client::TagResponse =
                serde_json::from_slice(&body).context("invalid OCI tag page")?;
            if page.tags.is_empty() {
                break;
            }
            for tag in &page.tags {
                if !seen.insert(tag.clone()) {
                    anyhow::bail!("OCI LIST returned duplicate tag {tag:?}");
                }
                key_bytes = key_bytes.saturating_add(tag.len());
                check_list_limits(seen.len(), key_bytes)?;
                let legacy = tag.starts_with(LEGACY_TAG_PREFIX);
                if !legacy && !tag.starts_with(TAG_PREFIX) {
                    continue;
                }
                if let Some(key) = tag_key(tag)? {
                    key_bytes = key_bytes.saturating_add(key.len());
                    check_list_limits(seen.len(), key_bytes)?;
                    if key.starts_with(prefix) {
                        keys.insert(key);
                    }
                    continue;
                }
                if !legacy && !tag.starts_with(&format!("{TAG_PREFIX}h-")) {
                    anyhow::bail!("invalid OCI object tag");
                }
                let remaining = super::LIST_TOTAL_TIMEOUT
                    .checked_sub(started.elapsed())
                    .context("OCI LIST exceeded total deadline")?;
                let Some((manifest, _)) = tokio::time::timeout(remaining, self.manifest(tag))
                    .await
                    .context("OCI LIST exceeded total deadline")??
                else {
                    continue;
                };
                let key = manifest
                    .annotations
                    .as_ref()
                    .and_then(|map| map.get(KEY_ANNOTATION))
                    .context("OCI cache artifact missing key annotation")?;
                validate_key(key)?;
                let expected_tag = if legacy {
                    legacy_object_tag(key)
                } else {
                    object_tag(key)
                };
                if expected_tag != *tag {
                    anyhow::bail!("OCI cache tag does not match its key annotation");
                }
                object_descriptor(&manifest, key)?;
                if key.starts_with(prefix) {
                    keys.insert(key.clone());
                }
                key_bytes = key_bytes.saturating_add(key.len());
                check_list_limits(seen.len(), key_bytes)?;
            }
            // Fetch again even for a short page: registries may use a smaller
            // page size than requested.
            last = page.tags.last().cloned();
        }
        Ok(keys.into_iter().collect())
    }

    fn describe(&self, key: &str) -> String {
        format!(
            "oci://{}/{}",
            self.repository.whole().trim_end_matches(":latest"),
            key
        )
    }
}

#[cfg(test)]
mod tests;