tensorlake 0.5.161

A Rust SDK for interacting with Tensorlake Cloud APIs.
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
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
//! The merged sandbox-image catalog: one client-side view over the platform
//! sandbox-template registry (legacy `.tlsnap` images) and the CAS image registry
//! catalog (CAS images), so `describe`, `ls`, `rm` and the SDK lookups behave
//! identically for both families.
//!
//! Dispatch is by reference shape: a `sandbox_template_` id is a platform
//! template, an `image_` id is a CAS image, anything else is a
//! name. A name is looked up in the CAS image registry first (when it fits that
//! catalog's grammar) and only a clean 404 falls through to the platform
//! registry; any other failure is reported, never mistaken for a miss. Single
//! image operations never list and match. Listings concatenate both catalogs
//! and sort newest first, then by name, with CAS images ahead of legacy ones
//! on a tie.
//!
//! The CAS image registry answers the platform template shape (snake_case, RFC 3339
//! timestamps); this module maps it onto [`SandboxTemplate`] so callers see
//! one record kind for both families.

use std::{collections::BTreeMap, time::Duration};

use chrono::{DateTime, TimeZone, Utc};
use reqwest::{Method, StatusCode};
use serde::{Deserialize, Serialize};
use tokio::time::Instant;

use crate::{
    Traced,
    client::Client,
    error::SdkError,
    image_builds::{DEFAULT_WAIT_LIMIT, ImageBuildError, InterruptSignal, new_client_request_id},
    sandbox_images::{SandboxImageBuildEvent, cas_image_service_url, client_builder},
    sandbox_templates::{SandboxTemplatesClient, models::SandboxTemplate},
};

/// Prefix of a CAS image id.
pub const IMAGE_SERVICE_ID_PREFIX: &str = "image_";
/// Prefix of a platform sandbox-template id.
pub const SANDBOX_TEMPLATE_ID_PREFIX: &str = "sandbox_template_";
/// The snapshot format version of a CAS (content-addressed streaming) image.
pub const CAS_SNAPSHOT_FORMAT_VERSION: &str = "content_addressed_streaming_v1";
/// The retired immutable reference scheme. Images are addressed by name or
/// `image_` id; a digest reference is an error, not a lookup.
const CAS_V1_REFERENCE_PREFIX: &str = "cas-v1:";
const IMAGE_SERVICE_LIST_PAGE_SIZE: &str = "100";

/// What [`SandboxImageCatalog::delete_reporting`] deleted.
#[derive(Debug)]
pub struct ImageDeletion {
    /// The family of the image the reference named.
    pub family: DeletedImageFamily,
    /// Set when a name's CAS image was deleted and a legacy image of the
    /// same name existed.
    pub same_named_legacy: Option<LegacyImageDeletion>,
}

impl ImageDeletion {
    fn only(family: DeletedImageFamily) -> Self {
        Self {
            family,
            same_named_legacy: None,
        }
    }

    /// The same-named legacy image this delete left behind and could not
    /// remove: its name, its `sandbox_template_` id when known, and why.
    /// `None` when there was none or it was deleted. Callers surface it as
    /// a warning; the delete itself succeeded.
    pub fn failed_legacy_cleanup(&self) -> Option<(&str, Option<&str>, &SdkError)> {
        match &self.same_named_legacy {
            Some(LegacyImageDeletion::Failed { name, id, error }) => {
                Some((name, id.as_deref(), error))
            }
            _ => None,
        }
    }
}

/// The catalog an image was deleted from.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeletedImageFamily {
    /// A CAS image, deleted through the CAS image registry.
    Cas,
    /// A legacy image, deleted from the platform template registry.
    Legacy,
}

/// The legacy image a name's CAS delete left behind, which can no longer
/// start sandboxes: the one named exactly like the deleted CAS image. `id`
/// is its `sandbox_template_` id when known.
#[derive(Debug)]
pub enum LegacyImageDeletion {
    Deleted {
        name: String,
        id: Option<String>,
    },
    /// Not deleted; deleting the name or id again finishes the job.
    Failed {
        name: String,
        id: Option<String>,
        error: SdkError,
    },
}

/// How a user-supplied image reference is dispatched.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ImageReference {
    /// A platform sandbox-template id.
    TemplateId(String),
    /// A CAS image id.
    ImageServiceId(String),
    /// A registered name, resolvable in either catalog.
    Name(String),
}

/// Classify `reference` by its shape. Content digests (`cas-v1:<digest>` or
/// a bare 64-hex digest) are rejected: they are not names and no catalog
/// resolves them.
pub fn classify_image_reference(reference: &str) -> Result<ImageReference, SdkError> {
    let reference = reference.trim();
    if reference.is_empty() {
        return Err(SdkError::ClientError(
            "image reference must not be empty".to_string(),
        ));
    }
    if reference.starts_with(CAS_V1_REFERENCE_PREFIX) || is_lower_sha256_hex(reference) {
        return Err(SdkError::ClientError(format!(
            "'{reference}' is a content digest, not an image reference; use the image name or \
             its image_ id"
        )));
    }
    if reference.starts_with(SANDBOX_TEMPLATE_ID_PREFIX) {
        return Ok(ImageReference::TemplateId(reference.to_string()));
    }
    if reference.starts_with(IMAGE_SERVICE_ID_PREFIX) {
        return Ok(ImageReference::ImageServiceId(reference.to_string()));
    }
    Ok(ImageReference::Name(reference.to_string()))
}

pub(crate) fn is_lower_sha256_hex(value: &str) -> bool {
    value.len() == 64
        && value
            .bytes()
            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
}

/// Whether `reference` is a CAS image id (`image_<lowercase alnum>`).
pub fn is_image_service_id(reference: &str) -> bool {
    reference
        .strip_prefix(IMAGE_SERVICE_ID_PREFIX)
        .is_some_and(|rest| {
            !rest.is_empty()
                && rest
                    .bytes()
                    .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit())
        })
}

