inferd-daemon 0.2.1

The inferd daemon: NDJSON-over-IPC server, admission queue, single-instance lock, router, activity log.
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
//! Operator-facing JSON config file.
//!
//! Default location: `~/.inferd/config.json` (Unix) /
//! `%USERPROFILE%\.inferd\config.json` (Windows). Override via
//! `--config` CLI flag or `INFERD_CONFIG` env var.
//!
//! # Schema (single-backend, legacy)
//!
//! ```json
//! {
//!   "auto_pull": true,
//!   "models_home": "~/.local/share/models",
//!   "model": {
//!     "name":       "gemma-4-e4b",
//!     "sha256":     "30d1e7949597a3446726064e80b876fd1b5cba4aa6eec53d27afa420e731fb36",
//!     "size_bytes": 5126304928,
//!     "source_url": "https://huggingface.co/unsloth/.../resolve/main/...gguf",
//!     "license":    "apache-2.0"
//!   },
//!   "n_ctx":         8192,
//!   "n_gpu_layers":  0,
//!   "admin_addr":    "/run/inferd/admin.sock"
//! }
//! ```
//!
//! # Schema (multi-backend, v0.2+)
//!
//! Per ADR 0007, the router walks an *ordered* list of backends; the
//! first that's `ready()` and not currently circuit-broken serves the
//! request. The config-file surface mirrors that:
//!
//! ```json
//! {
//!   "models_home": "~/.local/share/models",
//!   "backends": [
//!     {
//!       "kind": "llamacpp",
//!       "name": "local-gemma",
//!       "model": { "name": "gemma-4-e4b", "sha256": "...", "source_url": "https://...gguf" },
//!       "n_ctx": 8192,
//!       "n_gpu_layers": 35
//!     },
//!     {
//!       "kind": "openai-compat",
//!       "name": "anthropic-fallback",
//!       "base_url": "https://api.anthropic.com",
//!       "model": "claude-opus-4-7",
//!       "api_key_env": "ANTHROPIC_API_KEY",
//!       "timeout_secs": 300
//!     }
//!   ]
//! }
//! ```
//!
//! `backends:` and `model:` are mutually exclusive. When `model:` is
//! present (legacy single-backend shape), the daemon promotes it to a
//! one-element `backends:` list with `kind: "llamacpp"` so existing
//! v0.1.x configs keep working without edits.
//!
//! API keys for `openai-compat` are referenced by env-var **name**
//! via `api_key_env:` — never embedded literally in the file. The
//! daemon reads the named env at startup. When `api_key_env:` is
//! absent, falls back to `INFERD_OPENAI_API_KEY`, then `OPENAI_API_KEY`,
//! then empty (skips `Authorization` for self-hosted endpoints).
//!
//! The `kind:` field is an open-ended tagged union: future variants
//! (`bedrock-invoke`, `bedrock-converse`, etc.) slot in additively
//! without breaking existing configs.
//!
//! Resolution order for the model store (per ADR 0011):
//!
//! 1. `models_home` field if set in this config.
//! 2. `MODELS_HOME` env var.
//! 3. Platform default (XDG / Application Support / LOCALAPPDATA).
//!
//! CLI flags override config-file values when both are present.

use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::{self, BufReader};
use std::path::{Path, PathBuf};

/// Top-level config-file schema.
///
/// Two flavours coexist:
///
/// - **Legacy single-backend** — `model:` at the top level, plus
///   `n_ctx` / `n_gpu_layers`. Implies one `kind: "llamacpp"` backend.
/// - **Multi-backend** — `backends: [...]` carries an ordered list of
///   backend entries. Router walks the list per ADR 0007.
///
/// The two are mutually exclusive at parse time: setting both is a
/// validation error. `auto_pull` and `admin_addr` apply to both
/// flavours.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigFile {
    /// When `true` and a `kind: "llamacpp"` model file is absent, the
    /// daemon downloads it from the entry's `source_url` on startup.
    /// When `false`, the daemon refuses to start with a clear error
    /// pointing at the operator's next step. Default: `true`. Applies
    /// to every llamacpp entry in `backends:`.
    #[serde(default = "default_auto_pull")]
    pub auto_pull: bool,

    /// Override for the shared model store root. When unset the
    /// daemon falls back to `MODELS_HOME` env, then the platform
    /// default. Tilde-expanded on read.
    #[serde(default)]
    pub models_home: Option<PathBuf>,

    /// Legacy single-backend model spec. Deprecated in favour of
    /// `backends:` but kept for v0.1.x config-file compatibility.
    /// Mutually exclusive with `backends:`.
    #[serde(default)]
    pub model: Option<ModelConfig>,

    /// Llama.cpp context window in tokens. Default: 8192. Used as
    /// the fallback for legacy `model:` entries; multi-backend
    /// entries carry their own `n_ctx`.
    #[serde(default = "default_n_ctx")]
    pub n_ctx: u32,

    /// Llama.cpp GPU layer offload count. 0 = CPU-only. Default: 0.
    /// Used as the fallback for legacy `model:` entries; multi-
    /// backend entries carry their own `n_gpu_layers`.
    #[serde(default)]
    pub n_gpu_layers: i32,

    /// Admin socket address. Default: platform-specific path per
    /// `docs/protocol-v1.md` §"Admin endpoint".
    #[serde(default)]
    pub admin_addr: Option<String>,

    /// Ordered list of backends (multi-backend shape). First entry
    /// is highest priority — the router tries it first, then the
    /// next, etc. Mutually exclusive with `model:`.
    #[serde(default)]
    pub backends: Option<Vec<BackendEntry>>,

    /// Optional listener overrides. Default behaviour is unchanged:
    /// the operator picks a transport via `--tcp` / `--uds` /
    /// `--pipe` on the CLI. When `listen:` is present **and** the
    /// CLI did not pass a transport flag, the daemon binds the
    /// transports declared here. CLI flags always win when both
    /// are set. Restart-time only — no config watcher.
    #[serde(default)]
    pub listen: Option<ListenConfig>,
}

