mold-ai-core 0.12.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
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
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::OnceLock;

use crate::expand::ExpandSettings;
use crate::manifest::resolve_model_name;
use crate::types::Scheduler;

static RUNTIME_MODELS_DIR_OVERRIDE: OnceLock<PathBuf> = OnceLock::new();

/// Banner comment written at the top of a `save_bootstrap_only` output so
/// readers understand why the usual user-preference fields are missing.
const BOOTSTRAP_ONLY_BANNER: &str = "\
# mold config — bootstrap-only surface.
#
# User preferences (expand.*, generate.default_*, per-model generation
# defaults, lora, scheduler) live in SQLite at <MOLD_HOME>/mold.db.
# Edit them via:
#   mold config set <key> <value>
#   mold config get <key>
#   mold config reset <key>     # drop the DB row, fall back to this file
#
# This file retains only identifiers, paths, ports, credentials, logging,
# and per-model file-path entries. Adding generation defaults here is
# silently overridden by the DB on load.

";

/// Global + per-model keys moved to the DB by issue #265. Stripped from
/// the TOML value tree by [`Config::save_bootstrap_only_to`].
const STRIPPED_GLOBAL_KEYS: &[&str] = &[
    "default_width",
    "default_height",
    "default_steps",
    "embed_metadata",
    "default_negative_prompt",
    "t5_variant",
    "qwen3_variant",
    "expand",
];

const STRIPPED_MODEL_KEYS: &[&str] = &[
    "default_steps",
    "default_guidance",
    "default_width",
    "default_height",
    "scheduler",
    "negative_prompt",
    "lora",
    "lora_scale",
    "default_frames",
    "default_fps",
];

fn strip_user_pref_fields(doc: &mut toml::Value) {
    let Some(table) = doc.as_table_mut() else {
        return;
    };
    for key in STRIPPED_GLOBAL_KEYS {
        table.remove(*key);
    }
    if let Some(toml::Value::Table(models)) = table.get_mut("models") {
        for (_, mc) in models.iter_mut() {
            if let Some(mc_table) = mc.as_table_mut() {
                for key in STRIPPED_MODEL_KEYS {
                    mc_table.remove(*key);
                }
            }
        }
    }
}

/// Hook installed by callers (typically `mold-cli` at startup) that wants
/// to overlay DB-backed user preferences onto every freshly-loaded
/// `Config`. `mold-core` itself must not depend on `mold-db`, so the hook
/// is just an opaque function pointer registered once per process.
///
/// Runs after TOML parsing + legacy v0→v1 migration and before the
/// returned value is handed to the caller. Errors should be swallowed
/// inside the hook (the DB layer logs); failing here must never break
/// `load_or_default()`.
pub type ConfigPostLoadHook = fn(&mut Config);
static POST_LOAD_HOOK: OnceLock<ConfigPostLoadHook> = OnceLock::new();

/// Register a post-load hook. First caller wins; subsequent calls are a
/// no-op so tests can't clobber each other.
pub fn install_post_load_hook(hook: ConfigPostLoadHook) {
    let _ = POST_LOAD_HOOK.set(hook);
}

/// Hook for [`Config::read_last_model`] that lets the DB layer provide
/// the value without `mold-core` depending on `mold-db`. When installed,
/// the hook's return value takes precedence over the legacy sidecar file.
pub type ReadLastModelHook = fn() -> Option<String>;
static READ_LAST_MODEL_HOOK: OnceLock<ReadLastModelHook> = OnceLock::new();

/// Register a read-last-model hook. First caller wins.
pub fn install_read_last_model_hook(hook: ReadLastModelHook) {
    let _ = READ_LAST_MODEL_HOOK.set(hook);
}

/// Which fallback step resolved the default model.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DefaultModelSource {
    /// `MOLD_DEFAULT_MODEL` environment variable
    EnvVar,
    /// Config file `default_model` with a custom `[models]` entry
    ConfigCustomEntry,
    /// Config file `default_model` (manifest model, downloaded)
    Config,
    /// Last-used model from `$MOLD_HOME/last-model`
    LastUsed,
    /// Only one model is downloaded — auto-selected
    OnlyDownloaded,
    /// Config file default (model not downloaded, will auto-pull)
    ConfigDefault,
}

/// Result of resolving the default model: the model name and how it was resolved.
#[derive(Debug, Clone)]
pub struct DefaultModelResolution {
    pub model: String,
    pub source: DefaultModelSource,
}

/// Per-model file path + default settings configuration.
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct ModelConfig {
    // --- paths ---
    pub transformer: Option<String>,
    /// Multi-shard transformer paths (Z-Image BF16); empty means use single `transformer`
    pub transformer_shards: Option<Vec<String>>,
    pub vae: Option<String>,
    /// LTX latent upsampler / spatial upscaler weights.
    pub spatial_upscaler: Option<String>,
    /// Optional temporal upscaler weights for LTX-2/LTX-2.3.
    pub temporal_upscaler: Option<String>,
    /// Optional distilled LoRA bundled with a model manifest.
    pub distilled_lora: Option<String>,
    pub t5_encoder: Option<String>,
    pub clip_encoder: Option<String>,
    pub t5_tokenizer: Option<String>,
    pub clip_tokenizer: Option<String>,
    /// CLIP-G / OpenCLIP encoder path (SDXL only)
    pub clip_encoder_2: Option<String>,
    /// CLIP-G / OpenCLIP tokenizer path (SDXL only)
    pub clip_tokenizer_2: Option<String>,
    /// Generic text encoder shard paths (Qwen3 for Z-Image)
    pub text_encoder_files: Option<Vec<String>>,
    /// Generic text encoder tokenizer path (Qwen3 for Z-Image)
    pub text_tokenizer: Option<String>,
    /// Stage B decoder weights path (Wuerstchen only)
    pub decoder: Option<String>,

    // --- generation defaults ---
    /// Default inference steps (e.g. 4 for schnell, 25 for dev)
    pub default_steps: Option<u32>,
    /// Default guidance scale (0.0 for schnell, 3.5 for dev finetuned)
    pub default_guidance: Option<f64>,
    /// Default output width
    pub default_width: Option<u32>,
    /// Default output height
    pub default_height: Option<u32>,
    /// Whether this model uses the schnell (distilled) timestep schedule.
    /// If None, auto-detected from the transformer filename.
    pub is_schnell: Option<bool>,
    /// Whether this model uses a turbo (few-step distilled) schedule.
    /// If None, auto-detected from the model name.
    pub is_turbo: Option<bool>,
    /// Scheduler algorithm for UNet-based models (SD1.5, SDXL). Ignored by flow-matching models.
    pub scheduler: Option<Scheduler>,
    /// Per-model default negative prompt for CFG-based models.
    pub negative_prompt: Option<String>,
    /// Default LoRA adapter path for this model.
    pub lora: Option<String>,
    /// Default LoRA scale for this model (0.0-2.0).
    pub lora_scale: Option<f64>,
    /// Default number of video frames for video models (e.g. 25 for ltx-video).
    pub default_frames: Option<u32>,
    /// Default video FPS for video models (e.g. 24 for ltx-video).
    pub default_fps: Option<u32>,

    // --- metadata ---
    pub description: Option<String>,
    pub family: Option<String>,

    /// Per-component device placement override. `None` preserves the
    /// engine's VRAM-aware auto-placement.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub placement: Option<crate::types::DevicePlacement>,
}

