mold-ai-core 0.15.0

Shared types, API protocol, and HTTP client for mold
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
//! RunPod REST API client.
//!
//! Wraps `https://rest.runpod.io/v1/` for pod lifecycle management from within
//! the mold CLI. Uses Bearer-token auth via `RUNPOD_API_KEY` env var or an
//! explicit key. All methods are async.

use crate::error::MoldError;
use anyhow::Result;
use reqwest::{Client, StatusCode};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fmt;
use std::time::Duration;

/// Default REST base URL.
pub const DEFAULT_ENDPOINT: &str = "https://rest.runpod.io/v1";

/// GraphQL endpoint (used for /user since REST doesn't expose it).
pub const GRAPHQL_ENDPOINT: &str = "https://api.runpod.io/graphql";

/// Environment variable that holds the RunPod API key.
pub const API_KEY_ENV: &str = "RUNPOD_API_KEY";

/// Live RunPod REST limits. The generated API schema currently advertises a
/// wider range, but the production service rejects sizes below 10 GB and 4 TB.
pub const NETWORK_VOLUME_MIN_GB: u32 = 10;
pub const NETWORK_VOLUME_MAX_GB: u32 = 3999;

pub fn valid_network_volume_size(size: u32) -> bool {
    (NETWORK_VOLUME_MIN_GB..=NETWORK_VOLUME_MAX_GB).contains(&size)
}

/// Persisted configuration under `[runpod]` in `config.toml`.
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct RunPodSettings {
    /// API key stored in config. Env var `RUNPOD_API_KEY` takes precedence.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub api_key: Option<String>,

    /// Preferred GPU, e.g. `"NVIDIA GeForce RTX 5090"`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_gpu: Option<String>,

    /// Preferred datacenter id, e.g. `"EUR-IS-2"`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_datacenter: Option<String>,

    /// Attach this network volume to new pods (id from RunPod console).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_network_volume_id: Option<String>,

    /// When `true`, `mold runpod run` deletes the pod after generating.
    /// When `false`, the pod is left running for reuse. Default `false`.
    #[serde(default)]
    pub auto_teardown: bool,

    /// After this many minutes of idle time, background reap deletes the pod.
    /// `0` disables the idle reaper. Default `20`.
    #[serde(default = "default_auto_teardown_idle_mins")]
    pub auto_teardown_idle_mins: u32,

    /// Fail UAT or `run` if cumulative pod spend for the session exceeds this
    /// many USD. `0.0` disables the guard. Default `0.0`.
    #[serde(default)]
    pub cost_alert_usd: f64,

    /// Override the REST endpoint (mostly for testing).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub endpoint: Option<String>,
}

fn default_auto_teardown_idle_mins() -> u32 {
    20
}

/// Redact `api_key` when logging.
impl RunPodSettings {
    pub fn redacted_debug(&self) -> String {
        format!(
            "RunPodSettings {{ api_key: {}, default_gpu: {:?}, default_datacenter: {:?}, \
             default_network_volume_id: {:?}, auto_teardown: {}, auto_teardown_idle_mins: {}, \
             cost_alert_usd: {}, endpoint: {:?} }}",
            if self.api_key.is_some() {
                "Some(\"<redacted>\")"
            } else {
                "None"
            },
            self.default_gpu,
            self.default_datacenter,
            self.default_network_volume_id,
            self.auto_teardown,
            self.auto_teardown_idle_mins,
            self.cost_alert_usd,
            self.endpoint,
        )
    }
}

// ─── API response types ────────────────────────────────────────────────────

/// `GET /user` response.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct UserInfo {
    pub id: String,
    pub email: String,
    #[serde(default)]
    pub client_balance: f64,
    #[serde(default)]
    pub current_spend_per_hr: f64,
    #[serde(default)]
    pub spend_limit: Option<f64>,
}

/// One entry from `GET /gputypes`.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct GpuType {
    #[serde(default)]
    pub id: Option<String>,
    #[serde(rename = "displayName", default)]
    pub display_name: String,
    #[serde(rename = "gpuId", default)]
    pub gpu_id: String,
    #[serde(rename = "memoryInGb", default)]
    pub memory_in_gb: u32,
    #[serde(rename = "secureCloud", default)]
    pub secure_cloud: bool,
    #[serde(rename = "communityCloud", default)]
    pub community_cloud: bool,
    #[serde(rename = "stockStatus", default)]
    pub stock_status: Option<String>,
    #[serde(default)]
    pub available: bool,
}