/// A name a build, import or registration may claim: names carrying a
/// reserved id prefix could never be addressed, since clients dispatch an
/// identifier to a catalog by that prefix, and digest references are not
/// names at all.
pub fn validate_registered_name(name: &str) -> Result<(), String> {
    if name.is_empty() {
        return Err("image name must not be empty".to_string());
    }
    for reserved in [SANDBOX_TEMPLATE_ID_PREFIX, IMAGE_SERVICE_ID_PREFIX] {
        if name.starts_with(reserved) {
            return Err(format!(
                "image name must not start with the reserved id prefix {reserved:?}"
            ));
        }
    }
    if name.starts_with(CAS_V1_REFERENCE_PREFIX) {
        return Err("image name must not be a cas-v1 reference".to_string());
    }
    Ok(())
}

/// The CAS image registry name grammar, mirrored client-side so a name that
/// catalog could never hold (uppercase, reserved prefixes, odd separators) is
/// routed straight to the legacy image registry instead of producing a 400 that a
/// caller could mistake for a miss. Lowercase repository components joined by
/// `/`, each made of alphanumeric runs joined by single `.`, `_` or `-`
/// separators, with an optional `:tag`.
pub fn validate_image_service_name(name: &str) -> Result<(), String> {
    validate_registered_name(name)?;
    if name.len() > 255 {
        return Err("image name must be at most 255 characters".to_string());
    }
    let (repository, tag) = match name.split_once(':') {
        Some((repository, tag)) => (repository, Some(tag)),
        None => (name, None),
    };
    for component in repository.split('/') {
        if !is_valid_name_component(component) {
            return Err(format!(
                "name component {component:?} must be lowercase alphanumeric segments joined by \
                 single '.', '_' or '-' separators"
            ));
        }
    }
    if let Some(tag) = tag {
        let valid_tag = !tag.is_empty()
            && tag.len() <= 128
            && tag.bytes().all(|b| {
                b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'_' | b'.' | b'-')
            })
            && !tag.starts_with('.')
            && !tag.starts_with('-');
        if !valid_tag {
            return Err(format!("invalid tag {tag:?}"));
        }
    }
    Ok(())
}

fn is_valid_name_component(component: &str) -> bool {
    if component.is_empty() {
        return false;
    }
    let mut previous_was_separator = true; // rejects a leading separator
    for byte in component.bytes() {
        let is_separator = matches!(byte, b'.' | b'_' | b'-');
        if is_separator {
            if previous_was_separator {
                return false;
            }
        } else if !(byte.is_ascii_lowercase() || byte.is_ascii_digit()) {
            return false;
        }
        previous_was_separator = is_separator;
    }
    !previous_was_separator
}

/// A CAS image name as the CAS image registry stores it. The registry
/// ASCII-lowercases every name it creates, looks up or deletes, so
/// `MyProject` and `myproject` name one CAS image. Legacy image names are
/// matched exactly and are never canonicalized.
pub fn canonical_cas_image_name(name: &str) -> String {
    name.to_ascii_lowercase()
}

/// Derive a valid CAS image name from a generated default `raw` (a
/// Dockerfile stem, its parent directory or an imported image's last path
/// segment). The name is ASCII-lowercased and kept if that makes it valid.
/// Otherwise every character other than `a-z`, `0-9`, `.`, `_` and `-`
/// becomes `-`, each run of separators collapses to its first character,
/// leading and trailing separators are dropped and the result is capped at
/// 255 characters. `fallback` is used when no valid name remains. The
/// result is unchanged by the registry's own canonicalization. The Python
/// and TypeScript SDKs apply the same rule.
pub fn cas_image_name_from(raw: &str, fallback: &str) -> String {
    let lowered = canonical_cas_image_name(raw);
    if is_addressable_cas_image_name(&lowered) {
        return lowered;
    }
    let mut name = String::with_capacity(lowered.len());
    for ch in lowered.chars() {
        let ch = match ch {
            ch @ ('a'..='z' | '0'..='9' | '.' | '_' | '-') => ch,
            _ => '-',
        };
        if is_cas_name_separator(ch) && name.ends_with(is_cas_name_separator) {
            continue;
        }
        name.push(ch);
    }
    let mut name = name.trim_matches(is_cas_name_separator).to_string();
    name.truncate(MAX_CAS_IMAGE_NAME_LEN);
    let name = name.trim_end_matches(is_cas_name_separator);
    if is_addressable_cas_image_name(name) {
        name.to_string()
    } else {
        fallback.to_string()
    }
}

const MAX_CAS_IMAGE_NAME_LEN: usize = 255;

fn is_cas_name_separator(ch: char) -> bool {
    matches!(ch, '.' | '_' | '-')
}

/// Valid in the CAS image registry and not mistaken for a content digest
/// when used as a reference.
fn is_addressable_cas_image_name(name: &str) -> bool {
    validate_image_service_name(name).is_ok() && !is_lower_sha256_hex(name)
}

/// Whether a template record describes a CAS image.
pub fn is_cas_image(template: &SandboxTemplate) -> bool {
    template.snapshot_format_version.as_deref() == Some(CAS_SNAPSHOT_FORMAT_VERSION)
}

/// Parse a template's `createdAt` for ordering: RFC 3339, or an epoch number
/// in seconds, milliseconds or microseconds.
pub fn created_at_sort_key(value: Option<&str>) -> Option<DateTime<Utc>> {
    let value = value?.trim();
    if let Ok(timestamp) = DateTime::parse_from_rfc3339(value) {
        return Some(timestamp.with_timezone(&Utc));
    }
    let timestamp: f64 = value.parse().ok()?;
    let seconds = if timestamp > 1e15 {
        timestamp / 1_000_000.0
    } else if timestamp > 1e12 {
        timestamp / 1_000.0
    } else {
        timestamp
    };
    let whole_seconds = seconds.trunc() as i64;
    let nanos = ((seconds.fract() * 1_000_000_000.0).round() as u32).min(999_999_999);
    Utc.timestamp_opt(whole_seconds, nanos).single()
}