/// Operator-declared listener overrides. Every field is optional.
/// TCP is **off by default** — set `tcp:` (and `tcp_v2:` if running
/// with v2) to opt in for cross-VM use cases (WSL ↔ Windows host,
/// podman-on-machine, …) where Unix sockets / named pipes don't
/// cross the boundary cleanly. Mirrors the security shape of
/// `openai-compat`: the API key is referenced by env-var **name**,
/// never embedded literally in the file.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ListenConfig {
    /// Loopback TCP bind address for the v1 inference socket, e.g.
    /// `"127.0.0.1:9090"` or `"0.0.0.0:9090"`. When unset, no v1
    /// TCP listener is bound from config (CLI `--tcp` may still
    /// provide one). v0.1 invariant: CLI mutual exclusion still
    /// applies — if CLI passes `--uds` / `--pipe`, the config
    /// `tcp:` is ignored with a one-line warning at startup.
    #[serde(default)]
    pub tcp: Option<String>,

    /// Loopback TCP bind address for the v2 inference socket. Has
    /// no effect unless `--v2` is also set on the CLI.
    #[serde(default)]
    pub tcp_v2: Option<String>,

    /// Loopback TCP bind address for the embed socket per ADR 0017.
    /// Has no effect unless `--embed` is also set on the CLI and the
    /// active backend advertises `capabilities().embed == true`.
    #[serde(default)]
    pub tcp_embed: Option<String>,

    /// **Name** of the env var carrying the pre-shared API key for
    /// TCP clients (THREAT_MODEL F-8). When set, the daemon reads
    /// the named env at startup and clients must send
    /// `{"type":"auth","key":"<value>"}` as their first NDJSON
    /// frame. UDS and named-pipe transports ignore this — kernel-
    /// attested peer credentials (F-7) gate those. CLI `--api-key`
    /// always wins when both are set.
    #[serde(default)]
    pub api_key_env: Option<String>,
}

/// A single backend declaration. Tagged on `kind:` so future
/// variants (`bedrock-converse`, …) slot in additively. Unknown
/// kinds are rejected at parse time so operators see a clear error
/// rather than a silent skip.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum BackendEntry {
    /// Local llama.cpp backend over a GGUF file in the shared CAS.
    Llamacpp(LlamacppEntry),
    /// Outbound HTTPS adapter for any provider speaking the OpenAI
    /// Chat Completions wire (OpenAI, Anthropic via the compat layer
    /// at `api.anthropic.com/v1/`, OpenRouter, vLLM, LM Studio,
    /// LocalAI, Ollama, llama.cpp's HTTP server).
    OpenaiCompat(OpenaiCompatEntry),
    /// AWS Bedrock-runtime
    /// `InvokeModelWithResponseStream` adapter
    /// (Phase 6B-5). v0.2.0 ships only the Anthropic-on-Bedrock body
    /// shape — Claude models invoked via Bedrock's pinned
    /// `anthropic_version: "bedrock-2023-05-31"` payload.
    BedrockInvoke(BedrockInvokeEntry),
}

impl BackendEntry {
    /// Operator-supplied stable identifier, used in router feedback
    /// and admin-status events.
    pub fn name(&self) -> &str {
        match self {
            BackendEntry::Llamacpp(e) => &e.name,
            BackendEntry::OpenaiCompat(e) => &e.name,
            BackendEntry::BedrockInvoke(e) => &e.name,
        }
    }
}

/// Llamacpp backend entry inside `backends:`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LlamacppEntry {
    /// Stable operator-facing identifier, e.g. `"local-gemma"`. Used
    /// in router feedback + admin events. Required to be unique
    /// across all entries.
    pub name: String,

    /// Per-entry model spec (CAS layout, ADR 0011).
    pub model: ModelConfig,

    /// Llama.cpp context window in tokens. Default: 8192.
    #[serde(default = "default_n_ctx")]
    pub n_ctx: u32,

    /// Llama.cpp GPU layer offload count. 0 = CPU-only. Default: 0.
    #[serde(default)]
    pub n_gpu_layers: i32,

    /// Opt this backend into serving embeddings per ADR 0017. When
    /// `true`, the adapter allocates a *second* `llama_context`
    /// configured with `embeddings = true` so embed requests don't
    /// race the generation context. `capabilities().embed` flips
    /// `true` accordingly. Default: `false`.
    #[serde(default)]
    pub embed: bool,

    /// Pooling strategy for the embedding context, mapped 1:1 to
    /// llama.cpp's `enum llama_pooling_type`. Most embedding models
    /// expect `1` (`LLAMA_POOLING_TYPE_MEAN`), which is the default;
    /// EmbeddingGemma 300M is in this group. Set explicitly only if
    /// the model documents a different strategy (e.g. `2` =
    /// `CLS`, `3` = `LAST`). Ignored when `embed = false`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub embed_pooling: Option<i32>,

    /// Context window for the dedicated embedding `llama_context`,
    /// in tokens. Embedding models typically have a smaller window
    /// than generation models — 2048 is the EmbeddingGemma 300M
    /// default and is what the adapter uses when this is unset.
    /// Ignored when `embed = false`.
    #[serde(default = "default_embed_n_ctx")]
    pub embed_n_ctx: u32,
}