/// One entry from `GET /datacenters`.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Datacenter {
    pub id: String,
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub location: Option<String>,
    #[serde(rename = "gpuAvailability", default)]
    pub gpu_availability: Vec<GpuAvailability>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct GpuAvailability {
    #[serde(rename = "displayName", default)]
    pub display_name: String,
    #[serde(rename = "gpuId", default)]
    pub gpu_id: String,
    #[serde(rename = "stockStatus", default)]
    pub stock_status: Option<String>,
}

/// `GET /pods` / `GET /pods/{id}` response.
///
/// Only fields we actually use are deserialized — anything else is allowed via
/// `#[serde(default)]` on the struct to avoid breaking on RunPod API drift.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Pod {
    pub id: String,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(rename = "desiredStatus", default)]
    pub desired_status: String,
    #[serde(rename = "imageName", default)]
    pub image_name: Option<String>,
    #[serde(rename = "gpuCount", default)]
    pub gpu_count: u32,
    #[serde(rename = "costPerHr", default)]
    pub cost_per_hr: f64,
    #[serde(rename = "uptimeSeconds", default)]
    pub uptime_seconds: u64,
    #[serde(rename = "lastStatusChange", default)]
    pub last_status_change: Option<String>,
    #[serde(rename = "memoryInGb", default)]
    pub memory_in_gb: u32,
    #[serde(rename = "vcpuCount", default)]
    pub vcpu_count: u32,
    #[serde(rename = "volumeInGb", default)]
    pub volume_in_gb: u32,
    #[serde(rename = "volumeMountPath", default)]
    pub volume_mount_path: Option<String>,
    #[serde(default)]
    pub ports: serde_json::Value,
    #[serde(default)]
    pub env: serde_json::Value,
    #[serde(default)]
    pub machine: Option<PodMachine>,
    /// Current REST responses expose the assigned GPU here even when the
    /// optional machine expansion omits `gpuDisplayName`.
    #[serde(default)]
    pub gpu: Option<PodGpu>,
    #[serde(default)]
    pub runtime: Option<serde_json::Value>,
    #[serde(rename = "networkVolume", default)]
    pub network_volume: Option<NetworkVolume>,
    /// Current REST list/get responses expose only this id; create responses
    /// may additionally include the expanded `networkVolume` object above.
    #[serde(rename = "networkVolumeId", default)]
    pub network_volume_id: Option<String>,
}

impl Pod {
    pub fn attached_network_volume_id(&self) -> Option<&str> {
        self.network_volume
            .as_ref()
            .map(|volume| volume.id.as_str())
            .or(self.network_volume_id.as_deref())
    }

    pub fn gpu_name(&self) -> Option<&str> {
        self.gpu
            .as_ref()
            .and_then(|gpu| gpu.display_name.as_deref())
            .or_else(|| {
                self.machine
                    .as_ref()
                    .and_then(|machine| machine.gpu_display_name.as_deref())
            })
            .or_else(|| {
                self.machine
                    .as_ref()
                    .and_then(|machine| machine.gpu_type_id.as_deref())
            })
            .or_else(|| self.gpu.as_ref().and_then(|gpu| gpu.id.as_deref()))
    }