impl ModelConfig {
    /// Collect all file path strings from this model config into a flat list.
    /// Used for reference counting when determining which files are shared.
    pub fn all_file_paths(&self) -> Vec<String> {
        let mut paths = Vec::new();
        let singles = [
            &self.transformer,
            &self.vae,
            &self.spatial_upscaler,
            &self.temporal_upscaler,
            &self.distilled_lora,
            &self.t5_encoder,
            &self.clip_encoder,
            &self.t5_tokenizer,
            &self.clip_tokenizer,
            &self.clip_encoder_2,
            &self.clip_tokenizer_2,
            &self.text_tokenizer,
            &self.decoder,
        ];
        for p in singles.into_iter().flatten() {
            paths.push(p.clone());
        }
        if let Some(ref shards) = self.transformer_shards {
            paths.extend(shards.iter().cloned());
        }
        if let Some(ref files) = self.text_encoder_files {
            paths.extend(files.iter().cloned());
        }
        paths
    }

    /// Total disk usage of all model files: `(bytes, gigabytes)`.
    ///
    /// Sums the file sizes of all paths referenced by this config entry.
    /// Missing files are silently skipped.
    pub fn disk_usage(&self) -> (u64, f64) {
        let total: u64 = self
            .all_file_paths()
            .iter()
            .filter_map(|p| std::fs::metadata(p).ok())
            .map(|m| m.len())
            .sum();
        (total, total as f64 / 1_073_741_824.0)
    }

    /// Effective steps: model default → global fallback → hardcoded default.
    pub fn effective_steps(&self, global_cfg: &Config) -> u32 {
        self.default_steps.unwrap_or(global_cfg.default_steps)
    }

    /// Effective guidance.
    pub fn effective_guidance(&self) -> f64 {
        self.default_guidance.unwrap_or(3.5)
    }

    /// Effective width.
    pub fn effective_width(&self, global_cfg: &Config) -> u32 {
        self.default_width.unwrap_or(global_cfg.default_width)
    }

    /// Effective height.
    pub fn effective_height(&self, global_cfg: &Config) -> u32 {
        self.default_height.unwrap_or(global_cfg.default_height)
    }

    /// Effective negative prompt: per-model override → global default → None.
    pub fn effective_negative_prompt(&self, global_cfg: &Config) -> Option<String> {
        self.negative_prompt
            .clone()
            .or_else(|| global_cfg.default_negative_prompt.clone())
    }

    /// Effective LoRA config: per-model default path and scale, or None.
    pub fn effective_lora(&self) -> Option<(String, f64)> {
        self.lora
            .as_ref()
            .map(|path| (path.clone(), self.lora_scale.unwrap_or(1.0)))
    }

    /// Effective video frames: per-model default, or None for image-only models.
    pub fn effective_frames(&self) -> Option<u32> {
        self.default_frames
    }

    /// Effective video FPS: per-model default, or None for image-only models.
    pub fn effective_fps(&self) -> Option<u32> {
        self.default_fps
    }
}

/// Resolved model file paths.
/// For diffusion models, `transformer` and `vae` are always required.
/// For upscaler models, only `transformer` (weights) is required; `vae` is empty.
/// For utility models, only `transformer` is required; `vae` may be empty.
/// Other paths are optional — each engine validates what it needs at load time.
#[derive(Debug, Clone)]
pub struct ModelPaths {
    pub transformer: PathBuf,
    /// Multi-shard transformer paths (Z-Image BF16); empty means use single `transformer`
    pub transformer_shards: Vec<PathBuf>,
    pub vae: PathBuf,
    pub spatial_upscaler: Option<PathBuf>,
    pub temporal_upscaler: Option<PathBuf>,
    pub distilled_lora: Option<PathBuf>,
    pub t5_encoder: Option<PathBuf>,
    pub clip_encoder: Option<PathBuf>,
    pub t5_tokenizer: Option<PathBuf>,
    pub clip_tokenizer: Option<PathBuf>,
    /// CLIP-G / OpenCLIP encoder (SDXL only)
    pub clip_encoder_2: Option<PathBuf>,
    /// CLIP-G / OpenCLIP tokenizer (SDXL only)
    pub clip_tokenizer_2: Option<PathBuf>,
    /// Generic text encoder shard paths (Qwen3 for Z-Image)
    pub text_encoder_files: Vec<PathBuf>,
    /// Generic text encoder tokenizer (Qwen3 for Z-Image)
    pub text_tokenizer: Option<PathBuf>,
    /// Stage B decoder weights (Wuerstchen only)
    pub decoder: Option<PathBuf>,
}

impl ModelPaths {
    /// Resolve paths for a model. Checks config, then env vars.
    /// Returns None if transformer and VAE paths can't be resolved.
    /// All other paths are optional (depend on model family).
    pub fn resolve(model_name: &str, config: &Config) -> Option<Self> {
        if let Some(model_cfg) = config.discovered_manifest_model_config(model_name) {
            return Self::resolve_from_model_config(Some(&model_cfg));
        }

        if crate::manifest::find_manifest(model_name).is_some() && config.has_models_dir_override()
        {
            return Self::resolve_from_model_config(None);
        }

        let model_cfg = config.lookup_model_config(model_name);
        Self::resolve_from_model_config(model_cfg.as_ref())
    }