/// OpenAI-compat backend entry inside `backends:`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenaiCompatEntry {
    /// Stable operator-facing identifier, e.g. `"anthropic-fallback"`.
    pub name: String,

    /// Base URL of the upstream, no trailing slash and no path
    /// (the adapter appends `/v1/chat/completions`). Examples:
    /// `https://api.openai.com`, `https://api.anthropic.com`,
    /// `http://localhost:11434`.
    pub base_url: String,

    /// Upstream model identifier echoed in the request `model` field.
    /// Provider-specific (e.g. `gpt-4o-mini`, `claude-opus-4-7`,
    /// `llama3.1:8b`).
    pub model: String,

    /// **Name** of the env var carrying the bearer token — never the
    /// literal token. Operators set the env separately so secrets
    /// stay out of the config file. When unset, the daemon falls
    /// back to `INFERD_OPENAI_API_KEY`, then `OPENAI_API_KEY`, then
    /// skips the `Authorization` header (some self-hosted endpoints
    /// accept unauthenticated traffic).
    #[serde(default)]
    pub api_key_env: Option<String>,

    /// Total request timeout in seconds. Default 300 (5 minutes).
    #[serde(default = "default_openai_timeout_secs")]
    pub timeout_secs: u64,
}

/// Bedrock-invoke backend entry inside `backends:`.
///
/// Auth precedence at startup (mirrors `openai-compat` env-var-by-name
/// shape so secrets stay out of the file):
///
/// 1. `bearer_token_env: "<NAME>"` — when the named env contains a
///    non-empty value, the adapter sends `Authorization: Bearer
///    <value>` and skips SigV4. Mirrors AWS' 2025-06
///    `AWS_BEARER_TOKEN_BEDROCK` rollout.
/// 2. SigV4 against the standard AWS credential chain — env vars
///    `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` (+ optional
///    `AWS_SESSION_TOKEN`). Cross-account assume-role is out of
///    scope for v0.2.0; operators set the env vars from their own
///    session before starting the daemon.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BedrockInvokeEntry {
    /// Stable operator-facing identifier, e.g. `"bedrock-claude"`.
    pub name: String,

    /// AWS region the Bedrock endpoint lives in, e.g. `"us-east-1"`.
    /// Used for both the endpoint host and SigV4 signing scope.
    pub region: String,

    /// Bedrock model id, e.g.
    /// `"anthropic.claude-3-5-sonnet-20241022-v2:0"`. URL-encoded
    /// by the adapter.
    pub model_id: String,

    /// Optional **name** of the env var carrying the Bedrock bearer
    /// token (`AWS_BEARER_TOKEN_BEDROCK` shape) — never the literal
    /// token. When the named env is non-empty, bearer auth wins and
    /// SigV4 is skipped. When unset or empty, the adapter falls back
    /// to the standard `AWS_ACCESS_KEY_ID` /
    /// `AWS_SECRET_ACCESS_KEY` chain.
    #[serde(default)]
    pub bearer_token_env: Option<String>,

    /// Optional endpoint host override. Empty/absent → default
    /// `bedrock-runtime.<region>.amazonaws.com`. Useful for VPC
    /// endpoints / integration tests.
    #[serde(default)]
    pub endpoint: Option<String>,

    /// Total request timeout in seconds. Default 300 (5 minutes).
    #[serde(default = "default_bedrock_timeout_secs")]
    pub timeout_secs: u64,
}

/// Per-model entry: pinned URL + pinned SHA-256 + name.
///
/// The shape mirrors `fetch::ModelSpec` but as a serde-deserialisable
/// config-file type. Conversion is straightforward (`From` impl below).
///
/// Note: there is no `filename` field. The blob's on-disk location
/// is derived from its SHA-256 (CAS layout, ADR 0011); the manifest
/// at `<store>/manifests/<name>.json` is the only place a name maps
/// to a blob.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelConfig {
    /// Stable identifier, e.g. `"gemma-4-e4b"`. Used as the manifest
    /// filename and the lock-file basename.
    pub name: String,
    /// Lowercase hex SHA-256 of the GGUF bytes. Required.
    pub sha256: String,
    /// Advisory total size for progress reporting + manifest.
    #[serde(default)]
    pub size_bytes: Option<u64>,
    /// Direct-download HTTPS endpoint. Must be `https://`.
    pub source_url: String,
    /// SPDX-style license id when known. Recorded in the manifest.
    #[serde(default)]
    pub license: Option<String>,
}

fn default_auto_pull() -> bool {
    true
}

fn default_n_ctx() -> u32 {
    8192
}

fn default_embed_n_ctx() -> u32 {
    2048
}

fn default_openai_timeout_secs() -> u64 {
    300
}

fn default_bedrock_timeout_secs() -> u64 {
    300
}