    pub fn datacenter_id(&self) -> Option<&str> {
        self.machine
            .as_ref()
            .and_then(|machine| machine.data_center_id.as_deref())
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PodMachine {
    #[serde(rename = "gpuDisplayName", default)]
    pub gpu_display_name: Option<String>,
    #[serde(rename = "gpuTypeId", default)]
    pub gpu_type_id: Option<String>,
    #[serde(rename = "dataCenterId", default)]
    pub data_center_id: Option<String>,
    #[serde(default)]
    pub location: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PodGpu {
    #[serde(default)]
    pub id: Option<String>,
    #[serde(rename = "displayName", default)]
    pub display_name: Option<String>,
    #[serde(default)]
    pub count: Option<u32>,
}

/// Body for `POST /pods`.
#[derive(Debug, Clone, Serialize, Default)]
pub struct CreatePodRequest {
    pub name: String,
    #[serde(rename = "imageName")]
    pub image_name: String,
    #[serde(rename = "gpuTypeIds")]
    pub gpu_type_ids: Vec<String>,
    #[serde(rename = "cloudType")]
    pub cloud_type: String,
    #[serde(rename = "dataCenterIds", skip_serializing_if = "Option::is_none")]
    pub data_center_ids: Option<Vec<String>>,
    #[serde(rename = "gpuCount")]
    pub gpu_count: u32,
    #[serde(rename = "containerDiskInGb")]
    pub container_disk_in_gb: u32,
    #[serde(rename = "volumeInGb")]
    pub volume_in_gb: u32,
    #[serde(rename = "volumeMountPath")]
    pub volume_mount_path: String,
    pub ports: Vec<String>,
    pub env: serde_json::Map<String, serde_json::Value>,
    #[serde(rename = "networkVolumeId", skip_serializing_if = "Option::is_none")]
    pub network_volume_id: Option<String>,
}

/// One entry from `GET /networkvolumes`.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct NetworkVolume {
    pub id: String,
    pub name: String,
    #[serde(rename = "dataCenterId", default)]
    pub data_center_id: String,
    pub size: u32,
}

/// Body for `POST /networkvolumes`.
#[derive(Debug, Clone, Serialize)]
pub struct CreateNetworkVolumeRequest {
    pub name: String,
    pub size: u32,
    #[serde(rename = "dataCenterId")]
    pub data_center_id: String,
}

/// Body for `PATCH /networkvolumes/{id}`. Sizes may only increase.
#[derive(Debug, Clone, Serialize)]
pub struct UpdateNetworkVolumeRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<u32>,
}

// ─── Client ────────────────────────────────────────────────────────────────

#[derive(Clone)]
pub struct RunPodClient {
    endpoint: String,
    graphql_endpoint: String,
    api_key: String,
    http: Client,
}

impl fmt::Debug for RunPodClient {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RunPodClient")
            .field("endpoint", &self.endpoint)
            .field("api_key", &"<redacted>")
            .finish()
    }
}

impl RunPodClient {
    /// Construct with explicit endpoint + key. The GraphQL endpoint
    /// defaults to `GRAPHQL_ENDPOINT` when the REST endpoint is production,
    /// and falls back to the same URL when the REST endpoint is overridden
    /// (so tests pointing at a mock server route GraphQL calls there too).
    pub fn new(endpoint: impl Into<String>, api_key: impl Into<String>) -> Self {
        let rest = endpoint.into();
        let graphql = if rest.starts_with(DEFAULT_ENDPOINT) {
            GRAPHQL_ENDPOINT.to_string()
        } else {
            rest.clone()
        };
        Self::new_with_graphql(rest, graphql, api_key)
    }

    /// Construct with explicit REST + GraphQL endpoints.
    pub fn new_with_graphql(
        endpoint: impl Into<String>,
        graphql_endpoint: impl Into<String>,
        api_key: impl Into<String>,
    ) -> Self {
        let http = Client::builder()
            .timeout(Duration::from_secs(30))
            .build()
            .unwrap_or_default();
        Self {
            endpoint: endpoint.into(),
            graphql_endpoint: graphql_endpoint.into(),
            api_key: api_key.into(),
            http,
        }
    }

    /// Construct from config + environment. `RUNPOD_API_KEY` overrides
    /// `settings.api_key`. Returns `RunPodAuth` error if no key is available.
    pub fn from_settings(settings: &RunPodSettings) -> std::result::Result<Self, MoldError> {
        let key = std::env::var(API_KEY_ENV)
            .ok()
            .filter(|k| !k.is_empty())
            .or_else(|| settings.api_key.clone())
            .ok_or_else(|| {
                MoldError::RunPodAuth(format!(
                    "RunPod API key not set — export {API_KEY_ENV} or run \
                     `mold config set runpod.api_key <key>`"
                ))
            })?;
        let endpoint = settings
            .endpoint
            .clone()
            .unwrap_or_else(|| DEFAULT_ENDPOINT.to_string());
        Ok(Self::new(endpoint, key))
    }