/// The merge order: newest first, then name ascending, then CAS before
/// legacy. Records without a parseable creation time sort last.
pub fn sort_merged_images(images: &mut [SandboxTemplate]) {
    images.sort_by(|left, right| {
        created_at_sort_key(right.created_at.as_deref())
            .cmp(&created_at_sort_key(left.created_at.as_deref()))
            .then_with(|| left.name.cmp(&right.name))
            .then_with(|| is_cas_image(right).cmp(&is_cas_image(left)))
    });
}

// ---------------------------------------------------------------------------
// Image Service wire types
// ---------------------------------------------------------------------------

/// One logical image as the CAS image registry answers it (ADR 0107 as amended
/// by the API trim): the identity clients key on, its visibility, its sizes
/// and its timestamps. The platform template shape callers of both image
/// families receive is derived locally by [`Self::into_template`]; the
/// service sends no digest, parent pins, publisher provenance or template
/// placeholders.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImageServiceImage {
    /// The mutable `image_` id: names the current image under `name`.
    pub id: String,
    pub name: String,
    /// The engine row the image is registered against; a rebuild changes it.
    pub snapshot_id: String,
    /// Visible to every project.
    pub global: bool,
    /// Stored CAS bytes of the content.
    pub physical_size_bytes: u64,
    /// Guest-visible size of the rootfs block device.
    pub logical_size_bytes: u64,
    pub created_at: String,
    pub updated_at: String,
}

impl ImageServiceImage {
    /// The platform template shape callers of both image families receive.
    pub fn into_template(self) -> SandboxTemplate {
        self.into()
    }
}

impl From<ImageServiceImage> for SandboxTemplate {
    fn from(image: ImageServiceImage) -> Self {
        // Every Image Service record is content-addressed by construction;
        // the handle, format label, node kind and the legacy template
        // placeholders are supplied here so the merged catalog, `is_cas_image`
        // and the CAS-first sort keep one record shape. No `imageId` or
        // `buildId` extras: the service exposes neither.
        SandboxTemplate {
            id: Some(image.id),
            name: Some(image.name),
            snapshot_uri: Some(format!("snapshot://{}", image.snapshot_id)),
            snapshot_id: Some(image.snapshot_id),
            snapshot_sandbox_id: None,
            snapshot_format_version: Some(CAS_SNAPSHOT_FORMAT_VERSION.to_string()),
            snapshot_size_bytes: Some(image.physical_size_bytes),
            rootfs_disk_bytes: Some(image.logical_size_bytes),
            rootfs_node_kind: Some("base".to_string()),
            public: Some(image.global),
            dockerfile: Some(String::new()),
            created_by_user_id: None,
            created_at: Some(image.created_at),
            updated_at: Some(image.updated_at),
            extra: BTreeMap::new(),
        }
    }
}

#[derive(Debug, Default, Deserialize)]
struct ImageServiceListPage {
    #[serde(default)]
    items: Vec<ImageServiceImage>,
    #[serde(default)]
    pagination: Option<ImageServicePagination>,
}

#[derive(Debug, Default, Deserialize)]
struct ImageServicePagination {
    #[serde(default)]
    next: Option<String>,
}

#[derive(Debug, Serialize)]
struct AdoptSnapshotRequest<'a> {
    name: &'a str,
    snapshot_id: &'a str,
    public: bool,
}

/// The CAS image registry error body (`{"error": "..."}`), reduced to its message
/// so callers print the service's words rather than raw JSON.
#[derive(Debug, Deserialize)]
struct ImageServiceErrorBody {
    #[serde(default)]
    error: Option<String>,
    #[serde(default)]
    message: Option<String>,
}

fn tidy_error_message(message: String) -> String {
    match serde_json::from_str::<ImageServiceErrorBody>(&message) {
        Ok(body) => body.error.or(body.message).unwrap_or(message),
        Err(_) => message,
    }
}

fn tidy_error(error: SdkError) -> SdkError {
    match error {
        SdkError::ServerError { status, message } => SdkError::ServerError {
            status,
            message: tidy_error_message(message),
        },
        SdkError::Authentication(message) => SdkError::Authentication(tidy_error_message(message)),
        SdkError::Authorization(message) => SdkError::Authorization(tidy_error_message(message)),
        other => other,
    }
}

/// The CAS image registry half of the catalog, reached through the authenticated
/// `/images/v4` ingress path of the API host (or `TENSORLAKE_IMAGE_SERVICE_URL`
/// for local development, which then includes the service's `/v1` prefix).
#[derive(Clone)]
pub struct ImageServiceCatalog {
    client: Client,
}

impl ImageServiceCatalog {
    /// Share `client`'s credentials and scope headers, rebased onto the CAS
    /// image registry URL derived from `api_url`.
    pub fn new(client: &Client, api_url: &str) -> Self {
        Self {
            client: client.with_base_url(&cas_image_service_url(api_url)),
        }
    }