fn home_dir() -> Option<PathBuf> {
    #[cfg(unix)]
    {
        std::env::var_os("HOME").map(PathBuf::from)
    }
    #[cfg(not(unix))]
    {
        std::env::var_os("USERPROFILE").map(PathBuf::from)
    }
}

/// Default config-file path: `~/.inferd/config.json` on Unix /
/// `%USERPROFILE%\.inferd\config.json` on Windows. Honours
/// `INFERD_CONFIG` for tests and ops.
pub fn default_config_path() -> PathBuf {
    if let Ok(p) = std::env::var("INFERD_CONFIG") {
        return PathBuf::from(p);
    }
    let home = home_dir().unwrap_or_else(|| PathBuf::from("."));
    home.join(".inferd").join("config.json")
}

/// Errors produced by `ConfigFile::load`.
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
    /// The config file did not exist at the resolved path.
    #[error("config file not found: {0}")]
    NotFound(PathBuf),
    /// I/O error reading the file.
    #[error("io reading {path}: {source}")]
    Io {
        /// Path that failed.
        path: PathBuf,
        /// Underlying I/O error.
        #[source]
        source: io::Error,
    },
    /// JSON parse failure.
    #[error("parse {path}: {source}")]
    Parse {
        /// Path that failed.
        path: PathBuf,
        /// Underlying serde error.
        #[source]
        source: serde_json::Error,
    },
    /// Validation failure on otherwise-well-formed config.
    #[error("invalid config: {0}")]
    Invalid(String),
}

impl ConfigFile {
    /// Read + parse + validate a config file at `path`.
    pub fn load(path: &Path) -> Result<Self, ConfigError> {
        let file = File::open(path).map_err(|e| {
            if e.kind() == io::ErrorKind::NotFound {
                ConfigError::NotFound(path.to_path_buf())
            } else {
                ConfigError::Io {
                    path: path.to_path_buf(),
                    source: e,
                }
            }
        })?;
        let reader = BufReader::new(file);
        let mut cfg: ConfigFile =
            serde_json::from_reader(reader).map_err(|e| ConfigError::Parse {
                path: path.to_path_buf(),
                source: e,
            })?;
        cfg.expand_paths();
        cfg.validate()?;
        Ok(cfg)
    }

    fn expand_paths(&mut self) {
        if let Some(p) = self.models_home.as_ref()
            && let Some(stripped) = p
                .to_str()
                .and_then(|s| s.strip_prefix("~/").or_else(|| s.strip_prefix("~\\")))
            && let Some(home) = home_dir()
        {
            self.models_home = Some(home.join(stripped));
        }
    }

    fn validate(&self) -> Result<(), ConfigError> {
        match (&self.model, &self.backends) {
            (Some(_), Some(_)) => {
                return Err(ConfigError::Invalid(
                    "config: `model` and `backends` are mutually exclusive — \
                     pick one shape, not both"
                        .into(),
                ));
            }
            (None, None) => {
                return Err(ConfigError::Invalid(
                    "config: must specify either `model` (legacy single-backend) \
                     or `backends` (multi-backend list)"
                        .into(),
                ));
            }
            _ => {}
        }
        if self.n_ctx == 0 {
            return Err(ConfigError::Invalid("n_ctx must be > 0".into()));
        }
        if let Some(m) = &self.model {
            validate_model_config(m)?;
        }
        if let Some(listen) = &self.listen {
            if let Some(addr) = &listen.tcp
                && addr.trim().is_empty()
            {
                return Err(ConfigError::Invalid(
                    "listen.tcp must not be empty when set".into(),
                ));
            }
            if let Some(addr) = &listen.tcp_v2
                && addr.trim().is_empty()
            {
                return Err(ConfigError::Invalid(
                    "listen.tcp_v2 must not be empty when set".into(),
                ));
            }
            if let Some(addr) = &listen.tcp_embed
                && addr.trim().is_empty()
            {
                return Err(ConfigError::Invalid(
                    "listen.tcp_embed must not be empty when set".into(),
                ));
            }
        }
        if let Some(list) = &self.backends {
            if list.is_empty() {
                return Err(ConfigError::Invalid(
                    "backends list must not be empty".into(),
                ));
            }
            let mut seen = std::collections::HashSet::with_capacity(list.len());
            for entry in list {
                let name = entry.name();
                if name.is_empty() {
                    return Err(ConfigError::Invalid(
                        "backends[].name must not be empty".into(),
                    ));
                }
                if !seen.insert(name.to_string()) {
                    return Err(ConfigError::Invalid(format!(
                        "duplicate backends[].name {name:?} — names must be unique"
                    )));
                }
                match entry {
                    BackendEntry::Llamacpp(e) => {
                        validate_model_config(&e.model)?;
                        if e.n_ctx == 0 {
                            return Err(ConfigError::Invalid(format!(
                                "backends[{name:?}].n_ctx must be > 0"
                            )));
                        }
                    }
                    BackendEntry::OpenaiCompat(e) => {
                        if e.base_url.trim().is_empty() {
                            return Err(ConfigError::Invalid(format!(
                                "backends[{name:?}].base_url must not be empty"
                            )));
                        }
                        if !(e.base_url.starts_with("https://")
                            || e.base_url.starts_with("http://"))
                        {
                            return Err(ConfigError::Invalid(format!(
                                "backends[{name:?}].base_url must be http:// or https:// \
                                 (got {:?})",
                                e.base_url
                            )));
                        }
                        if e.model.trim().is_empty() {
                            return Err(ConfigError::Invalid(format!(
                                "backends[{name:?}].model must not be empty"
                            )));
                        }
                        if e.timeout_secs == 0 {
                            return Err(ConfigError::Invalid(format!(
                                "backends[{name:?}].timeout_secs must be > 0"
                            )));
                        }
                    }
                    BackendEntry::BedrockInvoke(e) => {
                        if e.region.trim().is_empty() {
                            return Err(ConfigError::Invalid(format!(
                                "backends[{name:?}].region must not be empty"
                            )));
                        }
                        if e.model_id.trim().is_empty() {
                            return Err(ConfigError::Invalid(format!(
                                "backends[{name:?}].model_id must not be empty"
                            )));
                        }
                        if e.timeout_secs == 0 {
                            return Err(ConfigError::Invalid(format!(
                                "backends[{name:?}].timeout_secs must be > 0"
                            )));
                        }
                    }
                }
            }
        }
        Ok(())
    }