    fn url(&self, path: &str) -> String {
        format!("{}{}", self.endpoint.trim_end_matches('/'), path)
    }

    async fn get_json<T: for<'de> Deserialize<'de>>(&self, path: &str) -> Result<T> {
        let resp = self
            .http
            .get(self.url(path))
            .bearer_auth(&self.api_key)
            .send()
            .await
            .map_err(|e| MoldError::RunPod(format!("RunPod {path}: {e}")))?;
        let status = resp.status();
        if status.is_success() {
            let body = resp
                .text()
                .await
                .map_err(|e| MoldError::RunPod(format!("RunPod {path} body: {e}")))?;
            serde_json::from_str(&body).map_err(|e| {
                MoldError::RunPod(format!(
                    "RunPod {path}: failed to parse response: {e} — body: {}",
                    truncate_for_error(&body)
                ))
                .into()
            })
        } else {
            Err(http_error(path, status, resp).await.into())
        }
    }

    async fn post_json<B: Serialize, T: for<'de> Deserialize<'de>>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T> {
        let resp = self
            .http
            .post(self.url(path))
            .bearer_auth(&self.api_key)
            .json(body)
            .send()
            .await
            .map_err(|e| MoldError::RunPod(format!("RunPod {path}: {e}")))?;
        let status = resp.status();
        if status.is_success() {
            let text = resp
                .text()
                .await
                .map_err(|e| MoldError::RunPod(format!("RunPod {path} body: {e}")))?;
            serde_json::from_str(&text).map_err(|e| {
                MoldError::RunPod(format!(
                    "RunPod {path}: failed to parse response: {e} — body: {}",
                    truncate_for_error(&text)
                ))
                .into()
            })
        } else {
            Err(http_error(path, status, resp).await.into())
        }
    }

    async fn post_empty(&self, path: &str) -> Result<()> {
        let resp = self
            .http
            .post(self.url(path))
            .bearer_auth(&self.api_key)
            .send()
            .await
            .map_err(|e| MoldError::RunPod(format!("RunPod {path}: {e}")))?;
        let status = resp.status();
        if status.is_success() {
            Ok(())
        } else {
            Err(http_error(path, status, resp).await.into())
        }
    }

    async fn patch_json<B: Serialize, T: for<'de> Deserialize<'de>>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T> {
        let resp = self
            .http
            .patch(self.url(path))
            .bearer_auth(&self.api_key)
            .json(body)
            .send()
            .await
            .map_err(|e| MoldError::RunPod(format!("RunPod {path}: {e}")))?;
        let status = resp.status();
        if status.is_success() {
            let text = resp
                .text()
                .await
                .map_err(|e| MoldError::RunPod(format!("RunPod {path} body: {e}")))?;
            serde_json::from_str(&text).map_err(|e| {
                MoldError::RunPod(format!(
                    "RunPod {path}: failed to parse response: {e} — body: {}",
                    truncate_for_error(&text)
                ))
                .into()
            })
        } else {
            Err(http_error(path, status, resp).await.into())
        }
    }

    async fn delete(&self, path: &str) -> Result<()> {
        let resp = self
            .http
            .delete(self.url(path))
            .bearer_auth(&self.api_key)
            .send()
            .await
            .map_err(|e| MoldError::RunPod(format!("RunPod {path}: {e}")))?;
        let status = resp.status();
        if status.is_success() {
            Ok(())
        } else {
            Err(http_error(path, status, resp).await.into())
        }
    }

    // ─── Typed endpoints ────────────────────────────────────────────