    fn resolve_from_model_config(model_cfg: Option<&ModelConfig>) -> Option<Self> {
        let transformer = Self::resolve_path(
            model_cfg.and_then(|m| m.transformer.as_deref()),
            "MOLD_TRANSFORMER_PATH",
        )?;
        let transformer_shards = model_cfg
            .and_then(|m| m.transformer_shards.as_ref())
            .map(|shards| shards.iter().map(PathBuf::from).collect())
            .unwrap_or_default();
        let vae = Self::resolve_path(model_cfg.and_then(|m| m.vae.as_deref()), "MOLD_VAE_PATH")?;
        let spatial_upscaler = Self::resolve_path(
            model_cfg.and_then(|m| m.spatial_upscaler.as_deref()),
            "MOLD_SPATIAL_UPSCALER_PATH",
        );
        let temporal_upscaler = Self::resolve_path(
            model_cfg.and_then(|m| m.temporal_upscaler.as_deref()),
            "MOLD_TEMPORAL_UPSCALER_PATH",
        );
        let distilled_lora = Self::resolve_path(
            model_cfg.and_then(|m| m.distilled_lora.as_deref()),
            "MOLD_DISTILLED_LORA_PATH",
        );
        let t5_encoder = Self::resolve_path(
            model_cfg.and_then(|m| m.t5_encoder.as_deref()),
            "MOLD_T5_PATH",
        );
        let clip_encoder = Self::resolve_path(
            model_cfg.and_then(|m| m.clip_encoder.as_deref()),
            "MOLD_CLIP_PATH",
        );
        let t5_tokenizer = Self::resolve_path(
            model_cfg.and_then(|m| m.t5_tokenizer.as_deref()),
            "MOLD_T5_TOKENIZER_PATH",
        );
        let clip_tokenizer = Self::resolve_path(
            model_cfg.and_then(|m| m.clip_tokenizer.as_deref()),
            "MOLD_CLIP_TOKENIZER_PATH",
        );
        let clip_encoder_2 = Self::resolve_path(
            model_cfg.and_then(|m| m.clip_encoder_2.as_deref()),
            "MOLD_CLIP2_PATH",
        );
        let clip_tokenizer_2 = Self::resolve_path(
            model_cfg.and_then(|m| m.clip_tokenizer_2.as_deref()),
            "MOLD_CLIP2_TOKENIZER_PATH",
        );
        let text_encoder_files = model_cfg
            .and_then(|m| m.text_encoder_files.as_ref())
            .map(|files| files.iter().map(PathBuf::from).collect())
            .unwrap_or_default();
        let text_tokenizer = Self::resolve_path(
            model_cfg.and_then(|m| m.text_tokenizer.as_deref()),
            "MOLD_TEXT_TOKENIZER_PATH",
        );
        let decoder = Self::resolve_path(
            model_cfg.and_then(|m| m.decoder.as_deref()),
            "MOLD_DECODER_PATH",
        );

        Some(Self {
            transformer,
            transformer_shards,
            vae,
            spatial_upscaler,
            temporal_upscaler,
            distilled_lora,
            t5_encoder,
            clip_encoder,
            t5_tokenizer,
            clip_tokenizer,
            clip_encoder_2,
            clip_tokenizer_2,
            text_encoder_files,
            text_tokenizer,
            decoder,
        })
    }

    fn resolve_path(config_val: Option<&str>, env_var: &str) -> Option<PathBuf> {
        if let Ok(path) = std::env::var(env_var) {
            return Some(PathBuf::from(path));
        }
        if let Some(path) = config_val {
            return Some(PathBuf::from(path));
        }
        None
    }
}

/// Current config schema version. Increment when adding migrations.
const CURRENT_CONFIG_VERSION: u32 = 1;

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Config {
    /// Config schema version for migrations. Old configs without this field
    /// default to 0 and are migrated on first load.
    #[serde(default)]
    pub config_version: u32,

    #[serde(default = "default_model")]
    pub default_model: String,

    #[serde(default = "default_models_dir")]
    pub models_dir: String,

    #[serde(default = "default_port")]
    pub server_port: u16,

    #[serde(default = "default_dimension")]
    pub default_width: u32,

    #[serde(default = "default_dimension")]
    pub default_height: u32,

    #[serde(default = "default_steps")]
    pub default_steps: u32,

    #[serde(default = "default_embed_metadata")]
    pub embed_metadata: bool,

    /// Preferred T5 encoder variant: "fp16" (default), "q8", "q6", "q5", "q4", "q3", or "auto".
    /// "auto" selects the best variant that fits in GPU VRAM.
    /// An explicit quantized tag always uses that variant regardless of VRAM.
    #[serde(default)]
    pub t5_variant: Option<String>,

    /// Preferred Qwen3 text encoder variant: "bf16" (default), "q8", "q6", "iq4", "q3", or "auto".
    /// "auto" selects the best variant that fits in GPU VRAM (with drop-and-reload).
    #[serde(default)]
    pub qwen3_variant: Option<String>,

    /// Directory to persist generated images. Default: `~/.mold/output/`.
    /// Override with `MOLD_OUTPUT_DIR` env var. Set to empty string to disable
    /// (TUI gallery will not function when disabled).
    #[serde(default)]
    pub output_dir: Option<String>,

    /// Allow roots for trusted server-local media request paths.
    /// Override with `MOLD_MEDIA_ROOTS` using the platform path-list separator.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub media_roots: Option<Vec<String>>,

    /// Global default negative prompt for CFG-based models (SD1.5, SDXL, SD3, Wuerstchen).
    /// Overridden by per-model `negative_prompt` or CLI `--negative-prompt`.
    #[serde(default)]
    pub default_negative_prompt: Option<String>,

    /// Prompt expansion settings.
    #[serde(default)]
    pub expand: ExpandSettings,

    /// Logging configuration.
    #[serde(default)]
    pub logging: LoggingConfig,

    /// RunPod integration settings (api key, defaults, auto-teardown behaviour).
    #[serde(default)]
    pub runpod: crate::runpod::RunPodSettings,

    /// Lambda Cloud integration settings.
    #[serde(default)]
    pub lambda: crate::lambda::LambdaSettings,

    /// GPU ordinals to use (None = all available).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub gpus: Option<Vec<usize>>,

    /// Max queued requests before 503 (default: 200).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub queue_size: Option<usize>,

    /// Per-model configurations, keyed by model name.
    #[serde(default)]
    pub models: HashMap<String, ModelConfig>,
}

/// Logging configuration for file output and rotation.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct LoggingConfig {
    /// Log level: trace, debug, info, warn, error. Overridden by MOLD_LOG env var.
    #[serde(default = "default_log_level")]
    pub level: String,

    /// Enable file logging. When true, logs go to ~/.mold/logs/.
    #[serde(default)]
    pub file: bool,

    /// Custom log file directory (default: ~/.mold/logs/).
    #[serde(default)]
    pub dir: Option<String>,

    /// Number of days to retain log files. Default: 7.
    #[serde(default = "default_log_max_days")]
    pub max_days: u32,
}

fn default_log_level() -> String {
    "info".to_string()
}
fn default_log_max_days() -> u32 {
    7
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: default_log_level(),
            file: false,
            dir: None,
            max_days: default_log_max_days(),
        }
    }
}

fn default_model() -> String {
    "flux2-klein:q8".to_string()
}

fn default_models_dir() -> String {
    if let Ok(home) = std::env::var("MOLD_HOME") {
        format!("{home}/models")
    } else {
        "~/.mold/models".to_string()
    }
}

fn default_port() -> u16 {
    7680
}