    /// Every image visible to the project, following the opaque cursor to
    /// the end of the collection.
    pub async fn list_all(&self) -> Result<Vec<SandboxTemplate>, SdkError> {
        let mut cursor: Option<String> = None;
        let mut items = Vec::new();
        loop {
            let mut query: Vec<(&str, &str)> = vec![("page_size", IMAGE_SERVICE_LIST_PAGE_SIZE)];
            if let Some(cursor) = cursor.as_deref() {
                query.push(("cursor", cursor));
            }
            let request = self
                .client
                .build_get_json_request("/images", Some(&query))?;
            let page = self
                .client
                .execute_json::<ImageServiceListPage>(request)
                .await
                .map_err(tidy_error)?
                .into_inner();
            items.extend(page.items.into_iter().map(SandboxTemplate::from));
            match page
                .pagination
                .and_then(|pagination| pagination.next)
                .filter(|next| !next.is_empty())
            {
                Some(next) => cursor = Some(next),
                None => return Ok(items),
            }
        }
    }

    /// The image registered under `name` in the caller's project, or a global
    /// image of that name. `Ok(None)` only on a clean 404.
    pub async fn get_by_name(&self, name: &str) -> Result<Option<SandboxTemplate>, SdkError> {
        self.get_optional(&format!("/images/name/{name}")).await
    }

    /// The image with the `image_` id `image_id`, when it is the caller's or
    /// global. `Ok(None)` only on a clean 404.
    pub async fn get_by_id(&self, image_id: &str) -> Result<Option<SandboxTemplate>, SdkError> {
        self.get_optional(&format!("/images/{image_id}")).await
    }

    async fn get_optional(&self, path: &str) -> Result<Option<SandboxTemplate>, SdkError> {
        let request = self.client.build_get_json_request(path, None)?;
        match self.client.execute_json::<ImageServiceImage>(request).await {
            Ok(traced) => Ok(Some(traced.into_inner().into())),
            Err(SdkError::ServerError { status, .. }) if status == StatusCode::NOT_FOUND => {
                Ok(None)
            }
            Err(error) => Err(tidy_error(error)),
        }
    }

    /// Delete the project's image named `name`. `Ok(false)` only on a clean
    /// 404; a 403 (the member role cannot delete) is reported as such, and
    /// so is a 409 `image_build_in_progress` (the name has an unfinished
    /// build: cancel it with the build id first; deleting never cancels).
    /// A 202 means the deletion is still in progress and counts as done.
    /// Deleting removes the published image only; a name whose only state is
    /// an unfinished first build has no image and answers 404.
    pub async fn delete_by_name(&self, name: &str) -> Result<bool, SdkError> {
        self.delete_optional(&format!("/images/name/{name}")).await
    }

    /// Delete the project's image with the `image_` id `image_id`; the same
    /// answers as [`Self::delete_by_name`].
    pub async fn delete_by_id(&self, image_id: &str) -> Result<bool, SdkError> {
        self.delete_optional(&format!("/images/{image_id}")).await
    }

    /// `Ok(true)` on any success (204 done, 202 deletion in progress),
    /// `Ok(false)` only on a clean 404. 202, 409, 5xx and transport failures
    /// are never mistaken for absence, and a lost response is never
    /// replayed here: the merged catalog decides what a miss means.
    async fn delete_optional(&self, path: &str) -> Result<bool, SdkError> {
        let request = self.client.request(Method::DELETE, path).build()?;
        match self.client.execute_traced(request).await {
            Ok(_) => Ok(true),
            Err(SdkError::ServerError { status, .. }) if status == StatusCode::NOT_FOUND => {
                Ok(false)
            }
            Err(error) => Err(tidy_error(error)),
        }
    }

    /// Register a completed CAS snapshot as the image `name` (ADR 0107
    /// D9-D12). `public` asks for global scope, which the service grants only
    /// to allowlisted publisher projects.
    ///
    /// The request is admitted under a fresh client request id
    /// (`Idempotency-Key`), retained across its transport retries. The
    /// service answers 201 with the published image, or `503
    /// publication_pending` naming the build when its bounded inline wait
    /// expired; the caller then retries the identical request under the same
    /// key ([`Self::adopt_snapshot_and_wait`] does so up to its registration
    /// wait bound). A 409 `image_build_in_progress` names the active
    /// operation to cancel first. Transport acceptance is never reported as
    /// completion.
    pub async fn adopt_snapshot(
        &self,
        name: &str,
        snapshot_id: &str,
        public: bool,
    ) -> Result<SnapshotAdoption, ImageBuildError> {
        let request_id = new_client_request_id();
        let wait = RegistrationWait::new(RegistrationWaitPolicy::default());
        self.adopt_snapshot_with_request_id(
            name,
            snapshot_id,
            public,
            &request_id,
            &wait,
            &InterruptSignal::new(),
        )
        .await
    }

    /// [`Self::adopt_snapshot`], retrying a pending registration under the
    /// same key until it publishes or the registration wait bound expires.
    /// Returns the published image in the template shape, so registration
    /// flows keep their completed-image result. `interrupt` cancels a pending
    /// Direct operation through the bounded handshake.
    pub async fn adopt_snapshot_and_wait<F>(
        &self,
        name: &str,
        snapshot_id: &str,
        public: bool,
        interrupt: &InterruptSignal,
        emit: F,
    ) -> Result<SandboxTemplate, ImageBuildError>
    where
        F: FnMut(SandboxImageBuildEvent),
    {
        self.adopt_snapshot_and_wait_with(
            name,
            snapshot_id,
            public,
            RegistrationWaitPolicy::default(),
            interrupt,
            emit,
        )
        .await
    }