    /// User/account info isn't exposed by the REST API, so we fall back to
    /// the GraphQL endpoint (same API key works for both).
    pub async fn user(&self) -> Result<UserInfo> {
        let query = serde_json::json!({
            "query": "query { myself { id email clientBalance currentSpendPerHr spendLimit } }"
        });
        let resp = self
            .http
            .post(&self.graphql_endpoint)
            .bearer_auth(&self.api_key)
            .json(&query)
            .send()
            .await
            .map_err(|e| MoldError::RunPod(format!("RunPod graphql /user: {e}")))?;
        let status = resp.status();
        if !status.is_success() {
            return Err(http_error("graphql /user", status, resp).await.into());
        }
        let body: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| MoldError::RunPod(format!("RunPod graphql /user json: {e}")))?;
        if let Some(errs) = body.get("errors") {
            return Err(MoldError::RunPod(format!("RunPod graphql errors: {errs}")).into());
        }
        let myself = body
            .get("data")
            .and_then(|d| d.get("myself"))
            .ok_or_else(|| MoldError::RunPod("graphql: missing data.myself".into()))?;
        let info = UserInfo {
            id: myself
                .get("id")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string(),
            email: myself
                .get("email")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string(),
            client_balance: myself
                .get("clientBalance")
                .and_then(|v| v.as_f64())
                .unwrap_or(0.0),
            current_spend_per_hr: myself
                .get("currentSpendPerHr")
                .and_then(|v| v.as_f64())
                .unwrap_or(0.0),
            spend_limit: myself.get("spendLimit").and_then(|v| v.as_f64()),
        };
        Ok(info)
    }

    /// Query GPU types via GraphQL (not exposed in REST v1).
    /// Stock status is aggregated: the highest stock level across all DCs.
    pub async fn gpu_types(&self) -> Result<Vec<GpuType>> {
        let query = serde_json::json!({
            "query": "query { gpuTypes { id displayName memoryInGb secureCloud communityCloud } dataCenters { gpuAvailability { displayName stockStatus } } }"
        });
        let body = self.graphql(&query).await?;
        let data = body
            .get("data")
            .ok_or_else(|| MoldError::RunPod("graphql: missing data".into()))?;
        let types: Vec<GpuType> = serde_json::from_value(
            data.get("gpuTypes")
                .cloned()
                .unwrap_or(serde_json::Value::Array(vec![])),
        )
        .map_err(|e| MoldError::RunPod(format!("parse gpuTypes: {e}")))?;
        // Aggregate stock across datacenters.
        let mut best_stock: std::collections::HashMap<String, String> =
            std::collections::HashMap::new();
        if let Some(dcs) = data.get("dataCenters").and_then(|v| v.as_array()) {
            for dc in dcs {
                if let Some(avail) = dc.get("gpuAvailability").and_then(|v| v.as_array()) {
                    for a in avail {
                        if let (Some(name), Some(stock)) = (
                            a.get("displayName").and_then(|v| v.as_str()),
                            a.get("stockStatus").and_then(|v| v.as_str()),
                        ) {
                            let current = best_stock.get(name).cloned().unwrap_or_default();
                            if stock_rank(stock) > stock_rank(&current) {
                                best_stock.insert(name.to_string(), stock.to_string());
                            }
                        }
                    }
                }
            }
        }
        let mut out = types;
        for g in out.iter_mut() {
            if let Some(s) = best_stock.get(&g.display_name) {
                if !s.is_empty() {
                    g.stock_status = Some(s.clone());
                }
            }
            g.available = g.stock_status.as_deref().is_some_and(|s| s != "None");
        }
        Ok(out)
    }

    /// Query datacenters with per-GPU availability via GraphQL.
    pub async fn datacenters(&self) -> Result<Vec<Datacenter>> {
        let query = serde_json::json!({
            "query": "query { dataCenters { id name listed gpuAvailability { id displayName stockStatus } } }"
        });
        let body = self.graphql(&query).await?;
        let arr = body
            .get("data")
            .and_then(|d| d.get("dataCenters"))
            .cloned()
            .unwrap_or(serde_json::Value::Array(vec![]));
        // Map GraphQL `id` → `gpuId` so we can reuse the same Datacenter type.
        let arr = match arr {
            serde_json::Value::Array(mut dcs) => {
                for dc in dcs.iter_mut() {
                    if let Some(avail) =
                        dc.get_mut("gpuAvailability").and_then(|v| v.as_array_mut())
                    {
                        for a in avail.iter_mut() {
                            if let Some(id) = a.get("id").and_then(|v| v.as_str()) {
                                let id = id.to_string();
                                if let Some(obj) = a.as_object_mut() {
                                    obj.insert("gpuId".into(), serde_json::Value::String(id));
                                }
                            }
                        }
                    }
                }
                serde_json::Value::Array(dcs)
            }
            other => other,
        };
        let dcs: Vec<Datacenter> = serde_json::from_value(arr)
            .map_err(|e| MoldError::RunPod(format!("parse dataCenters: {e}")))?;
        Ok(dcs)
    }

    async fn graphql(&self, query: &serde_json::Value) -> Result<serde_json::Value> {
        let resp = self
            .http
            .post(&self.graphql_endpoint)
            .bearer_auth(&self.api_key)
            .json(query)
            .send()
            .await
            .map_err(|e| MoldError::RunPod(format!("RunPod graphql: {e}")))?;
        let status = resp.status();
        if !status.is_success() {
            return Err(http_error("graphql", status, resp).await.into());
        }
        let body: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| MoldError::RunPod(format!("graphql body: {e}")))?;
        if let Some(errs) = body
            .get("errors")
            .filter(|e| !e.as_array().map(|a| a.is_empty()).unwrap_or(true))
        {
            return Err(MoldError::RunPod(format!("graphql errors: {errs}")).into());
        }
        Ok(body)
    }

    pub async fn list_pods(&self) -> Result<Vec<Pod>> {
        self.get_json("/pods?includeMachine=true").await
    }

    pub async fn get_pod(&self, id: &str) -> Result<Pod> {
        self.get_json(&format!("/pods/{id}?includeMachine=true"))
            .await
    }

    pub async fn create_pod(&self, req: &CreatePodRequest) -> Result<Pod> {
        self.post_json("/pods", req).await
    }

    /// GPU IDs accepted by the REST Pod create endpoint. RunPod's GraphQL
    /// inventory can advertise GPUs before the REST create schema accepts
    /// them, so launchers must intersect inventory with this live contract.
    pub async fn supported_pod_gpu_type_ids(&self) -> Result<HashSet<String>> {
        let spec: serde_json::Value = self.get_json("/openapi.json").await?;
        parse_pod_gpu_type_ids(&spec)
    }

    pub async fn stop_pod(&self, id: &str) -> Result<()> {
        self.post_empty(&format!("/pods/{id}/stop")).await
    }

    pub async fn start_pod(&self, id: &str) -> Result<()> {
        self.post_empty(&format!("/pods/{id}/start")).await
    }

    pub async fn delete_pod(&self, id: &str) -> Result<()> {
        self.delete(&format!("/pods/{id}")).await
    }

    pub async fn network_volumes(&self) -> Result<Vec<NetworkVolume>> {
        self.get_json("/networkvolumes").await
    }

    pub async fn get_network_volume(&self, id: &str) -> Result<NetworkVolume> {
        self.get_json(&format!("/networkvolumes/{id}")).await
    }

    pub async fn create_network_volume(
        &self,
        req: &CreateNetworkVolumeRequest,
    ) -> Result<NetworkVolume> {
        self.post_json("/networkvolumes", req).await
    }

    pub async fn update_network_volume(
        &self,
        id: &str,
        req: &UpdateNetworkVolumeRequest,
    ) -> Result<NetworkVolume> {
        self.patch_json(&format!("/networkvolumes/{id}"), req).await
    }

    pub async fn delete_network_volume(&self, id: &str) -> Result<()> {
        self.delete(&format!("/networkvolumes/{id}")).await
    }

    /// Permanently delete a network volume only after proving that no Pod is
    /// attached. The attachment check intentionally fails closed: an auth,
    /// transport, or API error must never fall through to destructive delete.
    pub async fn delete_network_volume_if_detached(&self, id: &str) -> Result<()> {
        let pods = self.list_pods().await?;
        if let Some(pod) = pods
            .iter()
            .find(|pod| pod.attached_network_volume_id() == Some(id))
        {
            return Err(MoldError::RunPod(format!(
                "delete pod {} before deleting its attached network volume",
                pod.id
            ))
            .into());
        }
        self.delete_network_volume(id).await
    }
}