fn default_dimension() -> u32 {
    768
}

fn default_steps() -> u32 {
    4
}

fn default_embed_metadata() -> bool {
    true
}

impl Default for Config {
    fn default() -> Self {
        Self {
            config_version: CURRENT_CONFIG_VERSION,
            default_model: default_model(),
            models_dir: default_models_dir(),
            server_port: default_port(),
            default_width: default_dimension(),
            default_height: default_dimension(),
            default_steps: default_steps(),
            embed_metadata: default_embed_metadata(),
            t5_variant: None,
            qwen3_variant: None,
            output_dir: None,
            media_roots: None,
            default_negative_prompt: None,
            expand: ExpandSettings::default(),
            logging: LoggingConfig::default(),
            runpod: crate::runpod::RunPodSettings::default(),
            lambda: crate::lambda::LambdaSettings::default(),
            gpus: None,
            queue_size: None,
            models: HashMap::new(),
        }
    }
}

impl Config {
    /// Build a `GpuSelection` from the config's `gpus` field.
    pub fn gpu_selection(&self) -> crate::types::GpuSelection {
        match &self.gpus {
            Some(ordinals) if !ordinals.is_empty() => {
                crate::types::GpuSelection::Specific(ordinals.clone())
            }
            _ => crate::types::GpuSelection::All,
        }
    }

    /// Return the configured queue size or the default (200).
    pub fn queue_size(&self) -> usize {
        self.queue_size.unwrap_or(200)
    }

    pub fn install_runtime_models_dir_override(models_dir: PathBuf) {
        let _ = RUNTIME_MODELS_DIR_OVERRIDE.get_or_init(|| models_dir);
    }

    pub fn load_or_default() -> Self {
        let Some(config_path) = Self::config_path() else {
            eprintln!("warning: could not determine home directory — using default config");
            return Config::default();
        };
        let mut cfg = if config_path.exists() {
            match std::fs::read_to_string(&config_path) {
                Ok(contents) => match toml::from_str(&contents) {
                    Ok(cfg) => cfg,
                    Err(e) => {
                        eprintln!(
                            "warning: failed to parse config at {}: {e} — using defaults",
                            config_path.display()
                        );
                        Config::default()
                    }
                },
                Err(e) => {
                    eprintln!(
                        "warning: failed to read config at {}: {e} — using defaults",
                        config_path.display()
                    );
                    Config::default()
                }
            }
        } else {
            Config::default()
        };

        // Run config migrations if needed
        if cfg.config_version < CURRENT_CONFIG_VERSION {
            Self::run_migrations(&mut cfg);
            cfg.config_version = CURRENT_CONFIG_VERSION;
            if let Err(e) = cfg.save() {
                eprintln!("warning: failed to save migrated config: {e}");
            }
        }

        // Post-load hook (DB-backed user-pref overlay, if installed).
        if let Some(hook) = POST_LOAD_HOOK.get() {
            hook(&mut cfg);
        }

        cfg
    }

    /// Run all pending config migrations from cfg.config_version to CURRENT.
    pub(crate) fn run_migrations(cfg: &mut Config) {
        if cfg.config_version < 1 {
            Self::migrate_v0_to_v1(cfg);
        }
        // Future migrations:
        // if cfg.config_version < 2 { Self::migrate_v1_to_v2(cfg); }
    }

    /// v0 → v1: Strip stale manifest defaults from known model entries.
    ///
    /// Old `mold pull` wrote all manifest defaults (steps, guidance, dimensions,
    /// description, family, is_schnell, scheduler) into config.toml. These become
    /// stale when manifests update. This migration removes them so
    /// `resolved_model_config()` reads fresh values from the manifest at runtime.
    fn migrate_v0_to_v1(cfg: &mut Config) {
        let model_names: Vec<String> = cfg.models.keys().cloned().collect();
        for name in model_names {
            if crate::manifest::find_manifest(&name).is_some() {
                if let Some(mc) = cfg.models.get_mut(&name) {
                    mc.default_steps = None;
                    mc.default_guidance = None;
                    mc.default_width = None;
                    mc.default_height = None;
                    mc.is_schnell = None;
                    mc.is_turbo = None;
                    mc.scheduler = None;
                    mc.negative_prompt = None;
                    mc.default_frames = None;
                    mc.default_fps = None;
                    mc.description = None;
                    mc.family = None;
                }
            }
        }
        eprintln!("config: migrated v0 → v1 (cleared stale manifest defaults)");
    }

    /// Reload config from disk while preserving runtime-only overrides.
    pub fn reload_from_disk_preserving_runtime(&self) -> Self {
        let mut fresh = Self::load_or_default();
        fresh.models_dir = self.models_dir.clone();
        fresh
    }

    /// The root mold directory.
    /// Resolution: `MOLD_HOME` env var → `~/.mold/` → `./.mold` (if HOME unset).
    pub fn mold_dir() -> Option<PathBuf> {
        if let Ok(home) = std::env::var("MOLD_HOME") {
            return Some(PathBuf::from(home));
        }
        Some(
            dirs::home_dir()
                .unwrap_or_else(|| PathBuf::from("."))
                .join(".mold"),
        )
    }

    pub fn config_path() -> Option<PathBuf> {
        Self::mold_dir().map(|d| d.join("config.toml"))
    }

    pub fn data_dir() -> Option<PathBuf> {
        Self::mold_dir()
    }

    pub fn resolved_models_dir(&self) -> PathBuf {
        if let Some(models_dir) = RUNTIME_MODELS_DIR_OVERRIDE.get() {
            return models_dir.clone();
        }
        if let Ok(env_dir) = std::env::var("MOLD_MODELS_DIR") {
            PathBuf::from(env_dir)
        } else {
            let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
            let expanded = self.models_dir.replace("~", &home.to_string_lossy());
            PathBuf::from(expanded)
        }
    }

    pub fn has_models_dir_override(&self) -> bool {
        RUNTIME_MODELS_DIR_OVERRIDE.get().is_some() || std::env::var_os("MOLD_MODELS_DIR").is_some()
    }

    /// Resolve the effective default model with idiot-proof fallback chain:
    /// 1. `MOLD_DEFAULT_MODEL` env var (if set and non-empty)
    /// 2. Config file `default_model` (if that model has a custom `[models]` entry)
    /// 3. Config file `default_model` (if that model is a known manifest model that is downloaded)
    /// 4. Last-used model from `$MOLD_HOME/last-model` (if downloaded)
    /// 5. If exactly one model is downloaded, use it automatically
    /// 6. Fall back to config value (will trigger auto-pull on use)
    pub fn resolved_default_model(&self) -> String {
        self.resolve_default_model().model
    }