    /// Canonical multi-backend list. When the operator wrote the
    /// legacy single-backend shape (`model:` at top level), this
    /// returns a one-element list with `kind: "llamacpp"` so the
    /// rest of the daemon only ever sees the multi-backend shape.
    pub fn resolved_backends(&self) -> Vec<BackendEntry> {
        if let Some(list) = &self.backends {
            return list.clone();
        }
        // Legacy promotion path. `validate()` ensures exactly one of
        // (`model`, `backends`) is set, so the unwrap is unreachable
        // for any value that reached this method.
        let m = self
            .model
            .as_ref()
            .expect("validate() guarantees one of model|backends is set")
            .clone();
        vec![BackendEntry::Llamacpp(LlamacppEntry {
            name: m.name.clone(),
            model: m,
            n_ctx: self.n_ctx,
            n_gpu_layers: self.n_gpu_layers,
            // Legacy single-model configs predate ADR 0017's embed
            // surface and stay generation-only. Operators wanting
            // embeddings migrate to the multi-backend `backends:`
            // shape.
            embed: false,
            embed_pooling: None,
            embed_n_ctx: default_embed_n_ctx(),
        })]
    }
}

fn validate_model_config(m: &ModelConfig) -> Result<(), ConfigError> {
    if m.name.is_empty() {
        return Err(ConfigError::Invalid("model.name must not be empty".into()));
    }
    if !m.source_url.starts_with("https://") {
        return Err(ConfigError::Invalid(format!(
            "model.source_url must be https:// (got {:?})",
            m.source_url
        )));
    }
    if m.sha256.len() != 64
        || !m
            .sha256
            .bytes()
            .all(|b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase())
    {
        return Err(ConfigError::Invalid(
            "model.sha256 must be 64 lowercase hex chars".into(),
        ));
    }
    Ok(())
}