// ─── Helpers ────────────────────────────────────────────────────────────────

async fn http_error(path: &str, status: StatusCode, resp: reqwest::Response) -> MoldError {
    let body = resp.text().await.unwrap_or_default();
    let msg = truncate_for_error(&body);
    match status {
        StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => {
            MoldError::RunPodAuth(format!("RunPod {path} {status}: {msg}"))
        }
        StatusCode::NOT_FOUND => {
            MoldError::RunPodNotFound(format!("RunPod {path} {status}: {msg}"))
        }
        StatusCode::CONFLICT
        | StatusCode::SERVICE_UNAVAILABLE
        | StatusCode::INTERNAL_SERVER_ERROR
            if {
                let lower = msg.to_lowercase();
                lower.contains("does not have the resources")
                    || lower.contains("no instances currently available")
            } =>
        {
            MoldError::RunPodNoStock(format!("RunPod {path} {status}: {msg}"))
        }
        _ => MoldError::RunPod(format!("RunPod {path} {status}: {msg}")),
    }
}

fn stock_rank(s: &str) -> u8 {
    match s {
        "High" => 3,
        "Medium" => 2,
        "Low" => 1,
        _ => 0,
    }
}

fn parse_pod_gpu_type_ids(spec: &serde_json::Value) -> Result<HashSet<String>> {
    let values = spec
        .pointer("/components/schemas/PodCreateInput/properties/gpuTypeIds/items/enum")
        .and_then(serde_json::Value::as_array)
        .ok_or_else(|| MoldError::RunPod("OpenAPI schema is missing Pod GPU types".into()))?;
    Ok(values
        .iter()
        .filter_map(serde_json::Value::as_str)
        .map(str::to_string)
        .collect())
}