    /// Like [`resolved_default_model`] but also returns which fallback step resolved it.
    pub fn resolve_default_model(&self) -> DefaultModelResolution {
        // 1. Env var override
        if let Ok(m) = std::env::var("MOLD_DEFAULT_MODEL") {
            if !m.is_empty() {
                return DefaultModelResolution {
                    model: m,
                    source: DefaultModelSource::EnvVar,
                };
            }
        }
        // 2. Explicit config entry — honor custom/manual models even when not manifest-backed.
        let configured = &self.default_model;
        if self.lookup_model_config(configured).is_some() {
            return DefaultModelResolution {
                model: configured.clone(),
                source: DefaultModelSource::ConfigCustomEntry,
            };
        }
        // 3. Configured manifest model — if downloaded
        if self.manifest_model_is_downloaded(configured) {
            return DefaultModelResolution {
                model: configured.clone(),
                source: DefaultModelSource::Config,
            };
        }
        // 4. Last-used model — if still downloaded
        if let Some(last) = Self::read_last_model() {
            if self.manifest_model_is_downloaded(&last) {
                return DefaultModelResolution {
                    model: last,
                    source: DefaultModelSource::LastUsed,
                };
            }
        }
        // 5. Single downloaded model (exclude utility and upscaler models)
        let downloaded: Vec<String> = crate::manifest::known_manifests()
            .iter()
            .filter(|m| {
                !m.is_utility() && !m.is_upscaler() && self.manifest_model_is_downloaded(&m.name)
            })
            .map(|m| m.name.clone())
            .collect();
        if downloaded.len() == 1 {
            return DefaultModelResolution {
                model: downloaded.into_iter().next().unwrap(),
                source: DefaultModelSource::OnlyDownloaded,
            };
        }
        // 6. Config default (will auto-pull) — resolve bare names like
        //    "flux2-klein" → "flux2-klein:q8" so the TUI/CLI show the real tag.
        DefaultModelResolution {
            model: crate::manifest::resolve_model_name(configured),
            source: DefaultModelSource::ConfigDefault,
        }
    }

    /// Path to the last-model state file: `$MOLD_HOME/last-model`
    fn last_model_path() -> Option<PathBuf> {
        Self::mold_dir().map(|d| d.join("last-model"))
    }

    /// Read the last-used model. When the DB-backed read hook is
    /// installed (production path), defers to it entirely; otherwise
    /// reads the legacy `$MOLD_HOME/last-model` sidecar. Callers doing
    /// one-shot sidecar migration should use
    /// [`Self::read_last_model_from_sidecar`] directly.
    pub fn read_last_model() -> Option<String> {
        if let Some(hook) = READ_LAST_MODEL_HOOK.get() {
            return hook();
        }
        Self::read_last_model_from_sidecar()
    }

    /// Read the legacy `$MOLD_HOME/last-model` sidecar directly. Used by
    /// the one-shot `config.toml + sidecar → DB` migration.
    pub fn read_last_model_from_sidecar() -> Option<String> {
        let path = Self::last_model_path()?;
        std::fs::read_to_string(path).ok().and_then(|s| {
            let trimmed = s.trim().to_string();
            if trimmed.is_empty() {
                None
            } else {
                Some(trimmed)
            }
        })
    }

    /// Resolve the output directory for server-mode image persistence.
    /// `MOLD_OUTPUT_DIR` env var takes precedence over the config file value.
    /// Returns `None` when disabled (default).
    pub fn resolved_output_dir(&self) -> Option<PathBuf> {
        let raw = if let Ok(env_dir) = std::env::var("MOLD_OUTPUT_DIR") {
            if env_dir.is_empty() {
                None
            } else {
                Some(env_dir)
            }
        } else {
            self.output_dir.clone().filter(|s| !s.is_empty())
        };
        raw.map(|dir| {
            let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
            if dir == "~" {
                home
            } else if let Some(rest) = dir.strip_prefix("~/") {
                home.join(rest)
            } else {
                PathBuf::from(dir)
            }
        })
    }

    /// Check if image output has been explicitly disabled by the user
    /// (empty `MOLD_OUTPUT_DIR` env var or empty `output_dir` config field).
    pub fn is_output_disabled(&self) -> bool {
        if let Ok(env_dir) = std::env::var("MOLD_OUTPUT_DIR") {
            return env_dir.is_empty();
        }
        matches!(self.output_dir.as_deref(), Some(""))
    }

    /// Resolved output directory with a default fallback to `~/.mold/output/`.
    /// Unlike `resolved_output_dir()`, this always returns a path.
    pub fn effective_output_dir(&self) -> PathBuf {
        self.resolved_output_dir().unwrap_or_else(|| {
            Self::mold_dir()
                .unwrap_or_else(|| PathBuf::from(".mold"))
                .join("output")
        })
    }

    pub fn resolved_media_roots(&self) -> Vec<PathBuf> {
        if let Ok(roots) = std::env::var("MOLD_MEDIA_ROOTS") {
            return crate::parse_media_roots_env(&roots);
        }
        self.media_roots
            .as_deref()
            .map(crate::configured_media_roots)
            .unwrap_or_default()
    }

    /// Resolved log directory from config or default (~/.mold/logs/).
    pub fn resolved_log_dir(&self) -> PathBuf {
        if let Some(ref dir) = self.logging.dir {
            let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
            if dir == "~" {
                home
            } else if let Some(rest) = dir.strip_prefix("~/") {
                home.join(rest)
            } else {
                PathBuf::from(dir)
            }
        } else {
            Self::mold_dir()
                .unwrap_or_else(|| PathBuf::from(".mold"))
                .join("logs")
        }
    }

    pub fn effective_embed_metadata(&self, override_value: Option<bool>) -> bool {
        if let Some(value) = override_value {
            return value;
        }

        match std::env::var("MOLD_EMBED_METADATA") {
            Ok(value) => match value.trim().to_ascii_lowercase().as_str() {
                "1" | "true" | "yes" | "on" => true,
                "0" | "false" | "no" | "off" => false,
                _ => {
                    eprintln!(
                        "warning: invalid MOLD_EMBED_METADATA value '{value}' — using config/default"
                    );
                    self.embed_metadata
                }
            },
            Err(_) => self.embed_metadata,
        }
    }