impl From<&ModelConfig> for crate::fetch::ModelSpec {
    fn from(m: &ModelConfig) -> Self {
        crate::fetch::ModelSpec {
            name: m.name.clone(),
            source_url: m.source_url.clone(),
            sha256_hex: m.sha256.clone(),
            size_bytes: m.size_bytes,
            license: m.license.clone(),
            source: None,
        }
    }
}

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

    fn write_config(s: &str) -> tempfile::NamedTempFile {
        let mut f = tempfile::NamedTempFile::new().unwrap();
        f.write_all(s.as_bytes()).unwrap();
        f.flush().unwrap();
        f
    }

    fn good_json() -> String {
        r#"{
            "auto_pull": true,
            "models_home": "/tmp/inferd-models-home",
            "model": {
                "name": "gemma-4-e4b",
                "sha256": "30d1e7949597a3446726064e80b876fd1b5cba4aa6eec53d27afa420e731fb36",
                "size_bytes": 5126304928,
                "source_url": "https://huggingface.co/unsloth/gemma-4-E4B-it-GGUF/resolve/main/gemma-4-E4B-it-UD-Q4_K_XL.gguf",
                "license": "apache-2.0"
            },
            "n_ctx": 8192,
            "n_gpu_layers": 0
        }"#
        .to_string()
    }

    #[test]
    fn load_well_formed_config() {
        let f = write_config(&good_json());
        let cfg = ConfigFile::load(f.path()).unwrap();
        let m = cfg.model.as_ref().expect("legacy model present");
        assert_eq!(m.name, "gemma-4-e4b");
        assert_eq!(m.size_bytes, Some(5_126_304_928));
        assert_eq!(m.license.as_deref(), Some("apache-2.0"));
        assert!(cfg.auto_pull);
        assert_eq!(cfg.n_ctx, 8192);
        assert_eq!(
            cfg.models_home,
            Some(PathBuf::from("/tmp/inferd-models-home"))
        );
    }

    #[test]
    fn missing_file_returns_not_found() {
        let path = std::env::temp_dir().join("inferd-config-does-not-exist.json");
        let _ = std::fs::remove_file(&path);
        let err = ConfigFile::load(&path).unwrap_err();
        assert!(matches!(err, ConfigError::NotFound(_)));
    }

    #[test]
    fn invalid_json_returns_parse_error() {
        let f = write_config("{ not valid json");
        let err = ConfigFile::load(f.path()).unwrap_err();
        assert!(matches!(err, ConfigError::Parse { .. }));
    }

    #[test]
    fn http_url_rejected() {
        let bad = good_json().replace("https://", "http://");
        let f = write_config(&bad);
        let err = ConfigFile::load(f.path()).unwrap_err();
        match err {
            ConfigError::Invalid(msg) => assert!(msg.contains("https://")),
            other => panic!("expected Invalid, got {other:?}"),
        }
    }

    #[test]
    fn uppercase_sha_rejected() {
        let bad = good_json().replace(
            "30d1e7949597a3446726064e80b876fd1b5cba4aa6eec53d27afa420e731fb36",
            "30D1E7949597A3446726064E80B876FD1B5CBA4AA6EEC53D27AFA420E731FB36",
        );
        let f = write_config(&bad);
        let err = ConfigFile::load(f.path()).unwrap_err();
        match err {
            ConfigError::Invalid(msg) => assert!(msg.contains("lowercase hex")),
            other => panic!("expected Invalid, got {other:?}"),
        }
    }

    #[test]
    fn short_sha_rejected() {
        let bad = good_json().replace(
            "30d1e7949597a3446726064e80b876fd1b5cba4aa6eec53d27afa420e731fb36",
            "30d1e7",
        );
        let f = write_config(&bad);
        let err = ConfigFile::load(f.path()).unwrap_err();
        assert!(matches!(err, ConfigError::Invalid(_)));
    }

    #[test]
    fn defaults_when_optional_fields_missing() {
        let json = r#"{
            "model": {
                "name": "gemma-4-e4b",
                "sha256": "30d1e7949597a3446726064e80b876fd1b5cba4aa6eec53d27afa420e731fb36",
                "source_url": "https://example.com/x.gguf"
            }
        }"#;
        let f = write_config(json);
        let cfg = ConfigFile::load(f.path()).unwrap();
        let m = cfg.model.as_ref().expect("legacy model present");
        assert!(cfg.auto_pull);
        assert_eq!(cfg.n_ctx, 8192);
        assert_eq!(cfg.n_gpu_layers, 0);
        assert!(m.size_bytes.is_none());
        assert!(cfg.models_home.is_none());
        assert!(m.license.is_none());
    }

    #[test]
    fn modelconfig_converts_to_fetch_modelspec() {
        let cfg = ModelConfig {
            name: "x".into(),
            sha256: "abc".into(),
            size_bytes: Some(42),
            source_url: "https://e/x.gguf".into(),
            license: Some("mit".into()),
        };
        let spec: crate::fetch::ModelSpec = (&cfg).into();
        assert_eq!(spec.name, "x");
        assert_eq!(spec.size_bytes, Some(42));
        assert_eq!(spec.sha256_hex, "abc");
        assert_eq!(spec.license.as_deref(), Some("mit"));
    }

    fn good_multi_backend_json() -> String {
        r#"{
            "models_home": "/tmp/inferd-models-home",
            "backends": [
                {
                    "kind": "llamacpp",
                    "name": "local-gemma",
                    "model": {
                        "name": "gemma-4-e4b",
                        "sha256": "30d1e7949597a3446726064e80b876fd1b5cba4aa6eec53d27afa420e731fb36",
                        "source_url": "https://example.com/gemma.gguf"
                    },
                    "n_ctx": 8192,
                    "n_gpu_layers": 35
                },
                {
                    "kind": "openai-compat",
                    "name": "anthropic-fallback",
                    "base_url": "https://api.anthropic.com",
                    "model": "claude-opus-4-7",
                    "api_key_env": "ANTHROPIC_API_KEY"
                }
            ]
        }"#
        .to_string()
    }

    #[test]
    fn load_multi_backend_config() {
        let f = write_config(&good_multi_backend_json());
        let cfg = ConfigFile::load(f.path()).unwrap();
        assert!(cfg.model.is_none());
        let list = cfg.backends.as_ref().expect("backends present");
        assert_eq!(list.len(), 2);
        match &list[0] {
            BackendEntry::Llamacpp(e) => {
                assert_eq!(e.name, "local-gemma");
                assert_eq!(e.model.name, "gemma-4-e4b");
                assert_eq!(e.n_ctx, 8192);
                assert_eq!(e.n_gpu_layers, 35);
            }
            other => panic!("expected llamacpp, got {other:?}"),
        }
        match &list[1] {
            BackendEntry::OpenaiCompat(e) => {
                assert_eq!(e.name, "anthropic-fallback");
                assert_eq!(e.base_url, "https://api.anthropic.com");
                assert_eq!(e.model, "claude-opus-4-7");
                assert_eq!(e.api_key_env.as_deref(), Some("ANTHROPIC_API_KEY"));
                assert_eq!(e.timeout_secs, 300);
            }
            other => panic!("expected openai-compat, got {other:?}"),
        }
    }

    #[test]
    fn rejects_both_model_and_backends() {
        let json = r#"{
            "model": {
                "name": "gemma-4-e4b",
                "sha256": "30d1e7949597a3446726064e80b876fd1b5cba4aa6eec53d27afa420e731fb36",
                "source_url": "https://example.com/x.gguf"
            },
            "backends": [
                {
                    "kind": "openai-compat",
                    "name": "x",
                    "base_url": "https://api.openai.com",
                    "model": "gpt-4o-mini"
                }
            ]
        }"#;
        let f = write_config(json);
        let err = ConfigFile::load(f.path()).unwrap_err();
        match err {
            ConfigError::Invalid(msg) => assert!(msg.contains("mutually exclusive")),
            other => panic!("expected Invalid, got {other:?}"),
        }
    }

    #[test]
    fn rejects_neither_model_nor_backends() {
        let json = r#"{ "auto_pull": true }"#;
        let f = write_config(json);
        let err = ConfigFile::load(f.path()).unwrap_err();
        match err {
            ConfigError::Invalid(msg) => assert!(msg.contains("must specify either")),
            other => panic!("expected Invalid, got {other:?}"),
        }
    }

    #[test]
    fn rejects_empty_backends_list() {
        let json = r#"{ "backends": [] }"#;
        let f = write_config(json);
        let err = ConfigFile::load(f.path()).unwrap_err();
        match err {
            ConfigError::Invalid(msg) => assert!(msg.contains("must not be empty")),
            other => panic!("expected Invalid, got {other:?}"),
        }
    }

    #[test]
    fn rejects_duplicate_backend_names() {
        let json = r#"{
            "backends": [
                {
                    "kind": "openai-compat",
                    "name": "dup",
                    "base_url": "https://api.openai.com",
                    "model": "gpt-4o-mini"
                },
                {
                    "kind": "openai-compat",
                    "name": "dup",
                    "base_url": "https://api.anthropic.com",
                    "model": "claude-opus-4-7"
                }
            ]
        }"#;
        let f = write_config(json);
        let err = ConfigFile::load(f.path()).unwrap_err();
        match err {
            ConfigError::Invalid(msg) => assert!(msg.contains("duplicate")),
            other => panic!("expected Invalid, got {other:?}"),
        }
    }

    #[test]
    fn rejects_openai_compat_without_base_url() {
        let json = r#"{
            "backends": [
                {
                    "kind": "openai-compat",
                    "name": "x",
                    "base_url": "",
                    "model": "gpt-4o-mini"
                }
            ]
        }"#;
        let f = write_config(json);
        let err = ConfigFile::load(f.path()).unwrap_err();
        assert!(matches!(err, ConfigError::Invalid(_)));
    }

    #[test]
    fn rejects_openai_compat_with_bad_scheme() {
        let json = r#"{
            "backends": [
                {
                    "kind": "openai-compat",
                    "name": "x",
                    "base_url": "ftp://api.openai.com",
                    "model": "gpt-4o-mini"
                }
            ]
        }"#;
        let f = write_config(json);
        let err = ConfigFile::load(f.path()).unwrap_err();
        match err {
            ConfigError::Invalid(msg) => assert!(msg.contains("http")),
            other => panic!("expected Invalid, got {other:?}"),
        }
    }

    #[test]
    fn accepts_openai_compat_with_localhost_http() {
        let json = r#"{
            "backends": [
                {
                    "kind": "openai-compat",
                    "name": "ollama",
                    "base_url": "http://localhost:11434",
                    "model": "llama3.1:8b"
                }
            ]
        }"#;
        let f = write_config(json);
        let cfg = ConfigFile::load(f.path()).unwrap();
        assert_eq!(cfg.resolved_backends().len(), 1);
    }

    #[test]
    fn rejects_unknown_kind() {
        let json = r#"{
            "backends": [
                {
                    "kind": "future-thing-not-supported",
                    "name": "x"
                }
            ]
        }"#;
        let f = write_config(json);
        let err = ConfigFile::load(f.path()).unwrap_err();
        assert!(matches!(err, ConfigError::Parse { .. }));
    }

    #[test]
    fn loads_bedrock_invoke_entry() {
        let json = r#"{
            "backends": [
                {
                    "kind": "bedrock-invoke",
                    "name": "bedrock-claude",
                    "region": "us-east-1",
                    "model_id": "anthropic.claude-3-5-sonnet-20241022-v2:0",
                    "bearer_token_env": "AWS_BEARER_TOKEN_BEDROCK"
                }
            ]
        }"#;
        let f = write_config(json);
        let cfg = ConfigFile::load(f.path()).unwrap();
        let list = cfg.backends.as_ref().unwrap();
        assert_eq!(list.len(), 1);
        match &list[0] {
            BackendEntry::BedrockInvoke(e) => {
                assert_eq!(e.name, "bedrock-claude");
                assert_eq!(e.region, "us-east-1");
                assert_eq!(e.model_id, "anthropic.claude-3-5-sonnet-20241022-v2:0");
                assert_eq!(
                    e.bearer_token_env.as_deref(),
                    Some("AWS_BEARER_TOKEN_BEDROCK")
                );
                assert!(e.endpoint.is_none());
                assert_eq!(e.timeout_secs, 300);
            }
            other => panic!("expected bedrock-invoke, got {other:?}"),
        }
    }

    #[test]
    fn rejects_bedrock_invoke_without_region() {
        let json = r#"{
            "backends": [
                {
                    "kind": "bedrock-invoke",
                    "name": "x",
                    "region": "",
                    "model_id": "anthropic.claude-3-5-sonnet-20241022-v2:0"
                }
            ]
        }"#;
        let f = write_config(json);
        let err = ConfigFile::load(f.path()).unwrap_err();
        match err {
            ConfigError::Invalid(msg) => assert!(msg.contains("region")),
            other => panic!("expected Invalid, got {other:?}"),
        }
    }

    #[test]
    fn rejects_bedrock_invoke_without_model_id() {
        let json = r#"{
            "backends": [
                {
                    "kind": "bedrock-invoke",
                    "name": "x",
                    "region": "us-east-1",
                    "model_id": ""
                }
            ]
        }"#;
        let f = write_config(json);
        let err = ConfigFile::load(f.path()).unwrap_err();
        match err {
            ConfigError::Invalid(msg) => assert!(msg.contains("model_id")),
            other => panic!("expected Invalid, got {other:?}"),
        }
    }

    #[test]
    fn legacy_model_promotes_to_one_backend() {
        let f = write_config(&good_json());
        let cfg = ConfigFile::load(f.path()).unwrap();
        let resolved = cfg.resolved_backends();
        assert_eq!(resolved.len(), 1);
        match &resolved[0] {
            BackendEntry::Llamacpp(e) => {
                assert_eq!(e.name, "gemma-4-e4b");
                assert_eq!(e.n_ctx, 8192);
                assert_eq!(e.n_gpu_layers, 0);
            }
            other => panic!("expected llamacpp, got {other:?}"),
        }
    }

    #[test]
    fn multi_backend_resolved_passes_through() {
        let f = write_config(&good_multi_backend_json());
        let cfg = ConfigFile::load(f.path()).unwrap();
        let resolved = cfg.resolved_backends();
        assert_eq!(resolved.len(), 2);
        assert_eq!(resolved[0].name(), "local-gemma");
        assert_eq!(resolved[1].name(), "anthropic-fallback");
    }

    #[test]
    fn listen_block_absent_by_default() {
        let f = write_config(&good_json());
        let cfg = ConfigFile::load(f.path()).unwrap();
        assert!(cfg.listen.is_none());
    }

    #[test]
    fn listen_block_carries_tcp_and_api_key_env() {
        let json = r#"{
            "model": {
                "name": "gemma-4-e4b",
                "sha256": "30d1e7949597a3446726064e80b876fd1b5cba4aa6eec53d27afa420e731fb36",
                "source_url": "https://example.com/x.gguf"
            },
            "listen": {
                "tcp": "127.0.0.1:9090",
                "tcp_v2": "127.0.0.1:9091",
                "api_key_env": "INFERD_TCP_API_KEY"
            }
        }"#;
        let f = write_config(json);
        let cfg = ConfigFile::load(f.path()).unwrap();
        let listen = cfg.listen.as_ref().expect("listen present");
        assert_eq!(listen.tcp.as_deref(), Some("127.0.0.1:9090"));
        assert_eq!(listen.tcp_v2.as_deref(), Some("127.0.0.1:9091"));
        assert_eq!(listen.api_key_env.as_deref(), Some("INFERD_TCP_API_KEY"));
    }

    #[test]
    fn llamacpp_entry_embed_defaults_off() {
        let f = write_config(&good_multi_backend_json());
        let cfg = ConfigFile::load(f.path()).unwrap();
        let list = cfg.backends.as_ref().unwrap();
        match &list[0] {
            BackendEntry::Llamacpp(e) => {
                assert!(!e.embed);
                assert!(e.embed_pooling.is_none());
                assert_eq!(e.embed_n_ctx, 2048);
            }
            other => panic!("expected llamacpp, got {other:?}"),
        }
    }

    #[test]
    fn llamacpp_entry_carries_embed_fields() {
        let json = r#"{
            "backends": [
                {
                    "kind": "llamacpp",
                    "name": "embeddings",
                    "embed": true,
                    "embed_pooling": 1,
                    "embed_n_ctx": 1024,
                    "model": {
                        "name": "embeddinggemma-300m",
                        "sha256": "30d1e7949597a3446726064e80b876fd1b5cba4aa6eec53d27afa420e731fb36",
                        "source_url": "https://example.com/embed.gguf"
                    }
                }
            ]
        }"#;
        let f = write_config(json);
        let cfg = ConfigFile::load(f.path()).unwrap();
        let list = cfg.backends.as_ref().unwrap();
        match &list[0] {
            BackendEntry::Llamacpp(e) => {
                assert!(e.embed);
                assert_eq!(e.embed_pooling, Some(1));
                assert_eq!(e.embed_n_ctx, 1024);
            }
            other => panic!("expected llamacpp, got {other:?}"),
        }
    }

    #[test]
    fn legacy_promotion_keeps_embed_off() {
        let f = write_config(&good_json());
        let cfg = ConfigFile::load(f.path()).unwrap();
        let list = cfg.resolved_backends();
        match &list[0] {
            BackendEntry::Llamacpp(e) => {
                assert!(!e.embed);
                assert!(e.embed_pooling.is_none());
                assert_eq!(e.embed_n_ctx, 2048);
            }
            other => panic!("expected llamacpp, got {other:?}"),
        }
    }

    #[test]
    fn listen_rejects_empty_tcp() {
        let json = r#"{
            "model": {
                "name": "gemma-4-e4b",
                "sha256": "30d1e7949597a3446726064e80b876fd1b5cba4aa6eec53d27afa420e731fb36",
                "source_url": "https://example.com/x.gguf"
            },
            "listen": { "tcp": "   " }
        }"#;
        let f = write_config(json);
        let err = ConfigFile::load(f.path()).unwrap_err();
        match err {
            ConfigError::Invalid(msg) => assert!(msg.contains("listen.tcp")),
            other => panic!("expected Invalid, got {other:?}"),
        }
    }
}