fn truncate_for_error(s: &str) -> String {
    const MAX: usize = 400;
    let s = s.trim();
    if s.len() <= MAX {
        s.to_string()
    } else {
        format!("{}", &s[..MAX])
    }
}

/// Map a RunPod GPU `displayName` (e.g. `"RTX 4090"`) to the matching
/// `ghcr.io/utensils/mold` image tag.
pub fn image_tag_for_gpu(display_name: &str) -> &'static str {
    let d = display_name.to_lowercase();
    if d.contains("5090") || d.contains("rtx pro") || d.contains("blackwell") || d.contains("b200")
    {
        "latest-sm120"
    } else if d.contains("h100")
        || d.contains("h200")
        || d.contains("gh200")
        || d.contains("hopper")
    {
        "latest-sm90"
    } else if d.contains("a100") || d.contains("3090") || d.contains("a40") || d.contains("ampere")
    {
        "latest-sm80"
    } else {
        // Ada (4090, L40, L40S) and fallback
        "latest"
    }
}

/// Ranked preference when auto-picking GPUs. Higher index = more preferred.
pub const GPU_PREFERENCE: &[&str] = &[
    "A100 PCIe",
    "L40",
    "L40S",
    "RTX A6000",
    "RTX 5090",
    "RTX 4090",
];

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

    #[test]
    fn image_tag_mapping() {
        assert_eq!(image_tag_for_gpu("RTX 4090"), "latest");
        assert_eq!(image_tag_for_gpu("NVIDIA GeForce RTX 4090"), "latest");
        assert_eq!(image_tag_for_gpu("L40S"), "latest");
        assert_eq!(image_tag_for_gpu("RTX 5090"), "latest-sm120");
        assert_eq!(image_tag_for_gpu("NVIDIA GeForce RTX 5090"), "latest-sm120");
        assert_eq!(image_tag_for_gpu("RTX PRO 4500"), "latest-sm120");
        assert_eq!(image_tag_for_gpu("A100 80GB"), "latest-sm80");
        assert_eq!(image_tag_for_gpu("A100 PCIe"), "latest-sm80");
        assert_eq!(image_tag_for_gpu("RTX 3090"), "latest-sm80");
        assert_eq!(image_tag_for_gpu("H100 SXM"), "latest-sm90");
        assert_eq!(image_tag_for_gpu("H200 SXM"), "latest-sm90");
    }

    #[test]
    fn live_network_volume_size_bounds_are_enforced() {
        assert!(!valid_network_volume_size(9));
        assert!(valid_network_volume_size(10));
        assert!(valid_network_volume_size(3999));
        assert!(!valid_network_volume_size(4000));
    }

    #[test]
    fn pod_reads_gpu_from_top_level_rest_shape() {
        let pod: Pod = serde_json::from_value(serde_json::json!({
            "id": "pod-1",
            "desiredStatus": "RUNNING",
            "gpu": { "id": "NVIDIA RTX 6000", "displayName": "RTX PRO 6000 Blackwell" }
        }))
        .unwrap();
        assert_eq!(
            pod.gpu.and_then(|gpu| gpu.display_name).as_deref(),
            Some("RTX PRO 6000 Blackwell")
        );
    }

    #[test]
    fn pod_reads_current_machine_gpu_and_network_volume_id_shape() {
        let pod: Pod = serde_json::from_value(serde_json::json!({
            "id": "pod-1",
            "desiredStatus": "RUNNING",
            "networkVolumeId": "nv-1",
            "machine": {
                "gpuTypeId": "NVIDIA GeForce RTX 4090",
                "dataCenterId": "EU-RO-1",
                "location": "RO"
            }
        }))
        .unwrap();
        assert_eq!(pod.attached_network_volume_id(), Some("nv-1"));
        assert_eq!(pod.gpu_name(), Some("NVIDIA GeForce RTX 4090"));
        assert_eq!(pod.datacenter_id(), Some("EU-RO-1"));
        assert_eq!(
            pod.machine
                .as_ref()
                .and_then(|machine| machine.gpu_type_id.as_deref()),
            Some("NVIDIA GeForce RTX 4090")
        );
    }

    #[test]
    fn pod_location_is_not_treated_as_an_exact_datacenter_id() {
        let pod: Pod = serde_json::from_value(serde_json::json!({
            "id": "pod-1",
            "machine": { "location": "RO" }
        }))
        .unwrap();

        assert_eq!(pod.datacenter_id(), None);
    }

    #[test]
    fn parses_rest_pod_gpu_ids_from_openapi() {
        let spec = serde_json::json!({
            "components": { "schemas": { "PodCreateInput": { "properties": {
                "gpuTypeIds": { "items": { "enum": ["NVIDIA GeForce RTX 5090", "NVIDIA L40S"] } }
            } } } }
        });
        assert_eq!(
            parse_pod_gpu_type_ids(&spec).unwrap(),
            ["NVIDIA GeForce RTX 5090", "NVIDIA L40S"]
                .into_iter()
                .map(str::to_string)
                .collect()
        );
    }

    #[test]
    fn redacted_debug_hides_api_key() {
        let s = RunPodSettings {
            api_key: Some("secret-key".to_string()),
            ..Default::default()
        };
        let out = s.redacted_debug();
        assert!(!out.contains("secret-key"));
        assert!(out.contains("<redacted>"));
    }

    #[test]
    fn from_settings_requires_key() {
        std::env::remove_var(API_KEY_ENV);
        let err = RunPodClient::from_settings(&RunPodSettings::default()).unwrap_err();
        assert!(matches!(err, MoldError::RunPodAuth(_)));
    }

    #[test]
    fn truncate_for_error_boundary() {
        let short = "short";
        assert_eq!(truncate_for_error(short), "short");
        let long = "x".repeat(500);
        let truncated = truncate_for_error(&long);
        assert!(truncated.ends_with(''));
        assert!(truncated.chars().count() <= 401);
    }

    #[test]
    fn runpod_settings_toml_roundtrip() {
        let original = RunPodSettings {
            api_key: Some("k".to_string()),
            default_gpu: Some("RTX 5090".to_string()),
            default_datacenter: Some("EUR-IS-2".to_string()),
            default_network_volume_id: Some("nv-123".to_string()),
            auto_teardown: true,
            auto_teardown_idle_mins: 30,
            cost_alert_usd: 3.5,
            endpoint: None,
        };
        let toml_s = toml::to_string(&original).unwrap();
        let round: RunPodSettings = toml::from_str(&toml_s).unwrap();
        assert_eq!(round.api_key, original.api_key);
        assert_eq!(round.default_gpu, original.default_gpu);
        assert_eq!(round.default_datacenter, original.default_datacenter);
        assert_eq!(
            round.default_network_volume_id,
            original.default_network_volume_id
        );
        assert_eq!(round.auto_teardown, original.auto_teardown);
        assert_eq!(
            round.auto_teardown_idle_mins,
            original.auto_teardown_idle_mins
        );
        assert_eq!(round.cost_alert_usd, original.cost_alert_usd);
    }

    #[test]
    fn default_auto_teardown_idle_mins_is_20() {
        let s: RunPodSettings = toml::from_str("").unwrap();
        assert_eq!(s.auto_teardown_idle_mins, 20);
    }
}