    pub fn discovered_manifest_paths(&self, name: &str) -> Option<ModelPaths> {
        let manifest = crate::manifest::find_manifest(name)?;
        if self.incomplete_pull_blocks_manifest(manifest) {
            return None;
        }
        let models_dir = self.resolved_models_dir();
        let downloads = manifest
            .files
            .iter()
            .map(|file| {
                // Prefer a canonical clean-path hit (or a documented legacy
                // path) under the models dir.
                let local = crate::manifest::storage_path_candidates(manifest, file)
                    .into_iter()
                    .map(|path| models_dir.join(path))
                    // Same completeness rules as `manifest_files_exist`:
                    // marker present OR size matches manifest. Plain
                    // `.exists()` here let truncated downloads masquerade
                    // as installed models — the gallery race lived here.
                    .find(|path| Self::file_is_complete(path, file.size_bytes));
                if let Some(path) = local {
                    return Some((file.component, path));
                }
                // Fallback (companion manifests only): walk mold's managed
                // `<models_dir>/.hf-cache/` for the same file. A previous
                // mold install may have placed companion files under a
                // different canonical layout — e.g. Gemma TE under
                // `shared/ltx2/...` from a manifest LTX-2 model install
                // vs the catalog `ltx2-te` companion expecting
                // `shared/companion/...`. Letting the layout-agnostic
                // hf-hub cache view satisfy either keeps a single Gemma
                // download serving both. Restricted to `family ==
                // "companion"` so non-companion manifests still require
                // their files at the canonical clean-path location with
                // the proper completeness guard above — that preserves
                // the "model not downloaded" branch the tests rely on.
                if manifest.family != "companion" {
                    return None;
                }
                crate::download::cached_file_path_in_mold_cache(&file.hf_repo, &file.hf_filename)
                    .map(|path| (file.component, path))
            })
            .collect::<Option<Vec<_>>>()?;
        crate::manifest::paths_from_downloads(&downloads, &manifest.family)
    }

    pub fn manifest_model_is_downloaded(&self, name: &str) -> bool {
        let manifest = match crate::manifest::find_manifest(name) {
            Some(m) => m,
            None => return false,
        };
        if self.incomplete_pull_blocks_manifest(manifest) {
            return false;
        }
        // Upscaler and utility models don't produce full ModelPaths (no VAE).
        // Check file existence directly instead of going through ModelPaths::resolve.
        if manifest.is_upscaler() || manifest.is_utility() {
            return self.manifest_files_exist(manifest);
        }
        self.resolved_local_manifest_model_config(name).is_some()
    }

    /// Return true when a known manifest-backed model is missing any required
    /// downloadable asset and should be repaired with `mold pull`.
    pub fn manifest_model_needs_download(&self, name: &str) -> bool {
        let canonical = crate::manifest::resolve_model_name(name);
        crate::manifest::find_manifest(&canonical).is_some()
            && !self.manifest_model_is_downloaded(&canonical)
    }

    /// Check whether all files for a manifest are completely on disk.
    ///
    /// Two acceptance signals (a file is considered complete if **either** holds):
    ///
    /// 1. A `.sha256-verified` sidecar exists. Written by the pull path on a
    ///    successful download — positive proof the file finished writing and,
    ///    when the manifest declared a hash, matches it.
    /// 2. The on-disk size matches the manifest's declared `size_bytes`.
    ///    Covers two legitimate cases without forcing an upgrade-day rehash:
    ///    legacy installs created before markers were written, and HF cache
    ///    symlinks pointing at fully-downloaded blobs in `~/.cache/huggingface`.
    ///
    /// Truncated / partial files reject under both signals — they have no
    /// marker (because no successful verify ever ran) and their size does not
    /// match. That's the load-bearing change for the "downloaded model
    /// sometimes doesn't show up" gallery race.
    fn manifest_files_exist(&self, manifest: &crate::manifest::ModelManifest) -> bool {
        let models_dir = self.resolved_models_dir();
        manifest.files.iter().all(|file| {
            crate::manifest::storage_path_candidates(manifest, file)
                .into_iter()
                .map(|path| models_dir.join(path))
                .any(|path| Self::file_is_complete(&path, file.size_bytes))
        })
    }

    /// True when the on-disk file at `path` should be treated as a fully
    /// downloaded artifact. See [`Self::manifest_files_exist`] for the
    /// acceptance rules.
    fn file_is_complete(path: &std::path::Path, expected_size: u64) -> bool {
        if !path.exists() {
            return false;
        }
        if crate::download::has_sha256_marker(path) {
            return true;
        }
        // Marker missing — fall back to size match against the manifest.
        // Symlinks (HF cache) follow through to the target's metadata.
        match path.metadata() {
            Ok(meta) => meta.len() == expected_size,
            Err(_) => false,
        }
    }

    /// Return true when an active `.pulling` marker should block manifest discovery.
    ///
    /// If all manifest files already exist, the marker is stale (for example a
    /// prior pull finished but crashed before marker cleanup). In that case we
    /// remove it and continue with manifest-derived paths instead of falling
    /// back to potentially stale config entries.
    fn incomplete_pull_blocks_manifest(&self, manifest: &crate::manifest::ModelManifest) -> bool {
        let marker_path =
            crate::download::pulling_marker_path_in(&self.resolved_models_dir(), &manifest.name);
        if !marker_path.exists() {
            return false;
        }

        if self.manifest_files_exist(manifest) {
            let _ = std::fs::remove_file(&marker_path);
            return false;
        }

        true
    }

    /// Return the ModelConfig for a given model name, or an empty default.
    /// Tries the exact name first, then the canonical `name:tag` form.
    pub fn model_config(&self, name: &str) -> ModelConfig {
        let mut cfg = self.lookup_model_config(name).unwrap_or_default();

        if let Some(discovered) = self.resolved_local_manifest_model_config(name) {
            overlay_model_paths(&mut cfg, &discovered);
            if cfg.description.is_none() {
                cfg.description = discovered.description;
            }
            if cfg.family.is_none() {
                cfg.family = discovered.family;
            }
        }

        cfg
    }

    /// Return a model config merged with manifest defaults and metadata.
    pub fn resolved_model_config(&self, name: &str) -> ModelConfig {
        let mut cfg = self.model_config(name);

        if let Some(manifest) = crate::manifest::find_manifest(name) {
            // Manifest provides defaults when the config file doesn't specify them.
            // Since to_model_config() no longer writes manifest defaults to config,
            // config values are only Some when the user explicitly set them.
            // User overrides are preserved; manifest fills in the rest.
            if cfg.default_steps.is_none() {
                cfg.default_steps = Some(manifest.defaults.steps);
            }
            if cfg.default_guidance.is_none() {
                cfg.default_guidance = Some(manifest.defaults.guidance);
            }
            if cfg.default_width.is_none() {
                cfg.default_width = Some(manifest.defaults.width);
            }
            if cfg.default_height.is_none() {
                cfg.default_height = Some(manifest.defaults.height);
            }
            if cfg.is_schnell.is_none() {
                cfg.is_schnell = Some(manifest.defaults.is_schnell);
            }
            if cfg.scheduler.is_none() {
                cfg.scheduler = manifest.defaults.scheduler;
            }
            if cfg.negative_prompt.is_none() {
                cfg.negative_prompt = manifest.defaults.negative_prompt.clone();
            }
            if cfg.default_frames.is_none() {
                cfg.default_frames = manifest.defaults.frames;
            }
            if cfg.default_fps.is_none() {
                cfg.default_fps = manifest.defaults.fps;
            }
            // Description and family always come from the manifest for known models.
            // These are metadata, not user-configurable settings.
            cfg.description = Some(manifest.description.clone());
            cfg.family = Some(manifest.family.clone());
        }

        cfg
    }