    /// [`Self::adopt_snapshot_and_wait`] under an explicit wait policy. A
    /// `publication_pending` answer is progress, not unavailability: it
    /// resets the per-request reconnect budget and the loop re-POSTs the
    /// identical request under the same key after `poll_interval`, every
    /// request and sleep clamped to the absolute registration deadline, so a
    /// request still in flight at the deadline is abandoned there. Expiry is
    /// [`ImageBuildError::Timeout`] naming the pending build (or the request
    /// id while no answer named one); the operation keeps running.
    pub(crate) async fn adopt_snapshot_and_wait_with<F>(
        &self,
        name: &str,
        snapshot_id: &str,
        public: bool,
        policy: RegistrationWaitPolicy,
        interrupt: &InterruptSignal,
        mut emit: F,
    ) -> Result<SandboxTemplate, ImageBuildError>
    where
        F: FnMut(SandboxImageBuildEvent),
    {
        let request_id = new_client_request_id();
        let mut wait = RegistrationWait::new(policy);
        loop {
            match self
                .adopt_snapshot_with_request_id(
                    name,
                    snapshot_id,
                    public,
                    &request_id,
                    &wait,
                    interrupt,
                )
                .await?
            {
                SnapshotAdoption::Published(image) => return Ok((*image).into_template()),
                SnapshotAdoption::Pending { build_id } => {
                    if wait.build_id.is_none() {
                        emit(SandboxImageBuildEvent::Accepted {
                            build_id: build_id.clone(),
                            client_request_id: Some(request_id.clone()),
                        });
                    }
                    wait.build_id = Some(build_id.clone());
                    emit(SandboxImageBuildEvent::Status(format!(
                        "Registration of '{name}' pending (build {build_id}); waiting for \
                         publication..."
                    )));
                    let now = Instant::now();
                    if now >= wait.deadline {
                        return Err(wait.timeout(&request_id));
                    }
                    let delay = wait.policy.poll_interval.min(wait.deadline - now);
                    tokio::select! {
                        biased;
                        _ = interrupt.triggered() => {
                            let report = crate::image_service_builds::interruption_for_build(
                                &self.client,
                                &build_id,
                                Some(&request_id),
                            )
                            .await;
                            emit(SandboxImageBuildEvent::Interrupted(report.clone()));
                            return Err(ImageBuildError::Interrupted(report));
                        }
                        _ = tokio::time::sleep(delay) => {}
                    }
                }
            }
        }
    }

    async fn adopt_snapshot_with_request_id(
        &self,
        name: &str,
        snapshot_id: &str,
        public: bool,
        request_id: &str,
        wait: &RegistrationWait,
        interrupt: &InterruptSignal,
    ) -> Result<SnapshotAdoption, ImageBuildError> {
        let body = serde_json::to_value(AdoptSnapshotRequest {
            name,
            snapshot_id,
            public,
        })?;
        crate::image_service_builds::admit_direct(
            &self.client,
            "/images/from-snapshot",
            &body,
            request_id,
            wait,
            interrupt,
        )
        .await
    }
}

/// The answer to a direct publication: done, or admitted as an operation
/// that outlived the service's bounded inline wait.
#[derive(Debug, Clone, PartialEq)]
pub enum SnapshotAdoption {
    /// 201: the image is published.
    Published(Box<ImageServiceImage>),
    /// 503 `publication_pending`: the operation is admitted and still
    /// running. Retry the identical request under the same key to join it;
    /// `build_id` is the operation to inspect or cancel meanwhile.
    Pending { build_id: String },
}

/// How long a registration keeps retrying `publication_pending` answers
/// before giving up on waiting (the operation itself keeps running), and how
/// long it pauses between identical POSTs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RegistrationWaitPolicy {
    pub limit: Duration,
    pub poll_interval: Duration,
}

impl Default for RegistrationWaitPolicy {
    /// The same bound every build wait has without a service deadline or a
    /// caller timeout, and the build poll interval.
    fn default() -> Self {
        Self {
            limit: DEFAULT_WAIT_LIMIT,
            poll_interval: crate::image_service_builds::BUILD_POLL_INTERVAL,
        }
    }
}

/// One registration's absolute deadline and what it has learnt so far.
#[derive(Debug, Clone)]
pub(crate) struct RegistrationWait {
    pub(crate) policy: RegistrationWaitPolicy,
    pub(crate) started: Instant,
    pub(crate) deadline: Instant,
    /// The build a `publication_pending` answer named, once one did.
    pub(crate) build_id: Option<String>,
}

impl RegistrationWait {
    pub(crate) fn new(policy: RegistrationWaitPolicy) -> Self {
        let started = Instant::now();
        Self {
            policy,
            started,
            deadline: started + policy.limit,
            build_id: None,
        }
    }

    /// The timeout the wait reports: the pending build when an answer named
    /// one, otherwise the request id, so the caller can still find the
    /// operation with the cancel route's request-id selector.
    pub(crate) fn timeout(&self, request_id: &str) -> ImageBuildError {
        ImageBuildError::Timeout {
            build_id: self
                .build_id
                .clone()
                .unwrap_or_else(|| format!("(request {request_id})")),
            waited: self.started.elapsed(),
        }
    }
}

/// Both catalogs behind one lookup, delete and list.
#[derive(Clone)]
pub struct SandboxImageCatalog {
    templates: SandboxTemplatesClient,
    image_service: ImageServiceCatalog,
}

impl SandboxImageCatalog {
    /// `scope` is the explicit organization/project pair PAT callers address;
    /// API keys are self-scoping and pass `None`.
    pub fn new(client: Client, api_url: &str, scope: Option<(String, String)>) -> Self {
        let image_service = ImageServiceCatalog::new(&client, api_url);
        let templates = match scope {
            Some((organization_id, project_id)) => {
                SandboxTemplatesClient::new(client, organization_id, project_id)
            }
            None => SandboxTemplatesClient::new_api_key_scoped(client),
        };
        Self {
            templates,
            image_service,
        }
    }