    /// Insert or update a model configuration entry.
    pub fn upsert_model(&mut self, name: String, config: ModelConfig) {
        self.models.insert(name, config);
    }

    /// Remove a model entry from the config, returning it if it existed.
    pub fn remove_model(&mut self, name: &str) -> Option<ModelConfig> {
        self.models.remove(name)
    }

    /// Return the effective placement for a model: config entry plus env overrides.
    ///
    /// Precedence (higher wins):
    ///   1. `MOLD_PLACE_TRANSFORMER`, `MOLD_PLACE_VAE`, `MOLD_PLACE_TEXT_ENCODERS`,
    ///      `MOLD_PLACE_T5`, `MOLD_PLACE_CLIP_L`, `MOLD_PLACE_CLIP_G`,
    ///      `MOLD_PLACE_QWEN` (env overrides per-component).
    ///   2. Config file `[models."name:tag".placement]` table.
    ///   3. `None` (use engine auto).
    ///
    /// Each env var parses:
    ///   - `"auto"`    — `DeviceRef::Auto`
    ///   - `"cpu"`     — `DeviceRef::Cpu`
    ///   - `"gpu:N"`   — `DeviceRef::Gpu { ordinal: N }`
    ///   - `"gpu"`     — `DeviceRef::Gpu { ordinal: 0 }`
    pub fn resolved_placement(&self, model_name: &str) -> Option<crate::types::DevicePlacement> {
        use crate::types::DevicePlacement;

        let mut placement = self
            .lookup_model_config(model_name)
            .and_then(|mc| mc.placement);

        let env_tier1 = parse_device_ref_env("MOLD_PLACE_TEXT_ENCODERS");
        let env_transformer = parse_device_ref_env("MOLD_PLACE_TRANSFORMER");
        let env_vae = parse_device_ref_env("MOLD_PLACE_VAE");
        let env_t5 = parse_device_ref_env("MOLD_PLACE_T5");
        let env_clip_l = parse_device_ref_env("MOLD_PLACE_CLIP_L");
        let env_clip_g = parse_device_ref_env("MOLD_PLACE_CLIP_G");
        let env_qwen = parse_device_ref_env("MOLD_PLACE_QWEN");

        let any_env = env_tier1.is_some()
            || env_transformer.is_some()
            || env_vae.is_some()
            || env_t5.is_some()
            || env_clip_l.is_some()
            || env_clip_g.is_some()
            || env_qwen.is_some();

        if !any_env {
            return placement;
        }

        let mut effective: DevicePlacement = placement.unwrap_or_default();
        if let Some(r) = env_tier1 {
            effective.text_encoders = r;
        }
        let any_advanced = env_transformer.is_some()
            || env_vae.is_some()
            || env_t5.is_some()
            || env_clip_l.is_some()
            || env_clip_g.is_some()
            || env_qwen.is_some();
        if any_advanced {
            let mut adv = effective.advanced.unwrap_or_default();
            if let Some(r) = env_transformer {
                adv.transformer = r;
            }
            if let Some(r) = env_vae {
                adv.vae = r;
            }
            if let Some(r) = env_t5 {
                adv.t5 = Some(r);
            }
            if let Some(r) = env_clip_l {
                adv.clip_l = Some(r);
            }
            if let Some(r) = env_clip_g {
                adv.clip_g = Some(r);
            }
            if let Some(r) = env_qwen {
                adv.qwen = Some(r);
            }
            effective.advanced = Some(adv);
        }
        placement = Some(effective);
        placement
    }

    /// Persist a placement for `model_name`, creating the model entry if
    /// missing. `None` clears the placement (and leaves the rest of the
    /// entry intact).
    pub fn set_model_placement(
        &mut self,
        model_name: &str,
        placement: Option<crate::types::DevicePlacement>,
    ) {
        let entry = self.models.entry(model_name.to_string()).or_default();
        entry.placement = placement;
    }

    /// Write the config to disk at `config_path()`.
    ///
    /// Safety: refuses to save if `models_dir` points to a temp/test directory,
    /// which can happen when tests race on the `MOLD_HOME` env var.
    pub fn save(&self) -> anyhow::Result<()> {
        let path = Self::config_path()
            .ok_or_else(|| anyhow::anyhow!("cannot determine home directory for config path"))?;

        // Guard: refuse to persist a config with a test-temp models_dir into a
        // non-temp config path. This catches the race condition where parallel tests
        // set MOLD_HOME to /tmp/... and a config.save() writes the corrupted
        // models_dir to the user's real config file.
        let path_str = path.to_string_lossy();
        let is_temp_config = path_str.contains("/tmp/") || path_str.contains("/mold-config-test-");
        let has_temp_models_dir = self.models_dir.contains("/tmp/mold-")
            || self.models_dir.contains("/mold-config-test-");
        if has_temp_models_dir && !is_temp_config {
            eprintln!(
                "warning: refusing to save config with test models_dir ({}) to real config ({})",
                self.models_dir,
                path.display()
            );
            return Ok(());
        }

        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let contents = toml::to_string_pretty(self)?;
        std::fs::write(&path, contents)?;
        Ok(())
    }

    /// Serialize only the bootstrap/ops slice of the config to TOML:
    /// identifiers, paths, ports, credentials, logging, runpod, per-model
    /// file-path entries. User-preference fields that now live in the DB
    /// (`expand.*`, `generate.*` globals, per-model generation defaults,
    /// lora, scheduler) are stripped.
    ///
    /// Used by the post-migration rewrite to keep `config.toml` honest
    /// after the user-preference surface has moved to SQLite.
    pub fn save_bootstrap_only_to(&self, path: &std::path::Path) -> anyhow::Result<()> {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let mut doc = toml::Value::try_from(self)?;
        strip_user_pref_fields(&mut doc);
        let body = toml::to_string_pretty(&doc)?;
        let contents = format!("{BOOTSTRAP_ONLY_BANNER}{body}");
        std::fs::write(path, contents)?;
        Ok(())
    }

    /// Save the bootstrap-only slice to the default config path.
    pub fn save_bootstrap_only(&self) -> anyhow::Result<()> {
        let path = Self::config_path()
            .ok_or_else(|| anyhow::anyhow!("cannot determine home directory for config path"))?;
        self.save_bootstrap_only_to(&path)
    }