    /// Build the catalog from raw credentials, the way the language bindings
    /// and the image build paths do. With `use_scope_headers` (PAT auth) the
    /// organization and project are required.
    pub fn from_credentials(
        api_url: &str,
        bearer_token: &str,
        use_scope_headers: bool,
        organization_id: Option<&str>,
        project_id: Option<&str>,
        user_agent: Option<&str>,
    ) -> Result<Self, SdkError> {
        let scope = if use_scope_headers {
            match (organization_id, project_id) {
                (Some(organization_id), Some(project_id)) => {
                    Some((organization_id.to_string(), project_id.to_string()))
                }
                _ => {
                    return Err(SdkError::ClientError(
                        "organization and project IDs are required with personal access token \
                         authentication"
                            .to_string(),
                    ));
                }
            }
        } else {
            None
        };
        let client = client_builder(
            api_url,
            bearer_token,
            use_scope_headers,
            organization_id,
            project_id,
            user_agent,
        )
        .build()?;
        Ok(Self::new(client, api_url, scope))
    }

    pub fn image_service(&self) -> &ImageServiceCatalog {
        &self.image_service
    }

    pub fn templates(&self) -> &SandboxTemplatesClient {
        &self.templates
    }

    /// Resolve one reference to its record, or `None` when neither catalog
    /// knows it. An id is tried in its own catalog first; a miss there falls
    /// through to the by-name order.
    ///
    /// Accepted merged-catalog behavior for legacy images: a name that fits
    /// the CAS image registry's grammar is looked up there first, and only a
    /// clean 404 falls through to the legacy image registry. Any other CAS
    /// image registry answer (unavailability, a deploy 5xx) fails the lookup
    /// of a legacy image name, where the released SDKs touched only the
    /// legacy image registry. A `sandbox_template_` id never consults the CAS
    /// image registry.
    pub async fn find(&self, reference: &str) -> Result<Option<SandboxTemplate>, SdkError> {
        match classify_image_reference(reference)? {
            ImageReference::TemplateId(id) => {
                if let Some(template) = self.templates.get_by_id(&id).await? {
                    return Ok(Some(template.into_inner()));
                }
                self.find_by_name(&id).await
            }
            ImageReference::ImageServiceId(id) => {
                if let Some(image) = self.image_service.get_by_id(&id).await? {
                    return Ok(Some(image));
                }
                self.find_by_name(&id).await
            }
            ImageReference::Name(name) => self.find_by_name(&name).await,
        }
    }

    /// The CAS image registry is asked for the canonical (lowercase)
    /// spelling, the legacy image registry for the spelling given, and only
    /// after a clean CAS 404.
    async fn find_by_name(&self, name: &str) -> Result<Option<SandboxTemplate>, SdkError> {
        let cas_name = canonical_cas_image_name(name);
        if validate_image_service_name(&cas_name).is_ok()
            && let Some(image) = self.image_service.get_by_name(&cas_name).await?
        {
            return Ok(Some(image));
        }
        Ok(self
            .templates
            .find_by_name(name)
            .await?
            .map(Traced::into_inner))
    }

    /// Delete one image. Not found (both catalogs miss) surfaces as a 404
    /// [`SdkError::ServerError`] so every binding reports it the same way.
    /// Never retried on a lost response: a replay could delete a same-named
    /// image in the other catalog.
    ///
    /// Accepted merged-catalog behavior for legacy images: deleting a legacy
    /// image removes it from the legacy image registry (the released SDKs
    /// only made it unusable and left it listed), and an unknown name is an
    /// error rather than a silent success. An untyped name probes the CAS
    /// image registry first and falls through to the legacy image registry
    /// only on a clean 404; a 202 (deletion in progress) is success, and a
    /// 409 `image_build_in_progress`, a 5xx or a transport failure is
    /// reported as is. Deleting removes published images only; an unfinished
    /// build is cancelled with its build id, never by deleting its name.
    ///
    /// A name is deleted from both registries: once its CAS image is
    /// deleted, a legacy image of the same name can no longer start
    /// sandboxes, so it is deleted too. The CAS image is addressed in the
    /// registry's lowercase spelling, a legacy image by the exact spelling
    /// given; see [`canonical_cas_image_name`]. Failing to delete that legacy image
    /// does not fail the call, since the CAS image is already gone;
    /// [`Self::delete_reporting`] reports it. An `image_` or
    /// `sandbox_template_` id deletes only the image it names.
    pub async fn delete(&self, reference: &str) -> Result<(), SdkError> {
        self.delete_reporting(reference).await.map(|_| ())
    }

    /// [`Self::delete`], also reporting the deleted image's family and what
    /// happened to a same-named legacy image.
    pub async fn delete_reporting(&self, reference: &str) -> Result<ImageDeletion, SdkError> {
        let deleted = match classify_image_reference(reference)? {
            ImageReference::TemplateId(id) => {
                if self.templates.delete_by_id(&id).await? {
                    Some(ImageDeletion::only(DeletedImageFamily::Legacy))
                } else {
                    self.delete_by_name(&id).await?
                }
            }
            ImageReference::ImageServiceId(id) => {
                if self.image_service.delete_by_id(&id).await? {
                    Some(ImageDeletion::only(DeletedImageFamily::Cas))
                } else {
                    self.delete_by_name(&id).await?
                }
            }
            ImageReference::Name(name) => self.delete_by_name(&name).await?,
        };
        deleted.ok_or_else(|| SdkError::ServerError {
            status: StatusCode::NOT_FOUND,
            message: format!("image '{}' not found", reference.trim()),
        })
    }

    /// Spellings as in [`Self::find_by_name`]. A deleted CAS image releases
    /// its canonical name for new sandboxes, so the legacy image cleaned up
    /// with it is the one named exactly that.
    async fn delete_by_name(&self, name: &str) -> Result<Option<ImageDeletion>, SdkError> {
        let cas_name = canonical_cas_image_name(name);
        if validate_image_service_name(&cas_name).is_ok()
            && self.image_service.delete_by_name(&cas_name).await?
        {
            return Ok(Some(ImageDeletion {
                family: DeletedImageFamily::Cas,
                same_named_legacy: self.delete_same_named_legacy_image(&cas_name).await,
            }));
        }
        Ok(self
            .templates
            .delete_by_name(name)
            .await?
            .then(|| ImageDeletion::only(DeletedImageFamily::Legacy)))
    }

    /// Delete the legacy image named `name` after its CAS namesake was
    /// deleted. `None` when there is none. A failure is reported rather
    /// than raised, because the CAS delete already succeeded.
    async fn delete_same_named_legacy_image(&self, name: &str) -> Option<LegacyImageDeletion> {
        let template = match self.templates.find_by_name(name).await {
            Ok(template) => template?.into_inner(),
            Err(error) => {
                return Some(LegacyImageDeletion::Failed {
                    name: name.to_string(),
                    id: None,
                    error,
                });
            }
        };
        let deleted = match template.id.as_deref() {
            Some(id) => self.templates.delete_by_id(id).await,
            None => self.templates.delete_by_name(name).await,
        };
        match deleted {
            Ok(true) => Some(LegacyImageDeletion::Deleted {
                name: name.to_string(),
                id: template.id,
            }),
            Ok(false) => None,
            Err(error) => Some(LegacyImageDeletion::Failed {
                name: name.to_string(),
                id: template.id,
                error,
            }),
        }
    }

    /// Every image of both families, merged and sorted (see
    /// [`sort_merged_images`]). Either catalog failing fails the listing.
    pub async fn list(&self) -> Result<Vec<SandboxTemplate>, SdkError> {
        let (templates, images) =
            tokio::try_join!(self.templates.list(), self.image_service.list_all())?;
        let mut all = templates.into_inner();
        all.extend(images);
        sort_merged_images(&mut all);
        Ok(all)
    }
}

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

    /// The CAS image naming rule's cases. The Python and TypeScript SDK
    /// tests check the same table so all three derive identical names.
    const CAS_IMAGE_NAME_CASES: &[(&str, &str)] = &[
        ("app", "app"),
        ("MyProject", "myproject"),
        ("App:V1", "app:v1"),
        ("app.v2", "app.v2"),
        ("My Project", "my-project"),
        ("my  project", "my-project"),
        ("my__app", "my_app"),
        ("-app-", "app"),
        ("._app_.", "app"),
        ("\u{dc}n\u{ef}code", "n-code"),
        ("\u{65e5}\u{672c}", "sandbox-image"),
        ("", "sandbox-image"),
        ("...", "sandbox-image"),
        ("image_foo", "sandbox-image"),
        ("sandbox_template_x", "sandbox-image"),
        (
            "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
            "sandbox-image",
        ),
    ];

    #[test]
    fn cas_image_name_from_follows_the_shared_rule() {
        for (raw, expected) in CAS_IMAGE_NAME_CASES {
            assert_eq!(
                cas_image_name_from(raw, "sandbox-image"),
                *expected,
                "{raw:?}"
            );
        }
        // The 255-character cap, then trailing separators are dropped again.
        let long = format!("{}-bc", "a".repeat(254));
        assert_eq!(cas_image_name_from(&long, "sandbox-image"), "a".repeat(254));
    }

    #[test]
    fn generated_cas_image_names_are_valid_and_already_canonical() {
        for (raw, _) in CAS_IMAGE_NAME_CASES {
            let name = cas_image_name_from(raw, "sandbox-image");
            assert!(validate_image_service_name(&name).is_ok(), "{name:?}");
            assert_eq!(canonical_cas_image_name(&name), name, "{name:?}");
            assert_eq!(
                classify_image_reference(&name).unwrap(),
                ImageReference::Name(name.clone())
            );
        }
    }

    #[test]
    fn canonical_cas_image_names_lowercase_ascii_only() {
        assert_eq!(canonical_cas_image_name("Team/App:V1"), "team/app:v1");
        assert_eq!(canonical_cas_image_name("myproject"), "myproject");
        assert_eq!(canonical_cas_image_name("\u{c9}t\u{c9}"), "\u{c9}t\u{c9}");
    }

    #[test]
    fn failed_legacy_cleanup_reports_only_a_failed_legacy_delete() {
        let failed = ImageDeletion {
            family: DeletedImageFamily::Cas,
            same_named_legacy: Some(LegacyImageDeletion::Failed {
                name: "app".to_string(),
                id: Some("sandbox_template_1".to_string()),
                error: SdkError::ServerError {
                    status: StatusCode::CONFLICT,
                    message: "retry the delete".to_string(),
                },
            }),
        };
        let (name, id, error) = failed.failed_legacy_cleanup().unwrap();
        assert_eq!(name, "app");
        assert_eq!(id, Some("sandbox_template_1"));
        assert!(error.to_string().contains("retry the delete"), "{error}");

        let deleted = ImageDeletion {
            family: DeletedImageFamily::Cas,
            same_named_legacy: Some(LegacyImageDeletion::Deleted {
                name: "app".to_string(),
                id: None,
            }),
        };
        assert!(deleted.failed_legacy_cleanup().is_none());
        assert!(
            ImageDeletion::only(DeletedImageFamily::Legacy)
                .failed_legacy_cleanup()
                .is_none()
        );
    }

    fn template(name: &str, created_at: Option<&str>, format: &str) -> SandboxTemplate {
        SandboxTemplate {
            name: Some(name.to_string()),
            created_at: created_at.map(str::to_string),
            snapshot_format_version: Some(format.to_string()),
            ..Default::default()
        }
    }

    #[test]
    fn references_dispatch_by_prefix_and_digests_are_refused() {
        assert_eq!(
            classify_image_reference("sandbox_template_abc").unwrap(),
            ImageReference::TemplateId("sandbox_template_abc".to_string())
        );
        assert_eq!(
            classify_image_reference(" image_abc123 ").unwrap(),
            ImageReference::ImageServiceId("image_abc123".to_string())
        );
        assert_eq!(
            classify_image_reference("team/app:v1").unwrap(),
            ImageReference::Name("team/app:v1".to_string())
        );
        for digest in [format!("cas-v1:{}", "a".repeat(64)), "a".repeat(64)] {
            let error = classify_image_reference(&digest).unwrap_err().to_string();
            assert!(
                error.contains("use the image name or its image_ id"),
                "{error}"
            );
        }
        assert!(classify_image_reference("  ").is_err());
    }

    #[test]
    fn reserved_prefixes_are_not_names() {
        assert!(validate_registered_name("image_x").is_err());
        assert!(validate_registered_name("sandbox_template_x").is_err());
        assert!(validate_registered_name(&format!("cas-v1:{}", "a".repeat(64))).is_err());
        assert!(validate_registered_name("MyImage").is_ok());
        assert!(validate_registered_name("team/app:v1").is_ok());
    }

    #[test]
    fn image_service_grammar_routes_only_lowercase_names() {
        for name in [
            "team/app",
            "team/app:v1.2-rc",
            "python",
            "ghcr.io/org/app:v1",
        ] {
            assert!(validate_image_service_name(name).is_ok(), "{name}");
        }
        for name in [
            "MyImage",
            "team//app",
            "-leading",
            "team/app:.tag",
            "a b",
            "image_abc",
            "sandbox_template_abc",
        ] {
            assert!(validate_image_service_name(name).is_err(), "{name}");
        }
        assert!(is_image_service_id("image_abc123"));
        assert!(!is_image_service_id("image_"));
        assert!(!is_image_service_id("image_ABC"));
        assert!(!is_image_service_id("sandbox_template_x"));
    }

    #[test]
    fn created_at_parses_rfc3339_and_epoch_numbers() {
        let rfc = created_at_sort_key(Some("2026-10-15T00:00:00.000Z")).unwrap();
        assert_eq!(rfc.timestamp(), 1_792_022_400);
        assert_eq!(
            created_at_sort_key(Some("1792022400")).unwrap().timestamp(),
            1_792_022_400
        );
        assert_eq!(
            created_at_sort_key(Some("1792022400000"))
                .unwrap()
                .timestamp(),
            1_792_022_400
        );
        assert_eq!(created_at_sort_key(Some("not a time")), None);
        assert_eq!(created_at_sort_key(None), None);
    }

    #[test]
    fn merged_listing_sorts_newest_then_name_then_cas_first() {
        let mut images = vec![
            template("zeta", Some("2026-10-14T00:00:00Z"), "durable_archive_v1"),
            template("alpha", Some("2026-10-15T00:00:00Z"), "durable_archive_v1"),
            template(
                "alpha",
                Some("2026-10-15T00:00:00Z"),
                CAS_SNAPSHOT_FORMAT_VERSION,
            ),
            template("beta", None, "durable_archive_v1"),
            template("beta", Some("2026-10-15T00:00:00Z"), "durable_archive_v1"),
        ];
        sort_merged_images(&mut images);
        let order: Vec<(String, bool)> = images
            .iter()
            .map(|image| (image.name.clone().unwrap(), is_cas_image(image)))
            .collect();
        assert_eq!(
            order,
            vec![
                ("alpha".to_string(), true),
                ("alpha".to_string(), false),
                ("beta".to_string(), false),
                ("zeta".to_string(), false),
                ("beta".to_string(), false),
            ]
        );
    }

    #[test]
    fn image_service_records_map_onto_the_template_shape() {
        // The service answers the trimmed eight fields; the template shape
        // (handle, format label, node kind, placeholders) is derived here.
        let image: ImageServiceImage = serde_json::from_value(serde_json::json!({
            "id": "image_abc",
            "name": "team/app",
            "snapshot_id": "snap123",
            "global": true,
            "physical_size_bytes": 42,
            "logical_size_bytes": 10_737_418_240_u64,
            "created_at": "2026-10-15T00:00:00.000Z",
            "updated_at": "2026-10-15T00:00:00.000Z",
        }))
        .unwrap();
        let template: SandboxTemplate = image.into();
        let json = serde_json::to_value(&template).unwrap();
        assert_eq!(
            json,
            serde_json::json!({
                "id": "image_abc",
                "name": "team/app",
                "snapshotId": "snap123",
                "snapshotSandboxId": null,
                "snapshotUri": "snapshot://snap123",
                "snapshotFormatVersion": CAS_SNAPSHOT_FORMAT_VERSION,
                "snapshotSizeBytes": 42,
                "rootfsDiskBytes": 10_737_418_240_u64,
                "rootfsNodeKind": "base",
                "public": true,
                "dockerfile": "",
                "createdAt": "2026-10-15T00:00:00.000Z",
                "updatedAt": "2026-10-15T00:00:00.000Z",
            }),
            "no imageId or buildId extras"
        );
        assert!(is_cas_image(&template));
        // A pre-trim record is not a current record.
        assert!(
            serde_json::from_value::<ImageServiceImage>(serde_json::json!({
                "id": "image_abc",
                "name": "team/app",
                "snapshot_id": "snap123",
                "public": true,
                "rootfs_disk_bytes": 10_737_418_240_u64,
            }))
            .is_err()
        );
    }

    #[test]
    fn image_service_error_bodies_reduce_to_their_message() {
        let error = tidy_error(SdkError::ServerError {
            status: StatusCode::CONFLICT,
            message: r#"{"error":"image is being deleted"}"#.to_string(),
        });
        assert!(error.to_string().contains("image is being deleted"));
        assert!(!error.to_string().contains('{'));
        let plain = tidy_error(SdkError::Authorization("Forbidden".to_string()));
        assert!(plain.to_string().contains("Forbidden"));
    }
}