    /// Whether a config file exists on disk.
    pub fn exists_on_disk() -> bool {
        Self::config_path().is_some_and(|p| p.exists())
    }

    /// Look up a model config entry by name (exact or canonical `name:tag` form).
    /// Public so CLI commands can check whether a model has a custom config entry.
    pub fn lookup_model_config(&self, name: &str) -> Option<ModelConfig> {
        if let Some(cfg) = self.models.get(name) {
            return Some(cfg.clone());
        }
        let canonical = resolve_model_name(name);
        if canonical != name {
            return self.models.get(&canonical).cloned();
        }
        None
    }

    fn discovered_manifest_model_config(&self, name: &str) -> Option<ModelConfig> {
        let manifest = crate::manifest::find_manifest(name)?;
        let paths = self.discovered_manifest_paths(name)?;
        Some(manifest.to_model_config(&paths))
    }

    fn resolved_local_manifest_model_config(&self, name: &str) -> Option<ModelConfig> {
        let manifest = crate::manifest::find_manifest(name)?;
        let paths = if let Some(paths) = self.discovered_manifest_paths(name) {
            paths
        } else {
            let paths = ModelPaths::resolve(name, self)?;
            if !resolved_manifest_paths_exist(manifest, &paths) {
                return None;
            }
            paths
        };
        Some(manifest.to_model_config(&paths))
    }
}

fn overlay_model_paths(target: &mut ModelConfig, source: &ModelConfig) {
    target.transformer = source.transformer.clone();
    target.transformer_shards = source.transformer_shards.clone();
    target.vae = source.vae.clone();
    if source.spatial_upscaler.is_some() {
        target.spatial_upscaler = source.spatial_upscaler.clone();
    }
    if source.temporal_upscaler.is_some() {
        target.temporal_upscaler = source.temporal_upscaler.clone();
    }
    if source.distilled_lora.is_some() {
        target.distilled_lora = source.distilled_lora.clone();
    }

    if source.t5_encoder.is_some() {
        target.t5_encoder = source.t5_encoder.clone();
    }
    if source.clip_encoder.is_some() {
        target.clip_encoder = source.clip_encoder.clone();
    }
    if source.t5_tokenizer.is_some() {
        target.t5_tokenizer = source.t5_tokenizer.clone();
    }
    if source.clip_tokenizer.is_some() {
        target.clip_tokenizer = source.clip_tokenizer.clone();
    }
    if source.clip_encoder_2.is_some() {
        target.clip_encoder_2 = source.clip_encoder_2.clone();
    }
    if source.clip_tokenizer_2.is_some() {
        target.clip_tokenizer_2 = source.clip_tokenizer_2.clone();
    }
    if source.text_encoder_files.is_some() {
        target.text_encoder_files = source.text_encoder_files.clone();
    }
    if source.text_tokenizer.is_some() {
        target.text_tokenizer = source.text_tokenizer.clone();
    }
    if source.decoder.is_some() {
        target.decoder = source.decoder.clone();
    }
}

fn resolved_manifest_paths_exist(
    manifest: &crate::manifest::ModelManifest,
    paths: &ModelPaths,
) -> bool {
    use crate::manifest::ModelComponent;

    let mut transformer_shard_idx = 0usize;
    let mut text_encoder_idx = 0usize;

    manifest.files.iter().all(|file| match file.component {
        ModelComponent::Transformer => paths.transformer.exists(),
        ModelComponent::TransformerShard => {
            let path = paths.transformer_shards.get(transformer_shard_idx);
            transformer_shard_idx += 1;
            path.is_some_and(|path| path.exists())
        }
        ModelComponent::Vae => paths.vae.exists(),
        ModelComponent::SpatialUpscaler => paths
            .spatial_upscaler
            .as_ref()
            .is_some_and(|path| path.exists()),
        ModelComponent::TemporalUpscaler => paths
            .temporal_upscaler
            .as_ref()
            .is_some_and(|path| path.exists()),
        ModelComponent::DistilledLora => paths
            .distilled_lora
            .as_ref()
            .is_some_and(|path| path.exists()),
        ModelComponent::T5Encoder => paths.t5_encoder.as_ref().is_some_and(|path| path.exists()),
        ModelComponent::ClipEncoder => paths
            .clip_encoder
            .as_ref()
            .is_some_and(|path| path.exists()),
        ModelComponent::T5Tokenizer => paths
            .t5_tokenizer
            .as_ref()
            .is_some_and(|path| path.exists()),
        ModelComponent::ClipTokenizer => paths
            .clip_tokenizer
            .as_ref()
            .is_some_and(|path| path.exists()),
        ModelComponent::ClipEncoder2 => paths
            .clip_encoder_2
            .as_ref()
            .is_some_and(|path| path.exists()),
        ModelComponent::ClipTokenizer2 => paths
            .clip_tokenizer_2
            .as_ref()
            .is_some_and(|path| path.exists()),
        ModelComponent::TextEncoder => {
            let path = paths.text_encoder_files.get(text_encoder_idx);
            text_encoder_idx += 1;
            path.is_some_and(|path| path.exists())
        }
        ModelComponent::TextTokenizer => paths
            .text_tokenizer
            .as_ref()
            .is_some_and(|path| path.exists()),
        ModelComponent::Decoder => paths.decoder.as_ref().is_some_and(|path| path.exists()),
        ModelComponent::Upscaler => paths.transformer.exists(),
    })
}

/// Parse a device-placement string (`auto`, `cpu`, `gpu`, `gpu:N`) into a
/// `DeviceRef`. Case-insensitive, whitespace-trimmed. Used by env-var and CLI
/// parsers alike so all three surfaces (TOML, env, CLI flag) accept the same
/// forms.
pub fn parse_device_ref_str(raw: &str) -> Result<crate::types::DeviceRef, String> {
    use crate::types::DeviceRef;
    let raw = raw.trim().to_lowercase();
    if raw == "auto" {
        Ok(DeviceRef::Auto)
    } else if raw == "cpu" {
        Ok(DeviceRef::Cpu)
    } else if raw == "gpu" {
        Ok(DeviceRef::gpu(0))
    } else if let Some(rest) = raw.strip_prefix("gpu:") {
        rest.parse::<usize>()
            .map(DeviceRef::gpu)
            .map_err(|_| format!("invalid device '{raw}' (expected auto|cpu|gpu[:N])"))
    } else {
        Err(format!(
            "invalid device '{raw}' (expected auto|cpu|gpu[:N])"
        ))
    }
}

fn parse_device_ref_env(key: &str) -> Option<crate::types::DeviceRef> {
    let raw = std::env::var(key).ok()?;
    match parse_device_ref_str(&raw) {
        Ok(dr) => Some(dr),
        Err(msg) => {
            eprintln!("mold: ignoring {key}={raw}: {msg}");
            None
        }
    }
}